Skip to content

Commit e686d61

Browse files
committed
Fix DestinationSet.removeAllExcept(Object) and add additional tests
The DestinationSet.removeAllExcept(Object) method takes a destination metadata id or destination name. If it's passed a reference to a destination which does not exist, then it should remove all entries from the set. This was working correctly if a number was passed to the method which did not refer to a destination which remained in the set, but not for a name. This change ensures that when a destination name does not appear in the destination set, that all destinations are removed. Additonal tests were also added to DestinationSetTest for greater coverage. Signed-off-by: Tony Germano <[email protected]> Issue: nextgenhealthcare/connect#5875 Reported-by: Mike Hokanson <[email protected]>
1 parent fc437f5 commit e686d61

File tree

2 files changed

+129
-7
lines changed

2 files changed

+129
-7
lines changed

server/src/com/mirth/connect/server/userutil/DestinationSet.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ public boolean removeAllExcept(Object metaDataIdOrConnectorName) {
101101
if (metaDataIds != null) {
102102
Integer metaDataId = convertToMetaDataId(metaDataIdOrConnectorName);
103103

104-
if (metaDataId != null) {
105-
return metaDataIds.retainAll(Collections.singleton(metaDataId));
106-
}
104+
Set<Integer> set = (metaDataId != null) ? Collections.singleton(metaDataId) : Collections.emptySet();
105+
106+
return metaDataIds.retainAll(set);
107107
}
108108

109109
return false;
@@ -165,4 +165,4 @@ private Integer convertToMetaDataId(Object metaDataIdOrConnectorName) {
165165

166166
return null;
167167
}
168-
}
168+
}

server/test/com/mirth/connect/server/userutil/DestinationSetTest.java

Lines changed: 125 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
// SPDX-License-Identifier: MPL-2.0
22
// SPDX-FileCopyrightText: 2025 Richard Ogin
3+
// SPDX-FileCopyrightText: 2025 Tony Germano <[email protected]>
34

45
package com.mirth.connect.server.userutil;
56

67
import static org.junit.Assert.assertFalse;
78
import static org.junit.Assert.assertTrue;
89

