48a7753f93edef13e29d9b0fea84aadebaa6d4de
[appc.git] / appc-dispatcher / appc-dispatcher-common / ranking-framework-lib / src / test / java / org / openecomp / 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.openecomp.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 import org.openecomp.appc.rankingframework.AbstractRankedAttributesResolverFactory;
36 import org.openecomp.appc.rankingframework.ConfigurationEntry;
37 import org.openecomp.appc.rankingframework.ConfigurationSet;
38 import org.openecomp.appc.rankingframework.RankedAttributesContext;
39 import org.openecomp.appc.rankingframework.RankedAttributesResolver;
40
41 public class  TestRankingFramework {
42
43     private static final String COUNTRY = "COUNTRY";
44     private static final String STATE = "STATE";
45     private static final String CITY = "CITY";
46
47     private static final List<String> NAMES = Arrays.asList(COUNTRY, STATE, CITY);
48
49     private static final String PACIFIC = "Pacific";
50     private static final String ATLANTIC = "Atlantic";
51     private static final String ARCTIC = "Arctic";
52     private static final String NA = "N/A";
53
54     private static ConfigurationEntry<String> entry(String [] attributes, String result) {
55         if (attributes == null || attributes.length != NAMES.size()) {
56             throw new IllegalArgumentException();
57         }
58
59         Map<String, String> map = new HashMap<>(attributes.length);
60         for (int i = 0; i < attributes.length; i++) {
61             map.put(NAMES.get(i), attributes[i]);
62         }
63
64         return new ConfigurationEntryImpl(map, result);
65     }
66
67     private static ConfigurationSet<String> config(ConfigurationEntry<String> ... entries) {
68         return new ConfigurationSetImpl(Arrays.asList(entries), NAMES);
69     }
70
71     private static RankedAttributesContext context(String ... attributes) {
72         if (attributes == null || attributes.length != NAMES.size()) {
73             throw new IllegalArgumentException();
74         }
75
76         Map<String, String> map = new HashMap<>(attributes.length);
77         for (int i = 0; i < attributes.length; i++) {
78             map.put(NAMES.get(i), attributes[i]);
79         }
80
81         return new Context(map);
82     }
83
84     private static class ConfigurationSetImpl implements ConfigurationSet<String> {
85
86         private final Collection<ConfigurationEntry<String>> entries;
87         private final Collection<String> names;
88
89         ConfigurationSetImpl(Collection<ConfigurationEntry<String>> entries, Collection<String> names) {
90             this.entries = entries;
91             this.names = names;
92         }
93
94         @Override
95         public Iterable<ConfigurationEntry<String>> getEntries() {
96             return entries;
97         }
98
99         @Override
100         public Collection<String> getRankedAttributeNames() {
101             return names;
102         }
103     }
104
105     private static class ConfigurationEntryImpl implements ConfigurationEntry<String> {
106
107         private final Map<String, String> attributes;
108         private final String result;
109
110         ConfigurationEntryImpl(Map<String, String> attributes, String result) {
111             this.attributes = attributes;
112             this.result = result;
113         }
114
115         @Override
116         public Object getAttributeValue(String name) {
117             return attributes.get(name);
118         }
119
120         @Override
121         public String getResult() {
122             return result;
123         }
124     }
125
126     private static class Context implements RankedAttributesContext {
127
128         private final Map<String, String> map;
129
130         Context(Map<String, String> map) {
131             this.map = map;
132         }
133
134         @Override
135         public Object getAttributeValue(String name) {
136             return map.get(name);
137         }
138     }
139
140     private static ConfigurationSet<String> testData() {
141         @SuppressWarnings("unchecked")
142         ConfigurationSet<String> config = config(
143                 entry(new String [] {"US", "CA", "SFO"}, PACIFIC),
144                 entry(new String [] {"US", "CA", "LA"}, PACIFIC),
145                 entry(new String [] {"US", "FL", "MIAMI"}, ATLANTIC),
146                 entry(new String [] {"US", "AK", "Barrow"}, ARCTIC),
147                 entry(new String [] {"US", "AK", "*"}, PACIFIC),
148                 entry(new String [] {"US", "*", "Houston"}, ATLANTIC),
149                 entry(new String [] {"US", "*", "*"}, NA)
150                 );
151
152         return config;
153     }
154
155     private static RankedAttributesResolver<String> resolver(ConfigurationSet<String> config) {
156         return AbstractRankedAttributesResolverFactory.getInstance().create(config);
157     }
158
159     @Test
160     public void testExactMatch() {
161
162         ConfigurationSet<String> config = testData();
163
164         RankedAttributesResolver<String> resolver = resolver(config) ;
165
166         RankedAttributesContext context = context("US", "CA", "SFO");
167
168         String result = resolver.resolve(context);
169
170         Assert.assertEquals(PACIFIC, result);
171     }
172
173     @Test
174     public void testDefaultMatchPartial() {
175
176         ConfigurationSet<String> config = testData();
177
178         RankedAttributesResolver<String> resolver = resolver(config) ;
179
180         RankedAttributesContext context = context("US", "AK", "Anchorage");
181
182         String result = resolver.resolve(context);
183
184         Assert.assertEquals(PACIFIC, result);
185     }
186
187     @Test
188     public void testDefaultMatchFull() {
189
190         ConfigurationSet<String> config = testData();
191
192         RankedAttributesResolver<String> resolver = resolver(config) ;
193
194         RankedAttributesContext context = context("US", "IL", "Chicago");
195
196         String result = resolver.resolve(context);
197
198         Assert.assertEquals(NA, result);
199     }
200
201     @Test
202     public void testDefaultMatchInTheMiddle() {
203
204         ConfigurationSet<String> config = testData();
205
206         RankedAttributesResolver<String> resolver = resolver(config) ;
207
208         RankedAttributesContext context = context("US", "TX", "Houston");
209
210         String result = resolver.resolve(context);
211
212         Assert.assertEquals(ATLANTIC, result);
213     }
214
215     @Test
216     public void testBacktrace() {
217
218         ConfigurationSet<String> config = testData();
219
220         RankedAttributesResolver<String> resolver = resolver(config) ;
221
222         RankedAttributesContext context = context("US", "CA", "SJC");
223
224         String result = resolver.resolve(context);
225
226         Assert.assertEquals(NA, result);
227     }
228 }