Support querying of model by type
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / operations / impl / ModelOperationTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19 package org.openecomp.sdc.be.model.operations.impl;
20
21 import static org.assertj.core.api.Assertions.assertThat;
22 import static org.junit.jupiter.api.Assertions.assertEquals;
23 import static org.junit.jupiter.api.Assertions.assertFalse;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.anyList;
28 import static org.mockito.ArgumentMatchers.anyMap;
29 import static org.mockito.ArgumentMatchers.anyString;
30 import static org.mockito.ArgumentMatchers.eq;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.never;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.when;
35
36 import fj.data.Either;
37 import java.nio.charset.StandardCharsets;
38 import java.util.Collections;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Optional;
42 import java.util.TreeMap;
43 import org.junit.jupiter.api.BeforeAll;
44 import org.junit.jupiter.api.BeforeEach;
45 import org.junit.jupiter.api.Test;
46 import org.mockito.ArgumentCaptor;
47 import org.mockito.InjectMocks;
48 import org.mockito.Mock;
49 import org.mockito.MockitoAnnotations;
50 import org.openecomp.sdc.be.dao.api.ActionStatus;
51 import org.openecomp.sdc.be.dao.cassandra.ToscaModelImportCassandraDao;
52 import org.openecomp.sdc.be.dao.graph.datatype.GraphRelation;
53 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao;
54 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
55 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
56 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
57 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
58 import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels;
59 import org.openecomp.sdc.be.data.model.ToscaImportByModel;
60 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
61 import org.openecomp.sdc.be.datatypes.enums.ModelTypeEnum;
62 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
63 import org.openecomp.sdc.be.model.Model;
64 import org.openecomp.sdc.be.model.ModelTestBase;
65 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.ModelOperationExceptionSupplier;
66 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.OperationException;
67 import org.openecomp.sdc.be.model.operations.api.DerivedFromOperation;
68 import org.openecomp.sdc.be.resources.data.ModelData;
69 import org.springframework.test.context.ContextConfiguration;
70
71 @ContextConfiguration("classpath:application-context-test.xml")
72 class ModelOperationTest extends ModelTestBase {
73
74     @InjectMocks
75     private ModelOperation modelOperation;
76     @Mock
77     private JanusGraphGenericDao janusGraphGenericDao;
78     @Mock
79     private JanusGraphDao janusGraphDao;
80     @Mock
81     private ToscaModelImportCassandraDao toscaModelImportCassandraDao;
82     @Mock
83     private DerivedFromOperation derivedFromOperation;
84
85     private final String modelName = "ETSI-SDC-MODEL-TEST";
86
87     @BeforeAll
88     static void beforeAllInit() {
89         init();
90     }
91
92     @BeforeEach
93     void beforeEachInit() {
94         MockitoAnnotations.openMocks(this);
95     }
96
97     @Test
98     void createModelSuccessTest() {
99         final ModelData modelData = new ModelData(modelName,  UniqueIdBuilder.buildModelUid(modelName), ModelTypeEnum.NORMATIVE);
100         when(janusGraphGenericDao.createNode(any(),any())).thenReturn(Either.left(modelData));
101         final Model createdModel = modelOperation.createModel(new Model(modelName, ModelTypeEnum.NORMATIVE), false);
102         assertThat(createdModel).isNotNull();
103         assertThat(createdModel.getName()).isEqualTo(modelName);
104     }
105     
106     @Test
107     void createDerivedModelSuccessTest() {
108         final String derivedModelName = "derivedModel";
109         final ModelData modelData = new ModelData(derivedModelName,  UniqueIdBuilder.buildModelUid(derivedModelName), ModelTypeEnum.NORMATIVE);
110         when(janusGraphGenericDao.createNode(any(),any())).thenReturn(Either.left(modelData));
111         
112         final GraphVertex modelVertex = new GraphVertex();
113         modelVertex.addMetadataProperty(GraphPropertyEnum.NAME, "baseModel");
114         modelVertex.addMetadataProperty(GraphPropertyEnum.MODEL_TYPE, ModelTypeEnum.NORMATIVE.getValue());
115         when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.MODEL), anyMap())).thenReturn(Either.left(Collections.singletonList(modelVertex)));
116         when(janusGraphGenericDao.getChild(eq("uid"), anyString(), eq(GraphEdgeLabels.DERIVED_FROM), eq(NodeTypeEnum.Model), eq(ModelData.class))).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
117         when(derivedFromOperation.addDerivedFromRelation("model.derivedModel", "model.baseModel", NodeTypeEnum.Model)).thenReturn(Either.left(new GraphRelation()));
118         
119         final Model createdModel = modelOperation.createModel(new Model(derivedModelName, modelName, ModelTypeEnum.NORMATIVE), false);
120         assertThat(createdModel).isNotNull();
121         assertThat(createdModel.getName()).isEqualTo(derivedModelName);
122     }
123
124     @Test
125     void createModelFailWithModelAlreadyExistTest() {
126         when(janusGraphGenericDao.createNode(any(),any())).thenReturn(Either.right(JanusGraphOperationStatus.JANUSGRAPH_SCHEMA_VIOLATION));
127         final var model = new Model(modelName, ModelTypeEnum.NORMATIVE);
128         assertThrows(OperationException.class, () -> modelOperation.createModel(model, false));
129     }
130
131     @Test
132     void createModelFailTest() {
133         when(janusGraphGenericDao.createNode(any(),any())).thenReturn(Either.right(JanusGraphOperationStatus.GRAPH_IS_NOT_AVAILABLE));
134         final var model = new Model(modelName, ModelTypeEnum.NORMATIVE);
135         assertThrows(OperationException.class, () -> modelOperation.createModel(model, false));
136     }
137
138     @Test
139     void createModelImportsSuccessTest() {
140         var modelId = "modelId";
141         var contentEntry1 = "contentEntry1";
142         var pathEntry1 = "entry1";
143         var contentEntry2 = "contentEntry2";
144         var pathEntry2 = "entry2/path";
145         final Map<String, byte[]> zipContent = new TreeMap<>();
146         zipContent.put(pathEntry1, contentEntry1.getBytes(StandardCharsets.UTF_8));
147         zipContent.put(pathEntry2, contentEntry2.getBytes(StandardCharsets.UTF_8));
148
149         modelOperation.createModelImports(modelId, zipContent);
150
151         final var toscaImport1 = new ToscaImportByModel();
152         toscaImport1.setModelId(modelId);
153         toscaImport1.setContent(contentEntry1);
154         toscaImport1.setFullPath(pathEntry1);
155         final var toscaImport2 = new ToscaImportByModel();
156         toscaImport2.setModelId(modelId);
157         toscaImport2.setContent(contentEntry2);
158         toscaImport2.setFullPath(pathEntry2);
159         final List<ToscaImportByModel> toscaImportByModelList = List.of(toscaImport1, toscaImport2);
160
161         verify(toscaModelImportCassandraDao).importAll(modelId, toscaImportByModelList);
162     }
163
164     @Test
165     void createModelImportsTest_emptyZipContent() {
166         var modelId = "modelId";
167         modelOperation.createModelImports(modelId, Collections.emptyMap());
168         verify(toscaModelImportCassandraDao, never()).importAll(eq(modelId), anyList());
169         modelOperation.createModelImports(modelId, null);
170         verify(toscaModelImportCassandraDao, never()).importAll(eq(null), anyList());
171     }
172
173     @Test
174     void findModelVertexSuccessTest() {
175         final ArgumentCaptor<Map<GraphPropertyEnum, Object>> mapArgumentCaptor = ArgumentCaptor.forClass(Map.class);
176         final GraphVertex expectedVertex = new GraphVertex();
177         when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.MODEL), mapArgumentCaptor.capture())).thenReturn(Either.left(List.of(expectedVertex)));
178         var modelName = "modelName";
179         final Optional<GraphVertex> modelVertexByNameOpt = modelOperation.findModelVertexByName(modelName);
180         assertTrue(modelVertexByNameOpt.isPresent());
181         assertEquals(expectedVertex, modelVertexByNameOpt.get());
182         final Map<GraphPropertyEnum, Object> value = mapArgumentCaptor.getValue();
183         assertEquals(modelName, value.get(GraphPropertyEnum.NAME));
184         assertEquals(UniqueIdBuilder.buildModelUid(modelName), value.get(GraphPropertyEnum.UNIQUE_ID));
185     }
186
187     @Test
188     void findModelVertexTest_modelNotFound() {
189         final ArgumentCaptor<Map<GraphPropertyEnum, Object>> mapArgumentCaptor = ArgumentCaptor.forClass(Map.class);
190         when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.MODEL), mapArgumentCaptor.capture()))
191             .thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
192         var modelName = "modelName";
193
194         final Optional<GraphVertex> modelVertexByNameOpt = modelOperation.findModelVertexByName(modelName);
195
196         assertTrue(modelVertexByNameOpt.isEmpty());
197         final Map<GraphPropertyEnum, Object> value = mapArgumentCaptor.getValue();
198         assertEquals(modelName, value.get(GraphPropertyEnum.NAME));
199         assertEquals(UniqueIdBuilder.buildModelUid(modelName), value.get(GraphPropertyEnum.UNIQUE_ID));
200     }
201
202     @Test
203     void findModelVertexTest_janusGraphError() {
204         final ArgumentCaptor<Map<GraphPropertyEnum, Object>> mapArgumentCaptor = ArgumentCaptor.forClass(Map.class);
205         when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.MODEL), mapArgumentCaptor.capture()))
206             .thenReturn(Either.right(JanusGraphOperationStatus.GENERAL_ERROR));
207         var modelName = "modelName";
208
209         final var actualException = assertThrows(OperationException.class, () -> modelOperation.findModelVertexByName(modelName));
210
211         assertEquals(ActionStatus.GENERAL_ERROR, actualException.getActionStatus());
212         final Map<GraphPropertyEnum, Object> value = mapArgumentCaptor.getValue();
213         assertEquals(modelName, value.get(GraphPropertyEnum.NAME));
214         assertEquals(UniqueIdBuilder.buildModelUid(modelName), value.get(GraphPropertyEnum.UNIQUE_ID));
215     }
216
217     @Test
218     void findModelVertexTest_emptyOrNullModelName() {
219         assertTrue(modelOperation.findModelVertexByName("").isEmpty());
220         assertTrue(modelOperation.findModelVertexByName(null).isEmpty());
221     }
222
223     @Test
224     void findModelByNameSuccessTest() {
225         final ArgumentCaptor<Map<GraphPropertyEnum, Object>> mapArgumentCaptor = ArgumentCaptor.forClass(Map.class);
226         var modelName = "modelName";
227         final GraphVertex expectedVertex = mock(GraphVertex.class);
228         when(expectedVertex.getMetadataProperty(GraphPropertyEnum.NAME)).thenReturn(modelName);
229         when(expectedVertex.getMetadataProperty(GraphPropertyEnum.MODEL_TYPE)).thenReturn(ModelTypeEnum.NORMATIVE.getValue());
230         when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.MODEL), mapArgumentCaptor.capture())).thenReturn(Either.left(List.of(expectedVertex)));
231         when(janusGraphGenericDao.getChild("uid", UniqueIdBuilder.buildModelUid(modelName), GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.Model,
232             ModelData.class)).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
233         final Optional<Model> modelByNameOpt = modelOperation.findModelByName(modelName);
234
235         final Map<GraphPropertyEnum, Object> value = mapArgumentCaptor.getValue();
236         assertEquals(modelName, value.get(GraphPropertyEnum.NAME));
237         assertEquals(UniqueIdBuilder.buildModelUid(modelName), value.get(GraphPropertyEnum.UNIQUE_ID));
238
239         final Model expectedModel = new Model(modelName, ModelTypeEnum.NORMATIVE);
240         assertTrue(modelByNameOpt.isPresent());
241         assertEquals(expectedModel, modelByNameOpt.get());
242     }
243
244     @Test
245     void findModelByNameTest_modelNameNotFound() {
246         final ArgumentCaptor<Map<GraphPropertyEnum, Object>> mapArgumentCaptor = ArgumentCaptor.forClass(Map.class);
247         var modelName = "modelName";
248         when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.MODEL), mapArgumentCaptor.capture()))
249             .thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
250         final Optional<Model> modelByNameOpt = modelOperation.findModelByName(modelName);
251         assertTrue(modelByNameOpt.isEmpty());
252     }
253
254     @Test
255     void findModelByNameTest_emptyOrNullModelName() {
256         assertTrue(modelOperation.findModelByName("").isEmpty());
257         assertTrue(modelOperation.findModelByName(null).isEmpty());
258     }
259
260     @Test
261     void findAllModelsSuccessTest() {
262         final GraphVertex expectedVertex = mock(GraphVertex.class);
263         when(expectedVertex.getMetadataProperty(GraphPropertyEnum.NAME)).thenReturn(modelName);
264         when(expectedVertex.getMetadataProperty(GraphPropertyEnum.MODEL_TYPE)).thenReturn(ModelTypeEnum.NORMATIVE.getValue());
265         when(janusGraphDao.getByCriteria(VertexTypeEnum.MODEL, Collections.emptyMap())).thenReturn(Either.left(List.of(expectedVertex)));
266         when(janusGraphGenericDao.getChild("uid", UniqueIdBuilder.buildModelUid(modelName), GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.Model,
267             ModelData.class)).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
268
269         final List<Model> actualModelList = modelOperation.findAllModels();
270         assertFalse(actualModelList.isEmpty());
271         assertEquals(1, actualModelList.size());
272         assertEquals(modelName, actualModelList.get(0).getName());
273     }
274
275     @Test
276     void findAllModelsTest_noModelsFound() {
277         when(janusGraphDao.getByCriteria(VertexTypeEnum.MODEL, Collections.emptyMap())).thenReturn(Either.left(Collections.emptyList()));
278         final List<Model> actualModelList = modelOperation.findAllModels();
279         assertTrue(actualModelList.isEmpty());
280     }
281
282     @Test
283     void findAllModelsTest_janusGraphNotFound() {
284         when(janusGraphDao.getByCriteria(VertexTypeEnum.MODEL, Collections.emptyMap()))
285             .thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
286         final List<Model> actualModelList = modelOperation.findAllModels();
287         assertTrue(actualModelList.isEmpty());
288     }
289
290     @Test
291     void findAllModelsTest_janusGraphError() {
292         when(janusGraphDao.getByCriteria(VertexTypeEnum.MODEL, Collections.emptyMap()))
293             .thenReturn(Either.right(JanusGraphOperationStatus.GENERAL_ERROR));
294         final var actualException = assertThrows(OperationException.class, () -> modelOperation.findAllModels());
295         final var expectedException = ModelOperationExceptionSupplier.failedToRetrieveModels(JanusGraphOperationStatus.GENERAL_ERROR).get();
296         assertEquals(expectedException.getMessage(), actualException.getMessage());
297     }
298 }