Skip to content

Commit

Permalink
DAO Test Coverage (#750)
Browse files Browse the repository at this point in the history
* fetchmetadata test cases

* refacgtor test file

* refactoring some more

* requested suggestions

* deleted isValidData var

* Added mockDriver and mockConn to setup()

* Added mockPreparedStatement and mockResultSet to setup()

* refactoring of spacing
  • Loading branch information
jorg3lopez authored Jan 4, 2024
1 parent a9944e3 commit 1e7f55d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ public synchronized PartnerMetadata fetchMetadata(String receivedSubmissionId)

ResultSet result = statement.executeQuery();

var hasValidData = result.next();
if (!hasValidData) {
if (!result.next()) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,28 @@ import java.sql.SQLException

class PostgresDaoTest extends Specification {

private def mockDriver
private def mockConn
private def mockPreparedStatement
private def mockResultSet

def setup() {
TestApplicationContext.reset()
TestApplicationContext.init()

mockDriver = Mock(SqlDriverManager)
mockConn = Mock(Connection)
mockPreparedStatement = Mock(PreparedStatement)
mockResultSet = Mock(ResultSet)
def mockAzureClient = Mock(AzureClient)
mockAzureClient.getScopedToken(_ as String) >> "DogCow password"
TestApplicationContext.register(AzureClient, mockAzureClient)

TestApplicationContext.register(AzureClient, mockAzureClient)
TestApplicationContext.register(PostgresDao, PostgresDao.getInstance())
}

def "connect happy path works"(){
given:
def mockDriver = Mock(SqlDriverManager)
Connection mockConn
mockDriver.getConnection(_ as String, _ as Properties) >> {mockConn}

TestApplicationContext.register(SqlDriverManager, mockDriver)
Expand All @@ -45,7 +52,6 @@ class PostgresDaoTest extends Specification {

def "connect unhappy path throws exception"() {
given:
def mockDriver = Mock(SqlDriverManager)
mockDriver.getConnection(_ as String, _ as Properties) >> {throw new SQLException()}
TestApplicationContext.register(SqlDriverManager, mockDriver)
TestApplicationContext.injectRegisteredImplementations()
Expand All @@ -59,34 +65,25 @@ class PostgresDaoTest extends Specification {

def "upsertMetadata works"() {
given:
def upsertMockDriver = Mock(SqlDriverManager)
Connection upsertMockConn = Mock(Connection)
PreparedStatement upsertMockStatement = Mock(PreparedStatement)
mockDriver.getConnection(_ as String, _ as Properties) >> mockConn
mockConn.prepareStatement(_ as String) >> mockPreparedStatement

upsertMockDriver.getConnection(_ as String, _ as Properties) >> upsertMockConn
upsertMockConn.prepareStatement(_ as String) >> upsertMockStatement

TestApplicationContext.register(SqlDriverManager, upsertMockDriver)
TestApplicationContext.register(SqlDriverManager, mockDriver)
TestApplicationContext.injectRegisteredImplementations()

when:
PostgresDao.getInstance().upsertMetadata("mock_id", "mock_sender", "mock_receiver", "mock_hash", Instant.now())

then:
1 * upsertMockStatement.executeUpdate()
1 * mockPreparedStatement.executeUpdate()
}


def "upsertMetadata unhappy path throws exception"() {
given:
def upsertMockDriver = Mock(SqlDriverManager)
Connection upsertMockConn = Mock(Connection)

upsertMockDriver.getConnection(_ as String, _ as Properties) >> upsertMockConn
upsertMockConn.prepareStatement(_ as String) >> { throw new SQLException() }
mockDriver.getConnection(_ as String, _ as Properties) >> mockConn
mockConn.prepareStatement(_ as String) >> { throw new SQLException() }


TestApplicationContext.register(SqlDriverManager, upsertMockDriver)
TestApplicationContext.register(SqlDriverManager, mockDriver)
TestApplicationContext.injectRegisteredImplementations()

when:
Expand All @@ -98,18 +95,13 @@ class PostgresDaoTest extends Specification {

def "select metadata retrieves data"(){
given:
def selectMockDriver = Mock(SqlDriverManager)
Connection selectMockConn = Mock(Connection)
PreparedStatement selectPreparedStatement = Mock(PreparedStatement)
ResultSet selectResultSet = Mock(ResultSet)

selectMockDriver.getConnection(_ as String, _ as Properties) >> selectMockConn
selectMockConn.prepareStatement(_ as String) >> selectPreparedStatement
selectPreparedStatement.executeQuery() >> selectResultSet
selectResultSet.next() >> true
selectResultSet.getTimestamp(_ as String) >> Timestamp.from(Instant.now())

TestApplicationContext.register(SqlDriverManager, selectMockDriver)
mockDriver.getConnection(_ as String, _ as Properties) >> mockConn
mockConn.prepareStatement(_ as String) >> mockPreparedStatement
mockPreparedStatement.executeQuery() >> mockResultSet
mockResultSet.next() >> true
mockResultSet.getTimestamp(_ as String) >> Timestamp.from(Instant.now())

TestApplicationContext.register(SqlDriverManager, mockDriver)
TestApplicationContext.injectRegisteredImplementations()

when:
Expand All @@ -121,14 +113,10 @@ class PostgresDaoTest extends Specification {

def "fetchMetadata unhappy path throws exception"() {
given:
def selectMockDriver = Mock(SqlDriverManager)
Connection selectMockConn = Mock(Connection)

selectMockDriver.getConnection(_ as String, _ as Properties) >> selectMockConn
selectMockConn.prepareStatement(_ as String) >> { throw new SQLException() }
mockDriver.getConnection(_ as String, _ as Properties) >> mockConn
mockConn.prepareStatement(_ as String) >> { throw new SQLException() }


TestApplicationContext.register(SqlDriverManager, selectMockDriver)
TestApplicationContext.register(SqlDriverManager, mockDriver)
TestApplicationContext.injectRegisteredImplementations()

when:
Expand All @@ -137,4 +125,51 @@ class PostgresDaoTest extends Specification {
then:
thrown(SQLException)
}

def "fetchMetadata returns null when rows do not exist"() {
given:
def expected = null

mockDriver.getConnection(_ as String, _ as Properties) >> mockConn
mockConn.prepareStatement(_ as String) >> mockPreparedStatement
mockResultSet.next() >> false
mockPreparedStatement.executeQuery() >> mockResultSet

TestApplicationContext.register(SqlDriverManager, mockDriver)
TestApplicationContext.injectRegisteredImplementations()

when:
def actual = PostgresDao.getInstance().fetchMetadata("mock_lookup")

then:
actual == expected
}

def "fetchMetadata returns partnermetadata when rows exist"() {
given:
def messageId = "12345"
def receiver = "DogCow"
Timestamp timestampForMock = Timestamp.from(Instant.parse("2024-01-03T15:45:33.30Z"))
Instant timeReceived = timestampForMock.toInstant()
def hash = receiver.hashCode().toString()
def expected = new PartnerMetadata(messageId, receiver, timeReceived, hash)

mockDriver.getConnection(_ as String, _ as Properties) >> mockConn
mockConn.prepareStatement(_ as String) >> mockPreparedStatement
mockResultSet.next() >> true
mockResultSet.getString("message_id") >> messageId
mockResultSet.getString("receiver") >> receiver
mockResultSet.getTimestamp("time_received") >> timestampForMock
mockResultSet.getString("hash_of_order") >> hash
mockPreparedStatement.executeQuery() >> mockResultSet

TestApplicationContext.register(SqlDriverManager, mockDriver)
TestApplicationContext.injectRegisteredImplementations()

when:
def actual = PostgresDao.getInstance().fetchMetadata("mock_lookup")

then:
actual == expected
}
}

0 comments on commit 1e7f55d

Please sign in to comment.