Skip to content

Commit

Permalink
Code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed Apr 5, 2024
1 parent 5c8eb03 commit c30bda8
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 15 deletions.
1 change: 1 addition & 0 deletions emp-exception-mapper/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
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;

Expand All @@ -14,7 +13,7 @@
public class GenericExceptionMapper implements ExceptionMapper<WebApplicationException> {
@Override
public Response toResponse(WebApplicationException ex) {
if ( ex.getResponse().getEntity() != null || ex.getMessage() == null ) {
if ( ex.getResponse().getEntity() != null ) {
return ex.getResponse();
} else {
Response.Status status = Response.Status.fromStatusCode( ex.getResponse().getStatus() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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;

Expand All @@ -22,6 +23,10 @@ void test() {
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() );
Response r3 = gem.toResponse( new WebApplicationException( null, null, Response.status( Response.Status.INTERNAL_SERVER_ERROR ).entity(
new ServiceResponse().addAllBySeverity( ServiceResponseHelper.newDefaultErrorMessage( TEST_MESSAGE+"3" ) )
).build() ) );
Assertions.assertEquals( TEST_MESSAGE+"3", ((ServiceResponse)r3.getEntity()).getErrors().get( 0 ).getText() );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,27 @@ public static WebApplicationException newEx( String message) {
return newEx( Response.Status.INTERNAL_SERVER_ERROR, message );
}

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

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

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

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ 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 ) {
public static ServiceMessage newMessageByStatus(Response.StatusType status, String message ) {
switch ( status.getFamily() ) {
case INFORMATIONAL:
return newDefaultInfoMessage( message );
Expand Down Expand Up @@ -66,7 +66,7 @@ public static ServiceMessage newDefaultSuccessMessage( String message ) {
}

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

public static ServiceMessage newErrorMessage( String code, String message ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;

Expand All @@ -24,6 +25,8 @@ private String getFirstError(WebApplicationException ex) {
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 ) ) );
Assertions.assertEquals( TEST_MESSAGE, this.getFirstError( WAExHelper.newEx(Response.Status.INTERNAL_SERVER_ERROR, ServiceResponseHelper.newDefaultErrorMessage( TEST_MESSAGE ) ) ) );
Assertions.assertEquals( TEST_MESSAGE, this.getFirstError( WAExHelper.newEx(Response.Status.INTERNAL_SERVER_ERROR, new ServiceResponse().addAllBySeverity( ServiceResponseHelper.newDefaultErrorMessage( TEST_MESSAGE ) ) ) ) );
}

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

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

import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -28,10 +29,11 @@ void testMessageAllArgs() {

@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() );
Assertions.assertEquals( TEST_MESSAGE, ServiceResponseHelper.newMessageByStatus( Response.Status.OK, TEST_MESSAGE ).getText() );
Assertions.assertEquals( TEST_MESSAGE, ServiceResponseHelper.newMessageByStatus( Response.Status.INTERNAL_SERVER_ERROR, TEST_MESSAGE ).getText() );
Assertions.assertEquals( TEST_MESSAGE, ServiceResponseHelper.newMessageByStatus( Response.Status.BAD_REQUEST, TEST_MESSAGE ).getText() );
Assertions.assertEquals( TEST_MESSAGE, ServiceResponseHelper.newMessageByStatus( new TestStatus( 100, "Test Info" ), TEST_MESSAGE ).getText() );
Assertions.assertEquals( TEST_MESSAGE, ServiceResponseHelper.newMessageByStatus( new TestStatus( 800, "Test Warning" ), TEST_MESSAGE ).getText() );
}

@Test
Expand All @@ -46,3 +48,35 @@ void testMessageNoArgs() {
}

}

class TestStatus implements Response.StatusType {

private int statusCode;

private String reasonPhrase;

public TestStatus(int statusCode, String reasonPhrase) {
this.statusCode = statusCode;
this.reasonPhrase = reasonPhrase;
}

@Override
public Response.Status toEnum() {
return Response.StatusType.super.toEnum();
}

@Override
public int getStatusCode() {
return this.statusCode;
}

@Override
public Response.Status.Family getFamily() {
return Response.Status.Family.familyOf( this.getStatusCode() );
}

@Override
public String getReasonPhrase() {
return this.reasonPhrase;
}
}
2 changes: 1 addition & 1 deletion fj-service-helper-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive-jackson</artifactId>
<artifactId>quarkus-rest-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
Expand Down

0 comments on commit c30bda8

Please sign in to comment.