Add Support for creating model 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.assertThrows;
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.Mockito.when;
25
26 import fj.data.Either;
27 import org.junit.jupiter.api.BeforeAll;
28 import org.junit.jupiter.api.Test;
29 import org.junit.jupiter.api.TestInstance;
30 import org.junit.jupiter.api.TestInstance.Lifecycle;
31 import org.mockito.InjectMocks;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao;
35 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
36 import org.openecomp.sdc.be.model.Model;
37 import org.openecomp.sdc.be.model.ModelTestBase;
38 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.OperationException;
39 import org.openecomp.sdc.be.resources.data.ModelData;
40 import org.springframework.test.context.ContextConfiguration;
41
42 @ContextConfiguration("classpath:application-context-test.xml")
43 @TestInstance(Lifecycle.PER_CLASS)
44 class ModelOperationTest extends ModelTestBase {
45
46     @InjectMocks
47     private ModelOperation modelOperation;
48     @Mock
49     private JanusGraphGenericDao janusGraphGenericDao;
50
51     private final String modelName = "ETSI-SDC-MODEL-TEST";
52
53     @BeforeAll
54     void setup() {
55         init();
56         MockitoAnnotations.openMocks(this);
57     }
58
59     @Test
60     void createModelSuccessTest() {
61         final ModelData modelData = new ModelData(modelName,  UniqueIdBuilder.buildModelUid(modelName));
62         when(janusGraphGenericDao.createNode(any(),any())).thenReturn(Either.left(modelData));
63         final Model createdModel = modelOperation.createModel(new Model(modelName), false);
64         assertThat(createdModel).isNotNull();
65         assertThat(createdModel.getName()).isEqualTo(modelName);
66     }
67
68     @Test
69     void createModelFailWithModelAlreadyExistTest() {
70         when(janusGraphGenericDao.createNode(any(),any())).thenReturn(Either.right(JanusGraphOperationStatus.JANUSGRAPH_SCHEMA_VIOLATION));
71         assertThrows(OperationException.class, () -> modelOperation.createModel(new Model(modelName), false));
72     }
73
74     @Test
75     void createModelFailTest() {
76         when(janusGraphGenericDao.createNode(any(),any())).thenReturn(Either.right(JanusGraphOperationStatus.GRAPH_IS_NOT_AVAILABLE));
77         assertThrows(OperationException.class, () -> modelOperation.createModel(new Model(modelName), false));
78     }
79
80 }