Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.felseje</groupId>
<artifactId>java-api-test-automation-framework</artifactId>
<version>1.0.3</version>
<version>1.1.0</version>

<properties>
<owner.version>1.0.12</owner.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.github.felseje.apitestautomation.client.login;

import io.github.felseje.apitestautomation.client.login.dto.request.LoginRequest;
import io.github.felseje.apitestautomation.core.client.AbstractClient;
import io.github.felseje.apitestautomation.core.request.RequestContext;
import io.restassured.http.ContentType;
import io.restassured.response.Response;

public class LoginClient extends AbstractClient {

public Response login(LoginRequest request) {
RequestContext context = RequestContext.builder()
.body(request)
.contentType(ContentType.JSON)
.build();
return post(context, "/login");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.github.felseje.apitestautomation.client.login.dto.request;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class LoginRequest {

private String email;
private String password;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.github.felseje.apitestautomation.client.login.dto.response;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class LoginResponse {

private String message;
private String authorization;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.github.felseje.apitestautomation.service.login;

import io.github.felseje.apitestautomation.client.login.LoginClient;
import io.github.felseje.apitestautomation.client.login.dto.request.LoginRequest;
import io.github.felseje.apitestautomation.core.response.ApiResponse;
import io.github.felseje.apitestautomation.util.ArgumentValidator;
import io.restassured.response.Response;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class LoginService {

private final LoginClient loginClient;

public ApiResponse login(LoginRequest request) {
ArgumentValidator.requireNotNull(request, "The login request cannot be null");
Response response = loginClient.login(request);
return new ApiResponse(response);
}

}
11 changes: 4 additions & 7 deletions src/main/resources/config/config.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# Api url
base.url=https://api-link-here.com

# Timeouts
http.connect.timeout=5000
http.socket.timeout=10000
http.connection-manager.timeout=10000
base.url=https://serverest.dev
http.connect.timeout=10000
http.socket.timeout=15000
http.connection-manager.timeout=15000
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.felseje.apitestautomation.suite;

import io.github.felseje.apitestautomation.test.LoginIT;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;
import org.junit.platform.suite.api.SuiteDisplayName;

@Suite
@SuiteDisplayName("Full Test Suite")
@SelectClasses({
LoginIT.class
})
public class FullTestSuite {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package io.github.felseje.apitestautomation.test;

import io.github.felseje.apitestautomation.client.login.LoginClient;
import io.github.felseje.apitestautomation.client.login.dto.request.LoginRequest;
import io.github.felseje.apitestautomation.core.response.ApiResponse;
import io.github.felseje.apitestautomation.service.login.LoginService;
import io.github.felseje.apitestautomation.testdata.LoginProvider;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

@Tag("login")
@DisplayName("Login Tests")
public class LoginIT {

private static final String LOGIN_SUCCESS_SCHEMA = "schemas/login/success.json";
private static final String LOGIN_UNAUTHORIZED_SCHEMA = "schemas/login/unauthorized.json";
private static final String LOGIN_BLANK_EMAIL_SCHEMA = "schemas/login/blank-email.json";
private static final String LOGIN_BLANK_PASSWORD_SCHEMA = "schemas/login/blank-password.json";
private final LoginService loginService = new LoginService(new LoginClient());

static Stream<Arguments> provideBlankLoginFieldsForBadRequest() {
return Stream.of(
Arguments.of(
"Should return Bad Request when email is blank",
LOGIN_BLANK_EMAIL_SCHEMA,
LoginProvider.getLoginRequestWithBlankEmail()
),
Arguments.of(
"Should return Bad Request when password is blank",
LOGIN_BLANK_PASSWORD_SCHEMA,
LoginProvider.getLoginRequestWithBlankPassword()
)
);
}

@Test
@DisplayName("Should authenticate successfully when valid credentials are provided")
void shouldAuthenticateSuccessfully() {
// Arrange
LoginRequest request = LoginProvider.getValidLoginRequest();

// Act
ApiResponse response = loginService.login(request);

// Assert
response.shouldBeOk()
.shouldMatchJsonSchemaInClasspath(LOGIN_SUCCESS_SCHEMA);
}

@Test
@DisplayName("Should return unauthorized when invalid credentials are provided")
void shouldReturnUnauthorizedWhenCredentialsAreInvalid() {
// Arrange
LoginRequest request = LoginProvider.getLoginRequestWithUnauthorizedCredentials();

// Act
ApiResponse response = loginService.login(request);

// Assert
response.shouldBeUnauthorized()
.shouldMatchJsonSchemaInClasspath(LOGIN_UNAUTHORIZED_SCHEMA);
}

@ParameterizedTest(name = "{0}")
@MethodSource("provideBlankLoginFieldsForBadRequest")
void shouldReturnBadRequestForBlankFields(String testName, String expectedSchema, LoginRequest request) {
// Act
ApiResponse response = loginService.login(request);

// Assert
response.shouldBeBadRequest()
.shouldMatchJsonSchemaInClasspath(expectedSchema);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.github.felseje.apitestautomation.testdata;

import io.github.felseje.apitestautomation.client.login.dto.request.LoginRequest;
import lombok.experimental.UtilityClass;

@UtilityClass
public final class LoginProvider {

private static final String VALID_EMAIL = "fulano@qa.com";
private static final String VALID_PASSWORD = "teste";

public static LoginRequest getValidLoginRequest() {
return new LoginRequest(VALID_EMAIL, VALID_PASSWORD);
}

public static LoginRequest getLoginRequestWithUnauthorizedCredentials() {
return new LoginRequest("unauth@mail.com", "p4ssw0rd");
}

public static LoginRequest getLoginRequestWithBlankEmail() {
return new LoginRequest("", "p4ssw0rd");
}

public static LoginRequest getLoginRequestWithBlankPassword() {
return new LoginRequest("rTyxUuv@qa.com", "");
}

}
13 changes: 13 additions & 0 deletions src/test/resources/schemas/login/blank-email.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "object",
"required": [
"email"
],
"additionalProperties": false,
"properties": {
"email": {
"type": "string",
"const": "email não pode ficar em branco"
}
}
}
13 changes: 13 additions & 0 deletions src/test/resources/schemas/login/blank-password.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "object",
"required": [
"password"
],
"additionalProperties": false,
"properties": {
"password": {
"type": "string",
"const": "password não pode ficar em branco"
}
}
}
17 changes: 17 additions & 0 deletions src/test/resources/schemas/login/success.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"type": "object",
"required": [
"message",
"authorization"
],
"properties": {
"message": {
"type": "string",
"const": "Login realizado com sucesso"
},
"authorization": {
"type": "string",
"pattern": "^Bearer .*"
}
}
}
13 changes: 13 additions & 0 deletions src/test/resources/schemas/login/unauthorized.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "object",
"required": [
"message"
],
"additionalProperties": false,
"properties": {
"message": {
"type": "string",
"const": "Email e/ou senha inválidos"
}
}
}
Loading