Skip to content

Commit 22f18fa

Browse files
authored
Fix tests for parsing of additional type annotations & refactor test code (#18751)
* Fix tests for parsing of additional type annotations These were comparing the same things with each other and this could not fail. Now using (as probably intended by the author) the before unused ArrayList for assertion of the expectation. * Remove Exception from signature that is never thrown * Simplify assertions using AssertJ * Replace stub implementation with abstract Mockito mock * Cache flat parsed openapi results for faster test execution Simply caching any calls to TestUtils.parseFlattenSpec that occur at least twice. * Fix some "Raw use of parameterized class 'Schema'" warnings
1 parent 746961d commit 22f18fa

3 files changed

Lines changed: 185 additions & 325 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ public static boolean shouldGenerateArrayModel(Schema schema) {
960960
* @param schema potentially containing a '$ref'
961961
* @return schema without '$ref'
962962
*/
963-
public static Schema getReferencedSchema(OpenAPI openAPI, Schema schema) {
963+
public static Schema<?> getReferencedSchema(OpenAPI openAPI, Schema schema) {
964964
if (schema == null) {
965965
return null;
966966
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/AbstractJavaCodegenExampleValuesTest.java

Lines changed: 23 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,39 @@
1717

1818
package org.openapitools.codegen.java;
1919

20+
import org.mockito.Answers;
21+
import org.mockito.Mockito;
2022
import org.openapitools.codegen.CodegenParameter;
2123
import org.openapitools.codegen.CodegenProperty;
22-
import org.openapitools.codegen.CodegenType;
2324
import org.openapitools.codegen.languages.AbstractJavaCodegen;
2425
import org.testng.Assert;
26+
import org.testng.annotations.BeforeMethod;
2527
import org.testng.annotations.Test;
2628

2729
import java.util.Arrays;
2830
import java.util.Collections;
2931

3032
public class AbstractJavaCodegenExampleValuesTest {
3133

32-
private final AbstractJavaCodegen fakeJavaCodegen = new P_AbstractJavaCodegen();
34+
private AbstractJavaCodegen codegen;
35+
36+
/**
37+
* In TEST-NG, test class (and its fields) is only constructed once (vs. for every test in Jupiter),
38+
* using @BeforeMethod to have a fresh codegen mock for each test
39+
*/
40+
@BeforeMethod void mockAbstractCodegen() {
41+
codegen = Mockito.mock(
42+
AbstractJavaCodegen.class, Mockito.withSettings().defaultAnswer(Answers.CALLS_REAL_METHODS).useConstructor()
43+
);
44+
}
3345

3446
@Test
3547
void referencedEnumTakeFirstName() {
3648
final CodegenParameter p = new CodegenParameter();
3749
p.allowableValues = Collections.singletonMap("values", Arrays.asList("first", "second"));
3850
p.dataType = "WrappedEnum";
3951

40-
fakeJavaCodegen.setParameterExampleValue(p);
52+
codegen.setParameterExampleValue(p);
4153
Assert.assertEquals(p.example, "WrappedEnum.fromValue(\"first\")");
4254
}
4355

@@ -48,7 +60,7 @@ void inlineEnum() {
4860
p.isEnum = true;
4961
p.dataType = "String";
5062

51-
fakeJavaCodegen.setParameterExampleValue(p);
63+
codegen.setParameterExampleValue(p);
5264
Assert.assertEquals(p.example, "\"first\"");
5365
}
5466

@@ -61,7 +73,7 @@ void inlineEnumArray() {
6173
p.dataType = "List<String>";
6274
p.items = new CodegenProperty();
6375

64-
fakeJavaCodegen.setParameterExampleValue(p);
76+
codegen.setParameterExampleValue(p);
6577
Assert.assertEquals(p.example, "Arrays.asList()");
6678
}
6779

@@ -71,7 +83,7 @@ void dateDefault() {
7183
p.isDate = true;
7284
p.dataType = "LocalDate";
7385

74-
fakeJavaCodegen.setParameterExampleValue(p);
86+
codegen.setParameterExampleValue(p);
7587
Assert.assertEquals(p.example, "LocalDate.now()");
7688
}
7789

@@ -82,7 +94,7 @@ void dateGivenExample() {
8294
p.dataType = "LocalDate";
8395
p.example = "2017-03-30";
8496

85-
fakeJavaCodegen.setParameterExampleValue(p);
97+
codegen.setParameterExampleValue(p);
8698
Assert.assertEquals(p.example, "LocalDate.parse(\"2017-03-30\")");
8799
}
88100

@@ -92,7 +104,7 @@ void dateTimeDefault() {
92104
p.isDateTime = true;
93105
p.dataType = "OffsetDateTime";
94106

95-
fakeJavaCodegen.setParameterExampleValue(p);
107+
codegen.setParameterExampleValue(p);
96108
Assert.assertEquals(p.example, "OffsetDateTime.now()");
97109
}
98110

@@ -103,7 +115,7 @@ void dateTimeGivenExample() {
103115
p.dataType = "OffsetDateTime";
104116
p.example = "2007-12-03T10:15:30+01:00";
105117

106-
fakeJavaCodegen.setParameterExampleValue(p);
118+
codegen.setParameterExampleValue(p);
107119
Assert.assertEquals(p.example, "OffsetDateTime.parse(\"2007-12-03T10:15:30+01:00\")");
108120
}
109121

@@ -113,7 +125,7 @@ void uuidDefault() {
113125
p.isUuid = true;
114126
p.dataType = "UUID";
115127

116-
fakeJavaCodegen.setParameterExampleValue(p);
128+
codegen.setParameterExampleValue(p);
117129
Assert.assertEquals(p.example, "UUID.randomUUID()");
118130
}
119131

@@ -124,46 +136,7 @@ void uuidGivenExample() {
124136
p.dataType = "UUID";
125137
p.example = "13b48713-b931-45ea-bd60-b07491245960";
126138

127-
fakeJavaCodegen.setParameterExampleValue(p);
139+
codegen.setParameterExampleValue(p);
128140
Assert.assertEquals(p.example, "UUID.fromString(\"13b48713-b931-45ea-bd60-b07491245960\")");
129141
}
130-
131-
private static class P_AbstractJavaCodegen extends AbstractJavaCodegen {
132-
@Override
133-
public CodegenType getTag() {
134-
return null;
135-
}
136-
137-
@Override
138-
public String getName() {
139-
return null;
140-
}
141-
142-
@Override
143-
public String getHelp() {
144-
return null;
145-
}
146-
147-
/**
148-
* Gets artifact version.
149-
* Only for testing purposes.
150-
*
151-
* @return version
152-
*/
153-
public String getArtifactVersion() {
154-
return this.artifactVersion;
155-
}
156-
@Test
157-
void customExampleForEnumValue() {
158-
final AbstractJavaCodegen fakeJavaCodegen = new P_AbstractJavaCodegen();
159-
final CodegenParameter p = new CodegenParameter();
160-
p.allowableValues = Collections.singletonMap("values", Arrays.asList("first", "second"));
161-
p.dataType = "WrappedEnum";
162-
p.example = "CustomEnumValue";
163-
164-
fakeJavaCodegen.setParameterExampleValue(p);
165-
// Custom example value should not be modified
166-
Assert.assertEquals(p.example, "CustomEnumValue");
167-
}
168-
}
169142
}

0 commit comments

Comments
 (0)