Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM openjdk:17-alpine as builder

WORKDIR app
COPY . .
RUN ./gradlew assemble

FROM openjdk:17-alpine

WORKDIR app
COPY --from=builder /app/build/libs/candlesticks-1.1.1.jar .
EXPOSE 9000
CMD ["java", "-jar", "/app/candlesticks-1.1.1.jar"]
9 changes: 2 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
test:
docker-compose up -d --wait
./gradlew flywayMigrate
./gradlew clean test
docker compose down
docker-compose up postgres tests

run:
docker-compose up -d --wait
./gradlew flywayMigrate
./gradlew run
docker-compose up java postgres candlesticks
17 changes: 17 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ object DependencyVersions {
const val hikariCP = "4.0.3"
const val typesafeConfig = "1.4.2"
const val exposed = "0.38.2"
const val flyway = "8.5.13"
}

dependencies {
Expand All @@ -62,6 +63,7 @@ dependencies {

implementation("org.postgresql:postgresql:${DependencyVersions.postgres}")
implementation("com.zaxxer:HikariCP:${DependencyVersions.hikariCP}")
implementation("org.flywaydb:flyway-core:${DependencyVersions.flyway}")

implementation("org.jetbrains.exposed:exposed-core:${DependencyVersions.exposed}")
implementation("org.jetbrains.exposed:exposed-dao:${DependencyVersions.exposed}")
Expand All @@ -77,3 +79,18 @@ tasks.test {
exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
}
}

tasks.withType<Jar> {
manifest {
attributes["Main-Class"] = "MainKt"
}

val dependencies = configurations
.runtimeClasspath
.get()
.map(::zipTree)

from(dependencies)
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}

29 changes: 29 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,35 @@ services:
interval: 10s
timeout: 5s
retries: 5
start_period: 30s
candlesticks:
depends_on:
- postgres
- java
container_name: candlesticks
restart: on-failure
build: .
ports:
- "8080:8080"
# networks:
# - postgres
network_mode: host
environment:
POSTGRES_USER: ${POSTGRES_USER:-postgres}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres}
DATABASE_HOST: ${DATABASE_HOST:-localhost}
tests:
depends_on:
- postgres
container_name: tests
network_mode: host
environment:
POSTGRES_USER: ${POSTGRES_USER:-postgres}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres}
DATABASE_HOST: ${DATABASE_HOST:-localhost}
build:
context: .
dockerfile: ./src/test/Dockerfile-tests

networks:
postgres:
Expand Down
7 changes: 6 additions & 1 deletion src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.typesafe.config.Config
import com.typesafe.config.ConfigFactory
import configs.DataSourceConfig
import org.flywaydb.core.Flyway
import org.jetbrains.exposed.sql.Database
import repositories.InstrumentRepository
import repositories.InstrumentRepositoryImpl
Expand All @@ -30,8 +31,12 @@ fun main() {
val quoteStream = QuoteStream()

val config = ConfigFactory.load()
val dataSource = DataSourceConfig.fromConfig(config).toHikariDataSource()
val dataSourceConfig = DataSourceConfig.fromConfig(config)

val flyway = Flyway.configure().dataSource(dataSourceConfig.url, dataSourceConfig.user, dataSourceConfig.password).load()
flyway.migrate()

val dataSource by lazy{ dataSourceConfig.toHikariDataSource() }
Database.connect(dataSource)

instrumentStream.connect { event ->
Expand Down
3 changes: 3 additions & 0 deletions src/test/Dockerfile-tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM gradle:7.2-jdk-alpine
COPY . .
ENTRYPOINT ["gradle", "test"]
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@ package integration
import Quote
import com.typesafe.config.ConfigFactory
import configs.DataSourceConfig
import org.flywaydb.core.Flyway
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.deleteAll
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.transaction
import repositories.InstrumentTable
import repositories.QuoteTable

object Utils {
private val config = ConfigFactory.load()
private val dataSource = DataSourceConfig.fromConfig(config).toHikariDataSource()
object DBUtils {
val config = ConfigFactory.load()
val dataSourceConfig = DataSourceConfig.fromConfig(config)

val flyway = Flyway.configure().dataSource(dataSourceConfig.url, dataSourceConfig.user, dataSourceConfig.password).load()


init {
flyway.migrate()
val dataSource by lazy{ dataSourceConfig.toHikariDataSource() }
Database.connect(dataSource)
}

Expand Down
7 changes: 1 addition & 6 deletions src/test/kotlin/integration/InstrumentRepositoryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@ package integration

import Generators.generateISIN
import Instrument
import Quote
import com.typesafe.config.ConfigFactory
import configs.DataSourceConfig
import org.jetbrains.exposed.sql.Database
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import repositories.InstrumentRepositoryImpl
import java.math.BigDecimal
import kotlin.test.assertEquals

class InstrumentRepositoryTest {
private val instrumentRepository = InstrumentRepositoryImpl()

@BeforeEach
fun beforeEach() {
Utils.clearDatabase()
DBUtils.clearDatabase()
}

@Test
Expand Down
14 changes: 4 additions & 10 deletions src/test/kotlin/integration/QuoteRepositoryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,16 @@ import Candlestick
import Generators.generateAndPopulateRandomInstrumentsWithQuotes
import Generators.generateISIN
import Generators.generateInstrument
import Instrument
import Quote
import com.typesafe.config.ConfigFactory
import configs.DataSourceConfig
import org.jetbrains.exposed.sql.Database
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import repositories.InstrumentRepositoryImpl
import repositories.QuoteRepository
import repositories.QuoteRepositoryImpl
import java.math.BigDecimal
import java.time.Clock
import java.time.Instant
import java.time.ZoneId
import java.time.temporal.ChronoUnit
import kotlin.random.Random
import kotlin.test.assertContains
import kotlin.test.assertEquals
import kotlin.test.assertFails
Expand All @@ -30,7 +24,7 @@ class QuoteRepositoryTest {

@BeforeEach
fun beforeEach() {
Utils.clearDatabase()
DBUtils.clearDatabase()
}

@Test
Expand All @@ -46,7 +40,7 @@ class QuoteRepositoryTest {
assertEquals(1, quoteRepository.createQuote(q, Instant.now()))
}

val insertedQuotes = Utils.getAllQuotes().map { it.copy(price = it.price.stripTrailingZeros()) }
val insertedQuotes = DBUtils.getAllQuotes().map { it.copy(price = it.price.stripTrailingZeros()) }
assertEquals(3, insertedQuotes.size)

quotes.map { q ->
Expand All @@ -67,10 +61,10 @@ class QuoteRepositoryTest {
val quote = Quote(instrument.isin, BigDecimal("123.12"))

assertEquals(1, quoteRepository.createQuote(quote, Instant.now()))
assertEquals(1, Utils.getAllQuotes().size)
assertEquals(1, DBUtils.getAllQuotes().size)

instrumentRepository.deleteInstrument(instrument.isin)
assertEquals(0, Utils.getAllQuotes().size)
assertEquals(0, DBUtils.getAllQuotes().size)
}

@Test
Expand Down