LoggingRequestHelperImpl implements LoggingRequestHelper { @Override public LogLevelQueryResponse sendLogLevelQueryRequest(String componentName, String hostName) { try { URI logLevelQueryURI = createLogLevelQueryURI(protocol, componentName, hostName); LOG.debug("Attempting to connect to LogSearch server at " + logLevelQueryURI); HttpURLConnection httpURLConnection = (HttpURLConnection) logLevelQueryURI.toURL().openConnection(); secure(httpURLConnection, protocol); httpURLConnection.setRequestMethod("GET"); addCookiesFromCookieStore(httpURLConnection); setupCredentials(httpURLConnection); StringBuffer buffer = networkConnection.readQueryResponseFromServer(httpURLConnection); addCookiesToCookieStoreFromResponse(httpURLConnection); StringReader stringReader = new StringReader(buffer.toString()); ObjectReader logQueryResponseReader = createObjectReader(LogLevelQueryResponse.class); return logQueryResponseReader.readValue(stringReader); } catch (Exception e) { Utils.logErrorMessageWithThrowableWithCounter(LOG, errorLogCounterForLogSearchConnectionExceptions, "Error occurred while trying to connect to the LogSearch service...", e); } return null; } 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 testLogLevelRequestBasic() throws Exception { LoggingCookieStore.INSTANCE.getCookiesMap().clear(); 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_LOG_LEVEL_QUERY)).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); LogLevelQueryResponse result = helper.sendLogLevelQueryRequest("hdfs_datanode", 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()); assertNotNull("Response object should not be null", result); assertEquals("startIndex not parsed properly", "0", result.getStartIndex()); assertEquals("pageSize not parsed properly", "0", result.getPageSize()); assertEquals("totalCount not parsed properly", "0", result.getTotalCount()); assertEquals("resultSize not parsed properly", "6", result.getResultSize()); assertEquals("queryTimeMS not parsed properly", "1459970731998", result.getQueryTimeMS()); assertEquals("Incorrect number of log level count items parsed", 6, result.getNameValueList().size()); List<NameValuePair> resultList = result.getNameValueList(); assertNameValuePair("FATAL", "0", resultList.get(0)); assertNameValuePair("ERROR", "0", resultList.get(1)); assertNameValuePair("WARN", "41", resultList.get(2)); assertNameValuePair("INFO", "186", resultList.get(3)); assertNameValuePair("DEBUG", "0", resultList.get(4)); assertNameValuePair("TRACE", "0", resultList.get(5)); mockSupport.verifyAll(); }