From 6cc300edaa7416682e2e66b27d4b3945389d5ead Mon Sep 17 00:00:00 2001 From: Alexey Nechaev Date: Tue, 20 Jun 2017 15:30:16 +0300 Subject: [PATCH] Added the 'ResolverUtilTest.testRequiredProperty' test method --- .../swagger/test/utils/ResolverUtilTest.java | 41 ++++++++++++++ .../io/swagger/test/utils/shared-model.yml | 54 +++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 src/test/resources/io/swagger/test/utils/shared-model.yml diff --git a/src/test/java/io/swagger/test/utils/ResolverUtilTest.java b/src/test/java/io/swagger/test/utils/ResolverUtilTest.java index 62f0faed..76707ff7 100644 --- a/src/test/java/io/swagger/test/utils/ResolverUtilTest.java +++ b/src/test/java/io/swagger/test/utils/ResolverUtilTest.java @@ -13,6 +13,9 @@ import io.swagger.parser.SwaggerParser; import io.swagger.sample.models.Dog; import io.swagger.util.Json; +import org.apache.commons.io.IOUtils; +import org.testng.Assert; +import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import static org.testng.Assert.assertNotNull; @@ -20,7 +23,12 @@ import static org.testng.Assert.fail; import static org.testng.AssertJUnit.assertEquals; +import java.io.IOException; +import java.nio.charset.StandardCharsets; + public class ResolverUtilTest { + private static final String REQUIRED_PROPERTY = "requiredProperty"; + @Test public void testArrayParam() { Swagger swagger = new SwaggerParser().read("./src/test/swagger/sample1.yaml"); @@ -197,4 +205,37 @@ public void testSelfReferenceResolution() { fail("Recursive loop found"); } } + + @Test(dataProvider = REQUIRED_PROPERTY) + public void testRequiredProperty(String yml, final String model, final String property, + final boolean required) throws Exception { + final Swagger swagger = new SwaggerParser().parse(yml); + final Runnable checker = new Runnable() { + @Override + public void run() { + final boolean actual = swagger.getDefinitions().get(model).getProperties() + .get(property).getRequired(); + if (required) { + Assert.assertTrue(actual, + String.format("The %s.%s is mandatory", model, property)); + } else { + Assert.assertFalse(actual, + String.format("The %s.%s is optional", model, property)); + } + } + }; + checker.run(); + new ResolverUtil().resolveFully(swagger); + checker.run(); + } + + @DataProvider(name = REQUIRED_PROPERTY) + private static Object[][] listRequiredProperties() throws IOException { + final String yml = IOUtils.toString(ResolverUtilTest.class.getResource("shared-model.yml"), + StandardCharsets.UTF_8); + return new Object[][] { + {yml, "One", "nested", false}, + {yml, "Two", "nested", true} + }; + } } diff --git a/src/test/resources/io/swagger/test/utils/shared-model.yml b/src/test/resources/io/swagger/test/utils/shared-model.yml new file mode 100644 index 00000000..21c3e7eb --- /dev/null +++ b/src/test/resources/io/swagger/test/utils/shared-model.yml @@ -0,0 +1,54 @@ +swagger: '2.0' +info: + description: Test + version: "1" + title: test +paths: + '/one': + put: + operationId: one + parameters: + - in: body + name: body + required: false + schema: + $ref: '#/definitions/One' + responses: + default: + description: successful operation + '/two': + post: + operationId: two + parameters: + - in: body + name: body + required: false + schema: + $ref: '#/definitions/Two' + responses: + default: + description: successful operation +definitions: + Shared: + type: object + required: + - type + properties: + type: + type: string + One: + type: object + properties: + nested: + $ref: '#/definitions/Shared' + one: + type: string + Two: + type: object + required: + - nested + properties: + nested: + $ref: '#/definitions/Shared' + two: + type: string