-
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
- Loading branch information
1 parent
18c9f03
commit 28af121
Showing
4 changed files
with
213 additions
and
34 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
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; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
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,29 @@ | ||
package com.github.joschi.jadconfig.response; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class ProcessingResponse { | ||
|
||
private List<ProcessingOutcome> outcomes; | ||
|
||
public ProcessingResponse() { | ||
this.outcomes = new LinkedList<>(); | ||
} | ||
|
||
public void addOutcome(final Object configurationBean, | ||
final Map<String, Exception> fieldProcessingProblems, | ||
final Map<String, Exception> validationMethodsProblems) { | ||
outcomes.add(new ProcessingOutcome(configurationBean, fieldProcessingProblems, validationMethodsProblems)); | ||
} | ||
|
||
public List<ProcessingOutcome> getOutcomes() { | ||
return outcomes; | ||
} | ||
|
||
public boolean isSuccess() { | ||
return outcomes.stream().noneMatch(ProcessingOutcome::hasProblems); | ||
} | ||
} | ||
|
47 changes: 47 additions & 0 deletions
47
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,47 @@ | ||
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.Test; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class JadConfigLazyProcessingTest { | ||
|
||
private JadConfig jadConfig; | ||
private Repository repository; | ||
|
||
|
||
@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 = new InMemoryRepository(properties); | ||
ValidatedConfigurationBean configurationBean = new ValidatedConfigurationBean(); | ||
jadConfig = new JadConfig(repository, configurationBean); | ||
|
||
ProcessingResponse response = jadConfig.processFailingLazily(); | ||
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("validate")); | ||
} | ||
} |