Skip to content

Commit

Permalink
Restructure logic for test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Frederiksen authored and kvnfrederiksen committed Jun 20, 2019
1 parent 4870272 commit e3509c5
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,6 @@ public void setRestAuthorizeUrl(String restAuthorizeUrl) {
this.restAuthorizeUrl = restAuthorizeUrl;
}

public boolean hasNoSessionProvided() {
return !(StringUtils.isNotBlank(restUrl) && StringUtils.isNotBlank(bhRestToken));
}

@Override
public int hashCode() {
final int prime = 31;
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/com/bullhornsdk/data/api/helper/RestApiSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
Expand Down Expand Up @@ -59,6 +60,8 @@ public class RestApiSession {

private static int SESSION_RETRY = 3;

private boolean hasNoSessionProvided;

public final static int MAX_TTL = 2880;

/**
Expand All @@ -81,10 +84,11 @@ public static RestApiSession create(@JsonProperty("bullhornRestCredentials") Bul
}

public RestApiSession(BullhornRestCredentials bullhornRestCredentials) {
this.hasNoSessionProvided = hasNoSessionProvided(bullhornRestCredentials);
this.restCredentials = bullhornRestCredentials;
this.restTemplate = RestTemplateFactory.getInstance();
this.dateTimeBhRestTokenWillExpire = getNow();
if (bullhornRestCredentials.hasNoSessionProvided()) {
if (this.hasNoSessionProvided) {
createSession();
} else {
this.restUrl = restCredentials.getRestUrl();
Expand All @@ -102,7 +106,7 @@ public RestApiSession(BullhornRestCredentials bullhornRestCredentials) {
*/
public String getBhRestToken() throws RestApiException {

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

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

if (restCredentials.hasNoSessionProvided()) {
if (this.hasNoSessionProvided) {
updateDateTimeBhRestTokenWillExpire();
}

Expand Down Expand Up @@ -322,6 +326,10 @@ public void setDateTimeBhRestTokenWillExpire(DateTime dateTimeBhRestTokenWillExp
this.dateTimeBhRestTokenWillExpire = dateTimeBhRestTokenWillExpire;
}

public boolean hasNoSessionProvided(BullhornRestCredentials restCredentials) {
return !(StringUtils.isNotBlank(restCredentials.getRestUrl()) && StringUtils.isNotBlank(restCredentials.getBhRestToken()));
}

/**
* Will return the un-encrypted RestCredentials for this RestApiSession. Note that this is only needed for a multi-tenant solution
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.bullhornsdk.data.api.helper;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import com.bullhornsdk.data.BaseTest;
import com.bullhornsdk.data.api.BullhornRestCredentials;

import static org.junit.Assert.*;

@Ignore
public class TestCredentiallessRestApiSession extends BaseTest {

private RestApiSession restApiSession;

private BullhornRestCredentials bullhornRestCredentials;

/**
* Add your own rest credentials here to test the connection.
*/
@Before
public void setupTheCredentials() {
BullhornRestCredentials creds = new BullhornRestCredentials();

creds.setRestUrl("MY-RESTURL");
creds.setBhRestToken("MY-BHRESTTOKEN");
this.bullhornRestCredentials = creds;
this.restApiSession = new RestApiSession(bullhornRestCredentials);
}

@Test
public void testRestApiSession() {

RestApiSession restApiSessionManual = new RestApiSession(bullhornRestCredentials);
assertNotNull("RestApiSession is null", restApiSessionManual);
assertNull("Username is not null", bullhornRestCredentials.getUsername());
assertNull("RestTokenUrl is not null", bullhornRestCredentials.getRestTokenUrl());
assertNull("RestSessionMinutesToLive is not null", bullhornRestCredentials.getRestSessionMinutesToLive());
assertNull("RestLoginUrl is not null", bullhornRestCredentials.getRestLoginUrl());
assertNull("RestClientSecret is not null", bullhornRestCredentials.getRestClientSecret());
assertNull("RestClientId is not null", bullhornRestCredentials.getRestClientId());
assertNull("RestAuthorizeUrl is not null", bullhornRestCredentials.getRestAuthorizeUrl());
}

@Test
public void testGetBhRestToken() {

String restToken = restApiSession.getBhRestToken();
assertNotNull("BhRestToken is null", restToken);
assertNull("Username is not null", bullhornRestCredentials.getUsername());
assertNull("RestTokenUrl is not null", bullhornRestCredentials.getRestTokenUrl());
assertNull("RestSessionMinutesToLive is not null", bullhornRestCredentials.getRestSessionMinutesToLive());
assertNull("RestLoginUrl is not null", bullhornRestCredentials.getRestLoginUrl());
assertNull("RestClientSecret is not null", bullhornRestCredentials.getRestClientSecret());
assertNull("RestClientId is not null", bullhornRestCredentials.getRestClientId());
assertNull("RestAuthorizeUrl is not null", bullhornRestCredentials.getRestAuthorizeUrl());
}

@Test
public void testGetRestUrl() {

String restUrl = restApiSession.getRestUrl();
assertNotNull("restUrl is null", restUrl);
assertNull("Username is not null", bullhornRestCredentials.getUsername());
assertNull("RestTokenUrl is not null", bullhornRestCredentials.getRestTokenUrl());
assertNull("RestSessionMinutesToLive is not null", bullhornRestCredentials.getRestSessionMinutesToLive());
assertNull("RestLoginUrl is not null", bullhornRestCredentials.getRestLoginUrl());
assertNull("RestClientSecret is not null", bullhornRestCredentials.getRestClientSecret());
assertNull("RestClientId is not null", bullhornRestCredentials.getRestClientId());
assertNull("RestAuthorizeUrl is not null", bullhornRestCredentials.getRestAuthorizeUrl());
}

@Test
public void testHasNoSessionProvided() {

boolean hasNoSession = restApiSession.hasNoSessionProvided(bullhornRestCredentials);
assertFalse(hasNoSession);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -140,47 +140,11 @@ public void testSessionSerialization() throws IOException {
}

@Test
public void testRestApiCredentiallessSession() {
public void testHasNoSessionProvided() {

setupCredentiallessTests();
boolean hasNoSession = restApiSession.hasNoSessionProvided(bullhornRestCredentials);
assertTrue(hasNoSession);

RestApiSession restApiSessionManual = new RestApiSession(bullhornRestCredentials);
assertNotNull("RestApiSession is null", restApiSessionManual);
assertNull("", bullhornRestCredentials.getUsername());
assertNull("", bullhornRestCredentials.getUsername());
assertNull("", bullhornRestCredentials.getRestTokenUrl());
assertNull("", bullhornRestCredentials.getRestSessionMinutesToLive());
assertNull("", bullhornRestCredentials.getRestLoginUrl());
assertNull("", bullhornRestCredentials.getRestClientSecret());
assertNull("", bullhornRestCredentials.getRestClientId());
assertNull("", bullhornRestCredentials.getRestAuthorizeUrl());
}

@Test
public void testGetCredentiallessBhRestToken() {
setupCredentiallessTests();

RestApiSession restApiSession = new RestApiSession(bullhornRestCredentials);

String restToken = restApiSession.getBhRestToken();
assertNotNull("BhRestToken is null", restToken);
assertNull("", bullhornRestCredentials.getUsername());
assertNull("", bullhornRestCredentials.getUsername());
assertNull("", bullhornRestCredentials.getRestTokenUrl());
assertNull("", bullhornRestCredentials.getRestSessionMinutesToLive());
assertNull("", bullhornRestCredentials.getRestLoginUrl());
assertNull("", bullhornRestCredentials.getRestClientSecret());
assertNull("", bullhornRestCredentials.getRestClientId());
assertNull("", bullhornRestCredentials.getRestAuthorizeUrl());
}

public void setupCredentiallessTests() {
BullhornRestCredentials creds = new BullhornRestCredentials();

creds.setRestUrl("MY-RESTURL");
creds.setBhRestToken("MY-BHRESTTOKEN");
this.bullhornRestCredentials = creds;
this.restApiSession = new RestApiSession(bullhornRestCredentials);
}

private DateTime getNow() {
Expand Down

0 comments on commit e3509c5

Please sign in to comment.