Added oparent to sdc main
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / merge / GlobalInputsFilteringBusinessLogicTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
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.merge;
22
23 import com.google.common.collect.Sets;
24 import fj.data.Either;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.InjectMocks;
28 import org.mockito.Mock;
29 import org.mockito.Mockito;
30 import org.mockito.MockitoAnnotations;
31 import org.openecomp.sdc.be.components.impl.generic.GenericTypeBusinessLogic;
32 import org.openecomp.sdc.be.components.utils.ObjectGenerator;
33 import org.openecomp.sdc.be.dao.api.ActionStatus;
34 import org.openecomp.sdc.be.impl.ComponentsUtils;
35 import org.openecomp.sdc.be.model.InputDefinition;
36 import org.openecomp.sdc.be.model.Resource;
37 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
38 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
39
40 import java.util.List;
41 import java.util.Set;
42 import java.util.stream.Collectors;
43
44 import static org.junit.Assert.assertEquals;
45 import static org.junit.Assert.assertTrue;
46 import static org.mockito.Mockito.verifyZeroInteractions;
47 import static org.mockito.Mockito.when;
48
49 public class GlobalInputsFilteringBusinessLogicTest {
50
51     private static final String GENERIC_TOSCA_TYPE = "myGenericType";
52
53     @InjectMocks
54     private GlobalInputsFilteringBusinessLogic testInstance;
55
56     @Mock
57     private GenericTypeBusinessLogic genericTypeBusinessLogicMock;
58
59     @Mock
60     private ToscaOperationFacade toscaOperationFacadeMock;
61
62     @Mock
63     private ComponentsUtils componentsUtils;
64
65     @Before
66     public void setUp() throws Exception {
67         MockitoAnnotations.initMocks(this);
68     }
69
70     @Test
71     public void testFilterGlobalInputs() throws Exception {
72         Resource mockResource = Mockito.mock(Resource.class);
73         String myGenericType = GENERIC_TOSCA_TYPE;
74         String[] genericProperties = {"property1", "property2"};
75         String[] allInputs = {"property1", "property2", "property3", "property4"};
76         when(mockResource.fetchGenericTypeToscaNameFromConfig()).thenReturn(myGenericType);
77         when(mockResource.getInputs()).thenReturn(ObjectGenerator.buildInputs(allInputs));
78         Resource genericNodeType = ObjectGenerator.buildResourceWithProperties(genericProperties);
79         when(toscaOperationFacadeMock.getLatestCertifiedNodeTypeByToscaResourceName(myGenericType)).thenReturn(Either.left(genericNodeType));
80         when(genericTypeBusinessLogicMock.generateInputsFromGenericTypeProperties(genericNodeType)).thenReturn(ObjectGenerator.buildInputs(genericProperties));
81         Either<List<InputDefinition>, ActionStatus> globalInputsEither = testInstance.filterGlobalInputs(mockResource);
82         verifyFilteredOnlyGlobalInputs(globalInputsEither, genericProperties);
83     }
84
85     @Test
86     public void testFilterGlobalInputs_errorGettingGenericType_convertToActionStatusAndReturn() throws Exception {
87         Resource mockResource = Mockito.mock(Resource.class);
88         when(mockResource.fetchGenericTypeToscaNameFromConfig()).thenReturn(GENERIC_TOSCA_TYPE);
89         when(toscaOperationFacadeMock.getLatestCertifiedNodeTypeByToscaResourceName(GENERIC_TOSCA_TYPE)).thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
90         when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR)).thenReturn(ActionStatus.GENERAL_ERROR);
91         Either<List<InputDefinition>, ActionStatus> globalInputsEither = testInstance.filterGlobalInputs(mockResource);
92         assertTrue(globalInputsEither.isRight());
93         assertEquals(ActionStatus.GENERAL_ERROR, globalInputsEither.right().value());
94         verifyZeroInteractions(genericTypeBusinessLogicMock);
95     }
96
97     private void verifyFilteredOnlyGlobalInputs(Either<List<InputDefinition>, ActionStatus> globalInputsEither, String[] genericProperties) {
98         assertTrue(globalInputsEither.isLeft());
99         List<InputDefinition> globalInputs = globalInputsEither.left().value();
100         assertEquals(2, globalInputs.size());
101         Set<String> actualGlobalInputNames = globalInputs.stream().map(InputDefinition::getName).collect(Collectors.toSet());
102         assertEquals(Sets.newHashSet(genericProperties), actualGlobalInputNames);
103     }
104 }