LoggingRequestHelperImpl implements LoggingRequestHelper { public Set<String> sendGetLogFileNamesRequest(String componentName, String hostName) { Map<String, String> queryParameters = new HashMap<String, String>(); queryParameters.put(HOST_QUERY_PARAMETER_NAME, hostName); queryParameters.put(COMPONENT_QUERY_PARAMETER_NAME,componentName); queryParameters.put("pageSize", "1"); LogQueryResponse response = sendQueryRequest(queryParameters); if ((response != null) && (response.getListOfResults() != null) && (!response.getListOfResults().isEmpty())) { LogLineResult lineOne = response.getListOfResults().get(0); LOG.debug("For componentName = " + componentName + ", log file name is = " + lineOne.getLogFilePath()); return Collections.singleton(lineOne.getLogFilePath()); } return Collections.emptySet(); } LoggingRequestHelperImpl(String hostName, String portNumber, String protocol, CredentialStoreService credentialStoreService, Cluster cluster); protected  LoggingRequestHelperImpl(String hostName, String portNumber, String protocol, CredentialStoreService credentialStoreService, Cluster cluster, NetworkConnection networkConnection); int getLogSearchConnectTimeoutInMilliseconds(); void setLogSearchConnectTimeoutInMilliseconds(int logSearchConnectTimeoutInMilliseconds); int getLogSearchReadTimeoutInMilliseconds(); void setLogSearchReadTimeoutInMilliseconds(int logSearchReadTimeoutInMilliseconds); LogQueryResponse sendQueryRequest(Map<String, String> queryParameters); Set<String> sendGetLogFileNamesRequest(String componentName, String hostName); @Override LogLevelQueryResponse sendLogLevelQueryRequest(String componentName, String hostName); @Override String createLogFileTailURI(String baseURI, String componentName, String hostName);  }
@Test public void testLogFileNameRequestBasic() throws Exception { LoggingCookieStore.INSTANCE.getCookiesMap().clear(); final String expectedComponentName = "hdfs_namenode"; EasyMockSupport mockSupport = new EasyMockSupport(); CredentialStoreService credentialStoreServiceMock = mockSupport.createMock(CredentialStoreService.class); Cluster clusterMock = mockSupport.createMock(Cluster.class); LoggingRequestHelperImpl.NetworkConnection networkConnectionMock = mockSupport.createMock(LoggingRequestHelperImpl.NetworkConnection.class); Config adminPropertiesConfigMock = mockSupport.createMock(Config.class); Map<String, String> testConfigProperties = new HashMap<String, String>(); testConfigProperties.put("logsearch_admin_username", "admin-user"); testConfigProperties.put("logsearch_admin_password", "admin-pwd"); testConfigProperties = Collections.unmodifiableMap(testConfigProperties); Capture<HttpURLConnection> captureURLConnection = EasyMock.newCapture(); Capture<HttpURLConnection> captureURLConnectionForAuthentication = EasyMock.newCapture(); expect(clusterMock.getDesiredConfigByType("logsearch-admin-json")).andReturn(adminPropertiesConfigMock).atLeastOnce(); expect(adminPropertiesConfigMock.getProperties()).andReturn(testConfigProperties).atLeastOnce(); expect(networkConnectionMock.readQueryResponseFromServer(capture(captureURLConnection))).andReturn(new StringBuffer(TEST_JSON_INPUT_TWO_LIST_ENTRIES)).atLeastOnce(); networkConnectionMock.setupBasicAuthentication(capture(captureURLConnectionForAuthentication), eq(EXPECTED_ENCODED_CREDENTIALS)); mockSupport.replayAll(); LoggingRequestHelper helper = new LoggingRequestHelperImpl(EXPECTED_HOST_NAME, EXPECTED_PORT_NUMBER, EXPECTED_PROTOCOL, credentialStoreServiceMock, clusterMock, networkConnectionMock); Set<String> result = helper.sendGetLogFileNamesRequest(expectedComponentName, EXPECTED_HOST_NAME); HttpURLConnection httpURLConnection = captureURLConnection.getValue(); assertEquals("URLConnection did not have the correct hostname information", EXPECTED_HOST_NAME, httpURLConnection.getURL().getHost()); assertEquals("URLConnection did not have the correct port information", EXPECTED_PORT_NUMBER, httpURLConnection.getURL().getPort() + ""); assertEquals("URLConnection did not have the expected http protocol scheme", "http", httpURLConnection.getURL().getProtocol()); assertEquals("URLConnection did not have the expected method set", "GET", httpURLConnection.getRequestMethod()); assertSame("HttpUrlConnection instances passed into NetworkConnection mock should have been the same instance", httpURLConnection, captureURLConnectionForAuthentication.getValue()); final String resultQuery = httpURLConnection.getURL().getQuery(); assertTrue("host_name parameter was not included in query", resultQuery.contains("host_name=c6401.ambari.apache.org")); assertTrue("component_name parameter was not included in the query", resultQuery.contains("component_name=" + expectedComponentName)); assertTrue("pageSize parameter was not included in query", resultQuery.contains("pageSize=1")); assertNotNull("Response object should not be null", result); assertEquals("Response Set was not of the expected size", 1, result.size()); assertEquals("Response did not include the expected file name", "/var/log/hadoop/hdfs/hadoop-hdfs-namenode-c6401.ambari.apache.org.log", result.iterator().next()); mockSupport.verifyAll(); }
@Test public void testLogFileNameRequestWithNullLogList() throws Exception { final String expectedComponentName = "hdfs_namenode"; EasyMockSupport mockSupport = new EasyMockSupport(); CredentialStoreService credentialStoreServiceMock = mockSupport.createMock(CredentialStoreService.class); Cluster clusterMock = mockSupport.createMock(Cluster.class); LoggingRequestHelperImpl.NetworkConnection networkConnectionMock = mockSupport.createMock(LoggingRequestHelperImpl.NetworkConnection.class); Config adminPropertiesConfigMock = mockSupport.createMock(Config.class); Map<String, String> testConfigProperties = new HashMap<String, String>(); testConfigProperties.put("logsearch_admin_username", "admin-user"); testConfigProperties.put("logsearch_admin_password", "admin-pwd"); testConfigProperties = Collections.unmodifiableMap(testConfigProperties); Capture<HttpURLConnection> captureURLConnection = new Capture<HttpURLConnection>(); Capture<HttpURLConnection> captureURLConnectionForAuthentication = new Capture<HttpURLConnection>(); expect(clusterMock.getDesiredConfigByType("logsearch-admin-json")).andReturn(adminPropertiesConfigMock).atLeastOnce(); expect(adminPropertiesConfigMock.getProperties()).andReturn(testConfigProperties).atLeastOnce(); expect(networkConnectionMock.readQueryResponseFromServer(capture(captureURLConnection))).andReturn(new StringBuffer(TEST_JSON_INPUT_NULL_LOG_LIST)).atLeastOnce(); networkConnectionMock.setupBasicAuthentication(capture(captureURLConnectionForAuthentication), eq(EXPECTED_ENCODED_CREDENTIALS)); mockSupport.replayAll(); LoggingRequestHelper helper = new LoggingRequestHelperImpl(EXPECTED_HOST_NAME, EXPECTED_PORT_NUMBER, EXPECTED_PROTOCOL, credentialStoreServiceMock, clusterMock, networkConnectionMock); Set<String> result = helper.sendGetLogFileNamesRequest(expectedComponentName, EXPECTED_HOST_NAME); HttpURLConnection httpURLConnection = captureURLConnection.getValue(); assertEquals("URLConnection did not have the correct hostname information", EXPECTED_HOST_NAME, httpURLConnection.getURL().getHost()); assertEquals("URLConnection did not have the correct port information", EXPECTED_PORT_NUMBER, httpURLConnection.getURL().getPort() + ""); assertEquals("URLConnection did not have the expected http protocol scheme", "http", httpURLConnection.getURL().getProtocol()); assertEquals("URLConnection did not have the expected method set", "GET", httpURLConnection.getRequestMethod()); assertSame("HttpUrlConnection instances passed into NetworkConnection mock should have been the same instance", httpURLConnection, captureURLConnectionForAuthentication.getValue()); final String resultQuery = httpURLConnection.getURL().getQuery(); assertTrue("host_name parameter was not included in query", resultQuery.contains("host_name=c6401.ambari.apache.org")); assertTrue("component_name parameter was not included in the query", resultQuery.contains("component_name=" + expectedComponentName)); assertTrue("pageSize parameter was not included in query", resultQuery.contains("pageSize=1")); assertNotNull("Response object should not be null", result); assertEquals("Response Set was not of the expected size, expected an empty set", 0, result.size()); mockSupport.verifyAll(); }