Sync Integ to Master
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / InputsBusinessLogicTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
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.impl;
22
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;
41
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.util.HashMap;
45 import java.util.List;
46 import java.util.Map;
47
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;
53
54 public class InputsBusinessLogicTest {
55
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";
60     @Mock
61     private ComponentsUtils componentsUtilsMock;
62
63     @Mock
64     private IUserBusinessLogic userAdminMock;
65
66     @Mock
67     private ToscaOperationFacade toscaOperationFacadeMock;
68
69     @Mock
70     private UserValidations userValidations;
71
72     @InjectMocks
73     private InputsBusinessLogic testInstance;
74
75     private Service service;
76
77     @Before
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));
85
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()));
94     }
95
96     @Test
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);
102     }
103
104     @Test
105     public void getComponentInstanceInputs_emptyInputsMap() throws Exception {
106         service.setComponentInstancesInputs(Collections.emptyMap());
107         getComponents_emptyInputs(service);
108     }
109
110     @Test
111     public void getComponentInstanceInputs_nullInputsMap() throws Exception {
112         service.setComponentInstancesInputs(null);
113         getComponents_emptyInputs(service);
114     }
115
116     @Test
117     public void getComponentInstanceInputs_instanceHasNoInputs() throws Exception {
118         service.setComponentInstancesInputs(Collections.singletonMap("someInputId", new ArrayList<>()));
119         getComponents_emptyInputs(service);
120     }
121
122     @Test
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());
127     }
128
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());
133     }
134
135 }