### Question:
ExternalGroupsRepository implements Repository { @Override public CheckpointCloseableIterable<IdentityGroup> listGroups(byte[] checkpoint) throws IOException { ExternalGroups groups = groupsReader.getGroups(); ImmutableList.Builder<IdentityGroup> groupsBuilder = ImmutableList.builder(); for (ExternalGroups.ExternalGroup group : groups.getExternalGroups()) { ImmutableSet.Builder<Membership> membersBuilder = ImmutableSet.builder(); for (ExternalGroups.MemberKey memberKey : group.getMembers()) { EntityKey entityKey = new EntityKey().setId(memberKey.getId()); if (memberKey.getNamespace() != null) { entityKey.setNamespace(memberKey.getNamespace()); } else if (memberKey.getReferenceIdentitySourceName() != null) { String sourceName = memberKey.getReferenceIdentitySourceName(); IdentitySourceConfiguration config = IdentitySourceConfiguration .getReferenceIdentitySourcesFromConfiguration().get(sourceName); if (config == null) { throw new InvalidConfigurationException( "Missing config for reference identity source " + sourceName); } entityKey.setNamespace(config.getGroupNamespace()); } membersBuilder.add(new Membership() .setPreferredMemberKey(entityKey) .setRoles(Collections.singletonList(new MembershipRole().setName("MEMBER"))) ); } groupsBuilder.add( repositoryContext.buildIdentityGroup(group.getName(), membersBuilder::build)); } return new CheckpointCloseableIterableImpl.Builder<IdentityGroup>(groupsBuilder.build()) .build(); } ExternalGroupsRepository(); @VisibleForTesting  ExternalGroupsRepository(GroupsReader groupsReader); @Override void init(RepositoryContext context); @Override CheckpointCloseableIterable<IdentityGroup> listGroups(byte[] checkpoint); @Override CheckpointCloseableIterable<IdentityUser> listUsers(byte[] checkpoint); @Override void close();  }### Answer:
@Test public void listGroups_missingFileConfig_throwsException() throws IOException { Properties config = new Properties(); config.put("api.identitySourceId", "test-identitySourceId"); setupConfig.initConfig(config); RepositoryContext repositoryContext = RepositoryContext.fromConfiguration(); ExternalGroupsRepository groupsRepository = new ExternalGroupsRepository(); thrown.expect(InvalidConfigurationException.class); groupsRepository.listGroups(null); }
@Test public void listGroups_invalidFileConfig_throwsException() throws IOException { Properties config = new Properties(); config.put("api.identitySourceId", "test-identitySourceId"); config.put("externalgroups.filename", "no-such-file"); setupConfig.initConfig(config); RepositoryContext repositoryContext = RepositoryContext.fromConfiguration(); ExternalGroupsRepository groupsRepository = new ExternalGroupsRepository(); thrown.expect(InvalidConfigurationException.class); groupsRepository.listGroups(null); }
@Test public void listGroups_emptyIterable() throws Exception { ExternalGroupsRepository groupsRepository = new ExternalGroupsRepository(() -> fromString("{}")); try (CheckpointCloseableIterable<IdentityGroup> iterable = groupsRepository.listGroups(null)) { assertNull(iterable.getCheckpoint()); assertFalse(iterable.hasMore()); Iterator<IdentityGroup> iterator = iterable.iterator(); assertNotNull(iterator); assertFalse(iterator.hasNext()); } }