Skip to content

Commit dc84de7

Browse files
Ajit GeorgeAjit George
authored andcommitted
Added the JS module for map function.
1 parent 6e3681e commit dc84de7

File tree

2 files changed

+113
-36
lines changed

2 files changed

+113
-36
lines changed

test-complete/src/test/java/com/marklogic/client/functionaltest/TestOpticOnLiterals.java

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,8 +1140,7 @@ public void testInvalidDefinitions() throws KeyManagementException, NoSuchAlgori
11401140
}
11411141
// Should have SQL-NOCOLUMN exceptions.
11421142
assertTrue("Exceptions not found", str.toString().contains("SQL-NOCOLUMN"));
1143-
assertTrue("Exceptions not found", str.toString().contains("Column not found: invalid_view.colorId"));
1144-
1143+
assertTrue("Exceptions not found", str.toString().contains("Column not found: invalid_view.colorId"));
11451144
}
11461145

11471146
/*
@@ -1217,8 +1216,9 @@ public void testInvalidQualifier() throws KeyManagementException, NoSuchAlgorith
12171216

12181217
/*
12191218
* Test Map function
1219+
* TODO Refer to Eric's test and make changes.
12201220
*/
1221-
@Test
1221+
@Ignore
12221222
public void testMapFunction() throws KeyManagementException, NoSuchAlgorithmException, IOException, SAXException, ParserConfigurationException
12231223
{
12241224
System.out.println("In testMapFunction method");
@@ -1287,29 +1287,8 @@ public void testMapFunction() throws KeyManagementException, NoSuchAlgorithmExce
12871287
// plans from literals
12881288
ModifyPlan plan1 = p.fromLiterals(literalsM1);
12891289
ModifyPlan plan2 = p.fromLiterals(literalsM2);
1290-
/*StringBuilder str = new StringBuilder();
1291-
str.append("function colorIdMapper(result) {");
1292-
str.append("switch(result.myColorId) {");
1293-
str.append("case 1:");
1294-
str.append("result.myColorId = 'RED';");
1295-
str.append(" break;");
1296-
str.append("case 2:");
1297-
str.append("result.myColorId = 'BLUE';");
1298-
str.append("break;");
1299-
str.append("case 3:");
1300-
str.append("result.myColorId = 'YELLOW';");
1301-
str.append("break;");
1302-
str.append("case 4:");
1303-
str.append("result.myColorId = 'BLACK';");
1304-
str.append("break;");
1305-
str.append("default:");
1306-
str.append("result.myColorId = 'NO COLOR';");
1307-
str.append("}");
1308-
str.append("return result;");
1309-
str.append("};");*/
1310-
1311-
1312-
ModifyPlan output = plan1.joinInner( plan2, p.on(p.col("colorId_shape"), p.col("colorId")))
1290+
1291+
ModifyPlan output = plan1.joinInner(plan2, p.on(p.col("colorId_shape"), p.col("colorId")))
13131292
.select(
13141293
"rowId",
13151294
"desc",
@@ -1318,22 +1297,25 @@ public void testMapFunction() throws KeyManagementException, NoSuchAlgorithmExce
13181297
)
13191298
.orderBy(p.asc("rowId"));
13201299

1321-
PlanFunction pf = output.installedFunction("/marklogic.rest.resource/OpticsJSResourceModule/assets/resource.sjs", "colorIdMapper");
1300+
PlanFunction pf = output.installedFunction("/marklogic.rest.resource/OpticsJSResourceModule/assets/resource.sjs", "colorIdMapper");
1301+
13221302
output.map(pf);
13231303

1324-
JacksonHandle jacksonHandle = new JacksonHandle();
1325-
jacksonHandle.setMimetype("application/json");
1304+
JacksonHandle jacksonHandle = new JacksonHandle();
1305+
jacksonHandle.setMimetype("application/json");
13261306

1327-
rowMgr.resultDoc(output, jacksonHandle);
1328-
JsonNode jsonBindingsNodes = jacksonHandle.get().path("rows");
1307+
rowMgr.resultDoc(output, jacksonHandle);
1308+
JsonNode jsonBindingsNodes = jacksonHandle.get().path("rows");
13291309

1330-
// Should have 1 node returned.
1331-
assertEquals("One node not returned from testIntersect method", 4, jsonBindingsNodes.size());
1332-
1333-
1334-
1310+
// Should have 4 node returned.
1311+
assertEquals("Four nodes not returned from testMapFunction method", 4, jsonBindingsNodes.size());
1312+
assertEquals("Row 1 rowId value incorrect", "1", jsonBindingsNodes.path(0).path("rowId").path("value").asText());
1313+
assertEquals("Row 1 descAgg value incorrect", "red", jsonBindingsNodes.path(0).path("colorDesc").path("value").asText());
1314+
assertEquals("Row 1 rowId value incorrect", "2", jsonBindingsNodes.path(1).path("rowId").path("value").asText());
1315+
assertEquals("Row 1 descAgg value incorrect", "blue", jsonBindingsNodes.path(1).path("colorDesc").path("value").asText());
13351316
}
13361317

