|
| 1 | +// SPDX-License-Identifier: MPL-2.0 |
| 2 | +// SPDX-FileCopyrightText: 2025 Richard Ogin |
| 3 | +// SPDX-FileCopyrightText: 2025 Tony Germano <[email protected]> |
| 4 | + |
| 5 | +package com.mirth.connect.server.userutil; |
| 6 | + |
| 7 | +import static org.junit.Assert.assertFalse; |
| 8 | +import static org.junit.Assert.assertTrue; |
| 9 | + |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.Collection; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.HashSet; |
| 14 | +import java.util.Map; |
| 15 | +import java.util.Set; |
| 16 | + |
| 17 | +import org.junit.Test; |
| 18 | + |
| 19 | +import com.mirth.connect.donkey.model.message.ConnectorMessage; |
| 20 | +import com.mirth.connect.donkey.server.Constants; |
| 21 | +import com.mirth.connect.userutil.ImmutableConnectorMessage; |
| 22 | + |
| 23 | +public class DestinationSetTest { |
| 24 | + private Set<Integer> createMetadataIds() { |
| 25 | + Set<Integer> metaDataIds = new HashSet<>(); |
| 26 | + metaDataIds.add(1); |
| 27 | + metaDataIds.add(3); |
| 28 | + metaDataIds.add(5); |
| 29 | + metaDataIds.add(7); |
| 30 | + metaDataIds.add(9); |
| 31 | + |
| 32 | + return metaDataIds; |
| 33 | + } |
| 34 | + |
| 35 | + private Map<String, Integer> createDestinationIdMap() { |
| 36 | + Map<String, Integer> destinationIdMap = new HashMap<>(); |
| 37 | + destinationIdMap.put("One", 1); |
| 38 | + destinationIdMap.put("Three", 3); |
| 39 | + destinationIdMap.put("Five", 5); |
| 40 | + destinationIdMap.put("Seven", 7); |
| 41 | + destinationIdMap.put("Nine", 9); |
| 42 | + |
| 43 | + return destinationIdMap; |
| 44 | + } |
| 45 | + |
| 46 | + private ImmutableConnectorMessage createMessage(Map<String, Integer> destinationIdMap, Set<Integer> metadataIds) { |
| 47 | + ConnectorMessage cm = new ConnectorMessage(); |
| 48 | + |
| 49 | + if (metadataIds != null) { |
| 50 | + cm.getSourceMap().put(Constants.DESTINATION_SET_KEY, metadataIds); |
| 51 | + } |
| 52 | + |
| 53 | + if (destinationIdMap != null) { |
| 54 | + return new ImmutableConnectorMessage(cm, true, destinationIdMap); |
| 55 | + } else { |
| 56 | + return new ImmutableConnectorMessage(cm, true); |
| 57 | + } |
| 58 | + } |
| 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) --- |
| 179 | + @Test |
| 180 | + public void test_removeAllExceptObject_withSourceMap_removeAllForMetadataIdWhichDoesNotExist() throws Exception { |
| 181 | + Set<Integer> metaDataIds = createMetadataIds(); |
| 182 | + DestinationSet destinationSet = new DestinationSet(createMessage(createDestinationIdMap(), metaDataIds)); |
| 183 | + |
| 184 | + assertTrue(destinationSet.removeAllExcept("I_DONT_EXIST")); |
| 185 | + assertTrue(metaDataIds.isEmpty()); |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + public void test_removeAllExceptObject_withSourceMap_removeForMatchingMetadataId() throws Exception { |
| 190 | + Set<Integer> metaDataIds = createMetadataIds(); |
| 191 | + DestinationSet destinationSet = new DestinationSet(createMessage(createDestinationIdMap(), metaDataIds)); |
| 192 | + |
| 193 | + assertTrue(destinationSet.removeAllExcept(3)); |
| 194 | + assertTrue(metaDataIds.size() == 1); |
| 195 | + } |
| 196 | + |
| 197 | + @Test |
| 198 | + public void test_removeAllExceptObject_withSourceMap_removeForMatchingConnectorName() throws Exception { |
| 199 | + Set<Integer> metaDataIds = createMetadataIds(); |
| 200 | + DestinationSet destinationSet = new DestinationSet(createMessage(createDestinationIdMap(), metaDataIds)); |
| 201 | + |
| 202 | + assertTrue(destinationSet.removeAllExcept("Seven")); |
| 203 | + assertTrue(metaDataIds.size() == 1); |
| 204 | + } |
| 205 | + |
| 206 | + @Test |
| 207 | + public void test_removeAllExceptObject_noSourceMap_noRemovalForMetadataIdWhichDoesNotExist() throws Exception { |
| 208 | + DestinationSet destinationSet = new DestinationSet(createMessage(createDestinationIdMap(), null)); |
| 209 | + |
| 210 | + assertFalse(destinationSet.removeAllExcept("I_DONT_EXIST")); |
| 211 | + } |
| 212 | + |
| 213 | + @Test |
| 214 | + public void test_removeAllExceptObject_noSourceMap_noRemovalForMatchingMetadataId() throws Exception { |
| 215 | + DestinationSet destinationSet = new DestinationSet(createMessage(createDestinationIdMap(), null)); |
| 216 | + |
| 217 | + assertFalse(destinationSet.removeAllExcept(3)); |
| 218 | + } |
| 219 | + |
| 220 | + @Test |
| 221 | + public void test_removeAllExceptObject_noSourceMap_noRemovalForMatchingConnectorName() throws Exception { |
| 222 | + DestinationSet destinationSet = new DestinationSet(createMessage(createDestinationIdMap(), null)); |
| 223 | + |
| 224 | + assertFalse(destinationSet.removeAllExcept("Seven")); |
| 225 | + } |
| 226 | +} |
0 commit comments