Skip to content

Commit

Permalink
chore: migrate tests in oauth2_http module from JUnit4 to JUnit5 - fi…
Browse files Browse the repository at this point in the history
…rst iteration (#756) (#762)

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

* chore: expand imports (#756)
  • Loading branch information
Captain1653 committed Oct 9, 2021
1 parent a64d35c commit 4e6576f
Show file tree
Hide file tree
Showing 11 changed files with 428 additions and 478 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
package com.google.auth.http;

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

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

/** Test case for {@link HttpCredentialsAdapter}. */
@RunWith(JUnit4.class)
public class HttpCredentialsAdapterTest {
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
public void initialize_populatesOAuth2Credentials() throws IOException {
void initialize_populatesOAuth2Credentials() throws IOException {
final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2";
final String expectedAuthorization = InternalAuthHttpConstants.BEARER_PREFIX + accessToken;
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
Expand All @@ -87,7 +84,7 @@ public void initialize_populatesOAuth2Credentials() throws IOException {
}

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

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

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

@Test
public void getCredentials() {
void getCredentials() {
final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2";
MockTokenServerTransportFactory tokenServerTransportFactory =
new MockTokenServerTransportFactory();
Expand Down
27 changes: 12 additions & 15 deletions oauth2_http/javatests/com/google/auth/oauth2/AccessTokenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,57 +31,54 @@

package com.google.auth.oauth2;

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

import java.io.IOException;
import java.util.Date;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.jupiter.api.Test;

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

private static final String TOKEN = "AccessToken";
private static final Date EXPIRATION_DATE = new Date();

@Test
public void constructor() {
void constructor() {
AccessToken accessToken = new AccessToken(TOKEN, EXPIRATION_DATE);
assertEquals(TOKEN, accessToken.getTokenValue());
assertEquals(EXPIRATION_DATE, accessToken.getExpirationTime());
assertEquals(EXPIRATION_DATE.getTime(), (long) accessToken.getExpirationTimeMillis());
}

@Test
public void equals_true() throws IOException {
void equals_true() {
AccessToken accessToken = new AccessToken(TOKEN, EXPIRATION_DATE);
AccessToken otherAccessToken = new AccessToken(TOKEN, EXPIRATION_DATE);
assertTrue(accessToken.equals(otherAccessToken));
assertTrue(otherAccessToken.equals(accessToken));
}

@Test
public void equals_false_token() throws IOException {
void equals_false_token() {
AccessToken accessToken = new AccessToken(TOKEN, EXPIRATION_DATE);
AccessToken otherAccessToken = new AccessToken("otherToken", EXPIRATION_DATE);
assertFalse(accessToken.equals(otherAccessToken));
assertFalse(otherAccessToken.equals(accessToken));
}

@Test
public void equals_false_expirationDate() throws IOException {
void equals_false_expirationDate() {
AccessToken accessToken = new AccessToken(TOKEN, EXPIRATION_DATE);
AccessToken otherAccessToken = new AccessToken(TOKEN, new Date(EXPIRATION_DATE.getTime() + 42));
assertFalse(accessToken.equals(otherAccessToken));
assertFalse(otherAccessToken.equals(accessToken));
}

@Test
public void toString_containsFields() {
void toString_containsFields() {
AccessToken accessToken = new AccessToken(TOKEN, EXPIRATION_DATE);
String expectedToString =
String.format(
Expand All @@ -91,14 +88,14 @@ public void toString_containsFields() {
}

@Test
public void hashCode_equals() throws IOException {
void hashCode_equals() throws IOException {
AccessToken accessToken = new AccessToken(TOKEN, EXPIRATION_DATE);
AccessToken otherAccessToken = new AccessToken(TOKEN, EXPIRATION_DATE);
assertEquals(accessToken.hashCode(), otherAccessToken.hashCode());
}

@Test
public void serialize() throws IOException, ClassNotFoundException {
void serialize() throws IOException, ClassNotFoundException {
AccessToken accessToken = new AccessToken(TOKEN, EXPIRATION_DATE);
AccessToken deserializedAccessToken = serializeAndDeserialize(accessToken);
assertEquals(accessToken, deserializedAccessToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@

package com.google.auth.oauth2;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
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.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.google.common.collect.ImmutableMap;
import java.io.IOException;
Expand All @@ -47,12 +47,9 @@
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.jupiter.api.Test;

@RunWith(JUnit4.class)
public class AppEngineCredentialsTest extends BaseSerializationTest {
class AppEngineCredentialsTest extends BaseSerializationTest {

private static final String EXPECTED_ACCESS_TOKEN = "ExpectedAccessToken";
private static final Date EXPECTED_EXPIRATION_DATE =
Expand All @@ -66,7 +63,7 @@ public class AppEngineCredentialsTest extends BaseSerializationTest {
Collections.unmodifiableCollection(Arrays.asList("scope3"));

@Test
public void constructor_usesAppIdentityService() throws IOException {
void constructor_usesAppIdentityService() throws IOException {
Collection<String> scopes = Collections.singleton("SomeScope");
TestAppEngineCredentials credentials = new TestAppEngineCredentials(scopes);
List<String> forNameArgs = credentials.getForNameArgs();
Expand All @@ -78,51 +75,50 @@ public void constructor_usesAppIdentityService() throws IOException {
}

@Test
public void constructor_noAppEngineRuntime_throwsHelpfulLoadError() throws IOException {
try {
new TestAppEngineCredentialsNoSdk();
fail("Credential expected to fail to load if credential class not present.");
} catch (IOException e) {
String message = e.getMessage();
assertTrue(message.contains("Check that the App Engine SDK is deployed."));
assertTrue(e.getCause() instanceof ClassNotFoundException);
assertTrue(
e.getCause()
.getMessage()
.contains(AppEngineCredentials.APP_IDENTITY_SERVICE_FACTORY_CLASS));
}
void constructor_noAppEngineRuntime_throwsHelpfulLoadError() {
IOException exception =
assertThrows(
IOException.class,
TestAppEngineCredentialsNoSdk::new,
"Credential expected to fail to load if credential class not present.");
String message = exception.getMessage();
assertTrue(message.contains("Check that the App Engine SDK is deployed."));
assertTrue(exception.getCause() instanceof ClassNotFoundException);
assertTrue(
exception
.getCause()
.getMessage()
.contains(AppEngineCredentials.APP_IDENTITY_SERVICE_FACTORY_CLASS));
}

@Test
public void refreshAccessToken_sameAs() throws IOException {
void refreshAccessToken_sameAs() throws IOException {
TestAppEngineCredentials credentials = new TestAppEngineCredentials(SCOPES);
AccessToken accessToken = credentials.refreshAccessToken();
assertEquals(EXPECTED_ACCESS_TOKEN, accessToken.getTokenValue());
assertEquals(EXPECTED_EXPIRATION_DATE, accessToken.getExpirationTime());
}

@Test
public void getAccount_sameAs() throws IOException {
void getAccount_sameAs() throws IOException {
TestAppEngineCredentials credentials = new TestAppEngineCredentials(SCOPES);
assertEquals(EXPECTED_ACCOUNT, credentials.getAccount());
}

@Test
public void sign_sameAs() throws IOException {
void sign_sameAs() throws IOException {
TestAppEngineCredentials credentials = new TestAppEngineCredentials(SCOPES);
assertArrayEquals(EXPECTED_SIGNATURE, credentials.sign("Bytes to sign".getBytes()));
}

@Test
public void createScoped_clonesWithScopes() throws IOException {
void createScoped_clonesWithScopes() throws IOException {
TestAppEngineCredentials credentials = new TestAppEngineCredentials(null);
assertTrue(credentials.createScopedRequired());
try {
credentials.refreshAccessToken();
fail("Should not be able to use credential without scopes.");
} catch (Exception expected) {
// Expected
}
assertThrows(
Exception.class,
credentials::refreshAccessToken,
"Should not be able to use credential without scopes.");

GoogleCredentials scopedCredentials = credentials.createScoped(SCOPES);
assertNotSame(credentials, scopedCredentials);
Expand All @@ -133,7 +129,7 @@ public void createScoped_clonesWithScopes() throws IOException {
}

@Test
public void createScoped_defaultScopes() throws IOException {
void createScoped_defaultScopes() throws IOException {
TestAppEngineCredentials credentials = new TestAppEngineCredentials(null);
assertTrue(credentials.createScopedRequired());

Expand All @@ -152,7 +148,7 @@ public void createScoped_defaultScopes() throws IOException {
}

@Test
public void equals_true() throws IOException {
void equals_true() throws IOException {
GoogleCredentials credentials = new TestAppEngineCredentials(SCOPES);
GoogleCredentials otherCredentials = new TestAppEngineCredentials(SCOPES);
assertTrue(credentials.equals(credentials));
Expand All @@ -161,7 +157,7 @@ public void equals_true() throws IOException {
}

@Test
public void equals_false_scopes() throws IOException {
void equals_false_scopes() throws IOException {
final Collection<String> emptyScopes = Collections.emptyList();
Collection<String> scopes = Collections.singleton("SomeScope");
AppEngineCredentials credentials = new TestAppEngineCredentials(emptyScopes);
Expand All @@ -171,7 +167,7 @@ public void equals_false_scopes() throws IOException {
}

@Test
public void toString_containsFields() throws IOException {
void toString_containsFields() throws IOException {
String expectedToString =
String.format(
"TestAppEngineCredentials{scopes=[%s], scopesRequired=%b}", "SomeScope", false);
Expand All @@ -181,13 +177,13 @@ public void toString_containsFields() throws IOException {
}

@Test
public void hashCode_equals() throws IOException {
void hashCode_equals() throws IOException {
AppEngineCredentials credentials = new TestAppEngineCredentials(SCOPES);
assertEquals(credentials.hashCode(), credentials.hashCode());
}

@Test
public void serialize() throws IOException, ClassNotFoundException {
void serialize() throws IOException, ClassNotFoundException {
Collection<String> scopes = Collections.singleton("SomeScope");
AppEngineCredentials credentials = new TestAppEngineCredentials(scopes);
GoogleCredentials deserializedCredentials = serializeAndDeserialize(credentials);
Expand Down

0 comments on commit 4e6576f

Please sign in to comment.