Add sdc-be-init support for artifact types
[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.assertNotNull;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.ArgumentMatchers.anyList;
29 import static org.mockito.ArgumentMatchers.anyMap;
30 import static org.mockito.ArgumentMatchers.anyString;
31 import static org.mockito.ArgumentMatchers.eq;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.never;
34 import static org.mockito.Mockito.verify;
35 import static org.mockito.Mockito.when;
36 import static org.openecomp.sdc.be.model.operations.impl.ModelOperation.ADDITIONAL_TYPE_DEFINITIONS_PATH;
37
38 import fj.data.Either;
39 import java.io.IOException;
40 import java.nio.charset.StandardCharsets;
41 import java.nio.file.Files;
42 import java.nio.file.Path;
43 import java.util.ArrayList;
44 import java.util.Collections;
45 import java.util.List;
46 import java.util.Map;
47 import java.util.Optional;
48 import java.util.TreeMap;
49 import java.util.stream.Stream;
50 import org.apache.commons.lang3.tuple.ImmutablePair;
51 import org.junit.jupiter.api.BeforeAll;
52 import org.junit.jupiter.api.BeforeEach;
53 import org.junit.jupiter.api.Test;
54 import org.mockito.ArgumentCaptor;
55 import org.mockito.InjectMocks;
56 import org.mockito.Mock;
57 import org.mockito.MockitoAnnotations;
58 import org.openecomp.sdc.be.dao.api.ActionStatus;
59 import org.openecomp.sdc.be.dao.cassandra.ToscaModelImportCassandraDao;
60 import org.openecomp.sdc.be.dao.graph.datatype.GraphEdge;
61 import org.openecomp.sdc.be.dao.graph.datatype.GraphRelation;
62 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
63 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao;
64 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
65 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
66 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
67 import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels;
68 import org.openecomp.sdc.be.data.model.ToscaImportByModel;
69 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
70 import org.openecomp.sdc.be.datatypes.enums.ModelTypeEnum;
71 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
72 import org.openecomp.sdc.be.model.Model;
73 import org.openecomp.sdc.be.model.ModelTestBase;
74 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.ModelOperationExceptionSupplier;
75 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.OperationException;
76 import org.openecomp.sdc.be.model.normatives.ElementTypeEnum;
77 import org.openecomp.sdc.be.model.operations.api.DerivedFromOperation;
78 import org.openecomp.sdc.be.resources.data.ModelData;
79 import org.springframework.test.context.ContextConfiguration;
80
81 @ContextConfiguration("classpath:application-context-test.xml")
82 class ModelOperationTest extends ModelTestBase {
83
84     private static final String modelName = "ETSI-SDC-MODEL-TEST";
85     @InjectMocks
86     private ModelOperation modelOperation;
87     @Mock
88     private JanusGraphGenericDao janusGraphGenericDao;
89     @Mock
90     private JanusGraphDao janusGraphDao;
91     @Mock
92     private ToscaModelImportCassandraDao toscaModelImportCassandraDao;
93     @Mock
94     private DerivedFromOperation derivedFromOperation;
95
96     @BeforeAll
97     static void beforeAllInit() {
98         init();
99     }
100
101     @BeforeEach
102     void beforeEachInit() {
103         MockitoAnnotations.openMocks(this);
104     }
105
106     @Test
107     void createModelSuccessTest() {
108         final ModelData modelData = new ModelData(modelName, UniqueIdBuilder.buildModelUid(modelName), ModelTypeEnum.NORMATIVE);
109         when(janusGraphGenericDao.createNode(any(), any())).thenReturn(Either.left(modelData));
110         final Model createdModel = modelOperation.createModel(new Model(modelName, ModelTypeEnum.NORMATIVE), false);
111         assertThat(createdModel).isNotNull();
112         assertThat(createdModel.getName()).isEqualTo(modelName);
113     }
114
115     @Test
116     void createDerivedModelSuccessTest() {
117         final String derivedModelName = "derivedModel";
118         final ModelData modelData = new ModelData(derivedModelName, UniqueIdBuilder.buildModelUid(derivedModelName), ModelTypeEnum.NORMATIVE);
119         when(janusGraphGenericDao.createNode(any(), any())).thenReturn(Either.left(modelData));
120
121         final GraphVertex modelVertex = new GraphVertex();
122         modelVertex.addMetadataProperty(GraphPropertyEnum.NAME, "baseModel");
123         modelVertex.addMetadataProperty(GraphPropertyEnum.MODEL_TYPE, ModelTypeEnum.NORMATIVE.getValue());
124         when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.MODEL), anyMap())).thenReturn(Either.left(Collections.singletonList(modelVertex)));
125         when(janusGraphGenericDao.getChild(eq("uid"), anyString(), eq(GraphEdgeLabels.DERIVED_FROM), eq(NodeTypeEnum.Model),
126             eq(ModelData.class))).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
127         when(derivedFromOperation.addDerivedFromRelation("model.derivedModel", "model.baseModel", NodeTypeEnum.Model)).thenReturn(
128             Either.left(new GraphRelation()));
129
130         final Model createdModel = modelOperation.createModel(new Model(derivedModelName, modelName, ModelTypeEnum.NORMATIVE), false);
131         assertThat(createdModel).isNotNull();
132         assertThat(createdModel.getName()).isEqualTo(derivedModelName);
133     }
134
135     @Test
136     void createModelFailWithModelAlreadyExistTest() {
137         when(janusGraphGenericDao.createNode(any(), any())).thenReturn(Either.right(JanusGraphOperationStatus.JANUSGRAPH_SCHEMA_VIOLATION));
138         final var model = new Model(modelName, ModelTypeEnum.NORMATIVE);
139         assertThrows(OperationException.class, () -> modelOperation.createModel(model, false));
140     }
141
142     @Test
143     void createModelFailTest() {
144         when(janusGraphGenericDao.createNode(any(), any())).thenReturn(Either.right(JanusGraphOperationStatus.GRAPH_IS_NOT_AVAILABLE));
145         final var model = new Model(modelName, ModelTypeEnum.NORMATIVE);
146         assertThrows(OperationException.class, () -> modelOperation.createModel(model, false));
147     }
148
149     @Test
150     void createModelImportsSuccessTest() {
151         var modelId = "modelId";
152         var contentEntry1 = "contentEntry1";
153         var pathEntry1 = "entry1";
154         var contentEntry2 = "contentEntry2";
155         var pathEntry2 = "entry2/path";
156         final Map<String, byte[]> zipContent = new TreeMap<>();
157         zipContent.put(pathEntry1, contentEntry1.getBytes(StandardCharsets.UTF_8));
158         zipContent.put(pathEntry2, contentEntry2.getBytes(StandardCharsets.UTF_8));
159
160         modelOperation.createModelImports(modelId, zipContent);
161
162         final var toscaImport1 = new ToscaImportByModel();
163         toscaImport1.setModelId(modelId);
164         toscaImport1.setContent(contentEntry1);
165         toscaImport1.setFullPath(pathEntry1);
166         final var toscaImport2 = new ToscaImportByModel();
167         toscaImport2.setModelId(modelId);
168         toscaImport2.setContent(contentEntry2);
169         toscaImport2.setFullPath(pathEntry2);
170         final List<ToscaImportByModel> toscaImportByModelList = List.of(toscaImport1, toscaImport2);
171
172         verify(toscaModelImportCassandraDao).replaceImports(modelId, toscaImportByModelList);
173     }
174
175     @Test
176     void createModelImportsTest_emptyZipContent() {
177         var modelId = "modelId";
178         modelOperation.createModelImports(modelId, Collections.emptyMap());
179         verify(toscaModelImportCassandraDao, never()).replaceImports(eq(modelId), anyList());
180         modelOperation.createModelImports(modelId, null);
181         verify(toscaModelImportCassandraDao, never()).replaceImports(eq(null), anyList());
182     }
183
184     @Test
185     void findModelVertexSuccessTest() {
186         final ArgumentCaptor<Map<GraphPropertyEnum, Object>> mapArgumentCaptor = ArgumentCaptor.forClass(Map.class);
187         final GraphVertex expectedVertex = new GraphVertex();
188         when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.MODEL), mapArgumentCaptor.capture())).thenReturn(Either.left(List.of(expectedVertex)));
189         var modelName = "modelName";
190         final Optional<GraphVertex> modelVertexByNameOpt = modelOperation.findModelVertexByName(modelName);
191         assertTrue(modelVertexByNameOpt.isPresent());
192         assertEquals(expectedVertex, modelVertexByNameOpt.get());
193         final Map<GraphPropertyEnum, Object> value = mapArgumentCaptor.getValue();
194         assertEquals(modelName, value.get(GraphPropertyEnum.NAME));
195         assertEquals(UniqueIdBuilder.buildModelUid(modelName), value.get(GraphPropertyEnum.UNIQUE_ID));
196     }
197
198     @Test
199     void findModelVertexTest_modelNotFound() {
200         final ArgumentCaptor<Map<GraphPropertyEnum, Object>> mapArgumentCaptor = ArgumentCaptor.forClass(Map.class);
201         when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.MODEL), mapArgumentCaptor.capture()))
202             .thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
203         var modelName = "modelName";
204
205         final Optional<GraphVertex> modelVertexByNameOpt = modelOperation.findModelVertexByName(modelName);
206
207         assertTrue(modelVertexByNameOpt.isEmpty());
208         final Map<GraphPropertyEnum, Object> value = mapArgumentCaptor.getValue();
209         assertEquals(modelName, value.get(GraphPropertyEnum.NAME));
210         assertEquals(UniqueIdBuilder.buildModelUid(modelName), value.get(GraphPropertyEnum.UNIQUE_ID));
211     }
212
213     @Test
214     void findModelVertexTest_janusGraphError() {
215         final ArgumentCaptor<Map<GraphPropertyEnum, Object>> mapArgumentCaptor = ArgumentCaptor.forClass(Map.class);
216         when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.MODEL), mapArgumentCaptor.capture()))
217             .thenReturn(Either.right(JanusGraphOperationStatus.GENERAL_ERROR));
218         var modelName = "modelName";
219
220         final var actualException = assertThrows(OperationException.class, () -> modelOperation.findModelVertexByName(modelName));
221
222         assertEquals(ActionStatus.GENERAL_ERROR, actualException.getActionStatus());
223         final Map<GraphPropertyEnum, Object> value = mapArgumentCaptor.getValue();
224         assertEquals(modelName, value.get(GraphPropertyEnum.NAME));
225         assertEquals(UniqueIdBuilder.buildModelUid(modelName), value.get(GraphPropertyEnum.UNIQUE_ID));
226     }
227
228     @Test
229     void findModelVertexTest_emptyOrNullModelName() {
230         assertTrue(modelOperation.findModelVertexByName("").isEmpty());
231         assertTrue(modelOperation.findModelVertexByName(null).isEmpty());
232     }
233
234     @Test
235     void findModelByNameSuccessTest() {
236         final ArgumentCaptor<Map<GraphPropertyEnum, Object>> mapArgumentCaptor = ArgumentCaptor.forClass(Map.class);
237         var modelName = "modelName";
238         final GraphVertex expectedVertex = mock(GraphVertex.class);
239         when(expectedVertex.getMetadataProperty(GraphPropertyEnum.NAME)).thenReturn(modelName);
240         when(expectedVertex.getMetadataProperty(GraphPropertyEnum.MODEL_TYPE)).thenReturn(ModelTypeEnum.NORMATIVE.getValue());
241         when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.MODEL), mapArgumentCaptor.capture())).thenReturn(Either.left(List.of(expectedVertex)));
242         when(janusGraphGenericDao.getChild(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Model), UniqueIdBuilder.buildModelUid(modelName),
243             GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.Model, ModelData.class)).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
244         final Optional<Model> modelByNameOpt = modelOperation.findModelByName(modelName);
245
246         final Map<GraphPropertyEnum, Object> value = mapArgumentCaptor.getValue();
247         assertEquals(modelName, value.get(GraphPropertyEnum.NAME));
248         assertEquals(UniqueIdBuilder.buildModelUid(modelName), value.get(GraphPropertyEnum.UNIQUE_ID));
249
250         final Model expectedModel = new Model(modelName, ModelTypeEnum.NORMATIVE);
251         assertTrue(modelByNameOpt.isPresent());
252         assertEquals(expectedModel, modelByNameOpt.get());
253     }
254
255     @Test
256     void findModelByNameTest_modelNameNotFound() {
257         final ArgumentCaptor<Map<GraphPropertyEnum, Object>> mapArgumentCaptor = ArgumentCaptor.forClass(Map.class);
258         var modelName = "modelName";
259         when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.MODEL), mapArgumentCaptor.capture()))
260             .thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
261         final Optional<Model> modelByNameOpt = modelOperation.findModelByName(modelName);
262         assertTrue(modelByNameOpt.isEmpty());
263     }
264
265     @Test
266     void findModelByNameTest_emptyOrNullModelName() {
267         assertTrue(modelOperation.findModelByName("").isEmpty());
268         assertTrue(modelOperation.findModelByName(null).isEmpty());
269     }
270
271     @Test
272     void findAllModelsSuccessTest() {
273         final GraphVertex expectedVertex = mock(GraphVertex.class);
274         when(expectedVertex.getMetadataProperty(GraphPropertyEnum.NAME)).thenReturn(modelName);
275         when(expectedVertex.getMetadataProperty(GraphPropertyEnum.MODEL_TYPE)).thenReturn(ModelTypeEnum.NORMATIVE.getValue());
276         when(janusGraphDao.getByCriteria(VertexTypeEnum.MODEL, Collections.emptyMap())).thenReturn(Either.left(List.of(expectedVertex)));
277         when(janusGraphGenericDao.getChild(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Model), UniqueIdBuilder.buildModelUid(modelName),
278             GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.Model, ModelData.class)).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
279
280         final List<Model> actualModelList = modelOperation.findAllModels();
281         assertFalse(actualModelList.isEmpty());
282         assertEquals(1, actualModelList.size());
283         assertEquals(modelName, actualModelList.get(0).getName());
284     }
285
286     @Test
287     void findAllModelsTest_noModelsFound() {
288         when(janusGraphDao.getByCriteria(VertexTypeEnum.MODEL, Collections.emptyMap())).thenReturn(Either.left(Collections.emptyList()));
289         final List<Model> actualModelList = modelOperation.findAllModels();
290         assertTrue(actualModelList.isEmpty());
291     }
292
293     @Test
294     void findAllModelsTest_janusGraphNotFound() {
295         when(janusGraphDao.getByCriteria(VertexTypeEnum.MODEL, Collections.emptyMap()))
296             .thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
297         final List<Model> actualModelList = modelOperation.findAllModels();
298         assertTrue(actualModelList.isEmpty());
299     }
300
301     @Test
302     void findAllModelsTest_janusGraphError() {
303         when(janusGraphDao.getByCriteria(VertexTypeEnum.MODEL, Collections.emptyMap()))
304             .thenReturn(Either.right(JanusGraphOperationStatus.GENERAL_ERROR));
305         final var actualException = assertThrows(OperationException.class, () -> modelOperation.findAllModels());
306         final var expectedException = ModelOperationExceptionSupplier.failedToRetrieveModels(JanusGraphOperationStatus.GENERAL_ERROR).get();
307         assertEquals(expectedException.getMessage(), actualException.getMessage());
308     }
309
310     @Test
311     void findAllModelImportsTest() {
312         //given
313         final var modelName = "modelName";
314         final var parentModelName = "parentModelName";
315         final GraphVertex expectedVertex = mock(GraphVertex.class);
316         when(expectedVertex.getMetadataProperty(GraphPropertyEnum.NAME)).thenReturn(modelName);
317         when(expectedVertex.getMetadataProperty(GraphPropertyEnum.MODEL_TYPE)).thenReturn(ModelTypeEnum.NORMATIVE_EXTENSION.getValue());
318         when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.MODEL), anyMap())).thenReturn(Either.left(List.of(expectedVertex)));
319
320         final var modelData = new ModelData(parentModelName, parentModelName, ModelTypeEnum.NORMATIVE);
321         final ImmutablePair<ModelData, GraphEdge> modelDataGraphEdgePair = new ImmutablePair<>(modelData, null);
322
323         when(janusGraphGenericDao.getChild(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Model), UniqueIdBuilder.buildModelUid(modelName),
324             GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.Model, ModelData.class)).thenReturn(Either.left(modelDataGraphEdgePair));
325
326         final ArrayList<ToscaImportByModel> childModelImportList = new ArrayList<>();
327         childModelImportList.add(createModelImport(modelName, "anyPath1"));
328         childModelImportList.add(createModelImport(modelName, "anyPath2"));
329         when(toscaModelImportCassandraDao.findAllByModel(modelName)).thenReturn(new ArrayList<>(childModelImportList));
330         final ArrayList<ToscaImportByModel> parentModelImportList = new ArrayList<>();
331         parentModelImportList.add(createModelImport(parentModelName, "anyPath1"));
332         parentModelImportList.add(createModelImport(parentModelName, "anyPath2"));
333         when(toscaModelImportCassandraDao.findAllByModel(parentModelName)).thenReturn(parentModelImportList);
334
335         //when
336         final List<ToscaImportByModel> actualModelImportList = modelOperation.findAllModelImports(modelName, true);
337
338         //then
339         assertFalse(actualModelImportList.isEmpty());
340         assertEquals(childModelImportList.size() + parentModelImportList.size(), actualModelImportList.size());
341         Stream.concat(childModelImportList.stream(), parentModelImportList.stream())
342             .forEach(toscaImportByModel -> assertTrue(actualModelImportList.contains(toscaImportByModel)));
343     }
344
345     @Test
346     void addTypesToDefaultImportsTest_nonExistingAdditionalTypesImport() throws IOException {
347         var modelName = "model";
348         final Path testResourcePath = Path.of("src/test/resources/modelOperation");
349
350         final var dataTypesPath = testResourcePath.resolve(Path.of("input-data_types.yaml"));
351         final var dataTypes = Files.readString(dataTypesPath);
352
353         final Path import1RelativePath = Path.of("original-import-1.yaml");
354         final Path import1Path = testResourcePath.resolve(import1RelativePath);
355         final Path import2RelativePath = Path.of("original-import-2.yaml");
356         final Path import2Path = testResourcePath.resolve(import2RelativePath);
357
358         var toscaImportByModel1 = new ToscaImportByModel();
359         toscaImportByModel1.setModelId(modelName);
360         toscaImportByModel1.setFullPath(import1RelativePath.toString());
361         toscaImportByModel1.setContent(Files.readString(import1Path));
362
363         var toscaImportByModel2 = new ToscaImportByModel();
364         toscaImportByModel2.setModelId(modelName);
365         toscaImportByModel2.setFullPath(import2RelativePath.toString());
366         toscaImportByModel2.setContent(Files.readString(import2Path));
367
368         final List<ToscaImportByModel> modelImports = new ArrayList<>();
369         modelImports.add(toscaImportByModel1);
370         modelImports.add(toscaImportByModel2);
371         when(toscaModelImportCassandraDao.findAllByModel(modelName)).thenReturn(modelImports);
372
373         modelOperation.addTypesToDefaultImports(ElementTypeEnum.DATA_TYPE, dataTypes, modelName);
374         ArgumentCaptor<List<ToscaImportByModel>> importListArgumentCaptor = ArgumentCaptor.forClass(List.class);
375         verify(toscaModelImportCassandraDao).saveAll(eq(modelName), importListArgumentCaptor.capture());
376
377         final List<ToscaImportByModel> actualImportList = importListArgumentCaptor.getValue();
378         assertEquals(3, actualImportList.size());
379         assertTrue(actualImportList.contains(toscaImportByModel1));
380         assertTrue(actualImportList.contains(toscaImportByModel2));
381
382         var expectedAdditionalTypesImport = new ToscaImportByModel();
383         expectedAdditionalTypesImport.setModelId(modelName);
384         expectedAdditionalTypesImport.setFullPath(ADDITIONAL_TYPE_DEFINITIONS_PATH.toString());
385         expectedAdditionalTypesImport.setContent(Files.readString(testResourcePath.resolve(Path.of("expected-additional_types-1.yaml"))));
386         final ToscaImportByModel actualAdditionalTypesImport =
387             actualImportList.stream().filter(expectedAdditionalTypesImport::equals).findFirst().orElse(null);
388         assertNotNull(actualAdditionalTypesImport);
389         assertEquals(expectedAdditionalTypesImport.getContent(), actualAdditionalTypesImport.getContent());
390
391         var expectedImport1 = new ToscaImportByModel();
392         expectedImport1.setModelId(modelName);
393         expectedImport1.setFullPath(import1RelativePath.toString());
394         expectedImport1.setContent(Files.readString(testResourcePath.resolve(Path.of("expected-import-1.yaml"))));
395         final ToscaImportByModel actualImport1 = actualImportList.stream().filter(expectedImport1::equals).findFirst().orElse(null);
396         assertNotNull(actualImport1);
397         assertEquals(expectedImport1.getContent(), actualImport1.getContent());
398
399         var expectedImport2 = new ToscaImportByModel();
400         expectedImport2.setModelId(modelName);
401         expectedImport2.setFullPath(import2RelativePath.toString());
402         expectedImport2.setContent(Files.readString(testResourcePath.resolve(Path.of("expected-import-2.yaml"))));
403         final ToscaImportByModel actualImport2 = actualImportList.stream().filter(expectedImport2::equals).findFirst().orElse(null);
404         assertNotNull(actualImport2);
405         assertEquals(expectedImport2.getContent(), actualImport2.getContent());
406     }
407
408     @Test
409     void addArtifactsToDefaultImportsTest_nonExistingAdditionalTypesImport() throws IOException {
410         var modelName = "model";
411         final Path testResourcePath = Path.of("src/test/resources/modelOperation");
412
413         final var dataTypesPath = testResourcePath.resolve(Path.of("input-artifact_types.yaml"));
414         final var dataTypes = Files.readString(dataTypesPath);
415
416         final Path import1RelativePath = Path.of("original-import-3.yaml");
417         final Path import1Path = testResourcePath.resolve(import1RelativePath);
418
419         var toscaImportByModel1 = new ToscaImportByModel();
420         toscaImportByModel1.setModelId(modelName);
421         toscaImportByModel1.setFullPath(import1RelativePath.toString());
422         toscaImportByModel1.setContent(Files.readString(import1Path));
423
424         final List<ToscaImportByModel> modelImports = new ArrayList<>();
425         modelImports.add(toscaImportByModel1);
426         when(toscaModelImportCassandraDao.findAllByModel(modelName)).thenReturn(modelImports);
427
428         modelOperation.addTypesToDefaultImports(ElementTypeEnum.ARTIFACT_TYPE, dataTypes, modelName);
429         ArgumentCaptor<List<ToscaImportByModel>> importListArgumentCaptor = ArgumentCaptor.forClass(List.class);
430         verify(toscaModelImportCassandraDao).saveAll(eq(modelName), importListArgumentCaptor.capture());
431
432         final List<ToscaImportByModel> actualImportList = importListArgumentCaptor.getValue();
433         assertEquals(2, actualImportList.size());
434         assertTrue(actualImportList.contains(toscaImportByModel1));
435
436         var expectedAdditionalTypesImport = new ToscaImportByModel();
437         expectedAdditionalTypesImport.setModelId(modelName);
438         expectedAdditionalTypesImport.setFullPath(ADDITIONAL_TYPE_DEFINITIONS_PATH.toString());
439         expectedAdditionalTypesImport.setContent(Files.readString(testResourcePath.resolve(Path.of("expected-additional_types-3.yaml"))));
440         final ToscaImportByModel actualAdditionalTypesImport =
441             actualImportList.stream().filter(expectedAdditionalTypesImport::equals).findFirst().orElse(null);
442         assertNotNull(actualAdditionalTypesImport);
443         assertEquals(expectedAdditionalTypesImport.getContent(), actualAdditionalTypesImport.getContent());
444
445         var expectedImport1 = new ToscaImportByModel();
446         expectedImport1.setModelId(modelName);
447         expectedImport1.setFullPath(import1RelativePath.toString());
448         expectedImport1.setContent(Files.readString(testResourcePath.resolve(Path.of("expected-import-3.yaml"))));
449         final ToscaImportByModel actualImport1 = actualImportList.stream().filter(expectedImport1::equals).findFirst().orElse(null);
450         assertNotNull(actualImport1);
451         assertEquals(expectedImport1.getContent(), actualImport1.getContent());
452
453     }
454
455     @Test
456     void addTypesToDefaultImportsTest_existingAdditionalTypesImport() throws IOException {
457         var modelName = "model";
458         final Path testResourcePath = Path.of("src/test/resources/modelOperation");
459
460         final var dataTypesPath = testResourcePath.resolve(Path.of("input-data_types.yaml"));
461         final var dataTypes = Files.readString(dataTypesPath);
462
463         final Path import1RelativePath = Path.of("original-import-1.yaml");
464         final Path import1Path = testResourcePath.resolve(import1RelativePath);
465
466         var toscaImportByModel1 = new ToscaImportByModel();
467         toscaImportByModel1.setModelId(modelName);
468         toscaImportByModel1.setFullPath(import1RelativePath.toString());
469         toscaImportByModel1.setContent(Files.readString(import1Path));
470
471         var originalAdditionalTypesImport = new ToscaImportByModel();
472         originalAdditionalTypesImport.setModelId(modelName);
473         originalAdditionalTypesImport.setFullPath(ADDITIONAL_TYPE_DEFINITIONS_PATH.toString());
474         final Path originalAdditionalTypesImportPath = testResourcePath.resolve(Path.of("original-additional_types-1.yaml"));
475         originalAdditionalTypesImport.setContent(Files.readString(originalAdditionalTypesImportPath));
476
477         final List<ToscaImportByModel> modelImports = new ArrayList<>();
478         modelImports.add(toscaImportByModel1);
479         modelImports.add(originalAdditionalTypesImport);
480         when(toscaModelImportCassandraDao.findAllByModel(modelName)).thenReturn(modelImports);
481
482         modelOperation.addTypesToDefaultImports(ElementTypeEnum.DATA_TYPE, dataTypes, modelName);
483         ArgumentCaptor<List<ToscaImportByModel>> importListArgumentCaptor = ArgumentCaptor.forClass(List.class);
484         verify(toscaModelImportCassandraDao).saveAll(eq(modelName), importListArgumentCaptor.capture());
485
486         final List<ToscaImportByModel> actualImportList = importListArgumentCaptor.getValue();
487         assertEquals(2, actualImportList.size());
488         assertTrue(actualImportList.contains(toscaImportByModel1));
489
490         var expectedAdditionalTypesImport = new ToscaImportByModel();
491         expectedAdditionalTypesImport.setModelId(modelName);
492         expectedAdditionalTypesImport.setFullPath(ADDITIONAL_TYPE_DEFINITIONS_PATH.toString());
493         expectedAdditionalTypesImport.setContent(Files.readString(testResourcePath.resolve(Path.of("expected-additional_types-2.yaml"))));
494         final ToscaImportByModel actualAdditionalTypesImport =
495             actualImportList.stream().filter(expectedAdditionalTypesImport::equals).findFirst().orElse(null);
496         assertNotNull(actualAdditionalTypesImport);
497         assertEquals(expectedAdditionalTypesImport.getContent(), actualAdditionalTypesImport.getContent());
498
499         var expectedImport1 = new ToscaImportByModel();
500         expectedImport1.setModelId(modelName);
501         expectedImport1.setFullPath(import1RelativePath.toString());
502         expectedImport1.setContent(Files.readString(testResourcePath.resolve(Path.of("expected-import-1.yaml"))));
503         final ToscaImportByModel actualImport1 = actualImportList.stream().filter(expectedImport1::equals).findFirst().orElse(null);
504         assertNotNull(actualImport1);
505         assertEquals(expectedImport1.getContent(), actualImport1.getContent());
506
507     }
508
509     @Test
510     void addArtifactsToDefaultImportsTest_existingAdditionalTypesImport() throws IOException {
511         var modelName = "model";
512         final Path testResourcePath = Path.of("src/test/resources/modelOperation");
513
514         final var dataTypesPath = testResourcePath.resolve(Path.of("input-artifact_types.yaml"));
515         final var dataTypes = Files.readString(dataTypesPath);
516
517         final Path import1RelativePath = Path.of("original-import-3.yaml");
518         final Path import1Path = testResourcePath.resolve(import1RelativePath);
519
520         var toscaImportByModel1 = new ToscaImportByModel();
521         toscaImportByModel1.setModelId(modelName);
522         toscaImportByModel1.setFullPath(import1RelativePath.toString());
523         toscaImportByModel1.setContent(Files.readString(import1Path));
524
525         var originalAdditionalTypesImport = new ToscaImportByModel();
526         originalAdditionalTypesImport.setModelId(modelName);
527         originalAdditionalTypesImport.setFullPath(ADDITIONAL_TYPE_DEFINITIONS_PATH.toString());
528         final Path originalAdditionalTypesImportPath = testResourcePath.resolve(Path.of("original-additional_types-2.yaml"));
529         originalAdditionalTypesImport.setContent(Files.readString(originalAdditionalTypesImportPath));
530
531         final List<ToscaImportByModel> modelImports = new ArrayList<>();
532         modelImports.add(toscaImportByModel1);
533         modelImports.add(originalAdditionalTypesImport);
534         when(toscaModelImportCassandraDao.findAllByModel(modelName)).thenReturn(modelImports);
535
536         modelOperation.addTypesToDefaultImports(ElementTypeEnum.ARTIFACT_TYPE, dataTypes, modelName);
537         ArgumentCaptor<List<ToscaImportByModel>> importListArgumentCaptor = ArgumentCaptor.forClass(List.class);
538         verify(toscaModelImportCassandraDao).saveAll(eq(modelName), importListArgumentCaptor.capture());
539
540         final List<ToscaImportByModel> actualImportList = importListArgumentCaptor.getValue();
541         assertEquals(2, actualImportList.size());
542         assertTrue(actualImportList.contains(toscaImportByModel1));
543
544         var expectedAdditionalTypesImport = new ToscaImportByModel();
545         expectedAdditionalTypesImport.setModelId(modelName);
546         expectedAdditionalTypesImport.setFullPath(ADDITIONAL_TYPE_DEFINITIONS_PATH.toString());
547         expectedAdditionalTypesImport.setContent(Files.readString(testResourcePath.resolve(Path.of("expected-additional_types-3.yaml"))));
548         final ToscaImportByModel actualAdditionalTypesImport =
549             actualImportList.stream().filter(expectedAdditionalTypesImport::equals).findFirst().orElse(null);
550         assertNotNull(actualAdditionalTypesImport);
551         assertEquals(expectedAdditionalTypesImport.getContent(), actualAdditionalTypesImport.getContent());
552
553         var expectedImport1 = new ToscaImportByModel();
554         expectedImport1.setModelId(modelName);
555         expectedImport1.setFullPath(import1RelativePath.toString());
556         expectedImport1.setContent(Files.readString(testResourcePath.resolve(Path.of("expected-import-3.yaml"))));
557         final ToscaImportByModel actualImport1 = actualImportList.stream().filter(expectedImport1::equals).findFirst().orElse(null);
558         assertNotNull(actualImport1);
559         assertEquals(expectedImport1.getContent(), actualImport1.getContent());
560
561     }
562
563     private ToscaImportByModel createModelImport(final String parentModelName, final String importPath) {
564         var toscaImportByModel = new ToscaImportByModel();
565         toscaImportByModel.setModelId(parentModelName);
566         toscaImportByModel.setFullPath(importPath);
567         return toscaImportByModel;
568     }
569 }