DS-3127: Process review feedback and fix tests

This commit is contained in:
Tom Desair
2017-06-22 15:01:45 +02:00
parent 0f51d5ad6a
commit 71791c720f
6 changed files with 66 additions and 42 deletions

View File

@@ -254,15 +254,15 @@ public class BitstreamServiceImpl extends DSpaceObjectServiceImpl<Bitstream> imp
bundle.getBitstreams().remove(bitstream); bundle.getBitstreams().remove(bitstream);
} }
//Remove all bundles from the bitstream object, clearing the connection in 2 ways
bundles.clear();
// Remove bitstream itself // Remove bitstream itself
bitstream.setDeleted(true); bitstream.setDeleted(true);
update(context, bitstream); update(context, bitstream);
// Remove policies only after the bitstream has been updated (otherwise the current user has not WRITE rights) // Remove policies only after the bitstream has been updated (otherwise the current user has not WRITE rights)
authorizeService.removeAllPolicies(context, bitstream); authorizeService.removeAllPolicies(context, bitstream);
//Remove all bundles from the bitstream object, clearing the connection in 2 ways
bundles.clear();
} }
@Override @Override

View File

@@ -399,16 +399,15 @@ public class BundleServiceImpl extends DSpaceObjectServiceImpl<Bundle> implement
bundle.getName(), getIdentifiers(context, bundle))); bundle.getName(), getIdentifiers(context, bundle)));
// Remove bitstreams // Remove bitstreams
Iterator<Bitstream> bitstreams = bundle.getBitstreams().iterator(); List<Bitstream> bitstreams = new LinkedList<>(bundle.getBitstreams());
while (bitstreams.hasNext()) { bundle.getBitstreams().clear();
Bitstream bitstream = bitstreams.next(); for (Bitstream bitstream : bitstreams) {
bitstreams.remove();
removeBitstream(context, bundle, bitstream); removeBitstream(context, bundle, bitstream);
} }
Iterator<Item> items = bundle.getItems().iterator(); List<Item> items = new LinkedList<>(bundle.getItems());
while (items.hasNext()) { bundle.getItems().clear();
Item item = items.next(); for (Item item : items) {
item.removeBundle(bundle); item.removeBundle(bundle);
} }

View File

