aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/AnnotationTypeOperationsTest.java
blob: 24a263a89b6f5cbe6d8f2c0fb5bf229d3073363c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package org.openecomp.sdc.be.model.operations.impl;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao;
import org.openecomp.sdc.be.model.AnnotationTypeDefinition;
import org.openecomp.sdc.be.model.ModelTestBase;
import org.openecomp.sdc.be.model.PropertyDefinition;
import org.openecomp.sdc.be.model.operations.StorageException;
import org.openecomp.sdc.be.utils.TypeUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:application-context-test.xml")
public class AnnotationTypeOperationsTest extends ModelTestBase {

    static final String TYPE = "org.openecomp.annotations.source";
    static final String NEW_TYPE = "org.openecomp.annotations.Source";
    static final String DESCRIPTION = "description";
    static final String NEW_DESCRIPTION = "new description";

    @Resource
    private JanusGraphGenericDao janusGraphGenericDao;

    @Resource
    private CommonTypeOperations commonTypeOperations;

    @Resource
    private AnnotationTypeOperations annotationTypeOperations;

    private PropertyDefinition prop1, prop2;
    private AnnotationTypeDefinition initialAnnotationDefinition;

    @BeforeClass
    public static void setupBeforeClass() {
        ModelTestBase.init();
    }

    @Before
    public void initTestData() {
        removeGraphVertices(janusGraphGenericDao.getGraph());
        prop1 = createSimpleProperty("val1", "prop1", "string");
    }

    @After
    public void tearDown() {
        janusGraphGenericDao.rollback();
    }

    @SuppressWarnings("unchecked")
    @Test
    public void testAddType() {
        prepareInitialType();
        AnnotationTypeDefinition result = annotationTypeOperations.addType(initialAnnotationDefinition);
        assertThat(result.getUniqueId()).isNotEmpty();
        assertThat(result)
                .isEqualToComparingOnlyGivenFields(initialAnnotationDefinition, "description", "type");
        assertThat(result.getProperties())
                .usingElementComparatorOnFields("defaultValue", "name", "type")
                .containsExactlyInAnyOrder(prop1);
        assertThat(result.isHighestVersion()).isTrue();
    }

   @Test
    public void testGetLatestType_TypeDoesntExist_shouldReturnNull() {
        AnnotationTypeDefinition latestType = annotationTypeOperations.getLatestType(TYPE);
        assertThat(latestType).isNull();
    }

    @Test
    public void testGetLatestType_TypeExists_shouldReturnIt() {
        addAnnotationType();
        AnnotationTypeDefinition latestType = annotationTypeOperations.getLatestType(TYPE);
        assertThat(latestType.getType()).isEqualTo(TYPE);
    }

    public void addAnnotationType() {
        prepareInitialType();
        annotationTypeOperations.addType(initialAnnotationDefinition);
        janusGraphGenericDao.commit();
    }

