@Override public Boolean executeAsk(SPARQLQueryDefinition qdef) { JsonNode result = executeQueryImpl(qdef, new JacksonHandle(), null, false).get(); return result.get("boolean").asBoolean(); }
@Override protected void receiveContent(InputStream content) { if (content == null) return; try { set( getMapper().readValue( new InputStreamReader(content, "UTF-8"), JsonNode.class )); } catch (JsonParseException e) { throw new MarkLogicIOException(e); } catch (JsonMappingException e) { throw new MarkLogicIOException(e); } catch (IOException e) { throw new MarkLogicIOException(e); } finally { try { content.close(); } catch (IOException e) { // ignore. } } } @Override
@Test public void testFromSqlGroupBy() throws KeyManagementException, NoSuchAlgorithmException, IOException, SAXException, ParserConfigurationException { System.out.println("In testFromSqlGroupBy method"); RowManager rowMgr = client.newRowManager(); PlanBuilder p = rowMgr.newPlanBuilder(); ModifyPlan plan1 = p.fromSql("SELECT opticFunctionalTest.master.name, opticFunctionalTest.detail.name AS DetailName, opticFunctionalTest.detail.color, SUM(amount) AS DetailSum " + " FROM opticFunctionalTest.detail " + " INNER JOIN opticFunctionalTest.master ON opticFunctionalTest.master.id = opticFunctionalTest.detail.masterId " + " GROUP BY opticFunctionalTest.master.name " + " ORDER BY DetailSum DESC"); JacksonHandle jacksonHandle = new JacksonHandle(); jacksonHandle.setMimetype("application/json"); rowMgr.resultDoc(plan1, jacksonHandle); JsonNode jsonResults = jacksonHandle.get(); jsonResults = jacksonHandle.get().path("rows"); // Should have 2 nodes returned. assertEquals("Two rows not returned from testFromSqlGroupBy method ", 2, jsonResults.size()); assertEquals("Row 1 opticFunctionalTest.master.name value incorrect", "Master 2", jsonResults.path(0).path("opticFunctionalTest.master.name").path("value").asText()); assertEquals("Row 1 DetailSum value incorrect", "120.12", jsonResults.path(0).path("DetailSum").path("value").asText()); assertEquals("Row 2 opticFunctionalTest.master.name value incorrect", "Master 1", jsonResults.path(1).path("opticFunctionalTest.master.name").path("value").asText()); assertEquals("Row 2 DetailSum value incorrect", "90.09", jsonResults.path(1).path("DetailSum").path("value").asText()); }
@Test public void test2GetAllResourceServicesMultipleTimes() throws KeyManagementException, NoSuchAlgorithmException, Exception { JacksonHandle jh = new JacksonHandle(); JacksonHandle jh2 = new JacksonHandle(); jh.set(jh2.getMapper().readTree(tjs.getJSON("helloJS0.json"))); assertEquals("Total documents loaded are", 150, jh.get().get("document-count").intValue()); jh.set(jh2.getMapper().readTree(tjs.getJSON("helloJS" + j + ".json"))); JSONAssert.assertEquals(expAftrPut, jh.get().get("document-content").findParent("array").toString(), false); JSONAssert.assertEquals(expectedResponse, tjs.deleteJSON("helloJS" + j + ".json"), false);
GenericDocumentManager docMgr = client.newDocumentManager(); QueryManager queryMgr = client.newQueryManager(); JacksonHandle jh = new JacksonHandle(); ObjectMapper mapper = new ObjectMapper(); ObjectNode mainNode = mapper.createObjectNode(); mainNode.set("query", queryArrayNode); jh.set(mainNode); RawStructuredQueryDefinition qd = queryMgr.newRawStructuredQueryDefinition(jh); System.out.println(jh.get().toString()); docMgr.setNonDocumentFormat(Format.JSON); JacksonHandle results = new JacksonHandle(); DocumentPage page = docMgr.search(qd, 1, results); DocumentMetadataHandle mh = new DocumentMetadataHandle(); System.out.println(results.get().toString()); assertEquals("Total search results before transaction rollback are ", "204", results.get().get("total").asText());
static public JacksonHandle JsonNodeToHandle(JsonNode value) { return (value == null) ? null : new JacksonHandle(value); } static public Stream<JacksonHandle> JsonNodeToHandle(Stream<? extends JsonNode> values) {
JacksonHandle jacksonHandle1 = new JacksonHandle(); JacksonHandle jacksonHandle2 = new JacksonHandle(); JacksonHandle jacksonHandle3 = new JacksonHandle(); jacksonHandle1.set(originalNode); jacksonHandle1.withFormat(Format.JSON); jacksonHandle2.set(updatedNode); jacksonHandle2.withFormat(Format.JSON); jacksonHandle3.set(constraintNode); jacksonHandle3.withFormat(Format.JSON);
JacksonHandle jh = new JacksonHandle(); ObjectMapper objM = new ObjectMapper(); JsonNode jn = objM.readTree(new String("{\"animal\":\"dog\", \"says\":\"woof\"}")); jh.set(jn); jh.setFormat(Format.JSON);
@Test public void test105Searches() throws IOException { QueryManager queryMgr = Common.client.newQueryManager(); StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); for (int i=0;i<105;i++) { for (QueryDefinition t : new QueryDefinition[] { qb.term("leaf3"), qb.build(qb.value(qb.element("leaf"), "leaf3")) }) { JacksonHandle results = queryMgr.search(t, new JacksonHandle()); assertNotNull(results); JsonNode jsonResults =results.get(); @SuppressWarnings("unused") String resultString = results.getMapper().writeValueAsString(jsonResults); // ignore. } } }
public void loadJSONDocuments() throws KeyManagementException, NoSuchAlgorithmException, JsonProcessingException, IOException { int count = 1; JSONDocumentManager docMgr = client.newJSONDocumentManager(); DocumentWriteSet writeset = docMgr.newWriteSet(); Map<String, String> map = new HashMap<>(); for (int i = 0; i < 102; i++) { JsonNode jn = new ObjectMapper().readTree("{\"animal\":\"dog " + i + "\", \"says\":\"woof\"}"); JacksonHandle jh = new JacksonHandle(); jh.set(jn); writeset.add(DIRECTORY + "dog" + i + ".json", jh); map.put(DIRECTORY + "dog" + i + ".json", jn.toString()); if (count % BATCH_SIZE == 0) { docMgr.write(writeset); writeset = docMgr.newWriteSet(); } count++; // System.out.println(jn.toString()); } if (count % BATCH_SIZE > 0) { docMgr.write(writeset); } }
@Test public void testselectAmbiguousColumns() throws KeyManagementException, NoSuchAlgorithmException, IOException, SAXException, ParserConfigurationException { System.out.println("In testselectAmbiguousColumns method"); StringBuilder str = new StringBuilder(); try { RowManager rowMgr = client.newRowManager(); PlanBuilder p = rowMgr.newPlanBuilder(); ModifyPlan plan1 = p.fromView("opticFunctionalTest", "detail"); ModifyPlan plan2 = p.fromView("opticFunctionalTest", "master"); ModifyPlan plan3 = plan1.joinInner(plan2) .where( p.eq( p.schemaCol("opticFunctionalTest", "master", "id"), p.col( "masterId") ) ) .orderBy(p.asc(p.col( "id"))) .select(p.colSeq("id")); JacksonHandle jacksonHandle = new JacksonHandle(); jacksonHandle.setMimetype("application/json"); rowMgr.resultDoc(plan3, jacksonHandle); } catch (Exception ex) { str.append(ex.getMessage()); System.out.println("Exception message is " + str.toString()); } // Should have SQL-AMBCOLUMN exceptions. assertTrue("Exceptions not found", str.toString().contains("SQL-AMBCOLUMN")); assertTrue("Exceptions not found", str.toString().contains("Ambiguous column reference: found opticFunctionalTest.master.id and opticFunctionalTest.detail.id")); }
@Test public void test1GetAllResourceServices() throws KeyManagementException, NoSuchAlgorithmException, Exception { JacksonHandle jh = new JacksonHandle(); resourceMgr.listServices(jh); assertEquals("Format on Handle", "JSON", jh.getFormat().name()); assertEquals("Mime Type on Handle", "application/json", jh.getMimetype()); String expectedList = "{\"resources\":{\"resource\":[{\"name\":\"simpleJSResourceModule\", \"source-format\":\"javascript\", \"description\":\"Testing resource extension for java script\", \"version\":\"1.0\", \"title\":\"BasicJSTest\", \"methods\":{\"method\":[{\"method-name\":\"get\", \"parameter\":[{\"parameter-name\":\"my-uri\", \"parameter-type\":\"xs:string?\"}]}, {\"method-name\":\"post\"}, {\"method-name\":\"put\"}, {\"method-name\":\"delete\"}]}, \"resource-source\":\"/v1/resources/simpleJSResourceModule\"}]}}"; JSONAssert.assertEquals(expectedList, jh.get().toString(), false); TestJSExtension tjs = new TestJSExtension(client); String expectedResponse = "{\"response\":[200, \"OK\"]}"; JSONAssert.assertEquals(expectedResponse, tjs.putJSON("helloJS.json"), false); String expAftrPut = "{\"argument1\":\"helloJS.json\", \"argument2\":\"Earth\",\"database-name\":\"TestJSResourceExtensionDB\", \"document-count\":1, \"content\":\"This is a JSON document\", \"document-content\":{\"argument1\":\"hello\", \"argument2\":\"Earth\", \"content\":\"This is a JSON document\", \"response\":[200, \"OK\"], \"outputTypes\":\"application/json\"}, \"response\":[200, \"OK\"], \"outputTypes\":[\"application/json\"]}"; JSONAssert.assertEquals(expAftrPut, tjs.getJSON("helloJS.json"), false); JSONAssert.assertEquals(expectedResponse, tjs.postJSON("helloJS.json"), false); String expAftrPost = "{\"argument1\":\"helloJS.json\", \"argument2\":\"Earth\", \"document-count\":1, \"content\":\"This is a JSON document\", \"document-content\":{\"argument1\":\"hello\", \"argument2\":\"Earth\", \"content\":\"This is a JSON document\", \"array\":[1, 2, 3], \"response\":[200, \"OK\"], \"outputTypes\":\"application/json\"}, \"response\":[200, \"OK\"], \"outputTypes\":[\"application/json\"]}"; JSONAssert.assertEquals(expAftrPost, tjs.getJSON("helloJS.json"), false); String expected = "{\"argument1\":\"helloJS.json\", \"argument2\":\"Earth\", \"document-count\":0, \"content\":\"This is a JSON document\", \"document-content\":null, \"response\":[200, \"OK\"], \"outputTypes\":[\"application/json\"]}"; JSONAssert.assertEquals(expectedResponse, tjs.deleteJSON("helloJS.json"), false); JSONAssert.assertEquals(expected, tjs.getJSON("helloJS.json"), false); }
JacksonHandle mh = new JacksonHandle(); Map<String, String> p = new HashMap<>(); p.put("myString", "Generic JSON"); p.put("myInt", "19"); mh.with(constructJSONPropertiesMetadata(p)); JacksonHandle jh = new JacksonHandle(); ObjectMapper objM = new ObjectMapper(); JsonNode jn = objM.readTree(new String("{\"animal\":\"dog\", \"says\":\"woof\"}")); jh.set(jn); jh.setFormat(Format.JSON); writeset.add("/generic/dog.json", new JacksonHandle().with(mapper.readTree("{\"quality\": 10}")), jh); // This sh.set(ds); sh.setFormat(Format.XML); writeset.add("/generic/foo.xml", new JacksonHandle().with(constructJSONCollectionMetadata("http://Json-Uri-generic-collections/")), sh); // This
ObjectMapper mapper = new ObjectMapper(); JacksonHandle jacksonHandle1 = new JacksonHandle(); JacksonHandle jacksonHandle2 = new JacksonHandle(); JacksonHandle jacksonHandle3 = new JacksonHandle(); jacksonHandle1.set(dogNode); jacksonHandle1.withFormat(Format.JSON); jacksonHandle2.set(catNode); jacksonHandle2.withFormat(Format.JSON); jacksonHandle3.set(ratNode); jacksonHandle3.withFormat(Format.JSON); JacksonHandle jacksonhandle = new JacksonHandle(); docMgr.read(docId[0], jacksonhandle); JSONAssert.assertEquals(json1, jacksonhandle.toString(), true); JSONAssert.assertEquals(json2, jacksonhandle.toString(), true); JSONAssert.assertEquals(json3, jacksonhandle.toString(), true);
JacksonHandle defaultMetadata1 = new JacksonHandle(); Map<String, String> p = new HashMap<>(); p.put("myString", "json"); p.put("myInt", "9"); defaultMetadata1.with(constructJSONPropertiesMetadata(p)); JacksonHandle defaultMetadata2 = new JacksonHandle(); JsonNode jn = mapper.readTree("{\"quality\": 20}"); defaultMetadata2.set(jn); JacksonHandle docSpecificMetadata = new JacksonHandle(); docSpecificMetadata.set(constructJSONCollectionMetadata("http://Json-Uri-spec-collections/"));
.addVariable("myPrefix:myString", "Mars") .addVariable("myArray", new JacksonHandle().with(new ObjectMapper().createArrayNode().add("item1").add("item2"))) .addVariable("myObject", new JacksonHandle().with(new ObjectMapper().createObjectNode().put("item1", "value1"))) .addVariable("myAnyUri", "http://marklogic.com/a") .addVariable("myBinary", DatatypeConverter.printHexBinary(",".getBytes()))
private void checkJSONDocRows(RowSet<JacksonHandle> rowSet) { int rowCount = 0; for (JacksonHandle jsonRow: rowSet) { JsonNode row = jsonRow.get(); rowCount++; String value = row.findValue("rowNum").findValue("value").asText(); assertEquals("unexpected first binding value in JSON", String.valueOf(rowCount), value); value = row.findValue("uri").findValue("value").asText(); assertEquals("unexpected second binding value in JSON", uris[rowCount - 1], value); if (uris[rowCount - 1].endsWith(".json")) { value = row.findValue("doc").findValue("value").toString(); assertEquals("unexpected third binding value in JSON", docs[rowCount - 1].replace(" ", ""), value); } else { value = row.findValue("doc").findValue("value").asText(); assertEquals("unexpected third binding value in JSON", docs[rowCount - 1], value); } } assertEquals("row count for JSON document join", 3, rowCount); } private void checkRecordDocRows(RowSet<RowRecord> rowSet) throws SAXException, IOException {
/** * Assigns a JSON tree as the content and returns the handle. * @param content the JSON root node. * @return the handle on the JSON tree. */ public JacksonHandle with(JsonNode content) { set(content); return this; }
/** * Specifies the format of the content and returns the handle * as a fluent convenience. * @param format the format of the content * @return this handle */ public JacksonHandle withFormat(Format format) { setFormat(format); return this; }