Sync Integ to Master
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / merge / GlobalInputsFilteringBusinessLogicTest.java
1 package org.openecomp.sdc.be.components.merge;
2
3 import com.google.common.collect.Sets;
4 import fj.data.Either;
5 import org.junit.Before;
6 import org.junit.Test;
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;
19
20 import java.util.List;
21 import java.util.Set;
22 import java.util.stream.Collectors;
23
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;
28
29 public class GlobalInputsFilteringBusinessLogicTest {
30
31     private static final String GENERIC_TOSCA_TYPE = "myGenericType";
32
33     @InjectMocks
34     private GlobalInputsFilteringBusinessLogic testInstance;
35
36     @Mock
37     private GenericTypeBusinessLogic genericTypeBusinessLogicMock;
38
39     @Mock
40     private ToscaOperationFacade toscaOperationFacadeMock;
41
42     @Mock
43     private ComponentsUtils componentsUtils;
44
45     @Before
46     public void setUp() throws Exception {
47         MockitoAnnotations.initMocks(this);
48     }
49
50     @Test
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);
63     }
64
65     @Test
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);
75     }
76
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);
83     }
84 }