Skip to content

Commit

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

`citrus-spring` module.
  • Loading branch information
bbortt committed Oct 24, 2024
1 parent 800d702 commit 53d09e0
Show file tree
Hide file tree
Showing 17 changed files with 161 additions and 201 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) thro
}

if (bean instanceof InitializingPhase) {
if (logger.isDebugEnabled()) {
logger.debug(String.format("Initializing component '%s'", beanName));
}
logger.debug("Initializing component '{}'", beanName);
((InitializingPhase) bean).initialize();
}

Expand All @@ -59,9 +57,7 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) thro
@Override
public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
if (requiresDestruction(bean)) {
if (logger.isDebugEnabled()) {
logger.debug(String.format("Destroying component '%s'", beanName));
}
logger.debug("Destroying component '{}'", beanName);
((ShutdownPhase) bean).destroy();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

package org.citrusframework.config;

import java.util.List;

import org.citrusframework.TestAction;
import org.citrusframework.TestCase;
import org.springframework.beans.factory.FactoryBean;

import java.util.List;

/**
* Test case factory bean constructs test cases with test actions and test finally block.
*
Expand All @@ -37,13 +37,13 @@ public class TestCaseFactory implements FactoryBean<TestCase> {

@Override
public TestCase getObject() throws Exception {
if (this.testActions != null && this.testActions.size() > 0) {
if (this.testActions != null && !this.testActions.isEmpty()) {
for (TestAction action : testActions) {
testCase.addTestAction(action);
}
}

if (this.finalActions != null && this.finalActions.size() > 0) {
if (this.finalActions != null && !this.finalActions.isEmpty()) {
for (TestAction action : finalActions) {
testCase.addFinalAction(action);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package org.citrusframework.config.handler;

import java.util.Map;

import org.citrusframework.config.xml.DefaultMessageQueueParser;
import org.citrusframework.config.xml.DirectEndpointAdapterParser;
import org.citrusframework.config.xml.DirectEndpointParser;
Expand All @@ -44,6 +42,8 @@
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;

import java.util.Map;

/**
* Namespace handler for components in Citrus configuration.
*
Expand Down Expand Up @@ -88,9 +88,7 @@ private void lookupBeanDefinitionParser() {

actionParserMap.forEach((k, p) -> {
registerBeanDefinitionParser(k, p);
if (logger.isDebugEnabled()) {
logger.debug(String.format("Register bean definition parser %s from resource %s", p.getClass(), k));
}
logger.debug("Register bean definition parser {} from resource {}", p.getClass(), k);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

package org.citrusframework.config.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.citrusframework.validation.DelegatingPayloadVariableExtractor;
import org.citrusframework.variable.VariableExtractor;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Helper for parsing 'extract' elements containing nested xpath or json variable-extractors.
*
Expand Down Expand Up @@ -52,7 +52,7 @@ public static void addPayloadVariableExtractors(Element element, List<VariableEx
Element messageElement = DomUtils.getChildElementByTagName(element, "message");
if (messageElement != null) {
List<?> namespaceElements = DomUtils.getChildElementsByTagName(messageElement, "namespace");
if (namespaceElements.size() > 0) {
if (!namespaceElements.isEmpty()) {
for (Object namespaceElementObject : namespaceElements) {
Element namespaceElement = (Element) namespaceElementObject;
namespaces.put(namespaceElement.getAttribute("prefix"), namespaceElement.getAttribute("value"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@

package org.citrusframework.config.xml;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.citrusframework.CitrusSettings;
import org.citrusframework.common.Named;
import org.citrusframework.config.util.BeanDefinitionParserUtils;
import org.citrusframework.config.xml.parser.CitrusXmlConfigParser;
import org.citrusframework.config.xml.parser.ScriptMessageBuilderParser;
import org.citrusframework.message.DelegatingPathExpressionProcessor;
Expand All @@ -47,16 +42,66 @@
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.CollectionUtils;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static org.citrusframework.util.StringUtils.hasText;

/**
* Parser providing basic message element configurations used in send and receive actions.
*
*/
public abstract class AbstractMessageActionParser implements BeanDefinitionParser {

protected BeanDefinitionBuilder getBeanDefinitionBuilder(Element element, ParserContext parserContext) {
String endpointUri = parseEndpoint(element);

BeanDefinitionBuilder builder = parseComponent(element, parserContext);
builder.addPropertyValue("name", element.getLocalName());

if (endpointUri.contains(":") || (endpointUri.contains(CitrusSettings.VARIABLE_PREFIX) && endpointUri.contains(CitrusSettings.VARIABLE_SUFFIX))) {
builder.addPropertyValue("endpointUri", endpointUri);
} else {
builder.addPropertyReference("endpoint", endpointUri);
}

DescriptionElementParser.doParse(element, builder);

BeanDefinitionParserUtils.setPropertyReference(builder, element.getAttribute("actor"), "actor");
return builder;
}

private String parseEndpoint(Element element) {
String endpointUri = element.getAttribute("endpoint");

if (!hasText(endpointUri)) {
throw new BeanCreationException("Endpoint reference must not be empty");
}

return endpointUri;
}

/**
* Parse component returning generic bean definition.
* @param element
* @param parserContext
* @return
*/
protected BeanDefinitionBuilder parseComponent(Element element, ParserContext parserContext) {
return BeanDefinitionBuilder.genericBeanDefinition(getMessageFactoryClass());
}

protected abstract Class<?> getMessageFactoryClass();

/**
* Static parse method taking care of basic message element parsing.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package org.citrusframework.config.xml;

import java.util.List;

import org.citrusframework.config.CitrusNamespaceParserRegistry;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
Expand All @@ -27,6 +25,8 @@
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;

import java.util.List;

/**
* Abstract parser implementation that is aware of several embedded test actions of a container. Bean definitions that use
* this parser component must have an 'actions' property of type {@link List} in order to receive the list of embedded test actions.
Expand Down Expand Up @@ -70,7 +70,7 @@ public static void doParse(Element element, ParserContext parserContext, BeanDef
}
}

if (actions.size() > 0) {
if (!actions.isEmpty()) {
builder.addPropertyValue(propertyName, actions);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,9 @@

package org.citrusframework.config.xml;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

import org.citrusframework.CitrusSettings;
import org.citrusframework.actions.ReceiveMessageAction;
import org.citrusframework.config.util.BeanDefinitionParserUtils;
import org.citrusframework.config.util.ValidateMessageParserUtil;
import org.citrusframework.config.util.VariableExtractorParserUtil;
import org.citrusframework.util.StringUtils;
import org.citrusframework.validation.builder.DefaultMessageBuilder;
import org.citrusframework.validation.context.HeaderValidationContext;
import org.citrusframework.validation.context.SchemaValidationContext;
Expand All @@ -51,6 +39,15 @@
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

import static java.lang.Boolean.parseBoolean;
import static org.citrusframework.util.StringUtils.hasText;

Expand All @@ -63,20 +60,7 @@ public class ReceiveMessageActionParser extends AbstractMessageActionParser {

@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
String endpointUri = parseEndpoint(element);

BeanDefinitionBuilder builder = parseComponent(element, parserContext);
builder.addPropertyValue("name", element.getLocalName());

if (endpointUri.contains(":") || (endpointUri.contains(CitrusSettings.VARIABLE_PREFIX) && endpointUri.contains(CitrusSettings.VARIABLE_SUFFIX))) {
builder.addPropertyValue("endpointUri", endpointUri);
} else {
builder.addPropertyReference("endpoint", endpointUri);
}

DescriptionElementParser.doParse(element, builder);

BeanDefinitionParserUtils.setPropertyReference(builder, element.getAttribute("actor"), "actor");
BeanDefinitionBuilder builder = getBeanDefinitionBuilder(element, parserContext);

String receiveTimeout = element.getAttribute("timeout");
if (hasText(receiveTimeout)) {
Expand All @@ -98,15 +82,6 @@ public BeanDefinition parse(Element element, ParserContext parserContext) {
return builder.getBeanDefinition();
}

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

if (!StringUtils.hasText(endpointUri)) {
throw new BeanCreationException("Endpoint reference must not be empty");
}
return endpointUri;
}

/**
* Parse message validation contexts.
* @param messageElement
Expand Down Expand Up @@ -323,7 +298,7 @@ private JsonPathMessageValidationContext getJsonPathMessageValidationContext(Ele
//for now we only handle jsonPath validation
Map<String, Object> validateJsonPathExpressions = new HashMap<>();
List<Element> validateElements = DomUtils.getChildElementsByTagName(messageElement, "validate");
if (validateElements.size() > 0) {
if (!validateElements.isEmpty()) {
for (Element validateElement : validateElements) {
extractJsonPathValidateExpressions(validateElement, validateJsonPathExpressions);
}
Expand Down Expand Up @@ -390,16 +365,17 @@ private void parseNamespaceValidationElements(Element messageElement, XmlMessage
Map<String, String> validateNamespaces = new HashMap<>();

List<Element> validateElements = DomUtils.getChildElementsByTagName(messageElement, "validate");
if (validateElements.size() > 0) {
if (!validateElements.isEmpty()) {
for (Element validateElement : validateElements) {
//check for namespace validation elements
List<Element> validateNamespaceElements = DomUtils.getChildElementsByTagName(validateElement, "namespace");
if (validateNamespaceElements.size() > 0) {
if (!validateNamespaceElements.isEmpty()) {
for (Element namespaceElement : validateNamespaceElements) {
validateNamespaces.put(namespaceElement.getAttribute("prefix"), namespaceElement.getAttribute("value"));
}
}
}

context.namespaces(validateNamespaces);
}
}
Expand All @@ -416,7 +392,7 @@ private void parseXPathValidationElements(Element messageElement, XpathMessageVa
Map<String, Object> validateXpathExpressions = new HashMap<>();

List<Element> validateElements = DomUtils.getChildElementsByTagName(messageElement, "validate");
if (validateElements.size() > 0) {
if (!validateElements.isEmpty()) {
for (Element validateElement : validateElements) {
extractXPathValidateExpressions(validateElement, validateXpathExpressions);
}
Expand Down Expand Up @@ -466,8 +442,7 @@ private void extractXPathValidateExpressions(
* @param validateElement
* @param validateJsonPathExpressions
*/
private void extractJsonPathValidateExpressions(
Element validateElement, Map<String, Object> validateJsonPathExpressions) {
private void extractJsonPathValidateExpressions(Element validateElement, Map<String, Object> validateJsonPathExpressions) {
//check for jsonPath validation - old style with direct attribute
String pathExpression = validateElement.getAttribute("path");
if (JsonPathMessageValidationContext.isJsonPathExpression(pathExpression)) {
Expand All @@ -478,16 +453,7 @@ private void extractJsonPathValidateExpressions(
ValidateMessageParserUtil.parseJsonPathElements(validateElement, validateJsonPathExpressions);
}

/**
* Parse component returning generic bean definition.
*
* @param element
* @return
*/
protected BeanDefinitionBuilder parseComponent(Element element, ParserContext parserContext) {
return BeanDefinitionBuilder.genericBeanDefinition(getMessageFactoryClass());
}

@Override
protected Class<? extends AbstractReceiveMessageActionFactoryBean> getMessageFactoryClass() {
return ReceiveMessageActionFactoryBean.class;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

package org.citrusframework.config.xml;

import java.util.HashMap;
import java.util.Map;

import org.citrusframework.spi.ResourcePathTypeResolver;
import org.citrusframework.util.FileUtils;
import org.slf4j.Logger;
Expand All @@ -28,6 +25,9 @@
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

import java.util.HashMap;
import java.util.Map;

/**
* Bean definition parser for schema configuration.
*
Expand Down Expand Up @@ -68,7 +68,7 @@ private BeanDefinitionParser lookupSchemaParser(String location) {
}

BeanDefinitionParser parser = TYPE_RESOLVER.resolve(fileExtension);
logger.info(String.format("Found schema bean definition parser %s from resource %s", parser.getClass(), RESOURCE_PATH + "/" + fileExtension));
logger.info("Found schema bean definition parser {} from resource {}", parser.getClass(), RESOURCE_PATH + "/" + fileExtension);
SCHEMA_PARSER.put(fileExtension, parser);
return parser;
}
Expand Down
Loading

0 comments on commit 53d09e0

Please sign in to comment.