Support unknown data types in service import
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / DataTypesImportManagerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.impl;
22
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.mockito.Mockito.any;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.times;
27 import static org.mockito.Mockito.when;
28 import fj.data.Either;
29 import java.util.Collections;
30 import org.junit.jupiter.api.AfterEach;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33 import org.mockito.ArgumentCaptor;
34 import org.mockito.InjectMocks;
35 import org.mockito.Mockito;
36 import org.mockito.MockitoAnnotations;
37 import org.mockito.Spy;
38 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao;
39 import org.openecomp.sdc.be.impl.ComponentsUtils;
40 import org.openecomp.sdc.be.model.DataTypeDefinition;
41 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
42 import org.openecomp.sdc.be.model.operations.impl.ModelOperation;
43 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
44
45 public class DataTypesImportManagerTest {
46
47     private static final ComponentsUtils componentsUtils = mock(ComponentsUtils.class);
48     private static final JanusGraphGenericDao janusGraphGenericDao = mock(JanusGraphGenericDao.class);
49     private static final PropertyOperation propertyOperation = mock(PropertyOperation.class);
50     private static final ModelOperation modelOperation = mock(ModelOperation.class);
51     @Spy
52     private CommonImportManager commonImportManager = new CommonImportManager(componentsUtils, propertyOperation, modelOperation);
53
54     @InjectMocks
55     private DataTypeImportManager dataTypeImportManager = new DataTypeImportManager();
56
57     private AutoCloseable closeable;
58
59     @BeforeEach
60     public void openMocks() {
61         closeable = MockitoAnnotations.openMocks(this);
62     }
63
64     @AfterEach
65     public void releaseMocks() throws Exception {
66         closeable.close();
67     }
68
69     @Test
70     public void testCreateDataTypes() {
71
72         DataTypeDefinition rootDataTypeDef = new DataTypeDefinition();
73         rootDataTypeDef.setName("tosca.datatypes.Root");
74         rootDataTypeDef.setProperties(Collections.emptyList());
75         DataTypeDefinition testADataTypeDef = new DataTypeDefinition();
76         testADataTypeDef.setName("tosca.datatypes.test_a");
77         testADataTypeDef.setProperties(Collections.emptyList());
78         DataTypeDefinition testCDataTypeDef = new DataTypeDefinition();
79         testCDataTypeDef.setName("tosca.datatypes.test_c");
80         testCDataTypeDef.setProperties(Collections.emptyList());
81         when(propertyOperation.getDataTypeByName("tosca.datatypes.Root", null)).thenReturn(Either.left(rootDataTypeDef));
82         when(propertyOperation.getDataTypeByName("tosca.datatypes.test_c", null)).thenReturn(Either.left(testCDataTypeDef));
83
84         when(propertyOperation.getJanusGraphGenericDao()).thenReturn(janusGraphGenericDao);
85         when(propertyOperation.getDataTypeByUidWithoutDerived("tosca.datatypes.Root.datatype", true)).thenReturn(Either.left(rootDataTypeDef));
86         when(propertyOperation.getDataTypeByUidWithoutDerived("tosca.datatypes.test_a.datatype", true))
87                 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
88         when(propertyOperation.getDataTypeByUidWithoutDerived("tosca.datatypes.test_b.datatype", true))
89                 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
90         when(propertyOperation.getDataTypeByUidWithoutDerived("tosca.datatypes.test_c.datatype", true))
91                 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
92         when(propertyOperation.addDataType(any())).thenReturn(Either.left(testADataTypeDef));
93
94         String dataTypeYml = 
95                 "tosca.datatypes.test_a:\n" + 
96                 "  derived_from: tosca.datatypes.Root\n" + 
97                 "  properties:\n" + 
98                 "    prop2:\n" +
99                 "      type: tosca.datatypes.test_b\n" +
100                 "tosca.datatypes.test_b:\n" +
101                 "  derived_from: tosca.datatypes.test_c\n" +
102                 "  properties:\n" +
103                 "    prop2:\n" +
104                 "      type: string\n" +
105                 "tosca.datatypes.test_c:\n" +
106                 "  derived_from: tosca.datatypes.Root\n" +
107                 "  properties:\n" +
108                 "    prop1:\n" +
109                 "      type: string";
110         dataTypeImportManager.createDataTypes(dataTypeYml, "", false);
111
112         ArgumentCaptor<DataTypeDefinition> dataTypeDefinitionCaptor = ArgumentCaptor.forClass(DataTypeDefinition.class);
113         Mockito.verify(propertyOperation, times(3)).addDataType(dataTypeDefinitionCaptor.capture());
114
115         assertEquals("tosca.datatypes.test_c", dataTypeDefinitionCaptor.getAllValues().get(0).getName());
116         assertEquals("tosca.datatypes.test_b", dataTypeDefinitionCaptor.getAllValues().get(1).getName());
117         assertEquals("tosca.datatypes.test_a", dataTypeDefinitionCaptor.getAllValues().get(2).getName());
118     }
119
120
121 }