Skip to content

Commit

Permalink
Migrate tests to JUnit 5 and latest test deps
Browse files Browse the repository at this point in the history
  • Loading branch information
chadlwilson committed Feb 19, 2022
1 parent 55d6b0b commit 624868a
Show file tree
Hide file tree
Showing 23 changed files with 146 additions and 176 deletions.
19 changes: 13 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,20 @@ dependencies {
implementation group: 'com.amazonaws', name: 'aws-java-sdk-ecr', version: '1.11.931'
implementation group: 'com.amazonaws', name: 'aws-java-sdk-sts', version: '1.11.931'

testImplementation group: 'junit', name: 'junit', version: '4.13.2'
testImplementation group: 'com.github.stefanbirkner', name: 'system-rules', version: '1.19.0'
testImplementation group: 'org.assertj', name: 'assertj-core', version: '3.18.1'
testImplementation group: 'org.mockito', name: 'mockito-core', version: '3.7.7'
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.8.2'
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.8.2'
testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.8.2'

testImplementation group: 'cd.go.plugin', name: 'go-plugin-api', version: '21.4.0'
testImplementation group: 'uk.org.webcompere', name: 'system-stubs-jupiter', version: '1.2.0'
testImplementation group: 'org.assertj', name: 'assertj-core', version: '3.22.0'
testImplementation group: 'org.mockito', name: 'mockito-core', version: '4.3.1'
testImplementation group: 'org.skyscreamer', name: 'jsonassert', version: '1.5.0'
testImplementation group: 'org.jsoup', name: 'jsoup', version: '1.13.1'
testImplementation group: 'cd.go.plugin', name: 'go-plugin-api', version: '18.6.0'
testImplementation group: 'org.jsoup', name: 'jsoup', version: '1.14.3'
}

test {
useJUnitPlatform()
}

jar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import org.junit.Test;
import org.junit.jupiter.api.Test;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import com.thoughtworks.go.plugin.api.request.GoApiRequest;
import com.thoughtworks.go.plugin.api.response.DefaultGoApiResponse;
import org.json.JSONException;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.skyscreamer.jsonassert.JSONAssert;

