@Test
  public void testSecretReferences() throws Exception, AuthorizationException {

    final String host1 = getUniqueName();
    final String host2 = getUniqueName();
    String cluster1 = getUniqueName();

    Cluster cl = setupClusterWithHosts(cluster1, "HDP-2.0.5", new ArrayList<String>() {
      {
        add(host1);
        add(host2);
      }
    }, "centos5");

    Long clusterId = cl.getClusterId();

    String serviceName = "HDFS";
    createService(cluster1, serviceName, null);
    String componentName1 = "NAMENODE";
    String componentName2 = "DATANODE";
    String componentName3 = "HDFS_CLIENT";

    createServiceComponent(cluster1, serviceName, componentName1, State.INIT);
    createServiceComponent(cluster1, serviceName, componentName2, State.INIT);
    createServiceComponent(cluster1, serviceName, componentName3, State.INIT);

    createServiceComponentHost(cluster1, serviceName, componentName1, host1, null);
    createServiceComponentHost(cluster1, serviceName, componentName2, host1, null);
    createServiceComponentHost(cluster1, serviceName, componentName3, host1, null);
    createServiceComponentHost(cluster1, serviceName, componentName2, host2, null);
    createServiceComponentHost(cluster1, serviceName, componentName3, host2, null);

    // Install
    installService(cluster1, serviceName, false, false);

    ClusterRequest crReq;
    ConfigurationRequest cr;

    cr = new ConfigurationRequest(cluster1,
        "hdfs-site",
        "version1",
        new HashMap<String, String>(){{
          put("test.password", "first");
          put("test.password.empty", "");
        }},
      new HashMap<>()
    );
    crReq = new ClusterRequest(clusterId, cluster1, null, null);
    crReq.setDesiredConfig(Collections.singletonList(cr));

    controller.updateClusters(Collections.singleton(crReq), null);
    // update config with secret reference
    cr = new ConfigurationRequest(cluster1,
        "hdfs-site",
        "version2",
        new HashMap<String, String>(){{
          put("test.password", "SECRET:hdfs-site:1:test.password");
          put("new", "new");//need this to mark config as "changed"
        }},
      new HashMap<>()
    );
    crReq = new ClusterRequest(clusterId, cluster1, null, null);
    crReq.setDesiredConfig(Collections.singletonList(cr));
    controller.updateClusters(Collections.singleton(crReq), null);
    // change password to new value
    cr = new ConfigurationRequest(cluster1,
        "hdfs-site",
        "version3",
        new HashMap<String, String>(){{
          put("test.password", "brandNewPassword");
        }},
      new HashMap<>()
    );
    crReq = new ClusterRequest(clusterId, cluster1, null, null);
    crReq.setDesiredConfig(Collections.singletonList(cr));
    controller.updateClusters(Collections.singleton(crReq), null);
    // wrong secret reference
    cr = new ConfigurationRequest(cluster1,
        "hdfs-site",
        "version3",
        new HashMap<String, String>(){{
          put("test.password", "SECRET:hdfs-site:666:test.password");
        }},
      new HashMap<>()
    );
    crReq = new ClusterRequest(clusterId, cluster1, null, null);
    crReq.setDesiredConfig(Collections.singletonList(cr));
    try {
      controller.updateClusters(Collections.singleton(crReq), null);
      fail("Request need to be failed with wrong secret reference");
    } catch (Exception e){

    }
    // reference to config which does not contain requested property
    cr = new ConfigurationRequest(cluster1,
        "hdfs-site",
        "version4",
        new HashMap<String, String>(){{
          put("foo", "bar");
        }},
      new HashMap<>()
    );
    crReq = new ClusterRequest(clusterId, cluster1, null, null);
    crReq.setDesiredConfig(Collections.singletonList(cr));
    controller.updateClusters(Collections.singleton(crReq), null);
    cr = new ConfigurationRequest(cluster1,
        "hdfs-site",
        "version5",
        new HashMap<String, String>(){{
          put("test.password", "SECRET:hdfs-site:4:test.password");
          put("new", "new");
        }},
      new HashMap<>()
    );
    crReq = new ClusterRequest(clusterId, cluster1, null, null);
    crReq.setDesiredConfig(Collections.singletonList(cr));
    try {
      controller.updateClusters(Collections.singleton(crReq), null);
      fail("Request need to be failed with wrong secret reference");
    } catch (Exception e) {
      assertEquals("Error when parsing secret reference. Cluster: " + cluster1 + " ConfigType: hdfs-site ConfigVersion: 4 does not contain property 'test.password'",
          e.getMessage());
    }
    cl.getAllConfigs();
    assertEquals(cl.getAllConfigs().size(), 4);

    Config v1 = cl.getConfigByVersion("hdfs-site", 1l);
    Config v2 = cl.getConfigByVersion("hdfs-site", 2l);
    Config v3 = cl.getConfigByVersion("hdfs-site", 3l);
    Config v4 = cl.getConfigByVersion("hdfs-site", 4l);

    assertEquals(v1.getProperties().get("test.password"), "first");
    assertEquals(v2.getProperties().get("test.password"), "first");
    assertEquals(v3.getProperties().get("test.password"), "brandNewPassword");
    assertFalse(v4.getProperties().containsKey("test.password"));

    // check if we have masked secret in responce
    final ConfigurationRequest configRequest = new ConfigurationRequest(cluster1, "hdfs-site", null, null, null);
    configRequest.setIncludeProperties(true);
    Set<ConfigurationResponse> requestedConfigs = controller.getConfigurations(new HashSet<ConfigurationRequest>() {{
      add(configRequest);
    }});
    for(ConfigurationResponse resp : requestedConfigs) {
      String secretName = "SECRET:hdfs-site:"+ resp.getVersion() +":test.password";
      if(resp.getConfigs().containsKey("test.password")) {
        assertEquals(resp.getConfigs().get("test.password"), secretName);
      }
      if(resp.getConfigs().containsKey("test.password.empty")) {
        assertEquals(resp.getConfigs().get("test.password.empty"), "");
      }
    }
  }
Ensure that ServiceNotFoundException is handled where there are multiple requests as would be the
case when an OR predicate is provided in the query.
ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java