Skip to content

Commit e404ec2

Browse files
WIP
1 parent b693e76 commit e404ec2

File tree

5 files changed

+95
-30
lines changed

5 files changed

+95
-30
lines changed
Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,57 @@
11
package io.vanillabp.cockpit.gui.api.v1;
22

3+
import java.io.Serial;
34
import java.util.Collection;
5+
import java.util.Optional;
6+
47
import org.springframework.context.ApplicationEvent;
8+
import org.springframework.lang.NonNull;
59

610
public class GuiEvent extends ApplicationEvent {
711

12+
@Serial
813
private static final long serialVersionUID = 1L;
9-
10-
private Object event;
11-
12-
private Collection<String> targetGroups;
14+
15+
private final Object event;
16+
17+
private final Collection<String> targetGroups;
1318

1419
public GuiEvent(
15-
final Object source,
20+
@NonNull final Object source,
1621
final Collection<String> targetGroups,
1722
final Object event) {
18-
23+
1924
super(source);
2025
this.event = event;
2126
this.targetGroups = targetGroups;
22-
27+
2328
}
24-
29+
2530
public Object getEvent() {
2631
return event;
2732
}
2833

2934
public Collection<String> getTargetGroups() {
3035
return targetGroups;
3136
}
32-
37+
3338
public boolean matchesTargetGroups(
3439
final Collection<String> groups) {
35-
40+
3641
if (targetGroups == null) {
3742
return true;
3843
}
39-
44+
4045
return targetGroups
4146
.stream()
4247
.flatMap(targetGroup -> groups.stream().map(targetGroups::equals))
4348
.anyMatch(hasMatchingGroup -> hasMatchingGroup);
44-
45-
}
4649

47-
public boolean relevantForTargetGroups(final Collection<String> groups) {
48-
if (targetGroups == null) {
49-
return true;
50-
}
51-
52-
return targetGroups.stream().anyMatch(groups::contains);
5350
}
5451

52+
public boolean relevantForTargetGroups(@NonNull Collection<String> groups) {
53+
return Optional.ofNullable(targetGroups)
54+
.map(tg -> tg.stream().anyMatch(groups::contains))
55+
.orElse(true);
56+
}
5557
}

container/src/main/java/io/vanillabp/cockpit/tasklist/UserTaskChangedNotification.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public class UserTaskChangedNotification extends NotificationEvent {
1919
public UserTaskChangedNotification(
2020
final Type type,
2121
final String userTaskId,
22-
final Collection<String> targetGroups) {
22+
final Collection<String> targetGroups,
23+
final Collection<String> targetUsers) {
2324

2425
super(
2526
"UserTask",

container/src/main/java/io/vanillabp/cockpit/util/events/NotificationEvent.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,46 @@
11
package io.vanillabp.cockpit.util.events;
22

3+
import java.io.Serial;
34
import java.time.Clock;
45
import java.util.Collection;
56
import java.util.List;
67
import org.springframework.context.ApplicationEvent;
78

89
public abstract class NotificationEvent extends ApplicationEvent {
910

11+
@Serial
1012
private static final long serialVersionUID = 1L;
1113

12-
public static enum Type { INSERT, UPDATE, DELETE };
14+
public enum Type { INSERT, UPDATE, DELETE };
1315

1416
private final Type type;
15-
16-
private Collection<String> targetGroups;
17+
private final Collection<String> targetGroups;
18+
private final Collection<String> targetUsers;
1719

1820
public NotificationEvent(
1921
final Object source,
2022
final Clock clock,
2123
final Type type,
22-
final Collection<String> targetGroups) {
24+
final Collection<String> targetGroups,
25+
final Collection<String> targetUsers) {
2326

2427
super(source, clock);
2528
this.type = type;
2629
this.targetGroups = targetGroups;
30+
this.targetUsers = targetUsers;
2731

2832
}
2933

3034
public NotificationEvent(
3135
final Object source,
3236
final Type type,
33-
Collection<String> targetGroups) {
37+
Collection<String> targetGroups,
38+
Collection<String> targetUsers) {
3439

3540
super(source);
3641
this.type = type;
3742
this.targetGroups = targetGroups;
43+
this.targetUsers = targetUsers;
3844

3945
}
4046

@@ -45,7 +51,11 @@ public Type getType() {
4551
public Collection<String> getTargetGroups() {
4652
return targetGroups;
4753
}
48-
54+
55+
public Collection<String> getTargetUsers() {
56+
return targetUsers;
57+
}
58+
4959
public boolean matchesTargetGroups(
5060
final List<String> groups) {
5161

container/src/main/java/io/vanillabp/cockpit/workflowlist/WorkflowChangedNotification.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ public class WorkflowChangedNotification extends NotificationEvent {
1818
public WorkflowChangedNotification(
1919
final Type type,
2020
final String workflowId,
21-
final Collection<String> targetGroups) {
21+
final Collection<String> targetGroups,
22+
final Collection<String> targetUsers) {
2223

2324
super(
2425
"Workflow",
2526
type,
26-
targetGroups);
27+
targetGroups,
28+
targetUsers);
2729

2830
this.workflowId = workflowId;
2931

@@ -53,7 +55,8 @@ public static WorkflowChangedNotification build(
5355
Type.valueOf(type.name()),
5456
message.getRaw().getDocumentKey().get(
5557
message.getRaw().getDocumentKey().getFirstKey()).asString().getValue(),
56-
null);
58+
null,
59+
null);
5760

5861
}
5962

@@ -81,7 +84,8 @@ public static WorkflowChangedNotification build(
8184
Type.valueOf(type.name()),
8285
event.getRaw().getDocumentKey().get(
8386
event.getRaw().getDocumentKey().getFirstKey()).asString().getValue(),
84-
null);
87+
null,
88+
null);
8589

8690
}
8791

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package io.vanillabp.cockpit.gui.api.v1;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.Collections;
7+
import java.util.List;
8+
9+
import static org.junit.jupiter.api.Assertions.assertFalse;
10+
import static org.junit.jupiter.api.Assertions.assertTrue;
11+
12+
record TestSource(){}
13+
14+
class GuiEventTest {
15+
private static final List<String> TARGET_GROUPS = List.of("A", "B");
16+
private static final List<String> MATCHING_GROUPS = Collections.singletonList("B");
17+
private static final List<String> OTHER_GROUPS = Collections.singletonList("C");
18+
19+
private GuiEvent event;
20+
21+
@BeforeEach
22+
void setUp() {
23+
event = new GuiEvent(new TestSource(), TARGET_GROUPS, null);
24+
}
25+
26+
@Test
27+
void eventIsRelevantForCollectionContainingGroupTest() {
28+
var isRelevant = event.relevantForTargetGroups(MATCHING_GROUPS);
29+
30+
assertTrue(isRelevant);
31+
}
32+
33+
@Test
34+
void eventIsRelevantWhenNoGroupsAreSet() {
35+
var eventWithoutGroups = new GuiEvent(new TestSource(), null, null);
36+
37+
var isRelevant = eventWithoutGroups.relevantForTargetGroups(MATCHING_GROUPS);
38+
39+
assertTrue(isRelevant);
40+
}
41+
42+
@Test
43+
void eventIsNotRelevantWhenNoGroupsAreMatching() {
44+
var isRelevant = event.relevantForTargetGroups(OTHER_GROUPS);
45+
46+
assertFalse(isRelevant);
47+
}
48+
}

0 commit comments

Comments
 (0)