Expand All @@ -34,7 +34,7 @@ public class ConsoleLoggerTest {
private static ConsoleLogger consoleLogger;
private static ArgumentCaptor<GoApiRequest> argumentCaptor;

@BeforeClass
@BeforeAll
public static void setUp() {
accessor = mock(GoApplicationAccessor.class);
argumentCaptor = ArgumentCaptor.forClass(GoApiRequest.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,30 @@

package cd.go.artifact.docker.registry;

import cd.go.artifact.docker.registry.ConsoleLogger;
import cd.go.artifact.docker.registry.DockerProgressHandler;
import com.spotify.docker.client.messages.ProgressMessage;
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.junit.Assert.fail;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.*;

public class DockerProgressHandlerTest {
private ConsoleLogger consoleLogger;
private DockerProgressHandler progressHandler;

@Before
@BeforeEach
public void setUp() {
consoleLogger = mock(ConsoleLogger.class);
progressHandler = new DockerProgressHandler(consoleLogger);
}

@Test
public void shouldLogErrorToConsoleLogger() {
try {
progressHandler.progress(ProgressMessage.builder().error("some-error").build());
fail("Should throw runtime exception with error message");
} catch (RuntimeException e) {
verify(consoleLogger, times(1)).error("some-error");
assertThat(e.getMessage()).isEqualTo("some-error");
}
assertThatThrownBy(() -> progressHandler.progress(ProgressMessage.builder().error("some-error").build()))
.isInstanceOf(RuntimeException.class)
.hasMessage("some-error");

verify(consoleLogger, times(1)).error("some-error");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import com.amazonaws.services.ecr.model.GetAuthorizationTokenRequest;
import com.amazonaws.services.ecr.model.GetAuthorizationTokenResult;
import com.spotify.docker.client.messages.RegistryAuth;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Base64;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,19 @@
import com.spotify.docker.client.exceptions.DockerException;
import com.thoughtworks.go.plugin.api.request.GoPluginApiRequest;
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;

import java.util.HashMap;

import static cd.go.artifact.docker.registry.executors.FetchArtifactExecutor.FetchArtifactRequest;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.times;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.mockito.Mockito.*;
import static org.mockito.MockitoAnnotations.openMocks;

public class FetchArtifactExecutorTest {
@Rule
public ExpectedException thrown = ExpectedException.none();

@Mock
private GoPluginApiRequest request;
@Mock
Expand All @@ -59,9 +51,9 @@ public class FetchArtifactExecutorTest {
@Mock
private ConsoleLogger consoleLogger;

@Before
@BeforeEach
public void setUp() throws InterruptedException, DockerException, DockerCertificateException {
initMocks(this);
openMocks(this);

when(dockerClientFactory.docker(any())).thenReturn(dockerClient);
}
Expand All @@ -87,7 +79,7 @@ public void shouldFetchArtifact() {
}

@Test
public void shouldNotFetchArtifactWhenSkipImagePullingIsToggled() {
public void shouldNotFetchArtifactWhenSkipImagePullingIsToggled() throws Exception {
final ArtifactStoreConfig storeConfig = new ArtifactStoreConfig("localhost:5000", "other", "admin", "admin123");
final HashMap<String, String> artifactMetadata = new HashMap<>();
artifactMetadata.put("image", "localhost:5000/alpine:v1");
Expand All @@ -102,11 +94,7 @@ public void shouldNotFetchArtifactWhenSkipImagePullingIsToggled() {

final GoPluginApiResponse response = new FetchArtifactExecutor(request, consoleLogger, dockerProgressHandler, dockerClientFactory).execute();

try {
verify(dockerClient, times(0)).pull("localhost:5000/alpine:v1", dockerProgressHandler);
} catch (DockerException | InterruptedException e) {
/* Should never happen */
}
verify(dockerClient, times(0)).pull("localhost:5000/alpine:v1", dockerProgressHandler);

assertThat(response.responseCode()).isEqualTo(200);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
import org.json.JSONException;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
import org.json.JSONException;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.util.Base64;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
import org.json.JSONException;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@
import com.spotify.docker.client.exceptions.DockerException;
import com.thoughtworks.go.plugin.api.request.GoPluginApiRequest;
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.EnvironmentVariables;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -50,13 +50,12 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.mockito.MockitoAnnotations.openMocks;

@ExtendWith(SystemStubsExtension.class)
public class PublishArtifactExecutorTest {
@Rule
public TemporaryFolder tmpFolder = new TemporaryFolder();
@Rule
public ExpectedException thrown = ExpectedException.none();
@TempDir
public Path tmpFolder;

@Mock
private GoPluginApiRequest request;
Expand All @@ -68,16 +67,16 @@ public class PublishArtifactExecutorTest {
private DefaultDockerClient dockerClient;
@Mock
private DockerClientFactory dockerClientFactory;
@Rule
public final EnvironmentVariables environmentVariables = new EnvironmentVariables();

private File agentWorkingDir;
@SystemStub
public EnvironmentVariables environmentVariables;

@Before
private Path agentWorkingDir;

@BeforeEach
public void setUp() throws IOException, InterruptedException, DockerException, DockerCertificateException {
initMocks(this);
environmentVariables.clear();
agentWorkingDir = tmpFolder.newFolder("go-agent");
openMocks(this);
agentWorkingDir = Files.createDirectory(tmpFolder.resolve("go-agent"));

when(dockerClientFactory.docker(any())).thenReturn(dockerClient);
}
Expand All @@ -87,9 +86,9 @@ public void shouldPublishArtifactUsingBuildFile() throws IOException, DockerExce
final ArtifactPlan artifactPlan = new ArtifactPlan("id", "storeId", "build.json");
final ArtifactStoreConfig storeConfig = new ArtifactStoreConfig("localhost:5000", "other", "admin", "admin123");
final ArtifactStore artifactStore = new ArtifactStore(artifactPlan.getId(), storeConfig);
final PublishArtifactRequest publishArtifactRequest = new PublishArtifactRequest(artifactStore, artifactPlan, agentWorkingDir.getAbsolutePath());
final PublishArtifactRequest publishArtifactRequest = new PublishArtifactRequest(artifactStore, artifactPlan, agentWorkingDir.toString());

Path path = Paths.get(agentWorkingDir.getAbsolutePath(), "build.json");
Path path = agentWorkingDir.resolve("build.json");
Files.write(path, "{\"image\":\"localhost:5000/alpine\",\"tag\":\"3.6\"}".getBytes());

when(request.requestBody()).thenReturn(publishArtifactRequest.toJSON());
Expand All @@ -103,11 +102,11 @@ public void shouldPublishArtifactUsingBuildFile() throws IOException, DockerExce
}

@Test
public void shouldPublishArtifactUsingImageAndTag() throws IOException, DockerException, InterruptedException {
public void shouldPublishArtifactUsingImageAndTag() throws DockerException, InterruptedException {
final ArtifactPlan artifactPlan = new ArtifactPlan("id", "storeId", "alpine", Optional.of("3.6"));
final ArtifactStoreConfig storeConfig = new ArtifactStoreConfig("localhost:5000", "other", "admin", "admin123");
final ArtifactStore artifactStore = new ArtifactStore(artifactPlan.getId(), storeConfig);
final PublishArtifactRequest publishArtifactRequest = new PublishArtifactRequest(artifactStore, artifactPlan, agentWorkingDir.getAbsolutePath());
final PublishArtifactRequest publishArtifactRequest = new PublishArtifactRequest(artifactStore, artifactPlan, agentWorkingDir.toString());

when(request.requestBody()).thenReturn(publishArtifactRequest.toJSON());
when(dockerProgressHandler.getDigest()).thenReturn("foo");
Expand All @@ -124,10 +123,10 @@ public void shouldAddErrorToPublishArtifactResponseWhenFailedToPublishImage() th
final ArtifactPlan artifactPlan = new ArtifactPlan("id", "storeId", "build.json");
final ArtifactStoreConfig artifactStoreConfig = new ArtifactStoreConfig("localhost:5000", "other", "admin", "admin123");
final ArtifactStore artifactStore = new ArtifactStore(artifactPlan.getId(), artifactStoreConfig);
final PublishArtifactRequest publishArtifactRequest = new PublishArtifactRequest(artifactStore, artifactPlan, agentWorkingDir.getAbsolutePath());
final PublishArtifactRequest publishArtifactRequest = new PublishArtifactRequest(artifactStore, artifactPlan, agentWorkingDir.toString());
final ArgumentCaptor<DockerProgressHandler> argumentCaptor = ArgumentCaptor.forClass(DockerProgressHandler.class);

Path path = Paths.get(agentWorkingDir.getAbsolutePath(), "build.json");
Path path = Paths.get(agentWorkingDir.toString(), "build.json");
Files.write(path, "{\"image\":\"localhost:5000/alpine\",\"tag\":\"3.6\"}".getBytes());

when(request.requestBody()).thenReturn(publishArtifactRequest.toJSON());
Expand All @@ -144,7 +143,7 @@ public void shouldReadEnvironmentVariablesPassedFromServer() throws IOException,
final ArtifactPlan artifactPlan = new ArtifactPlan("id", "storeId", "${IMAGE_NAME}", Optional.of("3.6"));
final ArtifactStoreConfig storeConfig = new ArtifactStoreConfig("localhost:5000", "other", "admin", "admin123");
final ArtifactStore artifactStore = new ArtifactStore(artifactPlan.getId(), storeConfig);
final PublishArtifactRequest publishArtifactRequest = new PublishArtifactRequest(artifactStore, artifactPlan, agentWorkingDir.getAbsolutePath(), Collections.singletonMap("IMAGE_NAME", "alpine"));
final PublishArtifactRequest publishArtifactRequest = new PublishArtifactRequest(artifactStore, artifactPlan, agentWorkingDir.toString(), Collections.singletonMap("IMAGE_NAME", "alpine"));

when(request.requestBody()).thenReturn(publishArtifactRequest.toJSON());
when(dockerProgressHandler.getDigest()).thenReturn("foo");
Expand All @@ -162,7 +161,7 @@ public void shouldReadEnvironmentVariablesFromTheSystem() throws IOException, Do
final ArtifactPlan artifactPlan = new ArtifactPlan("id", "storeId", "${IMAGE_NAME}", Optional.of("3.6"));
final ArtifactStoreConfig storeConfig = new ArtifactStoreConfig("localhost:5000", "other", "admin", "admin123");
final ArtifactStore artifactStore = new ArtifactStore(artifactPlan.getId(), storeConfig);
final PublishArtifactRequest publishArtifactRequest = new PublishArtifactRequest(artifactStore, artifactPlan, agentWorkingDir.getAbsolutePath());
final PublishArtifactRequest publishArtifactRequest = new PublishArtifactRequest(artifactStore, artifactPlan, agentWorkingDir.toString());

when(request.requestBody()).thenReturn(publishArtifactRequest.toJSON());
when(dockerProgressHandler.getDigest()).thenReturn("foo");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;

import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.mockito.MockitoAnnotations.openMocks;

public class ValidateArtifactStoreConfigExecutorExecutorTest {
@Mock
private GoPluginApiRequest request;

@Before
@BeforeEach
public void setUp() {
initMocks(this);
openMocks(this);
}

@Test
Expand Down
Loading

0 comments on commit 624868a

Please sign in to comment.