re base code
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / internalApis / annotations / AnnotationsTest.java
1 package org.openecomp.sdc.internalApis.annotations;
2
3 import com.google.common.collect.ImmutableSet;
4 import com.google.common.collect.Maps;
5 import org.apache.commons.lang3.tuple.Pair;
6 import org.junit.Rule;
7 import org.junit.rules.TestName;
8 import org.openecomp.sdc.be.dao.utils.MapUtil;
9 import org.openecomp.sdc.be.datatypes.elements.Annotation;
10 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
11 import org.openecomp.sdc.be.model.*;
12 import org.openecomp.sdc.be.model.utils.ComponentUtilities;
13 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
14 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
15 import org.openecomp.sdc.ci.tests.datatypes.enums.LifeCycleStatesEnum;
16 import org.openecomp.sdc.ci.tests.datatypes.enums.NormativeTypesEnum;
17 import org.openecomp.sdc.ci.tests.datatypes.enums.ResourceCategoryEnum;
18 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
19 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
20 import org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaAnnotationsTypesDefinition;
21 import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
22 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
23 import org.openecomp.sdc.ci.tests.utils.general.FileHandling;
24 import org.openecomp.sdc.ci.tests.utils.rest.PropertyRestUtils;
25 import org.testng.annotations.Test;
26
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30 import java.util.stream.Collectors;
31
32 import static java.util.stream.Collectors.groupingBy;
33 import static java.util.stream.Collectors.toMap;
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.junit.Assert.assertTrue;
36 import static org.openecomp.sdc.ci.tests.tosca.datatypes.ToscaAnnotationsTypesDefinition.SOURCE_ANNOTATION;
37 import static org.openecomp.sdc.ci.tests.utils.ToscaTypesDefinitionUtils.getToscaAnnotationsFromCsar;
38 import static org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils.*;
39 import static org.openecomp.sdc.ci.tests.utils.general.ElementFactory.getDefaultUser;
40 import static org.openecomp.sdc.ci.tests.utils.rest.InputsRestUtils.deleteInputFromComponent;
41 import static org.openecomp.sdc.ci.tests.utils.validation.BaseValidationUtils.checkSuccess;
42 import static org.springframework.util.CollectionUtils.isEmpty;
43
44 public class AnnotationsTest extends ComponentBaseTest{
45
46     private static final String PCM_FLAVOR_NAME = "pcm_flavor_name";
47     private static final String AVAILABILITY_ZONE = "availabilityzone_name";
48     private static final String NET_NAME = "net_name";
49     private static final String NF_NAMING_CODE = "nf_naming_code";
50     private static final String [] PROPS_TO_DECLARE = new String [] {PCM_FLAVOR_NAME, AVAILABILITY_ZONE, NET_NAME, NF_NAMING_CODE};
51     private static final String CSAR_WITH_ANNOTATIONS_V1 = "SIROV_annotations_VSP.csar";
52     private static final String CSAR_WITH_ANNOTATIONS_V2 = "SIROV_annotations_VSP_V2.csar";
53     private static final String SRIOV_PATH = FileHandling.getFilePath("SRIOV");
54
55     @Rule
56     public static TestName name = new TestName();
57
58     public AnnotationsTest() {
59         super(name, AnnotationsTest.class.getName());
60     }
61
62     @Test
63     public void whenExportingToscaOfTopologyTemplate_annotationTypeYamlExist_sourceAnnotationExist() throws Exception {
64         User sdncModifierDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
65         ResourceReqDetails resourceDetails = ElementFactory.getDefaultResourceByType("exportToscaAnnotationsYml", NormativeTypesEnum.ROOT, ResourceCategoryEnum.GENERIC_INFRASTRUCTURE, sdncModifierDetails.getUserId(), ResourceTypeEnum.VF.toString());
66         Resource createdVF = AtomicOperationUtils.createResourceByResourceDetails(resourceDetails, UserRoleEnum.DESIGNER, true).left().value();
67         ToscaAnnotationsTypesDefinition toscaAnnotations = getToscaAnnotationsFromCsar(createdVF, sdncModifierDetails);
68         assertTrue(toscaAnnotations.getAnnotation_types().containsKey(SOURCE_ANNOTATION));
69     }
70
71     @Test
72     public void whenDeclaringAnInputFromPropertyWhichOriginatedFromInputWithAnnotation_copyAnnotationsToNewInput() throws Exception {
73         Resource vfWithAnnotationsV1 = importAnnotationsCsarAndCheckIn();
74         Service service = createDefaultService(UserRoleEnum.DESIGNER, true).left().value();
75         ComponentInstance createdCmptInstance = addComponentInstanceToComponentContainer(vfWithAnnotationsV1, service).left().value();
76         Service fetchedService = getServiceObject(service.getUniqueId());
77         List<ComponentInstanceInput> declaredProps = declareProperties(fetchedService, createdCmptInstance, PROPS_TO_DECLARE);
78         verifyAnnotationsOnDeclaredInputs(vfWithAnnotationsV1, fetchedService, declaredProps);
79         Service serviceAfterPropertyDeclaration = getServiceObject(service.getUniqueId());
80         deleteDeclaredInputsAndVerifySuccess(serviceAfterPropertyDeclaration);
81     }
82
83     @Test
84     public void onChangeVersion_copyAnnotationsFromNewVspToServiceInputs() throws Exception {
85         Resource vfWithAnnotationsV1 = importAnnotationsCsarAndCheckIn();
86         Service service = createDefaultService(UserRoleEnum.DESIGNER, true).left().value();
87         ComponentInstance createdCmptInstance = addComponentInstanceToComponentContainer(vfWithAnnotationsV1, service).left().value();
88         Service fetchedService = getServiceObject(service.getUniqueId());
89         declareProperties(fetchedService, createdCmptInstance, PROPS_TO_DECLARE);
90
91         Resource vfWithAnnotationsV2 = updateAnnotationsCsarAndCheckIn(vfWithAnnotationsV1);
92         Pair<Component, ComponentInstance> changeVersionRes = changeComponentInstanceVersion(service, createdCmptInstance, vfWithAnnotationsV2, UserRoleEnum.DESIGNER, true).left().value();
93         Component serviceAfterChangeVersion = changeVersionRes.getKey();
94         ComponentInstance newInstance = changeVersionRes.getRight();
95         List<ComponentInstanceInput> declaredProps = getInstanceProperties(serviceAfterChangeVersion, newInstance.getUniqueId(), PROPS_TO_DECLARE);
96         verifyAnnotationsOnDeclaredInputs(vfWithAnnotationsV2, serviceAfterChangeVersion, declaredProps);
97
98     }
99
100     private void verifyAnnotationsOnDeclaredInputs(Resource vfWithAnnotations, Component fetchedService, List<ComponentInstanceInput> declaredProps) throws Exception {
101         Map<String, InputDefinition> serviceLevelInputsByProperty = getCreatedInputsByProperty(fetchedService, declaredProps);
102         Map<String, List<Annotation>> annotationsFromVfLevelInputs = getAnnotationsByInputName(vfWithAnnotations);
103         verifyInputsAnnotation(serviceLevelInputsByProperty, declaredProps, annotationsFromVfLevelInputs);
104     }
105
106     private Map<String, List<Annotation>> getAnnotationsByInputName(Resource resource) {
107         return resource.getInputs()
108                 .stream()
109                 .collect(toMap(InputDefinition::getName, input -> ComponentUtilities.getInputAnnotations(resource, input.getName())));
110     }
111
112     private void deleteDeclaredInputsAndVerifySuccess(Component service) throws Exception {
113         for (InputDefinition declaredInput : service.getInputs()) {
114             RestResponse deleteInputResponse = deleteInputFromComponent(service, declaredInput.getUniqueId());
115             checkSuccess(deleteInputResponse);
116         }
117         Service fetchedService = getServiceObject(service.getUniqueId());
118         assertThat(fetchedService.getInputs()).isNullOrEmpty();
119     }
120
121     private void verifyInputsAnnotation(Map<String, InputDefinition> inputsByProperty, List<ComponentInstanceInput> declaredProps, Map<String, List<Annotation>> expectedAnnotationsByInput) {
122         Map<String, ComponentInstanceInput> propsByName = MapUtil.toMap(declaredProps, ComponentInstanceInput::getName);
123
124         InputDefinition declaredFromPcmFlavourName = findInputDeclaredFromProperty(PCM_FLAVOR_NAME, inputsByProperty, propsByName);
125         verifyInputAnnotations(declaredFromPcmFlavourName, expectedAnnotationsByInput.get(PCM_FLAVOR_NAME));
126
127         InputDefinition declaredFromAvailabilityZone = findInputDeclaredFromProperty(AVAILABILITY_ZONE, inputsByProperty, propsByName);
128         verifyInputAnnotations(declaredFromAvailabilityZone, expectedAnnotationsByInput.get(AVAILABILITY_ZONE));
129
130         InputDefinition declaredFromNetName = findInputDeclaredFromProperty(NET_NAME, inputsByProperty, propsByName);
131         verifyInputAnnotations(declaredFromNetName, expectedAnnotationsByInput.get(NET_NAME));
132
133         InputDefinition declaredFromNFNamingCode = findInputDeclaredFromProperty(NF_NAMING_CODE, inputsByProperty, propsByName);
134         verifyInputHasNoAnnotations(declaredFromNFNamingCode);
135     }
136
137     private void verifyInputHasNoAnnotations(InputDefinition inputWithoutAnnotation) {
138         assertThat(inputWithoutAnnotation.getAnnotations()).isNullOrEmpty();
139     }
140
141     private InputDefinition findInputDeclaredFromProperty(String propName, Map<String, InputDefinition> inputsByProperty, Map<String, ComponentInstanceInput> propsByName) {
142         String propId = propsByName.get(propName).getUniqueId();
143         return inputsByProperty.get(propId);
144     }
145
146     private Map<String, InputDefinition> getCreatedInputsByProperty(Component service, List<ComponentInstanceInput> declaredProps) throws Exception {
147         Service fetchedService = getServiceObject(service.getUniqueId());
148         List<InputDefinition> inputs = fetchedService.getInputs();
149         return filterInputsCreatedByDeclaringFromProperties(declaredProps, inputs);
150     }
151
152     private Map<String, InputDefinition> filterInputsCreatedByDeclaringFromProperties(List<ComponentInstanceInput> declaredProps, List<InputDefinition> inputs) {
153         List<String> declaredPropsIds = declaredProps.stream().map(ComponentInstanceInput::getUniqueId).collect(Collectors.toList());
154         Map<String, InputDefinition> inputsByPropertyId = MapUtil.toMap(inputs, InputDefinition::getPropertyId);
155         return Maps.filterKeys(inputsByPropertyId, declaredPropsIds::contains);
156     }
157
158     private Resource importAnnotationsCsarAndCheckIn() throws Exception {
159         Resource annotationsVF = importResourceFromCsar(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, CSAR_WITH_ANNOTATIONS_V1, SRIOV_PATH);
160         changeComponentState(annotationsVF, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CHECKIN, true);
161         return annotationsVF;
162     }
163
164     private Resource updateAnnotationsCsarAndCheckIn(Resource vfToUpdate) throws Exception {
165         changeComponentState(vfToUpdate, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CHECKOUT, true);
166         Resource annotationsVfV2 = updateResourceFromCsar(vfToUpdate, UserRoleEnum.DESIGNER, CSAR_WITH_ANNOTATIONS_V2, SRIOV_PATH);
167         changeComponentState(annotationsVfV2, UserRoleEnum.DESIGNER, LifeCycleStatesEnum.CHECKIN, true);
168         return annotationsVfV2;
169     }
170
171     private List<ComponentInstanceInput> declareProperties(Service service, ComponentInstance instance, String ... propertiesToDeclareNames) throws Exception {
172         Map<String, List<ComponentInstanceInput>> propertiesToDeclare = getServiceInstancesProps(service, instance.getUniqueId(), propertiesToDeclareNames);
173         RestResponse restResponse = PropertyRestUtils.declareProporties(service, propertiesToDeclare, getDefaultUser(UserRoleEnum.DESIGNER));
174         checkSuccess(restResponse);
175         return propertiesToDeclare.get(instance.getUniqueId());
176     }
177
178     private List<ComponentInstanceInput> getInstanceProperties(Component service, String ofInstance, String ... propsNames) {
179         return getServiceInstancesProps(service, ofInstance, propsNames).get(ofInstance);
180     }
181
182     private void verifyInputAnnotations(InputDefinition input, List<Annotation> expectedAnnotations) {
183         if (isEmpty(expectedAnnotations)) {
184             assertThat(input.getAnnotations()).isNullOrEmpty();
185             return;
186         }
187         assertThat(input.getAnnotations())
188                 .usingElementComparatorOnFields("type", "name", "properties")
189                 .isNotEmpty()
190                 .containsExactlyElementsOf(expectedAnnotations);
191     }
192
193     private Map<String, List<ComponentInstanceInput>> getServiceInstancesProps(Component fromService, String ofInstance, String ... propsToDeclareNames) {
194         Set<String> propsToDeclare = ImmutableSet.<String>builder().add(propsToDeclareNames).build();
195         List<ComponentInstanceInput> componentInstancesInputs = fromService.getComponentInstancesInputs().get(ofInstance);
196         return componentInstancesInputs.stream()
197                 .filter(insInput -> propsToDeclare.contains(insInput.getName()))
198                 .collect(groupingBy((i) -> ofInstance));
199     }
200
201 }