Skip to content

Commit 7159b20

Browse files
committed
refactor unit test for better readability/maintainability
1 parent 6cabe21 commit 7159b20

6 files changed

Lines changed: 434 additions & 285 deletions

File tree

tooling/conga-aem-maven-plugin/src/test/java/io/wcm/devops/conga/plugins/aem/maven/allpackage/AllPackageBuilderTest.java

Lines changed: 148 additions & 281 deletions
Large diffs are not rendered by default.

tooling/conga-aem-maven-plugin/src/test/java/io/wcm/devops/conga/plugins/aem/maven/allpackage/ContentPackageTestUtil.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ public static Document getXmlFromZip(File file, String path) throws Exception {
6363
return documentBuilder.parse(new ByteArrayInputStream(data));
6464
}
6565

66-
public static void assertXpathEvaluatesTo(String expected, String xpath, Node node) throws Exception {
67-
assertEquals(expected, XPATH_ENGINE.evaluate(xpath, node));
66+
public static void assertXpathEvaluatesTo(String expected, String xpath, Node node, String message) throws Exception {
67+
assertEquals(expected, XPATH_ENGINE.evaluate(xpath, node), message);
6868
}
6969

70-
public static void assertXpathEvaluatesTo(String expected, String xpath, Document doc) throws Exception {
71-
assertXpathEvaluatesTo(expected, xpath, doc.getDocumentElement());
70+
public static void assertXpathEvaluatesTo(String expected, String xpath, Document doc, String message) throws Exception {
71+
assertXpathEvaluatesTo(expected, xpath, doc.getDocumentElement(), message);
7272
}
7373

7474
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* #%L
3+
* wcm.io
4+
* %%
5+
* Copyright (C) 2022 wcm.io
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package io.wcm.devops.conga.plugins.aem.maven.allpackage;
21+
22+
import java.util.Collection;
23+
24+
import org.apache.commons.lang3.StringUtils;
25+
import org.jetbrains.annotations.NotNull;
26+
import org.jetbrains.annotations.Nullable;
27+
28+
class ExpectedContentPackage extends ExpectedFile {
29+
30+
private final String packageName;
31+
private final String version;
32+
private final Collection<ExpectedDependency> dependencies;
33+
34+
ExpectedContentPackage(@NotNull String packageName, @Nullable String version, @NotNull Collection<ExpectedDependency> dependencies) {
35+
super(buildFileName(packageName, version));
36+
this.packageName = packageName;
37+
this.version = version;
38+
this.dependencies = dependencies;
39+
}
40+
41+
private static String buildFileName(@NotNull String packageName, @Nullable String version) {
42+
StringBuilder sb = new StringBuilder();
43+
sb.append(packageName);
44+
if (version != null) {
45+
sb.append("-").append(version);
46+
}
47+
sb.append(".zip");
48+
return sb.toString();
49+
}
50+
51+
public @NotNull String getPackageName(@NotNull String runModeSuffix) {
52+
return StringUtils.replace(this.packageName, RUNMODE_SUFFIX_PLACEHOLDER, runModeSuffix);
53+
}
54+
55+
public @Nullable String getVersion() {
56+
return this.version;
57+
}
58+
59+
public @NotNull Collection<ExpectedDependency> getDependencies() {
60+
return this.dependencies;
61+
}
62+
63+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* #%L
3+
* wcm.io
4+
* %%
5+
* Copyright (C) 2022 wcm.io
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package io.wcm.devops.conga.plugins.aem.maven.allpackage;
21+
22+
import static io.wcm.devops.conga.plugins.aem.maven.allpackage.ExpectedFile.RUNMODE_SUFFIX_PLACEHOLDER;
23+
24+
import org.apache.commons.lang3.StringUtils;
25+
import org.jetbrains.annotations.NotNull;
26+
27+
class ExpectedDependency {
28+
29+
private final String packageReference;
30+
31+
ExpectedDependency(@NotNull String packageReference) {
32+
this.packageReference = packageReference;
33+
}
34+
35+
public @NotNull String getPackageReference(@NotNull String runModeSuffix) {
36+
return StringUtils.replace(this.packageReference, RUNMODE_SUFFIX_PLACEHOLDER, runModeSuffix);
37+
}
38+
39+
@Override
40+
public String toString() {
41+
return packageReference;
42+
}
43+
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* #%L
3+
* wcm.io
4+
* %%
5+
* Copyright (C) 2022 wcm.io
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package io.wcm.devops.conga.plugins.aem.maven.allpackage;
21+
22+
import org.apache.commons.lang3.StringUtils;
23+
import org.jetbrains.annotations.NotNull;
24+
25+
class ExpectedFile {
26+
27+
static final String RUNMODE_SUFFIX_PLACEHOLDER = "{runmode}";
28+
29+
private final String fileName;
30+
31+
ExpectedFile(@NotNull String fileName) {
32+
this.fileName = fileName;
33+
}
34+
35+
public @NotNull String getFileName(@NotNull String runModeSuffix) {
36+
return StringUtils.replace(this.fileName, RUNMODE_SUFFIX_PLACEHOLDER, runModeSuffix);
37+
}
38+
39+
@Override
40+
public String toString() {
41+
return fileName;
42+
}
43+
44+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* #%L
3+
* wcm.io
4+
* %%
5+
* Copyright (C) 2020 wcm.io
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package io.wcm.devops.conga.plugins.aem.maven.allpackage;
21+
22+
import static io.wcm.devops.conga.plugins.aem.maven.allpackage.ContentPackageTestUtil.assertXpathEvaluatesTo;
23+
import static io.wcm.devops.conga.plugins.aem.maven.allpackage.ContentPackageTestUtil.getXmlFromZip;
24+
import static org.junit.jupiter.api.Assertions.assertEquals;
25+
import static org.junit.jupiter.api.Assertions.assertTrue;
26+
27+
import java.io.File;
28+
import java.util.Arrays;
29+
import java.util.Collections;
30+
import java.util.Set;
31+
import java.util.stream.Collectors;
32+
import java.util.stream.Stream;
33+
34+
import org.jetbrains.annotations.NotNull;
35+
import org.jetbrains.annotations.Nullable;
36+
import org.w3c.dom.Document;
37+
38+
import com.google.common.collect.ImmutableSet;
39+
40+
public final class FileTestUtil {
41+
42+
private FileTestUtil() {
43+
// static methods only
44+
}
45+
46+
static @NotNull ExpectedFile file(@NotNull String fileName) {
47+
return new ExpectedFile(fileName);
48+
}
49+
50+
static @NotNull ExpectedContentPackage contentPackage(@NotNull String packageName,
51+
@NotNull ExpectedDependency @NotNull... dependencies) {
52+
return new ExpectedContentPackage(packageName, null, Arrays.asList(dependencies));
53+
}
54+
55+
static @NotNull ExpectedContentPackage contentPackage(@NotNull String packageName, @Nullable String version,
56+
@NotNull ExpectedDependency @NotNull... dependencies) {
57+
return new ExpectedContentPackage(packageName, version, Arrays.asList(dependencies));
58+
}
59+
60+
static @NotNull ExpectedDependency dep(@NotNull String packageReference) {
61+
return new ExpectedDependency(packageReference);
62+
}
63+
64+
65+
/**
66+
* Assert existence of given content packages (with dependencies) and other files.
67+
* @param dir Directory
68+
* @param expectedFiles Expected content packages/files and their dependencies
69+
*/
70+
public static void assertFiles(File dir, String runmodeSuffix, ExpectedFile... expectedFiles) throws Exception {
71+
assertTrue(dir.exists(), "file exists: " + dir.getPath());
72+
assertTrue(dir.isDirectory(), "is directory: " + dir.getPath());
73+
74+
// assert existing files match the expected files
75+
Set<String> expectedFileNames = Stream.of(expectedFiles)
76+
.map(file -> file.getFileName(runmodeSuffix))
77+
.collect(Collectors.toSet());
78+
String[] fileNames = dir.list();
79+
Set<String> actualFileNames = fileNames != null ? ImmutableSet.copyOf(fileNames) : ImmutableSet.of();
80+
assertEquals(expectedFileNames, actualFileNames, "files in " + dir.getPath());
81+
82+
// extra check for content packages
83+
for (ExpectedFile file : expectedFiles) {
84+
if (file instanceof ExpectedContentPackage) {
85+
ExpectedContentPackage cp = (ExpectedContentPackage)file;
86+
File zipFile = new File(dir, cp.getFileName(runmodeSuffix));
87+
Document propsXml = getXmlFromZip(zipFile, "META-INF/vault/properties.xml");
88+
89+
// assert package name
90+
String expectedPackageName = cp.getPackageName(runmodeSuffix);
91+
assertXpathEvaluatesTo(expectedPackageName, "/properties/entry[@key='name']", propsXml,
92+
"Package name of " + file.getFileName(runmodeSuffix));
93+
94+
// assert package version
95+
String expectedVersion = cp.getVersion();
96+
if (expectedVersion != null) {
97+
assertXpathEvaluatesTo(expectedVersion, "/properties/entry[@key='version']", propsXml,
98+
"Package version of " + file.getFileName(runmodeSuffix));
99+
}
100+
101+
// assert package dependencies
102+
String expectedDependencies = cp.getDependencies().stream()
103+
.map(dep -> dep.getPackageReference(runmodeSuffix))
104+
.collect(Collectors.joining(","));
105+
assertXpathEvaluatesTo(expectedDependencies, "/properties/entry[@key='dependencies']", propsXml,
106+
"Package dependencies of " + file.getFileName(runmodeSuffix));
107+
}
108+
}
109+
}
110+
111+
/**
112+
* Assert existence of given directories.
113+
* @param dir Directory
114+
* @param fileNames Expected file names in directory
115+
*/
116+
public static void assertDirectories(File dir, String... fileNames) {
117+
assertTrue(dir.exists(), "file exists: " + dir.getPath());
118+
assertTrue(dir.isDirectory(), "is directory: " + dir.getPath());
119+
Set<String> expectedDirectoryNames = ImmutableSet.copyOf(fileNames);
120+
Set<String> actualDirectoryNames = Collections.emptySet();
121+
File[] files = dir.listFiles();
122+
if (files != null) {
123+
actualDirectoryNames = Stream.of(files)
124+
.filter(File::isDirectory)
125+
.map(File::getName)
126+
.collect(Collectors.toSet());
127+
}
128+
assertEquals(expectedDirectoryNames, actualDirectoryNames, "files in " + dir.getPath());
129+
}
130+
131+
}

0 commit comments

Comments
 (0)