use of org.orcid.persistence.dao.WorkDao in project ORCID-Source by ORCID.
the class WorkManagerTest method testCreateNewWorkGroup.
@Test
public void testCreateNewWorkGroup() {
WorkDao mockDao = Mockito.mock(WorkDao.class);
WorkEntityCacheManager cacheManager = Mockito.mock(WorkEntityCacheManager.class);
WorkEntityCacheManager oldCacheManager = (WorkEntityCacheManager) ReflectionTestUtils.getField(workManager, "workEntityCacheManager");
ReflectionTestUtils.setField(workManager, "workDao", mockDao);
ReflectionTestUtils.setField(workManager, "workEntityCacheManager", cacheManager);
// no work where user is source
List<MinimizedWorkEntity> works = getMinimizedWorksListForGrouping();
Mockito.when(cacheManager.retrieveMinimizedWorks(Mockito.anyString(), Mockito.anyList(), Mockito.anyLong())).thenReturn(works);
// full work matching user preferred id should be loaded from db (10 is highest display index)
Mockito.when(mockDao.find(Mockito.eq(4l))).thenReturn(getUserPreferredWork());
workManager.createNewWorkGroup(Arrays.asList(1l, 2l, 3l, 4l), "some-orcid");
// as no work with user as source, new work should be created
ArgumentCaptor<WorkEntity> captor = ArgumentCaptor.forClass(WorkEntity.class);
Mockito.verify(mockDao).persist(captor.capture());
WorkEntity entity = captor.getValue();
assertEquals("{\"workExternalIdentifier\":[{\"relationship\":null,\"url\":null,\"workExternalIdentifierType\":\"AGR\",\"workExternalIdentifierId\":{\"content\":\"123\"}},{\"relationship\":null,\"url\":null,\"workExternalIdentifierType\":\"DOI\",\"workExternalIdentifierId\":{\"content\":\"doi:10.1/123\"}}]}", entity.getExternalIdentifiersJson());
assertEquals("some title", entity.getTitle());
assertEquals("some subtitle", entity.getSubtitle());
// now test where user is source of one work
works = getMinimizedWorksListForGrouping();
MinimizedWorkEntity userSource = works.get(0);
userSource.setSourceId("some-orcid");
Mockito.when(cacheManager.retrieveMinimizedWorks(Mockito.anyString(), Mockito.anyList(), Mockito.anyLong())).thenReturn(works);
// full work matching user preferred id should be loaded from db (10 is highest display index)
Mockito.when(mockDao.find(Mockito.eq(4l))).thenReturn(getUserPreferredWork());
workManager.createNewWorkGroup(Arrays.asList(1l, 2l, 3l, 4l), "some-orcid");
// no new work should be created
Mockito.verify(mockDao, Mockito.times(1)).persist(Mockito.any(WorkEntity.class));
assertEquals("{\"workExternalIdentifier\":[{\"relationship\":null,\"url\":null,\"workExternalIdentifierType\":\"AGR\",\"workExternalIdentifierId\":{\"content\":\"123\"}},{\"relationship\":null,\"url\":null,\"workExternalIdentifierType\":\"DOI\",\"workExternalIdentifierId\":{\"content\":\"doi:10.1/123\"}}]}", userSource.getExternalIdentifiersJson());
// only identifiers should have been updated
assertEquals("work:title", userSource.getTitle());
assertEquals("work:subtitle", userSource.getSubtitle());
// reset dao
ReflectionTestUtils.setField(workManager, "workDao", workDao);
ReflectionTestUtils.setField(workManager, "workEntityCacheManager", oldCacheManager);
}
Aggregations