Skip to content

Commit

Permalink
WAExHelper to build WebApplicationException
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed Apr 5, 2024
1 parent 8332fee commit e758149
Show file tree
Hide file tree
Showing 11 changed files with 312 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- WAExHelper to build WebApplicationException

### Changed

- native-helper-maven-plugin 1.3.5
Expand Down
79 changes: 79 additions & 0 deletions emp-exception-mapper/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>emp-exception-mapper</artifactId>

<parent>
<groupId>org.fugerit.java</groupId>
<artifactId>fj-service-helper-bom</artifactId>
<version>1.0.1-SNAPSHOT</version>
</parent>

<name>Eclipse MicroProfile Exception Mapper</name>
<description>Collection of MicroProfile ExceptionMappers</description>

<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>

<build>

<plugins>

<plugin>
<groupId>org.fugerit.java</groupId>
<artifactId>native-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>generate-native-configuration</id>
<phase>prepare-package</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<nativeHelperConfigPath>src/main/config/native-helper-config.yaml</nativeHelperConfigPath>
<reflectConfigJsonOutputPath>${project.build.outputDirectory}/META-INF/native-image/reflect-config.json</reflectConfigJsonOutputPath>
<warnOnError>true</warnOnError>
</configuration>
</execution>
</executions>
</plugin>

</plugins>

</build>

<dependencies>

<dependency>
<groupId>org.fugerit.java</groupId>
<artifactId>emp-service-model</artifactId>
</dependency>

