Refactor tests to use Mockito

This commit is contained in:
Tim Donohue
2020-01-14 16:30:18 -06:00
parent 412c8a1ec8
commit 8c7127ed8c

View File

@@ -15,6 +15,11 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.spy;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
@@ -23,9 +28,9 @@ import java.sql.SQLException;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import mockit.NonStrictExpectations;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.dspace.authorize.AuthorizeException; import org.dspace.authorize.AuthorizeException;
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;
import org.dspace.core.Constants; import org.dspace.core.Constants;
@@ -33,6 +38,7 @@ import org.dspace.core.Context;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.test.util.ReflectionTestUtils;
/** /**
* Unit Tests for class Bitstream * Unit Tests for class Bitstream
@@ -54,6 +60,12 @@ public class BitstreamTest extends AbstractDSpaceObjectTest {
*/ */
private Bitstream bs; private Bitstream bs;
/**
* Spy of AuthorizeService to use for tests
* (initialized / setup in @Before method)
*/
private AuthorizeService authorizeServiceSpy;
/** /**
* This method will be run before every test as per @Before. It will * This method will be run before every test as per @Before. It will
* initialize resources required for the tests. * initialize resources required for the tests.
@@ -67,10 +79,18 @@ public class BitstreamTest extends AbstractDSpaceObjectTest {
super.init(); super.init();
try { try {
//we have to create a new bitstream in the database //we have to create a new bitstream in the database
context.turnOffAuthorisationSystem();
File f = new File(testProps.get("test.bitstream").toString()); File f = new File(testProps.get("test.bitstream").toString());
this.bs = bitstreamService.create(context, new FileInputStream(f)); this.bs = bitstreamService.create(context, new FileInputStream(f));
this.dspaceObject = bs; this.dspaceObject = bs;
//we need to commit the changes so we don't block the table for testing context.restoreAuthSystemState();
// Initialize our spy of the autowired (global) authorizeService bean.
// This allows us to customize the bean's method return values in tests below
authorizeServiceSpy = spy(authorizeService);
// "Wire" our spy to be used by the current loaded bitstreamService
// (To ensure it uses the spy instead of the real service)
ReflectionTestUtils.setField(bitstreamService, "authorizeService", authorizeServiceSpy);
} catch (IOException ex) { } catch (IOException ex) {
log.error("IO Error in init", ex); log.error("IO Error in init", ex);
fail("SQL Error in init: " + ex.getMessage()); fail("SQL Error in init: " + ex.getMessage());
@@ -146,11 +166,10 @@ public class BitstreamTest extends AbstractDSpaceObjectTest {
*/ */
@Test @Test
public void testRegister() throws IOException, SQLException, AuthorizeException { public void testRegister() throws IOException, SQLException, AuthorizeException {
new NonStrictExpectations(authorizeService.getClass()) {{ // Allow general Bitstream WRITE permissions
authorizeService.authorizeAction((Context) any, (Bitstream) any, doNothing().when(authorizeServiceSpy).authorizeAction(any(Context.class), any(Bitstream.class),
Constants.WRITE); eq(Constants.WRITE));
result = null;
}};
int assetstore = 0; int assetstore = 0;
File f = new File(testProps.get("test.bitstream").toString()); File f = new File(testProps.get("test.bitstream").toString());
Bitstream registered = bitstreamService.register(context, assetstore, f.getName()); Bitstream registered = bitstreamService.register(context, assetstore, f.getName());
@@ -366,13 +385,9 @@ public class BitstreamTest extends AbstractDSpaceObjectTest {
*/ */
@Test(expected = AuthorizeException.class) @Test(expected = AuthorizeException.class)
public void testUpdateNotAdmin() throws SQLException, AuthorizeException { public void testUpdateNotAdmin() throws SQLException, AuthorizeException {
new NonStrictExpectations(authorizeService.getClass()) {{ // Disallow Bitstream WRITE permissions
// Disallow Bitstream WRITE perms doThrow(new AuthorizeException()).when(authorizeServiceSpy).authorizeAction(context, bs, Constants.WRITE);
authorizeService.authorizeAction((Context) any, (Bitstream) any,
Constants.WRITE);
result = new AuthorizeException();
}};
//TODO: we need to verify the update, how? //TODO: we need to verify the update, how?
bitstreamService.update(context, bs); bitstreamService.update(context, bs);
} }
@@ -382,13 +397,8 @@ public class BitstreamTest extends AbstractDSpaceObjectTest {
*/ */
@Test @Test
public void testUpdateAdmin() throws SQLException, AuthorizeException { public void testUpdateAdmin() throws SQLException, AuthorizeException {
new NonStrictExpectations(authorizeService.getClass()) {{ // Allow Bitstream WRITE permissions
// Allow Bitstream WRITE perms doNothing().when(authorizeServiceSpy).authorizeAction(context, bs, Constants.WRITE);
authorizeService.authorizeAction((Context) any, (Bitstream) any,
Constants.WRITE);
result = null;
}};
//TODO: we need to verify the update, how? //TODO: we need to verify the update, how?
bitstreamService.update(context, bs); bitstreamService.update(context, bs);
@@ -399,21 +409,18 @@ public class BitstreamTest extends AbstractDSpaceObjectTest {
*/ */
@Test @Test
public void testDeleteAndExpunge() throws IOException, SQLException, AuthorizeException { public void testDeleteAndExpunge() throws IOException, SQLException, AuthorizeException {
new NonStrictExpectations(authorizeService.getClass()) {{
// Allow Bitstream WRITE perms
authorizeService.authorizeAction((Context) any, (Bitstream) any,
Constants.WRITE);
result = null;
authorizeService.authorizeAction((Context) any, (Bitstream) any,
Constants.DELETE);
result = null;
}};
// Create a new bitstream, which we can delete. As ordering of these // Create a new bitstream, which we can delete. As ordering of these
// tests is unpredictable we don't want to delete the global bitstream // tests is unpredictable we don't want to delete the global bitstream
context.ignoreAuthorization();
File f = new File(testProps.get("test.bitstream").toString()); File f = new File(testProps.get("test.bitstream").toString());
Bitstream delBS = bitstreamService.create(context, new FileInputStream(f)); Bitstream delBS = bitstreamService.create(context, new FileInputStream(f));
UUID bitstreamId = delBS.getID(); UUID bitstreamId = delBS.getID();
context.restoreAuthSystemState();
// Allow Bitstream WRITE permissions
doNothing().when(authorizeServiceSpy).authorizeAction(context, delBS, Constants.WRITE);
// Allow Bitstream DELETE permissions
doNothing().when(authorizeServiceSpy).authorizeAction(context, delBS, Constants.DELETE);
// Test that delete will flag the bitstream as deleted // Test that delete will flag the bitstream as deleted
assertFalse("testIsDeleted 0", delBS.isDeleted()); assertFalse("testIsDeleted 0", delBS.isDeleted());
@@ -431,12 +438,8 @@ public class BitstreamTest extends AbstractDSpaceObjectTest {
@Test @Test
public void testRetrieveCanRead() throws IOException, SQLException, public void testRetrieveCanRead() throws IOException, SQLException,
AuthorizeException { AuthorizeException {
new NonStrictExpectations(authorizeService.getClass()) {{ // Allow Bitstream READ permissions
// Allow Bitstream READ perms doNothing().when(authorizeServiceSpy).authorizeAction(context, bs, Constants.READ);
authorizeService.authorizeAction((Context) any, (Bitstream) any,
Constants.READ);
result = null;
}};
assertThat("testRetrieveCanRead 0", bitstreamService.retrieve(context, bs), notNullValue()); assertThat("testRetrieveCanRead 0", bitstreamService.retrieve(context, bs), notNullValue());
} }
@@ -447,12 +450,8 @@ public class BitstreamTest extends AbstractDSpaceObjectTest {
@Test(expected = AuthorizeException.class) @Test(expected = AuthorizeException.class)
public void testRetrieveNoRead() throws IOException, SQLException, public void testRetrieveNoRead() throws IOException, SQLException,
AuthorizeException { AuthorizeException {
new NonStrictExpectations(authorizeService.getClass()) {{ // Disallow Bitstream READ permissions
// Disallow Bitstream READ perms doThrow(new AuthorizeException()).when(authorizeServiceSpy).authorizeAction(context, bs, Constants.READ);
authorizeService.authorizeAction((Context) any, (Bitstream) any,
Constants.READ);
result = new AuthorizeException();
}};
assertThat("testRetrieveNoRead 0", bitstreamService.retrieve(context, bs), notNullValue()); assertThat("testRetrieveNoRead 0", bitstreamService.retrieve(context, bs), notNullValue());
} }