1
1
package io .github .stavshamir .springwolf .asyncapi ;
2
2
3
- import io .github .stavshamir .springwolf .asyncapi .dtos .KafkaMessageDto ;
4
3
import io .github .stavshamir .springwolf .producer .SpringwolfKafkaProducer ;
5
4
import org .junit .jupiter .api .Test ;
6
- import org .junit .jupiter .api .extension .ExtendWith ;
7
5
import org .mockito .ArgumentCaptor ;
8
6
import org .mockito .Captor ;
9
- import org .mockito .InjectMocks ;
10
- import org .mockito .Mock ;
11
- import org .mockito .junit .jupiter .MockitoExtension ;
12
- import org .springframework .http .HttpStatus ;
13
- import org .springframework .web .server .ResponseStatusException ;
7
+ import org .springframework .beans .factory .annotation .Autowired ;
8
+ import org .springframework .boot .test .autoconfigure .web .servlet .WebMvcTest ;
9
+ import org .springframework .boot .test .mock .mockito .MockBean ;
10
+ import org .springframework .http .MediaType ;
11
+ import org .springframework .test .context .ContextConfiguration ;
12
+ import org .springframework .test .context .TestPropertySource ;
13
+ import org .springframework .test .web .servlet .MockMvc ;
14
14
15
- import java .util .Collections ;
16
15
import java .util .Map ;
17
16
17
+ import static java .util .Collections .singletonMap ;
18
18
import static org .assertj .core .api .Assertions .assertThat ;
19
- import static org .assertj .core .api .Assertions .failBecauseExceptionWasNotThrown ;
20
- import static org .mockito .Mockito .*;
21
-
22
- @ ExtendWith (MockitoExtension .class )
19
+ import static org .mockito .ArgumentMatchers .eq ;
20
+ import static org .mockito .ArgumentMatchers .isNull ;
21
+ import static org .mockito .Mockito .verify ;
22
+ import static org .mockito .Mockito .verifyNoInteractions ;
23
+ import static org .mockito .Mockito .when ;
24
+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
25
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
26
+
27
+ @ WebMvcTest (SpringwolfKafkaController .class )
28
+ @ ContextConfiguration (classes = {SpringwolfKafkaController .class , SpringwolfKafkaProducer .class })
29
+ @ TestPropertySource (properties = {"springwolf.plugin.kafka.publishing.enabled=true" })
23
30
public class SpringwolfKafkaControllerTest {
24
31
25
- @ InjectMocks
26
- private SpringwolfKafkaController springwolfKafkaController ;
32
+ @ Autowired
33
+ private MockMvc mvc ;
27
34
28
- @ Mock
35
+ @ MockBean
29
36
private SpringwolfKafkaProducer springwolfKafkaProducer ;
30
37
31
38
@ Captor
@@ -37,57 +44,57 @@ public class SpringwolfKafkaControllerTest {
37
44
@ Test
38
45
public void testControllerShouldReturnBadRequestIfPayloadIsEmpty () {
39
46
try {
40
- springwolfKafkaController .publish ("test-topic" , new KafkaMessageDto (null , null ));
41
- failBecauseExceptionWasNotThrown (ResponseStatusException .class );
42
- } catch (ResponseStatusException e ) {
43
- assertThat (e .getStatus ()).isEqualTo (HttpStatus .BAD_REQUEST );
47
+ String content = "{\" headers\" : null, \" payload\" : null }" ;
48
+ mvc .perform (post ("/springwolf/kafka/publish?topic=test-topic" )
49
+ .contentType (MediaType .APPLICATION_JSON )
50
+ .content (content ))
51
+ .andExpect (status ().isBadRequest ());
52
+ } catch (Exception e ) {
44
53
verifyNoInteractions (springwolfKafkaProducer );
45
54
}
46
55
}
47
56
48
57
@ Test
49
- public void testControllerShouldReturnNotFoundIfNoKafkaProducerIsEnabled () {
58
+ public void testControllerShouldReturnNotFoundIfNoKafkaProducerIsEnabled () throws Exception {
50
59
when (springwolfKafkaProducer .isEnabled ()).thenReturn (false );
51
60
52
- Map <String , String > payload = Collections .singletonMap ("some-key" , "some-value" );
53
- KafkaMessageDto messageToPublish = new KafkaMessageDto (null , payload );
54
-
55
- try {
56
- springwolfKafkaController .publish ("test-topic" , messageToPublish );
57
- failBecauseExceptionWasNotThrown (ResponseStatusException .class );
58
- } catch (ResponseStatusException e ) {
59
- assertThat (e .getStatus ()).isEqualTo (HttpStatus .NOT_FOUND );
60
- }
61
+ String content = "{\" headers\" : null, \" payload\" : { \" some-key\" : \" some-value\" }}" ;
62
+ mvc .perform (post ("/springwolf/kafka/publish?topic=test-topic" )
63
+ .contentType (MediaType .APPLICATION_JSON )
64
+ .content (content ))
65
+ .andExpect (status ().isNotFound ());
61
66
}
62
67
63
68
@ Test
64
- public void testControllerShouldCallKafkaProducerIfOnlyPayloadIsSend () {
69
+ public void testControllerShouldCallKafkaProducerIfOnlyPayloadIsSend () throws Exception {
65
70
when (springwolfKafkaProducer .isEnabled ()).thenReturn (true );
66
71
67
- Map <String , String > payload = Collections .singletonMap ("some-key" , "some-value" );
68
- KafkaMessageDto messageToPublish = new KafkaMessageDto (null , payload );
72
+ String content = "{\" headers\" : null, \" payload\" : { \" some-key\" : \" some-value\" }}" ;
69
73
70
- springwolfKafkaController .publish ("test-topic" , messageToPublish );
74
+ mvc .perform (post ("/springwolf/kafka/publish" ).param ("topic" , "test-topic" )
75
+ .contentType (MediaType .APPLICATION_JSON )
76
+ .content (content ))
77
+ .andExpect (status ().isOk ());
71
78
72
79
verify (springwolfKafkaProducer ).send (eq ("test-topic" ), isNull (), payloadCaptor .capture ());
73
80
74
- assertThat (payloadCaptor .getValue ()).isEqualTo (payload );
81
+ assertThat (payloadCaptor .getValue ()).isEqualTo (singletonMap ( "some-key" , "some-value" ) );
75
82
}
76
83
77
84
@ Test
78
- public void testControllerShouldCallKafkaProducerIfPayloadAndHeadersAreSend () {
85
+ public void testControllerShouldCallKafkaProducerIfPayloadAndHeadersAreSend () throws Exception {
79
86
when (springwolfKafkaProducer .isEnabled ()).thenReturn (true );
80
87
81
- Map <String , String > headers = Collections .singletonMap ("some-header-key" , "some-header-value" );
82
- Map <String , String > payload = Collections .singletonMap ("some-payload-key" , "some-payload-value" );
83
-
84
- KafkaMessageDto messageToPublish = new KafkaMessageDto (headers , payload );
88
+ String content = "{\" headers\" : { \" some-header-key\" : \" some-header-value\" }, \" payload\" : { \" some-payload-key\" : \" some-payload-value\" }}" ;
85
89
86
- springwolfKafkaController .publish ("test-topic" , messageToPublish );
90
+ mvc .perform (post ("/springwolf/kafka/publish?topic=test-topic" )
91
+ .contentType (MediaType .APPLICATION_JSON )
92
+ .content (content ))
93
+ .andExpect (status ().isOk ());
87
94
88
95
verify (springwolfKafkaProducer ).send (eq ("test-topic" ), headerCaptor .capture (), payloadCaptor .capture ());
89
96
90
- assertThat (headerCaptor .getValue ()).isEqualTo (headers );
91
- assertThat (payloadCaptor .getValue ()).isEqualTo (payload );
97
+ assertThat (headerCaptor .getValue ()).isEqualTo (singletonMap ( "some-header-key" , "some-header-value" ) );
98
+ assertThat (payloadCaptor .getValue ()).isEqualTo (singletonMap ( "some- payload-key" , "some-payload-value" ) );
92
99
}
93
100
}
0 commit comments