/**
 * Copyright (c) 2009 Capgemini Sud, 2010-2012 EBM WebSourcing, 2012-2022 Linagora
 * 
 * This program/library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * This program/library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program/library; If not, see http://www.gnu.org/licenses/
 * for the GNU Lesser General Public License version 2.1.
 */
package org.ow2.petals.plugin.jbiplugin;

import java.io.File;

import org.apache.maven.plugin.MojoFailureException;
import org.junit.Assert;
import org.junit.Test;
import org.ow2.petals.plugin.jbiplugin.environement.GlobalUnitTestEnvironnement;
import org.ow2.petals.plugin.jbiplugin.environement.ServiceAssemblyWithSimpleServiceUnitWithSeveralJBIComponentDependenciesWithComponentToUseDeclared;
import org.ow2.petals.plugin.jbiplugin.environement.ServiceAssemblyWithSimpleServiceUnitWithSeveralJBIComponentDependenciesWithoutComponentToUseDeclared;
import org.ow2.petals.plugin.jbiplugin.environement.ServiceAssemblyWithSimpleServiceUnitWithoutJBIComponentDependency;
import org.ow2.petals.plugin.jbiplugin.environement.ServiceAssemblyWithUpdateWithoutDescriptionInServiceUnitPomFile;
import org.ow2.petals.plugin.jbiplugin.environement.SimpleServiceUnitWithSeveralJBIComponentDependenciesWithComponentToUseDeclaredJBIDescriptorNotUpdated;
import org.ow2.petals.plugin.jbiplugin.environement.SimpleServiceUnitWithSeveralJBIComponentDependenciesWithComponentToUseDeclaredJBIDescriptorUpdated;
import org.ow2.petals.plugin.jbiplugin.environement.SimpleServiceUnitWithUpdate;
import org.ow2.petals.plugin.jbiplugin.environement.SimpleServiceUnitWithoutJBIComponentDependency;

/**
 * Test the jbi maven plugin about the goal 'jbi-package'.
 * 
 * @author Christophe DENEUX - Capgemini Sud
 */
public class JBIValidateMojoTest extends JBIAbstractTestMojo {

    /**
     * <p>
     * Test the goal 'jbi-validate' about a simple service unit providing a JBI component dependency in the project (POM
     * file).
     * </p>
     * <p>
     * Expected results: no error occurs, the project is validated.
     * </p>
     */
    @Test
    public void testValidateStandardServiceUnitWithJBIComponentDependency() throws Exception {

        // First, we generate the service unit JBI archive to configure
        final File serviceUnitPomFile = new File(new File(GlobalUnitTestEnvironnement.VALIDATE_UNIT_TESTS_SRC_HOME,
                SimpleServiceUnitWithUpdate.ARTIFACT_ID), "pom.xml");
        final JBIValidateMojo validateMojo = this.getValidateMojo(serviceUnitPomFile);

        validateMojo.execute();
    }

    /**
     * <p>
     * Test the goal 'jbi-validate' about a simple service unit without JBI component dependency provided in the project
     * (POM file).
     * </p>
     * <p>
     * Expected results: an error occurs about the missing JBI component dependency.
     * </p>
     */
    @Test
    public void testValidateStandardServiceUnitWithoutJBIComponentDependency() throws Exception {

        // First, we generate the service unit JBI archive to configure
        final File serviceUnitPomFile = new File(new File(GlobalUnitTestEnvironnement.VALIDATE_UNIT_TESTS_SRC_HOME,
                SimpleServiceUnitWithoutJBIComponentDependency.ARTIFACT_ID), "pom.xml");
        final JBIValidateMojo validateMojo = this.getValidateMojo(serviceUnitPomFile);

        try {
            validateMojo.execute();
            fail("Validation should fail because of the missing JBI component dependency.");
        } catch (final MojoFailureException e) {
            Assert.assertTrue(
                    e.getMessage().contains("A dependency as JBI component is required for a JBI service unit"));
        }
    }

