Skip to content

Commit 24278c0

Browse files
committed
use a mock in the GameFlowTest rather than calling the real challenges service
1 parent ae273f0 commit 24278c0

File tree

4 files changed

+85
-4
lines changed

4 files changed

+85
-4
lines changed

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ micronaut {
125125
apiPackageName = "org.developerden.codosseum.challenges.client.api"
126126
modelPackageName = "org.developerden.codosseum.challenges.client.model"
127127
useOptional = true
128+
useSealed = true
128129
}
129130
}
130131
}

src/test/groovy/org/developerden/codosseum/GameFlowSpec.groovy

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,26 @@
1414

1515
package org.developerden.codosseum
1616

17-
1817
import io.micronaut.http.HttpRequest
1918
import io.micronaut.http.client.HttpClient
2019
import io.micronaut.http.client.annotation.Client
2120
import io.micronaut.http.sse.Event
21+
import io.micronaut.test.annotation.MockBean
2222
import io.micronaut.test.extensions.spock.annotation.MicronautTest
2323
import jakarta.inject.Inject
24+
import org.developerden.codosseum.challenges.client.api.DefaultApi
25+
import org.developerden.codosseum.challenges.client.model.Difficulty
2426
import org.developerden.codosseum.dto.*
2527
import org.developerden.codosseum.event.GameEvent
2628
import org.developerden.codosseum.event.RoundStartEvent
2729
import org.developerden.codosseum.event.SyncEvent
2830
import org.developerden.codosseum.mode.GameModeType
2931
import org.developerden.codosseum.model.GamePhase
3032
import org.developerden.codosseum.service.game.event.SseEventSink
33+
import org.developerden.codosseum.stubs.Stubs
3134
import org.reactivestreams.Subscription
3235
import reactor.core.publisher.BaseSubscriber
36+
import reactor.core.publisher.Mono
3337
import spock.lang.Specification
3438
import spock.util.concurrent.PollingConditions
3539

@@ -43,6 +47,8 @@ class GameFlowSpec extends Specification {
4347

4448
@Inject
4549
SseEventSink eventSink
50+
@Inject
51+
DefaultApi defaultApi
4652

4753
// has to be thread safe as events come from another thread
4854
List<GameEvent> eventsReceived = new CopyOnWriteArrayList<>()
@@ -64,6 +70,10 @@ class GameFlowSpec extends Specification {
6470
}
6571

6672
def "Warmup phase starts correctly"() {
73+
given:
74+
def challenge = Stubs.fakeChallengeInfo()
75+
1 * defaultApi.challengesRandomGet(_, _) >> Mono.just(challenge)
76+
6777
when:
6878
def response = http.toBlocking().exchange(HttpRequest.POST("/games",
6979
new GameCreateRequest(
@@ -79,16 +89,17 @@ class GameFlowSpec extends Specification {
7989
def gameId = response.body().id()
8090
def key = response.body().adminKey()
8191

92+
8293
when:
83-
def getResponse = http.toBlocking()
94+
def warmupResponse = http.toBlocking()
8495
.exchange(HttpRequest.POST("/games/${gameId}/warmup", null)
8596
.header("Authorization", "Game $key"), Void)
8697

8798
then:
88-
getResponse.status.code == 204
99+
warmupResponse.status.code == 204
89100

90101
and: "wait until both events arrive"
91-
def conditions = new PollingConditions(timeout: 6, initialDelay: 0.1, delay: 0.1)
102+
def conditions = new PollingConditions(timeout: 7, initialDelay: 0.1, delay: 0.1)
92103
conditions.eventually {
93104
assert eventsReceived.size() == 2
94105
assert eventsReceived[0] instanceof SyncEvent
@@ -108,4 +119,9 @@ class GameFlowSpec extends Specification {
108119
info.players().allPlayers().count() == 1
109120

110121
}
122+
123+
@MockBean(DefaultApi)
124+
DefaultApi defaultApi() {
125+
Mock(DefaultApi)
126+
}
111127
}

src/test/groovy/org/developerden/codosseum/PlayerControllerSpec.groovy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,6 @@ class PlayerControllerSpec extends Specification {
5959
info.settings().allowedGameModes() == [GameModeType.FASTEST]
6060

6161
}
62+
63+
6264
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* # SPDX-FileCopyrightText: 2025 Alexander Wood (BristerMitten)
3+
* # SPDX-License-Identifier: AGPL-3.0-or-later
4+
*
5+
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
6+
*
7+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
8+
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9+
* See the GNU Affero General Public License for more details.
10+
*
11+
* You should have received a copy of the GNU Affero General Public License along with this program.
12+
* If not, see <https://www.gnu.org/licenses/>.
13+
*/
14+
15+
package org.developerden.codosseum.stubs;
16+
17+
import java.util.List;
18+
import org.developerden.codosseum.challenges.client.model.Author;
19+
import org.developerden.codosseum.challenges.client.model.ChallengeInfo;
20+
import org.developerden.codosseum.challenges.client.model.Contact;
21+
import org.developerden.codosseum.challenges.client.model.Difficulty;
22+
import org.developerden.codosseum.challenges.client.model.Example;
23+
import org.developerden.codosseum.challenges.client.model.Solution;
24+
import org.developerden.codosseum.challenges.client.model.Test;
25+
26+
public class Stubs {
27+
28+
public static ChallengeInfo fakeChallengeInfo() {
29+
return new ChallengeInfo(
30+
"empty-schema",
31+
new Author(
32+
"Author",
33+
List.of(
34+
new Contact(
35+
"Author Contact Type",
36+
"Author Contact Value"
37+
)
38+
)
39+
),
40+
"License",
41+
"Language",
42+
"Test Challenge",
43+
Difficulty.EASY,
44+
List.of("tag1", "tag2"),
45+
"This is a test challenge.",
46+
"Input Format",
47+
List.of(new Example(
48+
List.of("Example Input"),
49+
List.of("Example Output")
50+
)),
51+
List.of(new Test(
52+
"Test",
53+
List.of("Test Input"),
54+
List.of("Test Output"
55+
))),
56+
new Solution(
57+
"Solution Language",
58+
"solution file"
59+
)
60+
);
61+
}
62+
}

0 commit comments

Comments
 (0)