aboutsummaryrefslogtreecommitdiffstats
path: root/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/execute/groups/GroupsTest.java
blob: 91f564993ef5ef2648ee71b57378633602f55249 (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
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2019 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.groups;

import fj.data.Either;
import org.apache.commons.collections.MapUtils;
import org.junit.Rule;
import org.junit.rules.TestName;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
import org.openecomp.sdc.be.model.*;
import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
import org.openecomp.sdc.ci.tests.datatypes.enums.ServiceCategoriesEnum;
import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
import org.openecomp.sdc.ci.tests.execute.lifecycle.LCSbaseTest;
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.testng.annotations.Test;
import org.testng.collections.Lists;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static org.junit.Assert.*;
import static org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils.*;
import static org.testng.Assert.assertNull;

public class GroupsTest extends ComponentBaseTest {
	@Rule
	public static TestName name = new TestName();
	
	public static final String groupName = "x_group";
	public static final String capabilityName = "vlan_assignment";
	public static final String propertyName = "vfc_instance_group_reference";
	public static final String csarsFilePath = System.getProperty("user.dir") + File.separator + "src" + File.separator + "test" + File.separator + "resources" + File.separator + "CI" + File.separator + "csars" ;
	
	public GroupsTest() {
		super(name,  GroupsTest.class.getName());
	}
	
	@Test
	public void importResourceWithGroupsTest() throws Exception {
		importResource("with_groups.csar");
	}
	
	@Test
	public void importResourceWitIncorrectCapabilityNameTest() throws Exception {
		RestResponse createResource = getCreateResourceRestResponse(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, "incorrect_cap.csar", csarsFilePath);
		BaseRestUtils.checkErrorResponse(createResource, ActionStatus.MISSING_CAPABILITIES,(Lists.newArrayList("vlan_assignment1")).toString(), "group", "x_group");
	}
	
	@Test
	public void importResourceWithoutCapabilitiesTest() throws Exception {
		Resource resource =  importResourceFromCsar(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, "without_caps.csar", csarsFilePath);
		validateComponentGroupCapabilityPropertyValue(resource, null);
	}
	
	@Test
	public void updateResourceWithGroupsTest() throws Exception {
		Resource resource = importResource("with_groups.csar");
		updateResource(resource, "with_groups_update.csar");
	}

	@Test
	public void supportGroupsWithCapabilitiesServiceLevelTest() throws Exception {
		Resource resource = importCertifiedResourceFromCsar(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, "VLANTaggingFromAmdox1303_2018.csar", csarsFilePath);
        CapReqDef caps = ComponentInstanceRestUtils.getInstancesCapabilitiesRequirements(resource, UserRoleEnum.DESIGNER.getUserId());
		validateVlanAssignmentGroupCapabilitiesInvisible(caps.getCapabilities());

        Service service1 = createCertifiedServiceWithInstance(ServiceCategoriesEnum.MOBILITY, resource);
        caps = ComponentInstanceRestUtils.getInstancesCapabilitiesRequirements(service1, UserRoleEnum.DESIGNER.getUserId());
		validateVlanAssignmentGroupCapabilitiesVisible(caps.getCapabilities());

        Service service2 = createCertifiedServiceWithInstance(ServiceCategoriesEnum.NETWORK_L4, resource);
        caps = ComponentInstanceRestUtils.getInstancesCapabilitiesRequirements(service2, UserRoleEnum.DESIGNER.getUserId());
		validateVlanAssignmentGroupCapabilitiesVisible(caps.getCapabilities());

        Service serviceContainer = createCertifiedServiceWithProxyInstances(ServiceCategoriesEnum.NETWORK_L3, service1, service2);
        caps = ComponentInstanceRestUtils.getInstancesCapabilitiesRequirements(serviceContainer, UserRoleEnum.DESIGNER.getUserId());
		validateVlanAssignmentGroupCapabilitiesVisible(caps.getCapabilities());

        assertTrue(serviceContainer.getComponentInstances()!=null);
    }

	private void validateVlanAssignmentGroupCapabilitiesVisible(Map<String, List<CapabilityDefinition>> capabilities) {
		if(MapUtils.isNotEmpty(capabilities)){
			validateVlanAssignmentGroupCapabilitiesVisibility(capabilities, true);
		}
	}

	private void validateVlanAssignmentGroupCapabilitiesInvisible(Map<String, List<CapabilityDefinition>> capabilities) {
		if(MapUtils.isNotEmpty(capabilities)){
			validateVlanAssignmentGroupCapabilitiesVisibility(capabilities, false);
		}
	}
    private void validateVlanAssignmentGroupCapabilitiesVisibility(Map<String, List<CapabilityDefinition>> capabilities, boolean shouldBeVisible) {
		assertTrue(capabilities.containsKey("org.openecomp.capabilities.VLANAssignment") == shouldBeVisible);
    }

    private Service createCertifiedServiceWithProxyInstances(ServiceCategoriesEnum category, Service service1, Service service2) throws Exception {
        Either<Service, RestResponse> createServiceRes = createServiceByCategory(category, UserRoleEnum.DESIGNER, true);
        assertTrue(createServiceRes.isLeft());
        Either<ComponentInstance, RestResponse> result = addComponentInstanceToComponentContainer(service1, createServiceRes.left().value());
        assertTrue(result.isLeft());
        result = addComponentInstanceToComponentContainer(service2, createServiceRes.left().value());
        assertTrue(result.isLeft());
        return certifyService(createServiceRes);
    }

    private Service createCertifiedServiceWithInstance(ServiceCategoriesEnum category, Resource resource) throws Exception {
        Either<Service, RestResponse> createServiceRes = createServiceByCategory(category, UserRoleEnum.DESIGNER, true);
        assertTrue(createServiceRes.isLeft());
        Either<ComponentInstance, RestResponse> result = addComponentInstanceToComponentContainer(resource, createServiceRes.left().value());
        assertTrue(result.isLeft());
        return certifyService(createServiceRes);
    }

    private Service certifyService(Either<Service, RestResponse> serviceProxy1) throws Exception {
        Service service = getServiceObjectByNameAndVersion(UserRoleEnum.DESIGNER, serviceProxy1.left().value().getName(), "0.1" );
        assertNotNull(service);
        ServiceReqDetails serviceReqDetails = new ServiceReqDetails(service);
        RestResponse restResponseService = LCSbaseTest.certifyService(serviceReqDetails, ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER));
        assertTrue(restResponseService.getErrorCode()==200);
        return getServiceObjectByNameAndVersion(UserRoleEnum.DESIGNER, serviceProxy1.left().value().getName(), "1.0" );
    }

    private static Resource updateResource(Resource resource, String csarFileName) throws Exception {
		Resource updatedResource = updateResourceFromCsar(resource, UserRoleEnum.DESIGNER, csarFileName, csarsFilePath);
		validateComponentGroupCapabilityPropertyValue(updatedResource, "new_value");
		return updatedResource;
	}

	private static Resource importResource(String csarFileName) throws Exception {
		Resource resource = importResourceFromCsar(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, csarFileName, csarsFilePath);
		validateComponentGroupCapabilityPropertyValue(resource, "success");
		return resource;
	}

	private static void validateComponentGroupCapabilityPropertyValue(Component component, String propertyValue) {
		assertNotNull(component);
		assertNotNull(component.getGroups());
		assertFalse(component.getGroups().isEmpty());
		assertTrue(component.getGroups().size() == 5);
		Optional<GroupDefinition> vfcInstanceGroup = component.getGroups().stream().filter(g->g.getName().equals(groupName)).findFirst();
		assertTrue(vfcInstanceGroup.isPresent());
		assertFalse(vfcInstanceGroup.get().getCapabilities().isEmpty());
		assertTrue(vfcInstanceGroup.get().getCapabilities().size() == 1);
		assertNotNull(vfcInstanceGroup.get().getCapabilities().get("org.openecomp.capabilities.VLANAssignment"));
		assertTrue(vfcInstanceGroup.get().getCapabilities().get("org.openecomp.capabilities.VLANAssignment").size() == 1);
		assertNotNull(vfcInstanceGroup.get().getCapabilities().get("org.openecomp.capabilities.VLANAssignment").get(0));
		assertTrue(vfcInstanceGroup.get().getCapabilities().get("org.openecomp.capabilities.VLANAssignment").get(0).getName().equals(capabilityName));
		assertNotNull(vfcInstanceGroup.get().getCapabilities().get("org.openecomp.capabilities.VLANAssignment").get(0).getProperties());
		assertTrue(vfcInstanceGroup.get().getCapabilities().get("org.openecomp.capabilities.VLANAssignment").get(0).getProperties().size() == 1);
		assertTrue(vfcInstanceGroup.get().getCapabilities().get("org.openecomp.capabilities.VLANAssignment").get(0).getProperties().get(0).getName().equals(propertyName));
		if(propertyValue == null)
			assertNull(vfcInstanceGroup.get().getCapabilities().get("org.openecomp.capabilities.VLANAssignment").get(0).getProperties().get(0).getValue());
		else
			assertTrue(vfcInstanceGroup.get().getCapabilities().get("org.openecomp.capabilities.VLANAssignment").get(0).getProperties().get(0).getValue().equals(propertyValue));
	}
	
}