diff --git a/dspace-api/src/test/java/org/dspace/content/EntityServiceImplTest.java b/dspace-api/src/test/java/org/dspace/content/EntityServiceImplTest.java index 642e1f8849..fb8e1c6ab3 100644 --- a/dspace-api/src/test/java/org/dspace/content/EntityServiceImplTest.java +++ b/dspace-api/src/test/java/org/dspace/content/EntityServiceImplTest.java @@ -58,20 +58,26 @@ public class EntityServiceImplTest { @Test public void testfindByItemId() throws Exception { + // Declare objects utilized in unit test Item item = mock(Item.class); List 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); @@ -79,71 +85,96 @@ public class EntityServiceImplTest { 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 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 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 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 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); + + // Currently this unit test will only test one case with one relationshipType List 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); @@ -154,22 +185,30 @@ public class EntityServiceImplTest { when(entityTypeService.findByEntityType(context, "value")).thenReturn(leftType); when(leftType.getID()).thenReturn(0); when(rightType.getID()).thenReturn(1); - when(entityService.getType(context, entity)).thenReturn(leftType); + 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 relationshipTypeList = new LinkedList<>(); relationshipTypeList.add(relationshipType); List 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); @@ -178,21 +217,28 @@ public class EntityServiceImplTest { 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 relationshipTypeList = new LinkedList<>(); relationshipTypeList.add(relationshipType); List 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); @@ -201,6 +247,7 @@ public class EntityServiceImplTest { 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)); } @@ -208,12 +255,18 @@ public class EntityServiceImplTest { @Test public void testGetRelationshipTypesByLabel() throws Exception { + // Declare objects utilized in unit test List 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")); } diff --git a/dspace-api/src/test/java/org/dspace/content/EntityTypeServiceImplTest.java b/dspace-api/src/test/java/org/dspace/content/EntityTypeServiceImplTest.java index b060ad8b5d..35cb147aa8 100644 --- a/dspace-api/src/test/java/org/dspace/content/EntityTypeServiceImplTest.java +++ b/dspace-api/src/test/java/org/dspace/content/EntityTypeServiceImplTest.java @@ -9,7 +9,6 @@ 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; @@ -48,53 +47,87 @@ public class EntityTypeServiceImplTest { @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 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 { - EntityType entityTypeTest = mock(EntityType.class); + // Declare objects utilized in unit test List entityTypeList = new ArrayList<>(); entityTypeList.add(entityType); + + // Mock admin state when(authorizeService.isAdmin(context)).thenReturn(true); - entityTypeService.update(context, entityTypeTest); + + // Invoke both impls of method update() + entityTypeService.update(context, entityType); entityTypeService.update(context, entityTypeList); - Mockito.verify(entityTypeDAO,times(1)).save(context, entityType); + + // 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(); } diff --git a/dspace-api/src/test/java/org/dspace/content/RelationshipServiceImplTest.java b/dspace-api/src/test/java/org/dspace/content/RelationshipServiceImplTest.java index 1f6f56f66f..47a1a0b473 100644 --- a/dspace-api/src/test/java/org/dspace/content/RelationshipServiceImplTest.java +++ b/dspace-api/src/test/java/org/dspace/content/RelationshipServiceImplTest.java @@ -65,12 +65,15 @@ public class RelationshipServiceImplTest { @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 relationshipTest = new ArrayList<>(); Item cindy = mock(Item.class); Item fred = mock(Item.class); @@ -96,63 +99,89 @@ public class RelationshipServiceImplTest { 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 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 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 relList = new LinkedList<>(); RelationshipType testRel = new RelationshipType(); + // The Relationship(s) reported should match our our relList assertEquals("TestFindByRelationshipType 0", relList, relationshipService.findByRelationshipType(context, testRel)); - assertEquals("TestFindByRelationshipType 1", 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(); - when(authorizeService.isAdmin(context)).thenReturn(true); assertEquals("TestCreate 0", relationship, relationshipService.create(context)); MetadataValue metVal = mock(MetadataValue.class); List metsList = new ArrayList<>(); @@ -171,6 +200,8 @@ public class RelationshipServiceImplTest { 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 @@ -189,7 +220,10 @@ public class RelationshipServiceImplTest { 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)); @@ -198,8 +232,10 @@ public class RelationshipServiceImplTest { @Test public void testDelete() throws Exception { - context.turnOffAuthorisationSystem(); + // Mock admin state when(authorizeService.isAdmin(context)).thenReturn(true); + + // Declare objects utilized in unit test MetadataValue metVal = mock(MetadataValue.class); List metsList = new ArrayList<>(); List leftTypelist = new ArrayList<>(); @@ -219,6 +255,8 @@ public class RelationshipServiceImplTest { 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)) @@ -237,14 +275,21 @@ public class RelationshipServiceImplTest { 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 metsList = new ArrayList<>(); Item leftItem = mock(Item.class); @@ -260,16 +305,31 @@ public class RelationshipServiceImplTest { 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(); diff --git a/dspace-api/src/test/java/org/dspace/content/RelationshipTypeTest.java b/dspace-api/src/test/java/org/dspace/content/RelationshipTypeTest.java index 956548884d..569fc3697c 100644 --- a/dspace-api/src/test/java/org/dspace/content/RelationshipTypeTest.java +++ b/dspace-api/src/test/java/org/dspace/content/RelationshipTypeTest.java @@ -44,11 +44,11 @@ public class RelationshipTypeTest { private Context context; - @Before public void init() { + // Default state of firstRelationshipType firstRelationshipType = mock(RelationshipType.class); - firstRelationshipType.setId(new Random().nextInt()); + firstRelationshipType.setId(1); firstRelationshipType.setLeftType(mock(EntityType.class)); firstRelationshipType.setRightType(mock(EntityType.class)); firstRelationshipType.setLeftLabel("isAuthorOfPublication"); @@ -58,6 +58,7 @@ public class RelationshipTypeTest { firstRelationshipType.setRightMinCardinality(0); firstRelationshipType.setRightMinCardinality(null); + // Default state of secondRelationshipType secondRelationshipType = mock(RelationshipType.class); secondRelationshipType.setId(new Random().nextInt()); secondRelationshipType.setLeftType(mock(EntityType.class)); @@ -68,44 +69,67 @@ public class RelationshipTypeTest { 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); - RelationshipType found = relationshipTypeService.find(context, new Random().nextInt()); + + // 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); + .thenReturn(firstRelationshipType); + + // Declare objects utilized for this test RelationshipType found = relationshipTypeService.findbyTypesAndLabels(context, mock(EntityType.class), - mock(EntityType.class), - "mock", "mock"); + 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 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 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 mockedList = new LinkedList<>(); mockedList.add(firstRelationshipType); + + // Mock DAO to return our mockedList when(relationshipTypeDAO.findByLeftOrRightLabel(any(), any())).thenReturn(mockedList); + + // Invoke findByLeftOrRightLabel() List 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); @@ -113,15 +137,27 @@ public class RelationshipTypeTest { @Test public void testRelationshipTypefindByEntityType() throws Exception { + // Declare objects utilized for this test List mockedList = new LinkedList<>(); mockedList.add(firstRelationshipType); + + // Mock DAO to return our mockedList when(relationshipTypeDAO.findByEntityType(any(), any())).thenReturn(mockedList); + + // Invoke findByEntityType() List 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())); 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 index 3161c08da4..39f64cbe7d 100644 --- a/dspace-api/src/test/java/org/dspace/content/virtual/CollectedTest.java +++ b/dspace-api/src/test/java/org/dspace/content/virtual/CollectedTest.java @@ -41,31 +41,42 @@ public class CollectedTest { @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 list = new ArrayList<>(); List valueList = new ArrayList<>(); List metadataValueList = new ArrayList<>(); @@ -76,6 +87,9 @@ public class CollectedTest { 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] : @@ -84,7 +98,8 @@ public class CollectedTest { null, Item.ANY, false)).thenReturn(metadataValueList); when(metadataValue.getValue()).thenReturn("TestValue"); - valueList.add("TestValue"); + + // The reported value(s) should match our valueList 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 index 40d11da23a..2380ddbe58 100644 --- a/dspace-api/src/test/java/org/dspace/content/virtual/ConcatenateTest.java +++ b/dspace-api/src/test/java/org/dspace/content/virtual/ConcatenateTest.java @@ -45,44 +45,60 @@ public class ConcatenateTest { @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 list = new ArrayList<>(); List valueList = new ArrayList<>(); List metadataValueList = new ArrayList<>(); @@ -93,6 +109,9 @@ public class ConcatenateTest { 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] : @@ -101,7 +120,9 @@ public class ConcatenateTest { null, Item.ANY, false)).thenReturn(metadataValueList); when(metadataValue.getValue()).thenReturn("TestValue"); - valueList.add("TestValue"); + + + // The reported values should match our defined valueList 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 index df9bf2d32d..8b856dd6c1 100644 --- a/dspace-api/src/test/java/org/dspace/content/virtual/EntityTypeToFilterQueryServiceTest.java +++ b/dspace-api/src/test/java/org/dspace/content/virtual/EntityTypeToFilterQueryServiceTest.java @@ -26,32 +26,44 @@ public class EntityTypeToFilterQueryServiceTest { @Test public void testSetMap() { + // Setup objects utilized in unit test Map map = new HashMap(); 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 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 map = new HashMap(); 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 map = new HashMap(); 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")); } 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 index b9debe6bc1..535a2db3ec 100644 --- a/dspace-api/src/test/java/org/dspace/content/virtual/RelatedTest.java +++ b/dspace-api/src/test/java/org/dspace/content/virtual/RelatedTest.java @@ -52,55 +52,71 @@ public class RelatedTest { @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 relationshipTypeList = new ArrayList<>(); List relationshipList = new ArrayList<>(); Relationship relationship = mock(Relationship.class); @@ -112,6 +128,8 @@ public class RelatedTest { 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"); @@ -128,12 +146,15 @@ public class RelatedTest { 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)); } 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 index 428c26d2e5..4312131997 100644 --- a/dspace-api/src/test/java/org/dspace/content/virtual/UUIDValueTest.java +++ b/dspace-api/src/test/java/org/dspace/content/virtual/UUIDValueTest.java @@ -35,24 +35,33 @@ public class UUIDValueTest { @Test public void testGetValues() throws Exception { + // Setup objects utilized in unit test List 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()); } } 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 index 34a693bc6e..5fd4f7934a 100644 --- a/dspace-api/src/test/java/org/dspace/content/virtual/VirtualMetadataPopulatorTest.java +++ b/dspace-api/src/test/java/org/dspace/content/virtual/VirtualMetadataPopulatorTest.java @@ -28,28 +28,35 @@ public class VirtualMetadataPopulatorTest { @Test public void testSetMap() { + // Setup objects utilized in unit test Map> map = new HashMap<>(); HashMap 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> map = new HashMap<>(); HashMap 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> map = new HashMap<>(); HashMap mapExt = new HashMap<>(); @@ -58,11 +65,17 @@ public class VirtualMetadataPopulatorTest { 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)); }