-
Notifications
You must be signed in to change notification settings - Fork 2
/
ChatControllerTest.java
117 lines (93 loc) · 4.03 KB
/
ChatControllerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.example.websocketdemo.controller;
import com.example.websocketdemo.model.ChatMessage;
import com.example.websocketdemo.screenplay.abilities.ConnectToWebsockets;
import com.example.websocketdemo.screenplay.actions.ConnectsToWebsocket;
import com.example.websocketdemo.screenplay.actions.SendsChatMessage;
import com.example.websocketdemo.screenplay.questions.TheLatestMessageReceived;
import net.serenitybdd.junit.runners.SerenityRunner;
import net.serenitybdd.junit.spring.integration.SpringIntegrationMethodRule;
import net.serenitybdd.screenplay.Actor;
import net.serenitybdd.screenplay.Consequence;
import net.serenitybdd.screenplay.actors.Cast;
import net.serenitybdd.screenplay.actors.Stage;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.annotation.DirtiesContext;
import static net.serenitybdd.screenplay.EventualConsequence.eventually;
import static net.serenitybdd.screenplay.GivenWhenThen.seeThat;
import static net.serenitybdd.screenplay.actors.OnStage.setTheStage;
import static net.serenitybdd.screenplay.actors.OnStage.theActorCalled;
import static net.serenitybdd.screenplay.actors.OnStage.theActorInTheSpotlight;
import static net.serenitybdd.screenplay.actors.OnStage.withCurrentActor;
import static org.hamcrest.core.IsEqual.equalTo;
// Todo, package.info and @narrative
// Maybe try markdown resources
@RunWith(SerenityRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ChatControllerTest {
@Rule
public SpringIntegrationMethodRule springIntegrationMethodRule = new SpringIntegrationMethodRule();
@LocalServerPort
private int port;
@Autowired
private ChatController chatController;
private Cast cast = Cast.whereEveryoneCan(new ConnectToWebsockets());
{
cast.actorNamed("Dana");
cast.actorNamed("Bob");
}
@Before
public void danaJoinsTheChat() {
Stage stage = setTheStage(cast);
stage.shineSpotlightOn("Dana");
withCurrentActor(ConnectsToWebsocket.onPort(port));
theActorInTheSpotlight().should(eventually(joinWebsocketSession()));
}
@Test
public void anotherActorJoinsTheChat() {
Actor bob = bob();
bob.attemptsTo(ConnectsToWebsocket.onPort(port));
bob.should(eventually(joinWebsocketSession()));
dana().should(eventually(seeThatBobJoined()));
}
@Test
public void sendMessage() {
ChatMessage helloMessage = ChatMessage.chat(theActorInTheSpotlight().getName(), "hello");
theActorInTheSpotlight().attemptsTo(SendsChatMessage.saying("hello"));
theActorInTheSpotlight().should(eventually(seeThat(new TheLatestMessageReceived(), equalTo(helloMessage))));
}
@Test
public void sendExplicitMessage() {
ChatMessage explicitMessage = ChatMessage.chat("Dana", "fuck");
theActorInTheSpotlight().attemptsTo(SendsChatMessage.saying("fuck"));
theActorInTheSpotlight().should(eventually(seeThat(new TheLatestMessageReceived(), equalTo(explicitMessage))));
}
@Test
@DirtiesContext
public void explicitMessageIsFiltered() {
chatController.enableSwearWordFilter();
ChatMessage censoredMessage = ChatMessage.chat("Dana", "***");
theActorInTheSpotlight().attemptsTo(SendsChatMessage.saying("fuck"));
theActorInTheSpotlight().should(eventually(seeThat(new TheLatestMessageReceived(), equalTo(censoredMessage))));
}
private Consequence<ChatMessage> joinWebsocketSession() {
return seeThat(new TheLatestMessageReceived(), equalTo(ChatMessage.join(theActorInTheSpotlight().getName())));
}
private Consequence<ChatMessage> seeThatBobJoined() {
return seeThat(new TheLatestMessageReceived(), equalTo(ChatMessage.join("Bob")));
}
private Actor dana() {
return getActor("dana");
}
private Actor bob() {
return getActor("Bob");
}
private Actor getActor(String actor) {
return theActorCalled(actor);
}
}