2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.ccsdk.apps.ms.neng.service.extinf.impl;
23 import static org.junit.Assert.assertNotNull;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.when;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.HashMap;
31 import java.util.List;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.InjectMocks;
36 import org.mockito.Matchers;
37 import org.mockito.Mock;
38 import org.mockito.Spy;
39 import org.mockito.runners.MockitoJUnitRunner;
40 import org.onap.ccsdk.apps.ms.neng.core.exceptions.NengException;
41 import org.onap.ccsdk.apps.ms.neng.core.resource.model.GetConfigRequest;
42 import org.onap.ccsdk.apps.ms.neng.core.resource.model.GetConfigResponse;
43 import org.onap.ccsdk.apps.ms.neng.core.rs.interceptors.PolicyManagerAuthorizationInterceptor;
44 import org.onap.ccsdk.apps.ms.neng.extinf.props.PolicyManagerProps;
45 import org.springframework.boot.web.client.RestTemplateBuilder;
46 import org.springframework.http.HttpStatus;
47 import org.springframework.http.RequestEntity;
48 import org.springframework.http.ResponseEntity;
49 import org.springframework.web.client.RestTemplate;
51 @RunWith(MockitoJUnitRunner.class)
52 public class PolicyFinderServiceImplTest {
55 PolicyFinderServiceImpl policyFinder;
57 PolicyManagerProps policManProps;
59 RestTemplateBuilder policyMgrRestTempBuilder;
61 PolicyManagerAuthorizationInterceptor authInt;
63 RestTemplate restTemplate;
66 public void testConfig() throws Exception {
67 doReturn(new GetConfigResponse()).when(policyFinder).makeOutboundCall(Matchers.any(), Matchers.any());
68 assertNotNull(policyFinder.getConfig("policy"));
71 @SuppressWarnings("unchecked")
73 public void testmakeOutboundCall() throws Exception {
74 Map<String, Object> configMap = buildPolicyResponse();
75 Object resp = Arrays.asList(new Object[] {configMap});
76 ResponseEntity<Object> respEn = new ResponseEntity<>(resp, HttpStatus.OK);
77 when(restTemplate.exchange(Matchers.any(RequestEntity.class), Matchers.any(Class.class))).thenReturn(respEn);
79 policManProps.setUrl("http://policyManager.onap.org");
81 GetConfigRequest request = new GetConfigRequest();
82 request.setPolicyName("policy");
83 GetConfigResponse configResp = policyFinder.makeOutboundCall(request, GetConfigResponse.class);
84 assertNotNull(configResp);
87 @SuppressWarnings("unchecked")
88 @Test(expected = NengException.class)
89 public void testmakeOutboundCall_500() throws Exception {
90 Map<String, Object> configMap = buildPolicyResponse();
91 Object resp = Arrays.asList(new Object[] {configMap});
92 ResponseEntity<Object> respEn = new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR);
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 policyFinder.makeOutboundCall(request, GetConfigResponse.class);
103 public void testGetRestTemplate() throws Exception {
104 PolicyFinderServiceImpl service = new PolicyFinderServiceImpl();
105 RestTemplateBuilder policyRestTemplateBuilder = new RestTemplateBuilder();
106 service.setPolicyMgrRestTempBuilder(policyRestTemplateBuilder);
107 service.setAuthInt(new PolicyManagerAuthorizationInterceptor());
109 assertNotNull(service.getPolicyMgrRestTempBuilder());
110 assertNotNull(service.getAuthInt());
111 assertNotNull(service.getRestTemplate());
115 public void testTransformConfigObject() throws Exception {
116 String config = "{\"riskLevel\":\"4\",\"riskType\":\"test\","
117 + "\"policyName\":\"1806SriovBigJson\",\"service\":\"SDNC-GenerateName\","
118 + "\"guard\":\"False\",\"description\":\"1806SriovBigJson\","
119 + "\"templateVersion\":\"1607\",\"priority\":\"4\",\"version\":\"pannny_nnnn\","
120 + "\"content\":{\"policy-instance-name\":\"1806NameGenerationPolicyForSRIOV\","
121 + "\"naming-models\":[{\"naming-properties\":[{\"property-operation\":\"substr(5)\","
122 + "\"property-name\":\"COMPLEX\"},{\"property-name\":\"SEQUENCE\","
123 + "\"increment-sequence\":{\"max\":\"zzz\",\"scope\":\"ENTIRETY\","
124 + "\"start-value\":\"001\",\"length\":\"3\",\"increment\":\"1\","
125 + "\"sequence-type\":\"alpha-numeric\"}},{\"property-name\":\"NF_NAMING_CODE\"}],"
126 + "\"naming-type\":\"VNF\",\"nfRole\":\"vPE\","
127 + "\"naming-recipe\":\"COMPLEX|SEQUENCE|NF_NAMING_CODE\"},"
128 + "{\"naming-properties\":[{\"property-name\":\"VNF_NAME\"},"
129 + "{\"property-name\":\"SEQUENCE\",\"increment-sequence\":"
130 + "{\"max\":\"999\",\"scope\":\"ENTIRETY\",\"start-value\":\"001\",\"length\":\"3\","
131 + "\"increment\":\"1\",\"sequence-type\":\"numeric\"}},"
132 + "{\"property-operation\":\"substr(-3)\",\"property-name\":\"NFC_NAMING_CODE\"}],"
133 + "\"naming-type\":\"VM\",\"nfRole\":\"vPE\","
134 + "\"naming-recipe\":\"VNF_NAME|SEQUENCE|NFC_NAMING_CODE\"},"
135 + "{\"naming-properties\":[{\"property-name\":\"VNF_NAME\"},"
136 + "{\"property-value\":\"-\",\"property-name\":\"DELIMITER\"},"
137 + "{\"property-name\":\"VF_MODULE_LABEL\"},{\"property-name\":\"VF_MODULE_TYPE\"},"
138 + "{\"property-name\":\"SEQUENCE\",\"increment-sequence\":"
139 + "{\"max\":\"99\",\"scope\":\"PRECEEDING\",\"start-value\":\"01\",\"length\":\"2\","
140 + "\"increment\":\"1\",\"sequence-type\":\"numeric\"}}],"
141 + "\"naming-type\":\"VF-MODULE\",\"nfRole\":\"vPE\","
142 + "\"naming-recipe\":\"VNF_NAME|DELIMITER|VF_MODULE_LABEL|DELIMITER"
143 + "|VF_MODULE_TYPE|DELIMITER|SEQUENCE\"},"
144 + "{\"naming-properties\":[{\"property-name\":\"VF-MODULE_NAME\"},"
145 + "{\"property-value\":\"-\",\"property-name\":\"DELIMITER\"},"
146 + "{\"property-value\":\"volumegroup\",\"property-name\":\"CONSTANT\"}],"
147 + "\"naming-type\":\"VOLUME_GROUP\",\"nfRole\":\"vPE\","
148 + "\"naming-recipe\":\"VF-MODULE_NAME|DELIMITER|CONSTANT\"},"
149 + "{\"naming-properties\":[{\"property-name\":\"VOLUME_GROUP_NAME\"},"
150 + "{\"property-value\":\"-\",\"property-name\":\"DELIMITER\"},"
151 + "{\"property-value\":\"volume\",\"property-name\":\"CONSTANT\"},"
152 + "{\"property-name\":\"SEQUENCE\",\"increment-sequence\":"
153 + "{\"max\":\"99\",\"scope\":\"PRECEEDING\",\"start-value\":\"01\","
154 + "\"length\":\"2\",\"increment\":\"1\",\"sequence-type\":\"numeric\"}}],"
155 + "\"naming-type\":\"VOLUME\",\"nfRole\":\"vPE\","
156 + "\"naming-recipe\":\"VOLUME_GROUP_NAME|DELIMITER|CONSTANT|DELIMITER|SEQUENCE\"},"
157 + "{\"naming-properties\":[{\"property-name\":\"VNF_NAME\"},"
158 + "{\"property-value\":\"-\",\"property-name\":\"DELIMITER\"},"
159 + "{\"property-value\":\"affinity\",\"property-name\":\"CONSTANT\"}],"
160 + "\"naming-type\":\"AFFINITY\",\"nfRole\":\"vPE\","
161 + "\"naming-recipe\":\"VNF_NAME|DELIMITER|CONSTANT\"},"
162 + "{\"naming-properties\":[{\"property-name\":\"VNF_NAME\"},"
163 + "{\"property-value\":\"-\",\"property-name\":\"DELIMITER\"},"
164 + "{\"property-value\":\"INT\",\"property-name\":\"CONSTANT\"},"
165 + "{\"property-name\":\"SEQUENCE\",\"increment-sequence\":"
166 + "{\"max\":\"99\",\"scope\":\"PRECEEDING\",\"start-value\":\"01\","
167 + "\"length\":\"2\",\"increment\":\"1\",\"sequence-type\":\"numeric\"}}],"
168 + "\"naming-type\":\"INTERNAL_NETWORK\",\"nfRole\":\"vPE\","
169 + "\"naming-recipe\":\"VNF_NAME|DELIMITER|CONSTANT|SEQUENCE\"}]}}";
170 Map<Object, Object> configMap = new HashMap<>();
171 configMap.put("config", config);
172 ObjectMapper objectmapper = new ObjectMapper();
173 List<Map<Object, Object>> respList = new ArrayList<>();
174 respList.add(configMap);
175 policyFinder.transformConfigObject(objectmapper, respList);
176 assertNotNull(respList.get(0).get("config"));
179 Map<String, Object> buildPolicyResponse() {
180 Map<String, Object> policyDataMap = new HashMap<>();
181 policyDataMap.put("policy-instance-name", "SDNC_Policy.Config_MS_VNFCNamingPolicy");
182 Map<String, Object> namingModelMap = new HashMap<>();
183 namingModelMap.put("nf-role", "vPE");
184 namingModelMap.put("naming-type", "VNF");
185 namingModelMap.put("naming-recipe", "COMPLEX|NF-NAMING-CODE|Field2|Field3|Field4");
186 Map<String, Object> namingPropertyMap = new HashMap<>();
187 Map<String, Object> propertyMap1 = new HashMap<>();
188 propertyMap1.put("property-name", "COMPLEX");
189 Map<String, Object> propertyMap2 = new HashMap<>();
190 propertyMap2.put("property-name", "NF-NAMING-CODE");
191 namingPropertyMap.put("", Arrays.asList(new Object[] {propertyMap1, propertyMap2}));
192 namingModelMap.put("naming-properties", namingPropertyMap);
193 policyDataMap.put("naming-models", Arrays.asList(new Object[] {namingModelMap}));
194 Map<String, Object> configMap = new HashMap<>();
195 Map<String, Object> contentMap = new HashMap<>();
196 contentMap.put("content", policyDataMap);
197 configMap.put("config", contentMap);