From b9bb54579e050f3294da7af251e044dfee17fe7c Mon Sep 17 00:00:00 2001 From: Silviu Dinuta Date: Tue, 27 Sep 2016 12:30:43 +0300 Subject: [PATCH] minor formating issue --- .../roles/DynamicAuthoritiesGetUnitTest.java | 587 +++++++++--------- 1 file changed, 296 insertions(+), 291 deletions(-) diff --git a/rm-server/unit-test/java/org/alfresco/repo/web/scripts/roles/DynamicAuthoritiesGetUnitTest.java b/rm-server/unit-test/java/org/alfresco/repo/web/scripts/roles/DynamicAuthoritiesGetUnitTest.java index 9ee07f681b..d3a3280cf0 100644 --- a/rm-server/unit-test/java/org/alfresco/repo/web/scripts/roles/DynamicAuthoritiesGetUnitTest.java +++ b/rm-server/unit-test/java/org/alfresco/repo/web/scripts/roles/DynamicAuthoritiesGetUnitTest.java @@ -1,28 +1,28 @@ -/* - * Copyright (C) 2005-2014 Alfresco Software Limited. - * - * This file is part of Alfresco - * - * 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 . - */ - -package org.alfresco.repo.web.scripts.roles; - -import static java.util.Collections.emptyMap; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +/* + * Copyright (C) 2005-2014 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * 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 . + */ + +package org.alfresco.repo.web.scripts.roles; + +import static java.util.Collections.emptyMap; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyBoolean; @@ -111,185 +111,187 @@ public class DynamicAuthoritiesGetUnitTest extends BaseWebScriptUnitTest impleme private RetryingTransactionHelper mockedRetryingTransactionHelper; @Mock private ContentStreamer contentStreamer; - - /** test component */ - @InjectMocks - private DynamicAuthoritiesGet webScript; - - @Override + + /** test component */ + @InjectMocks + private DynamicAuthoritiesGet webScript; + + @Override protected AbstractWebScript getWebScript() - { - return webScript; - } - - @Override - protected String getWebScriptTemplate() - { - return "alfresco/templates/webscripts/org/alfresco/repository/roles/rm-dynamicauthorities.get.json.ftl"; - } - - /** - * Before test - */ - @SuppressWarnings("unchecked") - @Before - public void before() - { - MockitoAnnotations.initMocks(this); - webScript.setNodeService(mockedNodeService); - webScript.setPermissionService(mockedPermissionService); - webScript.setExtendedSecurityService(mockedExtendedSecurityService); - // setup retrying transaction helper - Answer doInTransactionAnswer = new Answer() - { - @SuppressWarnings("rawtypes") - @Override - public Object answer(InvocationOnMock invocation) throws Throwable - { - RetryingTransactionCallback callback = (RetryingTransactionCallback) invocation.getArguments()[0]; - return callback.execute(); - } - }; - - doAnswer(doInTransactionAnswer).when(mockedRetryingTransactionHelper) - . doInTransaction(any(RetryingTransactionCallback.class), anyBoolean(), anyBoolean()); - - when(mockedTransactionService.getRetryingTransactionHelper()).thenReturn(mockedRetryingTransactionHelper); - - // max node id - when(mockedPatchDAO.getMaxAdmNodeID()).thenReturn(500000L); - - // aspect - when(mockedQnameDAO.getQName(ASPECT_EXTENDED_SECURITY)).thenReturn(new Pair(ASPECT_ID, ASPECT)); - } - - /** - * Given that there are no nodes with the extended security aspect When the action is executed Nothing happens - * - * @throws Exception - */ - @SuppressWarnings({ "unchecked" }) - @Test - public void noNodesWithExtendedSecurity() throws Exception - { - when(mockedPatchDAO.getNodesByAspectQNameId(eq(ASPECT_ID), anyLong(), anyLong())) - .thenReturn(Collections.emptyList()); - - // Set up parameters. - Map parameters = ImmutableMap.of("batchsize", "10", "maxProcessedRecords", "3"); - JSONObject json = executeJSONWebScript(parameters); - assertNotNull(json); - String actualJSONString = json.toString(); - - // Check the JSON result using Jackson to allow easy equality testing. - ObjectMapper mapper = new ObjectMapper(); - String expectedJSONString = "{\"responsestatus\":\"success\",\"message\":\"Processed 0 records.\"}"; - assertEquals(mapper.readTree(expectedJSONString), mapper.readTree(actualJSONString)); - - verify(mockedNodeService, never()).getProperty(any(NodeRef.class), eq(PROP_READERS)); - verify(mockedNodeService, never()).getProperty(any(NodeRef.class), eq(PROP_WRITERS)); - verify(mockedNodeService, never()).removeAspect(any(NodeRef.class), eq(ASPECT_EXTENDED_SECURITY)); - verify(mockedPermissionService, never()).clearPermission(any(NodeRef.class), - eq(ExtendedReaderDynamicAuthority.EXTENDED_READER)); - verify(mockedPermissionService, never()).clearPermission(any(NodeRef.class), - eq(ExtendedWriterDynamicAuthority.EXTENDED_WRITER)); - verify(mockedExtendedSecurityService, never()).set(any(NodeRef.class), any(Set.class), any(Set.class)); - } - - /** - * Given that there are records with the extended security aspect When the action is executed Then the aspect is - * removed And the dynamic authorities permissions are cleared And extended security is set via the updated API - * - * @throws Exception - */ - @SuppressWarnings("unchecked") - @Test - public void recordsWithExtendedSecurityAspect() throws Exception - { - List ids = Stream.of(1l, 2l, 3l).collect(Collectors.toList()); - - when(mockedPatchDAO.getNodesByAspectQNameId(eq(ASPECT_ID), anyLong(), anyLong())).thenReturn(ids) - .thenReturn(Collections.emptyList()); - - ids.stream().forEach((i) -> { - NodeRef nodeRef = AlfMock.generateNodeRef(mockedNodeService); - when(mockedNodeDAO.getNodePair(i)).thenReturn(new Pair(i, nodeRef)); - when(mockedNodeService.hasAspect(nodeRef, ASPECT_RECORD)).thenReturn(true); - when(mockedNodeService.getProperty(nodeRef, PROP_READERS)) - .thenReturn((Serializable) Collections.emptyMap()); - when(mockedNodeService.getProperty(nodeRef, PROP_WRITERS)) - .thenReturn((Serializable) Collections.emptyMap()); - - }); - - // Set up parameters. - Map parameters = ImmutableMap.of("batchsize", "10", "maxProcessedRecords", "4"); - JSONObject json = executeJSONWebScript(parameters); - assertNotNull(json); - String actualJSONString = json.toString(); - ObjectMapper mapper = new ObjectMapper(); - String expectedJSONString = "{\"responsestatus\":\"success\",\"message\":\"Processed 3 records.\"}"; - assertEquals(mapper.readTree(expectedJSONString), mapper.readTree(actualJSONString)); - - verify(mockedNodeService, times(3)).getProperty(any(NodeRef.class), eq(PROP_READERS)); - verify(mockedNodeService, times(3)).getProperty(any(NodeRef.class), eq(PROP_WRITERS)); - verify(mockedNodeService, times(3)).removeAspect(any(NodeRef.class), eq(ASPECT_EXTENDED_SECURITY)); - verify(mockedPermissionService, times(3)).clearPermission(any(NodeRef.class), - eq(ExtendedReaderDynamicAuthority.EXTENDED_READER)); - verify(mockedPermissionService, times(3)).clearPermission(any(NodeRef.class), - eq(ExtendedWriterDynamicAuthority.EXTENDED_WRITER)); - verify(mockedExtendedSecurityService, times(3)).set(any(NodeRef.class), any(Set.class), any(Set.class)); - - } - - /** - * Given that there are non-records with the extended security aspect When the web script is executed Then the - * aspect is removed And the dynamic authorities permissions are cleared - * - * @throws Exception - */ - @SuppressWarnings("unchecked") - @Test - public void nonRecordsWithExtendedSecurityAspect() throws Exception - { - List ids = Stream.of(1l, 2l, 3l).collect(Collectors.toList()); - - when(mockedPatchDAO.getNodesByAspectQNameId(eq(ASPECT_ID), anyLong(), anyLong())).thenReturn(ids) - .thenReturn(Collections.emptyList()); - - ids.stream().forEach((i) -> { - NodeRef nodeRef = AlfMock.generateNodeRef(mockedNodeService); - when(mockedNodeDAO.getNodePair(i)).thenReturn(new Pair(i, nodeRef)); - when(mockedNodeService.hasAspect(nodeRef, ASPECT_RECORD)).thenReturn(false); - when(mockedNodeService.getProperty(nodeRef, PROP_READERS)) - .thenReturn((Serializable) Collections.emptyMap()); - when(mockedNodeService.getProperty(nodeRef, PROP_WRITERS)) - .thenReturn((Serializable) Collections.emptyMap()); - - }); - - // Set up parameters. - Map parameters = ImmutableMap.of("batchsize", "10", "maxProcessedRecords", "4"); - JSONObject json = executeJSONWebScript(parameters); - assertNotNull(json); - String actualJSONString = json.toString(); - ObjectMapper mapper = new ObjectMapper(); - String expectedJSONString = "{\"responsestatus\":\"success\",\"message\":\"Processed 3 records.\"}"; - assertEquals(mapper.readTree(expectedJSONString), mapper.readTree(actualJSONString)); - - verify(mockedNodeService, times(3)).getProperty(any(NodeRef.class), eq(PROP_READERS)); - verify(mockedNodeService, times(3)).getProperty(any(NodeRef.class), eq(PROP_WRITERS)); - verify(mockedNodeService, times(3)).removeAspect(any(NodeRef.class), eq(ASPECT_EXTENDED_SECURITY)); - verify(mockedPermissionService, times(3)).clearPermission(any(NodeRef.class), - eq(ExtendedReaderDynamicAuthority.EXTENDED_READER)); - verify(mockedPermissionService, times(3)).clearPermission(any(NodeRef.class), - eq(ExtendedWriterDynamicAuthority.EXTENDED_WRITER)); - verify(mockedExtendedSecurityService, never()).set(any(NodeRef.class), any(Set.class), any(Set.class)); - } - - @Test - public void missingBatchSizeParameter() throws Exception - { + { + return webScript; + } + + @Override + protected String getWebScriptTemplate() + { + return "alfresco/templates/webscripts/org/alfresco/repository/roles/rm-dynamicauthorities.get.json.ftl"; + } + + /** + * Before test + */ + @SuppressWarnings("unchecked") + @Before + public void before() + { + MockitoAnnotations.initMocks(this); + webScript.setNodeService(mockedNodeService); + webScript.setPermissionService(mockedPermissionService); + webScript.setExtendedSecurityService(mockedExtendedSecurityService); + // setup retrying transaction helper + Answer doInTransactionAnswer = new Answer() + { + @SuppressWarnings("rawtypes") + @Override + public Object answer(InvocationOnMock invocation) throws Throwable + { + RetryingTransactionCallback callback = (RetryingTransactionCallback) invocation.getArguments()[0]; + return callback.execute(); + } + }; + + doAnswer(doInTransactionAnswer).when(mockedRetryingTransactionHelper) + . doInTransaction(any(RetryingTransactionCallback.class), anyBoolean(), anyBoolean()); + + when(mockedTransactionService.getRetryingTransactionHelper()).thenReturn(mockedRetryingTransactionHelper); + + // max node id + when(mockedPatchDAO.getMaxAdmNodeID()).thenReturn(500000L); + + // aspect + when(mockedQnameDAO.getQName(ASPECT_EXTENDED_SECURITY)).thenReturn(new Pair(ASPECT_ID, ASPECT)); + } + + /** + * Given that there are no nodes with the extended security aspect When the action is executed Nothing happens + * @throws Exception + */ + @SuppressWarnings({ "unchecked" }) + @Test + public void noNodesWithExtendedSecurity() throws Exception + { + when(mockedPatchDAO.getNodesByAspectQNameId(eq(ASPECT_ID), anyLong(), anyLong())) + .thenReturn(Collections.emptyList()); + + // Set up parameters. + Map parameters = ImmutableMap.of("batchsize", "10", "maxProcessedRecords", "3"); + JSONObject json = executeJSONWebScript(parameters); + assertNotNull(json); + String actualJSONString = json.toString(); + + // Check the JSON result using Jackson to allow easy equality testing. + ObjectMapper mapper = new ObjectMapper(); + String expectedJSONString = "{\"responsestatus\":\"success\",\"message\":\"Processed 0 records.\"}"; + assertEquals(mapper.readTree(expectedJSONString), mapper.readTree(actualJSONString)); + + + verify(mockedNodeService, never()).getProperty(any(NodeRef.class), eq(PROP_READERS)); + verify(mockedNodeService, never()).getProperty(any(NodeRef.class), eq(PROP_WRITERS)); + verify(mockedNodeService, never()).removeAspect(any(NodeRef.class), eq(ASPECT_EXTENDED_SECURITY)); + verify(mockedPermissionService, never()).clearPermission(any(NodeRef.class), + eq(ExtendedReaderDynamicAuthority.EXTENDED_READER)); + verify(mockedPermissionService, never()).clearPermission(any(NodeRef.class), + eq(ExtendedWriterDynamicAuthority.EXTENDED_WRITER)); + verify(mockedExtendedSecurityService, never()).set(any(NodeRef.class), any(Set.class), any(Set.class)); + } + + /** + * Given that there are records with the extended security aspect When the action is executed Then the aspect is + * removed And the dynamic authorities permissions are cleared And extended security is set via the updated API + * @throws Exception + */ + @SuppressWarnings("unchecked") + @Test + public void recordsWithExtendedSecurityAspect() throws Exception + { + List ids = Stream.of(1l, 2l, 3l).collect(Collectors.toList()); + + when(mockedPatchDAO.getNodesByAspectQNameId(eq(ASPECT_ID), anyLong(), anyLong())) + .thenReturn(ids) + .thenReturn(Collections.emptyList()); + + ids.stream().forEach((i) -> { + NodeRef nodeRef = AlfMock.generateNodeRef(mockedNodeService); + when(mockedNodeDAO.getNodePair(i)).thenReturn(new Pair(i, nodeRef)); + when(mockedNodeService.hasAspect(nodeRef, ASPECT_RECORD)).thenReturn(true); + when(mockedNodeService.getProperty(nodeRef, PROP_READERS)) + .thenReturn((Serializable) Collections.emptyMap()); + when(mockedNodeService.getProperty(nodeRef, PROP_WRITERS)) + .thenReturn((Serializable) Collections.emptyMap()); + + }); + + // Set up parameters. + Map parameters = ImmutableMap.of("batchsize", "10", "maxProcessedRecords", "4"); + JSONObject json = executeJSONWebScript(parameters); + assertNotNull(json); + String actualJSONString = json.toString(); + ObjectMapper mapper = new ObjectMapper(); + String expectedJSONString = "{\"responsestatus\":\"success\",\"message\":\"Processed 3 records.\"}"; + assertEquals(mapper.readTree(expectedJSONString), mapper.readTree(actualJSONString)); + + + verify(mockedNodeService, times(3)).getProperty(any(NodeRef.class), eq(PROP_READERS)); + verify(mockedNodeService, times(3)).getProperty(any(NodeRef.class), eq(PROP_WRITERS)); + verify(mockedNodeService, times(3)).removeAspect(any(NodeRef.class), eq(ASPECT_EXTENDED_SECURITY)); + verify(mockedPermissionService, times(3)).clearPermission(any(NodeRef.class), + eq(ExtendedReaderDynamicAuthority.EXTENDED_READER)); + verify(mockedPermissionService, times(3)).clearPermission(any(NodeRef.class), + eq(ExtendedWriterDynamicAuthority.EXTENDED_WRITER)); + verify(mockedExtendedSecurityService, times(3)).set(any(NodeRef.class), any(Set.class), any(Set.class)); + + } + + /** + * Given that there are non-records with the extended security aspect When the web script is executed Then the aspect is + * removed And the dynamic authorities permissions are cleared + * @throws Exception + */ + @SuppressWarnings("unchecked") + @Test + public void nonRecordsWithExtendedSecurityAspect() throws Exception + { + List ids = Stream.of(1l, 2l, 3l).collect(Collectors.toList()); + + when(mockedPatchDAO.getNodesByAspectQNameId(eq(ASPECT_ID), anyLong(), anyLong())) + .thenReturn(ids) + .thenReturn(Collections.emptyList()); + + ids.stream().forEach((i) -> { + NodeRef nodeRef = AlfMock.generateNodeRef(mockedNodeService); + when(mockedNodeDAO.getNodePair(i)).thenReturn(new Pair(i, nodeRef)); + when(mockedNodeService.hasAspect(nodeRef, ASPECT_RECORD)).thenReturn(false); + when(mockedNodeService.getProperty(nodeRef, PROP_READERS)) + .thenReturn((Serializable) Collections.emptyMap()); + when(mockedNodeService.getProperty(nodeRef, PROP_WRITERS)) + .thenReturn((Serializable) Collections.emptyMap()); + + }); + + // Set up parameters. + Map parameters = ImmutableMap.of("batchsize", "10", "maxProcessedRecords", "4"); + JSONObject json = executeJSONWebScript(parameters); + assertNotNull(json); + String actualJSONString = json.toString(); + ObjectMapper mapper = new ObjectMapper(); + String expectedJSONString = "{\"responsestatus\":\"success\",\"message\":\"Processed 3 records.\"}"; + assertEquals(mapper.readTree(expectedJSONString), mapper.readTree(actualJSONString)); + + + verify(mockedNodeService, times(3)).getProperty(any(NodeRef.class), eq(PROP_READERS)); + verify(mockedNodeService, times(3)).getProperty(any(NodeRef.class), eq(PROP_WRITERS)); + verify(mockedNodeService, times(3)).removeAspect(any(NodeRef.class), eq(ASPECT_EXTENDED_SECURITY)); + verify(mockedPermissionService, times(3)).clearPermission(any(NodeRef.class), + eq(ExtendedReaderDynamicAuthority.EXTENDED_READER)); + verify(mockedPermissionService, times(3)).clearPermission(any(NodeRef.class), + eq(ExtendedWriterDynamicAuthority.EXTENDED_WRITER)); + verify(mockedExtendedSecurityService, never()).set(any(NodeRef.class), any(Set.class), any(Set.class)); + } + + @Test + public void missingBatchSizeParameter() throws Exception + { try { executeJSONWebScript(emptyMap()); @@ -300,15 +302,15 @@ public class DynamicAuthoritiesGetUnitTest extends BaseWebScriptUnitTest impleme assertEquals("If parameter batchsize is not provided then 'Bad request' should be returned.", Status.STATUS_BAD_REQUEST, e.getStatus()); } - } - - @Test - public void invalidBatchSizeParameter() throws Exception - { + } + + @Test + public void invalidBatchSizeParameter() throws Exception + { try { - // Set up parameters. - Map parameters = ImmutableMap.of("batchsize", "dd"); + // Set up parameters. + Map parameters = ImmutableMap.of("batchsize", "dd"); executeJSONWebScript(parameters); fail("Expected exception as parameter batchsize is invalid."); } @@ -317,15 +319,15 @@ public class DynamicAuthoritiesGetUnitTest extends BaseWebScriptUnitTest impleme assertEquals("If parameter batchsize is invalid then 'Bad request' should be returned.", Status.STATUS_BAD_REQUEST, e.getStatus()); } - } - - @Test - public void batchSizeShouldBeGraterThanZero() throws Exception - { + } + + @Test + public void batchSizeShouldBeGraterThanZero() throws Exception + { try { - // Set up parameters. - Map parameters = ImmutableMap.of("batchsize", "0"); + // Set up parameters. + Map parameters = ImmutableMap.of("batchsize", "0"); executeJSONWebScript(parameters); fail("Expected exception as parameter batchsize is not a number greater than 0."); } @@ -334,79 +336,81 @@ public class DynamicAuthoritiesGetUnitTest extends BaseWebScriptUnitTest impleme assertEquals("If parameter batchsize is not a number greater than 0 then 'Bad request' should be returned.", Status.STATUS_BAD_REQUEST, e.getStatus()); } - } - - @Test - public void extendedSecurityAspectNotCreated() throws Exception - { - when(mockedQnameDAO.getQName(ASPECT_EXTENDED_SECURITY)).thenReturn(null); - // Set up parameters. - Map parameters = ImmutableMap.of("batchsize", "3"); - JSONObject json = executeJSONWebScript(parameters); - assertNotNull(json); - String actualJSONString = json.toString(); - ObjectMapper mapper = new ObjectMapper(); - String expectedJSONString = "{\"responsestatus\":\"success\",\"message\":\"There where no records to be processed.\"}"; - assertEquals(mapper.readTree(expectedJSONString), mapper.readTree(actualJSONString)); - } - - @Test - public void processAllRecordsWhenMaxProcessedRecordsIsZero() throws Exception - { - List ids = Stream.of(1l, 2l, 3l, 4l).collect(Collectors.toList()); - - when(mockedPatchDAO.getNodesByAspectQNameId(eq(ASPECT_ID), anyLong(), anyLong())).thenReturn(ids) - .thenReturn(Collections.emptyList()); - - ids.stream().forEach((i) -> { - NodeRef nodeRef = AlfMock.generateNodeRef(mockedNodeService); - when(mockedNodeDAO.getNodePair(i)).thenReturn(new Pair(i, nodeRef)); - when(mockedNodeService.hasAspect(nodeRef, ASPECT_RECORD)).thenReturn(false); - when(mockedNodeService.getProperty(nodeRef, PROP_READERS)) - .thenReturn((Serializable) Collections.emptyMap()); - when(mockedNodeService.getProperty(nodeRef, PROP_WRITERS)) - .thenReturn((Serializable) Collections.emptyMap()); - - }); - - // Set up parameters. - Map parameters = ImmutableMap.of("batchsize", "10", "maxProcessedRecords", "0"); - JSONObject json = executeJSONWebScript(parameters); - assertNotNull(json); - String actualJSONString = json.toString(); - ObjectMapper mapper = new ObjectMapper(); - String expectedJSONString = "{\"responsestatus\":\"success\",\"message\":\"Processed 4 records.\"}"; - assertEquals(mapper.readTree(expectedJSONString), mapper.readTree(actualJSONString)); - } - - @Test - public void whenMaxProcessedRecordsIsMissingItDefaultsToBatchSize() throws Exception - { - List ids = Stream.of(1l, 2l, 3l, 4l, 5l).collect(Collectors.toList()); - - when(mockedPatchDAO.getNodesByAspectQNameId(eq(ASPECT_ID), anyLong(), anyLong())).thenReturn(ids) - .thenReturn(Collections.emptyList()); - - ids.stream().forEach((i) -> { - NodeRef nodeRef = AlfMock.generateNodeRef(mockedNodeService); - when(mockedNodeDAO.getNodePair(i)).thenReturn(new Pair(i, nodeRef)); - when(mockedNodeService.hasAspect(nodeRef, ASPECT_RECORD)).thenReturn(false); - when(mockedNodeService.getProperty(nodeRef, PROP_READERS)) - .thenReturn((Serializable) Collections.emptyMap()); - when(mockedNodeService.getProperty(nodeRef, PROP_WRITERS)) - .thenReturn((Serializable) Collections.emptyMap()); - - }); - - // Set up parameters. - Map parameters = ImmutableMap.of("batchsize", "4"); - JSONObject json = executeJSONWebScript(parameters); - assertNotNull(json); - String actualJSONString = json.toString(); - ObjectMapper mapper = new ObjectMapper(); - String expectedJSONString = "{\"responsestatus\":\"success\",\"message\":\"Processed first 4 records.\"}"; - assertEquals(mapper.readTree(expectedJSONString), mapper.readTree(actualJSONString)); - } + } + + @Test + public void extendedSecurityAspectNotCreated() throws Exception + { + when(mockedQnameDAO.getQName(ASPECT_EXTENDED_SECURITY)).thenReturn(null); + // Set up parameters. + Map parameters = ImmutableMap.of("batchsize", "3"); + JSONObject json = executeJSONWebScript(parameters); + assertNotNull(json); + String actualJSONString = json.toString(); + ObjectMapper mapper = new ObjectMapper(); + String expectedJSONString = "{\"responsestatus\":\"success\",\"message\":\"There where no records to be processed.\"}"; + assertEquals(mapper.readTree(expectedJSONString), mapper.readTree(actualJSONString)); + } + + @Test + public void processAllRecordsWhenMaxProcessedRecordsIsZero() throws Exception + { + List ids = Stream.of(1l, 2l, 3l,4l).collect(Collectors.toList()); + + when(mockedPatchDAO.getNodesByAspectQNameId(eq(ASPECT_ID), anyLong(), anyLong())) + .thenReturn(ids) + .thenReturn(Collections.emptyList()); + + ids.stream().forEach((i) -> { + NodeRef nodeRef = AlfMock.generateNodeRef(mockedNodeService); + when(mockedNodeDAO.getNodePair(i)).thenReturn(new Pair(i, nodeRef)); + when(mockedNodeService.hasAspect(nodeRef, ASPECT_RECORD)).thenReturn(false); + when(mockedNodeService.getProperty(nodeRef, PROP_READERS)) + .thenReturn((Serializable) Collections.emptyMap()); + when(mockedNodeService.getProperty(nodeRef, PROP_WRITERS)) + .thenReturn((Serializable) Collections.emptyMap()); + + }); + + // Set up parameters. + Map parameters = ImmutableMap.of("batchsize", "10", "maxProcessedRecords", "0"); + JSONObject json = executeJSONWebScript(parameters); + assertNotNull(json); + String actualJSONString = json.toString(); + ObjectMapper mapper = new ObjectMapper(); + String expectedJSONString = "{\"responsestatus\":\"success\",\"message\":\"Processed 4 records.\"}"; + assertEquals(mapper.readTree(expectedJSONString), mapper.readTree(actualJSONString)); + } + + @Test + public void whenMaxProcessedRecordsIsMissingItDefaultsToBatchSize() throws Exception + { + List ids = Stream.of(1l, 2l, 3l, 4l, 5l).collect(Collectors.toList()); + + when(mockedPatchDAO.getNodesByAspectQNameId(eq(ASPECT_ID), anyLong(), anyLong())) + .thenReturn(ids) + .thenReturn(Collections.emptyList()); + + ids.stream().forEach((i) -> { + NodeRef nodeRef = AlfMock.generateNodeRef(mockedNodeService); + when(mockedNodeDAO.getNodePair(i)).thenReturn(new Pair(i, nodeRef)); + when(mockedNodeService.hasAspect(nodeRef, ASPECT_RECORD)).thenReturn(false); + when(mockedNodeService.getProperty(nodeRef, PROP_READERS)) + .thenReturn((Serializable) Collections.emptyMap()); + when(mockedNodeService.getProperty(nodeRef, PROP_WRITERS)) + .thenReturn((Serializable) Collections.emptyMap()); + + }); + + // Set up parameters. + Map parameters = ImmutableMap.of("batchsize", "4"); + JSONObject json = executeJSONWebScript(parameters); + assertNotNull(json); + String actualJSONString = json.toString(); + ObjectMapper mapper = new ObjectMapper(); + String expectedJSONString = "{\"responsestatus\":\"success\",\"message\":\"Processed first 4 records.\"}"; + assertEquals(mapper.readTree(expectedJSONString), mapper.readTree(actualJSONString)); + } @SuppressWarnings("unchecked") @Test @@ -414,7 +418,8 @@ public class DynamicAuthoritiesGetUnitTest extends BaseWebScriptUnitTest impleme { List ids = Stream.of(1l, 2l, 3l).collect(Collectors.toList()); - when(mockedPatchDAO.getNodesByAspectQNameId(eq(ASPECT_ID), anyLong(), anyLong())).thenReturn(ids) + when(mockedPatchDAO.getNodesByAspectQNameId(eq(ASPECT_ID), anyLong(), anyLong())) + .thenReturn(ids) .thenReturn(Collections.emptyList()); ids.stream().forEach((i) -> {