Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(core): use messageId internally #763

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@ public String getRef() {
/**
* Convenient Builder to create a Message reference to an existing Message
*
* @param message Message to create the reference to. This Message MUST have a 'messageName' field
* @return a Message with the 'ref' field pointing to "#/components/messages/{messageName}"
* @param message Message to create the reference to. This Message MUST have a 'messageId' field
* @return a Message with the 'ref' field pointing to "#/components/messages/{messageId}"
*/
public static MessageReference toComponentMessage(MessageObject message) {
return toComponentMessage(message.getName());
return toComponentMessage(message.getMessageId());
}

public static MessageReference toComponentMessage(String messageName) {
return new MessageReference("#/components/messages/" + messageName);
public static MessageReference toComponentMessage(String messageId) {
return new MessageReference("#/components/messages/" + messageId);
}

public static MessageReference toChannelMessage(String channelName, MessageObject message) {
return new MessageReference("#/channels/" + channelName + "/messages/" + message.getName());
return new MessageReference("#/channels/" + channelName + "/messages/" + message.getMessageId());
}

public static MessageReference toChannelMessage(String channelName, String messageName) {
return new MessageReference("#/channels/" + channelName + "/messages/" + messageName);
public static MessageReference toChannelMessage(String channelName, String messageId) {
return new MessageReference("#/channels/" + channelName + "/messages/" + messageId);
}

public static MessageReference toSchema(String schemaName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private AsyncAPI getAsyncAPITestObject() {
Operation newUserOperation = Operation.builder()
.action(OperationAction.SEND)
.channel(ChannelReference.fromChannel("new-user"))
.messages(List.of(MessageReference.toChannelMessage("new-user", message.getName())))
.messages(List.of(MessageReference.toChannelMessage("new-user", message)))
.bindings(Map.of("kafka", operationBinding))
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ public Map<String, Message> getMessages() {

@Override
public MessageReference registerMessage(MessageObject message) {
log.debug("Registering message for {}", message.getName());
log.debug("Registering message for {}", message.getMessageId());

messages.putIfAbsent(message.getName(), message);
messages.putIfAbsent(message.getMessageId(), message);

return MessageReference.toComponentMessage(message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private Map.Entry<String, ChannelObject> buildChannel(MethodAndAnnotation<A> met
MessageObject message = buildMessage(operationAnnotation, methodAndAnnotation.method());

ChannelObject channelItem = channelBuilder
.messages(Map.of(message.getName(), MessageReference.toComponentMessage(message)))
.messages(Map.of(message.getMessageId(), MessageReference.toComponentMessage(message)))
.build();
return Map.entry(channelName, channelItem);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private ChannelObject buildChannelItem(MethodAnnotation annotation, MessageObjec
Map<String, ChannelBinding> channelBinding = bindingFactory.buildChannelBinding(annotation);
Map<String, ChannelBinding> chBinding = channelBinding != null ? new HashMap<>(channelBinding) : null;
return ChannelObject.builder()
.messages(Map.of(message.getName(), MessageReference.toComponentMessage(message)))
.messages(Map.of(message.getMessageId(), MessageReference.toComponentMessage(message)))
.bindings(chBinding)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

@Slf4j
public class MessageHelper {
private static final Comparator<MessageObject> byMessageName = Comparator.comparing(MessageObject::getName);
private static final Comparator<MessageObject> byMessageId = Comparator.comparing(MessageObject::getMessageId);

private static final Supplier<Set<MessageObject>> messageSupplier = () -> new TreeSet<>(byMessageName);
private static final Supplier<Set<MessageObject>> messageSupplier = () -> new TreeSet<>(byMessageId);

private MessageHelper() {}

Expand All @@ -27,7 +27,7 @@ public static Map<String, MessageReference> toMessagesMap(Set<MessageObject> mes
}

return new ArrayList<>(messages.stream().collect(Collectors.toCollection(messageSupplier)))
.stream().collect(Collectors.toMap(MessageObject::getName, MessageReference::toComponentMessage));
.stream().collect(Collectors.toMap(MessageObject::getMessageId, MessageReference::toComponentMessage));
}

public static Map<String, MessageReference> toOperationsMessagesMap(
Expand All @@ -43,7 +43,7 @@ public static Map<String, MessageReference> toOperationsMessagesMap(
return new ArrayList<>(messages.stream().collect(Collectors.toCollection(messageSupplier)))
.stream()
.collect(Collectors.toMap(
MessageObject::getName,
e -> MessageReference.toChannelMessage(channelName, e.getName())));
MessageObject::getMessageId,
e -> MessageReference.toChannelMessage(channelName, e.getMessageId())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,28 +70,25 @@ void shouldMergeDifferentMessagesForSameChannel() {
// given
String channelName = "channel";
MessageObject message1 = MessageObject.builder()
.messageId("message1")
.name(String.class.getCanonicalName())
.description("This is a string")
.build();
MessageObject message2 = MessageObject.builder()
.messageId("message2")
.name(Integer.class.getCanonicalName())
.description("This is an integer")
.build();
MessageObject message3 = MessageObject.builder()
.messageId("message3")
.name(Integer.class.getCanonicalName())
.description("This is also an integer, but in essence the same payload type")
.build();
ChannelObject publisherChannel1 = ChannelObject.builder()
.messages(Map.of(message1.getName(), MessageReference.toComponentMessage(message1)))
.messages(Map.of(message1.getMessageId(), MessageReference.toComponentMessage(message1)))
.build();
ChannelObject publisherChannel2 = ChannelObject.builder()
.messages(Map.of(message2.getName(), MessageReference.toComponentMessage(message2)))
.messages(Map.of(message2.getMessageId(), MessageReference.toComponentMessage(message2)))
.build();
ChannelObject publisherChannel3 = ChannelObject.builder()
.messages(Map.of(message3.getName(), MessageReference.toComponentMessage(message3)))
.messages(Map.of(message3.getMessageId(), MessageReference.toComponentMessage(message3)))
.build();

// when
Expand All @@ -113,7 +110,6 @@ void shouldUseOtherMessageIfFirstMessageIsMissingForChannels() {
// given
String channelName = "channel";
MessageObject message2 = MessageObject.builder()
.messageId(String.class.getCanonicalName())
.name(String.class.getCanonicalName())
.description("This is a string")
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ void scan_componentHasAsyncMethodAnnotation() {
.build());

MessageObject message = MessageObject.builder()
.messageId("simpleFoo")
.messageId("simpleFooId")
.name("SimpleFooPayLoad")
.title("Message Title")
.description("Message description")
Expand All @@ -283,7 +283,7 @@ void scan_componentHasAsyncMethodAnnotation() {

ChannelObject expectedChannel = ChannelObject.builder()
.bindings(null)
.messages(Map.of(message.getName(), MessageReference.toComponentMessage(message)))
.messages(Map.of(message.getMessageId(), MessageReference.toComponentMessage(message)))
.build();

assertThat(actualChannels).containsExactly(Map.entry("test-channel", expectedChannel));
Expand Down Expand Up @@ -353,7 +353,7 @@ private static class ClassWithMessageAnnotation {
message =
@AsyncMessage(
description = "Message description",
messageId = "simpleFoo",
messageId = "simpleFooId",
name = "SimpleFooPayLoad",
contentType = "application/json",
title = "Message Title")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,14 @@ void shouldMergeDifferentMessageForSameOperation() {
String channelName = "channel";
String operationId = "operation";
MessageObject message1 = MessageObject.builder()
.messageId("message1")
.name(String.class.getCanonicalName())
.description("This is a string")
.build();
MessageObject message2 = MessageObject.builder()
.messageId("message2")
.name(Integer.class.getCanonicalName())
.description("This is an integer")
.build();
MessageObject message3 = MessageObject.builder()
.messageId("message3")
.name(Integer.class.getCanonicalName())
.description("This is also an integer, but in essence the same payload type")
.build();
Expand Down Expand Up @@ -137,15 +134,15 @@ void shouldUseOtherMessageIfFirstMessageIsMissingForChannels() {
.build();
ChannelObject publisherChannel1 = ChannelObject.builder().build();
ChannelObject publisherChannel2 = ChannelObject.builder()
.messages(Map.of(message2.getName(), message2))
.messages(Map.of(message2.getMessageId(), message2))
.build();

// when
Map<String, ChannelObject> mergedChannels = ChannelMerger.mergeChannels(
Arrays.asList(Map.entry(channelName, publisherChannel1), Map.entry(channelName, publisherChannel2)));

// then expectedMessage message2
var expectedMessages = Map.of(message2.getName(), message2);
var expectedMessages = Map.of(message2.getMessageId(), message2);

assertThat(mergedChannels).hasSize(1).hasEntrySatisfying(channelName, it -> {
assertThat(it.getMessages()).hasSize(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ void scan_componentHasAsyncMethodAnnotationInAbstractClass() {
.build());

MessageObject message = MessageObject.builder()
.messageId("simpleFoo")
.messageId("simpleFooAbstract")
.name("SimpleFooPayLoad")
.title("Message Title")
.description("Message description")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private ChannelObject buildChannel(FunctionalChannelBeanData beanData) {
Map<String, ChannelBinding> channelBinding = buildChannelBinding(beanData.annotatedElement());
return ChannelObject.builder()
.bindings(channelBinding)
.messages(Map.of(message.getName(), MessageReference.toComponentMessage(message)))
.messages(Map.of(message.getMessageId(), MessageReference.toComponentMessage(message)))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ void testConsumerBinding() {

ChannelObject expectedChannel = ChannelObject.builder()
.bindings(channelBinding)
.messages(Map.of(message.getName(), MessageReference.toComponentMessage(message)))
.messages(Map.of(message.getMessageId(), MessageReference.toComponentMessage(message)))
.build();

Operation expectedOperation = Operation.builder()
Expand Down Expand Up @@ -193,7 +193,7 @@ void testSupplierBinding() {

ChannelObject expectedChannel = ChannelObject.builder()
.bindings(channelBinding)
.messages(Map.of(message.getName(), MessageReference.toComponentMessage(message)))
.messages(Map.of(message.getMessageId(), MessageReference.toComponentMessage(message)))
.build();

assertThat(actualChannels).containsExactly(Map.entry(topicName, expectedChannel));
Expand Down Expand Up @@ -245,7 +245,8 @@ void testFunctionBinding() {

ChannelObject subscribeChannel = ChannelObject.builder()
.bindings(channelBinding)
.messages(Map.of(subscribeMessage.getName(), MessageReference.toComponentMessage(subscribeMessage)))
.messages(
Map.of(subscribeMessage.getMessageId(), MessageReference.toComponentMessage(subscribeMessage)))
.build();

MessageObject publishMessage = MessageObject.builder()
Expand All @@ -270,7 +271,7 @@ void testFunctionBinding() {

ChannelObject publishChannel = ChannelObject.builder()
.bindings(channelBinding)
.messages(Map.of(publishMessage.getName(), MessageReference.toComponentMessage(publishMessage)))
.messages(Map.of(publishMessage.getMessageId(), MessageReference.toComponentMessage(publishMessage)))
.build();

assertThat(actualChannels)
Expand Down Expand Up @@ -324,7 +325,8 @@ void testKStreamFunctionBinding() {

ChannelObject subscribeChannel = ChannelObject.builder()
.bindings(channelBinding)
.messages(Map.of(subscribeMessage.getName(), MessageReference.toComponentMessage(subscribeMessage)))
.messages(
Map.of(subscribeMessage.getMessageId(), MessageReference.toComponentMessage(subscribeMessage)))
.build();

MessageObject publishMessage = MessageObject.builder()
Expand All @@ -349,7 +351,7 @@ void testKStreamFunctionBinding() {

ChannelObject publishChannel = ChannelObject.builder()
.bindings(channelBinding)
.messages(Map.of(publishMessage.getName(), MessageReference.toComponentMessage(publishMessage)))
.messages(Map.of(publishMessage.getMessageId(), MessageReference.toComponentMessage(publishMessage)))
.build();

assertThat(actualChannels)
Expand Down Expand Up @@ -426,9 +428,9 @@ void testFunctionBindingWithSameTopicName() {
ChannelObject mergedChannel = ChannelObject.builder()
.bindings(channelBinding)
.messages(Map.of(
publishMessage.getName(),
publishMessage.getMessageId(),
MessageReference.toComponentMessage(publishMessage),
subscribeMessage.getName(),
subscribeMessage.getMessageId(),
MessageReference.toComponentMessage(subscribeMessage)))
.build();

Expand Down