Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the 'ResolverUtilTest.testRequiredProperty' test method #199

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/test/java/io/swagger/test/utils/ResolverUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 io.swagger.util.Yaml;
import org.testng.annotations.Test;

Expand All @@ -21,7 +24,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
Expand Down Expand Up @@ -66,6 +74,7 @@ public void testCircularRefs() {
}



@Test
public void testArrayParam() {
Swagger swagger = new SwaggerParser().read("./src/test/swagger/sample1.yaml");
Expand Down Expand Up @@ -282,6 +291,39 @@ public void testSelfReferenceResolution() {
}


@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}
};


@Test
public void testResolvingWithoutDefinitions() {
String yaml =
Expand All @@ -301,5 +343,6 @@ public void testResolvingWithoutDefinitions() {

Swagger swagger = new SwaggerParser().parse(yaml);
new ResolverUtil().resolveFully(swagger);

}
}
54 changes: 54 additions & 0 deletions src/test/resources/io/swagger/test/utils/shared-model.yml
Original file line number Diff line number Diff line change
@@ -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