From dac7a8bcf816d1271d88ea065733cb11a0019bcc Mon Sep 17 00:00:00 2001 From: ehardon Date: Tue, 14 Apr 2020 17:22:33 +0300 Subject: [PATCH] RM-6645: Added unit test for getRmActionDefinitions webscript when whitelisted actions are present --- .../scripts/rule/RmActionDefinitionsGet.java | 6 +- .../RmActionDefinitionsGetUnitTest.java | 124 ++++++++++++++++++ 2 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 rm-community/rm-community-repo/unit-test/java/org/alfresco/repo/web/scripts/actions/RmActionDefinitionsGetUnitTest.java diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/repo/web/scripts/rule/RmActionDefinitionsGet.java b/rm-community/rm-community-repo/source/java/org/alfresco/repo/web/scripts/rule/RmActionDefinitionsGet.java index 1dee37479d..6186b0e64e 100644 --- a/rm-community/rm-community-repo/source/java/org/alfresco/repo/web/scripts/rule/RmActionDefinitionsGet.java +++ b/rm-community/rm-community-repo/source/java/org/alfresco/repo/web/scripts/rule/RmActionDefinitionsGet.java @@ -82,8 +82,10 @@ public class RmActionDefinitionsGet extends DeclarativeWebScript } } // If there are any DM whitelisted actions for RM add them in the rule actions - for (ActionDefinition actionDefinition: actions) { - if (whitelistedActions.contains(actionDefinition.getName())){ + for (ActionDefinition actionDefinition : actions) + { + if (whitelistedActions.contains(actionDefinition.getName())) + { defs.add(actionDefinition); } } diff --git a/rm-community/rm-community-repo/unit-test/java/org/alfresco/repo/web/scripts/actions/RmActionDefinitionsGetUnitTest.java b/rm-community/rm-community-repo/unit-test/java/org/alfresco/repo/web/scripts/actions/RmActionDefinitionsGetUnitTest.java new file mode 100644 index 0000000000..62d41227b4 --- /dev/null +++ b/rm-community/rm-community-repo/unit-test/java/org/alfresco/repo/web/scripts/actions/RmActionDefinitionsGetUnitTest.java @@ -0,0 +1,124 @@ +/* + * #%L + * Alfresco Records Management Module + * %% + * Copyright (C) 2005 - 2020 Alfresco Software Limited + * %% + * This file is part of the Alfresco software. + * - + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is + * provided under the following open source license terms: + * - + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * - + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * - + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + * #L% + */ +package org.alfresco.repo.web.scripts.actions; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import com.fasterxml.jackson.databind.ObjectMapper; + +import org.alfresco.module.org_alfresco_module_rm.action.RecordsManagementActionService; +import org.alfresco.module.org_alfresco_module_rm.model.RecordsManagementModel; +import org.alfresco.module.org_alfresco_module_rm.test.util.BaseWebScriptUnitTest; +import org.alfresco.repo.action.ActionDefinitionImpl; +import org.alfresco.repo.web.scripts.rule.RmActionDefinitionsGet; +import org.alfresco.repo.web.scripts.rule.WhitelistedDMActions; +import org.alfresco.service.cmr.action.ActionDefinition; +import org.alfresco.service.cmr.action.ActionService; +import org.json.JSONObject; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.extensions.webscripts.AbstractWebScript; + +public class RmActionDefinitionsGetUnitTest extends BaseWebScriptUnitTest implements RecordsManagementModel +{ + @Mock + private RecordsManagementActionService recordsManagementActionService; + @Mock + private ActionService mockedExtendedActionService; + + @InjectMocks + private RmActionDefinitionsGet webScript; + + private List whitelistedActions = WhitelistedDMActions.getActionsList(); + + @Override + protected AbstractWebScript getWebScript() + { + return webScript; + } + + @Override + protected String getWebScriptTemplate() + { + return "alfresco/templates/webscripts/org/alfresco/repository/rule/rm-actiondefinitions.get.json.ftl"; + } + + /** + * Before test + */ + @Before + public void before() + { + MockitoAnnotations.initMocks(this); + webScript.setRecordsManagementActionService(recordsManagementActionService); + webScript.setExtendedActionService(mockedExtendedActionService); + } + + /** + * Given the extendedActionService contains all dm actions + * When retrieving RM Action Definitions + * Then the whitelisted dm actions are present in the response + */ + @Test + public void getRmActionDefinitionsWithWhitelistedDMActions() throws Exception + { + List dmActionDefinitions = new ArrayList<>(); + + for (String action: whitelistedActions) { + dmActionDefinitions.add(new ActionDefinitionImpl(action)); + } + + when(mockedRecordsManagementActionService.getRecordsManagementActions()).thenReturn(Collections.emptyList()); + when(mockedExtendedActionService.getActionDefinitions()).thenReturn(dmActionDefinitions); + + JSONObject jsonResponse = executeJSONWebScript(Collections.emptyMap()); + assertNotNull(jsonResponse); + String responseJSONString = jsonResponse.toString(); + + ObjectMapper mapper = new ObjectMapper(); + Map>> responseMap = mapper.readValue(responseJSONString, Map.class); + ArrayList> actionDefinitions = responseMap.get("data"); + + assertEquals(WhitelistedDMActions.getActionsList().size(), actionDefinitions.size()); + + for (LinkedHashMap actionDefinition: actionDefinitions) { + assertTrue(whitelistedActions.contains(actionDefinition.get("name"))); + } + } +}