Commit 113cf398 authored by Youn Cadoret's avatar Youn Cadoret
Browse files

I updated the gwt-message-recording-for-mock, added some unit tests and groovy...

I updated the gwt-message-recording-for-mock, added some unit tests and groovy script for the soapui integration

git-svn-id: https://scm.gforge.inria.fr/authscm/ycadoret/svn/gazelle/Maven/simulators/gazelle-webservice-tester/trunk@67426 356b4b1a-1d2b-0410-8bf1-ffa24008f01e
parent ca94eb98
......@@ -24,12 +24,15 @@
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.1.jre7</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
......
package net.ihe.gazelle.wstester.mockrecord;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
class DAOModel {
private DBConnection connection;
DAOModel(DBConnection connection) {
this.connection = connection;
}
/**
* Get id of the tf_domain
*
* @return integer domain_id
* @throws SQLException
*/
int getDomainId() throws SQLException, NotUniqueException {
return getDomainId();
}
/**
* Get id of the tf_domain
*
* @return integer domain_id
* @throws java.sql.SQLException
*/
int getDomainId(String keyword) throws SQLException, NotUniqueException {
String SQL = "SELECT id from tf_domain where keyword = ?;";
int result = getSelectId(SQL, keyword);
if (result == 0) {
String insert = "INSERT INTO tf_domain (id, description, keyword, name) VALUES (nextval('tf_domain_id_seq'), NULL, ?, ?);";
insertNewTf(insert, keyword);
result = getSelectId(SQL, keyword);
return result;
} else {
return result;
}
}
/**
* Get id of the tf_transaction
*
* @return integer transaction_id
* @throws java.sql.SQLException
*/
int getTransactionId(String keyword) throws SQLException, NotUniqueException {
String SQL = "SELECT id from tf_transaction where keyword = ?;";
int result = getSelectId(SQL, keyword);
if (result == 0) {
String insert = "INSERT INTO tf_transaction (id, description, keyword, name) VALUES (nextval('tf_transaction_id_seq'), NULL, ?, ?);";
insertNewTf(insert, keyword);
result = getSelectId(SQL, keyword);
return result;
} else {
return result;
}
}
/**
* Get id of the tf_actor
*
* @return integer actor_id
* @throws java.sql.SQLException
*/
int getActorId(String keyword) throws SQLException, NotUniqueException {
String SQL = "SELECT id from tf_actor where keyword = ?;";
int result = getSelectId(SQL, keyword);
if (result == 0) {
String insert = "INSERT INTO tf_actor (id, description, keyword, name) VALUES (nextval('tf_actor_id_seq'), NULL, ?, ?);";
insertNewTf(insert, keyword);
result = getSelectId(SQL, keyword);
return result;
} else {
return result;
}
}
/**
* Get id with select request
*
* @return integer select id
* @throws java.sql.SQLException
*/
int getSelectId(String SQL, String keyword) throws SQLException, NotUniqueException {
PreparedStatement st = connection.getConnection().prepareStatement(SQL);
st.setString(1, keyword);
ResultSet rs = st.executeQuery();
while (rs.next()) {
if (rs.isLast()) {
return rs.getInt("id");
} else {
throw new NotUniqueException("We have find several id with the same keyword");
}
}
return 0;
}
/**
* Insert new domain, transaction or actor
*
* @throws java.sql.SQLException
*/
void insertNewTf(String SQL, String keyword) throws SQLException {
PreparedStatement st = connection.getConnection().prepareStatement(SQL);
st.setString(1, keyword);
st.setString(2, keyword);
st.executeUpdate();
}
}
package net.ihe.gazelle.wstester.mockrecord;
import java.sql.*;
import java.util.Date;
class DAOTransaction {
private DBConnection connection;
private int requestId;
private int responseId;
DAOTransaction(DBConnection connection) {
this.connection = connection;
}
private int getRequestId() {
return requestId;
}
private void setRequestId(int requestId) {
this.requestId = requestId;
}
private int getResponseId() {
return responseId;
}
private void setResponseId(int responseId) {
this.responseId = responseId;
}
/**
* insertRequest messages in table
*
* @throws SQLException
*/
void insertRequest(Message message) throws SQLException, NotUniqueException {
String SQL = "INSERT INTO cmn_message_instance (id, content, issuer, issuer_ip_address, type, validation_detailed_result, validation_status, issuing_actor) "
+ "VALUES (nextval('cmn_message_instance_id_seq'),?,?,?,?, null, null,?);";
DAOModel daoModel = new DAOModel(connection);
Connection conn = connection.getConnection();
PreparedStatement st = conn.prepareStatement(SQL, Statement.RETURN_GENERATED_KEYS);
st.setBytes(1, message.getContent());
st.setString(2, message.getIssuer());
st.setString(3, message.getIssuerIP());
st.setString(4, message.getMessageType());
st.setInt(5, daoModel.getActorId(message.getIssuingActor()));
st.executeUpdate();
ResultSet generatedKeys = st.getGeneratedKeys();
if (generatedKeys.next()) {
this.setRequestId(generatedKeys.getInt("id"));
}
}
/**
* insert Response messages in table
*
* @throws SQLException
*/
void insertResponse(Message message) throws SQLException, NotUniqueException {
String SQL = "INSERT INTO cmn_message_instance (id, content, issuer, issuer_ip_address, type, validation_detailed_result, validation_status, issuing_actor) "
+ "VALUES (nextval('cmn_message_instance_id_seq'),?,?,?,?, null, null,?);";
DAOModel daoModel = new DAOModel(connection);
Connection conn = connection.getConnection();
PreparedStatement st = conn.prepareStatement(SQL, Statement.RETURN_GENERATED_KEYS);
st.setBytes(1, message.getContent());
st.setString(2, message.getIssuer());
st.setString(3, message.getIssuerIP());
st.setString(4, message.getMessageType());
st.setInt(5, daoModel.getActorId(message.getIssuingActor()));
st.executeUpdate();
ResultSet generatedKeys = st.getGeneratedKeys();
if (generatedKeys.next()) {
this.setResponseId(generatedKeys.getInt("id"));
}
}
/**
* insert transaction in table
*
* @throws java.sql.SQLException
*/
void insertTransactionInstance(Transaction transaction) throws SQLException, NotUniqueException {
String SQL = "INSERT INTO cmn_transaction_instance (id, company_keyword, standard, timestamp, is_visible, domain_id, request_id, response_id, simulated_actor_id, transaction_id) "
+ "VALUES (nextval('cmn_transaction_instance_id_seq'), null, ?, ?, ?, ?, ?, ?, ?, ? );";
Date date = new Date();
Timestamp ts = new Timestamp(date.getTime());
DAOModel daoModel = new DAOModel(connection);
Connection conn = connection.getConnection();
PreparedStatement st = conn.prepareStatement(SQL);
st.setString(1, transaction.getStandard());
st.setTimestamp(2, transaction.getTimestamp());
st.setBoolean(3, true);
st.setInt(4, daoModel.getDomainId(transaction.getDomainKeyword()));
st.setInt(5, this.getRequestId());
st.setInt(6, this.getResponseId());
st.setInt(7, daoModel.getActorId(transaction.getSimulatedActorKeyword()));
st.setInt(8, daoModel.getTransactionId(transaction.getTransactionKeyword()));
st.executeUpdate();
}
}
package net.ihe.gazelle.wstester.mockrecord;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
class DBConnection {
private String url;
private String user;
private String password;
private Connection connection;
DBConnection(String url, String user, String password) {
this.url = url;
this.user = user;
this.password = password;
}
void connect() throws SQLException {
connection = DriverManager.getConnection(url, user, password);
}
void close() throws SQLException {
getConnection().close();
}
Connection getConnection() throws SQLException {
if (connection == null) {
connect();
}
return connection;
}
}
package net.ihe.gazelle.wstester.mockrecord;
public class Message {
private String issuer;
private String issuerIP;
private String messageType;
private String issuingActor;
private byte[] content;
public Message(String issuer, String issuerIP, String messageType, String issuingActor, byte[] content) {
this.issuer = issuer;
this.issuerIP = issuerIP;
this.messageType = messageType;
this.issuingActor = issuingActor;
this.content = content;
}
public String getIssuer() {
return issuer;
}
public String getIssuerIP() {
return issuerIP;
}
public String getMessageType() {
return messageType;
}
public String getIssuingActor() {
return issuingActor;
}
public byte[] getContent() {
return content;
}
}
package net.ihe.gazelle.wstester.mockrecord;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
public class MessageRecorder {
private String urlDB;
private String userDB;
private String passwordDB;
public MessageRecorder(String urlDB, String userDB, String passwordDB) {
this.urlDB = urlDB;
this.userDB = userDB;
this.passwordDB = passwordDB;
}
public void record(String standardKeyword, String transactionKeyword, String domainKeyword, String simulatedActorKeyword, Message request, Message response) throws SQLException, NotUniqueException {
Date date = new Date();
Timestamp ts = new Timestamp(date.getTime());
DBConnection dbConnection = new DBConnection(urlDB, userDB, passwordDB);
DAOTransaction daoTransaction = new DAOTransaction(dbConnection);
Transaction transaction = new Transaction(standardKeyword, ts, domainKeyword, simulatedActorKeyword, transactionKeyword, request, response);
daoTransaction.insertRequest(request);
daoTransaction.insertResponse(response);
daoTransaction.insertTransactionInstance(transaction);
}
}
package net.ihe.gazelle.wstester.mockrecord;
public class NotUniqueException extends Exception {
public NotUniqueException(String s) {
super(s);
}
}
<
package net.ihe.gazelle.wstester.mockrecord;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* <p>RecordMessages class.</p>
*
* @author ycadoret
* @version 1.0: 28/05/19
*/
public class RecordMessages {
private final String url = "jdbc:postgresql://localhost:5432/gazelle-webservice-tester";
private final String user = "gazelle";
private final String password = "gazelle";
/**
* Connect to the PostgreSQL database
*
* @return a Connection object
* @throws java.sql.SQLException
*/
public Connection connect() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
/**
* Get all id in the domain table
*
* @return id domain
* @throws java.sql.SQLException
*/
public int getDomainId(String keyword) throws SQLException {
String SQL = "SELECT id from tf_domain where keyword like ?;";
int result = getSelectId(SQL, keyword);
return result;
}
/**
* Get all id in the transaction table
*
* @return id transaction
* @throws java.sql.SQLException
*/
public int getTransactionId(String keyword) throws SQLException {
String SQL = "SELECT id from tf_transaction where keyword like ?;";
int result = getSelectId(SQL, keyword);
return result;
}
/**
* Get all id in the actor table
*
* @return id actor
* @throws java.sql.SQLException
*/
public int getActorId(String keyword) throws SQLException {
String SQL = "SELECT id from tf_actor where keyword like ?;";
int result = getSelectId(SQL, keyword);
return result;
}
/**
* Get all id in the request table
*
* @return id request
* @throws java.sql.SQLException
*/
public int getRequestId(String address) throws SQLException {
String SQL = "SELECT MAX(ID) as id from cmn_message_instance where issuer_ip_address like ?;";
int result = getSelectId(SQL, address);
return result;
}
/**
* Get all id in the response table
*
* @return id response
* @throws java.sql.SQLException
*/
public int getResponseId(String address) throws SQLException {
String SQL = "SELECT MAX(ID) as id from cmn_message_instance where issuer_ip_address like ?";
int result = getSelectId(SQL, address);
return result;
}
/**
* Get id with select request
*
* @return int id
* @throws java.sql.SQLException
*/
public int getSelectId(String SQL, String keyword) throws SQLException {
int result = 0;
Connection conn = connect();
try (PreparedStatement st = conn.prepareStatement(SQL)) {
st.setString(1, keyword);
try (ResultSet rs = st.executeQuery()) {
while (rs.next()) {
result = rs.getInt("id");
}
}
}
return result;
}
/**
* insert messages in table
*
* @throws java.sql.SQLException
*/
public void insertMessageInstance(byte[] content, String issuer, String issuerAddress, String messageType, int actor) throws SQLException {
String SQL = "INSERT INTO cmn_message_instance (id, content, issuer, issuer_ip_address, type, validation_detailed_result, validation_status, issuing_actor) "
+ "VALUES (nextval('cmn_message_instance_id_seq'),?,?,?,?, null, null,?);";
try (Connection conn = connect();
PreparedStatement st = conn.prepareStatement(SQL)) {
if (actor == 0) {
st.setNull(5, Types.INTEGER);
} else {
st.setInt(5, actor);
}
st.setBytes(1, content);
st.setString(2, issuer);
st.setString(3, issuerAddress);
st.setString(4, messageType);
st.executeUpdate();
}
}
/**
* insert transaction in table
*
* @throws java.sql.SQLException
*/
public void insertTransactionInstance(String companyKeyword, String standard, int domainId, int requestId, int responseId, int actorId, int transactionId) throws SQLException {
String SQL = "INSERT INTO cmn_transaction_instance (id, company_keyword, standard, timestamp, is_visible, domain_id, request_id, response_id, simulated_actor_id, transaction_id) "
+ "VALUES (nextval('cmn_transaction_instance_id_seq'),?, ?, ?, ?, ?, ?, ?, ?, ? );";
Date date = new Date();
Timestamp ts = new Timestamp(date.getTime());
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try (Connection conn = connect();
PreparedStatement st = conn.prepareStatement(SQL)) {
if (domainId == 0) {
st.setNull(5, Types.INTEGER);
} else {
st.setInt(5, domainId);
}
if (actorId == 0) {
st.setNull(8, Types.INTEGER);
} else {
st.setInt(8, actorId);
}
if (transactionId == 0) {
st.setNull(9, Types.INTEGER);
} else {