-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Config processing with lazy error handling (#127)
Co-authored-by: Tomas Dvorak <[email protected]>
- Loading branch information
1 parent
18c9f03
commit 757eb6d
Showing
6 changed files
with
249 additions
and
40 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
33 changes: 33 additions & 0 deletions
33
src/main/java/com/github/joschi/jadconfig/LazyValidationException.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,33 @@ | ||
package com.github.joschi.jadconfig; | ||
|
||
import com.github.joschi.jadconfig.response.ProcessingOutcome; | ||
import com.github.joschi.jadconfig.response.ProcessingResponse; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
public class LazyValidationException extends ValidationException { | ||
private final ProcessingResponse processingResponse; | ||
|
||
public LazyValidationException(ProcessingResponse result) { | ||
super(toMessage(result)); | ||
this.processingResponse = result; | ||
} | ||
|
||
private static String toMessage(ProcessingResponse result) { | ||
final List<String> stringBuilder = new LinkedList<>(); | ||
stringBuilder.add("Following errors ocurred during configuration processing:"); | ||
result.getOutcomes().stream() | ||
.filter(ProcessingOutcome::hasProblems) | ||
.flatMap(processingOutcome -> Stream.concat( | ||
processingOutcome.getFieldProcessingProblems().values().stream().map(Throwable::getMessage), | ||
processingOutcome.getValidationMethodsProblems().values().stream().map(Throwable::getMessage) | ||
)).forEach(stringBuilder::add); | ||
return String.join("\n", stringBuilder); | ||
} | ||
|
||
public ProcessingResponse getProcessingResponse() { | ||
return processingResponse; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/github/joschi/jadconfig/response/ProcessingOutcome.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,35 @@ | ||
package com.github.joschi.jadconfig.response; | ||
|
||
import java.util.Map; | ||
|
||
public class ProcessingOutcome { | ||
|
||
private final Object configurationBean; | ||
private final Map<String, Exception> fieldProcessingProblems; | ||
private final Map<String, Exception> validationMethodsProblems; | ||
|
||
public ProcessingOutcome(final Object configurationBean, | ||
final Map<String, Exception> fieldProcessingProblems, | ||
final Map<String, Exception> validationMethodsProblems) { | ||
this.configurationBean = configurationBean; | ||
this.fieldProcessingProblems = fieldProcessingProblems; | ||
this.validationMethodsProblems = validationMethodsProblems; | ||
} | ||
|
||
public boolean hasProblems() { | ||
return (fieldProcessingProblems != null && !fieldProcessingProblems.isEmpty()) || | ||
(validationMethodsProblems != null && !validationMethodsProblems.isEmpty()); | ||
} | ||
|
||
public Object getConfigurationBean() { | ||
return configurationBean; | ||
} | ||
|
||
public Map<String, Exception> getFieldProcessingProblems() { | ||
return fieldProcessingProblems; | ||
} | ||
|
||
public Map<String, Exception> getValidationMethodsProblems() { | ||
return validationMethodsProblems; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/github/joschi/jadconfig/response/ProcessingResponse.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,21 @@ | ||
package com.github.joschi.jadconfig.response; | ||
|
||
import java.util.List; | ||
|
||
public class ProcessingResponse { | ||
|
||
private final List<ProcessingOutcome> outcomes; | ||
|
||
public ProcessingResponse(List<ProcessingOutcome> outcomes) { | ||
this.outcomes = outcomes; | ||
} | ||
|
||
public List<ProcessingOutcome> getOutcomes() { | ||
return outcomes; | ||
} | ||
|
||
public boolean isSuccess() { | ||
return outcomes.stream().noneMatch(ProcessingOutcome::hasProblems); | ||
} | ||
} | ||
|
50 changes: 50 additions & 0 deletions
50
src/test/java/com/github/joschi/jadconfig/JadConfigLazyProcessingTest.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,50 @@ | ||
package com.github.joschi.jadconfig; | ||
|
||
import com.github.joschi.jadconfig.repositories.InMemoryRepository; | ||
import com.github.joschi.jadconfig.response.ProcessingOutcome; | ||
import com.github.joschi.jadconfig.response.ProcessingResponse; | ||
import com.github.joschi.jadconfig.testbeans.ValidatedConfigurationBean; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class JadConfigLazyProcessingTest { | ||
|
||
@Test | ||
public void testProcess() throws RepositoryException { | ||
HashMap<String, String> properties = new HashMap<>(); | ||
properties.put("test.byte", "1"); | ||
properties.put("test.short", "2"); | ||
properties.put("test.integer", "-3");//negative, smaller than test.short | ||
properties.put("test.integer.port", "70000"); //bigger than allowed port | ||
properties.put("test.long", "4"); | ||
properties.put("test.string", "Test"); | ||
Repository repository = new InMemoryRepository(properties); | ||
ValidatedConfigurationBean configurationBean = new ValidatedConfigurationBean(); | ||
JadConfig jadConfig = new JadConfig(repository, configurationBean); | ||
try { | ||
jadConfig.processFailingLazily(); | ||
Assert.fail("Should throw an exception!"); | ||
} catch (LazyValidationException e) { | ||
final ProcessingResponse response = e.getProcessingResponse(); | ||
assertFalse(response.isSuccess()); | ||
assertEquals(1, response.getOutcomes().size()); | ||
ProcessingOutcome processingOutcome = response.getOutcomes().get(0); | ||
assertEquals(configurationBean, processingOutcome.getConfigurationBean()); | ||
Map<String, Exception> fieldProcessingProblems = processingOutcome.getFieldProcessingProblems(); | ||
assertEquals(2, fieldProcessingProblems.size()); | ||
assertTrue(fieldProcessingProblems.containsKey("test.integer")); | ||
assertTrue(fieldProcessingProblems.containsKey("test.integer.port")); | ||
|
||
Map<String, Exception> validationMethodsProblems = processingOutcome.getValidationMethodsProblems(); | ||
assertEquals(1, validationMethodsProblems.size()); | ||
assertTrue(validationMethodsProblems.containsKey("myCustomValidatorMethod")); | ||
} | ||
|
||
|
||
} | ||
} |
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