Skip to content

Commit 7eaee87

Browse files
Merge pull request #838 from symphony-enrico/main
Unregister Activity
2 parents 6fe3325 + 9d4aa8b commit 7eaee87

2 files changed

Lines changed: 32 additions & 8 deletions

File tree

symphony-bdk-core/src/main/java/com/symphony/bdk/core/activity/ActivityRegistry.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import java.util.Optional;
1515

1616
/**
17-
* This class allows to bind an {@link AbstractActivity} to the Real Time Events source, or Datafeed.
17+
* This class allows to bind/unbind an {@link AbstractActivity} to the Real Time Events source, or Datafeed.
1818
* It also maintains the list of registered activities.
1919
* <p>
2020
* If an activity to be registered is already existing in the registry, then the old one will be replaced.
@@ -55,22 +55,30 @@ public void register(final AbstractActivity<?, ?> activity) {
5555
this.activityList.add(activity);
5656
}
5757

58-
public List<AbstractActivity<?, ?>> getActivityList() {
59-
return new ArrayList<>(activityList);
60-
}
61-
62-
private void preProcessActivity(AbstractActivity<?, ?> activity) {
63-
58+
/**
59+
* Unregisters an activity from the registry.
60+
*
61+
* @param activity An activity.
62+
*/
63+
public void unregister(final AbstractActivity<?, ?> activity) {
6464
Optional<AbstractActivity<?, ?>> act = this.activityList.stream()
6565
.filter(a -> a.equals(activity))
6666
.findFirst();
6767

6868
act.ifPresent(abstractActivity -> {
6969
abstractActivity.bindToRealTimeEventsSource(this.datafeedLoop::unsubscribe);
7070
this.activityList.remove(abstractActivity);
71-
log.debug("One activity '{}' has been removed/unsubscribed in order to be replaced",
71+
log.debug("One activity '{}' has been removed/unsubscribed",
7272
abstractActivity.getInfo().name());
7373
});
74+
}
75+
76+
public List<AbstractActivity<?, ?>> getActivityList() {
77+
return new ArrayList<>(activityList);
78+
}
79+
80+
private void preProcessActivity(AbstractActivity<?, ?> activity) {
81+
unregister(activity); // Unregister, in case it already exists
7482

7583
// a command activity (potentially) needs the bot display name and user ID in order to parse the message text content
7684
// this way of passing this information is not very clean though, we should find something

symphony-bdk-core/src/test/java/com/symphony/bdk/core/activity/ActivityRegistryTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ void shouldReplaceActivity() {
6767
verify(this.datafeedService, times(1)).unsubscribe(any(RealTimeEventListener.class));
6868
}
6969

70+
@Test
71+
void shouldRemoveActivity() {
72+
final CommandActivity<?> act = new TestCommandActivity("test");
73+
assertTrue(this.registry.getActivityList().isEmpty(), "Registry must be empty");
74+
75+
this.registry.register(act);
76+
assertEquals(1, this.registry.getActivityList().size(), "Registry must contain only 1 activity");
77+
verify(this.datafeedService, times(1)).subscribe(any(RealTimeEventListener.class));
78+
assertEquals(this.botSession.getDisplayName(), act.getBotDisplayName());
79+
assertEquals(this.botSession.getId(), act.getBotUserId());
80+
81+
this.registry.unregister(act);
82+
assertTrue(this.registry.getActivityList().isEmpty(), "Registry must be empty");
83+
verify(this.datafeedService, times(1)).unsubscribe(any(RealTimeEventListener.class));
84+
}
85+
7086
@Test
7187
void shouldReplaceHelpActivity() {
7288
final HelpCommand helpCommand = new HelpCommand(this.registry, this.messageService);

0 commit comments

Comments
 (0)