Skip to content

Commit ea1c280

Browse files
committed
Merge pull request #1329 from iushankin/issue-1304-part3
Fixed #1304: Migrate tests from the Scala to the plain Java. Part 3
2 parents 6cb4473 + 4b62a94 commit ea1c280

File tree

13 files changed

+886
-952
lines changed

13 files changed

+886
-952
lines changed

modules/swagger-codegen/pom.xml

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -70,42 +70,6 @@
7070
</execution>
7171
</executions>
7272
</plugin>
73-
<plugin>
74-
<groupId>net.alchim31.maven</groupId>
75-
<artifactId>scala-maven-plugin</artifactId>
76-
<executions>
77-
<execution>
78-
<goals>
79-
<goal>add-source</goal>
80-
<goal>compile</goal>
81-
<goal>testCompile</goal>
82-
</goals>
83-
</execution>
84-
</executions>
85-
<configuration>
86-
<recompileMode>incremental</recompileMode>
87-
<jvmArgs>
88-
<jvmArg>-Xmx384m</jvmArg>
89-
</jvmArgs>
90-
<args>
91-
<arg>-target:jvm-1.6</arg>
92-
<arg>-deprecation</arg>
93-
</args>
94-
<launchers>
95-
<launcher>
96-
<id>run-scalatest</id>
97-
<mainClass>org.scalatest.tools.Runner</mainClass>
98-
<args>
99-
<arg>-p</arg>
100-
<arg>${project.build.testOutputDirectory}</arg>
101-
</args>
102-
<jvmArgs>
103-
<jvmArg>-Xmx512m</jvmArg>
104-
</jvmArgs>
105-
</launcher>
106-
</launchers>
107-
</configuration>
108-
</plugin>
10973
<plugin>
11074
<artifactId>maven-compiler-plugin</artifactId>
11175
<version>3.0</version>
@@ -140,59 +104,13 @@
140104
<version>2.1</version>
141105
</plugin>
142106
</plugins>
143-
<pluginManagement>
144-
<plugins>
145-
<plugin>
146-
<groupId>net.alchim31.maven</groupId>
147-
<artifactId>scala-maven-plugin</artifactId>
148-
<version>${scala-maven-plugin-version}</version>
149-
</plugin>
150-
</plugins>
151-
</pluginManagement>
152107
</build>
153108
<profiles>
154109
<profile>
155110
<id>release-profile</id>
156111
<properties>
157112
<skipTests>true</skipTests>
158113
</properties>
159-
<build>
160-
<plugins>
161-
<plugin>
162-
<groupId>net.alchim31.maven</groupId>
163-
<artifactId>scala-maven-plugin</artifactId>
164-
<executions>
165-
<execution>
166-
<goals>
167-
<goal>compile</goal>
168-
<goal>testCompile</goal>
169-
</goals>
170-
</execution>
171-
</executions>
172-
<configuration>
173-
<scalaVersion>${scala-version}</scalaVersion>
174-
</configuration>
175-
</plugin>
176-
<plugin>
177-
<groupId>org.codehaus.mojo</groupId>
178-
<artifactId>build-helper-maven-plugin</artifactId>
179-
<executions>
180-
<execution>
181-
<id>add-source</id>
182-
<phase>prepare-package</phase>
183-
<goals>
184-
<goal>add-source</goal>
185-
</goals>
186-
<configuration>
187-
<sources>
188-
<source>src/main/scala</source>
189-
</sources>
190-
</configuration>
191-
</execution>
192-
</executions>
193-
</plugin>
194-
</plugins>
195-
</build>
196114
</profile>
197115
<profile>
198116
<id>release-sign-artifacts</id>
@@ -238,11 +156,6 @@
238156
<excludePackageNames/>
239157
</configuration>
240158
</plugin>
241-
<plugin>
242-
<groupId>net.alchim31.maven</groupId>
243-
<artifactId>scala-maven-plugin</artifactId>
244-
<version>${scala-maven-plugin-version}</version>
245-
</plugin>
246159
<plugin>
247160
<groupId>org.apache.maven.plugins</groupId>
248161
<artifactId>maven-jxr-plugin</artifactId>
@@ -327,18 +240,6 @@
327240
<artifactId>commons-cli</artifactId>
328241
<version>${commons-cli-version}</version>
329242
</dependency>
330-
<dependency>
331-
<groupId>org.scalatest</groupId>
332-
<artifactId>scalatest_2.11</artifactId>
333-
<version>${scala-test-version}</version>
334-
<scope>test</scope>
335-
</dependency>
336-
<dependency>
337-
<groupId>org.scala-lang</groupId>
338-
<artifactId>scala-library</artifactId>
339-
<version>${scala-version}</version>
340-
<scope>test</scope>
341-
</dependency>
342243
<dependency>
343244
<groupId>org.testng</groupId>
344245
<artifactId>testng</artifactId>
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package io.swagger.codegen;
2+
3+
import io.swagger.models.Operation;
4+
import io.swagger.models.Swagger;
5+
import io.swagger.models.properties.Property;
6+
import io.swagger.parser.SwaggerParser;
7+
8+
import org.testng.Assert;
9+
import org.testng.annotations.Test;
10+
11+
import java.util.List;
12+
13+
public class CodegenTest {
14+
15+
@Test(description = "read a file upload param from a 2.0 spec")
16+
public void fileUploadParamTest() {
17+
final Swagger model = new SwaggerParser().read("src/test/resources/2_0/petstore.json");
18+
final DefaultCodegen codegen = new DefaultCodegen();
19+
final String path = "/pet/{petId}/uploadImage";
20+
final Operation p = model.getPaths().get(path).getPost();
21+
final CodegenOperation op = codegen.fromOperation(path, "post", p, model.getDefinitions());
22+
23+
Assert.assertEquals(op.operationId, "uploadFile");
24+
Assert.assertEquals(op.httpMethod, "POST");
25+
Assert.assertTrue(op.hasConsumes);
26+
Assert.assertEquals(op.consumes.size(), 1);
27+
Assert.assertEquals(op.consumes.get(0).get("mediaType"), "multipart/form-data");
28+
Assert.assertTrue(op.hasProduces);
29+
Assert.assertEquals(op.allParams.size(), 3);
30+
Assert.assertEquals(op.formParams.size(), 2);
31+
32+
final CodegenParameter file = op.formParams.get(1);
33+
Assert.assertTrue(file.isFormParam);
34+
Assert.assertEquals(file.dataType, "file");
35+
Assert.assertNull(file.required);
36+
Assert.assertTrue(file.isFile);
37+
Assert.assertNull(file.hasMore);
38+
}
39+
40+
@Test(description = "read formParam values from a 2.0 spec")
41+
public void formParamTest() {
42+
final Swagger model = new SwaggerParser().read("src/test/resources/2_0/petstore.json");
43+
final DefaultCodegen codegen = new DefaultCodegen();
44+
final String path = "/pet/{petId}";
45+
final Operation p = model.getPaths().get(path).getPost();
46+
final CodegenOperation op = codegen.fromOperation(path, "post", p, model.getDefinitions());
47+
48+
Assert.assertEquals(op.operationId, "updatePetWithForm");
49+
Assert.assertEquals(op.httpMethod, "POST");
50+
Assert.assertTrue(op.hasConsumes);
51+
Assert.assertEquals(op.consumes.size(), 1);
52+
Assert.assertEquals(op.consumes.get(0).get("mediaType"), "application/x-www-form-urlencoded");
53+
Assert.assertTrue(op.hasProduces);
54+
Assert.assertEquals(op.produces.size(), 2);
55+
Assert.assertEquals(op.produces.get(0).get("mediaType"), "application/json");
56+
Assert.assertEquals(op.produces.get(0).get("hasMore"), "true");
57+
Assert.assertEquals(op.produces.get(1).get("mediaType"), "application/xml");
58+
Assert.assertEquals(op.pathParams.size(), 1);
59+
60+
final CodegenParameter idParam = op.pathParams.get(0);
61+
Assert.assertTrue(idParam.isPathParam);
62+
Assert.assertEquals(idParam.dataType, "String");
63+
Assert.assertTrue(idParam.required);
64+
Assert.assertNull(idParam.hasMore);
65+
66+
Assert.assertEquals(op.allParams.size(), 3);
67+
Assert.assertEquals(op.formParams.size(), 2);
68+
69+
final CodegenParameter nameParam = op.formParams.get(0);
70+
Assert.assertTrue(nameParam.isFormParam);
71+
Assert.assertTrue(nameParam.notFile);
72+
Assert.assertEquals(nameParam.dataType, "String");
73+
Assert.assertNull(nameParam.required);
74+
Assert.assertTrue(nameParam.hasMore);
75+
76+
final CodegenParameter statusParam = op.formParams.get(1);
77+
Assert.assertTrue(statusParam.isFormParam);
78+
Assert.assertTrue(statusParam.notFile);
79+
Assert.assertEquals(statusParam.dataType, "String");
80+
Assert.assertNull(statusParam.required);
81+
Assert.assertNull(statusParam.hasMore);
82+
}
83+
84+
@Test(description = "handle required parameters from a 2.0 spec as required when figuring out Swagger types")
85+
public void requiredParametersTest() {
86+
final Swagger model = new SwaggerParser().read("src/test/resources/2_0/requiredTest.json");
87+
88+
final DefaultCodegen codegen = new DefaultCodegen() {
89+
public String getSwaggerType(Property p) {
90+
if (p != null && !p.getRequired()) {
91+
return "Optional<" + super.getSwaggerType(p) + ">";
92+
}
93+
return super.getSwaggerType(p);
94+
}
95+
};
96+
final String path = "/tests/requiredParams";
97+
final Operation p = model.getPaths().get(path).getGet();
98+
final CodegenOperation op = codegen.fromOperation(path, "get", p, model.getDefinitions());
99+
100+
final List<CodegenParameter> formParams = op.formParams;
101+
Assert.assertEquals(formParams.size(), 2);
102+
Assert.assertEquals(formParams.get(0).dataType, "Long");
103+
Assert.assertEquals(formParams.get(1).dataType, "Optional<string>");
104+
Assert.assertEquals(op.returnType, "Long");
105+
}
106+
107+
@Test(description = "select main response from a 2.0 spec using the lowest 2XX code")
108+
public void responseSelectionTest1() {
109+
final Swagger model = new SwaggerParser().read("src/test/resources/2_0/responseSelectionTest.json");
110+
final DefaultCodegen codegen = new DefaultCodegen();
111+
final String path = "/tests/withTwoHundredAndDefault";
112+
final Operation p = model.getPaths().get(path).getGet();
113+
final CodegenOperation op = codegen.fromOperation(path, "get", p, model.getDefinitions());
114+
115+
Assert.assertEquals(op.returnType, "String");
116+
}
117+
118+
@Test(description = "select main response from a 2.0 spec using the default keyword when no 2XX code")
119+
public void responseSelectionTest2() {
120+
final Swagger model = new SwaggerParser().read("src/test/resources/2_0/responseSelectionTest.json");
121+
final DefaultCodegen codegen = new DefaultCodegen();
122+
final String path = "/tests/withoutTwoHundredButDefault";
123+
final Operation p = model.getPaths().get(path).getGet();
124+
final CodegenOperation op = codegen.fromOperation(path, "get", p, model.getDefinitions());
125+
126+
Assert.assertEquals(op.returnType, "String");
127+
}
128+
129+
@Test(description = "return byte array when response format is byte")
130+
public void binaryDataTest() {
131+
final Swagger model = new SwaggerParser().read("src/test/resources/2_0/binaryDataTest.json");
132+
final DefaultCodegen codegen = new DefaultCodegen();
133+
final String path = "/tests/binaryResponse";
134+
final Operation p = model.getPaths().get(path).getPost();
135+
final CodegenOperation op = codegen.fromOperation(path, "post", p, model.getDefinitions());
136+
137+
Assert.assertEquals(op.returnType, "byte[]");
138+
Assert.assertEquals(op.bodyParam.dataType, "byte[]");
139+
Assert.assertTrue(op.bodyParam.isBinary);
140+
Assert.assertTrue(op.responses.get(0).isBinary);
141+
}
142+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package io.swagger.codegen;
2+
3+
import io.swagger.codegen.examples.ExampleGenerator;
4+
import io.swagger.models.Model;
5+
import io.swagger.models.ModelImpl;
6+
import io.swagger.models.Xml;
7+
import io.swagger.models.properties.ArrayProperty;
8+
import io.swagger.models.properties.RefProperty;
9+
import io.swagger.models.properties.StringProperty;
10+
11+
import com.google.common.collect.ImmutableMap;
12+
import com.google.common.collect.Sets;
13+
import org.testng.Assert;
14+
import org.testng.annotations.Test;
15+
16+
import java.util.Arrays;
17+
import java.util.List;
18+
import java.util.Map;
19+
import java.util.Set;
20+
21+
public class ExampleGeneratorTest {
22+
23+
@Test(description = "check handling of recursive models")
24+
public void recursiveModelsTest() {
25+
final String JSON = "application/json";
26+
final String XML = "application/xml";
27+
final String nodeType = "Node";
28+
final RefProperty ref = new RefProperty(nodeType);
29+
final Model node = new ModelImpl().name(nodeType).property("name", new StringProperty())
30+
.property("parent", ref)
31+
.property("children", new ArrayProperty(ref))
32+
.property("wrappedChildren", new ArrayProperty(ref).xml(new Xml().wrapped(true)));
33+
final String pairType = "Pair";
34+
final ModelImpl pair = new ModelImpl().name(pairType);
35+
for (Map.Entry<String, String> item : ImmutableMap.of("first", "First", "second", "Second").entrySet()) {
36+
final RefProperty property = new RefProperty(nodeType);
37+
property.setXml(new Xml().name(item.getValue()));
38+
pair.property(item.getKey(), property);
39+
40+
}
41+
final Set<String> types = Sets.newHashSet();
42+
final List<String> expectedTypes = Arrays.asList(JSON, XML);
43+
44+
final ExampleGenerator eg = new ExampleGenerator(ImmutableMap.of(nodeType, node, pairType, pair));
45+
for (Map<String, String> item : eg.generate(null, expectedTypes, new RefProperty(pairType))) {
46+
final String example = item.get("example");
47+
final String contentType = item.get("contentType");
48+
if (XML.equals(contentType)) {
49+
types.add(XML);
50+
Assert.assertEquals(example, "<Pair>\n" +
51+
" <Node>\n" +
52+
" <name>string</name>\n" +
53+
" <wrappedChildren>\n" +
54+
" </wrappedChildren>\n" +
55+
" </Node>\n" +
56+
" <Node>\n" +
57+
" <name>string</name>\n" +
58+
" <wrappedChildren>\n" +
59+
" </wrappedChildren>\n" +
60+
" </Node>\n" +
61+
"</Pair>");
62+
} else if (JSON.equals(contentType)) {
63+
types.add(JSON);
64+
// TODO - add JSON validation
65+
Assert.assertNotNull(example);
66+
}
67+
}
68+
69+
Assert.assertEqualsNoOrder(types.toArray(new String[types.size()]),
70+
expectedTypes.toArray(new String[expectedTypes.size()]));
71+
}
72+
}

0 commit comments

Comments
 (0)