Skip to content

Commit

Permalink
Generate run modes for configurations in order as expected by AEM Ana…
Browse files Browse the repository at this point in the history
…lyser Plugin.
  • Loading branch information
stefanseifert committed Nov 13, 2023
1 parent d1cd36d commit d7171d1
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 4 deletions.
6 changes: 6 additions & 0 deletions changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
xsi:schemaLocation="http://maven.apache.org/changes/1.0.0 http://maven.apache.org/plugins/maven-changes-plugin/xsd/changes-1.0.0.xsd">
<body>

<release version="1.3.4" date="not released">
<action type="fix" dev="sseifert">
Generate run modes for configurations in order as expected by AEM Analyser Plugin.
</action>
</release>

<release version="1.3.2" date="2023-03-27">
<action type="update" dev="sseifert">
Switch to Java 11 as minimum version.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.SortedSet;
Expand All @@ -42,8 +43,6 @@
import org.apache.sling.provisioning.model.Section;
import org.apache.sling.provisioning.model.io.ModelReader;

import com.google.common.collect.ImmutableList;

import io.wcm.devops.conga.generator.spi.context.FileContext;
import io.wcm.devops.conga.generator.util.FileUtil;

Expand Down Expand Up @@ -165,9 +164,9 @@ public static <R> List<R> visitOsgiConfigurations(Model model, ConfigConsumer<R>
* Get the relative path for a configuration
*/
private static String getPathForConfiguration(Configuration configuration, RunMode runMode) {
SortedSet<String> runModesList = new TreeSet<>();
SortedSet<String> runModesList = new TreeSet<>(new RunModeComparator());
if (runMode.getNames() != null) {
runModesList.addAll(ImmutableList.copyOf(runMode.getNames()));
runModesList.addAll(Arrays.asList(runMode.getNames()));
}

// run modes directory
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* #%L
* wcm.io
* %%
* Copyright (C) 2023 wcm.io
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package io.wcm.devops.conga.plugins.sling.util;

import java.io.Serializable;
import java.util.Comparator;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;

/**
* Comparator sorts run modes alphabetically.
* However, service run modes (author, publish) are always put at the first position.
*/
public class RunModeComparator implements Comparator<String>, Serializable {
private static final long serialVersionUID = 1L;

private static final Set<String> SERVICE_RUNMODES = Set.of("author", "publish");

@Override
public int compare(String runmode1, String runmode2) {
if (isServiceRunmode(runmode1) && !isServiceRunmode(runmode2)) {
return -1;
}
if (!isServiceRunmode(runmode1) && isServiceRunmode(runmode2)) {
return 1;
}
return StringUtils.compare(runmode1, runmode2);
}

private static boolean isServiceRunmode(@Nullable String runmode) {
return runmode != null && SERVICE_RUNMODES.contains(runmode);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ void testProvisioningExample() throws Exception {
assertExists("my.factory-my.pid.config");
assertExists("mode1/my.factory-my.pid2.config");
assertExists("mode2/my.pid2.config");
assertExists("publish.prod/my.pid2.config");

// validate repoinit statements
config = readConfig("org.apache.sling.jcr.repoinit.RepositoryInitializer-test.config");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* #%L
* wcm.io
* %%
* Copyright (C) 2023 wcm.io
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package io.wcm.devops.conga.plugins.sling.util;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Arrays;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Collectors;

import org.junit.jupiter.api.Test;

class RunModeComparatorTest {

@Test
void testCompare() {
assertEquals(List.of("mode1", "mode2"), comparedList("mode2", "mode1"));
assertEquals(List.of("publish", "mode1", "mode2", "prod"), comparedList("mode2", "mode1", "publish", "prod"));
assertEquals(List.of("author", "publish", "mode1", "mode2", "prod"), comparedList("mode2", "mode1", "publish", "prod", "author"));
}

private static List<String> comparedList(String... runmodes) {
SortedSet<String> set = new TreeSet<>(new RunModeComparator());
set.addAll(Arrays.asList(runmodes));
return set.stream().collect(Collectors.toList());
}

}
5 changes: 5 additions & 0 deletions conga-sling-plugin/src/test/resources/validProvisioning.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
my.pid2
stringProperty="value4"

[configurations runModes=publish,prod]

my.pid2
stringProperty="value5"


[:repoinit]
create path /repoinit/test1
Expand Down

0 comments on commit d7171d1

Please sign in to comment.