-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(#1175): added random generator framework
Framework was added in favour of OpenApiTestDataGenerator implementation
- Loading branch information
Showing
43 changed files
with
2,669 additions
and
1,106 deletions.
There are no files selected for viewing
601 changes: 18 additions & 583 deletions
601
...rs/citrus-openapi/src/main/java/org/citrusframework/openapi/OpenApiTestDataGenerator.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...citrus-openapi/src/main/java/org/citrusframework/openapi/random/RandomArrayGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.citrusframework.openapi.random; | ||
|
||
import io.apicurio.datamodels.openapi.models.OasSchema; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
import org.citrusframework.openapi.model.OasModelHelper; | ||
|
||
/** | ||
* A generator for producing random arrays based on an OpenAPI schema. This class extends the | ||
* {@link RandomGenerator} and provides a specific implementation for generating random arrays | ||
* with constraints defined in the schema. | ||
* | ||
* <p>The generator supports arrays with items of a single schema type. If the array's items have | ||
* different schemas, an {@link UnsupportedOperationException} will be thrown.</p>s | ||
* | ||
*/ | ||
public class RandomArrayGenerator extends RandomGenerator { | ||
|
||
@Override | ||
public boolean handles(OasSchema other) { | ||
return OasModelHelper.isArrayType(other); | ||
} | ||
|
||
@Override | ||
void generate(RandomContext randomContext, OasSchema schema) { | ||
Object items = schema.items; | ||
|
||
if (items instanceof OasSchema itemsSchema) { | ||
createRandomArrayValueWithSchemaItem(randomContext, schema, itemsSchema); | ||
} else { | ||
throw new UnsupportedOperationException( | ||
"Random array creation for an array with items having different schema is currently not supported!"); | ||
} | ||
} | ||
|
||
private static void createRandomArrayValueWithSchemaItem(RandomContext randomContext, | ||
OasSchema schema, | ||
OasSchema itemsSchema) { | ||
|
||
Number minItems = schema.minItems != null ? schema.minItems : 1; | ||
Number maxItems = schema.maxItems != null ? schema.maxItems : 10; | ||
|
||
int nItems = ThreadLocalRandom.current() | ||
.nextInt(minItems.intValue(), maxItems.intValue() + 1); | ||
|
||
randomContext.getRandomModelBuilder().array(() -> { | ||
for (int i = 0; i < nItems; i++) { | ||
randomContext.generate(itemsSchema); | ||
} | ||
}); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...us-openapi/src/main/java/org/citrusframework/openapi/random/RandomCompositeGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.citrusframework.openapi.random; | ||
|
||
import static org.springframework.util.CollectionUtils.isEmpty; | ||
|
||
import io.apicurio.datamodels.openapi.models.OasSchema; | ||
import io.apicurio.datamodels.openapi.v3.models.Oas30Schema; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
import org.citrusframework.openapi.model.OasModelHelper; | ||
|
||
/** | ||
* A generator for producing random composite schemas based on an OpenAPI schema. This class extends | ||
* the {@link RandomGenerator} and provides a specific implementation for generating composite schemas | ||
* with constraints defined in the schema. | ||
* | ||
* <p>The generator supports composite schemas, which include `allOf`, `anyOf`, and `oneOf` constructs.</p> | ||
*/ | ||
public class RandomCompositeGenerator extends RandomGenerator { | ||
|
||
@Override | ||
public boolean handles(OasSchema other) { | ||
return OasModelHelper.isCompositeSchema(other); | ||
} | ||
|
||
@Override | ||
void generate(RandomContext randomContext, OasSchema schema) { | ||
|
||
if (!isEmpty(schema.allOf)) { | ||
createAllOff(randomContext, schema); | ||
} else if (schema instanceof Oas30Schema oas30Schema && !isEmpty(oas30Schema.anyOf)) { | ||
createAnyOf(randomContext, oas30Schema); | ||
} else if (schema instanceof Oas30Schema oas30Schema && !isEmpty(oas30Schema.oneOf)) { | ||
createOneOf(randomContext, oas30Schema.oneOf); | ||
} | ||
} | ||
|
||
private static void createOneOf(RandomContext randomContext, List<OasSchema> schemas) { | ||
int schemaIndex = ThreadLocalRandom.current().nextInt(schemas.size()); | ||
randomContext.getRandomModelBuilder().object(() -> | ||
randomContext.generate(schemas.get(schemaIndex))); | ||
} | ||
|
||
private static void createAnyOf(RandomContext randomContext, Oas30Schema schema) { | ||
|
||
randomContext.getRandomModelBuilder().object(() -> { | ||
boolean anyAdded = false; | ||
for (OasSchema oneSchema : schema.anyOf) { | ||
if (ThreadLocalRandom.current().nextBoolean()) { | ||
randomContext.generate(oneSchema); | ||
anyAdded = true; | ||
} | ||
} | ||
|
||
// Add at least one | ||
if (!anyAdded) { | ||
createOneOf(randomContext, schema.anyOf); | ||
} | ||
}); | ||
} | ||
|
||
private static Map<String, Object> createAllOff(RandomContext randomContext, OasSchema schema) { | ||
Map<String, Object> allOf = new HashMap<>(); | ||
|
||
randomContext.getRandomModelBuilder().object(() -> { | ||
for (OasSchema oneSchema : schema.allOf) { | ||
randomContext.generate(oneSchema); | ||
} | ||
}); | ||
|
||
return allOf; | ||
} | ||
} |
Oops, something went wrong.