OperationHandlerImpl implements OperationHandler { protected static ClusterDef resolveClusterDef(Optional<String> wantedCluster, List<ClusterDef> clusters) throws RestApiException { if (clusters.size() == 0) { throw new IllegalArgumentException("Your Vespa cluster does not have any content clusters " + "declared. Visiting feature is not available."); } if (! wantedCluster.isPresent()) { if (clusters.size() != 1) { String message = "Several clusters exist: " + clusters.stream().map(c -> "'" + c.getName() + "'").collect(Collectors.joining(", ")) + ". You must specify one."; throw new RestApiException(Response.createErrorResponse(400, message, RestUri.apiErrorCodes.SEVERAL_CLUSTERS)); } return clusters.get(0); } for (ClusterDef clusterDef : clusters) { if (clusterDef.getName().equals(wantedCluster.get())) { return clusterDef; } } String message = "Your vespa cluster contains the content clusters " + clusters.stream().map(c -> "'" + c.getName() + "'").collect(Collectors.joining(", ")) + ", not '" + wantedCluster.get() + "'. Please select a valid vespa cluster."; throw new RestApiException(Response.createErrorResponse(400, message, RestUri.apiErrorCodes.MISSING_CLUSTER)); } 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(expected = IllegalArgumentException.class) public void missingClusterDef() throws RestApiException { List<ClusterDef> clusterDef = new ArrayList<>(); OperationHandlerImpl.resolveClusterDef(Optional.empty(), clusterDef); }
@Test(expected = IllegalArgumentException.class) public void missingClusterDefSpecifiedCluster() throws RestApiException { List<ClusterDef> clusterDef = new ArrayList<>(); OperationHandlerImpl.resolveClusterDef(Optional.of("cluster"), clusterDef); }
@Test(expected = RestApiException.class) public void oneClusterPresentNotMatching() throws RestApiException { List<ClusterDef> clusterDef = new ArrayList<>(); clusterDef.add(new ClusterDef("foo", "configId")); OperationHandlerImpl.resolveClusterDef(Optional.of("cluster"), clusterDef); }
@Test() public void oneClusterMatching() throws RestApiException { List<ClusterDef> clusterDef = new ArrayList<>(); clusterDef.add(new ClusterDef("foo", "configId")); assertThat(toRoute(OperationHandlerImpl.resolveClusterDef(Optional.of("foo"), clusterDef)), is("[Storage:cluster=foo;clusterconfigid=configId]")); }
@Test() public void oneClusterMatchingManyAvailable() throws RestApiException { List<ClusterDef> clusterDef = new ArrayList<>(); clusterDef.add(new ClusterDef("foo2", "configId2")); clusterDef.add(new ClusterDef("foo", "configId")); clusterDef.add(new ClusterDef("foo3", "configId2")); assertThat(toRoute(OperationHandlerImpl.resolveClusterDef(Optional.of("foo"), clusterDef)), is("[Storage:cluster=foo;clusterconfigid=configId]")); }
@Test() public void unknown_target_cluster_throws_exception() throws RestApiException, IOException { List<ClusterDef> clusterDef = new ArrayList<>(); clusterDef.add(new ClusterDef("foo2", "configId2")); clusterDef.add(new ClusterDef("foo", "configId")); clusterDef.add(new ClusterDef("foo3", "configId2")); try { OperationHandlerImpl.resolveClusterDef(Optional.of("wrong"), clusterDef); } catch(RestApiException e) { assertThat(e.getResponse().getStatus(), is(400)); String errorMsg = renderRestApiExceptionAsString(e); assertThat(errorMsg, is("{\"errors\":[{\"description\":" + "\"MISSING_CLUSTER Your vespa cluster contains the content clusters 'foo2', 'foo'," + " 'foo3', not 'wrong'. Please select a valid vespa cluster.\",\"id\":-9}]}")); return; } fail("Expected exception"); }