Mercurial > stress-tester
changeset 823:f85afb8f6d54
RuleSessionMapperValueTransformer
author | Devel 2 |
---|---|
date | Thu, 18 Jan 2018 11:51:18 +0100 |
parents | 8977a1a9087a |
children | 28ec5d69ea33 |
files | stress-tester/src/main/java/com/passus/st/emitter/RuleSessionMapperValueTransformer.java stress-tester/src/test/java/com/passus/st/emitter/RuleSessionMapperValueTransformerTest.java |
diffstat | 2 files changed, 57 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/emitter/RuleSessionMapperValueTransformer.java Thu Jan 18 11:51:18 2018 +0100 @@ -0,0 +1,33 @@ +package com.passus.st.emitter; + +import com.passus.commons.ConversionException; +import com.passus.config.ValueTransformer; + +/** + * + * @author Mirosław Hawrot + */ +public class RuleSessionMapperValueTransformer implements ValueTransformer { + + public static final RuleSessionMapperValueTransformer INSTANCE = new RuleSessionMapperValueTransformer(); + + @Override + public Object transform(Object value) throws ConversionException { + if (value == null) { + return null; + } + + String str = value.toString(); + try { + return RuleBasedSessionMapper.parseRule(str); + } catch (Exception e) { + throw new ConversionException("Unable to convert '" + str + "' to session mapper rule. " + e.getMessage(), e); + } + } + + @Override + public Object reverseTransform(Object obj) throws ConversionException { + throw new UnsupportedOperationException("Not supported yet."); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/test/java/com/passus/st/emitter/RuleSessionMapperValueTransformerTest.java Thu Jan 18 11:51:18 2018 +0100 @@ -0,0 +1,24 @@ +package com.passus.st.emitter; + +import com.passus.net.SocketAddress; +import static org.testng.Assert.*; +import org.testng.annotations.Test; + +/** + * + * @author Mirosław Hawrot + */ +public class RuleSessionMapperValueTransformerTest { + + @Test + public void testTransform() throws Exception { + RuleSessionMapperValueTransformer trans = new RuleSessionMapperValueTransformer(); + String value = "1.1.1.1:* -> 2.2.2.2:90"; + Object res = trans.transform(value); + assertTrue(res instanceof RuleBasedSessionMapper.Rule); + RuleBasedSessionMapper.Rule rule = (RuleBasedSessionMapper.Rule) res; + assertEquals(new SocketAddress("2.2.2.2:90"), rule.getRemoteAddress()); + assertEquals(SessionMapper.ANY_SOCKET, rule.getBindAddress()); + } + +}