com.consol.citrus.functions.core.MapValueFunction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of citrus-base Show documentation
Show all versions of citrus-base Show documentation
Citrus base and default implementation
/*
* 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.functions.core;
import java.util.List;
import java.util.Map;
import com.consol.citrus.common.InitializingPhase;
import com.consol.citrus.context.TestContext;
import com.consol.citrus.exceptions.CitrusRuntimeException;
import com.consol.citrus.exceptions.InvalidFunctionUsageException;
import com.consol.citrus.functions.Function;
/**
* Function to map the function's argument to a corresponding value configured using a map.
* Example of the function definition and its usage:
*
*
*
* <bean id="myCustomFunctionLibrary" class="com.consol.citrus.functions.FunctionLibrary">
* <property name="name" value="myCustomFunctionLibrary" />
* <property name="prefix" value="custom:" />
* <property name="members">
* <map>
* <entry key="mapHttpStatusCodeToMessage">
* <bean class="com.consol.citrus.functions.core.MapValueFunction">
* <property name="values">
* <map>
* <entry key="200" value="OK" />
* <entry key="401" value="Unauthorized" />
* <entry key="500" value="Internal Server Error" />
* </map>
* </property>
* </bean>
* </entry>
* </map>
* </property>
* </bean>
*
*
* and the corresponding usage in a test which maps the HTTP status code 500 to its message 'Internal Server Error':
*
*
* <variable name="httpStatusCodeMessage" value="custom:mapHttpStatusCodeToMessage('500')" />
*
*
*
* @author Dimo Velev ([email protected])
*
*/
public class MapValueFunction implements Function, InitializingPhase {
/** Mappings for key value logic in this function */
private Map map;
@Override
public String execute(List params, TestContext context) {
if (params.size() != 1) {
throw new InvalidFunctionUsageException("Expected exactly one argument but got " + params.size());
}
final String key = params.get(0);
final String result = map.get(key);
if (result == null) {
throw new InvalidFunctionUsageException("No mapping found for \"" + key + "\"");
}
return result;
}
@Override
public void initialize() {
if (map == null) {
throw new CitrusRuntimeException("MapValueFunction must not use an empty value map");
}
}
/**
* Gets the mappings for this function.
* @return
*/
public Map getMap() {
return map;
}
/**
* Sets the mappings for this function.
* @param map
*/
public void setMap(Map map) {
this.map = map;
}
}