@@ -16,16 +16,17 @@ import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import java.util.*; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import static org.junit.Assert.*; import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock; import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static org.mockito.Matchers.*;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
@@ -61,19 +62,25 @@ public class GoogleBitstreamComparatorTest extends AbstractUnitTest{
* @throws Exception * @throws Exception
*/ */
@Before @Before
public void setUp() throws Exception { @Override
public void init() {
super.init();
when(bitstream1.getName()).thenReturn("bitstream1"); when(bitstream1.getName()).thenReturn("bitstream1");
when(bitstream2.getName()).thenReturn("bitstream2"); when(bitstream2.getName()).thenReturn("bitstream2");
when(bitstream3.getName()).thenReturn("bitstream3"); when(bitstream3.getName()).thenReturn("bitstream3");
settings.put("citation.prioritized_types", "Adobe PDF, Microsoft Word, RTF, Photoshop"); settings.put("citation.prioritized_types", "Adobe PDF, Microsoft Word, RTF, Postscript");
List<Bitstream> bitstreams = new ArrayList<>(); List<Bitstream> bitstreams = new ArrayList<>();
bitstreams.add(bitstream1); bitstreams.add(bitstream1);
bitstreams.add(bitstream2); bitstreams.add(bitstream2);
bitstreams.add(bitstream3); bitstreams.add(bitstream3);
when(bundle.getBitstreams()).thenReturn(bitstreams); when(bundle.getBitstreams()).thenReturn(bitstreams);
when(bitstream1.getFormat(any(Context.class))).thenReturn(bitstreamFormat1); try {
when(bitstream2.getFormat(any(Context.class))).thenReturn(bitstreamFormat2); when(bitstream1.getFormat(any(Context.class))).thenReturn(bitstreamFormat1);
when(bitstream3.getFormat(any(Context.class))).thenReturn(bitstreamFormat3); when(bitstream2.getFormat(any(Context.class))).thenReturn(bitstreamFormat2);
when(bitstream3.getFormat(any(Context.class))).thenReturn(bitstreamFormat3);
} catch (Exception ex) {
//will not happen
}
} }
/** /**
@@ -104,7 +111,7 @@ public class GoogleBitstreamComparatorTest extends AbstractUnitTest{
public void testDifferentMimeTypes() throws Exception { public void testDifferentMimeTypes() throws Exception {
when(bitstreamFormat1.getMIMEType()).thenReturn("text/richtext"); when(bitstreamFormat1.getMIMEType()).thenReturn("text/richtext");
when(bitstreamFormat2.getMIMEType()).thenReturn("application/msword"); when(bitstreamFormat2.getMIMEType()).thenReturn("application/msword");
when(bitstreamFormat3.getMIMEType()).thenReturn("application/x-photoshop"); when(bitstreamFormat3.getMIMEType()).thenReturn("application/postscript");
List<Bitstream> toSort = bundle.getBitstreams(); List<Bitstream> toSort = bundle.getBitstreams();
Collections.sort(toSort, new GoogleBitstreamComparator(context, settings)); Collections.sort(toSort, new GoogleBitstreamComparator(context, settings));
@@ -122,7 +129,7 @@ public class GoogleBitstreamComparatorTest extends AbstractUnitTest{
public void testMimeTypesDifferentSizes() throws Exception { public void testMimeTypesDifferentSizes() throws Exception {
when(bitstreamFormat1.getMIMEType()).thenReturn("text/richtext"); when(bitstreamFormat1.getMIMEType()).thenReturn("text/richtext");
when(bitstreamFormat2.getMIMEType()).thenReturn("text/richtext"); when(bitstreamFormat2.getMIMEType()).thenReturn("text/richtext");
when(bitstreamFormat3.getMIMEType()).thenReturn("application/x-photoshop"); when(bitstreamFormat3.getMIMEType()).thenReturn("application/postscript");
when(bitstream1.getSize()).thenReturn(new Long(100)); when(bitstream1.getSize()).thenReturn(new Long(100));
when(bitstream2.getSize()).thenReturn(new Long(200)); when(bitstream2.getSize()).thenReturn(new Long(200));
when(bitstream3.getSize()).thenReturn(new Long(300)); when(bitstream3.getSize()).thenReturn(new Long(300));
@@ -199,10 +206,10 @@ public class GoogleBitstreamComparatorTest extends AbstractUnitTest{
*/ */
@Test @Test
public void testChangePriority() throws Exception{ public void testChangePriority() throws Exception{
settings.put("citation.prioritized_types", "Photoshop, RTF, Microsoft Word, Adobe PDF"); settings.put("citation.prioritized_types", "Postscript, RTF, Microsoft Word, Adobe PDF");
when(bitstreamFormat1.getMIMEType()).thenReturn("text/richtext"); when(bitstreamFormat1.getMIMEType()).thenReturn("text/richtext");
when(bitstreamFormat2.getMIMEType()).thenReturn("application/msword"); when(bitstreamFormat2.getMIMEType()).thenReturn("application/msword");
when(bitstreamFormat3.getMIMEType()).thenReturn("application/x-photoshop"); when(bitstreamFormat3.getMIMEType()).thenReturn("application/postscript");
List<Bitstream> toSort = bundle.getBitstreams(); List<Bitstream> toSort = bundle.getBitstreams();
Collections.sort(toSort, new GoogleBitstreamComparator(context, settings)); Collections.sort(toSort, new GoogleBitstreamComparator(context, settings));
@@ -264,7 +271,7 @@ public class GoogleBitstreamComparatorTest extends AbstractUnitTest{
settings.put("citation.prioritized_types", ""); settings.put("citation.prioritized_types", "");
when(bitstreamFormat1.getMIMEType()).thenReturn("text/richtext"); when(bitstreamFormat1.getMIMEType()).thenReturn("text/richtext");
when(bitstreamFormat2.getMIMEType()).thenReturn("application/msword"); when(bitstreamFormat2.getMIMEType()).thenReturn("application/msword");
when(bitstreamFormat3.getMIMEType()).thenReturn("application/x-photoshop"); when(bitstreamFormat3.getMIMEType()).thenReturn("application/postscript");
when(bitstream1.getSize()).thenReturn(new Long(100)); when(bitstream1.getSize()).thenReturn(new Long(100));
when(bitstream2.getSize()).thenReturn(new Long(200)); when(bitstream2.getSize()).thenReturn(new Long(200));
when(bitstream3.getSize()).thenReturn(new Long(300)); when(bitstream3.getSize()).thenReturn(new Long(300));
@@ -284,7 +291,7 @@ public class GoogleBitstreamComparatorTest extends AbstractUnitTest{
settings.remove("citation.prioritized_types"); settings.remove("citation.prioritized_types");
when(bitstreamFormat1.getMIMEType()).thenReturn("text/richtext"); when(bitstreamFormat1.getMIMEType()).thenReturn("text/richtext");
when(bitstreamFormat2.getMIMEType()).thenReturn("application/msword"); when(bitstreamFormat2.getMIMEType()).thenReturn("application/msword");
when(bitstreamFormat3.getMIMEType()).thenReturn("application/x-photoshop"); when(bitstreamFormat3.getMIMEType()).thenReturn("application/postscript");
when(bitstream1.getSize()).thenReturn(new Long(100)); when(bitstream1.getSize()).thenReturn(new Long(100));
when(bitstream2.getSize()).thenReturn(new Long(200)); when(bitstream2.getSize()).thenReturn(new Long(200));
when(bitstream3.getSize()).thenReturn(new Long(300)); when(bitstream3.getSize()).thenReturn(new Long(300));
@@ -301,10 +308,10 @@ public class GoogleBitstreamComparatorTest extends AbstractUnitTest{
*/ */
@Test @Test
public void testUndefinedShortDescription() { public void testUndefinedShortDescription() {
settings.put("citation.prioritized_types", "Photoshop, RTF, Undefined Type, Microsoft Word, Adobe PDF"); settings.put("citation.prioritized_types", "Postscript, RTF, Undefined Type, Microsoft Word, Adobe PDF");
when(bitstreamFormat1.getMIMEType()).thenReturn("text/richtext"); when(bitstreamFormat1.getMIMEType()).thenReturn("text/richtext");
when(bitstreamFormat2.getMIMEType()).thenReturn("application/msword"); when(bitstreamFormat2.getMIMEType()).thenReturn("application/msword");
when(bitstreamFormat3.getMIMEType()).thenReturn("application/x-photoshop"); when(bitstreamFormat3.getMIMEType()).thenReturn("application/postscript");
when(bitstream1.getSize()).thenReturn(new Long(100)); when(bitstream1.getSize()).thenReturn(new Long(100));
when(bitstream2.getSize()).thenReturn(new Long(200)); when(bitstream2.getSize()).thenReturn(new Long(200));
when(bitstream3.getSize()).thenReturn(new Long(300)); when(bitstream3.getSize()).thenReturn(new Long(300));
@@ -321,7 +328,7 @@ public class GoogleBitstreamComparatorTest extends AbstractUnitTest{
*/ */
@Test @Test
public void testAddingNewFormat() { public void testAddingNewFormat() {
settings.put("citation.prioritized_types", "WAV, Adobe PDF, Microsoft Word, RTF, Photoshop"); settings.put("citation.prioritized_types", "WAV, Adobe PDF, Microsoft Word, RTF, Postscript");
when(bitstreamFormat1.getMIMEType()).thenReturn("text/richtext"); when(bitstreamFormat1.getMIMEType()).thenReturn("text/richtext");
when(bitstreamFormat2.getMIMEType()).thenReturn("application/msword"); when(bitstreamFormat2.getMIMEType()).thenReturn("application/msword");
when(bitstreamFormat3.getMIMEType()).thenReturn("audio/x-wav"); when(bitstreamFormat3.getMIMEType()).thenReturn("audio/x-wav");
@@ -341,6 +348,7 @@ public class GoogleBitstreamComparatorTest extends AbstractUnitTest{
public void destroy() public void destroy()
{ {
settings = null; settings = null;
super.destroy();
} }

View File

@@ -10,29 +10,22 @@ package org.dspace.app.util;
import org.apache.commons.io.Charsets; import org.apache.commons.io.Charsets;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.dspace.AbstractUnitTest; import org.dspace.AbstractUnitTest;
import org.dspace.app.util.GoogleMetadata;
import org.dspace.authorize.AuthorizeException; import org.dspace.authorize.AuthorizeException;
import org.dspace.content.*; import org.dspace.content.*;
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.content.service.BitstreamService; import org.dspace.content.service.BitstreamService;
import org.dspace.content.service.BundleService; import org.dspace.content.service.BundleService;
import org.dspace.content.service.CollectionService;
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.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import sun.net.www.content.text.PlainTextInputStream;
import java.io.*; import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.HashMap;
import static org.junit.Assert.*; import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when; import static org.junit.Assert.fail;
public class GoogleMetadataTest extends AbstractUnitTest { public class GoogleMetadataTest extends AbstractUnitTest {
@@ -50,6 +43,8 @@ public class GoogleMetadataTest extends AbstractUnitTest {
private BitstreamService bitstreamService; private BitstreamService bitstreamService;
private Community community;
/** /**
* 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.
@@ -64,7 +59,7 @@ public class GoogleMetadataTest extends AbstractUnitTest {
try try
{ {
context.turnOffAuthorisationSystem(); context.turnOffAuthorisationSystem();
Community community = ContentServiceFactory.getInstance().getCommunityService().create(null, context); community = ContentServiceFactory.getInstance().getCommunityService().create(null, context);
Collection collection = ContentServiceFactory.getInstance().getCollectionService().create(context, community); Collection collection = ContentServiceFactory.getInstance().getCollectionService().create(context, community);
WorkspaceItem wi = ContentServiceFactory.getInstance().getWorkspaceItemService().create(context, collection, true); WorkspaceItem wi = ContentServiceFactory.getInstance().getWorkspaceItemService().create(context, collection, true);
Item item = wi.getItem(); Item item = wi.getItem();
@@ -300,6 +295,20 @@ public class GoogleMetadataTest extends AbstractUnitTest {
@Override @Override
public void destroy() public void destroy()
{ {
try {
context.turnOffAuthorisationSystem();
//Context might have been committed in the test method, so best to reload to entity so we're sure that it is attached.
community = context.reloadEntity(community);
ContentServiceFactory.getInstance().getCommunityService().delete(context, community);
community = null;
} catch (SQLException e) {
e.printStackTrace();
} catch (AuthorizeException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
it = null; it = null;
super.destroy(); super.destroy();
} }

View File

@@ -75,7 +75,7 @@ google.citation_technical_report_institution = dc.publisher
#priority whitelist for citation_pdf_url, shortnames are defined in dspace/config/registries/bitstream-formats.xml #priority whitelist for citation_pdf_url, shortnames are defined in dspace/config/registries/bitstream-formats.xml
#priority order is defined here, where the first type is the most important #priority order is defined here, where the first type is the most important
google.citation.prioritized_types = Adobe PDF, Microsoft Word, Microsoft Word XML, RTF, Photoshop, EPUB google.citation.prioritized_types = Adobe PDF, Postscript, Microsoft Word XML, Microsoft Word, RTF, EPUB

View File

@@ -733,5 +733,13 @@
<extension>rdf</extension> <extension>rdf</extension>
</bitstream-type> </bitstream-type>
<bitstream-type>
<mimetype>application/epub+zip</mimetype>
<short_description>EPUB</short_description>
<description>Electronic publishing</description>
<support_level>1</support_level>
<internal>false</internal>
<extension>epub</extension>
</bitstream-type>
</dspace-bitstream-types> </dspace-bitstream-types>