### Question:
AuthorityService implements IAuthorityService { @Override public List<IAuthorityEntry> findByUri(IUser user, String uri) { List<IAuthorityEntry> results = entryRepository.findByUsernameAndUriOrderByName(user.getUsername(), uri); if (uri.endsWith("/")) { uri = uri.substring(0, uri.length() - 1); } else { uri = uri + "/"; } results.addAll(entryRepository.findByUsernameAndUriOrderByName(user.getUsername(), uri)); return results; }  @Override void register(AuthorityImporter importer); @Override IAuthorityEntry importAuthority(String uri); @Override IAuthorityEntry find(String id); @Override List<IAuthorityEntry> findByUri(IUser user, String uri); @Override Set<IAuthorityEntry> findByUriInDataset(String uri, String citationGroupId); @Override boolean deleteAuthority(String id); @Override List<IAuthorityEntry> getAll(IUser user); @Override IAuthorityEntry create(IAuthorityEntry entry, IUser user); @Override IAuthorityEntry save(IAuthorityEntry entry);  }### Answer:
@Test public void test_findByUri_uriExists() { String username = "user"; IUser user = new User(); user.setUsername(username); String uri = "http: String uriNoSlash = "http: IAuthorityEntry entry = new AuthorityEntry(); entry.setUri(uri); List<IAuthorityEntry> entries = new ArrayList<>(); entries.add(entry); Mockito.when(entryRepository.findByUsernameAndUriOrderByName(username, uri)).thenReturn(entries); Mockito.when(entryRepository.findByUsernameAndUriOrderByName(username, uriNoSlash)) .thenReturn(new ArrayList<>()); List<IAuthorityEntry> actual = managerToTest.findByUri(user, uri); Assert.assertEquals(entries, actual); }
@Test public void test_findByUri_uriExistsNoSlash() { String username = "user"; IUser user = new User(); user.setUsername(username); String uri = "http: String uriNoSlash = "http: IAuthorityEntry entry = new AuthorityEntry(); entry.setUri(uriNoSlash); List<IAuthorityEntry> entries = new ArrayList<>(); entries.add(entry); Mockito.when(entryRepository.findByUsernameAndUriOrderByName(username, uriNoSlash)).thenReturn(entries); Mockito.when(entryRepository.findByUsernameAndUriOrderByName(username, uri)).thenReturn(new ArrayList<>()); List<IAuthorityEntry> actual = managerToTest.findByUri(user, uri); Assert.assertEquals(entries, actual); }
@Test public void test_findByUri_uriExistsSlashAndNoSlash() { String username = "user"; IUser user = new User(); user.setUsername(username); String uri = "http: String uriNoSlash = "http: IAuthorityEntry entry = new AuthorityEntry(); entry.setUri(uri); List<IAuthorityEntry> entries = new ArrayList<>(); entries.add(entry); IAuthorityEntry entryNoSlash = new AuthorityEntry(); entryNoSlash.setUri(uriNoSlash); List<IAuthorityEntry> entriesNoSlash = new ArrayList<>(); entriesNoSlash.add(entryNoSlash); Mockito.when(entryRepository.findByUsernameAndUriOrderByName(username, uri)).thenReturn(entries); Mockito.when(entryRepository.findByUsernameAndUriOrderByName(username, uriNoSlash)).thenReturn(entriesNoSlash); List<IAuthorityEntry> actual = managerToTest.findByUri(user, uri); Assert.assertEquals(2, actual.size()); Assert.assertTrue(actual.containsAll(entries)); Assert.assertTrue(actual.containsAll(entriesNoSlash)); }
@Test public void test_findByUri_noResults() { String username = "user"; IUser user = new User(); user.setUsername(username); String uri = "http: String uriNoSlash = "http: Mockito.when(entryRepository.findByUsernameAndUriOrderByName(username, uriNoSlash)) .thenReturn(new ArrayList<>()); Mockito.when(entryRepository.findByUsernameAndUriOrderByName(username, uri)).thenReturn(new ArrayList<>()); List<IAuthorityEntry> actual = managerToTest.findByUri(user, uri); Assert.assertTrue(actual.isEmpty()); }