Replace artifact folder ONBOARDED_PACKAGE in CSAR
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / execute / groups / GroupsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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.openecomp.sdc.ci.tests.execute.groups;
22
23 import fj.data.Either;
24 import org.apache.commons.collections.MapUtils;
25 import org.junit.Rule;
26 import org.junit.rules.TestName;
27 import org.openecomp.sdc.be.dao.api.ActionStatus;
28 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
29 import org.openecomp.sdc.be.model.*;
30 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
31 import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
32 import org.openecomp.sdc.ci.tests.datatypes.enums.ServiceCategoriesEnum;
33 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
34 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
35 import org.openecomp.sdc.ci.tests.execute.lifecycle.LCSbaseTest;
36 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
37 import org.openecomp.sdc.ci.tests.utils.rest.BaseRestUtils;
38 import org.openecomp.sdc.ci.tests.utils.rest.ComponentInstanceRestUtils;
39 import org.testng.annotations.Test;
40 import org.testng.collections.Lists;
41
42 import java.io.File;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.Optional;
46
47 import static org.junit.Assert.*;
48 import static org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils.*;
49 import static org.testng.Assert.assertNull;
50
51 public class GroupsTest extends ComponentBaseTest {
52         @Rule
53         public static TestName name = new TestName();
54         
55         public static final String groupName = "x_group";
56         public static final String capabilityName = "vlan_assignment";
57         public static final String propertyName = "vfc_instance_group_reference";
58         public static final String csarsFilePath = System.getProperty("user.dir") + File.separator + "src" + File.separator + "test" + File.separator + "resources" + File.separator + "CI" + File.separator + "csars" ;
59         
60         public GroupsTest() {
61                 super(name,  GroupsTest.class.getName());
62         }
63         
64         @Test
65         public void importResourceWithGroupsTest() throws Exception {
66                 importResource("with_groups.csar");
67         }
68         
69         @Test
70         public void importResourceWitIncorrectCapabilityNameTest() throws Exception {
71                 RestResponse createResource = getCreateResourceRestResponse(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, "incorrect_cap.csar", csarsFilePath);
72                 BaseRestUtils.checkErrorResponse(createResource, ActionStatus.MISSING_CAPABILITIES,(Lists.newArrayList("vlan_assignment1")).toString(), "group", "x_group");
73         }
74         
75         @Test
76         public void importResourceWithoutCapabilitiesTest() throws Exception {
77                 Resource resource =  importResourceFromCsar(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, "without_caps.csar", csarsFilePath);
78                 validateComponentGroupCapabilityPropertyValue(resource, null);
79         }
80         
81         @Test
82         public void updateResourceWithGroupsTest() throws Exception {
83                 Resource resource = importResource("with_groups.csar");
84                 updateResource(resource, "with_groups_update.csar");
85         }
86
87         @Test
88         public void supportGroupsWithCapabilitiesServiceLevelTest() throws Exception {
89                 Resource resource = importCertifiedResourceFromCsar(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, "VLANTaggingFromAmdox1303_2018.csar", csarsFilePath);
90         CapReqDef caps = ComponentInstanceRestUtils.getInstancesCapabilitiesRequirements(resource, UserRoleEnum.DESIGNER.getUserId());
91                 validateVlanAssignmentGroupCapabilitiesInvisible(caps.getCapabilities());
92
93         Service service1 = createCertifiedServiceWithInstance(ServiceCategoriesEnum.MOBILITY, resource);
94         caps = ComponentInstanceRestUtils.getInstancesCapabilitiesRequirements(service1, UserRoleEnum.DESIGNER.getUserId());
95                 validateVlanAssignmentGroupCapabilitiesVisible(caps.getCapabilities());
96
97         Service service2 = createCertifiedServiceWithInstance(ServiceCategoriesEnum.NETWORK_L4, resource);
98         caps = ComponentInstanceRestUtils.getInstancesCapabilitiesRequirements(service2, UserRoleEnum.DESIGNER.getUserId());
99                 validateVlanAssignmentGroupCapabilitiesVisible(caps.getCapabilities());
100
101         Service serviceContainer = createCertifiedServiceWithProxyInstances(ServiceCategoriesEnum.NETWORK_L3, service1, service2);
102         caps = ComponentInstanceRestUtils.getInstancesCapabilitiesRequirements(serviceContainer, UserRoleEnum.DESIGNER.getUserId());
103                 validateVlanAssignmentGroupCapabilitiesVisible(caps.getCapabilities());
104
105         assertTrue(serviceContainer.getComponentInstances()!=null);
106     }
107
108         private void validateVlanAssignmentGroupCapabilitiesVisible(Map<String, List<CapabilityDefinition>> capabilities) {
109                 if(MapUtils.isNotEmpty(capabilities)){
110                         validateVlanAssignmentGroupCapabilitiesVisibility(capabilities, true);
111                 }
112         }
113
114         private void validateVlanAssignmentGroupCapabilitiesInvisible(Map<String, List<CapabilityDefinition>> capabilities) {
115                 if(MapUtils.isNotEmpty(capabilities)){
116                         validateVlanAssignmentGroupCapabilitiesVisibility(capabilities, false);
117                 }
118         }
119     private void validateVlanAssignmentGroupCapabilitiesVisibility(Map<String, List<CapabilityDefinition>> capabilities, boolean shouldBeVisible) {
120                 assertTrue(capabilities.containsKey("org.openecomp.capabilities.VLANAssignment") == shouldBeVisible);
121     }
122
123     private Service createCertifiedServiceWithProxyInstances(ServiceCategoriesEnum category, Service service1, Service service2) throws Exception {
124         Either<Service, RestResponse> createServiceRes = createServiceByCategory(category, UserRoleEnum.DESIGNER, true);
125         assertTrue(createServiceRes.isLeft());
126         Either<ComponentInstance, RestResponse> result = addComponentInstanceToComponentContainer(service1, createServiceRes.left().value());
127         assertTrue(result.isLeft());
128         result = addComponentInstanceToComponentContainer(service2, createServiceRes.left().value());
129         assertTrue(result.isLeft());
130         return certifyService(createServiceRes);
131     }
132
133     private Service createCertifiedServiceWithInstance(ServiceCategoriesEnum category, Resource resource) throws Exception {
134         Either<Service, RestResponse> createServiceRes = createServiceByCategory(category, UserRoleEnum.DESIGNER, true);
135         assertTrue(createServiceRes.isLeft());
136         Either<ComponentInstance, RestResponse> result = addComponentInstanceToComponentContainer(resource, createServiceRes.left().value());
137         assertTrue(result.isLeft());
138         return certifyService(createServiceRes);
139     }
140
141     private Service certifyService(Either<Service, RestResponse> serviceProxy1) throws Exception {
142         Service service = getServiceObjectByNameAndVersion(UserRoleEnum.DESIGNER, serviceProxy1.left().value().getName(), "0.1" );
143         assertNotNull(service);
144         ServiceReqDetails serviceReqDetails = new ServiceReqDetails(service);
145         RestResponse restResponseService = LCSbaseTest.certifyService(serviceReqDetails, ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER));
146         assertTrue(restResponseService.getErrorCode()==200);
147         return getServiceObjectByNameAndVersion(UserRoleEnum.DESIGNER, serviceProxy1.left().value().getName(), "1.0" );
148     }
149
150     private static Resource updateResource(Resource resource, String csarFileName) throws Exception {
151                 Resource updatedResource = updateResourceFromCsar(resource, UserRoleEnum.DESIGNER, csarFileName, csarsFilePath);
152                 validateComponentGroupCapabilityPropertyValue(updatedResource, "new_value");
153                 return updatedResource;
154         }
155
156         private static Resource importResource(String csarFileName) throws Exception {
157                 Resource resource = importResourceFromCsar(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, csarFileName, csarsFilePath);
158                 validateComponentGroupCapabilityPropertyValue(resource, "success");
159                 return resource;
160         }
161
162         private static void validateComponentGroupCapabilityPropertyValue(Component component, String propertyValue) {
163                 assertNotNull(component);
164                 assertNotNull(component.getGroups());
165                 assertFalse(component.getGroups().isEmpty());
166                 assertTrue(component.getGroups().size() == 5);
167                 Optional<GroupDefinition> vfcInstanceGroup = component.getGroups().stream().filter(g->g.getName().equals(groupName)).findFirst();
168                 assertTrue(vfcInstanceGroup.isPresent());
169                 assertFalse(vfcInstanceGroup.get().getCapabilities().isEmpty());
170                 assertTrue(vfcInstanceGroup.get().getCapabilities().size() == 1);
171                 assertNotNull(vfcInstanceGroup.get().getCapabilities().get("org.openecomp.capabilities.VLANAssignment"));
172                 assertTrue(vfcInstanceGroup.get().getCapabilities().get("org.openecomp.capabilities.VLANAssignment").size() == 1);
173                 assertNotNull(vfcInstanceGroup.get().getCapabilities().get("org.openecomp.capabilities.VLANAssignment").get(0));
174                 assertTrue(vfcInstanceGroup.get().getCapabilities().get("org.openecomp.capabilities.VLANAssignment").get(0).getName().equals(capabilityName));
175                 assertNotNull(vfcInstanceGroup.get().getCapabilities().get("org.openecomp.capabilities.VLANAssignment").get(0).getProperties());
176                 assertTrue(vfcInstanceGroup.get().getCapabilities().get("org.openecomp.capabilities.VLANAssignment").get(0).getProperties().size() == 1);
177                 assertTrue(vfcInstanceGroup.get().getCapabilities().get("org.openecomp.capabilities.VLANAssignment").get(0).getProperties().get(0).getName().equals(propertyName));
178                 if(propertyValue == null)
179                         assertNull(vfcInstanceGroup.get().getCapabilities().get("org.openecomp.capabilities.VLANAssignment").get(0).getProperties().get(0).getValue());
180                 else
181                         assertTrue(vfcInstanceGroup.get().getCapabilities().get("org.openecomp.capabilities.VLANAssignment").get(0).getProperties().get(0).getValue().equals(propertyValue));
182         }
183         
184 }