Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/821/access variable by xpath json #827

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.consol.citrus.context;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -27,7 +26,6 @@
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import com.consol.citrus.CitrusSettings;
import com.consol.citrus.TestAction;
import com.consol.citrus.TestActionBuilder;
Expand Down Expand Up @@ -61,15 +59,16 @@
import com.consol.citrus.spi.ReferenceResolverAware;
import com.consol.citrus.util.DefaultTypeConverter;
import com.consol.citrus.util.TypeConverter;
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;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;

/**
Expand Down Expand Up @@ -177,6 +176,11 @@ public class TestContext implements ReferenceResolverAware, TestActionListenerAw
*/
private LogModifier logModifier;

/**
* SegmentVariableExtractorRegistry
*/
private SegmentVariableExtractorRegistry segmentVariableExtractorRegistry = new SegmentVariableExtractorRegistry();

/**
* Default constructor
*/
Expand Down Expand Up @@ -221,50 +225,15 @@ public <T> T getVariable(String variableExpression, Class<T> type) {
public Object getVariableObject(final String variableExpression) {
String variableName = VariableUtils.cutOffVariablesPrefix(variableExpression);


if (variableName.startsWith(CitrusSettings.VARIABLE_ESCAPE) && variableName.endsWith(CitrusSettings.VARIABLE_ESCAPE)) {
return CitrusSettings.VARIABLE_PREFIX + VariableUtils.cutOffVariablesEscaping(variableName) + CitrusSettings.VARIABLE_SUFFIX;
} else if (variables.containsKey(variableName)) {
return variables.get(variableName);
} else if (variableName.contains(".")) {
String objectName = variableName.substring(0, variableName.indexOf('.'));
if (variables.containsKey(objectName)) {
return getVariable(variables.get(objectName), variableName.substring(variableName.indexOf('.') + 1));
}
}

throw new CitrusRuntimeException("Unknown variable '" + variableName + "'");
}

/**
* Gets variable from path expression. Variable paths are translated to reflection fields on object instances.
* Path separators are '.'. Each separator is handled as object hierarchy.
*
* @param instance
* @param pathExpression
*/
private Object getVariable(Object instance, String pathExpression) {
String leftOver = null;
String fieldName;
if (pathExpression.contains(".")) {
fieldName = pathExpression.substring(0, pathExpression.indexOf('.'));
leftOver = pathExpression.substring(pathExpression.indexOf('.') + 1);
} else {
fieldName = pathExpression;
}

Field field = ReflectionUtils.findField(instance.getClass(), fieldName);
if (field == null) {
throw new CitrusRuntimeException(String.format("Failed to get variable - unknown field '%s' on type %s", fieldName, instance.getClass().getName()));
} else {
return VariableExpressionIterator.getLastExpressionValue(variableName, this, segmentVariableExtractorRegistry.getSegmentValueExtractors());
}

ReflectionUtils.makeAccessible(field);
Object fieldValue = ReflectionUtils.getField(field, instance);

if (StringUtils.hasText(leftOver)) {
return getVariable(fieldValue, leftOver);
}

return fieldValue;
}

/**
Expand All @@ -273,7 +242,6 @@ private Object getVariable(Object instance, String pathExpression) {
*
* @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 @@ -689,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 Expand Up @@ -1081,4 +1065,5 @@ public Class<?> getTestClass() {
return this.getClass();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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> {

private static final IsJsonPredicate INSTANCE = new IsJsonPredicate();

private IsJsonPredicate() {
// Singleton
}

public static IsJsonPredicate getInstance() {
return INSTANCE;
}

@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,28 @@
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> {

private static final IsXmlPredicate INSTANCE = new IsXmlPredicate();

private IsXmlPredicate() {
// Singleton
}

public static IsXmlPredicate getInstance() {
return INSTANCE;
}

@Override
public boolean test(String toTest) {

if (toTest != null) {
toTest = toTest.trim();
}
return toTest!=null && (toTest.length() == 0 || toTest.startsWith("<"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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 {

/**
* 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