Merge pull request #2446 from atmire/DS-4244_Entities-unit-tests

DS-4244 Add configurable entities unit tests
This commit is contained in:
Tim Donohue
2019-07-10 23:13:20 +02:00
committed by GitHub
10 changed files with 1546 additions and 0 deletions

View File

@@ -0,0 +1,277 @@
/**
* 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.content;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import org.dspace.content.dao.RelationshipTypeDAO;
import org.dspace.content.service.EntityTypeService;
import org.dspace.content.service.ItemService;
import org.dspace.content.service.RelationshipService;
import org.dspace.content.service.RelationshipTypeService;
import org.dspace.core.Context;
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 EntityServiceImplTest {
@InjectMocks
private EntityServiceImpl entityService;
@Mock
private Context context;
@Mock
private ItemService itemService;
@Mock
private RelationshipService relationshipService;
@Mock
private RelationshipTypeService relationshipTypeService;
@Mock
private EntityTypeService entityTypeService;
@Mock
private EntityType leftType;
@Mock
private EntityType rightType;
@Test
public void testfindByItemId() throws Exception {
// Declare objects utilized in unit test
Item item = mock(Item.class);
List<Relationship> relationshipList = new ArrayList<>();
Relationship relationship = mock(Relationship.class);
relationship.setId(9);
relationshipList.add(relationship);
// Mock the state of objects utilized in findByItemId() to meet the success criteria of an invocation
when(itemService.find(any(), any())).thenReturn(item);
when(item.getName()).thenReturn("ItemName");
when(relationshipService.findByItem(any(), any())).thenReturn(relationshipList);
// The returned Entity's item should match the mocked item's name
assertEquals("TestFindByItem 0", "ItemName",
entityService.findByItemId(context, item.getID()).getItem().getName());
}
@Test
public void testGetType() throws Exception {
// Declare objects utilized in unit test
Entity entity = mock(Entity.class);
EntityTypeService entityTypeService = mock(EntityTypeService.class);
Item item = mock(Item.class);
List<MetadataValue> list = new ArrayList<>();
MetadataValue metadataValue = mock(MetadataValue.class);
list.add(metadataValue);
EntityType entityType = entityTypeService.findByEntityType(context, "testType");
// Mock the state of objects utilized in getType() to meet the success criteria of an invocation
when(metadataValue.getValue()).thenReturn("testType");
when(itemService.getMetadata(item, "relationship", "type", null, Item.ANY)).thenReturn(list);
// The returned EntityType should equal our defined entityType case
assertEquals("TestGetType 0", entityType, entityService.getType(context, entity));
}
@Test
public void testGetLeftRelation() {
// Declare objects utilized in unit test
Item item = mock(Item.class);
UUID uuid = UUID.randomUUID();
Relationship relationship = mock(Relationship.class);
List<Relationship> relationshipList = new ArrayList<>();
relationshipList.add(relationship);
Entity entity = mock(Entity.class);
// Mock the state of objects utilized in getLeftRelation() to meet the success criteria of an invocation
when(entity.getItem()).thenReturn(item);
when(relationship.getLeftItem()).thenReturn(item);
when(item.getID()).thenReturn(uuid);
when(entity.getRelationships()).thenReturn(relationshipList);
// The left relation(s) reported from our mocked Entity should match our relationshipList
assertEquals("TestGetLeftRelations 0", relationshipList, entityService.getLeftRelations(context, entity));
}
@Test
public void testGetRightRelation() {
// Declare objects utilized in unit test
Item item = mock(Item.class);
UUID uuid = UUID.randomUUID();
Relationship relationship = mock(Relationship.class);
List<Relationship> relationshipList = new ArrayList<>();
relationshipList.add(relationship);
Entity entity = mock(Entity.class);
// Mock the state of objects utilized in getRightRelation() to meet the success criteria of an invocation
when(entity.getItem()).thenReturn(item);
when(relationship.getRightItem()).thenReturn(item);
when(item.getID()).thenReturn(uuid);
when(entity.getRelationships()).thenReturn(relationshipList);
// The right relation(s) reported from our mocked Entity should match our relationshipList
assertEquals("TestGetLeftRelations 0", relationshipList, entityService.getRightRelations(context, entity));
}
@Test
public void testGetRelationsByLabel() throws Exception {
// Declare objects utilized in unit test
Relationship relationship = mock(Relationship.class);
RelationshipType relationshipType = mock(RelationshipType.class);
relationship.setRelationshipType(relationshipType);
// Currently this unit test will only test one case with one relationship
List<Relationship> relationshipList = new ArrayList<>();
relationshipList.add(relationship);
// Mock the state of objects utilized in getRelationsByLabel() to meet the success criteria of an invocation
when(relationshipService.findAll(context)).thenReturn(relationshipList);
when(relationship.getRelationshipType()).thenReturn(relationshipType);
when(relationshipType.getLeftLabel()).thenReturn("leftLabel");
when(relationshipType.getRightLabel()).thenReturn("rightLabel");
// The relation(s) reported from our defined label should match our relationshipList
assertEquals("TestGetRelationsByLabel 0", relationshipList,
entityService.getRelationsByLabel(context, "leftLabel"));
}
@Test
public void testGetAllRelationshipTypes() throws Exception {
// Declare objects utilized for this test
List<MetadataValue> list = new ArrayList<>();
MetadataValue metadataValue = mock(MetadataValue.class);
list.add(metadataValue);
Item item = mock(Item.class);
RelationshipTypeDAO relationshipTypeDAO = mock(RelationshipTypeDAO.class);
Entity entity = mock(Entity.class);
RelationshipType relationshipType = mock(RelationshipType.class);
relationshipType.setLeftType(leftType);
relationshipType.setLeftType(rightType);
// Currently this unit test will only test one case with one relationshipType
List<RelationshipType> relationshipTypeList = new ArrayList<>();
relationshipTypeList.add(relationshipType);
// Mock the state of objects utilized in getAllRelationshipTypes()
// to meet the success criteria of the invocation
when(metadataValue.getValue()).thenReturn("testType");
when(entity.getItem()).thenReturn(item);
when(itemService.getMetadata(item, "relationship", "type", null, Item.ANY)).thenReturn(list);
when(relationshipTypeDAO.findAll(context, RelationshipType.class)).thenReturn(relationshipTypeList);
when(relationshipTypeService.findAll(context)).thenReturn(relationshipTypeList);
when(relationshipType.getLeftType()).thenReturn(leftType);
when(relationshipType.getRightType()).thenReturn(rightType);
when(entityTypeService.findByEntityType(context, "value")).thenReturn(leftType);
when(leftType.getID()).thenReturn(0);
when(rightType.getID()).thenReturn(1);
when(entityService.getType(context, entity)).thenReturn(leftType); // Mock
// The relation(s) reported from our mocked Entity should match our relationshipList
assertEquals("TestGetAllRelationshipTypes 0", relationshipTypeList,
entityService.getAllRelationshipTypes(context, entity));
}
@Test
public void testGetLeftRelationshipTypes() throws Exception {
// Declare objects utilized in unit test
Item item = mock(Item.class);
Entity entity = mock(Entity.class);
EntityType entityType = mock(EntityType.class);
RelationshipType relationshipType = mock(RelationshipType.class);
// Currently this unit test will only test one case with one relationshipType
List<RelationshipType> relationshipTypeList = new LinkedList<>();
relationshipTypeList.add(relationshipType);
List<MetadataValue> metsList = new ArrayList<>();
MetadataValue metadataValue = mock(MetadataValue.class);
metsList.add(metadataValue);
// Mock the state of objects utilized in getLeftRelationshipTypes()
// to meet the success criteria of the invocation
when(itemService.getMetadata(any(), any(), any(), any(), any())).thenReturn(metsList);
when(entity.getItem()).thenReturn(item);
when(entityType.getID()).thenReturn(0);
when(relationshipTypeService.findAll(any())).thenReturn(relationshipTypeList);
when(relationshipType.getLeftType()).thenReturn(entityType);
when(entityService.getType(context, entity)).thenReturn(entityType);
when(entityTypeService.findByEntityType(any(), any())).thenReturn(entityType);
// The left relationshipType(s) reported from our mocked Entity should match our relationshipList
assertEquals("TestGetLeftRelationshipTypes 0", relationshipTypeList,
entityService.getLeftRelationshipTypes(context, entity));
}
@Test
public void testGetRightRelationshipTypes() throws Exception {
// Declare objects utilized in unit test
Item item = mock(Item.class);
Entity entity = mock(Entity.class);
EntityType entityType = mock(EntityType.class);
RelationshipType relationshipType = mock(RelationshipType.class);
// Currently this unit test will only test one case with one relationshipType
List<RelationshipType> relationshipTypeList = new LinkedList<>();
relationshipTypeList.add(relationshipType);
List<MetadataValue> metsList = new ArrayList<>();
MetadataValue metadataValue = mock(MetadataValue.class);
metsList.add(metadataValue);
// Mock the state of objects utilized in getRightRelationshipTypes()
// to meet the success criteria of the invocation
when(itemService.getMetadata(any(), any(), any(), any(), any())).thenReturn(metsList);
when(entity.getItem()).thenReturn(item);
when(entityType.getID()).thenReturn(0);
when(relationshipTypeService.findAll(any())).thenReturn(relationshipTypeList);
when(relationshipType.getRightType()).thenReturn(entityType);
when(entityService.getType(context, entity)).thenReturn(entityType);
when(entityTypeService.findByEntityType(any(), any())).thenReturn(entityType);
// The right relationshipType(s) reported from our mocked Entity should match our relationshipList
assertEquals("TestGetRightRelationshipTypes 0", relationshipTypeList,
entityService.getRightRelationshipTypes(context, entity));
}
@Test
public void testGetRelationshipTypesByLabel() throws Exception {
// Declare objects utilized in unit test
List<RelationshipType> list = new LinkedList<>();
RelationshipType relationshipType = mock(RelationshipType.class);
list.add(relationshipType);
// Mock the state of objects utilized in getRelationshipTypesByLabel()
// to meet the success criteria of the invocation
when(relationshipTypeService.findAll(context)).thenReturn(list);
when(relationshipType.getLeftLabel()).thenReturn("leftLabel");
when(relationshipType.getRightLabel()).thenReturn("rightLabel");
// The RelationshipType(s) reported from our mocked Entity should match our list
assertEquals("TestGetRelationshipTypesByLabel 0", list,
entityService.getRelationshipTypesByLabel(context, "leftLabel"));
}
}

View File

@@ -0,0 +1,136 @@
/**
* 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.content;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.List;
import org.dspace.authorize.service.AuthorizeService;
import org.dspace.content.dao.EntityTypeDAO;
import org.dspace.core.Context;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class EntityTypeServiceImplTest {
@InjectMocks
private EntityTypeServiceImpl entityTypeService;
@Mock
private EntityType entityType;
@Mock
private EntityTypeDAO entityTypeDAO;
@Mock
private Context context;
@Mock
private AuthorizeService authorizeService;
@Test
public void testFindByEntityType() throws Exception {
// Mock DAO to return our mocked EntityType
when(entityTypeDAO.findByEntityType(context, "TestType")).thenReturn(entityType);
// The EntityType reported from our TestType parameter should match our mocked EntityType
assertEquals("TestFindByEntityType 0", entityType, entityTypeService.findByEntityType(context, "TestType"));
}
@Test
public void testFindAll() throws Exception {
// Declare objects utilized in unit test
List<EntityType> entityTypeList = new ArrayList<>();
// Mock DAO to return our mocked entityTypeList
when(entityTypeDAO.findAll(context, EntityType.class)).thenReturn(entityTypeList);
// The EntityType(s) reported from our mocked state should match our entityTypeList
assertEquals("TestFindAll 0", entityTypeList, entityTypeService.findAll(context));
}
@Test
public void testCreate() throws Exception {
// Mock admin state
when(authorizeService.isAdmin(context)).thenReturn(true);
// Declare objects utilized in unit test
EntityType entityType = new EntityType();
entityType.setLabel("Test");
// Mock DAO to return our defined EntityType
when(entityTypeDAO.create(any(), any())).thenReturn(entityType);
// The newly created EntityType's label should match our mocked EntityType's label
assertEquals("TestCreate 0", entityType.getLabel(), entityTypeService.create(context, "Test").getLabel());
// The newly created EntityType should match our mocked EntityType
assertEquals("TestCreate 1", entityType, entityTypeService.create(context));
}
@Test
public void testFind() throws Exception {
// Mock DAO to return our mocked EntityType
when(entityTypeDAO.findByID(context, EntityType.class, 0)).thenReturn(entityType);
// The reported EntityType should match our mocked entityType
assertEquals("TestFind 0", entityType, entityTypeService.find(context, 0));
}
@Test
public void testUpdate() throws Exception {
// Declare objects utilized in unit test
List<EntityType> entityTypeList = new ArrayList<>();
entityTypeList.add(entityType);
// Mock admin state
when(authorizeService.isAdmin(context)).thenReturn(true);
// Invoke both impls of method update()
entityTypeService.update(context, entityType);
entityTypeService.update(context, entityTypeList);
// Verify entityTypeDAO.save was invoked twice to confirm proper invocation of both impls of update()
Mockito.verify(entityTypeDAO,times(2)).save(context, entityType);
}
@Test
public void testDelete() throws Exception {
// Mock admin state
when(authorizeService.isAdmin(context)).thenReturn(true);
// Invoke method delete()
entityTypeService.delete(context, entityType);
// Verify entityTypeDAO.delete() ran once to confirm proper invocation of delete()
Mockito.verify(entityTypeDAO,times(1)).delete(context, entityType);
}
/**
* Helper method that reutrns new EntityType
* @return new EntityType
*/
public EntityType makeEntityType() {
return new EntityType();
}
}

