Skip to content

Commit

Permalink
[Task 64911] added RelationshipMetadataService test
Browse files Browse the repository at this point in the history
  • Loading branch information
Raf-atmire committed Sep 17, 2019
1 parent 46e8632 commit 2de90e0
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import org.dspace.content.DSpaceObject;
import org.dspace.content.InProgressSubmission;
import org.dspace.content.RelationshipMetadataService;
import org.dspace.content.WorkspaceItem;
import org.dspace.content.service.BitstreamFormatService;
import org.dspace.content.service.BitstreamService;
Expand Down Expand Up @@ -112,6 +113,8 @@ public abstract class ContentServiceFactory {
*/
public abstract EntityService getEntityService();

public abstract RelationshipMetadataService getRelationshipMetadataService();

public InProgressSubmissionService getInProgressSubmissionService(InProgressSubmission inProgressSubmission) {
if (inProgressSubmission instanceof WorkspaceItem) {
return getWorkspaceItemService();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.List;

import org.dspace.content.DSpaceObject;
import org.dspace.content.RelationshipMetadataService;
import org.dspace.content.service.BitstreamFormatService;
import org.dspace.content.service.BitstreamService;
import org.dspace.content.service.BundleService;
Expand Down Expand Up @@ -78,6 +79,8 @@ public class ContentServiceFactoryImpl extends ContentServiceFactory {
@Autowired(required = true)
private RelationshipTypeService relationshipTypeService;
@Autowired(required = true)
private RelationshipMetadataService relationshipMetadataService;
@Autowired(required = true)
private EntityTypeService entityTypeService;
@Autowired(required = true)
private EntityService entityService;
Expand Down Expand Up @@ -182,4 +185,8 @@ public EntityService getEntityService() {
return entityService;
}

@Override
public RelationshipMetadataService getRelationshipMetadataService() {
return relationshipMetadataService;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,134 @@
*/
package org.dspace.content;

import static org.mockito.Mockito.when;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

import java.util.LinkedList;
import java.sql.SQLException;
import java.util.List;

import org.apache.logging.log4j.Logger;
import org.dspace.AbstractUnitTest;
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.CollectionService;
import org.dspace.content.service.CommunityService;
import org.dspace.content.service.EntityService;
import org.dspace.content.service.EntityTypeService;
import org.dspace.content.service.InstallItemService;
import org.dspace.content.service.ItemService;
import org.dspace.content.service.RelationshipService;
import org.dspace.core.Context;
import org.dspace.content.service.RelationshipTypeService;
import org.dspace.content.service.WorkspaceItemService;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class RelationshipMetadataServiceTest {
public class RelationshipMetadataServiceTest extends AbstractUnitTest {

@InjectMocks
private RelationshipMetadataServiceImpl relationshipMetadataService;
private static final Logger log = org.apache.logging.log4j.LogManager
.getLogger(RelationshipMetadataServiceTest.class);

@Mock
private RelationshipService relationshipService;
protected RelationshipMetadataService relationshipMetadataService = ContentServiceFactory
.getInstance().getRelationshipMetadataService();
protected RelationshipService relationshipService = ContentServiceFactory.getInstance().getRelationshipService();
protected RelationshipTypeService relationshipTypeService = ContentServiceFactory.getInstance()
.getRelationshipTypeService();
protected EntityService entityService = ContentServiceFactory.getInstance().getEntityService();
protected EntityTypeService entityTypeService = ContentServiceFactory.getInstance().getEntityTypeService();
protected CommunityService communityService = ContentServiceFactory.getInstance().getCommunityService();
protected CollectionService collectionService = ContentServiceFactory.getInstance().getCollectionService();
protected ItemService itemService = ContentServiceFactory.getInstance().getItemService();
protected InstallItemService installItemService = ContentServiceFactory.getInstance().getInstallItemService();
protected WorkspaceItemService workspaceItemService = ContentServiceFactory.getInstance().getWorkspaceItemService();

@Mock
private Item item;
private Relationship firstRelationship;
private Relationship secondRelationship;
private LinkedList<Relationship> list;
private Context context;
Item item;
Item authorItem;

/**
* This method will be run before every test as per @Before. It will
* initialize resources required for the tests.
*
* Other methods can be annotated with @Before here or in subclasses
* but no execution order is guaranteed
*/
@Before
@Override
public void init() {
firstRelationship = new Relationship();
secondRelationship = new Relationship();
list = new LinkedList<>();
list.add(firstRelationship);
list.add(secondRelationship);
super.init();
try {
context.turnOffAuthorisationSystem();
Community community = communityService.create(null, context);

Collection col = collectionService.create(context, community);
WorkspaceItem is = workspaceItemService.create(context, col, false);
WorkspaceItem authorIs = workspaceItemService.create(context, col, false);

item = installItemService.installItem(context, is);
authorItem = installItemService.installItem(context, authorIs);
context.restoreAuthSystemState();
} catch (AuthorizeException ex) {
log.error("Authorization Error in init", ex);
fail("Authorization Error in init: " + ex.getMessage());
} catch (SQLException ex) {
log.error("SQL Error in init", ex);
fail("SQL Error in init: " + ex.getMessage());
}

}

/**
* This method will be run after every test as per @After. It will
* clean resources initialized by the @Before methods.
*
* Other methods can be annotated with @After here or in subclasses
* but no execution order is guaranteed
*/
@After
@Override
public void destroy() {
context.abort();
super.destroy();
}


@Test
@Ignore
public void testGetRelationshipMetadata() throws Exception {
when(item.getMetadata()).thenReturn(new LinkedList<>());
when(relationshipService.findByItem(context, item)).thenReturn(list);
context.turnOffAuthorisationSystem();
itemService.addMetadata(context, item, "relationship", "type", null, null, "Publication");
itemService.addMetadata(context, authorItem, "relationship", "type", null, null, "Author");
itemService.addMetadata(context, authorItem, "person", "familyName", null, null, "familyName");
itemService.addMetadata(context, authorItem, "person", "givenName", null, null, "firstName");
EntityType publicationEntityType = entityTypeService.create(context, "Publication");
EntityType authorEntityType = entityTypeService.create(context, "Author");
RelationshipType isAuthorOfPublication = relationshipTypeService
.create(context, publicationEntityType, authorEntityType, "isAuthorOfPublication", "isPublicationOfAuthor",
null, null, null, null);

Relationship relationship = relationshipService.create(context, item, authorItem, isAuthorOfPublication, 0, 0);
List<MetadataValue> authorList = itemService.getMetadata(item, "dc", "contributor", "author", Item.ANY);
assertThat(authorList.size(), equalTo(1));
assertThat(authorList.get(0).getValue(), equalTo("familyName, firstName"));

List<MetadataValue> relationshipMetadataList = itemService
.getMetadata(item, "relation", "isAuthorOfPublication", null, Item.ANY);
assertThat(relationshipMetadataList.size(), equalTo(1));
assertThat(relationshipMetadataList.get(0).getValue(), equalTo(String.valueOf(authorItem.getID())));

String t = "";
relationshipMetadataService.getRelationshipMetadata(item, true);
List<RelationshipMetadataValue> list = relationshipMetadataService.getRelationshipMetadata(item, true);
assertThat(list.size(), equalTo(2));
assertThat(list.get(0).getValue(), equalTo("familyName, firstName"));
assertThat(list.get(0).getMetadataField().getMetadataSchema().getName(), equalTo("dc"));
assertThat(list.get(0).getMetadataField().getElement(), equalTo("contributor"));
assertThat(list.get(0).getMetadataField().getQualifier(), equalTo("author"));
assertThat(list.get(0).getAuthority(), equalTo("virtual::" + relationship.getID()));

assertThat(list.get(1).getValue(), equalTo(String.valueOf(authorItem.getID())));
assertThat(list.get(1).getMetadataField().getMetadataSchema().getName(), equalTo("relation"));
assertThat(list.get(1).getMetadataField().getElement(), equalTo("isAuthorOfPublication"));
assertThat(list.get(1).getAuthority(), equalTo("virtual::" + relationship.getID()));

context.restoreAuthSystemState();
}
}

0 comments on commit 2de90e0

Please sign in to comment.