Skip to content

Commit

Permalink
Add integration test. Refactor for testability. DSpace#8234
Browse files Browse the repository at this point in the history
  • Loading branch information
mwoodiupui committed Apr 6, 2022
1 parent e475b5a commit 3fe6c31
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,17 @@
*/
@Named
public class CSLBibliographyGenerator {
@Inject
private MetadataSchemaService metadataSchemaService;

@Inject
private MetadataFieldService metadataFieldService;

@Inject
private ItemService itemService;

/** Map DSpace item types to CSL types. */
private final Map<String, String> documentTypes;
private final Map<String, String> documentTypeMap;

/** Map DSpace metadata fields to CSL fields. */
private final Map<String, String> metadataFields;
private final Map<String, String> metadataFieldMap;

/** CSL type of any work with an unmapped DSpace type. */
private final String defaultType;
Expand Down Expand Up @@ -95,14 +92,40 @@ public class CSLBibliographyGenerator {
public CSLBibliographyGenerator(Map<String, String> documentTypes,
Map<String, String> metadataFields,
String defaultType) {
this.documentTypes = documentTypes;
this.metadataFields = metadataFields;
this.documentTypeMap = documentTypes;
this.metadataFieldMap = metadataFields;
this.defaultType = defaultType.toUpperCase();
}

/**
* @param metadataSchemaService the metadataSchemaService to set
*/
@Inject
public void setMetadataSchemaService(
MetadataSchemaService metadataSchemaService) {
this.metadataSchemaService = metadataSchemaService;
}

/**
* @param metadataFieldService the metadataFieldService to set
*/
@Inject
public void setMetadataFieldService(
MetadataFieldService metadataFieldService) {
this.metadataFieldService = metadataFieldService;
}

/**
* @param itemService the itemService to set
*/
@Inject
public void setItemService(ItemService itemService) {
this.itemService = itemService;
}

/** Do not use. */
private CSLBibliographyGenerator() {
documentTypes = metadataFields = null;
documentTypeMap = metadataFieldMap = null;
defaultType = null;
}

Expand Down Expand Up @@ -203,15 +226,15 @@ private CSLItemData buildItem(Context context, Item item) {
// Fill the holder with field values.
for (MetadataSchema schema : metadataSchemaService.findAll(context)) {
String schemaName = schema.getName();
for (MetadataField field : metadataFieldService.findAllInSchema(context,
schema)) {
for (MetadataField field :
metadataFieldService.findAllInSchema(context, schema)) {
// What field is this?
String fieldElement = field.getElement();
String fieldQualifier = field.getQualifier();
String dsFieldName = mdFieldName(schemaName, fieldElement, fieldQualifier);

// Map to CSL field
String cslFieldName = metadataFields.get(dsFieldName);
String cslFieldName = metadataFieldMap.get(dsFieldName);
LOG.debug("Map DSpace {} to CSL {}", dsFieldName, cslFieldName);

// Skip this field if not mapped.
Expand All @@ -230,8 +253,8 @@ private CSLItemData buildItem(Context context, Item item) {
// Figure out the CSL work type.
if ("type".equals(cslFieldName)) {
String key = value.replaceAll(" ", "_");
if (documentTypes.containsKey(key)) {
value = documentTypes.get(key)
if (documentTypeMap.containsKey(key)) {
value = documentTypeMap.get(key)
.replaceAll("[ -]", "_")
.toUpperCase();
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.disseminate;

import static org.junit.Assert.assertTrue;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.dspace.AbstractIntegrationTestWithDatabase;
import org.dspace.builder.CollectionBuilder;
import org.dspace.builder.CommunityBuilder;
import org.dspace.builder.ItemBuilder;
import org.dspace.content.Collection;
import org.dspace.content.Item;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.ItemService;
import org.dspace.content.service.MetadataFieldService;
import org.dspace.content.service.MetadataSchemaService;
import org.dspace.kernel.ServiceManager;
import org.dspace.services.factory.DSpaceServicesFactory;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

/**
*
* @author Mark H. Wood <[email protected]>
*/
public class CSLBibliographyGeneratorIT
extends AbstractIntegrationTestWithDatabase {
private static Map<String, String> fields;
private static Map<String, String> types;

private static MetadataSchemaService metadataSchemaService;
private static MetadataFieldService metadataFieldService;
private static ItemService itemService;

@BeforeClass
public static void setup() {
ServiceManager serviceManager = DSpaceServicesFactory.getInstance().getServiceManager();
types = serviceManager.getServiceByName("csl-types", HashMap.class);
fields = serviceManager.getServiceByName("csl-fields", HashMap.class);

ContentServiceFactory contentServiceFactory = ContentServiceFactory.getInstance();
metadataSchemaService = contentServiceFactory.getMetadataSchemaService();
metadataFieldService = contentServiceFactory.getMetadataFieldService();
itemService = contentServiceFactory.getItemService();
}

/**
* Test of addWork method, of class CSLBibliographyGenerator.
* Also test render(). There is no good way to test them separately.
* @throws java.lang.Exception passed through.
*/
@Test
public void testAddWork()
throws Exception {
System.out.println("addWork");

// Build an Item
context.turnOffAuthorisationSystem();
parentCommunity = CommunityBuilder
.createCommunity(context)
.withName("Top Community")
.build();
Collection collection = CollectionBuilder
.createCollection(context, parentCommunity)
.withName("My Collection")
.build();
Item item1 = ItemBuilder.createItem(context, collection)
.withMetadata("dc", "title", null,
"The Endochronic Properties of Resublimated Thiotimoline")
.withAuthor("Asimov, Isaac")
.withIssueDate("1948-03")
.withSubject("physical chemistry")
.build();
context.restoreAuthSystemState();

// Instantiate the Unit Under Test
CSLBibliographyGenerator instance
= new CSLBibliographyGenerator(types, fields, "article");
instance.setMetadataSchemaService(metadataSchemaService);
instance.setMetadataFieldService(metadataFieldService);
instance.setItemService(itemService);

// Test!
instance.addWork(context, item1);

String style = "bibtex";
CSLBibliographyGenerator.OutputFormat outputFormat
= CSLBibliographyGenerator.OutputFormat.TEXT;
String rendered = instance.render(style, outputFormat);
System.out.println("BEGIN RENDERED");
System.out.println(rendered);
System.out.println("END RENDERED");
assertTrue("Bibliography should mention 'thiotimoline'",
rendered.contains("thiotimoline"));
}

/**
* Test of render method, of class CSLBibliographyGenerator.
* NOTE: there is no good way to test render() separately.
* See {@link #testAddWork}.
*
*/
@Ignore
@Test
public void testRender() {
}

/**
* Test of getStyles method, of class CSLBibliographyGenerator.
* @throws java.lang.Exception passed through.
*/
@Test
public void testGetStyles()
throws Exception {
System.out.println("getStyles");
Set<String> result = CSLBibliographyGenerator.getStyles();
assertTrue("Styles should include 'bibtex'", result.contains("bibtex"));
}
}

0 comments on commit 3fe6c31

Please sign in to comment.