/**
 * Copyright (c) 2021-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 static org.junit.Assert.assertEquals;

import java.lang.reflect.InvocationTargetException;
import java.util.Map.Entry;
import java.util.Properties;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.versioning.VersionRange;
import org.junit.Test;

import com.ebmwebsourcing.easycommons.lang.reflect.ReflectionHelper;

public class JBIAbstractConfigurableMojoTest {

    private class MyTestArtifact extends DefaultArtifact {

        public MyTestArtifact(final String baseVersion) {
            super("myGroupId", "myArtifactId", VersionRange.createFromVersion(baseVersion), null, "", "", null);
            this.setVersion(baseVersion);
        }
    }

    @Test
    public void test_setVersionElementInProperties() throws InvocationTargetException {

        final String versionMajor = "5";
        final String versionMinor = "8";
        final String versionMaintenance = "12";

        final Properties expectedProps = new Properties();
        expectedProps.setProperty(JBIAbstractConfigurableMojo.VERSION_MAJOR, versionMajor);
        expectedProps.setProperty(JBIAbstractConfigurableMojo.VERSION_MINOR, versionMinor);

        invokePrivateMethod(new MyTestArtifact(versionMajor + "." + versionMinor + "." + versionMaintenance),
                expectedProps);
        invokePrivateMethod(new MyTestArtifact(versionMajor + "-" + versionMinor + "-" + versionMaintenance),
                expectedProps);
        invokePrivateMethod(
                new MyTestArtifact(versionMajor + "." + versionMinor + "." + versionMaintenance + "-SNAPSHOT"),
                expectedProps);
        invokePrivateMethod(
                new MyTestArtifact(versionMajor + "-" + versionMinor + "-" + versionMaintenance + "-SNAPSHOT"),
                expectedProps);
        invokePrivateMethod(new MyTestArtifact(versionMajor + "." + versionMinor), expectedProps);
        invokePrivateMethod(new MyTestArtifact(versionMajor + "-" + versionMinor), expectedProps);
        invokePrivateMethod(new MyTestArtifact(versionMajor + "." + versionMinor + "-SNAPSHOT"), expectedProps);
        invokePrivateMethod(new MyTestArtifact(versionMajor + "-" + versionMinor + "-SNAPSHOT"), expectedProps);

        expectedProps.setProperty(JBIAbstractConfigurableMojo.VERSION_MINOR, "");

        invokePrivateMethod(new MyTestArtifact(versionMajor), expectedProps);
        invokePrivateMethod(new MyTestArtifact(versionMajor + "-SNAPSHOT"), expectedProps);
    }

    private static void invokePrivateMethod(final Artifact artifact, final Properties expectedProps)
            throws InvocationTargetException {

        final Properties actualProps = new Properties();
        ReflectionHelper.invokePrivateMethod(JBIAbstractConfigurableMojo.class, null, "setVersionElementInProperties",
                new Object[] { artifact, actualProps }, new Class[] { Artifact.class, Properties.class });

        for (final Entry<Object, Object> prop : expectedProps.entrySet()) {
            assertEquals(prop.getValue(), actualProps.get(prop.getKey()));
        }
    }

}
