-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(jms): add controller and binding tests
- Loading branch information
Showing
9 changed files
with
305 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
195 changes: 195 additions & 0 deletions
195
...hub/stavshamir/springwolf/asyncapi/controller/SpringwolfJmsControllerIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.stavshamir.springwolf.asyncapi.controller; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import io.github.stavshamir.springwolf.configuration.properties.SpringwolfConfigProperties; | ||
import io.github.stavshamir.springwolf.producer.SpringwolfJmsProducer; | ||
import io.github.stavshamir.springwolf.schemas.DefaultSchemasService; | ||
import io.github.stavshamir.springwolf.schemas.SchemasService; | ||
import io.github.stavshamir.springwolf.schemas.example.ExampleJsonGenerator; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.extern.jackson.Jacksonized; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.Captor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.TestPropertySource; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import java.util.Map; | ||
|
||
import static java.util.Collections.singletonMap; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.ArgumentMatchers.isNull; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoInteractions; | ||
import static org.mockito.Mockito.when; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@WebMvcTest(SpringwolfJmsController.class) | ||
@ContextConfiguration( | ||
classes = { | ||
SpringwolfJmsController.class, | ||
PublishingPayloadCreator.class, | ||
SpringwolfJmsProducer.class, | ||
DefaultSchemasService.class, | ||
ExampleJsonGenerator.class, | ||
SpringwolfConfigProperties.class, | ||
}) | ||
@TestPropertySource( | ||
properties = { | ||
"springwolf.docket.base-package=io.github.stavshamir.springwolf.asyncapi", | ||
"springwolf.docket.info.title=Title", | ||
"springwolf.docket.info.version=1.0", | ||
"springwolf.docket.servers.jms.protocol=jms", | ||
"springwolf.docket.servers.jms.url=127.0.0.1", | ||
"springwolf.plugin.jms.publishing.enabled=true", | ||
"springwolf.use-fqn=true" | ||
}) | ||
class SpringwolfJmsControllerIntegrationTest { | ||
|
||
@Autowired | ||
private MockMvc mvc; | ||
|
||
@Autowired | ||
private SchemasService schemasService; | ||
|
||
@MockBean | ||
private SpringwolfJmsProducer springwolfJmsProducer; | ||
|
||
@Captor | ||
private ArgumentCaptor<PayloadDto> payloadCaptor; | ||
|
||
@Captor | ||
private ArgumentCaptor<Map<String, String>> headerCaptor; | ||
|
||
@BeforeEach | ||
void setup() { | ||
when(springwolfJmsProducer.isEnabled()).thenReturn(true); | ||
|
||
schemasService.register(PayloadDto.class); | ||
} | ||
|
||
@Test | ||
void testControllerShouldReturnBadRequestIfPayloadIsEmpty() { | ||
try { | ||
String content = | ||
""" | ||
{ | ||
"bindings": null, | ||
"headers": null, | ||
"payload": "" | ||
}"""; | ||
mvc.perform(post("/springwolf/jms/publish?topic=test-topic") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(content)) | ||
.andExpect(status().isBadRequest()); | ||
} catch (Exception e) { | ||
verifyNoInteractions(springwolfJmsProducer); | ||
} | ||
} | ||
|
||
@Test | ||
void testControllerShouldReturnBadRequestIfPayloadIsNotSet() { | ||
try { | ||
String content = | ||
""" | ||
{ | ||
"bindings": null, | ||
"headers": null | ||
}"""; | ||
mvc.perform(post("/springwolf/jms/publish?topic=test-topic") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(content)) | ||
.andExpect(status().isBadRequest()); | ||
} catch (Exception e) { | ||
verifyNoInteractions(springwolfJmsProducer); | ||
} | ||
} | ||
|
||
@Test | ||
void testControllerShouldReturnNotFoundIfNoJmsProducerIsEnabled() throws Exception { | ||
when(springwolfJmsProducer.isEnabled()).thenReturn(false); | ||
|
||
String content = | ||
""" | ||
{ | ||
"bindings": null, | ||
"headers": null, | ||
"payload": "{ \\"some-payload-key\\" : \\"some-payload-value\\" }" | ||
}"""; | ||
mvc.perform(post("/springwolf/jms/publish?topic=test-topic") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(content)) | ||
.andExpect(status().isNotFound()); | ||
} | ||
|
||
@Test | ||
void testControllerShouldCallJmsProducerIfOnlyPayloadIsSend() throws Exception { | ||
when(springwolfJmsProducer.isEnabled()).thenReturn(true); | ||
|
||
String content = | ||
""" | ||
{ | ||
"bindings": null, | ||
"headers": null, | ||
"payload": "{ \\"some-payload-key\\" : \\"some-payload-value\\" }", | ||
"payloadType": "io.github.stavshamir.springwolf.asyncapi.controller.SpringwolfJmsControllerIntegrationTest$PayloadDto" | ||
}"""; | ||
|
||
mvc.perform(post("/springwolf/jms/publish") | ||
.param("topic", "test-topic") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(content)) | ||
.andExpect(status().isOk()); | ||
|
||
verify(springwolfJmsProducer).send(eq("test-topic"), isNull(), payloadCaptor.capture()); | ||
|
||
assertThat(payloadCaptor.getValue()).isEqualTo(new PayloadDto("some-payload-value")); | ||
} | ||
|
||
@Test | ||
void testControllerShouldCallJmsProducerIfPayloadAndHeadersAreSend() throws Exception { | ||
when(springwolfJmsProducer.isEnabled()).thenReturn(true); | ||
|
||
String content = | ||
""" | ||
{ | ||
"bindings": null, | ||
"headers": { | ||
"some-header-key": "some-header-value" | ||
}, | ||
"payload": "{ \\"some-payload-key\\" : \\"some-payload-value\\" }", | ||
"payloadType": "io.github.stavshamir.springwolf.asyncapi.controller.SpringwolfJmsControllerIntegrationTest$PayloadDto" | ||
} | ||
"""; | ||
|
||
mvc.perform(post("/springwolf/jms/publish?topic=test-topic") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(content)) | ||
.andExpect(status().isOk()); | ||
|
||
verify(springwolfJmsProducer).send(eq("test-topic"), headerCaptor.capture(), payloadCaptor.capture()); | ||
|
||
assertThat(headerCaptor.getValue()).isEqualTo(singletonMap("some-header-key", "some-header-value")); | ||
assertThat(payloadCaptor.getValue()).isEqualTo(new PayloadDto("some-payload-value")); | ||
} | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@Jacksonized | ||
@Builder | ||
public static class PayloadDto { | ||
@JsonProperty("some-payload-key") | ||
private String field; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...hamir/springwolf/asyncapi/scanners/bindings/processor/JmsMessageBindingProcessorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.stavshamir.springwolf.asyncapi.scanners.bindings.processor; | ||
|
||
import com.asyncapi.v2.binding.message.jms.JMSMessageBinding; | ||
import io.github.stavshamir.springwolf.asyncapi.scanners.bindings.ProcessedMessageBinding; | ||
import io.github.stavshamir.springwolf.asyncapi.scanners.channels.operationdata.annotation.JmsAsyncOperationBinding; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Optional; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class JmsMessageBindingProcessorTest { | ||
private final JmsMessageBindingProcessor processor = new JmsMessageBindingProcessor(); | ||
|
||
@Test | ||
void processTest() throws NoSuchMethodException { | ||
Method method = JmsMessageBindingProcessorTest.class.getMethod("methodWithAnnotation"); | ||
|
||
ProcessedMessageBinding binding = processor.process(method).get(); | ||
|
||
assertThat(binding.getType()).isEqualTo("jms"); | ||
assertThat(binding.getBinding()).isEqualTo(new JMSMessageBinding()); | ||
} | ||
|
||
@Test | ||
void processWithoutAnnotationTest() throws NoSuchMethodException { | ||
Method method = JmsMessageBindingProcessorTest.class.getMethod("methodWithoutAnnotation"); | ||
|
||
Optional<ProcessedMessageBinding> binding = processor.process(method); | ||
|
||
assertThat(binding).isNotPresent(); | ||
} | ||
|
||
@JmsAsyncOperationBinding | ||
public void methodWithAnnotation() {} | ||
|
||
public void methodWithoutAnnotation() {} | ||
} |
Oops, something went wrong.