1 package org.openecomp.sdc.internalApis.annotations;
3 import com.google.common.collect.ImmutableSet;
4 import com.google.common.collect.Maps;
5 import org.apache.commons.lang3.tuple.Pair;
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;
27 import java.util.List;
30 import java.util.stream.Collectors;
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;
44 public class AnnotationsTest extends ComponentBaseTest{
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");
56 public static TestName name = new TestName();
58 public AnnotationsTest() {
59 super(name, AnnotationsTest.class.getName());
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));
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);
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);
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);
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);
106 private Map<String, List<Annotation>> getAnnotationsByInputName(Resource resource) {
107 return resource.getInputs()
109 .collect(toMap(InputDefinition::getName, input -> ComponentUtilities.getInputAnnotations(resource, input.getName())));
112 private void deleteDeclaredInputsAndVerifySuccess(Component service) throws Exception {
113 for (InputDefinition declaredInput : service.getInputs()) {
114 RestResponse deleteInputResponse = deleteInputFromComponent(service, declaredInput.getUniqueId());
115 checkSuccess(deleteInputResponse);
117 Service fetchedService = getServiceObject(service.getUniqueId());
118 assertThat(fetchedService.getInputs()).isNullOrEmpty();
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);
124 InputDefinition declaredFromPcmFlavourName = findInputDeclaredFromProperty(PCM_FLAVOR_NAME, inputsByProperty, propsByName);
125 verifyInputAnnotations(declaredFromPcmFlavourName, expectedAnnotationsByInput.get(PCM_FLAVOR_NAME));
127 InputDefinition declaredFromAvailabilityZone = findInputDeclaredFromProperty(AVAILABILITY_ZONE, inputsByProperty, propsByName);
128 verifyInputAnnotations(declaredFromAvailabilityZone, expectedAnnotationsByInput.get(AVAILABILITY_ZONE));
130 InputDefinition declaredFromNetName = findInputDeclaredFromProperty(NET_NAME, inputsByProperty, propsByName);
131 verifyInputAnnotations(declaredFromNetName, expectedAnnotationsByInput.get(NET_NAME));
133 InputDefinition declaredFromNFNamingCode = findInputDeclaredFromProperty(NF_NAMING_CODE, inputsByProperty, propsByName);
134 verifyInputHasNoAnnotations(declaredFromNFNamingCode);
137 private void verifyInputHasNoAnnotations(InputDefinition inputWithoutAnnotation) {
138 assertThat(inputWithoutAnnotation.getAnnotations()).isNullOrEmpty();
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);
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);
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);
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;
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;
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());
178 private List<ComponentInstanceInput> getInstanceProperties(Component service, String ofInstance, String ... propsNames) {
179 return getServiceInstancesProps(service, ofInstance, propsNames).get(ofInstance);
182 private void verifyInputAnnotations(InputDefinition input, List<Annotation> expectedAnnotations) {
183 if (isEmpty(expectedAnnotations)) {
184 assertThat(input.getAnnotations()).isNullOrEmpty();
187 assertThat(input.getAnnotations())
188 .usingElementComparatorOnFields("type", "name", "properties")
190 .containsExactlyElementsOf(expectedAnnotations);
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));