Skip to content

Commit

Permalink
fj-bom set to 1.6.3
Browse files Browse the repository at this point in the history
test moved from junit4 to junit5
ServiceResponseHelper utility to handle messages
  • Loading branch information
fugerit79 committed Mar 24, 2024
1 parent b99aefe commit ff024f6
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 80 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- ServiceResponseHelper utility to handle messages

### Changed

- fj-bom set to 1.6.2
- fj-bom set to 1.6.3
- test moved from junit4 to junit5
- quarkus version for demo app set to 3.8.3
- fj-core version set to 8.5.3

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

import org.fugerit.java.dsb.DataService;
import org.fugerit.java.dsb.DataServiceIO;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import javax.xml.crypto.Data;
import java.io.IOException;
import java.io.InputStream;

public class TestDataService {

class TestDataService {

@Test
public void testBase() throws IOException {
void testBase() throws IOException {
String testId = DataServiceIO.generateId();
DataService ds = new DataService() {
@Override
Expand All @@ -25,7 +26,7 @@ public String save(InputStream data) throws IOException {
}

};
Assert.assertEquals( testId, ds.save( null, null ) );
Assertions.assertEquals( testId, ds.save( null, null ) );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,21 @@
import lombok.extern.slf4j.Slf4j;
import org.fugerit.java.dsb.attributes.SimpleServiceMap;
import org.fugerit.java.dsb.attributes.impl.SimpleServiceMapDefault;
import org.junit.Assert;
import org.junit.Test;

import static org.junit.Assert.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

@Slf4j
public class ServiceMapDefaultTest {
class ServiceMapDefaultTest {

@Test
public void testMapSimple() {
void testMapSimple() {
String key = "testKey";
String value = "testValue";
SimpleServiceMap map = new SimpleServiceMapDefault();
map.set( key, value );
Assert.assertEquals( value, map.get( key ) );
Assertions.assertEquals( value, map.get( key ) );
map.remove( key );
Assert.assertNull( map.get( key ) );
Assertions.assertNull( map.get( key ) );
log.info( "test key:{}, value:{} ok", key, value );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
import org.fugerit.java.dsb.DataServiceIO;
import org.fugerit.java.dsb.DataServiceWrapper;
import org.fugerit.java.dsb.file.FileDataServiceUUID;
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 TestFileDataServiceUUID {
class TestFileDataServiceUUID {

private static final File STORE_FOLDER = new File( "target" );

Expand All @@ -29,35 +29,35 @@ private InputStream newData() {
}

@Test
public void testFileDataService() throws IOException {
void testFileDataService() throws IOException {
FileDataServiceUUID service = new FileDataServiceUUID();
service.setStoreFolder(STORE_FOLDER);
DataServiceWrapper wrapper = new DataServiceWrapper( service );
log.info( "data service : {} : {}", wrapper, wrapper.unwrap() );
try ( InputStream data = this.newData() ) {
String id = DataServiceIO.saveBase64( wrapper , TEST_DATA_BASE64 );
String base64 = DataServiceIO.loadBase64( wrapper , id );
Assert.assertEquals( TEST_DATA_BASE64 , base64 );
Assertions.assertEquals( TEST_DATA_BASE64 , base64 );
}
try ( InputStream data = this.newData() ) {
String id = DataServiceIO.saveBase64( wrapper , TEST_DATA_BASE64, "res_name.txt" );
String base64 = DataServiceIO.loadBase64( wrapper , id );
Assert.assertEquals( TEST_DATA_BASE64 , base64 );
Assertions.assertEquals( TEST_DATA_BASE64 , base64 );
}
wrapper.wrap( service );
Assert.assertThrows( NullPointerException.class , () -> new DataServiceWrapper( null ) );
Assertions.assertThrows( NullPointerException.class , () -> new DataServiceWrapper( null ) );
}


@Test
public void testCreate() throws IOException {
Assert.assertThrows( NullPointerException.class , () -> new FileDataServiceUUID( null ) );
void testCreate() throws IOException {
Assertions.assertThrows( NullPointerException.class , () -> new FileDataServiceUUID( null ) );
FileDataServiceUUID service = new FileDataServiceUUID( STORE_FOLDER );
Assert.assertThrows( NullPointerException.class , () -> service.setStoreFolder( null ) );
Assertions.assertThrows( NullPointerException.class , () -> service.setStoreFolder( null ) );
log.info( "service : {}", service );
Assert.assertEquals( STORE_FOLDER , service.getStoreFolder() );
Assert.assertNull( DataServiceIO.loadBase64( service, "not-exists" ) );
Assert.assertNotNull( new DataServiceWrapper() );
Assertions.assertEquals( STORE_FOLDER , service.getStoreFolder() );
Assertions.assertNull( DataServiceIO.loadBase64( service, "not-exists" ) );
Assertions.assertNotNull( new DataServiceWrapper() );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
import org.fugerit.java.core.jvfs.file.RealJMount;
import org.fugerit.java.dsb.DataServiceIO;
import org.fugerit.java.dsb.jvfs.JVFSDataService;
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 TestJVFSDataService {
class TestJVFSDataService {

private static final JFile STORE_FOLDER = SafeFunction.get( () -> RealJMount.createJVFS( new File( "target" ) ).getRoot() );

Expand All @@ -30,26 +30,26 @@ private InputStream newData() {
}

@Test
public void testFileDataService() throws IOException {
void testFileDataService() throws IOException {
JVFSDataService service = new JVFSDataService();
service.setStoreJFolder(STORE_FOLDER);
log.info( "data service : {}", service );
try ( InputStream data = this.newData() ) {
String id = DataServiceIO.saveBase64( service , TEST_DATA_BASE64 );
String base64 = DataServiceIO.loadBase64( service , id );
Assert.assertEquals( TEST_DATA_BASE64 , base64 );
Assertions.assertEquals( TEST_DATA_BASE64 , base64 );
}
}


@Test
public void testCreate() throws IOException {
Assert.assertThrows( NullPointerException.class , () -> new JVFSDataService( null ) );
void testCreate() throws IOException {
Assertions.assertThrows( NullPointerException.class , () -> new JVFSDataService( null ) );
JVFSDataService service = new JVFSDataService( STORE_FOLDER );
Assert.assertThrows( NullPointerException.class , () -> service.setStoreJFolder( null ) );
Assertions.assertThrows( NullPointerException.class , () -> service.setStoreJFolder( null ) );
log.info( "service : {}", service );
Assert.assertEquals( STORE_FOLDER , service.getStoreJFolder() );
Assert.assertNull( DataServiceIO.loadBase64( service, "not-exists" ) );
Assertions.assertEquals( STORE_FOLDER , service.getStoreJFolder() );
Assertions.assertNull( DataServiceIO.loadBase64( service, "not-exists" ) );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,16 @@ public class ServiceMessage {
* Constant for severity ERROR (E)
*/
public static final String SEVERITY_ERROR = "E";


public enum Severity {
SUCCESS( SEVERITY_SUCCESS ),
INFO( SEVERITY_INFO ),
WARNING( SEVERITY_WARNING ),
ERROR( SEVERITY_ERROR );
@Getter private String level;
private Severity( String level ) { this.level = level; }
}

/**
* The code of the message
*
Expand Down
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 ) );
}

}
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() );
}

}
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() );
}

}
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.fugerit.java</groupId>
<artifactId>fj-bom</artifactId>
<version>1.6.2</version>
<version>1.6.3</version>
<relativePath></relativePath>
</parent>

Expand Down Expand Up @@ -170,8 +170,8 @@
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

Expand Down
Loading

0 comments on commit ff024f6

Please sign in to comment.