2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.appc.rankingframework;
25 import java.util.Arrays;
26 import java.util.Collection;
27 import java.util.HashMap;
28 import java.util.List;
31 import org.junit.Assert;
32 import org.junit.Test;
33 import org.openecomp.appc.rankingframework.AbstractRankedAttributesResolverFactory;
34 import org.openecomp.appc.rankingframework.ConfigurationEntry;
35 import org.openecomp.appc.rankingframework.ConfigurationSet;
36 import org.openecomp.appc.rankingframework.RankedAttributesContext;
37 import org.openecomp.appc.rankingframework.RankedAttributesResolver;
39 public class TestRankingFramework {
41 private static final String COUNTRY = "COUNTRY";
42 private static final String STATE = "STATE";
43 private static final String CITY = "CITY";
45 private static final List<String> NAMES = Arrays.asList(COUNTRY, STATE, CITY);
47 private static final String PACIFIC = "Pacific";
48 private static final String ATLANTIC = "Atlantic";
49 private static final String ARCTIC = "Arctic";
50 private static final String NA = "N/A";
52 private static ConfigurationEntry<String> entry(String [] attributes, String result) {
53 if (attributes == null || attributes.length != NAMES.size()) {
54 throw new IllegalArgumentException();
57 Map<String, String> map = new HashMap<>(attributes.length);
58 for (int i = 0; i < attributes.length; i++) {
59 map.put(NAMES.get(i), attributes[i]);
62 return new ConfigurationEntryImpl(map, result);
65 private static ConfigurationSet<String> config(ConfigurationEntry<String> ... entries) {
66 return new ConfigurationSetImpl(Arrays.asList(entries), NAMES);
69 private static RankedAttributesContext context(String ... attributes) {
70 if (attributes == null || attributes.length != NAMES.size()) {
71 throw new IllegalArgumentException();
74 Map<String, String> map = new HashMap<>(attributes.length);
75 for (int i = 0; i < attributes.length; i++) {
76 map.put(NAMES.get(i), attributes[i]);
79 return new Context(map);
82 private static class ConfigurationSetImpl implements ConfigurationSet<String> {
84 private final Collection<ConfigurationEntry<String>> entries;
85 private final Collection<String> names;
87 ConfigurationSetImpl(Collection<ConfigurationEntry<String>> entries, Collection<String> names) {
88 this.entries = entries;
93 public Iterable<ConfigurationEntry<String>> getEntries() {
98 public Collection<String> getRankedAttributeNames() {
103 private static class ConfigurationEntryImpl implements ConfigurationEntry<String> {
105 private final Map<String, String> attributes;
106 private final String result;
108 ConfigurationEntryImpl(Map<String, String> attributes, String result) {
109 this.attributes = attributes;
110 this.result = result;
114 public Object getAttributeValue(String name) {
115 return attributes.get(name);
119 public String getResult() {
124 private static class Context implements RankedAttributesContext {
126 private final Map<String, String> map;
128 Context(Map<String, String> map) {
133 public Object getAttributeValue(String name) {
134 return map.get(name);
138 private static ConfigurationSet<String> testData() {
139 @SuppressWarnings("unchecked")
140 ConfigurationSet<String> config = config(
141 entry(new String [] {"US", "CA", "SFO"}, PACIFIC),
142 entry(new String [] {"US", "CA", "LA"}, PACIFIC),
143 entry(new String [] {"US", "FL", "MIAMI"}, ATLANTIC),
144 entry(new String [] {"US", "AK", "Barrow"}, ARCTIC),
145 entry(new String [] {"US", "AK", "*"}, PACIFIC),
146 entry(new String [] {"US", "*", "Houston"}, ATLANTIC),
147 entry(new String [] {"US", "*", "*"}, NA)
153 private static RankedAttributesResolver<String> resolver(ConfigurationSet<String> config) {
154 return AbstractRankedAttributesResolverFactory.getInstance().create(config);
158 public void testExactMatch() {
160 ConfigurationSet<String> config = testData();
162 RankedAttributesResolver<String> resolver = resolver(config) ;
164 RankedAttributesContext context = context("US", "CA", "SFO");
166 String result = resolver.resolve(context);
168 Assert.assertEquals(PACIFIC, result);
172 public void testDefaultMatchPartial() {
174 ConfigurationSet<String> config = testData();
176 RankedAttributesResolver<String> resolver = resolver(config) ;
178 RankedAttributesContext context = context("US", "AK", "Anchorage");
180 String result = resolver.resolve(context);
182 Assert.assertEquals(PACIFIC, result);
186 public void testDefaultMatchFull() {
188 ConfigurationSet<String> config = testData();
190 RankedAttributesResolver<String> resolver = resolver(config) ;
192 RankedAttributesContext context = context("US", "IL", "Chicago");
194 String result = resolver.resolve(context);
196 Assert.assertEquals(NA, result);
200 public void testDefaultMatchInTheMiddle() {
202 ConfigurationSet<String> config = testData();
204 RankedAttributesResolver<String> resolver = resolver(config) ;
206 RankedAttributesContext context = context("US", "TX", "Houston");
208 String result = resolver.resolve(context);
210 Assert.assertEquals(ATLANTIC, result);
214 public void testBacktrace() {
216 ConfigurationSet<String> config = testData();
218 RankedAttributesResolver<String> resolver = resolver(config) ;
220 RankedAttributesContext context = context("US", "CA", "SJC");
222 String result = resolver.resolve(context);
224 Assert.assertEquals(NA, result);