@Test
  public void testAllowedReplacment() throws Exception {


    Cluster c = clusters.getCluster("c1");
    assertEquals(1, c.getConfigsByType("zoo.cfg").size());

    Map<String, String> properties = new HashMap<String, String>() {
      {
        put("replace.key.1", "r1");
        put("replace.key.2", "r2");
        put("replace.key.3", "r3a1");
        put("replace.key.4", "r4");
        put("replace.key.5", "r5");
      }
    };

    Config config = createConfig(c, repoVersion2200, "zoo.cfg", "version2", properties);

    c.addDesiredConfig("user", Collections.singleton(config));
    assertEquals(2, c.getConfigsByType("zoo.cfg").size());

    // create several configurations
    List<Replace> replacements = new ArrayList<>();
    Replace replace = new Replace();
    replace.key = "replace.key.3";
    replace.find = "a";
    replace.replaceWith = "A";
    replacements.add(replace);

    Replace replace2 = new Replace();
    replacements.add(replace2);
    replace2.key = "replace.key.4";
    replace2.find = "r";
    replace2.replaceWith = "R";
    replace2.ifKey = "replace.key.1";
    replace2.ifType = "zoo.cfg";
    replace2.ifValue = "r1";
    replacements.add(replace2);

    Replace replace3 = new Replace();
    replacements.add(replace3);
    replace3.key = "replace.key.2";
    replace3.find = "r";
    replace3.replaceWith = "R";
    replace3.ifKey = "replace.key.1";
    replace3.ifType = "zoo.cfg";
    replace3.ifKeyState = PropertyKeyState.PRESENT;
    replacements.add(replace3);

    Replace replace4 = new Replace();
    replacements.add(replace3);
    replace4.key = "replace.key.5";
    replace4.find = "r";
    replace4.replaceWith = "R";
    replace4.ifKey = "no.such.key";
    replace4.ifType = "zoo.cfg";
    replace4.ifKeyState = PropertyKeyState.ABSENT;
    replacements.add(replace4);

    createUpgrade(c, repoVersion2200);

    Map<String, String> commandParams = new HashMap<>();
    commandParams.put("clusterName", "c1");
    commandParams.put(ConfigureTask.PARAMETER_CONFIG_TYPE, "zoo.cfg");
    commandParams.put(ConfigureTask.PARAMETER_REPLACEMENTS, new Gson().toJson(replacements));

    ExecutionCommand executionCommand = getExecutionCommand(commandParams);
    HostRoleCommand hostRoleCommand = hostRoleCommandFactory.create(null, null, null, null);
    hostRoleCommand.setExecutionCommandWrapper(new ExecutionCommandWrapper(executionCommand));


    action.setExecutionCommand(executionCommand);
    action.setHostRoleCommand(hostRoleCommand);

    CommandReport report = action.execute(null);
    assertNotNull(report);

    assertEquals(2, c.getConfigsByType("zoo.cfg").size());

    config = c.getDesiredConfigByType("zoo.cfg");
    assertNotNull(config);
    assertEquals("r1", config.getProperties().get("replace.key.1"));
    assertEquals("R2", config.getProperties().get("replace.key.2"));
    assertEquals("r3A1", config.getProperties().get("replace.key.3"));
    assertEquals("R4", config.getProperties().get("replace.key.4"));
    assertEquals("R5", config.getProperties().get("replace.key.5"));
  }
Tests that replacing a {@code null} value works.

@throws Exception
ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/ConfigureActionTest.java