-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stomp): add OperationCustomizer
allows to add reply for SendTo and SendToUser
- Loading branch information
Showing
13 changed files
with
245 additions
and
9 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
.../github/springwolf/core/asyncapi/scanners/operations/annotations/OperationCustomizer.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,10 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.springwolf.core.asyncapi.scanners.operations.annotations; | ||
|
||
import io.github.springwolf.asyncapi.v3.model.operation.Operation; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
public interface OperationCustomizer { | ||
void customize(Operation operation, Method method); | ||
} |
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
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
37 changes: 37 additions & 0 deletions
37
...ub/springwolf/plugins/stomp/asyncapi/scanners/operation/annotations/SendToCustomizer.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,37 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.springwolf.plugins.stomp.asyncapi.scanners.operation.annotations; | ||
|
||
import io.github.springwolf.asyncapi.v3.model.ReferenceUtil; | ||
import io.github.springwolf.asyncapi.v3.model.channel.ChannelReference; | ||
import io.github.springwolf.asyncapi.v3.model.channel.message.MessageReference; | ||
import io.github.springwolf.asyncapi.v3.model.operation.Operation; | ||
import io.github.springwolf.asyncapi.v3.model.operation.OperationReply; | ||
import io.github.springwolf.core.asyncapi.scanners.common.payload.PayloadMethodReturnService; | ||
import io.github.springwolf.core.asyncapi.scanners.common.utils.AnnotationScannerUtil; | ||
import io.github.springwolf.core.asyncapi.scanners.operations.annotations.OperationCustomizer; | ||
import io.github.springwolf.plugins.stomp.asyncapi.scanners.bindings.StompBindingSendToFactory; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.messaging.handler.annotation.SendTo; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.List; | ||
|
||
@RequiredArgsConstructor | ||
public class SendToCustomizer implements OperationCustomizer { | ||
private final StompBindingSendToFactory bindingFactory; | ||
private final PayloadMethodReturnService payloadService; | ||
|
||
@Override | ||
public void customize(Operation operation, Method method) { | ||
SendTo annotation = AnnotationScannerUtil.findAnnotation(SendTo.class, method); | ||
if (annotation != null) { | ||
String channelId = ReferenceUtil.toValidId(bindingFactory.getChannelName(annotation)); | ||
String payloadName = payloadService.extractSchema(method).name(); | ||
|
||
operation.setReply(OperationReply.builder() | ||
.channel(ChannelReference.fromChannel(channelId)) | ||
.messages(List.of(MessageReference.toChannelMessage(channelId, payloadName))) | ||
.build()); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...pringwolf/plugins/stomp/asyncapi/scanners/operation/annotations/SendToUserCustomizer.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,37 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.springwolf.plugins.stomp.asyncapi.scanners.operation.annotations; | ||
|
||
import io.github.springwolf.asyncapi.v3.model.ReferenceUtil; | ||
import io.github.springwolf.asyncapi.v3.model.channel.ChannelReference; | ||
import io.github.springwolf.asyncapi.v3.model.channel.message.MessageReference; | ||
import io.github.springwolf.asyncapi.v3.model.operation.Operation; | ||
import io.github.springwolf.asyncapi.v3.model.operation.OperationReply; | ||
import io.github.springwolf.core.asyncapi.scanners.common.payload.PayloadMethodReturnService; | ||
import io.github.springwolf.core.asyncapi.scanners.common.utils.AnnotationScannerUtil; | ||
import io.github.springwolf.core.asyncapi.scanners.operations.annotations.OperationCustomizer; | ||
import io.github.springwolf.plugins.stomp.asyncapi.scanners.bindings.StompBindingSendToUserFactory; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.messaging.simp.annotation.SendToUser; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.List; | ||
|
||
@RequiredArgsConstructor | ||
public class SendToUserCustomizer implements OperationCustomizer { | ||
private final StompBindingSendToUserFactory bindingFactory; | ||
private final PayloadMethodReturnService payloadService; | ||
|
||
@Override | ||
public void customize(Operation operation, Method method) { | ||
SendToUser annotation = AnnotationScannerUtil.findAnnotation(SendToUser.class, method); | ||
if (annotation != null) { | ||
String channelId = ReferenceUtil.toValidId(bindingFactory.getChannelName(annotation)); | ||
String payloadName = payloadService.extractSchema(method).name(); | ||
|
||
operation.setReply(OperationReply.builder() | ||
.channel(ChannelReference.fromChannel(channelId)) | ||
.messages(List.of(MessageReference.toChannelMessage(channelId, payloadName))) | ||
.build()); | ||
} | ||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
...pringwolf/plugins/stomp/asyncapi/scanners/operation/annotations/SendToCustomizerTest.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,49 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.springwolf.plugins.stomp.asyncapi.scanners.operation.annotations; | ||
|
||
import io.github.springwolf.asyncapi.v3.model.channel.ChannelReference; | ||
import io.github.springwolf.asyncapi.v3.model.channel.message.MessageReference; | ||
import io.github.springwolf.asyncapi.v3.model.operation.Operation; | ||
import io.github.springwolf.core.asyncapi.scanners.common.payload.NamedSchemaObject; | ||
import io.github.springwolf.core.asyncapi.scanners.common.payload.PayloadMethodReturnService; | ||
import io.github.springwolf.plugins.stomp.asyncapi.scanners.bindings.StompBindingSendToFactory; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.messaging.handler.annotation.SendTo; | ||
|
||
import java.util.List; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class SendToCustomizerTest { | ||
|
||
private static final String CHANNEL_ID = "queue"; | ||
private static final String MESSAGE_ID = "schema-name"; | ||
|
||
private StompBindingSendToFactory bindingFactory = mock(StompBindingSendToFactory.class); | ||
private PayloadMethodReturnService payloadService = mock(PayloadMethodReturnService.class); | ||
|
||
private SendToCustomizer sendToCustomizer = new SendToCustomizer(bindingFactory, payloadService); | ||
|
||
@Test | ||
void customize() throws NoSuchMethodException { | ||
// given | ||
Operation operation = new Operation(); | ||
when(bindingFactory.getChannelName(any())).thenReturn(CHANNEL_ID); | ||
when(payloadService.extractSchema(any())).thenReturn(new NamedSchemaObject(MESSAGE_ID, null)); | ||
|
||
// when | ||
sendToCustomizer.customize(operation, this.getClass().getDeclaredMethod("testMethod")); | ||
|
||
// then | ||
assertThat(operation.getReply()).isNotNull(); | ||
assertThat(operation.getReply().getChannel()).isEqualTo(ChannelReference.fromChannel(CHANNEL_ID)); | ||
assertThat(operation.getReply().getMessages()) | ||
.isEqualTo(List.of(MessageReference.toChannelMessage(CHANNEL_ID, MESSAGE_ID))); | ||
} | ||
|
||
@SendTo(CHANNEL_ID) | ||
void testMethod() {} | ||
} |
49 changes: 49 additions & 0 deletions
49
...gwolf/plugins/stomp/asyncapi/scanners/operation/annotations/SendToUserCustomizerTest.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,49 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.springwolf.plugins.stomp.asyncapi.scanners.operation.annotations; | ||
|
||
import io.github.springwolf.asyncapi.v3.model.channel.ChannelReference; | ||
import io.github.springwolf.asyncapi.v3.model.channel.message.MessageReference; | ||
import io.github.springwolf.asyncapi.v3.model.operation.Operation; | ||
import io.github.springwolf.core.asyncapi.scanners.common.payload.NamedSchemaObject; | ||
import io.github.springwolf.core.asyncapi.scanners.common.payload.PayloadMethodReturnService; | ||
import io.github.springwolf.plugins.stomp.asyncapi.scanners.bindings.StompBindingSendToUserFactory; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.messaging.simp.annotation.SendToUser; | ||
|
||
import java.util.List; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class SendToUserCustomizerTest { | ||
|
||
private static final String CHANNEL_ID = "queue"; | ||
private static final String MESSAGE_ID = "schema-name"; | ||
|
||
private StompBindingSendToUserFactory bindingFactory = mock(StompBindingSendToUserFactory.class); | ||
private PayloadMethodReturnService payloadService = mock(PayloadMethodReturnService.class); | ||
|
||
private SendToUserCustomizer sendToCustomizer = new SendToUserCustomizer(bindingFactory, payloadService); | ||
|
||
@Test | ||
void customize() throws NoSuchMethodException { | ||
// given | ||
Operation operation = new Operation(); | ||
when(bindingFactory.getChannelName(any())).thenReturn(CHANNEL_ID); | ||
when(payloadService.extractSchema(any())).thenReturn(new NamedSchemaObject(MESSAGE_ID, null)); | ||
|
||
// when | ||
sendToCustomizer.customize(operation, this.getClass().getDeclaredMethod("testMethod")); | ||
|
||
// then | ||
assertThat(operation.getReply()).isNotNull(); | ||
assertThat(operation.getReply().getChannel()).isEqualTo(ChannelReference.fromChannel(CHANNEL_ID)); | ||
assertThat(operation.getReply().getMessages()) | ||
.isEqualTo(List.of(MessageReference.toChannelMessage(CHANNEL_ID, MESSAGE_ID))); | ||
} | ||
|
||
@SendToUser(CHANNEL_ID) | ||
void testMethod() {} | ||
} |