aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/DataTypeOperationTest.java
blob: c8a2b03fb38d8b67d403f965bcd38df35e9785be (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
/*
 * ============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.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 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.enums.ModelTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
import org.openecomp.sdc.be.model.DataTypeDefinition;
import org.openecomp.sdc.be.model.Model;
import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
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 ApplicationDataTypeCache applicationDataTypeCache;

    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);
        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 mapDataTypesDefinitionByModelTest() {
        final var allDataTypesMappedByModel =
            dataTypeOperation.mapDataTypesDefinitionByModel(allDataTypesFoundDefinitionMap);
        assertThat(allDataTypesMappedByModel.get(modelName).size()).isEqualTo(2);
        assertThat(allDataTypesMappedByModel.get(null)).isNotEmpty();
    }


    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;
    }

}