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