Skip to content

Commit

Permalink
Documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed Apr 5, 2024
1 parent c30bda8 commit ad96f43
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Refer to subproject's changelog for details :

- [emp-service-model](https://github.com/fugerit-org/fj-service-helper-bom/blob/main/emp-service-model/CHANGELOG.md)
- [emp-exception-mapper](https://github.com/fugerit-org/fj-service-helper-bom/blob/main/emp-exception-mapper/CHANGELOG.md)
- [data-service-base](https://github.com/fugerit-org/fj-service-helper-bom/blob/main/data-service-base/CHANGELOG.md)
- [data-service-jvfs](https://github.com/fugerit-org/fj-service-helper-bom/blob/main/data-service-jvfs/CHANGELOG.md)

## [Unreleased]

## [1.1.0 - 2024-04-05]

### Added

- WAExHelper to build WebApplicationException
- module emp-exception-mapper [incubator]

### Changed

Expand Down
14 changes: 14 additions & 0 deletions emp-exception-mapper/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [1.1.0 - 2024-04-05]

### Added

- GenericExceptionMapper (incubator)
3 changes: 3 additions & 0 deletions emp-service-model/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.1.0 - 2024-04-05]

### Added

- WAExHelper to build WebApplicationException
- [emp-service-model] native support : relfect-config.json

## [0.1.0 - 2023-11-12]
Expand Down
79 changes: 78 additions & 1 deletion emp-service-model/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,81 @@

[![Keep a Changelog v1.1.0 badge](https://img.shields.io/badge/changelog-Keep%20a%20Changelog%20v1.1.0-%23E05735)](https://github.com/fugerit-org/fj-service-helper-bom/blob/main/emp-service-model/CHANGELOG.md)

This project contains a collection of model object (like beans or POJOS) to be used in service implementation.
This project contains a collection of model object (like beans or POJOS) to be used in service implementation.

## 1. ServiceMessage

Sample usage :

```java
ServiceResponse response = new ServiceResponse()
.addAllBySeverity( ServiceMessage.newMessage( "400001", ServiceMessage.Severity.ERROR, "Test bad request message" ),
ServiceMessage.newMessage( ServiceMessage.Severity.INFO, "Test info message" ),
ServiceMessage.newMessage( ServiceMessage.Severity.WARNING, "Test warning message" ) );
```

Json output :

```json
{
"errors" : [ {
"code" : "400001",
"severity" : "E",
"text" : "Test bad request message"
} ],
"warnings" : [ {
"code" : "W",
"severity" : "W",
"text" : "Test warning message"
} ],
"infos" : [ {
"code" : "I",
"severity" : "I",
"text" : "Test info message"
} ]
}
```

## 2. WAExHelper

Sample usage :

```java
throw WAExHelper.newEx( Response.Status.BAD_REQUEST, "Test bad request message" );
```
json output :

```json
{
"status" : 400,
"entity" : {
"errors" : [ {
"code" : "E",
"severity" : "E",
"text" : "Test bad request message"
} ]
},
"headers" : { },
"entityStream" : null,
"stringHeaders" : { },
"entityAnnotations" : null,
"closed" : false,
"length" : -1,
"location" : null,
"language" : null,
"date" : null,
"lastModified" : null,
"statusInfo" : {
"reasonPhrase" : "Bad Request",
"family" : "CLIENT_ERROR",
"statusCode" : 400
},
"mediaType" : null,
"cookies" : { },
"entityTag" : null,
"links" : [ ],
"metadata" : { },
"allowedMethods" : [ ]
}

```
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static WebApplicationException newEx( String message) {
}

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

public static WebApplicationException newEx( Throwable cause, Response.StatusType status, String message ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,12 @@ public enum Severity {
@Schema(description = "Message test", example = "User not authorized to access the resource." , required = false, type = SchemaType.STRING )
@Getter @Setter private String text;

public static ServiceMessage newMessage( Severity severity, String message ) {
return newMessage( severity.getLevel(), severity, message );
}

public static ServiceMessage newMessage( String code, Severity severity, String message ) {
return ServiceResponseHelper.newMessage( code, severity.getLevel(), message );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ private ServiceResponseHelper() {}
private static List<ServiceMessage> addHelper( List<ServiceMessage> currentMessages, List<ServiceMessage> messages ) {
List<ServiceMessage> result = currentMessages == null ? new ArrayList<>() : currentMessages;
result.addAll( messages );
return result;
return result.isEmpty() ? null : result;
}

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

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.Response;
import lombok.extern.slf4j.Slf4j;
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.fugerit.java.emp.sm.service.ServiceResponseHelper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.StringWriter;

@Slf4j
class TestWAExHelper {

private static final String TEST_MESSAGE = "My Message";
Expand All @@ -29,4 +35,14 @@ void test() {
Assertions.assertEquals( TEST_MESSAGE, this.getFirstError( WAExHelper.newEx(Response.Status.INTERNAL_SERVER_ERROR, new ServiceResponse().addAllBySeverity( ServiceResponseHelper.newDefaultErrorMessage( TEST_MESSAGE ) ) ) ) );
}

@Test
void testSampleUsage() throws IOException {
WebApplicationException wae = WAExHelper.newEx( Response.Status.BAD_REQUEST, "Test bad request message" );
try (StringWriter writer = new StringWriter()) {
new ObjectMapper().writerWithDefaultPrettyPrinter().writeValue( writer, wae.getResponse() );
log.info( "test response : \n{}", writer );
}
Assertions.assertEquals( Response.Status.BAD_REQUEST.getStatusCode(), wae.getResponse().getStatus() );
}

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

import java.io.IOException;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.stream.Collectors;

import com.fasterxml.jackson.databind.ObjectMapper;
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;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

@Slf4j
class TestServiceResponse {

@Test
Expand All @@ -26,5 +31,18 @@ void testServiceResponse() {
Assertions.assertNotNull( response.getInfos() );
Assertions.assertNotNull( response.getSuccess() );
}

@Test
void testSampleUsage() throws IOException {
ServiceResponse response = new ServiceResponse()
.addAllBySeverity( ServiceMessage.newMessage( "400001", ServiceMessage.Severity.ERROR, "Test bad request message" ),
ServiceMessage.newMessage( ServiceMessage.Severity.INFO, "Test info message" ),
ServiceMessage.newMessage( ServiceMessage.Severity.WARNING, "Test warning message" ) );
try (StringWriter writer = new StringWriter()) {
new ObjectMapper().writerWithDefaultPrettyPrinter().writeValue( writer, response );
log.info( "test response : \n{}", writer );
}
Assertions.assertEquals( 1, response.getErrors().size() );
}

}

0 comments on commit ad96f43

Please sign in to comment.