aboutsummaryrefslogtreecommitdiffstats
path: root/integration-tests/src/test/java/org/openecomp/sdc/ci/tests/execute/inputs/InputsApiTests.java
blob: 292e4788844125647bf260c32474301ea339b359 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.ci.tests.execute.inputs;

import static org.testng.AssertJUnit.assertTrue;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import fj.data.Either;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.Rule;
import org.junit.rules.TestName;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
import org.openecomp.sdc.be.model.Component;
import org.openecomp.sdc.be.model.ComponentInstInputsMap;
import org.openecomp.sdc.be.model.ComponentInstance;
import org.openecomp.sdc.be.model.ComponentInstancePropInput;
import org.openecomp.sdc.be.model.InputDefinition;
import org.openecomp.sdc.be.model.PropertyConstraint;
import org.openecomp.sdc.be.model.PropertyDefinition;
import org.openecomp.sdc.be.model.Resource;
import org.openecomp.sdc.be.model.Service;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
import org.openecomp.sdc.ci.tests.datatypes.PropertyReqDetails;
import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
import org.openecomp.sdc.ci.tests.datatypes.enums.LifeCycleStatesEnum;
import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
import org.openecomp.sdc.ci.tests.utils.rest.BaseRestUtils;
import org.openecomp.sdc.ci.tests.utils.rest.ComponentInstanceRestUtils;
import org.openecomp.sdc.ci.tests.utils.rest.InputsRestUtils;
import org.openecomp.sdc.ci.tests.utils.rest.PropertyRestUtils;
import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
import org.openecomp.sdc.ci.tests.utils.rest.ServiceRestUtils;
import org.openecomp.sdc.ci.tests.utils.validation.BaseValidationUtils;
import org.testng.AssertJUnit;
import org.testng.annotations.Test;

/**
 * CI-Tests for inputs
 * @author il0695
 *
 */
public class InputsApiTests extends ComponentBaseTest {

	private static String inputCsar1 = "FCGI_with_inputs.csar";
	private static String inputCsar2 = "LDSA1_with_inputs.csar";
	private static User  sdncDesignerDetails = null;

	@Rule
	public static TestName name = new TestName();

	/**
	 * Constructor
	 */
	public InputsApiTests() {
		super();
		sdncDesignerDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
	}

	/**
	 * Create VF with inputs from CSAR file
	 *
	 * @throws Exception
	 */
	@Test
	public void testCreateResourceInstanceWithInputsFromCsar() throws Exception {
		Resource vf = AtomicOperationUtils.importResourceFromCsar(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, inputCsar1);
		assertTrue("Success creating VF from CSAR", !vf.getInputs().isEmpty());
	}

	/**
	 * Create service and add to it VF instance with inputs
	 *
	 * @throws Exception
	 */
	@Test
	public void testAddVfInstanceWithInputsToService() throws Exception {
		createServiceWithVFInstanceWithInputs();
	}

