Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/1161/fluent generated openapi actions #10

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions connectors/citrus-openapi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import static org.citrusframework.openapi.model.OasModelHelper.*;

/**
* Generates proper payloads and validation expressions based on Open API specification rules. Creates outbound payloads
* with generated random test data according to specification and creates inbound payloads with proper validation expressions to
Expand All @@ -37,19 +39,23 @@ public class OpenApiTestDataGenerator {

/**
* Creates payload from schema for outbound message.
*
* @param schema
* @param definitions
* @return
*/
public static String createOutboundPayload(OasSchema schema, Map<String, OasSchema> definitions,
OpenApiSpecification specification) {
if (OasModelHelper.isReferenceType(schema)) {
OasSchema resolved = definitions.get(OasModelHelper.getReferenceName(schema.$ref));
public static String createOutboundPayload(
OasSchema schema,
Map<String, OasSchema> definitions,
OpenApiSpecification specification
) {
if (isReferenceType(schema)) {
OasSchema resolved = definitions.get(getReferenceName(schema.$ref));
return createOutboundPayload(resolved, definitions, specification);
}

StringBuilder payload = new StringBuilder();
if (OasModelHelper.isObjectType(schema)) {
if (isObjectType(schema)) {
payload.append("{");

if (schema.properties != null) {
Expand Down Expand Up @@ -80,15 +86,72 @@ public static String createOutboundPayload(OasSchema schema, Map<String, OasSche
return payload.toString();
}

/**
* Creates control payload from schema for validation.
*
* @param schema
* @param definitions
* @return
*/
public static String createInboundPayload(
OasSchema schema,
Map<String, OasSchema> definitions,
OpenApiSpecification specification
) {
if (isReferenceType(schema)) {
OasSchema resolved = definitions.get(getReferenceName(schema.$ref));
return createInboundPayload(resolved, definitions, specification);
}

StringBuilder payload = new StringBuilder();
if (isObjectType(schema)) {
payload.append("{");

if (schema.properties != null) {
for (Map.Entry<String, OasSchema> entry : schema.properties.entrySet()) {
if (specification.isValidateOptionalFields() || isRequired(schema, entry.getKey())) {
payload.append("\"")
.append(entry.getKey())
.append("\": ")
.append(createValidationExpression(entry.getValue(), definitions, true, specification))
.append(",");
}
}
}

if (payload.toString().endsWith(",")) {
payload.replace(payload.length() - 1, payload.length(), "");
}

payload.append("}");
} else if (OasModelHelper.isArrayType(schema)) {
payload.append("[");
payload.append(createValidationExpression((OasSchema) schema.items, definitions, true, specification));
payload.append("]");
} else {
payload.append(createValidationExpression(schema, definitions, false, specification));
}

return payload.toString();
}

/**
* Use test variable with given name if present or create value from schema with random values
*
* @param schema
* @param definitions
* @param quotes
* @return
*/
public static String createRandomValueExpression(String name, OasSchema schema, Map<String, OasSchema> definitions,
boolean quotes, OpenApiSpecification specification, TestContext context) {
@Deprecated(forRemoval = true)
public static String createRandomValueExpression(
String name,
OasSchema schema,
Map<String, OasSchema> definitions,
boolean quotes,
OpenApiSpecification specification,
TestContext context
) {
if (context.getVariables().containsKey(name)) {
return CitrusSettings.VARIABLE_PREFIX + name + CitrusSettings.VARIABLE_SUFFIX;
}
Expand All @@ -98,20 +161,25 @@ public static String createRandomValueExpression(String name, OasSchema schema,

/**
* Create payload from schema with random values.
*
* @param schema
* @param definitions
* @param quotes
* @return
*/
public static String createRandomValueExpression(OasSchema schema, Map<String, OasSchema> definitions, boolean quotes,
OpenApiSpecification specification) {
if (OasModelHelper.isReferenceType(schema)) {
OasSchema resolved = definitions.get(OasModelHelper.getReferenceName(schema.$ref));
public static String createRandomValueExpression(
OasSchema schema,
Map<String, OasSchema> definitions,
boolean quotes,
OpenApiSpecification specification
) {
if (isReferenceType(schema)) {
OasSchema resolved = definitions.get(getReferenceName(schema.$ref));
return createRandomValueExpression(resolved, definitions, quotes, specification);
}

StringBuilder payload = new StringBuilder();
if (OasModelHelper.isObjectType(schema) || OasModelHelper.isArrayType(schema)) {
if (isObjectType(schema) || OasModelHelper.isArrayType(schema)) {
payload.append(createOutboundPayload(schema, definitions, specification));
} else if ("string".equals(schema.type)) {
if (quotes) {
Expand Down Expand Up @@ -147,57 +215,71 @@ public static String createRandomValueExpression(OasSchema schema, Map<String, O
}

/**
* Creates control payload from schema for validation.
* Use test variable with given name (if present) or create random value expression using functions according to
* schema type and format.
*
* @param name
* @param schema
* @param definitions
* @param context
* @return
*/
public static String createInboundPayload(OasSchema schema, Map<String, OasSchema> definitions,
OpenApiSpecification specification) {
if (OasModelHelper.isReferenceType(schema)) {
OasSchema resolved = definitions.get(OasModelHelper.getReferenceName(schema.$ref));
return createInboundPayload(resolved, definitions, specification);
public static String createRandomValueExpression(
String name,
OasSchema schema,
TestContext context
) {
if (context.getVariables().containsKey(name)) {
return CitrusSettings.VARIABLE_PREFIX + name + CitrusSettings.VARIABLE_SUFFIX;
}

StringBuilder payload = new StringBuilder();
if (OasModelHelper.isObjectType(schema)) {
payload.append("{");
return createRandomValueExpression(schema);
}

if (schema.properties != null) {
for (Map.Entry<String, OasSchema> entry : schema.properties.entrySet()) {
if (specification.isValidateOptionalFields() || isRequired(schema, entry.getKey())) {
payload.append("\"")
.append(entry.getKey())
.append("\": ")
.append(createValidationExpression(entry.getValue(), definitions, true, specification))
.append(",");
}
/**
* Create random value expression using functions according to schema type and format.
*
* @param schema
* @return
*/
public static String createRandomValueExpression(
OasSchema schema
) {
switch (schema.type) {
case "string":
if (schema.format != null && schema.format.equals("date")) {
return "\"citrus:currentDate('yyyy-MM-dd')\"";
} else if (schema.format != null && schema.format.equals("date-time")) {
return "\"citrus:currentDate('yyyy-MM-dd'T'hh:mm:ss')\"";
} else if (StringUtils.hasText(schema.pattern)) {
return "\"citrus:randomValue(" + schema.pattern + ")\"";
} else if (!CollectionUtils.isEmpty(schema.enum_)) {
return "\"citrus:randomEnumValue(" + (String.join(",", schema.enum_)) + ")\"";
} else if (schema.format != null && schema.format.equals("uuid")) {
return "citrus:randomUUID()";
} else {
return "citrus:randomString(10)";
}
}

if (payload.toString().endsWith(",")) {
payload.replace(payload.length() - 1, payload.length(), "");
}

payload.append("}");
} else if (OasModelHelper.isArrayType(schema)) {
payload.append("[");
payload.append(createValidationExpression((OasSchema) schema.items, definitions, true, specification));
payload.append("]");
} else {
payload.append(createValidationExpression(schema, definitions, false, specification));
case "number":
case "integer":
return "citrus:randomNumber(8)";
case "boolean":
return "citrus:randomEnumValue('true', 'false')";
default:
return "";
}

return payload.toString();
}

/**
* Checks if given field name is in list of required fields for this schema.
*
* @param schema
* @param field
* @return
*/
private static boolean isRequired(OasSchema schema, String field) {
private static boolean isRequired(
OasSchema schema,
String field
) {
if (schema.required == null) {
return true;
}
Expand All @@ -207,16 +289,22 @@ private static boolean isRequired(OasSchema schema, String field) {

/**
* Use test variable with given name if present or create validation expression using functions according to schema type and format.
*
* @param name
* @param schema
* @param definitions
* @param quotes
* @param context
* @return
*/
public static String createValidationExpression(String name, OasSchema schema, Map<String, OasSchema> definitions,
boolean quotes, OpenApiSpecification specification,
TestContext context) {
public static String createValidationExpression(
String name,
OasSchema schema,
Map<String, OasSchema> definitions,
boolean quotes,
OpenApiSpecification specification,
TestContext context
) {
if (context.getVariables().containsKey(name)) {
return CitrusSettings.VARIABLE_PREFIX + name + CitrusSettings.VARIABLE_SUFFIX;
}
Expand All @@ -226,20 +314,25 @@ public static String createValidationExpression(String name, OasSchema schema, M

/**
* Create validation expression using functions according to schema type and format.
*
* @param schema
* @param definitions
* @param quotes
* @return
*/
public static String createValidationExpression(OasSchema schema, Map<String, OasSchema> definitions, boolean quotes,
OpenApiSpecification specification) {
if (OasModelHelper.isReferenceType(schema)) {
OasSchema resolved = definitions.get(OasModelHelper.getReferenceName(schema.$ref));
public static String createValidationExpression(
OasSchema schema,
Map<String, OasSchema> definitions,
boolean quotes,
OpenApiSpecification specification
) {
if (isReferenceType(schema)) {
OasSchema resolved = definitions.get(getReferenceName(schema.$ref));
return createValidationExpression(resolved, definitions, quotes, specification);
}

StringBuilder payload = new StringBuilder();
if (OasModelHelper.isObjectType(schema)) {
if (isObjectType(schema)) {
payload.append("{");

if (schema.properties != null) {
Expand Down Expand Up @@ -276,10 +369,13 @@ public static String createValidationExpression(OasSchema schema, Map<String, Oa

/**
* Create validation expression using functions according to schema type and format.
*
* @param schema
* @return
*/
private static String createValidationExpression(OasSchema schema) {
private static String createValidationExpression(
OasSchema schema
) {
switch (schema.type) {
case "string":
if (schema.format != null && schema.format.equals("date")) {
Expand All @@ -303,50 +399,4 @@ private static String createValidationExpression(OasSchema schema) {
}
}

/**
* Use test variable with given name (if present) or create random value expression using functions according to
* schema type and format.
* @param name
* @param schema
* @param context
* @return
*/
public static String createRandomValueExpression(String name, OasSchema schema, TestContext context) {
if (context.getVariables().containsKey(name)) {
return CitrusSettings.VARIABLE_PREFIX + name + CitrusSettings.VARIABLE_SUFFIX;
}

return createRandomValueExpression(schema);
}

/**
* Create random value expression using functions according to schema type and format.
* @param schema
* @return
*/
public static String createRandomValueExpression(OasSchema schema) {
switch (schema.type) {
case "string":
if (schema.format != null && schema.format.equals("date")) {
return "\"citrus:currentDate('yyyy-MM-dd')\"";
} else if (schema.format != null && schema.format.equals("date-time")) {
return "\"citrus:currentDate('yyyy-MM-dd'T'hh:mm:ss')\"";
} else if (StringUtils.hasText(schema.pattern)) {
return "\"citrus:randomValue(" + schema.pattern + ")\"";
} else if (!CollectionUtils.isEmpty(schema.enum_)) {
return "\"citrus:randomEnumValue(" + (String.join(",", schema.enum_)) + ")\"";
} else if (schema.format != null && schema.format.equals("uuid")){
return "citrus:randomUUID()";
} else {
return "citrus:randomString(10)";
}
case "number":
case "integer":
return "citrus:randomNumber(8)";
case "boolean":
return "citrus:randomEnumValue('true', 'false')";
default:
return "";
}
}
}
Loading