@Test public void testExecute() throws LdapException { ApacheAPIConverter apacheAPIConverter = mock(ApacheAPIConverter.class); List<Endpoint> endpoints = new ArrayList<>(); Endpoint endpoint1 = mock(Endpoint.class); endpoints.add(endpoint1); SearchRequest searchRequest = mock(SearchRequest.class); RoundtripResult roundtripResult= mock(RoundtripResult.class); SearchCursor cursor = mock(SearchCursor.class); when(queryRequest.getEndpoints()).thenReturn(endpoints); doReturn(roundtripResult).when(queryRequestPagedExecutor).collectData(ldapConnection, searchRequest); doReturn(roundtripResult).when(queryRequestPagedExecutor).retrieveCookie(cursor); doReturn(driver).when(queryRequestPagedExecutor).driverGetInstance(); doReturn(apacheAPIConverter).when(queryRequestPagedExecutor).parserGetInstance(); when(apacheAPIConverter.toSearchRequest(queryRequest, endpoint1.getBaseSearchPath(), endpoint1.getCookie())).thenReturn(searchRequest); when(driver.connect(any(Endpoint.class))).thenReturn(ldapConnectionResult); when(ldapConnectionResult.connectionSucceeded()).thenReturn(true); when(ldapConnectionResult.getConnection()).thenReturn(ldapConnection); when(roundtripResult.getSearchCursor()).thenReturn(cursor); queryRequestPagedExecutor.execute(); verify(driver,times(endpoints.size())).connect(any(Endpoint.class)); verify(queryRequestPagedExecutor,times(endpoints.size())).collectData(any(LdapNetworkConnection.class),any(SearchRequest.class)); verify(queryRequestPagedExecutor,times(1)).retrieveCookie(cursor); verify(endpoint1, times(1)).setDestinationType(any()); }
@Override QueryResponse execute() { QueryResponse response = new QueryResponse(); for (Endpoint endpoint : this.queryRequest.getEndpoints()) { PartitionResponse partitionResponse = new PartitionResponse(); response.addPartitionResponse(partitionResponse); partitionResponse.setEndpoint(endpoint); try { LOGGER.debug("Executing request for: " + endpoint.getHost()); if (endpoint.hasNext() == CursorStatus.EOF) { LOGGER.debug("Host " + endpoint.getHost() + " skipped: EOF"); continue; } LdapConnectionResult ldapConnectionResult = driverGetInstance().connect(endpoint); String basePath = tryResolveBasePath(ldapConnectionResult.getConnection(), endpoint.getBaseSearchPath()); endpoint.setBaseSearchPath(basePath); endpoint.setDestinationType(ldapConnectionResult.getDestinationType()); partitionResponse.setStatus(ldapConnectionResult.getStatuses()); LdapConnection connection = ldapConnectionResult.getConnection(); if (ldapConnectionResult.connectionSucceeded()) { SearchRequest search = parserGetInstance().toSearchRequest(queryRequest, endpoint.getBaseSearchPath(), endpoint.getCookie()); RoundtripResult roundtripResult = collectData(connection, search); Object cookie = retrieveCookie(roundtripResult.getSearchCursor()); endpoint.setCookie(cookie); if (cookie == null) { endpoint.hasNext(CursorStatus.EOF); } else { endpoint.hasNext(CursorStatus.PAGING); } partitionResponse.setData(new ApacheAPIConverter().toEntityResponse(roundtripResult.getData(), queryRequest.getRequestedFields())); return response; } else { LOGGER.error(String.format("Could not execute query against AD {}. Connection failure!", endpoint.getHost())); endpoint.hasNext(CursorStatus.EOF); endpoint.setDestinationType(DestinationType.NONE); } } catch (LdapException | BaseException e) { LOGGER.error("Query Execution failed for Endpoint:" + endpoint.getHost(), e); partitionResponse.addStatus(endpoint.getHost(), new Oops(e)); if (!Utils.isEmpty(endpoint.getSecondaryHost())) { partitionResponse.addStatus(endpoint.getSecondaryHost(), new Oops(e)); } endpoint.hasNext(CursorStatus.EOF); endpoint.setDestinationType(DestinationType.NONE); } catch (Exception e) { endpoint.hasNext(CursorStatus.EOF); endpoint.setDestinationType(DestinationType.NONE); LOGGER.error("Some unhandled exception occurred at Endpoint: " + endpoint.getHost(), e); throw e; } } return response; }
QueryRequestPagedExecutor extends QueryRequestExecutor { @Override QueryResponse execute() { QueryResponse response = new QueryResponse(); for (Endpoint endpoint : this.queryRequest.getEndpoints()) { PartitionResponse partitionResponse = new PartitionResponse(); response.addPartitionResponse(partitionResponse); partitionResponse.setEndpoint(endpoint); try { LOGGER.debug("Executing request for: " + endpoint.getHost()); if (endpoint.hasNext() == CursorStatus.EOF) { LOGGER.debug("Host " + endpoint.getHost() + " skipped: EOF"); continue; } LdapConnectionResult ldapConnectionResult = driverGetInstance().connect(endpoint); String basePath = tryResolveBasePath(ldapConnectionResult.getConnection(), endpoint.getBaseSearchPath()); endpoint.setBaseSearchPath(basePath); endpoint.setDestinationType(ldapConnectionResult.getDestinationType()); partitionResponse.setStatus(ldapConnectionResult.getStatuses()); LdapConnection connection = ldapConnectionResult.getConnection(); if (ldapConnectionResult.connectionSucceeded()) { SearchRequest search = parserGetInstance().toSearchRequest(queryRequest, endpoint.getBaseSearchPath(), endpoint.getCookie()); RoundtripResult roundtripResult = collectData(connection, search); Object cookie = retrieveCookie(roundtripResult.getSearchCursor()); endpoint.setCookie(cookie); if (cookie == null) { endpoint.hasNext(CursorStatus.EOF); } else { endpoint.hasNext(CursorStatus.PAGING); } partitionResponse.setData(new ApacheAPIConverter().toEntityResponse(roundtripResult.getData(), queryRequest.getRequestedFields())); return response; } else { LOGGER.error(String.format("Could not execute query against AD {}. Connection failure!", endpoint.getHost())); endpoint.hasNext(CursorStatus.EOF); endpoint.setDestinationType(DestinationType.NONE); } } catch (LdapException | BaseException e) { LOGGER.error("Query Execution failed for Endpoint:" + endpoint.getHost(), e); partitionResponse.addStatus(endpoint.getHost(), new Oops(e)); if (!Utils.isEmpty(endpoint.getSecondaryHost())) { partitionResponse.addStatus(endpoint.getSecondaryHost(), new Oops(e)); } endpoint.hasNext(CursorStatus.EOF); endpoint.setDestinationType(DestinationType.NONE); } catch (Exception e) { endpoint.hasNext(CursorStatus.EOF); endpoint.setDestinationType(DestinationType.NONE); LOGGER.error("Some unhandled exception occurred at Endpoint: " + endpoint.getHost(), e); throw e; } } return response; } }
QueryRequestPagedExecutor extends QueryRequestExecutor { @Override QueryResponse execute() { QueryResponse response = new QueryResponse(); for (Endpoint endpoint : this.queryRequest.getEndpoints()) { PartitionResponse partitionResponse = new PartitionResponse(); response.addPartitionResponse(partitionResponse); partitionResponse.setEndpoint(endpoint); try { LOGGER.debug("Executing request for: " + endpoint.getHost()); if (endpoint.hasNext() == CursorStatus.EOF) { LOGGER.debug("Host " + endpoint.getHost() + " skipped: EOF"); continue; } LdapConnectionResult ldapConnectionResult = driverGetInstance().connect(endpoint); String basePath = tryResolveBasePath(ldapConnectionResult.getConnection(), endpoint.getBaseSearchPath()); endpoint.setBaseSearchPath(basePath); endpoint.setDestinationType(ldapConnectionResult.getDestinationType()); partitionResponse.setStatus(ldapConnectionResult.getStatuses()); LdapConnection connection = ldapConnectionResult.getConnection(); if (ldapConnectionResult.connectionSucceeded()) { SearchRequest search = parserGetInstance().toSearchRequest(queryRequest, endpoint.getBaseSearchPath(), endpoint.getCookie()); RoundtripResult roundtripResult = collectData(connection, search); Object cookie = retrieveCookie(roundtripResult.getSearchCursor()); endpoint.setCookie(cookie); if (cookie == null) { endpoint.hasNext(CursorStatus.EOF); } else { endpoint.hasNext(CursorStatus.PAGING); } partitionResponse.setData(new ApacheAPIConverter().toEntityResponse(roundtripResult.getData(), queryRequest.getRequestedFields())); return response; } else { LOGGER.error(String.format("Could not execute query against AD {}. Connection failure!", endpoint.getHost())); endpoint.hasNext(CursorStatus.EOF); endpoint.setDestinationType(DestinationType.NONE); } } catch (LdapException | BaseException e) { LOGGER.error("Query Execution failed for Endpoint:" + endpoint.getHost(), e); partitionResponse.addStatus(endpoint.getHost(), new Oops(e)); if (!Utils.isEmpty(endpoint.getSecondaryHost())) { partitionResponse.addStatus(endpoint.getSecondaryHost(), new Oops(e)); } endpoint.hasNext(CursorStatus.EOF); endpoint.setDestinationType(DestinationType.NONE); } catch (Exception e) { endpoint.hasNext(CursorStatus.EOF); endpoint.setDestinationType(DestinationType.NONE); LOGGER.error("Some unhandled exception occurred at Endpoint: " + endpoint.getHost(), e); throw e; } } return response; } QueryRequestPagedExecutor(QueryRequest queryRequest); }
QueryRequestPagedExecutor extends QueryRequestExecutor { @Override QueryResponse execute() { QueryResponse response = new QueryResponse(); for (Endpoint endpoint : this.queryRequest.getEndpoints()) { PartitionResponse partitionResponse = new PartitionResponse(); response.addPartitionResponse(partitionResponse); partitionResponse.setEndpoint(endpoint); try { LOGGER.debug("Executing request for: " + endpoint.getHost()); if (endpoint.hasNext() == CursorStatus.EOF) { LOGGER.debug("Host " + endpoint.getHost() + " skipped: EOF"); continue; } LdapConnectionResult ldapConnectionResult = driverGetInstance().connect(endpoint); String basePath = tryResolveBasePath(ldapConnectionResult.getConnection(), endpoint.getBaseSearchPath()); endpoint.setBaseSearchPath(basePath); endpoint.setDestinationType(ldapConnectionResult.getDestinationType()); partitionResponse.setStatus(ldapConnectionResult.getStatuses()); LdapConnection connection = ldapConnectionResult.getConnection(); if (ldapConnectionResult.connectionSucceeded()) { SearchRequest search = parserGetInstance().toSearchRequest(queryRequest, endpoint.getBaseSearchPath(), endpoint.getCookie()); RoundtripResult roundtripResult = collectData(connection, search); Object cookie = retrieveCookie(roundtripResult.getSearchCursor()); endpoint.setCookie(cookie); if (cookie == null) { endpoint.hasNext(CursorStatus.EOF); } else { endpoint.hasNext(CursorStatus.PAGING); } partitionResponse.setData(new ApacheAPIConverter().toEntityResponse(roundtripResult.getData(), queryRequest.getRequestedFields())); return response; } else { LOGGER.error(String.format("Could not execute query against AD {}. Connection failure!", endpoint.getHost())); endpoint.hasNext(CursorStatus.EOF); endpoint.setDestinationType(DestinationType.NONE); } } catch (LdapException | BaseException e) { LOGGER.error("Query Execution failed for Endpoint:" + endpoint.getHost(), e); partitionResponse.addStatus(endpoint.getHost(), new Oops(e)); if (!Utils.isEmpty(endpoint.getSecondaryHost())) { partitionResponse.addStatus(endpoint.getSecondaryHost(), new Oops(e)); } endpoint.hasNext(CursorStatus.EOF); endpoint.setDestinationType(DestinationType.NONE); } catch (Exception e) { endpoint.hasNext(CursorStatus.EOF); endpoint.setDestinationType(DestinationType.NONE); LOGGER.error("Some unhandled exception occurred at Endpoint: " + endpoint.getHost(), e); throw e; } } return response; } QueryRequestPagedExecutor(QueryRequest queryRequest);  }
QueryRequestPagedExecutor extends QueryRequestExecutor { @Override QueryResponse execute() { QueryResponse response = new QueryResponse(); for (Endpoint endpoint : this.queryRequest.getEndpoints()) { PartitionResponse partitionResponse = new PartitionResponse(); response.addPartitionResponse(partitionResponse); partitionResponse.setEndpoint(endpoint); try { LOGGER.debug("Executing request for: " + endpoint.getHost()); if (endpoint.hasNext() == CursorStatus.EOF) { LOGGER.debug("Host " + endpoint.getHost() + " skipped: EOF"); continue; } LdapConnectionResult ldapConnectionResult = driverGetInstance().connect(endpoint); String basePath = tryResolveBasePath(ldapConnectionResult.getConnection(), endpoint.getBaseSearchPath()); endpoint.setBaseSearchPath(basePath); endpoint.setDestinationType(ldapConnectionResult.getDestinationType()); partitionResponse.setStatus(ldapConnectionResult.getStatuses()); LdapConnection connection = ldapConnectionResult.getConnection(); if (ldapConnectionResult.connectionSucceeded()) { SearchRequest search = parserGetInstance().toSearchRequest(queryRequest, endpoint.getBaseSearchPath(), endpoint.getCookie()); RoundtripResult roundtripResult = collectData(connection, search); Object cookie = retrieveCookie(roundtripResult.getSearchCursor()); endpoint.setCookie(cookie); if (cookie == null) { endpoint.hasNext(CursorStatus.EOF); } else { endpoint.hasNext(CursorStatus.PAGING); } partitionResponse.setData(new ApacheAPIConverter().toEntityResponse(roundtripResult.getData(), queryRequest.getRequestedFields())); return response; } else { LOGGER.error(String.format("Could not execute query against AD {}. Connection failure!", endpoint.getHost())); endpoint.hasNext(CursorStatus.EOF); endpoint.setDestinationType(DestinationType.NONE); } } catch (LdapException | BaseException e) { LOGGER.error("Query Execution failed for Endpoint:" + endpoint.getHost(), e); partitionResponse.addStatus(endpoint.getHost(), new Oops(e)); if (!Utils.isEmpty(endpoint.getSecondaryHost())) { partitionResponse.addStatus(endpoint.getSecondaryHost(), new Oops(e)); } endpoint.hasNext(CursorStatus.EOF); endpoint.setDestinationType(DestinationType.NONE); } catch (Exception e) { endpoint.hasNext(CursorStatus.EOF); endpoint.setDestinationType(DestinationType.NONE); LOGGER.error("Some unhandled exception occurred at Endpoint: " + endpoint.getHost(), e); throw e; } } return response; } QueryRequestPagedExecutor(QueryRequest queryRequest);   }