2 * ============LICENSE_START=======================================================
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
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.internalApis.annotations;
23 import com.google.common.collect.ImmutableSet;
24 import com.google.common.collect.Maps;
25 import org.apache.commons.lang3.tuple.Pair;
26 import org.junit.Rule;
27 import org.junit.rules.TestName;
28 import org.openecomp.sdc.be.dao.utils.MapUtil;
29 import org.openecomp.sdc.be.datatypes.elements.Annotation;
30 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
31 import org.openecomp.sdc.be.model.*;
32 import org.openecomp.sdc.be.model.utils.ComponentUtilities;
33 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
34 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
35 import org.openecomp.sdc.ci.tests.datatypes.enums.LifeCycleStatesEnum;
36 import org.openecomp.sdc.ci.tests.datatypes.enums.NormativeTypesEnum;
37 import org.openecomp.sdc.ci.tests.datatypes.enums.ResourceCategoryEnum;
38 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
39 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
40 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaAnnotationsTypesDefinition;
41 import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
42 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
43 import org.openecomp.sdc.ci.tests.utils.general.FileHandling;
44 import org.openecomp.sdc.ci.tests.utils.rest.PropertyRestUtils;
45 import org.testng.annotations.Test;
47 import java.util.List;
50 import java.util.stream.Collectors;
52 import static java.util.stream.Collectors.groupingBy;
53 import static java.util.stream.Collectors.toMap;
54 import static org.assertj.core.api.Assertions.assertThat;
55 import static org.junit.Assert.assertTrue;
56 import static org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaAnnotationsTypesDefinition.SOURCE_ANNOTATION;
57 import static org.openecomp.sdc.ci.tests.utils.ToscaTypesDefinitionUtils.getToscaAnnotationsFromCsar;
58 import static org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils.*;
59 import static org.openecomp.sdc.ci.tests.utils.general.ElementFactory.getDefaultUser;
60 import static org.openecomp.sdc.ci.tests.utils.rest.InputsRestUtils.deleteInputFromComponent;
61 import static org.openecomp.sdc.ci.tests.utils.validation.BaseValidationUtils.checkSuccess;
62 import static org.springframework.util.CollectionUtils.isEmpty;
64 public class AnnotationsTest extends ComponentBaseTest{
66 private static final String PCM_FLAVOR_NAME = "pcm_flavor_name";
67 private static final String AVAILABILITY_ZONE = "availabilityzone_name";
68 private static final String NET_NAME = "net_name";
69 private static final String NF_NAMING_CODE = "nf_naming_code";
70 private static final String [] PROPS_TO_DECLARE = new String [] {PCM_FLAVOR_NAME, AVAILABILITY_ZONE, NET_NAME, NF_NAMING_CODE};
71 private static final String CSAR_WITH_ANNOTATIONS_V1 = "SIROV_annotations_VSP.csar";
72 private static final String CSAR_WITH_ANNOTATIONS_V2 = "SIROV_annotations_VSP_V2.csar";
73 private static final String SRIOV_PATH = FileHandling.getFilePath("SRIOV");
76 public static TestName name = new TestName();
78 public AnnotationsTest() {
79 super(name, AnnotationsTest.class.getName());
83 public void whenExportingToscaOfTopologyTemplate_annotationTypeYamlExist_sourceAnnotationExist() throws Exception {
84 User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
85 ResourceReqDetails resourceDetails = ElementFactory.getDefaultResourceByType("exportToscaAnnotationsYml", NormativeTypesEnum.ROOT, ResourceCategoryEnum.GENERIC_INFRASTRUCTURE, sdncModifierDetails.getUserId(), ResourceTypeEnum.VF.toString());
86 Resource createdVF = AtomicOperationUtils.createResourceByResourceDetails(resourceDetails, UserRoleEnum.DESIGNER, true).left().value();
87 ToscaAnnotationsTypesDefinition toscaAnnotations = getToscaAnnotationsFromCsar(createdVF, sdncModifierDetails);
88 assertTrue(toscaAnnotations.getAnnotation_types().containsKey(SOURCE_ANNOTATION));
92 public void whenDeclaringAnInputFromPropertyWhichOriginatedFromInputWithAnnotation_copyAnnotationsToNewInput() throws Exception {
93 Resource vfWithAnnotationsV1 = importAnnotationsCsarAndCheckIn();
94 Service service = createDefaultService(UserRoleEnum.DESIGNER, true).left().value();
95 ComponentInstance createdCmptInstance = addComponentInstanceToComponentContainer(vfWithAnnotationsV1, service).left().value();
96 Service fetchedService = getServiceObject(service.getUniqueId());
97 List<ComponentInstanceInput> declaredProps = declareProperties(fetchedService, createdCmptInstance, PROPS_TO_DECLARE);
98 verifyAnnotationsOnDeclaredInputs(vfWithAnnotationsV1, fetchedService, declaredProps);
99 Service serviceAfterPropertyDeclaration = getServiceObject(service.getUniqueId());
100 deleteDeclaredInputsAndVerifySuccess(serviceAfterPropertyDeclaration);
104 public void onChangeVersion_copyAnnotationsFromNewVspToServiceInputs() throws Exception {
105 Resource vfWithAnnotationsV1 = importAnnotationsCsarAndCheckIn();
106 Service service = createDefaultService(UserRoleEnum.DESIGNER, true).left().value();
107 ComponentInstance createdCmptInstance = addComponentInstanceToComponentContainer(vfWithAnnotationsV1, service).left().value();
108 Service fetchedService = getServiceObject(service.getUniqueId());
109 declareProperties(fetchedService, createdCmptInstance, PROPS_TO_DECLARE);
111 Resource vfWithAnnotationsV2 = updateAnnotationsCsarAndCheckIn(vfWithAnnotationsV1);
112 Pair<Component, ComponentInstance> changeVersionRes = changeComponentInstanceVersion(service, createdCmptInstance, vfWithAnnotationsV2, UserRoleEnum.DESIGNER, true).left().value();
113 Component serviceAfterChangeVersion = changeVersionRes.getKey();
114 ComponentInstance newInstance = changeVersionRes.getRight();
115 List<ComponentInstanceInput> declaredProps = getInstanceProperties(serviceAfterChangeVersion, newInstance.getUniqueId(), PROPS_TO_DECLARE);
116 verifyAnnotationsOnDeclaredInputs(vfWithAnnotationsV2, serviceAfterChangeVersion, declaredProps);
120 private void verifyAnnotationsOnDeclaredInputs(Resource vfWithAnnotations, Component fetchedService, List<ComponentInstanceInput> declaredProps) throws Exception {
121 Map<String, InputDefinition> serviceLevelInputsByProperty = getCreatedInputsByProperty(fetchedService, declaredProps);
122 Map<String, List<Annotation>> annotationsFromVfLevelInputs = getAnnotationsByInputName(vfWithAnnotations);
123 verifyInputsAnnotation(serviceLevelInputsByProperty, declaredProps, annotationsFromVfLevelInputs);
126 private Map<String, List<Annotation>> getAnnotationsByInputName(Resource resource) {
127 return resource.getInputs()
129 .collect(toMap(InputDefinition::getName, input -> ComponentUtilities.getInputAnnotations(resource, input.getName())));
132 private void deleteDeclaredInputsAndVerifySuccess(Component service) throws Exception {
133 for (InputDefinition declaredInput : service.getInputs()) {
134 RestResponse deleteInputResponse = deleteInputFromComponent(service, declaredInput.getUniqueId());
135 checkSuccess(deleteInputResponse);
137 Service fetchedService = getServiceObject(service.getUniqueId());
138 assertThat(fetchedService.getInputs()).isNullOrEmpty();
141 private void verifyInputsAnnotation(Map<String, InputDefinition> inputsByProperty, List<ComponentInstanceInput> declaredProps, Map<String, List<Annotation>> expectedAnnotationsByInput) {
142 Map<String, ComponentInstanceInput> propsByName = MapUtil.toMap(declaredProps, ComponentInstanceInput::getName);
144 InputDefinition declaredFromPcmFlavourName = findInputDeclaredFromProperty(PCM_FLAVOR_NAME, inputsByProperty, propsByName);
145 verifyInputAnnotations(declaredFromPcmFlavourName, expectedAnnotationsByInput.get(PCM_FLAVOR_NAME));
147 InputDefinition declaredFromAvailabilityZone = findInputDeclaredFromProperty(AVAILABILITY_ZONE, inputsByProperty, propsByName);
148 verifyInputAnnotations(declaredFromAvailabilityZone, expectedAnnotationsByInput.get(AVAILABILITY_ZONE));
150 InputDefinition declaredFromNetName = findInputDeclaredFromProperty(NET_NAME, inputsByProperty, propsByName);
151 verifyInputAnnotations(declaredFromNetName, expectedAnnotationsByInput.get(NET_NAME));
153 InputDefinition declaredFromNFNamingCode = findInputDeclaredFromProperty(NF_NAMING_CODE, inputsByProperty, propsByName);
154 verifyInputHasNoAnnotations(declaredFromNFNamingCode);
157 private void verifyInputHasNoAnnotations(InputDefinition inputWithoutAnnotation) {
158 assertThat(inputWithoutAnnotation.getAnnotations()).isNullOrEmpty();
161 private InputDefinition findInputDeclaredFromProperty(String propName, Map<String, InputDefinition> inputsByProperty, Map<String, ComponentInstanceInput> propsByName) {
162 String propId = propsByName.get(propName).getUniqueId();
163 return inputsByProperty.get(propId);
166 private Map<String, InputDefinition> getCreatedInputsByProperty(Component service, List<ComponentInstanceInput> declaredProps) throws Exception {
167 Service fetchedService = getServiceObject(service.getUniqueId());
168 List<InputDefinition> inputs = fetchedService.getInputs();
169 return filterInputsCreatedByDeclaringFromProperties(declaredProps, inputs);
172 private Map<String, InputDefinition> filterInputsCreatedByDeclaringFromProperties(List<ComponentInstanceInput> declaredProps, List<InputDefinition> inputs) {
173 List<String> declaredPropsIds = declaredProps.stream().map(ComponentInstanceInput::getUniqueId).collect(Collectors.toList());
174 Map<String, InputDefinition> inputsByPropertyId = MapUtil.toMap(inputs, InputDefinition::getPropertyId);
175 return Maps.filterKeys(inputsByPropertyId, declaredPropsIds::contains);
178 private Resource importAnnotationsCsarAndCheckIn() throws Exception {
179 Resource annotationsVF = importResourceFromCsar(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, CSAR_WITH_ANNOTATIONS_V1, SRIOV_PATH);
180 changeComponentState(annotationsVF, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CHECKIN, true);
181 return annotationsVF;
184 private Resource updateAnnotationsCsarAndCheckIn(Resource vfToUpdate) throws Exception {
185 changeComponentState(vfToUpdate, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CHECKOUT, true);
186 Resource annotationsVfV2 = updateResourceFromCsar(vfToUpdate, UserRoleEnum.DESIGNER, CSAR_WITH_ANNOTATIONS_V2, SRIOV_PATH);
187 changeComponentState(annotationsVfV2, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CHECKIN, true);
188 return annotationsVfV2;
191 private List<ComponentInstanceInput> declareProperties(Service service, ComponentInstance instance, String ... propertiesToDeclareNames) throws Exception {
192 Map<String, List<ComponentInstanceInput>> propertiesToDeclare = getServiceInstancesProps(service, instance.getUniqueId(), propertiesToDeclareNames);
193 RestResponse restResponse = PropertyRestUtils.declareProporties(service, propertiesToDeclare, getDefaultUser(UserRoleEnum.DESIGNER));
194 checkSuccess(restResponse);
195 return propertiesToDeclare.get(instance.getUniqueId());
198 private List<ComponentInstanceInput> getInstanceProperties(Component service, String ofInstance, String ... propsNames) {
199 return getServiceInstancesProps(service, ofInstance, propsNames).get(ofInstance);
202 private void verifyInputAnnotations(InputDefinition input, List<Annotation> expectedAnnotations) {
203 if (isEmpty(expectedAnnotations)) {
204 assertThat(input.getAnnotations()).isNullOrEmpty();
207 assertThat(input.getAnnotations())
208 .usingElementComparatorOnFields("type", "name", "properties")
210 .containsExactlyElementsOf(expectedAnnotations);
213 private Map<String, List<ComponentInstanceInput>> getServiceInstancesProps(Component fromService, String ofInstance, String ... propsToDeclareNames) {
214 Set<String> propsToDeclare = ImmutableSet.<String>builder().add(propsToDeclareNames).build();
215 List<ComponentInstanceInput> componentInstancesInputs = fromService.getComponentInstancesInputs().get(ofInstance);
216 return componentInstancesInputs.stream()
217 .filter(insInput -> propsToDeclare.contains(insInput.getName()))
218 .collect(groupingBy((i) -> ofInstance));