Skip to content

Commit

Permalink
refactor (jkube-kit-resource-service): Migrate test from JUnit4 to JU…
Browse files Browse the repository at this point in the history
…nit5 (eclipse-jkube#1567)

Signed-off-by: Anurag Rajawat <anuragsinghrajawat22@gmail.com>
  • Loading branch information
anurag-rajawat committed Jul 28, 2022
1 parent 65d37bf commit 91102b9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 58 deletions.
5 changes: 3 additions & 2 deletions jkube-kit/resource/service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jmockit</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,19 @@
import mockit.Mocked;
import mockit.Verifications;
import org.eclipse.jkube.kit.config.resource.ResourceServiceConfig;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import static org.assertj.core.api.Assertions.assertThat;

public class DefaultResourceServiceTest {
@SuppressWarnings("unused")
class DefaultResourceServiceTest {

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@SuppressWarnings("unused")
@Mocked
private EnricherManager enricherManager;
@SuppressWarnings("unused")
@Mocked
private KitLogger kitLogger;
@SuppressWarnings("unused")
@Mocked
private ResourceConfig resourceConfig;
@Mocked
Expand All @@ -60,22 +55,22 @@ public class DefaultResourceServiceTest {
private ResourceServiceConfig resourceServiceConfig;
private DefaultResourceService defaultResourceService;

@Before
public void init() throws IOException {
targetDir = temporaryFolder.newFolder("target");
@BeforeEach
void init(@TempDir File temporaryFolder) {
targetDir = new File(temporaryFolder, "target");
resourceServiceConfig = ResourceServiceConfig.builder()
.interpolateTemplateParameters(true)
.targetDir(targetDir)
.project(project)
.resourceFileType(ResourceFileType.yaml)
.resourceDirs(Collections.singletonList(temporaryFolder.newFolder("resources")))
.resourceDirs(Collections.singletonList(new File(temporaryFolder, "resources")))
.resourceConfig(resourceConfig)
.build();
defaultResourceService = new DefaultResourceService(resourceServiceConfig);
}

@Test
public void generateResourcesWithNoResourcesShouldReturnEmpty() throws IOException {
void generateResourcesWithNoResourcesShouldReturnEmpty() throws IOException {
// When
final KubernetesList result = defaultResourceService
.generateResources(PlatformMode.kubernetes, enricherManager, kitLogger);
Expand All @@ -84,7 +79,7 @@ public void generateResourcesWithNoResourcesShouldReturnEmpty() throws IOExcepti
}

@Test
public void generateResources_withResources_shouldReturnKubernetesResourceList() throws IOException {
void generateResources_withResources_shouldReturnKubernetesResourceList() throws IOException {
// Given
File resourceDir1 = new File(Objects.requireNonNull(getClass().getResource("/jkube/common")).getFile());
File resourceDir2 = new File(Objects.requireNonNull(getClass().getResource("/jkube/dev")).getFile());
Expand Down Expand Up @@ -113,7 +108,7 @@ public void generateResources_withResources_shouldReturnKubernetesResourceList()

@SuppressWarnings("AccessStaticViaInstance")
@Test
public void writeResources(@Mocked WriteUtil writeUtil, @Mocked TemplateUtil templateUtil) throws IOException {
void writeResources(@Mocked WriteUtil writeUtil, @Mocked TemplateUtil templateUtil) throws IOException {
// When
defaultResourceService.writeResources(null, ResourceClassifier.KUBERNETES, kitLogger);
// Then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,44 +24,43 @@
import mockit.Mocked;
import mockit.Verifications;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.jkube.kit.resource.service.TemplateUtil.getSingletonTemplate;
import static org.eclipse.jkube.kit.resource.service.TemplateUtil.interpolateTemplateVariables;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;

@SuppressWarnings({"AccessStaticViaInstance", "ConstantConditions"})
public class TemplateUtilTest {
@SuppressWarnings({"AccessStaticViaInstance", "ConstantConditions", "unused"})
class TemplateUtilTest {

@SuppressWarnings("unused")
@Mocked
private FileUtils fileUtils;

private KubernetesListBuilder klb;

@Before
public void initGlobalVariables() {
@BeforeEach
void initGlobalVariables() {
klb = new KubernetesListBuilder();
}

@Test
public void getSingletonTemplateWithNullShouldReturnNull() {
void getSingletonTemplateWithNullShouldReturnNull() {
assertThat(getSingletonTemplate(null)).isNull();
}

@Test
public void getSingletonTemplateWithMultipleItemsShouldReturnNull() {
void getSingletonTemplateWithMultipleItemsShouldReturnNull() {
// Given
klb.addToItems(new Template(), new Template());
// When - Then
assertThat(getSingletonTemplate(klb.build())).isNull();
}

@Test
public void getSingletonTemplateWithSingleItemsShouldReturnTemplate() {
void getSingletonTemplateWithSingleItemsShouldReturnTemplate() {
// Given
klb.addToItems(new TemplateBuilder().withNewMetadata().withName("template").endMetadata().build());
// When - Then
Expand All @@ -70,15 +69,15 @@ public void getSingletonTemplateWithSingleItemsShouldReturnTemplate() {
}

@Test
public void interpolateTemplateVariablesWithNoParametersShouldDoNothing() throws IOException {
void interpolateTemplateVariablesWithNoParametersShouldDoNothing() throws IOException {
// When
interpolateTemplateVariables(klb.build(), null);
// Then
verifyWriteStringToFile(0, null);
}

@Test
public void interpolateTemplateVariablesWithParametersAndNoPlaceholdersShouldDoNothing() throws IOException {
void interpolateTemplateVariablesWithParametersAndNoPlaceholdersShouldDoNothing() throws IOException {
// Given
klb.addToItems(new TemplateBuilder()
.addToParameters(new ParameterBuilder().withName("param1").withValue("value1").build())
Expand All @@ -91,7 +90,7 @@ public void interpolateTemplateVariablesWithParametersAndNoPlaceholdersShouldDoN
}

@Test
public void interpolateTemplateVariablesWithParametersAndPlaceholdersShouldReplace() throws IOException {
void interpolateTemplateVariablesWithParametersAndPlaceholdersShouldReplace() throws IOException {
// Given
klb.addToItems(new TemplateBuilder()
.addToParameters(new ParameterBuilder().withName("param1").withValue("value1").build())
Expand All @@ -104,7 +103,7 @@ public void interpolateTemplateVariablesWithParametersAndPlaceholdersShouldRepla
}

@Test
public void interpolateTemplateVariablesWithReadFileException() throws IOException {
void interpolateTemplateVariablesWithReadFileException() throws IOException {
// Given
klb.addToItems(new TemplateBuilder()
.addToParameters(new ParameterBuilder().withName("param1").withValue("value1").build())
Expand All @@ -122,7 +121,7 @@ public void interpolateTemplateVariablesWithReadFileException() throws IOExcepti
}

@Test
public void interpolateTemplateVariablesWithWriteFileException() throws IOException {
void interpolateTemplateVariablesWithWriteFileException() throws IOException {
// Given
klb.addToItems(new TemplateBuilder()
.addToParameters(new ParameterBuilder().withName("param1").withValue("value1").build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,36 @@
import mockit.Expectations;
import mockit.Mocked;
import mockit.Verifications;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThrows;
import static org.junit.jupiter.api.Assertions.assertThrows;

@SuppressWarnings({"ConstantConditions", "AccessStaticViaInstance"})
public class WriteUtilTest {
@SuppressWarnings({"ConstantConditions", "AccessStaticViaInstance", "unused"})
class WriteUtilTest {

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

@SuppressWarnings("unused")
@TempDir
File temporaryFolder;
@Mocked
private KitLogger log;
@SuppressWarnings("unused")
@Mocked
private ResourceUtil resourceUtil;

private KubernetesListBuilder klb;
private File resourceFileBase;

@Before
public void initGlobalVariables() throws IOException {
@BeforeEach
void initGlobalVariables() {
klb = new KubernetesListBuilder();
resourceFileBase = temporaryFolder.newFolder();
resourceFileBase = temporaryFolder;
}

@Test
public void writeResource() throws IOException {
void writeResource() throws IOException {
// Given
final File baton = temporaryFolder.newFile();
final File baton = File.createTempFile("junit", "ext", temporaryFolder);
mockResourceUtilSave(baton);
// When
final File result = WriteUtil.writeResource(null, null, null);
Expand All @@ -69,9 +65,9 @@ public void writeResource() throws IOException {
}

@Test
public void writeResourceThrowsException() throws IOException {
void writeResourceThrowsException() throws IOException {
// Given
final File resource = temporaryFolder.newFolder("resource-base");
final File resource = new File(temporaryFolder, "resource-base");
mockResourceUtilSave(new IOException("Message"));
// When
final IOException result = assertThrows(IOException.class,
Expand All @@ -85,15 +81,15 @@ public void writeResourceThrowsException() throws IOException {
}

@Test
public void writeResourcesIndividualAndCompositeWithNoResourcesShouldOnlyWriteComposite() throws IOException {
void writeResourcesIndividualAndCompositeWithNoResourcesShouldOnlyWriteComposite() throws IOException {
// When
WriteUtil.writeResourcesIndividualAndComposite(klb.build(), resourceFileBase, null, log);
// Then
verifyResourceUtilSave(resourceFileBase, 1);
}

@Test
public void writeResourcesIndividualAndCompositeWithResourcesShouldWriteAll() throws IOException {
void writeResourcesIndividualAndCompositeWithResourcesShouldWriteAll() throws IOException {
// Given
klb.addToItems(
new ConfigMapBuilder().withNewMetadata().withName("cm-1").endMetadata().build(),
Expand All @@ -110,7 +106,7 @@ public void writeResourcesIndividualAndCompositeWithResourcesShouldWriteAll() th
}

@Test
public void writeResourcesIndividualAndComposite_withResourcesWithSameName_shouldWriteAll() throws IOException {
void writeResourcesIndividualAndComposite_withResourcesWithSameName_shouldWriteAll() throws IOException {
// Given
klb.addToItems(
new ConfigMapBuilder().withNewMetadata().withNamespace("default").withName("cm-1").endMetadata().build(),
Expand Down

0 comments on commit 91102b9

Please sign in to comment.