w2p-80200 Add new tests for each dspace object, cleaned up java docs and authrorization inside of create

This commit is contained in:
Nathan Buckingham
2021-08-30 14:38:25 -04:00
parent cdeb22ed7b
commit e1f798fd82
9 changed files with 95 additions and 9 deletions

View File

@@ -104,6 +104,13 @@ public class Collection extends DSpaceObject implements DSpaceObjectLegacySuppor
protected Collection() { protected Collection() {
} }
/**
* Takes a pre-determined UUID to be passed to the object to allow for the
* restoration of previously defined UUID's.
*
* @param uuid Takes a uuid to be passed to the Pre-Defined UUID Generator
*/
protected Collection(UUID uuid) { protected Collection(UUID uuid) {
this.predefinedUUID = uuid; this.predefinedUUID = uuid;
} }

View File

@@ -97,6 +97,12 @@ public class Community extends DSpaceObject implements DSpaceObjectLegacySupport
} }
/**
* Takes a pre-determined UUID to be passed to the object to allow for the
* restoration of previously defined UUID's.
*
* @param uuid Takes a uuid to be passed to the Pre-Defined UUID Generator
*/
protected Community(UUID uuid) { protected Community(UUID uuid) {
this.predefinedUUID = uuid; this.predefinedUUID = uuid;
} }

View File

