added generic fabric support to SO
[so.git] / common / src / test / java / org / onap / so / client / policy / PolicyClientImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.client.policy;
22
23 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertThat;
26 import static org.mockito.Matchers.isA;
27 import static org.mockito.Mockito.doReturn;
28 import static org.mockito.Matchers.any;
29
30 import java.io.File;
31 import java.io.IOException;
32 import java.util.HashMap;
33 import java.util.Map;
34
35 import org.junit.BeforeClass;
36 import org.junit.Ignore;
37 import org.junit.Test;
38 import org.mockito.Mockito;
39 import org.onap.so.client.defaultproperties.PolicyRestPropertiesImpl;
40 import org.onap.so.client.policy.entities.Config;
41 import org.onap.so.client.policy.entities.ConfigRequestParameters;
42 import org.onap.so.client.policy.entities.DictionaryData;
43 import org.onap.so.client.policy.entities.PolicyConfig;
44 import org.onap.so.client.policy.entities.PolicyDecision;
45 import org.onap.so.client.policy.entities.PolicyServiceType;
46
47 import com.fasterxml.jackson.core.JsonParseException;
48 import com.fasterxml.jackson.databind.DeserializationFeature;
49 import com.fasterxml.jackson.databind.JsonMappingException;
50 import com.fasterxml.jackson.databind.ObjectMapper;
51 import com.fasterxml.jackson.databind.SerializationFeature;
52
53 public class PolicyClientImplTest {
54
55         @BeforeClass
56         public static void setUp() {
57                 System.setProperty("mso.config.path", "src/test/resources");
58         }
59
60         private static String RESOURCE_PATH = "src/test/resources/__files/Policy/";
61
62         @Test
63         public void successReadProperties() {
64                 PolicyRestClient client = new PolicyRestClient(new PolicyRestPropertiesImpl(), PolicyServiceType.GET_DECISION);
65                 Map<String, String> map = new HashMap<>();
66                 client.initializeHeaderMap(map);
67                 assertEquals("Found expected Client Auth", "Basic bTAzNzQzOnBvbGljeVIwY2sk", map.get("ClientAuth"));
68                 assertEquals("Found expected Authorization", "Basic dGVzdHBkcDphbHBoYTEyMw==", map.get("Authorization"));
69                 assertEquals("Found expected Environment", "TEST", map.get("Environment"));
70         }
71
72         @Test
73         @Ignore
74         public void getDecisionTest() {
75                 PolicyClient client = new PolicyClientImpl();
76                 PolicyDecision decision = client.getDecision("S", "V", "BB1", "1", "123");
77                 assertEquals("Decision is correct", decision.getDecision(), "PERMIT");
78                 assertEquals("Decision details is correct", decision.getDetails(), "Retry");
79         }
80
81         @Test
82         @Ignore
83         public void getAllowedTreatmentsTest() {
84                 PolicyClient client = new PolicyClientImpl();
85                 DictionaryData dictClient = client.getAllowedTreatments("BB1", "1");
86                 final String dictBbidString = dictClient.getBbid().getString();
87                 final String dictWorkStepString = dictClient.getWorkstep().getString();
88                 assertEquals("DictionaryData matches a response Bbid", dictBbidString, "BB1");
89                 assertEquals("DicitonaryData matches a response WorkStep", dictWorkStepString, "1");
90         }
91
92         @Test
93         public void getDecisionMockTest() {
94                 String serviceType = "S";
95                 String vnfType = "V";
96                 String bbID = "BB1";
97                 String workStep = "1";
98                 String errorCode = "123";
99
100                 PolicyDecision expected = new PolicyDecision();
101                 expected.setDecision("PERMIT");
102                 expected.setDetails("Retry");
103
104                 DecisionAttributes decisionAttributes = new DecisionAttributes();
105                 decisionAttributes.setServiceType(serviceType);
106                 decisionAttributes.setVNFType(vnfType);
107                 decisionAttributes.setBBID(bbID);
108                 decisionAttributes.setWorkStep(workStep);
109                 decisionAttributes.setErrorCode(errorCode);
110                 PolicyClient client = Mockito.spy(PolicyClientImpl.class);
111
112                 doReturn(expected).when(client).getDecision(serviceType, vnfType, bbID, workStep, errorCode);
113
114                 PolicyDecision actual = client.getDecision(serviceType, vnfType, bbID, workStep, errorCode);
115                 assertThat(actual, sameBeanAs(expected));
116         }
117
118         @Test
119         public void getConfigFromStringJsonTest() throws JsonParseException, JsonMappingException, IOException {
120                 PolicyClientImpl client = new PolicyClientImpl();
121                 ObjectMapper mapper = new ObjectMapper();
122                 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
123                 mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
124                 Config expected = mapper.readValue(new File(RESOURCE_PATH + "configJson.json"), Config.class);
125                 PolicyConfig[] returnedPolicyConfigList = mapper.readValue(new File(RESOURCE_PATH + "policyConfig.json"), PolicyConfig[].class);
126                 String configJson = returnedPolicyConfigList[0].getConfig();
127                 Config actual = client.getConfigFromStringJson(configJson);
128
129                 assertThat(actual, sameBeanAs(expected));
130         }
131         
132         @Test
133         public void getConfigWithPolicyNameTest() throws JsonParseException, JsonMappingException, IOException {
134                 PolicyClientImpl client = Mockito.spy(PolicyClientImpl.class);
135                 ObjectMapper mapper = new ObjectMapper();
136                 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
137                 mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
138                 PolicyConfig[] returnedPolicyConfigList = mapper.readValue(new File(RESOURCE_PATH + "policyConfig.json"), PolicyConfig[].class);
139                 Config expected = mapper.readValue(new File(RESOURCE_PATH + "configJson.json"), Config.class);
140                 
141                 PolicyRestClient mockedClient = Mockito.mock(PolicyRestClient.class);
142                 doReturn(mockedClient).when(client).getPolicyRestClient(PolicyServiceType.GET_CONFIG);
143                 doReturn(returnedPolicyConfigList).when(mockedClient).post(isA(ConfigRequestParameters.class), any());
144                 
145                 Config actual = client.getConfigWithPolicyName("policyName");
146                 
147                 assertThat(actual, sameBeanAs(expected));
148                 
149         }
150 }