Skip to content

Commit

Permalink
fix(#821): Include PR feedback of christophd
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Schlathölter committed Dec 20, 2021
1 parent 43dafde commit cb553a6
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -39,27 +41,47 @@ public class VariableExpressionSegmentMatcher {
/**
* Pattern to parse a variable expression
*/
private static final Pattern VAR_PATH_PATTERN = Pattern.compile("xpath\\((.*)\\)$|jsonPath\\((\\$[.\\[].*)\\)$|([^\\[\\].]+)(\\[([0-9])])?(\\.|$)");
private static final Pattern VAR_PATH_PATTERN = Pattern.compile("(xpath\\((.*)\\)$)|(jsonPath\\((\\$[.\\[].*)\\)$)|(([^\\[\\].]+)(\\[([0-9])])?)(\\.|$)");

/**
* The regex group index for the xpath part
* The regex group index for the full xpath segment
*/
private static final int XPATH_GROUP = 1;
private static final int XPATH_SEGMENT_GROUP = 1;

/**
* The regex group index for the xpath path
*/
private static final int XPATH_GROUP = 2;

/**
* The regex group index for the full jsonPath segment
*/
private static final int JSONPATH_SEGMENT_GROUP = 3;

/**
* The regex group index for the jsonPath part
*/
private static final int JSON_PATH_GROUP = 2;
private static final int JSON_PATH_GROUP = 4;

/**
* The regex group index for the full variable/property segment incl. index
*/
private static final int VAR_PROP_SEGMENT_GROUP = 5;

/**
* The regex group index for the name of the variable/property
*/
private static final int VAR_PROP_NAME_GROUP = 3;
private static final int VAR_PROP_NAME_GROUP = 6;

/**
* The regex group index for the full name/index expression
*/
private static final int NAME_INDEX_GROUP = 7;

/**
* The regex group index for the index when accessing array elements
*/
private static final int INDEX_GROUP = 5;
private static final int INDEX_GROUP = 8;

/**
* The variable expression the matcher is working on
Expand Down Expand Up @@ -99,15 +121,18 @@ public boolean nextMatch() {
currentSegmentIndex = -1;

if (matches) {

String fullSegment;
if (StringUtils.hasLength(matcher.group(JSON_PATH_GROUP))) {
currentSegmentExpression = matcher.group(JSON_PATH_GROUP);
fullSegment = matcher.group(JSONPATH_SEGMENT_GROUP);
} else if (StringUtils.hasLength(matcher.group(XPATH_GROUP))) {
currentSegmentExpression = matcher.group(XPATH_GROUP);
fullSegment = matcher.group(XPATH_SEGMENT_GROUP);
} else {
currentSegmentExpression = matcher.group(VAR_PROP_NAME_GROUP);
currentSegmentIndex = matcher.group(INDEX_GROUP) != null ? Integer.parseInt(matcher.group(INDEX_GROUP)) : -1;
}
fullSegment = matcher.group(VAR_PROP_SEGMENT_GROUP);
}
}
return matches;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.consol.citrus.variable;

import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.*;
import static org.testng.Assert.*;

public class VariableExpressionSegmentMatcherTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.consol.citrus.validation.MessageValidatorConfig;
import com.consol.citrus.validation.interceptor.MessageProcessorsFactory;
import com.consol.citrus.validation.matcher.ValidationMatcherConfig;
import com.consol.citrus.variable.SegmentVariableExtractorRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
Expand Down Expand Up @@ -112,4 +113,9 @@ public FailureStackTestListener failureStackTestListener() {
public ComponentLifecycleProcessor componentInitializer() {
return new ComponentLifecycleProcessor();
}

@Bean
public SegmentVariableExtractorRegistry segmentVariableExtractorRegistry() {
return new SegmentVariableExtractorRegistry();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@
import com.consol.citrus.message.MessageProcessors;
import com.consol.citrus.validation.matcher.ValidationMatcherRegistry;
import com.consol.citrus.variable.GlobalVariables;
import com.consol.citrus.variable.SegmentVariableExtractorRegistry;
import com.consol.citrus.xml.namespace.NamespaceContextBuilder;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.lang.NonNullApi;
import org.springframework.util.CollectionUtils;

/**
Expand Down Expand Up @@ -95,6 +97,9 @@ public class TestContextFactoryBean extends TestContextFactory implements Factor
@Autowired(required=false)
private NamespaceContextBuilder namespaceContextBuilder;

@Autowired
private SegmentVariableExtractorRegistry segmentVariableExtractorRegistry;

/** Spring bean application context that created this factory */
private ApplicationContext applicationContext;

Expand Down Expand Up @@ -172,6 +177,10 @@ public static TestContextFactory newInstance(ApplicationContext applicationConte
factory.setNamespaceContextBuilder(applicationContext.getBean(NamespaceContextBuilder.class));
}

if (!CollectionUtils.isEmpty(applicationContext.getBeansOfType(SegmentVariableExtractorRegistry.class))) {
factory.setSegmentVariableExtractorRegistry(applicationContext.getBean(SegmentVariableExtractorRegistry.class));
}

return factory;
}

Expand Down Expand Up @@ -251,6 +260,10 @@ public void afterPropertiesSet() throws Exception {
if (namespaceContextBuilder != null) {
delegate.setNamespaceContextBuilder(namespaceContextBuilder);
}

if (segmentVariableExtractorRegistry != null) {
delegate.setSegmentVariableExtractorRegistry(segmentVariableExtractorRegistry);
}
}

/**
Expand Down Expand Up @@ -340,4 +353,10 @@ public LogModifier getLogModifier() {
public NamespaceContextBuilder getNamespaceContextBuilder() {
return delegate.getNamespaceContextBuilder();
}

@Override
public SegmentVariableExtractorRegistry getSegmentVariableExtractorRegistry() {
return delegate.getSegmentVariableExtractorRegistry();
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.consol.citrus.xml;

import com.consol.citrus.XmlValidationHelper;
import com.consol.citrus.context.TestContext;
import com.consol.citrus.exceptions.CitrusRuntimeException;
import com.consol.citrus.message.DefaultMessage;
import com.consol.citrus.util.IsXmlPredicate;
import com.consol.citrus.util.XMLUtils;
import com.consol.citrus.variable.SegmentVariableExtractorRegistry;
Expand All @@ -14,6 +16,10 @@
import org.springframework.util.xml.SimpleNamespaceContext;
import org.w3c.dom.Document;

import javax.xml.namespace.NamespaceContext;
import java.util.Collections;
import java.util.UUID;

/**
* @author Thorsten Schlathoelter
*/
Expand Down Expand Up @@ -42,7 +48,7 @@ private Object extractXpath(TestContext testContext, Object xml, VariableExpress
if (xml instanceof Document) {
document = (Document) xml;
} else if (xml instanceof String) {
String documentCacheKey = matcher.getVariableExpression()+"_document";
String documentCacheKey = UUID.nameUUIDFromBytes(((String)xml).getBytes()).toString();
document = (Document)testContext.getVariables().get(documentCacheKey);
if (document == null) {
document = XMLUtils.parseMessagePayload((String)xml);
Expand All @@ -54,11 +60,7 @@ private Object extractXpath(TestContext testContext, Object xml, VariableExpress
throw new CitrusRuntimeException(String.format("Unable to extract xpath from object of type %s", xml.getClass()));
}

// TODO: namespace context?
SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();
NamespaceContextBuilder builder = new NamespaceContextBuilder();
return XPathUtils.evaluate(document, matcher.getSegmentExpression(), simpleNamespaceContext, XPathExpressionResult.STRING);
NamespaceContext namespaceContext = XmlValidationHelper.getNamespaceContextBuilder(testContext).buildContext(new DefaultMessage().setPayload(xml), Collections.emptyMap());
return XPathUtils.evaluate(document, matcher.getSegmentExpression(), namespaceContext, XPathExpressionResult.STRING);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.testng.annotations.Test;
import org.w3c.dom.Document;

import java.util.UUID;

public class XmlPathSegmentVariableExtractorTest extends UnitTestSupport {

private static final String XML_FIXTURE = "<person><name>Peter</name></person>";
Expand All @@ -22,7 +24,7 @@ public void testExtractFromXml() {
Assert.assertEquals(unitUnderTest.extractValue(context, XML_FIXTURE, matcher), "Peter");

// Assert that xml document was cached
Object cachedXmlDocument = context.getVariableObject(matcher.getVariableExpression()+"_document");
Object cachedXmlDocument = context.getVariableObject(UUID.nameUUIDFromBytes(XML_FIXTURE.getBytes()).toString());
Assert.assertTrue(cachedXmlDocument instanceof Document);

// Assert that another match can be matched
Expand Down

0 comments on commit cb553a6

Please sign in to comment.