Skip to content

Commit

Permalink
Merge pull request #135 from averak/feature/130-make-testfixture-with…
Browse files Browse the repository at this point in the history
…-builder

テストフィクスチャをbuilder経由で作成する
  • Loading branch information
averak authored Jan 31, 2024
2 parents 8e928a5 + 1ad4057 commit e41727e
Show file tree
Hide file tree
Showing 21 changed files with 263 additions and 99 deletions.
Original file line number Diff line number Diff line change
@@ -1,52 +1,32 @@
package net.averak.gsync.adapter.handler.player_api

import net.averak.gsync.adapter.dao.dto.base.PlayerDto
import net.averak.gsync.adapter.dao.dto.base.PlayerStorageEntryDto
import net.averak.gsync.adapter.dao.dto.base.PlayerStorageRevisionDto
import net.averak.gsync.adapter.handler.player_api.pbconv.PlayerStorageConverter
import net.averak.gsync.core.game_context.GameContext
import net.averak.gsync.domain.model.PlayerStorageEntry
import net.averak.gsync.schema.protobuf.player_api.PlayerStorageClearV1
import net.averak.gsync.schema.protobuf.player_api.PlayerStorageSearchV1
import net.averak.gsync.schema.protobuf.player_api.PlayerStorageSetV1
import net.averak.gsync.schema.protobuf.resource.player_storage.Criteria
import net.averak.gsync.testkit.AbstractDatabaseSpec
import net.averak.gsync.testkit.Faker
import net.averak.gsync.testkit.Fixture
import net.averak.gsync.testkit.fixture.builder.player.PlayerBuilder
import net.averak.gsync.testkit.fixture.builder.player.PlayerStorageBuilder

