Skip to content

Commit

Permalink
refactor: Revert JUnit5 to JUnit4 (#969)
Browse files Browse the repository at this point in the history
* refactor: Revert JUnit5 to JUnit4

Refactored tests added since 928dd04 to
use JUnit4 instead of JUnit5.

Revert "chore: migrate tests in oauth2_http module from JUnit4 to JUnit5 -  sixth iteration (#756) (#769)"

This reverts commit 928dd04.

Revert "chore: migrate tests in oauth2_http module from JUnit4 to JUnit5 -  fifth iteration (#756) (#767)"

This reverts commit 59dfb35.

Revert "chore: migrate tests in oauth2_http module from JUnit4 to JUnit5 -  fourth iteration (#756) (#766)"

This reverts commit d7edc45.

Revert "chore: migrate tests in oauth2_http module from JUnit4 to JUnit5 -  third iteration (#756) (#764)"

This reverts commit e9dfaf9.

Revert "chore: migrate tests in oauth2_http module from JUnit4 to JUnit5 - second iteration (#756) (#763)"

This reverts commit d045247.

Revert "chore: migrate tests in oauth2_http module from JUnit4 to JUnit5 - first iteration (#756) (#762)"

This reverts commit 4e6576f.

Revert "refactor: migration tests in credentials module from JUnit4 to JUnit5 (#756) (#761)"

This reverts commit a64d35c.

Revert "refactor: migration tests in appengine module from JUnit4 to JUnit5 (#756) (#760)"

This reverts commit cccaa44.
  • Loading branch information
clundin25 committed Aug 11, 2022
1 parent 1f4c9c7 commit 53355a7
Show file tree
Hide file tree
Showing 47 changed files with 2,261 additions and 2,072 deletions.
Expand Up @@ -31,13 +31,13 @@

package com.google.auth.appengine;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.google.auth.Credentials;
import com.google.auth.oauth2.AccessToken;
Expand All @@ -51,18 +51,21 @@
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Unit tests for AppEngineCredentials */
class AppEngineCredentialsTest extends BaseSerializationTest {
@RunWith(JUnit4.class)
public class AppEngineCredentialsTest extends BaseSerializationTest {

private static Collection<String> SCOPES =
Collections.unmodifiableCollection(Arrays.asList("scope1", "scope2"));
private static final URI CALL_URI = URI.create("http://googleapis.com/testapi/v1/foo");
private static final String EXPECTED_ACCOUNT = "serviceAccount";

@Test
void constructor_usesAppIdentityService() throws IOException {
public void constructor_usesAppIdentityService() throws IOException {
String expectedAccessToken = "ExpectedAccessToken";

MockAppIdentityService appIdentity = new MockAppIdentityService();
Expand All @@ -80,7 +83,7 @@ void constructor_usesAppIdentityService() throws IOException {
}

@Test
void refreshAccessToken_sameAs() throws IOException {
public void refreshAccessToken_sameAs() throws IOException {
String expectedAccessToken = "ExpectedAccessToken";

MockAppIdentityService appIdentity = new MockAppIdentityService();
Expand All @@ -97,7 +100,7 @@ void refreshAccessToken_sameAs() throws IOException {
}

@Test
void getAccount_sameAs() {
public void getAccount_sameAs() throws IOException {
MockAppIdentityService appIdentity = new MockAppIdentityService();
appIdentity.setServiceAccountName(EXPECTED_ACCOUNT);
AppEngineCredentials credentials =
Expand All @@ -109,7 +112,7 @@ void getAccount_sameAs() {
}

@Test
void sign_sameAs() {
public void sign_sameAs() throws IOException {
byte[] expectedSignature = {0xD, 0xE, 0xA, 0xD};
MockAppIdentityService appIdentity = new MockAppIdentityService();
appIdentity.setSignature(expectedSignature);
Expand All @@ -122,7 +125,7 @@ void sign_sameAs() {
}

@Test
void createScoped_clonesWithScopes() throws IOException {
public void createScoped_clonesWithScopes() throws IOException {
String expectedAccessToken = "ExpectedAccessToken";
Collection<String> emptyScopes = Collections.emptyList();

Expand All @@ -135,10 +138,11 @@ void createScoped_clonesWithScopes() throws IOException {
.setAppIdentityService(appIdentity)
.build();
assertTrue(credentials.createScopedRequired());
assertThrows(
Exception.class,
() -> credentials.getRequestMetadata(CALL_URI),
"Should not be able to use credential without scopes.");
try {
credentials.getRequestMetadata(CALL_URI);
fail("Should not be able to use credential without scopes.");
} catch (Exception expected) {
}
assertEquals(0, appIdentity.getGetAccessTokenCallCount());

GoogleCredentials scopedCredentials = credentials.createScoped(SCOPES);
Expand All @@ -151,7 +155,7 @@ void createScoped_clonesWithScopes() throws IOException {
}

@Test
void equals_true() {
public void equals_true() throws IOException {
Collection<String> emptyScopes = Collections.emptyList();
MockAppIdentityService appIdentity = new MockAppIdentityService();

Expand All @@ -171,7 +175,7 @@ void equals_true() {
}

@Test
void equals_false_scopes() {
public void equals_false_scopes() throws IOException {
Collection<String> emptyScopes = Collections.emptyList();
Collection<String> scopes = Collections.singleton("SomeScope");
MockAppIdentityService appIdentity = new MockAppIdentityService();
Expand All @@ -191,7 +195,7 @@ void equals_false_scopes() {
}

@Test
void toString_containsFields() {
public void toString_containsFields() throws IOException {
String expectedToString =
String.format(
"AppEngineCredentials{scopes=[%s], scopesRequired=%b, appIdentityServiceClassName=%s}",
Expand All @@ -209,7 +213,7 @@ void toString_containsFields() {
}

@Test
void hashCode_equals() {
public void hashCode_equals() throws IOException {
Collection<String> emptyScopes = Collections.emptyList();
MockAppIdentityService appIdentity = new MockAppIdentityService();
AppEngineCredentials credentials =
Expand All @@ -226,7 +230,7 @@ void hashCode_equals() {
}

@Test
void serialize() throws IOException, ClassNotFoundException {
public void serialize() throws IOException, ClassNotFoundException {
Collection<String> scopes = Collections.singleton("SomeScope");
MockAppIdentityService appIdentity = new MockAppIdentityService();
AppEngineCredentials credentials =
Expand All @@ -245,7 +249,7 @@ private static void assertContainsBearerToken(Map<String, List<String>> metadata
assertNotNull(token);
String expectedValue = "Bearer " + token;
List<String> authorizations = metadata.get("Authorization");
assertNotNull(authorizations, "Authorization headers not found");
assertTrue(authorizations.contains(expectedValue), "Bearer token not found");
assertNotNull("Authorization headers not found", authorizations);
assertTrue("Bearer token not found", authorizations.contains(expectedValue));
}
}
4 changes: 2 additions & 2 deletions appengine/pom.xml
Expand Up @@ -68,8 +68,8 @@
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
23 changes: 12 additions & 11 deletions credentials/javatests/com/google/auth/SigningExceptionTest.java
Expand Up @@ -31,44 +31,45 @@

package com.google.auth;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import com.google.auth.ServiceAccountSigner.SigningException;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import org.junit.Test;

class SigningExceptionTest {
public class SigningExceptionTest {

private static final String EXPECTED_MESSAGE = "message";
private static final RuntimeException EXPECTED_CAUSE = new RuntimeException();

@Test
void constructor() {
public void constructor() {
SigningException signingException = new SigningException(EXPECTED_MESSAGE, EXPECTED_CAUSE);
assertEquals(EXPECTED_MESSAGE, signingException.getMessage());
assertSame(EXPECTED_CAUSE, signingException.getCause());
}

@Test
void equals_true() {
public void equals_true() throws IOException {
SigningException signingException = new SigningException(EXPECTED_MESSAGE, EXPECTED_CAUSE);
SigningException otherSigningException = new SigningException(EXPECTED_MESSAGE, EXPECTED_CAUSE);
assertTrue(signingException.equals(otherSigningException));
assertTrue(otherSigningException.equals(signingException));
}

@Test
void equals_false_message() {
public void equals_false_message() throws IOException {
SigningException signingException = new SigningException(EXPECTED_MESSAGE, EXPECTED_CAUSE);
SigningException otherSigningException = new SigningException("otherMessage", EXPECTED_CAUSE);
assertFalse(signingException.equals(otherSigningException));
assertFalse(otherSigningException.equals(signingException));
}

@Test
void equals_false_cause() {
public void equals_false_cause() throws IOException {
SigningException signingException = new SigningException(EXPECTED_MESSAGE, EXPECTED_CAUSE);
SigningException otherSigningException =
new SigningException("otherMessage", new RuntimeException());
Expand All @@ -77,7 +78,7 @@ void equals_false_cause() {
}

@Test
void hashCode_equals() {
public void hashCode_equals() throws IOException {
SigningException signingException = new SigningException(EXPECTED_MESSAGE, EXPECTED_CAUSE);
SigningException otherSigningException = new SigningException(EXPECTED_MESSAGE, EXPECTED_CAUSE);
assertEquals(signingException.hashCode(), otherSigningException.hashCode());
Expand Down
4 changes: 2 additions & 2 deletions credentials/pom.xml
Expand Up @@ -50,8 +50,8 @@

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
22 changes: 13 additions & 9 deletions oauth2_http/javatests/com/google/auth/TestUtils.java
Expand Up @@ -31,9 +31,9 @@

package com.google.auth;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import com.google.api.client.http.HttpHeaders;
import com.google.api.client.http.HttpResponseException;
Expand All @@ -46,8 +46,8 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
Expand All @@ -64,31 +64,35 @@ public class TestUtils {
public static void assertContainsBearerToken(Map<String, List<String>> metadata, String token) {
assertNotNull(metadata);
assertNotNull(token);
assertTrue(hasBearerToken(metadata, token), "Bearer token not found");
assertTrue("Bearer token not found", hasBearerToken(metadata, token));
}

public static void assertNotContainsBearerToken(
Map<String, List<String>> metadata, String token) {
assertNotNull(metadata);
assertNotNull(token);
assertFalse(hasBearerToken(metadata, token), "Bearer token found");
assertFalse("Bearer token found", hasBearerToken(metadata, token));
}

private static boolean hasBearerToken(Map<String, List<String>> metadata, String token) {
String expectedValue = AuthHttpConstants.BEARER + " " + token;
List<String> authorizations = metadata.get(AuthHttpConstants.AUTHORIZATION);
assertNotNull(authorizations, "Authorization headers not found");
assertNotNull("Authorization headers not found", authorizations);
return authorizations.contains(expectedValue);
}

public static InputStream jsonToInputStream(GenericJson json) throws IOException {
json.setFactory(JSON_FACTORY);
String text = json.toPrettyString();
return new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8));
return new ByteArrayInputStream(text.getBytes("UTF-8"));
}

public static InputStream stringToInputStream(String text) {
return new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8));
try {
return new ByteArrayInputStream(text.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Unexpected encoding exception", e);
}
}

public static Map<String, String> parseQuery(String query) throws IOException {
Expand Down
Expand Up @@ -32,8 +32,8 @@
package com.google.auth.http;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpHeaders;
Expand All @@ -47,17 +47,20 @@
import com.google.auth.oauth2.OAuth2Credentials;
import com.google.auth.oauth2.UserCredentials;
import java.io.IOException;
import org.junit.jupiter.api.Test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Test case for {@link HttpCredentialsAdapter}. */
class HttpCredentialsAdapterTest {
@RunWith(JUnit4.class)
public class HttpCredentialsAdapterTest {

private static final String CLIENT_SECRET = "jakuaL9YyieakhECKL2SwZcu";
private static final String CLIENT_ID = "ya29.1.AADtN_UtlxN3PuGAxrN2XQnZTVRvDyVWnYq4I6dws";
private static final String REFRESH_TOKEN = "1/Tl6awhpFjkMkSJoj1xsli0H2eL5YsMgU_NKPY2TyGWY";

@Test
void initialize_populatesOAuth2Credentials() throws IOException {
public void initialize_populatesOAuth2Credentials() throws IOException {
final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2";
final String expectedAuthorization = InternalAuthHttpConstants.BEARER_PREFIX + accessToken;
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
Expand All @@ -84,7 +87,7 @@ void initialize_populatesOAuth2Credentials() throws IOException {
}

@Test
void initialize_populatesOAuth2Credentials_handle401() throws IOException {
public void initialize_populatesOAuth2Credentials_handle401() throws IOException {
final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2";
final String accessToken2 = "2/MkSJoj1xsli0AccessToken_NKPY2";

Expand Down Expand Up @@ -122,7 +125,7 @@ void initialize_populatesOAuth2Credentials_handle401() throws IOException {
}

@Test
void initialize_noURI() throws IOException {
public void initialize_noURI() throws IOException {
final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2";
final String expectedAuthorization = InternalAuthHttpConstants.BEARER_PREFIX + accessToken;
MockTokenServerTransportFactory tokenServerTransportFactory =
Expand Down Expand Up @@ -151,7 +154,7 @@ void initialize_noURI() throws IOException {
}

@Test
void getCredentials() {
public void getCredentials() {
final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2";
MockTokenServerTransportFactory tokenServerTransportFactory =
new MockTokenServerTransportFactory();
Expand Down

0 comments on commit 53355a7

Please sign in to comment.