openrat-java-client

Unnamed repository; edit this file 'description' to name the repository.
git clone http://git.code.weiherhei.de/openrat-java-client.git
Log | Files | Refs

commit c20e80fd6b120a807f992a75d04436dbd7b5d4c0
parent dc5bbb4209fbe640b1e9f31fae393e2d1b0cfdab
Author: Jan Dankert <jan.dankert@hansemerkur.de>
Date:   Fri,  8 Nov 2019 13:36:19 +0100

Make the API client more simple with a fluent interface.

Diffstat:
.gitignore | 3+++
.hgignore | 5-----
src/de/openrat/client/CMSClient.java | 31+++++++------------------------
src/de/openrat/client/Version.java | 5+++++
src/de/openrat/client/action/Action.java | 118-------------------------------------------------------------------------------
src/de/openrat/client/action/CMSAction.java | 67-------------------------------------------------------------------
src/de/openrat/client/action/LoginAction.java | 71-----------------------------------------------------------------------
src/de/openrat/client/action/ProjectAction.java | 31-------------------------------
src/de/openrat/client/dto/CMSObject.java | 78------------------------------------------------------------------------------
src/de/openrat/client/dto/Language.java | 6------
src/de/openrat/client/dto/Project.java | 6------
src/de/openrat/client/dto/Template.java | 6------
src/de/openrat/client/dto/User.java | 28----------------------------
src/de/openrat/client/util/CMSRequest.java | 183++++++++++++++++++++++++++++++++++---------------------------------------------
test/de/openrat/client/test/ExampleCMSTestAPI.java | 31+++++++++++++++++++++++++++++++
test/de/openrat/client/test/LoginAndInfoTest.java | 34++++++++++++++++++++++++++++++++++
test/de/openrat/client/test/TestConfiguration.java | 50++++++++++++++++++++++++++++++--------------------
test/de/openrat/client/test/TestLogin.java | 37-------------------------------------
test/de/openrat/client/test/TestLowLevelAPI.java | 56--------------------------------------------------------
19 files changed, 189 insertions(+), 657 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,2 @@ +.idea/ +classes/+ \ No newline at end of file diff --git a/.hgignore b/.hgignore @@ -1,5 +0,0 @@ -syntax: glob -bin/* -.settings/* -.project -.classpath diff --git a/src/de/openrat/client/CMSClient.java b/src/de/openrat/client/CMSClient.java @@ -22,14 +22,10 @@ package de.openrat.client; import java.io.IOException; import java.io.PrintWriter; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; import java.util.Locale; -import de.openrat.client.action.Action; -import de.openrat.client.action.CMSAction; -import de.openrat.client.action.LoginAction; import de.openrat.client.util.CMSConnection; +import de.openrat.client.util.CMSRequest; /** * Client for the OpenRat Content Management System. <br> @@ -210,6 +206,8 @@ public class CMSClient * closing all resources. Normally, you do not need to call this, because * the sockets are closed after each call. this is only for future versions * wenn keep-alive is implemented. + * + * thank you for calling this method. * */ public void close() @@ -243,28 +241,13 @@ public class CMSClient /** - * Creates a more low-level common action. + * Creating a new CMS request. * - * @return new action instance + * @return CMS request */ - public CMSAction createAction() { - return createAction( CMSAction.class ); + public CMSRequest request(String action, String method ) { + return new CMSRequest(this.connection, action,method); } - /** - * Creates a custom action. - * - * @param actionClass the action class to instantiate - * @param <T> - * @return new action instance - */ - public <T extends Action> T createAction(Class<T> actionClass) { - try { - Constructor<T> c = actionClass.getConstructor(CMSConnection.class); - return c.newInstance( this.connection ); - } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { - throw new IllegalArgumentException("The action '" + actionClass.getSimpleName() + " could not be created: " + e.getMessage(), e); - } - } } diff --git a/src/de/openrat/client/Version.java b/src/de/openrat/client/Version.java @@ -1,5 +1,10 @@ package de.openrat.client; +/** + * Checking and comparing versions. + * + * @source https://stackoverflow.com/questions/198431/how-do-you-compare-two-version-strings-in-java + */ public class Version implements Comparable<Version> { private String version; diff --git a/src/de/openrat/client/action/Action.java b/src/de/openrat/client/action/Action.java @@ -1,118 +0,0 @@ -package de.openrat.client.action; - -import de.openrat.client.util.CMSConnection; -import de.openrat.client.util.CMSException; -import de.openrat.client.util.CMSRequest; -import de.openrat.client.util.CMSResponse; -import de.openrat.client.util.HttpRequest.HttpMethod; -import de.openrat.client.util.Id; -import de.openrat.client.util.ParameterMap; - -/** - * Action. - * - * This class is NOT threadsafe and should be used by one thread simultaneously. - */ -public abstract class Action -{ - - /** - * Parameter map. - */ - private ParameterMap parameter = new ParameterMap(); - private Id id; - - private CMSConnection connection; - - /** - * Clear parameter values. - */ - public void clear() - { - parameter.clear(); - } - - protected Action(CMSConnection connection) - { - this.connection = connection; - } - - /** - * Setting a parameter value. - * - * @param paramName - * name - * @param paramValue - * value - */ - protected void setParameter(String paramName, String paramValue) - { - - parameter.put(paramName, paramValue); - } - - /** - * Setting a parameter value. <strong>DO NOT url-encode your values</strong> - * as this is done automatically inside this method! - * - * @param paramName - * name - * @param paramValue - * value - */ - public void addParameter(String paramName, String paramValue) - { - - if (paramName == null || paramValue == null || "" == paramName) - throw new IllegalArgumentException("parameter name and value must have values"); - - parameter.put(paramName, paramValue); - } - - public void setId(Id id) - { - this.id = id; - } - - public CMSConnection getConnection() - { - return connection; - } - - protected CMSResponse executeView(String action, String method) { - return execute(action,method,HttpMethod.GET); - } - - protected CMSResponse executePost(String action, String method) { - return execute(action,method,HttpMethod.POST); - } - - private CMSResponse execute(String action, String method, HttpMethod httpMethod) throws CMSException - { - - final ParameterMap parameter = new ParameterMap(); - - parameter.put(connection.getParamActionName(), action); - parameter.put(connection.getParamMethodName(), method); - parameter.put("id", String.valueOf(id)); - parameter.putAll(this.parameter); - - CMSRequest request = new CMSRequest(this.connection); - request.setMethod(httpMethod); - request.getParameter().putAll(parameter); - - try - { - CMSResponse response = request.execute(); - return response; - } - catch (CMSException e) - { - throw e; - } - catch (Exception e) - { - throw new CMSException(e); - } - } -} diff --git a/src/de/openrat/client/action/CMSAction.java b/src/de/openrat/client/action/CMSAction.java @@ -1,67 +0,0 @@ -package de.openrat.client.action; - -import de.openrat.client.util.CMSConnection; -import de.openrat.client.util.CMSException; -import de.openrat.client.util.CMSNode; -import de.openrat.client.util.CMSResponse; -import de.openrat.client.util.HttpRequest.HttpMethod; - -import javax.security.auth.login.LoginException; -import java.util.Map; - -/** - * This class is NOT threadsafe and should be used by one thread simultaneously. - * - * @author dankert - */ -public class CMSAction extends Action { - - public CMSAction(CMSConnection connection) { - - super(connection); - } - - /** - * Custom method call. - * - * @return CMSNode - */ - public CMSNode executeView(String action, String method, Map<String, String> parameter) { - - return execute(action,method,parameter,HttpMethod.GET); - } - /** - * Custom method call. - * - * @return CMSNode - */ - public CMSNode executePost(String action, String method, Map<String, String> parameter) { - - return execute(action,method,parameter,HttpMethod.POST); - } - - - /** - * Custom method call. - * - * @return CMSNode - */ - private CMSNode execute(String action, String method, Map<String, String> parameter,HttpMethod httpMethod) { - - clear(); - - for (Map.Entry<String, String> entry : parameter.entrySet()) - setParameter(entry.getKey(), entry.getValue()); - - try { - CMSResponse response; - if( httpMethod.equals(HttpMethod.GET)) - response = executeView(action, method); - else - response = executePost(action, method); - return response.getOutput(); - } catch (CMSException e) { - throw e; - } - } -} diff --git a/src/de/openrat/client/action/LoginAction.java b/src/de/openrat/client/action/LoginAction.java @@ -1,71 +0,0 @@ -package de.openrat.client.action; - -import javax.security.auth.login.LoginException; - -import de.openrat.client.dto.User; -import de.openrat.client.util.CMSConnection; -import de.openrat.client.util.CMSException; -import de.openrat.client.util.CMSResponse; -import de.openrat.client.util.CMSServerErrorException; -import de.openrat.client.util.HttpRequest.HttpMethod; - -/** - * This class is NOT threadsafe and should be used by one thread simultaneously. - * - * @author dankert - */ -public class LoginAction extends Action -{ - private final static String ACTION = "login"; - public LoginAction(CMSConnection connection) - { - super(connection); - } - - /** - * Login. - * - * @param username - * username - * @param password - * password - * @param databaseId - * database-id - * @return {@link User} - * @throws LoginException - * if credentials are wrong - */ - public User login(String username, String password, String databaseId) throws LoginException - { - - setParameter("dbid", databaseId); - executeView(ACTION, "login"); - - clear(); - setParameter("login_name", username); - setParameter("login_password", password); - setParameter("dbid", databaseId); - try - { - CMSResponse response = executePost(ACTION, "login"); - - User user = new User(); - return user; - } - catch (CMSException e) - { - if (e instanceof CMSServerErrorException && ((CMSServerErrorException) e).getStatus().equals("LOGIN_FAILED")) - // wrong credentials - throw checked exception - throw new LoginException(e.getLocalizedMessage()); - else - // otherwise it's a technical exception - throw e; - } - - } - - public void logout() - { - executePost(ACTION, "logout"); - } -} diff --git a/src/de/openrat/client/action/ProjectAction.java b/src/de/openrat/client/action/ProjectAction.java @@ -1,31 +0,0 @@ -package de.openrat.client.action; - -import de.openrat.client.dto.Project; -import de.openrat.client.util.CMSConnection; -import de.openrat.client.util.CMSResponse; -import de.openrat.client.util.Id; - -/** - * This class is NOT threadsafe and should be used by one thread simultaneously. - * - * @author dankert - */ -public class ProjectAction extends Action -{ - private final static String PROJECT = "project"; - - public ProjectAction(CMSConnection connection) - { - super(connection); - } - - public Project getInfo(Id id) - { - - CMSResponse response = executeView(PROJECT, "info"); - // TODO - - return new Project(); - } - -} diff --git a/src/de/openrat/client/dto/CMSObject.java b/src/de/openrat/client/dto/CMSObject.java @@ -1,78 +0,0 @@ -package de.openrat.client.dto; - -import java.io.Serializable; - -import de.openrat.client.util.Id; - -public class CMSObject implements Serializable -{ - private static final long serialVersionUID = -7483013624561450027L; - - private Id id; - - /** - * Inhalt des Feldes <code>id</code>. - * - * @return id - */ - public Id getId() - { - return id; - } - - /** - * Setzt das Feld <code>id</code>. - * - * @param id - * id - */ - public void setId(Id id) - { - this.id = id; - } - - /** - * {@inheritDoc} - * - * @see java.lang.Object#hashCode() - */ - @Override - public int hashCode() - { - final int prime = 31; - int result = 1; - result = prime * result + ((id == null) ? 0 : id.hashCode()); - return result; - } - - /** - * {@inheritDoc} - * - * @see java.lang.Object#equals(java.lang.Object) - */ - @Override - public boolean equals(Object obj) - { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - CMSObject other = (CMSObject) obj; - if (id == null) - { - if (other.id != null) - return false; - } - else if (!id.equals(other.id)) - return false; - return true; - } - - @Override - public String toString() - { - return super.toString() + ": Id " + id.longValue(); - } -} diff --git a/src/de/openrat/client/dto/Language.java b/src/de/openrat/client/dto/Language.java @@ -1,6 +0,0 @@ -package de.openrat.client.dto; - -public class Language extends CMSObject -{ - -} diff --git a/src/de/openrat/client/dto/Project.java b/src/de/openrat/client/dto/Project.java @@ -1,6 +0,0 @@ -package de.openrat.client.dto; - -public class Project extends CMSObject -{ - -} diff --git a/src/de/openrat/client/dto/Template.java b/src/de/openrat/client/dto/Template.java @@ -1,6 +0,0 @@ -package de.openrat.client.dto; - -public class Template extends CMSObject -{ - -} diff --git a/src/de/openrat/client/dto/User.java b/src/de/openrat/client/dto/User.java @@ -1,28 +0,0 @@ -package de.openrat.client.dto; - -public class User extends CMSObject -{ - private String name; - - /** - * Inhalt des Feldes <code>name</code>. - * - * @return name - */ - public String getName() - { - return name; - } - - /** - * Setzt das Feld <code>name</code>. - * - * @param name - * name - */ - public void setName(String name) - { - this.name = name; - } - -} diff --git a/src/de/openrat/client/util/CMSRequest.java b/src/de/openrat/client/util/CMSRequest.java @@ -70,9 +70,10 @@ import de.openrat.client.util.HttpRequest.HttpMethod; * * @author Jan Dankert */ -public class CMSRequest -{ +public class CMSRequest { + private String actionMethod; + private String action; private ParameterMap parameter = new ParameterMap(); private PrintWriter logWriter; @@ -82,15 +83,18 @@ public class CMSRequest private CMSConnection connection; /** - * Set the HTTP Method. Default is "GET". - * - * @param method - * HTTP-method + * Marks this request for writing. */ - public void setMethod(HttpMethod method) - { + public CMSRequest forWriting() { + this.method = HttpMethod.POST; + return this; + } - this.method = method; + /** + */ + public CMSRequest forReading() { + this.method = HttpMethod.GET; + return this; } /** @@ -98,25 +102,42 @@ public class CMSRequest * * @param connection Connection to server */ - public CMSRequest(CMSConnection connection) - { - + public CMSRequest(CMSConnection connection) { super(); this.connection = connection; this.logWriter = connection.getLogWriter(); } + + public CMSRequest(CMSConnection connection, String action, String method) { + this(connection); + this.action = action; + this.actionMethod = method; + } + + + public CMSRequest addParameter(String name, CharSequence value) { + this.parameter.put(name, value.toString()); + return this; + } + + public CMSRequest addParameter(String name, long value) { + this.parameter.put(name, Long.toString(value)); + return this; + } + + /** * Sends a request to the openrat-server and parses the response into a DOM * tree document. * * @return server response as a DOM tree - * @throws IOException - * if server is unrechable or responds non-wellformed XML + * @throws IOException if server is unrechable or responds non-wellformed XML */ - public CMSResponse execute() throws IOException - { + public CMSResponse execute() throws IOException { + parameter.put("action",this.action); + parameter.put("subaction",this.actionMethod); parameter.put("token", connection.getToken()); HttpRequest httpRequest = new HttpRequest(); @@ -132,24 +153,18 @@ public class CMSRequest HttpResponse httpResponse = httpClient.execute(httpRequest); final CMSNode rootNode; - try - { + try { // Try XML parsing final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilder builder = factory.newDocumentBuilder(); final Document document = builder.parse(new InputSource(new StringReader(httpResponse.getPayload()))); rootNode = convertXMLNodeIntoCMSNode(document.getDocumentElement()); - } - catch (ParserConfigurationException e) - { - if (logWriter != null) - { + } catch (ParserConfigurationException e) { + if (logWriter != null) { e.printStackTrace(logWriter); } - throw new CMSException("XML-Parser-Configuration invalid: "+e.getMessage(), e); - } - catch (SAXException e) - { + throw new CMSException("XML-Parser-Configuration invalid: " + e.getMessage(), e); + } catch (SAXException e) { throw new CMSServerErrorException("Server did not return a valid XML-document: " + httpResponse.getPayload(), "" + httpResponse.getHttpStatus().getStatusCode(), httpResponse.getHttpStatus().getServerMessage(), e.getMessage(), e); } @@ -165,22 +180,17 @@ public class CMSRequest * Erzeugt aus * * @param httpStatus - * * @param rootNode * @return */ - private CMSResponse createCMSResponse(HttpStatus httpStatus, final CMSNode rootNode) - { + private CMSResponse createCMSResponse(HttpStatus httpStatus, final CMSNode rootNode) { - if (httpStatus.getStatusCode() == 204) - { + if (httpStatus.getStatusCode() == 204) { return null; // No content } - if (httpStatus.isServerError()) - { - if (rootNode.getName().equals("server") || rootNode.getName().equals("error") ) - { + if (httpStatus.isServerError()) { + if (rootNode.getName().equals("server") || rootNode.getName().equals("error")) { // Server reports an technical error. String error = rootNode.getFirstChildByName("error").getValue(); String status = rootNode.getFirstChildByName("status").getValue(); @@ -189,37 +199,29 @@ public class CMSRequest ServerSideException cause = null; - if ( rootNode.getFirstChildByName("trace") != null ) - cause = createExceptionFromServerTrace( rootNode.getFirstChildByName("trace")); + if (rootNode.getFirstChildByName("trace") != null) + cause = createExceptionFromServerTrace(rootNode.getFirstChildByName("trace")); throw new CMSServerErrorException(error, status, description, reason, cause); - } - else - { + } else { throw new CMSServerErrorException(httpStatus.getServerMessage(), "" + httpStatus.getStatusCode(), "", ""); } } - if (httpStatus.getStatusCode() == 200) - { + if (httpStatus.getStatusCode() == 200) { - if (rootNode.getName() == "server") - { + if (rootNode.getName() == "server") { // Server reports an answer CMSResponse cmsResponse = createCMSReponse(rootNode); return cmsResponse; - } - else - { + } else { // HTTP-Status 200 OK, but no XML-Element "server" found. throw new CMSServerErrorException(httpStatus.getServerMessage(), "" + httpStatus.getStatusCode(), "", "no SERVER element found"); } - } - else - { + } else { // Unknown HTTP Status throw new CMSServerErrorException(httpStatus.getServerMessage(), "" + httpStatus.getStatusCode(), "", "Unsupported HTTP Status"); } @@ -229,40 +231,38 @@ public class CMSRequest final List<StackTraceElement> traceElements = new ArrayList<>(); - for( CMSNode traceElementNode : trace.getChildren() ) { + for (CMSNode traceElementNode : trace.getChildren()) { String file = traceElementNode.getFirstChildValue("file"); String line = traceElementNode.getFirstChildValue("line"); int lineNumber = 0; - if ( line != null) - lineNumber = Integer.parseInt(line); + if (line != null) + lineNumber = Integer.parseInt(line); String fct = traceElementNode.getFirstChildValue("function"); String cls = traceElementNode.getFirstChildValue("class"); - if(cls == null) cls = ""; - traceElements.add( new StackTraceElement(cls, fct,file,lineNumber) ); + if (cls == null) cls = ""; + traceElements.add(new StackTraceElement(cls, fct, file, lineNumber)); } String name = "Exception"; String message = "server error"; - ServerSideException cause = new ServerSideException(message,name); + ServerSideException cause = new ServerSideException(message, name); - cause.setStackTrace( traceElements.toArray( new StackTraceElement[]{}) ); + cause.setStackTrace(traceElements.toArray(new StackTraceElement[]{})); return cause; } - private CMSResponse createCMSReponse(final CMSNode rootNode) - { + private CMSResponse createCMSReponse(final CMSNode rootNode) { CMSResponse cmsResponse = new CMSResponse(); // Do we support the server api version? - Version apiVersion = new Version( rootNode.getFirstChildByName("api").getValue()); + Version apiVersion = new Version(rootNode.getFirstChildByName("api").getValue()); - if (! apiVersion.equals( CMSClient.SUPPORTED_API_VERSION )) - { + if (!apiVersion.equals(CMSClient.SUPPORTED_API_VERSION)) { // oh no, the server api is older or newer than our client api. // there is nothing we can do. throw new CMSServerErrorException("Only API Version " + CMSClient.SUPPORTED_API_VERSION + @@ -270,19 +270,17 @@ public class CMSRequest } cmsResponse.setApi(apiVersion); - cmsResponse.setVersion( new Version(rootNode.getFirstChildByName("version").getValue())); + cmsResponse.setVersion(new Version(rootNode.getFirstChildByName("version").getValue())); List<String> errorList = new ArrayList<String>(); - for (CMSNode errorNode : rootNode.getFirstChildByName("errors").getChildren()) - { + for (CMSNode errorNode : rootNode.getFirstChildByName("errors").getChildren()) { errorList.add(errorNode.getValue()); } cmsResponse.setValidationErrors(errorList); List<CMSNotice> noticeList = new ArrayList<CMSNotice>(); - for (CMSNode noticeNode : rootNode.getFirstChildByName("notices").getChildren()) - { + for (CMSNode noticeNode : rootNode.getFirstChildByName("notices").getChildren()) { CMSNotice error = new CMSNotice(); error.setKey(noticeNode.getFirstChildByName("key").getValue()); error.setType(noticeNode.getFirstChildByName("type").getValue()); @@ -310,48 +308,33 @@ public class CMSRequest session.setToken(sessionNode.getFirstChildByName("token").getValue()); cmsResponse.setSession(session); - cmsResponse.setOutput(rootNode.getFirstChildByName("outpout")); + cmsResponse.setOutput(rootNode.getFirstChildByName("output")); return cmsResponse; } - public void setLogWriter(PrintWriter logWriter) - { - this.logWriter = logWriter; - } - - public static Iterable<Node> iterable(final NodeList n) - { + public static Iterable<Node> iterable(final NodeList n) { - return new Iterable<Node>() - { + return new Iterable<Node>() { - public Iterator<Node> iterator() - { + public Iterator<Node> iterator() { - return new Iterator<Node>() - { + return new Iterator<Node>() { int index = 0; - public boolean hasNext() - { + public boolean hasNext() { return index < n.getLength(); } - public Node next() - { - if (hasNext()) - { + public Node next() { + if (hasNext()) { return n.item(index++); - } - else - { + } else { throw new NoSuchElementException(); } } - public void remove() - { + public void remove() { throw new UnsupportedOperationException(); } }; @@ -359,15 +342,12 @@ public class CMSRequest }; } - private static CMSNode convertXMLNodeIntoCMSNode(Node node) - { + private static CMSNode convertXMLNodeIntoCMSNode(Node node) { List<CMSNode> children = new ArrayList<CMSNode>(); - for (Node nodex : iterable(node.getChildNodes())) - { - if (nodex.getNodeType() == Node.ELEMENT_NODE) - { + for (Node nodex : iterable(node.getChildNodes())) { + if (nodex.getNodeType() == Node.ELEMENT_NODE) { CMSNode childNode = convertXMLNodeIntoCMSNode(nodex); children.add(childNode); @@ -376,9 +356,4 @@ public class CMSRequest return new CMSNode(node.getNodeName(), node.getTextContent(), children); } - - public ParameterMap getParameter() - { - return parameter; - } } diff --git a/test/de/openrat/client/test/ExampleCMSTestAPI.java b/test/de/openrat/client/test/ExampleCMSTestAPI.java @@ -0,0 +1,30 @@ +package de.openrat.client.test; + +import de.openrat.client.CMSClient; +import de.openrat.client.util.CMSNode; + +import java.io.IOException; + +/** + * this is an example API facade for calling CMS functions. + */ +public class ExampleCMSTestAPI { + + private CMSClient client; + + public ExampleCMSTestAPI(CMSClient client) { + this.client = client; + } + + public boolean login( String dbid, String user, String password ) throws IOException { + client.request("login","login").execute(); // Read Session and Token. + + return client.request("login","login").forWriting().addParameter("dbid",dbid).addParameter("login_name",user).addParameter("login_password",password).execute().getValidationErrors().isEmpty(); + } + + public CMSNode info() throws IOException { + return client.request("login","license").execute().getOutput(); + } + + // ... add another methods here ... +}+ \ No newline at end of file diff --git a/test/de/openrat/client/test/LoginAndInfoTest.java b/test/de/openrat/client/test/LoginAndInfoTest.java @@ -0,0 +1,34 @@ +package de.openrat.client.test; + +import de.openrat.client.CMSClient; +import org.junit.Test; + +import static de.openrat.client.test.TestConfiguration.*; + +public class LoginAndInfoTest { + + /** + * simple example for using the client. + */ + @Test + public void testUserInfo() throws Exception { + + final CMSClient client = new CMSClient(TestConfiguration.HOST, PATH, PORT); + client.setLogWriter(WRITER); + client.setProxy(PROXY_HOST, PROXY_PORT); + client.setLocale(LOCALE); + client.setKeepAlive(false); + client.setTimeout(15000); + + ExampleCMSTestAPI api = new ExampleCMSTestAPI(client); + + // let's login + api.login( DB,USER, PASS); + + // print user info + System.out.println(api.info()); + + client.close(); + } + +} diff --git a/test/de/openrat/client/test/TestConfiguration.java b/test/de/openrat/client/test/TestConfiguration.java @@ -1,20 +1,30 @@ -package de.openrat.client.test; - -import java.io.PrintWriter; -import java.io.Writer; -import java.util.Locale; - -public class TestConfiguration { - - public static final String HOST = "demo.openrat.de"; - public static final String PATH = "/"; - public static final int PORT = 80; - public static final String USER = "admin"; - public static final String PASS = "admin"; - public static final String DB = "db1"; - public static final String PROXY_HOST = null; - public static final int PROXY_PORT = 8080; - - public static final PrintWriter WRITER = new PrintWriter(System.out, true); - public static final Locale LOCALE = Locale.ENGLISH; -} +package de.openrat.client.test; + +import java.io.PrintWriter; +import java.io.Writer; +import java.util.Locale; + +public class TestConfiguration { + + public static final String HOST = "demo.openrat.de"; + public static final String PATH = "/"; + public static final int PORT = 80; + public static final String USER = "admin"; + public static final String PASS = "admin"; + public static final String DB = "demo"; + public static String PROXY_HOST = null; + public static int PROXY_PORT = 8080; + + public static final PrintWriter WRITER = new PrintWriter(System.out, true); + public static final Locale LOCALE = Locale.ENGLISH; + + static { + final String proxyHost = System.getenv("PROXY_HOST"); + if (proxyHost != null) + PROXY_HOST = proxyHost; + + final String proxyPort = System.getenv("PROXY_PORT"); + if (proxyPort != null) + PROXY_PORT = Integer.parseInt(proxyPort); + } +} diff --git a/test/de/openrat/client/test/TestLogin.java b/test/de/openrat/client/test/TestLogin.java @@ -1,37 +0,0 @@ -package de.openrat.client.test; - -import de.openrat.client.CMSClient; -import de.openrat.client.action.LoginAction; -import org.junit.Test; - -import javax.security.auth.login.LoginException; - -import static de.openrat.client.test.TestConfiguration.*; -import static org.junit.Assert.fail; - -public class TestLogin { - - /** - * simple example for using the client. - */ - @Test - public void test() { - final CMSClient client = new CMSClient(TestConfiguration.HOST, PATH, PORT); - client.setLogWriter(WRITER); - client.setProxy(PROXY_HOST, PROXY_PORT); - client.setLocale(LOCALE); - client.setKeepAlive(false); - client.setTimeout(15000); - - LoginAction loginAction = client.createAction(LoginAction.class); - - try { - loginAction.login(USER, PASS, DB); - } catch (LoginException e) { - fail("Login failed" + e.getLocalizedMessage()); - } - - client.close(); - } - -} diff --git a/test/de/openrat/client/test/TestLowLevelAPI.java b/test/de/openrat/client/test/TestLowLevelAPI.java @@ -1,56 +0,0 @@ -package de.openrat.client.test; - -import de.openrat.client.CMSClient; -import de.openrat.client.action.CMSAction; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import java.util.HashMap; -import java.util.Map; - -import static de.openrat.client.test.TestConfiguration.*; - -public class TestLowLevelAPI { - - private CMSClient client; - - /** - * - */ - @Before - public void connect() { - - client = new CMSClient(TestConfiguration.HOST, PATH, PORT); - client.setLogWriter(WRITER); - client.setProxy(PROXY_HOST, PROXY_PORT); - client.setLocale(LOCALE); - client.setKeepAlive(false); - client.setTimeout(15000); - } - - - /** - * simple example for using the client. - */ - @Test - public void test() { - final CMSAction action = client.createAction(); - - action.executeView("login", "login", new HashMap<>()); - - final Map<String, String> logindata = new HashMap<>(); - logindata.put("login_username", USER); - logindata.put("login_password", PASS); - logindata.put("dbid", DB); - - action.executePost("login", "login", logindata); - } - - @After - public void close() { - - client.close(); - } - -}