@Override public void execute(InstallContext ctx) throws TaskExecutionException { for (Task task : tasks) { task.execute(ctx); } }
public IsAuthorInstanceDelegateTask(String taskName, Task isAuthor, Task isPublic) { this(taskName, String.format("Execute task [%s] if this is author instance or [%s] if this is public instance", isAuthor == null ? DO_NOTHING : isAuthor.getDescription(), isPublic == null ? DO_NOTHING : isPublic.getDescription()), isAuthor, isPublic); }
@Test public void testExecute() throws RepositoryException, TaskExecutionException { // GIVEN session.getRootNode().addNode("someParent"); Task task = new CreateNodeTask("name", "/someParent", "someNode", NodeTypes.ContentNode.NAME); // WHEN task.execute(installContext); // THEN assertTrue("Node was created", session.nodeExists("/someParent/someNode")); assertEquals("Create node 'config:/someParent/someNode'.", task.getDescription()); } }
currentTask = task; log.debug("Module {}, executing {}", moduleDef, currentTask); task.execute(ctx); ctx.incExecutedTaskCount(); ctx.error("Could not install or update " + moduleDef.getName() + " module. Task '" + currentTask.getName() + "' failed. (" + ExceptionUtils.getRootCauseMessage(e) + ")", e); success = false; } catch (RuntimeException e) { ctx.error("Error while installing or updating " + moduleDef.getName() + " module. Task '" + (currentTask == null ? "<null>" : currentTask.getName()) + "' failed. (" + ExceptionUtils.getRootCauseMessage(e) + ")", e); ctx.setStatus(InstallStatus.installFailed); throw e;
@Test public void execute() throws Exception { // GIVEN Task task = new CreateNodePathTask("name", "/someParent/someNode", NodeTypes.ContentNode.NAME); // WHEN task.execute(installContext); // THEN assertTrue("Node was created", session.nodeExists("/someParent/someNode")); assertEquals("Create node path 'config:/someParent/someNode'.", task.getDescription()); } }
@Test public void testTaskExecutionExceptionInterruptsTasksAddsExplicitErrorMessage() throws RepositoryException, TaskExecutionException { final ModuleDefinition mod = new ModuleDefinition("foo", Version.parseVersion("2.3.4"), null, null); final InstallContextImpl ctx = createStrictMock(InstallContextImpl.class); final Delta d1 = createStrictMock(Delta.class); final Task t1 = createStrictMock(Task.class); final Task t2 = createStrictMock(Task.class); ctx.setCurrentModule(mod); expect(d1.getTasks()).andReturn(Arrays.asList(t1, t2)); t1.execute(ctx); expectLastCall().andThrow(new TaskExecutionException("boo")); expect(t1.getName()).andReturn("task#1").anyTimes(); ctx.error(eq("Could not install or update foo module. Task 'task#1' failed. (TaskExecutionException: boo)"), isA(TaskExecutionException.class)); ctx.setCurrentModule(null); expect(ctx.getJCRSession(anyObject())).andAnswer(new IAnswer<Session>() { public Session answer() throws Throwable { return repositoryManager.getSystemSession(getCurrentArguments()[0].toString()); } }).anyTimes(); replay(ctx, d1, t1, t2); final ModuleManager.ModuleAndDeltas moduleAndDeltas = new ModuleManager.ModuleAndDeltas(mod, Version.parseVersion("1.2.3"), Arrays.asList(d1)); new ModuleManagerImpl(ctx, Sets.newHashSet(new BetwixtModuleDefinitionReader()), moduleRegistry, new DependencyCheckerImpl(), new Node2BeanProcessorImpl(new TypeMappingImpl(), new Node2BeanTransformerImpl(new PreConfiguredBeanUtils())), repositoryManager).installOrUpdateModule(moduleAndDeltas, ctx); verify(ctx, d1, t1, t2); }
@Override public void execute(InstallContext ctx) throws TaskExecutionException { final Task task = condition(ctx) ? ifTrue : ifFalse; if (task != null) { task.execute(ctx); } }
@Test public void testExecute() throws RepositoryException, TaskExecutionException { // GIVEN Property property1 = NodeUtil.createPath(session.getRootNode(), "/demo-project/about/subsection-articles/article", NodeTypes.ContentNode.NAME).setProperty("someProperty", "someValue"); Task task = new RemovePropertyTask("name", property1.getParent().getPath(), "someProperty"); // WHEN task.execute(ctx); // THEN assertFalse("Property should have been removed", session.propertyExists("/demo-project/about/subsection-articles/article/someProperty")); assertEquals("Remove property 'config:/demo-project/about/subsection-articles/article/someProperty'.", task.getDescription()); } }
public ValueOfPropertyDelegateTask(String taskName, String parentPath, String propertyName, String expectedValue, boolean propertyMustExist, Task ifTrue, Task ifFalse) { this(taskName, String.format("Execute task [%s] if property '%s' has value '%s' or [%s] if not.", ifTrue == null ? DO_NOTHING : ifTrue.getDescription(), parentPath + "/" + propertyName, expectedValue, ifFalse == null ? DO_NOTHING : ifFalse.getDescription()), RepositoryConstants.CONFIG, parentPath, propertyName, expectedValue, propertyMustExist, ifTrue, ifFalse); }
@Override protected void doExecute(InstallContext ctx) throws RepositoryException, TaskExecutionException { Session session = ctx.getJCRSession(RepositoryConstants.CONFIG); for (Node node : NodeUtil.getNodes(session.getRootNode().getNode("server").getNode("security").getNode("userManagers"), NodeTypes.ContentNode.NAME)) { if (node.hasProperty("class")) { String className = node.getProperty("class").getString(); if (className.equals("info.magnolia.cms.security.MgnlUserManager") || className.equals("info.magnolia.cms.security.SystemUserManager")) { Task updateTask = new HasPropertyDelegateTask("", "", RepositoryConstants.CONFIG, "/server" + StringUtils.substringAfter(node.getPath(), "/server"), "maxFailedLoginAttempts", null, new NewPropertyTask("", "", RepositoryConstants.CONFIG, "/server" + StringUtils.substringAfter(node.getPath(), "/server"), "maxFailedLoginAttempts", "5")); Task updateTask2 = new HasPropertyDelegateTask("", "", RepositoryConstants.CONFIG, "/server" + StringUtils.substringAfter(node.getPath(), "/server"), "lockTimePeriod", null, new NewPropertyTask("", "", RepositoryConstants.CONFIG, "/server" + StringUtils.substringAfter(node.getPath(), "/server"), "lockTimePeriod", "0")); updateTask.execute(ctx); updateTask2.execute(ctx); } } } } }
@Test public void testOverride() throws Exception { // GIVEN sessionConfig.getRootNode().addNode("someSrcNode").setProperty("someProperty", "somePropertyValue"); sessionConfig.getRootNode().addNode("someDestNode").setProperty("someProperty", "someOldValue"); Task task = new MovePropertyTask("name", RepositoryConstants.CONFIG, "/someSrcNode/someProperty", "/someDestNode/someProperty", true); // WHEN task.execute(installContext); // THEN assertFalse(sessionConfig.propertyExists("/someSrcNode/someProperty")); assertTrue(sessionConfig.propertyExists("/someDestNode/someProperty")); assertEquals("somePropertyValue", sessionConfig.getProperty("/someDestNode/someProperty").getString()); assertEquals("Move property 'config:/someSrcNode/someProperty' to 'config:/someDestNode/someProperty'.", task.getDescription()); }
public IsModuleInstalledOrRegistered(String taskName, String moduleName, Task ifTrue, Task ifFalse) { this(taskName, String.format("Execute task [%s] if module '%s' is installed or task [%s] if not.", ifTrue == null ? DO_NOTHING : ifTrue.getDescription(), moduleName, ifFalse == null ? DO_NOTHING : ifFalse.getDescription()), moduleName, ifTrue, ifFalse); }
@Test public void ifFalseTaskIsRunIfPropertyDoesNotExist() throws Exception { // GIVEN Task task = new HasPropertyDelegateTask("taskName", "/someNode", "someProperty", ifTrue, ifFalse); session.getRootNode().addNode("someNode"); // WHEN task.execute(ctx); // THEN verify(ifTrue, times(0)).execute(ctx); verify(ifFalse, times(1)).execute(ctx); }
@Test public void override() throws Exception { // GIVEN sessionConfig.getRootNode().addNode("someSrcNode").setProperty("someProperty", "somePropertyValue"); sessionConfig.getRootNode().addNode("someDestNode").setProperty("someProperty", "someOldValue"); Task task = new CopyPropertyTask("name", RepositoryConstants.CONFIG, "/someSrcNode", "/someDestNode", "someProperty", true); // WHEN task.execute(installContext); // THEN assertTrue(sessionConfig.propertyExists("/someSrcNode/someProperty")); assertTrue(sessionConfig.propertyExists("/someDestNode/someProperty")); assertEquals("somePropertyValue", sessionConfig.getProperty("/someDestNode/someProperty").getString()); assertEquals("Copy property 'config:/someSrcNode' to 'config:/someDestNode'.", task.getDescription()); }
public HasPropertyDelegateTask(String taskName, String parentPath, String propertyName, Task ifTrue, Task ifFalse) { this(taskName, String.format("Execute task [%s] if property '%s' exists or [%s] if not.", ifTrue == null ? DO_NOTHING : ifTrue.getDescription(), parentPath + "/" + propertyName, ifFalse == null ? DO_NOTHING : ifFalse.getDescription()), RepositoryConstants.CONFIG, parentPath, propertyName, ifTrue, ifFalse); }
@Test public void testExecute() throws RepositoryException, TaskExecutionException { // GIVEN Property property1 = NodeUtil.createPath(websiteSession.getRootNode(), "/demo-project/about/subsection-articles/article", NodeTypes.ContentNode.NAME).setProperty("someProperty", "someValue"); String property1Path = property1.getPath(); List<String> pathsToRemove = Arrays.asList(property1Path, "/nonexistingPath"); Task task = new RemovePropertiesTask("name", RepositoryConstants.WEBSITE, pathsToRemove, true); // WHEN task.execute(ctx); // THEN assertFalse("Property should have been removed", websiteSession.propertyExists(property1Path)); assertEquals("Remove properties: '[/demo-project/about/subsection-articles/article/someProperty, /nonexistingPath]' from 'website' workspace.", task.getDescription()); } }
public NodeExistsDelegateTask(String name, String pathToCheck, Task ifTrue, Task ifFalse) { this(name, String.format("Execute task [%s] if node '%s' exists or [%s] if not.", ifTrue == null ? DO_NOTHING : ifTrue.getDescription(), pathToCheck, ifFalse == null ? DO_NOTHING : ifFalse.getDescription()), RepositoryConstants.CONFIG, pathToCheck, ifTrue, ifFalse); }
@Test public void ifTrueTaskIsRunIfNodeAndPropertyExist() throws Exception { // GIVEN Task task = new HasPropertyDelegateTask("taskName", "/someNode", "someProperty", ifTrue, ifFalse); session.getRootNode().addNode("someNode").setProperty("someProperty", "somePropertyValue"); // WHEN task.execute(ctx); // THEN verify(ifTrue, times(1)).execute(ctx); verify(ifFalse, times(0)).execute(ctx); } }
@Test public void testDoNotOverride() throws Exception { // GIVEN NodeUtil.createPath(websiteSession.getRootNode(), "/someNode/oldName", NodeTypes.ContentNode.NAME); NodeUtil.createPath(websiteSession.getRootNode(), "/someNode/newName", NodeTypes.ContentNode.NAME); // node with this name already exists Task task = new RenameNodeTask("name", RepositoryConstants.WEBSITE, "/someNode", "oldName", "newName", false); // we don't want to overwrite // WHEN task.execute(ctx); // THEN assertTrue("Node shouldn't have been renamed", websiteSession.nodeExists("/someNode/oldName")); assertEquals("Rename node 'website:/someNode/oldName' to 'newName'.", task.getDescription()); } }