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