Replaced all tabs with spaces in java and pom.xml
[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.ArgumentMatchers.isA;
27 import static org.mockito.Mockito.doReturn;
28 import static org.mockito.ArgumentMatchers.any;
29 import java.io.File;
30 import java.io.IOException;
31 import java.util.HashMap;
32 import java.util.Map;
33 import org.junit.BeforeClass;
34 import org.junit.Ignore;
35 import org.junit.Test;
36 import org.mockito.Mockito;
37 import org.onap.so.client.defaultproperties.PolicyRestPropertiesImpl;
38 import org.onap.so.client.policy.entities.Config;
39 import org.onap.so.client.policy.entities.ConfigRequestParameters;
40 import org.onap.so.client.policy.entities.DictionaryData;
41 import org.onap.so.client.policy.entities.PolicyConfig;
42 import org.onap.so.client.policy.entities.PolicyDecision;
43 import org.onap.so.client.policy.entities.PolicyServiceType;
44 import com.fasterxml.jackson.core.JsonParseException;
45 import com.fasterxml.jackson.databind.DeserializationFeature;
46 import com.fasterxml.jackson.databind.JsonMappingException;
47 import com.fasterxml.jackson.databind.ObjectMapper;
48 import com.fasterxml.jackson.databind.SerializationFeature;
49
50 public class PolicyClientImplTest {
51
52     @BeforeClass
53     public static void setUp() {
54         System.setProperty("mso.config.path", "src/test/resources");
55     }
56
57     private static String RESOURCE_PATH = "src/test/resources/__files/Policy/";
58
59     @Test
60     public void successReadProperties() {
61         PolicyRestClient client = new PolicyRestClient(new PolicyRestPropertiesImpl(), PolicyServiceType.GET_DECISION);
62         Map<String, String> map = new HashMap<>();
63         client.initializeHeaderMap(map);
64         assertEquals("Found expected Client Auth", "Basic bTAzNzQzOnBvbGljeVIwY2sk", map.get("ClientAuth"));
65         assertEquals("Found expected Authorization", "Basic dGVzdHBkcDphbHBoYTEyMw==", map.get("Authorization"));
66         assertEquals("Found expected Environment", "TEST", map.get("Environment"));
67     }
68
69     @Test
70     @Ignore
71     public void getDecisionTest() {
72         PolicyClient client = new PolicyClientImpl();
73         PolicyDecision decision = client.getDecision("S", "V", "BB1", "1", "123");
74         assertEquals("Decision is correct", decision.getDecision(), "PERMIT");
75         assertEquals("Decision details is correct", decision.getDetails(), "Retry");
76     }
77
78     @Test
79     @Ignore
80     public void getAllowedTreatmentsTest() {
81         PolicyClient client = new PolicyClientImpl();
82         DictionaryData dictClient = client.getAllowedTreatments("BB1", "1");
83         final String dictBbidString = dictClient.getBbid().getString();
84         final String dictWorkStepString = dictClient.getWorkstep().getString();
85         assertEquals("DictionaryData matches a response Bbid", dictBbidString, "BB1");
86         assertEquals("DicitonaryData matches a response WorkStep", dictWorkStepString, "1");
87     }
88
89     @Test
90     public void getDecisionMockTest() {
91         String serviceType = "S";
92         String vnfType = "V";
93         String bbID = "BB1";
94         String workStep = "1";
95         String errorCode = "123";
96
97         PolicyDecision expected = new PolicyDecision();
98         expected.setDecision("PERMIT");
99         expected.setDetails("Retry");
100
101         DecisionAttributes decisionAttributes = new DecisionAttributes();
102         decisionAttributes.setServiceType(serviceType);
103         decisionAttributes.setVNFType(vnfType);
104         decisionAttributes.setBBID(bbID);
105         decisionAttributes.setWorkStep(workStep);
106         decisionAttributes.setErrorCode(errorCode);
107         PolicyClient client = Mockito.spy(PolicyClientImpl.class);
108
109         doReturn(expected).when(client).getDecision(serviceType, vnfType, bbID, workStep, errorCode);
110
111         PolicyDecision actual = client.getDecision(serviceType, vnfType, bbID, workStep, errorCode);
112         assertThat(actual, sameBeanAs(expected));
113     }
114
115     @Test
116     public void getConfigFromStringJsonTest() throws JsonParseException, JsonMappingException, IOException {
117         PolicyClientImpl client = new PolicyClientImpl();
118         ObjectMapper mapper = new ObjectMapper();
119         mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
120         mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
121         Config expected = mapper.readValue(new File(RESOURCE_PATH + "configJson.json"), Config.class);
122         PolicyConfig[] returnedPolicyConfigList =
123                 mapper.readValue(new File(RESOURCE_PATH + "policyConfig.json"), PolicyConfig[].class);
124         String configJson = returnedPolicyConfigList[0].getConfig();
125         Config actual = client.getConfigFromStringJson(configJson);
126
127         assertThat(actual, sameBeanAs(expected));
128     }
129
130     @Test
131     public void getConfigWithPolicyNameTest() throws JsonParseException, JsonMappingException, IOException {
132         PolicyClientImpl client = Mockito.spy(PolicyClientImpl.class);
133         ObjectMapper mapper = new ObjectMapper();
134         mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
135         mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
136         PolicyConfig[] returnedPolicyConfigList =
137                 mapper.readValue(new File(RESOURCE_PATH + "policyConfig.json"), PolicyConfig[].class);
138         Config expected = mapper.readValue(new File(RESOURCE_PATH + "configJson.json"), Config.class);
139
140         PolicyRestClient mockedClient = Mockito.mock(PolicyRestClient.class);
141         doReturn(mockedClient).when(client).getPolicyRestClient(PolicyServiceType.GET_CONFIG);
142         doReturn(returnedPolicyConfigList).when(mockedClient).post(isA(ConfigRequestParameters.class), any());
143
144         Config actual = client.getConfigWithPolicyName("policyName");
145
146         assertThat(actual, sameBeanAs(expected));
147
148     }
149 }