/*
 * 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.uima.analysis_engine.impl;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import org.apache.uima.pear.tools.PackageBrowser;
import org.apache.uima.pear.tools.PackageInstaller;
import org.apache.uima.pear.util.FileUtil;
import org.apache.uima.resource.PearSpecifier;
import org.apache.uima.resource.impl.Parameter_impl;
import org.apache.uima.resource.impl.PearSpecifier_impl;
import org.apache.uima.resource.metadata.impl.NameValuePair_impl;
import org.apache.uima.test.junit_extension.JUnitExtension;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class PearAnalysisEngineWrapperTest {

  // This parameter is defined in the PEAR specifier file!
  private final static String PARAMETER_NAME = "StringParam";

  private final static String PARAMETER_VALUE = "test";

  private final static String PARAMETER_VALUE_OVERRIDE = "testOverride";

  private PearAnalysisEngineWrapper pearAnalysisEngineWrapper;

  private File tempInstallDirectory = null;

  private PackageBrowser installedPearPackage;

  @BeforeEach
  public void setUp() throws Exception {

    pearAnalysisEngineWrapper = new PearAnalysisEngineWrapper();

    tempInstallDirectory = createInstallationDirectory();

    installedPearPackage = installPearPackage();
  }

  @AfterEach
  public void tearDown() throws Exception {
    removeInstallationDirectory();
  }

  @Test
  public void testInitializeWithOverride() throws Exception {

    PearSpecifier_impl pearSpecifier = new PearSpecifier_impl();
    pearSpecifier.setPearPath(installedPearPackage.getRootDirectory().toString());
    pearSpecifier
            .setPearParameters(new NameValuePair_impl(PARAMETER_NAME, PARAMETER_VALUE_OVERRIDE));

    boolean initialized = pearAnalysisEngineWrapper.initialize(pearSpecifier, new HashMap<>());

    assertThat(initialized).isTrue().as("Pear was initialized");
    assertThat(pearAnalysisEngineWrapper.getConfigParameterValue(PARAMETER_NAME))
            .isEqualTo(PARAMETER_VALUE_OVERRIDE).as("The value of StringParam was overridden");
  }

  @Test
  public void testInitializeWithOverrideLegacy() throws Exception {

    PearSpecifier_impl pearSpecifier = new PearSpecifier_impl();
    pearSpecifier.setPearPath(installedPearPackage.getRootDirectory().toString());
    pearSpecifier.setParameters(new Parameter_impl(PARAMETER_NAME, PARAMETER_VALUE_OVERRIDE));

    boolean initialized = pearAnalysisEngineWrapper.initialize(pearSpecifier, new HashMap<>());

    assertThat(initialized).isTrue().as("Pear was initialized");
    assertThat(pearAnalysisEngineWrapper.getConfigParameterValue(PARAMETER_NAME))
            .isEqualTo(PARAMETER_VALUE_OVERRIDE).as("The value of StringParam was overridden");
  }

  @Test
  public void testInitializeWithOverrideModernTakingPrecedenceOverLegacy() throws Exception {

    PearSpecifier_impl pearSpecifier = new PearSpecifier_impl();
    pearSpecifier.setPearPath(installedPearPackage.getRootDirectory().toString());
    pearSpecifier.setParameters(new Parameter_impl(PARAMETER_NAME, "legacy"));
    pearSpecifier.setPearParameters(new NameValuePair_impl(PARAMETER_NAME, "modern"));

    boolean initialized = pearAnalysisEngineWrapper.initialize(pearSpecifier, new HashMap<>());

    assertThat(initialized).isTrue().as("Pear was initialized");
    assertThat(pearAnalysisEngineWrapper.getConfigParameterValue(PARAMETER_NAME))
            .isEqualTo("modern").as("The value of StringParam was overridden");
  }

  @Test
  public void testInitializeWithoutOverride() throws Exception {

    PearSpecifier pearSpecifier = createPearSpecifierWithoutParameters();

    boolean initialized = pearAnalysisEngineWrapper.initialize(pearSpecifier, new HashMap<>());

    assertThat(initialized).as("Pear was not initialized").isTrue();

    String stringParamValue = (String) pearAnalysisEngineWrapper
            .getConfigParameterValue(PearAnalysisEngineWrapperTest.PARAMETER_NAME);

    assertThat(stringParamValue).as("The value of StringParam has changed")
            .isEqualTo(PearAnalysisEngineWrapperTest.PARAMETER_VALUE);
  }

  private PearSpecifier createPearSpecifierWithoutParameters() {

    PearSpecifier_impl pearSpecifier_impl = new PearSpecifier_impl();
    pearSpecifier_impl.setPearPath(installedPearPackage.getRootDirectory().toString());
    return pearSpecifier_impl;
  }

  private File createInstallationDirectory() throws IOException {

    File tempDirectory = File.createTempFile("pear_verification_test_", "tmp");
    if (tempDirectory.delete()) {

      if (!tempDirectory.mkdirs()) {
        throw new IllegalStateException(
                "Tmp directory (" + tempDirectory + ") could not be created");
      }
    }

    return tempDirectory;
  }

  private PackageBrowser installPearPackage() {
    File pearFile = JUnitExtension.getFile("pearTests/analysisEngineWithParameters.pear");
    assertThat(pearFile).as("analysisEngine.pear file not found").isNotNull();

    return PackageInstaller.installPackage(tempInstallDirectory, pearFile, false);
  }

  private void removeInstallationDirectory() throws IOException {
    if (tempInstallDirectory != null) {
      FileUtil.deleteDirectory(tempInstallDirectory);
    }
  }
}
