From 7b99d190a0d4a8fec5275ab5fb5bf597c258d6ff Mon Sep 17 00:00:00 2001 From: Kevin Frederiksen Date: Fri, 10 May 2019 15:08:41 -0500 Subject: [PATCH] Add unit testing and refactor boolean logic --- pom.xml | 14 +++++++ .../data/api/helper/RestApiSession.java | 15 ++++--- .../TestCredentiallessRestApiSession.java | 4 +- .../data/api/helper/TestRestApiSession.java | 41 ++++++++++++++++++- 4 files changed, 62 insertions(+), 12 deletions(-) diff --git a/pom.xml b/pom.xml index d0c96fe7..06138fd8 100644 --- a/pom.xml +++ b/pom.xml @@ -173,6 +173,20 @@ test + + org.testng + testng + 6.14.3 + test + + + + org.jmockit + jmockit + 1.46 + test + + org.spockframework spock-core diff --git a/src/main/java/com/bullhornsdk/data/api/helper/RestApiSession.java b/src/main/java/com/bullhornsdk/data/api/helper/RestApiSession.java index 6a4bdbd5..8dc7a221 100644 --- a/src/main/java/com/bullhornsdk/data/api/helper/RestApiSession.java +++ b/src/main/java/com/bullhornsdk/data/api/helper/RestApiSession.java @@ -60,7 +60,7 @@ public class RestApiSession { private static int SESSION_RETRY = 3; - private boolean hasNoSessionProvided; + private boolean isSessionProvided; public final static int MAX_TTL = 2880; @@ -84,11 +84,11 @@ public static RestApiSession create(@JsonProperty("bullhornRestCredentials") Bul } public RestApiSession(BullhornRestCredentials bullhornRestCredentials) { - this.hasNoSessionProvided = hasNoSessionProvided(bullhornRestCredentials); + this.isSessionProvided = isSessionProvided(bullhornRestCredentials); this.restCredentials = bullhornRestCredentials; this.restTemplate = RestTemplateFactory.getInstance(); this.dateTimeBhRestTokenWillExpire = getNow(); - if (this.hasNoSessionProvided) { + if (!this.isSessionProvided) { createSession(); } else { this.restUrl = restCredentials.getRestUrl(); @@ -106,7 +106,7 @@ public RestApiSession(BullhornRestCredentials bullhornRestCredentials) { */ public String getBhRestToken() throws RestApiException { - if (isSessionExpired() && this.hasNoSessionProvided) { + if (isSessionExpired() && !this.isSessionProvided) { createSession(); } @@ -246,7 +246,6 @@ private void login() { } catch (Exception e) { log.error("Failed to login. " + responseJson, e); throw new RestApiException("Failed to login and get BhRestToken: " + responseJson); - } } @@ -297,7 +296,7 @@ public String getRestUrl() { private synchronized void setBhRestToken(String bhRestToken) { this.bhRestToken = bhRestToken; - if (this.hasNoSessionProvided) { + if (!this.isSessionProvided) { updateDateTimeBhRestTokenWillExpire(); } @@ -326,8 +325,8 @@ public void setDateTimeBhRestTokenWillExpire(DateTime dateTimeBhRestTokenWillExp this.dateTimeBhRestTokenWillExpire = dateTimeBhRestTokenWillExpire; } - public boolean hasNoSessionProvided(BullhornRestCredentials restCredentials) { - return !(StringUtils.isNotBlank(restCredentials.getRestUrl()) && StringUtils.isNotBlank(restCredentials.getBhRestToken())); + public boolean isSessionProvided(BullhornRestCredentials restCredentials) { + return StringUtils.isNotBlank(restCredentials.getRestUrl()) && StringUtils.isNotBlank(restCredentials.getBhRestToken()); } /** diff --git a/src/test/java/com/bullhornsdk/data/api/helper/TestCredentiallessRestApiSession.java b/src/test/java/com/bullhornsdk/data/api/helper/TestCredentiallessRestApiSession.java index 8ca95d91..2289dc7d 100644 --- a/src/test/java/com/bullhornsdk/data/api/helper/TestCredentiallessRestApiSession.java +++ b/src/test/java/com/bullhornsdk/data/api/helper/TestCredentiallessRestApiSession.java @@ -74,8 +74,8 @@ public void testGetRestUrl() { @Test public void testHasNoSessionProvided() { - boolean hasNoSession = restApiSession.hasNoSessionProvided(bullhornRestCredentials); - assertFalse(hasNoSession); + boolean hasNoSession = restApiSession.isSessionProvided(bullhornRestCredentials); + assertTrue(hasNoSession); } diff --git a/src/test/java/com/bullhornsdk/data/api/helper/TestRestApiSession.java b/src/test/java/com/bullhornsdk/data/api/helper/TestRestApiSession.java index f833ee88..501d863f 100644 --- a/src/test/java/com/bullhornsdk/data/api/helper/TestRestApiSession.java +++ b/src/test/java/com/bullhornsdk/data/api/helper/TestRestApiSession.java @@ -2,8 +2,10 @@ import java.io.IOException; +import com.bullhornsdk.data.exception.RestApiException; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; +import org.junit.Assert; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; @@ -15,6 +17,11 @@ import static org.junit.Assert.*; +import mockit.Expectations; +import mockit.Injectable; +import mockit.Mocked; +import mockit.Verifications; + @Ignore public class TestRestApiSession extends BaseTest { @@ -142,9 +149,39 @@ public void testSessionSerialization() throws IOException { @Test public void testHasNoSessionProvided() { - boolean hasNoSession = restApiSession.hasNoSessionProvided(bullhornRestCredentials); - assertTrue(hasNoSession); + boolean hasNoSession = restApiSession.isSessionProvided(bullhornRestCredentials); + assertFalse(hasNoSession); + + } + + @Test + public void testSetDateTimeBhRestTokenWillExpire() { + DateTime testDate = getNow(); + + restApiSession.setDateTimeBhRestTokenWillExpire(testDate); + + assertTrue(restApiSession.getDateTimeBhRestTokenWillExpire().equals(testDate)); + } + + @Test + public void testCreateSessionWithBadCreds_shouldThrowRestException() throws RestApiException { + BullhornRestCredentials creds = new BullhornRestCredentials(); + creds.setRestAuthorizeUrl("NO_VALUE"); + creds.setRestClientId("NO_VALUE"); + creds.setRestClientSecret("NO_VALUE"); + creds.setRestLoginUrl("NO_VALUE"); + creds.setRestSessionMinutesToLive("NO_VALUE"); + creds.setRestTokenUrl("NO_VALUE"); + creds.setUsername("NO_VALUE"); + creds.setPassword("NO_VALUE"); + + try { + new RestApiSession(creds); + Assert.fail("Should have thrown an exception"); + } catch (RestApiException e) { + assertTrue(e.getMessage().equals("Failed to create rest session")); + } } private DateTime getNow() {