@@ -67,47 +67,6 @@ class RestTestCaseWriter : HttpWsTestCaseWriter {
6767 testName : String
6868 ) {
6969 super .handleTestInitialization(lines, baseUrlOfSut, ind, insertionVars,testName)
70-
71- if (hasChainedLocations(ind.individual)) {
72- assert (ind.individual is RestIndividual )
73- /*
74- If the "location" header of a HTTP response is used in a following
75- call, we need to save it in a variable.
76- We declare all such variables at the beginning of the test.
77-
78- TODO: rather declare variable first time we access it?
79- Yes! can use location index for unique name
80- FIXME: refactor
81- */
82- lines.addEmpty()
83-
84- ind.evaluatedMainActions().asSequence()
85- .map { it.action }
86- .filterIsInstance(RestCallAction ::class .java)
87- /*
88- FIXME postLocationId() is not guaranteed to be unique...
89- in fitness function it works because we handle it by taking last definition,
90- but, here, if we refactor to declare it on its first use, we might end up with
91- variable name clashes, unless we change the id to consider the action index, somehow
92- */
93- .filter { it.saveCreatedResourceLocation }
94- .map { it.creationLocationId() }
95- // .filter { it.usePreviousLocationId != null }
96- // .map { it.usePreviousLocationId }
97- .distinct()
98- .forEach { id ->
99- val name = locationVar(id!! )
100- when {
101- format.isJava() -> lines.add(" String $name = \"\" ;" )
102- format.isKotlin() -> lines.add(" var $name : String? = \"\" " )
103- format.isJavaScript() -> lines.add(" let $name = \"\" ;" )
104- format.isCsharp() -> lines.add(" var $name = \"\" ;" )
105- format.isPython() -> {} // no need to declare variables
106- // should never happen
107- else -> throw IllegalStateException (" Unsupported format $format " )
108- }
109- }
110- }
11170 }
11271
11372 override fun handleActionCalls (
@@ -149,9 +108,12 @@ class RestTestCaseWriter : HttpWsTestCaseWriter {
149108 }
150109
151110 protected fun locationVar (id : String ): String {
111+ /*
112+ Ids are supposed to be unique, but might have invalid characters for a variable
113+ */
152114 // TODO make sure name is syntactically valid
153- // TODO use counters to make them unique
154- return " location_${id.trim().replace(" " , " _" )} "
115+
116+ return " location_${id.trim().replace(" " , " _" ).replace( Individual . LOCAL_ID_PREFIX_ACTION , " " ) } "
155117 }
156118
157119
@@ -421,12 +383,17 @@ class RestTestCaseWriter : HttpWsTestCaseWriter {
421383 when {
422384 format.isJavaOrKotlin() -> {
423385 val extract = " $resVarName .extract().header(\" location\" )"
424- lines.add(" $location = $extract " )
386+ if (format.isJava()){
387+ lines.add(" String " )
388+ } else {
389+ lines.add(" val " )
390+ }
391+ lines.append(" $location = $extract " )
425392 lines.appendSemicolon()
426393 lines.add(" assertTrue(isValidURIorEmpty($location ));" )
427394 }
428395 format.isJavaScript() -> {
429- lines.add(" $location = $resVarName .header['location'];" )
396+ lines.add(" const $location = $resVarName .header['location'];" )
430397 val validCheck = " ${TestSuiteWriter .jsImport} .isValidURIorEmpty($location )"
431398 lines.add(" expect($validCheck ).toBe(true);" )
432399 }
@@ -464,6 +431,12 @@ class RestTestCaseWriter : HttpWsTestCaseWriter {
464431 else -> throw IllegalStateException (" Unhandled format: $format " )
465432 }
466433
434+ when {
435+ format.isJavaScript() -> lines.add(" const " )
436+ format.isJava() -> lines.add(" String " )
437+ format.isKotlin() -> lines.add(" val " )
438+ format.isPython() -> { /* nothing to do in Python */ }
439+ }
467440 lines.add(" ${locationVar(call.creationLocationId())} = $baseUri + \" /\" + $extract " )
468441 lines.appendSemicolon()
469442 }
0 commit comments