diff --git a/pom.xml b/pom.xml index c72560c..c14ebfc 100644 --- a/pom.xml +++ b/pom.xml @@ -17,6 +17,19 @@ + + org.apache.maven.plugins + maven-surefire-plugin + 2.18.1 + + + + java.awt.headless + true + + + + @@ -56,5 +69,11 @@ 1.9.5 test + + junit + junit + 4.11 + test + diff --git a/src/main/java/com/silverpop/api/client/ApiClient.java b/src/main/java/com/silverpop/api/client/ApiClient.java index 099757e..74a8d82 100644 --- a/src/main/java/com/silverpop/api/client/ApiClient.java +++ b/src/main/java/com/silverpop/api/client/ApiClient.java @@ -9,6 +9,8 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import com.silverpop.api.client.command.LoginCommand; +import com.silverpop.api.client.command.LogoutCommand; import com.silverpop.api.client.xmlapi.NoResponseApiErrorResult; public abstract class ApiClient { @@ -43,7 +45,7 @@ public ApiResult executeCommand(ApiCommand command, Map requestHe try { return validateSessionAndExecuteCommand(command, requestHeaders); } catch(ApiResultException e) { - if(retryCommand(e.getErrorResult())) { + if(retryCommand(e.getErrorResult(), command)) { getSession().close(); return validateSessionAndExecuteCommand(command, requestHeaders); } else { @@ -52,11 +54,11 @@ public ApiResult executeCommand(ApiCommand command, Map requestHe } } - private boolean retryCommand(ApiErrorResult errorResult) { - return errorResult.isSessionLost() && getSession().isReAuthenticate(); + private boolean retryCommand(ApiErrorResult errorResult, ApiCommand command) { + return errorResult.isSessionLost() && getSession().isReAuthenticate() && !(command instanceof LogoutCommand); } - private ApiResult validateSessionAndExecuteCommand(ApiCommand command, Map requestHeaders) throws ApiResultException { + protected ApiResult validateSessionAndExecuteCommand(ApiCommand command, Map requestHeaders) throws ApiResultException { ensureSessionIsOpen(); REQUEST request = commandProcessor.prepareRequest(command, getSession()); diff --git a/src/test/java/com/silverpop/api/client/xmlapi/XmlApiClientTest.java b/src/test/java/com/silverpop/api/client/xmlapi/XmlApiClientTest.java new file mode 100644 index 0000000..e56e1ba --- /dev/null +++ b/src/test/java/com/silverpop/api/client/xmlapi/XmlApiClientTest.java @@ -0,0 +1,66 @@ +package com.silverpop.api.client.xmlapi; + +import static org.junit.Assert.fail; +import static org.mockito.Mockito.*; + +import java.util.Map; + +import org.apache.commons.httpclient.HttpMethodBase; +import org.apache.commons.httpclient.methods.GetMethod; +import org.junit.Test; + +import com.silverpop.api.client.ApiClient; +import com.silverpop.api.client.ApiCommand; +import com.silverpop.api.client.ApiCommandProcessor; +import com.silverpop.api.client.ApiErrorResult; +import com.silverpop.api.client.ApiRequest; +import com.silverpop.api.client.ApiResult; +import com.silverpop.api.client.ApiResultException; +import com.silverpop.api.client.ApiSession; +import com.silverpop.api.client.command.LoginCommand; +import com.silverpop.api.client.command.LogoutCommand; +import com.silverpop.api.client.xmlapi.util.XmlApiClientFactory; + +public class XmlApiClientTest { + + private XmlApiClient client = null; + + @Test(expected = ApiResultException.class) + public void logoutWhenSessionIsExpired() throws ApiResultException { + //GIVEN + XmlApiClientFactory factory = new XmlApiClientFactory() { + @Override + public XmlApiClient createClient(XmlApiSession session) { + return client; + } + }; + + XmlApiSession session = new XmlApiSession("", new LoginCommand(), factory) { + @Override + public boolean isOpen() { + return true; + } + }; + + client = new XmlApiClient(session) { + @Override + protected ApiResult validateSessionAndExecuteCommand(ApiCommand command, Map requestHeaders) throws ApiResultException { + session.open(); + ApiErrorResult result = mock(ApiErrorResult.class); + when(result.isSessionLost()).thenReturn(true); + ApiResultException exception = new ApiResultException("", result); + throw exception; + } + }; + + LogoutCommand command = new LogoutCommand(); + + //WHEN + try { + client.executeCommand(command); + } catch (StackOverflowError e) { + //THEN + fail(e.getClass().toString() + " should not occur"); + } + } +}