Skip to content

Commit 5dddf59

Browse files
authored
GH-83: fix: Merge operations when repeated ChannelItem found (#88)
As mentioned in GH-83, only one channelItem is part of the final map - although both channelItems are found. This improves the situation by merging the different operations for the same topic name into the same ChannelItem - while still the last one wins.
1 parent d3c8090 commit 5dddf59

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

springwolf-core/src/main/java/io/github/stavshamir/springwolf/asyncapi/DefaultChannelsService.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.stavshamir.springwolf.asyncapi;
22

33
import com.asyncapi.v2.model.channel.ChannelItem;
4+
import com.asyncapi.v2.model.channel.operation.Operation;
45
import io.github.stavshamir.springwolf.asyncapi.scanners.channels.ChannelsScanner;
56
import java.util.HashMap;
67
import lombok.RequiredArgsConstructor;
@@ -28,13 +29,34 @@ void findChannels() {
2829

2930
for (ChannelsScanner scanner : channelsScanners) {
3031
try {
31-
channels.putAll(scanner.scan());
32+
Map<String, ChannelItem> channels = scanner.scan();
33+
processFoundChannels(channels);
3234
} catch (Exception e) {
3335
log.error("An error was encountered during channel scanning with {}: {}", scanner, e.getMessage());
3436
}
3537
}
3638
}
3739

40+
private void processFoundChannels(Map<String, ChannelItem> foundChannels) {
41+
for (Map.Entry<String, ChannelItem> foundChannel: foundChannels.entrySet()) {
42+
if (!this.channels.containsKey(foundChannel.getKey())) {
43+
this.channels.put(foundChannel.getKey(), foundChannel.getValue());
44+
} else {
45+
ChannelItem existingChannel = this.channels.get(foundChannel.getKey());
46+
47+
Operation subscribeOperation = foundChannel.getValue().getSubscribe();
48+
if(subscribeOperation != null) {
49+
existingChannel.setSubscribe(subscribeOperation);
50+
}
51+
52+
Operation publishOperation = foundChannel.getValue().getPublish();
53+
if(publishOperation != null) {
54+
existingChannel.setPublish(publishOperation);
55+
}
56+
}
57+
}
58+
}
59+
3860
@Override
3961
public Map<String, ChannelItem> getChannels() {
4062
return channels;

springwolf-core/src/test/java/io/github/stavshamir/springwolf/asyncapi/DefaultChannelsServiceTest.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.stavshamir.springwolf.asyncapi;
22

33
import com.asyncapi.v2.model.channel.ChannelItem;
4+
import com.asyncapi.v2.model.channel.operation.Operation;
45
import com.google.common.collect.ImmutableMap;
56
import io.github.stavshamir.springwolf.asyncapi.scanners.channels.ChannelsScanner;
67
import org.junit.Test;
@@ -18,7 +19,9 @@
1819
@ContextConfiguration(classes = {
1920
DefaultChannelsService.class,
2021
DefaultChannelsServiceTest.FooChannelScanner.class,
21-
DefaultChannelsServiceTest.BarChannelScanner.class
22+
DefaultChannelsServiceTest.BarChannelScanner.class,
23+
DefaultChannelsServiceTest.SameTopic.SubscribeChannelScanner.class,
24+
DefaultChannelsServiceTest.SameTopic.ProduceChannelScanner.class
2225
})
2326
public class DefaultChannelsServiceTest {
2427

@@ -37,7 +40,8 @@ public void getChannels() {
3740

3841
assertThat(actualChannels)
3942
.containsAllEntriesOf(fooChannelScanner.scan())
40-
.containsAllEntriesOf(barChannelScanner.scan());
43+
.containsAllEntriesOf(barChannelScanner.scan())
44+
.containsEntry(SameTopic.topicName, SameTopic.expectedMergedChannel);
4145
}
4246

4347
@Component
@@ -56,4 +60,32 @@ public Map<String, ChannelItem> scan() {
5660
}
5761
}
5862

63+
static class SameTopic {
64+
static final String topicName = "subscribeProduceTopic";
65+
static final ChannelItem expectedMergedChannel = ChannelItem.builder()
66+
.publish(SameTopic.ProduceChannelScanner.publishOperation)
67+
.subscribe(SameTopic.SubscribeChannelScanner.subscribeOperation)
68+
.build();
69+
70+
@Component
71+
static class ProduceChannelScanner implements ChannelsScanner {
72+
static final Operation publishOperation = Operation.builder().message("publish").build();
73+
74+
@Override
75+
public Map<String, ChannelItem> scan() {
76+
return ImmutableMap.of(topicName, ChannelItem.builder().publish(publishOperation).build());
77+
}
78+
}
79+
80+
@Component
81+
static class SubscribeChannelScanner implements ChannelsScanner {
82+
static final Operation subscribeOperation = Operation.builder().message("consumer").build();
83+
84+
@Override
85+
public Map<String, ChannelItem> scan() {
86+
return ImmutableMap.of(topicName, ChannelItem.builder().subscribe(subscribeOperation).build());
87+
}
88+
}
89+
}
90+
5991
}

0 commit comments

Comments
 (0)