Skip to content

Commit 7554d2f

Browse files
authored
Merge pull request #699 from EMResearch/issue-698
Issue 698
2 parents 0ce58c8 + 5ba32e3 commit 7554d2f

File tree

5 files changed

+38
-11
lines changed

5 files changed

+38
-11
lines changed

core/src/main/kotlin/org/evomaster/core/EMConfig.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,13 @@ class EMConfig {
260260
They can be check only once all fields have been updated
261261
*/
262262

263+
if(!blackBox && bbSwaggerUrl.isNotBlank()){
264+
throw IllegalArgumentException("'bbSwaggerUrl' should be set only in black-box mode")
265+
}
266+
if(!blackBox && bbTargetUrl.isNotBlank()){
267+
throw IllegalArgumentException("'bbTargetUrl' should be set only in black-box mode")
268+
}
269+
263270
if (blackBox && !bbExperiments) {
264271

265272
if(problemType == ProblemType.DEFAULT){

core/src/main/kotlin/org/evomaster/core/output/TestSuiteSplitter.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,11 @@ object TestSuiteSplitter {
278278
* if it contains the field "errors" in its body.
279279
*/
280280
fun assessFailed(result: GraphQlCallResult): Boolean{
281-
val resultBody = Gson().fromJson(result.getBody(), HashMap::class.java)
281+
val resultBody = try {
282+
Gson().fromJson(result.getBody(), HashMap::class.java)
283+
} catch (e : JsonSyntaxException){
284+
return true //TODO should this be treated specially???
285+
}
282286
val errMsg = resultBody?.get("errors")
283287
return (resultBody!=null && errMsg != null)
284288
}

core/src/main/kotlin/org/evomaster/core/output/oracles/SchemaOracle.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.evomaster.core.output.oracles
22

33
import com.google.gson.Gson
4+
import com.google.gson.JsonSyntaxException
45
import io.swagger.v3.oas.models.media.*
56
import org.evomaster.core.logging.LoggingUtil
67
import org.evomaster.core.output.Lines
@@ -342,7 +343,12 @@ class SchemaOracle : ImplementedOracle() {
342343
var supported = true
343344

344345
if (res.getBodyType()?.isCompatible(MediaType.APPLICATION_JSON_TYPE) == true){
345-
val actualObject = Gson().fromJson(res.getBody(), Object::class.java)
346+
val actualObject = try {
347+
Gson().fromJson(res.getBody(), Object::class.java)
348+
} catch (e: JsonSyntaxException) {
349+
return false
350+
}
351+
346352
if (actualObject is Map<*,*>)
347353
supported = supportedObject(actualObject, call)
348354
else if (actualObject is List<*>

core/src/main/kotlin/org/evomaster/core/output/service/ApiTestCaseWriter.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.evomaster.core.output.service
22

33
import com.google.gson.Gson
4+
import com.google.gson.JsonSyntaxException
45
import org.evomaster.core.database.DbAction
56
import org.evomaster.core.database.DbActionResult
67
import org.evomaster.core.output.CookieWriter
@@ -68,14 +69,22 @@ abstract class ApiTestCaseWriter : TestCaseWriter() {
6869
when (bodyString?.trim()?.first()) {
6970
//TODO this should be handled recursively, and not ad-hoc here...
7071
'[' -> {
72+
try{
7173
// This would be run if the JSON contains an array of objects.
7274
val list = Gson().fromJson(bodyString, List::class.java)
7375
handleAssertionsOnList(list, lines, "", bodyVarName)
76+
} catch (e: JsonSyntaxException) {
77+
lines.add("/* Failed to parse JSON response */")
78+
}
7479
}
7580
'{' -> {
7681
// JSON contains an object
77-
val resContents = Gson().fromJson(bodyString, Map::class.java)
78-
handleAssertionsOnObject(resContents as Map<String, *>, lines, "", bodyVarName)
82+
try {
83+
val resContents = Gson().fromJson(bodyString, Map::class.java)
84+
handleAssertionsOnObject(resContents as Map<String, *>, lines, "", bodyVarName)
85+
} catch (e: JsonSyntaxException) {
86+
lines.add("/* Failed to parse JSON response */")
87+
}
7988
}
8089
'"' -> {
8190
lines.add(bodyIsString(bodyString, GeneUtils.EscapeMode.BODY, bodyVarName))

core/src/test/kotlin/org/evomaster/core/EMConfigTest.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,31 +138,32 @@ internal class EMConfigTest{
138138
}
139139

140140

141-
@ParameterizedTest
142-
@ValueSource(strings = ["bbSwaggerUrl", "bbTargetUrl"])
143-
fun testUrl(name: String){
141+
@Test
142+
fun testUrl(){
143+
144+
val name = "bbSwaggerUrl"
144145

145146
val parser = EMConfig.getOptionParser()
146147
val opt = parser.recognizedOptions()[name] ?:
147148
throw Exception("Cannot find option")
148149
val config = EMConfig()
149150

150151
val ok = "http://localhost:8080"
151-
var options = parser.parse("--$name", ok)
152+
var options = parser.parse("--$name", ok, "--blackBox","true","--outputFormat","JAVA_JUNIT_4")
152153
assertEquals(ok, opt.value(options))
153154
config.updateProperties(options) // no exception
154155

155156
val noPort = "http://localhost"
156-
options = parser.parse("--$name", noPort)
157+
options = parser.parse("--$name", noPort, "--blackBox","true","--outputFormat","JAVA_JUNIT_4")
157158
assertEquals(noPort, opt.value(options))
158159
config.updateProperties(options) // no exception
159160

160161
val wrong = "foobar"
161-
options = parser.parse("--$name", wrong)
162+
options = parser.parse("--$name", wrong, "--blackBox","true","--outputFormat","JAVA_JUNIT_4")
162163
assertThrows(Exception::class.java, {config.updateProperties(options)})
163164

164165
val noProtocol = "localhost:8080"
165-
options = parser.parse("--$name", noProtocol)
166+
options = parser.parse("--$name", noProtocol, "--blackBox","true","--outputFormat","JAVA_JUNIT_4")
166167
assertThrows(Exception::class.java, {config.updateProperties(options)})
167168
}
168169

0 commit comments

Comments
 (0)