<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-jackson</artifactId>
<version>${quarkus.platform.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

<organization>
<url>https://www.fugerit.org</url>
<name>Fugerit</name>
</organization>

<url>https://www.fugerit.org/perm/venus/</url>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.fugerit.java.emp.em;

import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;
import lombok.extern.slf4j.Slf4j;
import org.fugerit.java.emp.sm.service.ServiceMessage;
import org.fugerit.java.emp.sm.service.ServiceResponse;
import org.fugerit.java.emp.sm.service.ServiceResponseHelper;

@Slf4j
@Provider
public class GenericExceptionMapper implements ExceptionMapper<WebApplicationException> {
@Override
public Response toResponse(WebApplicationException ex) {
if ( ex.getResponse().getEntity() != null || ex.getMessage() == null ) {
return ex.getResponse();
} else {
Response.Status status = Response.Status.fromStatusCode( ex.getResponse().getStatus() );
return Response.status( status ).entity(
new ServiceResponse().addAllBySeverity( ServiceResponseHelper.newMessageByStatus( status, ex.getMessage() ) )
).build();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package test.org.fugerit.java.emp.em;

import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.Response;
import lombok.extern.slf4j.Slf4j;
import org.fugerit.java.emp.em.GenericExceptionMapper;
import org.fugerit.java.emp.ex.WAExHelper;
import org.fugerit.java.emp.sm.service.ServiceMessage;
import org.fugerit.java.emp.sm.service.ServiceResponse;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

@Slf4j
class TestGenericExceptionMapper {

private static final String TEST_MESSAGE = "test error";

@Test
void test() {
GenericExceptionMapper gem = new GenericExceptionMapper();
Response r1 = gem.toResponse( WAExHelper.newEx( TEST_MESSAGE+"1" ) );
Assertions.assertEquals( TEST_MESSAGE+"1", ((ServiceResponse)r1.getEntity()).getErrors().get( 0 ).getText() );
Response r2 = gem.toResponse( new WebApplicationException( TEST_MESSAGE+"2", (Response) null ) );
Assertions.assertEquals( TEST_MESSAGE+"2", ((ServiceResponse)r2.getEntity()).getErrors().get( 0 ).getText() );
}

}
11 changes: 9 additions & 2 deletions emp-service-model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@

<dependencies>

<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.eclipse.microprofile.openapi</groupId>
<artifactId>microprofile-openapi-api</artifactId>
Expand All @@ -63,8 +69,9 @@
</dependency>

<dependency>
<groupId>org.fugerit.java</groupId>
<artifactId>native-helper-graalvm</artifactId>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-jackson</artifactId>
<version>${quarkus.platform.version}</version>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.fugerit.java.emp.ex;

import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.Response;
import org.fugerit.java.emp.sm.service.ServiceMessage;
import org.fugerit.java.emp.sm.service.ServiceResponse;
import org.fugerit.java.emp.sm.service.ServiceResponseHelper;

public class WAExHelper {

private WAExHelper() {}

public static WebApplicationException newEx( String message) {
return newEx( Response.Status.INTERNAL_SERVER_ERROR, message );
}

public static WebApplicationException newEx( Response.Status status, String message ) {
return newEx( null, status, message );
}

public static WebApplicationException newEx( Throwable cause, Response.Status status, String message ) {
return newEx( cause, status, ServiceResponseHelper.newMessageByStatus( status, message ) );
}

public static WebApplicationException newEx( Response.Status status, ServiceMessage sm ) {
return newEx( null, status, sm );
}

public static WebApplicationException newEx( Throwable cause, Response.Status status, ServiceMessage sm ) {
return newEx( cause, status, new ServiceResponse().addAllBySeverity( sm ) );
}

public static WebApplicationException newEx( Response.Status status, ServiceResponse sr ) {
return newEx( null, status, sr );
}

public static WebApplicationException newEx( Throwable cause, Response.Status status, ServiceResponse sr ) {
return new WebApplicationException( cause , Response.status( status ).entity( sr ).build() );
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.fugerit.java.emp.sm.service;

import java.util.Arrays;
import java.util.List;

import org.eclipse.microprofile.openapi.annotations.media.Schema;
Expand All @@ -23,5 +24,14 @@ public class ServiceResponse {

@Schema(description = "Success messages list", example = "[{\"code\":\"200001\",\"severity\":\"S\",\"text\":\"sample success\"}]" , required = false )
@Getter @Setter private List<ServiceMessage> success;


public ServiceResponse addAllBySeverity(ServiceMessage... messages) {
return this.addAllBySeverity( Arrays.asList( messages ) );
}

public ServiceResponse addAllBySeverity(List<ServiceMessage> messages) {
ServiceResponseHelper.addAllBySeverity( this, messages );
return this;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.fugerit.java.emp.sm.service;

import jakarta.ws.rs.core.Response;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -34,6 +36,55 @@ public static List<ServiceMessage> filterBySeverity( List<ServiceMessage> messag
return messages.stream().filter( m -> m.getSeverity().equalsIgnoreCase( severity ) ).collect( Collectors.toList() );
}

public static ServiceMessage newMessageByStatus(Response.Status status, String message ) {
switch ( status.getFamily() ) {
case INFORMATIONAL:
return newDefaultInfoMessage( message );
case SUCCESSFUL:
return newDefaultSuccessMessage( message );
case OTHER:
return newDefaultWarningMessage( message );
default:
return newDefaultErrorMessage( message );
}
}

public static ServiceMessage newMessage( String code, String severity, String message ) {
return new ServiceMessage( code, severity, message );
}

public static ServiceMessage newDefaultErrorMessage( String message ) {
return newErrorMessage( ServiceMessage.SEVERITY_ERROR, message );
}

public static ServiceMessage newDefaultWarningMessage( String message ) {
return newWarningMessage( ServiceMessage.SEVERITY_WARNING, message );
}

public static ServiceMessage newDefaultSuccessMessage( String message ) {
return newSuccessMessage( ServiceMessage.SEVERITY_SUCCESS, message );
}

public static ServiceMessage newDefaultInfoMessage( String message ) {
return newSuccessMessage( ServiceMessage.SEVERITY_INFO, message );
}

public static ServiceMessage newErrorMessage( String code, String message ) {
return newMessage( code, ServiceMessage.SEVERITY_ERROR, message );
}

public static ServiceMessage newWarningMessage( String code, String message ) {
return newMessage( code, ServiceMessage.SEVERITY_WARNING, message );
}

public static ServiceMessage newSuccessMessage( String code, String message ) {
return newMessage( code, ServiceMessage.SEVERITY_SUCCESS, message );
}

public static ServiceMessage newInfoMessage( String code, String message ) {
return newMessage( code, ServiceMessage.SEVERITY_INFO, message );
}

public static void addAllBySeverity(ServiceResponse response, List<ServiceMessage> messages) {
addErrors( response, filterBySeverity( messages, ServiceMessage.SEVERITY_ERROR ) );
addWarnings( response, filterBySeverity( messages, ServiceMessage.SEVERITY_WARNING ) );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package test.org.fugerit.java.emp.ex;

import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.Response;
import org.fugerit.java.emp.ex.WAExHelper;
import org.fugerit.java.emp.sm.service.ServiceMessage;
import org.fugerit.java.emp.sm.service.ServiceResponse;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class TestWAExHelper {

private static final String TEST_MESSAGE = "My Message";

private ServiceResponse getResponse(WebApplicationException ex) {
return ((ServiceResponse)ex.getResponse().getEntity());
}

private String getFirstError(WebApplicationException ex) {
return this.getResponse( ex ).getErrors().get( 0 ).getText();
}

@Test
void test() {
Assertions.assertEquals( TEST_MESSAGE, this.getFirstError( WAExHelper.newEx( TEST_MESSAGE ) ) );
Assertions.assertEquals( TEST_MESSAGE, this.getFirstError( WAExHelper.newEx(Response.Status.INTERNAL_SERVER_ERROR, TEST_MESSAGE ) ) );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.fugerit.java.emp.sm.service.ServiceMessage;

import lombok.extern.slf4j.Slf4j;
import org.fugerit.java.emp.sm.service.ServiceResponseHelper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand All @@ -24,7 +25,15 @@ void testMessageAllArgs() {
Assertions.assertEquals( TEST_MESSAGE , message.getText() );
Assertions.assertEquals( ServiceMessage.SEVERITY_ERROR , message.getSeverity() );
}


@Test
void testNewMessage() {
Assertions.assertEquals( TEST_MESSAGE, ServiceResponseHelper.newDefaultErrorMessage( TEST_MESSAGE ).getText() );
Assertions.assertEquals( TEST_MESSAGE, ServiceResponseHelper.newDefaultWarningMessage( TEST_MESSAGE ).getText() );
Assertions.assertEquals( TEST_MESSAGE, ServiceResponseHelper.newDefaultSuccessMessage( TEST_MESSAGE ).getText() );
Assertions.assertEquals( TEST_MESSAGE, ServiceResponseHelper.newDefaultInfoMessage( TEST_MESSAGE ).getText() );
}

@Test
void testMessageNoArgs() {
ServiceMessage message = new ServiceMessage();
Expand Down
Loading

0 comments on commit e758149

Please sign in to comment.