WilmaAdminHostsFileParser { public void parseFile() { String filePath = propertyHolder.get(WILMA_ADMIN_HOSTS_FILE); List<String> wilmaAdminHosts = new ArrayList<>(); boolean securityEnabled = !"".equals(filePath) && filePath != null; if (securityEnabled) { try (BufferedReader bufferedReader = bufferedReaderFactory.createBufferedReaderFromFilePath(filePath)) { while (bufferedReader.ready()) { String host = bufferedReader.readLine(); wilmaAdminHosts.add(host); } } catch (IOException e) { throw new CannotParseExternalResourceException(String.format(ERROR_TEMPLATE, filePath), e); } } wilmaAdminHostsDTO.setWilmaAdminHosts(wilmaAdminHosts); wilmaAdminHostsDTO.setSecurityEnabled(securityEnabled); }  void parseFile();  }
@Test public void testWhenNoFileIsGivenShouldSetWilmaAdminHostsToEmpty() { List<String> emptyList = new ArrayList<String>(); given(propertyHolder.get(WILMA_ADMIN_HOSTS_FILE)).willReturn(""); underTest.parseFile(); verify(wilmaAdminHostsDTO).setWilmaAdminHosts(emptyList); }
@Test public void testWhenNoFileIsGivenShouldDisableSecurity() { given(propertyHolder.get(WILMA_ADMIN_HOSTS_FILE)).willReturn(""); underTest.parseFile(); verify(wilmaAdminHostsDTO).setSecurityEnabled(false); }
@Test public void testWhenPropertyIsMissingShouldDisableSecurity() { given(propertyHolder.get(WILMA_ADMIN_HOSTS_FILE)).willReturn(null); underTest.parseFile(); verify(wilmaAdminHostsDTO).setSecurityEnabled(false); }
@Test public void testWhenAnExistingFileIsGivenShouldEnableSecurity() throws IOException { String filePath = "file.txt"; given(propertyHolder.get(WILMA_ADMIN_HOSTS_FILE)).willReturn(filePath); given(bufferedReaderFactory.createBufferedReaderFromFilePath(filePath)).willReturn(bufferedReader); given(bufferedReader.ready()).willReturn(false); underTest.parseFile(); verify(wilmaAdminHostsDTO).setSecurityEnabled(true); }
@Test(expectedExceptions = CannotParseExternalResourceException.class) public void testWhenGivenFileDoesNotExistShouldThrowCannotParseExternalResourceException() throws IOException { String filePath = "file.txt"; given(propertyHolder.get(WILMA_ADMIN_HOSTS_FILE)).willReturn(filePath); given(bufferedReaderFactory.createBufferedReaderFromFilePath(filePath)).willThrow( new CannotParseExternalResourceException("error", new IOException())); underTest.parseFile(); verify(wilmaAdminHostsDTO, never()).setSecurityEnabled(Mockito.anyBoolean()); verify(wilmaAdminHostsDTO, never()).setWilmaAdminHosts(Mockito.anyListOf(String.class)); }
@Test public void testWhenAnExistingFileIsGivenShouldSetAdmins() throws IOException { List<String> wilmaAdminHosts = new ArrayList<String>(); wilmaAdminHosts.add("127.0.0.1"); wilmaAdminHosts.add("otherhost"); String filePath = "file.txt"; given(propertyHolder.get(WILMA_ADMIN_HOSTS_FILE)).willReturn(filePath); given(bufferedReaderFactory.createBufferedReaderFromFilePath(filePath)).willReturn(bufferedReader); given(bufferedReader.ready()).willReturn(true).willReturn(true).willReturn(false); given(bufferedReader.readLine()).willReturn("127.0.0.1").willReturn("otherhost"); underTest.parseFile(); verify(wilmaAdminHostsDTO).setWilmaAdminHosts(wilmaAdminHosts); }