Commit e0450fea authored by Youn Cadoret's avatar Youn Cadoret
Browse files

I added tests and i solved some errors

git-svn-id: https://scm.gforge.inria.fr/authscm/ycadoret/svn/gazelle/Maven/simulators/gazelle-webservice-tester/trunk@67452 356b4b1a-1d2b-0410-8bf1-ffa24008f01e
parent 9216403e
......@@ -33,6 +33,10 @@
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
......
......@@ -2,7 +2,6 @@ package net.ihe.gazelle.wstester.mockrecord;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
class DAOModel {
......@@ -19,14 +18,12 @@ class DAOModel {
* @return integer domain_id
* @throws java.sql.SQLException
*/
int getDomainId(String keyword) throws SQLException, NotUniqueException {
int getOrCreateDomainId(String keyword) throws MessageException {
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;
String insert = "INSERT INTO tf_domain (id, description, keyword, name) VALUES (nextval('tf_domain_id_seq'), NULL, ?, ?) returning id;";
return insertNewTf(insert, keyword);
} else {
return result;
}
......@@ -39,14 +36,12 @@ class DAOModel {
* @return integer transaction_id
* @throws java.sql.SQLException
*/
int getTransactionId(String keyword) throws SQLException, NotUniqueException {
int getOrCreateTransactionId(String keyword) throws MessageException {
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;
String insert = "INSERT INTO tf_transaction (id, description, keyword, name) VALUES (nextval('tf_transaction_id_seq'), NULL, ?, ?) returning id;";
return insertNewTf(insert, keyword);
} else {
return result;
}
......@@ -59,14 +54,12 @@ class DAOModel {
* @return integer actor_id
* @throws java.sql.SQLException
*/
int getActorId(String keyword) throws SQLException, NotUniqueException {
int getOrCreateActorId(String keyword) throws MessageException {
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;
String insert = "INSERT INTO tf_actor (id, description, keyword, name) VALUES (nextval('tf_actor_id_seq'), NULL, ?, ?) returning id;";
return insertNewTf(insert, keyword);
} else {
return result;
}
......@@ -79,21 +72,21 @@ class DAOModel {
* @return integer select id
* @throws java.sql.SQLException
*/
int getSelectId(String SQL, String keyword) throws SQLException, NotUniqueException {
try (PreparedStatement st = connection.getConnection().prepareStatement(SQL)) {
int getSelectId(String SQL, String keyword) throws MessageException {
try {
PreparedStatement st = connection.getConnection().prepareStatement(SQL);
st.setString(1, keyword);
try (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");
}
ResultSet rs = st.executeQuery();
while (rs.next()) {
if (rs.isLast()) {
return rs.getInt("id");
} else {
throw new MessageException("We have found several id with the same keyword '" + keyword + "'");
}
return 0;
}
return 0;
} catch (Exception e) {
throw new MessageException("An error occurred while searching the keyword '" + keyword + "'");
}
}
......@@ -103,12 +96,23 @@ class DAOModel {
*
* @throws java.sql.SQLException
*/
void insertNewTf(String SQL, String keyword) throws SQLException {
try (PreparedStatement st = connection.getConnection().prepareStatement(SQL)) {
int insertNewTf(String SQL, String keyword) throws MessageException {
try {
int id = 0;
PreparedStatement st = connection.getConnection().prepareStatement(SQL);
st.setString(1, keyword);
st.setString(2, keyword);
st.executeUpdate();
ResultSet resultSet = st.executeQuery();
if (resultSet.next()) {
id = (resultSet.getInt("id"));
}
return id;
} catch (Exception e) {
throw new MessageException("An error occurred while adding the new keyword '" + keyword + "'");
}
}
......
package net.ihe.gazelle.wstester.mockrecord;
import java.sql.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
class DAOTransaction {
......@@ -37,31 +40,30 @@ class DAOTransaction {
*
* @throws SQLException
*/
void insertRequest(Message message) throws SQLException, NotUniqueException {
void insertRequest(Message message) throws MessageException {
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 {
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,?) returning id;";
DAOModel daoModel = new DAOModel(connection);
Connection conn = connection.getConnection();
try (PreparedStatement st = conn.prepareStatement(SQL)) {
DAOModel daoModel = new DAOModel(connection);
Connection conn = connection.getConnection();
PreparedStatement st = conn.prepareStatement(SQL);
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();
st.setInt(5, daoModel.getOrCreateActorId(message.getIssuingActor()));
ResultSet resultSet = st.executeQuery();
try (Statement statement = conn.createStatement()) {
try (ResultSet generatedKeys = statement.executeQuery("SELECT MAX(id) as id from cmn_message_instance;")) {
if (generatedKeys.next()) {
this.setRequestId(generatedKeys.getInt("id"));
}
}
if (resultSet.next()) {
this.setRequestId(resultSet.getInt("id"));
}
}
} catch (Exception e) {
throw new MessageException("An error occurred while recording the request message");
}
}
......@@ -70,29 +72,29 @@ class DAOTransaction {
*
* @throws SQLException
*/
void insertResponse(Message message) throws SQLException, NotUniqueException {
void insertResponse(Message message) throws MessageException {
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 {
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,?) returning id;";
DAOModel daoModel = new DAOModel(connection);
Connection conn = connection.getConnection();
try (PreparedStatement st = conn.prepareStatement(SQL)) {
DAOModel daoModel = new DAOModel(connection);
Connection conn = connection.getConnection();
PreparedStatement st = conn.prepareStatement(SQL);
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();
st.setInt(5, daoModel.getOrCreateActorId(message.getIssuingActor()));
ResultSet resultSet = st.executeQuery();
try (Statement statement = conn.createStatement()) {
try (ResultSet generatedKeys = statement.executeQuery("SELECT MAX(id) as id from cmn_message_instance;")) {
if (generatedKeys.next()) {
this.setResponseId(generatedKeys.getInt("id"));
}
}
if (resultSet.next()) {
this.setResponseId(resultSet.getInt("id"));
}
} catch (Exception e) {
throw new MessageException("An error occurred while recording the response message");
}
}
......@@ -102,24 +104,31 @@ class DAOTransaction {
*
* @throws java.sql.SQLException
*/
void insertTransactionInstance(Transaction transaction) throws SQLException, NotUniqueException {
void insertTransactionInstance(Transaction transaction) throws MessageException {
try {
this.insertRequest(transaction.getRequest());
this.insertResponse(transaction.getResponse());
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, ?, ?, ?, ?, ?, ?, ?, ? );";
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, ?, ?, ?, ?, ?, ?, ?, ? );";
DAOModel daoModel = new DAOModel(connection);
Connection conn = connection.getConnection();
try (PreparedStatement st = conn.prepareStatement(SQL)) {
DAOModel daoModel = new DAOModel(connection);
Connection conn = connection.getConnection();
PreparedStatement st = conn.prepareStatement(SQL);
st.setString(1, transaction.getStandard());
st.setString(1, transaction.getStandard().name());
st.setTimestamp(2, transaction.getTimestamp());
st.setBoolean(3, true);
st.setInt(4, daoModel.getDomainId(transaction.getDomainKeyword()));
st.setInt(4, daoModel.getOrCreateDomainId(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.setInt(7, daoModel.getOrCreateActorId(transaction.getSimulatedActorKeyword()));
st.setInt(8, daoModel.getOrCreateTransactionId(transaction.getTransactionKeyword()));
st.executeUpdate();
} catch (Exception e) {
throw new MessageException("An error occurred while recording the transaction");
}
}
......
package net.ihe.gazelle.wstester.mockrecord;
/*
This is a duplication of simulator Common net.ihe.gazelle.simulator.message.model.EStandard
*/
public enum EStandard {
HL7V2,
HL7V3,
XDS,
FHIR_XML,
FHIR_JSON,
SVS,
HPD,
OTHER
}
package net.ihe.gazelle.wstester.mockrecord;
public class MessageException extends Exception {
public MessageException(String s) {
super(s);
}
}
package net.ihe.gazelle.wstester.mockrecord;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
......@@ -17,18 +16,21 @@ public class MessageRecorder {
this.passwordDB = passwordDB;
}
public void record(String standardKeyword, String transactionKeyword, String domainKeyword, String simulatedActorKeyword, Message request, Message response) throws SQLException, NotUniqueException {
public void record(EStandard standardKeyword, String transactionKeyword, String domainKeyword, String simulatedActorKeyword, Message request, Message response) throws MessageException {
Date date = new Date();
Timestamp timestamp = new Timestamp(date.getTime());
DBConnection dbConnection = new DBConnection(urlDB, userDB, passwordDB);
try {
Date date = new Date();
Timestamp timestamp = new Timestamp(date.getTime());
DBConnection dbConnection = new DBConnection(urlDB, userDB, passwordDB);
DAOTransaction daoTransaction = new DAOTransaction(dbConnection);
Transaction transaction = new Transaction(standardKeyword, timestamp, domainKeyword, simulatedActorKeyword, transactionKeyword, request, response);
daoTransaction.insertTransactionInstance(transaction);
dbConnection.close();
DAOTransaction daoTransaction = new DAOTransaction(dbConnection);
Transaction transaction = new Transaction(standardKeyword, timestamp, domainKeyword, simulatedActorKeyword, transactionKeyword, request, response);
} catch (Exception e) {
daoTransaction.insertRequest(request);
daoTransaction.insertResponse(response);
daoTransaction.insertTransactionInstance(transaction);
throw new MessageException("An error occurred while inserting the transaction");
}
}
......
......@@ -5,7 +5,7 @@ import java.sql.Timestamp;
class Transaction {
private String standard;
private EStandard standard;
private Timestamp timestamp;
private String domainKeyword;
private String simulatedActorKeyword;
......@@ -14,7 +14,7 @@ class Transaction {
private Message response;
Transaction(String standard, Timestamp timestamp, String domainKeyword, String simulatedActorKeyword, String transactionKeyword, Message request, Message response) {
Transaction(EStandard standard, Timestamp timestamp, String domainKeyword, String simulatedActorKeyword, String transactionKeyword, Message request, Message response) {
this.standard = standard;
this.timestamp = timestamp;
this.domainKeyword = domainKeyword;
......@@ -25,7 +25,7 @@ class Transaction {
}
String getStandard() {
EStandard getStandard() {
return standard;
}
......
package net.ihe.gazelle.wstester.mockrecord;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class DAOModelTest {
@Mock
private DataSource ds;
@Mock
private Connection c;
@Mock
private PreparedStatement stmt;
@Mock
private ResultSet rs;
private DBConnection mockedDbConnection;
@Before
public void setUp() throws SQLException {
assertNotNull(ds);
mockedDbConnection = mock(DBConnection.class);
when(c.prepareStatement(any(String.class))).thenReturn(stmt);
when(mockedDbConnection.getConnection()).thenReturn(c);
when(ds.getConnection()).thenReturn(c);
when(rs.next()).thenReturn(true);
when(rs.isLast()).thenReturn(true);
when(rs.getInt("id")).thenReturn(3);
when(stmt.executeQuery()).thenReturn(rs);
}
@Test
public void getOrCreateDomainIdTest() throws MessageException {
DAOModel daoModel = new DAOModel(mockedDbConnection);
Assert.assertEquals(daoModel.getOrCreateDomainId("CH"), 3);
}
@Test
public void getOrCreateActorIdTest() throws MessageException {
DAOModel daoModel = new DAOModel(mockedDbConnection);
Assert.assertEquals(daoModel.getOrCreateActorId("Provider"), 3);
}
@Test
public void getOrCreateTransactionIdTest() throws MessageException {
DAOModel daoModel = new DAOModel(mockedDbConnection);
Assert.assertEquals(daoModel.getOrCreateTransactionId("ADR"), 3);
}
@Test
public void getSelectIdTest() throws MessageException {
DAOModel daoModel = new DAOModel(mockedDbConnection);
Assert.assertEquals(daoModel.getSelectId("MOCK SELECT REQUEST", "TEST"), 3);
}
@Test
public void insertNewTfTest() throws MessageException {
DAOModel daoModel = new DAOModel(mockedDbConnection);
Assert.assertEquals(daoModel.insertNewTf("MOCK INSERT REQUEST", "TEST"), 3);
}
@Test(expected = MessageException.class)
public void getSelectIdErrorTest() throws SQLException, MessageException {
when(rs.isLast()).thenReturn(false);
DAOModel daoModel = new DAOModel(mockedDbConnection);
daoModel.getSelectId("MOCK SELECT REQUEST", "TEST");
}
}
\ No newline at end of file
package net.ihe.gazelle.wstester.mockrecord;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import javax.sql.DataSource;
import java.sql.*;
import java.util.Date;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class DAOTransactionTest {
@Mock
private DataSource ds;
@Mock
private Connection c;
@Mock
private PreparedStatement stmt;
@Mock
private ResultSet rs;
private DBConnection mockedDbConnection;
@Before
public void setUp() throws SQLException {
assertNotNull(ds);
mockedDbConnection = mock(DBConnection.class);
when(c.prepareStatement(any(String.class))).thenReturn(stmt);
when(mockedDbConnection.getConnection()).thenReturn(c);
when(ds.getConnection()).thenReturn(c);
when(rs.next()).thenReturn(true);
when(rs.isLast()).thenReturn(true);
when(stmt.executeQuery()).thenReturn(rs);
}
@Test
public void insertRequestTest() throws MessageException, SQLException {
DAOTransaction daoTransaction = new DAOTransaction(mockedDbConnection);
Message request = new Message("Consumer", "127.0.0.1", "SubmitObjectsRequest", "Consumer", null);
daoTransaction.insertRequest(request);
Assert.assertEquals(rs.isClosed(), false);
}
@Test
public void insertResponseTest() throws MessageException, SQLException {
DAOTransaction daoTransaction = new DAOTransaction(mockedDbConnection);
Message response = new Message("Provider", "soapui mock", "RegistryResponse", "Provider", null);
daoTransaction.insertResponse(response);
Assert.assertEquals(rs.isClosed(), false);
}
@Test
public void insertTransactionInstanceTest() throws MessageException, SQLException {
Date date = new Date();
Timestamp timestamp = new Timestamp(date.getTime());
Message request = new Message("Consumer", "127.0.0.1", "SubmitObjectsRequest", "Consumer", null);
Message response = new Message("Provider", "soapui mock", "RegistryResponse", "Provider", null);
Transaction transaction = new Transaction(EStandard.XDS, timestamp, "UNKNOWN", "simulatedActorKeyword", "ITI", request, response);
DAOTransaction daoTransaction = new DAOTransaction(mockedDbConnection);
daoTransaction.insertTransactionInstance(transaction);
Assert.assertEquals(rs.isClosed(), false);
}
}
\ No newline at end of file
......@@ -2,41 +2,50 @@ package net.ihe.gazelle.wstester.mockrecord;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import java.sql.Timestamp;
import java.util.Date;
@RunWith(MockitoJUnitRunner.class)
public class MessageRecorderTest {
@Test
public void recordMessageTest() throws Exception {
DBConnection dbConnection = new DBConnection("jdbc:postgresql://localhost:5432/gazelle-webservice-tester", "gazelle", "gazelle");
DAOTransaction daoTransaction = new DAOTransaction(dbConnection);