@Test
  public void testCreateServiceComponentHostWithInvalidRequest()
      throws Exception, AuthorizationException {
    // multiple clusters
    // dup objects
    // existing components
    // invalid request params
    // invalid service
    // invalid cluster
    // invalid component
    // invalid host

    Set<ServiceComponentHostRequest> set1 =
      new HashSet<>();

    try {
      set1.clear();
      ServiceComponentHostRequest rInvalid =
          new ServiceComponentHostRequest(null, null, null, null, null);
      set1.add(rInvalid);
      controller.createHostComponents(set1);
      fail("Expected failure for invalid requests");
    } catch (IllegalArgumentException e) {
      // Expected
    }

    try {
      set1.clear();
      ServiceComponentHostRequest rInvalid =
          new ServiceComponentHostRequest("foo", null, null, null, null);
      set1.add(rInvalid);
      controller.createHostComponents(set1);
      fail("Expected failure for invalid requests");
    } catch (IllegalArgumentException e) {
      // Expected
    }

    try {
      set1.clear();
      ServiceComponentHostRequest rInvalid =
          new ServiceComponentHostRequest("foo", "HDFS", null, null, null);
      set1.add(rInvalid);
      controller.createHostComponents(set1);
      fail("Expected failure for invalid requests");
    } catch (IllegalArgumentException e) {
      // Expected
    }

    try {
      set1.clear();
      ServiceComponentHostRequest rInvalid =
          new ServiceComponentHostRequest("foo", "HDFS", "NAMENODE", null, null);
      set1.add(rInvalid);
      controller.createHostComponents(set1);
      fail("Expected failure for invalid requests");
    } catch (IllegalArgumentException e) {
      // Expected
    }

    String host1 = getUniqueName();
    String host2 = getUniqueName();
    String host3 = getUniqueName();

    String clusterFoo = getUniqueName();
    String cluster1 = getUniqueName();
    String cluster2 = getUniqueName();

    try {
      set1.clear();
      ServiceComponentHostRequest rInvalid =
          new ServiceComponentHostRequest(clusterFoo, "HDFS", "NAMENODE", host1, null);
      set1.add(rInvalid);
      controller.createHostComponents(set1);
      fail("Expected failure for invalid cluster");
    } catch (ParentObjectNotFoundException e) {
      // Expected
    }

    clusters.addCluster(clusterFoo, new StackId("HDP-0.2"));
    clusters.addCluster(cluster1, new StackId("HDP-0.2"));
    clusters.addCluster(cluster2, new StackId("HDP-0.2"));
    Cluster foo = clusters.getCluster(clusterFoo);
    Cluster c1 = clusters.getCluster(cluster1);
    Cluster c2 = clusters.getCluster(cluster2);




    StackId stackId = new StackId("HDP-0.2");
    RepositoryVersionEntity repositoryVersion = helper.getOrCreateRepositoryVersion(stackId,
        stackId.getStackVersion());

    foo.setDesiredStackVersion(stackId);
    foo.setCurrentStackVersion(stackId);

    stackId = new StackId("HDP-0.2");
    c1.setDesiredStackVersion(stackId);
    c1.setCurrentStackVersion(stackId);

    stackId = new StackId("HDP-0.2");
    c2.setDesiredStackVersion(stackId);
    c2.setCurrentStackVersion(stackId);

    try {
      set1.clear();
      ServiceComponentHostRequest rInvalid =
          new ServiceComponentHostRequest(clusterFoo, "HDFS", "NAMENODE", host1, null);
      set1.add(rInvalid);
      controller.createHostComponents(set1);
      fail("Expected failure for invalid service");
    } catch (IllegalArgumentException e) {
      // Expected
    }

    Service s1 = serviceFactory.createNew(foo, "HDFS", repositoryVersion);
    foo.addService(s1);
    Service s2 = serviceFactory.createNew(c1, "HDFS", repositoryVersion);
    c1.addService(s2);
    Service s3 = serviceFactory.createNew(c2, "HDFS", repositoryVersion);
    c2.addService(s3);


    try {
      set1.clear();
      ServiceComponentHostRequest rInvalid =
          new ServiceComponentHostRequest(clusterFoo, "HDFS", "NAMENODE", host1, null);
      set1.add(rInvalid);
      controller.createHostComponents(set1);
      fail("Expected failure for invalid service");
    } catch (Exception e) {
      // Expected
    }

    ServiceComponent sc1 = serviceComponentFactory.createNew(s1, "NAMENODE");
    s1.addServiceComponent(sc1);
    ServiceComponent sc2 = serviceComponentFactory.createNew(s2, "NAMENODE");
    s2.addServiceComponent(sc2);
    ServiceComponent sc3 = serviceComponentFactory.createNew(s3, "NAMENODE");
    s3.addServiceComponent(sc3);


    try {
      set1.clear();
      ServiceComponentHostRequest rInvalid =
          new ServiceComponentHostRequest(clusterFoo, "HDFS", "NAMENODE", host1, null);
      set1.add(rInvalid);
      controller.createHostComponents(set1);
      fail("Expected failure for invalid host");
    } catch (Exception e) {
      // Expected
    }

    clusters.addHost(host1);
    Host h1 = clusters.getHost(host1);
    h1.setIPv4("ipv41");
    h1.setIPv6("ipv61");
    setOsFamily(h1, "redhat", "6.3");
    clusters.addHost(host2);
    Host h2 = clusters.getHost(host2);
    h2.setIPv4("ipv42");
    h2.setIPv6("ipv62");
    setOsFamily(h2, "redhat", "6.3");
    clusters.addHost(host3);
    Host h3 = clusters.getHost(host3);
    h3.setIPv4("ipv43");
    h3.setIPv6("ipv63");
    setOsFamily(h3, "redhat", "6.3");

    try {
      set1.clear();
      ServiceComponentHostRequest rInvalid =
          new ServiceComponentHostRequest(clusterFoo, "HDFS", "NAMENODE", host1, null);
      set1.add(rInvalid);
      controller.createHostComponents(set1);
      fail("Expected failure for invalid host cluster mapping");
    } catch (Exception e) {
      // Expected
    }

    Set<String> hostnames = new HashSet<>();
    hostnames.add(host1);
    hostnames.add(host2);
    hostnames.add(host3);
    clusters.mapAndPublishHostsToCluster(hostnames, clusterFoo);
    clusters.mapAndPublishHostsToCluster(hostnames, cluster1);
    clusters.mapAndPublishHostsToCluster(hostnames, cluster2);
    clusters.updateHostMappings(clusters.getHost(host1));
    clusters.updateHostMappings(clusters.getHost(host2));
    clusters.updateHostMappings(clusters.getHost(host3));
    set1.clear();
    ServiceComponentHostRequest valid =
        new ServiceComponentHostRequest(clusterFoo, "HDFS", "NAMENODE", host1, null);
    set1.add(valid);
    controller.createHostComponents(set1);

    try {
      set1.clear();
      ServiceComponentHostRequest rInvalid1 =
          new ServiceComponentHostRequest(clusterFoo, "HDFS", "NAMENODE", host2, null);
      ServiceComponentHostRequest rInvalid2 =
          new ServiceComponentHostRequest(clusterFoo, "HDFS", "NAMENODE", host2, null);
      set1.add(rInvalid1);
      set1.add(rInvalid2);
      controller.createHostComponents(set1);
      fail("Expected failure for dup requests");
    } catch (DuplicateResourceException e) {
      // Expected
    }

    try {
      set1.clear();
      ServiceComponentHostRequest rInvalid1 =
          new ServiceComponentHostRequest(cluster1, "HDFS", "NAMENODE", host2,
              null);
      ServiceComponentHostRequest rInvalid2 =
          new ServiceComponentHostRequest(cluster2, "HDFS", "NAMENODE", host3,
              null);
      set1.add(rInvalid1);
      set1.add(rInvalid2);
      controller.createHostComponents(set1);
      fail("Expected failure for multiple clusters");
    } catch (IllegalArgumentException e) {
      // Expected
    }

    try {
      set1.clear();
      ServiceComponentHostRequest rInvalid1 =
          new ServiceComponentHostRequest(clusterFoo, "HDFS", "NAMENODE", host1,
              null);
      ServiceComponentHostRequest rInvalid2 =
          new ServiceComponentHostRequest(clusterFoo, "HDFS", "NAMENODE", host2,
              null);
      set1.add(rInvalid1);
      set1.add(rInvalid2);
      controller.createHostComponents(set1);
      fail("Expected failure for already existing");
    } catch (DuplicateResourceException e) {
      // Expected
    }

    Assert.assertEquals(1, foo.getServiceComponentHosts(host1).size());
    Assert.assertEquals(0, foo.getServiceComponentHosts(host2).size());
    Assert.assertEquals(0, foo.getServiceComponentHosts(host3).size());

    set1.clear();
    ServiceComponentHostRequest valid1 =
        new ServiceComponentHostRequest(cluster1, "HDFS", "NAMENODE", host1,
            null);
    set1.add(valid1);
    controller.createHostComponents(set1);

    set1.clear();
    ServiceComponentHostRequest valid2 =
        new ServiceComponentHostRequest(cluster2, "HDFS", "NAMENODE", host1,
            null);
    set1.add(valid2);
    controller.createHostComponents(set1);

    Assert.assertEquals(1, foo.getServiceComponentHosts(host1).size());
    Assert.assertEquals(1, c1.getServiceComponentHosts(host1).size());
    Assert.assertEquals(1, c2.getServiceComponentHosts(host1).size());

  }
Allows to set maintenanceStateHelper. For use when there is anything to test
with maintenance mode.
ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java