LoggingRequestHelperImpl implements LoggingRequestHelper { public LogQueryResponse sendQueryRequest(Map<String, String> queryParameters) { try { URI logSearchURI = createLogSearchQueryURI(protocol, queryParameters); LOG.debug("Attempting to connect to LogSearch server at " + logSearchURI); HttpURLConnection httpURLConnection = (HttpURLConnection) logSearchURI.toURL().openConnection(); secure(httpURLConnection, protocol); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setConnectTimeout(logSearchConnectTimeoutInMilliseconds); httpURLConnection.setReadTimeout(logSearchReadTimeoutInMilliseconds); addCookiesFromCookieStore(httpURLConnection); LOG.debug("Attempting request to LogSearch Portal Server, with connect timeout = {} milliseconds and read timeout = {} milliseconds", logSearchConnectTimeoutInMilliseconds, logSearchReadTimeoutInMilliseconds); setupCredentials(httpURLConnection); StringBuffer buffer = networkConnection.readQueryResponseFromServer(httpURLConnection); addCookiesToCookieStoreFromResponse(httpURLConnection); StringReader stringReader = new StringReader(buffer.toString()); ObjectReader logQueryResponseReader = createObjectReader(LogQueryResponse.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 testLogQueryRequestBasic() 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", EXPECTED_USER_NAME); testConfigProperties.put("logsearch_admin_password", EXPECTED_ADMIN_PASSWORD); 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); LogQueryResponse result = helper.sendQueryRequest(Collections.<String,String>emptyMap()); 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", "5", result.getPageSize()); assertEquals("totalCount not parsed properly", "10452", result.getTotalCount()); assertEquals("resultSize not parsed properly", "5", result.getResultSize()); assertEquals("queryTimeMS not parsed properly", "1458148754113", result.getQueryTimeMS()); assertEquals("incorrect number of LogLineResult items parsed", 2, result.getListOfResults().size()); List<LogLineResult> listOfLineResults = result.getListOfResults(); verifyFirstLine(listOfLineResults); verifySecondLine(listOfLineResults); mockSupport.verifyAll(); }
@Test public void testLogQueryRequestBasicCredentialsNotInConfig() throws Exception { LoggingCookieStore.INSTANCE.getCookiesMap().clear(); final String expectedClusterName = "my-test-cluster"; 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); Capture<HttpURLConnection> captureURLConnection = EasyMock.newCapture(); Capture<HttpURLConnection> captureURLConnectionForAuthentication = EasyMock.newCapture(); expect(clusterMock.getDesiredConfigByType("logsearch-admin-json")).andReturn(adminPropertiesConfigMock).atLeastOnce(); expect(clusterMock.getClusterName()).andReturn(expectedClusterName).atLeastOnce(); expect(adminPropertiesConfigMock.getProperties()).andReturn(Collections.<String, String>emptyMap()).atLeastOnce(); expect(networkConnectionMock.readQueryResponseFromServer(capture(captureURLConnection))).andReturn(new StringBuffer(TEST_JSON_INPUT_TWO_LIST_ENTRIES)).atLeastOnce(); expect(credentialStoreServiceMock.getCredential(expectedClusterName, "logsearch.admin.credential")).andReturn(new PrincipalKeyCredential(EXPECTED_USER_NAME, EXPECTED_ADMIN_PASSWORD)).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); LogQueryResponse result = helper.sendQueryRequest(Collections.<String,String>emptyMap()); 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", "5", result.getPageSize()); assertEquals("totalCount not parsed properly", "10452", result.getTotalCount()); assertEquals("resultSize not parsed properly", "5", result.getResultSize()); assertEquals("queryTimeMS not parsed properly", "1458148754113", result.getQueryTimeMS()); assertEquals("incorrect number of LogLineResult items parsed", 2, result.getListOfResults().size()); List<LogLineResult> listOfLineResults = result.getListOfResults(); verifyFirstLine(listOfLineResults); verifySecondLine(listOfLineResults); mockSupport.verifyAll(); }