Updating licenses in all files
[appc.git] / appc-dispatcher / appc-dispatcher-common / ranking-framework-lib / src / test / java / org / openecomp / appc / rankingframework / TestRankingFramework.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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.
21  */
22
23 package org.openecomp.appc.rankingframework;
24
25 import java.util.Arrays;
26 import java.util.Collection;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30
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;
38
39 public class  TestRankingFramework {
40
41     private static final String COUNTRY = "COUNTRY";
42     private static final String STATE = "STATE";
43     private static final String CITY = "CITY";
44
45     private static final List<String> NAMES = Arrays.asList(COUNTRY, STATE, CITY);
46
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";
51
52     private static ConfigurationEntry<String> entry(String [] attributes, String result) {
53         if (attributes == null || attributes.length != NAMES.size()) {
54             throw new IllegalArgumentException();
55         }
56
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]);
60         }
61
62         return new ConfigurationEntryImpl(map, result);
63     }
64
65     private static ConfigurationSet<String> config(ConfigurationEntry<String> ... entries) {
66         return new ConfigurationSetImpl(Arrays.asList(entries), NAMES);
67     }
68
69     private static RankedAttributesContext context(String ... attributes) {
70         if (attributes == null || attributes.length != NAMES.size()) {
71             throw new IllegalArgumentException();
72         }
73
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]);
77         }
78
79         return new Context(map);
80     }
81
82     private static class ConfigurationSetImpl implements ConfigurationSet<String> {
83
84         private final Collection<ConfigurationEntry<String>> entries;
85         private final Collection<String> names;
86
87         ConfigurationSetImpl(Collection<ConfigurationEntry<String>> entries, Collection<String> names) {
88             this.entries = entries;
89             this.names = names;
90         }
91
92         @Override
93         public Iterable<ConfigurationEntry<String>> getEntries() {
94             return entries;
95         }
96
97         @Override
98         public Collection<String> getRankedAttributeNames() {
99             return names;
100         }
101     }
102
103     private static class ConfigurationEntryImpl implements ConfigurationEntry<String> {
104
105         private final Map<String, String> attributes;
106         private final String result;
107
108         ConfigurationEntryImpl(Map<String, String> attributes, String result) {
109             this.attributes = attributes;
110             this.result = result;
111         }
112
113         @Override
114         public Object getAttributeValue(String name) {
115             return attributes.get(name);
116         }
117
118         @Override
119         public String getResult() {
120             return result;
121         }
122     }
123
124     private static class Context implements RankedAttributesContext {
125
126         private final Map<String, String> map;
127
128         Context(Map<String, String> map) {
129             this.map = map;
130         }
131
132         @Override
133         public Object getAttributeValue(String name) {
134             return map.get(name);
135         }
136     }
137
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)
148                 );
149
150         return config;
151     }
152
153     private static RankedAttributesResolver<String> resolver(ConfigurationSet<String> config) {
154         return AbstractRankedAttributesResolverFactory.getInstance().create(config);
155     }
156
157     @Test
158     public void testExactMatch() {
159
160         ConfigurationSet<String> config = testData();
161
162         RankedAttributesResolver<String> resolver = resolver(config) ;
163
164         RankedAttributesContext context = context("US", "CA", "SFO");
165
166         String result = resolver.resolve(context);
167
168         Assert.assertEquals(PACIFIC, result);
169     }
170
171     @Test
172     public void testDefaultMatchPartial() {
173
174         ConfigurationSet<String> config = testData();
175
176         RankedAttributesResolver<String> resolver = resolver(config) ;
177
178         RankedAttributesContext context = context("US", "AK", "Anchorage");
179
180         String result = resolver.resolve(context);
181
182         Assert.assertEquals(PACIFIC, result);
183     }
184
185     @Test
186     public void testDefaultMatchFull() {
187
188         ConfigurationSet<String> config = testData();
189
190         RankedAttributesResolver<String> resolver = resolver(config) ;
191
192         RankedAttributesContext context = context("US", "IL", "Chicago");
193
194         String result = resolver.resolve(context);
195
196         Assert.assertEquals(NA, result);
197     }
198
199     @Test
200     public void testDefaultMatchInTheMiddle() {
201
202         ConfigurationSet<String> config = testData();
203
204         RankedAttributesResolver<String> resolver = resolver(config) ;
205
206         RankedAttributesContext context = context("US", "TX", "Houston");
207
208         String result = resolver.resolve(context);
209
210         Assert.assertEquals(ATLANTIC, result);
211     }
212
213     @Test
214     public void testBacktrace() {
215
216         ConfigurationSet<String> config = testData();
217
218         RankedAttributesResolver<String> resolver = resolver(config) ;
219
220         RankedAttributesContext context = context("US", "CA", "SJC");
221
222         String result = resolver.resolve(context);
223
224         Assert.assertEquals(NA, result);
225     }
226 }