Skip to content

Commit

Permalink
Added test for composed model deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
tomtit committed Jun 3, 2017
1 parent 000bee0 commit 9fec596
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.swagger.inflector.utils.ApiException;
import io.swagger.sample.models.Dog;
import io.swagger.test.models.Address;
import io.swagger.test.models.ExtendedAddress;
import io.swagger.test.models.User;

import javax.ws.rs.core.MediaType;
Expand Down Expand Up @@ -92,6 +93,13 @@ public ResponseContext withModelArray(RequestContext request, String id, Address
.status(Status.OK);
}

public ResponseContext withValidComposedModel(RequestContext request, ExtendedAddress body) {
if (body == null) {
throw new NullPointerException();
}
return new ResponseContext().status(Status.OK);
}

public ResponseContext arrayInputTest(RequestContext request, List<String> users) {
return new ResponseContext()
.status(Status.OK)
Expand Down
39 changes: 31 additions & 8 deletions src/test/java/io/swagger/test/integration/RequestTestIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
import io.swagger.test.models.Address;
import io.swagger.test.models.ExtendedAddress;
import org.joda.time.DateTime;
import org.testng.Assert;
import org.testng.annotations.Test;

import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import java.net.HttpURLConnection;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -72,18 +74,39 @@ public void verifyArrayModelMapping() throws Exception {
"application/json", null, new String[0]);
}

@Test(expectedExceptions = ApiException.class)
@Test
public void verifyComposedModelValidation() throws Exception {
client.invokeAPI("/withComposedModel", "POST", new HashMap<String, String>(),
new ExtendedAddress(), new HashMap<String, String>(), null, "application/json",
null, new String[0]);
try {
client.invokeAPI("/withInvalidComposedModel", "POST", new HashMap<String, String>(),
new ExtendedAddress(), new HashMap<String, String>(), null, "application/json",
null, new String[0]);
Assert.fail("No exception was thrown");
} catch (ApiException e) {
Assert.assertEquals(e.getCode(), HttpURLConnection.HTTP_BAD_REQUEST);
}
}

@Test(expectedExceptions = ApiException.class)
@Test()
public void verifyComposedModelArrayValidation() throws Exception {
client.invokeAPI("/withComposedModelArray", "POST", new HashMap<String, String>(),
Arrays.asList(new ExtendedAddress(), new ExtendedAddress()),
new HashMap<String, String>(), null, "application/json", null, new String[0]);
try {
client.invokeAPI("/withInvalidComposedModelArray", "POST", new HashMap<String, String>(),

Arrays.asList(new ExtendedAddress(), new ExtendedAddress()),
new HashMap<String, String>(), null, "application/json", null, new String[0]);
Assert.fail("No exception was thrown");
} catch (ApiException e) {
Assert.assertEquals(e.getCode(), HttpURLConnection.HTTP_BAD_REQUEST);
}
}

@Test
public void verifyComposedModelDeserialization() throws Exception {
final ExtendedAddress body = new ExtendedAddress();
body.setGps("gps");
body.setStreet("street");
client.invokeAPI("/withValidComposedModel", "POST", new HashMap<String, String>(),
body, new HashMap<String, String>(), null, MediaType.APPLICATION_JSON,
MediaType.APPLICATION_JSON, new String[0]);
}

@Test
Expand Down
20 changes: 16 additions & 4 deletions src/test/swagger/sample1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -613,9 +613,9 @@ paths:
description: 'success!'
schema:
$ref: '#/definitions/Address'
'/withComposedModel':
'/withInvalidComposedModel':
post:
operationId: withComposedModel
operationId: withInvalidComposedModel
x-swagger-router-controller: TestController
parameters:
- name: body
Expand All @@ -625,9 +625,21 @@ paths:
responses:
'200':
description: 'success!'
'/withComposedModelArray':
'/withValidComposedModel':
post:
operationId: withComposedModelArray
operationId: withValidComposedModel
x-swagger-router-controller: TestController
parameters:
- name: body
in: body
schema:
$ref: '#/definitions/ExtendedAddress'
responses:
'200':
description: 'success!'
'/withInvalidComposedModelArray':
post:
operationId: withInvalidComposedModelArray
x-swagger-router-controller: TestController
parameters:
- name: body
Expand Down

0 comments on commit 9fec596

Please sign in to comment.