### Question:
OriginResolver implements Function<UUID, OriginEntity> { @Override public OriginEntity apply(UUID id) { OriginEntity origin = originManager.getOrigin(id); if (origin == null) return null; if (origin.getType() == OriginEntity.Type.Group) return origin; Subject user = subjectResolver.resolveSubject(origin.getId()); if (user == null) return origin; return updateFields(origin, user); } @Inject  OriginResolver(OriginManager originManager, SubjectResolver subjectResolver); @Override OriginEntity apply(UUID id);  }### Answer:
@Test public void testApplyNonExistingOrigin() { UUID id = UUID.randomUUID(); assertNull(resolver.apply(id)); verify(originManager).getOrigin(id); }
@Test public void testApplyFetchGroupOrigin() { when(originManager.getOrigin(groupOrigin.getId())).thenReturn(groupOrigin); assertSame(groupOrigin, resolver.apply(groupOrigin.getId())); verify(originManager).getOrigin(groupOrigin.getId()); verifyNoMoreInteractions(originManager); verifyZeroInteractions(subjectResolver); }
@Test public void testApplyFetchUserOriginWithoutUpdate() { when(originManager.getOrigin(userOrigin.getId())).thenReturn(userOrigin); assertSame(userOrigin, resolver.apply(userOrigin.getId())); verify(originManager).getOrigin(userOrigin.getId()); verify(subjectResolver).resolveSubject(userOrigin.getId()); verifyNoMoreInteractions(originManager); }
@Test public void testApplyFetchUserOriginWithUpdate() { Subject userSubject = Subject.builder() .setId(userOrigin.getId()) .setName("updatedName") .setOrganization(Organization.builder().setId(UUID.randomUUID()).build().toInfo()) .build(); when(originManager.getOrigin(userOrigin.getId())).thenReturn(userOrigin); when(subjectResolver.resolveSubject(userOrigin.getId())).thenReturn(userSubject); resolver.apply(userOrigin.getId()); verify(originManager).saveOrigin(argThat(entity -> { assertEquals(userSubject.getName(), entity.getName()); assertEquals(userSubject.getOrganization().getId(), entity.getOrganizationID()); return true; })); }
@Test public void testApplyFetchUserOriginSkipUpdateFields() { Subject userSubject = Subject.builder() .setId(userOrigin.getId()) .setName(" ") .build(); when(originManager.getOrigin(userOrigin.getId())).thenReturn(userOrigin); when(subjectResolver.resolveSubject(userOrigin.getId())).thenReturn(userSubject); resolver.apply(userOrigin.getId()); verify(originManager, never()).saveOrigin(any()); }
@Test public void testApplyFetchUserOriginSkipUpdateNameNA() { Subject userSubject = Subject.builder() .setId(userOrigin.getId()) .setName("N/A") .build(); when(originManager.getOrigin(userOrigin.getId())).thenReturn(userOrigin); when(subjectResolver.resolveSubject(userOrigin.getId())).thenReturn(userSubject); resolver.apply(userOrigin.getId()); verify(originManager, never()).saveOrigin(any()); }
@Test public void testApplyFetchUserOriginSkipUpdateNoChanges() { Subject userSubject = Subject.builder() .setId(userOrigin.getId()) .setName(userOrigin.getName()) .setOrganization(Organization.builder().setId(userOrigin.getOrganizationID()).build().toInfo()) .build(); when(originManager.getOrigin(userOrigin.getId())).thenReturn(userOrigin); when(subjectResolver.resolveSubject(userOrigin.getId())).thenReturn(userSubject); resolver.apply(userOrigin.getId()); verify(originManager, never()).saveOrigin(any()); }
@Test public void testApplyFetchUserOriginSkipUpdateNameFromOtherOrigin() { OriginEntity otherOrigin = new OriginEntity() .setId(UUID.randomUUID()) .setName("otherOrigin"); Subject userSubject = Subject.builder() .setId(userOrigin.getId()) .setName(otherOrigin.getName()) .build(); when(originManager.getOrigin(userOrigin.getId())).thenReturn(userOrigin); when(originManager.getOrigin(otherOrigin.getName())).thenReturn(otherOrigin); when(subjectResolver.resolveSubject(userOrigin.getId())).thenReturn(userSubject); resolver.apply(userOrigin.getId()); verify(originManager, never()).saveOrigin(any()); }