diff --git a/dspace-api/src/test/java/org/dspace/content/EntityServiceImplTest.java b/dspace-api/src/test/java/org/dspace/content/EntityServiceImplTest.java new file mode 100644 index 0000000000..642e1f8849 --- /dev/null +++ b/dspace-api/src/test/java/org/dspace/content/EntityServiceImplTest.java @@ -0,0 +1,224 @@ +/** + * 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 { + Item item = mock(Item.class); + List relationshipList = new ArrayList<>(); + Relationship relationship = mock(Relationship.class); + relationship.setId(9); + relationshipList.add(relationship); + when(itemService.find(any(), any())).thenReturn(item); + when(item.getName()).thenReturn("ItemName"); + when(relationshipService.findByItem(any(), any())).thenReturn(relationshipList); + assertEquals("TestFindByItem 0", "ItemName", + entityService.findByItemId(context, item.getID()).getItem().getName()); + } + + @Test + public void testGetType() throws Exception { + Entity entity = mock(Entity.class); + EntityTypeService entityTypeService = mock(EntityTypeService.class); + Item item = mock(Item.class); + List list = new ArrayList<>(); + MetadataValue metadataValue = mock(MetadataValue.class); + list.add(metadataValue); + EntityType entityType = entityTypeService.findByEntityType(context, "testType"); + when(metadataValue.getValue()).thenReturn("testType"); + when(itemService.getMetadata(item, "relationship", "type", null, Item.ANY)).thenReturn(list); + assertEquals("TestGetType 0", entityType, entityService.getType(context, entity)); + } + + @Test + public void testGetLeftRelation() { + Item item = mock(Item.class); + UUID uuid = UUID.randomUUID(); + Relationship relationship = mock(Relationship.class); + List relationshipList = new ArrayList<>(); + relationshipList.add(relationship); + Entity entity = mock(Entity.class); + when(entity.getItem()).thenReturn(item); + when(relationship.getLeftItem()).thenReturn(item); + when(item.getID()).thenReturn(uuid); + when(entity.getRelationships()).thenReturn(relationshipList); + assertEquals("TestGetLeftRelations 0", relationshipList, entityService.getLeftRelations(context, entity)); + } + + @Test + public void testGetRightRelation() { + Item item = mock(Item.class); + UUID uuid = UUID.randomUUID(); + Relationship relationship = mock(Relationship.class); + List relationshipList = new ArrayList<>(); + relationshipList.add(relationship); + Entity entity = mock(Entity.class); + when(entity.getItem()).thenReturn(item); + when(relationship.getRightItem()).thenReturn(item); + when(item.getID()).thenReturn(uuid); + when(entity.getRelationships()).thenReturn(relationshipList); + assertEquals("TestGetLeftRelations 0", relationshipList, entityService.getRightRelations(context, entity)); + } + + @Test + public void testGetRelationsByLabel() throws Exception { + Relationship relationship = mock(Relationship.class); + RelationshipType relationshipType = mock(RelationshipType.class); + relationship.setRelationshipType(relationshipType); + List relationshipList = new ArrayList<>(); + relationshipList.add(relationship); + when(relationshipService.findAll(context)).thenReturn(relationshipList); + when(relationship.getRelationshipType()).thenReturn(relationshipType); + when(relationshipType.getLeftLabel()).thenReturn("leftLabel"); + when(relationshipType.getRightLabel()).thenReturn("rightLabel"); + assertEquals("TestGetRelationsByLabel 0", relationshipList, + entityService.getRelationsByLabel(context, "leftLabel")); + } + + @Test + public void testGetAllRelationshipTypes() throws Exception { + List 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); + Relationship relationship = mock(Relationship.class); + RelationshipType relationshipType = mock(RelationshipType.class); + relationship.setRelationshipType(relationshipType); + relationshipType.setLeftType(leftType); + relationshipType.setLeftType(rightType); + List relationshipTypeList = new ArrayList<>(); + relationshipTypeList.add(relationshipType); + 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); + assertEquals("TestGetAllRelationshipTypes 0", relationshipTypeList, + entityService.getAllRelationshipTypes(context, entity)); + } + + @Test + public void testGetLeftRelationshipTypes() throws Exception { + Item item = mock(Item.class); + Entity entity = mock(Entity.class); + EntityType entityType = mock(EntityType.class); + RelationshipType relationshipType = mock(RelationshipType.class); + List relationshipTypeList = new LinkedList<>(); + relationshipTypeList.add(relationshipType); + List metsList = new ArrayList<>(); + MetadataValue metadataValue = mock(MetadataValue.class); + metsList.add(metadataValue); + 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); + + assertEquals("TestGetLeftRelationshipTypes 0", relationshipTypeList, + entityService.getLeftRelationshipTypes(context, entity)); + } + + @Test + public void testGetRightRelationshipTypes() throws Exception { + Item item = mock(Item.class); + Entity entity = mock(Entity.class); + EntityType entityType = mock(EntityType.class); + RelationshipType relationshipType = mock(RelationshipType.class); + List relationshipTypeList = new LinkedList<>(); + relationshipTypeList.add(relationshipType); + List metsList = new ArrayList<>(); + MetadataValue metadataValue = mock(MetadataValue.class); + metsList.add(metadataValue); + 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); + + assertEquals("TestGetRightRelationshipTypes 0", relationshipTypeList, + entityService.getRightRelationshipTypes(context, entity)); + } + + + @Test + public void testGetRelationshipTypesByLabel() throws Exception { + List list = new LinkedList<>(); + RelationshipType relationshipType = mock(RelationshipType.class); + list.add(relationshipType); + when(relationshipTypeService.findAll(context)).thenReturn(list); + when(relationshipType.getLeftLabel()).thenReturn("leftLabel"); + when(relationshipType.getRightLabel()).thenReturn("rightLabel"); + assertEquals("TestGetRelationshipTypesByLabel 0", list, + entityService.getRelationshipTypesByLabel(context, "leftLabel")); + } + + + + +} diff --git a/dspace-api/src/test/java/org/dspace/content/EntityTypeServiceImplTest.java b/dspace-api/src/test/java/org/dspace/content/EntityTypeServiceImplTest.java new file mode 100644 index 0000000000..b060ad8b5d --- /dev/null +++ b/dspace-api/src/test/java/org/dspace/content/EntityTypeServiceImplTest.java @@ -0,0 +1,103 @@ +/** + * 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.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 { + when(entityTypeDAO.findByEntityType(context, "TestType")).thenReturn(entityType); + assertEquals("TestFindByEntityType 0", entityType, entityTypeService.findByEntityType(context, "TestType")); + } + + + @Test + public void testFindAll() throws Exception { + List entityTypeList = new ArrayList<>(); + when(entityTypeDAO.findAll(context, EntityType.class)).thenReturn(entityTypeList); + assertEquals("TestFindAll 0", entityTypeList, entityTypeService.findAll(context)); + } + + + @Test + public void testCreate() throws Exception { + when(authorizeService.isAdmin(context)).thenReturn(true); + EntityType entityType = new EntityType(); + entityType.setLabel("Test"); + when(entityTypeDAO.create(any(), any())).thenReturn(entityType); + assertEquals("TestCreate 0", entityType.getLabel(), entityTypeService.create(context, "Test").getLabel()); + assertEquals("TestCreate 1", entityType, entityTypeService.create(context)); + } + + @Test + public void testFind() throws Exception { + when(entityTypeDAO.findByID(context, EntityType.class, 0)).thenReturn(entityType); + assertEquals("TestFind 0", entityType, entityTypeService.find(context, 0)); + } + + @Test + public void testUpdate() throws Exception { + EntityType entityTypeTest = mock(EntityType.class); + List entityTypeList = new ArrayList<>(); + entityTypeList.add(entityType); + when(authorizeService.isAdmin(context)).thenReturn(true); + entityTypeService.update(context, entityTypeTest); + entityTypeService.update(context, entityTypeList); + Mockito.verify(entityTypeDAO,times(1)).save(context, entityType); + } + + @Test + public void testDelete() throws Exception { + when(authorizeService.isAdmin(context)).thenReturn(true); + entityTypeService.delete(context, entityType); + Mockito.verify(entityTypeDAO,times(1)).delete(context, entityType); + } + + public EntityType makeEntityType() { + return new EntityType(); + } + + +} diff --git a/dspace-api/src/test/java/org/dspace/content/RelationshipServiceImplTest.java b/dspace-api/src/test/java/org/dspace/content/RelationshipServiceImplTest.java new file mode 100644 index 0000000000..1f6f56f66f --- /dev/null +++ b/dspace-api/src/test/java/org/dspace/content/RelationshipServiceImplTest.java @@ -0,0 +1,287 @@ +/** + * 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 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 { + when(relationshipDAO.findAll(context, Relationship.class)).thenReturn(relationshipsList); + assertEquals("TestFindAll 0", relationshipsList, relationshipService.findAll(context)); + } + + @Test + public void testFindByItem() throws Exception { + List 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); + + + List results = relationshipService.findByItem(context, cindy); + assertEquals("TestFindByItem 0", relationshipTest, results); + } + + @Test + public void testFindLeftPlaceByLeftItem() throws Exception { + Item item = mock(Item.class); + when(relationshipDAO.findLeftPlaceByLeftItem(context, item)).thenReturn(0); + assertEquals("TestFindLeftPlaceByLeftItem 0", relationshipDAO.findLeftPlaceByLeftItem(context, item), + relationshipService.findLeftPlaceByLeftItem(context, item)); + } + + @Test + public void testFindRightPlaceByRightItem() throws Exception { + Item item = mock(Item.class); + when(relationshipDAO.findRightPlaceByRightItem(context, item)).thenReturn(0); + assertEquals("TestFindRightPlaceByRightItem 0", relationshipDAO.findRightPlaceByRightItem(context, item), + relationshipService.findRightPlaceByRightItem(context, item)); + } + + @Test + public void testFindByItemAndRelationshipType() throws Exception { + List relList = new LinkedList<>(); + Item item = mock(Item.class); + RelationshipType testRel = new RelationshipType(); + + assertEquals("TestFindByItemAndRelationshipType 0", relList, + relationshipService.findByItemAndRelationshipType(context, item, testRel, true)); + assertEquals("TestFindByItemAndRelationshipType 1", relList, + relationshipService.findByItemAndRelationshipType(context, item, testRel)); + } + + @Test + public void testFindByRelationshipType() throws Exception { + List relList = new LinkedList<>(); + RelationshipType testRel = new RelationshipType(); + + assertEquals("TestFindByRelationshipType 0", relList, + relationshipService.findByRelationshipType(context, testRel)); + assertEquals("TestFindByRelationshipType 1", relList, + relationshipService.findByRelationshipType(context, testRel)); + } + + @Test + public void find() throws Exception { + Relationship relationship = new Relationship(); + relationship.setId(1337); + when(relationshipDAO.findByID(context, Relationship.class, relationship.getID())).thenReturn(relationship); + assertEquals("TestFind 0", relationship, relationshipService.find(context, relationship.getID())); + } + + @Test + public void testCreate() throws Exception { + Relationship relationship = relationshipDAO.create(context,new Relationship()); + context.turnOffAuthorisationSystem(); + when(authorizeService.isAdmin(context)).thenReturn(true); + assertEquals("TestCreate 0", relationship, relationshipService.create(context)); + MetadataValue metVal = mock(MetadataValue.class); + List metsList = new ArrayList<>(); + List leftTypelist = new ArrayList<>(); + List 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); + 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); + + assertEquals("TestCreate 1", relationship, relationshipService.create(context, relationship)); + assertEquals("TestCreate 2", relationship, relationshipService.create(context, leftItem, rightItem, + testRel,0,0)); + + context.restoreAuthSystemState(); + } + + @Test + public void testDelete() throws Exception { + context.turnOffAuthorisationSystem(); + when(authorizeService.isAdmin(context)).thenReturn(true); + MetadataValue metVal = mock(MetadataValue.class); + List metsList = new ArrayList<>(); + List leftTypelist = new ArrayList<>(); + List 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); + 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); + relationshipService.delete(context, relationship); + Mockito.verify(relationshipDAO).delete(context, relationship); + } + + @Test + public void testUpdate() throws Exception { + context.turnOffAuthorisationSystem(); + when(authorizeService.isAdmin(context)).thenReturn(true); + MetadataValue metVal = mock(MetadataValue.class); + List 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); + 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); + relationshipService.update(context, relationship); + Mockito.verify(relationshipDAO).save(context, 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; + } + + +} diff --git a/dspace-api/src/test/java/org/dspace/content/virtual/CollectedTest.java b/dspace-api/src/test/java/org/dspace/content/virtual/CollectedTest.java new file mode 100644 index 0000000000..3161c08da4 --- /dev/null +++ b/dspace-api/src/test/java/org/dspace/content/virtual/CollectedTest.java @@ -0,0 +1,90 @@ +/** + * 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 fields; + + @Mock + private ItemService itemService; + + @Mock + private Context context; + + @Test + public void testGetFields() { + assertEquals("TestGetFields 0", fields.getClass(), collected.getFields().getClass()); + } + + @Test + public void testSetFields() { + collected.setFields(fields); + assertEquals("TestSetFields 0", fields, collected.getFields()); + + } + + @Test + public void testSetUseForPlace() { + collected.setUseForPlace(true); + assertEquals("TestSetUseForPlace 0", true, collected.getUseForPlace()); + } + + @Test + public void testGetUseForPlace() { + boolean bool = true; + collected.setUseForPlace(true); + assertEquals("TestGetUseForPlace 0", bool, collected.getUseForPlace()); + } + + @Test + public void testGetValues() { + List list = new ArrayList<>(); + List valueList = new ArrayList<>(); + List 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); + 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"); + valueList.add("TestValue"); + assertEquals("TestGetValues 0", valueList, collected.getValues(context, item)); + } +} diff --git a/dspace-api/src/test/java/org/dspace/content/virtual/ConcatenateTest.java b/dspace-api/src/test/java/org/dspace/content/virtual/ConcatenateTest.java new file mode 100644 index 0000000000..40d11da23a --- /dev/null +++ b/dspace-api/src/test/java/org/dspace/content/virtual/ConcatenateTest.java @@ -0,0 +1,107 @@ +/** + * 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 fields; + + @Mock + private ItemService itemService; + + @Mock + private Context context; + + + @Test + public void testGetFields() { + assertEquals("TestGetFields 0", fields.getClass(), concatenate.getFields().getClass()); + } + + @Test + public void testSetFields() { + concatenate.setFields(fields); + assertEquals("TestSetFields 0", fields, concatenate.getFields()); + } + + @Test + public void testGetSeperator() { + String seperator = ","; + concatenate.setSeparator(","); + assertEquals("TestGetSeperator 0", seperator, concatenate.getSeparator()); + } + + @Test + public void testSetSeperator() { + concatenate.setSeparator(","); + assertEquals("TestSetSeperator 0", ",", concatenate.getSeparator()); + } + + @Test + public void testSetUseForPlace() { + concatenate.setUseForPlace(true); + assertEquals("TestSetUseForPlace 0", true, concatenate.getUseForPlace()); + + } + + @Test + public void testGetUseForPlace() { + boolean bool = true; + concatenate.setUseForPlace(true); + assertEquals("TestGetUseForPlace 0", bool, concatenate.getUseForPlace()); + } + + @Test + public void testGetValues() { + List list = new ArrayList<>(); + List valueList = new ArrayList<>(); + List 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); + 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"); + valueList.add("TestValue"); + assertEquals("TestGetValues 0", valueList, concatenate.getValues(context, item)); + } +} diff --git a/dspace-api/src/test/java/org/dspace/content/virtual/EntityTypeToFilterQueryServiceTest.java b/dspace-api/src/test/java/org/dspace/content/virtual/EntityTypeToFilterQueryServiceTest.java new file mode 100644 index 0000000000..df9bf2d32d --- /dev/null +++ b/dspace-api/src/test/java/org/dspace/content/virtual/EntityTypeToFilterQueryServiceTest.java @@ -0,0 +1,58 @@ +/** + * 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() { + Map map = new HashMap(); + map.put("key", "value"); + entityTypeToFilterQueryService.setMap(map); + assertEquals("TestSetMap 0", map, entityTypeToFilterQueryService.getMap()); + } + + @Test + public void testGetMap() { + Map map = Collections.emptyMap(); + entityTypeToFilterQueryService.setMap(map); + assertEquals("TestGetFields 0", map, entityTypeToFilterQueryService.getMap()); + } + + @Test + public void testHasKey() { + Map map = new HashMap(); + map.put("key", "value"); + entityTypeToFilterQueryService.setMap(map); + assertEquals("TestHasKey 0", true, entityTypeToFilterQueryService.hasKey("key")); + } + + @Test + public void testGetFilterQueryForKey() { + Map map = new HashMap(); + map.put("key", "value"); + entityTypeToFilterQueryService.setMap(map); + assertEquals("TestGetFilterQueryForKey 0", "value", + entityTypeToFilterQueryService.getFilterQueryForKey("key")); + } +} diff --git a/dspace-api/src/test/java/org/dspace/content/virtual/RelatedTest.java b/dspace-api/src/test/java/org/dspace/content/virtual/RelatedTest.java new file mode 100644 index 0000000000..b9debe6bc1 --- /dev/null +++ b/dspace-api/src/test/java/org/dspace/content/virtual/RelatedTest.java @@ -0,0 +1,141 @@ +/** + * 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() { + related.setRelationshipTypeString("TestType"); + assertEquals("TestGetRelationshipTypeString 0", "TestType", related.getRelationshipTypeString()); + } + + @Test + public void testSetRelationshipTypeString() { + related.setRelationshipTypeString("TestType"); + assertEquals("TestSetRelationshipTypeString 0", "TestType", related.getRelationshipTypeString()); + } + + @Test + public void testSetPlace() { + related.setPlace(0); + assertTrue("TestSetPlace 0", 0 == related.getPlace()); + } + + @Test + public void testGetPlace() { + related.setPlace(0); + assertTrue("TestGetPlace 0", 0 == related.getPlace()); + } + + @Test + public void testGetVirtualMetadataConfiguration() { + assertEquals("TestGetVirtualMetadataConfiguration 0", virtualMetadataConfiguration.getClass(), + related.getVirtualMetadataConfiguration().getClass()); + } + + @Test + public void testSetVirtualMetadataConfiguration() { + related.setVirtualMetadataConfiguration(virtualMetadataConfiguration); + assertEquals("TestGetVirtualMetadataConfiguration 0", virtualMetadataConfiguration, + related.getVirtualMetadataConfiguration()); + } + + @Test + public void testSetUseForPlace() { + related.setUseForPlace(true); + assertEquals("TestSetVirtualMetadataConfiguration 0", true, related.getUseForPlace()); + } + + @Test + public void testGetUseForPlace() { + related.setUseForPlace(true); + assertEquals("TestSetVirtualMetadataConfiguration 0", true, related.getUseForPlace()); + } + + @Test + public void testGetValues() throws Exception { + List relationshipTypeList = new ArrayList<>(); + List 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); + 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); + + assertEquals("TestGetValues 0", virtualMetadataConfiguration.getValues(context, item), + related.getValues(context, item)); + related.setPlace(1); + assertEquals("TestGetValues 1", virtualMetadataConfiguration.getValues(context, item), + related.getValues(context, item)); + related.setPlace(2); + assertEquals("TestGetValues 2", new LinkedList<>(), related.getValues(context, item)); + } + + +} diff --git a/dspace-api/src/test/java/org/dspace/content/virtual/UUIDValueTest.java b/dspace-api/src/test/java/org/dspace/content/virtual/UUIDValueTest.java new file mode 100644 index 0000000000..428c26d2e5 --- /dev/null +++ b/dspace-api/src/test/java/org/dspace/content/virtual/UUIDValueTest.java @@ -0,0 +1,58 @@ +/** + * 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 { + List list = new LinkedList<>(); + Item item = mock(Item.class); + UUID uuid = UUID.randomUUID(); + when(item.getID()).thenReturn(uuid); + list.add(String.valueOf(uuid)); + assertEquals("TestGetValues 0", list, UUIDValue.getValues(context, item)); + } + + @Test + public void testSetUseForPlace() { + UUIDValue.setUseForPlace(true); + assertEquals("TestSetUseForPlace 0", true, UUIDValue.getUseForPlace()); + + } + + @Test + public void testGetUseForPlace() { + UUIDValue.setUseForPlace(true); + assertEquals("TestGetUseForPlace 0", true, UUIDValue.getUseForPlace()); + } +} diff --git a/dspace-api/src/test/java/org/dspace/content/virtual/VirtualMetadataPopulatorTest.java b/dspace-api/src/test/java/org/dspace/content/virtual/VirtualMetadataPopulatorTest.java new file mode 100644 index 0000000000..34a693bc6e --- /dev/null +++ b/dspace-api/src/test/java/org/dspace/content/virtual/VirtualMetadataPopulatorTest.java @@ -0,0 +1,69 @@ +/** + * 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() { + Map> map = new HashMap<>(); + HashMap mapExt = new HashMap<>(); + VirtualMetadataConfiguration virtualMetadataConfiguration = mock(VirtualMetadataConfiguration.class); + mapExt.put("hashKey", virtualMetadataConfiguration); + map.put("key", mapExt); + virtualMetadataPopulator.setMap(map); + assertEquals("TestSetMap 0", map, virtualMetadataPopulator.getMap()); + } + + @Test + public void testGetMap() { + Map> map = new HashMap<>(); + HashMap mapExt = new HashMap<>(); + VirtualMetadataConfiguration virtualMetadataConfiguration = mock(VirtualMetadataConfiguration.class); + mapExt.put("hashKey", virtualMetadataConfiguration); + map.put("key", mapExt); + virtualMetadataPopulator.setMap(map); + assertEquals("TestGetMap 0", map, virtualMetadataPopulator.getMap()); + } + + @Test + public void testIsUseForPlaceTrueForRelationshipType() { + RelationshipType relationshipType = mock(RelationshipType.class); + Map> map = new HashMap<>(); + HashMap mapExt = new HashMap<>(); + VirtualMetadataConfiguration virtualMetadataConfiguration = mock(VirtualMetadataConfiguration.class); + mapExt.put("hashKey", virtualMetadataConfiguration); + map.put("LeftLabel", mapExt); + map.put("NotRightLabel", mapExt); + virtualMetadataPopulator.setMap(map); + when(virtualMetadataConfiguration.getUseForPlace()).thenReturn(true); + when(relationshipType.getLeftLabel()).thenReturn("LeftLabel"); + when(relationshipType.getRightLabel()).thenReturn("RightLabel"); + assertEquals("TestGetFields 0", false, + virtualMetadataPopulator.isUseForPlaceTrueForRelationshipType(relationshipType, false)); + assertEquals("TestGetFields 1", true, + virtualMetadataPopulator.isUseForPlaceTrueForRelationshipType(relationshipType, true)); + } +}