-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added postgres initializer to prod config
- Loading branch information
kamil.jedrzejuk
committed
Oct 8, 2024
1 parent
aa83eb0
commit d60fceb
Showing
8 changed files
with
100 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
### Step 1: Generate X-Request-Id (UUID for the scenario) | ||
@requestId = {{uuid}} // Automatically generates a valid UUID for X-Request-Id | ||
|
||
### Step 2: Create a new account (capture the accountId from the response) | ||
POST http://localhost:8090/api/accounts | ||
Content-Type: application/json | ||
X-Request-Id: 550e8400-e29b-41d4-a716-446655440000 // Use the generated requestId | ||
|
||
{ | ||
"owner": "John Doe", | ||
"initialBalance": "1000.00" | ||
} | ||
|
||
### Capture the accountId from the response | ||
> {% | ||
client.test("Status Code is 201", function() { | ||
client.assert(response.status === 201, "Expected status code 201, but got " + response.status); | ||
}); | ||
|
||
var jsonData = JSON.parse(response.body); | ||
client.global.set("accountId", jsonData.id); // Save the accountId globally for use in the next request | ||
%} | ||
|
||
|
||
### Step 3: Exchange PLN to USD (use captured accountId) | ||
PUT http://localhost:8090/api/accounts/exchange-pln-to-usd | ||
Content-Type: application/json | ||
X-Request-Id: 550e8400-e29b-41d4-a716-446655449999 | ||
|
||
{ | ||
"accountId": "f0cf47c7-24d8-40ff-9a14-5c935596f7e7", | ||
"amount": "100.00" | ||
} | ||
|
||
> {% | ||
client.test("Status Code is 200", function() { | ||
client.assert(response.status === 200, "Expected status code 200, but got " + response.status); | ||
}); | ||
%} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 0 additions & 22 deletions
22
src/main/kotlin/camilyed/github/io/currencyexchangeapi/domain/AccountOperationRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,10 @@ | ||
package camilyed.github.io.currencyexchangeapi.domain | ||
|
||
import org.springframework.context.annotation.Profile | ||
import org.springframework.stereotype.Component | ||
import java.util.UUID | ||
import java.util.concurrent.ConcurrentHashMap | ||
|
||
interface AccountOperationRepository { | ||
|
||
fun findAccountIdBy(operationId: UUID): UUID? | ||
|
||
fun save(events: List<AccountEvent>) | ||
} | ||
|
||
@Component | ||
@Profile("!test") | ||
class InMemoryAccountOperationRepository : AccountOperationRepository { | ||
|
||
private val operations = ConcurrentHashMap<UUID, UUID>() | ||
private val events = mutableListOf<AccountEvent>() | ||
|
||
override fun findAccountIdBy(operationId: UUID): UUID? { | ||
return operations[operationId] | ||
} | ||
|
||
override fun save(events: List<AccountEvent>) { | ||
events.forEach { event -> | ||
this.events.add(event) | ||
operations[event.operationId] = event.accountId | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/kotlin/camilyed/github/io/currencyexchangeapi/infrastructure/PostgresInitializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package camilyed.github.io.currencyexchangeapi.infrastructure | ||
|
||
import mu.KotlinLogging | ||
import org.springframework.beans.factory.DisposableBean | ||
import org.springframework.context.ApplicationContextInitializer | ||
import org.springframework.context.ConfigurableApplicationContext | ||
import org.springframework.context.annotation.Profile | ||
import org.testcontainers.containers.PostgreSQLContainer | ||
|
||
@Profile("!test") | ||
class PostgresInitializer : ApplicationContextInitializer<ConfigurableApplicationContext>, DisposableBean { | ||
override fun initialize(applicationContext: ConfigurableApplicationContext) { | ||
log.info("Overriding system properties") | ||
overrideSystemProperties() | ||
} | ||
|
||
override fun destroy() { | ||
if (pg.isRunning) { | ||
log.info("Stopping PostgreSQL container...") | ||
pg.stop() | ||
} | ||
} | ||
|
||
private fun overrideSystemProperties() { | ||
System.setProperty("spring.datasource.url", pg.jdbcUrl) | ||
System.setProperty("spring.datasource.username", pg.username) | ||
System.setProperty("spring.datasource.password", pg.password) | ||
} | ||
|
||
private companion object { | ||
private val log = KotlinLogging.logger {} | ||
private val pg: PostgreSQLContainer<*> = | ||
PostgreSQLContainer("postgres:13.4-alpine") | ||
.apply { | ||
withReuse(true) | ||
withDatabaseName("test_db") | ||
withUsername("test_user") | ||
withPassword("test_password") | ||
} | ||
|
||
init { | ||
pg.withInitScript("init.sql") | ||
pg.start() | ||
log.info("Postgres started") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters