From 9bbad127ea2134e964566781385053947dcc3905 Mon Sep 17 00:00:00 2001 From: Timon Back Date: Fri, 18 Oct 2024 18:00:39 +0200 Subject: [PATCH] feat(core): mark messages (wip) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: David Müller --- .../asyncapi/AsyncApiGroupingService.java | 8 +- .../configuration/docket/AsyncApiGroup.java | 12 +- .../asyncapi/AsyncApiGroupingServiceTest.java | 160 +++++++----------- 3 files changed, 77 insertions(+), 103 deletions(-) diff --git a/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/AsyncApiGroupingService.java b/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/AsyncApiGroupingService.java index 019f1255e..646d65b17 100644 --- a/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/AsyncApiGroupingService.java +++ b/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/AsyncApiGroupingService.java @@ -22,17 +22,19 @@ public class AsyncApiGroupingService { // if empty, keep all // TODO: Context class/object - // private final Map markedChannelIds; private final Set markedOperationIds = new HashSet<>(); private final Set markedChannelIds = new HashSet<>(); + // private final Set markedMessageIds = new HashSet<>(); // private final Map markedComponentIds; public AsyncAPI groupAPI(AsyncAPI fullAsyncApi, AsyncApiGroup asyncApiGroup) { Boolean markEverything = asyncApiGroup.getOperationActionsToKeep().isEmpty() - && asyncApiGroup.getChannelNamesToKeep().isEmpty(); + && asyncApiGroup.getChannelNamesToKeep().isEmpty() + && asyncApiGroup.getMessageNamesToKeep().isEmpty(); markOperations(fullAsyncApi, asyncApiGroup, markEverything); markChannels(fullAsyncApi, asyncApiGroup); + // markMessages(fullAsyncApi, asyncApiGroup); AsyncAPI asyncAPI = AsyncAPI.builder() .info(fullAsyncApi.getInfo()) @@ -41,7 +43,7 @@ public AsyncAPI groupAPI(AsyncAPI fullAsyncApi, AsyncApiGroup asyncApiGroup) { // .servers(docket.getServers()) .channels(filterChannels(fullAsyncApi)) .operations(filterOperations(fullAsyncApi)) - // .components(components) + // .components(components) // TODO: for DaTi .build(); return asyncAPI; diff --git a/springwolf-core/src/main/java/io/github/springwolf/core/configuration/docket/AsyncApiGroup.java b/springwolf-core/src/main/java/io/github/springwolf/core/configuration/docket/AsyncApiGroup.java index 5cc1a6242..809cd9871 100644 --- a/springwolf-core/src/main/java/io/github/springwolf/core/configuration/docket/AsyncApiGroup.java +++ b/springwolf-core/src/main/java/io/github/springwolf/core/configuration/docket/AsyncApiGroup.java @@ -53,9 +53,15 @@ public boolean matchOperation(OperationMatcher op) { return false; } - public boolean matchesChannel(ChannelObject value) { - return channelNamesToKeep.stream() - .anyMatch(pattern -> pattern.matcher(value.getAddress()).matches()); + public boolean matchesChannel(ChannelObject channelObject) { + // TODO: differentiate between messageId (wrong) and messageName (should be) + + boolean messageMatch = messageNamesToKeep.stream() + .anyMatch(pattern -> channelObject.getMessages().keySet().stream() + .anyMatch(messageId -> pattern.matcher(messageId).matches())); + boolean channelNameMatch = channelNamesToKeep.stream() + .anyMatch(pattern -> pattern.matcher(channelObject.getAddress()).matches()); + return messageMatch || channelNameMatch; } class OperationMatcher { diff --git a/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/AsyncApiGroupingServiceTest.java b/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/AsyncApiGroupingServiceTest.java index d44b7c9b8..16b64c15b 100644 --- a/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/AsyncApiGroupingServiceTest.java +++ b/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/AsyncApiGroupingServiceTest.java @@ -4,13 +4,16 @@ import io.github.springwolf.asyncapi.v3.model.AsyncAPI; import io.github.springwolf.asyncapi.v3.model.channel.ChannelObject; import io.github.springwolf.asyncapi.v3.model.channel.ChannelReference; +import io.github.springwolf.asyncapi.v3.model.channel.message.MessageObject; +import io.github.springwolf.asyncapi.v3.model.channel.message.MessageReference; +import io.github.springwolf.asyncapi.v3.model.components.Components; import io.github.springwolf.asyncapi.v3.model.info.Info; import io.github.springwolf.asyncapi.v3.model.operation.Operation; import io.github.springwolf.asyncapi.v3.model.operation.OperationAction; import io.github.springwolf.core.configuration.docket.AsyncApiGroup; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Pattern; @@ -19,6 +22,42 @@ class AsyncApiGroupingServiceTest { + private final MessageObject message1 = + MessageObject.builder().messageId("messageId1").build(); + private final MessageObject message2 = + MessageObject.builder().messageId("messageId2").build(); + private final MessageObject message3 = + MessageObject.builder().messageId("messageId3").build(); + + private final ChannelObject channel1 = ChannelObject.builder() + .channelId("channelId1") + .address("channelId1") // TODO: change value + .messages(new HashMap<>(Map.of( + message1.getMessageId(), + MessageReference.toComponentMessage(message1.getMessageId()), + message2.getMessageId(), + MessageReference.toComponentMessage(message2.getMessageId())))) + .build(); + private final ChannelObject channel2 = ChannelObject.builder() + .channelId("channelId2") + .address("channelId2") // TODO: change value) + .messages(new HashMap<>( + Map.of(message3.getMessageId(), MessageReference.toComponentMessage(message3.getMessageId())))) + .build(); + + private final Operation sendOperation = Operation.builder() + .action(OperationAction.SEND) + .channel(ChannelReference.fromChannel(channel1)) + .messages(List.of( + MessageReference.toComponentMessage(message1.getMessageId()), + MessageReference.toComponentMessage(message2.getMessageId()))) + .build(); + private final Operation receiveOperation = Operation.builder() + .action(OperationAction.RECEIVE) + .channel(ChannelReference.fromChannel(channel2)) + .messages(List.of(MessageReference.toComponentMessage(message3.getMessageId()))) + .build(); + @Test void shouldCreateNewAsyncApi() { // given @@ -27,6 +66,7 @@ void shouldCreateNewAsyncApi() { AsyncApiGroup asyncApiGroup = AsyncApiGroup.builder() .operationActionsToKeep(List.of()) .channelNamesToKeep(List.of()) + .messageNamesToKeep(List.of()) .build(); // when @@ -45,6 +85,7 @@ void shouldUseIdenticalInfo() { AsyncApiGroup asyncApiGroup = AsyncApiGroup.builder() .operationActionsToKeep(List.of()) .channelNamesToKeep(List.of()) + .messageNamesToKeep(List.of()) .build(); // when @@ -62,6 +103,7 @@ void shouldUseIdenticalId() { AsyncApiGroup asyncApiGroup = AsyncApiGroup.builder() .operationActionsToKeep(List.of()) .channelNamesToKeep(List.of()) + .messageNamesToKeep(List.of()) .build(); // when @@ -80,6 +122,7 @@ void shouldUseIdenticalDefaultContentType() { AsyncApiGroup asyncApiGroup = AsyncApiGroup.builder() .operationActionsToKeep(List.of()) .channelNamesToKeep(List.of()) + .messageNamesToKeep(List.of()) .build(); // when @@ -92,33 +135,22 @@ void shouldUseIdenticalDefaultContentType() { @Test void shouldFilterOperationByAction() { // given - String channelId = "channelId"; - ChannelObject channel = ChannelObject.builder().channelId(channelId).build(); - - Operation sendOperation = Operation.builder() - .action(OperationAction.SEND) - .channel(ChannelReference.fromChannel(channel)) - .build(); - Operation receiveOperation = Operation.builder() - .action(OperationAction.RECEIVE) - .channel(ChannelReference.fromChannel(channel)) - .build(); - AsyncAPI full = AsyncAPI.builder() - .channels(Map.of(channel.getChannelId(), channel)) + .channels(Map.of(channel1.getChannelId(), channel1)) .operations(Map.of("send", sendOperation, "receive", receiveOperation)) .build(); AsyncApiGroup asyncApiGroup = AsyncApiGroup.builder() .operationActionsToKeep(List.of(OperationAction.SEND)) .channelNamesToKeep(List.of()) + .messageNamesToKeep(List.of()) .build(); // when AsyncAPI grouped = new AsyncApiGroupingService().groupAPI(full, asyncApiGroup); // then - assertThat(grouped.getChannels()).isEqualTo(Map.of(channel.getChannelId(), channel)); + assertThat(grouped.getChannels()).isEqualTo(Map.of(channel1.getChannelId(), channel1)); assertThat(grouped.getOperations()).isEqualTo(Map.of("send", sendOperation)); } @@ -126,54 +158,28 @@ void shouldFilterOperationByAction() { // should get all copied for all empty filters void shouldGetAllOperationsWhenActionsIsEmpty() { // given - String channelId = "channelId"; - ChannelObject channel = ChannelObject.builder().channelId(channelId).build(); - - Operation sendOperation = Operation.builder() - .action(OperationAction.SEND) - .channel(ChannelReference.fromChannel(channel)) - .build(); - Operation receiveOperation = Operation.builder() - .action(OperationAction.RECEIVE) - .channel(ChannelReference.fromChannel(channel)) - .build(); - AsyncAPI full = AsyncAPI.builder() - .channels(Map.of(channel.getChannelId(), channel)) + .channels(Map.of(channel1.getChannelId(), channel1)) .operations(Map.of("send", sendOperation, "receive", receiveOperation)) .build(); AsyncApiGroup asyncApiGroup = AsyncApiGroup.builder() .operationActionsToKeep(List.of()) .channelNamesToKeep(List.of()) + .messageNamesToKeep(List.of()) .build(); // when AsyncAPI grouped = new AsyncApiGroupingService().groupAPI(full, asyncApiGroup); // then - // TODO: special case: when nothing is marked, mark everything assertThat(grouped.getOperations()).isEqualTo(Map.of("send", sendOperation, "receive", receiveOperation)); - assertThat(grouped.getChannels()).isEqualTo(Map.of(channel.getChannelId(), channel)); + assertThat(grouped.getChannels()).isEqualTo(Map.of(channel1.getChannelId(), channel1)); } @Test void shouldFilterOperationByActionAndTransitiveChannels() { // given - String channelId1 = "channelId1"; - ChannelObject channel1 = ChannelObject.builder().channelId(channelId1).build(); - String channelId2 = "channelId2"; - ChannelObject channel2 = ChannelObject.builder().channelId(channelId2).build(); - - Operation sendOperation = Operation.builder() - .action(OperationAction.SEND) - .channel(ChannelReference.fromChannel(channel1)) - .build(); - Operation receiveOperation = Operation.builder() - .action(OperationAction.RECEIVE) - .channel(ChannelReference.fromChannel(channelId2)) - .build(); - AsyncAPI full = AsyncAPI.builder() .channels(Map.of(channel1.getChannelId(), channel1, channel2.getChannelId(), channel2)) .operations(Map.of("send", sendOperation, "receive", receiveOperation)) @@ -182,6 +188,7 @@ void shouldFilterOperationByActionAndTransitiveChannels() { AsyncApiGroup asyncApiGroup = AsyncApiGroup.builder() .operationActionsToKeep(List.of(OperationAction.SEND)) .channelNamesToKeep(List.of()) + .messageNamesToKeep(List.of()) .build(); // when @@ -195,26 +202,6 @@ void shouldFilterOperationByActionAndTransitiveChannels() { @Test void shouldFilterOperationsByChannelName() { // given - String channelId1 = "channelId1"; - ChannelObject channel1 = ChannelObject.builder() - .channelId(channelId1) - .address(channelId1) - .build(); - String channelId2 = "channelId2"; - ChannelObject channel2 = ChannelObject.builder() - .channelId(channelId2) - .address(channelId2) - .build(); - - Operation sendOperation = Operation.builder() - .action(OperationAction.SEND) - .channel(ChannelReference.fromChannel(channel1)) - .build(); - Operation receiveOperation = Operation.builder() - .action(OperationAction.RECEIVE) - .channel(ChannelReference.fromChannel(channelId2)) - .build(); - AsyncAPI full = AsyncAPI.builder() .channels(Map.of(channel1.getChannelId(), channel1, channel2.getChannelId(), channel2)) .operations(Map.of("send", sendOperation, "receive", receiveOperation)) @@ -223,6 +210,7 @@ void shouldFilterOperationsByChannelName() { AsyncApiGroup asyncApiGroup = AsyncApiGroup.builder() .operationActionsToKeep(List.of()) .channelNamesToKeep(List.of(Pattern.compile(channel1.getChannelId()))) + .messageNamesToKeep(List.of()) .build(); // when @@ -234,51 +222,29 @@ void shouldFilterOperationsByChannelName() { } @Test - @Disabled - void testOrIsNotEnough() { + void shouldFilterOperationByMessageName() { // given - String channelId1 = "channelId1"; - ChannelObject channel1 = ChannelObject.builder().channelId(channelId1).build(); - String channelId2 = "channelId2"; - ChannelObject channel2 = ChannelObject.builder().channelId(channelId2).build(); - - Operation sendOperation1 = Operation.builder() - .action(OperationAction.SEND) - .channel(ChannelReference.fromChannel(channel1)) - .build(); - Operation receiveOperation1 = Operation.builder() - .action(OperationAction.RECEIVE) - .channel(ChannelReference.fromChannel(channelId1)) - .build(); - - Operation sendOperation2 = Operation.builder() - .action(OperationAction.SEND) - .channel(ChannelReference.fromChannel(channel2)) - .build(); - Operation receiveOperation2 = Operation.builder() - .action(OperationAction.RECEIVE) - .channel(ChannelReference.fromChannel(channelId2)) - .build(); - AsyncAPI full = AsyncAPI.builder() .channels(Map.of(channel1.getChannelId(), channel1, channel2.getChannelId(), channel2)) - .operations(Map.of( - "channel1_send", sendOperation1, - "channel1_receive", receiveOperation1, - "channel2_send", sendOperation2, - "channel2_receive", receiveOperation2)) + .operations(Map.of("send", sendOperation, "receive", receiveOperation)) + .components(Components.builder() + .messages(Map.of(message1.getMessageId(), message1, message2.getMessageId(), message2)) + .build()) .build(); AsyncApiGroup asyncApiGroup = AsyncApiGroup.builder() - .operationActionsToKeep(List.of(OperationAction.SEND)) - .channelNamesToKeep(List.of(Pattern.compile(channel1.getChannelId()))) + .operationActionsToKeep(List.of()) + .channelNamesToKeep(List.of()) + .messageNamesToKeep(List.of(Pattern.compile(message1.getMessageId()))) .build(); // when AsyncAPI grouped = new AsyncApiGroupingService().groupAPI(full, asyncApiGroup); // then - assertThat(grouped.getOperations()).isEqualTo(Map.of("send", sendOperation1)); + assertThat(grouped.getOperations()).isEqualTo(Map.of("send", sendOperation)); + channel1.getMessages().remove(message2.getMessageId()); assertThat(grouped.getChannels()).isEqualTo(Map.of(channel1.getChannelId(), channel1)); + assertThat(grouped.getComponents().getMessages()).isEqualTo(Map.of(message1.getMessageId(), message1)); } }