    /**
     * <p>
     * Test the goal 'jbi-validate' about a simple service unit with several JBI component dependencies provided in the
     * project (POM file).
     * </p>
     * <p>
     * Expected results: an error occurs because of JBI component dependencies, only one is required.
     * </p>
     */
    @Test
    public void testValidateStandardServiceUnitWithSeveralJBIComponentDependency() throws Exception {

        // First, we generate the service unit JBI archive to configure
        final File serviceUnitPomFile = new File(new File(GlobalUnitTestEnvironnement.VALIDATE_UNIT_TESTS_SRC_HOME,
                ServiceAssemblyWithSimpleServiceUnitWithSeveralJBIComponentDependenciesWithoutComponentToUseDeclared.ARTIFACT_ID), "pom.xml");
        final JBIValidateMojo validateMojo = this.getValidateMojo(serviceUnitPomFile);

        try {
            validateMojo.execute();
            fail("Validation should fail because of too many JBI component dependencies.");
        } catch (final MojoFailureException e) {
            Assert.assertTrue(e.getMessage()
                    .contains("Several JBI component dependencies are declared for the JBI service unit"));
        }
    }

    /**
     * <p>
     * Test the goal 'jbi-validate' about a simple service unit with several JBI component dependencies provided in the
     * project (POM file). The component to use is configured and the JBI descriptor can be updated.
     * </p>
     * <p>
     * Expected results: no error occurs, the project is validated.
     * </p>
     */
    @Test
    public void testValidateStandardServiceUnitWithSeveralJBIComponentDependencyWithComponentToUseDeclaredJBIDescriptorUpdated()
            throws Exception {

        // First, we generate the service unit JBI archive to configure
        final File serviceUnitPomFile = new File(new File(GlobalUnitTestEnvironnement.VALIDATE_UNIT_TESTS_SRC_HOME,
                SimpleServiceUnitWithSeveralJBIComponentDependenciesWithComponentToUseDeclaredJBIDescriptorUpdated.ARTIFACT_ID),
                "pom.xml");
        final JBIValidateMojo validateMojo = this.getValidateMojo(serviceUnitPomFile);

        validateMojo.execute();
    }

    /**
     * <p>
     * Test the goal 'jbi-validate' about a simple service unit with several JBI component dependencies provided in the
     * project (POM file). The component to use is configured and the JBI descriptor can't be updated.
     * </p>
     * <p>
     * Expected results: no error occurs, the project is validated.
     * </p>
     */
    @Test
    public void testValidateStandardServiceUnitWithSeveralJBIComponentDependencyWithComponentToUseDeclaredJBIDescriptorNotUpdated()
            throws Exception {

        // First, we generate the service unit JBI archive to configure
        final File serviceUnitPomFile = new File(new File(GlobalUnitTestEnvironnement.VALIDATE_UNIT_TESTS_SRC_HOME,
                SimpleServiceUnitWithSeveralJBIComponentDependenciesWithComponentToUseDeclaredJBIDescriptorNotUpdated.ARTIFACT_ID),
                "pom.xml");
        final JBIValidateMojo validateMojo = this.getValidateMojo(serviceUnitPomFile);

        try {
            validateMojo.execute();
            fail("Validation should fail because of too many JBI component dependencies.");
        } catch (final MojoFailureException e) {
            Assert.assertTrue(e.getMessage()
                    .contains("Several JBI component dependencies are declared for the JBI service unit"));
        }
    }

