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 com.fasterxml.jackson.databind.ObjectMapper;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.InjectMocks;
29 import org.mockito.Matchers;
30 import org.mockito.Mock;
31 import org.mockito.Spy;
32 import org.mockito.runners.MockitoJUnitRunner;
33 import org.onap.ccsdk.apps.ms.neng.core.exceptions.NengException;
34 import org.onap.ccsdk.apps.ms.neng.core.resource.model.GetConfigRequest;
35 import org.onap.ccsdk.apps.ms.neng.core.resource.model.GetConfigResponse;
36 import org.onap.ccsdk.apps.ms.neng.core.rs.interceptors.PolicyManagerAuthorizationInterceptor;
37 import org.onap.ccsdk.apps.ms.neng.extinf.props.PolicyManagerProps;
38 import org.springframework.boot.web.client.RestTemplateBuilder;
39 import org.springframework.http.HttpStatus;
40 import org.springframework.http.RequestEntity;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.web.client.HttpClientErrorException;
43 import org.springframework.web.client.HttpStatusCodeException;
44 import org.springframework.web.client.RestTemplate;
46 import java.nio.charset.StandardCharsets;
49 import static org.junit.Assert.assertNotNull;
50 import static org.junit.Assert.assertNull;
51 import static org.mockito.Mockito.doReturn;
52 import static org.mockito.Mockito.when;
54 @RunWith(MockitoJUnitRunner.class)
55 public class PolicyFinderServiceImplTest {
58 PolicyFinderServiceImpl policyFinder;
60 PolicyManagerProps policManProps;
62 RestTemplateBuilder policyMgrRestTempBuilder;
64 PolicyManagerAuthorizationInterceptor authInt;
66 RestTemplate restTemplate;
69 public void testConfig() throws Exception {
70 doReturn(new GetConfigResponse()).when(policyFinder).makeOutboundCall(Matchers.any(), Matchers.any());
71 assertNotNull(policyFinder.getConfig("policy"));
75 public void testFindPolicy() throws Exception {
76 doReturn(new GetConfigResponse()).when(policyFinder).makeOutboundCall(Matchers.any(), Matchers.any());
77 assertNull(policyFinder.findPolicy("policy"));
80 @SuppressWarnings("unchecked")
81 @Test(expected = NengException.class)
82 public void testHandleError_NOT_FOUND() throws Exception{
83 HttpStatusCodeException e = new HttpClientErrorException(HttpStatus.NOT_FOUND,"",null,StandardCharsets.US_ASCII);
84 policyFinder.handleError(e);
87 @SuppressWarnings("unchecked")
89 public void testmakeOutboundCall() throws Exception {
90 Map<String, Object> configMap = buildPolicyResponse();
91 Object resp = Arrays.asList(new Object[] {configMap});
92 ResponseEntity<Object> respEn = new ResponseEntity<>(resp, HttpStatus.OK);
93 when(restTemplate.exchange(Matchers.any(RequestEntity.class), Matchers.any(Class.class))).thenReturn(respEn);
95 policManProps.setUrl("http://policyManager.onap.org");
97 GetConfigRequest request = new GetConfigRequest();
98 request.setPolicyName("policy");
99 GetConfigResponse configResp = policyFinder.makeOutboundCall(request, GetConfigResponse.class);
100 assertNotNull(configResp);
103 @SuppressWarnings("unchecked")
104 @Test(expected = NengException.class)
105 public void testmakeOutboundCall_500() throws Exception {
106 Map<String, Object> configMap = buildPolicyResponse();
107 Object resp = Arrays.asList(new Object[] {configMap});
108 ResponseEntity<Object> respEn = new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR);
109 when(restTemplate.exchange(Matchers.any(RequestEntity.class), Matchers.any(Class.class))).thenReturn(respEn);
111 policManProps.setUrl("http://policyManager.onap.org");
113 GetConfigRequest request = new GetConfigRequest();
114 request.setPolicyName("policy");
115 policyFinder.makeOutboundCall(request, GetConfigResponse.class);
119 public void testGetRestTemplate() throws Exception {
120 PolicyFinderServiceImpl service = new PolicyFinderServiceImpl();
121 RestTemplateBuilder policyRestTemplateBuilder = new RestTemplateBuilder();
122 service.setPolicyMgrRestTempBuilder(policyRestTemplateBuilder);
123 service.setAuthInt(new PolicyManagerAuthorizationInterceptor());
125 assertNotNull(service.getPolicyMgrRestTempBuilder());
126 assertNotNull(service.getAuthInt());
127 assertNotNull(service.getRestTemplate());
131 public void testTransformConfigObject() throws Exception {
132 String config = "{\"riskLevel\":\"4\",\"riskType\":\"test\","
133 + "\"policyName\":\"1806SriovBigJson\",\"service\":\"SDNC-GenerateName\","
134 + "\"guard\":\"False\",\"description\":\"1806SriovBigJson\","
135 + "\"templateVersion\":\"1607\",\"priority\":\"4\",\"version\":\"pannny_nnnn\","
136 + "\"content\":{\"policy-instance-name\":\"1806NameGenerationPolicyForSRIOV\","
137 + "\"naming-models\":[{\"naming-properties\":[{\"property-operation\":\"substr(5)\","
138 + "\"property-name\":\"COMPLEX\"},{\"property-name\":\"SEQUENCE\","
139 + "\"increment-sequence\":{\"max\":\"zzz\",\"scope\":\"ENTIRETY\","
140 + "\"start-value\":\"001\",\"length\":\"3\",\"increment\":\"1\","
141 + "\"sequence-type\":\"alpha-numeric\"}},{\"property-name\":\"NF_NAMING_CODE\"}],"
142 + "\"naming-type\":\"VNF\",\"nfRole\":\"vPE\","
143 + "\"naming-recipe\":\"COMPLEX|SEQUENCE|NF_NAMING_CODE\"},"
144 + "{\"naming-properties\":[{\"property-name\":\"VNF_NAME\"},"
145 + "{\"property-name\":\"SEQUENCE\",\"increment-sequence\":"
146 + "{\"max\":\"999\",\"scope\":\"ENTIRETY\",\"start-value\":\"001\",\"length\":\"3\","
147 + "\"increment\":\"1\",\"sequence-type\":\"numeric\"}},"
148 + "{\"property-operation\":\"substr(-3)\",\"property-name\":\"NFC_NAMING_CODE\"}],"
149 + "\"naming-type\":\"VM\",\"nfRole\":\"vPE\","
150 + "\"naming-recipe\":\"VNF_NAME|SEQUENCE|NFC_NAMING_CODE\"},"
151 + "{\"naming-properties\":[{\"property-name\":\"VNF_NAME\"},"
152 + "{\"property-value\":\"-\",\"property-name\":\"DELIMITER\"},"
153 + "{\"property-name\":\"VF_MODULE_LABEL\"},{\"property-name\":\"VF_MODULE_TYPE\"},"
154 + "{\"property-name\":\"SEQUENCE\",\"increment-sequence\":"
155 + "{\"max\":\"99\",\"scope\":\"PRECEEDING\",\"start-value\":\"01\",\"length\":\"2\","
156 + "\"increment\":\"1\",\"sequence-type\":\"numeric\"}}],"
157 + "\"naming-type\":\"VF-MODULE\",\"nfRole\":\"vPE\","
158 + "\"naming-recipe\":\"VNF_NAME|DELIMITER|VF_MODULE_LABEL|DELIMITER"
159 + "|VF_MODULE_TYPE|DELIMITER|SEQUENCE\"},"
160 + "{\"naming-properties\":[{\"property-name\":\"VF-MODULE_NAME\"},"
161 + "{\"property-value\":\"-\",\"property-name\":\"DELIMITER\"},"
162 + "{\"property-value\":\"volumegroup\",\"property-name\":\"CONSTANT\"}],"
163 + "\"naming-type\":\"VOLUME_GROUP\",\"nfRole\":\"vPE\","
164 + "\"naming-recipe\":\"VF-MODULE_NAME|DELIMITER|CONSTANT\"},"
165 + "{\"naming-properties\":[{\"property-name\":\"VOLUME_GROUP_NAME\"},"
166 + "{\"property-value\":\"-\",\"property-name\":\"DELIMITER\"},"
167 + "{\"property-value\":\"volume\",\"property-name\":\"CONSTANT\"},"
168 + "{\"property-name\":\"SEQUENCE\",\"increment-sequence\":"
169 + "{\"max\":\"99\",\"scope\":\"PRECEEDING\",\"start-value\":\"01\","
170 + "\"length\":\"2\",\"increment\":\"1\",\"sequence-type\":\"numeric\"}}],"
171 + "\"naming-type\":\"VOLUME\",\"nfRole\":\"vPE\","
172 + "\"naming-recipe\":\"VOLUME_GROUP_NAME|DELIMITER|CONSTANT|DELIMITER|SEQUENCE\"},"
173 + "{\"naming-properties\":[{\"property-name\":\"VNF_NAME\"},"
174 + "{\"property-value\":\"-\",\"property-name\":\"DELIMITER\"},"
175 + "{\"property-value\":\"affinity\",\"property-name\":\"CONSTANT\"}],"
176 + "\"naming-type\":\"AFFINITY\",\"nfRole\":\"vPE\","
177 + "\"naming-recipe\":\"VNF_NAME|DELIMITER|CONSTANT\"},"
178 + "{\"naming-properties\":[{\"property-name\":\"VNF_NAME\"},"
179 + "{\"property-value\":\"-\",\"property-name\":\"DELIMITER\"},"
180 + "{\"property-value\":\"INT\",\"property-name\":\"CONSTANT\"},"
181 + "{\"property-name\":\"SEQUENCE\",\"increment-sequence\":"
182 + "{\"max\":\"99\",\"scope\":\"PRECEEDING\",\"start-value\":\"01\","
183 + "\"length\":\"2\",\"increment\":\"1\",\"sequence-type\":\"numeric\"}}],"
184 + "\"naming-type\":\"INTERNAL_NETWORK\",\"nfRole\":\"vPE\","
185 + "\"naming-recipe\":\"VNF_NAME|DELIMITER|CONSTANT|SEQUENCE\"}]}}";
186 Map<Object, Object> configMap = new HashMap<>();
187 configMap.put("config", config);
188 ObjectMapper objectmapper = new ObjectMapper();
189 List<Map<Object, Object>> respList = new ArrayList<>();
190 respList.add(configMap);
191 policyFinder.transformConfigObject(objectmapper, respList);
192 assertNotNull(respList.get(0).get("config"));
195 Map<String, Object> buildPolicyResponse() {
196 Map<String, Object> policyDataMap = new HashMap<>();
197 policyDataMap.put("policy-instance-name", "SDNC_Policy.Config_MS_VNFCNamingPolicy");
198 Map<String, Object> namingModelMap = new HashMap<>();
199 namingModelMap.put("nf-role", "vPE");
200 namingModelMap.put("naming-type", "VNF");
201 namingModelMap.put("naming-recipe", "COMPLEX|NF-NAMING-CODE|Field2|Field3|Field4");
202 Map<String, Object> namingPropertyMap = new HashMap<>();
203 Map<String, Object> propertyMap1 = new HashMap<>();
204 propertyMap1.put("property-name", "COMPLEX");
205 Map<String, Object> propertyMap2 = new HashMap<>();
206 propertyMap2.put("property-name", "NF-NAMING-CODE");
207 namingPropertyMap.put("", Arrays.asList(new Object[] {propertyMap1, propertyMap2}));
208 namingModelMap.put("naming-properties", namingPropertyMap);
209 policyDataMap.put("naming-models", Arrays.asList(new Object[] {namingModelMap}));
210 Map<String, Object> configMap = new HashMap<>();
211 Map<String, Object> contentMap = new HashMap<>();
212 contentMap.put("content", policyDataMap);
213 configMap.put("config", contentMap);