Skip to content

Commit

Permalink
fix(#821): Allow direct access to path/jsonPath in variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Schlathölter committed Dec 16, 2021
1 parent 8c2179f commit 203527e
Show file tree
Hide file tree
Showing 22 changed files with 1,156 additions and 236 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@
import com.consol.citrus.spi.ReferenceResolverAware;
import com.consol.citrus.util.DefaultTypeConverter;
import com.consol.citrus.util.TypeConverter;
import com.consol.citrus.util.VariableExpressionIterator;
import com.consol.citrus.variable.VariableExpressionIterator;
import com.consol.citrus.validation.MessageValidatorRegistry;
import com.consol.citrus.validation.matcher.ValidationMatcherRegistry;
import com.consol.citrus.variable.GlobalVariables;
import com.consol.citrus.variable.SegmentVariableExtractorRegistry;
import com.consol.citrus.variable.VariableUtils;
import com.consol.citrus.xml.namespace.NamespaceContextBuilder;
import org.slf4j.Logger;
Expand Down Expand Up @@ -175,6 +176,11 @@ public class TestContext implements ReferenceResolverAware, TestActionListenerAw
*/
private LogModifier logModifier;

/**
* SegmentVariableExtractorRegistry
*/
private SegmentVariableExtractorRegistry segmentVariableExtractorRegistry;

/**
* Default constructor
*/
Expand Down Expand Up @@ -225,7 +231,7 @@ public Object getVariableObject(final String variableExpression) {
} else if (variables.containsKey(variableName)) {
return variables.get(variableName);
} else {
return VariableExpressionIterator.getLastExpressionValue(variableName, this);
return VariableExpressionIterator.getLastExpressionValue(variableName, this, segmentVariableExtractorRegistry.getSegmentValueExtractors());
}

}
Expand All @@ -236,7 +242,6 @@ public Object getVariableObject(final String variableExpression) {
*
* @param variableName the name of the new variable
* @param value the new variable value
* @return
* @throws CitrusRuntimeException
*/
public void setVariable(final String variableName, Object value) {
Expand Down Expand Up @@ -652,6 +657,22 @@ public void setAfterTest(List<AfterTest> afterTest) {
this.afterTest = afterTest;
}

/**
* Obtains the segmentVariableExtractorRegistry
* @return
*/
public SegmentVariableExtractorRegistry getSegmentVariableExtractorRegistry() {
return segmentVariableExtractorRegistry;
}

/**
* Specifies the segmentVariableExtractorRegistry
* @param segmentVariableExtractorRegistry
*/
public void setSegmentVariableExtractorRegistry(SegmentVariableExtractorRegistry segmentVariableExtractorRegistry) {
this.segmentVariableExtractorRegistry = segmentVariableExtractorRegistry;
}

/**
* Gets the global message processors for given direction.
* @return
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.consol.citrus.util;

import java.util.function.Predicate;

/**
* Tests if a string represents a Json. An empty string is considered to be a
* valid Json.
*/
public class IsJsonPredicate implements Predicate<String> {

public static IsJsonPredicate INSTANCE = new IsJsonPredicate();

@Override
public boolean test(String toTest) {

if (toTest != null) {
toTest = toTest.trim();
}

return toTest != null && (toTest.length() == 0 || toTest.startsWith("{") || toTest.startsWith("["));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.consol.citrus.util;

import java.util.function.Predicate;

/**
* Tests if a string represents a XML. An empty string is considered to be a valid XML.
*/
public class IsXmlPredicate implements Predicate<String> {

public static IsXmlPredicate INSTANCE = new IsXmlPredicate();
@Override
public boolean test(String toTest) {

if (toTest != null) {
toTest = toTest.trim();
}
return toTest!=null && (toTest.length() == 0 || toTest.startsWith("<"));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2006-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.consol.citrus.variable;

import com.consol.citrus.context.TestContext;
import com.consol.citrus.exceptions.CitrusRuntimeException;
import com.consol.citrus.spi.ResourcePathTypeResolver;
import com.consol.citrus.spi.TypeResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;

/**
* Class extracting values of segments of VariableExpressions.
*
* @author Thorsten Schlathoelter
*/
public interface SegmentVariableExtractor {

/** Logger */
Logger LOG = LoggerFactory.getLogger(SegmentVariableExtractor.class);

/** Segment variable extractor resource lookup path */
String RESOURCE_PATH = "META-INF/citrus/variable/extractor/segment";

/** Type resolver to find custom segment variable extractors on classpath via resource path lookup */
TypeResolver TYPE_RESOLVER = new ResourcePathTypeResolver(RESOURCE_PATH);

/**
* Resolves extractor from resource path lookup with given extractor resource name. Scans classpath for extractor meta information
* with given name and returns instance of extractor. Returns optional instead of throwing exception when no extractor
* could be found.
* @return
*/
static Collection<SegmentVariableExtractor> lookup() {
try {
Map<String, SegmentVariableExtractor> extractors = TYPE_RESOLVER.resolveAll();
return extractors.values();
} catch (CitrusRuntimeException e) {
LOG.warn(String.format("Failed to resolve segment variable extractor from resource '%s'", RESOURCE_PATH));
}

return Collections.emptyList();
}

/**
* Extract variables from given object.
* @param object the object of which to extract the value
* @param matcher
*/
boolean canExtract(TestContext testContext, Object object, VariableExpressionSegmentMatcher matcher);

/**
* Extract variables from given object. Implementations should throw a CitrusRuntimeException
* @param object the object of which to extract the value
* @param matcher
*/
Object extractValue(TestContext testContext, Object object, VariableExpressionSegmentMatcher matcher);

}
Loading

0 comments on commit 203527e

Please sign in to comment.