@@ -38,8 +38,8 @@ import org.hibernate.annotations.GenericGenerator;
@Table(name = "dspaceobject") @Table(name = "dspaceobject")
public abstract class DSpaceObject implements Serializable, ReloadableEntity<java.util.UUID> { public abstract class DSpaceObject implements Serializable, ReloadableEntity<java.util.UUID> {
@Id @Id
@GeneratedValue(generator = "check-existing-uuid") @GeneratedValue(generator = "predefined-uuid")
@GenericGenerator(name = "check-existing-uuid", strategy = "org.dspace.content.CheckExistingUUIDGenerator") @GenericGenerator(name = "predefined-uuid", strategy = "org.dspace.content.PredefinedUUIDGenerator")
@Column(name = "uuid", unique = true, nullable = false, insertable = true, updatable = false) @Column(name = "uuid", unique = true, nullable = false, insertable = true, updatable = false)
protected java.util.UUID id; protected java.util.UUID id;
@@ -76,6 +76,9 @@ public abstract class DSpaceObject implements Serializable, ReloadableEntity<jav
@Transient @Transient
private boolean modified = false; private boolean modified = false;
/**
* This will read our predefinedUUID property to pass it along to the UUID generator
*/
@Transient @Transient
protected UUID predefinedUUID; protected UUID predefinedUUID;
public UUID getPredefinedUUID() { public UUID getPredefinedUUID() {

View File

@@ -132,6 +132,12 @@ public class Item extends DSpaceObject implements DSpaceObjectLegacySupport {
} }
/**
* Takes a pre-determined UUID to be passed to the object to allow for the
* restoration of previously defined UUID's.
*
* @param uuid Takes a uuid to be passed to the Pre-Defined UUID Generator
*/
protected Item(UUID uuid) { protected Item(UUID uuid) {
this.predefinedUUID = uuid; this.predefinedUUID = uuid;
} }

View File

@@ -176,6 +176,7 @@ public class ItemServiceImpl extends DSpaceObjectServiceImpl<Item> implements It
@Override @Override
public Item create(Context context, WorkspaceItem workspaceItem) throws SQLException, AuthorizeException { public Item create(Context context, WorkspaceItem workspaceItem) throws SQLException, AuthorizeException {
authorizeService.authorizeAction(context, workspaceItem.getCollection(), Constants.ADD);
if (workspaceItem.getItem() != null) { if (workspaceItem.getItem() != null) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Attempting to create an item for a workspace item that already contains an item"); "Attempting to create an item for a workspace item that already contains an item");
@@ -193,6 +194,7 @@ public class ItemServiceImpl extends DSpaceObjectServiceImpl<Item> implements It
@Override @Override
public Item create(Context context, WorkspaceItem workspaceItem, public Item create(Context context, WorkspaceItem workspaceItem,
UUID uuid) throws SQLException, AuthorizeException { UUID uuid) throws SQLException, AuthorizeException {
authorizeService.authorizeAction(context, workspaceItem.getCollection(), Constants.ADD);
if (workspaceItem.getItem() != null) { if (workspaceItem.getItem() != null) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Attempting to create an item for a workspace item that already contains an item"); "Attempting to create an item for a workspace item that already contains an item");

View File

@@ -18,7 +18,7 @@ import org.hibernate.id.UUIDGenerator;
* *
* @author April Herron * @author April Herron
*/ */
public class CheckExistingUUIDGenerator extends UUIDGenerator { public class PredefinedUUIDGenerator extends UUIDGenerator {
@Override @Override
public Serializable generate(SharedSessionContractImplementor session, Object object) { public Serializable generate(SharedSessionContractImplementor session, Object object) {

View File

@@ -43,9 +43,8 @@ public interface ItemService
public Thumbnail getThumbnail(Context context, Item item, boolean requireOriginal) throws SQLException; public Thumbnail getThumbnail(Context context, Item item, boolean requireOriginal) throws SQLException;
/** /**
* Create a new item, with a new internal ID. This method is not public, * Create a new item, with a new internal ID. Authorization is done
* since items need to be created as workspace items. Authorisation is the * inside of this method.
* responsibility of the caller.
* *
* @param context DSpace context object * @param context DSpace context object
* @param workspaceItem in progress workspace item * @param workspaceItem in progress workspace item
@@ -56,9 +55,8 @@ public interface ItemService
public Item create(Context context, WorkspaceItem workspaceItem) throws SQLException, AuthorizeException; public Item create(Context context, WorkspaceItem workspaceItem) throws SQLException, AuthorizeException;
/** /**
* Create a new item, with a provided ID. This method is not public, * Create a new item, with a provided ID. Authorisation is done
* since items need to be created as workspace items. Authorisation is the * inside of this method.
* responsibility of the caller.
* *
* @param context DSpace context object * @param context DSpace context object
* @param workspaceItem in progress workspace item * @param workspaceItem in progress workspace item

View File

@@ -13,10 +13,12 @@ import static org.junit.Assert.assertTrue;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Iterator;
import java.util.UUID; import java.util.UUID;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;
import com.google.common.collect.Iterators;
import org.dspace.AbstractIntegrationTestWithDatabase; import org.dspace.AbstractIntegrationTestWithDatabase;
import org.dspace.builder.CollectionBuilder; import org.dspace.builder.CollectionBuilder;
import org.dspace.builder.CommunityBuilder; import org.dspace.builder.CommunityBuilder;
@@ -27,13 +29,20 @@ import org.dspace.content.Item;
import org.dspace.content.crosswalk.MetadataValidationException; import org.dspace.content.crosswalk.MetadataValidationException;
import org.dspace.content.factory.ContentServiceFactory; import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.packager.METSManifest; import org.dspace.content.packager.METSManifest;
import org.dspace.content.service.CollectionService;
import org.dspace.content.service.CommunityService;
import org.dspace.content.service.ItemService; import org.dspace.content.service.ItemService;
import org.dspace.services.ConfigurationService;
import org.dspace.services.factory.DSpaceServicesFactory;
import org.jdom.Element; import org.jdom.Element;
import org.junit.Test; import org.junit.Test;
public class PackagerIT extends AbstractIntegrationTestWithDatabase { public class PackagerIT extends AbstractIntegrationTestWithDatabase {
private ItemService itemService = ContentServiceFactory.getInstance().getItemService(); private ItemService itemService = ContentServiceFactory.getInstance().getItemService();
private CollectionService collectionService = ContentServiceFactory.getInstance().getCollectionService();
private CommunityService communityService = ContentServiceFactory.getInstance().getCommunityService();
protected ConfigurationService configService = DSpaceServicesFactory.getInstance().getConfigurationService();
protected Community child1; protected Community child1;
protected Collection col1; protected Collection col1;
protected Item article; protected Item article;
@@ -60,6 +69,7 @@ public class PackagerIT extends AbstractIntegrationTestWithDatabase {
createTemplate(); createTemplate();
createFiles(); createFiles();
try { try {
//Item
performExportScript(article.getHandle(), tempFile); performExportScript(article.getHandle(), tempFile);
String idStr = getID(); String idStr = getID();
itemService.delete(context, article); itemService.delete(context, article);
@@ -71,6 +81,59 @@ public class PackagerIT extends AbstractIntegrationTestWithDatabase {
} }
} }
@Test
public void packagerImportColUUIDTest() throws Exception {
context.turnOffAuthorisationSystem();
createTemplate();
createFiles();
configService.setProperty("upload.temp.dir",tempFile.getParent());
try {
performExportScript(col1.getHandle(), tempFile);
String idStr = getID();
collectionService.delete(context, col1);
performImportScript(tempFile);
Collection collection = collectionService.find(context, UUID.fromString(idStr));
assertNotNull(collection);
} finally {
tempFile.delete();
}
}
@Test
public void packagerImportComUUIDTest() throws Exception {
context.turnOffAuthorisationSystem();
createTemplate();
createFiles();
configService.setProperty("upload.temp.dir",tempFile.getParent());
try {
//Community
performExportScript(child1.getHandle(), tempFile);
String idStr = getID();
communityService.delete(context, child1);
performImportScript(tempFile);
Community community = communityService.find(context, UUID.fromString(idStr));
assertNotNull(community);
} finally {
tempFile.delete();
}
}
@Test
public void packagerUUIDAlreadyExistTest() throws Exception {
context.turnOffAuthorisationSystem();
createTemplate();
createFiles();
try {
//Item should be overwritten if UUID already Exists
performExportScript(article.getHandle(), tempFile);
performImportScript(tempFile);
Iterator<Item> items = itemService.findByCollection(context, col1);
assertEquals(1, Iterators.size(items));
} finally {
tempFile.delete();
}
}
protected void createTemplate() { protected void createTemplate() {
parentCommunity = CommunityBuilder.createCommunity(context) parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community") .withName("Parent Community")

Submodule dspace/modules/dspace-replicate added at 5c19e3e515