Skip to content

Commit

Permalink
#71 Fix AsyncApi Message testing. (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
jemacineiras authored Oct 17, 2022
1 parent 777a99d commit 97ba4ef
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>net.coru</groupId>
<artifactId>scc-multiapi-converter</artifactId>
<version>3.0.2</version>
<version>3.1.0</version>
<name>SCC-MultiApi-Converter</name>
<description>Generates Spring Cloud Contracts based on an OpenApi and AsyncApi document</description>
<url>https://github.com/corunet/scc-multiapi-converter</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public final class MultiApiContractConverter implements ContractConverter<Collec
@Override
public boolean isAccepted(final File file) {
final String name = file.getName();
boolean isAccepted = name.endsWith(".yml") || name.endsWith(".yaml");
boolean isAccepted = name.endsWith(".yml") || name.endsWith(".yaml") || name.endsWith(".json");
if (isAccepted) {
try {
final JsonNode node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public Collection<Contract> convertFrom(final File file) {

final String topicName = topicIterator.next();
if (operationType.equals(BasicTypeConstants.SUBSCRIBE)) {
processSubscribeOperation(contract, bodyProcessed, topicName);
processSubscribeOperation(contract, bodyProcessed, topicName, operationId);
} else if (operationType.equals(BasicTypeConstants.PUBLISH)) {
processPublishOperation(contract, operationId, responseBodyMatchers, bodyProcessed, topicName);
}
Expand All @@ -88,11 +88,12 @@ private void processPublishOperation(
contract.setOutputMessage(outputMessage);
}

private void processSubscribeOperation(final Contract contract, final Map<String, Object> bodyProcessed, final String topicName) {
private void processSubscribeOperation(final Contract contract, final Map<String, Object> bodyProcessed, final String topicName, final String operationId) {
final Input input = new Input();
input.messageFrom(topicName);
input.messageBody(bodyProcessed);
input.messageHeaders(headers -> headers.accept("application/json"));
input.assertThat(operationId + "Validation()");
contract.setInput(input);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
Expand All @@ -19,6 +20,7 @@
import net.coru.multiapi.converter.utils.RandomGenerator;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.cloud.contract.spec.internal.RegexProperty;
import org.springframework.cloud.contract.spec.internal.ResponseBodyMatchers;

public final class AsyncApiContractConverterUtils {
Expand Down Expand Up @@ -103,7 +105,11 @@ public static void processStringPropertyType(
messageBody.put(property, RandomStringUtils.random(5, true, false));
}
} else {
responseBodyMatchers.jsonPath(path, responseBodyMatchers.byRegex(BasicTypeConstants.STRING_REGEX));
if (!properties.get(property).has("pattern")) {
responseBodyMatchers.jsonPath(path, responseBodyMatchers.byRegex(BasicTypeConstants.STRING_REGEX));
} else {
responseBodyMatchers.jsonPath(path, responseBodyMatchers.byRegex(new RegexProperty(Pattern.compile(properties.get(property).get("pattern").asText())).asString()));
}
messageBody.put(property, RandomStringUtils.random(5, true, false));
}
}
Expand Down Expand Up @@ -309,9 +315,9 @@ public static JsonNode getInternalProperties(final JsonNode properties, final St

public static String getType(final JsonNode node) {
final String type;
if (node.get(BasicTypeConstants.FORMAT) != null) {
if (node.has(BasicTypeConstants.FORMAT)) {
type = node.get(BasicTypeConstants.FORMAT).asText();
} else if (node.get(BasicTypeConstants.TYPE) != null) {
} else if (node.has(BasicTypeConstants.TYPE)) {
type = node.get(BasicTypeConstants.TYPE).asText();
} else {
type = node.get(node.fieldNames().next()).get(BasicTypeConstants.TYPE).asText();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;

import io.swagger.v3.oas.models.examples.Example;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
import net.coru.multiapi.converter.utils.BasicTypeConstants;
import org.apache.commons.lang3.tuple.Pair;
import org.codehaus.plexus.util.StringUtils;
import org.springframework.cloud.contract.spec.internal.Body;
import org.springframework.cloud.contract.spec.internal.BodyMatchers;
import org.springframework.cloud.contract.spec.internal.MatchingStrategy;
import org.springframework.cloud.contract.spec.internal.MatchingStrategy.Type;
import org.springframework.cloud.contract.spec.internal.QueryParameters;
import org.springframework.cloud.contract.spec.internal.RegexProperty;
import org.springframework.cloud.contract.spec.internal.Response;

public final class OpenApiContractConverterUtils {
Expand Down Expand Up @@ -93,7 +96,11 @@ public static void processBasicQueryParameterTypeBody(final QueryParameters quer
final String type = parameter.getSchema().getType();
switch (type) {
case BasicTypeConstants.STRING:
queryParameters.parameter(parameter.getName(), BasicTypeConstants.STRING_REGEX);
if (StringUtils.isEmpty(parameter.getSchema().getPattern())) {
queryParameters.parameter(parameter.getName(), BasicTypeConstants.STRING_REGEX);
} else {
queryParameters.parameter(parameter.getName(), new RegexProperty(Pattern.compile(parameter.getSchema().getFormat())).asString());
}
break;
case BasicTypeConstants.INTEGER:
OpenApiContractConverterUtils.processIntegerFormat(queryParameters, parameter);
Expand All @@ -111,10 +118,6 @@ public static void processBasicQueryParameterTypeBody(final QueryParameters quer
}
}

public static void processNumberFormat(final Response response, final Schema schema) {
response.body(processNumberFormat(schema.getFormat(), schema.getName()));
}

public static Map<String, Object> processNumberFormat(final Schema schema) {
return processNumberFormat(schema.getFormat(), schema.getName());
}
Expand All @@ -135,10 +138,6 @@ private static Map<String, Object> processNumberFormat(final String format, fina
return parameter;
}

public static void processIntegerFormat(final Response response, final Schema schema) {
response.body(processIntegerFormat(schema.getFormat(), schema.getName()));
}

public static Map<String, Object> processIntegerFormat(final Schema schema) {
return processIntegerFormat(schema.getFormat(), schema.getName());
}
Expand Down

0 comments on commit 97ba4ef

Please sign in to comment.