Skip to content

Commit a5a1281

Browse files
committed
chore: Use parametrized tests to group mutiple tests together
1 parent 4fd2fd5 commit a5a1281

5 files changed

Lines changed: 75 additions & 111 deletions

File tree

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ your API, so your users don't have to.**
3535

3636
## Features
3737

38-
- 🚀 **Instant Setup**: Point it at your OpenAPI spec and go
39-
- 🧠 **Smart Testing**: 120+ playbooks create realistic, context-aware test cases
40-
- 🔍 **Find Hidden Issues**: Edge cases, invalid inputs, and boundary conditions
41-
- 📊 **Clear Results**: Actionable reports with specific fixes
42-
- 🔄 **Replay Mode**: Replay and investigate specific test scenarios
43-
- 📋 **OpenAPI Native**: Understands your API structure automatically
38+
- **Instant Setup**: Point it at your OpenAPI spec and go
39+
- **Smart Testing**: 120+ playbooks create realistic, context-aware test cases
40+
- **Find Hidden Issues**: Edge cases, invalid inputs, and boundary conditions
41+
- **Clear Results**: Actionable reports with specific fixes
42+
- **Replay Mode**: Replay and investigate specific test scenarios
43+
- **OpenAPI Native**: Understands your API structure automatically
4444

4545
## About the Name
4646

