Skip to content

Commit 7ad6ca2

Browse files
committed
Add unit tests for package names
1 parent 0da9932 commit 7ad6ca2

File tree

5 files changed

+261
-36
lines changed

5 files changed

+261
-36
lines changed

src/main/java/io/swagger/codegen/languages/DefaultCodegenConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,10 @@ public void setApiPackage(String apiPackage) {
588588
this.apiPackage = apiPackage;
589589
}
590590

591+
public Boolean getSortParamsByRequiredFlag() {
592+
return sortParamsByRequiredFlag;
593+
}
594+
591595
public void setSortParamsByRequiredFlag(Boolean sortParamsByRequiredFlag) {
592596
this.sortParamsByRequiredFlag = sortParamsByRequiredFlag;
593597
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package io.swagger.codegen.languages;
2+
3+
import io.swagger.codegen.CodegenArgument;
4+
import io.swagger.codegen.CodegenType;
5+
import org.testng.Assert;
6+
import org.testng.annotations.Test;
7+
import java.util.List;
8+
9+
public class DefaultCodegenConfigTest {
10+
11+
@Test
12+
public void testInitialValues() throws Exception {
13+
final DefaultCodegenConfig codegen = new P_DefaultCodegenConfig();
14+
codegen.processOpts();
15+
16+
Assert.assertEquals(codegen.modelPackage, "");
17+
Assert.assertEquals(codegen.apiPackage, "");
18+
Assert.assertEquals(codegen.sortParamsByRequiredFlag, Boolean.TRUE);
19+
}
20+
21+
@Test
22+
public void testAdditionalProperties() throws Exception {
23+
final DefaultCodegenConfig codegen = new P_DefaultCodegenConfig();
24+
codegen.setModelPackage("xxx.yyyyy.zzzzzzz.model");
25+
codegen.setApiPackage("xxx.yyyyy.zzzzzzz.api");
26+
codegen.setSortParamsByRequiredFlag(false);
27+
codegen.processOpts();
28+
29+
Assert.assertEquals(codegen.modelPackage, "xxx.yyyyy.zzzzzzz.model");
30+
Assert.assertEquals(codegen.apiPackage, "xxx.yyyyy.zzzzzzz.api");
31+
Assert.assertEquals(codegen.sortParamsByRequiredFlag, Boolean.FALSE);
32+
}
33+
34+
private static class P_DefaultCodegenConfig extends DefaultCodegenConfig{
35+
@Override
36+
public String getArgumentsLocation() {
37+
return null;
38+
}
39+
40+
@Override
41+
public CodegenType getTag() {
42+
return null;
43+
}
44+
45+
@Override
46+
public String getName() {
47+
return null;
48+
}
49+
50+
@Override
51+
public String getHelp() {
52+
return null;
53+
}
54+
55+
@Override
56+
public List<CodegenArgument> readLanguageArguments() {
57+
return null;
58+
}
59+
}
60+
}

src/test/java/io/swagger/codegen/languages/java/AbstractJavaCodegenTest.java

Lines changed: 73 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.swagger.codegen.languages.java;
22

33
import io.swagger.codegen.CodegenArgument;
4+
import io.swagger.codegen.CodegenConstants;
45
import io.swagger.codegen.CodegenType;
56
import io.swagger.v3.oas.models.OpenAPI;
67
import io.swagger.v3.oas.models.Operation;
@@ -13,32 +14,7 @@
1314

1415
public class AbstractJavaCodegenTest {
1516

16-
private final AbstractJavaCodegen fakeJavaCodegen = new AbstractJavaCodegen() {
17-
@Override
18-
public String getArgumentsLocation() {
19-
return null;
20-
}
21-
22-
@Override
23-
public CodegenType getTag() {
24-
return null;
25-
}
26-
27-
@Override
28-
public String getName() {
29-
return null;
30-
}
31-
32-
@Override
33-
public String getHelp() {
34-
return null;
35-
}
36-
37-
@Override
38-
public List<CodegenArgument> readLanguageArguments() {
39-
return null;
40-
}
41-
};
17+
private final AbstractJavaCodegen fakeJavaCodegen = new P_AbstractJavaCodegen();
4218

4319
@Test
4420
public void toEnumVarNameShouldNotShortenUnderScore() throws Exception {
@@ -128,4 +104,75 @@ public void convertModelName() throws Exception {
128104
Assert.assertEquals(fakeJavaCodegen.toModelName("nam#e"), "Name");
129105
Assert.assertEquals(fakeJavaCodegen.toModelName("$another-fake?"), "AnotherFake");
130106
}
107+
108+
@Test
109+
public void testInitialPackageNamesValues() throws Exception {
110+
final AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
111+
codegen.processOpts();
112+
113+
Assert.assertEquals(codegen.modelPackage(), "invalidPackageName");
114+
Assert.assertEquals(codegen.apiPackage(), "invalidPackageName");
115+
Assert.assertEquals(codegen.invokerPackage, "io.swagger");
116+
}
117+
118+
@Test
119+
public void testPackageNamesSetWithSetters() throws Exception {
120+
final AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
121+
codegen.setModelPackage("xxx.yyyyy.zzzzzzz.model");
122+
codegen.setApiPackage("xxx.yyyyy.zzzzzzz.api");
123+
codegen.setInvokerPackage("xxx.yyyyy.zzzzzzz.invoker");
124+
codegen.setSortParamsByRequiredFlag(false);
125+
codegen.processOpts();
126+
127+
Assert.assertEquals(codegen.modelPackage(), "xxx.yyyyy.zzzzzzz.model");
128+
Assert.assertEquals(codegen.apiPackage(), "xxx.yyyyy.zzzzzzz.api");
129+
Assert.assertEquals(codegen.invokerPackage, "xxx.yyyyy.zzzzzzz.invoker");
130+
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "xxx.yyyyy.zzzzzzz.invoker");
131+
Assert.assertEquals(codegen.getSortParamsByRequiredFlag(), Boolean.FALSE);
132+
}
133+
134+
@Test
135+
public void testPackageNamesSetWithAdditionalProperties() throws Exception {
136+
final AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
137+
codegen.additionalProperties().put(CodegenConstants.MODEL_PACKAGE, "xxx.yyyyy.model.xxxxxx");
138+
codegen.additionalProperties().put(CodegenConstants.API_PACKAGE, "xxx.yyyyy.api.xxxxxx");
139+
codegen.additionalProperties().put(CodegenConstants.INVOKER_PACKAGE, "xxx.yyyyy.invoker.xxxxxx");
140+
codegen.setSortParamsByRequiredFlag(true);
141+
codegen.processOpts();
142+
143+
Assert.assertEquals(codegen.modelPackage(), "xxx.yyyyy.model.xxxxxx");
144+
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.MODEL_PACKAGE), "xxx.yyyyy.model.xxxxxx");
145+
Assert.assertEquals(codegen.apiPackage(), "xxx.yyyyy.api.xxxxxx");
146+
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.API_PACKAGE), "xxx.yyyyy.api.xxxxxx");
147+
Assert.assertEquals(codegen.invokerPackage, "xxx.yyyyy.invoker.xxxxxx");
148+
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "xxx.yyyyy.invoker.xxxxxx");
149+
Assert.assertEquals(codegen.getSortParamsByRequiredFlag(), Boolean.TRUE);
150+
}
151+
152+
public static class P_AbstractJavaCodegen extends AbstractJavaCodegen {
153+
@Override
154+
public String getArgumentsLocation() {
155+
return null;
156+
}
157+
158+
@Override
159+
public CodegenType getTag() {
160+
return null;
161+
}
162+
163+
@Override
164+
public String getName() {
165+
return null;
166+
}
167+
168+
@Override
169+
public String getHelp() {
170+
return null;
171+
}
172+
173+
@Override
174+
public List<CodegenArgument> readLanguageArguments() {
175+
return null;
176+
}
177+
}
131178
}

