1

Split project into sub-projects

This commit is contained in:
2018-05-04 17:23:08 +03:00
parent a3e6f119e7
commit fb4baec57f
28 changed files with 119 additions and 42 deletions

View File

@@ -0,0 +1,40 @@
import com.devsoap.dbt.DBTModule
import com.devsoap.dbt.config.DBTConfig
import org.flywaydb.core.Flyway
import org.h2.jdbcx.JdbcDataSource
import ratpack.service.Service
import ratpack.service.StartEvent
import ratpack.session.SessionModule
import javax.sql.DataSource
import static ratpack.groovy.Groovy.ratpack
ratpack {
serverConfig {
sysProps()
require('/dbt', DBTConfig)
}
bindings {
module SessionModule
module (DBTModule) {
it.ledger.remoteUrl = 'http://localhost:8888/ledger'
it.executor.remoteUrl = 'http://localhost:8888/executor'
}
bindInstance(DataSource, new JdbcDataSource(url: 'jdbc:h2:mem:dbtdb;DB_CLOSE_DELAY=-1', user: ''))
bind DatabaseService
}
}
class DatabaseService implements Service {
@Override
void onStart(StartEvent event){
new Flyway(dataSource: event.registry.get(DataSource)).migrate()
}
}

View File

@@ -0,0 +1,31 @@
package com.devsoap.dbt
import ratpack.groovy.test.GroovyRatpackMainApplicationUnderTest
import ratpack.impose.Impositions
import ratpack.impose.ServerConfigImposition
import ratpack.server.RatpackServer
class CustomPortMainApplicationUnderTest extends GroovyRatpackMainApplicationUnderTest {
private final int port
CustomPortMainApplicationUnderTest(int port) {
this.port = port
}
@Override
protected Impositions createImpositions() throws Exception {
Impositions.of {
it.add(ServerConfigImposition.of {
it.port(port)
})
}
}
@Override
protected RatpackServer createServer() throws Exception {
System.setProperty('ratpack.dbt.ledger.remoteUrl', "http://localhost:$port/ledger")
System.setProperty('ratpack.dbt.executor.remoteUrl', "http://localhost:$port/executor")
return super.createServer()
}
}

View File

@@ -0,0 +1,35 @@
package com.devsoap.dbt
import com.devsoap.dbt.data.BlockTransaction
import com.fasterxml.jackson.databind.ObjectMapper
import spock.lang.AutoCleanup
import spock.lang.Specification
class ExecutorSpec extends Specification {
def mapper = new ObjectMapper()
def PATH = 'executor'
@AutoCleanup
def aut = new CustomPortMainApplicationUnderTest(8888)
void 'transaction sent to executor'() {
setup:
def transaction = new BlockTransaction()
transaction.execute("SELECT * FROM LOGS")
transaction.end()
when:
String json = aut.httpClient.requestSpec{ spec ->
spec.body.text(mapper.writeValueAsString(transaction))
}.postText(PATH)
def recievedTransaction = mapper.readValue(json, BlockTransaction)
then:
recievedTransaction.id == transaction.id
recievedTransaction.completed
recievedTransaction.executed
}
}

View File

@@ -0,0 +1,90 @@
package com.devsoap.dbt
import com.devsoap.dbt.data.BlockTransaction
import com.fasterxml.jackson.databind.ObjectMapper
import groovy.json.JsonSlurper
import ratpack.groovy.test.GroovyRatpackMainApplicationUnderTest
import ratpack.impose.Imposition
import ratpack.impose.Impositions
import ratpack.impose.ImpositionsSpec
import ratpack.impose.ServerConfigImposition
import ratpack.server.RatpackServer
import spock.lang.AutoCleanup
import spock.lang.Shared
import spock.lang.Specification
class LedgerSpec extends Specification {
def mapper = new ObjectMapper()
def PATH = 'ledger'
@AutoCleanup
GroovyRatpackMainApplicationUnderTest aut = new CustomPortMainApplicationUnderTest(8888)
void 'transaction sent to ledger'() {
setup:
def transaction = new BlockTransaction()
transaction.execute("SELECT * FROM LOGS")
when:
String json = aut.httpClient.requestSpec{ spec ->
spec.body.text(mapper.writeValueAsString(transaction))
}.postText(PATH)
def recievedTransaction = mapper.readValue(json, BlockTransaction)
then:
recievedTransaction.id == transaction.id
!recievedTransaction.completed
}
void 'completed transaction marked as completed'() {
setup:
def transaction = new BlockTransaction()
transaction.execute("SELECT * FROM LOGS")
transaction.end()
when:
String json = aut.httpClient.requestSpec{ spec ->
spec.body.text(mapper.writeValueAsString(transaction))
}.postText(PATH)
def recievedTransaction = mapper.readValue(json, BlockTransaction)
then:
recievedTransaction.id == transaction.id
recievedTransaction.completed
}
void 'completed transaction sent to executor from ledger'() {
setup:
def transaction = new BlockTransaction()
transaction.execute("SELECT * FROM LOGS")
transaction.end()
when:
def response = mapper.readValue(aut.httpClient.requestSpec { spec ->
spec.body.text(mapper.writeValueAsString(transaction))
}.post(PATH).body.text, BlockTransaction)
then:
response.id == transaction.id
response.executed == true
response.rolledback == false
response.completed == true
}
void 'result is attached to block and executed in order'() {
setup:
def transaction = new BlockTransaction()
transaction.execute("INSERT INTO LOGS(LOG_ID,LOG_VALUE) VALUES (1, 'HELLO')")
transaction.execute("SELECT * FROM LOGS")
transaction.end()
when:
String json = aut.httpClient.requestSpec{ spec ->
spec.body.text(mapper.writeValueAsString(transaction))
}.postText(PATH)
def recievedTransaction = mapper.readValue(json, BlockTransaction)
def result = recievedTransaction.queries[1].result
then:
recievedTransaction.id == transaction.id
recievedTransaction.queries.first().result == null // insert query has no result
result['LOG_ID'].first() == 1
result['LOG_VALUE'].first() == 'HELLO'
}
}

View File

@@ -0,0 +1,32 @@
package com.devsoap.dbt
import com.devsoap.dbt.data.BlockTransaction
import com.fasterxml.jackson.databind.ObjectMapper
import ratpack.groovy.test.GroovyRatpackMainApplicationUnderTest
import spock.lang.AutoCleanup
import spock.lang.Specification
class TransactionManagementServiceSpec extends Specification {
def mapper = new ObjectMapper()
def PATH = 'ledger'
@AutoCleanup
GroovyRatpackMainApplicationUnderTest aut = new CustomPortMainApplicationUnderTest(8888)
void 'transaction sent to ledger'() {
setup:
def transaction = new BlockTransaction()
transaction.execute("SELECT * FROM LOGS")
when:
String json = aut.httpClient.requestSpec{ spec ->
spec.body.text(mapper.writeValueAsString(transaction))
}.postText(PATH)
def recievedTransaction = mapper.readValue(json, BlockTransaction)
then:
recievedTransaction.id == transaction.id
!recievedTransaction.completed
}
}

View File

@@ -0,0 +1,4 @@
create table LOGS (
LOG_ID int not null,
LOG_VALUE varchar(100) not null
);