Skip to content

Commit

Permalink
Silverpop#20 don't reexecute logout command when session is expired t…
Browse files Browse the repository at this point in the history
…o prevent infinite loop
  • Loading branch information
rkovarik committed Sep 7, 2015
1 parent 29c19bf commit 96078e9
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 4 deletions.
19 changes: 19 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<systemProperties>
<property>
<name>java.awt.headless</name>
<value>true</value>
</property>
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>

Expand Down Expand Up @@ -56,5 +69,11 @@
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
10 changes: 6 additions & 4 deletions src/main/java/com/silverpop/api/client/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<REQUEST extends ApiRequest> {
Expand Down Expand Up @@ -43,7 +45,7 @@ public ApiResult executeCommand(ApiCommand command, Map<String,String> 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 {
Expand All @@ -52,11 +54,11 @@ public ApiResult executeCommand(ApiCommand command, Map<String,String> 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<String,String> requestHeaders) throws ApiResultException {
protected ApiResult validateSessionAndExecuteCommand(ApiCommand command, Map<String,String> requestHeaders) throws ApiResultException {
ensureSessionIsOpen();

REQUEST request = commandProcessor.prepareRequest(command, getSession());
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> 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");
}
}
}

0 comments on commit 96078e9

Please sign in to comment.