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.be.model.operations.impl;
23 import static java.util.Arrays.asList;
24 import static org.assertj.core.api.Assertions.assertThat;
26 import javax.annotation.Resource;
27 import org.junit.jupiter.api.AfterEach;
28 import org.junit.jupiter.api.Assertions;
29 import org.junit.jupiter.api.BeforeAll;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao;
33 import org.openecomp.sdc.be.model.AnnotationTypeDefinition;
34 import org.openecomp.sdc.be.model.ModelTestBase;
35 import org.openecomp.sdc.be.model.PropertyDefinition;
36 import org.openecomp.sdc.be.model.operations.StorageException;
37 import org.openecomp.sdc.be.utils.TypeUtils;
38 import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
40 @SpringJUnitConfig(locations = "classpath:application-context-test.xml")
41 public class AnnotationTypeOperationsTest extends ModelTestBase {
43 static final String TYPE = "org.openecomp.annotations.source";
44 static final String NEW_TYPE = "org.openecomp.annotations.Source";
45 static final String DESCRIPTION = "description";
46 static final String NEW_DESCRIPTION = "new description";
49 private JanusGraphGenericDao janusGraphGenericDao;
52 private CommonTypeOperations commonTypeOperations;
55 private AnnotationTypeOperations annotationTypeOperations;
57 private PropertyDefinition prop1, prop2;
58 private AnnotationTypeDefinition initialAnnotationDefinition;
61 public static void setupBeforeClass() {
66 public void initTestData() {
67 removeGraphVertices(janusGraphGenericDao.getGraph());
68 prop1 = createSimpleProperty("val1", "prop1", "string");
72 public void tearDown() {
73 janusGraphGenericDao.rollback();
76 @SuppressWarnings("unchecked")
78 public void testAddType() {
80 AnnotationTypeDefinition result = annotationTypeOperations.addType(initialAnnotationDefinition);
81 assertThat(result.getUniqueId()).isNotEmpty();
83 .isEqualToComparingOnlyGivenFields(initialAnnotationDefinition, "description", "type");
84 assertThat(result.getProperties())
85 .usingElementComparatorOnFields("defaultValue", "name", "type")
86 .containsExactlyInAnyOrder(prop1);
87 assertThat(result.isHighestVersion()).isTrue();
91 public void testGetLatestType_TypeDoesntExist_shouldReturnNull() {
92 AnnotationTypeDefinition latestType = annotationTypeOperations.getLatestType(TYPE);
93 assertThat(latestType).isNull();
97 public void testGetLatestType_TypeExists_shouldReturnIt() {
99 AnnotationTypeDefinition latestType = annotationTypeOperations.getLatestType(TYPE);
100 assertThat(latestType.getType()).isEqualTo(TYPE);
103 public void addAnnotationType() {
104 prepareInitialType();
105 annotationTypeOperations.addType(initialAnnotationDefinition);
106 janusGraphGenericDao.commit();
110 public void compareTypes_same_shouldReturnTrue() {
111 AnnotationTypeDefinition type1 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop1);
112 AnnotationTypeDefinition type2 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop1);
113 assertThat(annotationTypeOperations.isSameType(type1, type2)).isTrue();
117 public void compareTypes_sameExceptVersions_shouldReturnTrue() {
118 AnnotationTypeDefinition type1 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop1);
119 AnnotationTypeDefinition type2 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop1);
120 type1.setVersion("1");
121 type2.setVersion("2");
122 assertThat(annotationTypeOperations.isSameType(type1, type2)).isTrue();
126 public void compareTypes_differentType_shouldReturnFalse() {
127 AnnotationTypeDefinition type1 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop1);
128 AnnotationTypeDefinition type2 = buildAnnotationDefinition(DESCRIPTION, NEW_TYPE, prop1);
129 assertThat(annotationTypeOperations.isSameType(type1, type2)).isFalse();
133 public void compareTypes_differentDescription_shouldReturnFalse() {
134 AnnotationTypeDefinition type1 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop1);
135 AnnotationTypeDefinition type2 = buildAnnotationDefinition(NEW_DESCRIPTION, TYPE, prop1);
136 assertThat(annotationTypeOperations.isSameType(type1, type2)).isFalse();
140 public void compareTypes_differentProperty_shouldReturnFalse() {
141 AnnotationTypeDefinition type1 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop1);
142 prop2 = createSimpleProperty("val2", "prop2", "string");
143 AnnotationTypeDefinition type2 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop2);
144 assertThat(annotationTypeOperations.isSameType(type1, type2)).isFalse();
148 public void testUpdateType_propertyAdded_shouldSucceed() {
150 prop2 = createSimpleProperty("val2", "prop2", "string");
151 AnnotationTypeDefinition advancedDefinition = buildAnnotationDefinition(NEW_DESCRIPTION, TYPE, prop1, prop2);
152 AnnotationTypeDefinition updatedType = annotationTypeOperations.updateType(initialAnnotationDefinition, advancedDefinition);
153 assertThat(updatedType.getDescription()).isEqualTo(NEW_DESCRIPTION);
154 assertThat(updatedType.getProperties())
155 .usingElementComparatorOnFields("defaultValue", "name", "type")
156 .containsExactlyInAnyOrder(prop1, prop2);
160 public void testUpdateType_propertyDefaultValueModification_shouldSucceed() {
162 prop2 = createSimpleProperty("val3", "prop1", "string");
163 AnnotationTypeDefinition advancedDefinition = buildAnnotationDefinition(DESCRIPTION, TYPE, prop2);
164 AnnotationTypeDefinition updatedType = annotationTypeOperations.updateType(initialAnnotationDefinition, advancedDefinition);
165 assertThat(updatedType.getProperties())
166 .usingElementComparatorOnFields("defaultValue", "name", "type")
167 .containsExactlyInAnyOrder(prop2);
171 public void testUpdateType_propertyDescriptionModification_shouldSucceed() {
173 prop2 = createSimpleProperty("val1", "prop1", "string");
174 prop2.setDescription("bla");
175 AnnotationTypeDefinition advancedDefinition = buildAnnotationDefinition(DESCRIPTION, TYPE, prop2);
176 AnnotationTypeDefinition updatedType = annotationTypeOperations.updateType(initialAnnotationDefinition, advancedDefinition);
177 assertThat(updatedType.getProperties())
178 .usingElementComparatorOnFields("defaultValue", "name", "type", "description")
179 .containsExactlyInAnyOrder(prop2);
183 public void testUpdateType_propertyTypeModification_shouldFail() {
184 Assertions.assertThrows(StorageException.class,()->{
186 prop2 = createSimpleProperty("val1", "prop1", "int");
187 AnnotationTypeDefinition advancedDefinition = buildAnnotationDefinition(DESCRIPTION, TYPE, prop2);
188 annotationTypeOperations.updateType(initialAnnotationDefinition, advancedDefinition);
193 public void testUpdateType_propertyRemoved_shouldFail() {
194 Assertions.assertThrows(StorageException.class,()->{
196 prop2 = createSimpleProperty("val1", "prop2", "int");
197 AnnotationTypeDefinition advancedDefinition = buildAnnotationDefinition(DESCRIPTION, TYPE, prop2);
198 annotationTypeOperations.updateType(initialAnnotationDefinition, advancedDefinition);
202 private void prepareInitialType() {
203 initialAnnotationDefinition = buildAnnotationDefinition(DESCRIPTION,
206 initialAnnotationDefinition.setVersion(TypeUtils.getFirstCertifiedVersionVersion());
209 private AnnotationTypeDefinition buildAnnotationDefinition(String description, String type, PropertyDefinition... properties) {
210 AnnotationTypeDefinition annotationTypeDefinition = new AnnotationTypeDefinition();
211 annotationTypeDefinition.setDescription(description);
212 annotationTypeDefinition.setType(type);
213 annotationTypeDefinition.setHighestVersion(true);
214 annotationTypeDefinition.setProperties(asList(properties));
215 return annotationTypeDefinition;