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 org.junit.After;
24 import org.junit.Before;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao;
29 import org.openecomp.sdc.be.model.AnnotationTypeDefinition;
30 import org.openecomp.sdc.be.model.ModelTestBase;
31 import org.openecomp.sdc.be.model.PropertyDefinition;
32 import org.openecomp.sdc.be.model.operations.StorageException;
33 import org.openecomp.sdc.be.utils.TypeUtils;
34 import org.springframework.test.context.ContextConfiguration;
35 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
37 import javax.annotation.Resource;
39 import static java.util.Arrays.asList;
40 import static org.assertj.core.api.Assertions.assertThat;
42 @RunWith(SpringJUnit4ClassRunner.class)
43 @ContextConfiguration("classpath:application-context-test.xml")
44 public class AnnotationTypeOperationsTest extends ModelTestBase {
46 static final String TYPE = "org.openecomp.annotations.source";
47 static final String NEW_TYPE = "org.openecomp.annotations.Source";
48 static final String DESCRIPTION = "description";
49 static final String NEW_DESCRIPTION = "new description";
52 private JanusGraphGenericDao janusGraphGenericDao;
55 private CommonTypeOperations commonTypeOperations;
58 private AnnotationTypeOperations annotationTypeOperations;
60 private PropertyDefinition prop1, prop2;
61 private AnnotationTypeDefinition initialAnnotationDefinition;
64 public static void setupBeforeClass() {
69 public void initTestData() {
70 removeGraphVertices(janusGraphGenericDao.getGraph());
71 prop1 = createSimpleProperty("val1", "prop1", "string");
75 public void tearDown() {
76 janusGraphGenericDao.rollback();
79 @SuppressWarnings("unchecked")
81 public void testAddType() {
83 AnnotationTypeDefinition result = annotationTypeOperations.addType(initialAnnotationDefinition);
84 assertThat(result.getUniqueId()).isNotEmpty();
86 .isEqualToComparingOnlyGivenFields(initialAnnotationDefinition, "description", "type");
87 assertThat(result.getProperties())
88 .usingElementComparatorOnFields("defaultValue", "name", "type")
89 .containsExactlyInAnyOrder(prop1);
90 assertThat(result.isHighestVersion()).isTrue();
94 public void testGetLatestType_TypeDoesntExist_shouldReturnNull() {
95 AnnotationTypeDefinition latestType = annotationTypeOperations.getLatestType(TYPE);
96 assertThat(latestType).isNull();
100 public void testGetLatestType_TypeExists_shouldReturnIt() {
102 AnnotationTypeDefinition latestType = annotationTypeOperations.getLatestType(TYPE);
103 assertThat(latestType.getType()).isEqualTo(TYPE);
106 public void addAnnotationType() {
107 prepareInitialType();
108 annotationTypeOperations.addType(initialAnnotationDefinition);
109 janusGraphGenericDao.commit();
113 public void compareTypes_same_shouldReturnTrue() {
114 AnnotationTypeDefinition type1 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop1);
115 AnnotationTypeDefinition type2 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop1);
116 assertThat(annotationTypeOperations.isSameType(type1, type2)).isTrue();
120 public void compareTypes_sameExceptVersions_shouldReturnTrue() {
121 AnnotationTypeDefinition type1 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop1);
122 AnnotationTypeDefinition type2 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop1);
123 type1.setVersion("1");
124 type2.setVersion("2");
125 assertThat(annotationTypeOperations.isSameType(type1, type2)).isTrue();
129 public void compareTypes_differentType_shouldReturnFalse() {
130 AnnotationTypeDefinition type1 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop1);
131 AnnotationTypeDefinition type2 = buildAnnotationDefinition(DESCRIPTION, NEW_TYPE, prop1);
132 assertThat(annotationTypeOperations.isSameType(type1, type2)).isFalse();
136 public void compareTypes_differentDescription_shouldReturnFalse() {
137 AnnotationTypeDefinition type1 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop1);
138 AnnotationTypeDefinition type2 = buildAnnotationDefinition(NEW_DESCRIPTION, TYPE, prop1);
139 assertThat(annotationTypeOperations.isSameType(type1, type2)).isFalse();
143 public void compareTypes_differentProperty_shouldReturnFalse() {
144 AnnotationTypeDefinition type1 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop1);
145 prop2 = createSimpleProperty("val2", "prop2", "string");
146 AnnotationTypeDefinition type2 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop2);
147 assertThat(annotationTypeOperations.isSameType(type1, type2)).isFalse();
151 public void testUpdateType_propertyAdded_shouldSucceed() {
153 prop2 = createSimpleProperty("val2", "prop2", "string");
154 AnnotationTypeDefinition advancedDefinition = buildAnnotationDefinition(NEW_DESCRIPTION, TYPE, prop1, prop2);
155 AnnotationTypeDefinition updatedType = annotationTypeOperations.updateType(initialAnnotationDefinition, advancedDefinition);
156 assertThat(updatedType.getDescription()).isEqualTo(NEW_DESCRIPTION);
157 assertThat(updatedType.getProperties())
158 .usingElementComparatorOnFields("defaultValue", "name", "type")
159 .containsExactlyInAnyOrder(prop1, prop2);
163 public void testUpdateType_propertyDefaultValueModification_shouldSucceed() {
165 prop2 = createSimpleProperty("val3", "prop1", "string");
166 AnnotationTypeDefinition advancedDefinition = buildAnnotationDefinition(DESCRIPTION, TYPE, prop2);
167 AnnotationTypeDefinition updatedType = annotationTypeOperations.updateType(initialAnnotationDefinition, advancedDefinition);
168 assertThat(updatedType.getProperties())
169 .usingElementComparatorOnFields("defaultValue", "name", "type")
170 .containsExactlyInAnyOrder(prop2);
174 public void testUpdateType_propertyDescriptionModification_shouldSucceed() {
176 prop2 = createSimpleProperty("val1", "prop1", "string");
177 prop2.setDescription("bla");
178 AnnotationTypeDefinition advancedDefinition = buildAnnotationDefinition(DESCRIPTION, TYPE, prop2);
179 AnnotationTypeDefinition updatedType = annotationTypeOperations.updateType(initialAnnotationDefinition, advancedDefinition);
180 assertThat(updatedType.getProperties())
181 .usingElementComparatorOnFields("defaultValue", "name", "type", "description")
182 .containsExactlyInAnyOrder(prop2);
185 @Test(expected = StorageException.class)
186 public void testUpdateType_propertyTypeModification_shouldFail() {
188 prop2 = createSimpleProperty("val1", "prop1", "int");
189 AnnotationTypeDefinition advancedDefinition = buildAnnotationDefinition(DESCRIPTION, TYPE, prop2);
190 annotationTypeOperations.updateType(initialAnnotationDefinition, advancedDefinition);
193 @Test(expected = StorageException.class)
194 public void testUpdateType_propertyRemoved_shouldFail() {
196 prop2 = createSimpleProperty("val1", "prop2", "int");
197 AnnotationTypeDefinition advancedDefinition = buildAnnotationDefinition(DESCRIPTION, TYPE, prop2);
198 annotationTypeOperations.updateType(initialAnnotationDefinition, advancedDefinition);
201 private void prepareInitialType() {
202 initialAnnotationDefinition = buildAnnotationDefinition(DESCRIPTION,
205 initialAnnotationDefinition.setVersion(TypeUtils.getFirstCertifiedVersionVersion());
208 private AnnotationTypeDefinition buildAnnotationDefinition(String description, String type, PropertyDefinition ... properties) {
209 AnnotationTypeDefinition annotationTypeDefinition = new AnnotationTypeDefinition();
210 annotationTypeDefinition.setDescription(description);
211 annotationTypeDefinition.setType(type);
212 annotationTypeDefinition.setHighestVersion(true);
213 annotationTypeDefinition.setProperties(asList(properties));
214 return annotationTypeDefinition;