From 0f00c63ac14d5ecd67da011972fab2cbc2e46496 Mon Sep 17 00:00:00 2001 From: Ana Bozianu Date: Wed, 4 Jan 2017 17:57:10 +0200 Subject: [PATCH] RM-4436 and RM-4549 - unit tests for record api --- .../rm-public-rest-context.xml | 1 + .../rm/rest/api/impl/RecordsImpl.java | 30 +- .../api/records/RecordsEntityResource.java | 7 +- .../rm/rest/api/impl/RecordsImplUnitTest.java | 337 ++++++++++++++++++ .../main/webapp/definitions/ig-core-api.yaml | 2 +- 5 files changed, 357 insertions(+), 20 deletions(-) create mode 100644 rm-community/rm-community-repo/unit-test/java/org/alfresco/rm/rest/api/impl/RecordsImplUnitTest.java diff --git a/rm-community/rm-community-repo/config/alfresco/module/org_alfresco_module_rm/rm-public-rest-context.xml b/rm-community/rm-community-repo/config/alfresco/module/org_alfresco_module_rm/rm-public-rest-context.xml index 71db247827..dab99eb8eb 100644 --- a/rm-community/rm-community-repo/config/alfresco/module/org_alfresco_module_rm/rm-public-rest-context.xml +++ b/rm-community/rm-community-repo/config/alfresco/module/org_alfresco_module_rm/rm-public-rest-context.xml @@ -97,6 +97,7 @@ + diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/rm/rest/api/impl/RecordsImpl.java b/rm-community/rm-community-repo/source/java/org/alfresco/rm/rest/api/impl/RecordsImpl.java index df2f5049e9..2938feee94 100644 --- a/rm-community/rm-community-repo/source/java/org/alfresco/rm/rest/api/impl/RecordsImpl.java +++ b/rm-community/rm-community-repo/source/java/org/alfresco/rm/rest/api/impl/RecordsImpl.java @@ -33,8 +33,8 @@ import org.alfresco.model.ContentModel; import org.alfresco.module.org_alfresco_module_rm.fileplan.FilePlanService; import org.alfresco.module.org_alfresco_module_rm.model.RecordsManagementModel; import org.alfresco.module.org_alfresco_module_rm.record.RecordService; +import org.alfresco.module.org_alfresco_module_rm.util.AuthenticationUtil; import org.alfresco.repo.node.integrity.IntegrityException; -import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork; import org.alfresco.rest.api.model.Node; import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException; @@ -48,10 +48,17 @@ import org.alfresco.service.cmr.model.FileFolderService; import org.alfresco.service.cmr.model.FileNotFoundException; import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeService; +import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.InitializingBean; import org.springframework.dao.ConcurrencyFailureException; import org.springframework.extensions.surf.util.ParameterCheck; +/** + * Centralizes access to record services + * + * @author Ana Bozianu + * @since 2.6 + */ public class RecordsImpl implements Records, InitializingBean { protected RecordService recordService; @@ -59,6 +66,7 @@ public class RecordsImpl implements Records, InitializingBean protected NodeService nodeService; protected FileFolderService fileFolderService; protected DictionaryService dictionaryService; + protected AuthenticationUtil authenticationUtil; protected RMNodes nodes; public void setRecordService(RecordService recordService) @@ -86,6 +94,11 @@ public class RecordsImpl implements Records, InitializingBean this.dictionaryService = dictionaryService; } + public void setAuthenticationUtil(AuthenticationUtil authenticationUtil) + { + this.authenticationUtil = authenticationUtil; + } + public void setNodes(RMNodes nodes) { this.nodes = nodes; @@ -94,17 +107,11 @@ public class RecordsImpl implements Records, InitializingBean @Override public Node declareFileAsRecord(String fileId, Parameters parameters) { - // Parameter check - if ((fileId == null) || (fileId.isEmpty())) - { - throw new InvalidArgumentException("Missing fileId"); - } - // Get file to be declared NodeRef fileNodeRef = nodes.validateNode(fileId) ; // Get fileplan - NodeRef filePlan = AuthenticationUtil.runAsSystem(new RunAsWork() + NodeRef filePlan = authenticationUtil.runAsSystem(new RunAsWork() { @Override public NodeRef doWork() @@ -126,10 +133,7 @@ public class RecordsImpl implements Records, InitializingBean @Override public Node fileOrLinkRecord(String recordId, TargetContainer target, Parameters parameters) { - ParameterCheck.mandatoryString("recordId", recordId); - - if((target.getTargetParentId() == null || target.getTargetParentId().isEmpty()) && - (target.getRelativePath() == null || target.getRelativePath().isEmpty())) + if(StringUtils.isBlank(target.getTargetParentId()) && StringUtils.isBlank(target.getRelativePath())) { throw new InvalidParameterException("No target folder information was provided"); } @@ -142,7 +146,7 @@ public class RecordsImpl implements Records, InitializingBean if(parentContainerId == null || parentContainerId.isEmpty()) { // If target container not provided get fileplan - parentContainerId = AuthenticationUtil.runAsSystem(new RunAsWork() + parentContainerId = authenticationUtil.runAsSystem(new RunAsWork() { @Override public String doWork() diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/rm/rest/api/records/RecordsEntityResource.java b/rm-community/rm-community-repo/source/java/org/alfresco/rm/rest/api/records/RecordsEntityResource.java index 39827d01ff..9075648c11 100644 --- a/rm-community/rm-community-repo/source/java/org/alfresco/rm/rest/api/records/RecordsEntityResource.java +++ b/rm-community/rm-community-repo/source/java/org/alfresco/rm/rest/api/records/RecordsEntityResource.java @@ -87,12 +87,7 @@ public class RecordsEntityResource implements BinaryResourceAction.Read, @WebApiDescription(title = "File record", description="File a record into fileplan.") public Node fileRecord(String recordId, TargetContainer target, Parameters parameters, WithResponse withResponse) { - try{ - return records.fileOrLinkRecord(recordId, target, parameters); - }catch(Exception ex) - { - throw ex; - } + return records.fileOrLinkRecord(recordId, target, parameters); } @Override diff --git a/rm-community/rm-community-repo/unit-test/java/org/alfresco/rm/rest/api/impl/RecordsImplUnitTest.java b/rm-community/rm-community-repo/unit-test/java/org/alfresco/rm/rest/api/impl/RecordsImplUnitTest.java new file mode 100644 index 0000000000..c10b272441 --- /dev/null +++ b/rm-community/rm-community-repo/unit-test/java/org/alfresco/rm/rest/api/impl/RecordsImplUnitTest.java @@ -0,0 +1,337 @@ +/* + * #%L + * Alfresco Records Management Module + * %% + * Copyright (C) 2005 - 2017 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.rm.rest.api.impl; + +import static org.alfresco.model.ContentModel.TYPE_CONTENT; +import static org.alfresco.module.org_alfresco_module_rm.model.RecordsManagementModel.TYPE_RECORD_FOLDER; +import static org.alfresco.module.org_alfresco_module_rm.model.RecordsManagementModel.TYPE_UNFILED_RECORD_CONTAINER; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.security.InvalidParameterException; + +import org.alfresco.module.org_alfresco_module_rm.fileplan.FilePlanService; +import org.alfresco.module.org_alfresco_module_rm.record.RecordService; +import org.alfresco.module.org_alfresco_module_rm.test.util.AlfMock; +import org.alfresco.module.org_alfresco_module_rm.test.util.MockAuthenticationUtilHelper; +import org.alfresco.module.org_alfresco_module_rm.util.AuthenticationUtil; +import org.alfresco.rest.framework.resource.parameters.Parameters; +import org.alfresco.rm.rest.api.RMNodes; +import org.alfresco.rm.rest.api.Records; +import org.alfresco.rm.rest.api.model.TargetContainer; +import org.alfresco.service.cmr.dictionary.DictionaryService; +import org.alfresco.service.cmr.model.FileFolderService; +import org.alfresco.service.cmr.repository.ChildAssociationRef; +import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.service.cmr.repository.NodeService; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; + +/** + * Unit tests for RecordsImpl + * + * @author Ana Bozianu + * @since 2.6 + */ +public class RecordsImplUnitTest +{ + @Mock + private RecordService mockedRecordService; + @Mock + protected FilePlanService mockedFilePlanService; + @Mock + protected NodeService mockedNodeService; + @Mock + protected FileFolderService mockedFileFolderService; + @Mock + protected DictionaryService mockedDictionaryService; + @Mock + protected AuthenticationUtil mockedAuthenticationUtil; + @Mock + protected RMNodes mockedNodes; + + @InjectMocks + private RecordsImpl recordsImpl; + + @Before + public void before() + { + MockitoAnnotations.initMocks(this); + + // setup mocked authentication util + MockAuthenticationUtilHelper.setup(mockedAuthenticationUtil); + } + + /** + * Given a file and an existing fileplan + * When declaring a file as record + * Then a record is created under the existing fileplan from the provided file + */ + @Test + public void testDeclareFileAsRecord() + { + /* + * Given a file and an existing fileplan + */ + NodeRef mockedFile = AlfMock.generateNodeRef(mockedNodeService); + when(mockedNodes.validateNode(mockedFile.getId())).thenReturn(mockedFile); + + NodeRef fileplan = AlfMock.generateNodeRef(mockedNodeService); + when(mockedFilePlanService.getFilePlanBySiteId(FilePlanService.DEFAULT_RM_SITE_ID)).thenReturn(fileplan); + + /* + * When declare the file as record + */ + Parameters params = Mockito.mock(Parameters.class); + when(params.getParameter(Records.PARAM_HIDE_RECORD)).thenReturn("true"); + recordsImpl.declareFileAsRecord(mockedFile.getId(), params); + + /* + * Then a record is created under the existing fileplan from the provided file + */ + verify(mockedRecordService).createRecord(fileplan, mockedFile, false); + verify(mockedNodes).getFolderOrDocument(mockedFile.getId(), params); + } + + /** + * Given a record + * When trying to filing a record providing a blank destination path + * Then an InvalidParameterException is thrown + */ + @Test(expected=InvalidParameterException.class) + public void testFileRecord_BlankDestinationPath() + { + /* + * Given a record + */ + NodeRef mockedRecord = AlfMock.generateNodeRef(mockedNodeService); + when(mockedNodes.validateNode(mockedRecord.getId())).thenReturn(mockedRecord); + + /* + * When trying to filing a record providing a blank destination path + */ + Parameters params = Mockito.mock(Parameters.class); + TargetContainer target = new TargetContainer(); + recordsImpl.fileOrLinkRecord(mockedRecord.getId(), target, params); + } + + /** + * Given an unfiled record and an existing record folder + * When trying to file the record in the record folder + * Then the record is moved under the destination folder + */ + @Test + public void testFileRecord_DestinationById() throws Exception + { + /* + * Given an unfiled record and an existing record folder + */ + // mock the record to file + NodeRef mockedRecord = AlfMock.generateNodeRef(mockedNodeService); + when(mockedNodes.validateNode(mockedRecord.getId())).thenReturn(mockedRecord); + + // mock the current primary parent + NodeRef mockedPrimaryParent = AlfMock.generateNodeRef(mockedNodeService); + ChildAssociationRef mockedChildAssoc = mock(ChildAssociationRef.class); + when(mockedChildAssoc.getParentRef()).thenReturn(mockedPrimaryParent); + when(mockedNodeService.getPrimaryParent(mockedRecord)).thenReturn(mockedChildAssoc); + when(mockedNodeService.getType(mockedPrimaryParent)).thenReturn(TYPE_UNFILED_RECORD_CONTAINER); + when(mockedDictionaryService.isSubClass(TYPE_UNFILED_RECORD_CONTAINER, TYPE_RECORD_FOLDER)).thenReturn(false); + + // mock the target record folder to file the record into + NodeRef mockedTargetRecordFolder = AlfMock.generateNodeRef(mockedNodeService); + when(mockedNodes.getOrCreatePath(mockedTargetRecordFolder.getId(), null, TYPE_CONTENT)).thenReturn(mockedTargetRecordFolder); + when(mockedNodeService.getType(mockedTargetRecordFolder)).thenReturn(TYPE_RECORD_FOLDER); + when(mockedDictionaryService.isSubClass(TYPE_RECORD_FOLDER, TYPE_RECORD_FOLDER)).thenReturn(true); + + /* + * When trying to file the record in the record folder + */ + TargetContainer destination = new TargetContainer(); + destination.setTargetParentId(mockedTargetRecordFolder.getId()); + + Parameters params = Mockito.mock(Parameters.class); + recordsImpl.fileOrLinkRecord(mockedRecord.getId(), destination, params); + + /* + * Then the record is moved under the destination folder + */ + verify(mockedNodes).getOrCreatePath(mockedTargetRecordFolder.getId(), null, TYPE_CONTENT); + verify(mockedFileFolderService).moveFrom(mockedRecord, mockedPrimaryParent, mockedTargetRecordFolder, null); + } + + /** + * Given an unfiled record + * and an existing record folder with relative path category/recordFolder from fileplan + * When trying to file the record using a relative path and no target id + * Then the record is moved under the destination folder relative to the fileplan + */ + @Test + public void testFileRecord_DestinationRelativeToFileplan() throws Exception + { + /* + * Given an unfiled record and an existing record folder + */ + // mock the record to file + NodeRef mockedRecord = AlfMock.generateNodeRef(mockedNodeService); + when(mockedNodes.validateNode(mockedRecord.getId())).thenReturn(mockedRecord); + + // mock the current primary parent + NodeRef mockedPrimaryParent = AlfMock.generateNodeRef(mockedNodeService); + ChildAssociationRef mockedChildAssoc = mock(ChildAssociationRef.class); + when(mockedChildAssoc.getParentRef()).thenReturn(mockedPrimaryParent); + when(mockedNodeService.getPrimaryParent(mockedRecord)).thenReturn(mockedChildAssoc); + when(mockedNodeService.getType(mockedPrimaryParent)).thenReturn(TYPE_UNFILED_RECORD_CONTAINER); + when(mockedDictionaryService.isSubClass(TYPE_UNFILED_RECORD_CONTAINER, TYPE_RECORD_FOLDER)).thenReturn(false); + + // mock the fileplan + NodeRef fileplan = AlfMock.generateNodeRef(mockedNodeService); + when(mockedFilePlanService.getFilePlanBySiteId(FilePlanService.DEFAULT_RM_SITE_ID)).thenReturn(fileplan); + + // mock the target record folder to file the record into + String relativePath = "category/recordFolder"; + NodeRef mockedTargetRecordFolder = AlfMock.generateNodeRef(mockedNodeService); + when(mockedNodes.getOrCreatePath(fileplan.getId(), relativePath, TYPE_CONTENT)).thenReturn(mockedTargetRecordFolder); + when(mockedNodeService.getType(mockedTargetRecordFolder)).thenReturn(TYPE_RECORD_FOLDER); + when(mockedDictionaryService.isSubClass(TYPE_RECORD_FOLDER, TYPE_RECORD_FOLDER)).thenReturn(true); + + /* + * When trying to file the record using a relative path and no target id + */ + TargetContainer destination = new TargetContainer(); + destination.setRelativePath(relativePath); + + Parameters params = Mockito.mock(Parameters.class); + recordsImpl.fileOrLinkRecord(mockedRecord.getId(), destination, params); + + /* + * Then the record is moved under the destination folder relative to the fileplan + */ + verify(mockedNodes).getOrCreatePath(fileplan.getId(), relativePath, TYPE_CONTENT); + verify(mockedFileFolderService).moveFrom(mockedRecord, mockedPrimaryParent, mockedTargetRecordFolder, null); + } + + /** + * Given an unfiled record + * and an existing record folder with relative path category/recordFolder from a given category + * When trying to file the record describing the target folder with the category id and the relative path + * Then the record is moved under the correct destination folder + */ + @Test + public void testFileRecord_DestinationRelativeToProvidedId() throws Exception + { + /* + * Given an unfiled record and an existing record folder with relative path category/recordFolder from a given category + */ + // mock the record to file + NodeRef mockedRecord = AlfMock.generateNodeRef(mockedNodeService); + when(mockedNodes.validateNode(mockedRecord.getId())).thenReturn(mockedRecord); + + // mock the current primary parent + NodeRef mockedPrimaryParent = AlfMock.generateNodeRef(mockedNodeService); + ChildAssociationRef mockedChildAssoc = mock(ChildAssociationRef.class); + when(mockedChildAssoc.getParentRef()).thenReturn(mockedPrimaryParent); + when(mockedNodeService.getPrimaryParent(mockedRecord)).thenReturn(mockedChildAssoc); + when(mockedNodeService.getType(mockedPrimaryParent)).thenReturn(TYPE_UNFILED_RECORD_CONTAINER); + when(mockedDictionaryService.isSubClass(TYPE_UNFILED_RECORD_CONTAINER, TYPE_RECORD_FOLDER)).thenReturn(false); + + // mock the target category + NodeRef mockedTargetCategory = AlfMock.generateNodeRef(mockedNodeService); + + // mock the target record folder to file the record into + String relativePath = "category/recordFolder"; + NodeRef mockedTargetRecordFolder = AlfMock.generateNodeRef(mockedNodeService); + when(mockedNodes.getOrCreatePath(mockedTargetCategory.getId(), relativePath, TYPE_CONTENT)).thenReturn(mockedTargetRecordFolder); + when(mockedNodeService.getType(mockedTargetRecordFolder)).thenReturn(TYPE_RECORD_FOLDER); + when(mockedDictionaryService.isSubClass(TYPE_RECORD_FOLDER, TYPE_RECORD_FOLDER)).thenReturn(true); + + /* + * When trying to file the record describing the target folder with the category id and the relative path + */ + TargetContainer destination = new TargetContainer(); + destination.setTargetParentId(mockedTargetCategory.getId()); + destination.setRelativePath(relativePath); + + Parameters params = Mockito.mock(Parameters.class); + recordsImpl.fileOrLinkRecord(mockedRecord.getId(), destination, params); + + /* + * Then the record is moved under the correct destination folder + */ + verify(mockedNodes).getOrCreatePath(mockedTargetCategory.getId(), relativePath, TYPE_CONTENT); + verify(mockedFileFolderService).moveFrom(mockedRecord, mockedPrimaryParent, mockedTargetRecordFolder, null); + } + + /** + * Given an filed record and an existing record folder + * When trying to link the record to the record folder + * Then the record is linked to the destination folder + */ + @Test + public void testLinkRecord() + { + /* + * Given an filed record and an existing record folder + */ + // mock the record to link + NodeRef mockedRecord = AlfMock.generateNodeRef(mockedNodeService); + when(mockedNodes.validateNode(mockedRecord.getId())).thenReturn(mockedRecord); + + // mock the current primary parent + NodeRef mockedPrimaryParent = AlfMock.generateNodeRef(mockedNodeService); + ChildAssociationRef mockedChildAssoc = mock(ChildAssociationRef.class); + when(mockedChildAssoc.getParentRef()).thenReturn(mockedPrimaryParent); + when(mockedNodeService.getPrimaryParent(mockedRecord)).thenReturn(mockedChildAssoc); + when(mockedNodeService.getType(mockedPrimaryParent)).thenReturn(TYPE_RECORD_FOLDER); + when(mockedDictionaryService.isSubClass(TYPE_RECORD_FOLDER, TYPE_RECORD_FOLDER)).thenReturn(true); + + // mock the target record folder to file the record into + NodeRef mockedTargetRecordFolder = AlfMock.generateNodeRef(mockedNodeService); + when(mockedNodes.getOrCreatePath(mockedTargetRecordFolder.getId(), null, TYPE_CONTENT)).thenReturn(mockedTargetRecordFolder); + when(mockedNodeService.getType(mockedTargetRecordFolder)).thenReturn(TYPE_RECORD_FOLDER); + + /* + * When trying to link the record to the record folder + */ + TargetContainer destination = new TargetContainer(); + destination.setTargetParentId(mockedTargetRecordFolder.getId()); + + Parameters params = Mockito.mock(Parameters.class); + recordsImpl.fileOrLinkRecord(mockedRecord.getId(), destination, params); + + /* + * Then the record is linked to the destination folder + */ + verify(mockedNodes).getOrCreatePath(mockedTargetRecordFolder.getId(), null, TYPE_CONTENT); + verify(mockedRecordService).link(mockedRecord, mockedTargetRecordFolder); + } +} diff --git a/rm-community/rm-community-rest-api-explorer/src/main/webapp/definitions/ig-core-api.yaml b/rm-community/rm-community-rest-api-explorer/src/main/webapp/definitions/ig-core-api.yaml index e1275d29fb..b1897925c2 100644 --- a/rm-community/rm-community-rest-api-explorer/src/main/webapp/definitions/ig-core-api.yaml +++ b/rm-community/rm-community-rest-api-explorer/src/main/webapp/definitions/ig-core-api.yaml @@ -667,7 +667,7 @@ paths: type: string - name: hideRecord in: query - description: Flag to indicate whether the record should be hiden from the curent parent folder. + description: Flag to indicate whether the record should be hidden from the current parent folder. type: boolean default: false - $ref: '#/parameters/IGNodeEntryIncludeParam'