Skip to content

Commit

Permalink
Migrated to lombrok, completed CorbadoServerException, Completed
Browse files Browse the repository at this point in the history
ValidationService integration test
  • Loading branch information
alexbalakirev committed Jul 12, 2024
1 parent ee55af1 commit 6bed96b
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 180 deletions.
44 changes: 33 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,29 @@
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.3.1</version>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.4.1</version>
<executions>
Expand All @@ -64,10 +85,11 @@
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<version>3.3.1</version>
<configuration>
<systemPropertyVariables>
<property>
Expand Down Expand Up @@ -211,7 +233,6 @@
</dependencies>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<configuration>
<consoleOutput>true</consoleOutput>
Expand All @@ -231,6 +252,13 @@

<!-- Dependencies -->
<dependencies>
<!--Lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
<scope>provided</scope>
</dependency>
<!-- @Nullable annotation -->
<dependency>
<groupId>com.google.code.findbugs</groupId>
Expand Down Expand Up @@ -296,12 +324,6 @@
<version>${junit-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit-platform-runner.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<properties>
Expand All @@ -314,8 +336,8 @@
<commons-lang3-version>3.14.0</commons-lang3-version>
<jackson-databind-nullable-version>0.2.6</jackson-databind-nullable-version>
<jakarta-annotation-version>1.3.5</jakarta-annotation-version>
<junit-version>5.10.2</junit-version>
<junit-platform-runner.version>1.10.0</junit-platform-runner.version>
<junit-version>5.10.3</junit-version>
<junit-platform-runner.version>1.10.3</junit-platform-runner.version>
<javax.ws.rs-api-version>2.1.1</javax.ws.rs-api-version>
<jsr311-api-version>1.1.1</jsr311-api-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
131 changes: 121 additions & 10 deletions src/main/java/com/corbado/exceptions/CorbadoServerException.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,147 @@
package com.corbado.exceptions;

import com.corbado.generated.invoker.ApiException;
import com.google.gson.Gson;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.ToString;

/** Custom exception class for server-related errors. */
// TODO: Complete
/** Custom exception class for server-related errors. */
@ToString
public class CorbadoServerException extends Exception {

/** The Constant serialVersionUID. */
private static final long serialVersionUID = 5970919574670247150L;

private final int httpStatusCode;
private final String body;
/** The http status code. */
@Getter private final int httpStatusCode;

/** The body. */
@NonNull @Getter private final ErrorResponse errorResponse;

/**
* Convert ApiException to ServerException.
*
* @param e ApiException to be converted
* @throws StandardException If response body is not a string
*/
public CorbadoServerException(final ApiException e) {
public CorbadoServerException(@NonNull final ApiException e) {
this(e.getCode(), e.getResponseBody());
}

public CorbadoServerException(final int statusCode, final String body) {
/**
* Instantiates a new corbado server exception.
*
* @param statusCode the status code
* @param body the body
*/
public CorbadoServerException(final int statusCode, @NonNull final String body) {
final Gson gson = new Gson();
httpStatusCode = statusCode;
this.body = body;
this.errorResponse = gson.fromJson(body, ErrorResponse.class);
}

public String getBody() {
return body;
/**
* Gets the request id.
*
* @return the request id
*/
public String getRequestId() {
final Optional<String> requestId =
Optional.ofNullable(this.errorResponse)
.map(ErrorResponse::getRequestData)
.map(RequestData::getRequestID);

if (requestId.isPresent()) {
return requestId.get();
} else {
// requestId should always be present
return null;
}
}

/**
* Gets the validation messages.
*
* @return the validation messages
*/
public List<ValidationMessage> getValidationMessages() {
final Optional<List<ValidationMessage>> validationMessages =
Optional.ofNullable(this.errorResponse)
.map(ErrorResponse::getError)
.map(ErrorDetails::getValidation);

if (validationMessages.isPresent()) {
return validationMessages.get();
} else {
return Collections.emptyList();
}
}

public int getHttpStatusCode() {
return httpStatusCode;
/** The Class ErrorResponse. */
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class ErrorResponse {

/** The error. */
private ErrorDetails error;

/** The http status code. */
private int httpStatusCode;

/** The request data. */
private RequestData requestData;

/** The runtime. */
private double runtime;
}

/** The Class ErrorDetails. */
@Data
@AllArgsConstructor
public static class ErrorDetails {

/** The links. */
private List<String> links;

/** The type. */
private String type;

/** The validation. */
private List<ValidationMessage> validation;
}

/** The Class ValidationDetails. */
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class ValidationMessage {

/** The field. */
private String field;

/** The message. */
private String message;
}

/** The Class RequestData. */
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class RequestData {

/** The link. */
private String link;

/** The request ID. */
private String requestID;
}
}
Loading

0 comments on commit 6bed96b

Please sign in to comment.