@@ -44,102 +44,110 @@ class ChatKotlinApplicationTests {
4444 val now: Instant = Instant .now()
4545
4646 @BeforeEach
47- fun setUp () = runBlocking {
48- val secondBeforeNow = now.minusSeconds(1 )
49- val twoSecondBeforeNow = now.minusSeconds(2 )
50- val savedMessages = messageRepository.saveAll(
51- listOf (
52- Message (
53- " *testMessage*" ,
54- ContentType .PLAIN ,
55- twoSecondBeforeNow,
56- " test" ,
57- " http://test.com"
58- ),
59- Message (
60- " **testMessage2**" ,
61- ContentType .MARKDOWN ,
62- secondBeforeNow,
63- " test1" ,
64- " http://test.com"
65- ),
66- Message (
67- " `testMessage3`" ,
68- ContentType .MARKDOWN ,
69- now,
70- " test2" ,
71- " http://test.com"
47+ fun setUp () {
48+ runBlocking {
49+ val secondBeforeNow = now.minusSeconds(1 )
50+ val twoSecondBeforeNow = now.minusSeconds(2 )
51+ val savedMessages = messageRepository.saveAll(
52+ listOf (
53+ Message (
54+ " *testMessage*" ,
55+ ContentType .PLAIN ,
56+ twoSecondBeforeNow,
57+ " test" ,
58+ " http://test.com"
59+ ),
60+ Message (
61+ " **testMessage2**" ,
62+ ContentType .MARKDOWN ,
63+ secondBeforeNow,
64+ " test1" ,
65+ " http://test.com"
66+ ),
67+ Message (
68+ " `testMessage3`" ,
69+ ContentType .MARKDOWN ,
70+ now,
71+ " test2" ,
72+ " http://test.com"
73+ )
7274 )
7375 )
74- )
75- lastMessageId = savedMessages.first().id ? : " "
76+ lastMessageId = savedMessages.first().id ? : " "
77+ }
7678 }
7779
7880 @AfterEach
79- fun tearDown () = runBlocking {
80- messageRepository.deleteAll()
81+ fun tearDown () {
82+ runBlocking {
83+ messageRepository.deleteAll()
84+ }
8185 }
8286
8387 @ParameterizedTest
8488 @ValueSource(booleans = [true , false ])
85- fun `test that messages API returns latest messages` (withLastMessageId : Boolean ) = runBlocking{
86- val messages: List <MessageVM >? = client.exchange(
87- RequestEntity <Any >(
88- HttpMethod .GET ,
89- URI (" /api/v1/messages?lastMessageId=${if (withLastMessageId) lastMessageId else " " } " )
90- ),
91- object : ParameterizedTypeReference <List <MessageVM >>() {}).body
89+ fun `test that messages API returns latest messages` (withLastMessageId : Boolean ) {
90+ runBlocking {
91+
92+ val messages: List <MessageVM >? = client.exchange(
93+ RequestEntity <Any >(
94+ HttpMethod .GET ,
95+ URI (" /api/v1/messages?lastMessageId=${if (withLastMessageId) lastMessageId else " " } " )
96+ ),
97+ object : ParameterizedTypeReference <List <MessageVM >>() {}).body
98+
99+ if (! withLastMessageId) {
100+ assertThat(messages?.map { with (it) { copy(id = null , sent = sent.truncatedTo(MILLIS )) } })
101+ .first()
102+ .isEqualTo(
103+ MessageVM (
104+ " *testMessage*" ,
105+ UserVM (" test" , URL (" http://test.com" )),
106+ now.minusSeconds(2 ).truncatedTo(MILLIS )
107+ )
108+ )
109+ }
92110
93- if (! withLastMessageId) {
94- assertThat(messages?.map { with (it) { copy(id = null , sent = sent.truncatedTo(MILLIS )) } })
95- .first()
96- .isEqualTo(
111+ assertThat(messages?.map { it.prepareForTesting() })
112+ .containsSubsequence(
97113 MessageVM (
98- " *testMessage*" ,
99- UserVM (" test" , URL (" http://test.com" )),
100- now.minusSeconds(2 ).truncatedTo(MILLIS )
114+ " <body><p><strong>testMessage2</strong></p></body>" ,
115+ UserVM (" test1" , URL (" http://test.com" )),
116+ now.minusSeconds(1 ).truncatedTo(MILLIS )
117+ ),
118+ MessageVM (
119+ " <body><p><code>testMessage3</code></p></body>" ,
120+ UserVM (" test2" , URL (" http://test.com" )),
121+ now.truncatedTo(MILLIS )
101122 )
102123 )
103124 }
104-
105- assertThat(messages?.map { it.prepareForTesting() })
106- .containsSubsequence(
107- MessageVM (
108- " <body><p><strong>testMessage2</strong></p></body>" ,
109- UserVM (" test1" , URL (" http://test.com" )),
110- now.minusSeconds(1 ).truncatedTo(MILLIS )
111- ),
112- MessageVM (
113- " <body><p><code>testMessage3</code></p></body>" ,
114- UserVM (" test2" , URL (" http://test.com" )),
115- now.truncatedTo(MILLIS )
116- )
117- )
118125 }
119126
120127
121128 @Test
122- fun `test that messages posted to the API is stored` () = runBlocking {
123- client.postForEntity<Any >(
124- URI (" /api/v1/messages" ),
125- MessageVM (
126- " `HelloWorld`" ,
127- UserVM (" test" , URL (" http://test.com" )),
128- now.plusSeconds(1 )
129- )
130- )
131-
132- messageRepository.findAll()
133- .first { it.content.contains(" HelloWorld" ) }
134- .apply {
135- assertThat(this .prepareForTesting())
136- .isEqualTo(Message (
137- " `HelloWorld`" ,
138- ContentType .MARKDOWN ,
139- now.plusSeconds(1 ).truncatedTo(MILLIS ),
140- " test" ,
141- " http://test.com"
142- ))
143- }
129+ fun `test that messages posted to the API is stored` () {
130+ runBlocking {
131+ client.postForEntity<Any >(
132+ URI (" /api/v1/messages" ),
133+ MessageVM (
134+ " `HelloWorld`" ,
135+ UserVM (" test" , URL (" http://test.com" )),
136+ now.plusSeconds(1 )
137+ )
138+ )
139+ messageRepository.findAll()
140+ .first { it.content.contains(" HelloWorld" ) }
141+ .apply {
142+ assertThat(this .prepareForTesting())
143+ .isEqualTo(Message (
144+ " `HelloWorld`" ,
145+ ContentType .MARKDOWN ,
146+ now.plusSeconds(1 ).truncatedTo(MILLIS ),
147+ " test" ,
148+ " http://test.com"
149+ ))
150+ }
151+ }
144152 }
145153}
0 commit comments