df164b9d310b529cb3ae3ab4a9a469aca30b66d8
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / operations / impl / AnnotationTypeOperationsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.model.operations.impl;
22
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;
36
37 import javax.annotation.Resource;
38
39 import static java.util.Arrays.asList;
40 import static org.assertj.core.api.Assertions.assertThat;
41
42 @RunWith(SpringJUnit4ClassRunner.class)
43 @ContextConfiguration("classpath:application-context-test.xml")
44 public class AnnotationTypeOperationsTest extends ModelTestBase {
45
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";
50
51     @Resource
52     private JanusGraphGenericDao janusGraphGenericDao;
53
54     @Resource
55     private CommonTypeOperations commonTypeOperations;
56
57     @Resource
58     private AnnotationTypeOperations annotationTypeOperations;
59
60     private PropertyDefinition prop1, prop2;
61     private AnnotationTypeDefinition initialAnnotationDefinition;
62
63     @BeforeClass
64     public static void setupBeforeClass() {
65         ModelTestBase.init();
66     }
67
68     @Before
69     public void initTestData() {
70         removeGraphVertices(janusGraphGenericDao.getGraph());
71         prop1 = createSimpleProperty("val1", "prop1", "string");
72     }
73
74     @After
75     public void tearDown() {
76         janusGraphGenericDao.rollback();
77     }
78
79     @SuppressWarnings("unchecked")
80     @Test
81     public void testAddType() {
82         prepareInitialType();
83         AnnotationTypeDefinition result = annotationTypeOperations.addType(initialAnnotationDefinition);
84         assertThat(result.getUniqueId()).isNotEmpty();
85         assertThat(result)
86                 .isEqualToComparingOnlyGivenFields(initialAnnotationDefinition, "description", "type");
87         assertThat(result.getProperties())
88                 .usingElementComparatorOnFields("defaultValue", "name", "type")
89                 .containsExactlyInAnyOrder(prop1);
90         assertThat(result.isHighestVersion()).isTrue();
91     }
92
93    @Test
94     public void testGetLatestType_TypeDoesntExist_shouldReturnNull() {
95         AnnotationTypeDefinition latestType = annotationTypeOperations.getLatestType(TYPE);
96         assertThat(latestType).isNull();
97     }
98
99     @Test
100     public void testGetLatestType_TypeExists_shouldReturnIt() {
101         addAnnotationType();
102         AnnotationTypeDefinition latestType = annotationTypeOperations.getLatestType(TYPE);
103         assertThat(latestType.getType()).isEqualTo(TYPE);
104     }
105
106     public void addAnnotationType() {
107         prepareInitialType();
108         annotationTypeOperations.addType(initialAnnotationDefinition);
109         janusGraphGenericDao.commit();
110     }
111
112     @Test
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();
117     }
118
119     @Test
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();
126     }
127
128     @Test
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();
133     }
134
135     @Test
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();
140     }
141
142     @Test
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();
148     }
149
150     @Test
151     public void testUpdateType_propertyAdded_shouldSucceed() {
152         addAnnotationType();
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);
160     }
161
162     @Test
163     public void testUpdateType_propertyDefaultValueModification_shouldSucceed() {
164         addAnnotationType();
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);
171     }
172
173     @Test
174     public void testUpdateType_propertyDescriptionModification_shouldSucceed() {
175         addAnnotationType();
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);
183     }
184
185     @Test(expected = StorageException.class)
186     public void testUpdateType_propertyTypeModification_shouldFail() {
187         addAnnotationType();
188         prop2 = createSimpleProperty("val1", "prop1", "int");
189         AnnotationTypeDefinition advancedDefinition = buildAnnotationDefinition(DESCRIPTION, TYPE, prop2);
190         annotationTypeOperations.updateType(initialAnnotationDefinition, advancedDefinition);
191     }
192
193     @Test(expected = StorageException.class)
194     public void testUpdateType_propertyRemoved_shouldFail() {
195         addAnnotationType();
196         prop2 = createSimpleProperty("val1", "prop2", "int");
197         AnnotationTypeDefinition advancedDefinition = buildAnnotationDefinition(DESCRIPTION, TYPE, prop2);
198         annotationTypeOperations.updateType(initialAnnotationDefinition, advancedDefinition);
199     }
200
201     private void prepareInitialType() {
202         initialAnnotationDefinition = buildAnnotationDefinition(DESCRIPTION,
203                 TYPE,
204                 prop1);
205         initialAnnotationDefinition.setVersion(TypeUtils.getFirstCertifiedVersionVersion());
206     }
207
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;
215     }
216
217 }