src/test/java/io/swagger/codegen/languages/java/AbstractJavaJAXRSServerCodegenTest.java

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,67 @@
11
package io.swagger.codegen.languages.java;
22

33
import io.swagger.codegen.CodegenArgument;
4+
import io.swagger.codegen.CodegenConstants;
45
import io.swagger.codegen.CodegenType;
56
import org.testng.Assert;
67
import org.testng.annotations.Test;
78
import java.util.List;
89

910
public class AbstractJavaJAXRSServerCodegenTest {
1011

11-
private final AbstractJavaJAXRSServerCodegen fakeJavaJAXRSCodegen = new AbstractJavaJAXRSServerCodegen() {
12+
private final AbstractJavaJAXRSServerCodegen fakeJavaJAXRSCodegen = new P_AbstractJavaJAXRSServerCodegen();
13+
14+
@Test
15+
public void convertApiName() throws Exception {
16+
Assert.assertEquals(fakeJavaJAXRSCodegen.toApiName("name"), "NameApi");
17+
Assert.assertEquals(fakeJavaJAXRSCodegen.toApiName("$name"), "NameApi");
18+
Assert.assertEquals(fakeJavaJAXRSCodegen.toApiName("nam#e"), "NameApi");
19+
Assert.assertEquals(fakeJavaJAXRSCodegen.toApiName("$another-fake?"), "AnotherFakeApi");
20+
Assert.assertEquals(fakeJavaJAXRSCodegen.toApiName("fake_classname_tags 123#$%^"), "FakeClassnameTags123Api");
21+
}
22+
23+
@Test
24+
public void testInitialPackageNamesValues() throws Exception {
25+
final AbstractJavaJAXRSServerCodegen codegen = new P_AbstractJavaJAXRSServerCodegen();
26+
codegen.processOpts();
27+
28+
Assert.assertEquals(codegen.modelPackage(), "io.swagger.model");
29+
Assert.assertEquals(codegen.apiPackage(), "io.swagger.api");
30+
Assert.assertEquals(codegen.invokerPackage, "io.swagger.api");
31+
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "io.swagger.api");
32+
}
33+
34+
@Test
35+
public void testPackageNamesSetWithSetters() throws Exception {
36+
final AbstractJavaJAXRSServerCodegen codegen = new P_AbstractJavaJAXRSServerCodegen();
37+
codegen.setModelPackage("xx.yyyyyyyy.model");
38+
codegen.setApiPackage("xx.yyyyyyyy.api");
39+
codegen.setInvokerPackage("xx.yyyyyyyy.invoker");
40+
codegen.processOpts();
41+
42+
Assert.assertEquals(codegen.modelPackage(), "xx.yyyyyyyy.model");
43+
Assert.assertEquals(codegen.apiPackage(), "xx.yyyyyyyy.api");
44+
Assert.assertEquals(codegen.invokerPackage, "xx.yyyyyyyy.invoker");
45+
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "xx.yyyyyyyy.invoker");
46+
}
47+
48+
@Test
49+
public void testPackageNamesSetWithAdditionalProperties() throws Exception {
50+
final AbstractJavaJAXRSServerCodegen codegen = new P_AbstractJavaJAXRSServerCodegen();
51+
codegen.additionalProperties().put(CodegenConstants.MODEL_PACKAGE, "xxx.yyyyy.mmmmm.model");
52+
codegen.additionalProperties().put(CodegenConstants.API_PACKAGE, "xxx.yyyyy.aaaaa.api");
53+
codegen.additionalProperties().put(CodegenConstants.INVOKER_PACKAGE,"xxx.yyyyy.iiii.invoker");
54+
codegen.processOpts();
55+
56+
Assert.assertEquals(codegen.modelPackage(), "xxx.yyyyy.mmmmm.model");
57+
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.MODEL_PACKAGE), "xxx.yyyyy.mmmmm.model");
58+
Assert.assertEquals(codegen.apiPackage(), "xxx.yyyyy.aaaaa.api");
59+
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.API_PACKAGE), "xxx.yyyyy.aaaaa.api");
60+
Assert.assertEquals(codegen.invokerPackage, "xxx.yyyyy.iiii.invoker");
61+
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "xxx.yyyyy.iiii.invoker");
62+
}
63+
64+
private static class P_AbstractJavaJAXRSServerCodegen extends AbstractJavaJAXRSServerCodegen {
1265
@Override
1366
public String getArgumentsLocation() {
1467
return null;
@@ -33,14 +86,5 @@ public String getHelp() {
3386
public List<CodegenArgument> readLanguageArguments() {
3487
return null;
3588
}
36-
};
37-
38-
@Test
39-
public void convertApiName() throws Exception {
40-
Assert.assertEquals(fakeJavaJAXRSCodegen.toApiName("name"), "NameApi");
41-
Assert.assertEquals(fakeJavaJAXRSCodegen.toApiName("$name"), "NameApi");
42-
Assert.assertEquals(fakeJavaJAXRSCodegen.toApiName("nam#e"), "NameApi");
43-
Assert.assertEquals(fakeJavaJAXRSCodegen.toApiName("$another-fake?"), "AnotherFakeApi");
44-
Assert.assertEquals(fakeJavaJAXRSCodegen.toApiName("fake_classname_tags 123#$%^"), "FakeClassnameTags123Api");
4589
}
4690
}

src/test/java/io/swagger/codegen/languages/java/JavaClientCodegenTest.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.swagger.codegen.languages.java;
22

3+
import io.swagger.codegen.CodegenConstants;
34
import io.swagger.codegen.CodegenModel;
45
import io.swagger.codegen.CodegenModelFactory;
56
import io.swagger.codegen.CodegenModelType;
@@ -173,4 +174,73 @@ public void nullValuesInComposedSchema() throws Exception {
173174
new ComposedSchema());
174175
Assert.assertEquals(result.name, "CompSche");
175176
}
177+
178+
@Test
179+
public void testInitialPackageNamesValues() throws Exception {
180+
final JavaClientCodegen codegen = new JavaClientCodegen();
181+
codegen.processOpts();
182+
183+
Assert.assertEquals(codegen.modelPackage(), "io.swagger.client.model");
184+
Assert.assertEquals(codegen.apiPackage(), "io.swagger.client.api");
185+
Assert.assertEquals(codegen.invokerPackage, "io.swagger.client");
186+
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "io.swagger.client");
187+
}
188+
189+
@Test
190+
public void testPackageNamesSetWithSetters() throws Exception {
191+
final JavaClientCodegen codegen = new JavaClientCodegen();
192+
codegen.setModelPackage("xxx.yyyyy.zzzzzzz.model");
193+
codegen.setApiPackage("xxx.yyyyy.zzzzzzz.api");
194+
codegen.setInvokerPackage("xxx.yyyyy.zzzzzzz.invoker");
195+
codegen.processOpts();
196+
197+
Assert.assertEquals(codegen.modelPackage(), "xxx.yyyyy.zzzzzzz.model");
198+
Assert.assertEquals(codegen.apiPackage(), "xxx.yyyyy.zzzzzzz.api");
199+
Assert.assertEquals(codegen.invokerPackage, "xxx.yyyyy.zzzzzzz.invoker");
200+
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "xxx.yyyyy.zzzzzzz.invoker");
201+
}
202+
203+
@Test
204+
public void testPackageNamesSetWithAdditionalProperties() throws Exception {
205+
final JavaClientCodegen codegen = new JavaClientCodegen();
206+
codegen.additionalProperties().put(CodegenConstants.MODEL_PACKAGE, "xxx.yyyyy.zzzzzzz.mmmmm.model");
207+
codegen.additionalProperties().put(CodegenConstants.API_PACKAGE, "xxx.yyyyy.zzzzzzz.aaaaa.api");
208+
codegen.additionalProperties().put(CodegenConstants.INVOKER_PACKAGE,"xxx.yyyyy.zzzzzzz.iiii.invoker");
209+
codegen.processOpts();
210+
211+
Assert.assertEquals(codegen.modelPackage(), "xxx.yyyyy.zzzzzzz.mmmmm.model");
212+
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.MODEL_PACKAGE), "xxx.yyyyy.zzzzzzz.mmmmm.model");
213+
Assert.assertEquals(codegen.apiPackage(), "xxx.yyyyy.zzzzzzz.aaaaa.api");
214+
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.API_PACKAGE), "xxx.yyyyy.zzzzzzz.aaaaa.api");
215+
Assert.assertEquals(codegen.invokerPackage, "xxx.yyyyy.zzzzzzz.iiii.invoker");
216+
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "xxx.yyyyy.zzzzzzz.iiii.invoker");
217+
}
218+
219+
@Test
220+
public void testPackageNamesSetInvokerDerivedFromApi() throws Exception {
221+
final JavaClientCodegen codegen = new JavaClientCodegen();
222+
codegen.additionalProperties().put(CodegenConstants.MODEL_PACKAGE, "xxx.yyyyy.zzzzzzz.mmmmm.model");
223+
codegen.additionalProperties().put(CodegenConstants.API_PACKAGE, "xxx.yyyyy.zzzzzzz.aaaaa.api");
224+
codegen.processOpts();
225+
226+
Assert.assertEquals(codegen.modelPackage(), "xxx.yyyyy.zzzzzzz.mmmmm.model");
227+
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.MODEL_PACKAGE), "xxx.yyyyy.zzzzzzz.mmmmm.model");
228+
Assert.assertEquals(codegen.apiPackage(), "xxx.yyyyy.zzzzzzz.aaaaa.api");
229+
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.API_PACKAGE), "xxx.yyyyy.zzzzzzz.aaaaa.api");
230+
Assert.assertEquals(codegen.invokerPackage, "xxx.yyyyy.zzzzzzz.aaaaa");
231+
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "xxx.yyyyy.zzzzzzz.aaaaa");
232+
}
233+
234+
@Test
235+
public void testPackageNamesSetInvokerDerivedFromModel() throws Exception {
236+
final JavaClientCodegen codegen = new JavaClientCodegen();
237+
codegen.additionalProperties().put(CodegenConstants.MODEL_PACKAGE, "xxx.yyyyy.zzzzzzz.mmmmm.model");
238+
codegen.processOpts();
239+
240+
Assert.assertEquals(codegen.modelPackage(), "xxx.yyyyy.zzzzzzz.mmmmm.model");
241+
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.MODEL_PACKAGE), "xxx.yyyyy.zzzzzzz.mmmmm.model");
242+
Assert.assertEquals(codegen.apiPackage(), "io.swagger.client.api");
243+
Assert.assertEquals(codegen.invokerPackage, "xxx.yyyyy.zzzzzzz.mmmmm");
244+
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "xxx.yyyyy.zzzzzzz.mmmmm");
245+
}
176246
}

0 commit comments

Comments
 (0)