2 * ============LICENSE_START=======================================================
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
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.openecomp.sdc.ci.tests.execute.inputs;
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;
43 import java.io.IOException;
44 import java.util.ArrayList;
45 import java.util.HashMap;
46 import java.util.List;
48 import java.util.stream.Collectors;
50 import static org.testng.AssertJUnit.assertTrue;
57 public class InputsApiTests extends ComponentBaseTest {
59 private static String inputCsar1 = "FCGI_with_inputs.csar";
60 private static String inputCsar2 = "LDSA1_with_inputs.csar";
61 private static User sdncDesignerDetails = null;
64 public static TestName name = new TestName();
69 public InputsApiTests() {
70 super(name, InputsApiTests.class.getName());
71 sdncDesignerDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
75 * Create VF with inputs from CSAR file
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());
86 * Create service and add to it VF instance with inputs
91 public void testAddVfInstanceWithInputsToService() throws Exception {
92 createServiceWithVFInstanceWithInputs();
96 * General test to check most functionality of inputs
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>
108 public void testInputsMainFunctionality() throws Exception {
109 Service service = createServiceWithVFInstanceWithInputs();
110 int totalInputsBeforeAdd = service.getInputs().size();
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());
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());
123 // Take only the 2 first inputs
124 List<ComponentInstancePropInput> inputsToAdd = instanceInputs.stream().limit(2).collect(Collectors.toList());
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);
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);
139 // Delete 1 input from service
140 RestResponse deleteInputFromComponentResponse = InputsRestUtils.deleteInputFromComponent(service, serviceInputsAfterAdd.get(0).getUniqueId());
141 BaseValidationUtils.checkSuccess(deleteInputFromComponentResponse);
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);
151 assertTrue("Success testing inputs main functionality", true);
155 * Private method to create service with VF instance that has inputs
156 * This is private method to be used by multiple tests
158 * @return {@link org.openecomp.sdc.be.model}
160 * @throws IOException
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);
168 Service service = createDefaultServiceEither.left().value();
170 // Create VF from CSAR file
171 Resource vfWithInputs = AtomicOperationUtils.importResourceFromCsar(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, inputCsar2);
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);
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);
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);
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);
193 assertTrue("Error creating service with VF instance", false);
199 * Return default ComponentInstInputsMap
203 * @return {@link org.openecomp.sdc.be.model.ComponentInstInputsMap}
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;