3 * Copyright © 2017-2018 European Support Limited
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 package org.openecomp.sdcrests.action.rest.services;
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;
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;
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;
55 public class ActionsImplTest {
58 HttpServletRequest request;
60 private ActionManager actionManager;
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");
72 public void testGetActionsByActionInvariantUuIdShouldWithEmptyQueryString() {
74 when(request.getQueryString()).thenReturn("");
75 when(actionManager.getActionsByActionInvariantUuId(anyString())).thenReturn(mockActionsToReturn());
77 Response actionsByActionInvariantUuId = action.getActionsByActionInvariantUuId("invariantId", "actionUUID", request);
78 Assert.assertEquals(200, actionsByActionInvariantUuId.getStatus());
82 public void testGetActionsByActionInvariantUuIdShouldWithQueryString() {
83 when(request.getQueryString()).thenReturn("queryString");
84 when(actionManager.getActionsByActionUuId(anyString())).thenReturn(createAction());
86 Response actionsByActionInvariantUuId = action.getActionsByActionInvariantUuId("actionInvariantUuId", "actionUUID", request);
87 Assert.assertEquals(200, actionsByActionInvariantUuId.getStatus());
90 @Test(expected = ActionException.class)
91 public void testGetActionsByActionInvariantUuIdShouldThrowExceptionWhenActionIsEmpty() {
92 when(request.getQueryString()).thenReturn("queryString");
93 when(actionManager.getActionsByActionUuId(anyString())).thenReturn(new Action());
95 action.getActionsByActionInvariantUuId("actionInvariantUuId", "actionUUID", request);
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());
106 @Test(expected = ActionException.class)
107 public void testGetOpenEcompComponentsShouldCatchActionException() {
108 when(actionManager.getOpenEcompComponents()).thenThrow(new ActionException());
109 action.getOpenEcompComponents(request).getStatus();
113 public void testGetFilteredActionsShouldPassWhenQueryStringIsEmpty() {
114 when(actionManager.getFilteredActions(anyString(), anyString())).thenReturn(mockActionsToReturn());
115 Response filteredActions = action.getFilteredActions("vendor", "category", "name", "modelID",
116 "componentID", request);
118 Assert.assertEquals(200, filteredActions.getStatus());
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,
128 Assert.assertEquals(200, filteredActions.getStatus());
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,
138 Assert.assertEquals(200, filteredActions.getStatus());
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,
148 Assert.assertEquals(200, filteredActions.getStatus());
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",
158 Assert.assertEquals(200, filteredActions.getStatus());
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);
168 Assert.assertEquals(200, filteredActions.getStatus());
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,
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());
187 @Test(expected = ActionException.class)
188 public void testCreateActionShouldFailForInvalidRequestJson() {
189 String requestJson = "{actionUuId : actionUuId, actionInvariantUuId : actionInvariantUuId," +
191 when(actionManager.createAction(any(Action.class), anyString())).thenReturn(createAction());
192 action.createAction( requestJson, request);
196 public void testUpdateActionShouldPassForHappyScenario() {
197 String requestJson = "{actionUuId : actionUuId, actionInvariantUuId : actionInvariantUuId," +
198 " name : actionToUpdate, version: 2.2 }";
200 when(request.getRemoteUser()).thenReturn("remoteUser");
201 when(actionManager.updateAction(any(Action.class), anyString())).thenReturn(createAction());
203 Assert.assertEquals(200, action.updateAction("invariantUUID", requestJson, request).getStatus());
206 @Test(expected = ActionException.class)
207 public void testUpdateActionShouldThrowActionExceptionWhenActionManagerUpdateFails() {
208 String requestJson = "{actionUuId : actionUuId, actionInvariantUuId : actionInvariantUuId," +
209 " name : actionToUpdate, version: 2.2 }";
211 when(request.getRemoteUser()).thenReturn("remoteUser");
212 when(actionManager.updateAction(any(Action.class), anyString())).thenThrow(new ActionException());
213 action.updateAction("invariantUUID", requestJson, request);
217 public void testDeleteActionShouldPassForHappyScenario() {
218 Assert.assertEquals(200, action.deleteAction("actionInvariantUUID", request).getStatus());
219 Mockito.verify(actionManager, times(1)).deleteAction(anyString(), anyString());
223 public void testActOnActionShouldPassForStatusCheckout() {
224 String requestJson = "{status : Checkout}";
225 when(request.getRemoteUser()).thenReturn("remoteUser");
226 when(actionManager.checkout(anyString(), anyString())).thenReturn(createAction());
228 Assert.assertEquals(200, action.actOnAction("invariantUUID", requestJson, request).getStatus());
233 public void testActOnActionShouldPassForStatusUndo_Checkout() {
234 String requestJson = "{status : Undo_Checkout}";
235 when(request.getRemoteUser()).thenReturn("remoteUser");
237 Assert.assertEquals(200, action.actOnAction("invariantUUID", requestJson, request).getStatus());
238 Mockito.verify(actionManager, times(1)).undoCheckout(anyString(), anyString());
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());
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());
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);
267 public void testUploadArtifactShouldPassForHappyScenario() throws IOException {
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);
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);
281 Assert.assertEquals(200, response.getStatus());
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",
295 public void testDownloadArtifactShouldPassForHappyScenario() {
296 ActionArtifact actionArtifact = new ActionArtifact();
297 actionArtifact.setArtifactUuId("artifactUUID");
298 actionArtifact.setArtifact(new byte[0]);
299 actionArtifact.setArtifactName("artifactName");
301 when(actionManager.downloadArtifact(anyString(), anyString())).thenReturn(actionArtifact);
302 Response response = action.downloadArtifact("actionUUID", "artifactUUID", request);
303 Assert.assertEquals(200, response.getStatus());
306 @Test(expected = ActionException.class)
307 public void testDownloadArtifactShouldThrowActionExceptionWhenReDownloadedArtifactIsEmpty() {
309 when(actionManager.downloadArtifact(anyString(), anyString())).thenReturn(new ActionArtifact());
310 action.downloadArtifact("actionUUID", "artifactUUID", request);
314 public void testDeleteArtifactShouldPassForHappyScenario() {
315 action.deleteArtifact("actionInvariantUUID", "artifactUUID", request);
316 Mockito.verify(actionManager, times(1)).deleteArtifact(anyString(), anyString(), anyString());
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);
326 when(request.getContentType()).thenReturn("contentType");
328 Response response = action.updateArtifact("actionInvariantUUID", "artifactUUID", "artifactName",
330 "artifactCategory", "artifactDescription", "readWrite",
331 "d41d8cd98f00b204e9800998ecf8427e",
332 artifactToUpdate, request);
334 Mockito.verify(actionManager, times(1)).updateArtifact(any(ActionArtifact.class), anyString(), anyString());
335 Assert.assertEquals(200, response.getStatus());
339 @Test(expected = ActionException.class)
340 public void testUpdateArtifactShouldThrowActionExceptionWhenCheckSumDidNotMatchWithCalculatedCheckSum() throws IOException {
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);
347 when(request.getContentType()).thenReturn("contentType");
348 action.updateArtifact("actionInvariantUUID", "artifactUUID", "artifactName",
350 "artifactCategory", "artifactDescription", "readWrite",
352 artifactToUpdate, request);
356 private List<Action> mockActionsToReturn() {
357 List<Action> actionList = new ArrayList<>();
358 Action action = createAction();
360 actionList.add(action);
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());
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}] }");