Skip to content

Commit

Permalink
chore(citrus-test-api-spring): review and code cleanup
Browse files Browse the repository at this point in the history
pr: #1224

`citrus-test-api-spring` module.
  • Loading branch information
bbortt committed Oct 28, 2024
1 parent 17025a6 commit 6454189
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 86 deletions.
9 changes: 1 addition & 8 deletions test-api-generator/citrus-test-api-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
<description>Citrus Test API Spring Integration</description>
<packaging>jar</packaging>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.citrusframework</groupId>
Expand All @@ -33,5 +27,4 @@
<version>${project.version}</version>
</dependency>
</dependencies>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@

import java.util.List;

import static org.citrusframework.openapi.validation.OpenApiMessageValidationContext.Builder.openApi;
import static org.springframework.beans.factory.support.BeanDefinitionBuilder.genericBeanDefinition;

/**
* Parses XML configuration for receiving API responses based on OpenAPI specifications. Extends
* {@link HttpReceiveResponseActionParser} to handle OpenAPI-specific response builders and
Expand Down Expand Up @@ -83,9 +86,7 @@ public RestApiReceiveMessageActionParser(OpenApiSpecification openApiSpecificati
@Override
protected BeanDefinitionBuilder createBeanDefinitionBuilder(Element element,
ParserContext parserContext) {

BeanDefinitionBuilder beanDefinitionBuilder = super.createBeanDefinitionBuilder(element,
parserContext);
BeanDefinitionBuilder beanDefinitionBuilder = super.createBeanDefinitionBuilder(element, parserContext);

// Remove the messageBuilder property and inject it directly into the action builder.
BeanDefinition beanDefinition = beanDefinitionBuilder.getBeanDefinition();
Expand All @@ -95,8 +96,7 @@ protected BeanDefinitionBuilder createBeanDefinitionBuilder(Element element,

beanDefinition.getPropertyValues().removePropertyValue("messageBuilder");

BeanDefinitionBuilder actionBuilder = BeanDefinitionBuilder.genericBeanDefinition(
beanClass);
BeanDefinitionBuilder actionBuilder = genericBeanDefinition(beanClass);
actionBuilder.addConstructorArgValue(new RuntimeBeanReference(apiBeanClass));
actionBuilder.addConstructorArgValue(openApiSpecification);
actionBuilder.addConstructorArgValue(messageBuilder);
Expand Down Expand Up @@ -135,21 +135,17 @@ protected HttpMessageBuilder createMessageBuilder(HttpMessage httpMessage) {
@Override
protected List<ValidationContext> parseValidationContexts(Element messageElement,
BeanDefinitionBuilder builder) {
List<ValidationContext> validationContexts = super.parseValidationContexts(messageElement,
builder);
OpenApiMessageValidationContext openApiMessageValidationContext = getOpenApiMessageValidationContext(
messageElement);
List<ValidationContext> validationContexts = super.parseValidationContexts(messageElement, builder);
OpenApiMessageValidationContext openApiMessageValidationContext = getOpenApiMessageValidationContext(messageElement);
validationContexts.add(openApiMessageValidationContext);
return validationContexts;
}

/**
* Constructs the OpenAPI message validation context based on the XML element.
*/
private OpenApiMessageValidationContext getOpenApiMessageValidationContext(
Element messageElement) {
OpenApiMessageValidationContext.Builder context = OpenApiMessageValidationContext.Builder.openApi(
openApiSpecification);
private OpenApiMessageValidationContext getOpenApiMessageValidationContext(Element messageElement) {
OpenApiMessageValidationContext.Builder context = openApi(openApiSpecification);

if (messageElement != null) {
addSchemaInformationToValidationContext(messageElement, context);
Expand All @@ -167,8 +163,7 @@ public static class TestApiOpenApiClientReceiveActionBuilderFactoryBean extends

private RestApiReceiveMessageActionBuilder builder;

public TestApiOpenApiClientReceiveActionBuilderFactoryBean(
RestApiReceiveMessageActionBuilder builder) {
public TestApiOpenApiClientReceiveActionBuilderFactoryBean(RestApiReceiveMessageActionBuilder builder) {
this.builder = builder;
}

Expand All @@ -191,5 +186,4 @@ public void setBuilder(RestApiReceiveMessageActionBuilder builder) {
this.builder = builder;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.citrusframework.openapi.testapi.RestApiReceiveMessageActionBuilder;
import org.citrusframework.openapi.testapi.RestApiSendMessageActionBuilder;
import org.citrusframework.openapi.testapi.RestApiSendMessageActionBuilder.TestApiClientRequestMessageBuilder;
import org.citrusframework.openapi.testapi.TestApiUtils;
import org.citrusframework.util.StringUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.RuntimeBeanReference;
Expand All @@ -48,8 +47,12 @@
import java.util.List;
import java.util.stream.Collectors;

import static java.lang.Boolean.parseBoolean;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.citrusframework.openapi.testapi.TestApiUtils.mapXmlAttributeNameToJavaPropertyName;
import static org.springframework.beans.factory.support.BeanDefinitionBuilder.genericBeanDefinition;
import static org.springframework.util.xml.DomUtils.getChildElementByTagName;

/**
* Parses the XML configuration for sending API requests based on OpenAPI specifications. Extends
Expand Down Expand Up @@ -125,30 +128,25 @@ private static List<String> collectChildNodeContents(Element element, String par
@Override
protected BeanDefinitionBuilder createBeanDefinitionBuilder(final Element element,
ParserContext parserContext) {
BeanDefinitionBuilder beanDefinitionBuilder = super.createBeanDefinitionBuilder(element, parserContext);

BeanDefinitionBuilder beanDefinitionBuilder = super.createBeanDefinitionBuilder(element,
parserContext);

BeanDefinitionBuilder actionBuilder = createTestApiActionBuilder(
element, beanDefinitionBuilder);
BeanDefinitionBuilder actionBuilder = createTestApiActionBuilder(element, beanDefinitionBuilder);
beanDefinitionBuilder.addConstructorArgValue(actionBuilder.getBeanDefinition());

setDefaultEndpoint(beanDefinitionBuilder);

Element receive = DomUtils.getChildElementByTagName(element, "receive");
Element receive = getChildElementByTagName(element, "receive");
if (receive != null) {
boolean fork = Boolean.parseBoolean(element.getAttribute("fork"));
return wrapSendAndReceiveActionInSequence(fork, receive, parserContext,
beanDefinitionBuilder);
boolean fork = parseBoolean(element.getAttribute("fork"));
return wrapSendAndReceiveActionInSequence(fork, receive, parserContext, beanDefinitionBuilder);
}

return beanDefinitionBuilder;
}

private BeanDefinitionBuilder createTestApiActionBuilder(Element element,
BeanDefinitionBuilder beanDefinitionBuilder) {
BeanDefinitionBuilder actionBuilder = propagateMessageBuilderToActionBuilder(
beanDefinitionBuilder);
BeanDefinitionBuilder actionBuilder = propagateMessageBuilderToActionBuilder(beanDefinitionBuilder);
readConstructorParameters(element, actionBuilder);
readNonConstructorParameters(element, actionBuilder);
return actionBuilder;
Expand All @@ -160,19 +158,18 @@ private BeanDefinitionBuilder createTestApiActionBuilder(Element element,
* and receiving messages and adds them to a container that executes these actions in sequence
* or asynchronously, depending on the {@code fork} parameter.
*/
private BeanDefinitionBuilder wrapSendAndReceiveActionInSequence(boolean fork, Element receive,
ParserContext parserContext, BeanDefinitionBuilder beanDefinitionBuilder) {

Class<? extends AbstractTestContainerFactoryBean<?, ?>> containerClass =
fork ? AsyncFactoryBean.class : SequenceFactoryBean.class;
private BeanDefinitionBuilder wrapSendAndReceiveActionInSequence(boolean fork,
Element receive,
ParserContext parserContext,
BeanDefinitionBuilder beanDefinitionBuilder) {
Class<? extends AbstractTestContainerFactoryBean<?, ?>> containerClass = fork ? AsyncFactoryBean.class
: SequenceFactoryBean.class;

BeanDefinitionBuilder sequenceBuilder = BeanDefinitionBuilder.genericBeanDefinition(
containerClass);
BeanDefinitionBuilder sequenceBuilder = genericBeanDefinition(containerClass);

RestApiReceiveMessageActionParser receiveApiResponseActionParser = new RestApiReceiveMessageActionParser(
openApiSpecification, operationId, apiBeanClass, receiveBeanClass, defaultEndpointName);
BeanDefinition receiveResponseBeanDefinition = receiveApiResponseActionParser.parse(
receive, parserContext);
BeanDefinition receiveResponseBeanDefinition = receiveApiResponseActionParser.parse(receive, parserContext);

ManagedList<BeanDefinition> actions = new ManagedList<>();
actions.add(beanDefinitionBuilder.getBeanDefinition());
Expand All @@ -187,16 +184,13 @@ private BeanDefinitionBuilder wrapSendAndReceiveActionInSequence(boolean fork, E
* Propagates the message builder created by the superclass into the specific send message
* action builder.
*/
private BeanDefinitionBuilder propagateMessageBuilderToActionBuilder(
BeanDefinitionBuilder beanDefinitionBuilder) {

private BeanDefinitionBuilder propagateMessageBuilderToActionBuilder(BeanDefinitionBuilder beanDefinitionBuilder) {
BeanDefinition beanDefinition = beanDefinitionBuilder.getBeanDefinition();
OpenApiClientRequestMessageBuilder messageBuilder = (OpenApiClientRequestMessageBuilder) beanDefinition.getPropertyValues()
.get("messageBuilder");
beanDefinition.getPropertyValues().removePropertyValue("messageBuilder");

BeanDefinitionBuilder actionBuilder = BeanDefinitionBuilder.genericBeanDefinition(
requestBeanClass);
BeanDefinitionBuilder actionBuilder = genericBeanDefinition(requestBeanClass);

actionBuilder.addConstructorArgValue(new RuntimeBeanReference(apiBeanClass));
actionBuilder.addConstructorArgValue(openApiSpecification);
Expand All @@ -210,7 +204,6 @@ private BeanDefinitionBuilder propagateMessageBuilderToActionBuilder(
* the provided {@link BeanDefinitionBuilder}.
*/
private void readConstructorParameters(Element element, BeanDefinitionBuilder actionBuilder) {

for (String parameterName : constructorParameters) {
if (element.hasAttribute(parameterName)) {
actionBuilder.addConstructorArgValue(element.getAttribute(parameterName));
Expand All @@ -227,9 +220,7 @@ private void readConstructorParameters(Element element, BeanDefinitionBuilder ac
*/
private void readNonConstructorParameters(Element element,
BeanDefinitionBuilder actionBuilder) {

for (String parameterName : nonConstructorParameters) {

if (isHandledBySuper(parameterName)) {
continue;
}
Expand All @@ -245,13 +236,13 @@ private void readNonConstructorParameters(Element element,
Attr attribute = element.getAttributeNode(attributeName);
if (attribute != null) {
actionBuilder.addPropertyValue(
TestApiUtils.mapXmlAttributeNameToJavaPropertyName(parameterName),
mapXmlAttributeNameToJavaPropertyName(parameterName),
attribute.getValue());
} else {
List<String> values = collectChildNodeContents(element, attributeName);
if (values != null && !values.isEmpty()) {
actionBuilder.addPropertyValue(
TestApiUtils.mapXmlAttributeNameToJavaPropertyName(parameterName),
mapXmlAttributeNameToJavaPropertyName(parameterName),
values);
}
}
Expand Down Expand Up @@ -320,8 +311,7 @@ public static class TestApiOpenApiClientSendActionBuilderFactoryBean extends

private RestApiSendMessageActionBuilder builder;

public TestApiOpenApiClientSendActionBuilderFactoryBean(
RestApiSendMessageActionBuilder builder) {
public TestApiOpenApiClientSendActionBuilderFactoryBean(RestApiSendMessageActionBuilder builder) {
this.builder = builder;
}

Expand All @@ -344,5 +334,4 @@ public void setBuilder(RestApiSendMessageActionBuilder builder) {
this.builder = builder;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@

import org.citrusframework.openapi.testapi.GeneratedApi;
import org.citrusframework.openapi.testapi.SoapApiReceiveMessageActionBuilder;
import org.citrusframework.util.StringUtils;
import org.citrusframework.ws.config.xml.ReceiveSoapMessageActionParser;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

import static org.citrusframework.util.StringUtils.hasText;
import static org.springframework.beans.factory.support.BeanDefinitionBuilder.genericBeanDefinition;

public class SoapApiReceiveMessageActionParser extends ReceiveSoapMessageActionParser {

/**
Expand Down Expand Up @@ -58,19 +60,19 @@ protected BeanDefinitionBuilder parseComponent(Element element, ParserContext pa
return beanDefinitionBuilder;
}

@Override
protected String parseEndpoint(Element element) {
String endpointUri = element.getAttribute("endpoint");

if (!StringUtils.hasText(endpointUri)) {
if (!hasText(endpointUri)) {
endpointUri = defaultEndpointName;
}

return endpointUri;
}

private BeanDefinitionBuilder createTestApiActionBuilder() {

BeanDefinitionBuilder actionBuilder = BeanDefinitionBuilder.genericBeanDefinition(
receiveBeanClass);
BeanDefinitionBuilder actionBuilder = genericBeanDefinition(receiveBeanClass);
actionBuilder.addConstructorArgValue(new RuntimeBeanReference(apiBeanClass));

return actionBuilder;
Expand All @@ -87,10 +89,8 @@ protected Class<TestApiSoapClientReceiveActionBuilderFactoryBean> getMessageFact
public static class TestApiSoapClientReceiveActionBuilderFactoryBean extends
ReceiveSoapMessageActionFactoryBean {

public TestApiSoapClientReceiveActionBuilderFactoryBean(
SoapApiReceiveMessageActionBuilder builder) {
public TestApiSoapClientReceiveActionBuilderFactoryBean(SoapApiReceiveMessageActionBuilder builder) {
super(builder);
}
}

}
Loading

0 comments on commit 6454189

Please sign in to comment.