	/**
	 * General test to check most functionality of inputs
	 * <ul>
	 * 	<li>Create service with VF instance that has inputs)</li>
	 * 	<li>Get all inputs of VF instance</li>
	 * 	<li>Add inputs to service</li>
	 * 	<li>Get service inputs</li>
	 * 	<li>Delete service inputs</li>
	 * </ul>
	 *
	 * @throws Exception
	 */
	@Test
	public void testInputsMainFunctionality() throws Exception {
		Service service = createServiceWithVFInstanceWithInputs();
		int totalInputsBeforeAdd = service.getInputs().size();

		// Get component instances
		RestResponse getInstancesResponse = ComponentInstanceRestUtils.getComponentInstances(ComponentTypeEnum.SERVICE, service.getUniqueId(), sdncDesignerDetails);
		BaseValidationUtils.checkSuccess(getInstancesResponse);
		List<ComponentInstance> serviceInstances = new Gson().fromJson(getInstancesResponse.getResponse(), new TypeToken<ArrayList<ComponentInstance>>(){}.getType());

		// Get all inputs of first instance
		ComponentInstance vfInstance = serviceInstances.get(0);
		RestResponse getComponentInstanceInputsResponse = InputsRestUtils.getComponentInstanceInputs(service, vfInstance);
		BaseValidationUtils.checkSuccess(getComponentInstanceInputsResponse);
		List<ComponentInstancePropInput> instanceInputs = new Gson().fromJson(getComponentInstanceInputsResponse.getResponse(), new TypeToken<ArrayList<ComponentInstancePropInput>>(){}.getType());

		// Take only the 2 first inputs
		List<ComponentInstancePropInput> inputsToAdd = instanceInputs.stream().limit(2).collect(Collectors.toList());

		// Build component instances input map to add to server
		ComponentInstInputsMap buildComponentInstInputsMap = buildComponentInstInputsMap(vfInstance.getUniqueId(), inputsToAdd);
		RestResponse addInputResponse = InputsRestUtils.addInput(service, buildComponentInstInputsMap, UserRoleEnum.DESIGNER);
		BaseValidationUtils.checkSuccess(addInputResponse);

		// Get service inputs count
		RestResponse getComponentInputsResponse = InputsRestUtils.getComponentInputs(service);
		BaseValidationUtils.checkSuccess(getComponentInputsResponse);
		List<InputDefinition> serviceInputsAfterAdd = new Gson().fromJson(getComponentInputsResponse.getResponse(), new TypeToken<ArrayList<InputDefinition>>(){}.getType());
		if (serviceInputsAfterAdd.size()-totalInputsBeforeAdd!=2) {
			assertTrue("Error adding inputs to service (service should have 2 inputs)", false);
		}

		// Delete 1 input from service
		RestResponse deleteInputFromComponentResponse = InputsRestUtils.deleteInputFromComponent(service, serviceInputsAfterAdd.get(0).getUniqueId());
		BaseValidationUtils.checkSuccess(deleteInputFromComponentResponse);

		// Get service inputs count after delete
		RestResponse getComponentInputsResponseAfterDelete = InputsRestUtils.getComponentInputs(service);
		BaseValidationUtils.checkSuccess(getComponentInputsResponseAfterDelete);
		List<InputDefinition> serviceInputsAfterDelete = new Gson().fromJson(getComponentInputsResponseAfterDelete.getResponse(), new TypeToken<ArrayList<InputDefinition>>(){}.getType());
		if (serviceInputsAfterDelete.size()-totalInputsBeforeAdd!=1) {
			assertTrue("Error deleting inputs from service (service should have 1 input)", false);
		}

		assertTrue("Success testing inputs main functionality", true);
	}

	/**
	 * Private method to create service with VF instance that has inputs
	 * This is private method to be used by multiple tests
	 *
	 * @return {@link org.openecomp.sdc.be.model}
	 * @throws Exception
	 * @throws IOException
	 */
	private Service createServiceWithVFInstanceWithInputs() throws Exception, IOException {
		// Create default service
		Either<Service, RestResponse> createDefaultServiceEither = AtomicOperationUtils.createDefaultService(UserRoleEnum.DESIGNER, true);
		if (createDefaultServiceEither.isRight()){
			assertTrue("Error creating default service", false);
		}
		Service service = createDefaultServiceEither.left().value();

		// Create VF from CSAR file
		Resource vfWithInputs = AtomicOperationUtils.importResourceFromCsar(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, inputCsar2);

		// Certify VF
		Pair<Component, RestResponse> changeComponentState = AtomicOperationUtils.changeComponentState(vfWithInputs, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CERTIFY, true);
		assertTrue("response code is BaseRestUtils.STATUS_CODE_SUCCESS, returned :" + changeComponentState.getRight().getErrorCode(), changeComponentState.getRight().getErrorCode() == BaseRestUtils.STATUS_CODE_SUCCESS);

		// Add VF instance to service
		Either<ComponentInstance, RestResponse> addComponentInstanceToComponentContainerEither = AtomicOperationUtils.addComponentInstanceToComponentContainer(vfWithInputs, service, UserRoleEnum.DESIGNER, true);
		if (addComponentInstanceToComponentContainerEither.isRight()){
			assertTrue("Error adding VF to service", false);
		}

		// Get service response
		ServiceReqDetails serviceDetails = new ServiceReqDetails(service);
		RestResponse getServiceResponse = ServiceRestUtils.getService(serviceDetails, ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER));
		service = ResponseParser.parseToObjectUsingMapper(getServiceResponse.getResponse(), Service.class);