View File

@@ -0,0 +1,347 @@
/**
* 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.content;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.dspace.authorize.service.AuthorizeService;
import org.dspace.content.dao.RelationshipDAO;
import org.dspace.content.service.ItemService;
import org.dspace.content.virtual.VirtualMetadataPopulator;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class RelationshipServiceImplTest {
@InjectMocks
private RelationshipServiceImpl relationshipService;
@Mock
private RelationshipDAO relationshipDAO;
@Mock
private Context context;
@Mock
private Relationship relationship;
@Mock
private List<Relationship> relationshipsList;
@Mock
private AuthorizeService authorizeService;
@Mock
private ItemService itemService;
@Mock
private VirtualMetadataPopulator virtualMetadataPopulator;
@Before
public void init() {
relationshipsList = new ArrayList<>();
relationshipsList.add(relationship);
}
@Test
public void testFindAll() throws Exception {
// Mock DAO to return our mocked relationshipsList
when(relationshipDAO.findAll(context, Relationship.class)).thenReturn(relationshipsList);
// The reported Relationship(s) should match our relationshipsList
assertEquals("TestFindAll 0", relationshipsList, relationshipService.findAll(context));
}
@Test
public void testFindByItem() throws Exception {
// Declare objects utilized in unit test
List<Relationship> relationshipTest = new ArrayList<>();
Item cindy = mock(Item.class);
Item fred = mock(Item.class);
Item bob = mock(Item.class);
Item hank = mock(Item.class);
Item jasper = mock(Item.class);
Item spot = mock(Item.class);
RelationshipType hasDog = new RelationshipType();
RelationshipType hasFather = new RelationshipType();
RelationshipType hasMother = new RelationshipType();
hasDog.setLeftLabel("hasDog");
hasDog.setRightLabel("isDogOf");
hasFather.setLeftLabel("hasFather");
hasFather.setRightLabel("isFatherOf");
hasMother.setLeftLabel("hasMother");
hasMother.setRightLabel("isMotherOf");
relationshipTest.add(getRelationship(cindy, spot, hasDog,0,0));
relationshipTest.add(getRelationship(cindy, jasper, hasDog,0,1));
relationshipTest.add(getRelationship(cindy, hank, hasFather,0,0));
relationshipTest.add(getRelationship(fred, cindy, hasMother,0,0));
relationshipTest.add(getRelationship(bob, cindy, hasMother,1,0));
when(relationshipService.findByItem(context, cindy)).thenReturn(relationshipTest);
when(relationshipDAO.findByItem(context, cindy)).thenReturn(relationshipTest);
// Mock the state of objects utilized in findByItem() to meet the success criteria of the invocation
when(relationshipDAO.findByItem(context, cindy)).thenReturn(relationshipTest);
List<Relationship> results = relationshipService.findByItem(context, cindy);
assertEquals("TestFindByItem 0", relationshipTest, results);
for (int i = 0; i < relationshipTest.size(); i++) {
assertEquals("TestFindByItem sort integrity", relationshipTest.get(i), results.get(i));
}
}
@Test
public void testFindLeftPlaceByLeftItem() throws Exception {
// Declare objects utilized in unit test
Item item = mock(Item.class);
// Mock DAO to return mocked left place as 0
when(relationshipDAO.findLeftPlaceByLeftItem(context, item)).thenReturn(0);
// The left place reported from out mocked item should match the DAO's report of the left place
assertEquals("TestFindLeftPlaceByLeftItem 0", relationshipDAO.findLeftPlaceByLeftItem(context, item),
relationshipService.findLeftPlaceByLeftItem(context, item));
}
@Test
public void testFindRightPlaceByRightItem() throws Exception {
// Declare objects utilized in unit test
Item item = mock(Item.class);
// Mock lower level DAO to return mocked right place as 0
when(relationshipDAO.findRightPlaceByRightItem(context, item)).thenReturn(0);
// The right place reported from out mocked item should match the DAO's report of the right place
assertEquals("TestFindRightPlaceByRightItem 0", relationshipDAO.findRightPlaceByRightItem(context, item),
relationshipService.findRightPlaceByRightItem(context, item));
}
@Test
public void testFindByItemAndRelationshipType() throws Exception {
// Declare objects utilized in unit test
List<Relationship> relList = new LinkedList<>();
Item item = mock(Item.class);
RelationshipType testRel = new RelationshipType();
// The Relationship(s) reported should match our our relList, given left place as true
assertEquals("TestFindByItemAndRelationshipType 0", relList,
relationshipService.findByItemAndRelationshipType(context, item, testRel, true));
// The Relationship(s) reported should match our our relList
assertEquals("TestFindByItemAndRelationshipType 1", relList,
relationshipService.findByItemAndRelationshipType(context, item, testRel));
}
@Test
public void testFindByRelationshipType() throws Exception {
// Declare objects utilized in unit test
List<Relationship> relList = new LinkedList<>();
RelationshipType testRel = new RelationshipType();
// The Relationship(s) reported should match our our relList
assertEquals("TestFindByRelationshipType 0", relList,
relationshipService.findByRelationshipType(context, testRel));
}
@Test
public void find() throws Exception {
// Declare objects utilized in unit test
Relationship relationship = new Relationship();
relationship.setId(1337);
// Mock DAO to return our mocked relationship
when(relationshipDAO.findByID(context, Relationship.class, relationship.getID())).thenReturn(relationship);
// The reported Relationship should match our mocked relationship
assertEquals("TestFind 0", relationship, relationshipService.find(context, relationship.getID()));
}
@Test
public void testCreate() throws Exception {
// Mock admin state
when(authorizeService.isAdmin(context)).thenReturn(true);
// Declare objects utilized in unit test
Relationship relationship = relationshipDAO.create(context,new Relationship());
context.turnOffAuthorisationSystem();
assertEquals("TestCreate 0", relationship, relationshipService.create(context));
MetadataValue metVal = mock(MetadataValue.class);
List<MetadataValue> metsList = new ArrayList<>();
List<Relationship> leftTypelist = new ArrayList<>();
List<Relationship> rightTypelist = new ArrayList<>();
Item leftItem = mock(Item.class);
Item rightItem = mock(Item.class);
RelationshipType testRel = new RelationshipType();
EntityType leftEntityType = mock(EntityType.class);
EntityType rightEntityType = mock(EntityType.class);
testRel.setLeftType(leftEntityType);
testRel.setRightType(rightEntityType);
testRel.setLeftLabel("Entitylabel");
testRel.setRightLabel("Entitylabel");
metsList.add(metVal);
relationship = getRelationship(leftItem, rightItem, testRel, 0,0);
leftTypelist.add(relationship);
rightTypelist.add(relationship);
// Mock the state of objects utilized in create() to meet the success criteria of the invocation
when(virtualMetadataPopulator
.isUseForPlaceTrueForRelationshipType(relationship.getRelationshipType(), true)).thenReturn(true);
when(authorizeService
.authorizeActionBoolean(context, relationship.getLeftItem(), Constants.WRITE)).thenReturn(true);
when(authorizeService
.authorizeActionBoolean(context, relationship.getRightItem(), Constants.WRITE)).thenReturn(true);
when(relationshipService.findByItem(context,leftItem)).thenReturn(leftTypelist);
when(relationshipService.findByItem(context,rightItem)).thenReturn(rightTypelist);
when(leftEntityType.getLabel()).thenReturn("Entitylabel");
when(rightEntityType.getLabel()).thenReturn("Entitylabel");
when(metVal.getValue()).thenReturn("Entitylabel");
when(metsList.get(0).getValue()).thenReturn("Entitylabel");
when(relationshipService
.findByItemAndRelationshipType(context, leftItem, testRel, true)).thenReturn(leftTypelist);
when(itemService.getMetadata(leftItem, "relationship", "type", null, Item.ANY)).thenReturn(metsList);
when(itemService.getMetadata(rightItem, "relationship", "type", null, Item.ANY)).thenReturn(metsList);
when(relationshipDAO.create(any(), any())).thenReturn(relationship);
// The reported Relationship should match our defined relationship
assertEquals("TestCreate 1", relationship, relationshipService.create(context, relationship));
// The reported Relationship should match our defined relationship, given left/right item
// and RelationshipType
assertEquals("TestCreate 2", relationship, relationshipService.create(context, leftItem, rightItem,
testRel,0,0));
context.restoreAuthSystemState();
}
@Test
public void testDelete() throws Exception {
// Mock admin state
when(authorizeService.isAdmin(context)).thenReturn(true);
// Declare objects utilized in unit test
MetadataValue metVal = mock(MetadataValue.class);
List<MetadataValue> metsList = new ArrayList<>();
List<Relationship> leftTypelist = new ArrayList<>();
List<Relationship> rightTypelist = new ArrayList<>();
Item leftItem = mock(Item.class);
Item rightItem = mock(Item.class);
RelationshipType testRel = new RelationshipType();
EntityType leftEntityType = mock(EntityType.class);
EntityType rightEntityType = mock(EntityType.class);
testRel.setLeftType(leftEntityType);
testRel.setRightType(rightEntityType);
testRel.setLeftLabel("Entitylabel");
testRel.setRightLabel("Entitylabel");
testRel.setLeftMinCardinality(0);
testRel.setRightMinCardinality(0);
metsList.add(metVal);
relationship = getRelationship(leftItem, rightItem, testRel, 0,0);
leftTypelist.add(relationship);
rightTypelist.add(relationship);
// Mock the state of objects utilized in delete() to meet the success criteria of the invocation
when(virtualMetadataPopulator.isUseForPlaceTrueForRelationshipType(relationship.getRelationshipType(), true))
.thenReturn(true);
when(authorizeService.authorizeActionBoolean(context, relationship.getLeftItem(), Constants.WRITE))
.thenReturn(true);
when(authorizeService.authorizeActionBoolean(context, relationship.getRightItem(), Constants.WRITE))
.thenReturn(true);
when(relationshipService.findByItem(context,leftItem)).thenReturn(leftTypelist);
when(relationshipService.findByItem(context,rightItem)).thenReturn(rightTypelist);
when(leftEntityType.getLabel()).thenReturn("Entitylabel");
when(rightEntityType.getLabel()).thenReturn("Entitylabel");
when(metVal.getValue()).thenReturn("Entitylabel");
when(metsList.get(0).getValue()).thenReturn("Entitylabel");
when(relationshipService.findByItemAndRelationshipType(context, leftItem, testRel, true))
.thenReturn(leftTypelist);
when(itemService.getMetadata(leftItem, "relationship", "type", null, Item.ANY)).thenReturn(metsList);
when(itemService.getMetadata(rightItem, "relationship", "type", null, Item.ANY)).thenReturn(metsList);
when(relationshipDAO.create(any(), any())).thenReturn(relationship);
when(relationshipService.find(context,0)).thenReturn(relationship);
// Invoke delete()
relationshipService.delete(context, relationship);
// Verify RelationshipService.delete() ran once to confirm proper invocation of delete()
Mockito.verify(relationshipDAO).delete(context, relationship);
}
@Test
public void testUpdate() throws Exception {
// Mock admin state
context.turnOffAuthorisationSystem();
when(authorizeService.isAdmin(context)).thenReturn(true);
// Declare objects utilized in unit test
MetadataValue metVal = mock(MetadataValue.class);
List<MetadataValue> metsList = new ArrayList<>();
Item leftItem = mock(Item.class);
Item rightItem = mock(Item.class);
RelationshipType testRel = new RelationshipType();
EntityType leftEntityType = mock(EntityType.class);
EntityType rightEntityType = mock(EntityType.class);
testRel.setLeftType(leftEntityType);
testRel.setRightType(rightEntityType);
testRel.setLeftLabel("Entitylabel");
testRel.setRightLabel("Entitylabel");
testRel.setLeftMinCardinality(0);
testRel.setRightMinCardinality(0);
metsList.add(metVal);
relationship = getRelationship(leftItem, rightItem, testRel, 0,0);
// Mock the state of objects utilized in update() to meet the success criteria of the invocation
when(itemService.getMetadata(leftItem, "relationship", "type", null, Item.ANY)).thenReturn(metsList);
when(itemService.getMetadata(rightItem, "relationship", "type", null, Item.ANY)).thenReturn(metsList);
when(authorizeService.authorizeActionBoolean(context, relationship.getLeftItem(),
Constants.WRITE)).thenReturn(true);
when(authorizeService.authorizeActionBoolean(context, relationship.getRightItem(),
Constants.WRITE)).thenReturn(true);
// Invoke update()
relationshipService.update(context, relationship);
// Verify RelationshipDAO.delete() ran once to confirm proper invocation of update()
Mockito.verify(relationshipDAO).save(context, relationship);
}
/**
* Helper method that returns a configured Relationship
* @param leftItem Relationship's left item
* @param rightItem Relationship's right item
* @param relationshipType Relationship's RelationshipType
* @param leftPlace Relationship's left place
* @param rightPlace Relationship's right place
* @return Configured Relationship
*/
private Relationship getRelationship(Item leftItem, Item rightItem, RelationshipType relationshipType,
int leftPlace, int rightPlace) {
Relationship relationship = new Relationship();
relationship.setId(0);
relationship.setLeftItem(leftItem);
relationship.setRightItem(rightItem);
relationship.setRelationshipType(relationshipType);
relationship.setLeftPlace(leftPlace);
relationship.setRightPlace(rightPlace);
return relationship;
}
}

