Skip to content

Commit

Permalink
feat(core): mark messages (wip)
Browse files Browse the repository at this point in the history
Co-authored-by: David Müller <[email protected]>
  • Loading branch information
timonback and sam0r040 committed Oct 18, 2024
1 parent a5eddad commit 9bbad12
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ public class AsyncApiGroupingService {
// if empty, keep all

// TODO: Context class/object
// private final Map<String, Boolean> markedChannelIds;
private final Set<String> markedOperationIds = new HashSet<>();
private final Set<String> markedChannelIds = new HashSet<>();
// private final Set<String> markedMessageIds = new HashSet<>();
// private final Map<String, Boolean> 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())
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -27,6 +66,7 @@ void shouldCreateNewAsyncApi() {
AsyncApiGroup asyncApiGroup = AsyncApiGroup.builder()
.operationActionsToKeep(List.of())
.channelNamesToKeep(List.of())
.messageNamesToKeep(List.of())
.build();

// when
Expand All @@ -45,6 +85,7 @@ void shouldUseIdenticalInfo() {
AsyncApiGroup asyncApiGroup = AsyncApiGroup.builder()
.operationActionsToKeep(List.of())
.channelNamesToKeep(List.of())
.messageNamesToKeep(List.of())
.build();

// when
Expand All @@ -62,6 +103,7 @@ void shouldUseIdenticalId() {
AsyncApiGroup asyncApiGroup = AsyncApiGroup.builder()
.operationActionsToKeep(List.of())
.channelNamesToKeep(List.of())
.messageNamesToKeep(List.of())
.build();

// when
Expand All @@ -80,6 +122,7 @@ void shouldUseIdenticalDefaultContentType() {
AsyncApiGroup asyncApiGroup = AsyncApiGroup.builder()
.operationActionsToKeep(List.of())
.channelNamesToKeep(List.of())
.messageNamesToKeep(List.of())
.build();

// when
Expand All @@ -92,88 +135,51 @@ 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));
}

@Test
// 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))
Expand All @@ -182,6 +188,7 @@ void shouldFilterOperationByActionAndTransitiveChannels() {
AsyncApiGroup asyncApiGroup = AsyncApiGroup.builder()
.operationActionsToKeep(List.of(OperationAction.SEND))
.channelNamesToKeep(List.of())
.messageNamesToKeep(List.of())
.build();

// when
Expand All @@ -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))
Expand All @@ -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
Expand All @@ -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));
}
}

0 comments on commit 9bbad12

Please sign in to comment.