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
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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
19 package org.openecomp.sdc.be.model.operations.impl;
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;
36 import fj.data.Either;
37 import java.nio.charset.StandardCharsets;
38 import java.util.Collections;
39 import java.util.List;
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;
71 @ContextConfiguration("classpath:application-context-test.xml")
72 class ModelOperationTest extends ModelTestBase {
75 private ModelOperation modelOperation;
77 private JanusGraphGenericDao janusGraphGenericDao;
79 private JanusGraphDao janusGraphDao;
81 private ToscaModelImportCassandraDao toscaModelImportCassandraDao;
83 private DerivedFromOperation derivedFromOperation;
85 private final String modelName = "ETSI-SDC-MODEL-TEST";
88 static void beforeAllInit() {
93 void beforeEachInit() {
94 MockitoAnnotations.openMocks(this);
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);
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));
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()));
119 final Model createdModel = modelOperation.createModel(new Model(derivedModelName, modelName, ModelTypeEnum.NORMATIVE), false);
120 assertThat(createdModel).isNotNull();
121 assertThat(createdModel.getName()).isEqualTo(derivedModelName);
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));
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));
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));
149 modelOperation.createModelImports(modelId, zipContent);
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);
161 verify(toscaModelImportCassandraDao).importAll(modelId, toscaImportByModelList);
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());
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));
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";
194 final Optional<GraphVertex> modelVertexByNameOpt = modelOperation.findModelVertexByName(modelName);
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));
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";
209 final var actualException = assertThrows(OperationException.class, () -> modelOperation.findModelVertexByName(modelName));
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));
218 void findModelVertexTest_emptyOrNullModelName() {
219 assertTrue(modelOperation.findModelVertexByName("").isEmpty());
220 assertTrue(modelOperation.findModelVertexByName(null).isEmpty());
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);
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));
239 final Model expectedModel = new Model(modelName, ModelTypeEnum.NORMATIVE);
240 assertTrue(modelByNameOpt.isPresent());
241 assertEquals(expectedModel, modelByNameOpt.get());
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());
255 void findModelByNameTest_emptyOrNullModelName() {
256 assertTrue(modelOperation.findModelByName("").isEmpty());
257 assertTrue(modelOperation.findModelByName(null).isEmpty());
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));
269 final List<Model> actualModelList = modelOperation.findAllModels();
270 assertFalse(actualModelList.isEmpty());
271 assertEquals(1, actualModelList.size());
272 assertEquals(modelName, actualModelList.get(0).getName());
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());
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());
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());