From 5e9ce3a9f82b2648161aa0324b79017f0cd2997a Mon Sep 17 00:00:00 2001 From: Claude Brisson Date: Tue, 23 May 2023 09:34:31 +0200 Subject: [PATCH] Team basic unit tests ok --- .../jeudego/pairgoth/api/PairingHandler.kt | 2 +- .../org/jeudego/pairgoth/model/Tournament.kt | 4 +- webapp/src/test/kotlin/BasicTests.kt | 45 +++++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/webapp/src/main/kotlin/org/jeudego/pairgoth/api/PairingHandler.kt b/webapp/src/main/kotlin/org/jeudego/pairgoth/api/PairingHandler.kt index 9ba2727..445d55b 100644 --- a/webapp/src/main/kotlin/org/jeudego/pairgoth/api/PairingHandler.kt +++ b/webapp/src/main/kotlin/org/jeudego/pairgoth/api/PairingHandler.kt @@ -18,7 +18,7 @@ object PairingHandler: PairgothApiHandler { val playing = (tournament.games.getOrNull(round)?.values ?: emptyList()).flatMap { listOf(it.black, it.white) }.toSet() - return tournament.pairables.values.filter { !it.skip.contains(round) && !playing.contains(it.id) }.toJsonArray() + return tournament.pairables.values.filter { !it.skip.contains(round) && !playing.contains(it.id) }.map { it.id }.toJsonArray() } override fun post(request: HttpServletRequest): Json { diff --git a/webapp/src/main/kotlin/org/jeudego/pairgoth/model/Tournament.kt b/webapp/src/main/kotlin/org/jeudego/pairgoth/model/Tournament.kt index b5ad1e6..469f685 100644 --- a/webapp/src/main/kotlin/org/jeudego/pairgoth/model/Tournament.kt +++ b/webapp/src/main/kotlin/org/jeudego/pairgoth/model/Tournament.kt @@ -132,8 +132,8 @@ class TeamTournament( name = json.getString("name") ?: default?.name ?: badRequest("missing name") ).apply { json.getArray("players")?.let { arr -> - arr.map { - if (it != null && it is Json.Object) Player.fromJson(it) + arr.mapTo(playerIds) { + if (it != null && it is Number) it.toInt().also { id -> players.containsKey(id) } else badRequest("invalid players array") } } ?: badRequest("missing players") diff --git a/webapp/src/test/kotlin/BasicTests.kt b/webapp/src/test/kotlin/BasicTests.kt index 02690c2..c76de8d 100644 --- a/webapp/src/test/kotlin/BasicTests.kt +++ b/webapp/src/test/kotlin/BasicTests.kt @@ -5,6 +5,7 @@ import org.junit.jupiter.api.MethodOrderer.Alphanumeric import org.junit.jupiter.api.Test import org.junit.jupiter.api.TestInstance import org.junit.jupiter.api.TestMethodOrder +import java.util.Objects import kotlin.test.assertEquals import kotlin.test.assertTrue @@ -34,6 +35,27 @@ class BasicTests: TestBase() { ) ) + val aTeamTournament = Json.Object( + "type" to "TEAM2", + "name" to "Mon Tournoi par équipes", + "shortName" to "mon-tournoi-par-equipes", + "startDate" to "2023-05-20", + "endDate" to "2023-05-23", + "country" to "FR", + "location" to "Marseille", + "online" to true, + "timeSystem" to Json.Object( + "type" to "FISCHER", + "mainTime" to 1200, + "increment" to 10 + ), + "rounds" to 2, + "pairing" to Json.Object( + "type" to "SWISS", + "method" to "SPLIT_AND_RANDOM" + ) + ) + val aPlayer = Json.Object( "name" to "Burma", "firstname" to "Nestor", @@ -98,4 +120,27 @@ class BasicTests: TestBase() { "[{\"id\":1,\"w\":2,\"b\":1,\"r\":\"?\"}]") assertTrue(possibleResults.contains(games.toString()), "pairing differs") } + + @Test + fun `006 team tournament`() { + var resp = TestAPI.post("/api/tour", aTeamTournament) as Json.Object + assertTrue(resp.getBoolean("success") == true, "expecting success") + assertEquals(2, resp.getInt("id"), "expecting id #2 for new tournament") + resp = TestAPI.post("api/tour/2/part", aPlayer) as Json.Object + assertTrue(resp.getBoolean("success") == true, "expecting success") + assertEquals(3, resp.getInt("id"), "expecting id #3 for new player") + resp = TestAPI.post("api/tour/2/part", anotherPlayer) as Json.Object + assertTrue(resp.getBoolean("success") == true, "expecting success") + assertEquals(4, resp.getInt("id"), "expecting id #{ for new player") + assertTrue(resp.getBoolean("success") == true, "expecting success") + var arr = TestAPI.get("/api/tour/2/pair/1") as Json.Array + assertEquals("[]", arr.toString(), "expecting an empty array") + resp = TestAPI.post("api/tour/2/team", Json.parse("""{ "name":"The Buffallos", "players":[3, 4] }""") as Json.Object) as Json.Object + assertTrue(resp.getBoolean("success") == true, "expecting success") + assertEquals(5, resp.getInt("id"), "expecting team id #5") + resp = TestAPI.get("api/tour/2/team/5") as Json.Object + assertEquals("""{"id":5,"name":"The Buffallos","players":[3,4]}""", resp.toString(), "expecting team description") + arr = TestAPI.get("/api/tour/2/pair/1") as Json.Array + assertEquals("[5]", arr.toString(), "expecting a singleton array") + } }