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