/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.ambari.server.serveraction.kerberos;

import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.capture;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.apache.ambari.server.agent.ExecutionCommand;
import org.apache.ambari.server.audit.AuditLogger;
import org.apache.ambari.server.controller.AmbariManagementController;
import org.apache.ambari.server.state.Cluster;
import org.apache.ambari.server.state.Clusters;
import org.apache.ambari.server.state.ConfigHelper;
import org.apache.ambari.server.state.stack.OsFamily;
import org.easymock.Capture;
import org.easymock.EasyMock;
import org.easymock.EasyMockSupport;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;

public class UpdateKerberosConfigsServerActionTest extends EasyMockSupport{

  @Rule
  public TemporaryFolder testFolder = new TemporaryFolder();
  String dataDir;
  private Injector injector;
  private UpdateKerberosConfigsServerAction action;


  @Before
  public void setup() throws Exception {
    final AmbariManagementController controller = createNiceMock(AmbariManagementController.class);
    final Clusters clusters = createNiceMock(Clusters.class);
    final Cluster cluster = createNiceMock(Cluster.class);

    expect(controller.getClusters()).andReturn(clusters).once();

    expect(clusters.getCluster(anyObject(String.class))).andReturn(cluster).once();

    injector = Guice.createInjector(new AbstractModule() {

      @Override
      protected void configure() {
        bind(AmbariManagementController.class).toInstance(controller);
        bind(ConfigHelper.class).toInstance(createNiceMock(ConfigHelper.class));
        bind(AuditLogger.class).toInstance(createNiceMock(AuditLogger.class));
        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
      }
    });

    dataDir = testFolder.getRoot().getAbsolutePath();

    setupConfigDat();

    action = injector.getInstance(UpdateKerberosConfigsServerAction.class);
  }

  private void setupConfigDat() throws Exception {
    File configFile = new File(dataDir, KerberosConfigDataFileWriter.DATA_FILE_NAME);
    KerberosConfigDataFileWriterFactory factory = injector.getInstance(KerberosConfigDataFileWriterFactory.class);
    KerberosConfigDataFileWriter writer = factory.createKerberosConfigDataFileWriter(configFile);
    writer.addRecord("hdfs-site", "hadoop.security.authentication", "kerberos", KerberosConfigDataFileWriter.OPERATION_TYPE_SET);
    writer.addRecord("hdfs-site", "remove.me", null, KerberosConfigDataFileWriter.OPERATION_TYPE_REMOVE);
    writer.close();
  }

  @Test
  public void testUpdateConfig() throws Exception {
    Map<String, String> commandParams = new HashMap<String, String>();
    commandParams.put(KerberosServerAction.DATA_DIRECTORY, dataDir);

    ExecutionCommand executionCommand = new ExecutionCommand();
    executionCommand.setCommandParams(commandParams);

    ConfigHelper configHelper = injector.getInstance(ConfigHelper.class);
    configHelper.updateConfigType(anyObject(Cluster.class), anyObject(AmbariManagementController.class),
        anyObject(String.class), EasyMock.<Map<String, String>>anyObject(), EasyMock.<Collection<String>>anyObject(), anyObject(String.class), anyObject(String.class));
    expectLastCall().atLeastOnce();

    replayAll();

    action.setExecutionCommand(executionCommand);
    action.execute(null);

    verifyAll();
  }

  @Test
  public void testUpdateConfigMissingDataDirectory() throws Exception {
    ExecutionCommand executionCommand = new ExecutionCommand();
    Map<String, String> commandParams = new HashMap<String, String>();
    executionCommand.setCommandParams(commandParams);

    replayAll();

    action.setExecutionCommand(executionCommand);
    action.execute(null);

    verifyAll();
  }

  @Test
  public void testUpdateConfigEmptyDataDirectory() throws Exception {
    ExecutionCommand executionCommand = new ExecutionCommand();
    Map<String, String> commandParams = new HashMap<String, String>();
    commandParams.put(KerberosServerAction.DATA_DIRECTORY, testFolder.newFolder().getAbsolutePath());
    executionCommand.setCommandParams(commandParams);

    replayAll();

    action.setExecutionCommand(executionCommand);
    action.execute(null);

    verifyAll();
  }

  @Test
  public void testUpdateConfigForceSecurityEnabled() throws Exception {
    Map<String, String> commandParams = new HashMap<String, String>();
    commandParams.put(KerberosServerAction.DATA_DIRECTORY, dataDir);

    ExecutionCommand executionCommand = new ExecutionCommand();
    executionCommand.setCommandParams(commandParams);

    ConfigHelper configHelper = injector.getInstance(ConfigHelper.class);

    Capture<String> configTypes = Capture.newInstance();
    Capture<Map<String, String>> configUpdates = Capture.newInstance();
    configHelper.updateConfigType(anyObject(Cluster.class), anyObject(AmbariManagementController.class),
        capture(configTypes), capture(configUpdates), anyObject(Collection.class), anyObject(String.class), anyObject(String.class));
    expectLastCall().atLeastOnce();

    replayAll();

    action.setExecutionCommand(executionCommand);
    action.execute(null);

    assertTrue(configTypes.getValues().contains("cluster-env"));
    boolean containsSecurityEnabled = false;
    for(Map<String, String> properties: configUpdates.getValues()) {
      if(properties.containsKey("security_enabled")) {
        containsSecurityEnabled = true;
        break;
      }
    }
    assertTrue(containsSecurityEnabled);
    verifyAll();
  }


}

@@ -1082,20 +1082,22 @@
 t.assert
-True
+Equals
 ;%0A%0Aimpor
@@ -1772,24 +1772,25 @@
 raryFolder;%0A
+%0A
 import com.g
@@ -6376,28 +6376,30 @@
 %0A%0A    assert
-True
+Equals
 (configTypes
@@ -6411,21 +6411,12 @@
 alue
-s
 ()
-.contains(
+, 
 %22clu
@@ -6429,96 +6429,27 @@
 nv%22)
-)
 ;%0A    
-boolean containsSecurityEnabled = false;%0A    for(Map%3CString, String%3E properties: 
+assertEquals(
 conf
@@ -6470,177 +6470,43 @@
 alue
-s
 ()
-) %7B%0A      if(properties.containsKey(%22security_enabled%22)) %7B%0A        containsSecurityEnabled = true;%0A        break;%0A      %7D%0A    %7D%0A    assertTrue(containsSecurityEnabled
+.get(%22security_enabled%22), %22false%22
 );%0A 

Revert "AMBARI-19741. Ambari Server Unit Test failure on branch-2.5/trunk for testUpdateConfigForceSecurityEnabled (echekanskiy via dlysnichenko)"