Skip to content

Commit d7ce65e

Browse files
authored
Merge pull request #69 from ZenWave360/develop
tests: improve coverage
2 parents 92721ed + 1438963 commit d7ce65e

File tree

9 files changed

+648
-34
lines changed

9 files changed

+648
-34
lines changed

zenwave-sdk-cli/src/main/java/io/zenwave360/sdk/zdl/utils/ZDLJavaSignatureUtils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,9 @@ public static String populateField(Map field) {
305305
String value;
306306
if(field.get("initialValue") != null) {
307307
if ("String".equals(field.get("type")) || "TextBlob".equals(field.get("type"))) {
308-
return "\"" + field.get("initialValue") + "\"";
308+
if(!JSONPath.get(field,"isArray", false)) {
309+
return "\"" + field.get("initialValue") + "\"";
310+
}
309311
}
310312
return String.valueOf(field.get("initialValue"));
311313
}

zenwave-sdk-cli/src/test/java/io/zenwave360/sdk/formatters/JavaFormatterTest.java

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,20 @@
77
import org.eclipse.jface.text.BadLocationException;
88
import org.junit.jupiter.api.Disabled;
99
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.params.ParameterizedTest;
11+
import org.junit.jupiter.params.provider.EnumSource;
12+
import org.junit.jupiter.api.Assertions;
1013

1114
import java.util.List;
1215

1316
public class JavaFormatterTest {
1417

15-
@Test
16-
void testJavaFormatter() throws BadLocationException, FormatterException {
18+
@ParameterizedTest
19+
@EnumSource(Formatter.Formatters.class)
20+
void testJavaFormatterWithAllFormatters(Formatter.Formatters formatterType) throws BadLocationException, FormatterException {
1721
JavaFormatter formatter = new JavaFormatter();
22+
formatter.formatter = formatterType;
23+
formatter.onPropertiesSet();
1824

1925
var source = """
2026
package io.zenwave360.sdk.formatters;
@@ -37,47 +43,58 @@ void testSpringFormatter() {
3743

3844
GeneratedProjectFiles generatedProjectFiles = new GeneratedProjectFiles();
3945
generatedProjectFiles.singleFiles.add(new TemplateOutput("test.java", source, OutputFormatType.JAVA.toString(), false));
46+
4047
formatter.format(generatedProjectFiles);
48+
4149
String formattedContent = generatedProjectFiles.getAllTemplateOutputs().get(0).getContent();
42-
// System.out.println(formattedContent);
50+
Assertions.assertNotNull(formattedContent);
51+
Assertions.assertNotEquals(source, formattedContent);
4352
}
4453

4554
@Test
46-
void testPalantirJavaFormatter() throws BadLocationException, FormatterException, com.palantir.javaformat.java.FormatterException {
55+
void testSkipFormatting() {
4756
JavaFormatter formatter = new JavaFormatter();
48-
formatter.formatter = Formatter.Formatters.palantir;
57+
formatter.skipFormatting = true;
4958
formatter.onPropertiesSet();
5059

51-
var source = """
52-
package io.zenwave360.sdk.formatters;
53-
54-
import java.io.File;
55-
import org.eclipse.jface.text.BadLocationException;
56-
import io.spring.javaformat.config.JavaFormatConfig;
60+
var source = "public class Test{void method(){}}";
61+
GeneratedProjectFiles generatedProjectFiles = new GeneratedProjectFiles();
62+
generatedProjectFiles.singleFiles.add(new TemplateOutput("test.java", source, OutputFormatType.JAVA.toString(), false));
5763

64+
formatter.format(generatedProjectFiles);
5865

66+
String formattedContent = generatedProjectFiles.getAllTemplateOutputs().get(0).getContent();
67+
Assertions.assertEquals(source, formattedContent);
68+
}
5969

60-
public class SpringJavaFormatterTest {
61-
\s
62-
void testSpringFormatter() {
63-
final io.spring.javaformat.formatter.Formatter formatter = new io.spring.javaformat.formatter.Formatter(JavaFormatConfig.findFrom(new File(".")));
64-
\s
65-
var source = null;
66-
}
67-
}
68-
""";
70+
@Test
71+
void testHaltOnFailFormatting() {
72+
JavaFormatter formatter = new JavaFormatter();
73+
formatter.haltOnFailFormatting = true;
74+
formatter.onPropertiesSet();
6975

76+
var invalidSource = "invalid java syntax {{{";
7077
GeneratedProjectFiles generatedProjectFiles = new GeneratedProjectFiles();
71-
generatedProjectFiles.singleFiles.add(new TemplateOutput("test.java", source, OutputFormatType.JAVA.toString(), false));
72-
formatter.format(generatedProjectFiles);
73-
String formattedContent = generatedProjectFiles.getAllTemplateOutputs().get(0).getContent();
74-
// System.out.println(formattedContent);
78+
generatedProjectFiles.singleFiles.add(new TemplateOutput("test.java", invalidSource, OutputFormatType.JAVA.toString(), false));
7579

76-
// This code is raw palantir formatter usage
77-
// var formatter = com.palantir.javaformat.java.Formatter.create();
78-
// var formated = formatter.formatSourceAndFixImports(source);
79-
// System.out.println(formated);
80+
Assertions.assertThrows(RuntimeException.class, () -> {
81+
formatter.format(generatedProjectFiles);
82+
});
8083
}
8184

85+
@Test
86+
void testHaltOnFailFormattingDisabled() {
87+
JavaFormatter formatter = new JavaFormatter();
88+
formatter.haltOnFailFormatting = false;
89+
formatter.onPropertiesSet();
90+
91+
var invalidSource = "invalid java syntax {{{";
92+
GeneratedProjectFiles generatedProjectFiles = new GeneratedProjectFiles();
93+
generatedProjectFiles.singleFiles.add(new TemplateOutput("test.java", invalidSource, OutputFormatType.JAVA.toString(), false));
8294

95+
formatter.format(generatedProjectFiles);
96+
97+
String formattedContent = generatedProjectFiles.getAllTemplateOutputs().get(0).getContent();
98+
Assertions.assertEquals(invalidSource, formattedContent);
99+
}
83100
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package io.zenwave360.sdk.formatters;
2+
3+
import io.zenwave360.sdk.templating.OutputFormatType;
4+
import io.zenwave360.sdk.templating.TemplateOutput;
5+
import io.zenwave360.sdk.zdl.GeneratedProjectFiles;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.api.Assertions;
9+
10+
import java.util.List;
11+
12+
public class KotlinFormatterTest {
13+
14+
private KotlinFormatter formatter;
15+
16+
@BeforeEach
17+
void setUp() {
18+
formatter = new KotlinFormatter();
19+
}
20+
21+
@Test
22+
void testFormatKotlinCode() {
23+
String unformattedKotlin = "class Test{fun hello(){println(\"Hello World\")}}";
24+
TemplateOutput templateOutput = new TemplateOutput("Test.kt", unformattedKotlin, OutputFormatType.KOTLIN.toString(), false);
25+
26+
TemplateOutput result = formatter.format(templateOutput);
27+
28+
Assertions.assertNotEquals(unformattedKotlin, result.getContent());
29+
Assertions.assertTrue(result.getContent().contains("class Test"));
30+
Assertions.assertTrue(result.getContent().contains("fun hello()"));
31+
}
32+
33+
@Test
34+
void testSkipFormattingWhenDisabled() {
35+
formatter.skipFormatting = true;
36+
String kotlinCode = "class Test{fun hello(){println(\"Hello World\")}}";
37+
TemplateOutput templateOutput = new TemplateOutput("Test.kt", kotlinCode, OutputFormatType.KOTLIN.toString(), false);
38+
39+
TemplateOutput result = formatter.format(templateOutput);
40+
41+
Assertions.assertEquals(kotlinCode, result.getContent());
42+
}
43+
44+
@Test
45+
void testSkipFormattingWithFormatterOffComment() {
46+
String kotlinCode = "// formatter:off\nclass Test{fun hello(){println(\"Hello World\")}}";
47+
TemplateOutput templateOutput = new TemplateOutput("Test.kt", kotlinCode, OutputFormatType.KOTLIN.toString(), false);
48+
49+
TemplateOutput result = formatter.format(templateOutput);
50+
51+
Assertions.assertEquals(kotlinCode, result.getContent());
52+
}
53+
54+
@Test
55+
void testNonKotlinFilePassthrough() {
56+
String javaCode = "public class Test { }";
57+
TemplateOutput templateOutput = new TemplateOutput("Test.java", javaCode, OutputFormatType.JAVA.toString(), false);
58+
59+
TemplateOutput result = formatter.format(templateOutput);
60+
61+
Assertions.assertEquals(javaCode, result.getContent());
62+
}
63+
64+
@Test
65+
void testNullMimeTypePassthrough() {
66+
String code = "some content";
67+
TemplateOutput templateOutput = new TemplateOutput("Test.txt", code, null, false);
68+
69+
TemplateOutput result = formatter.format(templateOutput);
70+
71+
Assertions.assertEquals(code, result.getContent());
72+
}
73+
74+
@Test
75+
void testFormattingErrorWithHaltEnabled() {
76+
formatter.haltOnFailFormatting = true;
77+
String invalidKotlin = "invalid kotlin syntax {{{";
78+
TemplateOutput templateOutput = new TemplateOutput("Test.kt", invalidKotlin, OutputFormatType.KOTLIN.toString(), false);
79+
80+
Assertions.assertThrows(RuntimeException.class, () -> {
81+
formatter.format(templateOutput);
82+
});
83+
}
84+
85+
@Test
86+
void testFormattingErrorWithHaltDisabled() {
87+
formatter.haltOnFailFormatting = false;
88+
String invalidKotlin = "invalid kotlin syntax {{{";
89+
TemplateOutput templateOutput = new TemplateOutput("Test.kt", invalidKotlin, OutputFormatType.KOTLIN.toString(), false);
90+
91+
TemplateOutput result = formatter.format(templateOutput);
92+
93+
Assertions.assertEquals(invalidKotlin, result.getContent());
94+
}
95+
96+
@Test
97+
void testFormatGeneratedProjectFiles() {
98+
String kotlinCode1 = "class Test1{fun hello(){println(\"Hello\")}}";
99+
String kotlinCode2 = "class Test2{fun world(){println(\"World\")}}";
100+
String javaCode = "public class Test3 { }";
101+
102+
TemplateOutput kotlinOutput1 = new TemplateOutput("Test1.kt", kotlinCode1, OutputFormatType.KOTLIN.toString(), false);
103+
TemplateOutput kotlinOutput2 = new TemplateOutput("Test2.kt", kotlinCode2, OutputFormatType.KOTLIN.toString(), false);
104+
TemplateOutput javaOutput = new TemplateOutput("Test3.java", javaCode, OutputFormatType.JAVA.toString(), false);
105+
106+
GeneratedProjectFiles projectFiles = new GeneratedProjectFiles();
107+
projectFiles.singleFiles.add(kotlinOutput1);
108+
projectFiles.singleFiles.add(kotlinOutput2);
109+
projectFiles.singleFiles.add(javaOutput);
110+
111+
formatter.format(projectFiles);
112+
113+
List<TemplateOutput> outputs = projectFiles.getAllTemplateOutputs();
114+
115+
// Kotlin files should be formatted
116+
Assertions.assertNotEquals(kotlinCode1, outputs.get(0).getContent());
117+
Assertions.assertNotEquals(kotlinCode2, outputs.get(1).getContent());
118+
119+
// Java file should remain unchanged
120+
Assertions.assertEquals(javaCode, outputs.get(2).getContent());
121+
}
122+
123+
@Test
124+
void testPreserveTemplateOutputProperties() {
125+
String kotlinCode = "class Test{fun hello(){println(\"Hello\")}}";
126+
TemplateOutput templateOutput = new TemplateOutput("Test.kt", kotlinCode, OutputFormatType.KOTLIN.toString(), true);
127+
128+
TemplateOutput result = formatter.format(templateOutput);
129+
130+
Assertions.assertEquals("Test.kt", result.getTargetFile());
131+
Assertions.assertEquals(OutputFormatType.KOTLIN.toString(), result.getMimeType());
132+
Assertions.assertTrue(result.isSkipOverwrite());
133+
}
134+
}

zenwave-sdk-cli/src/test/java/io/zenwave360/sdk/generators/AbstractOpenAPIGeneratorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void test_filter_operations_by_tag_and_verb() throws Exception {
3131
Map<String, List<Map<String, Object>>> putOperations = openapiGenerator.getOperationsGroupedByTag(model, AbstractOpenAPIGenerator.OperationType.PUT);
3232
Map<String, List<Map<String, Object>>> headOperations = openapiGenerator.getOperationsGroupedByTag(model, AbstractOpenAPIGenerator.OperationType.HEAD);
3333
Map<String, List<Map<String, Object>>> parametersInPath = openapiGenerator.getOperationsGroupedByTag(model, AbstractOpenAPIGenerator.OperationType.HEAD);
34-
Assertions.assertEquals(8, allOperations.get("Pet").size());
34+
Assertions.assertEquals(9, allOperations.get("Pet").size());
3535
Assertions.assertEquals(3, getOperations.get("Pet").size());
3636
Assertions.assertEquals(1, putOperations.get("Pet").size());
3737
Assertions.assertEquals("findPetsByStatus", getOperations.get("Pet").get(0).get("operationId"));

zenwave-sdk-cli/src/test/java/io/zenwave360/sdk/zdl/utils/ZDLHttpUtilsTest.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,33 @@ void testFindMethodParams() throws IOException {
3838
Assertions.assertEquals("CustomerInputDTO", parameters.get(1).getKey());
3939
}
4040

41+
@Test
42+
void testFindMethodParamsMultipart() throws IOException {
43+
var model = loadOpenAPI("classpath:io/zenwave360/sdk/resources/openapi/documents-openapi.yml");
44+
var operation = (Map) JSONPath.get(model, "$.paths./documents/upload.post");
45+
46+
var parameters = ZDLHttpUtils.methodParameters(operation, "", "DTO");
47+
Assertions.assertNotNull(parameters);
48+
Assertions.assertEquals(9, parameters.size());
49+
Assertions.assertEquals("org.springframework.web.multipart.MultipartFile", parameters.get(0).getKey());
50+
Assertions.assertEquals("file", parameters.get(0).getValue());
51+
Assertions.assertEquals("Long", parameters.get(1).getKey());
52+
Assertions.assertEquals("id", parameters.get(1).getValue());
53+
}
54+
55+
@Test
56+
void testFindMethodParamsPatch() throws IOException {
57+
var model = loadOpenAPI("classpath:io/zenwave360/sdk/resources/openapi/openapi-petstore.yml");
58+
var operation = (Map) JSONPath.get(model, "$.paths./pet.patch");
59+
60+
var parameters = ZDLHttpUtils.methodParameters(operation, "", "DTO");
61+
Assertions.assertNotNull(parameters);
62+
Assertions.assertEquals(1, parameters.size());
63+
Assertions.assertEquals("Map", parameters.get(0).getKey());
64+
Assertions.assertEquals("input", parameters.get(0).getValue());
65+
}
66+
67+
4168
@Test
4269
void testFindPathParams() throws IOException {
4370
var model = loadZDL("classpath:io/zenwave360/sdk/resources/zdl/customer-address.zdl");
@@ -98,4 +125,48 @@ void getRequestBodyTypeInline() throws IOException {
98125
Assertions.assertEquals("Address", pathParam);
99126
}
100127

128+
@Test
129+
void testGetJavaType() {
130+
// Test binary format
131+
Map binaryParam = Map.of("schema", Map.of("type", "string", "format", "binary"));
132+
Assertions.assertEquals("org.springframework.web.multipart.MultipartFile",
133+
ZDLHttpUtils.getJavaType(binaryParam, "", ""));
134+
135+
// Test date format
136+
Map dateParam = Map.of("schema", Map.of("type", "string", "format", "date"));
137+
Assertions.assertEquals("LocalDate", ZDLHttpUtils.getJavaType(dateParam, "", ""));
138+
139+
// Test date-time format
140+
Map dateTimeParam = Map.of("schema", Map.of("type", "string", "format", "date-time"));
141+
Assertions.assertEquals("Instant", ZDLHttpUtils.getJavaType(dateTimeParam, "", ""));
142+
143+
// Test integer with int64 format
144+
Map longParam = Map.of("schema", Map.of("type", "integer", "format", "int64"));
145+
Assertions.assertEquals("Long", ZDLHttpUtils.getJavaType(longParam, "", ""));
146+
147+
// Test integer without format
148+
Map intParam = Map.of("schema", Map.of("type", "integer"));
149+
Assertions.assertEquals("Integer", ZDLHttpUtils.getJavaType(intParam, "", ""));
150+
151+
// Test number type
152+
Map numberParam = Map.of("schema", Map.of("type", "number"));
153+
Assertions.assertEquals("BigDecimal", ZDLHttpUtils.getJavaType(numberParam, "", ""));
154+
155+
// Test boolean type
156+
Map booleanParam = Map.of("schema", Map.of("type", "boolean"));
157+
Assertions.assertEquals("Boolean", ZDLHttpUtils.getJavaType(booleanParam, "", ""));
158+
159+
// Test array type
160+
Map arrayParam = Map.of("schema", Map.of("type", "array", "items", Map.of("type", "string")));
161+
Assertions.assertEquals("List<String>", ZDLHttpUtils.getJavaType(arrayParam, "", ""));
162+
163+
// Test schema name with prefixes/suffixes
164+
Map schemaParam = Map.of("schema", Map.of("x--schema-name", "Customer"));
165+
Assertions.assertEquals("CustomerDTO", ZDLHttpUtils.getJavaType(schemaParam, "", "DTO"));
166+
167+
// Test default string type
168+
Map stringParam = Map.of("schema", Map.of("type", "string"));
169+
Assertions.assertEquals("String", ZDLHttpUtils.getJavaType(stringParam, "", ""));
170+
}
171+
101172
}

0 commit comments

Comments
 (0)