Commit a7085e2d authored by R.W.Majeed's avatar R.W.Majeed

improved abstract service and unit tests

parent 75a8781c
......@@ -160,5 +160,42 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-undertow -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-undertow</artifactId>
<version>3.0.18.Final</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-cdi -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-cdi</artifactId>
<version>3.0.18.Final</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.enterprise/cdi-api -->
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jboss.weld.servlet/weld-servlet -->
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet</artifactId>
<version>2.3.5.Final</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.undertow/undertow-servlet -->
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-servlet</artifactId>
<version>1.3.23.Final</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -23,6 +23,15 @@ public interface SessionManager {
public Session accessSession(String sessionId);
public Session createSession(String userId);
public void deleteSession(String sessionId);
/**
* Get the timeout duration in milliseconds. Sessions
* last accessed longer than this will be discarded.
* @return timeout in milliseconds
*/
public long getTimeoutMillis();
/**
* Iterate over all sessions. The remove() operation should
* be supported by the iterators.
......
......@@ -9,4 +9,5 @@ public interface User extends Principal{
Iterable<Project> getProjects();
// check password
boolean hasPassword(char[] password);
void setPassword(char[] newPassword);
}
......@@ -9,7 +9,6 @@ import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
......@@ -26,15 +25,26 @@ public abstract class AbstractService {
public static final String HIVE_NS="http://www.i2b2.org/xsd/hive/msg/1.1/";
/**
* Service name (for communication to client)
* Service name (for communication to client).
* <p>
* The default implementation returns {@link Class#getSimpleName()}.
* </p>
* @return service name, e.g. Workplace Cell
*/
public abstract String getName();
public String getName(){
return getClass().getSimpleName();
}
/**
* Service version (for communication to client)
* <p>
* The default implementation returns {@link Package#getImplementationVersion()}
* for the implementing class.
* </p>
* @return service version, e.g. 1.700
*/
public abstract String getVersion();
public String getVersion(){
return getClass().getPackage().getImplementationVersion();
}
DocumentBuilder newDocumentBuilder() throws ParserConfigurationException{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
......@@ -54,6 +64,9 @@ public abstract class AbstractService {
}
return dom;
}
HiveRequest parseRequest(Document request){
return HiveRequest.parse(request.getDocumentElement());
}
private void stripWhitespace(Element node) throws XPathExpressionException{
XPathFactory xf = XPathFactory.newInstance();
// XPath to find empty text nodes.
......@@ -67,24 +80,32 @@ public abstract class AbstractService {
}
}
private void appendTextNode(Element el, String name, String value){
Element sub = (Element)el.appendChild(el.getOwnerDocument().createElement(name));
if( value != null ){
sub.appendChild(el.getOwnerDocument().createTextNode(value));
}
}
// private void appendTextNode(Element el, String name, String value){
// Element sub = (Element)el.appendChild(el.getOwnerDocument().createElement(name));
// if( value != null ){
// sub.appendChild(el.getOwnerDocument().createTextNode(value));
// }
// }
Document createResponse(DocumentBuilder builder, Element request_header){
Document dom = builder.newDocument();
Element re = (Element)dom.appendChild(dom.createElementNS(HIVE_NS, "response"));
appendTextNode(re, "i2b2_version_compatible", "1.1");
appendTextNode(re, "hl7_version_compatible", "2.4");
// find sending application from request
NodeList nl = request_header.getElementsByTagName("sending_application");
Element el = (Element)re.appendChild(dom.createElement("receiving_application"));
appendTextNode(el, "application_name", nl.item(0).getFirstChild().getTextContent());
appendTextNode(el, "application_version", nl.item(0).getLastChild().getTextContent());
// TODO read response_header.xml template, fill and parse
NodeList nl;
try {
Document rh = builder.parse(getClass().getResourceAsStream("/templates/response_header.xml"));
stripWhitespace(rh.getDocumentElement());
// sending application
nl = rh.getElementsByTagName("sending_application").item(0).getChildNodes();
nl.item(0).setTextContent(getName());
nl.item(1).setTextContent(getVersion());
// timestamp
rh.getElementsByTagName("datetime_of_message").item(0).setTextContent(Instant.now().toString());
// security
nl = rh.getElementsByTagName("security").item(0).getChildNodes();
// TODO message_id, project, session
re.appendChild(dom.adoptNode(rh.getDocumentElement()));
} catch (SAXException | IOException | XPathExpressionException e) {
log.log(Level.WARNING, "unable to process response header template", e);
}
return dom;
}
......
package de.sekmi.histream.i2b2.services;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class HiveRequest {
// message header
String clientName;
String clientVersion;
String secDomain;
String secUser;
String secPassword;
boolean secToken;
String messageId;
String projectId;
// request header
// message body
Element messageBody;
public static HiveRequest parse(Element request){
HiveRequest me = new HiveRequest();
Node n = request.getFirstChild();
if( n.getNodeType() != Node.ELEMENT_NODE || !n.getNodeName().equals("message_header") ){
throw new RuntimeException("request should have message_header as first node instead of "+n.getNodeName());
}
Element mh = (Element)n;
NodeList nl = mh.getElementsByTagName("sending_application").item(0).getChildNodes();
me.clientName = nl.item(0).getTextContent();
me.clientVersion = nl.item(1).getTextContent();
nl = mh.getElementsByTagName("security").item(0).getChildNodes();
me.secDomain = nl.item(0).getTextContent();
me.secUser = nl.item(1).getTextContent();
me.secPassword = nl.item(2).getTextContent();
me.secToken = ((Element)nl.item(2)).getAttribute("is_token").equals("true");
me.messageId = mh.getElementsByTagName("message_control_id").item(0).getFirstChild().getTextContent();
n = request.getLastChild();
if( n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals("message_body") ){
me.messageBody = (Element)n;
}
return me;
}
}
......@@ -20,7 +20,7 @@ import freemarker.template.TemplateException;
@Singleton
@Path("/i2b2/services/PMService")
public class PMService {
public class PMService extends AbstractService{
private static final Logger log = Logger.getLogger(PMService.class.getName());
@Inject
......@@ -56,4 +56,9 @@ public class PMService {
return Response.ok(w.toString()).build();
}
@Override
public String getVersion() {
// TODO Auto-generated method stub
return null;
}
}
<message_header>
<message_header xmlns="http://www.i2b2.org/xsd/hive/msg/1.1/">
<i2b2_version_compatible>1.1</i2b2_version_compatible>
<hl7_version_compatible>2.4</hl7_version_compatible>
<sending_application>
<application_name>Workplace Cell</application_name>
<application_version>1.700</application_version>
<application_name>${service_name}</application_name>
<application_version>${service_version}</application_version>
</sending_application>
<sending_facility>
<facility_name>i2b2 Hive</facility_name>
</sending_facility>
<receiving_application>
<application_name>i2b2 Ontology</application_name>
<application_version>1.6</application_version>
<application_name>${client_name}</application_name>
<application_version>${client_version}</application_version>
</receiving_application>
<receiving_facility>
<facility_name>i2b2 Hive</facility_name>
......
......@@ -4,7 +4,6 @@ import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import org.junit.Test;
import org.w3c.dom.Document;
......@@ -31,6 +30,16 @@ public class TestAbstractService extends AbstractService{
Document resp = createResponse(b, el);
XMLUtils.printDOM(resp, System.out);
}
@Test
public void parse_PM_request() throws Exception{
DocumentBuilder b = newDocumentBuilder();
Document dom = parseRequest(b, getClass().getResourceAsStream("/pm_request_test.xml"));
HiveRequest hr = parseRequest(dom);
assertEquals("i2b2 Project Management", hr.clientName);
assertEquals("1.6", hr.clientVersion);
assertEquals("8b5M3z5lb2nU1g6Zq2QSm", hr.messageId);
}
@Override
public String getName() {
......
package de.sekmi.histream.i2b2.services;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.jboss.resteasy.cdi.CdiInjectorFactory;
import org.jboss.resteasy.plugins.server.undertow.UndertowJaxrsServer;
import org.jboss.resteasy.spi.ResteasyDeployment;
import org.jboss.resteasy.test.TestPortProvider;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class TestPMService {
import io.undertow.servlet.Servlets;
import io.undertow.servlet.api.DeploymentInfo;
public class TestPMService{
private UndertowJaxrsServer server;
@Before
public void startServer() throws Exception {
server = new UndertowJaxrsServer().start();
ResteasyDeployment rd = new ResteasyDeployment();
// rd.getActualResourceClasses().add(WorkplaceService.class);
rd.getActualResourceClasses().add(PMService.class);
rd.setInjectorFactoryClass(CdiInjectorFactory.class.getName());
DeploymentInfo di = server.undertowDeployment(rd);
di.setClassLoader(getClass().getClassLoader());
di.setDeploymentName("TestPM");
di.setContextPath("/");
di.addListeners(Servlets.listener(org.jboss.weld.environment.servlet.Listener.class));
server.deploy(di);
}
public URL createURL(String name) throws MalformedURLException{
return TestPortProvider.createURL("/i2b2/services/PMService"+name);
}
@Test
public void dummy(){
public void testPost() throws MalformedURLException, IOException{
URL url = createURL("/getServices");
System.out.println(url);
HttpURLConnection c = (HttpURLConnection)url.openConnection();
c.setRequestMethod("POST");
c.connect();
Assert.assertEquals(200, c.getResponseCode());
InputStream in = c.getInputStream();
in.close();
}
@Test
public void invalidLoginCredentialsShouldFail(){
}
@After
public void stopServer() {
server.stop();
}
}
package de.sekmi.histream.i2b2.services;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import org.junit.Test;
import freemarker.core.ParseException;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateHashModel;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import freemarker.template.TemplateNotFoundException;
import freemarker.template.TemplateScalarModel;
public class TestTemplates {
@Test
public void test() throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException{
Settings s = new Settings();
Template t = s.getFreemarkerConfiguration().getTemplate("templates/response_header.xml");
t.process(new TemplateHashModel() {
@Override
public boolean isEmpty() throws TemplateModelException {
return false;
}
@Override
public TemplateModel get(String key) throws TemplateModelException {
return new TemplateScalarModel() {
@Override
public String getAsString() throws TemplateModelException {
return "dummy";
}
};
}
}, new PrintWriter(System.out));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment