Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit f64aecf

Browse files
committed
#113 The existing URI is now excluded during TDE validation
1 parent b6da76f commit f64aecf

File tree

6 files changed

+102
-6
lines changed

6 files changed

+102
-6
lines changed

src/main/java/com/marklogic/client/ext/schemasloader/impl/TdeDocumentFileProcessor.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ protected void validateTdeTemplate(DocumentFile documentFile) {
7878
try {
7979
ServerEvaluationCall call = null;
8080
if (Format.XML.equals(documentFile.getFormat())) {
81-
call = buildXqueryCall(fileContent);
81+
call = buildXqueryCall(documentFile, fileContent);
8282
} else if (Format.JSON.equals(documentFile.getFormat())) {
83-
call = buildJavascriptCall(fileContent);
83+
call = buildJavascriptCall(documentFile, fileContent);
8484
} else {
8585
logger.info("Unrecognized file format, will not try to validate TDE template in file: " + file + "; format: " + documentFile.getFormat());
8686
}
@@ -100,16 +100,22 @@ protected void validateTdeTemplate(DocumentFile documentFile) {
100100
}
101101
}
102102

103-
protected ServerEvaluationCall buildJavascriptCall(String fileContent) {
103+
protected ServerEvaluationCall buildJavascriptCall(DocumentFile documentFile, String fileContent) {
104104
StringBuilder script = new StringBuilder("xdmp.invokeFunction(function() {var tde = require('/MarkLogic/tde.xqy');");
105-
script.append(format("\nreturn tde.validate([xdmp.toJSON(%s)])}, {database: xdmp.database('%s')})", fileContent, tdeValidationDatabase));
105+
script.append(format(
106+
"\nreturn tde.validate([xdmp.toJSON(%s)], ['%s'])}, {database: xdmp.database('%s')})",
107+
fileContent, documentFile.getUri(), tdeValidationDatabase
108+
));
106109
return databaseClient.newServerEval().javascript(script.toString());
107110
}
108111

109-
protected ServerEvaluationCall buildXqueryCall(String fileContent) {
112+
protected ServerEvaluationCall buildXqueryCall(DocumentFile documentFile, String fileContent) {
110113
StringBuilder script = new StringBuilder("import module namespace tde = 'http://marklogic.com/xdmp/tde' at '/MarkLogic/tde.xqy'; ");
111114
script.append(format("\nxdmp:invoke-function(function() { \nlet $t := %s ", fileContent));
112-
script.append(format("\nreturn tde:validate($t)}, <options xmlns='xdmp:eval'><database>{xdmp:database('%s')}</database></options>)", tdeValidationDatabase));
115+
script.append(format(
116+
"\nreturn tde:validate($t, '%s')}, <options xmlns='xdmp:eval'><database>{xdmp:database('%s')}</database></options>)",
117+
documentFile.getUri(), tdeValidationDatabase
118+
));
113119
return databaseClient.newServerEval().xquery(script.toString());
114120
}
115121

src/test/java/com/marklogic/client/ext/schemasloader/impl/ValidateTdeTemplatesTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ public void badJsonFile() {
3434
}
3535
}
3636

37+
@Test
38+
public void goodTemplateThatsThenUpdatedViaJavascript() {
39+
List<DocumentFile> files = loader.loadSchemas(Paths.get("src", "test", "resources", "good-schemas", "originals").toString());
40+
assertEquals(2, files.size());
41+
42+
files = loader.loadSchemas(Paths.get("src", "test", "resources", "good-schemas", "updated").toString());
43+
assertEquals("Verifying that the updated schemas were still loaded correctly, which depends on telling the " +
44+
"TDE validation function to exclude the schema that's currently loaded; otherwise a TDE-INCONSISTENTVIEW " +
45+
"error will be thrown", 2, files.size());
46+
}
47+
48+
3749
@Test
3850
public void badXmlFile() {
3951
try {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"template": {
3+
"context": "/example",
4+
"description": "This schema is valid",
5+
"collections": [
6+
"example"
7+
],
8+
"rows": [
9+
{
10+
"schemaName": "Example",
11+
"viewName": "default",
12+
"columns": [
13+
{
14+
"name": "Id",
15+
"scalarType": "string",
16+
"val": "Id"
17+
}
18+
]
19+
}
20+
]
21+
}
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<template xmlns="http://marklogic.com/xdmp/tde">
2+
<description>Sample description</description>
3+
<context>/Citation/Article</context>
4+
<rows>
5+
<row>
6+
<schema-name>Medical2</schema-name>
7+
<view-name>Publications</view-name>
8+
<columns>
9+
<column>
10+
<name>ID</name>
11+
<scalar-type>long</scalar-type>
12+
<val>../ID</val>
13+
</column>
14+
</columns>
15+
</row>
16+
</rows>
17+
</template>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"template": {
3+
"context": "/example",
4+
"description": "This schema is valid",
5+
"collections": [
6+
"example"
7+
],
8+
"rows": [
9+
{
10+
"schemaName": "Example",
11+
"viewName": "default",
12+
"columns": [
13+
{
14+
"name": "IdUpdated",
15+
"scalarType": "string",
16+
"val": "IdUpdated"
17+
}
18+
]
19+
}
20+
]
21+
}
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<template xmlns="http://marklogic.com/xdmp/tde">
2+
<description>Sample description</description>
3+
<context>/Citation/Article</context>
4+
<rows>
5+
<row>
6+
<schema-name>Medical2</schema-name>
7+
<view-name>Publications</view-name>
8+
<columns>
9+
<column>
10+
<name>UpdatedID</name>
11+
<scalar-type>long</scalar-type>
12+
<val>../UpdatedID</val>
13+
</column>
14+
</columns>
15+
</row>
16+
</rows>
17+
</template>

0 commit comments

Comments
 (0)