Skip to content

Commit

Permalink
Add unit testing and refactor boolean logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Frederiksen committed May 10, 2019
1 parent 4fd6297 commit 7b99d19
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 12 deletions.
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,20 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<version>1.46</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand All @@ -106,7 +106,7 @@ public RestApiSession(BullhornRestCredentials bullhornRestCredentials) {
*/
public String getBhRestToken() throws RestApiException {

if (isSessionExpired() && this.hasNoSessionProvided) {
if (isSessionExpired() && !this.isSessionProvided) {
createSession();
}

Expand Down Expand Up @@ -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);

}
}

Expand Down Expand Up @@ -297,7 +296,7 @@ public String getRestUrl() {
private synchronized void setBhRestToken(String bhRestToken) {
this.bhRestToken = bhRestToken;

if (this.hasNoSessionProvided) {
if (!this.isSessionProvided) {
updateDateTimeBhRestTokenWillExpire();
}

Expand Down Expand Up @@ -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());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 7b99d19

Please sign in to comment.