From d41a7f5907f975678ecf518e3e121a5bcd1f6617 Mon Sep 17 00:00:00 2001 From: Javier Ochoa Date: Thu, 5 Dec 2024 16:38:15 -0500 Subject: [PATCH] simple restassured tests; they need more edge cases added --- pom.xml | 4 + .../api/AssistantAdminApiAssistantTest.java | 155 ++++++++++++++++++ .../AssistantAdminApiLlmConnectionTest.java | 135 +++++++++++++++ ...istantAdminApiRetrieverConnectionTest.java | 136 +++++++++++++++ .../composer/api/AssistantAdminApiTest.java | 4 - 5 files changed, 430 insertions(+), 4 deletions(-) create mode 100644 src/test/java/com/redhat/composer/api/AssistantAdminApiAssistantTest.java create mode 100644 src/test/java/com/redhat/composer/api/AssistantAdminApiLlmConnectionTest.java create mode 100644 src/test/java/com/redhat/composer/api/AssistantAdminApiRetrieverConnectionTest.java delete mode 100644 src/test/java/com/redhat/composer/api/AssistantAdminApiTest.java diff --git a/pom.xml b/pom.xml index 3491791..fb47683 100644 --- a/pom.xml +++ b/pom.xml @@ -134,6 +134,10 @@ rest-assured test + + org.hamcrest + hamcrest + org.mapstruct mapstruct diff --git a/src/test/java/com/redhat/composer/api/AssistantAdminApiAssistantTest.java b/src/test/java/com/redhat/composer/api/AssistantAdminApiAssistantTest.java new file mode 100644 index 0000000..29e4207 --- /dev/null +++ b/src/test/java/com/redhat/composer/api/AssistantAdminApiAssistantTest.java @@ -0,0 +1,155 @@ +package com.redhat.composer.api; + +import com.redhat.composer.model.request.AssistantCreationRequest; +import io.quarkus.test.common.http.TestHTTPEndpoint; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.http.ContentType; +import jakarta.ws.rs.core.Response; +import org.bson.types.ObjectId; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.emptyArray; +import static org.hamcrest.Matchers.hasEntry; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.notNullValue; + +@QuarkusTest +@TestHTTPEndpoint(AssistantAdminApi.class) +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class AssistantAdminApiAssistantTest { + + private static AssistantCreationRequest assistantCreationRequest; + + @BeforeAll + public static void beforeAll() { + assistantCreationRequest = new AssistantCreationRequest(); + assistantCreationRequest.setName("assistant"); + assistantCreationRequest.setDescription("Assistant X"); + } + + @Test + @Order(10) + public void createAsssitantShouldSucceed() { + + + String llmConnectionId = given() + .contentType(ContentType.JSON) + .and() + .body(""" + { + "name":"conn1", + "description": "Llm Connection" + } + """) + .when() + .post("/llm") + .then() + .extract() + .path("id"); + + + assistantCreationRequest.setLlmConnectionId(llmConnectionId); + + String assistantHexStringId = given() + .contentType(ContentType.JSON) + .and() + .body(assistantCreationRequest) + .when() + .post() + .then() + .assertThat() + .body("id", notNullValue()) + .statusCode( + Response.Status.OK.getStatusCode()) + .extract() + .path("id"); + + assistantCreationRequest.setId(assistantHexStringId); + } + + + @Test + @Order(20) + public void retrieveAsssitants() { + given() + .contentType(ContentType.JSON) + .and() + .when() + .get() + .then() + .assertThat() + .body("$", Matchers.is(not(emptyArray()))) + .and() + .body("", hasItem(hasEntry("id", assistantCreationRequest.getId()))) + .statusCode( + Response.Status.OK.getStatusCode()); + } + + + @Test + @Order(30) + public void retrieveSpecificAsssitant() { + given() + .contentType(ContentType.JSON) + .and() + .when() + .pathParams("assistantId", assistantCreationRequest.getId()) + .get("/{assistantId}") + .then() + .assertThat() + .body("", hasEntry("id", assistantCreationRequest.getId())) + .statusCode( + Response.Status.OK.getStatusCode()); + } + + + @Test + @Order(35) + public void noyExistingAsssitant() { + given() + .contentType(ContentType.JSON) + .and() + .when() + .pathParams("assistantId", new ObjectId().toHexString()) + .get("/{assistantId}") + .then() + .assertThat() + .statusCode( + Response.Status.NOT_FOUND.getStatusCode()); + } + + @Test + @Order(40) + public void deleteAsssitant() { + given() + .contentType(ContentType.JSON) + .and() + .when() + .pathParams("assistantId", assistantCreationRequest.getId()) + .delete("/{assistantId}") + .then() + .assertThat() + .statusCode( + Response.Status.NO_CONTENT.getStatusCode()); + + given() + .contentType(ContentType.JSON) + .and() + .when() + .pathParams("assistantId", assistantCreationRequest.getId()) + .get("/{assistantId}") + .then() + .assertThat() + .statusCode( + Response.Status.NOT_FOUND.getStatusCode()); + } + + +} \ No newline at end of file diff --git a/src/test/java/com/redhat/composer/api/AssistantAdminApiLlmConnectionTest.java b/src/test/java/com/redhat/composer/api/AssistantAdminApiLlmConnectionTest.java new file mode 100644 index 0000000..0b96953 --- /dev/null +++ b/src/test/java/com/redhat/composer/api/AssistantAdminApiLlmConnectionTest.java @@ -0,0 +1,135 @@ +package com.redhat.composer.api; + +import com.redhat.composer.model.mongo.LlmConnectionEntity; +import io.quarkus.test.common.http.TestHTTPEndpoint; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.http.ContentType; +import jakarta.ws.rs.core.Response; +import org.bson.types.ObjectId; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.emptyArray; +import static org.hamcrest.Matchers.hasEntry; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.notNullValue; + +@QuarkusTest +@TestHTTPEndpoint(AssistantAdminApi.class) +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class AssistantAdminApiLlmConnectionTest { + + private static LlmConnectionEntity llmConnectionEntity; + + @BeforeAll + public static void beforeAll() { + llmConnectionEntity = new LlmConnectionEntity(); + llmConnectionEntity.setName("llm_name"); + llmConnectionEntity.setDescription("LLM Connection X"); + } + + @Test + @Order(10) + public void createLlmConnectionShouldSucceed() { + String hexStringId = given() + .contentType(ContentType.JSON) + .and() + .body(llmConnectionEntity) + .when() + .post("/llm") + .then() + .assertThat() + .body("id", notNullValue()) + .statusCode( + Response.Status.OK.getStatusCode()) + .extract() + .path("id"); + + llmConnectionEntity.id = new ObjectId(hexStringId); + } + + + @Test + @Order(20) + public void retrieveLlmConnections() { + given() + .contentType(ContentType.JSON) + .and() + .when() + .get("/llm") + .then() + .assertThat() + .body("$", Matchers.is(not(emptyArray()))) + .and() + .body("", hasItem(hasEntry("id", llmConnectionEntity.id.toHexString()))) + .statusCode( + Response.Status.OK.getStatusCode()); + } + + + @Test + @Order(30) + public void retrieveSpecificLlmConnection() { + given() + .contentType(ContentType.JSON) + .and() + .when() + .pathParams("llmConnectionId", llmConnectionEntity.id.toHexString()) + .get("/llm/{llmConnectionId}") + .then() + .assertThat() + .body("", hasEntry("id", llmConnectionEntity.id.toHexString())) + .statusCode( + Response.Status.OK.getStatusCode()); + } + + + @Test + @Order(35) + public void noyExistingLlmConnection() { + given() + .contentType(ContentType.JSON) + .and() + .when() + .pathParams("llmConnectionId", new ObjectId().toHexString()) + .get("/llm/{llmConnectionId}") + .then() + .assertThat() + .statusCode( + Response.Status.NOT_FOUND.getStatusCode()); + } + + @Test + @Order(40) + public void deleteLlmConnection() { + given() + .contentType(ContentType.JSON) + .and() + .when() + .pathParams("llmConnectionId", llmConnectionEntity.id.toHexString()) + .delete("/llm/{llmConnectionId}") + .then() + .assertThat() + .statusCode( + Response.Status.NO_CONTENT.getStatusCode()); + + given() + .contentType(ContentType.JSON) + .and() + .when() + .pathParams("llmConnectionId", llmConnectionEntity.id.toHexString()) + .get("/llm/{llmConnectionId}") + .then() + .assertThat() + .statusCode( + Response.Status.NOT_FOUND.getStatusCode()); + } + + +} \ No newline at end of file diff --git a/src/test/java/com/redhat/composer/api/AssistantAdminApiRetrieverConnectionTest.java b/src/test/java/com/redhat/composer/api/AssistantAdminApiRetrieverConnectionTest.java new file mode 100644 index 0000000..9e30c68 --- /dev/null +++ b/src/test/java/com/redhat/composer/api/AssistantAdminApiRetrieverConnectionTest.java @@ -0,0 +1,136 @@ +package com.redhat.composer.api; + +import com.redhat.composer.model.mongo.LlmConnectionEntity; +import com.redhat.composer.model.mongo.RetrieverConnectionEntity; +import io.quarkus.test.common.http.TestHTTPEndpoint; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.http.ContentType; +import jakarta.ws.rs.core.Response; +import org.bson.types.ObjectId; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.emptyArray; +import static org.hamcrest.Matchers.hasEntry; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.notNullValue; + +@QuarkusTest +@TestHTTPEndpoint(AssistantAdminApi.class) +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class AssistantAdminApiRetrieverConnectionTest { + + private static RetrieverConnectionEntity retrieverConnectionEntity; + + @BeforeAll + public static void beforeAll() { + retrieverConnectionEntity = new RetrieverConnectionEntity(); + retrieverConnectionEntity.setName("retriever_connection"); + retrieverConnectionEntity.setDescription("Retriever Connection X"); + } + + @Test + @Order(10) + public void createRetrieverConnectionShouldSucceed() { + String hexStringId = given() + .contentType(ContentType.JSON) + .and() + .body(retrieverConnectionEntity) + .when() + .post("/retrieverConnection") + .then() + .assertThat() + .body("id", notNullValue()) + .statusCode( + Response.Status.OK.getStatusCode()) + .extract() + .path("id"); + + retrieverConnectionEntity.id = new ObjectId(hexStringId); + } + + + @Test + @Order(20) + public void retrieveRetrieverConnections() { + given() + .contentType(ContentType.JSON) + .and() + .when() + .get("/retrieverConnection") + .then() + .assertThat() + .body("$", Matchers.is(not(emptyArray()))) + .and() + .body("", hasItem(hasEntry("id", retrieverConnectionEntity.id.toHexString()))) + .statusCode( + Response.Status.OK.getStatusCode()); + } + + + @Test + @Order(30) + public void retrieveSpecificRetrieverConnection() { + given() + .contentType(ContentType.JSON) + .and() + .when() + .pathParams("retrieverConnectionId", retrieverConnectionEntity.id.toHexString()) + .get("/retrieverConnection/{retrieverConnectionId}") + .then() + .assertThat() + .body("", hasEntry("id", retrieverConnectionEntity.id.toHexString())) + .statusCode( + Response.Status.OK.getStatusCode()); + } + + + @Test + @Order(35) + public void noyExistingRetrieverConnection() { + given() + .contentType(ContentType.JSON) + .and() + .when() + .pathParams("retrieverConnectionId", new ObjectId().toHexString()) + .get("/retrieverConnection/{retrieverConnectionId}") + .then() + .assertThat() + .statusCode( + Response.Status.NOT_FOUND.getStatusCode()); + } + + @Test + @Order(40) + public void deleteRetrieverConnection() { + given() + .contentType(ContentType.JSON) + .and() + .when() + .pathParams("retrieverConnectionId", retrieverConnectionEntity.id.toHexString()) + .delete("/retrieverConnection/{retrieverConnectionId}") + .then() + .assertThat() + .statusCode( + Response.Status.NO_CONTENT.getStatusCode()); + + given() + .contentType(ContentType.JSON) + .and() + .when() + .pathParams("retrieverConnectionId", retrieverConnectionEntity.id.toHexString()) + .get("/retrieverConnection/{retrieverConnectionId}") + .then() + .assertThat() + .statusCode( + Response.Status.NOT_FOUND.getStatusCode()); + } + + +} \ No newline at end of file diff --git a/src/test/java/com/redhat/composer/api/AssistantAdminApiTest.java b/src/test/java/com/redhat/composer/api/AssistantAdminApiTest.java deleted file mode 100644 index 3c041cf..0000000 --- a/src/test/java/com/redhat/composer/api/AssistantAdminApiTest.java +++ /dev/null @@ -1,4 +0,0 @@ -import static org.junit.jupiter.api.Assertions.*; -class AssistantAdminApiTest { - -} \ No newline at end of file