Skip to content

Commit c407360

Browse files
authored
Merge pull request #472 from fugerit-org/471-enhancement-fj-doc-val-expose-failed-validation-reason
DocTypeValidationResult has now validation message and exceptions #471
2 parents ab1ef29 + de5ef19 commit c407360

File tree

12 files changed

+69
-5
lines changed

12 files changed

+69
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- [fj-doc-val-*] DocTypeValidationResult has now validation message and exceptions
13+
1014
### Changed
1115

1216
- quarkus-version set to 3.24.3 across all the modules <https://github.com/fugerit-org/fj-doc/issues/469>
Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package org.fugerit.java.doc.val.core;
2+
import org.fugerit.java.core.util.ObjectUtils;
23
import org.fugerit.java.core.util.result.BasicResult;
34

5+
import java.util.ArrayList;
6+
import java.util.List;
7+
48
public class DocTypeValidationResult extends BasicResult {
59

610
public DocTypeValidationResult(int resultCode) {
711
super(resultCode);
812
}
9-
13+
1014
public static DocTypeValidationResult newOk() {
1115
return new DocTypeValidationResult( RESULT_CODE_OK );
1216
}
@@ -15,4 +19,28 @@ public static DocTypeValidationResult newFail() {
1519
return new DocTypeValidationResult( RESULT_CODE_KO );
1620
}
1721

22+
private static final String ATT_VALIDATION_MESSAGE = "validation-message";
23+
24+
private static final String ATT_VALIDATION_EXECPTIONS = "validation-exceptions";
25+
26+
27+
public DocTypeValidationResult withValidationMessage(String validationMessage) {
28+
this.getInfoMap().put(ATT_VALIDATION_MESSAGE, validationMessage);
29+
return this;
30+
}
31+
32+
public String getValidationMessage() {
33+
return (String)this.getInfoMap().get(ATT_VALIDATION_MESSAGE);
34+
}
35+
36+
public List<Exception> getValidationExceptions() {
37+
return ObjectUtils.objectWithDefault((List<Exception>)this.getInfoMap().get(ATT_VALIDATION_EXECPTIONS), new ArrayList<>());
38+
}
39+
40+
public DocTypeValidationResult withMainException( Exception e ) {
41+
this.withValidationMessage( e.getMessage() );
42+
this.getValidationExceptions().add( e );
43+
return this;
44+
}
45+
1846
}

fj-doc-val-core/src/main/java/org/fugerit/java/doc/val/core/basic/AbstractDocTypeValidator.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@ public static Set<String> createSet( String... s ) {
3030
}
3131

3232
protected DocTypeValidationResult validationHelper( UnsafeVoid<Exception> fun ) {
33+
final DocTypeValidationResult resultFail = DocTypeValidationResult.newFail();
3334
return ObjectUtils.objectWithDefault( SafeFunction.get( () -> {
3435
fun.apply();
3536
return DocTypeValidationResult.newOk();
36-
}, e -> log.warn( "validation failed {}, {}", this.getMimeType(), e.toString() ) ), DocTypeValidationResult.newFail() );
37+
}, e -> {
38+
log.warn( "validation failed {}, {}", this.getMimeType(), e.toString() );
39+
resultFail.withMainException( e );
40+
} ), resultFail );
3741
}
3842

