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