DeviceNetworkAddressCleanupService { public void clearDuplicateAddresses(final String deviceIdentification, final String host) throws UnknownHostException { this.clearDuplicateAddresses(deviceIdentification, InetAddress.getByName(host)); } @Autowired  DeviceNetworkAddressCleanupService(final DeviceRepository deviceRepository,
            @Value("${device.network.address.cleanup.never}") final boolean allowMultipleDevicesPerNetworkAddress,
            @Value("#{'${device.network.address.cleanup.duplicates.allowed}'.split(',')}") final List<String> ipRangesAllowingMultipleDevicesPerAddress); static InetAddress getLocalHost(); void clearDuplicateAddresses(final String deviceIdentification, final String host); void clearDuplicateAddresses(final String deviceIdentification, final InetAddress inetAddress); boolean allowDuplicateEntries(final InetAddress inetAddress);  }
@Test public void noDevicesAreCleanedWhenDuplicateAddressesAreAllowed() throws Exception { this.withConfigurationAllowingAllDuplicates(); this.withHostAndInetAddress(IP_ADDRESS); this.deviceNetworkAddressCleanupService.clearDuplicateAddresses(DEVICE_IDENTIFICATION, this.host); verify(this.deviceRepository, never()).findByNetworkAddress(this.inetAddress); verify(this.deviceRepository, never()).save(any(Device.class)); }
@Test public void noDevicesAreCleanedWhenTheNetworkAddressIsExplicitlyAllowedWithDuplicates() throws Exception { this.withHostAndInetAddress(IP_ADDRESS); this.withConfigurationAllowingDuplicatesFor(this.host); this.deviceNetworkAddressCleanupService.clearDuplicateAddresses(DEVICE_IDENTIFICATION, this.host); verify(this.deviceRepository, never()).findByNetworkAddress(this.inetAddress); verify(this.deviceRepository, never()).save(any(Device.class)); }
@Test public void noDevicesAreCleanedWhenTheNetworkAddressIsPartOfARangeAllowingDuplicates() throws Exception { final String ipAddress = "172.16.138.27"; this.withHostAndInetAddress(ipAddress); final String rangeContainingIpAddress = "172.16.130.0-172.16.139.255"; this.withConfigurationAllowingDuplicatesFor("10.1.1.111", rangeContainingIpAddress, "192.168.0.0-192.168.255.255"); this.deviceNetworkAddressCleanupService.clearDuplicateAddresses(DEVICE_IDENTIFICATION, this.host); verify(this.deviceRepository, never()).findByNetworkAddress(this.inetAddress); verify(this.deviceRepository, never()).save(any(Device.class)); }
@Test public void localhostIsAlwaysAllowedToHaveDuplicates() throws Exception { this.withConfigurationNotAllowingDuplicates(); this.withHostAndInetAddress(LOCALHOST); this.deviceNetworkAddressCleanupService.clearDuplicateAddresses(DEVICE_IDENTIFICATION, this.host); verify(this.deviceRepository, never()).findByNetworkAddress(this.inetAddress); verify(this.deviceRepository, never()).save(any(Device.class)); }
@Test public void devicesAreCleanedWhenTheNetworkAddressIsNotAllowedToHaveDuplicates() throws Exception { this.withConfigurationNotAllowingDuplicates(); this.withHostAndInetAddress(IP_ADDRESS); this.theNetworkAddressIsUsedBy(this.inetAddress, "device1", "device2"); this.deviceNetworkAddressCleanupService.clearDuplicateAddresses(DEVICE_IDENTIFICATION, this.host); verify(this.deviceRepository, times(1)).findByNetworkAddress(this.inetAddress); final ArgumentCaptor<Device> deviceCaptor = ArgumentCaptor.forClass(Device.class); verify(this.deviceRepository, times(2)).save(deviceCaptor.capture()); final List<Device> savedDevices = deviceCaptor.getAllValues(); this.deviceIsSavedWithCleanedNetworkAddress("device1", savedDevices); this.deviceIsSavedWithCleanedNetworkAddress("device2", savedDevices); }
@Test public void noDevicesAreCleanedWhenTheNetworkAddressIsNotUsed() throws Exception { this.withConfigurationNotAllowingDuplicates(); this.withHostAndInetAddress(IP_ADDRESS); this.theNetworkAddressIsNotUsed(this.inetAddress); this.deviceNetworkAddressCleanupService.clearDuplicateAddresses(DEVICE_IDENTIFICATION, this.host); verify(this.deviceRepository, times(1)).findByNetworkAddress(this.inetAddress); verify(this.deviceRepository, never()).save(any(Device.class)); }