Skip to content

Commit

Permalink
simple restassured tests; they need more edge cases added
Browse files Browse the repository at this point in the history
  • Loading branch information
Javier Ochoa committed Dec 5, 2024
1 parent 47aea2c commit d41a7f5
Show file tree
Hide file tree
Showing 5 changed files with 430 additions and 4 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}


}
Original file line number Diff line number Diff line change
@@ -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());
}


}
Loading

0 comments on commit d41a7f5

Please sign in to comment.