1 package org.openecomp.sdc.be.model.operations.impl;
3 import org.junit.After;
4 import org.junit.Before;
5 import org.junit.BeforeClass;
7 import org.junit.runner.RunWith;
8 import org.openecomp.sdc.be.dao.titan.TitanGenericDao;
9 import org.openecomp.sdc.be.model.AnnotationTypeDefinition;
10 import org.openecomp.sdc.be.model.ModelTestBase;
11 import org.openecomp.sdc.be.model.PropertyDefinition;
12 import org.openecomp.sdc.be.model.operations.StorageException;
13 import org.openecomp.sdc.be.utils.TypeUtils;
14 import org.springframework.test.context.ContextConfiguration;
15 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
17 import javax.annotation.Resource;
19 import static java.util.Arrays.asList;
20 import static org.assertj.core.api.Assertions.assertThat;
22 @RunWith(SpringJUnit4ClassRunner.class)
23 @ContextConfiguration("classpath:application-context-test.xml")
24 public class AnnotationTypeOperationsTest extends ModelTestBase {
26 static final String TYPE = "org.openecomp.annotations.source";
27 static final String NEW_TYPE = "org.openecomp.annotations.Source";
28 static final String DESCRIPTION = "description";
29 static final String NEW_DESCRIPTION = "new description";
32 private TitanGenericDao titanGenericDao;
35 private CommonTypeOperations commonTypeOperations;
38 private AnnotationTypeOperations annotationTypeOperations;
40 private PropertyDefinition prop1, prop2;
41 private AnnotationTypeDefinition initialAnnotationDefinition;
44 public static void setupBeforeClass() {
49 public void initTestData() {
50 removeGraphVertices(titanGenericDao.getGraph());
51 prop1 = createSimpleProperty("val1", "prop1", "string");
55 public void tearDown() {
56 titanGenericDao.rollback();
59 @SuppressWarnings("unchecked")
61 public void testAddType() {
63 AnnotationTypeDefinition result = annotationTypeOperations.addType(initialAnnotationDefinition);
64 assertThat(result.getUniqueId()).isNotEmpty();
66 .isEqualToComparingOnlyGivenFields(initialAnnotationDefinition, "description", "type");
67 assertThat(result.getProperties())
68 .usingElementComparatorOnFields("defaultValue", "name", "type")
69 .containsExactlyInAnyOrder(prop1);
70 assertThat(result.isHighestVersion()).isTrue();
74 public void testGetLatestType_TypeDoesntExist_shouldReturnNull() {
75 AnnotationTypeDefinition latestType = annotationTypeOperations.getLatestType(TYPE);
76 assertThat(latestType).isNull();
80 public void testGetLatestType_TypeExists_shouldReturnIt() {
82 AnnotationTypeDefinition latestType = annotationTypeOperations.getLatestType(TYPE);
83 assertThat(latestType.getType()).isEqualTo(TYPE);
86 public void addAnnotationType() {
88 annotationTypeOperations.addType(initialAnnotationDefinition);
89 titanGenericDao.commit();
93 public void compareTypes_same_shouldReturnTrue() {
94 AnnotationTypeDefinition type1 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop1);
95 AnnotationTypeDefinition type2 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop1);
96 assertThat(annotationTypeOperations.isSameType(type1, type2)).isTrue();
100 public void compareTypes_sameExceptVersions_shouldReturnTrue() {
101 AnnotationTypeDefinition type1 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop1);
102 AnnotationTypeDefinition type2 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop1);
103 type1.setVersion("1");
104 type2.setVersion("2");
105 assertThat(annotationTypeOperations.isSameType(type1, type2)).isTrue();
109 public void compareTypes_differentType_shouldReturnFalse() {
110 AnnotationTypeDefinition type1 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop1);
111 AnnotationTypeDefinition type2 = buildAnnotationDefinition(DESCRIPTION, NEW_TYPE, prop1);
112 assertThat(annotationTypeOperations.isSameType(type1, type2)).isFalse();
116 public void compareTypes_differentDescription_shouldReturnFalse() {
117 AnnotationTypeDefinition type1 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop1);
118 AnnotationTypeDefinition type2 = buildAnnotationDefinition(NEW_DESCRIPTION, TYPE, prop1);
119 assertThat(annotationTypeOperations.isSameType(type1, type2)).isFalse();
123 public void compareTypes_differentProperty_shouldReturnFalse() {
124 AnnotationTypeDefinition type1 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop1);
125 prop2 = createSimpleProperty("val2", "prop2", "string");
126 AnnotationTypeDefinition type2 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop2);
127 assertThat(annotationTypeOperations.isSameType(type1, type2)).isFalse();
131 public void testUpdateType_propertyAdded_shouldSucceed() {
133 prop2 = createSimpleProperty("val2", "prop2", "string");
134 AnnotationTypeDefinition advancedDefinition = buildAnnotationDefinition(NEW_DESCRIPTION, TYPE, prop1, prop2);
135 AnnotationTypeDefinition updatedType = annotationTypeOperations.updateType(initialAnnotationDefinition, advancedDefinition);
136 assertThat(updatedType.getDescription()).isEqualTo(NEW_DESCRIPTION);
137 assertThat(updatedType.getProperties())
138 .usingElementComparatorOnFields("defaultValue", "name", "type")
139 .containsExactlyInAnyOrder(prop1, prop2);
143 public void testUpdateType_propertyDefaultValueModification_shouldSucceed() {
145 prop2 = createSimpleProperty("val3", "prop1", "string");
146 AnnotationTypeDefinition advancedDefinition = buildAnnotationDefinition(DESCRIPTION, TYPE, prop2);
147 AnnotationTypeDefinition updatedType = annotationTypeOperations.updateType(initialAnnotationDefinition, advancedDefinition);
148 assertThat(updatedType.getProperties())
149 .usingElementComparatorOnFields("defaultValue", "name", "type")
150 .containsExactlyInAnyOrder(prop2);
154 public void testUpdateType_propertyDescriptionModification_shouldSucceed() {
156 prop2 = createSimpleProperty("val1", "prop1", "string");
157 prop2.setDescription("bla");
158 AnnotationTypeDefinition advancedDefinition = buildAnnotationDefinition(DESCRIPTION, TYPE, prop2);
159 AnnotationTypeDefinition updatedType = annotationTypeOperations.updateType(initialAnnotationDefinition, advancedDefinition);
160 assertThat(updatedType.getProperties())
161 .usingElementComparatorOnFields("defaultValue", "name", "type", "description")
162 .containsExactlyInAnyOrder(prop2);
165 @Test(expected = StorageException.class)
166 public void testUpdateType_propertyTypeModification_shouldFail() {
168 prop2 = createSimpleProperty("val1", "prop1", "int");
169 AnnotationTypeDefinition advancedDefinition = buildAnnotationDefinition(DESCRIPTION, TYPE, prop2);
170 annotationTypeOperations.updateType(initialAnnotationDefinition, advancedDefinition);
173 @Test(expected = StorageException.class)
174 public void testUpdateType_propertyRemoved_shouldFail() {
176 prop2 = createSimpleProperty("val1", "prop2", "int");
177 AnnotationTypeDefinition advancedDefinition = buildAnnotationDefinition(DESCRIPTION, TYPE, prop2);
178 annotationTypeOperations.updateType(initialAnnotationDefinition, advancedDefinition);
181 private void prepareInitialType() {
182 initialAnnotationDefinition = buildAnnotationDefinition(DESCRIPTION,
185 initialAnnotationDefinition.setVersion(TypeUtils.FIRST_CERTIFIED_VERSION_VERSION);
188 private AnnotationTypeDefinition buildAnnotationDefinition(String description, String type, PropertyDefinition ... properties) {
189 AnnotationTypeDefinition annotationTypeDefinition = new AnnotationTypeDefinition();
190 annotationTypeDefinition.setDescription(description);
191 annotationTypeDefinition.setType(type);
192 annotationTypeDefinition.setHighestVersion(true);
193 annotationTypeDefinition.setProperties(asList(properties));
194 return annotationTypeDefinition;