Skip to content

Commit

Permalink
adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanCheshire committed Sep 10, 2024
1 parent 37ce510 commit ce6c736
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.io.FileWriter;
import java.util.Arrays;
import java.util.Objects;
import java.util.function.Supplier;

/**
* A request for obtaining a Gravatar Profile from the API.
Expand All @@ -23,7 +22,7 @@ public final class GravatarProfileRequest {
/**
* The authorization token supplier
*/
private Supplier<byte[]> tokenSupplier;
private GravatarProfileTokenProvider tokenSupplier;

/**
* The SHA256 hash or profile ID.
Expand All @@ -39,7 +38,7 @@ private GravatarProfileRequest(String hashOrId) {
*
* @param hashOrId the hash or ID
* @return a new GravatarProfileRequest
* @throws NullPointerException if the provided hash/ID is null
* @throws NullPointerException if the provided hash/ID is null
* @throws IllegalArgumentException if the provided hash/ID is empty
*/
public static GravatarProfileRequest fromHashOrId(String hashOrId) {
Expand All @@ -54,7 +53,7 @@ public static GravatarProfileRequest fromHashOrId(String hashOrId) {
*
* @param email the email address
* @return a new GravatarProfileRequest
* @throws NullPointerException if the provided email is null
* @throws NullPointerException if the provided email is null
* @throws IllegalArgumentException if the provided email is empty or not a valid address
*/
public static GravatarProfileRequest fromEmail(String email) {
Expand All @@ -72,7 +71,7 @@ public static GravatarProfileRequest fromEmail(String email) {
* @param tokenSupplier a supplier for returning a token
* @return this request builder
*/
public GravatarProfileRequest setTokenSupplier(Supplier<byte[]> tokenSupplier) {
public GravatarProfileRequest setTokenSupplier(GravatarProfileTokenProvider tokenSupplier) {
Preconditions.checkNotNull(tokenSupplier);
this.tokenSupplier = tokenSupplier;
return this;
Expand All @@ -95,7 +94,7 @@ public String getHashOrId() {
* @throws GravatarJavaClientException if an exception occurs when fetching the profile
*/
public GravatarProfile getProfile() {
byte[] token = tokenSupplier == null ? null : tokenSupplier.get();
byte[] token = tokenSupplier == null ? null : tokenSupplier.getToken();
return GravatarProfileRequestHandler.INSTANCE.getProfile(token, hashOrId);
}

Expand All @@ -104,7 +103,7 @@ public GravatarProfile getProfile() {
*
* @param file the file to write the object to
* @return whether the write operation was successful
* @throws NullPointerException if the provided file is null
* @throws NullPointerException if the provided file is null
* @throws IllegalArgumentException if the provided file is a directory
*/
@CanIgnoreReturnValue
Expand All @@ -117,9 +116,9 @@ public boolean writeToFile(File file) {
* provided GSON object as the serializer.
*
* @param serializer the GSON object to serialize the profile
* @param file the file to write the object to
* @param file the file to write the object to
* @return whether the write operation was successful
* @throws NullPointerException if the provided file or serializer is null
* @throws NullPointerException if the provided file or serializer is null
* @throws IllegalArgumentException if the provided file is a directory
*/
@CanIgnoreReturnValue
Expand All @@ -144,7 +143,7 @@ public boolean writeToFile(Gson serializer, File file) {
@Override
public int hashCode() {
int ret = hashOrId.hashCode();
if (tokenSupplier != null) ret = 31 * ret + Arrays.hashCode(tokenSupplier.get());
if (tokenSupplier != null) ret = 31 * ret + Arrays.hashCode(tokenSupplier.getToken());
return ret;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.github.natche.gravatarjavaclient.profile;

import com.google.common.base.Preconditions;

import java.util.function.Supplier;

/**
* An authentication token provider for Gravatar Profile API calls.
*/
public final class GravatarProfileTokenProvider {
private final Supplier<byte[]> tokenProvider;
private final String source;

/**
* Constructs a new token provider.
*
* @param tokenProvider the encapsulated provider
* @param source the source/key, this is used for hash and equals comparisons
* @throws NullPointerException if any parameter is null
* @throws IllegalArgumentException if source is empty
*/
public GravatarProfileTokenProvider(Supplier<byte[]> tokenProvider, String source) {
Preconditions.checkNotNull(tokenProvider);
Preconditions.checkNotNull(source);
Preconditions.checkArgument(!source.trim().isEmpty());

this.tokenProvider = tokenProvider;
this.source = source;
}

public byte[] getToken() {
return tokenProvider.get();
}

/**
* Returns a hashcode representation of this object.
*
* @return a hashcode representation of this object
*/
@Override
public int hashCode() {
return source.hashCode();
}

/**
* Returns whether the provided object equals this.
*
* @param o the other object
* @return whether the provided object equals this
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
else if (!(o instanceof GravatarProfileTokenProvider)) return false;

GravatarProfileTokenProvider other = (GravatarProfileTokenProvider) o;
return source.equals(other.source);
}

/**
* Returns a string representation of this object.
*
* @return a string representation of this object
*/
@Override
public String toString() {
return "GravatarProfileTokenProvider{source=\"" + source + "\"}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ void testFromEmail() {
void testSetTokenSupplier() {
assertThrows(NullPointerException.class,
() -> GravatarProfileRequest.fromHashOrId("hash").setTokenSupplier(null));
assertDoesNotThrow(() -> GravatarProfileRequest.fromHashOrId("hash").setTokenSupplier(() -> null));
assertDoesNotThrow(() -> GravatarProfileRequest.fromHashOrId("hash").setTokenSupplier(() -> new byte[0]));
assertDoesNotThrow(
() -> GravatarProfileRequest.fromHashOrId("hash").setTokenSupplier(TokenSupplier.getTokenSupplier()));
}

/**
Expand Down Expand Up @@ -109,7 +109,8 @@ void testToString() {
GravatarProfileRequest unauthenticatedRequest = GravatarProfileRequest.fromHashOrId("nathanvcheshire");

assertEquals("GravatarProfileRequest{hash=\"nathanvcheshire\","
+ " tokenSupplier=null}", authenticatedRequest.toString());
+ " tokenSupplier=GravatarProfileTokenProvider{source=\"TokenSupplier class\"}}",
authenticatedRequest.toString());
assertEquals("GravatarProfileRequest{hash=\"nathanvcheshire\", tokenSupplier=null}",
unauthenticatedRequest.toString());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package com.github.natche.gravatarjavaclient.profile;

import java.nio.charset.StandardCharsets;
import java.util.function.Supplier;

/**
* A token supplier for grabbing the Profile API key from the system environment.
*/
public final class TokenSupplier {
private static final String KEY_NAME = "GRAVATAR_JAVA_CLIENT_GITHUB_API_KEY";

private static final Supplier<byte[]> tokenSupplier = () -> {
try {
return System.getenv(KEY_NAME).getBytes(StandardCharsets.UTF_8);
} catch (Exception e) {
return new byte[0];
}
};
private static final GravatarProfileTokenProvider tokenSupplier = new GravatarProfileTokenProvider(
() -> System.getenv(KEY_NAME).getBytes(StandardCharsets.US_ASCII), "TokenSupplier class"
);

/**
* Suppress default constructor
Expand All @@ -29,7 +24,7 @@ private TokenSupplier() {
*
* @return the token supplier to grab the API key from the system environment.
*/
public static Supplier<byte[]> getTokenSupplier() {
public static GravatarProfileTokenProvider getTokenSupplier() {
return tokenSupplier;
}
}

0 comments on commit ce6c736

Please sign in to comment.