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.merge;
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;
40 import java.util.List;
42 import java.util.stream.Collectors;
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;
49 public class GlobalInputsFilteringBusinessLogicTest {
51 private static final String GENERIC_TOSCA_TYPE = "myGenericType";
54 private GlobalInputsFilteringBusinessLogic testInstance;
57 private GenericTypeBusinessLogic genericTypeBusinessLogicMock;
60 private ToscaOperationFacade toscaOperationFacadeMock;
63 private ComponentsUtils componentsUtils;
66 public void setUp() throws Exception {
67 MockitoAnnotations.initMocks(this);
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);
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);
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);