10+
import java.util.Arrays;
11+
import java.util.Collection;
912
import java.util.HashMap;
1013
import java.util.HashSet;
1114
import java.util.Map;
@@ -43,17 +46,136 @@ private Map<String, Integer> createDestinationIdMap() {
4346
private ImmutableConnectorMessage createMessage(Map<String, Integer> destinationIdMap, Set<Integer> metadataIds) {
4447
ConnectorMessage cm = new ConnectorMessage();
4548

46-
if(metadataIds != null) {
49+
if (metadataIds != null) {
4750
cm.getSourceMap().put(Constants.DESTINATION_SET_KEY, metadataIds);
4851
}
4952

50-
if(destinationIdMap != null) {
51-
return new ImmutableConnectorMessage(cm,true, destinationIdMap);
53+
if (destinationIdMap != null) {
54+
return new ImmutableConnectorMessage(cm, true, destinationIdMap);
5255
} else {
5356
return new ImmutableConnectorMessage(cm, true);
5457
}
5558
}
5659

60+
// --- Tests for remove(Object) ---
61+
@Test
62+
public void test_removeObject_withSourceMap_removeForMatchingMetadataId() throws Exception {
63+
Set<Integer> metaDataIds = createMetadataIds();
64+
DestinationSet destinationSet = new DestinationSet(createMessage(createDestinationIdMap(), metaDataIds));
65+
66+
assertTrue(destinationSet.remove(3));
67+
assertFalse(metaDataIds.contains(3));
68+
assertTrue(metaDataIds.size() == 4);
69+
}
70+
71+
@Test
72+
public void test_removeObject_withSourceMap_removeForMatchingConnectorName() throws Exception {
73+
Set<Integer> metaDataIds = createMetadataIds();
74+
DestinationSet destinationSet = new DestinationSet(createMessage(createDestinationIdMap(), metaDataIds));
75+
76+
assertTrue(destinationSet.remove("Five"));
77+
assertFalse(metaDataIds.contains(5));
78+
assertTrue(metaDataIds.size() == 4);
79+
}
80+
81+
@Test
82+
public void test_removeObject_withSourceMap_noRemovalForNonExistentName() throws Exception {
83+
Set<Integer> metaDataIds = createMetadataIds();
84+
DestinationSet destinationSet = new DestinationSet(createMessage(createDestinationIdMap(), metaDataIds));
85+
86+
assertFalse(destinationSet.remove("I_DONT_EXIST"));
87+
assertTrue(metaDataIds.size() == 5);
88+
}
89+
90+
@Test
91+
public void test_removeObject_noSourceMap_noRemoval() throws Exception {
92+
DestinationSet destinationSet = new DestinationSet(createMessage(createDestinationIdMap(), null));
93+
assertFalse(destinationSet.remove(3));
94+
}
95+
96+
// --- Tests for remove(Collection) ---
97+
@Test
98+
public void test_removeCollection_withSourceMap_removeForMatchingItems() throws Exception {
99+
Set<Integer> metaDataIds = createMetadataIds();
100+
DestinationSet destinationSet = new DestinationSet(createMessage(createDestinationIdMap(), metaDataIds));
101+
102+
Collection<Object> toRemove = Arrays.asList(1, "Three", 99); // "99" does not exist
103+
assertTrue(destinationSet.remove(toRemove));
104+
assertFalse(metaDataIds.contains(1));
105+
assertFalse(metaDataIds.contains(3));
106+
assertTrue(metaDataIds.size() == 3);
107+
}
108+
109+
@Test
110+
public void test_removeCollection_withSourceMap_noRemovalForNonExistentItems() throws Exception {
111+
Set<Integer> metaDataIds = createMetadataIds();
112+
DestinationSet destinationSet = new DestinationSet(createMessage(createDestinationIdMap(), metaDataIds));
113+
114+
Collection<Object> toRemove = Arrays.asList(100, "OneHundred");
115+
assertFalse(destinationSet.remove(toRemove));
116+
assertTrue(metaDataIds.size() == 5);
117+
}
118+
119+
@Test
120+
public void test_removeCollection_noSourceMap_noRemoval() throws Exception {
121+
DestinationSet destinationSet = new DestinationSet(createMessage(createDestinationIdMap(), null));
122+
assertFalse(destinationSet.remove(Arrays.asList(1, "Three")));
123+
}
124+
125+
// --- Tests for removeAll() ---
126+
@Test
127+
public void test_removeAll_withSourceMap_removesAll() throws Exception {
128+
Set<Integer> metaDataIds = createMetadataIds();
129+
DestinationSet destinationSet = new DestinationSet(createMessage(createDestinationIdMap(), metaDataIds));
130+
131+
assertTrue(destinationSet.removeAll());
132+
assertTrue(metaDataIds.isEmpty());
133+
}
134+
135+
@Test
136+
public void test_removeAll_withSourceMap_returnsFalseWhenAlreadyEmpty() throws Exception {
137+
Set<Integer> metaDataIds = new HashSet<>();
138+
DestinationSet destinationSet = new DestinationSet(createMessage(createDestinationIdMap(), metaDataIds));
139+
140+
assertFalse(destinationSet.removeAll());
141+
}
142+
143+
@Test
144+
public void test_removeAll_noSourceMap_noRemoval() throws Exception {
145+
DestinationSet destinationSet = new DestinationSet(createMessage(createDestinationIdMap(), null));
146+
assertFalse(destinationSet.removeAll());
147+
}
148+
149+
// --- Tests for removeAllExcept(Collection) ---
150+
@Test
151+
public void test_removeAllExceptCollection_withSourceMap_retainsMatchingItems() throws Exception {
152+
Set<Integer> metaDataIds = createMetadataIds();
153+
DestinationSet destinationSet = new DestinationSet(createMessage(createDestinationIdMap(), metaDataIds));
154+
155+
Collection<Object> toRetain = Arrays.asList(1, "Five", 99); // "99" does not exist
156+
assertTrue(destinationSet.removeAllExcept(toRetain));
157+
assertTrue(metaDataIds.size() == 2);
158+
assertTrue(metaDataIds.contains(1));
159+
assertTrue(metaDataIds.contains(5));
160+
}
161+
162+
@Test
163+
public void test_removeAllExceptCollection_withSourceMap_removesAllForNonExistentItems() throws Exception {
164+
Set<Integer> metaDataIds = createMetadataIds();
165+
DestinationSet destinationSet = new DestinationSet(createMessage(createDestinationIdMap(), metaDataIds));
166+
167+
Collection<Object> toRetain = Arrays.asList(100, "OneHundred");
168+
assertTrue(destinationSet.removeAllExcept(toRetain));
169+
assertTrue(metaDataIds.isEmpty());
170+
}
171+
172+
@Test
173+
public void test_removeAllExceptCollection_noSourceMap_noRemoval() throws Exception {
174+
DestinationSet destinationSet = new DestinationSet(createMessage(createDestinationIdMap(), null));
175+
assertFalse(destinationSet.removeAllExcept(Arrays.asList(1, "Five")));
176+
}
177+
178+
// --- Tests for removeAllExcept(Object) ---
57179
@Test
58180
public void test_removeAllExceptObject_withSourceMap_removeAllForMetadataIdWhichDoesNotExist() throws Exception {
59181
Set<Integer> metaDataIds = createMetadataIds();

0 commit comments

Comments
 (0)