Skip to content

Commit

Permalink
refactor: Migrate to JUnit 5 (#483)
Browse files Browse the repository at this point in the history
* Use JUnit 5 dependency

* Fix warnings raised by IntelliJ

* Use JUnit5 assertions
  • Loading branch information
SMadani authored Sep 28, 2023
1 parent 76e3150 commit 9e13fcd
Show file tree
Hide file tree
Showing 186 changed files with 517 additions and 625 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Deprecated public internal request classes in Account API
- Internal refactoring of Verify v1 and Account API implementations
- Added `/v1` to Meetings API endpoint URL paths
- Migrated assertions and test dependencies to JUnit 5

# [7.8.0] - 2023-09-07
- Added capability to configure request timeouts (default is 60 seconds)
Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ dependencies {
implementation 'io.openapitools.jackson.dataformat:jackson-dataformat-hal:1.0.9'
implementation 'com.vonage:jwt:1.0.2'

testImplementation 'junit:junit:4.13.2'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.0'
testImplementation 'org.junit.vintage:junit-vintage-engine:5.10.0'
testImplementation 'org.mockito:mockito-inline:4.11.0'
testImplementation 'jakarta.servlet:jakarta.servlet-api:4.0.4'
}
Expand Down
13 changes: 4 additions & 9 deletions src/test/java/com/vonage/client/AbstractMethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.message.BasicStatusLine;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.*;
import org.mockito.ArgumentCaptor;
import static org.mockito.Mockito.*;
import java.io.*;
Expand All @@ -48,10 +47,6 @@

public class AbstractMethodTest {

static {
TestUtils.mockStaticLoggingUtils();
}

private static class ConcreteMethod extends AbstractMethod<String, String> {
public ConcreteMethod(HttpWrapper httpWrapper) {
super(httpWrapper);
Expand Down Expand Up @@ -95,15 +90,15 @@ static String getEntityContentsAsString(HttpEntity entity) throws IOException {
private HttpClient mockHttpClient;
private AuthCollection mockAuthMethods;
private AuthMethod mockAuthMethod;
private HttpResponse basicResponse = new BasicHttpResponse(
private final HttpResponse basicResponse = new BasicHttpResponse(
new BasicStatusLine(
new ProtocolVersion("1.1", 1, 1),
200,
"OK"
)
);

@Before
@BeforeEach
public void setUp() throws Exception {
mockWrapper = mock(HttpWrapper.class);
mockAuthMethods = mock(AuthCollection.class);
Expand Down
23 changes: 12 additions & 11 deletions src/test/java/com/vonage/client/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import static org.junit.Assert.*;
import org.junit.function.ThrowingRunnable;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.function.Executable;
import static org.mockito.Mockito.*;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
Expand All @@ -35,8 +35,9 @@
import java.util.function.Supplier;

public abstract class ClientTest<T> {
protected String applicationId = UUID.randomUUID().toString();
protected String apiKey = "a1b2c3d4", apiSecret = "1234567890abcdef";
protected final String applicationId = UUID.randomUUID().toString();
protected final String apiKey = "a1b2c3d4";
protected final String apiSecret = "1234567890abcdef";
protected HttpWrapper wrapper;
protected T client;

Expand Down Expand Up @@ -83,24 +84,24 @@ protected void stubResponse(int code) throws Exception {
wrapper.setHttpClient(stubHttpClient(code));
}

protected void stubResponseAndAssertThrows(ThrowingRunnable invocation,
protected void stubResponseAndAssertThrows(Executable invocation,
Class<? extends Exception> exceptionClass) throws Exception {
stubResponseAndAssertThrows(200, invocation, exceptionClass);
}

protected void stubResponseAndAssertThrows(int statusCode, ThrowingRunnable invocation,
protected void stubResponseAndAssertThrows(int statusCode, Executable invocation,
Class<? extends Exception> exceptionClass) throws Exception {
stubResponse(statusCode);
assertThrows(exceptionClass, invocation);
}

protected void stubResponseAndAssertThrows(String response, ThrowingRunnable invocation,
protected void stubResponseAndAssertThrows(String response, Executable invocation,
Class<? extends Exception> exceptionClass) throws Exception {
stubResponse(200, response);
assertThrows(exceptionClass, invocation);
}

protected void stubResponseAndAssertThrows(int statusCode, String response, ThrowingRunnable invocation,
protected void stubResponseAndAssertThrows(int statusCode, String response, Executable invocation,
Class<? extends Exception> exceptionClass) throws Exception {
stubResponse(statusCode, response);
assertThrows(exceptionClass, invocation);
Expand All @@ -127,7 +128,7 @@ protected <R> R stubResponseAndGet(int statusCode, String response, Supplier<? e

@SuppressWarnings("unchecked")
protected <E extends VonageApiResponseException> E assertApiResponseException(
int statusCode, String response, Class<E> exClass, ThrowingRunnable invocation) throws Exception {
int statusCode, String response, Class<E> exClass, Executable invocation) throws Exception {
E expectedResponse = (E) exClass.getDeclaredMethod("fromJson", String.class).invoke(exClass, response);
String expectedJson = expectedResponse.toJson();
assertTrue(expectedJson.length() > 1 && expectedJson.length() <= response.length());
Expand All @@ -138,11 +139,11 @@ protected <E extends VonageApiResponseException> E assertApiResponseException(
String failPrefix = "Expected "+exClass.getSimpleName()+", but got ";

try {
invocation.run();
invocation.execute();
fail(failPrefix + "nothing.");
}
catch (Throwable ex) {
assertEquals(failPrefix + ex.getClass(), exClass, ex.getClass());
assertEquals(exClass, ex.getClass(), failPrefix + ex.getClass());
assertEquals(expectedResponse, ex);
assertEquals(expectedJson, ((E) ex).toJson());
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/com/vonage/client/DynamicEndpointTestSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.util.EntityUtils;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.*;
Expand Down Expand Up @@ -191,13 +191,13 @@ private RequestBuilder makeTestRequest(T request) throws Exception {
String expectedContentTypeHeader = expectedContentTypeHeader(request);
if (expectedContentTypeHeader != null) {
Header contentTypeHeader = builder.getFirstHeader("Content-Type");
assertNotNull("Content-Type header is null", contentTypeHeader);
assertNotNull(contentTypeHeader, "Content-Type header is null");
assertEquals(expectedContentTypeHeader, contentTypeHeader.getValue());
}
String expectedAcceptHeader = expectedAcceptHeader();
if (expectedAcceptHeader != null) {
Header acceptHeader = builder.getFirstHeader("Accept");
assertNotNull("Accept header is null", acceptHeader);
assertNotNull(acceptHeader, "Accept header is null");
assertEquals(expectedAcceptHeader, acceptHeader.getValue());
}
return builder;
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/vonage/client/HttpConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package com.vonage.client;

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;

public class HttpConfigTest {
static final String EXPECTED_DEFAULT_API_BASE_URI = "https://api.nexmo.com";
Expand Down
8 changes: 3 additions & 5 deletions src/test/java/com/vonage/client/HttpWrapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
package com.vonage.client;

import com.vonage.client.auth.AuthCollection;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;

public class HttpWrapperTest {
private HttpWrapper wrapper;

@Before
@BeforeEach
public void setUp() {
wrapper = new HttpWrapper(new AuthCollection());
}
Expand Down
11 changes: 4 additions & 7 deletions src/test/java/com/vonage/client/LoggingUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,17 @@
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.message.BasicStatusLine;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.*;

public class LoggingUtilTest {

@BeforeClass
@BeforeAll
public static void setupBeforeClass() {
TestUtils.unmockStaticLoggingUtils();
}

@AfterClass
@AfterAll
public static void cleanupAfterClass() {
TestUtils.mockStaticLoggingUtils();
}
Expand Down
6 changes: 5 additions & 1 deletion src/test/java/com/vonage/client/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.message.BasicStatusLine;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.*;
import org.mockito.MockedStatic;
import static org.mockito.Mockito.mockStatic;
import java.io.ByteArrayInputStream;
Expand All @@ -38,6 +38,10 @@

public class TestUtils {

static {
mockStaticLoggingUtils();
}

private static MockedStatic<LoggingUtils> staticMockLoggingUtils;

public static void mockStaticLoggingUtils() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.Test;
import java.net.URI;
import java.util.Collections;
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/com/vonage/client/VonageClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.message.BasicNameValuePair;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.Test;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -165,7 +165,7 @@ public void testApiKeyWithSecret() throws VonageUnacceptableAuthException {
}

@Test
public void testApiKeyWithSignatureSecret() throws VonageUnacceptableAuthException, NoSuchAlgorithmException, InvalidKeyException {
public void testApiKeyWithSignatureSecret() throws Exception {
VonageClient vonageClient = VonageClient.builder().apiKey("api-key").signatureSecret("api-secret").build();
AuthCollection authCollection = vonageClient.getHttpWrapper().getAuthCollection();

Expand All @@ -190,7 +190,7 @@ public void testApiKeyWithSignatureSecret() throws VonageUnacceptableAuthExcepti
}

@Test
public void testApiKeyWithSignatureSecretAsHMACSHA256() throws VonageUnacceptableAuthException, NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
public void testApiKeyWithSignatureSecretAsHMACSHA256() throws Exception {
VonageClient vonageClient = VonageClient.builder().hashType(HashUtil.HashType.HMAC_SHA256).apiKey("api-key").signatureSecret("api-secret").build();
AuthCollection authCollection = vonageClient.getHttpWrapper().getAuthCollection();

Expand All @@ -215,7 +215,7 @@ public void testApiKeyWithSignatureSecretAsHMACSHA256() throws VonageUnacceptabl
}

@Test
public void testApiKeyWithSignatureSecretAsHmacSHA256() throws VonageUnacceptableAuthException, NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
public void testApiKeyWithSignatureSecretAsHmacSHA256() throws Exception {
VonageClient vonageClient = VonageClient.builder().hashType(HashUtil.HashType.HMAC_SHA256).apiKey("api-key").signatureSecret("api-secret").build();
AuthCollection authCollection = vonageClient.getHttpWrapper().getAuthCollection();

Expand Down Expand Up @@ -348,6 +348,6 @@ public void testSubClientGetters() {

private void assertContainsParam(List<NameValuePair> params, String key, String value) {
NameValuePair item = new BasicNameValuePair(key, value);
assertTrue(params + " should contain " + item, params.contains(item));
assertTrue(params.contains(item), params + " should contain " + item);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.vonage.client.RestEndpoint;
import com.vonage.client.VonageResponseParseException;
import com.vonage.client.common.HttpMethod;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.Test;
import java.math.BigDecimal;
import java.util.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
import com.vonage.client.application.capabilities.*;
import com.vonage.client.common.HttpMethod;
import com.vonage.client.common.Webhook;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.function.ThrowingRunnable;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.function.Executable;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class ApplicationClientTest extends ClientTest<ApplicationClient> {
static final UUID SAMPLE_APPLICATION_ID = UUID.randomUUID();
Expand Down Expand Up @@ -133,7 +136,7 @@ static void assertEqualsSampleApplication(Application response) {
assertTrue(privacy.getImproveAi());
}

void assert400ResponseException(ThrowingRunnable invocation) throws Exception {
void assert400ResponseException(Executable invocation) throws Exception {
String response = "{\n" +
" \"type\": \"https://developer.nexmo.com/api-errors/application#payload-validation\",\n" +
" \"title\": \"Bad Request\",\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import com.vonage.client.application.capabilities.Voice;
import com.vonage.client.common.HttpMethod;
import com.vonage.client.common.Webhook;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
import java.util.UUID;

public class ApplicationTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@

import com.vonage.client.common.HttpMethod;
import com.vonage.client.common.Webhook;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;

public class MessagesTest {
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@

import com.vonage.client.common.HttpMethod;
import com.vonage.client.common.Webhook;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;

public class RtcTest {
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package com.vonage.client.application.capabilities;

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.*;

public class VbcTest {
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import com.vonage.client.Jsonable;
import com.vonage.client.common.HttpMethod;
import com.vonage.client.common.Webhook;
import static org.junit.Assert.*;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.*;

public class VoiceTest {

Expand Down
Loading

0 comments on commit 9e13fcd

Please sign in to comment.