Skip to content

Commit

Permalink
Update more AT tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sai-sunder-s committed Dec 12, 2022
1 parent d29c1f1 commit ef9234c
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 11 deletions.
2 changes: 1 addition & 1 deletion oauth2_http/java/com/google/auth/oauth2/AccessToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public AccessToken(String tokenValue, Date expirationTime) {
this.scopes = null;
}

public AccessToken(Builder builder) {
private AccessToken(Builder builder) {
this.tokenValue = builder.getTokenValue();
Date expirationTime = builder.getExpirationTime();
this.expirationTimeMillis = (expirationTime == null) ? null : expirationTime.getTime();
Expand Down
100 changes: 90 additions & 10 deletions oauth2_http/javatests/com/google/auth/oauth2/AccessTokenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,35 +55,97 @@ public void constructor() {
assertEquals(TOKEN, accessToken.getTokenValue());
assertEquals(EXPIRATION_DATE, accessToken.getExpirationTime());
assertEquals(EXPIRATION_DATE.getTime(), (long) accessToken.getExpirationTimeMillis());
assertEquals(null, accessToken.getScopes());
}

@Test
public void equals_true() throws IOException {
AccessToken accessToken = new AccessToken(TOKEN, EXPIRATION_DATE);
AccessToken otherAccessToken = new AccessToken(TOKEN, EXPIRATION_DATE);
AccessToken accessToken =
AccessToken.newBuilder()
.setExpirationTime(EXPIRATION_DATE)
.setTokenValue(TOKEN)
.setScopes(SCOPES)
.build();

AccessToken otherAccessToken =
AccessToken.newBuilder()
.setExpirationTime(EXPIRATION_DATE)
.setTokenValue(TOKEN)
.setScopes(SCOPES)
.build();

assertTrue(accessToken.equals(otherAccessToken));
assertTrue(otherAccessToken.equals(accessToken));
}

@Test
public void equals_false_scopes() throws IOException {
AccessToken accessToken =
AccessToken.newBuilder()
.setExpirationTime(EXPIRATION_DATE)
.setTokenValue(TOKEN)
.setScopes(SCOPES)
.build();

AccessToken otherAccessToken =
AccessToken.newBuilder()
.setExpirationTime(EXPIRATION_DATE)
.setTokenValue(TOKEN)
.setScopes("scope1")
.build();

assertFalse(accessToken.equals(otherAccessToken));
assertFalse(otherAccessToken.equals(accessToken));
}

@Test
public void equals_false_token() throws IOException {
AccessToken accessToken = new AccessToken(TOKEN, EXPIRATION_DATE);
AccessToken otherAccessToken = new AccessToken("otherToken", EXPIRATION_DATE);
AccessToken accessToken =
AccessToken.newBuilder()
.setExpirationTime(EXPIRATION_DATE)
.setTokenValue(TOKEN)
.setScopes(SCOPES)
.build();

AccessToken otherAccessToken =
AccessToken.newBuilder()
.setExpirationTime(EXPIRATION_DATE)
.setTokenValue("otherToken")
.setScopes(SCOPES)
.build();

assertFalse(accessToken.equals(otherAccessToken));
assertFalse(otherAccessToken.equals(accessToken));
}

@Test
public void equals_false_expirationDate() throws IOException {
AccessToken accessToken = new AccessToken(TOKEN, EXPIRATION_DATE);
AccessToken otherAccessToken = new AccessToken(TOKEN, new Date(EXPIRATION_DATE.getTime() + 42));
AccessToken accessToken =
AccessToken.newBuilder()
.setExpirationTime(EXPIRATION_DATE)
.setTokenValue(TOKEN)
.setScopes(SCOPES)
.build();

AccessToken otherAccessToken =
AccessToken.newBuilder()
.setExpirationTime(new Date(EXPIRATION_DATE.getTime() + 42))
.setTokenValue(TOKEN)
.setScopes(SCOPES)
.build();

assertFalse(accessToken.equals(otherAccessToken));
assertFalse(otherAccessToken.equals(accessToken));
}

@Test
public void toString_containsFields() {
AccessToken accessToken = AccessToken.newBuilder().setExpirationTime(EXPIRATION_DATE).setTokenValue(TOKEN).setScopes(SCOPES).build();
AccessToken accessToken =
AccessToken.newBuilder()
.setExpirationTime(EXPIRATION_DATE)
.setTokenValue(TOKEN)
.setScopes(SCOPES)
.build();
String expectedToString =
String.format(
"AccessToken{tokenValue=%s, expirationTimeMillis=%d, scopes=%s}",
Expand All @@ -93,14 +155,32 @@ public void toString_containsFields() {

@Test
public void hashCode_equals() throws IOException {
AccessToken accessToken = new AccessToken(TOKEN, EXPIRATION_DATE);
AccessToken otherAccessToken = new AccessToken(TOKEN, EXPIRATION_DATE);
AccessToken accessToken =
AccessToken.newBuilder()
.setExpirationTime(EXPIRATION_DATE)
.setTokenValue(TOKEN)
.setScopes(SCOPES)
.build();

AccessToken otherAccessToken =
AccessToken.newBuilder()
.setExpirationTime(EXPIRATION_DATE)
.setTokenValue(TOKEN)
.setScopes(SCOPES)
.build();

assertEquals(accessToken.hashCode(), otherAccessToken.hashCode());
}

@Test
public void serialize() throws IOException, ClassNotFoundException {
AccessToken accessToken = new AccessToken(TOKEN, EXPIRATION_DATE);
AccessToken accessToken =
AccessToken.newBuilder()
.setExpirationTime(EXPIRATION_DATE)
.setTokenValue(TOKEN)
.setScopes(SCOPES)
.build();

AccessToken deserializedAccessToken = serializeAndDeserialize(accessToken);
assertEquals(accessToken, deserializedAccessToken);
assertEquals(accessToken.hashCode(), deserializedAccessToken.hashCode());
Expand Down

0 comments on commit ef9234c

Please sign in to comment.