Skip to content

Commit 9ea0d5a

Browse files
committed
chore: test configuration precedence
1 parent 299519b commit 9ea0d5a

File tree

3 files changed

+102
-13
lines changed

3 files changed

+102
-13
lines changed

fj-doc-base/src/main/java/org/fugerit/java/doc/base/feature/tableintegritycheck/TableIntegrityCheck.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,22 @@
1111
import java.util.HashMap;
1212
import java.util.Map;
1313
import java.util.function.Consumer;
14+
import java.util.function.Function;
1415

1516
@Slf4j
1617
public class TableIntegrityCheck {
1718

1819
private TableIntegrityCheck() {}
1920

20-
private static final Map<String, Consumer<DocTable>> DOC_CONSUMER_MAP = new HashMap<>();
21+
private static final Map<String, Function<DocTable, Integer>> DOC_FUNCTION_MAP = new HashMap<>();
2122
static {
22-
DOC_CONSUMER_MAP.put(TableIntegrityCheckConstants.TABLE_INTEGRITY_CHECK_DISABLED, doc -> log.debug("Table Integrity Check disabled") );
23-
DOC_CONSUMER_MAP.put(TableIntegrityCheckConstants.TABLE_INTEGRITY_CHECK_WARN, doc -> checkWorker( doc, result -> {} ) );
24-
DOC_CONSUMER_MAP.put(TableIntegrityCheckConstants.TABLE_INTEGRITY_CHECK_FAIL, doc -> checkWorker( doc, result -> {
23+
DOC_FUNCTION_MAP.put(TableIntegrityCheckConstants.TABLE_INTEGRITY_CHECK_DISABLED, doc -> Result.RESULT_CODE_OK );
24+
DOC_FUNCTION_MAP.put(TableIntegrityCheckConstants.TABLE_INTEGRITY_CHECK_WARN, doc -> checkWorker( doc, result -> {}).getResultCode() );
25+
DOC_FUNCTION_MAP.put(TableIntegrityCheckConstants.TABLE_INTEGRITY_CHECK_FAIL, doc -> checkWorker( doc, result -> {
2526
if ( result.getResultCode() != Result.RESULT_CODE_KO ) {
2627
throw new DocFeatureRuntimeException( "Table check integrity failed, see logs for details.", result.getResultCode(), result.getMessages() );
2728
}
28-
} ) );
29+
} ).getResultCode() );
2930
}
3031

3132
private static int processCells( DocRow docRow, final int currentColParam, Map<Integer,Integer> rowSpanTracker ) {
@@ -89,12 +90,12 @@ private static TableIntegrityCheckResult checkWorker(DocTable docTable, Consumer
8990
return result;
9091
}
9192

92-
public static void apply(DocBase docBase, DocTable docTable, FeatureConfig featureConfig) {
93+
public static int apply(DocBase docBase, DocTable docTable, FeatureConfig featureConfig) {
9394
String tableCheckIntegrityInfo = StringUtils.valueWithDefault(
9495
docBase.getStableInfoSafe().getProperty( GenericConsts.DOC_TABLE_CHECK_INTEGRITY ),
9596
featureConfig.getTableCheckIntegrity() );
96-
Consumer<DocTable> consumer = DOC_CONSUMER_MAP.get( tableCheckIntegrityInfo );
97-
consumer.accept( docTable );
97+
Function<DocTable, Integer> fun = DOC_FUNCTION_MAP.get( tableCheckIntegrityInfo );
98+
return fun.apply( docTable );
9899
}
99100

100101
}

fj-doc-base/src/test/java/test/org/fugerit/java/doc/base/feature/TableCheckIntegrityTest.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33
import lombok.extern.slf4j.Slf4j;
44
import org.fugerit.java.core.cfg.ConfigRuntimeException;
55
import org.fugerit.java.core.db.daogen.BasicDaoResult;
6+
import org.fugerit.java.core.lang.ex.CodeRuntimeException;
67
import org.fugerit.java.core.lang.helpers.ClassHelper;
8+
import org.fugerit.java.core.util.result.Result;
79
import org.fugerit.java.doc.base.facade.DocFacadeSource;
810
import org.fugerit.java.doc.base.feature.DocFeatureRuntimeException;
911
import org.fugerit.java.doc.base.feature.FeatureConfig;
12+
import org.fugerit.java.doc.base.feature.tableintegritycheck.TableIntegrityCheck;
13+
import org.fugerit.java.doc.base.feature.tableintegritycheck.TableIntegrityCheckConstants;
1014
import org.fugerit.java.doc.base.model.DocBase;
15+
import org.fugerit.java.doc.base.model.DocTable;
16+
import org.fugerit.java.doc.base.typehelper.generic.GenericConsts;
1117
import org.junit.jupiter.api.Assertions;
1218
import org.junit.jupiter.api.Test;
1319

@@ -22,25 +28,47 @@
2228
@Slf4j
2329
class TableCheckIntegrityTest {
2430

25-
private int testWorker( String xmlPath ) {
26-
String fullCLPath = String.format( "feature-info/table-check-integrity/%s.xml", xmlPath );
27-
log.info( "xmlPath: {}, fullPath: {}", xmlPath, fullCLPath );
31+
private DocBase readDocBase( String fullCLPath ) {
2832
try (Reader reader = new InputStreamReader( ClassHelper.loadFromDefaultClassLoader( fullCLPath ) ) ) {
2933
DocFacadeSource docFacadeSource = DocFacadeSource.getInstance();
3034
DocBase docBase = docFacadeSource.parseRE( reader, DocFacadeSource.SOURCE_TYPE_XML, FeatureConfig.DEFAULT );
31-
log.info( "docBase:{}", docBase );
32-
return BasicDaoResult.RESULT_CODE_OK;
35+
return docBase;
3336
} catch (IOException e) {
3437
throw ConfigRuntimeException.convertEx( e );
3538
}
3639
}
3740

41+
private int testWorker( String xmlPath ) {
42+
String fullCLPath = String.format( "feature-info/table-check-integrity/%s.xml", xmlPath );
43+
log.info( "xmlPath: {}, fullPath: {}", xmlPath, fullCLPath );
44+
DocBase docBase = readDocBase( fullCLPath );
45+
log.info( "docBase:{}", docBase );
46+
return BasicDaoResult.RESULT_CODE_OK;
47+
}
48+
3849
@Test
3950
void testOk() {
4051
Assertions.assertEquals( BasicDaoResult.RESULT_CODE_OK, this.testWorker( "colspan-rowspan-sample-ok" ) );
4152
Assertions.assertTrue( Boolean.TRUE );
4253
}
4354

55+
@Test
56+
void testConfiguration() {
57+
FeatureConfig featureConfig = new FeatureConfig() {
58+
@Override
59+
public String getTableCheckIntegrity() {
60+
return TableIntegrityCheckConstants.TABLE_INTEGRITY_CHECK_FAIL;
61+
}
62+
};
63+
DocBase docBase = readDocBase( "feature-info/table-check-integrity/colspan-rowspan-sample-ko-noconfig.xml" );
64+
DocTable docTable = (DocTable) docBase.getDocBody().getElementList().stream().filter( e -> e instanceof DocTable ).findFirst().get();
65+
// global configuration set to fail
66+
Assertions.assertThrows( DocFeatureRuntimeException.class, () -> TableIntegrityCheck.apply( docBase, docTable, featureConfig ) );
67+
// override configuration for this document, will not fail but result != OK
68+
docBase.getStableInfo().setProperty(GenericConsts.DOC_TABLE_CHECK_INTEGRITY, TableIntegrityCheckConstants.TABLE_INTEGRITY_CHECK_WARN );
69+
Assertions.assertNotEquals(Result.RESULT_CODE_OK, TableIntegrityCheck.apply( docBase, docTable, featureConfig ) );
70+
}
71+
4472
@Test
4573
void testKoNoException() {
4674
Arrays.asList( "colspan-rowspan-sample-ko-disabled", "colspan-rowspan-sample-ko-warn" )
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<doc
3+
xmlns="http://javacoredoc.fugerit.org"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://javacoredoc.fugerit.org https://www.fugerit.org/data/java/doc/xsd/doc-2-1.xsd" >
6+
7+
<metadata>
8+
<!-- Margin for document : left;right;top;bottom -->
9+
<info name="margins">10;10;10;30</info>
10+
<!-- documenta meta information -->
11+
<info name="doc-title">colspan / rowspan sample</info>
12+
<info name="doc-subject">fj doc venus sample source FreeMarker Template XML</info>
13+
<info name="doc-author">fugerit79</info>
14+
<info name="doc-language">en</info>
15+
<!-- font must be loaded -->
16+
<info name="default-font-name">TitilliumWeb</info>
17+
<!-- property specific for xls/xlsx -->
18+
<info name="excel-table-id">data-table=print</info>
19+
<!-- property specific for csv -->
20+
<info name="csv-table-id">data-table</info>
21+
<footer-ext>
22+
<para align="right">${currentPage} / ${pageCount}</para>
23+
</footer-ext>
24+
</metadata>
25+
<body>
26+
<para>colspan / rowspan sample</para>
27+
<table columns="3" colwidths="30;30;40" width="100" id="data-table" padding="2">
28+
<row header="true">
29+
<cell rowspan="2" align="center"><para>Title</para></cell>
30+
<cell colspan="2" align="center"><para>Data</para></cell>
31+
</row>
32+
<row header="true">
33+
<cell align="center"><para>WRONG CELL!</para></cell>
34+
<cell align="center"><para>Name</para></cell>
35+
<cell align="center"><para>Surname</para></cell>
36+
</row>
37+
<row>
38+
<cell><para>Queen</para></cell>
39+
<cell><para>Luthien</para></cell>
40+
<cell><para>Tinuviel</para></cell>
41+
</row>
42+
<row>
43+
<cell><para>King</para></cell>
44+
<cell><para>Thorin</para></cell>
45+
<cell><para>Oakshield</para></cell>
46+
</row>
47+
<row>
48+
<cell><para>Ring bearer</para></cell>
49+
<cell><para>Bilbo</para></cell>
50+
<cell><para>Bagging</para></cell>
51+
</row>
52+
<row>
53+
<cell><para>Eldest</para></cell>
54+
<cell><para>Tom</para></cell>
55+
<cell><para>Bombadil</para></cell>
56+
</row>
57+
</table>
58+
</body>
59+
60+
</doc>

0 commit comments

Comments
 (0)