2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.junit.Before;
25 import org.junit.Test;
26 import org.mockito.InjectMocks;
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.mockito.MockitoAnnotations;
30 import org.openecomp.sdc.be.components.validation.UserValidations;
31 import org.openecomp.sdc.be.dao.api.ActionStatus;
32 import org.openecomp.sdc.be.impl.ComponentsUtils;
33 import org.openecomp.sdc.be.model.*;
34 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
35 import org.openecomp.sdc.be.user.IUserBusinessLogic;
36 import org.openecomp.sdc.exception.ResponseFormat;
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertTrue;
42 import static org.mockito.ArgumentMatchers.anyString;
43 import static org.mockito.ArgumentMatchers.eq;
44 import static org.mockito.Mockito.when;
46 public class InputsBusinessLogicTest {
48 private static final String COMPONENT_INSTANCE_ID = "instanceId";
49 private static final String COMPONENT_ID = "componentId";
50 private static final String USER_ID = "userId";
51 public static final String INSTANCE_INPUT_ID = "inputId";
53 private ComponentsUtils componentsUtilsMock;
56 private IUserBusinessLogic userAdminMock;
59 private ToscaOperationFacade toscaOperationFacadeMock;
62 private UserValidations userValidations;
65 private InputsBusinessLogic testInstance;
67 private Service service;
70 public void setUp() throws Exception {
71 MockitoAnnotations.initMocks(this);
72 service = new Service();
73 service.setUniqueId(COMPONENT_INSTANCE_ID);
74 ComponentInstance componentInstance = new ComponentInstance();
75 componentInstance.setUniqueId(COMPONENT_INSTANCE_ID);
76 service.setComponentInstances(Collections.singletonList(componentInstance));
78 Map<String, List<ComponentInstanceInput>> instanceInputMap = new HashMap<>();
79 ComponentInstanceInput componentInstanceInput = new ComponentInstanceInput();
80 componentInstanceInput.setInputId(INSTANCE_INPUT_ID);
81 instanceInputMap.put(COMPONENT_INSTANCE_ID, Collections.singletonList(componentInstanceInput));
82 instanceInputMap.put("someInputId", Collections.singletonList(new ComponentInstanceInput()));
83 service.setComponentInstancesInputs(instanceInputMap);
84 when(userValidations.validateUserExists(eq(USER_ID), anyString(), eq(false))).thenReturn(new User());
85 when(userAdminMock.getUser(USER_ID, false)).thenReturn(Either.left(new User()));
89 public void getComponentInstanceInputs_ComponentInstanceNotExist() throws Exception {
90 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(service));
91 Either<List<ComponentInstanceInput>, ResponseFormat> componentInstanceInputs = testInstance.getComponentInstanceInputs(USER_ID, COMPONENT_ID, "nonExisting");
92 assertTrue(componentInstanceInputs.isRight());
93 Mockito.verify(componentsUtilsMock).getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND);
97 public void getComponentInstanceInputs_emptyInputsMap() throws Exception {
98 service.setComponentInstancesInputs(Collections.emptyMap());
99 getComponents_emptyInputs(service);
103 public void getComponentInstanceInputs_nullInputsMap() throws Exception {
104 service.setComponentInstancesInputs(null);
105 getComponents_emptyInputs(service);
109 public void getComponentInstanceInputs_instanceHasNoInputs() throws Exception {
110 service.setComponentInstancesInputs(Collections.singletonMap("someInputId", new ArrayList<>()));
111 getComponents_emptyInputs(service);
115 public void getComponentInstanceInputs() throws Exception {
116 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(service));
117 Either<List<ComponentInstanceInput>, ResponseFormat> componentInstanceInputs = testInstance.getComponentInstanceInputs(USER_ID, COMPONENT_ID, COMPONENT_INSTANCE_ID);
118 assertEquals("inputId", componentInstanceInputs.left().value().get(0).getInputId());
121 private void getComponents_emptyInputs(Service service) {
122 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(service));
123 Either<List<ComponentInstanceInput>, ResponseFormat> componentInstanceInputs = testInstance.getComponentInstanceInputs(USER_ID, COMPONENT_ID, COMPONENT_INSTANCE_ID);
124 assertEquals(Collections.emptyList(), componentInstanceInputs.left().value());