Skip to content

Commit

Permalink
eliminate usage of guava collections in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed Jan 10, 2024
1 parent d95c39c commit d28d37d
Show file tree
Hide file tree
Showing 41 changed files with 478 additions and 532 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@
import org.mockito.stubbing.Answer;

import com.github.jknack.handlebars.Template;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

import io.wcm.devops.conga.generator.spi.FileHeaderPlugin;
import io.wcm.devops.conga.generator.spi.ImplicitApplyOptions;
Expand Down Expand Up @@ -91,7 +89,7 @@ void setUp(TestInfo testInfo) {
when(pluginManager.getAll(FileHeaderPlugin.class)).thenAnswer(new Answer<List<FileHeaderPlugin>>() {
@Override
public List<FileHeaderPlugin> answer(InvocationOnMock invocation) throws Throwable {
return ImmutableList.copyOf(fileHeaderPlugins.values());
return List.copyOf(fileHeaderPlugins.values());
}
});
when(pluginManager.get(anyString(), eq(FileHeaderPlugin.class))).thenAnswer(new Answer<FileHeaderPlugin>() {
Expand All @@ -111,9 +109,9 @@ public FileHeaderPlugin answer(InvocationOnMock invocation) throws Throwable {
VariableMapResolver variableMapResolver = new VariableMapResolver(
new ValueProviderGlobalContext().pluginContextOptions(pluginContextOptions));
underTest = new FileGenerator(options, "env1",
"role1", ImmutableList.of("variant1"), "template1",
destDir, file, null, null, roleFile, ImmutableMap.<String, Object>of(), template,
variableMapResolver, urlFileManager, pluginContextOptions, ImmutableList.of(
"role1", List.of("variant1"), "template1",
destDir, file, null, null, roleFile, Map.<String, Object>of(), template,
variableMapResolver, urlFileManager, pluginContextOptions, List.of(
"version1/1.0.0",
"version2/2.0.0-SNAPSHOT",
"version3/1.2.0-SNAPSHOT",
Expand All @@ -125,7 +123,7 @@ public FileHeaderPlugin answer(InvocationOnMock invocation) throws Throwable {
void testWithoutFileHeader() throws Exception {
FileHeaderPlugin one = mockFileHeader("one", "txt", ImplicitApplyOptions.NEVER);

List<GeneratedFileContext> result = ImmutableList.copyOf(underTest.generate());
List<GeneratedFileContext> result = List.copyOf(underTest.generate());

assertEquals(1, result.size());
assertItem(result.get(0), "test.txt");
Expand All @@ -138,7 +136,7 @@ void testExplicitFileHeader() throws Exception {
FileHeaderPlugin one = mockFileHeader("one", "txt", ImplicitApplyOptions.NEVER);
roleFile.setFileHeader("one");

List<GeneratedFileContext> result = ImmutableList.copyOf(underTest.generate());
List<GeneratedFileContext> result = List.copyOf(underTest.generate());

assertEquals(1, result.size());
assertItem(result.get(0), "test.txt");
Expand All @@ -150,7 +148,7 @@ void testExplicitFileHeader() throws Exception {
void testImplicitFileHeader() throws Exception {
FileHeaderPlugin one = mockFileHeader("one", "txt", ImplicitApplyOptions.WHEN_UNCONFIGURED);

List<GeneratedFileContext> result = ImmutableList.copyOf(underTest.generate());
List<GeneratedFileContext> result = List.copyOf(underTest.generate());

assertEquals(1, result.size());
assertItem(result.get(0), "test.txt");
Expand All @@ -162,7 +160,7 @@ void testImplicitFileHeader() throws Exception {
void testAlwaysFileHeader() throws Exception {
FileHeaderPlugin one = mockFileHeader("one", "txt", ImplicitApplyOptions.ALWAYS);

List<GeneratedFileContext> result = ImmutableList.copyOf(underTest.generate());
List<GeneratedFileContext> result = List.copyOf(underTest.generate());

assertEquals(1, result.size());
assertItem(result.get(0), "test.txt");
Expand All @@ -175,7 +173,7 @@ void testImplicitAndAlwaysFileHeader() throws Exception {
FileHeaderPlugin one = mockFileHeader("one", "txt", ImplicitApplyOptions.WHEN_UNCONFIGURED);
FileHeaderPlugin two = mockFileHeader("two", "txt", ImplicitApplyOptions.ALWAYS);

List<GeneratedFileContext> result = ImmutableList.copyOf(underTest.generate());
List<GeneratedFileContext> result = List.copyOf(underTest.generate());

assertEquals(1, result.size());
assertItem(result.get(0), "test.txt");
Expand All @@ -189,7 +187,7 @@ void testVersions() throws Exception {
FileHeaderPlugin one = mockFileHeader("one", "txt", ImplicitApplyOptions.NEVER);
roleFile.setFileHeader("one");

List<GeneratedFileContext> result = ImmutableList.copyOf(underTest.generate());
List<GeneratedFileContext> result = List.copyOf(underTest.generate());

assertEquals(1, result.size());
assertItem(result.get(0), "test.txt");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@
import org.slf4j.Logger;

import com.github.jknack.handlebars.Template;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.wcm.devops.conga.generator.spi.FileHeaderPlugin;
Expand Down Expand Up @@ -105,7 +102,7 @@ void setUp(TestInfo testInfo) {
when(pluginManager.getAll(PostProcessorPlugin.class)).thenAnswer(new Answer<List<PostProcessorPlugin>>() {
@Override
public List<PostProcessorPlugin> answer(InvocationOnMock invocation) throws Throwable {
return ImmutableList.copyOf(postProcessorPlugins.values());
return List.copyOf(postProcessorPlugins.values());
}
});
when(pluginManager.get(anyString(), eq(PostProcessorPlugin.class))).thenAnswer(new Answer<PostProcessorPlugin>() {
Expand All @@ -125,32 +122,32 @@ public PostProcessorPlugin answer(InvocationOnMock invocation) throws Throwable
variableMapResolver = new VariableMapResolver(
new ValueProviderGlobalContext().pluginContextOptions(pluginContextOptions));
underTest = new FileGenerator(options, "env1",
"role1", ImmutableList.of("variant1"), "template1",
destDir, file, null, null, roleFile, ImmutableMap.<String, Object>of(), template,
variableMapResolver, urlFileManager, pluginContextOptions, ImmutableList.of());
"role1", List.of("variant1"), "template1",
destDir, file, null, null, roleFile, Map.<String, Object>of(), template,
variableMapResolver, urlFileManager, pluginContextOptions, List.of());
}

@Test
void testWithoutPostProcessor() throws Exception {
PostProcessorPlugin one = mockPostProcessor("one", "txt", ImplicitApplyOptions.NEVER);

List<GeneratedFileContext> result = ImmutableList.copyOf(underTest.generate());
List<GeneratedFileContext> result = List.copyOf(underTest.generate());

assertEquals(1, result.size());
assertItem(result.get(0), "test.txt", ImmutableMap.of(), ImmutableSet.of());
assertItem(result.get(0), "test.txt", Map.of(), Set.of());

verify(one, never()).apply(any(FileContext.class), any(PostProcessorContext.class));
}

@Test
void testOnePostProcessor() throws Exception {
PostProcessorPlugin one = mockPostProcessor("one", "txt", ImplicitApplyOptions.NEVER);
roleFile.setPostProcessors(ImmutableList.of("one"));
roleFile.setPostProcessors(List.of("one"));

List<GeneratedFileContext> result = ImmutableList.copyOf(underTest.generate());
List<GeneratedFileContext> result = List.copyOf(underTest.generate());

assertEquals(1, result.size());
assertItem(result.get(0), "test.txt", ImmutableMap.of(), ImmutableSet.of("one"));
assertItem(result.get(0), "test.txt", Map.of(), Set.of("one"));

verify(one, times(1)).apply(any(FileContext.class), any(PostProcessorContext.class));
}
Expand All @@ -165,17 +162,17 @@ void testOnePostProcessorWithRewrite() throws Exception {
public List<FileContext> answer(InvocationOnMock invocation) throws Throwable {
// delete input file and create new file test.abc instead
FileContext input = invocation.getArgument(0);
assertItem(input, "test.txt", ImmutableMap.of());
assertItem(input, "test.txt", Map.of());
input.getFile().delete();
return ImmutableList.of(newFile("test.abc"));
return List.of(newFile("test.abc"));
}
});
roleFile.setPostProcessors(ImmutableList.of("one"));
roleFile.setPostProcessors(List.of("one"));

List<GeneratedFileContext> result = ImmutableList.copyOf(underTest.generate());
List<GeneratedFileContext> result = List.copyOf(underTest.generate());

assertEquals(1, result.size());
assertItem(result.get(0), "test.abc", ImmutableMap.of(), ImmutableSet.of("one"));
assertItem(result.get(0), "test.abc", Map.of(), Set.of("one"));

verify(one, times(1)).apply(any(FileContext.class), any(PostProcessorContext.class));
}
Expand All @@ -189,20 +186,20 @@ void testOnePostProcessorWithRewrite_WithFileHeaderAndValidator() throws Excepti
public List<FileContext> answer(InvocationOnMock invocation) throws Throwable {
// delete input file and create new file test.abc instead
FileContext input = invocation.getArgument(0);
assertItem(input, "test.txt", ImmutableMap.of());
assertItem(input, "test.txt", Map.of());
input.getFile().delete();
return ImmutableList.of(newFile("test.abc"));
return List.of(newFile("test.abc"));
}
});
roleFile.setPostProcessors(ImmutableList.of("one"));
roleFile.setPostProcessors(List.of("one"));

roleFile.setPostProcessorOptions(ImmutableMap.of(
roleFile.setPostProcessorOptions(Map.of(
FileGenerator.POSTPROCESSOR_KEY_FILE_HEADER, "my-fileheader",
FileGenerator.POSTPROCESSOR_KEY_VALIDATORS, ImmutableList.of("my-validator")));
FileGenerator.POSTPROCESSOR_KEY_VALIDATORS, List.of("my-validator")));
underTest = new FileGenerator(options, "env1",
"role1", ImmutableList.of("variant1"), "template1",
destDir, file, null, null, roleFile, ImmutableMap.<String, Object>of(), template,
variableMapResolver, urlFileManager, pluginContextOptions, ImmutableList.of());
"role1", List.of("variant1"), "template1",
destDir, file, null, null, roleFile, Map.<String, Object>of(), template,
variableMapResolver, urlFileManager, pluginContextOptions, List.of());

FileHeaderPlugin fileHeaderPlugin = mock(FileHeaderPlugin.class);
when(pluginManager.get(eq("my-fileheader"), eq(FileHeaderPlugin.class))).thenAnswer(new Answer<FileHeaderPlugin>() {
Expand All @@ -220,10 +217,10 @@ public ValidatorPlugin answer(InvocationOnMock invocation) throws Throwable {
}
});

List<GeneratedFileContext> result = ImmutableList.copyOf(underTest.generate());
List<GeneratedFileContext> result = List.copyOf(underTest.generate());

assertEquals(1, result.size());
assertItem(result.get(0), "test.abc", ImmutableMap.of(), ImmutableSet.of("one"));
assertItem(result.get(0), "test.abc", Map.of(), Set.of("one"));

verify(one, times(1)).apply(any(FileContext.class), any(PostProcessorContext.class));
verify(fileHeaderPlugin, times(1)).apply(any(FileContext.class), any(FileHeaderContext.class));
Expand All @@ -234,12 +231,12 @@ public ValidatorPlugin answer(InvocationOnMock invocation) throws Throwable {
void testTwoPostProcessors() throws Exception {
PostProcessorPlugin one = mockPostProcessor("one", "txt", ImplicitApplyOptions.NEVER);
PostProcessorPlugin two = mockPostProcessor("two", "txt", ImplicitApplyOptions.NEVER);
roleFile.setPostProcessors(ImmutableList.of("one", "two"));
roleFile.setPostProcessors(List.of("one", "two"));

List<GeneratedFileContext> result = ImmutableList.copyOf(underTest.generate());
List<GeneratedFileContext> result = List.copyOf(underTest.generate());

assertEquals(1, result.size());
assertItem(result.get(0), "test.txt", ImmutableMap.of(), ImmutableSet.of("one", "two"));
assertItem(result.get(0), "test.txt", Map.of(), Set.of("one", "two"));

verify(one, times(1)).apply(any(FileContext.class), any(PostProcessorContext.class));
verify(two, times(1)).apply(any(FileContext.class), any(PostProcessorContext.class));
Expand All @@ -254,9 +251,9 @@ void testTwoPostProcessorsWithRewrite() throws Exception {
public List<FileContext> answer(InvocationOnMock invocation) throws Throwable {
// delete input file and create new file test.abc instead
FileContext input = invocation.getArgument(0);
assertItem(input, "test.txt", ImmutableMap.of());
assertItem(input, "test.txt", Map.of());
input.getFile().delete();
return ImmutableList.of(newFile("test.abc"));
return List.of(newFile("test.abc"));
}
});

Expand All @@ -266,28 +263,28 @@ public List<FileContext> answer(InvocationOnMock invocation) throws Throwable {
public List<FileContext> answer(InvocationOnMock invocation) throws Throwable {
// create new file test.def
FileContext input = invocation.getArgument(0);
assertItem(input, "test.abc", ImmutableMap.of());
return ImmutableList.of(newFile("test.def"));
assertItem(input, "test.abc", Map.of());
return List.of(newFile("test.def"));
}
});

roleFile.setPostProcessors(ImmutableList.of("one", "two"));
roleFile.setPostProcessors(List.of("one", "two"));

List<GeneratedFileContext> result = ImmutableList.copyOf(underTest.generate());
List<GeneratedFileContext> result = List.copyOf(underTest.generate());

assertEquals(2, result.size());
assertItem(result.get(0), "test.abc", ImmutableMap.of(), ImmutableSet.of("one", "two"));
assertItem(result.get(1), "test.def", ImmutableMap.of(), ImmutableSet.of("two"));
assertItem(result.get(0), "test.abc", Map.of(), Set.of("one", "two"));
assertItem(result.get(1), "test.def", Map.of(), Set.of("two"));
}

@Test
void testImplicitPostProcessor() throws Exception {
PostProcessorPlugin one = mockPostProcessor("one", "txt", ImplicitApplyOptions.WHEN_UNCONFIGURED);

List<GeneratedFileContext> result = ImmutableList.copyOf(underTest.generate());
List<GeneratedFileContext> result = List.copyOf(underTest.generate());

assertEquals(1, result.size());
assertItem(result.get(0), "test.txt", ImmutableMap.of(), ImmutableSet.of("one"));
assertItem(result.get(0), "test.txt", Map.of(), Set.of("one"));

verify(one, times(1)).apply(any(FileContext.class), any(PostProcessorContext.class));
}
Expand All @@ -296,10 +293,10 @@ void testImplicitPostProcessor() throws Exception {
void testAlwaysPostProcessor() throws Exception {
PostProcessorPlugin one = mockPostProcessor("one", "txt", ImplicitApplyOptions.ALWAYS);

List<GeneratedFileContext> result = ImmutableList.copyOf(underTest.generate());
List<GeneratedFileContext> result = List.copyOf(underTest.generate());

assertEquals(1, result.size());
assertItem(result.get(0), "test.txt", ImmutableMap.of(), ImmutableSet.of("one"));
assertItem(result.get(0), "test.txt", Map.of(), Set.of("one"));

verify(one, times(1)).apply(any(FileContext.class), any(PostProcessorContext.class));
}
Expand All @@ -312,20 +309,20 @@ void testPostProcessorWithRewriteAndImplicit() throws Exception {
public List<FileContext> answer(InvocationOnMock invocation) throws Throwable {
// delete input file and create new file test.abc instead
FileContext input = invocation.getArgument(0);
assertItem(input, "test.txt", ImmutableMap.of());
return ImmutableList.of(newFile("test.abc"));
assertItem(input, "test.txt", Map.of());
return List.of(newFile("test.abc"));
}
});
PostProcessorPlugin two = mockPostProcessor("two", "txt", ImplicitApplyOptions.ALWAYS);
PostProcessorPlugin three = mockPostProcessor("three", "abc", ImplicitApplyOptions.ALWAYS);

roleFile.setPostProcessors(ImmutableList.of("one"));
roleFile.setPostProcessors(List.of("one"));

List<GeneratedFileContext> result = ImmutableList.copyOf(underTest.generate());
List<GeneratedFileContext> result = List.copyOf(underTest.generate());

assertEquals(2, result.size());
assertItem(result.get(0), "test.txt", ImmutableMap.of(), ImmutableSet.of("one", "two"));
assertItem(result.get(1), "test.abc", ImmutableMap.of(), ImmutableSet.of("one", "three"));
assertItem(result.get(0), "test.txt", Map.of(), Set.of("one", "two"));
assertItem(result.get(1), "test.abc", Map.of(), Set.of("one", "three"));

verify(one, times(1)).apply(any(FileContext.class), any(PostProcessorContext.class));
verify(two, times(1)).apply(any(FileContext.class), any(PostProcessorContext.class));
Expand Down
Loading

0 comments on commit d28d37d

Please sign in to comment.