Skip to content

Commit

Permalink
refs #130 テストフィクスチャをbuilder経由で作成する
Browse files Browse the repository at this point in the history
  • Loading branch information
averak committed Jan 31, 2024
1 parent f8c4987 commit 1ad4057
Show file tree
Hide file tree
Showing 19 changed files with 253 additions and 89 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
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,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package net.averak.gsync.testkit.fixture.builder.player

import net.averak.gsync.domain.model.Player
import net.averak.gsync.domain.model.PlayerProfile
import net.averak.gsync.testkit.Faker
import java.util.*

class PlayerBuilder(
private var data: Player,
) {

constructor(playerID: UUID) : this(
Player(
id = playerID,
friendID = UUID.randomUUID(),
isBanned = false,
profile = PlayerProfile(
nickname = Faker.alphanumeric(10),
iconID = Faker.uuidv4().toString(),
),
login = null,
),
)

fun profile(profile: PlayerProfile): PlayerBuilder {
data = data.copy(profile = profile)
return this
}

fun build(): Registry {
return Registry(player = data)
}
}
Loading

0 comments on commit 1ad4057

Please sign in to comment.