1318+
13371319
@AfterClass
13381320
public static void tearDownAfterClass() throws Exception
13391321
{
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2014-2015 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// module that helps tests in TestOpticOnLiterals.class for map functionality
18+
function get(context, params) {
19+
context.outputTypes = ["application/json"];
20+
var arg1 = params.arg1;
21+
var arg2 = params.arg2;
22+
var x = arg1.toString();
23+
24+
25+
return {
26+
"argument1": x,
27+
"argument2": arg2,
28+
"database-name":xdmp.databaseName(xdmp.database()),
29+
"document-count":fn.count(fn.doc()),
30+
"content": "This is a JSON document",
31+
"document-content": fn.doc(),
32+
"response": xdmp.getResponseCode(),
33+
"outputTypes": context.outputTypes,
34+
35+
}
36+
};
37+
function post(context, params, input) {
38+
39+
var argUrl = params.uri;
40+
41+
xdmp.eval(" \
42+
declareUpdate(); \
43+
var argUrl; \
44+
var sibling=cts.doc(argUrl).root.content; \
45+
var inputObject; \
46+
var newNode = new NodeBuilder(); \
47+
newNode.addNode(inputObject); \
48+
var named = newNode.toNode().xpath('.//array-node()'); xdmp.nodeInsertAfter(sibling,named);\
49+
",{"inputObject":input,"argUrl":argUrl},{"isolation":"different-transaction"});
50+
return ({"response": xdmp.getResponseCode()})
51+
52+
};
53+
54+
// Function responding to PUT method - must use local name 'put'.
55+
function put(context, params, input) {
56+
var argUrl = params.uri;
57+
var inputObject = input;
58+
//xdmp.documentInsert(argUrl,input);
59+
xdmp.eval("declareUpdate(); var argUrl; var input;xdmp.documentInsert(argUrl,input)",{"argUrl":argUrl,"input":inputObject},{"isolation":"different-transaction"});
60+
var count = xdmp.eval("fn.count(fn.doc())");
61+
xdmp.log(count);
62+
return ({"response": xdmp.getResponseCode()})
63+
};
64+
65+
// Function responding to DELETE method - must use local name 'delete'.
66+
function deleteFunction(context, params) {
67+
var docuri = params.uri;
68+
xdmp.documentDelete(docuri);
69+
return({"response": xdmp.getResponseCode()})
70+
};
71+
72+
function colorIdMapper() {
73+
console.log("Inside the func ==== ");
74+
switch(colorId) {
75+
case 1:
76+
colorDesc = 'RED';
77+
break;
78+
case 2:
79+
colorDesc = 'BLUE';
80+
break;
81+
case 3:
82+
colorDesc = 'YELLOW';
83+
break;
84+
case 4:
85+
colorDesc = 'BLACK';
86+
break;
87+
default:
88+
colorDesc = 'NO COLOR';
89+
}
90+
return colorDesc.;
91+
};
92+
exports.GET = colorIdMapper;
93+
exports.POST = post;
94+
exports.PUT = put;
95+
exports.DELETE = deleteFunction;

0 commit comments

Comments
 (0)