View File

@@ -0,0 +1,172 @@
/**
* 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.content;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import org.apache.logging.log4j.Logger;
import org.dspace.content.dao.RelationshipTypeDAO;
import org.dspace.core.Context;
import org.junit.Before;
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 RelationshipTypeTest {
private static final Logger log = org.apache.logging.log4j.LogManager.getLogger(RelationshipTypeTest.class);
@InjectMocks
private RelationshipTypeServiceImpl relationshipTypeService;
@Mock
private RelationshipTypeDAO relationshipTypeDAO;
private RelationshipType firstRelationshipType;
private RelationshipType secondRelationshipType;
private Context context;
@Before
public void init() {
// Default state of firstRelationshipType
firstRelationshipType = mock(RelationshipType.class);
firstRelationshipType.setId(1);
firstRelationshipType.setLeftType(mock(EntityType.class));
firstRelationshipType.setRightType(mock(EntityType.class));
firstRelationshipType.setLeftLabel("isAuthorOfPublication");
firstRelationshipType.setRightLabel("isPublicationOfAuthor");
firstRelationshipType.setLeftMinCardinality(0);
firstRelationshipType.setLeftMaxCardinality(null);
firstRelationshipType.setRightMinCardinality(0);
firstRelationshipType.setRightMinCardinality(null);
// Default state of secondRelationshipType
secondRelationshipType = mock(RelationshipType.class);
secondRelationshipType.setId(new Random().nextInt());
secondRelationshipType.setLeftType(mock(EntityType.class));
secondRelationshipType.setRightType(mock(EntityType.class));
secondRelationshipType.setLeftLabel("isProjectOfPerson");
secondRelationshipType.setRightLabel("isPersonOfProject");
secondRelationshipType.setLeftMinCardinality(0);
secondRelationshipType.setLeftMaxCardinality(null);
secondRelationshipType.setRightMinCardinality(0);
secondRelationshipType.setRightMinCardinality(null);
}
@Test
public void testRelationshipTypeFind() throws Exception {
// Mock DAO to return our firstRelationshipType
when(relationshipTypeDAO.findByID(any(), any(), any(Integer.class))).thenReturn(firstRelationshipType);
// Declare objects utilized for this test
RelationshipType found = relationshipTypeService.find(context, 1);
// Pass expected and actual RelationshipTypes into comparator method
checkRelationshipTypeValues(found, firstRelationshipType);
}
@Test
public void testRelationshipTypeFindByTypesAndLabels() throws Exception {
// Mock DAO to return our firstRelationshipType
when(relationshipTypeDAO.findByTypesAndLabels(any(), any(), any(), any(), any()))
.thenReturn(firstRelationshipType);
// Declare objects utilized for this test
RelationshipType found = relationshipTypeService.findbyTypesAndLabels(context, mock(EntityType.class),
mock(EntityType.class),
"mock", "mock");
// Pass expected and actual RelationshipTypes into comparator method
checkRelationshipTypeValues(found, firstRelationshipType);
}
@Test
public void testRelationshipTypeFindAll() throws Exception {
// Declare objects utilized for this test
List<RelationshipType> mockedList = new LinkedList<>();
mockedList.add(firstRelationshipType);
mockedList.add(secondRelationshipType);
// Mock DAO to return our mockedList
when(relationshipTypeDAO.findAll(context, RelationshipType.class)).thenReturn(mockedList);
// Invoke findAll()
List<RelationshipType> foundRelationshipTypes = relationshipTypeService.findAll(context);
// Assert that our foundRelationshipTypes should not be null and contain two RelationshipTypes
assertThat(foundRelationshipTypes, notNullValue());
assertThat(foundRelationshipTypes.size(), equalTo(2));
}
@Test
public void testRelationshipTypeFindByLeftOrRightLabel() throws Exception {
// Declare objects utilized for this test
List<RelationshipType> mockedList = new LinkedList<>();
mockedList.add(firstRelationshipType);
// Mock DAO to return our mockedList
when(relationshipTypeDAO.findByLeftOrRightLabel(any(), any())).thenReturn(mockedList);
// Invoke findByLeftOrRightLabel()
List<RelationshipType> found = relationshipTypeService.findByLeftOrRightLabel(context, "mock");
// Assert that our expected list contains our expected RelationshipType and nothing more
assertThat(found, notNullValue());
assertThat(found.size(), equalTo(1));
checkRelationshipTypeValues(found.get(0), firstRelationshipType);
}
@Test
public void testRelationshipTypefindByEntityType() throws Exception {
// Declare objects utilized for this test
List<RelationshipType> mockedList = new LinkedList<>();
mockedList.add(firstRelationshipType);
// Mock DAO to return our mockedList
when(relationshipTypeDAO.findByEntityType(any(), any())).thenReturn(mockedList);
// Invoke findByEntityType()
List<RelationshipType> found = relationshipTypeService.findByEntityType(context, mock(EntityType.class));
// Assert that our expected list contains our expected RelationshipType and nothing more
assertThat(found, notNullValue());
assertThat(found.size(), equalTo(1));
checkRelationshipTypeValues(found.get(0), firstRelationshipType);
}
/**
* Helper method that compares RelationshipTypes
* @param found The reported RelationshipType
* @param original The original RelationshipType
*/
private void checkRelationshipTypeValues(RelationshipType found, RelationshipType original) {
assertThat(found, notNullValue());
assertThat(found.getLeftLabel(), equalTo(original.getLeftLabel()));
assertThat(found.getRightLabel(), equalTo(original.getRightLabel()));
assertThat(found.getLeftType(), equalTo(original.getLeftType()));
assertThat(found.getRightType(), equalTo(original.getRightType()));
assertThat(found.getLeftMinCardinality(), equalTo(original.getLeftMinCardinality()));
assertThat(found.getLeftMaxCardinality(), equalTo(original.getLeftMaxCardinality()));
assertThat(found.getRightMinCardinality(), equalTo(original.getRightMinCardinality()));
assertThat(found.getRightMaxCardinality(), equalTo(original.getRightMaxCardinality()));
}
}