class PlayerStorageHandler_SearchV1_IT extends AbstractDatabaseSpec {

def "正常系: パターンにマッチしたエントリを取得できる"() {
given:
Fixture.setup(
Faker.fake(PlayerDto, [
"playerId": Faker.uuidv5("p1").toString(),
]),
)
Fixture.setup(
Faker.fake(PlayerStorageRevisionDto, [
"playerId": Faker.uuidv5("p1").toString(),
"gameId" : Faker.uuidv5("g1").toString(),
]),
)
Fixture.setup(
Faker.fake(PlayerStorageEntryDto, [
"playerId": Faker.uuidv5("p1").toString(),
"gameId" : Faker.uuidv5("g1").toString(),
"key" : "group1#key1",
"value" : "value1".bytes,
]),
Faker.fake(PlayerStorageEntryDto, [
"playerId": Faker.uuidv5("p1").toString(),
"gameId" : Faker.uuidv5("g1").toString(),
"key" : "group1#key2",
"value" : "value2".bytes,
]),
Faker.fake(PlayerStorageEntryDto, [
"playerId": Faker.uuidv5("p1").toString(),
"gameId" : Faker.uuidv5("g1").toString(),
"key" : "group2#key1",
"value" : "value3".bytes,
]),
final gctx = Faker.fake(GameContext)
playerUp.setup(
gctx,
new PlayerBuilder(Faker.uuidv5("p1")).build(),
new PlayerStorageBuilder(Faker.uuidv5("p1"), Faker.uuidv5("g1"))
.entries(
new PlayerStorageEntry("group1#key1", "value1".bytes),
new PlayerStorageEntry("group1#key2", "value2".bytes),
new PlayerStorageEntry("group2#key1", "value3".bytes),
)
.build(),
)

when:
Expand All @@ -60,7 +40,7 @@ class PlayerStorageHandler_SearchV1_IT extends AbstractDatabaseSpec {
).build(),
) {
it.session(Faker.uuidv5("p1"), Faker.uuidv5("g1"))
it.spoofingMasterVersion(Faker.uuidv4())
it.spoofingMasterVersion(gctx.masterVersion)
}

then:
Expand All @@ -73,17 +53,14 @@ class PlayerStorageHandler_SetV1_IT extends AbstractDatabaseSpec {

def "正常系: パターンにマッチしたエントリを作成できる"() {
given:
Fixture.setup(
Faker.fake(PlayerDto, [
"playerId": Faker.uuidv5("p1").toString(),
]),
)
Fixture.setup(
Faker.fake(PlayerStorageRevisionDto, [
"playerId" : Faker.uuidv5("p1").toString(),
"gameId" : Faker.uuidv5("g1").toString(),
"playerStorageRevisionId": Faker.uuidv5("r1").toString(),
]),
final gctx = Faker.fake(GameContext)
playerUp.setup(
gctx,
new PlayerBuilder(Faker.uuidv5("p1")).build(),
new PlayerStorageBuilder(Faker.uuidv5("p1"), Faker.uuidv5("g1"))
.revision(Faker.uuidv5("r1"))
.entries()
.build(),
)

final entry = Faker.fake(PlayerStorageEntry)
Expand All @@ -97,7 +74,7 @@ class PlayerStorageHandler_SetV1_IT extends AbstractDatabaseSpec {
.build(),
) {
it.session(Faker.uuidv5("p1"), Faker.uuidv5("g1"))
it.spoofingMasterVersion(Faker.uuidv4())
it.spoofingMasterVersion(gctx.masterVersion)
}

then:
Expand All @@ -120,37 +97,18 @@ class PlayerStorageHandler_ClearV1_IT extends AbstractDatabaseSpec {

def "正常系: パターンにマッチしたエントリを削除できる"() {
given:
Fixture.setup(
Faker.fake(PlayerDto, [
"playerId": Faker.uuidv5("p1").toString(),
]),
)
Fixture.setup(
Faker.fake(PlayerStorageRevisionDto, [
"playerId" : Faker.uuidv5("p1").toString(),
"gameId" : Faker.uuidv5("g1").toString(),
"playerStorageRevisionId": Faker.uuidv5("current revision").toString(),
]),
)
Fixture.setup(
Faker.fake(PlayerStorageEntryDto, [
"playerId": Faker.uuidv5("p1").toString(),
"gameId" : Faker.uuidv5("g1").toString(),
"key" : "group1#key1",
"value" : "value1".bytes,
]),
Faker.fake(PlayerStorageEntryDto, [
"playerId": Faker.uuidv5("p1").toString(),
"gameId" : Faker.uuidv5("g1").toString(),
"key" : "group1#key2",
"value" : "value2".bytes,
]),
Faker.fake(PlayerStorageEntryDto, [
"playerId": Faker.uuidv5("p1").toString(),
"gameId" : Faker.uuidv5("g1").toString(),
"key" : "group2#key1",
"value" : "value3".bytes,
]),
final gctx = Faker.fake(GameContext)
playerUp.setup(
gctx,
new PlayerBuilder(Faker.uuidv5("p1")).build(),
new PlayerStorageBuilder(Faker.uuidv5("p1"), Faker.uuidv5("g1"))
.revision(Faker.uuidv5("current revision"))
.entries(
new PlayerStorageEntry("group1#key1", "value1".bytes),
new PlayerStorageEntry("group1#key2", "value2".bytes),
new PlayerStorageEntry("group2#key1", "value3".bytes),
)
.build(),
)

when:
Expand All @@ -165,7 +123,7 @@ class PlayerStorageHandler_ClearV1_IT extends AbstractDatabaseSpec {
).build(),
) {
it.session(Faker.uuidv5("p1"), Faker.uuidv5("g1"))
it.spoofingMasterVersion(Faker.uuidv4())
it.spoofingMasterVersion(gctx.masterVersion)
}

then:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import net.averak.gsync.schema.protobuf.player_api.EchoEchoV1
import net.averak.gsync.testkit.AbstractDatabaseSpec
import net.averak.gsync.testkit.Assert
import net.averak.gsync.testkit.Faker
import net.averak.gsync.testkit.Fixture
import net.averak.gsync.testkit.fixture.Fixture
import org.springframework.beans.factory.annotation.Autowired

class ClientVersionInterceptor_UT extends AbstractDatabaseSpec {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import net.averak.gsync.core.exception.GsyncException
import net.averak.gsync.testkit.AbstractDatabaseSpec
import net.averak.gsync.testkit.Assert
import net.averak.gsync.testkit.Faker
import net.averak.gsync.testkit.Fixture
import net.averak.gsync.testkit.fixture.Fixture
import org.springframework.beans.factory.annotation.Autowired

class MasterVersionUtils_UT extends AbstractDatabaseSpec {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import net.averak.gsync.domain.model.Echo
import net.averak.gsync.testkit.AbstractDatabaseSpec
import net.averak.gsync.testkit.Assert
import net.averak.gsync.testkit.Faker
import net.averak.gsync.testkit.Fixture
import net.averak.gsync.testkit.fixture.Fixture
import org.springframework.beans.factory.annotation.Autowired
import spock.lang.Shared

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import net.averak.gsync.domain.repository.exception.MasterDataNotFoundException
import net.averak.gsync.testkit.AbstractDatabaseSpec
import net.averak.gsync.testkit.Assert
import net.averak.gsync.testkit.Faker
import net.averak.gsync.testkit.Fixture
import net.averak.gsync.testkit.fixture.Fixture
import org.springframework.beans.factory.annotation.Autowired

import java.time.LocalDateTime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import net.averak.gsync.core.game_context.GameContext
import net.averak.gsync.domain.model.Game
import net.averak.gsync.testkit.AbstractDatabaseSpec
import net.averak.gsync.testkit.Faker
import net.averak.gsync.testkit.Fixture
import net.averak.gsync.testkit.fixture.Fixture
import org.springframework.beans.factory.annotation.Autowired

class GameRepository_UT extends AbstractDatabaseSpec {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import net.averak.gsync.core.game_context.GameContext
import net.averak.gsync.domain.model.Operator
import net.averak.gsync.testkit.AbstractDatabaseSpec
import net.averak.gsync.testkit.Faker
import net.averak.gsync.testkit.Fixture
import net.averak.gsync.testkit.fixture.Fixture
import org.springframework.beans.factory.annotation.Autowired

class OperatorRepository_UT extends AbstractDatabaseSpec {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import net.averak.gsync.domain.model.PlayerProfile
import net.averak.gsync.testkit.AbstractDatabaseSpec
import net.averak.gsync.testkit.Assert
import net.averak.gsync.testkit.Faker
import net.averak.gsync.testkit.Fixture
import net.averak.gsync.testkit.fixture.Fixture
import org.springframework.beans.factory.annotation.Autowired
import spock.lang.Shared

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import net.averak.gsync.domain.repository.IPlayerStorageRepository
import net.averak.gsync.domain.repository.exception.AlreadyDoneException
import net.averak.gsync.testkit.AbstractDatabaseSpec
import net.averak.gsync.testkit.Faker
import net.averak.gsync.testkit.Fixture
import net.averak.gsync.testkit.fixture.Fixture
import org.springframework.beans.factory.annotation.Autowired
import spock.lang.Shared

Expand Down
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ allprojects {
}

// 現時点で K2 compiler はベータ版なので、導入を見送っている
// K2 コンパイラはまだ試験的なものなので、
// sourceSets {
// all {
// languageSettings {
Expand Down
19 changes: 10 additions & 9 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
[versions]
easy-random = "6.2.1"
flyway = "10.4.1"
easy-random = "7.0.0"
flyway = "10.7.1"
groovy = "4.0.18"
mybatis-generator = "1.4.2"
grpc = "1.61.0"
mybatis-generator = "1.4.2"
protobuf = "3.25.2"
spock = "2.4-M1-groovy-4.0"
spring-boot = "3.2.1"
spring-boot = "3.2.2"

[libraries]
commons-lang3 = "org.apache.commons:commons-lang3:3.14.0"
Expand All @@ -15,8 +16,8 @@ flyway-gradle-plugin = { module = "org.flywaydb:flyway-gradle-plugin", version.r
flyway-spanner = { module = "org.flywaydb:flyway-gcp-spanner", version.ref = "flyway" }
google-cloud-spanner-jdbc = "com.google.cloud:google-cloud-spanner-jdbc:2.15.0"
google-cloud-spanner-spring = "com.google.cloud:spring-cloud-gcp-starter-data-spanner:5.0.0"
google-protobuf = "com.google.protobuf:protobuf-java:3.25.2"
google-protobuf-util = "com.google.protobuf:protobuf-java-util:3.25.2"
google-protobuf = { module = "com.google.protobuf:protobuf-java", version.ref = "protobuf" }
google-protobuf-util = { module = "com.google.protobuf:protobuf-java-util", version.ref = "protobuf" }
groovy = { module = "org.apache.groovy:groovy", version.ref = "groovy" }
groovy-sql = { module = "org.apache.groovy:groovy-sql", version.ref = "groovy" }
guava = "com.google.guava:guava:33.0.0-jre"
Expand Down Expand Up @@ -47,7 +48,7 @@ spring-boot-starter-webflux = { module = "org.springframework.boot:spring-boot-s
flyway = { id = "org.flywaydb.flyway", version.ref = "flyway" }
gradle-git-properties = "com.gorylenko.gradle-git-properties:2.4.1"
sonarqube = "org.sonarqube:4.4.1.3373"
spotless = "com.diffplug.spotless:6.23.3"
spotless = "com.diffplug.spotless:6.25.0"
springboot = { id = "org.springframework.boot", version.ref = "spring-boot" }
version-catalog-update = "nl.littlerobots.version-catalog-update:0.8.3"
versions = "com.github.ben-manes.versions:0.50.0"
version-catalog-update = "nl.littlerobots.version-catalog-update:0.8.4"
versions = "com.github.ben-manes.versions:0.51.0"
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package net.averak.gsync.testkit
import groovy.sql.Sql
import jakarta.annotation.PostConstruct
import net.averak.gsync.infrastructure.redis.RedisClient
import net.averak.gsync.testkit.fixture.Fixture
import net.averak.gsync.testkit.fixture.setupper.MasterUp
import net.averak.gsync.testkit.fixture.setupper.PlayerUp
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import spock.lang.Shared
Expand All @@ -18,6 +21,14 @@ abstract class AbstractDatabaseSpec extends AbstractSpec {
@Shared
RedisClient redis

@Autowired
@Shared
MasterUp masterUp

@Autowired
@Shared
PlayerUp playerUp

@PostConstruct
private void init() {
Fixture.init(sql)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.google.protobuf.AbstractMessage
import io.grpc.*
import jakarta.annotation.PostConstruct
import net.averak.gsync.core.config.Config
import net.averak.gsync.core.game_context.GameContext
import net.averak.gsync.domain.model.Os
import net.averak.gsync.infrastructure.grpc.player_api.metadata.IncomingHeaderKey
import net.averak.gsync.schema.protobuf.player_api.EchoGrpc
Expand Down Expand Up @@ -74,6 +75,10 @@ class GrpcTester(
metadata[IncomingHeaderKey.GAME_ID] = value.toString()
}

fun idempotencyKey(value: UUID) {
metadata[IncomingHeaderKey.IDEMPOTENCY_KEY] = value.toString()
}

fun spoofingPlayerID(value: UUID) {
metadata[IncomingHeaderKey.DEBUG_SPOOFING_PLAYER_ID] = value.toString()
}
Expand All @@ -85,6 +90,12 @@ class GrpcTester(
fun spoofingCurrentTime(value: LocalDateTime) {
metadata[IncomingHeaderKey.DEBUG_SPOOFING_CURRENT_TIME] = value.toString()
}

fun gameContext(gctx: GameContext) {
idempotencyKey(gctx.idempotencyKey)
spoofingMasterVersion(gctx.masterVersion)
spoofingCurrentTime(gctx.currentTime)
}
}

private class MetadataInterceptor : ClientInterceptor {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.averak.gsync.testkit
package net.averak.gsync.testkit.fixture

import com.google.common.base.CaseFormat
import groovy.sql.Sql
Expand All @@ -19,7 +19,7 @@ class Fixture {
*/
@JvmStatic
fun init(sql: Sql) {
Fixture.sql = sql
Companion.sql = sql
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.averak.gsync.testkit.fixture.builder.master

import net.averak.gsync.domain.model.FriendSetting

class FriendSettingBuilder(
private var data: FriendSetting,
) {

constructor() : this(
FriendSetting(
maxFriendCount = 100,
maxFriendRequestCount = 100,
),
)

fun maxFriendCount(maxFriendCount: Int): FriendSettingBuilder {
data = data.copy(maxFriendCount = maxFriendCount)
return this
}

fun maxFriendRequestCount(maxFriendRequestCount: Int): FriendSettingBuilder {
data = data.copy(maxFriendRequestCount = maxFriendRequestCount)
return this
}

fun build(): Registry {
return Registry(friendSetting = data)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.averak.gsync.testkit.fixture.builder.master

import net.averak.gsync.domain.model.FriendSetting

data class Registry(
val friendSetting: FriendSetting? = null,
)
Loading

0 comments on commit e41727e

Please sign in to comment.