2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.impl;
23 import fj.data.Either;
24 import org.apache.commons.lang3.tuple.ImmutablePair;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.Mock;
29 import org.mockito.Mockito;
30 import org.mockito.junit.MockitoJUnitRunner;
31 import org.openecomp.sdc.be.components.impl.CommonImportManager.ElementTypeEnum;
32 import org.openecomp.sdc.be.dao.api.ActionStatus;
33 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao;
34 import org.openecomp.sdc.be.impl.ComponentsUtils;
35 import org.openecomp.sdc.be.model.CapabilityTypeDefinition;
36 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
37 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
38 import org.openecomp.sdc.exception.ResponseFormat;
39 import org.openecomp.sdc.exception.ServiceException;
41 import java.util.Arrays;
42 import java.util.List;
43 import java.util.function.BiFunction;
44 import java.util.function.Function;
46 import static org.junit.Assert.assertEquals;
47 import static org.mockito.Mockito.never;
48 import static org.mockito.Mockito.verify;
49 import static org.mockito.Mockito.when;
51 @RunWith(MockitoJUnitRunner.class)
52 public class CommonImportManagerTest {
53 private CommonImportManager commonImportManager;
55 private ComponentsUtils componentsUtils;
57 PropertyOperation propertyOperation;
59 private JanusGraphGenericDao janusGraphGenericDao;
62 private Function<Object, Either<ActionStatus, ResponseFormat>> validator;
64 private Function<Object, ImmutablePair<ElementTypeEnum, String>> elementInfoGetter;
66 private Function<String, Either<Object, StorageOperationStatus>> elementFetcher;
68 private Function<Object, Either<Object, StorageOperationStatus>> elementAdder;
70 private BiFunction<Object, Object, Either<Object, StorageOperationStatus>> elementUpgrader;
73 public void startUp() {
74 commonImportManager = new CommonImportManager(componentsUtils, propertyOperation);
76 when(propertyOperation.getJanusGraphGenericDao()).thenReturn(janusGraphGenericDao);
80 public void testCreateElementTypesByDao_validationFailed() {
81 Object type1 = new Object();
82 List<Object> elementTypesToCreate = Arrays.asList(type1);
84 ImmutablePair<ElementTypeEnum, String> elementInfo = new ImmutablePair<>(ElementTypeEnum.CAPABILITY_TYPE, "TestCapability");
85 when(elementInfoGetter.apply(type1)).thenReturn(elementInfo);
87 ResponseFormat responseFormat = new ResponseFormat();
88 responseFormat.setServiceException(new ServiceException());
89 when(validator.apply(type1)).thenReturn(Either.right(responseFormat));
92 commonImportManager.createElementTypesByDao(elementTypesToCreate , validator , elementInfoGetter, elementFetcher, elementAdder, elementUpgrader);
94 verify(elementAdder, never()).apply(Mockito.any());
95 verify(elementUpgrader, never()).apply(Mockito.any(), Mockito.any());
96 verify(janusGraphGenericDao).rollback();
100 public void testCreateElementTypesByDao_RuntTimeExceptionInValidation() {
101 Object type1 = new Object();
102 List<Object> elementTypesToCreate = Arrays.asList(type1);
104 ImmutablePair<ElementTypeEnum, String> elementInfo = new ImmutablePair<>(ElementTypeEnum.CAPABILITY_TYPE, "TestCapability");
105 when(elementInfoGetter.apply(type1)).thenReturn(elementInfo);
106 when(validator.apply(type1)).thenThrow(new RuntimeException("Test Exception"));
109 commonImportManager.createElementTypesByDao(elementTypesToCreate , validator , elementInfoGetter, elementFetcher, elementAdder, elementUpgrader);
111 catch(Exception skip) {
114 verify(elementAdder, never()).apply(Mockito.any());
115 verify(elementUpgrader, never()).apply(Mockito.any(), Mockito.any());
116 verify(janusGraphGenericDao).rollback();
120 public void testCreateElementTypesByDao_capabilityTypeFetcherFailed() {
121 CapabilityTypeDefinition type1 = new CapabilityTypeDefinition();
122 List<Object> elementTypesToCreate = Arrays.asList(type1);
124 ImmutablePair<ElementTypeEnum, String> elementInfo = new ImmutablePair<>(ElementTypeEnum.CAPABILITY_TYPE, "TestCapability");
125 when(elementInfoGetter.apply(type1)).thenReturn(elementInfo);
127 when(validator.apply(type1)).thenReturn(Either.left(ActionStatus.OK));
128 when(elementFetcher.apply("TestCapability")).thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
129 ResponseFormat responseFormat = new ResponseFormat();
130 responseFormat.setServiceException(new ServiceException());
131 when(componentsUtils.convertFromStorageResponseForCapabilityType(Mockito.any())).thenCallRealMethod();
132 when(componentsUtils.getResponseFormatByCapabilityType(ActionStatus.INVALID_CONTENT, type1)).thenReturn(responseFormat);
135 commonImportManager.createElementTypesByDao(elementTypesToCreate , validator , elementInfoGetter, elementFetcher, elementAdder, elementUpgrader);
137 verify(elementAdder, never()).apply(Mockito.any());
138 verify(elementUpgrader, never()).apply(Mockito.any(), Mockito.any());
139 verify(janusGraphGenericDao).rollback();
143 public void testCreateElementTypesByDao_capabilityTypeNotFound_AddFailed() {
144 CapabilityTypeDefinition type1 = new CapabilityTypeDefinition();
145 List<Object> elementTypesToCreate = Arrays.asList(type1);
147 ImmutablePair<ElementTypeEnum, String> elementInfo = new ImmutablePair<>(ElementTypeEnum.CAPABILITY_TYPE, "TestCapability");
148 when(elementInfoGetter.apply(type1)).thenReturn(elementInfo);
150 when(validator.apply(type1)).thenReturn(Either.left(ActionStatus.OK));
151 when(elementFetcher.apply("TestCapability")).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
152 when(elementAdder.apply(type1)).thenReturn(Either.right(StorageOperationStatus.SCHEMA_VIOLATION));
154 ResponseFormat responseFormat = new ResponseFormat();
155 responseFormat.setServiceException(new ServiceException());
156 when(componentsUtils.convertFromStorageResponseForCapabilityType(Mockito.any())).thenCallRealMethod();
157 when(componentsUtils.getResponseFormatByCapabilityType(ActionStatus.CAPABILITY_TYPE_ALREADY_EXIST, type1)).thenReturn(responseFormat);
160 commonImportManager.createElementTypesByDao(elementTypesToCreate , validator , elementInfoGetter, elementFetcher, elementAdder, elementUpgrader);
162 verify(elementAdder).apply(type1);
163 verify(elementUpgrader, never()).apply(Mockito.any(), Mockito.any());
164 verify(janusGraphGenericDao).rollback();
169 public void testCreateElementTypesByDao_capabilityTypeNotFound_AddSucceeded() {
170 CapabilityTypeDefinition type1 = new CapabilityTypeDefinition();
171 List<Object> elementTypesToCreate = Arrays.asList(type1);
173 ImmutablePair<ElementTypeEnum, String> elementInfo = new ImmutablePair<>(ElementTypeEnum.CAPABILITY_TYPE, "TestCapability");
174 when(elementInfoGetter.apply(type1)).thenReturn(elementInfo);
176 when(validator.apply(type1)).thenReturn(Either.left(ActionStatus.OK));
177 when(elementFetcher.apply("TestCapability")).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
178 when(elementAdder.apply(type1)).thenReturn(Either.left(type1));
181 Either<List<ImmutablePair<Object, Boolean>>, ResponseFormat> result = commonImportManager.createElementTypesByDao(elementTypesToCreate , validator , elementInfoGetter, elementFetcher, elementAdder, elementUpgrader);
183 verify(elementAdder).apply(type1);
184 verify(elementUpgrader, never()).apply(Mockito.any(), Mockito.any());
185 verify(janusGraphGenericDao).commit();
187 assertEquals(type1, result.left().value().get(0).getLeft());
188 assertEquals(true, result.left().value().get(0).getRight());
192 public void testCreateElementTypesByDao_capabilityTypeFound_UpgradeFailed() {
193 CapabilityTypeDefinition type1 = new CapabilityTypeDefinition();
194 CapabilityTypeDefinition type1_1 = new CapabilityTypeDefinition();
195 List<Object> elementTypesToCreate = Arrays.asList(type1_1);
197 ImmutablePair<ElementTypeEnum, String> elementInfo = new ImmutablePair<>(ElementTypeEnum.CAPABILITY_TYPE, "TestCapability");
198 when(elementInfoGetter.apply(type1_1)).thenReturn(elementInfo);
200 when(validator.apply(type1_1)).thenReturn(Either.left(ActionStatus.OK));
201 when(elementFetcher.apply("TestCapability")).thenReturn(Either.left(type1));
202 when(elementUpgrader.apply(type1_1, type1)).thenReturn(Either.right(StorageOperationStatus.SCHEMA_VIOLATION));
204 ResponseFormat responseFormat = new ResponseFormat();
205 responseFormat.setServiceException(new ServiceException());
206 when(componentsUtils.convertFromStorageResponseForCapabilityType(Mockito.any())).thenCallRealMethod();
207 when(componentsUtils.getResponseFormatByCapabilityType(ActionStatus.CAPABILITY_TYPE_ALREADY_EXIST, type1_1)).thenReturn(responseFormat);
210 commonImportManager.createElementTypesByDao(elementTypesToCreate , validator , elementInfoGetter, elementFetcher, elementAdder, elementUpgrader);
212 verify(elementAdder, never()).apply(Mockito.any());
213 verify(elementUpgrader).apply(type1_1, type1);
214 verify(janusGraphGenericDao).rollback();
218 public void testCreateElementTypesByDao_capabilityTypeFound_UpgradeSucceeded() {
219 CapabilityTypeDefinition type1 = new CapabilityTypeDefinition();
220 CapabilityTypeDefinition type1_1 = new CapabilityTypeDefinition();
221 List<Object> elementTypesToCreate = Arrays.asList(type1_1);
223 ImmutablePair<ElementTypeEnum, String> elementInfo = new ImmutablePair<>(ElementTypeEnum.CAPABILITY_TYPE, "TestCapability");
224 when(elementInfoGetter.apply(type1_1)).thenReturn(elementInfo);
226 when(validator.apply(type1_1)).thenReturn(Either.left(ActionStatus.OK));
227 when(elementFetcher.apply("TestCapability")).thenReturn(Either.left(type1));
228 when(elementUpgrader.apply(type1_1, type1)).thenReturn(Either.left(type1_1));
230 Either<List<ImmutablePair<Object, Boolean>>, ResponseFormat> result = commonImportManager.createElementTypesByDao(elementTypesToCreate , validator , elementInfoGetter, elementFetcher, elementAdder, elementUpgrader);
232 verify(elementAdder, never()).apply(Mockito.any());
233 verify(elementUpgrader).apply(type1_1, type1);
234 verify(janusGraphGenericDao).commit();
236 assertEquals(type1_1, result.left().value().get(0).getLeft());
237 assertEquals(true, result.left().value().get(0).getRight());
241 public void testCreateElementTypesByDao_capabilityTypeFound_UpgradeAlreadyExists() {
242 CapabilityTypeDefinition type1 = new CapabilityTypeDefinition();
243 CapabilityTypeDefinition type1_1 = new CapabilityTypeDefinition();
244 List<Object> elementTypesToCreate = Arrays.asList(type1_1);
246 ImmutablePair<ElementTypeEnum, String> elementInfo = new ImmutablePair<>(ElementTypeEnum.CAPABILITY_TYPE, "TestCapability");
247 when(elementInfoGetter.apply(type1_1)).thenReturn(elementInfo);
249 when(validator.apply(type1_1)).thenReturn(Either.left(ActionStatus.OK));
250 when(elementFetcher.apply("TestCapability")).thenReturn(Either.left(type1));
251 when(elementUpgrader.apply(type1_1, type1)).thenReturn(Either.right(StorageOperationStatus.OK));
253 Either<List<ImmutablePair<Object, Boolean>>, ResponseFormat> result = commonImportManager.createElementTypesByDao(elementTypesToCreate , validator , elementInfoGetter, elementFetcher, elementAdder, elementUpgrader);
255 verify(elementAdder, never()).apply(Mockito.any());
256 verify(elementUpgrader).apply(type1_1, type1);
257 verify(janusGraphGenericDao).commit();
259 assertEquals(type1_1, result.left().value().get(0).getLeft());
260 assertEquals(false, result.left().value().get(0).getRight());