REOC-68 Test session and use sessionUtils to login and logout

This commit is contained in:
YuCheng Hu 2019-09-25 07:22:16 -04:00
parent c61b3ea348
commit cb811d33c1
2 changed files with 110 additions and 0 deletions

View File

@ -0,0 +1,62 @@
package com.ossez.reoc.rets.common.util;
import com.ossez.reoc.rets.client.*;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* SessionUtils for RETS server session
*
* @author YuCheng Hu
*/
public final class SessionUtils {
private static final Logger logger = LoggerFactory.getLogger(SessionUtils.class);
// Prevent the class from being constructed
private SessionUtils() {
}
/**
* Login to Server and return session Object
*
* @param retsLoginUrl
* @param retsUsername
* @param retsPassword
* @return
*/
public static RetsSession retsLogin(String retsLoginUrl, String retsUsername, String retsPassword, RetsVersion retsVersion) {
logger.debug("RETS Session Login URL: [{}]", retsLoginUrl);
LoginResponse loginResponse = new LoginResponse();
//Create a RetsHttpClient (other constructors provide configuration i.e. timeout, gzip capability)
RetsHttpClient httpClient = new CommonsHttpClient();
// SET RETS VERSION
if (ObjectUtils.isEmpty(retsVersion))
retsVersion = RetsVersion.DEFAULT;
//Create a RetesSession with RetsHttpClient
RetsSession session = new RetsSession(retsLoginUrl, httpClient, retsVersion);
//Set method as GET or POST
session.setMethod("POST");
try {
//Login
loginResponse = session.login(retsUsername, retsPassword);
} catch (RetsException ex) {
logger.error("Login RETS Server Error", ex);
}
// SESSION NULL CHECK
if (!(!ObjectUtils.isEmpty(session) && StringUtils.isNotEmpty(loginResponse.getSessionId()))) {
session = null;
}
logger.info("Session ID :[{}]", loginResponse.getSessionId());
return session;
}
}

View File

@ -0,0 +1,48 @@
package com.ossez.reoc.rets.client;
import com.ossez.reoc.rets.common.util.SessionUtils;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Test for RETS session
*
* @author YuCheng Hu
*/
public class RetsSessionTest extends RetsTestCase {
private final Logger logger = LoggerFactory.getLogger(RetsSessionTest.class);
/**
* Test Login
*/
@Test
public void testLogin() {
RetsSession session = SessionUtils.retsLogin(retsLoginUrl, retsUsername, retsPassword, RetsVersion.RETS_1_7_2);
Assert.assertNotNull(session.getSessionId());
}
/**
* TEST Logout
*/
public void testLogout() {
logger.debug("RETS Session Logout URL: [{}]", retsLoginUrl);
RetsSession session = SessionUtils.retsLogin(retsLoginUrl, retsUsername, retsPassword, RetsVersion.RETS_1_7_2);
Assert.assertNotNull(session.getSessionId());
if (session != null) {
try {
session.logout();
} catch (RetsException e) {
e.printStackTrace();
}
}
Assert.assertNull(session.getSessionId());
}
}