1 package org.openecomp.sdc.be.components.merge;
3 import com.google.common.collect.Sets;
5 import org.junit.Before;
7 import org.mockito.InjectMocks;
8 import org.mockito.Mock;
9 import org.mockito.Mockito;
10 import org.mockito.MockitoAnnotations;
11 import org.openecomp.sdc.be.components.impl.generic.GenericTypeBusinessLogic;
12 import org.openecomp.sdc.be.components.utils.ObjectGenerator;
13 import org.openecomp.sdc.be.dao.api.ActionStatus;
14 import org.openecomp.sdc.be.impl.ComponentsUtils;
15 import org.openecomp.sdc.be.model.InputDefinition;
16 import org.openecomp.sdc.be.model.Resource;
17 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
18 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
20 import java.util.List;
22 import java.util.stream.Collectors;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Mockito.verifyZeroInteractions;
27 import static org.mockito.Mockito.when;
29 public class GlobalInputsFilteringBusinessLogicTest {
31 private static final String GENERIC_TOSCA_TYPE = "myGenericType";
34 private GlobalInputsFilteringBusinessLogic testInstance;
37 private GenericTypeBusinessLogic genericTypeBusinessLogicMock;
40 private ToscaOperationFacade toscaOperationFacadeMock;
43 private ComponentsUtils componentsUtils;
46 public void setUp() throws Exception {
47 MockitoAnnotations.initMocks(this);
51 public void testFilterGlobalInputs() throws Exception {
52 Resource mockResource = Mockito.mock(Resource.class);
53 String myGenericType = GENERIC_TOSCA_TYPE;
54 String[] genericProperties = {"property1", "property2"};
55 String[] allInputs = {"property1", "property2", "property3", "property4"};
56 when(mockResource.fetchGenericTypeToscaNameFromConfig()).thenReturn(myGenericType);
57 when(mockResource.getInputs()).thenReturn(ObjectGenerator.buildInputs(allInputs));
58 Resource genericNodeType = ObjectGenerator.buildResourceWithProperties(genericProperties);
59 when(toscaOperationFacadeMock.getLatestCertifiedNodeTypeByToscaResourceName(myGenericType)).thenReturn(Either.left(genericNodeType));
60 when(genericTypeBusinessLogicMock.generateInputsFromGenericTypeProperties(genericNodeType)).thenReturn(ObjectGenerator.buildInputs(genericProperties));
61 Either<List<InputDefinition>, ActionStatus> globalInputsEither = testInstance.filterGlobalInputs(mockResource);
62 verifyFilteredOnlyGlobalInputs(globalInputsEither, genericProperties);
66 public void testFilterGlobalInputs_errorGettingGenericType_convertToActionStatusAndReturn() throws Exception {
67 Resource mockResource = Mockito.mock(Resource.class);
68 when(mockResource.fetchGenericTypeToscaNameFromConfig()).thenReturn(GENERIC_TOSCA_TYPE);
69 when(toscaOperationFacadeMock.getLatestCertifiedNodeTypeByToscaResourceName(GENERIC_TOSCA_TYPE)).thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
70 when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR)).thenReturn(ActionStatus.GENERAL_ERROR);
71 Either<List<InputDefinition>, ActionStatus> globalInputsEither = testInstance.filterGlobalInputs(mockResource);
72 assertTrue(globalInputsEither.isRight());
73 assertEquals(ActionStatus.GENERAL_ERROR, globalInputsEither.right().value());
74 verifyZeroInteractions(genericTypeBusinessLogicMock);
77 private void verifyFilteredOnlyGlobalInputs(Either<List<InputDefinition>, ActionStatus> globalInputsEither, String[] genericProperties) {
78 assertTrue(globalInputsEither.isLeft());
79 List<InputDefinition> globalInputs = globalInputsEither.left().value();
80 assertEquals(2, globalInputs.size());
81 Set<String> actualGlobalInputNames = globalInputs.stream().map(InputDefinition::getName).collect(Collectors.toSet());
82 assertEquals(Sets.newHashSet(genericProperties), actualGlobalInputNames);