|
1 | 1 | // SPDX-License-Identifier: MPL-2.0 |
2 | 2 | // SPDX-FileCopyrightText: 2025 Richard Ogin |
| 3 | +// SPDX-FileCopyrightText: 2025 Tony Germano <[email protected]> |
3 | 4 |
|
4 | 5 | package com.mirth.connect.server.userutil; |
5 | 6 |
|
6 | 7 | import static org.junit.Assert.assertFalse; |
7 | 8 | import static org.junit.Assert.assertTrue; |
8 | 9 |
|
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.Collection; |
9 | 12 | import java.util.HashMap; |
10 | 13 | import java.util.HashSet; |
11 | 14 | import java.util.Map; |
@@ -43,17 +46,136 @@ private Map<String, Integer> createDestinationIdMap() { |
43 | 46 | private ImmutableConnectorMessage createMessage(Map<String, Integer> destinationIdMap, Set<Integer> metadataIds) { |
44 | 47 | ConnectorMessage cm = new ConnectorMessage(); |
45 | 48 |
|
46 | | - if(metadataIds != null) { |
| 49 | + if (metadataIds != null) { |
47 | 50 | cm.getSourceMap().put(Constants.DESTINATION_SET_KEY, metadataIds); |
48 | 51 | } |
49 | 52 |
|
50 | | - if(destinationIdMap != null) { |
51 | | - return new ImmutableConnectorMessage(cm,true, destinationIdMap); |
| 53 | + if (destinationIdMap != null) { |
| 54 | + return new ImmutableConnectorMessage(cm, true, destinationIdMap); |
52 | 55 | } else { |
53 | 56 | return new ImmutableConnectorMessage(cm, true); |
54 | 57 | } |
55 | 58 | } |
56 | 59 |
|
| 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) --- |
57 | 179 | @Test |
58 | 180 | public void test_removeAllExceptObject_withSourceMap_removeAllForMetadataIdWhichDoesNotExist() throws Exception { |
59 | 181 | Set<Integer> metaDataIds = createMetadataIds(); |
|
0 commit comments