re base code
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / execute / inputs / InputsApiTests.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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.inputs;
22
23 import com.google.gson.Gson;
24 import com.google.gson.reflect.TypeToken;
25 import fj.data.Either;
26 import org.apache.commons.lang3.tuple.Pair;
27 import org.junit.Rule;
28 import org.junit.rules.TestName;
29 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
30 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
31 import org.openecomp.sdc.be.model.*;
32 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
33 import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
34 import org.openecomp.sdc.ci.tests.datatypes.enums.LifeCycleStatesEnum;
35 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
36 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
37 import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
38 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
39 import org.openecomp.sdc.ci.tests.utils.rest.*;
40 import org.openecomp.sdc.ci.tests.utils.validation.BaseValidationUtils;
41 import org.testng.annotations.Test;
42
43 import java.io.IOException;
44 import java.util.ArrayList;
45 import java.util.HashMap;
46 import java.util.List;
47 import java.util.Map;
48 import java.util.stream.Collectors;
49
50 import static org.testng.AssertJUnit.assertTrue;
51
52 /**
53  * CI-Tests for inputs 
54  * @author il0695
55  *
56  */
57 public class InputsApiTests extends ComponentBaseTest {
58
59         private static String inputCsar1 = "FCGI_with_inputs.csar";
60         private static String inputCsar2 = "LDSA1_with_inputs.csar";
61         private static User  sdncDesignerDetails = null;
62         
63         @Rule
64         public static TestName name = new TestName();
65         
66         /**
67          * Constructor
68          */
69         public InputsApiTests() {
70                 super(name, InputsApiTests.class.getName());
71                 sdncDesignerDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
72         }
73         
74         /**
75          * Create VF with inputs from CSAR file
76          * 
77          * @throws Exception 
78          */
79         @Test
80         public void testCreateResourceInstanceWithInputsFromCsar() throws Exception {
81                 Resource vf = AtomicOperationUtils.importResourceFromCsar(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, inputCsar1);
82                 assertTrue("Success creating VF from CSAR", !vf.getInputs().isEmpty());
83         }
84         
85         /**
86          * Create service and add to it VF instance with inputs
87          * 
88          * @throws Exception 
89          */
90         @Test
91         public void testAddVfInstanceWithInputsToService() throws Exception {
92                 createServiceWithVFInstanceWithInputs();
93         }
94         
95         /**
96          * General test to check most functionality of inputs
97          * <ul>
98          *      <li>Create service with VF instance that has inputs)</li>
99          *      <li>Get all inputs of VF instance</li>
100          *      <li>Add inputs to service</li>
101          *      <li>Get service inputs</li>
102          *      <li>Delete service inputs</li>
103          * </ul>
104          * 
105          * @throws Exception 
106          */
107         @Test
108         public void testInputsMainFunctionality() throws Exception {
109                 Service service = createServiceWithVFInstanceWithInputs();
110                 int totalInputsBeforeAdd = service.getInputs().size();
111                 
112                 // Get component instances
113                 RestResponse getInstancesResponse = ComponentInstanceRestUtils.getComponentInstances(ComponentTypeEnum.SERVICE, service.getUniqueId(), sdncDesignerDetails);
114                 BaseValidationUtils.checkSuccess(getInstancesResponse);
115                 List<ComponentInstance> serviceInstances = new Gson().fromJson(getInstancesResponse.getResponse(), new TypeToken<ArrayList<ComponentInstance>>(){}.getType());
116                 
117                 // Get all inputs of first instance
118                 ComponentInstance vfInstance = serviceInstances.get(0);
119                 RestResponse getComponentInstanceInputsResponse = InputsRestUtils.getComponentInstanceInputs(service, vfInstance);
120                 BaseValidationUtils.checkSuccess(getComponentInstanceInputsResponse);
121                 List<ComponentInstancePropInput> instanceInputs = new Gson().fromJson(getComponentInstanceInputsResponse.getResponse(), new TypeToken<ArrayList<ComponentInstancePropInput>>(){}.getType());
122                 
123                 // Take only the 2 first inputs
124                 List<ComponentInstancePropInput> inputsToAdd = instanceInputs.stream().limit(2).collect(Collectors.toList());
125                 
126                 // Build component instances input map to add to server
127                 ComponentInstInputsMap buildComponentInstInputsMap = buildComponentInstInputsMap(vfInstance.getUniqueId(), inputsToAdd);
128                 RestResponse addInputResponse = InputsRestUtils.addInput(service, buildComponentInstInputsMap, UserRoleEnum.DESIGNER);
129                 BaseValidationUtils.checkSuccess(addInputResponse);
130                 
131                 // Get service inputs count
132                 RestResponse getComponentInputsResponse = InputsRestUtils.getComponentInputs(service);
133                 BaseValidationUtils.checkSuccess(getComponentInputsResponse);
134                 List<InputDefinition> serviceInputsAfterAdd = new Gson().fromJson(getComponentInputsResponse.getResponse(), new TypeToken<ArrayList<InputDefinition>>(){}.getType());
135                 if (serviceInputsAfterAdd.size()-totalInputsBeforeAdd!=2) {
136                         assertTrue("Error adding inputs to service (service should have 2 inputs)", false);
137                 }
138                 
139                 // Delete 1 input from service
140                 RestResponse deleteInputFromComponentResponse = InputsRestUtils.deleteInputFromComponent(service, serviceInputsAfterAdd.get(0).getUniqueId());
141                 BaseValidationUtils.checkSuccess(deleteInputFromComponentResponse);
142                 
143                 // Get service inputs count after delete
144                 RestResponse getComponentInputsResponseAfterDelete = InputsRestUtils.getComponentInputs(service);
145                 BaseValidationUtils.checkSuccess(getComponentInputsResponseAfterDelete);
146                 List<InputDefinition> serviceInputsAfterDelete = new Gson().fromJson(getComponentInputsResponseAfterDelete.getResponse(), new TypeToken<ArrayList<InputDefinition>>(){}.getType());
147                 if (serviceInputsAfterDelete.size()-totalInputsBeforeAdd!=1) {
148                         assertTrue("Error deleting inputs from service (service should have 1 input)", false);
149                 }
150                 
151                 assertTrue("Success testing inputs main functionality", true);
152         }
153
154         /**
155          * Private method to create service with VF instance that has inputs
156          * This is private method to be used by multiple tests
157          * 
158          * @return {@link org.openecomp.sdc.be.model}
159          * @throws Exception
160          * @throws IOException
161          */
162         private Service createServiceWithVFInstanceWithInputs() throws Exception, IOException {
163                 // Create default service
164                 Either<Service, RestResponse> createDefaultServiceEither = AtomicOperationUtils.createDefaultService(UserRoleEnum.DESIGNER, true);
165                 if (createDefaultServiceEither.isRight()){
166                         assertTrue("Error creating default service", false);
167                 }
168                 Service service = createDefaultServiceEither.left().value();
169                 
170                 // Create VF from CSAR file
171                 Resource vfWithInputs = AtomicOperationUtils.importResourceFromCsar(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, inputCsar2);
172
173                 // Certify VF
174                 Pair<Component, RestResponse> changeComponentState = AtomicOperationUtils.changeComponentState(vfWithInputs, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true);
175                 assertTrue("response code is BaseRestUtils.STATUS_CODE_SUCCESS, returned :" + changeComponentState.getRight().getErrorCode(), changeComponentState.getRight().getErrorCode() == BaseRestUtils.STATUS_CODE_SUCCESS);
176                 
177                 // Add VF instance to service
178                 Either<ComponentInstance, RestResponse> addComponentInstanceToComponentContainerEither = AtomicOperationUtils.addComponentInstanceToComponentContainer(vfWithInputs, service, UserRoleEnum.DESIGNER, true);
179                 if (addComponentInstanceToComponentContainerEither.isRight()){
180                         assertTrue("Error adding VF to service", false);
181                 }
182                                 
183                 // Get service response
184                 ServiceReqDetails serviceDetails = new ServiceReqDetails(service);
185                 RestResponse getServiceResponse = ServiceRestUtils.getService(serviceDetails, ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER));
186                 service = ResponseParser.parseToObjectUsingMapper(getServiceResponse.getResponse(), Service.class);
187                 
188                 // Get VF instance from service
189                 ComponentInstance vfInstance = service.getComponentInstances().get(0);
190                 if (vfInstance!=null){
191                         assertTrue("Success creating service with VF instance", true);
192                 } else {
193                         assertTrue("Error creating service with VF instance", false);
194                 }
195                 return service;
196         }
197         
198         /**
199          * Return default ComponentInstInputsMap
200          * 
201          * @param addToInput
202          * @param inputs
203          * @return {@link org.openecomp.sdc.be.model.ComponentInstInputsMap}
204          */
205         private ComponentInstInputsMap buildComponentInstInputsMap (String addToInput, List<ComponentInstancePropInput> inputs) {
206                 Map<String, List<ComponentInstancePropInput>> map = new HashMap<>();
207                 map.put(addToInput, inputs);
208                 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
209                 componentInstInputsMap.setComponentInstanceInputsMap(map);              
210                 return componentInstInputsMap;
211         }
212
213 }