Skip to content

Instantly share code, notes, and snippets.

@iundarigun
iundarigun / EmployeeWithDocker.kt
Created April 18, 2020 20:22
TestWithProfileDocker
@Tag("system")
@Testcontainers
@ActiveProfiles("docker")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class EmployeesWithDockerEndpoint {
// Tests
}
@iundarigun
iundarigun / DockerConfig.kt
Created April 18, 2020 20:15
DockerConfigurationClass
@Configuration
@Profile("docker")
class DockerConfig {
companion object {
val mock: GenericContainer<Nothing> = GenericContainer<Nothing>("postgres").also {
it.withExposedPorts(5432)
it.portBindings.add("55432:5432")
it.addEnv("POSTGRES_USER", "test")
it.addEnv("POSTGRES_PASSWORD", "test")
it.withCopyFileToContainer(
@iundarigun
iundarigun / EmployeeWithDocker.kt
Created April 18, 2020 20:04
EmployeeDockerIntegration
@Tag("integration")
@Testcontainers
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class EmployeesWithDockerEndpoint {
companion object {
@Container
val mock: GenericContainer<Nothing> = GenericContainer<Nothing>("iundarigun/mock-ws").also {
it.withExposedPorts(1899)
it.portBindings.add("18899:1899")
@Testcontainers
@Tag("unit")
class DockerContainerTest {
private val log = LoggerFactory.getLogger(javaClass)
companion object {
@Container
val mock: GenericContainer<Nothing> = GenericContainer<Nothing>("iundarigun/mock-ws").also {
it.withExposedPorts(1899)
@iundarigun
iundarigun / docker-compose.yml
Created March 29, 2020 19:31
docker-compose rabbit cluster
version: '3'
services:
rabbitmq1:
image: rabbitmq:3-management
container_name: rabbitmq1
hostname: rabbitmq1
network_mode: "bridge"
environment:
- RABBITMQ_ERLANG_COOKIE=docker-compose-rabbitmq-cluster
ports:
@Configuration
class RedisConfiguration(
@Value("\${spring.redis.host:}")
private val masterHost: String,
@Value("\${spring.redis.port:}")
private val masterPort: Int,
@Value("\${spring.redis.password:}")
private val masterPassword: String,
@Value("\${spring.redis-slave.host:}")
private val slaveHost: String,
@iundarigun
iundarigun / RedisRepository.kt
Created February 16, 2020 22:10
StringRedisRepository
@Repository
class RedisRepository(
private val redisMasterTemplate: StringRedisTemplate,
private val redisSlaveTemplate: StringRedisTemplate
) {
private val suffixKey = "devcave:masterslave:"
fun setValue(key: String, value: String) {
redisMasterTemplate.opsForValue().set(suffixKey + key, value)
}
@iundarigun
iundarigun / ApplicationControllerTest.kt
Created February 10, 2020 16:25
ApplicationControllerTest
@Import(RedisTestConfiguration::class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ApplicationControllerTest {
// Some variables
@Test
fun `findAll with some instance expired`() {
val app = "APP1"
val instance1 = InstanceDefinition(
UUID.randomUUID().toString(),
@iundarigun
iundarigun / RedisTestConfiguration.kt
Created February 10, 2020 16:24
RedisTestConfiguration
@TestConfiguration
class RedisTestConfiguration(
@Value("\${spring.redis.port}")
private val redisPort: Int
) {
private val redisServer: RedisServer = RedisServer(redisPort)
@PostConstruct
fun postConstruct() {
redisServer.start()
@iundarigun
iundarigun / ApplicationRepository.kt
Created February 10, 2020 10:46
ApplicationRepositoryLua
fun findInstancesByApplicationIdLua(applicationId: String): List<String> {
val appKey = "$applicationPrefixKey:$applicationId"
val instanceKey = "$instancePrefixKey:$applicationId"
val redisScript = RedisScript.of(
javaClass.getResource("/scripts/validation.lua").readText(),
List::class.java)
return redisTemplate.execute(redisScript, mutableListOf(appKey, instanceKey)).let {
it as List<String>
}
}