2  * ============LICENSE_START=======================================================
 
   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
 
  13  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 
  22  * ============LICENSE_END=========================================================
 
  25 package org.openecomp.appc.rankingframework;
 
  27 import java.util.Arrays;
 
  28 import java.util.Collection;
 
  29 import java.util.HashMap;
 
  30 import java.util.List;
 
  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;
 
  41 public class  TestRankingFramework {
 
  43     private static final String COUNTRY = "COUNTRY";
 
  44     private static final String STATE = "STATE";
 
  45     private static final String CITY = "CITY";
 
  47     private static final List<String> NAMES = Arrays.asList(COUNTRY, STATE, CITY);
 
  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";
 
  54     private static ConfigurationEntry<String> entry(String [] attributes, String result) {
 
  55         if (attributes == null || attributes.length != NAMES.size()) {
 
  56             throw new IllegalArgumentException();
 
  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]);
 
  64         return new ConfigurationEntryImpl(map, result);
 
  67     private static ConfigurationSet<String> config(ConfigurationEntry<String> ... entries) {
 
  68         return new ConfigurationSetImpl(Arrays.asList(entries), NAMES);
 
  71     private static RankedAttributesContext context(String ... attributes) {
 
  72         if (attributes == null || attributes.length != NAMES.size()) {
 
  73             throw new IllegalArgumentException();
 
  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]);
 
  81         return new Context(map);
 
  84     private static class ConfigurationSetImpl implements ConfigurationSet<String> {
 
  86         private final Collection<ConfigurationEntry<String>> entries;
 
  87         private final Collection<String> names;
 
  89         ConfigurationSetImpl(Collection<ConfigurationEntry<String>> entries, Collection<String> names) {
 
  90             this.entries = entries;
 
  95         public Iterable<ConfigurationEntry<String>> getEntries() {
 
 100         public Collection<String> getRankedAttributeNames() {
 
 105     private static class ConfigurationEntryImpl implements ConfigurationEntry<String> {
 
 107         private final Map<String, String> attributes;
 
 108         private final String result;
 
 110         ConfigurationEntryImpl(Map<String, String> attributes, String result) {
 
 111             this.attributes = attributes;
 
 112             this.result = result;
 
 116         public Object getAttributeValue(String name) {
 
 117             return attributes.get(name);
 
 121         public String getResult() {
 
 126     private static class Context implements RankedAttributesContext {
 
 128         private final Map<String, String> map;
 
 130         Context(Map<String, String> map) {
 
 135         public Object getAttributeValue(String name) {
 
 136             return map.get(name);
 
 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)
 
 155     private static RankedAttributesResolver<String> resolver(ConfigurationSet<String> config) {
 
 156         return AbstractRankedAttributesResolverFactory.getInstance().create(config);
 
 160     public void testExactMatch() {
 
 162         ConfigurationSet<String> config = testData();
 
 164         RankedAttributesResolver<String> resolver = resolver(config) ;
 
 166         RankedAttributesContext context = context("US", "CA", "SFO");
 
 168         String result = resolver.resolve(context);
 
 170         Assert.assertEquals(PACIFIC, result);
 
 174     public void testDefaultMatchPartial() {
 
 176         ConfigurationSet<String> config = testData();
 
 178         RankedAttributesResolver<String> resolver = resolver(config) ;
 
 180         RankedAttributesContext context = context("US", "AK", "Anchorage");
 
 182         String result = resolver.resolve(context);
 
 184         Assert.assertEquals(PACIFIC, result);
 
 188     public void testDefaultMatchFull() {
 
 190         ConfigurationSet<String> config = testData();
 
 192         RankedAttributesResolver<String> resolver = resolver(config) ;
 
 194         RankedAttributesContext context = context("US", "IL", "Chicago");
 
 196         String result = resolver.resolve(context);
 
 198         Assert.assertEquals(NA, result);
 
 202     public void testDefaultMatchInTheMiddle() {
 
 204         ConfigurationSet<String> config = testData();
 
 206         RankedAttributesResolver<String> resolver = resolver(config) ;
 
 208         RankedAttributesContext context = context("US", "TX", "Houston");
 
 210         String result = resolver.resolve(context);
 
 212         Assert.assertEquals(ATLANTIC, result);
 
 216     public void testBacktrace() {
 
 218         ConfigurationSet<String> config = testData();
 
 220         RankedAttributesResolver<String> resolver = resolver(config) ;
 
 222         RankedAttributesContext context = context("US", "CA", "SJC");
 
 224         String result = resolver.resolve(context);
 
 226         Assert.assertEquals(NA, result);