diff --git a/accesscontroltool-bundle/src/main/java/biz/netcentric/cq/tools/actool/history/impl/AcToolExecutionImpl.java b/accesscontroltool-bundle/src/main/java/biz/netcentric/cq/tools/actool/history/impl/AcToolExecutionImpl.java index 94601801..c1b480c3 100644 --- a/accesscontroltool-bundle/src/main/java/biz/netcentric/cq/tools/actool/history/impl/AcToolExecutionImpl.java +++ b/accesscontroltool-bundle/src/main/java/biz/netcentric/cq/tools/actool/history/impl/AcToolExecutionImpl.java @@ -85,7 +85,11 @@ public int getAclChanges() { @Override public int compareTo(AcToolExecution otherExecution) { - return -getInstallationDate().compareTo(otherExecution.getInstallationDate()); + int compareByDate = -getInstallationDate().compareTo(otherExecution.getInstallationDate()); + if (compareByDate != 0) { + return compareByDate; + } + return getId().compareTo(otherExecution.getId()); } } diff --git a/accesscontroltool-bundle/src/main/java/biz/netcentric/cq/tools/actool/history/impl/HistoryUtils.java b/accesscontroltool-bundle/src/main/java/biz/netcentric/cq/tools/actool/history/impl/HistoryUtils.java index ddf096ce..4bf027af 100644 --- a/accesscontroltool-bundle/src/main/java/biz/netcentric/cq/tools/actool/history/impl/HistoryUtils.java +++ b/accesscontroltool-bundle/src/main/java/biz/netcentric/cq/tools/actool/history/impl/HistoryUtils.java @@ -79,6 +79,8 @@ public class HistoryUtils { public static final String PROPERTY_CONFIG_ROOT_PATH = "configurationRootPath"; public static final String PROPERTY_ACL_CHANGES = "aclsChanges"; public static final String PROPERTY_AUTHORIZABLES_CHANGES = "authorizableChanges"; + static final String TRIGGER_INSTALL_HOOK = "installhook"; + static final String TRIGGER_STARTUP_HOOK_PREFIX = "startup_hook"; private static final String AC_TOOL_STARTUPHOOK_CLASS = "biz.netcentric.cq.tools.actool.startuphook.impl.AcToolStartupHookServiceImpl"; private static final String BUNDLE_START_TASK_CLASS = "org.apache.sling.installer.core.impl.tasks.BundleStartTask"; @@ -114,7 +116,7 @@ public static Node persistHistory(final Session session, String trigger; if (StringUtils.isNotBlank(installLog.getCrxPackageName())) { - trigger = "installhook"; + trigger = TRIGGER_INSTALL_HOOK; } else if(isInStrackTracke(stackTrace, AceServiceMBeanImpl.class)) { trigger = "jmx"; } else if(isInStrackTracke(stackTrace, AC_TOOL_TOUCH_UI_SERVLET_CLASS)) { @@ -273,6 +275,11 @@ static List getAcToolExecutions(final Session session) String configRoot = node.hasProperty(PROPERTY_CONFIG_ROOT_PATH)? node.getProperty(PROPERTY_CONFIG_ROOT_PATH).getString() : null; int authorizableChanges = node.hasProperty(PROPERTY_AUTHORIZABLES_CHANGES) ? (int) node.getProperty(PROPERTY_AUTHORIZABLES_CHANGES).getLong() : -1; int aclChanges = node.hasProperty(PROPERTY_ACL_CHANGES) ? (int) node.getProperty(PROPERTY_ACL_CHANGES).getLong() : -1; + String trigger = node.hasProperty(PROPERTY_TRIGGER) ? node.getProperty(PROPERTY_TRIGGER).getString() : null; + + if (!shouldExposeExecution(trigger, authorizableChanges, aclChanges)) { + continue; + } historyInfos.add(new AcToolExecutionImpl(getIdFromPath(node.getPath()), node.getPath(), @@ -285,6 +292,13 @@ static List getAcToolExecutions(final Session session) return new ArrayList<>(historyInfos); } + static boolean shouldExposeExecution(String trigger, int authorizableChanges, int aclChanges) { + boolean hasNoChanges = authorizableChanges == 0 && aclChanges == 0; + boolean isAutomaticExecution = StringUtils.equals(trigger, TRIGGER_INSTALL_HOOK) + || StringUtils.startsWith(trigger, TRIGGER_STARTUP_HOOK_PREFIX); + return !hasNoChanges || !isAutomaticExecution; + } + static String getIdFromPath(String path) { return StringUtils.removeStart(Text.getName(path), HISTORY_NODE_NAME_PREFIX); } diff --git a/accesscontroltool-bundle/src/test/java/biz/netcentric/cq/tools/actool/history/impl/HistoryUtilsTest.java b/accesscontroltool-bundle/src/test/java/biz/netcentric/cq/tools/actool/history/impl/HistoryUtilsTest.java index de995dcc..4591340b 100644 --- a/accesscontroltool-bundle/src/test/java/biz/netcentric/cq/tools/actool/history/impl/HistoryUtilsTest.java +++ b/accesscontroltool-bundle/src/test/java/biz/netcentric/cq/tools/actool/history/impl/HistoryUtilsTest.java @@ -16,8 +16,14 @@ import static org.junit.jupiter.api.Assertions.*; +import java.util.Date; +import java.util.Set; +import java.util.TreeSet; + import org.junit.jupiter.api.Test; +import biz.netcentric.cq.tools.actool.history.AcToolExecution; + class HistoryUtilsTest { @Test @@ -31,4 +37,37 @@ void testGetPathFromId() { assertEquals("/base/history_test", HistoryUtils.getPathFromId("test", "/base")); assertEquals("/deeply/nested/base/history_test", HistoryUtils.getPathFromId("test", "/deeply/nested/base")); } + + @Test + void testExecutionsWithSameTimestampAreBothRetained() { + Date sameTimestamp = new Date(); + Set executions = new TreeSet<>(); + executions.add(new AcToolExecutionImpl("100_via_jmx", + "/var/statistics/achistory/history_100_via_jmx", sameTimestamp, true, "/apps/test", 0, 0)); + executions.add(new AcToolExecutionImpl("101_via_jmx", + "/var/statistics/achistory/history_101_via_jmx", sameTimestamp, true, "/apps/test", 0, 0)); + + assertEquals(2, executions.size()); + assertEquals("100_via_jmx", executions.iterator().next().getId()); + } + + @Test + void testShouldExposeExecutionForManualNoChangeRun() { + assertTrue(HistoryUtils.shouldExposeExecution("jmx", 0, 0)); + assertTrue(HistoryUtils.shouldExposeExecution("aem_admin_ui", 0, 0)); + assertTrue(HistoryUtils.shouldExposeExecution("webconsole", 0, 0)); + } + + @Test + void testShouldNotExposeExecutionForAutomaticNoChangeRun() { + assertFalse(HistoryUtils.shouldExposeExecution("installhook", 0, 0)); + assertFalse(HistoryUtils.shouldExposeExecution("startup_hook", 0, 0)); + assertFalse(HistoryUtils.shouldExposeExecution("startup_hook_image_build", 0, 0)); + } + + @Test + void testShouldExposeExecutionWhenChangesExist() { + assertTrue(HistoryUtils.shouldExposeExecution("installhook", 1, 0)); + assertTrue(HistoryUtils.shouldExposeExecution("startup_hook", 0, 1)); + } }