Add models imports endpoint and persistence structure
[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.assertThrows;
24 import static org.junit.jupiter.api.Assertions.assertTrue;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.anyList;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.never;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
32
33 import fj.data.Either;
34 import java.nio.charset.StandardCharsets;
35 import java.util.Collections;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Optional;
39 import java.util.TreeMap;
40 import org.junit.jupiter.api.BeforeAll;
41 import org.junit.jupiter.api.BeforeEach;
42 import org.junit.jupiter.api.Test;
43 import org.mockito.ArgumentCaptor;
44 import org.mockito.InjectMocks;
45 import org.mockito.Mock;
46 import org.mockito.MockitoAnnotations;
47 import org.openecomp.sdc.be.dao.api.ActionStatus;
48 import org.openecomp.sdc.be.dao.cassandra.ToscaModelImportCassandraDao;
49 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao;
50 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
51 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
52 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
53 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
54 import org.openecomp.sdc.be.data.model.ToscaImportByModel;
55 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
56 import org.openecomp.sdc.be.model.Model;
57 import org.openecomp.sdc.be.model.ModelTestBase;
58 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.OperationException;
59 import org.openecomp.sdc.be.resources.data.ModelData;
60 import org.springframework.test.context.ContextConfiguration;
61
62 @ContextConfiguration("classpath:application-context-test.xml")
63 class ModelOperationTest extends ModelTestBase {
64
65     @InjectMocks
66     private ModelOperation modelOperation;
67     @Mock
68     private JanusGraphGenericDao janusGraphGenericDao;
69     @Mock
70     private JanusGraphDao janusGraphDao;
71     @Mock
72     private ToscaModelImportCassandraDao toscaModelImportCassandraDao;
73
74     private final String modelName = "ETSI-SDC-MODEL-TEST";
75
76     @BeforeAll
77     static void beforeAllInit() {
78         init();
79     }
80
81     @BeforeEach
82     void beforeEachInit() {
83         MockitoAnnotations.openMocks(this);
84     }
85
86     @Test
87     void createModelSuccessTest() {
88         final ModelData modelData = new ModelData(modelName,  UniqueIdBuilder.buildModelUid(modelName));
89         when(janusGraphGenericDao.createNode(any(),any())).thenReturn(Either.left(modelData));
90         final Model createdModel = modelOperation.createModel(new Model(modelName), false);
91         assertThat(createdModel).isNotNull();
92         assertThat(createdModel.getName()).isEqualTo(modelName);
93     }
94
95     @Test
96     void createModelFailWithModelAlreadyExistTest() {
97         when(janusGraphGenericDao.createNode(any(),any())).thenReturn(Either.right(JanusGraphOperationStatus.JANUSGRAPH_SCHEMA_VIOLATION));
98         final var model = new Model(modelName);
99         assertThrows(OperationException.class, () -> modelOperation.createModel(model, false));
100     }
101
102     @Test
103     void createModelFailTest() {
104         when(janusGraphGenericDao.createNode(any(),any())).thenReturn(Either.right(JanusGraphOperationStatus.GRAPH_IS_NOT_AVAILABLE));
105         final var model = new Model(modelName);
106         assertThrows(OperationException.class, () -> modelOperation.createModel(model, false));
107     }
108
109     @Test
110     void createModelImportsSuccessTest() {
111         var modelId = "modelId";
112         var contentEntry1 = "contentEntry1";
113         var pathEntry1 = "entry1";
114         var contentEntry2 = "contentEntry2";
115         var pathEntry2 = "entry2/path";
116         final Map<String, byte[]> zipContent = new TreeMap<>();
117         zipContent.put(pathEntry1, contentEntry1.getBytes(StandardCharsets.UTF_8));
118         zipContent.put(pathEntry2, contentEntry2.getBytes(StandardCharsets.UTF_8));
119
120         modelOperation.createModelImports(modelId, zipContent);
121
122         final var toscaImport1 = new ToscaImportByModel();
123         toscaImport1.setModelId(modelId);
124         toscaImport1.setContent(contentEntry1);
125         toscaImport1.setFullPath(pathEntry1);
126         final var toscaImport2 = new ToscaImportByModel();
127         toscaImport2.setModelId(modelId);
128         toscaImport2.setContent(contentEntry2);
129         toscaImport2.setFullPath(pathEntry2);
130         final List<ToscaImportByModel> toscaImportByModelList = List.of(toscaImport1, toscaImport2);
131
132         verify(toscaModelImportCassandraDao).importAll(modelId, toscaImportByModelList);
133     }
134
135     @Test
136     void createModelImportsTest_emptyZipContent() {
137         var modelId = "modelId";
138         modelOperation.createModelImports(modelId, Collections.emptyMap());
139         verify(toscaModelImportCassandraDao, never()).importAll(eq(modelId), anyList());
140         modelOperation.createModelImports(modelId, null);
141         verify(toscaModelImportCassandraDao, never()).importAll(eq(null), anyList());
142     }
143
144     @Test
145     void findModelVertexSuccessTest() {
146         final ArgumentCaptor<Map<GraphPropertyEnum, Object>> mapArgumentCaptor = ArgumentCaptor.forClass(Map.class);
147         final GraphVertex expectedVertex = new GraphVertex();
148         when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.MODEL), mapArgumentCaptor.capture())).thenReturn(Either.left(List.of(expectedVertex)));
149         var modelName = "modelName";
150         final Optional<GraphVertex> modelVertexByNameOpt = modelOperation.findModelVertexByName(modelName);
151         assertTrue(modelVertexByNameOpt.isPresent());
152         assertEquals(expectedVertex, modelVertexByNameOpt.get());
153         final Map<GraphPropertyEnum, Object> value = mapArgumentCaptor.getValue();
154         assertEquals(modelName, value.get(GraphPropertyEnum.NAME));
155         assertEquals(UniqueIdBuilder.buildModelUid(modelName), value.get(GraphPropertyEnum.UNIQUE_ID));
156     }
157
158     @Test
159     void findModelVertexTest_modelNotFound() {
160         final ArgumentCaptor<Map<GraphPropertyEnum, Object>> mapArgumentCaptor = ArgumentCaptor.forClass(Map.class);
161         when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.MODEL), mapArgumentCaptor.capture()))
162             .thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
163         var modelName = "modelName";
164
165         final Optional<GraphVertex> modelVertexByNameOpt = modelOperation.findModelVertexByName(modelName);
166
167         assertTrue(modelVertexByNameOpt.isEmpty());
168         final Map<GraphPropertyEnum, Object> value = mapArgumentCaptor.getValue();
169         assertEquals(modelName, value.get(GraphPropertyEnum.NAME));
170         assertEquals(UniqueIdBuilder.buildModelUid(modelName), value.get(GraphPropertyEnum.UNIQUE_ID));
171     }
172
173     @Test
174     void findModelVertexTest_janusGraphError() {
175         final ArgumentCaptor<Map<GraphPropertyEnum, Object>> mapArgumentCaptor = ArgumentCaptor.forClass(Map.class);
176         when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.MODEL), mapArgumentCaptor.capture()))
177             .thenReturn(Either.right(JanusGraphOperationStatus.GENERAL_ERROR));
178         var modelName = "modelName";
179
180         final var actualException = assertThrows(OperationException.class, () -> modelOperation.findModelVertexByName(modelName));
181
182         assertEquals(ActionStatus.GENERAL_ERROR, actualException.getActionStatus());
183         final Map<GraphPropertyEnum, Object> value = mapArgumentCaptor.getValue();
184         assertEquals(modelName, value.get(GraphPropertyEnum.NAME));
185         assertEquals(UniqueIdBuilder.buildModelUid(modelName), value.get(GraphPropertyEnum.UNIQUE_ID));
186     }
187
188     @Test
189     void findModelVertexTest_emptyOrNullModelName() {
190         assertTrue(modelOperation.findModelVertexByName("").isEmpty());
191         assertTrue(modelOperation.findModelVertexByName(null).isEmpty());
192     }
193
194     @Test
195     void findModelByNameSuccessTest() {
196         final ArgumentCaptor<Map<GraphPropertyEnum, Object>> mapArgumentCaptor = ArgumentCaptor.forClass(Map.class);
197         var modelName = "modelName";
198         final GraphVertex expectedVertex = mock(GraphVertex.class);
199         when(expectedVertex.getMetadataProperty(GraphPropertyEnum.NAME)).thenReturn(modelName);
200         when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.MODEL), mapArgumentCaptor.capture())).thenReturn(Either.left(List.of(expectedVertex)));
201         final Optional<Model> modelByNameOpt = modelOperation.findModelByName(modelName);
202
203         final Map<GraphPropertyEnum, Object> value = mapArgumentCaptor.getValue();
204         assertEquals(modelName, value.get(GraphPropertyEnum.NAME));
205         assertEquals(UniqueIdBuilder.buildModelUid(modelName), value.get(GraphPropertyEnum.UNIQUE_ID));
206
207         final Model expectedModel = new Model(modelName);
208         assertTrue(modelByNameOpt.isPresent());
209         assertEquals(expectedModel, modelByNameOpt.get());
210     }
211
212     @Test
213     void findModelByNameTest_modelNameNotFound() {
214         final ArgumentCaptor<Map<GraphPropertyEnum, Object>> mapArgumentCaptor = ArgumentCaptor.forClass(Map.class);
215         var modelName = "modelName";
216         when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.MODEL), mapArgumentCaptor.capture()))
217             .thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
218         final Optional<Model> modelByNameOpt = modelOperation.findModelByName(modelName);
219         assertTrue(modelByNameOpt.isEmpty());
220     }
221
222     @Test
223     void findModelByNameTest_emptyOrNullModelName() {
224         assertTrue(modelOperation.findModelByName("").isEmpty());
225         assertTrue(modelOperation.findModelByName(null).isEmpty());
226     }
227
228 }