Skip to content
This repository was archived by the owner on Jan 11, 2019. It is now read-only.

Commit 61a5f10

Browse files
author
Ole Lensmar
authored
Merge pull request #58 from SmartBear/swagger-compliance-assertion-improvements
Swagger compliance assertion improvements
2 parents dc39ff2 + d2f4ce3 commit 61a5f10

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<groupId>com.smartbear.soapui.plugins</groupId>
1010
<artifactId>soapui-swagger-plugin</artifactId>
11-
<version>2.5.2</version>
11+
<version>2.5.3-SNAPSHOT</version>
1212

1313
<name>Swagger Plugin</name>
1414
<description>Provides Swagger 1.X/2.0 import/export functionality for REST APIs</description>
@@ -53,7 +53,7 @@
5353
<properties>
5454
<jackson-version>2.8.4</jackson-version>
5555
<swagger-version>1.5.12</swagger-version>
56-
<swagger-parser.version>1.0.25</swagger-parser.version>
56+
<swagger-parser.version>1.0.26</swagger-parser.version>
5757
<swagger-inflector.version>1.0.11</swagger-inflector.version>
5858
<readyapi-version>1.9.0</readyapi-version>
5959
</properties>

src/main/groovy/com/smartbear/swagger/SwaggerComplianceAssertion.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import com.eviware.x.form.XFormDialogBuilder;
4141
import com.eviware.x.form.XFormFactory;
4242
import com.fasterxml.jackson.databind.JsonNode;
43+
import com.fasterxml.jackson.databind.ObjectMapper;
44+
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
4345
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
4446
import com.github.fge.jsonschema.core.load.configuration.LoadingConfiguration;
4547
import com.github.fge.jsonschema.main.JsonSchema;
@@ -192,7 +194,7 @@ private boolean validateMessage(HttpMessageExchange messageExchange, SubmitConte
192194
Operation operation = findOperation(swagger.getPath(swaggerPath), method);
193195
if (operation != null) {
194196
validateOperation(swagger, operation, String.valueOf(messageExchange.getResponseStatusCode()),
195-
messageExchange.getResponseContent()
197+
messageExchange.getResponseContent(), messageExchange.getResponseContentType()
196198
);
197199

198200
return true;
@@ -228,31 +230,31 @@ private Operation findOperation(Path path, RestRequestInterface.HttpMethod metho
228230
return null;
229231
}
230232

231-
void validateOperation(Swagger swagger, Operation operation, String responseCode, String contentAsString) throws AssertionException {
233+
void validateOperation(Swagger swagger, Operation operation, String responseCode, String contentAsString, String contentType) throws AssertionException {
232234

233235
Response responseSchema = operation.getResponses().get(responseCode);
234236
if (responseSchema == null) {
235237
responseSchema = operation.getResponses().get("default");
236238
}
237239

238240
if (responseSchema != null) {
239-
validateResponse(contentAsString, swagger, responseSchema);
241+
validateResponse(contentAsString, contentType, swagger, responseSchema);
240242
} else if (strictMode) {
241243
throw new AssertionException(new AssertionError(
242244
"Missing response definition for " + responseCode + " response in operation " + operation.getOperationId()));
243245
}
244246
}
245247

246-
void validateResponse(String contentAsString, Swagger swagger, Response responseSchema) throws AssertionException {
248+
void validateResponse(String contentAsString, String contentType, Swagger swagger, Response responseSchema) throws AssertionException {
247249
if (responseSchema.getSchema() != null) {
248250
Property schema = responseSchema.getSchema();
249251
if (schema instanceof RefProperty) {
250252
Model model = swagger.getDefinitions().get(((RefProperty) schema).getSimpleRef());
251253
if (model != null) {
252-
validatePayload(contentAsString, null);
254+
validatePayload(contentAsString, null, contentType );
253255
}
254256
} else {
255-
validatePayload(contentAsString, Json.pretty(schema));
257+
validatePayload(contentAsString, Json.pretty(schema), contentType);
256258
}
257259
}
258260
}
@@ -309,7 +311,7 @@ private Swagger parseFileContent() throws AssertionException {
309311
}
310312
}
311313

312-
public void validatePayload(String payload, String schema) throws AssertionException {
314+
public void validatePayload(String payload, String schema, String contentType) throws AssertionException {
313315
try {
314316
JsonSchema jsonSchema;
315317

@@ -328,7 +330,16 @@ public void validatePayload(String payload, String schema) throws AssertionExcep
328330
jsonSchema = getSwaggerSchema();
329331
}
330332

331-
JsonNode contentObject = Json.mapper().readTree(payload);
333+
JsonNode contentObject;
334+
335+
if (contentType.equalsIgnoreCase("application/json")) {
336+
contentObject = Json.mapper().readTree(payload);
337+
} else if (contentType.equalsIgnoreCase("application/xml")) {
338+
final XmlMapper xmlMapper = new XmlMapper();
339+
contentObject = xmlMapper.readTree(payload);
340+
} else {
341+
throw new AssertionException(new AssertionError("Swagger Compliance testing failed. Invalid content type: " + contentType));
342+
}
332343

333344
ValidationSupport.validateMessage(jsonSchema, contentObject);
334345
} catch (AssertionException e) {

src/test/java/com/smartbear/swagger/TestComplianceAssertion.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,15 @@ public void testComplianceAssertion() throws Exception {
5454
SwaggerComplianceAssertion assertion = new SwaggerComplianceAssertion(config, testStep);
5555
Swagger swagger = assertion.getSwagger(new WsdlSubmitContext(project));
5656

57-
assertion.validateOperation(swagger, swagger.getPath("/pet/findByTags").getGet(), "200", "[{\"id\":1500,\"category\":{\"id\":0,\"name\":\"\"},\"name\":\"butch\",\"photoUrls\":[\"\"],\"tags\":[{\"id\":0,\"name\":\"\"}],\"status\":\"available\"}]");
57+
assertion.validateOperation(swagger, swagger.getPath("/pet/findByTags").getGet(), "200",
58+
"[{\"id\":1500,\"category\":{\"id\":0,\"name\":\"\"},\"name\":\"butch\",\"photoUrls\":[\"\"],\"tags\":[{\"id\":0,\"name\":\"\"}],\"status\":\"available\"}]",
59+
"application/json");
5860

5961
try {
60-
assertion.validateOperation(swagger, swagger.getPath("/pet/findByTags").getGet(), "200", "[{\"id\":1500,\"category\":{\"id\":0,\"name\":\"\"},\"name\":\"butch\",\"photoUrdls\":[\"\"],\"tags\":[{\"id\":0,\"name\":\"\"}],\"status\":\"available\"}]");
62+
assertion.validateOperation(swagger, swagger.getPath("/pet/findByTags").getGet(), "200",
63+
"[{\"id\":1500,\"category\":{\"id\":0,\"name\":\"\"},\"name\":\"butch\",\"photoUrdls\":[\"\"],\"tags\":[{\"id\":0,\"name\":\"\"}],\"status\":\"available\"}]",
64+
"application/json");
65+
6166
assertTrue("Validation should have failed", false);
6267
} catch (Exception e) {
6368
}

0 commit comments

Comments
 (0)