View File

@@ -0,0 +1,105 @@
/**
* 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.content.virtual;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.List;
import org.dspace.content.Item;
import org.dspace.content.MetadataValue;
import org.dspace.content.service.ItemService;
import org.dspace.core.Context;
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 CollectedTest {
@InjectMocks
private Collected collected;
@Mock
private List<String> fields;
@Mock
private ItemService itemService;
@Mock
private Context context;
@Test
public void testGetFields() {
// The reported Class should match our mocked fields class
assertEquals("TestGetFields 0", fields.getClass(), collected.getFields().getClass());
}
@Test
public void testSetFields() {
// Setup objects utilized in unit test
collected.setFields(fields);
// The reported fields should math our defined fields
assertEquals("TestSetFields 0", fields, collected.getFields());
}
@Test
public void testSetUseForPlace() {
// Setup objects utilized in unit test
collected.setUseForPlace(true);
// collected.getUseForPlace() should return true
assertEquals("TestSetUseForPlace 0", true, collected.getUseForPlace());
}
@Test
public void testGetUseForPlace() {
// Setup objects utilized in unit test
boolean bool = true;
collected.setUseForPlace(true);
// The reported boolean should math our defined bool
assertEquals("TestGetUseForPlace 0", bool, collected.getUseForPlace());
}
@Test
public void testGetValues() {
// Setup objects utilized in unit test
List<String> list = new ArrayList<>();
List<String> valueList = new ArrayList<>();
List<MetadataValue> metadataValueList = new ArrayList<>();
MetadataValue metadataValue = mock(MetadataValue.class);
Item item = mock(Item.class);
metadataValueList.add(metadataValue);
String s = "dc.title";
list.add(s);
String[] splittedString = s.split("\\.");
collected.setFields(list);
valueList.add("TestValue");
// Mock the state of objects utilized in getValues() to meet the success criteria of an invocation
when(itemService.getMetadata(item, splittedString.length > 0 ? splittedString[0] :
null,
splittedString.length > 1 ? splittedString[1] :
null,
splittedString.length > 2 ? splittedString[2] :
null,
Item.ANY, false)).thenReturn(metadataValueList);
when(metadataValue.getValue()).thenReturn("TestValue");
// The reported value(s) should match our valueList
assertEquals("TestGetValues 0", valueList, collected.getValues(context, item));
}
}

View File

@@ -0,0 +1,128 @@
/**
* 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.content.virtual;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.List;
import org.dspace.content.Item;
import org.dspace.content.MetadataValue;
import org.dspace.content.service.ItemService;
import org.dspace.core.Context;
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 ConcatenateTest {
@InjectMocks
private Concatenate concatenate;
/**
* The fields for which the metadata will be retrieved
*/
@Mock
private List<String> fields;
@Mock
private ItemService itemService;
@Mock
private Context context;
@Test
public void testGetFields() {
// The reported Class should match our mocked fields class
assertEquals("TestGetFields 0", fields.getClass(), concatenate.getFields().getClass());
}
@Test
public void testSetFields() {
// Setup objects utilized in unit test
concatenate.setFields(fields);
// The reported Class should match our mocked fields class
assertEquals("TestSetFields 0", fields, concatenate.getFields());
}
@Test
public void testGetSeperator() {
// Setup objects utilized in unit test
String seperator = ",";
concatenate.setSeparator(",");
// The reported seperator should match our defined seperator
assertEquals("TestGetSeperator 0", seperator, concatenate.getSeparator());
}
@Test
public void testSetSeperator() {
// Setup objects utilized in unit test
concatenate.setSeparator(",");
// The reported seperator should match our defined seperator
assertEquals("TestSetSeperator 0", ",", concatenate.getSeparator());
}
@Test
public void testSetUseForPlace() {
// Setup objects utilized in unit test
concatenate.setUseForPlace(true);
// The reported seperator should match our defined seperator
assertEquals("TestSetUseForPlace 0", true, concatenate.getUseForPlace());
}
@Test
public void testGetUseForPlace() {
// Setup objects utilized in unit test
boolean bool = true;
concatenate.setUseForPlace(true);
// The reported boolean should match our defined bool
assertEquals("TestGetUseForPlace 0", bool, concatenate.getUseForPlace());
}
@Test
public void testGetValues() {
// Setup objects utilized in unit test
List<String> list = new ArrayList<>();
List<String> valueList = new ArrayList<>();
List<MetadataValue> metadataValueList = new ArrayList<>();
MetadataValue metadataValue = mock(MetadataValue.class);
Item item = mock(Item.class);
metadataValueList.add(metadataValue);
String s = "dc.title";
list.add(s);
String[] splittedString = s.split("\\.");
concatenate.setFields(list);
valueList.add("TestValue");
// Mock the state of objects utilized in getValues() to meet the success criteria of an invocation
when(itemService.getMetadata(item, splittedString.length > 0 ? splittedString[0] :
null,
splittedString.length > 1 ? splittedString[1] :
null,
splittedString.length > 2 ? splittedString[2] :
null,
Item.ANY, false)).thenReturn(metadataValueList);
when(metadataValue.getValue()).thenReturn("TestValue");
// The reported values should match our defined valueList
assertEquals("TestGetValues 0", valueList, concatenate.getValues(context, item));
}
}

