diff options
Diffstat (limited to 'appc-dispatcher/appc-dispatcher-common/ranking-framework-lib/src/test')
3 files changed, 298 insertions, 0 deletions
diff --git a/appc-dispatcher/appc-dispatcher-common/ranking-framework-lib/src/test/java/org/openecomp/appc/rankingframework/TestRankingFramework.java b/appc-dispatcher/appc-dispatcher-common/ranking-framework-lib/src/test/java/org/openecomp/appc/rankingframework/TestRankingFramework.java new file mode 100644 index 000000000..75301dc37 --- /dev/null +++ b/appc-dispatcher/appc-dispatcher-common/ranking-framework-lib/src/test/java/org/openecomp/appc/rankingframework/TestRankingFramework.java @@ -0,0 +1,225 @@ +/*- + * ============LICENSE_START======================================================= + * openECOMP : APP-C + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.appc.rankingframework; + +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; +import org.openecomp.appc.rankingframework.AbstractRankedAttributesResolverFactory; +import org.openecomp.appc.rankingframework.ConfigurationEntry; +import org.openecomp.appc.rankingframework.ConfigurationSet; +import org.openecomp.appc.rankingframework.RankedAttributesContext; +import org.openecomp.appc.rankingframework.RankedAttributesResolver; + +public class TestRankingFramework { + + private static final String COUNTRY = "COUNTRY"; + private static final String STATE = "STATE"; + private static final String CITY = "CITY"; + + private static final List<String> NAMES = Arrays.asList(COUNTRY, STATE, CITY); + + private static final String PACIFIC = "Pacific"; + private static final String ATLANTIC = "Atlantic"; + private static final String ARCTIC = "Arctic"; + private static final String NA = "N/A"; + + private static ConfigurationEntry<String> entry(String [] attributes, String result) { + if (attributes == null || attributes.length != NAMES.size()) { + throw new IllegalArgumentException(); + } + + Map<String, String> map = new HashMap<>(attributes.length); + for (int i = 0; i < attributes.length; i++) { + map.put(NAMES.get(i), attributes[i]); + } + + return new ConfigurationEntryImpl(map, result); + } + + private static ConfigurationSet<String> config(ConfigurationEntry<String> ... entries) { + return new ConfigurationSetImpl(Arrays.asList(entries), NAMES); + } + + private static RankedAttributesContext context(String ... attributes) { + if (attributes == null || attributes.length != NAMES.size()) { + throw new IllegalArgumentException(); + } + + Map<String, String> map = new HashMap<>(attributes.length); + for (int i = 0; i < attributes.length; i++) { + map.put(NAMES.get(i), attributes[i]); + } + + return new Context(map); + } + + private static class ConfigurationSetImpl implements ConfigurationSet<String> { + + private final Collection<ConfigurationEntry<String>> entries; + private final Collection<String> names; + + ConfigurationSetImpl(Collection<ConfigurationEntry<String>> entries, Collection<String> names) { + this.entries = entries; + this.names = names; + } + + @Override + public Iterable<ConfigurationEntry<String>> getEntries() { + return entries; + } + + @Override + public Collection<String> getRankedAttributeNames() { + return names; + } + } + + private static class ConfigurationEntryImpl implements ConfigurationEntry<String> { + + private final Map<String, String> attributes; + private final String result; + + ConfigurationEntryImpl(Map<String, String> attributes, String result) { + this.attributes = attributes; + this.result = result; + } + + @Override + public Object getAttributeValue(String name) { + return attributes.get(name); + } + + @Override + public String getResult() { + return result; + } + } + + private static class Context implements RankedAttributesContext { + + private final Map<String, String> map; + + Context(Map<String, String> map) { + this.map = map; + } + + @Override + public Object getAttributeValue(String name) { + return map.get(name); + } + } + + private static ConfigurationSet<String> testData() { + @SuppressWarnings("unchecked") + ConfigurationSet<String> config = config( + entry(new String [] {"US", "CA", "SFO"}, PACIFIC), + entry(new String [] {"US", "CA", "LA"}, PACIFIC), + entry(new String [] {"US", "FL", "MIAMI"}, ATLANTIC), + entry(new String [] {"US", "AK", "Barrow"}, ARCTIC), + entry(new String [] {"US", "AK", "*"}, PACIFIC), + entry(new String [] {"US", "*", "Houston"}, ATLANTIC), + entry(new String [] {"US", "*", "*"}, NA) + ); + + return config; + } + + private static RankedAttributesResolver<String> resolver(ConfigurationSet<String> config) { + return AbstractRankedAttributesResolverFactory.getInstance().create(config); + } + + @Test + public void testExactMatch() { + + ConfigurationSet<String> config = testData(); + + RankedAttributesResolver<String> resolver = resolver(config) ; + + RankedAttributesContext context = context("US", "CA", "SFO"); + + String result = resolver.resolve(context); + + Assert.assertEquals(PACIFIC, result); + } + + @Test + public void testDefaultMatchPartial() { + + ConfigurationSet<String> config = testData(); + + RankedAttributesResolver<String> resolver = resolver(config) ; + + RankedAttributesContext context = context("US", "AK", "Anchorage"); + + String result = resolver.resolve(context); + + Assert.assertEquals(PACIFIC, result); + } + + @Test + public void testDefaultMatchFull() { + + ConfigurationSet<String> config = testData(); + + RankedAttributesResolver<String> resolver = resolver(config) ; + + RankedAttributesContext context = context("US", "IL", "Chicago"); + + String result = resolver.resolve(context); + + Assert.assertEquals(NA, result); + } + + @Test + public void testDefaultMatchInTheMiddle() { + + ConfigurationSet<String> config = testData(); + + RankedAttributesResolver<String> resolver = resolver(config) ; + + RankedAttributesContext context = context("US", "TX", "Houston"); + + String result = resolver.resolve(context); + + Assert.assertEquals(ATLANTIC, result); + } + + @Test + public void testBacktrace() { + + ConfigurationSet<String> config = testData(); + + RankedAttributesResolver<String> resolver = resolver(config) ; + + RankedAttributesContext context = context("US", "CA", "SJC"); + + String result = resolver.resolve(context); + + Assert.assertEquals(NA, result); + } +} diff --git a/appc-dispatcher/appc-dispatcher-common/ranking-framework-lib/src/test/resources/com/att/eelf/logback.xml b/appc-dispatcher/appc-dispatcher-common/ranking-framework-lib/src/test/resources/com/att/eelf/logback.xml new file mode 100644 index 000000000..2ac88adbc --- /dev/null +++ b/appc-dispatcher/appc-dispatcher-common/ranking-framework-lib/src/test/resources/com/att/eelf/logback.xml @@ -0,0 +1,27 @@ +<!-- + ============LICENSE_START======================================================= + openECOMP : APP-C + ================================================================================ + Copyright (C) 2017 AT&T Intellectual Property. All rights + reserved. + ================================================================================ + 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. + ============LICENSE_END========================================================= + --> + +<configuration scan="true" scanPeriod="3 seconds" debug="false"> + +<!-- + Dummy file to suppress default EELF configuration + --> +</configuration> diff --git a/appc-dispatcher/appc-dispatcher-common/ranking-framework-lib/src/test/resources/logback.xml b/appc-dispatcher/appc-dispatcher-common/ranking-framework-lib/src/test/resources/logback.xml new file mode 100644 index 000000000..c93e4e4a9 --- /dev/null +++ b/appc-dispatcher/appc-dispatcher-common/ranking-framework-lib/src/test/resources/logback.xml @@ -0,0 +1,46 @@ +<!-- + ============LICENSE_START======================================================= + openECOMP : APP-C + ================================================================================ + Copyright (C) 2017 AT&T Intellectual Property. All rights + reserved. + ================================================================================ + 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. + ============LICENSE_END========================================================= + --> + +<configuration scan="true" scanPeriod="3 seconds" debug="false"> + + <property name="logDirectory" value="target" /> + + <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> + <encoder> + <pattern> %.-5level %-36.36logger - %msg%n</pattern> + </encoder> + </appender> + + <appender name="FILE" class="ch.qos.logback.core.FileAppender"> + <file>${logDirectory}/debug.log</file> + <encoder> + <pattern> %.-5level %-36.36logger - %msg%n</pattern> + </encoder> + </appender> + + <logger name="org.openecomp.appc" level="DEBUG" additivity="false"> + <appender-ref ref="FILE" /> + </logger> + + <root level="WARN"> + <appender-ref ref="STDOUT" /> + </root> +</configuration> |