@Test
public void testLogSearchUpdateConfigs() throws Exception {

  reset(clusters, cluster);
  expect(clusters.getClusters()).andReturn(ImmutableMap.of("normal", cluster)).once();
  EasyMockSupport easyMockSupport = new EasyMockSupport();
  Injector injector2 = easyMockSupport.createNiceMock(Injector.class);
  AmbariManagementControllerImpl controller = createMockBuilder(AmbariManagementControllerImpl.class)
      .addMockedMethod("createConfiguration")
      .addMockedMethod("getClusters", new Class[]{})
      .addMockedMethod("createConfig")
      .withConstructor(actionManager, clusters, injector)
      .createNiceMock();
  expect(injector2.getInstance(AmbariManagementController.class)).andReturn(controller).anyTimes();
  expect(controller.getClusters()).andReturn(clusters).anyTimes();
  Map<String, String> oldLogSearchProperties = ImmutableMap.of(
      "logsearch.external.auth.enabled", "true",
      "logsearch.external.auth.host_url", "host_url",
      "logsearch.external.auth.login_url", "login_url");
  Map<String, String> expectedLogSearchProperties = ImmutableMap.of(
      "logsearch.auth.external_auth.enabled", "true",
      "logsearch.auth.external_auth.host_url", "host_url",
      "logsearch.auth.external_auth.login_url", "login_url");
  Config mockLogSearchProperties = easyMockSupport.createNiceMock(Config.class);
  expect(cluster.getDesiredConfigByType("logsearch-properties")).andReturn(mockLogSearchProperties).atLeastOnce();
  expect(mockLogSearchProperties.getProperties()).andReturn(oldLogSearchProperties).anyTimes();
  Capture<Map<String, String>> logSearchPropertiesCapture = EasyMock.newCapture();
  expect(controller.createConfig(anyObject(Cluster.class), anyString(), capture(logSearchPropertiesCapture), anyString(),
      EasyMock.<Map<String, Map<String, String>>>anyObject())).andReturn(config).once();
  Map<String, String> oldLogFeederEnv = ImmutableMap.of(
      "content", "infra_solr_ssl_enabled");
  Map<String, String> expectedLogFeederEnv = ImmutableMap.of(
      "content", "logfeeder_use_ssl");
  Config mockLogFeederEnv = easyMockSupport.createNiceMock(Config.class);
  expect(cluster.getDesiredConfigByType("logfeeder-env")).andReturn(mockLogFeederEnv).atLeastOnce();
  expect(mockLogFeederEnv.getProperties()).andReturn(oldLogFeederEnv).anyTimes();
  Capture<Map<String, String>> logFeederEnvCapture = EasyMock.newCapture();
  expect(controller.createConfig(anyObject(Cluster.class), anyString(), capture(logFeederEnvCapture), anyString(),
      EasyMock.<Map<String, Map<String, String>>>anyObject())).andReturn(config).once();
  Map<String, String> oldLogSearchEnv = new HashMap<>();
  oldLogSearchEnv.put("logsearch_solr_audit_logs_use_ranger", "false");
  oldLogSearchEnv.put("logsearch_solr_audit_logs_zk_node", "zk_node");
  oldLogSearchEnv.put("logsearch_solr_audit_logs_zk_quorum", "zk_quorum");
  oldLogSearchEnv.put("logsearch_ui_protocol", "http");
  oldLogSearchEnv.put("logsearch_truststore_location", "/etc/security/serverKeys/logsearch.trustStore.jks");
  oldLogSearchEnv.put("logsearch_keystore_location", "/etc/security/serverKeys/logsearch.keyStore.jks");
  oldLogSearchEnv.put("content", "infra_solr_ssl_enabled or logsearch_ui_protocol == 'https'");
  Map<String, String> expectedLogSearchEnv = ImmutableMap.of(
      "logsearch_ui_protocol", "http",
      "logsearch_truststore_location", "/etc/ambari-logsearch-portal/conf/keys/logsearch.jks",
      "logsearch_keystore_location", "/etc/ambari-logsearch-portal/conf/keys/logsearch.jks",
      "content", "logsearch_use_ssl");
  Config mockLogSearchEnv = easyMockSupport.createNiceMock(Config.class);
  expect(cluster.getDesiredConfigByType("logsearch-env")).andReturn(mockLogSearchEnv).atLeastOnce();
  expect(mockLogSearchEnv.getProperties()).andReturn(oldLogSearchEnv).anyTimes();
  Capture<Map<String, String>> logSearchEnvCapture = EasyMock.newCapture();
  expect(controller.createConfig(anyObject(Cluster.class), anyString(), capture(logSearchEnvCapture), anyString(),
      EasyMock.<Map<String, Map<String, String>>>anyObject())).andReturn(config).once();
  Map<String, String> oldLogFeederLog4j = ImmutableMap.of(
      "content",
      "    <appender name=\"rolling_file\" class=\"org.apache.log4j.RollingFileAppender\">\n" +
          "    <param name=\"file\" value=\"{{logfeeder_log_dir}}/logfeeder.log\"/>\n" +
          "    <param name=\"append\" value=\"true\"/>\n" +
          "    <param name=\"maxFileSize\" value=\"11MB\"/>\n" +
          "    <param name=\"maxBackupIndex\" value=\"12\"/>\n" +
          "    <layout class=\"org.apache.log4j.PatternLayout\">\n" +
          "      <param name=\"ConversionPattern\" value=\"%d [%t] %-5p %C{6} (%F:%L) - %m%n\"/>\n" +
          "    </layout>\n" +
          "  </appender>\n" +
          "\n" +
          "  <appender name=\"rolling_file_json\"\n" +
          "    class=\"org.apache.ambari.logsearch.appender.LogsearchRollingFileAppender\">\n" +
          "    <param name=\"file\" value=\"{{logfeeder_log_dir}}/logsearch-logfeeder.json\" />\n" +
          "    <param name=\"append\" value=\"true\" />\n" +
          "    <param name=\"maxFileSize\" value=\"13MB\" />\n" +
          "    <param name=\"maxBackupIndex\" value=\"14\" />\n" +
          "    <layout class=\"org.apache.ambari.logsearch.appender.LogsearchConversion\" />\n" +
          "  </appender>");
  Map<String, String> expectedLogFeederLog4j = ImmutableMap.of(
      "content",
      "    <appender name=\"rolling_file\" class=\"org.apache.log4j.RollingFileAppender\">\n" +
          "    <param name=\"file\" value=\"{{logfeeder_log_dir}}/logfeeder.log\"/>\n" +
          "    <param name=\"append\" value=\"true\"/>\n" +
          "    <param name=\"maxFileSize\" value=\"{{logfeeder_log_maxfilesize}}MB\"/>\n" +
          "    <param name=\"maxBackupIndex\" value=\"{{logfeeder_log_maxbackupindex}}\"/>\n" +
          "    <layout class=\"org.apache.log4j.PatternLayout\">\n" +
          "      <param name=\"ConversionPattern\" value=\"%d [%t] %-5p %C{6} (%F:%L) - %m%n\"/>\n" +
          "    </layout>\n" +
          "  </appender>\n" +
          "\n" +
          "  <appender name=\"rolling_file_json\"\n" +
          "    class=\"org.apache.ambari.logsearch.appender.LogsearchRollingFileAppender\">\n" +
          "    <param name=\"file\" value=\"{{logfeeder_log_dir}}/logsearch-logfeeder.json\" />\n" +
          "    <param name=\"append\" value=\"true\" />\n" +
          "    <param name=\"maxFileSize\" value=\"{{logfeeder_json_log_maxfilesize}}MB\" />\n" +
          "    <param name=\"maxBackupIndex\" value=\"{{logfeeder_json_log_maxbackupindex}}\" />\n" +
          "    <layout class=\"org.apache.ambari.logsearch.appender.LogsearchConversion\" />\n" +
          "  </appender>",
      "logfeeder_log_maxfilesize", "11",
      "logfeeder_log_maxbackupindex", "12",
      "logfeeder_json_log_maxfilesize", "13",
      "logfeeder_json_log_maxbackupindex", "14");
  Config mockLogFeederLog4j = easyMockSupport.createNiceMock(Config.class);
  expect(cluster.getDesiredConfigByType("logfeeder-log4j")).andReturn(mockLogFeederLog4j).atLeastOnce();
  expect(mockLogFeederLog4j.getProperties()).andReturn(oldLogFeederLog4j).anyTimes();
  Capture<Map<String, String>> logFeederLog4jCapture = EasyMock.newCapture();
  expect(controller.createConfig(anyObject(Cluster.class), anyString(), capture(logFeederLog4jCapture), anyString(),
      EasyMock.<Map<String, Map<String, String>>>anyObject())).andReturn(config).once();
  Map<String, String> oldLogSearchLog4j = ImmutableMap.of(
      "content",
      "  <appender name=\"rolling_file\" class=\"org.apache.log4j.RollingFileAppender\">\n" +
          "    <param name=\"file\" value=\"{{logsearch_log_dir}}/logsearch.err\" />\n" +
          "    <param name=\"Threshold\" value=\"info\" />\n" +
          "    <param name=\"append\" value=\"true\" />\n" +
          "    <param name=\"maxFileSize\" value=\"11MB\" />\n" +
          "    <param name=\"maxBackupIndex\" value=\"12\" />\n" +
          "    <layout class=\"org.apache.log4j.PatternLayout\">\n" +
          "      <param name=\"ConversionPattern\" value=\"%d [%t] %-5p %C{6} (%F:%L) - %m%n\" />\n" +
          "    </layout>\n" +
          "  </appender>\n" +
          "\n" +
          "  <appender name=\"rolling_file_json\" class=\"org.apache.ambari.logsearch.appender.LogsearchRollingFileAppender\">\n" +
          "    <param name=\"file\" value=\"{{logsearch_log_dir}}/logsearch.json\"/>\n" +
          "    <param name=\"append\" value=\"true\"/>\n" +
          "    <param name=\"maxFileSize\" value=\"13MB\"/>\n" +
          "    <param name=\"maxBackupIndex\" value=\"14\"/>\n" +
          "    <layout class=\"org.apache.ambari.logsearch.appender.LogsearchConversion\"/>\n" +
          "  </appender>\n" +
          "\n" +
          "  <appender name=\"audit_rolling_file_json\" class=\"org.apache.ambari.logsearch.appender.LogsearchRollingFileAppender\">\n" +
          "    <param name=\"file\" value=\"{{logsearch_log_dir}}/logsearch-audit.json\"/>\n" +
          "    <param name=\"append\" value=\"true\"/>\n" +
          "    <param name=\"maxFileSize\" value=\"15MB\"/>\n" +
          "    <param name=\"maxBackupIndex\" value=\"16\"/>\n" +
          "    <layout class=\"org.apache.ambari.logsearch.appender.LogsearchConversion\"/>\n" +
          "  </appender>\n" +
          "\n" +
          "  <appender name=\"performance_analyzer_json\" class=\"org.apache.ambari.logsearch.appender.LogsearchRollingFileAppender\">\n" +
          "    <param name=\"file\" value=\"{{logsearch_log_dir}}/logsearch-performance.json\"/>\n" +
          "    <param name=\"Threshold\" value=\"info\"/>\n" +
          "    <param name=\"append\" value=\"true\"/>\n" +
          "    <param name=\"maxFileSize\" value=\"17MB\"/>\n" +
          "    <param name=\"maxBackupIndex\" value=\"18\"/>\n" +
          "    <layout class=\"org.apache.ambari.logsearch.appender.LogsearchConversion\"/>\n" +
          "  </appender>\n" +
          "\n" +
          "  <logger name=\"org.apache.ambari.logsearch.audit\" additivity=\"true\">\n" +
          "     <appender-ref ref=\"audit_rolling_file_json\"/>\n" +
          "  </logger>\n" +
          "\n" +
          "  <logger name=\"org.apache.ambari.logsearch.performance\" additivity=\"false\">\n" +
          "    <appender-ref ref=\"performance_analyzer_json\"/>\n" +
          "  </logger>\n" +
          "\n" +
          "  <category name=\"org.apache.ambari.logsearch\" additivity=\"false\">\n" +
          "    <priority value=\"warn\"/>\n" +
          "    <appender-ref ref=\"rolling_file_json\"/>\n" +
          "  </category>");
  Map<String, String> expectedLogSearchLog4j = new HashMap<>();
  expectedLogSearchLog4j.put("content",
      "  <appender name=\"rolling_file\" class=\"org.apache.log4j.RollingFileAppender\">\n" +
          "    <param name=\"file\" value=\"{{logsearch_log_dir}}/logsearch.log\" />\n" +
          "    <param name=\"Threshold\" value=\"info\" />\n" +
          "    <param name=\"append\" value=\"true\" />\n" +
          "    <param name=\"maxFileSize\" value=\"{{logsearch_log_maxfilesize}}MB\" />\n" +
          "    <param name=\"maxBackupIndex\" value=\"{{logsearch_log_maxbackupindex}}\" />\n" +
          "    <layout class=\"org.apache.log4j.PatternLayout\">\n" +
          "      <param name=\"ConversionPattern\" value=\"%d [%t] %-5p %C{6} (%F:%L) - %m%n\" />\n" +
          "    </layout>\n" +
          "  </appender>\n" +
          "\n" +
          "  <appender name=\"rolling_file_json\" class=\"org.apache.ambari.logsearch.appender.LogsearchRollingFileAppender\">\n" +
          "    <param name=\"file\" value=\"{{logsearch_log_dir}}/logsearch.json\"/>\n" +
          "    <param name=\"append\" value=\"true\"/>\n" +
          "    <param name=\"maxFileSize\" value=\"{{logsearch_json_log_maxfilesize}}MB\"/>\n" +
          "    <param name=\"maxBackupIndex\" value=\"{{logsearch_json_log_maxbackupindex}}\"/>\n" +
          "    <layout class=\"org.apache.ambari.logsearch.appender.LogsearchConversion\"/>\n" +
          "  </appender>\n" +
          "\n" +
          "  <appender name=\"audit_rolling_file_json\" class=\"org.apache.ambari.logsearch.appender.LogsearchRollingFileAppender\">\n" +
          "    <param name=\"file\" value=\"{{logsearch_log_dir}}/logsearch-audit.json\"/>\n" +
          "    <param name=\"append\" value=\"true\"/>\n" +
          "    <param name=\"maxFileSize\" value=\"{{logsearch_audit_log_maxfilesize}}MB\"/>\n" +
          "    <param name=\"maxBackupIndex\" value=\"{{logsearch_audit_log_maxbackupindex}}\"/>\n" +
          "    <layout class=\"org.apache.ambari.logsearch.appender.LogsearchConversion\"/>\n" +
          "  </appender>\n" +
          "\n" +
          "  <appender name=\"performance_analyzer_json\" class=\"org.apache.ambari.logsearch.appender.LogsearchRollingFileAppender\">\n" +
          "    <param name=\"file\" value=\"{{logsearch_log_dir}}/logsearch-performance.json\"/>\n" +
          "    <param name=\"Threshold\" value=\"info\"/>\n" +
          "    <param name=\"append\" value=\"true\"/>\n" +
          "    <param name=\"maxFileSize\" value=\"{{logsearch_perf_log_maxfilesize}}MB\"/>\n" +
          "    <param name=\"maxBackupIndex\" value=\"{{logsearch_perf_log_maxbackupindex}}\"/>\n" +
          "    <layout class=\"org.apache.ambari.logsearch.appender.LogsearchConversion\"/>\n" +
          "  </appender>\n" +
          "\n" +
          "  <logger name=\"org.apache.ambari.logsearch.audit\" additivity=\"true\">\n" +
          "     <appender-ref ref=\"audit_rolling_file_json\"/>\n" +
          "  </logger>\n" +
          "\n" +
          "  <logger name=\"org.apache.ambari.logsearch.performance\" additivity=\"false\">\n" +
          "    <appender-ref ref=\"performance_analyzer_json\"/>\n" +
          "  </logger>\n" +
          "\n" +
          "  <category name=\"org.apache.ambari.logsearch\" additivity=\"false\">\n" +
          "    <priority value=\"info\"/>\n" +
          "    <appender-ref ref=\"rolling_file_json\"/>\n" +
          "  </category>");
  expectedLogSearchLog4j.put("logsearch_log_maxfilesize", "11");
  expectedLogSearchLog4j.put("logsearch_log_maxbackupindex", "12");
  expectedLogSearchLog4j.put("logsearch_json_log_maxfilesize", "13");
  expectedLogSearchLog4j.put("logsearch_json_log_maxbackupindex", "14");
  expectedLogSearchLog4j.put("logsearch_audit_log_maxfilesize", "15");
  expectedLogSearchLog4j.put("logsearch_audit_log_maxbackupindex", "16");
  expectedLogSearchLog4j.put("logsearch_perf_log_maxfilesize", "17");
  expectedLogSearchLog4j.put("logsearch_perf_log_maxbackupindex", "18");
  Config mockLogSearchLog4j = easyMockSupport.createNiceMock(Config.class);
  expect(cluster.getDesiredConfigByType("logsearch-log4j")).andReturn(mockLogSearchLog4j).atLeastOnce();
  expect(mockLogSearchLog4j.getProperties()).andReturn(oldLogSearchLog4j).anyTimes();
  Capture<Map<String, String>> logSearchLog4jCapture = EasyMock.newCapture();
  expect(controller.createConfig(anyObject(Cluster.class), anyString(), capture(logSearchLog4jCapture), anyString(),
      EasyMock.<Map<String, Map<String, String>>>anyObject())).andReturn(config).once();
  replay(clusters, cluster);
  replay(controller, injector2);
  replay(mockLogSearchProperties, mockLogFeederEnv, mockLogSearchEnv, mockLogFeederLog4j, mockLogSearchLog4j);
  new UpgradeCatalog250(injector2).updateLogSearchConfigs();
  easyMockSupport.verifyAll();
  Map<String, String> updatedLogSearchProperties = logSearchPropertiesCapture.getValue();
  assertTrue(Maps.difference(expectedLogSearchProperties, updatedLogSearchProperties).areEqual());
  Map<String, String> updatedLogFeederEnv = logFeederEnvCapture.getValue();
  assertTrue(Maps.difference(expectedLogFeederEnv, updatedLogFeederEnv).areEqual());
  Map<String, String> updatedLogSearchEnv = logSearchEnvCapture.getValue();
  assertTrue(Maps.difference(expectedLogSearchEnv, updatedLogSearchEnv).areEqual());
  Map<String, String> updatedLogFeederLog4j = logFeederLog4jCapture.getValue();
  assertTrue(Maps.difference(expectedLogFeederLog4j, updatedLogFeederLog4j).areEqual());
  Map<String, String> updatedLogSearchLog4j = logSearchLog4jCapture.getValue();
  assertTrue(Maps.difference(expectedLogSearchLog4j, updatedLogSearchLog4j).areEqual());
}