UpgradeProcessor implements InstallHook { @Override public void execute(InstallContext ctx) throws PackageException { LOG.status(ctx, "phase [{}]", null, ctx.getPhase()); try { switch (ctx.getPhase()) { case PREPARE: loadStatus(ctx); loadInfos(ctx); executeActions(ctx); break; case PREPARE_FAILED: case INSTALLED: case INSTALL_FAILED: executeActions(ctx); break; case END: executeActionsAndSaveStatus(ctx); break; } } catch (Throwable e) { failed = true; if (ctx.getPhase() != Phase.PREPARE) { LOG.error(ctx, "Error during " + ctx.getPhase() + " phase, this will *not* mark the package installation as failed.", e); } throw new PackageException("Error during content upgrade", e); } finally { LOG.debug(ctx, "finished [{}]", ctx.getPhase()); } }  @Override void execute(InstallContext ctx); static final String STATUS_PATH; static final String UPGRADER_PATH_IN_PACKAGE; }
@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); processor.execute(ctx); Mockito.verify(action).execute(ctx); Mockito.reset(action); Mockito.when(info.getInstallationMode()).thenReturn(InstallationMode.ON_CHANGE); processor.execute(ctx); Mockito.verify(action, Mockito.never()).execute(ctx); Mockito.reset(action); Mockito.when(action.isRelevant(ctx, info)).thenReturn(true); processor.execute(ctx); Mockito.verify(action).execute(ctx); }
@Test public void testExecuteEnd() throws Exception { processor.status = status; processor.infos = Arrays.asList(info); Mockito.when(action.isRelevant(ctx, info)).thenReturn(true); Mockito.when(ctx.getPhase()).thenReturn(Phase.END); Mockito.when(info.getActions()).thenReturn(Collections.singletonMap(Phase.END, Arrays.asList(action))); JcrUtils.getOrCreateByPath("/test", JcrConstants.NT_UNSTRUCTURED, ctx.getSession()); Assert.assertTrue("Make sure the session has changed to verify save after execution.", ctx.getSession().hasPendingChanges()); processor.execute(ctx); Assert.assertFalse(ctx.getSession().hasPendingChanges()); Mockito.verify(info).executed(action); Mockito.verify(action).execute(ctx); Mockito.verify(status).update(ctx, false); Mockito.verify(status).update(ctx, info); }
@Test(expected = PackageException.class) public void testException() throws Exception { processor.infos = Arrays.asList(info); Mockito.when(ctx.getPhase()).thenReturn(Phase.INSTALLED); Mockito.doThrow(new IllegalArgumentException("testException")).when(info).getActions(); processor.execute(ctx); }
@Test public void testOrderedExecution() 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/second", "handler", "biz.netcentric.vlt.upgrade.UpgradeProcessorTest$TestHandler" ); sling.build().resource( "/test/installation/path.zip/jcr:content/vlt:definition/upgrader/first", "handler", "biz.netcentric.vlt.upgrade.UpgradeProcessorTest$TestHandler" ); processor.execute(ctx); Assert.assertEquals(2, processor.infos.size()); Assert.assertEquals("first", processor.infos.get(0).getNode().getName()); }
@Test(expected = PackageException.class) public void testActionException() throws Exception { processor.infos = Arrays.asList(info); Mockito.when(ctx.getPhase()).thenReturn(Phase.INSTALLED); Mockito.when(info.getActions()).thenReturn(Collections.singletonMap(Phase.INSTALLED, Arrays.asList(action))); Mockito.when(action.isRelevant(ctx, info)).thenReturn(true); Mockito.doThrow(new RuntimeException("testException")).when(action).execute(ctx); try { processor.execute(ctx); } catch (Exception e) { Assert.assertTrue(info.getExecutedActions().isEmpty()); throw e; } }