TMTextUnitIntegrityCheckService { public void checkTMTextUnitIntegrity(Long tmTextUnitId, String contentToCheck) throws IntegrityCheckException { logger.debug("Checking Integrity of the TMTextUnit"); TMTextUnit tmTextUnit = tmTextUnitRepository.findById(tmTextUnitId).orElse(null); Asset asset = tmTextUnit.getAsset(); Set<TextUnitIntegrityChecker> textUnitCheckers = integrityCheckerFactory.getTextUnitCheckers(asset); if (textUnitCheckers.isEmpty()) { logger.debug("No designated checker for this asset. Nothing to do"); } else { for (TextUnitIntegrityChecker textUnitChecker : textUnitCheckers) { textUnitChecker.check(tmTextUnit.getContent(), contentToCheck); } } }  void checkTMTextUnitIntegrity(Long tmTextUnitId, String contentToCheck);  }
@Test public void testCheckTMTextUnitIntegrityWillRunThroughIfAssetHasNoIntegrityChecker() { Asset asset = Mockito.mock(Asset.class); TMTextUnit tmTextUnit = Mockito.mock(TMTextUnit.class); Mockito.when(tmTextUnit.getAsset()).thenReturn(asset); Mockito.when(tmTextUnitRepository.findById(Mockito.anyLong())).thenReturn(Optional.of(tmTextUnit)); Mockito.when(integrityCheckerFactory.getTextUnitCheckers(asset)).thenReturn(new HashSet<TextUnitIntegrityChecker>()); integrityCheckService.checkTMTextUnitIntegrity(1L, "string to check"); Mockito.verify(integrityCheckerFactory, Mockito.times(1)).getTextUnitCheckers(asset); }
@Test public void testCheckTMTextUnitIntegrityWillRunThroughAndCheck() { Asset asset = Mockito.mock(Asset.class); TMTextUnit tmTextUnit = Mockito.mock(TMTextUnit.class); Mockito.when(tmTextUnit.getContent()).thenReturn("some content"); TextUnitIntegrityChecker checker = Mockito.mock(TextUnitIntegrityChecker.class); Mockito.when(tmTextUnit.getAsset()).thenReturn(asset); Mockito.when(tmTextUnitRepository.findById(Mockito.anyLong())).thenReturn(Optional.of(tmTextUnit)); Mockito.when(integrityCheckerFactory.getTextUnitCheckers(asset)).thenReturn(Sets.newHashSet(checker)); integrityCheckService.checkTMTextUnitIntegrity(1L, "string to check"); Mockito.verify(checker, Mockito.times(1)).check(Mockito.anyString(), Mockito.anyString()); }
@Test(expected = IntegrityCheckException.class) public void testCheckTMTextUnitIntegrityWillThrowWhenCheckFails() { Asset asset = Mockito.mock(Asset.class); TMTextUnit tmTextUnit = Mockito.mock(TMTextUnit.class); Mockito.when(tmTextUnit.getContent()).thenReturn("some content"); TextUnitIntegrityChecker checker = Mockito.mock(TextUnitIntegrityChecker.class); Mockito.when(tmTextUnit.getAsset()).thenReturn(asset); Mockito.when(tmTextUnitRepository.findById(Mockito.anyLong())).thenReturn(Optional.of(tmTextUnit)); Mockito.when(integrityCheckerFactory.getTextUnitCheckers(asset)).thenReturn(Sets.newHashSet(checker)); Mockito.doThrow(new IntegrityCheckException("bad")).when(checker).check(Mockito.anyString(), Mockito.anyString()); integrityCheckService.checkTMTextUnitIntegrity(1L, "string to check"); Mockito.verify(checker, Mockito.times(1)).check(Mockito.anyString(), Mockito.anyString()); }