|
2 | 2 | package com.swirlds.config.impl; |
3 | 3 |
|
4 | 4 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
5 | 7 | import static org.junit.jupiter.api.Assertions.assertNull; |
6 | 8 | import static org.junit.jupiter.api.Assertions.assertThrows; |
7 | 9 | import static org.junit.jupiter.api.Assertions.assertTrue; |
8 | 10 |
|
| 11 | +import com.swirlds.config.api.ConfigData; |
| 12 | +import com.swirlds.config.api.ConfigProperty; |
9 | 13 | import com.swirlds.config.api.Configuration; |
10 | 14 | import com.swirlds.config.api.ConfigurationBuilder; |
11 | 15 | import com.swirlds.config.extensions.sources.SimpleConfigSource; |
| 16 | +import java.net.InetAddress; |
| 17 | +import java.net.UnknownHostException; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Iterator; |
12 | 20 | import java.util.List; |
13 | 21 | import java.util.NoSuchElementException; |
14 | 22 | import java.util.Set; |
@@ -114,4 +122,146 @@ void testNotDefinedEmptySet() { |
114 | 122 | assertThrows(NoSuchElementException.class, () -> configuration.getValueSet("sample.list", String.class)); |
115 | 123 | assertThrows(NoSuchElementException.class, () -> configuration.getValueSet("sample.list", Integer.class)); |
116 | 124 | } |
| 125 | + |
| 126 | + /** Verify if the iteration order of the set is the same as the order of items in the list. */ |
| 127 | + private static <T> void verifyIterationOrder(final Set<T> set, final List<T> list) { |
| 128 | + assertEquals(list.size(), set.size(), "The list size should be equal to the set size"); |
| 129 | + |
| 130 | + final Iterator<T> setIterator = set.iterator(); |
| 131 | + final Iterator<T> listIterator = list.iterator(); |
| 132 | + |
| 133 | + final List<T> actualOrder = new ArrayList<>(list.size()); |
| 134 | + |
| 135 | + while (listIterator.hasNext()) { |
| 136 | + assertEquals(listIterator.hasNext(), setIterator.hasNext()); |
| 137 | + |
| 138 | + final T next = setIterator.next(); |
| 139 | + actualOrder.add(next); |
| 140 | + |
| 141 | + assertEquals( |
| 142 | + listIterator.next(), |
| 143 | + next, |
| 144 | + "The set iteration order should be stable. Expected: " + list + ", actual so far: " + actualOrder); |
| 145 | + } |
| 146 | + assertFalse(setIterator.hasNext()); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + void checkIntegerSetStable() { |
| 151 | + // given |
| 152 | + final Configuration configuration = ConfigurationBuilder.create() |
| 153 | + .withSource(new SimpleConfigSource("testNumbers", "3,1,2")) |
| 154 | + .build(); |
| 155 | + |
| 156 | + // when |
| 157 | + final Set<Integer> values = configuration.getValueSet("testNumbers", Integer.class); |
| 158 | + |
| 159 | + // then |
| 160 | + verifyIterationOrder(values, List.of(1, 2, 3)); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + void checkStringSetStable() { |
| 165 | + // given |
| 166 | + final Configuration configuration = ConfigurationBuilder.create() |
| 167 | + .withSource(new SimpleConfigSource("testStrings", "x,a,d")) |
| 168 | + .build(); |
| 169 | + |
| 170 | + // when |
| 171 | + final Set<String> values = configuration.getValueSet("testStrings", String.class); |
| 172 | + |
| 173 | + // then |
| 174 | + verifyIterationOrder(values, List.of("a", "d", "x")); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + void checkEnumSetStable() { |
| 179 | + // given |
| 180 | + final Configuration configuration = ConfigurationBuilder.create() |
| 181 | + .withSource(new SimpleConfigSource("testEnums", "x,a,d")) |
| 182 | + .build(); |
| 183 | + enum TestEnum { |
| 184 | + d, |
| 185 | + x, |
| 186 | + a |
| 187 | + } |
| 188 | + |
| 189 | + // when |
| 190 | + final Set<TestEnum> values = configuration.getValueSet("testEnums", TestEnum.class); |
| 191 | + |
| 192 | + // then |
| 193 | + verifyIterationOrder(values, List.of(TestEnum.d, TestEnum.x, TestEnum.a)); |
| 194 | + } |
| 195 | + |
| 196 | + @ConfigData("settest") |
| 197 | + public record SetTestConfig(@ConfigProperty(value = "testSet", defaultValue = "666,404,500") Set<Long> testSet) {} |
| 198 | + |
| 199 | + @Test |
| 200 | + void checkSetInRecord() { |
| 201 | + // given |
| 202 | + final Configuration configuration = ConfigurationBuilder.create() |
| 203 | + .withSource(new SimpleConfigSource("settest.testSet", "333,111,222")) |
| 204 | + .withConfigDataType(SetTestConfig.class) |
| 205 | + .build(); |
| 206 | + |
| 207 | + // when |
| 208 | + final SetTestConfig setTestConfig = configuration.getConfigData(SetTestConfig.class); |
| 209 | + |
| 210 | + // then |
| 211 | + verifyIterationOrder(setTestConfig.testSet(), List.of(111L, 222L, 333L)); |
| 212 | + } |
| 213 | + |
| 214 | + @Test |
| 215 | + void checkInetAddressSet() throws UnknownHostException { |
| 216 | + // InetAddress is not Comparable: |
| 217 | + |
| 218 | + // given |
| 219 | + final Configuration configuration = ConfigurationBuilder.create() |
| 220 | + .withSource(new SimpleConfigSource("setinetaddresstest.testInetAddressSet", "1.1.1.1,2.2.2.2")) |
| 221 | + .build(); |
| 222 | + |
| 223 | + final List<InetAddress> expectedOrder = List.of( |
| 224 | + InetAddress.getByAddress(new byte[] {1, 1, 1, 1}), InetAddress.getByAddress(new byte[] {2, 2, 2, 2})); |
| 225 | + |
| 226 | + // Case #1: as a List |
| 227 | + // when |
| 228 | + final List<InetAddress> list = |
| 229 | + configuration.getValues("setinetaddresstest.testInetAddressSet", InetAddress.class); |
| 230 | + |
| 231 | + // then |
| 232 | + assertNotNull(list); |
| 233 | + assertEquals(2, list.size()); |
| 234 | + assertEquals(expectedOrder, list); |
| 235 | + |
| 236 | + // Case #2: as a Set |
| 237 | + // when/then |
| 238 | + final Set<InetAddress> set = |
| 239 | + configuration.getValueSet("setinetaddresstest.testInetAddressSet", InetAddress.class); |
| 240 | + verifyIterationOrder(set, expectedOrder); |
| 241 | + } |
| 242 | + |
| 243 | + @ConfigData("setinetaddresstest") |
| 244 | + public record SetInetAddressTestConfig( |
| 245 | + @ConfigProperty(value = "testInetAddressSet", defaultValue = "1.1.1.1,2.2.2.2") Set<InetAddress> testSet) {} |
| 246 | + |
| 247 | + @Test |
| 248 | + void checkInetAddressSetInRecord() throws UnknownHostException { |
| 249 | + final List<InetAddress> expectedOrder = List.of( |
| 250 | + InetAddress.getByAddress(new byte[] {1, 1, 1, 1}), InetAddress.getByAddress(new byte[] {2, 2, 2, 2})); |
| 251 | + |
| 252 | + // InetAddress is not Comparable: |
| 253 | + final Configuration configuration = ConfigurationBuilder.create() |
| 254 | + .withSource(new SimpleConfigSource("setinetaddresstest.testInetAddressSet", "1.1.1.1,2.2.2.2")) |
| 255 | + .withConfigDataType(SetInetAddressTestConfig.class) |
| 256 | + .build(); |
| 257 | + |
| 258 | + // case 1: getValueSet |
| 259 | + final Set<InetAddress> set = |
| 260 | + configuration.getValueSet("setinetaddresstest.testInetAddressSet", InetAddress.class); |
| 261 | + verifyIterationOrder(set, expectedOrder); |
| 262 | + |
| 263 | + // case 2: getConfigData as record |
| 264 | + final SetInetAddressTestConfig configData = configuration.getConfigData(SetInetAddressTestConfig.class); |
| 265 | + verifyIterationOrder(configData.testSet(), expectedOrder); |
| 266 | + } |
117 | 267 | } |
0 commit comments