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.Mockito;
43 import org.mockito.Spy;
44 import org.mockito.runners.MockitoJUnitRunner;
45 import org.onap.ccsdk.apps.ms.neng.core.exceptions.NengException;
46 import org.onap.ccsdk.apps.ms.neng.core.resource.model.GetConfigRequest;
47 import org.onap.ccsdk.apps.ms.neng.core.resource.model.GetConfigResponse;
48 import org.onap.ccsdk.apps.ms.neng.core.rs.interceptors.PolicyManagerAuthorizationInterceptor;
49 import org.onap.ccsdk.apps.ms.neng.extinf.props.PolicyManagerProps;
50 import org.springframework.boot.web.client.RestTemplateBuilder;
51 import org.springframework.http.HttpStatus;
52 import org.springframework.http.RequestEntity;
53 import org.springframework.http.ResponseEntity;
54 import org.springframework.web.client.HttpClientErrorException;
55 import org.springframework.web.client.HttpStatusCodeException;
56 import org.springframework.web.client.RestTemplate;
58 @RunWith(MockitoJUnitRunner.class)
59 public class PolicyFinderServiceImplTest {
62 PolicyFinderServiceImpl policyFinder;
64 PolicyManagerProps policManProps;
66 RestTemplateBuilder policyMgrRestTempBuilder;
68 PolicyManagerAuthorizationInterceptor authInt;
70 RestTemplate restTemplate;
73 public void testConfig() throws Exception {
74 doReturn(new GetConfigResponse()).when(policyFinder).makeOutboundCall(Matchers.any(), Matchers.any());
75 assertNotNull(policyFinder.getConfig("policy"));
79 public void testFindPolicy() throws Exception {
80 doReturn(new GetConfigResponse()).when(policyFinder).makeOutboundCall(Matchers.any(), Matchers.any());
81 assertNull(policyFinder.findPolicy("policy"));
84 @Test(expected = NengException.class)
85 public void testHandleError_Not_Found() throws Exception {
86 HttpStatusCodeException e = new HttpClientErrorException(HttpStatus.NOT_FOUND,"",
87 null,StandardCharsets.US_ASCII);
88 policyFinder.handleError(e);
91 @SuppressWarnings("unchecked")
93 public void testmakeOutboundCall() throws Exception {
94 Map<String, Object> configMap = buildPolicyResponse();
95 Object resp = Arrays.asList(new Object[] {configMap});
96 ResponseEntity<Object> respEn = new ResponseEntity<>(resp, HttpStatus.OK);
97 Mockito.lenient().when(restTemplate.exchange(Matchers.any(RequestEntity.class), Matchers.any(Class.class))).thenReturn(respEn);
99 policManProps.setUrl("http://policyManager.onap.org");
101 GetConfigRequest request = new GetConfigRequest();
102 request.setPolicyName("policy");
103 GetConfigResponse configResp = policyFinder.makeOutboundCall(request, GetConfigResponse.class);
104 assertNotNull(configResp);
107 @SuppressWarnings("unchecked")
108 @Test(expected = NengException.class)
109 public void testmakeOutboundCall_500() throws Exception {
110 Map<String, Object> configMap = buildPolicyResponse();
111 Object resp = Arrays.asList(new Object[] {configMap});
112 ResponseEntity<Object> respEn = new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR);
113 Mockito.lenient().when(restTemplate.exchange(Matchers.any(RequestEntity.class), Matchers.any(Class.class))).thenReturn(respEn);
115 policManProps.setUrl("http://policyManager.onap.org");
117 GetConfigRequest request = new GetConfigRequest();
118 request.setPolicyName("policy");
119 policyFinder.makeOutboundCall(request, GetConfigResponse.class);
123 public void testGetRestTemplate() throws Exception {
124 PolicyFinderServiceImpl service = new PolicyFinderServiceImpl();
125 RestTemplateBuilder policyRestTemplateBuilder = new RestTemplateBuilder();
126 service.setPolicyMgrRestTempBuilder(policyRestTemplateBuilder);
127 service.setAuthInt(new PolicyManagerAuthorizationInterceptor());
129 assertNotNull(service.getPolicyMgrRestTempBuilder());
130 assertNotNull(service.getAuthInt());
131 assertNotNull(service.getRestTemplate());
135 public void testTransformConfigObject() throws Exception {
136 String config = "{\"riskLevel\":\"4\",\"riskType\":\"test\","
137 + "\"policyName\":\"1806SriovBigJson\",\"service\":\"SDNC-GenerateName\","
138 + "\"guard\":\"False\",\"description\":\"1806SriovBigJson\","
139 + "\"templateVersion\":\"1607\",\"priority\":\"4\",\"version\":\"pannny_nnnn\","
140 + "\"content\":{\"policy-instance-name\":\"1806NameGenerationPolicyForSRIOV\","
141 + "\"naming-models\":[{\"naming-properties\":[{\"property-operation\":\"substr(5)\","
142 + "\"property-name\":\"COMPLEX\"},{\"property-name\":\"SEQUENCE\","
143 + "\"increment-sequence\":{\"max\":\"zzz\",\"scope\":\"ENTIRETY\","
144 + "\"start-value\":\"001\",\"length\":\"3\",\"increment\":\"1\","
145 + "\"sequence-type\":\"alpha-numeric\"}},{\"property-name\":\"NF_NAMING_CODE\"}],"
146 + "\"naming-type\":\"VNF\",\"nfRole\":\"vPE\","
147 + "\"naming-recipe\":\"COMPLEX|SEQUENCE|NF_NAMING_CODE\"},"
148 + "{\"naming-properties\":[{\"property-name\":\"VNF_NAME\"},"
149 + "{\"property-name\":\"SEQUENCE\",\"increment-sequence\":"
150 + "{\"max\":\"999\",\"scope\":\"ENTIRETY\",\"start-value\":\"001\",\"length\":\"3\","
151 + "\"increment\":\"1\",\"sequence-type\":\"numeric\"}},"
152 + "{\"property-operation\":\"substr(-3)\",\"property-name\":\"NFC_NAMING_CODE\"}],"
153 + "\"naming-type\":\"VM\",\"nfRole\":\"vPE\","
154 + "\"naming-recipe\":\"VNF_NAME|SEQUENCE|NFC_NAMING_CODE\"},"
155 + "{\"naming-properties\":[{\"property-name\":\"VNF_NAME\"},"
156 + "{\"property-value\":\"-\",\"property-name\":\"DELIMITER\"},"
157 + "{\"property-name\":\"VF_MODULE_LABEL\"},{\"property-name\":\"VF_MODULE_TYPE\"},"
158 + "{\"property-name\":\"SEQUENCE\",\"increment-sequence\":"
159 + "{\"max\":\"99\",\"scope\":\"PRECEEDING\",\"start-value\":\"01\",\"length\":\"2\","
160 + "\"increment\":\"1\",\"sequence-type\":\"numeric\"}}],"
161 + "\"naming-type\":\"VF-MODULE\",\"nfRole\":\"vPE\","
162 + "\"naming-recipe\":\"VNF_NAME|DELIMITER|VF_MODULE_LABEL|DELIMITER"
163 + "|VF_MODULE_TYPE|DELIMITER|SEQUENCE\"},"
164 + "{\"naming-properties\":[{\"property-name\":\"VF-MODULE_NAME\"},"
165 + "{\"property-value\":\"-\",\"property-name\":\"DELIMITER\"},"
166 + "{\"property-value\":\"volumegroup\",\"property-name\":\"CONSTANT\"}],"
167 + "\"naming-type\":\"VOLUME_GROUP\",\"nfRole\":\"vPE\","
168 + "\"naming-recipe\":\"VF-MODULE_NAME|DELIMITER|CONSTANT\"},"
169 + "{\"naming-properties\":[{\"property-name\":\"VOLUME_GROUP_NAME\"},"
170 + "{\"property-value\":\"-\",\"property-name\":\"DELIMITER\"},"
171 + "{\"property-value\":\"volume\",\"property-name\":\"CONSTANT\"},"
172 + "{\"property-name\":\"SEQUENCE\",\"increment-sequence\":"
173 + "{\"max\":\"99\",\"scope\":\"PRECEEDING\",\"start-value\":\"01\","
174 + "\"length\":\"2\",\"increment\":\"1\",\"sequence-type\":\"numeric\"}}],"
175 + "\"naming-type\":\"VOLUME\",\"nfRole\":\"vPE\","
176 + "\"naming-recipe\":\"VOLUME_GROUP_NAME|DELIMITER|CONSTANT|DELIMITER|SEQUENCE\"},"
177 + "{\"naming-properties\":[{\"property-name\":\"VNF_NAME\"},"
178 + "{\"property-value\":\"-\",\"property-name\":\"DELIMITER\"},"
179 + "{\"property-value\":\"affinity\",\"property-name\":\"CONSTANT\"}],"
180 + "\"naming-type\":\"AFFINITY\",\"nfRole\":\"vPE\","
181 + "\"naming-recipe\":\"VNF_NAME|DELIMITER|CONSTANT\"},"
182 + "{\"naming-properties\":[{\"property-name\":\"VNF_NAME\"},"
183 + "{\"property-value\":\"-\",\"property-name\":\"DELIMITER\"},"
184 + "{\"property-value\":\"INT\",\"property-name\":\"CONSTANT\"},"
185 + "{\"property-name\":\"SEQUENCE\",\"increment-sequence\":"
186 + "{\"max\":\"99\",\"scope\":\"PRECEEDING\",\"start-value\":\"01\","
187 + "\"length\":\"2\",\"increment\":\"1\",\"sequence-type\":\"numeric\"}}],"
188 + "\"naming-type\":\"INTERNAL_NETWORK\",\"nfRole\":\"vPE\","
189 + "\"naming-recipe\":\"VNF_NAME|DELIMITER|CONSTANT|SEQUENCE\"}]}}";
190 Map<Object, Object> configMap = new HashMap<>();
191 configMap.put("config", config);
192 ObjectMapper objectmapper = new ObjectMapper();
193 List<Map<Object, Object>> respList = new ArrayList<>();
194 respList.add(configMap);
195 policyFinder.transformConfigObject(objectmapper, respList);
196 assertNotNull(respList.get(0).get("config"));
199 Map<String, Object> buildPolicyResponse() {
200 Map<String, Object> policyDataMap = new HashMap<>();
201 policyDataMap.put("policy-instance-name", "SDNC_Policy.Config_MS_VNFCNamingPolicy");
202 Map<String, Object> namingModelMap = new HashMap<>();
203 namingModelMap.put("nf-role", "vPE");
204 namingModelMap.put("naming-type", "VNF");
205 namingModelMap.put("naming-recipe", "COMPLEX|NF-NAMING-CODE|Field2|Field3|Field4");
206 Map<String, Object> namingPropertyMap = new HashMap<>();
207 Map<String, Object> propertyMap1 = new HashMap<>();
208 propertyMap1.put("property-name", "COMPLEX");
209 Map<String, Object> propertyMap2 = new HashMap<>();
210 propertyMap2.put("property-name", "NF-NAMING-CODE");
211 namingPropertyMap.put("", Arrays.asList(new Object[] {propertyMap1, propertyMap2}));
212 namingModelMap.put("naming-properties", namingPropertyMap);
213 policyDataMap.put("naming-models", Arrays.asList(new Object[] {namingModelMap}));
214 Map<String, Object> configMap = new HashMap<>();
215 Map<String, Object> contentMap = new HashMap<>();
216 contentMap.put("content", policyDataMap);
217 configMap.put("config", contentMap);
221 @SuppressWarnings("unchecked")
222 @Test(expected = NengException.class)
223 public void testmakeOutboundCall_500_statusExp() throws Exception {
224 HttpClientErrorException exp = new HttpClientErrorException(HttpStatus.METHOD_NOT_ALLOWED, "{error}");
225 Mockito.lenient().when(restTemplate.exchange(Matchers.any(RequestEntity.class), Matchers.any(Class.class))).thenThrow(exp);
226 policManProps.setUrl("http://policyManager.onap.org");
227 GetConfigRequest request = new GetConfigRequest();
228 request.setPolicyName("policy");
229 policyFinder.makeOutboundCall(request, GetConfigResponse.class);
232 @SuppressWarnings("unchecked")
233 @Test(expected = NengException.class)
234 public void testmakeOutboundCall_500_statusExp_notFound() throws Exception {
235 HttpClientErrorException exp = new HttpClientErrorException(HttpStatus.NOT_FOUND, "{error}");
236 Mockito.lenient().when(restTemplate.exchange(Matchers.any(RequestEntity.class), Matchers.any(Class.class))).thenThrow(exp);
237 policManProps.setUrl("http://policyManager.onap.org");
238 GetConfigRequest request = new GetConfigRequest();
239 request.setPolicyName("policy");
240 policyFinder.makeOutboundCall(request, GetConfigResponse.class);
244 public void testmakeOutboundCall_err_policy() throws Exception {
245 Map<String, Object> configMap = buildPolicyResponse();
246 Object resp = configMap;
247 GetConfigResponse configResp = new GetConfigResponse();
248 configResp.setResponse(resp);
249 doReturn(configResp).when(policyFinder).getConfig("policy");
250 assertNull(policyFinder.findPolicy("policy"));
254 public void testmakeOutboundCall_err_policy_empty() throws Exception {
255 Object resp = Arrays.asList(new Object[]{});
256 GetConfigResponse configResp = new GetConfigResponse();
257 configResp.setResponse(resp);
258 doReturn(configResp).when(policyFinder).getConfig("policy");
259 assertNull(policyFinder.findPolicy("policy"));