OperationHandlerImpl implements OperationHandler { @Override public VisitResult visit(RestUri restUri, String documentSelection, VisitOptions options) throws RestApiException { VisitorParameters visitorParameters = createVisitorParameters(restUri, documentSelection, options); VisitorControlHandler visitorControlHandler = new VisitorControlHandler(); visitorParameters.setControlHandler(visitorControlHandler); LocalDataVisitorHandler localDataVisitorHandler = new LocalDataVisitorHandler(); visitorParameters.setLocalDataHandler(localDataVisitorHandler); final VisitorSession visitorSession; try { visitorSession = documentAccess.createVisitorSession(visitorParameters); visitorControlHandler.setSession(visitorSession); } catch (Exception e) { throw new RestApiException(Response.createErrorResponse( 500, "Failed during parsing of arguments for visiting: " + ExceptionUtils.getStackTraceAsString(e), restUri, RestUri.apiErrorCodes.VISITOR_ERROR)); } try { return doVisit(visitorControlHandler, localDataVisitorHandler, restUri); } finally { visitorSession.destroy(); } } OperationHandlerImpl(DocumentAccess documentAccess, ClusterEnumerator clusterEnumerator,
                                BucketSpaceResolver bucketSpaceResolver, MetricReceiver metricReceiver); @Override void shutdown(); static int getHTTPStatusCode(Set<Integer> errorCodes); @Override VisitResult visit(RestUri restUri, String documentSelection, VisitOptions options); @Override void put(RestUri restUri, FeedOperation data, Optional<String> route); @Override void update(RestUri restUri, FeedOperation data, Optional<String> route); @Override void delete(RestUri restUri, String condition, Optional<String> route); @Override Optional<String> get(RestUri restUri, Optional<String> fieldSet, Optional<String> cluster); @Override Optional<String> get(RestUri restUri, Optional<String> fieldSet); @Override Optional<String> get(RestUri restUri); static final int VISIT_TIMEOUT_MS; static final int WANTED_DOCUMENT_COUNT_UPPER_BOUND; static final int CONCURRENCY_UPPER_BOUND; }
@Test public void timeout_without_buckets_visited_throws_timeout_error() throws Exception { OperationHandlerImplFixture fixture = new OperationHandlerImplFixture(); fixture.completionCode = VisitorControlHandler.CompletionCode.TIMEOUT; fixture.bucketsVisited = 0; try { OperationHandlerImpl handler = fixture.createHandler(); handler.visit(dummyVisitUri(), "", emptyVisitOptions()); fail("Exception expected"); } catch (RestApiException e) { assertThat(e.getResponse().getStatus(), is(500)); assertThat(renderRestApiExceptionAsString(e), containsString("Timed out")); } }
@Test public void timeout_with_buckets_visited_does_not_throw_timeout_error() throws Exception { OperationHandlerImplFixture fixture = new OperationHandlerImplFixture(); fixture.completionCode = VisitorControlHandler.CompletionCode.TIMEOUT; fixture.bucketsVisited = 1; OperationHandlerImpl handler = fixture.createHandler(); handler.visit(dummyVisitUri(), "", emptyVisitOptions()); }
@Test public void unknown_bucket_space_mapping_throws_exception() throws Exception { OperationHandlerImplFixture fixture = new OperationHandlerImplFixture(); fixture.bucketSpaces.remove("document-type"); try { OperationHandlerImpl handler = fixture.createHandler(); handler.visit(dummyVisitUri(), "", emptyVisitOptions()); fail("Exception expected"); } catch (RestApiException e) { assertThat(e.getResponse().getStatus(), is(400)); String errorMsg = renderRestApiExceptionAsString(e); assertThat(errorMsg, is("{\"errors\":[{\"description\":" + "\"UNKNOWN_BUCKET_SPACE Document type 'document-type' in cluster 'foo' is not mapped to a known bucket space\",\"id\":-16}]}")); } }
@Test public void api_root_visit_uri_requires_cluster_set() throws Exception { OperationHandlerImplFixture fixture = new OperationHandlerImplFixture(); OperationHandlerImpl handler = fixture.createHandler(); try { handler.visit(apiRootVisitUri(), "", emptyVisitOptions()); fail("Exception expected"); } catch (RestApiException e) { assertThat(e.getResponse().getStatus(), is(400)); assertThat(renderRestApiExceptionAsString(e), containsString( "MISSING_CLUSTER Must set 'cluster' parameter to a valid content cluster id " + "when visiting at a root /document/v1/ level")); } }