### Question:
ClusterLayoutUpdater { public ClusterLayoutTracker addServicesToCluster(Cluster cluster, Set<Node> clusterNodes, Set<String> servicesToAdd) throws Exception { Preconditions.checkArgument(cluster != null, "Cannot add services to a nonexistant cluster."); Preconditions.checkArgument(clusterNodes != null && !clusterNodes.isEmpty(), "Cannot add services to nonexistant nodes."); Constraints clusterConstraints = cluster.getClusterTemplate().getConstraints(); ClusterLayout clusterLayout = ClusterLayout.fromNodes(clusterNodes, clusterConstraints); Set<String> servicesToAddCopy = Sets.newHashSet(servicesToAdd); SortedSet<Map.Entry<String, ServiceConstraint>> sortedConstraints = Sets.newTreeSet(serviceComparator); sortedConstraints.addAll(clusterConstraints.getServiceConstraints().entrySet()); Queue<String> sortedServices = Lists.newLinkedList(); for (Map.Entry<String, ServiceConstraint> entry : sortedConstraints) { if (servicesToAddCopy.contains(entry.getKey())) { sortedServices.add(entry.getKey()); servicesToAddCopy.remove(entry.getKey()); } } sortedServices.addAll(servicesToAddCopy); ClusterLayoutTracker tracker = new ClusterLayoutTracker(clusterLayout); return canAddServicesToCluster(tracker, sortedServices) ? tracker : null; }  ClusterLayoutTracker addServicesToCluster(Cluster cluster, Set<Node> clusterNodes,
                                                   Set<String> servicesToAdd);  }### Answer:
@Test public void testAddService() throws Exception { Multiset<NodeLayout> expectedCounts = HashMultiset.create(); NodeLayout masterNodeLayout = new NodeLayout("large-mem", "centos6", ImmutableSet.of(namenode.getName(), resourcemanager.getName())); expectedCounts.add(masterNodeLayout, 1); NodeLayout slaveNodeLayout = new NodeLayout("medium", "centos6", ImmutableSet.of(datanode.getName())); expectedCounts.add(slaveNodeLayout, 50); ClusterLayout expected = new ClusterLayout(reactorTemplate.getConstraints(), expectedCounts); ClusterLayout layout = updater.addServicesToCluster(cluster, nodes, ImmutableSet.of(resourcemanager.getName())).getCurrentLayout(); Assert.assertEquals(expected, layout); }
@Test public void testAddServices() throws Exception { Multiset<NodeLayout> expectedCounts = HashMultiset.create(); NodeLayout masterNodeLayout = new NodeLayout("large-mem", "centos6", ImmutableSet.of(namenode.getName(), resourcemanager.getName(), hbasemaster.getName())); expectedCounts.add(masterNodeLayout, 1); NodeLayout slaveNodeLayout = new NodeLayout("medium", "centos6", ImmutableSet.of(datanode.getName(), nodemanager.getName(), regionserver.getName())); expectedCounts.add(slaveNodeLayout, 50); ClusterLayout expected = new ClusterLayout(reactorTemplate.getConstraints(), expectedCounts); ClusterLayout layout = updater.addServicesToCluster( cluster, nodes, ImmutableSet.of(resourcemanager.getName(), nodemanager.getName(), hbasemaster.getName(), regionserver.getName())).getCurrentLayout(); Assert.assertEquals(expected, layout); }
@Test public void testNoSolutionReturnsNull() throws Exception { Assert.assertNull(updater.addServicesToCluster(cluster, nodes, ImmutableSet.of(zookeeper.getName()))); }
@Test public void testNoSolutionWithMixedServicesReturnsNull() throws Exception { Assert.assertNull(updater.addServicesToCluster(cluster, nodes, ImmutableSet.of( resourcemanager.getName(), nodemanager.getName(), zookeeper.getName()))); }