Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Commit

Permalink
ci: add a sample test using rest assured
Browse files Browse the repository at this point in the history
  • Loading branch information
bbortt committed Jun 29, 2024
1 parent 682894c commit 34263f6
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<quarkus.platform.version>3.12.0</quarkus.platform.version>

<iban4j.version>3.2.8-RELEASE</iban4j.version>
<rest-assured.version>5.4.0</rest-assured.version>
<quarkus-openapi-generator-server.version>2.4.2</quarkus-openapi-generator-server.version>

<compiler-plugin.version>3.13.0</compiler-plugin.version>
Expand Down Expand Up @@ -91,6 +92,13 @@
<version>3.26.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>${rest-assured.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import ch.postfinance.swiss.hacks.domain.Login;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.transaction.Transactional;
import jakarta.ws.rs.BadRequestException;

import java.security.SecureRandom;
import java.time.Instant;

import static ch.postfinance.swiss.hacks.domain.Login.newLogin;
import static org.apache.commons.lang3.StringUtils.isEmpty;

@ApplicationScoped
public class LoginService {
Expand All @@ -17,6 +19,9 @@ public class LoginService {
@Transactional
public RegistrationInformation register(String firstName, String lastName, Instant dateOfBirth) {
// TODO: Some verification/validity checks, probably
if (isEmpty(firstName)){
throw new BadRequestException("First name is required");
}

var password = String.valueOf(SECURE_RANDOM.nextInt(100_000, 100_000_000));
var login = newLogin(firstName, lastName, dateOfBirth, password);
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/openapi/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ paths:
/customers/register:
post:
summary: Register a new customer
description: Allows a new customer to register for an online banking account.
description: Allows a new customer to register for an online banking account. U can later login using the returned credentials.
requestBody:
required: true
content:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package ch.postfinance.swiss.hacks.service;

import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.path.json.JsonPath;
import org.junit.jupiter.api.Test;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;

@QuarkusTest
public class CustomerApiTest {

@Test
public void testSuccessfulRegistration() {
// Define customer data
String firstName = "Foo";
String lastName = "Bar";
String dateOfBirth = "2000-01-01";

// Build request body
String requestBody = String.format("{" +
"\"firstName\": \"%s\"," +
"\"lastName\": \"%s\"," +
"\"dateOfBirth\": \"%s\"" +
"}", firstName, lastName, dateOfBirth);

// Send POST request and verify response
String response = RestAssured.given()
.contentType(ContentType.JSON)
.body(requestBody)
.post("/customers/register")
.then()
.statusCode(200) // TODO: this should be 201 according to spec!
.body("username", equalTo((firstName + "." + lastName).toLowerCase()))
// Replace "expected_password" with the actual password generation logic
.body("password", notNullValue())
.extract().asString();

// Extract username from response
String username = JsonPath.from(response).getString("username");
String password = JsonPath.from(response).getString("password");

// Send login request with extracted username and password
RestAssured.given()
// .contentType(ContentType.FORM) // Use form data for login
.formParam("j_username", username)
.formParam("j_password", password)
.post("/j_security_check")
.then()
.statusCode(302)
.header("location", equalTo("http://localhost:8081/index.html"));
}

@Test
public void testMissingRequiredField() {
// Define customer data with missing first name
String lastName = "Doe";
String dateOfBirth = "2000-01-01";

// Build request body
String requestBody = String.format("{" +
"\"lastName\": \"%s\"," +
"\"dateOfBirth\": \"%s\"" +
"}", lastName, dateOfBirth);

// Send POST request and verify response
RestAssured.given()
.contentType(ContentType.JSON)
.body(requestBody)
.post("/customers/register")
.then()
.statusCode(400);
}
}

0 comments on commit 34263f6

Please sign in to comment.