Skip to content

Commit

Permalink
refactor dynamodb test into contract test
Browse files Browse the repository at this point in the history
  • Loading branch information
mrFlick72 committed Oct 25, 2024
1 parent 9dec67d commit 9860ceb
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.vauthenticator.server.password.adapter.jdbc

import com.vauthenticator.server.password.domain.Password
import com.vauthenticator.server.password.domain.PasswordHistoryRepository
import org.springframework.jdbc.core.JdbcTemplate
import software.amazon.awssdk.services.dynamodb.model.AttributeValue
import java.time.Clock
import java.time.LocalDateTime
import java.time.ZoneOffset

class JdbcPasswordHistoryRepository(
private val historyEvaluationLimit: Int,
private val maxHistoryAllowedSize: Int,
private val clock: Clock,
private val dynamoPasswordHistoryTableName: String,
private val jdbcTemplate: JdbcTemplate
) : PasswordHistoryRepository {
override fun store(userName: String, password: Password) {
// dynamoDbClient.putItem(
// PutItemRequest.builder()
// .tableName(dynamoPasswordHistoryTableName)
// .item(
// mapOf(
// "user_name" to userName.asDynamoAttribute(),
// "created_at" to createdAt().asDynamoAttribute(),
// "password" to password.content.asDynamoAttribute()
// )
// )
// .build()
// )
}

private fun createdAt() =
LocalDateTime.now(clock)
.toInstant(ZoneOffset.UTC)
.toEpochMilli()

override fun load(userName: String): List<Password> {
/* val items = dynamoDbClient.query(
QueryRequest.builder()
.tableName(dynamoPasswordHistoryTableName)
.scanIndexForward(false)
.keyConditionExpression("user_name=:email")
.expressionAttributeValues(mapOf(":email" to userName.asDynamoAttribute())).build()
).items()
val allowedPassword = items.take(historyEvaluationLimit)
deleteUselessPasswordHistory(items)
return allowedPassword.map { Password(it.valueAsStringFor("password")) }*/
TODO()
}

private fun deleteUselessPasswordHistory(itemsInTheHistory: List<Map<String, AttributeValue>>) {
val leftoverSize = itemsInTheHistory.size - maxHistoryAllowedSize
if (leftoverSize > 0) {
// itemsInTheHistory.takeLast(leftoverSize)
// .forEach { itemToDelete ->
// dynamoDbClient.deleteItem(
// DeleteItemRequest.builder()
// .tableName(dynamoPasswordHistoryTableName)
// .key(itemToDelete.filterKeys { it != "password" })
// .build()
// )
// }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.vauthenticator.server.password.adapter

import com.vauthenticator.server.password.domain.Password
import com.vauthenticator.server.password.domain.PasswordHistoryRepository
import io.mockk.junit5.MockKExtension
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

private const val A_USERNAME = "A_USERNAME"

abstract class AbstractPasswordHistoryRepositoryTest {

lateinit var uut: PasswordHistoryRepository

abstract fun initPasswordHistoryRepository(): PasswordHistoryRepository
abstract fun resetDatabase()
abstract fun loadActualDynamoSizeFor(userName: String): List<Map<String, Any>>

@BeforeEach
fun setUp() {
uut = initPasswordHistoryRepository()
resetDatabase()
}

@Test
fun `when store a new password in an empty the history`() {

uut.store(A_USERNAME, Password("A_PASSWORD"))
val history = uut.load(A_USERNAME)

Assertions.assertEquals(1, history.size)
}

@Test
fun `when store a new password in an non empty the history`() {
uut.store(A_USERNAME, Password("A_PASSWORD 3"))
uut.store(A_USERNAME, Password("A_PASSWORD 1"))
uut.store(A_USERNAME, Password("A_PASSWORD 2"))
uut.store(A_USERNAME, Password("A_PASSWORD 10"))
uut.store(A_USERNAME, Password("A_PASSWORD 12"))
uut.store(A_USERNAME, Password("A_PASSWORD 22"))
uut.store(A_USERNAME, Password("A_PASSWORD 32"))
uut.store(A_USERNAME, Password("A_PASSWORD 42"))
val history = uut.load(A_USERNAME)

val expected = listOf(
Password("A_PASSWORD 42"),
Password("A_PASSWORD 32"),
)
Assertions.assertEquals(expected, history)
Assertions.assertEquals(3, loadActualDynamoSizeFor(A_USERNAME).size)
}

@Test
fun `when store a new password in an non empty the history with less item then requested as limit`() {
uut.store(A_USERNAME, Password("A_PASSWORD 3"))
val history = uut.load(A_USERNAME)

val expected = listOf(
Password("A_PASSWORD 3"),
)
Assertions.assertEquals(expected, history)
Assertions.assertEquals(1, loadActualDynamoSizeFor(A_USERNAME).size)
}

}
Original file line number Diff line number Diff line change
@@ -1,83 +1,37 @@
package com.vauthenticator.server.password.adapter.dynamodb

import com.vauthenticator.server.extentions.asDynamoAttribute
import com.vauthenticator.server.password.domain.Password
import com.vauthenticator.server.password.adapter.AbstractPasswordHistoryRepositoryTest
import com.vauthenticator.server.password.domain.PasswordHistoryRepository
import com.vauthenticator.server.support.DynamoDbUtils
import com.vauthenticator.server.support.DynamoDbUtils.dynamoDbClient
import com.vauthenticator.server.support.DynamoDbUtils.dynamoPasswordHistoryTableName
import io.mockk.junit5.MockKExtension
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import software.amazon.awssdk.services.dynamodb.model.AttributeValue
import software.amazon.awssdk.services.dynamodb.model.QueryRequest
import java.time.Clock

private const val A_USERNAME = "A_USERNAME"

@ExtendWith(MockKExtension::class)
class DynamoPasswordHistoryRepositoryTest {
class DynamoPasswordHistoryRepositoryTest : AbstractPasswordHistoryRepositoryTest() {

lateinit var uut: PasswordHistoryRepository

@BeforeEach
fun setUp() {
uut = DynamoPasswordHistoryRepository(
2,
3,
Clock.systemUTC(),
DynamoDbUtils.dynamoPasswordHistoryTableName,
DynamoDbUtils.dynamoDbClient
)
override fun initPasswordHistoryRepository(): PasswordHistoryRepository = DynamoPasswordHistoryRepository(
2,
3,
Clock.systemUTC(),
dynamoPasswordHistoryTableName,
dynamoDbClient
)

override fun resetDatabase() {
DynamoDbUtils.resetDynamoDb()
}

@Test
fun `when store a new password in an empty the history`() {

uut.store(A_USERNAME, Password("A_PASSWORD"))
val history = uut.load(A_USERNAME)

Assertions.assertEquals(1, history.size)
}

@Test
fun `when store a new password in an non empty the history`() {
uut.store(A_USERNAME, Password("A_PASSWORD 3"))
uut.store(A_USERNAME, Password("A_PASSWORD 1"))
uut.store(A_USERNAME, Password("A_PASSWORD 2"))
uut.store(A_USERNAME, Password("A_PASSWORD 10"))
uut.store(A_USERNAME, Password("A_PASSWORD 12"))
uut.store(A_USERNAME, Password("A_PASSWORD 22"))
uut.store(A_USERNAME, Password("A_PASSWORD 32"))
uut.store(A_USERNAME, Password("A_PASSWORD 42"))
val history = uut.load(A_USERNAME)

val expected = listOf(
Password("A_PASSWORD 42"),
Password("A_PASSWORD 32"),
)
Assertions.assertEquals(expected, history)
Assertions.assertEquals(3, loadActualDynamoSizeFor(A_USERNAME).size)
}

@Test
fun `when store a new password in an non empty the history with less item then requested as limit`() {
uut.store(A_USERNAME, Password("A_PASSWORD 3"))
val history = uut.load(A_USERNAME)

val expected = listOf(
Password("A_PASSWORD 3"),
)
Assertions.assertEquals(expected, history)
Assertions.assertEquals(1, loadActualDynamoSizeFor(A_USERNAME).size)
}

private fun loadActualDynamoSizeFor(userName: String): MutableList<MutableMap<String, AttributeValue>> {
return DynamoDbUtils.dynamoDbClient.query(
override fun loadActualDynamoSizeFor(userName: String): List<Map<String, Any>> {
return dynamoDbClient.query(
QueryRequest.builder()
.tableName(DynamoDbUtils.dynamoPasswordHistoryTableName)
.tableName(dynamoPasswordHistoryTableName)
.scanIndexForward(false)
.keyConditionExpression("user_name=:email")
.expressionAttributeValues(mapOf(":email" to userName.asDynamoAttribute())).build()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.vauthenticator.server.password.adapter.jdbc

import org.junit.jupiter.api.Assertions.*

class JdbcPasswordHistoryRepositoryTest

0 comments on commit 9860ceb

Please sign in to comment.