Interface operation feature enhancements
[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.*;
34 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
35 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
36 import org.openecomp.sdc.be.user.IUserBusinessLogic;
37 import org.openecomp.sdc.exception.ResponseFormat;
38
39 import java.util.*;
40
41 import static org.junit.Assert.assertEquals;
42 import static org.junit.Assert.assertTrue;
43 import static org.mockito.ArgumentMatchers.anyObject;
44 import static org.mockito.ArgumentMatchers.anyString;
45 import static org.mockito.ArgumentMatchers.eq;
46 import static org.mockito.Mockito.when;
47
48 public class InputsBusinessLogicTest {
49
50     private static final String COMPONENT_INSTANCE_ID = "instanceId";
51     private static final String COMPONENT_ID = "componentId";
52     private static final String USER_ID = "userId";
53     public static final String INSTANCE_INPUT_ID = "inputId";
54
55     @Mock
56     private ComponentsUtils componentsUtilsMock;
57
58     @Mock
59     private IUserBusinessLogic userAdminMock;
60
61     @Mock
62     private ToscaOperationFacade toscaOperationFacadeMock;
63
64     @Mock
65     private UserValidations userValidations;
66
67     @Mock
68     private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
69
70     @InjectMocks
71     private InputsBusinessLogic testInstance;
72
73     private Service service;
74
75     @Before
76     public void setUp() throws Exception {
77         MockitoAnnotations.initMocks(this);
78         service = new Service();
79         service.setUniqueId(COMPONENT_INSTANCE_ID);
80         ComponentInstance componentInstance = new ComponentInstance();
81         componentInstance.setUniqueId(COMPONENT_INSTANCE_ID);
82         service.setComponentInstances(Collections.singletonList(componentInstance));
83
84         Map<String, List<ComponentInstanceInput>> instanceInputMap = new HashMap<>();
85         ComponentInstanceInput componentInstanceInput = new ComponentInstanceInput();
86         componentInstanceInput.setInputId(INSTANCE_INPUT_ID);
87         instanceInputMap.put(COMPONENT_INSTANCE_ID, Collections.singletonList(componentInstanceInput));
88         instanceInputMap.put("someInputId", Collections.singletonList(new ComponentInstanceInput()));
89         service.setComponentInstancesInputs(instanceInputMap);
90         when(userValidations.validateUserExists(eq(USER_ID), anyString(), eq(false))).thenReturn(new User());
91         when(userAdminMock.getUser(USER_ID, false)).thenReturn(Either.left(new User()));
92     }
93
94     @Test
95     public void getComponentInstanceInputs_ComponentInstanceNotExist() throws Exception {
96         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(service));
97         Either<List<ComponentInstanceInput>, ResponseFormat> componentInstanceInputs = testInstance.getComponentInstanceInputs(USER_ID, COMPONENT_ID, "nonExisting");
98         assertTrue(componentInstanceInputs.isRight());
99         Mockito.verify(componentsUtilsMock).getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND);
100     }
101
102     @Test
103     public void getComponentInstanceInputs_emptyInputsMap() throws Exception {
104         service.setComponentInstancesInputs(Collections.emptyMap());
105         getComponents_emptyInputs(service);
106     }
107
108     @Test
109     public void getComponentInstanceInputs_nullInputsMap() throws Exception {
110         service.setComponentInstancesInputs(null);
111         getComponents_emptyInputs(service);
112     }
113
114     @Test
115     public void getComponentInstanceInputs_instanceHasNoInputs() throws Exception {
116         service.setComponentInstancesInputs(Collections.singletonMap("someInputId", new ArrayList<>()));
117         getComponents_emptyInputs(service);
118     }
119
120     @Test
121     public void getComponentInstanceInputs() throws Exception {
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("inputId", componentInstanceInputs.left().value().get(0).getInputId());
125     }
126
127     @Test
128     public void testGetInputs() {
129         String userId = "userId";
130         String componentId = "compId";
131         Component component = new Resource();
132         when(toscaOperationFacadeMock.getToscaElement(Mockito.any(String.class), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(component));
133         testInstance.getInputs(userId, componentId);
134         assertEquals(null, component.getInputs());
135     }
136
137     @Test
138     public void testGetCIPropertiesByInputId() {
139         Either<List<ComponentInstanceProperty>, ResponseFormat> result;
140         String userId = "userId";
141         String componentId = "compId";
142         Component component = new Resource();
143         List<InputDefinition> listDef = new ArrayList<>();
144         InputDefinition inputDef = new InputDefinition();
145         inputDef.setUniqueId(componentId);
146         listDef.add(inputDef);
147         component.setInputs(listDef);
148         when(toscaOperationFacadeMock.getToscaElement(Mockito.any(String.class), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(component));
149         result = testInstance.getComponentInstancePropertiesByInputId(userId, componentId, componentId, componentId);
150         assertTrue(result.isLeft());
151     }
152
153     private void getComponents_emptyInputs(Service service) {
154         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(service));
155         Either<List<ComponentInstanceInput>, ResponseFormat> componentInstanceInputs = testInstance.getComponentInstanceInputs(USER_ID, COMPONENT_ID, COMPONENT_INSTANCE_ID);
156         assertEquals(Collections.emptyList(), componentInstanceInputs.left().value());
157     }
158
159     @Test
160     public void testgetInputs_ARTIFACT_NOT_FOUND() throws Exception {
161
162         when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.ARTIFACT_NOT_FOUND)).thenReturn(ActionStatus.ARTIFACT_NOT_FOUND);
163         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
164        Either<List<InputDefinition>, ResponseFormat> responseFormatEither = testInstance.getInputs("USR01", COMPONENT_ID);
165         assertEquals(true,responseFormatEither.isRight());
166
167     }
168
169     @Test
170     public void testgetInputs_SUCCESS() throws Exception {
171         Component component = new Service();
172         InputDefinition input= new InputDefinition();
173         List inputlist = new ArrayList<>();
174         inputlist.add(input);
175         component.setInputs(inputlist);
176         when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.ARTIFACT_NOT_FOUND)).thenReturn(ActionStatus.ARTIFACT_NOT_FOUND);
177         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(component));
178         Either<List<InputDefinition>, ResponseFormat> responseFormatEither = testInstance.getInputs("USR01", COMPONENT_ID);
179         assertEquals(inputlist,responseFormatEither.left().value());
180     }
181
182     @Test
183     public void testgetComponentInstancePropertiesByInputId_Artifactnotfound() throws Exception
184     {
185         Component component = new Service();
186         InputDefinition input= new InputDefinition();
187         List inputlist = new ArrayList<>();
188         inputlist.add(input);
189         component.setInputs(inputlist);
190         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
191         Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither = testInstance.getComponentInstancePropertiesByInputId("USR01", COMPONENT_ID,"INST0.1", "INPO1");
192         assertEquals(true,responseFormatEither.isRight());
193     }
194
195     @Test
196     public void testgetComponentInstancePropertiesByInputId_PARENT_ARTIFACT_NOT_FOUND() throws Exception
197     {
198         Component component = new Service();
199         InputDefinition input= new InputDefinition();
200         List inputlist = new ArrayList<>();
201         inputlist.add(input);
202         component.setInputs(inputlist);
203         ComponentInstance componentInstance = new ComponentInstance();
204         componentInstance.setUniqueId("INST0.1");
205         componentInstance.setComponentUid("RES0.1");
206         List compinstancelist = new ArrayList<>();
207         compinstancelist.add(componentInstance);
208         component.setComponentInstances(compinstancelist);
209         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(component));
210         when(toscaOperationFacadeMock.getToscaElement(eq("RES0.1"), Mockito.any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
211         Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither = testInstance.getComponentInstancePropertiesByInputId("USR01", COMPONENT_ID,"INST0.1", "INPO1");
212         assertEquals(true,responseFormatEither.isRight());
213     }
214
215     @Test
216     public void testgetComponentInstancePropertiesByInputId() throws Exception
217     {
218         Component component = new Service();
219         InputDefinition input= new InputDefinition();
220         input.setUniqueId("INPO1");
221         List inputlist = new ArrayList<>();
222         inputlist.add(input);
223         component.setInputs(inputlist);
224         ComponentInstance componentInstance = new ComponentInstance();
225         componentInstance.setUniqueId("INST0.1");
226         componentInstance.setComponentUid("RES0.1");
227         List compinstancelist = new ArrayList<>();
228         compinstancelist.add(componentInstance);
229         component.setComponentInstances(compinstancelist);
230         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(component));
231         when(componentInstanceBusinessLogic.getComponentInstancePropertiesByInputId(Mockito.any(Component.class),eq("INPO1"))).thenReturn(compinstancelist);
232         //when(toscaOperationFacadeMock.getToscaElement(eq("RES0.1"), Mockito.any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
233         when(toscaOperationFacadeMock.getToscaElement(eq("RES0.1"), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(component));
234         Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither = testInstance.getComponentInstancePropertiesByInputId("USR01", COMPONENT_ID,"INST0.1", "INPO1");
235         assertEquals(compinstancelist,responseFormatEither.left().value());
236     }
237
238     @Test
239     public void testgetInputsForComponentInput_ARTIFACT_NOT_FOUND() throws Exception
240     {
241         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
242         Either<List<ComponentInstanceInput>, ResponseFormat> result = testInstance.getInputsForComponentInput("USR01", COMPONENT_ID,"INPO1");
243         assertEquals(true,result.isRight());
244     }
245
246     @Test
247     public void testgetInputsForComponentInput() throws Exception
248     {
249         Component component = new Service();
250         InputDefinition input= new InputDefinition();
251         input.setUniqueId("INPO1");
252         List inputlist = new ArrayList<>();
253         inputlist.add(input);
254         component.setInputs(inputlist);
255         ComponentInstance componentInstance = new ComponentInstance();
256         componentInstance.setUniqueId("INST0.1");
257         componentInstance.setComponentUid("RES0.1");
258         List compinstancelist = new ArrayList<>();
259         compinstancelist.add(componentInstance);
260         component.setComponentInstances(compinstancelist);
261         when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(component));
262         when(componentInstanceBusinessLogic.getComponentInstancePropertiesByInputId(Mockito.any(Component.class),eq("INPO1"))).thenReturn(compinstancelist);
263         Either<List<ComponentInstanceInput>, ResponseFormat> result = testInstance.getInputsForComponentInput("USR01", COMPONENT_ID,"INPO1");
264         assertEquals(true,result.isLeft());
265     }
266 }