From 39184fab6c7a501a8133426d6ac02e83f7d61146 Mon Sep 17 00:00:00 2001 From: Christian Winkler Date: Wed, 29 Nov 2023 09:58:52 +0100 Subject: [PATCH] Added tests Signed-off-by: Christian Winkler --- .../core/bulk/UpdateOperationData.java | 2 +- .../opensearch/integTest/AbstractCrudIT.java | 98 +++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/core/bulk/UpdateOperationData.java b/java-client/src/main/java/org/opensearch/client/opensearch/core/bulk/UpdateOperationData.java index 7f99ecf6af..635514c3e4 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/core/bulk/UpdateOperationData.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/core/bulk/UpdateOperationData.java @@ -72,7 +72,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { if (this.detectNoop != null) { generator.writeKey("detect_noop"); - generator.write(scriptedUpsert); + generator.write(detectNoop); } if (this.document != null) { diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractCrudIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractCrudIT.java index 16a9d5ff57..ced5e3ede8 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractCrudIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractCrudIT.java @@ -392,6 +392,104 @@ public void testBulkUpdateScriptUpsert() throws IOException { assertTrue(getResponse.found()); assertEquals(1337, getResponse.source().getIntValue()); } + + public void testBulkUpdateScriptedUpsertUpdate() throws IOException { + final String id = "777"; + + final AppData appData = new AppData(); + appData.setIntValue(1337); + appData.setMsg("foo"); + + assertEquals(Result.Created, javaClient().index(b -> b.index("index").id(id).document(appData)).result()); + + final BulkOperation op = new BulkOperation.Builder().update( + o -> o.index("index") + .id(id) + .scriptedUpsert(true) + .upsert(Collections.emptyMap()) + .script( + Script.of( + s -> s.inline( + new InlineScript.Builder().lang("painless") + .source("ctx._source.intValue = ctx?._source?.intValue == null ? 7777 : 9999") + .build())))) + .build(); + + BulkRequest bulkRequest = new BulkRequest.Builder().operations(op).build(); + BulkResponse bulkResponse = javaClient().bulk(bulkRequest); + + assertTrue(bulkResponse.took() > 0); + assertEquals(1, bulkResponse.items().size()); + + final GetResponse getResponse = javaClient().get(b -> b.index("index").id(id), AppData.class); + assertTrue(getResponse.found()); + assertEquals(9999, getResponse.source().getIntValue()); + } + + public void testBulkUpdateScriptedUpsertInsert() throws IOException { + final String id = "778"; + + final BulkOperation op = new BulkOperation.Builder().update( + o -> o.index("index") + .id(id) + .scriptedUpsert(true) + .upsert(Collections.emptyMap()) + .script( + Script.of( + s -> s.inline( + new InlineScript.Builder().lang("painless") + .source("ctx._source.intValue = ctx?._source?.intValue == null ? 7777 : 9999") + .build())))) + .build(); + + BulkRequest bulkRequest = new BulkRequest.Builder().operations(op).build(); + BulkResponse bulkResponse = javaClient().bulk(bulkRequest); + + assertTrue(bulkResponse.took() > 0); + assertEquals(1, bulkResponse.items().size()); + + final GetResponse getResponse = javaClient().get(b -> b.index("index").id(id), AppData.class); + assertTrue(getResponse.found()); + assertEquals(7777, getResponse.source().getIntValue()); + } + + public void testBulkUpdateDetectNoop() throws IOException { + final String id = "779"; + + final AppData appData = new AppData(); + appData.setIntValue(1337); + appData.setMsg("foo"); + + assertEquals(Result.Created, javaClient().index(b -> b.index("index").id(id).document(appData)).result()); + + BulkOperation op = new BulkOperation.Builder().update( + o -> o.index("index") + .id(id) + .detectNoop(true) + .document(appData)) + .build(); + + BulkRequest bulkRequest = new BulkRequest.Builder().operations(op).build(); + BulkResponse bulkResponse = javaClient().bulk(bulkRequest); + + assertTrue(bulkResponse.took() > 0); + assertEquals(1, bulkResponse.items().size()); + assertEquals(Result.NoOp.jsonValue(), bulkResponse.items().get(0).result()); + + op = new BulkOperation.Builder().update( + o -> o.index("index") + .id(id) + .detectNoop(false) + .document(appData)) + .build(); + + bulkRequest = new BulkRequest.Builder().operations(op).build(); + bulkResponse = javaClient().bulk(bulkRequest); + assertTrue(bulkResponse.took() > 0); + assertEquals(1, bulkResponse.items().size()); + assertEquals(Result.Updated.jsonValue(), bulkResponse.items().get(0).result()); + + } public void testBulkUpdateUpsert() throws IOException { final String id = "100";