Refactor to no longer use JMockit mocks.

This commit is contained in:
Tim Donohue
2020-01-23 15:42:47 -06:00
parent 40f053751b
commit ebfc5dfb1f
3 changed files with 50 additions and 218 deletions

View File

@@ -28,11 +28,9 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import mockit.Mock;
import mockit.MockUp;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.dspace.app.util.AuthorizeUtil;
import org.dspace.authorize.AuthorizeException; import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.factory.AuthorizeServiceFactory;
import org.dspace.authorize.service.AuthorizeService; import org.dspace.authorize.service.AuthorizeService;
import org.dspace.core.Constants; import org.dspace.core.Constants;
import org.dspace.core.Context; import org.dspace.core.Context;
@@ -95,11 +93,14 @@ public class CollectionTest extends AbstractDSpaceObjectTest {
// Initialize our spy of the autowired (global) authorizeService bean. // Initialize our spy of the autowired (global) authorizeService bean.
// This allows us to customize the bean's method return values in tests below // This allows us to customize the bean's method return values in tests below
authorizeServiceSpy = spy(authorizeService); authorizeServiceSpy = spy(authorizeService);
// "Wire" our spy to be used by the current loaded CommunityService, CollectionService & ItemService // "Wire" our spy to be used by the current loaded object services
// (To ensure these services use the spy instead of the real service) // (To ensure these services use the spy instead of the real service)
ReflectionTestUtils.setField(communityService, "authorizeService", authorizeServiceSpy); ReflectionTestUtils.setField(communityService, "authorizeService", authorizeServiceSpy);
ReflectionTestUtils.setField(collectionService, "authorizeService", authorizeServiceSpy); ReflectionTestUtils.setField(collectionService, "authorizeService", authorizeServiceSpy);
ReflectionTestUtils.setField(itemService, "authorizeService", authorizeServiceSpy); ReflectionTestUtils.setField(itemService, "authorizeService", authorizeServiceSpy);
// Also wire into current AuthorizeServiceFactory, as that is used for some checks (e.g. AuthorizeUtil)
ReflectionTestUtils.setField(AuthorizeServiceFactory.getInstance(), "authorizeService",
authorizeServiceSpy);
} catch (AuthorizeException ex) { } catch (AuthorizeException ex) {
log.error("Authorization Error in init", ex); log.error("Authorization Error in init", ex);
fail("Authorization Error in init: " + ex.getMessage()); fail("Authorization Error in init: " + ex.getMessage());
@@ -378,13 +379,8 @@ public class CollectionTest extends AbstractDSpaceObjectTest {
*/ */
@Test @Test
public void testCreateWorkflowGroupAuth() throws Exception { public void testCreateWorkflowGroupAuth() throws Exception {
// Use JMockit to mock the AuthorizeUtil & allow permissions // Allow Collection ADMIN (to manage workflow group)
new MockUp<AuthorizeUtil>() { doNothing().when(authorizeServiceSpy).authorizeAction(context, collection, Constants.ADMIN);
@Mock
public void authorizeManageWorkflowsGroup(Context context, Collection collection) {
return; // allow permissions by not throwing an exception
}
};
int step = 1; int step = 1;
Group result = collectionService.createWorkflowGroup(context, collection, step); Group result = collectionService.createWorkflowGroup(context, collection, step);
@@ -396,15 +392,6 @@ public class CollectionTest extends AbstractDSpaceObjectTest {
*/ */
@Test(expected = AuthorizeException.class) @Test(expected = AuthorizeException.class)
public void testCreateWorkflowGroupNoAuth() throws Exception { public void testCreateWorkflowGroupNoAuth() throws Exception {
// Use JMockit to mock the AuthorizeUtil & disallow permissions
new MockUp<AuthorizeUtil>() {
@Mock
public void authorizeManageWorkflowsGroup(Context context, Collection collection)
throws AuthorizeException {
throw new AuthorizeException();
}
};
int step = 1; int step = 1;
Group result = collectionService.createWorkflowGroup(context, collection, step); Group result = collectionService.createWorkflowGroup(context, collection, step);
fail("Exception expected"); fail("Exception expected");
@@ -462,13 +449,8 @@ public class CollectionTest extends AbstractDSpaceObjectTest {
*/ */
@Test @Test
public void testCreateSubmittersAuth() throws Exception { public void testCreateSubmittersAuth() throws Exception {
// Use JMockit to mock the AuthorizeUtil & allow permissions // Allow Collection ADMIN (to manage submitter group)
new MockUp<AuthorizeUtil>() { doNothing().when(authorizeServiceSpy).authorizeAction(context, collection, Constants.ADMIN);
@Mock
public void authorizeManageSubmittersGroup(Context context, Collection collection) {
return; // allow permissions by not throwing an exception
}
};
Group result = collectionService.createSubmitters(context, collection); Group result = collectionService.createSubmitters(context, collection);
assertThat("testCreateSubmittersAuth 0", result, notNullValue()); assertThat("testCreateSubmittersAuth 0", result, notNullValue());
@@ -479,15 +461,6 @@ public class CollectionTest extends AbstractDSpaceObjectTest {
*/ */
@Test(expected = AuthorizeException.class) @Test(expected = AuthorizeException.class)
public void testCreateSubmittersNoAuth() throws Exception { public void testCreateSubmittersNoAuth() throws Exception {
// Use JMockit to mock the AuthorizeUtil & disallow permissions
new MockUp<AuthorizeUtil>() {
@Mock
public void authorizeManageSubmittersGroup(Context context, Collection collection)
throws AuthorizeException {
throw new AuthorizeException();
}
};
Group result = collectionService.createSubmitters(context, collection); Group result = collectionService.createSubmitters(context, collection);
fail("Exception expected"); fail("Exception expected");
} }
@@ -497,13 +470,8 @@ public class CollectionTest extends AbstractDSpaceObjectTest {
*/ */
@Test @Test
public void testRemoveSubmittersAuth() throws Exception { public void testRemoveSubmittersAuth() throws Exception {
// Use JMockit to mock the AuthorizeUtil & allow permissions // Allow Collection ADMIN (to manage submitter group)
new MockUp<AuthorizeUtil>() { doNothing().when(authorizeServiceSpy).authorizeAction(context, collection, Constants.ADMIN);
@Mock
public void authorizeManageSubmittersGroup(Context context, Collection collection) {
return; // allow permissions by not throwing an exception
}
};
collectionService.removeSubmitters(context, collection); collectionService.removeSubmitters(context, collection);
assertThat("testRemoveSubmittersAuth 0", collection.getSubmitters(), nullValue()); assertThat("testRemoveSubmittersAuth 0", collection.getSubmitters(), nullValue());
@@ -514,15 +482,6 @@ public class CollectionTest extends AbstractDSpaceObjectTest {
*/ */
@Test(expected = AuthorizeException.class) @Test(expected = AuthorizeException.class)
public void testRemoveSubmittersNoAuth() throws Exception { public void testRemoveSubmittersNoAuth() throws Exception {
// Use JMockit to mock the AuthorizeUtil & disallow permissions
new MockUp<AuthorizeUtil>() {
@Mock
public void authorizeManageSubmittersGroup(Context context, Collection collection)
throws AuthorizeException {
throw new AuthorizeException();
}
};
collectionService.removeSubmitters(context, collection); collectionService.removeSubmitters(context, collection);
fail("Exception expected"); fail("Exception expected");
} }
@@ -540,13 +499,8 @@ public class CollectionTest extends AbstractDSpaceObjectTest {
*/ */
@Test @Test
public void testCreateAdministratorsAuth() throws Exception { public void testCreateAdministratorsAuth() throws Exception {
// Use JMockit to mock the AuthorizeUtil & allow permissions // Allow Collection ADMIN (to manage admin group)
new MockUp<AuthorizeUtil>() { doNothing().when(authorizeServiceSpy).authorizeAction(context, collection, Constants.ADMIN);
@Mock
public void authorizeManageAdminGroup(Context context, Collection collection) {
return; // allow permissions by not throwing an exception
}
};
Group result = collectionService.createAdministrators(context, collection); Group result = collectionService.createAdministrators(context, collection);
assertThat("testCreateAdministratorsAuth 0", result, notNullValue()); assertThat("testCreateAdministratorsAuth 0", result, notNullValue());
@@ -557,15 +511,6 @@ public class CollectionTest extends AbstractDSpaceObjectTest {
*/ */
@Test(expected = AuthorizeException.class) @Test(expected = AuthorizeException.class)
public void testCreateAdministratorsNoAuth() throws Exception { public void testCreateAdministratorsNoAuth() throws Exception {
// Use JMockit to mock the AuthorizeUtil & disallow permissions
new MockUp<AuthorizeUtil>() {
@Mock
public void authorizeManageAdminGroup(Context context, Collection collection)
throws AuthorizeException {
throw new AuthorizeException();
}
};
Group result = collectionService.createAdministrators(context, collection); Group result = collectionService.createAdministrators(context, collection);
fail("Exception expected"); fail("Exception expected");
} }
@@ -575,13 +520,8 @@ public class CollectionTest extends AbstractDSpaceObjectTest {
*/ */
@Test @Test
public void testRemoveAdministratorsAuth() throws Exception { public void testRemoveAdministratorsAuth() throws Exception {
// Use JMockit to mock the AuthorizeUtil & allow permissions // Allow parent Community ADMIN (only Community Admins can delete a Collection Admin group)
new MockUp<AuthorizeUtil>() { doNothing().when(authorizeServiceSpy).authorizeAction(context, owningCommunity, Constants.ADMIN);
@Mock
public void authorizeRemoveAdminGroup(Context context, Collection collection) {
return; // allow permissions by not throwing an exception
}
};
// Ensure admin group is created first // Ensure admin group is created first
context.turnOffAuthorisationSystem(); context.turnOffAuthorisationSystem();
@@ -599,14 +539,6 @@ public class CollectionTest extends AbstractDSpaceObjectTest {
*/ */
@Test(expected = AuthorizeException.class) @Test(expected = AuthorizeException.class)
public void testRemoveAdministratorsNoAuth() throws Exception { public void testRemoveAdministratorsNoAuth() throws Exception {
// Use JMockit to mock the AuthorizeUtil & disallow permissions
new MockUp<AuthorizeUtil>() {
@Mock
public void authorizeRemoveAdminGroup(Context context, Collection collection) throws AuthorizeException {
throw new AuthorizeException();
}
};
// Ensure admin group is created first // Ensure admin group is created first
context.turnOffAuthorisationSystem(); context.turnOffAuthorisationSystem();
Group result = collectionService.createAdministrators(context, collection); Group result = collectionService.createAdministrators(context, collection);
@@ -679,13 +611,8 @@ public class CollectionTest extends AbstractDSpaceObjectTest {
*/ */
@Test @Test
public void testCreateTemplateItemAuth() throws Exception { public void testCreateTemplateItemAuth() throws Exception {
// Use JMockit to mock the AuthorizeUtil & allow permissions // Allow Collection ADMIN (to manage template item)
new MockUp<AuthorizeUtil>() { doNothing().when(authorizeServiceSpy).authorizeAction(context, collection, Constants.ADMIN);
@Mock
public void authorizeManageTemplateItem(Context context, Collection collection) {
return; // allow permissions by not throwing an exception
}
};
itemService.createTemplateItem(context, collection); itemService.createTemplateItem(context, collection);
assertThat("testCreateTemplateItemAuth 0", collection.getTemplateItem(), notNullValue()); assertThat("testCreateTemplateItemAuth 0", collection.getTemplateItem(), notNullValue());
@@ -696,15 +623,6 @@ public class CollectionTest extends AbstractDSpaceObjectTest {
*/ */
@Test(expected = AuthorizeException.class) @Test(expected = AuthorizeException.class)
public void testCreateTemplateItemNoAuth() throws Exception { public void testCreateTemplateItemNoAuth() throws Exception {
// Use JMockit to mock the AuthorizeUtil & disallow permissions
new MockUp<AuthorizeUtil>() {
@Mock
public void authorizeManageTemplateItem(Context context, Collection collection)
throws AuthorizeException {
throw new AuthorizeException();
}
};
itemService.createTemplateItem(context, collection); itemService.createTemplateItem(context, collection);
fail("Exception expected"); fail("Exception expected");
} }
@@ -714,13 +632,8 @@ public class CollectionTest extends AbstractDSpaceObjectTest {
*/ */
@Test @Test
public void testRemoveTemplateItemAuth() throws Exception { public void testRemoveTemplateItemAuth() throws Exception {
// Use JMockit to mock the AuthorizeUtil & allow permissions // Allow Collection ADMIN (to manage template item)
new MockUp<AuthorizeUtil>() { doNothing().when(authorizeServiceSpy).authorizeAction(context, collection, Constants.ADMIN);
@Mock
public void authorizeManageTemplateItem(Context context, Collection collection) {
return; // allow permissions by not throwing an exception
}
};
collectionService.removeTemplateItem(context, collection); collectionService.removeTemplateItem(context, collection);
assertThat("testRemoveTemplateItemAuth 0", collection.getTemplateItem(), nullValue()); assertThat("testRemoveTemplateItemAuth 0", collection.getTemplateItem(), nullValue());
@@ -731,15 +644,6 @@ public class CollectionTest extends AbstractDSpaceObjectTest {
*/ */
@Test(expected = AuthorizeException.class) @Test(expected = AuthorizeException.class)
public void testRemoveTemplateItemNoAuth() throws Exception { public void testRemoveTemplateItemNoAuth() throws Exception {
// Use JMockit to mock the AuthorizeUtil & disallow permissions
new MockUp<AuthorizeUtil>() {
@Mock
public void authorizeManageTemplateItem(Context context, Collection collection)
throws AuthorizeException {
throw new AuthorizeException();
}
};
collectionService.removeTemplateItem(context, collection); collectionService.removeTemplateItem(context, collection);
fail("Exception expected"); fail("Exception expected");
} }
@@ -1069,13 +973,8 @@ public class CollectionTest extends AbstractDSpaceObjectTest {
public void testDeleteAuth() throws Exception { public void testDeleteAuth() throws Exception {
// Allow Collection WRITE perms // Allow Collection WRITE perms
doNothing().when(authorizeServiceSpy).authorizeAction(context, collection, Constants.WRITE, true); doNothing().when(authorizeServiceSpy).authorizeAction(context, collection, Constants.WRITE, true);
// Use JMockit to mock the AuthorizeUtil & allow permissions // Allow Collection ADMIN perms
new MockUp<AuthorizeUtil>() { doNothing().when(authorizeServiceSpy).authorizeAction(context, collection, Constants.ADMIN);
@Mock
public void authorizeManageTemplateItem(Context context, Collection collection) {
return; // allow permissions by not throwing an exception
}
};
UUID id = collection.getID(); UUID id = collection.getID();
collectionService.delete(context, collection); collectionService.delete(context, collection);
@@ -1092,26 +991,6 @@ public class CollectionTest extends AbstractDSpaceObjectTest {
fail("Exception expected"); fail("Exception expected");
} }
/**
* Test of delete method, of class Collection.
*/
@Test(expected = AuthorizeException.class)
public void testDeleteNoAuth2() throws Exception {
// Allow Collection WRITE perms
doThrow(new AuthorizeException()).when(authorizeServiceSpy)
.authorizeAction(context, collection, Constants.WRITE, true);
// Use JMockit to mock the AuthorizeUtil & allow permissions
new MockUp<AuthorizeUtil>() {
@Mock
public void authorizeManageTemplateItem(Context context, Collection collection) {
return; // allow permissions by not throwing an exception
}
};
collectionService.delete(context, collection);
fail("Exception expected");
}
/** /**
* Test of getCommunities method, of class Collection. * Test of getCommunities method, of class Collection.
*/ */

View File

@@ -29,11 +29,9 @@ import java.sql.SQLException;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import mockit.Mock;
import mockit.MockUp;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.dspace.app.util.AuthorizeUtil;
import org.dspace.authorize.AuthorizeException; import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.factory.AuthorizeServiceFactory;
import org.dspace.authorize.service.AuthorizeService; import org.dspace.authorize.service.AuthorizeService;
import org.dspace.core.Constants; import org.dspace.core.Constants;
import org.dspace.core.Context; import org.dspace.core.Context;
@@ -89,11 +87,14 @@ public class CommunityTest extends AbstractDSpaceObjectTest {
// Initialize our spy of the autowired (global) authorizeService bean. // Initialize our spy of the autowired (global) authorizeService bean.
// This allows us to customize the bean's method return values in tests below // This allows us to customize the bean's method return values in tests below
authorizeServiceSpy = spy(authorizeService); authorizeServiceSpy = spy(authorizeService);
// "Wire" our spy to be used by the current loaded CommunityService, CollectionService & ItemService // "Wire" our spy to be used by the current loaded object services
// (To ensure both these services use the spy instead of the real service) // (To ensure both these services use the spy instead of the real service)
ReflectionTestUtils.setField(communityService, "authorizeService", authorizeServiceSpy); ReflectionTestUtils.setField(communityService, "authorizeService", authorizeServiceSpy);
ReflectionTestUtils.setField(collectionService, "authorizeService", authorizeServiceSpy); ReflectionTestUtils.setField(collectionService, "authorizeService", authorizeServiceSpy);
ReflectionTestUtils.setField(itemService, "authorizeService", authorizeServiceSpy); ReflectionTestUtils.setField(itemService, "authorizeService", authorizeServiceSpy);
// Also wire into current AuthorizeServiceFactory, as that is used for some checks (e.g. AuthorizeUtil)
ReflectionTestUtils.setField(AuthorizeServiceFactory.getInstance(), "authorizeService",
authorizeServiceSpy);
} catch (AuthorizeException ex) { } catch (AuthorizeException ex) {
log.error("Authorization Error in init", ex); log.error("Authorization Error in init", ex);
fail("Authorization Error in init: " + ex.getMessage()); fail("Authorization Error in init: " + ex.getMessage());
@@ -411,13 +412,8 @@ public class CommunityTest extends AbstractDSpaceObjectTest {
*/ */
@Test @Test
public void testCreateAdministratorsAuth() throws Exception { public void testCreateAdministratorsAuth() throws Exception {
// Use JMockit to mock the AuthorizeUtil which is used by createAdministrators() to check permissions // Allow Community ADMIN perms
new MockUp<AuthorizeUtil>() { doNothing().when(authorizeServiceSpy).authorizeAction(context, c, Constants.ADMIN);
@Mock
public void authorizeManageAdminGroup(Context context, Community community) {
return; // allow permissions by not throwing an exception
}
};
Group result = communityService.createAdministrators(context, c); Group result = communityService.createAdministrators(context, c);
assertThat("testCreateAdministratorsAuth 0", c.getAdministrators(), notNullValue()); assertThat("testCreateAdministratorsAuth 0", c.getAdministrators(), notNullValue());
@@ -429,14 +425,6 @@ public class CommunityTest extends AbstractDSpaceObjectTest {
*/ */
@Test(expected = AuthorizeException.class) @Test(expected = AuthorizeException.class)
public void testCreateAdministratorsNoAuth() throws Exception { public void testCreateAdministratorsNoAuth() throws Exception {
// Use JMockit to mock the AuthorizeUtil which is used by createAdministrators() to check permissions
new MockUp<AuthorizeUtil>() {
@Mock
public void authorizeManageAdminGroup(Context context, Community community) throws AuthorizeException {
throw new AuthorizeException("fake error message");
}
};
Group result = communityService.createAdministrators(context, c); Group result = communityService.createAdministrators(context, c);
fail("Exception should have been thrown"); fail("Exception should have been thrown");
} }
@@ -447,13 +435,8 @@ public class CommunityTest extends AbstractDSpaceObjectTest {
*/ */
@Test @Test
public void testRemoveAdministratorsAuth() throws Exception { public void testRemoveAdministratorsAuth() throws Exception {
// Use JMockit to mock the AuthorizeUtil which is used by createAdministrators() to check permissions // Allow Full ADMIN perms (Community Admins cannot delete their Admin Group)
new MockUp<AuthorizeUtil>() { when(authorizeServiceSpy.isAdmin(context)).thenReturn(true);
@Mock
public void authorizeRemoveAdminGroup(Context context, Community community) {
return; // allow permissions by not throwing an exception
}
};
// Ensure admin group is created first // Ensure admin group is created first
context.turnOffAuthorisationSystem(); context.turnOffAuthorisationSystem();
@@ -471,13 +454,8 @@ public class CommunityTest extends AbstractDSpaceObjectTest {
*/ */
@Test(expected = AuthorizeException.class) @Test(expected = AuthorizeException.class)
public void testRemoveAdministratorsNoAuth() throws Exception { public void testRemoveAdministratorsNoAuth() throws Exception {
// Use JMockit to mock the AuthorizeUtil which is used by createAdministrators() to check permissions // Allow Community ADMIN perms (Community Admins cannot delete their Admin Group)
new MockUp<AuthorizeUtil>() { doNothing().when(authorizeServiceSpy).authorizeAction(context, c, Constants.ADMIN);
@Mock
public void authorizeRemoveAdminGroup(Context context, Community community) throws AuthorizeException {
throw new AuthorizeException("fake error message");
}
};
// Ensure admin group is created first // Ensure admin group is created first
context.turnOffAuthorisationSystem(); context.turnOffAuthorisationSystem();
@@ -736,20 +714,6 @@ public class CommunityTest extends AbstractDSpaceObjectTest {
*/ */
@Test @Test
public void testRemoveCollectionAuth() throws Exception { public void testRemoveCollectionAuth() throws Exception {
// Allow current Community REMOVE perms (to remove Collection from Community & delete)
doNothing().when(authorizeServiceSpy).authorizeAction(context, c, Constants.REMOVE);
// Allow collection WRITE perms (needed to remove Logo before Collection can be deleted)
doNothing().when(authorizeServiceSpy)
.authorizeAction(any(Context.class), any(Collection.class), eq(Constants.WRITE), eq(true));
// Use JMockit to mock the AuthorizeUtil
new MockUp<AuthorizeUtil>() {
// Allow Collection ManageTemplateItem perms (needed to remove Item Template before Collection is deleted)
@Mock
public void authorizeManageTemplateItem(Context context, Collection collection) {
return; // allow permissions by not throwing an exception
}
};
// Turn off authorization temporarily to create a new Collection // Turn off authorization temporarily to create a new Collection
context.turnOffAuthorisationSystem(); context.turnOffAuthorisationSystem();
Collection col = collectionService.create(context, c); Collection col = collectionService.create(context, c);
@@ -758,6 +722,13 @@ public class CommunityTest extends AbstractDSpaceObjectTest {
assertTrue("testRemoveCollectionAuth 1", c.getCollections().size() == 1); assertTrue("testRemoveCollectionAuth 1", c.getCollections().size() == 1);
assertThat("testRemoveCollectionAuth 2", c.getCollections().get(0), equalTo(col)); assertThat("testRemoveCollectionAuth 2", c.getCollections().get(0), equalTo(col));
// Allow current Community REMOVE perms (to remove Collection from Community & delete)
doNothing().when(authorizeServiceSpy).authorizeAction(context, c, Constants.REMOVE);
// Allow collection WRITE perms (needed to remove Logo before Collection can be deleted)
doNothing().when(authorizeServiceSpy).authorizeAction(context, col, Constants.WRITE,true);
// Allow current Community ADMIN perms (to remove Collection from Community & delete)
doNothing().when(authorizeServiceSpy).authorizeAction(context, col, Constants.ADMIN);
// Note that this will *also* delete the collection (hence the extra permissions provided above) // Note that this will *also* delete the collection (hence the extra permissions provided above)
communityService.removeCollection(context, c, col); communityService.removeCollection(context, c, col);
assertThat("testRemoveCollectionAuth 3", c.getCollections(), notNullValue()); assertThat("testRemoveCollectionAuth 3", c.getCollections(), notNullValue());
@@ -876,6 +847,9 @@ public class CommunityTest extends AbstractDSpaceObjectTest {
// Allow current Collection DELETE perms (needed to delete a Collection) // Allow current Collection DELETE perms (needed to delete a Collection)
doNothing().when(authorizeServiceSpy) doNothing().when(authorizeServiceSpy)
.authorizeAction(any(Context.class), any(Collection.class), eq(Constants.DELETE), eq(true)); .authorizeAction(any(Context.class), any(Collection.class), eq(Constants.DELETE), eq(true));
// Allow current Collection ADMIN perms (needed to delete a Collection)
doNothing().when(authorizeServiceSpy)
.authorizeAction(any(Context.class), any(Collection.class), eq(Constants.ADMIN));
// Allow current Item WRITE perms (needed to remove identifiers from an Item prior to deletion) // Allow current Item WRITE perms (needed to remove identifiers from an Item prior to deletion)
doNothing().when(authorizeServiceSpy) doNothing().when(authorizeServiceSpy)
.authorizeAction(any(Context.class), any(Item.class), eq(Constants.WRITE), eq(true)); .authorizeAction(any(Context.class), any(Item.class), eq(Constants.WRITE), eq(true));
@@ -885,14 +859,6 @@ public class CommunityTest extends AbstractDSpaceObjectTest {
// Allow current Item REMOVE perms (needed to delete Item) // Allow current Item REMOVE perms (needed to delete Item)
doNothing().when(authorizeServiceSpy) doNothing().when(authorizeServiceSpy)
.authorizeAction(any(Context.class), any(Item.class), eq(Constants.REMOVE), eq(true)); .authorizeAction(any(Context.class), any(Item.class), eq(Constants.REMOVE), eq(true));
// Use JMockit to mock the AuthorizeUtil
new MockUp<AuthorizeUtil>() {
// Allow Collection ManageTemplateItem perms (needed to delete a collection)
@Mock
public void authorizeManageTemplateItem(Context context, Collection collection) {
return; // allow permissions by not throwing an exception
}
};
// Create a dummy Community hierarchy to test delete with // Create a dummy Community hierarchy to test delete with
// Turn off authorization temporarily to create some test objects. // Turn off authorization temporarily to create some test objects.

View File

@@ -33,13 +33,11 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import mockit.Mock;
import mockit.MockUp;
import org.apache.commons.lang3.time.DateUtils; import org.apache.commons.lang3.time.DateUtils;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.dspace.app.util.AuthorizeUtil;
import org.dspace.authorize.AuthorizeException; import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.ResourcePolicy; import org.dspace.authorize.ResourcePolicy;
import org.dspace.authorize.factory.AuthorizeServiceFactory;
import org.dspace.authorize.service.AuthorizeService; import org.dspace.authorize.service.AuthorizeService;
import org.dspace.content.factory.ContentServiceFactory; import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.BitstreamFormatService; import org.dspace.content.service.BitstreamFormatService;
@@ -121,6 +119,9 @@ public class ItemTest extends AbstractDSpaceObjectTest {
ReflectionTestUtils.setField(workspaceItemService, "authorizeService", authorizeServiceSpy); ReflectionTestUtils.setField(workspaceItemService, "authorizeService", authorizeServiceSpy);
ReflectionTestUtils.setField(bundleService, "authorizeService", authorizeServiceSpy); ReflectionTestUtils.setField(bundleService, "authorizeService", authorizeServiceSpy);
ReflectionTestUtils.setField(bitstreamService, "authorizeService", authorizeServiceSpy); ReflectionTestUtils.setField(bitstreamService, "authorizeService", authorizeServiceSpy);
// Also wire into current AuthorizeServiceFactory, as that is used for some checks (e.g. AuthorizeUtil)
ReflectionTestUtils.setField(AuthorizeServiceFactory.getInstance(), "authorizeService",
authorizeServiceSpy);
} catch (AuthorizeException ex) { } catch (AuthorizeException ex) {
log.error("Authorization Error in init", ex); log.error("Authorization Error in init", ex);
fail("Authorization Error in init: " + ex.getMessage()); fail("Authorization Error in init: " + ex.getMessage());
@@ -1124,14 +1125,8 @@ public class ItemTest extends AbstractDSpaceObjectTest {
public void testWithdrawAuth() throws Exception { public void testWithdrawAuth() throws Exception {
// Allow Item WRITE perms // Allow Item WRITE perms
doNothing().when(authorizeServiceSpy).authorizeAction(context, it, Constants.WRITE); doNothing().when(authorizeServiceSpy).authorizeAction(context, it, Constants.WRITE);
// Allow Collection ADMIN perms
// Use JMockit to mock the AuthorizeUtil & allow permissions when(authorizeServiceSpy.authorizeActionBoolean(context, collection, Constants.ADMIN)).thenReturn(true);
new MockUp<AuthorizeUtil>() {
@Mock
public void authorizeWithdrawItem(Context context, Item item) {
return; // allow permissions by not throwing an exception
}
};
itemService.withdraw(context, it); itemService.withdraw(context, it);
assertTrue("testWithdrawAuth 0", it.isWithdrawn()); assertTrue("testWithdrawAuth 0", it.isWithdrawn());
@@ -1153,14 +1148,8 @@ public class ItemTest extends AbstractDSpaceObjectTest {
public void testReinstateAuth() throws Exception { public void testReinstateAuth() throws Exception {
// Allow Item WRITE perms // Allow Item WRITE perms
doNothing().when(authorizeServiceSpy).authorizeAction(context, it, Constants.WRITE); doNothing().when(authorizeServiceSpy).authorizeAction(context, it, Constants.WRITE);
// Allow Collection ADD perms (needed to reinstate)
// Use JMockit to mock the AuthorizeUtil & allow permissions doNothing().when(authorizeServiceSpy).authorizeAction(context, collection, Constants.ADD);
new MockUp<AuthorizeUtil>() {
@Mock
public void authorizeReinstateItem(Context context, Item item) {
return; // allow permissions by not throwing an exception
}
};
// initialize item as withdrawn // initialize item as withdrawn
context.turnOffAuthorisationSystem(); context.turnOffAuthorisationSystem();
@@ -1199,8 +1188,6 @@ public class ItemTest extends AbstractDSpaceObjectTest {
doNothing().when(authorizeServiceSpy).authorizeAction(context, item, Constants.REMOVE, true); doNothing().when(authorizeServiceSpy).authorizeAction(context, item, Constants.REMOVE, true);
// Allow Item DELETE perms // Allow Item DELETE perms
doNothing().when(authorizeServiceSpy).authorizeAction(context, item, Constants.DELETE); doNothing().when(authorizeServiceSpy).authorizeAction(context, item, Constants.DELETE);
// Allow Item WRITE perms
doNothing().when(authorizeServiceSpy).authorizeAction(context, item, Constants.WRITE);
UUID id = item.getID(); UUID id = item.getID();
itemService.delete(context, item); itemService.delete(context, item);