### Question:
DeploymentVerifier { void ensureGroups(final ServiceElement serviceElement) throws IOException { if (serviceElement.getServiceBeanConfig().getGroups()==DiscoveryGroupManagement.ALL_GROUPS) { throw new IOException(String.format("Service %s has been declared for ALL_GROUPS", serviceElement.getName())); } for(ServiceRegistrar registrar : discoveryManagement.getRegistrars()) { try { List<String> toAdd = new ArrayList<>(); DiscoveryAdmin admin = (DiscoveryAdmin) ((Administrable)registrar).getAdmin(); String[] knownGroups = admin.getMemberGroups(); for(String serviceGroup : serviceElement.getServiceBeanConfig().getGroups()) { boolean found = false; for(String known : knownGroups) { if (serviceGroup.equals(known)) { found = true; break; } } if (!found) { toAdd.add(serviceGroup); } } if (!toAdd.isEmpty()) { admin.addMemberGroups(toAdd.toArray(new String[0])); if (logger.isDebugEnabled()) { logger.debug("Added {} to ServiceRegistrar at {}:{}", toAdd, registrar.getLocator().getHost(), registrar.getLocator().getPort()); } } } catch (RemoteException e) { logger.warn("While trying to ensure groups", e); } } } DeploymentVerifier(final Configuration config, final DiscoveryManagement discoveryManagement); void verifyDeploymentRequest(final DeployRequest request); void verifyOperationalString(final OperationalString opString, final RemoteRepository[] repositories); void verifyOperationalStringService(final ServiceElement service,
                                               final Resolver resolver,
                                               final RemoteRepository[] repositories);  }### Answer:
@Test public void testEnsureGroups() throws Exception { startLookup("foo"); Assert.assertFalse(hasGroup(registrar, "gack")); ServiceElement service = createSE("gack"); Assert.assertTrue(waitForDiscovery(listener)); Assert.assertTrue(listener.discovered.get()); DL newDiscoListener = new DL(); new LookupDiscoveryManager(new String[]{"gack"}, null, newDiscoListener); deploymentVerifier.ensureGroups(service); Assert.assertTrue(waitForDiscovery(newDiscoListener)); Assert.assertTrue(hasGroup(registrar, "gack")); }
@Test public void testEnsureMultiGroups() throws Exception { startLookup("foo"); Assert.assertTrue(waitForDiscovery(listener)); Assert.assertFalse(hasGroup(registrar, "gack")); Assert.assertFalse(hasGroup(registrar, "blutarsky")); ServiceElement service = createSE("gack", "blutarsky"); Assert.assertTrue(listener.discovered.get()); DL newDiscoListener = new DL(); new LookupDiscoveryManager(new String[]{"gack"}, null, newDiscoListener); deploymentVerifier.ensureGroups(service); Assert.assertTrue(waitForDiscovery(newDiscoListener)); Assert.assertTrue(hasGroup(registrar, "gack")); Assert.assertTrue(hasGroup(registrar, "blutarsky")); }
@Test(expected = IOException.class) public void testAllGroupsFail() throws Exception { ServiceElement service = new ServiceElement(); ServiceBeanConfig serviceBeanConfig = new ServiceBeanConfig(); serviceBeanConfig.setGroups("all"); service.setServiceBeanConfig(serviceBeanConfig); deploymentVerifier.ensureGroups(service); }