View File

@@ -0,0 +1,70 @@
/**
* 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.content.virtual;
import static org.junit.Assert.assertEquals;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.runners.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class EntityTypeToFilterQueryServiceTest {
@InjectMocks
private EntityTypeToFilterQueryService entityTypeToFilterQueryService;
@Test
public void testSetMap() {
// Setup objects utilized in unit test
Map<String, String> map = new HashMap<String, String>();
map.put("key", "value");
entityTypeToFilterQueryService.setMap(map);
// The reported map should match our defined map
assertEquals("TestSetMap 0", map, entityTypeToFilterQueryService.getMap());
}
@Test
public void testGetMap() {
// Setup objects utilized in unit test
Map<String, String> map = Collections.emptyMap();
entityTypeToFilterQueryService.setMap(map);
// The reported map should match our defined map
assertEquals("TestGetFields 0", map, entityTypeToFilterQueryService.getMap());
}
@Test
public void testHasKey() {
// Setup objects utilized in unit test
Map<String, String> map = new HashMap<String, String>();
map.put("key", "value");
entityTypeToFilterQueryService.setMap(map);
// The mocked entityTypeToFilterQueryService should report true for hasKey("key")
assertEquals("TestHasKey 0", true, entityTypeToFilterQueryService.hasKey("key"));
}
@Test
public void testGetFilterQueryForKey() {
// Setup objects utilized in unit test
Map<String, String> map = new HashMap<String, String>();
map.put("key", "value");
entityTypeToFilterQueryService.setMap(map);
// The reported value for our defined key should match our defined value
assertEquals("TestGetFilterQueryForKey 0", "value",
entityTypeToFilterQueryService.getFilterQueryForKey("key"));
}
}

View File

@@ -0,0 +1,162 @@
/**
* 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.content.virtual;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import org.dspace.content.Entity;
import org.dspace.content.EntityType;
import org.dspace.content.Item;
import org.dspace.content.Relationship;
import org.dspace.content.RelationshipType;
import org.dspace.content.service.EntityService;
import org.dspace.content.service.RelationshipService;
import org.dspace.core.Context;
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 RelatedTest {
@InjectMocks
private Related related;
@Mock
private RelationshipService relationshipService;
@Mock
private EntityService entityService;
@Mock
private VirtualMetadataConfiguration virtualMetadataConfiguration;
@Mock
private Context context;
@Test
public void testGetRelationshipTypeString() {
// Setup objects utilized in unit test
related.setRelationshipTypeString("TestType");
// The Type String reported should match our defined Type String
assertEquals("TestGetRelationshipTypeString 0", "TestType", related.getRelationshipTypeString());
}
@Test
public void testSetRelationshipTypeString() {
// Setup objects utilized in unit test
related.setRelationshipTypeString("TestType");
// The Type String reported should match our defined Type String
assertEquals("TestSetRelationshipTypeString 0", "TestType", related.getRelationshipTypeString());
}
@Test
public void testSetPlace() {
// Setup objects utilized in unit test
related.setPlace(0);
// The place reported should match our defined place
assertTrue("TestSetPlace 0", 0 == related.getPlace());
}
@Test
public void testGetPlace() {
// Setup objects utilized in unit test
related.setPlace(0);
// The place reported should match our defined place
assertTrue("TestGetPlace 0", 0 == related.getPlace());
}
@Test
public void testGetVirtualMetadataConfiguration() {
// The class reported should match our defined virtualMetadataConfiguration.getClass()
assertEquals("TestGetVirtualMetadataConfiguration 0", virtualMetadataConfiguration.getClass(),
related.getVirtualMetadataConfiguration().getClass());
}
@Test
public void testSetVirtualMetadataConfiguration() {
// Setup objects utilized in unit test
related.setVirtualMetadataConfiguration(virtualMetadataConfiguration);
// The class reported should match our defined virtualMetadataConfiguration.getClass()
assertEquals("TestGetVirtualMetadataConfiguration 0", virtualMetadataConfiguration,
related.getVirtualMetadataConfiguration());
}
@Test
public void testSetUseForPlace() {
// Setup objects utilized in unit test
related.setUseForPlace(true);
// related.getUseForPlace() should return true
assertEquals("TestSetVirtualMetadataConfiguration 0", true, related.getUseForPlace());
}
@Test
public void testGetUseForPlace() {
// Setup objects utilized in unit test
related.setUseForPlace(true);
// related.getUseForPlace() should return true
assertEquals("TestSetVirtualMetadataConfiguration 0", true, related.getUseForPlace());
}
@Test
public void testGetValues() throws Exception {
// Declare objects utilized in unit test
List<RelationshipType> relationshipTypeList = new ArrayList<>();
List<Relationship> relationshipList = new ArrayList<>();
Relationship relationship = mock(Relationship.class);
Item item = mock(Item.class);
Entity entity = mock(Entity.class);
EntityType entityType = mock(EntityType.class);
RelationshipType relationshipType = mock(RelationshipType.class);
related.setRelationshipTypeString("LeftLabel");
relationshipTypeList.add(relationshipType);
relationshipList.add(relationship);
related.setPlace(0);
// Mock the state of objects utilized in getRelationsByLabel() to meet the success criteria of an invocation
when(item.getID()).thenReturn(UUID.randomUUID());
when(relationshipType.getLeftLabel()).thenReturn("LeftLabel");
when(relationshipType.getRightLabel()).thenReturn("RightLabel");
when(relationshipType.getLeftType()).thenReturn(entityType);
when(relationshipType.getRightType()).thenReturn(entityType);
when(entityService.getAllRelationshipTypes(context, entity)).thenReturn(relationshipTypeList);
when(entityService.findByItemId(context, item.getID())).thenReturn(entity);
when(entityService.getType(context, entity)).thenReturn(entityType);
when(relationshipService.findByItemAndRelationshipType(context, item, relationshipType))
.thenReturn(relationshipList);
when(relationship.getRelationshipType()).thenReturn(relationshipType);
when(relationship.getLeftPlace()).thenReturn(0);
when(relationship.getRightPlace()).thenReturn(1);
when(relationship.getRightItem()).thenReturn(item);
when(relationship.getLeftItem()).thenReturn(item);
// The reported values should match out mocked collection of values
assertEquals("TestGetValues 0", virtualMetadataConfiguration.getValues(context, item),
related.getValues(context, item));
related.setPlace(1);
// Mock state to hit else if coverage
assertEquals("TestGetValues 1", virtualMetadataConfiguration.getValues(context, item),
related.getValues(context, item));
related.setPlace(2);
// No match should return empty LinkedList
assertEquals("TestGetValues 2", new LinkedList<>(), related.getValues(context, item));
}
}

View File

@@ -0,0 +1,67 @@
/**
* 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.content.virtual;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import org.dspace.content.Item;
import org.dspace.core.Context;
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 UUIDValueTest {
@InjectMocks
private UUIDValue UUIDValue;
@Mock
private Context context;
@Test
public void testGetValues() throws Exception {
// Setup objects utilized in unit test
List<String> list = new LinkedList<>();
Item item = mock(Item.class);
UUID uuid = UUID.randomUUID();
when(item.getID()).thenReturn(uuid);
list.add(String.valueOf(uuid));
// The reported value(s) should match our defined list
assertEquals("TestGetValues 0", list, UUIDValue.getValues(context, item));
}
@Test
public void testSetUseForPlace() {
// Setup objects utilized in unit test
UUIDValue.setUseForPlace(true);
// The reported boolean should return true
assertEquals("TestSetUseForPlace 0", true, UUIDValue.getUseForPlace());
}
@Test
public void testGetUseForPlace() {
// Setup objects utilized in unit test
UUIDValue.setUseForPlace(true);
// The reported boolean should return true
assertEquals("TestGetUseForPlace 0", true, UUIDValue.getUseForPlace());
}
}

View File

@@ -0,0 +1,82 @@
/**
* 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.content.virtual;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.HashMap;
import java.util.Map;
import org.dspace.content.RelationshipType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.runners.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class VirtualMetadataPopulatorTest {
@InjectMocks
private VirtualMetadataPopulator virtualMetadataPopulator;
@Test
public void testSetMap() {
// Setup objects utilized in unit test
Map<String, HashMap<String, VirtualMetadataConfiguration>> map = new HashMap<>();
HashMap<String, VirtualMetadataConfiguration> mapExt = new HashMap<>();
VirtualMetadataConfiguration virtualMetadataConfiguration = mock(VirtualMetadataConfiguration.class);
mapExt.put("hashKey", virtualMetadataConfiguration);
map.put("key", mapExt);
virtualMetadataPopulator.setMap(map);
// The returned map should match our defined map
assertEquals("TestSetMap 0", map, virtualMetadataPopulator.getMap());
}
@Test
public void testGetMap() {
// Setup objects utilized in unit test
Map<String, HashMap<String, VirtualMetadataConfiguration>> map = new HashMap<>();
HashMap<String, VirtualMetadataConfiguration> mapExt = new HashMap<>();
VirtualMetadataConfiguration virtualMetadataConfiguration = mock(VirtualMetadataConfiguration.class);
mapExt.put("hashKey", virtualMetadataConfiguration);
map.put("key", mapExt);
virtualMetadataPopulator.setMap(map);
// The returned map should match our defined map
assertEquals("TestGetMap 0", map, virtualMetadataPopulator.getMap());
}
@Test
public void testIsUseForPlaceTrueForRelationshipType() {
// Setup objects utilized in unit test
RelationshipType relationshipType = mock(RelationshipType.class);
Map<String, HashMap<String, VirtualMetadataConfiguration>> map = new HashMap<>();
HashMap<String, VirtualMetadataConfiguration> mapExt = new HashMap<>();
VirtualMetadataConfiguration virtualMetadataConfiguration = mock(VirtualMetadataConfiguration.class);
mapExt.put("hashKey", virtualMetadataConfiguration);
map.put("LeftLabel", mapExt);
map.put("NotRightLabel", mapExt);
virtualMetadataPopulator.setMap(map);
// Mock the state of objects utilized in isUseForPlaceTrueForRelationshipType()
// to meet the success criteria of an invocation
when(virtualMetadataConfiguration.getUseForPlace()).thenReturn(true);
when(relationshipType.getLeftLabel()).thenReturn("LeftLabel");
when(relationshipType.getRightLabel()).thenReturn("RightLabel");
// Assert that the useForPlace for our mocked relationshipType is false
assertEquals("TestGetFields 0", false,
virtualMetadataPopulator.isUseForPlaceTrueForRelationshipType(relationshipType, false));
// Assert that the useForPlace for our mocked relationshipType is true
assertEquals("TestGetFields 1", true,
virtualMetadataPopulator.isUseForPlaceTrueForRelationshipType(relationshipType, true));
}
}