		// Get VF instance from service
		ComponentInstance vfInstance = service.getComponentInstances().get(0);
		if (vfInstance!=null){
			assertTrue("Success creating service with VF instance", true);
		} else {
			assertTrue("Error creating service with VF instance", false);
		}
		return service;
	}

	/**
	 * Return default ComponentInstInputsMap
	 *
	 * @param addToInput
	 * @param inputs
	 * @return {@link org.openecomp.sdc.be.model.ComponentInstInputsMap}
	 */
	private ComponentInstInputsMap buildComponentInstInputsMap (String addToInput, List<ComponentInstancePropInput> inputs) {
		Map<String, List<ComponentInstancePropInput>> map = new HashMap<>();
		map.put(addToInput, inputs);
		ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
		componentInstInputsMap.setComponentInstanceInputsMap(map);
		return componentInstInputsMap;
	}


	@Test
	public void validateValidValueConstraintOnInputFailTest() throws Exception {
		Either<Service, RestResponse> createDefaultServiceEither = AtomicOperationUtils.createDefaultService(UserRoleEnum.DESIGNER, true);
		if (createDefaultServiceEither.isRight()){
			assertTrue("Error creating default service", false);
		}
		Service service = createDefaultServiceEither.left().value();

		String body = "{\"SubnetProp\": {\"schema\": {\"property\": {\"type\": \"\"}},\"type\": \"org.openecomp"
				+ ".datatypes.heat.network.neutron.Subnet\",\"name\": \"SubnetProp\"}}";
		RestResponse createPropertyResponse = PropertyRestUtils.createProperty(service.getUniqueId(), body,
				sdncDesignerDetails);
		AssertJUnit.assertEquals("Expected result code - 200, received - " + createPropertyResponse.getErrorCode(), 200,
				(int) createPropertyResponse.getErrorCode());

		ObjectMapper mapper = new ObjectMapper();
		mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		PropertyReqDetails propertyResponseObject = mapper.readValue(createPropertyResponse.getResponse(),
				PropertyReqDetails.class);

		PropertyDefinition input = new PropertyDefinition();
		input.setName("ipv6_address_mode");
		input.setType("string");
		input.setUniqueId("org.openecomp.datatypes.heat.network.neutron.Subnet.datatype.ipv6_address_mode");

		ComponentInstancePropInput componentInstancePropInput = new ComponentInstancePropInput();
		componentInstancePropInput.setPropertiesName("SubnetProp#ipv6_address_mode");
		componentInstancePropInput.setName("SubnetProp");
		componentInstancePropInput.setParentUniqueId(service.getUniqueId());
		componentInstancePropInput.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
		componentInstancePropInput.setUniqueId(propertyResponseObject.getUniqueId());
		componentInstancePropInput.setInput(input);

		ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
		componentInstInputsMap.setServiceProperties(Collections.singletonMap(service.getUniqueId(),
				Collections.singletonList(componentInstancePropInput)));
		RestResponse addInputResponse = InputsRestUtils.addInput(service, componentInstInputsMap,
				UserRoleEnum.DESIGNER);

		AssertJUnit.assertEquals("Expected result code - 200, received - " + addInputResponse.getErrorCode(),
				200, (int) addInputResponse.getErrorCode());

		Type constraintType = new TypeToken<PropertyConstraint>() {}.getType();
		Type inDefType = new TypeToken<List<InputDefinition>>() { }.getType();
		Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyOperation.PropertyConstraintDeserialiser()).create();
		try {
			List<InputDefinition> inputDefinitions = gson.fromJson(addInputResponse.getResponse(),
					inDefType);

			inputDefinitions.get(0).setDefaultValue("Fail");

			RestResponse updateInputResponse = InputsRestUtils.updateInput(service, mapper.writeValueAsString(inputDefinitions),
					UserRoleEnum.DESIGNER);

			AssertJUnit.assertEquals("Expected result code - 400, received - " + addInputResponse.getErrorCode(),
					400, (int) updateInputResponse.getErrorCode());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void validateValidValueConstraintOnInputSuccessTest() throws Exception {
		Either<Service, RestResponse> createDefaultServiceEither = AtomicOperationUtils.createDefaultService(UserRoleEnum.DESIGNER, true);
		if (createDefaultServiceEither.isRight()){
			assertTrue("Error creating default service", false);
		}
		Service service = createDefaultServiceEither.left().value();

		String body = "{\"SubnetProp\": {\"schema\": {\"property\": {\"type\": \"\"}},\"type\": \"org.openecomp"
				+ ".datatypes.heat.network.neutron.Subnet\",\"name\": \"SubnetProp\"}}";
		RestResponse createPropertyResponse = PropertyRestUtils.createProperty(service.getUniqueId(), body,
				sdncDesignerDetails);
		AssertJUnit.assertEquals("Expected result code - 200, received - " + createPropertyResponse.getErrorCode(), 200,
				(int) createPropertyResponse.getErrorCode());

		ObjectMapper mapper = new ObjectMapper();
		mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		PropertyReqDetails propertyResponseObject = mapper.readValue(createPropertyResponse.getResponse(),
				PropertyReqDetails.class);

		PropertyDefinition input = new PropertyDefinition();
		input.setName("ipv6_address_mode");
		input.setType("string");
		input.setUniqueId("org.openecomp.datatypes.heat.network.neutron.Subnet.datatype.ipv6_address_mode");

		ComponentInstancePropInput componentInstancePropInput = new ComponentInstancePropInput();
		componentInstancePropInput.setPropertiesName("SubnetProp#ipv6_address_mode");
		componentInstancePropInput.setName("SubnetProp");
		componentInstancePropInput.setParentUniqueId(service.getUniqueId());
		componentInstancePropInput.setType("org.openecomp.datatypes.heat.network.neutron.Subnet");
		componentInstancePropInput.setUniqueId(propertyResponseObject.getUniqueId());
		componentInstancePropInput.setInput(input);

		ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
		componentInstInputsMap.setServiceProperties(Collections.singletonMap(service.getUniqueId(),
				Collections.singletonList(componentInstancePropInput)));
		RestResponse addInputResponse = InputsRestUtils.addInput(service, componentInstInputsMap,
				UserRoleEnum.DESIGNER);

		AssertJUnit.assertEquals("Expected result code - 200, received - " + addInputResponse.getErrorCode(),
				200, (int) addInputResponse.getErrorCode());

		Type constraintType = new TypeToken<PropertyConstraint>() {}.getType();
		Type inDefType = new TypeToken<List<InputDefinition>>() { }.getType();
		Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyOperation.PropertyConstraintDeserialiser()).create();
		try {
			List<InputDefinition> inputDefinitions = gson.fromJson(addInputResponse.getResponse(),
					inDefType);

			inputDefinitions.get(0).setDefaultValue("slaac");

			RestResponse updateInputResponse = InputsRestUtils.updateInput(service, mapper.writeValueAsString(inputDefinitions),
					UserRoleEnum.DESIGNER);

			AssertJUnit.assertEquals("Expected result code - 200, received - " + addInputResponse.getErrorCode(),
					200, (int) updateInputResponse.getErrorCode());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}