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.ComponentInstance;
34 import org.openecomp.sdc.be.model.ComponentInstanceInput;
35 import org.openecomp.sdc.be.model.ComponentParametersView;
36 import org.openecomp.sdc.be.model.Service;
37 import org.openecomp.sdc.be.model.User;
38 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
39 import org.openecomp.sdc.be.user.IUserBusinessLogic;
40 import org.openecomp.sdc.exception.ResponseFormat;
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.util.HashMap;
45 import java.util.List;
48 import static org.junit.Assert.assertEquals;
49 import static org.junit.Assert.assertTrue;
50 import static org.mockito.Matchers.anyString;
51 import static org.mockito.Matchers.eq;
52 import static org.mockito.Mockito.when;
54 public class InputsBusinessLogicTest {
56 private static final String COMPONENT_INSTANCE_ID = "instanceId";
57 private static final String COMPONENT_ID = "componentId";
58 private static final String USER_ID = "userId";
59 public static final String INSTANCE_INPUT_ID = "inputId";
61 private ComponentsUtils componentsUtilsMock;
64 private IUserBusinessLogic userAdminMock;
67 private ToscaOperationFacade toscaOperationFacadeMock;
70 private UserValidations userValidations;
73 private InputsBusinessLogic testInstance;
75 private Service service;
78 public void setUp() throws Exception {
79 MockitoAnnotations.initMocks(this);
80 service = new Service();
81 service.setUniqueId(COMPONENT_INSTANCE_ID);
82 ComponentInstance componentInstance = new ComponentInstance();
83 componentInstance.setUniqueId(COMPONENT_INSTANCE_ID);
84 service.setComponentInstances(Collections.singletonList(componentInstance));
86 Map<String, List<ComponentInstanceInput>> instanceInputMap = new HashMap<>();
87 ComponentInstanceInput componentInstanceInput = new ComponentInstanceInput();
88 componentInstanceInput.setInputId(INSTANCE_INPUT_ID);
89 instanceInputMap.put(COMPONENT_INSTANCE_ID, Collections.singletonList(componentInstanceInput));
90 instanceInputMap.put("someInputId", Collections.singletonList(new ComponentInstanceInput()));
91 service.setComponentInstancesInputs(instanceInputMap);
92 when(userValidations.validateUserExists(eq(USER_ID), anyString(), eq(false))).thenReturn(Either.left(new User()));
93 when(userAdminMock.getUser(USER_ID, false)).thenReturn(Either.left(new User()));
97 public void getComponentInstanceInputs_ComponentInstanceNotExist() throws Exception {
98 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(service));
99 Either<List<ComponentInstanceInput>, ResponseFormat> componentInstanceInputs = testInstance.getComponentInstanceInputs(USER_ID, COMPONENT_ID, "nonExisting");
100 assertTrue(componentInstanceInputs.isRight());
101 Mockito.verify(componentsUtilsMock).getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND);
105 public void getComponentInstanceInputs_emptyInputsMap() throws Exception {
106 service.setComponentInstancesInputs(Collections.emptyMap());
107 getComponents_emptyInputs(service);
111 public void getComponentInstanceInputs_nullInputsMap() throws Exception {
112 service.setComponentInstancesInputs(null);
113 getComponents_emptyInputs(service);
117 public void getComponentInstanceInputs_instanceHasNoInputs() throws Exception {
118 service.setComponentInstancesInputs(Collections.singletonMap("someInputId", new ArrayList<>()));
119 getComponents_emptyInputs(service);
123 public void getComponentInstanceInputs() throws Exception {
124 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(service));
125 Either<List<ComponentInstanceInput>, ResponseFormat> componentInstanceInputs = testInstance.getComponentInstanceInputs(USER_ID, COMPONENT_ID, COMPONENT_INSTANCE_ID);
126 assertEquals("inputId", componentInstanceInputs.left().value().get(0).getInputId());
129 private void getComponents_emptyInputs(Service service) {
130 when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(service));
131 Either<List<ComponentInstanceInput>, ResponseFormat> componentInstanceInputs = testInstance.getComponentInstanceInputs(USER_ID, COMPONENT_ID, COMPONENT_INSTANCE_ID);
132 assertEquals(Collections.emptyList(), componentInstanceInputs.left().value());