// Path: vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/UpgradeInfo.java
// public enum InstallationMode {
//     /**
//      * Run all new or changed actions.
//      */
//     ON_CHANGE,
// 
//     /**
//      * Completely disregarding previous executions.
//      */
//     ALWAYS
// }
// 
// Path: vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/UpgradeHandler.java
// public interface UpgradeHandler {
// 
//     /**
//      * Is called to check if all requirements are met to create and run
//      * {@link UpgradeAction}s provided by this handler.
//      * 
//      * @return
//      */
//     boolean isAvailable(InstallContext ctx);
// 
//     /**
//      * Called by {@link UpgradeInfo} to get the {@link UpgradeAction}s which are
//      * configured in the content package.
//      * 
//      * @param ctx
//      * @param info
//      * @return
//      * @throws RepositoryException
//      */
//     Iterable<UpgradeAction> create(InstallContext ctx, UpgradeInfo info) throws RepositoryException;
// 
// }


import static org.mockito.Mockito.*;
import java.util.Arrays;
import java.util.Collections;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.jackrabbit.JcrConstants;
import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.jackrabbit.vault.fs.io.ImportOptions;
import org.apache.jackrabbit.vault.packaging.InstallContext;
import org.apache.jackrabbit.vault.packaging.InstallContext.Phase;
import org.apache.jackrabbit.vault.packaging.PackageException;
import org.apache.jackrabbit.vault.packaging.VaultPackage;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.apache.sling.testing.mock.sling.junit.SlingContext;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import biz.netcentric.vlt.upgrade.UpgradeInfo.InstallationMode;
import biz.netcentric.vlt.upgrade.handler.UpgradeHandler;
        Mockito.when(info.getExecutedActions()).thenReturn(Collections.<UpgradeAction>emptySet());
    }

    @Test
    public void testExecutePrepare() throws Exception {
        Mockito.when(ctx.getPhase()).thenReturn(Phase.PREPARE);
        Mockito.when(ctx.getPackage().getId().getName()).thenReturn("testVaultPackage");
        Mockito.when(ctx.getPackage().getId().getGroup()).thenReturn("testVaultGroup");
        Mockito.when(ctx.getPackage().getId().getInstallationPath()).thenReturn("/test/installation/path");
        Mockito.when(ctx.getPackage().getId().getVersionString()).thenReturn("1.0.1-SNAPSHOT");

        sling.build().resource("/test/installation/path.zip/jcr:content/vlt:definition/upgrader/test", //
                "handler", "biz.netcentric.vlt.upgrade.UpgradeProcessorTest$TestHandler" //
        );

        processor.execute(ctx);

        Assert.assertEquals(1, processor.infos.size());
        Assert.assertTrue(processor.infos.get(0).getHandler() instanceof TestHandler);
        TestHandler handler = (TestHandler) processor.infos.get(0).getHandler();
        Mockito.verify(handler.action).execute(ctx);
    }

    @Test
    public void testExecuteInstalled() throws Exception {
        Mockito.when(ctx.getPhase()).thenReturn(Phase.INSTALLED);

        processor.infos = Arrays.asList(info);
        Mockito.when(info.getActions()).thenReturn(Collections.singletonMap(Phase.INSTALLED, Arrays.asList(action)));

// Path: vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/UpgradeInfo.java
// public enum InstallationMode {
//     /**
//      * Run all new or changed actions.
//      */
//     ON_CHANGE,
// 
//     /**
//      * Completely disregarding previous executions.
//      */
//     ALWAYS
// }
// 
// Path: vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/handler/UpgradeHandler.java
// public interface UpgradeHandler {
// 
//     /**
//      * Is called to check if all requirements are met to create and run
//      * {@link UpgradeAction}s provided by this handler.
//      * 
//      * @return
//      */
//     boolean isAvailable(InstallContext ctx);
// 
//     /**
//      * Called by {@link UpgradeInfo} to get the {@link UpgradeAction}s which are
//      * configured in the content package.
//      * 
//      * @param ctx
//      * @param info
//      * @return
//      * @throws RepositoryException
//      */
//     Iterable<UpgradeAction> create(InstallContext ctx, UpgradeInfo info) throws RepositoryException;
// 
// }


// Path: vault-upgrade-hook/src/test/java/biz/netcentric/vlt/upgrade/UpgradeProcessorTest.java
import static org.mockito.Mockito.*;
import java.util.Arrays;
import java.util.Collections;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.jackrabbit.JcrConstants;
import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.jackrabbit.vault.fs.io.ImportOptions;
import org.apache.jackrabbit.vault.packaging.InstallContext;
import org.apache.jackrabbit.vault.packaging.InstallContext.Phase;
import org.apache.jackrabbit.vault.packaging.PackageException;
import org.apache.jackrabbit.vault.packaging.VaultPackage;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.apache.sling.testing.mock.sling.junit.SlingContext;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import biz.netcentric.vlt.upgrade.UpgradeInfo.InstallationMode;
import biz.netcentric.vlt.upgrade.handler.UpgradeHandler;

        Mockito.when(info.getExecutedActions()).thenReturn(Collections.<UpgradeAction>emptySet());
    }

    @Test
    public void testExecutePrepare() throws Exception {
        Mockito.when(ctx.getPhase()).thenReturn(Phase.PREPARE);
        Mockito.when(ctx.getPackage().getId().getName()).thenReturn("testVaultPackage");
        Mockito.when(ctx.getPackage().getId().getGroup()).thenReturn("testVaultGroup");
        Mockito.when(ctx.getPackage().getId().getInstallationPath()).thenReturn("/test/installation/path");
        Mockito.when(ctx.getPackage().getId().getVersionString()).thenReturn("1.0.1-SNAPSHOT");

        sling.build().resource("/test/installation/path.zip/jcr:content/vlt:definition/upgrader/test", //
                "handler", "biz.netcentric.vlt.upgrade.UpgradeProcessorTest$TestHandler" //
        );

        processor.execute(ctx);

        Assert.assertEquals(1, processor.infos.size());
        Assert.assertTrue(processor.infos.get(0).getHandler() instanceof TestHandler);
        TestHandler handler = (TestHandler) processor.infos.get(0).getHandler();
        Mockito.verify(handler.action).execute(ctx);
    }

    @Test
    public void testExecuteInstalled() throws Exception {
        Mockito.when(ctx.getPhase()).thenReturn(Phase.INSTALLED);

        processor.infos = Arrays.asList(info);
        Mockito.when(info.getActions()).thenReturn(Collections.singletonMap(Phase.INSTALLED, Arrays.asList(action)));

        Mockito.when(info.getInstallationMode()).thenReturn(InstallationMode.ALWAYS);