2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (C) 2018 IBM.
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.
20 * ============LICENSE_END=========================================================
23 package org.onap.ccsdk.apps.ms.neng.service.extinf.impl;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.mockito.Mockito.doReturn;
28 import static org.mockito.Mockito.when;
30 import com.fasterxml.jackson.databind.ObjectMapper;
31 import java.nio.charset.StandardCharsets;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.HashMap;
35 import java.util.List;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.InjectMocks;
40 import org.mockito.Matchers;
41 import org.mockito.Mock;
42 import org.mockito.Spy;
43 import org.mockito.runners.MockitoJUnitRunner;
44 import org.onap.ccsdk.apps.ms.neng.core.exceptions.NengException;
45 import org.onap.ccsdk.apps.ms.neng.core.resource.model.GetConfigRequest;
46 import org.onap.ccsdk.apps.ms.neng.core.resource.model.GetConfigResponse;
47 import org.onap.ccsdk.apps.ms.neng.core.rs.interceptors.PolicyManagerAuthorizationInterceptor;
48 import org.onap.ccsdk.apps.ms.neng.extinf.props.PolicyManagerProps;
49 import org.springframework.boot.web.client.RestTemplateBuilder;
50 import org.springframework.http.HttpStatus;
51 import org.springframework.http.RequestEntity;
52 import org.springframework.http.ResponseEntity;
53 import org.springframework.web.client.HttpClientErrorException;
54 import org.springframework.web.client.HttpStatusCodeException;
55 import org.springframework.web.client.RestTemplate;
57 @RunWith(MockitoJUnitRunner.class)
58 public class PolicyFinderServiceImplTest {
61 PolicyFinderServiceImpl policyFinder;
63 PolicyManagerProps policManProps;
65 RestTemplateBuilder policyMgrRestTempBuilder;
67 PolicyManagerAuthorizationInterceptor authInt;
69 RestTemplate restTemplate;
72 public void testConfig() throws Exception {
73 doReturn(new GetConfigResponse()).when(policyFinder).makeOutboundCall(Matchers.any(), Matchers.any());
74 assertNotNull(policyFinder.getConfig("policy"));
78 public void testFindPolicy() throws Exception {
79 doReturn(new GetConfigResponse()).when(policyFinder).makeOutboundCall(Matchers.any(), Matchers.any());
80 assertNull(policyFinder.findPolicy("policy"));
83 @Test(expected = NengException.class)
84 public void testHandleError_Not_Found() throws Exception {
85 HttpStatusCodeException e = new HttpClientErrorException(HttpStatus.NOT_FOUND,"",
86 null,StandardCharsets.US_ASCII);
87 policyFinder.handleError(e);
90 @SuppressWarnings("unchecked")
92 public void testmakeOutboundCall() throws Exception {
93 Map<String, Object> configMap = buildPolicyResponse();
94 Object resp = Arrays.asList(new Object[] {configMap});
95 ResponseEntity<Object> respEn = new ResponseEntity<>(resp, HttpStatus.OK);
96 when(restTemplate.exchange(Matchers.any(RequestEntity.class), Matchers.any(Class.class))).thenReturn(respEn);
98 policManProps.setUrl("http://policyManager.onap.org");
100 GetConfigRequest request = new GetConfigRequest();
101 request.setPolicyName("policy");
102 GetConfigResponse configResp = policyFinder.makeOutboundCall(request, GetConfigResponse.class);
103 assertNotNull(configResp);
106 @SuppressWarnings("unchecked")
107 @Test(expected = NengException.class)
108 public void testmakeOutboundCall_500() throws Exception {
109 Map<String, Object> configMap = buildPolicyResponse();
110 Object resp = Arrays.asList(new Object[] {configMap});
111 ResponseEntity<Object> respEn = new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR);
112 when(restTemplate.exchange(Matchers.any(RequestEntity.class), Matchers.any(Class.class))).thenReturn(respEn);
114 policManProps.setUrl("http://policyManager.onap.org");
116 GetConfigRequest request = new GetConfigRequest();
117 request.setPolicyName("policy");
118 policyFinder.makeOutboundCall(request, GetConfigResponse.class);
122 public void testGetRestTemplate() throws Exception {
123 PolicyFinderServiceImpl service = new PolicyFinderServiceImpl();
124 RestTemplateBuilder policyRestTemplateBuilder = new RestTemplateBuilder();
125 service.setPolicyMgrRestTempBuilder(policyRestTemplateBuilder);
126 service.setAuthInt(new PolicyManagerAuthorizationInterceptor());
128 assertNotNull(service.getPolicyMgrRestTempBuilder());
129 assertNotNull(service.getAuthInt());
130 assertNotNull(service.getRestTemplate());
134 public void testTransformConfigObject() throws Exception {
135 String config = "{\"riskLevel\":\"4\",\"riskType\":\"test\","
136 + "\"policyName\":\"1806SriovBigJson\",\"service\":\"SDNC-GenerateName\","
137 + "\"guard\":\"False\",\"description\":\"1806SriovBigJson\","
138 + "\"templateVersion\":\"1607\",\"priority\":\"4\",\"version\":\"pannny_nnnn\","
139 + "\"content\":{\"policy-instance-name\":\"1806NameGenerationPolicyForSRIOV\","
140 + "\"naming-models\":[{\"naming-properties\":[{\"property-operation\":\"substr(5)\","
141 + "\"property-name\":\"COMPLEX\"},{\"property-name\":\"SEQUENCE\","
142 + "\"increment-sequence\":{\"max\":\"zzz\",\"scope\":\"ENTIRETY\","
143 + "\"start-value\":\"001\",\"length\":\"3\",\"increment\":\"1\","
144 + "\"sequence-type\":\"alpha-numeric\"}},{\"property-name\":\"NF_NAMING_CODE\"}],"
145 + "\"naming-type\":\"VNF\",\"nfRole\":\"vPE\","
146 + "\"naming-recipe\":\"COMPLEX|SEQUENCE|NF_NAMING_CODE\"},"
147 + "{\"naming-properties\":[{\"property-name\":\"VNF_NAME\"},"
148 + "{\"property-name\":\"SEQUENCE\",\"increment-sequence\":"
149 + "{\"max\":\"999\",\"scope\":\"ENTIRETY\",\"start-value\":\"001\",\"length\":\"3\","
150 + "\"increment\":\"1\",\"sequence-type\":\"numeric\"}},"
151 + "{\"property-operation\":\"substr(-3)\",\"property-name\":\"NFC_NAMING_CODE\"}],"
152 + "\"naming-type\":\"VM\",\"nfRole\":\"vPE\","
153 + "\"naming-recipe\":\"VNF_NAME|SEQUENCE|NFC_NAMING_CODE\"},"
154 + "{\"naming-properties\":[{\"property-name\":\"VNF_NAME\"},"
155 + "{\"property-value\":\"-\",\"property-name\":\"DELIMITER\"},"
156 + "{\"property-name\":\"VF_MODULE_LABEL\"},{\"property-name\":\"VF_MODULE_TYPE\"},"
157 + "{\"property-name\":\"SEQUENCE\",\"increment-sequence\":"
158 + "{\"max\":\"99\",\"scope\":\"PRECEEDING\",\"start-value\":\"01\",\"length\":\"2\","
159 + "\"increment\":\"1\",\"sequence-type\":\"numeric\"}}],"
160 + "\"naming-type\":\"VF-MODULE\",\"nfRole\":\"vPE\","
161 + "\"naming-recipe\":\"VNF_NAME|DELIMITER|VF_MODULE_LABEL|DELIMITER"
162 + "|VF_MODULE_TYPE|DELIMITER|SEQUENCE\"},"
163 + "{\"naming-properties\":[{\"property-name\":\"VF-MODULE_NAME\"},"
164 + "{\"property-value\":\"-\",\"property-name\":\"DELIMITER\"},"
165 + "{\"property-value\":\"volumegroup\",\"property-name\":\"CONSTANT\"}],"
166 + "\"naming-type\":\"VOLUME_GROUP\",\"nfRole\":\"vPE\","
167 + "\"naming-recipe\":\"VF-MODULE_NAME|DELIMITER|CONSTANT\"},"
168 + "{\"naming-properties\":[{\"property-name\":\"VOLUME_GROUP_NAME\"},"
169 + "{\"property-value\":\"-\",\"property-name\":\"DELIMITER\"},"
170 + "{\"property-value\":\"volume\",\"property-name\":\"CONSTANT\"},"
171 + "{\"property-name\":\"SEQUENCE\",\"increment-sequence\":"
172 + "{\"max\":\"99\",\"scope\":\"PRECEEDING\",\"start-value\":\"01\","
173 + "\"length\":\"2\",\"increment\":\"1\",\"sequence-type\":\"numeric\"}}],"
174 + "\"naming-type\":\"VOLUME\",\"nfRole\":\"vPE\","
175 + "\"naming-recipe\":\"VOLUME_GROUP_NAME|DELIMITER|CONSTANT|DELIMITER|SEQUENCE\"},"
176 + "{\"naming-properties\":[{\"property-name\":\"VNF_NAME\"},"
177 + "{\"property-value\":\"-\",\"property-name\":\"DELIMITER\"},"
178 + "{\"property-value\":\"affinity\",\"property-name\":\"CONSTANT\"}],"
179 + "\"naming-type\":\"AFFINITY\",\"nfRole\":\"vPE\","
180 + "\"naming-recipe\":\"VNF_NAME|DELIMITER|CONSTANT\"},"
181 + "{\"naming-properties\":[{\"property-name\":\"VNF_NAME\"},"
182 + "{\"property-value\":\"-\",\"property-name\":\"DELIMITER\"},"
183 + "{\"property-value\":\"INT\",\"property-name\":\"CONSTANT\"},"
184 + "{\"property-name\":\"SEQUENCE\",\"increment-sequence\":"
185 + "{\"max\":\"99\",\"scope\":\"PRECEEDING\",\"start-value\":\"01\","
186 + "\"length\":\"2\",\"increment\":\"1\",\"sequence-type\":\"numeric\"}}],"
187 + "\"naming-type\":\"INTERNAL_NETWORK\",\"nfRole\":\"vPE\","
188 + "\"naming-recipe\":\"VNF_NAME|DELIMITER|CONSTANT|SEQUENCE\"}]}}";
189 Map<Object, Object> configMap = new HashMap<>();
190 configMap.put("config", config);
191 ObjectMapper objectmapper = new ObjectMapper();
192 List<Map<Object, Object>> respList = new ArrayList<>();
193 respList.add(configMap);
194 policyFinder.transformConfigObject(objectmapper, respList);
195 assertNotNull(respList.get(0).get("config"));
198 Map<String, Object> buildPolicyResponse() {
199 Map<String, Object> policyDataMap = new HashMap<>();
200 policyDataMap.put("policy-instance-name", "SDNC_Policy.Config_MS_VNFCNamingPolicy");
201 Map<String, Object> namingModelMap = new HashMap<>();
202 namingModelMap.put("nf-role", "vPE");
203 namingModelMap.put("naming-type", "VNF");
204 namingModelMap.put("naming-recipe", "COMPLEX|NF-NAMING-CODE|Field2|Field3|Field4");
205 Map<String, Object> namingPropertyMap = new HashMap<>();
206 Map<String, Object> propertyMap1 = new HashMap<>();
207 propertyMap1.put("property-name", "COMPLEX");
208 Map<String, Object> propertyMap2 = new HashMap<>();
209 propertyMap2.put("property-name", "NF-NAMING-CODE");
210 namingPropertyMap.put("", Arrays.asList(new Object[] {propertyMap1, propertyMap2}));
211 namingModelMap.put("naming-properties", namingPropertyMap);
212 policyDataMap.put("naming-models", Arrays.asList(new Object[] {namingModelMap}));
213 Map<String, Object> configMap = new HashMap<>();
214 Map<String, Object> contentMap = new HashMap<>();
215 contentMap.put("content", policyDataMap);
216 configMap.put("config", contentMap);
220 @SuppressWarnings("unchecked")
221 @Test(expected = NengException.class)
222 public void testmakeOutboundCall_500_statusExp() throws Exception {
223 HttpClientErrorException exp = new HttpClientErrorException(HttpStatus.METHOD_NOT_ALLOWED, "{error}");
224 when(restTemplate.exchange(Matchers.any(RequestEntity.class), Matchers.any(Class.class))).thenThrow(exp);
225 policManProps.setUrl("http://policyManager.onap.org");
226 GetConfigRequest request = new GetConfigRequest();
227 request.setPolicyName("policy");
228 policyFinder.makeOutboundCall(request, GetConfigResponse.class);
231 @SuppressWarnings("unchecked")
232 @Test(expected = NengException.class)
233 public void testmakeOutboundCall_500_statusExp_notFound() throws Exception {
234 HttpClientErrorException exp = new HttpClientErrorException(HttpStatus.NOT_FOUND, "{error}");
235 when(restTemplate.exchange(Matchers.any(RequestEntity.class), Matchers.any(Class.class))).thenThrow(exp);
236 policManProps.setUrl("http://policyManager.onap.org");
237 GetConfigRequest request = new GetConfigRequest();
238 request.setPolicyName("policy");
239 policyFinder.makeOutboundCall(request, GetConfigResponse.class);
243 public void testmakeOutboundCall_err_policy() throws Exception {
244 Map<String, Object> configMap = buildPolicyResponse();
245 Object resp = configMap;
246 GetConfigResponse configResp = new GetConfigResponse();
247 configResp.setResponse(resp);
248 doReturn(configResp).when(policyFinder).getConfig("policy");
249 assertNull(policyFinder.findPolicy("policy"));
253 public void testmakeOutboundCall_err_policy_empty() throws Exception {
254 Object resp = Arrays.asList(new Object[]{});
255 GetConfigResponse configResp = new GetConfigResponse();
256 configResp.setResponse(resp);
257 doReturn(configResp).when(policyFinder).getConfig("policy");
258 assertNull(policyFinder.findPolicy("policy"));