-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test moved from junit4 to junit5 ServiceResponseHelper utility to handle messages
- Loading branch information
Showing
11 changed files
with
133 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
emp-service-model/src/main/java/org/fugerit/java/emp/sm/service/ServiceResponseHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.fugerit.java.emp.sm.service; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class 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; | ||
} | ||
|
||
public static void addErrors(ServiceResponse response, List<ServiceMessage> messages) { | ||
response.setErrors( addHelper( response.getErrors(), messages ) ); | ||
} | ||
|
||
public static void addWarnings(ServiceResponse response, List<ServiceMessage> messages) { | ||
response.setWarnings( addHelper( response.getWarnings(), messages ) ); | ||
} | ||
|
||
public static void addInfos(ServiceResponse response, List<ServiceMessage> messages) { | ||
response.setInfos( addHelper( response.getInfos(), messages ) ); | ||
} | ||
|
||
public static void addSuccess(ServiceResponse response, List<ServiceMessage> messages) { | ||
response.setSuccess( addHelper( response.getSuccess(), messages ) ); | ||
} | ||
|
||
public static List<ServiceMessage> filterBySeverity( List<ServiceMessage> messages, String severity ) { | ||
return messages.stream().filter( m -> m.getSeverity().equalsIgnoreCase( severity ) ).collect( Collectors.toList() ); | ||
} | ||
|
||
public static void addAllBySeverity(ServiceResponse response, List<ServiceMessage> messages) { | ||
addErrors( response, filterBySeverity( messages, ServiceMessage.SEVERITY_ERROR ) ); | ||
addWarnings( response, filterBySeverity( messages, ServiceMessage.SEVERITY_WARNING ) ); | ||
addInfos( response, filterBySeverity( messages, ServiceMessage.SEVERITY_INFO ) ); | ||
addSuccess( response, filterBySeverity( messages, ServiceMessage.SEVERITY_SUCCESS ) ); | ||
} | ||
|
||
} |
24 changes: 12 additions & 12 deletions
24
emp-service-model/src/test/java/test/org/fugerit/java/emp/sm/service/TestServiceMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,39 @@ | ||
package test.org.fugerit.java.emp.sm.service; | ||
|
||
import org.fugerit.java.emp.sm.service.ServiceMessage; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@Slf4j | ||
public class TestServiceMessage { | ||
class TestServiceMessage { | ||
|
||
private static final String TEST_CODE = "500001"; | ||
private static final String TEST_MESSAGE = "Test error"; | ||
|
||
public static ServiceMessage newDefaultMessage( String severity ) { | ||
static ServiceMessage newDefaultMessage( String severity ) { | ||
return new ServiceMessage(TEST_CODE, severity, TEST_MESSAGE); | ||
} | ||
|
||
@Test | ||
public void testMessageAllArgs() { | ||
void testMessageAllArgs() { | ||
ServiceMessage message = new ServiceMessage( TEST_CODE , ServiceMessage.SEVERITY_ERROR, TEST_MESSAGE); | ||
log.info( "message : {}", message ); | ||
Assert.assertEquals( TEST_CODE , message.getCode() ); | ||
Assert.assertEquals( TEST_MESSAGE , message.getText() ); | ||
Assert.assertEquals( ServiceMessage.SEVERITY_ERROR , message.getSeverity() ); | ||
Assertions.assertEquals( TEST_CODE , message.getCode() ); | ||
Assertions.assertEquals( TEST_MESSAGE , message.getText() ); | ||
Assertions.assertEquals( ServiceMessage.SEVERITY_ERROR , message.getSeverity() ); | ||
} | ||
|
||
@Test | ||
public void testMessageNoArgs() { | ||
void testMessageNoArgs() { | ||
ServiceMessage message = new ServiceMessage(); | ||
message.setCode( TEST_CODE ); | ||
message.setSeverity( ServiceMessage.SEVERITY_WARNING ); | ||
message.setText( TEST_MESSAGE ); | ||
Assert.assertEquals( TEST_CODE , message.getCode() ); | ||
Assert.assertEquals( TEST_MESSAGE , message.getText() ); | ||
Assert.assertEquals( ServiceMessage.SEVERITY_WARNING , message.getSeverity() ); | ||
Assertions.assertEquals( TEST_CODE , message.getCode() ); | ||
Assertions.assertEquals( TEST_MESSAGE , message.getText() ); | ||
Assertions.assertEquals( ServiceMessage.SEVERITY_WARNING , message.getSeverity() ); | ||
} | ||
|
||
} |
38 changes: 18 additions & 20 deletions
38
...service-model/src/test/java/test/org/fugerit/java/emp/sm/service/TestServiceResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,30 @@ | ||
package test.org.fugerit.java.emp.sm.service; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Arrays; | ||
import java.util.stream.Collectors; | ||
|
||
import org.fugerit.java.emp.sm.service.ServiceMessage; | ||
import org.fugerit.java.emp.sm.service.ServiceResponse; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.fugerit.java.emp.sm.service.ServiceResponseHelper; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class TestServiceResponse { | ||
class TestServiceResponse { | ||
|
||
public static List<ServiceMessage> newList( String severity ) { | ||
List<ServiceMessage> list = new ArrayList<>(); | ||
list.add( TestServiceMessage.newDefaultMessage(severity) ); | ||
return list; | ||
} | ||
|
||
@Test | ||
public void testServiceResponse() { | ||
void testServiceResponse() { | ||
ServiceResponse response = new ServiceResponse(); | ||
response.setErrors( newList( ServiceMessage.SEVERITY_ERROR ) ); | ||
response.setWarnings( newList( ServiceMessage.SEVERITY_WARNING ) ); | ||
response.setInfos( newList( ServiceMessage.SEVERITY_INFO ) ); | ||
response.setSuccess( newList( ServiceMessage.SEVERITY_SUCCESS ) ); | ||
Assert.assertNotNull( response.getErrors() ); | ||
Assert.assertNotNull( response.getWarnings() ); | ||
Assert.assertNotNull( response.getInfos() ); | ||
Assert.assertNotNull( response.getSuccess() ); | ||
ServiceResponseHelper.addAllBySeverity( response, | ||
Arrays.stream(ServiceMessage.Severity.values()) | ||
.map( s -> TestServiceMessage.newDefaultMessage( s.getLevel() ) ) | ||
.collect( Collectors.toList() ) | ||
); | ||
ServiceResponseHelper.addErrors( response, | ||
Arrays.asList( new ServiceMessage( "custom", ServiceMessage.SEVERITY_ERROR, "Second error message" ) ) ); | ||
Assertions.assertNotNull( response.getErrors() ); | ||
Assertions.assertNotNull( response.getWarnings() ); | ||
Assertions.assertNotNull( response.getInfos() ); | ||
Assertions.assertNotNull( response.getSuccess() ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.