### Question:
StageUtils { public static void useAmbariJdkInCommandParams(Map<String, String> commandParams, Configuration configuration) { if (StringUtils.isNotEmpty(configuration.getJavaHome()) && !configuration.getJavaHome().equals(configuration.getStackJavaHome())) { commandParams.put(AMBARI_JAVA_HOME, configuration.getJavaHome()); commandParams.put(AMBARI_JAVA_VERSION, String.valueOf(configuration.getJavaVersion())); if (StringUtils.isNotEmpty(configuration.getJDKName())) { commandParams.put(AMBARI_JDK_NAME, configuration.getJDKName()); } if (StringUtils.isNotEmpty(configuration.getJCEName())) { commandParams.put(AMBARI_JCE_NAME, configuration.getJCEName()); } } } @Inject  StageUtils(StageFactory stageFactory); static Gson getGson(); static void setGson(Gson gson); static void setTopologyManager(TopologyManager topologyManager); static void setConfiguration(Configuration configuration); static String getActionId(long requestId, long stageId); static long[] getRequestStage(String actionId); static Stage getATestStage(long requestId, long stageId, String commandParamsStage, String hostParamsStage); @Inject static Stage getATestStage(long requestId, long stageId, String hostname, String commandParamsStage, String hostParamsStage); static String jaxbToString(Object jaxbObj); static T fromJson(String json, Class<T> clazz); static Map<String, String> getCommandParamsStage(ActionExecutionContext actionExecContext, String requestContext); static String getClusterHostInfoKey(String componentName); static Map<String, Set<String>> getClusterHostInfo(Cluster cluster); static Map<String, Set<String>> substituteHostIndexes(Map<String, Set<String>> clusterHostInfo); static Set<String> replaceRanges(SortedSet<Integer> set); static Set<String> replaceMappedRanges(List<T> values); static String getHostName(); static void useAmbariJdkInCommandParams(Map<String, String> commandParams, Configuration configuration); static void useStackJdkIfExists(Map<String, String> hostLevelParams, Configuration configuration); static final Integer DEFAULT_PING_PORT; static final String DEFAULT_RACK; static final String DEFAULT_IPV4_ADDRESS; }### Answer:
@Test public void testUseAmbariJdkWithoutavaHome() { Map<String, String> commandParams = new HashMap<>(); Configuration configuration = new Configuration(); StageUtils.useAmbariJdkInCommandParams(commandParams, configuration); assertTrue(commandParams.isEmpty()); }
@Test public void testUseAmbariJdkWithCustomJavaHome() { Map<String, String> commandParams = new HashMap<>(); Configuration configuration = new Configuration(); configuration.setProperty("java.home", "myJavaHome"); StageUtils.useAmbariJdkInCommandParams(commandParams, configuration); assertEquals("myJavaHome", commandParams.get("ambari_java_home")); assertEquals(2, commandParams.size()); }
@Test public void testUseAmbariJdk() { Map<String, String> commandParams = new HashMap<>(); Configuration configuration = new Configuration(); configuration.setProperty("java.home", "myJavaHome"); configuration.setProperty("jdk.name", "myJdkName"); configuration.setProperty("jce.name", "myJceName"); StageUtils.useAmbariJdkInCommandParams(commandParams, configuration); assertEquals("myJavaHome", commandParams.get("ambari_java_home")); assertEquals("myJdkName", commandParams.get("ambari_jdk_name")); assertEquals("myJceName", commandParams.get("ambari_jce_name")); assertEquals(4, commandParams.size()); }