From cb811d33c138baaaef7960e8dbc739b2acce87fc Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Wed, 25 Sep 2019 07:22:16 -0400 Subject: [PATCH] REOC-68 Test session and use sessionUtils to login and logout --- .../reoc/rets/common/util/SessionUtils.java | 62 +++++++++++++++++++ .../reoc/rets/client/RetsSessionTest.java | 48 ++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 src/main/java/com/ossez/reoc/rets/common/util/SessionUtils.java create mode 100644 src/test/java/com/ossez/reoc/rets/client/RetsSessionTest.java diff --git a/src/main/java/com/ossez/reoc/rets/common/util/SessionUtils.java b/src/main/java/com/ossez/reoc/rets/common/util/SessionUtils.java new file mode 100644 index 0000000..dc571f6 --- /dev/null +++ b/src/main/java/com/ossez/reoc/rets/common/util/SessionUtils.java @@ -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; + } +} diff --git a/src/test/java/com/ossez/reoc/rets/client/RetsSessionTest.java b/src/test/java/com/ossez/reoc/rets/client/RetsSessionTest.java new file mode 100644 index 0000000..77ce866 --- /dev/null +++ b/src/test/java/com/ossez/reoc/rets/client/RetsSessionTest.java @@ -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()); + + } + + +}