    /**
     * <p>
     * Test the goal 'jbi-validate' about a simple SA embedding a simple service unit providing a JBI component
     * dependency in its project (POM file).
     * </p>
     * <p>
     * Expected results: no error occurs, the project is validated.
     * </p>
     */
    @Test
    public void testValidateStandardServiceAssemblyWhereServiceUnitDefinesOnlyOneJBIComponentDependency()
            throws Exception {

        // First, we generate the service assembly JBI archive to configure
        final File serviceAssemblyPomFile = new File(new File(GlobalUnitTestEnvironnement.VALIDATE_UNIT_TESTS_SRC_HOME,
                ServiceAssemblyWithUpdateWithoutDescriptionInServiceUnitPomFile.ARTIFACT_ID), "pom.xml");
        final JBIValidateMojo validateMojo = this.getValidateMojo(serviceAssemblyPomFile);

        validateMojo.execute();
    }

    /**
     * <p>
     * Test the goal 'jbi-validate' about a simple SA embedding a service unit with several JBI component dependencies
     * provided in its project (POM file).
     * </p>
     * <p>
     * Expected results: an error occurs because of JBI component dependencies, only one is required.
     * </p>
     */
    @Test
    public void testValidateStandardServiceAssemblyWhereServiceUnitDefinesSeveralJBIComponentDependencies()
            throws Exception {

        // First, we generate the service unit JBI archive to configure
        final File serviceUnitPomFile = new File(
                new File(GlobalUnitTestEnvironnement.VALIDATE_UNIT_TESTS_SRC_HOME,
                        ServiceAssemblyWithSimpleServiceUnitWithSeveralJBIComponentDependenciesWithoutComponentToUseDeclared.ARTIFACT_ID),
                "pom.xml");
        final JBIValidateMojo validateMojo = this.getValidateMojo(serviceUnitPomFile);

        try {
            validateMojo.execute();
            fail("Validation should fail because of too many JBI component dependencies.");
        } catch (final MojoFailureException e) {
            Assert.assertTrue(
                    e.getMessage()
                            .contains("Several JBI component dependencies are declared for the JBI service unit"));
        }
    }

    /**
     * <p>
     * Test the goal 'jbi-validate' about a simple SA embedding a service unit with several JBI component dependencies
     * provided in its project (POM file) and a component to use is declared.
     * </p>
     * <p>
     * Expected results: no error occurs, the project is validated.
     * </p>
     */
    @Test
    public void testValidateStandardServiceAssemblyWhereServiceUnitDefinesSeveralJBIComponentDependenciesWithComponentToUseDeclared()
            throws Exception {

        // First, we generate the service assembly JBI archive to configure
        final File serviceAssemblyPomFile = new File(new File(GlobalUnitTestEnvironnement.VALIDATE_UNIT_TESTS_SRC_HOME,
                ServiceAssemblyWithSimpleServiceUnitWithSeveralJBIComponentDependenciesWithComponentToUseDeclared.ARTIFACT_ID),
                "pom.xml");
        final JBIValidateMojo validateMojo = this.getValidateMojo(serviceAssemblyPomFile);

        validateMojo.execute();
    }

    /**
     * <p>
     * Test the goal 'jbi-validate' about a simple SA embedding a service unit without JBI component dependency provided
     * in its project (POM file).
     * </p>
     * <p>
     * Expected results: an error occurs because of JBI component dependencies, only one is required.
     * </p>
     */
    @Test
    public void testValidateStandardServiceAssemblyWhereServiceUnitDefinesNoJBIComponentDependency() throws Exception {

        // First, we generate the service unit JBI archive to configure
        final File serviceUnitPomFile = new File(new File(GlobalUnitTestEnvironnement.VALIDATE_UNIT_TESTS_SRC_HOME,
                ServiceAssemblyWithSimpleServiceUnitWithoutJBIComponentDependency.ARTIFACT_ID), "pom.xml");
        final JBIValidateMojo validateMojo = this.getValidateMojo(serviceUnitPomFile);

        try {
            validateMojo.execute();
            fail("Validation should fail because of the missing JBI component dependency.");
        } catch (final MojoFailureException e) {
            Assert.assertTrue(
                    e.getMessage().contains("A dependency as JBI component is required for a JBI service unit"));
        }
    }
}
