6fef074730ef6844f0ba400d29b6fa0745ca3383
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / action-library-rest / action-library-rest-services / src / test / java / org / openecomp / sdcrests / action / rest / services / ActionsImplTest.java
1 /*
2  *
3  *  Copyright © 2017-2018 European Support Limited
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  * Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  */
18
19 package org.openecomp.sdcrests.action.rest.services;
20
21
22 import org.apache.cxf.attachment.AttachmentDataSource;
23 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
24 import org.junit.Assert;
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.action.ActionManager;
32 import org.openecomp.sdc.action.errors.ActionException;
33 import org.openecomp.sdc.action.types.Action;
34 import org.openecomp.sdc.action.types.ActionArtifact;
35 import org.openecomp.sdc.action.types.ActionStatus;
36 import org.openecomp.sdc.action.types.OpenEcompComponent;
37
38 import javax.activation.DataHandler;
39 import javax.activation.DataSource;
40 import javax.servlet.http.HttpServletRequest;
41 import javax.ws.rs.core.Response;
42 import java.io.ByteArrayInputStream;
43 import java.io.IOException;
44 import java.util.ArrayList;
45 import java.util.Date;
46 import java.util.List;
47
48 import static org.mockito.Matchers.any;
49 import static org.mockito.Matchers.anyString;
50 import static org.mockito.Mockito.times;
51 import static org.mockito.Mockito.when;
52 import static org.openecomp.sdc.action.ActionConstants.X_OPEN_ECOMP_INSTANCE_ID_HEADER_PARAM;
53 import static org.openecomp.sdc.action.ActionConstants.X_OPEN_ECOMP_REQUEST_ID_HEADER_PARAM;
54
55 public class ActionsImplTest {
56
57     @Mock
58     HttpServletRequest request;
59     @Mock
60     private ActionManager actionManager;
61     @InjectMocks
62     ActionsImpl action ;
63
64     @Before
65     public void init() {
66         MockitoAnnotations.initMocks(this);
67         when(request.getHeader(X_OPEN_ECOMP_INSTANCE_ID_HEADER_PARAM)).thenReturn("X-OPEN-ECOMP-InstanceID");
68         when(request.getHeader(X_OPEN_ECOMP_REQUEST_ID_HEADER_PARAM)).thenReturn("X-OPEN-ECOMP-RequestID");
69     }
70
71     @Test
72     public void testGetActionsByActionInvariantUuIdShouldWithEmptyQueryString() {
73
74         when(request.getQueryString()).thenReturn("");
75         when(actionManager.getActionsByActionInvariantUuId(anyString())).thenReturn(mockActionsToReturn());
76
77         Response actionsByActionInvariantUuId = action.getActionsByActionInvariantUuId("invariantId", "actionUUID", request);
78         Assert.assertEquals(200, actionsByActionInvariantUuId.getStatus());
79     }
80
81     @Test
82     public void testGetActionsByActionInvariantUuIdShouldWithQueryString() {
83         when(request.getQueryString()).thenReturn("queryString");
84         when(actionManager.getActionsByActionUuId(anyString())).thenReturn(createAction());
85
86         Response actionsByActionInvariantUuId = action.getActionsByActionInvariantUuId("actionInvariantUuId", "actionUUID", request);
87         Assert.assertEquals(200, actionsByActionInvariantUuId.getStatus());
88     }
89
90     @Test(expected = ActionException.class)
91     public void testGetActionsByActionInvariantUuIdShouldThrowExceptionWhenActionIsEmpty() {
92         when(request.getQueryString()).thenReturn("queryString");
93         when(actionManager.getActionsByActionUuId(anyString())).thenReturn(new Action());
94
95         action.getActionsByActionInvariantUuId("actionInvariantUuId", "actionUUID", request);
96     }
97
98     @Test
99     public void testGetOpenEcompComponentsShouldPassForHappyScenario() {
100         ArrayList<OpenEcompComponent> ecompComponents = new ArrayList<>();
101         ecompComponents.add(new OpenEcompComponent());
102         when(actionManager.getOpenEcompComponents()).thenReturn(ecompComponents);
103         Assert.assertEquals(200, action.getOpenEcompComponents(request).getStatus());
104     }
105
106     @Test(expected = ActionException.class)
107     public void testGetOpenEcompComponentsShouldCatchActionException() {
108         when(actionManager.getOpenEcompComponents()).thenThrow(new ActionException());
109          action.getOpenEcompComponents(request).getStatus();
110     }
111
112     @Test
113     public void testGetFilteredActionsShouldPassWhenQueryStringIsEmpty() {
114         when(actionManager.getFilteredActions(anyString(), anyString())).thenReturn(mockActionsToReturn());
115         Response filteredActions = action.getFilteredActions("vendor", "category", "name", "modelID",
116                 "componentID", request);
117
118         Assert.assertEquals(200, filteredActions.getStatus());
119     }
120
121     @Test
122     public void testGetFilteredActionsShouldPassWhenQueryStringIsNotEmptyWithVendor() {
123         when(request.getQueryString()).thenReturn("queryString");
124         when(actionManager.getFilteredActions(anyString(), anyString())).thenReturn(mockActionsToReturn());
125         Response filteredActions = action.getFilteredActions("vendor", null, null, null,
126                 null, request);
127
128         Assert.assertEquals(200, filteredActions.getStatus());
129     }
130
131     @Test
132     public void testGetFilteredActionsShouldPassWhenQueryStringIsNotEmptyWithCategory() {
133         when(request.getQueryString()).thenReturn("queryString");
134         when(actionManager.getFilteredActions(anyString(), anyString())).thenReturn(mockActionsToReturn());
135         Response filteredActions = action.getFilteredActions(null, "category", null, null,
136                 null, request);
137
138         Assert.assertEquals(200, filteredActions.getStatus());
139     }
140
141     @Test
142     public void testGetFilteredActionsShouldPassWhenQueryStringIsNotEmptyWithName() {
143         when(request.getQueryString()).thenReturn("queryString");
144         when(actionManager.getFilteredActions(anyString(), anyString())).thenReturn(mockActionsToReturn());
145         Response filteredActions = action.getFilteredActions(null, null, "name", null,
146                 null, request);
147
148         Assert.assertEquals(200, filteredActions.getStatus());
149     }
150
151     @Test
152     public void testGetFilteredActionsShouldWhenQueryStringIsNotEmptyWithModel() {
153         when(request.getQueryString()).thenReturn("queryString");
154         when(actionManager.getFilteredActions(anyString(), anyString())).thenReturn(mockActionsToReturn());
155         Response filteredActions = action.getFilteredActions(null, null, null, "modelId",
156                 null, request);
157
158         Assert.assertEquals(200, filteredActions.getStatus());
159     }
160
161     @Test
162     public void testGetFilteredActionsShouldPassWhenQueryStringIsNotEmptyWithComponent() {
163         when(request.getQueryString()).thenReturn("queryString");
164         when(actionManager.getFilteredActions(anyString(), anyString())).thenReturn(mockActionsToReturn());
165         Response filteredActions = action.getFilteredActions(null, null, null, null,
166                 "componentId", request);
167
168         Assert.assertEquals(200, filteredActions.getStatus());
169     }
170
171     @Test(expected = ActionException.class)
172     public void testGetFilteredActionsShouldThrowActionExceptionWhenNumberOfFiltersAreZero() {
173         when(request.getQueryString()).thenReturn("queryString");
174         when(actionManager.getFilteredActions(anyString(), anyString())).thenReturn(mockActionsToReturn());
175         action.getFilteredActions(null, null, null, null,
176                 null, request);
177
178     }
179
180     @Test
181     public void testCreateActionShouldPassForHappyScenario() {
182     String requestJson = "{actionUuId : actionUuId, actionInvariantUuId : actionInvariantUuId," +
183             " name : actionToCreate, version: 2.1 }";
184         when(actionManager.createAction(any(Action.class), anyString())).thenReturn(createAction());
185         Assert.assertEquals(200, action.createAction( requestJson, request).getStatus());
186     }
187     @Test(expected = ActionException.class)
188     public void testCreateActionShouldFailForInvalidRequestJson() {
189         String requestJson = "{actionUuId : actionUuId, actionInvariantUuId : actionInvariantUuId," +
190                 "  version: 2.1 }";
191         when(actionManager.createAction(any(Action.class), anyString())).thenReturn(createAction());
192         action.createAction( requestJson, request);
193     }
194
195     @Test
196     public void testUpdateActionShouldPassForHappyScenario() {
197         String requestJson = "{actionUuId : actionUuId, actionInvariantUuId : actionInvariantUuId," +
198                 " name : actionToUpdate, version: 2.2 }";
199
200         when(request.getRemoteUser()).thenReturn("remoteUser");
201         when(actionManager.updateAction(any(Action.class), anyString())).thenReturn(createAction());
202
203         Assert.assertEquals(200, action.updateAction("invariantUUID", requestJson, request).getStatus());
204     }
205
206     @Test(expected = ActionException.class)
207     public void testUpdateActionShouldThrowActionExceptionWhenActionManagerUpdateFails() {
208         String requestJson = "{actionUuId : actionUuId, actionInvariantUuId : actionInvariantUuId," +
209                 " name : actionToUpdate, version: 2.2 }";
210
211         when(request.getRemoteUser()).thenReturn("remoteUser");
212         when(actionManager.updateAction(any(Action.class), anyString())).thenThrow(new ActionException());
213         action.updateAction("invariantUUID", requestJson, request);
214     }
215
216     @Test
217     public void testDeleteActionShouldPassForHappyScenario() {
218        Assert.assertEquals(200, action.deleteAction("actionInvariantUUID", request).getStatus());
219        Mockito.verify(actionManager, times(1)).deleteAction(anyString(), anyString());
220     }
221
222     @Test
223     public void testActOnActionShouldPassForStatusCheckout() {
224         String requestJson = "{status : Checkout}";
225         when(request.getRemoteUser()).thenReturn("remoteUser");
226         when(actionManager.checkout(anyString(), anyString())).thenReturn(createAction());
227
228        Assert.assertEquals(200, action.actOnAction("invariantUUID", requestJson, request).getStatus());
229
230     }
231
232     @Test
233     public void testActOnActionShouldPassForStatusUndo_Checkout() {
234         String requestJson = "{status : Undo_Checkout}";
235         when(request.getRemoteUser()).thenReturn("remoteUser");
236
237         Assert.assertEquals(200, action.actOnAction("invariantUUID", requestJson, request).getStatus());
238         Mockito.verify(actionManager, times(1)).undoCheckout(anyString(), anyString());
239     }
240
241     @Test
242     public void testActOnActionShouldPassForStatusCheckin() {
243         String requestJson = "{status : Checkin}";
244         when(request.getRemoteUser()).thenReturn("remoteUser");
245         when(actionManager.checkin(anyString(), anyString())).thenReturn(createAction());
246         Assert.assertEquals(200, action.actOnAction("invariantUUID", requestJson, request).getStatus());
247     }
248
249     @Test
250     public void testActOnActionShouldPassForStatusSubmit() {
251         String requestJson = "{status : Submit}";
252         when(request.getRemoteUser()).thenReturn("remoteUser");
253         when(actionManager.submit(anyString(), anyString())).thenReturn(createAction());
254         Assert.assertEquals(200, action.actOnAction("invariantUUID", requestJson, request).getStatus());
255     }
256
257
258     @Test(expected = ActionException.class)
259     public void testActOnActionShouldThrowActionExceptionWhenPassingInvalidAction() {
260         String requestJson = "{status : Status}";
261         when(request.getRemoteUser()).thenReturn("remoteUser");
262         action.actOnAction("invariantUUID", requestJson, request);
263     }
264
265
266     @Test
267     public void testUploadArtifactShouldPassForHappyScenario() throws IOException {
268
269         Attachment artifactToUpload = new Attachment("id", "mediaType", new Object());
270         DataSource dataSource = new AttachmentDataSource("ctParam", new ByteArrayInputStream(new byte[0]));
271         DataHandler dataHandler = new DataHandler(dataSource);
272         artifactToUpload.setDataHandler(dataHandler);
273
274         when(request.getContentType()).thenReturn("contentType");
275         when(actionManager.uploadArtifact(any(ActionArtifact.class), anyString(), anyString())).thenReturn(new ActionArtifact());
276         Response response = action.uploadArtifact("actionInvariantUUID", "artifactName", "artifactLabel",
277                 "artifactCategory", "artifactDescription", "readOnly",
278                 "d41d8cd98f00b204e9800998ecf8427e",
279                 artifactToUpload, request);
280
281         Assert.assertEquals(200, response.getStatus());
282     }
283
284     @Test(expected = ActionException.class)
285     public void testUploadArtifactShouldThrowActionExceptionWhenArtifactToUploadIsNull() throws IOException {
286         when(request.getContentType()).thenReturn("contentType");
287         action.uploadArtifact("actionInvariantUUID", "artifactName", "artifactLabel",
288                 "artifactCategory", "artifactDescription", "readOnly",
289                 "d41d8cd98f00b204e9800998ecf8427e",
290                 null, request);
291
292     }
293
294     @Test
295     public void testDownloadArtifactShouldPassForHappyScenario() {
296         ActionArtifact actionArtifact = new ActionArtifact();
297         actionArtifact.setArtifactUuId("artifactUUID");
298         actionArtifact.setArtifact(new byte[0]);
299         actionArtifact.setArtifactName("artifactName");
300
301         when(actionManager.downloadArtifact(anyString(), anyString())).thenReturn(actionArtifact);
302         Response response = action.downloadArtifact("actionUUID", "artifactUUID", request);
303         Assert.assertEquals(200, response.getStatus());
304     }
305
306     @Test(expected = ActionException.class)
307     public void testDownloadArtifactShouldThrowActionExceptionWhenReDownloadedArtifactIsEmpty() {
308
309         when(actionManager.downloadArtifact(anyString(), anyString())).thenReturn(new ActionArtifact());
310         action.downloadArtifact("actionUUID", "artifactUUID", request);
311     }
312
313     @Test
314     public void testDeleteArtifactShouldPassForHappyScenario() {
315         action.deleteArtifact("actionInvariantUUID", "artifactUUID", request);
316         Mockito.verify(actionManager, times(1)).deleteArtifact(anyString(), anyString(), anyString());
317     }
318
319     @Test
320     public void testUpdateArtifactShouldPassForHappyScenario() throws IOException {
321         Attachment artifactToUpdate = new Attachment("id", "mediaType", new Object());
322         DataSource dataSource = new AttachmentDataSource("ctParam", new ByteArrayInputStream(new byte[0]));
323         DataHandler dataHandler = new DataHandler(dataSource);
324         artifactToUpdate.setDataHandler(dataHandler);
325
326         when(request.getContentType()).thenReturn("contentType");
327
328         Response response = action.updateArtifact("actionInvariantUUID", "artifactUUID", "artifactName",
329                 "artifactLabel",
330                 "artifactCategory", "artifactDescription", "readWrite",
331                 "d41d8cd98f00b204e9800998ecf8427e",
332                 artifactToUpdate, request);
333
334         Mockito.verify(actionManager, times(1)).updateArtifact(any(ActionArtifact.class), anyString(), anyString());
335         Assert.assertEquals(200, response.getStatus());
336     }
337
338
339     @Test(expected = ActionException.class)
340     public void testUpdateArtifactShouldThrowActionExceptionWhenCheckSumDidNotMatchWithCalculatedCheckSum() throws IOException {
341
342         Attachment artifactToUpdate = new Attachment("id", "mediaType", new Object());
343         DataSource dataSource = new AttachmentDataSource("ctParam", new ByteArrayInputStream(new byte[0]));
344         DataHandler dataHandler = new DataHandler(dataSource);
345         artifactToUpdate.setDataHandler(dataHandler);
346
347         when(request.getContentType()).thenReturn("contentType");
348         action.updateArtifact("actionInvariantUUID", "artifactUUID", "artifactName",
349                 "artifactLabel",
350                 "artifactCategory", "artifactDescription", "readWrite",
351                 "checksum",
352                 artifactToUpdate, request);
353
354     }
355
356     private List<Action> mockActionsToReturn() {
357         List<Action> actionList = new ArrayList<>();
358         Action action = createAction();
359
360         actionList.add(action);
361         return actionList;
362     }
363
364     private Action createAction() {
365         Action action = new Action();
366         action.setActionInvariantUuId("actionInvariantUuId");
367         action.setVersion("1.1");
368         action.setUser("user");
369         action.setStatus(ActionStatus.Available);
370         action.setTimestamp(new Date());
371
372         action.setData("{actionUuId : actionUuId, actionInvariantUuId : actionInvariantUuId," +
373                 " name : actionToupdate,version: 2.1 ," +
374                 " artifacts : [{artifactUuId: artifactUuId ,artifactName : artifactName," +
375                 "artifactLabel: artifactLabel, artifactProtection : readWrite, artifactCategory : artifactCategory," +
376                 "artifactDescription: artifactDescription}] }");
377         return action;
378     }
379
380 }