aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-feed-client
diff options
context:
space:
mode:
authorJon Marius Venstad <venstad@gmail.com>2021-06-28 13:16:19 +0200
committerJon Marius Venstad <venstad@gmail.com>2021-06-28 13:16:19 +0200
commitdd4213f72dbcc6a0dd40fb4e0a20a1aebb01d342 (patch)
tree3ca18ed2d401a87a3abf73ebdb55995278c7fec1 /vespa-feed-client
parent51b4f15b25068cab7eff193046cb9f18512f4a84 (diff)
More tests for feed client
Diffstat (limited to 'vespa-feed-client')
-rw-r--r--vespa-feed-client/src/main/java/ai/vespa/feed/client/HttpFeedClient.java2
-rw-r--r--vespa-feed-client/src/test/java/ai/vespa/feed/client/HttpFeedClientTest.java72
2 files changed, 69 insertions, 5 deletions
diff --git a/vespa-feed-client/src/main/java/ai/vespa/feed/client/HttpFeedClient.java b/vespa-feed-client/src/main/java/ai/vespa/feed/client/HttpFeedClient.java
index 90b5707c8a0..e3a8a6af9b0 100644
--- a/vespa-feed-client/src/main/java/ai/vespa/feed/client/HttpFeedClient.java
+++ b/vespa-feed-client/src/main/java/ai/vespa/feed/client/HttpFeedClient.java
@@ -79,7 +79,7 @@ class HttpFeedClient implements FeedClient {
HttpRequest request = new HttpRequest(method,
getPath(documentId) + getQuery(params),
requestHeaders,
- operationJson.getBytes(UTF_8)); // TODO: make it bytes all the way?
+ operationJson == null ? null : operationJson.getBytes(UTF_8)); // TODO: make it bytes all the way?
return requestStrategy.enqueue(documentId, request)
.thenApply(response -> toResult(request, response, documentId));
diff --git a/vespa-feed-client/src/test/java/ai/vespa/feed/client/HttpFeedClientTest.java b/vespa-feed-client/src/test/java/ai/vespa/feed/client/HttpFeedClientTest.java
index 6aa0de2160c..dd0ead799ff 100644
--- a/vespa-feed-client/src/test/java/ai/vespa/feed/client/HttpFeedClientTest.java
+++ b/vespa-feed-client/src/test/java/ai/vespa/feed/client/HttpFeedClientTest.java
@@ -5,6 +5,7 @@ import org.junit.jupiter.api.Test;
import java.net.URI;
import java.time.Duration;
+import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicReference;
@@ -12,6 +13,7 @@ import java.util.function.BiFunction;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -33,7 +35,70 @@ class HttpFeedClientTest {
}
FeedClient client = new HttpFeedClient(FeedClientBuilder.create(URI.create("https://dummy:123")), new MockRequestStrategy());
- // Vespa error is an error result.
+ // Update is a PUT, and 200 OK is a success.
+ dispatch.set((documentId, request) -> {
+ try {
+ assertEquals(id, documentId);
+ assertEquals("/document/v1/ns/type/docid/0",
+ request.path());
+ assertEquals("PUT", request.method());
+ assertEquals("json", new String(request.body(), UTF_8));
+
+ HttpResponse response = HttpResponse.of(200,
+ ("{\n" +
+ " \"pathId\": \"/document/v1/ns/type/docid/0\",\n" +
+ " \"id\": \"id:ns:type::0\"\n" +
+ "}").getBytes(UTF_8));
+ return CompletableFuture.completedFuture(response);
+ }
+ catch (Throwable thrown) {
+ CompletableFuture<HttpResponse> failed = new CompletableFuture<>();
+ failed.completeExceptionally(thrown);
+ return failed;
+ }
+ });
+ Result result = client.update(id,
+ "json",
+ OperationParameters.empty())
+ .get();
+ assertEquals(Result.Type.success, result.type());
+ assertEquals(id, result.documentId());
+ assertEquals(Optional.empty(), result.resultMessage());
+ assertEquals(Optional.empty(), result.traceMessage());
+
+ // Remove is a DELETE, and 412 OK is a conditionNotMet.
+ dispatch.set((documentId, request) -> {
+ try {
+ assertEquals(id, documentId);
+ assertEquals("/document/v1/ns/type/docid/0?tracelevel=1",
+ request.path());
+ assertEquals("DELETE", request.method());
+ assertNull(request.body());
+
+ HttpResponse response = HttpResponse.of(412,
+ ("{\n" +
+ " \"pathId\": \"/document/v1/ns/type/docid/0\",\n" +
+ " \"id\": \"id:ns:type::0\",\n" +
+ " \"message\": \"Relax, take it easy,\",\n" +
+ " \"trace\": \"for there is nothing that we can do.\"\n" +
+ "}").getBytes(UTF_8));
+ return CompletableFuture.completedFuture(response);
+ }
+ catch (Throwable thrown) {
+ CompletableFuture<HttpResponse> failed = new CompletableFuture<>();
+ failed.completeExceptionally(thrown);
+ return failed;
+ }
+ });
+ result = client.remove(id,
+ OperationParameters.empty().tracelevel(1))
+ .get();
+ assertEquals(Result.Type.conditionNotMet, result.type());
+ assertEquals(id, result.documentId());
+ assertEquals(Optional.of("Relax, take it easy,"), result.resultMessage());
+ assertEquals(Optional.of("for there is nothing that we can do."), result.traceMessage());
+
+ // Put is a POST, and a Vespa error is a ResultException.
dispatch.set((documentId, request) -> {
try {
assertEquals(id, documentId);
@@ -66,9 +131,8 @@ class HttpFeedClientTest {
.timeout(Duration.ofSeconds(5)))
.get());
assertTrue(expected.getCause() instanceof ResultException);
- ResultException result = (ResultException) expected.getCause();
- assertEquals("Ooops! ... I did it again.", result.getMessage());
- assertEquals("I played with your heart. Got lost in the game.", result.getTrace().get());
+ assertEquals("Ooops! ... I did it again.", expected.getCause().getMessage());
+ assertEquals("I played with your heart. Got lost in the game.", ((ResultException) expected.getCause()).getTrace().get());
// Handler error is a FeedException.