3943
protected AbstractDocTypeValidator(String mimeType, String extension) {
@@ -67,7 +71,7 @@ public boolean check(InputStream is) throws IOException {
6771

6872
@Override
6973
public boolean checkCompatibility() {
70-
return true;
74+
return Boolean.TRUE;
7175
}
7276

7377
}

fj-doc-val-core/src/main/java/org/fugerit/java/doc/val/core/basic/ImageValidator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public static boolean javaVersionSupportHelper( int javaMajorVersionFound, int j
5353

5454
@Override
5555
public DocTypeValidationResult validate(InputStream is) {
56+
DocTypeValidationResult resultFail = DocTypeValidationResult.newFail()
57+
.withValidationMessage( String.format( "No image reader found for format :%s", this.format ) );
5658
try ( ImageInputStream iis = ImageIO.createImageInputStream( is ) ) {
5759
Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName( this.format );
5860
if (readers.hasNext()) {
@@ -63,8 +65,9 @@ public DocTypeValidationResult validate(InputStream is) {
6365
}
6466
} catch (Exception exp) {
6567
log.debug( "checkImage (v2) {}", exp.getMessage() );
68+
resultFail.withMainException( exp );
6669
}
67-
return DocTypeValidationResult.newFail();
70+
return resultFail;
6871
}
6972

7073
protected ImageValidator(String mimeType, Set<String> supportedExtensions, String format, int javaMajorVersionRequired) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package test.org.fugerit.java.doc.core.val;
2+
3+
import org.fugerit.java.doc.val.core.DocTypeValidationResult;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.Test;
6+
7+
class TestDocTypeValidationResult {
8+
9+
@Test
10+
void getValidationMessage() {
11+
String testMessage = "test message";
12+
DocTypeValidationResult result =DocTypeValidationResult.newFail().withValidationMessage( testMessage );
13+
Assertions.assertEquals( testMessage, result.getValidationMessage() );
14+
}
15+
16+
}

fj-doc-val-pdfbox/src/main/java/org/fugerit/java/doc/val/pdf/box/PdfboxStrictValidator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public DocTypeValidationResult validate(InputStream is) {
2525
result = DocTypeValidationResult.newOk();
2626
} catch (Exception e) {
2727
log.warn( "Failed check on pdf : {}", e.toString() );
28+
result.withMainException( e );
2829
}
2930
return result;
3031
}

fj-doc-val-pdfbox/src/main/java/org/fugerit/java/doc/val/pdf/box/PdfboxValidator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public DocTypeValidationResult validate(InputStream is) {
2929
result = DocTypeValidationResult.newOk();
3030
} catch (Exception e) {
3131
log.warn( "Failed check on pdf : {}", e.toString() );
32+
result.withMainException( e );
3233
}
3334
return result;
3435
}

fj-doc-val-pdfbox/src/test/java/test/org/fugerit/java/doc/pdfbox/val/TestDocValidatorFacade.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import org.fugerit.java.core.function.SafeFunction;
66
import org.fugerit.java.core.lang.helpers.ClassHelper;
7+
import org.fugerit.java.doc.val.core.DocTypeValidationResult;
78
import org.fugerit.java.doc.val.core.DocValidatorFacade;
89
import org.junit.jupiter.api.Assertions;
910
import org.slf4j.Logger;
@@ -20,7 +21,9 @@ protected boolean worker( DocValidatorFacade facade, String fileName, boolean r
2021
String path = BASE_PATH+"/"+fileName;
2122
logger.info( "test path {}, expected result {}", path, result );
2223
try ( InputStream is = ClassHelper.loadFromDefaultClassLoader( path ) ) {
23-
boolean check = facade.check(fileName, is);
24+
DocTypeValidationResult validationResult = facade.validate(fileName, is);
25+
logger.info( "validation message : {}", validationResult.getValidationMessage() );
26+
boolean check = validationResult.isResultOk();
2427
Assertions.assertEquals( result, check );
2528
return ( result == check );
2629
}

fj-doc-val-poi/src/main/java/org/fugerit/java/doc/val/poi/DocValidator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public DocTypeValidationResult validate(InputStream is) {
3030
result = DocTypeValidationResult.newOk();
3131
} catch (Exception e) {
3232
logger.warn( "Failed check on pdf : {}", e.toString() );
33+
result.withMainException( e );
3334
}
3435
return result;
3536
}

fj-doc-val-poi/src/main/java/org/fugerit/java/doc/val/poi/DocxValidator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public DocTypeValidationResult validate(InputStream is) {
3030
result = DocTypeValidationResult.newOk();
3131
} catch (Exception e) {
3232
logger.warn("Failed check on pdf : {}", e.toString());
33+
result.withMainException( e );
3334
}
3435
return result;
3536
}

0 commit comments

Comments
 (0)