@@ -195,11 +195,11 @@ sdk use java 22.3.r17-grl
195195
./target/dochia-runner --version
196196
```
197197

198-
## 📄 License
198+
## License
199199

200200
This project is licensed under the Apache 2.0 License - see the [LICENSE](LICENSE) file for details.
201201

202-
## 🔗 Links
202+
## Links
203203

204204
- **Documentation**: [docs.dochia.dev](https://docs.dochia.dev)
205205
- **Website**: [dochia.dev](https://dochia.dev)

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
<sonar.coverage.exclusions>
3535
**/*TestCaseExporter*.*, **/*TestCase*.*, **/*TestReport*.*, **/*Aspect*.*, **/*DochiaMain*.*,
3636
**/*VersionProvider*.*, **/*ReflectionConfig*.*,**/*TimeExecution*.*, **/*FormEncoder*.*,
37-
**/*HttpContent*.*, **/*MultipartProcessor*.*, **Entry.*, **/*ConsoleUtils*.*, **/*Mutator*.*
37+
**/*HttpContent*.*, **/*MultipartProcessor*.*, **Entry.*, **/*ConsoleUtils*.*, **/*Mutator*.*,
38+
**/*AnsiUtils*.*
3839
</sonar.coverage.exclusions>
3940
<sonar.cpd.exclusions>**/*Generator*.*,**/*Playbook*.*</sonar.cpd.exclusions>
4041
<sonar.coverage.jacoco.xmlReportPaths>target/jacoco-report/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>

src/main/java/dev/dochia/cli/core/openapi/OpenAPIModelGenerator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ private List<Map<String, Object>> resolveAllOfSchemaProperties(Schema schema, St
537537
}
538538

539539
String detectedCasing = parentWithDiscriminator != null
540-
? detectCasingConvention(parentWithDiscriminator, discriminatorPropertyName)
540+
? detectCasingConvention(parentWithDiscriminator)
541541
: this.discriminatorCasing;
542542
String discriminatorValue = WordUtils.convertToDetectedCasing(propertyName, detectedCasing);
543543

@@ -801,14 +801,14 @@ private Object matchToEnumOrEmpty(String name, Schema innerSchema, String proper
801801
// If still empty, try to infer from schema name (for allOf pattern without explicit mapping)
802802
// Detect casing convention from enum values or existing discriminator mappings
803803
if (result.isEmpty() && name != null) {
804-
String detectedCasing = detectCasingConvention(resolveInnerSchema, propertyName);
804+
String detectedCasing = detectCasingConvention(resolveInnerSchema);
805805
result = WordUtils.convertToDetectedCasing(name, detectedCasing);
806806
}
807807

808808
return result;
809809
}
810810

811-
private String detectCasingConvention(Schema schema, String propertyName) {
811+
private String detectCasingConvention(Schema schema) {
812812
// Try to detect from enum values first
813813
List<Object> enumValues = Optional.ofNullable(schema.getEnum()).orElse(List.of());
814814
if (!enumValues.isEmpty()) {

src/test/java/dev/dochia/cli/core/openapi/OpenAPIModelGeneratorTest.java

Lines changed: 12 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -276,118 +276,31 @@ void shouldGenerateOneOfDiscriminatorAsObjectNotString() throws Exception {
276276
}
277277
}
278278

279-
@Test
280-
void shouldGenerateAllOfWithDiscriminatorParentUsingUpperSnakeCaseCasing() throws Exception {
281-
OpenAPIParser openAPIV3Parser = new OpenAPIParser();
282-
ParseOptions options = new ParseOptions();
283-
options.setResolve(true);
284-
options.setFlatten(true);
285-
OpenAPI openAPI = openAPIV3Parser.readContents(Files.readString(Paths.get("src/test/resources/test-allof-discriminator-schema.yaml")), null, options).getOpenAPI();
286-
Map<String, Schema> schemas = OpenApiUtils.getSchemas(openAPI, List.of("application/json"));
287-
globalContext.getSchemaMap().putAll(schemas);
288-
OpenAPIModelGenerator generator = new OpenAPIModelGenerator(globalContext, validDataFormat, new ProcessingArguments.ExamplesFlags(true, true, true, true), 3, true, 2, "UPPER_SNAKE_CASE");
289-
290-
List<String> decimalExamples = generator.generate("DecimalValue");
291-
292-
Assertions.assertThat(decimalExamples).isNotEmpty();
293-
Object kindValue = JsonUtils.getVariableFromJson(decimalExamples.getFirst(), "$.kind");
294-
Assertions.assertThat(kindValue).isNotNull();
295-
Assertions.assertThat(kindValue.toString()).isEqualTo("DECIMAL_VALUE");
296-
}
297-
298-
@Test
299-
void shouldGenerateAllOfWithDiscriminatorParentUsingPascalCaseCasing() throws Exception {
300-
OpenAPIParser openAPIV3Parser = new OpenAPIParser();
301-
ParseOptions options = new ParseOptions();
302-
options.setResolve(true);
303-
options.setFlatten(true);
304-
OpenAPI openAPI = openAPIV3Parser.readContents(Files.readString(Paths.get("src/test/resources/test-allof-discriminator-schema.yaml")), null, options).getOpenAPI();
305-
Map<String, Schema> schemas = OpenApiUtils.getSchemas(openAPI, List.of("application/json"));
306-
globalContext.getSchemaMap().putAll(schemas);
307-
OpenAPIModelGenerator generator = new OpenAPIModelGenerator(globalContext, validDataFormat, new ProcessingArguments.ExamplesFlags(true, true, true, true), 3, true, 2, "PascalCase");
308-
309-
List<String> decimalExamples = generator.generate("DecimalValue");
310-
311-
Assertions.assertThat(decimalExamples).isNotEmpty();
312-
Object kindValue = JsonUtils.getVariableFromJson(decimalExamples.getFirst(), "$.kind");
313-
Assertions.assertThat(kindValue).isNotNull();
314-
Assertions.assertThat(kindValue.toString()).isEqualTo("DecimalValue");
315-
}
316-
317-
@Test
318-
void shouldGenerateAllOfWithDiscriminatorParentUsingCamelCaseCasing() throws Exception {
319-
OpenAPIParser openAPIV3Parser = new OpenAPIParser();
320-
ParseOptions options = new ParseOptions();
321-
options.setResolve(true);
322-
options.setFlatten(true);
323-
OpenAPI openAPI = openAPIV3Parser.readContents(Files.readString(Paths.get("src/test/resources/test-allof-discriminator-schema.yaml")), null, options).getOpenAPI();
324-
Map<String, Schema> schemas = OpenApiUtils.getSchemas(openAPI, List.of("application/json"));
325-
globalContext.getSchemaMap().putAll(schemas);
326-
OpenAPIModelGenerator generator = new OpenAPIModelGenerator(globalContext, validDataFormat, new ProcessingArguments.ExamplesFlags(true, true, true, true), 3, true, 2, "camelCase");
327-
328-
List<String> decimalExamples = generator.generate("DecimalValue");
329-
330-
Assertions.assertThat(decimalExamples).isNotEmpty();
331-
Object kindValue = JsonUtils.getVariableFromJson(decimalExamples.getFirst(), "$.kind");
332-
Assertions.assertThat(kindValue).isNotNull();
333-
Assertions.assertThat(kindValue.toString()).isEqualTo("decimalValue");
334-
}
335-
336-
@Test
337-
void shouldGenerateAllOfWithDiscriminatorParentUsingLowerSnakeCaseCasing() throws Exception {
338-
OpenAPIParser openAPIV3Parser = new OpenAPIParser();
339-
ParseOptions options = new ParseOptions();
340-
options.setResolve(true);
341-
options.setFlatten(true);
342-
OpenAPI openAPI = openAPIV3Parser.readContents(Files.readString(Paths.get("src/test/resources/test-allof-discriminator-schema.yaml")), null, options).getOpenAPI();
343-
Map<String, Schema> schemas = OpenApiUtils.getSchemas(openAPI, List.of("application/json"));
344-
globalContext.getSchemaMap().putAll(schemas);
345-
OpenAPIModelGenerator generator = new OpenAPIModelGenerator(globalContext, validDataFormat, new ProcessingArguments.ExamplesFlags(true, true, true, true), 3, true, 2, "lower_snake_case");
346-
347-
List<String> decimalExamples = generator.generate("DecimalValue");
348-
349-
Assertions.assertThat(decimalExamples).isNotEmpty();
350-
Object kindValue = JsonUtils.getVariableFromJson(decimalExamples.getFirst(), "$.kind");
351-
Assertions.assertThat(kindValue).isNotNull();
352-
Assertions.assertThat(kindValue.toString()).isEqualTo("decimal_value");
353-
}
354-
355-
@Test
356-
void shouldGenerateAllOfWithDiscriminatorParentUsingKebabCaseCasing() throws Exception {
357-
OpenAPIParser openAPIV3Parser = new OpenAPIParser();
358-
ParseOptions options = new ParseOptions();
359-
options.setResolve(true);
360-
options.setFlatten(true);
361-
OpenAPI openAPI = openAPIV3Parser.readContents(Files.readString(Paths.get("src/test/resources/test-allof-discriminator-schema.yaml")), null, options).getOpenAPI();
362-
Map<String, Schema> schemas = OpenApiUtils.getSchemas(openAPI, List.of("application/json"));
363-
globalContext.getSchemaMap().putAll(schemas);
364-
OpenAPIModelGenerator generator = new OpenAPIModelGenerator(globalContext, validDataFormat, new ProcessingArguments.ExamplesFlags(true, true, true, true), 3, true, 2, "kebab-case");
365-
366-
List<String> decimalExamples = generator.generate("DecimalValue");
367-
368-
Assertions.assertThat(decimalExamples).isNotEmpty();
369-
Object kindValue = JsonUtils.getVariableFromJson(decimalExamples.getFirst(), "$.kind");
370-
Assertions.assertThat(kindValue).isNotNull();
371-
Assertions.assertThat(kindValue.toString()).isEqualTo("decimal-value");
372-
}
373-
374-
@Test
375-
void shouldGenerateAllOfWithDiscriminatorParentUsingLowercaseCasing() throws Exception {
279+
@ParameterizedTest
280+
@CsvSource({
281+
"UPPER_SNAKE_CASE, DECIMAL_VALUE",
282+
"PascalCase, DecimalValue",
283+
"camelCase, decimalValue",
284+
"lower_snake_case, decimal_value",
285+
"kebab-case, decimal-value",
286+
"lowercase, decimalvalue"
287+
})
288+
void shouldGenerateAllOfWithDiscriminatorParentUsingUpperSnakeCaseCasing(String casing, String expectedKindValue) throws Exception {
376289
OpenAPIParser openAPIV3Parser = new OpenAPIParser();
377290
ParseOptions options = new ParseOptions();
378291
options.setResolve(true);
379292
options.setFlatten(true);
380293
OpenAPI openAPI = openAPIV3Parser.readContents(Files.readString(Paths.get("src/test/resources/test-allof-discriminator-schema.yaml")), null, options).getOpenAPI();
381294
Map<String, Schema> schemas = OpenApiUtils.getSchemas(openAPI, List.of("application/json"));
382295
globalContext.getSchemaMap().putAll(schemas);
383-
OpenAPIModelGenerator generator = new OpenAPIModelGenerator(globalContext, validDataFormat, new ProcessingArguments.ExamplesFlags(true, true, true, true), 3, true, 2, "lowercase");
296+
OpenAPIModelGenerator generator = new OpenAPIModelGenerator(globalContext, validDataFormat, new ProcessingArguments.ExamplesFlags(true, true, true, true), 3, true, 2, casing);
384297

385298
List<String> decimalExamples = generator.generate("DecimalValue");
386299

387300
Assertions.assertThat(decimalExamples).isNotEmpty();
388301
Object kindValue = JsonUtils.getVariableFromJson(decimalExamples.getFirst(), "$.kind");
389302
Assertions.assertThat(kindValue).isNotNull();
390-
Assertions.assertThat(kindValue.toString()).isEqualTo("decimalvalue");
303+
Assertions.assertThat(kindValue).hasToString(expectedKindValue);
391304
}
392305

393306
private OpenAPIModelGenerator setupPayloadGenerator() throws IOException {

src/test/java/dev/dochia/cli/core/util/DochiaRandomTest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import org.junit.jupiter.api.DisplayName;
77
import org.junit.jupiter.api.Nested;
88
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.params.ParameterizedTest;
10+
import org.junit.jupiter.params.provider.CsvSource;
911

1012
import java.util.Random;
1113

@@ -96,6 +98,54 @@ void shouldGenerateDeterministicAlphanumeric() {
9698
}
9799
}
98100

101+
@Nested
102+
@DisplayName("Next Range Generation Tests")
103+
class NextRangeTests {
104+
105+
@ParameterizedTest
106+
@CsvSource({
107+
"5,10",
108+
"0,0",
109+
"3,3",
110+
"1,2"
111+
})
112+
@DisplayName("Should generate random string within range or equal bounds")
113+
void shouldGenerateRandomStringWithinRange(int min, int max) {
114+
String result = DochiaRandom.next(min, max);
115+
116+
if (min == max) {
117+
assertThat(result).hasSize(min);
118+
} else {
119+
assertThat(result.length()).isBetween(min, max - 1);
120+
}
121+
}
122+
123+
@Test
124+
@DisplayName("Should generate deterministic random string within range with same seed")
125+
void shouldGenerateDeterministicRandomStringWithinRange() {
126+
DochiaRandom.initRandom(42L);
127+
String result1 = DochiaRandom.next(5, 10);
128+
129+
DochiaRandom.initRandom(42L);
130+
String result2 = DochiaRandom.next(5, 10);
131+
132+
assertThat(result1).isEqualTo(result2);
133+
}
134+
135+
@ParameterizedTest
136+
@CsvSource({
137+
"10,5,Start value must be smaller or equal to end value.",
138+
"-1,5,Both range values must be non-negative.",
139+
"-5,-1,Both range values must be non-negative."
140+
})
141+
@DisplayName("Should throw exception for invalid ranges")
142+
void shouldThrowExceptionForInvalidRanges(int min, int max, String message) {
143+
assertThatThrownBy(() -> DochiaRandom.next(min, max))
144+
.isInstanceOf(IllegalArgumentException.class)
145+
.hasMessage(message);
146+
}
147+
}
148+
99149
@Nested
100150
@DisplayName("Alphabetic Generation Tests")
101151
class AlphabeticTests {

0 commit comments

Comments
 (0)