Commit b7bb7616 authored by Nicolas Tachker's avatar Nicolas Tachker
Browse files

first REST tests.

parent d349aa98
/*
* JORAM: Java(TM) Open Reliable Asynchronous Messaging
* Copyright (C) 2016 ScalAgent Distributed Technologies
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Initial developer(s): ScalAgent D.T.
* Contributor(s):
*/
package joram.rest;
import java.net.URI;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.xml.bind.DatatypeConverter;
import org.glassfish.jersey.client.ClientConfig;
import framework.TestCase;
/**
* Test: REST admin command with authentication
* create queues, users and connections factories
*/
public class AdminREST extends TestCase {
public static void main(String[] args) {
new AdminREST().run();
}
public void run() {
try {
startAgentServer((short)0);
// wait REST bundle
Helper.waitConnection(Helper.getBaseJmsURI(), 10);
ClientConfig config = new ClientConfig();
Client client = ClientBuilder.newClient(config);
WebTarget target = client.target(Helper.getBaseAdminURI());
URI uri = target.path("queue").path("myQueue").getUri();
String encodedUserPassword = DatatypeConverter.printBase64Binary("admin:admin".getBytes());
// Create a queue
Response response = client.target(uri).request().header("Authorization", encodedUserPassword).accept(MediaType.TEXT_PLAIN).get();
assertEquals("admin create queue (myQueue)", 201, response.getStatus());
//create queue
uri = target.path("queue").path("myQueue1").getUri();
response = client.target(uri).request().header("Authorization", encodedUserPassword).accept(MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON).post(
Entity.entity(
"{\n" +
" \"name\": \"myqueue\",\n" +
" \"type\": \"0\",\n" +
" \"description\": \"Queue test\"\n" +
"}\n", MediaType.APPLICATION_JSON));
assertEquals("admin create queue (myQueue1)", 201, response.getStatus());
//create tcp CF
uri = target.path("tcp").path("create").queryParam("jndi-name", "myCF").getUri();
response = client.target(uri).request().header("Authorization", encodedUserPassword).accept(MediaType.TEXT_PLAIN).get();
assertEquals("admin create tcp CF (myCF)", 201, response.getStatus());
//create local CF
uri = target.path("local").path("create").queryParam("jndi-name", "myCF1").getUri();
response = client.target(uri).request().header("Authorization", encodedUserPassword).accept(MediaType.TEXT_PLAIN).get();
assertEquals("admin create local CF (myCF1)", 201, response.getStatus());
//create user
uri = target.path("user").path("myUser").queryParam("password", "pass").getUri();
response = client.target(uri).request().header("Authorization", encodedUserPassword).accept(MediaType.TEXT_PLAIN).get();
assertEquals("admin create user (myUser)", 201, response.getStatus());
//create user
uri = target.path("user").path("myUser1").queryParam("password", "pass").getUri();
response = client.target(uri).request().header("Authorization", encodedUserPassword).accept(MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON).post(
Entity.entity(
"{\n" +
" \"name\": \"user1\",\n" +
" \"type\": \"0\",\n" +
" \"description\": \"User test\"\n" +
"}\n", MediaType.APPLICATION_JSON));
assertEquals("admin create user (myUser1)", 201, response.getStatus());
//delete user
uri = target.path("user").path("myUser1").queryParam("password", "pass").getUri();
response = client.target(uri).request().header("Authorization", encodedUserPassword).accept(MediaType.TEXT_PLAIN).delete();
assertEquals("admin delete user (myUser1)", 200, response.getStatus());
//delete queue
uri = target.path("queue").path("myQueue").getUri();
response = client.target(uri).request().header("Authorization", encodedUserPassword).accept(MediaType.TEXT_PLAIN).delete();
assertEquals("admin delete queue (myQueue)", 200, response.getStatus());
} catch (Throwable exc) {
exc.printStackTrace();
error(exc);
} finally {
killAgentServer((short)0);
endTest();
}
}
}
/*
* JORAM: Java(TM) Open Reliable Asynchronous Messaging
* Copyright (C) 2016 ScalAgent Distributed Technologies
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Initial developer(s): ScalAgent D.T.
* Contributor(s):
*/
package joram.rest;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.client.ClientConfig;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import framework.TestCase;
/**
* Test:
* - create jms queue (if not exist)
* - send receive a ByteMessage
*
*/
public class ClientRESTBytesMessage extends TestCase {
public static void main(String[] args) {
new ClientRESTBytesMessage().run();
}
public void run() {
try {
startAgentServer((short)0);
// wait REST bundle
Helper.waitConnection(Helper.getBaseJmsURI(), 10);
ClientConfig config = new ClientConfig();
Client client = ClientBuilder.newClient(config);
WebTarget target = client.target(Helper.getBaseJmsURI());
URI uriCreateProd = target.path("jms").path("queue").path("myQueue1").path("create-producer")
.queryParam("name", "prod1").getUri();
URI uriCreateCons = target.path("jms").path("queue").path("myQueue1").path("create-consumer")
.queryParam("name", "cons1")
.getUri();
// Create the producer
Response response = client.target(uriCreateProd).request().accept(MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON).head();
assertEquals("create-producer jms (myQueue1)", 201, response.getStatus());
URI uriCloseProd = response.getLink("close-context").getUri();
URI uriSendNextMsg = response.getLink("send-next-message").getUri();
String messageSend = "my test message";
HashMap<String, Object> maps = new HashMap<>();
// message type
maps.put("type", "BytesMessage");
// message body
byte[] m = messageSend.getBytes();
maps.put("body", m);
// message properties
HashMap<String, Object> props = new HashMap<>();
props.put("p1", new String[]{"value1", String.class.getName()});
props.put("p2", new String[]{"2", Integer.class.getName()});
maps.put("properties", props);
// message header
HashMap<String, Object> header = new HashMap<>();
header.put("CorrelationID", new String[]{"correlation1234", String.class.getName()});
header.put("Priority", new String[]{"2", Integer.class.getName()});
maps.put("header", header);
Gson gson = new GsonBuilder().create();
String json = gson.toJson(maps);
// System.out.println("send json = " + json);
// Send next message
response = client.target(uriSendNextMsg).request().accept(MediaType.TEXT_PLAIN).post(
Entity.entity(json, MediaType.APPLICATION_JSON));
assertEquals("send-next-message", 200, response.getStatus());
// Create the consumer
response = client.target(uriCreateCons).request().accept(MediaType.TEXT_PLAIN).head();
assertEquals("create-consumer jms (myQueue1)", 201, response.getStatus());
URI uriCloseCons = response.getLink("close-context").getUri();
// receive the message
URI uriReceive = response.getLink("receive-next-message").getUri();
Builder builder = client.target(uriReceive).request().accept(MediaType.APPLICATION_JSON);
response = builder.get();
json = response.readEntity(String.class);
assertEquals("receive-next-message", 200, response.getStatus());
assertNotNull("receive-next-message json", json);
HashMap<String, Object> msg = gson.fromJson(json, HashMap.class);
assertNotNull("receive-next-message msg", msg);
String type = (String) msg.get("type");
assertEquals("receive-next-message type", "BytesMessage", type);
String message = new String(gson.fromJson(msg.get("body").toString(), byte[].class));
assertEquals("receive-next-message type", messageSend, message);
Map jmsProperties = (Map) msg.get("properties");
// System.out.println("*** properties " + jmsProperties);
assertNotNull("receive-next-message jmsProperties", jmsProperties);
Map jmsHeader = (Map) msg.get("header");
// System.out.println("*** header " + jmsHeader);
assertNotNull("receive-next-message jmsHeader", jmsHeader);
// close the producer
response = client.target(uriCloseProd).request().accept(MediaType.TEXT_PLAIN).delete();
assertEquals("close-producer", 200, response.getStatus());
// close the consumer
response = client.target(uriCloseCons).request().accept(MediaType.TEXT_PLAIN).delete();
assertEquals("close-consumer", 200, response.getStatus());
} catch (Throwable exc) {
exc.printStackTrace();
error(exc);
} finally {
killAgentServer((short)0);
endTest();
}
}
}
/*
* JORAM: Java(TM) Open Reliable Asynchronous Messaging
* Copyright (C) 2016 ScalAgent Distributed Technologies
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Initial developer(s): ScalAgent D.T.
* Contributor(s):
*/
package joram.rest;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.client.ClientConfig;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import framework.TestCase;
/**
* Test:
* - create jms queue (if not exist)
* - send receive a MapMessage
*
*/
public class ClientRESTMapMessage extends TestCase {
public static void main(String[] args) {
new ClientRESTMapMessage().run();
}
public void run() {
try {
startAgentServer((short)0);
// wait REST bundle
Helper.waitConnection(Helper.getBaseJmsURI(), 10);
ClientConfig config = new ClientConfig();
Client client = ClientBuilder.newClient(config);
WebTarget target = client.target(Helper.getBaseJmsURI());
URI uriCreateProd = target.path("jms").path("queue").path("myQueue1").path("create-producer")
.queryParam("name", "prod1").getUri();
URI uriCreateCons = target.path("jms").path("queue").path("myQueue1").path("create-consumer")
.queryParam("name", "cons1")
.getUri();
// Create the producer
Response response = client.target(uriCreateProd).request().accept(MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON).head();
assertEquals("create-producer jms (myQueue1)", 201, response.getStatus());
URI uriCloseProd = response.getLink("close-context").getUri();
URI uriSendNextMsg = response.getLink("send-next-message").getUri();
HashMap<String, Object> maps = new HashMap<>();
maps.put("type", "MapMessage");
String value1 = "my test message";
HashMap mapMessage = new HashMap();
mapMessage.put("key1", value1);
mapMessage.put("key2", 2);
maps.put("body", mapMessage);
HashMap<String, Object> props = new HashMap<>();
props.put("p1", new String[]{"value1", String.class.getName()});
props.put("p2", new String[]{"2", Integer.class.getName()});
maps.put("properties", props);
//maps.put("header", props);
Gson gson = new GsonBuilder().create();
String json = gson.toJson(maps);
//System.out.println("send json = " + json);
// Send next message
response = client.target(uriSendNextMsg).request().accept(MediaType.TEXT_PLAIN).post(
Entity.entity(json, MediaType.APPLICATION_JSON));
assertEquals("send-next-message", 200, response.getStatus());
// Create the consumer
response = client.target(uriCreateCons).request().accept(MediaType.TEXT_PLAIN).head();
assertEquals("create-consumer jms (myQueue1)", 201, response.getStatus());
URI uriCloseCons = response.getLink("close-context").getUri();
// receive the message
URI uriReceive = response.getLink("receive-next-message").getUri();
Builder builder = client.target(uriReceive).request().accept(MediaType.APPLICATION_JSON);
response = builder.get();
json = response.readEntity(String.class);
assertEquals("receive-next-message", 200, response.getStatus());
assertNotNull("receive-next-message json", json);
HashMap<String, Object> msg = gson.fromJson(json, HashMap.class);
assertNotNull("receive-next-message msg", msg);
String type = (String) msg.get("type");
assertEquals("receive-next-message type", "MapMessage", type);
Map jmsBody = (Map) msg.get("body");
// System.out.println("*** message = " + jmsBody);
assertEquals("receive-next-message key1", value1, jmsBody.get("key1"));
Map jmsProperties = (Map) msg.get("properties");
// System.out.println("*** jmsProperties " + jmsProperties);
assertNotNull("receive-next-message jmsProperties", jmsProperties);
Map jmsHeader = (Map) msg.get("header");
// System.out.println("*** header " + jmsHeader);
assertNotNull("receive-next-message jmsHeader", jmsHeader);
// close the producer
response = client.target(uriCloseProd).request().accept(MediaType.TEXT_PLAIN).delete();
assertEquals("close-producer", 200, response.getStatus());
// close the consumer
response = client.target(uriCloseCons).request().accept(MediaType.TEXT_PLAIN).delete();
assertEquals("close-consumer", 200, response.getStatus());
} catch (Throwable exc) {
exc.printStackTrace();
error(exc);
} finally {
killAgentServer((short)0);
endTest();
}
}
}
\ No newline at end of file
/*
* JORAM: Java(TM) Open Reliable Asynchronous Messaging
* Copyright (C) 2016 ScalAgent Distributed Technologies
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Initial developer(s): ScalAgent D.T.
* Contributor(s):
*/
package joram.rest;
import java.io.FileInputStream;
import java.net.URI;
import java.security.KeyStore;
import javax.net.ssl.SSLContext;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.SslConfigurator;
import framework.TestCase;
public class ClientRESTSSL extends TestCase {
public static void main(String[] args) {
new ClientRESTSSL().run();
}
public void run() {
try {
startAgentServer((short)0);
// wait REST bundle
Helper.waitConnection(Helper.getBaseJmsURI(), 10);
javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
new javax.net.ssl.HostnameVerifier(){
public boolean verify(String hostname,
javax.net.ssl.SSLSession sslSession) {
//System.out.println("== verify " + hostname);
if (hostname.equals("localhost")) {
return true;
}
return false;
}
});
SslConfigurator sslConfig = SslConfigurator.newInstance()
.trustStoreFile("./jssecacerts")
.keyStoreFile("./joram_ks")
.keyPassword("jorampass");
SSLContext sslContext = sslConfig.createSSLContext();
Client client = ClientBuilder.newBuilder().sslContext(sslContext).build();
WebTarget target = client.target("https://localhost:8443/joram/");
// lookup the destination
Builder builder = target.path("jndi").path("queue").request();
Response response = builder.accept(MediaType.TEXT_PLAIN).get();
assertEquals("jndi-queue", 201, response.getStatus());
URI uriCreateProd = response.getLink("create-producer").getUri();
URI uriCreateCons = response.getLink("create-consumer").getUri();
// Create the producer
response = client.target(uriCreateProd).queryParam("user", "anonymous").queryParam("password", "anonymous").request().accept(MediaType.TEXT_PLAIN).head();
assertEquals("create-producer", 201, response.getStatus());
URI uriCloseProd = response.getLink("close-context").getUri();
URI uriSendNextMsg = response.getLink("send-next-message").getUri();
// Send next message
String message = "Test message.";
response = client.target(uriSendNextMsg).request().
accept(MediaType.TEXT_PLAIN).post(Entity.entity(message, MediaType.TEXT_PLAIN));
assertEquals("send-next-message", 200, response.getStatus());
// Create the consumer
response = client.target(uriCreateCons).request().accept(MediaType.TEXT_PLAIN).head();
assertEquals("create-consumer", 201, response.getStatus());
URI uriCloseCons = response.getLink("close-context").getUri();
// receive the message
URI uriReceive = response.getLink("receive-next-message").getUri();
builder = client.target(uriReceive).request().accept(MediaType.TEXT_PLAIN);
response = builder.get();
String msg = response.readEntity(String.class);
assertEquals("receive-next-message", 200, response.getStatus());
assertNotNull("receive-message", msg);
assertEquals("receive-message", message, msg);
// close the producer
response = client.target(uriCloseProd).request().accept(MediaType.TEXT_PLAIN).delete();
assertEquals("close-producer", 200, response.getStatus());
// close the consumer
response = client.target(uriCloseCons).request().accept(MediaType.TEXT_PLAIN).delete();
assertEquals("close-consumer", 200, response.getStatus());
} catch (Throwable exc) {
exc.printStackTrace();
error(exc);
} finally {
killAgentServer((short)0);
endTest();
}
}
public static KeyStore loadStore(String trustStoreFile, String password) throws Exception {
KeyStore store = KeyStore.getInstance("JKS");
store.load(new FileInputStream(trustStoreFile), password.toCharArray());
return store;
}
}
/*
* JORAM: Java(TM) Open Reliable Asynchronous Messaging
* Copyright (C) 2016 ScalAgent Distributed Technologies
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Initial developer(s): ScalAgent D.T.
* Contributor(s):
*/
package joram.rest;
import java.net.URI;
import java.util.HashMap;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation.Builder;