|
29 | 29 | import java.io.UnsupportedEncodingException; |
30 | 30 | import java.net.URLEncoder; |
31 | 31 | import java.util.Collections; |
| 32 | +import java.util.HashMap; |
32 | 33 | import java.util.LinkedList; |
33 | 34 | import java.util.List; |
| 35 | +import java.util.Map; |
34 | 36 |
|
35 | 37 | import static org.junit.jupiter.api.Assertions.assertEquals; |
36 | 38 | import static org.junit.jupiter.api.Assertions.assertFalse; |
| 39 | +import static org.junit.jupiter.api.Assertions.assertNotSame; |
37 | 40 | import static org.junit.jupiter.api.Assertions.assertThrows; |
38 | 41 | import static org.junit.jupiter.api.Assertions.assertTrue; |
39 | 42 |
|
@@ -202,4 +205,197 @@ void testGetKeyWithoutClusters() { |
202 | 205 | ServiceInfo serviceInfo3 = new ServiceInfo("group@@name@@cluster", ""); |
203 | 206 | assertEquals("group@@name@@cluster", serviceInfo3.getKeyWithoutClusters()); |
204 | 207 | } |
| 208 | + |
| 209 | + @Test |
| 210 | + void testCloneBasicFields() { |
| 211 | + // Setup original ServiceInfo with all fields |
| 212 | + ServiceInfo original = new ServiceInfo("testGroup@@testName", "testClusters"); |
| 213 | + original.setCacheMillis(2000L); |
| 214 | + original.setLastRefTime(1234567890L); |
| 215 | + original.setChecksum("testChecksum"); |
| 216 | + original.setAllIps(true); |
| 217 | + original.setReachProtectionThreshold(true); |
| 218 | + original.setJsonFromServer("testJson"); |
| 219 | + |
| 220 | + // Clone |
| 221 | + ServiceInfo cloned = original.clone(); |
| 222 | + |
| 223 | + // Verify it's a different object |
| 224 | + assertNotSame(original, cloned); |
| 225 | + |
| 226 | + // Verify all basic fields are copied |
| 227 | + assertEquals(original.getName(), cloned.getName()); |
| 228 | + assertEquals(original.getGroupName(), cloned.getGroupName()); |
| 229 | + assertEquals(original.getClusters(), cloned.getClusters()); |
| 230 | + assertEquals(original.getCacheMillis(), cloned.getCacheMillis()); |
| 231 | + assertEquals(original.getLastRefTime(), cloned.getLastRefTime()); |
| 232 | + assertEquals(original.getChecksum(), cloned.getChecksum()); |
| 233 | + assertEquals(original.isAllIps(), cloned.isAllIps()); |
| 234 | + assertEquals(original.isReachProtectionThreshold(), cloned.isReachProtectionThreshold()); |
| 235 | + assertEquals(original.getJsonFromServer(), cloned.getJsonFromServer()); |
| 236 | + } |
| 237 | + |
| 238 | + @Test |
| 239 | + void testCloneWithNullHosts() { |
| 240 | + final ServiceInfo original = new ServiceInfo("testGroup@@testName", "testClusters"); |
| 241 | + original.setHosts(null); |
| 242 | + |
| 243 | + ServiceInfo cloned = original.clone(); |
| 244 | + |
| 245 | + assertNotSame(original, cloned); |
| 246 | + // Clone method initializes hosts to empty list even if original is null |
| 247 | + assertTrue(cloned.getHosts().isEmpty()); |
| 248 | + } |
| 249 | + |
| 250 | + @Test |
| 251 | + void testCloneWithEmptyHosts() { |
| 252 | + ServiceInfo original = new ServiceInfo("testGroup@@testName", "testClusters"); |
| 253 | + original.setHosts(new LinkedList<>()); |
| 254 | + |
| 255 | + ServiceInfo cloned = original.clone(); |
| 256 | + |
| 257 | + assertNotSame(original, cloned); |
| 258 | + assertNotSame(original.getHosts(), cloned.getHosts()); |
| 259 | + assertTrue(cloned.getHosts().isEmpty()); |
| 260 | + } |
| 261 | + |
| 262 | + @Test |
| 263 | + void testCloneWithHosts() { |
| 264 | + // Setup original ServiceInfo with hosts |
| 265 | + final ServiceInfo original = new ServiceInfo("testGroup@@testName", "testClusters"); |
| 266 | + |
| 267 | + Instance instance1 = new Instance(); |
| 268 | + instance1.setInstanceId("instance1"); |
| 269 | + instance1.setIp("192.168.1.1"); |
| 270 | + instance1.setPort(8080); |
| 271 | + instance1.setWeight(1.0); |
| 272 | + instance1.setHealthy(true); |
| 273 | + instance1.setEnabled(true); |
| 274 | + instance1.setEphemeral(true); |
| 275 | + instance1.setClusterName("cluster1"); |
| 276 | + instance1.setServiceName("service1"); |
| 277 | + |
| 278 | + Instance instance2 = new Instance(); |
| 279 | + instance2.setInstanceId("instance2"); |
| 280 | + instance2.setIp("192.168.1.2"); |
| 281 | + instance2.setPort(8081); |
| 282 | + instance2.setWeight(2.0); |
| 283 | + instance2.setHealthy(false); |
| 284 | + instance2.setEnabled(false); |
| 285 | + instance2.setEphemeral(false); |
| 286 | + instance2.setClusterName("cluster2"); |
| 287 | + instance2.setServiceName("service2"); |
| 288 | + |
| 289 | + original.addHost(instance1); |
| 290 | + original.addHost(instance2); |
| 291 | + |
| 292 | + // Clone |
| 293 | + ServiceInfo cloned = original.clone(); |
| 294 | + |
| 295 | + // Verify it's a different object |
| 296 | + assertNotSame(original, cloned); |
| 297 | + |
| 298 | + // Verify hosts list is different |
| 299 | + assertNotSame(original.getHosts(), cloned.getHosts()); |
| 300 | + assertEquals(original.getHosts().size(), cloned.getHosts().size()); |
| 301 | + |
| 302 | + // Verify each host is a different object but with same values |
| 303 | + for (int i = 0; i < original.getHosts().size(); i++) { |
| 304 | + Instance originalHost = original.getHosts().get(i); |
| 305 | + Instance clonedHost = cloned.getHosts().get(i); |
| 306 | + |
| 307 | + assertNotSame(originalHost, clonedHost); |
| 308 | + assertEquals(originalHost.getInstanceId(), clonedHost.getInstanceId()); |
| 309 | + assertEquals(originalHost.getIp(), clonedHost.getIp()); |
| 310 | + assertEquals(originalHost.getPort(), clonedHost.getPort()); |
| 311 | + assertEquals(originalHost.getWeight(), clonedHost.getWeight()); |
| 312 | + assertEquals(originalHost.isHealthy(), clonedHost.isHealthy()); |
| 313 | + assertEquals(originalHost.isEnabled(), clonedHost.isEnabled()); |
| 314 | + assertEquals(originalHost.isEphemeral(), clonedHost.isEphemeral()); |
| 315 | + assertEquals(originalHost.getClusterName(), clonedHost.getClusterName()); |
| 316 | + assertEquals(originalHost.getServiceName(), clonedHost.getServiceName()); |
| 317 | + } |
| 318 | + } |
| 319 | + |
| 320 | + @Test |
| 321 | + void testCloneWithHostsMetadata() { |
| 322 | + final ServiceInfo original = new ServiceInfo("testGroup@@testName", "testClusters"); |
| 323 | + |
| 324 | + Instance instance = new Instance(); |
| 325 | + instance.setIp("192.168.1.1"); |
| 326 | + instance.setPort(8080); |
| 327 | + |
| 328 | + Map<String, String> metadata = new HashMap<>(); |
| 329 | + metadata.put("key1", "value1"); |
| 330 | + metadata.put("key2", "value2"); |
| 331 | + instance.setMetadata(metadata); |
| 332 | + |
| 333 | + original.addHost(instance); |
| 334 | + |
| 335 | + // Clone |
| 336 | + ServiceInfo cloned = original.clone(); |
| 337 | + |
| 338 | + // Verify metadata is deep copied |
| 339 | + Instance originalHost = original.getHosts().get(0); |
| 340 | + Instance clonedHost = cloned.getHosts().get(0); |
| 341 | + |
| 342 | + assertNotSame(originalHost.getMetadata(), clonedHost.getMetadata()); |
| 343 | + assertEquals(originalHost.getMetadata(), clonedHost.getMetadata()); |
| 344 | + assertEquals(originalHost.getMetadata().size(), clonedHost.getMetadata().size()); |
| 345 | + assertEquals("value1", clonedHost.getMetadata().get("key1")); |
| 346 | + assertEquals("value2", clonedHost.getMetadata().get("key2")); |
| 347 | + } |
| 348 | + |
| 349 | + @Test |
| 350 | + void testCloneWithHostsNullMetadata() { |
| 351 | + final ServiceInfo original = new ServiceInfo("testGroup@@testName", "testClusters"); |
| 352 | + |
| 353 | + Instance instance = new Instance(); |
| 354 | + instance.setIp("192.168.1.1"); |
| 355 | + instance.setPort(8080); |
| 356 | + instance.setMetadata(null); |
| 357 | + |
| 358 | + original.addHost(instance); |
| 359 | + |
| 360 | + ServiceInfo cloned = original.clone(); |
| 361 | + |
| 362 | + Instance clonedHost = cloned.getHosts().get(0); |
| 363 | + // Instance metadata is initialized to empty HashMap by default |
| 364 | + // When clone method doesn't set metadata (because original is null), |
| 365 | + // the cloned Instance keeps its default empty HashMap |
| 366 | + assertTrue(clonedHost.getMetadata() != null && clonedHost.getMetadata().isEmpty()); |
| 367 | + } |
| 368 | + |
| 369 | + @Test |
| 370 | + void testCloneModificationDoesNotAffectOriginal() { |
| 371 | + ServiceInfo original = new ServiceInfo("testGroup@@testName", "testClusters"); |
| 372 | + original.setCacheMillis(1000L); |
| 373 | + original.setAllIps(false); |
| 374 | + |
| 375 | + Instance instance = new Instance(); |
| 376 | + instance.setIp("192.168.1.1"); |
| 377 | + instance.setPort(8080); |
| 378 | + Map<String, String> metadata = new HashMap<>(); |
| 379 | + metadata.put("key1", "value1"); |
| 380 | + instance.setMetadata(metadata); |
| 381 | + original.addHost(instance); |
| 382 | + |
| 383 | + // Clone |
| 384 | + ServiceInfo cloned = original.clone(); |
| 385 | + |
| 386 | + // Modify cloned object |
| 387 | + cloned.setCacheMillis(2000L); |
| 388 | + cloned.setAllIps(true); |
| 389 | + cloned.setName("modifiedName"); |
| 390 | + cloned.getHosts().get(0).setIp("10.0.0.1"); |
| 391 | + cloned.getHosts().get(0).getMetadata().put("key2", "value2"); |
| 392 | + |
| 393 | + // Verify original is not affected |
| 394 | + assertEquals(1000L, original.getCacheMillis()); |
| 395 | + assertFalse(original.isAllIps()); |
| 396 | + assertEquals("testGroup@@testName", original.getName()); |
| 397 | + assertEquals("192.168.1.1", original.getHosts().get(0).getIp()); |
| 398 | + assertEquals(1, original.getHosts().get(0).getMetadata().size()); |
| 399 | + assertFalse(original.getHosts().get(0).getMetadata().containsKey("key2")); |
| 400 | + } |
205 | 401 | } |
0 commit comments