re base code
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / operations / impl / AnnotationTypeOperationsTest.java
1 package org.openecomp.sdc.be.model.operations.impl;
2
3 import org.junit.After;
4 import org.junit.Before;
5 import org.junit.BeforeClass;
6 import org.junit.Test;
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;
16
17 import javax.annotation.Resource;
18
19 import static java.util.Arrays.asList;
20 import static org.assertj.core.api.Assertions.assertThat;
21
22 @RunWith(SpringJUnit4ClassRunner.class)
23 @ContextConfiguration("classpath:application-context-test.xml")
24 public class AnnotationTypeOperationsTest extends ModelTestBase {
25
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";
30
31     @Resource
32     private TitanGenericDao titanGenericDao;
33
34     @Resource
35     private CommonTypeOperations commonTypeOperations;
36
37     @Resource
38     private AnnotationTypeOperations annotationTypeOperations;
39
40     private PropertyDefinition prop1, prop2;
41     private AnnotationTypeDefinition initialAnnotationDefinition;
42
43     @BeforeClass
44     public static void setupBeforeClass() {
45         ModelTestBase.init();
46     }
47
48     @Before
49     public void initTestData() {
50         removeGraphVertices(titanGenericDao.getGraph());
51         prop1 = createSimpleProperty("val1", "prop1", "string");
52     }
53
54     @After
55     public void tearDown() {
56         titanGenericDao.rollback();
57     }
58
59     @SuppressWarnings("unchecked")
60     @Test
61     public void testAddType() {
62         prepareInitialType();
63         AnnotationTypeDefinition result = annotationTypeOperations.addType(initialAnnotationDefinition);
64         assertThat(result.getUniqueId()).isNotEmpty();
65         assertThat(result)
66                 .isEqualToComparingOnlyGivenFields(initialAnnotationDefinition, "description", "type");
67         assertThat(result.getProperties())
68                 .usingElementComparatorOnFields("defaultValue", "name", "type")
69                 .containsExactlyInAnyOrder(prop1);
70         assertThat(result.isHighestVersion()).isTrue();
71     }
72
73    @Test
74     public void testGetLatestType_TypeDoesntExist_shouldReturnNull() {
75         AnnotationTypeDefinition latestType = annotationTypeOperations.getLatestType(TYPE);
76         assertThat(latestType).isNull();
77     }
78
79     @Test
80     public void testGetLatestType_TypeExists_shouldReturnIt() {
81         addAnnotationType();
82         AnnotationTypeDefinition latestType = annotationTypeOperations.getLatestType(TYPE);
83         assertThat(latestType.getType()).isEqualTo(TYPE);
84     }
85
86     public void addAnnotationType() {
87         prepareInitialType();
88         annotationTypeOperations.addType(initialAnnotationDefinition);
89         titanGenericDao.commit();
90     }
91
92     @Test
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();
97     }
98
99     @Test
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();
106     }
107
108     @Test
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();
113     }
114
115     @Test
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();
120     }
121
122     @Test
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();
128     }
129
130     @Test
131     public void testUpdateType_propertyAdded_shouldSucceed() {
132         addAnnotationType();
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);
140     }
141
142     @Test
143     public void testUpdateType_propertyDefaultValueModification_shouldSucceed() {
144         addAnnotationType();
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);
151     }
152
153     @Test
154     public void testUpdateType_propertyDescriptionModification_shouldSucceed() {
155         addAnnotationType();
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);
163     }
164
165     @Test(expected = StorageException.class)
166     public void testUpdateType_propertyTypeModification_shouldFail() {
167         addAnnotationType();
168         prop2 = createSimpleProperty("val1", "prop1", "int");
169         AnnotationTypeDefinition advancedDefinition = buildAnnotationDefinition(DESCRIPTION, TYPE, prop2);
170         annotationTypeOperations.updateType(initialAnnotationDefinition, advancedDefinition);
171     }
172
173     @Test(expected = StorageException.class)
174     public void testUpdateType_propertyRemoved_shouldFail() {
175         addAnnotationType();
176         prop2 = createSimpleProperty("val1", "prop2", "int");
177         AnnotationTypeDefinition advancedDefinition = buildAnnotationDefinition(DESCRIPTION, TYPE, prop2);
178         annotationTypeOperations.updateType(initialAnnotationDefinition, advancedDefinition);
179     }
180
181     private void prepareInitialType() {
182         initialAnnotationDefinition = buildAnnotationDefinition(DESCRIPTION,
183                 TYPE,
184                 prop1);
185         initialAnnotationDefinition.setVersion(TypeUtils.FIRST_CERTIFIED_VERSION_VERSION);
186     }
187
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;
195     }
196
197 }