From ab180d215ac08a73971dc0f843747bdc87999746 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Tue, 23 Jul 2024 11:32:21 -0700 Subject: [PATCH 1/3] Convert DeleteOneIntegrationTest to compact code --- .../api/v1/DeleteOneIntegrationTest.java | 322 ++++-------------- 1 file changed, 71 insertions(+), 251 deletions(-) diff --git a/src/test/java/io/stargate/sgv2/jsonapi/api/v1/DeleteOneIntegrationTest.java b/src/test/java/io/stargate/sgv2/jsonapi/api/v1/DeleteOneIntegrationTest.java index 933d69ff5e..cbe44cd1d6 100644 --- a/src/test/java/io/stargate/sgv2/jsonapi/api/v1/DeleteOneIntegrationTest.java +++ b/src/test/java/io/stargate/sgv2/jsonapi/api/v1/DeleteOneIntegrationTest.java @@ -1,6 +1,5 @@ package io.stargate.sgv2.jsonapi.api.v1; -import static io.restassured.RestAssured.given; import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.Matchers.anyOf; import static org.hamcrest.Matchers.empty; @@ -11,7 +10,6 @@ import io.quarkus.test.common.QuarkusTestResource; import io.quarkus.test.junit.QuarkusIntegrationTest; -import io.restassured.http.ContentType; import io.stargate.sgv2.jsonapi.testresource.DseTestResource; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; @@ -32,7 +30,7 @@ public class DeleteOneIntegrationTest extends AbstractCollectionIntegrationTestB class DeleteOne { @Test public void byId() { - String json = + givenHeadersPostJsonThenOkNoErrors( """ { "insertOne": { @@ -42,107 +40,58 @@ public void byId() { } } } - """; + """); - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) - .body("errors", is(nullValue())); - - json = - """ + givenHeadersPostJsonThenOkNoErrors( + """ { "deleteOne": { "filter" : {"_id" : "doc3"} } } - """; - - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) + """) .body("status.deletedCount", is(1)) - .body("data", is(nullValue())) - .body("errors", is(nullValue())); + .body("data", is(nullValue())); // ensure find does not find the document - json = - """ + givenHeadersPostJsonThenOkNoErrors( + """ { "findOne": { "filter" : {"_id" : "doc3"} } } - """; - - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) + """) .body("data.document", is(nullValue())) - .body("status", is(nullValue())) - .body("errors", is(nullValue())); + .body("status", is(nullValue())); } @Test public void emptyOptionsAllowed() { - String json = - """ + givenHeadersPostJsonThenOkNoErrors( + """ { "deleteOne": { "filter" : {"_id" : "doc3"}, "options": {} } } - """; - - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) + """) .body("status.deletedCount", is(0)) - .body("data", is(nullValue())) - .body("errors", is(nullValue())); + .body("data", is(nullValue())); } @Test public void noOptionsAllowed() { - String json = - """ + givenHeadersPostJsonThenOk( + """ { "deleteOne": { "filter" : {"_id" : "docWithOptions"}, "options": {"setting":"abc"} } } - """; - - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) + """) .body("data.document", is(nullValue())) .body("status", is(nullValue())) .body("errors", is(notNullValue())) @@ -154,7 +103,7 @@ public void noOptionsAllowed() { @Test public void byColumn() { - String json = + givenHeadersPostJsonThenOkNoErrors( """ { "insertOne": { @@ -164,65 +113,34 @@ public void byColumn() { } } } - """; - - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) - .body("errors", is(nullValue())); - - json = - """ + """); + givenHeadersPostJsonThenOkNoErrors( + """ { "deleteOne": { "filter" : {"username" : "user4"} } } - """; - - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) + """) .body("status.deletedCount", is(1)) - .body("data", is(nullValue())) - .body("errors", is(nullValue())); + .body("data", is(nullValue())); // ensure find does not find the document - json = - """ + givenHeadersPostJsonThenOkNoErrors( + """ { "findOne": { "filter" : {"_id" : "doc4"} } } - """; - - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) + """) .body("data.document", is(nullValue())) - .body("status", is(nullValue())) - .body("errors", is(nullValue())); + .body("status", is(nullValue())); } @Test public void noFilter() { - String json = + givenHeadersPostJsonThenOkNoErrors( """ { "insertOne": { @@ -232,65 +150,35 @@ public void noFilter() { } } } - """; - - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) - .body("errors", is(nullValue())); + """); - json = - """ + givenHeadersPostJsonThenOkNoErrors( + """ { "deleteOne": { "filter": {} } } - """; - - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) + """) .body("status.deletedCount", is(1)) - .body("data", is(nullValue())) - .body("errors", is(nullValue())); + .body("data", is(nullValue())); // ensure find does not find the document - json = - """ + givenHeadersPostJsonThenOkNoErrors( + """ { "findOne": { "filter" : {"_id" : "doc3"} } } - """; - - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) + """) .body("data.document", is(nullValue())) - .body("status", is(nullValue())) - .body("errors", is(nullValue())); + .body("status", is(nullValue())); } @Test public void noMatch() { - String json = + givenHeadersPostJsonThenOkNoErrors( """ { "insertOne": { @@ -300,121 +188,71 @@ public void noMatch() { } } } - """; + """); - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) - .body("errors", is(nullValue())); - - json = - """ + givenHeadersPostJsonThenOkNoErrors( + """ { "deleteOne": { "filter" : {"username" : "user12345"} } } - """; - - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) + """) .body("status.deletedCount", is(0)) - .body("data", is(nullValue())) - .body("errors", is(nullValue())); + .body("data", is(nullValue())); // ensure find does find the document - json = - """ + givenHeadersPostJsonThenOkNoErrors( + """ { "findOne": { "filter" : {"_id" : "doc5"} } } - """; - - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) + """) .body("data.document._id", is("doc5")) - .body("status", is(nullValue())) - .body("errors", is(nullValue())); + .body("status", is(nullValue())); } @Test public void withSort() { - String document = + insertDoc( """ { "_id": "doc7", "username": "user7", "active_user" : true } - """; - insertDoc(document); - - String document1 = + """); + insertDoc( """ { "_id": "doc6", "username": "user6", "active_user" : true } - """; - insertDoc(document1); + """); - String json = - """ + givenHeadersPostJsonThenOkNoErrors( + """ { "deleteOne": { "filter" : {"active_user" : true}, "sort" : {"username" : 1} } } - """; - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) - .body("status.deletedCount", is(1)) - .body("errors", is(nullValue())); + """) + .body("status.deletedCount", is(1)); // assert state after update - json = - """ + givenHeadersPostJsonThenOkNoErrors( + """ { "find": { "filter" : {"_id" : "doc6"} } } - """; - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) + """) .body("data.documents", hasSize(0)); // cleanUp @@ -427,26 +265,17 @@ class Concurrency { @RepeatedTest(10) public void concurrentDeletes() throws Exception { - String document = + insertDoc( """ { "_id": "concurrent" } - """; - insertDoc(document); + """); // we can hit with more threads, max 1 retry per thread int threads = Math.max(Runtime.getRuntime().availableProcessors() - 1, 3); CountDownLatch latch = new CountDownLatch(threads); - String deleteJson = - """ - { - "deleteOne": { - "filter" : {"_id" : "concurrent"} - } - } - """; // start all threads AtomicInteger reportedDeletions = new AtomicInteger(0); AtomicReferenceArray exceptions = new AtomicReferenceArray<>(threads); @@ -456,16 +285,15 @@ public void concurrentDeletes() throws Exception { () -> { try { Integer deletedCount = - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(deleteJson) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) + givenHeadersPostJsonThenOkNoErrors( + """ + { + "deleteOne": { + "filter" : {"_id" : "concurrent"} + } + } + """) .body("status.deletedCount", anyOf(is(0), is(1))) - .body("errors", is(nullValue())) .extract() .path("status.deletedCount"); @@ -499,22 +327,14 @@ public void concurrentDeletes() throws Exception { assertThat(reportedDeletions.get()).isOne(); // assert state after all deletes - String findJson = - """ + givenHeadersPostJsonThenOkNoErrors( + """ { "find": { "filter" : {"_id" : "concurrent"} } } - """; - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(findJson) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) + """) .body("data.documents", is(empty())); } } From 150ead515ce74d6887cbc9925ef50914ad55c79d Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Tue, 23 Jul 2024 11:59:01 -0700 Subject: [PATCH 2/3] Convert DeleteManyIntegrationTest --- .../api/v1/DeleteManyIntegrationTest.java | 286 ++++-------------- 1 file changed, 67 insertions(+), 219 deletions(-) diff --git a/src/test/java/io/stargate/sgv2/jsonapi/api/v1/DeleteManyIntegrationTest.java b/src/test/java/io/stargate/sgv2/jsonapi/api/v1/DeleteManyIntegrationTest.java index 953acd45fc..f6f645d750 100644 --- a/src/test/java/io/stargate/sgv2/jsonapi/api/v1/DeleteManyIntegrationTest.java +++ b/src/test/java/io/stargate/sgv2/jsonapi/api/v1/DeleteManyIntegrationTest.java @@ -1,6 +1,5 @@ package io.stargate.sgv2.jsonapi.api.v1; -import static io.restassured.RestAssured.given; import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals; import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.Matchers.anyOf; @@ -13,7 +12,6 @@ import io.quarkus.test.common.QuarkusTestResource; import io.quarkus.test.junit.QuarkusIntegrationTest; -import io.restassured.http.ContentType; import io.stargate.sgv2.jsonapi.testresource.DseTestResource; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; @@ -36,174 +34,101 @@ class DeleteMany { private void insert(int countOfDocument) { for (int i = 1; i <= countOfDocument; i++) { - String json = - """ + insertDoc( + """ { - "insertOne": { - "document": { "_id": "doc%s", "username": "user%s", "status": "active" - } - } } - """; - - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json.formatted(i, i)) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) - .body("errors", is(nullValue())); + """ + .formatted(i, i)); } } @Test public void byId() { insert(2); - String json = - """ + givenHeadersPostJsonThenOkNoErrors( + """ { "deleteMany": { "filter" : {"_id" : "doc1"} } } - """; - - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) + """) .body("status.deletedCount", is(1)) .body("status.moreData", is(nullValue())) - .body("data", is(nullValue())) - .body("errors", is(nullValue())); + .body("data", is(nullValue())); // ensure find does not find the document - json = - """ + givenHeadersPostJsonThenOkNoErrors( + """ { "findOne": { "filter" : {"_id" : "doc1"} } } - """; - - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) + """) .body("data.document", is(nullValue())) - .body("status", is(nullValue())) - .body("errors", is(nullValue())); + .body("status", is(nullValue())); // but can find does the non-deleted document - json = - """ + givenHeadersPostJsonThenOkNoErrors( + """ { "findOne": { "filter" : {"_id" : "doc2"} } } - """; - - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) + """) .body("data.document._id", is("doc2")) - .body("status", is(nullValue())) - .body("errors", is(nullValue())); + .body("status", is(nullValue())); } @Test public void emptyOptionsAllowed() { insert(2); - String json = - """ + givenHeadersPostJsonThenOkNoErrors( + """ { "deleteMany": { "filter" : {"_id" : "doc1"}, "options": {} } } - """; - - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) + """) .body("status.deletedCount", is(1)) .body("status.moreData", is(nullValue())) - .body("data", is(nullValue())) - .body("errors", is(nullValue())); + .body("data", is(nullValue())); } @Test public void byColumn() { insert(5); - String json = - """ + givenHeadersPostJsonThenOkNoErrors( + """ { "deleteMany": { "filter" : {"status": "active"} } } - """; - - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) + """) .body("status.deletedCount", is(5)) .body("status.moreData", is(nullValue())) - .body("data", is(nullValue())) - .body("errors", is(nullValue())); + .body("data", is(nullValue())); // ensure find does not find the documents - json = - """ + givenHeadersPostJsonThenOkNoErrors( + """ { "find": { "filter" : {"status": "active"} } } - """; - - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) + """) .body("data.documents", jsonEquals("[]")) - .body("status", is(nullValue())) - .body("errors", is(nullValue())); + .body("status", is(nullValue())); } /** @@ -213,46 +138,26 @@ public void byColumn() { @Test public void noFilter() { insert(25); - String json = - """ + givenHeadersPostJsonThenOkNoErrors( + """ { "deleteMany": { } } - """; - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) + """) .body("status.deletedCount", is(-1)) .body("status.moreData", is(nullValue())) - .body("data", is(nullValue())) - .body("errors", is(nullValue())); + .body("data", is(nullValue())); // ensure find does not find the documents - json = - """ + givenHeadersPostJsonThenOkNoErrors( + """ { - "find": { - } + "find": { } } - """; - - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) + """) .body("data.documents", jsonEquals("[]")) - .body("status", is(nullValue())) - .body("errors", is(nullValue())); + .body("status", is(nullValue())); } /** @@ -262,95 +167,56 @@ public void noFilter() { @Test public void emptyFilter() { insert(25); - String json = - """ + givenHeadersPostJsonThenOkNoErrors( + """ { "deleteMany": { "filter": {} } } - """; - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) + """) .body("status.deletedCount", is(-1)) .body("status.moreData", is(nullValue())) - .body("data", is(nullValue())) - .body("errors", is(nullValue())); + .body("data", is(nullValue())); // ensure find does not find the documents - json = - """ + givenHeadersPostJsonThenOkNoErrors( + """ { - "find": { - } + "find": { } } - """; - - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) + """) .body("data.documents", jsonEquals("[]")) - .body("status", is(nullValue())) - .body("errors", is(nullValue())); + .body("status", is(nullValue())); } @Test public void withFilterMoreDataFlag() { insert(25); - String json = - """ + givenHeadersPostJsonThenOkNoErrors( + """ { "deleteMany": { "filter" : {"status": "active"} } } - """; - // moreData will only exist when filter exist. If filter doesn't exist, it will delete all - // data - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) + """) + // moreData will only exist when filter exist. If filter doesn't exist, it will delete all + // data .body("status.deletedCount", is(20)) .body("status.moreData", is(true)) - .body("data", is(nullValue())) - .body("errors", is(nullValue())); + .body("data", is(nullValue())); // ensure only 20 are really deleted - json = - """ + givenHeadersPostJsonThenOkNoErrors( + """ { "find": { } } - """; - - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) + """) .body("data.documents", hasSize(5)) - .body("status", is(nullValue())) - .body("errors", is(nullValue())); + .body("status", is(nullValue())); } } @@ -361,7 +227,6 @@ class Concurrency { public void concurrentDeletes() throws Exception { // with 10 documents int totalDocuments = 10; - String document = """ { @@ -377,14 +242,6 @@ public void concurrentDeletes() throws Exception { int threads = Math.max(Runtime.getRuntime().availableProcessors() - 1, 3); CountDownLatch latch = new CountDownLatch(threads); - String deleteJson = - """ - { - "deleteMany": { - "filter" : {"status": "active"} - } - } - """; // start all threads AtomicInteger reportedDeletions = new AtomicInteger(0); AtomicReferenceArray exceptions = new AtomicReferenceArray<>(threads); @@ -394,18 +251,17 @@ public void concurrentDeletes() throws Exception { () -> { try { Integer deletedCount = - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(deleteJson) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) + givenHeadersPostJsonThenOkNoErrors( + """ + { + "deleteMany": { + "filter" : {"status": "active"} + } + } + """) .body( "status.deletedCount", anyOf(greaterThanOrEqualTo(0), lessThanOrEqualTo(totalDocuments))) - .body("errors", is(nullValue())) .extract() .path("status.deletedCount"); @@ -439,21 +295,13 @@ public void concurrentDeletes() throws Exception { assertThat(reportedDeletions.get()).isEqualTo(totalDocuments); // assert state after all deletes - String findJson = - """ + givenHeadersPostJsonThenOkNoErrors( + """ { "find": { } } - """; - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(findJson) - .when() - .post(CollectionResource.BASE_PATH, namespaceName, collectionName) - .then() - .statusCode(200) + """) .body("data.documents", is(empty())); } } From c2c0413a2942e70e09f49737b2ca62121bcd76e7 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Tue, 23 Jul 2024 12:23:24 -0700 Subject: [PATCH 3/3] And simplify DeleteCollectionIntegrationTest as well --- ...AbstractCollectionIntegrationTestBase.java | 5 +- .../AbstractNamespaceIntegrationTestBase.java | 6 ++ .../v1/DeleteCollectionIntegrationTest.java | 84 +++++++------------ 3 files changed, 38 insertions(+), 57 deletions(-) diff --git a/src/test/java/io/stargate/sgv2/jsonapi/api/v1/AbstractCollectionIntegrationTestBase.java b/src/test/java/io/stargate/sgv2/jsonapi/api/v1/AbstractCollectionIntegrationTestBase.java index 2ac7fe0c7c..da263ebc37 100644 --- a/src/test/java/io/stargate/sgv2/jsonapi/api/v1/AbstractCollectionIntegrationTestBase.java +++ b/src/test/java/io/stargate/sgv2/jsonapi/api/v1/AbstractCollectionIntegrationTestBase.java @@ -149,10 +149,7 @@ protected void insertManyDocs(String docsJson, int docsAmount) { /** Utility method for reducing boilerplate code for sending JSON commands */ protected ValidatableResponse givenHeadersPostJsonThen(String json) { - return given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) + return givenHeadersAndJson(json) .when() .post(CollectionResource.BASE_PATH, namespaceName, collectionName) .then(); diff --git a/src/test/java/io/stargate/sgv2/jsonapi/api/v1/AbstractNamespaceIntegrationTestBase.java b/src/test/java/io/stargate/sgv2/jsonapi/api/v1/AbstractNamespaceIntegrationTestBase.java index 3dfda1bbab..49032179a6 100644 --- a/src/test/java/io/stargate/sgv2/jsonapi/api/v1/AbstractNamespaceIntegrationTestBase.java +++ b/src/test/java/io/stargate/sgv2/jsonapi/api/v1/AbstractNamespaceIntegrationTestBase.java @@ -8,6 +8,7 @@ import io.restassured.RestAssured; import io.restassured.http.ContentType; +import io.restassured.specification.RequestSpecification; import io.stargate.sgv2.jsonapi.config.constants.HttpConstants; import io.stargate.sgv2.jsonapi.service.embedding.operation.test.CustomITEmbeddingProvider; import java.util.Base64; @@ -209,4 +210,9 @@ public static void checkIndexUsageMetrics(String commandName, boolean vector) { .toList(); assertThat(countMetrics.size()).isGreaterThan(0); } + + /** Utility method for reducing boilerplate code for sending JSON commands */ + protected RequestSpecification givenHeadersAndJson(String json) { + return given().headers(getHeaders()).contentType(ContentType.JSON).body(json); + } } diff --git a/src/test/java/io/stargate/sgv2/jsonapi/api/v1/DeleteCollectionIntegrationTest.java b/src/test/java/io/stargate/sgv2/jsonapi/api/v1/DeleteCollectionIntegrationTest.java index 65b12cf438..918bc187d5 100644 --- a/src/test/java/io/stargate/sgv2/jsonapi/api/v1/DeleteCollectionIntegrationTest.java +++ b/src/test/java/io/stargate/sgv2/jsonapi/api/v1/DeleteCollectionIntegrationTest.java @@ -1,11 +1,9 @@ package io.stargate.sgv2.jsonapi.api.v1; -import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.is; import io.quarkus.test.common.QuarkusTestResource; import io.quarkus.test.junit.QuarkusIntegrationTest; -import io.restassured.http.ContentType; import io.stargate.sgv2.jsonapi.testresource.DseTestResource; import org.apache.commons.lang3.RandomStringUtils; import org.junit.jupiter.api.ClassOrderer; @@ -28,20 +26,15 @@ public void happyPath() { String collection = RandomStringUtils.randomAlphabetic(16); // first create - String createJson = + givenHeadersAndJson( + """ + { + "createCollection": { + "name": "%s" + } + } """ - { - "createCollection": { - "name": "%s" - } - } - """ - .formatted(collection); - - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(createJson) + .formatted(collection)) .when() .post(NamespaceResource.BASE_PATH, namespaceName) .then() @@ -49,20 +42,15 @@ public void happyPath() { .body("status.ok", is(1)); // then delete - String json = + givenHeadersAndJson( + """ + { + "deleteCollection": { + "name": "%s" + } + } """ - { - "deleteCollection": { - "name": "%s" - } - } - """ - .formatted(collection); - - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) + .formatted(collection)) .when() .post(NamespaceResource.BASE_PATH, namespaceName) .then() @@ -75,20 +63,15 @@ public void notExisting() { String collection = RandomStringUtils.randomAlphabetic(16); // delete not existing - String json = + givenHeadersAndJson( + """ + { + "deleteCollection": { + "name": "%s" + } + } """ - { - "deleteCollection": { - "name": "%s" - } - } - """ - .formatted(collection); - - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) + .formatted(collection)) .when() .post(NamespaceResource.BASE_PATH, namespaceName) .then() @@ -98,18 +81,13 @@ public void notExisting() { @Test public void invalidCommand() { - String json = - """ - { - "deleteCollection": { - } - } - """; - - given() - .headers(getHeaders()) - .contentType(ContentType.JSON) - .body(json) + givenHeadersAndJson( + """ + { + "deleteCollection": { + } + } + """) .when() .post(NamespaceResource.BASE_PATH, namespaceName) .then()