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