Skip to content

Commit

Permalink
adding test for token provider
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanCheshire committed Sep 10, 2024
1 parent 94d4897 commit f048c9d
Showing 1 changed file with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.github.natche.gravatarjavaclient.profile;

import org.junit.jupiter.api.Test;

import java.nio.charset.StandardCharsets;

import static org.junit.jupiter.api.Assertions.*;

/**
* Tests for {@link GravatarProfileTokenProvider}s.
*/
public class GravatarProfileTokenProviderTest {
/**
* Creates a new instance for testing purposes.
*/
GravatarProfileTokenProviderTest() {}

/**
*
*/
@Test
void testCreation() {
assertThrows(NullPointerException.class,
() -> new GravatarProfileTokenProvider(null, null));
assertThrows(NullPointerException.class,
() -> new GravatarProfileTokenProvider(() -> new byte[0], null));
assertThrows(IllegalArgumentException.class,
() -> new GravatarProfileTokenProvider(() -> new byte[0], ""));
assertThrows(IllegalArgumentException.class,
() -> new GravatarProfileTokenProvider(() -> new byte[0], " "));
assertDoesNotThrow(
() -> new GravatarProfileTokenProvider(() -> new byte[0], "source"));
}

/**
*
*/
@Test
void testGetToken() {
byte[] bytes = "PostMalone".getBytes(StandardCharsets.US_ASCII);
GravatarProfileTokenProvider provider = new GravatarProfileTokenProvider(() -> bytes, "source");
assertEquals(bytes, provider.getToken());
}

/**
*
*/
@Test
void testToString() {
byte[] bytes = "PostMalone".getBytes(StandardCharsets.US_ASCII);
GravatarProfileTokenProvider provider = new GravatarProfileTokenProvider(() -> bytes, "source");
GravatarProfileTokenProvider nonEqualProvider
= new GravatarProfileTokenProvider(() -> new byte[0], "another source");

assertEquals("GravatarProfileTokenProvider{source=\"source\"}", provider.toString());
assertEquals("GravatarProfileTokenProvider{source=\"another source\"}", nonEqualProvider.toString());
}

/**
*
*/
@Test
void testEquals() {
byte[] bytes = "PostMalone".getBytes(StandardCharsets.US_ASCII);
GravatarProfileTokenProvider provider = new GravatarProfileTokenProvider(() -> bytes, "source");
GravatarProfileTokenProvider equalProvider = new GravatarProfileTokenProvider(() -> bytes, "source");
GravatarProfileTokenProvider nonEqualProvider
= new GravatarProfileTokenProvider(() -> new byte[0], "another source");

assertEquals(provider, provider);
assertEquals(provider, equalProvider);
assertNotEquals(provider, nonEqualProvider);
assertNotEquals(provider, new Object());
}

/**
*
*/
@Test
void testHashCode() {
byte[] bytes = "PostMalone".getBytes(StandardCharsets.US_ASCII);
GravatarProfileTokenProvider provider = new GravatarProfileTokenProvider(() -> bytes, "source");
GravatarProfileTokenProvider equalProvider = new GravatarProfileTokenProvider(() -> bytes, "source");
GravatarProfileTokenProvider nonEqualProvider
= new GravatarProfileTokenProvider(() -> new byte[0], "another source");

assertEquals(-896505829, provider.hashCode());
assertEquals(-896505829, equalProvider.hashCode());
assertEquals(1165062104, nonEqualProvider.hashCode());
assertNotEquals(equalProvider.hashCode(), nonEqualProvider.hashCode());
}
}

0 comments on commit f048c9d

Please sign in to comment.