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.*;
49 @RunWith(MockitoJUnitRunner.class)
50 public class CommonImportManagerTest {
51 private CommonImportManager commonImportManager;
53 private ComponentsUtils componentsUtils;
55 PropertyOperation propertyOperation;
57 private JanusGraphGenericDao janusGraphGenericDao;
60 private Function<Object, Either<ActionStatus, ResponseFormat>> validator;
62 private Function<Object, ImmutablePair<ElementTypeEnum, String>> elementInfoGetter;
64 private Function<String, Either<Object, StorageOperationStatus>> elementFetcher;
66 private Function<Object, Either<Object, StorageOperationStatus>> elementAdder;
68 private BiFunction<Object, Object, Either<Object, StorageOperationStatus>> elementUpgrader;
71 public void startUp() {
72 commonImportManager = new CommonImportManager(componentsUtils, propertyOperation);
74 when(propertyOperation.getJanusGraphGenericDao()).thenReturn(janusGraphGenericDao);
78 public void testCreateElementTypesByDao_validationFailed() {
79 Object type1 = new Object();
80 List<Object> elementTypesToCreate = Arrays.asList(type1);
82 ImmutablePair<ElementTypeEnum, String> elementInfo = new ImmutablePair<>(ElementTypeEnum.CAPABILITY_TYPE, "TestCapability");
83 when(elementInfoGetter.apply(type1)).thenReturn(elementInfo);
85 ResponseFormat responseFormat = new ResponseFormat();
86 responseFormat.setServiceException(new ServiceException());
87 when(validator.apply(type1)).thenReturn(Either.right(responseFormat));
90 commonImportManager.createElementTypesByDao(elementTypesToCreate , validator , elementInfoGetter, elementFetcher, elementAdder, elementUpgrader);
92 verify(elementAdder, never()).apply(Mockito.any());
93 verify(elementUpgrader, never()).apply(Mockito.any(), Mockito.any());
94 verify(janusGraphGenericDao).rollback();
98 public void testCreateElementTypesByDao_RuntTimeExceptionInValidation() {
99 Object type1 = new Object();
100 List<Object> elementTypesToCreate = Arrays.asList(type1);
102 ImmutablePair<ElementTypeEnum, String> elementInfo = new ImmutablePair<>(ElementTypeEnum.CAPABILITY_TYPE, "TestCapability");
103 when(elementInfoGetter.apply(type1)).thenReturn(elementInfo);
104 when(validator.apply(type1)).thenThrow(new RuntimeException("Test Exception"));
107 commonImportManager.createElementTypesByDao(elementTypesToCreate , validator , elementInfoGetter, elementFetcher, elementAdder, elementUpgrader);
109 catch(Exception skip) {
112 verify(elementAdder, never()).apply(Mockito.any());
113 verify(elementUpgrader, never()).apply(Mockito.any(), Mockito.any());
114 verify(janusGraphGenericDao).rollback();
118 public void testCreateElementTypesByDao_capabilityTypeFetcherFailed() {
119 CapabilityTypeDefinition type1 = new CapabilityTypeDefinition();
120 List<Object> elementTypesToCreate = Arrays.asList(type1);
122 ImmutablePair<ElementTypeEnum, String> elementInfo = new ImmutablePair<>(ElementTypeEnum.CAPABILITY_TYPE, "TestCapability");
123 when(elementInfoGetter.apply(type1)).thenReturn(elementInfo);
125 when(validator.apply(type1)).thenReturn(Either.left(ActionStatus.OK));
126 when(elementFetcher.apply("TestCapability")).thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
127 ResponseFormat responseFormat = new ResponseFormat();
128 responseFormat.setServiceException(new ServiceException());
129 when(componentsUtils.convertFromStorageResponseForCapabilityType(Mockito.any())).thenCallRealMethod();
130 when(componentsUtils.getResponseFormatByCapabilityType(ActionStatus.INVALID_CONTENT, type1)).thenReturn(responseFormat);
133 commonImportManager.createElementTypesByDao(elementTypesToCreate , validator , elementInfoGetter, elementFetcher, elementAdder, elementUpgrader);
135 verify(elementAdder, never()).apply(Mockito.any());
136 verify(elementUpgrader, never()).apply(Mockito.any(), Mockito.any());
137 verify(janusGraphGenericDao).rollback();
141 public void testCreateElementTypesByDao_capabilityTypeNotFound_AddFailed() {
142 CapabilityTypeDefinition type1 = new CapabilityTypeDefinition();
143 List<Object> elementTypesToCreate = Arrays.asList(type1);
145 ImmutablePair<ElementTypeEnum, String> elementInfo = new ImmutablePair<>(ElementTypeEnum.CAPABILITY_TYPE, "TestCapability");
146 when(elementInfoGetter.apply(type1)).thenReturn(elementInfo);
148 when(validator.apply(type1)).thenReturn(Either.left(ActionStatus.OK));
149 when(elementFetcher.apply("TestCapability")).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
150 when(elementAdder.apply(type1)).thenReturn(Either.right(StorageOperationStatus.SCHEMA_VIOLATION));
152 ResponseFormat responseFormat = new ResponseFormat();
153 responseFormat.setServiceException(new ServiceException());
154 when(componentsUtils.convertFromStorageResponseForCapabilityType(Mockito.any())).thenCallRealMethod();
155 when(componentsUtils.getResponseFormatByCapabilityType(ActionStatus.CAPABILITY_TYPE_ALREADY_EXIST, type1)).thenReturn(responseFormat);
158 commonImportManager.createElementTypesByDao(elementTypesToCreate , validator , elementInfoGetter, elementFetcher, elementAdder, elementUpgrader);
160 verify(elementAdder).apply(type1);
161 verify(elementUpgrader, never()).apply(Mockito.any(), Mockito.any());
162 verify(janusGraphGenericDao).rollback();
167 public void testCreateElementTypesByDao_capabilityTypeNotFound_AddSucceeded() {
168 CapabilityTypeDefinition type1 = new CapabilityTypeDefinition();
169 List<Object> elementTypesToCreate = Arrays.asList(type1);
171 ImmutablePair<ElementTypeEnum, String> elementInfo = new ImmutablePair<>(ElementTypeEnum.CAPABILITY_TYPE, "TestCapability");
172 when(elementInfoGetter.apply(type1)).thenReturn(elementInfo);
174 when(validator.apply(type1)).thenReturn(Either.left(ActionStatus.OK));
175 when(elementFetcher.apply("TestCapability")).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
176 when(elementAdder.apply(type1)).thenReturn(Either.left(type1));
179 Either<List<ImmutablePair<Object, Boolean>>, ResponseFormat> result = commonImportManager.createElementTypesByDao(elementTypesToCreate , validator , elementInfoGetter, elementFetcher, elementAdder, elementUpgrader);
181 verify(elementAdder).apply(type1);
182 verify(elementUpgrader, never()).apply(Mockito.any(), Mockito.any());
183 verify(janusGraphGenericDao).commit();
185 assertEquals(type1, result.left().value().get(0).getLeft());
186 assertEquals(true, result.left().value().get(0).getRight());
190 public void testCreateElementTypesByDao_capabilityTypeFound_UpgradeFailed() {
191 CapabilityTypeDefinition type1 = new CapabilityTypeDefinition();
192 CapabilityTypeDefinition type1_1 = new CapabilityTypeDefinition();
193 List<Object> elementTypesToCreate = Arrays.asList(type1_1);
195 ImmutablePair<ElementTypeEnum, String> elementInfo = new ImmutablePair<>(ElementTypeEnum.CAPABILITY_TYPE, "TestCapability");
196 when(elementInfoGetter.apply(type1_1)).thenReturn(elementInfo);
198 when(validator.apply(type1_1)).thenReturn(Either.left(ActionStatus.OK));
199 when(elementFetcher.apply("TestCapability")).thenReturn(Either.left(type1));
200 when(elementUpgrader.apply(type1_1, type1)).thenReturn(Either.right(StorageOperationStatus.SCHEMA_VIOLATION));
202 ResponseFormat responseFormat = new ResponseFormat();
203 responseFormat.setServiceException(new ServiceException());
204 when(componentsUtils.convertFromStorageResponseForCapabilityType(Mockito.any())).thenCallRealMethod();
205 when(componentsUtils.getResponseFormatByCapabilityType(ActionStatus.CAPABILITY_TYPE_ALREADY_EXIST, type1_1)).thenReturn(responseFormat);
208 commonImportManager.createElementTypesByDao(elementTypesToCreate , validator , elementInfoGetter, elementFetcher, elementAdder, elementUpgrader);
210 verify(elementAdder, never()).apply(Mockito.any());
211 verify(elementUpgrader).apply(type1_1, type1);
212 verify(janusGraphGenericDao).rollback();
216 public void testCreateElementTypesByDao_capabilityTypeFound_UpgradeSucceeded() {
217 CapabilityTypeDefinition type1 = new CapabilityTypeDefinition();
218 CapabilityTypeDefinition type1_1 = new CapabilityTypeDefinition();
219 List<Object> elementTypesToCreate = Arrays.asList(type1_1);
221 ImmutablePair<ElementTypeEnum, String> elementInfo = new ImmutablePair<>(ElementTypeEnum.CAPABILITY_TYPE, "TestCapability");
222 when(elementInfoGetter.apply(type1_1)).thenReturn(elementInfo);
224 when(validator.apply(type1_1)).thenReturn(Either.left(ActionStatus.OK));
225 when(elementFetcher.apply("TestCapability")).thenReturn(Either.left(type1));
226 when(elementUpgrader.apply(type1_1, type1)).thenReturn(Either.left(type1_1));
228 Either<List<ImmutablePair<Object, Boolean>>, ResponseFormat> result = commonImportManager.createElementTypesByDao(elementTypesToCreate , validator , elementInfoGetter, elementFetcher, elementAdder, elementUpgrader);
230 verify(elementAdder, never()).apply(Mockito.any());
231 verify(elementUpgrader).apply(type1_1, type1);
232 verify(janusGraphGenericDao).commit();
234 assertEquals(type1_1, result.left().value().get(0).getLeft());
235 assertEquals(true, result.left().value().get(0).getRight());
239 public void testCreateElementTypesByDao_capabilityTypeFound_UpgradeAlreadyExists() {
240 CapabilityTypeDefinition type1 = new CapabilityTypeDefinition();
241 CapabilityTypeDefinition type1_1 = new CapabilityTypeDefinition();
242 List<Object> elementTypesToCreate = Arrays.asList(type1_1);
244 ImmutablePair<ElementTypeEnum, String> elementInfo = new ImmutablePair<>(ElementTypeEnum.CAPABILITY_TYPE, "TestCapability");
245 when(elementInfoGetter.apply(type1_1)).thenReturn(elementInfo);
247 when(validator.apply(type1_1)).thenReturn(Either.left(ActionStatus.OK));
248 when(elementFetcher.apply("TestCapability")).thenReturn(Either.left(type1));
249 when(elementUpgrader.apply(type1_1, type1)).thenReturn(Either.right(StorageOperationStatus.OK));
251 Either<List<ImmutablePair<Object, Boolean>>, ResponseFormat> result = commonImportManager.createElementTypesByDao(elementTypesToCreate , validator , elementInfoGetter, elementFetcher, elementAdder, elementUpgrader);
253 verify(elementAdder, never()).apply(Mockito.any());
254 verify(elementUpgrader).apply(type1_1, type1);
255 verify(janusGraphGenericDao).commit();
257 assertEquals(type1_1, result.left().value().get(0).getLeft());
258 assertEquals(false, result.left().value().get(0).getRight());