mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-19 07:53:08 +00:00
DSpace refactored service api
This commit is contained in:
@@ -8,9 +8,10 @@
|
||||
package org.dspace.content;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.dspace.core.Constants;
|
||||
import org.dspace.core.Context;
|
||||
import org.dspace.authorize.AuthorizeManager;
|
||||
import mockit.NonStrictExpectations;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.File;
|
||||
@@ -20,6 +21,8 @@ import org.apache.log4j.Logger;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.dspace.authorize.AuthorizeException;
|
||||
import org.dspace.authorize.ResourcePolicy;
|
||||
|
||||
@@ -42,6 +45,10 @@ public class BundleTest extends AbstractDSpaceObjectTest
|
||||
* Bundle instance for the tests
|
||||
*/
|
||||
private Bundle b;
|
||||
private Item item;
|
||||
private Collection collection;
|
||||
private Community owningCommunity;
|
||||
|
||||
|
||||
/**
|
||||
* This method will be run before every test as per @Before. It will
|
||||
@@ -57,13 +64,18 @@ public class BundleTest extends AbstractDSpaceObjectTest
|
||||
super.init();
|
||||
try
|
||||
{
|
||||
this.b = Bundle.create(context);
|
||||
context.turnOffAuthorisationSystem();
|
||||
this.owningCommunity = communityService.create(null, context);
|
||||
this.collection = collectionService.create(context, owningCommunity);
|
||||
WorkspaceItem workspaceItem = workspaceItemService.create(context, collection, false);
|
||||
this.item = installItemService.installItem(context, workspaceItem);
|
||||
this.b = bundleService.create(context, item, "TESTBUNDLE");
|
||||
this.dspaceObject = b;
|
||||
|
||||
//we need to commit the changes so we don't block the table for testing
|
||||
context.commit();
|
||||
context.restoreAuthSystemState();
|
||||
}
|
||||
catch (SQLException ex)
|
||||
catch (SQLException | AuthorizeException ex)
|
||||
{
|
||||
log.error("SQL Error in init", ex);
|
||||
fail("SQL Error in init: " + ex.getMessage());
|
||||
@@ -81,18 +93,60 @@ public class BundleTest extends AbstractDSpaceObjectTest
|
||||
@Override
|
||||
public void destroy()
|
||||
{
|
||||
// try {
|
||||
// context.turnOffAuthorisationSystem();
|
||||
// b = bundleService.find(context, b.getID());
|
||||
// if(b != null)
|
||||
// {
|
||||
// itemService.removeBundle(context, item, b);
|
||||
// }
|
||||
// collectionService.removeItem(context, collection, item);
|
||||
// communityService.removeCollection(context, owningCommunity, collection);
|
||||
// communityService.delete(context, owningCommunity);
|
||||
// context.restoreAuthSystemState();
|
||||
// } catch (SQLException | AuthorizeException | IOException ex) {
|
||||
// log.error("SQL Error in destroy", ex);
|
||||
// fail("SQL Error in destroy: " + ex.getMessage());
|
||||
// }
|
||||
b = null;
|
||||
item = null;
|
||||
collection = null;
|
||||
owningCommunity = null;
|
||||
super.destroy();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteParents() throws Exception
|
||||
{
|
||||
try {
|
||||
context.turnOffAuthorisationSystem();
|
||||
b = bundleService.find(context, b.getID());
|
||||
if(b != null)
|
||||
{
|
||||
itemService.removeBundle(context, item, b);
|
||||
}
|
||||
item = itemService.find(context, item.getID());
|
||||
collection = collectionService.find(context, collection.getID());
|
||||
owningCommunity = communityService.find(context, owningCommunity.getID());
|
||||
|
||||
collectionService.removeItem(context, collection, item);
|
||||
communityService.removeCollection(context, owningCommunity, collection);
|
||||
communityService.delete(context, owningCommunity);
|
||||
context.restoreAuthSystemState();
|
||||
} catch (SQLException | AuthorizeException | IOException ex) {
|
||||
log.error("SQL Error in destroy", ex);
|
||||
fail("SQL Error in destroy: " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of find method, of class Bundle.
|
||||
*/
|
||||
@Test
|
||||
public void testBundleFind() throws SQLException
|
||||
{
|
||||
int id = b.getID();
|
||||
Bundle found = Bundle.find(context, id);
|
||||
UUID id = b.getID();
|
||||
Bundle found = bundleService.find(context, id);
|
||||
assertThat("testBundleFind 0", found, notNullValue());
|
||||
assertThat("testBundleFind 1", found.getID(), equalTo(id));
|
||||
}
|
||||
@@ -101,43 +155,56 @@ public class BundleTest extends AbstractDSpaceObjectTest
|
||||
* Test of create method, of class Bundle.
|
||||
*/
|
||||
@Test
|
||||
public void testCreate() throws SQLException
|
||||
public void testCreate() throws SQLException, AuthorizeException
|
||||
{
|
||||
Bundle created = Bundle.create(context);
|
||||
new NonStrictExpectations(authorizeService.getClass())
|
||||
{
|
||||
{
|
||||
// Allow Bundle ADD perms
|
||||
authorizeService.authorizeAction((Context) any, (Bundle) any,
|
||||
Constants.ADD);
|
||||
result = null;
|
||||
}};
|
||||
Bundle created = bundleService.create(context, item, "testCreateBundle");
|
||||
//the item created by default has no name nor type set
|
||||
assertThat("testCreate 0", created, notNullValue());
|
||||
assertTrue("testCreate 1", created.getID() >= 0);
|
||||
assertTrue("testCreate 2", created.getBitstreams().length == 0);
|
||||
assertThat("testCreate 3", created.getName(), nullValue());
|
||||
assertTrue("testCreate 1", created.getID() != null);
|
||||
assertTrue("testCreate 2", created.getBitstreams().size() == 0);
|
||||
assertEquals("testCreate 3", created.getName(), "testCreateBundle");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of getID method, of class Bundle.
|
||||
*/
|
||||
@Override
|
||||
@Test
|
||||
public void testGetID()
|
||||
{
|
||||
assertTrue("testGetID 0", b.getID() >= 0);
|
||||
assertTrue("testGetID 0", b.getID() != null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLegacyID() { assertTrue("testGetLegacyID 0", b.getLegacyId() == null);}
|
||||
|
||||
/**
|
||||
* Test of getName method, of class Bundle.
|
||||
*/
|
||||
@Override
|
||||
@Test
|
||||
public void testGetName()
|
||||
{
|
||||
//created bundle has no name
|
||||
assertThat("testGetName 0", b.getName(), nullValue());
|
||||
assertThat("testGetName 0", b.getName(), equalTo("TESTBUNDLE"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of setName method, of class Bundle.
|
||||
*/
|
||||
@Test
|
||||
public void testSetName()
|
||||
public void testSetName() throws SQLException
|
||||
{
|
||||
String name = "new name";
|
||||
b.setName(name);
|
||||
b.setName(context, name);
|
||||
assertThat("testSetName 0", b.getName(), notNullValue());
|
||||
assertThat("testSetName 1", b.getName(), not(equalTo("")));
|
||||
assertThat("testSetName 2", b.getName(), equalTo(name));
|
||||
@@ -150,38 +217,63 @@ public class BundleTest extends AbstractDSpaceObjectTest
|
||||
public void testGetPrimaryBitstreamID()
|
||||
{
|
||||
//is -1 when not set
|
||||
assertThat("testGetPrimaryBitstreamID 0", b.getPrimaryBitstreamID(), equalTo(-1));
|
||||
assertThat("testGetPrimaryBitstreamID 0", b.getPrimaryBitstream(), equalTo(null));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of setPrimaryBitstreamID method, of class Bundle.
|
||||
*/
|
||||
@Test
|
||||
public void testSetPrimaryBitstreamID()
|
||||
public void testSetPrimaryBitstreamID() throws SQLException, AuthorizeException, IOException
|
||||
{
|
||||
int id = 1;
|
||||
b.setPrimaryBitstreamID(id);
|
||||
assertThat("testSetPrimaryBitstreamID 0", b.getPrimaryBitstreamID(), equalTo(id));
|
||||
new NonStrictExpectations(authorizeService.getClass())
|
||||
{
|
||||
{
|
||||
// Allow Bundle ADD perms
|
||||
authorizeService.authorizeAction((Context) any, (Bundle) any,
|
||||
Constants.ADD);
|
||||
authorizeService.authorizeAction((Context) any, (Bitstream) any,
|
||||
Constants.WRITE);
|
||||
result = null;
|
||||
}};
|
||||
File f = new File(testProps.get("test.bitstream").toString());
|
||||
Bitstream bs = bitstreamService.create(context, new FileInputStream(f));
|
||||
bundleService.addBitstream(context, b, bs);
|
||||
b.setPrimaryBitstreamID(bs);
|
||||
assertThat("testSetPrimaryBitstreamID 0", b.getPrimaryBitstream(), equalTo(bs));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of unsetPrimaryBitstreamID method, of class Bundle.
|
||||
*/
|
||||
@Test
|
||||
public void testUnsetPrimaryBitstreamID()
|
||||
public void testUnsetPrimaryBitstreamID() throws IOException, SQLException, AuthorizeException
|
||||
{
|
||||
new NonStrictExpectations(authorizeService.getClass())
|
||||
{
|
||||
{
|
||||
// Allow Bundle ADD perms
|
||||
authorizeService.authorizeAction((Context) any, (Bundle) any,
|
||||
Constants.ADD);
|
||||
authorizeService.authorizeAction((Context) any, (Bitstream) any,
|
||||
Constants.WRITE);
|
||||
result = null;
|
||||
}};
|
||||
//set a value different than default
|
||||
int id = 6;
|
||||
b.setPrimaryBitstreamID(id);
|
||||
File f = new File(testProps.get("test.bitstream").toString());
|
||||
Bitstream bs = bitstreamService.create(context, new FileInputStream(f));
|
||||
bundleService.addBitstream(context, b, bs);
|
||||
b.setPrimaryBitstreamID(bs);
|
||||
//unset
|
||||
b.unsetPrimaryBitstreamID();
|
||||
//is -1 when not set
|
||||
assertThat("testUnsetPrimaryBitstreamID 0", b.getPrimaryBitstreamID(), equalTo(-1));
|
||||
assertThat("testUnsetPrimaryBitstreamID 0", b.getPrimaryBitstream(), equalTo(null));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of getHandle method, of class Bundle.
|
||||
*/
|
||||
@Override
|
||||
@Test
|
||||
public void testGetHandle()
|
||||
{
|
||||
@@ -195,27 +287,29 @@ public class BundleTest extends AbstractDSpaceObjectTest
|
||||
@Test
|
||||
public void testGetBitstreamByName() throws FileNotFoundException, SQLException, IOException, AuthorizeException
|
||||
{
|
||||
new NonStrictExpectations(AuthorizeManager.class)
|
||||
new NonStrictExpectations(authorizeService.getClass())
|
||||
{{
|
||||
// Allow Bundle ADD perms
|
||||
AuthorizeManager.authorizeAction((Context) any, (Bundle) any,
|
||||
authorizeService.authorizeAction((Context) any, (Bundle) any,
|
||||
Constants.ADD); result = null;
|
||||
authorizeService.authorizeAction((Context) any, (Bitstream) any,
|
||||
Constants.WRITE); result = null;
|
||||
|
||||
}};
|
||||
|
||||
String name = "name";
|
||||
//by default there is no bitstream
|
||||
assertThat("testGetHandle 0", b.getBitstreamByName(name), nullValue());
|
||||
assertThat("testGetHandle 0", bundleService.getBitstreamByName(b, name), nullValue());
|
||||
|
||||
//let's add a bitstream
|
||||
File f = new File(testProps.get("test.bitstream").toString());
|
||||
Bitstream bs = Bitstream.create(context, new FileInputStream(f));
|
||||
bs.setName(name);
|
||||
b.addBitstream(bs);
|
||||
assertThat("testGetHandle 1", b.getBitstreamByName(name), notNullValue());
|
||||
assertThat("testGetHandle 2", b.getBitstreamByName(name), equalTo(bs));
|
||||
assertThat("testGetHandle 3", b.getBitstreamByName(name).getName(), equalTo(name));
|
||||
context.commit();
|
||||
Bitstream bs = bitstreamService.create(context, new FileInputStream(f));
|
||||
bs.setName(context, name);
|
||||
bundleService.addBitstream(context, b, bs);
|
||||
bundleService.update(context, b);
|
||||
assertThat("testGetHandle 1", bundleService.getBitstreamByName(b, name), notNullValue());
|
||||
assertThat("testGetHandle 2", bundleService.getBitstreamByName(b, name), equalTo(bs));
|
||||
assertThat("testGetHandle 3", bundleService.getBitstreamByName(b, name).getName(), equalTo(name));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -224,28 +318,29 @@ public class BundleTest extends AbstractDSpaceObjectTest
|
||||
@Test
|
||||
public void testGetBitstreams() throws FileNotFoundException, SQLException, IOException, AuthorizeException
|
||||
{
|
||||
new NonStrictExpectations(AuthorizeManager.class)
|
||||
new NonStrictExpectations(authorizeService.getClass())
|
||||
{{
|
||||
// Allow Bundle ADD perms
|
||||
AuthorizeManager.authorizeAction((Context) any, (Bundle) any,
|
||||
authorizeService.authorizeAction((Context) any, (Bundle) any,
|
||||
Constants.ADD); result = null;
|
||||
authorizeService.authorizeAction((Context) any, (Bitstream) any,
|
||||
Constants.WRITE); result = null;
|
||||
|
||||
}};
|
||||
|
||||
//default bundle has no bitstreams
|
||||
assertThat("testGetBitstreams 0", b.getBitstreams(), notNullValue());
|
||||
assertThat("testGetBitstreams 1", b.getBitstreams().length, equalTo(0));
|
||||
assertThat("testGetBitstreams 1", b.getBitstreams().size(), equalTo(0));
|
||||
|
||||
//let's add a bitstream
|
||||
String name = "name";
|
||||
File f = new File(testProps.get("test.bitstream").toString());
|
||||
Bitstream bs = Bitstream.create(context, new FileInputStream(f));
|
||||
bs.setName(name);
|
||||
b.addBitstream(bs);
|
||||
Bitstream bs = bitstreamService.create(context, new FileInputStream(f));
|
||||
bs.setName(context, name);
|
||||
bundleService.addBitstream(context, b, bs);
|
||||
assertThat("testGetBitstreams 2", b.getBitstreams(), notNullValue());
|
||||
assertThat("testGetBitstreams 3", b.getBitstreams().length, equalTo(1));
|
||||
assertThat("testGetBitstreams 4", b.getBitstreams()[0].getName(), equalTo(name));
|
||||
context.commit();
|
||||
assertThat("testGetBitstreams 3", b.getBitstreams().size(), equalTo(1));
|
||||
assertThat("testGetBitstreams 4", b.getBitstreams().get(0).getBitstream().getName(), equalTo(name));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -256,7 +351,7 @@ public class BundleTest extends AbstractDSpaceObjectTest
|
||||
{
|
||||
//by default this bundle belong to no item
|
||||
assertThat("testGetItems 0", b.getItems(), notNullValue());
|
||||
assertThat("testGetItems 1", b.getItems().length, equalTo(0));
|
||||
assertThat("testGetItems 1", b.getItems().size(), equalTo(1));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -265,16 +360,16 @@ public class BundleTest extends AbstractDSpaceObjectTest
|
||||
@Test(expected=AuthorizeException.class)
|
||||
public void testCreateBitstreamNoAuth() throws FileNotFoundException, AuthorizeException, SQLException, IOException
|
||||
{
|
||||
new NonStrictExpectations(AuthorizeManager.class)
|
||||
new NonStrictExpectations(authorizeService.getClass())
|
||||
{{
|
||||
// Disallow Bundle ADD perms
|
||||
AuthorizeManager.authorizeAction((Context) any, (Bundle) any,
|
||||
authorizeService.authorizeAction((Context) any, (Bundle) any,
|
||||
Constants.ADD); result = new AuthorizeException();
|
||||
|
||||
}};
|
||||
|
||||
File f = new File(testProps.get("test.bitstream").toString());
|
||||
Bitstream bs = b.createBitstream(new FileInputStream(f));
|
||||
Bitstream bs = bitstreamService.create(context, b, new FileInputStream(f));
|
||||
fail("Exception should be thrown");
|
||||
}
|
||||
|
||||
@@ -284,21 +379,23 @@ public class BundleTest extends AbstractDSpaceObjectTest
|
||||
@Test
|
||||
public void testCreateBitstreamAuth() throws FileNotFoundException, AuthorizeException, SQLException, IOException
|
||||
{
|
||||
new NonStrictExpectations(AuthorizeManager.class)
|
||||
new NonStrictExpectations(authorizeService.getClass())
|
||||
{{
|
||||
// Allow Bundle ADD perms
|
||||
AuthorizeManager.authorizeAction((Context) any, (Bundle) any,
|
||||
authorizeService.authorizeAction((Context) any, (Bundle) any,
|
||||
Constants.ADD); result = null;
|
||||
authorizeService.authorizeAction((Context) any, (Bitstream) any,
|
||||
Constants.WRITE); result = null;
|
||||
|
||||
}};
|
||||
|
||||
String name = "name";
|
||||
File f = new File(testProps.get("test.bitstream").toString());
|
||||
Bitstream bs = b.createBitstream(new FileInputStream(f));
|
||||
bs.setName(name);
|
||||
assertThat("testCreateBitstreamAuth 0", b.getBitstreamByName(name), notNullValue());
|
||||
assertThat("testCreateBitstreamAuth 1", b.getBitstreamByName(name), equalTo(bs));
|
||||
assertThat("testCreateBitstreamAuth 2", b.getBitstreamByName(name).getName(), equalTo(name));
|
||||
Bitstream bs = bitstreamService.create(context, b, new FileInputStream(f));
|
||||
bs.setName(context, name);
|
||||
assertThat("testCreateBitstreamAuth 0", bundleService.getBitstreamByName(b, name), notNullValue());
|
||||
assertThat("testCreateBitstreamAuth 1", bundleService.getBitstreamByName(b, name), equalTo(bs));
|
||||
assertThat("testCreateBitstreamAuth 2", bundleService.getBitstreamByName(b, name).getName(), equalTo(name));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -307,17 +404,17 @@ public class BundleTest extends AbstractDSpaceObjectTest
|
||||
@Test(expected=AuthorizeException.class)
|
||||
public void testRegisterBitstreamNoAuth() throws AuthorizeException, IOException, SQLException
|
||||
{
|
||||
new NonStrictExpectations(AuthorizeManager.class)
|
||||
new NonStrictExpectations(authorizeService.getClass())
|
||||
{{
|
||||
// Disallow Bundle ADD perms
|
||||
AuthorizeManager.authorizeAction((Context) any, (Bundle) any,
|
||||
authorizeService.authorizeAction((Context) any, (Bundle) any,
|
||||
Constants.ADD); result = new AuthorizeException();
|
||||
|
||||
}};
|
||||
|
||||
int assetstore = 0; //default assetstore
|
||||
File f = new File(testProps.get("test.bitstream").toString());
|
||||
Bitstream bs = b.registerBitstream(assetstore, f.getAbsolutePath());
|
||||
Bitstream bs = bitstreamService.register(context, b, assetstore, f.getAbsolutePath());
|
||||
fail("Exception should be thrown");
|
||||
}
|
||||
|
||||
@@ -327,22 +424,24 @@ public class BundleTest extends AbstractDSpaceObjectTest
|
||||
@Test
|
||||
public void testRegisterBitstreamAuth() throws AuthorizeException, IOException, SQLException
|
||||
{
|
||||
new NonStrictExpectations(AuthorizeManager.class)
|
||||
new NonStrictExpectations(authorizeService.getClass())
|
||||
{{
|
||||
// Allow Bundle ADD perms
|
||||
AuthorizeManager.authorizeAction((Context) any, (Bundle) any,
|
||||
authorizeService.authorizeAction((Context) any, (Bundle) any,
|
||||
Constants.ADD); result = null;
|
||||
|
||||
authorizeService.authorizeAction((Context) any, (Bitstream) any,
|
||||
Constants.WRITE); result = null;
|
||||
}};
|
||||
|
||||
int assetstore = 0; //default assetstore
|
||||
String name = "name bitstream";
|
||||
File f = new File(testProps.get("test.bitstream").toString());
|
||||
Bitstream bs = b.registerBitstream(assetstore, f.getName());
|
||||
bs.setName(name);
|
||||
assertThat("testRegisterBitstream 0", b.getBitstreamByName(name), notNullValue());
|
||||
assertThat("testRegisterBitstream 1", b.getBitstreamByName(name), equalTo(bs));
|
||||
assertThat("testRegisterBitstream 2", b.getBitstreamByName(name).getName(), equalTo(name));
|
||||
Bitstream bs = bitstreamService.register(context, b, assetstore, f.getName());
|
||||
bs.setName(context, name);
|
||||
assertThat("testRegisterBitstream 0", bundleService.getBitstreamByName(b, name), notNullValue());
|
||||
assertThat("testRegisterBitstream 1", bundleService.getBitstreamByName(b, name), equalTo(bs));
|
||||
assertThat("testRegisterBitstream 2", bundleService.getBitstreamByName(b, name).getName(), equalTo(name));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -351,19 +450,19 @@ public class BundleTest extends AbstractDSpaceObjectTest
|
||||
@Test(expected=AuthorizeException.class)
|
||||
public void testAddBitstreamNoAuth() throws SQLException, AuthorizeException, IOException
|
||||
{
|
||||
new NonStrictExpectations(AuthorizeManager.class)
|
||||
new NonStrictExpectations(authorizeService.getClass())
|
||||
{{
|
||||
// Disallow Bundle ADD perms
|
||||
AuthorizeManager.authorizeAction((Context) any, (Bundle) any,
|
||||
authorizeService.authorizeAction((Context) any, (Bundle) any,
|
||||
Constants.ADD); result = new AuthorizeException();
|
||||
|
||||
}};
|
||||
|
||||
// create a new Bitstream to add to Bundle
|
||||
File f = new File(testProps.get("test.bitstream").toString());
|
||||
Bitstream bs = Bitstream.create(context, new FileInputStream(f));
|
||||
bs.setName("name");
|
||||
b.addBitstream(bs);
|
||||
Bitstream bs = bitstreamService.create(context, new FileInputStream(f));
|
||||
bs.setName(context, "name");
|
||||
bundleService.addBitstream(context, b, bs);
|
||||
fail("Exception should have been thrown");
|
||||
}
|
||||
|
||||
@@ -373,21 +472,23 @@ public class BundleTest extends AbstractDSpaceObjectTest
|
||||
@Test
|
||||
public void testAddBitstreamAuth() throws SQLException, AuthorizeException, FileNotFoundException, IOException
|
||||
{
|
||||
new NonStrictExpectations(AuthorizeManager.class)
|
||||
new NonStrictExpectations(authorizeService.getClass())
|
||||
{{
|
||||
// Allow Bundle ADD perms
|
||||
AuthorizeManager.authorizeAction((Context) any, (Bundle) any,
|
||||
authorizeService.authorizeAction((Context) any, (Bundle) any,
|
||||
Constants.ADD); result = null;
|
||||
authorizeService.authorizeAction((Context) any, (Bitstream) any,
|
||||
Constants.WRITE); result = null;
|
||||
|
||||
}};
|
||||
|
||||
File f = new File(testProps.get("test.bitstream").toString());
|
||||
Bitstream bs = Bitstream.create(context, new FileInputStream(f));
|
||||
bs.setName("name");
|
||||
b.addBitstream(bs);
|
||||
assertThat("testAddBitstreamAuth 0", b.getBitstreamByName(bs.getName()), notNullValue());
|
||||
assertThat("testAddBitstreamAuth 1", b.getBitstreamByName(bs.getName()), equalTo(bs));
|
||||
assertThat("testAddBitstreamAuth 2", b.getBitstreamByName(bs.getName()).getName(), equalTo(bs.getName()));
|
||||
Bitstream bs = bitstreamService.create(context, new FileInputStream(f));
|
||||
bs.setName(context, "name");
|
||||
bundleService.addBitstream(context, b, bs);
|
||||
assertThat("testAddBitstreamAuth 0", bundleService.getBitstreamByName(b, bs.getName()), notNullValue());
|
||||
assertThat("testAddBitstreamAuth 1", bundleService.getBitstreamByName(b, bs.getName()), equalTo(bs));
|
||||
assertThat("testAddBitstreamAuth 2", bundleService.getBitstreamByName(b, bs.getName()).getName(), equalTo(bs.getName()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -396,18 +497,18 @@ public class BundleTest extends AbstractDSpaceObjectTest
|
||||
@Test(expected=AuthorizeException.class)
|
||||
public void testRemoveBitstreamNoAuth() throws SQLException, AuthorizeException, IOException
|
||||
{
|
||||
new NonStrictExpectations(AuthorizeManager.class)
|
||||
new NonStrictExpectations(authorizeService.getClass())
|
||||
{{
|
||||
// Disallow Bundle REMOVE perms
|
||||
AuthorizeManager.authorizeAction((Context) any, (Bundle) any,
|
||||
authorizeService.authorizeAction((Context) any, (Bundle) any,
|
||||
Constants.REMOVE); result = new AuthorizeException();
|
||||
|
||||
}};
|
||||
|
||||
File f = new File(testProps.get("test.bitstream").toString());
|
||||
Bitstream bs = Bitstream.create(context, new FileInputStream(f));
|
||||
bs.setName("name");
|
||||
b.removeBitstream(bs);
|
||||
Bitstream bs = bitstreamService.create(context, new FileInputStream(f));
|
||||
bs.setName(context, "name");
|
||||
bundleService.removeBitstream(context, b, bs);
|
||||
fail("Exception should have been thrown");
|
||||
}
|
||||
|
||||
@@ -417,23 +518,27 @@ public class BundleTest extends AbstractDSpaceObjectTest
|
||||
@Test
|
||||
public void testRemoveBitstreamAuth() throws SQLException, AuthorizeException, IOException
|
||||
{
|
||||
new NonStrictExpectations(AuthorizeManager.class)
|
||||
new NonStrictExpectations(authorizeService.getClass())
|
||||
{{
|
||||
// Allow Bundle ADD perms (to create a new Bitstream and add it)
|
||||
AuthorizeManager.authorizeAction((Context) any, (Bundle) any,
|
||||
authorizeService.authorizeAction((Context) any, (Bundle) any,
|
||||
Constants.ADD); result = null;
|
||||
// Allow Bundle REMOVE perms (to test remove)
|
||||
AuthorizeManager.authorizeAction((Context) any, (Bundle) any,
|
||||
authorizeService.authorizeAction((Context) any, (Bundle) any,
|
||||
Constants.REMOVE); result = null;
|
||||
authorizeService.authorizeAction((Context) any, (Bitstream) any,
|
||||
Constants.WRITE); result = null;
|
||||
authorizeService.authorizeAction(context, (Bitstream) any,
|
||||
Constants.DELETE); result = null;
|
||||
|
||||
}};
|
||||
|
||||
// Create a new Bitstream to test with
|
||||
File f = new File(testProps.get("test.bitstream").toString());
|
||||
Bitstream bs = Bitstream.create(context, new FileInputStream(f));
|
||||
b.addBitstream(bs);
|
||||
context.commit();
|
||||
b.removeBitstream(bs);
|
||||
assertThat("testRemoveBitstreamAuth 0", b.getBitstreamByName(bs.getName()), nullValue());
|
||||
Bitstream bs = bitstreamService.create(context, new FileInputStream(f));
|
||||
bundleService.addBitstream(context, b, bs);
|
||||
bundleService.removeBitstream(context, b, bs);
|
||||
assertThat("testRemoveBitstreamAuth 0", bundleService.getBitstreamByName(b, bs.getName()), nullValue());
|
||||
}
|
||||
|
||||
|
||||
@@ -445,7 +550,7 @@ public class BundleTest extends AbstractDSpaceObjectTest
|
||||
{
|
||||
//TODO: we only check for sql errors
|
||||
//TODO: note that update can't throw authorize exception!!
|
||||
b.update();
|
||||
bundleService.update(context, b);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -454,35 +559,25 @@ public class BundleTest extends AbstractDSpaceObjectTest
|
||||
@Test
|
||||
public void testDelete() throws SQLException, AuthorizeException, IOException
|
||||
{
|
||||
new NonStrictExpectations(AuthorizeManager.class)
|
||||
new NonStrictExpectations(authorizeService.getClass())
|
||||
{{
|
||||
// Allow Bundle ADD perms (to create a new Bitstream and add it)
|
||||
AuthorizeManager.authorizeAction((Context) any, (Bundle) any,
|
||||
authorizeService.authorizeAction((Context) any, (Bundle) any,
|
||||
Constants.ADD); result = null;
|
||||
// Allow Bundle REMOVE perms (to test remove)
|
||||
AuthorizeManager.authorizeAction((Context) any, (Bundle) any,
|
||||
authorizeService.authorizeAction((Context) any, (Bundle) any,
|
||||
Constants.REMOVE); result = null;
|
||||
}};
|
||||
|
||||
// Create a new Bundle to be deleted
|
||||
Bundle created = Bundle.create(context);
|
||||
//let's add a bitstream
|
||||
File f = new File(testProps.get("test.bitstream").toString());
|
||||
Bitstream bs = Bitstream.create(context, new FileInputStream(f));
|
||||
created.addBitstream(bs);
|
||||
// Ensure both are saved to context
|
||||
context.commit();
|
||||
|
||||
// Now, delete the newly added Bundle and Bitstream
|
||||
int id = created.getID();
|
||||
created.delete();
|
||||
// Bundle should not exist anymore
|
||||
assertThat("testDelete 0", Bundle.find(context, id), nullValue());
|
||||
UUID id = b.getID();
|
||||
itemService.removeBundle(context, item, b);
|
||||
assertThat("testDelete 0", bundleService.find(context, id), nullValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of getType method, of class Bundle.
|
||||
*/
|
||||
@Override
|
||||
@Test
|
||||
public void testGetType()
|
||||
{
|
||||
@@ -495,40 +590,43 @@ public class BundleTest extends AbstractDSpaceObjectTest
|
||||
@Test
|
||||
public void testInheritCollectionDefaultPolicies() throws AuthorizeException, SQLException
|
||||
{
|
||||
Collection c = Collection.create(context);
|
||||
|
||||
//TODO: we would need a method to get policies from collection, probably better!
|
||||
List<ResourcePolicy> newpolicies = AuthorizeManager.getPoliciesActionFilter(context, c,
|
||||
List<ResourcePolicy> defaultCollectionPolicies = authorizeService.getPoliciesActionFilter(context, collection,
|
||||
Constants.DEFAULT_BITSTREAM_READ);
|
||||
Iterator<ResourcePolicy> it = newpolicies.iterator();
|
||||
Iterator<ResourcePolicy> it = defaultCollectionPolicies.iterator();
|
||||
|
||||
bundleService.inheritCollectionDefaultPolicies(context, b, collection);
|
||||
|
||||
while (it.hasNext())
|
||||
{
|
||||
ResourcePolicy rp = (ResourcePolicy) it.next();
|
||||
rp.setAction(Constants.READ);
|
||||
}
|
||||
|
||||
b.inheritCollectionDefaultPolicies(c);
|
||||
List<ResourcePolicy> bspolicies = bundleService.getBundlePolicies(context, b);
|
||||
assertTrue("testInheritCollectionDefaultPolicies 0", defaultCollectionPolicies.size() == bspolicies.size());
|
||||
|
||||
List<ResourcePolicy> bspolicies = b.getBundlePolicies();
|
||||
assertTrue("testInheritCollectionDefaultPolicies 0", newpolicies.size() == bspolicies.size());
|
||||
|
||||
boolean equals = true;
|
||||
for(int i=0; i < newpolicies.size() && equals; i++)
|
||||
boolean equals = false;
|
||||
for(int i=0; i < defaultCollectionPolicies.size(); i++)
|
||||
{
|
||||
if(!newpolicies.contains(bspolicies.get(i)))
|
||||
ResourcePolicy collectionPolicy = defaultCollectionPolicies.get(i);
|
||||
ResourcePolicy bundlePolicy = bspolicies.get(i);
|
||||
if(collectionPolicy.getAction() == bundlePolicy.getAction() && collectionPolicy.getGroup().equals(bundlePolicy.getGroup()))
|
||||
{
|
||||
equals = false;
|
||||
equals = true;
|
||||
}
|
||||
}
|
||||
assertTrue("testInheritCollectionDefaultPolicies 1", equals);
|
||||
|
||||
bspolicies = b.getBitstreamPolicies();
|
||||
bspolicies = bundleService.getBitstreamPolicies(context, b);
|
||||
boolean exists = true;
|
||||
for(int i=0; bspolicies.size() > 0 && i < newpolicies.size() && exists; i++)
|
||||
for(int i=0; bspolicies.size() > 0 && i < defaultCollectionPolicies.size(); i++)
|
||||
{
|
||||
if(!bspolicies.contains(newpolicies.get(i)))
|
||||
ResourcePolicy collectionPolicy = defaultCollectionPolicies.get(i);
|
||||
ResourcePolicy bitstreamPolicy = bspolicies.get(i);
|
||||
if(collectionPolicy.getAction() == bitstreamPolicy.getAction() && collectionPolicy.getGroup().equals(bitstreamPolicy.getGroup()))
|
||||
{
|
||||
exists = false;
|
||||
exists = true;
|
||||
}
|
||||
}
|
||||
assertTrue("testInheritCollectionDefaultPolicies 2", exists);
|
||||
@@ -542,12 +640,12 @@ public class BundleTest extends AbstractDSpaceObjectTest
|
||||
public void testReplaceAllBitstreamPolicies() throws SQLException, AuthorizeException
|
||||
{
|
||||
List<ResourcePolicy> newpolicies = new ArrayList<ResourcePolicy>();
|
||||
newpolicies.add(ResourcePolicy.create(context));
|
||||
newpolicies.add(ResourcePolicy.create(context));
|
||||
newpolicies.add(ResourcePolicy.create(context));
|
||||
b.replaceAllBitstreamPolicies(newpolicies);
|
||||
newpolicies.add(resourcePolicyService.create(context));
|
||||
newpolicies.add(resourcePolicyService.create(context));
|
||||
newpolicies.add(resourcePolicyService.create(context));
|
||||
bundleService.replaceAllBitstreamPolicies(context, b, newpolicies);
|
||||
|
||||
List<ResourcePolicy> bspolicies = b.getBundlePolicies();
|
||||
List<ResourcePolicy> bspolicies = bundleService.getBundlePolicies(context, b);
|
||||
assertTrue("testReplaceAllBitstreamPolicies 0", newpolicies.size() == bspolicies.size());
|
||||
|
||||
boolean equals = true;
|
||||
@@ -560,7 +658,7 @@ public class BundleTest extends AbstractDSpaceObjectTest
|
||||
}
|
||||
assertTrue("testReplaceAllBitstreamPolicies 1", equals);
|
||||
|
||||
bspolicies = b.getBitstreamPolicies();
|
||||
bspolicies = bundleService.getBitstreamPolicies(context, b);
|
||||
boolean exists = true;
|
||||
for(int i=0; bspolicies.size() > 0 && i < newpolicies.size() && exists; i++)
|
||||
{
|
||||
@@ -579,8 +677,8 @@ public class BundleTest extends AbstractDSpaceObjectTest
|
||||
public void testGetBundlePolicies() throws SQLException
|
||||
{
|
||||
//empty by default
|
||||
List<ResourcePolicy> bspolicies = b.getBundlePolicies();
|
||||
assertTrue("testGetBundlePolicies 0", bspolicies.isEmpty());
|
||||
List<ResourcePolicy> bspolicies = bundleService.getBundlePolicies(context, b);
|
||||
assertTrue("testGetBundlePolicies 0", CollectionUtils.isNotEmpty(bspolicies));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -590,7 +688,7 @@ public class BundleTest extends AbstractDSpaceObjectTest
|
||||
public void testGetBitstreamPolicies() throws SQLException
|
||||
{
|
||||
//empty by default
|
||||
List<ResourcePolicy> bspolicies = b.getBitstreamPolicies();
|
||||
List<ResourcePolicy> bspolicies = bundleService.getBitstreamPolicies(context, b);
|
||||
assertTrue("testGetBitstreamPolicies 0", bspolicies.isEmpty());
|
||||
}
|
||||
|
||||
@@ -602,8 +700,8 @@ public class BundleTest extends AbstractDSpaceObjectTest
|
||||
public void testGetAdminObject() throws SQLException
|
||||
{
|
||||
//default bundle has no admin object
|
||||
assertThat("testGetAdminObject 0", b.getAdminObject(Constants.REMOVE), nullValue());
|
||||
assertThat("testGetAdminObject 1", b.getAdminObject(Constants.ADD), nullValue());
|
||||
assertThat("testGetAdminObject 0", bundleService.getAdminObject(context, b, Constants.REMOVE), instanceOf(Item.class));
|
||||
assertThat("testGetAdminObject 1", bundleService.getAdminObject(context, b, Constants.ADD), instanceOf(Item.class));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -614,7 +712,8 @@ public class BundleTest extends AbstractDSpaceObjectTest
|
||||
public void testGetParentObject() throws SQLException
|
||||
{
|
||||
//default bundle has no parent
|
||||
assertThat("testGetParentObject 0", b.getParentObject(), nullValue());
|
||||
assertThat("testGetParentObject 0", bundleService.getParentObject(context, b), notNullValue());
|
||||
assertThat("testGetParentObject 0", bundleService.getParentObject(context, b), instanceOf(Item.class));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user