aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/DataTypeOperationTest.java
blob: 1f448fd875294ab044883e5c97fe766565040c8c (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2021 Nordix Foundation
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *  ============LICENSE_END=========================================================
 */
package org.openecomp.sdc.be.model.operations.impl;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.when;

import fj.data.Either;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphGenericDao;
import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
import org.openecomp.sdc.be.datatypes.elements.DataTypeDataDefinition;
import org.openecomp.sdc.be.datatypes.enums.ModelTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
import org.openecomp.sdc.be.exception.OperationException;
import org.openecomp.sdc.be.exception.supplier.DataTypeOperationExceptionSupplier;
import org.openecomp.sdc.be.model.DataTypeDefinition;
import org.openecomp.sdc.be.model.Model;
import org.openecomp.sdc.be.model.PropertyDefinition;
import org.openecomp.sdc.be.resources.data.DataTypeData;
import org.springframework.test.context.ContextConfiguration;

@ContextConfiguration("classpath:application-context-test.xml")
class DataTypeOperationTest {

    @InjectMocks
    private DataTypeOperation dataTypeOperation;
    @Mock
    private ModelOperation modelOperation;
    @Mock
    private HealingJanusGraphGenericDao janusGraphGenericDao;
    @Mock
    private PropertyOperation propertyOperation;

    private final String modelName = "ETSI-SDC-MODEL-TEST";
    private final List<DataTypeData> dataTypesWithoutModel = new ArrayList<>();
    private final List<DataTypeData> dataTypesWithModel = new ArrayList<>();
    final Map<String, Map<String, DataTypeDefinition>> dataTypesMappedByModel = new HashMap<>();
    final Map<String, DataTypeDefinition> allDataTypesFoundDefinitionMap = new HashMap<>();
    private Model model;

    @BeforeEach
    void beforeEachInit() {
        MockitoAnnotations.openMocks(this);
        dataTypeOperation.setModelOperation(modelOperation);
        dataTypeOperation.setPropertyOperation(propertyOperation);
        initTestData();
    }

    @Test
    void getAllDataTypeNodesTest() {
        when(janusGraphGenericDao.getByCriteria(NodeTypeEnum.DataType, null, DataTypeData.class))
            .thenReturn(Either.left(dataTypesWithoutModel));
        when(modelOperation.findAllModels()).thenReturn(Collections.singletonList(model));
        when(janusGraphGenericDao.getByCriteriaForModel(NodeTypeEnum.DataType, null, modelName, DataTypeData.class))
            .thenReturn(Either.left(dataTypesWithModel));
        final var dataTypesFound = dataTypeOperation.getAllDataTypeNodes();
        assertThat(dataTypesFound.size()).isEqualTo(4);
        assertThat(dataTypesFound.containsAll(dataTypesWithoutModel)).isTrue();
        assertThat(dataTypesFound.containsAll(dataTypesWithModel)).isTrue();
    }

    @Test
    void getAllDataTypesWithModelTest() {
        when(janusGraphGenericDao.getByCriteria(NodeTypeEnum.DataType, null, DataTypeData.class))
            .thenReturn(Either.left(Collections.emptyList()));
        when(modelOperation.findAllModels()).thenReturn(Collections.singletonList(model));
        when(janusGraphGenericDao.getByCriteriaForModel(NodeTypeEnum.DataType, null, modelName, DataTypeData.class))
            .thenReturn(Either.left(dataTypesWithModel));
        final var dataTypesFound = dataTypeOperation.getAllDataTypeNodes();
        assertThat(dataTypesFound.size()).isEqualTo(2);
        assertThat(dataTypesFound.containsAll(dataTypesWithModel)).isTrue();
        assertThat(dataTypesFound.containsAll(dataTypesWithoutModel)).isFalse();
    }

    @Test
    void getAllDataTypeNodesWithValidationErrorTest() {
        when(janusGraphGenericDao.getByCriteria(NodeTypeEnum.DataType, null, DataTypeData.class))
            .thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
        final var dataTypesFound = dataTypeOperation.getAllDataTypeNodes();
        assertThat(dataTypesFound.isEmpty()).isTrue();
    }

    @Test
    void getAllDataTypesWithModelWithValidationErrorTest() {
        when(janusGraphGenericDao.getByCriteria(NodeTypeEnum.DataType, null, DataTypeData.class))
            .thenReturn(Either.left(Collections.emptyList()));
        when(modelOperation.findAllModels()).thenReturn(Collections.singletonList(model));
        when(janusGraphGenericDao.getByCriteriaForModel(NodeTypeEnum.DataType, null, modelName, DataTypeData.class))
            .thenReturn(Either.right(JanusGraphOperationStatus.GENERAL_ERROR));
        final var dataTypesFound = dataTypeOperation.getAllDataTypeNodes();
        assertThat(dataTypesFound).isEmpty();
    }

    @Test
    void getDataTypeByUidTest_Success() {
        doReturn(Either.left(createDataTypeData("test.data.type99", "test.data.type00099", 888L, 999L, modelName)))
            .when(janusGraphGenericDao).getNode(eq("uid"), eq("dataType"), any());
        final Optional<DataTypeDataDefinition> dataType = dataTypeOperation.getDataTypeByUid("dataType");
        assertTrue(dataType.isPresent());
        assertEquals("test.data.type99", dataType.get().getName());
        assertEquals("test.data.type00099", dataType.get().getUniqueId());
        assertEquals(modelName, dataType.get().getModel());
    }

    @Test
    void getDataTypeByUidTest_Fail() {
        doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(janusGraphGenericDao).getNode(eq("uid"), eq("dataType"), any());
        Optional<DataTypeDataDefinition> result = dataTypeOperation.getDataTypeByUid("dataType");
        assertTrue(result.isEmpty());
    }

    @Test
    void findAllPropertiesTest_Success() {
        final PropertyDefinition property1 = new PropertyDefinition();
        property1.setName("property1");
        final PropertyDefinition property2 = new PropertyDefinition();
        property2.setName("property2");
        final PropertyDefinition property3 = new PropertyDefinition();
        property3.setName("property3");

        when(propertyOperation.findPropertiesOfNode(NodeTypeEnum.DataType, "uniqueId"))
            .thenReturn(Either.left(Map.of(property3.getName(), property3, property1.getName(), property1, property2.getName(), property2)));
        final List<PropertyDefinition> dataTypeProperties = dataTypeOperation.findAllProperties("uniqueId");
        assertEquals(3, dataTypeProperties.size());
        assertEquals(property1.getName(), dataTypeProperties.get(0).getName());
        assertEquals(property2.getName(), dataTypeProperties.get(1).getName());
        assertEquals(property3.getName(), dataTypeProperties.get(2).getName());
    }

    @Test
    void findAllPropertiesTest_propertiesNotFoundSuccess() {
        when(propertyOperation.findPropertiesOfNode(NodeTypeEnum.DataType, "uniqueId"))
            .thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
        final List<PropertyDefinition> dataTypeProperties = dataTypeOperation.findAllProperties("uniqueId");
        assertTrue(dataTypeProperties.isEmpty());
    }

    @Test
    void findAllPropertiesTest_emptyPropertiesSuccess() {
        when(propertyOperation.findPropertiesOfNode(NodeTypeEnum.DataType, "uniqueId"))
            .thenReturn(Either.left(Map.of()));
        final List<PropertyDefinition> dataTypeProperties = dataTypeOperation.findAllProperties("uniqueId");
        assertTrue(dataTypeProperties.isEmpty());
    }

    @Test
    void findAllPropertiesTest_unknownError() {
        final String uniqueId = "uniqueId";
        when(propertyOperation.findPropertiesOfNode(NodeTypeEnum.DataType, uniqueId))
            .thenReturn(Either.right(JanusGraphOperationStatus.GENERAL_ERROR));
        final OperationException actualException = assertThrows(OperationException.class, () -> dataTypeOperation.findAllProperties(uniqueId));
        final OperationException expectedException =
            DataTypeOperationExceptionSupplier.unexpectedErrorWhileFetchingProperties(uniqueId).get();
        assertEquals(expectedException.getMessage(), actualException.getMessage());
    }

    private void initTestData() {
        model = new Model(modelName, ModelTypeEnum.NORMATIVE);
        final String TEST_DATA_TYPE_001 = "test.data.type001";
        final String TEST_DATA_TYPE_002 = "test.data.type002";
        final String TEST_DATA_TYPE_003 = "test.data.type003";
        final String TEST_DATA_TYPE_004 = "test.data.type004";
        final DataTypeData dataTypeData1 = createDataTypeData("test.data.type1", TEST_DATA_TYPE_001, 101L,
            101L, null);
        final DataTypeData dataTypeData2 = createDataTypeData("test.data.type2", TEST_DATA_TYPE_002, 101L,
            1002L, null);
        dataTypesWithoutModel.add(dataTypeData1);
        dataTypesWithoutModel.add(dataTypeData2);

        final DataTypeData dataTypeWithModel1 = createDataTypeData("test.data.type1", TEST_DATA_TYPE_003, 101L,
            101L, modelName);
        final DataTypeData dataTypeWithModel2 = createDataTypeData("test.data.type2", TEST_DATA_TYPE_004, 101L,
            1002L, modelName);
        dataTypesWithModel.add(dataTypeWithModel1);
        dataTypesWithModel.add(dataTypeWithModel2);

        allDataTypesFoundDefinitionMap.put(TEST_DATA_TYPE_001, createDataTypeDefinition("test.data.type1", TEST_DATA_TYPE_001,
            101L, 101L, null));
        allDataTypesFoundDefinitionMap.put(TEST_DATA_TYPE_002, createDataTypeDefinition("test.data.type2", TEST_DATA_TYPE_002,
            101L, 101L, null));
        allDataTypesFoundDefinitionMap.put(TEST_DATA_TYPE_003, createDataTypeDefinition("test.data.type1", TEST_DATA_TYPE_003,
            101L, 101L, modelName));
        allDataTypesFoundDefinitionMap.put(TEST_DATA_TYPE_004, createDataTypeDefinition("test.data.type2", TEST_DATA_TYPE_004,
            101L, 101L, modelName));

        dataTypesMappedByModel.put(null, allDataTypesFoundDefinitionMap);
    }

    private DataTypeData createDataTypeData(final String name, final String uniqueId, final long creationTime, final long modificationTime,
                                            final String model) {
        final DataTypeData dataTypeData = new DataTypeData();
        dataTypeData.setDataTypeDataDefinition(createDataTypeDefinition(name, uniqueId, creationTime, modificationTime, model));
        return dataTypeData;
    }

    private DataTypeDefinition createDataTypeDefinition(final String name, final String uniqueId, final long creationTime,
                                                        final long modificationTime, String model) {
        final DataTypeDefinition dataTypeDefinition = new DataTypeDefinition();
        dataTypeDefinition.setName(name);
        dataTypeDefinition.setUniqueId(uniqueId);
        dataTypeDefinition.setCreationTime(creationTime);
        dataTypeDefinition.setModificationTime(modificationTime);
        dataTypeDefinition.setModel(model);
        return dataTypeDefinition;
    }

}