@Test
  public void testMergeConfigurationsWithClusterEnv() throws Exception {
    Cluster cluster = makeCluster(true);

    StackId oldStack = cluster.getDesiredStackVersion();
    StackId newStack = new StackId("HDP-2.5.0");

    ConfigFactory cf = injector.getInstance(ConfigFactory.class);

    Config clusterEnv = cf.createNew(cluster, "cluster-env", "version1",
            ImmutableMap.<String, String>builder().put("a", "b").build(),
            Collections.emptyMap());

    Config zooCfg = cf.createNew(cluster, "zoo.cfg", "version1",
            ImmutableMap.<String, String>builder().put("c", "d").build(),
            Collections.emptyMap());

    cluster.addDesiredConfig("admin", Sets.newHashSet(clusterEnv, zooCfg));

    Map<String, Map<String, String>> stackMap = new HashMap<>();
    stackMap.put("cluster-env", new HashMap<>());
    stackMap.put("hive-site", new HashMap<>());

    final ClusterEnvWrapper clusterEnvWrapper = new ClusterEnvWrapper();
    System.out.println("Initial clusterEnvMap: " + clusterEnvWrapper.getClusterEnvMap()); // Debug print

    EasyMock.reset(m_configHelper);
    expect(m_configHelper.getDefaultProperties(oldStack, "HIVE")).andReturn(stackMap).atLeastOnce();
    expect(m_configHelper.getDefaultProperties(newStack, "HIVE")).andReturn(stackMap).atLeastOnce();
    expect(m_configHelper.getDefaultProperties(oldStack, "ZOOKEEPER")).andReturn(stackMap).atLeastOnce();
    expect(m_configHelper.getDefaultProperties(newStack, "ZOOKEEPER")).andReturn(stackMap).atLeastOnce();
    expect(m_configHelper.createConfigTypes(
            EasyMock.anyObject(Cluster.class),
            EasyMock.anyObject(StackId.class),
            EasyMock.anyObject(AmbariManagementController.class),
            eqClusterEnv(clusterEnvWrapper),
            EasyMock.anyString(),
            EasyMock.anyString()
    )).andReturn(true);
    replay(m_configHelper);

    RepositoryVersionEntity repoVersionEntity = helper.getOrCreateRepositoryVersion(new StackId("HDP-2.5.0"), "2.5.0-1234");

    Map<String, Object> upgradeRequestMap = new HashMap<>();
    upgradeRequestMap.put(UpgradeResourceProvider.UPGRADE_DIRECTION, Direction.UPGRADE.name());
    upgradeRequestMap.put(UpgradeResourceProvider.UPGRADE_REPO_VERSION_ID, repoVersionEntity.getId().toString());
    upgradeRequestMap.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_test_HDP-250");
    upgradeRequestMap.put(UpgradeResourceProvider.UPGRADE_SKIP_PREREQUISITE_CHECKS, Boolean.TRUE.toString());

    UpgradeContextFactory contextFactory = injector.getInstance(UpgradeContextFactory.class);
    UpgradeContext context = contextFactory.create(cluster, upgradeRequestMap);

    UpgradeHelper upgradeHelper = injector.getInstance(UpgradeHelper.class);
    upgradeHelper.updateDesiredRepositoriesAndConfigs(context);

    assertEquals("b",clusterEnvWrapper.getClusterEnvMap().get("a"));

    assertTrue(clusterEnvWrapper.getClusterEnvMap().containsKey("a"));
    // Do stacks cleanup
    stackManagerMock.invalidateCurrentPaths();
    ambariMetaInfo.init();
  }
Tests merging configurations between existing and new stack values on
upgrade.

@throws Exception
ambari-server/src/test/java/org/apache/ambari/server/stack/upgrade/orchestrate/UpgradeHelperTest.java