764a147d14cf4d357e7daef52f8492bbe451dc25
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.ci.tests.execute.interfaceoperation;
22
23 import static org.testng.AssertJUnit.fail;
24
25 import com.fasterxml.jackson.annotation.JsonInclude;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27
28 import java.util.HashMap;
29 import java.util.Map;
30
31 import fj.data.Either;
32 import org.apache.commons.collections4.CollectionUtils;
33 import org.junit.Rule;
34 import org.junit.rules.TestName;
35 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
36 import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
37 import org.openecomp.sdc.be.datatypes.elements.OperationOutputDefinition;
38 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
39 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
40 import org.openecomp.sdc.be.model.InputDefinition;
41 import org.openecomp.sdc.be.model.InterfaceDefinition;
42 import org.openecomp.sdc.be.model.Operation;
43 import org.openecomp.sdc.be.model.PropertyDefinition;
44 import org.openecomp.sdc.be.model.Resource;
45 import org.openecomp.sdc.be.model.Service;
46 import org.openecomp.sdc.be.model.User;
47 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
48 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
49 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
50 import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
51 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
52 import org.openecomp.sdc.ci.tests.utils.rest.BaseRestUtils;
53 import org.openecomp.sdc.ci.tests.utils.rest.InterfaceOperationsRestUtils;
54 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
55 import org.openecomp.sdc.ci.tests.utils.rest.ServiceRestUtils;
56 import org.testng.Assert;
57 import org.testng.annotations.BeforeClass;
58 import org.testng.annotations.Test;
59
60 public class InterfaceOperationsTest extends ComponentBaseTest {
61
62     @Rule
63     private static final TestName name = new TestName();
64     private static final String INTERFACES = "interfaces";
65     private static final String TOSCA_PRESENTATION = "toscaPresentation";
66     private static final User user = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
67     private static final String WORKFLOW_ID_STR = "WorkflowId";
68     private static final String WORKFLOW_VERSION_ID_STR = "workflowVersionId";
69     private static final String WORKFLOW_ASSOCIATION_TYPE_NONE_STR = "NONE";
70
71     private static Service service;
72     private static Resource resource;
73     private static Resource pnfResource;
74     private String resourceInterfaceUniqueId;
75     private String resourceOperationUniqueId;
76     private String pnfResourceInterfaceUniqueId;
77     private String pnfResourceOperationUniqueId;
78     private String serviceInterfaceUniqueId;
79     private String serviceOperationUniqueId;
80
81     @BeforeClass
82     public static void init() throws Exception {
83         // Create default service
84         Either<Service, RestResponse> createDefaultServiceEither =
85                 AtomicOperationUtils.createDefaultService(UserRoleEnum.DESIGNER, true);
86         if (createDefaultServiceEither.isRight()) {
87             fail("Error creating default service");
88         }
89         service = createDefaultServiceEither.left().value();
90
91         // Create default resource
92         Either<Resource, RestResponse> createDefaultResourceEither =
93                 AtomicOperationUtils.createResourceByType(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, true);
94         if (createDefaultResourceEither.isRight()) {
95             fail("Error creating default resource");
96         }
97         resource = createDefaultResourceEither.left().value();
98
99         // Create default PNF resource
100         Either<Resource, RestResponse> createDefaultPNFResourceEither =
101                 AtomicOperationUtils.createResourceByType(ResourceTypeEnum.PNF, UserRoleEnum.DESIGNER, true);
102         if (createDefaultPNFResourceEither.isRight()) {
103             fail("Error creating default pnf resource");
104         }
105         pnfResource = createDefaultPNFResourceEither.left().value();
106     }
107
108     public Map<String, Object> buildInterfaceDefinitionForResource(Resource resource,
109                                                                     String resourceInterfaceUniqueId,
110                                                                     String resourceOperationUniqueId) {
111         Operation operation = new Operation();
112         operation.setName("TestOperationOnResource");
113         operation.setWorkflowId(WORKFLOW_ID_STR);
114         operation.setWorkflowVersionId(WORKFLOW_VERSION_ID_STR);
115         operation.setWorkflowAssociationType(WORKFLOW_ASSOCIATION_TYPE_NONE_STR);
116         if(CollectionUtils.isNotEmpty(resource.getInputs())){
117             PropertyDefinition property =
118                     resource.getInputs().stream().filter(a -> a.getName().equalsIgnoreCase("nf_naming")).findFirst()
119                             .orElse(new InputDefinition());
120             ListDataDefinition<OperationInputDefinition> operationInputDefinitionList = new ListDataDefinition<>();
121             operationInputDefinitionList.add(createOperationInputDefinition("TestInput1", property.getUniqueId()));
122             operation.setInputs(operationInputDefinitionList);
123         }
124         ListDataDefinition<OperationOutputDefinition> operationOutputDefinitionList = new ListDataDefinition<>();
125         operationOutputDefinitionList.add(createOperationOutputDefinition("TestOutput1"));
126         operation.setOutputs(operationOutputDefinitionList);
127         return buildInterfaceDefinitionMap(operation, "TestInterface", resourceInterfaceUniqueId,
128                 resourceOperationUniqueId);
129     }
130
131     private Map<String, Object> buildInterfaceDefinitionOfGlobalTypeForResource(Resource resource) {
132         Operation operation = new Operation();
133         operation.setName("create");
134         operation.setWorkflowId(WORKFLOW_ID_STR);
135         operation.setWorkflowVersionId(WORKFLOW_VERSION_ID_STR);
136         operation.setWorkflowAssociationType("NONE");
137         if(CollectionUtils.isNotEmpty(resource.getInputs())){
138             PropertyDefinition property =
139                     resource.getInputs().stream().filter(a -> a.getName().equalsIgnoreCase("nf_naming")).findFirst()
140                             .orElse(new InputDefinition());
141             ListDataDefinition<OperationInputDefinition> operationInputDefinitionList = new ListDataDefinition<>();
142             operationInputDefinitionList.add(createOperationInputDefinition("TestInput1", property.getUniqueId()));
143             operation.setInputs(operationInputDefinitionList);
144         }
145         ListDataDefinition<OperationOutputDefinition> operationOutputDefinitionList = new ListDataDefinition<>();
146         operationOutputDefinitionList.add(createOperationOutputDefinition("TestOutput1"));
147         operation.setOutputs(operationOutputDefinitionList);
148         return buildInterfaceDefinitionMap(operation, "tosca.interfaces.node.lifecycle.Standard",
149                 resourceInterfaceUniqueId, resourceOperationUniqueId);
150     }
151
152     private OperationInputDefinition createOperationInputDefinition(String name, String inputId) {
153         OperationInputDefinition operationInputDefinition = new OperationInputDefinition();
154         operationInputDefinition.setName(name);
155         operationInputDefinition.setInputId(inputId);
156         operationInputDefinition.setRequired(true);
157         operationInputDefinition.setType("string");
158         return operationInputDefinition;
159     }
160
161     private OperationOutputDefinition createOperationOutputDefinition(String name) {
162         OperationOutputDefinition operationOutputDefinition = new OperationOutputDefinition();
163         operationOutputDefinition.setName(name);
164         operationOutputDefinition.setRequired(true);
165         operationOutputDefinition.setType("string");
166         return operationOutputDefinition;
167     }
168
169     private Map<String, Object> buildInterfaceDefinitionMap(Operation operation, String interfaceType,
170                                                             String interfaceId,
171                                                             String operationId) {
172         if (operationId != null) {
173             operation.setUniqueId(operationId);
174         }
175         Map<String, Operation> operationMap = new HashMap<>();
176         operationMap.put(operation.getName(), operation);
177
178         InterfaceDefinition interfaceDefinition = new InterfaceDefinition();
179         interfaceDefinition.setType(interfaceType);
180         interfaceDefinition.setOperationsMap(operationMap);
181         if (interfaceId != null) {
182             interfaceDefinition.setUniqueId(interfaceId);
183         }
184         interfaceDefinition.setOperationsMap(operationMap);
185
186         Map<String, Object> interfaceDefAsMap = getObjectAsMap(interfaceDefinition);
187         Map<String, Object> interfaceMap = new HashMap<>();
188         interfaceMap.put(interfaceDefinition.getType(), interfaceDefAsMap);
189         Map<String, Object> outerMap = new HashMap<>();
190         outerMap.put(INTERFACES, interfaceMap);
191         return outerMap;
192     }
193
194     private static Map<String, Object> getObjectAsMap(Object obj) {
195         ObjectMapper objectMapper = new ObjectMapper();
196         if (obj instanceof InterfaceDefinition) {
197             objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
198         }
199         Map<String, Object> objectAsMap =
200                 obj instanceof Map ? (Map<String, Object>) obj : objectMapper.convertValue(obj, Map.class);
201         objectAsMap.remove(TOSCA_PRESENTATION);
202         return objectAsMap;
203     }
204
205     public Map<String, Object> buildInterfaceDefinitionForService() {
206         Operation operation = new Operation();
207         operation.setName("TestOperationOnService");
208         operation.setWorkflowId(WORKFLOW_ID_STR);
209         operation.setWorkflowVersionId(WORKFLOW_VERSION_ID_STR);
210         operation.setWorkflowAssociationType("NONE");
211         return buildInterfaceDefinitionMap(operation, "TestInterface", serviceInterfaceUniqueId,
212                 serviceOperationUniqueId);
213     }
214
215     @Test
216     public void addInterfaceOperationsOnResource() throws Exception {
217         RestResponse restResponse = InterfaceOperationsRestUtils
218                 .addInterfaceOperations(resource,
219                         buildInterfaceDefinitionForResource(resource, resourceInterfaceUniqueId,
220                                 resourceOperationUniqueId), user);
221         logger.info("addInterfaceOperationsOnResource Response Code:" + restResponse.getErrorCode());
222         Assert.assertEquals((int) restResponse.getErrorCode(), BaseRestUtils.STATUS_CODE_SUCCESS);
223         String interfaceDefinitionStr = ResponseParser.getListFromJson(restResponse, INTERFACES).get(0).toString();
224         InterfaceDefinition interfaceDefinition =
225                 ResponseParser.convertInterfaceDefinitionResponseToJavaObject(interfaceDefinitionStr);
226         resourceInterfaceUniqueId = interfaceDefinition.getUniqueId();
227         resourceOperationUniqueId = interfaceDefinition.getOperationsMap().keySet().stream().findFirst().orElse(null);
228     }
229
230     @Test(dependsOnMethods = "addInterfaceOperationsOnResource")
231     public void getInterfaceOperationsFromResource() throws Exception {
232         RestResponse restResponse = InterfaceOperationsRestUtils
233                 .getInterfaceOperations(resource, resourceInterfaceUniqueId,
234                         resourceOperationUniqueId, user);
235         logger.info("getInterfaceOperationsFromResource Response Code:" + restResponse.getErrorCode());
236         Assert.assertEquals((int) restResponse.getErrorCode(), BaseRestUtils.STATUS_CODE_SUCCESS);
237     }
238
239     @Test(dependsOnMethods = "getInterfaceOperationsFromResource")
240     public void updateInterfaceOperationsOnResource() throws Exception {
241         RestResponse restResponse = InterfaceOperationsRestUtils
242                 .updateInterfaceOperations(resource,
243                         buildInterfaceDefinitionForResource(resource, resourceInterfaceUniqueId, resourceOperationUniqueId),
244                         user);
245         logger.info("updateInterfaceOperationsOnResource Response Code:" + restResponse.getErrorCode());
246         Assert.assertEquals((int) restResponse.getErrorCode(), BaseRestUtils.STATUS_CODE_SUCCESS);
247     }
248
249     @Test(dependsOnMethods = "updateInterfaceOperationsOnResource")
250     public void deleteInterfaceOperationsFromResource() throws Exception {
251         RestResponse restResponse = InterfaceOperationsRestUtils
252                 .deleteInterfaceOperations(resource, resourceInterfaceUniqueId,
253                         resourceOperationUniqueId, user);
254         logger.info("deleteInterfaceOperationsFromResource Response Code:" + restResponse.getErrorCode());
255         Assert.assertEquals((int) restResponse.getErrorCode(), BaseRestUtils.STATUS_CODE_SUCCESS);
256     }
257
258     @Test
259     public void addInterfaceOperationsOnPNFResource() throws Exception {
260         RestResponse restResponse = InterfaceOperationsRestUtils
261                 .addInterfaceOperations(pnfResource, buildInterfaceDefinitionForResource(pnfResource, pnfResourceInterfaceUniqueId,
262                         pnfResourceOperationUniqueId), user);
263         logger.info("addInterfaceOperationsOnPNFResource Response Code:" + restResponse.getErrorCode());
264         Assert.assertEquals((int) restResponse.getErrorCode(), BaseRestUtils.STATUS_CODE_SUCCESS);
265         String interfaceDefinitionStr = ResponseParser.getListFromJson(restResponse, INTERFACES).get(0).toString();
266         InterfaceDefinition interfaceDefinition =
267                 ResponseParser.convertInterfaceDefinitionResponseToJavaObject(interfaceDefinitionStr);
268         pnfResourceInterfaceUniqueId = interfaceDefinition.getUniqueId();
269         pnfResourceOperationUniqueId =
270                 interfaceDefinition.getOperationsMap().keySet().stream().findFirst().orElse(null);
271     }
272
273     @Test(dependsOnMethods = "addInterfaceOperationsOnPNFResource")
274     public void getInterfaceOperationsFromPNFResource() throws Exception {
275         RestResponse restResponse = InterfaceOperationsRestUtils
276                 .getInterfaceOperations(pnfResource, pnfResourceInterfaceUniqueId,
277                         pnfResourceOperationUniqueId, user);
278         logger.info("getInterfaceOperationsFromPNFResource Response Code:" + restResponse.getErrorCode());
279         Assert.assertEquals((int) restResponse.getErrorCode(), BaseRestUtils.STATUS_CODE_SUCCESS);
280     }
281
282     @Test(dependsOnMethods = "getInterfaceOperationsFromPNFResource")
283     public void updateInterfaceOperationsOnPNFResource() throws Exception {
284         RestResponse restResponse = InterfaceOperationsRestUtils
285                 .updateInterfaceOperations(pnfResource,
286                         buildInterfaceDefinitionForResource(pnfResource, pnfResourceInterfaceUniqueId,
287                                 pnfResourceOperationUniqueId), user);
288         logger.info("updateInterfaceOperationsOnPNFResource Response Code:" + restResponse.getErrorCode());
289         Assert.assertEquals((int) restResponse.getErrorCode(), BaseRestUtils.STATUS_CODE_SUCCESS);
290     }
291
292     @Test(dependsOnMethods = "updateInterfaceOperationsOnPNFResource")
293     public void deleteInterfaceOperationsFromPNFResource() throws Exception {
294         RestResponse restResponse = InterfaceOperationsRestUtils
295                 .deleteInterfaceOperations(pnfResource, pnfResourceInterfaceUniqueId,
296                         pnfResourceOperationUniqueId, user);
297         logger.info("deleteInterfaceOperationsFromPNFResource Response Code:" + restResponse.getErrorCode());
298         Assert.assertEquals((int) restResponse.getErrorCode(), BaseRestUtils.STATUS_CODE_SUCCESS);
299     }
300
301     @Test
302     public void testCreateOperationWithLocalInterfaceAndDownloadArtifact() throws Exception{
303         Either<Service, RestResponse> createDefaultServiceEither =
304                 AtomicOperationUtils.createDefaultService(UserRoleEnum.DESIGNER, true);
305         if (createDefaultServiceEither.isRight()) {
306             fail("Error creating default service");
307         }
308         Service service = createDefaultServiceEither.left().value();
309         String serviceUniqueId = service.getUniqueId();
310         Operation operation = new Operation();
311         operation.setName("LocalOper");
312         operation.setWorkflowAssociationType("NONE");
313         Map<String, Object> interfaceOperationMap = buildInterfaceDefinitionMap(operation, "Local", null, null);
314
315         RestResponse restResponse = InterfaceOperationsRestUtils.addInterfaceOperations(service, interfaceOperationMap,
316                 user);
317
318         Integer responseCode = restResponse.getErrorCode();
319         Integer expectedCode = 200;
320         Assert.assertEquals(responseCode, expectedCode);
321
322         service = ResponseParser.convertServiceResponseToJavaObject(
323                 ServiceRestUtils.getServiceToscaArtifacts(service.getUniqueId()).getResponse());
324         service.setUniqueId(serviceUniqueId);
325         service.setComponentType(ComponentTypeEnum.SERVICE);
326         service.setLastUpdaterUserId(user.getUserId());
327         Either<String, RestResponse> responseEither = AtomicOperationUtils
328                 .getComponenetArtifactPayload(service, "assettoscacsar");
329
330         Assert.assertTrue(responseEither.isLeft());
331     }
332
333     @Test
334     public void addInterfaceOperationsOnService() throws Exception {
335         RestResponse restResponse = InterfaceOperationsRestUtils
336                 .addInterfaceOperations(service, buildInterfaceDefinitionForService(),
337                         user);
338         logger.info("addInterfaceOperationsOnService Response Code:" + restResponse.getErrorCode());
339         Assert.assertEquals((int) restResponse.getErrorCode(), BaseRestUtils.STATUS_CODE_SUCCESS);
340         String interfaceDefinitionStr = ResponseParser.getListFromJson(restResponse, INTERFACES).get(0).toString();
341         InterfaceDefinition interfaceDefinition =
342                 ResponseParser.convertInterfaceDefinitionResponseToJavaObject(interfaceDefinitionStr);
343         serviceInterfaceUniqueId = interfaceDefinition.getUniqueId();
344         serviceOperationUniqueId = interfaceDefinition.getOperationsMap().keySet().stream().findFirst().orElse(null);
345     }
346
347     @Test(dependsOnMethods = "addInterfaceOperationsOnService")
348     public void getInterfaceOperationsFromService() throws Exception {
349         RestResponse restResponse = InterfaceOperationsRestUtils
350                 .getInterfaceOperations(service, serviceInterfaceUniqueId,
351                         serviceOperationUniqueId, user);
352         logger.info("getInterfaceOperationsFromService Response Code:" + restResponse.getErrorCode());
353         Assert.assertEquals((int) restResponse.getErrorCode(), BaseRestUtils.STATUS_CODE_SUCCESS);
354     }
355
356     @Test(dependsOnMethods = "getInterfaceOperationsFromService")
357     public void updateInterfaceOperationsOnService() throws Exception {
358         RestResponse restResponse = InterfaceOperationsRestUtils
359                 .updateInterfaceOperations(service, buildInterfaceDefinitionForService(),
360                         user);
361         logger.info("updateInterfaceOperations Response Code:" + restResponse.getErrorCode());
362         Assert.assertEquals((int) restResponse.getErrorCode(), BaseRestUtils.STATUS_CODE_SUCCESS);
363     }
364
365     @Test(dependsOnMethods = "updateInterfaceOperationsOnService")
366     public void deleteInterfaceOperationsFromService() throws Exception {
367         RestResponse restResponse = InterfaceOperationsRestUtils
368                 .deleteInterfaceOperations(service, serviceInterfaceUniqueId,
369                         serviceOperationUniqueId, user);
370         logger.info("deleteInterfaceOperations Response Code:" + restResponse.getErrorCode());
371         Assert.assertEquals((int) restResponse.getErrorCode(), BaseRestUtils.STATUS_CODE_SUCCESS);
372     }
373
374     @Test
375     public void addInterfaceOperationsOfGlobalTypeOnResource() throws Exception {
376         RestResponse restResponse =
377                 InterfaceOperationsRestUtils.addInterfaceOperations(resource,
378                         buildInterfaceDefinitionOfGlobalTypeForResource(resource), user);
379
380         logger.info("addInterfaceOperationsOnResource Response Code:" + restResponse.getErrorCode());
381         Assert.assertEquals((int) restResponse.getErrorCode(), BaseRestUtils.STATUS_CODE_SUCCESS);
382     }
383
384     @Test
385     public void addInterfaceOperationsOfGlobalTypeOnPNFResource() throws Exception {
386         RestResponse restResponse =
387                 InterfaceOperationsRestUtils.addInterfaceOperations(pnfResource,
388                         buildInterfaceDefinitionOfGlobalTypeForResource(pnfResource), user);
389
390         logger.info("addInterfaceOperationsOnPNFResource Response Code:" + restResponse.getErrorCode());
391         Assert.assertEquals((int) restResponse.getErrorCode(), BaseRestUtils.STATUS_CODE_SUCCESS);
392         String interfaceDefinitionStr = ResponseParser.getListFromJson(restResponse, INTERFACES).get(0).toString();
393         InterfaceDefinition interfaceDefinition =
394                 ResponseParser.convertInterfaceDefinitionResponseToJavaObject(interfaceDefinitionStr);
395     }
396
397 }