Fix test cases failing incorrectly
[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 static java.util.Arrays.asList;
24 import static org.assertj.core.api.Assertions.assertThat;
25
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;
39
40 @SpringJUnitConfig(locations = "classpath:application-context-test.xml")
41 public class AnnotationTypeOperationsTest extends ModelTestBase {
42
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";
47
48     @Resource
49     private JanusGraphGenericDao janusGraphGenericDao;
50
51     @Resource
52     private CommonTypeOperations commonTypeOperations;
53
54     @Resource
55     private AnnotationTypeOperations annotationTypeOperations;
56
57     private PropertyDefinition prop1, prop2;
58     private AnnotationTypeDefinition initialAnnotationDefinition;
59
60     @BeforeAll
61     public static void setupBeforeClass() {
62         ModelTestBase.init();
63     }
64
65     @BeforeEach
66     public void initTestData() {
67         removeGraphVertices(janusGraphGenericDao.getGraph());
68         prop1 = createSimpleProperty("val1", "prop1", "string");
69     }
70
71     @AfterEach
72     public void tearDown() {
73         janusGraphGenericDao.rollback();
74     }
75
76     @SuppressWarnings("unchecked")
77     @Test
78     public void testAddType() {
79         prepareInitialType();
80         AnnotationTypeDefinition result = annotationTypeOperations.addType(initialAnnotationDefinition);
81         assertThat(result.getUniqueId()).isNotEmpty();
82         assertThat(result)
83             .isEqualToComparingOnlyGivenFields(initialAnnotationDefinition, "description", "type");
84         assertThat(result.getProperties())
85             .usingElementComparatorOnFields("defaultValue", "name", "type")
86             .containsExactlyInAnyOrder(prop1);
87         assertThat(result.isHighestVersion()).isTrue();
88     }
89
90     @Test
91     public void testGetLatestType_TypeDoesntExist_shouldReturnNull() {
92         AnnotationTypeDefinition latestType = annotationTypeOperations.getLatestType(TYPE);
93         assertThat(latestType).isNull();
94     }
95
96     @Test
97     public void testGetLatestType_TypeExists_shouldReturnIt() {
98         addAnnotationType();
99         AnnotationTypeDefinition latestType = annotationTypeOperations.getLatestType(TYPE);
100         assertThat(latestType.getType()).isEqualTo(TYPE);
101     }
102
103     public void addAnnotationType() {
104         prepareInitialType();
105         annotationTypeOperations.addType(initialAnnotationDefinition);
106         janusGraphGenericDao.commit();
107     }
108
109     @Test
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();
114     }
115
116     @Test
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();
123     }
124
125     @Test
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();
130     }
131
132     @Test
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();
137     }
138
139     @Test
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();
145     }
146
147     @Test
148     public void testUpdateType_propertyAdded_shouldSucceed() {
149         addAnnotationType();
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);
157     }
158
159     @Test
160     public void testUpdateType_propertyDefaultValueModification_shouldSucceed() {
161         addAnnotationType();
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);
168     }
169
170     @Test
171     public void testUpdateType_propertyDescriptionModification_shouldSucceed() {
172         addAnnotationType();
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);
180     }
181
182     @Test
183     public void testUpdateType_propertyTypeModification_shouldFail() {
184         Assertions.assertThrows(StorageException.class,()->{
185         addAnnotationType();
186         prop2 = createSimpleProperty("val1", "prop1", "int");
187         AnnotationTypeDefinition advancedDefinition = buildAnnotationDefinition(DESCRIPTION, TYPE, prop2);
188         annotationTypeOperations.updateType(initialAnnotationDefinition, advancedDefinition);
189         });
190     }
191
192     @Test
193     public void testUpdateType_propertyRemoved_shouldFail() {
194         Assertions.assertThrows(StorageException.class,()->{
195             addAnnotationType();
196             prop2 = createSimpleProperty("val1", "prop2", "int");
197             AnnotationTypeDefinition advancedDefinition = buildAnnotationDefinition(DESCRIPTION, TYPE, prop2);
198             annotationTypeOperations.updateType(initialAnnotationDefinition, advancedDefinition);
199         });
200     }
201
202     private void prepareInitialType() {
203         initialAnnotationDefinition = buildAnnotationDefinition(DESCRIPTION,
204             TYPE,
205             prop1);
206         initialAnnotationDefinition.setVersion(TypeUtils.getFirstCertifiedVersionVersion());
207     }
208
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;
216     }
217
218 }