Support for defining attributes on a node_type
[sdc.git] / integration-tests / src / test / java / org / onap / sdc / backend / ci / tests / execute / attribute / ComponentInstanceAttributeTest.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.onap.sdc.backend.ci.tests.execute.attribute;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.openecomp.sdc.common.datastructure.FunctionalInterfaces.swallowException;
25
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28 import java.io.File;
29 import java.util.function.Function;
30 import org.junit.Rule;
31 import org.junit.rules.TestName;
32 import org.onap.sdc.backend.ci.tests.api.ComponentBaseTest;
33 import org.onap.sdc.backend.ci.tests.api.Urls;
34 import org.onap.sdc.backend.ci.tests.datatypes.enums.LifeCycleStatesEnum;
35 import org.onap.sdc.backend.ci.tests.datatypes.enums.UserRoleEnum;
36 import org.onap.sdc.backend.ci.tests.utils.general.AtomicOperationUtils;
37 import org.onap.sdc.backend.ci.tests.utils.rest.BaseRestUtils;
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.ComponentInstance;
41 import org.openecomp.sdc.be.model.ComponentInstanceAttribute;
42 import org.openecomp.sdc.be.model.Resource;
43 import org.testng.annotations.Test;
44
45 public class ComponentInstanceAttributeTest extends ComponentBaseTest {
46
47         public static Gson gson = new GsonBuilder().setPrettyPrinting().create();
48
49         @Rule
50         public static TestName name = new TestName();
51
52         @Test
53         public void testUpdateAttributeOnResourceInstance() {
54                 // Prepare VF with vfc instance with Attributes
55                 String testResourcesPath = config.getResourceConfigDir() + File.separator + "importToscaResourceByCreateUrl";
56                 final Resource vfcWithAttributes = AtomicOperationUtils
57                         .importResource(testResourcesPath, "CPWithAttributes.yml").left().value();
58                 swallowException(() -> AtomicOperationUtils.changeComponentState(vfcWithAttributes, UserRoleEnum.DESIGNER,
59                         LifeCycleStatesEnum.CHECKIN, false));
60                 Resource vf = AtomicOperationUtils.createResourceByType(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, false)
61                         .left().value();
62                 ComponentInstance vfcInstance = AtomicOperationUtils
63                         .addComponentInstanceToComponentContainer(vfcWithAttributes, vf).left().value();
64
65                 // util method to get the specific attribute from the vf
66                 Function<Resource, ComponentInstanceAttribute> attributeGetter = resourceVf -> resourceVf
67                         .getComponentInstancesAttributes().values().iterator().next().stream()
68                         .filter(att -> att.getName().equals("private_address")).findAny().get();
69                 // update attribute on vfc instance
70                 final Resource vfWithInsatncePreUpdate = swallowException(
71                         () -> (Resource) AtomicOperationUtils.getComponentObject(vf, UserRoleEnum.DESIGNER));
72                 ComponentInstanceAttribute attributeOfRI = attributeGetter.apply(vfWithInsatncePreUpdate);
73                 final String newAttValue = "NewValue";
74                 attributeOfRI.setValue(newAttValue);
75                 String body = gson.toJson(attributeOfRI);
76                 String url = String.format(Urls.UPDATE_ATTRIBUTE_ON_RESOURCE_INSTANCE, config.getCatalogBeHost(),
77                         config.getCatalogBePort(), ComponentTypeEnum.findParamByType(ComponentTypeEnum.RESOURCE),
78                         vf.getUniqueId(), vfcInstance.getUniqueId());
79                 swallowException(() -> BaseRestUtils.sendPost(url, body, UserRoleEnum.DESIGNER.getUserId(),
80                         BaseRestUtils.acceptHeaderData));
81                 // Retrieve updated vf and verify attribute was updated
82                 final Resource vfWithInsatncePostUpdate = swallowException(
83                         () -> (Resource) AtomicOperationUtils.getComponentObject(vf, UserRoleEnum.DESIGNER));
84                 ComponentInstanceAttribute updatedAttribute = attributeGetter.apply(vfWithInsatncePostUpdate);
85                 assertEquals(updatedAttribute.getValue(), newAttValue);
86
87         }
88 }