2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.rankingframework;
27 import java.util.Arrays;
28 import java.util.Collection;
29 import java.util.HashMap;
30 import java.util.List;
33 import org.junit.Assert;
34 import org.junit.Test;
36 public class TestRankingFramework {
38 private static final String COUNTRY = "COUNTRY";
39 private static final String STATE = "STATE";
40 private static final String CITY = "CITY";
42 private static final List<String> NAMES = Arrays.asList(COUNTRY, STATE, CITY);
44 private static final String PACIFIC = "Pacific";
45 private static final String ATLANTIC = "Atlantic";
46 private static final String ARCTIC = "Arctic";
47 private static final String NA = "N/A";
49 private static ConfigurationEntry<String> entry(String [] attributes, String result) {
50 if (attributes == null || attributes.length != NAMES.size()) {
51 throw new IllegalArgumentException();
54 Map<String, String> map = new HashMap<>(attributes.length);
55 for (int i = 0; i < attributes.length; i++) {
56 map.put(NAMES.get(i), attributes[i]);
59 return new ConfigurationEntryImpl(map, result);
63 private static ConfigurationSet<String> config(ConfigurationEntry<String> ... entries) {
64 return new ConfigurationSetImpl(Arrays.asList(entries), NAMES);
67 private static RankedAttributesContext context(String ... attributes) {
68 if (attributes == null || attributes.length != NAMES.size()) {
69 throw new IllegalArgumentException();
72 Map<String, String> map = new HashMap<>(attributes.length);
73 for (int i = 0; i < attributes.length; i++) {
74 map.put(NAMES.get(i), attributes[i]);
77 return new Context(map);
80 private static class ConfigurationSetImpl implements ConfigurationSet<String> {
82 private final Collection<ConfigurationEntry<String>> entries;
83 private final Collection<String> names;
85 ConfigurationSetImpl(Collection<ConfigurationEntry<String>> entries, Collection<String> names) {
86 this.entries = entries;
91 public Iterable<ConfigurationEntry<String>> getEntries() {
96 public Collection<String> getRankedAttributeNames() {
101 private static class ConfigurationEntryImpl implements ConfigurationEntry<String> {
103 private final Map<String, String> attributes;
104 private final String result;
106 ConfigurationEntryImpl(Map<String, String> attributes, String result) {
107 this.attributes = attributes;
108 this.result = result;
112 public Object getAttributeValue(String name) {
113 return attributes.get(name);
117 public String getResult() {
122 private static class Context implements RankedAttributesContext {
124 private final Map<String, String> map;
126 Context(Map<String, String> map) {
131 public Object getAttributeValue(String name) {
132 return map.get(name);
136 private static ConfigurationSet<String> testData() {
137 @SuppressWarnings("unchecked")
138 ConfigurationSet<String> config = config(
139 entry(new String [] {"US", "CA", "SFO"}, PACIFIC),
140 entry(new String [] {"US", "CA", "LA"}, PACIFIC),
141 entry(new String [] {"US", "FL", "MIAMI"}, ATLANTIC),
142 entry(new String [] {"US", "AK", "Barrow"}, ARCTIC),
143 entry(new String [] {"US", "AK", "*"}, PACIFIC),
144 entry(new String [] {"US", "*", "Houston"}, ATLANTIC),
145 entry(new String [] {"US", "*", "*"}, NA)
151 private static RankedAttributesResolver<String> resolver(ConfigurationSet<String> config) {
152 return AbstractRankedAttributesResolverFactory.getInstance().create(config);
156 public void testExactMatch() {
158 ConfigurationSet<String> config = testData();
160 RankedAttributesResolver<String> resolver = resolver(config) ;
162 RankedAttributesContext context = context("US", "CA", "SFO");
164 String result = resolver.resolve(context);
166 Assert.assertEquals(PACIFIC, result);
170 public void testDefaultMatchPartial() {
172 ConfigurationSet<String> config = testData();
174 RankedAttributesResolver<String> resolver = resolver(config) ;
176 RankedAttributesContext context = context("US", "AK", "Anchorage");
178 String result = resolver.resolve(context);
180 Assert.assertEquals(PACIFIC, result);
184 public void testDefaultMatchFull() {
186 ConfigurationSet<String> config = testData();
188 RankedAttributesResolver<String> resolver = resolver(config) ;
190 RankedAttributesContext context = context("US", "IL", "Chicago");
192 String result = resolver.resolve(context);
194 Assert.assertEquals(NA, result);
198 public void testDefaultMatchInTheMiddle() {
200 ConfigurationSet<String> config = testData();
202 RankedAttributesResolver<String> resolver = resolver(config) ;
204 RankedAttributesContext context = context("US", "TX", "Houston");
206 String result = resolver.resolve(context);
208 Assert.assertEquals(ATLANTIC, result);
212 public void testBacktrace() {
214 ConfigurationSet<String> config = testData();
216 RankedAttributesResolver<String> resolver = resolver(config) ;
218 RankedAttributesContext context = context("US", "CA", "SJC");
220 String result = resolver.resolve(context);
222 Assert.assertEquals(NA, result);