2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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=========================================================
22 package org.openecomp.appc.rankingframework;
24 import java.util.Arrays;
25 import java.util.Collection;
26 import java.util.HashMap;
27 import java.util.List;
30 import org.junit.Assert;
31 import org.junit.Test;
32 import org.openecomp.appc.rankingframework.AbstractRankedAttributesResolverFactory;
33 import org.openecomp.appc.rankingframework.ConfigurationEntry;
34 import org.openecomp.appc.rankingframework.ConfigurationSet;
35 import org.openecomp.appc.rankingframework.RankedAttributesContext;
36 import org.openecomp.appc.rankingframework.RankedAttributesResolver;
38 public class TestRankingFramework {
40 private static final String COUNTRY = "COUNTRY";
41 private static final String STATE = "STATE";
42 private static final String CITY = "CITY";
44 private static final List<String> NAMES = Arrays.asList(COUNTRY, STATE, CITY);
46 private static final String PACIFIC = "Pacific";
47 private static final String ATLANTIC = "Atlantic";
48 private static final String ARCTIC = "Arctic";
49 private static final String NA = "N/A";
51 private static ConfigurationEntry<String> entry(String [] attributes, String result) {
52 if (attributes == null || attributes.length != NAMES.size()) {
53 throw new IllegalArgumentException();
56 Map<String, String> map = new HashMap<>(attributes.length);
57 for (int i = 0; i < attributes.length; i++) {
58 map.put(NAMES.get(i), attributes[i]);
61 return new ConfigurationEntryImpl(map, result);
64 private static ConfigurationSet<String> config(ConfigurationEntry<String> ... entries) {
65 return new ConfigurationSetImpl(Arrays.asList(entries), NAMES);
68 private static RankedAttributesContext context(String ... attributes) {
69 if (attributes == null || attributes.length != NAMES.size()) {
70 throw new IllegalArgumentException();
73 Map<String, String> map = new HashMap<>(attributes.length);
74 for (int i = 0; i < attributes.length; i++) {
75 map.put(NAMES.get(i), attributes[i]);
78 return new Context(map);
81 private static class ConfigurationSetImpl implements ConfigurationSet<String> {
83 private final Collection<ConfigurationEntry<String>> entries;
84 private final Collection<String> names;
86 ConfigurationSetImpl(Collection<ConfigurationEntry<String>> entries, Collection<String> names) {
87 this.entries = entries;
92 public Iterable<ConfigurationEntry<String>> getEntries() {
97 public Collection<String> getRankedAttributeNames() {
102 private static class ConfigurationEntryImpl implements ConfigurationEntry<String> {
104 private final Map<String, String> attributes;
105 private final String result;
107 ConfigurationEntryImpl(Map<String, String> attributes, String result) {
108 this.attributes = attributes;
109 this.result = result;
113 public Object getAttributeValue(String name) {
114 return attributes.get(name);
118 public String getResult() {
123 private static class Context implements RankedAttributesContext {
125 private final Map<String, String> map;
127 Context(Map<String, String> map) {
132 public Object getAttributeValue(String name) {
133 return map.get(name);
137 private static ConfigurationSet<String> testData() {
138 @SuppressWarnings("unchecked")
139 ConfigurationSet<String> config = config(
140 entry(new String [] {"US", "CA", "SFO"}, PACIFIC),
141 entry(new String [] {"US", "CA", "LA"}, PACIFIC),
142 entry(new String [] {"US", "FL", "MIAMI"}, ATLANTIC),
143 entry(new String [] {"US", "AK", "Barrow"}, ARCTIC),
144 entry(new String [] {"US", "AK", "*"}, PACIFIC),
145 entry(new String [] {"US", "*", "Houston"}, ATLANTIC),
146 entry(new String [] {"US", "*", "*"}, NA)
152 private static RankedAttributesResolver<String> resolver(ConfigurationSet<String> config) {
153 return AbstractRankedAttributesResolverFactory.getInstance().create(config);
157 public void testExactMatch() {
159 ConfigurationSet<String> config = testData();
161 RankedAttributesResolver<String> resolver = resolver(config) ;
163 RankedAttributesContext context = context("US", "CA", "SFO");
165 String result = resolver.resolve(context);
167 Assert.assertEquals(PACIFIC, result);
171 public void testDefaultMatchPartial() {
173 ConfigurationSet<String> config = testData();
175 RankedAttributesResolver<String> resolver = resolver(config) ;
177 RankedAttributesContext context = context("US", "AK", "Anchorage");
179 String result = resolver.resolve(context);
181 Assert.assertEquals(PACIFIC, result);
185 public void testDefaultMatchFull() {
187 ConfigurationSet<String> config = testData();
189 RankedAttributesResolver<String> resolver = resolver(config) ;
191 RankedAttributesContext context = context("US", "IL", "Chicago");
193 String result = resolver.resolve(context);
195 Assert.assertEquals(NA, result);
199 public void testDefaultMatchInTheMiddle() {
201 ConfigurationSet<String> config = testData();
203 RankedAttributesResolver<String> resolver = resolver(config) ;
205 RankedAttributesContext context = context("US", "TX", "Houston");
207 String result = resolver.resolve(context);
209 Assert.assertEquals(ATLANTIC, result);
213 public void testBacktrace() {
215 ConfigurationSet<String> config = testData();
217 RankedAttributesResolver<String> resolver = resolver(config) ;
219 RankedAttributesContext context = context("US", "CA", "SJC");
221 String result = resolver.resolve(context);
223 Assert.assertEquals(NA, result);