    @Test
    public void compareTypes_same_shouldReturnTrue() {
        AnnotationTypeDefinition type1 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop1);
        AnnotationTypeDefinition type2 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop1);
        assertThat(annotationTypeOperations.isSameType(type1, type2)).isTrue();
    }

    @Test
    public void compareTypes_sameExceptVersions_shouldReturnTrue() {
        AnnotationTypeDefinition type1 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop1);
        AnnotationTypeDefinition type2 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop1);
        type1.setVersion("1");
        type2.setVersion("2");
        assertThat(annotationTypeOperations.isSameType(type1, type2)).isTrue();
    }

    @Test
    public void compareTypes_differentType_shouldReturnFalse() {
        AnnotationTypeDefinition type1 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop1);
        AnnotationTypeDefinition type2 = buildAnnotationDefinition(DESCRIPTION, NEW_TYPE, prop1);
        assertThat(annotationTypeOperations.isSameType(type1, type2)).isFalse();
    }

    @Test
    public void compareTypes_differentDescription_shouldReturnFalse() {
        AnnotationTypeDefinition type1 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop1);
        AnnotationTypeDefinition type2 = buildAnnotationDefinition(NEW_DESCRIPTION, TYPE, prop1);
        assertThat(annotationTypeOperations.isSameType(type1, type2)).isFalse();
    }

    @Test
    public void compareTypes_differentProperty_shouldReturnFalse() {
        AnnotationTypeDefinition type1 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop1);
        prop2 = createSimpleProperty("val2", "prop2", "string");
        AnnotationTypeDefinition type2 = buildAnnotationDefinition(DESCRIPTION, TYPE, prop2);
        assertThat(annotationTypeOperations.isSameType(type1, type2)).isFalse();
    }

    @Test
    public void testUpdateType_propertyAdded_shouldSucceed() {
        addAnnotationType();
        prop2 = createSimpleProperty("val2", "prop2", "string");
        AnnotationTypeDefinition advancedDefinition = buildAnnotationDefinition(NEW_DESCRIPTION, TYPE, prop1, prop2);
        AnnotationTypeDefinition updatedType = annotationTypeOperations.updateType(initialAnnotationDefinition, advancedDefinition);
        assertThat(updatedType.getDescription()).isEqualTo(NEW_DESCRIPTION);
        assertThat(updatedType.getProperties())
                .usingElementComparatorOnFields("defaultValue", "name", "type")
                .containsExactlyInAnyOrder(prop1, prop2);
    }

    @Test
    public void testUpdateType_propertyDefaultValueModification_shouldSucceed() {
        addAnnotationType();
        prop2 = createSimpleProperty("val3", "prop1", "string");
        AnnotationTypeDefinition advancedDefinition = buildAnnotationDefinition(DESCRIPTION, TYPE, prop2);
        AnnotationTypeDefinition updatedType = annotationTypeOperations.updateType(initialAnnotationDefinition, advancedDefinition);
        assertThat(updatedType.getProperties())
                .usingElementComparatorOnFields("defaultValue", "name", "type")
                .containsExactlyInAnyOrder(prop2);
    }

    @Test
    public void testUpdateType_propertyDescriptionModification_shouldSucceed() {
        addAnnotationType();
        prop2 = createSimpleProperty("val1", "prop1", "string");
        prop2.setDescription("bla");
        AnnotationTypeDefinition advancedDefinition = buildAnnotationDefinition(DESCRIPTION, TYPE, prop2);
        AnnotationTypeDefinition updatedType = annotationTypeOperations.updateType(initialAnnotationDefinition, advancedDefinition);
        assertThat(updatedType.getProperties())
                .usingElementComparatorOnFields("defaultValue", "name", "type", "description")
                .containsExactlyInAnyOrder(prop2);
    }

    @Test(expected = StorageException.class)
    public void testUpdateType_propertyTypeModification_shouldFail() {
        addAnnotationType();
        prop2 = createSimpleProperty("val1", "prop1", "int");
        AnnotationTypeDefinition advancedDefinition = buildAnnotationDefinition(DESCRIPTION, TYPE, prop2);
        annotationTypeOperations.updateType(initialAnnotationDefinition, advancedDefinition);
    }

    @Test(expected = StorageException.class)
    public void testUpdateType_propertyRemoved_shouldFail() {
        addAnnotationType();
        prop2 = createSimpleProperty("val1", "prop2", "int");
        AnnotationTypeDefinition advancedDefinition = buildAnnotationDefinition(DESCRIPTION, TYPE, prop2);
        annotationTypeOperations.updateType(initialAnnotationDefinition, advancedDefinition);
    }

    private void prepareInitialType() {
        initialAnnotationDefinition = buildAnnotationDefinition(DESCRIPTION,
                TYPE,
                prop1);
        initialAnnotationDefinition.setVersion(TypeUtils.FIRST_CERTIFIED_VERSION_VERSION);
    }

    private AnnotationTypeDefinition buildAnnotationDefinition(String description, String type, PropertyDefinition ... properties) {
        AnnotationTypeDefinition annotationTypeDefinition = new AnnotationTypeDefinition();
        annotationTypeDefinition.setDescription(description);
        annotationTypeDefinition.setType(type);
        annotationTypeDefinition.setHighestVersion(true);
        annotationTypeDefinition.setProperties(asList(properties));
        return annotationTypeDefinition;
    }

}