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