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.NodeTypeEnum;
62 import org.openecomp.sdc.be.model.Model;
63 import org.openecomp.sdc.be.model.ModelTestBase;
64 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.ModelOperationExceptionSupplier;
65 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.OperationException;
66 import org.openecomp.sdc.be.model.operations.api.DerivedFromOperation;
67 import org.openecomp.sdc.be.resources.data.ModelData;
68 import org.springframework.test.context.ContextConfiguration;
70 @ContextConfiguration("classpath:application-context-test.xml")
71 class ModelOperationTest extends ModelTestBase {
74 private ModelOperation modelOperation;
76 private JanusGraphGenericDao janusGraphGenericDao;
78 private JanusGraphDao janusGraphDao;
80 private ToscaModelImportCassandraDao toscaModelImportCassandraDao;
82 private DerivedFromOperation derivedFromOperation;
84 private final String modelName = "ETSI-SDC-MODEL-TEST";
87 static void beforeAllInit() {
92 void beforeEachInit() {
93 MockitoAnnotations.openMocks(this);
97 void createModelSuccessTest() {
98 final ModelData modelData = new ModelData(modelName, UniqueIdBuilder.buildModelUid(modelName));
99 when(janusGraphGenericDao.createNode(any(),any())).thenReturn(Either.left(modelData));
100 final Model createdModel = modelOperation.createModel(new Model(modelName), false);
101 assertThat(createdModel).isNotNull();
102 assertThat(createdModel.getName()).isEqualTo(modelName);
106 void createDerivedModelSuccessTest() {
107 final String derivedModelName = "derivedModel";
108 final ModelData modelData = new ModelData(derivedModelName, UniqueIdBuilder.buildModelUid(derivedModelName));
109 when(janusGraphGenericDao.createNode(any(),any())).thenReturn(Either.left(modelData));
111 final GraphVertex modelVertex = new GraphVertex();
112 modelVertex.addMetadataProperty(GraphPropertyEnum.NAME, "baseModel");
113 when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.MODEL), anyMap())).thenReturn(Either.left(Collections.singletonList(modelVertex)));
114 when(janusGraphGenericDao.getChild(eq("uid"), anyString(), eq(GraphEdgeLabels.DERIVED_FROM), eq(NodeTypeEnum.Model), eq(ModelData.class))).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
115 when(derivedFromOperation.addDerivedFromRelation("model.derivedModel", "model.baseModel", NodeTypeEnum.Model)).thenReturn(Either.left(new GraphRelation()));
117 final Model createdModel = modelOperation.createModel(new Model(derivedModelName, modelName), false);
118 assertThat(createdModel).isNotNull();
119 assertThat(createdModel.getName()).isEqualTo(derivedModelName);
123 void createModelFailWithModelAlreadyExistTest() {
124 when(janusGraphGenericDao.createNode(any(),any())).thenReturn(Either.right(JanusGraphOperationStatus.JANUSGRAPH_SCHEMA_VIOLATION));
125 final var model = new Model(modelName);
126 assertThrows(OperationException.class, () -> modelOperation.createModel(model, false));
130 void createModelFailTest() {
131 when(janusGraphGenericDao.createNode(any(),any())).thenReturn(Either.right(JanusGraphOperationStatus.GRAPH_IS_NOT_AVAILABLE));
132 final var model = new Model(modelName);
133 assertThrows(OperationException.class, () -> modelOperation.createModel(model, false));
137 void createModelImportsSuccessTest() {
138 var modelId = "modelId";
139 var contentEntry1 = "contentEntry1";
140 var pathEntry1 = "entry1";
141 var contentEntry2 = "contentEntry2";
142 var pathEntry2 = "entry2/path";
143 final Map<String, byte[]> zipContent = new TreeMap<>();
144 zipContent.put(pathEntry1, contentEntry1.getBytes(StandardCharsets.UTF_8));
145 zipContent.put(pathEntry2, contentEntry2.getBytes(StandardCharsets.UTF_8));
147 modelOperation.createModelImports(modelId, zipContent);
149 final var toscaImport1 = new ToscaImportByModel();
150 toscaImport1.setModelId(modelId);
151 toscaImport1.setContent(contentEntry1);
152 toscaImport1.setFullPath(pathEntry1);
153 final var toscaImport2 = new ToscaImportByModel();
154 toscaImport2.setModelId(modelId);
155 toscaImport2.setContent(contentEntry2);
156 toscaImport2.setFullPath(pathEntry2);
157 final List<ToscaImportByModel> toscaImportByModelList = List.of(toscaImport1, toscaImport2);
159 verify(toscaModelImportCassandraDao).importAll(modelId, toscaImportByModelList);
163 void createModelImportsTest_emptyZipContent() {
164 var modelId = "modelId";
165 modelOperation.createModelImports(modelId, Collections.emptyMap());
166 verify(toscaModelImportCassandraDao, never()).importAll(eq(modelId), anyList());
167 modelOperation.createModelImports(modelId, null);
168 verify(toscaModelImportCassandraDao, never()).importAll(eq(null), anyList());
172 void findModelVertexSuccessTest() {
173 final ArgumentCaptor<Map<GraphPropertyEnum, Object>> mapArgumentCaptor = ArgumentCaptor.forClass(Map.class);
174 final GraphVertex expectedVertex = new GraphVertex();
175 when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.MODEL), mapArgumentCaptor.capture())).thenReturn(Either.left(List.of(expectedVertex)));
176 var modelName = "modelName";
177 final Optional<GraphVertex> modelVertexByNameOpt = modelOperation.findModelVertexByName(modelName);
178 assertTrue(modelVertexByNameOpt.isPresent());
179 assertEquals(expectedVertex, modelVertexByNameOpt.get());
180 final Map<GraphPropertyEnum, Object> value = mapArgumentCaptor.getValue();
181 assertEquals(modelName, value.get(GraphPropertyEnum.NAME));
182 assertEquals(UniqueIdBuilder.buildModelUid(modelName), value.get(GraphPropertyEnum.UNIQUE_ID));
186 void findModelVertexTest_modelNotFound() {
187 final ArgumentCaptor<Map<GraphPropertyEnum, Object>> mapArgumentCaptor = ArgumentCaptor.forClass(Map.class);
188 when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.MODEL), mapArgumentCaptor.capture()))
189 .thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
190 var modelName = "modelName";
192 final Optional<GraphVertex> modelVertexByNameOpt = modelOperation.findModelVertexByName(modelName);
194 assertTrue(modelVertexByNameOpt.isEmpty());
195 final Map<GraphPropertyEnum, Object> value = mapArgumentCaptor.getValue();
196 assertEquals(modelName, value.get(GraphPropertyEnum.NAME));
197 assertEquals(UniqueIdBuilder.buildModelUid(modelName), value.get(GraphPropertyEnum.UNIQUE_ID));
201 void findModelVertexTest_janusGraphError() {
202 final ArgumentCaptor<Map<GraphPropertyEnum, Object>> mapArgumentCaptor = ArgumentCaptor.forClass(Map.class);
203 when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.MODEL), mapArgumentCaptor.capture()))
204 .thenReturn(Either.right(JanusGraphOperationStatus.GENERAL_ERROR));
205 var modelName = "modelName";
207 final var actualException = assertThrows(OperationException.class, () -> modelOperation.findModelVertexByName(modelName));
209 assertEquals(ActionStatus.GENERAL_ERROR, actualException.getActionStatus());
210 final Map<GraphPropertyEnum, Object> value = mapArgumentCaptor.getValue();
211 assertEquals(modelName, value.get(GraphPropertyEnum.NAME));
212 assertEquals(UniqueIdBuilder.buildModelUid(modelName), value.get(GraphPropertyEnum.UNIQUE_ID));
216 void findModelVertexTest_emptyOrNullModelName() {
217 assertTrue(modelOperation.findModelVertexByName("").isEmpty());
218 assertTrue(modelOperation.findModelVertexByName(null).isEmpty());
222 void findModelByNameSuccessTest() {
223 final ArgumentCaptor<Map<GraphPropertyEnum, Object>> mapArgumentCaptor = ArgumentCaptor.forClass(Map.class);
224 var modelName = "modelName";
225 final GraphVertex expectedVertex = mock(GraphVertex.class);
226 when(expectedVertex.getMetadataProperty(GraphPropertyEnum.NAME)).thenReturn(modelName);
227 when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.MODEL), mapArgumentCaptor.capture())).thenReturn(Either.left(List.of(expectedVertex)));
228 when(janusGraphGenericDao.getChild("uid", UniqueIdBuilder.buildModelUid(modelName), GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.Model,
229 ModelData.class)).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
230 final Optional<Model> modelByNameOpt = modelOperation.findModelByName(modelName);
232 final Map<GraphPropertyEnum, Object> value = mapArgumentCaptor.getValue();
233 assertEquals(modelName, value.get(GraphPropertyEnum.NAME));
234 assertEquals(UniqueIdBuilder.buildModelUid(modelName), value.get(GraphPropertyEnum.UNIQUE_ID));
236 final Model expectedModel = new Model(modelName);
237 assertTrue(modelByNameOpt.isPresent());
238 assertEquals(expectedModel, modelByNameOpt.get());
242 void findModelByNameTest_modelNameNotFound() {
243 final ArgumentCaptor<Map<GraphPropertyEnum, Object>> mapArgumentCaptor = ArgumentCaptor.forClass(Map.class);
244 var modelName = "modelName";
245 when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.MODEL), mapArgumentCaptor.capture()))
246 .thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
247 final Optional<Model> modelByNameOpt = modelOperation.findModelByName(modelName);
248 assertTrue(modelByNameOpt.isEmpty());
252 void findModelByNameTest_emptyOrNullModelName() {
253 assertTrue(modelOperation.findModelByName("").isEmpty());
254 assertTrue(modelOperation.findModelByName(null).isEmpty());
258 void findAllModelsSuccessTest() {
259 final GraphVertex expectedVertex = mock(GraphVertex.class);
260 when(expectedVertex.getMetadataProperty(GraphPropertyEnum.NAME)).thenReturn(modelName);
261 when(janusGraphDao.getByCriteria(VertexTypeEnum.MODEL, Collections.emptyMap())).thenReturn(Either.left(List.of(expectedVertex)));
262 when(janusGraphGenericDao.getChild("uid", UniqueIdBuilder.buildModelUid(modelName), GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.Model,
263 ModelData.class)).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
265 final List<Model> actualModelList = modelOperation.findAllModels();
266 assertFalse(actualModelList.isEmpty());
267 assertEquals(1, actualModelList.size());
268 assertEquals(modelName, actualModelList.get(0).getName());
272 void findAllModelsTest_noModelsFound() {
273 when(janusGraphDao.getByCriteria(VertexTypeEnum.MODEL, Collections.emptyMap())).thenReturn(Either.left(Collections.emptyList()));
274 final List<Model> actualModelList = modelOperation.findAllModels();
275 assertTrue(actualModelList.isEmpty());
279 void findAllModelsTest_janusGraphNotFound() {
280 when(janusGraphDao.getByCriteria(VertexTypeEnum.MODEL, Collections.emptyMap()))
281 .thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
282 final List<Model> actualModelList = modelOperation.findAllModels();
283 assertTrue(actualModelList.isEmpty());
287 void findAllModelsTest_janusGraphError() {
288 when(janusGraphDao.getByCriteria(VertexTypeEnum.MODEL, Collections.emptyMap()))
289 .thenReturn(Either.right(JanusGraphOperationStatus.GENERAL_ERROR));
290 final var actualException = assertThrows(OperationException.class, () -> modelOperation.findAllModels());
291 final var expectedException = ModelOperationExceptionSupplier.failedToRetrieveModels(JanusGraphOperationStatus.GENERAL_ERROR).get();
292 assertEquals(expectedException.getMessage(), actualException.getMessage());