From bd460ba1e14e7adab800ef9edb6d1d403f3d9d8b Mon Sep 17 00:00:00 2001 From: Roy Wetherall Date: Fri, 12 Aug 2016 08:39:13 +1000 Subject: [PATCH] RM-3074: Integration Tests, including BDT test helpers to simplify code --- .../record/CreateInplaceRecordTest.java | 188 ---------- .../record/InplaceRecordPermissionTest.java | 347 ++++++++++++++++++ .../test/util/bdt/BehaviourTest.java | 258 +++++++++++++ .../test/util/bdt/ExpectedFailure.java | 92 +++++ .../test/util/bdt/ExpectedValue.java | 85 +++++ 5 files changed, 782 insertions(+), 188 deletions(-) delete mode 100644 rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/integration/record/CreateInplaceRecordTest.java create mode 100644 rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/integration/record/InplaceRecordPermissionTest.java create mode 100644 rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/util/bdt/BehaviourTest.java create mode 100644 rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/util/bdt/ExpectedFailure.java create mode 100644 rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/util/bdt/ExpectedValue.java diff --git a/rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/integration/record/CreateInplaceRecordTest.java b/rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/integration/record/CreateInplaceRecordTest.java deleted file mode 100644 index 0394e67af0..0000000000 --- a/rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/integration/record/CreateInplaceRecordTest.java +++ /dev/null @@ -1,188 +0,0 @@ -/* - * #%L - * Alfresco Records Management Module - * %% - * Copyright (C) 2005 - 2016 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.module.org_alfresco_module_rm.test.integration.record; - -import org.alfresco.module.org_alfresco_module_rm.capability.RMPermissionModel; -import org.alfresco.module.org_alfresco_module_rm.test.util.BaseRMTestCase; -import org.alfresco.repo.security.authentication.AuthenticationUtil; -import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork; -import org.alfresco.service.cmr.model.FileExistsException; -import org.alfresco.service.cmr.model.FileNotFoundException; -import org.alfresco.service.cmr.security.AccessStatus; - -/** - * Create Inplace Record Test - * - * @author Roy Wetherall - */ -public class CreateInplaceRecordTest extends BaseRMTestCase -{ - @Override - protected boolean isCollaborationSiteTest() - { - return true; - } - - /** - * Given a document in a collaboration site - * When the document is declared by a site collaborator - * Then the document becomes a record - * And the site users have the appropriate in-place permissions on the record - */ - public void testCreateInplaceRecordFromCollabSite() - { - doBehaviourDrivenTest(new BehaviourDrivenTest() - { - public void given() - { - // Check that the document is not a record - assertFalse("The document should not be a record", recordService.isRecord(dmDocument)); - } - - public void when() - { - // Declare the document as a record - AuthenticationUtil.runAs(new RunAsWork() - { - public Void doWork() throws Exception - { - // Declare record - recordService.createRecord(filePlan, dmDocument); - - return null; - } - }, dmCollaborator); - } - - public void then() - { - // Check that the document is a record now - assertTrue("The document should now be a record", recordService.isRecord(dmDocument)); - - // Check that the record is in the unfiled container - - // Check that the record is still a child of the collaboration folder - - // Check that the collaborator has filling permissions on the record - AuthenticationUtil.runAs(new RunAsWork() - { - public Void doWork() throws Exception - { - assertEquals(AccessStatus.ALLOWED, permissionService.hasPermission(dmDocument, RMPermissionModel.FILING)); - assertEquals(AccessStatus.ALLOWED, permissionService.hasPermission(dmDocument, RMPermissionModel.READ_RECORDS)); - return null; - } - }, dmCollaborator); - - - // Check that the consumer has read permissions on the record - AuthenticationUtil.runAs(new RunAsWork() - { - public Void doWork() throws Exception - { - assertEquals(AccessStatus.DENIED, permissionService.hasPermission(dmDocument, RMPermissionModel.FILING)); - assertEquals(AccessStatus.ALLOWED, permissionService.hasPermission(dmDocument, RMPermissionModel.READ_RECORDS)); - return null; - } - }, dmConsumer); - - } - }); - } - - public void testFileInplaceRecordFromCollabSite() - { - doBehaviourDrivenTest(new BehaviourDrivenTest() - { - public void given() - { - // Check that the document is not a record - assertFalse("The document should not be a record", recordService.isRecord(dmDocument)); - - // Declare the document as a record - AuthenticationUtil.runAs(new RunAsWork() - { - public Void doWork() throws Exception - { - // Declare record - recordService.createRecord(filePlan, dmDocument); - - return null; - } - }, dmCollaborator); - - // Check that the document is a record - assertTrue("The document should be a record", recordService.isRecord(dmDocument)); - assertFalse("The record should not be filed", recordService.isFiled(dmDocument)); - } - - public void when() throws FileExistsException, FileNotFoundException - { - // file the document to a location in the file plan - fileFolderService.move(dmDocument, rmFolder, null); - } - - public void then() - { - // Check that the document is a record now - assertTrue("The document should be a record", recordService.isRecord(dmDocument)); - assertTrue("The record hsould be filed", recordService.isFiled(dmDocument)); - - // Check that the record is in the unfiled container - // TODO - - // Check that the record is still a child of the collaboration folder - // TODO - - // Check that the collaborator has filling permissions on the record - AuthenticationUtil.runAs(new RunAsWork() - { - public Void doWork() throws Exception - { - assertEquals(AccessStatus.ALLOWED, permissionService.hasPermission(dmDocument, RMPermissionModel.FILING)); - assertEquals(AccessStatus.ALLOWED, permissionService.hasPermission(dmDocument, RMPermissionModel.READ_RECORDS)); - return null; - } - }, dmCollaborator); - - - // Check that the consumer has read permissions on the record - AuthenticationUtil.runAs(new RunAsWork() - { - public Void doWork() throws Exception - { - assertEquals(AccessStatus.DENIED, permissionService.hasPermission(dmDocument, RMPermissionModel.FILING)); - assertEquals(AccessStatus.ALLOWED, permissionService.hasPermission(dmDocument, RMPermissionModel.READ_RECORDS)); - return null; - } - }, dmConsumer); - - } - }); - } -} diff --git a/rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/integration/record/InplaceRecordPermissionTest.java b/rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/integration/record/InplaceRecordPermissionTest.java new file mode 100644 index 0000000000..7b5478b3ca --- /dev/null +++ b/rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/integration/record/InplaceRecordPermissionTest.java @@ -0,0 +1,347 @@ +/* + * #%L + * Alfresco Records Management Module + * %% + * Copyright (C) 2005 - 2016 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.module.org_alfresco_module_rm.test.integration.record; + +import static org.alfresco.module.org_alfresco_module_rm.test.util.bdt.BehaviourTest.test; + +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.alfresco.module.org_alfresco_module_rm.action.impl.DeclareRecordAction; +import org.alfresco.module.org_alfresco_module_rm.capability.Capability; +import org.alfresco.module.org_alfresco_module_rm.capability.RMPermissionModel; +import org.alfresco.module.org_alfresco_module_rm.test.util.BaseRMTestCase; +import org.alfresco.module.org_alfresco_module_rm.test.util.bdt.BehaviourTest; +import org.alfresco.repo.security.permissions.AccessDeniedException; +import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.service.cmr.security.AccessStatus; +import org.alfresco.service.cmr.security.PermissionService; + +/** + * In-place record permission integration test. + * + * @author Roy Wetherall + * @since 2.5 + */ +public class InplaceRecordPermissionTest extends BaseRMTestCase +{ + /** capability list */ + private static final List CAPABILITIES = Stream + .of(RMPermissionModel.VIEW_RECORDS, + RMPermissionModel.EDIT_NON_RECORD_METADATA, + RMPermissionModel.EDIT_RECORD_METADATA) + .collect(Collectors.toList()); + + /** capabilities */ + private Capability viewRecordsCapability; + private Capability editNonRecordMetadataCapability; + private Capability editRecordMetadataCapability; + + /** test characteristics */ + @Override protected boolean isCollaborationSiteTest() { return true; } + @Override protected boolean isUserTest() { return true; } + + /** + * @see org.alfresco.module.org_alfresco_module_rm.test.util.BaseRMTestCase#initServices() + */ + @Override + protected void initServices() + { + super.initServices(); + + // initialise behaviour tests + BehaviourTest.initBehaviourTests(retryingTransactionHelper); + + // get capability references + viewRecordsCapability = capabilityService.getCapability(RMPermissionModel.VIEW_RECORDS); + editNonRecordMetadataCapability = capabilityService.getCapability(RMPermissionModel.EDIT_NON_RECORD_METADATA); + editRecordMetadataCapability = capabilityService.getCapability(RMPermissionModel.EDIT_RECORD_METADATA); + } + + /** + * Given a document in a collaboration site + * When a user without write permissions on the document tries to declare it as a record + * Then the declaration fails + * And the document does not become a record + */ + public void testUserWithOutWriteCantDeclareInPlaceRecord() + { + test() + .given() + + // Given a document in a collaboration site that is not a record + .expect(false) + .from(() -> recordService.isRecord(dmDocument)) + .because("The document is not a record.") + + // And a user with no write permission on the document + .as(userName) + .expect(AccessStatus.DENIED.toString()) + .from(() -> permissionService.hasPermission(dmDocument, PermissionService.WRITE).toString()) + .because("User does not have write access to document.") + + // When the user tries to declare the record + // Then we expect this to fail + .when() + .as(userName) + .expectException(AccessDeniedException.class) + .from(() -> recordService.createRecord(filePlan, dmDocument)) + .because("The user does not have write permission on the document."); + } + + + /** + * Given a document in a collaboration site is not a record + * When the document is declared by a site collaborator + * Then the document becomes a record + * And the site users have the appropriate in-place permissions on the record + */ + public void testCreateInplaceRecordFromCollabSite() + { + test() + + // Given that a document in a collaboration site is not a record + .given() + .asAdmin() + .expect(false) + .from(() -> recordService.isRecord(dmDocument)) + .because("The document is not a record") + + // When it is declared as an inplace record + .when() + .as(dmCollaborator) + .perform(() -> recordService.createRecord(filePlan, dmDocument)) + + .then() + .asAdmin() + // Then it becomes a record + .expect(true) + .from(() -> recordService.isRecord(dmDocument)) + .because("The document is a record") + + // And it isn't filed + .expect(false) + .from(() -> recordService.isFiled(dmDocument)) + .because("The record is not filed") + + // And a site collaborator has filling permissions and filling capability on the record + .as(dmCollaborator) + .perform(() -> + checkInPlaceAccess(dmDocument, + AccessStatus.ALLOWED, // read record permission + AccessStatus.ALLOWED, // filing permission + AccessStatus.ALLOWED, // view record capability + AccessStatus.ALLOWED, // edit non record metadata capability + AccessStatus.DENIED)) // edit record metadata capability + + // And a site consumer has read permissions and view record capability on the record + .as(dmConsumer) + .perform(() -> + checkInPlaceAccess(dmDocument, + AccessStatus.ALLOWED, // read record permission + AccessStatus.DENIED, // filing permission + AccessStatus.ALLOWED, // view record capability + AccessStatus.DENIED, // edit non record metadata capability + AccessStatus.DENIED)) // edit record metadata capability + + // And a user that is not a member of the site has no access to the inplace record + .as(userName) + .perform(() -> + checkInPlaceAccess(dmDocument, + AccessStatus.DENIED, // read record permission + AccessStatus.DENIED, // filing permission + AccessStatus.DENIED, // view record capability + AccessStatus.DENIED, // edit non record metadata capability + AccessStatus.DENIED)); // edit record metadata capability + } + + /** + * Helper method to check in place access for a user on a record. + */ + private void checkInPlaceAccess(NodeRef nodeRef, AccessStatus ... accessStatus) + { + // check permission access + assertEquals(accessStatus[0], permissionService.hasPermission(nodeRef, RMPermissionModel.READ_RECORDS)); + assertEquals(accessStatus[1], permissionService.hasPermission(nodeRef, RMPermissionModel.FILING)); + + // check capability access + Map access = capabilityService.getCapabilitiesAccessState(nodeRef, CAPABILITIES); + assertEquals(accessStatus[2], access.get(viewRecordsCapability)); + assertEquals(accessStatus[3], access.get(editNonRecordMetadataCapability)); + assertEquals(accessStatus[4], access.get(editRecordMetadataCapability)); + } + + /** + * Given an unfiled in-place record + * When the record is moved to the file plan (ie filed) + * Then the site users still have the appropriate in-place permissions on the record + */ + public void testFileInplaceRecordFromCollabSite() throws Exception + { + test() + + // Given an unfiled inplace record + .given() + .as(dmCollaborator) + .perform(() -> recordService.createRecord(filePlan, dmDocument)) + .expect(true) + .from(() -> recordService.isRecord(dmDocument)) + .because("The document is a record.") + .expect(false) + .from(() -> recordService.isFiled(dmDocument)) + .because("The record is not filed") + + // When the record is filed + .when() + .asAdmin() + .perform(() -> fileFolderService.move(dmDocument, rmFolder, null)) + + .then() + + // Then the record is filed + .asAdmin() + .expect(true) + .from(() -> recordService.isFiled(dmDocument)) + .because("The record is filed.") + + // And the collaborator has filling permissions and filling capability on the record + .as(dmCollaborator) + .perform(() -> + checkInPlaceAccess(dmDocument, + AccessStatus.ALLOWED, // read record permission + AccessStatus.ALLOWED, // filing permission + AccessStatus.ALLOWED, // view record capability + AccessStatus.ALLOWED, // edit non record metadata capability + AccessStatus.DENIED)) // edit record metadata capability + + // And the consumer has read permissions and view record capability on the record + .as(dmConsumer) + .perform(() -> + checkInPlaceAccess(dmDocument, + AccessStatus.ALLOWED, // read record permission + AccessStatus.DENIED, // filing permission + AccessStatus.ALLOWED, // view record capability + AccessStatus.DENIED, // edit non record metadata capability + AccessStatus.DENIED)) // edit record metadata capability + + // And a user that is not in the site has no permissions on the record + .as(userName) + .perform(() -> + checkInPlaceAccess(dmDocument, + AccessStatus.DENIED, // read record permission + AccessStatus.DENIED, // filing permission + AccessStatus.DENIED, // view record capability + AccessStatus.DENIED, // edit non record metadata capability + AccessStatus.DENIED)); // edit record metadata capability + } + + /** + * Given an incomplete inplace record + * When it is completed + * Then the inplace users still have access to the record + * And can't edit the records meta-data + */ + public void testCompletedInPlaceRecord() + { + test() + + // Given an incomplete record + .given() + .as(dmCollaborator) + .perform(() -> recordService.createRecord(filePlan, dmDocument)) + .expect(false) + .from(() -> recordService.isDeclared(dmDocument)) + .because("Record is not complete.") + + // When it is completed + .when() + .asAdmin() + .perform(() -> rmActionService.executeRecordsManagementAction(dmDocument, DeclareRecordAction.NAME)) + .expect(true) + .from(() -> recordService.isDeclared(dmDocument)) + .because("Record is complete.") + + .then() + + // Then the collaborator has filling permissions, view record capability, but not edit non-record metadata + .as(dmCollaborator) + .perform(() -> + checkInPlaceAccess(dmDocument, + AccessStatus.ALLOWED, // read record permission + AccessStatus.ALLOWED, // filing permission + AccessStatus.ALLOWED, // view record capability + AccessStatus.DENIED, // edit non record metadata capability + AccessStatus.DENIED)) // edit record metadata capability + + // And the consumer has read permissions and view record capability on the record + .as(dmConsumer) + .perform(() -> + checkInPlaceAccess(dmDocument, + AccessStatus.ALLOWED, // read record permission + AccessStatus.DENIED, // filing permission + AccessStatus.ALLOWED, // view record capability + AccessStatus.DENIED, // edit non record metadata capability + AccessStatus.DENIED)) // edit record metadata capability + + // And a user that is not in the site has no permissions on the record + .as(userName) + .perform(() -> + checkInPlaceAccess(dmDocument, + AccessStatus.DENIED, // read record permission + AccessStatus.DENIED, // filing permission + AccessStatus.DENIED, // view record capability + AccessStatus.DENIED, // edit non record metadata capability + AccessStatus.DENIED)); // edit record metadata capability + } + + /** + * Given a record + * When it is destroyed + * And it's metadata is maintained + * Then the inplace users still have access to the meta-data stub + */ + // TODO + + /** + * Given an inplace user with write access + * When a role is added to the inplace writers role + * Then then they receive that additional capability on the inplace record + */ + // TODO + + // hide? + // TODO + + // reject? + // TODO + + // user added to group ? + // TODO +} diff --git a/rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/util/bdt/BehaviourTest.java b/rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/util/bdt/BehaviourTest.java new file mode 100644 index 0000000000..09b7dcb249 --- /dev/null +++ b/rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/util/bdt/BehaviourTest.java @@ -0,0 +1,258 @@ +/* + * #%L + * Alfresco Records Management Module + * %% + * Copyright (C) 2005 - 2016 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.module.org_alfresco_module_rm.test.util.bdt; + +import org.alfresco.repo.security.authentication.AuthenticationUtil; +import org.alfresco.repo.transaction.RetryingTransactionHelper; + +/** + * Helper class that provides an simple way to write behaviour integration tests. + *

+ * Note that initBehaviourTest() must be called before given() is called. + * + * @author Roy Wetherall + * @since 2.5 + */ +public class BehaviourTest +{ + /** retrying transaction helper */ + private static RetryingTransactionHelper retryingTransactionHelper; + + /** current execution user */ + private String asUser = AuthenticationUtil.getAdminUserName(); + + /** + * Initialise behaviour tests for execution with retrying transaction helper + * + * @param retryingTransactionHelper retrying transaction helper + */ + public static void initBehaviourTests(RetryingTransactionHelper retryingTransactionHelper) + { + BehaviourTest.retryingTransactionHelper = retryingTransactionHelper; + } + + /** + * Start a test + * + * @return BehaviourTest new test instance + */ + public static BehaviourTest test() + { + return new BehaviourTest(); + } + + /** + * Helper method to get the retrying transaction helper + * + * @return RetryingTransactionHelper retrying transaction helper + */ + /*package*/ RetryingTransactionHelper getRetryingTransactionHelper() + { + return retryingTransactionHelper; + } + + /** + * Helper method to get the execution user + * + * @return String execution user + */ + /* package*/ String getAsUser() + { + return asUser; + } + + /** + * Helper method to switch the current execution user to admin. + * + * @return BehaviourTest test instance + */ + public BehaviourTest asAdmin() + { + return as(AuthenticationUtil.getAdminUserName()); + } + + /** + * Set execution user + * + * @param asUser execution user + * @return BehaviourTest test instance + */ + public BehaviourTest as(String asUser) + { + this.asUser = asUser; + return this; + } + + /** + * Given. + *

+ * Used to group together given conditions. + * + * @return BehaviourTest test instance + */ + public BehaviourTest given() + { + return this; + } + + /** + * Given. + *

+ * Performs work. + * + * @param given work to do + * @return BehaviourTest test instance + */ + public BehaviourTest given(Work given) + { + return perform(given); + } + + /** + * When. + *

+ * Used to group together when actions. + * + * @return BehaviourTest test instance + */ + public BehaviourTest when() + { + return this; + } + + /** + * When. + *

+ * Performs work. + * + * @param when work to do + * @return BehaviourTest test instance + */ + public BehaviourTest when(Work when) + { + return perform(when); + } + + /** + * Then. + *

+ * Used to group together then actions. + * + * @return BehaviourTest test instance + */ + public BehaviourTest then() + { + return this; + } + + /** + * Then. + *

+ * Performs work. + * + * @param then work to do + * @return BehaviourTest test instance + */ + public BehaviourTest then(Work then) + { + return perform(then); + } + + /** + * Expect a value. + * + * @param value value + * @return ExpectedValue expected value evaluator + */ + public ExpectedValue expect(boolean value) + { + return new ExpectedValue(this, value); + } + + /** + * Expect a value. + * + * @param value value + * @return ExpectedValue expected value evaluator + */ + public ExpectedValue expect(String value) + { + return new ExpectedValue(this, value); + } + + /** + * Expect a value. + * + * @param value value + * @return ExpectedValue expected value evaluator + */ + public ExpectedValue expect(Object value) + { + return new ExpectedValue(this, value); + } + + + /** + * Expect a failure. + * + * @param exceptionClass expected exception + * @return ExpectedFailure expected failure evaluator + */ + public ExpectedFailure expectException(Class exceptionClass) + { + return new ExpectedFailure(this, exceptionClass); + } + + /** + * Perform work + * + * @param work work to perform + * @return BehaviourTest test instance + */ + public BehaviourTest perform(Work work) + { + return retryingTransactionHelper.doInTransaction(() -> + { + return AuthenticationUtil.runAs(() -> + { + work.doIt(); + return this; + }, this.asUser); + }); + } + + /** + * Work Interface + */ + public interface Work + { + /** + * Do the work. + */ + void doIt() throws Exception; + } +} diff --git a/rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/util/bdt/ExpectedFailure.java b/rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/util/bdt/ExpectedFailure.java new file mode 100644 index 0000000000..33c5662d98 --- /dev/null +++ b/rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/util/bdt/ExpectedFailure.java @@ -0,0 +1,92 @@ +/* + * #%L + * Alfresco Records Management Module + * %% + * Copyright (C) 2005 - 2016 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.module.org_alfresco_module_rm.test.util.bdt; + +import static org.junit.Assert.fail; + +import java.text.MessageFormat; +import java.util.Arrays; +import java.util.Set; +import java.util.stream.Collectors; + +import org.alfresco.module.org_alfresco_module_rm.test.util.bdt.BehaviourTest.Work; + +/** + * Expected failure. + * + * @author Roy Wetherall + * @since 2.5 + */ +public class ExpectedFailure +{ + private static final String MESSAGE = "Expected failure \"{0}\" was not observed."; + + private BehaviourTest test; + private Set> exceptionClasses; + private Work work; + + @SafeVarargs + public ExpectedFailure(BehaviourTest test, Class ...exceptionClasses) + { + this.test = test; + this.exceptionClasses = Arrays.stream(exceptionClasses).collect(Collectors.toSet()); + } + + public ExpectedFailure from(Work work) + { + this.work = work; + return this; + } + + public BehaviourTest because(String message) + { + try + { + test.perform(work); + } + catch(Exception actualException) + { + boolean found = false; + + for (Class exceptionClass : exceptionClasses) + { + if (exceptionClass.isAssignableFrom(actualException.getClass())) + { + found = true; + } + } + + if (!found) + { + fail(MessageFormat.format(MESSAGE, message)); + } + } + + return test; + } + +} diff --git a/rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/util/bdt/ExpectedValue.java b/rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/util/bdt/ExpectedValue.java new file mode 100644 index 0000000000..78c10ac3fd --- /dev/null +++ b/rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/util/bdt/ExpectedValue.java @@ -0,0 +1,85 @@ +/* + * #%L + * Alfresco Records Management Module + * %% + * Copyright (C) 2005 - 2016 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.module.org_alfresco_module_rm.test.util.bdt; + +import static org.junit.Assert.assertEquals; + +import java.text.MessageFormat; + +import org.alfresco.repo.security.authentication.AuthenticationUtil; + +/** + * Expected value. + * + * @author Roy Wetherall + * @since 2.5 + */ +public class ExpectedValue +{ + private static final String MESSAGE = "Expected value outcome \"{0}\" was not observed."; + + private T expectedValue; + private Evaluation evaluation; + private BehaviourTest test; + + public ExpectedValue(BehaviourTest test, T value) + { + this.expectedValue = value; + this.test = test; + } + + public ExpectedValue from(Evaluation evaluation) + { + this.evaluation = evaluation; + return this; + } + + public BehaviourTest because(String message) + { + T actualValue = (T)test.getRetryingTransactionHelper().doInTransaction(() -> + { + return AuthenticationUtil.runAs(() -> + { + return evaluation.eval(); + }, test.getAsUser()); + }); + + if (message != null) + { + message = MessageFormat.format(MESSAGE, message); + } + + assertEquals(message, expectedValue, actualValue); + + return test; + } + + public interface Evaluation + { + T eval() throws Exception; + } +}