From d3caec91db9e1595d2393eaeabbefa93b7a15675 Mon Sep 17 00:00:00 2001 From: MarkLogic Builder Date: Wed, 30 Jul 2014 18:31:40 -0700 Subject: [PATCH 001/357] adding new test for transforms --- .../TestBulkWriteWithTransformations.java | 169 ++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteWithTransformations.java diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteWithTransformations.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteWithTransformations.java new file mode 100644 index 000000000..3bb09be38 --- /dev/null +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteWithTransformations.java @@ -0,0 +1,169 @@ +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.File; +import java.util.HashMap; +import java.util.Scanner; + +import javax.xml.bind.JAXBContext; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.stream.StreamSource; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.admin.ExtensionMetadata; +import com.marklogic.client.admin.TransformExtensionsManager; +import com.marklogic.client.document.DocumentPage; +import com.marklogic.client.document.DocumentRecord; +import com.marklogic.client.document.DocumentWriteSet; +import com.marklogic.client.document.ServerTransform; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.FileHandle; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.InputStreamHandle; +import com.marklogic.client.io.JAXBHandle; +import com.marklogic.client.io.SourceHandle; + +public class TestBulkWriteWithTransformations extends BasicJavaClientREST{ + private static final int BATCH_SIZE=100; + private static final String DIRECTORY ="/bulkTransform/"; + private static String dbName = "TestBulkWriteWithTransformDB"; + private static String [] fNames = {"TestBulkWriteWithTransformDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client ; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + System.out.println("In setup"); +// setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); +// setupAppServicesConstraint(dbName); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + System.out.println("In tear down" ); +// tearDownJavaRESTServer(dbName, fNames, restServerName); + } + + @Before + public void setUp() throws Exception { + System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + // create new connection for each test below + client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST); + } + + @After + public void tearDown() throws Exception { + System.out.println("Running clear script"); + // release client + client.release(); + } + + @Test + public void testBulkLoadWithXSLTClientSideTransform() throws Exception { + String docId[] ={"/transform/emp.xml","/transform/food1.xml","/transform/food2.xml"}; + Source s[] = new Source[3]; + s[0] = new StreamSource("src/test/java/com/marklogic/javaclient/data/employee.xml"); + s[1] = new StreamSource("src/test/java/com/marklogic/javaclient/data/xml-original.xml"); + s[2] = new StreamSource("src/test/java/com/marklogic/javaclient/data/xml-original-test.xml"); + // get the xslt + Source xsl = new StreamSource("src/test/java/com/marklogic/javaclient/data/employee-stylesheet.xsl"); + + // create transformer + TransformerFactory factory = TransformerFactory.newInstance(); + Transformer transformer = factory.newTransformer(xsl); + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + for(int i=0;i<3;i++){ + SourceHandle handle = new SourceHandle(); + handle.set(s[i]); + // set the transformer + handle.setTransformer(transformer); + writeset.add(docId[i],handle); + } + docMgr.write(writeset); + FileHandle dh = new FileHandle(); + // DOMHandle dh = new DOMHandle(); + docMgr.read(docId[0], dh); + Scanner scanner = new Scanner(dh.get()).useDelimiter("\\Z"); + String readContent = scanner.next(); + assertTrue("xml document contains firstname", readContent.contains("firstname")); + docMgr.read(docId[1], dh); + Scanner sc1 = new Scanner(dh.get()).useDelimiter("\\Z"); + readContent = sc1.next(); + assertTrue("xml document contains firstname", readContent.contains("firstname")); + docMgr.read(docId[2], dh); + Scanner sc2 = new Scanner(dh.get()).useDelimiter("\\Z"); + readContent = sc2.next(); + assertTrue("xml document contains firstname", readContent.contains("firstname")); + + } + @Test + public void testBulkLoadWithXQueryTransform() throws Exception { + + TransformExtensionsManager transMgr = + client.newServerConfigManager().newTransformExtensionsManager(); + ExtensionMetadata metadata = new ExtensionMetadata(); + metadata.setTitle("Adding attribute xquery Transform"); + metadata.setDescription("This plugin transforms an XML document by adding attribute to root node"); + metadata.setProvider("MarkLogic"); + metadata.setVersion("0.1"); + // get the transform file + File transformFile = new File("src/test/java/com/marklogic/javaclient/transforms/add-attr-xquery-transform.xqy"); + FileHandle transformHandle = new FileHandle(transformFile); + transMgr.writeXQueryTransform("add-attr-xquery-transform", transformHandle, metadata); + ServerTransform transform = new ServerTransform("add-attr-xquery-transform"); + transform.put("trans:name", "Lang"); + transform.put("trans:value", "English"); + int count=1; + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + HashMap map= new HashMap(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + for(int i =0;i<12;i++){ + + writeset.add(DIRECTORY+"foo"+i+".xml", new DOMHandle(getDocumentContent("This is so foo"+i))); + map.put(DIRECTORY+"foo"+i+".xml", convertXMLDocumentToString(getDocumentContent("This is so foo"+i))); + if(count%10 == 0){ + docMgr.write(writeset,transform); + writeset = docMgr.newWriteSet(); + } + count++; + } + if(count%10 > 0){ + docMgr.write(writeset,transform); + } + docMgr.write("00.xml",new DOMHandle(getDocumentContent("This is so foo")),transform); + String uris[] = new String[102]; + for(int i =0;i<102;i++){ + uris[i]=DIRECTORY+"foo"+i+".xml"; + } + count=0; + DocumentPage page = docMgr.read(uris); + DOMHandle dh = new DOMHandle(); + while(page.hasNext()){ + DocumentRecord rec = page.next(); + rec.getContent(dh); + assertEquals("Comparing the content :",map.get(rec.getUri()),convertXMLDocumentToString(dh.get())); + count++; + } + + assertEquals("document count", 102,count); + + } + +} From a65b73511fa673f07fb070cad65f771784605bc2 Mon Sep 17 00:00:00 2001 From: MarkLogic Builder Date: Wed, 30 Jul 2014 18:43:40 -0700 Subject: [PATCH 002/357] adding transform for test --- .../transforms/add-attr-xquery-transform.xqy | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/transforms/add-attr-xquery-transform.xqy diff --git a/test-complete/src/test/java/com/marklogic/javaclient/transforms/add-attr-xquery-transform.xqy b/test-complete/src/test/java/com/marklogic/javaclient/transforms/add-attr-xquery-transform.xqy new file mode 100644 index 000000000..387ff516d --- /dev/null +++ b/test-complete/src/test/java/com/marklogic/javaclient/transforms/add-attr-xquery-transform.xqy @@ -0,0 +1,30 @@ +xquery version "1.0-ml"; +module namespace example = + "http://marklogic.com/rest-api/transform/add-attr-xquery-transform"; + +declare function example:transform( + $context as map:map, + $params as map:map, + $content as document-node() +) as document-node() +{ + if (fn:empty($content/*)) then $content + else ( + let $value := (map:get($params,"trans:value"),"UNDEFINED")[1] + let $name := (map:get($params, "trans:name"), "transformed")[1] + let $root := $content/* + return + ( + document { + $root/preceding-sibling::node(), + element {fn:name($root)} { + attribute { fn:QName("", $name) } {$value}, + $root/@*, + $root/node() + }, + $root/following-sibling::node() + }, + xdmp:log(fn:concat($name,"-",$value)) + ) + ) +}; \ No newline at end of file From a376704cff5e20ec84cca30bcac8122920655cc4 Mon Sep 17 00:00:00 2001 From: MarkLogic Builder Date: Fri, 1 Aug 2014 11:29:29 -0700 Subject: [PATCH 003/357] adding more scenario --- .../TestBulkWriteWithTransformations.java | 68 ++++++++++++++++--- 1 file changed, 60 insertions(+), 8 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteWithTransformations.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteWithTransformations.java index 3bb09be38..bdb0d8a00 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteWithTransformations.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteWithTransformations.java @@ -60,7 +60,7 @@ public static void tearDownAfterClass() throws Exception { @Before public void setUp() throws Exception { - System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); +// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); // create new connection for each test below client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST); } @@ -128,26 +128,26 @@ public void testBulkLoadWithXQueryTransform() throws Exception { FileHandle transformHandle = new FileHandle(transformFile); transMgr.writeXQueryTransform("add-attr-xquery-transform", transformHandle, metadata); ServerTransform transform = new ServerTransform("add-attr-xquery-transform"); - transform.put("trans:name", "Lang"); - transform.put("trans:value", "English"); + transform.put("name", "Lang"); + transform.put("value", "English"); int count=1; XMLDocumentManager docMgr = client.newXMLDocumentManager(); HashMap map= new HashMap(); DocumentWriteSet writeset =docMgr.newWriteSet(); - for(int i =0;i<12;i++){ + for(int i =0;i<102;i++){ writeset.add(DIRECTORY+"foo"+i+".xml", new DOMHandle(getDocumentContent("This is so foo"+i))); map.put(DIRECTORY+"foo"+i+".xml", convertXMLDocumentToString(getDocumentContent("This is so foo"+i))); - if(count%10 == 0){ + if(count%BATCH_SIZE == 0){ docMgr.write(writeset,transform); writeset = docMgr.newWriteSet(); } count++; } - if(count%10 > 0){ + if(count%BATCH_SIZE > 0){ docMgr.write(writeset,transform); } - docMgr.write("00.xml",new DOMHandle(getDocumentContent("This is so foo")),transform); + String uris[] = new String[102]; for(int i =0;i<102;i++){ uris[i]=DIRECTORY+"foo"+i+".xml"; @@ -158,7 +158,59 @@ public void testBulkLoadWithXQueryTransform() throws Exception { while(page.hasNext()){ DocumentRecord rec = page.next(); rec.getContent(dh); - assertEquals("Comparing the content :",map.get(rec.getUri()),convertXMLDocumentToString(dh.get())); + assertTrue("Element has attribure ? :",dh.get().getElementsByTagName("foo").item(0).hasAttributes()); + count++; + } + + assertEquals("document count", 102,count); + + } + @Test + public void testBulkReadWithXQueryTransform() throws Exception { + + TransformExtensionsManager transMgr = + client.newServerConfigManager().newTransformExtensionsManager(); + ExtensionMetadata metadata = new ExtensionMetadata(); + metadata.setTitle("Adding attribute xquery Transform"); + metadata.setDescription("This plugin transforms an XML document by adding attribute to root node"); + metadata.setProvider("MarkLogic"); + metadata.setVersion("0.1"); + // get the transform file + File transformFile = new File("src/test/java/com/marklogic/javaclient/transforms/add-attr-xquery-transform.xqy"); + FileHandle transformHandle = new FileHandle(transformFile); + transMgr.writeXQueryTransform("add-attr-xquery-transform", transformHandle, metadata); + ServerTransform transform = new ServerTransform("add-attr-xquery-transform"); + transform.put("name", "Lang"); + transform.put("value", "English"); + int count=1; + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + HashMap map= new HashMap(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + for(int i =0;i<102;i++){ + + writeset.add(DIRECTORY+"sec"+i+".xml", new DOMHandle(getDocumentContent("This is to read"+i))); + map.put(DIRECTORY+"sec"+i+".xml", convertXMLDocumentToString(getDocumentContent("This is to read"+i))); + if(count%BATCH_SIZE == 0){ + docMgr.write(writeset); + writeset = docMgr.newWriteSet(); + } + count++; + } + if(count%BATCH_SIZE > 0){ + docMgr.write(writeset); + } + + String uris[] = new String[102]; + for(int i =0;i<102;i++){ + uris[i]=DIRECTORY+"sec"+i+".xml"; + } + count=0; + DocumentPage page = docMgr.read(transform,uris); + DOMHandle dh = new DOMHandle(); + while(page.hasNext()){ + DocumentRecord rec = page.next(); + rec.getContent(dh); + assertTrue("Element has attribure ? :",dh.get().getElementsByTagName("foo").item(0).hasAttributes()); count++; } From 599940f51e39e1be4df79a65ddb088ebb58fce71 Mon Sep 17 00:00:00 2001 From: MarkLogic Builder Date: Wed, 6 Aug 2014 19:00:42 -0700 Subject: [PATCH 004/357] updating with changes to file paths --- .../marklogic/javaclient/ConnectedRESTQA.java | 1497 +++++++++-------- .../marklogic/javaclient/TestAggregates.java | 2 +- .../marklogic/javaclient/TestBug21159.java | 4 +- .../javaclient/TestBulkReadSample1.java | 2 +- .../javaclient/TestBulkWriteMetadata1.java | 2 +- .../javaclient/TestBulkWriteMetadata2.java | 2 +- .../TestBulkWriteMetadatawithRawXML.java | 13 +- .../TestBulkWriteWithTransformations.java | 6 +- .../javaclient/TestCRUDModulesDb.java | 20 +- .../javaclient/TestQueryByExample.java | 24 +- .../javaclient/TestQueryOptionsHandle.java | 10 +- .../marklogic/javaclient/TestRawAlert.java | 38 +- .../javaclient/TestRawCombinedQuery.java | 28 +- .../javaclient/TestRawCombinedQueryGeo.java | 14 +- .../javaclient/TestRawStructuredQuery.java | 18 +- .../javaclient/TestResponseTransform.java | 8 +- .../javaclient/TestSearchMultibyte.java | 2 +- .../javaclient/TestSearchOnJSON.java | 4 +- .../javaclient/TestStructuredQuery.java | 18 +- .../javaclient/TestTransformXMLWithXSLT.java | 12 +- .../transforms/add-attr-xquery-transform.xqy | 4 +- 21 files changed, 878 insertions(+), 850 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java b/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java index 08a6d382a..faf347ce8 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java @@ -52,15 +52,15 @@ import static org.junit.Assert.*; public abstract class ConnectedRESTQA { - + /** * Use Rest call to create a database. * @param dbName */ - + public static void createDB(String dbName) { - try { - + try { + DefaultHttpClient client = new DefaultHttpClient(); client.getCredentialsProvider().setCredentials( new AuthScope("localhost", 8002), @@ -69,56 +69,56 @@ public static void createDB(String dbName) { HttpPost post = new HttpPost("http://localhost:8002"+ "/manage/v2/databases?format=json"); String JSONString = "[{\"name\":\""+ dbName + "\"}]"; - post.addHeader("Content-type", "application/json"); - post.setEntity( new StringEntity(JSONString)); - - + post.addHeader("Content-type", "application/json"); + post.setEntity( new StringEntity(JSONString)); + + HttpResponse response = client.execute(post); HttpEntity respEntity = response.getEntity(); - if (respEntity != null) { - // EntityUtils to get the response content - String content = EntityUtils.toString(respEntity); - System.out.println(content); - } + if (respEntity != null) { + // EntityUtils to get the response content + String content = EntityUtils.toString(respEntity); + System.out.println(content); + } }catch (Exception e) { - // writing error to Log - e.printStackTrace(); + // writing error to Log + e.printStackTrace(); } } - /* + /* * */ public static void createForest(String fName,String dbName) { - try{ - DefaultHttpClient client = new DefaultHttpClient(); - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); - HttpPost post = new HttpPost("http://localhost:8002"+ "/manage/v2/forests?format=json"); - String hName =InetAddress.getLocalHost().getCanonicalHostName().toLowerCase(); - String JSONString = - "{\"database\":\""+ - dbName + - "\",\"forest-name\":\""+ - fName+ - "\",\"host\":\""+hName+"\"}" - ; -// System.out.println(JSONString); - post.addHeader("Content-type", "application/json"); - post.setEntity(new StringEntity(JSONString)); - - HttpResponse response = client.execute(post); - HttpEntity respEntity = response.getEntity(); - - if (respEntity != null) { - // EntityUtils to get the response content - String content = EntityUtils.toString(respEntity); - System.out.println(content); - } + try{ + DefaultHttpClient client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + HttpPost post = new HttpPost("http://localhost:8002"+ "/manage/v2/forests?format=json"); + String hName =InetAddress.getLocalHost().getCanonicalHostName().toLowerCase(); + String JSONString = + "{\"database\":\""+ + dbName + + "\",\"forest-name\":\""+ + fName+ + "\",\"host\":\""+hName+"\"}" + ; + // System.out.println(JSONString); + post.addHeader("Content-type", "application/json"); + post.setEntity(new StringEntity(JSONString)); + + HttpResponse response = client.execute(post); + HttpEntity respEntity = response.getEntity(); + + if (respEntity != null) { + // EntityUtils to get the response content + String content = EntityUtils.toString(respEntity); + System.out.println(content); + } }catch (Exception e) { - // writing error to Log - e.printStackTrace(); + // writing error to Log + e.printStackTrace(); } } /* @@ -133,29 +133,29 @@ public static void createForestonHost(String fName,String dbName,String hName) { HttpPost post = new HttpPost("http://localhost:8002"+ "/manage/v2/forests?format=json"); String JSONString = "{\"database\":\""+ - dbName + - "\",\"forest-name\":\""+ - fName+ - "\",\"host\":\""+hName+"\"}" - ; -// System.out.println(JSONString); + dbName + + "\",\"forest-name\":\""+ + fName+ + "\",\"host\":\""+hName+"\"}" + ; + // System.out.println(JSONString); post.addHeader("Content-type", "application/json"); post.setEntity(new StringEntity(JSONString)); - + HttpResponse response = client.execute(post); HttpEntity respEntity = response.getEntity(); if (respEntity != null) { - // EntityUtils to get the response content - String content = EntityUtils.toString(respEntity); - System.out.println(content); - } - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); + // EntityUtils to get the response content + String content = EntityUtils.toString(respEntity); + System.out.println(content); } + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); } - + } + public static void assocRESTServer(String restServerName,String dbName,int restPort) { try{ DefaultHttpClient client = new DefaultHttpClient(); @@ -165,35 +165,55 @@ public static void assocRESTServer(String restServerName,String dbName,int restP new UsernamePasswordCredentials("admin", "admin")); HttpPost post = new HttpPost("http://localhost:8002"+ "/v1/rest-apis?format=json"); -// + // String JSONString = "{ \"rest-api\": {\"database\":\""+ - dbName + - "\",\"name\":\""+ - restServerName + - "\",\"port\":\""+ - restPort+ - "\"}}"; -// System.out.println(JSONString); + dbName + + "\",\"name\":\""+ + restServerName + + "\",\"port\":\""+ + restPort+ + "\"}}"; + // System.out.println(JSONString); post.addHeader("Content-type", "application/json"); post.setEntity(new StringEntity(JSONString)); - - HttpResponse response = client.execute(post); - HttpEntity respEntity = response.getEntity(); - if (respEntity != null) { - // EntityUtils to get the response content - String content = EntityUtils.toString(respEntity); - System.out.println(content); - } - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); + HttpResponse response = client.execute(post); + HttpEntity respEntity = response.getEntity(); + System.out.println(JSONString); + if (response.getStatusLine().getStatusCode() == 400) { + // EntityUtils to get the response content + + System.out.println("AppServer already exist, so changing the content database to new DB"); + associateRESTServerWithDB(restServerName,dbName); } + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); } -/* - * Creating RESTServer With default content and module database - */ + } + public static void associateRESTServerWithDB(String restServerName,String dbName)throws Exception{ + DefaultHttpClient client = new DefaultHttpClient(); + + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + String body = "{\"content-database\": \""+dbName+"\",\"group-name\": \"Default\"}"; + + HttpPut put = new HttpPut("http://localhost:8002/manage/v2/servers/"+restServerName+"/properties?server-type=http"); + put.addHeader("Content-type", "application/json"); + put.setEntity(new StringEntity(body)); + + HttpResponse response2 = client.execute(put); + HttpEntity respEntity = response2.getEntity(); + if(respEntity != null){ + String content = EntityUtils.toString(respEntity); + System.out.println(content); + } + } + /* + * Creating RESTServer With default content and module database + */ public static void createRESTServerWithDB(String restServerName,int restPort) { try{ DefaultHttpClient client = new DefaultHttpClient(); @@ -202,236 +222,204 @@ public static void createRESTServerWithDB(String restServerName,int restPort) { new AuthScope("localhost", 8002), new UsernamePasswordCredentials("admin", "admin")); HttpPost post = new HttpPost("http://localhost:8002"+ "/v1/rest-apis?format=json"); -// + // String JSONString = "{ \"rest-api\": {\"name\":\""+ - restServerName + - "\",\"port\":\""+ - restPort+ - "\"}}"; + restServerName + + "\",\"port\":\""+ + restPort+ + "\"}}"; //System.out.println(JSONString); post.addHeader("Content-type", "application/json"); post.setEntity(new StringEntity(JSONString)); - - HttpResponse response = client.execute(post); - HttpEntity respEntity = response.getEntity(); - if (respEntity != null) { - // EntityUtils to get the response content - String content = EntityUtils.toString(respEntity); - System.out.println(content); - } - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); + HttpResponse response = client.execute(post); + HttpEntity respEntity = response.getEntity(); + + if (respEntity != null) { + // EntityUtils to get the response content + String content = EntityUtils.toString(respEntity); + System.out.println(content); } + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); } + } /* * This function creates database,forests and REST server independently and attaches the database to the rest server * */ public static void setupJavaRESTServer(String dbName, String fName, String restServerName, int restPort)throws Exception{ - - createDB(dbName); - createForest(fName,dbName); - Thread.sleep(1500); - assocRESTServer(restServerName, dbName,restPort); - createRESTUser("rest-admin","x","rest-admin"); - createRESTUser("rest-writer","x","rest-writer"); - createRESTUser("rest-reader","x","rest-reader"); + + createDB(dbName); + createForest(fName,dbName); + Thread.sleep(1500); + assocRESTServer(restServerName, dbName,restPort); + createRESTUser("rest-admin","x","rest-admin"); + createRESTUser("rest-writer","x","rest-writer"); + createRESTUser("rest-reader","x","rest-reader"); } /* * This function creates a REST server with default content DB, Module DB */ - public static void createRESTUser(String usrName, String pass, String... roleNames ){ - try{ - DefaultHttpClient client = new DefaultHttpClient(); - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); + public static void createRESTUser(String usrName, String pass, String... roleNames ){ + try{ + DefaultHttpClient client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); HttpGet getrequest = new HttpGet("http://localhost:8002"+ "/manage/v2/users/"+usrName); HttpResponse resp = client.execute(getrequest); - + if( resp.getStatusLine().getStatusCode() == 200) - { - System.out.println("User already exist"); - } - else { - System.out.println("User dont exist"); - client = new DefaultHttpClient(); - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); - - ObjectMapper mapper = new ObjectMapper(); - ObjectNode mainNode = mapper.createObjectNode(); -// ObjectNode childNode = mapper.createObjectNode(); - ArrayNode childArray = mapper.createArrayNode(); - mainNode.put("name",usrName); - mainNode.put("description", "user discription"); - mainNode.put("password", pass); - for(String rolename: roleNames) - childArray.add(rolename); - mainNode.withArray("role").addAll(childArray); - //System.out.println(type + mainNode.path("range-element-indexes").path("range-element-index").toString()); + { + System.out.println("User already exist"); + } + else { + System.out.println("User dont exist"); + client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + + ObjectMapper mapper = new ObjectMapper(); + ObjectNode mainNode = mapper.createObjectNode(); + // ObjectNode childNode = mapper.createObjectNode(); + ArrayNode childArray = mapper.createArrayNode(); + mainNode.put("name",usrName); + mainNode.put("description", "user discription"); + mainNode.put("password", pass); + for(String rolename: roleNames) + childArray.add(rolename); + mainNode.withArray("role").addAll(childArray); + //System.out.println(type + mainNode.path("range-element-indexes").path("range-element-index").toString()); System.out.println(mainNode.toString()); HttpPost post = new HttpPost("http://localhost:8002"+ "/manage/v2/users?format=json"); post.addHeader("Content-type", "application/json"); post.setEntity(new StringEntity(mainNode.toString())); - + HttpResponse response = client.execute(post); HttpEntity respEntity = response.getEntity(); - if( response.getStatusLine().getStatusCode() == 400) - { - System.out.println("User already exist"); - } - else if (respEntity != null) { - // EntityUtils to get the response content - String content = EntityUtils.toString(respEntity); - System.out.println(content); + if( response.getStatusLine().getStatusCode() == 400) + { + System.out.println("User already exist"); } - else {System.out.print("No Proper Response");} - } - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); + else if (respEntity != null) { + // EntityUtils to get the response content + String content = EntityUtils.toString(respEntity); + System.out.println(content); } - } - - /* - * "permission": [ + else {System.out.print("No Proper Response");} + } + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } + } + + /* + * "permission": [ { "role-name": "dls-user", "capability": "read" } - */ - - public static ObjectNode getPermissionNode(String roleName, DocumentMetadataHandle.Capability... cap){ - ObjectMapper mapper= new ObjectMapper(); - ObjectNode mNode = mapper.createObjectNode(); - ArrayNode aNode = mapper.createArrayNode(); - - for(DocumentMetadataHandle.Capability c : cap){ - ObjectNode roleNode =mapper.createObjectNode(); - roleNode.put("role-name",roleName); - roleNode.put("capability", c.toString().toLowerCase()); - aNode.add(roleNode); - } - mNode.withArray("permission").addAll(aNode); - return mNode; - } - - /* - * "collection": + */ + + public static ObjectNode getPermissionNode(String roleName, DocumentMetadataHandle.Capability... cap){ + ObjectMapper mapper= new ObjectMapper(); + ObjectNode mNode = mapper.createObjectNode(); + ArrayNode aNode = mapper.createArrayNode(); + + for(DocumentMetadataHandle.Capability c : cap){ + ObjectNode roleNode =mapper.createObjectNode(); + roleNode.put("role-name",roleName); + roleNode.put("capability", c.toString().toLowerCase()); + aNode.add(roleNode); + } + mNode.withArray("permission").addAll(aNode); + return mNode; + } + + /* + * "collection": [ "dadfasd", "adfadsfads" ] - */ - public static ObjectNode getCollectionNode(String... collections){ - ObjectMapper mapper= new ObjectMapper(); - ObjectNode mNode = mapper.createObjectNode(); - ArrayNode aNode = mapper.createArrayNode(); - - for(String c : collections){ - aNode.add(c); - } - mNode.withArray("collection").addAll(aNode); - return mNode; - } - - public static void createRESTUserWithPermissions(String usrName, String pass,ObjectNode perm,ObjectNode colections, String... roleNames ){ - try{ - DefaultHttpClient client = new DefaultHttpClient(); - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); + */ + public static ObjectNode getCollectionNode(String... collections){ + ObjectMapper mapper= new ObjectMapper(); + ObjectNode mNode = mapper.createObjectNode(); + ArrayNode aNode = mapper.createArrayNode(); + + for(String c : collections){ + aNode.add(c); + } + mNode.withArray("collection").addAll(aNode); + return mNode; + } + + public static void createRESTUserWithPermissions(String usrName, String pass,ObjectNode perm,ObjectNode colections, String... roleNames ){ + try{ + DefaultHttpClient client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); HttpGet getrequest = new HttpGet("http://localhost:8002"+ "/manage/v2/users/"+usrName); HttpResponse resp = client.execute(getrequest); - + if( resp.getStatusLine().getStatusCode() == 200) - { - System.out.println("User already exist"); - } - else { - System.out.println("User dont exist"); - client = new DefaultHttpClient(); - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); - - ObjectMapper mapper = new ObjectMapper(); - ObjectNode mainNode = mapper.createObjectNode(); -// ObjectNode childNode = mapper.createObjectNode(); - ArrayNode childArray = mapper.createArrayNode(); - mainNode.put("name",usrName); - mainNode.put("description", "user discription"); - mainNode.put("password", pass); - for(String rolename: roleNames) - childArray.add(rolename); - mainNode.withArray("role").addAll(childArray); - mainNode.setAll(perm); - mainNode.setAll(colections); - //System.out.println(type + mainNode.path("range-element-indexes").path("range-element-index").toString()); + { + System.out.println("User already exist"); + } + else { + System.out.println("User dont exist"); + client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + + ObjectMapper mapper = new ObjectMapper(); + ObjectNode mainNode = mapper.createObjectNode(); + // ObjectNode childNode = mapper.createObjectNode(); + ArrayNode childArray = mapper.createArrayNode(); + mainNode.put("name",usrName); + mainNode.put("description", "user discription"); + mainNode.put("password", pass); + for(String rolename: roleNames) + childArray.add(rolename); + mainNode.withArray("role").addAll(childArray); + mainNode.setAll(perm); + mainNode.setAll(colections); + //System.out.println(type + mainNode.path("range-element-indexes").path("range-element-index").toString()); System.out.println(mainNode.toString()); HttpPost post = new HttpPost("http://localhost:8002"+ "/manage/v2/users?format=json"); post.addHeader("Content-type", "application/json"); post.setEntity(new StringEntity(mainNode.toString())); - + HttpResponse response = client.execute(post); HttpEntity respEntity = response.getEntity(); - if( response.getStatusLine().getStatusCode() == 400) - { - System.out.println("Bad User creation request"); - } - else if (respEntity != null) { - // EntityUtils to get the response content - String content = EntityUtils.toString(respEntity); - System.out.println(content); + if( response.getStatusLine().getStatusCode() == 400) + { + System.out.println("Bad User creation request"); } - else {System.out.print("No Proper Response");} - } - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); + else if (respEntity != null) { + // EntityUtils to get the response content + String content = EntityUtils.toString(respEntity); + System.out.println(content); } - } - - public static void deleteRESTUser(String usrName){ - try{ - DefaultHttpClient client = new DefaultHttpClient(); - - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); - - HttpDelete delete = new HttpDelete("http://localhost:8002/manage/v2/users/"+usrName); - - HttpResponse response = client.execute(delete); - if(response.getStatusLine().getStatusCode()== 202){ - Thread.sleep(3500); - } + else {System.out.print("No Proper Response");} + } }catch (Exception e) { - // writing error to Log - e.printStackTrace(); + // writing error to Log + e.printStackTrace(); } - -} - - public static void setupJavaRESTServerWithDB( String restServerName, int restPort)throws Exception{ - createRESTServerWithDB(restServerName, restPort); - createRESTUser("rest-admin","x","rest-admin"); - createRESTUser("rest-writer","x","rest-writer"); - createRESTUser("rest-reader","x","rest-reader"); -} -/* - * This function deletes the REST appserver along with attached content database and module database - */ - - - public static void deleteRESTServerWithDB(String restServerName) { + } + + public static void deleteRESTUser(String usrName){ try{ DefaultHttpClient client = new DefaultHttpClient(); @@ -439,21 +427,31 @@ public static void deleteRESTServerWithDB(String restServerName) { new AuthScope("localhost", 8002), new UsernamePasswordCredentials("admin", "admin")); - HttpDelete delete = new HttpDelete("http://localhost:8002/v1/rest-apis/"+restServerName+"?include=content&include=modules"); - - HttpResponse response = client.execute(delete); + HttpDelete delete = new HttpDelete("http://localhost:8002/manage/v2/users/"+usrName); + + HttpResponse response = client.execute(delete); if(response.getStatusLine().getStatusCode()== 202){ - Thread.sleep(3500); - } - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); + Thread.sleep(3500); } + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); } + + } + + public static void setupJavaRESTServerWithDB( String restServerName, int restPort)throws Exception{ + createRESTServerWithDB(restServerName, restPort); + createRESTUser("rest-admin","x","rest-admin"); + createRESTUser("rest-writer","x","rest-writer"); + createRESTUser("rest-reader","x","rest-reader"); + } /* - * + * This function deletes the REST appserver along with attached content database and module database */ - public static void deleteRESTServer(String restServerName) { + + + public static void deleteRESTServerWithDB(String restServerName) { try{ DefaultHttpClient client = new DefaultHttpClient(); @@ -461,20 +459,43 @@ public static void deleteRESTServer(String restServerName) { new AuthScope("localhost", 8002), new UsernamePasswordCredentials("admin", "admin")); - HttpDelete delete = new HttpDelete("http://localhost:8002/v1/rest-apis/"+restServerName+"&include=modules"); + HttpDelete delete = new HttpDelete("http://localhost:8002/v1/rest-apis/"+restServerName+"?include=content&include=modules"); + HttpResponse response = client.execute(delete); - if(response.getStatusLine().getStatusCode()== 202){ Thread.sleep(3500); - waitForServerRestart(); - } - else System.out.println("Server response "+response.getStatusLine().getStatusCode()); - }catch (Exception e) { - // writing error to Log - System.out.println("Inside Deleting Rest server is throwing an error"); - e.printStackTrace(); } + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } + } + /* + * + */ + public static void deleteRESTServer(String restServerName) { + try{ + associateRESTServerWithDB(restServerName,"Documents"); + // DefaultHttpClient client = new DefaultHttpClient(); + // + // client.getCredentialsProvider().setCredentials( + // new AuthScope("localhost", 8002), + // new UsernamePasswordCredentials("admin", "admin")); + // + // HttpDelete delete = new HttpDelete("http://localhost:8002/v1/rest-apis/"+restServerName+"&include=modules"); + // HttpResponse response = client.execute(delete); + // + // if(response.getStatusLine().getStatusCode()== 202){ + // Thread.sleep(3500); + // waitForServerRestart(); + // } + // else System.out.println("Server response "+response.getStatusLine().getStatusCode()); + }catch (Exception e) { + // writing error to Log + System.out.println("Inside Deleting Rest server is throwing an error"); + e.printStackTrace(); } + } public static void detachForest(String dbName, String fName){ try{ DefaultHttpClient client = new DefaultHttpClient(); @@ -484,25 +505,25 @@ public static void detachForest(String dbName, String fName){ new UsernamePasswordCredentials("admin", "admin")); HttpPost post = new HttpPost("http://localhost:8002"+ "/manage/v2/forests/"+fName); -// + // List urlParameters = new ArrayList(); urlParameters.add(new BasicNameValuePair("state", "detach")); urlParameters.add(new BasicNameValuePair("database", dbName)); - + post.setEntity(new UrlEncodedFormEntity(urlParameters)); - - HttpResponse response = client.execute(post); - HttpEntity respEntity = response.getEntity(); - if (respEntity != null) { - // EntityUtils to get the response content - String content = EntityUtils.toString(respEntity); - System.out.println(content); - } - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); - } + HttpResponse response = client.execute(post); + HttpEntity respEntity = response.getEntity(); + + if (respEntity != null) { + // EntityUtils to get the response content + String content = EntityUtils.toString(respEntity); + System.out.println(content); + } + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } } /* * Deleting a forest is a HTTP Delete request @@ -517,18 +538,18 @@ public static void deleteForest(String fName){ HttpDelete delete = new HttpDelete("http://localhost:8002/manage/v2/forests/"+fName+"?level=full"); client.execute(delete); - - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); - } + + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } } -/* - * Deleting Database - * - */ + /* + * Deleting Database + * + */ public static void deleteDB(String dbName){ - try{ + try{ DefaultHttpClient client = new DefaultHttpClient(); client.getCredentialsProvider().setCredentials( new AuthScope("localhost", 8002), @@ -536,13 +557,13 @@ public static void deleteDB(String dbName){ HttpDelete delete = new HttpDelete("http://localhost:8002/manage/v2/databases/"+dbName); client.execute(delete); - - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); - } + + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } } - + public static void clearDB(int port){ try{ DefaultHttpClient client = new DefaultHttpClient(); @@ -552,46 +573,46 @@ public static void clearDB(int port){ String uri = "http://localhost:"+port+"/v1/search/"; HttpDelete delete = new HttpDelete(uri); client.execute(delete); - - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); - } + + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } } public static void waitForServerRestart() { try{ - int count = 0; - while(count <20){ - DefaultHttpClient client = new DefaultHttpClient(); - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8001), - new UsernamePasswordCredentials("admin", "admin")); - - count++; - try{ - HttpGet getrequest = new HttpGet("http://localhost:8001/admin/v1/timestamp"); - HttpResponse response = client.execute(getrequest); - if(response.getStatusLine().getStatusCode() == 503){Thread.sleep(4000);} - else if(response.getStatusLine().getStatusCode() == 200){ - break; - } - else { - System.out.println("Waiting for response from server, Trial :"+response.getStatusLine().getStatusCode()+count); - Thread.sleep(2000); - } - }catch(Exception e){Thread.sleep(2000);} + int count = 0; + while(count <20){ + DefaultHttpClient client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8001), + new UsernamePasswordCredentials("admin", "admin")); + + count++; + try{ + HttpGet getrequest = new HttpGet("http://localhost:8001/admin/v1/timestamp"); + HttpResponse response = client.execute(getrequest); + if(response.getStatusLine().getStatusCode() == 503){Thread.sleep(4000);} + else if(response.getStatusLine().getStatusCode() == 200){ + break; + } + else { + System.out.println("Waiting for response from server, Trial :"+response.getStatusLine().getStatusCode()+count); + Thread.sleep(2000); + } + }catch(Exception e){Thread.sleep(2000);} + } + }catch(Exception e){ + System.out.println("Inside wait for server restart is throwing an error"); + e.printStackTrace(); } - }catch(Exception e){ - System.out.println("Inside wait for server restart is throwing an error"); - e.printStackTrace(); - } } /* * This function deletes rest server first and deletes forests and databases in separate calls */ public static void tearDownJavaRESTServer(String dbName, String [] fNames, String restServerName) throws Exception{ - + try{ deleteRESTServer(restServerName); }catch(Exception e){ @@ -604,34 +625,34 @@ public static void tearDownJavaRESTServer(String dbName, String [] fNames, Strin detachForest(dbName, fNames[i]); } }catch(Exception e){ - e.printStackTrace(); - } + e.printStackTrace(); + } try{ for(int i = 0; i < fNames.length; i++){ deleteForest(fNames[i]); } }catch(Exception e){ - e.printStackTrace(); - } - - deleteDB(dbName); - } - + e.printStackTrace(); + } + + deleteDB(dbName); + } + /* * This function deletes rest server along with default forest and database */ public static void tearDownJavaRESTServerWithDB(String restServerName) throws Exception{ - + try{ deleteRESTServerWithDB(restServerName); }catch(Exception e){ e.printStackTrace(); } Thread.sleep(6000); - - } - + + } + /* * * setting up AppServices configurations @@ -648,35 +669,35 @@ public static void setDatabaseProperties(String dbName,String prop,String propVa HttpResponse response1 = client.execute(getrequest); jsonstream =response1.getEntity().getContent(); JsonNode jnode= new ObjectMapper().readTree(jsonstream); - if(!jnode.isNull()){ - ((ObjectNode)jnode).put(prop, propValue); -// System.out.println(jnode.toString()+"\n"+ response1.getStatusLine().getStatusCode()); - HttpPut put = new HttpPut("http://localhost:8002"+ "/manage/v2/databases/"+dbName+"/properties?format=json"); - put.addHeader("Content-type", "application/json"); - put.setEntity(new StringEntity(jnode.toString())); - - HttpResponse response2 = client.execute(put); - HttpEntity respEntity = response2.getEntity(); - if(respEntity != null){ - String content = EntityUtils.toString(respEntity); - System.out.println(content); - } - } - else{ - System.out.println("REST call for database properties returned NULL "); - } + if(!jnode.isNull()){ + ((ObjectNode)jnode).put(prop, propValue); + // System.out.println(jnode.toString()+"\n"+ response1.getStatusLine().getStatusCode()); + HttpPut put = new HttpPut("http://localhost:8002"+ "/manage/v2/databases/"+dbName+"/properties?format=json"); + put.addHeader("Content-type", "application/json"); + put.setEntity(new StringEntity(jnode.toString())); + + HttpResponse response2 = client.execute(put); + HttpEntity respEntity = response2.getEntity(); + if(respEntity != null){ + String content = EntityUtils.toString(respEntity); + System.out.println(content); + } + } + else{ + System.out.println("REST call for database properties returned NULL "); + } }catch (Exception e) { - // writing error to Log - e.printStackTrace(); + // writing error to Log + e.printStackTrace(); } finally{ if(jsonstream == null){} else{ jsonstream.close(); } - } } - + } + public static void setDatabaseProperties(String dbName,String prop,boolean propValue ) throws IOException{ InputStream jsonstream=null; try{ @@ -688,35 +709,35 @@ public static void setDatabaseProperties(String dbName,String prop,boolean propV HttpResponse response1 = client.execute(getrequest); jsonstream =response1.getEntity().getContent(); JsonNode jnode= new ObjectMapper().readTree(jsonstream); - if(!jnode.isNull()){ - ((ObjectNode)jnode).put(prop, propValue) ; -// System.out.println(jnode.toString()+"\n"+ response1.getStatusLine().getStatusCode()); - HttpPut put = new HttpPut("http://localhost:8002"+ "/manage/v2/databases/"+dbName+"/properties?format=json"); - put.addHeader("Content-type", "application/json"); - put.setEntity(new StringEntity(jnode.toString())); - - HttpResponse response2 = client.execute(put); - HttpEntity respEntity = response2.getEntity(); - if(respEntity != null){ - String content = EntityUtils.toString(respEntity); - System.out.println(content); - } - } - else{ - System.out.println("REST call for database properties returned NULL "); - } + if(!jnode.isNull()){ + ((ObjectNode)jnode).put(prop, propValue) ; + // System.out.println(jnode.toString()+"\n"+ response1.getStatusLine().getStatusCode()); + HttpPut put = new HttpPut("http://localhost:8002"+ "/manage/v2/databases/"+dbName+"/properties?format=json"); + put.addHeader("Content-type", "application/json"); + put.setEntity(new StringEntity(jnode.toString())); + + HttpResponse response2 = client.execute(put); + HttpEntity respEntity = response2.getEntity(); + if(respEntity != null){ + String content = EntityUtils.toString(respEntity); + System.out.println(content); + } + } + else{ + System.out.println("REST call for database properties returned NULL "); + } }catch (Exception e) { - // writing error to Log - e.printStackTrace(); + // writing error to Log + e.printStackTrace(); } finally{ if(jsonstream == null){} else{ jsonstream.close(); } - } } - + } + /* * This Method takes the root property name and object node under it * if root propname exist and equals to null then it just add the object node under root property name else if it has an existing sub property name then it adds @@ -734,52 +755,52 @@ public static void setDatabaseProperties(String dbName,String propName, ObjectNo jsonstream =response1.getEntity().getContent(); ObjectMapper mapper = new ObjectMapper(); JsonNode jnode= mapper.readTree(jsonstream); - if(!jnode.isNull()){ - - if(!jnode.has(propName)){ - ((ObjectNode)jnode).putArray(propName).addAll(objNode.withArray(propName)); -// System.out.println("when Node is null"+propName + objNode.toString()); - } - else{ - if(!jnode.path(propName).isArray()){ - System.out.println("property is not array"); - ((ObjectNode)jnode).putAll(objNode); - } - else{ - JsonNode member = jnode.withArray(propName); - if(objNode.path(propName).isArray()){ - ((ArrayNode)member).addAll(objNode.withArray(propName)); - // System.out.println("when Node is not null"+ propName + objNode.withArray(propName).toString()); - } - } - } - - HttpPut put = new HttpPut("http://localhost:8002"+ "/manage/v2/databases/"+dbName+"/properties?format=json"); - put.addHeader("Content-type", "application/json"); - put.setEntity(new StringEntity(jnode.toString())); - - HttpResponse response2 = client.execute(put); - HttpEntity respEntity = response2.getEntity(); - if(respEntity != null){ - String content = EntityUtils.toString(respEntity); - System.out.println(content); - } - } - else{ - System.out.println("REST call for database properties returned NULL \n"+jnode.toString()+"\n"+ response1.getStatusLine().getStatusCode()); - } + if(!jnode.isNull()){ + + if(!jnode.has(propName)){ + ((ObjectNode)jnode).putArray(propName).addAll(objNode.withArray(propName)); + // System.out.println("when Node is null"+propName + objNode.toString()); + } + else{ + if(!jnode.path(propName).isArray()){ + System.out.println("property is not array"); + ((ObjectNode)jnode).putAll(objNode); + } + else{ + JsonNode member = jnode.withArray(propName); + if(objNode.path(propName).isArray()){ + ((ArrayNode)member).addAll(objNode.withArray(propName)); + // System.out.println("when Node is not null"+ propName + objNode.withArray(propName).toString()); + } + } + } + + HttpPut put = new HttpPut("http://localhost:8002"+ "/manage/v2/databases/"+dbName+"/properties?format=json"); + put.addHeader("Content-type", "application/json"); + put.setEntity(new StringEntity(jnode.toString())); + + HttpResponse response2 = client.execute(put); + HttpEntity respEntity = response2.getEntity(); + if(respEntity != null){ + String content = EntityUtils.toString(respEntity); + System.out.println(content); + } + } + else{ + System.out.println("REST call for database properties returned NULL \n"+jnode.toString()+"\n"+ response1.getStatusLine().getStatusCode()); + } }catch (Exception e) { - // writing error to Log - e.printStackTrace(); + // writing error to Log + e.printStackTrace(); } finally{ if(jsonstream == null){} else{ jsonstream.close(); } - } } - + } + public static void enableCollectionLexicon(String dbName) throws Exception{ setDatabaseProperties(dbName,"collection-lexicon",true ); } @@ -796,12 +817,12 @@ public static void enableWordLexicon(String dbName) throws Exception{ childArray.add("http://marklogic.com/collation/"); childNode.putArray("word-lexicon").addAll(childArray); setDatabaseProperties(dbName,"word-lexicons",childNode); - + } public static void enableTrailingWildcardSearches(String dbName) throws Exception{ setDatabaseProperties(dbName,"trailing-wildcard-searches",true ); } - + public static void setMaintainLastModified(String dbName,boolean opt) throws Exception{ setDatabaseProperties(dbName,"maintain-last-modified",opt); } @@ -812,7 +833,7 @@ public static void setMaintainLastModified(String dbName,boolean opt) throws Exc public static void addRangeElementIndex(String dbName, String type, String namespace, String localname) throws Exception{ ObjectMapper mapper = new ObjectMapper(); ObjectNode mainNode = mapper.createObjectNode(); - // ObjectNode childNode = mapper.createObjectNode(); + // ObjectNode childNode = mapper.createObjectNode(); ArrayNode childArray = mapper.createArrayNode(); ObjectNode childNodeObject = mapper.createObjectNode(); childNodeObject.put( "scalar-type", type); @@ -823,12 +844,12 @@ public static void addRangeElementIndex(String dbName, String type, String name childNodeObject.put("invalid-values", "reject"); childArray.add(childNodeObject); mainNode.putArray("range-element-index").addAll(childArray); - // mainNode.put("range-element-indexes", childNode); -// System.out.println(type + mainNode.path("range-element-indexes").path("range-element-index").toString()); + // mainNode.put("range-element-indexes", childNode); + // System.out.println(type + mainNode.path("range-element-indexes").path("range-element-index").toString()); setDatabaseProperties(dbName,"range-element-index",mainNode); - + } - + /* * This is a overloaded function constructs a range element index with default range-value-positions and invalid values @@ -837,7 +858,7 @@ public static void addRangeElementIndex(String dbName, String type, String name public static void addRangeElementIndex(String dbName, String type, String namespace, String localname, String collation) throws Exception{ ObjectMapper mapper = new ObjectMapper(); ObjectNode mainNode = mapper.createObjectNode(); - // ObjectNode childNode = mapper.createObjectNode(); + // ObjectNode childNode = mapper.createObjectNode(); ArrayNode childArray = mapper.createArrayNode(); ObjectNode childNodeObject = mapper.createObjectNode(); childNodeObject.put( "scalar-type", type); @@ -848,10 +869,10 @@ public static void addRangeElementIndex(String dbName, String type, String name childNodeObject.put("invalid-values", "reject"); childArray.add(childNodeObject); mainNode.putArray("range-element-index").addAll(childArray); - -// System.out.println(type + mainNode.path("range-element-indexes").path("range-element-index").toString()); + + // System.out.println(type + mainNode.path("range-element-indexes").path("range-element-index").toString()); setDatabaseProperties(dbName,"range-element-index",mainNode); - + } /* @@ -864,10 +885,10 @@ public static void addRangeElementIndex(String dbName, String type, String name "range-value-positions": false, "invalid-values": "reject" */ - + public static void addRangeElementAttributeIndex(String dbName, String type, String parentnamespace, String parentlocalname, String namespace, String localname, String collation) throws Exception{ ObjectMapper mapper = new ObjectMapper(); - // ObjectNode mainNode = mapper.createObjectNode(); + // ObjectNode mainNode = mapper.createObjectNode(); ObjectNode childNode = mapper.createObjectNode(); ArrayNode childArray = mapper.createArrayNode(); ObjectNode childNodeObject = mapper.createObjectNode(); @@ -877,16 +898,16 @@ public static void addRangeElementAttributeIndex(String dbName, String type, Str childNodeObject.put( "parent-localname", parentlocalname); childNodeObject.put( "namespace-uri", namespace); childNodeObject.put( "localname", localname); - + childNodeObject.put("range-value-positions", false); childNodeObject.put("invalid-values", "reject"); childArray.add(childNodeObject); childNode.putArray("range-element-attribute-index").addAll(childArray); - // mainNode.put("range-element-attribute-indexes", childNode); -// System.out.println(type + mainNode.path("range-element-attribute-indexes").path("range-element-attribute-index").toString()); + // mainNode.put("range-element-attribute-indexes", childNode); + // System.out.println(type + mainNode.path("range-element-attribute-indexes").path("range-element-attribute-index").toString()); setDatabaseProperties(dbName,"range-element-attribute-index",childNode); - + } /* * Overloaded function with default collation @@ -903,15 +924,15 @@ public static void addRangeElementAttributeIndex(String dbName, String type, Str childNodeObject.put( "parent-localname", parentlocalname); childNodeObject.put( "namespace-uri", namespace); childNodeObject.put( "localname", localname); - + childNodeObject.put("range-value-positions", false); childNodeObject.put("invalid-values", "reject"); childArray.add(childNodeObject); childNode.putArray("range-element-attribute-index").addAll(childArray); - // mainNode.put("range-element-attribute-indexes", childNode); -// System.out.println(type + mainNode.path("range-element-attribute-indexes").path("range-element-attribute-index").toString()); + // mainNode.put("range-element-attribute-indexes", childNode); + // System.out.println(type + mainNode.path("range-element-attribute-indexes").path("range-element-attribute-index").toString()); setDatabaseProperties(dbName,"range-element-attribute-index",childNode); - + } /* * "range-path-indexes": { @@ -928,7 +949,7 @@ public static void addRangeElementAttributeIndex(String dbName, String type, Str */ public static void addRangePathIndex(String dbName, String type, String pathexpr, String collation, String invalidValues) throws Exception{ ObjectMapper mapper = new ObjectMapper(); -// ObjectNode mainNode = mapper.createObjectNode(); + // ObjectNode mainNode = mapper.createObjectNode(); ObjectNode childNode = mapper.createObjectNode(); ArrayNode childArray = mapper.createArrayNode(); ObjectNode childNodeObject = mapper.createObjectNode(); @@ -939,108 +960,108 @@ public static void addRangePathIndex(String dbName, String type, String pathexpr childNodeObject.put("invalid-values", invalidValues); childArray.add(childNodeObject); childNode.putArray("range-path-index").addAll(childArray); -// mainNode.put("range-path-indexes", childNode); -// System.out.println(type + mainNode.path("range-path-indexes").path("range-path-index").toString()); + // mainNode.put("range-path-indexes", childNode); + // System.out.println(type + mainNode.path("range-path-indexes").path("range-path-index").toString()); setDatabaseProperties(dbName,"range-path-index",childNode); - + + } + public static void addGeospatialElementIndexes(String dbName,String localname,String namespace,String coordinateSystem,String pointFormat,boolean rangeValuePositions,String invalidValues) throws Exception{ + ObjectMapper mapper = new ObjectMapper(); + // ObjectNode mainNode = mapper.createObjectNode(); + ObjectNode childNode = mapper.createObjectNode(); + ArrayNode childArray = mapper.createArrayNode(); + ObjectNode childNodeObject = mapper.createObjectNode(); + childNodeObject.put( "namespace-uri", namespace); + childNodeObject.put( "localname", localname); + childNodeObject.put( "coordinate-system", coordinateSystem); + childNodeObject.put("range-value-positions", false); + childNodeObject.put("invalid-values", invalidValues); + childNodeObject.put("point-format",pointFormat); + childArray.add(childNodeObject); + childNode.putArray("geospatial-element-index").addAll(childArray); + // mainNode.put("geospatial-element-indexes", childNode); + // System.out.println(type + mainNode.path("range-path-indexes").path("range-path-index").toString()); + setDatabaseProperties(dbName,"geospatial-element-index",childNode); + } + public static void addGeoSpatialElementChildIndexes(String dbName,String parentNamespaceUri,String parentLocalName,String namespace,String localname,String coordinateSystem,String pointFormat,boolean rangeValuePositions,String invalidValues) throws Exception{ + ObjectMapper mapper = new ObjectMapper(); + // ObjectNode mainNode = mapper.createObjectNode(); + ObjectNode childNode = mapper.createObjectNode(); + ArrayNode childArray = mapper.createArrayNode(); + ObjectNode childNodeObject = mapper.createObjectNode(); + childNodeObject.put( "parent-namespace-uri", parentNamespaceUri); + childNodeObject.put( "parent-localname", parentLocalName); + childNodeObject.put( "namespace-uri", namespace); + childNodeObject.put( "localname", localname); + childNodeObject.put( "coordinate-system", coordinateSystem); + childNodeObject.put("range-value-positions", false); + childNodeObject.put("invalid-values", invalidValues); + childNodeObject.put("point-format",pointFormat); + childArray.add(childNodeObject); + childNode.putArray("geospatial-element-child-index").addAll(childArray); + // mainNode.put("geospatial-element-child-indexes", childNode); + // System.out.println(type + mainNode.path("range-path-indexes").path("range-path-index").toString()); + setDatabaseProperties(dbName,"geospatial-element-child-index",childNode); + } + public static void addGeospatialElementPairIndexes(String dbName,String parentNamespaceUri,String parentLocalName,String latNamespace,String latLocalname,String longNamespace,String longLocalname,String coordinateSystem,boolean rangeValuePositions,String invalidValues) throws Exception{ + ObjectMapper mapper = new ObjectMapper(); + // ObjectNode mainNode = mapper.createObjectNode(); + ObjectNode childNode = mapper.createObjectNode(); + ArrayNode childArray = mapper.createArrayNode(); + ObjectNode childNodeObject = mapper.createObjectNode(); + childNodeObject.put( "parent-namespace-uri", parentNamespaceUri); + childNodeObject.put( "parent-localname", parentLocalName); + childNodeObject.put( "latitude-namespace-uri", latNamespace); + childNodeObject.put( "latitude-localname", latLocalname); + childNodeObject.put( "longitude-namespace-uri", latNamespace); + childNodeObject.put( "longitude-localname", longLocalname); + childNodeObject.put( "coordinate-system", coordinateSystem); + childNodeObject.put("range-value-positions", false); + childNodeObject.put("invalid-values", invalidValues); + childArray.add(childNodeObject); + childNode.putArray("geospatial-element-pair-index").addAll(childArray); + // mainNode.put("geospatial-element-pair-indexes", childNode); + // System.out.println(type + mainNode.path("range-path-indexes").path("range-path-index").toString()); + setDatabaseProperties(dbName,"geospatial-element-pair-index",childNode); } - public static void addGeospatialElementIndexes(String dbName,String localname,String namespace,String coordinateSystem,String pointFormat,boolean rangeValuePositions,String invalidValues) throws Exception{ - ObjectMapper mapper = new ObjectMapper(); - // ObjectNode mainNode = mapper.createObjectNode(); - ObjectNode childNode = mapper.createObjectNode(); - ArrayNode childArray = mapper.createArrayNode(); - ObjectNode childNodeObject = mapper.createObjectNode(); - childNodeObject.put( "namespace-uri", namespace); - childNodeObject.put( "localname", localname); - childNodeObject.put( "coordinate-system", coordinateSystem); - childNodeObject.put("range-value-positions", false); - childNodeObject.put("invalid-values", invalidValues); - childNodeObject.put("point-format",pointFormat); - childArray.add(childNodeObject); - childNode.putArray("geospatial-element-index").addAll(childArray); -// mainNode.put("geospatial-element-indexes", childNode); -// System.out.println(type + mainNode.path("range-path-indexes").path("range-path-index").toString()); - setDatabaseProperties(dbName,"geospatial-element-index",childNode); - } - public static void addGeoSpatialElementChildIndexes(String dbName,String parentNamespaceUri,String parentLocalName,String namespace,String localname,String coordinateSystem,String pointFormat,boolean rangeValuePositions,String invalidValues) throws Exception{ - ObjectMapper mapper = new ObjectMapper(); - // ObjectNode mainNode = mapper.createObjectNode(); - ObjectNode childNode = mapper.createObjectNode(); - ArrayNode childArray = mapper.createArrayNode(); - ObjectNode childNodeObject = mapper.createObjectNode(); - childNodeObject.put( "parent-namespace-uri", parentNamespaceUri); - childNodeObject.put( "parent-localname", parentLocalName); - childNodeObject.put( "namespace-uri", namespace); - childNodeObject.put( "localname", localname); - childNodeObject.put( "coordinate-system", coordinateSystem); - childNodeObject.put("range-value-positions", false); - childNodeObject.put("invalid-values", invalidValues); - childNodeObject.put("point-format",pointFormat); - childArray.add(childNodeObject); - childNode.putArray("geospatial-element-child-index").addAll(childArray); -// mainNode.put("geospatial-element-child-indexes", childNode); -// System.out.println(type + mainNode.path("range-path-indexes").path("range-path-index").toString()); - setDatabaseProperties(dbName,"geospatial-element-child-index",childNode); - } - public static void addGeospatialElementPairIndexes(String dbName,String parentNamespaceUri,String parentLocalName,String latNamespace,String latLocalname,String longNamespace,String longLocalname,String coordinateSystem,boolean rangeValuePositions,String invalidValues) throws Exception{ - ObjectMapper mapper = new ObjectMapper(); - // ObjectNode mainNode = mapper.createObjectNode(); - ObjectNode childNode = mapper.createObjectNode(); - ArrayNode childArray = mapper.createArrayNode(); - ObjectNode childNodeObject = mapper.createObjectNode(); - childNodeObject.put( "parent-namespace-uri", parentNamespaceUri); - childNodeObject.put( "parent-localname", parentLocalName); - childNodeObject.put( "latitude-namespace-uri", latNamespace); - childNodeObject.put( "latitude-localname", latLocalname); - childNodeObject.put( "longitude-namespace-uri", latNamespace); - childNodeObject.put( "longitude-localname", longLocalname); - childNodeObject.put( "coordinate-system", coordinateSystem); - childNodeObject.put("range-value-positions", false); - childNodeObject.put("invalid-values", invalidValues); - childArray.add(childNodeObject); - childNode.putArray("geospatial-element-pair-index").addAll(childArray); -// mainNode.put("geospatial-element-pair-indexes", childNode); -// System.out.println(type + mainNode.path("range-path-indexes").path("range-path-index").toString()); - setDatabaseProperties(dbName,"geospatial-element-pair-index",childNode); - } - public static void addGeospatialElementAttributePairIndexes(String dbName,String parentNamespaceUri,String parentLocalName,String latNamespace,String latLocalname,String longNamespace,String longLocalname,String coordinateSystem,boolean rangeValuePositions,String invalidValues) throws Exception{ - ObjectMapper mapper = new ObjectMapper(); -// ObjectNode mainNode = mapper.createObjectNode(); - ObjectNode childNode = mapper.createObjectNode(); - ArrayNode childArray = mapper.createArrayNode(); - ObjectNode childNodeObject = mapper.createObjectNode(); - childNodeObject.put( "parent-namespace-uri", parentNamespaceUri); - childNodeObject.put( "parent-localname", parentLocalName); - childNodeObject.put( "latitude-namespace-uri", latNamespace); - childNodeObject.put( "latitude-localname", latLocalname); - childNodeObject.put( "longitude-namespace-uri", latNamespace); - childNodeObject.put( "longitude-localname", longLocalname); - childNodeObject.put( "coordinate-system", coordinateSystem); - childNodeObject.put("range-value-positions", false); - childNodeObject.put("invalid-values", invalidValues); - childArray.add(childNodeObject); - childNode.putArray("geospatial-element-attribute-pair-index").addAll(childArray); -// mainNode.put("geospatial-element-attribute-pair-indexes", childNode); -// System.out.println(type + mainNode.path("range-path-indexes").path("range-path-index").toString()); - setDatabaseProperties(dbName,"geospatial-element-attribute-pair-index",childNode); - } - public static void addGeospatialPathIndexes(String dbName,String pathExpression,String coordinateSystem,String pointFormat,boolean rangeValuePositions,String invalidValues) throws Exception{ - ObjectMapper mapper = new ObjectMapper(); + public static void addGeospatialElementAttributePairIndexes(String dbName,String parentNamespaceUri,String parentLocalName,String latNamespace,String latLocalname,String longNamespace,String longLocalname,String coordinateSystem,boolean rangeValuePositions,String invalidValues) throws Exception{ + ObjectMapper mapper = new ObjectMapper(); + // ObjectNode mainNode = mapper.createObjectNode(); + ObjectNode childNode = mapper.createObjectNode(); + ArrayNode childArray = mapper.createArrayNode(); + ObjectNode childNodeObject = mapper.createObjectNode(); + childNodeObject.put( "parent-namespace-uri", parentNamespaceUri); + childNodeObject.put( "parent-localname", parentLocalName); + childNodeObject.put( "latitude-namespace-uri", latNamespace); + childNodeObject.put( "latitude-localname", latLocalname); + childNodeObject.put( "longitude-namespace-uri", latNamespace); + childNodeObject.put( "longitude-localname", longLocalname); + childNodeObject.put( "coordinate-system", coordinateSystem); + childNodeObject.put("range-value-positions", false); + childNodeObject.put("invalid-values", invalidValues); + childArray.add(childNodeObject); + childNode.putArray("geospatial-element-attribute-pair-index").addAll(childArray); + // mainNode.put("geospatial-element-attribute-pair-indexes", childNode); + // System.out.println(type + mainNode.path("range-path-indexes").path("range-path-index").toString()); + setDatabaseProperties(dbName,"geospatial-element-attribute-pair-index",childNode); + } + public static void addGeospatialPathIndexes(String dbName,String pathExpression,String coordinateSystem,String pointFormat,boolean rangeValuePositions,String invalidValues) throws Exception{ + ObjectMapper mapper = new ObjectMapper(); // ObjectNode mainNode = mapper.createObjectNode(); - ObjectNode childNode = mapper.createObjectNode(); - ArrayNode childArray = mapper.createArrayNode(); - ObjectNode childNodeObject = mapper.createObjectNode(); - childNodeObject.put( "path-expression", pathExpression); - childNodeObject.put( "coordinate-system", coordinateSystem); - childNodeObject.put("range-value-positions", false); - childNodeObject.put("invalid-values", invalidValues); - childNodeObject.put("point-format",pointFormat); - childArray.add(childNodeObject); - childNode.putArray("geospatial-path-index").addAll(childArray); - // mainNode.put("geospatial-path-indexes", childNode); -// System.out.println(type + mainNode.path("range-path-indexes").path("range-path-index").toString()); - setDatabaseProperties(dbName,"geospatial-path-index",childNode); - } + ObjectNode childNode = mapper.createObjectNode(); + ArrayNode childArray = mapper.createArrayNode(); + ObjectNode childNodeObject = mapper.createObjectNode(); + childNodeObject.put( "path-expression", pathExpression); + childNodeObject.put( "coordinate-system", coordinateSystem); + childNodeObject.put("range-value-positions", false); + childNodeObject.put("invalid-values", invalidValues); + childNodeObject.put("point-format",pointFormat); + childArray.add(childNodeObject); + childNode.putArray("geospatial-path-index").addAll(childArray); + // mainNode.put("geospatial-path-indexes", childNode); + // System.out.println(type + mainNode.path("range-path-indexes").path("range-path-index").toString()); + setDatabaseProperties(dbName,"geospatial-path-index",childNode); + } /* * Add field will include root and it appends field to an existing fields * "fields":{ @@ -1062,200 +1083,200 @@ public static void addGeospatialPathIndexes(String dbName,String pathExpression, ] } */ - public static void addField(String dbName, String fieldName) throws Exception{ - ObjectMapper mapper = new ObjectMapper(); + public static void addField(String dbName, String fieldName) throws Exception{ + ObjectMapper mapper = new ObjectMapper(); // ObjectNode mainNode = mapper.createObjectNode(); - ObjectNode childNode = mapper.createObjectNode(); - ArrayNode arrNode = mapper.createArrayNode(); - ObjectNode childNodeObject = mapper.createObjectNode(); - childNodeObject.put( "field-name", fieldName); - childNodeObject.put("field-type", "root"); - childNodeObject.put( "include-root", true); - childNodeObject.putNull( "included-elements"); - childNodeObject.putNull( "excluded-elements"); - childNodeObject.putNull( "tokenizer-overrides"); - arrNode.add(childNodeObject); - childNode.putArray("field").addAll(arrNode); - // mainNode.put("fields", childNode); -// System.out.println("Entered field to make it true"); - setDatabaseProperties(dbName,"field",childNode); - - } - public static void addFieldExcludeRoot(String dbName, String fieldName) throws Exception{ - ObjectMapper mapper = new ObjectMapper(); -// ObjectNode mainNode = mapper.createObjectNode(); - ObjectNode childNode = mapper.createObjectNode(); - ArrayNode arrNode = mapper.createArrayNode(); - ObjectNode childNodeObject = mapper.createObjectNode(); - childNodeObject.put( "field-name", fieldName); - childNodeObject.put( "include-root", false); - childNodeObject.putNull( "included-elements"); - childNodeObject.putNull( "excluded-elements"); - childNodeObject.putNull( "tokenizer-overrides"); - arrNode.add(childNodeObject); - childNode.putArray("field").addAll(arrNode); -// mainNode.put("fields", childNode); -// System.out.println( childNode.toString()); - setDatabaseProperties(dbName,"field",childNode); - - } - public static void addBuiltInGeoIndex (String dbName)throws Exception { - addGeospatialElementIndexes(dbName,"g-elem-point","","wgs84","point",false,"reject"); - addGeoSpatialElementChildIndexes(dbName,"","g-elem-child-parent","","g-elem-child-point","wgs84","point",false,"reject"); - addGeospatialElementPairIndexes(dbName,"","g-elem-pair","","lat","","long","wgs84",false,"reject"); - addGeospatialElementAttributePairIndexes(dbName,"","g-attr-pair","","lat","","long","wgs84",false,"reject"); - addGeospatialPathIndexes(dbName,"/doc/g-elem-point","wgs84","point",false,"ignore"); - } - -/* + ObjectNode childNode = mapper.createObjectNode(); + ArrayNode arrNode = mapper.createArrayNode(); + ObjectNode childNodeObject = mapper.createObjectNode(); + childNodeObject.put( "field-name", fieldName); + childNodeObject.put("field-type", "root"); + childNodeObject.put( "include-root", true); + childNodeObject.putNull( "included-elements"); + childNodeObject.putNull( "excluded-elements"); + childNodeObject.putNull( "tokenizer-overrides"); + arrNode.add(childNodeObject); + childNode.putArray("field").addAll(arrNode); + // mainNode.put("fields", childNode); + // System.out.println("Entered field to make it true"); + setDatabaseProperties(dbName,"field",childNode); + + } + public static void addFieldExcludeRoot(String dbName, String fieldName) throws Exception{ + ObjectMapper mapper = new ObjectMapper(); + // ObjectNode mainNode = mapper.createObjectNode(); + ObjectNode childNode = mapper.createObjectNode(); + ArrayNode arrNode = mapper.createArrayNode(); + ObjectNode childNodeObject = mapper.createObjectNode(); + childNodeObject.put( "field-name", fieldName); + childNodeObject.put( "include-root", false); + childNodeObject.putNull( "included-elements"); + childNodeObject.putNull( "excluded-elements"); + childNodeObject.putNull( "tokenizer-overrides"); + arrNode.add(childNodeObject); + childNode.putArray("field").addAll(arrNode); + // mainNode.put("fields", childNode); + // System.out.println( childNode.toString()); + setDatabaseProperties(dbName,"field",childNode); + + } + public static void addBuiltInGeoIndex (String dbName)throws Exception { + addGeospatialElementIndexes(dbName,"g-elem-point","","wgs84","point",false,"reject"); + addGeoSpatialElementChildIndexes(dbName,"","g-elem-child-parent","","g-elem-child-point","wgs84","point",false,"reject"); + addGeospatialElementPairIndexes(dbName,"","g-elem-pair","","lat","","long","wgs84",false,"reject"); + addGeospatialElementAttributePairIndexes(dbName,"","g-attr-pair","","lat","","long","wgs84",false,"reject"); + addGeospatialPathIndexes(dbName,"/doc/g-elem-point","wgs84","point",false,"ignore"); + } + + /* This method is trying to add include element or exclude elements to the existing fields - * - */ - public static void setDatabaseFieldProperties(String dbName,String field_name, String propName, ObjectNode objNode ) throws IOException{ - InputStream jsonstream=null; - try{ - DefaultHttpClient client = new DefaultHttpClient(); - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); - HttpGet getrequest = new HttpGet("http://localhost:8002"+ "/manage/v2/databases/"+dbName+"/properties?format=json"); - HttpResponse response1 = client.execute(getrequest); - jsonstream =response1.getEntity().getContent(); - ObjectMapper mapper = new ObjectMapper(); - JsonNode jnode= mapper.readTree(jsonstream); - if(!jnode.isNull()&& jnode.has("field")){ - JsonNode fieldNode = jnode.withArray("field"); - Iterator fnode = fieldNode.elements(); - while(fnode.hasNext()) { - JsonNode fnchild =fnode.next(); - if((fnchild.path("field-name").asText()).equals(field_name)){ -// System.out.println("Hurray" +fnchild.has(propName)); - if(!fnchild.has(propName)){ - ((ObjectNode)fnchild).putArray(propName).addAll(objNode.withArray(propName)); - System.out.println("Adding child array include node" + jnode.toString()); - } - else{ - JsonNode member = fnchild.withArray(propName); - ((ArrayNode)member).addAll(objNode.withArray(propName)); - } - - } - } - - HttpPut put = new HttpPut("http://localhost:8002"+ "/manage/v2/databases/"+dbName+"/properties?format=json"); - put.addHeader("Content-type", "application/json"); - put.setEntity(new StringEntity(jnode.toString())); - - HttpResponse response2 = client.execute(put); - HttpEntity respEntity = response2.getEntity(); - if(respEntity != null){ - String content = EntityUtils.toString(respEntity); - System.out.println(content); - } - } - else{ - System.out.println("REST call for database properties returned NULL \n"+jnode.toString()+"\n"+ response1.getStatusLine().getStatusCode()); - } - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); - } - finally{ - if(jsonstream == null){} - else{ - jsonstream.close(); + * + */ + public static void setDatabaseFieldProperties(String dbName,String field_name, String propName, ObjectNode objNode ) throws IOException{ + InputStream jsonstream=null; + try{ + DefaultHttpClient client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + HttpGet getrequest = new HttpGet("http://localhost:8002"+ "/manage/v2/databases/"+dbName+"/properties?format=json"); + HttpResponse response1 = client.execute(getrequest); + jsonstream =response1.getEntity().getContent(); + ObjectMapper mapper = new ObjectMapper(); + JsonNode jnode= mapper.readTree(jsonstream); + if(!jnode.isNull()&& jnode.has("field")){ + JsonNode fieldNode = jnode.withArray("field"); + Iterator fnode = fieldNode.elements(); + while(fnode.hasNext()) { + JsonNode fnchild =fnode.next(); + if((fnchild.path("field-name").asText()).equals(field_name)){ + // System.out.println("Hurray" +fnchild.has(propName)); + if(!fnchild.has(propName)){ + ((ObjectNode)fnchild).putArray(propName).addAll(objNode.withArray(propName)); + System.out.println("Adding child array include node" + jnode.toString()); + } + else{ + JsonNode member = fnchild.withArray(propName); + ((ArrayNode)member).addAll(objNode.withArray(propName)); + } + + } } + + HttpPut put = new HttpPut("http://localhost:8002"+ "/manage/v2/databases/"+dbName+"/properties?format=json"); + put.addHeader("Content-type", "application/json"); + put.setEntity(new StringEntity(jnode.toString())); + + HttpResponse response2 = client.execute(put); + HttpEntity respEntity = response2.getEntity(); + if(respEntity != null){ + String content = EntityUtils.toString(respEntity); + System.out.println(content); } } + else{ + System.out.println("REST call for database properties returned NULL \n"+jnode.toString()+"\n"+ response1.getStatusLine().getStatusCode()); + } + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } + finally{ + if(jsonstream == null){} + else{ + jsonstream.close(); + } + } + } - public static void includeElementField(String dbName, String field_name, String namespace, String elementName) throws Exception{ - ObjectMapper mapper = new ObjectMapper(); + public static void includeElementField(String dbName, String field_name, String namespace, String elementName) throws Exception{ + ObjectMapper mapper = new ObjectMapper(); // ObjectNode mainNode = mapper.createObjectNode(); - ObjectNode childNode = mapper.createObjectNode(); - ArrayNode arrNode = mapper.createArrayNode(); - ObjectNode childNodeObject = mapper.createObjectNode(); - childNodeObject.put( "namespace-uri", namespace); - childNodeObject.put( "localname", elementName); - childNodeObject.put("weight", 1.0); - arrNode.add(childNodeObject); - childNode.putArray("included-element").addAll(arrNode); + ObjectNode childNode = mapper.createObjectNode(); + ArrayNode arrNode = mapper.createArrayNode(); + ObjectNode childNodeObject = mapper.createObjectNode(); + childNodeObject.put( "namespace-uri", namespace); + childNodeObject.put( "localname", elementName); + childNodeObject.put("weight", 1.0); + arrNode.add(childNodeObject); + childNode.putArray("included-element").addAll(arrNode); // mainNode.put("included-elements", childNode); - System.out.println( childNode.toString()); - setDatabaseFieldProperties(dbName,field_name,"included-element",childNode); - - } - public static void includeElementFieldWithWeight(String dbName, String field_name, String namespace, String elementName, double weight) throws Exception{ - - ObjectMapper mapper = new ObjectMapper(); -// ObjectNode mainNode = mapper.createObjectNode(); - ObjectNode childNode = mapper.createObjectNode(); - ArrayNode arrNode = mapper.createArrayNode(); - ObjectNode childNodeObject = mapper.createObjectNode(); - childNodeObject.put( "namespace-uri", namespace); - childNodeObject.put( "localname", elementName); - childNodeObject.put("weight", weight); - arrNode.add(childNodeObject); - childNode.putArray("included-element").addAll(arrNode); -// mainNode.put("included-elements", childNode); -// System.out.println( childNode.toString()); - setDatabaseFieldProperties(dbName,field_name,"included-element",childNode); - - } - public static void setupAppServicesConstraint(String dbName) throws Exception { + System.out.println( childNode.toString()); + setDatabaseFieldProperties(dbName,field_name,"included-element",childNode); + + } + public static void includeElementFieldWithWeight(String dbName, String field_name, String namespace, String elementName, double weight) throws Exception{ + + ObjectMapper mapper = new ObjectMapper(); + // ObjectNode mainNode = mapper.createObjectNode(); + ObjectNode childNode = mapper.createObjectNode(); + ArrayNode arrNode = mapper.createArrayNode(); + ObjectNode childNodeObject = mapper.createObjectNode(); + childNodeObject.put( "namespace-uri", namespace); + childNodeObject.put( "localname", elementName); + childNodeObject.put("weight", weight); + arrNode.add(childNodeObject); + childNode.putArray("included-element").addAll(arrNode); + // mainNode.put("included-elements", childNode); + // System.out.println( childNode.toString()); + setDatabaseFieldProperties(dbName,field_name,"included-element",childNode); + + } + public static void setupAppServicesConstraint(String dbName) throws Exception { enableCollectionLexicon(dbName); - enableWordLexicon(dbName); - addRangeElementIndex(dbName, "date", "http://purl.org/dc/elements/1.1/", "date"); - addRangeElementIndex(dbName, "int", "", "popularity"); - addRangeElementIndex(dbName, "int", "http://test.tups.com", "rate"); - addRangeElementIndex(dbName, "decimal", "http://test.aggr.com", "score"); - addRangeElementIndex(dbName, "string", "", "title", "http://marklogic.com/collation/"); - addRangeElementAttributeIndex(dbName, "decimal", "http://cloudbank.com", "price", "", "amt", "http://marklogic.com/collation/"); - enableTrailingWildcardSearches(dbName); - addFieldExcludeRoot(dbName, "para"); - includeElementFieldWithWeight(dbName, "para", "", "p", 5); - addRangePathIndex(dbName, "string", "/Employee/fn", "http://marklogic.com/collation/", "ignore"); - addRangePathIndex(dbName, "int", "/root/popularity", "", "ignore"); - addRangePathIndex(dbName, "decimal", "//@amt", "", "ignore"); - } - public static void setupAppServicesGeoConstraint(String dbName) throws Exception { - enableCollectionLexicon(dbName); - addRangeElementIndex(dbName, "dateTime", "", "bday", "http://marklogic.com/collation/"); - addRangeElementIndex(dbName, "int", "", "height1", "http://marklogic.com/collation/"); - addRangeElementIndex(dbName, "int", "", "height2", "http://marklogic.com/collation/"); - addRangePathIndex(dbName, "string", "/doc/name", "http://marklogic.com/collation/", "ignore"); - addField(dbName, "description"); - includeElementField(dbName, "description", "", "description"); - addBuiltInGeoIndex(dbName); - - } - public static void loadBug18993(){ - try{ - DefaultHttpClient client = new DefaultHttpClient(); - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8011), - new UsernamePasswordCredentials("admin", "admin")); - String document ="a space b"; - String perm = "perm:rest-writer=read&perm:rest-writer=insert&perm:rest-writer=update&perm:rest-writer=execute"; - HttpPut put = new HttpPut("http://localhost:8011/v1/documents?uri=/a%20b&"+perm); - put.addHeader("Content-type", "application/xml"); - put.setEntity(new StringEntity(document)); - HttpResponse response = client.execute(put); - HttpEntity respEntity = response.getEntity(); - if(respEntity != null){ - String content = EntityUtils.toString(respEntity); - System.out.println(content); - } - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); + enableWordLexicon(dbName); + addRangeElementIndex(dbName, "date", "http://purl.org/dc/elements/1.1/", "date"); + addRangeElementIndex(dbName, "int", "", "popularity"); + addRangeElementIndex(dbName, "int", "http://test.tups.com", "rate"); + addRangeElementIndex(dbName, "decimal", "http://test.aggr.com", "score"); + addRangeElementIndex(dbName, "string", "", "title", "http://marklogic.com/collation/"); + addRangeElementAttributeIndex(dbName, "decimal", "http://cloudbank.com", "price", "", "amt", "http://marklogic.com/collation/"); + enableTrailingWildcardSearches(dbName); + addFieldExcludeRoot(dbName, "para"); + includeElementFieldWithWeight(dbName, "para", "", "p", 5); + addRangePathIndex(dbName, "string", "/Employee/fn", "http://marklogic.com/collation/", "ignore"); + addRangePathIndex(dbName, "int", "/root/popularity", "", "ignore"); + addRangePathIndex(dbName, "decimal", "//@amt", "", "ignore"); + } + public static void setupAppServicesGeoConstraint(String dbName) throws Exception { + enableCollectionLexicon(dbName); + addRangeElementIndex(dbName, "dateTime", "", "bday", "http://marklogic.com/collation/"); + addRangeElementIndex(dbName, "int", "", "height1", "http://marklogic.com/collation/"); + addRangeElementIndex(dbName, "int", "", "height2", "http://marklogic.com/collation/"); + addRangePathIndex(dbName, "string", "/doc/name", "http://marklogic.com/collation/", "ignore"); + addField(dbName, "description"); + includeElementField(dbName, "description", "", "description"); + addBuiltInGeoIndex(dbName); + + } + public static void loadBug18993(){ + try{ + DefaultHttpClient client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8011), + new UsernamePasswordCredentials("admin", "admin")); + String document ="a space b"; + String perm = "perm:rest-writer=read&perm:rest-writer=insert&perm:rest-writer=update&perm:rest-writer=execute"; + HttpPut put = new HttpPut("http://localhost:8011/v1/documents?uri=/a%20b&"+perm); + put.addHeader("Content-type", "application/xml"); + put.setEntity(new StringEntity(document)); + HttpResponse response = client.execute(put); + HttpEntity respEntity = response.getEntity(); + if(respEntity != null){ + String content = EntityUtils.toString(respEntity); + System.out.println(content); } - - } - - public static void setAuthentication(String level){ - - } - public static void setDefaultUser(String usr){ - - } + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } + + } + + public static void setAuthentication(String level){ + + } + public static void setDefaultUser(String usr){ + + } } \ No newline at end of file diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestAggregates.java b/test-complete/src/test/java/com/marklogic/javaclient/TestAggregates.java index 4fc9f7b50..675fd760f 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestAggregates.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestAggregates.java @@ -31,7 +31,7 @@ public class TestAggregates extends BasicJavaClientREST { private static String dbName = "TestAggregatesDB"; private static String [] fNames = {"TestAggregatesDB-1"}; - private static String restServerName = "REST-Java-Client-API-TestAggregateServer"; + private static String restServerName = "REST-Java-Client-API-Server"; private static int restPort = 8011; @BeforeClass diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBug21159.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBug21159.java index 25bce19e5..516229255 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBug21159.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBug21159.java @@ -74,7 +74,7 @@ public void testBug21159Tuples() throws IOException, ParserConfigurationExceptio } // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/LexiconOptions.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/LexiconOptions.xml"); String combinedQuery = convertFileToString(file); @@ -125,7 +125,7 @@ public void testBug21159Values() throws IOException, ParserConfigurationExceptio } // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/LexiconOptions.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/LexiconOptions.xml"); String combinedQuery = convertFileToString(file); diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadSample1.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadSample1.java index f350e42a7..5ac725ae6 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadSample1.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadSample1.java @@ -59,7 +59,7 @@ public class TestBulkReadSample1 extends BasicJavaClientREST { private static final String DIRECTORY ="/bulkread/"; private static String dbName = "TestBulkReadSampleDB"; private static String [] fNames = {"TestBulkReadSampleDB-1"}; - private static String restServerName = "TestBulkReadSample-RESTServer"; + private static String restServerName = "REST-Java-Client-API-Server"; private static int restPort = 8011; private DatabaseClient client ; @BeforeClass diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata1.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata1.java index 47806ae32..79f95eea0 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata1.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata1.java @@ -64,7 +64,7 @@ public class TestBulkWriteMetadata1 extends BasicJavaClientREST { private static String dbName = "TestBulkWriteDefaultMetadataDB"; private static String [] fNames = {"TestBulkWriteDefaultMetadataDB-1"}; - private static String restServerName = "TestBulkWriteDefaultMetadata-RESTServer"; + private static String restServerName = "REST-Java-Client-API-Server"; private static int restPort = 8011; private DatabaseClient client ; /** diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata2.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata2.java index f03215454..70b229221 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata2.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata2.java @@ -65,7 +65,7 @@ public class TestBulkWriteMetadata2 extends BasicJavaClientREST{ private static String dbName = "TestBulkWriteDefaultMetadataDB2"; private static String [] fNames = {"TestBulkWriteDefaultMetadataDB-2"}; - private static String restServerName = "TestBulkWriteDefaultMetadata2-RESTServer"; + private static String restServerName = "REST-Java-Client-API-Server"; private static int restPort = 8011; private DatabaseClient client ; /** diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadatawithRawXML.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadatawithRawXML.java index 25248c415..dcfae9128 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadatawithRawXML.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadatawithRawXML.java @@ -69,7 +69,7 @@ public class TestBulkWriteMetadatawithRawXML extends BasicJavaClientREST{ private static String dbName = "TestBulkWriteDefaultMetadataDB3"; private static String [] fNames = {"TestBulkWriteDefaultMetadataDB-3"}; - private static String restServerName = "TestBulkWriteDefaultMetadata3-RESTServer"; + private static String restServerName = "REST-Java-Client-API-Server"; private static int restPort = 8011; private DatabaseClient client ; /** @@ -80,7 +80,7 @@ public static void setUpBeforeClass() throws Exception { System.out.println("In Setup"); setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); createRESTUser("app-user", "password","rest-writer","rest-reader" ); - createRESTUserWithPermissions("usr1", "password",getPermissionNode("eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); + createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); } /** @@ -140,8 +140,8 @@ public void validateMetadata(DocumentMetadataHandle mh){ assertTrue("Document properties count", result); // Permissions - String expectedPermissions1 = "size:4|rest-reader:[READ]|eval:[READ]|app-user:[UPDATE, READ]|rest-writer:[UPDATE]|"; - String expectedPermissions2 = "size:4|rest-reader:[READ]|eval:[READ]|app-user:[READ, UPDATE]|rest-writer:[UPDATE]|"; + String expectedPermissions1 = "size:4|flexrep-eval:[READ]|rest-reader:[READ]|app-user:[UPDATE, READ]|rest-writer:[UPDATE]|"; + String expectedPermissions2 = "size:4|flexrep-eval:[READ]|rest-reader:[READ]|app-user:[READ, UPDATE]|rest-writer:[UPDATE]|"; String actualPermissions = getDocumentPermissionsString(permissions); System.out.println(actualPermissions); if(actualPermissions.contains("[UPDATE, READ]")) @@ -166,7 +166,7 @@ public void validateDefaultMetadata(DocumentMetadataHandle mh){ // Permissions - String expectedPermissions1 = "size:3|rest-reader:[READ]|eval:[READ]|rest-writer:[UPDATE]|"; + String expectedPermissions1 = "size:3|flexrep-eval:[READ]|rest-reader:[READ]|rest-writer:[UPDATE]|"; String actualPermissions = getDocumentPermissionsString(permissions); assertEquals("Document permissions difference", expectedPermissions1, actualPermissions); @@ -488,7 +488,8 @@ public void testWriteGenericDocMgrWithDefaultMetadata() throws Exception docMgr.readMetadata(rec.getUri(), mh1); // until issue 24 is fixed //this.validateDefaultMetadata(mh1); - + assertEquals("default quality",0,mh1.getQuality()); + assertEquals("default permissions","{flexrep-eval=[READ], rest-reader=[READ], rest-writer=[UPDATE]}",mh1.getPermissions().toString()); page = docMgr.read("/generic/foo.xml"); rec = page.next(); docMgr.readMetadata(rec.getUri(), mh1); diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteWithTransformations.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteWithTransformations.java index bdb0d8a00..176833685 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteWithTransformations.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteWithTransformations.java @@ -48,14 +48,14 @@ public class TestBulkWriteWithTransformations extends BasicJavaClientREST{ @BeforeClass public static void setUpBeforeClass() throws Exception { System.out.println("In setup"); -// setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); -// setupAppServicesConstraint(dbName); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + setupAppServicesConstraint(dbName); } @AfterClass public static void tearDownAfterClass() throws Exception { System.out.println("In tear down" ); -// tearDownJavaRESTServer(dbName, fNames, restServerName); + tearDownJavaRESTServer(dbName, fNames, restServerName); } @Before diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestCRUDModulesDb.java b/test-complete/src/test/java/com/marklogic/javaclient/TestCRUDModulesDb.java index 5a65b0c98..274086d3e 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestCRUDModulesDb.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestCRUDModulesDb.java @@ -13,6 +13,7 @@ import com.marklogic.client.io.Format; import com.marklogic.client.io.StringHandle; import com.marklogic.client.DatabaseClient; + import org.junit.*; public class TestCRUDModulesDb extends BasicJavaClientREST { @@ -26,7 +27,12 @@ public static void setUp() throws Exception assocRESTServer(restServerName, dbName,8011); } -@SuppressWarnings("deprecation") +@After +public void testCleanUp() throws Exception +{ + clearDB(8011); + System.out.println("Running clear script"); +} @Test public void testXQueryModuleCRUDDuplicateFile() { @@ -72,7 +78,7 @@ public void testXQueryModuleCRUDDuplicateFile() } -@SuppressWarnings("deprecation") + @Test public void testXQueryModuleCRUDDifferentPath() { DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); @@ -115,7 +121,7 @@ public void testXQueryModuleCRUDDuplicateFile() } -@SuppressWarnings("deprecation") + @Test public void testXQueryModuleCRUDBinaryFile() { @@ -153,7 +159,7 @@ public void testXQueryModuleCRUDBinaryFile() { } -@SuppressWarnings("deprecation") + @Test public void testXQueryModuleCRUDTextFile() { DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); @@ -190,7 +196,7 @@ public void testXQueryModuleCRUDBinaryFile() { } -@SuppressWarnings("deprecation") + @Test public void testXQueryModuleCRUDXmlFile() { DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); @@ -227,7 +233,7 @@ public void testXQueryModuleCRUDBinaryFile() { } -@SuppressWarnings("deprecation") + @Test public void testXQueryModuleReadModulesDb() { DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); @@ -273,7 +279,6 @@ public void testXQueryModuleCRUDBinaryFile() { } -@SuppressWarnings("deprecation") @Test public void testXQueryModuleReadExtensionLibraryDescriptor () { System.out.println("testXQueryModuleReadExtensionLibraryDescriptor"); @@ -324,7 +329,6 @@ public void testXQueryModuleReadExtensionLibraryDescriptor () { } -@SuppressWarnings("deprecation") @Test public void testXQueryModuleCRUDXmlFileNegative() { diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryByExample.java b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryByExample.java index 6e52f3bc7..1c01fa3d0 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryByExample.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryByExample.java @@ -60,7 +60,7 @@ public void testQueryByExampleXML() throws IOException, TransformerException, Xp } // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/qbe/qbe1.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/qbe/qbe1.xml"); String qbeQuery = convertFileToString(file); StringHandle qbeHandle = new StringHandle(qbeQuery); @@ -97,7 +97,7 @@ public void testQueryByExampleXML() throws IOException, TransformerException, Xp } // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/qbe/qbe1.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/qbe/qbe1.xml"); String qbeQuery = convertFileToString(file); StringHandle qbeHandle = new StringHandle(qbeQuery); @@ -134,7 +134,7 @@ public void testQueryByExampleXML() throws IOException, TransformerException, Xp } // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/qbe/qbe1.json"); + File file = new File("src/test/java/com/marklogic/javaclient/qbe/qbe1.json"); String qbeQuery = convertFileToString(file); StringHandle qbeHandle = new StringHandle(qbeQuery); @@ -201,7 +201,7 @@ public void testQueryByExampleXML() throws IOException, TransformerException, Xp } // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/qbe/qbe1.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/qbe/qbe1.xml"); FileHandle fileHandle = new FileHandle(file); QueryManager queryMgr = client.newQueryManager(); @@ -234,7 +234,7 @@ public void testQueryByExampleXML() throws IOException, TransformerException, Xp } // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/qbe/qbe1.json"); + File file = new File("src/test/java/com/marklogic/javaclient/qbe/qbe1.json"); FileHandle fileHandle = new FileHandle(file); QueryManager queryMgr = client.newQueryManager(); @@ -266,7 +266,7 @@ public void testQueryByExampleXML() throws IOException, TransformerException, Xp // get the combined query try{ - File file = new File("src/junit/com/marklogic/javaclient/qbe/qbe2.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/qbe/qbe2.xml"); FileHandle fileHandle = new FileHandle(file); QueryManager queryMgr = client.newQueryManager(); @@ -301,7 +301,7 @@ public void testQueryByExampleXML() throws IOException, TransformerException, Xp // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/qbe/qbe1.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/qbe/qbe1.xml"); FileHandle fileHandle = new FileHandle(file); QueryManager queryMgr = client.newQueryManager(); @@ -334,7 +334,7 @@ public void testQueryByExampleXML() throws IOException, TransformerException, Xp writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "JSON"); // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/qbe/qbe1.json"); + File file = new File("src/test/java/com/marklogic/javaclient/qbe/qbe1.json"); String qbeQuery = convertFileToString(file); StringHandle qbeHandle = new StringHandle(qbeQuery); @@ -373,7 +373,7 @@ public void testQueryByExampleXML() throws IOException, TransformerException, Xp } // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/qbe/qbe1.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/qbe/qbe1.xml"); FileHandle fileHandle = new FileHandle(file); QueryManager queryMgr = client.newQueryManager(); @@ -386,7 +386,7 @@ public void testQueryByExampleXML() throws IOException, TransformerException, Xp assertEquals("Wrong Document Searched",result.getUri() , "/qbe/constraint1.xml"); } try{ - File wrongFile = new File("src/junit/com/marklogic/javaclient/qbe/WrongQbe.xml"); + File wrongFile = new File("src/test/java/com/marklogic/javaclient/qbe/WrongQbe.xml"); FileHandle wrongFileHandle = new FileHandle(wrongFile); QueryManager newQueryMgr = client.newQueryManager(); @@ -421,7 +421,7 @@ public void testQueryByExampleXML() throws IOException, TransformerException, Xp } // get the Correct query - File file = new File("src/junit/com/marklogic/javaclient/qbe/qbe1.json"); + File file = new File("src/test/java/com/marklogic/javaclient/qbe/qbe1.json"); String qbeQuery = convertFileToString(file); StringHandle qbeHandle = new StringHandle(qbeQuery); @@ -440,7 +440,7 @@ public void testQueryByExampleXML() throws IOException, TransformerException, Xp // get the query with Wrong Format - File wrongFile = new File("src/junit/com/marklogic/javaclient/qbe/WrongQbe.json"); + File wrongFile = new File("src/test/java/com/marklogic/javaclient/qbe/WrongQbe.json"); String wrongQbeQuery = convertFileToString(wrongFile); StringHandle newQbeHandle = new StringHandle(wrongQbeQuery); diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionsHandle.java b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionsHandle.java index f5a9787e0..b4d6601e2 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionsHandle.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionsHandle.java @@ -66,7 +66,7 @@ public class TestQueryOptionsHandle extends BasicJavaClientREST { ReaderHandle handle = new ReaderHandle(); // write the files - BufferedReader docStream = new BufferedReader(new FileReader("src/junit/com/marklogic/javaclient/queryoptions/" + queryOptionName)); + BufferedReader docStream = new BufferedReader(new FileReader("src/test/java/com/marklogic/javaclient/queryoptions/" + queryOptionName)); handle.set(docStream); // write the query options to the database @@ -136,7 +136,7 @@ public class TestQueryOptionsHandle extends BasicJavaClientREST { ReaderHandle handle = new ReaderHandle(); // write the files - BufferedReader docStream = new BufferedReader(new FileReader("src/junit/com/marklogic/javaclient/queryoptions/" + queryOptionName)); + BufferedReader docStream = new BufferedReader(new FileReader("src/test/java/com/marklogic/javaclient/queryoptions/" + queryOptionName)); handle.set(docStream); // write the query options to the database @@ -207,11 +207,11 @@ public class TestQueryOptionsHandle extends BasicJavaClientREST { } // create handle - FileHandle handle = new FileHandle(new File("src/junit/com/marklogic/javaclient/queryoptions/" + queryOptionName)); + FileHandle handle = new FileHandle(new File("src/test/java/com/marklogic/javaclient/queryoptions/" + queryOptionName)); handle.setFormat(Format.JSON); // write the files - //BufferedReader docStream = new BufferedReader(new FileReader("src/junit/com/marklogic/javaclient/queryoptions/" + queryOptionName)); + //BufferedReader docStream = new BufferedReader(new FileReader("src/test/java/com/marklogic/javaclient/queryoptions/" + queryOptionName)); //handle.set(docStream); @@ -280,7 +280,7 @@ public class TestQueryOptionsHandle extends BasicJavaClientREST { QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); // create handle - FileHandle handle = new FileHandle(new File("src/junit/com/marklogic/javaclient/queryoptions/" + queryOptionName)); + FileHandle handle = new FileHandle(new File("src/test/java/com/marklogic/javaclient/queryoptions/" + queryOptionName)); handle.setFormat(Format.JSON); // write the query options to the database diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestRawAlert.java b/test-complete/src/test/java/com/marklogic/javaclient/TestRawAlert.java index 6b01ebb9f..a84848a05 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestRawAlert.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestRawAlert.java @@ -66,7 +66,7 @@ public class TestRawAlert extends BasicJavaClientREST { RuleManager ruleMgr = client.newRuleManager(); // get the rule - File file = new File("src/junit/com/marklogic/javaclient/rules/alertRule1.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/rules/alertRule1.xml"); // create a handle for the rule FileHandle writeHandle = new FileHandle(file); @@ -134,7 +134,7 @@ public class TestRawAlert extends BasicJavaClientREST { RuleManager ruleMgr = client.newRuleManager(); // get the rule - File file = new File("src/junit/com/marklogic/javaclient/rules/alertRule1.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/rules/alertRule1.xml"); // create a handle for the rule FileHandle writeHandle = new FileHandle(file); @@ -188,8 +188,8 @@ public class TestRawAlert extends BasicJavaClientREST { InputStreamHandle ruleHandle2 = new InputStreamHandle(); // get the rule file - InputStream inputStream1 = new FileInputStream("src/junit/com/marklogic/javaclient/rules/alertRule1.xml"); - InputStream inputStream2 = new FileInputStream("src/junit/com/marklogic/javaclient/rules/alertRule2.xml"); + InputStream inputStream1 = new FileInputStream("src/test/java/com/marklogic/javaclient/rules/alertRule1.xml"); + InputStream inputStream2 = new FileInputStream("src/test/java/com/marklogic/javaclient/rules/alertRule2.xml"); ruleHandle1.set(inputStream1); ruleHandle2.set(inputStream2); @@ -259,7 +259,7 @@ public class TestRawAlert extends BasicJavaClientREST { InputStreamHandle ruleHandle1 = new InputStreamHandle(); // get the rule file - InputStream inputStream1 = new FileInputStream("src/junit/com/marklogic/javaclient/rules/alertRule1.xml"); + InputStream inputStream1 = new FileInputStream("src/test/java/com/marklogic/javaclient/rules/alertRule1.xml"); ruleHandle1.set(inputStream1); @@ -303,7 +303,7 @@ public class TestRawAlert extends BasicJavaClientREST { RuleManager ruleMgr = client.newRuleManager(); // get the rule - File file = new File("src/junit/com/marklogic/javaclient/rules/alertRule1.json"); + File file = new File("src/test/java/com/marklogic/javaclient/rules/alertRule1.json"); String ruleInJson = convertFileToString(file); @@ -377,7 +377,7 @@ public class TestRawAlert extends BasicJavaClientREST { RuleManager ruleMgr = client.newRuleManager(); // get the rule - File file = new File("src/junit/com/marklogic/javaclient/rules/alertRule1.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/rules/alertRule1.xml"); // create a handle for the rule FileHandle writeHandle = new FileHandle(file); @@ -437,7 +437,7 @@ public class TestRawAlert extends BasicJavaClientREST { RuleManager ruleMgr = client.newRuleManager(); // get the rule - File file = new File("src/junit/com/marklogic/javaclient/rules/alertRule1.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/rules/alertRule1.xml"); // create a handle for the rule FileHandle writeHandle = new FileHandle(file); @@ -455,7 +455,7 @@ public class TestRawAlert extends BasicJavaClientREST { // Write the rule in Modules database of Server TransformExtensionsManager transformManager= client.newServerConfigManager().newTransformExtensionsManager(); - File ruleTransform = new File("src/junit/com/marklogic/javaclient/rules/rule-transform.xqy"); + File ruleTransform = new File("src/test/java/com/marklogic/javaclient/rules/rule-transform.xqy"); transformManager.writeXQueryTransform("ruleTransform", new FileHandle(ruleTransform)); // create a manager for matching rules @@ -507,9 +507,9 @@ public class TestRawAlert extends BasicJavaClientREST { InputStreamHandle ruleHandle3 = new InputStreamHandle(); // get the rule file - InputStream inputStream1 = new FileInputStream("src/junit/com/marklogic/javaclient/rules/alertRule1.xml"); - InputStream inputStream2 = new FileInputStream("src/junit/com/marklogic/javaclient/rules/alertRule2.xml"); - InputStream inputStream3 = new FileInputStream("src/junit/com/marklogic/javaclient/rules/alertRule3.xml"); + InputStream inputStream1 = new FileInputStream("src/test/java/com/marklogic/javaclient/rules/alertRule1.xml"); + InputStream inputStream2 = new FileInputStream("src/test/java/com/marklogic/javaclient/rules/alertRule2.xml"); + InputStream inputStream3 = new FileInputStream("src/test/java/com/marklogic/javaclient/rules/alertRule3.xml"); ruleHandle1.set(inputStream1); ruleHandle2.set(inputStream2); @@ -521,7 +521,7 @@ public class TestRawAlert extends BasicJavaClientREST { ruleMgr.writeRule("RULE-TEST-3", ruleHandle3); // get the json rule - File file = new File("src/junit/com/marklogic/javaclient/rules/alertRule3.json"); + File file = new File("src/test/java/com/marklogic/javaclient/rules/alertRule3.json"); String ruleInJson = convertFileToString(file); @@ -608,9 +608,9 @@ else if(expected.equals("RULE-TEST-3-JSON - {{http://marklogic.com/rest-api}rule InputStreamHandle ruleHandle3 = new InputStreamHandle(); // get the rule file - InputStream inputStream1 = new FileInputStream("src/junit/com/marklogic/javaclient/rules/alertRule1.xml"); - InputStream inputStream2 = new FileInputStream("src/junit/com/marklogic/javaclient/rules/alertRule2.xml"); - InputStream inputStream3 = new FileInputStream("src/junit/com/marklogic/javaclient/rules/alertRule3.xml"); + InputStream inputStream1 = new FileInputStream("src/test/java/com/marklogic/javaclient/rules/alertRule1.xml"); + InputStream inputStream2 = new FileInputStream("src/test/java/com/marklogic/javaclient/rules/alertRule2.xml"); + InputStream inputStream3 = new FileInputStream("src/test/java/com/marklogic/javaclient/rules/alertRule3.xml"); ruleHandle1.set(inputStream1); ruleHandle2.set(inputStream2); @@ -622,7 +622,7 @@ else if(expected.equals("RULE-TEST-3-JSON - {{http://marklogic.com/rest-api}rule ruleMgr.writeRule("RULE-TEST-3", ruleHandle3); // get the json rule - File file = new File("src/junit/com/marklogic/javaclient/rules/alertRule3.json"); + File file = new File("src/test/java/com/marklogic/javaclient/rules/alertRule3.json"); String ruleInJson = convertFileToString(file); @@ -678,7 +678,7 @@ else if(expected.equals("RULE-TEST-3-JSON - {{http://marklogic.com/rest-api}rule RuleManager ruleMgr = client.newRuleManager(); // get the rule - File file = new File("src/junit/com/marklogic/javaclient/rules/alertRule1.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/rules/alertRule1.xml"); //String combinedQuery = convertFileToString(file); @@ -746,7 +746,7 @@ else if(expected.equals("RULE-TEST-3-JSON - {{http://marklogic.com/rest-api}rule RuleManager ruleMgr = client.newRuleManager(); // get the rule - File file = new File("src/junit/com/marklogic/javaclient/rules/alertRule2.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/rules/alertRule2.xml"); // create a handle for the rule FileHandle writeHandle = new FileHandle(file); diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQuery.java b/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQuery.java index 6e7738a90..17c6ecad1 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQuery.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQuery.java @@ -62,7 +62,7 @@ public class TestRawCombinedQuery extends BasicJavaClientREST { } // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryOption.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOption.xml"); String combinedQuery = convertFileToString(file); @@ -114,7 +114,7 @@ public class TestRawCombinedQuery extends BasicJavaClientREST { } // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryOption.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOption.xml"); String combinedQuery = convertFileToString(file); @@ -171,7 +171,7 @@ public class TestRawCombinedQuery extends BasicJavaClientREST { setQueryOption(client, queryOptionName); // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryNoOption.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryNoOption.xml"); // create a handle for the search criteria FileHandle rawHandle = new FileHandle(file); @@ -223,7 +223,7 @@ public class TestRawCombinedQuery extends BasicJavaClientREST { setQueryOption(client, queryOptionName); // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryOptionCollectionOverwrite.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionCollectionOverwrite.xml"); // create a handle for the search criteria FileHandle rawHandle = new FileHandle(file); @@ -275,7 +275,7 @@ public class TestRawCombinedQuery extends BasicJavaClientREST { setQueryOption(client, queryOptionName); // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryOptionJSONOverwrite.json"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionJSONOverwrite.json"); String combinedQuery = convertFileToString(file); @@ -328,7 +328,7 @@ public class TestRawCombinedQuery extends BasicJavaClientREST { } // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryOptionJSON.json"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionJSON.json"); String combinedQuery = convertFileToString(file); @@ -381,7 +381,7 @@ public class TestRawCombinedQuery extends BasicJavaClientREST { } // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryOptionWildcard.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionWildcard.xml"); String combinedQuery = convertFileToString(file); @@ -450,7 +450,7 @@ public class TestRawCombinedQuery extends BasicJavaClientREST { writeDocumentUsingInputStreamHandle(client, filename5, "/collection-constraint/", metadataHandle5, "XML"); // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryOptionCollection.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionCollection.xml"); // create a handle for the search criteria FileHandle rawHandle = new FileHandle(file); // bug 21107 @@ -478,7 +478,7 @@ public class TestRawCombinedQuery extends BasicJavaClientREST { -@SuppressWarnings("deprecation") + @Test public void testRawCombinedQueryCombo() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testRawCombinedQueryCombo"); @@ -515,7 +515,7 @@ public class TestRawCombinedQuery extends BasicJavaClientREST { writeDocumentUsingInputStreamHandle(client, filename5, "/collection-constraint/", metadataHandle5, "XML"); // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryOptionCombo.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionCombo.xml"); // create a handle for the search criteria FileHandle rawHandle = new FileHandle(file); // bug 21107 @@ -558,7 +558,7 @@ public class TestRawCombinedQuery extends BasicJavaClientREST { } // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryOptionField.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionField.xml"); // create a handle for the search criteria FileHandle rawHandle = new FileHandle(file); // bug 21107 @@ -604,7 +604,7 @@ public class TestRawCombinedQuery extends BasicJavaClientREST { } // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryOptionPathIndex.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionPathIndex.xml"); // create a handle for the search criteria FileHandle rawHandle = new FileHandle(file); // bug 21107 @@ -676,7 +676,7 @@ public class TestRawCombinedQuery extends BasicJavaClientREST { srvMgr.writeConfiguration(); // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryOptionComboJSON.json"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionComboJSON.json"); String combinedQuery = convertFileToString(file); @@ -729,7 +729,7 @@ public class TestRawCombinedQuery extends BasicJavaClientREST { srvMgr.writeConfiguration(); // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryOptionFieldJSON.json"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionFieldJSON.json"); String combinedQuery = convertFileToString(file); diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQueryGeo.java b/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQueryGeo.java index 3be461c60..1af6113fd 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQueryGeo.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQueryGeo.java @@ -58,7 +58,7 @@ public class TestRawCombinedQueryGeo extends BasicJavaClientREST { srvMgr.writeConfiguration(); // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryOptionGeo.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionGeo.xml"); // create a handle for the search criteria FileHandle rawHandle = new FileHandle(file); // bug 21107 @@ -102,7 +102,7 @@ public class TestRawCombinedQueryGeo extends BasicJavaClientREST { srvMgr.writeConfiguration(); // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryOptionGeoJSON.json"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionGeoJSON.json"); String combinedQuery = convertFileToString(file); @@ -150,7 +150,7 @@ public class TestRawCombinedQueryGeo extends BasicJavaClientREST { srvMgr.writeConfiguration(); // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryOptionGeoBoxAndWordJSON.json"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionGeoBoxAndWordJSON.json"); String combinedQuery = convertFileToString(file); @@ -191,7 +191,7 @@ public class TestRawCombinedQueryGeo extends BasicJavaClientREST { loadGeoData(); // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryOptionGeoCircle.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionGeoCircle.xml"); // create a handle for the search criteria FileHandle rawHandle = new FileHandle(file); // bug 21107 @@ -233,7 +233,7 @@ public class TestRawCombinedQueryGeo extends BasicJavaClientREST { loadGeoData(); // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryOptionGeoBox.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionGeoBox.xml"); // create a handle for the search criteria FileHandle rawHandle = new FileHandle(file); // bug 21107 @@ -273,7 +273,7 @@ public class TestRawCombinedQueryGeo extends BasicJavaClientREST { loadGeoData(); // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryOptionGeoBoxAndWord.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionGeoBoxAndWord.xml"); // create a handle for the search criteria FileHandle rawHandle = new FileHandle(file); // bug 21107 @@ -314,7 +314,7 @@ public class TestRawCombinedQueryGeo extends BasicJavaClientREST { } // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryOptionGeoPointAndWord.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionGeoPointAndWord.xml"); // create a handle for the search criteria FileHandle rawHandle = new FileHandle(file); // bug 21107 diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestRawStructuredQuery.java b/test-complete/src/test/java/com/marklogic/javaclient/TestRawStructuredQuery.java index 804489fa9..10c950f95 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestRawStructuredQuery.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestRawStructuredQuery.java @@ -67,7 +67,7 @@ public class TestRawStructuredQuery extends BasicJavaClientREST { } // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryOption.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOption.xml"); // create a handle for the search criteria FileHandle rawHandle = new FileHandle(file); @@ -116,7 +116,7 @@ public class TestRawStructuredQuery extends BasicJavaClientREST { } // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryOptionJSON.json"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionJSON.json"); String combinedQuery = convertFileToString(file); @@ -172,7 +172,7 @@ public class TestRawStructuredQuery extends BasicJavaClientREST { setQueryOption(client, queryOptionName); // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryNoOption.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryNoOption.xml"); // create a handle for the search criteria FileHandle rawHandle = new FileHandle(file); @@ -224,7 +224,7 @@ public class TestRawStructuredQuery extends BasicJavaClientREST { setQueryOption(client, queryOptionName); // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryOptionJSONOverwrite.json"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionJSONOverwrite.json"); String combinedQuery = convertFileToString(file); @@ -292,7 +292,7 @@ public class TestRawStructuredQuery extends BasicJavaClientREST { writeDocumentUsingInputStreamHandle(client, filename5, "/collection-constraint/", metadataHandle5, "XML"); // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryOptionCollection.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionCollection.xml"); // create a handle for the search criteria FileHandle rawHandle = new FileHandle(file); // bug 21107 @@ -357,7 +357,7 @@ public class TestRawStructuredQuery extends BasicJavaClientREST { writeDocumentUsingInputStreamHandle(client, filename5, "/collection-constraint/", metadataHandle5, "XML"); // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryOptionCombo.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionCombo.xml"); // create a handle for the search criteria FileHandle rawHandle = new FileHandle(file); // bug 21107 @@ -400,7 +400,7 @@ public class TestRawStructuredQuery extends BasicJavaClientREST { } // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryOptionField.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionField.xml"); // create a handle for the search criteria FileHandle rawHandle = new FileHandle(file); // bug 21107 @@ -446,7 +446,7 @@ public class TestRawStructuredQuery extends BasicJavaClientREST { } // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryOptionPathIndex.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionPathIndex.xml"); // create a handle for the search criteria FileHandle rawHandle = new FileHandle(file); // bug 21107 @@ -515,7 +515,7 @@ public class TestRawStructuredQuery extends BasicJavaClientREST { srvMgr.writeConfiguration(); // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryOptionComboJSON.json"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionComboJSON.json"); String combinedQuery = convertFileToString(file); diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestResponseTransform.java b/test-complete/src/test/java/com/marklogic/javaclient/TestResponseTransform.java index d10f60078..de3d1d1f0 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestResponseTransform.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestResponseTransform.java @@ -72,7 +72,7 @@ public class TestResponseTransform extends BasicJavaClientREST { metadata.setVersion("0.1"); // get the transform file - File transformFile = new File("src/junit/com/marklogic/javaclient/transforms/search2html.xsl"); + File transformFile = new File("src/test/java/com/marklogic/javaclient/transforms/search2html.xsl"); FileHandle transformHandle = new FileHandle(transformFile); @@ -80,7 +80,7 @@ public class TestResponseTransform extends BasicJavaClientREST { transMgr.writeXSLTransform("search2html", transformHandle, metadata); // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryOption.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOption.xml"); String combinedQuery = convertFileToString(file); @@ -146,7 +146,7 @@ public class TestResponseTransform extends BasicJavaClientREST { metadata.setVersion("0.1"); // get the transform file - File transformFile = new File("src/junit/com/marklogic/javaclient/transforms/search2html.xsl"); + File transformFile = new File("src/test/java/com/marklogic/javaclient/transforms/search2html.xsl"); FileHandle transformHandle = new FileHandle(transformFile); @@ -154,7 +154,7 @@ public class TestResponseTransform extends BasicJavaClientREST { transMgr.writeXSLTransform("search2html", transformHandle, metadata); // get the combined query - File file = new File("src/junit/com/marklogic/javaclient/combined/combinedQueryOption.xml"); + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOption.xml"); String combinedQuery = convertFileToString(file); diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestSearchMultibyte.java b/test-complete/src/test/java/com/marklogic/javaclient/TestSearchMultibyte.java index f2545c658..c366d230b 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestSearchMultibyte.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestSearchMultibyte.java @@ -32,7 +32,7 @@ public class TestSearchMultibyte extends BasicJavaClientREST { } -@SuppressWarnings("deprecation") + @Test public void testSearchString() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testSearchString"); diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestSearchOnJSON.java b/test-complete/src/test/java/com/marklogic/javaclient/TestSearchOnJSON.java index 42dd96019..42b31150f 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestSearchOnJSON.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestSearchOnJSON.java @@ -55,7 +55,7 @@ public class TestSearchOnJSON extends BasicJavaClientREST { ReaderHandle handle = new ReaderHandle(); // write the files - BufferedReader docStream = new BufferedReader(new FileReader("src/junit/com/marklogic/javaclient/queryoptions/" + queryOptionName)); + BufferedReader docStream = new BufferedReader(new FileReader("src/test/java/com/marklogic/javaclient/queryoptions/" + queryOptionName)); handle.set(docStream); // write the query options to the database @@ -133,7 +133,7 @@ public class TestSearchOnJSON extends BasicJavaClientREST { ReaderHandle handle = new ReaderHandle(); // write the files - BufferedReader docStream = new BufferedReader(new FileReader("src/junit/com/marklogic/javaclient/queryoptions/" + queryOptionName)); + BufferedReader docStream = new BufferedReader(new FileReader("src/test/java/com/marklogic/javaclient/queryoptions/" + queryOptionName)); handle.set(docStream); // write the query options to the database diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestStructuredQuery.java b/test-complete/src/test/java/com/marklogic/javaclient/TestStructuredQuery.java index 87646580f..87453d59d 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestStructuredQuery.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestStructuredQuery.java @@ -43,7 +43,7 @@ public void testCleanUp() throws Exception System.out.println("Running clear script"); } -@SuppressWarnings("deprecation") + @Test public void testStructuredQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testStructuredQuery"); @@ -87,7 +87,7 @@ public void testCleanUp() throws Exception client.release(); } -@SuppressWarnings("deprecation") + @Test public void testStructuredQueryJSON() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testStructuredQueryJSON"); @@ -155,7 +155,7 @@ public void testCleanUp() throws Exception } -@SuppressWarnings("deprecation") + @Test public void testValueConstraintWildCard() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testValueConstraintWildCard"); @@ -208,7 +208,7 @@ public void testCleanUp() throws Exception } -@SuppressWarnings("deprecation") + @Test public void testAndNotQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testAndNotQuery"); @@ -263,7 +263,7 @@ public void testCleanUp() throws Exception } -@SuppressWarnings("deprecation") + @Test public void testNearQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testNearQuery"); @@ -316,7 +316,7 @@ public void testCleanUp() throws Exception } -@SuppressWarnings("deprecation") + @Test public void testDirectoryQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testDirectoryQuery"); @@ -375,7 +375,7 @@ public void testCleanUp() throws Exception } -@SuppressWarnings("deprecation") + @Test public void testDocumentQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testDocumentQuery"); @@ -435,7 +435,7 @@ public void testCleanUp() throws Exception } -@SuppressWarnings("deprecation") + @Test public void testCollectionQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testCollectionQuery"); @@ -509,7 +509,7 @@ public void testCleanUp() throws Exception } -@SuppressWarnings("deprecation") + @Test public void testContainerConstraintQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testContainerConstraintQuery"); diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestTransformXMLWithXSLT.java b/test-complete/src/test/java/com/marklogic/javaclient/TestTransformXMLWithXSLT.java index 66a105e18..429d70e8a 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestTransformXMLWithXSLT.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestTransformXMLWithXSLT.java @@ -26,7 +26,7 @@ public class TestTransformXMLWithXSLT extends BasicJavaClientREST { @BeforeClass public static void setUp() throws Exception { System.out.println("In setup"); - setupJavaRESTServerWithDB( "REST-Java-Client-API-Server", 8011); + setupJavaRESTServerWithDB( "REST-Java-Client-API-Server-withDB", 8015); } @@ -34,7 +34,7 @@ public class TestTransformXMLWithXSLT extends BasicJavaClientREST { @Test public void testWriteXMLWithXSLTransform() throws TransformerException, FileNotFoundException { // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8015, "rest-writer", "x", Authentication.DIGEST); // get the doc Source source = new StreamSource("src/test/java/com/marklogic/javaclient/data/employee.xml"); @@ -76,16 +76,18 @@ public class TestTransformXMLWithXSLT extends BasicJavaClientREST { Scanner scanner = new Scanner(fileRead).useDelimiter("\\Z"); String readContent = scanner.next(); - String transformedContent = readContent.replaceAll("^name$", "firstname"); - assertEquals("XML document write difference", transformedContent, readContent); +// String transformedContent = readContent.replaceAll("^name$", "firstname"); +// assertEquals("XML document write difference", transformedContent, readContent); + assertTrue("check document from DB has name element changed",readContent.contains("firstname")); scanner.close(); // release client client.release(); + scanner.close(); } @AfterClass public static void tearDown() throws Exception { System.out.println("In tear down"); - deleteRESTServerWithDB("REST-Java-Client-API-Server"); + deleteRESTServerWithDB("REST-Java-Client-API-Server-withDB"); } } diff --git a/test-complete/src/test/java/com/marklogic/javaclient/transforms/add-attr-xquery-transform.xqy b/test-complete/src/test/java/com/marklogic/javaclient/transforms/add-attr-xquery-transform.xqy index 387ff516d..af41daae1 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/transforms/add-attr-xquery-transform.xqy +++ b/test-complete/src/test/java/com/marklogic/javaclient/transforms/add-attr-xquery-transform.xqy @@ -10,8 +10,8 @@ declare function example:transform( { if (fn:empty($content/*)) then $content else ( - let $value := (map:get($params,"trans:value"),"UNDEFINED")[1] - let $name := (map:get($params, "trans:name"), "transformed")[1] + let $value := (map:get($params,"value"),"UNDEFINED")[1] + let $name := (map:get($params, "name"), "transformed")[1] let $root := $content/* return ( From 742f858440247225e350e0e2db7bdd2dd343251c Mon Sep 17 00:00:00 2001 From: MarkLogic Builder Date: Wed, 6 Aug 2014 19:02:32 -0700 Subject: [PATCH 005/357] adding new test for page method testing --- .../TestBulkSearchWithStringQueryDef.java | 202 ++++++++++++++++++ 1 file changed, 202 insertions(+) create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java new file mode 100644 index 000000000..c0d46793c --- /dev/null +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java @@ -0,0 +1,202 @@ +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.DocumentPage; +import com.marklogic.client.document.DocumentRecord; +import com.marklogic.client.document.DocumentWriteSet; +import com.marklogic.client.document.TextDocumentManager; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.SearchHandle; +import com.marklogic.client.io.StringHandle; +import com.marklogic.client.io.DocumentMetadataHandle.Capability; +import com.marklogic.client.query.MatchDocumentSummary; +import com.marklogic.client.query.MatchLocation; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.QueryManager.QueryView; +import com.marklogic.client.query.StringQueryDefinition; + +public class TestBulkSearchWithStringQueryDef extends BasicJavaClientREST{ + private static final int BATCH_SIZE=100; + private static final String DIRECTORY ="/bulkSearch/"; + private static String dbName = "TestBulkSearchSQDDB"; + private static String [] fNames = {"TestBulkSearchSQDDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client ; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + setupAppServicesConstraint(dbName); + createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + System.out.println("In tear down" ); + tearDownJavaRESTServer(dbName, fNames, restServerName); + deleteRESTUser("usr1"); + } + + @Before + public void setUp() throws Exception { + // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + // create new connection for each test below + client = DatabaseClientFactory.newClient("localhost", restPort, "usr1", "password", Authentication.DIGEST); + } + + @After + public void tearDown() throws Exception { + System.out.println("Running clear script"); + // release client + client.release(); + } + + public void validateRecord(DocumentRecord record,Format type) { + + assertNotNull("DocumentRecord should never be null", record); + assertNotNull("Document uri should never be null", record.getUri()); + assertTrue("Document uri should start with " + DIRECTORY, record.getUri().startsWith(DIRECTORY)); + assertEquals("All records are expected to be in same format", type, record.getFormat()); + // System.out.println(record.getMimetype()); + + } + public void loadTxtDocuments(){ + int count =1; + TextDocumentManager docMgr = client.newTextDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + for(int i =0;i<101;i++){ + writeset.add(DIRECTORY+"Textfoo"+i+".txt", new StringHandle().with("bar can be foo"+i)); + if(count%BATCH_SIZE == 0){ + docMgr.write(writeset); + writeset = docMgr.newWriteSet(); + } + count++; + } + if(count%BATCH_SIZE > 0){ + docMgr.write(writeset); + } + } + @Test + public void testBulkSearchSQDwithDifferentPageSizes() { + int count; + loadTxtDocuments(); + //Creating a txt document manager for bulk search + TextDocumentManager docMgr = client.newTextDocumentManager(); + //using QueryManger for query definition and set the search criteria + QueryManager queryMgr = client.newQueryManager(); + StringQueryDefinition qd = queryMgr.newStringDefinition(); + qd.setCriteria("bar"); + // set document manager level settings for search response + System.out.println("Default Page length setting on docMgr :"+docMgr.getPageLength()); + docMgr.setPageLength(1); + docMgr.setSearchView(QueryView.RESULTS); + docMgr.setResponseFormat(Format.XML); + assertEquals("format set on document manager","XML",docMgr.getResponseFormat().toString()); + // assertEquals("Queryview set on document manager ","RESULTS" ,docMgr.getSearchView().toString()); + assertEquals("Page length ",1,docMgr.getPageLength()); + // Search for documents where content has bar and get first result record, get search handle on it + SearchHandle sh = new SearchHandle(); + DocumentPage page= docMgr.search(qd, 0); + // test for page methods + assertEquals("Number of records",1,page.size()); + assertEquals("Starting record in first page ",1,page.getStart()); + assertEquals("Total number of estimated results:",101,page.getTotalSize()); + assertEquals("Total number of estimated pages :",101,page.getTotalPages()); + // till the issue #78 get fixed + assertFalse("Is this First page :",page.isFirstPage());//this is bug + assertFalse("Is this Last page :",page.isLastPage()); + assertTrue("Is this First page has content:",page.hasContent()); + // Need the Issue #75 to be fixed + assertTrue("Is first page has previous page ?",page.hasPreviousPage()); + // + long pageNo=1; + do{ + count=0; + page = docMgr.search(qd, pageNo,sh); + if(pageNo >1){ + assertFalse("Is this first Page", page.isFirstPage()); + assertTrue("Is page has previous page ?",page.hasPreviousPage()); + } + while(page.hasNext()){ + DocumentRecord rec = page.next(); + rec.getFormat(); + validateRecord(rec,Format.TEXT); + // System.out.println(rec.getUri()); + count++; + } + MatchDocumentSummary[] mds= sh.getMatchResults(); + assertEquals("Matched document count",1,mds.length); + //since we set the query view to get only results, facet count supposed be 0 + assertEquals("Matched Facet count",0,sh.getFacetNames().length); + + assertEquals("document count", page.size(),count); + // assertEquals("Page Number #",pageNo,page.getPageNumber()); + pageNo = pageNo + page.getPageSize(); + }while(!page.isLastPage()); + assertTrue("page count is 101 ",pageNo == page.getTotalPages()); + assertTrue("Page has previous page ?",page.hasPreviousPage()); + assertEquals("page size", 1,page.getPageSize()); + assertEquals("document count", 101,page.getTotalSize()); + page= docMgr.search(qd, 102); + assertFalse("Page has any records ?",page.hasContent()); + } + //This test is trying to set the setResponse to JSON on DocumentManager and use search handle which only work with XML + @Test(expected = UnsupportedOperationException.class) + public void testBulkSearchSQDwithWrongResponseFormat() { + loadTxtDocuments(); + TextDocumentManager docMgr = client.newTextDocumentManager(); + QueryManager queryMgr = client.newQueryManager(); + StringQueryDefinition qd = queryMgr.newStringDefinition(); + qd.setCriteria("bar"); + System.out.println(docMgr.getResponseFormat().toString()); + docMgr.setResponseFormat(Format.JSON); + SearchHandle results = new SearchHandle(); + DocumentPage page= docMgr.search(qd, 1,results); + MatchDocumentSummary[] summaries = results.getMatchResults(); + for (MatchDocumentSummary summary : summaries ) { + MatchLocation[] locations = summary.getMatchLocations(); + for (MatchLocation location : locations) { + System.out.println(location.getAllSnippetText()); + // do something with the snippet text + } + } + + } + // + @Test + public void testBulkSearchSQDwithResponseFormat() { + int count =1; + loadTxtDocuments(); + TextDocumentManager docMgr = client.newTextDocumentManager(); + docMgr.setResponseFormat(Format.TEXT); + QueryManager queryMgr = client.newQueryManager(); + StringQueryDefinition qd = queryMgr.newStringDefinition(); + qd.setCriteria("bar"); + System.out.println(docMgr.getResponseFormat().toString()); + // docMgr.setResponseFormat(Format.JSON); + SearchHandle results = new SearchHandle(); + DocumentPage page= docMgr.search(qd, 1,results); + MatchDocumentSummary[] summaries = results.getMatchResults(); + for (MatchDocumentSummary summary : summaries ) { + MatchLocation[] locations = summary.getMatchLocations(); + for (MatchLocation location : locations) { + System.out.println(summary.getUri()); + // do something with the snippet text + System.out.println(location.getPath()); + } + } + + } +} From 450ee31e4b2ba0f9a404810b73c8758482488d4f Mon Sep 17 00:00:00 2001 From: MarkLogic Builder Date: Thu, 7 Aug 2014 08:59:11 -0700 Subject: [PATCH 006/357] updating tests --- .../marklogic/javaclient/TestBug21183.java | 1 - .../marklogic/javaclient/TestBug26248.java | 90 ++++++ .../javaclient/TestBulkSearchEWithQBE.java | 260 ++++++++++++++++++ .../javaclient/TestRawCombinedQuery.java | 12 +- 4 files changed, 358 insertions(+), 5 deletions(-) create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/TestBug26248.java create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchEWithQBE.java diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBug21183.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBug21183.java index a6f363bf9..af34b88a4 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBug21183.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBug21183.java @@ -35,7 +35,6 @@ public static void setUp() throws Exception setupAppServicesConstraint(dbName); } -@SuppressWarnings("deprecation") @Test public void testBug21183() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBug26248.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBug26248.java new file mode 100644 index 000000000..1f36621e5 --- /dev/null +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBug26248.java @@ -0,0 +1,90 @@ +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.TextDocumentManager; +import com.marklogic.client.io.InputStreamHandle; +import com.marklogic.client.io.StringHandle; + +public class TestBug26248 { + DatabaseClient client ; + @Before + public void setUp() throws Exception { + + } + + @After + public void tearDown() throws Exception { + client.release(); + } + + static class DocWriter implements Runnable { + static boolean isResendable = true; + String id; + String resendableDoc; + InputStream onetimeDoc; + TextDocumentManager docMgr; + DocWriter(DatabaseClient client, String id, String doc) throws Exception { + this.id = id; + if (isResendable) + this.resendableDoc = doc; + else + this.onetimeDoc = new ByteArrayInputStream(doc.getBytes("UTF-8")); + docMgr = client.newTextDocumentManager(); + } + @Override + public void run() { + try { + System.out.println("sleeping "+id); + Thread.sleep(1000); + } catch (InterruptedException e) { + } + runImpl(); + } + public void runImpl() { + System.out.println("writing "+id); + if (isResendable) + docMgr.write(id, new StringHandle(resendableDoc)); + else + docMgr.write(id, new InputStreamHandle(onetimeDoc)); + System.out.println(id+"="+docMgr.read(id, new StringHandle()).get()); + docMgr.delete(id); + System.out.println("finished "+id); + } + } + + + @Test + public void testBug26248() throws Exception { + try{ + DocWriter.isResendable = false; + client= DatabaseClientFactory.newClient( + "localhost", 8011, "rest-writer", "x", Authentication.DIGEST + ); + DocWriter dw0 = new DocWriter(client, "/tmp/test0.txt", "The zeroth text"); + DocWriter dw1 = new DocWriter(client, "/tmp/test1.txt", "The first text"); + DocWriter dw2 = new DocWriter(client, "/tmp/test2.txt", "The second text"); + + Thread t1 = new Thread(dw1); + Thread t2 = new Thread(dw2); + + t1.start(); + t2.start(); + dw0.runImpl(); + Thread.sleep(2000); + }catch(Exception e){ + throw e; + } + } + +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchEWithQBE.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchEWithQBE.java new file mode 100644 index 000000000..d4d94388c --- /dev/null +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchEWithQBE.java @@ -0,0 +1,260 @@ +package com.marklogic.javaclient; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; +import static org.junit.Assert.*; + +import java.io.IOException; +import java.util.HashMap; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.DocumentPage; +import com.marklogic.client.document.DocumentRecord; +import com.marklogic.client.document.DocumentWriteSet; +import com.marklogic.client.document.JSONDocumentManager; +import com.marklogic.client.document.TextDocumentManager; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.JacksonHandle; +import com.marklogic.client.io.SearchHandle; +import com.marklogic.client.io.StringHandle; +import com.marklogic.client.io.DocumentMetadataHandle.Capability; +import com.marklogic.client.query.MatchDocumentSummary; +import com.marklogic.client.query.MatchLocation; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.RawQueryByExampleDefinition; +import com.marklogic.client.query.StringQueryDefinition; +import com.marklogic.client.query.QueryManager.QueryView; + + + + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; + +public class TestBulkSearchEWithQBE extends BasicJavaClientREST{ + private static final int BATCH_SIZE=100; + private static final String DIRECTORY ="/bulkSearch/"; + private static String dbName = "TestBulkSearchQBEDB"; + private static String [] fNames = {"TestBulkSearchQBEDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client ; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + setupAppServicesConstraint(dbName); + createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + System.out.println("In tear down" ); + tearDownJavaRESTServer(dbName, fNames, restServerName); + deleteRESTUser("usr1"); + } + + @Before + public void setUp() throws Exception { + // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + // create new connection for each test below + client = DatabaseClientFactory.newClient("localhost", restPort, "usr1", "password", Authentication.DIGEST); + } + + @After + public void tearDown() throws Exception { + System.out.println("Running clear script"); + // release client + client.release(); + } + + public void validateRecord(DocumentRecord record,Format type) { + + assertNotNull("DocumentRecord should never be null", record); + assertNotNull("Document uri should never be null", record.getUri()); + assertTrue("Document uri should start with " + DIRECTORY, record.getUri().startsWith(DIRECTORY)); + assertEquals("All records are expected to be in same format", type, record.getFormat()); + // System.out.println(record.getMimetype()); + + } + public void loadXMLDocuments() throws IOException, ParserConfigurationException, SAXException, TransformerException{ + int count=1; + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + for(int i =0;i<102;i++){ + + writeset.add(DIRECTORY+"foo"+i+".xml", new DOMHandle(getDocumentContent("This is so foo with a bar "+i))); + + if(count%BATCH_SIZE == 0){ + docMgr.write(writeset); + writeset = docMgr.newWriteSet(); + } + count++; + } + if(count%BATCH_SIZE > 0){ + docMgr.write(writeset); + } + } + public void loadTxtDocuments(){ + int count =1; + TextDocumentManager docMgr = client.newTextDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + for(int i =0;i<101;i++){ + writeset.add(DIRECTORY+"Textfoo"+i+".txt", new StringHandle().with("bar can be foo"+i)); + if(count%BATCH_SIZE == 0){ + docMgr.write(writeset); + writeset = docMgr.newWriteSet(); + } + count++; + } + if(count%BATCH_SIZE > 0){ + docMgr.write(writeset); + } + } + + public void loadJSONDocuments() throws JsonProcessingException, IOException{ + int count=1; + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + + HashMap 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 testBulkSearchQBEWithXMLResponseFormat() throws IOException, ParserConfigurationException, SAXException, TransformerException, XpathException { + int count; + loadTxtDocuments(); + loadXMLDocuments(); + loadJSONDocuments(); + //Creating a xml document manager for bulk search + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + //using QBE for query definition and set the search criteria + + QueryManager queryMgr = client.newQueryManager(); + String queryAsString = + "foo"; + RawQueryByExampleDefinition qd = queryMgr.newRawQueryByExampleDefinition(new StringHandle(queryAsString)); + + // set document manager level settings for search response + docMgr.setPageLength(25); + docMgr.setSearchView(QueryView.RESULTS); + docMgr.setResponseFormat(Format.XML); + + // Search for documents where content has bar and get first result record, get search handle on it,Use DOMHandle to read results + DOMHandle dh = new DOMHandle(); + DocumentPage page; + + long pageNo=1; + do{ + count=0; + page = docMgr.search(qd, pageNo,dh); + if(pageNo >1){ + assertFalse("Is this first Page", page.isFirstPage()); + assertTrue("Is page has previous page ?",page.hasPreviousPage()); + } + while(page.hasNext()){ + DocumentRecord rec = page.next(); + rec.getFormat(); + validateRecord(rec,Format.XML); + + count++; + } + + Document resultDoc = dh.get(); + assertXpathEvaluatesTo("xml", "string(//*[local-name()='result'][last()]//@*[local-name()='format'])", resultDoc); + assertEquals("document count", page.size(),count); + // assertEquals("Page Number #",pageNo,page.getPageNumber()); + pageNo = pageNo + page.getPageSize(); + }while(!page.isLastPage() && page.hasContent() ); + assertEquals("page count is 5 ",5, page.getTotalPages()); + assertTrue("Page has previous page ?",page.hasPreviousPage()); + assertEquals("page size", 25,page.getPageSize()); + assertEquals("document count", 102,page.getTotalSize()); + + } + @Test + public void testBulkSearchQBEWithJSONResponseFormat() throws IOException, ParserConfigurationException, SAXException, TransformerException { + int count; + loadTxtDocuments(); + loadXMLDocuments(); + loadJSONDocuments(); + //Creating a xml document manager for bulk search + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + //using QBE for query definition and set the search criteria + + QueryManager queryMgr = client.newQueryManager(); + String queryAsString = "{\"$query\": { \"says\": {\"$word\":\"woof\",\"$exact\": false}}}"; + RawQueryByExampleDefinition qd = queryMgr.newRawQueryByExampleDefinition(new StringHandle(queryAsString).withFormat(Format.JSON)); + + // set document manager level settings for search response + docMgr.setPageLength(25); + docMgr.setSearchView(QueryView.RESULTS); + docMgr.setResponseFormat(Format.JSON); + + // Search for documents where content has bar and get first result record, get search handle on it,Use DOMHandle to read results + JacksonHandle sh = new JacksonHandle(); + DocumentPage page; + + long pageNo=1; + do{ + count=0; + page = docMgr.search(qd, pageNo,sh); + if(pageNo >1){ + assertFalse("Is this first Page", page.isFirstPage()); + assertTrue("Is page has previous page ?",page.hasPreviousPage()); + } + while(page.hasNext()){ + DocumentRecord rec = page.next(); + rec.getFormat(); + validateRecord(rec,Format.JSON); + System.out.println(rec.getContent(new StringHandle()).get().toString()); + count++; + } + assertTrue("Page start in results and on page",sh.get().get("start").asLong() == page.getStart()); + assertEquals("document count", page.size(),count); + // assertEquals("Page Number #",pageNo,page.getPageNumber()); + pageNo = pageNo + page.getPageSize(); + }while(!page.isLastPage() && page.hasContent() ); + + assertEquals("page count is ",5,page.getTotalPages()); + assertTrue("Page has previous page ?",page.hasPreviousPage()); + assertEquals("page size", 25,page.getPageSize()); + assertEquals("document count", 102,page.getTotalSize()); + + } + +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQuery.java b/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQuery.java index 17c6ecad1..12702329a 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQuery.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQuery.java @@ -32,14 +32,19 @@ public class TestRawCombinedQuery extends BasicJavaClientREST { private static String dbName = "TestRawCombinedQueryDB"; private static String [] fNames = {"TestRawCombinedQueryDB-1"}; private static String restServerName = "REST-Java-Client-API-Server"; - + private static int restPort =8011; @BeforeClass public static void setUp() throws Exception { System.out.println("In setup"); setupJavaRESTServer(dbName, fNames[0],restServerName,8011); setupAppServicesConstraint(dbName); } - +@After +public void testCleanUp() throws Exception +{ + clearDB(restPort); + System.out.println("Running clear script"); +} @SuppressWarnings("deprecation") @Test public void testBug22353() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { @@ -632,7 +637,7 @@ public class TestRawCombinedQuery extends BasicJavaClientREST { } -@SuppressWarnings("deprecation") + @Test public void testRawCombinedQueryComboJSON() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testRawCombinedQueryComboJSON"); @@ -706,7 +711,6 @@ public class TestRawCombinedQuery extends BasicJavaClientREST { } -@SuppressWarnings("deprecation") @Test public void testRawCombinedQueryFieldJSON() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { System.out.println("Running testRawCombinedQueryFieldJSON"); From eb9a6a8b77fe9114a5652803ad2dfee5b4263bf0 Mon Sep 17 00:00:00 2001 From: MarkLogic Builder Date: Thu, 7 Aug 2014 10:25:01 -0700 Subject: [PATCH 007/357] updating tests --- .../TestAppServicesValueConstraint.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesValueConstraint.java b/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesValueConstraint.java index f0b21cd1e..bdb82a657 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesValueConstraint.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesValueConstraint.java @@ -39,7 +39,7 @@ public void testCleanUp() throws Exception clearDB(8011); System.out.println("Running clear script"); } - @SuppressWarnings("deprecation") + @Test public void testWildcard() throws IOException, ParserConfigurationException, SAXException, XpathException { @@ -75,7 +75,7 @@ public void testWildcard() throws IOException, ParserConfigurationException, SAX assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); - String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:element-value-query(fn:QName(\"\", \"id\"), \"00*2\", (\"lang=en\"), 1), cts:element-value-query(fn:QName(\"\", \"id\"), \"0??6\", (\"lang=en\"), 1))), (\"score-logtfidf\"), 1))[1 to 10]"; + String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:element-value-query(fn:QName(\"\",\"id\"), \"00*2\", (\"lang=en\"), 1), cts:element-value-query(fn:QName(\"\",\"id\"), \"0??6\", (\"lang=en\"), 1))), (\"score-logtfidf\"), 1))[1 to 10]"; assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); @@ -83,7 +83,7 @@ public void testWildcard() throws IOException, ParserConfigurationException, SAX client.release(); } - @SuppressWarnings("deprecation") + @Test public void testGoogleStyleGrammar() throws IOException, ParserConfigurationException, SAXException, XpathException { @@ -121,7 +121,7 @@ public void testGoogleStyleGrammar() throws IOException, ParserConfigurationExce assertXpathEvaluatesTo("0113", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); - String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:element-value-query(fn:QName(\"\", \"title\"), \"The memex\", (\"lang=en\"), 1), cts:and-query((cts:element-value-query(fn:QName(\"\", \"id\"), \"0113\", (\"lang=en\"), 1), cts:element-value-query(fn:QName(\"http://purl.org/dc/elements/1.1/\", \"date\"), \"2007-03-03\", (\"lang=en\"), 1)), ()))), (\"score-logtfidf\"), 1))[1 to 10]"; + String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:element-value-query(fn:QName(\"\",\"title\"), \"The memex\", (\"lang=en\"), 1), cts:and-query((cts:element-value-query(fn:QName(\"\",\"id\"), \"0113\", (\"lang=en\"), 1), cts:element-value-query(fn:QName(\"http://purl.org/dc/elements/1.1/\",\"date\"), \"2007-03-03\", (\"lang=en\"), 1)), ()))), (\"score-logtfidf\"), 1))[1 to 10]"; assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); @@ -129,7 +129,7 @@ public void testGoogleStyleGrammar() throws IOException, ParserConfigurationExce client.release(); } - @SuppressWarnings("deprecation") + @Test public void testWithoutIndexSettingsAndNS() throws IOException, ParserConfigurationException, SAXException, XpathException { @@ -164,7 +164,7 @@ public void testWithoutIndexSettingsAndNS() throws IOException, ParserConfigurat assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - String expectedSearchReport = "(cts:search(fn:collection(), cts:element-value-query(fn:QName(\"\", \"id\"), \"0012\", (\"lang=en\"), 1), (\"score-logtfidf\"), 1))[1 to 10]"; + String expectedSearchReport = "(cts:search(fn:collection(), cts:element-value-query(fn:QName(\"\",\"id\"), \"0012\", (\"lang=en\"), 1), (\"score-logtfidf\"), 1))[1 to 10]"; assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); @@ -207,7 +207,7 @@ public void testWithIndexSettingsAndNS() throws IOException, ParserConfiguration assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); assertXpathEvaluatesTo("2007-03-03", "string(//*[local-name()='result'][1]//*[local-name()='date'])", resultDoc); - String expectedSearchReport = "(cts:search(fn:collection(), cts:element-value-query(fn:QName(\"http://purl.org/dc/elements/1.1/\", \"date\"), \"2007-03-03\", (\"lang=en\"), 1), (\"score-logtfidf\"), 1))[1 to 10]"; + String expectedSearchReport = "(cts:search(fn:collection(), cts:element-value-query(fn:QName(\"http://purl.org/dc/elements/1.1/\",\"date\"), \"2007-03-03\", (\"lang=en\"), 1), (\"score-logtfidf\"), 1))[1 to 10]"; assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); @@ -215,7 +215,7 @@ public void testWithIndexSettingsAndNS() throws IOException, ParserConfiguration client.release(); } - @SuppressWarnings("deprecation") + @Test public void testSpaceSeparated() throws IOException, ParserConfigurationException, SAXException, XpathException { @@ -251,7 +251,7 @@ public void testSpaceSeparated() throws IOException, ParserConfigurationExceptio assertXpathEvaluatesTo("Vannevar Bush", "string(//*[local-name()='result']//*[local-name()='title'])", resultDoc); assertXpathEvaluatesTo("0.1", "string(//*[local-name()='result']//@*[local-name()='amt'])", resultDoc); - String expectedSearchReport = "(cts:search(fn:collection(), cts:and-query((cts:element-value-query(fn:QName(\"\", \"title\"), \"Vannevar Bush\", (\"lang=en\"), 1), cts:element-attribute-value-query(fn:QName(\"http://cloudbank.com\", \"price\"), fn:QName(\"\", \"amt\"), \"0.1\", (\"lang=en\"), 1)), ()), (\"score-logtfidf\"), 1))[1 to 10]"; + String expectedSearchReport = "(cts:search(fn:collection(), cts:and-query((cts:element-value-query(fn:QName(\"\",\"title\"), \"Vannevar Bush\", (\"lang=en\"), 1), cts:element-attribute-value-query(fn:QName(\"http://cloudbank.com\",\"price\"), fn:QName(\"\",\"amt\"), \"0.1\", (\"lang=en\"), 1)), ()), (\"score-logtfidf\"), 1))[1 to 10]"; assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); From 19c3dc8f6a931793d37f24673e63c427c214a676 Mon Sep 17 00:00:00 2001 From: MarkLogic Builder Date: Thu, 7 Aug 2014 11:41:09 -0700 Subject: [PATCH 008/357] updated the test for the env issue --- .../javaclient/TestQueryOptionBuilder.java | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilder.java b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilder.java index e0e0beb73..b18cc4820 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilder.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilder.java @@ -20,6 +20,7 @@ import com.marklogic.client.query.StringQueryDefinition; import com.marklogic.client.query.StructuredQueryBuilder; import com.marklogic.client.query.StructuredQueryDefinition; + import org.custommonkey.xmlunit.exceptions.XpathException; import org.w3c.dom.Document; import org.xml.sax.SAXException; @@ -31,6 +32,7 @@ import com.marklogic.client.io.DocumentMetadataHandle; import com.marklogic.client.io.QueryOptionsHandle; import com.marklogic.client.io.StringHandle; + import org.junit.*; public class TestQueryOptionBuilder extends BasicJavaClientREST { @@ -46,7 +48,13 @@ public static void setUp() throws Exception } -@SuppressWarnings("deprecation") +@After +public void testCleanUp() throws Exception +{ + clearDB(8011); + System.out.println("Running clear script"); +} + @Test public void testValueConstraintWildcard() throws FileNotFoundException, XpathException { System.out.println("Running testValueConstraintWildcard"); @@ -107,7 +115,7 @@ public static void setUp() throws Exception assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); - String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:element-value-query(fn:QName(\"\", \"id\"), \"00*2\", (\"lang=en\"), 1), cts:element-value-query(fn:QName(\"\", \"id\"), \"0??6\", (\"lang=en\"), 1))), (\"score-logtfidf\"), 1))[1 to 10]"; + String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:element-value-query(fn:QName(\"\",\"id\"), \"00*2\", (\"lang=en\"), 1), cts:element-value-query(fn:QName(\"\",\"id\"), \"0??6\", (\"lang=en\"), 1))), (\"score-logtfidf\"), 1))[1 to 10]"; assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); @@ -184,7 +192,7 @@ public static void setUp() throws Exception assertXpathEvaluatesTo("0.12", "string(//*[local-name()='result'][1]//@*[local-name()='amt'])", resultDoc); assertXpathEvaluatesTo("123.45", "string(//*[local-name()='result'][2]//@*[local-name()='amt'])", resultDoc); - String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:word-query(\"Memex\", (\"lang=en\"), 1), cts:element-attribute-word-query(fn:QName(\"http://cloudbank.com\", \"price\"), fn:QName(\"\", \"amt\"), \".12\", (\"lang=en\"), 1))), (\"score-logtfidf\"), 1))[1 to 10]"; + String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:word-query(\"Memex\", (\"lang=en\"), 1), cts:element-attribute-word-query(fn:QName(\"http://cloudbank.com\",\"price\"), fn:QName(\"\",\"amt\"), \".12\", (\"lang=en\"), 1))), (\"score-logtfidf\"), 1))[1 to 10]"; assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); @@ -193,7 +201,7 @@ public static void setUp() throws Exception } -@SuppressWarnings("deprecation") + @Test public void testAllConstraintsWithStringSearch() throws FileNotFoundException, XpathException, TransformerException { System.out.println("Running testAllConstraintsWithStringSearch"); @@ -310,7 +318,7 @@ public static void setUp() throws Exception } -@SuppressWarnings("deprecation") + @Test public void testAllConstraintsWithStructuredSearch() throws FileNotFoundException, XpathException, TransformerException { System.out.println("Running testAllConstraintsWithStructuredSearch"); @@ -434,7 +442,7 @@ public static void setUp() throws Exception } -@SuppressWarnings("deprecation") + @Test public void testExtractMetadataWithStructuredSearch() throws XpathException, TransformerException, ParserConfigurationException, SAXException, IOException { System.out.println("testExtractMetadataWithStructuredSearch"); @@ -590,8 +598,6 @@ public static void setUp() throws Exception client.release(); }*/ - -@SuppressWarnings("deprecation") @Test public void testExtractMetadataWithStructuredSearchAndRangeConstraint() throws XpathException, TransformerException, ParserConfigurationException, SAXException, IOException { System.out.println("testExtractMetadataWithStructuredSearchAndRangeConstraint"); @@ -680,7 +686,6 @@ public static void setUp() throws Exception } -@SuppressWarnings("deprecation") @Test public void testDocumentLevelMetadata() throws XpathException, TransformerException, ParserConfigurationException, SAXException, IOException { System.out.println("testDocumentLevelMetadata"); From 8f7f0bd33d57bd7efff97948c12f9fdac28e940c Mon Sep 17 00:00:00 2001 From: sravan Date: Thu, 7 Aug 2014 11:49:58 -0700 Subject: [PATCH 009/357] updated the test for the env issue --- .../javaclient/TestQueryOptionBuilderGrammar.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderGrammar.java b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderGrammar.java index abe460ba6..0c4b28e09 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderGrammar.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderGrammar.java @@ -25,6 +25,7 @@ import com.marklogic.client.io.DOMHandle; import com.marklogic.client.io.QueryOptionsHandle; import com.marklogic.client.io.StringHandle; + import org.junit.*; public class TestQueryOptionBuilderGrammar extends BasicJavaClientREST { @@ -40,6 +41,13 @@ public static void setUp() throws Exception } +@After +public void testCleanUp() throws Exception +{ + clearDB(8011); + System.out.println("Running clear script"); +} + @SuppressWarnings("deprecation") @Test public void testGrammarOperatorQuotation() throws FileNotFoundException, XpathException, TransformerException { From 0d077af76270d40c23c7b959d771e29ac090715c Mon Sep 17 00:00:00 2001 From: sravan Date: Thu, 7 Aug 2014 11:55:33 -0700 Subject: [PATCH 010/357] updated the test for the env issue --- .../TestQueryOptionBuilderSearchableExpression.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderSearchableExpression.java b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderSearchableExpression.java index 04e5cb39f..9f27234e1 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderSearchableExpression.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderSearchableExpression.java @@ -21,6 +21,7 @@ import com.marklogic.client.io.DOMHandle; import com.marklogic.client.io.QueryOptionsHandle; import com.marklogic.client.io.StringHandle; + import org.junit.*; public class TestQueryOptionBuilderSearchableExpression extends BasicJavaClientREST { @@ -34,7 +35,13 @@ public class TestQueryOptionBuilderSearchableExpression extends BasicJavaClientR setupJavaRESTServer(dbName, fNames[0],restServerName,8011); setupAppServicesConstraint(dbName); } - + +@After +public void testCleanUp() throws Exception +{ + clearDB(8011); + System.out.println("Running clear script"); +} @SuppressWarnings("deprecation") @Test public void testSearchableExpressionChildAxis() throws FileNotFoundException, XpathException, TransformerException From 4faf03e40698d94ab85ef8451e6464fc7d59ee56 Mon Sep 17 00:00:00 2001 From: sravan Date: Thu, 7 Aug 2014 12:13:46 -0700 Subject: [PATCH 011/357] updated the test for the env issue --- .../java/com/marklogic/javaclient/TestSearchOptions.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestSearchOptions.java b/test-complete/src/test/java/com/marklogic/javaclient/TestSearchOptions.java index 2915448ed..0b0818a2c 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestSearchOptions.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestSearchOptions.java @@ -22,6 +22,7 @@ import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; import static org.custommonkey.xmlunit.XMLAssert.assertXpathNotExists; import static org.custommonkey.xmlunit.XMLAssert.assertXpathExists; + import org.custommonkey.xmlunit.exceptions.XpathException; import org.junit.*; public class TestSearchOptions extends BasicJavaClientREST { @@ -37,7 +38,13 @@ public class TestSearchOptions extends BasicJavaClientREST { setupJavaRESTServer(dbName, fNames[0],restServerName,8011); setupAppServicesConstraint(dbName); } - + +@After +public void testCleanUp() throws Exception +{ + clearDB(8011); + System.out.println("Running clear script"); +} @SuppressWarnings("deprecation") @Test public void testReturnResultsFalse() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException From 981d686aeddbeee5976171e136e2a14a2eb846fd Mon Sep 17 00:00:00 2001 From: sravan Date: Mon, 11 Aug 2014 18:36:57 -0700 Subject: [PATCH 012/357] updated the test with jsonPath language --- .../javaclient/TestPartialUpdate.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java index c303fd88c..7bea62ed4 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java @@ -18,6 +18,7 @@ import com.marklogic.client.document.DocumentDescriptor; import com.marklogic.client.document.DocumentMetadataPatchBuilder; import com.marklogic.client.document.DocumentPatchBuilder; +import com.marklogic.client.document.DocumentPatchBuilder.PathLanguage; import com.marklogic.client.document.DocumentUriTemplate; import com.marklogic.client.document.JSONDocumentManager; import com.marklogic.client.document.XMLDocumentManager; @@ -30,6 +31,7 @@ import com.marklogic.client.io.marker.DocumentPatchHandle; import com.marklogic.client.query.StructuredQueryBuilder.FragmentScope; import com.marklogic.client.query.ValuesDefinition.Frequency; + import org.junit.*; @@ -39,9 +41,10 @@ public class TestPartialUpdate extends BasicJavaClientREST { private static String [] fNames = {"TestPartialUpdateDB-1"}; private static String restServerName = "REST-Java-Client-API-Server"; @BeforeClass - public static void setUp() throws Exception + public static void setUp() throws Exception { System.out.println("In setup"); + System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); setupJavaRESTServer(dbName, fNames[0], restServerName,8011); setupAppServicesConstraint(dbName); } @@ -101,7 +104,7 @@ public void testPartialUpdateXML() throws IOException String docId = "/partial-update/json-original.json"; JSONDocumentManager docMgr = client.newJSONDocumentManager(); DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); - + patchBldr.pathLanguage(PathLanguage.JSONPATH); ObjectNode fragmentNode = mapper.createObjectNode(); fragmentNode = mapper.createObjectNode(); fragmentNode.put("insertedKey", 9); @@ -540,6 +543,7 @@ public void testPartialUpdateXML() throws IOException JSONDocumentManager docMgr = client.newJSONDocumentManager(); DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); + patchBldr.pathLanguage(PathLanguage.JSONPATH); String content1 = docMgr.read(docId, new StringHandle()).get(); System.out.println("Before" + content1); @@ -696,7 +700,7 @@ public void testPartialUpdateXML() throws IOException DocumentDescriptor desc = docMgr.newDescriptor(docId); DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); - + patchBldr.pathLanguage(PathLanguage.JSONPATH); ObjectNode fragmentNode = mapper.createObjectNode(); fragmentNode = mapper.createObjectNode(); fragmentNode.put("insertedKey", 9); @@ -786,12 +790,12 @@ public void testPartialUpdateXML() throws IOException fragmentNode = mapper.createObjectNode(); fragmentNode.put("insertedKey", 9); String fragment = mapper.writeValueAsString(fragmentNode); - + patchBldr.pathLanguage(PathLanguage.JSONPATH); patchBldr.insertFragment("$.employees", Position.LAST_CHILD, fragment); DocumentPatchHandle patchHandle = patchBldr.build(); - Transaction t = client.openTransaction("Tranc"); - docMgr.patch(desc, patchHandle,t); - t.commit(); +// Transaction t = client.openTransaction("Tranc"); + docMgr.patch(desc, patchHandle);//,t); +// t.commit(); String content = docMgr.read(docId, new StringHandle()).get(); System.out.println("After"+content); @@ -850,7 +854,7 @@ public void testPartialUpdateXML() throws IOException // release client client.release(); } -@AfterClass +@AfterClass public static void tearDown() throws Exception { tearDownJavaRESTServer(dbName, fNames, restServerName); From a630383e58874df3e985e4efc2f820fd4ad444b1 Mon Sep 17 00:00:00 2001 From: sravan Date: Mon, 11 Aug 2014 20:20:38 -0700 Subject: [PATCH 013/357] updated the test with module func implemetation --- .../javaclient/TestDatabaseAuthentication.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestDatabaseAuthentication.java b/test-complete/src/test/java/com/marklogic/javaclient/TestDatabaseAuthentication.java index 053a1659f..473a33e22 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestDatabaseAuthentication.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestDatabaseAuthentication.java @@ -32,8 +32,8 @@ public static void setUp() throws Exception @Test public void testAuthenticationNone() throws IOException { - setAuthentication("application-level"); - setDefaultUser("rest-admin"); + setAuthentication("application-level",restServerName); + setDefaultUser("rest-admin",restServerName); System.out.println("Running testAuthenticationNone"); @@ -60,14 +60,14 @@ public static void setUp() throws Exception // release client client.release(); - setAuthentication("digest"); - setDefaultUser("nobody"); + setAuthentication("digest",restServerName); + setDefaultUser("nobody",restServerName); } @Test public void testAuthenticationBasic() throws IOException { - setAuthentication("basic"); - setDefaultUser("rest-writer"); + setAuthentication("basic",restServerName); + setDefaultUser("rest-writer",restServerName); System.out.println("Running testAuthenticationBasic"); @@ -94,8 +94,8 @@ public static void setUp() throws Exception // release client client.release(); - setAuthentication("digest"); - setDefaultUser("nobody"); + setAuthentication("digest",restServerName); + setDefaultUser("nobody",restServerName); } public void tearDown() throws Exception From b8615af0ab4f2d16c90eeadc87e9d22641c43420 Mon Sep 17 00:00:00 2001 From: sravan Date: Mon, 11 Aug 2014 20:20:53 -0700 Subject: [PATCH 014/357] updated the test with module func implemetation --- .../marklogic/javaclient/ConnectedRESTQA.java | 40 ++++++++++++++++++- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java b/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java index faf347ce8..13116cc12 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java @@ -31,6 +31,7 @@ import java.io.Reader; import java.io.StringReader; import java.io.StringWriter; +import java.io.UnsupportedEncodingException; import java.nio.channels.FileChannel; import java.util.Iterator; import java.util.Map; @@ -1273,10 +1274,45 @@ public static void loadBug18993(){ } - public static void setAuthentication(String level){ + public static void setAuthentication(String level,String restServerName) throws ClientProtocolException, IOException + { + DefaultHttpClient client = new DefaultHttpClient(); + + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + String body = "{\"authentication\": \""+level+"\"}"; + HttpPut put = new HttpPut("http://localhost:8002/manage/v2/servers/"+restServerName+"/properties?server-type=http&group-id=Default"); + put.addHeader("Content-type", "application/json"); + put.setEntity(new StringEntity(body)); + + HttpResponse response2 = client.execute(put); + HttpEntity respEntity = response2.getEntity(); + if(respEntity != null){ + String content = EntityUtils.toString(respEntity); + System.out.println(content); + } } - public static void setDefaultUser(String usr){ + public static void setDefaultUser(String usr,String restServerName) throws ClientProtocolException, IOException { + + DefaultHttpClient client = new DefaultHttpClient(); + + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + String body = "{\"default-user\": \""+usr+"\"}"; + + HttpPut put = new HttpPut("http://localhost:8002/manage/v2/servers/"+restServerName+"/properties?server-type=http&group-id=Default"); + put.addHeader("Content-type", "application/json"); + put.setEntity(new StringEntity(body)); + HttpResponse response2 = client.execute(put); + HttpEntity respEntity = response2.getEntity(); + if(respEntity != null){ + String content = EntityUtils.toString(respEntity); + System.out.println(content); + } } + } \ No newline at end of file From c9d46933130d9c74d926e48729f92632e2cfd624 Mon Sep 17 00:00:00 2001 From: sravan Date: Mon, 11 Aug 2014 20:40:23 -0700 Subject: [PATCH 015/357] updated the test with module func implemetation --- .../com/marklogic/javaclient/TestDatabaseAuthentication.java | 1 + 1 file changed, 1 insertion(+) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestDatabaseAuthentication.java b/test-complete/src/test/java/com/marklogic/javaclient/TestDatabaseAuthentication.java index 473a33e22..d9c6f8ba5 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestDatabaseAuthentication.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestDatabaseAuthentication.java @@ -98,6 +98,7 @@ public static void setUp() throws Exception setDefaultUser("nobody",restServerName); } +@AfterClass public void tearDown() throws Exception { System.out.println("In tear down" ); From 26fb855a9fa2c979c8439c36f23ca3df1e1fcd94 Mon Sep 17 00:00:00 2001 From: sravan Date: Tue, 12 Aug 2014 13:04:34 -0700 Subject: [PATCH 016/357] Adding AfterClass annotation for teardown --- .../src/test/java/com/marklogic/javaclient/TestBug21183.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBug21183.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBug21183.java index af34b88a4..4090b519d 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBug21183.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBug21183.java @@ -85,8 +85,8 @@ public void testBug21183() throws IOException, ParserConfigurationException, SAX // release client client.release(); } - - public void tearDown() throws Exception + @AfterClass + public static void tearDown() throws Exception { System.out.println("In tear down"); tearDownJavaRESTServer(dbName, fNames, restServerName); From 467b8bf514c8c6febaf5fdb30829403f8a3cfb03 Mon Sep 17 00:00:00 2001 From: Niko Schmuck Date: Wed, 13 Aug 2014 08:26:18 +0200 Subject: [PATCH 017/357] Upgrading to latest stable logback version, also simplified dependency to logback a bit. --- pom.xml | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index dd24ccff1..8b107d612 100644 --- a/pom.xml +++ b/pom.xml @@ -181,7 +181,7 @@ ch.qos.logback logback-classic - 1.0.12 + 1.1.2 commons-logging @@ -203,11 +203,6 @@ mimepull 1.9.4 - - ch.qos.logback - logback-core - 1.0.12 - org.slf4j slf4j-api From 7245f3107efcf85a925256c50c06e8c1cde750ec Mon Sep 17 00:00:00 2001 From: sammefford Date: Wed, 13 Aug 2014 09:08:55 -0600 Subject: [PATCH 018/357] fix #82, handle more than just InputStream as the way to receive SearchReadHandle content --- src/main/java/com/marklogic/client/impl/JerseyServices.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/marklogic/client/impl/JerseyServices.java b/src/main/java/com/marklogic/client/impl/JerseyServices.java index bb6431e40..76d3f7e9a 100644 --- a/src/main/java/com/marklogic/client/impl/JerseyServices.java +++ b/src/main/java/com/marklogic/client/impl/JerseyServices.java @@ -793,9 +793,9 @@ private JerseyResultIterator getBulkDocumentsImpl(RequestLogger reqlog, if ( partList != null && partList.size() > 0 ) { BodyPart searchResponsePart = partList.get(0); HandleImplementation handleBase = HandleAccessor.as(searchHandle); - - InputStream stream = searchResponsePart.getEntityAs(InputStream.class); - handleBase.receiveContent(stream); + handleBase.receiveContent( + searchResponsePart.getEntityAs(handleBase.receiveAs()) + ); partList = partList.subList(1, partList.size()); } return makeResults(JerseyServiceResultIterator.class, reqlog, "read", "resource", partList, response); From 6dabcce8ba26ba5d22d82ee6b3ef9a70862f2c12 Mon Sep 17 00:00:00 2001 From: sammefford Date: Wed, 13 Aug 2014 12:37:28 -0600 Subject: [PATCH 019/357] initial working demo connecting to XDBC server and passing string variable --- pom.xml | 19 +++----- .../com/marklogic/client/impl/XdbcEval.java | 44 +++++++++++++++++++ 2 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 src/main/java/com/marklogic/client/impl/XdbcEval.java diff --git a/pom.xml b/pom.xml index 637b42985..f0eecc899 100644 --- a/pom.xml +++ b/pom.xml @@ -164,20 +164,15 @@ - com.sun.jersey + org.glassfish.jersey.core jersey-client - 1.17 - - - com.sun.jersey.contribs - jersey-apache-client4 - 1.17 - - - com.sun.jersey.contribs - jersey-multipart - 1.17 + 2.11 + + org.glassfish.jersey.media + jersey-media-multipart + 2.11 + ch.qos.logback logback-classic diff --git a/src/main/java/com/marklogic/client/impl/XdbcEval.java b/src/main/java/com/marklogic/client/impl/XdbcEval.java new file mode 100644 index 000000000..ea8c9cca0 --- /dev/null +++ b/src/main/java/com/marklogic/client/impl/XdbcEval.java @@ -0,0 +1,44 @@ +package com.marklogic.client.impl; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.Form; +import javax.ws.rs.core.Form; +import javax.ws.rs.core.Response; +import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; +import org.glassfish.jersey.media.multipart.BodyPart; +import org.glassfish.jersey.media.multipart.MultiPart; +import org.glassfish.jersey.media.multipart.MultiPartFeature; + +public class XdbcEval { + public static void main(String[] args) { + Client client = ClientBuilder.newClient(); + // set up digest auth + HttpAuthenticationFeature feature = HttpAuthenticationFeature.digest("admin", "admin"); + client.register(feature); + // set up multipart response parsing + client.register(MultiPartFeature.class); + // set up x-www-form-urlencoded request params + Form params = new Form() + .param("xquery", "declare variable $myvar external;\"Hello+World: \"||$myvar") + .param("evn0", "") + .param("evl0", "myvar") + .param("evt0", "xs:string") + .param("evv0", "monsters") + .param("locale", "en_US") + .param("tzoffset", "-21600") + .param("dbname", "java-unittest"); + // make request + Response res = client.target("http://localhost:8020/eval") + .request() + .buildPost(Entity.form(params)) + .invoke(); + // read response as multipart even though there's only one part + MultiPart body = res.readEntity(MultiPart.class); + System.out.println("response=[" + res + "]"); + for ( BodyPart part : body.getBodyParts() ) { + System.out.println("part body=[" + part.getEntityAs(String.class) + "]"); + } + } +} From 8a3ce475b2fe712978900d9e10886ca697e2154e Mon Sep 17 00:00:00 2001 From: gvaidees Date: Wed, 13 Aug 2014 15:07:21 -0700 Subject: [PATCH 020/357] Fixed addField to not set field-type which does not seem valid --- .../src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java | 1 - 1 file changed, 1 deletion(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java b/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java index 13116cc12..da083c681 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java @@ -1091,7 +1091,6 @@ public static void addField(String dbName, String fieldName) throws Exception{ ArrayNode arrNode = mapper.createArrayNode(); ObjectNode childNodeObject = mapper.createObjectNode(); childNodeObject.put( "field-name", fieldName); - childNodeObject.put("field-type", "root"); childNodeObject.put( "include-root", true); childNodeObject.putNull( "included-elements"); childNodeObject.putNull( "excluded-elements"); From 999dbff12a82ed807da7b337cb46d983d94402a0 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Wed, 13 Aug 2014 15:39:54 -0700 Subject: [PATCH 021/357] Uses XMLUnit assert to validate 2 XML nodes instead of string comparison --- .../marklogic/javaclient/TestBug19443.java | 36 ++++++++++++++----- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBug19443.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBug19443.java index c11be6011..29eb2249e 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBug19443.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBug19443.java @@ -1,27 +1,29 @@ package com.marklogic.javaclient; -import static org.junit.Assert.*; +import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; -import java.io.FileNotFoundException; +import org.custommonkey.xmlunit.XMLUnit; import javax.xml.namespace.QName; import com.marklogic.client.admin.QueryOptionsManager; import com.marklogic.client.admin.config.QueryOptionsBuilder; import com.marklogic.client.io.Format; -import org.custommonkey.xmlunit.exceptions.XpathException; import com.marklogic.client.DatabaseClient; import com.marklogic.client.DatabaseClientFactory; import com.marklogic.client.DatabaseClientFactory.Authentication; import com.marklogic.client.io.QueryOptionsHandle; import com.marklogic.client.io.StringHandle; + import org.junit.*; public class TestBug19443 extends BasicJavaClientREST { private static String dbName = "TestBug19443DB"; private static String [] fNames = {"TestBug19443DB-1"}; private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + @BeforeClass public static void setUp() throws Exception { @@ -30,9 +32,16 @@ public static void setUp() throws Exception setupAppServicesConstraint(dbName); } +@After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + @SuppressWarnings("deprecation") @Test - public void testBug19443() throws FileNotFoundException, XpathException + public void testBug19443() throws Exception { System.out.println("Running testBug19443"); @@ -59,13 +68,22 @@ public void testBug19443() throws FileNotFoundException, XpathException optionsMgr.readOptions("ElementChildGeoSpatialIndex", readHandle); String output = readHandle.get(); - String outputTrimmed = output.replaceAll("\n", ""); - outputTrimmed = outputTrimmed.replaceAll(" ", ""); - System.out.println(output); - assertTrue("option is not correct", outputTrimmed.contains("type=long-lat-point") || outputTrimmed.contains("type=long-lat-point")); - + String actual = + "" + + "" + + "" + + "" + + "type=long-lat-point" + + "" + + "" + + "" + + ""; + + XMLUnit.setIgnoreWhitespace(true); + assertXMLEqual("Hello", actual, output); + // release client client.release(); } From bc67d3ea697987d6ebec8bfb173fb3fb20787019 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Wed, 13 Aug 2014 15:43:06 -0700 Subject: [PATCH 022/357] Clearing DB after every test run --- .../javaclient/TestConstraintCombination.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestConstraintCombination.java b/test-complete/src/test/java/com/marklogic/javaclient/TestConstraintCombination.java index 7c7b2877a..8cd9fc185 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestConstraintCombination.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestConstraintCombination.java @@ -3,6 +3,7 @@ import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; + import org.w3c.dom.Document; import org.xml.sax.SAXException; @@ -15,6 +16,7 @@ import com.marklogic.client.io.DocumentMetadataHandle; import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; + import org.custommonkey.xmlunit.exceptions.XpathException; import org.junit.*; public class TestConstraintCombination extends BasicJavaClientREST { @@ -22,6 +24,8 @@ public class TestConstraintCombination extends BasicJavaClientREST { private static String dbName = "ConstraintCombinationDB"; private static String [] fNames = {"ConstraintCombinationDB-1"}; private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + @BeforeClass public static void setUp() throws Exception { @@ -37,6 +41,13 @@ public static void setUp() throws Exception enableTrailingWildcardSearches(dbName); } +@After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + @SuppressWarnings("deprecation") @Test public void testConstraintCombination() throws IOException, ParserConfigurationException, SAXException, XpathException From e607268ac7f7dbc3a21edc7688347aef729237c5 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Wed, 13 Aug 2014 15:45:20 -0700 Subject: [PATCH 023/357] Clear DB after each test run --- .../javaclient/TestDatabaseAuthentication.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestDatabaseAuthentication.java b/test-complete/src/test/java/com/marklogic/javaclient/TestDatabaseAuthentication.java index d9c6f8ba5..692018ce7 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestDatabaseAuthentication.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestDatabaseAuthentication.java @@ -29,6 +29,14 @@ public static void setUp() throws Exception setupAppServicesConstraint(dbName); } + + @After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + @Test public void testAuthenticationNone() throws IOException { @@ -99,7 +107,7 @@ public static void setUp() throws Exception } @AfterClass - public void tearDown() throws Exception + public static void tearDown() throws Exception { System.out.println("In tear down" ); tearDownJavaRESTServer(dbName, fNames, restServerName); From 91914ed5acc55d9be3bb1cb5252dbb6282c92e6a Mon Sep 17 00:00:00 2001 From: gvaidees Date: Wed, 13 Aug 2014 15:46:55 -0700 Subject: [PATCH 024/357] Clear DB after each test run --- .../com/marklogic/javaclient/TestFieldConstraint.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestFieldConstraint.java b/test-complete/src/test/java/com/marklogic/javaclient/TestFieldConstraint.java index 574eeb97a..4245eab95 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestFieldConstraint.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestFieldConstraint.java @@ -9,6 +9,7 @@ import com.marklogic.client.DatabaseClientFactory.Authentication; import com.marklogic.client.io.SearchHandle; import com.marklogic.javaclient.BasicJavaClientREST; + import org.junit.*; public class TestFieldConstraint extends BasicJavaClientREST { static String filenames[] = {"bbq1.xml", "bbq2.xml", "bbq3.xml", "bbq4.xml", "bbq5.xml"}; @@ -16,6 +17,8 @@ public class TestFieldConstraint extends BasicJavaClientREST { private static String dbName = "FieldConstraintDB"; private static String [] fNames = {"FieldConstraintDB-1"}; private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort=8011; + @BeforeClass public static void setUp() throws Exception { @@ -25,6 +28,13 @@ public static void setUp() throws Exception includeElementField(dbName, "bbqtext", "http://example.com", "title"); includeElementField(dbName, "bbqtext", "http://example.com", "abstract"); } + + @After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } @SuppressWarnings("deprecation") @Test From 744d3a476645de377d51258f5e2daa219ec916dc Mon Sep 17 00:00:00 2001 From: gvaidees Date: Wed, 13 Aug 2014 15:48:19 -0700 Subject: [PATCH 025/357] Clear DB after test run and expectes outout needed a space --- .../com/marklogic/javaclient/TestKeyValueSearch.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestKeyValueSearch.java b/test-complete/src/test/java/com/marklogic/javaclient/TestKeyValueSearch.java index 40a634335..f82d23d4d 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestKeyValueSearch.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestKeyValueSearch.java @@ -9,6 +9,7 @@ import com.marklogic.client.query.MatchDocumentSummary; import com.marklogic.client.query.MatchLocation; import com.marklogic.client.query.QueryManager; + import org.w3c.dom.Document; import org.xml.sax.SAXException; @@ -31,6 +32,8 @@ public class TestKeyValueSearch extends BasicJavaClientREST { private static String dbName = "TestKeyValueSearchDB"; private static String [] fNames = {"TestKeyValueSearchDB-1"}; private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort=8011; + @BeforeClass public static void setUp() throws Exception { @@ -39,6 +42,13 @@ public static void setUp() throws Exception setupAppServicesConstraint(dbName); } +@After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + @SuppressWarnings("deprecation") @Test public void testKeyValueSearch() throws IOException, ParserConfigurationException, SAXException, XpathException @@ -85,7 +95,7 @@ public void testKeyValueSearch() throws IOException, ParserConfigurationExceptio assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - String expectedSearchReport = "(cts:search(fn:collection(), cts:element-value-query(fn:QName(\"\", \"id\"), \"0012\", (\"case-sensitive\",\"diacritic-sensitive\",\"punctuation-sensitive\",\"whitespace-sensitive\",\"unstemmed\",\"unwildcarded\",\"lexicon-expand=heuristic\",\"lang=en\"), 1), (\"score-logtfidf\"), 1))[1 to 10]"; + String expectedSearchReport = "(cts:search(fn:collection(), cts:element-value-query(fn:QName(\"\",\"id\"), \"0012\", (\"case-sensitive\",\"diacritic-sensitive\",\"punctuation-sensitive\",\"whitespace-sensitive\",\"unstemmed\",\"unwildcarded\",\"lexicon-expand=heuristic\",\"lang=en\"), 1), (\"score-logtfidf\"), 1))[1 to 10]"; assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); From b1f2b9a3d0ae2c918eb242ad61bb4ba9cdf5e827 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Wed, 13 Aug 2014 15:50:11 -0700 Subject: [PATCH 026/357] Clear DB after each test run --- .../java/com/marklogic/javaclient/TestPartialUpdate.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java index 7bea62ed4..34173f438 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java @@ -40,6 +40,8 @@ public class TestPartialUpdate extends BasicJavaClientREST { private static String dbName = "TestPartialUpdateDB"; private static String [] fNames = {"TestPartialUpdateDB-1"}; private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort=8011; + @BeforeClass public static void setUp() throws Exception { @@ -49,6 +51,13 @@ public static void setUp() throws Exception setupAppServicesConstraint(dbName); } +@After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + @SuppressWarnings("deprecation") @Test public void testPartialUpdateXML() throws IOException From 7ff8bd1dcfc370c6610018bb533bb007f8a8e359 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Wed, 13 Aug 2014 15:58:34 -0700 Subject: [PATCH 027/357] Merged with svn changes --- .../marklogic/javaclient/TestQueryByExample.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryByExample.java b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryByExample.java index 1c01fa3d0..989eda4ff 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryByExample.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryByExample.java @@ -35,6 +35,7 @@ public class TestQueryByExample extends BasicJavaClientREST { private static String dbName = "TestQueryByExampleDB"; private static String [] fNames = {"TestQueryByExampleDB-1"}; private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort=8011; @BeforeClass public static void setUp() throws Exception { @@ -43,6 +44,13 @@ public static void setUp() throws Exception setupAppServicesConstraint(dbName); } +@After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + @SuppressWarnings("deprecation") @Test public void testQueryByExampleXML() throws IOException, TransformerException, XpathException @@ -146,10 +154,10 @@ public void testQueryByExampleXML() throws IOException, TransformerException, Xp String resultDoc = queryMgr.search(qbyex, new StringHandle()).get(); - System.out.println(resultDoc); + System.out.println("testQueryByExampleJSON Result : "+resultDoc); - assertTrue("total result is not correct", resultDoc.contains("\"total\":1")); - assertTrue("doc returned is not correct", resultDoc.contains("{\"snippet-format\":\"snippet\",\"total\":1,\"start\":1,\"page-length\":10,\"results\":[{\"index\":1,\"uri\":\"/qbe/constraint1.json\",\"path\":\"fn:doc(\\\"/qbe/constraint1.json\\\")\",\"score\":28672,\"confidence\":0.6951694,\"fitness\":0.6951694,\"href\":\"/v1/documents?uri=%2Fqbe%2Fconstraint1.json\",\"mimetype\":\"application/json\",\"format\":\"json\",\"matches\":[{\"path\":\"fn:doc(\\\"/qbe/constraint1.json\\\")/*:json/*:root/*:id\",\"match-text\":[{\"highlight\":11}]}],\"metadata\":[{\"title\":\"Vannevar Bush\",\"metadata-type\":\"element\"},{\"id\":11,\"metadata-type\":\"element\"},{\"p\":\"Vannevar Bush wrote an article for The Atlantic Monthly\",\"metadata-type\":\"element\"},{\"popularity\":5,\"metadata-type\":\"element\"}]}]")); + + assertTrue("doc returned is not correct", resultDoc.contains("")); // release client client.release(); From 99f351891aec0c0aa74246c89a966ae92667e60d Mon Sep 17 00:00:00 2001 From: gvaidees Date: Wed, 13 Aug 2014 15:59:10 -0700 Subject: [PATCH 028/357] Clear DB after each test run --- .../marklogic/javaclient/TestQueryOptionsHandle.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionsHandle.java b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionsHandle.java index b4d6601e2..6fe6064d3 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionsHandle.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionsHandle.java @@ -12,6 +12,7 @@ import javax.xml.transform.TransformerException; import com.marklogic.client.query.QueryManager; + import org.w3c.dom.Document; import org.xml.sax.SAXException; @@ -34,6 +35,7 @@ public class TestQueryOptionsHandle extends BasicJavaClientREST { private static String dbName = "TestQueryOptionsHandleDB"; private static String [] fNames = {"TestQueryOptionsHandleDB-1"}; private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort=8011; @BeforeClass public static void setUp() throws Exception { @@ -41,6 +43,14 @@ public class TestQueryOptionsHandle extends BasicJavaClientREST { setupJavaRESTServer(dbName, fNames[0], restServerName,8011); setupAppServicesConstraint(dbName); } + +@After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + @SuppressWarnings("deprecation") From 3ff4fbd7a05755d316ce655dbeec0ed999067a00 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Thu, 14 Aug 2014 10:39:41 -0700 Subject: [PATCH 029/357] Asserts same as what is in svn --- .../TestAppServicesGeoElementConstraint.java | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesGeoElementConstraint.java b/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesGeoElementConstraint.java index d723d2279..d99af177b 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesGeoElementConstraint.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesGeoElementConstraint.java @@ -73,9 +73,10 @@ public void testPointPositiveLangLat() throws IOException, ParserConfigurationEx // get the result Document resultDoc = resultsHandle.get(); - + + System.out.println("Returned result of testPointPositiveLangLat :"+convertXMLDocumentToString(resultDoc)+" Ends here"); assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("karl_kara 12,5 12,5 12 5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); + assertXpathEvaluatesTo("12,5", "string(//*[local-name()='result'][1]//*[local-name()='highlight'])", resultDoc); // release client client.release(); @@ -110,9 +111,10 @@ public void testPointNegativeLangLat() throws IOException, ParserConfigurationEx // get the result Document resultDoc = resultsHandle.get(); - + + System.out.println("testPointNegativeLangLat Result : "+convertXMLDocumentToString(resultDoc)); assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("karl_kara -12,-5 -12,-5 -12 -5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); + assertXpathEvaluatesTo("-12,-5", "string(//*[local-name()='result'][1]//*[local-name()='highlight'])", resultDoc); // release client client.release(); @@ -192,13 +194,13 @@ public void testCirclePositiveLangLat() throws IOException, ParserConfigurationE // get the result Document resultDoc = resultsHandle.get(); - + assertXpathEvaluatesTo("5", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("karl_kara 12,5 12,5 12 5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("bill_kara 13,5 13,5 13 5", "string(//*[local-name()='result'][2]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("karl_gale 12,6 12,6 12 6", "string(//*[local-name()='result'][3]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("jack_kara 11,5 11,5 11 5", "string(//*[local-name()='result'][4]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("karl_jill 12,4 12,4 12 4", "string(//*[local-name()='result'][5]//*[local-name()='match'])", resultDoc); + assertXpathEvaluatesTo("12,5", "string(//*[local-name()='result'][1]//*[local-name()='highlight'])", resultDoc); + assertXpathEvaluatesTo("13,5", "string(//*[local-name()='result'][2]//*[local-name()='highlight'])", resultDoc); + assertXpathEvaluatesTo("12,6", "string(//*[local-name()='result'][3]//*[local-name()='highlight'])", resultDoc); + assertXpathEvaluatesTo("11,5", "string(//*[local-name()='result'][4]//*[local-name()='highlight'])", resultDoc); + assertXpathEvaluatesTo("12,4", "string(//*[local-name()='result'][5]//*[local-name()='highlight'])", resultDoc); // release client client.release(); @@ -233,12 +235,12 @@ public void testBoxPositiveLangLat() throws IOException, ParserConfigurationExce // get the result Document resultDoc = resultsHandle.get(); - + assertXpathEvaluatesTo("4", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("karl_kara 12,5 12,5 12 5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("jack_jill 11,4 11,4 11 4", "string(//*[local-name()='result'][2]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("jack_kara 11,5 11,5 11 5", "string(//*[local-name()='result'][3]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("karl_jill 12,4 12,4 12 4", "string(//*[local-name()='result'][4]//*[local-name()='match'])", resultDoc); + assertXpathEvaluatesTo("12,5", "string(//*[local-name()='result'][1]//*[local-name()='highlight'])", resultDoc); + assertXpathEvaluatesTo("11,4", "string(//*[local-name()='result'][2]//*[local-name()='highlight'])", resultDoc); + assertXpathEvaluatesTo("11,5", "string(//*[local-name()='result'][3]//*[local-name()='highlight'])", resultDoc); + assertXpathEvaluatesTo("12,4", "string(//*[local-name()='result'][4]//*[local-name()='highlight'])", resultDoc); // release client client.release(); @@ -273,7 +275,7 @@ public void testPointAndWord() throws IOException, ParserConfigurationException, // get the result Document resultDoc = resultsHandle.get(); - + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); assertXpathEvaluatesTo("/geo-constraint/geo-constraint8.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); From 8bb7e92137d38c0570dc5a1a6372a013debefa0e Mon Sep 17 00:00:00 2001 From: sravan Date: Thu, 14 Aug 2014 10:45:44 -0700 Subject: [PATCH 030/357] updating the test with more sceanrios --- .../TestBulkSearchWithStringQueryDef.java | 206 ++++++++++++++++-- 1 file changed, 183 insertions(+), 23 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java index c0d46793c..143c32aec 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java @@ -2,20 +2,37 @@ import static org.junit.Assert.*; +import java.io.IOException; +import java.util.HashMap; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; +import org.xml.sax.SAXException; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; import com.marklogic.client.DatabaseClient; import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.Transaction; import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.DocumentManager.Metadata; import com.marklogic.client.document.DocumentPage; import com.marklogic.client.document.DocumentRecord; import com.marklogic.client.document.DocumentWriteSet; +import com.marklogic.client.document.JSONDocumentManager; import com.marklogic.client.document.TextDocumentManager; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.DocumentMetadataHandle; import com.marklogic.client.io.Format; +import com.marklogic.client.io.JacksonHandle; import com.marklogic.client.io.SearchHandle; import com.marklogic.client.io.StringHandle; import com.marklogic.client.io.DocumentMetadataHandle.Capability; @@ -37,21 +54,21 @@ public class TestBulkSearchWithStringQueryDef extends BasicJavaClientREST{ @BeforeClass public static void setUpBeforeClass() throws Exception { System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); - setupAppServicesConstraint(dbName); - createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + setupAppServicesConstraint(dbName); + createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); } @AfterClass public static void tearDownAfterClass() throws Exception { System.out.println("In tear down" ); - tearDownJavaRESTServer(dbName, fNames, restServerName); - deleteRESTUser("usr1"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + deleteRESTUser("usr1"); } @Before public void setUp() throws Exception { - // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); // create new connection for each test below client = DatabaseClientFactory.newClient("localhost", restPort, "usr1", "password", Authentication.DIGEST); } @@ -62,7 +79,30 @@ public void tearDown() throws Exception { // release client client.release(); } + public void loadJSONDocuments() throws JsonProcessingException, IOException{ + int count=1; + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + HashMap 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); + } + } public void validateRecord(DocumentRecord record,Format type) { assertNotNull("DocumentRecord should never be null", record); @@ -88,6 +128,24 @@ public void loadTxtDocuments(){ docMgr.write(writeset); } } + public void loadXMLDocuments() throws IOException, ParserConfigurationException, SAXException, TransformerException{ + int count=1; + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + for(int i =0;i<102;i++){ + + writeset.add(DIRECTORY+"foo"+i+".xml", new DOMHandle(getDocumentContent("This is so foo with a bar "+i))); + + if(count%BATCH_SIZE == 0){ + docMgr.write(writeset); + writeset = docMgr.newWriteSet(); + } + count++; + } + if(count%BATCH_SIZE > 0){ + docMgr.write(writeset); + } + } @Test public void testBulkSearchSQDwithDifferentPageSizes() { int count; @@ -104,7 +162,7 @@ public void testBulkSearchSQDwithDifferentPageSizes() { docMgr.setSearchView(QueryView.RESULTS); docMgr.setResponseFormat(Format.XML); assertEquals("format set on document manager","XML",docMgr.getResponseFormat().toString()); - // assertEquals("Queryview set on document manager ","RESULTS" ,docMgr.getSearchView().toString()); + assertEquals("Queryview set on document manager ","RESULTS" ,docMgr.getSearchView().toString()); assertEquals("Page length ",1,docMgr.getPageLength()); // Search for documents where content has bar and get first result record, get search handle on it SearchHandle sh = new SearchHandle(); @@ -154,13 +212,12 @@ public void testBulkSearchSQDwithDifferentPageSizes() { } //This test is trying to set the setResponse to JSON on DocumentManager and use search handle which only work with XML @Test(expected = UnsupportedOperationException.class) - public void testBulkSearchSQDwithWrongResponseFormat() { + public void testBulkSearchSQDwithWrongResponseFormat() throws Exception { loadTxtDocuments(); TextDocumentManager docMgr = client.newTextDocumentManager(); QueryManager queryMgr = client.newQueryManager(); StringQueryDefinition qd = queryMgr.newStringDefinition(); qd.setCriteria("bar"); - System.out.println(docMgr.getResponseFormat().toString()); docMgr.setResponseFormat(Format.JSON); SearchHandle results = new SearchHandle(); DocumentPage page= docMgr.search(qd, 1,results); @@ -174,29 +231,132 @@ public void testBulkSearchSQDwithWrongResponseFormat() { } } - // + //This test has set response to JSON and pass StringHandle with format as JSON, expectint it to work, logged an issue 82 @Test - public void testBulkSearchSQDwithResponseFormat() { + public void testBulkSearchSQDwithResponseFormatandStringHandle() throws Exception{ int count =1; loadTxtDocuments(); + loadJSONDocuments(); TextDocumentManager docMgr = client.newTextDocumentManager(); - docMgr.setResponseFormat(Format.TEXT); + QueryManager queryMgr = client.newQueryManager(); StringQueryDefinition qd = queryMgr.newStringDefinition(); qd.setCriteria("bar"); - System.out.println(docMgr.getResponseFormat().toString()); - // docMgr.setResponseFormat(Format.JSON); - SearchHandle results = new SearchHandle(); + + + docMgr.setResponseFormat(Format.JSON); + docMgr.setSearchView(QueryView.METADATA); + docMgr.setMetadataCategories(Metadata.PERMISSIONS); + + StringHandle results = new StringHandle().withFormat(Format.JSON); DocumentPage page= docMgr.search(qd, 1,results); - MatchDocumentSummary[] summaries = results.getMatchResults(); - for (MatchDocumentSummary summary : summaries ) { - MatchLocation[] locations = summary.getMatchLocations(); - for (MatchLocation location : locations) { - System.out.println(summary.getUri()); - // do something with the snippet text - System.out.println(location.getPath()); - } + DocumentMetadataHandle mh = new DocumentMetadataHandle(); + while(page.hasNext()){ + DocumentRecord rec = page.next(); + validateRecord(rec,Format.TEXT); + docMgr.readMetadata(rec.getUri(),mh); + assertTrue("Records has permissions? ",mh.getPermissions().containsKey("flexrep-eval")); + assertTrue("Record has collections ?",mh.getCollections().isEmpty()); + count++; } + assertFalse("Search handle contains",results.get().isEmpty()); + + + } + //This test is testing SearchView options and search handle + @Test + public void testBulkSearchSQDwithJSONResponseFormat() throws Exception{ + + loadTxtDocuments(); + loadJSONDocuments(); + TextDocumentManager docMgr = client.newTextDocumentManager(); + + QueryManager queryMgr = client.newQueryManager(); + StringQueryDefinition qd = queryMgr.newStringDefinition(); + qd.setCriteria("woof"); + docMgr.setResponseFormat(Format.JSON); + + docMgr.setSearchView(QueryView.FACETS); + JacksonHandle jh = new JacksonHandle(); + DocumentPage page= docMgr.search(qd, 1,jh); + + // System.out.println(jh.get().toString()); + assertTrue("Searh response has entry for facets",jh.get().has("facets")); + assertTrue("Searh response has entry for facets",jh.get().has("results"));//Issue 84 is tracking this + assertTrue("Searh response has entry for facets",jh.get().has("metrics")); + + docMgr.setSearchView(QueryView.RESULTS); + page= docMgr.search(qd, 1,jh); + + assertFalse("Searh response has entry for facets",jh.get().has("facets")); + assertTrue("Searh response has entry for facets",jh.get().has("results")); + assertTrue("Searh response has entry for facets",jh.get().has("metrics"));//Issue 84 is tracking this + + docMgr.setSearchView(QueryView.METADATA); + page= docMgr.search(qd, 1,jh); + + assertFalse("Searh response has entry for facets",jh.get().has("facets")); + assertTrue("Searh response has entry for facets",jh.get().has("results")); + assertTrue("Searh response has entry for facets",jh.get().has("metrics")); + + docMgr.setSearchView(QueryView.ALL); + page= docMgr.search(qd, 1,jh); + + assertTrue("Searh response has entry for facets",jh.get().has("facets")); + assertTrue("Searh response has entry for facets",jh.get().has("results")); + assertTrue("Searh response has entry for facets",jh.get().has("metrics")); + + queryMgr.setView(QueryView.FACETS); + queryMgr.search(qd, jh); + System.out.println(jh.get().toString()); + + } + + //This test is to verify the transactions, verifies the search works with transaction before commit, after rollback and after commit + @Test + public void testBulkSearchSQDwithTransactionsandDOMHandle() throws Exception{ + TextDocumentManager docMgr = client.newTextDocumentManager(); + DOMHandle results = new DOMHandle(); + QueryManager queryMgr = client.newQueryManager(); + StringQueryDefinition qd = queryMgr.newStringDefinition(); + qd.setCriteria("thought"); + Transaction t= client.openTransaction(); + try{ + loadTxtDocuments(); + int count=1; + XMLDocumentManager xmldocMgr = client.newXMLDocumentManager(); + DocumentWriteSet writeset =xmldocMgr.newWriteSet(); + for(int i =0;i<102;i++){ + writeset.add(DIRECTORY+"boo"+i+".xml", new DOMHandle(getDocumentContent("This is so too much thought "+i))); + if(count%BATCH_SIZE == 0){ + xmldocMgr.write(writeset,t); + writeset = xmldocMgr.newWriteSet(); + } + count++; + } + if(count%BATCH_SIZE > 0){ + xmldocMgr.write(writeset,t); + } + count=0; + docMgr.setSearchView(QueryView.RESULTS); + + DocumentPage page= docMgr.search(qd, 1,results,t); + while(page.hasNext()){ + DocumentRecord rec = page.next(); + + validateRecord(rec,Format.XML); + count++; + } + assertTrue("Page has conttent :",page.hasContent()); + assertEquals("Total search results before transaction rollback are ","102",results.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total").getNodeValue()); + // System.out.println(results.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total").getNodeValue()); + + }catch(Exception e){ throw e;} + finally{t.rollback();} + + DocumentPage page= docMgr.search(qd, 1,results); + assertEquals("Total search results after rollback are ","0",results.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total").getNodeValue()); } } + From 86dd0468e179c1befec25e676f28fc216ce66772 Mon Sep 17 00:00:00 2001 From: sravan Date: Thu, 14 Aug 2014 12:13:36 -0700 Subject: [PATCH 031/357] updates with more scenarios --- .../TestBulkSearchWithStringQueryDef.java | 206 ++++++++++++++++-- 1 file changed, 183 insertions(+), 23 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java index c0d46793c..143c32aec 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java @@ -2,20 +2,37 @@ import static org.junit.Assert.*; +import java.io.IOException; +import java.util.HashMap; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; +import org.xml.sax.SAXException; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; import com.marklogic.client.DatabaseClient; import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.Transaction; import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.DocumentManager.Metadata; import com.marklogic.client.document.DocumentPage; import com.marklogic.client.document.DocumentRecord; import com.marklogic.client.document.DocumentWriteSet; +import com.marklogic.client.document.JSONDocumentManager; import com.marklogic.client.document.TextDocumentManager; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.DocumentMetadataHandle; import com.marklogic.client.io.Format; +import com.marklogic.client.io.JacksonHandle; import com.marklogic.client.io.SearchHandle; import com.marklogic.client.io.StringHandle; import com.marklogic.client.io.DocumentMetadataHandle.Capability; @@ -37,21 +54,21 @@ public class TestBulkSearchWithStringQueryDef extends BasicJavaClientREST{ @BeforeClass public static void setUpBeforeClass() throws Exception { System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); - setupAppServicesConstraint(dbName); - createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + setupAppServicesConstraint(dbName); + createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); } @AfterClass public static void tearDownAfterClass() throws Exception { System.out.println("In tear down" ); - tearDownJavaRESTServer(dbName, fNames, restServerName); - deleteRESTUser("usr1"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + deleteRESTUser("usr1"); } @Before public void setUp() throws Exception { - // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); // create new connection for each test below client = DatabaseClientFactory.newClient("localhost", restPort, "usr1", "password", Authentication.DIGEST); } @@ -62,7 +79,30 @@ public void tearDown() throws Exception { // release client client.release(); } + public void loadJSONDocuments() throws JsonProcessingException, IOException{ + int count=1; + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + HashMap 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); + } + } public void validateRecord(DocumentRecord record,Format type) { assertNotNull("DocumentRecord should never be null", record); @@ -88,6 +128,24 @@ public void loadTxtDocuments(){ docMgr.write(writeset); } } + public void loadXMLDocuments() throws IOException, ParserConfigurationException, SAXException, TransformerException{ + int count=1; + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + for(int i =0;i<102;i++){ + + writeset.add(DIRECTORY+"foo"+i+".xml", new DOMHandle(getDocumentContent("This is so foo with a bar "+i))); + + if(count%BATCH_SIZE == 0){ + docMgr.write(writeset); + writeset = docMgr.newWriteSet(); + } + count++; + } + if(count%BATCH_SIZE > 0){ + docMgr.write(writeset); + } + } @Test public void testBulkSearchSQDwithDifferentPageSizes() { int count; @@ -104,7 +162,7 @@ public void testBulkSearchSQDwithDifferentPageSizes() { docMgr.setSearchView(QueryView.RESULTS); docMgr.setResponseFormat(Format.XML); assertEquals("format set on document manager","XML",docMgr.getResponseFormat().toString()); - // assertEquals("Queryview set on document manager ","RESULTS" ,docMgr.getSearchView().toString()); + assertEquals("Queryview set on document manager ","RESULTS" ,docMgr.getSearchView().toString()); assertEquals("Page length ",1,docMgr.getPageLength()); // Search for documents where content has bar and get first result record, get search handle on it SearchHandle sh = new SearchHandle(); @@ -154,13 +212,12 @@ public void testBulkSearchSQDwithDifferentPageSizes() { } //This test is trying to set the setResponse to JSON on DocumentManager and use search handle which only work with XML @Test(expected = UnsupportedOperationException.class) - public void testBulkSearchSQDwithWrongResponseFormat() { + public void testBulkSearchSQDwithWrongResponseFormat() throws Exception { loadTxtDocuments(); TextDocumentManager docMgr = client.newTextDocumentManager(); QueryManager queryMgr = client.newQueryManager(); StringQueryDefinition qd = queryMgr.newStringDefinition(); qd.setCriteria("bar"); - System.out.println(docMgr.getResponseFormat().toString()); docMgr.setResponseFormat(Format.JSON); SearchHandle results = new SearchHandle(); DocumentPage page= docMgr.search(qd, 1,results); @@ -174,29 +231,132 @@ public void testBulkSearchSQDwithWrongResponseFormat() { } } - // + //This test has set response to JSON and pass StringHandle with format as JSON, expectint it to work, logged an issue 82 @Test - public void testBulkSearchSQDwithResponseFormat() { + public void testBulkSearchSQDwithResponseFormatandStringHandle() throws Exception{ int count =1; loadTxtDocuments(); + loadJSONDocuments(); TextDocumentManager docMgr = client.newTextDocumentManager(); - docMgr.setResponseFormat(Format.TEXT); + QueryManager queryMgr = client.newQueryManager(); StringQueryDefinition qd = queryMgr.newStringDefinition(); qd.setCriteria("bar"); - System.out.println(docMgr.getResponseFormat().toString()); - // docMgr.setResponseFormat(Format.JSON); - SearchHandle results = new SearchHandle(); + + + docMgr.setResponseFormat(Format.JSON); + docMgr.setSearchView(QueryView.METADATA); + docMgr.setMetadataCategories(Metadata.PERMISSIONS); + + StringHandle results = new StringHandle().withFormat(Format.JSON); DocumentPage page= docMgr.search(qd, 1,results); - MatchDocumentSummary[] summaries = results.getMatchResults(); - for (MatchDocumentSummary summary : summaries ) { - MatchLocation[] locations = summary.getMatchLocations(); - for (MatchLocation location : locations) { - System.out.println(summary.getUri()); - // do something with the snippet text - System.out.println(location.getPath()); - } + DocumentMetadataHandle mh = new DocumentMetadataHandle(); + while(page.hasNext()){ + DocumentRecord rec = page.next(); + validateRecord(rec,Format.TEXT); + docMgr.readMetadata(rec.getUri(),mh); + assertTrue("Records has permissions? ",mh.getPermissions().containsKey("flexrep-eval")); + assertTrue("Record has collections ?",mh.getCollections().isEmpty()); + count++; } + assertFalse("Search handle contains",results.get().isEmpty()); + + + } + //This test is testing SearchView options and search handle + @Test + public void testBulkSearchSQDwithJSONResponseFormat() throws Exception{ + + loadTxtDocuments(); + loadJSONDocuments(); + TextDocumentManager docMgr = client.newTextDocumentManager(); + + QueryManager queryMgr = client.newQueryManager(); + StringQueryDefinition qd = queryMgr.newStringDefinition(); + qd.setCriteria("woof"); + docMgr.setResponseFormat(Format.JSON); + + docMgr.setSearchView(QueryView.FACETS); + JacksonHandle jh = new JacksonHandle(); + DocumentPage page= docMgr.search(qd, 1,jh); + + // System.out.println(jh.get().toString()); + assertTrue("Searh response has entry for facets",jh.get().has("facets")); + assertTrue("Searh response has entry for facets",jh.get().has("results"));//Issue 84 is tracking this + assertTrue("Searh response has entry for facets",jh.get().has("metrics")); + + docMgr.setSearchView(QueryView.RESULTS); + page= docMgr.search(qd, 1,jh); + + assertFalse("Searh response has entry for facets",jh.get().has("facets")); + assertTrue("Searh response has entry for facets",jh.get().has("results")); + assertTrue("Searh response has entry for facets",jh.get().has("metrics"));//Issue 84 is tracking this + + docMgr.setSearchView(QueryView.METADATA); + page= docMgr.search(qd, 1,jh); + + assertFalse("Searh response has entry for facets",jh.get().has("facets")); + assertTrue("Searh response has entry for facets",jh.get().has("results")); + assertTrue("Searh response has entry for facets",jh.get().has("metrics")); + + docMgr.setSearchView(QueryView.ALL); + page= docMgr.search(qd, 1,jh); + + assertTrue("Searh response has entry for facets",jh.get().has("facets")); + assertTrue("Searh response has entry for facets",jh.get().has("results")); + assertTrue("Searh response has entry for facets",jh.get().has("metrics")); + + queryMgr.setView(QueryView.FACETS); + queryMgr.search(qd, jh); + System.out.println(jh.get().toString()); + + } + + //This test is to verify the transactions, verifies the search works with transaction before commit, after rollback and after commit + @Test + public void testBulkSearchSQDwithTransactionsandDOMHandle() throws Exception{ + TextDocumentManager docMgr = client.newTextDocumentManager(); + DOMHandle results = new DOMHandle(); + QueryManager queryMgr = client.newQueryManager(); + StringQueryDefinition qd = queryMgr.newStringDefinition(); + qd.setCriteria("thought"); + Transaction t= client.openTransaction(); + try{ + loadTxtDocuments(); + int count=1; + XMLDocumentManager xmldocMgr = client.newXMLDocumentManager(); + DocumentWriteSet writeset =xmldocMgr.newWriteSet(); + for(int i =0;i<102;i++){ + writeset.add(DIRECTORY+"boo"+i+".xml", new DOMHandle(getDocumentContent("This is so too much thought "+i))); + if(count%BATCH_SIZE == 0){ + xmldocMgr.write(writeset,t); + writeset = xmldocMgr.newWriteSet(); + } + count++; + } + if(count%BATCH_SIZE > 0){ + xmldocMgr.write(writeset,t); + } + count=0; + docMgr.setSearchView(QueryView.RESULTS); + + DocumentPage page= docMgr.search(qd, 1,results,t); + while(page.hasNext()){ + DocumentRecord rec = page.next(); + + validateRecord(rec,Format.XML); + count++; + } + assertTrue("Page has conttent :",page.hasContent()); + assertEquals("Total search results before transaction rollback are ","102",results.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total").getNodeValue()); + // System.out.println(results.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total").getNodeValue()); + + }catch(Exception e){ throw e;} + finally{t.rollback();} + + DocumentPage page= docMgr.search(qd, 1,results); + assertEquals("Total search results after rollback are ","0",results.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total").getNodeValue()); } } + From 698c87456fad076a4c379f35c05a34d11e43194b Mon Sep 17 00:00:00 2001 From: Ajit George Date: Thu, 14 Aug 2014 15:09:53 -0700 Subject: [PATCH 032/357] Added testCleanUp. --- .../test/java/com/marklogic/javaclient/TestBug18724.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBug18724.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBug18724.java index 523b09b6b..6caa60843 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBug18724.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBug18724.java @@ -25,6 +25,7 @@ import com.marklogic.client.io.ValuesHandle; import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; + import org.custommonkey.xmlunit.exceptions.XpathException; import org.junit.*; public class TestBug18724 extends BasicJavaClientREST { @@ -32,6 +33,7 @@ public class TestBug18724 extends BasicJavaClientREST { private static String dbName = "Bug18724DB"; private static String [] fNames = {"Bug18724DB-1"}; private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; @BeforeClass public static void setUp() throws Exception { @@ -39,7 +41,12 @@ public static void setUp() throws Exception setupJavaRESTServer(dbName, fNames[0], restServerName,8011); setupAppServicesConstraint(dbName); } - +@After +public void testCleanUp() throws Exception +{ + clearDB(restPort); + System.out.println("Running clear script"); +} @SuppressWarnings("deprecation") @Test public void testDefaultStringSearch() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException From 645600306e73a47ca9e7d192fdbb852d1db9f7e4 Mon Sep 17 00:00:00 2001 From: Ajit George Date: Thu, 14 Aug 2014 15:26:43 -0700 Subject: [PATCH 033/357] Added testCleanUp --- .../java/com/marklogic/javaclient/TestRawAlert.java | 7 +++++++ .../marklogic/javaclient/TestRawStructuredQuery.java | 9 +++++++-- .../com/marklogic/javaclient/TestSearchOnJSON.java | 10 +++++++++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestRawAlert.java b/test-complete/src/test/java/com/marklogic/javaclient/TestRawAlert.java index a84848a05..879c9968e 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestRawAlert.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestRawAlert.java @@ -38,6 +38,7 @@ public class TestRawAlert extends BasicJavaClientREST { private static String dbName = "TestRawAlertDB"; private static String [] fNames = {"TestRawAlertDB-1"}; private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; @BeforeClass public static void setUp() throws Exception { @@ -46,6 +47,12 @@ public class TestRawAlert extends BasicJavaClientREST { setupAppServicesConstraint(dbName); } +@After +public void testCleanUp() throws Exception +{ + clearDB(restPort); + System.out.println("Running clear script"); +} @SuppressWarnings("deprecation") @Test public void testRawAlert() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestRawStructuredQuery.java b/test-complete/src/test/java/com/marklogic/javaclient/TestRawStructuredQuery.java index 10c950f95..57a23b3fa 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestRawStructuredQuery.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestRawStructuredQuery.java @@ -28,13 +28,13 @@ import org.custommonkey.xmlunit.exceptions.XpathException; import org.junit.*; -import org.junit.Assert; public class TestRawStructuredQuery extends BasicJavaClientREST { private static String dbName = "TestRawStructuredQueryDB"; private static String [] fNames = {"TestRawStructuredQueryDB-1"}; private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; @BeforeClass public static void setUp() throws Exception { @@ -44,7 +44,12 @@ public class TestRawStructuredQuery extends BasicJavaClientREST { System.out.println("after setup"); } - +@After +public void testCleanUp() throws Exception +{ + clearDB(restPort); + System.out.println("Running clear script"); +} @Test public void testRawStructuredQueryXML() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestSearchOnJSON.java b/test-complete/src/test/java/com/marklogic/javaclient/TestSearchOnJSON.java index 42b31150f..80aafd5cd 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestSearchOnJSON.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestSearchOnJSON.java @@ -10,6 +10,7 @@ import javax.xml.parsers.ParserConfigurationException; import com.marklogic.client.query.QueryManager; + import org.xml.sax.SAXException; import com.marklogic.client.DatabaseClient; @@ -30,6 +31,7 @@ public class TestSearchOnJSON extends BasicJavaClientREST { private static String dbName = "TestSearchOnJSONDB"; private static String [] fNames = {"TestSearchOnJSONDB-1"}; private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; @BeforeClass public static void setUp() throws Exception { @@ -38,6 +40,12 @@ public class TestSearchOnJSON extends BasicJavaClientREST { setupAppServicesConstraint(dbName); } +@After +public void testCleanUp() throws Exception +{ + clearDB(restPort); + System.out.println("Running clear script"); +} @SuppressWarnings("deprecation") @Test public void testRoundtrippingQueryOption() throws IOException, ParserConfigurationException, SAXException, XpathException @@ -74,7 +82,7 @@ public class TestSearchOnJSON extends BasicJavaClientREST { String expectedOutput = "{\"options\":{\"return-metrics\":false, \"return-qtext\":false, \"debug\":true, \"transform-results\":{\"apply\":\"raw\"}, \"constraint\":[{\"name\":\"id\", \"value\":{\"element\":{\"ns\":\"\", \"name\":\"id\"}}}]}}"; - assertEquals("query option in JSON is difference", expectedOutput, output); + assertEquals("query option in JSON is difference", expectedOutput, output.trim()); // create handle to write back option in json String queryOptionNameJson = queryOptionName.replaceAll(".xml", ".json"); From c87ba79eab4ec1d0f6bc6fc656e7a43f8626d39c Mon Sep 17 00:00:00 2001 From: sravan Date: Thu, 14 Aug 2014 23:25:50 -0700 Subject: [PATCH 034/357] updating test to have different server port and db name for tests that are creating their own rest server --- .../test/java/com/marklogic/javaclient/TestWriteTextDoc.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestWriteTextDoc.java b/test-complete/src/test/java/com/marklogic/javaclient/TestWriteTextDoc.java index 5f6fc725d..c208bbe36 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestWriteTextDoc.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestWriteTextDoc.java @@ -15,7 +15,7 @@ public class TestWriteTextDoc extends BasicJavaClientREST @BeforeClass public static void setUp() throws Exception { System.out.println("In setup"); - setupJavaRESTServerWithDB( "REST-Java-Client-API-Server", 8011); + setupJavaRESTServerWithDB( "REST-Java-Client-API-Server-withDB", 8015); } @@ -32,6 +32,6 @@ public class TestWriteTextDoc extends BasicJavaClientREST @AfterClass public static void tearDown() throws Exception { System.out.println("In tear down"); - deleteRESTServerWithDB("REST-Java-Client-API-Server"); + deleteRESTServerWithDB("REST-Java-Client-API-Server-withDB"); } } From c7c98978a3e722471b3cb76992afcd2a989f8e29 Mon Sep 17 00:00:00 2001 From: sravan Date: Thu, 14 Aug 2014 23:26:08 -0700 Subject: [PATCH 035/357] updating test to have different server port and db name for tests that are creating their own rest server --- .../java/com/marklogic/javaclient/TestXMLDocumentRepair.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestXMLDocumentRepair.java b/test-complete/src/test/java/com/marklogic/javaclient/TestXMLDocumentRepair.java index 3dd6f6816..d0c0e9d4c 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestXMLDocumentRepair.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestXMLDocumentRepair.java @@ -16,7 +16,7 @@ public class TestXMLDocumentRepair extends BasicJavaClientREST { @BeforeClass public static void setUp() throws Exception { System.out.println("In setup"); - setupJavaRESTServerWithDB( "REST-Java-Client-API-Server", 8011); + setupJavaRESTServerWithDB( "REST-Java-Client-API-Server-withDB", 8015); } @@ -91,6 +91,6 @@ public class TestXMLDocumentRepair extends BasicJavaClientREST { @AfterClass public static void tearDown() throws Exception { System.out.println("In tear down"); - deleteRESTServerWithDB("REST-Java-Client-API-Server"); + deleteRESTServerWithDB("REST-Java-Client-API-Server-withDB"); } } From 097efbc38e63c24ed8c8641c7062d1cf039c1b04 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Fri, 15 Aug 2014 14:51:24 -0700 Subject: [PATCH 036/357] Fixed an xpected message in one of the methods --- .../javaclient/TestOptimisticLocking.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestOptimisticLocking.java b/test-complete/src/test/java/com/marklogic/javaclient/TestOptimisticLocking.java index 34e89f7cf..6cf1ab6e4 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestOptimisticLocking.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestOptimisticLocking.java @@ -22,12 +22,15 @@ import com.marklogic.client.io.FileHandle; import com.marklogic.client.io.StringHandle; import com.marklogic.client.document.XMLDocumentManager; + import org.junit.*; public class TestOptimisticLocking extends BasicJavaClientREST{ private static String dbName = "TestOptimisticLockingDB"; private static String [] fNames = {"TestOptimisticLockingDB-1"}; private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort=8011; + @BeforeClass public static void setUp() throws Exception { @@ -36,6 +39,13 @@ public static void setUp() throws Exception setupJavaRESTServer(dbName, fNames[0], restServerName,8011); } + @After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + @SuppressWarnings("deprecation") @Test public void testRequired() throws IOException, ParserConfigurationException, SAXException, XpathException @@ -382,7 +392,7 @@ public void testOptionalWithUnknownVersion() throws IOException, ParserConfigura @Test public void testOptionalWithGoodVersion() throws IOException, ParserConfigurationException, SAXException, XpathException { - System.out.println("Running testOptionalWithGoodVersion"); +System.out.println("Running testOptionalWithGoodVersion"); String filename = "json-original.json"; String updateFilename = "json-updated.json"; @@ -408,7 +418,7 @@ public void testOptionalWithGoodVersion() throws IOException, ParserConfiguratio System.out.println("set optimistic locking to optional"); // create document manager - JSONDocumentManager docMgr = client.newJSONDocumentManager(); + JSONDocumentManager docMgr = client.newJSONDocumentManager(); File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); @@ -495,7 +505,7 @@ public void testOptionalWithGoodVersion() throws IOException, ParserConfiguratio desc.setVersion(badVersion); String deleteException = ""; - String expectedDeleteException = "com.marklogic.client.FailedRequestException: Content version must match to delete document"; + String expectedDeleteException = "com.marklogic.client.FailedRequestException: Local message: Content version must match to delete document"; try { From 0f009c44b470e52dede5c3834274786d13fa8d8c Mon Sep 17 00:00:00 2001 From: gvaidees Date: Fri, 15 Aug 2014 14:53:38 -0700 Subject: [PATCH 037/357] Fixed expected error message in testRequired --- .../java/com/marklogic/javaclient/TestOptimisticLocking.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestOptimisticLocking.java b/test-complete/src/test/java/com/marklogic/javaclient/TestOptimisticLocking.java index 6cf1ab6e4..7342e3bb4 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestOptimisticLocking.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestOptimisticLocking.java @@ -171,7 +171,7 @@ public void testRequired() throws IOException, ParserConfigurationException, SAX desc.setVersion(badVersion); String deleteException = ""; - String expectedDeleteException = "com.marklogic.client.FailedRequestException: Content version must match to delete document"; + String expectedDeleteException = "com.marklogic.client.FailedRequestException: Local message: Content version must match to delete document"; try { From 6b45f468dec796eb4a5431030ecedabf10d92463 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Fri, 15 Aug 2014 14:56:00 -0700 Subject: [PATCH 038/357] Fixed more expected messages in testRequired --- .../java/com/marklogic/javaclient/TestOptimisticLocking.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestOptimisticLocking.java b/test-complete/src/test/java/com/marklogic/javaclient/TestOptimisticLocking.java index 7342e3bb4..45a29ea4e 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestOptimisticLocking.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestOptimisticLocking.java @@ -345,7 +345,7 @@ public void testOptionalWithUnknownVersion() throws IOException, ParserConfigura desc.setVersion(badVersion); String deleteException = ""; - String expectedDeleteException = "com.marklogic.client.FailedRequestException: Content version must match to delete document"; + String expectedDeleteException = "com.marklogic.client.FailedRequestException: Local message: Content version must match to delete document"; try { From 5ebfa41d0feebcc07bfd6ce102649bd1d47a8d53 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Fri, 15 Aug 2014 15:28:46 -0700 Subject: [PATCH 039/357] Fixed string passed to XMLEquals as "testBug19443" --- .../src/test/java/com/marklogic/javaclient/TestBug19443.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBug19443.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBug19443.java index 29eb2249e..3698c0b36 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBug19443.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBug19443.java @@ -82,7 +82,7 @@ public void testBug19443() throws Exception ""; XMLUnit.setIgnoreWhitespace(true); - assertXMLEqual("Hello", actual, output); + assertXMLEqual("testBug19443", actual, output); // release client client.release(); From 7ec21c2bb1c67b283f4020287b9755958605530d Mon Sep 17 00:00:00 2001 From: Ajit George Date: Fri, 15 Aug 2014 16:36:50 -0700 Subject: [PATCH 040/357] Edited expectedOutput string value for exception, since document file extensions takes precedence over the explicit format setting. --- .../com/marklogic/javaclient/TestDocumentFormat.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestDocumentFormat.java b/test-complete/src/test/java/com/marklogic/javaclient/TestDocumentFormat.java index b713a81c0..5a4822e10 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestDocumentFormat.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestDocumentFormat.java @@ -95,7 +95,7 @@ public void testJSONFormatOnXML() throws IOException, ParserConfigurationExcepti String docId = uri + filename; String exception = ""; - String expectedException = "write failed: Bad Request. Server Message: XDMP-JSONCHAR"; + String expectedException = ""; try { @@ -388,7 +388,7 @@ public void testTextFormatOnJSON() throws IOException, ParserConfigurationExcept String docId = uri + filename; String exception = ""; - String expectedException = "Local message: write failed: Bad Request. Server Message: XDMP-DOCUTF8SEQ"; + String expectedException = ""; try { @@ -429,7 +429,7 @@ public void testTextFormatOnJSON() throws IOException, ParserConfigurationExcept String docId = uri + filename; String exception = ""; - String expectedException = "Local message: write failed: Bad Request. Server Message: XDMP-DOCUTF8SEQ"; + String expectedException = ""; try { @@ -470,7 +470,7 @@ public void testTextFormatOnJSON() throws IOException, ParserConfigurationExcept String docId = uri + filename; String exception = ""; - String expectedException = "Local message: write failed: Bad Request. Server Message: XDMP-DOCUTF8SEQ"; + String expectedException = ""; try { @@ -583,7 +583,7 @@ public void testTextFormatOnJSON() throws IOException, ParserConfigurationExcept String docId = uri + filename; String exception = ""; - String expectedException = "write failed: Bad Request. Server Message: XDMP-JSONCHAR"; + String expectedException = ""; try { From 4ce49c661bc9f55b10120c665b668b30b37e95e7 Mon Sep 17 00:00:00 2001 From: Ajit George Date: Fri, 15 Aug 2014 16:41:26 -0700 Subject: [PATCH 041/357] Changed expected values back to the ones from the SVN. --- .../com/marklogic/javaclient/TestDocumentMimetype.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestDocumentMimetype.java b/test-complete/src/test/java/com/marklogic/javaclient/TestDocumentMimetype.java index e71ca8ec4..e326866bd 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestDocumentMimetype.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestDocumentMimetype.java @@ -278,7 +278,7 @@ public void testUnknownMimetypeOnXML() throws IOException, ParserConfigurationEx // read document mimetype docMgr.read(docId, handle); String format = handle.getFormat().name(); - String expectedFormat = "BINARY"; + String expectedFormat = "JSON"; assertEquals("Format does not match", expectedFormat, format); @@ -321,7 +321,7 @@ public void testUnknownMimetypeOnXML() throws IOException, ParserConfigurationEx // read document mimetype docMgr.read(docId, handle); String format = handle.getFormat().name(); - String expectedFormat = "TEXT"; + String expectedFormat = "JSON"; assertEquals("Format does not match", expectedFormat, format); @@ -493,7 +493,7 @@ public void testUnknownMimetypeOnXML() throws IOException, ParserConfigurationEx // read document mimetype docMgr.read(docId, handle); String format = handle.getFormat().name(); - String expectedFormat = "TEXT"; + String expectedFormat = "BINARY"; assertEquals("Format does not match", expectedFormat, format); @@ -665,7 +665,7 @@ public void testUnknownMimetypeOnXML() throws IOException, ParserConfigurationEx // read document mimetype docMgr.read(docId, handle); String format = handle.getFormat().name(); - String expectedFormat = "BINARY"; + String expectedFormat = "TEXT"; assertEquals("Format does not match", expectedFormat, format); From 0b6f3eae5c42c888f39b3f973b94870e28dd6e63 Mon Sep 17 00:00:00 2001 From: Ajit George Date: Fri, 15 Aug 2014 17:10:16 -0700 Subject: [PATCH 042/357] Changes made to expectedSearchReport string to be same as that in SVN. --- .../marklogic/javaclient/TestAppServicesWordConstraint.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesWordConstraint.java b/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesWordConstraint.java index 1a6be2255..29bea87dc 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesWordConstraint.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesWordConstraint.java @@ -123,7 +123,7 @@ public void testWithNormalWordQuery() throws IOException, ParserConfigurationExc assertXpathEvaluatesTo("0.12", "string(//*[local-name()='result'][1]//@*[local-name()='amt'])", resultDoc); assertXpathEvaluatesTo("123.45", "string(//*[local-name()='result'][2]//@*[local-name()='amt'])", resultDoc); - String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:word-query(\"Memex\", (\"lang=en\"), 1), cts:element-attribute-word-query(fn:QName(\"http://cloudbank.com\", \"price\"), fn:QName(\"\", \"amt\"), \".12\", (\"lang=en\"), 1))), (\"score-logtfidf\"), 1))[1 to 10]"; + String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:word-query(\"Memex\", (\"lang=en\"), 1), cts:element-attribute-word-query(fn:QName(\"http://cloudbank.com\",\"price\"), fn:QName(\"\",\"amt\"), \".12\", (\"lang=en\"), 1))), (\"score-logtfidf\"), 1))[1 to 10]"; assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); @@ -169,7 +169,7 @@ public void testWithTermOptionCaseInsensitive() throws IOException, ParserConfig assertXpathEvaluatesTo("1.23", "string(//*[local-name()='result'][1]//@*[local-name()='amt'])", resultDoc); assertXpathEvaluatesTo("0.12", "string(//*[local-name()='result'][2]//@*[local-name()='amt'])", resultDoc); - String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:element-word-query(fn:QName(\"\", \"title\"), \"for\", (\"case-insensitive\",\"lang=en\"), 1), cts:element-attribute-range-query(fn:QName(\"http://cloudbank.com\", \"price\"), fn:QName(\"\", \"amt\"), \"=\", 0.12, (), 1))), (\"score-logtfidf\",\"faceted\"), 1))[1 to 10]"; + String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:element-word-query(fn:QName(\"\",\"title\"), \"for\", (\"case-insensitive\",\"lang=en\"), 1), cts:element-attribute-range-query(fn:QName(\"http://cloudbank.com\",\"price\"), fn:QName(\"\",\"amt\"), \"=\", 0.12, (), 1))), (\"score-logtfidf\",\"faceted\"), 1))[1 to 10]"; assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); From 3e0714864b4a7b7bad97f9aafc4f1f35983f9ca2 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Fri, 15 Aug 2014 17:17:12 -0700 Subject: [PATCH 043/357] Element order in the returned metadata structure in testBug23843 method was different from expected order. Using assertXPathEvaluate instead of string comparison --- .../javaclient/TestPatchCardinality.java | 166 ++++++++++-------- 1 file changed, 89 insertions(+), 77 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPatchCardinality.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPatchCardinality.java index 54182bb50..1f0785104 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPatchCardinality.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPatchCardinality.java @@ -1,11 +1,13 @@ package com.marklogic.javaclient; +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; + +import org.custommonkey.xmlunit.*; + import static org.junit.Assert.*; import java.io.IOException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ObjectNode; import com.marklogic.client.DatabaseClient; import com.marklogic.client.DatabaseClientFactory; import com.marklogic.client.DatabaseClientFactory.Authentication; @@ -13,13 +15,12 @@ import com.marklogic.client.document.DocumentMetadataPatchBuilder.Cardinality; import com.marklogic.client.document.DocumentPatchBuilder; import com.marklogic.client.document.DocumentPatchBuilder.Position; -import com.marklogic.client.document.DocumentMetadataPatchBuilder; -import com.marklogic.client.document.JSONDocumentManager; import com.marklogic.client.document.XMLDocumentManager; import com.marklogic.client.io.DocumentMetadataHandle; -import com.marklogic.client.io.Format; import com.marklogic.client.io.StringHandle; import com.marklogic.client.io.marker.DocumentPatchHandle; + +import org.custommonkey.xmlunit.XMLUnit; import org.junit.*; public class TestPatchCardinality extends BasicJavaClientREST { @@ -27,6 +28,7 @@ public class TestPatchCardinality extends BasicJavaClientREST { private static String dbName = "TestPatchCardinalityDB"; private static String [] fNames = {"TestPatchCardinalityDB-1"}; private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort=8011; @BeforeClass public static void setUp() throws Exception { @@ -34,9 +36,14 @@ public static void setUp() throws Exception setupJavaRESTServer(dbName, fNames[0], restServerName,8011); setupAppServicesConstraint(dbName); } - -@SuppressWarnings("deprecation") +@After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + @Test public void testOneCardinalityNegative() throws IOException { System.out.println("Running testOneCardinalityNegative"); @@ -76,8 +83,6 @@ public static void setUp() throws Exception client.release(); } - -@SuppressWarnings("deprecation") @Test public void testOneCardinalityPositve() throws IOException { System.out.println("Running testOneCardinalityPositive"); @@ -109,8 +114,6 @@ public static void setUp() throws Exception client.release(); } - -@SuppressWarnings("deprecation") @Test public void testOneOrMoreCardinalityPositve() throws IOException { System.out.println("Running testOneOrMoreCardinalityPositive"); @@ -146,8 +149,6 @@ public static void setUp() throws Exception client.release(); } - -@SuppressWarnings("deprecation") @Test public void testOneOrMoreCardinalityNegative() throws IOException { System.out.println("Running testOneOrMoreCardinalityNegative"); @@ -187,8 +188,6 @@ public static void setUp() throws Exception client.release(); } - -@SuppressWarnings("deprecation") @Test public void testZeroOrOneCardinalityNegative() throws IOException { System.out.println("Running testZeroOrOneCardinalityNegative"); @@ -229,7 +228,6 @@ public static void setUp() throws Exception } -@SuppressWarnings("deprecation") @Test public void testZeroOrOneCardinalityPositive() throws IOException { System.out.println("Running testZeroOrOneCardinalityPositive"); @@ -262,7 +260,6 @@ public static void setUp() throws Exception } -@SuppressWarnings("deprecation") @Test public void testZeroOrOneCardinalityPositiveWithZero() throws IOException { System.out.println("Running testZeroOrOneCardinalityPositiveWithZero"); @@ -294,8 +291,6 @@ public static void setUp() throws Exception client.release(); } - -@SuppressWarnings("deprecation") @Test public void testZeroOrMoreCardinality() throws IOException { System.out.println("Running testZeroOrMoreCardinality"); @@ -331,74 +326,91 @@ public static void setUp() throws Exception client.release(); } -@SuppressWarnings("deprecation") @Test public void testBug23843() throws IOException { - System.out.println("Running testBug23843"); - - String[] filenames = {"cardinal1.xml","cardinal4.xml"}; + System.out.println("Running testBug23843"); + + String[] filenames = {"cardinal1.xml","cardinal4.xml"}; - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/cardinal/", "XML"); - - String docId = ""; + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - DocumentMetadataHandle readMetadataHandle = new DocumentMetadataHandle(); - - DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); - if (filename == "cardinal1.xml"){ - patchBldr.insertFragment("/root", Position.LAST_CHILD, Cardinality.ONE, "added"); + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/cardinal/", "XML"); + + String docId = ""; + + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DocumentMetadataHandle readMetadataHandle = new DocumentMetadataHandle(); + + DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); + if (filename == "cardinal1.xml"){ + patchBldr.insertFragment("/root", Position.LAST_CHILD, Cardinality.ONE, "added"); + } + else if (filename == "cardinal4.xml") { + patchBldr.insertFragment("/root", Position.LAST_CHILD, "added"); + } + DocumentPatchHandle patchHandle = patchBldr.build(); + String RawPatch = patchHandle.toString(); + System.out.println("Before"+RawPatch); + + String exception = ""; + if (filename == "cardinal1.xml"){ + try + { docId= "/cardinal/cardinal1.xml"; + docMgr.patch(docId, patchHandle); + + String actual = docMgr.readMetadata(docId, new DocumentMetadataHandle()).toString(); + System.out.println("Actual" + actual); + + assertXpathEvaluatesTo("2", "count(/*[local-name()='metadata']/*[local-name()='permissions']/*[local-name()='permission'])", actual); + assertXpathEvaluatesTo("1", "count(/*[local-name()='metadata']/*[local-name()='permissions']/*[local-name()='permission']/*[local-name()='role-name' and string(.)='rest-reader'])", actual); + assertXpathEvaluatesTo("1", "count(/*[local-name()='metadata']/*[local-name()='permissions']/*[local-name()='permission']/*[local-name()='role-name' and string(.)='rest-writer'])", actual); + assertXpathEvaluatesTo("1","count(/*[local-name()='metadata']/*[local-name()='quality' and string(.)='0'])", actual); + + assertXpathEvaluatesTo("read","string(/*[local-name()='metadata']/*[local-name()='permissions']/*[local-name()='permission']/*[[local-name()='role-name' and string(.)='rest-reader'] and [local-name()='capability' and string(.)='read']])", actual); + assertXpathEvaluatesTo("update","string(/*[local-name()='metadata']/*[local-name()='permissions']/*[local-name()='permission']/*[[local-name()='role-name' and string(.)='rest-writer'] and [local-name()='capability' and string(.)='update']])", actual); } - else if (filename == "cardinal4.xml") { - patchBldr.insertFragment("/root", Position.LAST_CHILD, "added"); + catch (Exception e) + { + System.out.println(e.getMessage()); + exception = e.getMessage(); } - DocumentPatchHandle patchHandle = patchBldr.build(); - String RawPatch = patchHandle.toString(); - System.out.println("Before"+RawPatch); - - String exception = ""; - if (filename == "cardinal1.xml"){ - try - { docId= "/cardinal/cardinal1.xml"; - docMgr.patch(docId, patchHandle); - System.out.println("After"+docMgr.readMetadata(docId, new DocumentMetadataHandle()).toString()); - assertEquals("rest-readerreadrest-writerupdate0", docMgr.readMetadata(docId, new DocumentMetadataHandle()).toString()); - } - catch (Exception e) - { - System.out.println(e.getMessage()); - exception = e.getMessage(); - } + } + else if (filename == "cardinal4.xml") { + try + { + docId = "/cardinal/cardinal4.xml"; + docMgr.clearMetadataCategories(); + docMgr.patch(docId, new StringHandle(patchHandle.toString())); + docMgr.setMetadataCategories(Metadata.ALL); + + String actual = docMgr.readMetadata(docId, new DocumentMetadataHandle()).toString(); + System.out.println("Actual" + actual); + + assertXpathEvaluatesTo("2", "count(/*[local-name()='metadata']/*[local-name()='permissions']/*[local-name()='permission'])", actual); + assertXpathEvaluatesTo("1", "count(/*[local-name()='metadata']/*[local-name()='permissions']/*[local-name()='permission']/*[local-name()='role-name' and string(.)='rest-reader'])", actual); + assertXpathEvaluatesTo("1", "count(/*[local-name()='metadata']/*[local-name()='permissions']/*[local-name()='permission']/*[local-name()='role-name' and string(.)='rest-writer'])", actual); + assertXpathEvaluatesTo("1","count(/*[local-name()='metadata']/*[local-name()='quality' and string(.)='0'])", actual); + + assertXpathEvaluatesTo("read","string(/*[local-name()='metadata']/*[local-name()='permissions']/*[local-name()='permission']/*[[local-name()='role-name' and string(.)='rest-reader'] and [local-name()='capability' and string(.)='read']])", actual); + assertXpathEvaluatesTo("update","string(/*[local-name()='metadata']/*[local-name()='permissions']/*[local-name()='permission']/*[[local-name()='role-name' and string(.)='rest-writer'] and [local-name()='capability' and string(.)='update']])", actual); } - else if (filename == "cardinal4.xml") { - try - { - docId = "/cardinal/cardinal4.xml"; - docMgr.clearMetadataCategories(); - docMgr.patch(docId, new StringHandle(patchHandle.toString())); - docMgr.setMetadataCategories(Metadata.ALL); - System.out.println("After"+docMgr.readMetadata(docId, new DocumentMetadataHandle()).toString()); - assertEquals("", "rest-readerreadrest-writerupdate0", docMgr.readMetadata(docId, new DocumentMetadataHandle()).toString()); - } - catch (Exception e) - { - System.out.println(e.getMessage()); - exception = e.getMessage(); - } + catch (Exception e) + { + System.out.println(e.getMessage()); + exception = e.getMessage(); } - - String actual = docMgr.read(docId, new StringHandle()).get(); - - System.out.println("Actual : "+actual); } + + String actual = docMgr.read(docId, new StringHandle()).get(); - // release client - client.release(); + System.out.println("Actual : "+actual); + } + + // release client + client.release(); } @AfterClass public static void tearDown() throws Exception From 51c3019b9f0add4664adb04a03adbb4abbfa7b2c Mon Sep 17 00:00:00 2001 From: gvaidees Date: Fri, 15 Aug 2014 17:21:27 -0700 Subject: [PATCH 044/357] Removed unused variable --- .../java/com/marklogic/javaclient/TestPatchCardinality.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPatchCardinality.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPatchCardinality.java index 1f0785104..c5c3bd262 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPatchCardinality.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPatchCardinality.java @@ -355,7 +355,6 @@ else if (filename == "cardinal4.xml") { String RawPatch = patchHandle.toString(); System.out.println("Before"+RawPatch); - String exception = ""; if (filename == "cardinal1.xml"){ try { docId= "/cardinal/cardinal1.xml"; @@ -375,7 +374,6 @@ else if (filename == "cardinal4.xml") { catch (Exception e) { System.out.println(e.getMessage()); - exception = e.getMessage(); } } else if (filename == "cardinal4.xml") { @@ -400,7 +398,6 @@ else if (filename == "cardinal4.xml") { catch (Exception e) { System.out.println(e.getMessage()); - exception = e.getMessage(); } } From c6d64d291553494068879039b36e918cbd44dbb4 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Sat, 16 Aug 2014 23:15:28 -0700 Subject: [PATCH 045/357] Fixed aserts in in bug23843 method --- .../javaclient/TestPatchCardinality.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPatchCardinality.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPatchCardinality.java index c5c3bd262..7b9495ec7 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPatchCardinality.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPatchCardinality.java @@ -342,7 +342,6 @@ public void testCleanUp() throws Exception String docId = ""; XMLDocumentManager docMgr = client.newXMLDocumentManager(); - DocumentMetadataHandle readMetadataHandle = new DocumentMetadataHandle(); DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); if (filename == "cardinal1.xml"){ @@ -368,9 +367,10 @@ else if (filename == "cardinal4.xml") { assertXpathEvaluatesTo("1", "count(/*[local-name()='metadata']/*[local-name()='permissions']/*[local-name()='permission']/*[local-name()='role-name' and string(.)='rest-writer'])", actual); assertXpathEvaluatesTo("1","count(/*[local-name()='metadata']/*[local-name()='quality' and string(.)='0'])", actual); - assertXpathEvaluatesTo("read","string(/*[local-name()='metadata']/*[local-name()='permissions']/*[local-name()='permission']/*[[local-name()='role-name' and string(.)='rest-reader'] and [local-name()='capability' and string(.)='read']])", actual); - assertXpathEvaluatesTo("update","string(/*[local-name()='metadata']/*[local-name()='permissions']/*[local-name()='permission']/*[[local-name()='role-name' and string(.)='rest-writer'] and [local-name()='capability' and string(.)='update']])", actual); - } + XMLUnit.getControlDocumentBuilderFactory().setNamespaceAware(false); + XMLAssert.assertXpathEvaluatesTo("rest-readerread", "//permissions/permission[role-name[. = 'rest-reader'] and capability[. = 'read']]", actual); + XMLAssert.assertXpathEvaluatesTo("rest-writerupdate", "//permissions/permission[role-name[. = 'rest-writer'] and capability[. = 'update']]", actual); + } catch (Exception e) { System.out.println(e.getMessage()); @@ -390,11 +390,12 @@ else if (filename == "cardinal4.xml") { assertXpathEvaluatesTo("2", "count(/*[local-name()='metadata']/*[local-name()='permissions']/*[local-name()='permission'])", actual); assertXpathEvaluatesTo("1", "count(/*[local-name()='metadata']/*[local-name()='permissions']/*[local-name()='permission']/*[local-name()='role-name' and string(.)='rest-reader'])", actual); assertXpathEvaluatesTo("1", "count(/*[local-name()='metadata']/*[local-name()='permissions']/*[local-name()='permission']/*[local-name()='role-name' and string(.)='rest-writer'])", actual); - assertXpathEvaluatesTo("1","count(/*[local-name()='metadata']/*[local-name()='quality' and string(.)='0'])", actual); + assertXpathEvaluatesTo("1","count(/*[local-name()='metadata']/*[local-name()='quality' and string(.)='0'])", actual); - assertXpathEvaluatesTo("read","string(/*[local-name()='metadata']/*[local-name()='permissions']/*[local-name()='permission']/*[[local-name()='role-name' and string(.)='rest-reader'] and [local-name()='capability' and string(.)='read']])", actual); - assertXpathEvaluatesTo("update","string(/*[local-name()='metadata']/*[local-name()='permissions']/*[local-name()='permission']/*[[local-name()='role-name' and string(.)='rest-writer'] and [local-name()='capability' and string(.)='update']])", actual); - } + XMLUnit.getControlDocumentBuilderFactory().setNamespaceAware(false); + XMLAssert.assertXpathEvaluatesTo("rest-readerread", "//permissions/permission[role-name[. = 'rest-reader'] and capability[. = 'read']]", actual); + XMLAssert.assertXpathEvaluatesTo("rest-writerupdate", "//permissions/permission[role-name[. = 'rest-writer'] and capability[. = 'update']]", actual); + } catch (Exception e) { System.out.println(e.getMessage()); From 62cf0681a815a070fa6a8bf915fe654878a7fe72 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Sun, 17 Aug 2014 13:57:43 -0700 Subject: [PATCH 046/357] JSON comparison is not based on string, but based on traversing through an object for comparison --- .../TestQueryOptionBuilderSortOrder.java | 234 ++++++++++++++++-- 1 file changed, 220 insertions(+), 14 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderSortOrder.java b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderSortOrder.java index 60b6aa4dd..b531aee93 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderSortOrder.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderSortOrder.java @@ -1,8 +1,13 @@ package com.marklogic.javaclient; +import java.io.IOException; + import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; import static org.junit.Assert.*; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + import java.io.FileNotFoundException; import javax.xml.namespace.QName; @@ -24,12 +29,14 @@ import com.marklogic.client.io.DOMHandle; import com.marklogic.client.io.QueryOptionsHandle; import com.marklogic.client.io.StringHandle; + import org.junit.*; public class TestQueryOptionBuilderSortOrder extends BasicJavaClientREST { private static String dbName = "TestQueryOptionBuilderSortOrderDB"; private static String [] fNames = {"TestQueryOptionBuilderSortOrderDB-1"}; private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; @BeforeClass public static void setUp() throws Exception { @@ -37,7 +44,13 @@ public class TestQueryOptionBuilderSortOrder extends BasicJavaClientREST { setupJavaRESTServer(dbName, fNames[0], restServerName,8011); setupAppServicesConstraint(dbName); } - + +@After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } @SuppressWarnings("deprecation") @Test public void testSortOrderDescendingScore() throws FileNotFoundException, XpathException, TransformerException @@ -107,7 +120,7 @@ public class TestQueryOptionBuilderSortOrder extends BasicJavaClientREST { @SuppressWarnings("deprecation") -@Test public void testSortOrderPrimaryDescScoreSecondaryAscDate() throws FileNotFoundException, XpathException, TransformerException +@Test public void testSortOrderPrimaryDescScoreSecondaryAscDate() throws FileNotFoundException, XpathException, TransformerException, IOException { System.out.println("Running testSortOrderPrimaryDescScoreSecondaryAscDate"); @@ -147,10 +160,64 @@ public class TestQueryOptionBuilderSortOrder extends BasicJavaClientREST { String output = readHandle.get(); System.out.println(output + "testSortOrderPrimaryDescScoreSecondaryAscDate"); - //String expectedOutput = "{\"options\":{\"sort-order\":[{\"direction\":\"descending\", \"score\":null},{\"direction\":\"ascending\", \"type\":\"xs:date\", \"element\":{\"name\":\"date\",\"ns\":\"http:\\/\\/purl.org\\/dc\\/elements\\/1.1\\/\"}}], \"return-metrics\":false, \"return-qtext\":false, \"transform-results\":{\"apply\":\"raw\"}}}"; - String expectedOutput = "{\"options\":{\"return-metrics\":false, \"return-qtext\":false, \"sort-order\":[{\"direction\":\"descending\", \"score\":null}, {\"type\":\"xs:date\", \"direction\":\"ascending\", \"element\":{\"ns\":\"http:\\/\\/purl.org\\/dc\\/elements\\/1.1\\/\", \"name\":\"date\"}}], \"transform-results\":{\"apply\":\"raw\"}}}"; - assertTrue("Query Options in json is incorrect", output.contains(expectedOutput)); - + // ----------- Validate options node ---------------------- + ObjectMapper mapper = new ObjectMapper(); + JsonNode optionsContent = mapper.readTree(output); + assertNotNull(optionsContent); + System.out.println("JSON output: " + optionsContent); + + JsonNode optionsNode = optionsContent.get("options"); + assertNotNull(optionsNode); + + JsonNode metrics = optionsNode.get("return-metrics"); + assertNotNull(metrics); + assertEquals(metrics.booleanValue(), false); + + JsonNode qtext = optionsNode.get("return-qtext"); + assertNotNull(qtext); + assertEquals(qtext.booleanValue(), false); + + JsonNode sortOrders = optionsNode.get("sort-order"); + assertNotNull(sortOrders); + System.out.println(optionsContent.get("options").get("sort-order")); + + assertTrue(sortOrders.isArray()); + for (final JsonNode sortOrder : sortOrders) { + assertNotNull(sortOrder.get("direction")); + String direction = sortOrder.get("direction").textValue(); + + if (direction.equals("descending")) { + assertTrue(sortOrder.has("score")); + assertTrue(sortOrder.get("score").isNull()); + } + else if (direction.equals("ascending")) { + assertTrue(sortOrder.has("type")); + assertEquals(sortOrder.get("type").textValue(), "xs:date"); + + JsonNode element = sortOrder.get("element"); + assertNotNull(element); + + JsonNode name = element.get("name"); + assertNotNull(name); + assertEquals(name.textValue(), "date"); + + JsonNode ns = element.get("ns"); + assertNotNull(ns); + assertEquals(ns.textValue(), "http://purl.org/dc/elements/1.1/"); + } + else { + assertTrue("Found an unexpected object", false); + } + } + + JsonNode transformResults = optionsNode.get("transform-results"); + assertNotNull(transformResults); + + JsonNode apply = transformResults.get("apply"); + assertNotNull(apply); + assertEquals(apply.textValue(), "raw"); + + // ----------- Validate search using options node created ---------------------- // create query manager QueryManager queryMgr = client.newQueryManager(); @@ -165,7 +232,6 @@ public class TestQueryOptionBuilderSortOrder extends BasicJavaClientREST { // get the result Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); assertXpathEvaluatesTo("3", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); @@ -178,7 +244,7 @@ public class TestQueryOptionBuilderSortOrder extends BasicJavaClientREST { @SuppressWarnings("deprecation") -@Test public void testMultipleSortOrder() throws FileNotFoundException, XpathException, TransformerException +@Test public void testMultipleSortOrder() throws FileNotFoundException, XpathException, TransformerException, IOException { System.out.println("Running testMultipleSortOrder"); @@ -220,9 +286,83 @@ public class TestQueryOptionBuilderSortOrder extends BasicJavaClientREST { String output = readHandle.get(); System.out.println(output); - String expectedOutput = "{\"options\":{\"return-metrics\":false, \"return-qtext\":false, \"sort-order\":[{\"direction\":\"descending\", \"score\":null}, {\"type\":\"xs:int\", \"direction\":\"ascending\", \"element\":{\"ns\":\"\", \"name\":\"popularity\"}}, {\"type\":\"xs:string\", \"direction\":\"descending\", \"element\":{\"ns\":\"\", \"name\":\"title\"}}], \"transform-results\":{\"apply\":\"raw\"}}}"; - assertTrue("Query Options in json is incorrect", output.contains(expectedOutput)); - + // ----------- Validate options node ---------------------- + ObjectMapper mapper = new ObjectMapper(); + JsonNode optionsContent = mapper.readTree(output); + assertNotNull(optionsContent); + System.out.println("JSON output: " + optionsContent); + + JsonNode optionsNode = optionsContent.get("options"); + assertNotNull(optionsNode); + + JsonNode metrics = optionsNode.get("return-metrics"); + assertNotNull(metrics); + assertEquals(metrics.booleanValue(), false); + + JsonNode qtext = optionsNode.get("return-qtext"); + assertNotNull(qtext); + assertEquals(qtext.booleanValue(), false); + + JsonNode sortOrders = optionsNode.get("sort-order"); + assertNotNull(sortOrders); + System.out.println(optionsContent.get("options").get("sort-order")); + + assertTrue(sortOrders.isArray()); + for (final JsonNode sortOrder : sortOrders) { + assertNotNull(sortOrder.get("direction")); + String direction = sortOrder.get("direction").textValue(); + + if (direction.equals("descending")) { + if (sortOrder.has("score")) { + assertTrue(sortOrder.get("score").isNull()); + } + else if (sortOrder.has("type")) { + assertEquals(sortOrder.get("type").textValue(), "xs:string"); + + JsonNode element = sortOrder.get("element"); + assertNotNull(element); + + JsonNode elementName = element.get("name"); + assertNotNull(elementName); + assertEquals(elementName.textValue(), "title"); + + JsonNode elementNS = element.get("ns"); + assertNotNull(elementNS); + assertEquals(elementNS.textValue(), ""); + } + else { + assertTrue("Found an unexpected object", false); + } + + } + else if (direction.equals("ascending")) { + assertTrue(sortOrder.has("type")); + assertEquals(sortOrder.get("type").textValue(), "xs:int"); + + JsonNode element = sortOrder.get("element"); + assertNotNull(element); + + JsonNode elementName = element.get("name"); + assertNotNull(elementName); + assertEquals(elementName.textValue(), "popularity"); + + JsonNode elementNS = element.get("ns"); + assertNotNull(elementNS); + assertEquals(elementNS.textValue(), ""); + } + else { + assertTrue("Found an unexpected object", false); + } + } + + JsonNode transformResults = optionsNode.get("transform-results"); + assertNotNull(transformResults); + + JsonNode apply = transformResults.get("apply"); + assertNotNull(apply); + assertEquals(apply.textValue(), "raw"); + + // ----------- Search based on options node inserted ---------------------- // create query manager QueryManager queryMgr = client.newQueryManager(); @@ -251,7 +391,7 @@ public class TestQueryOptionBuilderSortOrder extends BasicJavaClientREST { @SuppressWarnings("deprecation") -@Test public void testSortOrderAttribute() throws FileNotFoundException, XpathException, TransformerException +@Test public void testSortOrderAttribute() throws FileNotFoundException, XpathException, TransformerException, IOException { System.out.println("Running testSortOrderAttribute"); @@ -292,9 +432,75 @@ public class TestQueryOptionBuilderSortOrder extends BasicJavaClientREST { String output = readHandle.get(); System.out.println(output); - String expectedOutput = "{\"options\":{\"return-metrics\":false, \"return-qtext\":false, \"sort-order\":[{\"direction\":\"descending\", \"score\":null}, {\"type\":\"xs:decimal\", \"direction\":\"ascending\", \"attribute\":{\"ns\":\"\", \"name\":\"amt\"}, \"element\":{\"ns\":\"http:\\/\\/cloudbank.com\", \"name\":\"price\"}}], \"transform-results\":{\"apply\":\"raw\"}}}"; - assertTrue("Query Options in json is incorrect", output.contains(expectedOutput)); + // ----------- Validate options node ---------------------- + ObjectMapper mapper = new ObjectMapper(); + JsonNode optionsContent = mapper.readTree(output); + assertNotNull(optionsContent); + System.out.println("JSON output: " + optionsContent); + + JsonNode optionsNode = optionsContent.get("options"); + assertNotNull(optionsNode); + + JsonNode metrics = optionsNode.get("return-metrics"); + assertNotNull(metrics); + assertEquals(metrics.booleanValue(), false); + + JsonNode qtext = optionsNode.get("return-qtext"); + assertNotNull(qtext); + assertEquals(qtext.booleanValue(), false); + + JsonNode sortOrders = optionsNode.get("sort-order"); + assertNotNull(sortOrders); + System.out.println(optionsContent.get("options").get("sort-order")); + + assertTrue(sortOrders.isArray()); + for (final JsonNode sortOrder : sortOrders) { + assertNotNull(sortOrder.get("direction")); + String direction = sortOrder.get("direction").textValue(); + + if (direction.equals("descending")) { + assertTrue(sortOrder.has("score")); + assertTrue(sortOrder.get("score").isNull()); + } + else if (direction.equals("ascending")) { + assertTrue(sortOrder.has("type")); + assertEquals(sortOrder.get("type").textValue(), "xs:decimal"); + + JsonNode attribute = sortOrder.get("attribute"); + assertNotNull(attribute); + + JsonNode attributeName = attribute.get("name"); + assertNotNull(attributeName); + assertEquals(attributeName.textValue(), "amt"); + + JsonNode attributeNS = attribute.get("ns"); + assertNotNull(attributeNS); + assertEquals(attributeNS.textValue(), ""); + + JsonNode element = sortOrder.get("element"); + assertNotNull(element); + + JsonNode elementName = element.get("name"); + assertNotNull(elementName); + assertEquals(elementName.textValue(), "price"); + + JsonNode elementNS = element.get("ns"); + assertNotNull(elementNS); + assertEquals(elementNS.textValue(), "http://cloudbank.com"); + } + else { + assertTrue("Found an unexpected object", false); + } + } + + JsonNode transformResults = optionsNode.get("transform-results"); + assertNotNull(transformResults); + + JsonNode apply = transformResults.get("apply"); + assertNotNull(apply); + assertEquals(apply.textValue(), "raw"); + // ----------- Validate search using options node created ---------------------- // create query manager QueryManager queryMgr = client.newQueryManager(); From 31b523eb1c231c52a111496da283acb7a537dcc4 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Sun, 17 Aug 2014 14:25:45 -0700 Subject: [PATCH 047/357] Output in testRawCombinedQueryFieldJSON was made to be the same as what is in svn --- .../com/marklogic/javaclient/TestRawCombinedQuery.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQuery.java b/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQuery.java index 12702329a..601f3df7e 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQuery.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQuery.java @@ -753,10 +753,10 @@ public void testCleanUp() throws Exception String resultDoc = resultsHandle.get(); System.out.println(resultDoc); - - assertTrue("total document returned is incorrect", resultDoc.contains("\"total\":2")); - assertTrue("returned doc is incorrect", resultDoc.contains("\"uri\":\"/field-constraint/constraint5.xml\"")); - assertTrue("returned doc is incorrect", resultDoc.contains("\"uri\":\"/field-constraint/constraint1.xml\"")); + + assertTrue("total document returned is incorrect", resultDoc.contains("total=\"2\"")); + assertTrue("returned doc is incorrect", resultDoc.contains("uri=\"/field-constraint/constraint5.xml\"")); + assertTrue("returned doc is incorrect", resultDoc.contains("uri=\"/field-constraint/constraint1.xml\"")); // release client client.release(); From bb32623335a16bb99e2d80dd1f671bc16f7434a6 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Sun, 17 Aug 2014 14:29:35 -0700 Subject: [PATCH 048/357] Expected Output in testRawCombinedQueryComboJSON made same as svn --- .../com/marklogic/javaclient/TestRawCombinedQuery.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQuery.java b/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQuery.java index 601f3df7e..d81af0a45 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQuery.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQuery.java @@ -702,10 +702,9 @@ public void testCleanUp() throws Exception String resultDoc = resultsHandle.get(); System.out.println(resultDoc); - - assertTrue("total document returned is incorrect", resultDoc.contains("\"total\":1")); - assertTrue("returned doc is incorrect", resultDoc.contains("\"uri\":\"/collection-constraint/constraint1.xml\"")); - + + assertTrue("Returned result is not correct", resultDoc.contains("")); + // release client client.release(); } From 715098f58a0ab4234b5f7d2b8734b0eaef1303b8 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Sun, 17 Aug 2014 14:36:23 -0700 Subject: [PATCH 049/357] JSON tests expected output was made same as svn --- .../javaclient/TestRawCombinedQueryGeo.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQueryGeo.java b/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQueryGeo.java index 1af6113fd..8248d4afa 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQueryGeo.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQueryGeo.java @@ -32,6 +32,7 @@ public class TestRawCombinedQueryGeo extends BasicJavaClientREST { private static String dbName = "TestRawCombinedQueryGeoDB"; private static String [] fNames = {"TestRawCombinedQueryGeoDB-1"}; private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort =8011; @BeforeClass public static void setUp() throws Exception { @@ -39,7 +40,13 @@ public class TestRawCombinedQueryGeo extends BasicJavaClientREST { setupJavaRESTServer(dbName, fNames[0],restServerName,8011); setupAppServicesGeoConstraint(dbName); } - + +@After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } @SuppressWarnings("deprecation") @Test public void testRawCombinedQueryGeo() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException @@ -123,11 +130,11 @@ public class TestRawCombinedQueryGeo extends BasicJavaClientREST { String resultDoc = resultsHandle.get(); System.out.println(resultDoc); + + assertTrue("total document returned is incorrect", resultDoc.contains("total=\"1\"")); + assertTrue("returned doc is incorrect", resultDoc.contains("uri=\"/geo-constraint/geo-constraint1.xml\"")); + assertTrue("matched text is incorrect", resultDoc.contains("karl_kara 12,5 12,5 12 5")); - assertTrue("total document returned is incorrect", resultDoc.contains("\"total\":1")); - assertTrue("returned doc is incorrect", resultDoc.contains("\"uri\":\"/geo-constraint/geo-constraint1.xml\"")); - assertTrue("matched text is incorrect", resultDoc.contains("\"match-text\":[\"karl_kara 12,5 12,5 12 5\"]")); - // release client client.release(); } @@ -172,8 +179,8 @@ public class TestRawCombinedQueryGeo extends BasicJavaClientREST { System.out.println(resultDoc); - assertTrue("total document returned is incorrect", resultDoc.contains("\"total\":1")); - assertTrue("returned doc is incorrect", resultDoc.contains("\"uri\":\"/geo-constraint/geo-constraint20.xml\"")); + assertTrue("total document returned is incorrect", resultDoc.contains("total=\"1\"")); + assertTrue("returned doc is incorrect", resultDoc.contains("uri=\"/geo-constraint/geo-constraint20.xml\"")); // release client client.release(); From 7b2a1128d4216963c3752589b1d078155a13b552 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Sun, 17 Aug 2014 15:57:50 -0700 Subject: [PATCH 050/357] Clearing DB after every test --- .../com/marklogic/javaclient/TestValueConstraint.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestValueConstraint.java b/test-complete/src/test/java/com/marklogic/javaclient/TestValueConstraint.java index b3d7ef816..c03b1284f 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestValueConstraint.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestValueConstraint.java @@ -18,6 +18,7 @@ import com.marklogic.client.io.ReaderHandle; import com.marklogic.client.io.SearchHandle; import com.marklogic.client.io.StringHandle; + import org.junit.*; public class TestValueConstraint extends BasicJavaClientREST { @@ -26,12 +27,19 @@ public class TestValueConstraint extends BasicJavaClientREST { private static String dbName = "ValueConstraintDB"; private static String [] fNames = {"ValueConstraintDB-1"}; private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; @BeforeClass public static void setUp() throws Exception { System.out.println("In setup"); setupJavaRESTServer(dbName, fNames[0], restServerName,8011); } +@After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } @SuppressWarnings("deprecation") @Test public void testElementValueConstraint() throws FileNotFoundException From 0f49549e355efdef12265cc1e07d5c863d2b62db Mon Sep 17 00:00:00 2001 From: gvaidees Date: Sun, 17 Aug 2014 16:22:10 -0700 Subject: [PATCH 051/357] Writing via port 8011 instead of 8012 (same as what svn does) --- .../src/test/java/com/marklogic/javaclient/ThreadClass.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/ThreadClass.java b/test-complete/src/test/java/com/marklogic/javaclient/ThreadClass.java index 8e48c8bd9..864869b5c 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/ThreadClass.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/ThreadClass.java @@ -14,7 +14,7 @@ public class ThreadClass extends Thread{ public void run() { - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8013, "rest-admin", "x", Authentication.DIGEST); + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); TextDocumentManager docMgr = client.newTextDocumentManager(); From 39047a0802e723e9265b4d906226bfe3096b185d Mon Sep 17 00:00:00 2001 From: gvaidees Date: Sun, 17 Aug 2014 16:22:51 -0700 Subject: [PATCH 052/357] Clearinf DB after every test --- .../com/marklogic/javaclient/TestMultithreading.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestMultithreading.java b/test-complete/src/test/java/com/marklogic/javaclient/TestMultithreading.java index ab1ee3e09..51974c2b2 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestMultithreading.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestMultithreading.java @@ -6,17 +6,27 @@ import com.marklogic.client.DatabaseClientFactory; import com.marklogic.client.DatabaseClientFactory.Authentication; import com.marklogic.client.document.TextDocumentManager; + import org.junit.*; public class TestMultithreading extends BasicJavaClientREST { private static String dbName = "TestMultithreadingDB"; private static String [] fNames = {"TestMultithreadingDBDB-1"}; private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort=8011; + @BeforeClass public static void setUp() throws Exception { System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupJavaRESTServer(dbName, fNames[0], restServerName, restPort); + } + +@After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); } @SuppressWarnings("deprecation") From 11093f5fd1608236d78ff6885ebadd85d8c949eb Mon Sep 17 00:00:00 2001 From: gvaidees Date: Mon, 18 Aug 2014 10:07:17 -0700 Subject: [PATCH 053/357] Clearing DB after every test --- .../TestQueryOptionBuilderTransformResults.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderTransformResults.java b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderTransformResults.java index 9b06ff262..c6d114e8c 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderTransformResults.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderTransformResults.java @@ -23,12 +23,14 @@ import com.marklogic.client.DatabaseClientFactory.Authentication; import com.marklogic.client.io.QueryOptionsHandle; import com.marklogic.client.io.StringHandle; + import org.junit.*; public class TestQueryOptionBuilderTransformResults extends BasicJavaClientREST { private static String dbName = "TestQueryOptionBuilderTransformResultsDB"; private static String [] fNames = {"TestQueryOptionBuilderTransformResultsDB-1"}; private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; @BeforeClass public static void setUp() throws Exception { @@ -36,7 +38,13 @@ public class TestQueryOptionBuilderTransformResults extends BasicJavaClientREST setupJavaRESTServer(dbName, fNames[0], restServerName,8011); setupAppServicesConstraint(dbName); } - + +@After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } @SuppressWarnings("deprecation") @Test public void testTransformResuleWithSnippetFunction() throws FileNotFoundException, XpathException, TransformerException From 6d8c2d26b0f0619888b0fb6c2376ff0af700fc16 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Mon, 18 Aug 2014 11:33:30 -0700 Subject: [PATCH 054/357] Clearing DB after every test --- .../com/marklogic/javaclient/TestSearchMultibyte.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestSearchMultibyte.java b/test-complete/src/test/java/com/marklogic/javaclient/TestSearchMultibyte.java index c366d230b..716cab378 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestSearchMultibyte.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestSearchMultibyte.java @@ -6,6 +6,7 @@ import javax.xml.transform.TransformerException; import com.marklogic.client.query.QueryManager; + import org.w3c.dom.Document; import org.xml.sax.SAXException; @@ -16,6 +17,7 @@ import com.marklogic.client.io.DOMHandle; import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; + import org.custommonkey.xmlunit.exceptions.XpathException; import org.junit.*; public class TestSearchMultibyte extends BasicJavaClientREST { @@ -23,6 +25,7 @@ public class TestSearchMultibyte extends BasicJavaClientREST { private static String dbName = "TestSearchMultibyteDB"; private static String [] fNames = {"TestSearchMultibyteDB-1"}; private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; @BeforeClass public static void setUp() throws Exception { @@ -30,8 +33,13 @@ public class TestSearchMultibyte extends BasicJavaClientREST { setupJavaRESTServer(dbName, fNames[0], restServerName,8011); setupAppServicesConstraint(dbName); } - +@After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } @Test public void testSearchString() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException { From a6f449d3a4e6d7adfd4642f092300d47c17ee665 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Mon, 18 Aug 2014 11:36:10 -0700 Subject: [PATCH 055/357] Clearing DB after every test --- .../com/marklogic/javaclient/TestSearchSuggestion.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestSearchSuggestion.java b/test-complete/src/test/java/com/marklogic/javaclient/TestSearchSuggestion.java index ec9ddb990..79cc32295 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestSearchSuggestion.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestSearchSuggestion.java @@ -3,17 +3,20 @@ import static org.junit.Assert.*; import java.io.FileNotFoundException; + import com.marklogic.client.query.QueryManager; import com.marklogic.client.query.SuggestDefinition; import com.marklogic.client.DatabaseClient; import com.marklogic.client.DatabaseClientFactory; import com.marklogic.client.DatabaseClientFactory.Authentication; + import org.junit.*; public class TestSearchSuggestion extends BasicJavaClientREST { private static String dbName = "SearchSuggestionDB"; private static String [] fNames = {"SearchSuggestionDB-1"}; private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; @BeforeClass public static void setUp() throws Exception { @@ -25,6 +28,13 @@ public class TestSearchSuggestion extends BasicJavaClientREST { addRangeElementIndex(dbName, "string", "http://noun/", "title", "http://marklogic.com/collation/"); } +@After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + @SuppressWarnings("deprecation") @Test public void testSearchSuggestion() throws FileNotFoundException From cd2c422d87b16ad1a07ed1fd3619a40985518ebf Mon Sep 17 00:00:00 2001 From: gvaidees Date: Mon, 18 Aug 2014 13:17:09 -0700 Subject: [PATCH 056/357] Using correct port for test --- .../marklogic/javaclient/TestSSLConnection.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestSSLConnection.java b/test-complete/src/test/java/com/marklogic/javaclient/TestSSLConnection.java index 416e358b0..8a26be3a0 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestSSLConnection.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestSSLConnection.java @@ -31,6 +31,7 @@ import com.marklogic.client.io.DocumentMetadataHandle; import com.marklogic.client.io.QueryOptionsHandle; import com.marklogic.client.io.StringHandle; + import org.junit.*; public class TestSSLConnection extends BasicJavaClientREST { @@ -38,10 +39,11 @@ public class TestSSLConnection extends BasicJavaClientREST { private static String [] fNames = {"TestSSLConnectionDB-1"}; private static String restServerName = "REST-Java-Client-API-SSL-Server"; - protected void setUp() throws Exception +@BeforeClass + public static void setUp() throws Exception { System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0],restServerName,8033); + setupJavaRESTServer(dbName, fNames[0],restServerName,8012); setupAppServicesConstraint(dbName); } @@ -286,7 +288,7 @@ public X509Certificate[] getAcceptedIssuers() { @SuppressWarnings("deprecation") @Test public void testSSLConnectionInvalidPassword() throws IOException, NoSuchAlgorithmException, KeyManagementException { - System.out.println("Running testSSLConnectionInvalidPassword"); + System.out.println("Running testSSLConnectionInvalidPassword"); String filename = "facebook-10443244874876159931"; @@ -324,6 +326,7 @@ public X509Certificate[] getAcceptedIssuers() { } catch (Exception e) { exception = e.toString(); } + System.out.println("Actual exception: " + exception); boolean isExceptionThrown = exception.contains(expectedException); assertTrue("Exception is not thrown", isExceptionThrown); @@ -381,8 +384,9 @@ public X509Certificate[] getAcceptedIssuers() { // release client client.release(); } - - public void tearDown() throws Exception + + @AfterClass + public static void tearDown() throws Exception { System.out.println("In tear down"); tearDownJavaRESTServer(dbName, fNames, restServerName); From 7564358946cf70832f98c2d730d1b8234c4900dd Mon Sep 17 00:00:00 2001 From: gvaidees Date: Mon, 18 Aug 2014 13:17:34 -0700 Subject: [PATCH 057/357] Clearing DB after each test --- .../java/com/marklogic/javaclient/TestBug18993.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBug18993.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBug18993.java index 863c21dc6..b0334ff66 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBug18993.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBug18993.java @@ -10,12 +10,15 @@ import com.marklogic.client.document.XMLDocumentManager; import com.marklogic.client.io.StringHandle; import com.marklogic.javaclient.BasicJavaClientREST; + import org.junit.*; public class TestBug18993 extends BasicJavaClientREST { private static String dbName = "Bug18993DB"; private static String [] fNames = {"Bug18993DB-1"}; private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + @BeforeClass public static void setUp() throws Exception { @@ -24,6 +27,13 @@ public static void setUp() throws Exception loadBug18993(); } +@After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + @SuppressWarnings("deprecation") @Test public void testBug18993() throws IOException From 049082d6408d5986ebdbde8fb76b3e19f9c5ab93 Mon Sep 17 00:00:00 2001 From: sravan Date: Mon, 18 Aug 2014 18:49:22 -0700 Subject: [PATCH 058/357] Adding tests for keyvalue def and structured def --- .../TestBulkSearchWithKeyValQueryDef.java | 351 ++++++++++++++++++ 1 file changed, 351 insertions(+) create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithKeyValQueryDef.java diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithKeyValQueryDef.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithKeyValQueryDef.java new file mode 100644 index 000000000..90a41292b --- /dev/null +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithKeyValQueryDef.java @@ -0,0 +1,351 @@ +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.IOException; +import java.util.HashMap; + +import javax.xml.namespace.QName; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.xml.sax.SAXException; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.Transaction; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.DocumentPage; +import com.marklogic.client.document.DocumentRecord; +import com.marklogic.client.document.DocumentWriteSet; +import com.marklogic.client.document.JSONDocumentManager; +import com.marklogic.client.document.TextDocumentManager; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.document.DocumentManager.Metadata; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.DocumentMetadataHandle; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.JacksonHandle; +import com.marklogic.client.io.SearchHandle; +import com.marklogic.client.io.StringHandle; +import com.marklogic.client.io.DocumentMetadataHandle.Capability; +import com.marklogic.client.query.KeyValueQueryDefinition; +import com.marklogic.client.query.MatchDocumentSummary; +import com.marklogic.client.query.MatchLocation; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.StringQueryDefinition; +import com.marklogic.client.query.QueryManager.QueryView; + +public class TestBulkSearchWithKeyValQueryDef extends BasicJavaClientREST { + + private static final int BATCH_SIZE=100; + private static final String DIRECTORY ="/bulkSearch/"; + private static String dbName = "TestBulkSearchKVDDB"; + private static String [] fNames = {"TestBulkSearchKVDDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client ; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + setupAppServicesConstraint(dbName); + createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + System.out.println("In tear down" ); + tearDownJavaRESTServer(dbName, fNames, restServerName); + deleteRESTUser("usr1"); + } + + @Before + public void setUp() throws Exception { + System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + // create new connection for each test below + client = DatabaseClientFactory.newClient("localhost", restPort, "usr1", "password", Authentication.DIGEST); + } + + @After + public void tearDown() throws Exception { + System.out.println("Running clear script"); + // release client + client.release(); + } + public void loadJSONDocuments() throws JsonProcessingException, IOException{ + int count=1; + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + + HashMap 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); + } + } + public void validateRecord(DocumentRecord record,Format type) { + + assertNotNull("DocumentRecord should never be null", record); + assertNotNull("Document uri should never be null", record.getUri()); + assertTrue("Document uri should start with " + DIRECTORY, record.getUri().startsWith(DIRECTORY)); + assertEquals("All records are expected to be in same format", type, record.getFormat()); + // System.out.println(record.getMimetype()); + + } + public void loadXMLDocuments() throws IOException, ParserConfigurationException, SAXException, TransformerException{ + int count=1; + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + for(int i =0;i<102;i++){ + + writeset.add(DIRECTORY+"foo"+i+".xml", new DOMHandle(getDocumentContent("This is so foo with a bar "+i))); + + if(count%BATCH_SIZE == 0){ + docMgr.write(writeset); + writeset = docMgr.newWriteSet(); + } + count++; + } + if(count%BATCH_SIZE > 0){ + docMgr.write(writeset); + } + } + @Test + public void testBulkSearchKVDwithDifferentPageSizes() throws IOException, ParserConfigurationException, SAXException, TransformerException { + int count; + loadXMLDocuments(); + //Creating a XML document manager for bulk search + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + //using QueryManger for query definition and set the search criteria + QueryManager queryMgr = client.newQueryManager(); + KeyValueQueryDefinition kvqd = queryMgr.newKeyValueDefinition(); + kvqd.put(queryMgr.newElementLocator(new QName("foo")), "bar"); + // set document manager level settings for search response + System.out.println("Default Page length setting on docMgr :"+docMgr.getPageLength()); + docMgr.setPageLength(1); + docMgr.setSearchView(QueryView.RESULTS); + docMgr.setResponseFormat(Format.XML); + assertEquals("format set on document manager","XML",docMgr.getResponseFormat().toString()); + assertEquals("Queryview set on document manager ","RESULTS" ,docMgr.getSearchView().toString()); + assertEquals("Page length ",1,docMgr.getPageLength()); + // Search for documents where content has bar and get first result record, get search handle on it + SearchHandle sh = new SearchHandle(); + DocumentPage page= docMgr.search(kvqd, 0); + // test for page methods + assertEquals("Number of records",1,page.size()); + assertEquals("Starting record in first page ",1,page.getStart()); + assertEquals("Total number of estimated results:",101,page.getTotalSize()); + assertEquals("Total number of estimated pages :",101,page.getTotalPages()); + // till the issue #78 get fixed + assertFalse("Is this First page :",page.isFirstPage());//this is bug + assertFalse("Is this Last page :",page.isLastPage()); + assertTrue("Is this First page has content:",page.hasContent()); + // Need the Issue #75 to be fixed + assertTrue("Is first page has previous page ?",page.hasPreviousPage()); + // + long pageNo=1; + do{ + count=0; + page = docMgr.search(kvqd, pageNo,sh); + if(pageNo >1){ + assertFalse("Is this first Page", page.isFirstPage()); + assertTrue("Is page has previous page ?",page.hasPreviousPage()); + } + while(page.hasNext()){ + DocumentRecord rec = page.next(); + rec.getFormat(); + validateRecord(rec,Format.XML); + // System.out.println(rec.getUri()); + count++; + } + MatchDocumentSummary[] mds= sh.getMatchResults(); + assertEquals("Matched document count",1,mds.length); + //since we set the query view to get only results, facet count supposed be 0 + assertEquals("Matched Facet count",0,sh.getFacetNames().length); + + assertEquals("document count", page.size(),count); + // assertEquals("Page Number #",pageNo,page.getPageNumber()); + pageNo = pageNo + page.getPageSize(); + }while(!page.isLastPage()); + assertTrue("page count is 101 ",pageNo == page.getTotalPages()); + assertTrue("Page has previous page ?",page.hasPreviousPage()); + assertEquals("page size", 1,page.getPageSize()); + assertEquals("document count", 101,page.getTotalSize()); + page= docMgr.search(kvqd, 102); + assertFalse("Page has any records ?",page.hasContent()); + } + //This test is trying to set the setResponse to JSON on DocumentManager and use search handle which only work with XML + @Test(expected = UnsupportedOperationException.class) + public void testBulkSearchKVDwithWrongResponseFormat() throws Exception { + + TextDocumentManager docMgr = client.newTextDocumentManager(); + QueryManager queryMgr = client.newQueryManager(); + StringQueryDefinition qd = queryMgr.newStringDefinition(); + qd.setCriteria("bar"); + docMgr.setResponseFormat(Format.TEXT); + SearchHandle results = new SearchHandle(); + DocumentPage page= docMgr.search(qd, 1,results); + MatchDocumentSummary[] summaries = results.getMatchResults(); + for (MatchDocumentSummary summary : summaries ) { + MatchLocation[] locations = summary.getMatchLocations(); + for (MatchLocation location : locations) { + System.out.println(location.getAllSnippetText()); + // do something with the snippet text + } + } + + } + //This test has set response to JSON and pass StringHandle with format as JSON, expectint it to work, logged an issue 82 + @Test + public void testBulkSearchKVDwithResponseFormatandStringHandle() throws Exception{ + int count =1; + loadJSONDocuments(); + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + + QueryManager queryMgr = client.newQueryManager(); + KeyValueQueryDefinition kvqd = queryMgr.newKeyValueDefinition(); + kvqd.put(queryMgr.newKeyLocator("says"), "woof"); + SearchHandle sh=queryMgr.search(kvqd, new SearchHandle()); + System.out.println(sh.getTotalResults()); + StringQueryDefinition qd = queryMgr.newStringDefinition(); + qd.setCriteria("woof"); + sh=queryMgr.search(qd, new SearchHandle()); + System.out.println(sh.getTotalResults()); + docMgr.setResponseFormat(Format.JSON); + docMgr.setSearchView(QueryView.METADATA); + docMgr.setMetadataCategories(Metadata.PERMISSIONS); + + StringHandle results = new StringHandle().withFormat(Format.JSON); + DocumentPage page= docMgr.search(kvqd, 1,results); + DocumentMetadataHandle mh = new DocumentMetadataHandle(); + while(page.hasNext()){ + DocumentRecord rec = page.next(); + validateRecord(rec,Format.JSON); + docMgr.readMetadata(rec.getUri(),mh); + assertTrue("Records has permissions? ",mh.getPermissions().containsKey("flexrep-eval")); + assertTrue("Record has collections ?",mh.getCollections().isEmpty()); + count++; + } + assertFalse("Search handle contains",results.get().isEmpty()); + + } + //This test is testing SearchView options and search handle + @Test + public void testBulkSearchKVDwithJSONResponseFormat() throws Exception{ + + + loadJSONDocuments(); + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + QueryManager queryMgr = client.newQueryManager(); + KeyValueQueryDefinition kvqd = queryMgr.newKeyValueDefinition(); + kvqd.put(queryMgr.newKeyLocator("says"), "woof"); + docMgr.setResponseFormat(Format.JSON); + + docMgr.setSearchView(QueryView.FACETS); + JacksonHandle jh = new JacksonHandle(); + DocumentPage page= docMgr.search(kvqd, 1,jh); + + // System.out.println(jh.get().toString()); + assertTrue("Searh response has entry for facets",jh.get().has("facets")); + assertTrue("Searh response has entry for facets",jh.get().has("results"));//Issue 84 is tracking this + assertTrue("Searh response has entry for facets",jh.get().has("metrics")); + + docMgr.setSearchView(QueryView.RESULTS); + page= docMgr.search(kvqd, 1,jh); + + assertFalse("Searh response has entry for facets",jh.get().has("facets")); + assertTrue("Searh response has entry for facets",jh.get().has("results")); + assertTrue("Searh response has entry for facets",jh.get().has("metrics"));//Issue 84 is tracking this + + docMgr.setSearchView(QueryView.METADATA); + page= docMgr.search(kvqd, 1,jh); + + assertFalse("Searh response has entry for facets",jh.get().has("facets")); + assertTrue("Searh response has entry for facets",jh.get().has("results")); + assertTrue("Searh response has entry for facets",jh.get().has("metrics")); + + docMgr.setSearchView(QueryView.ALL); + page= docMgr.search(kvqd, 1,jh); + + assertTrue("Searh response has entry for facets",jh.get().has("facets")); + assertTrue("Searh response has entry for facets",jh.get().has("results")); + assertTrue("Searh response has entry for facets",jh.get().has("metrics")); + + queryMgr.setView(QueryView.FACETS); + queryMgr.search(kvqd, jh); + System.out.println(jh.get().toString()); + + } + + //This test is to verify the transactions, verifies the search works with transaction before commit, after rollback and after commit + @Test + public void testBulkSearchKVDwithTransactionsandDOMHandle() throws Exception{ + TextDocumentManager docMgr = client.newTextDocumentManager(); + DOMHandle results = new DOMHandle(); + QueryManager queryMgr = client.newQueryManager(); + KeyValueQueryDefinition kvqd = queryMgr.newKeyValueDefinition(); + kvqd.put(queryMgr.newElementLocator(new QName("foo")), "thought"); + Transaction t= client.openTransaction(); + try{ + this.loadXMLDocuments(); + int count=1; + XMLDocumentManager xmldocMgr = client.newXMLDocumentManager(); + DocumentWriteSet writeset =xmldocMgr.newWriteSet(); + for(int i =0;i<102;i++){ + writeset.add(DIRECTORY+"boo"+i+".xml", new DOMHandle(getDocumentContent("This is so too much thought "+i))); + if(count%BATCH_SIZE == 0){ + xmldocMgr.write(writeset,t); + writeset = xmldocMgr.newWriteSet(); + } + count++; + } + if(count%BATCH_SIZE > 0){ + xmldocMgr.write(writeset,t); + } + count=0; + docMgr.setSearchView(QueryView.RESULTS); + + DocumentPage page= docMgr.search(kvqd, 1,results,t); + while(page.hasNext()){ + DocumentRecord rec = page.next(); + + validateRecord(rec,Format.XML); + count++; + } + assertTrue("Page has conttent :",page.hasContent()); + assertEquals("Total search results before transaction rollback are ","102",results.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total").getNodeValue()); + // System.out.println(results.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total").getNodeValue()); + + }catch(Exception e){ throw e;} + finally{t.rollback();} + + DocumentPage page= docMgr.search(kvqd, 1,results); + assertEquals("Total search results after rollback are ","0",results.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total").getNodeValue()); + + } + + +} From 97946ee0768988a784195c3bc1831c3a405f897a Mon Sep 17 00:00:00 2001 From: sravan Date: Tue, 19 Aug 2014 16:04:54 -0700 Subject: [PATCH 059/357] Adding more tests for raw structured query def --- .../javaclient/TestBulkSearchWithStringQueryDef.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java index 143c32aec..2b0fed570 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java @@ -54,16 +54,16 @@ public class TestBulkSearchWithStringQueryDef extends BasicJavaClientREST{ @BeforeClass public static void setUpBeforeClass() throws Exception { System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); - setupAppServicesConstraint(dbName); - createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + setupAppServicesConstraint(dbName); + createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); } @AfterClass public static void tearDownAfterClass() throws Exception { System.out.println("In tear down" ); - tearDownJavaRESTServer(dbName, fNames, restServerName); - deleteRESTUser("usr1"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + deleteRESTUser("usr1"); } @Before From 28013fb6d6efdccd83dd2f695b8430e8297259e4 Mon Sep 17 00:00:00 2001 From: sravan Date: Tue, 19 Aug 2014 16:12:53 -0700 Subject: [PATCH 060/357] test is commented untill we see a resolution in issue 88 --- .../javaclient/TestBulkSearchWithKeyValQueryDef.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithKeyValQueryDef.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithKeyValQueryDef.java index 90a41292b..c9e45503b 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithKeyValQueryDef.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithKeyValQueryDef.java @@ -53,7 +53,7 @@ public class TestBulkSearchWithKeyValQueryDef extends BasicJavaClientREST { private static String restServerName = "REST-Java-Client-API-Server"; private static int restPort = 8011; private DatabaseClient client ; - +/* this test is commented out untill we make decision in Issue 88 @BeforeClass public static void setUpBeforeClass() throws Exception { System.out.println("In setup"); @@ -347,5 +347,5 @@ public void testBulkSearchKVDwithTransactionsandDOMHandle() throws Exception{ } - +*/ } From 5f974dc70b73952593a061052cee58c9ac9ff0dc Mon Sep 17 00:00:00 2001 From: sravan Date: Tue, 19 Aug 2014 16:41:15 -0700 Subject: [PATCH 061/357] updated with change in user default roles --- .../com/marklogic/javaclient/TestBulkWriteMetadata2.java | 8 ++++---- .../javaclient/TestBulkWriteWithTransactions.java | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata2.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata2.java index 70b229221..30f70dd03 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata2.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata2.java @@ -76,7 +76,7 @@ public static void setUpBeforeClass() throws Exception { System.out.println("In Setup"); setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); createRESTUser("app-user", "password","rest-writer","rest-reader" ); - createRESTUserWithPermissions("usr1", "password",getPermissionNode("eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); + createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); setMaintainLastModified(dbName, true); } @@ -137,8 +137,8 @@ public void validateMetadata(DocumentMetadataHandle mh){ assertTrue("Document properties count", result); // Permissions - String expectedPermissions1 = "size:4|rest-reader:[READ]|eval:[READ]|app-user:[UPDATE, READ]|rest-writer:[UPDATE]|"; - String expectedPermissions2 = "size:4|rest-reader:[READ]|eval:[READ]|app-user:[READ, UPDATE]|rest-writer:[UPDATE]|"; + String expectedPermissions1 = "size:4|flexrep-eval:[READ]|rest-reader:[READ]|app-user:[UPDATE, READ]|rest-writer:[UPDATE]|"; + String expectedPermissions2 = "size:4|flexrep-eval:[READ]|rest-reader:[READ]|app-user:[READ, UPDATE]|rest-writer:[UPDATE]|"; String actualPermissions = getDocumentPermissionsString(permissions); System.out.println(actualPermissions); if(actualPermissions.contains("[UPDATE, READ]")) @@ -170,7 +170,7 @@ public void validateDefaultMetadata(DocumentMetadataHandle mh){ // Permissions - String expectedPermissions1 = "size:3|rest-reader:[READ]|eval:[READ]|rest-writer:[UPDATE]|"; + String expectedPermissions1 = "size:3|flexrep-eval:[READ]|rest-reader:[READ]|rest-writer:[UPDATE]|"; String actualPermissions = getDocumentPermissionsString(permissions); assertEquals("Document permissions difference", expectedPermissions1, actualPermissions); diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteWithTransactions.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteWithTransactions.java index 7e48f17d7..5b06607d3 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteWithTransactions.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteWithTransactions.java @@ -45,7 +45,7 @@ public static void setUpBeforeClass() throws Exception { System.out.println("In Setup"); setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); createRESTUser("app-user", "password","rest-writer","rest-reader" ); - createRESTUserWithPermissions("usr1", "password",getPermissionNode("eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); + createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); } @AfterClass @@ -95,8 +95,8 @@ public void validateMetadata(DocumentMetadataHandle mh){ assertTrue("Document properties count", result); // Permissions - String expectedPermissions1 = "size:4|rest-reader:[READ]|eval:[READ]|app-user:[UPDATE, READ]|rest-writer:[UPDATE]|"; - String expectedPermissions2 = "size:4|rest-reader:[READ]|eval:[READ]|app-user:[READ, UPDATE]|rest-writer:[UPDATE]|"; + String expectedPermissions1 = "size:4|flexrep-eval:[READ]|rest-reader:[READ]|app-user:[UPDATE, READ]|rest-writer:[UPDATE]|"; + String expectedPermissions2 = "size:4|flexrep-eval:[READ]|rest-reader:[READ]|app-user:[READ, UPDATE]|rest-writer:[UPDATE]|"; String actualPermissions = getDocumentPermissionsString(permissions); // System.out.println(actualPermissions); if(actualPermissions.contains("[UPDATE, READ]")) @@ -120,7 +120,7 @@ public void validateDefaultMetadata(DocumentMetadataHandle mh){ // Permissions - String expectedPermissions1 = "size:3|rest-reader:[READ]|eval:[READ]|rest-writer:[UPDATE]|"; + String expectedPermissions1 = "size:3|flexrep-eval:[READ]|rest-reader:[READ]|rest-writer:[UPDATE]|"; String actualPermissions = getDocumentPermissionsString(permissions); assertEquals("Document permissions difference", expectedPermissions1, actualPermissions); From aac9287444d1de145e6cf14a4f65499de2b0ad6b Mon Sep 17 00:00:00 2001 From: gvaidees Date: Tue, 19 Aug 2014 17:15:54 -0700 Subject: [PATCH 062/357] Using port 8011 instead of 8013 --- .../src/test/java/com/marklogic/javaclient/ThreadSearch.java | 2 +- .../src/test/java/com/marklogic/javaclient/ThreadWrite.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/ThreadSearch.java b/test-complete/src/test/java/com/marklogic/javaclient/ThreadSearch.java index 940b168af..38f230620 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/ThreadSearch.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/ThreadSearch.java @@ -19,7 +19,7 @@ public void run() { long totalResults = 0; - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8013, "rest-reader", "x", Authentication.DIGEST); + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-reader", "x", Authentication.DIGEST); QueryManager queryMgr = client.newQueryManager(); StringQueryDefinition querydef = queryMgr.newStringDefinition(null); diff --git a/test-complete/src/test/java/com/marklogic/javaclient/ThreadWrite.java b/test-complete/src/test/java/com/marklogic/javaclient/ThreadWrite.java index e63e02768..1500a61f2 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/ThreadWrite.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/ThreadWrite.java @@ -17,7 +17,7 @@ public void run() { String filename = "flipper.xml"; - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8013, "rest-writer", "x", Authentication.DIGEST); + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); From 8c86c2f7c27ddf1ec239e82a18f3a6fde482471a Mon Sep 17 00:00:00 2001 From: gvaidees Date: Tue, 19 Aug 2014 22:35:21 -0700 Subject: [PATCH 063/357] Restoring back authentication type to digest and default user to nobody in teardown --- .../TestDatabaseAuthentication.java | 235 +++++------ .../TestDatabaseClientConnection.java | 391 +++++++++--------- 2 files changed, 316 insertions(+), 310 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestDatabaseAuthentication.java b/test-complete/src/test/java/com/marklogic/javaclient/TestDatabaseAuthentication.java index 692018ce7..0bd78276f 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestDatabaseAuthentication.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestDatabaseAuthentication.java @@ -1,116 +1,119 @@ -package com.marklogic.javaclient; - -import java.io.IOException; -import java.io.InputStream; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.InputStreamHandle; -import com.marklogic.javaclient.BasicJavaClientREST; - -import org.junit.*; - -import static org.junit.Assert.*; - -public class TestDatabaseAuthentication extends BasicJavaClientREST{ - - - private static String dbName = "DatabaseAuthenticationDB"; - private static String [] fNames = {"DatabaseAuthenticationDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort=8011; - - @BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); - setupAppServicesConstraint(dbName); - - } - - @After - public void testCleanUp() throws Exception - { - clearDB(restPort); - System.out.println("Running clear script"); - } - - - @Test public void testAuthenticationNone() throws IOException - { - setAuthentication("application-level",restServerName); - setDefaultUser("rest-admin",restServerName); - - System.out.println("Running testAuthenticationNone"); - - String filename = "text-original.txt"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011); - - // write doc - writeDocumentUsingStringHandle(client, filename, "/write-text-doc-app-level/", "Text"); - - // read docs - InputStreamHandle contentHandle = readDocumentUsingInputStreamHandle(client, "/write-text-doc-app-level/" + filename, "Text"); - - // get the contents - InputStream fileRead = contentHandle.get(); - - String readContent = convertInputStreamToString(fileRead); - - String expectedContent = "hello world, welcome to java API"; - - assertEquals("Write Text difference", expectedContent.trim(), readContent.trim()); - - // release client - client.release(); - - setAuthentication("digest",restServerName); - setDefaultUser("nobody",restServerName); - } - -@Test public void testAuthenticationBasic() throws IOException - { - setAuthentication("basic",restServerName); - setDefaultUser("rest-writer",restServerName); - - System.out.println("Running testAuthenticationBasic"); - - String filename = "text-original.txt"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.BASIC); - - // write doc - writeDocumentUsingStringHandle(client, filename, "/write-text-doc-basic/", "Text"); - - // read docs - InputStreamHandle contentHandle = readDocumentUsingInputStreamHandle(client, "/write-text-doc-basic/" + filename, "Text"); - - // get the contents - InputStream fileRead = contentHandle.get(); - - String readContent = convertInputStreamToString(fileRead); - - String expectedContent = "hello world, welcome to java API"; - - assertEquals("Write Text difference", expectedContent.trim(), readContent.trim()); - - // release client - client.release(); - - setAuthentication("digest",restServerName); - setDefaultUser("nobody",restServerName); - } - -@AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down" ); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } -} - +package com.marklogic.javaclient; + +import java.io.IOException; +import java.io.InputStream; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.InputStreamHandle; +import com.marklogic.javaclient.BasicJavaClientREST; + +import org.junit.*; + +import static org.junit.Assert.*; + +public class TestDatabaseAuthentication extends BasicJavaClientREST{ + + + private static String dbName = "DatabaseAuthenticationDB"; + private static String [] fNames = {"DatabaseAuthenticationDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort=8011; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + setupAppServicesConstraint(dbName); + + } + + @After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + + + @Test public void testAuthenticationNone() throws IOException + { + setAuthentication("application-level",restServerName); + setDefaultUser("rest-admin",restServerName); + + System.out.println("Running testAuthenticationNone"); + + String filename = "text-original.txt"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011); + + // write doc + writeDocumentUsingStringHandle(client, filename, "/write-text-doc-app-level/", "Text"); + + // read docs + InputStreamHandle contentHandle = readDocumentUsingInputStreamHandle(client, "/write-text-doc-app-level/" + filename, "Text"); + + // get the contents + InputStream fileRead = contentHandle.get(); + + String readContent = convertInputStreamToString(fileRead); + + String expectedContent = "hello world, welcome to java API"; + + assertEquals("Write Text difference", expectedContent.trim(), readContent.trim()); + + // release client + client.release(); + + setAuthentication("digest",restServerName); + setDefaultUser("nobody",restServerName); + } + +@Test public void testAuthenticationBasic() throws IOException + { + setAuthentication("basic",restServerName); + setDefaultUser("rest-writer",restServerName); + + System.out.println("Running testAuthenticationBasic"); + + String filename = "text-original.txt"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.BASIC); + + // write doc + writeDocumentUsingStringHandle(client, filename, "/write-text-doc-basic/", "Text"); + + // read docs + InputStreamHandle contentHandle = readDocumentUsingInputStreamHandle(client, "/write-text-doc-basic/" + filename, "Text"); + + // get the contents + InputStream fileRead = contentHandle.get(); + + String readContent = convertInputStreamToString(fileRead); + + String expectedContent = "hello world, welcome to java API"; + + assertEquals("Write Text difference", expectedContent.trim(), readContent.trim()); + + // release client + client.release(); + + setAuthentication("digest",restServerName); + setDefaultUser("nobody",restServerName); + } + +@AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down" ); + + setAuthentication("digest",restServerName); + setDefaultUser("nobody",restServerName); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} + diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestDatabaseClientConnection.java b/test-complete/src/test/java/com/marklogic/javaclient/TestDatabaseClientConnection.java index 1606fec46..2e09081de 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestDatabaseClientConnection.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestDatabaseClientConnection.java @@ -1,194 +1,197 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.IOException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.javaclient.BasicJavaClientREST; -import org.junit.*; - -public class TestDatabaseClientConnection extends BasicJavaClientREST{ - - private static String dbName = "DatabaeClientConnectionDB"; - private static String [] fNames = {"DatabaeClientConnectionDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - @BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - } - - @SuppressWarnings("deprecation") - @Test - public void testReleasedClient() throws IOException - { - System.out.println("Running testReleasedClient"); - - String filename = "facebook-10443244874876159931"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write doc - writeDocumentUsingStringHandle(client, filename, "/write-text-doc/", "Text"); - - // release client - client.release(); - - String stringException = ""; - - // write doc on released client - try - { - writeDocumentUsingStringHandle(client, filename, "/write-txt-doc-released-client/", "Text"); - } - catch (Exception e) - { - stringException = "Client is not available - " + e; - } - - String expectedException = "Client is not available - java.lang.NullPointerException"; - assertEquals("Exception is not thrown", expectedException, stringException); - } - - @SuppressWarnings("deprecation") - @Test - public void testDatabaseClientConnectionExist() - { - System.out.println("Running testDatabaseClientConnectionExist"); - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-reader", "x", Authentication.DIGEST); - String[] stringClient = client.toString().split("@"); - assertEquals("Object does not exist", "com.marklogic.client.impl.DatabaseClientImpl", stringClient[0]); - - // release client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testDatabaseClientConnectionInvalidPort() throws IOException - { - System.out.println("Running testDatabaseClientConnectionInvalidPort"); - - String filename = "facebook-10443244874876159931"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8033, "rest-reader", "x", Authentication.DIGEST); - - String expectedException = "com.sun.jersey.api.client.ClientHandlerException: org.apache.http.conn.HttpHostConnectException: Connection to http://localhost:8033 refused"; - String exception = ""; - - // write doc - try - { - writeDocumentUsingStringHandle(client, filename, "/write-text-doc/", "Text"); - } - catch (Exception e) { exception = e.toString(); } - - assertEquals("Exception is not thrown", expectedException, exception); - - // release client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testDatabaseClientConnectionInvalidUser() throws IOException - { - System.out.println("Running testDatabaseClientConnectionInvalidUser"); - - String filename = "facebook-10443244874876159931"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "foo-the-bar", "x", Authentication.DIGEST); - - String expectedException = "com.marklogic.client.FailedRequestException: Local message: write failed: Unauthorized. Server Message: Unauthorized"; - String exception = ""; - - // write doc - try - { - writeDocumentUsingStringHandle(client, filename, "/write-text-doc/", "Text"); - } - catch (Exception e) { exception = e.toString(); } - - //System.out.println(exception); - - boolean exceptionIsThrown = exception.contains(expectedException); - assertTrue("Exception is not thrown", exceptionIsThrown); - - // release client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testDatabaseClientConnectionInvalidPassword() throws IOException - { - System.out.println("Running testDatabaseClientConnectionInvalidPassword"); - - String filename = "facebook-10443244874876159931"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "foobar", Authentication.DIGEST); - - String expectedException = "com.marklogic.client.FailedRequestException: Local message: write failed: Unauthorized. Server Message: Unauthorized"; - String exception = ""; - - // write doc - try - { - writeDocumentUsingStringHandle(client, filename, "/write-text-doc/", "Text"); - } - catch (Exception e) { exception = e.toString(); } - - //System.out.println(exception); - - boolean exceptionIsThrown = exception.contains(expectedException); - assertTrue("Exception is not thrown", exceptionIsThrown); - - // release client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testDatabaseClientConnectionInvalidHost() throws IOException - { - System.out.println("Running testDatabaseClientConnectionInvalidHost"); - - String filename = "facebook-10443244874876159931"; - - DatabaseClient client = DatabaseClientFactory.newClient("foobarhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - //String expectedException = "com.sun.jersey.api.client.ClientHandlerException: java.net.UnknownHostException: foobarhost: Name or service not known"; - String expectedException = "UnknownHostException"; - - String exception = ""; - - // write doc - try - { - writeDocumentUsingStringHandle(client, filename, "/write-text-doc/", "Text"); - } - catch (Exception e) { exception = e.toString(); } - - System.out.println(exception); - - assertTrue("Exception is not thrown", exception.contains(expectedException)); - - // release client - client.release(); - } -@AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} - +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.IOException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.javaclient.BasicJavaClientREST; +import org.junit.*; + +public class TestDatabaseClientConnection extends BasicJavaClientREST{ + + private static String dbName = "DatabaeClientConnectionDB"; + private static String [] fNames = {"DatabaeClientConnectionDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + } + + @SuppressWarnings("deprecation") + @Test + public void testReleasedClient() throws IOException + { + System.out.println("Running testReleasedClient"); + + String filename = "facebook-10443244874876159931"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write doc + writeDocumentUsingStringHandle(client, filename, "/write-text-doc/", "Text"); + + // release client + client.release(); + + String stringException = ""; + + // write doc on released client + try + { + writeDocumentUsingStringHandle(client, filename, "/write-txt-doc-released-client/", "Text"); + } + catch (Exception e) + { + stringException = "Client is not available - " + e; + } + + String expectedException = "Client is not available - java.lang.NullPointerException"; + assertEquals("Exception is not thrown", expectedException, stringException); + } + + @SuppressWarnings("deprecation") + @Test + public void testDatabaseClientConnectionExist() + { + System.out.println("Running testDatabaseClientConnectionExist"); + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-reader", "x", Authentication.DIGEST); + String[] stringClient = client.toString().split("@"); + assertEquals("Object does not exist", "com.marklogic.client.impl.DatabaseClientImpl", stringClient[0]); + + // release client + client.release(); + } + + + @SuppressWarnings("deprecation") + @Test public void testDatabaseClientConnectionInvalidPort() throws IOException + { + System.out.println("Running testDatabaseClientConnectionInvalidPort"); + + String filename = "facebook-10443244874876159931"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8033, "rest-reader", "x", Authentication.DIGEST); + + String expectedException = "com.sun.jersey.api.client.ClientHandlerException: org.apache.http.conn.HttpHostConnectException: Connection to http://localhost:8033 refused"; + String exception = ""; + + // write doc + try + { + writeDocumentUsingStringHandle(client, filename, "/write-text-doc/", "Text"); + } + catch (Exception e) { exception = e.toString(); } + + assertEquals("Exception is not thrown", expectedException, exception); + + // release client + client.release(); + } + + + @SuppressWarnings("deprecation") + @Test public void testDatabaseClientConnectionInvalidUser() throws IOException + { + System.out.println("Running testDatabaseClientConnectionInvalidUser"); + + String filename = "facebook-10443244874876159931"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "foo-the-bar", "x", Authentication.DIGEST); + + String expectedException = "com.marklogic.client.FailedRequestException: Local message: write failed: Unauthorized. Server Message: Unauthorized"; + String exception = ""; + + // write doc + try + { + writeDocumentUsingStringHandle(client, filename, "/write-text-doc/", "Text"); + } + catch (Exception e) { exception = e.toString(); } + + //System.out.println(exception); + + boolean exceptionIsThrown = exception.contains(expectedException); + assertTrue("Exception is not thrown", exceptionIsThrown); + + // release client + client.release(); + } + + + @SuppressWarnings("deprecation") + @Test public void testDatabaseClientConnectionInvalidPassword() throws IOException + { + System.out.println("Running testDatabaseClientConnectionInvalidPassword"); + + String filename = "facebook-10443244874876159931"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "foobar", Authentication.DIGEST); + + String expectedException = "com.marklogic.client.FailedRequestException: Local message: write failed: Unauthorized. Server Message: Unauthorized"; + String exception = ""; + + // write doc + try + { + writeDocumentUsingStringHandle(client, filename, "/write-text-doc/", "Text"); + } + catch (Exception e) { exception = e.toString(); } + + //System.out.println(exception); + + boolean exceptionIsThrown = exception.contains(expectedException); + assertTrue("Exception is not thrown", exceptionIsThrown); + + // release client + client.release(); + } + + + @SuppressWarnings("deprecation") + @Test public void testDatabaseClientConnectionInvalidHost() throws IOException + { + System.out.println("Running testDatabaseClientConnectionInvalidHost"); + + String filename = "facebook-10443244874876159931"; + + DatabaseClient client = DatabaseClientFactory.newClient("foobarhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + //String expectedException = "com.sun.jersey.api.client.ClientHandlerException: java.net.UnknownHostException: foobarhost: Name or service not known"; + String expectedException = "UnknownHostException"; + + String exception = ""; + + // write doc + try + { + writeDocumentUsingStringHandle(client, filename, "/write-text-doc/", "Text"); + } + catch (Exception e) { exception = e.toString(); } + + System.out.println(exception); + + assertTrue("Exception is not thrown", exception.contains(expectedException)); + + // release client + client.release(); + } +@AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + + setAuthentication("digest",restServerName); + setDefaultUser("nobody",restServerName); + tearDownJavaRESTServer(dbName, fNames, restServerName); + + } +} + From 6be50cf22de1142dfd5388e5035bd45e4e89e9f3 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Wed, 20 Aug 2014 11:23:11 -0700 Subject: [PATCH 064/357] Fixed a warning on line 122 where the characters are not UTF-8 --- .../TestServerAssignedDocumentURI.java | 610 +++++++++--------- 1 file changed, 305 insertions(+), 305 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestServerAssignedDocumentURI.java b/test-complete/src/test/java/com/marklogic/javaclient/TestServerAssignedDocumentURI.java index db8bae868..e7e8737f5 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestServerAssignedDocumentURI.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestServerAssignedDocumentURI.java @@ -1,305 +1,305 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.File; -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.Source; -import javax.xml.transform.TransformerException; -import javax.xml.transform.stream.StreamSource; - -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.ResourceNotFoundException; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.Transaction; -import com.marklogic.client.admin.TransformExtensionsManager; -import com.marklogic.client.document.DocumentDescriptor; -import com.marklogic.client.document.DocumentUriTemplate; -import com.marklogic.client.document.ServerTransform; -import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.FileHandle; -import com.marklogic.client.io.SourceHandle; -import com.marklogic.client.io.StringHandle; -import org.junit.*; -public class TestServerAssignedDocumentURI extends BasicJavaClientREST { - - private static String dbName = "TestServerAssignedDocumentUriDB"; - private static String [] fNames = {"TestServerAssignedDocumentUri-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0],restServerName,8011); - } - - -@SuppressWarnings("deprecation") -@Test public void testCreate() - { - System.out.println("Running testCreate"); - - String filename = "flipper.xml"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - - // create template - DocumentUriTemplate template = docMgr.newDocumentUriTemplate("xml"); - template.withDirectory("/mytest/create/"); - - // get the file - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - // create doc - DocumentDescriptor desc = docMgr.create(template, handle); - - // get the uri - String docId = desc.getUri(); - System.out.println(docId); - - String content = docMgr.read(desc, new StringHandle()).get(); - System.out.println(content); - - assertTrue("document is not created", content.contains("Flipper")); - - // release the client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testCreateMultibyte() - { - System.out.println("Running testCreateMultibyte"); - - String filename = "flipper.xml"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - - // create template - DocumentUriTemplate template = docMgr.newDocumentUriTemplate("xml"); - template.withDirectory("/里/里/"); - - // get the file - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - String content = null; - try { - // create doc - DocumentDescriptor desc = docMgr.create(template, handle); - - // get the uri - String docId = desc.getUri(); - System.out.println(docId); - - content = docMgr.read(desc, new StringHandle()).get(); - System.out.println(content); - - assertTrue("document is not created", content.contains("Flipper")); - }catch(ResourceNotFoundException e){ - System.out.println("Because of Special Characters in uri'/éâ?¡Å?/éâ?¡Å?/10455375835218157514.xml'it is throwing exception"); - } - // release the client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testCreateInvalidURI() // should be failed - { - System.out.println("Running testCreateInvalidURI"); - - String filename = "flipper.xml"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - - // create template - try{ - DocumentUriTemplate template = docMgr.newDocumentUriTemplate("/"); - template.withDirectory("/mytest/create/"); - - // get the file - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - // create doc - DocumentDescriptor desc = docMgr.create(template, handle); - - // get the uri - String docId = desc.getUri(); - System.out.println(docId); - } - catch(IllegalArgumentException i){ - i.printStackTrace(); - System.out.println("Expected Output"); - } - // release the client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testCreateInvalidDirectory() // Should be failed - { - System.out.println("Running testCreateInvalidDirectory"); - - String filename = "flipper.xml"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - try{ - // create template - DocumentUriTemplate template = docMgr.newDocumentUriTemplate("xml"); - template.withDirectory("/:?#[]@/"); - - // get the file - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - // create doc - DocumentDescriptor desc = docMgr.create(template, handle); - - // get the uri - String docId = desc.getUri(); - System.out.println(docId); - } - catch(IllegalArgumentException i){ - i.printStackTrace(); - assertTrue("Expected error didnt came up",i.toString().contains("Directory is not valid: /:?#[]@/")); - } - // release the client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testCreateWithTransformerTxMetadata() throws TransformerException, ParserConfigurationException, SAXException, IOException - { - System.out.println("Running testCreateWithTransformerTxMetadata"); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // get the doc - Source source = new StreamSource("src/test/java/com/marklogic/javaclient/data/employee.xml"); - - // get the xsl - File file = new File("src/test/java/com/marklogic/javaclient/data/employee-stylesheet.xsl"); - - String xsl = convertFileToString(file); - - // create transform - TransformExtensionsManager extensionMgr = client.newServerConfigManager().newTransformExtensionsManager(); - extensionMgr.writeXSLTransform("somename", new StringHandle().with(xsl)); - ServerTransform transform = new ServerTransform("somename"); - - // release rest-admin client - client.release(); - - // connect the rest-writer client - DatabaseClient client1 = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create a doc manager - XMLDocumentManager docMgr = client1.newXMLDocumentManager(); - - // create template - DocumentUriTemplate template = docMgr.newDocumentUriTemplate("xml"); - template.withDirectory("/mytest/create/transformer/"); - - // create a handle on the content - SourceHandle handle = new SourceHandle(); - handle.set(source); - - // get the original metadata - Document docMetadata = getXMLMetadata("metadata-original.xml"); - - // create handle to write metadata - DOMHandle writeMetadataHandle = new DOMHandle(); - writeMetadataHandle.set(docMetadata); - - // create transaction - Transaction transaction = client1.openTransaction(); - - // create doc - DocumentDescriptor desc = docMgr.create(template, writeMetadataHandle, handle, transform, transaction); - - // get the uri - String docId = desc.getUri(); - System.out.println(docId); - - System.out.println("Before commit:"); - - String exception = ""; - try - { - String content = docMgr.read(desc, new StringHandle()).get(); - System.out.println(content); - } catch (Exception e) - - { - System.out.println(e); - exception = e.toString(); - } - - String expectedException = "com.marklogic.client.ResourceNotFoundException: Local message: Could not read non-existent document. Server Message: RESTAPI-NODOCUMENT"; - assertTrue("Exception is not thrown", exception.contains(expectedException)); - - System.out.println("After commit:"); - transaction.commit(); - - String content1 = docMgr.read(desc, new StringHandle()).get(); - System.out.println(content1); - - assertTrue("document is not created", content1.contains("firstname")); - - // read metadata - String metadataContent = docMgr.readMetadata(docId, new StringHandle()).get(); - System.out.println(metadataContent); - - assertTrue("metadata is not created", metadataContent.contains("MarkLogic")); - - // release client - client1.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.Source; +import javax.xml.transform.TransformerException; +import javax.xml.transform.stream.StreamSource; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.ResourceNotFoundException; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.Transaction; +import com.marklogic.client.admin.TransformExtensionsManager; +import com.marklogic.client.document.DocumentDescriptor; +import com.marklogic.client.document.DocumentUriTemplate; +import com.marklogic.client.document.ServerTransform; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.FileHandle; +import com.marklogic.client.io.SourceHandle; +import com.marklogic.client.io.StringHandle; +import org.junit.*; +public class TestServerAssignedDocumentURI extends BasicJavaClientREST { + + private static String dbName = "TestServerAssignedDocumentUriDB"; + private static String [] fNames = {"TestServerAssignedDocumentUri-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + +@BeforeClass public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0],restServerName,8011); + } + + +@SuppressWarnings("deprecation") +@Test public void testCreate() + { + System.out.println("Running testCreate"); + + String filename = "flipper.xml"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + + // create template + DocumentUriTemplate template = docMgr.newDocumentUriTemplate("xml"); + template.withDirectory("/mytest/create/"); + + // get the file + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + // create doc + DocumentDescriptor desc = docMgr.create(template, handle); + + // get the uri + String docId = desc.getUri(); + System.out.println(docId); + + String content = docMgr.read(desc, new StringHandle()).get(); + System.out.println(content); + + assertTrue("document is not created", content.contains("Flipper")); + + // release the client + client.release(); + } + + +@SuppressWarnings("deprecation") +@Test public void testCreateMultibyte() + { + System.out.println("Running testCreateMultibyte"); + + String filename = "flipper.xml"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + + // create template + DocumentUriTemplate template = docMgr.newDocumentUriTemplate("xml"); + template.withDirectory("/里/里/"); + + // get the file + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + String content = null; + try { + // create doc + DocumentDescriptor desc = docMgr.create(template, handle); + + // get the uri + String docId = desc.getUri(); + System.out.println(docId); + + content = docMgr.read(desc, new StringHandle()).get(); + System.out.println(content); + + assertTrue("document is not created", content.contains("Flipper")); + }catch(ResourceNotFoundException e){ + System.out.println("Because of Special Characters in uri, it is throwing exception"); + } + // release the client + client.release(); + } + + +@SuppressWarnings("deprecation") +@Test public void testCreateInvalidURI() // should be failed + { + System.out.println("Running testCreateInvalidURI"); + + String filename = "flipper.xml"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + + // create template + try{ + DocumentUriTemplate template = docMgr.newDocumentUriTemplate("/"); + template.withDirectory("/mytest/create/"); + + // get the file + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + // create doc + DocumentDescriptor desc = docMgr.create(template, handle); + + // get the uri + String docId = desc.getUri(); + System.out.println(docId); + } + catch(IllegalArgumentException i){ + i.printStackTrace(); + System.out.println("Expected Output"); + } + // release the client + client.release(); + } + + +@SuppressWarnings("deprecation") +@Test public void testCreateInvalidDirectory() // Should be failed + { + System.out.println("Running testCreateInvalidDirectory"); + + String filename = "flipper.xml"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + try{ + // create template + DocumentUriTemplate template = docMgr.newDocumentUriTemplate("xml"); + template.withDirectory("/:?#[]@/"); + + // get the file + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + // create doc + DocumentDescriptor desc = docMgr.create(template, handle); + + // get the uri + String docId = desc.getUri(); + System.out.println(docId); + } + catch(IllegalArgumentException i){ + i.printStackTrace(); + assertTrue("Expected error didnt came up",i.toString().contains("Directory is not valid: /:?#[]@/")); + } + // release the client + client.release(); + } + + +@SuppressWarnings("deprecation") +@Test public void testCreateWithTransformerTxMetadata() throws TransformerException, ParserConfigurationException, SAXException, IOException + { + System.out.println("Running testCreateWithTransformerTxMetadata"); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // get the doc + Source source = new StreamSource("src/test/java/com/marklogic/javaclient/data/employee.xml"); + + // get the xsl + File file = new File("src/test/java/com/marklogic/javaclient/data/employee-stylesheet.xsl"); + + String xsl = convertFileToString(file); + + // create transform + TransformExtensionsManager extensionMgr = client.newServerConfigManager().newTransformExtensionsManager(); + extensionMgr.writeXSLTransform("somename", new StringHandle().with(xsl)); + ServerTransform transform = new ServerTransform("somename"); + + // release rest-admin client + client.release(); + + // connect the rest-writer client + DatabaseClient client1 = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create a doc manager + XMLDocumentManager docMgr = client1.newXMLDocumentManager(); + + // create template + DocumentUriTemplate template = docMgr.newDocumentUriTemplate("xml"); + template.withDirectory("/mytest/create/transformer/"); + + // create a handle on the content + SourceHandle handle = new SourceHandle(); + handle.set(source); + + // get the original metadata + Document docMetadata = getXMLMetadata("metadata-original.xml"); + + // create handle to write metadata + DOMHandle writeMetadataHandle = new DOMHandle(); + writeMetadataHandle.set(docMetadata); + + // create transaction + Transaction transaction = client1.openTransaction(); + + // create doc + DocumentDescriptor desc = docMgr.create(template, writeMetadataHandle, handle, transform, transaction); + + // get the uri + String docId = desc.getUri(); + System.out.println(docId); + + System.out.println("Before commit:"); + + String exception = ""; + try + { + String content = docMgr.read(desc, new StringHandle()).get(); + System.out.println(content); + } catch (Exception e) + + { + System.out.println(e); + exception = e.toString(); + } + + String expectedException = "com.marklogic.client.ResourceNotFoundException: Local message: Could not read non-existent document. Server Message: RESTAPI-NODOCUMENT"; + assertTrue("Exception is not thrown", exception.contains(expectedException)); + + System.out.println("After commit:"); + transaction.commit(); + + String content1 = docMgr.read(desc, new StringHandle()).get(); + System.out.println(content1); + + assertTrue("document is not created", content1.contains("firstname")); + + // read metadata + String metadataContent = docMgr.readMetadata(docId, new StringHandle()).get(); + System.out.println(metadataContent); + + assertTrue("metadata is not created", metadataContent.contains("MarkLogic")); + + // release client + client1.release(); + } + +@AfterClass public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} From d0824310449768813d0579318d2d365e5c6f8937 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Wed, 20 Aug 2014 11:36:53 -0700 Subject: [PATCH 065/357] Using UTF-8 to run tests and excluding 'com/marklogic/javaclient/TestSSLConnection.class' and 'com/marklogic/javaclient/TestBug18993.class' for now (temporarily), since these require setup changes --- test-complete/build.gradle | 41 +++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/test-complete/build.gradle b/test-complete/build.gradle index 2ad90743b..0b968210d 100644 --- a/test-complete/build.gradle +++ b/test-complete/build.gradle @@ -1,16 +1,25 @@ - -/* plugins */ -apply plugin: 'java' - - -/* The code repositories to consult for dependencies */ -repositories { - mavenLocal() - mavenCentral() -} - -dependencies { - compile('com.marklogic:client-api-java:3.0-SNAPSHOT') - compile('xmlunit:xmlunit:1.5') - compile('junit:junit:4.11') -} + +/* plugins */ +apply plugin: 'java' + +test { + tasks.withType(Compile) { + options.encoding = 'UTF-8' + } + + exclude 'com/marklogic/javaclient/TestSSLConnection.class' + exclude 'com/marklogic/javaclient/TestBug18993.class' +} + +/* The code repositories to consult for dependencies */ +repositories { + mavenLocal() + mavenCentral() +} + +dependencies { + compile('com.marklogic:client-api-java:3.0-SNAPSHOT') + compile('xmlunit:xmlunit:1.5') + compile('junit:junit:4.11') +} + From 397e39330165e8cc9332f993f20d7cc6b0a43767 Mon Sep 17 00:00:00 2001 From: sravan Date: Wed, 20 Aug 2014 14:41:29 -0700 Subject: [PATCH 066/357] updating tests with more scenarios --- .../javaclient/TestBulkSearchEWithQBE.java | 183 +++++-- .../TestBulkSearchWithStrucQueryDef.java | 448 ++++++++++++++++++ 2 files changed, 598 insertions(+), 33 deletions(-) create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStrucQueryDef.java diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchEWithQBE.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchEWithQBE.java index d4d94388c..dcaa918ec 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchEWithQBE.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchEWithQBE.java @@ -16,6 +16,7 @@ import org.junit.BeforeClass; import org.junit.Test; import org.w3c.dom.Document; +import org.w3c.dom.Element; import org.xml.sax.SAXException; import com.fasterxml.jackson.core.JsonProcessingException; @@ -39,6 +40,7 @@ import com.marklogic.client.query.MatchDocumentSummary; import com.marklogic.client.query.MatchLocation; import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.RawCombinedQueryDefinition; import com.marklogic.client.query.RawQueryByExampleDefinition; import com.marklogic.client.query.StringQueryDefinition; import com.marklogic.client.query.QueryManager.QueryView; @@ -46,6 +48,8 @@ + + import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; public class TestBulkSearchEWithQBE extends BasicJavaClientREST{ @@ -60,23 +64,26 @@ public class TestBulkSearchEWithQBE extends BasicJavaClientREST{ @BeforeClass public static void setUpBeforeClass() throws Exception { System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); - setupAppServicesConstraint(dbName); - createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); + // setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + // setupAppServicesConstraint(dbName); + // createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); } @AfterClass public static void tearDownAfterClass() throws Exception { System.out.println("In tear down" ); - tearDownJavaRESTServer(dbName, fNames, restServerName); - deleteRESTUser("usr1"); + // tearDownJavaRESTServer(dbName, fNames, restServerName); + // deleteRESTUser("usr1"); } @Before public void setUp() throws Exception { - // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); // create new connection for each test below client = DatabaseClientFactory.newClient("localhost", restPort, "usr1", "password", Authentication.DIGEST); +// loadTxtDocuments(); +// loadXMLDocuments(); +// loadJSONDocuments(); } @After @@ -100,8 +107,11 @@ public void loadXMLDocuments() throws IOException, ParserConfigurationException, XMLDocumentManager docMgr = client.newXMLDocumentManager(); DocumentWriteSet writeset =docMgr.newWriteSet(); for(int i =0;i<102;i++){ - - writeset.add(DIRECTORY+"foo"+i+".xml", new DOMHandle(getDocumentContent("This is so foo with a bar "+i))); + Document doc = this.getDocumentContent("This is so foo with a bar "+i); + Element childElement = doc.createElement("author"); + childElement.appendChild(doc.createTextNode("rhiea")); + doc.getElementsByTagName("foo").item(0).appendChild(childElement); + writeset.add(DIRECTORY+"foo"+i+".xml", new DOMHandle(doc)); if(count%BATCH_SIZE == 0){ docMgr.write(writeset); @@ -129,7 +139,6 @@ public void loadTxtDocuments(){ docMgr.write(writeset); } } - public void loadJSONDocuments() throws JsonProcessingException, IOException{ int count=1; JSONDocumentManager docMgr = client.newJSONDocumentManager(); @@ -138,7 +147,7 @@ public void loadJSONDocuments() throws JsonProcessingException, IOException{ HashMap map= new HashMap(); for(int i =0;i<102;i++){ - JsonNode jn = new ObjectMapper().readTree("{\"animal\":\"dog"+i+"\", \"says\":\"woof\"}"); + JsonNode jn = new ObjectMapper().readTree("{\"animal\":\"dog "+i+"\", \"says\":\"woof\"}"); JacksonHandle jh = new JacksonHandle(); jh.set(jn); writeset.add(DIRECTORY+"dog"+i+".json",jh); @@ -157,27 +166,24 @@ public void loadJSONDocuments() throws JsonProcessingException, IOException{ @Test public void testBulkSearchQBEWithXMLResponseFormat() throws IOException, ParserConfigurationException, SAXException, TransformerException, XpathException { int count; - loadTxtDocuments(); - loadXMLDocuments(); - loadJSONDocuments(); //Creating a xml document manager for bulk search XMLDocumentManager docMgr = client.newXMLDocumentManager(); //using QBE for query definition and set the search criteria - + QueryManager queryMgr = client.newQueryManager(); - String queryAsString = - "foo"; - RawQueryByExampleDefinition qd = queryMgr.newRawQueryByExampleDefinition(new StringHandle(queryAsString)); - + String queryAsString = + "foo"; + RawQueryByExampleDefinition qd = queryMgr.newRawQueryByExampleDefinition(new StringHandle(queryAsString)); + // set document manager level settings for search response docMgr.setPageLength(25); docMgr.setSearchView(QueryView.RESULTS); docMgr.setResponseFormat(Format.XML); - + // Search for documents where content has bar and get first result record, get search handle on it,Use DOMHandle to read results DOMHandle dh = new DOMHandle(); DocumentPage page; - + long pageNo=1; do{ count=0; @@ -190,10 +196,10 @@ public void testBulkSearchQBEWithXMLResponseFormat() throws IOException, ParserC DocumentRecord rec = page.next(); rec.getFormat(); validateRecord(rec,Format.XML); - + count++; } - + Document resultDoc = dh.get(); assertXpathEvaluatesTo("xml", "string(//*[local-name()='result'][last()]//@*[local-name()='format'])", resultDoc); assertEquals("document count", page.size(),count); @@ -204,31 +210,29 @@ public void testBulkSearchQBEWithXMLResponseFormat() throws IOException, ParserC assertTrue("Page has previous page ?",page.hasPreviousPage()); assertEquals("page size", 25,page.getPageSize()); assertEquals("document count", 102,page.getTotalSize()); - + } @Test public void testBulkSearchQBEWithJSONResponseFormat() throws IOException, ParserConfigurationException, SAXException, TransformerException { int count; - loadTxtDocuments(); - loadXMLDocuments(); - loadJSONDocuments(); + //Creating a xml document manager for bulk search XMLDocumentManager docMgr = client.newXMLDocumentManager(); //using QBE for query definition and set the search criteria - + QueryManager queryMgr = client.newQueryManager(); String queryAsString = "{\"$query\": { \"says\": {\"$word\":\"woof\",\"$exact\": false}}}"; - RawQueryByExampleDefinition qd = queryMgr.newRawQueryByExampleDefinition(new StringHandle(queryAsString).withFormat(Format.JSON)); - + RawQueryByExampleDefinition qd = queryMgr.newRawQueryByExampleDefinition(new StringHandle(queryAsString).withFormat(Format.JSON)); + // set document manager level settings for search response docMgr.setPageLength(25); docMgr.setSearchView(QueryView.RESULTS); docMgr.setResponseFormat(Format.JSON); - + // Search for documents where content has bar and get first result record, get search handle on it,Use DOMHandle to read results JacksonHandle sh = new JacksonHandle(); DocumentPage page; - + long pageNo=1; do{ count=0; @@ -249,12 +253,125 @@ public void testBulkSearchQBEWithJSONResponseFormat() throws IOException, Parser // assertEquals("Page Number #",pageNo,page.getPageNumber()); pageNo = pageNo + page.getPageSize(); }while(!page.isLastPage() && page.hasContent() ); - + + assertEquals("page count is ",5,page.getTotalPages()); + assertTrue("Page has previous page ?",page.hasPreviousPage()); + assertEquals("page size", 25,page.getPageSize()); + assertEquals("document count", 102,page.getTotalSize()); + + } + @Test + public void testBulkSearchQBECombinedQuery() throws IOException, ParserConfigurationException, SAXException, TransformerException, XpathException { + int count; + + //Creating a xml document manager for bulk search + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + //using QBE for query definition and set the search criteria + + QueryManager queryMgr = client.newQueryManager(); + + String queryAsString = ""+ + ""+ + ""+ + "bar"+ + ""+ + ""+ + "authorName"+ + "rhiea"+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""; + RawCombinedQueryDefinition qd = queryMgr.newRawCombinedQueryDefinition(new StringHandle(queryAsString).withFormat(Format.XML)); + + // set document manager level settings for search response + docMgr.setPageLength(25); + docMgr.setSearchView(QueryView.RESULTS); + + // Search for documents where content has bar and get first result record, get search handle on it,Use DOMHandle to read results + DOMHandle dh = new DOMHandle(); + DocumentPage page; + + long pageNo=1; + do{ + count=0; + page = docMgr.search(qd, pageNo,dh); + if(pageNo >1){ + assertFalse("Is this first Page", page.isFirstPage()); + assertTrue("Is page has previous page ?",page.hasPreviousPage()); + } + while(page.hasNext()){ + DocumentRecord rec = page.next(); + validateRecord(rec,Format.XML); + count++; + } + Document resultDoc = dh.get(); + assertXpathEvaluatesTo("xml", "string(//*[local-name()='result'][last()]//@*[local-name()='format'])", resultDoc); + assertEquals("document count", page.size(),count); + // assertEquals("Page Number #",pageNo,page.getPageNumber()); + pageNo = pageNo + page.getPageSize(); + }while(!page.isLastPage() && page.hasContent() ); + + assertEquals("page count is ",5,page.getTotalPages()); + assertTrue("Page has previous page ?",page.hasPreviousPage()); + assertEquals("page size", 25,page.getPageSize()); + assertEquals("document count", 102,page.getTotalSize()); + + } + @Test + public void testBulkSearchQBEWithJSONCombinedQuery() throws IOException, ParserConfigurationException, SAXException, TransformerException { + int count; + + //Creating a xml document manager for bulk search + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + //using QBE for query definition and set the search criteria + + QueryManager queryMgr = client.newQueryManager(); + String queryAsString = "{\"search\":{\"query\":{\"says\":{\"word\":\"woof\",\"exact\":true}}}}"; + //RawQueryByExampleDefinition qd = queryMgr.newRawQueryByExampleDefinition(new StringHandle(queryAsString).withFormat(Format.JSON)); + RawCombinedQueryDefinition qd = queryMgr.newRawCombinedQueryDefinition(new StringHandle(queryAsString).withFormat(Format.JSON)); + // set document manager level settings for search response + docMgr.setPageLength(25); + docMgr.setSearchView(QueryView.RESULTS); + docMgr.setResponseFormat(Format.JSON); + + // Search for documents where content has bar and get first result record, get search handle on it,Use DOMHandle to read results + JacksonHandle sh = new JacksonHandle(); + DocumentPage page; + + long pageNo=1; + do{ + count=0; + page = docMgr.search(qd, pageNo,sh); + if(pageNo >1){ + assertFalse("Is this first Page", page.isFirstPage()); + assertTrue("Is page has previous page ?",page.hasPreviousPage()); + } + while(page.hasNext()){ + DocumentRecord rec = page.next(); + rec.getFormat(); + System.out.println(rec.getContent(new StringHandle()).get().toString()); + validateRecord(rec,Format.JSON); + count++; + } + assertTrue("Page start in results and on page",sh.get().get("start").asLong() == page.getStart()); + assertEquals("document count", page.size(),count); + // assertEquals("Page Number #",pageNo,page.getPageNumber()); + pageNo = pageNo + page.getPageSize(); + }while(!page.isLastPage() && page.hasContent() ); + assertEquals("page count is ",5,page.getTotalPages()); assertTrue("Page has previous page ?",page.hasPreviousPage()); assertEquals("page size", 25,page.getPageSize()); assertEquals("document count", 102,page.getTotalSize()); - + } } diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStrucQueryDef.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStrucQueryDef.java new file mode 100644 index 000000000..f5c164221 --- /dev/null +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStrucQueryDef.java @@ -0,0 +1,448 @@ +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.IOException; +import java.util.HashMap; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.xml.sax.SAXException; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.Transaction; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.DocumentPage; +import com.marklogic.client.document.DocumentRecord; +import com.marklogic.client.document.DocumentWriteSet; +import com.marklogic.client.document.GenericDocumentManager; +import com.marklogic.client.document.JSONDocumentManager; +import com.marklogic.client.document.TextDocumentManager; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.document.DocumentManager.Metadata; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.DocumentMetadataHandle; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.JacksonHandle; +import com.marklogic.client.io.SearchHandle; +import com.marklogic.client.io.StringHandle; +import com.marklogic.client.io.DocumentMetadataHandle.Capability; +import com.marklogic.client.query.MatchDocumentSummary; +import com.marklogic.client.query.MatchLocation; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.RawStructuredQueryDefinition; +import com.marklogic.client.query.StringQueryDefinition; +import com.marklogic.client.query.QueryManager.QueryView; +import com.marklogic.client.query.StructuredQueryBuilder; +import com.marklogic.client.query.StructuredQueryDefinition; + +public class TestBulkSearchWithStrucQueryDef extends BasicJavaClientREST{ + + private static final int BATCH_SIZE=100; + private static final String DIRECTORY ="/bulkSearch/"; + private static String dbName = "TestBulkSearchStrucQDDB"; + private static String [] fNames = {"TestBulkSearchStrucQDDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client ; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + setupAppServicesConstraint(dbName); + createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + System.out.println("In tear down" ); + tearDownJavaRESTServer(dbName, fNames, restServerName); + deleteRESTUser("usr1"); + } + + @Before + public void setUp() throws Exception { + // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + // create new connection for each test below + client = DatabaseClientFactory.newClient("localhost", restPort, "usr1", "password", Authentication.DIGEST); + } + + @After + public void tearDown() throws Exception { + System.out.println("Running clear script"); + // release client + client.release(); + } + public void loadJSONDocuments() throws JsonProcessingException, IOException{ + int count=1; + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + + HashMap 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); + } + } + public void validateRecord(DocumentRecord record,Format type) { + + assertNotNull("DocumentRecord should never be null", record); + assertNotNull("Document uri should never be null", record.getUri()); + assertTrue("Document uri should start with " + DIRECTORY, record.getUri().startsWith(DIRECTORY)); + assertEquals("All records are expected to be in same format", type, record.getFormat()); + // System.out.println(record.getMimetype()); + + } + + public void loadXMLDocuments() throws IOException, ParserConfigurationException, SAXException, TransformerException{ + int count=1; + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + for(int i =0;i<102;i++){ + + writeset.add(DIRECTORY+"foo"+i+".xml", new DOMHandle(getDocumentContent("This is so foo with a bar "+i))); + + if(count%BATCH_SIZE == 0){ + docMgr.write(writeset); + writeset = docMgr.newWriteSet(); + } + count++; + } + if(count%BATCH_SIZE > 0){ + docMgr.write(writeset); + } + } + @Test + public void testBulkSearchSQDwithDifferentPageSizes() throws IOException, ParserConfigurationException, SAXException, TransformerException { + int count; + loadXMLDocuments(); + //Creating a txt document manager for bulk search + TextDocumentManager docMgr = client.newTextDocumentManager(); + //using QueryManger for query definition and set the search criteria + QueryManager queryMgr = client.newQueryManager(); + StructuredQueryBuilder qb = new StructuredQueryBuilder(); + StructuredQueryDefinition qd = qb.and(qb.term("foo","bar")); + + // set document manager level settings for search response + System.out.println("Default Page length setting on docMgr :"+docMgr.getPageLength()); + docMgr.setPageLength(1); + docMgr.setSearchView(QueryView.RESULTS); + docMgr.setResponseFormat(Format.XML); + assertEquals("format set on document manager","XML",docMgr.getResponseFormat().toString()); + assertEquals("Queryview set on document manager ","RESULTS" ,docMgr.getSearchView().toString()); + assertEquals("Page length ",1,docMgr.getPageLength()); + // Search for documents where content has bar and get first result record, get search handle on it + SearchHandle sh = new SearchHandle(); + DocumentPage page= docMgr.search(qd, 0); + // test for page methods + assertEquals("Number of records",1,page.size()); + assertEquals("Starting record in first page ",1,page.getStart()); + assertEquals("Total number of estimated results:",102,page.getTotalSize()); + assertEquals("Total number of estimated pages :",102,page.getTotalPages()); + // till the issue #78 get fixed + assertFalse("Is this First page :",page.isFirstPage());//this is bug + assertFalse("Is this Last page :",page.isLastPage()); + assertTrue("Is this First page has content:",page.hasContent()); + // Need the Issue #75 to be fixed + assertTrue("Is first page has previous page ?",page.hasPreviousPage()); + // + long pageNo=1; + do{ + count=0; + page = docMgr.search(qd, pageNo,sh); + if(pageNo >1){ + assertFalse("Is this first Page", page.isFirstPage()); + assertTrue("Is page has previous page ?",page.hasPreviousPage()); + } + while(page.hasNext()){ + DocumentRecord rec = page.next(); + rec.getFormat(); + validateRecord(rec,Format.XML); + // System.out.println(rec.getUri()); + count++; + } + MatchDocumentSummary[] mds= sh.getMatchResults(); + assertEquals("Matched document count",1,mds.length); + //since we set the query view to get only results, facet count supposed be 0 + assertEquals("Matched Facet count",0,sh.getFacetNames().length); + + assertEquals("document count", page.size(),count); + // assertEquals("Page Number #",pageNo,page.getPageNumber()); + pageNo = pageNo + page.getPageSize(); + }while(!page.isLastPage()); + assertTrue("page count is 101 ",pageNo == page.getTotalPages()); + assertTrue("Page has previous page ?",page.hasPreviousPage()); + assertEquals("page size", 1,page.getPageSize()); + assertEquals("document count", 102,page.getTotalSize()); + page= docMgr.search(qd, 103); + assertFalse("Page has any records ?",page.hasContent()); + } + + //This test has set response to JSON and pass StringHandle with format as JSON, expectint it to work, logged an issue 82 + @Test + public void testBulkSearchSQDwithResponseFormatandStringHandle() throws Exception{ + int count =1; + loadJSONDocuments(); + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + + QueryManager queryMgr = client.newQueryManager(); + StructuredQueryBuilder qb = new StructuredQueryBuilder(); + StructuredQueryDefinition qd = qb.and(qb.term("dog1","dog11")); + SearchHandle r = queryMgr.search(qd, new SearchHandle()); + + docMgr.setResponseFormat(Format.JSON); + docMgr.setSearchView(QueryView.METADATA); + docMgr.setMetadataCategories(Metadata.PERMISSIONS); + + StringHandle results = new StringHandle().withFormat(Format.JSON); + DocumentPage page= docMgr.search(qd, 1,results); + DocumentMetadataHandle mh = new DocumentMetadataHandle(); + while(page.hasNext()){ + DocumentRecord rec = page.next(); + validateRecord(rec,Format.JSON); + docMgr.readMetadata(rec.getUri(),mh); + assertTrue("Records has permissions? ",mh.getPermissions().containsKey("flexrep-eval")); + assertTrue("Record has collections ?",mh.getCollections().isEmpty()); + count++; + } + assertFalse("Search handle contains",results.get().isEmpty()); + + + } + //This test is testing SearchView options and search handle + @Test + public void testBulkSearchSQDwithJSONResponseFormat() throws Exception{ + + loadJSONDocuments(); + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + + QueryManager queryMgr = client.newQueryManager(); + StructuredQueryBuilder qb = new StructuredQueryBuilder(); + StructuredQueryDefinition qd = qb.and(qb.term("woof")); + docMgr.setResponseFormat(Format.JSON); + + docMgr.setSearchView(QueryView.FACETS); + JacksonHandle jh = new JacksonHandle(); + DocumentPage page= docMgr.search(qd, 1,jh); + + // System.out.println(jh.get().toString()); + assertTrue("Searh response has entry for facets",jh.get().has("facets")); + assertTrue("Searh response has entry for facets",jh.get().has("results"));//Issue 84 is tracking this + assertTrue("Searh response has entry for facets",jh.get().has("metrics")); + + docMgr.setSearchView(QueryView.RESULTS); + page= docMgr.search(qd, 1,jh); + + assertFalse("Searh response has entry for facets",jh.get().has("facets")); + assertTrue("Searh response has entry for facets",jh.get().has("results")); + assertTrue("Searh response has entry for facets",jh.get().has("metrics"));//Issue 84 is tracking this + + docMgr.setSearchView(QueryView.METADATA); + page= docMgr.search(qd, 1,jh); + + assertFalse("Searh response has entry for facets",jh.get().has("facets")); + assertTrue("Searh response has entry for facets",jh.get().has("results")); + assertTrue("Searh response has entry for facets",jh.get().has("metrics")); + + docMgr.setSearchView(QueryView.ALL); + page= docMgr.search(qd, 1,jh); + + assertTrue("Searh response has entry for facets",jh.get().has("facets")); + assertTrue("Searh response has entry for facets",jh.get().has("results")); + assertTrue("Searh response has entry for facets",jh.get().has("metrics")); + + queryMgr.setView(QueryView.FACETS); + queryMgr.search(qd, jh); + System.out.println(jh.get().toString()); + + } + + //This test is to verify the transactions, verifies the search works with transaction before commit, after rollback and after commit + @Test + public void testBulkSearchSQDwithTransactionsandDOMHandle() throws Exception{ + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DOMHandle results = new DOMHandle(); + QueryManager queryMgr = client.newQueryManager(); + StructuredQueryBuilder qb = new StructuredQueryBuilder(); + StructuredQueryDefinition qd = qb.and(qb.term("much","thought")); + Transaction t= client.openTransaction(); + try{ + int count=1; + XMLDocumentManager xmldocMgr = client.newXMLDocumentManager(); + DocumentWriteSet writeset =xmldocMgr.newWriteSet(); + for(int i =0;i<102;i++){ + writeset.add(DIRECTORY+"boo"+i+".xml", new DOMHandle(getDocumentContent("This is so too much thought "+i))); + if(count%BATCH_SIZE == 0){ + xmldocMgr.write(writeset,t); + writeset = xmldocMgr.newWriteSet(); + } + count++; + } + if(count%BATCH_SIZE > 0){ + xmldocMgr.write(writeset,t); + } + count=0; + docMgr.setSearchView(QueryView.RESULTS); + + DocumentPage page= docMgr.search(qd, 1,results,t); + while(page.hasNext()){ + DocumentRecord rec = page.next(); + + validateRecord(rec,Format.XML); + count++; + } + assertTrue("Page has conttent :",page.hasContent()); + assertEquals("Total search results before transaction rollback are ","102",results.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total").getNodeValue()); + // System.out.println(results.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total").getNodeValue()); + + }catch(Exception e){ throw e;} + finally{t.rollback();} + + DocumentPage page= docMgr.search(qd, 1,results); + assertEquals("Total search results after rollback are ","0",results.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total").getNodeValue()); + + } + //This test is to verify RAW XML structured Query + @Test + public void testBulkSearchRawXMLStrucQD() throws Exception{ + setAutomaticDirectoryCreation(dbName,"automatic"); + setMaintainLastModified(dbName,true); + this.loadJSONDocuments(); + this.loadXMLDocuments(); + GenericDocumentManager docMgr= client.newDocumentManager(); + QueryManager queryMgr = client.newQueryManager(); + String rawXMLQuery = + ""+ + " "+ + "bar"+ + ""+ + ""+ + "woof"+ + " "+ + ""; + StringHandle rh = new StringHandle(rawXMLQuery); + RawStructuredQueryDefinition qd = + queryMgr.newRawStructuredQueryDefinition(rh); + DOMHandle dh = new DOMHandle(); + DocumentPage page= docMgr.search(qd, 1,dh); + DocumentMetadataHandle mh = new DocumentMetadataHandle(); + + int count=1; + while(count < 4) + { + page= docMgr.search(qd, count,dh); + + while(page.hasNext()){ + DocumentRecord rec = page.next(); + if(rec.getMimetype().contains("xml")){ + validateRecord(rec,Format.XML); + } + else{ + validateRecord(rec,Format.JSON); + } + docMgr.readMetadata(rec.getUri(),mh); + assertTrue("Records has permissions? ",mh.getPermissions().containsKey("flexrep-eval")); + assertFalse("Record has collections ?",mh.getCollections().isEmpty()); + + } + System.out.println(this.convertXMLDocumentToString(dh.get())); + assertEquals("Total search results before transaction rollback are ","204",dh.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total").getNodeValue()); + count++; + + } + + } + //This test is to verify RAW JSON structured query + @Test + public void testBulkSearchRawJSONStrucQD() throws Exception{ + setAutomaticDirectoryCreation(dbName,"automatic"); + setMaintainLastModified(dbName,true); + this.loadJSONDocuments(); + this.loadXMLDocuments(); + GenericDocumentManager docMgr= client.newDocumentManager(); + QueryManager queryMgr = client.newQueryManager(); + JacksonHandle jh = new JacksonHandle(); + ObjectMapper mapper = new ObjectMapper(); + // constructing JSON representation of Raw JSON Structured Query + + ObjectNode txtNode = mapper.createObjectNode(); + txtNode.putArray("text").add("woof"); + ObjectNode termQNode = mapper.createObjectNode(); + termQNode.set("term-query", txtNode); + ObjectNode queriesArrayNode = mapper.createObjectNode(); + queriesArrayNode.putArray("queries").add(termQNode); + + ObjectNode txtNode2 = mapper.createObjectNode(); + txtNode2.putArray("text").add("bar"); + ObjectNode termQNode2 = mapper.createObjectNode(); + termQNode2.set("term-query", txtNode2); + queriesArrayNode.withArray("queries").add(termQNode2); + + ObjectNode orQueryNode = mapper.createObjectNode(); + orQueryNode.set("or-query",queriesArrayNode ); + + ObjectNode queryArrayNode = mapper.createObjectNode(); + queryArrayNode.putArray("queries").add(orQueryNode); + ObjectNode mainNode = mapper.createObjectNode(); + mainNode.set("query", queryArrayNode); + jh.set(mainNode); + RawStructuredQueryDefinition qd = + queryMgr.newRawStructuredQueryDefinition(jh); + System.out.println(jh.get().toString()); + docMgr.setResponseFormat(Format.JSON); + JacksonHandle results = new JacksonHandle(); + DocumentPage page= docMgr.search(qd, 1,results); + DocumentMetadataHandle mh = new DocumentMetadataHandle(); + + int count=1; + while(count < 4) + { + page= docMgr.search(qd, count,results); + + while(page.hasNext()){ + DocumentRecord rec = page.next(); + if(rec.getMimetype().contains("xml")){ + validateRecord(rec,Format.XML); + } + else{ + validateRecord(rec,Format.JSON); + } + docMgr.readMetadata(rec.getUri(),mh); + assertTrue("Records has permissions? ",mh.getPermissions().containsKey("flexrep-eval")); + assertFalse("Record has collections ?",mh.getCollections().isEmpty()); + + } + + count++; + } + System.out.println(results.get().toString()); + assertEquals("Total search results before transaction rollback are ","204",results.get().get("total").asText()); + } +} From 52848d6cb5f0d51224e4b6595997eb6c745f3582 Mon Sep 17 00:00:00 2001 From: sravan Date: Wed, 20 Aug 2014 16:04:22 -0700 Subject: [PATCH 067/357] updated the test with json combined query --- .../javaclient/TestBulkSearchEWithQBE.java | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchEWithQBE.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchEWithQBE.java index dcaa918ec..d8b9d347c 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchEWithQBE.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchEWithQBE.java @@ -64,26 +64,26 @@ public class TestBulkSearchEWithQBE extends BasicJavaClientREST{ @BeforeClass public static void setUpBeforeClass() throws Exception { System.out.println("In setup"); - // setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); - // setupAppServicesConstraint(dbName); - // createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + setupAppServicesConstraint(dbName); + createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); } @AfterClass public static void tearDownAfterClass() throws Exception { System.out.println("In tear down" ); - // tearDownJavaRESTServer(dbName, fNames, restServerName); - // deleteRESTUser("usr1"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + deleteRESTUser("usr1"); } @Before public void setUp() throws Exception { - System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); +// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); // create new connection for each test below client = DatabaseClientFactory.newClient("localhost", restPort, "usr1", "password", Authentication.DIGEST); -// loadTxtDocuments(); -// loadXMLDocuments(); -// loadJSONDocuments(); + loadTxtDocuments(); + loadXMLDocuments(); + loadJSONDocuments(); } @After @@ -334,16 +334,18 @@ public void testBulkSearchQBEWithJSONCombinedQuery() throws IOException, ParserC //using QBE for query definition and set the search criteria QueryManager queryMgr = client.newQueryManager(); - String queryAsString = "{\"search\":{\"query\":{\"says\":{\"word\":\"woof\",\"exact\":true}}}}"; - //RawQueryByExampleDefinition qd = queryMgr.newRawQueryByExampleDefinition(new StringHandle(queryAsString).withFormat(Format.JSON)); + String queryAsString ="{\"search\":{\"query\":{\"value-constraint-query\":{\"constraint-name\":\"animal\", \"text\":\"woof\"}}, \"options\":{\"constraint\":{\"name\":\"animal\", \"value\":{\"json-property\":\"says\"}}}}}"; + RawCombinedQueryDefinition qd = queryMgr.newRawCombinedQueryDefinition(new StringHandle(queryAsString).withFormat(Format.JSON)); // set document manager level settings for search response docMgr.setPageLength(25); docMgr.setSearchView(QueryView.RESULTS); docMgr.setResponseFormat(Format.JSON); - - // Search for documents where content has bar and get first result record, get search handle on it,Use DOMHandle to read results + + // Search for documents where content has woof and get first result record, JacksonHandle sh = new JacksonHandle(); + queryMgr.search(qd, sh); + System.out.println(sh.get().toString()); DocumentPage page; long pageNo=1; @@ -357,16 +359,14 @@ public void testBulkSearchQBEWithJSONCombinedQuery() throws IOException, ParserC while(page.hasNext()){ DocumentRecord rec = page.next(); rec.getFormat(); - System.out.println(rec.getContent(new StringHandle()).get().toString()); validateRecord(rec,Format.JSON); count++; } assertTrue("Page start in results and on page",sh.get().get("start").asLong() == page.getStart()); assertEquals("document count", page.size(),count); - // assertEquals("Page Number #",pageNo,page.getPageNumber()); pageNo = pageNo + page.getPageSize(); }while(!page.isLastPage() && page.hasContent() ); - + System.out.println(sh.get().toString()); assertEquals("page count is ",5,page.getTotalPages()); assertTrue("Page has previous page ?",page.hasPreviousPage()); assertEquals("page size", 25,page.getPageSize()); From a065d913bdfb18fc50a077672e7691b97702b622 Mon Sep 17 00:00:00 2001 From: sravan Date: Wed, 20 Aug 2014 16:24:22 -0700 Subject: [PATCH 068/357] Setting automatic directory creation to auto --- .../test/java/com/marklogic/javaclient/ConnectedRESTQA.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java b/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java index da083c681..724d380ec 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java @@ -827,6 +827,9 @@ public static void enableTrailingWildcardSearches(String dbName) throws Exceptio public static void setMaintainLastModified(String dbName,boolean opt) throws Exception{ setDatabaseProperties(dbName,"maintain-last-modified",opt); } + public static void setAutomaticDirectoryCreation(String dbName, String opt) throws Exception{ + setDatabaseProperties(dbName,"directory-creation",opt); + } /* * This function constructs a range element index with default collation,range-value-positions and invalid values * From c777798873345b8c29145c5bc421b4603e2c870b Mon Sep 17 00:00:00 2001 From: gvaidees Date: Wed, 20 Aug 2014 17:38:14 -0700 Subject: [PATCH 069/357] Fixed asserts to be not string based, but value based so that order does not matter --- .../javaclient/TestBulkWriteMetadata2.java | 1101 ++++++++--------- 1 file changed, 549 insertions(+), 552 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata2.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata2.java index 30f70dd03..39bef805b 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata2.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata2.java @@ -1,552 +1,549 @@ -/** - * - */ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.File; -import java.io.FileInputStream; -import java.io.Reader; -import java.io.StringReader; -import java.util.Calendar; - -import javax.xml.transform.dom.DOMSource; - -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ObjectNode; -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.document.BinaryDocumentManager; -import com.marklogic.client.document.DocumentPage; -import com.marklogic.client.document.DocumentRecord; -import com.marklogic.client.document.DocumentWriteSet; -import com.marklogic.client.document.DocumentDescriptor; -import com.marklogic.client.document.GenericDocumentManager; -import com.marklogic.client.document.JSONDocumentManager; -import com.marklogic.client.document.TextDocumentManager; -import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.io.BytesHandle; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.DocumentMetadataHandle; -import com.marklogic.client.io.FileHandle; -import com.marklogic.client.io.Format; -import com.marklogic.client.io.InputStreamHandle; -import com.marklogic.client.io.JacksonHandle; -import com.marklogic.client.io.ReaderHandle; -import com.marklogic.client.io.SourceHandle; -import com.marklogic.client.io.StringHandle; -import com.marklogic.client.io.DocumentMetadataHandle.Capability; -import com.marklogic.client.io.DocumentMetadataHandle.DocumentCollections; -import com.marklogic.client.io.DocumentMetadataHandle.DocumentPermissions; -import com.marklogic.client.io.DocumentMetadataHandle.DocumentProperties; - -/** - * @author skottam - * This test is test the DocumentWriteSet function - * public DocumentWriteSet add(String uri, DocumentMetadataWriteHandle metadataHandle); - * This test intention is to test the system default , request wide, and document specific update to metadata will overwrite the existing metadata - * setup: create a usr1 with system level meta data as default - * client make a connection with usr1 to do bulk loading - * load first set of documents without default meta data and check documents get defaults from usr defaults - * load second set of documents where default metadata is set in the middle to see documents at the begining has old defaults and later have latest - * load third set of documents with defaultset and do a document specific metadata update and check that is updated. - * - */ -public class TestBulkWriteMetadata2 extends BasicJavaClientREST{ - - private static String dbName = "TestBulkWriteDefaultMetadataDB2"; - private static String [] fNames = {"TestBulkWriteDefaultMetadataDB-2"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort = 8011; - private DatabaseClient client ; - /** - * @throws java.lang.Exception - */ - @BeforeClass - public static void setUpBeforeClass() throws Exception { - System.out.println("In Setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); - createRESTUser("app-user", "password","rest-writer","rest-reader" ); - createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); - setMaintainLastModified(dbName, true); - } - - /** - * @throws java.lang.Exception - */ - @AfterClass - public static void tearDownAfterClass() throws Exception { - System.out.println("In tear down" ); - tearDownJavaRESTServer(dbName, fNames, restServerName); - deleteRESTUser("app-user"); - deleteRESTUser("usr1"); - } - - /** - * @throws java.lang.Exception - */ - @Before - public void setUp() throws Exception { - // create new connection for each test below - client = DatabaseClientFactory.newClient("localhost", restPort, "usr1", "password", Authentication.DIGEST); - } - - /** - * @throws java.lang.Exception - */ - @After - public void tearDown() throws Exception { - System.out.println("Running clear script"); - // release client - client.release(); - } - - public DocumentMetadataHandle setMetadata(){ - // create and initialize a handle on the metadata - DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); - metadataHandle.getCollections().addAll("my-collection1","my-collection2"); - metadataHandle.getPermissions().add("app-user", Capability.UPDATE, Capability.READ); - metadataHandle.getProperties().put("reviewed", true); - metadataHandle.getProperties().put("myString", "foo"); - metadataHandle.getProperties().put("myInteger", 10); - metadataHandle.getProperties().put("myDecimal", 34.56678); - metadataHandle.getProperties().put("myCalendar", Calendar.getInstance().get(Calendar.YEAR)); - metadataHandle.setQuality(23); - return metadataHandle; - } - public void validateMetadata(DocumentMetadataHandle mh){ - - // get metadata values - DocumentProperties properties = mh.getProperties(); - DocumentPermissions permissions = mh.getPermissions(); - DocumentCollections collections = mh.getCollections(); - - // Properties - // String expectedProperties = "size:5|reviewed:true|myInteger:10|myDecimal:34.56678|myCalendar:2014|myString:foo|"; - String actualProperties = getDocumentPropertiesString(properties); - boolean result = actualProperties.contains("size:6|"); - assertTrue("Document properties count", result); - - // Permissions - String expectedPermissions1 = "size:4|flexrep-eval:[READ]|rest-reader:[READ]|app-user:[UPDATE, READ]|rest-writer:[UPDATE]|"; - String expectedPermissions2 = "size:4|flexrep-eval:[READ]|rest-reader:[READ]|app-user:[READ, UPDATE]|rest-writer:[UPDATE]|"; - String actualPermissions = getDocumentPermissionsString(permissions); - System.out.println(actualPermissions); - if(actualPermissions.contains("[UPDATE, READ]")) - assertEquals("Document permissions difference", expectedPermissions1, actualPermissions); - else if(actualPermissions.contains("[READ, UPDATE]")) - assertEquals("Document permissions difference", expectedPermissions2, actualPermissions); - else - assertEquals("Document permissions difference", "wrong", actualPermissions); - - // Collections - String expectedCollections = "size:2|my-collection1|my-collection2|"; - String actualCollections = getDocumentCollectionsString(collections); - assertEquals("Document collections difference", expectedCollections, actualCollections); - - } - public void validateDefaultMetadata(DocumentMetadataHandle mh){ - - // get metadata values - DocumentProperties properties = mh.getProperties(); - DocumentPermissions permissions = mh.getPermissions(); - DocumentCollections collections = mh.getCollections(); - - // Properties - String actualProperties = getDocumentPropertiesString(properties); - boolean result =actualProperties.contains("size:1|"); - System.out.println(actualProperties +result); - assertTrue("Document default last modified properties count1?", result); - - - // Permissions - - String expectedPermissions1 = "size:3|flexrep-eval:[READ]|rest-reader:[READ]|rest-writer:[UPDATE]|"; - String actualPermissions = getDocumentPermissionsString(permissions); - assertEquals("Document permissions difference", expectedPermissions1, actualPermissions); - - // Collections - String expectedCollections = "size:1|http://permission-collections/|"; - String actualCollections = getDocumentCollectionsString(collections); - - assertEquals("Document collections difference", expectedCollections, actualCollections); -// System.out.println(actualPermissions); - } - @Test - public void testWriteMultipleTextDocWithDefaultMetadata2() - { - DocumentMetadataHandle mh1,mh2; - String docId[] = {"/foo/test/myFoo1.txt","/foo/test/myFoo2.txt","/foo/test/myFoo3.txt", - "/foo/test/myFoo4.txt","/foo/test/myFoo5.txt","/foo/test/myFoo6.txt", - "/foo/test/myFoo7.txt","/foo/test/myFoo8.txt","/foo/test/myFoo9.txt"}; - - TextDocumentManager docMgr = client.newTextDocumentManager(); - - DocumentWriteSet writeset =docMgr.newWriteSet(); - - writeset.add(docId[0], new StringHandle().with("This is so foo1")); - writeset.add(docId[1], new StringHandle().with("This is so foo2")); - writeset.add(docId[2], new StringHandle().with("This is so foo3")); - docMgr.write(writeset); - //Default system property document set - System.out.println("Assert if document set doesnt show default properties"); - DocumentPage page = docMgr.read(docId); - mh1=new DocumentMetadataHandle(); - while(page.hasNext()){ - DocumentRecord rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh1); - validateDefaultMetadata(mh1); - } - validateDefaultMetadata(mh1); - - //Adding document specific properties in document set2 - - System.out.println("Assert if document 5 show default properties"); - - writeset =docMgr.newWriteSet(); - // put metadata - mh2 = setMetadata(); - - writeset.add(docId[3], new StringHandle().with("This is so foo4")); - writeset.add(docId[4],mh2, new StringHandle().with("This is so foo5")); - writeset.add(docId[5], new StringHandle().with("This is so foo6")); - docMgr.write(writeset); - - - page = docMgr.read(docId[4]); - mh2=new DocumentMetadataHandle(); - DocumentRecord rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh2); - validateMetadata(mh2); - page = docMgr.read(docId[3]); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh2); - validateDefaultMetadata(mh2); - page = docMgr.read(docId[5]); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh2); - validateDefaultMetadata(mh2); - - System.out.println("Assert if documents 8 and 9 show default properties and if 7 doesnt show default"); - - writeset =docMgr.newWriteSet(); - // put metadata - mh2 = setMetadata(); - - writeset.add(docId[6], new StringHandle().with("This is so foo4")); - writeset.addDefault(mh2); - writeset.add(docId[7], new StringHandle().with("This is so foo5")); - writeset.add(docId[8], new StringHandle().with("This is so foo6")); - docMgr.write(writeset); - - page = docMgr.read(docId[6]); - mh2=new DocumentMetadataHandle(); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh2); - validateDefaultMetadata(mh2); - - page = docMgr.read(docId[7]); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh2); - validateMetadata(mh2); - - page = docMgr.read(docId[8]); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh2); - validateMetadata(mh2); - - - } - @Test - public void testWriteMultipleXMLDocWithDefaultMetadata2() throws Exception - { - // put metadata - DocumentMetadataHandle mh1,mh2; - String docId[] = {"/foo/test/Foo1.xml","/foo/test/Foo2.xml","/foo/test/Foo3.xml", - "/foo/test/Foo4.xml","/foo/test/Foo5.xml","/foo/test/Foo6.xml", - "/foo/test/Foo7.xml","/foo/test/Foo8.xml","/foo/test/Foo8.xml"}; - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - - DocumentDescriptor docdisc = docMgr.newDescriptor("test1"); - - writeset.add(docdisc, new DOMHandle(getDocumentContent("This is so foo1"))); - writeset.add(docId[1], new DOMHandle().with(getDocumentContent("This is so foo2"))); - writeset.add(docId[2], new DOMHandle().with(getDocumentContent("This is so foo3"))); - - docMgr.write(writeset); - - DocumentPage page = docMgr.read(docId); - DocumentRecord rec; - mh1 = new DocumentMetadataHandle(); - while(page.hasNext()){ - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh1); - validateDefaultMetadata(mh1); - } - validateDefaultMetadata(mh1); - - // put metadata - mh2 = setMetadata(); - writeset =docMgr.newWriteSet(); - - writeset.add(docId[3], new DOMHandle(getDocumentContent("This is so foo4"))); - writeset.addDefault(mh2); - writeset.add(docId[4], new DOMHandle().with(getDocumentContent("This is so foo5"))); - writeset.add(docId[5], new DOMHandle().with(getDocumentContent("This is so foo6"))); - docMgr.write(writeset); - - page = docMgr.read(docId[3]); - mh2=new DocumentMetadataHandle(); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh2); - validateDefaultMetadata(mh2); - - page = docMgr.read(docId[4]); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh2); - validateMetadata(mh2); - - page = docMgr.read(docId[5]); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh2); - validateMetadata(mh2); - //Overwriting the existing properties - mh2 = setMetadata(); - writeset =docMgr.newWriteSet(); - writeset.add(docId[3], mh2,new DOMHandle(getDocumentContent("This is so foo4"))); - docMgr.write(writeset); - - page = docMgr.read(docId[3]); - mh2=new DocumentMetadataHandle(); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh2); - validateMetadata(mh2); - - } - @Test - public void testWriteMultipleBinaryDocWithDefaultMetadata2() throws Exception - { - String docId[] = {"Pandakarlino.jpg","mlfavicon.png"}; - - BinaryDocumentManager docMgr = client.newBinaryDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - // put metadata - DocumentMetadataHandle mh = setMetadata(); - - File file1= null,file2=null; - file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); - FileHandle handle1 = new FileHandle(file1); - writeset.add("/1/"+docId[0],handle1.withFormat(Format.BINARY)); - - writeset.add("/2/"+docId[0],new DocumentMetadataHandle().withQuality(5),handle1.withFormat(Format.BINARY)); - - file2 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[1]); - FileHandle handle2 = new FileHandle(file2); - writeset.add("/1/"+docId[1],new DocumentMetadataHandle().withCollections("collection1"),handle2.withFormat(Format.BINARY)); - - writeset.addDefault(mh); - - writeset.add("/2/"+docId[1],handle2.withFormat(Format.BINARY)); - docMgr.write(writeset); - - - DocumentPage page = docMgr.read("/1/Pandakarlino.jpg"); - DocumentRecord rec; - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh); -// System.out.println(rec.getUri()+mh.getQuality()); - assertEquals("default quality",0,mh.getQuality()); - validateDefaultMetadata(mh); - page = docMgr.read("/2/Pandakarlino.jpg"); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh); - assertEquals(" quality",5,mh.getQuality()); - - assertTrue("default collections reset",mh.getCollections().isEmpty()); - - page = docMgr.read("/1/mlfavicon.png"); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh); - assertEquals("default quality",0,mh.getQuality()); -// System.out.println(rec.getUri()+mh.getCollections().isEmpty()); - assertFalse("default collections reset",mh.getCollections().isEmpty()); - - page = docMgr.read("/2/mlfavicon.png"); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh); - validateMetadata(mh); -} - - /* - * This is test is made up for the github issue 25 - */ -@Test - public void testWriteMultipleJSONDocsWithDefaultMetadata2() throws Exception - { - // Synthesize input content - StringHandle doc1 = new StringHandle( - "{\"number\": 1}").withFormat(Format.JSON); - StringHandle doc2 = new StringHandle( - "{\"number\": 2}").withFormat(Format.JSON); - StringHandle doc3 = new StringHandle( - "{\"number\": 3}").withFormat(Format.JSON); - StringHandle doc4 = new StringHandle( - "{\"number\": 4}").withFormat(Format.JSON); - StringHandle doc5 = new StringHandle( - "{\"number\": 5}").withFormat(Format.JSON); - - // Synthesize input metadata - DocumentMetadataHandle defaultMetadata1 = - new DocumentMetadataHandle().withQuality(1); - DocumentMetadataHandle defaultMetadata2 = - new DocumentMetadataHandle().withQuality(2); - DocumentMetadataHandle docSpecificMetadata = - new DocumentMetadataHandle().withCollections("mySpecificCollection"); - - // Create and build up the batch - JSONDocumentManager jdm = client.newJSONDocumentManager(); - DocumentWriteSet batch = jdm.newWriteSet(); - - // use system default metadata - batch.add("doc1.json", doc1); // system default metadata - - // using batch default metadata - batch.addDefault(defaultMetadata1); - batch.add("doc2.json", doc2); // batch default metadata - batch.add("doc3.json", docSpecificMetadata, doc3); - batch.add("doc4.json", doc4); // batch default metadata - - // replace batch default metadata with new metadata - batch.addDefault(defaultMetadata2); - batch.add("doc5.json", doc5); // batch default - - // Execute the write operation - jdm.write(batch); - DocumentPage page ; - DocumentRecord rec; - // Check the results - // Doc1 should have the system default quality of 0 - page = jdm.read("doc1.json"); - DocumentMetadataHandle mh = new DocumentMetadataHandle(); - rec = page.next(); - jdm.readMetadata(rec.getUri(), mh); - validateDefaultMetadata(mh); - assertEquals("default quality",0,mh.getQuality()); - - // Doc2 should use the first batch default metadata, with quality 1 - page = jdm.read("doc2.json"); - rec = page.next(); - jdm.readMetadata(rec.getUri(), mh); - System.out.print(mh.getCollections().isEmpty()); - assertEquals("default quality",1,mh.getQuality()); - assertTrue("default collections reset",mh.getCollections().isEmpty()); - - // Doc3 should have the system default document quality (0) because quality - // was not included in the document-specific metadata. It should be in the - // collection "mySpecificCollection", from the document-specific metadata. - - page = jdm.read("doc3.json"); - rec = page.next(); - jdm.readMetadata(rec.getUri(), mh); - assertEquals("default quality",0,mh.getQuality()); - assertEquals("default collection must change","[mySpecificCollection]",mh.getCollections().toString()); - - DocumentMetadataHandle doc3Metadata = - jdm.readMetadata("doc3.json", new DocumentMetadataHandle()); - System.out.println("doc3 quality: Expected=0, Actual=" + doc3Metadata.getPermissions()); - System.out.print("doc3 collections: Expected: myCollection, Actual="); - for (String collection : doc3Metadata.getCollections()) { - System.out.print(collection + " "); - } - System.out.println(); - - // Doc 4 should also use the 1st batch default metadata, with quality 1 - page = jdm.read("doc4.json"); - rec = page.next(); - jdm.readMetadata(rec.getUri(), mh); - assertEquals("default quality",1,mh.getQuality()); - assertTrue("default collections reset",mh.getCollections().isEmpty()); - // Doc5 should use the 2nd batch default metadata, with quality 2 - page = jdm.read("doc5.json"); - rec = page.next(); - jdm.readMetadata(rec.getUri(), mh); - assertEquals("default quality",2,mh.getQuality()); - - } - @Test - public void testWriteGenericDocMgrWithDefaultMetadata() throws Exception - { - String docId[] = {"Sega-4MB.jpg"}; - - GenericDocumentManager docMgr = client.newDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - // put metadata - DocumentMetadataHandle mh = setMetadata(); - - writeset.addDefault(mh); // Adding default metadata to the entire batch - File file1= null; - file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); - FileInputStream fis = new FileInputStream(file1); - InputStreamHandle handle1 = new InputStreamHandle(fis); - handle1.setFormat(Format.BINARY); - - writeset.add("/generic/Pandakarlino.jpg",handle1); // This document implicitly gets the default metadata that we added in the begining - - 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 DocumentMetadataHandle().withQuality(10),jh); //This document suppose to get the in scope metadata quality and should set collections to system default - - String foo1 = "This is foo1 of byte Array"; - byte[] ba = foo1.getBytes(); - BytesHandle bh = new BytesHandle(ba); - bh.setFormat(Format.TEXT); - - writeset.add("/generic/foo1.txt",bh); - - DOMSource ds = new DOMSource(getDocumentContent("This is so foo1")); - SourceHandle sh = new SourceHandle(); - sh.set(ds); - sh.setFormat(Format.XML); - DocumentMetadataHandle mh2 = new DocumentMetadataHandle(); - writeset.add("/generic/foo.xml", mh2.withCollections("genericCollection"), sh); //This document should over write the system default and default collection list and get document specific collection - - docMgr.write(writeset); - - DocumentPage page = docMgr.read("/generic/Pandakarlino.jpg","/generic/foo1.txt"); - - while(page.hasNext()){ - DocumentRecord rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh); - System.out.println(rec.getUri()); - validateMetadata(mh); - } - validateMetadata(mh); - page = docMgr.read("/generic/dog.json"); - DocumentRecord rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh); - assertEquals("default quality",10,mh.getQuality()); - assertTrue("default collections missing",mh.getCollections().isEmpty()); - - page = docMgr.read("/generic/foo.xml"); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh); - assertEquals("default quality",0,mh.getQuality()); - assertEquals("default collection must change","[genericCollection]",mh.getCollections().toString()); - - } - -} +/** + * + */ +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.FileInputStream; +import java.util.Calendar; + +import javax.xml.transform.dom.DOMSource; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.BinaryDocumentManager; +import com.marklogic.client.document.DocumentPage; +import com.marklogic.client.document.DocumentRecord; +import com.marklogic.client.document.DocumentWriteSet; +import com.marklogic.client.document.DocumentDescriptor; +import com.marklogic.client.document.GenericDocumentManager; +import com.marklogic.client.document.JSONDocumentManager; +import com.marklogic.client.document.TextDocumentManager; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.io.BytesHandle; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.DocumentMetadataHandle; +import com.marklogic.client.io.FileHandle; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.InputStreamHandle; +import com.marklogic.client.io.JacksonHandle; +import com.marklogic.client.io.SourceHandle; +import com.marklogic.client.io.StringHandle; +import com.marklogic.client.io.DocumentMetadataHandle.Capability; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentCollections; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentPermissions; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentProperties; + +/** + * @author skottam + * This test is test the DocumentWriteSet function + * public DocumentWriteSet add(String uri, DocumentMetadataWriteHandle metadataHandle); + * This test intention is to test the system default , request wide, and document specific update to metadata will overwrite the existing metadata + * setup: create a usr1 with system level meta data as default + * client make a connection with usr1 to do bulk loading + * load first set of documents without default meta data and check documents get defaults from usr defaults + * load second set of documents where default metadata is set in the middle to see documents at the begining has old defaults and later have latest + * load third set of documents with defaultset and do a document specific metadata update and check that is updated. + * + */ +public class TestBulkWriteMetadata2 extends BasicJavaClientREST{ + + private static String dbName = "TestBulkWriteDefaultMetadataDB2"; + private static String [] fNames = {"TestBulkWriteDefaultMetadataDB-2"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client ; + /** + * @throws java.lang.Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + System.out.println("In Setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + createRESTUser("app-user", "password","rest-writer","rest-reader" ); + createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); + setMaintainLastModified(dbName, true); + } + + /** + * @throws java.lang.Exception + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + System.out.println("In tear down" ); + tearDownJavaRESTServer(dbName, fNames, restServerName); + deleteRESTUser("app-user"); + deleteRESTUser("usr1"); + } + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + // create new connection for each test below + client = DatabaseClientFactory.newClient("localhost", restPort, "usr1", "password", Authentication.DIGEST); + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + System.out.println("Running clear script"); + // release client + client.release(); + } + + public DocumentMetadataHandle setMetadata(){ + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); + metadataHandle.getCollections().addAll("my-collection1","my-collection2"); + metadataHandle.getPermissions().add("app-user", Capability.UPDATE, Capability.READ); + metadataHandle.getProperties().put("reviewed", true); + metadataHandle.getProperties().put("myString", "foo"); + metadataHandle.getProperties().put("myInteger", 10); + metadataHandle.getProperties().put("myDecimal", 34.56678); + metadataHandle.getProperties().put("myCalendar", Calendar.getInstance().get(Calendar.YEAR)); + metadataHandle.setQuality(23); + return metadataHandle; + } + public void validateMetadata(DocumentMetadataHandle mh){ + + // get metadata values + DocumentProperties properties = mh.getProperties(); + DocumentPermissions permissions = mh.getPermissions(); + DocumentCollections collections = mh.getCollections(); + + // Properties + // String expectedProperties = "size:5|reviewed:true|myInteger:10|myDecimal:34.56678|myCalendar:2014|myString:foo|"; + String actualProperties = getDocumentPropertiesString(properties); + boolean result = actualProperties.contains("size:6|"); + assertTrue("Document properties count", result); + + // Permissions + String actualPermissions = getDocumentPermissionsString(permissions); + System.out.println(actualPermissions); + + assertTrue("Document permissions difference in size value", actualPermissions.contains("size:4")); + assertTrue("Document permissions difference in flexrep-eval permission", actualPermissions.contains("flexrep-eval:[READ]")); + assertTrue("Document permissions difference in rest-reader permission", actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue("Document permissions difference in app-user permissions", + (actualPermissions.contains("app-user:[UPDATE, READ]") || actualPermissions.contains("app-user:[READ, UPDATE]"))); + + // Collections + String actualCollections = getDocumentCollectionsString(collections); + assertTrue("Document permissions difference in size value", actualCollections.contains("size:2")); + assertTrue("my-collection1 not found", actualCollections.contains("my-collection1")); + assertTrue("my-collection2 not found", actualCollections.contains("my-collection2")); + } + public void validateDefaultMetadata(DocumentMetadataHandle mh){ + + // get metadata values + DocumentProperties properties = mh.getProperties(); + DocumentPermissions permissions = mh.getPermissions(); + DocumentCollections collections = mh.getCollections(); + + // Properties + String actualProperties = getDocumentPropertiesString(properties); + boolean result =actualProperties.contains("size:1|"); + System.out.println(actualProperties +result); + assertTrue("Document default last modified properties count1?", result); + + + // Permissions + String actualPermissions = getDocumentPermissionsString(permissions); + + assertTrue("Document permissions difference in size value", actualPermissions.contains("size:3")); + assertTrue("Document permissions difference in flexrep-eval permission", actualPermissions.contains("flexrep-eval:[READ]")); + assertTrue("Document permissions difference in rest-reader permission", actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", actualPermissions.contains("rest-writer:[UPDATE]")); + + // Collections + String expectedCollections = "size:1|http://permission-collections/|"; + String actualCollections = getDocumentCollectionsString(collections); + + assertEquals("Document collections difference", expectedCollections, actualCollections); +// System.out.println(actualPermissions); + } + @Test + public void testWriteMultipleTextDocWithDefaultMetadata2() + { + DocumentMetadataHandle mh1,mh2; + String docId[] = {"/foo/test/myFoo1.txt","/foo/test/myFoo2.txt","/foo/test/myFoo3.txt", + "/foo/test/myFoo4.txt","/foo/test/myFoo5.txt","/foo/test/myFoo6.txt", + "/foo/test/myFoo7.txt","/foo/test/myFoo8.txt","/foo/test/myFoo9.txt"}; + + TextDocumentManager docMgr = client.newTextDocumentManager(); + + DocumentWriteSet writeset =docMgr.newWriteSet(); + + writeset.add(docId[0], new StringHandle().with("This is so foo1")); + writeset.add(docId[1], new StringHandle().with("This is so foo2")); + writeset.add(docId[2], new StringHandle().with("This is so foo3")); + docMgr.write(writeset); + //Default system property document set + System.out.println("Assert if document set doesnt show default properties"); + DocumentPage page = docMgr.read(docId); + mh1=new DocumentMetadataHandle(); + while(page.hasNext()){ + DocumentRecord rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh1); + validateDefaultMetadata(mh1); + } + validateDefaultMetadata(mh1); + + //Adding document specific properties in document set2 + + System.out.println("Assert if document 5 show default properties"); + + writeset =docMgr.newWriteSet(); + // put metadata + mh2 = setMetadata(); + + writeset.add(docId[3], new StringHandle().with("This is so foo4")); + writeset.add(docId[4],mh2, new StringHandle().with("This is so foo5")); + writeset.add(docId[5], new StringHandle().with("This is so foo6")); + docMgr.write(writeset); + + + page = docMgr.read(docId[4]); + mh2=new DocumentMetadataHandle(); + DocumentRecord rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh2); + validateMetadata(mh2); + page = docMgr.read(docId[3]); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh2); + validateDefaultMetadata(mh2); + page = docMgr.read(docId[5]); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh2); + validateDefaultMetadata(mh2); + + System.out.println("Assert if documents 8 and 9 show default properties and if 7 doesnt show default"); + + writeset =docMgr.newWriteSet(); + // put metadata + mh2 = setMetadata(); + + writeset.add(docId[6], new StringHandle().with("This is so foo4")); + writeset.addDefault(mh2); + writeset.add(docId[7], new StringHandle().with("This is so foo5")); + writeset.add(docId[8], new StringHandle().with("This is so foo6")); + docMgr.write(writeset); + + page = docMgr.read(docId[6]); + mh2=new DocumentMetadataHandle(); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh2); + validateDefaultMetadata(mh2); + + page = docMgr.read(docId[7]); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh2); + validateMetadata(mh2); + + page = docMgr.read(docId[8]); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh2); + validateMetadata(mh2); + + + } + @Test + public void testWriteMultipleXMLDocWithDefaultMetadata2() throws Exception + { + // put metadata + DocumentMetadataHandle mh1,mh2; + String docId[] = {"/foo/test/Foo1.xml","/foo/test/Foo2.xml","/foo/test/Foo3.xml", + "/foo/test/Foo4.xml","/foo/test/Foo5.xml","/foo/test/Foo6.xml", + "/foo/test/Foo7.xml","/foo/test/Foo8.xml","/foo/test/Foo8.xml"}; + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + + DocumentDescriptor docdisc = docMgr.newDescriptor("test1"); + + writeset.add(docdisc, new DOMHandle(getDocumentContent("This is so foo1"))); + writeset.add(docId[1], new DOMHandle().with(getDocumentContent("This is so foo2"))); + writeset.add(docId[2], new DOMHandle().with(getDocumentContent("This is so foo3"))); + + docMgr.write(writeset); + + DocumentPage page = docMgr.read(docId); + DocumentRecord rec; + mh1 = new DocumentMetadataHandle(); + while(page.hasNext()){ + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh1); + validateDefaultMetadata(mh1); + } + validateDefaultMetadata(mh1); + + // put metadata + mh2 = setMetadata(); + writeset =docMgr.newWriteSet(); + + writeset.add(docId[3], new DOMHandle(getDocumentContent("This is so foo4"))); + writeset.addDefault(mh2); + writeset.add(docId[4], new DOMHandle().with(getDocumentContent("This is so foo5"))); + writeset.add(docId[5], new DOMHandle().with(getDocumentContent("This is so foo6"))); + docMgr.write(writeset); + + page = docMgr.read(docId[3]); + mh2=new DocumentMetadataHandle(); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh2); + validateDefaultMetadata(mh2); + + page = docMgr.read(docId[4]); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh2); + validateMetadata(mh2); + + page = docMgr.read(docId[5]); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh2); + validateMetadata(mh2); + //Overwriting the existing properties + mh2 = setMetadata(); + writeset =docMgr.newWriteSet(); + writeset.add(docId[3], mh2,new DOMHandle(getDocumentContent("This is so foo4"))); + docMgr.write(writeset); + + page = docMgr.read(docId[3]); + mh2=new DocumentMetadataHandle(); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh2); + validateMetadata(mh2); + + } + @Test + public void testWriteMultipleBinaryDocWithDefaultMetadata2() throws Exception + { + String docId[] = {"Pandakarlino.jpg","mlfavicon.png"}; + + BinaryDocumentManager docMgr = client.newBinaryDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + // put metadata + DocumentMetadataHandle mh = setMetadata(); + + File file1= null,file2=null; + file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); + FileHandle handle1 = new FileHandle(file1); + writeset.add("/1/"+docId[0],handle1.withFormat(Format.BINARY)); + + writeset.add("/2/"+docId[0],new DocumentMetadataHandle().withQuality(5),handle1.withFormat(Format.BINARY)); + + file2 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[1]); + FileHandle handle2 = new FileHandle(file2); + writeset.add("/1/"+docId[1],new DocumentMetadataHandle().withCollections("collection1"),handle2.withFormat(Format.BINARY)); + + writeset.addDefault(mh); + + writeset.add("/2/"+docId[1],handle2.withFormat(Format.BINARY)); + docMgr.write(writeset); + + + DocumentPage page = docMgr.read("/1/Pandakarlino.jpg"); + DocumentRecord rec; + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh); +// System.out.println(rec.getUri()+mh.getQuality()); + assertEquals("default quality",0,mh.getQuality()); + validateDefaultMetadata(mh); + page = docMgr.read("/2/Pandakarlino.jpg"); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh); + assertEquals(" quality",5,mh.getQuality()); + + assertTrue("default collections reset",mh.getCollections().isEmpty()); + + page = docMgr.read("/1/mlfavicon.png"); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh); + assertEquals("default quality",0,mh.getQuality()); +// System.out.println(rec.getUri()+mh.getCollections().isEmpty()); + assertFalse("default collections reset",mh.getCollections().isEmpty()); + + page = docMgr.read("/2/mlfavicon.png"); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh); + validateMetadata(mh); +} + + /* + * This is test is made up for the github issue 25 + */ +@Test + public void testWriteMultipleJSONDocsWithDefaultMetadata2() throws Exception + { + // Synthesize input content + StringHandle doc1 = new StringHandle( + "{\"number\": 1}").withFormat(Format.JSON); + StringHandle doc2 = new StringHandle( + "{\"number\": 2}").withFormat(Format.JSON); + StringHandle doc3 = new StringHandle( + "{\"number\": 3}").withFormat(Format.JSON); + StringHandle doc4 = new StringHandle( + "{\"number\": 4}").withFormat(Format.JSON); + StringHandle doc5 = new StringHandle( + "{\"number\": 5}").withFormat(Format.JSON); + + // Synthesize input metadata + DocumentMetadataHandle defaultMetadata1 = + new DocumentMetadataHandle().withQuality(1); + DocumentMetadataHandle defaultMetadata2 = + new DocumentMetadataHandle().withQuality(2); + DocumentMetadataHandle docSpecificMetadata = + new DocumentMetadataHandle().withCollections("mySpecificCollection"); + + // Create and build up the batch + JSONDocumentManager jdm = client.newJSONDocumentManager(); + DocumentWriteSet batch = jdm.newWriteSet(); + + // use system default metadata + batch.add("doc1.json", doc1); // system default metadata + + // using batch default metadata + batch.addDefault(defaultMetadata1); + batch.add("doc2.json", doc2); // batch default metadata + batch.add("doc3.json", docSpecificMetadata, doc3); + batch.add("doc4.json", doc4); // batch default metadata + + // replace batch default metadata with new metadata + batch.addDefault(defaultMetadata2); + batch.add("doc5.json", doc5); // batch default + + // Execute the write operation + jdm.write(batch); + DocumentPage page ; + DocumentRecord rec; + // Check the results + // Doc1 should have the system default quality of 0 + page = jdm.read("doc1.json"); + DocumentMetadataHandle mh = new DocumentMetadataHandle(); + rec = page.next(); + jdm.readMetadata(rec.getUri(), mh); + validateDefaultMetadata(mh); + assertEquals("default quality",0,mh.getQuality()); + + // Doc2 should use the first batch default metadata, with quality 1 + page = jdm.read("doc2.json"); + rec = page.next(); + jdm.readMetadata(rec.getUri(), mh); + System.out.print(mh.getCollections().isEmpty()); + assertEquals("default quality",1,mh.getQuality()); + assertTrue("default collections reset",mh.getCollections().isEmpty()); + + // Doc3 should have the system default document quality (0) because quality + // was not included in the document-specific metadata. It should be in the + // collection "mySpecificCollection", from the document-specific metadata. + + page = jdm.read("doc3.json"); + rec = page.next(); + jdm.readMetadata(rec.getUri(), mh); + assertEquals("default quality",0,mh.getQuality()); + assertEquals("default collection must change","[mySpecificCollection]",mh.getCollections().toString()); + + DocumentMetadataHandle doc3Metadata = + jdm.readMetadata("doc3.json", new DocumentMetadataHandle()); + System.out.println("doc3 quality: Expected=0, Actual=" + doc3Metadata.getPermissions()); + System.out.print("doc3 collections: Expected: myCollection, Actual="); + for (String collection : doc3Metadata.getCollections()) { + System.out.print(collection + " "); + } + System.out.println(); + + // Doc 4 should also use the 1st batch default metadata, with quality 1 + page = jdm.read("doc4.json"); + rec = page.next(); + jdm.readMetadata(rec.getUri(), mh); + assertEquals("default quality",1,mh.getQuality()); + assertTrue("default collections reset",mh.getCollections().isEmpty()); + // Doc5 should use the 2nd batch default metadata, with quality 2 + page = jdm.read("doc5.json"); + rec = page.next(); + jdm.readMetadata(rec.getUri(), mh); + assertEquals("default quality",2,mh.getQuality()); + + } + @Test + public void testWriteGenericDocMgrWithDefaultMetadata() throws Exception + { + String docId[] = {"Sega-4MB.jpg"}; + + GenericDocumentManager docMgr = client.newDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + // put metadata + DocumentMetadataHandle mh = setMetadata(); + + writeset.addDefault(mh); // Adding default metadata to the entire batch + File file1= null; + file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); + FileInputStream fis = new FileInputStream(file1); + InputStreamHandle handle1 = new InputStreamHandle(fis); + handle1.setFormat(Format.BINARY); + + writeset.add("/generic/Pandakarlino.jpg",handle1); // This document implicitly gets the default metadata that we added in the begining + + 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 DocumentMetadataHandle().withQuality(10),jh); //This document suppose to get the in scope metadata quality and should set collections to system default + + String foo1 = "This is foo1 of byte Array"; + byte[] ba = foo1.getBytes(); + BytesHandle bh = new BytesHandle(ba); + bh.setFormat(Format.TEXT); + + writeset.add("/generic/foo1.txt",bh); + + DOMSource ds = new DOMSource(getDocumentContent("This is so foo1")); + SourceHandle sh = new SourceHandle(); + sh.set(ds); + sh.setFormat(Format.XML); + DocumentMetadataHandle mh2 = new DocumentMetadataHandle(); + writeset.add("/generic/foo.xml", mh2.withCollections("genericCollection"), sh); //This document should over write the system default and default collection list and get document specific collection + + docMgr.write(writeset); + + DocumentPage page = docMgr.read("/generic/Pandakarlino.jpg","/generic/foo1.txt"); + + while(page.hasNext()){ + DocumentRecord rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh); + System.out.println(rec.getUri()); + validateMetadata(mh); + } + validateMetadata(mh); + page = docMgr.read("/generic/dog.json"); + DocumentRecord rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh); + assertEquals("default quality",10,mh.getQuality()); + assertTrue("default collections missing",mh.getCollections().isEmpty()); + + page = docMgr.read("/generic/foo.xml"); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh); + assertEquals("default quality",0,mh.getQuality()); + assertEquals("default collection must change","[genericCollection]",mh.getCollections().toString()); + + } + +} From 32062d6104ccd50898593d6d9c0f279e1893ca0e Mon Sep 17 00:00:00 2001 From: gvaidees Date: Wed, 20 Aug 2014 17:45:54 -0700 Subject: [PATCH 070/357] Asserts are more robust and not string based --- .../javaclient/TestBulkWriteMetadata1.java | 653 +++++++++--------- .../javaclient/TestBulkWriteMetadata2.java | 2 +- 2 files changed, 327 insertions(+), 328 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata1.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata1.java index 79f95eea0..7a39de14c 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata1.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata1.java @@ -1,327 +1,326 @@ -/** - * - */ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileReader; -import java.io.Reader; -import java.io.StringReader; -import java.util.Calendar; - -import javax.xml.transform.dom.DOMSource; - -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.document.BinaryDocumentManager; -import com.marklogic.client.document.DocumentPage; -import com.marklogic.client.document.DocumentRecord; -import com.marklogic.client.document.DocumentWriteSet; -import com.marklogic.client.document.GenericDocumentManager; -import com.marklogic.client.document.JSONDocumentManager; -import com.marklogic.client.document.TextDocumentManager; -import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.io.BytesHandle; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.DocumentMetadataHandle; -import com.marklogic.client.io.FileHandle; -import com.marklogic.client.io.Format; -import com.marklogic.client.io.InputStreamHandle; -import com.marklogic.client.io.JacksonHandle; -import com.marklogic.client.io.ReaderHandle; -import com.marklogic.client.io.SourceHandle; -import com.marklogic.client.io.StringHandle; -import com.marklogic.client.io.DocumentMetadataHandle.Capability; -import com.marklogic.client.io.DocumentMetadataHandle.DocumentCollections; -import com.marklogic.client.io.DocumentMetadataHandle.DocumentPermissions; -import com.marklogic.client.io.DocumentMetadataHandle.DocumentProperties; - -/** - * @author skottam - *This test is designed to add default meta data bulk writes with different types of Managers and different content type like JSON,text,binary,XMl - * - * TextDocumentManager - * XMLDocumentManager - * BinaryDocumentManager - * JSONDocumentManager - * GenericDocumentManager - * - */ -public class TestBulkWriteMetadata1 extends BasicJavaClientREST { - - private static String dbName = "TestBulkWriteDefaultMetadataDB"; - private static String [] fNames = {"TestBulkWriteDefaultMetadataDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort = 8011; - private DatabaseClient client ; - /** - * @throws java.lang.Exception - */ - @BeforeClass - public static void setUpBeforeClass() throws Exception { - System.out.println("In Setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); - createRESTUser("app-user", "password", "rest-writer","rest-reader" ); - - } - - /** - * @throws java.lang.Exception - */ - @AfterClass - public static void tearDownAfterClass() throws Exception { - System.out.println("In tear down" ); - tearDownJavaRESTServer(dbName, fNames, restServerName); - deleteRESTUser("app-user"); - } - - /** - * @throws java.lang.Exception - */ - @Before - public void setUp() throws Exception { - // create new connection for each test below - client = DatabaseClientFactory.newClient("localhost", restPort, "app-user", "password", Authentication.DIGEST); - } - - /** - * @throws java.lang.Exception - */ - @After - public void tearDown() throws Exception { - System.out.println("Running clear script"); - // release client - client.release(); - } - - public DocumentMetadataHandle setMetadata(){ - // create and initialize a handle on the metadata - DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); - metadataHandle.getCollections().addAll("my-collection1","my-collection2"); - metadataHandle.getPermissions().add("app-user", Capability.UPDATE, Capability.READ); - metadataHandle.getProperties().put("reviewed", true); - metadataHandle.getProperties().put("myString", "foo"); - metadataHandle.getProperties().put("myInteger", 10); - metadataHandle.getProperties().put("myDecimal", 34.56678); - metadataHandle.getProperties().put("myCalendar", Calendar.getInstance().get(Calendar.YEAR)); - metadataHandle.setQuality(23); - return metadataHandle; - } - public void validateMetadata(DocumentMetadataHandle mh){ - - // get metadata values - DocumentProperties properties = mh.getProperties(); - DocumentPermissions permissions = mh.getPermissions(); - DocumentCollections collections = mh.getCollections(); - - // Properties - String expectedProperties = "size:5|reviewed:true|myInteger:10|myDecimal:34.56678|myCalendar:2014|myString:foo|"; - String actualProperties = getDocumentPropertiesString(properties); - assertEquals("Document properties difference", expectedProperties, actualProperties); - - // Permissions - String expectedPermissions1 = "size:3|rest-reader:[READ]|app-user:[UPDATE, READ]|rest-writer:[UPDATE]|"; - String expectedPermissions2 = "size:3|rest-reader:[READ]|app-user:[READ, UPDATE]|rest-writer:[UPDATE]|"; - String actualPermissions = getDocumentPermissionsString(permissions); - if(actualPermissions.contains("[UPDATE, READ]")) - assertEquals("Document permissions difference", expectedPermissions1, actualPermissions); - else if(actualPermissions.contains("[READ, UPDATE]")) - assertEquals("Document permissions difference", expectedPermissions2, actualPermissions); - else - assertEquals("Document permissions difference", "wrong", actualPermissions); - - // Collections - String expectedCollections = "size:2|my-collection1|my-collection2|"; - String actualCollections = getDocumentCollectionsString(collections); - assertEquals("Document collections difference", expectedCollections, actualCollections); - - } - - @Test - public void testWriteMultipleTextDocWithDefaultMetadata() - { - DocumentMetadataHandle mh1,mh2; - String docId[] = {"/foo/test/myFoo1.txt","/foo/test/myFoo2.txt","/foo/test/myFoo3.txt"}; - - TextDocumentManager docMgr = client.newTextDocumentManager(); - - DocumentWriteSet writeset =docMgr.newWriteSet(); - // put metadata - DocumentMetadataHandle mh = setMetadata(); - - writeset.addDefault(mh); - writeset.add(docId[0], new StringHandle().with("This is so foo1")); - writeset.add(docId[1], new StringHandle().with("This is so foo2")); - writeset.add(docId[2], new StringHandle().with("This is so foo3")); - docMgr.write(writeset); - DocumentPage page = docMgr.read(docId); - - while(page.hasNext()){ - DocumentRecord rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh); - validateMetadata(mh); - } - validateMetadata(mh); - } - @Test - public void testWriteMultipleXMLDocWithDefaultMetadata() throws Exception - { - String docId[] = {"/foo/test/Foo1.xml","/foo/test/Foo2.xml","/foo/test/Foo3.xml"}; - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - // put metadata - DocumentMetadataHandle mh = setMetadata(); - - writeset.addDefault(mh); - writeset.add(docId[0], new DOMHandle(getDocumentContent("This is so foo1"))); - writeset.add(docId[1], new DOMHandle().with(getDocumentContent("This is so foo2"))); - writeset.add(docId[2], new DOMHandle().with(getDocumentContent("This is so foo3"))); - - docMgr.write(writeset); - - DocumentPage page = docMgr.read(docId); - - while(page.hasNext()){ - DocumentRecord rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh); - validateMetadata(mh); - } - validateMetadata(mh); - } - @Test - public void testWriteMultipleBinaryDocWithDefaultMetadata() throws Exception - { - String docId[] = {"Pandakarlino.jpg","mlfavicon.png"}; - - BinaryDocumentManager docMgr = client.newBinaryDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - // put metadata - DocumentMetadataHandle mh = setMetadata(); - - writeset.addDefault(mh); - File file1= null,file2=null; - file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); - FileHandle handle1 = new FileHandle(file1); - writeset.add("/1/"+docId[0],handle1.withFormat(Format.BINARY)); - writeset.add("/2/"+docId[0],handle1.withFormat(Format.BINARY)); - file2 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[1]); - FileHandle handle2 = new FileHandle(file2); - writeset.add("/1/"+docId[1],handle2.withFormat(Format.BINARY)); - writeset.add("/2/"+docId[1],handle2.withFormat(Format.BINARY)); - - docMgr.write(writeset); - String uris[] = new String[102]; - int j=0; - for(int i =1;i<3;i++){ - uris[i]="/"+i+"/"+docId[j]; - uris[i]="/"+i+"/"+docId[j+1]; - } - DocumentPage page = docMgr.read(uris); - - while(page.hasNext()){ - DocumentRecord rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh); - System.out.println(rec.getUri()); - validateMetadata(mh); - } - validateMetadata(mh); - } - - @Test - public void testWriteMultipleJSONDocsWithDefaultMetadata() throws Exception - { - String docId[] = {"/a.json","/b.json","/c.json"}; - String json1 = new String("{\"animal\":\"dog\", \"says\":\"woof\"}"); - String json2 = new String("{\"animal\":\"cat\", \"says\":\"meow\"}"); - String json3 = new String("{\"animal\":\"rat\", \"says\":\"keek\"}"); - Reader strReader = new StringReader(json1); - JSONDocumentManager docMgr = client.newJSONDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - // put metadata - DocumentMetadataHandle mh = setMetadata(); - - writeset.addDefault(mh); - writeset.add(docId[0],new ReaderHandle(strReader).withFormat(Format.JSON)); - writeset.add(docId[1],new ReaderHandle(new StringReader(json2))); - writeset.add(docId[2],new ReaderHandle(new StringReader(json3))); - - docMgr.write(writeset); - - DocumentPage page = docMgr.read(docId); - - while(page.hasNext()){ - DocumentRecord rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh); - System.out.println(rec.getUri()); - validateMetadata(mh); - } - validateMetadata(mh); - } - @Test - public void testWriteGenericDocMgrWithDefaultMetadata() throws Exception - { - String docId[] = {"Pandakarlino.jpg","mlfavicon.png"}; - - GenericDocumentManager docMgr = client.newDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - // put metadata - DocumentMetadataHandle mh = setMetadata(); - - writeset.addDefault(mh); - File file1= null; - file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); - FileInputStream fis = new FileInputStream(file1); - InputStreamHandle handle1 = new InputStreamHandle(fis); - handle1.setFormat(Format.BINARY); - - writeset.add("/generic/Pandakarlino.jpg",handle1); - - 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",jh); - - String foo1 = "This is foo1 of byte Array"; - byte[] ba = foo1.getBytes(); - BytesHandle bh = new BytesHandle(ba); - bh.setFormat(Format.TEXT); - - writeset.add("/generic/foo1.txt",bh); - - DOMSource ds = new DOMSource(getDocumentContent("This is so foo1")); - SourceHandle sh = new SourceHandle(); - sh.set(ds); - sh.setFormat(Format.XML); - - writeset.add("/generic/foo.xml",sh); - - docMgr.write(writeset); - - DocumentPage page = docMgr.read("/generic/Pandakarlino.jpg","/generic/dog.json","/generic/foo1.txt","/generic/foo.xml"); - - while(page.hasNext()){ - DocumentRecord rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh); - System.out.println(rec.getUri()); - validateMetadata(mh); - } - validateMetadata(mh); - } - -} +/** + * + */ +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.FileInputStream; +import java.io.Reader; +import java.io.StringReader; +import java.util.Calendar; + +import javax.xml.transform.dom.DOMSource; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.BinaryDocumentManager; +import com.marklogic.client.document.DocumentPage; +import com.marklogic.client.document.DocumentRecord; +import com.marklogic.client.document.DocumentWriteSet; +import com.marklogic.client.document.GenericDocumentManager; +import com.marklogic.client.document.JSONDocumentManager; +import com.marklogic.client.document.TextDocumentManager; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.io.BytesHandle; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.DocumentMetadataHandle; +import com.marklogic.client.io.FileHandle; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.InputStreamHandle; +import com.marklogic.client.io.JacksonHandle; +import com.marklogic.client.io.ReaderHandle; +import com.marklogic.client.io.SourceHandle; +import com.marklogic.client.io.StringHandle; +import com.marklogic.client.io.DocumentMetadataHandle.Capability; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentCollections; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentPermissions; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentProperties; + +/** + * @author skottam + *This test is designed to add default meta data bulk writes with different types of Managers and different content type like JSON,text,binary,XMl + * + * TextDocumentManager + * XMLDocumentManager + * BinaryDocumentManager + * JSONDocumentManager + * GenericDocumentManager + * + */ +public class TestBulkWriteMetadata1 extends BasicJavaClientREST { + + private static String dbName = "TestBulkWriteDefaultMetadataDB"; + private static String [] fNames = {"TestBulkWriteDefaultMetadataDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client ; + /** + * @throws java.lang.Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + System.out.println("In Setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + createRESTUser("app-user", "password", "rest-writer","rest-reader" ); + + } + + /** + * @throws java.lang.Exception + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + System.out.println("In tear down" ); + tearDownJavaRESTServer(dbName, fNames, restServerName); + deleteRESTUser("app-user"); + } + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + // create new connection for each test below + client = DatabaseClientFactory.newClient("localhost", restPort, "app-user", "password", Authentication.DIGEST); + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + System.out.println("Running clear script"); + // release client + client.release(); + } + + public DocumentMetadataHandle setMetadata(){ + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); + metadataHandle.getCollections().addAll("my-collection1","my-collection2"); + metadataHandle.getPermissions().add("app-user", Capability.UPDATE, Capability.READ); + metadataHandle.getProperties().put("reviewed", true); + metadataHandle.getProperties().put("myString", "foo"); + metadataHandle.getProperties().put("myInteger", 10); + metadataHandle.getProperties().put("myDecimal", 34.56678); + metadataHandle.getProperties().put("myCalendar", Calendar.getInstance().get(Calendar.YEAR)); + metadataHandle.setQuality(23); + return metadataHandle; + } + public void validateMetadata(DocumentMetadataHandle mh){ + + // get metadata values + DocumentProperties properties = mh.getProperties(); + DocumentPermissions permissions = mh.getPermissions(); + DocumentCollections collections = mh.getCollections(); + + // Properties + String actualProperties = getDocumentPropertiesString(properties); + + assertTrue("Document properties difference in size value", actualProperties.contains("size:5")); + assertTrue("Document property reviewed not found or not correct", actualProperties.contains("reviewed:true")); + assertTrue("Document property myInteger not found or not correct", actualProperties.contains("myInteger:10")); + assertTrue("Document property myDecimal not found or not correct", actualProperties.contains("myDecimal:34.56678")); + assertTrue("Document property myCalendar not found or not correct", actualProperties.contains("myCalendar:2014")); + assertTrue("Document property myString not found or not correct", actualProperties.contains("myString:foo")); + + // Permissions + String actualPermissions = getDocumentPermissionsString(permissions); + assertTrue("Document permissions difference in size value", actualPermissions.contains("size:3")); + assertTrue("Document permissions difference in rest-reader permission", actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue("Document permissions difference in app-user permissions", + (actualPermissions.contains("app-user:[UPDATE, READ]") || actualPermissions.contains("app-user:[READ, UPDATE]"))); + + // Collections + String actualCollections = getDocumentCollectionsString(collections); + assertTrue("Document collections difference in size value", actualCollections.contains("size:2")); + assertTrue("my-collection1 not found", actualCollections.contains("my-collection1")); + assertTrue("my-collection2 not found", actualCollections.contains("my-collection2")); + } + + @Test + public void testWriteMultipleTextDocWithDefaultMetadata() + { + String docId[] = {"/foo/test/myFoo1.txt","/foo/test/myFoo2.txt","/foo/test/myFoo3.txt"}; + + TextDocumentManager docMgr = client.newTextDocumentManager(); + + DocumentWriteSet writeset =docMgr.newWriteSet(); + // put metadata + DocumentMetadataHandle mh = setMetadata(); + + writeset.addDefault(mh); + writeset.add(docId[0], new StringHandle().with("This is so foo1")); + writeset.add(docId[1], new StringHandle().with("This is so foo2")); + writeset.add(docId[2], new StringHandle().with("This is so foo3")); + docMgr.write(writeset); + DocumentPage page = docMgr.read(docId); + + while(page.hasNext()){ + DocumentRecord rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh); + validateMetadata(mh); + } + validateMetadata(mh); + } + @Test + public void testWriteMultipleXMLDocWithDefaultMetadata() throws Exception + { + String docId[] = {"/foo/test/Foo1.xml","/foo/test/Foo2.xml","/foo/test/Foo3.xml"}; + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + // put metadata + DocumentMetadataHandle mh = setMetadata(); + + writeset.addDefault(mh); + writeset.add(docId[0], new DOMHandle(getDocumentContent("This is so foo1"))); + writeset.add(docId[1], new DOMHandle().with(getDocumentContent("This is so foo2"))); + writeset.add(docId[2], new DOMHandle().with(getDocumentContent("This is so foo3"))); + + docMgr.write(writeset); + + DocumentPage page = docMgr.read(docId); + + while(page.hasNext()){ + DocumentRecord rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh); + validateMetadata(mh); + } + validateMetadata(mh); + } + @Test + public void testWriteMultipleBinaryDocWithDefaultMetadata() throws Exception + { + String docId[] = {"Pandakarlino.jpg","mlfavicon.png"}; + + BinaryDocumentManager docMgr = client.newBinaryDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + // put metadata + DocumentMetadataHandle mh = setMetadata(); + + writeset.addDefault(mh); + File file1= null,file2=null; + file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); + FileHandle handle1 = new FileHandle(file1); + writeset.add("/1/"+docId[0],handle1.withFormat(Format.BINARY)); + writeset.add("/2/"+docId[0],handle1.withFormat(Format.BINARY)); + file2 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[1]); + FileHandle handle2 = new FileHandle(file2); + writeset.add("/1/"+docId[1],handle2.withFormat(Format.BINARY)); + writeset.add("/2/"+docId[1],handle2.withFormat(Format.BINARY)); + + docMgr.write(writeset); + String uris[] = new String[102]; + int j=0; + for(int i =1;i<3;i++){ + uris[i]="/"+i+"/"+docId[j]; + uris[i]="/"+i+"/"+docId[j+1]; + } + DocumentPage page = docMgr.read(uris); + + while(page.hasNext()){ + DocumentRecord rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh); + System.out.println(rec.getUri()); + validateMetadata(mh); + } + validateMetadata(mh); + } + + @Test + public void testWriteMultipleJSONDocsWithDefaultMetadata() throws Exception + { + String docId[] = {"/a.json","/b.json","/c.json"}; + String json1 = new String("{\"animal\":\"dog\", \"says\":\"woof\"}"); + String json2 = new String("{\"animal\":\"cat\", \"says\":\"meow\"}"); + String json3 = new String("{\"animal\":\"rat\", \"says\":\"keek\"}"); + Reader strReader = new StringReader(json1); + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + // put metadata + DocumentMetadataHandle mh = setMetadata(); + + writeset.addDefault(mh); + writeset.add(docId[0],new ReaderHandle(strReader).withFormat(Format.JSON)); + writeset.add(docId[1],new ReaderHandle(new StringReader(json2))); + writeset.add(docId[2],new ReaderHandle(new StringReader(json3))); + + docMgr.write(writeset); + + DocumentPage page = docMgr.read(docId); + + while(page.hasNext()){ + DocumentRecord rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh); + System.out.println(rec.getUri()); + validateMetadata(mh); + } + validateMetadata(mh); + } + @Test + public void testWriteGenericDocMgrWithDefaultMetadata() throws Exception + { + String docId[] = {"Pandakarlino.jpg","mlfavicon.png"}; + + GenericDocumentManager docMgr = client.newDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + // put metadata + DocumentMetadataHandle mh = setMetadata(); + + writeset.addDefault(mh); + File file1= null; + file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); + FileInputStream fis = new FileInputStream(file1); + InputStreamHandle handle1 = new InputStreamHandle(fis); + handle1.setFormat(Format.BINARY); + + writeset.add("/generic/Pandakarlino.jpg",handle1); + + 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",jh); + + String foo1 = "This is foo1 of byte Array"; + byte[] ba = foo1.getBytes(); + BytesHandle bh = new BytesHandle(ba); + bh.setFormat(Format.TEXT); + + writeset.add("/generic/foo1.txt",bh); + + DOMSource ds = new DOMSource(getDocumentContent("This is so foo1")); + SourceHandle sh = new SourceHandle(); + sh.set(ds); + sh.setFormat(Format.XML); + + writeset.add("/generic/foo.xml",sh); + + docMgr.write(writeset); + + DocumentPage page = docMgr.read("/generic/Pandakarlino.jpg","/generic/dog.json","/generic/foo1.txt","/generic/foo.xml"); + + while(page.hasNext()){ + DocumentRecord rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh); + System.out.println(rec.getUri()); + validateMetadata(mh); + } + validateMetadata(mh); + } + +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata2.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata2.java index 39bef805b..b1a62a1e8 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata2.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata2.java @@ -145,7 +145,7 @@ public void validateMetadata(DocumentMetadataHandle mh){ // Collections String actualCollections = getDocumentCollectionsString(collections); - assertTrue("Document permissions difference in size value", actualCollections.contains("size:2")); + assertTrue("Document collections difference in size value", actualCollections.contains("size:2")); assertTrue("my-collection1 not found", actualCollections.contains("my-collection1")); assertTrue("my-collection2 not found", actualCollections.contains("my-collection2")); } From 1cb4469729aa3afe2c4e4c6e61c3bb9782bfaad9 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Wed, 20 Aug 2014 17:57:38 -0700 Subject: [PATCH 071/357] More robustness in terms of asserts. Checking individual values --- .../javaclient/TestBulkWriteMetadata2.java | 4 +- .../TestBulkWriteMetadatawithRawXML.java | 1003 +++++++++-------- 2 files changed, 505 insertions(+), 502 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata2.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata2.java index b1a62a1e8..bfadc6fb1 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata2.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata2.java @@ -144,7 +144,9 @@ public void validateMetadata(DocumentMetadataHandle mh){ (actualPermissions.contains("app-user:[UPDATE, READ]") || actualPermissions.contains("app-user:[READ, UPDATE]"))); // Collections - String actualCollections = getDocumentCollectionsString(collections); + String actualCollections = getDocumentCollectionsString(collections); + System.out.println(collections); + assertTrue("Document collections difference in size value", actualCollections.contains("size:2")); assertTrue("my-collection1 not found", actualCollections.contains("my-collection1")); assertTrue("my-collection2 not found", actualCollections.contains("my-collection2")); diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadatawithRawXML.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadatawithRawXML.java index dcfae9128..76d68dfa1 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadatawithRawXML.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadatawithRawXML.java @@ -1,501 +1,502 @@ -/** - * - */ -package com.marklogic.javaclient; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; -import static org.junit.Assert.*; - -import java.io.File; -import java.io.FileInputStream; -import java.io.Reader; -import java.io.StringReader; -import java.util.Calendar; -import java.util.HashMap; -import java.util.Map; - -import javax.xml.transform.dom.DOMSource; - -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.w3c.dom.Document; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ObjectNode; -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.document.BinaryDocumentManager; -import com.marklogic.client.document.DocumentPage; -import com.marklogic.client.document.DocumentRecord; -import com.marklogic.client.document.DocumentWriteSet; -import com.marklogic.client.document.DocumentDescriptor; -import com.marklogic.client.document.GenericDocumentManager; -import com.marklogic.client.document.JSONDocumentManager; -import com.marklogic.client.document.TextDocumentManager; -import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.io.BytesHandle; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.DocumentMetadataHandle; -import com.marklogic.client.io.FileHandle; -import com.marklogic.client.io.Format; -import com.marklogic.client.io.InputStreamHandle; -import com.marklogic.client.io.JacksonHandle; -import com.marklogic.client.io.JacksonParserHandle; -import com.marklogic.client.io.ReaderHandle; -import com.marklogic.client.io.SourceHandle; -import com.marklogic.client.io.StringHandle; -import com.marklogic.client.io.DocumentMetadataHandle.Capability; -import com.marklogic.client.io.DocumentMetadataHandle.DocumentCollections; -import com.marklogic.client.io.DocumentMetadataHandle.DocumentPermissions; -import com.marklogic.client.io.DocumentMetadataHandle.DocumentProperties; -import com.marklogic.client.FailedRequestException; -/** - * @author skottam - * This test is test the DocumentWriteSet function - * public DocumentWriteSet add(String uri, DocumentMetadataWriteHandle metadataHandle); - * This test intention is to test the system default , request wide, and document specific update to metadata will overwrite the existing metadata - * setup: create a usr1 with system level meta data as default - * client make a connection with usr1 to do bulk loading - * load set of documents where default metadata is set with raw xml or json documents - * test disableDefault(). - * - */ -public class TestBulkWriteMetadatawithRawXML extends BasicJavaClientREST{ - - private static String dbName = "TestBulkWriteDefaultMetadataDB3"; - private static String [] fNames = {"TestBulkWriteDefaultMetadataDB-3"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort = 8011; - private DatabaseClient client ; - /** - * @throws java.lang.Exception - */ - @BeforeClass - public static void setUpBeforeClass() throws Exception { - System.out.println("In Setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); - createRESTUser("app-user", "password","rest-writer","rest-reader" ); - createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); - } - - /** - * @throws java.lang.Exception - */ - @AfterClass - public static void tearDownAfterClass() throws Exception { - System.out.println("In tear down" ); - tearDownJavaRESTServer(dbName, fNames, restServerName); - deleteRESTUser("app-user"); - deleteRESTUser("usr1"); - } - - /** - * @throws java.lang.Exception - */ - @Before - public void setUp() throws Exception { - // create new connection for each test below - client = DatabaseClientFactory.newClient("localhost", restPort, "usr1", "password", Authentication.DIGEST); - } - - /** - * @throws java.lang.Exception - */ - @After - public void tearDown() throws Exception { - System.out.println("Running clear script"); - // release client - client.release(); - } - - public DocumentMetadataHandle setMetadata(){ - // create and initialize a handle on the metadata - DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); - metadataHandle.getCollections().addAll("my-collection1","my-collection2"); - metadataHandle.getPermissions().add("app-user", Capability.UPDATE, Capability.READ); - metadataHandle.getProperties().put("reviewed", true); - metadataHandle.getProperties().put("myString", "foo"); - metadataHandle.getProperties().put("myInteger", 10); - metadataHandle.getProperties().put("myDecimal", 34.56678); - metadataHandle.getProperties().put("myCalendar", Calendar.getInstance().get(Calendar.YEAR)); - metadataHandle.setQuality(23); - return metadataHandle; - } - public void validateMetadata(DocumentMetadataHandle mh){ - - // get metadata values - DocumentProperties properties = mh.getProperties(); - DocumentPermissions permissions = mh.getPermissions(); - DocumentCollections collections = mh.getCollections(); - - // Properties - // String expectedProperties = "size:5|reviewed:true|myInteger:10|myDecimal:34.56678|myCalendar:2014|myString:foo|"; - String actualProperties = getDocumentPropertiesString(properties); - boolean result = actualProperties.contains("size:5|"); - assertTrue("Document properties count", result); - - // Permissions - String expectedPermissions1 = "size:4|flexrep-eval:[READ]|rest-reader:[READ]|app-user:[UPDATE, READ]|rest-writer:[UPDATE]|"; - String expectedPermissions2 = "size:4|flexrep-eval:[READ]|rest-reader:[READ]|app-user:[READ, UPDATE]|rest-writer:[UPDATE]|"; - String actualPermissions = getDocumentPermissionsString(permissions); - System.out.println(actualPermissions); - if(actualPermissions.contains("[UPDATE, READ]")) - assertEquals("Document permissions difference", expectedPermissions1, actualPermissions); - else if(actualPermissions.contains("[READ, UPDATE]")) - assertEquals("Document permissions difference", expectedPermissions2, actualPermissions); - else - assertEquals("Document permissions difference", "wrong", actualPermissions); - - // Collections - String expectedCollections = "size:2|my-collection1|my-collection2|"; - String actualCollections = getDocumentCollectionsString(collections); - assertEquals("Document collections difference", expectedCollections, actualCollections); - - } - public void validateDefaultMetadata(DocumentMetadataHandle mh){ - - // get metadata values - - DocumentPermissions permissions = mh.getPermissions(); - DocumentCollections collections = mh.getCollections(); - - // Permissions - - String expectedPermissions1 = "size:3|flexrep-eval:[READ]|rest-reader:[READ]|rest-writer:[UPDATE]|"; - String actualPermissions = getDocumentPermissionsString(permissions); - assertEquals("Document permissions difference", expectedPermissions1, actualPermissions); - - // Collections - String expectedCollections = "size:1|http://permission-collections/|"; - String actualCollections = getDocumentCollectionsString(collections); - - assertEquals("Document collections difference", expectedCollections, actualCollections); -// System.out.println(actualPermissions); - } - @Test - public void testWriteMultipleTextDocWithXMLMetadata() throws Exception - { - DocumentMetadataHandle mh1,mh2; - String docId[] = {"/foo/test/myFoo1.txt","/foo/test/myFoo2.txt","/foo/test/myFoo3.txt", - "/foo/test/myFoo4.txt","/foo/test/myFoo5.txt","/foo/test/myFoo6.txt", - "/foo/test/myFoo7.txt","/foo/test/myFoo8.txt","/foo/test/myFoo9.txt"}; - - TextDocumentManager docMgr = client.newTextDocumentManager(); - - DocumentWriteSet writeset =docMgr.newWriteSet(); - // get the original metadata - Document docMetadata = getXMLMetadata("metadata-original.xml"); - // create handle to write metadata - DOMHandle writeMetadataHandle = new DOMHandle(); - writeMetadataHandle.set(docMetadata); - - writeset.addDefault(writeMetadataHandle); - - writeset.add(docId[0], new StringHandle().with("This is so foo1")); - writeset.add(docId[1], new StringHandle().with("This is so foo2")); - writeset.add(docId[2], new StringHandle().with("This is so foo3")); - docMgr.write(writeset); - //Default properties for are document set - - DocumentPage page = docMgr.read(docId); - DOMHandle mh= new DOMHandle(); - while(page.hasNext()){ - DocumentRecord rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh); - Document docReadMetadata = mh.get(); - assertXpathEvaluatesTo("coll1", "string(//*[local-name()='collection'][1])", docReadMetadata); - assertXpathEvaluatesTo("coll2", "string(//*[local-name()='collection'][2])", docReadMetadata); - assertXpathEvaluatesTo("MarkLogic", "string(//*[local-name()='Author'])", docReadMetadata); - } - - - //Adding document specific properties in document set2 - - writeset =docMgr.newWriteSet(); - // put metadata - mh2 = setMetadata(); - - writeset.add(docId[3], new StringHandle().with("This is so foo4")); - writeset.add(docId[4],mh2, new StringHandle().with("This is so foo5")); - writeset.add(docId[5],writeMetadataHandle, new StringHandle().with("This is so foo6")); - docMgr.write(writeset); - - - page = docMgr.read(docId[3]); - DocumentRecord rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh2); - validateDefaultMetadata(mh2); - - page = docMgr.read(docId[4]); - mh2=new DocumentMetadataHandle(); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh2); - validateMetadata(mh2); - - - page = docMgr.read(docId[5]); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh); - Document docReadMetadata = mh.get(); - assertXpathEvaluatesTo("coll1", "string(//*[local-name()='collection'][1])", docReadMetadata); - assertXpathEvaluatesTo("coll2", "string(//*[local-name()='collection'][2])", docReadMetadata); - assertXpathEvaluatesTo("MarkLogic", "string(//*[local-name()='Author'])", docReadMetadata); - - } - @Test - public void testWriteMultipleXMLDocWithXMLMetadata() throws Exception - { - // put metadata - - String docId[] = {"/foo/test/Foo1.xml","/foo/test/Foo2.xml","/foo/test/Foo3.xml", - "/foo/test/Foo4.xml","/foo/test/Foo5.xml","/foo/test/Foo6.xml", - "/foo/test/Foo7.xml","/foo/test/Foo8.xml","/foo/test/Foo8.xml"}; - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - // get the original metadata - Document docMetadata = getXMLMetadata("metadata-updated.xml"); - // create handle to write metadata - DOMHandle writeMetadataHandle = new DOMHandle(); - writeMetadataHandle.set(docMetadata); - - writeset.addDefault(writeMetadataHandle); - - DocumentDescriptor docdisc = docMgr.newDescriptor("test1"); - - writeset.add(docdisc, new DOMHandle(getDocumentContent("This is so foo1"))); - writeset.add(docId[1], new DOMHandle().with(getDocumentContent("This is so foo2"))); - writeset.add(docId[2], new DOMHandle().with(getDocumentContent("This is so foo3"))); - - docMgr.write(writeset); - - DocumentPage page = docMgr.read(docId); - DocumentRecord rec; - Document docReadMetadata =null; - DOMHandle mh= new DOMHandle(); - while(page.hasNext()){ - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh); - docReadMetadata = mh.get(); - assertXpathEvaluatesTo("coll1", "string(//*[local-name()='collection'][1])", docReadMetadata); - assertXpathEvaluatesTo("coll3", "string(//*[local-name()='collection'][2])", docReadMetadata); - assertXpathEvaluatesTo("23", "string(//*[local-name()='quality'])", docReadMetadata); - assertXpathEvaluatesTo("Aries", "string(//*[local-name()='Author'])", docReadMetadata); - } - assertXpathEvaluatesTo("coll1", "string(//*[local-name()='collection'][1])", docReadMetadata); - - // put metadata - DocumentMetadataHandle mh2 = setMetadata(); - writeset =docMgr.newWriteSet(); - - writeset.add(docId[3], new DOMHandle(getDocumentContent("This is so foo4"))); - writeset.addDefault(mh2); - writeset.add(docId[4],writeMetadataHandle ,new DOMHandle().with(getDocumentContent("This is so foo5"))); - writeset.add(docId[5], new DOMHandle().with(getDocumentContent("This is so foo6"))); - docMgr.write(writeset); - - page = docMgr.read(docId[3]); - mh2=new DocumentMetadataHandle(); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh2); - validateDefaultMetadata(mh2); - - page = docMgr.read(docId[4]); - rec= page.next(); - docMgr.readMetadata(rec.getUri(), mh); - docReadMetadata = mh.get(); - System.out.println(rec.getUri()+ " "+ docReadMetadata+docId[4]); - assertXpathEvaluatesTo("coll1", "string(//*[local-name()='collection'][1])", docReadMetadata); - assertXpathEvaluatesTo("coll3", "string(//*[local-name()='collection'][2])", docReadMetadata); - assertXpathEvaluatesTo("23", "string(//*[local-name()='quality'])", docReadMetadata); - assertXpathEvaluatesTo("Aries", "string(//*[local-name()='Author'])", docReadMetadata); - - page = docMgr.read(docId[5]); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh2); - validateMetadata(mh2); - - } - - - /* - * This is test is made up for the github issue 41 - */ -@Test - public void testWriteMultipleJSONDocsWithRawJSONMetadata() throws Exception - { - // Synthesize input content - - - StringHandle doc1 = new StringHandle( - "{\"number\": 1}").withFormat(Format.JSON); - StringHandle doc2 = new StringHandle( - "{\"number\": 2}").withFormat(Format.JSON); - StringHandle doc3 = new StringHandle( - "{\"number\": 3}").withFormat(Format.JSON); - StringHandle doc4 = new StringHandle( - "{\"number\": 4}").withFormat(Format.JSON); - StringHandle doc5 = new StringHandle( - "{\"number\": 5}").withFormat(Format.JSON); - - // Synthesize input metadata - - ObjectMapper mapper = new ObjectMapper(); - JacksonHandle defaultMetadata1 = new JacksonHandle(); - Map 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/")); - - // Create and build up the batch - JSONDocumentManager jdm = client.newJSONDocumentManager(); - DocumentWriteSet batch = jdm.newWriteSet(); - - // use system default metadata - batch.add("doc1.json", doc1); // system default metadata - // using batch default metadata - batch.addDefault(defaultMetadata1); - batch.add("doc2.json", doc2); // batch default metadata - batch.add("doc3.json", docSpecificMetadata, doc3); - batch.add("doc4.json", doc4); // batch default metadata - - // replace batch default metadata with new metadata - batch.addDefault(defaultMetadata2); - batch.add("doc5.json", doc5); // batch default - - // Execute the write operation - jdm.write(batch); - DocumentPage page ; - DocumentRecord rec; - // Check the results - // Doc1 should have the system default quality of 0 - page = jdm.read("doc1.json"); - DocumentMetadataHandle mh = new DocumentMetadataHandle(); - rec = page.next(); - jdm.readMetadata(rec.getUri(), mh); - System.out.print(mh.getQuality()); - validateDefaultMetadata(mh); - assertEquals("default quality",0,mh.getQuality()); - - // Doc2 should use the first batch default metadata, that has only properties i.e. rest needs to be default to system defaults - page = jdm.read("doc2.json"); - rec = page.next(); - jdm.readMetadata(rec.getUri(), mh); - System.out.print(mh.getCollections().isEmpty()); - assertEquals("default quality",0,mh.getQuality()); - assertTrue("default collections reset",mh.getCollections().isEmpty()); - - // Doc3 should have the system default document quality (0) because quality - // was not included in the document-specific metadata. It should be in the - // collection "mySpecificCollection", from the document-specific metadata. - - page = jdm.read("doc3.json"); - rec = page.next(); - jdm.readMetadata(rec.getUri(), mh); - assertEquals("default quality",0,mh.getQuality()); - assertEquals("default collection must change","[http://Json-Uri-spec-collections/]",mh.getCollections().toString()); - - // Doc 4 should also use the 1st batch default metadata, with quality 1 - page = jdm.read("doc4.json"); - rec = page.next(); - jdm.readMetadata(rec.getUri(), mh); - assertEquals("default quality",0,mh.getQuality()); - assertTrue("default collections reset",mh.getProperties().containsValue("9")); - // Doc5 should use the 2nd batch default metadata, with quality 2 - page = jdm.read("doc5.json"); - rec = page.next(); - jdm.readMetadata(rec.getUri(), mh); - assertEquals("default quality",20,mh.getQuality()); - - } -/* - * Git Issue #24 is tested here - * - */ - @Test - public void testWriteGenericDocMgrWithDefaultMetadata() throws Exception - { - String docId[] = {"Sega-4MB.jpg"}; - - GenericDocumentManager docMgr = client.newDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - // put metadata - ObjectMapper mapper = new ObjectMapper(); - JacksonHandle mh = new JacksonHandle(); - Map p = new HashMap(); - p.put("myString", "Generic JSON"); - p.put("myInt", "19"); - mh.with(constructJSONPropertiesMetadata(p)); - - writeset.addDefault(mh); // Adding default metadata to the entire batch - File file1= null; - file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); - FileInputStream fis = new FileInputStream(file1); - InputStreamHandle handle1 = new InputStreamHandle(fis); - handle1.setFormat(Format.BINARY); - - writeset.add("/generic/Sega.jpg",handle1); // This document implicitly gets the default metadata that we added in the begining - - 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 document suppose to get the in scope metadata quality and should set collections to system default - writeset.disableDefault(); - String foo1 = "This is foo1 of byte Array"; - byte[] ba = foo1.getBytes(); - BytesHandle bh = new BytesHandle(ba); - bh.setFormat(Format.TEXT); - - writeset.add("/generic/foo1.txt",bh); - - DOMSource ds = new DOMSource(getDocumentContent("This is so foo1")); - SourceHandle sh = new SourceHandle(); - sh.set(ds); - sh.setFormat(Format.XML); - DocumentMetadataHandle mh2 = new DocumentMetadataHandle(); - writeset.add("/generic/foo.xml", new JacksonHandle().with(constructJSONCollectionMetadata("http://Json-Uri-generic-collections/")), sh); //This document should over write the system default and default collection list and get document specific collection - - docMgr.write(writeset); - DocumentMetadataHandle mh1 = new DocumentMetadataHandle(); - DocumentPage page = docMgr.read("/generic/Sega.jpg"); - DocumentRecord rec = page.next(); - docMgr.readMetadata(rec.getUri(),mh1); - assertEquals("default quality",0,mh1.getQuality()); - assertTrue("Properties contains value 19",mh1.getProperties().containsValue("19")); - - page = docMgr.read("/generic/dog.json"); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh1); - assertEquals("default quality",10,mh1.getQuality()); - assertTrue("default collections missing",mh1.getCollections().isEmpty()); - - page = docMgr.read("/generic/foo1.txt"); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh1); - // until issue 24 is fixed - //this.validateDefaultMetadata(mh1); - assertEquals("default quality",0,mh1.getQuality()); - assertEquals("default permissions","{flexrep-eval=[READ], rest-reader=[READ], rest-writer=[UPDATE]}",mh1.getPermissions().toString()); - page = docMgr.read("/generic/foo.xml"); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh1); - assertEquals("default quality",0,mh1.getQuality()); - assertEquals("default collection must change","[http://Json-Uri-generic-collections/]",mh1.getCollections().toString()); - - } - -} +/** + * + */ +package com.marklogic.javaclient; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; +import static org.junit.Assert.*; + +import java.io.File; +import java.io.FileInputStream; +import java.util.Calendar; +import java.util.HashMap; +import java.util.Map; + +import javax.xml.transform.dom.DOMSource; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.w3c.dom.Document; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.DocumentPage; +import com.marklogic.client.document.DocumentRecord; +import com.marklogic.client.document.DocumentWriteSet; +import com.marklogic.client.document.DocumentDescriptor; +import com.marklogic.client.document.GenericDocumentManager; +import com.marklogic.client.document.JSONDocumentManager; +import com.marklogic.client.document.TextDocumentManager; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.io.BytesHandle; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.DocumentMetadataHandle; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.InputStreamHandle; +import com.marklogic.client.io.JacksonHandle; +import com.marklogic.client.io.SourceHandle; +import com.marklogic.client.io.StringHandle; +import com.marklogic.client.io.DocumentMetadataHandle.Capability; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentCollections; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentPermissions; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentProperties; +/** + * @author skottam + * This test is test the DocumentWriteSet function + * public DocumentWriteSet add(String uri, DocumentMetadataWriteHandle metadataHandle); + * This test intention is to test the system default , request wide, and document specific update to metadata will overwrite the existing metadata + * setup: create a usr1 with system level meta data as default + * client make a connection with usr1 to do bulk loading + * load set of documents where default metadata is set with raw xml or json documents + * test disableDefault(). + * + */ +public class TestBulkWriteMetadatawithRawXML extends BasicJavaClientREST{ + + private static String dbName = "TestBulkWriteDefaultMetadataDB3"; + private static String [] fNames = {"TestBulkWriteDefaultMetadataDB-3"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client ; + /** + * @throws java.lang.Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + System.out.println("In Setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + createRESTUser("app-user", "password","rest-writer","rest-reader" ); + createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); + } + + /** + * @throws java.lang.Exception + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + System.out.println("In tear down" ); + tearDownJavaRESTServer(dbName, fNames, restServerName); + deleteRESTUser("app-user"); + deleteRESTUser("usr1"); + } + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + // create new connection for each test below + client = DatabaseClientFactory.newClient("localhost", restPort, "usr1", "password", Authentication.DIGEST); + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + System.out.println("Running clear script"); + // release client + client.release(); + } + + public DocumentMetadataHandle setMetadata(){ + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); + metadataHandle.getCollections().addAll("my-collection1","my-collection2"); + metadataHandle.getPermissions().add("app-user", Capability.UPDATE, Capability.READ); + metadataHandle.getProperties().put("reviewed", true); + metadataHandle.getProperties().put("myString", "foo"); + metadataHandle.getProperties().put("myInteger", 10); + metadataHandle.getProperties().put("myDecimal", 34.56678); + metadataHandle.getProperties().put("myCalendar", Calendar.getInstance().get(Calendar.YEAR)); + metadataHandle.setQuality(23); + return metadataHandle; + } + public void validateMetadata(DocumentMetadataHandle mh){ + + // get metadata values + DocumentProperties properties = mh.getProperties(); + DocumentPermissions permissions = mh.getPermissions(); + DocumentCollections collections = mh.getCollections(); + + // Properties + // String expectedProperties = "size:5|reviewed:true|myInteger:10|myDecimal:34.56678|myCalendar:2014|myString:foo|"; + String actualProperties = getDocumentPropertiesString(properties); + boolean result = actualProperties.contains("size:5|"); + assertTrue("Document properties count", result); + + // Permissions + String actualPermissions = getDocumentPermissionsString(permissions); + System.out.println(actualPermissions); + + assertTrue("Document permissions difference in size value", actualPermissions.contains("size:4")); + assertTrue("Document permissions difference in flexrep-eval permission", actualPermissions.contains("flexrep-eval:[READ]")); + assertTrue("Document permissions difference in rest-reader permission", actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue("Document permissions difference in app-user permissions", + (actualPermissions.contains("app-user:[UPDATE, READ]") || actualPermissions.contains("app-user:[READ, UPDATE]"))); + + + // Collections + String actualCollections = getDocumentCollectionsString(collections); + System.out.println(collections); + + assertTrue("Document collections difference in size value", actualCollections.contains("size:2")); + assertTrue("my-collection1 not found", actualCollections.contains("my-collection1")); + assertTrue("my-collection2 not found", actualCollections.contains("my-collection2")); + + } + public void validateDefaultMetadata(DocumentMetadataHandle mh){ + + // get metadata values + + DocumentPermissions permissions = mh.getPermissions(); + DocumentCollections collections = mh.getCollections(); + + // Permissions + String actualPermissions = getDocumentPermissionsString(permissions); + System.out.println(actualPermissions); + + assertTrue("Document permissions difference in size value", actualPermissions.contains("size:3")); + assertTrue("Document permissions difference in flexrep-eval permission", actualPermissions.contains("flexrep-eval:[READ]")); + assertTrue("Document permissions difference in rest-reader permission", actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", actualPermissions.contains("rest-writer:[UPDATE]")); + + // Collections + String actualCollections = getDocumentCollectionsString(collections); + assertTrue("Document collections difference in size value", actualCollections.contains("size:1")); + assertTrue("my-collection1 not found", actualCollections.contains("http://permission-collections/")); + } + @Test + public void testWriteMultipleTextDocWithXMLMetadata() throws Exception + { + DocumentMetadataHandle mh2; + String docId[] = {"/foo/test/myFoo1.txt","/foo/test/myFoo2.txt","/foo/test/myFoo3.txt", + "/foo/test/myFoo4.txt","/foo/test/myFoo5.txt","/foo/test/myFoo6.txt", + "/foo/test/myFoo7.txt","/foo/test/myFoo8.txt","/foo/test/myFoo9.txt"}; + + TextDocumentManager docMgr = client.newTextDocumentManager(); + + DocumentWriteSet writeset =docMgr.newWriteSet(); + // get the original metadata + Document docMetadata = getXMLMetadata("metadata-original.xml"); + // create handle to write metadata + DOMHandle writeMetadataHandle = new DOMHandle(); + writeMetadataHandle.set(docMetadata); + + writeset.addDefault(writeMetadataHandle); + + writeset.add(docId[0], new StringHandle().with("This is so foo1")); + writeset.add(docId[1], new StringHandle().with("This is so foo2")); + writeset.add(docId[2], new StringHandle().with("This is so foo3")); + docMgr.write(writeset); + //Default properties for are document set + + DocumentPage page = docMgr.read(docId); + DOMHandle mh= new DOMHandle(); + while(page.hasNext()){ + DocumentRecord rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh); + Document docReadMetadata = mh.get(); + assertXpathEvaluatesTo("coll1", "string(//*[local-name()='collection'][1])", docReadMetadata); + assertXpathEvaluatesTo("coll2", "string(//*[local-name()='collection'][2])", docReadMetadata); + assertXpathEvaluatesTo("MarkLogic", "string(//*[local-name()='Author'])", docReadMetadata); + } + + + //Adding document specific properties in document set2 + + writeset =docMgr.newWriteSet(); + // put metadata + mh2 = setMetadata(); + + writeset.add(docId[3], new StringHandle().with("This is so foo4")); + writeset.add(docId[4],mh2, new StringHandle().with("This is so foo5")); + writeset.add(docId[5],writeMetadataHandle, new StringHandle().with("This is so foo6")); + docMgr.write(writeset); + + + page = docMgr.read(docId[3]); + DocumentRecord rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh2); + validateDefaultMetadata(mh2); + + page = docMgr.read(docId[4]); + mh2=new DocumentMetadataHandle(); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh2); + validateMetadata(mh2); + + + page = docMgr.read(docId[5]); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh); + Document docReadMetadata = mh.get(); + assertXpathEvaluatesTo("coll1", "string(//*[local-name()='collection'][1])", docReadMetadata); + assertXpathEvaluatesTo("coll2", "string(//*[local-name()='collection'][2])", docReadMetadata); + assertXpathEvaluatesTo("MarkLogic", "string(//*[local-name()='Author'])", docReadMetadata); + + } + @Test + public void testWriteMultipleXMLDocWithXMLMetadata() throws Exception + { + // put metadata + + String docId[] = {"/foo/test/Foo1.xml","/foo/test/Foo2.xml","/foo/test/Foo3.xml", + "/foo/test/Foo4.xml","/foo/test/Foo5.xml","/foo/test/Foo6.xml", + "/foo/test/Foo7.xml","/foo/test/Foo8.xml","/foo/test/Foo8.xml"}; + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + // get the original metadata + Document docMetadata = getXMLMetadata("metadata-updated.xml"); + // create handle to write metadata + DOMHandle writeMetadataHandle = new DOMHandle(); + writeMetadataHandle.set(docMetadata); + + writeset.addDefault(writeMetadataHandle); + + DocumentDescriptor docdisc = docMgr.newDescriptor("test1"); + + writeset.add(docdisc, new DOMHandle(getDocumentContent("This is so foo1"))); + writeset.add(docId[1], new DOMHandle().with(getDocumentContent("This is so foo2"))); + writeset.add(docId[2], new DOMHandle().with(getDocumentContent("This is so foo3"))); + + docMgr.write(writeset); + + DocumentPage page = docMgr.read(docId); + DocumentRecord rec; + Document docReadMetadata =null; + DOMHandle mh= new DOMHandle(); + while(page.hasNext()){ + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh); + docReadMetadata = mh.get(); + assertXpathEvaluatesTo("coll1", "string(//*[local-name()='collection'][1])", docReadMetadata); + assertXpathEvaluatesTo("coll3", "string(//*[local-name()='collection'][2])", docReadMetadata); + assertXpathEvaluatesTo("23", "string(//*[local-name()='quality'])", docReadMetadata); + assertXpathEvaluatesTo("Aries", "string(//*[local-name()='Author'])", docReadMetadata); + } + assertXpathEvaluatesTo("coll1", "string(//*[local-name()='collection'][1])", docReadMetadata); + + // put metadata + DocumentMetadataHandle mh2 = setMetadata(); + writeset =docMgr.newWriteSet(); + + writeset.add(docId[3], new DOMHandle(getDocumentContent("This is so foo4"))); + writeset.addDefault(mh2); + writeset.add(docId[4],writeMetadataHandle ,new DOMHandle().with(getDocumentContent("This is so foo5"))); + writeset.add(docId[5], new DOMHandle().with(getDocumentContent("This is so foo6"))); + docMgr.write(writeset); + + page = docMgr.read(docId[3]); + mh2=new DocumentMetadataHandle(); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh2); + validateDefaultMetadata(mh2); + + page = docMgr.read(docId[4]); + rec= page.next(); + docMgr.readMetadata(rec.getUri(), mh); + docReadMetadata = mh.get(); + System.out.println(rec.getUri()+ " "+ docReadMetadata+docId[4]); + assertXpathEvaluatesTo("coll1", "string(//*[local-name()='collection'][1])", docReadMetadata); + assertXpathEvaluatesTo("coll3", "string(//*[local-name()='collection'][2])", docReadMetadata); + assertXpathEvaluatesTo("23", "string(//*[local-name()='quality'])", docReadMetadata); + assertXpathEvaluatesTo("Aries", "string(//*[local-name()='Author'])", docReadMetadata); + + page = docMgr.read(docId[5]); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh2); + validateMetadata(mh2); + + } + + + /* + * This is test is made up for the github issue 41 + */ +@Test + public void testWriteMultipleJSONDocsWithRawJSONMetadata() throws Exception + { + // Synthesize input content + + + StringHandle doc1 = new StringHandle( + "{\"number\": 1}").withFormat(Format.JSON); + StringHandle doc2 = new StringHandle( + "{\"number\": 2}").withFormat(Format.JSON); + StringHandle doc3 = new StringHandle( + "{\"number\": 3}").withFormat(Format.JSON); + StringHandle doc4 = new StringHandle( + "{\"number\": 4}").withFormat(Format.JSON); + StringHandle doc5 = new StringHandle( + "{\"number\": 5}").withFormat(Format.JSON); + + // Synthesize input metadata + + ObjectMapper mapper = new ObjectMapper(); + JacksonHandle defaultMetadata1 = new JacksonHandle(); + Map 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/")); + + // Create and build up the batch + JSONDocumentManager jdm = client.newJSONDocumentManager(); + DocumentWriteSet batch = jdm.newWriteSet(); + + // use system default metadata + batch.add("doc1.json", doc1); // system default metadata + // using batch default metadata + batch.addDefault(defaultMetadata1); + batch.add("doc2.json", doc2); // batch default metadata + batch.add("doc3.json", docSpecificMetadata, doc3); + batch.add("doc4.json", doc4); // batch default metadata + + // replace batch default metadata with new metadata + batch.addDefault(defaultMetadata2); + batch.add("doc5.json", doc5); // batch default + + // Execute the write operation + jdm.write(batch); + DocumentPage page ; + DocumentRecord rec; + // Check the results + // Doc1 should have the system default quality of 0 + page = jdm.read("doc1.json"); + DocumentMetadataHandle mh = new DocumentMetadataHandle(); + rec = page.next(); + jdm.readMetadata(rec.getUri(), mh); + System.out.print(mh.getQuality()); + validateDefaultMetadata(mh); + assertEquals("default quality",0,mh.getQuality()); + + // Doc2 should use the first batch default metadata, that has only properties i.e. rest needs to be default to system defaults + page = jdm.read("doc2.json"); + rec = page.next(); + jdm.readMetadata(rec.getUri(), mh); + System.out.print(mh.getCollections().isEmpty()); + assertEquals("default quality",0,mh.getQuality()); + assertTrue("default collections reset",mh.getCollections().isEmpty()); + + // Doc3 should have the system default document quality (0) because quality + // was not included in the document-specific metadata. It should be in the + // collection "mySpecificCollection", from the document-specific metadata. + + page = jdm.read("doc3.json"); + rec = page.next(); + jdm.readMetadata(rec.getUri(), mh); + assertEquals("default quality",0,mh.getQuality()); + assertEquals("default collection must change","[http://Json-Uri-spec-collections/]",mh.getCollections().toString()); + + // Doc 4 should also use the 1st batch default metadata, with quality 1 + page = jdm.read("doc4.json"); + rec = page.next(); + jdm.readMetadata(rec.getUri(), mh); + assertEquals("default quality",0,mh.getQuality()); + assertTrue("default collections reset",mh.getProperties().containsValue("9")); + // Doc5 should use the 2nd batch default metadata, with quality 2 + page = jdm.read("doc5.json"); + rec = page.next(); + jdm.readMetadata(rec.getUri(), mh); + assertEquals("default quality",20,mh.getQuality()); + + } +/* + * Git Issue #24 is tested here + * + */ + @Test + public void testWriteGenericDocMgrWithDefaultMetadata() throws Exception + { + String docId[] = {"Sega-4MB.jpg"}; + + GenericDocumentManager docMgr = client.newDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + // put metadata + ObjectMapper mapper = new ObjectMapper(); + JacksonHandle mh = new JacksonHandle(); + Map p = new HashMap(); + p.put("myString", "Generic JSON"); + p.put("myInt", "19"); + mh.with(constructJSONPropertiesMetadata(p)); + + writeset.addDefault(mh); // Adding default metadata to the entire batch + File file1= null; + file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); + FileInputStream fis = new FileInputStream(file1); + InputStreamHandle handle1 = new InputStreamHandle(fis); + handle1.setFormat(Format.BINARY); + + writeset.add("/generic/Sega.jpg",handle1); // This document implicitly gets the default metadata that we added in the begining + + 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 document suppose to get the in scope metadata quality and should set collections to system default + writeset.disableDefault(); + String foo1 = "This is foo1 of byte Array"; + byte[] ba = foo1.getBytes(); + BytesHandle bh = new BytesHandle(ba); + bh.setFormat(Format.TEXT); + + writeset.add("/generic/foo1.txt",bh); + + DOMSource ds = new DOMSource(getDocumentContent("This is so foo1")); + SourceHandle sh = new SourceHandle(); + sh.set(ds); + sh.setFormat(Format.XML); + writeset.add("/generic/foo.xml", new JacksonHandle().with(constructJSONCollectionMetadata("http://Json-Uri-generic-collections/")), sh); //This document should over write the system default and default collection list and get document specific collection + + docMgr.write(writeset); + DocumentMetadataHandle mh1 = new DocumentMetadataHandle(); + DocumentPage page = docMgr.read("/generic/Sega.jpg"); + DocumentRecord rec = page.next(); + docMgr.readMetadata(rec.getUri(),mh1); + assertEquals("default quality",0,mh1.getQuality()); + assertTrue("Properties contains value 19",mh1.getProperties().containsValue("19")); + + page = docMgr.read("/generic/dog.json"); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh1); + assertEquals("default quality",10,mh1.getQuality()); + assertTrue("default collections missing",mh1.getCollections().isEmpty()); + + page = docMgr.read("/generic/foo1.txt"); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh1); + // until issue 24 is fixed + //this.validateDefaultMetadata(mh1); + assertEquals("default quality", 0, mh1.getQuality()); + + String actualPermissions = mh1.getPermissions().toString(); + System.out.println(actualPermissions); + assertTrue("Default permissions must contain flexrep-eval=[READ]", actualPermissions.contains("flexrep-eval=[READ]")); + assertTrue("Default permissions must contain rest-reader=[READ]", actualPermissions.contains("rest-reader=[READ]")); + assertTrue("Default permissions must contain rest-writer=[UPDATE]", actualPermissions.contains("rest-writer=[UPDATE]")); + + page = docMgr.read("/generic/foo.xml"); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh1); + assertEquals("default quality",0,mh1.getQuality()); + assertEquals("default collection must change","[http://Json-Uri-generic-collections/]",mh1.getCollections().toString()); + + } + +} From 75c9cad384a7c738c60f3f1441de0e72a382fb13 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Wed, 20 Aug 2014 18:04:05 -0700 Subject: [PATCH 072/357] Code cleanup --- .../javaclient/TestBulkWriteSample1.java | 692 +++++++++--------- 1 file changed, 343 insertions(+), 349 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteSample1.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteSample1.java index f284afd52..9e07f1347 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteSample1.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteSample1.java @@ -1,350 +1,344 @@ -package com.marklogic.javaclient; - -import java.io.File; -import java.io.FileInputStream; -import java.io.Reader; -import java.io.FileReader; -import java.io.BufferedReader; -import java.io.StringReader; - -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.namespace.QName; - -import org.custommonkey.xmlunit.exceptions.XpathException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.document.TextDocumentManager; -import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.document.BinaryDocumentManager; -import com.marklogic.client.document.JSONDocumentManager; -import com.marklogic.client.document.GenericDocumentManager; -import com.marklogic.client.document.DocumentManager; -import com.marklogic.client.io.FileHandle; -import com.marklogic.client.io.Format; -import com.marklogic.client.io.JAXBHandle; -import com.marklogic.client.io.StringHandle; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.ReaderHandle; -import com.marklogic.client.io.InputStreamHandle; -import com.marklogic.client.io.BytesHandle; -import com.marklogic.client.io.SourceHandle; -import com.marklogic.client.io.JacksonHandle; -import com.marklogic.client.document.DocumentWriteSet; -import com.marklogic.client.FailedRequestException; - - - - - -import javax.xml.transform.dom.DOMSource; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; - -import org.junit.*; - -import static org.junit.Assert.*; - -/* - * This test is designed to to test simple bulk writes with different types of Managers and different content type like JSON,text,binary,XMl - * - * TextDocumentManager - * XMLDocumentManager - * BinaryDocumentManager - * JSONDocumentManager - * GenericDocumentManager - */ - - - -public class TestBulkWriteSample1 extends BasicJavaClientREST { - - - private static String dbName = "TestBulkWriteSampleDB"; - private static String [] fNames = {"TestBulkWriteSampleDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort = 8011; - private DatabaseClient client ; - @BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); - - //To enable client side http logging - -// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); -// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "debug"); -// setupAppServicesConstraint(dbName); - - } - - @Before public void testSetup() throws Exception - { - // create new connection for each test below - client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST); - } - @After - public void testCleanUp() throws Exception - { - System.out.println("Running clear script"); - // release client - client.release(); - - - } - /* - * This is cloned in github with tracking bug #27685 - * https://github.com/marklogic/java-client-api/issues/23 - * - * This test uses StringHandle to load 3 text documents, writes to database using bulk write set. - * Verified by reading individual documents - */ - @Test - public void testWriteMultipleTextDoc() - { - - String docId[] = {"/foo/test/myFoo1.txt","/foo/test/myFoo2.txt","/foo/test/myFoo3.txt"}; - - TextDocumentManager docMgr = client.newTextDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - - writeset.add(docId[0], new StringHandle().with("This is so foo1")); - writeset.add(docId[1], new StringHandle().with("This is so foo2")); - writeset.add(docId[2], new StringHandle().with("This is so foo3")); - - docMgr.write(writeset); - - assertEquals("Text document write difference", "This is so foo1", docMgr.read(docId[0], new StringHandle()).get()); - assertEquals("Text document write difference", "This is so foo2", docMgr.read(docId[1], new StringHandle()).get()); - assertEquals("Text document write difference", "This is so foo3", docMgr.read(docId[2], new StringHandle()).get()); - } - /* - * This test uses DOMHandle to load 3 xml documents, writes to database using bulk write set. - * Verified by reading individual documents - */ - @Test - public void testWriteMultipleXMLDoc() throws Exception - { - - - String docId[] = {"/foo/test/Foo1.xml","/foo/test/Foo2.xml","/foo/test/Foo3.xml"}; - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - - writeset.add(docId[0], new DOMHandle(getDocumentContent("This is so foo1"))); - writeset.add(docId[1], new DOMHandle().with(getDocumentContent("This is so foo2"))); - writeset.add(docId[2], new DOMHandle().with(getDocumentContent("This is so foo3"))); - - docMgr.write(writeset); - - DOMHandle dh = new DOMHandle(); - docMgr.read(docId[0], dh); - - - assertEquals("xml document write difference", "This is so foo1",dh.get().getChildNodes().item(0).getTextContent()); - docMgr.read(docId[1], dh); - assertEquals("xml document write difference", "This is so foo2", dh.get().getChildNodes().item(0).getTextContent()); - docMgr.read(docId[2], dh); - assertEquals("xml document write difference", "This is so foo3", dh.get().getChildNodes().item(0).getTextContent()); - } - /* - * This test uses FileHandle to load 3 binary documents with same URI, writes to database using bulk write set. - * Expecting an exception. - */ - @Test (expected = FailedRequestException.class) - public void testWriteMultipleSameBinaryDoc() throws Exception - { - String docId[] = {"Pandakarlino.jpg","mlfavicon.png"}; - - BinaryDocumentManager docMgr = client.newBinaryDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - File file1= null; - file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); - FileHandle handle1 = new FileHandle(file1); - writeset.add("/1/"+docId[0],handle1.withFormat(Format.BINARY)); - writeset.add("/1/"+docId[0],handle1.withFormat(Format.BINARY)); - - docMgr.write(writeset); - - } - /* - * This test uses FileHandle to load 3 binary documents, writes to database using bulk write set. - * Verified by reading individual documents - */ - @Test - public void testWriteMultipleBinaryDoc() throws Exception - { - String docId[] = {"Pandakarlino.jpg","mlfavicon.png"}; - - BinaryDocumentManager docMgr = client.newBinaryDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - File file1= null,file2=null; - file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); - FileHandle handle1 = new FileHandle(file1); - writeset.add("/1/"+docId[0],handle1.withFormat(Format.BINARY)); - writeset.add("/2/"+docId[0],handle1.withFormat(Format.BINARY)); - file2 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[1]); - FileHandle handle2 = new FileHandle(file2); - writeset.add("/1/"+docId[1],handle2.withFormat(Format.BINARY)); - writeset.add("/2/"+docId[1],handle2.withFormat(Format.BINARY)); - - docMgr.write(writeset); - long fsize1 = file1.length(),fsize2 = file2.length(); - - - FileHandle readHandle1 = new FileHandle(); - docMgr.read("/1/"+docId[0],readHandle1); - - FileHandle readHandle2 = new FileHandle(); - docMgr.read("/1/"+docId[1],readHandle2); - System.out.println(file1.getName()+":"+fsize1+" "+readHandle1.get().getName()+":"+readHandle1.get().length()); - System.out.println(file2.getName()+":"+fsize2+" "+readHandle2.get().getName()+":"+readHandle2.get().length()); - assertEquals("Size of the File 1"+docId[0],fsize1,readHandle1.get().length()); - assertEquals("Size of the File 1"+docId[1],fsize2,readHandle2.get().length()); - } - -/* - * This test uses ReaderHandle to load 3 JSON documents, writes to database using bulk write set. - * Verified by reading individual documents - - */ - @Test - public void testWriteMultipleJSONDocs() throws Exception - { - String docId[] = {"/a.json","/b.json","/c.json"}; - String json1 = new String("{\"animal\":\"dog\", \"says\":\"woof\"}"); - String json2 = new String("{\"animal\":\"cat\", \"says\":\"meow\"}"); - String json3 = new String("{\"animal\":\"rat\", \"says\":\"keek\"}"); - Reader strReader = new StringReader(json1); - JSONDocumentManager docMgr = client.newJSONDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - writeset.add(docId[0],new ReaderHandle(strReader).withFormat(Format.JSON)); - writeset.add(docId[1],new ReaderHandle(new StringReader(json2))); - writeset.add(docId[2],new ReaderHandle(new StringReader(json3))); - - docMgr.write(writeset); - - ReaderHandle r1 = new ReaderHandle(); - docMgr.read(docId[0],r1); - BufferedReader bfr = new BufferedReader(r1.get()); - assertEquals(json1,bfr.readLine()); - docMgr.read(docId[1],r1); - assertEquals("Json File Content"+docId[1],json2,new BufferedReader(r1.get()).readLine()); - docMgr.read(docId[2],r1); - assertEquals("Json File Content"+docId[2],json3,new BufferedReader(r1.get()).readLine()); - bfr.close(); - } - @Test - public void testWriteMultipleJAXBDocs() throws Exception - { - String docId[] ={"/jaxb/iphone.xml","/jaxb/ipad.xml","/jaxb/ipod.xml"}; - Product product1 = new Product(); - product1.setName("iPhone"); - product1.setIndustry("Hardware"); - product1.setDescription("Very cool Iphone"); - Product product2 = new Product(); - product2.setName("iPad"); - product2.setIndustry("Hardware"); - product2.setDescription("Very cool Ipad"); - Product product3 = new Product(); - product3.setName("iPod"); - product3.setIndustry("Hardware"); - product3.setDescription("Very cool Ipod"); - JAXBContext context = JAXBContext.newInstance(Product.class); - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); -// JAXBHandle contentHandle = new JAXBHandle(context); -// contentHandle.set(product1); - writeset.add(docId[0],new JAXBHandle(context).with(product1)); - writeset.add(docId[1],new JAXBHandle(context).with(product2)); - writeset.add(docId[2],new JAXBHandle(context).with(product3)); - - docMgr.write(writeset); - - DOMHandle dh = new DOMHandle(); - docMgr.read(docId[0], dh); - - assertEquals("xml document write difference", "Very cool Iphone",dh.get().getChildNodes().item(0).getChildNodes().item(1).getTextContent()); - docMgr.read(docId[1], dh); - assertEquals("xml document write difference", "Very cool Ipad", dh.get().getChildNodes().item(0).getChildNodes().item(1).getTextContent()); - docMgr.read(docId[2], dh); - assertEquals("xml document write difference", "Very cool Ipod", dh.get().getChildNodes().item(0).getChildNodes().item(1).getTextContent()); - - } -/* - * This test uses GenericManager to load all different document types - * This test has a bug logged in github with tracking Issue#33 - * - */ - @Test - public void testWriteGenericDocMgr() throws Exception - { - String docId[] = {"Pandakarlino.jpg","mlfavicon.png"}; - - GenericDocumentManager docMgr = client.newDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - - File file1= null; - file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); - FileInputStream fis = new FileInputStream(file1); - InputStreamHandle handle1 = new InputStreamHandle(fis); - handle1.setFormat(Format.BINARY); - - writeset.add("/generic/"+docId[0],handle1); - - 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",jh); - - String foo1 = "This is foo1 of byte Array"; - byte[] ba = foo1.getBytes(); - BytesHandle bh = new BytesHandle(ba); - bh.setFormat(Format.TEXT); - - writeset.add("/generic/foo1.txt",bh); - - DOMSource ds = new DOMSource(getDocumentContent("This is so foo1")); - SourceHandle sh = new SourceHandle(); - sh.set(ds); - sh.setFormat(Format.XML); - - writeset.add("/generic/foo.xml",sh); - - docMgr.write(writeset); - - FileHandle rh = new FileHandle(); - - docMgr.read("/generic/"+docId[0],rh); - assertEquals("Size of the File /generic/"+docId[0],file1.length(),rh.get().length()); - System.out.println(rh.get().getName()+":"+rh.get().length()+"\n"); - - docMgr.read("/generic/foo.xml",rh); - BufferedReader br=new BufferedReader(new FileReader(rh.get())); - br.readLine(); - assertEquals("xml document write difference", "This is so foo1", br.readLine() ); - docMgr.read("/generic/foo1.txt",rh); - br.close(); - br=new BufferedReader(new FileReader(rh.get())); - assertEquals("txt document write difference", foo1, br.readLine() ); - br.close(); - docMgr.read("/generic/dog.json",rh); - br=new BufferedReader(new FileReader(rh.get())); - assertEquals("Json document write difference", "{\"animal\":\"dog\", \"says\":\"woof\"}", br.readLine() ); - br.close(); - fis.close(); - } - @AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down" ); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } +package com.marklogic.javaclient; + +import java.io.File; +import java.io.FileInputStream; +import java.io.Reader; +import java.io.FileReader; +import java.io.BufferedReader; +import java.io.StringReader; + +import javax.xml.bind.JAXBContext; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.TextDocumentManager; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.document.BinaryDocumentManager; +import com.marklogic.client.document.JSONDocumentManager; +import com.marklogic.client.document.GenericDocumentManager; +import com.marklogic.client.io.FileHandle; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.JAXBHandle; +import com.marklogic.client.io.StringHandle; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.ReaderHandle; +import com.marklogic.client.io.InputStreamHandle; +import com.marklogic.client.io.BytesHandle; +import com.marklogic.client.io.SourceHandle; +import com.marklogic.client.io.JacksonHandle; +import com.marklogic.client.document.DocumentWriteSet; +import com.marklogic.client.FailedRequestException; + + + + + +import javax.xml.transform.dom.DOMSource; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +import org.junit.*; + +import static org.junit.Assert.*; + +/* + * This test is designed to to test simple bulk writes with different types of Managers and different content type like JSON,text,binary,XMl + * + * TextDocumentManager + * XMLDocumentManager + * BinaryDocumentManager + * JSONDocumentManager + * GenericDocumentManager + */ + + + +public class TestBulkWriteSample1 extends BasicJavaClientREST { + + + private static String dbName = "TestBulkWriteSampleDB"; + private static String [] fNames = {"TestBulkWriteSampleDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client ; + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + + //To enable client side http logging + +// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); +// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "debug"); +// setupAppServicesConstraint(dbName); + + } + + @Before public void testSetup() throws Exception + { + // create new connection for each test below + client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST); + } + @After + public void testCleanUp() throws Exception + { + System.out.println("Running clear script"); + // release client + client.release(); + + + } + /* + * This is cloned in github with tracking bug #27685 + * https://github.com/marklogic/java-client-api/issues/23 + * + * This test uses StringHandle to load 3 text documents, writes to database using bulk write set. + * Verified by reading individual documents + */ + @Test + public void testWriteMultipleTextDoc() + { + + String docId[] = {"/foo/test/myFoo1.txt","/foo/test/myFoo2.txt","/foo/test/myFoo3.txt"}; + + TextDocumentManager docMgr = client.newTextDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + + writeset.add(docId[0], new StringHandle().with("This is so foo1")); + writeset.add(docId[1], new StringHandle().with("This is so foo2")); + writeset.add(docId[2], new StringHandle().with("This is so foo3")); + + docMgr.write(writeset); + + assertEquals("Text document write difference", "This is so foo1", docMgr.read(docId[0], new StringHandle()).get()); + assertEquals("Text document write difference", "This is so foo2", docMgr.read(docId[1], new StringHandle()).get()); + assertEquals("Text document write difference", "This is so foo3", docMgr.read(docId[2], new StringHandle()).get()); + } + /* + * This test uses DOMHandle to load 3 xml documents, writes to database using bulk write set. + * Verified by reading individual documents + */ + @Test + public void testWriteMultipleXMLDoc() throws Exception + { + + + String docId[] = {"/foo/test/Foo1.xml","/foo/test/Foo2.xml","/foo/test/Foo3.xml"}; + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + + writeset.add(docId[0], new DOMHandle(getDocumentContent("This is so foo1"))); + writeset.add(docId[1], new DOMHandle().with(getDocumentContent("This is so foo2"))); + writeset.add(docId[2], new DOMHandle().with(getDocumentContent("This is so foo3"))); + + docMgr.write(writeset); + + DOMHandle dh = new DOMHandle(); + docMgr.read(docId[0], dh); + + + assertEquals("xml document write difference", "This is so foo1",dh.get().getChildNodes().item(0).getTextContent()); + docMgr.read(docId[1], dh); + assertEquals("xml document write difference", "This is so foo2", dh.get().getChildNodes().item(0).getTextContent()); + docMgr.read(docId[2], dh); + assertEquals("xml document write difference", "This is so foo3", dh.get().getChildNodes().item(0).getTextContent()); + } + /* + * This test uses FileHandle to load 3 binary documents with same URI, writes to database using bulk write set. + * Expecting an exception. + */ + @Test (expected = FailedRequestException.class) + public void testWriteMultipleSameBinaryDoc() throws Exception + { + String docId[] = {"Pandakarlino.jpg","mlfavicon.png"}; + + BinaryDocumentManager docMgr = client.newBinaryDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + File file1= null; + file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); + FileHandle handle1 = new FileHandle(file1); + writeset.add("/1/"+docId[0],handle1.withFormat(Format.BINARY)); + writeset.add("/1/"+docId[0],handle1.withFormat(Format.BINARY)); + + docMgr.write(writeset); + + } + /* + * This test uses FileHandle to load 3 binary documents, writes to database using bulk write set. + * Verified by reading individual documents + */ + @Test + public void testWriteMultipleBinaryDoc() throws Exception + { + String docId[] = {"Pandakarlino.jpg","mlfavicon.png"}; + + BinaryDocumentManager docMgr = client.newBinaryDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + File file1= null,file2=null; + file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); + FileHandle handle1 = new FileHandle(file1); + writeset.add("/1/"+docId[0],handle1.withFormat(Format.BINARY)); + writeset.add("/2/"+docId[0],handle1.withFormat(Format.BINARY)); + file2 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[1]); + FileHandle handle2 = new FileHandle(file2); + writeset.add("/1/"+docId[1],handle2.withFormat(Format.BINARY)); + writeset.add("/2/"+docId[1],handle2.withFormat(Format.BINARY)); + + docMgr.write(writeset); + long fsize1 = file1.length(),fsize2 = file2.length(); + + + FileHandle readHandle1 = new FileHandle(); + docMgr.read("/1/"+docId[0],readHandle1); + + FileHandle readHandle2 = new FileHandle(); + docMgr.read("/1/"+docId[1],readHandle2); + System.out.println(file1.getName()+":"+fsize1+" "+readHandle1.get().getName()+":"+readHandle1.get().length()); + System.out.println(file2.getName()+":"+fsize2+" "+readHandle2.get().getName()+":"+readHandle2.get().length()); + assertEquals("Size of the File 1"+docId[0],fsize1,readHandle1.get().length()); + assertEquals("Size of the File 1"+docId[1],fsize2,readHandle2.get().length()); + } + +/* + * This test uses ReaderHandle to load 3 JSON documents, writes to database using bulk write set. + * Verified by reading individual documents + + */ + @Test + public void testWriteMultipleJSONDocs() throws Exception + { + String docId[] = {"/a.json","/b.json","/c.json"}; + String json1 = new String("{\"animal\":\"dog\", \"says\":\"woof\"}"); + String json2 = new String("{\"animal\":\"cat\", \"says\":\"meow\"}"); + String json3 = new String("{\"animal\":\"rat\", \"says\":\"keek\"}"); + Reader strReader = new StringReader(json1); + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + writeset.add(docId[0],new ReaderHandle(strReader).withFormat(Format.JSON)); + writeset.add(docId[1],new ReaderHandle(new StringReader(json2))); + writeset.add(docId[2],new ReaderHandle(new StringReader(json3))); + + docMgr.write(writeset); + + ReaderHandle r1 = new ReaderHandle(); + docMgr.read(docId[0],r1); + BufferedReader bfr = new BufferedReader(r1.get()); + assertEquals(json1,bfr.readLine()); + docMgr.read(docId[1],r1); + assertEquals("Json File Content"+docId[1],json2,new BufferedReader(r1.get()).readLine()); + docMgr.read(docId[2],r1); + assertEquals("Json File Content"+docId[2],json3,new BufferedReader(r1.get()).readLine()); + bfr.close(); + } + @Test + public void testWriteMultipleJAXBDocs() throws Exception + { + String docId[] ={"/jaxb/iphone.xml","/jaxb/ipad.xml","/jaxb/ipod.xml"}; + Product product1 = new Product(); + product1.setName("iPhone"); + product1.setIndustry("Hardware"); + product1.setDescription("Very cool Iphone"); + Product product2 = new Product(); + product2.setName("iPad"); + product2.setIndustry("Hardware"); + product2.setDescription("Very cool Ipad"); + Product product3 = new Product(); + product3.setName("iPod"); + product3.setIndustry("Hardware"); + product3.setDescription("Very cool Ipod"); + JAXBContext context = JAXBContext.newInstance(Product.class); + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); +// JAXBHandle contentHandle = new JAXBHandle(context); +// contentHandle.set(product1); + writeset.add(docId[0],new JAXBHandle(context).with(product1)); + writeset.add(docId[1],new JAXBHandle(context).with(product2)); + writeset.add(docId[2],new JAXBHandle(context).with(product3)); + + docMgr.write(writeset); + + DOMHandle dh = new DOMHandle(); + docMgr.read(docId[0], dh); + + assertEquals("xml document write difference", "Very cool Iphone",dh.get().getChildNodes().item(0).getChildNodes().item(1).getTextContent()); + docMgr.read(docId[1], dh); + assertEquals("xml document write difference", "Very cool Ipad", dh.get().getChildNodes().item(0).getChildNodes().item(1).getTextContent()); + docMgr.read(docId[2], dh); + assertEquals("xml document write difference", "Very cool Ipod", dh.get().getChildNodes().item(0).getChildNodes().item(1).getTextContent()); + + } +/* + * This test uses GenericManager to load all different document types + * This test has a bug logged in github with tracking Issue#33 + * + */ + @Test + public void testWriteGenericDocMgr() throws Exception + { + String docId[] = {"Pandakarlino.jpg","mlfavicon.png"}; + + GenericDocumentManager docMgr = client.newDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + + File file1= null; + file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); + FileInputStream fis = new FileInputStream(file1); + InputStreamHandle handle1 = new InputStreamHandle(fis); + handle1.setFormat(Format.BINARY); + + writeset.add("/generic/"+docId[0],handle1); + + 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",jh); + + String foo1 = "This is foo1 of byte Array"; + byte[] ba = foo1.getBytes(); + BytesHandle bh = new BytesHandle(ba); + bh.setFormat(Format.TEXT); + + writeset.add("/generic/foo1.txt",bh); + + DOMSource ds = new DOMSource(getDocumentContent("This is so foo1")); + SourceHandle sh = new SourceHandle(); + sh.set(ds); + sh.setFormat(Format.XML); + + writeset.add("/generic/foo.xml",sh); + + docMgr.write(writeset); + + FileHandle rh = new FileHandle(); + + docMgr.read("/generic/"+docId[0],rh); + assertEquals("Size of the File /generic/"+docId[0],file1.length(),rh.get().length()); + System.out.println(rh.get().getName()+":"+rh.get().length()+"\n"); + + docMgr.read("/generic/foo.xml",rh); + BufferedReader br=new BufferedReader(new FileReader(rh.get())); + br.readLine(); + assertEquals("xml document write difference", "This is so foo1", br.readLine() ); + docMgr.read("/generic/foo1.txt",rh); + br.close(); + br=new BufferedReader(new FileReader(rh.get())); + assertEquals("txt document write difference", foo1, br.readLine() ); + br.close(); + docMgr.read("/generic/dog.json",rh); + br=new BufferedReader(new FileReader(rh.get())); + assertEquals("Json document write difference", "{\"animal\":\"dog\", \"says\":\"woof\"}", br.readLine() ); + br.close(); + fis.close(); + } + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down" ); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } } \ No newline at end of file From 98c873329fd42c1c178c4cea7e171211a9687251 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Wed, 20 Aug 2014 18:04:18 -0700 Subject: [PATCH 073/357] Code cleanup --- .../TestBulkSearchWithStringQueryDef.java | 722 +++++++------- .../TestBulkSearchWithStrucQueryDef.java | 889 +++++++++--------- 2 files changed, 801 insertions(+), 810 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java index 2b0fed570..529d6c927 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java @@ -1,362 +1,360 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.IOException; -import java.util.HashMap; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.xml.sax.SAXException; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.Transaction; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.document.DocumentManager.Metadata; -import com.marklogic.client.document.DocumentPage; -import com.marklogic.client.document.DocumentRecord; -import com.marklogic.client.document.DocumentWriteSet; -import com.marklogic.client.document.JSONDocumentManager; -import com.marklogic.client.document.TextDocumentManager; -import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.DocumentMetadataHandle; -import com.marklogic.client.io.Format; -import com.marklogic.client.io.JacksonHandle; -import com.marklogic.client.io.SearchHandle; -import com.marklogic.client.io.StringHandle; -import com.marklogic.client.io.DocumentMetadataHandle.Capability; -import com.marklogic.client.query.MatchDocumentSummary; -import com.marklogic.client.query.MatchLocation; -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.query.QueryManager.QueryView; -import com.marklogic.client.query.StringQueryDefinition; - -public class TestBulkSearchWithStringQueryDef extends BasicJavaClientREST{ - private static final int BATCH_SIZE=100; - private static final String DIRECTORY ="/bulkSearch/"; - private static String dbName = "TestBulkSearchSQDDB"; - private static String [] fNames = {"TestBulkSearchSQDDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort = 8011; - private DatabaseClient client ; - - @BeforeClass - public static void setUpBeforeClass() throws Exception { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); - setupAppServicesConstraint(dbName); - createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); - } - - @AfterClass - public static void tearDownAfterClass() throws Exception { - System.out.println("In tear down" ); - tearDownJavaRESTServer(dbName, fNames, restServerName); - deleteRESTUser("usr1"); - } - - @Before - public void setUp() throws Exception { - // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); - // create new connection for each test below - client = DatabaseClientFactory.newClient("localhost", restPort, "usr1", "password", Authentication.DIGEST); - } - - @After - public void tearDown() throws Exception { - System.out.println("Running clear script"); - // release client - client.release(); - } - public void loadJSONDocuments() throws JsonProcessingException, IOException{ - int count=1; - JSONDocumentManager docMgr = client.newJSONDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - - HashMap 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); - } - } - public void validateRecord(DocumentRecord record,Format type) { - - assertNotNull("DocumentRecord should never be null", record); - assertNotNull("Document uri should never be null", record.getUri()); - assertTrue("Document uri should start with " + DIRECTORY, record.getUri().startsWith(DIRECTORY)); - assertEquals("All records are expected to be in same format", type, record.getFormat()); - // System.out.println(record.getMimetype()); - - } - public void loadTxtDocuments(){ - int count =1; - TextDocumentManager docMgr = client.newTextDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - for(int i =0;i<101;i++){ - writeset.add(DIRECTORY+"Textfoo"+i+".txt", new StringHandle().with("bar can be foo"+i)); - if(count%BATCH_SIZE == 0){ - docMgr.write(writeset); - writeset = docMgr.newWriteSet(); - } - count++; - } - if(count%BATCH_SIZE > 0){ - docMgr.write(writeset); - } - } - public void loadXMLDocuments() throws IOException, ParserConfigurationException, SAXException, TransformerException{ - int count=1; - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - for(int i =0;i<102;i++){ - - writeset.add(DIRECTORY+"foo"+i+".xml", new DOMHandle(getDocumentContent("This is so foo with a bar "+i))); - - if(count%BATCH_SIZE == 0){ - docMgr.write(writeset); - writeset = docMgr.newWriteSet(); - } - count++; - } - if(count%BATCH_SIZE > 0){ - docMgr.write(writeset); - } - } - @Test - public void testBulkSearchSQDwithDifferentPageSizes() { - int count; - loadTxtDocuments(); - //Creating a txt document manager for bulk search - TextDocumentManager docMgr = client.newTextDocumentManager(); - //using QueryManger for query definition and set the search criteria - QueryManager queryMgr = client.newQueryManager(); - StringQueryDefinition qd = queryMgr.newStringDefinition(); - qd.setCriteria("bar"); - // set document manager level settings for search response - System.out.println("Default Page length setting on docMgr :"+docMgr.getPageLength()); - docMgr.setPageLength(1); - docMgr.setSearchView(QueryView.RESULTS); - docMgr.setResponseFormat(Format.XML); - assertEquals("format set on document manager","XML",docMgr.getResponseFormat().toString()); - assertEquals("Queryview set on document manager ","RESULTS" ,docMgr.getSearchView().toString()); - assertEquals("Page length ",1,docMgr.getPageLength()); - // Search for documents where content has bar and get first result record, get search handle on it - SearchHandle sh = new SearchHandle(); - DocumentPage page= docMgr.search(qd, 0); - // test for page methods - assertEquals("Number of records",1,page.size()); - assertEquals("Starting record in first page ",1,page.getStart()); - assertEquals("Total number of estimated results:",101,page.getTotalSize()); - assertEquals("Total number of estimated pages :",101,page.getTotalPages()); - // till the issue #78 get fixed - assertFalse("Is this First page :",page.isFirstPage());//this is bug - assertFalse("Is this Last page :",page.isLastPage()); - assertTrue("Is this First page has content:",page.hasContent()); - // Need the Issue #75 to be fixed - assertTrue("Is first page has previous page ?",page.hasPreviousPage()); - // - long pageNo=1; - do{ - count=0; - page = docMgr.search(qd, pageNo,sh); - if(pageNo >1){ - assertFalse("Is this first Page", page.isFirstPage()); - assertTrue("Is page has previous page ?",page.hasPreviousPage()); - } - while(page.hasNext()){ - DocumentRecord rec = page.next(); - rec.getFormat(); - validateRecord(rec,Format.TEXT); - // System.out.println(rec.getUri()); - count++; - } - MatchDocumentSummary[] mds= sh.getMatchResults(); - assertEquals("Matched document count",1,mds.length); - //since we set the query view to get only results, facet count supposed be 0 - assertEquals("Matched Facet count",0,sh.getFacetNames().length); - - assertEquals("document count", page.size(),count); - // assertEquals("Page Number #",pageNo,page.getPageNumber()); - pageNo = pageNo + page.getPageSize(); - }while(!page.isLastPage()); - assertTrue("page count is 101 ",pageNo == page.getTotalPages()); - assertTrue("Page has previous page ?",page.hasPreviousPage()); - assertEquals("page size", 1,page.getPageSize()); - assertEquals("document count", 101,page.getTotalSize()); - page= docMgr.search(qd, 102); - assertFalse("Page has any records ?",page.hasContent()); - } - //This test is trying to set the setResponse to JSON on DocumentManager and use search handle which only work with XML - @Test(expected = UnsupportedOperationException.class) - public void testBulkSearchSQDwithWrongResponseFormat() throws Exception { - loadTxtDocuments(); - TextDocumentManager docMgr = client.newTextDocumentManager(); - QueryManager queryMgr = client.newQueryManager(); - StringQueryDefinition qd = queryMgr.newStringDefinition(); - qd.setCriteria("bar"); - docMgr.setResponseFormat(Format.JSON); - SearchHandle results = new SearchHandle(); - DocumentPage page= docMgr.search(qd, 1,results); - MatchDocumentSummary[] summaries = results.getMatchResults(); - for (MatchDocumentSummary summary : summaries ) { - MatchLocation[] locations = summary.getMatchLocations(); - for (MatchLocation location : locations) { - System.out.println(location.getAllSnippetText()); - // do something with the snippet text - } - } - - } - //This test has set response to JSON and pass StringHandle with format as JSON, expectint it to work, logged an issue 82 - @Test - public void testBulkSearchSQDwithResponseFormatandStringHandle() throws Exception{ - int count =1; - loadTxtDocuments(); - loadJSONDocuments(); - TextDocumentManager docMgr = client.newTextDocumentManager(); - - QueryManager queryMgr = client.newQueryManager(); - StringQueryDefinition qd = queryMgr.newStringDefinition(); - qd.setCriteria("bar"); - - - docMgr.setResponseFormat(Format.JSON); - docMgr.setSearchView(QueryView.METADATA); - docMgr.setMetadataCategories(Metadata.PERMISSIONS); - - StringHandle results = new StringHandle().withFormat(Format.JSON); - DocumentPage page= docMgr.search(qd, 1,results); - DocumentMetadataHandle mh = new DocumentMetadataHandle(); - while(page.hasNext()){ - DocumentRecord rec = page.next(); - validateRecord(rec,Format.TEXT); - docMgr.readMetadata(rec.getUri(),mh); - assertTrue("Records has permissions? ",mh.getPermissions().containsKey("flexrep-eval")); - assertTrue("Record has collections ?",mh.getCollections().isEmpty()); - count++; - } - assertFalse("Search handle contains",results.get().isEmpty()); - - - } - //This test is testing SearchView options and search handle - @Test - public void testBulkSearchSQDwithJSONResponseFormat() throws Exception{ - - loadTxtDocuments(); - loadJSONDocuments(); - TextDocumentManager docMgr = client.newTextDocumentManager(); - - QueryManager queryMgr = client.newQueryManager(); - StringQueryDefinition qd = queryMgr.newStringDefinition(); - qd.setCriteria("woof"); - docMgr.setResponseFormat(Format.JSON); - - docMgr.setSearchView(QueryView.FACETS); - JacksonHandle jh = new JacksonHandle(); - DocumentPage page= docMgr.search(qd, 1,jh); - - // System.out.println(jh.get().toString()); - assertTrue("Searh response has entry for facets",jh.get().has("facets")); - assertTrue("Searh response has entry for facets",jh.get().has("results"));//Issue 84 is tracking this - assertTrue("Searh response has entry for facets",jh.get().has("metrics")); - - docMgr.setSearchView(QueryView.RESULTS); - page= docMgr.search(qd, 1,jh); - - assertFalse("Searh response has entry for facets",jh.get().has("facets")); - assertTrue("Searh response has entry for facets",jh.get().has("results")); - assertTrue("Searh response has entry for facets",jh.get().has("metrics"));//Issue 84 is tracking this - - docMgr.setSearchView(QueryView.METADATA); - page= docMgr.search(qd, 1,jh); - - assertFalse("Searh response has entry for facets",jh.get().has("facets")); - assertTrue("Searh response has entry for facets",jh.get().has("results")); - assertTrue("Searh response has entry for facets",jh.get().has("metrics")); - - docMgr.setSearchView(QueryView.ALL); - page= docMgr.search(qd, 1,jh); - - assertTrue("Searh response has entry for facets",jh.get().has("facets")); - assertTrue("Searh response has entry for facets",jh.get().has("results")); - assertTrue("Searh response has entry for facets",jh.get().has("metrics")); - - queryMgr.setView(QueryView.FACETS); - queryMgr.search(qd, jh); - System.out.println(jh.get().toString()); - - } - - //This test is to verify the transactions, verifies the search works with transaction before commit, after rollback and after commit - @Test - public void testBulkSearchSQDwithTransactionsandDOMHandle() throws Exception{ - TextDocumentManager docMgr = client.newTextDocumentManager(); - DOMHandle results = new DOMHandle(); - QueryManager queryMgr = client.newQueryManager(); - StringQueryDefinition qd = queryMgr.newStringDefinition(); - qd.setCriteria("thought"); - Transaction t= client.openTransaction(); - try{ - loadTxtDocuments(); - int count=1; - XMLDocumentManager xmldocMgr = client.newXMLDocumentManager(); - DocumentWriteSet writeset =xmldocMgr.newWriteSet(); - for(int i =0;i<102;i++){ - writeset.add(DIRECTORY+"boo"+i+".xml", new DOMHandle(getDocumentContent("This is so too much thought "+i))); - if(count%BATCH_SIZE == 0){ - xmldocMgr.write(writeset,t); - writeset = xmldocMgr.newWriteSet(); - } - count++; - } - if(count%BATCH_SIZE > 0){ - xmldocMgr.write(writeset,t); - } - count=0; - docMgr.setSearchView(QueryView.RESULTS); - - DocumentPage page= docMgr.search(qd, 1,results,t); - while(page.hasNext()){ - DocumentRecord rec = page.next(); - - validateRecord(rec,Format.XML); - count++; - } - assertTrue("Page has conttent :",page.hasContent()); - assertEquals("Total search results before transaction rollback are ","102",results.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total").getNodeValue()); - // System.out.println(results.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total").getNodeValue()); - - }catch(Exception e){ throw e;} - finally{t.rollback();} - - DocumentPage page= docMgr.search(qd, 1,results); - assertEquals("Total search results after rollback are ","0",results.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total").getNodeValue()); - - } -} - +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.IOException; +import java.util.HashMap; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.xml.sax.SAXException; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.Transaction; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.DocumentManager.Metadata; +import com.marklogic.client.document.DocumentPage; +import com.marklogic.client.document.DocumentRecord; +import com.marklogic.client.document.DocumentWriteSet; +import com.marklogic.client.document.JSONDocumentManager; +import com.marklogic.client.document.TextDocumentManager; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.DocumentMetadataHandle; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.JacksonHandle; +import com.marklogic.client.io.SearchHandle; +import com.marklogic.client.io.StringHandle; +import com.marklogic.client.io.DocumentMetadataHandle.Capability; +import com.marklogic.client.query.MatchDocumentSummary; +import com.marklogic.client.query.MatchLocation; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.QueryManager.QueryView; +import com.marklogic.client.query.StringQueryDefinition; + +public class TestBulkSearchWithStringQueryDef extends BasicJavaClientREST{ + private static final int BATCH_SIZE=100; + private static final String DIRECTORY ="/bulkSearch/"; + private static String dbName = "TestBulkSearchSQDDB"; + private static String [] fNames = {"TestBulkSearchSQDDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client ; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + setupAppServicesConstraint(dbName); + createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + System.out.println("In tear down" ); + tearDownJavaRESTServer(dbName, fNames, restServerName); + deleteRESTUser("usr1"); + } + + @Before + public void setUp() throws Exception { + // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + // create new connection for each test below + client = DatabaseClientFactory.newClient("localhost", restPort, "usr1", "password", Authentication.DIGEST); + } + + @After + public void tearDown() throws Exception { + System.out.println("Running clear script"); + // release client + client.release(); + } + public void loadJSONDocuments() throws JsonProcessingException, IOException{ + int count=1; + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + + HashMap 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); + } + } + public void validateRecord(DocumentRecord record,Format type) { + + assertNotNull("DocumentRecord should never be null", record); + assertNotNull("Document uri should never be null", record.getUri()); + assertTrue("Document uri should start with " + DIRECTORY, record.getUri().startsWith(DIRECTORY)); + assertEquals("All records are expected to be in same format", type, record.getFormat()); + // System.out.println(record.getMimetype()); + + } + public void loadTxtDocuments(){ + int count =1; + TextDocumentManager docMgr = client.newTextDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + for(int i =0;i<101;i++){ + writeset.add(DIRECTORY+"Textfoo"+i+".txt", new StringHandle().with("bar can be foo"+i)); + if(count%BATCH_SIZE == 0){ + docMgr.write(writeset); + writeset = docMgr.newWriteSet(); + } + count++; + } + if(count%BATCH_SIZE > 0){ + docMgr.write(writeset); + } + } + public void loadXMLDocuments() throws IOException, ParserConfigurationException, SAXException, TransformerException{ + int count=1; + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + for(int i =0;i<102;i++){ + + writeset.add(DIRECTORY+"foo"+i+".xml", new DOMHandle(getDocumentContent("This is so foo with a bar "+i))); + + if(count%BATCH_SIZE == 0){ + docMgr.write(writeset); + writeset = docMgr.newWriteSet(); + } + count++; + } + if(count%BATCH_SIZE > 0){ + docMgr.write(writeset); + } + } + @Test + public void testBulkSearchSQDwithDifferentPageSizes() { + int count; + loadTxtDocuments(); + //Creating a txt document manager for bulk search + TextDocumentManager docMgr = client.newTextDocumentManager(); + //using QueryManger for query definition and set the search criteria + QueryManager queryMgr = client.newQueryManager(); + StringQueryDefinition qd = queryMgr.newStringDefinition(); + qd.setCriteria("bar"); + // set document manager level settings for search response + System.out.println("Default Page length setting on docMgr :"+docMgr.getPageLength()); + docMgr.setPageLength(1); + docMgr.setSearchView(QueryView.RESULTS); + docMgr.setResponseFormat(Format.XML); + assertEquals("format set on document manager","XML",docMgr.getResponseFormat().toString()); + assertEquals("Queryview set on document manager ","RESULTS" ,docMgr.getSearchView().toString()); + assertEquals("Page length ",1,docMgr.getPageLength()); + // Search for documents where content has bar and get first result record, get search handle on it + SearchHandle sh = new SearchHandle(); + DocumentPage page= docMgr.search(qd, 0); + // test for page methods + assertEquals("Number of records",1,page.size()); + assertEquals("Starting record in first page ",1,page.getStart()); + assertEquals("Total number of estimated results:",101,page.getTotalSize()); + assertEquals("Total number of estimated pages :",101,page.getTotalPages()); + // till the issue #78 get fixed + assertFalse("Is this First page :",page.isFirstPage());//this is bug + assertFalse("Is this Last page :",page.isLastPage()); + assertTrue("Is this First page has content:",page.hasContent()); + // Need the Issue #75 to be fixed + assertTrue("Is first page has previous page ?",page.hasPreviousPage()); + // + long pageNo=1; + do{ + count=0; + page = docMgr.search(qd, pageNo,sh); + if(pageNo >1){ + assertFalse("Is this first Page", page.isFirstPage()); + assertTrue("Is page has previous page ?",page.hasPreviousPage()); + } + while(page.hasNext()){ + DocumentRecord rec = page.next(); + rec.getFormat(); + validateRecord(rec,Format.TEXT); + // System.out.println(rec.getUri()); + count++; + } + MatchDocumentSummary[] mds= sh.getMatchResults(); + assertEquals("Matched document count",1,mds.length); + //since we set the query view to get only results, facet count supposed be 0 + assertEquals("Matched Facet count",0,sh.getFacetNames().length); + + assertEquals("document count", page.size(),count); + // assertEquals("Page Number #",pageNo,page.getPageNumber()); + pageNo = pageNo + page.getPageSize(); + }while(!page.isLastPage()); + assertTrue("page count is 101 ",pageNo == page.getTotalPages()); + assertTrue("Page has previous page ?",page.hasPreviousPage()); + assertEquals("page size", 1,page.getPageSize()); + assertEquals("document count", 101,page.getTotalSize()); + page= docMgr.search(qd, 102); + assertFalse("Page has any records ?",page.hasContent()); + } + //This test is trying to set the setResponse to JSON on DocumentManager and use search handle which only work with XML + @Test(expected = UnsupportedOperationException.class) + public void testBulkSearchSQDwithWrongResponseFormat() throws Exception { + loadTxtDocuments(); + TextDocumentManager docMgr = client.newTextDocumentManager(); + QueryManager queryMgr = client.newQueryManager(); + StringQueryDefinition qd = queryMgr.newStringDefinition(); + qd.setCriteria("bar"); + docMgr.setResponseFormat(Format.JSON); + SearchHandle results = new SearchHandle(); + docMgr.search(qd, 1,results); + MatchDocumentSummary[] summaries = results.getMatchResults(); + for (MatchDocumentSummary summary : summaries ) { + MatchLocation[] locations = summary.getMatchLocations(); + for (MatchLocation location : locations) { + System.out.println(location.getAllSnippetText()); + // do something with the snippet text + } + } + + } + //This test has set response to JSON and pass StringHandle with format as JSON, expectint it to work, logged an issue 82 + @Test + public void testBulkSearchSQDwithResponseFormatandStringHandle() throws Exception{ + loadTxtDocuments(); + loadJSONDocuments(); + TextDocumentManager docMgr = client.newTextDocumentManager(); + + QueryManager queryMgr = client.newQueryManager(); + StringQueryDefinition qd = queryMgr.newStringDefinition(); + qd.setCriteria("bar"); + + + docMgr.setResponseFormat(Format.JSON); + docMgr.setSearchView(QueryView.METADATA); + docMgr.setMetadataCategories(Metadata.PERMISSIONS); + + StringHandle results = new StringHandle().withFormat(Format.JSON); + DocumentPage page= docMgr.search(qd, 1,results); + DocumentMetadataHandle mh = new DocumentMetadataHandle(); + while(page.hasNext()){ + DocumentRecord rec = page.next(); + validateRecord(rec,Format.TEXT); + docMgr.readMetadata(rec.getUri(),mh); + assertTrue("Records has permissions? ",mh.getPermissions().containsKey("flexrep-eval")); + assertTrue("Record has collections ?",mh.getCollections().isEmpty()); + } + assertFalse("Search handle contains",results.get().isEmpty()); + + + } + //This test is testing SearchView options and search handle + @Test + public void testBulkSearchSQDwithJSONResponseFormat() throws Exception{ + + loadTxtDocuments(); + loadJSONDocuments(); + TextDocumentManager docMgr = client.newTextDocumentManager(); + + QueryManager queryMgr = client.newQueryManager(); + StringQueryDefinition qd = queryMgr.newStringDefinition(); + qd.setCriteria("woof"); + docMgr.setResponseFormat(Format.JSON); + + docMgr.setSearchView(QueryView.FACETS); + JacksonHandle jh = new JacksonHandle(); + docMgr.search(qd, 1,jh); + + // System.out.println(jh.get().toString()); + assertTrue("Searh response has entry for facets",jh.get().has("facets")); + assertTrue("Searh response has entry for facets",jh.get().has("results"));//Issue 84 is tracking this + assertTrue("Searh response has entry for facets",jh.get().has("metrics")); + + docMgr.setSearchView(QueryView.RESULTS); + docMgr.search(qd, 1,jh); + + assertFalse("Searh response has entry for facets",jh.get().has("facets")); + assertTrue("Searh response has entry for facets",jh.get().has("results")); + assertTrue("Searh response has entry for facets",jh.get().has("metrics"));//Issue 84 is tracking this + + docMgr.setSearchView(QueryView.METADATA); + docMgr.search(qd, 1,jh); + + assertFalse("Searh response has entry for facets",jh.get().has("facets")); + assertTrue("Searh response has entry for facets",jh.get().has("results")); + assertTrue("Searh response has entry for facets",jh.get().has("metrics")); + + docMgr.setSearchView(QueryView.ALL); + docMgr.search(qd, 1,jh); + + assertTrue("Searh response has entry for facets",jh.get().has("facets")); + assertTrue("Searh response has entry for facets",jh.get().has("results")); + assertTrue("Searh response has entry for facets",jh.get().has("metrics")); + + queryMgr.setView(QueryView.FACETS); + queryMgr.search(qd, jh); + System.out.println(jh.get().toString()); + + } + + //This test is to verify the transactions, verifies the search works with transaction before commit, after rollback and after commit + @Test + public void testBulkSearchSQDwithTransactionsandDOMHandle() throws Exception{ + TextDocumentManager docMgr = client.newTextDocumentManager(); + DOMHandle results = new DOMHandle(); + QueryManager queryMgr = client.newQueryManager(); + StringQueryDefinition qd = queryMgr.newStringDefinition(); + qd.setCriteria("thought"); + Transaction t= client.openTransaction(); + try{ + loadTxtDocuments(); + int count=1; + XMLDocumentManager xmldocMgr = client.newXMLDocumentManager(); + DocumentWriteSet writeset =xmldocMgr.newWriteSet(); + for(int i =0;i<102;i++){ + writeset.add(DIRECTORY+"boo"+i+".xml", new DOMHandle(getDocumentContent("This is so too much thought "+i))); + if(count%BATCH_SIZE == 0){ + xmldocMgr.write(writeset,t); + writeset = xmldocMgr.newWriteSet(); + } + count++; + } + if(count%BATCH_SIZE > 0){ + xmldocMgr.write(writeset,t); + } + count=0; + docMgr.setSearchView(QueryView.RESULTS); + + DocumentPage page= docMgr.search(qd, 1,results,t); + while(page.hasNext()){ + DocumentRecord rec = page.next(); + + validateRecord(rec,Format.XML); + count++; + } + assertTrue("Page has conttent :",page.hasContent()); + assertEquals("Total search results before transaction rollback are ","102",results.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total").getNodeValue()); + // System.out.println(results.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total").getNodeValue()); + + }catch(Exception e){ throw e;} + finally{t.rollback();} + + DocumentPage page= docMgr.search(qd, 1,results); + assertEquals("Total search results after rollback are ","0",results.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total").getNodeValue()); + + } +} + diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStrucQueryDef.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStrucQueryDef.java index f5c164221..a98602cd8 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStrucQueryDef.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStrucQueryDef.java @@ -1,448 +1,441 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.IOException; -import java.util.HashMap; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.xml.sax.SAXException; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ArrayNode; -import com.fasterxml.jackson.databind.node.ObjectNode; -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.Transaction; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.document.DocumentPage; -import com.marklogic.client.document.DocumentRecord; -import com.marklogic.client.document.DocumentWriteSet; -import com.marklogic.client.document.GenericDocumentManager; -import com.marklogic.client.document.JSONDocumentManager; -import com.marklogic.client.document.TextDocumentManager; -import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.document.DocumentManager.Metadata; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.DocumentMetadataHandle; -import com.marklogic.client.io.Format; -import com.marklogic.client.io.JacksonHandle; -import com.marklogic.client.io.SearchHandle; -import com.marklogic.client.io.StringHandle; -import com.marklogic.client.io.DocumentMetadataHandle.Capability; -import com.marklogic.client.query.MatchDocumentSummary; -import com.marklogic.client.query.MatchLocation; -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.query.RawStructuredQueryDefinition; -import com.marklogic.client.query.StringQueryDefinition; -import com.marklogic.client.query.QueryManager.QueryView; -import com.marklogic.client.query.StructuredQueryBuilder; -import com.marklogic.client.query.StructuredQueryDefinition; - -public class TestBulkSearchWithStrucQueryDef extends BasicJavaClientREST{ - - private static final int BATCH_SIZE=100; - private static final String DIRECTORY ="/bulkSearch/"; - private static String dbName = "TestBulkSearchStrucQDDB"; - private static String [] fNames = {"TestBulkSearchStrucQDDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort = 8011; - private DatabaseClient client ; - - @BeforeClass - public static void setUpBeforeClass() throws Exception { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); - setupAppServicesConstraint(dbName); - createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); - } - - @AfterClass - public static void tearDownAfterClass() throws Exception { - System.out.println("In tear down" ); - tearDownJavaRESTServer(dbName, fNames, restServerName); - deleteRESTUser("usr1"); - } - - @Before - public void setUp() throws Exception { - // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); - // create new connection for each test below - client = DatabaseClientFactory.newClient("localhost", restPort, "usr1", "password", Authentication.DIGEST); - } - - @After - public void tearDown() throws Exception { - System.out.println("Running clear script"); - // release client - client.release(); - } - public void loadJSONDocuments() throws JsonProcessingException, IOException{ - int count=1; - JSONDocumentManager docMgr = client.newJSONDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - - HashMap 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); - } - } - public void validateRecord(DocumentRecord record,Format type) { - - assertNotNull("DocumentRecord should never be null", record); - assertNotNull("Document uri should never be null", record.getUri()); - assertTrue("Document uri should start with " + DIRECTORY, record.getUri().startsWith(DIRECTORY)); - assertEquals("All records are expected to be in same format", type, record.getFormat()); - // System.out.println(record.getMimetype()); - - } - - public void loadXMLDocuments() throws IOException, ParserConfigurationException, SAXException, TransformerException{ - int count=1; - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - for(int i =0;i<102;i++){ - - writeset.add(DIRECTORY+"foo"+i+".xml", new DOMHandle(getDocumentContent("This is so foo with a bar "+i))); - - if(count%BATCH_SIZE == 0){ - docMgr.write(writeset); - writeset = docMgr.newWriteSet(); - } - count++; - } - if(count%BATCH_SIZE > 0){ - docMgr.write(writeset); - } - } - @Test - public void testBulkSearchSQDwithDifferentPageSizes() throws IOException, ParserConfigurationException, SAXException, TransformerException { - int count; - loadXMLDocuments(); - //Creating a txt document manager for bulk search - TextDocumentManager docMgr = client.newTextDocumentManager(); - //using QueryManger for query definition and set the search criteria - QueryManager queryMgr = client.newQueryManager(); - StructuredQueryBuilder qb = new StructuredQueryBuilder(); - StructuredQueryDefinition qd = qb.and(qb.term("foo","bar")); - - // set document manager level settings for search response - System.out.println("Default Page length setting on docMgr :"+docMgr.getPageLength()); - docMgr.setPageLength(1); - docMgr.setSearchView(QueryView.RESULTS); - docMgr.setResponseFormat(Format.XML); - assertEquals("format set on document manager","XML",docMgr.getResponseFormat().toString()); - assertEquals("Queryview set on document manager ","RESULTS" ,docMgr.getSearchView().toString()); - assertEquals("Page length ",1,docMgr.getPageLength()); - // Search for documents where content has bar and get first result record, get search handle on it - SearchHandle sh = new SearchHandle(); - DocumentPage page= docMgr.search(qd, 0); - // test for page methods - assertEquals("Number of records",1,page.size()); - assertEquals("Starting record in first page ",1,page.getStart()); - assertEquals("Total number of estimated results:",102,page.getTotalSize()); - assertEquals("Total number of estimated pages :",102,page.getTotalPages()); - // till the issue #78 get fixed - assertFalse("Is this First page :",page.isFirstPage());//this is bug - assertFalse("Is this Last page :",page.isLastPage()); - assertTrue("Is this First page has content:",page.hasContent()); - // Need the Issue #75 to be fixed - assertTrue("Is first page has previous page ?",page.hasPreviousPage()); - // - long pageNo=1; - do{ - count=0; - page = docMgr.search(qd, pageNo,sh); - if(pageNo >1){ - assertFalse("Is this first Page", page.isFirstPage()); - assertTrue("Is page has previous page ?",page.hasPreviousPage()); - } - while(page.hasNext()){ - DocumentRecord rec = page.next(); - rec.getFormat(); - validateRecord(rec,Format.XML); - // System.out.println(rec.getUri()); - count++; - } - MatchDocumentSummary[] mds= sh.getMatchResults(); - assertEquals("Matched document count",1,mds.length); - //since we set the query view to get only results, facet count supposed be 0 - assertEquals("Matched Facet count",0,sh.getFacetNames().length); - - assertEquals("document count", page.size(),count); - // assertEquals("Page Number #",pageNo,page.getPageNumber()); - pageNo = pageNo + page.getPageSize(); - }while(!page.isLastPage()); - assertTrue("page count is 101 ",pageNo == page.getTotalPages()); - assertTrue("Page has previous page ?",page.hasPreviousPage()); - assertEquals("page size", 1,page.getPageSize()); - assertEquals("document count", 102,page.getTotalSize()); - page= docMgr.search(qd, 103); - assertFalse("Page has any records ?",page.hasContent()); - } - - //This test has set response to JSON and pass StringHandle with format as JSON, expectint it to work, logged an issue 82 - @Test - public void testBulkSearchSQDwithResponseFormatandStringHandle() throws Exception{ - int count =1; - loadJSONDocuments(); - JSONDocumentManager docMgr = client.newJSONDocumentManager(); - - QueryManager queryMgr = client.newQueryManager(); - StructuredQueryBuilder qb = new StructuredQueryBuilder(); - StructuredQueryDefinition qd = qb.and(qb.term("dog1","dog11")); - SearchHandle r = queryMgr.search(qd, new SearchHandle()); - - docMgr.setResponseFormat(Format.JSON); - docMgr.setSearchView(QueryView.METADATA); - docMgr.setMetadataCategories(Metadata.PERMISSIONS); - - StringHandle results = new StringHandle().withFormat(Format.JSON); - DocumentPage page= docMgr.search(qd, 1,results); - DocumentMetadataHandle mh = new DocumentMetadataHandle(); - while(page.hasNext()){ - DocumentRecord rec = page.next(); - validateRecord(rec,Format.JSON); - docMgr.readMetadata(rec.getUri(),mh); - assertTrue("Records has permissions? ",mh.getPermissions().containsKey("flexrep-eval")); - assertTrue("Record has collections ?",mh.getCollections().isEmpty()); - count++; - } - assertFalse("Search handle contains",results.get().isEmpty()); - - - } - //This test is testing SearchView options and search handle - @Test - public void testBulkSearchSQDwithJSONResponseFormat() throws Exception{ - - loadJSONDocuments(); - JSONDocumentManager docMgr = client.newJSONDocumentManager(); - - QueryManager queryMgr = client.newQueryManager(); - StructuredQueryBuilder qb = new StructuredQueryBuilder(); - StructuredQueryDefinition qd = qb.and(qb.term("woof")); - docMgr.setResponseFormat(Format.JSON); - - docMgr.setSearchView(QueryView.FACETS); - JacksonHandle jh = new JacksonHandle(); - DocumentPage page= docMgr.search(qd, 1,jh); - - // System.out.println(jh.get().toString()); - assertTrue("Searh response has entry for facets",jh.get().has("facets")); - assertTrue("Searh response has entry for facets",jh.get().has("results"));//Issue 84 is tracking this - assertTrue("Searh response has entry for facets",jh.get().has("metrics")); - - docMgr.setSearchView(QueryView.RESULTS); - page= docMgr.search(qd, 1,jh); - - assertFalse("Searh response has entry for facets",jh.get().has("facets")); - assertTrue("Searh response has entry for facets",jh.get().has("results")); - assertTrue("Searh response has entry for facets",jh.get().has("metrics"));//Issue 84 is tracking this - - docMgr.setSearchView(QueryView.METADATA); - page= docMgr.search(qd, 1,jh); - - assertFalse("Searh response has entry for facets",jh.get().has("facets")); - assertTrue("Searh response has entry for facets",jh.get().has("results")); - assertTrue("Searh response has entry for facets",jh.get().has("metrics")); - - docMgr.setSearchView(QueryView.ALL); - page= docMgr.search(qd, 1,jh); - - assertTrue("Searh response has entry for facets",jh.get().has("facets")); - assertTrue("Searh response has entry for facets",jh.get().has("results")); - assertTrue("Searh response has entry for facets",jh.get().has("metrics")); - - queryMgr.setView(QueryView.FACETS); - queryMgr.search(qd, jh); - System.out.println(jh.get().toString()); - - } - - //This test is to verify the transactions, verifies the search works with transaction before commit, after rollback and after commit - @Test - public void testBulkSearchSQDwithTransactionsandDOMHandle() throws Exception{ - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - DOMHandle results = new DOMHandle(); - QueryManager queryMgr = client.newQueryManager(); - StructuredQueryBuilder qb = new StructuredQueryBuilder(); - StructuredQueryDefinition qd = qb.and(qb.term("much","thought")); - Transaction t= client.openTransaction(); - try{ - int count=1; - XMLDocumentManager xmldocMgr = client.newXMLDocumentManager(); - DocumentWriteSet writeset =xmldocMgr.newWriteSet(); - for(int i =0;i<102;i++){ - writeset.add(DIRECTORY+"boo"+i+".xml", new DOMHandle(getDocumentContent("This is so too much thought "+i))); - if(count%BATCH_SIZE == 0){ - xmldocMgr.write(writeset,t); - writeset = xmldocMgr.newWriteSet(); - } - count++; - } - if(count%BATCH_SIZE > 0){ - xmldocMgr.write(writeset,t); - } - count=0; - docMgr.setSearchView(QueryView.RESULTS); - - DocumentPage page= docMgr.search(qd, 1,results,t); - while(page.hasNext()){ - DocumentRecord rec = page.next(); - - validateRecord(rec,Format.XML); - count++; - } - assertTrue("Page has conttent :",page.hasContent()); - assertEquals("Total search results before transaction rollback are ","102",results.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total").getNodeValue()); - // System.out.println(results.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total").getNodeValue()); - - }catch(Exception e){ throw e;} - finally{t.rollback();} - - DocumentPage page= docMgr.search(qd, 1,results); - assertEquals("Total search results after rollback are ","0",results.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total").getNodeValue()); - - } - //This test is to verify RAW XML structured Query - @Test - public void testBulkSearchRawXMLStrucQD() throws Exception{ - setAutomaticDirectoryCreation(dbName,"automatic"); - setMaintainLastModified(dbName,true); - this.loadJSONDocuments(); - this.loadXMLDocuments(); - GenericDocumentManager docMgr= client.newDocumentManager(); - QueryManager queryMgr = client.newQueryManager(); - String rawXMLQuery = - ""+ - " "+ - "bar"+ - ""+ - ""+ - "woof"+ - " "+ - ""; - StringHandle rh = new StringHandle(rawXMLQuery); - RawStructuredQueryDefinition qd = - queryMgr.newRawStructuredQueryDefinition(rh); - DOMHandle dh = new DOMHandle(); - DocumentPage page= docMgr.search(qd, 1,dh); - DocumentMetadataHandle mh = new DocumentMetadataHandle(); - - int count=1; - while(count < 4) - { - page= docMgr.search(qd, count,dh); - - while(page.hasNext()){ - DocumentRecord rec = page.next(); - if(rec.getMimetype().contains("xml")){ - validateRecord(rec,Format.XML); - } - else{ - validateRecord(rec,Format.JSON); - } - docMgr.readMetadata(rec.getUri(),mh); - assertTrue("Records has permissions? ",mh.getPermissions().containsKey("flexrep-eval")); - assertFalse("Record has collections ?",mh.getCollections().isEmpty()); - - } - System.out.println(this.convertXMLDocumentToString(dh.get())); - assertEquals("Total search results before transaction rollback are ","204",dh.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total").getNodeValue()); - count++; - - } - - } - //This test is to verify RAW JSON structured query - @Test - public void testBulkSearchRawJSONStrucQD() throws Exception{ - setAutomaticDirectoryCreation(dbName,"automatic"); - setMaintainLastModified(dbName,true); - this.loadJSONDocuments(); - this.loadXMLDocuments(); - GenericDocumentManager docMgr= client.newDocumentManager(); - QueryManager queryMgr = client.newQueryManager(); - JacksonHandle jh = new JacksonHandle(); - ObjectMapper mapper = new ObjectMapper(); - // constructing JSON representation of Raw JSON Structured Query - - ObjectNode txtNode = mapper.createObjectNode(); - txtNode.putArray("text").add("woof"); - ObjectNode termQNode = mapper.createObjectNode(); - termQNode.set("term-query", txtNode); - ObjectNode queriesArrayNode = mapper.createObjectNode(); - queriesArrayNode.putArray("queries").add(termQNode); - - ObjectNode txtNode2 = mapper.createObjectNode(); - txtNode2.putArray("text").add("bar"); - ObjectNode termQNode2 = mapper.createObjectNode(); - termQNode2.set("term-query", txtNode2); - queriesArrayNode.withArray("queries").add(termQNode2); - - ObjectNode orQueryNode = mapper.createObjectNode(); - orQueryNode.set("or-query",queriesArrayNode ); - - ObjectNode queryArrayNode = mapper.createObjectNode(); - queryArrayNode.putArray("queries").add(orQueryNode); - ObjectNode mainNode = mapper.createObjectNode(); - mainNode.set("query", queryArrayNode); - jh.set(mainNode); - RawStructuredQueryDefinition qd = - queryMgr.newRawStructuredQueryDefinition(jh); - System.out.println(jh.get().toString()); - docMgr.setResponseFormat(Format.JSON); - JacksonHandle results = new JacksonHandle(); - DocumentPage page= docMgr.search(qd, 1,results); - DocumentMetadataHandle mh = new DocumentMetadataHandle(); - - int count=1; - while(count < 4) - { - page= docMgr.search(qd, count,results); - - while(page.hasNext()){ - DocumentRecord rec = page.next(); - if(rec.getMimetype().contains("xml")){ - validateRecord(rec,Format.XML); - } - else{ - validateRecord(rec,Format.JSON); - } - docMgr.readMetadata(rec.getUri(),mh); - assertTrue("Records has permissions? ",mh.getPermissions().containsKey("flexrep-eval")); - assertFalse("Record has collections ?",mh.getCollections().isEmpty()); - - } - - count++; - } - System.out.println(results.get().toString()); - assertEquals("Total search results before transaction rollback are ","204",results.get().get("total").asText()); - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.IOException; +import java.util.HashMap; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.xml.sax.SAXException; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.Transaction; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.DocumentPage; +import com.marklogic.client.document.DocumentRecord; +import com.marklogic.client.document.DocumentWriteSet; +import com.marklogic.client.document.GenericDocumentManager; +import com.marklogic.client.document.JSONDocumentManager; +import com.marklogic.client.document.TextDocumentManager; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.document.DocumentManager.Metadata; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.DocumentMetadataHandle; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.JacksonHandle; +import com.marklogic.client.io.SearchHandle; +import com.marklogic.client.io.StringHandle; +import com.marklogic.client.io.DocumentMetadataHandle.Capability; +import com.marklogic.client.query.MatchDocumentSummary; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.RawStructuredQueryDefinition; +import com.marklogic.client.query.QueryManager.QueryView; +import com.marklogic.client.query.StructuredQueryBuilder; +import com.marklogic.client.query.StructuredQueryDefinition; + +public class TestBulkSearchWithStrucQueryDef extends BasicJavaClientREST{ + + private static final int BATCH_SIZE=100; + private static final String DIRECTORY ="/bulkSearch/"; + private static String dbName = "TestBulkSearchStrucQDDB"; + private static String [] fNames = {"TestBulkSearchStrucQDDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client ; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + setupAppServicesConstraint(dbName); + createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + System.out.println("In tear down" ); + tearDownJavaRESTServer(dbName, fNames, restServerName); + deleteRESTUser("usr1"); + } + + @Before + public void setUp() throws Exception { + // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + // create new connection for each test below + client = DatabaseClientFactory.newClient("localhost", restPort, "usr1", "password", Authentication.DIGEST); + } + + @After + public void tearDown() throws Exception { + System.out.println("Running clear script"); + // release client + client.release(); + } + public void loadJSONDocuments() throws JsonProcessingException, IOException{ + int count=1; + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + + HashMap 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); + } + } + public void validateRecord(DocumentRecord record,Format type) { + + assertNotNull("DocumentRecord should never be null", record); + assertNotNull("Document uri should never be null", record.getUri()); + assertTrue("Document uri should start with " + DIRECTORY, record.getUri().startsWith(DIRECTORY)); + assertEquals("All records are expected to be in same format", type, record.getFormat()); + // System.out.println(record.getMimetype()); + + } + + public void loadXMLDocuments() throws IOException, ParserConfigurationException, SAXException, TransformerException{ + int count=1; + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + for(int i =0;i<102;i++){ + + writeset.add(DIRECTORY+"foo"+i+".xml", new DOMHandle(getDocumentContent("This is so foo with a bar "+i))); + + if(count%BATCH_SIZE == 0){ + docMgr.write(writeset); + writeset = docMgr.newWriteSet(); + } + count++; + } + if(count%BATCH_SIZE > 0){ + docMgr.write(writeset); + } + } + @Test + public void testBulkSearchSQDwithDifferentPageSizes() throws IOException, ParserConfigurationException, SAXException, TransformerException { + int count; + loadXMLDocuments(); + //Creating a txt document manager for bulk search + TextDocumentManager docMgr = client.newTextDocumentManager(); + //using QueryManger for query definition and set the search criteria + StructuredQueryBuilder qb = new StructuredQueryBuilder(); + StructuredQueryDefinition qd = qb.and(qb.term("foo","bar")); + + // set document manager level settings for search response + System.out.println("Default Page length setting on docMgr :"+docMgr.getPageLength()); + docMgr.setPageLength(1); + docMgr.setSearchView(QueryView.RESULTS); + docMgr.setResponseFormat(Format.XML); + assertEquals("format set on document manager","XML",docMgr.getResponseFormat().toString()); + assertEquals("Queryview set on document manager ","RESULTS" ,docMgr.getSearchView().toString()); + assertEquals("Page length ",1,docMgr.getPageLength()); + // Search for documents where content has bar and get first result record, get search handle on it + SearchHandle sh = new SearchHandle(); + DocumentPage page= docMgr.search(qd, 0); + // test for page methods + assertEquals("Number of records",1,page.size()); + assertEquals("Starting record in first page ",1,page.getStart()); + assertEquals("Total number of estimated results:",102,page.getTotalSize()); + assertEquals("Total number of estimated pages :",102,page.getTotalPages()); + // till the issue #78 get fixed + assertFalse("Is this First page :",page.isFirstPage());//this is bug + assertFalse("Is this Last page :",page.isLastPage()); + assertTrue("Is this First page has content:",page.hasContent()); + // Need the Issue #75 to be fixed + assertTrue("Is first page has previous page ?",page.hasPreviousPage()); + // + long pageNo=1; + do{ + count=0; + page = docMgr.search(qd, pageNo,sh); + if(pageNo >1){ + assertFalse("Is this first Page", page.isFirstPage()); + assertTrue("Is page has previous page ?",page.hasPreviousPage()); + } + while(page.hasNext()){ + DocumentRecord rec = page.next(); + rec.getFormat(); + validateRecord(rec,Format.XML); + // System.out.println(rec.getUri()); + count++; + } + MatchDocumentSummary[] mds= sh.getMatchResults(); + assertEquals("Matched document count",1,mds.length); + //since we set the query view to get only results, facet count supposed be 0 + assertEquals("Matched Facet count",0,sh.getFacetNames().length); + + assertEquals("document count", page.size(),count); + // assertEquals("Page Number #",pageNo,page.getPageNumber()); + pageNo = pageNo + page.getPageSize(); + }while(!page.isLastPage()); + assertTrue("page count is 101 ",pageNo == page.getTotalPages()); + assertTrue("Page has previous page ?",page.hasPreviousPage()); + assertEquals("page size", 1,page.getPageSize()); + assertEquals("document count", 102,page.getTotalSize()); + page= docMgr.search(qd, 103); + assertFalse("Page has any records ?",page.hasContent()); + } + + //This test has set response to JSON and pass StringHandle with format as JSON, expectint it to work, logged an issue 82 + @Test + public void testBulkSearchSQDwithResponseFormatandStringHandle() throws Exception{ + loadJSONDocuments(); + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + + QueryManager queryMgr = client.newQueryManager(); + StructuredQueryBuilder qb = new StructuredQueryBuilder(); + StructuredQueryDefinition qd = qb.and(qb.term("dog1","dog11")); + queryMgr.search(qd, new SearchHandle()); + + docMgr.setResponseFormat(Format.JSON); + docMgr.setSearchView(QueryView.METADATA); + docMgr.setMetadataCategories(Metadata.PERMISSIONS); + + StringHandle results = new StringHandle().withFormat(Format.JSON); + DocumentPage page= docMgr.search(qd, 1,results); + DocumentMetadataHandle mh = new DocumentMetadataHandle(); + while(page.hasNext()){ + DocumentRecord rec = page.next(); + validateRecord(rec,Format.JSON); + docMgr.readMetadata(rec.getUri(),mh); + assertTrue("Records has permissions? ",mh.getPermissions().containsKey("flexrep-eval")); + assertTrue("Record has collections ?",mh.getCollections().isEmpty()); + } + assertFalse("Search handle contains",results.get().isEmpty()); + + + } + //This test is testing SearchView options and search handle + @Test + public void testBulkSearchSQDwithJSONResponseFormat() throws Exception{ + + loadJSONDocuments(); + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + + QueryManager queryMgr = client.newQueryManager(); + StructuredQueryBuilder qb = new StructuredQueryBuilder(); + StructuredQueryDefinition qd = qb.and(qb.term("woof")); + docMgr.setResponseFormat(Format.JSON); + + docMgr.setSearchView(QueryView.FACETS); + JacksonHandle jh = new JacksonHandle(); + docMgr.search(qd, 1,jh); + + // System.out.println(jh.get().toString()); + assertTrue("Searh response has entry for facets",jh.get().has("facets")); + assertTrue("Searh response has entry for facets",jh.get().has("results"));//Issue 84 is tracking this + assertTrue("Searh response has entry for facets",jh.get().has("metrics")); + + docMgr.setSearchView(QueryView.RESULTS); + docMgr.search(qd, 1,jh); + + assertFalse("Searh response has entry for facets",jh.get().has("facets")); + assertTrue("Searh response has entry for facets",jh.get().has("results")); + assertTrue("Searh response has entry for facets",jh.get().has("metrics"));//Issue 84 is tracking this + + docMgr.setSearchView(QueryView.METADATA); + docMgr.search(qd, 1,jh); + + assertFalse("Searh response has entry for facets",jh.get().has("facets")); + assertTrue("Searh response has entry for facets",jh.get().has("results")); + assertTrue("Searh response has entry for facets",jh.get().has("metrics")); + + docMgr.setSearchView(QueryView.ALL); + docMgr.search(qd, 1,jh); + + assertTrue("Searh response has entry for facets",jh.get().has("facets")); + assertTrue("Searh response has entry for facets",jh.get().has("results")); + assertTrue("Searh response has entry for facets",jh.get().has("metrics")); + + queryMgr.setView(QueryView.FACETS); + queryMgr.search(qd, jh); + System.out.println(jh.get().toString()); + + } + + //This test is to verify the transactions, verifies the search works with transaction before commit, after rollback and after commit + @Test + public void testBulkSearchSQDwithTransactionsandDOMHandle() throws Exception{ + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DOMHandle results = new DOMHandle(); + StructuredQueryBuilder qb = new StructuredQueryBuilder(); + StructuredQueryDefinition qd = qb.and(qb.term("much","thought")); + Transaction t= client.openTransaction(); + try{ + int count=1; + XMLDocumentManager xmldocMgr = client.newXMLDocumentManager(); + DocumentWriteSet writeset =xmldocMgr.newWriteSet(); + for(int i =0;i<102;i++){ + writeset.add(DIRECTORY+"boo"+i+".xml", new DOMHandle(getDocumentContent("This is so too much thought "+i))); + if(count%BATCH_SIZE == 0){ + xmldocMgr.write(writeset,t); + writeset = xmldocMgr.newWriteSet(); + } + count++; + } + if(count%BATCH_SIZE > 0){ + xmldocMgr.write(writeset,t); + } + count=0; + docMgr.setSearchView(QueryView.RESULTS); + + DocumentPage page= docMgr.search(qd, 1,results,t); + while(page.hasNext()){ + DocumentRecord rec = page.next(); + + validateRecord(rec,Format.XML); + count++; + } + assertTrue("Page has conttent :",page.hasContent()); + assertEquals("Total search results before transaction rollback are ","102",results.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total").getNodeValue()); + // System.out.println(results.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total").getNodeValue()); + + }catch(Exception e){ throw e;} + finally{t.rollback();} + + docMgr.search(qd, 1,results); + assertEquals("Total search results after rollback are ","0",results.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total").getNodeValue()); + + } + //This test is to verify RAW XML structured Query + @Test + public void testBulkSearchRawXMLStrucQD() throws Exception{ + setAutomaticDirectoryCreation(dbName,"automatic"); + setMaintainLastModified(dbName,true); + this.loadJSONDocuments(); + this.loadXMLDocuments(); + GenericDocumentManager docMgr= client.newDocumentManager(); + QueryManager queryMgr = client.newQueryManager(); + String rawXMLQuery = + ""+ + " "+ + "bar"+ + ""+ + ""+ + "woof"+ + " "+ + ""; + StringHandle rh = new StringHandle(rawXMLQuery); + RawStructuredQueryDefinition qd = + queryMgr.newRawStructuredQueryDefinition(rh); + DOMHandle dh = new DOMHandle(); + DocumentPage page= docMgr.search(qd, 1,dh); + DocumentMetadataHandle mh = new DocumentMetadataHandle(); + + int count=1; + while(count < 4) + { + page= docMgr.search(qd, count,dh); + + while(page.hasNext()){ + DocumentRecord rec = page.next(); + if(rec.getMimetype().contains("xml")){ + validateRecord(rec,Format.XML); + } + else{ + validateRecord(rec,Format.JSON); + } + docMgr.readMetadata(rec.getUri(),mh); + assertTrue("Records has permissions? ",mh.getPermissions().containsKey("flexrep-eval")); + assertFalse("Record has collections ?",mh.getCollections().isEmpty()); + + } + System.out.println(this.convertXMLDocumentToString(dh.get())); + assertEquals("Total search results before transaction rollback are ","204",dh.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total").getNodeValue()); + count++; + + } + + } + //This test is to verify RAW JSON structured query + @Test + public void testBulkSearchRawJSONStrucQD() throws Exception{ + setAutomaticDirectoryCreation(dbName,"automatic"); + setMaintainLastModified(dbName,true); + this.loadJSONDocuments(); + this.loadXMLDocuments(); + GenericDocumentManager docMgr= client.newDocumentManager(); + QueryManager queryMgr = client.newQueryManager(); + JacksonHandle jh = new JacksonHandle(); + ObjectMapper mapper = new ObjectMapper(); + // constructing JSON representation of Raw JSON Structured Query + + ObjectNode txtNode = mapper.createObjectNode(); + txtNode.putArray("text").add("woof"); + ObjectNode termQNode = mapper.createObjectNode(); + termQNode.set("term-query", txtNode); + ObjectNode queriesArrayNode = mapper.createObjectNode(); + queriesArrayNode.putArray("queries").add(termQNode); + + ObjectNode txtNode2 = mapper.createObjectNode(); + txtNode2.putArray("text").add("bar"); + ObjectNode termQNode2 = mapper.createObjectNode(); + termQNode2.set("term-query", txtNode2); + queriesArrayNode.withArray("queries").add(termQNode2); + + ObjectNode orQueryNode = mapper.createObjectNode(); + orQueryNode.set("or-query",queriesArrayNode ); + + ObjectNode queryArrayNode = mapper.createObjectNode(); + queryArrayNode.putArray("queries").add(orQueryNode); + ObjectNode mainNode = mapper.createObjectNode(); + mainNode.set("query", queryArrayNode); + jh.set(mainNode); + RawStructuredQueryDefinition qd = + queryMgr.newRawStructuredQueryDefinition(jh); + System.out.println(jh.get().toString()); + docMgr.setResponseFormat(Format.JSON); + JacksonHandle results = new JacksonHandle(); + DocumentPage page= docMgr.search(qd, 1,results); + DocumentMetadataHandle mh = new DocumentMetadataHandle(); + + int count=1; + while(count < 4) + { + page= docMgr.search(qd, count,results); + + while(page.hasNext()){ + DocumentRecord rec = page.next(); + if(rec.getMimetype().contains("xml")){ + validateRecord(rec,Format.XML); + } + else{ + validateRecord(rec,Format.JSON); + } + docMgr.readMetadata(rec.getUri(),mh); + assertTrue("Records has permissions? ",mh.getPermissions().containsKey("flexrep-eval")); + assertFalse("Record has collections ?",mh.getCollections().isEmpty()); + + } + + count++; + } + System.out.println(results.get().toString()); + assertEquals("Total search results before transaction rollback are ","204",results.get().get("total").asText()); + } +} From 4d2fb9f31505884ce98dde56bf2977f1cccd9aab Mon Sep 17 00:00:00 2001 From: gvaidees Date: Wed, 20 Aug 2014 18:05:00 -0700 Subject: [PATCH 074/357] Code cleanup --- .../TestBulkWriteWithTransformations.java | 438 +++++++++--------- 1 file changed, 217 insertions(+), 221 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteWithTransformations.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteWithTransformations.java index 176833685..2208fc213 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteWithTransformations.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteWithTransformations.java @@ -1,221 +1,217 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.File; -import java.util.HashMap; -import java.util.Scanner; - -import javax.xml.bind.JAXBContext; -import javax.xml.transform.OutputKeys; -import javax.xml.transform.Source; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.stream.StreamSource; - -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.admin.ExtensionMetadata; -import com.marklogic.client.admin.TransformExtensionsManager; -import com.marklogic.client.document.DocumentPage; -import com.marklogic.client.document.DocumentRecord; -import com.marklogic.client.document.DocumentWriteSet; -import com.marklogic.client.document.ServerTransform; -import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.FileHandle; -import com.marklogic.client.io.Format; -import com.marklogic.client.io.InputStreamHandle; -import com.marklogic.client.io.JAXBHandle; -import com.marklogic.client.io.SourceHandle; - -public class TestBulkWriteWithTransformations extends BasicJavaClientREST{ - private static final int BATCH_SIZE=100; - private static final String DIRECTORY ="/bulkTransform/"; - private static String dbName = "TestBulkWriteWithTransformDB"; - private static String [] fNames = {"TestBulkWriteWithTransformDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort = 8011; - private DatabaseClient client ; - - @BeforeClass - public static void setUpBeforeClass() throws Exception { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); - setupAppServicesConstraint(dbName); - } - - @AfterClass - public static void tearDownAfterClass() throws Exception { - System.out.println("In tear down" ); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } - - @Before - public void setUp() throws Exception { -// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); - // create new connection for each test below - client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST); - } - - @After - public void tearDown() throws Exception { - System.out.println("Running clear script"); - // release client - client.release(); - } - - @Test - public void testBulkLoadWithXSLTClientSideTransform() throws Exception { - String docId[] ={"/transform/emp.xml","/transform/food1.xml","/transform/food2.xml"}; - Source s[] = new Source[3]; - s[0] = new StreamSource("src/test/java/com/marklogic/javaclient/data/employee.xml"); - s[1] = new StreamSource("src/test/java/com/marklogic/javaclient/data/xml-original.xml"); - s[2] = new StreamSource("src/test/java/com/marklogic/javaclient/data/xml-original-test.xml"); - // get the xslt - Source xsl = new StreamSource("src/test/java/com/marklogic/javaclient/data/employee-stylesheet.xsl"); - - // create transformer - TransformerFactory factory = TransformerFactory.newInstance(); - Transformer transformer = factory.newTransformer(xsl); - transformer.setOutputProperty(OutputKeys.INDENT, "yes"); - - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - for(int i=0;i<3;i++){ - SourceHandle handle = new SourceHandle(); - handle.set(s[i]); - // set the transformer - handle.setTransformer(transformer); - writeset.add(docId[i],handle); - } - docMgr.write(writeset); - FileHandle dh = new FileHandle(); - // DOMHandle dh = new DOMHandle(); - docMgr.read(docId[0], dh); - Scanner scanner = new Scanner(dh.get()).useDelimiter("\\Z"); - String readContent = scanner.next(); - assertTrue("xml document contains firstname", readContent.contains("firstname")); - docMgr.read(docId[1], dh); - Scanner sc1 = new Scanner(dh.get()).useDelimiter("\\Z"); - readContent = sc1.next(); - assertTrue("xml document contains firstname", readContent.contains("firstname")); - docMgr.read(docId[2], dh); - Scanner sc2 = new Scanner(dh.get()).useDelimiter("\\Z"); - readContent = sc2.next(); - assertTrue("xml document contains firstname", readContent.contains("firstname")); - - } - @Test - public void testBulkLoadWithXQueryTransform() throws Exception { - - TransformExtensionsManager transMgr = - client.newServerConfigManager().newTransformExtensionsManager(); - ExtensionMetadata metadata = new ExtensionMetadata(); - metadata.setTitle("Adding attribute xquery Transform"); - metadata.setDescription("This plugin transforms an XML document by adding attribute to root node"); - metadata.setProvider("MarkLogic"); - metadata.setVersion("0.1"); - // get the transform file - File transformFile = new File("src/test/java/com/marklogic/javaclient/transforms/add-attr-xquery-transform.xqy"); - FileHandle transformHandle = new FileHandle(transformFile); - transMgr.writeXQueryTransform("add-attr-xquery-transform", transformHandle, metadata); - ServerTransform transform = new ServerTransform("add-attr-xquery-transform"); - transform.put("name", "Lang"); - transform.put("value", "English"); - int count=1; - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - HashMap map= new HashMap(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - for(int i =0;i<102;i++){ - - writeset.add(DIRECTORY+"foo"+i+".xml", new DOMHandle(getDocumentContent("This is so foo"+i))); - map.put(DIRECTORY+"foo"+i+".xml", convertXMLDocumentToString(getDocumentContent("This is so foo"+i))); - if(count%BATCH_SIZE == 0){ - docMgr.write(writeset,transform); - writeset = docMgr.newWriteSet(); - } - count++; - } - if(count%BATCH_SIZE > 0){ - docMgr.write(writeset,transform); - } - - String uris[] = new String[102]; - for(int i =0;i<102;i++){ - uris[i]=DIRECTORY+"foo"+i+".xml"; - } - count=0; - DocumentPage page = docMgr.read(uris); - DOMHandle dh = new DOMHandle(); - while(page.hasNext()){ - DocumentRecord rec = page.next(); - rec.getContent(dh); - assertTrue("Element has attribure ? :",dh.get().getElementsByTagName("foo").item(0).hasAttributes()); - count++; - } - - assertEquals("document count", 102,count); - - } - @Test - public void testBulkReadWithXQueryTransform() throws Exception { - - TransformExtensionsManager transMgr = - client.newServerConfigManager().newTransformExtensionsManager(); - ExtensionMetadata metadata = new ExtensionMetadata(); - metadata.setTitle("Adding attribute xquery Transform"); - metadata.setDescription("This plugin transforms an XML document by adding attribute to root node"); - metadata.setProvider("MarkLogic"); - metadata.setVersion("0.1"); - // get the transform file - File transformFile = new File("src/test/java/com/marklogic/javaclient/transforms/add-attr-xquery-transform.xqy"); - FileHandle transformHandle = new FileHandle(transformFile); - transMgr.writeXQueryTransform("add-attr-xquery-transform", transformHandle, metadata); - ServerTransform transform = new ServerTransform("add-attr-xquery-transform"); - transform.put("name", "Lang"); - transform.put("value", "English"); - int count=1; - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - HashMap map= new HashMap(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - for(int i =0;i<102;i++){ - - writeset.add(DIRECTORY+"sec"+i+".xml", new DOMHandle(getDocumentContent("This is to read"+i))); - map.put(DIRECTORY+"sec"+i+".xml", convertXMLDocumentToString(getDocumentContent("This is to read"+i))); - if(count%BATCH_SIZE == 0){ - docMgr.write(writeset); - writeset = docMgr.newWriteSet(); - } - count++; - } - if(count%BATCH_SIZE > 0){ - docMgr.write(writeset); - } - - String uris[] = new String[102]; - for(int i =0;i<102;i++){ - uris[i]=DIRECTORY+"sec"+i+".xml"; - } - count=0; - DocumentPage page = docMgr.read(transform,uris); - DOMHandle dh = new DOMHandle(); - while(page.hasNext()){ - DocumentRecord rec = page.next(); - rec.getContent(dh); - assertTrue("Element has attribure ? :",dh.get().getElementsByTagName("foo").item(0).hasAttributes()); - count++; - } - - assertEquals("document count", 102,count); - - } - -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.File; +import java.util.HashMap; +import java.util.Scanner; + +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.stream.StreamSource; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.admin.ExtensionMetadata; +import com.marklogic.client.admin.TransformExtensionsManager; +import com.marklogic.client.document.DocumentPage; +import com.marklogic.client.document.DocumentRecord; +import com.marklogic.client.document.DocumentWriteSet; +import com.marklogic.client.document.ServerTransform; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.FileHandle; +import com.marklogic.client.io.SourceHandle; + +public class TestBulkWriteWithTransformations extends BasicJavaClientREST{ + private static final int BATCH_SIZE=100; + private static final String DIRECTORY ="/bulkTransform/"; + private static String dbName = "TestBulkWriteWithTransformDB"; + private static String [] fNames = {"TestBulkWriteWithTransformDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client ; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + setupAppServicesConstraint(dbName); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + System.out.println("In tear down" ); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } + + @Before + public void setUp() throws Exception { +// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + // create new connection for each test below + client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST); + } + + @After + public void tearDown() throws Exception { + System.out.println("Running clear script"); + // release client + client.release(); + } + + @Test + public void testBulkLoadWithXSLTClientSideTransform() throws Exception { + String docId[] ={"/transform/emp.xml","/transform/food1.xml","/transform/food2.xml"}; + Source s[] = new Source[3]; + s[0] = new StreamSource("src/test/java/com/marklogic/javaclient/data/employee.xml"); + s[1] = new StreamSource("src/test/java/com/marklogic/javaclient/data/xml-original.xml"); + s[2] = new StreamSource("src/test/java/com/marklogic/javaclient/data/xml-original-test.xml"); + // get the xslt + Source xsl = new StreamSource("src/test/java/com/marklogic/javaclient/data/employee-stylesheet.xsl"); + + // create transformer + TransformerFactory factory = TransformerFactory.newInstance(); + Transformer transformer = factory.newTransformer(xsl); + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + for(int i=0;i<3;i++){ + SourceHandle handle = new SourceHandle(); + handle.set(s[i]); + // set the transformer + handle.setTransformer(transformer); + writeset.add(docId[i],handle); + } + docMgr.write(writeset); + FileHandle dh = new FileHandle(); + // DOMHandle dh = new DOMHandle(); + docMgr.read(docId[0], dh); + Scanner scanner = new Scanner(dh.get()).useDelimiter("\\Z"); + String readContent = scanner.next(); + assertTrue("xml document contains firstname", readContent.contains("firstname")); + docMgr.read(docId[1], dh); + Scanner sc1 = new Scanner(dh.get()).useDelimiter("\\Z"); + readContent = sc1.next(); + assertTrue("xml document contains firstname", readContent.contains("firstname")); + docMgr.read(docId[2], dh); + Scanner sc2 = new Scanner(dh.get()).useDelimiter("\\Z"); + readContent = sc2.next(); + assertTrue("xml document contains firstname", readContent.contains("firstname")); + + } + @Test + public void testBulkLoadWithXQueryTransform() throws Exception { + + TransformExtensionsManager transMgr = + client.newServerConfigManager().newTransformExtensionsManager(); + ExtensionMetadata metadata = new ExtensionMetadata(); + metadata.setTitle("Adding attribute xquery Transform"); + metadata.setDescription("This plugin transforms an XML document by adding attribute to root node"); + metadata.setProvider("MarkLogic"); + metadata.setVersion("0.1"); + // get the transform file + File transformFile = new File("src/test/java/com/marklogic/javaclient/transforms/add-attr-xquery-transform.xqy"); + FileHandle transformHandle = new FileHandle(transformFile); + transMgr.writeXQueryTransform("add-attr-xquery-transform", transformHandle, metadata); + ServerTransform transform = new ServerTransform("add-attr-xquery-transform"); + transform.put("name", "Lang"); + transform.put("value", "English"); + int count=1; + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + HashMap map= new HashMap(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + for(int i =0;i<102;i++){ + + writeset.add(DIRECTORY+"foo"+i+".xml", new DOMHandle(getDocumentContent("This is so foo"+i))); + map.put(DIRECTORY+"foo"+i+".xml", convertXMLDocumentToString(getDocumentContent("This is so foo"+i))); + if(count%BATCH_SIZE == 0){ + docMgr.write(writeset,transform); + writeset = docMgr.newWriteSet(); + } + count++; + } + if(count%BATCH_SIZE > 0){ + docMgr.write(writeset,transform); + } + + String uris[] = new String[102]; + for(int i =0;i<102;i++){ + uris[i]=DIRECTORY+"foo"+i+".xml"; + } + count=0; + DocumentPage page = docMgr.read(uris); + DOMHandle dh = new DOMHandle(); + while(page.hasNext()){ + DocumentRecord rec = page.next(); + rec.getContent(dh); + assertTrue("Element has attribure ? :",dh.get().getElementsByTagName("foo").item(0).hasAttributes()); + count++; + } + + assertEquals("document count", 102,count); + + } + @Test + public void testBulkReadWithXQueryTransform() throws Exception { + + TransformExtensionsManager transMgr = + client.newServerConfigManager().newTransformExtensionsManager(); + ExtensionMetadata metadata = new ExtensionMetadata(); + metadata.setTitle("Adding attribute xquery Transform"); + metadata.setDescription("This plugin transforms an XML document by adding attribute to root node"); + metadata.setProvider("MarkLogic"); + metadata.setVersion("0.1"); + // get the transform file + File transformFile = new File("src/test/java/com/marklogic/javaclient/transforms/add-attr-xquery-transform.xqy"); + FileHandle transformHandle = new FileHandle(transformFile); + transMgr.writeXQueryTransform("add-attr-xquery-transform", transformHandle, metadata); + ServerTransform transform = new ServerTransform("add-attr-xquery-transform"); + transform.put("name", "Lang"); + transform.put("value", "English"); + int count=1; + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + HashMap map= new HashMap(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + for(int i =0;i<102;i++){ + + writeset.add(DIRECTORY+"sec"+i+".xml", new DOMHandle(getDocumentContent("This is to read"+i))); + map.put(DIRECTORY+"sec"+i+".xml", convertXMLDocumentToString(getDocumentContent("This is to read"+i))); + if(count%BATCH_SIZE == 0){ + docMgr.write(writeset); + writeset = docMgr.newWriteSet(); + } + count++; + } + if(count%BATCH_SIZE > 0){ + docMgr.write(writeset); + } + + String uris[] = new String[102]; + for(int i =0;i<102;i++){ + uris[i]=DIRECTORY+"sec"+i+".xml"; + } + count=0; + DocumentPage page = docMgr.read(transform,uris); + DOMHandle dh = new DOMHandle(); + while(page.hasNext()){ + DocumentRecord rec = page.next(); + rec.getContent(dh); + assertTrue("Element has attribure ? :",dh.get().getElementsByTagName("foo").item(0).hasAttributes()); + count++; + } + + assertEquals("document count", 102,count); + + } + +} From 69f7cf34eeee2d67747b26bccc3cafa59a88f880 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Wed, 20 Aug 2014 18:05:21 -0700 Subject: [PATCH 075/357] Code cleanup --- .../javaclient/TestBulkReadSample1.java | 659 ++++++++-------- .../javaclient/TestBulkSearchEWithQBE.java | 743 +++++++++--------- 2 files changed, 690 insertions(+), 712 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadSample1.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadSample1.java index 5ac725ae6..e4e1e2d76 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadSample1.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadSample1.java @@ -1,336 +1,325 @@ -package com.marklogic.javaclient; - -import java.io.File; - -import java.util.HashMap; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.document.DocumentRecord; -import com.marklogic.client.document.TextDocumentManager; -import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.document.BinaryDocumentManager; -import com.marklogic.client.document.JSONDocumentManager; -import com.marklogic.client.document.GenericDocumentManager; -import com.marklogic.client.document.DocumentManager; -import com.marklogic.client.document.DocumentPage; -import com.marklogic.client.document.DocumentRecord; -import com.marklogic.client.extra.jdom.*; -import com.marklogic.client.io.FileHandle; -import com.marklogic.client.io.Format; -import com.marklogic.client.io.JAXBHandle; -import com.marklogic.client.io.StringHandle; -import com.marklogic.client.io.DOMHandle; - -import com.marklogic.client.io.JacksonHandle; - - -import com.marklogic.client.document.DocumentWriteSet; -import com.marklogic.client.FailedRequestException; - - - -import javax.xml.transform.dom.DOMSource; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; - -import org.w3c.dom.Document; -import org.junit.*; - -import static org.junit.Assert.*; - -/* - * This test is designed to to test simple bulk reads with different types of Managers and different content type like JSON,text,binary,XMl by passing set of uris - * - * TextDocumentManager - * XMLDocumentManager - * BinaryDocumentManager - * JSONDocumentManager - * GenericDocumentManager - */ - - - -public class TestBulkReadSample1 extends BasicJavaClientREST { - - private static final int BATCH_SIZE=100; - private static final String DIRECTORY ="/bulkread/"; - private static String dbName = "TestBulkReadSampleDB"; - private static String [] fNames = {"TestBulkReadSampleDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort = 8011; - private DatabaseClient client ; - @BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); - setupAppServicesConstraint(dbName); - - } - - @Before public void testSetup() throws Exception - { - // create new connection for each test below - client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST); - } - @After - public void testCleanUp() throws Exception - { - System.out.println("Running CleanUp script"); - // release client - client.release(); - - - } - /* - * - * Use StringHandle to load 102 text documents using bulk write set. - * Test Bulk Read to see you can read all the documents? - */ - @Test - public void testReadMultipleTextDoc() - { - int count=1; - TextDocumentManager docMgr = client.newTextDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - - for(int i =0;i<102;i++){ - writeset.add(DIRECTORY+"foo"+i+".txt", new StringHandle().with("This is so foo"+i)); - if(count%BATCH_SIZE == 0){ - docMgr.write(writeset); - writeset = docMgr.newWriteSet(); - } - count++; - } - if(count%BATCH_SIZE > 0){ - docMgr.write(writeset); - } - String uris[] = new String[102]; - for(int i =0;i<102;i++){ - uris[i]=DIRECTORY+"foo"+i+".txt"; - } - count=0; - DocumentPage page = docMgr.read(uris); - while(page.hasNext()){ - DocumentRecord rec = page.next(); - validateRecord(rec,Format.TEXT); - count++; - } - assertEquals("document count", 102,count); - - } - /* - * This test uses DOMHandle to do bulk write 102 xml documents, and does a bulk read from database. - * Verified by reading individual documents - */ -@Test - public void testReadMultipleXMLDoc() throws Exception - { - int count=1; - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - HashMap map= new HashMap(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - for(int i =0;i<102;i++){ - - writeset.add(DIRECTORY+"foo"+i+".xml", new DOMHandle(getDocumentContent("This is so foo"+i))); - map.put(DIRECTORY+"foo"+i+".xml", convertXMLDocumentToString(getDocumentContent("This is so foo"+i))); - if(count%BATCH_SIZE == 0){ - docMgr.write(writeset); - writeset = docMgr.newWriteSet(); - } - count++; - } - if(count%BATCH_SIZE > 0){ - docMgr.write(writeset); - } - String uris[] = new String[102]; - for(int i =0;i<102;i++){ - uris[i]=DIRECTORY+"foo"+i+".xml"; - } - count=0; - DocumentPage page = docMgr.read(uris); - DOMHandle dh = new DOMHandle(); - while(page.hasNext()){ - DocumentRecord rec = page.next(); - validateRecord(rec,Format.XML); - rec.getContent(dh); - assertEquals("Comparing the content :",map.get(rec.getUri()),convertXMLDocumentToString(dh.get())); - count++; - } - - assertEquals("document count", 102,count); - - } - /* - * This test uses FileHandle to bulkload 102 binary documents,test bulk read from database. - * - */ - @Test - public void testReadMultipleBinaryDoc() throws Exception - { - String docId[] = {"Sega-4MB.jpg"}; - int count=1; - BinaryDocumentManager docMgr = client.newBinaryDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - File file1= null; - file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); - FileHandle h1 = new FileHandle(file1); - for(int i =0;i<102;i++){ - writeset.add(DIRECTORY+"binary"+i+".jpg", h1); - if(count%BATCH_SIZE == 0){ - docMgr.write(writeset); - writeset = docMgr.newWriteSet(); - } - count++; - } - if(count%BATCH_SIZE > 0){ - docMgr.write(writeset); - } - String uris[] = new String[102]; - for(int i =0;i<102;i++){ - uris[i]=DIRECTORY+"binary"+i+".jpg"; - } - count=0; - FileHandle rh = new FileHandle(); - DocumentPage page = docMgr.read(uris); - while(page.hasNext()){ - DocumentRecord rec = page.next(); - validateRecord(rec,Format.BINARY); - rec.getContent(rh); - assertEquals("Content length :",file1.length(),rh.get().length()); - count++; - } - assertEquals("document count", 102,count); - //Testing the multiple same uris will not read multiple records - for(int i =0;i<102;i++){ - uris[i]=DIRECTORY+"binary"+12+".jpg"; - } - count=0; - page = docMgr.read(uris); - while(page.hasNext()){ - DocumentRecord rec = page.next(); - validateRecord(rec,Format.BINARY); - count++; - } - assertEquals("document count", 1,count); - } - -/* - * Load 102 JSON documents using JacksonHandle, do a bulk read. - * Verify by reading individual documents - * This test has a bug logged in github with tracking Issue#33 - */ - @Test - public void testWriteMultipleJSONDocs() throws Exception - { - int count=1; - JSONDocumentManager docMgr = client.newJSONDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - - HashMap map= new HashMap(); - - for(int i =0;i<102;i++){ - JsonNode jn = new ObjectMapper().readTree("{\"animal"+i+"\":\"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); - } - String uris[] = new String[102]; - for(int i =0;i<102;i++){ - uris[i]=DIRECTORY+"dog"+i+".json"; - } - count=0; - DocumentPage page = docMgr.read(uris); - DocumentRecord rec; - JacksonHandle jh = new JacksonHandle(); - while(page.hasNext()){ - rec = page.next(); - validateRecord(rec,Format.JSON); - rec.getContent(jh); - assertEquals("Comparing the content :",map.get(rec.getUri()),jh.get().toString()); - count++; - } -// validateRecord(rec,Format.JSON); - assertEquals("document count", 102,count); - - - } -/* - * This test uses GenericManager to load all different document types - * This test has a bug logged in github with tracking Issue#33 - * - */ -@Test - public void testWriteGenericDocMgr() throws Exception - { - - GenericDocumentManager docMgr = client.newDocumentManager(); - int countXML=0,countJson=0,countJpg=0,countTEXT=0; - String uris[] = new String[102]; - for(int i =0;i<99;){ - uris[i]=DIRECTORY+"foo"+i+".xml"; - i++; - uris[i]=DIRECTORY+"foo"+i+".txt"; - i++; - uris[i]=DIRECTORY+"binary"+i+".jpg"; - i++; - uris[i]=DIRECTORY+"dog"+i+".json"; - i++; - } -// for(String uri:uris){System.out.println(uri);} - DocumentPage page = docMgr.read(uris); - if(!page.hasNext()){ - testReadMultipleTextDoc(); - testReadMultipleXMLDoc(); - testReadMultipleBinaryDoc(); - testWriteMultipleJSONDocs(); - page = docMgr.read(uris); - } - while(page.hasNext()){ - DocumentRecord rec = page.next(); - switch(rec.getFormat()) - { - case XML: countXML++; break; - case TEXT: countTEXT++;break; - case JSON: countJson++; break; - case BINARY: countJpg++; break; - default : - break; - } - validateRecord(rec,rec.getFormat()); - } - System.out.println("xml :"+countXML+"TXT :"+countTEXT+" json :"+countJpg+" "+countJson); - assertEquals("xml document count", 25,countXML); - assertEquals("text document count", 25,countTEXT); - assertEquals("binary document count", 25,countJpg); - assertEquals("Json document count", 25,countJson); - } - @AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down" ); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } - - public void validateRecord(DocumentRecord record,Format type) { - - assertNotNull("DocumentRecord should never be null", record); - assertNotNull("Document uri should never be null", record.getUri()); - assertTrue("Document uri should start with " + DIRECTORY, record.getUri().startsWith(DIRECTORY)); - assertEquals("All records are expected to be in same format", type, record.getFormat()); - - } +package com.marklogic.javaclient; + +import java.io.File; + +import java.util.HashMap; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.DocumentRecord; +import com.marklogic.client.document.TextDocumentManager; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.document.BinaryDocumentManager; +import com.marklogic.client.document.JSONDocumentManager; +import com.marklogic.client.document.GenericDocumentManager; +import com.marklogic.client.document.DocumentPage; +import com.marklogic.client.io.FileHandle; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.StringHandle; +import com.marklogic.client.io.DOMHandle; + +import com.marklogic.client.io.JacksonHandle; + + +import com.marklogic.client.document.DocumentWriteSet; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +import org.junit.*; + +import static org.junit.Assert.*; + +/* + * This test is designed to to test simple bulk reads with different types of Managers and different content type like JSON,text,binary,XMl by passing set of uris + * + * TextDocumentManager + * XMLDocumentManager + * BinaryDocumentManager + * JSONDocumentManager + * GenericDocumentManager + */ + + + +public class TestBulkReadSample1 extends BasicJavaClientREST { + + private static final int BATCH_SIZE=100; + private static final String DIRECTORY ="/bulkread/"; + private static String dbName = "TestBulkReadSampleDB"; + private static String [] fNames = {"TestBulkReadSampleDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client ; + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + setupAppServicesConstraint(dbName); + + } + + @Before public void testSetup() throws Exception + { + // create new connection for each test below + client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST); + } + @After + public void testCleanUp() throws Exception + { + System.out.println("Running CleanUp script"); + // release client + client.release(); + + + } + /* + * + * Use StringHandle to load 102 text documents using bulk write set. + * Test Bulk Read to see you can read all the documents? + */ + @Test + public void testReadMultipleTextDoc() + { + int count=1; + TextDocumentManager docMgr = client.newTextDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + + for(int i =0;i<102;i++){ + writeset.add(DIRECTORY+"foo"+i+".txt", new StringHandle().with("This is so foo"+i)); + if(count%BATCH_SIZE == 0){ + docMgr.write(writeset); + writeset = docMgr.newWriteSet(); + } + count++; + } + if(count%BATCH_SIZE > 0){ + docMgr.write(writeset); + } + String uris[] = new String[102]; + for(int i =0;i<102;i++){ + uris[i]=DIRECTORY+"foo"+i+".txt"; + } + count=0; + DocumentPage page = docMgr.read(uris); + while(page.hasNext()){ + DocumentRecord rec = page.next(); + validateRecord(rec,Format.TEXT); + count++; + } + assertEquals("document count", 102,count); + + } + /* + * This test uses DOMHandle to do bulk write 102 xml documents, and does a bulk read from database. + * Verified by reading individual documents + */ +@Test + public void testReadMultipleXMLDoc() throws Exception + { + int count=1; + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + HashMap map= new HashMap(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + for(int i =0;i<102;i++){ + + writeset.add(DIRECTORY+"foo"+i+".xml", new DOMHandle(getDocumentContent("This is so foo"+i))); + map.put(DIRECTORY+"foo"+i+".xml", convertXMLDocumentToString(getDocumentContent("This is so foo"+i))); + if(count%BATCH_SIZE == 0){ + docMgr.write(writeset); + writeset = docMgr.newWriteSet(); + } + count++; + } + if(count%BATCH_SIZE > 0){ + docMgr.write(writeset); + } + String uris[] = new String[102]; + for(int i =0;i<102;i++){ + uris[i]=DIRECTORY+"foo"+i+".xml"; + } + count=0; + DocumentPage page = docMgr.read(uris); + DOMHandle dh = new DOMHandle(); + while(page.hasNext()){ + DocumentRecord rec = page.next(); + validateRecord(rec,Format.XML); + rec.getContent(dh); + assertEquals("Comparing the content :",map.get(rec.getUri()),convertXMLDocumentToString(dh.get())); + count++; + } + + assertEquals("document count", 102,count); + + } + /* + * This test uses FileHandle to bulkload 102 binary documents,test bulk read from database. + * + */ + @Test + public void testReadMultipleBinaryDoc() throws Exception + { + String docId[] = {"Sega-4MB.jpg"}; + int count=1; + BinaryDocumentManager docMgr = client.newBinaryDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + File file1= null; + file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); + FileHandle h1 = new FileHandle(file1); + for(int i =0;i<102;i++){ + writeset.add(DIRECTORY+"binary"+i+".jpg", h1); + if(count%BATCH_SIZE == 0){ + docMgr.write(writeset); + writeset = docMgr.newWriteSet(); + } + count++; + } + if(count%BATCH_SIZE > 0){ + docMgr.write(writeset); + } + String uris[] = new String[102]; + for(int i =0;i<102;i++){ + uris[i]=DIRECTORY+"binary"+i+".jpg"; + } + count=0; + FileHandle rh = new FileHandle(); + DocumentPage page = docMgr.read(uris); + while(page.hasNext()){ + DocumentRecord rec = page.next(); + validateRecord(rec,Format.BINARY); + rec.getContent(rh); + assertEquals("Content length :",file1.length(),rh.get().length()); + count++; + } + assertEquals("document count", 102,count); + //Testing the multiple same uris will not read multiple records + for(int i =0;i<102;i++){ + uris[i]=DIRECTORY+"binary"+12+".jpg"; + } + count=0; + page = docMgr.read(uris); + while(page.hasNext()){ + DocumentRecord rec = page.next(); + validateRecord(rec,Format.BINARY); + count++; + } + assertEquals("document count", 1,count); + } + +/* + * Load 102 JSON documents using JacksonHandle, do a bulk read. + * Verify by reading individual documents + * This test has a bug logged in github with tracking Issue#33 + */ + @Test + public void testWriteMultipleJSONDocs() throws Exception + { + int count=1; + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + + HashMap map= new HashMap(); + + for(int i =0;i<102;i++){ + JsonNode jn = new ObjectMapper().readTree("{\"animal"+i+"\":\"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); + } + String uris[] = new String[102]; + for(int i =0;i<102;i++){ + uris[i]=DIRECTORY+"dog"+i+".json"; + } + count=0; + DocumentPage page = docMgr.read(uris); + DocumentRecord rec; + JacksonHandle jh = new JacksonHandle(); + while(page.hasNext()){ + rec = page.next(); + validateRecord(rec,Format.JSON); + rec.getContent(jh); + assertEquals("Comparing the content :",map.get(rec.getUri()),jh.get().toString()); + count++; + } +// validateRecord(rec,Format.JSON); + assertEquals("document count", 102,count); + + + } +/* + * This test uses GenericManager to load all different document types + * This test has a bug logged in github with tracking Issue#33 + * + */ +@Test + public void testWriteGenericDocMgr() throws Exception + { + + GenericDocumentManager docMgr = client.newDocumentManager(); + int countXML=0,countJson=0,countJpg=0,countTEXT=0; + String uris[] = new String[102]; + for(int i =0;i<99;){ + uris[i]=DIRECTORY+"foo"+i+".xml"; + i++; + uris[i]=DIRECTORY+"foo"+i+".txt"; + i++; + uris[i]=DIRECTORY+"binary"+i+".jpg"; + i++; + uris[i]=DIRECTORY+"dog"+i+".json"; + i++; + } +// for(String uri:uris){System.out.println(uri);} + DocumentPage page = docMgr.read(uris); + if(!page.hasNext()){ + testReadMultipleTextDoc(); + testReadMultipleXMLDoc(); + testReadMultipleBinaryDoc(); + testWriteMultipleJSONDocs(); + page = docMgr.read(uris); + } + while(page.hasNext()){ + DocumentRecord rec = page.next(); + switch(rec.getFormat()) + { + case XML: countXML++; break; + case TEXT: countTEXT++;break; + case JSON: countJson++; break; + case BINARY: countJpg++; break; + default : + break; + } + validateRecord(rec,rec.getFormat()); + } + System.out.println("xml :"+countXML+"TXT :"+countTEXT+" json :"+countJpg+" "+countJson); + assertEquals("xml document count", 25,countXML); + assertEquals("text document count", 25,countTEXT); + assertEquals("binary document count", 25,countJpg); + assertEquals("Json document count", 25,countJson); + } + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down" ); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } + + public void validateRecord(DocumentRecord record,Format type) { + + assertNotNull("DocumentRecord should never be null", record); + assertNotNull("Document uri should never be null", record.getUri()); + assertTrue("Document uri should start with " + DIRECTORY, record.getUri().startsWith(DIRECTORY)); + assertEquals("All records are expected to be in same format", type, record.getFormat()); + + } } \ No newline at end of file diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchEWithQBE.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchEWithQBE.java index d8b9d347c..0a9309a82 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchEWithQBE.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchEWithQBE.java @@ -1,377 +1,366 @@ -package com.marklogic.javaclient; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; -import static org.junit.Assert.*; - -import java.io.IOException; -import java.util.HashMap; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.xml.sax.SAXException; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.document.DocumentPage; -import com.marklogic.client.document.DocumentRecord; -import com.marklogic.client.document.DocumentWriteSet; -import com.marklogic.client.document.JSONDocumentManager; -import com.marklogic.client.document.TextDocumentManager; -import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.Format; -import com.marklogic.client.io.JacksonHandle; -import com.marklogic.client.io.SearchHandle; -import com.marklogic.client.io.StringHandle; -import com.marklogic.client.io.DocumentMetadataHandle.Capability; -import com.marklogic.client.query.MatchDocumentSummary; -import com.marklogic.client.query.MatchLocation; -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.query.RawCombinedQueryDefinition; -import com.marklogic.client.query.RawQueryByExampleDefinition; -import com.marklogic.client.query.StringQueryDefinition; -import com.marklogic.client.query.QueryManager.QueryView; - - - - - - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; - -public class TestBulkSearchEWithQBE extends BasicJavaClientREST{ - private static final int BATCH_SIZE=100; - private static final String DIRECTORY ="/bulkSearch/"; - private static String dbName = "TestBulkSearchQBEDB"; - private static String [] fNames = {"TestBulkSearchQBEDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort = 8011; - private DatabaseClient client ; - - @BeforeClass - public static void setUpBeforeClass() throws Exception { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); - setupAppServicesConstraint(dbName); - createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); - } - - @AfterClass - public static void tearDownAfterClass() throws Exception { - System.out.println("In tear down" ); - tearDownJavaRESTServer(dbName, fNames, restServerName); - deleteRESTUser("usr1"); - } - - @Before - public void setUp() throws Exception { -// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); - // create new connection for each test below - client = DatabaseClientFactory.newClient("localhost", restPort, "usr1", "password", Authentication.DIGEST); - loadTxtDocuments(); - loadXMLDocuments(); - loadJSONDocuments(); - } - - @After - public void tearDown() throws Exception { - System.out.println("Running clear script"); - // release client - client.release(); - } - - public void validateRecord(DocumentRecord record,Format type) { - - assertNotNull("DocumentRecord should never be null", record); - assertNotNull("Document uri should never be null", record.getUri()); - assertTrue("Document uri should start with " + DIRECTORY, record.getUri().startsWith(DIRECTORY)); - assertEquals("All records are expected to be in same format", type, record.getFormat()); - // System.out.println(record.getMimetype()); - - } - public void loadXMLDocuments() throws IOException, ParserConfigurationException, SAXException, TransformerException{ - int count=1; - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - for(int i =0;i<102;i++){ - Document doc = this.getDocumentContent("This is so foo with a bar "+i); - Element childElement = doc.createElement("author"); - childElement.appendChild(doc.createTextNode("rhiea")); - doc.getElementsByTagName("foo").item(0).appendChild(childElement); - writeset.add(DIRECTORY+"foo"+i+".xml", new DOMHandle(doc)); - - if(count%BATCH_SIZE == 0){ - docMgr.write(writeset); - writeset = docMgr.newWriteSet(); - } - count++; - } - if(count%BATCH_SIZE > 0){ - docMgr.write(writeset); - } - } - public void loadTxtDocuments(){ - int count =1; - TextDocumentManager docMgr = client.newTextDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - for(int i =0;i<101;i++){ - writeset.add(DIRECTORY+"Textfoo"+i+".txt", new StringHandle().with("bar can be foo"+i)); - if(count%BATCH_SIZE == 0){ - docMgr.write(writeset); - writeset = docMgr.newWriteSet(); - } - count++; - } - if(count%BATCH_SIZE > 0){ - docMgr.write(writeset); - } - } - public void loadJSONDocuments() throws JsonProcessingException, IOException{ - int count=1; - JSONDocumentManager docMgr = client.newJSONDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - - HashMap 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 testBulkSearchQBEWithXMLResponseFormat() throws IOException, ParserConfigurationException, SAXException, TransformerException, XpathException { - int count; - //Creating a xml document manager for bulk search - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - //using QBE for query definition and set the search criteria - - QueryManager queryMgr = client.newQueryManager(); - String queryAsString = - "foo"; - RawQueryByExampleDefinition qd = queryMgr.newRawQueryByExampleDefinition(new StringHandle(queryAsString)); - - // set document manager level settings for search response - docMgr.setPageLength(25); - docMgr.setSearchView(QueryView.RESULTS); - docMgr.setResponseFormat(Format.XML); - - // Search for documents where content has bar and get first result record, get search handle on it,Use DOMHandle to read results - DOMHandle dh = new DOMHandle(); - DocumentPage page; - - long pageNo=1; - do{ - count=0; - page = docMgr.search(qd, pageNo,dh); - if(pageNo >1){ - assertFalse("Is this first Page", page.isFirstPage()); - assertTrue("Is page has previous page ?",page.hasPreviousPage()); - } - while(page.hasNext()){ - DocumentRecord rec = page.next(); - rec.getFormat(); - validateRecord(rec,Format.XML); - - count++; - } - - Document resultDoc = dh.get(); - assertXpathEvaluatesTo("xml", "string(//*[local-name()='result'][last()]//@*[local-name()='format'])", resultDoc); - assertEquals("document count", page.size(),count); - // assertEquals("Page Number #",pageNo,page.getPageNumber()); - pageNo = pageNo + page.getPageSize(); - }while(!page.isLastPage() && page.hasContent() ); - assertEquals("page count is 5 ",5, page.getTotalPages()); - assertTrue("Page has previous page ?",page.hasPreviousPage()); - assertEquals("page size", 25,page.getPageSize()); - assertEquals("document count", 102,page.getTotalSize()); - - } - @Test - public void testBulkSearchQBEWithJSONResponseFormat() throws IOException, ParserConfigurationException, SAXException, TransformerException { - int count; - - //Creating a xml document manager for bulk search - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - //using QBE for query definition and set the search criteria - - QueryManager queryMgr = client.newQueryManager(); - String queryAsString = "{\"$query\": { \"says\": {\"$word\":\"woof\",\"$exact\": false}}}"; - RawQueryByExampleDefinition qd = queryMgr.newRawQueryByExampleDefinition(new StringHandle(queryAsString).withFormat(Format.JSON)); - - // set document manager level settings for search response - docMgr.setPageLength(25); - docMgr.setSearchView(QueryView.RESULTS); - docMgr.setResponseFormat(Format.JSON); - - // Search for documents where content has bar and get first result record, get search handle on it,Use DOMHandle to read results - JacksonHandle sh = new JacksonHandle(); - DocumentPage page; - - long pageNo=1; - do{ - count=0; - page = docMgr.search(qd, pageNo,sh); - if(pageNo >1){ - assertFalse("Is this first Page", page.isFirstPage()); - assertTrue("Is page has previous page ?",page.hasPreviousPage()); - } - while(page.hasNext()){ - DocumentRecord rec = page.next(); - rec.getFormat(); - validateRecord(rec,Format.JSON); - System.out.println(rec.getContent(new StringHandle()).get().toString()); - count++; - } - assertTrue("Page start in results and on page",sh.get().get("start").asLong() == page.getStart()); - assertEquals("document count", page.size(),count); - // assertEquals("Page Number #",pageNo,page.getPageNumber()); - pageNo = pageNo + page.getPageSize(); - }while(!page.isLastPage() && page.hasContent() ); - - assertEquals("page count is ",5,page.getTotalPages()); - assertTrue("Page has previous page ?",page.hasPreviousPage()); - assertEquals("page size", 25,page.getPageSize()); - assertEquals("document count", 102,page.getTotalSize()); - - } - @Test - public void testBulkSearchQBECombinedQuery() throws IOException, ParserConfigurationException, SAXException, TransformerException, XpathException { - int count; - - //Creating a xml document manager for bulk search - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - //using QBE for query definition and set the search criteria - - QueryManager queryMgr = client.newQueryManager(); - - String queryAsString = ""+ - ""+ - ""+ - "bar"+ - ""+ - ""+ - "authorName"+ - "rhiea"+ - ""+ - ""+ - ""+ - ""+ - ""+ - ""+ - ""+ - ""+ - ""+ - ""; - RawCombinedQueryDefinition qd = queryMgr.newRawCombinedQueryDefinition(new StringHandle(queryAsString).withFormat(Format.XML)); - - // set document manager level settings for search response - docMgr.setPageLength(25); - docMgr.setSearchView(QueryView.RESULTS); - - // Search for documents where content has bar and get first result record, get search handle on it,Use DOMHandle to read results - DOMHandle dh = new DOMHandle(); - DocumentPage page; - - long pageNo=1; - do{ - count=0; - page = docMgr.search(qd, pageNo,dh); - if(pageNo >1){ - assertFalse("Is this first Page", page.isFirstPage()); - assertTrue("Is page has previous page ?",page.hasPreviousPage()); - } - while(page.hasNext()){ - DocumentRecord rec = page.next(); - validateRecord(rec,Format.XML); - count++; - } - Document resultDoc = dh.get(); - assertXpathEvaluatesTo("xml", "string(//*[local-name()='result'][last()]//@*[local-name()='format'])", resultDoc); - assertEquals("document count", page.size(),count); - // assertEquals("Page Number #",pageNo,page.getPageNumber()); - pageNo = pageNo + page.getPageSize(); - }while(!page.isLastPage() && page.hasContent() ); - - assertEquals("page count is ",5,page.getTotalPages()); - assertTrue("Page has previous page ?",page.hasPreviousPage()); - assertEquals("page size", 25,page.getPageSize()); - assertEquals("document count", 102,page.getTotalSize()); - - } - @Test - public void testBulkSearchQBEWithJSONCombinedQuery() throws IOException, ParserConfigurationException, SAXException, TransformerException { - int count; - - //Creating a xml document manager for bulk search - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - //using QBE for query definition and set the search criteria - - QueryManager queryMgr = client.newQueryManager(); - String queryAsString ="{\"search\":{\"query\":{\"value-constraint-query\":{\"constraint-name\":\"animal\", \"text\":\"woof\"}}, \"options\":{\"constraint\":{\"name\":\"animal\", \"value\":{\"json-property\":\"says\"}}}}}"; - - RawCombinedQueryDefinition qd = queryMgr.newRawCombinedQueryDefinition(new StringHandle(queryAsString).withFormat(Format.JSON)); - // set document manager level settings for search response - docMgr.setPageLength(25); - docMgr.setSearchView(QueryView.RESULTS); - docMgr.setResponseFormat(Format.JSON); - - // Search for documents where content has woof and get first result record, - JacksonHandle sh = new JacksonHandle(); - queryMgr.search(qd, sh); - System.out.println(sh.get().toString()); - DocumentPage page; - - long pageNo=1; - do{ - count=0; - page = docMgr.search(qd, pageNo,sh); - if(pageNo >1){ - assertFalse("Is this first Page", page.isFirstPage()); - assertTrue("Is page has previous page ?",page.hasPreviousPage()); - } - while(page.hasNext()){ - DocumentRecord rec = page.next(); - rec.getFormat(); - validateRecord(rec,Format.JSON); - count++; - } - assertTrue("Page start in results and on page",sh.get().get("start").asLong() == page.getStart()); - assertEquals("document count", page.size(),count); - pageNo = pageNo + page.getPageSize(); - }while(!page.isLastPage() && page.hasContent() ); - System.out.println(sh.get().toString()); - assertEquals("page count is ",5,page.getTotalPages()); - assertTrue("Page has previous page ?",page.hasPreviousPage()); - assertEquals("page size", 25,page.getPageSize()); - assertEquals("document count", 102,page.getTotalSize()); - - } - -} +package com.marklogic.javaclient; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; +import static org.junit.Assert.*; + +import java.io.IOException; +import java.util.HashMap; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.xml.sax.SAXException; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.DocumentPage; +import com.marklogic.client.document.DocumentRecord; +import com.marklogic.client.document.DocumentWriteSet; +import com.marklogic.client.document.JSONDocumentManager; +import com.marklogic.client.document.TextDocumentManager; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.JacksonHandle; +import com.marklogic.client.io.StringHandle; +import com.marklogic.client.io.DocumentMetadataHandle.Capability; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.RawCombinedQueryDefinition; +import com.marklogic.client.query.RawQueryByExampleDefinition; +import com.marklogic.client.query.QueryManager.QueryView; + +public class TestBulkSearchEWithQBE extends BasicJavaClientREST{ + private static final int BATCH_SIZE=100; + private static final String DIRECTORY ="/bulkSearch/"; + private static String dbName = "TestBulkSearchQBEDB"; + private static String [] fNames = {"TestBulkSearchQBEDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client ; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + setupAppServicesConstraint(dbName); + createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + System.out.println("In tear down" ); + tearDownJavaRESTServer(dbName, fNames, restServerName); + deleteRESTUser("usr1"); + } + + @Before + public void setUp() throws Exception { +// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + // create new connection for each test below + client = DatabaseClientFactory.newClient("localhost", restPort, "usr1", "password", Authentication.DIGEST); + loadTxtDocuments(); + loadXMLDocuments(); + loadJSONDocuments(); + } + + @After + public void tearDown() throws Exception { + System.out.println("Running clear script"); + // release client + client.release(); + } + + public void validateRecord(DocumentRecord record,Format type) { + + assertNotNull("DocumentRecord should never be null", record); + assertNotNull("Document uri should never be null", record.getUri()); + assertTrue("Document uri should start with " + DIRECTORY, record.getUri().startsWith(DIRECTORY)); + assertEquals("All records are expected to be in same format", type, record.getFormat()); + // System.out.println(record.getMimetype()); + + } + public void loadXMLDocuments() throws IOException, ParserConfigurationException, SAXException, TransformerException{ + int count=1; + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + for(int i =0;i<102;i++){ + Document doc = this.getDocumentContent("This is so foo with a bar "+i); + Element childElement = doc.createElement("author"); + childElement.appendChild(doc.createTextNode("rhiea")); + doc.getElementsByTagName("foo").item(0).appendChild(childElement); + writeset.add(DIRECTORY+"foo"+i+".xml", new DOMHandle(doc)); + + if(count%BATCH_SIZE == 0){ + docMgr.write(writeset); + writeset = docMgr.newWriteSet(); + } + count++; + } + if(count%BATCH_SIZE > 0){ + docMgr.write(writeset); + } + } + public void loadTxtDocuments(){ + int count =1; + TextDocumentManager docMgr = client.newTextDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + for(int i =0;i<101;i++){ + writeset.add(DIRECTORY+"Textfoo"+i+".txt", new StringHandle().with("bar can be foo"+i)); + if(count%BATCH_SIZE == 0){ + docMgr.write(writeset); + writeset = docMgr.newWriteSet(); + } + count++; + } + if(count%BATCH_SIZE > 0){ + docMgr.write(writeset); + } + } + public void loadJSONDocuments() throws JsonProcessingException, IOException{ + int count=1; + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + + HashMap 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 testBulkSearchQBEWithXMLResponseFormat() throws IOException, ParserConfigurationException, SAXException, TransformerException, XpathException { + int count; + //Creating a xml document manager for bulk search + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + //using QBE for query definition and set the search criteria + + QueryManager queryMgr = client.newQueryManager(); + String queryAsString = + "foo"; + RawQueryByExampleDefinition qd = queryMgr.newRawQueryByExampleDefinition(new StringHandle(queryAsString)); + + // set document manager level settings for search response + docMgr.setPageLength(25); + docMgr.setSearchView(QueryView.RESULTS); + docMgr.setResponseFormat(Format.XML); + + // Search for documents where content has bar and get first result record, get search handle on it,Use DOMHandle to read results + DOMHandle dh = new DOMHandle(); + DocumentPage page; + + long pageNo=1; + do{ + count=0; + page = docMgr.search(qd, pageNo,dh); + if(pageNo >1){ + assertFalse("Is this first Page", page.isFirstPage()); + assertTrue("Is page has previous page ?",page.hasPreviousPage()); + } + while(page.hasNext()){ + DocumentRecord rec = page.next(); + rec.getFormat(); + validateRecord(rec,Format.XML); + + count++; + } + + Document resultDoc = dh.get(); + assertXpathEvaluatesTo("xml", "string(//*[local-name()='result'][last()]//@*[local-name()='format'])", resultDoc); + assertEquals("document count", page.size(),count); + // assertEquals("Page Number #",pageNo,page.getPageNumber()); + pageNo = pageNo + page.getPageSize(); + }while(!page.isLastPage() && page.hasContent() ); + assertEquals("page count is 5 ",5, page.getTotalPages()); + assertTrue("Page has previous page ?",page.hasPreviousPage()); + assertEquals("page size", 25,page.getPageSize()); + assertEquals("document count", 102,page.getTotalSize()); + + } + @Test + public void testBulkSearchQBEWithJSONResponseFormat() throws IOException, ParserConfigurationException, SAXException, TransformerException { + int count; + + //Creating a xml document manager for bulk search + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + //using QBE for query definition and set the search criteria + + QueryManager queryMgr = client.newQueryManager(); + String queryAsString = "{\"$query\": { \"says\": {\"$word\":\"woof\",\"$exact\": false}}}"; + RawQueryByExampleDefinition qd = queryMgr.newRawQueryByExampleDefinition(new StringHandle(queryAsString).withFormat(Format.JSON)); + + // set document manager level settings for search response + docMgr.setPageLength(25); + docMgr.setSearchView(QueryView.RESULTS); + docMgr.setResponseFormat(Format.JSON); + + // Search for documents where content has bar and get first result record, get search handle on it,Use DOMHandle to read results + JacksonHandle sh = new JacksonHandle(); + DocumentPage page; + + long pageNo=1; + do{ + count=0; + page = docMgr.search(qd, pageNo,sh); + if(pageNo >1){ + assertFalse("Is this first Page", page.isFirstPage()); + assertTrue("Is page has previous page ?",page.hasPreviousPage()); + } + while(page.hasNext()){ + DocumentRecord rec = page.next(); + rec.getFormat(); + validateRecord(rec,Format.JSON); + System.out.println(rec.getContent(new StringHandle()).get().toString()); + count++; + } + assertTrue("Page start in results and on page",sh.get().get("start").asLong() == page.getStart()); + assertEquals("document count", page.size(),count); + // assertEquals("Page Number #",pageNo,page.getPageNumber()); + pageNo = pageNo + page.getPageSize(); + }while(!page.isLastPage() && page.hasContent() ); + + assertEquals("page count is ",5,page.getTotalPages()); + assertTrue("Page has previous page ?",page.hasPreviousPage()); + assertEquals("page size", 25,page.getPageSize()); + assertEquals("document count", 102,page.getTotalSize()); + + } + @Test + public void testBulkSearchQBECombinedQuery() throws IOException, ParserConfigurationException, SAXException, TransformerException, XpathException { + int count; + + //Creating a xml document manager for bulk search + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + //using QBE for query definition and set the search criteria + + QueryManager queryMgr = client.newQueryManager(); + + String queryAsString = ""+ + ""+ + ""+ + "bar"+ + ""+ + ""+ + "authorName"+ + "rhiea"+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""; + RawCombinedQueryDefinition qd = queryMgr.newRawCombinedQueryDefinition(new StringHandle(queryAsString).withFormat(Format.XML)); + + // set document manager level settings for search response + docMgr.setPageLength(25); + docMgr.setSearchView(QueryView.RESULTS); + + // Search for documents where content has bar and get first result record, get search handle on it,Use DOMHandle to read results + DOMHandle dh = new DOMHandle(); + DocumentPage page; + + long pageNo=1; + do{ + count=0; + page = docMgr.search(qd, pageNo,dh); + if(pageNo >1){ + assertFalse("Is this first Page", page.isFirstPage()); + assertTrue("Is page has previous page ?",page.hasPreviousPage()); + } + while(page.hasNext()){ + DocumentRecord rec = page.next(); + validateRecord(rec,Format.XML); + count++; + } + Document resultDoc = dh.get(); + assertXpathEvaluatesTo("xml", "string(//*[local-name()='result'][last()]//@*[local-name()='format'])", resultDoc); + assertEquals("document count", page.size(),count); + // assertEquals("Page Number #",pageNo,page.getPageNumber()); + pageNo = pageNo + page.getPageSize(); + }while(!page.isLastPage() && page.hasContent() ); + + assertEquals("page count is ",5,page.getTotalPages()); + assertTrue("Page has previous page ?",page.hasPreviousPage()); + assertEquals("page size", 25,page.getPageSize()); + assertEquals("document count", 102,page.getTotalSize()); + + } + @Test + public void testBulkSearchQBEWithJSONCombinedQuery() throws IOException, ParserConfigurationException, SAXException, TransformerException { + int count; + + //Creating a xml document manager for bulk search + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + //using QBE for query definition and set the search criteria + + QueryManager queryMgr = client.newQueryManager(); + String queryAsString ="{\"search\":{\"query\":{\"value-constraint-query\":{\"constraint-name\":\"animal\", \"text\":\"woof\"}}, \"options\":{\"constraint\":{\"name\":\"animal\", \"value\":{\"json-property\":\"says\"}}}}}"; + + RawCombinedQueryDefinition qd = queryMgr.newRawCombinedQueryDefinition(new StringHandle(queryAsString).withFormat(Format.JSON)); + // set document manager level settings for search response + docMgr.setPageLength(25); + docMgr.setSearchView(QueryView.RESULTS); + docMgr.setResponseFormat(Format.JSON); + + // Search for documents where content has woof and get first result record, + JacksonHandle sh = new JacksonHandle(); + queryMgr.search(qd, sh); + System.out.println(sh.get().toString()); + DocumentPage page; + + long pageNo=1; + do{ + count=0; + page = docMgr.search(qd, pageNo,sh); + if(pageNo >1){ + assertFalse("Is this first Page", page.isFirstPage()); + assertTrue("Is page has previous page ?",page.hasPreviousPage()); + } + while(page.hasNext()){ + DocumentRecord rec = page.next(); + rec.getFormat(); + validateRecord(rec,Format.JSON); + count++; + } + assertTrue("Page start in results and on page",sh.get().get("start").asLong() == page.getStart()); + assertEquals("document count", page.size(),count); + pageNo = pageNo + page.getPageSize(); + }while(!page.isLastPage() && page.hasContent() ); + System.out.println(sh.get().toString()); + assertEquals("page count is ",5,page.getTotalPages()); + assertTrue("Page has previous page ?",page.hasPreviousPage()); + assertEquals("page size", 25,page.getPageSize()); + assertEquals("document count", 102,page.getTotalSize()); + + } + +} From 3208da5cdc0479171549310ae5c3ff9c4e3e309b Mon Sep 17 00:00:00 2001 From: gvaidees Date: Wed, 20 Aug 2014 18:13:32 -0700 Subject: [PATCH 076/357] Code cleanup --- .../javaclient/JavaApiBatchSuite.java | 528 +++--- .../TestAppServicesGeoAttrPairConstraint.java | 539 ++++--- .../TestAppServicesGeoElemPairConstraint.java | 539 ++++--- ...tAppServicesGeoElementChildConstraint.java | 545 ++++--- .../TestAppServicesRangeConstraint.java | 377 +++-- .../TestAppServicesValueConstraint.java | 535 ++++--- .../TestAppServicesWordConstraint.java | 370 +++-- .../marklogic/javaclient/TestBug18026.java | 348 ++-- .../marklogic/javaclient/TestBug18724.java | 488 +++--- .../marklogic/javaclient/TestBug18736.java | 209 ++- .../marklogic/javaclient/TestBug18990.java | 189 ++- .../marklogic/javaclient/TestBug18993.java | 149 +- .../marklogic/javaclient/TestBug19016.java | 139 +- .../marklogic/javaclient/TestBug19046.java | 145 +- .../marklogic/javaclient/TestBug19144.java | 242 ++- .../marklogic/javaclient/TestBug19389.java | 157 +- .../marklogic/javaclient/TestBug20979.java | 168 +- .../marklogic/javaclient/TestBug21159.java | 310 ++-- .../marklogic/javaclient/TestBug22037.java | 162 +- .../marklogic/javaclient/TestBug26248.java | 178 +-- .../marklogic/javaclient/TestBytesHandle.java | 574 ++++--- .../javaclient/TestCRUDModulesDb.java | 735 +++++---- .../javaclient/TestConstraintCombination.java | 467 +++--- .../marklogic/javaclient/TestDOMHandle.java | 205 ++- .../javaclient/TestDocumentEncoding.java | 269 ++-- .../javaclient/TestDocumentFormat.java | 1333 ++++++++-------- .../javaclient/TestDocumentMimetype.java | 1420 ++++++++--------- .../javaclient/TestFieldConstraint.java | 151 +- .../marklogic/javaclient/TestFileHandle.java | 623 ++++---- 29 files changed, 5975 insertions(+), 6119 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/JavaApiBatchSuite.java b/test-complete/src/test/java/com/marklogic/javaclient/JavaApiBatchSuite.java index e4c220240..d4dbf4de9 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/JavaApiBatchSuite.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/JavaApiBatchSuite.java @@ -1,267 +1,261 @@ -package com.marklogic.javaclient; -import java.util.ArrayList; -import java.util.List; - -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.auth.AuthScope; -import org.apache.http.auth.UsernamePasswordCredentials; -import org.apache.http.client.methods.HttpDelete; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.util.EntityUtils; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ArrayNode; -import com.fasterxml.jackson.databind.node.ObjectNode; - -import org.junit.runner.JUnitCore; -import org.junit.runner.Result; -import org.junit.runner.notification.Failure; - -import com.marklogic.*; - -public class JavaApiBatchSuite { - - /* - * Copyright 2003-2013 MarkLogic Corporation. All Rights Reserved. - */ - - private static final Class[] testClasses = { TestAggregates.class, - TestAppServicesAbsRangeConstraint.class, - TestAppServicesCollectionConstraint.class, - TestAppServicesConstraintCombination.class, - TestAppServicesFieldConstraint.class, - TestAppServicesGeoAttrPairConstraint.class, - TestAppServicesGeoElementChildConstraint.class, - TestAppServicesGeoElementConstraint.class, - TestAppServicesGeoElemPairConstraint.class, - TestAppServicesRangeConstraint.class, - TestAppServicesRangePathIndexConstraint.class, - TestAppServicesValueConstraint.class, - TestAppServicesWordConstraint.class, - TestBug18026.class, - TestBug18724.class, - TestBug18736.class, - TestBug18801.class, - TestBug18920.class, - TestBug18990.class, - TestBug18993.class, - TestBug19016.class, - TestBug19046.class, - TestBug19092.class, - TestBug19140.class, - TestBug19144.class, - TestBug19389.class, - TestBug19443.class, - TestBug20979.class, - TestBug21159.class, - TestBug21183.class, - TestBug22037.class, - TestBytesHandle.class, - TestBulkReadSample1.class, - TestBulkWriteMetadata1.class, - TestBulkWriteSample1.class, - TestConstraintCombination.class, - TestCRUDModulesDb.class, - TestDatabaseAuthentication.class, - TestDatabaseClientConnection.class, - TestDocumentEncoding.class, - TestDocumentFormat.class, - TestDocumentMimetype.class, - TestDOMHandle.class, - TestFieldConstraint.class, - TestFileHandle.class, - TestInputSourceHandle.class, - TestInputStreamHandle.class, - TestKeyValueSearch.class, - TestLinkResultDocuments.class, - TestMetadata.class, - TestMetadataXML.class, - TestMultithreading.class, - TestNamespaces.class, - TestOptimisticLocking.class, - TestOutputStreamHandle.class, - TestPartialUpdate.class, - TestPatchCardinality.class, - TestQueryByExample.class, - TestQueryOptionBuilder.class, - TestQueryOptionBuilderGrammar.class, - TestQueryOptionBuilderSearchableExpression.class, - TestQueryOptionBuilderSearchOptions.class, - TestQueryOptionBuilderSortOrder.class, - TestQueryOptionBuilderTransformResults.class, - TestQueryOptionsHandle.class, - TestQueryOptionsListHandle.class, - TestRangeConstraint.class, - TestRangeConstraintAbsoluteBucket.class, - TestRangeConstraintRelativeBucket.class, - TestRawAlert.class, - TestRawCombinedQuery.class, - TestRawCombinedQueryGeo.class, - TestRawStructuredQuery.class, - TestReaderHandle.class, - TestRequestLogger.class, - TestResponseTransform.class, - TestRollbackTransaction.class, - TestSearchMultibyte.class, - TestSearchMultipleForests.class, - TestSearchOnJSON.class, - TestSearchOnProperties.class, - TestSearchOptions.class, - TestSearchSuggestion.class, - TestServerAssignedDocumentURI.class, - TestSourceHandle.class, - TestSSLConnection.class, - TestStandaloneGeoQuery.class, - TestStandaloneQuery.class, - TestStringHandle.class, - TestStructuredQuery.class, - TestStructuredQueryMildNot.class, - TestStructuredSearchGeo.class, - TestTransformXMLWithXSLT.class, - TestValueConstraint.class, - TestWordConstraint.class, - TestWriteTextDoc.class, - TestXMLDocumentRepair.class, - TestXMLEventReaderHandle.class, - TestXMLMultiByte.class, - TestXMLStreamReaderHandle.class, - ThreadClass.class, - ThreadSearch.class, - ThreadWrite.class }; - - private JavaApiBatchSuite() { - // cannot instantiate - } - - public static void runTestCase(Class testCase) - { - Result result = JUnitCore.runClasses(testCase); - for (Failure failure : result.getFailures()) - { - System.out.println("##################################\n Test Failed "+testCase+"with reason \n "+failure.toString()+"\n############################\n"); - } - } - public static void createRESTAppServer(String restServerName ,int restPort){ - try{ - DefaultHttpClient client = new DefaultHttpClient(); - - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); - HttpPost post = new HttpPost("http://localhost:8002"+ "/v1/rest-apis?format=json"); - String JSONString = - "{ \"rest-api\": {\"name\":\""+ - restServerName + - "\",\"port\":\""+ - restPort+ - "\"}}"; - //System.out.println(JSONString); - post.addHeader("Content-type", "application/json"); - post.setEntity(new StringEntity(JSONString)); - - HttpResponse response = client.execute(post); - HttpEntity respEntity = response.getEntity(); - - if (respEntity != null) { - // EntityUtils to get the response content - String content = EntityUtils.toString(respEntity); - System.out.println(content); - } - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); - } - } - public static void createRESTUser(String usrName, String pass, String roleName ){ - try{ - DefaultHttpClient client = new DefaultHttpClient(); - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); - HttpPost post = new HttpPost("http://localhost:8002"+ "/manage/v2/users?format=json"); - ObjectMapper mapper = new ObjectMapper(); - ObjectNode mainNode = mapper.createObjectNode(); -// ObjectNode childNode = mapper.createObjectNode(); - ArrayNode childArray = mapper.createArrayNode(); - mainNode.put("name",usrName); - mainNode.put("description", "user discription"); - mainNode.put("password", pass); - childArray.add(roleName); - mainNode.put("role", childArray); - //System.out.println(type + mainNode.path("range-element-indexes").path("range-element-index").toString()); -// System.out.println(mainNode.toString()); - post.addHeader("Content-type", "application/json"); - post.setEntity(new StringEntity(mainNode.toString())); - - HttpResponse response = client.execute(post); - HttpEntity respEntity = response.getEntity(); - if( response.getStatusLine().getStatusCode() == 400) - { - System.out.println("User already exist or a bad create request"); - } - else if (respEntity != null) { - // EntityUtils to get the response content - String content = EntityUtils.toString(respEntity); - System.out.println(content); - } - else {System.out.print("No Proper Response");} - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); - } - } - - // ---------------------------------------------------------- - public static void createRESTSSLAppServer(String SSLServerName ,int restPort ){ - - } - public static void deleteRESTAppServerWithDB(String restServerName) { - try{ - DefaultHttpClient client = new DefaultHttpClient(); - - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); - - HttpDelete delete = new HttpDelete("http://localhost:8002/v1/rest-apis/"+restServerName+"?include=content&include=modules"); - - HttpResponse response = client.execute(delete); - if(response.getStatusLine().getStatusCode()== 202){ - Thread.sleep(3500); - } - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); - } - } - /** - * This class is for the .Net IKVM port of the junit tests. - * - * @param args - * The args - */ - public static void main(String[] args) { - System.setProperty("REST.host", "localhost"); - System.setProperty("REST.port", "8013"); - System.setProperty("REST.user", "rest-admin"); - System.setProperty("xcc.pass", "x"); - createRESTUser("rest-admin","x","rest-admin"); - createRESTUser("rest-writer","x","rest-writer"); - createRESTUser("rest-reader","x","rest-reader"); - createRESTAppServer("JavaClientApiDefault",8013); - createRESTSSLAppServer("JavaClientApiDefaultSSL",8014); - for(Class testclass: testClasses ) { - System.out.println("Starting the Test: "+testclass); - runTestCase(testclass); - } - deleteRESTAppServerWithDB("JavaClientApiDefault"); - deleteRESTAppServerWithDB("JavaClientApiDefaultSSL"); - - } - - -} +package com.marklogic.javaclient; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.util.EntityUtils; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; + +import org.junit.runner.JUnitCore; +import org.junit.runner.Result; +import org.junit.runner.notification.Failure; + +public class JavaApiBatchSuite { + + /* + * Copyright 2003-2013 MarkLogic Corporation. All Rights Reserved. + */ + + private static final Class[] testClasses = { TestAggregates.class, + TestAppServicesAbsRangeConstraint.class, + TestAppServicesCollectionConstraint.class, + TestAppServicesConstraintCombination.class, + TestAppServicesFieldConstraint.class, + TestAppServicesGeoAttrPairConstraint.class, + TestAppServicesGeoElementChildConstraint.class, + TestAppServicesGeoElementConstraint.class, + TestAppServicesGeoElemPairConstraint.class, + TestAppServicesRangeConstraint.class, + TestAppServicesRangePathIndexConstraint.class, + TestAppServicesValueConstraint.class, + TestAppServicesWordConstraint.class, + TestBug18026.class, + TestBug18724.class, + TestBug18736.class, + TestBug18801.class, + TestBug18920.class, + TestBug18990.class, + TestBug18993.class, + TestBug19016.class, + TestBug19046.class, + TestBug19092.class, + TestBug19140.class, + TestBug19144.class, + TestBug19389.class, + TestBug19443.class, + TestBug20979.class, + TestBug21159.class, + TestBug21183.class, + TestBug22037.class, + TestBytesHandle.class, + TestBulkReadSample1.class, + TestBulkWriteMetadata1.class, + TestBulkWriteSample1.class, + TestConstraintCombination.class, + TestCRUDModulesDb.class, + TestDatabaseAuthentication.class, + TestDatabaseClientConnection.class, + TestDocumentEncoding.class, + TestDocumentFormat.class, + TestDocumentMimetype.class, + TestDOMHandle.class, + TestFieldConstraint.class, + TestFileHandle.class, + TestInputSourceHandle.class, + TestInputStreamHandle.class, + TestKeyValueSearch.class, + TestLinkResultDocuments.class, + TestMetadata.class, + TestMetadataXML.class, + TestMultithreading.class, + TestNamespaces.class, + TestOptimisticLocking.class, + TestOutputStreamHandle.class, + TestPartialUpdate.class, + TestPatchCardinality.class, + TestQueryByExample.class, + TestQueryOptionBuilder.class, + TestQueryOptionBuilderGrammar.class, + TestQueryOptionBuilderSearchableExpression.class, + TestQueryOptionBuilderSearchOptions.class, + TestQueryOptionBuilderSortOrder.class, + TestQueryOptionBuilderTransformResults.class, + TestQueryOptionsHandle.class, + TestQueryOptionsListHandle.class, + TestRangeConstraint.class, + TestRangeConstraintAbsoluteBucket.class, + TestRangeConstraintRelativeBucket.class, + TestRawAlert.class, + TestRawCombinedQuery.class, + TestRawCombinedQueryGeo.class, + TestRawStructuredQuery.class, + TestReaderHandle.class, + TestRequestLogger.class, + TestResponseTransform.class, + TestRollbackTransaction.class, + TestSearchMultibyte.class, + TestSearchMultipleForests.class, + TestSearchOnJSON.class, + TestSearchOnProperties.class, + TestSearchOptions.class, + TestSearchSuggestion.class, + TestServerAssignedDocumentURI.class, + TestSourceHandle.class, + TestSSLConnection.class, + TestStandaloneGeoQuery.class, + TestStandaloneQuery.class, + TestStringHandle.class, + TestStructuredQuery.class, + TestStructuredQueryMildNot.class, + TestStructuredSearchGeo.class, + TestTransformXMLWithXSLT.class, + TestValueConstraint.class, + TestWordConstraint.class, + TestWriteTextDoc.class, + TestXMLDocumentRepair.class, + TestXMLEventReaderHandle.class, + TestXMLMultiByte.class, + TestXMLStreamReaderHandle.class, + ThreadClass.class, + ThreadSearch.class, + ThreadWrite.class }; + + private JavaApiBatchSuite() { + // cannot instantiate + } + + public static void runTestCase(Class testCase) + { + Result result = JUnitCore.runClasses(testCase); + for (Failure failure : result.getFailures()) + { + System.out.println("##################################\n Test Failed "+testCase+"with reason \n "+failure.toString()+"\n############################\n"); + } + } + public static void createRESTAppServer(String restServerName ,int restPort){ + try{ + DefaultHttpClient client = new DefaultHttpClient(); + + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + HttpPost post = new HttpPost("http://localhost:8002"+ "/v1/rest-apis?format=json"); + String JSONString = + "{ \"rest-api\": {\"name\":\""+ + restServerName + + "\",\"port\":\""+ + restPort+ + "\"}}"; + //System.out.println(JSONString); + post.addHeader("Content-type", "application/json"); + post.setEntity(new StringEntity(JSONString)); + + HttpResponse response = client.execute(post); + HttpEntity respEntity = response.getEntity(); + + if (respEntity != null) { + // EntityUtils to get the response content + String content = EntityUtils.toString(respEntity); + System.out.println(content); + } + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } + } + public static void createRESTUser(String usrName, String pass, String roleName ){ + try{ + DefaultHttpClient client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + HttpPost post = new HttpPost("http://localhost:8002"+ "/manage/v2/users?format=json"); + ObjectMapper mapper = new ObjectMapper(); + ObjectNode mainNode = mapper.createObjectNode(); +// ObjectNode childNode = mapper.createObjectNode(); + ArrayNode childArray = mapper.createArrayNode(); + mainNode.put("name",usrName); + mainNode.put("description", "user discription"); + mainNode.put("password", pass); + childArray.add(roleName); + mainNode.put("role", childArray); + //System.out.println(type + mainNode.path("range-element-indexes").path("range-element-index").toString()); +// System.out.println(mainNode.toString()); + post.addHeader("Content-type", "application/json"); + post.setEntity(new StringEntity(mainNode.toString())); + + HttpResponse response = client.execute(post); + HttpEntity respEntity = response.getEntity(); + if( response.getStatusLine().getStatusCode() == 400) + { + System.out.println("User already exist or a bad create request"); + } + else if (respEntity != null) { + // EntityUtils to get the response content + String content = EntityUtils.toString(respEntity); + System.out.println(content); + } + else {System.out.print("No Proper Response");} + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } + } + + // ---------------------------------------------------------- + public static void createRESTSSLAppServer(String SSLServerName ,int restPort ){ + + } + public static void deleteRESTAppServerWithDB(String restServerName) { + try{ + DefaultHttpClient client = new DefaultHttpClient(); + + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + + HttpDelete delete = new HttpDelete("http://localhost:8002/v1/rest-apis/"+restServerName+"?include=content&include=modules"); + + HttpResponse response = client.execute(delete); + if(response.getStatusLine().getStatusCode()== 202){ + Thread.sleep(3500); + } + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } + } + /** + * This class is for the .Net IKVM port of the junit tests. + * + * @param args + * The args + */ + public static void main(String[] args) { + System.setProperty("REST.host", "localhost"); + System.setProperty("REST.port", "8013"); + System.setProperty("REST.user", "rest-admin"); + System.setProperty("xcc.pass", "x"); + createRESTUser("rest-admin","x","rest-admin"); + createRESTUser("rest-writer","x","rest-writer"); + createRESTUser("rest-reader","x","rest-reader"); + createRESTAppServer("JavaClientApiDefault",8013); + createRESTSSLAppServer("JavaClientApiDefaultSSL",8014); + for(Class testclass: testClasses ) { + System.out.println("Starting the Test: "+testclass); + runTestCase(testclass); + } + deleteRESTAppServerWithDB("JavaClientApiDefault"); + deleteRESTAppServerWithDB("JavaClientApiDefaultSSL"); + + } + + +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesGeoAttrPairConstraint.java b/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesGeoAttrPairConstraint.java index a456afe71..a9601fcc2 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesGeoAttrPairConstraint.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesGeoAttrPairConstraint.java @@ -1,270 +1,269 @@ -package com.marklogic.javaclient; - -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import com.marklogic.client.query.QueryManager; - -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.query.StringQueryDefinition; -import com.marklogic.client.io.DOMHandle; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; -import static org.junit.Assert.*; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.junit.*; - -public class TestAppServicesGeoAttrPairConstraint extends BasicJavaClientREST { - -// private String serverName = ""; - private static String dbName = "AppServicesGeoAttrPairConstraintDB"; - private static String [] fNames = {"AppServicesGeoAttrPairConstraintDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; -@BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); -// super.setUp(); -// serverName = getConnectedServerName(); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesGeoConstraint(dbName); - } -@After -public void testCleanUp() throws Exception -{ - clearDB(8011); - System.out.println("Running clear script"); -} -@Test - public void testPointPositiveLangLat() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testPointPositiveLangLat"); - - String queryOptionName = "geoConstraintOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - loadGeoData(); - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("geo-attr-pair:\"12,5\""); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("karl_kara 12,5 12,5 12 5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test - public void testPointPositiveLatNegativeLang() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testPointNegativeLangLat"); - - String queryOptionName = "geoConstraintOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - loadGeoData(); - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("geo-attr-pair:\"12,-5\""); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("karl_kara 12,-5 12,-5 12 -5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); - - // release client - client.release(); - } - -@Test - public void testNegativePointInvalidValue() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testNegativePointInvalidValue"); - - String queryOptionName = "geoConstraintOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - loadGeoData(); - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("geo-attr-pair:\"12,A\""); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - - String result = ""; - - try - { - queryMgr.search(querydef, resultsHandle); - Document resultDoc = resultsHandle.get(); - result = convertXMLDocumentToString(resultDoc).toString(); - System.out.println("Result : "+result); - - } catch (Exception e) { e.toString(); } - - assertTrue("Expected Warning message is not thrown", result.contains("[Invalid text, cannot parse geospatial point from '12,A'.]")); - - // release client - client.release(); - } - -@Test - public void testCirclePositiveLatNegativeLang() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("testCirclePositiveLatNegativeLang"); - - String queryOptionName = "geoConstraintOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - loadGeoData(); - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("geo-elem-child:\"@70 12,-5\""); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("5", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("karl_kara 12,-5 12,-5 12 -5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("jack_kara 11,-5 11,-5 11 -5", "string(//*[local-name()='result'][2]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("karl_jill 12,-4 12,-4 12 -4", "string(//*[local-name()='result'][3]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("bill_kara 13,-5 13,-5 13 -5", "string(//*[local-name()='result'][4]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("karl_gale 12,-6 12,-6 12 -6", "string(//*[local-name()='result'][5]//*[local-name()='match'])", resultDoc); - - // release client - client.release(); - } - - -@Test - public void testBoxPositiveLatNegativeLang() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("testBoxPositiveLatNegativeLang"); - - String queryOptionName = "geoConstraintOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - loadGeoData(); - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("geo-elem-child:\"[11,-5,12,-4]\""); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("3", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("karl_kara 12,-5 12,-5 12 -5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("jack_kara 11,-5 11,-5 11 -5", "string(//*[local-name()='result'][2]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("karl_jill 12,-4 12,-4 12 -4", "string(//*[local-name()='result'][3]//*[local-name()='match'])", resultDoc); - - // release client - client.release(); - } - -@Test - public void testCircleAndWord() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testCircleAndWord"); - - String queryOptionName = "geoConstraintOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - loadGeoData(); - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("geo-elem-child:\"@70 12,-5\" AND karl_kara"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("/geo-constraint/geo-constraint15.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); - - // release client - client.release(); - } - @AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); -// super.tearDown(); - } -} +package com.marklogic.javaclient; + +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import com.marklogic.client.query.QueryManager; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.query.StringQueryDefinition; +import com.marklogic.client.io.DOMHandle; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; +import static org.junit.Assert.*; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.*; + +public class TestAppServicesGeoAttrPairConstraint extends BasicJavaClientREST { + +// private String serverName = ""; + private static String dbName = "AppServicesGeoAttrPairConstraintDB"; + private static String [] fNames = {"AppServicesGeoAttrPairConstraintDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; +@BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); +// super.setUp(); +// serverName = getConnectedServerName(); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesGeoConstraint(dbName); + } +@After +public void testCleanUp() throws Exception +{ + clearDB(8011); + System.out.println("Running clear script"); +} +@Test + public void testPointPositiveLangLat() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testPointPositiveLangLat"); + + String queryOptionName = "geoConstraintOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + loadGeoData(); + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("geo-attr-pair:\"12,5\""); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("karl_kara 12,5 12,5 12 5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); + + // release client + client.release(); + } + +@Test + public void testPointPositiveLatNegativeLang() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testPointNegativeLangLat"); + + String queryOptionName = "geoConstraintOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + loadGeoData(); + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("geo-attr-pair:\"12,-5\""); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("karl_kara 12,-5 12,-5 12 -5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); + + // release client + client.release(); + } + +@Test + public void testNegativePointInvalidValue() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testNegativePointInvalidValue"); + + String queryOptionName = "geoConstraintOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + loadGeoData(); + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("geo-attr-pair:\"12,A\""); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + + String result = ""; + + try + { + queryMgr.search(querydef, resultsHandle); + Document resultDoc = resultsHandle.get(); + result = convertXMLDocumentToString(resultDoc).toString(); + System.out.println("Result : "+result); + + } catch (Exception e) { e.toString(); } + + assertTrue("Expected Warning message is not thrown", result.contains("[Invalid text, cannot parse geospatial point from '12,A'.]")); + + // release client + client.release(); + } + +@Test + public void testCirclePositiveLatNegativeLang() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("testCirclePositiveLatNegativeLang"); + + String queryOptionName = "geoConstraintOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + loadGeoData(); + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("geo-elem-child:\"@70 12,-5\""); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("5", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("karl_kara 12,-5 12,-5 12 -5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); + assertXpathEvaluatesTo("jack_kara 11,-5 11,-5 11 -5", "string(//*[local-name()='result'][2]//*[local-name()='match'])", resultDoc); + assertXpathEvaluatesTo("karl_jill 12,-4 12,-4 12 -4", "string(//*[local-name()='result'][3]//*[local-name()='match'])", resultDoc); + assertXpathEvaluatesTo("bill_kara 13,-5 13,-5 13 -5", "string(//*[local-name()='result'][4]//*[local-name()='match'])", resultDoc); + assertXpathEvaluatesTo("karl_gale 12,-6 12,-6 12 -6", "string(//*[local-name()='result'][5]//*[local-name()='match'])", resultDoc); + + // release client + client.release(); + } + + +@Test + public void testBoxPositiveLatNegativeLang() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("testBoxPositiveLatNegativeLang"); + + String queryOptionName = "geoConstraintOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + loadGeoData(); + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("geo-elem-child:\"[11,-5,12,-4]\""); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("3", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("karl_kara 12,-5 12,-5 12 -5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); + assertXpathEvaluatesTo("jack_kara 11,-5 11,-5 11 -5", "string(//*[local-name()='result'][2]//*[local-name()='match'])", resultDoc); + assertXpathEvaluatesTo("karl_jill 12,-4 12,-4 12 -4", "string(//*[local-name()='result'][3]//*[local-name()='match'])", resultDoc); + + // release client + client.release(); + } + +@Test + public void testCircleAndWord() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testCircleAndWord"); + + String queryOptionName = "geoConstraintOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + loadGeoData(); + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("geo-elem-child:\"@70 12,-5\" AND karl_kara"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("/geo-constraint/geo-constraint15.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); + + // release client + client.release(); + } + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); +// super.tearDown(); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesGeoElemPairConstraint.java b/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesGeoElemPairConstraint.java index 7d520ea88..bfecaa5ee 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesGeoElemPairConstraint.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesGeoElemPairConstraint.java @@ -1,272 +1,267 @@ -package com.marklogic.javaclient; - -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.query.StringQueryDefinition; -import com.marklogic.client.io.DOMHandle; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; -import static org.junit.Assert.*; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.junit.*; - -public class TestAppServicesGeoElemPairConstraint extends BasicJavaClientREST { - -// private static String serverName = ""; - private static String dbName = "AppServicesGeoElemPairConstraintDB"; - private static String [] fNames = {"AppServicesGeoElemPairConstraintDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - - @BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); -// super.setUp(); -// serverName = getConnectedServerName(); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesGeoConstraint(dbName); - } - @After - public void testCleanUp() throws Exception - { - clearDB(8011); - System.out.println("Running clear script"); - } - @SuppressWarnings("deprecation") - @Test - public void testPointPositiveLangLat() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testPointPositiveLangLat"); - - String queryOptionName = "geoConstraintOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - loadGeoData(); - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("geo-elem-pair:\"12,5\""); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("karl_kara 12,5 12,5 12 5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); - - // release client - client.release(); - } - - @SuppressWarnings("deprecation") - @Test - public void testPointNegativeLatPositiveLang() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testPointNegativeLatPositiveLang"); - - String queryOptionName = "geoConstraintOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - loadGeoData(); - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("geo-elem-pair:\"-12,5\""); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("karl_kara -12,5 -12,5 -12 5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); - - // release client - client.release(); - } - - @SuppressWarnings("deprecation") - @Test - public void testNegativePointInvalidValue() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testNegativePointInvalidValue"); - - String queryOptionName = "geoConstraintOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - loadGeoData(); - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("geo-elem-pair:\"-12,A\""); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - String result = ""; - try - { - queryMgr.search(querydef, resultsHandle); - Document resultDoc = resultsHandle.get(); - result = convertXMLDocumentToString(resultDoc).toString(); - System.out.println("Result : "+result); - } catch (Exception e) { e.toString(); } - - assertTrue("Expected Warning message is not thrown", result.contains("[Invalid text, cannot parse geospatial point from '-12,A'.]")); - - - // release client - client.release(); - } - - @SuppressWarnings("deprecation") - @Test - public void testCircleNegativeLatPositiveLang() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("testCircleNegativeLatPositiveLang"); - - String queryOptionName = "geoConstraintOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - loadGeoData(); - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("geo-elem-pair:\"@70 -12,5\""); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("5", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("bill_kara -13,5 -13,5 -13 5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("karl_gale -12,6 -12,6 -12 6", "string(//*[local-name()='result'][2]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("karl_kara -12,5 -12,5 -12 5", "string(//*[local-name()='result'][3]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("jack_kara -11,5 -11,5 -11 5", "string(//*[local-name()='result'][4]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("karl_jill -12,4 -12,4 -12 4", "string(//*[local-name()='result'][5]//*[local-name()='match'])", resultDoc); - - // release client - client.release(); - } - - @SuppressWarnings("deprecation") - @Test - public void testBoxNegativeLatPositiveLang() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("testBoxNegativeLatPositiveLang"); - - String queryOptionName = "geoConstraintOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - loadGeoData(); - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("geo-elem-pair:\"[-12,4,-11,5]\""); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("3", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("karl_kara -12,5 -12,5 -12 5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("jack_kara -11,5 -11,5 -11 5", "string(//*[local-name()='result'][2]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("karl_jill -12,4 -12,4 -12 4", "string(//*[local-name()='result'][3]//*[local-name()='match'])", resultDoc); - - // release client - client.release(); - } - - @SuppressWarnings("deprecation") - @Test - public void testBoxAndWord() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testBoxAndWord"); - - String queryOptionName = "geoConstraintOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - loadGeoData(); - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("geo-elem-pair:\"[-12,4,-11,5]\" AND karl_kara"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("/geo-constraint/geo-constraint20.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); - - // release client - client.release(); - } - @AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); -// super.tearDown(); - } -} +package com.marklogic.javaclient; + +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.StringQueryDefinition; +import com.marklogic.client.io.DOMHandle; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; +import static org.junit.Assert.*; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.*; + +public class TestAppServicesGeoElemPairConstraint extends BasicJavaClientREST { + +// private static String serverName = ""; + private static String dbName = "AppServicesGeoElemPairConstraintDB"; + private static String [] fNames = {"AppServicesGeoElemPairConstraintDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); +// super.setUp(); +// serverName = getConnectedServerName(); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesGeoConstraint(dbName); + } + @After + public void testCleanUp() throws Exception + { + clearDB(8011); + System.out.println("Running clear script"); + } + + @Test + public void testPointPositiveLangLat() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testPointPositiveLangLat"); + + String queryOptionName = "geoConstraintOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + loadGeoData(); + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("geo-elem-pair:\"12,5\""); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("karl_kara 12,5 12,5 12 5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testPointNegativeLatPositiveLang() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testPointNegativeLatPositiveLang"); + + String queryOptionName = "geoConstraintOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + loadGeoData(); + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("geo-elem-pair:\"-12,5\""); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("karl_kara -12,5 -12,5 -12 5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testNegativePointInvalidValue() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testNegativePointInvalidValue"); + + String queryOptionName = "geoConstraintOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + loadGeoData(); + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("geo-elem-pair:\"-12,A\""); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + String result = ""; + try + { + queryMgr.search(querydef, resultsHandle); + Document resultDoc = resultsHandle.get(); + result = convertXMLDocumentToString(resultDoc).toString(); + System.out.println("Result : "+result); + } catch (Exception e) { e.toString(); } + + assertTrue("Expected Warning message is not thrown", result.contains("[Invalid text, cannot parse geospatial point from '-12,A'.]")); + + + // release client + client.release(); + } + + @Test + public void testCircleNegativeLatPositiveLang() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("testCircleNegativeLatPositiveLang"); + + String queryOptionName = "geoConstraintOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + loadGeoData(); + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("geo-elem-pair:\"@70 -12,5\""); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("5", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("bill_kara -13,5 -13,5 -13 5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); + assertXpathEvaluatesTo("karl_gale -12,6 -12,6 -12 6", "string(//*[local-name()='result'][2]//*[local-name()='match'])", resultDoc); + assertXpathEvaluatesTo("karl_kara -12,5 -12,5 -12 5", "string(//*[local-name()='result'][3]//*[local-name()='match'])", resultDoc); + assertXpathEvaluatesTo("jack_kara -11,5 -11,5 -11 5", "string(//*[local-name()='result'][4]//*[local-name()='match'])", resultDoc); + assertXpathEvaluatesTo("karl_jill -12,4 -12,4 -12 4", "string(//*[local-name()='result'][5]//*[local-name()='match'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testBoxNegativeLatPositiveLang() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("testBoxNegativeLatPositiveLang"); + + String queryOptionName = "geoConstraintOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + loadGeoData(); + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("geo-elem-pair:\"[-12,4,-11,5]\""); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("3", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("karl_kara -12,5 -12,5 -12 5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); + assertXpathEvaluatesTo("jack_kara -11,5 -11,5 -11 5", "string(//*[local-name()='result'][2]//*[local-name()='match'])", resultDoc); + assertXpathEvaluatesTo("karl_jill -12,4 -12,4 -12 4", "string(//*[local-name()='result'][3]//*[local-name()='match'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testBoxAndWord() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testBoxAndWord"); + + String queryOptionName = "geoConstraintOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + loadGeoData(); + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("geo-elem-pair:\"[-12,4,-11,5]\" AND karl_kara"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("/geo-constraint/geo-constraint20.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); + + // release client + client.release(); + } + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); +// super.tearDown(); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesGeoElementChildConstraint.java b/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesGeoElementChildConstraint.java index daa15e05b..df571a552 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesGeoElementChildConstraint.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesGeoElementChildConstraint.java @@ -1,275 +1,270 @@ -package com.marklogic.javaclient; - -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import com.marklogic.client.query.QueryManager; - -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.query.StringQueryDefinition; -import com.marklogic.client.io.DOMHandle; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; -import static org.junit.Assert.*; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.junit.*; - -public class TestAppServicesGeoElementChildConstraint extends BasicJavaClientREST { - -// private String serverName = ""; - private static String dbName = "AppServicesGeoElementChildConstraintDB"; - private static String [] fNames = {"AppServicesGeoElementChildConstraintDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; -@BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); -// super.setUp(); -// serverName = getConnectedServerName(); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesGeoConstraint(dbName); - } -@After -public void testCleanUp() throws Exception -{ - clearDB(8011); - System.out.println("Running clear script"); -} -@SuppressWarnings("deprecation") -@Test - public void testPointPositiveLangLat() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testPointPositiveLangLat"); - - String queryOptionName = "geoConstraintOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - loadGeoData(); - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("geo-elem-child:\"12,5\""); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("karl_kara 12,5 12,5 12 5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test - public void testPointNegativeLangLat() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testPointNegativeLangLat"); - - String queryOptionName = "geoConstraintOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - loadGeoData(); - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("geo-elem-child:\"-12,-5\""); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("karl_kara -12,-5 -12,-5 -12 -5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test - public void testNegativePointInvalidValue() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testNegativePointInvalidValue"); - - String queryOptionName = "geoConstraintOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - loadGeoData(); - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("geo-elem-child:\"12,A\""); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - - String result = ""; - try - { - queryMgr.search(querydef, resultsHandle); - Document resultDoc = resultsHandle.get(); - result = convertXMLDocumentToString(resultDoc).toString(); - System.out.println("Result : "+result); - - } catch (Exception e) { e.toString(); } - - assertTrue("Expected Warning message is not thrown", result.contains("[Invalid text, cannot parse geospatial point from '12,A'.]")); - - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test - public void testCircleNegativeLangLat() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("testCircleNegativeLangLat"); - - String queryOptionName = "geoConstraintOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - loadGeoData(); - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("geo-elem-child:\"@70 -12,-5\""); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("5", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("jack_kara -11,-5 -11,-5 -11 -5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("karl_jill -12,-4 -12,-4 -12 -4", "string(//*[local-name()='result'][2]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("karl_kara -12,-5 -12,-5 -12 -5", "string(//*[local-name()='result'][3]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("bill_kara -13,-5 -13,-5 -13 -5", "string(//*[local-name()='result'][4]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("karl_gale -12,-6 -12,-6 -12 -6", "string(//*[local-name()='result'][5]//*[local-name()='match'])", resultDoc); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test - public void testBoxNegativeLangLat() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("testBoxNegativeLangLat"); - - String queryOptionName = "geoConstraintOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - loadGeoData(); - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("geo-elem-child:\"[-12,-5,-11,-4]\""); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("4", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("jack_kara -11,-5 -11,-5 -11 -5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("karl_jill -12,-4 -12,-4 -12 -4", "string(//*[local-name()='result'][2]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("karl_kara -12,-5 -12,-5 -12 -5", "string(//*[local-name()='result'][3]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("jack_jill -11,-4 -11,-4 -11 -4", "string(//*[local-name()='result'][4]//*[local-name()='match'])", resultDoc); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test - public void testBoxAndWord() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testPointAndWord"); - - String queryOptionName = "geoConstraintOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - loadGeoData(); - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("geo-elem-child:\"[-12,-5,-11,-4]\" AND karl_kara"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("/geo-constraint/geo-constraint2.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); - - // release client - client.release(); - } - @AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); -// super.tearDown(); - } -} +package com.marklogic.javaclient; + +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import com.marklogic.client.query.QueryManager; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.query.StringQueryDefinition; +import com.marklogic.client.io.DOMHandle; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; +import static org.junit.Assert.*; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.*; + +public class TestAppServicesGeoElementChildConstraint extends BasicJavaClientREST { + +// private String serverName = ""; + private static String dbName = "AppServicesGeoElementChildConstraintDB"; + private static String [] fNames = {"AppServicesGeoElementChildConstraintDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; +@BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); +// super.setUp(); +// serverName = getConnectedServerName(); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesGeoConstraint(dbName); + } +@After +public void testCleanUp() throws Exception +{ + clearDB(8011); + System.out.println("Running clear script"); +} + +@Test + public void testPointPositiveLangLat() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testPointPositiveLangLat"); + + String queryOptionName = "geoConstraintOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + loadGeoData(); + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("geo-elem-child:\"12,5\""); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("karl_kara 12,5 12,5 12 5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); + + // release client + client.release(); + } + +@Test + public void testPointNegativeLangLat() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testPointNegativeLangLat"); + + String queryOptionName = "geoConstraintOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + loadGeoData(); + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("geo-elem-child:\"-12,-5\""); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("karl_kara -12,-5 -12,-5 -12 -5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); + + // release client + client.release(); + } + +@Test + public void testNegativePointInvalidValue() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testNegativePointInvalidValue"); + + String queryOptionName = "geoConstraintOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + loadGeoData(); + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("geo-elem-child:\"12,A\""); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + + String result = ""; + try + { + queryMgr.search(querydef, resultsHandle); + Document resultDoc = resultsHandle.get(); + result = convertXMLDocumentToString(resultDoc).toString(); + System.out.println("Result : "+result); + + } catch (Exception e) { e.toString(); } + + assertTrue("Expected Warning message is not thrown", result.contains("[Invalid text, cannot parse geospatial point from '12,A'.]")); + + + // release client + client.release(); + } + +@Test + public void testCircleNegativeLangLat() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("testCircleNegativeLangLat"); + + String queryOptionName = "geoConstraintOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + loadGeoData(); + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("geo-elem-child:\"@70 -12,-5\""); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("5", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("jack_kara -11,-5 -11,-5 -11 -5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); + assertXpathEvaluatesTo("karl_jill -12,-4 -12,-4 -12 -4", "string(//*[local-name()='result'][2]//*[local-name()='match'])", resultDoc); + assertXpathEvaluatesTo("karl_kara -12,-5 -12,-5 -12 -5", "string(//*[local-name()='result'][3]//*[local-name()='match'])", resultDoc); + assertXpathEvaluatesTo("bill_kara -13,-5 -13,-5 -13 -5", "string(//*[local-name()='result'][4]//*[local-name()='match'])", resultDoc); + assertXpathEvaluatesTo("karl_gale -12,-6 -12,-6 -12 -6", "string(//*[local-name()='result'][5]//*[local-name()='match'])", resultDoc); + + // release client + client.release(); + } + +@Test + public void testBoxNegativeLangLat() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("testBoxNegativeLangLat"); + + String queryOptionName = "geoConstraintOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + loadGeoData(); + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("geo-elem-child:\"[-12,-5,-11,-4]\""); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("4", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("jack_kara -11,-5 -11,-5 -11 -5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); + assertXpathEvaluatesTo("karl_jill -12,-4 -12,-4 -12 -4", "string(//*[local-name()='result'][2]//*[local-name()='match'])", resultDoc); + assertXpathEvaluatesTo("karl_kara -12,-5 -12,-5 -12 -5", "string(//*[local-name()='result'][3]//*[local-name()='match'])", resultDoc); + assertXpathEvaluatesTo("jack_jill -11,-4 -11,-4 -11 -4", "string(//*[local-name()='result'][4]//*[local-name()='match'])", resultDoc); + + // release client + client.release(); + } + +@Test + public void testBoxAndWord() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testPointAndWord"); + + String queryOptionName = "geoConstraintOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + loadGeoData(); + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("geo-elem-child:\"[-12,-5,-11,-4]\" AND karl_kara"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("/geo-constraint/geo-constraint2.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); + + // release client + client.release(); + } + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); +// super.tearDown(); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesRangeConstraint.java b/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesRangeConstraint.java index 0d1ac60fd..460fd7971 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesRangeConstraint.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesRangeConstraint.java @@ -1,189 +1,188 @@ -package com.marklogic.javaclient; - -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.query.StringQueryDefinition; -import com.marklogic.client.io.DOMHandle; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; -import static org.junit.Assert.*; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.junit.*; - -public class TestAppServicesRangeConstraint extends BasicJavaClientREST { - -// private String serverName = ""; - private static String dbName = "AppServicesRangeConstraintDB"; - private static String [] fNames = {"AppServicesRangeConstraintDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; -@BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); -// super.setUp(); -// serverName = getConnectedServerName(); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - } -@After -public void testCleanUp() throws Exception -{ - clearDB(8011); - System.out.println("Running clear script"); -} -@SuppressWarnings("deprecation") -@Test - public void testWithWordSearch() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testWithWordSearch"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "rangeConstraintWithWordSearchOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/range-constraint/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("date:2006-02-02 OR policymaker"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("2008-04-04", "string(//*[local-name()='result'][1]//*[local-name()='date'])", resultDoc); - assertXpathEvaluatesTo("2006-02-02", "string(//*[local-name()='result'][2]//*[local-name()='date'])", resultDoc); - assertXpathEvaluatesTo("Vannevar served as a prominent policymaker and public intellectual.", "string(//*[local-name()='result'][1]//*[local-name()='p'])", resultDoc); - assertXpathEvaluatesTo("The Bush article described a device called a Memex.", "string(//*[local-name()='result'][2]//*[local-name()='p'])", resultDoc); - - String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:element-range-query(fn:QName(\"http://purl.org/dc/elements/1.1/\",\"date\"), \"=\", xs:date(\"2006-02-02\"), (), 1), cts:word-query(\"policymaker\", (\"lang=en\"), 1))), (\"score-logtfidf\"), 1))[1 to 10]"; - - assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); - - // release client - client.release(); - } - - /*public void testNegativeWithoutIndexSettings() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testNegativeWithoutIndexSettings"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "rangeConstraintNegativeWithoutIndexSettingsOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/range-constraint/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("title:Bush"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - - String exception = ""; - - // run search - try - { - queryMgr.search(querydef, resultsHandle); - } - catch (Exception e) { exception = e.toString(); } - - String expectedException = "com.marklogic.client.FailedRequestException: Local message: search failed: Internal Server ErrorServer Message: XDMP-ELEMRIDXNOTFOUND"; - - //assertEquals("Wrong exception", expectedException, exception); - boolean exceptionIsThrown = exception.contains(expectedException); - assertTrue("Exception is not thrown", exceptionIsThrown); - - // release client - client.release(); - }*/ - -@SuppressWarnings("deprecation") -@Test - public void testNegativeTypeMismatch() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testNegativeTypeMismatch"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "rangeConstraintNegativeTypeMismatchOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/range-constraint/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("date:2006-02-02"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - - String exception = ""; - - // run search - try - { - queryMgr.search(querydef, resultsHandle); - } - catch (Exception e) { exception = e.toString(); } - - String expectedException = "com.marklogic.client.FailedRequestException: Local message: search failed: Bad Request. Server Message: XDMP-ELEMRIDXNOTFOUND"; - - //assertEquals("Wrong exception", expectedException, exception); - boolean exceptionIsThrown = exception.contains(expectedException); - assertTrue("Exception is not thrown", exceptionIsThrown); - - // release client - client.release(); - } -@AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); -// super.tearDown(); - } -} +package com.marklogic.javaclient; + +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.StringQueryDefinition; +import com.marklogic.client.io.DOMHandle; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; +import static org.junit.Assert.*; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.*; + +public class TestAppServicesRangeConstraint extends BasicJavaClientREST { + +// private String serverName = ""; + private static String dbName = "AppServicesRangeConstraintDB"; + private static String [] fNames = {"AppServicesRangeConstraintDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; +@BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); +// super.setUp(); +// serverName = getConnectedServerName(); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + } +@After +public void testCleanUp() throws Exception +{ + clearDB(8011); + System.out.println("Running clear script"); +} + +@Test + public void testWithWordSearch() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testWithWordSearch"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "rangeConstraintWithWordSearchOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/range-constraint/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("date:2006-02-02 OR policymaker"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("2008-04-04", "string(//*[local-name()='result'][1]//*[local-name()='date'])", resultDoc); + assertXpathEvaluatesTo("2006-02-02", "string(//*[local-name()='result'][2]//*[local-name()='date'])", resultDoc); + assertXpathEvaluatesTo("Vannevar served as a prominent policymaker and public intellectual.", "string(//*[local-name()='result'][1]//*[local-name()='p'])", resultDoc); + assertXpathEvaluatesTo("The Bush article described a device called a Memex.", "string(//*[local-name()='result'][2]//*[local-name()='p'])", resultDoc); + + String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:element-range-query(fn:QName(\"http://purl.org/dc/elements/1.1/\",\"date\"), \"=\", xs:date(\"2006-02-02\"), (), 1), cts:word-query(\"policymaker\", (\"lang=en\"), 1))), (\"score-logtfidf\"), 1))[1 to 10]"; + + assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); + + // release client + client.release(); + } + + /*public void testNegativeWithoutIndexSettings() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testNegativeWithoutIndexSettings"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "rangeConstraintNegativeWithoutIndexSettingsOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/range-constraint/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("title:Bush"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + + String exception = ""; + + // run search + try + { + queryMgr.search(querydef, resultsHandle); + } + catch (Exception e) { exception = e.toString(); } + + String expectedException = "com.marklogic.client.FailedRequestException: Local message: search failed: Internal Server ErrorServer Message: XDMP-ELEMRIDXNOTFOUND"; + + //assertEquals("Wrong exception", expectedException, exception); + boolean exceptionIsThrown = exception.contains(expectedException); + assertTrue("Exception is not thrown", exceptionIsThrown); + + // release client + client.release(); + }*/ + +@Test + public void testNegativeTypeMismatch() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testNegativeTypeMismatch"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "rangeConstraintNegativeTypeMismatchOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/range-constraint/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("date:2006-02-02"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + + String exception = ""; + + // run search + try + { + queryMgr.search(querydef, resultsHandle); + } + catch (Exception e) { exception = e.toString(); } + + String expectedException = "com.marklogic.client.FailedRequestException: Local message: search failed: Bad Request. Server Message: XDMP-ELEMRIDXNOTFOUND"; + + //assertEquals("Wrong exception", expectedException, exception); + boolean exceptionIsThrown = exception.contains(expectedException); + assertTrue("Exception is not thrown", exceptionIsThrown); + + // release client + client.release(); + } +@AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); +// super.tearDown(); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesValueConstraint.java b/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesValueConstraint.java index bdb82a657..09d37eeda 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesValueConstraint.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesValueConstraint.java @@ -1,268 +1,267 @@ -package com.marklogic.javaclient; - -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; - -import com.marklogic.client.query.QueryManager; - -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.query.StringQueryDefinition; -import com.marklogic.client.io.DOMHandle; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.junit.*; - -public class TestAppServicesValueConstraint extends BasicJavaClientREST { - - private static String dbName = "AppServicesValueConstraintDB"; - private static String [] fNames = {"AppServicesValueConstraintDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - - @BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - } - @After - public void testCleanUp() throws Exception - { - clearDB(8011); - System.out.println("Running clear script"); - } - - @Test - public void testWildcard() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testWildcard"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "valueConstraintWildCardOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/value-constraint/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("id:00*2 OR id:0??6"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); - - String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:element-value-query(fn:QName(\"\",\"id\"), \"00*2\", (\"lang=en\"), 1), cts:element-value-query(fn:QName(\"\",\"id\"), \"0??6\", (\"lang=en\"), 1))), (\"score-logtfidf\"), 1))[1 to 10]"; - - assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); - - // release client - client.release(); - } - - - @Test - public void testGoogleStyleGrammar() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testGoogleStyleGrammar"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "valueConstraintGoogleGrammarOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/value-constraint/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("title:\"The memex\" OR (id:0113 AND date:2007-03-03)"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("For 1945", "string(//*[local-name()='result'][1]//*[local-name()='title'])", resultDoc); - assertXpathEvaluatesTo("The memex", "string(//*[local-name()='result'][2]//*[local-name()='title'])", resultDoc); - assertXpathEvaluatesTo("0113", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); - - String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:element-value-query(fn:QName(\"\",\"title\"), \"The memex\", (\"lang=en\"), 1), cts:and-query((cts:element-value-query(fn:QName(\"\",\"id\"), \"0113\", (\"lang=en\"), 1), cts:element-value-query(fn:QName(\"http://purl.org/dc/elements/1.1/\",\"date\"), \"2007-03-03\", (\"lang=en\"), 1)), ()))), (\"score-logtfidf\"), 1))[1 to 10]"; - - assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); - - // release client - client.release(); - } - - - @Test - public void testWithoutIndexSettingsAndNS() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testWithoutIndexSettingsAndNS"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "valueConstraintWithoutIndexSettingsAndNSOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/value-constraint/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("id:0012"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - - String expectedSearchReport = "(cts:search(fn:collection(), cts:element-value-query(fn:QName(\"\",\"id\"), \"0012\", (\"lang=en\"), 1), (\"score-logtfidf\"), 1))[1 to 10]"; - - assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); - - // release client - client.release(); - } - - @SuppressWarnings("deprecation") - @Test - public void testWithIndexSettingsAndNS() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testWithIndexSettingsAndNS"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "valueConstraintWithIndexSettingsAndNSOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/value-constraint/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("dt:2007-03-03"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("2007-03-03", "string(//*[local-name()='result'][1]//*[local-name()='date'])", resultDoc); - - String expectedSearchReport = "(cts:search(fn:collection(), cts:element-value-query(fn:QName(\"http://purl.org/dc/elements/1.1/\",\"date\"), \"2007-03-03\", (\"lang=en\"), 1), (\"score-logtfidf\"), 1))[1 to 10]"; - - assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); - - // release client - client.release(); - } - - - @Test - public void testSpaceSeparated() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testSpaceSeparated"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "valueConstraintSpaceSeparatedOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/value-constraint/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("title:\"Vannevar Bush\" price:0.1"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("Vannevar Bush", "string(//*[local-name()='result']//*[local-name()='title'])", resultDoc); - assertXpathEvaluatesTo("0.1", "string(//*[local-name()='result']//@*[local-name()='amt'])", resultDoc); - - String expectedSearchReport = "(cts:search(fn:collection(), cts:and-query((cts:element-value-query(fn:QName(\"\",\"title\"), \"Vannevar Bush\", (\"lang=en\"), 1), cts:element-attribute-value-query(fn:QName(\"http://cloudbank.com\",\"price\"), fn:QName(\"\",\"amt\"), \"0.1\", (\"lang=en\"), 1)), ()), (\"score-logtfidf\"), 1))[1 to 10]"; - - assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); - - // release client - client.release(); - } - - @AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } -} +package com.marklogic.javaclient; + +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; + +import com.marklogic.client.query.QueryManager; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.query.StringQueryDefinition; +import com.marklogic.client.io.DOMHandle; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.*; + +public class TestAppServicesValueConstraint extends BasicJavaClientREST { + + private static String dbName = "AppServicesValueConstraintDB"; + private static String [] fNames = {"AppServicesValueConstraintDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + } + @After + public void testCleanUp() throws Exception + { + clearDB(8011); + System.out.println("Running clear script"); + } + + @Test + public void testWildcard() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testWildcard"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "valueConstraintWildCardOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/value-constraint/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("id:00*2 OR id:0??6"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); + assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); + + String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:element-value-query(fn:QName(\"\",\"id\"), \"00*2\", (\"lang=en\"), 1), cts:element-value-query(fn:QName(\"\",\"id\"), \"0??6\", (\"lang=en\"), 1))), (\"score-logtfidf\"), 1))[1 to 10]"; + + assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); + + // release client + client.release(); + } + + + @Test + public void testGoogleStyleGrammar() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testGoogleStyleGrammar"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "valueConstraintGoogleGrammarOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/value-constraint/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("title:\"The memex\" OR (id:0113 AND date:2007-03-03)"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("For 1945", "string(//*[local-name()='result'][1]//*[local-name()='title'])", resultDoc); + assertXpathEvaluatesTo("The memex", "string(//*[local-name()='result'][2]//*[local-name()='title'])", resultDoc); + assertXpathEvaluatesTo("0113", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); + assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); + + String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:element-value-query(fn:QName(\"\",\"title\"), \"The memex\", (\"lang=en\"), 1), cts:and-query((cts:element-value-query(fn:QName(\"\",\"id\"), \"0113\", (\"lang=en\"), 1), cts:element-value-query(fn:QName(\"http://purl.org/dc/elements/1.1/\",\"date\"), \"2007-03-03\", (\"lang=en\"), 1)), ()))), (\"score-logtfidf\"), 1))[1 to 10]"; + + assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); + + // release client + client.release(); + } + + + @Test + public void testWithoutIndexSettingsAndNS() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testWithoutIndexSettingsAndNS"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "valueConstraintWithoutIndexSettingsAndNSOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/value-constraint/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("id:0012"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); + + String expectedSearchReport = "(cts:search(fn:collection(), cts:element-value-query(fn:QName(\"\",\"id\"), \"0012\", (\"lang=en\"), 1), (\"score-logtfidf\"), 1))[1 to 10]"; + + assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testWithIndexSettingsAndNS() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testWithIndexSettingsAndNS"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "valueConstraintWithIndexSettingsAndNSOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/value-constraint/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("dt:2007-03-03"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("2007-03-03", "string(//*[local-name()='result'][1]//*[local-name()='date'])", resultDoc); + + String expectedSearchReport = "(cts:search(fn:collection(), cts:element-value-query(fn:QName(\"http://purl.org/dc/elements/1.1/\",\"date\"), \"2007-03-03\", (\"lang=en\"), 1), (\"score-logtfidf\"), 1))[1 to 10]"; + + assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); + + // release client + client.release(); + } + + + @Test + public void testSpaceSeparated() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testSpaceSeparated"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "valueConstraintSpaceSeparatedOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/value-constraint/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("title:\"Vannevar Bush\" price:0.1"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("Vannevar Bush", "string(//*[local-name()='result']//*[local-name()='title'])", resultDoc); + assertXpathEvaluatesTo("0.1", "string(//*[local-name()='result']//@*[local-name()='amt'])", resultDoc); + + String expectedSearchReport = "(cts:search(fn:collection(), cts:and-query((cts:element-value-query(fn:QName(\"\",\"title\"), \"Vannevar Bush\", (\"lang=en\"), 1), cts:element-attribute-value-query(fn:QName(\"http://cloudbank.com\",\"price\"), fn:QName(\"\",\"amt\"), \"0.1\", (\"lang=en\"), 1)), ()), (\"score-logtfidf\"), 1))[1 to 10]"; + + assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesWordConstraint.java b/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesWordConstraint.java index 29bea87dc..a3e85834b 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesWordConstraint.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesWordConstraint.java @@ -1,186 +1,184 @@ -package com.marklogic.javaclient; - -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; - -import com.marklogic.client.query.QueryManager; - -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.query.StringQueryDefinition; -import com.marklogic.client.io.DOMHandle; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.junit.*; -public class TestAppServicesWordConstraint extends BasicJavaClientREST { - - private static String dbName = "AppServicesWordConstraintDB"; - private static String [] fNames = {"AppServicesWordConstraintDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; -@BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0],restServerName,8011); - setupAppServicesConstraint(dbName); - } -@After -public void testCleanUp() throws Exception -{ - clearDB(8011); - System.out.println("Running clear script"); -} -@SuppressWarnings("deprecation") -@Test - public void testWithElementAndAttributeIndex() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testWithElementAndAttributeIndex"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "wordConstraintWithElementAndAttributeIndexOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/word-constraint/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("intitle:1945 OR inprice:12"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("3", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("For 1945", "string(//*[local-name()='result'][1]//*[local-name()='title'])", resultDoc); - assertXpathEvaluatesTo("The Bush article", "string(//*[local-name()='result'][2]//*[local-name()='title'])", resultDoc); - assertXpathEvaluatesTo("Vannevar served", "string(//*[local-name()='result'][3]//*[local-name()='title'])", resultDoc); - assertXpathEvaluatesTo("1.23", "string(//*[local-name()='result'][1]//@*[local-name()='amt'])", resultDoc); - assertXpathEvaluatesTo("0.12", "string(//*[local-name()='result'][2]//@*[local-name()='amt'])", resultDoc); - assertXpathEvaluatesTo("12.34", "string(//*[local-name()='result'][3]//@*[local-name()='amt'])", resultDoc); - - //String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:element-range-query(fn:QName(\"http://purl.org/dc/elements/1.1/\", \"date\"), \"=\", xs:date(\"2006-02-02\"), (), 1), cts:word-query(\"policymaker\", (\"lang=en\"), 1))), (\"score-logtfidf\"), 1))[1 to 10]"; - - //assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test - public void testWithNormalWordQuery() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testWithNormalWordQuery"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "wordConstraintWithNormalWordQueryOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/word-constraint/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("Memex OR inprice:.12"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("The Bush article", "string(//*[local-name()='result'][1]//*[local-name()='title'])", resultDoc); - assertXpathEvaluatesTo("The memex", "string(//*[local-name()='result'][2]//*[local-name()='title'])", resultDoc); - assertXpathEvaluatesTo("0.12", "string(//*[local-name()='result'][1]//@*[local-name()='amt'])", resultDoc); - assertXpathEvaluatesTo("123.45", "string(//*[local-name()='result'][2]//@*[local-name()='amt'])", resultDoc); - - String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:word-query(\"Memex\", (\"lang=en\"), 1), cts:element-attribute-word-query(fn:QName(\"http://cloudbank.com\",\"price\"), fn:QName(\"\",\"amt\"), \".12\", (\"lang=en\"), 1))), (\"score-logtfidf\"), 1))[1 to 10]"; - - assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test - public void testWithTermOptionCaseInsensitive() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testWithTermOptionCaseInsensitive"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "wordConstraintWithTermOptionCaseInsensitiveOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/word-constraint/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("intitle:for OR price:0.12"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("For 1945", "string(//*[local-name()='result'][1]//*[local-name()='title'])", resultDoc); - assertXpathEvaluatesTo("The Bush article", "string(//*[local-name()='result'][2]//*[local-name()='title'])", resultDoc); - assertXpathEvaluatesTo("1.23", "string(//*[local-name()='result'][1]//@*[local-name()='amt'])", resultDoc); - assertXpathEvaluatesTo("0.12", "string(//*[local-name()='result'][2]//@*[local-name()='amt'])", resultDoc); - - String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:element-word-query(fn:QName(\"\",\"title\"), \"for\", (\"case-insensitive\",\"lang=en\"), 1), cts:element-attribute-range-query(fn:QName(\"http://cloudbank.com\",\"price\"), fn:QName(\"\",\"amt\"), \"=\", 0.12, (), 1))), (\"score-logtfidf\",\"faceted\"), 1))[1 to 10]"; - - assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); - - // release client - client.release(); - } -@AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} +package com.marklogic.javaclient; + +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; + +import com.marklogic.client.query.QueryManager; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.query.StringQueryDefinition; +import com.marklogic.client.io.DOMHandle; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.*; +public class TestAppServicesWordConstraint extends BasicJavaClientREST { + + private static String dbName = "AppServicesWordConstraintDB"; + private static String [] fNames = {"AppServicesWordConstraintDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; +@BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0],restServerName,8011); + setupAppServicesConstraint(dbName); + } +@After +public void testCleanUp() throws Exception +{ + clearDB(8011); + System.out.println("Running clear script"); +} + +@Test + public void testWithElementAndAttributeIndex() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testWithElementAndAttributeIndex"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "wordConstraintWithElementAndAttributeIndexOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/word-constraint/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("intitle:1945 OR inprice:12"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("3", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("For 1945", "string(//*[local-name()='result'][1]//*[local-name()='title'])", resultDoc); + assertXpathEvaluatesTo("The Bush article", "string(//*[local-name()='result'][2]//*[local-name()='title'])", resultDoc); + assertXpathEvaluatesTo("Vannevar served", "string(//*[local-name()='result'][3]//*[local-name()='title'])", resultDoc); + assertXpathEvaluatesTo("1.23", "string(//*[local-name()='result'][1]//@*[local-name()='amt'])", resultDoc); + assertXpathEvaluatesTo("0.12", "string(//*[local-name()='result'][2]//@*[local-name()='amt'])", resultDoc); + assertXpathEvaluatesTo("12.34", "string(//*[local-name()='result'][3]//@*[local-name()='amt'])", resultDoc); + + //String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:element-range-query(fn:QName(\"http://purl.org/dc/elements/1.1/\", \"date\"), \"=\", xs:date(\"2006-02-02\"), (), 1), cts:word-query(\"policymaker\", (\"lang=en\"), 1))), (\"score-logtfidf\"), 1))[1 to 10]"; + + //assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); + + // release client + client.release(); + } + +@Test + public void testWithNormalWordQuery() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testWithNormalWordQuery"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "wordConstraintWithNormalWordQueryOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/word-constraint/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("Memex OR inprice:.12"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("The Bush article", "string(//*[local-name()='result'][1]//*[local-name()='title'])", resultDoc); + assertXpathEvaluatesTo("The memex", "string(//*[local-name()='result'][2]//*[local-name()='title'])", resultDoc); + assertXpathEvaluatesTo("0.12", "string(//*[local-name()='result'][1]//@*[local-name()='amt'])", resultDoc); + assertXpathEvaluatesTo("123.45", "string(//*[local-name()='result'][2]//@*[local-name()='amt'])", resultDoc); + + String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:word-query(\"Memex\", (\"lang=en\"), 1), cts:element-attribute-word-query(fn:QName(\"http://cloudbank.com\",\"price\"), fn:QName(\"\",\"amt\"), \".12\", (\"lang=en\"), 1))), (\"score-logtfidf\"), 1))[1 to 10]"; + + assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); + + // release client + client.release(); + } + +@Test + public void testWithTermOptionCaseInsensitive() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testWithTermOptionCaseInsensitive"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "wordConstraintWithTermOptionCaseInsensitiveOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/word-constraint/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("intitle:for OR price:0.12"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("For 1945", "string(//*[local-name()='result'][1]//*[local-name()='title'])", resultDoc); + assertXpathEvaluatesTo("The Bush article", "string(//*[local-name()='result'][2]//*[local-name()='title'])", resultDoc); + assertXpathEvaluatesTo("1.23", "string(//*[local-name()='result'][1]//@*[local-name()='amt'])", resultDoc); + assertXpathEvaluatesTo("0.12", "string(//*[local-name()='result'][2]//@*[local-name()='amt'])", resultDoc); + + String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:element-word-query(fn:QName(\"\",\"title\"), \"for\", (\"case-insensitive\",\"lang=en\"), 1), cts:element-attribute-range-query(fn:QName(\"http://cloudbank.com\",\"price\"), fn:QName(\"\",\"amt\"), \"=\", 0.12, (), 1))), (\"score-logtfidf\",\"faceted\"), 1))[1 to 10]"; + + assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); + + // release client + client.release(); + } +@AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBug18026.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBug18026.java index 938f0f82d..7d00fa138 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBug18026.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBug18026.java @@ -1,175 +1,173 @@ -package com.marklogic.javaclient; - -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.HashMap; - -import javax.xml.parsers.ParserConfigurationException; - -import org.custommonkey.xmlunit.SimpleNamespaceContext; -import org.custommonkey.xmlunit.XMLUnit; -import org.custommonkey.xmlunit.XpathEngine; -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.document.JSONDocumentManager; -import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.InputStreamHandle; - -import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; -import static org.junit.Assert.*; - -import org.junit.*; -public class TestBug18026 extends BasicJavaClientREST { - - private static String dbName = "Bug18026DB"; - private static String [] fNames = {"Bug18026DB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private XpathEngine xpather; - @BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - } - - @SuppressWarnings("deprecation") - @Test - public void testBug18026() throws IOException, SAXException, ParserConfigurationException - { - String filename = "xml-original.xml"; - String uri = "/write-buffer/"; - byte[] before = null; - byte[] after = null; - String strBefore = null; - String strAfter = null; - - System.out.println("Running testBug18026"); - - XMLUnit.setIgnoreAttributeOrder(true); - XMLUnit.setIgnoreWhitespace(true); - XMLUnit.setNormalize(true); - XMLUnit.setNormalizeWhitespace(true); - XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true); - - HashMap namespaces = new HashMap(); - namespaces.put("rapi", "http://marklogic.com/rest-api"); - namespaces.put("prop", "http://marklogic.com/xdmp/property"); - namespaces.put("xs", "http://www.w3.org/2001/XMLSchema"); - namespaces.put("xsi", "http://www.w3.org/2001/XMLSchema-instance"); - - SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext(namespaces); - - xpather = XMLUnit.newXpathEngine(); - xpather.setNamespaceContext(namespaceContext); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - Document readDoc = expectedXMLDocument(filename); - - DOMHandle dHandle = new DOMHandle(); - dHandle.set(readDoc); - before = dHandle.toBuffer(); - strBefore = new String(before); - System.out.println("Before: " + strBefore); - - // write doc - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - String docId = uri + filename; - docMgr.write(docId, dHandle); - - // read doc - docMgr.read(docId, dHandle); - dHandle.get(); - - after = dHandle.toBuffer(); - strAfter = new String(after); - System.out.println("After: " + strAfter); - - assertXMLEqual("Buffer is not the same", strBefore, strAfter); - - // release client - client.release(); - } - - @SuppressWarnings("deprecation") - @Test - public void testBug18026WithJson() throws IOException, SAXException, ParserConfigurationException - { - String filename = "json-original.json"; - String uri = "/write-buffer/"; - byte[] before = null; - byte[] after = null; - String strBefore = null; - String strAfter = null; - - System.out.println("Running testBug18026WithJson"); - - XMLUnit.setIgnoreAttributeOrder(true); - XMLUnit.setIgnoreWhitespace(true); - XMLUnit.setNormalize(true); - XMLUnit.setNormalizeWhitespace(true); - XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true); - - HashMap namespaces = new HashMap(); - namespaces.put("rapi", "http://marklogic.com/rest-api"); - namespaces.put("prop", "http://marklogic.com/xdmp/property"); - namespaces.put("xs", "http://www.w3.org/2001/XMLSchema"); - namespaces.put("xsi", "http://www.w3.org/2001/XMLSchema-instance"); - - SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext(namespaces); - - xpather = XMLUnit.newXpathEngine(); - xpather.setNamespaceContext(namespaceContext); - - ObjectMapper mapper = new ObjectMapper(); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - InputStream inputStream = new FileInputStream("src/test/java/com/marklogic/javaclient/data/" + filename); - - InputStreamHandle isHandle = new InputStreamHandle(); - isHandle.set(inputStream); - before = isHandle.toBuffer(); - strBefore = new String(before); - System.out.println("Before: " + strBefore); - - JsonNode contentBefore = mapper.readValue(strBefore, JsonNode.class); - - // write doc - JSONDocumentManager docMgr = client.newJSONDocumentManager(); - String docId = uri + filename; - docMgr.write(docId, isHandle); - - // read doc - docMgr.read(docId, isHandle); - isHandle.get(); - - after = isHandle.toBuffer(); - strAfter = new String(after); - System.out.println("After: " + strAfter); - - JsonNode contentAfter = mapper.readValue(strAfter, JsonNode.class); - - assertTrue("Buffered JSON document difference", contentBefore.equals(contentAfter)); - - // release client - client.release(); - } -@AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} +package com.marklogic.javaclient; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; + +import javax.xml.parsers.ParserConfigurationException; + +import org.custommonkey.xmlunit.SimpleNamespaceContext; +import org.custommonkey.xmlunit.XMLUnit; +import org.custommonkey.xmlunit.XpathEngine; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.JSONDocumentManager; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.InputStreamHandle; + +import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; +import static org.junit.Assert.*; + +import org.junit.*; +public class TestBug18026 extends BasicJavaClientREST { + + private static String dbName = "Bug18026DB"; + private static String [] fNames = {"Bug18026DB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private XpathEngine xpather; + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + } + + @Test + public void testBug18026() throws IOException, SAXException, ParserConfigurationException + { + String filename = "xml-original.xml"; + String uri = "/write-buffer/"; + byte[] before = null; + byte[] after = null; + String strBefore = null; + String strAfter = null; + + System.out.println("Running testBug18026"); + + XMLUnit.setIgnoreAttributeOrder(true); + XMLUnit.setIgnoreWhitespace(true); + XMLUnit.setNormalize(true); + XMLUnit.setNormalizeWhitespace(true); + XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true); + + HashMap namespaces = new HashMap(); + namespaces.put("rapi", "http://marklogic.com/rest-api"); + namespaces.put("prop", "http://marklogic.com/xdmp/property"); + namespaces.put("xs", "http://www.w3.org/2001/XMLSchema"); + namespaces.put("xsi", "http://www.w3.org/2001/XMLSchema-instance"); + + SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext(namespaces); + + xpather = XMLUnit.newXpathEngine(); + xpather.setNamespaceContext(namespaceContext); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + Document readDoc = expectedXMLDocument(filename); + + DOMHandle dHandle = new DOMHandle(); + dHandle.set(readDoc); + before = dHandle.toBuffer(); + strBefore = new String(before); + System.out.println("Before: " + strBefore); + + // write doc + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + String docId = uri + filename; + docMgr.write(docId, dHandle); + + // read doc + docMgr.read(docId, dHandle); + dHandle.get(); + + after = dHandle.toBuffer(); + strAfter = new String(after); + System.out.println("After: " + strAfter); + + assertXMLEqual("Buffer is not the same", strBefore, strAfter); + + // release client + client.release(); + } + + @Test + public void testBug18026WithJson() throws IOException, SAXException, ParserConfigurationException + { + String filename = "json-original.json"; + String uri = "/write-buffer/"; + byte[] before = null; + byte[] after = null; + String strBefore = null; + String strAfter = null; + + System.out.println("Running testBug18026WithJson"); + + XMLUnit.setIgnoreAttributeOrder(true); + XMLUnit.setIgnoreWhitespace(true); + XMLUnit.setNormalize(true); + XMLUnit.setNormalizeWhitespace(true); + XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true); + + HashMap namespaces = new HashMap(); + namespaces.put("rapi", "http://marklogic.com/rest-api"); + namespaces.put("prop", "http://marklogic.com/xdmp/property"); + namespaces.put("xs", "http://www.w3.org/2001/XMLSchema"); + namespaces.put("xsi", "http://www.w3.org/2001/XMLSchema-instance"); + + SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext(namespaces); + + xpather = XMLUnit.newXpathEngine(); + xpather.setNamespaceContext(namespaceContext); + + ObjectMapper mapper = new ObjectMapper(); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + InputStream inputStream = new FileInputStream("src/test/java/com/marklogic/javaclient/data/" + filename); + + InputStreamHandle isHandle = new InputStreamHandle(); + isHandle.set(inputStream); + before = isHandle.toBuffer(); + strBefore = new String(before); + System.out.println("Before: " + strBefore); + + JsonNode contentBefore = mapper.readValue(strBefore, JsonNode.class); + + // write doc + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + String docId = uri + filename; + docMgr.write(docId, isHandle); + + // read doc + docMgr.read(docId, isHandle); + isHandle.get(); + + after = isHandle.toBuffer(); + strAfter = new String(after); + System.out.println("After: " + strAfter); + + JsonNode contentAfter = mapper.readValue(strAfter, JsonNode.class); + + assertTrue("Buffered JSON document difference", contentBefore.equals(contentAfter)); + + // release client + client.release(); + } +@AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBug18724.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBug18724.java index 6caa60843..d9148e8ac 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBug18724.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBug18724.java @@ -1,248 +1,240 @@ -package com.marklogic.javaclient; - -import java.io.IOException; - -import javax.xml.namespace.QName; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import com.marklogic.client.query.AggregateResult; -import com.marklogic.client.query.KeyValueQueryDefinition; -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.query.StringQueryDefinition; -import com.marklogic.client.query.StructuredQueryBuilder; -import com.marklogic.client.query.StructuredQueryDefinition; -import com.marklogic.client.query.ValuesDefinition; - -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.Transaction; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.ValuesHandle; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.junit.*; -public class TestBug18724 extends BasicJavaClientREST { - - private static String dbName = "Bug18724DB"; - private static String [] fNames = {"Bug18724DB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort = 8011; -@BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - } -@After -public void testCleanUp() throws Exception -{ - clearDB(restPort); - System.out.println("Running clear script"); -} -@SuppressWarnings("deprecation") -@Test - public void testDefaultStringSearch() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testDefaultStringSearch"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create transaction - Transaction transaction1 = client.openTransaction(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/def-string-search/", transaction1, "XML"); - } - - // commit transaction - transaction1.commit(); - - // create transaction - Transaction transaction2 = client.openTransaction(); - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition - StringQueryDefinition querydef = queryMgr.newStringDefinition(); - querydef.setCriteria("0012"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle, transaction2); - - // commit transaction - transaction2.commit(); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='response']//@*[local-name()='total'])", resultDoc); - assertXpathEvaluatesTo("0012", "string(//*[local-name()='result']//*[local-name()='highlight'])", resultDoc); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test - public void testDefaultKeyValueSearch() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testDefaultKeyValueSearch"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create transaction - Transaction transaction1 = client.openTransaction(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/def-keyvalue-search/", transaction1, "XML"); - } - - // commit transaction - transaction1.commit(); - - // create transaction - Transaction transaction2 = client.openTransaction(); - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition - KeyValueQueryDefinition querydef = queryMgr.newKeyValueDefinition(); - querydef.put(queryMgr.newElementLocator(new QName("id")), "0012"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle, transaction2); - - // commit transaction - transaction2.commit(); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='response']//@*[local-name()='total'])", resultDoc); - assertXpathEvaluatesTo("0012", "string(//*[local-name()='result']//*[local-name()='highlight'])", resultDoc); - - // release client - client.release(); - } - - /*public void testDefaultStructuredQueryBuilderSearch() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testDefaultStructuredQueryBuilderSearch"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/def-query-builder-search/", "XML"); - } - - // create transaction - Transaction transaction2 = client.openTransaction(); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); - StructuredQueryDefinition termQuery = qb.term("0012"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(termQuery, resultsHandle, transaction2); - - // commit transaction - transaction2.commit(); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='response']//@*[local-name()='total'])", resultDoc); - assertXpathEvaluatesTo("0012", "string(//*[local-name()='result']//*[local-name()='highlight'])", resultDoc); - - // release client - client.release(); - }*/ - - /*public void testDefaultValuesSearch() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testDefaultValuesSearch"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create transaction - Transaction transaction1 = client.openTransaction(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/def-values-search/", transaction1, "XML"); - } - - // commit transaction - transaction1.commit(); - - // create transaction - Transaction transaction2 = client.openTransaction(); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - ValuesDefinition queryDef = queryMgr.newValuesDefinition("popularity"); - queryDef.setAggregate("sum"); - //queryDef.setName("popularity"); - - // create handle - ValuesHandle valuesHandle = new ValuesHandle(); - queryMgr.values(queryDef, valuesHandle, transaction2); - - // commit transaction - transaction2.commit(); - - // get the result - //Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - AggregateResult[] agg = valuesHandle.getAggregates(); - System.out.println(agg.length); - int first = agg[0].get("xs:int", Integer.class); - System.out.println(first); - - //assertXpathEvaluatesTo("1", "string(//*[local-name()='response']//@*[local-name()='total'])", resultDoc); - //assertXpathEvaluatesTo("0012", "string(//*[local-name()='result']//*[local-name()='highlight'])", resultDoc); - - // release client - client.release(); - }*/ - @AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} +package com.marklogic.javaclient; + +import java.io.IOException; + +import javax.xml.namespace.QName; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import com.marklogic.client.query.KeyValueQueryDefinition; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.StringQueryDefinition; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.Transaction; +import com.marklogic.client.io.DOMHandle; +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.*; +public class TestBug18724 extends BasicJavaClientREST { + + private static String dbName = "Bug18724DB"; + private static String [] fNames = {"Bug18724DB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; +@BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + } +@After +public void testCleanUp() throws Exception +{ + clearDB(restPort); + System.out.println("Running clear script"); +} + +@Test + public void testDefaultStringSearch() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testDefaultStringSearch"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create transaction + Transaction transaction1 = client.openTransaction(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/def-string-search/", transaction1, "XML"); + } + + // commit transaction + transaction1.commit(); + + // create transaction + Transaction transaction2 = client.openTransaction(); + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition + StringQueryDefinition querydef = queryMgr.newStringDefinition(); + querydef.setCriteria("0012"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle, transaction2); + + // commit transaction + transaction2.commit(); + + // get the result + Document resultDoc = resultsHandle.get(); + //System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='response']//@*[local-name()='total'])", resultDoc); + assertXpathEvaluatesTo("0012", "string(//*[local-name()='result']//*[local-name()='highlight'])", resultDoc); + + // release client + client.release(); + } + +@Test + public void testDefaultKeyValueSearch() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testDefaultKeyValueSearch"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create transaction + Transaction transaction1 = client.openTransaction(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/def-keyvalue-search/", transaction1, "XML"); + } + + // commit transaction + transaction1.commit(); + + // create transaction + Transaction transaction2 = client.openTransaction(); + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition + KeyValueQueryDefinition querydef = queryMgr.newKeyValueDefinition(); + querydef.put(queryMgr.newElementLocator(new QName("id")), "0012"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle, transaction2); + + // commit transaction + transaction2.commit(); + + // get the result + Document resultDoc = resultsHandle.get(); + //System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='response']//@*[local-name()='total'])", resultDoc); + assertXpathEvaluatesTo("0012", "string(//*[local-name()='result']//*[local-name()='highlight'])", resultDoc); + + // release client + client.release(); + } + + /*public void testDefaultStructuredQueryBuilderSearch() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testDefaultStructuredQueryBuilderSearch"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/def-query-builder-search/", "XML"); + } + + // create transaction + Transaction transaction2 = client.openTransaction(); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); + StructuredQueryDefinition termQuery = qb.term("0012"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(termQuery, resultsHandle, transaction2); + + // commit transaction + transaction2.commit(); + + // get the result + Document resultDoc = resultsHandle.get(); + //System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='response']//@*[local-name()='total'])", resultDoc); + assertXpathEvaluatesTo("0012", "string(//*[local-name()='result']//*[local-name()='highlight'])", resultDoc); + + // release client + client.release(); + }*/ + + /*public void testDefaultValuesSearch() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testDefaultValuesSearch"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create transaction + Transaction transaction1 = client.openTransaction(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/def-values-search/", transaction1, "XML"); + } + + // commit transaction + transaction1.commit(); + + // create transaction + Transaction transaction2 = client.openTransaction(); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + ValuesDefinition queryDef = queryMgr.newValuesDefinition("popularity"); + queryDef.setAggregate("sum"); + //queryDef.setName("popularity"); + + // create handle + ValuesHandle valuesHandle = new ValuesHandle(); + queryMgr.values(queryDef, valuesHandle, transaction2); + + // commit transaction + transaction2.commit(); + + // get the result + //Document resultDoc = resultsHandle.get(); + //System.out.println(convertXMLDocumentToString(resultDoc)); + + AggregateResult[] agg = valuesHandle.getAggregates(); + System.out.println(agg.length); + int first = agg[0].get("xs:int", Integer.class); + System.out.println(first); + + //assertXpathEvaluatesTo("1", "string(//*[local-name()='response']//@*[local-name()='total'])", resultDoc); + //assertXpathEvaluatesTo("0012", "string(//*[local-name()='result']//*[local-name()='highlight'])", resultDoc); + + // release client + client.release(); + }*/ + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBug18736.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBug18736.java index 7fd0f0767..d72b96885 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBug18736.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBug18736.java @@ -1,108 +1,101 @@ -package com.marklogic.javaclient; - -import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; -import static org.junit.Assert.*; - -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.HashMap; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import com.marklogic.client.document.DocumentDescriptor; -import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.io.Format; - -import org.custommonkey.xmlunit.SimpleNamespaceContext; -import org.custommonkey.xmlunit.XMLUnit; -import org.custommonkey.xmlunit.XpathEngine; -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.InputStreamHandle; -import org.junit.*; -public class TestBug18736 extends BasicJavaClientREST { - - private static String dbName = "Bug18736DB"; - private static String [] fNames = {"Bug18736DB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; -@BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - } - -@SuppressWarnings("deprecation") -@Test - public void testBug18736() throws XpathException, TransformerException, ParserConfigurationException, SAXException, IOException - { - System.out.println("Running testBug18736"); - - String filename = "constraint1.xml"; - String docId = "/content/without-xml-ext"; - //XpathEngine xpathEngine; - - /*HashMap xpathNS = new HashMap(); - xpathNS.put("", "http://purl.org/dc/elements/1.1/"); - SimpleNamespaceContext xpathNsContext = new SimpleNamespaceContext(xpathNS); - - XMLUnit.setIgnoreAttributeOrder(true); - XMLUnit.setIgnoreWhitespace(true); - XMLUnit.setNormalize(true); - XMLUnit.setNormalizeWhitespace(true); - XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true); - - xpathEngine = XMLUnit.newXpathEngine(); - xpathEngine.setNamespaceContext(xpathNsContext);*/ - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - - // create write handle - InputStreamHandle writeHandle = new InputStreamHandle(); - - // get the file - InputStream inputStream = new FileInputStream("src/test/java/com/marklogic/javaclient/data/" + filename); - - writeHandle.set(inputStream); - - // create doc descriptor - DocumentDescriptor docDesc = docMgr.newDescriptor(docId); - - docMgr.write(docDesc, writeHandle); - - docDesc.setFormat(Format.XML); - DOMHandle readHandle = new DOMHandle(); - docMgr.read(docDesc, readHandle); - Document readDoc = readHandle.get(); - String out = convertXMLDocumentToString(readDoc); - System.out.println(out); - - assertTrue("Unable to read doc", out.contains("0011")); - - // get xml document for expected result - //Document expectedDoc = expectedXMLDocument(filename); - - //assertXMLEqual("Write XML difference", expectedDoc, readDoc); - - // release client - client.release(); - } -@AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import com.marklogic.client.document.DocumentDescriptor; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.io.Format; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.InputStreamHandle; +import org.junit.*; +public class TestBug18736 extends BasicJavaClientREST { + + private static String dbName = "Bug18736DB"; + private static String [] fNames = {"Bug18736DB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; +@BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + } + +@Test + public void testBug18736() throws XpathException, TransformerException, ParserConfigurationException, SAXException, IOException + { + System.out.println("Running testBug18736"); + + String filename = "constraint1.xml"; + String docId = "/content/without-xml-ext"; + //XpathEngine xpathEngine; + + /*HashMap xpathNS = new HashMap(); + xpathNS.put("", "http://purl.org/dc/elements/1.1/"); + SimpleNamespaceContext xpathNsContext = new SimpleNamespaceContext(xpathNS); + + XMLUnit.setIgnoreAttributeOrder(true); + XMLUnit.setIgnoreWhitespace(true); + XMLUnit.setNormalize(true); + XMLUnit.setNormalizeWhitespace(true); + XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true); + + xpathEngine = XMLUnit.newXpathEngine(); + xpathEngine.setNamespaceContext(xpathNsContext);*/ + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + + // create write handle + InputStreamHandle writeHandle = new InputStreamHandle(); + + // get the file + InputStream inputStream = new FileInputStream("src/test/java/com/marklogic/javaclient/data/" + filename); + + writeHandle.set(inputStream); + + // create doc descriptor + DocumentDescriptor docDesc = docMgr.newDescriptor(docId); + + docMgr.write(docDesc, writeHandle); + + docDesc.setFormat(Format.XML); + DOMHandle readHandle = new DOMHandle(); + docMgr.read(docDesc, readHandle); + Document readDoc = readHandle.get(); + String out = convertXMLDocumentToString(readDoc); + System.out.println(out); + + assertTrue("Unable to read doc", out.contains("0011")); + + // get xml document for expected result + //Document expectedDoc = expectedXMLDocument(filename); + + //assertXMLEqual("Write XML difference", expectedDoc, readDoc); + + // release client + client.release(); + } +@AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBug18990.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBug18990.java index 53e266a12..a8e18846d 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBug18990.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBug18990.java @@ -1,95 +1,94 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.admin.ServerConfigurationManager; -import com.marklogic.client.query.StructuredQueryBuilder; -import com.marklogic.client.query.StructuredQueryDefinition; -import com.marklogic.client.io.Format; -import com.marklogic.client.io.StringHandle; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.junit.*; -public class TestBug18990 extends BasicJavaClientREST { - - private static String dbName = "TestBug18990DB"; - private static String [] fNames = {"TestBug18990DB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; -@BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - } - -@SuppressWarnings("deprecation") -@Test - public void testBug18990() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testBug18990"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "valueConstraintWildCardOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true and server logger to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setServerRequestLogging(true); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/bug18990/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); - StructuredQueryDefinition valueConstraintQuery1 = qb.valueConstraint("id", "00*2"); - StructuredQueryDefinition valueConstraintQuery2 = qb.valueConstraint("id", "0??6"); - StructuredQueryDefinition orFinalQuery = qb.or(valueConstraintQuery1, valueConstraintQuery2); - - // create handle - StringHandle resultsHandle = new StringHandle().withFormat(Format.JSON); - queryMgr.search(orFinalQuery, resultsHandle); - - // get the result - String resultDoc = resultsHandle.get(); - - System.out.println(resultDoc); - - assertTrue("Result in json is not correct", resultDoc.contains("{\"snippet-format\":\"raw\",\"total\":4,\"start\":1,\"page-length\":10,\"results\":[{\"index\":1,\"uri\":\"/bug18990/constraint5.xml\"")); - - // turn off server logger - srvMgr.setServerRequestLogging(false); - srvMgr.writeConfiguration(); - - // release client - client.release(); - } - @AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.admin.ServerConfigurationManager; +import com.marklogic.client.query.StructuredQueryBuilder; +import com.marklogic.client.query.StructuredQueryDefinition; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.StringHandle; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.*; +public class TestBug18990 extends BasicJavaClientREST { + + private static String dbName = "TestBug18990DB"; + private static String [] fNames = {"TestBug18990DB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; +@BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + } + +@Test + public void testBug18990() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testBug18990"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "valueConstraintWildCardOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true and server logger to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setServerRequestLogging(true); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/bug18990/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); + StructuredQueryDefinition valueConstraintQuery1 = qb.valueConstraint("id", "00*2"); + StructuredQueryDefinition valueConstraintQuery2 = qb.valueConstraint("id", "0??6"); + StructuredQueryDefinition orFinalQuery = qb.or(valueConstraintQuery1, valueConstraintQuery2); + + // create handle + StringHandle resultsHandle = new StringHandle().withFormat(Format.JSON); + queryMgr.search(orFinalQuery, resultsHandle); + + // get the result + String resultDoc = resultsHandle.get(); + + System.out.println(resultDoc); + + assertTrue("Result in json is not correct", resultDoc.contains("{\"snippet-format\":\"raw\",\"total\":4,\"start\":1,\"page-length\":10,\"results\":[{\"index\":1,\"uri\":\"/bug18990/constraint5.xml\"")); + + // turn off server logger + srvMgr.setServerRequestLogging(false); + srvMgr.writeConfiguration(); + + // release client + client.release(); + } + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBug18993.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBug18993.java index b0334ff66..f27686d1d 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBug18993.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBug18993.java @@ -1,75 +1,74 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.IOException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.io.StringHandle; -import com.marklogic.javaclient.BasicJavaClientREST; - -import org.junit.*; -public class TestBug18993 extends BasicJavaClientREST { - - private static String dbName = "Bug18993DB"; - private static String [] fNames = {"Bug18993DB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort = 8011; - - @BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - loadBug18993(); - } - -@After - public void testCleanUp() throws Exception - { - clearDB(restPort); - System.out.println("Running clear script"); - } - - @SuppressWarnings("deprecation") - @Test - public void testBug18993() throws IOException - { - System.out.println("Running testBug18993"); - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - - StringHandle readHandle = new StringHandle(); - - String uris[] = {"/a b"}; - - String expectedXML = "a space b"; - - for (String uri : uris) - { - System.out.println("uri = " + uri); - docMgr.read(uri, readHandle); - System.out.println(); - String strXML = readHandle.toString(); - System.out.print(readHandle.toString()); - assertTrue("Document is not returned", strXML.contains(expectedXML)); - System.out.println(); - } - - // release client - client.release(); - } - @AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames,restServerName); - - } -} - +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.IOException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.io.StringHandle; +import com.marklogic.javaclient.BasicJavaClientREST; + +import org.junit.*; +public class TestBug18993 extends BasicJavaClientREST { + + private static String dbName = "Bug18993DB"; + private static String [] fNames = {"Bug18993DB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + loadBug18993(); + } + +@After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + + @Test + public void testBug18993() throws IOException + { + System.out.println("Running testBug18993"); + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + + StringHandle readHandle = new StringHandle(); + + String uris[] = {"/a b"}; + + String expectedXML = "a space b"; + + for (String uri : uris) + { + System.out.println("uri = " + uri); + docMgr.read(uri, readHandle); + System.out.println(); + String strXML = readHandle.toString(); + System.out.print(readHandle.toString()); + assertTrue("Document is not returned", strXML.contains(expectedXML)); + System.out.println(); + } + + // release client + client.release(); + } + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames,restServerName); + + } +} + diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBug19016.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBug19016.java index 3f7929be4..bd2ad81ed 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBug19016.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBug19016.java @@ -1,70 +1,69 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; - -import com.marklogic.client.query.QueryManager; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.query.StringQueryDefinition; -import com.marklogic.client.io.StringHandle; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.junit.*; -public class TestBug19016 extends BasicJavaClientREST { - - private static String dbName = "Bug19016DB"; - private static String [] fNames = {"Bug19016DB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; -@BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - } - - @SuppressWarnings("deprecation") - @Test - public void testBug19016() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testBug19016"); - - String[] filenames = {"bug19016.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/bug19016/", "XML"); - } - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(); - querydef.setCriteria("this\"is\"an%33odd string"); - - String result = queryMgr.search(querydef, new StringHandle()).get(); - - System.out.println(result); - - assertTrue("qtext is not correct", result.contains("this\"is\"an%33odd string")); - - // release client - client.release(); - } -@AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; + +import com.marklogic.client.query.QueryManager; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.query.StringQueryDefinition; +import com.marklogic.client.io.StringHandle; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.*; +public class TestBug19016 extends BasicJavaClientREST { + + private static String dbName = "Bug19016DB"; + private static String [] fNames = {"Bug19016DB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; +@BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + } + + @Test + public void testBug19016() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testBug19016"); + + String[] filenames = {"bug19016.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/bug19016/", "XML"); + } + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(); + querydef.setCriteria("this\"is\"an%33odd string"); + + String result = queryMgr.search(querydef, new StringHandle()).get(); + + System.out.println(result); + + assertTrue("qtext is not correct", result.contains("this\"is\"an%33odd string")); + + // release client + client.release(); + } +@AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBug19046.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBug19046.java index 675dda5a0..0d1b73ae2 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBug19046.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBug19046.java @@ -1,73 +1,72 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.admin.QueryOptionsManager; -import com.marklogic.client.io.Format; -import com.marklogic.client.io.StringHandle; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.junit.*; -public class TestBug19046 extends BasicJavaClientREST { - - private static String dbName = "Bug19046DB"; - private static String [] fNames = {"Bug19046DB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; -@BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - } - -@SuppressWarnings("deprecation") -@Test - public void testBug19046() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testBug19046"); - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create query options manager - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // read non-existent query option - StringHandle readHandle = new StringHandle(); - readHandle.setFormat(Format.XML); - - String expectedException = "com.marklogic.client.ResourceNotFoundException: Could not get /config/query/NonExistentOpt"; - - String exception = ""; - - try - { - optionsMgr.readOptions("NonExistentOpt", readHandle); - } - catch (Exception e) { exception = e.toString(); } - - System.out.println(exception); - - assertTrue("Exception is not thrown", exception.contains(expectedException)); - - // release client - client.release(); - } - @AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.admin.QueryOptionsManager; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.StringHandle; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.*; +public class TestBug19046 extends BasicJavaClientREST { + + private static String dbName = "Bug19046DB"; + private static String [] fNames = {"Bug19046DB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; +@BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + } + +@Test + public void testBug19046() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testBug19046"); + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create query options manager + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // read non-existent query option + StringHandle readHandle = new StringHandle(); + readHandle.setFormat(Format.XML); + + String expectedException = "com.marklogic.client.ResourceNotFoundException: Could not get /config/query/NonExistentOpt"; + + String exception = ""; + + try + { + optionsMgr.readOptions("NonExistentOpt", readHandle); + } + catch (Exception e) { exception = e.toString(); } + + System.out.println(exception); + + assertTrue("Exception is not thrown", exception.contains(expectedException)); + + // release client + client.release(); + } + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBug19144.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBug19144.java index d9e7bd943..97984b372 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBug19144.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBug19144.java @@ -1,122 +1,120 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.query.ValuesDefinition; - -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.Format; -import com.marklogic.client.io.StringHandle; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.junit.*; -public class TestBug19144 extends BasicJavaClientREST { - - private static String dbName = "TestBug19144DB"; - private static String [] fNames = {"TestBug19144DB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; -@BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - } - -@SuppressWarnings("deprecation") -@Test - public void testBug19144WithJson() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testBug19144WithJson"); - - String[] filenames = {"aggr1.xml", "aggr2.xml", "aggr3.xml", "aggr4.xml", "aggr5.xml"}; - String queryOptionName = "aggregatesOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/bug19144/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - ValuesDefinition queryDef = queryMgr.newValuesDefinition("popularity", "aggregatesOpt.xml"); - queryDef.setAggregate("correlation", "covariance"); - queryDef.setName("pop-rate-tups"); - - // create handle - StringHandle resultHandle = new StringHandle().withFormat(Format.JSON); - queryMgr.tuples(queryDef, resultHandle); - - String result = resultHandle.get(); - - System.out.println(result); - - assertEquals("{", result.substring(0, 1)); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test - public void testBug19144WithXml() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testBug19144WithXml"); - - String[] filenames = {"aggr1.xml", "aggr2.xml", "aggr3.xml", "aggr4.xml", "aggr5.xml"}; - String queryOptionName = "aggregatesOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/bug19144/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - ValuesDefinition queryDef = queryMgr.newValuesDefinition("popularity", "aggregatesOpt.xml"); - queryDef.setAggregate("correlation", "covariance"); - queryDef.setName("pop-rate-tups"); - - // create handle - StringHandle resultHandle = new StringHandle().withFormat(Format.XML); - queryMgr.tuples(queryDef, resultHandle); - - String result = resultHandle.get(); - - System.out.println(result); - - assertEquals("<", result.substring(0, 1)); - - // release client - client.release(); - } - @AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.ValuesDefinition; + +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.StringHandle; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.*; +public class TestBug19144 extends BasicJavaClientREST { + + private static String dbName = "TestBug19144DB"; + private static String [] fNames = {"TestBug19144DB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; +@BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + } + +@Test + public void testBug19144WithJson() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testBug19144WithJson"); + + String[] filenames = {"aggr1.xml", "aggr2.xml", "aggr3.xml", "aggr4.xml", "aggr5.xml"}; + String queryOptionName = "aggregatesOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/bug19144/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + ValuesDefinition queryDef = queryMgr.newValuesDefinition("popularity", "aggregatesOpt.xml"); + queryDef.setAggregate("correlation", "covariance"); + queryDef.setName("pop-rate-tups"); + + // create handle + StringHandle resultHandle = new StringHandle().withFormat(Format.JSON); + queryMgr.tuples(queryDef, resultHandle); + + String result = resultHandle.get(); + + System.out.println(result); + + assertEquals("{", result.substring(0, 1)); + + // release client + client.release(); + } + +@Test + public void testBug19144WithXml() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testBug19144WithXml"); + + String[] filenames = {"aggr1.xml", "aggr2.xml", "aggr3.xml", "aggr4.xml", "aggr5.xml"}; + String queryOptionName = "aggregatesOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/bug19144/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + ValuesDefinition queryDef = queryMgr.newValuesDefinition("popularity", "aggregatesOpt.xml"); + queryDef.setAggregate("correlation", "covariance"); + queryDef.setName("pop-rate-tups"); + + // create handle + StringHandle resultHandle = new StringHandle().withFormat(Format.XML); + queryMgr.tuples(queryDef, resultHandle); + + String result = resultHandle.get(); + + System.out.println(result); + + assertEquals("<", result.substring(0, 1)); + + // release client + client.release(); + } + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBug19389.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBug19389.java index e7528b03e..d0f6b21cf 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBug19389.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBug19389.java @@ -1,79 +1,78 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.admin.QueryOptionsManager; -import com.marklogic.client.admin.ServerConfigurationManager; -import com.marklogic.client.io.Format; -import com.marklogic.client.io.StringHandle; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.junit.*; -public class TestBug19389 extends BasicJavaClientREST { - - private static String dbName = "Bug19389DB"; - private static String [] fNames = {"Bug19389DB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; -@BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - } - -@SuppressWarnings("deprecation") -@Test - public void testBug19389() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testBug19389"); - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set error format to JSON - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.setErrorFormat(Format.JSON); - srvMgr.writeConfiguration(); - - // create query options manager - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // read non-existent query option - StringHandle readHandle = new StringHandle(); - readHandle.setFormat(Format.XML); - - String expectedException = "com.marklogic.client.ResourceNotFoundException: Could not get /config/query/NonExistentOpt"; - - String exception = ""; - - try - { - optionsMgr.readOptions("NonExistentOpt", readHandle); - } - catch (Exception e) { exception = e.toString(); } - - System.out.println(exception); - - assertTrue("Exception is not thrown", exception.contains(expectedException)); - - // release client - client.release(); - } -@AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.admin.QueryOptionsManager; +import com.marklogic.client.admin.ServerConfigurationManager; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.StringHandle; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.*; +public class TestBug19389 extends BasicJavaClientREST { + + private static String dbName = "Bug19389DB"; + private static String [] fNames = {"Bug19389DB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; +@BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + } + +@Test + public void testBug19389() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testBug19389"); + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set error format to JSON + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.setErrorFormat(Format.JSON); + srvMgr.writeConfiguration(); + + // create query options manager + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // read non-existent query option + StringHandle readHandle = new StringHandle(); + readHandle.setFormat(Format.XML); + + String expectedException = "com.marklogic.client.ResourceNotFoundException: Could not get /config/query/NonExistentOpt"; + + String exception = ""; + + try + { + optionsMgr.readOptions("NonExistentOpt", readHandle); + } + catch (Exception e) { exception = e.toString(); } + + System.out.println(exception); + + assertTrue("Exception is not thrown", exception.contains(expectedException)); + + // release client + client.release(); + } +@AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBug20979.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBug20979.java index b848bba32..e47f5d569 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBug20979.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBug20979.java @@ -1,86 +1,82 @@ -package com.marklogic.javaclient; - -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import com.marklogic.client.query.QueryManager; -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.query.StringQueryDefinition; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.SearchHandle; -import com.marklogic.client.io.StringHandle; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; -import static org.junit.Assert.*; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.junit.*; -public class TestBug20979 extends BasicJavaClientREST { - - private static String dbName = "TestBug20979DB"; - private static String [] fNames = {"TestBug20979DB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; -@BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - } - -@SuppressWarnings("deprecation") -@Test - public void testBug20979() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testBug20979"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "wordConstraintWithElementAndAttributeIndexPlanOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/word-constraint/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("intitle:1945 OR inprice:12"); - - // create handle - SearchHandle resultsHandle = new SearchHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - //Document resultDoc = resultsHandle.getPlan(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - String strPlan = resultsHandle.getPlan(new StringHandle()).get(); - System.out.println(strPlan); - - assertTrue("string is not matched", strPlan.contains("qry:result estimate=\"3\"")); - - // release client - client.release(); - } -@AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} +package com.marklogic.javaclient; + +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import com.marklogic.client.query.QueryManager; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.query.StringQueryDefinition; +import com.marklogic.client.io.SearchHandle; +import com.marklogic.client.io.StringHandle; + +import static org.junit.Assert.*; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.*; +public class TestBug20979 extends BasicJavaClientREST { + + private static String dbName = "TestBug20979DB"; + private static String [] fNames = {"TestBug20979DB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; +@BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + } + +@Test + public void testBug20979() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testBug20979"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "wordConstraintWithElementAndAttributeIndexPlanOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/word-constraint/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("intitle:1945 OR inprice:12"); + + // create handle + SearchHandle resultsHandle = new SearchHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + //Document resultDoc = resultsHandle.getPlan(); + //System.out.println(convertXMLDocumentToString(resultDoc)); + + String strPlan = resultsHandle.getPlan(new StringHandle()).get(); + System.out.println(strPlan); + + assertTrue("string is not matched", strPlan.contains("qry:result estimate=\"3\"")); + + // release client + client.release(); + } +@AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBug21159.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBug21159.java index 516229255..b07317325 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBug21159.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBug21159.java @@ -1,160 +1,152 @@ -package com.marklogic.javaclient; - -import java.io.File; -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import com.marklogic.client.query.CountedDistinctValue; -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.query.RawCombinedQueryDefinition; -import com.marklogic.client.query.Tuple; -import com.marklogic.client.query.ValuesDefinition; - -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.admin.ServerConfigurationManager; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.DocumentMetadataHandle; -import com.marklogic.client.io.FileHandle; -import com.marklogic.client.io.Format; -import com.marklogic.client.io.StringHandle; -import com.marklogic.client.io.TuplesHandle; -import com.marklogic.client.io.ValuesHandle; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; -import static org.junit.Assert.*; - - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.junit.*; -public class TestBug21159 extends BasicJavaClientREST { - - private static String dbName = "TestRawCombinedQueryDB"; - private static String [] fNames = {"TestRawCombinedQueryDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; -@BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - - addRangeElementIndex(dbName, "string", "", "grandchild", "http://marklogic.com/collation/"); - addRangeElementIndex(dbName, "double", "", "double"); - addRangeElementIndex(dbName, "int", "", "int"); - addRangeElementIndex(dbName, "string", "", "string", "http://marklogic.com/collation/"); - } - -@SuppressWarnings("deprecation") -@Test -public void testBug21159Tuples() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testBug21159Tuples"); - - String[] filenames = {"tuples-test1.xml", "tuples-test2.xml", "tuples-test3.xml", "tuples-test4.xml", "lexicon-test1.xml","lexicon-test2.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/raw-combined-query/", "XML"); - } - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/LexiconOptions.xml"); - - String combinedQuery = convertFileToString(file); - - RawCombinedQueryDefinition rawCombinedQueryDefinition; - QueryManager queryMgr = client.newQueryManager(); - rawCombinedQueryDefinition = queryMgr.newRawCombinedQueryDefinition(new StringHandle(combinedQuery).withMimetype("application/xml")); - - StringHandle stringResults = null; - ValuesDefinition vdef = queryMgr.newValuesDefinition("grandchild"); - - vdef.setQueryDefinition(rawCombinedQueryDefinition); - - stringResults = queryMgr.tuples(vdef, new StringHandle()); - System.out.println(stringResults.get()); - - ValuesHandle valuesResults = queryMgr.values(vdef,new ValuesHandle()); - - assertFalse(valuesResults.getMetrics().getTotalTime() == -1); - - CountedDistinctValue[] values = valuesResults.getValues(); - - assertNotNull(values); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test -public void testBug21159Values() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException -{ - System.out.println("Running testBug21159Values"); - - String[] filenames = {"tuples-test1.xml", "tuples-test2.xml", "tuples-test3.xml", "tuples-test4.xml", "lexicon-test1.xml","lexicon-test2.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/raw-combined-query/", "XML"); - } - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/LexiconOptions.xml"); - - String combinedQuery = convertFileToString(file); - - RawCombinedQueryDefinition rawCombinedQueryDefinition; - QueryManager queryMgr = client.newQueryManager(); - rawCombinedQueryDefinition = queryMgr.newRawCombinedQueryDefinition(new StringHandle(combinedQuery).withMimetype("application/xml")); - - StringHandle stringResults = null; - ValuesDefinition vdef = queryMgr.newValuesDefinition("n-way"); - - vdef.setQueryDefinition(rawCombinedQueryDefinition); - - stringResults = queryMgr.tuples(vdef, new StringHandle()); - System.out.println(stringResults.get()); - - TuplesHandle tuplesResults = queryMgr.tuples(vdef, - new TuplesHandle()); - Tuple[] tuples = tuplesResults.getTuples(); - assertNotNull(tuples); - - // release client - client.release(); -} - -@AfterClass -public static void tearDown() throws Exception -{ - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - -} +package com.marklogic.javaclient; + +import java.io.File; +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import com.marklogic.client.query.CountedDistinctValue; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.RawCombinedQueryDefinition; +import com.marklogic.client.query.Tuple; +import com.marklogic.client.query.ValuesDefinition; + +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.admin.ServerConfigurationManager; +import com.marklogic.client.io.StringHandle; +import com.marklogic.client.io.TuplesHandle; +import com.marklogic.client.io.ValuesHandle; + +import static org.junit.Assert.*; + + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.*; +public class TestBug21159 extends BasicJavaClientREST { + + private static String dbName = "TestRawCombinedQueryDB"; + private static String [] fNames = {"TestRawCombinedQueryDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; +@BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + + addRangeElementIndex(dbName, "string", "", "grandchild", "http://marklogic.com/collation/"); + addRangeElementIndex(dbName, "double", "", "double"); + addRangeElementIndex(dbName, "int", "", "int"); + addRangeElementIndex(dbName, "string", "", "string", "http://marklogic.com/collation/"); + } + +@Test +public void testBug21159Tuples() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testBug21159Tuples"); + + String[] filenames = {"tuples-test1.xml", "tuples-test2.xml", "tuples-test3.xml", "tuples-test4.xml", "lexicon-test1.xml","lexicon-test2.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/raw-combined-query/", "XML"); + } + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/LexiconOptions.xml"); + + String combinedQuery = convertFileToString(file); + + RawCombinedQueryDefinition rawCombinedQueryDefinition; + QueryManager queryMgr = client.newQueryManager(); + rawCombinedQueryDefinition = queryMgr.newRawCombinedQueryDefinition(new StringHandle(combinedQuery).withMimetype("application/xml")); + + StringHandle stringResults = null; + ValuesDefinition vdef = queryMgr.newValuesDefinition("grandchild"); + + vdef.setQueryDefinition(rawCombinedQueryDefinition); + + stringResults = queryMgr.tuples(vdef, new StringHandle()); + System.out.println(stringResults.get()); + + ValuesHandle valuesResults = queryMgr.values(vdef,new ValuesHandle()); + + assertFalse(valuesResults.getMetrics().getTotalTime() == -1); + + CountedDistinctValue[] values = valuesResults.getValues(); + + assertNotNull(values); + + // release client + client.release(); + } + +@Test +public void testBug21159Values() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException +{ + System.out.println("Running testBug21159Values"); + + String[] filenames = {"tuples-test1.xml", "tuples-test2.xml", "tuples-test3.xml", "tuples-test4.xml", "lexicon-test1.xml","lexicon-test2.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/raw-combined-query/", "XML"); + } + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/LexiconOptions.xml"); + + String combinedQuery = convertFileToString(file); + + RawCombinedQueryDefinition rawCombinedQueryDefinition; + QueryManager queryMgr = client.newQueryManager(); + rawCombinedQueryDefinition = queryMgr.newRawCombinedQueryDefinition(new StringHandle(combinedQuery).withMimetype("application/xml")); + + StringHandle stringResults = null; + ValuesDefinition vdef = queryMgr.newValuesDefinition("n-way"); + + vdef.setQueryDefinition(rawCombinedQueryDefinition); + + stringResults = queryMgr.tuples(vdef, new StringHandle()); + System.out.println(stringResults.get()); + + TuplesHandle tuplesResults = queryMgr.tuples(vdef, + new TuplesHandle()); + Tuple[] tuples = tuplesResults.getTuples(); + assertNotNull(tuples); + + // release client + client.release(); +} + +@AfterClass +public static void tearDown() throws Exception +{ + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + +} } \ No newline at end of file diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBug22037.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBug22037.java index a14848d46..f1915f86c 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBug22037.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBug22037.java @@ -1,82 +1,80 @@ -package com.marklogic.javaclient; - -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.query.StringQueryDefinition; -import com.marklogic.client.query.StructuredQueryBuilder; -import com.marklogic.client.query.StructuredQueryBuilder.Operator; -import com.marklogic.client.query.StructuredQueryDefinition; -import com.marklogic.client.io.DOMHandle; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.junit.*; -public class TestBug22037 extends BasicJavaClientREST { - - - private static String dbName = "TestBug22037DB"; - private static String [] fNames = {"TestBug22037DB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; -@BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - } - -@SuppressWarnings("deprecation") -@Test - public void testBug22037() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testBug22037"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "rangeConstraintIntOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/range-constraint/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); - StructuredQueryDefinition rangeQuery = qb.range(qb.element("popularity"), "xs:int", Operator.GE, 4); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(rangeQuery, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("4", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - - // release client - client.release(); - } -@AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} +package com.marklogic.javaclient; + +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.StructuredQueryBuilder; +import com.marklogic.client.query.StructuredQueryBuilder.Operator; +import com.marklogic.client.query.StructuredQueryDefinition; +import com.marklogic.client.io.DOMHandle; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.*; +public class TestBug22037 extends BasicJavaClientREST { + + + private static String dbName = "TestBug22037DB"; + private static String [] fNames = {"TestBug22037DB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; +@BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + } + +@Test + public void testBug22037() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testBug22037"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "rangeConstraintIntOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/range-constraint/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); + StructuredQueryDefinition rangeQuery = qb.range(qb.element("popularity"), "xs:int", Operator.GE, 4); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(rangeQuery, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + //System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("4", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + + // release client + client.release(); + } +@AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBug26248.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBug26248.java index 1f36621e5..784fc11a9 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBug26248.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBug26248.java @@ -1,90 +1,88 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.ByteArrayInputStream; -import java.io.InputStream; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.document.TextDocumentManager; -import com.marklogic.client.io.InputStreamHandle; -import com.marklogic.client.io.StringHandle; - -public class TestBug26248 { - DatabaseClient client ; - @Before - public void setUp() throws Exception { - - } - - @After - public void tearDown() throws Exception { - client.release(); - } - - static class DocWriter implements Runnable { - static boolean isResendable = true; - String id; - String resendableDoc; - InputStream onetimeDoc; - TextDocumentManager docMgr; - DocWriter(DatabaseClient client, String id, String doc) throws Exception { - this.id = id; - if (isResendable) - this.resendableDoc = doc; - else - this.onetimeDoc = new ByteArrayInputStream(doc.getBytes("UTF-8")); - docMgr = client.newTextDocumentManager(); - } - @Override - public void run() { - try { - System.out.println("sleeping "+id); - Thread.sleep(1000); - } catch (InterruptedException e) { - } - runImpl(); - } - public void runImpl() { - System.out.println("writing "+id); - if (isResendable) - docMgr.write(id, new StringHandle(resendableDoc)); - else - docMgr.write(id, new InputStreamHandle(onetimeDoc)); - System.out.println(id+"="+docMgr.read(id, new StringHandle()).get()); - docMgr.delete(id); - System.out.println("finished "+id); - } - } - - - @Test - public void testBug26248() throws Exception { - try{ - DocWriter.isResendable = false; - client= DatabaseClientFactory.newClient( - "localhost", 8011, "rest-writer", "x", Authentication.DIGEST - ); - DocWriter dw0 = new DocWriter(client, "/tmp/test0.txt", "The zeroth text"); - DocWriter dw1 = new DocWriter(client, "/tmp/test1.txt", "The first text"); - DocWriter dw2 = new DocWriter(client, "/tmp/test2.txt", "The second text"); - - Thread t1 = new Thread(dw1); - Thread t2 = new Thread(dw2); - - t1.start(); - t2.start(); - dw0.runImpl(); - Thread.sleep(2000); - }catch(Exception e){ - throw e; - } - } - -} +package com.marklogic.javaclient; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.TextDocumentManager; +import com.marklogic.client.io.InputStreamHandle; +import com.marklogic.client.io.StringHandle; + +public class TestBug26248 { + DatabaseClient client ; + @Before + public void setUp() throws Exception { + + } + + @After + public void tearDown() throws Exception { + client.release(); + } + + static class DocWriter implements Runnable { + static boolean isResendable = true; + String id; + String resendableDoc; + InputStream onetimeDoc; + TextDocumentManager docMgr; + DocWriter(DatabaseClient client, String id, String doc) throws Exception { + this.id = id; + if (isResendable) + this.resendableDoc = doc; + else + this.onetimeDoc = new ByteArrayInputStream(doc.getBytes("UTF-8")); + docMgr = client.newTextDocumentManager(); + } + @Override + public void run() { + try { + System.out.println("sleeping "+id); + Thread.sleep(1000); + } catch (InterruptedException e) { + } + runImpl(); + } + public void runImpl() { + System.out.println("writing "+id); + if (isResendable) + docMgr.write(id, new StringHandle(resendableDoc)); + else + docMgr.write(id, new InputStreamHandle(onetimeDoc)); + System.out.println(id+"="+docMgr.read(id, new StringHandle()).get()); + docMgr.delete(id); + System.out.println("finished "+id); + } + } + + + @Test + public void testBug26248() throws Exception { + try{ + DocWriter.isResendable = false; + client= DatabaseClientFactory.newClient( + "localhost", 8011, "rest-writer", "x", Authentication.DIGEST + ); + DocWriter dw0 = new DocWriter(client, "/tmp/test0.txt", "The zeroth text"); + DocWriter dw1 = new DocWriter(client, "/tmp/test1.txt", "The first text"); + DocWriter dw2 = new DocWriter(client, "/tmp/test2.txt", "The second text"); + + Thread t1 = new Thread(dw1); + Thread t2 = new Thread(dw2); + + t1.start(); + t2.start(); + dw0.runImpl(); + Thread.sleep(2000); + }catch(Exception e){ + throw e; + } + } + +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBytesHandle.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBytesHandle.java index 428ee0dc4..53adeaf94 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBytesHandle.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBytesHandle.java @@ -1,289 +1,285 @@ -package com.marklogic.javaclient; -import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; -import static org.junit.Assert.*; - -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; - -import org.custommonkey.xmlunit.XMLUnit; -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.BytesHandle; -import com.marklogic.client.io.InputStreamHandle; -import org.junit.*; -public class TestBytesHandle extends BasicJavaClientREST{ - - -private static String dbName = "BytesHandleDB"; -private static String [] fNames = {"BytesHandleDB-1"}; -private static String restServerName = "REST-Java-Client-API-Server"; -@BeforeClass -public static void setUp() throws Exception{ - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - } -@SuppressWarnings("null") -@Test -public void testXmlCRUD() throws IOException , SAXException, ParserConfigurationException{ - - String filename = "xml-original-test.xml"; - String uri = "/write-xml-domhandle/"; - System.out.println("Running testXmlCRUD"); - XMLUnit.setIgnoreWhitespace(true); - XMLUnit.setNormalizeWhitespace(true); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x",Authentication.DIGEST); - - // write docs - writeDocumentUsingBytesHandle(client, filename, uri, null,"XML");//*********** - - //read docs - BytesHandle contentHandle = readDocumentUsingBytesHandle(client, uri + filename,"XML"); - - // get the contents - - byte[] readDoc1 = (byte[])contentHandle.get(); - String readDoc2 = new String(readDoc1); - Document readDoc = convertStringToXMLDocument(readDoc2); - - // get xml document for expected result - Document expectedDoc = expectedXMLDocument(filename); - assertXMLEqual("Write XML difference", expectedDoc, readDoc); - - // Update the Doc - //acquire the content for update - String updateFilename = "xml-updated-test.xml"; - updateDocumentUsingByteHandle(client, updateFilename, uri + filename, "XML"); - - // read the document - BytesHandle updateHandle = readDocumentUsingBytesHandle(client, uri+filename, "XML"); - byte[] readDocUpdateInBytes = updateHandle.get(); - String readDocUpdateInString= new String(readDocUpdateInBytes); - - // convert actual string to xml doc - Document readDocUpdate = convertStringToXMLDocument(readDocUpdateInString); - - // get xml document for expected result - Document expectedDocUpdate = expectedXMLDocument(updateFilename); - assertXMLEqual("Write XML Difference", expectedDocUpdate, readDocUpdate); - - // delete the document - deleteDocument(client, uri+filename,"XML"); - - // read the deleted document - String exception = ""; - try - { - BytesHandle deleteHandle = readDocumentUsingBytesHandle(client, uri+filename, "XML"); - } catch (Exception e) { exception = e.toString(); } - - String expectedException = "com.marklogic.client.ResourceNotFoundException: Local message: Could not read non-existent document. Server Message: RESTAPI-NODOCUMENT: (err:FOER0000) Resource or document does not exist: category: content message: /write-xml-domhandle/xml-original-test.xml"; - assertEquals("Document is not deleted", expectedException, exception); - - //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "XML")); - - // release client - client.release(); - - } - -@SuppressWarnings("deprecation") -@Test -public void testTextCRUD() throws IOException, ParserConfigurationException, SAXException{ - String filename = "text-original.txt"; - String uri = "/write-text-Byteshandle/"; - System.out.println("Runing test TextCRUD"); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - writeDocumentUsingBytesHandle(client, filename, uri, "Text"); - - //read docs - BytesHandle contentHandle = readDocumentUsingBytesHandle(client, uri + filename,"Text"); - - // get the contents - byte[] fileRead = (byte[])contentHandle.get(); - //String readContent = contentHandle.get().toString(); - String readContent = new String(fileRead); - String expectedContent = "hello world, welcome to java API"; - assertEquals("Write Text difference", expectedContent.trim(), readContent.trim()); - - //UPDATE the doc - // acquire the content for update - //String updateFilename = "text-updated.txt"; - String updateFilename = "text-updated.txt"; - updateDocumentUsingByteHandle(client, updateFilename, uri+filename, "Text"); - - //read the document - BytesHandle updateHandle = readDocumentUsingBytesHandle(client, uri+filename, "Text"); - - // get the contents - byte[] fileReadUpdate = updateHandle.get(); - String readContentUpdate = new String(fileReadUpdate); - String expectedContentUpdate = "hello world, welcome to java API after new updates"; - - assertEquals("Write Text difference", expectedContentUpdate.trim(), readContentUpdate.toString().trim()); - - // delete the document - deleteDocument(client, uri + filename, "Text"); - - // read the deleted document - //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "Text")); - - String exception = ""; - try - { - InputStreamHandle deleteHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "Text"); - } catch (Exception e) { exception = e.toString(); } - - String expectedException = "com.marklogic.client.ResourceNotFoundException: Local message: Could not read non-existent document. Server Message: RESTAPI-NODOCUMENT: (err:FOER0000) Resource or document does not exist: category: content message: /write-text-Byteshandle/text-original.txt"; - assertEquals("Document is not deleted", expectedException, exception); - - // release client - client.release(); - -} - -@SuppressWarnings("deprecation") -@Test -public void testJsonCRUD() throws IOException, ParserConfigurationException, SAXException{ - String filename = "json-original.json"; - String uri = "/write-json-Byteshandle/"; - System.out.println("Running testJsonCRUD"); - - ObjectMapper mapper = new ObjectMapper(); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - writeDocumentUsingBytesHandle(client, filename, uri, "JSON"); - - // read docs - BytesHandle contentHandle = readDocumentUsingBytesHandle(client, uri + filename,"JSON"); - - // get the contents - byte[] fileRead = contentHandle.get(); - JsonNode readContent = mapper.readTree(fileRead); - - // get expected contents - JsonNode expectedContent = expectedJSONDocument(filename); - - assertTrue("Write JSON document difference", readContent.equals(expectedContent)); - - // update the doc - // acquire the content for update - String updateFilename = "json-updated.json"; - updateDocumentUsingByteHandle(client, updateFilename, uri + filename, "JSON"); - - // read the document - BytesHandle updateHandle = readDocumentUsingBytesHandle(client, uri+filename, "JSON"); - - // get the contents - byte[] fileReadUpdate = updateHandle.get(); - JsonNode readContentUpdate = mapper.readTree(fileReadUpdate); - - // get expected contents - JsonNode expectedContentUpdate = expectedJSONDocument(updateFilename); - assertTrue("Write JSON document difference", readContentUpdate.equals(expectedContentUpdate)); - - // delete the document - deleteDocument(client, uri + filename, "JSON"); - - // read the deleted document - //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "JSON")); - - String exception = ""; - try - { - InputStreamHandle deleteHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "JSON"); - } catch (Exception e) { exception = e.toString(); } - - String expectedException = "com.marklogic.client.ResourceNotFoundException: Local message: Could not read non-existent document. Server Message: RESTAPI-NODOCUMENT: (err:FOER0000) Resource or document does not exist: category: content message: /write-json-Byteshandle/json-original.json"; - assertEquals("Document is not deleted", expectedException, exception); - - // release client - client.release(); -} - -@SuppressWarnings("deprecation") -@Test -public void testBinaryCRUD() throws IOException, ParserConfigurationException, SAXException{ - String filename = "Pandakarlino.jpg"; - String uri = "/write-bin-Bytehandle/"; - System.out.println("Running testBinaryCRUD"); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - writeDocumentUsingBytesHandle(client, filename, uri, "Binary"); - - // read docs - BytesHandle contentHandle = readDocumentUsingBytesHandle(client, uri + filename,"Binary"); - - // get the contents - byte[] fileRead = contentHandle.get(); - - // get the binary size - long size = getBinarySizeFromByte(fileRead); - long expectedSize = 17154; - - assertEquals("Binary size difference", expectedSize, size); - - // update the doc - // acquire the content for update - String updateFilename = "mlfavicon.png"; - updateDocumentUsingByteHandle(client, updateFilename, uri + filename, "Binary"); - - // read the document - BytesHandle updateHandle = readDocumentUsingBytesHandle(client, uri+filename, "Binary"); - - // get the contents - byte[] fileReadUpdate = updateHandle.get(); - - // get the binary size - long sizeUpdate = getBinarySizeFromByte(fileReadUpdate); - long expectedSizeUpdate = 3290; - //long expectedSizeUpdate = 3322; - assertEquals("Binary size difference", expectedSizeUpdate, sizeUpdate); - - // delete the document - deleteDocument(client, uri + filename, "Binary"); - - // read the deleted document - //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "Binary")); - - String exception = ""; - try - { - InputStreamHandle deleteHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "Binary"); - } catch (Exception e) { exception = e.toString(); } - - String expectedException = "com.marklogic.client.ResourceNotFoundException: Local message: Could not read non-existent document. Server Message: RESTAPI-NODOCUMENT: (err:FOER0000) Resource or document does not exist: category: content message: /write-bin-Bytehandle/Pandakarlino.jpg"; - assertEquals("Document is not deleted", expectedException, exception); - - // release client - client.release(); - - -} -@AfterClass -public static void tearDown() throws Exception -{ - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - -} - -} +package com.marklogic.javaclient; +import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; +import static org.junit.Assert.*; + +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; + +import org.custommonkey.xmlunit.XMLUnit; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.BytesHandle; +import org.junit.*; +public class TestBytesHandle extends BasicJavaClientREST{ + + +private static String dbName = "BytesHandleDB"; +private static String [] fNames = {"BytesHandleDB-1"}; +private static String restServerName = "REST-Java-Client-API-Server"; +@BeforeClass +public static void setUp() throws Exception{ + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + } + +@Test +public void testXmlCRUD() throws IOException , SAXException, ParserConfigurationException{ + + String filename = "xml-original-test.xml"; + String uri = "/write-xml-domhandle/"; + System.out.println("Running testXmlCRUD"); + XMLUnit.setIgnoreWhitespace(true); + XMLUnit.setNormalizeWhitespace(true); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x",Authentication.DIGEST); + + // write docs + writeDocumentUsingBytesHandle(client, filename, uri, null,"XML");//*********** + + //read docs + BytesHandle contentHandle = readDocumentUsingBytesHandle(client, uri + filename,"XML"); + + // get the contents + + byte[] readDoc1 = (byte[])contentHandle.get(); + String readDoc2 = new String(readDoc1); + Document readDoc = convertStringToXMLDocument(readDoc2); + + // get xml document for expected result + Document expectedDoc = expectedXMLDocument(filename); + assertXMLEqual("Write XML difference", expectedDoc, readDoc); + + // Update the Doc + //acquire the content for update + String updateFilename = "xml-updated-test.xml"; + updateDocumentUsingByteHandle(client, updateFilename, uri + filename, "XML"); + + // read the document + BytesHandle updateHandle = readDocumentUsingBytesHandle(client, uri+filename, "XML"); + byte[] readDocUpdateInBytes = updateHandle.get(); + String readDocUpdateInString= new String(readDocUpdateInBytes); + + // convert actual string to xml doc + Document readDocUpdate = convertStringToXMLDocument(readDocUpdateInString); + + // get xml document for expected result + Document expectedDocUpdate = expectedXMLDocument(updateFilename); + assertXMLEqual("Write XML Difference", expectedDocUpdate, readDocUpdate); + + // delete the document + deleteDocument(client, uri+filename,"XML"); + + // read the deleted document + String exception = ""; + try + { + readDocumentUsingBytesHandle(client, uri+filename, "XML"); + } catch (Exception e) { exception = e.toString(); } + + String expectedException = "com.marklogic.client.ResourceNotFoundException: Local message: Could not read non-existent document. Server Message: RESTAPI-NODOCUMENT: (err:FOER0000) Resource or document does not exist: category: content message: /write-xml-domhandle/xml-original-test.xml"; + assertEquals("Document is not deleted", expectedException, exception); + + //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "XML")); + + // release client + client.release(); + + } + +@Test +public void testTextCRUD() throws IOException, ParserConfigurationException, SAXException{ + String filename = "text-original.txt"; + String uri = "/write-text-Byteshandle/"; + System.out.println("Runing test TextCRUD"); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + writeDocumentUsingBytesHandle(client, filename, uri, "Text"); + + //read docs + BytesHandle contentHandle = readDocumentUsingBytesHandle(client, uri + filename,"Text"); + + // get the contents + byte[] fileRead = (byte[])contentHandle.get(); + //String readContent = contentHandle.get().toString(); + String readContent = new String(fileRead); + String expectedContent = "hello world, welcome to java API"; + assertEquals("Write Text difference", expectedContent.trim(), readContent.trim()); + + //UPDATE the doc + // acquire the content for update + //String updateFilename = "text-updated.txt"; + String updateFilename = "text-updated.txt"; + updateDocumentUsingByteHandle(client, updateFilename, uri+filename, "Text"); + + //read the document + BytesHandle updateHandle = readDocumentUsingBytesHandle(client, uri+filename, "Text"); + + // get the contents + byte[] fileReadUpdate = updateHandle.get(); + String readContentUpdate = new String(fileReadUpdate); + String expectedContentUpdate = "hello world, welcome to java API after new updates"; + + assertEquals("Write Text difference", expectedContentUpdate.trim(), readContentUpdate.toString().trim()); + + // delete the document + deleteDocument(client, uri + filename, "Text"); + + // read the deleted document + //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "Text")); + + String exception = ""; + try + { + readDocumentUsingInputStreamHandle(client, uri + filename, "Text"); + } catch (Exception e) { exception = e.toString(); } + + String expectedException = "com.marklogic.client.ResourceNotFoundException: Local message: Could not read non-existent document. Server Message: RESTAPI-NODOCUMENT: (err:FOER0000) Resource or document does not exist: category: content message: /write-text-Byteshandle/text-original.txt"; + assertEquals("Document is not deleted", expectedException, exception); + + // release client + client.release(); + +} + +@Test +public void testJsonCRUD() throws IOException, ParserConfigurationException, SAXException{ + String filename = "json-original.json"; + String uri = "/write-json-Byteshandle/"; + System.out.println("Running testJsonCRUD"); + + ObjectMapper mapper = new ObjectMapper(); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + writeDocumentUsingBytesHandle(client, filename, uri, "JSON"); + + // read docs + BytesHandle contentHandle = readDocumentUsingBytesHandle(client, uri + filename,"JSON"); + + // get the contents + byte[] fileRead = contentHandle.get(); + JsonNode readContent = mapper.readTree(fileRead); + + // get expected contents + JsonNode expectedContent = expectedJSONDocument(filename); + + assertTrue("Write JSON document difference", readContent.equals(expectedContent)); + + // update the doc + // acquire the content for update + String updateFilename = "json-updated.json"; + updateDocumentUsingByteHandle(client, updateFilename, uri + filename, "JSON"); + + // read the document + BytesHandle updateHandle = readDocumentUsingBytesHandle(client, uri+filename, "JSON"); + + // get the contents + byte[] fileReadUpdate = updateHandle.get(); + JsonNode readContentUpdate = mapper.readTree(fileReadUpdate); + + // get expected contents + JsonNode expectedContentUpdate = expectedJSONDocument(updateFilename); + assertTrue("Write JSON document difference", readContentUpdate.equals(expectedContentUpdate)); + + // delete the document + deleteDocument(client, uri + filename, "JSON"); + + // read the deleted document + //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "JSON")); + + String exception = ""; + try + { + readDocumentUsingInputStreamHandle(client, uri + filename, "JSON"); + } catch (Exception e) { exception = e.toString(); } + + String expectedException = "com.marklogic.client.ResourceNotFoundException: Local message: Could not read non-existent document. Server Message: RESTAPI-NODOCUMENT: (err:FOER0000) Resource or document does not exist: category: content message: /write-json-Byteshandle/json-original.json"; + assertEquals("Document is not deleted", expectedException, exception); + + // release client + client.release(); +} + +@Test +public void testBinaryCRUD() throws IOException, ParserConfigurationException, SAXException{ + String filename = "Pandakarlino.jpg"; + String uri = "/write-bin-Bytehandle/"; + System.out.println("Running testBinaryCRUD"); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + writeDocumentUsingBytesHandle(client, filename, uri, "Binary"); + + // read docs + BytesHandle contentHandle = readDocumentUsingBytesHandle(client, uri + filename,"Binary"); + + // get the contents + byte[] fileRead = contentHandle.get(); + + // get the binary size + long size = getBinarySizeFromByte(fileRead); + long expectedSize = 17154; + + assertEquals("Binary size difference", expectedSize, size); + + // update the doc + // acquire the content for update + String updateFilename = "mlfavicon.png"; + updateDocumentUsingByteHandle(client, updateFilename, uri + filename, "Binary"); + + // read the document + BytesHandle updateHandle = readDocumentUsingBytesHandle(client, uri+filename, "Binary"); + + // get the contents + byte[] fileReadUpdate = updateHandle.get(); + + // get the binary size + long sizeUpdate = getBinarySizeFromByte(fileReadUpdate); + long expectedSizeUpdate = 3290; + //long expectedSizeUpdate = 3322; + assertEquals("Binary size difference", expectedSizeUpdate, sizeUpdate); + + // delete the document + deleteDocument(client, uri + filename, "Binary"); + + // read the deleted document + //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "Binary")); + + String exception = ""; + try + { + readDocumentUsingInputStreamHandle(client, uri + filename, "Binary"); + } catch (Exception e) { exception = e.toString(); } + + String expectedException = "com.marklogic.client.ResourceNotFoundException: Local message: Could not read non-existent document. Server Message: RESTAPI-NODOCUMENT: (err:FOER0000) Resource or document does not exist: category: content message: /write-bin-Bytehandle/Pandakarlino.jpg"; + assertEquals("Document is not deleted", expectedException, exception); + + // release client + client.release(); + + +} +@AfterClass +public static void tearDown() throws Exception +{ + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + +} + +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestCRUDModulesDb.java b/test-complete/src/test/java/com/marklogic/javaclient/TestCRUDModulesDb.java index 274086d3e..4221286a0 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestCRUDModulesDb.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestCRUDModulesDb.java @@ -1,368 +1,367 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.File; - -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.ResourceNotFoundException; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.admin.ExtensionLibrariesManager; -import com.marklogic.client.admin.ExtensionLibraryDescriptor; -import com.marklogic.client.io.FileHandle; -import com.marklogic.client.io.Format; -import com.marklogic.client.io.StringHandle; -import com.marklogic.client.DatabaseClient; - -import org.junit.*; -public class TestCRUDModulesDb extends BasicJavaClientREST { - - private static String dbName = "Modules"; - private static String [] fNames = {"Modules"}; - private static String restServerName = "REST-Java-Client-API-Server"; -@BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - assocRESTServer(restServerName, dbName,8011); - } - -@After -public void testCleanUp() throws Exception -{ - clearDB(8011); - System.out.println("Running clear script"); -} -@Test - public void testXQueryModuleCRUDDuplicateFile() - { - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // get a manager - //ExtensionLibrariesManager libsMgr = Common.client.newServerConfigManager().newExtensionLibrariesManager(); - ExtensionLibrariesManager libsMgr = client.newServerConfigManager().newExtensionLibrariesManager(); - - String Path = "/ext/my/path/to/my/module.xqy"; - - // write XQuery file to the modules database - libsMgr.write(Path, new FileHandle(new File("src/test/java/com/marklogic/javaclient/data/module.xqy")).withFormat(Format.TEXT)); - - // read it back - String xqueryModuleAsString = libsMgr.read(Path, new StringHandle()).get(); - assertTrue("module read and read back", xqueryModuleAsString.startsWith("xquery version \"1.0-ml\";")); - - // write Duplicate XQuery file to the modules database with different content - libsMgr.write(Path, new FileHandle(new File("src/test/java/com/marklogic/javaclient/data/module.xqy")).withFormat(Format.TEXT)); - - // read it back to check overwritten - String xqueryModuleAsDuplicateString = libsMgr.read(Path, new StringHandle()).get(); - assertTrue("module read and read back", xqueryModuleAsDuplicateString.startsWith("xquery version \"1.0-ml\";")); - - // get the list of descriptors - ExtensionLibraryDescriptor[] descriptors = libsMgr.list(); - assertEquals("number of modules installed", descriptors.length, 1); - - for (ExtensionLibraryDescriptor descriptor : descriptors) { - assertEquals(descriptor.getPath(), Path); - } - - // delete it - libsMgr.delete(Path); - - try { - // read deleted module - xqueryModuleAsString = libsMgr.read(Path, new StringHandle()).get(); - } catch (ResourceNotFoundException e) { - // pass; - } - - } - - -@Test public void testXQueryModuleCRUDDifferentPath() { - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // get a manager - ExtensionLibrariesManager libsMgr = client.newServerConfigManager().newExtensionLibrariesManager(); - String firstPath = "/ext/my/path/to/my/module.xqy"; - String secondPath = "/ext/my/path/to/my/other/module.xqy" ; - // write XQuery file to the modules database - libsMgr.write(firstPath, new FileHandle(new File("src/test/java/com/marklogic/javaclient/data/module.xqy")).withFormat(Format.TEXT)); - - // write XQuery file to the modules database Different Path - libsMgr.write(secondPath, new FileHandle(new File("src/test/java/com/marklogic/javaclient/data/module.xqy")).withFormat(Format.TEXT)); - - // read 1st file back - String xqueryModuleAsString = libsMgr.read(firstPath, new StringHandle()).get(); - assertTrue("module read and read back", xqueryModuleAsString.startsWith("xquery version \"1.0-ml\";")); - - // read 2nd file back - String xqueryModuleAsString1 = libsMgr.read(secondPath, new StringHandle()).get(); - assertTrue("module read and read back", xqueryModuleAsString1.startsWith("xquery version \"1.0-ml\";")); - - // get the list of descriptors - ExtensionLibraryDescriptor[] descriptors = libsMgr.list(); - assertEquals("number of modules installed", descriptors.length, 2); - assertEquals(descriptors[0].getPath(), firstPath ); - assertEquals(descriptors[1].getPath(), secondPath); - - // delete it - libsMgr.delete(firstPath ); - libsMgr.delete(secondPath); - - try { - // read deleted module - xqueryModuleAsString = libsMgr.read(firstPath, new StringHandle()).get(); - xqueryModuleAsString = libsMgr.read(secondPath, new StringHandle()).get(); - } catch (ResourceNotFoundException e) { - // pass; - } - - } - - -@Test -public void testXQueryModuleCRUDBinaryFile() { - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // get a manager - ExtensionLibrariesManager libsMgr = client.newServerConfigManager().newExtensionLibrariesManager(); - - String Path = "/ext/my/path/to/my/module.xqy"; - - // write XQuery file to the modules database - libsMgr.write(Path, new FileHandle(new File("src/test/java/com/marklogic/javaclient/data/binary.jpg")).withFormat(Format.BINARY)); - - // read it back - FileHandle f = new FileHandle(new File("src/test/java/com/marklogic/javaclient/data/binary.jpg")); - assertEquals(f.getByteLength(),libsMgr.read(Path, new StringHandle()).getByteLength()); - - // get the list of descriptors - ExtensionLibraryDescriptor[] descriptors = libsMgr.list(); - assertEquals("number of modules installed", descriptors.length, 1); - - for (ExtensionLibraryDescriptor descriptor : descriptors) { - assertEquals(descriptor.getPath(), Path); - } - - // delete it - libsMgr.delete(Path); - - try { - // read deleted module - String xqueryModuleAsString = libsMgr.read(Path, new StringHandle()).get(); - } catch (ResourceNotFoundException e) { - // pass; - } - - } - - -@Test public void testXQueryModuleCRUDTextFile() { - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // get a manager - ExtensionLibrariesManager libsMgr = client.newServerConfigManager().newExtensionLibrariesManager(); - - String Path = "/ext/my/path/to/my/module.xqy"; - - // write XQuery file to the modules database - libsMgr.write(Path, new FileHandle(new File("src/test/java/com/marklogic/javaclient/data/readme.txt")).withFormat(Format.TEXT)); - - // read it back - String xqueryModuleAsString = libsMgr.read(Path, new StringHandle()).get(); - assertTrue("module read and read back", xqueryModuleAsString.startsWith("Copyright 2012 MarkLogic Corporation")); - - // get the list of descriptors - ExtensionLibraryDescriptor[] descriptors = libsMgr.list(); - assertEquals("number of modules installed", descriptors.length, 1); - - for (ExtensionLibraryDescriptor descriptor : descriptors) { - assertEquals(descriptor.getPath(), Path); - } - - // delete it - libsMgr.delete(Path); - - try { - // read deleted module - xqueryModuleAsString = libsMgr.read(Path, new StringHandle()).get(); - } catch (ResourceNotFoundException e) { - // pass; - } - - } - - -@Test public void testXQueryModuleCRUDXmlFile() { - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // get a manager - ExtensionLibrariesManager libsMgr = client.newServerConfigManager().newExtensionLibrariesManager(); - - String Path = "/ext/my/path/to/my/module.xqy"; - FileHandle f = new FileHandle(new File("src/test/java/com/marklogic/javaclient/data/all_well.xml")).withFormat(Format.XML); - - // write XQuery file to the modules database - libsMgr.write(Path, f); - - // read it back - assertEquals(f.getByteLength(), libsMgr.read(Path, new StringHandle()).getByteLength()); - - // get the list of descriptors - ExtensionLibraryDescriptor[] descriptors = libsMgr.list(); - assertEquals("number of modules installed", descriptors.length, 1); - - for (ExtensionLibraryDescriptor descriptor : descriptors) { - assertEquals(descriptor.getPath(), Path); - } - - // delete it - libsMgr.delete(Path); - - try { - // read deleted module - String xqueryModuleAsString = libsMgr.read(Path, new StringHandle()).get(); - } catch (ResourceNotFoundException e) { - // pass; - } - - } - - -@Test public void testXQueryModuleReadModulesDb() { - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // get a manager - ExtensionLibrariesManager libsMgr = client.newServerConfigManager().newExtensionLibrariesManager(); - - String Path = "/ext/my/path/to/my/module.xqy"; - FileHandle f = new FileHandle(new File("src/test/java/com/marklogic/javaclient/data/module.xqy")).withFormat(Format.TEXT); - - // write XQuery file to the modules database - libsMgr.write(Path, f); - - // read it back - - String xqueryModuleAsString = libsMgr.read(Path, new StringHandle()).get(); - assertTrue("module read and read back", xqueryModuleAsString.contains("let $x := (1,2,3,4,5)")); - - // get the list of descriptors - ExtensionLibraryDescriptor[] descriptors = libsMgr.list(); - assertEquals("number of modules installed", descriptors.length, 1); - - for (ExtensionLibraryDescriptor descriptor : descriptors) { - assertEquals(descriptor.getPath(), Path); - System.out.println("Path returned by Descriptor "+ descriptor.getPath()); - } - System.out.println("Path"+Path); - // delete it - libsMgr.delete(Path); - try { - // read deleted module - xqueryModuleAsString = libsMgr.read(Path, new StringHandle()).get(); - } catch (ResourceNotFoundException e) { - System.out.println("Reading deleted file Failed"); - // pass; - } - try{ - libsMgr.delete(Path); - }catch(Exception e){ - System.out.println("Attempt to Delete Non exsting file Failed"); - e.printStackTrace(); - } - - } - -@Test -public void testXQueryModuleReadExtensionLibraryDescriptor () { - System.out.println("testXQueryModuleReadExtensionLibraryDescriptor"); - -DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // get a manager - ExtensionLibrariesManager libsMgr = client.newServerConfigManager().newExtensionLibrariesManager(); - - String Path = "/ext/my/path/to/my/module.xqy"; - FileHandle f = new FileHandle(new File("src/test/java/com/marklogic/javaclient/data/module.xqy")).withFormat(Format.TEXT); - - // write XQuery file to the modules database - libsMgr.write(Path, f); - - // read it back - - String xqueryModuleAsString = libsMgr.read(Path, new StringHandle()).get(); - assertTrue("module read and read back", xqueryModuleAsString.contains("let $x := (1,2,3,4,5)")); - - // get the list of descriptors - ExtensionLibraryDescriptor[] descriptors = libsMgr.list(); - assertEquals("number of modules installed", descriptors.length, 1); - - for (ExtensionLibraryDescriptor descriptor : descriptors) { - descriptor.setPath("/ext/my/path/to/my/new/module.xqy"); - String xqueryModuleAsStringNew = libsMgr.read(Path, new StringHandle()).get(); - System.out.println("Path returned by Descriptor "+ descriptor.getPath()+"Document returned by Descriptor"+xqueryModuleAsStringNew); - libsMgr.delete(descriptor.getPath()); - } - - // delete it - libsMgr.delete(Path); - - try { - // read deleted module - xqueryModuleAsString = libsMgr.read(Path, new StringHandle()).get(); - } catch (ResourceNotFoundException e) { - System.out.println("Reading deleted file Failed"); - // pass; - } - try{ - libsMgr.delete(Path); - }catch(Exception e){ - System.out.println("Attempt to Delete Non exsting file Failed"); - e.printStackTrace(); - } - - } - -@Test -public void testXQueryModuleCRUDXmlFileNegative() { - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // get a manager - ExtensionLibrariesManager libsMgr = client.newServerConfigManager().newExtensionLibrariesManager(); - - String Path = "/foo/my/path/to/my/module.xqy"; - FileHandle f = new FileHandle(new File("src/test/java/com/marklogic/javaclient/data/all_well.xml")).withFormat(Format.XML); - - // write XQuery file to the modules database - try{ - libsMgr.write(Path, f); - }catch(ResourceNotFoundException e){ - assertEquals("Negative test Passed as","Local message: Could not write resource at /foo/my/path/to/my/module.xqy. Server Message: /v1/foo/my/path/to/my/module.xqy" , e.getMessage()); - } - // delete it - try{ - libsMgr.delete(Path); - }catch(Exception e){ - assertEquals("", "Local message: Could not delete resource at /foo/my/path/to/my/module.xqy. Server Message: /v1/foo/my/path/to/my/module.xqy", e.getMessage()); - } - -} -@AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - try{ - deleteRESTServer(restServerName); - }catch(Exception e){ - e.printStackTrace(); - } - } -} - +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.File; + +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.ResourceNotFoundException; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.admin.ExtensionLibrariesManager; +import com.marklogic.client.admin.ExtensionLibraryDescriptor; +import com.marklogic.client.io.FileHandle; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.StringHandle; +import com.marklogic.client.DatabaseClient; + +import org.junit.*; +public class TestCRUDModulesDb extends BasicJavaClientREST { + + private static String dbName = "Modules"; + private static String restServerName = "REST-Java-Client-API-Server"; +@BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + assocRESTServer(restServerName, dbName,8011); + } + +@After +public void testCleanUp() throws Exception +{ + clearDB(8011); + System.out.println("Running clear script"); +} +@Test + public void testXQueryModuleCRUDDuplicateFile() + { + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // get a manager + //ExtensionLibrariesManager libsMgr = Common.client.newServerConfigManager().newExtensionLibrariesManager(); + ExtensionLibrariesManager libsMgr = client.newServerConfigManager().newExtensionLibrariesManager(); + + String Path = "/ext/my/path/to/my/module.xqy"; + + // write XQuery file to the modules database + libsMgr.write(Path, new FileHandle(new File("src/test/java/com/marklogic/javaclient/data/module.xqy")).withFormat(Format.TEXT)); + + // read it back + String xqueryModuleAsString = libsMgr.read(Path, new StringHandle()).get(); + assertTrue("module read and read back", xqueryModuleAsString.startsWith("xquery version \"1.0-ml\";")); + + // write Duplicate XQuery file to the modules database with different content + libsMgr.write(Path, new FileHandle(new File("src/test/java/com/marklogic/javaclient/data/module.xqy")).withFormat(Format.TEXT)); + + // read it back to check overwritten + String xqueryModuleAsDuplicateString = libsMgr.read(Path, new StringHandle()).get(); + assertTrue("module read and read back", xqueryModuleAsDuplicateString.startsWith("xquery version \"1.0-ml\";")); + + // get the list of descriptors + ExtensionLibraryDescriptor[] descriptors = libsMgr.list(); + assertEquals("number of modules installed", descriptors.length, 1); + + for (ExtensionLibraryDescriptor descriptor : descriptors) { + assertEquals(descriptor.getPath(), Path); + } + + // delete it + libsMgr.delete(Path); + + try { + // read deleted module + xqueryModuleAsString = libsMgr.read(Path, new StringHandle()).get(); + } catch (ResourceNotFoundException e) { + // pass; + } + + } + + +@Test public void testXQueryModuleCRUDDifferentPath() { + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // get a manager + ExtensionLibrariesManager libsMgr = client.newServerConfigManager().newExtensionLibrariesManager(); + String firstPath = "/ext/my/path/to/my/module.xqy"; + String secondPath = "/ext/my/path/to/my/other/module.xqy" ; + // write XQuery file to the modules database + libsMgr.write(firstPath, new FileHandle(new File("src/test/java/com/marklogic/javaclient/data/module.xqy")).withFormat(Format.TEXT)); + + // write XQuery file to the modules database Different Path + libsMgr.write(secondPath, new FileHandle(new File("src/test/java/com/marklogic/javaclient/data/module.xqy")).withFormat(Format.TEXT)); + + // read 1st file back + String xqueryModuleAsString = libsMgr.read(firstPath, new StringHandle()).get(); + assertTrue("module read and read back", xqueryModuleAsString.startsWith("xquery version \"1.0-ml\";")); + + // read 2nd file back + String xqueryModuleAsString1 = libsMgr.read(secondPath, new StringHandle()).get(); + assertTrue("module read and read back", xqueryModuleAsString1.startsWith("xquery version \"1.0-ml\";")); + + // get the list of descriptors + ExtensionLibraryDescriptor[] descriptors = libsMgr.list(); + assertEquals("number of modules installed", descriptors.length, 2); + assertEquals(descriptors[0].getPath(), firstPath ); + assertEquals(descriptors[1].getPath(), secondPath); + + // delete it + libsMgr.delete(firstPath ); + libsMgr.delete(secondPath); + + try { + // read deleted module + xqueryModuleAsString = libsMgr.read(firstPath, new StringHandle()).get(); + xqueryModuleAsString = libsMgr.read(secondPath, new StringHandle()).get(); + } catch (ResourceNotFoundException e) { + // pass; + } + + } + + +@Test +public void testXQueryModuleCRUDBinaryFile() { + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // get a manager + ExtensionLibrariesManager libsMgr = client.newServerConfigManager().newExtensionLibrariesManager(); + + String Path = "/ext/my/path/to/my/module.xqy"; + + // write XQuery file to the modules database + libsMgr.write(Path, new FileHandle(new File("src/test/java/com/marklogic/javaclient/data/binary.jpg")).withFormat(Format.BINARY)); + + // read it back + FileHandle f = new FileHandle(new File("src/test/java/com/marklogic/javaclient/data/binary.jpg")); + assertEquals(f.getByteLength(),libsMgr.read(Path, new StringHandle()).getByteLength()); + + // get the list of descriptors + ExtensionLibraryDescriptor[] descriptors = libsMgr.list(); + assertEquals("number of modules installed", descriptors.length, 1); + + for (ExtensionLibraryDescriptor descriptor : descriptors) { + assertEquals(descriptor.getPath(), Path); + } + + // delete it + libsMgr.delete(Path); + + try { + // read deleted module + libsMgr.read(Path, new StringHandle()).get(); + } catch (ResourceNotFoundException e) { + // pass; + } + + } + + +@Test public void testXQueryModuleCRUDTextFile() { + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // get a manager + ExtensionLibrariesManager libsMgr = client.newServerConfigManager().newExtensionLibrariesManager(); + + String Path = "/ext/my/path/to/my/module.xqy"; + + // write XQuery file to the modules database + libsMgr.write(Path, new FileHandle(new File("src/test/java/com/marklogic/javaclient/data/readme.txt")).withFormat(Format.TEXT)); + + // read it back + String xqueryModuleAsString = libsMgr.read(Path, new StringHandle()).get(); + assertTrue("module read and read back", xqueryModuleAsString.startsWith("Copyright 2012 MarkLogic Corporation")); + + // get the list of descriptors + ExtensionLibraryDescriptor[] descriptors = libsMgr.list(); + assertEquals("number of modules installed", descriptors.length, 1); + + for (ExtensionLibraryDescriptor descriptor : descriptors) { + assertEquals(descriptor.getPath(), Path); + } + + // delete it + libsMgr.delete(Path); + + try { + // read deleted module + xqueryModuleAsString = libsMgr.read(Path, new StringHandle()).get(); + } catch (ResourceNotFoundException e) { + // pass; + } + + } + + +@Test public void testXQueryModuleCRUDXmlFile() { + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // get a manager + ExtensionLibrariesManager libsMgr = client.newServerConfigManager().newExtensionLibrariesManager(); + + String Path = "/ext/my/path/to/my/module.xqy"; + FileHandle f = new FileHandle(new File("src/test/java/com/marklogic/javaclient/data/all_well.xml")).withFormat(Format.XML); + + // write XQuery file to the modules database + libsMgr.write(Path, f); + + // read it back + assertEquals(f.getByteLength(), libsMgr.read(Path, new StringHandle()).getByteLength()); + + // get the list of descriptors + ExtensionLibraryDescriptor[] descriptors = libsMgr.list(); + assertEquals("number of modules installed", descriptors.length, 1); + + for (ExtensionLibraryDescriptor descriptor : descriptors) { + assertEquals(descriptor.getPath(), Path); + } + + // delete it + libsMgr.delete(Path); + + try { + // read deleted module + libsMgr.read(Path, new StringHandle()).get(); + } catch (ResourceNotFoundException e) { + // pass; + } + + } + + +@Test public void testXQueryModuleReadModulesDb() { + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // get a manager + ExtensionLibrariesManager libsMgr = client.newServerConfigManager().newExtensionLibrariesManager(); + + String Path = "/ext/my/path/to/my/module.xqy"; + FileHandle f = new FileHandle(new File("src/test/java/com/marklogic/javaclient/data/module.xqy")).withFormat(Format.TEXT); + + // write XQuery file to the modules database + libsMgr.write(Path, f); + + // read it back + + String xqueryModuleAsString = libsMgr.read(Path, new StringHandle()).get(); + assertTrue("module read and read back", xqueryModuleAsString.contains("let $x := (1,2,3,4,5)")); + + // get the list of descriptors + ExtensionLibraryDescriptor[] descriptors = libsMgr.list(); + assertEquals("number of modules installed", descriptors.length, 1); + + for (ExtensionLibraryDescriptor descriptor : descriptors) { + assertEquals(descriptor.getPath(), Path); + System.out.println("Path returned by Descriptor "+ descriptor.getPath()); + } + System.out.println("Path"+Path); + // delete it + libsMgr.delete(Path); + try { + // read deleted module + xqueryModuleAsString = libsMgr.read(Path, new StringHandle()).get(); + } catch (ResourceNotFoundException e) { + System.out.println("Reading deleted file Failed"); + // pass; + } + try{ + libsMgr.delete(Path); + }catch(Exception e){ + System.out.println("Attempt to Delete Non exsting file Failed"); + e.printStackTrace(); + } + + } + +@Test +public void testXQueryModuleReadExtensionLibraryDescriptor () { + System.out.println("testXQueryModuleReadExtensionLibraryDescriptor"); + +DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // get a manager + ExtensionLibrariesManager libsMgr = client.newServerConfigManager().newExtensionLibrariesManager(); + + String Path = "/ext/my/path/to/my/module.xqy"; + FileHandle f = new FileHandle(new File("src/test/java/com/marklogic/javaclient/data/module.xqy")).withFormat(Format.TEXT); + + // write XQuery file to the modules database + libsMgr.write(Path, f); + + // read it back + + String xqueryModuleAsString = libsMgr.read(Path, new StringHandle()).get(); + assertTrue("module read and read back", xqueryModuleAsString.contains("let $x := (1,2,3,4,5)")); + + // get the list of descriptors + ExtensionLibraryDescriptor[] descriptors = libsMgr.list(); + assertEquals("number of modules installed", descriptors.length, 1); + + for (ExtensionLibraryDescriptor descriptor : descriptors) { + descriptor.setPath("/ext/my/path/to/my/new/module.xqy"); + String xqueryModuleAsStringNew = libsMgr.read(Path, new StringHandle()).get(); + System.out.println("Path returned by Descriptor "+ descriptor.getPath()+"Document returned by Descriptor"+xqueryModuleAsStringNew); + libsMgr.delete(descriptor.getPath()); + } + + // delete it + libsMgr.delete(Path); + + try { + // read deleted module + xqueryModuleAsString = libsMgr.read(Path, new StringHandle()).get(); + } catch (ResourceNotFoundException e) { + System.out.println("Reading deleted file Failed"); + // pass; + } + try{ + libsMgr.delete(Path); + }catch(Exception e){ + System.out.println("Attempt to Delete Non exsting file Failed"); + e.printStackTrace(); + } + + } + +@Test +public void testXQueryModuleCRUDXmlFileNegative() { + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // get a manager + ExtensionLibrariesManager libsMgr = client.newServerConfigManager().newExtensionLibrariesManager(); + + String Path = "/foo/my/path/to/my/module.xqy"; + FileHandle f = new FileHandle(new File("src/test/java/com/marklogic/javaclient/data/all_well.xml")).withFormat(Format.XML); + + // write XQuery file to the modules database + try{ + libsMgr.write(Path, f); + }catch(ResourceNotFoundException e){ + assertEquals("Negative test Passed as","Local message: Could not write resource at /foo/my/path/to/my/module.xqy. Server Message: /v1/foo/my/path/to/my/module.xqy" , e.getMessage()); + } + // delete it + try{ + libsMgr.delete(Path); + }catch(Exception e){ + assertEquals("", "Local message: Could not delete resource at /foo/my/path/to/my/module.xqy. Server Message: /v1/foo/my/path/to/my/module.xqy", e.getMessage()); + } + +} +@AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + try{ + deleteRESTServer(restServerName); + }catch(Exception e){ + e.printStackTrace(); + } + } +} + diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestConstraintCombination.java b/test-complete/src/test/java/com/marklogic/javaclient/TestConstraintCombination.java index 8cd9fc185..238c7408d 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestConstraintCombination.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestConstraintCombination.java @@ -1,235 +1,232 @@ -package com.marklogic.javaclient; - -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; - -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.query.StringQueryDefinition; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.DocumentMetadataHandle; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.junit.*; -public class TestConstraintCombination extends BasicJavaClientREST { - - private static String dbName = "ConstraintCombinationDB"; - private static String [] fNames = {"ConstraintCombinationDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort = 8011; - -@BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - addRangeElementAttributeIndex(dbName, "dateTime", "http://example.com", "entry", "", "date"); - addRangeElementIndex(dbName, "int", "http://example.com", "scoville"); - addRangeElementIndex(dbName, "decimal", "http://example.com", "rating"); - addField(dbName, "bbqtext"); - includeElementField(dbName, "bbqtext", "http://example.com", "title"); - includeElementField(dbName, "bbqtext", "http://example.com", "abstract"); - enableCollectionLexicon(dbName); - enableTrailingWildcardSearches(dbName); - } - -@After - public void testCleanUp() throws Exception - { - clearDB(restPort); - System.out.println("Running clear script"); - } - -@SuppressWarnings("deprecation") -@Test - public void testConstraintCombination() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testConstraintCombination"); - - String filename1 = "bbq1.xml"; - String filename2 = "bbq2.xml"; - String filename3 = "bbq3.xml"; - String filename4 = "bbq4.xml"; - String filename5 = "bbq5.xml"; - - String queryOptionName = "constraintCombinationOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create and initialize a handle on the metadata - DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle4 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle5 = new DocumentMetadataHandle(); - - // set the metadata - metadataHandle1.getCollections().addAll("http://bbq.com/contributor/AuntSally"); - metadataHandle2.getCollections().addAll("http://bbq.com/contributor/BigTex"); - metadataHandle3.getCollections().addAll("http://bbq.com/contributor/Dubois"); - metadataHandle4.getCollections().addAll("http://bbq.com/contributor/BigTex"); - metadataHandle5.getCollections().addAll("http://bbq.com/contributor/Dorothy"); - - // write docs - writeDocumentUsingInputStreamHandle(client, filename1, "/combination-constraint/", metadataHandle1, "XML"); - writeDocumentUsingInputStreamHandle(client, filename2, "/combination-constraint/", metadataHandle2, "XML"); - writeDocumentUsingInputStreamHandle(client, filename3, "/combination-constraint/", metadataHandle3, "XML"); - writeDocumentUsingInputStreamHandle(client, filename4, "/combination-constraint/", metadataHandle4, "XML"); - writeDocumentUsingInputStreamHandle(client, filename5, "/combination-constraint/", metadataHandle5, "XML"); - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("intitle:BBQ AND flavor:smok* AND heat:moderate AND contributor:AuntSally AND (summary:Southern AND summary:classic)"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("/combination-constraint/bbq1.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); - assertXpathEvaluatesTo("1", "string(//*[local-name()='facet']//*[local-name()='facet-value']//@*[local-name()='count'])", resultDoc); - assertXpathEvaluatesTo("Moderate (500 - 2500)", "string(//*[local-name()='facet']//*[local-name()='facet-value'])", resultDoc); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test - public void testConstraintCombinationWordAndCollection() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testConstraintCombinationWordAndCollection"); - - String filename1 = "bbq1.xml"; - String filename2 = "bbq2.xml"; - String filename3 = "bbq3.xml"; - String filename4 = "bbq4.xml"; - String filename5 = "bbq5.xml"; - - String queryOptionName = "constraintCombinationOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create and initialize a handle on the metadata - DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle4 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle5 = new DocumentMetadataHandle(); - - // set the metadata - metadataHandle1.getCollections().addAll("http://bbq.com/contributor/AuntSally"); - metadataHandle2.getCollections().addAll("http://bbq.com/contributor/BigTex"); - metadataHandle3.getCollections().addAll("http://bbq.com/contributor/Dubois"); - metadataHandle4.getCollections().addAll("http://bbq.com/contributor/BigTex"); - metadataHandle5.getCollections().addAll("http://bbq.com/contributor/Dorothy"); - - // write docs - writeDocumentUsingInputStreamHandle(client, filename1, "/combination-constraint/", metadataHandle1, "XML"); - writeDocumentUsingInputStreamHandle(client, filename2, "/combination-constraint/", metadataHandle2, "XML"); - writeDocumentUsingInputStreamHandle(client, filename3, "/combination-constraint/", metadataHandle3, "XML"); - writeDocumentUsingInputStreamHandle(client, filename4, "/combination-constraint/", metadataHandle4, "XML"); - writeDocumentUsingInputStreamHandle(client, filename5, "/combination-constraint/", metadataHandle5, "XML"); - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("intitle:pigs contributor:BigTex"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("/combination-constraint/bbq4.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test - public void testConstraintCombinationFieldAndRange() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("testConstraintCombinationFieldAndRange"); - - String filename1 = "bbq1.xml"; - String filename2 = "bbq2.xml"; - String filename3 = "bbq3.xml"; - String filename4 = "bbq4.xml"; - String filename5 = "bbq5.xml"; - - String queryOptionName = "constraintCombinationOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create and initialize a handle on the metadata - DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle4 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle5 = new DocumentMetadataHandle(); - - // set the metadata - metadataHandle1.getCollections().addAll("http://bbq.com/contributor/AuntSally"); - metadataHandle2.getCollections().addAll("http://bbq.com/contributor/BigTex"); - metadataHandle3.getCollections().addAll("http://bbq.com/contributor/Dubois"); - metadataHandle4.getCollections().addAll("http://bbq.com/contributor/BigTex"); - metadataHandle5.getCollections().addAll("http://bbq.com/contributor/Dorothy"); - - // write docs - writeDocumentUsingInputStreamHandle(client, filename1, "/combination-constraint/", metadataHandle1, "XML"); - writeDocumentUsingInputStreamHandle(client, filename2, "/combination-constraint/", metadataHandle2, "XML"); - writeDocumentUsingInputStreamHandle(client, filename3, "/combination-constraint/", metadataHandle3, "XML"); - writeDocumentUsingInputStreamHandle(client, filename4, "/combination-constraint/", metadataHandle4, "XML"); - writeDocumentUsingInputStreamHandle(client, filename5, "/combination-constraint/", metadataHandle5, "XML"); - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("summary:Louisiana AND summary:sweet heat:moderate"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("/combination-constraint/bbq3.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); - - // release client - client.release(); - } -@AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } -} +package com.marklogic.javaclient; + +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.StringQueryDefinition; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.DocumentMetadataHandle; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.*; +public class TestConstraintCombination extends BasicJavaClientREST { + + private static String dbName = "ConstraintCombinationDB"; + private static String [] fNames = {"ConstraintCombinationDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + +@BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + addRangeElementAttributeIndex(dbName, "dateTime", "http://example.com", "entry", "", "date"); + addRangeElementIndex(dbName, "int", "http://example.com", "scoville"); + addRangeElementIndex(dbName, "decimal", "http://example.com", "rating"); + addField(dbName, "bbqtext"); + includeElementField(dbName, "bbqtext", "http://example.com", "title"); + includeElementField(dbName, "bbqtext", "http://example.com", "abstract"); + enableCollectionLexicon(dbName); + enableTrailingWildcardSearches(dbName); + } + +@After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + +@Test + public void testConstraintCombination() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testConstraintCombination"); + + String filename1 = "bbq1.xml"; + String filename2 = "bbq2.xml"; + String filename3 = "bbq3.xml"; + String filename4 = "bbq4.xml"; + String filename5 = "bbq5.xml"; + + String queryOptionName = "constraintCombinationOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle4 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle5 = new DocumentMetadataHandle(); + + // set the metadata + metadataHandle1.getCollections().addAll("http://bbq.com/contributor/AuntSally"); + metadataHandle2.getCollections().addAll("http://bbq.com/contributor/BigTex"); + metadataHandle3.getCollections().addAll("http://bbq.com/contributor/Dubois"); + metadataHandle4.getCollections().addAll("http://bbq.com/contributor/BigTex"); + metadataHandle5.getCollections().addAll("http://bbq.com/contributor/Dorothy"); + + // write docs + writeDocumentUsingInputStreamHandle(client, filename1, "/combination-constraint/", metadataHandle1, "XML"); + writeDocumentUsingInputStreamHandle(client, filename2, "/combination-constraint/", metadataHandle2, "XML"); + writeDocumentUsingInputStreamHandle(client, filename3, "/combination-constraint/", metadataHandle3, "XML"); + writeDocumentUsingInputStreamHandle(client, filename4, "/combination-constraint/", metadataHandle4, "XML"); + writeDocumentUsingInputStreamHandle(client, filename5, "/combination-constraint/", metadataHandle5, "XML"); + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("intitle:BBQ AND flavor:smok* AND heat:moderate AND contributor:AuntSally AND (summary:Southern AND summary:classic)"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("/combination-constraint/bbq1.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); + assertXpathEvaluatesTo("1", "string(//*[local-name()='facet']//*[local-name()='facet-value']//@*[local-name()='count'])", resultDoc); + assertXpathEvaluatesTo("Moderate (500 - 2500)", "string(//*[local-name()='facet']//*[local-name()='facet-value'])", resultDoc); + + // release client + client.release(); + } + +@Test + public void testConstraintCombinationWordAndCollection() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testConstraintCombinationWordAndCollection"); + + String filename1 = "bbq1.xml"; + String filename2 = "bbq2.xml"; + String filename3 = "bbq3.xml"; + String filename4 = "bbq4.xml"; + String filename5 = "bbq5.xml"; + + String queryOptionName = "constraintCombinationOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle4 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle5 = new DocumentMetadataHandle(); + + // set the metadata + metadataHandle1.getCollections().addAll("http://bbq.com/contributor/AuntSally"); + metadataHandle2.getCollections().addAll("http://bbq.com/contributor/BigTex"); + metadataHandle3.getCollections().addAll("http://bbq.com/contributor/Dubois"); + metadataHandle4.getCollections().addAll("http://bbq.com/contributor/BigTex"); + metadataHandle5.getCollections().addAll("http://bbq.com/contributor/Dorothy"); + + // write docs + writeDocumentUsingInputStreamHandle(client, filename1, "/combination-constraint/", metadataHandle1, "XML"); + writeDocumentUsingInputStreamHandle(client, filename2, "/combination-constraint/", metadataHandle2, "XML"); + writeDocumentUsingInputStreamHandle(client, filename3, "/combination-constraint/", metadataHandle3, "XML"); + writeDocumentUsingInputStreamHandle(client, filename4, "/combination-constraint/", metadataHandle4, "XML"); + writeDocumentUsingInputStreamHandle(client, filename5, "/combination-constraint/", metadataHandle5, "XML"); + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("intitle:pigs contributor:BigTex"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("/combination-constraint/bbq4.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); + + // release client + client.release(); + } + +@Test + public void testConstraintCombinationFieldAndRange() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("testConstraintCombinationFieldAndRange"); + + String filename1 = "bbq1.xml"; + String filename2 = "bbq2.xml"; + String filename3 = "bbq3.xml"; + String filename4 = "bbq4.xml"; + String filename5 = "bbq5.xml"; + + String queryOptionName = "constraintCombinationOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle4 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle5 = new DocumentMetadataHandle(); + + // set the metadata + metadataHandle1.getCollections().addAll("http://bbq.com/contributor/AuntSally"); + metadataHandle2.getCollections().addAll("http://bbq.com/contributor/BigTex"); + metadataHandle3.getCollections().addAll("http://bbq.com/contributor/Dubois"); + metadataHandle4.getCollections().addAll("http://bbq.com/contributor/BigTex"); + metadataHandle5.getCollections().addAll("http://bbq.com/contributor/Dorothy"); + + // write docs + writeDocumentUsingInputStreamHandle(client, filename1, "/combination-constraint/", metadataHandle1, "XML"); + writeDocumentUsingInputStreamHandle(client, filename2, "/combination-constraint/", metadataHandle2, "XML"); + writeDocumentUsingInputStreamHandle(client, filename3, "/combination-constraint/", metadataHandle3, "XML"); + writeDocumentUsingInputStreamHandle(client, filename4, "/combination-constraint/", metadataHandle4, "XML"); + writeDocumentUsingInputStreamHandle(client, filename5, "/combination-constraint/", metadataHandle5, "XML"); + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("summary:Louisiana AND summary:sweet heat:moderate"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("/combination-constraint/bbq3.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); + + // release client + client.release(); + } +@AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestDOMHandle.java b/test-complete/src/test/java/com/marklogic/javaclient/TestDOMHandle.java index a5d11095b..a3528c1c1 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestDOMHandle.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestDOMHandle.java @@ -1,105 +1,100 @@ -package com.marklogic.javaclient; - -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.InputStreamHandle; -import com.marklogic.javaclient.BasicJavaClientREST; - -import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; -import static org.junit.Assert.*; - -import org.custommonkey.xmlunit.XMLUnit; -import org.junit.*; -public class TestDOMHandle extends BasicJavaClientREST { - - - private static String dbName = "DOMHandleDB"; - private static String [] fNames = {"DOMHandleDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - @BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - } - - - @SuppressWarnings("deprecation") - @Test public void testXmlCRUD() throws IOException, SAXException, ParserConfigurationException - { - String filename = "xml-original-test.xml"; - String uri = "/write-xml-domhandle/"; - - System.out.println("Running testXmlCRUD"); - - XMLUnit.setIgnoreWhitespace(true); - XMLUnit.setNormalizeWhitespace(true); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - writeDocumentUsingDOMHandle(client, filename, uri, "XML"); - - // read docs - DOMHandle contentHandle = readDocumentUsingDOMHandle(client, uri + filename, "XML"); - - Document readDoc = contentHandle.get(); - - // get xml document for expected result - Document expectedDoc = expectedXMLDocument(filename); - - assertXMLEqual("Write XML difference", expectedDoc, readDoc); - - // update the doc - // acquire the content for update - String updateFilename = "xml-updated-test.xml"; - updateDocumentUsingDOMHandle(client, updateFilename, uri + filename, "XML"); - - // read the document - DOMHandle updateHandle = readDocumentUsingDOMHandle(client, uri + filename, "XML"); - - Document readDocUpdate = updateHandle.get(); - - // get xml document for expected result - Document expectedDocUpdate = expectedXMLDocument(updateFilename); - - assertXMLEqual("Write XML difference", expectedDocUpdate, readDocUpdate); - - // delete the document - deleteDocument(client, uri + filename, "XML"); - - // read the deleted document - String exception = ""; - try - { - InputStreamHandle deleteHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "XML"); - } catch (Exception e) { exception = e.toString(); } - - String expectedException = "Could not read non-existent document"; - boolean documentIsDeleted = exception.contains(expectedException); - assertTrue("Document is not deleted", documentIsDeleted); - - //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "XML")); - - // release client - client.release(); - } -@AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} +package com.marklogic.javaclient; + +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.javaclient.BasicJavaClientREST; + +import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; +import static org.junit.Assert.*; + +import org.custommonkey.xmlunit.XMLUnit; +import org.junit.*; +public class TestDOMHandle extends BasicJavaClientREST { + + + private static String dbName = "DOMHandleDB"; + private static String [] fNames = {"DOMHandleDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + } + + @Test public void testXmlCRUD() throws IOException, SAXException, ParserConfigurationException + { + String filename = "xml-original-test.xml"; + String uri = "/write-xml-domhandle/"; + + System.out.println("Running testXmlCRUD"); + + XMLUnit.setIgnoreWhitespace(true); + XMLUnit.setNormalizeWhitespace(true); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + writeDocumentUsingDOMHandle(client, filename, uri, "XML"); + + // read docs + DOMHandle contentHandle = readDocumentUsingDOMHandle(client, uri + filename, "XML"); + + Document readDoc = contentHandle.get(); + + // get xml document for expected result + Document expectedDoc = expectedXMLDocument(filename); + + assertXMLEqual("Write XML difference", expectedDoc, readDoc); + + // update the doc + // acquire the content for update + String updateFilename = "xml-updated-test.xml"; + updateDocumentUsingDOMHandle(client, updateFilename, uri + filename, "XML"); + + // read the document + DOMHandle updateHandle = readDocumentUsingDOMHandle(client, uri + filename, "XML"); + + Document readDocUpdate = updateHandle.get(); + + // get xml document for expected result + Document expectedDocUpdate = expectedXMLDocument(updateFilename); + + assertXMLEqual("Write XML difference", expectedDocUpdate, readDocUpdate); + + // delete the document + deleteDocument(client, uri + filename, "XML"); + + // read the deleted document + String exception = ""; + try + { + readDocumentUsingInputStreamHandle(client, uri + filename, "XML"); + } catch (Exception e) { exception = e.toString(); } + + String expectedException = "Could not read non-existent document"; + boolean documentIsDeleted = exception.contains(expectedException); + assertTrue("Document is not deleted", documentIsDeleted); + + //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "XML")); + + // release client + client.release(); + } +@AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestDocumentEncoding.java b/test-complete/src/test/java/com/marklogic/javaclient/TestDocumentEncoding.java index 3dae8dae9..e39956f03 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestDocumentEncoding.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestDocumentEncoding.java @@ -1,135 +1,134 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.IOException; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.OutputKeys; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; - -import org.w3c.dom.DOMImplementation; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.Text; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.io.BytesHandle; -import com.marklogic.client.io.StringHandle; -import org.junit.*; - -public class TestDocumentEncoding extends BasicJavaClientREST -{ - private static String dbName = "DocumentEncodingDB"; - private static String [] fNames = {"DocumentEncodingDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - @BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - } - - @SuppressWarnings("deprecation") - @Test - public void testEncoding() throws IOException, TransformerException, ParserConfigurationException - { - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - DocumentBuilder builder = factory.newDocumentBuilder(); - DOMImplementation impl = builder.getDOMImplementation(); - - Document doc = impl.createDocument(null,null,null); - Element e1 = doc.createElement("howto"); - doc.appendChild(e1); - Element e2 = doc.createElement("java"); - e1.appendChild(e2); - e2.setAttribute("url","http://www.rgagnon.com/howto.html"); - Text text = doc.createTextNode("漢字"); - e2.appendChild(text); - - // transform the Document into a String - DOMSource domSource = new DOMSource(doc); - TransformerFactory tf = TransformerFactory.newInstance(); - Transformer transformer = tf.newTransformer(); - transformer.setOutputProperty(OutputKeys.METHOD, "xml"); - transformer.setOutputProperty(OutputKeys.ENCODING,"Cp1252"); - transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); - transformer.setOutputProperty(OutputKeys.INDENT, "yes"); - java.io.StringWriter sw = new java.io.StringWriter(); - StreamResult sr = new StreamResult(sw); - transformer.transform(domSource, sr); - - String xml = sw.toString(); - System.out.println(xml); - - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - StringHandle writeHandle = new StringHandle(); - writeHandle.set(xml); - docMgr.write("/doc/foo.xml", writeHandle); - - System.out.println(docMgr.read("/doc/foo.xml", new StringHandle()).get()); - int length1 = docMgr.read("/doc/foo.xml", new BytesHandle()).get().length; - System.out.println(length1); - - // ************************ - - DocumentBuilderFactory factory2 = DocumentBuilderFactory.newInstance(); - DocumentBuilder builder2 = factory2.newDocumentBuilder(); - DOMImplementation impl2 = builder2.getDOMImplementation(); - - Document doc2 = impl2.createDocument(null,null,null); - Element x1 = doc2.createElement("howto"); - doc2.appendChild(x1); - Element x2 = doc2.createElement("java"); - x1.appendChild(x2); - x2.setAttribute("url","http://www.rgagnon.com/howto.html"); - Text text2 = doc2.createTextNode("漢字"); - x2.appendChild(text2); - - DOMSource domSource2 = new DOMSource(doc2); - TransformerFactory tf2 = TransformerFactory.newInstance(); - Transformer transformer2 = tf2.newTransformer(); - transformer2.setOutputProperty(OutputKeys.METHOD, "xml"); - transformer2.setOutputProperty(OutputKeys.ENCODING,"UTF-8"); - transformer2.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); - transformer2.setOutputProperty(OutputKeys.INDENT, "yes"); - java.io.StringWriter sw2 = new java.io.StringWriter(); - StreamResult sr2 = new StreamResult(sw2); - transformer2.transform(domSource2, sr2); - String xml2 = sw2.toString(); - - System.out.println("*********** UTF-8 ************"); - System.out.println(xml2); - - StringHandle writeHandle2 = new StringHandle(); - writeHandle2.set(xml2); - docMgr.write("/doc/bar.xml", writeHandle2); - System.out.println(docMgr.read("/doc/bar.xml", new StringHandle()).get()); - int length2 = docMgr.read("/doc/bar.xml", new BytesHandle()).get().length; - System.out.println(length2); - - assertEquals("Byte size is not the same", length1, length2); - - // ************************** - - client.release(); - } - @AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.IOException; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Text; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.io.BytesHandle; +import com.marklogic.client.io.StringHandle; +import org.junit.*; + +public class TestDocumentEncoding extends BasicJavaClientREST +{ + private static String dbName = "DocumentEncodingDB"; + private static String [] fNames = {"DocumentEncodingDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + } + + @Test + public void testEncoding() throws IOException, TransformerException, ParserConfigurationException + { + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + DOMImplementation impl = builder.getDOMImplementation(); + + Document doc = impl.createDocument(null,null,null); + Element e1 = doc.createElement("howto"); + doc.appendChild(e1); + Element e2 = doc.createElement("java"); + e1.appendChild(e2); + e2.setAttribute("url","http://www.rgagnon.com/howto.html"); + Text text = doc.createTextNode("漢字"); + e2.appendChild(text); + + // transform the Document into a String + DOMSource domSource = new DOMSource(doc); + TransformerFactory tf = TransformerFactory.newInstance(); + Transformer transformer = tf.newTransformer(); + transformer.setOutputProperty(OutputKeys.METHOD, "xml"); + transformer.setOutputProperty(OutputKeys.ENCODING,"Cp1252"); + transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + java.io.StringWriter sw = new java.io.StringWriter(); + StreamResult sr = new StreamResult(sw); + transformer.transform(domSource, sr); + + String xml = sw.toString(); + System.out.println(xml); + + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + StringHandle writeHandle = new StringHandle(); + writeHandle.set(xml); + docMgr.write("/doc/foo.xml", writeHandle); + + System.out.println(docMgr.read("/doc/foo.xml", new StringHandle()).get()); + int length1 = docMgr.read("/doc/foo.xml", new BytesHandle()).get().length; + System.out.println(length1); + + // ************************ + + DocumentBuilderFactory factory2 = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder2 = factory2.newDocumentBuilder(); + DOMImplementation impl2 = builder2.getDOMImplementation(); + + Document doc2 = impl2.createDocument(null,null,null); + Element x1 = doc2.createElement("howto"); + doc2.appendChild(x1); + Element x2 = doc2.createElement("java"); + x1.appendChild(x2); + x2.setAttribute("url","http://www.rgagnon.com/howto.html"); + Text text2 = doc2.createTextNode("漢字"); + x2.appendChild(text2); + + DOMSource domSource2 = new DOMSource(doc2); + TransformerFactory tf2 = TransformerFactory.newInstance(); + Transformer transformer2 = tf2.newTransformer(); + transformer2.setOutputProperty(OutputKeys.METHOD, "xml"); + transformer2.setOutputProperty(OutputKeys.ENCODING,"UTF-8"); + transformer2.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); + transformer2.setOutputProperty(OutputKeys.INDENT, "yes"); + java.io.StringWriter sw2 = new java.io.StringWriter(); + StreamResult sr2 = new StreamResult(sw2); + transformer2.transform(domSource2, sr2); + String xml2 = sw2.toString(); + + System.out.println("*********** UTF-8 ************"); + System.out.println(xml2); + + StringHandle writeHandle2 = new StringHandle(); + writeHandle2.set(xml2); + docMgr.write("/doc/bar.xml", writeHandle2); + System.out.println(docMgr.read("/doc/bar.xml", new StringHandle()).get()); + int length2 = docMgr.read("/doc/bar.xml", new BytesHandle()).get().length; + System.out.println(length2); + + assertEquals("Byte size is not the same", length1, length2); + + // ************************** + + client.release(); + } + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestDocumentFormat.java b/test-complete/src/test/java/com/marklogic/javaclient/TestDocumentFormat.java index 5a4822e10..153558abc 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestDocumentFormat.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestDocumentFormat.java @@ -1,679 +1,654 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.File; -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.document.DocumentManager; -import com.marklogic.client.io.Format; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.FileHandle; -import org.junit.*; -public class TestDocumentFormat extends BasicJavaClientREST { - - private static String dbName = "TestDocumentFormatDB"; - private static String [] fNames = {"TestDocumentFormat-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - @BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - } - - @SuppressWarnings("deprecation") - @Test - public void testXMLFormatOnXML() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testXMLFormatOnXML"); - - String filename = "flipper.xml"; - String uri = "/xml-format-xml-file/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setFormat(Format.XML); - - // create docId - String docId = uri + filename; - - docMgr.write(docId, handle); - - String expectedUri = uri + filename; - String docUri = docMgr.exists(expectedUri).getUri(); - assertEquals("URI is not found", expectedUri, docUri); - - // release the client - client.release(); - } - - @SuppressWarnings("deprecation") - @Test - public void testJSONFormatOnXML() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testJSONFormatOnXML"); - - String filename = "flipper.xml"; - String uri = "/json-format-xml-file/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setFormat(Format.JSON); - - // create docId - String docId = uri + filename; - - String exception = ""; - String expectedException = ""; - - try - { - docMgr.write(docId, handle); - } catch (Exception e) { exception = e.toString(); } - - boolean isExceptionThrown = exception.contains(expectedException); - assertTrue("Exception is not thrown", isExceptionThrown); - - // release the client - client.release(); - } - - @SuppressWarnings("deprecation") - @Test - public void testBinaryFormatOnXML() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testBinaryFormatOnXML"); - - String filename = "flipper.xml"; - String uri = "/bin-format-xml-file/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setFormat(Format.BINARY); - - // create docId - String docId = uri + filename; - - docMgr.write(docId, handle); - - String expectedUri = uri + filename; - String docUri = docMgr.exists(expectedUri).getUri(); - assertEquals("URI is not found", expectedUri, docUri); - - // release the client - client.release(); - } - - @SuppressWarnings("deprecation") - @Test - public void testTextFormatOnXML() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testTextFormatOnXML"); - - String filename = "flipper.xml"; - String uri = "/txt-format-xml-file/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setFormat(Format.TEXT); - - // create docId - String docId = uri + filename; - - docMgr.write(docId, handle); - - String expectedUri = uri + filename; - String docUri = docMgr.exists(expectedUri).getUri(); - assertEquals("URI is not found", expectedUri, docUri); - - // release the client - client.release(); - } - - @SuppressWarnings("deprecation") - @Test - public void testJSONFormatOnJSON() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testJSONFormatOnJSON"); - - String filename = "json-original.json"; - String uri = "/json-format-json-file/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setFormat(Format.JSON); - - // create docId - String docId = uri + filename; - - docMgr.write(docId, handle); - - String expectedUri = uri + filename; - String docUri = docMgr.exists(expectedUri).getUri(); - assertEquals("URI is not found", expectedUri, docUri); - - // release the client - client.release(); - } - - @SuppressWarnings("deprecation") - @Test - public void testXMLFormatOnJSON() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testXMLFormatOnJSON"); - - String filename = "json-original.json"; - String uri = "/xml-format-json-file/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setFormat(Format.XML); - - // create docId - String docId = uri + filename; - - docMgr.write(docId, handle); - - String expectedUri = uri + filename; - String docUri = docMgr.exists(expectedUri).getUri(); - assertEquals("URI is not found", expectedUri, docUri); - - // release the client - client.release(); - } - - @SuppressWarnings("deprecation") - @Test - public void testBinaryFormatOnJSON() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testBinaryFormatOnJSON"); - - String filename = "json-original.json"; - String uri = "/bin-format-json-file/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setFormat(Format.BINARY); - - // create docId - String docId = uri + filename; - - docMgr.write(docId, handle); - - String expectedUri = uri + filename; - String docUri = docMgr.exists(expectedUri).getUri(); - assertEquals("URI is not found", expectedUri, docUri); - - // release the client - client.release(); - } - - @SuppressWarnings("deprecation") - @Test - public void testTextFormatOnJSON() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testTextFormatOnJSON"); - - String filename = "json-original.json"; - String uri = "/txt-format-json-file/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setFormat(Format.TEXT); - - // create docId - String docId = uri + filename; - - docMgr.write(docId, handle); - - String expectedUri = uri + filename; - String docUri = docMgr.exists(expectedUri).getUri(); - assertEquals("URI is not found", expectedUri, docUri); - - // release the client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testBinaryFormatOnBinary() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testBinaryFormatOnBinary"); - - String filename = "Pandakarlino.jpg"; - String uri = "/bin-format-bin-file/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setFormat(Format.BINARY); - - // create docId - String docId = uri + filename; - - docMgr.write(docId, handle); - - String expectedUri = uri + filename; - String docUri = docMgr.exists(expectedUri).getUri(); - assertEquals("URI is not found", expectedUri, docUri); - - // release the client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testXMLFormatOnBinary() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testXMLFormatOnBinary"); - - String filename = "Pandakarlino.jpg"; - String uri = "/xml-format-bin-file/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setFormat(Format.XML); - - // create docId - String docId = uri + filename; - - String exception = ""; - String expectedException = ""; - - try - { - docMgr.write(docId, handle); - } catch (Exception e) { exception = e.toString(); } - - boolean isExceptionThrown = exception.contains(expectedException); - assertTrue("Exception is not thrown", isExceptionThrown); - - // release the client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testJSONFormatOnBinary() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testJSONFormatOnBinary"); - - String filename = "Pandakarlino.jpg"; - String uri = "/json-format-bin-file/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setFormat(Format.JSON); - - // create docId - String docId = uri + filename; - - String exception = ""; - String expectedException = ""; - - try - { - docMgr.write(docId, handle); - } catch (Exception e) { exception = e.toString(); } - - boolean isExceptionThrown = exception.contains(expectedException); - assertTrue("Exception is not thrown", isExceptionThrown); - - // release the client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testTextFormatOnBinary() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testTextFormatOnBinary"); - - String filename = "Pandakarlino.jpg"; - String uri = "/bin-format-bin-file/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setFormat(Format.TEXT); - - // create docId - String docId = uri + filename; - - String exception = ""; - String expectedException = ""; - - try - { - docMgr.write(docId, handle); - } catch (Exception e) { exception = e.toString(); } - - boolean isExceptionThrown = exception.contains(expectedException); - assertTrue("Exception is not thrown", isExceptionThrown); - - // release the client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testTextFormatOnText() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testTextFormatOnText"); - - String filename = "text-original.txt"; - String uri = "/txt-format-txt-file/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setFormat(Format.TEXT); - - // create docId - String docId = uri + filename; - - docMgr.write(docId, handle); - - String expectedUri = uri + filename; - String docUri = docMgr.exists(expectedUri).getUri(); - assertEquals("URI is not found", expectedUri, docUri); - - // release the client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testXMLFormatOnText() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testXMLFormatOnText"); - - String filename = "text-original.txt"; - String uri = "/xml-format-txt-file/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setFormat(Format.XML); - - // create docId - String docId = uri + filename; - - docMgr.write(docId, handle); - - String expectedUri = uri + filename; - String docUri = docMgr.exists(expectedUri).getUri(); - assertEquals("URI is not found", expectedUri, docUri); - - // release the client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testJSONFormatOnText() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testJSONFormatOnText"); - - String filename = "text-original.txt"; - String uri = "/json-format-txt-file/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setFormat(Format.JSON); - - // create docId - String docId = uri + filename; - - String exception = ""; - String expectedException = ""; - - try - { - docMgr.write(docId, handle); - } catch (Exception e) { exception = e.toString(); } - - boolean isExceptionThrown = exception.contains(expectedException); - assertTrue("Exception is not thrown", isExceptionThrown); - - // release the client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testBinaryFormatOnText() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testBinaryFormatOnText"); - - String filename = "text-original.txt"; - String uri = "/bin-format-txt-file/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setFormat(Format.BINARY); - - // create docId - String docId = uri + filename; - - docMgr.write(docId, handle); - - String expectedUri = uri + filename; - String docUri = docMgr.exists(expectedUri).getUri(); - assertEquals("URI is not found", expectedUri, docUri); - - // release the client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testNegativeJSONFormatWithDOMHandle() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testNegativeJSONFormatWithDOMHandle"); - - String filename = "xml-original.xml"; - String uri = "/negative-format/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - Document readDoc = expectedXMLDocument(filename); - - //File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - DOMHandle handle = new DOMHandle(); - handle.set(readDoc); - - String exception = ""; - String expectedException = "java.lang.IllegalArgumentException: DOMHandle supports the XML format only"; - - try - { - handle.setFormat(Format.JSON); - } catch (IllegalArgumentException e) { exception = e.toString(); } - - boolean isExceptionThrown = exception.contains(expectedException); - assertTrue("Wrong exception", isExceptionThrown); - - // release the client - client.release(); - } -@AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.DocumentManager; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.FileHandle; +import org.junit.*; +public class TestDocumentFormat extends BasicJavaClientREST { + + private static String dbName = "TestDocumentFormatDB"; + private static String [] fNames = {"TestDocumentFormat-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + } + + @Test + public void testXMLFormatOnXML() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testXMLFormatOnXML"); + + String filename = "flipper.xml"; + String uri = "/xml-format-xml-file/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setFormat(Format.XML); + + // create docId + String docId = uri + filename; + + docMgr.write(docId, handle); + + String expectedUri = uri + filename; + String docUri = docMgr.exists(expectedUri).getUri(); + assertEquals("URI is not found", expectedUri, docUri); + + // release the client + client.release(); + } + + @Test + public void testJSONFormatOnXML() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testJSONFormatOnXML"); + + String filename = "flipper.xml"; + String uri = "/json-format-xml-file/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setFormat(Format.JSON); + + // create docId + String docId = uri + filename; + + String exception = ""; + String expectedException = ""; + + try + { + docMgr.write(docId, handle); + } catch (Exception e) { exception = e.toString(); } + + boolean isExceptionThrown = exception.contains(expectedException); + assertTrue("Exception is not thrown", isExceptionThrown); + + // release the client + client.release(); + } + + @Test + public void testBinaryFormatOnXML() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testBinaryFormatOnXML"); + + String filename = "flipper.xml"; + String uri = "/bin-format-xml-file/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setFormat(Format.BINARY); + + // create docId + String docId = uri + filename; + + docMgr.write(docId, handle); + + String expectedUri = uri + filename; + String docUri = docMgr.exists(expectedUri).getUri(); + assertEquals("URI is not found", expectedUri, docUri); + + // release the client + client.release(); + } + + @Test + public void testTextFormatOnXML() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testTextFormatOnXML"); + + String filename = "flipper.xml"; + String uri = "/txt-format-xml-file/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setFormat(Format.TEXT); + + // create docId + String docId = uri + filename; + + docMgr.write(docId, handle); + + String expectedUri = uri + filename; + String docUri = docMgr.exists(expectedUri).getUri(); + assertEquals("URI is not found", expectedUri, docUri); + + // release the client + client.release(); + } + + @Test + public void testJSONFormatOnJSON() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testJSONFormatOnJSON"); + + String filename = "json-original.json"; + String uri = "/json-format-json-file/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setFormat(Format.JSON); + + // create docId + String docId = uri + filename; + + docMgr.write(docId, handle); + + String expectedUri = uri + filename; + String docUri = docMgr.exists(expectedUri).getUri(); + assertEquals("URI is not found", expectedUri, docUri); + + // release the client + client.release(); + } + + @Test + public void testXMLFormatOnJSON() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testXMLFormatOnJSON"); + + String filename = "json-original.json"; + String uri = "/xml-format-json-file/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setFormat(Format.XML); + + // create docId + String docId = uri + filename; + + docMgr.write(docId, handle); + + String expectedUri = uri + filename; + String docUri = docMgr.exists(expectedUri).getUri(); + assertEquals("URI is not found", expectedUri, docUri); + + // release the client + client.release(); + } + + @Test + public void testBinaryFormatOnJSON() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testBinaryFormatOnJSON"); + + String filename = "json-original.json"; + String uri = "/bin-format-json-file/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setFormat(Format.BINARY); + + // create docId + String docId = uri + filename; + + docMgr.write(docId, handle); + + String expectedUri = uri + filename; + String docUri = docMgr.exists(expectedUri).getUri(); + assertEquals("URI is not found", expectedUri, docUri); + + // release the client + client.release(); + } + + @Test + public void testTextFormatOnJSON() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testTextFormatOnJSON"); + + String filename = "json-original.json"; + String uri = "/txt-format-json-file/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setFormat(Format.TEXT); + + // create docId + String docId = uri + filename; + + docMgr.write(docId, handle); + + String expectedUri = uri + filename; + String docUri = docMgr.exists(expectedUri).getUri(); + assertEquals("URI is not found", expectedUri, docUri); + + // release the client + client.release(); + } + + @Test public void testBinaryFormatOnBinary() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testBinaryFormatOnBinary"); + + String filename = "Pandakarlino.jpg"; + String uri = "/bin-format-bin-file/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setFormat(Format.BINARY); + + // create docId + String docId = uri + filename; + + docMgr.write(docId, handle); + + String expectedUri = uri + filename; + String docUri = docMgr.exists(expectedUri).getUri(); + assertEquals("URI is not found", expectedUri, docUri); + + // release the client + client.release(); + } + + @Test public void testXMLFormatOnBinary() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testXMLFormatOnBinary"); + + String filename = "Pandakarlino.jpg"; + String uri = "/xml-format-bin-file/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setFormat(Format.XML); + + // create docId + String docId = uri + filename; + + String exception = ""; + String expectedException = ""; + + try + { + docMgr.write(docId, handle); + } catch (Exception e) { exception = e.toString(); } + + boolean isExceptionThrown = exception.contains(expectedException); + assertTrue("Exception is not thrown", isExceptionThrown); + + // release the client + client.release(); + } + + @Test public void testJSONFormatOnBinary() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testJSONFormatOnBinary"); + + String filename = "Pandakarlino.jpg"; + String uri = "/json-format-bin-file/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setFormat(Format.JSON); + + // create docId + String docId = uri + filename; + + String exception = ""; + String expectedException = ""; + + try + { + docMgr.write(docId, handle); + } catch (Exception e) { exception = e.toString(); } + + boolean isExceptionThrown = exception.contains(expectedException); + assertTrue("Exception is not thrown", isExceptionThrown); + + // release the client + client.release(); + } + + @Test public void testTextFormatOnBinary() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testTextFormatOnBinary"); + + String filename = "Pandakarlino.jpg"; + String uri = "/bin-format-bin-file/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setFormat(Format.TEXT); + + // create docId + String docId = uri + filename; + + String exception = ""; + String expectedException = ""; + + try + { + docMgr.write(docId, handle); + } catch (Exception e) { exception = e.toString(); } + + boolean isExceptionThrown = exception.contains(expectedException); + assertTrue("Exception is not thrown", isExceptionThrown); + + // release the client + client.release(); + } + + @Test public void testTextFormatOnText() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testTextFormatOnText"); + + String filename = "text-original.txt"; + String uri = "/txt-format-txt-file/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setFormat(Format.TEXT); + + // create docId + String docId = uri + filename; + + docMgr.write(docId, handle); + + String expectedUri = uri + filename; + String docUri = docMgr.exists(expectedUri).getUri(); + assertEquals("URI is not found", expectedUri, docUri); + + // release the client + client.release(); + } + + + @Test public void testXMLFormatOnText() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testXMLFormatOnText"); + + String filename = "text-original.txt"; + String uri = "/xml-format-txt-file/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setFormat(Format.XML); + + // create docId + String docId = uri + filename; + + docMgr.write(docId, handle); + + String expectedUri = uri + filename; + String docUri = docMgr.exists(expectedUri).getUri(); + assertEquals("URI is not found", expectedUri, docUri); + + // release the client + client.release(); + } + + @Test public void testJSONFormatOnText() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testJSONFormatOnText"); + + String filename = "text-original.txt"; + String uri = "/json-format-txt-file/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setFormat(Format.JSON); + + // create docId + String docId = uri + filename; + + String exception = ""; + String expectedException = ""; + + try + { + docMgr.write(docId, handle); + } catch (Exception e) { exception = e.toString(); } + + boolean isExceptionThrown = exception.contains(expectedException); + assertTrue("Exception is not thrown", isExceptionThrown); + + // release the client + client.release(); + } + + @Test public void testBinaryFormatOnText() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testBinaryFormatOnText"); + + String filename = "text-original.txt"; + String uri = "/bin-format-txt-file/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setFormat(Format.BINARY); + + // create docId + String docId = uri + filename; + + docMgr.write(docId, handle); + + String expectedUri = uri + filename; + String docUri = docMgr.exists(expectedUri).getUri(); + assertEquals("URI is not found", expectedUri, docUri); + + // release the client + client.release(); + } + + @Test public void testNegativeJSONFormatWithDOMHandle() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testNegativeJSONFormatWithDOMHandle"); + + String filename = "xml-original.xml"; + String uri = "/negative-format/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + Document readDoc = expectedXMLDocument(filename); + + //File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + DOMHandle handle = new DOMHandle(); + handle.set(readDoc); + + String exception = ""; + String expectedException = "java.lang.IllegalArgumentException: DOMHandle supports the XML format only"; + + try + { + handle.setFormat(Format.JSON); + } catch (IllegalArgumentException e) { exception = e.toString(); } + + boolean isExceptionThrown = exception.contains(expectedException); + assertTrue("Wrong exception", isExceptionThrown); + + // release the client + client.release(); + } +@AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestDocumentMimetype.java b/test-complete/src/test/java/com/marklogic/javaclient/TestDocumentMimetype.java index e326866bd..fbc9f7f38 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestDocumentMimetype.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestDocumentMimetype.java @@ -1,725 +1,695 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.File; -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.document.DocumentManager; -import com.marklogic.client.io.FileHandle; -import org.junit.*; -public class TestDocumentMimetype extends BasicJavaClientREST { - - private static String dbName = "TestDocumentMimetypeDB"; - private static String [] fNames = {"TestDocumentMimetypeDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - @BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - } - - @SuppressWarnings("deprecation") - @Test - public void testMatchedMimetypeOnXML() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testMatchedMimetypeOnXML"); - - String filename = "flipper.xml"; - String uri = "/xml-mimetype/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setMimetype("application/xml"); - - // create docId - String docId = uri + filename; - - docMgr.write(docId, handle); - - String expectedUri = uri + filename; - String docUri = docMgr.exists(expectedUri).getUri(); - assertEquals("URI is not found", expectedUri, docUri); - - // read document format - docMgr.read(docId, handle); - String format = handle.getFormat().name(); - String expectedFormat = "XML"; - - assertEquals("Format does not match", expectedFormat, format); - - // release the client - client.release(); - } - - @SuppressWarnings("deprecation") - @Test - public void testUnknownMimetypeOnXML() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testUnknownMimetypeOnXML"); - - String filename = "flipper.xml"; - String uri = "/xml-mimetype/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setMimetype("application/x-excel"); - - // create docId - String docId = uri + filename; - - docMgr.write(docId, handle); - - String expectedUri = uri + filename; - String docUri = docMgr.exists(expectedUri).getUri(); - assertEquals("URI is not found", expectedUri, docUri); - - // read document mimetype - docMgr.read(docId, handle); - String format = handle.getFormat().name(); - String expectedFormat = "XML"; - - assertEquals("Format does not match", expectedFormat, format); - - // release the client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testUnmatchedMimetypeOnXML() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testUnmatchedMimetypeOnXML"); - - String filename = "flipper.xml"; - String uri = "/xml-mimetype/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setMimetype("image/svg+xml"); - - // create docId - String docId = uri + filename; - - docMgr.write(docId, handle); - - String expectedUri = uri + filename; - String docUri = docMgr.exists(expectedUri).getUri(); - assertEquals("URI is not found", expectedUri, docUri); - - // read document mimetype - docMgr.read(docId, handle); - String format = handle.getFormat().name(); - String expectedFormat = "XML"; - - assertEquals("Format does not match", expectedFormat, format); - - // release the client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testUnsupportedMimetypeOnXML() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testUnsupportedMimetypeOnXML"); - - String filename = "flipper.xml"; - String uri = "/xml-mimetype/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setMimetype("application/vnd.nokia.configuration-message"); - - // create docId - String docId = uri + filename; - - docMgr.write(docId, handle); - - String expectedUri = uri + filename; - String docUri = docMgr.exists(expectedUri).getUri(); - assertEquals("URI is not found", expectedUri, docUri); - - // read document mimetype - docMgr.read(docId, handle); - String format = handle.getFormat().name(); - String expectedFormat = "XML"; - - assertEquals("Format does not match", expectedFormat, format); - - // release the client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testMatchedMimetypeOnJSON() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testMatchedMimetypeOnJSON"); - - String filename = "json-original.json"; - String uri = "/json-mimetype/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setMimetype("application/json"); - - // create docId - String docId = uri + filename; - - docMgr.write(docId, handle); - - String expectedUri = uri + filename; - String docUri = docMgr.exists(expectedUri).getUri(); - assertEquals("URI is not found", expectedUri, docUri); - - // read document format - docMgr.read(docId, handle); - String format = handle.getFormat().name(); - String expectedFormat = "JSON"; - - assertEquals("Format does not match", expectedFormat, format); - - // release the client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testUnknownMimetypeOnJSON() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testUnknownMimetypeOnJSON"); - - String filename = "json-original.json"; - String uri = "/json-mimetype/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setMimetype("image/jpeg"); - - // create docId - String docId = uri + filename; - - docMgr.write(docId, handle); - - String expectedUri = uri + filename; - String docUri = docMgr.exists(expectedUri).getUri(); - assertEquals("URI is not found", expectedUri, docUri); - - // read document mimetype - docMgr.read(docId, handle); - String format = handle.getFormat().name(); - String expectedFormat = "JSON"; - - assertEquals("Format does not match", expectedFormat, format); - - // release the client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testUnmatchedMimetypeOnJSON() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testUnmatchedMimetypeOnJSON"); - - String filename = "json-original.json"; - String uri = "/json-mimetype/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setMimetype("text/html"); - - // create docId - String docId = uri + filename; - - docMgr.write(docId, handle); - - String expectedUri = uri + filename; - String docUri = docMgr.exists(expectedUri).getUri(); - assertEquals("URI is not found", expectedUri, docUri); - - // read document mimetype - docMgr.read(docId, handle); - String format = handle.getFormat().name(); - String expectedFormat = "JSON"; - - assertEquals("Format does not match", expectedFormat, format); - - // release the client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testUnsupportedMimetypeOnJSON() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testUnsupportedMimetypeOnJSON"); - - String filename = "json-original.json"; - String uri = "/json-mimetype/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setMimetype("application/vnd.nokia.configuration-message"); - - // create docId - String docId = uri + filename; - - docMgr.write(docId, handle); - - String expectedUri = uri + filename; - String docUri = docMgr.exists(expectedUri).getUri(); - assertEquals("URI is not found", expectedUri, docUri); - - // read document mimetype - docMgr.read(docId, handle); - String format = handle.getFormat().name(); - String expectedFormat = "JSON"; - - assertEquals("Format does not match", expectedFormat, format); - - // release the client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testMatchedMimetypeOnBinary() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testMatchedMimetypeOnBinary"); - - String filename = "Pandakarlino.jpg"; - String uri = "/bin-mimetype/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setMimetype("image/jpeg"); - - // create docId - String docId = uri + filename; - - docMgr.write(docId, handle); - - String expectedUri = uri + filename; - String docUri = docMgr.exists(expectedUri).getUri(); - assertEquals("URI is not found", expectedUri, docUri); - - // read document format - docMgr.read(docId, handle); - String format = handle.getFormat().name(); - String expectedFormat = "BINARY"; - - assertEquals("Format does not match", expectedFormat, format); - - // release the client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testUnknownMimetypeOnBinary() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testUnknownMimetypeOnBinary"); - - String filename = "Pandakarlino.jpg"; - String uri = "/bin-mimetype/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setMimetype("application/rtf"); - - // create docId - String docId = uri + filename; - - docMgr.write(docId, handle); - - String expectedUri = uri + filename; - String docUri = docMgr.exists(expectedUri).getUri(); - assertEquals("URI is not found", expectedUri, docUri); - - // read document mimetype - docMgr.read(docId, handle); - String format = handle.getFormat().name(); - String expectedFormat = "BINARY"; - - assertEquals("Format does not match", expectedFormat, format); - - // release the client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testUnmatchedMimetypeOnBinary() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testUnmatchedMimetypeOnBinary"); - - String filename = "Pandakarlino.jpg"; - String uri = "/bin-mimetype/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setMimetype("text/rtf"); - - // create docId - String docId = uri + filename; - - docMgr.write(docId, handle); - - String expectedUri = uri + filename; - String docUri = docMgr.exists(expectedUri).getUri(); - assertEquals("URI is not found", expectedUri, docUri); - - // read document mimetype - docMgr.read(docId, handle); - String format = handle.getFormat().name(); - String expectedFormat = "BINARY"; - - assertEquals("Format does not match", expectedFormat, format); - - // release the client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testUnsupportedMimetypeOnBinary() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testUnsupportedMimetypeOnBinary"); - - String filename = "Pandakarlino.jpg"; - String uri = "/bin-mimetype/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setMimetype("application/vnd.nokia.configuration-message"); - - // create docId - String docId = uri + filename; - - docMgr.write(docId, handle); - - String expectedUri = uri + filename; - String docUri = docMgr.exists(expectedUri).getUri(); - assertEquals("URI is not found", expectedUri, docUri); - - // read document mimetype - docMgr.read(docId, handle); - String format = handle.getFormat().name(); - String expectedFormat = "BINARY"; - - assertEquals("Format does not match", expectedFormat, format); - - // release the client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testMatchedMimetypeOnText() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testMatchedMimetypeOnText"); - - String filename = "text-original.txt"; - String uri = "/txt-mimetype/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setMimetype("text/plain"); - - // create docId - String docId = uri + filename; - - docMgr.write(docId, handle); - - String expectedUri = uri + filename; - String docUri = docMgr.exists(expectedUri).getUri(); - assertEquals("URI is not found", expectedUri, docUri); - - // read document format - docMgr.read(docId, handle); - String format = handle.getFormat().name(); - String expectedFormat = "TEXT"; - - assertEquals("Format does not match", expectedFormat, format); - - // release the client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testUnknownMimetypeOnText() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testUnknownMimetypeOnText"); - - String filename = "text-original.txt"; - String uri = "/txt-mimetype/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setMimetype("application/rtf"); - - // create docId - String docId = uri + filename; - - docMgr.write(docId, handle); - - String expectedUri = uri + filename; - String docUri = docMgr.exists(expectedUri).getUri(); - assertEquals("URI is not found", expectedUri, docUri); - - // read document mimetype - docMgr.read(docId, handle); - String format = handle.getFormat().name(); - String expectedFormat = "TEXT"; - - assertEquals("Format does not match", expectedFormat, format); - - // release the client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testUnmatchedMimetypeOnText() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testUnmatchedMimetypeOnText"); - - String filename = "text-original.txt"; - String uri = "/txt-mimetype/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setMimetype("image/jpeg"); - - // create docId - String docId = uri + filename; - - docMgr.write(docId, handle); - - String expectedUri = uri + filename; - String docUri = docMgr.exists(expectedUri).getUri(); - assertEquals("URI is not found", expectedUri, docUri); - - // read document mimetype - docMgr.read(docId, handle); - String format = handle.getFormat().name(); - String expectedFormat = "TEXT"; - - assertEquals("Format does not match", expectedFormat, format); - - // release the client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testUnsupportedMimetypeOnText() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testUnsupportedMimetypeOnText"); - - String filename = "text-original.txt"; - String uri = "/txt-mimetype/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - handle.setMimetype("application/vnd.nokia.configuration-message"); - - // create docId - String docId = uri + filename; - - docMgr.write(docId, handle); - - String expectedUri = uri + filename; - String docUri = docMgr.exists(expectedUri).getUri(); - assertEquals("URI is not found", expectedUri, docUri); - - // read document mimetype - docMgr.read(docId, handle); - String format = handle.getFormat().name(); - String expectedFormat = "TEXT"; - - assertEquals("Format does not match", expectedFormat, format); - - // release the client - client.release(); - } -@AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.DocumentManager; +import com.marklogic.client.io.FileHandle; +import org.junit.*; +public class TestDocumentMimetype extends BasicJavaClientREST { + + private static String dbName = "TestDocumentMimetypeDB"; + private static String [] fNames = {"TestDocumentMimetypeDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + } + + @Test + public void testMatchedMimetypeOnXML() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testMatchedMimetypeOnXML"); + + String filename = "flipper.xml"; + String uri = "/xml-mimetype/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setMimetype("application/xml"); + + // create docId + String docId = uri + filename; + + docMgr.write(docId, handle); + + String expectedUri = uri + filename; + String docUri = docMgr.exists(expectedUri).getUri(); + assertEquals("URI is not found", expectedUri, docUri); + + // read document format + docMgr.read(docId, handle); + String format = handle.getFormat().name(); + String expectedFormat = "XML"; + + assertEquals("Format does not match", expectedFormat, format); + + // release the client + client.release(); + } + + @Test + public void testUnknownMimetypeOnXML() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testUnknownMimetypeOnXML"); + + String filename = "flipper.xml"; + String uri = "/xml-mimetype/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setMimetype("application/x-excel"); + + // create docId + String docId = uri + filename; + + docMgr.write(docId, handle); + + String expectedUri = uri + filename; + String docUri = docMgr.exists(expectedUri).getUri(); + assertEquals("URI is not found", expectedUri, docUri); + + // read document mimetype + docMgr.read(docId, handle); + String format = handle.getFormat().name(); + String expectedFormat = "XML"; + + assertEquals("Format does not match", expectedFormat, format); + + // release the client + client.release(); + } + + @Test public void testUnmatchedMimetypeOnXML() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testUnmatchedMimetypeOnXML"); + + String filename = "flipper.xml"; + String uri = "/xml-mimetype/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setMimetype("image/svg+xml"); + + // create docId + String docId = uri + filename; + + docMgr.write(docId, handle); + + String expectedUri = uri + filename; + String docUri = docMgr.exists(expectedUri).getUri(); + assertEquals("URI is not found", expectedUri, docUri); + + // read document mimetype + docMgr.read(docId, handle); + String format = handle.getFormat().name(); + String expectedFormat = "XML"; + + assertEquals("Format does not match", expectedFormat, format); + + // release the client + client.release(); + } + + @Test public void testUnsupportedMimetypeOnXML() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testUnsupportedMimetypeOnXML"); + + String filename = "flipper.xml"; + String uri = "/xml-mimetype/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setMimetype("application/vnd.nokia.configuration-message"); + + // create docId + String docId = uri + filename; + + docMgr.write(docId, handle); + + String expectedUri = uri + filename; + String docUri = docMgr.exists(expectedUri).getUri(); + assertEquals("URI is not found", expectedUri, docUri); + + // read document mimetype + docMgr.read(docId, handle); + String format = handle.getFormat().name(); + String expectedFormat = "XML"; + + assertEquals("Format does not match", expectedFormat, format); + + // release the client + client.release(); + } + + @Test public void testMatchedMimetypeOnJSON() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testMatchedMimetypeOnJSON"); + + String filename = "json-original.json"; + String uri = "/json-mimetype/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setMimetype("application/json"); + + // create docId + String docId = uri + filename; + + docMgr.write(docId, handle); + + String expectedUri = uri + filename; + String docUri = docMgr.exists(expectedUri).getUri(); + assertEquals("URI is not found", expectedUri, docUri); + + // read document format + docMgr.read(docId, handle); + String format = handle.getFormat().name(); + String expectedFormat = "JSON"; + + assertEquals("Format does not match", expectedFormat, format); + + // release the client + client.release(); + } + + @Test public void testUnknownMimetypeOnJSON() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testUnknownMimetypeOnJSON"); + + String filename = "json-original.json"; + String uri = "/json-mimetype/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setMimetype("image/jpeg"); + + // create docId + String docId = uri + filename; + + docMgr.write(docId, handle); + + String expectedUri = uri + filename; + String docUri = docMgr.exists(expectedUri).getUri(); + assertEquals("URI is not found", expectedUri, docUri); + + // read document mimetype + docMgr.read(docId, handle); + String format = handle.getFormat().name(); + String expectedFormat = "JSON"; + + assertEquals("Format does not match", expectedFormat, format); + + // release the client + client.release(); + } + + @Test public void testUnmatchedMimetypeOnJSON() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testUnmatchedMimetypeOnJSON"); + + String filename = "json-original.json"; + String uri = "/json-mimetype/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setMimetype("text/html"); + + // create docId + String docId = uri + filename; + + docMgr.write(docId, handle); + + String expectedUri = uri + filename; + String docUri = docMgr.exists(expectedUri).getUri(); + assertEquals("URI is not found", expectedUri, docUri); + + // read document mimetype + docMgr.read(docId, handle); + String format = handle.getFormat().name(); + String expectedFormat = "JSON"; + + assertEquals("Format does not match", expectedFormat, format); + + // release the client + client.release(); + } + + @Test public void testUnsupportedMimetypeOnJSON() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testUnsupportedMimetypeOnJSON"); + + String filename = "json-original.json"; + String uri = "/json-mimetype/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setMimetype("application/vnd.nokia.configuration-message"); + + // create docId + String docId = uri + filename; + + docMgr.write(docId, handle); + + String expectedUri = uri + filename; + String docUri = docMgr.exists(expectedUri).getUri(); + assertEquals("URI is not found", expectedUri, docUri); + + // read document mimetype + docMgr.read(docId, handle); + String format = handle.getFormat().name(); + String expectedFormat = "JSON"; + + assertEquals("Format does not match", expectedFormat, format); + + // release the client + client.release(); + } + + @Test public void testMatchedMimetypeOnBinary() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testMatchedMimetypeOnBinary"); + + String filename = "Pandakarlino.jpg"; + String uri = "/bin-mimetype/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setMimetype("image/jpeg"); + + // create docId + String docId = uri + filename; + + docMgr.write(docId, handle); + + String expectedUri = uri + filename; + String docUri = docMgr.exists(expectedUri).getUri(); + assertEquals("URI is not found", expectedUri, docUri); + + // read document format + docMgr.read(docId, handle); + String format = handle.getFormat().name(); + String expectedFormat = "BINARY"; + + assertEquals("Format does not match", expectedFormat, format); + + // release the client + client.release(); + } + + @Test public void testUnknownMimetypeOnBinary() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testUnknownMimetypeOnBinary"); + + String filename = "Pandakarlino.jpg"; + String uri = "/bin-mimetype/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setMimetype("application/rtf"); + + // create docId + String docId = uri + filename; + + docMgr.write(docId, handle); + + String expectedUri = uri + filename; + String docUri = docMgr.exists(expectedUri).getUri(); + assertEquals("URI is not found", expectedUri, docUri); + + // read document mimetype + docMgr.read(docId, handle); + String format = handle.getFormat().name(); + String expectedFormat = "BINARY"; + + assertEquals("Format does not match", expectedFormat, format); + + // release the client + client.release(); + } + + @Test public void testUnmatchedMimetypeOnBinary() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testUnmatchedMimetypeOnBinary"); + + String filename = "Pandakarlino.jpg"; + String uri = "/bin-mimetype/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setMimetype("text/rtf"); + + // create docId + String docId = uri + filename; + + docMgr.write(docId, handle); + + String expectedUri = uri + filename; + String docUri = docMgr.exists(expectedUri).getUri(); + assertEquals("URI is not found", expectedUri, docUri); + + // read document mimetype + docMgr.read(docId, handle); + String format = handle.getFormat().name(); + String expectedFormat = "BINARY"; + + assertEquals("Format does not match", expectedFormat, format); + + // release the client + client.release(); + } + + @Test public void testUnsupportedMimetypeOnBinary() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testUnsupportedMimetypeOnBinary"); + + String filename = "Pandakarlino.jpg"; + String uri = "/bin-mimetype/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setMimetype("application/vnd.nokia.configuration-message"); + + // create docId + String docId = uri + filename; + + docMgr.write(docId, handle); + + String expectedUri = uri + filename; + String docUri = docMgr.exists(expectedUri).getUri(); + assertEquals("URI is not found", expectedUri, docUri); + + // read document mimetype + docMgr.read(docId, handle); + String format = handle.getFormat().name(); + String expectedFormat = "BINARY"; + + assertEquals("Format does not match", expectedFormat, format); + + // release the client + client.release(); + } + + @Test public void testMatchedMimetypeOnText() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testMatchedMimetypeOnText"); + + String filename = "text-original.txt"; + String uri = "/txt-mimetype/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setMimetype("text/plain"); + + // create docId + String docId = uri + filename; + + docMgr.write(docId, handle); + + String expectedUri = uri + filename; + String docUri = docMgr.exists(expectedUri).getUri(); + assertEquals("URI is not found", expectedUri, docUri); + + // read document format + docMgr.read(docId, handle); + String format = handle.getFormat().name(); + String expectedFormat = "TEXT"; + + assertEquals("Format does not match", expectedFormat, format); + + // release the client + client.release(); + } + + @Test public void testUnknownMimetypeOnText() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testUnknownMimetypeOnText"); + + String filename = "text-original.txt"; + String uri = "/txt-mimetype/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setMimetype("application/rtf"); + + // create docId + String docId = uri + filename; + + docMgr.write(docId, handle); + + String expectedUri = uri + filename; + String docUri = docMgr.exists(expectedUri).getUri(); + assertEquals("URI is not found", expectedUri, docUri); + + // read document mimetype + docMgr.read(docId, handle); + String format = handle.getFormat().name(); + String expectedFormat = "TEXT"; + + assertEquals("Format does not match", expectedFormat, format); + + // release the client + client.release(); + } + + @Test public void testUnmatchedMimetypeOnText() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testUnmatchedMimetypeOnText"); + + String filename = "text-original.txt"; + String uri = "/txt-mimetype/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setMimetype("image/jpeg"); + + // create docId + String docId = uri + filename; + + docMgr.write(docId, handle); + + String expectedUri = uri + filename; + String docUri = docMgr.exists(expectedUri).getUri(); + assertEquals("URI is not found", expectedUri, docUri); + + // read document mimetype + docMgr.read(docId, handle); + String format = handle.getFormat().name(); + String expectedFormat = "TEXT"; + + assertEquals("Format does not match", expectedFormat, format); + + // release the client + client.release(); + } + + @Test public void testUnsupportedMimetypeOnText() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testUnsupportedMimetypeOnText"); + + String filename = "text-original.txt"; + String uri = "/txt-mimetype/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + handle.setMimetype("application/vnd.nokia.configuration-message"); + + // create docId + String docId = uri + filename; + + docMgr.write(docId, handle); + + String expectedUri = uri + filename; + String docUri = docMgr.exists(expectedUri).getUri(); + assertEquals("URI is not found", expectedUri, docUri); + + // read document mimetype + docMgr.read(docId, handle); + String format = handle.getFormat().name(); + String expectedFormat = "TEXT"; + + assertEquals("Format does not match", expectedFormat, format); + + // release the client + client.release(); + } +@AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestFieldConstraint.java b/test-complete/src/test/java/com/marklogic/javaclient/TestFieldConstraint.java index 4245eab95..d19971cf6 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestFieldConstraint.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestFieldConstraint.java @@ -1,76 +1,75 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.IOException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.SearchHandle; -import com.marklogic.javaclient.BasicJavaClientREST; - -import org.junit.*; -public class TestFieldConstraint extends BasicJavaClientREST { - static String filenames[] = {"bbq1.xml", "bbq2.xml", "bbq3.xml", "bbq4.xml", "bbq5.xml"}; - static String queryOptionName = "fieldConstraintOpt.xml"; - private static String dbName = "FieldConstraintDB"; - private static String [] fNames = {"FieldConstraintDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort=8011; - - @BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - addField(dbName, "bbqtext"); - includeElementField(dbName, "bbqtext", "http://example.com", "title"); - includeElementField(dbName, "bbqtext", "http://example.com", "abstract"); - } - - @After - public void testCleanUp() throws Exception - { - clearDB(restPort); - System.out.println("Running clear script"); - } - - @SuppressWarnings("deprecation") - @Test - public void testFieldConstraint() throws IOException - { - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename:filenames) - { - writeDocumentReaderHandle(client, filename, "/field-constraint/", "XML"); - } - - // write the query options to the database - setQueryOption(client, queryOptionName); - - // run the search - SearchHandle resultsHandle = runSearch(client, queryOptionName, "summary:Louisiana AND summary:sweet"); - - // search result - String matchResult = "Matched "+resultsHandle.getTotalResults(); - String expectedMatchResult = "Matched 1"; - assertEquals("Match results difference", expectedMatchResult, matchResult); - - String result = returnSearchResult(resultsHandle); - String expectedResult = "|Matched 3 locations in /field-constraint/bbq3.xml"; - - assertEquals("Results difference", expectedResult, result); - - // release client - client.release(); - } - @AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.IOException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.SearchHandle; +import com.marklogic.javaclient.BasicJavaClientREST; + +import org.junit.*; +public class TestFieldConstraint extends BasicJavaClientREST { + static String filenames[] = {"bbq1.xml", "bbq2.xml", "bbq3.xml", "bbq4.xml", "bbq5.xml"}; + static String queryOptionName = "fieldConstraintOpt.xml"; + private static String dbName = "FieldConstraintDB"; + private static String [] fNames = {"FieldConstraintDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort=8011; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + addField(dbName, "bbqtext"); + includeElementField(dbName, "bbqtext", "http://example.com", "title"); + includeElementField(dbName, "bbqtext", "http://example.com", "abstract"); + } + + @After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + + @Test + public void testFieldConstraint() throws IOException + { + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename:filenames) + { + writeDocumentReaderHandle(client, filename, "/field-constraint/", "XML"); + } + + // write the query options to the database + setQueryOption(client, queryOptionName); + + // run the search + SearchHandle resultsHandle = runSearch(client, queryOptionName, "summary:Louisiana AND summary:sweet"); + + // search result + String matchResult = "Matched "+resultsHandle.getTotalResults(); + String expectedMatchResult = "Matched 1"; + assertEquals("Match results difference", expectedMatchResult, matchResult); + + String result = returnSearchResult(resultsHandle); + String expectedResult = "|Matched 3 locations in /field-constraint/bbq3.xml"; + + assertEquals("Results difference", expectedResult, result); + + // release client + client.release(); + } + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestFileHandle.java b/test-complete/src/test/java/com/marklogic/javaclient/TestFileHandle.java index b776dfcb3..8000f7328 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestFileHandle.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestFileHandle.java @@ -1,318 +1,305 @@ -package com.marklogic.javaclient; - -import java.io.File; -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; - -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.FileHandle; -import com.marklogic.javaclient.BasicJavaClientREST; - -import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; -import static org.junit.Assert.*; - -import org.junit.*; -public class TestFileHandle extends BasicJavaClientREST { - - private static String dbName = "FileHandleDB"; - private static String [] fNames = {"FileHandleDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - @BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - } - - @SuppressWarnings("deprecation") - @Test - public void testXmlCRUD() throws IOException, SAXException, ParserConfigurationException - { - String filename = "xml-original-test.xml"; - String uri = "/write-xml-filehandle/"; - - System.out.println("Running testXmlCRUD"); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - writeDocumentUsingFileHandle(client, filename, uri, "XML"); - - // read docs - FileHandle contentHandle = readDocumentUsingFileHandle(client, uri + filename, "XML"); - - // get the contents - File fileRead = contentHandle.get(); - - String readContent = convertFileToString(fileRead); - - // get xml document for expected result - Document expectedDoc = expectedXMLDocument(filename); - - // convert actual string to xml doc - Document readDoc = convertStringToXMLDocument(readContent); - - assertXMLEqual("Write XML difference", expectedDoc, readDoc); - - // update the doc - // acquire the content for update - String updateFilename = "xml-updated-test.xml"; - updateDocumentUsingFileHandle(client, updateFilename, uri + filename, "XML"); - - // read the document - FileHandle updateHandle = readDocumentUsingFileHandle(client, uri + filename, "XML"); - - // get the contents - File fileReadUpdate = updateHandle.get(); - - String readContentUpdate = convertFileToString(fileReadUpdate); - - // get xml document for expected result - Document expectedDocUpdate = expectedXMLDocument(updateFilename); - - // convert actual string to xml doc - Document readDocUpdate = convertStringToXMLDocument(readContentUpdate); - - assertXMLEqual("Write XML difference", expectedDocUpdate, readDocUpdate); - - // delete the document - deleteDocument(client, uri + filename, "XML"); - - // read the deleted document - String exception = ""; - try - { - FileHandle deleteHandle = readDocumentUsingFileHandle(client, uri + filename, "XML"); - } catch (Exception e) { exception = e.toString(); } -// - String expectedException = "Could not read non-existent document"; - boolean documentIsDeleted = exception.contains(expectedException); - assertTrue("Document is not deleted", documentIsDeleted); - - //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "XML")); - - // release client - client.release(); - } - - @SuppressWarnings("deprecation") - @Test - public void testTextCRUD() throws IOException - { - String filename = "text-original.txt"; - String uri = "/write-text-filehandle/"; - - System.out.println("Running testTextCRUD"); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - writeDocumentUsingFileHandle(client, filename, uri, "Text"); - - // read docs - FileHandle contentHandle = readDocumentUsingFileHandle(client, uri + filename, "Text"); - - // get the contents - File fileRead = contentHandle.get(); - - String readContent = convertFileToString(fileRead); - - String expectedContent = "hello world, welcome to java API"; - - assertEquals("Write Text difference", expectedContent.trim(), readContent.trim()); - - // update the doc - // acquire the content for update - String updateFilename = "text-updated.txt"; - updateDocumentUsingFileHandle(client, updateFilename, uri + filename, "Text"); - - // read the document - FileHandle updateHandle = readDocumentUsingFileHandle(client, uri + filename, "Text"); - - // get the contents - File fileReadUpdate = updateHandle.get(); - - String readContentUpdate = convertFileToString(fileReadUpdate); - - String expectedContentUpdate = "hello world, welcome to java API after new updates"; - - assertEquals("Write Text difference", expectedContentUpdate.trim(), readContentUpdate.toString().trim()); - - // delete the document - deleteDocument(client, uri + filename, "Text"); - - // read the deleted document - //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "Text")); - - String exception = ""; - try - { - FileHandle deleteHandle = readDocumentUsingFileHandle(client, uri + filename, "Text"); - } catch (Exception e) { exception = e.toString(); } -// - String expectedException = "Could not read non-existent document"; - boolean documentIsDeleted = exception.contains(expectedException); - assertTrue("Document is not deleted", documentIsDeleted); - - // release client - client.release(); - } - - @SuppressWarnings("deprecation") - @Test - public void testJsonCRUD() throws IOException - { - String filename = "json-original.json"; - String uri = "/write-json-filehandle/"; - - System.out.println("Running testJsonCRUD"); - - ObjectMapper mapper = new ObjectMapper(); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - writeDocumentUsingFileHandle(client, filename, uri, "JSON"); - - // read docs - FileHandle contentHandle = readDocumentUsingFileHandle(client, uri + filename, "JSON"); - - // get the contents - File fileRead = contentHandle.get(); - - JsonNode readContent = mapper.readTree(fileRead); - - // get expected contents - JsonNode expectedContent = expectedJSONDocument(filename); - - assertTrue("Write JSON document difference", readContent.equals(expectedContent)); - - // update the doc - // acquire the content for update - String updateFilename = "json-updated.json"; - updateDocumentUsingFileHandle(client, updateFilename, uri + filename, "JSON"); - - // read the document - FileHandle updateHandle = readDocumentUsingFileHandle(client, uri + filename, "JSON"); - - // get the contents - File fileReadUpdate = updateHandle.get(); - - JsonNode readContentUpdate = mapper.readTree(fileReadUpdate); - - // get expected contents - JsonNode expectedContentUpdate = expectedJSONDocument(updateFilename); - - assertTrue("Write JSON document difference", readContentUpdate.equals(expectedContentUpdate)); - - // delete the document - deleteDocument(client, uri + filename, "JSON"); - - // read the deleted document - //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "JSON")); - - String exception = ""; - try - { - FileHandle deleteHandle = readDocumentUsingFileHandle(client, uri + filename, "JSON"); - } catch (Exception e) { exception = e.toString(); } - - - String expectedException = "Could not read non-existent document"; - boolean documentIsDeleted = exception.contains(expectedException); - assertTrue("Document is not deleted", documentIsDeleted); - - // release client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testBinaryCRUD() throws IOException - { - String filename = "Pandakarlino.jpg"; - String uri = "/write-bin-filehandle/"; - - System.out.println("Running testBinaryCRUD"); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - writeDocumentUsingFileHandle(client, filename, uri, "Binary"); - - // read docs - FileHandle contentHandle = readDocumentUsingFileHandle(client, uri + filename, "Binary"); - - // get the contents - File fileRead = contentHandle.get(); - - // get the binary size - long size = fileRead.length(); - long expectedSize = 17154; - - assertEquals("Binary size difference", expectedSize, size); - - // update the doc - // acquire the content for update - String updateFilename = "mlfavicon.png"; - updateDocumentUsingFileHandle(client, updateFilename, uri + filename, "Binary"); - - // read the document - FileHandle updateHandle = readDocumentUsingFileHandle(client, uri + filename, "Binary"); - - // get the contents - File fileReadUpdate = updateHandle.get(); - - // get the binary size - long sizeUpdate = fileReadUpdate.length(); - long expectedSizeUpdate = 3322; - - assertEquals("Binary size difference", expectedSizeUpdate, sizeUpdate); - - // delete the document - deleteDocument(client, uri + filename, "Binary"); - - // read the deleted document - //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "Binary")); - - String exception = ""; - try - { - FileHandle deleteHandle = readDocumentUsingFileHandle(client, uri + filename, "Binary"); - } catch (Exception e) { exception = e.toString(); } -// -//<<<<<<< .mine -// String expectedException = "com.marklogic.client.ResourceNotFoundException: Local message: Could not read non-existent documentServer Message: RESTAPI-NODOCUMENT: (err:FOER0000) Resource or document does not exist: category: content message: /write-bin-filehandle/Pandakarlino.jpg"; -// assertEquals("Document is not deleted", expectedException, exception); -//======= -// //String expectedException = "com.marklogic.client.ResourceNotFoundException: Could not read non-existent document"; -// //assertEquals("Document is not deleted", expectedException, exception); -//>>>>>>> .r106786 - - String expectedException = "Could not read non-existent document"; - boolean documentIsDeleted = exception.contains(expectedException); - assertTrue("Document is not deleted", documentIsDeleted); - - // release client - client.release(); - } - @AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} +package com.marklogic.javaclient; + +import java.io.File; +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.FileHandle; +import com.marklogic.javaclient.BasicJavaClientREST; + +import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; +import static org.junit.Assert.*; + +import org.junit.*; +public class TestFileHandle extends BasicJavaClientREST { + + private static String dbName = "FileHandleDB"; + private static String [] fNames = {"FileHandleDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + } + + @Test + public void testXmlCRUD() throws IOException, SAXException, ParserConfigurationException + { + String filename = "xml-original-test.xml"; + String uri = "/write-xml-filehandle/"; + + System.out.println("Running testXmlCRUD"); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + writeDocumentUsingFileHandle(client, filename, uri, "XML"); + + // read docs + FileHandle contentHandle = readDocumentUsingFileHandle(client, uri + filename, "XML"); + + // get the contents + File fileRead = contentHandle.get(); + + String readContent = convertFileToString(fileRead); + + // get xml document for expected result + Document expectedDoc = expectedXMLDocument(filename); + + // convert actual string to xml doc + Document readDoc = convertStringToXMLDocument(readContent); + + assertXMLEqual("Write XML difference", expectedDoc, readDoc); + + // update the doc + // acquire the content for update + String updateFilename = "xml-updated-test.xml"; + updateDocumentUsingFileHandle(client, updateFilename, uri + filename, "XML"); + + // read the document + FileHandle updateHandle = readDocumentUsingFileHandle(client, uri + filename, "XML"); + + // get the contents + File fileReadUpdate = updateHandle.get(); + + String readContentUpdate = convertFileToString(fileReadUpdate); + + // get xml document for expected result + Document expectedDocUpdate = expectedXMLDocument(updateFilename); + + // convert actual string to xml doc + Document readDocUpdate = convertStringToXMLDocument(readContentUpdate); + + assertXMLEqual("Write XML difference", expectedDocUpdate, readDocUpdate); + + // delete the document + deleteDocument(client, uri + filename, "XML"); + + // read the deleted document + String exception = ""; + try + { + readDocumentUsingFileHandle(client, uri + filename, "XML"); + } catch (Exception e) { exception = e.toString(); } +// + String expectedException = "Could not read non-existent document"; + boolean documentIsDeleted = exception.contains(expectedException); + assertTrue("Document is not deleted", documentIsDeleted); + + //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "XML")); + + // release client + client.release(); + } + + @Test + public void testTextCRUD() throws IOException + { + String filename = "text-original.txt"; + String uri = "/write-text-filehandle/"; + + System.out.println("Running testTextCRUD"); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + writeDocumentUsingFileHandle(client, filename, uri, "Text"); + + // read docs + FileHandle contentHandle = readDocumentUsingFileHandle(client, uri + filename, "Text"); + + // get the contents + File fileRead = contentHandle.get(); + + String readContent = convertFileToString(fileRead); + + String expectedContent = "hello world, welcome to java API"; + + assertEquals("Write Text difference", expectedContent.trim(), readContent.trim()); + + // update the doc + // acquire the content for update + String updateFilename = "text-updated.txt"; + updateDocumentUsingFileHandle(client, updateFilename, uri + filename, "Text"); + + // read the document + FileHandle updateHandle = readDocumentUsingFileHandle(client, uri + filename, "Text"); + + // get the contents + File fileReadUpdate = updateHandle.get(); + + String readContentUpdate = convertFileToString(fileReadUpdate); + + String expectedContentUpdate = "hello world, welcome to java API after new updates"; + + assertEquals("Write Text difference", expectedContentUpdate.trim(), readContentUpdate.toString().trim()); + + // delete the document + deleteDocument(client, uri + filename, "Text"); + + // read the deleted document + //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "Text")); + + String exception = ""; + try + { + readDocumentUsingFileHandle(client, uri + filename, "Text"); + } catch (Exception e) { exception = e.toString(); } +// + String expectedException = "Could not read non-existent document"; + boolean documentIsDeleted = exception.contains(expectedException); + assertTrue("Document is not deleted", documentIsDeleted); + + // release client + client.release(); + } + + @Test + public void testJsonCRUD() throws IOException + { + String filename = "json-original.json"; + String uri = "/write-json-filehandle/"; + + System.out.println("Running testJsonCRUD"); + + ObjectMapper mapper = new ObjectMapper(); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + writeDocumentUsingFileHandle(client, filename, uri, "JSON"); + + // read docs + FileHandle contentHandle = readDocumentUsingFileHandle(client, uri + filename, "JSON"); + + // get the contents + File fileRead = contentHandle.get(); + + JsonNode readContent = mapper.readTree(fileRead); + + // get expected contents + JsonNode expectedContent = expectedJSONDocument(filename); + + assertTrue("Write JSON document difference", readContent.equals(expectedContent)); + + // update the doc + // acquire the content for update + String updateFilename = "json-updated.json"; + updateDocumentUsingFileHandle(client, updateFilename, uri + filename, "JSON"); + + // read the document + FileHandle updateHandle = readDocumentUsingFileHandle(client, uri + filename, "JSON"); + + // get the contents + File fileReadUpdate = updateHandle.get(); + + JsonNode readContentUpdate = mapper.readTree(fileReadUpdate); + + // get expected contents + JsonNode expectedContentUpdate = expectedJSONDocument(updateFilename); + + assertTrue("Write JSON document difference", readContentUpdate.equals(expectedContentUpdate)); + + // delete the document + deleteDocument(client, uri + filename, "JSON"); + + // read the deleted document + //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "JSON")); + + String exception = ""; + try + { + readDocumentUsingFileHandle(client, uri + filename, "JSON"); + } catch (Exception e) { exception = e.toString(); } + + + String expectedException = "Could not read non-existent document"; + boolean documentIsDeleted = exception.contains(expectedException); + assertTrue("Document is not deleted", documentIsDeleted); + + // release client + client.release(); + } + + @Test public void testBinaryCRUD() throws IOException + { + String filename = "Pandakarlino.jpg"; + String uri = "/write-bin-filehandle/"; + + System.out.println("Running testBinaryCRUD"); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + writeDocumentUsingFileHandle(client, filename, uri, "Binary"); + + // read docs + FileHandle contentHandle = readDocumentUsingFileHandle(client, uri + filename, "Binary"); + + // get the contents + File fileRead = contentHandle.get(); + + // get the binary size + long size = fileRead.length(); + long expectedSize = 17154; + + assertEquals("Binary size difference", expectedSize, size); + + // update the doc + // acquire the content for update + String updateFilename = "mlfavicon.png"; + updateDocumentUsingFileHandle(client, updateFilename, uri + filename, "Binary"); + + // read the document + FileHandle updateHandle = readDocumentUsingFileHandle(client, uri + filename, "Binary"); + + // get the contents + File fileReadUpdate = updateHandle.get(); + + // get the binary size + long sizeUpdate = fileReadUpdate.length(); + long expectedSizeUpdate = 3322; + + assertEquals("Binary size difference", expectedSizeUpdate, sizeUpdate); + + // delete the document + deleteDocument(client, uri + filename, "Binary"); + + // read the deleted document + //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "Binary")); + + String exception = ""; + try + { + readDocumentUsingFileHandle(client, uri + filename, "Binary"); + } catch (Exception e) { exception = e.toString(); } + + String expectedException = "Could not read non-existent document"; + boolean documentIsDeleted = exception.contains(expectedException); + assertTrue("Document is not deleted", documentIsDeleted); + + // release client + client.release(); + } + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + + } +} From 3913b41dd0a23f520ab74559c6ea6fe5dfde0f87 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Wed, 20 Aug 2014 18:14:43 -0700 Subject: [PATCH 077/357] Code cleanup --- .../javaclient/TestInputSourceHandle.java | 237 ++++--- .../javaclient/TestInputStreamHandle.java | 605 +++++++++--------- 2 files changed, 410 insertions(+), 432 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestInputSourceHandle.java b/test-complete/src/test/java/com/marklogic/javaclient/TestInputSourceHandle.java index 4bf2f6a86..6a454d296 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestInputSourceHandle.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestInputSourceHandle.java @@ -1,129 +1,110 @@ -package com.marklogic.javaclient; - -import java.io.IOException; -import java.io.InputStream; -import java.io.Reader; -import java.io.StringWriter; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.OutputKeys; -import javax.xml.transform.Source; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerConfigurationException; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.sax.SAXSource; -import javax.xml.transform.stream.StreamResult; - -import org.w3c.dom.Document; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.io.InputSourceHandle; -import com.marklogic.client.io.InputStreamHandle; -import com.marklogic.client.io.StringHandle; -import com.marklogic.javaclient.BasicJavaClientREST; - -import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; -import static org.junit.Assert.*; - -import org.junit.*; -public class TestInputSourceHandle extends BasicJavaClientREST { - - - private static String dbName = "InputSourceHandleDB"; - private static String [] fNames = {"InputSourceHandleDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - @BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - } - - - @SuppressWarnings("deprecation") - @Test public void testXmlCRUD() throws IOException, SAXException, ParserConfigurationException, TransformerException - { - String filename = "xml-original-test.xml"; - String uri = "/write-xml-inputsourcehandle/"; - - System.out.println("Running testXmlCRUD"); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - writeDocumentUsingInputStreamHandle(client, filename, uri, "XML"); - - // read docs - InputSourceHandle contentHandle = readDocumentUsingInputSourceHandle(client, uri + filename, "XML"); - - // get the contents - InputSource fileRead = contentHandle.get(); - - String readContent = convertInputSourceToString(fileRead); - System.out.println(readContent); - - // get xml document for expected result - Document expectedDoc = expectedXMLDocument(filename); - - // convert actual string to xml doc - Document readDoc = convertStringToXMLDocument(readContent); - - assertXMLEqual("Write XML difference", expectedDoc, readDoc); - - // update the doc - // acquire the content for update - String updateFilename = "xml-updated-test.xml"; - updateDocumentUsingInputStreamHandle(client, updateFilename, uri + filename, "XML"); - - // read the document - InputSourceHandle updateHandle = readDocumentUsingInputSourceHandle(client, uri + filename, "XML"); - - // get the contents - InputSource fileReadUpdate = updateHandle.get(); - - String readContentUpdate = convertInputSourceToString(fileReadUpdate); - - // get xml document for expected result - Document expectedDocUpdate = expectedXMLDocument(updateFilename); - - // convert actual string to xml doc - Document readDocUpdate = convertStringToXMLDocument(readContentUpdate); - - assertXMLEqual("Write XML difference", expectedDocUpdate, readDocUpdate); - - // delete the document - deleteDocument(client, uri + filename, "XML"); - - // read the deleted document - String exception = ""; - try - { - InputStreamHandle deleteHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "XML"); - } catch (Exception e) { exception = e.toString(); } - - String expectedException = "Could not read non-existent document"; - boolean documentIsDeleted = exception.contains(expectedException); - assertTrue("Document is not deleted", documentIsDeleted); - - // release client - client.release(); - } - @AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } +package com.marklogic.javaclient; + +import java.io.IOException; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; +import org.w3c.dom.Document; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.InputSourceHandle; +import com.marklogic.client.io.InputStreamHandle; +import com.marklogic.javaclient.BasicJavaClientREST; + +import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; +import static org.junit.Assert.*; + +import org.junit.*; +public class TestInputSourceHandle extends BasicJavaClientREST { + + + private static String dbName = "InputSourceHandleDB"; + private static String [] fNames = {"InputSourceHandleDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + } + + @Test public void testXmlCRUD() throws IOException, SAXException, ParserConfigurationException, TransformerException + { + String filename = "xml-original-test.xml"; + String uri = "/write-xml-inputsourcehandle/"; + + System.out.println("Running testXmlCRUD"); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + writeDocumentUsingInputStreamHandle(client, filename, uri, "XML"); + + // read docs + InputSourceHandle contentHandle = readDocumentUsingInputSourceHandle(client, uri + filename, "XML"); + + // get the contents + InputSource fileRead = contentHandle.get(); + + String readContent = convertInputSourceToString(fileRead); + System.out.println(readContent); + + // get xml document for expected result + Document expectedDoc = expectedXMLDocument(filename); + + // convert actual string to xml doc + Document readDoc = convertStringToXMLDocument(readContent); + + assertXMLEqual("Write XML difference", expectedDoc, readDoc); + + // update the doc + // acquire the content for update + String updateFilename = "xml-updated-test.xml"; + updateDocumentUsingInputStreamHandle(client, updateFilename, uri + filename, "XML"); + + // read the document + InputSourceHandle updateHandle = readDocumentUsingInputSourceHandle(client, uri + filename, "XML"); + + // get the contents + InputSource fileReadUpdate = updateHandle.get(); + + String readContentUpdate = convertInputSourceToString(fileReadUpdate); + + // get xml document for expected result + Document expectedDocUpdate = expectedXMLDocument(updateFilename); + + // convert actual string to xml doc + Document readDocUpdate = convertStringToXMLDocument(readContentUpdate); + + assertXMLEqual("Write XML difference", expectedDocUpdate, readDocUpdate); + + // delete the document + deleteDocument(client, uri + filename, "XML"); + + // read the deleted document + String exception = ""; + try + { + readDocumentUsingInputStreamHandle(client, uri + filename, "XML"); + } catch (Exception e) { exception = e.toString(); } + + String expectedException = "Could not read non-existent document"; + boolean documentIsDeleted = exception.contains(expectedException); + assertTrue("Document is not deleted", documentIsDeleted); + + // release client + client.release(); + } + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + + } } \ No newline at end of file diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestInputStreamHandle.java b/test-complete/src/test/java/com/marklogic/javaclient/TestInputStreamHandle.java index a1b741d15..082ec5c41 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestInputStreamHandle.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestInputStreamHandle.java @@ -1,304 +1,301 @@ -package com.marklogic.javaclient; - -import java.io.IOException; -import java.io.InputStream; - -import javax.xml.parsers.ParserConfigurationException; - -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.InputStreamHandle; -import com.marklogic.javaclient.BasicJavaClientREST; - -import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; -import static org.junit.Assert.*; - -import org.junit.*; -public class TestInputStreamHandle extends BasicJavaClientREST { - - private static String dbName = "InputStreamHandleDB"; - private static String [] fNames = {"InputStreamHandleDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - @BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - } - @SuppressWarnings("deprecation") - @Test - public void testXmlCRUD() throws IOException, SAXException, ParserConfigurationException - { - String filename = "xml-original-test.xml"; - String uri = "/write-xml-inputstreamhandle/"; - - System.out.println("Running testXmlCRUD"); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - writeDocumentUsingInputStreamHandle(client, filename, uri, "XML"); - - // read docs - InputStreamHandle contentHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "XML"); - - // get the contents - InputStream fileRead = contentHandle.get(); - - String readContent = convertInputStreamToString(fileRead); - - // get xml document for expected result - Document expectedDoc = expectedXMLDocument(filename); - - // convert actual string to xml doc - Document readDoc = convertStringToXMLDocument(readContent); - - assertXMLEqual("Write XML difference", expectedDoc, readDoc); - - // update the doc - // acquire the content for update - String updateFilename = "xml-updated-test.xml"; - updateDocumentUsingInputStreamHandle(client, updateFilename, uri + filename, "XML"); - - // read the document - InputStreamHandle updateHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "XML"); - - // get the contents - InputStream fileReadUpdate = updateHandle.get(); - - String readContentUpdate = convertInputStreamToString(fileReadUpdate); - - // get xml document for expected result - Document expectedDocUpdate = expectedXMLDocument(updateFilename); - - // convert actual string to xml doc - Document readDocUpdate = convertStringToXMLDocument(readContentUpdate); - - assertXMLEqual("Write XML difference", expectedDocUpdate, readDocUpdate); - - // delete the document - deleteDocument(client, uri + filename, "XML"); - - // read the deleted document - String exception = ""; - try - { - InputStreamHandle deleteHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "XML"); - } catch (Exception e) { exception = e.toString(); } - - String expectedException = "Could not read non-existent document"; - boolean documentIsDeleted = exception.contains(expectedException); - assertTrue("Document is not deleted", documentIsDeleted); - - // release client - client.release(); - } - - @SuppressWarnings("deprecation") - @Test - public void testTextCRUD() throws IOException - { - String filename = "text-original.txt"; - String uri = "/write-text-inputstreamhandle/"; - - System.out.println("Running testTextCRUD"); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - writeDocumentUsingInputStreamHandle(client, filename, uri, "Text"); - - // read docs - InputStreamHandle contentHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "Text"); - - // get the contents - InputStream fileRead = contentHandle.get(); - - String readContent = convertInputStreamToString(fileRead); - - String expectedContent = "hello world, welcome to java API"; - - assertEquals("Write Text difference", expectedContent.trim(), readContent.trim()); - - // update the doc - // acquire the content for update - String updateFilename = "text-updated.txt"; - updateDocumentUsingInputStreamHandle(client, updateFilename, uri + filename, "Text"); - - // read the document - InputStreamHandle updateHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "Text"); - - // get the contents - InputStream fileReadUpdate = updateHandle.get(); - - String readContentUpdate = convertInputStreamToString(fileReadUpdate); - - String expectedContentUpdate = "hello world, welcome to java API after new updates"; - - assertEquals("Write Text difference", expectedContentUpdate.trim(), readContentUpdate.toString().trim()); - - // delete the document - deleteDocument(client, uri + filename, "Text"); - - // read the deleted document - //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "Text")); - - String exception = ""; - try - { - InputStreamHandle deleteHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "Text"); - } catch (Exception e) { exception = e.toString(); } - - String expectedException = "Could not read non-existent document"; - boolean documentIsDeleted = exception.contains(expectedException); - assertTrue("Document is not deleted", documentIsDeleted); - - // release client - client.release(); - } - - @SuppressWarnings("deprecation") - @Test - public void testJsonCRUD() throws IOException - { - String filename = "json-original.json"; - String uri = "/write-json-inputstreamhandle/"; - - System.out.println("Running testJsonCRUD"); - - ObjectMapper mapper = new ObjectMapper(); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - writeDocumentUsingInputStreamHandle(client, filename, uri, "JSON"); - - // read docs - InputStreamHandle contentHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "JSON"); - - // get the contents - InputStream fileRead = contentHandle.get(); - JsonNode readContent = mapper.readTree(fileRead); - - // get expected contents - JsonNode expectedContent = expectedJSONDocument(filename); - - assertTrue("Write JSON document difference", readContent.equals(expectedContent)); - - // update the doc - // acquire the content for update - String updateFilename = "json-updated.json"; - updateDocumentUsingInputStreamHandle(client, updateFilename, uri + filename, "JSON"); - - // read the document - InputStreamHandle updateHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "JSON"); - - // get the contents - InputStream fileReadUpdate = updateHandle.get(); - - JsonNode readContentUpdate = mapper.readTree(fileReadUpdate); - - // get expected contents - JsonNode expectedContentUpdate = expectedJSONDocument(updateFilename); - - assertTrue("Write JSON document difference", readContentUpdate.equals(expectedContentUpdate)); - - // delete the document - deleteDocument(client, uri + filename, "JSON"); - - // read the deleted document - //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "JSON")); - - String exception = ""; - try - { - InputStreamHandle deleteHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "JSON"); - } catch (Exception e) { exception = e.toString(); } - - String expectedException = "Could not read non-existent document"; - boolean documentIsDeleted = exception.contains(expectedException); - assertTrue("Document is not deleted", documentIsDeleted); - - // release client - client.release(); - } - - @SuppressWarnings("deprecation") - @Test - public void testBinaryCRUD() throws IOException - { - String filename = "Pandakarlino.jpg"; - String uri = "/write-bin-inputstreamhandle/"; - - System.out.println("Running testBinaryCRUD"); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - writeDocumentUsingInputStreamHandle(client, filename, uri, "Binary"); - - // read docs - InputStreamHandle contentHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "Binary"); - - // get the contents - InputStream fileRead = contentHandle.get(); - - // get the binary size - int size = getBinarySize(fileRead); - int expectedSize = 17154; - - assertEquals("Binary size difference", expectedSize, size); - - // update the doc - // acquire the content for update - String updateFilename = "mlfavicon.png"; - updateDocumentUsingInputStreamHandle(client, updateFilename, uri + filename, "Binary"); - - // read the document - InputStreamHandle updateHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "Binary"); - - // get the contents - InputStream fileReadUpdate = updateHandle.get(); - - // get the binary size - int sizeUpdate = getBinarySize(fileReadUpdate); - int expectedSizeUpdate = 3322; - - assertEquals("Binary size difference", expectedSizeUpdate, sizeUpdate); - - // delete the document - deleteDocument(client, uri + filename, "Binary"); - - // read the deleted document - //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "Binary")); - - String exception = ""; - try - { - InputStreamHandle deleteHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "Binary"); - } catch (Exception e) { exception = e.toString(); } - - String expectedException = "Could not read non-existent document"; - boolean documentIsDeleted = exception.contains(expectedException); - assertTrue("Document is not deleted", documentIsDeleted); - - // release client - client.release(); - } - @AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } -} +package com.marklogic.javaclient; + +import java.io.IOException; +import java.io.InputStream; + +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.InputStreamHandle; +import com.marklogic.javaclient.BasicJavaClientREST; + +import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; +import static org.junit.Assert.*; + +import org.junit.*; +public class TestInputStreamHandle extends BasicJavaClientREST { + + private static String dbName = "InputStreamHandleDB"; + private static String [] fNames = {"InputStreamHandleDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + } + + @Test + public void testXmlCRUD() throws IOException, SAXException, ParserConfigurationException + { + String filename = "xml-original-test.xml"; + String uri = "/write-xml-inputstreamhandle/"; + + System.out.println("Running testXmlCRUD"); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + writeDocumentUsingInputStreamHandle(client, filename, uri, "XML"); + + // read docs + InputStreamHandle contentHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "XML"); + + // get the contents + InputStream fileRead = contentHandle.get(); + + String readContent = convertInputStreamToString(fileRead); + + // get xml document for expected result + Document expectedDoc = expectedXMLDocument(filename); + + // convert actual string to xml doc + Document readDoc = convertStringToXMLDocument(readContent); + + assertXMLEqual("Write XML difference", expectedDoc, readDoc); + + // update the doc + // acquire the content for update + String updateFilename = "xml-updated-test.xml"; + updateDocumentUsingInputStreamHandle(client, updateFilename, uri + filename, "XML"); + + // read the document + InputStreamHandle updateHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "XML"); + + // get the contents + InputStream fileReadUpdate = updateHandle.get(); + + String readContentUpdate = convertInputStreamToString(fileReadUpdate); + + // get xml document for expected result + Document expectedDocUpdate = expectedXMLDocument(updateFilename); + + // convert actual string to xml doc + Document readDocUpdate = convertStringToXMLDocument(readContentUpdate); + + assertXMLEqual("Write XML difference", expectedDocUpdate, readDocUpdate); + + // delete the document + deleteDocument(client, uri + filename, "XML"); + + // read the deleted document + String exception = ""; + try + { + readDocumentUsingInputStreamHandle(client, uri + filename, "XML"); + } catch (Exception e) { exception = e.toString(); } + + String expectedException = "Could not read non-existent document"; + boolean documentIsDeleted = exception.contains(expectedException); + assertTrue("Document is not deleted", documentIsDeleted); + + // release client + client.release(); + } + + @Test + public void testTextCRUD() throws IOException + { + String filename = "text-original.txt"; + String uri = "/write-text-inputstreamhandle/"; + + System.out.println("Running testTextCRUD"); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + writeDocumentUsingInputStreamHandle(client, filename, uri, "Text"); + + // read docs + InputStreamHandle contentHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "Text"); + + // get the contents + InputStream fileRead = contentHandle.get(); + + String readContent = convertInputStreamToString(fileRead); + + String expectedContent = "hello world, welcome to java API"; + + assertEquals("Write Text difference", expectedContent.trim(), readContent.trim()); + + // update the doc + // acquire the content for update + String updateFilename = "text-updated.txt"; + updateDocumentUsingInputStreamHandle(client, updateFilename, uri + filename, "Text"); + + // read the document + InputStreamHandle updateHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "Text"); + + // get the contents + InputStream fileReadUpdate = updateHandle.get(); + + String readContentUpdate = convertInputStreamToString(fileReadUpdate); + + String expectedContentUpdate = "hello world, welcome to java API after new updates"; + + assertEquals("Write Text difference", expectedContentUpdate.trim(), readContentUpdate.toString().trim()); + + // delete the document + deleteDocument(client, uri + filename, "Text"); + + // read the deleted document + //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "Text")); + + String exception = ""; + try + { + readDocumentUsingInputStreamHandle(client, uri + filename, "Text"); + } catch (Exception e) { exception = e.toString(); } + + String expectedException = "Could not read non-existent document"; + boolean documentIsDeleted = exception.contains(expectedException); + assertTrue("Document is not deleted", documentIsDeleted); + + // release client + client.release(); + } + + @Test + public void testJsonCRUD() throws IOException + { + String filename = "json-original.json"; + String uri = "/write-json-inputstreamhandle/"; + + System.out.println("Running testJsonCRUD"); + + ObjectMapper mapper = new ObjectMapper(); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + writeDocumentUsingInputStreamHandle(client, filename, uri, "JSON"); + + // read docs + InputStreamHandle contentHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "JSON"); + + // get the contents + InputStream fileRead = contentHandle.get(); + JsonNode readContent = mapper.readTree(fileRead); + + // get expected contents + JsonNode expectedContent = expectedJSONDocument(filename); + + assertTrue("Write JSON document difference", readContent.equals(expectedContent)); + + // update the doc + // acquire the content for update + String updateFilename = "json-updated.json"; + updateDocumentUsingInputStreamHandle(client, updateFilename, uri + filename, "JSON"); + + // read the document + InputStreamHandle updateHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "JSON"); + + // get the contents + InputStream fileReadUpdate = updateHandle.get(); + + JsonNode readContentUpdate = mapper.readTree(fileReadUpdate); + + // get expected contents + JsonNode expectedContentUpdate = expectedJSONDocument(updateFilename); + + assertTrue("Write JSON document difference", readContentUpdate.equals(expectedContentUpdate)); + + // delete the document + deleteDocument(client, uri + filename, "JSON"); + + // read the deleted document + //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "JSON")); + + String exception = ""; + try + { + readDocumentUsingInputStreamHandle(client, uri + filename, "JSON"); + } catch (Exception e) { exception = e.toString(); } + + String expectedException = "Could not read non-existent document"; + boolean documentIsDeleted = exception.contains(expectedException); + assertTrue("Document is not deleted", documentIsDeleted); + + // release client + client.release(); + } + + @Test + public void testBinaryCRUD() throws IOException + { + String filename = "Pandakarlino.jpg"; + String uri = "/write-bin-inputstreamhandle/"; + + System.out.println("Running testBinaryCRUD"); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + writeDocumentUsingInputStreamHandle(client, filename, uri, "Binary"); + + // read docs + InputStreamHandle contentHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "Binary"); + + // get the contents + InputStream fileRead = contentHandle.get(); + + // get the binary size + int size = getBinarySize(fileRead); + int expectedSize = 17154; + + assertEquals("Binary size difference", expectedSize, size); + + // update the doc + // acquire the content for update + String updateFilename = "mlfavicon.png"; + updateDocumentUsingInputStreamHandle(client, updateFilename, uri + filename, "Binary"); + + // read the document + InputStreamHandle updateHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "Binary"); + + // get the contents + InputStream fileReadUpdate = updateHandle.get(); + + // get the binary size + int sizeUpdate = getBinarySize(fileReadUpdate); + int expectedSizeUpdate = 3322; + + assertEquals("Binary size difference", expectedSizeUpdate, sizeUpdate); + + // delete the document + deleteDocument(client, uri + filename, "Binary"); + + // read the deleted document + //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "Binary")); + + String exception = ""; + try + { + readDocumentUsingInputStreamHandle(client, uri + filename, "Binary"); + } catch (Exception e) { exception = e.toString(); } + + String expectedException = "Could not read non-existent document"; + boolean documentIsDeleted = exception.contains(expectedException); + assertTrue("Document is not deleted", documentIsDeleted); + + // release client + client.release(); + } + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} From b3398dea5eab5c3579a7d76c360ea9722f2d1d98 Mon Sep 17 00:00:00 2001 From: sravan Date: Wed, 20 Aug 2014 22:59:53 -0700 Subject: [PATCH 078/357] merging with dev branch --- .../TestBulkSearchWithStringQueryDef.java | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java index 529d6c927..acc841e96 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java @@ -54,16 +54,16 @@ public class TestBulkSearchWithStringQueryDef extends BasicJavaClientREST{ @BeforeClass public static void setUpBeforeClass() throws Exception { System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); - setupAppServicesConstraint(dbName); - createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + setupAppServicesConstraint(dbName); + createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); } @AfterClass public static void tearDownAfterClass() throws Exception { System.out.println("In tear down" ); - tearDownJavaRESTServer(dbName, fNames, restServerName); - deleteRESTUser("usr1"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + deleteRESTUser("usr1"); } @Before @@ -220,7 +220,7 @@ public void testBulkSearchSQDwithWrongResponseFormat() throws Exception { qd.setCriteria("bar"); docMgr.setResponseFormat(Format.JSON); SearchHandle results = new SearchHandle(); - docMgr.search(qd, 1,results); + DocumentPage page= docMgr.search(qd, 1,results); MatchDocumentSummary[] summaries = results.getMatchResults(); for (MatchDocumentSummary summary : summaries ) { MatchLocation[] locations = summary.getMatchLocations(); @@ -234,6 +234,7 @@ public void testBulkSearchSQDwithWrongResponseFormat() throws Exception { //This test has set response to JSON and pass StringHandle with format as JSON, expectint it to work, logged an issue 82 @Test public void testBulkSearchSQDwithResponseFormatandStringHandle() throws Exception{ + int count =1; loadTxtDocuments(); loadJSONDocuments(); TextDocumentManager docMgr = client.newTextDocumentManager(); @@ -256,6 +257,7 @@ public void testBulkSearchSQDwithResponseFormatandStringHandle() throws Exceptio docMgr.readMetadata(rec.getUri(),mh); assertTrue("Records has permissions? ",mh.getPermissions().containsKey("flexrep-eval")); assertTrue("Record has collections ?",mh.getCollections().isEmpty()); + count++; } assertFalse("Search handle contains",results.get().isEmpty()); @@ -276,7 +278,7 @@ public void testBulkSearchSQDwithJSONResponseFormat() throws Exception{ docMgr.setSearchView(QueryView.FACETS); JacksonHandle jh = new JacksonHandle(); - docMgr.search(qd, 1,jh); + DocumentPage page= docMgr.search(qd, 1,jh); // System.out.println(jh.get().toString()); assertTrue("Searh response has entry for facets",jh.get().has("facets")); @@ -284,21 +286,21 @@ public void testBulkSearchSQDwithJSONResponseFormat() throws Exception{ assertTrue("Searh response has entry for facets",jh.get().has("metrics")); docMgr.setSearchView(QueryView.RESULTS); - docMgr.search(qd, 1,jh); + page= docMgr.search(qd, 1,jh); assertFalse("Searh response has entry for facets",jh.get().has("facets")); assertTrue("Searh response has entry for facets",jh.get().has("results")); assertTrue("Searh response has entry for facets",jh.get().has("metrics"));//Issue 84 is tracking this docMgr.setSearchView(QueryView.METADATA); - docMgr.search(qd, 1,jh); + page= docMgr.search(qd, 1,jh); assertFalse("Searh response has entry for facets",jh.get().has("facets")); assertTrue("Searh response has entry for facets",jh.get().has("results")); assertTrue("Searh response has entry for facets",jh.get().has("metrics")); docMgr.setSearchView(QueryView.ALL); - docMgr.search(qd, 1,jh); + page= docMgr.search(qd, 1,jh); assertTrue("Searh response has entry for facets",jh.get().has("facets")); assertTrue("Searh response has entry for facets",jh.get().has("results")); From 7ad729739cd9a7ca9659ef0730f49debd165e0a6 Mon Sep 17 00:00:00 2001 From: sravan Date: Wed, 20 Aug 2014 23:37:12 -0700 Subject: [PATCH 079/357] commenting the automatic dir creation --- .../marklogic/javaclient/TestBulkSearchWithStrucQueryDef.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStrucQueryDef.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStrucQueryDef.java index a98602cd8..354a53923 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStrucQueryDef.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStrucQueryDef.java @@ -324,7 +324,7 @@ public void testBulkSearchSQDwithTransactionsandDOMHandle() throws Exception{ //This test is to verify RAW XML structured Query @Test public void testBulkSearchRawXMLStrucQD() throws Exception{ - setAutomaticDirectoryCreation(dbName,"automatic"); +// setAutomaticDirectoryCreation(dbName,"automatic"); setMaintainLastModified(dbName,true); this.loadJSONDocuments(); this.loadXMLDocuments(); @@ -375,7 +375,7 @@ public void testBulkSearchRawXMLStrucQD() throws Exception{ //This test is to verify RAW JSON structured query @Test public void testBulkSearchRawJSONStrucQD() throws Exception{ - setAutomaticDirectoryCreation(dbName,"automatic"); +// setAutomaticDirectoryCreation(dbName,"automatic"); setMaintainLastModified(dbName,true); this.loadJSONDocuments(); this.loadXMLDocuments(); From 7999613ec95435a676737bab67856835913486e9 Mon Sep 17 00:00:00 2001 From: sravan Date: Wed, 20 Aug 2014 23:44:20 -0700 Subject: [PATCH 080/357] updating changes made in new regressionTests branch --- .../TestBulkSearchWithStrucQueryDef.java | 23 +++++++------------ 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStrucQueryDef.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStrucQueryDef.java index f5c164221..78a936d07 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStrucQueryDef.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStrucQueryDef.java @@ -18,7 +18,6 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import com.marklogic.client.DatabaseClient; import com.marklogic.client.DatabaseClientFactory; @@ -40,10 +39,8 @@ import com.marklogic.client.io.StringHandle; import com.marklogic.client.io.DocumentMetadataHandle.Capability; import com.marklogic.client.query.MatchDocumentSummary; -import com.marklogic.client.query.MatchLocation; import com.marklogic.client.query.QueryManager; import com.marklogic.client.query.RawStructuredQueryDefinition; -import com.marklogic.client.query.StringQueryDefinition; import com.marklogic.client.query.QueryManager.QueryView; import com.marklogic.client.query.StructuredQueryBuilder; import com.marklogic.client.query.StructuredQueryDefinition; @@ -145,7 +142,6 @@ public void testBulkSearchSQDwithDifferentPageSizes() throws IOException, Parser //Creating a txt document manager for bulk search TextDocumentManager docMgr = client.newTextDocumentManager(); //using QueryManger for query definition and set the search criteria - QueryManager queryMgr = client.newQueryManager(); StructuredQueryBuilder qb = new StructuredQueryBuilder(); StructuredQueryDefinition qd = qb.and(qb.term("foo","bar")); @@ -207,14 +203,13 @@ public void testBulkSearchSQDwithDifferentPageSizes() throws IOException, Parser //This test has set response to JSON and pass StringHandle with format as JSON, expectint it to work, logged an issue 82 @Test public void testBulkSearchSQDwithResponseFormatandStringHandle() throws Exception{ - int count =1; loadJSONDocuments(); JSONDocumentManager docMgr = client.newJSONDocumentManager(); QueryManager queryMgr = client.newQueryManager(); StructuredQueryBuilder qb = new StructuredQueryBuilder(); StructuredQueryDefinition qd = qb.and(qb.term("dog1","dog11")); - SearchHandle r = queryMgr.search(qd, new SearchHandle()); + queryMgr.search(qd, new SearchHandle()); docMgr.setResponseFormat(Format.JSON); docMgr.setSearchView(QueryView.METADATA); @@ -229,7 +224,6 @@ public void testBulkSearchSQDwithResponseFormatandStringHandle() throws Exceptio docMgr.readMetadata(rec.getUri(),mh); assertTrue("Records has permissions? ",mh.getPermissions().containsKey("flexrep-eval")); assertTrue("Record has collections ?",mh.getCollections().isEmpty()); - count++; } assertFalse("Search handle contains",results.get().isEmpty()); @@ -249,7 +243,7 @@ public void testBulkSearchSQDwithJSONResponseFormat() throws Exception{ docMgr.setSearchView(QueryView.FACETS); JacksonHandle jh = new JacksonHandle(); - DocumentPage page= docMgr.search(qd, 1,jh); + docMgr.search(qd, 1,jh); // System.out.println(jh.get().toString()); assertTrue("Searh response has entry for facets",jh.get().has("facets")); @@ -257,21 +251,21 @@ public void testBulkSearchSQDwithJSONResponseFormat() throws Exception{ assertTrue("Searh response has entry for facets",jh.get().has("metrics")); docMgr.setSearchView(QueryView.RESULTS); - page= docMgr.search(qd, 1,jh); + docMgr.search(qd, 1,jh); assertFalse("Searh response has entry for facets",jh.get().has("facets")); assertTrue("Searh response has entry for facets",jh.get().has("results")); assertTrue("Searh response has entry for facets",jh.get().has("metrics"));//Issue 84 is tracking this docMgr.setSearchView(QueryView.METADATA); - page= docMgr.search(qd, 1,jh); + docMgr.search(qd, 1,jh); assertFalse("Searh response has entry for facets",jh.get().has("facets")); assertTrue("Searh response has entry for facets",jh.get().has("results")); assertTrue("Searh response has entry for facets",jh.get().has("metrics")); docMgr.setSearchView(QueryView.ALL); - page= docMgr.search(qd, 1,jh); + docMgr.search(qd, 1,jh); assertTrue("Searh response has entry for facets",jh.get().has("facets")); assertTrue("Searh response has entry for facets",jh.get().has("results")); @@ -288,7 +282,6 @@ public void testBulkSearchSQDwithJSONResponseFormat() throws Exception{ public void testBulkSearchSQDwithTransactionsandDOMHandle() throws Exception{ XMLDocumentManager docMgr = client.newXMLDocumentManager(); DOMHandle results = new DOMHandle(); - QueryManager queryMgr = client.newQueryManager(); StructuredQueryBuilder qb = new StructuredQueryBuilder(); StructuredQueryDefinition qd = qb.and(qb.term("much","thought")); Transaction t= client.openTransaction(); @@ -324,14 +317,14 @@ public void testBulkSearchSQDwithTransactionsandDOMHandle() throws Exception{ }catch(Exception e){ throw e;} finally{t.rollback();} - DocumentPage page= docMgr.search(qd, 1,results); + docMgr.search(qd, 1,results); assertEquals("Total search results after rollback are ","0",results.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total").getNodeValue()); } //This test is to verify RAW XML structured Query @Test public void testBulkSearchRawXMLStrucQD() throws Exception{ - setAutomaticDirectoryCreation(dbName,"automatic"); +// setAutomaticDirectoryCreation(dbName,"automatic"); setMaintainLastModified(dbName,true); this.loadJSONDocuments(); this.loadXMLDocuments(); @@ -382,7 +375,7 @@ public void testBulkSearchRawXMLStrucQD() throws Exception{ //This test is to verify RAW JSON structured query @Test public void testBulkSearchRawJSONStrucQD() throws Exception{ - setAutomaticDirectoryCreation(dbName,"automatic"); +// setAutomaticDirectoryCreation(dbName,"automatic"); setMaintainLastModified(dbName,true); this.loadJSONDocuments(); this.loadXMLDocuments(); From 92e21bea1f1eb669f8c29502e8c938f56b70212b Mon Sep 17 00:00:00 2001 From: gvaidees Date: Wed, 20 Aug 2014 23:59:05 -0700 Subject: [PATCH 081/357] Code cleanup --- .../javaclient/TestKeyValueSearch.java | 386 ++-- .../javaclient/TestLinkResultDocuments.java | 513 +++-- .../marklogic/javaclient/TestMetadata.java | 699 +++---- .../marklogic/javaclient/TestMetadataXML.java | 350 ++-- .../javaclient/TestMultithreading.java | 270 ++- .../marklogic/javaclient/TestNamespaces.java | 330 ++-- .../javaclient/TestOptimisticLocking.java | 1311 +++++++------ .../javaclient/TestOutputStreamHandle.java | 602 +++--- .../javaclient/TestPartialUpdate.java | 1723 ++++++++--------- .../javaclient/TestPatchCardinality.java | 850 ++++---- .../javaclient/TestQueryByExample.java | 960 ++++----- .../javaclient/TestQueryOptionBuilder.java | 1565 +++++++-------- .../TestQueryOptionBuilderGrammar.java | 684 ++++--- .../TestQueryOptionBuilderSearchOptions.java | 381 ++-- ...ueryOptionBuilderSearchableExpression.java | 797 ++++---- .../TestQueryOptionBuilderSortOrder.java | 1068 +++++----- ...estQueryOptionBuilderTransformResults.java | 369 ++-- .../javaclient/TestQueryOptionsHandle.java | 621 +++--- .../TestQueryOptionsListHandle.java | 102 +- .../javaclient/TestRangeConstraint.java | 129 +- .../TestRangeConstraintAbsoluteBucket.java | 123 +- .../TestRangeConstraintRelativeBucket.java | 120 +- .../marklogic/javaclient/TestRawAlert.java | 1605 ++++++++------- .../javaclient/TestRawCombinedQuery.java | 1531 ++++++++------- .../javaclient/TestRawCombinedQueryGeo.java | 705 ++++--- .../javaclient/TestRawStructuredQuery.java | 1104 ++++++----- 26 files changed, 9431 insertions(+), 9467 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestKeyValueSearch.java b/test-complete/src/test/java/com/marklogic/javaclient/TestKeyValueSearch.java index f82d23d4d..ef78b7a48 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestKeyValueSearch.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestKeyValueSearch.java @@ -1,197 +1,189 @@ -package com.marklogic.javaclient; - -import java.io.IOException; - -import javax.xml.namespace.QName; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import com.marklogic.client.query.MatchDocumentSummary; -import com.marklogic.client.query.MatchLocation; -import com.marklogic.client.query.QueryManager; - -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.Transaction; -import com.marklogic.client.query.KeyValueQueryDefinition; -import com.marklogic.client.admin.NamespacesManager; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.SearchHandle; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; -import static org.junit.Assert.*; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.junit.*; -public class TestKeyValueSearch extends BasicJavaClientREST { - - private static String dbName = "TestKeyValueSearchDB"; - private static String [] fNames = {"TestKeyValueSearchDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort=8011; - -@BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - } - -@After - public void testCleanUp() throws Exception - { - clearDB(restPort); - System.out.println("Running clear script"); - } - -@SuppressWarnings("deprecation") -@Test - public void testKeyValueSearch() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testKeyValueSearch"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "valueConstraintWithoutIndexSettingsAndNSOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create transaction - Transaction transaction1 = client.openTransaction(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/key-value-search/", transaction1, "XML"); - } - - // commit transaction - transaction1.commit(); - - // create transaction - Transaction transaction2 = client.openTransaction(); - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition - KeyValueQueryDefinition querydef = queryMgr.newKeyValueDefinition(queryOptionName); - querydef.put(queryMgr.newElementLocator(new QName("id")), "0012"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle, transaction2); - - // commit transaction - transaction2.commit(); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - - String expectedSearchReport = "(cts:search(fn:collection(), cts:element-value-query(fn:QName(\"\",\"id\"), \"0012\", (\"case-sensitive\",\"diacritic-sensitive\",\"punctuation-sensitive\",\"whitespace-sensitive\",\"unstemmed\",\"unwildcarded\",\"lexicon-expand=heuristic\",\"lang=en\"), 1), (\"score-logtfidf\"), 1))[1 to 10]"; - - assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testKeyValueSearchWithNS() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testKeyValueSearchWithNS"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - //String queryOptionName = "valueConstraintWithoutIndexSettingsAndNSOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // setup namespaces to test kv with namespaces - NamespacesManager nsMgr = client.newServerConfigManager().newNamespacesManager(); - nsMgr.updatePrefix("dt","http://purl.org/dc/elements/1.1/"); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/key-value-search-ns/", "XML"); - } - - //setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition - KeyValueQueryDefinition querydef = queryMgr.newKeyValueDefinition(); - querydef.put(queryMgr.newElementLocator(new QName("dt:date")), "2005-01-01"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - //SearchHandle resultsHandle = new SearchHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("/key-value-search-ns/constraint1.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testKeyValueSearchJSON() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("testKeyValueSearchJSON"); - - String[] filenames = {"json-original.json", "json-updated.json"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/key-value-search-json/", "JSON"); - } - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition - KeyValueQueryDefinition querydef = queryMgr.newKeyValueDefinition(); - querydef.put(queryMgr.newKeyLocator("firstName"), "Aries"); - - SearchHandle results = queryMgr.search(querydef, new SearchHandle()); - - MatchDocumentSummary[] summaries = results.getMatchResults(); - - for (MatchDocumentSummary summary : summaries) - { - MatchLocation[] locations = summary.getMatchLocations(); - for (MatchLocation location : locations) - { - System.out.println(location.getAllSnippetText()); - assertEquals("Invalid value", "Aries", location.getAllSnippetText()); - } - } - - // release client - client.release(); - } - @AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} +package com.marklogic.javaclient; + +import java.io.IOException; + +import javax.xml.namespace.QName; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import com.marklogic.client.query.MatchDocumentSummary; +import com.marklogic.client.query.MatchLocation; +import com.marklogic.client.query.QueryManager; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.Transaction; +import com.marklogic.client.query.KeyValueQueryDefinition; +import com.marklogic.client.admin.NamespacesManager; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.SearchHandle; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; +import static org.junit.Assert.*; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.*; +public class TestKeyValueSearch extends BasicJavaClientREST { + private static String dbName = "TestKeyValueSearchDB"; + private static String [] fNames = {"TestKeyValueSearchDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort=8011; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + } + + @After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + + @Test + public void testKeyValueSearch() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testKeyValueSearch"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "valueConstraintWithoutIndexSettingsAndNSOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create transaction + Transaction transaction1 = client.openTransaction(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/key-value-search/", transaction1, "XML"); + } + + // commit transaction + transaction1.commit(); + + // create transaction + Transaction transaction2 = client.openTransaction(); + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition + KeyValueQueryDefinition querydef = queryMgr.newKeyValueDefinition(queryOptionName); + querydef.put(queryMgr.newElementLocator(new QName("id")), "0012"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle, transaction2); + + // commit transaction + transaction2.commit(); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); + + String expectedSearchReport = "(cts:search(fn:collection(), cts:element-value-query(fn:QName(\"\",\"id\"), \"0012\", (\"case-sensitive\",\"diacritic-sensitive\",\"punctuation-sensitive\",\"whitespace-sensitive\",\"unstemmed\",\"unwildcarded\",\"lexicon-expand=heuristic\",\"lang=en\"), 1), (\"score-logtfidf\"), 1))[1 to 10]"; + + assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testKeyValueSearchWithNS() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testKeyValueSearchWithNS"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + //String queryOptionName = "valueConstraintWithoutIndexSettingsAndNSOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // setup namespaces to test kv with namespaces + NamespacesManager nsMgr = client.newServerConfigManager().newNamespacesManager(); + nsMgr.updatePrefix("dt","http://purl.org/dc/elements/1.1/"); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/key-value-search-ns/", "XML"); + } + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition + KeyValueQueryDefinition querydef = queryMgr.newKeyValueDefinition(); + querydef.put(queryMgr.newElementLocator(new QName("dt:date")), "2005-01-01"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + //System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("/key-value-search-ns/constraint1.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testKeyValueSearchJSON() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("testKeyValueSearchJSON"); + + String[] filenames = {"json-original.json", "json-updated.json"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/key-value-search-json/", "JSON"); + } + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition + KeyValueQueryDefinition querydef = queryMgr.newKeyValueDefinition(); + querydef.put(queryMgr.newKeyLocator("firstName"), "Aries"); + + SearchHandle results = queryMgr.search(querydef, new SearchHandle()); + + MatchDocumentSummary[] summaries = results.getMatchResults(); + + for (MatchDocumentSummary summary : summaries) + { + MatchLocation[] locations = summary.getMatchLocations(); + for (MatchLocation location : locations) + { + System.out.println(location.getAllSnippetText()); + assertEquals("Invalid value", "Aries", location.getAllSnippetText()); + } + } + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestLinkResultDocuments.java b/test-complete/src/test/java/com/marklogic/javaclient/TestLinkResultDocuments.java index 363729482..6fda787f9 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestLinkResultDocuments.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestLinkResultDocuments.java @@ -1,260 +1,255 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.File; -import java.io.IOException; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.xml.sax.SAXException; - - - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.SearchHandle; -import com.marklogic.client.io.XMLStreamReaderHandle; -import com.marklogic.client.query.MatchDocumentSummary; -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.query.StringQueryDefinition; -import org.junit.*; -public class TestLinkResultDocuments extends BasicJavaClientREST { - - private static String dbName = "TestLinkResultDocuments"; - private static String [] fNames = {"TestLinkResultDocuments-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; -@BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - // assocXDBCServer(serverName, dbName); - // assocRESTServer(restServerName, dbName); - setupAppServicesConstraint(dbName); - } - -@SuppressWarnings("deprecation") -@Test - public void testMimeType() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running TestLinkResultDocuments"); - - String[] filenames = {"constraint4.xml", "binary.jpg", "constraint4.json"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - if (filename.contains("xml")){ - writeDocumentUsingInputStreamHandle(client, filename, "/mime-type/", "XML"); - } - else if (filename.contains("json")){ - writeDocumentUsingInputStreamHandle(client, filename, "/mime-type/", "JSON"); - } - else if (filename.contains("jpg")){ - writeDocumentUsingInputStreamHandle(client, filename, "/mime-type/", "Binary"); - } - } - - // set query option - setQueryOption(client,"LinkResultDocumentsOpt.xml"); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition("LinkResultDocumentsOpt.xml"); - querydef.setCriteria("5"); - // QueryOptionsHandle as = new QueryOptionsHandle(); - - // create result handle - SearchHandle resultsHandle = queryMgr.search(querydef, new SearchHandle()); - - // get the result - for (MatchDocumentSummary result : resultsHandle.getMatchResults()) - { - System.out.println(result.getMimeType()+ ": Mime Type"); - System.out.println(result.getPath()+ ": Path"); - System.out.println(result.getFormat()+ ": Format"); - System.out.println(result.getUri()+ ": Uri"); - assertTrue("Uri is Wrong", result.getPath().contains("/mime-type/constraint4.json")||result.getPath().contains("/mime-type/constraint4.xml")); - } - - - XMLStreamReaderHandle shandle = queryMgr.search(querydef, new XMLStreamReaderHandle()); - String resultDoc2 = shandle.toString(); - System.out.println("Statics : \n"+resultDoc2); - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testResultDecorator() throws IOException { - - System.out.println("Running testResultDecorator"); - - String[] filenames = {"constraint4.xml", "binary.jpg", "constraint3.json"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - if (filename.contains("xml")){ - writeDocumentUsingInputStreamHandle(client, filename, "/mime-type/", "XML"); - } - else if (filename.contains("json")){ - writeDocumentUsingInputStreamHandle(client, filename, "/mime-type/", "JSON"); - } - else if (filename.contains("jpg")){ - writeDocumentUsingInputStreamHandle(client, filename, "/mime-type/", "Binary"); - } - } - try{ - String OS = System.getProperty("os.name"); - System.out.println("OS name : "+ OS); - File source = null; - File target = null; - if (OS.contains("Windows 7")){ - source = new File("C:/builds/winnt/HEAD/xcc/api_tests/src/test/java/com/marklogic/javaclient/data/result-decorator-test.xqy"); - target = new File("C:/Program Files/MarkLogic/Modules/MarkLogic/appservices/search/result-decorator-test.xqy"); - } - else if (OS.contains("Mac OS X")) { - source = new File("/space/builder/builds/macosx-64/HEAD/xcc/api_tests/src/test/java/com/marklogic/javaclient/data/result-decorator-test.xqy"); - target = new File("/Users/buildermac/Library/MarkLogic/Modules/MarkLogic/appservices/search/result-decorator-test.xqy"); - } - else if (OS.contains("Linux")) { - source = new File("/space/builder/builds/macosx/HEAD/xcc/api_tests/src/test/java/com/marklogic/javaclient/data/result-decorator-test.xqy"); - target = new File("/opt/MarkLogic/Modules/MarkLogic/appservices/search/result-decorator-test.xqy"); - } - - System.out.println(source.exists()); - System.out.println(target.exists()); - if (target.exists()){ - target.delete(); - } - copyWithChannels(source, target, true); - // set query option - setQueryOption(client,"LinkResultDocumentsOptDecorator.xml"); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition("LinkResultDocumentsOptDecorator.xml"); - querydef.setCriteria("5"); - - // create result handle - SearchHandle resultsHandle = queryMgr.search(querydef, new SearchHandle()); - - // get the result - for (MatchDocumentSummary result : resultsHandle.getMatchResults()) - { - System.out.println(result.getMimeType()+ ": Mime Type"); - System.out.println(result.getPath()+ ": Path"); - System.out.println(result.getFormat()+ ": Format"); - System.out.println(result.getUri()+ ": Uri"); - } - XMLStreamReaderHandle shandle = queryMgr.search(querydef, new XMLStreamReaderHandle()); - String resultDoc2 = shandle.toString(); - System.out.println("Statics : \n"+resultDoc2); - //libsMgr.delete(Path); - - } - catch(Exception e){ - e.printStackTrace(); - } - // release client - - client.release(); - - } - -@SuppressWarnings("deprecation") -@Test public void testResultDecoratorNoMimeType() throws IOException { - - System.out.println("Running testResultDecoratorNoMimeType"); - - String[] filenames = {"constraint4.xml", "binary.jpg", "constraint4.json"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - if (filename.contains("xml")){ - writeDocumentUsingInputStreamHandle(client, filename, "/mime-type/", "XML"); - } - else if (filename.contains("json")){ - writeDocumentUsingInputStreamHandle(client, filename, "/mime-type/", "JSON"); - } - else if (filename.contains("jpg")){ - writeDocumentUsingInputStreamHandle(client, filename, "/mime-type/", "Binary"); - } - } - try{ - String OS = System.getProperty("os.name"); - System.out.println("OS name : "+ OS); - File source = null; - File target = null; - if (OS.contains("Windows 7")){ - source = new File("C:/builds/winnt/HEAD/xcc/api_tests/src/test/java/com/marklogic/javaclient/data/result-decorator-test.xqy"); - target = new File("C:/Program Files/MarkLogic/Modules/MarkLogic/appservices/search/result-decorator-test.xqy"); - } - else if (OS.contains("Mac OS X")) { - source = new File("/space/builder/builds/macosx-64/HEAD/xcc/api_tests/src/test/java/com/marklogic/javaclient/data/result-decorator-test.xqy"); - target = new File("/Users/buildermac/Library/MarkLogic/Modules/MarkLogic/appservices/search/result-decorator-test.xqy"); - } - else if (OS.contains("Linux")) { - source = new File("/space/builder/builds/macosx/HEAD/xcc/api_tests/src/test/java/com/marklogic/javaclient/data/result-decorator-test.xqy"); - target = new File("/opt/MarkLogic/Modules/MarkLogic/appservices/search/result-decorator-test.xqy"); - } - - System.out.println(source.exists()); - System.out.println(target.exists()); - if (target.exists()){ - target.delete(); - } - copyWithChannels(source, target, true); - // set query option - setQueryOption(client,"LinkResultDocumentsOptDecorator1.xml"); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition("LinkResultDocumentsOptDecorator1.xml"); - querydef.setCriteria("5"); - - // create result handle - SearchHandle resultsHandle = queryMgr.search(querydef, new SearchHandle()); - - // get the result - for (MatchDocumentSummary result : resultsHandle.getMatchResults()) - { - System.out.println(result.getMimeType()+ ": Mime Type"); - System.out.println(result.getPath()+ ": Path"); - System.out.println(result.getFormat()+ ": Format"); - System.out.println(result.getUri()+ ": Uri"); - } - XMLStreamReaderHandle shandle = queryMgr.search(querydef, new XMLStreamReaderHandle()); - String resultDoc2 = shandle.toString(); - System.out.println("Statics : \n"+resultDoc2); - //libsMgr.delete(Path); - - } - catch(Exception e){ - e.printStackTrace(); - } - // release client - - client.release(); - - } -@AfterClass - public static void tearDown() throws Exception - { - tearDownJavaRESTServer(dbName, fNames, restServerName); - } +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.IOException; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.xml.sax.SAXException; + + + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.SearchHandle; +import com.marklogic.client.io.XMLStreamReaderHandle; +import com.marklogic.client.query.MatchDocumentSummary; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.StringQueryDefinition; +import org.junit.*; +public class TestLinkResultDocuments extends BasicJavaClientREST { + + private static String dbName = "TestLinkResultDocuments"; + private static String [] fNames = {"TestLinkResultDocuments-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011);; + setupAppServicesConstraint(dbName); + } + + @Test + public void testMimeType() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running TestLinkResultDocuments"); + + String[] filenames = {"constraint4.xml", "binary.jpg", "constraint4.json"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + if (filename.contains("xml")){ + writeDocumentUsingInputStreamHandle(client, filename, "/mime-type/", "XML"); + } + else if (filename.contains("json")){ + writeDocumentUsingInputStreamHandle(client, filename, "/mime-type/", "JSON"); + } + else if (filename.contains("jpg")){ + writeDocumentUsingInputStreamHandle(client, filename, "/mime-type/", "Binary"); + } + } + + // set query option + setQueryOption(client,"LinkResultDocumentsOpt.xml"); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition("LinkResultDocumentsOpt.xml"); + querydef.setCriteria("5"); + + // create result handle + SearchHandle resultsHandle = queryMgr.search(querydef, new SearchHandle()); + + // get the result + for (MatchDocumentSummary result : resultsHandle.getMatchResults()) + { + System.out.println(result.getMimeType()+ ": Mime Type"); + System.out.println(result.getPath()+ ": Path"); + System.out.println(result.getFormat()+ ": Format"); + System.out.println(result.getUri()+ ": Uri"); + assertTrue("Uri is Wrong", result.getPath().contains("/mime-type/constraint4.json")||result.getPath().contains("/mime-type/constraint4.xml")); + } + + + XMLStreamReaderHandle shandle = queryMgr.search(querydef, new XMLStreamReaderHandle()); + String resultDoc2 = shandle.toString(); + System.out.println("Statics : \n"+resultDoc2); + // release client + client.release(); + } + + @Test + public void testResultDecorator() throws IOException { + + System.out.println("Running testResultDecorator"); + + String[] filenames = {"constraint4.xml", "binary.jpg", "constraint3.json"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + if (filename.contains("xml")){ + writeDocumentUsingInputStreamHandle(client, filename, "/mime-type/", "XML"); + } + else if (filename.contains("json")){ + writeDocumentUsingInputStreamHandle(client, filename, "/mime-type/", "JSON"); + } + else if (filename.contains("jpg")){ + writeDocumentUsingInputStreamHandle(client, filename, "/mime-type/", "Binary"); + } + } + try{ + String OS = System.getProperty("os.name"); + System.out.println("OS name : "+ OS); + File source = null; + File target = null; + if (OS.contains("Windows 7")){ + source = new File("C:/builds/winnt/HEAD/xcc/api_tests/src/test/java/com/marklogic/javaclient/data/result-decorator-test.xqy"); + target = new File("C:/Program Files/MarkLogic/Modules/MarkLogic/appservices/search/result-decorator-test.xqy"); + } + else if (OS.contains("Mac OS X")) { + source = new File("/space/builder/builds/macosx-64/HEAD/xcc/api_tests/src/test/java/com/marklogic/javaclient/data/result-decorator-test.xqy"); + target = new File("/Users/buildermac/Library/MarkLogic/Modules/MarkLogic/appservices/search/result-decorator-test.xqy"); + } + else if (OS.contains("Linux")) { + source = new File("/space/builder/builds/macosx/HEAD/xcc/api_tests/src/test/java/com/marklogic/javaclient/data/result-decorator-test.xqy"); + target = new File("/opt/MarkLogic/Modules/MarkLogic/appservices/search/result-decorator-test.xqy"); + } + + System.out.println(source.exists()); + System.out.println(target.exists()); + if (target.exists()){ + target.delete(); + } + copyWithChannels(source, target, true); + // set query option + setQueryOption(client,"LinkResultDocumentsOptDecorator.xml"); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition("LinkResultDocumentsOptDecorator.xml"); + querydef.setCriteria("5"); + + // create result handle + SearchHandle resultsHandle = queryMgr.search(querydef, new SearchHandle()); + + // get the result + for (MatchDocumentSummary result : resultsHandle.getMatchResults()) + { + System.out.println(result.getMimeType()+ ": Mime Type"); + System.out.println(result.getPath()+ ": Path"); + System.out.println(result.getFormat()+ ": Format"); + System.out.println(result.getUri()+ ": Uri"); + } + XMLStreamReaderHandle shandle = queryMgr.search(querydef, new XMLStreamReaderHandle()); + String resultDoc2 = shandle.toString(); + System.out.println("Statics : \n"+resultDoc2); + + } + catch(Exception e){ + e.printStackTrace(); + } + + // release client + client.release(); + + } + + @Test + public void testResultDecoratorNoMimeType() throws IOException { + + System.out.println("Running testResultDecoratorNoMimeType"); + + String[] filenames = {"constraint4.xml", "binary.jpg", "constraint4.json"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + if (filename.contains("xml")){ + writeDocumentUsingInputStreamHandle(client, filename, "/mime-type/", "XML"); + } + else if (filename.contains("json")){ + writeDocumentUsingInputStreamHandle(client, filename, "/mime-type/", "JSON"); + } + else if (filename.contains("jpg")){ + writeDocumentUsingInputStreamHandle(client, filename, "/mime-type/", "Binary"); + } + } + try{ + String OS = System.getProperty("os.name"); + System.out.println("OS name : "+ OS); + File source = null; + File target = null; + if (OS.contains("Windows 7")){ + source = new File("C:/builds/winnt/HEAD/xcc/api_tests/src/test/java/com/marklogic/javaclient/data/result-decorator-test.xqy"); + target = new File("C:/Program Files/MarkLogic/Modules/MarkLogic/appservices/search/result-decorator-test.xqy"); + } + else if (OS.contains("Mac OS X")) { + source = new File("/space/builder/builds/macosx-64/HEAD/xcc/api_tests/src/test/java/com/marklogic/javaclient/data/result-decorator-test.xqy"); + target = new File("/Users/buildermac/Library/MarkLogic/Modules/MarkLogic/appservices/search/result-decorator-test.xqy"); + } + else if (OS.contains("Linux")) { + source = new File("/space/builder/builds/macosx/HEAD/xcc/api_tests/src/test/java/com/marklogic/javaclient/data/result-decorator-test.xqy"); + target = new File("/opt/MarkLogic/Modules/MarkLogic/appservices/search/result-decorator-test.xqy"); + } + + System.out.println(source.exists()); + System.out.println(target.exists()); + if (target.exists()){ + target.delete(); + } + copyWithChannels(source, target, true); + // set query option + setQueryOption(client,"LinkResultDocumentsOptDecorator1.xml"); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition("LinkResultDocumentsOptDecorator1.xml"); + querydef.setCriteria("5"); + + // create result handle + SearchHandle resultsHandle = queryMgr.search(querydef, new SearchHandle()); + + // get the result + for (MatchDocumentSummary result : resultsHandle.getMatchResults()) + { + System.out.println(result.getMimeType()+ ": Mime Type"); + System.out.println(result.getPath()+ ": Path"); + System.out.println(result.getFormat()+ ": Format"); + System.out.println(result.getUri()+ ": Uri"); + } + XMLStreamReaderHandle shandle = queryMgr.search(querydef, new XMLStreamReaderHandle()); + String resultDoc2 = shandle.toString(); + System.out.println("Statics : \n"+resultDoc2); + + } + catch(Exception e){ + e.printStackTrace(); + } + // release client + + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + tearDownJavaRESTServer(dbName, fNames, restServerName); + } } \ No newline at end of file diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestMetadata.java b/test-complete/src/test/java/com/marklogic/javaclient/TestMetadata.java index 816425649..1515a8fd1 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestMetadata.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestMetadata.java @@ -1,335 +1,364 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.IOException; -import java.util.Calendar; - -import javax.xml.bind.JAXBException; -import javax.xml.namespace.QName; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.DocumentMetadataHandle; -import com.marklogic.client.io.DocumentMetadataHandle.Capability; -import com.marklogic.client.io.DocumentMetadataHandle.DocumentCollections; -import com.marklogic.client.io.DocumentMetadataHandle.DocumentPermissions; -import com.marklogic.client.io.DocumentMetadataHandle.DocumentProperties; -import com.marklogic.javaclient.BasicJavaClientREST; -import org.junit.*; -public class TestMetadata extends BasicJavaClientREST{ - - private static String dbName = "TestMetadataDB"; - private static String [] fNames = {"TestMetadataDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - @BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - } - - @SuppressWarnings("deprecation") - @Test - public void testBinaryMetadataBytesHandle() throws IOException - { - System.out.println("Running testBinaryMetadataBytesHandle"); - - String filename = "Simple_ScanTe.png"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create and initialize a handle on the metadata - DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); - - // put metadata - metadataHandle.getCollections().addAll("my-collection"); - metadataHandle.getCollections().addAll("another-collection"); - metadataHandle.getPermissions().add("app-user", Capability.UPDATE, Capability.READ); - metadataHandle.getProperties().put("reviewed", true); - metadataHandle.getProperties().put("myString", "foo"); - metadataHandle.getProperties().put("myInteger", 10); - metadataHandle.getProperties().put("myDecimal", 34.56678); - metadataHandle.getProperties().put("myCalendar", Calendar.getInstance().get(Calendar.YEAR)); - metadataHandle.setQuality(23); - - // write the doc with the metadata - writeDocumentUsingBytesHandle(client, filename, "/write-bin-byteshandle-metadata/", metadataHandle, "Binary"); - - // create handle to read metadata - DocumentMetadataHandle readMetadataHandle = new DocumentMetadataHandle(); - - // read metadata - readMetadataHandle = readMetadataFromDocument(client, "/write-bin-byteshandle-metadata/" + filename, "Binary"); - - // get metadata values - DocumentProperties properties = readMetadataHandle.getProperties(); - DocumentPermissions permissions = readMetadataHandle.getPermissions(); - DocumentCollections collections = readMetadataHandle.getCollections(); - - // Properties - String expectedProperties = "size:5|reviewed:true|myInteger:10|myDecimal:34.56678|myCalendar:2014|myString:foo|"; - String actualProperties = getDocumentPropertiesString(properties); - System.out.println("Actual Prop : "+actualProperties); - assertEquals("Document properties difference", expectedProperties, actualProperties); - - // Permissions - String expectedPermissions1 = "size:3|rest-reader:[READ]|app-user:[UPDATE, READ]|rest-writer:[UPDATE]|"; - String expectedPermissions2 = "size:3|rest-reader:[READ]|app-user:[READ, UPDATE]|rest-writer:[UPDATE]|"; - String actualPermissions = getDocumentPermissionsString(permissions); - if(actualPermissions.contains("[UPDATE, READ]")) - assertEquals("Document permissions difference", expectedPermissions1, actualPermissions); - else if(actualPermissions.contains("[READ, UPDATE]")) - assertEquals("Document permissions difference", expectedPermissions2, actualPermissions); - else - assertEquals("Document permissions difference", "wrong", actualPermissions); - - // Collections - String expectedCollections = "size:2|another-collection|my-collection|"; - String actualCollections = getDocumentCollectionsString(collections); - assertEquals("Document collections difference", expectedCollections, actualCollections); - - // release the client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testTextMetadataStringHandle() throws IOException - { - System.out.println("Running testTextMetadataStringHandle"); - - String filename = "facebook-10443244874876159931"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create and initialize a handle on the metadata - DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); - - // put metadata - metadataHandle.getCollections().addAll("my-collection"); - metadataHandle.getCollections().addAll("another-collection"); - metadataHandle.getPermissions().add("app-user", Capability.UPDATE, Capability.READ); - metadataHandle.getProperties().put("reviewed", true); - metadataHandle.getProperties().put("myString", "foo"); - metadataHandle.getProperties().put("myInteger", 10); - metadataHandle.getProperties().put("myDecimal", 34.56678); - metadataHandle.getProperties().put("myCalendar", Calendar.getInstance().get(Calendar.YEAR)); - metadataHandle.setQuality(23); - - // write the doc with the metadata - writeDocumentUsingStringHandle(client, filename, "/write-text-stringhandle-metadata/", metadataHandle, "Text"); - - // create handle to read metadata - DocumentMetadataHandle readMetadataHandle = new DocumentMetadataHandle(); - - // read metadata - readMetadataHandle = readMetadataFromDocument(client, "/write-text-stringhandle-metadata/" + filename, "Text"); - - // get metadata values - DocumentProperties properties = readMetadataHandle.getProperties(); - DocumentPermissions permissions = readMetadataHandle.getPermissions(); - DocumentCollections collections = readMetadataHandle.getCollections(); - - // Properties - String expectedProperties = "size:5|reviewed:true|myInteger:10|myDecimal:34.56678|myCalendar:2014|myString:foo|"; - String actualProperties = getDocumentPropertiesString(properties); - assertEquals("Document properties difference", expectedProperties, actualProperties); - - // Permissions - String expectedPermissions1 = "size:3|rest-reader:[READ]|app-user:[UPDATE, READ]|rest-writer:[UPDATE]|"; - String expectedPermissions2 = "size:3|rest-reader:[READ]|app-user:[READ, UPDATE]|rest-writer:[UPDATE]|"; - String actualPermissions = getDocumentPermissionsString(permissions); - if(actualPermissions.contains("[UPDATE, READ]")) - assertEquals("Document permissions difference", expectedPermissions1, actualPermissions); - else if(actualPermissions.contains("[READ, UPDATE]")) - assertEquals("Document permissions difference", expectedPermissions2, actualPermissions); - else - assertEquals("Document permissions difference", "wrong", actualPermissions); - - // Collections - String expectedCollections = "size:2|another-collection|my-collection|"; - String actualCollections = getDocumentCollectionsString(collections); - assertEquals("Document collections difference", expectedCollections, actualCollections); - - // release the client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testXMLMetadataJAXBHandle() throws JAXBException - { - System.out.println("Running testXMLMetadataJAXBHandle"); - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - Product product1 = new Product(); - product1.setName("iPad"); - product1.setIndustry("Hardware"); - product1.setDescription("Very cool device"); - - // create and initialize a handle on the metadata - DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); - - // put metadata - metadataHandle.getCollections().addAll("my-collection"); - metadataHandle.getCollections().addAll("another-collection"); - metadataHandle.getPermissions().add("app-user", Capability.UPDATE, Capability.READ); - metadataHandle.getProperties().put("reviewed", true); - metadataHandle.getProperties().put("myString", "foo"); - metadataHandle.getProperties().put("myInteger", 10); - metadataHandle.getProperties().put("myDecimal", 34.56678); - metadataHandle.getProperties().put("myCalendar", Calendar.getInstance().get(Calendar.YEAR)); - metadataHandle.setQuality(23); - - // write the doc with the metadata - writeDocumentUsingJAXBHandle(client, product1, "/write-xml-jaxbhandle-metadata/", metadataHandle, "XML"); - - // create handle to read metadata - DocumentMetadataHandle readMetadataHandle = new DocumentMetadataHandle(); - - // read metadata - readMetadataHandle = readMetadataFromDocument(client, "/write-xml-jaxbhandle-metadata/" + product1.getName() + ".xml", "XML"); - - // get metadata values - DocumentProperties properties = readMetadataHandle.getProperties(); - DocumentPermissions permissions = readMetadataHandle.getPermissions(); - DocumentCollections collections = readMetadataHandle.getCollections(); - - // Properties - String expectedProperties = "size:5|reviewed:true|myInteger:10|myDecimal:34.56678|myCalendar:2014|myString:foo|"; - String actualProperties = getDocumentPropertiesString(properties); - assertEquals("Document properties difference", expectedProperties, actualProperties); - - // Permissions - String expectedPermissions1 = "size:3|rest-reader:[READ]|app-user:[UPDATE, READ]|rest-writer:[UPDATE]|"; - String expectedPermissions2 = "size:3|rest-reader:[READ]|app-user:[READ, UPDATE]|rest-writer:[UPDATE]|"; - String actualPermissions = getDocumentPermissionsString(permissions); - if(actualPermissions.contains("[UPDATE, READ]")) - assertEquals("Document permissions difference", expectedPermissions1, actualPermissions); - else if(actualPermissions.contains("[READ, UPDATE]")) - assertEquals("Document permissions difference", expectedPermissions2, actualPermissions); - else - assertEquals("Document permissions difference", "wrong", actualPermissions); - - // Collections - String expectedCollections = "size:2|another-collection|my-collection|"; - String actualCollections = getDocumentCollectionsString(collections); - assertEquals("Document collections difference", expectedCollections, actualCollections); - - // release the client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testJSONMetadataOutputStreamHandle() throws JAXBException - { - System.out.println("Running testJSONMetadataOutputStreamHandle"); - - String filename = "myJSONFile.json"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create and initialize a handle on the metadata - DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); - - // put metadata - metadataHandle.getCollections().addAll("my-collection"); - metadataHandle.getCollections().addAll("another-collection"); - metadataHandle.getPermissions().add("app-user", Capability.UPDATE, Capability.READ); - metadataHandle.getProperties().put("reviewed", true); - metadataHandle.getProperties().put("myString", "foo"); - metadataHandle.getProperties().put("myInteger", 10); - metadataHandle.getProperties().put("myDecimal", 34.56678); - metadataHandle.getProperties().put("myCalendar", Calendar.getInstance().get(Calendar.YEAR)); - metadataHandle.setQuality(23); - - // write the doc with the metadata - writeDocumentUsingOutputStreamHandle(client, filename, "/write-json-outputstreamhandle-metadata/", metadataHandle, "JSON"); - - // create handle to read metadata - DocumentMetadataHandle readMetadataHandle = new DocumentMetadataHandle(); - - // read metadata - readMetadataHandle = readMetadataFromDocument(client, "/write-json-outputstreamhandle-metadata/" + filename, "JSON"); - - // get metadata values - DocumentProperties properties = readMetadataHandle.getProperties(); - DocumentPermissions permissions = readMetadataHandle.getPermissions(); - DocumentCollections collections = readMetadataHandle.getCollections(); - - // Properties - String expectedProperties = "size:5|reviewed:true|myInteger:10|myDecimal:34.56678|myCalendar:2014|myString:foo|"; - String actualProperties = getDocumentPropertiesString(properties); - assertEquals("Document properties difference", expectedProperties, actualProperties); - - // Permissions - String expectedPermissions1 = "size:3|rest-reader:[READ]|app-user:[UPDATE, READ]|rest-writer:[UPDATE]|"; - String expectedPermissions2 = "size:3|rest-reader:[READ]|app-user:[READ, UPDATE]|rest-writer:[UPDATE]|"; - String actualPermissions = getDocumentPermissionsString(permissions); - if(actualPermissions.contains("[UPDATE, READ]")) - assertEquals("Document permissions difference", expectedPermissions1, actualPermissions); - else if(actualPermissions.contains("[READ, UPDATE]")) - assertEquals("Document permissions difference", expectedPermissions2, actualPermissions); - else - assertEquals("Document permissions difference", "wrong", actualPermissions); - - // Collections - String expectedCollections = "size:2|another-collection|my-collection|"; - String actualCollections = getDocumentCollectionsString(collections); - assertEquals("Document collections difference", expectedCollections, actualCollections); - - // release the client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testJSONMetadataQName() throws JAXBException - { - System.out.println("Running testJSONMetadataQName"); - - String filename = "myJSONFile.json"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create and initialize a handle on the metadata - DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); - - // put metadata - metadataHandle.getProperties().put(new QName("http://www.example.com", "foo"), "bar"); - - // write the doc with the metadata - writeDocumentUsingOutputStreamHandle(client, filename, "/write-json-outputstreamhandle-metadata/", metadataHandle, "JSON"); - - // create handle to read metadata - DocumentMetadataHandle readMetadataHandle = new DocumentMetadataHandle(); - - // read metadata - readMetadataHandle = readMetadataFromDocument(client, "/write-json-outputstreamhandle-metadata/" + filename, "JSON"); - - // get metadata values - DocumentProperties properties = readMetadataHandle.getProperties(); - - // Properties - String expectedProperties = "size:1|{http://www.example.com}foo:bar|"; - String actualProperties = getDocumentPropertiesString(properties); - System.out.println(actualProperties); - assertEquals("Document properties difference", expectedProperties, actualProperties); - - // release the client - client.release(); - } - @AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.IOException; +import java.util.Calendar; + +import javax.xml.bind.JAXBException; +import javax.xml.namespace.QName; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.DocumentMetadataHandle; +import com.marklogic.client.io.DocumentMetadataHandle.Capability; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentCollections; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentPermissions; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentProperties; +import com.marklogic.javaclient.BasicJavaClientREST; + +import org.junit.*; +public class TestMetadata extends BasicJavaClientREST{ + + private static String dbName = "TestMetadataDB"; + private static String [] fNames = {"TestMetadataDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + } + + @Test + public void testBinaryMetadataBytesHandle() throws IOException + { + System.out.println("Running testBinaryMetadataBytesHandle"); + + String filename = "Simple_ScanTe.png"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); + + // put metadata + metadataHandle.getCollections().addAll("my-collection"); + metadataHandle.getCollections().addAll("another-collection"); + metadataHandle.getPermissions().add("app-user", Capability.UPDATE, Capability.READ); + metadataHandle.getProperties().put("reviewed", true); + metadataHandle.getProperties().put("myString", "foo"); + metadataHandle.getProperties().put("myInteger", 10); + metadataHandle.getProperties().put("myDecimal", 34.56678); + metadataHandle.getProperties().put("myCalendar", Calendar.getInstance().get(Calendar.YEAR)); + metadataHandle.setQuality(23); + + // write the doc with the metadata + writeDocumentUsingBytesHandle(client, filename, "/write-bin-byteshandle-metadata/", metadataHandle, "Binary"); + + // create handle to read metadata + DocumentMetadataHandle readMetadataHandle = new DocumentMetadataHandle(); + + // read metadata + readMetadataHandle = readMetadataFromDocument(client, "/write-bin-byteshandle-metadata/" + filename, "Binary"); + + // get metadata values + DocumentProperties properties = readMetadataHandle.getProperties(); + DocumentPermissions permissions = readMetadataHandle.getPermissions(); + DocumentCollections collections = readMetadataHandle.getCollections(); + + // Properties + String actualProperties = getDocumentPropertiesString(properties); + System.out.println("Returned properties: " + actualProperties); + + assertTrue("Document properties difference in size value", actualProperties.contains("size:5")); + assertTrue("Document property reviewed not found or not correct", actualProperties.contains("reviewed:true")); + assertTrue("Document property myInteger not found or not correct", actualProperties.contains("myInteger:10")); + assertTrue("Document property myDecimal not found or not correct", actualProperties.contains("myDecimal:34.56678")); + assertTrue("Document property myCalendar not found or not correct", actualProperties.contains("myCalendar:2014")); + assertTrue("Document property myString not found or not correct", actualProperties.contains("myString:foo")); + + // Permissions + String actualPermissions = getDocumentPermissionsString(permissions); + System.out.println("Returned permissions: " + actualPermissions); + + assertTrue("Document permissions difference in size value", actualPermissions.contains("size:3")); + assertTrue("Document permissions difference in rest-reader permission", actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue("Document permissions difference in app-user permissions", + (actualPermissions.contains("app-user:[UPDATE, READ]") || actualPermissions.contains("app-user:[READ, UPDATE]"))); + + // Collections + String actualCollections = getDocumentCollectionsString(collections); + System.out.println("Returned collections: " + actualCollections); + + assertTrue("Document collections difference in size value", actualCollections.contains("size:2")); + assertTrue("my-collection1 not found", actualCollections.contains("another-collection")); + assertTrue("my-collection2 not found", actualCollections.contains("my-collection")); + + // release the client + client.release(); + } + + @Test + public void testTextMetadataStringHandle() throws IOException + { + System.out.println("Running testTextMetadataStringHandle"); + + String filename = "facebook-10443244874876159931"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); + + // put metadata + metadataHandle.getCollections().addAll("my-collection"); + metadataHandle.getCollections().addAll("another-collection"); + metadataHandle.getPermissions().add("app-user", Capability.UPDATE, Capability.READ); + metadataHandle.getProperties().put("reviewed", true); + metadataHandle.getProperties().put("myString", "foo"); + metadataHandle.getProperties().put("myInteger", 10); + metadataHandle.getProperties().put("myDecimal", 34.56678); + metadataHandle.getProperties().put("myCalendar", Calendar.getInstance().get(Calendar.YEAR)); + metadataHandle.setQuality(23); + + // write the doc with the metadata + writeDocumentUsingStringHandle(client, filename, "/write-text-stringhandle-metadata/", metadataHandle, "Text"); + + // create handle to read metadata + DocumentMetadataHandle readMetadataHandle = new DocumentMetadataHandle(); + + // read metadata + readMetadataHandle = readMetadataFromDocument(client, "/write-text-stringhandle-metadata/" + filename, "Text"); + + // get metadata values + DocumentProperties properties = readMetadataHandle.getProperties(); + DocumentPermissions permissions = readMetadataHandle.getPermissions(); + DocumentCollections collections = readMetadataHandle.getCollections(); + + // Properties + String actualProperties = getDocumentPropertiesString(properties); + System.out.println("Returned properties: " + actualProperties); + + assertTrue("Document properties difference in size value", actualProperties.contains("size:5")); + assertTrue("Document property reviewed not found or not correct", actualProperties.contains("reviewed:true")); + assertTrue("Document property myInteger not found or not correct", actualProperties.contains("myInteger:10")); + assertTrue("Document property myDecimal not found or not correct", actualProperties.contains("myDecimal:34.56678")); + assertTrue("Document property myCalendar not found or not correct", actualProperties.contains("myCalendar:2014")); + assertTrue("Document property myString not found or not correct", actualProperties.contains("myString:foo")); + + // Permissions + String actualPermissions = getDocumentPermissionsString(permissions); + System.out.println("Returned permissions: " + actualPermissions); + + assertTrue("Document permissions difference in size value", actualPermissions.contains("size:3")); + assertTrue("Document permissions difference in rest-reader permission", actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue("Document permissions difference in app-user permissions", + (actualPermissions.contains("app-user:[UPDATE, READ]") || actualPermissions.contains("app-user:[READ, UPDATE]"))); + + // Collections + String actualCollections = getDocumentCollectionsString(collections); + System.out.println("Returned collections: " + actualCollections); + + assertTrue("Document collections difference in size value", actualCollections.contains("size:2")); + assertTrue("my-collection1 not found", actualCollections.contains("another-collection")); + assertTrue("my-collection2 not found", actualCollections.contains("my-collection")); + + // release the client + client.release(); + } + + @Test + public void testXMLMetadataJAXBHandle() throws JAXBException + { + System.out.println("Running testXMLMetadataJAXBHandle"); + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + Product product1 = new Product(); + product1.setName("iPad"); + product1.setIndustry("Hardware"); + product1.setDescription("Very cool device"); + + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); + + // put metadata + metadataHandle.getCollections().addAll("my-collection"); + metadataHandle.getCollections().addAll("another-collection"); + metadataHandle.getPermissions().add("app-user", Capability.UPDATE, Capability.READ); + metadataHandle.getProperties().put("reviewed", true); + metadataHandle.getProperties().put("myString", "foo"); + metadataHandle.getProperties().put("myInteger", 10); + metadataHandle.getProperties().put("myDecimal", 34.56678); + metadataHandle.getProperties().put("myCalendar", Calendar.getInstance().get(Calendar.YEAR)); + metadataHandle.setQuality(23); + + // write the doc with the metadata + writeDocumentUsingJAXBHandle(client, product1, "/write-xml-jaxbhandle-metadata/", metadataHandle, "XML"); + + // create handle to read metadata + DocumentMetadataHandle readMetadataHandle = new DocumentMetadataHandle(); + + // read metadata + readMetadataHandle = readMetadataFromDocument(client, "/write-xml-jaxbhandle-metadata/" + product1.getName() + ".xml", "XML"); + + // get metadata values + DocumentProperties properties = readMetadataHandle.getProperties(); + DocumentPermissions permissions = readMetadataHandle.getPermissions(); + DocumentCollections collections = readMetadataHandle.getCollections(); + + // Properties + String actualProperties = getDocumentPropertiesString(properties); + System.out.println("Returned properties: " + actualProperties); + + assertTrue("Document properties difference in size value", actualProperties.contains("size:5")); + assertTrue("Document property reviewed not found or not correct", actualProperties.contains("reviewed:true")); + assertTrue("Document property myInteger not found or not correct", actualProperties.contains("myInteger:10")); + assertTrue("Document property myDecimal not found or not correct", actualProperties.contains("myDecimal:34.56678")); + assertTrue("Document property myCalendar not found or not correct", actualProperties.contains("myCalendar:2014")); + assertTrue("Document property myString not found or not correct", actualProperties.contains("myString:foo")); + + // Permissions + String actualPermissions = getDocumentPermissionsString(permissions); + System.out.println("Returned permissions: " + actualPermissions); + + assertTrue("Document permissions difference in size value", actualPermissions.contains("size:3")); + assertTrue("Document permissions difference in rest-reader permission", actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue("Document permissions difference in app-user permissions", + (actualPermissions.contains("app-user:[UPDATE, READ]") || actualPermissions.contains("app-user:[READ, UPDATE]"))); + + // Collections + String actualCollections = getDocumentCollectionsString(collections); + System.out.println("Returned collections: " + actualCollections); + + assertTrue("Document collections difference in size value", actualCollections.contains("size:2")); + assertTrue("my-collection1 not found", actualCollections.contains("another-collection")); + assertTrue("my-collection2 not found", actualCollections.contains("my-collection")); + + // release the client + client.release(); + } + + @Test + public void testJSONMetadataOutputStreamHandle() throws JAXBException + { + System.out.println("Running testJSONMetadataOutputStreamHandle"); + + String filename = "myJSONFile.json"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); + + // put metadata + metadataHandle.getCollections().addAll("my-collection"); + metadataHandle.getCollections().addAll("another-collection"); + metadataHandle.getPermissions().add("app-user", Capability.UPDATE, Capability.READ); + metadataHandle.getProperties().put("reviewed", true); + metadataHandle.getProperties().put("myString", "foo"); + metadataHandle.getProperties().put("myInteger", 10); + metadataHandle.getProperties().put("myDecimal", 34.56678); + metadataHandle.getProperties().put("myCalendar", Calendar.getInstance().get(Calendar.YEAR)); + metadataHandle.setQuality(23); + + // write the doc with the metadata + writeDocumentUsingOutputStreamHandle(client, filename, "/write-json-outputstreamhandle-metadata/", metadataHandle, "JSON"); + + // create handle to read metadata + DocumentMetadataHandle readMetadataHandle = new DocumentMetadataHandle(); + + // read metadata + readMetadataHandle = readMetadataFromDocument(client, "/write-json-outputstreamhandle-metadata/" + filename, "JSON"); + + // get metadata values + DocumentProperties properties = readMetadataHandle.getProperties(); + DocumentPermissions permissions = readMetadataHandle.getPermissions(); + DocumentCollections collections = readMetadataHandle.getCollections(); + + // Properties + String actualProperties = getDocumentPropertiesString(properties); + System.out.println("Returned properties: " + actualProperties); + + assertTrue("Document properties difference in size value", actualProperties.contains("size:5")); + assertTrue("Document property reviewed not found or not correct", actualProperties.contains("reviewed:true")); + assertTrue("Document property myInteger not found or not correct", actualProperties.contains("myInteger:10")); + assertTrue("Document property myDecimal not found or not correct", actualProperties.contains("myDecimal:34.56678")); + assertTrue("Document property myCalendar not found or not correct", actualProperties.contains("myCalendar:2014")); + assertTrue("Document property myString not found or not correct", actualProperties.contains("myString:foo")); + + // Permissions + String actualPermissions = getDocumentPermissionsString(permissions); + System.out.println("Returned permissions: " + actualPermissions); + + assertTrue("Document permissions difference in size value", actualPermissions.contains("size:3")); + assertTrue("Document permissions difference in rest-reader permission", actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue("Document permissions difference in app-user permissions", + (actualPermissions.contains("app-user:[UPDATE, READ]") || actualPermissions.contains("app-user:[READ, UPDATE]"))); + + // Collections + String actualCollections = getDocumentCollectionsString(collections); + System.out.println("Returned collections: " + actualCollections); + + assertTrue("Document collections difference in size value", actualCollections.contains("size:2")); + assertTrue("my-collection1 not found", actualCollections.contains("another-collection")); + assertTrue("my-collection2 not found", actualCollections.contains("my-collection")); + + // release the client + client.release(); + } + + @Test + public void testJSONMetadataQName() throws JAXBException + { + System.out.println("Running testJSONMetadataQName"); + + String filename = "myJSONFile.json"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); + + // put metadata + metadataHandle.getProperties().put(new QName("http://www.example.com", "foo"), "bar"); + + // write the doc with the metadata + writeDocumentUsingOutputStreamHandle(client, filename, "/write-json-outputstreamhandle-metadata/", metadataHandle, "JSON"); + + // create handle to read metadata + DocumentMetadataHandle readMetadataHandle = new DocumentMetadataHandle(); + + // read metadata + readMetadataHandle = readMetadataFromDocument(client, "/write-json-outputstreamhandle-metadata/" + filename, "JSON"); + + // get metadata values + DocumentProperties properties = readMetadataHandle.getProperties(); + + // Properties + String expectedProperties = "size:1|{http://www.example.com}foo:bar|"; + String actualProperties = getDocumentPropertiesString(properties); + System.out.println(actualProperties); + assertEquals("Document properties difference", expectedProperties, actualProperties); + + // release the client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestMetadataXML.java b/test-complete/src/test/java/com/marklogic/javaclient/TestMetadataXML.java index 776822266..db65f924e 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestMetadataXML.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestMetadataXML.java @@ -1,175 +1,175 @@ -package com.marklogic.javaclient; - -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.io.DOMHandle; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; -import static org.junit.Assert.*; - -import org.junit.*; -public class TestMetadataXML extends BasicJavaClientREST { - - private static String dbName = "TestMetadataXMLDB"; - private static String [] fNames = {"TestMetadataXMLDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - @BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - } - - - @Test - public void testMetadataXMLCRUD() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testMetadataXMLCRUD"); - - String filename = "Simple_ScanTe.png"; - String uri = "/write-bin-metadata/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc manager - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - - // get the original metadata - Document docMetadata = getXMLMetadata("metadata-original.xml"); - - // WRITE - // write the doc - writeDocumentUsingBytesHandle(client, filename, uri, "Binary"); - - // create handle to write metadata - DOMHandle writeMetadataHandle = new DOMHandle(); - writeMetadataHandle.set(docMetadata); - - // create doc id - String docId = uri + filename; - - // write original metadata - docMgr.writeMetadata(docId, writeMetadataHandle); - - // create handle to read metadata - DOMHandle readMetadataHandle = new DOMHandle(); - - // READ - // read metadata - docMgr.readMetadata(docId, readMetadataHandle); - Document docReadMetadata = readMetadataHandle.get(); - - assertXpathEvaluatesTo("coll1", "string(//*[local-name()='collection'][1])", docReadMetadata); - assertXpathEvaluatesTo("coll2", "string(//*[local-name()='collection'][2])", docReadMetadata); - assertXpathEvaluatesTo("MarkLogic", "string(//*[local-name()='Author'])", docReadMetadata); - - // UPDATE - // get the update metadata - Document docMetadataUpdate = getXMLMetadata("metadata-updated.xml"); - - // create handle for metadata update - DOMHandle writeMetadataHandleUpdate = new DOMHandle(); - writeMetadataHandleUpdate.set(docMetadataUpdate); - - // write updated metadata - docMgr.writeMetadata(docId, writeMetadataHandleUpdate); - - // create handle to read updated metadata - DOMHandle readMetadataHandleUpdate = new DOMHandle(); - - // read updated metadata - docMgr.readMetadata(docId, readMetadataHandleUpdate); - Document docReadMetadataUpdate = readMetadataHandleUpdate.get(); - - assertXpathEvaluatesTo("coll1", "string(//*[local-name()='collection'][1])", docReadMetadataUpdate); - assertXpathEvaluatesTo("coll3", "string(//*[local-name()='collection'][2])", docReadMetadataUpdate); - assertXpathEvaluatesTo("23", "string(//*[local-name()='quality'])", docReadMetadataUpdate); - assertXpathEvaluatesTo("Aries", "string(//*[local-name()='Author'])", docReadMetadataUpdate); - - // DELETE - // write default metadata - docMgr.writeDefaultMetadata(docId); - - // create handle to read deleted metadata - DOMHandle readMetadataHandleDelete = new DOMHandle(); - - // read deleted metadata - docMgr.readMetadata(docId, readMetadataHandleDelete); - Document docReadMetadataDelete = readMetadataHandleDelete.get(); - - assertXpathEvaluatesTo("0", "string(//*[local-name()='quality'])", docReadMetadataDelete); - - // release the client - client.release(); - } - - - - @Test - public void testMetadataXMLNegative() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testMetadataXMLNegative"); - - String filename = "Simple_ScanTe.png"; - String uri = "/write-neg-metadata/"; - - // connect the client - DatabaseClient client1 = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write the doc - writeDocumentUsingBytesHandle(client1, filename, uri, "Binary"); - - // connect with another client to write metadata - DatabaseClient client2 = DatabaseClientFactory.newClient("localhost", 8011, "rest-reader", "x", Authentication.DIGEST); - - // create doc manager - XMLDocumentManager docMgr = client2.newXMLDocumentManager(); - - // get the original metadata - Document docMetadata = getXMLMetadata("metadata-original.xml"); - - // create handle to write metadata - DOMHandle writeMetadataHandle = new DOMHandle(); - writeMetadataHandle.set(docMetadata); - - // create doc id - String docId = uri + filename; - - String expectedException = "You do not have permission to this method and URL"; - String exception = ""; - - // write original metadata - try - { - docMgr.writeMetadata(docId, writeMetadataHandle); - } - catch (Exception e) { exception = e.toString(); } - - //assertEquals("Could write metadata with forbidden user", expectedException, exception); - - boolean exceptionIsThrown = exception.contains(expectedException); - assertTrue("Exception is not thrown", exceptionIsThrown); - - // release the clients - client1.release(); - client2.release(); - } -@AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } -} +package com.marklogic.javaclient; + +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.io.DOMHandle; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; +import static org.junit.Assert.*; + +import org.junit.*; +public class TestMetadataXML extends BasicJavaClientREST { + + private static String dbName = "TestMetadataXMLDB"; + private static String [] fNames = {"TestMetadataXMLDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + } + + @Test + public void testMetadataXMLCRUD() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testMetadataXMLCRUD"); + + String filename = "Simple_ScanTe.png"; + String uri = "/write-bin-metadata/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc manager + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + + // get the original metadata + Document docMetadata = getXMLMetadata("metadata-original.xml"); + + // WRITE + // write the doc + writeDocumentUsingBytesHandle(client, filename, uri, "Binary"); + + // create handle to write metadata + DOMHandle writeMetadataHandle = new DOMHandle(); + writeMetadataHandle.set(docMetadata); + + // create doc id + String docId = uri + filename; + + // write original metadata + docMgr.writeMetadata(docId, writeMetadataHandle); + + // create handle to read metadata + DOMHandle readMetadataHandle = new DOMHandle(); + + // READ + // read metadata + docMgr.readMetadata(docId, readMetadataHandle); + Document docReadMetadata = readMetadataHandle.get(); + + assertXpathEvaluatesTo("coll1", "string(//*[local-name()='collection'][1])", docReadMetadata); + assertXpathEvaluatesTo("coll2", "string(//*[local-name()='collection'][2])", docReadMetadata); + assertXpathEvaluatesTo("MarkLogic", "string(//*[local-name()='Author'])", docReadMetadata); + + // UPDATE + // get the update metadata + Document docMetadataUpdate = getXMLMetadata("metadata-updated.xml"); + + // create handle for metadata update + DOMHandle writeMetadataHandleUpdate = new DOMHandle(); + writeMetadataHandleUpdate.set(docMetadataUpdate); + + // write updated metadata + docMgr.writeMetadata(docId, writeMetadataHandleUpdate); + + // create handle to read updated metadata + DOMHandle readMetadataHandleUpdate = new DOMHandle(); + + // read updated metadata + docMgr.readMetadata(docId, readMetadataHandleUpdate); + Document docReadMetadataUpdate = readMetadataHandleUpdate.get(); + + assertXpathEvaluatesTo("coll1", "string(//*[local-name()='collection'][1])", docReadMetadataUpdate); + assertXpathEvaluatesTo("coll3", "string(//*[local-name()='collection'][2])", docReadMetadataUpdate); + assertXpathEvaluatesTo("23", "string(//*[local-name()='quality'])", docReadMetadataUpdate); + assertXpathEvaluatesTo("Aries", "string(//*[local-name()='Author'])", docReadMetadataUpdate); + + // DELETE + // write default metadata + docMgr.writeDefaultMetadata(docId); + + // create handle to read deleted metadata + DOMHandle readMetadataHandleDelete = new DOMHandle(); + + // read deleted metadata + docMgr.readMetadata(docId, readMetadataHandleDelete); + Document docReadMetadataDelete = readMetadataHandleDelete.get(); + + assertXpathEvaluatesTo("0", "string(//*[local-name()='quality'])", docReadMetadataDelete); + + // release the client + client.release(); + } + + + + @Test + public void testMetadataXMLNegative() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testMetadataXMLNegative"); + + String filename = "Simple_ScanTe.png"; + String uri = "/write-neg-metadata/"; + + // connect the client + DatabaseClient client1 = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write the doc + writeDocumentUsingBytesHandle(client1, filename, uri, "Binary"); + + // connect with another client to write metadata + DatabaseClient client2 = DatabaseClientFactory.newClient("localhost", 8011, "rest-reader", "x", Authentication.DIGEST); + + // create doc manager + XMLDocumentManager docMgr = client2.newXMLDocumentManager(); + + // get the original metadata + Document docMetadata = getXMLMetadata("metadata-original.xml"); + + // create handle to write metadata + DOMHandle writeMetadataHandle = new DOMHandle(); + writeMetadataHandle.set(docMetadata); + + // create doc id + String docId = uri + filename; + + String expectedException = "You do not have permission to this method and URL"; + String exception = ""; + + // write original metadata + try + { + docMgr.writeMetadata(docId, writeMetadataHandle); + } + catch (Exception e) { exception = e.toString(); } + + //assertEquals("Could write metadata with forbidden user", expectedException, exception); + + boolean exceptionIsThrown = exception.contains(expectedException); + assertTrue("Exception is not thrown", exceptionIsThrown); + + // release the clients + client1.release(); + client2.release(); + } + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestMultithreading.java b/test-complete/src/test/java/com/marklogic/javaclient/TestMultithreading.java index 51974c2b2..effdf41c8 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestMultithreading.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestMultithreading.java @@ -1,136 +1,134 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.document.TextDocumentManager; - -import org.junit.*; -public class TestMultithreading extends BasicJavaClientREST { - - private static String dbName = "TestMultithreadingDB"; - private static String [] fNames = {"TestMultithreadingDBDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort=8011; - -@BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName, restPort); - } - -@After - public void testCleanUp() throws Exception - { - clearDB(restPort); - System.out.println("Running clear script"); - } - -@SuppressWarnings("deprecation") -@Test - public void testMultithreading() throws InterruptedException - { - ThreadClass dt1 = new ThreadClass("Thread A"); - ThreadClass dt2 = new ThreadClass("Thread B"); - - dt1.start(); // this will start thread of object 1 - dt2.start(); // this will start thread of object 2 - dt2.join(); - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-reader", "x", Authentication.DIGEST); - TextDocumentManager docMgr = client.newTextDocumentManager(); - - for (int i = 1; i <= 5; i++) - { - String expectedUri = "/multithread-content-A/filename" + i + ".txt"; - String docUri = docMgr.exists("/multithread-content-A/filename" + i + ".txt").getUri(); - assertEquals("URI is not found", expectedUri, docUri); - } - - for (int i = 1; i <= 5; i++) - { - String expectedUri = "/multithread-content-B/filename" + i + ".txt"; - String docUri = docMgr.exists("/multithread-content-B/filename" + i + ".txt").getUri(); - assertEquals("URI is not found", expectedUri, docUri); - } - - // release client - client.release(); - } - - /*public void testMultithreadingSearchAndWrite() throws InterruptedException - { - System.out.println("testMultithreadingSearchAndWrite"); - - ThreadWrite tw1 = new ThreadWrite("Write Thread"); - ThreadSearch ts1 = new ThreadSearch("Search Thread"); - - tw1.start(); - ts1.start(); - tw1.join(); - ts1.join(); - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - TextDocumentManager docMgr = client.newTextDocumentManager(); - - for (int i = 1; i <= 15; i++) - { - String expectedUri = "/multithread-write/filename" + i + ".xml"; - String docUri = docMgr.exists("/multithread-write/filename" + i + ".xml").getUri(); - assertEquals("URI is not found", expectedUri, docUri); - } - - for(int x = 0; x <= 9; x++) - { - System.out.println(ts1.totalResultsArray[x]); - assertTrue("Search result is 0", ts1.totalResultsArray[x] != 0); - } - - // release client - client.release(); - }*/ - -@SuppressWarnings("deprecation") -@Test - public void testMultithreadingMultipleSearch() throws InterruptedException - { - System.out.println("testMultithreadingMultipleSearch"); - - ThreadWrite tw1 = new ThreadWrite("Write Thread"); - tw1.start(); - tw1.join(); - - ThreadSearch ts1 = new ThreadSearch("Search Thread 1"); - ThreadSearch ts2 = new ThreadSearch("Search Thread 2"); - ThreadSearch ts3 = new ThreadSearch("Search Thread 3"); - ThreadSearch ts4 = new ThreadSearch("Search Thread 4"); - ThreadSearch ts5 = new ThreadSearch("Search Thread 5"); - - ts1.start(); - ts2.start(); - ts3.start(); - ts4.start(); - ts5.start(); - - ts1.join(); - ts2.join(); - ts3.join(); - ts4.join(); - ts5.join(); - - long totalAllDocumentsReturned = ts1.totalAllResults + ts2.totalAllResults + ts3.totalAllResults + ts4.totalAllResults + ts5.totalAllResults; - assertTrue("Documents count is incorrect", totalAllDocumentsReturned == 750); - } - @AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } - -} - - +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.TextDocumentManager; + +import org.junit.*; +public class TestMultithreading extends BasicJavaClientREST { + + private static String dbName = "TestMultithreadingDB"; + private static String [] fNames = {"TestMultithreadingDBDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort=8011; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName, restPort); + } + + @After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + + @Test + public void testMultithreading() throws InterruptedException + { + ThreadClass dt1 = new ThreadClass("Thread A"); + ThreadClass dt2 = new ThreadClass("Thread B"); + + dt1.start(); // this will start thread of object 1 + dt2.start(); // this will start thread of object 2 + dt2.join(); + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-reader", "x", Authentication.DIGEST); + TextDocumentManager docMgr = client.newTextDocumentManager(); + + for (int i = 1; i <= 5; i++) + { + String expectedUri = "/multithread-content-A/filename" + i + ".txt"; + String docUri = docMgr.exists("/multithread-content-A/filename" + i + ".txt").getUri(); + assertEquals("URI is not found", expectedUri, docUri); + } + + for (int i = 1; i <= 5; i++) + { + String expectedUri = "/multithread-content-B/filename" + i + ".txt"; + String docUri = docMgr.exists("/multithread-content-B/filename" + i + ".txt").getUri(); + assertEquals("URI is not found", expectedUri, docUri); + } + + // release client + client.release(); + } + + /*public void testMultithreadingSearchAndWrite() throws InterruptedException + { + System.out.println("testMultithreadingSearchAndWrite"); + + ThreadWrite tw1 = new ThreadWrite("Write Thread"); + ThreadSearch ts1 = new ThreadSearch("Search Thread"); + + tw1.start(); + ts1.start(); + tw1.join(); + ts1.join(); + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + TextDocumentManager docMgr = client.newTextDocumentManager(); + + for (int i = 1; i <= 15; i++) + { + String expectedUri = "/multithread-write/filename" + i + ".xml"; + String docUri = docMgr.exists("/multithread-write/filename" + i + ".xml").getUri(); + assertEquals("URI is not found", expectedUri, docUri); + } + + for(int x = 0; x <= 9; x++) + { + System.out.println(ts1.totalResultsArray[x]); + assertTrue("Search result is 0", ts1.totalResultsArray[x] != 0); + } + + // release client + client.release(); + }*/ + + @Test + public void testMultithreadingMultipleSearch() throws InterruptedException + { + System.out.println("testMultithreadingMultipleSearch"); + + ThreadWrite tw1 = new ThreadWrite("Write Thread"); + tw1.start(); + tw1.join(); + + ThreadSearch ts1 = new ThreadSearch("Search Thread 1"); + ThreadSearch ts2 = new ThreadSearch("Search Thread 2"); + ThreadSearch ts3 = new ThreadSearch("Search Thread 3"); + ThreadSearch ts4 = new ThreadSearch("Search Thread 4"); + ThreadSearch ts5 = new ThreadSearch("Search Thread 5"); + + ts1.start(); + ts2.start(); + ts3.start(); + ts4.start(); + ts5.start(); + + ts1.join(); + ts2.join(); + ts3.join(); + ts4.join(); + ts5.join(); + + long totalAllDocumentsReturned = ts1.totalAllResults + ts2.totalAllResults + ts3.totalAllResults + ts4.totalAllResults + ts5.totalAllResults; + assertTrue("Documents count is incorrect", totalAllDocumentsReturned == 750); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} + + diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestNamespaces.java b/test-complete/src/test/java/com/marklogic/javaclient/TestNamespaces.java index ac22d7c86..3f2656bed 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestNamespaces.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestNamespaces.java @@ -1,166 +1,164 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.IOException; -import java.util.Collection; - -import javax.xml.namespace.NamespaceContext; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.admin.NamespacesManager; -import com.marklogic.client.document.DocumentPatchBuilder; -import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.io.StringHandle; -import com.marklogic.client.util.EditableNamespaceContext; -import com.marklogic.client.util.RequestLogger; -import org.junit.*; -public class TestNamespaces extends BasicJavaClientREST { - - private static String dbName = "TestNamespacesDB"; - private static String [] fNames = {"TestNamespacesDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; -@BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - } - -@SuppressWarnings("deprecation") -@Test - public void testNamespaces() - { - System.out.println("Running testNamespaces"); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create namespaces manager - NamespacesManager nsMgr = client.newServerConfigManager().newNamespacesManager(); - - // create logger - RequestLogger logger = client.newLogger(System.out); - logger.setContentMax(RequestLogger.ALL_CONTENT); - - // start logging - nsMgr.startLogging(logger); - - // add prefix - nsMgr.addPrefix("foo", "http://example.com"); - - NamespaceContext nsContext = nsMgr.readAll(); - - assertEquals("Prefix is not equal", "foo", nsContext.getPrefix("http://example.com")); - assertEquals("Namespace URI is not equal", "http://example.com", nsContext.getNamespaceURI("foo")); - - // update prefix - nsMgr.updatePrefix("foo", "http://exampleupdated.com"); - nsContext = nsMgr.readAll(); - assertEquals("Updated Namespace URI is not equal", "http://exampleupdated.com", nsContext.getNamespaceURI("foo")); - - // stop logging - nsMgr.stopLogging(); - - String expectedLogContentMax = "9223372036854775807"; - assertEquals("Content log is not equal", expectedLogContentMax, Long.toString(logger.getContentMax())); - - // delete prefix - nsMgr.deletePrefix("foo"); - assertTrue("Namespace URI is not deleted", nsMgr.readPrefix("foo") == null); - - nsMgr.deleteAll(); - assertTrue("Namespace URI is not deleted", nsMgr.readPrefix("foo") == null); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testDefaultNamespaces() - { - System.out.println("Running testDefaultNamespaces"); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create namespaces manager - NamespacesManager nsMgr = client.newServerConfigManager().newNamespacesManager(); - - // add namespaces - nsMgr.addPrefix("ns1", "http://foo.com"); - nsMgr.addPrefix("ns2", "http://bar.com"); - nsMgr.addPrefix("ns3", "http://baz.com"); - - NamespaceContext context = nsMgr.readAll(); - - // set default namespace - nsMgr.updatePrefix("defaultns", "http://baz.com"); - String defaultNsUri = nsMgr.readPrefix("defaultns"); - assertEquals("Default NS is wrong", "http://baz.com", defaultNsUri); - - // delete namespace - nsMgr.deletePrefix("baz"); - context = nsMgr.readAll(); - - // get default namespace - assertEquals("Default NS is wrong", "http://baz.com", nsMgr.readPrefix("defaultns")); - - nsMgr.deleteAll(); - context = nsMgr.readAll(); - assertTrue("Namespace URI is not deleted", nsMgr.readPrefix("ns1") == null); - assertTrue("Namespace URI is not deleted", nsMgr.readPrefix("ns2") == null); - assertTrue("Namespace URI is not deleted", nsMgr.readPrefix("ns3") == null); - assertTrue("Namespace URI is not deleted", nsMgr.readPrefix("defaultns") == null); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testBug22396() throws IOException { - - System.out.println("Runing testBug22396"); - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - writeDocumentUsingInputStreamHandle(client, "constraint1.xml", "/testBug22396/", "XML"); - - String docId = "/testBug22396/constraint1.xml"; - - //create document manager - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); - - //get namespaces - Collection nameSpaceCollection = patchBldr.getNamespaces().getAllPrefixes(); - assertEquals("getNamespace failed ",false, nameSpaceCollection.isEmpty()); - for(String prefix : nameSpaceCollection){ - System.out.println("Prefixes : "+prefix); - System.out.println(patchBldr.getNamespaces().getNamespaceURI(prefix)); - } - //set namespace - EditableNamespaceContext namespaces = new EditableNamespaceContext(); - namespaces.put("new", "http://www.marklogic.com"); - patchBldr.setNamespaces(namespaces); - System.out.println("\n Namespace Output : "+patchBldr.getNamespaces().getNamespaceURI("xmlns")+"\n Next xml : "+patchBldr.getNamespaces().getNamespaceURI("xml")+"\n Next xs : "+patchBldr.getNamespaces().getNamespaceURI("xs")+"\n Next xsi : "+patchBldr.getNamespaces().getNamespaceURI("xsi")+"\n Next rapi : "+patchBldr.getNamespaces().getNamespaceURI("rapi")+"\n Next new : "+patchBldr.getNamespaces().getNamespaceURI("new")); - String content = docMgr.read(docId, new StringHandle()).get(); - assertTrue("setNamespace didn't worked", patchBldr.getNamespaces().getNamespaceURI("new").contains("www.marklogic.com")); - System.out.println(content); - - // release client - client.release(); - } -@AfterClass -public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.IOException; +import java.util.Collection; + +import javax.xml.namespace.NamespaceContext; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.admin.NamespacesManager; +import com.marklogic.client.document.DocumentPatchBuilder; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.io.StringHandle; +import com.marklogic.client.util.EditableNamespaceContext; +import com.marklogic.client.util.RequestLogger; +import org.junit.*; + +public class TestNamespaces extends BasicJavaClientREST { + private static String dbName = "TestNamespacesDB"; + private static String [] fNames = {"TestNamespacesDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + } + + @Test + public void testNamespaces() + { + System.out.println("Running testNamespaces"); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create namespaces manager + NamespacesManager nsMgr = client.newServerConfigManager().newNamespacesManager(); + + // create logger + RequestLogger logger = client.newLogger(System.out); + logger.setContentMax(RequestLogger.ALL_CONTENT); + + // start logging + nsMgr.startLogging(logger); + + // add prefix + nsMgr.addPrefix("foo", "http://example.com"); + + NamespaceContext nsContext = nsMgr.readAll(); + + assertEquals("Prefix is not equal", "foo", nsContext.getPrefix("http://example.com")); + assertEquals("Namespace URI is not equal", "http://example.com", nsContext.getNamespaceURI("foo")); + + // update prefix + nsMgr.updatePrefix("foo", "http://exampleupdated.com"); + nsContext = nsMgr.readAll(); + assertEquals("Updated Namespace URI is not equal", "http://exampleupdated.com", nsContext.getNamespaceURI("foo")); + + // stop logging + nsMgr.stopLogging(); + + String expectedLogContentMax = "9223372036854775807"; + assertEquals("Content log is not equal", expectedLogContentMax, Long.toString(logger.getContentMax())); + + // delete prefix + nsMgr.deletePrefix("foo"); + assertTrue("Namespace URI is not deleted", nsMgr.readPrefix("foo") == null); + + nsMgr.deleteAll(); + assertTrue("Namespace URI is not deleted", nsMgr.readPrefix("foo") == null); + + // release client + client.release(); + } + + @Test + public void testDefaultNamespaces() + { + System.out.println("Running testDefaultNamespaces"); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create namespaces manager + NamespacesManager nsMgr = client.newServerConfigManager().newNamespacesManager(); + + // add namespaces + nsMgr.addPrefix("ns1", "http://foo.com"); + nsMgr.addPrefix("ns2", "http://bar.com"); + nsMgr.addPrefix("ns3", "http://baz.com"); + + nsMgr.readAll(); + + // set default namespace + nsMgr.updatePrefix("defaultns", "http://baz.com"); + String defaultNsUri = nsMgr.readPrefix("defaultns"); + assertEquals("Default NS is wrong", "http://baz.com", defaultNsUri); + + // delete namespace + nsMgr.deletePrefix("baz"); + nsMgr.readAll(); + + // get default namespace + assertEquals("Default NS is wrong", "http://baz.com", nsMgr.readPrefix("defaultns")); + + nsMgr.deleteAll(); + nsMgr.readAll(); + assertTrue("Namespace URI is not deleted", nsMgr.readPrefix("ns1") == null); + assertTrue("Namespace URI is not deleted", nsMgr.readPrefix("ns2") == null); + assertTrue("Namespace URI is not deleted", nsMgr.readPrefix("ns3") == null); + assertTrue("Namespace URI is not deleted", nsMgr.readPrefix("defaultns") == null); + + // release client + client.release(); + } + + @Test + public void testBug22396() throws IOException { + + System.out.println("Runing testBug22396"); + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + writeDocumentUsingInputStreamHandle(client, "constraint1.xml", "/testBug22396/", "XML"); + + String docId = "/testBug22396/constraint1.xml"; + + //create document manager + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); + + //get namespaces + Collection nameSpaceCollection = patchBldr.getNamespaces().getAllPrefixes(); + assertEquals("getNamespace failed ",false, nameSpaceCollection.isEmpty()); + for(String prefix : nameSpaceCollection){ + System.out.println("Prefixes : "+prefix); + System.out.println(patchBldr.getNamespaces().getNamespaceURI(prefix)); + } + //set namespace + EditableNamespaceContext namespaces = new EditableNamespaceContext(); + namespaces.put("new", "http://www.marklogic.com"); + patchBldr.setNamespaces(namespaces); + System.out.println("\n Namespace Output : "+patchBldr.getNamespaces().getNamespaceURI("xmlns")+"\n Next xml : "+patchBldr.getNamespaces().getNamespaceURI("xml")+"\n Next xs : "+patchBldr.getNamespaces().getNamespaceURI("xs")+"\n Next xsi : "+patchBldr.getNamespaces().getNamespaceURI("xsi")+"\n Next rapi : "+patchBldr.getNamespaces().getNamespaceURI("rapi")+"\n Next new : "+patchBldr.getNamespaces().getNamespaceURI("new")); + String content = docMgr.read(docId, new StringHandle()).get(); + assertTrue("setNamespace didn't worked", patchBldr.getNamespaces().getNamespaceURI("new").contains("www.marklogic.com")); + System.out.println(content); + + // release client + client.release(); + } + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestOptimisticLocking.java b/test-complete/src/test/java/com/marklogic/javaclient/TestOptimisticLocking.java index 45a29ea4e..9ac8f2204 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestOptimisticLocking.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestOptimisticLocking.java @@ -1,656 +1,655 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.File; -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.document.DocumentDescriptor; -import com.marklogic.client.FailedRequestException; -import com.marklogic.client.document.JSONDocumentManager; -import com.marklogic.client.ResourceNotFoundException; -import com.marklogic.client.admin.ServerConfigurationManager; -import com.marklogic.client.admin.ServerConfigurationManager.Policy; -import com.marklogic.client.io.FileHandle; -import com.marklogic.client.io.StringHandle; -import com.marklogic.client.document.XMLDocumentManager; - -import org.junit.*; -public class TestOptimisticLocking extends BasicJavaClientREST{ - - private static String dbName = "TestOptimisticLockingDB"; - private static String [] fNames = {"TestOptimisticLockingDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort=8011; - - @BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - } - - @After - public void testCleanUp() throws Exception - { - clearDB(restPort); - System.out.println("Running clear script"); - } - - @SuppressWarnings("deprecation") - @Test - public void testRequired() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testRequired"); - - String filename = "xml-original.xml"; - String updateFilename = "xml-updated.xml"; - String uri = "/optimistic-locking/"; - String docId = uri + filename; - long badVersion = 1111; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create a manager for the server configuration - ServerConfigurationManager configMgr = client.newServerConfigManager(); - - // read the server configuration from the database - configMgr.readConfiguration(); - - // require content versions for updates and deletes - // use Policy.OPTIONAL to allow but not require versions - configMgr.setContentVersionRequests(Policy.REQUIRED); - - // write the server configuration to the database - configMgr.writeConfiguration(); - - System.out.println("set optimistic locking to required"); - - // create document manager - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - // create document descriptor - DocumentDescriptor desc = docMgr.newDescriptor(docId); - - desc.setVersion(badVersion); - - String exception = ""; - String expectedException = "com.marklogic.client.FailedRequestException: Local message: Content version must match to write document. Server Message: RESTAPI-CONTENTWRONGVERSION"; - - // CREATE - // write document with bad version - try - { - docMgr.write(desc, handle); - } catch (FailedRequestException e) { exception = e.toString(); } - - boolean isExceptionThrown = exception.contains(expectedException); - assertTrue("Exception is not thrown", isExceptionThrown); - - // write document with unknown version - desc.setVersion(DocumentDescriptor.UNKNOWN_VERSION); - docMgr.write(desc, handle); - - StringHandle readHandle = new StringHandle(); - docMgr.read(desc, readHandle); - String content = readHandle.get(); - assertTrue("Wrong content", content.contains("noodle")); - - // get the good version - long goodVersion = desc.getVersion(); - - System.out.println("version before create: " + goodVersion); - - // UPDATE - File updateFile = new File("src/test/java/com/marklogic/javaclient/data/" + updateFilename); - - // create a handle on the content - FileHandle updateHandle = new FileHandle(updateFile); - updateHandle.set(updateFile); - - // update with bad version - desc.setVersion(badVersion); - - String updateException = ""; - String expectedUpdateException = "com.marklogic.client.FailedRequestException: Local message: Content version must match to write document. Server Message: RESTAPI-CONTENTWRONGVERSION"; - - try - { - docMgr.write(desc, updateHandle); - } catch (FailedRequestException e) { updateException = e.toString(); } - - boolean isUpdateExceptionThrown = updateException.contains(expectedUpdateException); - assertTrue("Exception is not thrown", isUpdateExceptionThrown); - - // update with unknown version - desc.setVersion(DocumentDescriptor.UNKNOWN_VERSION); - - String updateUnknownException = ""; - String expectedUpdateUnknownException = "com.marklogic.client.FailedRequestException: Local message: Content version required to write document. Server Message: You do not have permission to this method and URL"; - - try - { - docMgr.write(desc, updateHandle); - } catch (FailedRequestException e) { updateUnknownException = e.toString(); } - - boolean isUpdateUnknownExceptionThrown = updateUnknownException.contains(expectedUpdateUnknownException); - assertTrue("Exception is not thrown", isUpdateUnknownExceptionThrown); - - desc = docMgr.exists(docId); - goodVersion = desc.getVersion(); - - System.out.println("version before update: " + goodVersion); - - // update with good version - desc.setVersion(goodVersion); - docMgr.write(desc, updateHandle); - - StringHandle updateReadHandle = new StringHandle(); - docMgr.read(desc, updateReadHandle); - String updateContent = updateReadHandle.get(); - assertTrue("Wrong content", updateContent.contains("fried noodle")); - - // DELETE - // delete using bad version - desc.setVersion(badVersion); - - String deleteException = ""; - String expectedDeleteException = "com.marklogic.client.FailedRequestException: Local message: Content version must match to delete document"; - - try - { - docMgr.delete(desc); - } catch (FailedRequestException e) { deleteException = e.toString(); } - - boolean isDeleteExceptionThrown = deleteException.contains(expectedDeleteException); - assertTrue("Exception is not thrown", isDeleteExceptionThrown); - - // delete using unknown version - desc.setVersion(DocumentDescriptor.UNKNOWN_VERSION); - - String deleteUnknownException = ""; - String expectedDeleteUnknownException = "com.marklogic.client.FailedRequestException: Local message: Content version required to delete document. Server Message: You do not have permission to this method and URL"; - - try - { - docMgr.delete(desc); - } catch (FailedRequestException e) { deleteUnknownException = e.toString(); } - - boolean isDeleteUnknownExceptionThrown = deleteUnknownException.contains(expectedDeleteUnknownException); - assertTrue("Exception is not thrown", isDeleteUnknownExceptionThrown); - - // delete using good version - desc = docMgr.exists(docId); - goodVersion = desc.getVersion(); - - System.out.println("version before delete: " + goodVersion); - - docMgr.delete(desc); - - String verifyDeleteException = ""; - String expectedVerifyDeleteException = "com.marklogic.client.ResourceNotFoundException: Local message: Could not read non-existent document"; - - StringHandle deleteHandle = new StringHandle(); - try - { - docMgr.read(desc, deleteHandle); - } catch (ResourceNotFoundException e) { verifyDeleteException = e.toString(); } - - boolean isVerifyDeleteExceptionThrown = verifyDeleteException.contains(expectedVerifyDeleteException); - assertTrue("Exception is not thrown", isVerifyDeleteExceptionThrown); - - // release client - client.release(); - - try { - Thread.sleep(30000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - System.out.println(configMgr.getContentVersionRequests()); - - } - - @SuppressWarnings("deprecation") - @Test - public void testOptionalWithUnknownVersion() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testOptionalWithUnknownVersion"); - - String filename = "json-original.json"; - String updateFilename = "json-updated.json"; - String uri = "/optimistic-locking/"; - String docId = uri + filename; - long badVersion = 1111; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create a manager for the server configuration - ServerConfigurationManager configMgr = client.newServerConfigManager(); - - // read the server configuration from the database - configMgr.readConfiguration(); - - // use Policy.OPTIONAL to allow but not require versions - configMgr.setContentVersionRequests(Policy.OPTIONAL); - - // write the server configuration to the database - configMgr.writeConfiguration(); - - System.out.println("set optimistic locking to optional"); - - // create document manager - JSONDocumentManager docMgr = client.newJSONDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - // create document descriptor - DocumentDescriptor desc = docMgr.newDescriptor(docId); - - desc.setVersion(badVersion); - - String exception = ""; - String expectedException = "com.marklogic.client.FailedRequestException: Local message: Content version must match to write document. Server Message: RESTAPI-CONTENTWRONGVERSION"; - - // CREATE - // write document with bad version - try - { - docMgr.write(desc, handle); - } catch (FailedRequestException e) { exception = e.toString(); } - - boolean isExceptionThrown = exception.contains(expectedException); - assertTrue("Exception is not thrown", isExceptionThrown); - - // write document with unknown version - desc.setVersion(DocumentDescriptor.UNKNOWN_VERSION); - docMgr.write(desc, handle); - - StringHandle readHandle = new StringHandle(); - docMgr.read(desc, readHandle); - String content = readHandle.get(); - assertTrue("Wrong content", content.contains("John")); - - // get the unknown version - long unknownVersion = desc.getVersion(); - - System.out.println("unknown version after create: " + unknownVersion); - - // UPDATE - File updateFile = new File("src/test/java/com/marklogic/javaclient/data/" + updateFilename); - - // create a handle on the content - FileHandle updateHandle = new FileHandle(updateFile); - updateHandle.set(updateFile); - - // update with bad version - desc.setVersion(badVersion); - - String updateException = ""; - String expectedUpdateException = "com.marklogic.client.FailedRequestException: Local message: Content version must match to write document. Server Message: RESTAPI-CONTENTWRONGVERSION"; - - try - { - docMgr.write(desc, updateHandle); - } catch (FailedRequestException e) { updateException = e.toString(); } - - boolean isUpdateExceptionThrown = updateException.contains(expectedUpdateException); - assertTrue("Exception is not thrown", isUpdateExceptionThrown); - - // update with unknown version - desc.setVersion(DocumentDescriptor.UNKNOWN_VERSION); - - docMgr.write(desc, updateHandle); - - StringHandle updateReadHandle = new StringHandle(); - docMgr.read(desc, updateReadHandle); - String updateContent = updateReadHandle.get(); - assertTrue("Wrong content", updateContent.contains("Aries")); - - unknownVersion = desc.getVersion(); - - System.out.println("unknown version after update: " + unknownVersion); - - // read using matched version - desc.setVersion(unknownVersion); - StringHandle readMatchHandle = new StringHandle(); - docMgr.read(desc, readMatchHandle); - String readMatchContent = readMatchHandle.get(); - assertTrue("Document does not return null", readMatchContent == null); - - // DELETE - // delete using bad version - desc.setVersion(badVersion); - - String deleteException = ""; - String expectedDeleteException = "com.marklogic.client.FailedRequestException: Local message: Content version must match to delete document"; - - try - { - docMgr.delete(desc); - } catch (FailedRequestException e) { deleteException = e.toString(); } - - boolean isDeleteExceptionThrown = deleteException.contains(expectedDeleteException); - assertTrue("Exception is not thrown", isDeleteExceptionThrown); - - // delete using unknown version - desc.setVersion(DocumentDescriptor.UNKNOWN_VERSION); - docMgr.delete(desc); - - unknownVersion = desc.getVersion(); - - System.out.println("unknown version after delete: " + unknownVersion); - - String verifyDeleteException = ""; - String expectedVerifyDeleteException = "com.marklogic.client.ResourceNotFoundException: Local message: Could not read non-existent document"; - - StringHandle deleteHandle = new StringHandle(); - try - { - docMgr.read(desc, deleteHandle); - } catch (ResourceNotFoundException e) { verifyDeleteException = e.toString(); } - - boolean isVerifyDeleteExceptionThrown = verifyDeleteException.contains(expectedVerifyDeleteException); - assertTrue("Exception is not thrown", isVerifyDeleteExceptionThrown); - - // release client - client.release(); - - try { - Thread.sleep(30000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - System.out.println(configMgr.getContentVersionRequests()); - - } - - @SuppressWarnings("deprecation") - @Test - public void testOptionalWithGoodVersion() throws IOException, ParserConfigurationException, SAXException, XpathException - { -System.out.println("Running testOptionalWithGoodVersion"); - - String filename = "json-original.json"; - String updateFilename = "json-updated.json"; - String uri = "/optimistic-locking/"; - String docId = uri + filename; - long badVersion = 1111; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create a manager for the server configuration - ServerConfigurationManager configMgr = client.newServerConfigManager(); - - // read the server configuration from the database - configMgr.readConfiguration(); - - // use Policy.OPTIONAL to allow but not require versions - configMgr.setContentVersionRequests(Policy.OPTIONAL); - - // write the server configuration to the database - configMgr.writeConfiguration(); - - System.out.println("set optimistic locking to optional"); - - // create document manager - JSONDocumentManager docMgr = client.newJSONDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - // create document descriptor - DocumentDescriptor desc = docMgr.newDescriptor(docId); - - desc.setVersion(badVersion); - - String exception = ""; - String expectedException = "com.marklogic.client.FailedRequestException: Local message: Content version must match to write document. Server Message: RESTAPI-CONTENTWRONGVERSION"; - - // CREATE - // write document with bad version - try - { - docMgr.write(desc, handle); - } catch (FailedRequestException e) { exception = e.toString(); } - - boolean isExceptionThrown = exception.contains(expectedException); - assertTrue("Exception is not thrown", isExceptionThrown); - - // write document with unknown version - desc.setVersion(DocumentDescriptor.UNKNOWN_VERSION); - docMgr.write(desc, handle); - - StringHandle readHandle = new StringHandle(); - docMgr.read(desc, readHandle); - String content = readHandle.get(); - assertTrue("Wrong content", content.contains("John")); - - // get the good version - long goodVersion = desc.getVersion(); - - System.out.println("good version after create: " + goodVersion); - - // UPDATE - File updateFile = new File("src/test/java/com/marklogic/javaclient/data/" + updateFilename); - - // create a handle on the content - FileHandle updateHandle = new FileHandle(updateFile); - updateHandle.set(updateFile); - - // update with bad version - desc.setVersion(badVersion); - - String updateException = ""; - String expectedUpdateException = "com.marklogic.client.FailedRequestException: Local message: Content version must match to write document. Server Message: RESTAPI-CONTENTWRONGVERSION"; - - try - { - docMgr.write(desc, updateHandle); - } catch (FailedRequestException e) { updateException = e.toString(); } - - boolean isUpdateExceptionThrown = updateException.contains(expectedUpdateException); - assertTrue("Exception is not thrown", isUpdateExceptionThrown); - - // update with good version - desc.setVersion(goodVersion); - - docMgr.write(desc, updateHandle); - - StringHandle updateReadHandle = new StringHandle(); - docMgr.read(desc, updateReadHandle); - String updateContent = updateReadHandle.get(); - assertTrue("Wrong content", updateContent.contains("Aries")); - - goodVersion = desc.getVersion(); - - System.out.println("good version after update: " + goodVersion); - - // read using matched version - desc.setVersion(goodVersion); - StringHandle readMatchHandle = new StringHandle(); - docMgr.read(desc, readMatchHandle); - String readMatchContent = readMatchHandle.get(); - assertTrue("Document does not return null", readMatchContent == null); - - // DELETE - // delete using bad version - desc.setVersion(badVersion); - - String deleteException = ""; - String expectedDeleteException = "com.marklogic.client.FailedRequestException: Local message: Content version must match to delete document"; - - try - { - docMgr.delete(desc); - } catch (FailedRequestException e) { deleteException = e.toString(); } - - boolean isDeleteExceptionThrown = deleteException.contains(expectedDeleteException); - assertTrue("Exception is not thrown", isDeleteExceptionThrown); - - // delete using good version - desc.setVersion(goodVersion); - docMgr.delete(desc); - - goodVersion = desc.getVersion(); - - System.out.println("unknown version after delete: " + goodVersion); - - String verifyDeleteException = ""; - String expectedVerifyDeleteException = "com.marklogic.client.ResourceNotFoundException: Local message: Could not read non-existent document"; - - StringHandle deleteHandle = new StringHandle(); - try - { - docMgr.read(desc, deleteHandle); - } catch (ResourceNotFoundException e) { verifyDeleteException = e.toString(); } - - boolean isVerifyDeleteExceptionThrown = verifyDeleteException.contains(expectedVerifyDeleteException); - assertTrue("Exception is not thrown", isVerifyDeleteExceptionThrown); - - // release client - client.release(); - - try { - Thread.sleep(30000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - System.out.println(configMgr.getContentVersionRequests()); - } - - - @SuppressWarnings("deprecation") - @Test public void testNone() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testNone"); - - String filename = "json-original.json"; - String updateFilename = "json-updated.json"; - String uri = "/optimistic-locking/"; - String docId = uri + filename; - long badVersion = 1111; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create a manager for the server configuration - ServerConfigurationManager configMgr = client.newServerConfigManager(); - - // read the server configuration from the database - configMgr.readConfiguration(); - - // use Policy.NONE - configMgr.setContentVersionRequests(Policy.NONE); - - // write the server configuration to the database - configMgr.writeConfiguration(); - - System.out.println("set optimistic locking to none"); - - // create document manager - JSONDocumentManager docMgr = client.newJSONDocumentManager(); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - // create document descriptor - DocumentDescriptor desc = docMgr.newDescriptor(docId); - - desc.setVersion(badVersion); - - // CREATE - // write document with bad version - - docMgr.write(desc, handle); - - StringHandle readHandle = new StringHandle(); - docMgr.read(desc, readHandle); - String content = readHandle.get(); - assertTrue("Wrong content", content.contains("John")); - - // UPDATE - File updateFile = new File("src/test/java/com/marklogic/javaclient/data/" + updateFilename); - - // create a handle on the content - FileHandle updateHandle = new FileHandle(updateFile); - updateHandle.set(updateFile); - - // update with bad version - desc.setVersion(badVersion); - - docMgr.write(desc, updateHandle); - - StringHandle updateReadHandle = new StringHandle(); - docMgr.read(desc, updateReadHandle); - String updateContent = updateReadHandle.get(); - assertTrue("Wrong content", updateContent.contains("Aries")); - - // DELETE - // delete using bad version - desc.setVersion(badVersion); - - docMgr.delete(desc); - - String verifyDeleteException = ""; - String expectedVerifyDeleteException = "com.marklogic.client.ResourceNotFoundException: Local message: Could not read non-existent document"; - - StringHandle deleteHandle = new StringHandle(); - try - { - docMgr.read(desc, deleteHandle); - } catch (ResourceNotFoundException e) { verifyDeleteException = e.toString(); } - - boolean isVerifyDeleteExceptionThrown = verifyDeleteException.contains(expectedVerifyDeleteException); - assertTrue("Exception is not thrown", isVerifyDeleteExceptionThrown); - - // release client - client.release(); - - try { - Thread.sleep(30000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - System.out.println(configMgr.getContentVersionRequests()); - } - @AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.DocumentDescriptor; +import com.marklogic.client.FailedRequestException; +import com.marklogic.client.document.JSONDocumentManager; +import com.marklogic.client.ResourceNotFoundException; +import com.marklogic.client.admin.ServerConfigurationManager; +import com.marklogic.client.admin.ServerConfigurationManager.Policy; +import com.marklogic.client.io.FileHandle; +import com.marklogic.client.io.StringHandle; +import com.marklogic.client.document.XMLDocumentManager; + +import org.junit.*; + +public class TestOptimisticLocking extends BasicJavaClientREST{ + private static String dbName = "TestOptimisticLockingDB"; + private static String [] fNames = {"TestOptimisticLockingDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort=8011; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + } + + @After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + + @Test + public void testRequired() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testRequired"); + + String filename = "xml-original.xml"; + String updateFilename = "xml-updated.xml"; + String uri = "/optimistic-locking/"; + String docId = uri + filename; + long badVersion = 1111; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create a manager for the server configuration + ServerConfigurationManager configMgr = client.newServerConfigManager(); + + // read the server configuration from the database + configMgr.readConfiguration(); + + // require content versions for updates and deletes + // use Policy.OPTIONAL to allow but not require versions + configMgr.setContentVersionRequests(Policy.REQUIRED); + + // write the server configuration to the database + configMgr.writeConfiguration(); + + System.out.println("set optimistic locking to required"); + + // create document manager + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + // create document descriptor + DocumentDescriptor desc = docMgr.newDescriptor(docId); + + desc.setVersion(badVersion); + + String exception = ""; + String expectedException = "com.marklogic.client.FailedRequestException: Local message: Content version must match to write document. Server Message: RESTAPI-CONTENTWRONGVERSION"; + + // CREATE + // write document with bad version + try + { + docMgr.write(desc, handle); + } catch (FailedRequestException e) { exception = e.toString(); } + + boolean isExceptionThrown = exception.contains(expectedException); + assertTrue("Exception is not thrown", isExceptionThrown); + + // write document with unknown version + desc.setVersion(DocumentDescriptor.UNKNOWN_VERSION); + docMgr.write(desc, handle); + + StringHandle readHandle = new StringHandle(); + docMgr.read(desc, readHandle); + String content = readHandle.get(); + assertTrue("Wrong content", content.contains("noodle")); + + // get the good version + long goodVersion = desc.getVersion(); + + System.out.println("version before create: " + goodVersion); + + // UPDATE + File updateFile = new File("src/test/java/com/marklogic/javaclient/data/" + updateFilename); + + // create a handle on the content + FileHandle updateHandle = new FileHandle(updateFile); + updateHandle.set(updateFile); + + // update with bad version + desc.setVersion(badVersion); + + String updateException = ""; + String expectedUpdateException = "com.marklogic.client.FailedRequestException: Local message: Content version must match to write document. Server Message: RESTAPI-CONTENTWRONGVERSION"; + + try + { + docMgr.write(desc, updateHandle); + } catch (FailedRequestException e) { updateException = e.toString(); } + + boolean isUpdateExceptionThrown = updateException.contains(expectedUpdateException); + assertTrue("Exception is not thrown", isUpdateExceptionThrown); + + // update with unknown version + desc.setVersion(DocumentDescriptor.UNKNOWN_VERSION); + + String updateUnknownException = ""; + String expectedUpdateUnknownException = "com.marklogic.client.FailedRequestException: Local message: Content version required to write document. Server Message: You do not have permission to this method and URL"; + + try + { + docMgr.write(desc, updateHandle); + } catch (FailedRequestException e) { updateUnknownException = e.toString(); } + + boolean isUpdateUnknownExceptionThrown = updateUnknownException.contains(expectedUpdateUnknownException); + assertTrue("Exception is not thrown", isUpdateUnknownExceptionThrown); + + desc = docMgr.exists(docId); + goodVersion = desc.getVersion(); + + System.out.println("version before update: " + goodVersion); + + // update with good version + desc.setVersion(goodVersion); + docMgr.write(desc, updateHandle); + + StringHandle updateReadHandle = new StringHandle(); + docMgr.read(desc, updateReadHandle); + String updateContent = updateReadHandle.get(); + assertTrue("Wrong content", updateContent.contains("fried noodle")); + + // DELETE + // delete using bad version + desc.setVersion(badVersion); + + String deleteException = ""; + String expectedDeleteException = "com.marklogic.client.FailedRequestException: Local message: Content version must match to delete document"; + + try + { + docMgr.delete(desc); + } catch (FailedRequestException e) { deleteException = e.toString(); } + + boolean isDeleteExceptionThrown = deleteException.contains(expectedDeleteException); + assertTrue("Exception is not thrown", isDeleteExceptionThrown); + + // delete using unknown version + desc.setVersion(DocumentDescriptor.UNKNOWN_VERSION); + + String deleteUnknownException = ""; + String expectedDeleteUnknownException = "com.marklogic.client.FailedRequestException: Local message: Content version required to delete document. Server Message: You do not have permission to this method and URL"; + + try + { + docMgr.delete(desc); + } catch (FailedRequestException e) { deleteUnknownException = e.toString(); } + + boolean isDeleteUnknownExceptionThrown = deleteUnknownException.contains(expectedDeleteUnknownException); + assertTrue("Exception is not thrown", isDeleteUnknownExceptionThrown); + + // delete using good version + desc = docMgr.exists(docId); + goodVersion = desc.getVersion(); + + System.out.println("version before delete: " + goodVersion); + + docMgr.delete(desc); + + String verifyDeleteException = ""; + String expectedVerifyDeleteException = "com.marklogic.client.ResourceNotFoundException: Local message: Could not read non-existent document"; + + StringHandle deleteHandle = new StringHandle(); + try + { + docMgr.read(desc, deleteHandle); + } catch (ResourceNotFoundException e) { verifyDeleteException = e.toString(); } + + boolean isVerifyDeleteExceptionThrown = verifyDeleteException.contains(expectedVerifyDeleteException); + assertTrue("Exception is not thrown", isVerifyDeleteExceptionThrown); + + // release client + client.release(); + + try { + Thread.sleep(30000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + System.out.println(configMgr.getContentVersionRequests()); + + } + + @SuppressWarnings("deprecation") + @Test + public void testOptionalWithUnknownVersion() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testOptionalWithUnknownVersion"); + + String filename = "json-original.json"; + String updateFilename = "json-updated.json"; + String uri = "/optimistic-locking/"; + String docId = uri + filename; + long badVersion = 1111; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create a manager for the server configuration + ServerConfigurationManager configMgr = client.newServerConfigManager(); + + // read the server configuration from the database + configMgr.readConfiguration(); + + // use Policy.OPTIONAL to allow but not require versions + configMgr.setContentVersionRequests(Policy.OPTIONAL); + + // write the server configuration to the database + configMgr.writeConfiguration(); + + System.out.println("set optimistic locking to optional"); + + // create document manager + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + // create document descriptor + DocumentDescriptor desc = docMgr.newDescriptor(docId); + + desc.setVersion(badVersion); + + String exception = ""; + String expectedException = "com.marklogic.client.FailedRequestException: Local message: Content version must match to write document. Server Message: RESTAPI-CONTENTWRONGVERSION"; + + // CREATE + // write document with bad version + try + { + docMgr.write(desc, handle); + } catch (FailedRequestException e) { exception = e.toString(); } + + boolean isExceptionThrown = exception.contains(expectedException); + assertTrue("Exception is not thrown", isExceptionThrown); + + // write document with unknown version + desc.setVersion(DocumentDescriptor.UNKNOWN_VERSION); + docMgr.write(desc, handle); + + StringHandle readHandle = new StringHandle(); + docMgr.read(desc, readHandle); + String content = readHandle.get(); + assertTrue("Wrong content", content.contains("John")); + + // get the unknown version + long unknownVersion = desc.getVersion(); + + System.out.println("unknown version after create: " + unknownVersion); + + // UPDATE + File updateFile = new File("src/test/java/com/marklogic/javaclient/data/" + updateFilename); + + // create a handle on the content + FileHandle updateHandle = new FileHandle(updateFile); + updateHandle.set(updateFile); + + // update with bad version + desc.setVersion(badVersion); + + String updateException = ""; + String expectedUpdateException = "com.marklogic.client.FailedRequestException: Local message: Content version must match to write document. Server Message: RESTAPI-CONTENTWRONGVERSION"; + + try + { + docMgr.write(desc, updateHandle); + } catch (FailedRequestException e) { updateException = e.toString(); } + + boolean isUpdateExceptionThrown = updateException.contains(expectedUpdateException); + assertTrue("Exception is not thrown", isUpdateExceptionThrown); + + // update with unknown version + desc.setVersion(DocumentDescriptor.UNKNOWN_VERSION); + + docMgr.write(desc, updateHandle); + + StringHandle updateReadHandle = new StringHandle(); + docMgr.read(desc, updateReadHandle); + String updateContent = updateReadHandle.get(); + assertTrue("Wrong content", updateContent.contains("Aries")); + + unknownVersion = desc.getVersion(); + + System.out.println("unknown version after update: " + unknownVersion); + + // read using matched version + desc.setVersion(unknownVersion); + StringHandle readMatchHandle = new StringHandle(); + docMgr.read(desc, readMatchHandle); + String readMatchContent = readMatchHandle.get(); + assertTrue("Document does not return null", readMatchContent == null); + + // DELETE + // delete using bad version + desc.setVersion(badVersion); + + String deleteException = ""; + String expectedDeleteException = "com.marklogic.client.FailedRequestException: Local message: Content version must match to delete document"; + + try + { + docMgr.delete(desc); + } catch (FailedRequestException e) { deleteException = e.toString(); } + + boolean isDeleteExceptionThrown = deleteException.contains(expectedDeleteException); + assertTrue("Exception is not thrown", isDeleteExceptionThrown); + + // delete using unknown version + desc.setVersion(DocumentDescriptor.UNKNOWN_VERSION); + docMgr.delete(desc); + + unknownVersion = desc.getVersion(); + + System.out.println("unknown version after delete: " + unknownVersion); + + String verifyDeleteException = ""; + String expectedVerifyDeleteException = "com.marklogic.client.ResourceNotFoundException: Local message: Could not read non-existent document"; + + StringHandle deleteHandle = new StringHandle(); + try + { + docMgr.read(desc, deleteHandle); + } catch (ResourceNotFoundException e) { verifyDeleteException = e.toString(); } + + boolean isVerifyDeleteExceptionThrown = verifyDeleteException.contains(expectedVerifyDeleteException); + assertTrue("Exception is not thrown", isVerifyDeleteExceptionThrown); + + // release client + client.release(); + + try { + Thread.sleep(30000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + System.out.println(configMgr.getContentVersionRequests()); + + } + + @SuppressWarnings("deprecation") + @Test + public void testOptionalWithGoodVersion() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testOptionalWithGoodVersion"); + + String filename = "json-original.json"; + String updateFilename = "json-updated.json"; + String uri = "/optimistic-locking/"; + String docId = uri + filename; + long badVersion = 1111; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create a manager for the server configuration + ServerConfigurationManager configMgr = client.newServerConfigManager(); + + // read the server configuration from the database + configMgr.readConfiguration(); + + // use Policy.OPTIONAL to allow but not require versions + configMgr.setContentVersionRequests(Policy.OPTIONAL); + + // write the server configuration to the database + configMgr.writeConfiguration(); + + System.out.println("set optimistic locking to optional"); + + // create document manager + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + // create document descriptor + DocumentDescriptor desc = docMgr.newDescriptor(docId); + + desc.setVersion(badVersion); + + String exception = ""; + String expectedException = "com.marklogic.client.FailedRequestException: Local message: Content version must match to write document. Server Message: RESTAPI-CONTENTWRONGVERSION"; + + // CREATE + // write document with bad version + try + { + docMgr.write(desc, handle); + } catch (FailedRequestException e) { exception = e.toString(); } + + boolean isExceptionThrown = exception.contains(expectedException); + assertTrue("Exception is not thrown", isExceptionThrown); + + // write document with unknown version + desc.setVersion(DocumentDescriptor.UNKNOWN_VERSION); + docMgr.write(desc, handle); + + StringHandle readHandle = new StringHandle(); + docMgr.read(desc, readHandle); + String content = readHandle.get(); + assertTrue("Wrong content", content.contains("John")); + + // get the good version + long goodVersion = desc.getVersion(); + + System.out.println("good version after create: " + goodVersion); + + // UPDATE + File updateFile = new File("src/test/java/com/marklogic/javaclient/data/" + updateFilename); + + // create a handle on the content + FileHandle updateHandle = new FileHandle(updateFile); + updateHandle.set(updateFile); + + // update with bad version + desc.setVersion(badVersion); + + String updateException = ""; + String expectedUpdateException = "com.marklogic.client.FailedRequestException: Local message: Content version must match to write document. Server Message: RESTAPI-CONTENTWRONGVERSION"; + + try + { + docMgr.write(desc, updateHandle); + } catch (FailedRequestException e) { updateException = e.toString(); } + + boolean isUpdateExceptionThrown = updateException.contains(expectedUpdateException); + assertTrue("Exception is not thrown", isUpdateExceptionThrown); + + // update with good version + desc.setVersion(goodVersion); + + docMgr.write(desc, updateHandle); + + StringHandle updateReadHandle = new StringHandle(); + docMgr.read(desc, updateReadHandle); + String updateContent = updateReadHandle.get(); + assertTrue("Wrong content", updateContent.contains("Aries")); + + goodVersion = desc.getVersion(); + + System.out.println("good version after update: " + goodVersion); + + // read using matched version + desc.setVersion(goodVersion); + StringHandle readMatchHandle = new StringHandle(); + docMgr.read(desc, readMatchHandle); + String readMatchContent = readMatchHandle.get(); + assertTrue("Document does not return null", readMatchContent == null); + + // DELETE + // delete using bad version + desc.setVersion(badVersion); + + String deleteException = ""; + String expectedDeleteException = "com.marklogic.client.FailedRequestException: Local message: Content version must match to delete document"; + + try + { + docMgr.delete(desc); + } catch (FailedRequestException e) { deleteException = e.toString(); } + + boolean isDeleteExceptionThrown = deleteException.contains(expectedDeleteException); + assertTrue("Exception is not thrown", isDeleteExceptionThrown); + + // delete using good version + desc.setVersion(goodVersion); + docMgr.delete(desc); + + goodVersion = desc.getVersion(); + + System.out.println("unknown version after delete: " + goodVersion); + + String verifyDeleteException = ""; + String expectedVerifyDeleteException = "com.marklogic.client.ResourceNotFoundException: Local message: Could not read non-existent document"; + + StringHandle deleteHandle = new StringHandle(); + try + { + docMgr.read(desc, deleteHandle); + } catch (ResourceNotFoundException e) { verifyDeleteException = e.toString(); } + + boolean isVerifyDeleteExceptionThrown = verifyDeleteException.contains(expectedVerifyDeleteException); + assertTrue("Exception is not thrown", isVerifyDeleteExceptionThrown); + + // release client + client.release(); + + try { + Thread.sleep(30000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + System.out.println(configMgr.getContentVersionRequests()); + } + + + @SuppressWarnings("deprecation") + @Test public void testNone() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testNone"); + + String filename = "json-original.json"; + String updateFilename = "json-updated.json"; + String uri = "/optimistic-locking/"; + String docId = uri + filename; + long badVersion = 1111; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create a manager for the server configuration + ServerConfigurationManager configMgr = client.newServerConfigManager(); + + // read the server configuration from the database + configMgr.readConfiguration(); + + // use Policy.NONE + configMgr.setContentVersionRequests(Policy.NONE); + + // write the server configuration to the database + configMgr.writeConfiguration(); + + System.out.println("set optimistic locking to none"); + + // create document manager + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + // create document descriptor + DocumentDescriptor desc = docMgr.newDescriptor(docId); + + desc.setVersion(badVersion); + + // CREATE + // write document with bad version + + docMgr.write(desc, handle); + + StringHandle readHandle = new StringHandle(); + docMgr.read(desc, readHandle); + String content = readHandle.get(); + assertTrue("Wrong content", content.contains("John")); + + // UPDATE + File updateFile = new File("src/test/java/com/marklogic/javaclient/data/" + updateFilename); + + // create a handle on the content + FileHandle updateHandle = new FileHandle(updateFile); + updateHandle.set(updateFile); + + // update with bad version + desc.setVersion(badVersion); + + docMgr.write(desc, updateHandle); + + StringHandle updateReadHandle = new StringHandle(); + docMgr.read(desc, updateReadHandle); + String updateContent = updateReadHandle.get(); + assertTrue("Wrong content", updateContent.contains("Aries")); + + // DELETE + // delete using bad version + desc.setVersion(badVersion); + + docMgr.delete(desc); + + String verifyDeleteException = ""; + String expectedVerifyDeleteException = "com.marklogic.client.ResourceNotFoundException: Local message: Could not read non-existent document"; + + StringHandle deleteHandle = new StringHandle(); + try + { + docMgr.read(desc, deleteHandle); + } catch (ResourceNotFoundException e) { verifyDeleteException = e.toString(); } + + boolean isVerifyDeleteExceptionThrown = verifyDeleteException.contains(expectedVerifyDeleteException); + assertTrue("Exception is not thrown", isVerifyDeleteExceptionThrown); + + // release client + client.release(); + + try { + Thread.sleep(30000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + System.out.println(configMgr.getContentVersionRequests()); + } + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestOutputStreamHandle.java b/test-complete/src/test/java/com/marklogic/javaclient/TestOutputStreamHandle.java index 1b297e7cb..08487aa62 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestOutputStreamHandle.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestOutputStreamHandle.java @@ -1,306 +1,296 @@ -package com.marklogic.javaclient; - -import java.io.IOException; -import java.io.InputStream; - -import javax.xml.parsers.ParserConfigurationException; - -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.InputStreamHandle; -import com.marklogic.client.io.OutputStreamHandle; -import com.marklogic.client.io.OutputStreamSender; -import com.marklogic.javaclient.BasicJavaClientREST; - -import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; -import static org.junit.Assert.*; - -import org.junit.*; -public class TestOutputStreamHandle extends BasicJavaClientREST { - - private static String dbName = "OutputStreamHandleDB"; - private static String [] fNames = {"OutputStreamHandleDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - @BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - } - - @SuppressWarnings("deprecation") - @Test - public void testXmlCRUD() throws IOException, SAXException, ParserConfigurationException - { - String filename = "xml-original-test.xml"; - String uri = "/write-xml-outputstreamhandle/"; - - System.out.println("Running testXmlCRUD"); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - writeDocumentUsingOutputStreamHandle(client, filename, uri, "XML"); - - // read docs - InputStreamHandle contentHandle = readDocumentUsingInputStreamHandle(client, uri+filename, "XML"); - // get the contents - InputStream fileRead = contentHandle.get(); - String readContent = convertInputStreamToString(fileRead); - - // get xml document for expected result - Document expectedDoc = expectedXMLDocument(filename); - - // convert actual string to xml doc - Document readDoc = convertStringToXMLDocument(readContent); - - assertXMLEqual("Write XML difference", expectedDoc, readDoc); - - // update the doc - // acquire the content for update - String updateFilename = "xml-updated-test.xml"; - updateDocumentUsingOutputStreamHandle(client, updateFilename, uri + filename, "XML"); - - // read the document - InputStreamHandle updateHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "XML"); - - // get the contents - InputStream fileReadUpdate = updateHandle.get(); - - String readContentUpdate = convertInputStreamToString(fileReadUpdate); - - // get xml document for expected result - Document expectedDocUpdate = expectedXMLDocument(updateFilename); - - // convert actual string to xml doc - Document readDocUpdate = convertStringToXMLDocument(readContentUpdate); - - assertXMLEqual("Write XML difference", expectedDocUpdate, readDocUpdate); - - // delete the document - deleteDocument(client, uri + filename, "XML"); - - // read the deleted document - String exception = ""; - try - { - InputStreamHandle deleteHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "XML"); - } catch (Exception e) { exception = e.toString(); } - - String expectedException = "Could not read non-existent document"; - boolean documentIsDeleted = exception.contains(expectedException); - assertTrue("Document is not deleted", documentIsDeleted); - - // release client - client.release(); - } - - @SuppressWarnings("deprecation") - @Test - public void testTextCRUD() throws IOException - { - String filename = "text-original.txt"; - String uri = "/write-text-outputstreamhandle/"; - - System.out.println("Running testTextCRUD"); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - writeDocumentUsingOutputStreamHandle(client, filename, uri, "Text"); - - // read docs - InputStreamHandle contentHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "Text"); - - // get the contents - InputStream fileRead = contentHandle.get(); - - String readContent = convertInputStreamToString(fileRead); - - String expectedContent = "hello world, welcome to java API"; - - assertEquals("Write Text difference", expectedContent.trim(), readContent.trim()); - - // update the doc - // acquire the content for update - String updateFilename = "text-updated.txt"; - updateDocumentUsingOutputStreamHandle(client, updateFilename, uri + filename, "Text"); - - // read the document - InputStreamHandle updateHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "Text"); - - // get the contents - InputStream fileReadUpdate = updateHandle.get(); - - String readContentUpdate = convertInputStreamToString(fileReadUpdate); - - String expectedContentUpdate = "hello world, welcome to java API after new updates"; - - assertEquals("Write Text difference", expectedContentUpdate.trim(), readContentUpdate.toString().trim()); - - // delete the document - deleteDocument(client, uri + filename, "Text"); - - // read the deleted document - //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "Text")); - - String exception = ""; - try - { - InputStreamHandle deleteHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "Text"); - } catch (Exception e) { exception = e.toString(); } - - String expectedException = "Could not read non-existent document"; - boolean documentIsDeleted = exception.contains(expectedException); - assertTrue("Document is not deleted", documentIsDeleted); - - // release client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testJsonCRUD() throws IOException - { - String filename = "json-original.json"; - String uri = "/write-json-outputstreamhandle/"; - - System.out.println("Running testJsonCRUD"); - - ObjectMapper mapper = new ObjectMapper(); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - writeDocumentUsingOutputStreamHandle(client, filename, uri, "JSON"); - - // read docs - InputStreamHandle contentHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "JSON"); - - // get the contents - InputStream fileRead = contentHandle.get(); - JsonNode readContent = mapper.readTree(fileRead); - - // get expected contents - JsonNode expectedContent = expectedJSONDocument(filename); - - assertTrue("Write JSON document difference", readContent.equals(expectedContent)); - - // update the doc - // acquire the content for update - String updateFilename = "json-updated.json"; - updateDocumentUsingOutputStreamHandle(client, updateFilename, uri + filename, "JSON"); - - // read the document - InputStreamHandle updateHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "JSON"); - - // get the contents - InputStream fileReadUpdate = updateHandle.get(); - - JsonNode readContentUpdate = mapper.readTree(fileReadUpdate); - - // get expected contents - JsonNode expectedContentUpdate = expectedJSONDocument(updateFilename); - - assertTrue("Write JSON document difference", readContentUpdate.equals(expectedContentUpdate)); - - // delete the document - deleteDocument(client, uri + filename, "JSON"); - - // read the deleted document - //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "JSON")); - - String exception = ""; - try - { - InputStreamHandle deleteHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "JSON"); - } catch (Exception e) { exception = e.toString(); } - - String expectedException = "Could not read non-existent document"; - boolean documentIsDeleted = exception.contains(expectedException); - assertTrue("Document is not deleted", documentIsDeleted); - - // release client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testBinaryCRUD() throws IOException - { - String filename = "Pandakarlino.jpg"; - String uri = "/write-bin-outputstreamhandle/"; - - System.out.println("Running testBinaryCRUD"); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - writeDocumentUsingOutputStreamHandle(client, filename, uri, "Binary"); - - // read docs - InputStreamHandle contentHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "Binary"); - - // get the contents - InputStream fileRead = contentHandle.get(); - - // get the binary size - int size = getBinarySize(fileRead); - int expectedSize = 17154; - - assertEquals("Binary size difference", expectedSize, size); - - // update the doc - // acquire the content for update - String updateFilename = "mlfavicon.png"; - updateDocumentUsingOutputStreamHandle(client, updateFilename, uri + filename, "Binary"); - - // read the document - InputStreamHandle updateHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "Binary"); - - // get the contents - InputStream fileReadUpdate = updateHandle.get(); - - // get the binary size - int sizeUpdate = getBinarySize(fileReadUpdate); - int expectedSizeUpdate = 3322; - - assertEquals("Binary size difference", expectedSizeUpdate, sizeUpdate); - - // delete the document - deleteDocument(client, uri + filename, "Binary"); - - // read the deleted document - //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "Binary")); - - String exception = ""; - try - { - InputStreamHandle deleteHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "Binary"); - } catch (Exception e) { exception = e.toString(); } - - String expectedException = "Could not read non-existent document"; - boolean documentIsDeleted = exception.contains(expectedException); - assertTrue("Document is not deleted", documentIsDeleted); - - // release client - client.release(); - } -@AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} +package com.marklogic.javaclient; + +import java.io.IOException; +import java.io.InputStream; + +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.InputStreamHandle; +import com.marklogic.javaclient.BasicJavaClientREST; + +import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; +import static org.junit.Assert.*; + +import org.junit.*; + +public class TestOutputStreamHandle extends BasicJavaClientREST { + private static String dbName = "OutputStreamHandleDB"; + private static String [] fNames = {"OutputStreamHandleDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + } + + @Test + public void testXmlCRUD() throws IOException, SAXException, ParserConfigurationException + { + String filename = "xml-original-test.xml"; + String uri = "/write-xml-outputstreamhandle/"; + + System.out.println("Running testXmlCRUD"); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + writeDocumentUsingOutputStreamHandle(client, filename, uri, "XML"); + + // read docs + InputStreamHandle contentHandle = readDocumentUsingInputStreamHandle(client, uri+filename, "XML"); + // get the contents + InputStream fileRead = contentHandle.get(); + String readContent = convertInputStreamToString(fileRead); + + // get xml document for expected result + Document expectedDoc = expectedXMLDocument(filename); + + // convert actual string to xml doc + Document readDoc = convertStringToXMLDocument(readContent); + + assertXMLEqual("Write XML difference", expectedDoc, readDoc); + + // update the doc + // acquire the content for update + String updateFilename = "xml-updated-test.xml"; + updateDocumentUsingOutputStreamHandle(client, updateFilename, uri + filename, "XML"); + + // read the document + InputStreamHandle updateHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "XML"); + + // get the contents + InputStream fileReadUpdate = updateHandle.get(); + + String readContentUpdate = convertInputStreamToString(fileReadUpdate); + + // get xml document for expected result + Document expectedDocUpdate = expectedXMLDocument(updateFilename); + + // convert actual string to xml doc + Document readDocUpdate = convertStringToXMLDocument(readContentUpdate); + + assertXMLEqual("Write XML difference", expectedDocUpdate, readDocUpdate); + + // delete the document + deleteDocument(client, uri + filename, "XML"); + + // read the deleted document + String exception = ""; + try + { + readDocumentUsingInputStreamHandle(client, uri + filename, "XML"); + } catch (Exception e) { exception = e.toString(); } + + String expectedException = "Could not read non-existent document"; + boolean documentIsDeleted = exception.contains(expectedException); + assertTrue("Document is not deleted", documentIsDeleted); + + // release client + client.release(); + } + + @Test + public void testTextCRUD() throws IOException + { + String filename = "text-original.txt"; + String uri = "/write-text-outputstreamhandle/"; + + System.out.println("Running testTextCRUD"); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + writeDocumentUsingOutputStreamHandle(client, filename, uri, "Text"); + + // read docs + InputStreamHandle contentHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "Text"); + + // get the contents + InputStream fileRead = contentHandle.get(); + + String readContent = convertInputStreamToString(fileRead); + + String expectedContent = "hello world, welcome to java API"; + + assertEquals("Write Text difference", expectedContent.trim(), readContent.trim()); + + // update the doc + // acquire the content for update + String updateFilename = "text-updated.txt"; + updateDocumentUsingOutputStreamHandle(client, updateFilename, uri + filename, "Text"); + + // read the document + InputStreamHandle updateHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "Text"); + + // get the contents + InputStream fileReadUpdate = updateHandle.get(); + + String readContentUpdate = convertInputStreamToString(fileReadUpdate); + + String expectedContentUpdate = "hello world, welcome to java API after new updates"; + + assertEquals("Write Text difference", expectedContentUpdate.trim(), readContentUpdate.toString().trim()); + + // delete the document + deleteDocument(client, uri + filename, "Text"); + + // read the deleted document + String exception = ""; + try + { + readDocumentUsingInputStreamHandle(client, uri + filename, "Text"); + } catch (Exception e) { exception = e.toString(); } + + String expectedException = "Could not read non-existent document"; + boolean documentIsDeleted = exception.contains(expectedException); + assertTrue("Document is not deleted", documentIsDeleted); + + // release client + client.release(); + } + + @Test + public void testJsonCRUD() throws IOException + { + String filename = "json-original.json"; + String uri = "/write-json-outputstreamhandle/"; + + System.out.println("Running testJsonCRUD"); + + ObjectMapper mapper = new ObjectMapper(); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + writeDocumentUsingOutputStreamHandle(client, filename, uri, "JSON"); + + // read docs + InputStreamHandle contentHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "JSON"); + + // get the contents + InputStream fileRead = contentHandle.get(); + JsonNode readContent = mapper.readTree(fileRead); + + // get expected contents + JsonNode expectedContent = expectedJSONDocument(filename); + + assertTrue("Write JSON document difference", readContent.equals(expectedContent)); + + // update the doc + // acquire the content for update + String updateFilename = "json-updated.json"; + updateDocumentUsingOutputStreamHandle(client, updateFilename, uri + filename, "JSON"); + + // read the document + InputStreamHandle updateHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "JSON"); + + // get the contents + InputStream fileReadUpdate = updateHandle.get(); + + JsonNode readContentUpdate = mapper.readTree(fileReadUpdate); + + // get expected contents + JsonNode expectedContentUpdate = expectedJSONDocument(updateFilename); + + assertTrue("Write JSON document difference", readContentUpdate.equals(expectedContentUpdate)); + + // delete the document + deleteDocument(client, uri + filename, "JSON"); + + // read the deleted document + //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "JSON")); + + String exception = ""; + try + { + readDocumentUsingInputStreamHandle(client, uri + filename, "JSON"); + } catch (Exception e) { exception = e.toString(); } + + String expectedException = "Could not read non-existent document"; + boolean documentIsDeleted = exception.contains(expectedException); + assertTrue("Document is not deleted", documentIsDeleted); + + // release client + client.release(); + } + + @Test + public void testBinaryCRUD() throws IOException + { + String filename = "Pandakarlino.jpg"; + String uri = "/write-bin-outputstreamhandle/"; + + System.out.println("Running testBinaryCRUD"); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + writeDocumentUsingOutputStreamHandle(client, filename, uri, "Binary"); + + // read docs + InputStreamHandle contentHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "Binary"); + + // get the contents + InputStream fileRead = contentHandle.get(); + + // get the binary size + int size = getBinarySize(fileRead); + int expectedSize = 17154; + + assertEquals("Binary size difference", expectedSize, size); + + // update the doc + // acquire the content for update + String updateFilename = "mlfavicon.png"; + updateDocumentUsingOutputStreamHandle(client, updateFilename, uri + filename, "Binary"); + + // read the document + InputStreamHandle updateHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "Binary"); + + // get the contents + InputStream fileReadUpdate = updateHandle.get(); + + // get the binary size + int sizeUpdate = getBinarySize(fileReadUpdate); + int expectedSizeUpdate = 3322; + + assertEquals("Binary size difference", expectedSizeUpdate, sizeUpdate); + + // delete the document + deleteDocument(client, uri + filename, "Binary"); + + // read the deleted document + String exception = ""; + try + { + readDocumentUsingInputStreamHandle(client, uri + filename, "Binary"); + } catch (Exception e) { exception = e.toString(); } + + String expectedException = "Could not read non-existent document"; + boolean documentIsDeleted = exception.contains(expectedException); + assertTrue("Document is not deleted", documentIsDeleted); + + // release client + client.release(); + } +@AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java index 34173f438..5287f1f54 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java @@ -1,871 +1,852 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.File; -import java.io.IOException; - -import javax.xml.namespace.QName; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ObjectNode; -import com.marklogic.client.DatabaseClient; -import com.marklogic.javaclient.TestCRUDModulesDb; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.Transaction; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.admin.ExtensionLibrariesManager; -import com.marklogic.client.document.DocumentDescriptor; -import com.marklogic.client.document.DocumentMetadataPatchBuilder; -import com.marklogic.client.document.DocumentPatchBuilder; -import com.marklogic.client.document.DocumentPatchBuilder.PathLanguage; -import com.marklogic.client.document.DocumentUriTemplate; -import com.marklogic.client.document.JSONDocumentManager; -import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.document.DocumentMetadataPatchBuilder.Cardinality; -import com.marklogic.client.document.DocumentPatchBuilder.Position; -import com.marklogic.client.io.FileHandle; -import com.marklogic.client.io.Format; -import com.marklogic.client.io.StringHandle; -import com.marklogic.client.io.DocumentMetadataHandle.Capability; -import com.marklogic.client.io.marker.DocumentPatchHandle; -import com.marklogic.client.query.StructuredQueryBuilder.FragmentScope; -import com.marklogic.client.query.ValuesDefinition.Frequency; - -import org.junit.*; - - -public class TestPartialUpdate extends BasicJavaClientREST { - - private static String dbName = "TestPartialUpdateDB"; - private static String [] fNames = {"TestPartialUpdateDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort=8011; - -@BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - } - -@After - public void testCleanUp() throws Exception - { - clearDB(restPort); - System.out.println("Running clear script"); - } - -@SuppressWarnings("deprecation") -@Test - public void testPartialUpdateXML() throws IOException - { - System.out.println("Running testPartialUpdateXML"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML"); - } - - String docId = "/partial-update/constraint1.xml"; - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); - - patchBldr.insertFragment("/root", Position.LAST_CHILD, "2013-03-21"); - DocumentPatchHandle patchHandle = patchBldr.build(); - docMgr.patch(docId, patchHandle); - - String content = docMgr.read(docId, new StringHandle()).get(); - - System.out.println(content); - - assertTrue("fragment is not inserted", content.contains("2013-03-21")); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testPartialUpdateJSON() throws IOException - { - System.out.println("Running testPartialUpdateJSON"); - - String[] filenames = {"json-original.json"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "JSON"); - } - - ObjectMapper mapper = new ObjectMapper(); - - String docId = "/partial-update/json-original.json"; - JSONDocumentManager docMgr = client.newJSONDocumentManager(); - DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); - patchBldr.pathLanguage(PathLanguage.JSONPATH); - ObjectNode fragmentNode = mapper.createObjectNode(); - fragmentNode = mapper.createObjectNode(); - fragmentNode.put("insertedKey", 9); - String fragment = mapper.writeValueAsString(fragmentNode); - - patchBldr.insertFragment("$.employees", Position.LAST_CHILD, fragment); - DocumentPatchHandle patchHandle = patchBldr.build(); - docMgr.patch(docId, patchHandle); - - String content = docMgr.read(docId, new StringHandle()).get(); - - System.out.println(content); - - assertTrue("fragment is not inserted", content.contains("{\"insertedKey\":9}]")); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testPartialUpdateContent() throws IOException - { - System.out.println("Running testPartialUpdateContent"); - - String filename = "constraint1.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML"); - - String docId = "/partial-update/constraint1.xml"; - - //Creating Manager - XMLDocumentManager xmlDocMgr = client.newXMLDocumentManager(); - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - - - //// - //Updating Content - //// - //Inserting Node - DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); - patchBldr.insertFragment("/root", Position.LAST_CHILD, "2013-03-21"); - DocumentPatchHandle patchHandle = patchBldr.build(); - docMgr.patch(docId, patchHandle); - String contentBefore = xmlDocMgr.read(docId, new StringHandle()).get(); - - System.out.println(" Before Updating "+ contentBefore ); - - //Updating inserted Node - DocumentPatchBuilder xmlPatchBldr = xmlDocMgr.newPatchBuilder(); - DocumentPatchHandle xmlPatchForNode = xmlPatchBldr.replaceFragment("/root/modified", "2012-11-5").build(); - xmlDocMgr.patch(docId, xmlPatchForNode); - String contentAfter = xmlDocMgr.read(docId, new StringHandle()).get(); - - System.out.println("After Updating" + contentAfter); - - assertTrue("fragment is not inserted", contentAfter.contains("2012-11-5")); - - //// - //Updating Doc Element - //// - String contentBeforeElement = xmlDocMgr.read(docId, new StringHandle()).get(); - System.out.println(" Before Updating "+ contentBeforeElement ); - DocumentPatchHandle xmlPatchForElement = xmlPatchBldr.replaceValue("/root/popularity", 10).build(); - xmlDocMgr.patch(docId, xmlPatchForElement); - contentAfter = xmlDocMgr.read(docId, new StringHandle()).get(); - - System.out.println("After Updating" + contentAfter); - - //Check - assertTrue("Element Value has not Changed", contentAfter.contains("10")); - - //// - //Updating Doc Attribute - //// - String contentBeforeAttribute = xmlDocMgr.read(docId, new StringHandle()).get(); - System.out.println(" Before Updating "+ contentBeforeAttribute ); - - //Updating Attribute Value - xmlPatchBldr.replaceValue("/root/*:price/@amt",0.5); - //xmlPatchBldr.replaceValue("/root/*:price/@xmlns","http://marklogic.com"); - DocumentPatchHandle xmlPatchForValue = xmlPatchBldr.build(); - xmlDocMgr.patch(docId, xmlPatchForValue); - contentAfter = xmlDocMgr.read(docId, new StringHandle()).get(); - - System.out.println("After Updating" + contentAfter); - //Check - assertTrue("Value of amt has not Chenged", contentAfter.contains("")); - - - //// - //Updating Doc Namespace - //// - String contentBeforeNamespace = xmlDocMgr.read(docId, new StringHandle()).get(); - System.out.println(" Before Updating "+ contentBeforeNamespace ); - - //Changing Element Value - DocumentPatchHandle xmlPatch = xmlPatchBldr.replaceValue("/root/*:date", "2006-02-02").build(); - xmlDocMgr.patch(docId, xmlPatch); - contentAfter = xmlDocMgr.read(docId, new StringHandle()).get(); - - System.out.println("After Updating" + contentAfter); - //Check - assertTrue("Element Value has not Changed", contentAfter.contains("2006-02-02")); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testPartialUpdateDeletePath() throws IOException - { - System.out.println("Running testPartialUpdateDeletePath"); - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - String filename = "constraint1.xml"; - writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML"); - String docId = "/partial-update/constraint1.xml"; - - //Creating Manager - XMLDocumentManager xmlDocMgr = client.newXMLDocumentManager(); - - String contentBefore = xmlDocMgr.read(docId, new StringHandle()).get(); - System.out.println(" Before Updating "+ contentBefore ); - - //Deleting Element Value - DocumentPatchBuilder xmlPatchBldr = xmlDocMgr.newPatchBuilder(); - //DocumentPatchHandle xmlPatch = xmlPatchBldr.replaceValue("/root/*:date", "2006-02-02").build(); - DocumentPatchHandle xmlPatch = xmlPatchBldr.delete("/root/*:date").build(); - xmlDocMgr.patch(docId, xmlPatch); - - //Delete invalid Path - try{ - xmlPatch = xmlPatchBldr.delete("InvalidPath").build(); - xmlDocMgr.patch(docId, xmlPatch); - } - catch (Exception e){ - System.out.println(e.toString()); - assertTrue("Haven't deleted Invalid path", e.toString().contains(" invalid path: //InvalidPath")); - } - String contentAfter = xmlDocMgr.read(docId, new StringHandle()).get(); - - System.out.println("After Updating" + contentAfter); - assertFalse("Element is not Deleted", contentAfter.contains("2005-01-01")); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testPartialUpdateFragments() throws Exception{ - System.out.println("Running testPartialUpdateFragments"); - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - String filename = "constraint1.xml"; - writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML"); - String docId = "/partial-update/constraint1.xml"; - - //Creating Manager - XMLDocumentManager xmlDocMgr = client.newXMLDocumentManager(); - String contentBefore = xmlDocMgr.read(docId, new StringHandle()).get(); - System.out.println(" Before Updating "+ contentBefore ); - //Inserting Fragments with valid path - DocumentPatchBuilder patchBldr = xmlDocMgr.newPatchBuilder(); - patchBldr.insertFragment("/root/title", Position.BEFORE , "Hi\n "); - patchBldr.insertFragment("/root/id", Position.AFTER , "\n 2013-03-21"); - patchBldr.insertFragment("/root", Position.LAST_CHILD , " bye\n"); - //Inserting Fragments with invalid path - patchBldr.insertFragment("/root/someinvalidpath", Position.BEFORE, "Entry"); - DocumentPatchHandle patchHandle = patchBldr.build(); - xmlDocMgr.patch(docId, patchHandle); - String content = xmlDocMgr.read(docId, new StringHandle()).get(); - - System.out.println(content); - - assertTrue("fragment is not inserted Before", content.contains("Hi")); - assertTrue("fragment is not inserted After", content.contains("2013-03-21")); - assertTrue("fragment is not inserted as Last Child", content.contains("bye")); - assertFalse("fragment with invalid path has entered", content.contains("Entry")); - // release client - client.release(); - - - } - -@SuppressWarnings("deprecation") -@Test public void testPartialUpdateInsertFragments() throws Exception{ - System.out.println("Running testPartialUpdateInsertFragments"); - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - String filename = "constraint1.xml"; - writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML"); - String docId = "/partial-update/constraint1.xml"; - - //Creating Manager - XMLDocumentManager xmlDocMgr = client.newXMLDocumentManager(); - String contentBefore = xmlDocMgr.read(docId, new StringHandle()).get(); - System.out.println(" Before Updating "+ contentBefore ); - //Replacing Fragments with valid path - DocumentPatchBuilder patchBldr = xmlDocMgr.newPatchBuilder(); - patchBldr.replaceFragment("/root/title", "foo"); - //Replacing Fragments with invalid path - patchBldr.replaceFragment("/root/invalidpath", "FalseEntry"); - patchBldr.replaceInsertFragment("/root/nonexist", "/root", Position.LAST_CHILD, " bar\n "); - DocumentPatchHandle patchHandle = patchBldr.build(); - xmlDocMgr.patch(docId, patchHandle); - String content = xmlDocMgr.read(docId, new StringHandle()).get(); - - System.out.println(content); - - assertTrue("fragment is not Replaced", content.contains("foo")); - assertFalse("fragment is not Replaced", content.contains("FalseEntry")); - assertTrue("replaceInsertFragment has Failed", content.contains("bar")); - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testPartialUpdateInsertExistingFragments() throws Exception{ - System.out.println("Running testPartialUpdateInsertExistingFragments"); - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - String filename = "constraint1.xml"; - writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML"); - String docId = "/partial-update/constraint1.xml"; - - //Creating Manager - XMLDocumentManager xmlDocMgr = client.newXMLDocumentManager(); - String contentBefore = xmlDocMgr.read(docId, new StringHandle()).get(); - System.out.println(" Before Updating "+ contentBefore ); - //Replacing Fragments with valid path - DocumentPatchBuilder patchBldr = xmlDocMgr.newPatchBuilder(); - patchBldr.replaceInsertFragment("/root/title", "/root", Position.LAST_CHILD, "LastChild"); - patchBldr.replaceInsertFragment("/root/id", "/root", Position.BEFORE, "Before"); - patchBldr.replaceInsertFragment("/root/p", "/root", Position.AFTER, "After"); - DocumentPatchHandle patchHandle = patchBldr.build(); - xmlDocMgr.patch(docId, patchHandle); - String content = xmlDocMgr.read(docId, new StringHandle()).get(); - - System.out.println(content); - - assertTrue("replaceInsertFragment Failed at Position.LAST_CHILD", content.contains("LastChild")); - assertTrue("replaceInsertFragment Failed at Position.BEFORE", content.contains("Before")); - assertTrue("replaceInsertFragment Failed at Position.AFTER", content.contains("After")); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testPartialUpdateReplaceApply() throws Exception{ - System.out.println("Running testPartialUpdateReplaceApply"); - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - ExtensionLibrariesManager libsMgr = client.newServerConfigManager().newExtensionLibrariesManager(); - - libsMgr.write("/ext/patch/custom-lib.xqy", new FileHandle(new File("src/test/java/com/marklogic/javaclient/data/custom-lib.xqy")).withFormat(Format.TEXT)); - // write docs - String filename = "constraint6.xml"; - writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML"); - //writeDocumentUsingInputStreamHandle(client, "custom-lib.xqy", "/partial-update/", "XML"); - String docId = "/partial-update/constraint6.xml"; - - //Creating Manager - XMLDocumentManager xmlDocMgr = client.newXMLDocumentManager(); - String contentBefore = xmlDocMgr.read(docId, new StringHandle()).get(); - System.out.println(" Before Updating "+ contentBefore ); - // Executing different operations on XML - DocumentPatchBuilder patchBldr = xmlDocMgr.newPatchBuilder(); - patchBldr.replaceApply("/root/add", patchBldr.call().add(10)); - patchBldr.replaceApply("/root/subtract", patchBldr.call().subtract(2)); - patchBldr.replaceApply("/root/multiply", patchBldr.call().multiply(2)); - patchBldr.replaceApply("/root/divide", patchBldr.call().divideBy(2)); - patchBldr.replaceApply("/root/concatenateAfter", patchBldr.call().concatenateAfter(" ML7")); - patchBldr.replaceApply("/root/concatenateBetween", patchBldr.call().concatenateBetween("ML "," 7")); - patchBldr.replaceApply("/root/concatenateBefore", patchBldr.call().concatenateBefore("ML ")); - patchBldr.replaceApply("/root/substringAfter", patchBldr.call().substringAfter("Version")); - patchBldr.replaceApply("/root/substringBefore", patchBldr.call().substringBefore("Version")); - patchBldr.replaceApply("/root/replaceRegex", patchBldr.call().replaceRegex("[a-m]","1")); - patchBldr.replaceApply("/root/applyLibrary", patchBldr.call().applyLibraryFragments("underwrite","API")).library("http://marklogic.com/ext/patch/custom-lib","/ext/patch/custom-lib.xqy"); - //patchBldr.replaceApply("/root/applyLibrary", patchBldr.call().applyLibraryValues("any-content","")).library("http://marklogic.com/ext/patch/custom-lib","/ext/patch/custom-lib.xqy"); - DocumentPatchHandle patchHandle = patchBldr.build(); - xmlDocMgr.patch(docId, patchHandle); - String content = xmlDocMgr.read(docId, new StringHandle()).get(); - - System.out.println("After Update" + content); - //Check - assertTrue("Add Failed", content.contains("15")); - assertTrue("Subtract Failed", content.contains("3")); - assertTrue("Multiplication Failed", content.contains("4")); - assertTrue("Division Failed", content.contains("10")); - assertTrue("concatenateAfter Failed", content.contains("Hi ML7")); - assertTrue("concatenateBefore Failed", content.contains("ML 7")); - assertTrue("substringAfter Failed", content.contains(" 7")); - assertTrue("substringBefore Failed", content.contains("ML ")); - assertTrue("concatenateBetween Failed", content.contains("ML Version 7")); - assertTrue("Ragex Failed", content.contains("C111nt")); - assertTrue("Apply Library Fragments Failed ", content.contains("APIAPI")); - // release client - libsMgr.delete("/ext/patch/custom-lib.xqy"); - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testPartialUpdateCombination() throws Exception{ - System.out.println("Running testPartialUpdateCombination"); - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - String filename = "constraint1.xml"; - writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML"); - String docId = "/partial-update/constraint1.xml"; - - //Creating Manager - XMLDocumentManager xmlDocMgr = client.newXMLDocumentManager(); - String contentBefore = xmlDocMgr.read(docId, new StringHandle()).get(); - System.out.println(" Before Updating "+ contentBefore ); - - DocumentPatchBuilder xmlPatchBldr = xmlDocMgr.newPatchBuilder(); - DocumentPatchHandle xmlPatch = xmlPatchBldr.insertFragment("/root", Position.LAST_CHILD, "2012-11-5").delete("/root/*:date").replaceApply("/root/popularity", xmlPatchBldr.call().multiply(2)).build(); - xmlDocMgr.patch(docId, xmlPatch); - String content = xmlDocMgr.read(docId, new StringHandle()).get(); - - System.out.println(" After Updating "+ content); - //Check - assertTrue("Multiplication Failed", content.contains("10")); - assertFalse("Deletion Failed", content.contains("2005-01-01")); - assertTrue("Insertion Failed", content.contains("2012-11-5")); - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testPartialUpdateCombinationTransc() throws Exception{ - System.out.println("Running testPartialUpdateCombination"); - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - Transaction t = client.openTransaction("Transac"); - // write docs - String filename = "constraint1.xml"; - //writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML"); - writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/","XML"); - //t.commit(); - String docId = "/partial-update/constraint1.xml"; - - //Creating Manager - XMLDocumentManager xmlDocMgr = client.newXMLDocumentManager(); - String contentBefore = xmlDocMgr.read(docId, new StringHandle()).get(); - System.out.println(" Before Updating "+ contentBefore ); - //Transaction t1 = client.openTransaction(); - DocumentPatchBuilder xmlPatchBldr = xmlDocMgr.newPatchBuilder(); - DocumentPatchHandle xmlPatch = xmlPatchBldr.insertFragment("/root", Position.LAST_CHILD, "2012-11-5").delete("/root/*:date").replaceApply("/root/popularity", xmlPatchBldr.call().multiply(2)).build(); - xmlDocMgr.patch(docId, xmlPatch,t); - t.commit(); - String content = xmlDocMgr.read(docId, new StringHandle()).get(); - - System.out.println(" After Updating "+ content); - - //Check - assertTrue("Multiplication Failed", content.contains("10")); - assertFalse("Deletion Failed", content.contains("2005-01-01")); - assertTrue("Insertion Failed", content.contains("2012-11-5")); - - // release client - client.release(); - - } - -@SuppressWarnings("deprecation") -@Test public void testPartialUpdateCombinationTranscRevert() throws Exception{ - System.out.println("Running testPartialUpdateCombinationTranscRevert"); - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - // write docs - String[] filenames = {"constraint1.xml", "constraint2.xml"}; - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML"); - } - String docId1= "/partial-update/constraint1.xml"; - String docId2 = "/partial-update/constraint2.xml"; - //Creating Manager - XMLDocumentManager xmlDocMgr1 = client.newXMLDocumentManager(); - XMLDocumentManager xmlDocMgr2= client.newXMLDocumentManager(); - String contentBefore1 = xmlDocMgr1.read(docId1, new StringHandle()).get(); - String contentBefore2 = xmlDocMgr2.read(docId2, new StringHandle()).get(); - System.out.println(" Before Updating Document 1 "+ contentBefore1 ); - System.out.println(" Before Updating Document 2 "+ contentBefore2 ); - - DocumentPatchBuilder xmlPatchBldr1 = xmlDocMgr1.newPatchBuilder(); - DocumentPatchBuilder xmlPatchBldr2 = xmlDocMgr2.newPatchBuilder(); - - DocumentPatchHandle xmlPatch1 = xmlPatchBldr1.insertFragment("/root", Position.LAST_CHILD, "2012-11-5").build(); - DocumentPatchHandle xmlPatch2 = xmlPatchBldr2.insertFragment("/root", Position.LAST_CHILD, "2012-11-5").build(); - - Transaction t1 = client.openTransaction(); - xmlDocMgr1.patch(docId1, xmlPatch1,t1); - t1.commit(); - String content1 = xmlDocMgr1.read(docId1, new StringHandle()).get(); - System.out.println(" After Updating Documant 1 : Transaction Commit"+ content1); - Transaction t2 = client.openTransaction(); - xmlDocMgr1.patch(docId2, xmlPatch2,t2); - t2.rollback(); - - String content2 = xmlDocMgr2.read(docId2, new StringHandle()).get(); - System.out.println(" After Updating Document 2 : Transaction Rollback"+ content2); - - //Check -// assertTrue("Insertion Failed", content.contains("2012-11-5")); - - // release client - client.release(); - - } - - -@SuppressWarnings("deprecation") -@Test public void testPartialUpdateCombinationJSON() throws Exception{ - System.out.println("Running testPartialUpdateCombinationJSON"); - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - String[] filenames = {"json-original.json"}; - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "JSON"); - } - String docId = "/partial-update/json-original.json"; - - ObjectMapper mapper = new ObjectMapper(); - - JSONDocumentManager docMgr = client.newJSONDocumentManager(); - DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); - patchBldr.pathLanguage(PathLanguage.JSONPATH); - String content1 = docMgr.read(docId, new StringHandle()).get(); - - System.out.println("Before" + content1); - ObjectNode fragmentNode = mapper.createObjectNode(); - fragmentNode = mapper.createObjectNode(); - fragmentNode.put("insertedKey", 9); - String fragment = mapper.writeValueAsString(fragmentNode); - patchBldr.insertFragment("$.employees", Position.LAST_CHILD, fragment).delete("$.employees[2]").replaceApply("$.employees[1].firstName", patchBldr.call().concatenateAfter("Hi")); - DocumentPatchHandle patchHandle = patchBldr.build(); - docMgr.patch(docId, patchHandle); - - String content = docMgr.read(docId, new StringHandle()).get(); - - System.out.println("After" + content); - - assertTrue("fragment is not inserted", content.contains("{\"insertedKey\":9}]")); - assertTrue("fragment is not inserted", content.contains("{\"firstName\":\"AnnHi\", \"lastName\":\"Smith\"}")); - assertFalse("fragment is not deleted",content.contains("{\"firstName\":\"Bob\", \"lastName\":\"Foo\"}")); - // release client - client.release(); - - } - - -@SuppressWarnings("deprecation") -@Test public void testPartialUpdateMetadata() throws Exception{ - System.out.println("Running testPartialUpdateMetadata"); - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - String filename = "constraint1.xml"; - writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML"); - String docId = "/partial-update/constraint1.xml"; - - //Creating Manager - XMLDocumentManager xmlDocMgr = client.newXMLDocumentManager(); - String contentMetadata = xmlDocMgr.readMetadata(docId, new StringHandle()).get(); - System.out.println(" Before Updating "+ contentMetadata); - - DocumentMetadataPatchBuilder patchBldr = xmlDocMgr.newPatchBuilder(Format.XML); - patchBldr.addCollection("/document/collection3"); - patchBldr.addPermission("admin", Capability.READ); - patchBldr.addPropertyValue("Hello","Hi"); - DocumentPatchHandle patchHandle = patchBldr.build(); - xmlDocMgr.patch(docId, patchHandle); - - String contentMetadata1 = xmlDocMgr.readMetadata(docId, new StringHandle()).get(); - System.out.println(" After Changing "+ contentMetadata1); - - //Check - assertTrue("Collection not added", contentMetadata1.contains("/document/collection3")); - assertTrue("Permission not added", contentMetadata1.contains("admin")); - assertTrue("Property not added", contentMetadata1.contains("Hi")); - - //// - //replacing Metadata Values - //// - DocumentMetadataPatchBuilder patchBldrRep = xmlDocMgr.newPatchBuilder(Format.XML); - patchBldrRep.replaceCollection("/document/collection3", "/document/collection4"); - patchBldrRep.replacePermission("admin",Capability.UPDATE); - patchBldrRep.replacePropertyValue("Hello", "Bye"); - DocumentPatchHandle patchHandleRep = patchBldrRep.build(); - xmlDocMgr.patch(docId, patchHandleRep); - String contentMetadataRep = xmlDocMgr.readMetadata(docId, new StringHandle()).get(); - System.out.println(" After Updating "+ contentMetadataRep); - - //Check - assertTrue("Collection not added", contentMetadataRep.contains("/document/collection4")); - assertTrue("Permission not added", contentMetadataRep.contains("admin")); - assertTrue("Property not added", contentMetadataRep.contains("Bye")); - - //// - //Deleting Metadata Values - //// - DocumentMetadataPatchBuilder patchBldrDel = xmlDocMgr.newPatchBuilder(Format.XML); - patchBldrDel.deleteCollection("/document/collection4"); - patchBldrDel.deletePermission("admin"); - patchBldrDel.deleteProperty("Hello"); - DocumentPatchHandle patchHandleDel = patchBldrDel.build(); - xmlDocMgr.patch(docId, patchHandleDel); - String contentMetadataDel = xmlDocMgr.readMetadata(docId, new StringHandle()).get(); - System.out.println(" After Deleting "+ contentMetadataDel); - - //Check - assertFalse("Collection not deleted", contentMetadataDel.contains("/document/collection4")); - assertFalse("Permission not deleted", contentMetadataDel.contains("admin")); - assertFalse("Property not deleted", contentMetadataDel.contains("Bye")); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testPartialUpdateXMLDscriptor() throws IOException - { - System.out.println("Running testPartialUpdateXMLDescriptor"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML"); - } - - String docId = "/partial-update/constraint1.xml"; - // create doc manager - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - - //Create Document Descriptor - DocumentDescriptor desc = docMgr.newDescriptor(docId); - DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); - patchBldr.insertFragment("/root", Position.LAST_CHILD, "2013-03-21"); - DocumentPatchHandle patchHandle = patchBldr.build(); - - docMgr.patch(desc, patchHandle); - - String content = docMgr.read(docId, new StringHandle()).get(); - - System.out.println("After"+content); - - assertTrue("fragment is not inserted", content.contains("2013-03-21")); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testPartialUpdateJSONDescriptor() throws IOException - { - System.out.println("Running testPartialUpdateJSONDescriptor"); - - String[] filenames = {"json-original.json"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "JSON"); - } - - - - ObjectMapper mapper = new ObjectMapper(); - String docId = "/partial-update/json-original.json"; - // create doc manager - JSONDocumentManager docMgr = client.newJSONDocumentManager(); - - //Create Document Descriptor - DocumentDescriptor desc = docMgr.newDescriptor(docId); - - DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); - patchBldr.pathLanguage(PathLanguage.JSONPATH); - ObjectNode fragmentNode = mapper.createObjectNode(); - fragmentNode = mapper.createObjectNode(); - fragmentNode.put("insertedKey", 9); - String fragment = mapper.writeValueAsString(fragmentNode); - - patchBldr.insertFragment("$.employees", Position.LAST_CHILD, fragment); - DocumentPatchHandle patchHandle = patchBldr.build(); - - docMgr.patch(desc, patchHandle); - - String content = docMgr.read(docId, new StringHandle()).get(); - - System.out.println("After"+content); - - assertTrue("fragment is not inserted", content.contains("{\"insertedKey\":9}]")); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testPartialUpdateXMLDscriptorTranc() throws IOException - { - System.out.println("Running testPartialUpdateXMLDescriptorTranc"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML"); - } - - String docId = "/partial-update/constraint1.xml"; - // create doc manager - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - // create template - DocumentUriTemplate template = docMgr.newDocumentUriTemplate("xml"); - template.withDirectory(docId); - - DocumentDescriptor desc = docMgr.newDescriptor(template.getDirectory()); - DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); - patchBldr.insertFragment("/root", Position.LAST_CHILD, "2013-03-21"); - DocumentPatchHandle patchHandle = patchBldr.build(); - Transaction t = client.openTransaction("Tranc"); - docMgr.patch(desc, patchHandle, t); - t.commit(); - String content = docMgr.read(docId, new StringHandle()).get(); - - System.out.println("After"+content); - - assertTrue("fragment is not inserted", content.contains("2013-03-21")); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testPartialUpdateJSONDescriptorTranc() throws IOException - { - System.out.println("Running testPartialUpdateJSONDescriptorTranc"); - - String[] filenames = {"json-original.json"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "JSON"); - } - ObjectMapper mapper = new ObjectMapper(); - String docId = "/partial-update/json-original.json"; - // create doc manager - JSONDocumentManager docMgr = client.newJSONDocumentManager(); - // create template - DocumentUriTemplate template = docMgr.newDocumentUriTemplate("JSON"); - template.withDirectory(docId); - //Create Document Descriptor - DocumentDescriptor desc = docMgr.newDescriptor(template.getDirectory()); - DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); - - ObjectNode fragmentNode = mapper.createObjectNode(); - fragmentNode = mapper.createObjectNode(); - fragmentNode.put("insertedKey", 9); - String fragment = mapper.writeValueAsString(fragmentNode); - patchBldr.pathLanguage(PathLanguage.JSONPATH); - patchBldr.insertFragment("$.employees", Position.LAST_CHILD, fragment); - DocumentPatchHandle patchHandle = patchBldr.build(); -// Transaction t = client.openTransaction("Tranc"); - docMgr.patch(desc, patchHandle);//,t); -// t.commit(); - String content = docMgr.read(docId, new StringHandle()).get(); - - System.out.println("After"+content); - - assertTrue("fragment is not inserted", content.contains("{\"insertedKey\":9}]")); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testPartialUpdateCardinality() throws IOException - { - System.out.println("Running testPartialUpdateCardinality"); - - String filename = "constraint1.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML"); - - String docId = "/partial-update/constraint1.xml"; - - //Creating Manager - XMLDocumentManager xmlDocMgr = client.newXMLDocumentManager(); - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - - //Inserting Node - DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); - patchBldr.insertFragment("/root", Position.LAST_CHILD, Cardinality.ONE, "2013-03-21"); - DocumentPatchHandle patchHandle = patchBldr.build(); - docMgr.patch(docId, patchHandle); - String contentBefore = xmlDocMgr.read(docId, new StringHandle()).get(); - - System.out.println(" Content after Updating with Cardinality.ONE : "+ contentBefore ); - assertTrue("Insertion Failed ", contentBefore.contains("")); - //Updating again - DocumentPatchBuilder xmlPatchBldr = xmlDocMgr.newPatchBuilder(); - DocumentPatchHandle xmlPatchForNode = xmlPatchBldr.insertFragment("/root/id", Position.BEFORE , Cardinality.ONE_OR_MORE, "1989-04-06").build(); - xmlDocMgr.patch(docId, xmlPatchForNode); - String contentAfter = xmlDocMgr.read(docId, new StringHandle()).get(); - - System.out.println("Content after Updating with Cardinality.ONE_OR_MORE" + contentAfter); - assertTrue("Insertion Failed ", contentAfter.contains("1989-04-06")); - //Updating again - DocumentPatchBuilder xmlPatchBldr1 = xmlDocMgr.newPatchBuilder(); - DocumentPatchHandle xmlPatchForNode1 = xmlPatchBldr1.insertFragment("/root/id", Position.AFTER , Cardinality.ZERO_OR_ONE, "2013-07-29").build(); - xmlDocMgr.patch(docId, xmlPatchForNode1); - contentAfter = xmlDocMgr.read(docId, new StringHandle()).get(); - - System.out.println("Content after Updating with Cardinality.ZERO_OR_ONE" + contentAfter); - assertTrue("Insertion Failed ", contentAfter.contains("2013-07-29")); - - // release client - client.release(); - } -@AfterClass -public static void tearDown() throws Exception - { - tearDownJavaRESTServer(dbName, fNames, restServerName); - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.IOException; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.Transaction; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.admin.ExtensionLibrariesManager; +import com.marklogic.client.document.DocumentDescriptor; +import com.marklogic.client.document.DocumentMetadataPatchBuilder; +import com.marklogic.client.document.DocumentPatchBuilder; +import com.marklogic.client.document.DocumentPatchBuilder.PathLanguage; +import com.marklogic.client.document.DocumentUriTemplate; +import com.marklogic.client.document.JSONDocumentManager; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.document.DocumentMetadataPatchBuilder.Cardinality; +import com.marklogic.client.document.DocumentPatchBuilder.Position; +import com.marklogic.client.io.FileHandle; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.StringHandle; +import com.marklogic.client.io.DocumentMetadataHandle.Capability; +import com.marklogic.client.io.marker.DocumentPatchHandle; +import org.junit.*; + +public class TestPartialUpdate extends BasicJavaClientREST { + private static String dbName = "TestPartialUpdateDB"; + private static String [] fNames = {"TestPartialUpdateDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort=8011; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + } + + @After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + + @Test + public void testPartialUpdateXML() throws IOException + { + System.out.println("Running testPartialUpdateXML"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML"); + } + + String docId = "/partial-update/constraint1.xml"; + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); + + patchBldr.insertFragment("/root", Position.LAST_CHILD, "2013-03-21"); + DocumentPatchHandle patchHandle = patchBldr.build(); + docMgr.patch(docId, patchHandle); + + String content = docMgr.read(docId, new StringHandle()).get(); + + System.out.println(content); + + assertTrue("fragment is not inserted", content.contains("2013-03-21")); + + // release client + client.release(); + } + + @Test + public void testPartialUpdateJSON() throws IOException + { + System.out.println("Running testPartialUpdateJSON"); + + String[] filenames = {"json-original.json"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "JSON"); + } + + ObjectMapper mapper = new ObjectMapper(); + + String docId = "/partial-update/json-original.json"; + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); + patchBldr.pathLanguage(PathLanguage.JSONPATH); + ObjectNode fragmentNode = mapper.createObjectNode(); + fragmentNode = mapper.createObjectNode(); + fragmentNode.put("insertedKey", 9); + String fragment = mapper.writeValueAsString(fragmentNode); + + patchBldr.insertFragment("$.employees", Position.LAST_CHILD, fragment); + DocumentPatchHandle patchHandle = patchBldr.build(); + docMgr.patch(docId, patchHandle); + + String content = docMgr.read(docId, new StringHandle()).get(); + + System.out.println(content); + + assertTrue("fragment is not inserted", content.contains("{\"insertedKey\":9}]")); + + // release client + client.release(); + } + + @Test + public void testPartialUpdateContent() throws IOException + { + System.out.println("Running testPartialUpdateContent"); + + String filename = "constraint1.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML"); + + String docId = "/partial-update/constraint1.xml"; + + //Creating Manager + XMLDocumentManager xmlDocMgr = client.newXMLDocumentManager(); + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + + + //// + //Updating Content + //// + //Inserting Node + DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); + patchBldr.insertFragment("/root", Position.LAST_CHILD, "2013-03-21"); + DocumentPatchHandle patchHandle = patchBldr.build(); + docMgr.patch(docId, patchHandle); + String contentBefore = xmlDocMgr.read(docId, new StringHandle()).get(); + + System.out.println(" Before Updating "+ contentBefore ); + + //Updating inserted Node + DocumentPatchBuilder xmlPatchBldr = xmlDocMgr.newPatchBuilder(); + DocumentPatchHandle xmlPatchForNode = xmlPatchBldr.replaceFragment("/root/modified", "2012-11-5").build(); + xmlDocMgr.patch(docId, xmlPatchForNode); + String contentAfter = xmlDocMgr.read(docId, new StringHandle()).get(); + + System.out.println("After Updating" + contentAfter); + + assertTrue("fragment is not inserted", contentAfter.contains("2012-11-5")); + + //// + //Updating Doc Element + //// + String contentBeforeElement = xmlDocMgr.read(docId, new StringHandle()).get(); + System.out.println(" Before Updating "+ contentBeforeElement ); + DocumentPatchHandle xmlPatchForElement = xmlPatchBldr.replaceValue("/root/popularity", 10).build(); + xmlDocMgr.patch(docId, xmlPatchForElement); + contentAfter = xmlDocMgr.read(docId, new StringHandle()).get(); + + System.out.println("After Updating" + contentAfter); + + //Check + assertTrue("Element Value has not Changed", contentAfter.contains("10")); + + //// + //Updating Doc Attribute + //// + String contentBeforeAttribute = xmlDocMgr.read(docId, new StringHandle()).get(); + System.out.println(" Before Updating "+ contentBeforeAttribute ); + + //Updating Attribute Value + xmlPatchBldr.replaceValue("/root/*:price/@amt",0.5); + //xmlPatchBldr.replaceValue("/root/*:price/@xmlns","http://marklogic.com"); + DocumentPatchHandle xmlPatchForValue = xmlPatchBldr.build(); + xmlDocMgr.patch(docId, xmlPatchForValue); + contentAfter = xmlDocMgr.read(docId, new StringHandle()).get(); + + System.out.println("After Updating" + contentAfter); + //Check + assertTrue("Value of amt has not Chenged", contentAfter.contains("")); + + + //// + //Updating Doc Namespace + //// + String contentBeforeNamespace = xmlDocMgr.read(docId, new StringHandle()).get(); + System.out.println(" Before Updating "+ contentBeforeNamespace ); + + //Changing Element Value + DocumentPatchHandle xmlPatch = xmlPatchBldr.replaceValue("/root/*:date", "2006-02-02").build(); + xmlDocMgr.patch(docId, xmlPatch); + contentAfter = xmlDocMgr.read(docId, new StringHandle()).get(); + + System.out.println("After Updating" + contentAfter); + //Check + assertTrue("Element Value has not Changed", contentAfter.contains("2006-02-02")); + + // release client + client.release(); + } + + @Test + public void testPartialUpdateDeletePath() throws IOException + { + System.out.println("Running testPartialUpdateDeletePath"); + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + String filename = "constraint1.xml"; + writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML"); + String docId = "/partial-update/constraint1.xml"; + + //Creating Manager + XMLDocumentManager xmlDocMgr = client.newXMLDocumentManager(); + + String contentBefore = xmlDocMgr.read(docId, new StringHandle()).get(); + System.out.println(" Before Updating "+ contentBefore ); + + //Deleting Element Value + DocumentPatchBuilder xmlPatchBldr = xmlDocMgr.newPatchBuilder(); + //DocumentPatchHandle xmlPatch = xmlPatchBldr.replaceValue("/root/*:date", "2006-02-02").build(); + DocumentPatchHandle xmlPatch = xmlPatchBldr.delete("/root/*:date").build(); + xmlDocMgr.patch(docId, xmlPatch); + + //Delete invalid Path + try{ + xmlPatch = xmlPatchBldr.delete("InvalidPath").build(); + xmlDocMgr.patch(docId, xmlPatch); + } + catch (Exception e){ + System.out.println(e.toString()); + assertTrue("Haven't deleted Invalid path", e.toString().contains(" invalid path: //InvalidPath")); + } + String contentAfter = xmlDocMgr.read(docId, new StringHandle()).get(); + + System.out.println("After Updating" + contentAfter); + assertFalse("Element is not Deleted", contentAfter.contains("2005-01-01")); + + // release client + client.release(); + } + + @Test + public void testPartialUpdateFragments() throws Exception{ + System.out.println("Running testPartialUpdateFragments"); + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + String filename = "constraint1.xml"; + writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML"); + String docId = "/partial-update/constraint1.xml"; + + //Creating Manager + XMLDocumentManager xmlDocMgr = client.newXMLDocumentManager(); + String contentBefore = xmlDocMgr.read(docId, new StringHandle()).get(); + System.out.println(" Before Updating "+ contentBefore ); + //Inserting Fragments with valid path + DocumentPatchBuilder patchBldr = xmlDocMgr.newPatchBuilder(); + patchBldr.insertFragment("/root/title", Position.BEFORE , "Hi\n "); + patchBldr.insertFragment("/root/id", Position.AFTER , "\n 2013-03-21"); + patchBldr.insertFragment("/root", Position.LAST_CHILD , " bye\n"); + //Inserting Fragments with invalid path + patchBldr.insertFragment("/root/someinvalidpath", Position.BEFORE, "Entry"); + DocumentPatchHandle patchHandle = patchBldr.build(); + xmlDocMgr.patch(docId, patchHandle); + String content = xmlDocMgr.read(docId, new StringHandle()).get(); + + System.out.println(content); + + assertTrue("fragment is not inserted Before", content.contains("Hi")); + assertTrue("fragment is not inserted After", content.contains("2013-03-21")); + assertTrue("fragment is not inserted as Last Child", content.contains("bye")); + assertFalse("fragment with invalid path has entered", content.contains("Entry")); + // release client + client.release(); + } + + @Test + public void testPartialUpdateInsertFragments() throws Exception{ + System.out.println("Running testPartialUpdateInsertFragments"); + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + String filename = "constraint1.xml"; + writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML"); + String docId = "/partial-update/constraint1.xml"; + + //Creating Manager + XMLDocumentManager xmlDocMgr = client.newXMLDocumentManager(); + String contentBefore = xmlDocMgr.read(docId, new StringHandle()).get(); + System.out.println(" Before Updating "+ contentBefore ); + //Replacing Fragments with valid path + DocumentPatchBuilder patchBldr = xmlDocMgr.newPatchBuilder(); + patchBldr.replaceFragment("/root/title", "foo"); + //Replacing Fragments with invalid path + patchBldr.replaceFragment("/root/invalidpath", "FalseEntry"); + patchBldr.replaceInsertFragment("/root/nonexist", "/root", Position.LAST_CHILD, " bar\n "); + DocumentPatchHandle patchHandle = patchBldr.build(); + xmlDocMgr.patch(docId, patchHandle); + String content = xmlDocMgr.read(docId, new StringHandle()).get(); + + System.out.println(content); + + assertTrue("fragment is not Replaced", content.contains("foo")); + assertFalse("fragment is not Replaced", content.contains("FalseEntry")); + assertTrue("replaceInsertFragment has Failed", content.contains("bar")); + // release client + client.release(); + } + + @Test + public void testPartialUpdateInsertExistingFragments() throws Exception{ + System.out.println("Running testPartialUpdateInsertExistingFragments"); + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + String filename = "constraint1.xml"; + writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML"); + String docId = "/partial-update/constraint1.xml"; + + //Creating Manager + XMLDocumentManager xmlDocMgr = client.newXMLDocumentManager(); + String contentBefore = xmlDocMgr.read(docId, new StringHandle()).get(); + System.out.println(" Before Updating "+ contentBefore ); + //Replacing Fragments with valid path + DocumentPatchBuilder patchBldr = xmlDocMgr.newPatchBuilder(); + patchBldr.replaceInsertFragment("/root/title", "/root", Position.LAST_CHILD, "LastChild"); + patchBldr.replaceInsertFragment("/root/id", "/root", Position.BEFORE, "Before"); + patchBldr.replaceInsertFragment("/root/p", "/root", Position.AFTER, "After"); + DocumentPatchHandle patchHandle = patchBldr.build(); + xmlDocMgr.patch(docId, patchHandle); + String content = xmlDocMgr.read(docId, new StringHandle()).get(); + + System.out.println(content); + + assertTrue("replaceInsertFragment Failed at Position.LAST_CHILD", content.contains("LastChild")); + assertTrue("replaceInsertFragment Failed at Position.BEFORE", content.contains("Before")); + assertTrue("replaceInsertFragment Failed at Position.AFTER", content.contains("After")); + + // release client + client.release(); + } + + @Test + public void testPartialUpdateReplaceApply() throws Exception{ + System.out.println("Running testPartialUpdateReplaceApply"); + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + ExtensionLibrariesManager libsMgr = client.newServerConfigManager().newExtensionLibrariesManager(); + + libsMgr.write("/ext/patch/custom-lib.xqy", new FileHandle(new File("src/test/java/com/marklogic/javaclient/data/custom-lib.xqy")).withFormat(Format.TEXT)); + // write docs + String filename = "constraint6.xml"; + writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML"); + //writeDocumentUsingInputStreamHandle(client, "custom-lib.xqy", "/partial-update/", "XML"); + String docId = "/partial-update/constraint6.xml"; + + //Creating Manager + XMLDocumentManager xmlDocMgr = client.newXMLDocumentManager(); + String contentBefore = xmlDocMgr.read(docId, new StringHandle()).get(); + System.out.println(" Before Updating "+ contentBefore ); + // Executing different operations on XML + DocumentPatchBuilder patchBldr = xmlDocMgr.newPatchBuilder(); + patchBldr.replaceApply("/root/add", patchBldr.call().add(10)); + patchBldr.replaceApply("/root/subtract", patchBldr.call().subtract(2)); + patchBldr.replaceApply("/root/multiply", patchBldr.call().multiply(2)); + patchBldr.replaceApply("/root/divide", patchBldr.call().divideBy(2)); + patchBldr.replaceApply("/root/concatenateAfter", patchBldr.call().concatenateAfter(" ML7")); + patchBldr.replaceApply("/root/concatenateBetween", patchBldr.call().concatenateBetween("ML "," 7")); + patchBldr.replaceApply("/root/concatenateBefore", patchBldr.call().concatenateBefore("ML ")); + patchBldr.replaceApply("/root/substringAfter", patchBldr.call().substringAfter("Version")); + patchBldr.replaceApply("/root/substringBefore", patchBldr.call().substringBefore("Version")); + patchBldr.replaceApply("/root/replaceRegex", patchBldr.call().replaceRegex("[a-m]","1")); + patchBldr.replaceApply("/root/applyLibrary", patchBldr.call().applyLibraryFragments("underwrite","API")).library("http://marklogic.com/ext/patch/custom-lib","/ext/patch/custom-lib.xqy"); + //patchBldr.replaceApply("/root/applyLibrary", patchBldr.call().applyLibraryValues("any-content","")).library("http://marklogic.com/ext/patch/custom-lib","/ext/patch/custom-lib.xqy"); + DocumentPatchHandle patchHandle = patchBldr.build(); + xmlDocMgr.patch(docId, patchHandle); + String content = xmlDocMgr.read(docId, new StringHandle()).get(); + + System.out.println("After Update" + content); + //Check + assertTrue("Add Failed", content.contains("15")); + assertTrue("Subtract Failed", content.contains("3")); + assertTrue("Multiplication Failed", content.contains("4")); + assertTrue("Division Failed", content.contains("10")); + assertTrue("concatenateAfter Failed", content.contains("Hi ML7")); + assertTrue("concatenateBefore Failed", content.contains("ML 7")); + assertTrue("substringAfter Failed", content.contains(" 7")); + assertTrue("substringBefore Failed", content.contains("ML ")); + assertTrue("concatenateBetween Failed", content.contains("ML Version 7")); + assertTrue("Ragex Failed", content.contains("C111nt")); + assertTrue("Apply Library Fragments Failed ", content.contains("APIAPI")); + // release client + libsMgr.delete("/ext/patch/custom-lib.xqy"); + client.release(); + } + + @Test + public void testPartialUpdateCombination() throws Exception{ + System.out.println("Running testPartialUpdateCombination"); + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + String filename = "constraint1.xml"; + writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML"); + String docId = "/partial-update/constraint1.xml"; + + //Creating Manager + XMLDocumentManager xmlDocMgr = client.newXMLDocumentManager(); + String contentBefore = xmlDocMgr.read(docId, new StringHandle()).get(); + System.out.println(" Before Updating "+ contentBefore ); + + DocumentPatchBuilder xmlPatchBldr = xmlDocMgr.newPatchBuilder(); + DocumentPatchHandle xmlPatch = xmlPatchBldr.insertFragment("/root", Position.LAST_CHILD, "2012-11-5").delete("/root/*:date").replaceApply("/root/popularity", xmlPatchBldr.call().multiply(2)).build(); + xmlDocMgr.patch(docId, xmlPatch); + String content = xmlDocMgr.read(docId, new StringHandle()).get(); + + System.out.println(" After Updating "+ content); + //Check + assertTrue("Multiplication Failed", content.contains("10")); + assertFalse("Deletion Failed", content.contains("2005-01-01")); + assertTrue("Insertion Failed", content.contains("2012-11-5")); + // release client + client.release(); + } + + @Test + public void testPartialUpdateCombinationTransc() throws Exception{ + System.out.println("Running testPartialUpdateCombination"); + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + Transaction t = client.openTransaction("Transac"); + // write docs + String filename = "constraint1.xml"; + //writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML"); + writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/","XML"); + //t.commit(); + String docId = "/partial-update/constraint1.xml"; + + //Creating Manager + XMLDocumentManager xmlDocMgr = client.newXMLDocumentManager(); + String contentBefore = xmlDocMgr.read(docId, new StringHandle()).get(); + System.out.println(" Before Updating "+ contentBefore ); + //Transaction t1 = client.openTransaction(); + DocumentPatchBuilder xmlPatchBldr = xmlDocMgr.newPatchBuilder(); + DocumentPatchHandle xmlPatch = xmlPatchBldr.insertFragment("/root", Position.LAST_CHILD, "2012-11-5").delete("/root/*:date").replaceApply("/root/popularity", xmlPatchBldr.call().multiply(2)).build(); + xmlDocMgr.patch(docId, xmlPatch,t); + t.commit(); + String content = xmlDocMgr.read(docId, new StringHandle()).get(); + + System.out.println(" After Updating "+ content); + + //Check + assertTrue("Multiplication Failed", content.contains("10")); + assertFalse("Deletion Failed", content.contains("2005-01-01")); + assertTrue("Insertion Failed", content.contains("2012-11-5")); + + // release client + client.release(); + + } + + @Test + public void testPartialUpdateCombinationTranscRevert() throws Exception{ + System.out.println("Running testPartialUpdateCombinationTranscRevert"); + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + // write docs + String[] filenames = {"constraint1.xml", "constraint2.xml"}; + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML"); + } + String docId1= "/partial-update/constraint1.xml"; + String docId2 = "/partial-update/constraint2.xml"; + //Creating Manager + XMLDocumentManager xmlDocMgr1 = client.newXMLDocumentManager(); + XMLDocumentManager xmlDocMgr2= client.newXMLDocumentManager(); + String contentBefore1 = xmlDocMgr1.read(docId1, new StringHandle()).get(); + String contentBefore2 = xmlDocMgr2.read(docId2, new StringHandle()).get(); + System.out.println(" Before Updating Document 1 "+ contentBefore1 ); + System.out.println(" Before Updating Document 2 "+ contentBefore2 ); + + DocumentPatchBuilder xmlPatchBldr1 = xmlDocMgr1.newPatchBuilder(); + DocumentPatchBuilder xmlPatchBldr2 = xmlDocMgr2.newPatchBuilder(); + + DocumentPatchHandle xmlPatch1 = xmlPatchBldr1.insertFragment("/root", Position.LAST_CHILD, "2012-11-5").build(); + DocumentPatchHandle xmlPatch2 = xmlPatchBldr2.insertFragment("/root", Position.LAST_CHILD, "2012-11-5").build(); + + Transaction t1 = client.openTransaction(); + xmlDocMgr1.patch(docId1, xmlPatch1,t1); + t1.commit(); + String content1 = xmlDocMgr1.read(docId1, new StringHandle()).get(); + System.out.println(" After Updating Documant 1 : Transaction Commit"+ content1); + Transaction t2 = client.openTransaction(); + xmlDocMgr1.patch(docId2, xmlPatch2,t2); + t2.rollback(); + + String content2 = xmlDocMgr2.read(docId2, new StringHandle()).get(); + System.out.println(" After Updating Document 2 : Transaction Rollback"+ content2); + + // release client + client.release(); + + } + + @Test + public void testPartialUpdateCombinationJSON() throws Exception{ + System.out.println("Running testPartialUpdateCombinationJSON"); + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + String[] filenames = {"json-original.json"}; + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "JSON"); + } + String docId = "/partial-update/json-original.json"; + + ObjectMapper mapper = new ObjectMapper(); + + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); + patchBldr.pathLanguage(PathLanguage.JSONPATH); + String content1 = docMgr.read(docId, new StringHandle()).get(); + + System.out.println("Before" + content1); + ObjectNode fragmentNode = mapper.createObjectNode(); + fragmentNode = mapper.createObjectNode(); + fragmentNode.put("insertedKey", 9); + String fragment = mapper.writeValueAsString(fragmentNode); + patchBldr.insertFragment("$.employees", Position.LAST_CHILD, fragment).delete("$.employees[2]").replaceApply("$.employees[1].firstName", patchBldr.call().concatenateAfter("Hi")); + DocumentPatchHandle patchHandle = patchBldr.build(); + docMgr.patch(docId, patchHandle); + + String content = docMgr.read(docId, new StringHandle()).get(); + + System.out.println("After" + content); + + assertTrue("fragment is not inserted", content.contains("{\"insertedKey\":9}]")); + assertTrue("fragment is not inserted", content.contains("{\"firstName\":\"AnnHi\", \"lastName\":\"Smith\"}")); + assertFalse("fragment is not deleted",content.contains("{\"firstName\":\"Bob\", \"lastName\":\"Foo\"}")); + + // release client + client.release(); + + } + + @Test + public void testPartialUpdateMetadata() throws Exception{ + System.out.println("Running testPartialUpdateMetadata"); + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + String filename = "constraint1.xml"; + writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML"); + String docId = "/partial-update/constraint1.xml"; + + //Creating Manager + XMLDocumentManager xmlDocMgr = client.newXMLDocumentManager(); + String contentMetadata = xmlDocMgr.readMetadata(docId, new StringHandle()).get(); + System.out.println(" Before Updating "+ contentMetadata); + + DocumentMetadataPatchBuilder patchBldr = xmlDocMgr.newPatchBuilder(Format.XML); + patchBldr.addCollection("/document/collection3"); + patchBldr.addPermission("admin", Capability.READ); + patchBldr.addPropertyValue("Hello","Hi"); + DocumentPatchHandle patchHandle = patchBldr.build(); + xmlDocMgr.patch(docId, patchHandle); + + String contentMetadata1 = xmlDocMgr.readMetadata(docId, new StringHandle()).get(); + System.out.println(" After Changing "+ contentMetadata1); + + //Check + assertTrue("Collection not added", contentMetadata1.contains("/document/collection3")); + assertTrue("Permission not added", contentMetadata1.contains("admin")); + assertTrue("Property not added", contentMetadata1.contains("Hi")); + + //// + //replacing Metadata Values + //// + DocumentMetadataPatchBuilder patchBldrRep = xmlDocMgr.newPatchBuilder(Format.XML); + patchBldrRep.replaceCollection("/document/collection3", "/document/collection4"); + patchBldrRep.replacePermission("admin",Capability.UPDATE); + patchBldrRep.replacePropertyValue("Hello", "Bye"); + DocumentPatchHandle patchHandleRep = patchBldrRep.build(); + xmlDocMgr.patch(docId, patchHandleRep); + String contentMetadataRep = xmlDocMgr.readMetadata(docId, new StringHandle()).get(); + System.out.println(" After Updating "+ contentMetadataRep); + + //Check + assertTrue("Collection not added", contentMetadataRep.contains("/document/collection4")); + assertTrue("Permission not added", contentMetadataRep.contains("admin")); + assertTrue("Property not added", contentMetadataRep.contains("Bye")); + + //// + //Deleting Metadata Values + //// + DocumentMetadataPatchBuilder patchBldrDel = xmlDocMgr.newPatchBuilder(Format.XML); + patchBldrDel.deleteCollection("/document/collection4"); + patchBldrDel.deletePermission("admin"); + patchBldrDel.deleteProperty("Hello"); + DocumentPatchHandle patchHandleDel = patchBldrDel.build(); + xmlDocMgr.patch(docId, patchHandleDel); + String contentMetadataDel = xmlDocMgr.readMetadata(docId, new StringHandle()).get(); + System.out.println(" After Deleting "+ contentMetadataDel); + + //Check + assertFalse("Collection not deleted", contentMetadataDel.contains("/document/collection4")); + assertFalse("Permission not deleted", contentMetadataDel.contains("admin")); + assertFalse("Property not deleted", contentMetadataDel.contains("Bye")); + + // release client + client.release(); + } + + @Test + public void testPartialUpdateXMLDscriptor() throws IOException + { + System.out.println("Running testPartialUpdateXMLDescriptor"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML"); + } + + String docId = "/partial-update/constraint1.xml"; + // create doc manager + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + + //Create Document Descriptor + DocumentDescriptor desc = docMgr.newDescriptor(docId); + DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); + patchBldr.insertFragment("/root", Position.LAST_CHILD, "2013-03-21"); + DocumentPatchHandle patchHandle = patchBldr.build(); + + docMgr.patch(desc, patchHandle); + + String content = docMgr.read(docId, new StringHandle()).get(); + + System.out.println("After"+content); + + assertTrue("fragment is not inserted", content.contains("2013-03-21")); + + // release client + client.release(); + } + + @Test + public void testPartialUpdateJSONDescriptor() throws IOException + { + System.out.println("Running testPartialUpdateJSONDescriptor"); + + String[] filenames = {"json-original.json"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "JSON"); + } + + + + ObjectMapper mapper = new ObjectMapper(); + String docId = "/partial-update/json-original.json"; + // create doc manager + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + + //Create Document Descriptor + DocumentDescriptor desc = docMgr.newDescriptor(docId); + + DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); + patchBldr.pathLanguage(PathLanguage.JSONPATH); + ObjectNode fragmentNode = mapper.createObjectNode(); + fragmentNode = mapper.createObjectNode(); + fragmentNode.put("insertedKey", 9); + String fragment = mapper.writeValueAsString(fragmentNode); + + patchBldr.insertFragment("$.employees", Position.LAST_CHILD, fragment); + DocumentPatchHandle patchHandle = patchBldr.build(); + + docMgr.patch(desc, patchHandle); + + String content = docMgr.read(docId, new StringHandle()).get(); + + System.out.println("After"+content); + + assertTrue("fragment is not inserted", content.contains("{\"insertedKey\":9}]")); + + // release client + client.release(); + } + + @Test + public void testPartialUpdateXMLDscriptorTranc() throws IOException + { + System.out.println("Running testPartialUpdateXMLDescriptorTranc"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML"); + } + + String docId = "/partial-update/constraint1.xml"; + // create doc manager + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + // create template + DocumentUriTemplate template = docMgr.newDocumentUriTemplate("xml"); + template.withDirectory(docId); + + DocumentDescriptor desc = docMgr.newDescriptor(template.getDirectory()); + DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); + patchBldr.insertFragment("/root", Position.LAST_CHILD, "2013-03-21"); + DocumentPatchHandle patchHandle = patchBldr.build(); + Transaction t = client.openTransaction("Tranc"); + docMgr.patch(desc, patchHandle, t); + t.commit(); + String content = docMgr.read(docId, new StringHandle()).get(); + + System.out.println("After"+content); + + assertTrue("fragment is not inserted", content.contains("2013-03-21")); + + // release client + client.release(); + } + + @Test + public void testPartialUpdateJSONDescriptorTranc() throws IOException + { + System.out.println("Running testPartialUpdateJSONDescriptorTranc"); + + String[] filenames = {"json-original.json"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "JSON"); + } + ObjectMapper mapper = new ObjectMapper(); + String docId = "/partial-update/json-original.json"; + // create doc manager + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + // create template + DocumentUriTemplate template = docMgr.newDocumentUriTemplate("JSON"); + template.withDirectory(docId); + //Create Document Descriptor + DocumentDescriptor desc = docMgr.newDescriptor(template.getDirectory()); + DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); + + ObjectNode fragmentNode = mapper.createObjectNode(); + fragmentNode = mapper.createObjectNode(); + fragmentNode.put("insertedKey", 9); + String fragment = mapper.writeValueAsString(fragmentNode); + patchBldr.pathLanguage(PathLanguage.JSONPATH); + patchBldr.insertFragment("$.employees", Position.LAST_CHILD, fragment); + DocumentPatchHandle patchHandle = patchBldr.build(); + // Transaction t = client.openTransaction("Tranc"); + docMgr.patch(desc, patchHandle);//,t); + // t.commit(); + String content = docMgr.read(docId, new StringHandle()).get(); + + System.out.println("After"+content); + + assertTrue("fragment is not inserted", content.contains("{\"insertedKey\":9}]")); + + // release client + client.release(); + } + + @Test + public void testPartialUpdateCardinality() throws IOException + { + System.out.println("Running testPartialUpdateCardinality"); + + String filename = "constraint1.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML"); + + String docId = "/partial-update/constraint1.xml"; + + //Creating Manager + XMLDocumentManager xmlDocMgr = client.newXMLDocumentManager(); + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + + //Inserting Node + DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); + patchBldr.insertFragment("/root", Position.LAST_CHILD, Cardinality.ONE, "2013-03-21"); + DocumentPatchHandle patchHandle = patchBldr.build(); + docMgr.patch(docId, patchHandle); + String contentBefore = xmlDocMgr.read(docId, new StringHandle()).get(); + + System.out.println(" Content after Updating with Cardinality.ONE : "+ contentBefore ); + assertTrue("Insertion Failed ", contentBefore.contains("")); + //Updating again + DocumentPatchBuilder xmlPatchBldr = xmlDocMgr.newPatchBuilder(); + DocumentPatchHandle xmlPatchForNode = xmlPatchBldr.insertFragment("/root/id", Position.BEFORE , Cardinality.ONE_OR_MORE, "1989-04-06").build(); + xmlDocMgr.patch(docId, xmlPatchForNode); + String contentAfter = xmlDocMgr.read(docId, new StringHandle()).get(); + + System.out.println("Content after Updating with Cardinality.ONE_OR_MORE" + contentAfter); + assertTrue("Insertion Failed ", contentAfter.contains("1989-04-06")); + //Updating again + DocumentPatchBuilder xmlPatchBldr1 = xmlDocMgr.newPatchBuilder(); + DocumentPatchHandle xmlPatchForNode1 = xmlPatchBldr1.insertFragment("/root/id", Position.AFTER , Cardinality.ZERO_OR_ONE, "2013-07-29").build(); + xmlDocMgr.patch(docId, xmlPatchForNode1); + contentAfter = xmlDocMgr.read(docId, new StringHandle()).get(); + + System.out.println("Content after Updating with Cardinality.ZERO_OR_ONE" + contentAfter); + assertTrue("Insertion Failed ", contentAfter.contains("2013-07-29")); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPatchCardinality.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPatchCardinality.java index 7b9495ec7..5604f90c0 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPatchCardinality.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPatchCardinality.java @@ -1,420 +1,430 @@ -package com.marklogic.javaclient; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; - -import org.custommonkey.xmlunit.*; - -import static org.junit.Assert.*; - -import java.io.IOException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.document.DocumentManager.Metadata; -import com.marklogic.client.document.DocumentMetadataPatchBuilder.Cardinality; -import com.marklogic.client.document.DocumentPatchBuilder; -import com.marklogic.client.document.DocumentPatchBuilder.Position; -import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.io.DocumentMetadataHandle; -import com.marklogic.client.io.StringHandle; -import com.marklogic.client.io.marker.DocumentPatchHandle; - -import org.custommonkey.xmlunit.XMLUnit; -import org.junit.*; - -public class TestPatchCardinality extends BasicJavaClientREST { - - private static String dbName = "TestPatchCardinalityDB"; - private static String [] fNames = {"TestPatchCardinalityDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort=8011; -@BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - } - -@After - public void testCleanUp() throws Exception - { - clearDB(restPort); - System.out.println("Running clear script"); - } - -@Test public void testOneCardinalityNegative() throws IOException - { - System.out.println("Running testOneCardinalityNegative"); - - String[] filenames = {"cardinal1.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/cardinal/", "XML"); - } - - String docId = "/cardinal/cardinal1.xml"; - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); - patchBldr.insertFragment("/root/foo", Position.AFTER, Cardinality.ONE, "added"); - DocumentPatchHandle patchHandle = patchBldr.build(); - - String exception = ""; - try - { - docMgr.patch(docId, patchHandle); - } - catch (Exception e) - { - System.out.println(e.getMessage()); - exception = e.getMessage(); - } - - String expectedException = "Local message: write failed: Bad Request. Server Message: RESTAPI-INVALIDREQ: (err:FOER0000) Invalid request: reason: invalid content patch operations for uri /cardinal/cardinal1.xml: invalid cardinality of 5 nodes for: /root/foo"; - - assertTrue("Exception is not thrown", exception.contains(expectedException)); - - // release client - client.release(); - } - -@Test public void testOneCardinalityPositve() throws IOException - { - System.out.println("Running testOneCardinalityPositive"); - - String[] filenames = {"cardinal2.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/cardinal/", "XML"); - } - - String docId = "/cardinal/cardinal2.xml"; - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); - patchBldr.insertFragment("/root/foo", Position.AFTER, Cardinality.ONE, "added"); - DocumentPatchHandle patchHandle = patchBldr.build(); - docMgr.patch(docId, patchHandle); - - String content = docMgr.read(docId, new StringHandle()).get(); - - System.out.println(content); - - assertTrue("fragment is not inserted", content.contains("added")); - - // release client - client.release(); - } - -@Test public void testOneOrMoreCardinalityPositve() throws IOException - { - System.out.println("Running testOneOrMoreCardinalityPositive"); - - String[] filenames = {"cardinal1.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/cardinal/", "XML"); - } - - String docId = "/cardinal/cardinal1.xml"; - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); - patchBldr.insertFragment("/root/foo", Position.AFTER, Cardinality.ONE_OR_MORE, "added"); - DocumentPatchHandle patchHandle = patchBldr.build(); - docMgr.patch(docId, patchHandle); - - String content = docMgr.read(docId, new StringHandle()).get(); - - System.out.println(content); - - assertTrue("fragment is not inserted", content.contains("oneadded")); - assertTrue("fragment is not inserted", content.contains("twoadded")); - assertTrue("fragment is not inserted", content.contains("threeadded")); - assertTrue("fragment is not inserted", content.contains("fouradded")); - assertTrue("fragment is not inserted", content.contains("fiveadded")); - - // release client - client.release(); - } - -@Test public void testOneOrMoreCardinalityNegative() throws IOException - { - System.out.println("Running testOneOrMoreCardinalityNegative"); - - String[] filenames = {"cardinal3.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/cardinal/", "XML"); - } - - String docId = "/cardinal/cardinal3.xml"; - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); - patchBldr.insertFragment("/root/foo", Position.AFTER, Cardinality.ONE_OR_MORE, "added"); - DocumentPatchHandle patchHandle = patchBldr.build(); - - String exception = ""; - try - { - docMgr.patch(docId, patchHandle); - } - catch (Exception e) - { - System.out.println(e.getMessage()); - exception = e.getMessage(); - } - - String expectedException = "Local message: write failed: Bad Request. Server Message: RESTAPI-INVALIDREQ: (err:FOER0000) Invalid request: reason: invalid content patch operations for uri /cardinal/cardinal3.xml: invalid cardinality of 0 nodes for: /root/foo"; - - assertTrue("Exception is not thrown", exception.contains(expectedException)); - - // release client - client.release(); - } - -@Test public void testZeroOrOneCardinalityNegative() throws IOException - { - System.out.println("Running testZeroOrOneCardinalityNegative"); - - String[] filenames = {"cardinal1.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/cardinal/", "XML"); - } - - String docId = "/cardinal/cardinal1.xml"; - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); - patchBldr.insertFragment("/root/foo", Position.AFTER, Cardinality.ZERO_OR_ONE, "added"); - DocumentPatchHandle patchHandle = patchBldr.build(); - - String exception = ""; - try - { - docMgr.patch(docId, patchHandle); - } - catch (Exception e) - { - System.out.println(e.getMessage()); - exception = e.getMessage(); - } - - String expectedException = "Local message: write failed: Bad Request. Server Message: RESTAPI-INVALIDREQ: (err:FOER0000) Invalid request: reason: invalid content patch operations for uri /cardinal/cardinal1.xml: invalid cardinality of 5 nodes for: /root/foo"; - - assertTrue("Exception is not thrown", exception.contains(expectedException)); - - // release client - client.release(); - } - - -@Test public void testZeroOrOneCardinalityPositive() throws IOException - { - System.out.println("Running testZeroOrOneCardinalityPositive"); - - String[] filenames = {"cardinal2.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/cardinal/", "XML"); - } - - String docId = "/cardinal/cardinal2.xml"; - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); - patchBldr.insertFragment("/root/foo", Position.AFTER, Cardinality.ZERO_OR_ONE, "added"); - DocumentPatchHandle patchHandle = patchBldr.build(); - docMgr.patch(docId, patchHandle); - - String content = docMgr.read(docId, new StringHandle()).get(); - - System.out.println(content); - - assertTrue("fragment is not inserted", content.contains("oneadded")); - - // release client - client.release(); - } - - -@Test public void testZeroOrOneCardinalityPositiveWithZero() throws IOException - { - System.out.println("Running testZeroOrOneCardinalityPositiveWithZero"); - - String[] filenames = {"cardinal3.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/cardinal/", "XML"); - } - - String docId = "/cardinal/cardinal3.xml"; - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); - patchBldr.insertFragment("/root/foo", Position.AFTER, Cardinality.ZERO_OR_ONE, "added"); - DocumentPatchHandle patchHandle = patchBldr.build(); - docMgr.patch(docId, patchHandle); - - String content = docMgr.read(docId, new StringHandle()).get(); - - System.out.println(content); - - assertFalse("fragment is inserted", content.contains("oneadded")); - - // release client - client.release(); - } - -@Test public void testZeroOrMoreCardinality() throws IOException - { - System.out.println("Running testZeroOrMoreCardinality"); - - String[] filenames = {"cardinal1.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/cardinal/", "XML"); - } - - String docId = "/cardinal/cardinal1.xml"; - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); - patchBldr.insertFragment("/root/foo", Position.AFTER, Cardinality.ZERO_OR_MORE, "added"); - DocumentPatchHandle patchHandle = patchBldr.build(); - docMgr.patch(docId, patchHandle); - - String content = docMgr.read(docId, new StringHandle()).get(); - - System.out.println(content); - - assertTrue("fragment is not inserted", content.contains("oneadded")); - assertTrue("fragment is not inserted", content.contains("twoadded")); - assertTrue("fragment is not inserted", content.contains("threeadded")); - assertTrue("fragment is not inserted", content.contains("fouradded")); - assertTrue("fragment is not inserted", content.contains("fiveadded")); - - // release client - client.release(); - } - -@Test public void testBug23843() throws IOException - { - System.out.println("Running testBug23843"); - - String[] filenames = {"cardinal1.xml","cardinal4.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/cardinal/", "XML"); - - String docId = ""; - - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - - DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); - if (filename == "cardinal1.xml"){ - patchBldr.insertFragment("/root", Position.LAST_CHILD, Cardinality.ONE, "added"); - } - else if (filename == "cardinal4.xml") { - patchBldr.insertFragment("/root", Position.LAST_CHILD, "added"); - } - DocumentPatchHandle patchHandle = patchBldr.build(); - String RawPatch = patchHandle.toString(); - System.out.println("Before"+RawPatch); - - if (filename == "cardinal1.xml"){ - try - { docId= "/cardinal/cardinal1.xml"; - docMgr.patch(docId, patchHandle); - - String actual = docMgr.readMetadata(docId, new DocumentMetadataHandle()).toString(); - System.out.println("Actual" + actual); - - assertXpathEvaluatesTo("2", "count(/*[local-name()='metadata']/*[local-name()='permissions']/*[local-name()='permission'])", actual); - assertXpathEvaluatesTo("1", "count(/*[local-name()='metadata']/*[local-name()='permissions']/*[local-name()='permission']/*[local-name()='role-name' and string(.)='rest-reader'])", actual); - assertXpathEvaluatesTo("1", "count(/*[local-name()='metadata']/*[local-name()='permissions']/*[local-name()='permission']/*[local-name()='role-name' and string(.)='rest-writer'])", actual); - assertXpathEvaluatesTo("1","count(/*[local-name()='metadata']/*[local-name()='quality' and string(.)='0'])", actual); - - XMLUnit.getControlDocumentBuilderFactory().setNamespaceAware(false); - XMLAssert.assertXpathEvaluatesTo("rest-readerread", "//permissions/permission[role-name[. = 'rest-reader'] and capability[. = 'read']]", actual); - XMLAssert.assertXpathEvaluatesTo("rest-writerupdate", "//permissions/permission[role-name[. = 'rest-writer'] and capability[. = 'update']]", actual); - } - catch (Exception e) - { - System.out.println(e.getMessage()); - } - } - else if (filename == "cardinal4.xml") { - try - { - docId = "/cardinal/cardinal4.xml"; - docMgr.clearMetadataCategories(); - docMgr.patch(docId, new StringHandle(patchHandle.toString())); - docMgr.setMetadataCategories(Metadata.ALL); - - String actual = docMgr.readMetadata(docId, new DocumentMetadataHandle()).toString(); - System.out.println("Actual" + actual); - - assertXpathEvaluatesTo("2", "count(/*[local-name()='metadata']/*[local-name()='permissions']/*[local-name()='permission'])", actual); - assertXpathEvaluatesTo("1", "count(/*[local-name()='metadata']/*[local-name()='permissions']/*[local-name()='permission']/*[local-name()='role-name' and string(.)='rest-reader'])", actual); - assertXpathEvaluatesTo("1", "count(/*[local-name()='metadata']/*[local-name()='permissions']/*[local-name()='permission']/*[local-name()='role-name' and string(.)='rest-writer'])", actual); - assertXpathEvaluatesTo("1","count(/*[local-name()='metadata']/*[local-name()='quality' and string(.)='0'])", actual); - - XMLUnit.getControlDocumentBuilderFactory().setNamespaceAware(false); - XMLAssert.assertXpathEvaluatesTo("rest-readerread", "//permissions/permission[role-name[. = 'rest-reader'] and capability[. = 'read']]", actual); - XMLAssert.assertXpathEvaluatesTo("rest-writerupdate", "//permissions/permission[role-name[. = 'rest-writer'] and capability[. = 'update']]", actual); - } - catch (Exception e) - { - System.out.println(e.getMessage()); - } - } - - String actual = docMgr.read(docId, new StringHandle()).get(); - - System.out.println("Actual : "+actual); - } - - // release client - client.release(); - } -@AfterClass -public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} +package com.marklogic.javaclient; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; + +import org.custommonkey.xmlunit.*; + +import static org.junit.Assert.*; + +import java.io.IOException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.DocumentManager.Metadata; +import com.marklogic.client.document.DocumentMetadataPatchBuilder.Cardinality; +import com.marklogic.client.document.DocumentPatchBuilder; +import com.marklogic.client.document.DocumentPatchBuilder.Position; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.io.DocumentMetadataHandle; +import com.marklogic.client.io.StringHandle; +import com.marklogic.client.io.marker.DocumentPatchHandle; + +import org.custommonkey.xmlunit.XMLUnit; +import org.junit.*; + +public class TestPatchCardinality extends BasicJavaClientREST { + + private static String dbName = "TestPatchCardinalityDB"; + private static String [] fNames = {"TestPatchCardinalityDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort=8011; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + } + + @After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + + @Test + public void testOneCardinalityNegative() throws IOException + { + System.out.println("Running testOneCardinalityNegative"); + + String[] filenames = {"cardinal1.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/cardinal/", "XML"); + } + + String docId = "/cardinal/cardinal1.xml"; + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); + patchBldr.insertFragment("/root/foo", Position.AFTER, Cardinality.ONE, "added"); + DocumentPatchHandle patchHandle = patchBldr.build(); + + String exception = ""; + try + { + docMgr.patch(docId, patchHandle); + } + catch (Exception e) + { + System.out.println(e.getMessage()); + exception = e.getMessage(); + } + + String expectedException = "Local message: write failed: Bad Request. Server Message: RESTAPI-INVALIDREQ: (err:FOER0000) Invalid request: reason: invalid content patch operations for uri /cardinal/cardinal1.xml: invalid cardinality of 5 nodes for: /root/foo"; + + assertTrue("Exception is not thrown", exception.contains(expectedException)); + + // release client + client.release(); + } + + @Test + public void testOneCardinalityPositve() throws IOException + { + System.out.println("Running testOneCardinalityPositive"); + + String[] filenames = {"cardinal2.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/cardinal/", "XML"); + } + + String docId = "/cardinal/cardinal2.xml"; + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); + patchBldr.insertFragment("/root/foo", Position.AFTER, Cardinality.ONE, "added"); + DocumentPatchHandle patchHandle = patchBldr.build(); + docMgr.patch(docId, patchHandle); + + String content = docMgr.read(docId, new StringHandle()).get(); + + System.out.println(content); + + assertTrue("fragment is not inserted", content.contains("added")); + + // release client + client.release(); + } + + @Test + public void testOneOrMoreCardinalityPositve() throws IOException + { + System.out.println("Running testOneOrMoreCardinalityPositive"); + + String[] filenames = {"cardinal1.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/cardinal/", "XML"); + } + + String docId = "/cardinal/cardinal1.xml"; + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); + patchBldr.insertFragment("/root/foo", Position.AFTER, Cardinality.ONE_OR_MORE, "added"); + DocumentPatchHandle patchHandle = patchBldr.build(); + docMgr.patch(docId, patchHandle); + + String content = docMgr.read(docId, new StringHandle()).get(); + + System.out.println(content); + + assertTrue("fragment is not inserted", content.contains("oneadded")); + assertTrue("fragment is not inserted", content.contains("twoadded")); + assertTrue("fragment is not inserted", content.contains("threeadded")); + assertTrue("fragment is not inserted", content.contains("fouradded")); + assertTrue("fragment is not inserted", content.contains("fiveadded")); + + // release client + client.release(); + } + + @Test + public void testOneOrMoreCardinalityNegative() throws IOException + { + System.out.println("Running testOneOrMoreCardinalityNegative"); + + String[] filenames = {"cardinal3.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/cardinal/", "XML"); + } + + String docId = "/cardinal/cardinal3.xml"; + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); + patchBldr.insertFragment("/root/foo", Position.AFTER, Cardinality.ONE_OR_MORE, "added"); + DocumentPatchHandle patchHandle = patchBldr.build(); + + String exception = ""; + try + { + docMgr.patch(docId, patchHandle); + } + catch (Exception e) + { + System.out.println(e.getMessage()); + exception = e.getMessage(); + } + + String expectedException = "Local message: write failed: Bad Request. Server Message: RESTAPI-INVALIDREQ: (err:FOER0000) Invalid request: reason: invalid content patch operations for uri /cardinal/cardinal3.xml: invalid cardinality of 0 nodes for: /root/foo"; + + assertTrue("Exception is not thrown", exception.contains(expectedException)); + + // release client + client.release(); + } + + @Test + public void testZeroOrOneCardinalityNegative() throws IOException + { + System.out.println("Running testZeroOrOneCardinalityNegative"); + + String[] filenames = {"cardinal1.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/cardinal/", "XML"); + } + + String docId = "/cardinal/cardinal1.xml"; + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); + patchBldr.insertFragment("/root/foo", Position.AFTER, Cardinality.ZERO_OR_ONE, "added"); + DocumentPatchHandle patchHandle = patchBldr.build(); + + String exception = ""; + try + { + docMgr.patch(docId, patchHandle); + } + catch (Exception e) + { + System.out.println(e.getMessage()); + exception = e.getMessage(); + } + + String expectedException = "Local message: write failed: Bad Request. Server Message: RESTAPI-INVALIDREQ: (err:FOER0000) Invalid request: reason: invalid content patch operations for uri /cardinal/cardinal1.xml: invalid cardinality of 5 nodes for: /root/foo"; + + assertTrue("Exception is not thrown", exception.contains(expectedException)); + + // release client + client.release(); + } + + + @Test + public void testZeroOrOneCardinalityPositive() throws IOException + { + System.out.println("Running testZeroOrOneCardinalityPositive"); + + String[] filenames = {"cardinal2.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/cardinal/", "XML"); + } + + String docId = "/cardinal/cardinal2.xml"; + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); + patchBldr.insertFragment("/root/foo", Position.AFTER, Cardinality.ZERO_OR_ONE, "added"); + DocumentPatchHandle patchHandle = patchBldr.build(); + docMgr.patch(docId, patchHandle); + + String content = docMgr.read(docId, new StringHandle()).get(); + + System.out.println(content); + + assertTrue("fragment is not inserted", content.contains("oneadded")); + + // release client + client.release(); + } + + @Test + public void testZeroOrOneCardinalityPositiveWithZero() throws IOException + { + System.out.println("Running testZeroOrOneCardinalityPositiveWithZero"); + + String[] filenames = {"cardinal3.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/cardinal/", "XML"); + } + + String docId = "/cardinal/cardinal3.xml"; + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); + patchBldr.insertFragment("/root/foo", Position.AFTER, Cardinality.ZERO_OR_ONE, "added"); + DocumentPatchHandle patchHandle = patchBldr.build(); + docMgr.patch(docId, patchHandle); + + String content = docMgr.read(docId, new StringHandle()).get(); + + System.out.println(content); + + assertFalse("fragment is inserted", content.contains("oneadded")); + + // release client + client.release(); + } + + @Test + public void testZeroOrMoreCardinality() throws IOException + { + System.out.println("Running testZeroOrMoreCardinality"); + + String[] filenames = {"cardinal1.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/cardinal/", "XML"); + } + + String docId = "/cardinal/cardinal1.xml"; + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); + patchBldr.insertFragment("/root/foo", Position.AFTER, Cardinality.ZERO_OR_MORE, "added"); + DocumentPatchHandle patchHandle = patchBldr.build(); + docMgr.patch(docId, patchHandle); + + String content = docMgr.read(docId, new StringHandle()).get(); + + System.out.println(content); + + assertTrue("fragment is not inserted", content.contains("oneadded")); + assertTrue("fragment is not inserted", content.contains("twoadded")); + assertTrue("fragment is not inserted", content.contains("threeadded")); + assertTrue("fragment is not inserted", content.contains("fouradded")); + assertTrue("fragment is not inserted", content.contains("fiveadded")); + + // release client + client.release(); + } + + @Test + public void testBug23843() throws IOException + { + System.out.println("Running testBug23843"); + + String[] filenames = {"cardinal1.xml","cardinal4.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/cardinal/", "XML"); + + String docId = ""; + + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + + DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); + if (filename == "cardinal1.xml"){ + patchBldr.insertFragment("/root", Position.LAST_CHILD, Cardinality.ONE, "added"); + } + else if (filename == "cardinal4.xml") { + patchBldr.insertFragment("/root", Position.LAST_CHILD, "added"); + } + DocumentPatchHandle patchHandle = patchBldr.build(); + String RawPatch = patchHandle.toString(); + System.out.println("Before"+RawPatch); + + if (filename == "cardinal1.xml"){ + try + { docId= "/cardinal/cardinal1.xml"; + docMgr.patch(docId, patchHandle); + + String actual = docMgr.readMetadata(docId, new DocumentMetadataHandle()).toString(); + System.out.println("Actual" + actual); + + assertXpathEvaluatesTo("2", "count(/*[local-name()='metadata']/*[local-name()='permissions']/*[local-name()='permission'])", actual); + assertXpathEvaluatesTo("1", "count(/*[local-name()='metadata']/*[local-name()='permissions']/*[local-name()='permission']/*[local-name()='role-name' and string(.)='rest-reader'])", actual); + assertXpathEvaluatesTo("1", "count(/*[local-name()='metadata']/*[local-name()='permissions']/*[local-name()='permission']/*[local-name()='role-name' and string(.)='rest-writer'])", actual); + assertXpathEvaluatesTo("1","count(/*[local-name()='metadata']/*[local-name()='quality' and string(.)='0'])", actual); + + XMLUnit.getControlDocumentBuilderFactory().setNamespaceAware(false); + XMLAssert.assertXpathEvaluatesTo("rest-readerread", "//permissions/permission[role-name[. = 'rest-reader'] and capability[. = 'read']]", actual); + XMLAssert.assertXpathEvaluatesTo("rest-writerupdate", "//permissions/permission[role-name[. = 'rest-writer'] and capability[. = 'update']]", actual); + } + catch (Exception e) + { + System.out.println(e.getMessage()); + } + } + else if (filename == "cardinal4.xml") { + try + { + docId = "/cardinal/cardinal4.xml"; + docMgr.clearMetadataCategories(); + docMgr.patch(docId, new StringHandle(patchHandle.toString())); + docMgr.setMetadataCategories(Metadata.ALL); + + String actual = docMgr.readMetadata(docId, new DocumentMetadataHandle()).toString(); + System.out.println("Actual" + actual); + + assertXpathEvaluatesTo("2", "count(/*[local-name()='metadata']/*[local-name()='permissions']/*[local-name()='permission'])", actual); + assertXpathEvaluatesTo("1", "count(/*[local-name()='metadata']/*[local-name()='permissions']/*[local-name()='permission']/*[local-name()='role-name' and string(.)='rest-reader'])", actual); + assertXpathEvaluatesTo("1", "count(/*[local-name()='metadata']/*[local-name()='permissions']/*[local-name()='permission']/*[local-name()='role-name' and string(.)='rest-writer'])", actual); + assertXpathEvaluatesTo("1","count(/*[local-name()='metadata']/*[local-name()='quality' and string(.)='0'])", actual); + + XMLUnit.getControlDocumentBuilderFactory().setNamespaceAware(false); + XMLAssert.assertXpathEvaluatesTo("rest-readerread", "//permissions/permission[role-name[. = 'rest-reader'] and capability[. = 'read']]", actual); + XMLAssert.assertXpathEvaluatesTo("rest-writerupdate", "//permissions/permission[role-name[. = 'rest-writer'] and capability[. = 'update']]", actual); + } + catch (Exception e) + { + System.out.println(e.getMessage()); + } + } + + String actual = docMgr.read(docId, new StringHandle()).get(); + + System.out.println("Actual : "+actual); + } + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryByExample.java b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryByExample.java index 989eda4ff..39514bdec 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryByExample.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryByExample.java @@ -1,478 +1,482 @@ -package com.marklogic.javaclient; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; - -import javax.xml.transform.TransformerException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.FailedRequestException; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.admin.ServerConfigurationManager; -import com.marklogic.client.document.DocumentMetadataPatchBuilder; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.FileHandle; -import com.marklogic.client.io.Format; -import com.marklogic.client.io.SearchHandle; -import com.marklogic.client.io.StringHandle; -import com.marklogic.client.query.MatchDocumentSummary; -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.query.RawCombinedQueryDefinition; -import com.marklogic.client.query.RawQueryByExampleDefinition; -import com.sun.jersey.api.client.ClientHandlerException; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; -import static org.junit.Assert.*; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.w3c.dom.Document; -import org.junit.*; -public class TestQueryByExample extends BasicJavaClientREST { - - private static String dbName = "TestQueryByExampleDB"; - private static String [] fNames = {"TestQueryByExampleDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort=8011; -@BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - } - -@After - public void testCleanUp() throws Exception - { - clearDB(restPort); - System.out.println("Running clear script"); - } - -@SuppressWarnings("deprecation") -@Test - public void testQueryByExampleXML() throws IOException, TransformerException, XpathException - { - System.out.println("Running testQueryByExampleXML"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "XML"); - } - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/qbe/qbe1.xml"); - - String qbeQuery = convertFileToString(file); - StringHandle qbeHandle = new StringHandle(qbeQuery); - qbeHandle.setFormat(Format.XML); - - QueryManager queryMgr = client.newQueryManager(); - - RawQueryByExampleDefinition qbyex = queryMgr.newRawQueryByExampleDefinition(qbeHandle); - - Document resultDoc = queryMgr.search(qbyex, new DOMHandle()).get(); - - System.out.println("XML Result"+convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("0011", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testQueryByExampleXMLnew() throws IOException, TransformerException, XpathException - { - System.out.println("Running testQueryByExampleXML"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "XML"); - } - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/qbe/qbe1.xml"); - - String qbeQuery = convertFileToString(file); - StringHandle qbeHandle = new StringHandle(qbeQuery); - qbeHandle.setFormat(Format.XML); - - - QueryManager queryMgr = client.newQueryManager(); - - RawQueryByExampleDefinition qbyex = queryMgr.newRawQueryByExampleDefinition(qbeHandle); - Document resultDoc = queryMgr.search(qbyex, new DOMHandle()).get(); - - System.out.println("XML Result"+convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("0011", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testQueryByExampleJSON() throws IOException - { - System.out.println("Running testQueryByExampleJSON"); - - String[] filenames = {"constraint1.json", "constraint2.json", "constraint3.json", "constraint4.json", "constraint5.json"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "JSON"); - } - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/qbe/qbe1.json"); - - String qbeQuery = convertFileToString(file); - StringHandle qbeHandle = new StringHandle(qbeQuery); - qbeHandle.setFormat(Format.JSON); - - QueryManager queryMgr = client.newQueryManager(); - - RawQueryByExampleDefinition qbyex = queryMgr.newRawQueryByExampleDefinition(qbeHandle); - - String resultDoc = queryMgr.search(qbyex, new StringHandle()).get(); - - System.out.println("testQueryByExampleJSON Result : "+resultDoc); - - - assertTrue("doc returned is not correct", resultDoc.contains("")); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testBug22179() throws IOException - { - System.out.println("Running testBug22179"); - - String[] filenames = {"constraint1.json", "constraint2.json", "constraint3.json", "constraint4.json", "constraint5.json"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "JSON"); - } - - ServerConfigurationManager confMgr = client.newServerConfigManager(); - confMgr.setQueryValidation(true); - - String combinedCriteria ="{\"search\":{\"options\":{\"constraint\":[{\"name\":\"para\", \"word\":{\"term-option\":[\"case-insensitive\"], \"field\":{\"name\":\"para\"}}},{\"name\":\"id\", \"value\":{\"element\":{\"ns\":\"\", \"name\":\"id\"}}}], \"return-metrics\":false, \"debug\":true, \"return-qtext\":false, \"transform-results\":{\"apply\":\"snippet\"}}, \"query\":{\"queries\":[{\"or-query\":{\"queries\":[{\"and-query\":{\"queries\":[{\"word-constraint-query\":{\"text\":[\"Bush\"], \"constraint-name\":\"para\"}},{\"not-query\":{\"word-constraint-query\":{\"text\":[\"memex\"], \"constraint-name\":\"para\"}}}]}},{\"and-query\":{\"queries\":[{\"value-constraint-query\":{\"text\":[\"0026\"], \"constraint-name\":\"id\"}},{\"term-query\":{\"text\":[\"memex\"]}}]}}]}}]}}}"; - QueryManager queryMgr = client.newQueryManager(); - - StringHandle combinedHandle = new StringHandle(combinedCriteria).withFormat(Format.JSON); - RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(combinedHandle); - String output = queryMgr.search(querydef, new StringHandle()).get(); - System.out.println(output); - assertTrue(output.contains("(cts:search(fn:collection(), cts:or-query((cts:and-query((cts:field-word-query")); - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testQueryByExampleXMLPayload() throws IOException, TransformerException, XpathException - { - System.out.println("Running testQueryByExampleXMLPayload"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "XML"); - } - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/qbe/qbe1.xml"); - FileHandle fileHandle = new FileHandle(file); - QueryManager queryMgr = client.newQueryManager(); - - RawQueryByExampleDefinition rw = queryMgr.newRawQueryByExampleDefinition(fileHandle.withFormat(Format.XML)); - SearchHandle results = queryMgr.search(rw, new SearchHandle()); - - for (MatchDocumentSummary result : results.getMatchResults()) - { - System.out.println(result.getUri()+ ": Uri"); - assertEquals("Wrong Document Searched",result.getUri() , "/qbe/constraint1.xml"); - } - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testQueryByExampleJSONPayload() throws IOException, Exception - { - System.out.println("Running testQueryByExampleJSONPayload"); - - String[] filenames = {"constraint1.json", "constraint2.json", "constraint3.json", "constraint4.json", "constraint5.json"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "JSON"); - } - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/qbe/qbe1.json"); - FileHandle fileHandle = new FileHandle(file); - - QueryManager queryMgr = client.newQueryManager(); - RawQueryByExampleDefinition qbyex = queryMgr.newRawQueryByExampleDefinition(fileHandle.withFormat(Format.JSON)); - String resultDoc = queryMgr.search(qbyex, new StringHandle()).get(); - System.out.println(resultDoc); - assertTrue("Result is not proper", resultDoc.contains("/qbe/constraint1.json")); - - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testQueryByExampleXMLPermission() throws IOException, TransformerException, XpathException - { - System.out.println("Running testQueryByExampleXMLPermission"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "XML"); - } - - // get the combined query - try{ - File file = new File("src/test/java/com/marklogic/javaclient/qbe/qbe2.xml"); - - FileHandle fileHandle = new FileHandle(file); - QueryManager queryMgr = client.newQueryManager(); - - RawQueryByExampleDefinition rw = queryMgr.newRawQueryByExampleDefinition(fileHandle.withFormat(Format.XML)); - SearchHandle results = queryMgr.search(rw, new SearchHandle()); - - for (MatchDocumentSummary result : results.getMatchResults()) - { - System.out.println(result.getUri()+ ": Uri"); - assertEquals("Wrong Document Searched",result.getUri() , "/qbe/constraint1.xml"); - } - }catch(ClientHandlerException e){ - System.out.println("Negative Test Passed of executing nonreadable file"); - } - // release client - client.release(); - - } - -@SuppressWarnings("deprecation") -@Test public void testQueryByExampleWrongXML() throws IOException, TransformerException, XpathException - { - System.out.println("Running testQueryByExampleXMLPayload"); - - String filename = "WrongFormat.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - try{ - // write docs - writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "XML"); - - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/qbe/qbe1.xml"); - FileHandle fileHandle = new FileHandle(file); - QueryManager queryMgr = client.newQueryManager(); - - RawQueryByExampleDefinition rw = queryMgr.newRawQueryByExampleDefinition(fileHandle.withFormat(Format.XML)); - SearchHandle results = queryMgr.search(rw, new SearchHandle()); - - for (MatchDocumentSummary result : results.getMatchResults()) - { - System.out.println(result.getUri()+ ": Uri"); - // assertEquals("Wrong Document Searched",result.getUri() , "/qbe/constraint1.xml"); - } - }catch(FailedRequestException e){ - System.out.println("Negative test passed as XML with invalid structure gave FailedRequestException "); - } - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testQueryByExampleWrongJSON() throws IOException - { - System.out.println("Running testQueryByExampleJSON"); - - String filename = "WrongFormat.json"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - try{ - // write docs - writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "JSON"); - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/qbe/qbe1.json"); - - String qbeQuery = convertFileToString(file); - StringHandle qbeHandle = new StringHandle(qbeQuery); - qbeHandle.setFormat(Format.JSON); - - QueryManager queryMgr = client.newQueryManager(); - - RawQueryByExampleDefinition qbyex = queryMgr.newRawQueryByExampleDefinition(qbeHandle); - - String resultDoc = queryMgr.search(qbyex, new StringHandle()).get(); - - System.out.println(resultDoc); - - assertTrue("total result is not correct", resultDoc.contains("\"total\":1")); - assertTrue("doc returned is not correct", resultDoc.contains("\"metadata\":[{\"title\":\"Vannevar Bush\"},{\"id\":11},{\"p\":\"Vannevar Bush wrote an article for The Atlantic Monthly\"},{\"popularity\":5}]")); - }catch(FailedRequestException e){ - System.out.println("Negative test passed as JSON with invalid structure gave FailedRequestException "); - } - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testQueryByExampleXMLWrongQuery() throws IOException, TransformerException, XpathException - { - System.out.println("Running testQueryByExampleXMLWrongQuery"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "XML"); - } - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/qbe/qbe1.xml"); - FileHandle fileHandle = new FileHandle(file); - QueryManager queryMgr = client.newQueryManager(); - - RawQueryByExampleDefinition rw = queryMgr.newRawQueryByExampleDefinition(fileHandle.withFormat(Format.XML)); - SearchHandle results = queryMgr.search(rw, new SearchHandle()); - - for (MatchDocumentSummary result : results.getMatchResults()) - { - System.out.println(result.getUri()+ ": Uri"); - assertEquals("Wrong Document Searched",result.getUri() , "/qbe/constraint1.xml"); - } - try{ - File wrongFile = new File("src/test/java/com/marklogic/javaclient/qbe/WrongQbe.xml"); - FileHandle wrongFileHandle = new FileHandle(wrongFile); - QueryManager newQueryMgr = client.newQueryManager(); - - RawQueryByExampleDefinition newRw = newQueryMgr.newRawQueryByExampleDefinition(wrongFileHandle.withFormat(Format.XML)); - SearchHandle newResults = queryMgr.search(newRw, new SearchHandle()); - - for (MatchDocumentSummary result : newResults.getMatchResults()) - { - System.out.println(result.getUri()+ ": Uri"); - assertEquals("Wrong Document Searched",result.getUri() , "/qbe/constraint1.xml"); - } - }catch(FailedRequestException e){ - System.out.println("Negative test passed as Query with improper Xml format gave FailedRequestException "); - } - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testQueryByExampleJSONWrongQuery() throws IOException - { - System.out.println("Running testQueryByExampleJSONWrongQuery"); - - String[] filenames = {"constraint1.json", "constraint2.json", "constraint3.json", "constraint4.json", "constraint5.json"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "JSON"); - } - - // get the Correct query - File file = new File("src/test/java/com/marklogic/javaclient/qbe/qbe1.json"); - - String qbeQuery = convertFileToString(file); - StringHandle qbeHandle = new StringHandle(qbeQuery); - qbeHandle.setFormat(Format.JSON); - - QueryManager queryMgr = client.newQueryManager(); - - RawQueryByExampleDefinition qbyex = queryMgr.newRawQueryByExampleDefinition(qbeHandle); - - String resultDoc = queryMgr.search(qbyex, new StringHandle()).get(); - - System.out.println("Result of Correct Query"+ resultDoc); - - //assertTrue("total result is not correct", resultDoc.contains("\"total\":1")); - // assertTrue("doc returned is not correct", resultDoc.contains("\"metadata\":[{\"title\":\"Vannevar Bush\"},{\"id\":11},{\"p\":\"Vannevar Bush wrote an article for The Atlantic Monthly\"},{\"popularity\":5}]")); - - // get the query with Wrong Format - - File wrongFile = new File("src/test/java/com/marklogic/javaclient/qbe/WrongQbe.json"); - - String wrongQbeQuery = convertFileToString(wrongFile); - StringHandle newQbeHandle = new StringHandle(wrongQbeQuery); - newQbeHandle.setFormat(Format.JSON); - - QueryManager newQueryMgr = client.newQueryManager(); - - RawQueryByExampleDefinition newQbyex = newQueryMgr.newRawQueryByExampleDefinition(newQbeHandle); - try{ - String newResultDoc = newQueryMgr.search(newQbyex, new StringHandle()).get(); - - System.out.println("Result of Wrong Query"+newResultDoc); - - assertTrue("total result is not correct", resultDoc.contains("\"total\":1")); - assertTrue("doc returned is not correct", resultDoc.contains("\"metadata\":[{\"title\":\"Vannevar Bush\"},{\"id\":11},{\"p\":\"Vannevar Bush wrote an article for The Atlantic Monthly\"},{\"popularity\":5}]")); - }catch(FailedRequestException e){ - System.out.println("Negative test passed as Query with improper JSON format gave FailedRequestException "); - } - // release client - client.release(); - } -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } -} +package com.marklogic.javaclient; + +import java.io.File; +import java.io.IOException; + +import javax.xml.transform.TransformerException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.FailedRequestException; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.admin.ServerConfigurationManager; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.FileHandle; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.SearchHandle; +import com.marklogic.client.io.StringHandle; +import com.marklogic.client.query.MatchDocumentSummary; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.RawCombinedQueryDefinition; +import com.marklogic.client.query.RawQueryByExampleDefinition; +import com.sun.jersey.api.client.ClientHandlerException; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; +import static org.junit.Assert.*; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.w3c.dom.Document; +import org.junit.*; + +public class TestQueryByExample extends BasicJavaClientREST { + private static String dbName = "TestQueryByExampleDB"; + private static String [] fNames = {"TestQueryByExampleDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort=8011; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + } + + @After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + + @Test + public void testQueryByExampleXML() throws IOException, TransformerException, XpathException + { + System.out.println("Running testQueryByExampleXML"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "XML"); + } + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/qbe/qbe1.xml"); + + String qbeQuery = convertFileToString(file); + StringHandle qbeHandle = new StringHandle(qbeQuery); + qbeHandle.setFormat(Format.XML); + + QueryManager queryMgr = client.newQueryManager(); + + RawQueryByExampleDefinition qbyex = queryMgr.newRawQueryByExampleDefinition(qbeHandle); + + Document resultDoc = queryMgr.search(qbyex, new DOMHandle()).get(); + + System.out.println("XML Result"+convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("0011", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testQueryByExampleXMLnew() throws IOException, TransformerException, XpathException + { + System.out.println("Running testQueryByExampleXML"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "XML"); + } + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/qbe/qbe1.xml"); + + String qbeQuery = convertFileToString(file); + StringHandle qbeHandle = new StringHandle(qbeQuery); + qbeHandle.setFormat(Format.XML); + + + QueryManager queryMgr = client.newQueryManager(); + + RawQueryByExampleDefinition qbyex = queryMgr.newRawQueryByExampleDefinition(qbeHandle); + Document resultDoc = queryMgr.search(qbyex, new DOMHandle()).get(); + + System.out.println("XML Result"+convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("0011", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testQueryByExampleJSON() throws IOException + { + System.out.println("Running testQueryByExampleJSON"); + + String[] filenames = {"constraint1.json", "constraint2.json", "constraint3.json", "constraint4.json", "constraint5.json"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "JSON"); + } + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/qbe/qbe1.json"); + + String qbeQuery = convertFileToString(file); + StringHandle qbeHandle = new StringHandle(qbeQuery); + qbeHandle.setFormat(Format.JSON); + + QueryManager queryMgr = client.newQueryManager(); + + RawQueryByExampleDefinition qbyex = queryMgr.newRawQueryByExampleDefinition(qbeHandle); + + String resultDoc = queryMgr.search(qbyex, new StringHandle()).get(); + + System.out.println("testQueryByExampleJSON Result : "+resultDoc); + + + assertTrue("doc returned is not correct", resultDoc.contains("")); + + // release client + client.release(); + } + + @Test + public void testBug22179() throws IOException + { + System.out.println("Running testBug22179"); + + String[] filenames = {"constraint1.json", "constraint2.json", "constraint3.json", "constraint4.json", "constraint5.json"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "JSON"); + } + + ServerConfigurationManager confMgr = client.newServerConfigManager(); + confMgr.setQueryValidation(true); + + String combinedCriteria ="{\"search\":{\"options\":{\"constraint\":[{\"name\":\"para\", \"word\":{\"term-option\":[\"case-insensitive\"], \"field\":{\"name\":\"para\"}}},{\"name\":\"id\", \"value\":{\"element\":{\"ns\":\"\", \"name\":\"id\"}}}], \"return-metrics\":false, \"debug\":true, \"return-qtext\":false, \"transform-results\":{\"apply\":\"snippet\"}}, \"query\":{\"queries\":[{\"or-query\":{\"queries\":[{\"and-query\":{\"queries\":[{\"word-constraint-query\":{\"text\":[\"Bush\"], \"constraint-name\":\"para\"}},{\"not-query\":{\"word-constraint-query\":{\"text\":[\"memex\"], \"constraint-name\":\"para\"}}}]}},{\"and-query\":{\"queries\":[{\"value-constraint-query\":{\"text\":[\"0026\"], \"constraint-name\":\"id\"}},{\"term-query\":{\"text\":[\"memex\"]}}]}}]}}]}}}"; + QueryManager queryMgr = client.newQueryManager(); + + StringHandle combinedHandle = new StringHandle(combinedCriteria).withFormat(Format.JSON); + RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(combinedHandle); + String output = queryMgr.search(querydef, new StringHandle()).get(); + System.out.println(output); + assertTrue(output.contains("(cts:search(fn:collection(), cts:or-query((cts:and-query((cts:field-word-query")); + // release client + client.release(); + } + + @Test + public void testQueryByExampleXMLPayload() throws IOException, TransformerException, XpathException + { + System.out.println("Running testQueryByExampleXMLPayload"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "XML"); + } + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/qbe/qbe1.xml"); + FileHandle fileHandle = new FileHandle(file); + QueryManager queryMgr = client.newQueryManager(); + + RawQueryByExampleDefinition rw = queryMgr.newRawQueryByExampleDefinition(fileHandle.withFormat(Format.XML)); + SearchHandle results = queryMgr.search(rw, new SearchHandle()); + + for (MatchDocumentSummary result : results.getMatchResults()) + { + System.out.println(result.getUri()+ ": Uri"); + assertEquals("Wrong Document Searched",result.getUri() , "/qbe/constraint1.xml"); + } + + // release client + client.release(); + } + + @Test + public void testQueryByExampleJSONPayload() throws IOException, Exception + { + System.out.println("Running testQueryByExampleJSONPayload"); + + String[] filenames = {"constraint1.json", "constraint2.json", "constraint3.json", "constraint4.json", "constraint5.json"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "JSON"); + } + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/qbe/qbe1.json"); + FileHandle fileHandle = new FileHandle(file); + + QueryManager queryMgr = client.newQueryManager(); + RawQueryByExampleDefinition qbyex = queryMgr.newRawQueryByExampleDefinition(fileHandle.withFormat(Format.JSON)); + String resultDoc = queryMgr.search(qbyex, new StringHandle()).get(); + System.out.println(resultDoc); + assertTrue("Result is not proper", resultDoc.contains("/qbe/constraint1.json")); + + + // release client + client.release(); + } + + @Test + public void testQueryByExampleXMLPermission() throws IOException, TransformerException, XpathException + { + System.out.println("Running testQueryByExampleXMLPermission"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "XML"); + } + + // get the combined query + try{ + File file = new File("src/test/java/com/marklogic/javaclient/qbe/qbe2.xml"); + + FileHandle fileHandle = new FileHandle(file); + QueryManager queryMgr = client.newQueryManager(); + + RawQueryByExampleDefinition rw = queryMgr.newRawQueryByExampleDefinition(fileHandle.withFormat(Format.XML)); + SearchHandle results = queryMgr.search(rw, new SearchHandle()); + + for (MatchDocumentSummary result : results.getMatchResults()) + { + System.out.println(result.getUri()+ ": Uri"); + assertEquals("Wrong Document Searched",result.getUri() , "/qbe/constraint1.xml"); + } + }catch(ClientHandlerException e){ + System.out.println("Negative Test Passed of executing nonreadable file"); + } + // release client + client.release(); + + } + + @Test + public void testQueryByExampleWrongXML() throws IOException, TransformerException, XpathException + { + System.out.println("Running testQueryByExampleXMLPayload"); + + String filename = "WrongFormat.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + try{ + // write docs + writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "XML"); + + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/qbe/qbe1.xml"); + FileHandle fileHandle = new FileHandle(file); + QueryManager queryMgr = client.newQueryManager(); + + RawQueryByExampleDefinition rw = queryMgr.newRawQueryByExampleDefinition(fileHandle.withFormat(Format.XML)); + SearchHandle results = queryMgr.search(rw, new SearchHandle()); + + for (MatchDocumentSummary result : results.getMatchResults()) + { + System.out.println(result.getUri()+ ": Uri"); + // assertEquals("Wrong Document Searched",result.getUri() , "/qbe/constraint1.xml"); + } + } + catch(FailedRequestException e){ + System.out.println("Negative test passed as XML with invalid structure gave FailedRequestException "); + } + + // release client + client.release(); + } + + @Test + public void testQueryByExampleWrongJSON() throws IOException + { + System.out.println("Running testQueryByExampleJSON"); + + String filename = "WrongFormat.json"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + try{ + // write docs + writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "JSON"); + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/qbe/qbe1.json"); + + String qbeQuery = convertFileToString(file); + StringHandle qbeHandle = new StringHandle(qbeQuery); + qbeHandle.setFormat(Format.JSON); + + QueryManager queryMgr = client.newQueryManager(); + + RawQueryByExampleDefinition qbyex = queryMgr.newRawQueryByExampleDefinition(qbeHandle); + + String resultDoc = queryMgr.search(qbyex, new StringHandle()).get(); + + System.out.println(resultDoc); + + assertTrue("total result is not correct", resultDoc.contains("\"total\":1")); + assertTrue("doc returned is not correct", resultDoc.contains("\"metadata\":[{\"title\":\"Vannevar Bush\"},{\"id\":11},{\"p\":\"Vannevar Bush wrote an article for The Atlantic Monthly\"},{\"popularity\":5}]")); + } + catch(FailedRequestException e){ + System.out.println("Negative test passed as JSON with invalid structure gave FailedRequestException "); + } + + // release client + client.release(); + } + + @Test + public void testQueryByExampleXMLWrongQuery() throws IOException, TransformerException, XpathException + { + System.out.println("Running testQueryByExampleXMLWrongQuery"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "XML"); + } + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/qbe/qbe1.xml"); + FileHandle fileHandle = new FileHandle(file); + QueryManager queryMgr = client.newQueryManager(); + + RawQueryByExampleDefinition rw = queryMgr.newRawQueryByExampleDefinition(fileHandle.withFormat(Format.XML)); + SearchHandle results = queryMgr.search(rw, new SearchHandle()); + + for (MatchDocumentSummary result : results.getMatchResults()) + { + System.out.println(result.getUri()+ ": Uri"); + assertEquals("Wrong Document Searched",result.getUri() , "/qbe/constraint1.xml"); + } + try{ + File wrongFile = new File("src/test/java/com/marklogic/javaclient/qbe/WrongQbe.xml"); + FileHandle wrongFileHandle = new FileHandle(wrongFile); + QueryManager newQueryMgr = client.newQueryManager(); + + RawQueryByExampleDefinition newRw = newQueryMgr.newRawQueryByExampleDefinition(wrongFileHandle.withFormat(Format.XML)); + SearchHandle newResults = queryMgr.search(newRw, new SearchHandle()); + + for (MatchDocumentSummary result : newResults.getMatchResults()) + { + System.out.println(result.getUri()+ ": Uri"); + assertEquals("Wrong Document Searched",result.getUri() , "/qbe/constraint1.xml"); + } + } + catch(FailedRequestException e){ + System.out.println("Negative test passed as Query with improper Xml format gave FailedRequestException "); + } + + // release client + client.release(); + } + + @Test + public void testQueryByExampleJSONWrongQuery() throws IOException + { + System.out.println("Running testQueryByExampleJSONWrongQuery"); + + String[] filenames = {"constraint1.json", "constraint2.json", "constraint3.json", "constraint4.json", "constraint5.json"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/qbe/", "JSON"); + } + + // get the Correct query + File file = new File("src/test/java/com/marklogic/javaclient/qbe/qbe1.json"); + + String qbeQuery = convertFileToString(file); + StringHandle qbeHandle = new StringHandle(qbeQuery); + qbeHandle.setFormat(Format.JSON); + + QueryManager queryMgr = client.newQueryManager(); + + RawQueryByExampleDefinition qbyex = queryMgr.newRawQueryByExampleDefinition(qbeHandle); + + String resultDoc = queryMgr.search(qbyex, new StringHandle()).get(); + + System.out.println("Result of Correct Query"+ resultDoc); + + //assertTrue("total result is not correct", resultDoc.contains("\"total\":1")); + // assertTrue("doc returned is not correct", resultDoc.contains("\"metadata\":[{\"title\":\"Vannevar Bush\"},{\"id\":11},{\"p\":\"Vannevar Bush wrote an article for The Atlantic Monthly\"},{\"popularity\":5}]")); + + // get the query with Wrong Format + + File wrongFile = new File("src/test/java/com/marklogic/javaclient/qbe/WrongQbe.json"); + + String wrongQbeQuery = convertFileToString(wrongFile); + StringHandle newQbeHandle = new StringHandle(wrongQbeQuery); + newQbeHandle.setFormat(Format.JSON); + + QueryManager newQueryMgr = client.newQueryManager(); + + RawQueryByExampleDefinition newQbyex = newQueryMgr.newRawQueryByExampleDefinition(newQbeHandle); + try{ + String newResultDoc = newQueryMgr.search(newQbyex, new StringHandle()).get(); + + System.out.println("Result of Wrong Query"+newResultDoc); + + assertTrue("total result is not correct", resultDoc.contains("\"total\":1")); + assertTrue("doc returned is not correct", resultDoc.contains("\"metadata\":[{\"title\":\"Vannevar Bush\"},{\"id\":11},{\"p\":\"Vannevar Bush wrote an article for The Atlantic Monthly\"},{\"popularity\":5}]")); + } + catch(FailedRequestException e){ + System.out.println("Negative test passed as Query with improper JSON format gave FailedRequestException "); + } + + // release client + client.release(); + } + + @AfterClass public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilder.java b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilder.java index b18cc4820..1de4573b3 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilder.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilder.java @@ -1,782 +1,783 @@ -package com.marklogic.javaclient; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; - -import java.io.FileNotFoundException; -import java.io.IOException; - -import javax.xml.namespace.QName; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import com.marklogic.client.admin.QueryOptionsManager; -import com.marklogic.client.admin.ServerConfigurationManager; -import com.marklogic.client.admin.config.QueryOptions.Facets; -import com.marklogic.client.admin.config.QueryOptions.FragmentScope; -import com.marklogic.client.admin.config.QueryOptionsBuilder; -import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.io.Format; -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.query.StringQueryDefinition; -import com.marklogic.client.query.StructuredQueryBuilder; -import com.marklogic.client.query.StructuredQueryDefinition; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.DocumentMetadataHandle; -import com.marklogic.client.io.QueryOptionsHandle; -import com.marklogic.client.io.StringHandle; - -import org.junit.*; -public class TestQueryOptionBuilder extends BasicJavaClientREST { - - private static String dbName = "TestQueryOptionBuilderDB"; - private static String [] fNames = {"TestQueryOptionBuilderDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; -@BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - } - - -@After -public void testCleanUp() throws Exception -{ - clearDB(8011); - System.out.println("Running clear script"); -} - -@Test public void testValueConstraintWildcard() throws FileNotFoundException, XpathException - { - System.out.println("Running testValueConstraintWildcard"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/value-constraint-query-builder/", "XML"); - } - - // create query options manager - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create query options builder - QueryOptionsBuilder builder = new QueryOptionsBuilder(); - - // create query options handle - QueryOptionsHandle handle = new QueryOptionsHandle(); - - // build query options - handle.withConstraints( builder.constraint("id", - builder.value(builder.elementTermIndex(new QName("id"))))) - .withConfiguration(builder.configure() - .returnMetrics(false) - .returnQtext(false) - .debug(true)) - .withTransformResults(builder.rawResults()); - - // write query options - optionsMgr.writeOptions("ValueConstraintWildcard", handle); - - // read query option - StringHandle readHandle = new StringHandle(); - readHandle.setFormat(Format.XML); - optionsMgr.readOptions("ValueConstraintWildcard", readHandle); - String output = readHandle.get(); - System.out.println(output); - - // create query manager - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition("ValueConstraintWildcard"); - querydef.setCriteria("id:00*2 OR id:0??6"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); - - String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:element-value-query(fn:QName(\"\",\"id\"), \"00*2\", (\"lang=en\"), 1), cts:element-value-query(fn:QName(\"\",\"id\"), \"0??6\", (\"lang=en\"), 1))), (\"score-logtfidf\"), 1))[1 to 10]"; - - assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testWordConstraintNormalWordQuery() throws FileNotFoundException, XpathException - { - System.out.println("Running testWordConstraintNormalWordQuery"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/word-constraint-query-builder/", "XML"); - } - - // create query options manager - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create query options builder - QueryOptionsBuilder builder = new QueryOptionsBuilder(); - - // create query options handle - QueryOptionsHandle handle = new QueryOptionsHandle(); - - // build query options - handle.withConstraints(builder.constraint("intitle", - builder.word(builder.elementTermIndex(new QName("title")))), - builder.constraint("inprice", - builder.word(builder.elementAttributeTermIndex( - new QName("http://cloudbank.com", "price"), - new QName("amt"))))) - .withConfiguration(builder.configure() - .returnMetrics(false) - .returnQtext(false) - .debug(true)) - .withTransformResults(builder.rawResults()); - - - // write query options - optionsMgr.writeOptions("WordConstraintNormalWordQuery", handle); - - // read query option - StringHandle readHandle = new StringHandle(); - readHandle.setFormat(Format.XML); - optionsMgr.readOptions("WordConstraintNormalWordQuery", readHandle); - String output = readHandle.get(); - System.out.println(output); - - // create query manager - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition("WordConstraintNormalWordQuery"); - querydef.setCriteria("Memex OR inprice:.12"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("The Bush article", "string(//*[local-name()='result'][1]//*[local-name()='title'])", resultDoc); - assertXpathEvaluatesTo("The memex", "string(//*[local-name()='result'][2]//*[local-name()='title'])", resultDoc); - assertXpathEvaluatesTo("0.12", "string(//*[local-name()='result'][1]//@*[local-name()='amt'])", resultDoc); - assertXpathEvaluatesTo("123.45", "string(//*[local-name()='result'][2]//@*[local-name()='amt'])", resultDoc); - - String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:word-query(\"Memex\", (\"lang=en\"), 1), cts:element-attribute-word-query(fn:QName(\"http://cloudbank.com\",\"price\"), fn:QName(\"\",\"amt\"), \".12\", (\"lang=en\"), 1))), (\"score-logtfidf\"), 1))[1 to 10]"; - - assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); - - // release client - client.release(); - } - - - -@Test public void testAllConstraintsWithStringSearch() throws FileNotFoundException, XpathException, TransformerException - { - System.out.println("Running testAllConstraintsWithStringSearch"); - - String filename1 = "constraint1.xml"; - String filename2 = "constraint2.xml"; - String filename3 = "constraint3.xml"; - String filename4 = "constraint4.xml"; - String filename5 = "constraint5.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create and initialize a handle on the metadata - DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle4 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle5 = new DocumentMetadataHandle(); - - // set the metadata - metadataHandle1.getCollections().addAll("http://test.com/set1"); - metadataHandle1.getCollections().addAll("http://test.com/set5"); - metadataHandle2.getCollections().addAll("http://test.com/set1"); - metadataHandle3.getCollections().addAll("http://test.com/set3"); - metadataHandle4.getCollections().addAll("http://test.com/set3/set3-1"); - metadataHandle5.getCollections().addAll("http://test.com/set1"); - metadataHandle5.getCollections().addAll("http://test.com/set5"); - - // write docs - writeDocumentUsingInputStreamHandle(client, filename1, "/collection-constraint/", metadataHandle1, "XML"); - writeDocumentUsingInputStreamHandle(client, filename2, "/collection-constraint/", metadataHandle2, "XML"); - writeDocumentUsingInputStreamHandle(client, filename3, "/collection-constraint/", metadataHandle3, "XML"); - writeDocumentUsingInputStreamHandle(client, filename4, "/collection-constraint/", metadataHandle4, "XML"); - writeDocumentUsingInputStreamHandle(client, filename5, "/collection-constraint/", metadataHandle5, "XML"); - - // create query options manager - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create query options builder - QueryOptionsBuilder builder = new QueryOptionsBuilder(); - - // create query options handle - QueryOptionsHandle handle = new QueryOptionsHandle(); - - // build query options - - handle. - withConstraints( - builder.constraint("id", - builder.value(builder.elementTermIndex(new QName("id")))), - builder.constraint("date", - builder.range( - builder.elementRangeIndex(new QName("http://purl.org/dc/elements/1.1/", "date"), - builder.rangeType("xs:date")))), - builder.constraint("coll", - builder.collection("http://test.com/", Facets.FACETED)), - builder.constraint("para", - builder.word(builder.fieldTermIndex("para"), null, null, "case-insensitive")), - builder.constraint("intitle", - builder.word(builder.elementTermIndex(new QName("title")))), - builder.constraint("price", - builder.range(builder.elementAttributeRangeIndex(new QName("http://cloudbank.com", "price"), - new QName("amt"), builder.rangeType("xs:decimal")), - Facets.UNFACETED, - null, - builder.buckets( - builder.bucket("high", "High", "120", null), - builder.bucket("medium", "Medium", "3", "14"), - builder.bucket("low", "Low", "0", "2")))), - builder.constraint("pop", - builder.range(builder.elementRangeIndex(new QName("popularity"), - builder.rangeType("xs:int")), - Facets.FACETED, - null, - builder.buckets(builder.bucket("high", "High", "5", null), - builder.bucket("medium", "Medium", "3", "5"), - builder.bucket("low", "Low", "1", "3"))))) - .withConfiguration(builder.configure() - .returnMetrics(false) - .returnQtext(false) - .debug(true)) - .withTransformResults(builder.rawResults()); - - - // write query options - optionsMgr.writeOptions("AllConstraintsWithStringSearch", handle); - - // read query option - StringHandle readHandle = new StringHandle(); - readHandle.setFormat(Format.XML); - optionsMgr.readOptions("AllConstraintsWithStringSearch", readHandle); - String output = readHandle.get(); - System.out.println(output); - - // create query manager - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition("AllConstraintsWithStringSearch"); - querydef.setCriteria("(coll:set1 AND coll:set5) AND -intitle:memex AND (pop:high OR pop:medium) AND price:low AND id:**11 AND date:2005-01-01 AND (para:Bush AND -para:memex)"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("Vannevar Bush", "string(//*[local-name()='result'][1]//*[local-name()='title'])", resultDoc); - - // release client - client.release(); - } - - - -@Test public void testAllConstraintsWithStructuredSearch() throws FileNotFoundException, XpathException, TransformerException - { - System.out.println("Running testAllConstraintsWithStructuredSearch"); - - String filename1 = "constraint1.xml"; - String filename2 = "constraint2.xml"; - String filename3 = "constraint3.xml"; - String filename4 = "constraint4.xml"; - String filename5 = "constraint5.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create and initialize a handle on the metadata - DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle4 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle5 = new DocumentMetadataHandle(); - - // set the metadata - metadataHandle1.getCollections().addAll("http://test.com/set1"); - metadataHandle1.getCollections().addAll("http://test.com/set5"); - metadataHandle2.getCollections().addAll("http://test.com/set1"); - metadataHandle3.getCollections().addAll("http://test.com/set3"); - metadataHandle4.getCollections().addAll("http://test.com/set3/set3-1"); - metadataHandle5.getCollections().addAll("http://test.com/set1"); - metadataHandle5.getCollections().addAll("http://test.com/set5"); - - // write docs - writeDocumentUsingInputStreamHandle(client, filename1, "/collection-constraint/", metadataHandle1, "XML"); - writeDocumentUsingInputStreamHandle(client, filename2, "/collection-constraint/", metadataHandle2, "XML"); - writeDocumentUsingInputStreamHandle(client, filename3, "/collection-constraint/", metadataHandle3, "XML"); - writeDocumentUsingInputStreamHandle(client, filename4, "/collection-constraint/", metadataHandle4, "XML"); - writeDocumentUsingInputStreamHandle(client, filename5, "/collection-constraint/", metadataHandle5, "XML"); - - // create query options manager - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create query options builder - QueryOptionsBuilder builder = new QueryOptionsBuilder(); - - // create query options handle - QueryOptionsHandle handle = new QueryOptionsHandle(); - - // build query options - - handle. - withConstraints( - builder.constraint("id", - builder.value(builder.elementTermIndex(new QName("id")))), - builder.constraint("date", - builder.range( - builder.elementRangeIndex(new QName("http://purl.org/dc/elements/1.1/", "date"), - builder.rangeType("xs:date")))), - builder.constraint("coll", - builder.collection("http://test.com/", Facets.FACETED)), - builder.constraint("para", - builder.word(builder.fieldTermIndex("para"), null, null, "case-insensitive")), - builder.constraint("intitle", - builder.word(builder.elementTermIndex(new QName("title")))), - builder.constraint("price", - builder.range(builder.elementAttributeRangeIndex(new QName("http://cloudbank.com", "price"), - new QName("amt"), builder.rangeType("xs:decimal")), - Facets.UNFACETED, - null, - builder.buckets( - builder.bucket("high", "High", "120", null), - builder.bucket("medium", "Medium", "3", "14"), - builder.bucket("low", "Low", "0", "2")))), - builder.constraint("pop", - builder.range(builder.elementRangeIndex(new QName("popularity"), - builder.rangeType("xs:int")), - Facets.FACETED, - null, - builder.buckets(builder.bucket("high", "High", "5", null), - builder.bucket("medium", "Medium", "3", "5"), - builder.bucket("low", "Low", "1", "3"))))) - .withConfiguration(builder.configure() - .returnMetrics(false) - .returnQtext(false) - .debug(true)) - .withTransformResults(builder.rawResults()); - - - // write query options - optionsMgr.writeOptions("AllConstraintsWithStructuredSearch", handle); - - // read query option - StringHandle readHandle = new StringHandle(); - readHandle.setFormat(Format.XML); - optionsMgr.readOptions("AllConstraintsWithStructuredSearch", readHandle); - String output = readHandle.get(); - System.out.println(output); - - // create query manager - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder("AllConstraintsWithStructuredSearch"); - StructuredQueryDefinition query1 = qb.and(qb.collectionConstraint("coll", "set1"), qb.collectionConstraint("coll", "set5")); - StructuredQueryDefinition query2 = qb.not(qb.wordConstraint("intitle", "memex")); - StructuredQueryDefinition query3 = qb.valueConstraint("id", "**11"); - StructuredQueryDefinition query4 = qb.rangeConstraint("date", StructuredQueryBuilder.Operator.EQ, "2005-01-01"); - StructuredQueryDefinition query5 = qb.and(qb.wordConstraint("para", "Bush"), qb.not(qb.wordConstraint("para", "memex"))); - StructuredQueryDefinition query6 = qb.rangeConstraint("price", StructuredQueryBuilder.Operator.EQ, "low"); - StructuredQueryDefinition query7 = qb.or(qb.rangeConstraint("pop", StructuredQueryBuilder.Operator.EQ, "high"), qb.rangeConstraint("pop", StructuredQueryBuilder.Operator.EQ, "medium")); - StructuredQueryDefinition queryFinal = qb.and(query1, query2, query3, query4, query5, query6, query7); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(queryFinal, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("Vannevar Bush", "string(//*[local-name()='result'][1]//*[local-name()='title'])", resultDoc); - - // release client - client.release(); - } - - - -@Test public void testExtractMetadataWithStructuredSearch() throws XpathException, TransformerException, ParserConfigurationException, SAXException, IOException - { - System.out.println("testExtractMetadataWithStructuredSearch"); - - String filename = "xml-original.xml"; - String uri = "/extract-metadata/"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - ServerConfigurationManager scMgr = client.newServerConfigManager(); - scMgr.setServerRequestLogging(true); - scMgr.writeConfiguration(); - - // get the original metadata - Document docMetadata = getXMLMetadata("metadata-original.xml"); - - // create doc manager - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - - // write the doc - writeDocumentUsingInputStreamHandle(client, filename, uri, "XML"); - - // create handle to write metadata - DOMHandle writeMetadataHandle = new DOMHandle(); - writeMetadataHandle.set(docMetadata); - - // create doc id - String docId = uri + filename; - - // write metadata - docMgr.writeMetadata(docId, writeMetadataHandle); - - // create query options manager - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create query options builder - QueryOptionsBuilder builder = new QueryOptionsBuilder(); - - // create query options handle - QueryOptionsHandle handle = new QueryOptionsHandle(); - - // build query options - handle.withConfiguration(builder.configure().fragmentScope(FragmentScope.PROPERTIES)); - handle.withExtractMetadata(builder.extractMetadata(builder.elementValue(new QName("", "Author")), builder.elementValue(new QName("", "AppName")))); - //handle.withExtractMetadata(builder.extractMetadata(builder.elementValue(new QName("", "Author")), builder.constraintValue("appname"))); - handle.withConstraints(builder.constraint("appname", builder.word(builder.elementTermIndex(new QName("AppName"))))); - - // write query options - optionsMgr.writeOptions("ExtractMetadataWithStructuredSearch", handle); - - // read query option - StringHandle readHandle = new StringHandle(); - readHandle.setFormat(Format.XML); - optionsMgr.readOptions("ExtractMetadataWithStructuredSearch", readHandle); - String output = readHandle.get(); - System.out.println(output); - - // create query manager - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder("ExtractMetadataWithStructuredSearch"); - StructuredQueryDefinition queryTerm1 = qb.term("MarkLogic"); - StructuredQueryDefinition queryTerm2 = qb.term("Microsoft"); - //StructuredQueryDefinition queryWord = qb.wordConstraint("appname", "Microsoft"); - StructuredQueryDefinition queryFinal = qb.and(queryTerm1, queryTerm2); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(queryFinal, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("MarkLogic", "string(//*[local-name()='result']//*[local-name()='metadata']/*[local-name()='Author'])", resultDoc); - assertXpathEvaluatesTo("Microsoft Office Word", "string(//*[local-name()='result']//*[local-name()='metadata']/*[local-name()='AppName'])", resultDoc); - - // release client - client.release(); - } - - // See bug 18361 - /*public void testExtractMetadataWithStructuredSearchAndConstraint() throws XpathException, TransformerException, ParserConfigurationException, SAXException, IOException - { - System.out.println("testExtractMetadataWithStructuredSearchAndConstraint"); - - String filename = "xml-original.xml"; - String uri = "/extract-metadata/"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - ServerConfigurationManager scMgr = client.newServerConfigManager(); - scMgr.setServerRequestLogging(true); - scMgr.writeConfiguration(); - - // get the original metadata - Document docMetadata = getXMLMetadata("metadata-original.xml"); - - // create doc manager - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - - // write the doc - writeDocumentUsingInputStreamHandle(client, filename, uri, "XML"); - - // create handle to write metadata - DOMHandle writeMetadataHandle = new DOMHandle(); - writeMetadataHandle.set(docMetadata); - - // create doc id - String docId = uri + filename; - - // write metadata - docMgr.writeMetadata(docId, writeMetadataHandle); - - // create query options manager - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create query options builder - QueryOptionsBuilder builder = new QueryOptionsBuilder(); - - // create query options handle - QueryOptionsHandle handle = new QueryOptionsHandle(); - - // build query options - handle.withConfiguration(builder.configure().fragmentScope(FragmentScope.PROPERTIES)); - handle.withExtractMetadata(builder.extractMetadata(builder.elementValue(new QName("", "Author")), builder.constraintValue("appname"))); - handle.withConstraints(builder.constraint("appname", builder.word(builder.elementTermIndex(new QName("AppName"))))); - - // write query options - optionsMgr.writeOptions("ExtractMetadataWithStructuredSearchAndConstraint", handle); - - // create query manager - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder("ExtractMetadataWithStructuredSearchAndConstraint"); - StructuredQueryDefinition queryTerm = qb.term("MarkLogic"); - StructuredQueryDefinition queryWord = qb.wordConstraint("appname", "Microsoft"); - StructuredQueryDefinition queryFinal = qb.and(queryTerm, queryWord); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(queryFinal, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - System.out.println(convertXMLDocumentToString(resultDoc)); - - //assertXpathEvaluatesTo("MarkLogic", "string(//*[local-name()='result']//*[local-name()='metadata']/*[local-name()='Author'])", resultDoc); - //assertXpathEvaluatesTo("Microsoft Office Word", "string(//*[local-name()='result']//*[local-name()='metadata']/*[local-name()='AppName'])", resultDoc); - - // release client - client.release(); - }*/ - -@Test public void testExtractMetadataWithStructuredSearchAndRangeConstraint() throws XpathException, TransformerException, ParserConfigurationException, SAXException, IOException - { - System.out.println("testExtractMetadataWithStructuredSearchAndRangeConstraint"); - - String filename = "xml-original.xml"; - String uri = "/extract-metadata/"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - ServerConfigurationManager scMgr = client.newServerConfigManager(); - scMgr.setServerRequestLogging(true); - scMgr.writeConfiguration(); - - // get the original metadata - Document docMetadata = getXMLMetadata("metadata-original.xml"); - - // create doc manager - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - - // write the doc - writeDocumentUsingInputStreamHandle(client, filename, uri, "XML"); - - // create handle to write metadata - DOMHandle writeMetadataHandle = new DOMHandle(); - writeMetadataHandle.set(docMetadata); - - // create doc id - String docId = uri + filename; - - // write metadata - docMgr.writeMetadata(docId, writeMetadataHandle); - - // create query options manager - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create query options builder - QueryOptionsBuilder builder = new QueryOptionsBuilder(); - - // create query options handle - QueryOptionsHandle handle = new QueryOptionsHandle(); - - // build query options - handle.withConfiguration(builder.configure().fragmentScope(FragmentScope.PROPERTIES)); - handle.withExtractMetadata(builder.extractMetadata(builder.elementValue(new QName("", "Author")), builder.constraintValue("pop"))); - //handle.withConstraints(builder.constraint("appname", builder.word(builder.elementTermIndex(new QName("AppName"))))); - handle.withConstraints(builder.constraint("pop", - builder.range(builder.elementRangeIndex(new QName("popularity"), - builder.rangeType("xs:int")), - Facets.FACETED, - null, - builder.buckets(builder.bucket("high", "High", "5", null), - builder.bucket("medium", "Medium", "3", "5"), - builder.bucket("low", "Low", "1", "3"))))); - - // write query options - optionsMgr.writeOptions("ExtractMetadataWithStructuredSearchAndRangeConstraint", handle); - - // read query option - StringHandle readHandle = new StringHandle(); - readHandle.setFormat(Format.XML); - optionsMgr.readOptions("ExtractMetadataWithStructuredSearchAndRangeConstraint", readHandle); - String output = readHandle.get(); - System.out.println(output); - - // create query manager - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder("ExtractMetadataWithStructuredSearchAndRangeConstraint"); - StructuredQueryDefinition queryFinal = qb.rangeConstraint("pop", StructuredQueryBuilder.Operator.EQ, "high"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(queryFinal, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("MarkLogic", "string(//*[local-name()='result']//*[local-name()='metadata']/*[local-name()='Author'])", resultDoc); - assertXpathEvaluatesTo("5", "string(//*[local-name()='result']//*[local-name()='metadata']/*[local-name()='constraint-meta'])", resultDoc); - //assertXpathEvaluatesTo("Microsoft Office Word", "string(//*[local-name()='result']//*[local-name()='metadata']/*[local-name()='AppName'])", resultDoc); - - // release client - client.release(); - } - - -@Test public void testDocumentLevelMetadata() throws XpathException, TransformerException, ParserConfigurationException, SAXException, IOException - { - System.out.println("testDocumentLevelMetadata"); - - String filename = "xml-original.xml"; - String uri = "/extract-metadata/"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - ServerConfigurationManager scMgr = client.newServerConfigManager(); - scMgr.setServerRequestLogging(true); - scMgr.writeConfiguration(); - - // get the original metadata - Document docMetadata = getXMLMetadata("metadata-original.xml"); - - // create doc manager - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - - // write the doc - writeDocumentUsingInputStreamHandle(client, filename, uri, "XML"); - - // create handle to write metadata - DOMHandle writeMetadataHandle = new DOMHandle(); - writeMetadataHandle.set(docMetadata); - - // create doc id - String docId = uri + filename; - - // write metadata - docMgr.writeMetadata(docId, writeMetadataHandle); - - // create query options manager - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create query options builder - QueryOptionsBuilder builder = new QueryOptionsBuilder(); - - // create query options handle - QueryOptionsHandle handle = new QueryOptionsHandle(); - - // build query options - handle.withConfiguration(builder.configure().fragmentScope(FragmentScope.DOCUMENTS)); - handle.withExtractMetadata(builder.extractMetadata(builder.elementValue(new QName("", "Author")), builder.constraintValue("pop"), builder.elementValue(new QName("", "name")))); - //handle.withConstraints(builder.constraint("appname", builder.word(builder.elementTermIndex(new QName("AppName"))))); - handle.withConstraints(builder.constraint("pop", - builder.range(builder.elementRangeIndex(new QName("popularity"), - builder.rangeType("xs:int")), - Facets.FACETED, - null, - builder.buckets(builder.bucket("high", "High", "5", null), - builder.bucket("medium", "Medium", "3", "5"), - builder.bucket("low", "Low", "1", "3"))))); - - // write query options - optionsMgr.writeOptions("DocumentLevelMetadata", handle); - - // read query option - StringHandle readHandle = new StringHandle(); - readHandle.setFormat(Format.XML); - optionsMgr.readOptions("DocumentLevelMetadata", readHandle); - String output = readHandle.get(); - System.out.println(output); - - // create query manager - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder("DocumentLevelMetadata"); - StructuredQueryDefinition queryFinal = qb.term("noodle"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(queryFinal, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("noodle", "string(//*[local-name()='result']//*[local-name()='metadata']/*[local-name()='name'])", resultDoc); - //assertXpathEvaluatesTo("5", "string(//*[local-name()='result']//*[local-name()='metadata']/*[local-name()='constraint-meta'])", resultDoc); - //assertXpathEvaluatesTo("Microsoft Office Word", "string(//*[local-name()='result']//*[local-name()='metadata']/*[local-name()='AppName'])", resultDoc); - - // release client - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} +package com.marklogic.javaclient; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; + +import java.io.FileNotFoundException; +import java.io.IOException; + +import javax.xml.namespace.QName; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import com.marklogic.client.admin.QueryOptionsManager; +import com.marklogic.client.admin.ServerConfigurationManager; +import com.marklogic.client.admin.config.QueryOptions.Facets; +import com.marklogic.client.admin.config.QueryOptions.FragmentScope; +import com.marklogic.client.admin.config.QueryOptionsBuilder; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.io.Format; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.StringQueryDefinition; +import com.marklogic.client.query.StructuredQueryBuilder; +import com.marklogic.client.query.StructuredQueryDefinition; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.DocumentMetadataHandle; +import com.marklogic.client.io.QueryOptionsHandle; +import com.marklogic.client.io.StringHandle; + +import org.junit.*; + +public class TestQueryOptionBuilder extends BasicJavaClientREST { + + private static String dbName = "TestQueryOptionBuilderDB"; + private static String [] fNames = {"TestQueryOptionBuilderDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + } + + + @After + public void testCleanUp() throws Exception + { + clearDB(8011); + System.out.println("Running clear script"); + } + + @Test + public void testValueConstraintWildcard() throws FileNotFoundException, XpathException + { + System.out.println("Running testValueConstraintWildcard"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/value-constraint-query-builder/", "XML"); + } + + // create query options manager + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create query options builder + QueryOptionsBuilder builder = new QueryOptionsBuilder(); + + // create query options handle + QueryOptionsHandle handle = new QueryOptionsHandle(); + + // build query options + handle.withConstraints( builder.constraint("id", + builder.value(builder.elementTermIndex(new QName("id"))))) + .withConfiguration(builder.configure() + .returnMetrics(false) + .returnQtext(false) + .debug(true)) + .withTransformResults(builder.rawResults()); + + // write query options + optionsMgr.writeOptions("ValueConstraintWildcard", handle); + + // read query option + StringHandle readHandle = new StringHandle(); + readHandle.setFormat(Format.XML); + optionsMgr.readOptions("ValueConstraintWildcard", readHandle); + String output = readHandle.get(); + System.out.println(output); + + // create query manager + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition("ValueConstraintWildcard"); + querydef.setCriteria("id:00*2 OR id:0??6"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); + assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); + + String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:element-value-query(fn:QName(\"\",\"id\"), \"00*2\", (\"lang=en\"), 1), cts:element-value-query(fn:QName(\"\",\"id\"), \"0??6\", (\"lang=en\"), 1))), (\"score-logtfidf\"), 1))[1 to 10]"; + + assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testWordConstraintNormalWordQuery() throws FileNotFoundException, XpathException + { + System.out.println("Running testWordConstraintNormalWordQuery"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/word-constraint-query-builder/", "XML"); + } + + // create query options manager + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create query options builder + QueryOptionsBuilder builder = new QueryOptionsBuilder(); + + // create query options handle + QueryOptionsHandle handle = new QueryOptionsHandle(); + + // build query options + handle.withConstraints(builder.constraint("intitle", + builder.word(builder.elementTermIndex(new QName("title")))), + builder.constraint("inprice", + builder.word(builder.elementAttributeTermIndex( + new QName("http://cloudbank.com", "price"), + new QName("amt"))))) + .withConfiguration(builder.configure() + .returnMetrics(false) + .returnQtext(false) + .debug(true)) + .withTransformResults(builder.rawResults()); + + + // write query options + optionsMgr.writeOptions("WordConstraintNormalWordQuery", handle); + + // read query option + StringHandle readHandle = new StringHandle(); + readHandle.setFormat(Format.XML); + optionsMgr.readOptions("WordConstraintNormalWordQuery", readHandle); + String output = readHandle.get(); + System.out.println(output); + + // create query manager + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition("WordConstraintNormalWordQuery"); + querydef.setCriteria("Memex OR inprice:.12"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("The Bush article", "string(//*[local-name()='result'][1]//*[local-name()='title'])", resultDoc); + assertXpathEvaluatesTo("The memex", "string(//*[local-name()='result'][2]//*[local-name()='title'])", resultDoc); + assertXpathEvaluatesTo("0.12", "string(//*[local-name()='result'][1]//@*[local-name()='amt'])", resultDoc); + assertXpathEvaluatesTo("123.45", "string(//*[local-name()='result'][2]//@*[local-name()='amt'])", resultDoc); + + String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:word-query(\"Memex\", (\"lang=en\"), 1), cts:element-attribute-word-query(fn:QName(\"http://cloudbank.com\",\"price\"), fn:QName(\"\",\"amt\"), \".12\", (\"lang=en\"), 1))), (\"score-logtfidf\"), 1))[1 to 10]"; + + assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testAllConstraintsWithStringSearch() throws FileNotFoundException, XpathException, TransformerException + { + System.out.println("Running testAllConstraintsWithStringSearch"); + + String filename1 = "constraint1.xml"; + String filename2 = "constraint2.xml"; + String filename3 = "constraint3.xml"; + String filename4 = "constraint4.xml"; + String filename5 = "constraint5.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle4 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle5 = new DocumentMetadataHandle(); + + // set the metadata + metadataHandle1.getCollections().addAll("http://test.com/set1"); + metadataHandle1.getCollections().addAll("http://test.com/set5"); + metadataHandle2.getCollections().addAll("http://test.com/set1"); + metadataHandle3.getCollections().addAll("http://test.com/set3"); + metadataHandle4.getCollections().addAll("http://test.com/set3/set3-1"); + metadataHandle5.getCollections().addAll("http://test.com/set1"); + metadataHandle5.getCollections().addAll("http://test.com/set5"); + + // write docs + writeDocumentUsingInputStreamHandle(client, filename1, "/collection-constraint/", metadataHandle1, "XML"); + writeDocumentUsingInputStreamHandle(client, filename2, "/collection-constraint/", metadataHandle2, "XML"); + writeDocumentUsingInputStreamHandle(client, filename3, "/collection-constraint/", metadataHandle3, "XML"); + writeDocumentUsingInputStreamHandle(client, filename4, "/collection-constraint/", metadataHandle4, "XML"); + writeDocumentUsingInputStreamHandle(client, filename5, "/collection-constraint/", metadataHandle5, "XML"); + + // create query options manager + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create query options builder + QueryOptionsBuilder builder = new QueryOptionsBuilder(); + + // create query options handle + QueryOptionsHandle handle = new QueryOptionsHandle(); + + // build query options + + handle. + withConstraints( + builder.constraint("id", + builder.value(builder.elementTermIndex(new QName("id")))), + builder.constraint("date", + builder.range( + builder.elementRangeIndex(new QName("http://purl.org/dc/elements/1.1/", "date"), + builder.rangeType("xs:date")))), + builder.constraint("coll", + builder.collection("http://test.com/", Facets.FACETED)), + builder.constraint("para", + builder.word(builder.fieldTermIndex("para"), null, null, "case-insensitive")), + builder.constraint("intitle", + builder.word(builder.elementTermIndex(new QName("title")))), + builder.constraint("price", + builder.range(builder.elementAttributeRangeIndex(new QName("http://cloudbank.com", "price"), + new QName("amt"), builder.rangeType("xs:decimal")), + Facets.UNFACETED, + null, + builder.buckets( + builder.bucket("high", "High", "120", null), + builder.bucket("medium", "Medium", "3", "14"), + builder.bucket("low", "Low", "0", "2")))), + builder.constraint("pop", + builder.range(builder.elementRangeIndex(new QName("popularity"), + builder.rangeType("xs:int")), + Facets.FACETED, + null, + builder.buckets(builder.bucket("high", "High", "5", null), + builder.bucket("medium", "Medium", "3", "5"), + builder.bucket("low", "Low", "1", "3"))))) + .withConfiguration(builder.configure() + .returnMetrics(false) + .returnQtext(false) + .debug(true)) + .withTransformResults(builder.rawResults()); + + + // write query options + optionsMgr.writeOptions("AllConstraintsWithStringSearch", handle); + + // read query option + StringHandle readHandle = new StringHandle(); + readHandle.setFormat(Format.XML); + optionsMgr.readOptions("AllConstraintsWithStringSearch", readHandle); + String output = readHandle.get(); + System.out.println(output); + + // create query manager + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition("AllConstraintsWithStringSearch"); + querydef.setCriteria("(coll:set1 AND coll:set5) AND -intitle:memex AND (pop:high OR pop:medium) AND price:low AND id:**11 AND date:2005-01-01 AND (para:Bush AND -para:memex)"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("Vannevar Bush", "string(//*[local-name()='result'][1]//*[local-name()='title'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testAllConstraintsWithStructuredSearch() throws FileNotFoundException, XpathException, TransformerException + { + System.out.println("Running testAllConstraintsWithStructuredSearch"); + + String filename1 = "constraint1.xml"; + String filename2 = "constraint2.xml"; + String filename3 = "constraint3.xml"; + String filename4 = "constraint4.xml"; + String filename5 = "constraint5.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle4 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle5 = new DocumentMetadataHandle(); + + // set the metadata + metadataHandle1.getCollections().addAll("http://test.com/set1"); + metadataHandle1.getCollections().addAll("http://test.com/set5"); + metadataHandle2.getCollections().addAll("http://test.com/set1"); + metadataHandle3.getCollections().addAll("http://test.com/set3"); + metadataHandle4.getCollections().addAll("http://test.com/set3/set3-1"); + metadataHandle5.getCollections().addAll("http://test.com/set1"); + metadataHandle5.getCollections().addAll("http://test.com/set5"); + + // write docs + writeDocumentUsingInputStreamHandle(client, filename1, "/collection-constraint/", metadataHandle1, "XML"); + writeDocumentUsingInputStreamHandle(client, filename2, "/collection-constraint/", metadataHandle2, "XML"); + writeDocumentUsingInputStreamHandle(client, filename3, "/collection-constraint/", metadataHandle3, "XML"); + writeDocumentUsingInputStreamHandle(client, filename4, "/collection-constraint/", metadataHandle4, "XML"); + writeDocumentUsingInputStreamHandle(client, filename5, "/collection-constraint/", metadataHandle5, "XML"); + + // create query options manager + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create query options builder + QueryOptionsBuilder builder = new QueryOptionsBuilder(); + + // create query options handle + QueryOptionsHandle handle = new QueryOptionsHandle(); + + // build query options + + handle. + withConstraints( + builder.constraint("id", + builder.value(builder.elementTermIndex(new QName("id")))), + builder.constraint("date", + builder.range( + builder.elementRangeIndex(new QName("http://purl.org/dc/elements/1.1/", "date"), + builder.rangeType("xs:date")))), + builder.constraint("coll", + builder.collection("http://test.com/", Facets.FACETED)), + builder.constraint("para", + builder.word(builder.fieldTermIndex("para"), null, null, "case-insensitive")), + builder.constraint("intitle", + builder.word(builder.elementTermIndex(new QName("title")))), + builder.constraint("price", + builder.range(builder.elementAttributeRangeIndex(new QName("http://cloudbank.com", "price"), + new QName("amt"), builder.rangeType("xs:decimal")), + Facets.UNFACETED, + null, + builder.buckets( + builder.bucket("high", "High", "120", null), + builder.bucket("medium", "Medium", "3", "14"), + builder.bucket("low", "Low", "0", "2")))), + builder.constraint("pop", + builder.range(builder.elementRangeIndex(new QName("popularity"), + builder.rangeType("xs:int")), + Facets.FACETED, + null, + builder.buckets(builder.bucket("high", "High", "5", null), + builder.bucket("medium", "Medium", "3", "5"), + builder.bucket("low", "Low", "1", "3"))))) + .withConfiguration(builder.configure() + .returnMetrics(false) + .returnQtext(false) + .debug(true)) + .withTransformResults(builder.rawResults()); + + + // write query options + optionsMgr.writeOptions("AllConstraintsWithStructuredSearch", handle); + + // read query option + StringHandle readHandle = new StringHandle(); + readHandle.setFormat(Format.XML); + optionsMgr.readOptions("AllConstraintsWithStructuredSearch", readHandle); + String output = readHandle.get(); + System.out.println(output); + + // create query manager + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder("AllConstraintsWithStructuredSearch"); + StructuredQueryDefinition query1 = qb.and(qb.collectionConstraint("coll", "set1"), qb.collectionConstraint("coll", "set5")); + StructuredQueryDefinition query2 = qb.not(qb.wordConstraint("intitle", "memex")); + StructuredQueryDefinition query3 = qb.valueConstraint("id", "**11"); + StructuredQueryDefinition query4 = qb.rangeConstraint("date", StructuredQueryBuilder.Operator.EQ, "2005-01-01"); + StructuredQueryDefinition query5 = qb.and(qb.wordConstraint("para", "Bush"), qb.not(qb.wordConstraint("para", "memex"))); + StructuredQueryDefinition query6 = qb.rangeConstraint("price", StructuredQueryBuilder.Operator.EQ, "low"); + StructuredQueryDefinition query7 = qb.or(qb.rangeConstraint("pop", StructuredQueryBuilder.Operator.EQ, "high"), qb.rangeConstraint("pop", StructuredQueryBuilder.Operator.EQ, "medium")); + StructuredQueryDefinition queryFinal = qb.and(query1, query2, query3, query4, query5, query6, query7); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(queryFinal, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("Vannevar Bush", "string(//*[local-name()='result'][1]//*[local-name()='title'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testExtractMetadataWithStructuredSearch() throws XpathException, TransformerException, ParserConfigurationException, SAXException, IOException + { + System.out.println("testExtractMetadataWithStructuredSearch"); + + String filename = "xml-original.xml"; + String uri = "/extract-metadata/"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + ServerConfigurationManager scMgr = client.newServerConfigManager(); + scMgr.setServerRequestLogging(true); + scMgr.writeConfiguration(); + + // get the original metadata + Document docMetadata = getXMLMetadata("metadata-original.xml"); + + // create doc manager + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + + // write the doc + writeDocumentUsingInputStreamHandle(client, filename, uri, "XML"); + + // create handle to write metadata + DOMHandle writeMetadataHandle = new DOMHandle(); + writeMetadataHandle.set(docMetadata); + + // create doc id + String docId = uri + filename; + + // write metadata + docMgr.writeMetadata(docId, writeMetadataHandle); + + // create query options manager + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create query options builder + QueryOptionsBuilder builder = new QueryOptionsBuilder(); + + // create query options handle + QueryOptionsHandle handle = new QueryOptionsHandle(); + + // build query options + handle.withConfiguration(builder.configure().fragmentScope(FragmentScope.PROPERTIES)); + handle.withExtractMetadata(builder.extractMetadata(builder.elementValue(new QName("", "Author")), builder.elementValue(new QName("", "AppName")))); + //handle.withExtractMetadata(builder.extractMetadata(builder.elementValue(new QName("", "Author")), builder.constraintValue("appname"))); + handle.withConstraints(builder.constraint("appname", builder.word(builder.elementTermIndex(new QName("AppName"))))); + + // write query options + optionsMgr.writeOptions("ExtractMetadataWithStructuredSearch", handle); + + // read query option + StringHandle readHandle = new StringHandle(); + readHandle.setFormat(Format.XML); + optionsMgr.readOptions("ExtractMetadataWithStructuredSearch", readHandle); + String output = readHandle.get(); + System.out.println(output); + + // create query manager + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder("ExtractMetadataWithStructuredSearch"); + StructuredQueryDefinition queryTerm1 = qb.term("MarkLogic"); + StructuredQueryDefinition queryTerm2 = qb.term("Microsoft"); + //StructuredQueryDefinition queryWord = qb.wordConstraint("appname", "Microsoft"); + StructuredQueryDefinition queryFinal = qb.and(queryTerm1, queryTerm2); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(queryFinal, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("MarkLogic", "string(//*[local-name()='result']//*[local-name()='metadata']/*[local-name()='Author'])", resultDoc); + assertXpathEvaluatesTo("Microsoft Office Word", "string(//*[local-name()='result']//*[local-name()='metadata']/*[local-name()='AppName'])", resultDoc); + + // release client + client.release(); + } + + // See bug 18361 + /*public void testExtractMetadataWithStructuredSearchAndConstraint() throws XpathException, TransformerException, ParserConfigurationException, SAXException, IOException + { + System.out.println("testExtractMetadataWithStructuredSearchAndConstraint"); + + String filename = "xml-original.xml"; + String uri = "/extract-metadata/"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + ServerConfigurationManager scMgr = client.newServerConfigManager(); + scMgr.setServerRequestLogging(true); + scMgr.writeConfiguration(); + + // get the original metadata + Document docMetadata = getXMLMetadata("metadata-original.xml"); + + // create doc manager + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + + // write the doc + writeDocumentUsingInputStreamHandle(client, filename, uri, "XML"); + + // create handle to write metadata + DOMHandle writeMetadataHandle = new DOMHandle(); + writeMetadataHandle.set(docMetadata); + + // create doc id + String docId = uri + filename; + + // write metadata + docMgr.writeMetadata(docId, writeMetadataHandle); + + // create query options manager + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create query options builder + QueryOptionsBuilder builder = new QueryOptionsBuilder(); + + // create query options handle + QueryOptionsHandle handle = new QueryOptionsHandle(); + + // build query options + handle.withConfiguration(builder.configure().fragmentScope(FragmentScope.PROPERTIES)); + handle.withExtractMetadata(builder.extractMetadata(builder.elementValue(new QName("", "Author")), builder.constraintValue("appname"))); + handle.withConstraints(builder.constraint("appname", builder.word(builder.elementTermIndex(new QName("AppName"))))); + + // write query options + optionsMgr.writeOptions("ExtractMetadataWithStructuredSearchAndConstraint", handle); + + // create query manager + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder("ExtractMetadataWithStructuredSearchAndConstraint"); + StructuredQueryDefinition queryTerm = qb.term("MarkLogic"); + StructuredQueryDefinition queryWord = qb.wordConstraint("appname", "Microsoft"); + StructuredQueryDefinition queryFinal = qb.and(queryTerm, queryWord); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(queryFinal, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println(convertXMLDocumentToString(resultDoc)); + + //assertXpathEvaluatesTo("MarkLogic", "string(//*[local-name()='result']//*[local-name()='metadata']/*[local-name()='Author'])", resultDoc); + //assertXpathEvaluatesTo("Microsoft Office Word", "string(//*[local-name()='result']//*[local-name()='metadata']/*[local-name()='AppName'])", resultDoc); + + // release client + client.release(); + }*/ + + @Test + public void testExtractMetadataWithStructuredSearchAndRangeConstraint() throws XpathException, TransformerException, ParserConfigurationException, SAXException, IOException + { + System.out.println("testExtractMetadataWithStructuredSearchAndRangeConstraint"); + + String filename = "xml-original.xml"; + String uri = "/extract-metadata/"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + ServerConfigurationManager scMgr = client.newServerConfigManager(); + scMgr.setServerRequestLogging(true); + scMgr.writeConfiguration(); + + // get the original metadata + Document docMetadata = getXMLMetadata("metadata-original.xml"); + + // create doc manager + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + + // write the doc + writeDocumentUsingInputStreamHandle(client, filename, uri, "XML"); + + // create handle to write metadata + DOMHandle writeMetadataHandle = new DOMHandle(); + writeMetadataHandle.set(docMetadata); + + // create doc id + String docId = uri + filename; + + // write metadata + docMgr.writeMetadata(docId, writeMetadataHandle); + + // create query options manager + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create query options builder + QueryOptionsBuilder builder = new QueryOptionsBuilder(); + + // create query options handle + QueryOptionsHandle handle = new QueryOptionsHandle(); + + // build query options + handle.withConfiguration(builder.configure().fragmentScope(FragmentScope.PROPERTIES)); + handle.withExtractMetadata(builder.extractMetadata(builder.elementValue(new QName("", "Author")), builder.constraintValue("pop"))); + //handle.withConstraints(builder.constraint("appname", builder.word(builder.elementTermIndex(new QName("AppName"))))); + handle.withConstraints(builder.constraint("pop", + builder.range(builder.elementRangeIndex(new QName("popularity"), + builder.rangeType("xs:int")), + Facets.FACETED, + null, + builder.buckets(builder.bucket("high", "High", "5", null), + builder.bucket("medium", "Medium", "3", "5"), + builder.bucket("low", "Low", "1", "3"))))); + + // write query options + optionsMgr.writeOptions("ExtractMetadataWithStructuredSearchAndRangeConstraint", handle); + + // read query option + StringHandle readHandle = new StringHandle(); + readHandle.setFormat(Format.XML); + optionsMgr.readOptions("ExtractMetadataWithStructuredSearchAndRangeConstraint", readHandle); + String output = readHandle.get(); + System.out.println(output); + + // create query manager + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder("ExtractMetadataWithStructuredSearchAndRangeConstraint"); + StructuredQueryDefinition queryFinal = qb.rangeConstraint("pop", StructuredQueryBuilder.Operator.EQ, "high"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(queryFinal, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("MarkLogic", "string(//*[local-name()='result']//*[local-name()='metadata']/*[local-name()='Author'])", resultDoc); + assertXpathEvaluatesTo("5", "string(//*[local-name()='result']//*[local-name()='metadata']/*[local-name()='constraint-meta'])", resultDoc); + //assertXpathEvaluatesTo("Microsoft Office Word", "string(//*[local-name()='result']//*[local-name()='metadata']/*[local-name()='AppName'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testDocumentLevelMetadata() throws XpathException, TransformerException, ParserConfigurationException, SAXException, IOException + { + System.out.println("testDocumentLevelMetadata"); + + String filename = "xml-original.xml"; + String uri = "/extract-metadata/"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + ServerConfigurationManager scMgr = client.newServerConfigManager(); + scMgr.setServerRequestLogging(true); + scMgr.writeConfiguration(); + + // get the original metadata + Document docMetadata = getXMLMetadata("metadata-original.xml"); + + // create doc manager + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + + // write the doc + writeDocumentUsingInputStreamHandle(client, filename, uri, "XML"); + + // create handle to write metadata + DOMHandle writeMetadataHandle = new DOMHandle(); + writeMetadataHandle.set(docMetadata); + + // create doc id + String docId = uri + filename; + + // write metadata + docMgr.writeMetadata(docId, writeMetadataHandle); + + // create query options manager + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create query options builder + QueryOptionsBuilder builder = new QueryOptionsBuilder(); + + // create query options handle + QueryOptionsHandle handle = new QueryOptionsHandle(); + + // build query options + handle.withConfiguration(builder.configure().fragmentScope(FragmentScope.DOCUMENTS)); + handle.withExtractMetadata(builder.extractMetadata(builder.elementValue(new QName("", "Author")), builder.constraintValue("pop"), builder.elementValue(new QName("", "name")))); + //handle.withConstraints(builder.constraint("appname", builder.word(builder.elementTermIndex(new QName("AppName"))))); + handle.withConstraints(builder.constraint("pop", + builder.range(builder.elementRangeIndex(new QName("popularity"), + builder.rangeType("xs:int")), + Facets.FACETED, + null, + builder.buckets(builder.bucket("high", "High", "5", null), + builder.bucket("medium", "Medium", "3", "5"), + builder.bucket("low", "Low", "1", "3"))))); + + // write query options + optionsMgr.writeOptions("DocumentLevelMetadata", handle); + + // read query option + StringHandle readHandle = new StringHandle(); + readHandle.setFormat(Format.XML); + optionsMgr.readOptions("DocumentLevelMetadata", readHandle); + String output = readHandle.get(); + System.out.println(output); + + // create query manager + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder("DocumentLevelMetadata"); + StructuredQueryDefinition queryFinal = qb.term("noodle"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(queryFinal, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + //System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("noodle", "string(//*[local-name()='result']//*[local-name()='metadata']/*[local-name()='name'])", resultDoc); + //assertXpathEvaluatesTo("5", "string(//*[local-name()='result']//*[local-name()='metadata']/*[local-name()='constraint-meta'])", resultDoc); + //assertXpathEvaluatesTo("Microsoft Office Word", "string(//*[local-name()='result']//*[local-name()='metadata']/*[local-name()='AppName'])", resultDoc); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderGrammar.java b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderGrammar.java index 0c4b28e09..92bd95ced 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderGrammar.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderGrammar.java @@ -1,343 +1,341 @@ -package com.marklogic.javaclient; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; - -import java.io.FileNotFoundException; - -import javax.xml.namespace.QName; -import javax.xml.transform.TransformerException; - -import com.marklogic.client.admin.QueryOptionsManager; -import com.marklogic.client.admin.config.QueryOptions.QueryGrammar.QueryJoiner.JoinerApply; -import com.marklogic.client.admin.config.QueryOptions.QueryGrammar.Tokenize; -import com.marklogic.client.admin.config.QueryOptionsBuilder; -import com.marklogic.client.impl.Utilities; -import com.marklogic.client.io.Format; -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.query.StringQueryDefinition; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.w3c.dom.Document; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.QueryOptionsHandle; -import com.marklogic.client.io.StringHandle; - -import org.junit.*; -public class TestQueryOptionBuilderGrammar extends BasicJavaClientREST { - - private static String dbName = "TestQueryOptionBuilderGrammarDB"; - private static String [] fNames = {"TestQueryOptionBuilderGrammarDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; -@BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - } - - -@After -public void testCleanUp() throws Exception -{ - clearDB(8011); - System.out.println("Running clear script"); -} - -@SuppressWarnings("deprecation") -@Test public void testGrammarOperatorQuotation() throws FileNotFoundException, XpathException, TransformerException - { - System.out.println("Running testGrammarOperatorQuotation"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/gramar-op-quote/", "XML"); - } - - // create query options manager - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create query options builder - QueryOptionsBuilder builder = new QueryOptionsBuilder(); - - // create query options handle - QueryOptionsHandle handle = new QueryOptionsHandle(); - - // build query options - handle.withConfiguration(builder.configure() - .returnMetrics(false) - .returnQtext(false)) - .withTransformResults(builder.rawResults()) - .withGrammar(builder.grammar(builder.starters(builder.starterGrouping("(", 30, ")"), - builder.starterPrefix("-", 40, "cts:not-query")), - builder.joiners(builder.joiner("OR", 20, JoinerApply.INFIX, "cts:or-query", Tokenize.WORD), - builder.joiner("AND", 30, JoinerApply.INFIX, "cts:and-query", Tokenize.WORD), - builder.joiner(":", 50, JoinerApply.CONSTRAINT)), - "\"", - Utilities.domElement(""))); - - // write query options - optionsMgr.writeOptions("GrammarOperatorQuotation", handle); - - // read query option - StringHandle readHandle = new StringHandle(); - readHandle.setFormat(Format.JSON); - optionsMgr.readOptions("GrammarOperatorQuotation", readHandle); - String output = readHandle.get(); - System.out.println(output); - - // create query manager - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition("GrammarOperatorQuotation"); - querydef.setCriteria("1945 OR \"Atlantic Monthly\""); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - resultsHandle.setFormat(Format.XML); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("0113", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - assertXpathEvaluatesTo("0011", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testGrammarTwoWordsSpace() throws FileNotFoundException, XpathException, TransformerException - { - System.out.println("Running testGrammarTwoWordsSpace"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/gramar-two-words-space/", "XML"); - } - - // create query options manager - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create query options builder - QueryOptionsBuilder builder = new QueryOptionsBuilder(); - - // create query options handle - QueryOptionsHandle handle = new QueryOptionsHandle(); - - // build query options - handle.withConfiguration(builder.configure() - .returnMetrics(false) - .returnQtext(false)) - .withTransformResults(builder.rawResults()) - .withGrammar(builder.grammar(builder.starters(builder.starterGrouping("(", 30, ")"), - builder.starterPrefix("-", 40, "cts:not-query")), - builder.joiners(builder.joiner("OR", 20, JoinerApply.INFIX, "cts:or-query", Tokenize.WORD), - builder.joiner("AND", 30, JoinerApply.INFIX, "cts:and-query", Tokenize.WORD), - builder.joiner(":", 50, JoinerApply.CONSTRAINT)), - "\"", - Utilities.domElement(""))); - - // write query options - optionsMgr.writeOptions("GrammarTwoWordsSpace", handle); - - // read query option - StringHandle readHandle = new StringHandle(); - readHandle.setFormat(Format.JSON); - optionsMgr.readOptions("GrammarTwoWordsSpace", readHandle); - String output = readHandle.get(); - System.out.println(output); - - // create query manager - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition("GrammarTwoWordsSpace"); - querydef.setCriteria("\"Atlantic Monthly\" \"Bush\""); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - resultsHandle.setFormat(Format.XML); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("0011", "string(//*[local-name()='result']//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testGrammarPrecedenceAndNegate() throws FileNotFoundException, XpathException, TransformerException - { - System.out.println("Running testGrammarPrecedenceAndNegate"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/gramar-two-words-space/", "XML"); - } - - // create query options manager - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create query options builder - QueryOptionsBuilder builder = new QueryOptionsBuilder(); - - // create query options handle - QueryOptionsHandle handle = new QueryOptionsHandle(); - - // build query options - handle.withConfiguration(builder.configure() - .returnMetrics(false) - .returnQtext(false)) - .withTransformResults(builder.rawResults()) - .withGrammar(builder.grammar(builder.starters(builder.starterGrouping("(", 30, ")"), - builder.starterPrefix("-", 40, "cts:not-query")), - builder.joiners(builder.joiner("OR", 10, JoinerApply.INFIX, "cts:or-query", Tokenize.WORD), - builder.joiner("AND", 20, JoinerApply.INFIX, "cts:and-query", Tokenize.WORD), - builder.joiner(":", 50, JoinerApply.CONSTRAINT)), - "\"", - Utilities.domElement(""))); - - // write query options - optionsMgr.writeOptions("GrammarPrecedenceAndNegate", handle); - - // read query option - StringHandle readHandle = new StringHandle(); - readHandle.setFormat(Format.JSON); - optionsMgr.readOptions("GrammarPrecedenceAndNegate", readHandle); - String output = readHandle.get(); - System.out.println(output); - - // create query manager - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition("GrammarPrecedenceAndNegate"); - querydef.setCriteria("-bush AND -memex"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - resultsHandle.setFormat(Format.XML); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("0024", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - assertXpathEvaluatesTo("0113", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testGrammarConstraint() throws FileNotFoundException, XpathException, TransformerException - { - System.out.println("Running testGrammarConstraint"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/gramar-two-words-space/", "XML"); - } - - // create query options manager - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create query options builder - QueryOptionsBuilder builder = new QueryOptionsBuilder(); - - // create query options handle - QueryOptionsHandle handle = new QueryOptionsHandle(); - - // build query options - handle.withConfiguration(builder.configure() - .returnMetrics(false) - .returnQtext(false)) - .withTransformResults(builder.rawResults()) - .withConstraints(builder.constraint("intitle", builder.word(builder.elementTermIndex(new QName("title"))))) - .withGrammar(builder.grammar(builder.starters(builder.starterGrouping("(", 30, ")"), - builder.starterPrefix("-", 20, "cts:not-query")), - builder.joiners(builder.joiner("OR", 20, JoinerApply.INFIX, "cts:or-query", Tokenize.WORD), - builder.joiner("AND", 30, JoinerApply.INFIX, "cts:and-query", Tokenize.WORD), - builder.joiner(":", 50, JoinerApply.CONSTRAINT)), - "\"", - Utilities.domElement(""))); - - // write query options - optionsMgr.writeOptions("GrammarConstraint", handle); - - // read query option - StringHandle readHandle = new StringHandle(); - readHandle.setFormat(Format.JSON); - optionsMgr.readOptions("GrammarConstraint", readHandle); - String output = readHandle.get(); - System.out.println(output); - - // create query manager - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition("GrammarConstraint"); - querydef.setCriteria("intitle:Vannevar AND served"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - resultsHandle.setFormat(Format.XML); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("0024", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} +package com.marklogic.javaclient; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; + +import java.io.FileNotFoundException; + +import javax.xml.namespace.QName; +import javax.xml.transform.TransformerException; + +import com.marklogic.client.admin.QueryOptionsManager; +import com.marklogic.client.admin.config.QueryOptions.QueryGrammar.QueryJoiner.JoinerApply; +import com.marklogic.client.admin.config.QueryOptions.QueryGrammar.Tokenize; +import com.marklogic.client.admin.config.QueryOptionsBuilder; +import com.marklogic.client.impl.Utilities; +import com.marklogic.client.io.Format; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.StringQueryDefinition; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.w3c.dom.Document; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.QueryOptionsHandle; +import com.marklogic.client.io.StringHandle; + +import org.junit.*; +public class TestQueryOptionBuilderGrammar extends BasicJavaClientREST { + + private static String dbName = "TestQueryOptionBuilderGrammarDB"; + private static String [] fNames = {"TestQueryOptionBuilderGrammarDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + } + + @After + public void testCleanUp() throws Exception + { + clearDB(8011); + System.out.println("Running clear script"); + } + + @Test + public void testGrammarOperatorQuotation() throws FileNotFoundException, XpathException, TransformerException + { + System.out.println("Running testGrammarOperatorQuotation"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/gramar-op-quote/", "XML"); + } + + // create query options manager + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create query options builder + QueryOptionsBuilder builder = new QueryOptionsBuilder(); + + // create query options handle + QueryOptionsHandle handle = new QueryOptionsHandle(); + + // build query options + handle.withConfiguration(builder.configure() + .returnMetrics(false) + .returnQtext(false)) + .withTransformResults(builder.rawResults()) + .withGrammar(builder.grammar(builder.starters(builder.starterGrouping("(", 30, ")"), + builder.starterPrefix("-", 40, "cts:not-query")), + builder.joiners(builder.joiner("OR", 20, JoinerApply.INFIX, "cts:or-query", Tokenize.WORD), + builder.joiner("AND", 30, JoinerApply.INFIX, "cts:and-query", Tokenize.WORD), + builder.joiner(":", 50, JoinerApply.CONSTRAINT)), + "\"", + Utilities.domElement(""))); + + // write query options + optionsMgr.writeOptions("GrammarOperatorQuotation", handle); + + // read query option + StringHandle readHandle = new StringHandle(); + readHandle.setFormat(Format.JSON); + optionsMgr.readOptions("GrammarOperatorQuotation", readHandle); + String output = readHandle.get(); + System.out.println(output); + + // create query manager + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition("GrammarOperatorQuotation"); + querydef.setCriteria("1945 OR \"Atlantic Monthly\""); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + resultsHandle.setFormat(Format.XML); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + //System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("0113", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); + assertXpathEvaluatesTo("0011", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testGrammarTwoWordsSpace() throws FileNotFoundException, XpathException, TransformerException + { + System.out.println("Running testGrammarTwoWordsSpace"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/gramar-two-words-space/", "XML"); + } + + // create query options manager + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create query options builder + QueryOptionsBuilder builder = new QueryOptionsBuilder(); + + // create query options handle + QueryOptionsHandle handle = new QueryOptionsHandle(); + + // build query options + handle.withConfiguration(builder.configure() + .returnMetrics(false) + .returnQtext(false)) + .withTransformResults(builder.rawResults()) + .withGrammar(builder.grammar(builder.starters(builder.starterGrouping("(", 30, ")"), + builder.starterPrefix("-", 40, "cts:not-query")), + builder.joiners(builder.joiner("OR", 20, JoinerApply.INFIX, "cts:or-query", Tokenize.WORD), + builder.joiner("AND", 30, JoinerApply.INFIX, "cts:and-query", Tokenize.WORD), + builder.joiner(":", 50, JoinerApply.CONSTRAINT)), + "\"", + Utilities.domElement(""))); + + // write query options + optionsMgr.writeOptions("GrammarTwoWordsSpace", handle); + + // read query option + StringHandle readHandle = new StringHandle(); + readHandle.setFormat(Format.JSON); + optionsMgr.readOptions("GrammarTwoWordsSpace", readHandle); + String output = readHandle.get(); + System.out.println(output); + + // create query manager + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition("GrammarTwoWordsSpace"); + querydef.setCriteria("\"Atlantic Monthly\" \"Bush\""); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + resultsHandle.setFormat(Format.XML); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + //System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("0011", "string(//*[local-name()='result']//*[local-name()='id'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testGrammarPrecedenceAndNegate() throws FileNotFoundException, XpathException, TransformerException + { + System.out.println("Running testGrammarPrecedenceAndNegate"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/gramar-two-words-space/", "XML"); + } + + // create query options manager + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create query options builder + QueryOptionsBuilder builder = new QueryOptionsBuilder(); + + // create query options handle + QueryOptionsHandle handle = new QueryOptionsHandle(); + + // build query options + handle.withConfiguration(builder.configure() + .returnMetrics(false) + .returnQtext(false)) + .withTransformResults(builder.rawResults()) + .withGrammar(builder.grammar(builder.starters(builder.starterGrouping("(", 30, ")"), + builder.starterPrefix("-", 40, "cts:not-query")), + builder.joiners(builder.joiner("OR", 10, JoinerApply.INFIX, "cts:or-query", Tokenize.WORD), + builder.joiner("AND", 20, JoinerApply.INFIX, "cts:and-query", Tokenize.WORD), + builder.joiner(":", 50, JoinerApply.CONSTRAINT)), + "\"", + Utilities.domElement(""))); + + // write query options + optionsMgr.writeOptions("GrammarPrecedenceAndNegate", handle); + + // read query option + StringHandle readHandle = new StringHandle(); + readHandle.setFormat(Format.JSON); + optionsMgr.readOptions("GrammarPrecedenceAndNegate", readHandle); + String output = readHandle.get(); + System.out.println(output); + + // create query manager + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition("GrammarPrecedenceAndNegate"); + querydef.setCriteria("-bush AND -memex"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + resultsHandle.setFormat(Format.XML); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + //System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("0024", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); + assertXpathEvaluatesTo("0113", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testGrammarConstraint() throws FileNotFoundException, XpathException, TransformerException + { + System.out.println("Running testGrammarConstraint"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/gramar-two-words-space/", "XML"); + } + + // create query options manager + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create query options builder + QueryOptionsBuilder builder = new QueryOptionsBuilder(); + + // create query options handle + QueryOptionsHandle handle = new QueryOptionsHandle(); + + // build query options + handle.withConfiguration(builder.configure() + .returnMetrics(false) + .returnQtext(false)) + .withTransformResults(builder.rawResults()) + .withConstraints(builder.constraint("intitle", builder.word(builder.elementTermIndex(new QName("title"))))) + .withGrammar(builder.grammar(builder.starters(builder.starterGrouping("(", 30, ")"), + builder.starterPrefix("-", 20, "cts:not-query")), + builder.joiners(builder.joiner("OR", 20, JoinerApply.INFIX, "cts:or-query", Tokenize.WORD), + builder.joiner("AND", 30, JoinerApply.INFIX, "cts:and-query", Tokenize.WORD), + builder.joiner(":", 50, JoinerApply.CONSTRAINT)), + "\"", + Utilities.domElement(""))); + + // write query options + optionsMgr.writeOptions("GrammarConstraint", handle); + + // read query option + StringHandle readHandle = new StringHandle(); + readHandle.setFormat(Format.JSON); + optionsMgr.readOptions("GrammarConstraint", readHandle); + String output = readHandle.get(); + System.out.println(output); + + // create query manager + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition("GrammarConstraint"); + querydef.setCriteria("intitle:Vannevar AND served"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + resultsHandle.setFormat(Format.XML); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + //System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("0024", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderSearchOptions.java b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderSearchOptions.java index 7de5359dd..841945e73 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderSearchOptions.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderSearchOptions.java @@ -1,190 +1,191 @@ -package com.marklogic.javaclient; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; - -import java.io.FileNotFoundException; -import java.util.ArrayList; -import java.util.List; - -import javax.xml.transform.TransformerException; - -import com.marklogic.client.admin.QueryOptionsManager; -import com.marklogic.client.admin.config.QueryOptionsBuilder; -import com.marklogic.client.io.Format; -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.query.StringQueryDefinition; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.w3c.dom.Document; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.QueryOptionsHandle; -import com.marklogic.client.io.StringHandle; -import org.junit.*; -public class TestQueryOptionBuilderSearchOptions extends BasicJavaClientREST { - - private static String dbName = "TestQueryOptionBuilderSearchOptionsDB"; - private static String [] fNames = {"TestQueryOptionBuilderSearchOptionsDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - } - - -@SuppressWarnings("deprecation") -@Test public void testSearchOptions1() throws FileNotFoundException, XpathException, TransformerException - { - System.out.println("Running testSearchOptions1"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/search-ops-1/", "XML"); - } - - // create query options manager - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create query options builder - QueryOptionsBuilder builder = new QueryOptionsBuilder(); - - // create query options handle - QueryOptionsHandle handle = new QueryOptionsHandle(); - - List listOfSearchOptions = new ArrayList (); - listOfSearchOptions.add("checked"); - listOfSearchOptions.add("filtered"); - listOfSearchOptions.add("score-simple"); - - // build query options - handle.withConfiguration(builder.configure() - .returnMetrics(false) - .returnQtext(false) - .debug(true)) - .withTransformResults(builder.rawResults()) - .setSearchOptions(listOfSearchOptions); - - // write query options - optionsMgr.writeOptions("SearchOptions1", handle); - - // read query option - StringHandle readHandle = new StringHandle(); - readHandle.setFormat(Format.JSON); - optionsMgr.readOptions("SearchOptions1", readHandle); - String output = readHandle.get(); - System.out.println(output); - - // create query manager - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition("SearchOptions1"); - querydef.setCriteria("bush"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - resultsHandle.setFormat(Format.XML); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - String expectedSearchReport = "(cts:search(fn:collection(), cts:word-query(\"bush\", (\"lang=en\"), 1), (\"checked\",\"filtered\",\"score-simple\"), 1))[1 to 10]"; - - assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testSearchOptions2() throws FileNotFoundException, XpathException, TransformerException - { - System.out.println("Running testSearchOptions2"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/search-ops-2/", "XML"); - } - - // create query options manager - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create query options builder - QueryOptionsBuilder builder = new QueryOptionsBuilder(); - - // create query options handle - QueryOptionsHandle handle = new QueryOptionsHandle(); - - List listOfSearchOptions = new ArrayList (); - listOfSearchOptions.add("unchecked"); - listOfSearchOptions.add("unfiltered"); - listOfSearchOptions.add("score-logtfidf"); - - // build query options - handle.withConfiguration(builder.configure() - .returnMetrics(false) - .returnQtext(false) - .debug(true)) - .withTransformResults(builder.rawResults()) - .setSearchOptions(listOfSearchOptions); - - // write query options - optionsMgr.writeOptions("SearchOptions2", handle); - - // read query option - StringHandle readHandle = new StringHandle(); - readHandle.setFormat(Format.JSON); - optionsMgr.readOptions("SearchOptions2", readHandle); - String output = readHandle.get(); - System.out.println(output); - - // create query manager - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition("SearchOptions2"); - querydef.setCriteria("bush"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - resultsHandle.setFormat(Format.XML); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - String expectedSearchReport = "(cts:search(fn:collection(), cts:word-query(\"bush\", (\"lang=en\"), 1), (\"unchecked\",\"unfiltered\",\"score-logtfidf\"), 1))[1 to 10]"; - - assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); - - // release client - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} +package com.marklogic.javaclient; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; + +import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.transform.TransformerException; + +import com.marklogic.client.admin.QueryOptionsManager; +import com.marklogic.client.admin.config.QueryOptionsBuilder; +import com.marklogic.client.io.Format; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.StringQueryDefinition; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.w3c.dom.Document; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.QueryOptionsHandle; +import com.marklogic.client.io.StringHandle; +import org.junit.*; + +public class TestQueryOptionBuilderSearchOptions extends BasicJavaClientREST { + + private static String dbName = "TestQueryOptionBuilderSearchOptionsDB"; + private static String [] fNames = {"TestQueryOptionBuilderSearchOptionsDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + } + + @Test + public void testSearchOptions1() throws FileNotFoundException, XpathException, TransformerException + { + System.out.println("Running testSearchOptions1"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/search-ops-1/", "XML"); + } + + // create query options manager + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create query options builder + QueryOptionsBuilder builder = new QueryOptionsBuilder(); + + // create query options handle + QueryOptionsHandle handle = new QueryOptionsHandle(); + + List listOfSearchOptions = new ArrayList (); + listOfSearchOptions.add("checked"); + listOfSearchOptions.add("filtered"); + listOfSearchOptions.add("score-simple"); + + // build query options + handle.withConfiguration(builder.configure() + .returnMetrics(false) + .returnQtext(false) + .debug(true)) + .withTransformResults(builder.rawResults()) + .setSearchOptions(listOfSearchOptions); + + // write query options + optionsMgr.writeOptions("SearchOptions1", handle); + + // read query option + StringHandle readHandle = new StringHandle(); + readHandle.setFormat(Format.JSON); + optionsMgr.readOptions("SearchOptions1", readHandle); + String output = readHandle.get(); + System.out.println(output); + + // create query manager + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition("SearchOptions1"); + querydef.setCriteria("bush"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + resultsHandle.setFormat(Format.XML); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + //System.out.println(convertXMLDocumentToString(resultDoc)); + + String expectedSearchReport = "(cts:search(fn:collection(), cts:word-query(\"bush\", (\"lang=en\"), 1), (\"checked\",\"filtered\",\"score-simple\"), 1))[1 to 10]"; + + assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testSearchOptions2() throws FileNotFoundException, XpathException, TransformerException + { + System.out.println("Running testSearchOptions2"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/search-ops-2/", "XML"); + } + + // create query options manager + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create query options builder + QueryOptionsBuilder builder = new QueryOptionsBuilder(); + + // create query options handle + QueryOptionsHandle handle = new QueryOptionsHandle(); + + List listOfSearchOptions = new ArrayList (); + listOfSearchOptions.add("unchecked"); + listOfSearchOptions.add("unfiltered"); + listOfSearchOptions.add("score-logtfidf"); + + // build query options + handle.withConfiguration(builder.configure() + .returnMetrics(false) + .returnQtext(false) + .debug(true)) + .withTransformResults(builder.rawResults()) + .setSearchOptions(listOfSearchOptions); + + // write query options + optionsMgr.writeOptions("SearchOptions2", handle); + + // read query option + StringHandle readHandle = new StringHandle(); + readHandle.setFormat(Format.JSON); + optionsMgr.readOptions("SearchOptions2", readHandle); + String output = readHandle.get(); + System.out.println(output); + + // create query manager + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition("SearchOptions2"); + querydef.setCriteria("bush"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + resultsHandle.setFormat(Format.XML); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + //System.out.println(convertXMLDocumentToString(resultDoc)); + + String expectedSearchReport = "(cts:search(fn:collection(), cts:word-query(\"bush\", (\"lang=en\"), 1), (\"unchecked\",\"unfiltered\",\"score-logtfidf\"), 1))[1 to 10]"; + + assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderSearchableExpression.java b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderSearchableExpression.java index 9f27234e1..b61351b65 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderSearchableExpression.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderSearchableExpression.java @@ -1,399 +1,398 @@ -package com.marklogic.javaclient; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; - -import java.io.FileNotFoundException; - -import javax.xml.transform.TransformerException; - -import com.marklogic.client.admin.QueryOptionsManager; -import com.marklogic.client.admin.config.QueryOptionsBuilder; -import com.marklogic.client.io.Format; -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.query.StringQueryDefinition; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.w3c.dom.Document; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.QueryOptionsHandle; -import com.marklogic.client.io.StringHandle; - -import org.junit.*; -public class TestQueryOptionBuilderSearchableExpression extends BasicJavaClientREST { - - private static String dbName = "TestQueryOptionBuilderSearchableExpressionDB"; - private static String [] fNames = {"TestQueryOptionBuilderSearchableExpressionDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0],restServerName,8011); - setupAppServicesConstraint(dbName); - } - -@After -public void testCleanUp() throws Exception -{ - clearDB(8011); - System.out.println("Running clear script"); -} - -@SuppressWarnings("deprecation") -@Test public void testSearchableExpressionChildAxis() throws FileNotFoundException, XpathException, TransformerException - { - System.out.println("Running testSearchableExpressionChildAxis"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/search-expr-child-axis/", "XML"); - } - - // create query options manager - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create query options builder - QueryOptionsBuilder builder = new QueryOptionsBuilder(); - - // create query options handle - QueryOptionsHandle handle = new QueryOptionsHandle(); - - // build query options - handle.withConfiguration(builder.configure() - .returnMetrics(false) - .returnQtext(false)) - .withTransformResults(builder.rawResults()) - .withSearchableExpression(builder.searchableExpression("//root/child::p")); - - // write query options - optionsMgr.writeOptions("SearchableExpressionChildAxis", handle); - - // read query option - StringHandle readHandle = new StringHandle(); - readHandle.setFormat(Format.JSON); - optionsMgr.readOptions("SearchableExpressionChildAxis", readHandle); - String output = readHandle.get(); - System.out.println(output); - - //String expectedOutput = "{\"options\":{\"sort-order\":[{\"direction\":\"descending\", \"score\":null},{\"type\":\"xs:decimal\", \"direction\":\"ascending\", \"attribute\":{\"ns\":\"\", \"name\":\"amt\"}, \"element\":{\"ns\":\"http:\\/\\/cloudbank.com\", \"name\":\"price\"}}], \"return-metrics\":false, \"return-qtext\":false, \"transform-results\":{\"apply\":\"raw\"}}}"; - //assertTrue("Query Options in json is incorrect", output.contains(expectedOutput)); - - // create query manager - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition("SearchableExpressionChildAxis"); - querydef.setCriteria("bush"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - resultsHandle.setFormat(Format.XML); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("The Bush article described a device called a Memex.", "string(//*[local-name()='result'][1]//*[local-name()='p'])", resultDoc); - assertXpathEvaluatesTo("Vannevar Bush wrote an article for The Atlantic Monthly", "string(//*[local-name()='result'][2]//*[local-name()='p'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testSearchableExpressionDescendantAxis() throws FileNotFoundException, XpathException, TransformerException - { - System.out.println("Running testSearchableExpressionDescendantAxis"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/search-expr-desc-axis/", "XML"); - } - - // create query options manager - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create query options builder - QueryOptionsBuilder builder = new QueryOptionsBuilder(); - - // create query options handle - QueryOptionsHandle handle = new QueryOptionsHandle(); - - // build query options - handle.withConfiguration(builder.configure() - .returnMetrics(false) - .returnQtext(false)) - .withTransformResults(builder.rawResults()) - .withSearchableExpression(builder.searchableExpression("/root/descendant::title")); - - // write query options - optionsMgr.writeOptions("SearchableExpressionDescendantAxis", handle); - - // read query option - StringHandle readHandle = new StringHandle(); - readHandle.setFormat(Format.XML); - optionsMgr.readOptions("SearchableExpressionDescendantAxis", readHandle); - String output = readHandle.get(); - System.out.println(output); - - //String expectedOutput = "{\"options\":{\"sort-order\":[{\"direction\":\"descending\", \"score\":null},{\"type\":\"xs:decimal\", \"direction\":\"ascending\", \"attribute\":{\"ns\":\"\", \"name\":\"amt\"}, \"element\":{\"ns\":\"http:\\/\\/cloudbank.com\", \"name\":\"price\"}}], \"return-metrics\":false, \"return-qtext\":false, \"transform-results\":{\"apply\":\"raw\"}}}"; - //assertTrue("Query Options in json is incorrect", output.contains(expectedOutput)); - - // create query manager - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition("SearchableExpressionDescendantAxis"); - querydef.setCriteria("bush OR memex"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - resultsHandle.setFormat(Format.XML); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("3", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("The Bush article", "string(//*[local-name()='result'][1]//*[local-name()='title'])", resultDoc); - assertXpathEvaluatesTo("Vannevar Bush", "string(//*[local-name()='result'][2]//*[local-name()='title'])", resultDoc); - assertXpathEvaluatesTo("The memex", "string(//*[local-name()='result'][3]//*[local-name()='title'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testSearchableExpressionOrOperator() throws FileNotFoundException, XpathException, TransformerException - { - System.out.println("Running testSearchableExpressionOrOperator"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/search-expr-or-op/", "XML"); - } - - // create query options manager - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create query options builder - QueryOptionsBuilder builder = new QueryOptionsBuilder(); - - // create query options handle - QueryOptionsHandle handle = new QueryOptionsHandle(); - - // build query options - handle.withConfiguration(builder.configure() - .returnMetrics(false) - .returnQtext(false)) - .withTransformResults(builder.snippetTransform(30, 4, 200)) - .withSearchableExpression(builder.searchableExpression("//(title|id)")); - - // write query options - optionsMgr.writeOptions("SearchableExpressionOrOperator", handle); - - // read query option - StringHandle readHandle = new StringHandle(); - readHandle.setFormat(Format.XML); - optionsMgr.readOptions("SearchableExpressionOrOperator", readHandle); - String output = readHandle.get(); - System.out.println(output); - - //String expectedOutput = "{\"options\":{\"sort-order\":[{\"direction\":\"descending\", \"score\":null},{\"type\":\"xs:decimal\", \"direction\":\"ascending\", \"attribute\":{\"ns\":\"\", \"name\":\"amt\"}, \"element\":{\"ns\":\"http:\\/\\/cloudbank.com\", \"name\":\"price\"}}], \"return-metrics\":false, \"return-qtext\":false, \"transform-results\":{\"apply\":\"raw\"}}}"; - //assertTrue("Query Options in json is incorrect", output.contains(expectedOutput)); - - // create query manager - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition("SearchableExpressionOrOperator"); - querydef.setCriteria("bush OR 0011"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - resultsHandle.setFormat(Format.XML); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("3", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("Bush", "string(//*[local-name()='result'][1]//*[local-name()='highlight'])", resultDoc); - assertXpathEvaluatesTo("0011", "string(//*[local-name()='result'][2]//*[local-name()='highlight'])", resultDoc); - assertXpathEvaluatesTo("Bush", "string(//*[local-name()='result'][3]//*[local-name()='highlight'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testSearchableExpressionDescendantOrSelf() throws FileNotFoundException, XpathException, TransformerException - { - System.out.println("Running testSearchableExpressionDescendantOrSelf"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/search-expr-desc-or-self/", "XML"); - } - - // create query options manager - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create query options builder - QueryOptionsBuilder builder = new QueryOptionsBuilder(); - - // create query options handle - QueryOptionsHandle handle = new QueryOptionsHandle(); - - // build query options - handle.withConfiguration(builder.configure() - .returnMetrics(false) - .returnQtext(false)) - .withTransformResults(builder.snippetTransform(30, 10, 200)) - .withSearchableExpression(builder.searchableExpression("/descendant-or-self::root")); - - // write query options - optionsMgr.writeOptions("SearchableExpressionDescendantOrSelf", handle); - - // read query option - StringHandle readHandle = new StringHandle(); - readHandle.setFormat(Format.XML); - optionsMgr.readOptions("SearchableExpressionDescendantOrSelf", readHandle); - String output = readHandle.get(); - System.out.println(output); - - //String expectedOutput = "{\"options\":{\"sort-order\":[{\"direction\":\"descending\", \"score\":null},{\"type\":\"xs:decimal\", \"direction\":\"ascending\", \"attribute\":{\"ns\":\"\", \"name\":\"amt\"}, \"element\":{\"ns\":\"http:\\/\\/cloudbank.com\", \"name\":\"price\"}}], \"return-metrics\":false, \"return-qtext\":false, \"transform-results\":{\"apply\":\"raw\"}}}"; - //assertTrue("Query Options in json is incorrect", output.contains(expectedOutput)); - - // create query manager - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition("SearchableExpressionDescendantOrSelf"); - querydef.setCriteria("Bush"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - resultsHandle.setFormat(Format.XML); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - //assertXpathEvaluatesTo("Vannevar Bush", "string(//*[local-name()='result'][1]//*[local-name()='match'][1])", resultDoc); - //assertXpathEvaluatesTo("Vannevar Bush wrote an article for The Atlantic Monthly", "string(//*[local-name()='result'][1]//*[local-name()='match'][2])", resultDoc); - //assertXpathEvaluatesTo("The Bush article", "string(//*[local-name()='result'][2]//*[local-name()='match'][1])", resultDoc); - //assertXpathEvaluatesTo("The Bush article described a device called a Memex.", "string(//*[local-name()='result'][2]//*[local-name()='match'][2])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testSearchableExpressionFunction() throws FileNotFoundException, XpathException, TransformerException - { - System.out.println("Running testSearchableExpressionFunction"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/search-expr-func/", "XML"); - } - - // create query options manager - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create query options builder - QueryOptionsBuilder builder = new QueryOptionsBuilder(); - - // create query options handle - QueryOptionsHandle handle = new QueryOptionsHandle(); - - // build query options - handle.withConfiguration(builder.configure() - .returnMetrics(false) - .returnQtext(false)) - .withTransformResults(builder.snippetTransform(30, 10, 200)) - .withSearchableExpression(builder.searchableExpression("//p[contains(.,\"groundbreaking\")]")); - - // write query options - optionsMgr.writeOptions("SearchableExpressionFunction", handle); - - // read query option - StringHandle readHandle = new StringHandle(); - readHandle.setFormat(Format.XML); - optionsMgr.readOptions("SearchableExpressionFunction", readHandle); - String output = readHandle.get(); - System.out.println(output); - - //String expectedOutput = "{\"options\":{\"sort-order\":[{\"direction\":\"descending\", \"score\":null},{\"type\":\"xs:decimal\", \"direction\":\"ascending\", \"attribute\":{\"ns\":\"\", \"name\":\"amt\"}, \"element\":{\"ns\":\"http:\\/\\/cloudbank.com\", \"name\":\"price\"}}], \"return-metrics\":false, \"return-qtext\":false, \"transform-results\":{\"apply\":\"raw\"}}}"; - //assertTrue("Query Options in json is incorrect", output.contains(expectedOutput)); - - // create query manager - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition("SearchableExpressionFunction"); - querydef.setCriteria("atlantic"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - resultsHandle.setFormat(Format.XML); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("/search-expr-func/constraint3.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); - - // release client - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames,restServerName); - - } -} +package com.marklogic.javaclient; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; + +import java.io.FileNotFoundException; + +import javax.xml.transform.TransformerException; + +import com.marklogic.client.admin.QueryOptionsManager; +import com.marklogic.client.admin.config.QueryOptionsBuilder; +import com.marklogic.client.io.Format; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.StringQueryDefinition; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.w3c.dom.Document; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.QueryOptionsHandle; +import com.marklogic.client.io.StringHandle; + +import org.junit.*; + +public class TestQueryOptionBuilderSearchableExpression extends BasicJavaClientREST { + + private static String dbName = "TestQueryOptionBuilderSearchableExpressionDB"; + private static String [] fNames = {"TestQueryOptionBuilderSearchableExpressionDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0],restServerName,8011); + setupAppServicesConstraint(dbName); + } + + @After + public void testCleanUp() throws Exception + { + clearDB(8011); + System.out.println("Running clear script"); + } + + @Test + public void testSearchableExpressionChildAxis() throws FileNotFoundException, XpathException, TransformerException + { + System.out.println("Running testSearchableExpressionChildAxis"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/search-expr-child-axis/", "XML"); + } + + // create query options manager + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create query options builder + QueryOptionsBuilder builder = new QueryOptionsBuilder(); + + // create query options handle + QueryOptionsHandle handle = new QueryOptionsHandle(); + + // build query options + handle.withConfiguration(builder.configure() + .returnMetrics(false) + .returnQtext(false)) + .withTransformResults(builder.rawResults()) + .withSearchableExpression(builder.searchableExpression("//root/child::p")); + + // write query options + optionsMgr.writeOptions("SearchableExpressionChildAxis", handle); + + // read query option + StringHandle readHandle = new StringHandle(); + readHandle.setFormat(Format.JSON); + optionsMgr.readOptions("SearchableExpressionChildAxis", readHandle); + String output = readHandle.get(); + System.out.println(output); + + //String expectedOutput = "{\"options\":{\"sort-order\":[{\"direction\":\"descending\", \"score\":null},{\"type\":\"xs:decimal\", \"direction\":\"ascending\", \"attribute\":{\"ns\":\"\", \"name\":\"amt\"}, \"element\":{\"ns\":\"http:\\/\\/cloudbank.com\", \"name\":\"price\"}}], \"return-metrics\":false, \"return-qtext\":false, \"transform-results\":{\"apply\":\"raw\"}}}"; + //assertTrue("Query Options in json is incorrect", output.contains(expectedOutput)); + + // create query manager + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition("SearchableExpressionChildAxis"); + querydef.setCriteria("bush"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + resultsHandle.setFormat(Format.XML); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + //System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("The Bush article described a device called a Memex.", "string(//*[local-name()='result'][1]//*[local-name()='p'])", resultDoc); + assertXpathEvaluatesTo("Vannevar Bush wrote an article for The Atlantic Monthly", "string(//*[local-name()='result'][2]//*[local-name()='p'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testSearchableExpressionDescendantAxis() throws FileNotFoundException, XpathException, TransformerException + { + System.out.println("Running testSearchableExpressionDescendantAxis"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/search-expr-desc-axis/", "XML"); + } + + // create query options manager + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create query options builder + QueryOptionsBuilder builder = new QueryOptionsBuilder(); + + // create query options handle + QueryOptionsHandle handle = new QueryOptionsHandle(); + + // build query options + handle.withConfiguration(builder.configure() + .returnMetrics(false) + .returnQtext(false)) + .withTransformResults(builder.rawResults()) + .withSearchableExpression(builder.searchableExpression("/root/descendant::title")); + + // write query options + optionsMgr.writeOptions("SearchableExpressionDescendantAxis", handle); + + // read query option + StringHandle readHandle = new StringHandle(); + readHandle.setFormat(Format.XML); + optionsMgr.readOptions("SearchableExpressionDescendantAxis", readHandle); + String output = readHandle.get(); + System.out.println(output); + + //String expectedOutput = "{\"options\":{\"sort-order\":[{\"direction\":\"descending\", \"score\":null},{\"type\":\"xs:decimal\", \"direction\":\"ascending\", \"attribute\":{\"ns\":\"\", \"name\":\"amt\"}, \"element\":{\"ns\":\"http:\\/\\/cloudbank.com\", \"name\":\"price\"}}], \"return-metrics\":false, \"return-qtext\":false, \"transform-results\":{\"apply\":\"raw\"}}}"; + //assertTrue("Query Options in json is incorrect", output.contains(expectedOutput)); + + // create query manager + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition("SearchableExpressionDescendantAxis"); + querydef.setCriteria("bush OR memex"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + resultsHandle.setFormat(Format.XML); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + //System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("3", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("The Bush article", "string(//*[local-name()='result'][1]//*[local-name()='title'])", resultDoc); + assertXpathEvaluatesTo("Vannevar Bush", "string(//*[local-name()='result'][2]//*[local-name()='title'])", resultDoc); + assertXpathEvaluatesTo("The memex", "string(//*[local-name()='result'][3]//*[local-name()='title'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testSearchableExpressionOrOperator() throws FileNotFoundException, XpathException, TransformerException + { + System.out.println("Running testSearchableExpressionOrOperator"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/search-expr-or-op/", "XML"); + } + + // create query options manager + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create query options builder + QueryOptionsBuilder builder = new QueryOptionsBuilder(); + + // create query options handle + QueryOptionsHandle handle = new QueryOptionsHandle(); + + // build query options + handle.withConfiguration(builder.configure() + .returnMetrics(false) + .returnQtext(false)) + .withTransformResults(builder.snippetTransform(30, 4, 200)) + .withSearchableExpression(builder.searchableExpression("//(title|id)")); + + // write query options + optionsMgr.writeOptions("SearchableExpressionOrOperator", handle); + + // read query option + StringHandle readHandle = new StringHandle(); + readHandle.setFormat(Format.XML); + optionsMgr.readOptions("SearchableExpressionOrOperator", readHandle); + String output = readHandle.get(); + System.out.println(output); + + //String expectedOutput = "{\"options\":{\"sort-order\":[{\"direction\":\"descending\", \"score\":null},{\"type\":\"xs:decimal\", \"direction\":\"ascending\", \"attribute\":{\"ns\":\"\", \"name\":\"amt\"}, \"element\":{\"ns\":\"http:\\/\\/cloudbank.com\", \"name\":\"price\"}}], \"return-metrics\":false, \"return-qtext\":false, \"transform-results\":{\"apply\":\"raw\"}}}"; + //assertTrue("Query Options in json is incorrect", output.contains(expectedOutput)); + + // create query manager + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition("SearchableExpressionOrOperator"); + querydef.setCriteria("bush OR 0011"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + resultsHandle.setFormat(Format.XML); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + //System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("3", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("Bush", "string(//*[local-name()='result'][1]//*[local-name()='highlight'])", resultDoc); + assertXpathEvaluatesTo("0011", "string(//*[local-name()='result'][2]//*[local-name()='highlight'])", resultDoc); + assertXpathEvaluatesTo("Bush", "string(//*[local-name()='result'][3]//*[local-name()='highlight'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testSearchableExpressionDescendantOrSelf() throws FileNotFoundException, XpathException, TransformerException + { + System.out.println("Running testSearchableExpressionDescendantOrSelf"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/search-expr-desc-or-self/", "XML"); + } + + // create query options manager + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create query options builder + QueryOptionsBuilder builder = new QueryOptionsBuilder(); + + // create query options handle + QueryOptionsHandle handle = new QueryOptionsHandle(); + + // build query options + handle.withConfiguration(builder.configure() + .returnMetrics(false) + .returnQtext(false)) + .withTransformResults(builder.snippetTransform(30, 10, 200)) + .withSearchableExpression(builder.searchableExpression("/descendant-or-self::root")); + + // write query options + optionsMgr.writeOptions("SearchableExpressionDescendantOrSelf", handle); + + // read query option + StringHandle readHandle = new StringHandle(); + readHandle.setFormat(Format.XML); + optionsMgr.readOptions("SearchableExpressionDescendantOrSelf", readHandle); + String output = readHandle.get(); + System.out.println(output); + + //String expectedOutput = "{\"options\":{\"sort-order\":[{\"direction\":\"descending\", \"score\":null},{\"type\":\"xs:decimal\", \"direction\":\"ascending\", \"attribute\":{\"ns\":\"\", \"name\":\"amt\"}, \"element\":{\"ns\":\"http:\\/\\/cloudbank.com\", \"name\":\"price\"}}], \"return-metrics\":false, \"return-qtext\":false, \"transform-results\":{\"apply\":\"raw\"}}}"; + //assertTrue("Query Options in json is incorrect", output.contains(expectedOutput)); + + // create query manager + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition("SearchableExpressionDescendantOrSelf"); + querydef.setCriteria("Bush"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + resultsHandle.setFormat(Format.XML); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + //System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + //assertXpathEvaluatesTo("Vannevar Bush", "string(//*[local-name()='result'][1]//*[local-name()='match'][1])", resultDoc); + //assertXpathEvaluatesTo("Vannevar Bush wrote an article for The Atlantic Monthly", "string(//*[local-name()='result'][1]//*[local-name()='match'][2])", resultDoc); + //assertXpathEvaluatesTo("The Bush article", "string(//*[local-name()='result'][2]//*[local-name()='match'][1])", resultDoc); + //assertXpathEvaluatesTo("The Bush article described a device called a Memex.", "string(//*[local-name()='result'][2]//*[local-name()='match'][2])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testSearchableExpressionFunction() throws FileNotFoundException, XpathException, TransformerException + { + System.out.println("Running testSearchableExpressionFunction"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/search-expr-func/", "XML"); + } + + // create query options manager + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create query options builder + QueryOptionsBuilder builder = new QueryOptionsBuilder(); + + // create query options handle + QueryOptionsHandle handle = new QueryOptionsHandle(); + + // build query options + handle.withConfiguration(builder.configure() + .returnMetrics(false) + .returnQtext(false)) + .withTransformResults(builder.snippetTransform(30, 10, 200)) + .withSearchableExpression(builder.searchableExpression("//p[contains(.,\"groundbreaking\")]")); + + // write query options + optionsMgr.writeOptions("SearchableExpressionFunction", handle); + + // read query option + StringHandle readHandle = new StringHandle(); + readHandle.setFormat(Format.XML); + optionsMgr.readOptions("SearchableExpressionFunction", readHandle); + String output = readHandle.get(); + System.out.println(output); + + //String expectedOutput = "{\"options\":{\"sort-order\":[{\"direction\":\"descending\", \"score\":null},{\"type\":\"xs:decimal\", \"direction\":\"ascending\", \"attribute\":{\"ns\":\"\", \"name\":\"amt\"}, \"element\":{\"ns\":\"http:\\/\\/cloudbank.com\", \"name\":\"price\"}}], \"return-metrics\":false, \"return-qtext\":false, \"transform-results\":{\"apply\":\"raw\"}}}"; + //assertTrue("Query Options in json is incorrect", output.contains(expectedOutput)); + + // create query manager + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition("SearchableExpressionFunction"); + querydef.setCriteria("atlantic"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + resultsHandle.setFormat(Format.XML); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + //System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("/search-expr-func/constraint3.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames,restServerName); + + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderSortOrder.java b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderSortOrder.java index b531aee93..ea11403d1 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderSortOrder.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderSortOrder.java @@ -1,534 +1,534 @@ -package com.marklogic.javaclient; - -import java.io.IOException; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; -import static org.junit.Assert.*; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; - -import java.io.FileNotFoundException; - -import javax.xml.namespace.QName; -import javax.xml.transform.TransformerException; - -import com.marklogic.client.admin.QueryOptionsManager; -import com.marklogic.client.admin.config.QueryOptionsBuilder; -import com.marklogic.client.io.Format; -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.query.StringQueryDefinition; -import com.marklogic.client.admin.config.QueryOptions.QuerySortOrder.Direction; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.w3c.dom.Document; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.QueryOptionsHandle; -import com.marklogic.client.io.StringHandle; - -import org.junit.*; -public class TestQueryOptionBuilderSortOrder extends BasicJavaClientREST { - - private static String dbName = "TestQueryOptionBuilderSortOrderDB"; - private static String [] fNames = {"TestQueryOptionBuilderSortOrderDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort = 8011; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - } - -@After - public void testCleanUp() throws Exception - { - clearDB(restPort); - System.out.println("Running clear script"); - } - -@SuppressWarnings("deprecation") -@Test public void testSortOrderDescendingScore() throws FileNotFoundException, XpathException, TransformerException - { - System.out.println("Running testSortOrderDescendingScore"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/sort-desc-score/", "XML"); - } - - // create query options manager - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create query options builder - QueryOptionsBuilder builder = new QueryOptionsBuilder(); - - // create query options handle - QueryOptionsHandle handle = new QueryOptionsHandle(); - - // build query options - handle.withConfiguration(builder.configure() - .returnMetrics(false) - .returnQtext(false)) - .withTransformResults(builder.rawResults()) - .withSortOrders(builder.sortByScore(Direction.DESCENDING)); - - // write query options - optionsMgr.writeOptions("SortOrderDescendingScore", handle); - - // read query option - StringHandle readHandle = new StringHandle(); - readHandle.setFormat(Format.XML); - optionsMgr.readOptions("SortOrderDescendingScore", readHandle); - String output = readHandle.get(); - System.out.println(output); - - // create query manager - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition("SortOrderDescendingScore"); - querydef.setCriteria("bush OR memex"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - resultsHandle.setFormat(Format.XML); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("3", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - assertXpathEvaluatesTo("0011", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); - assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][3]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testSortOrderPrimaryDescScoreSecondaryAscDate() throws FileNotFoundException, XpathException, TransformerException, IOException - { - System.out.println("Running testSortOrderPrimaryDescScoreSecondaryAscDate"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/sort-desc-score-asc-date/", "XML"); - } - - // create query options manager - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create query options builder - QueryOptionsBuilder builder = new QueryOptionsBuilder(); - - // create query options handle - QueryOptionsHandle handle = new QueryOptionsHandle(); - - // build query options - handle.withConfiguration(builder.configure() - .returnMetrics(false) - .returnQtext(false)) - .withTransformResults(builder.rawResults()) - .withSortOrders(builder.sortByScore(Direction.DESCENDING), builder.sortOrder(builder.elementRangeIndex(new QName("http://purl.org/dc/elements/1.1/", "date"), builder.rangeType("xs:date")), Direction.ASCENDING)); - - // write query options - optionsMgr.writeOptions("SortOrderPrimaryDescScoreSecondaryAscDate", handle); - - // read query option - StringHandle readHandle = new StringHandle(); - readHandle.setFormat(Format.JSON); - optionsMgr.readOptions("SortOrderPrimaryDescScoreSecondaryAscDate", readHandle); - String output = readHandle.get(); - System.out.println(output + "testSortOrderPrimaryDescScoreSecondaryAscDate"); - - // ----------- Validate options node ---------------------- - ObjectMapper mapper = new ObjectMapper(); - JsonNode optionsContent = mapper.readTree(output); - assertNotNull(optionsContent); - System.out.println("JSON output: " + optionsContent); - - JsonNode optionsNode = optionsContent.get("options"); - assertNotNull(optionsNode); - - JsonNode metrics = optionsNode.get("return-metrics"); - assertNotNull(metrics); - assertEquals(metrics.booleanValue(), false); - - JsonNode qtext = optionsNode.get("return-qtext"); - assertNotNull(qtext); - assertEquals(qtext.booleanValue(), false); - - JsonNode sortOrders = optionsNode.get("sort-order"); - assertNotNull(sortOrders); - System.out.println(optionsContent.get("options").get("sort-order")); - - assertTrue(sortOrders.isArray()); - for (final JsonNode sortOrder : sortOrders) { - assertNotNull(sortOrder.get("direction")); - String direction = sortOrder.get("direction").textValue(); - - if (direction.equals("descending")) { - assertTrue(sortOrder.has("score")); - assertTrue(sortOrder.get("score").isNull()); - } - else if (direction.equals("ascending")) { - assertTrue(sortOrder.has("type")); - assertEquals(sortOrder.get("type").textValue(), "xs:date"); - - JsonNode element = sortOrder.get("element"); - assertNotNull(element); - - JsonNode name = element.get("name"); - assertNotNull(name); - assertEquals(name.textValue(), "date"); - - JsonNode ns = element.get("ns"); - assertNotNull(ns); - assertEquals(ns.textValue(), "http://purl.org/dc/elements/1.1/"); - } - else { - assertTrue("Found an unexpected object", false); - } - } - - JsonNode transformResults = optionsNode.get("transform-results"); - assertNotNull(transformResults); - - JsonNode apply = transformResults.get("apply"); - assertNotNull(apply); - assertEquals(apply.textValue(), "raw"); - - // ----------- Validate search using options node created ---------------------- - // create query manager - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition("SortOrderPrimaryDescScoreSecondaryAscDate"); - querydef.setCriteria("bush OR memex"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - resultsHandle.setFormat(Format.XML); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("3", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - assertXpathEvaluatesTo("0011", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); - assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][3]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testMultipleSortOrder() throws FileNotFoundException, XpathException, TransformerException, IOException - { - System.out.println("Running testMultipleSortOrder"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/mult-sort-order/", "XML"); - } - - // create query options manager - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create query options builder - QueryOptionsBuilder builder = new QueryOptionsBuilder(); - - // create query options handle - QueryOptionsHandle handle = new QueryOptionsHandle(); - - // build query options - handle.withConfiguration(builder.configure() - .returnMetrics(false) - .returnQtext(false)) - .withTransformResults(builder.rawResults()) - .withSortOrders(builder.sortByScore(Direction.DESCENDING), - builder.sortOrder(builder.elementRangeIndex(new QName("", "popularity"), builder.rangeType("xs:int")), Direction.ASCENDING), - builder.sortOrder(builder.elementRangeIndex(new QName("", "title"), builder.rangeType("xs:string")), Direction.DESCENDING)); - - // write query options - optionsMgr.writeOptions("MultipleSortOrder", handle); - - // read query option - StringHandle readHandle = new StringHandle(); - readHandle.setFormat(Format.JSON); - optionsMgr.readOptions("MultipleSortOrder", readHandle); - String output = readHandle.get(); - System.out.println(output); - - // ----------- Validate options node ---------------------- - ObjectMapper mapper = new ObjectMapper(); - JsonNode optionsContent = mapper.readTree(output); - assertNotNull(optionsContent); - System.out.println("JSON output: " + optionsContent); - - JsonNode optionsNode = optionsContent.get("options"); - assertNotNull(optionsNode); - - JsonNode metrics = optionsNode.get("return-metrics"); - assertNotNull(metrics); - assertEquals(metrics.booleanValue(), false); - - JsonNode qtext = optionsNode.get("return-qtext"); - assertNotNull(qtext); - assertEquals(qtext.booleanValue(), false); - - JsonNode sortOrders = optionsNode.get("sort-order"); - assertNotNull(sortOrders); - System.out.println(optionsContent.get("options").get("sort-order")); - - assertTrue(sortOrders.isArray()); - for (final JsonNode sortOrder : sortOrders) { - assertNotNull(sortOrder.get("direction")); - String direction = sortOrder.get("direction").textValue(); - - if (direction.equals("descending")) { - if (sortOrder.has("score")) { - assertTrue(sortOrder.get("score").isNull()); - } - else if (sortOrder.has("type")) { - assertEquals(sortOrder.get("type").textValue(), "xs:string"); - - JsonNode element = sortOrder.get("element"); - assertNotNull(element); - - JsonNode elementName = element.get("name"); - assertNotNull(elementName); - assertEquals(elementName.textValue(), "title"); - - JsonNode elementNS = element.get("ns"); - assertNotNull(elementNS); - assertEquals(elementNS.textValue(), ""); - } - else { - assertTrue("Found an unexpected object", false); - } - - } - else if (direction.equals("ascending")) { - assertTrue(sortOrder.has("type")); - assertEquals(sortOrder.get("type").textValue(), "xs:int"); - - JsonNode element = sortOrder.get("element"); - assertNotNull(element); - - JsonNode elementName = element.get("name"); - assertNotNull(elementName); - assertEquals(elementName.textValue(), "popularity"); - - JsonNode elementNS = element.get("ns"); - assertNotNull(elementNS); - assertEquals(elementNS.textValue(), ""); - } - else { - assertTrue("Found an unexpected object", false); - } - } - - JsonNode transformResults = optionsNode.get("transform-results"); - assertNotNull(transformResults); - - JsonNode apply = transformResults.get("apply"); - assertNotNull(apply); - assertEquals(apply.textValue(), "raw"); - - // ----------- Search based on options node inserted ---------------------- - // create query manager - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition("MultipleSortOrder"); - querydef.setCriteria("Vannevar OR memex"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - resultsHandle.setFormat(Format.XML); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("4", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("0024", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - assertXpathEvaluatesTo("0011", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); - assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][3]//*[local-name()='id'])", resultDoc); - assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][4]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testSortOrderAttribute() throws FileNotFoundException, XpathException, TransformerException, IOException - { - System.out.println("Running testSortOrderAttribute"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/attr-sort-order/", "XML"); - } - - // create query options manager - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create query options builder - QueryOptionsBuilder builder = new QueryOptionsBuilder(); - - // create query options handle - QueryOptionsHandle handle = new QueryOptionsHandle(); - - // build query options - handle.withConfiguration(builder.configure() - .returnMetrics(false) - .returnQtext(false)) - .withTransformResults(builder.rawResults()) - .withSortOrders(builder.sortByScore(Direction.DESCENDING), - builder.sortOrder(builder.elementAttributeRangeIndex(new QName("http://cloudbank.com", "price"), new QName("", "amt"), builder.rangeType("xs:decimal")), Direction.ASCENDING)); - - // write query options - optionsMgr.writeOptions("SortOrderAttribute", handle); - - // read query option - StringHandle readHandle = new StringHandle(); - readHandle.setFormat(Format.JSON); - optionsMgr.readOptions("SortOrderAttribute", readHandle); - String output = readHandle.get(); - System.out.println(output); - - // ----------- Validate options node ---------------------- - ObjectMapper mapper = new ObjectMapper(); - JsonNode optionsContent = mapper.readTree(output); - assertNotNull(optionsContent); - System.out.println("JSON output: " + optionsContent); - - JsonNode optionsNode = optionsContent.get("options"); - assertNotNull(optionsNode); - - JsonNode metrics = optionsNode.get("return-metrics"); - assertNotNull(metrics); - assertEquals(metrics.booleanValue(), false); - - JsonNode qtext = optionsNode.get("return-qtext"); - assertNotNull(qtext); - assertEquals(qtext.booleanValue(), false); - - JsonNode sortOrders = optionsNode.get("sort-order"); - assertNotNull(sortOrders); - System.out.println(optionsContent.get("options").get("sort-order")); - - assertTrue(sortOrders.isArray()); - for (final JsonNode sortOrder : sortOrders) { - assertNotNull(sortOrder.get("direction")); - String direction = sortOrder.get("direction").textValue(); - - if (direction.equals("descending")) { - assertTrue(sortOrder.has("score")); - assertTrue(sortOrder.get("score").isNull()); - } - else if (direction.equals("ascending")) { - assertTrue(sortOrder.has("type")); - assertEquals(sortOrder.get("type").textValue(), "xs:decimal"); - - JsonNode attribute = sortOrder.get("attribute"); - assertNotNull(attribute); - - JsonNode attributeName = attribute.get("name"); - assertNotNull(attributeName); - assertEquals(attributeName.textValue(), "amt"); - - JsonNode attributeNS = attribute.get("ns"); - assertNotNull(attributeNS); - assertEquals(attributeNS.textValue(), ""); - - JsonNode element = sortOrder.get("element"); - assertNotNull(element); - - JsonNode elementName = element.get("name"); - assertNotNull(elementName); - assertEquals(elementName.textValue(), "price"); - - JsonNode elementNS = element.get("ns"); - assertNotNull(elementNS); - assertEquals(elementNS.textValue(), "http://cloudbank.com"); - } - else { - assertTrue("Found an unexpected object", false); - } - } - - JsonNode transformResults = optionsNode.get("transform-results"); - assertNotNull(transformResults); - - JsonNode apply = transformResults.get("apply"); - assertNotNull(apply); - assertEquals(apply.textValue(), "raw"); - - // ----------- Validate search using options node created ---------------------- - // create query manager - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition("SortOrderAttribute"); - querydef.setCriteria("Bush OR Memex"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - resultsHandle.setFormat(Format.XML); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("3", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - assertXpathEvaluatesTo("0011", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); - assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][3]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames,restServerName); - } -} +package com.marklogic.javaclient; + +import java.io.IOException; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; +import static org.junit.Assert.*; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.io.FileNotFoundException; + +import javax.xml.namespace.QName; +import javax.xml.transform.TransformerException; + +import com.marklogic.client.admin.QueryOptionsManager; +import com.marklogic.client.admin.config.QueryOptionsBuilder; +import com.marklogic.client.io.Format; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.StringQueryDefinition; +import com.marklogic.client.admin.config.QueryOptions.QuerySortOrder.Direction; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.w3c.dom.Document; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.QueryOptionsHandle; +import com.marklogic.client.io.StringHandle; + +import org.junit.*; + +public class TestQueryOptionBuilderSortOrder extends BasicJavaClientREST { + + private static String dbName = "TestQueryOptionBuilderSortOrderDB"; + private static String [] fNames = {"TestQueryOptionBuilderSortOrderDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + } + + @After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + + @Test + public void testSortOrderDescendingScore() throws FileNotFoundException, XpathException, TransformerException + { + System.out.println("Running testSortOrderDescendingScore"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/sort-desc-score/", "XML"); + } + + // create query options manager + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create query options builder + QueryOptionsBuilder builder = new QueryOptionsBuilder(); + + // create query options handle + QueryOptionsHandle handle = new QueryOptionsHandle(); + + // build query options + handle.withConfiguration(builder.configure() + .returnMetrics(false) + .returnQtext(false)) + .withTransformResults(builder.rawResults()) + .withSortOrders(builder.sortByScore(Direction.DESCENDING)); + + // write query options + optionsMgr.writeOptions("SortOrderDescendingScore", handle); + + // read query option + StringHandle readHandle = new StringHandle(); + readHandle.setFormat(Format.XML); + optionsMgr.readOptions("SortOrderDescendingScore", readHandle); + String output = readHandle.get(); + System.out.println(output); + + // create query manager + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition("SortOrderDescendingScore"); + querydef.setCriteria("bush OR memex"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + resultsHandle.setFormat(Format.XML); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + //System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("3", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); + assertXpathEvaluatesTo("0011", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); + assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][3]//*[local-name()='id'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testSortOrderPrimaryDescScoreSecondaryAscDate() throws FileNotFoundException, XpathException, TransformerException, IOException + { + System.out.println("Running testSortOrderPrimaryDescScoreSecondaryAscDate"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/sort-desc-score-asc-date/", "XML"); + } + + // create query options manager + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create query options builder + QueryOptionsBuilder builder = new QueryOptionsBuilder(); + + // create query options handle + QueryOptionsHandle handle = new QueryOptionsHandle(); + + // build query options + handle.withConfiguration(builder.configure() + .returnMetrics(false) + .returnQtext(false)) + .withTransformResults(builder.rawResults()) + .withSortOrders(builder.sortByScore(Direction.DESCENDING), builder.sortOrder(builder.elementRangeIndex(new QName("http://purl.org/dc/elements/1.1/", "date"), builder.rangeType("xs:date")), Direction.ASCENDING)); + + // write query options + optionsMgr.writeOptions("SortOrderPrimaryDescScoreSecondaryAscDate", handle); + + // read query option + StringHandle readHandle = new StringHandle(); + readHandle.setFormat(Format.JSON); + optionsMgr.readOptions("SortOrderPrimaryDescScoreSecondaryAscDate", readHandle); + String output = readHandle.get(); + System.out.println(output + "testSortOrderPrimaryDescScoreSecondaryAscDate"); + + // ----------- Validate options node ---------------------- + ObjectMapper mapper = new ObjectMapper(); + JsonNode optionsContent = mapper.readTree(output); + assertNotNull(optionsContent); + System.out.println("JSON output: " + optionsContent); + + JsonNode optionsNode = optionsContent.get("options"); + assertNotNull(optionsNode); + + JsonNode metrics = optionsNode.get("return-metrics"); + assertNotNull(metrics); + assertEquals(metrics.booleanValue(), false); + + JsonNode qtext = optionsNode.get("return-qtext"); + assertNotNull(qtext); + assertEquals(qtext.booleanValue(), false); + + JsonNode sortOrders = optionsNode.get("sort-order"); + assertNotNull(sortOrders); + System.out.println(optionsContent.get("options").get("sort-order")); + + assertTrue(sortOrders.isArray()); + for (final JsonNode sortOrder : sortOrders) { + assertNotNull(sortOrder.get("direction")); + String direction = sortOrder.get("direction").textValue(); + + if (direction.equals("descending")) { + assertTrue(sortOrder.has("score")); + assertTrue(sortOrder.get("score").isNull()); + } + else if (direction.equals("ascending")) { + assertTrue(sortOrder.has("type")); + assertEquals(sortOrder.get("type").textValue(), "xs:date"); + + JsonNode element = sortOrder.get("element"); + assertNotNull(element); + + JsonNode name = element.get("name"); + assertNotNull(name); + assertEquals(name.textValue(), "date"); + + JsonNode ns = element.get("ns"); + assertNotNull(ns); + assertEquals(ns.textValue(), "http://purl.org/dc/elements/1.1/"); + } + else { + assertTrue("Found an unexpected object", false); + } + } + + JsonNode transformResults = optionsNode.get("transform-results"); + assertNotNull(transformResults); + + JsonNode apply = transformResults.get("apply"); + assertNotNull(apply); + assertEquals(apply.textValue(), "raw"); + + // ----------- Validate search using options node created ---------------------- + // create query manager + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition("SortOrderPrimaryDescScoreSecondaryAscDate"); + querydef.setCriteria("bush OR memex"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + resultsHandle.setFormat(Format.XML); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("3", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); + assertXpathEvaluatesTo("0011", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); + assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][3]//*[local-name()='id'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testMultipleSortOrder() throws FileNotFoundException, XpathException, TransformerException, IOException + { + System.out.println("Running testMultipleSortOrder"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/mult-sort-order/", "XML"); + } + + // create query options manager + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create query options builder + QueryOptionsBuilder builder = new QueryOptionsBuilder(); + + // create query options handle + QueryOptionsHandle handle = new QueryOptionsHandle(); + + // build query options + handle.withConfiguration(builder.configure() + .returnMetrics(false) + .returnQtext(false)) + .withTransformResults(builder.rawResults()) + .withSortOrders(builder.sortByScore(Direction.DESCENDING), + builder.sortOrder(builder.elementRangeIndex(new QName("", "popularity"), builder.rangeType("xs:int")), Direction.ASCENDING), + builder.sortOrder(builder.elementRangeIndex(new QName("", "title"), builder.rangeType("xs:string")), Direction.DESCENDING)); + + // write query options + optionsMgr.writeOptions("MultipleSortOrder", handle); + + // read query option + StringHandle readHandle = new StringHandle(); + readHandle.setFormat(Format.JSON); + optionsMgr.readOptions("MultipleSortOrder", readHandle); + String output = readHandle.get(); + System.out.println(output); + + // ----------- Validate options node ---------------------- + ObjectMapper mapper = new ObjectMapper(); + JsonNode optionsContent = mapper.readTree(output); + assertNotNull(optionsContent); + System.out.println("JSON output: " + optionsContent); + + JsonNode optionsNode = optionsContent.get("options"); + assertNotNull(optionsNode); + + JsonNode metrics = optionsNode.get("return-metrics"); + assertNotNull(metrics); + assertEquals(metrics.booleanValue(), false); + + JsonNode qtext = optionsNode.get("return-qtext"); + assertNotNull(qtext); + assertEquals(qtext.booleanValue(), false); + + JsonNode sortOrders = optionsNode.get("sort-order"); + assertNotNull(sortOrders); + System.out.println(optionsContent.get("options").get("sort-order")); + + assertTrue(sortOrders.isArray()); + for (final JsonNode sortOrder : sortOrders) { + assertNotNull(sortOrder.get("direction")); + String direction = sortOrder.get("direction").textValue(); + + if (direction.equals("descending")) { + if (sortOrder.has("score")) { + assertTrue(sortOrder.get("score").isNull()); + } + else if (sortOrder.has("type")) { + assertEquals(sortOrder.get("type").textValue(), "xs:string"); + + JsonNode element = sortOrder.get("element"); + assertNotNull(element); + + JsonNode elementName = element.get("name"); + assertNotNull(elementName); + assertEquals(elementName.textValue(), "title"); + + JsonNode elementNS = element.get("ns"); + assertNotNull(elementNS); + assertEquals(elementNS.textValue(), ""); + } + else { + assertTrue("Found an unexpected object", false); + } + + } + else if (direction.equals("ascending")) { + assertTrue(sortOrder.has("type")); + assertEquals(sortOrder.get("type").textValue(), "xs:int"); + + JsonNode element = sortOrder.get("element"); + assertNotNull(element); + + JsonNode elementName = element.get("name"); + assertNotNull(elementName); + assertEquals(elementName.textValue(), "popularity"); + + JsonNode elementNS = element.get("ns"); + assertNotNull(elementNS); + assertEquals(elementNS.textValue(), ""); + } + else { + assertTrue("Found an unexpected object", false); + } + } + + JsonNode transformResults = optionsNode.get("transform-results"); + assertNotNull(transformResults); + + JsonNode apply = transformResults.get("apply"); + assertNotNull(apply); + assertEquals(apply.textValue(), "raw"); + + // ----------- Search based on options node inserted ---------------------- + // create query manager + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition("MultipleSortOrder"); + querydef.setCriteria("Vannevar OR memex"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + resultsHandle.setFormat(Format.XML); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + //System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("4", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("0024", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); + assertXpathEvaluatesTo("0011", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); + assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][3]//*[local-name()='id'])", resultDoc); + assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][4]//*[local-name()='id'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testSortOrderAttribute() throws FileNotFoundException, XpathException, TransformerException, IOException + { + System.out.println("Running testSortOrderAttribute"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/attr-sort-order/", "XML"); + } + + // create query options manager + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create query options builder + QueryOptionsBuilder builder = new QueryOptionsBuilder(); + + // create query options handle + QueryOptionsHandle handle = new QueryOptionsHandle(); + + // build query options + handle.withConfiguration(builder.configure() + .returnMetrics(false) + .returnQtext(false)) + .withTransformResults(builder.rawResults()) + .withSortOrders(builder.sortByScore(Direction.DESCENDING), + builder.sortOrder(builder.elementAttributeRangeIndex(new QName("http://cloudbank.com", "price"), new QName("", "amt"), builder.rangeType("xs:decimal")), Direction.ASCENDING)); + + // write query options + optionsMgr.writeOptions("SortOrderAttribute", handle); + + // read query option + StringHandle readHandle = new StringHandle(); + readHandle.setFormat(Format.JSON); + optionsMgr.readOptions("SortOrderAttribute", readHandle); + String output = readHandle.get(); + System.out.println(output); + + // ----------- Validate options node ---------------------- + ObjectMapper mapper = new ObjectMapper(); + JsonNode optionsContent = mapper.readTree(output); + assertNotNull(optionsContent); + System.out.println("JSON output: " + optionsContent); + + JsonNode optionsNode = optionsContent.get("options"); + assertNotNull(optionsNode); + + JsonNode metrics = optionsNode.get("return-metrics"); + assertNotNull(metrics); + assertEquals(metrics.booleanValue(), false); + + JsonNode qtext = optionsNode.get("return-qtext"); + assertNotNull(qtext); + assertEquals(qtext.booleanValue(), false); + + JsonNode sortOrders = optionsNode.get("sort-order"); + assertNotNull(sortOrders); + System.out.println(optionsContent.get("options").get("sort-order")); + + assertTrue(sortOrders.isArray()); + for (final JsonNode sortOrder : sortOrders) { + assertNotNull(sortOrder.get("direction")); + String direction = sortOrder.get("direction").textValue(); + + if (direction.equals("descending")) { + assertTrue(sortOrder.has("score")); + assertTrue(sortOrder.get("score").isNull()); + } + else if (direction.equals("ascending")) { + assertTrue(sortOrder.has("type")); + assertEquals(sortOrder.get("type").textValue(), "xs:decimal"); + + JsonNode attribute = sortOrder.get("attribute"); + assertNotNull(attribute); + + JsonNode attributeName = attribute.get("name"); + assertNotNull(attributeName); + assertEquals(attributeName.textValue(), "amt"); + + JsonNode attributeNS = attribute.get("ns"); + assertNotNull(attributeNS); + assertEquals(attributeNS.textValue(), ""); + + JsonNode element = sortOrder.get("element"); + assertNotNull(element); + + JsonNode elementName = element.get("name"); + assertNotNull(elementName); + assertEquals(elementName.textValue(), "price"); + + JsonNode elementNS = element.get("ns"); + assertNotNull(elementNS); + assertEquals(elementNS.textValue(), "http://cloudbank.com"); + } + else { + assertTrue("Found an unexpected object", false); + } + } + + JsonNode transformResults = optionsNode.get("transform-results"); + assertNotNull(transformResults); + + JsonNode apply = transformResults.get("apply"); + assertNotNull(apply); + assertEquals(apply.textValue(), "raw"); + + // ----------- Validate search using options node created ---------------------- + // create query manager + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition("SortOrderAttribute"); + querydef.setCriteria("Bush OR Memex"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + resultsHandle.setFormat(Format.XML); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + //System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("3", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); + assertXpathEvaluatesTo("0011", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); + assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][3]//*[local-name()='id'])", resultDoc); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames,restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderTransformResults.java b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderTransformResults.java index c6d114e8c..c314fb2e1 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderTransformResults.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderTransformResults.java @@ -1,183 +1,186 @@ -package com.marklogic.javaclient; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; -import static org.junit.Assert.*; - -import java.io.FileNotFoundException; -import java.io.IOException; - -import javax.xml.namespace.QName; -import javax.xml.transform.TransformerException; - -import com.marklogic.client.admin.QueryOptionsManager; -import com.marklogic.client.admin.config.QueryOptionsBuilder; -import com.marklogic.client.io.Format; -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.query.StringQueryDefinition; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.QueryOptionsHandle; -import com.marklogic.client.io.StringHandle; - -import org.junit.*; -public class TestQueryOptionBuilderTransformResults extends BasicJavaClientREST { - - private static String dbName = "TestQueryOptionBuilderTransformResultsDB"; - private static String [] fNames = {"TestQueryOptionBuilderTransformResultsDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort = 8011; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - } - -@After - public void testCleanUp() throws Exception - { - clearDB(restPort); - System.out.println("Running clear script"); - } - -@SuppressWarnings("deprecation") -@Test public void testTransformResuleWithSnippetFunction() throws FileNotFoundException, XpathException, TransformerException - { - System.out.println("Running testTransformResuleWithSnippetFunction"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/trans-res-with-snip-func/", "XML"); - } - - // create query options manager - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create query options builder - QueryOptionsBuilder builder = new QueryOptionsBuilder(); - - // create query options handle - QueryOptionsHandle handle = new QueryOptionsHandle(); - - // build query options - handle.withConfiguration(builder.configure() - .returnMetrics(false) - .returnQtext(false)) - .withTransformResults(builder.snippetTransform(30, 4, 200, new QName("ns", "elem"))); - - // write query options - optionsMgr.writeOptions("TransformResuleWithSnippetFunction", handle); - - // read query option - StringHandle readHandle = new StringHandle(); - readHandle.setFormat(Format.JSON); - optionsMgr.readOptions("TransformResuleWithSnippetFunction", readHandle); - String output = readHandle.get(); - System.out.println(output); - - // create query manager - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition("TransformResuleWithSnippetFunction"); - querydef.setCriteria("Atlantic groundbreaking"); - - // create handle - StringHandle resultsHandle = new StringHandle(); - resultsHandle.setFormat(Format.JSON); - queryMgr.search(querydef, resultsHandle); - - // get the result - String resultDoc = resultsHandle.get(); - System.out.println(resultDoc); - - String expectedResult = "{\"snippet-format\":\"snippet\",\"total\":1,\"start\":1,\"page-length\":10,\"results\":[{\"index\":1,\"uri\":\"/trans-res-with-snip-func/constraint3.xml\""; - assertTrue("Result is wrong", resultDoc.contains(expectedResult)); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testTransformResuleWithEmptySnippetFunction() throws XpathException, TransformerException, SAXException, IOException - { - System.out.println("Running testTransformResuleWithEmptySnippetFunction"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/trans-res-with-emp-snip-func/", "XML"); - } - - // create query options manager - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create query options builder - QueryOptionsBuilder builder = new QueryOptionsBuilder(); - - // create query options handle - QueryOptionsHandle handle = new QueryOptionsHandle(); - - // build query options - handle.withConfiguration(builder.configure() - .returnMetrics(false) - .returnQtext(false)) - .withTransformResults(builder.emptySnippets()); - - // write query options - optionsMgr.writeOptions("TransformResuleWithEmptySnippetFunction", handle); - - // read query option - StringHandle readHandle = new StringHandle(); - readHandle.setFormat(Format.XML); - optionsMgr.readOptions("TransformResuleWithEmptySnippetFunction", readHandle); - String output = readHandle.get(); - System.out.println(output); - - // create query manager - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition("TransformResuleWithEmptySnippetFunction"); - querydef.setCriteria("Atlantic groundbreaking"); - - // create handle - StringHandle resultsHandle = new StringHandle(); - resultsHandle.setFormat(Format.XML); - queryMgr.search(querydef, resultsHandle); - - // get the result - String resultDoc = resultsHandle.get(); - System.out.println(resultDoc); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("/trans-res-with-emp-snip-func/constraint3.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); - //assertXpathEvaluatesTo("groundbreaking", "string(//*[local-name()='result']//*[local-name()='highlight'][2])", resultDoc); - - // release client - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} +package com.marklogic.javaclient; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; +import static org.junit.Assert.*; + +import java.io.FileNotFoundException; +import java.io.IOException; + +import javax.xml.namespace.QName; +import javax.xml.transform.TransformerException; + +import com.marklogic.client.admin.QueryOptionsManager; +import com.marklogic.client.admin.config.QueryOptionsBuilder; +import com.marklogic.client.io.Format; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.StringQueryDefinition; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.QueryOptionsHandle; +import com.marklogic.client.io.StringHandle; + +import org.junit.*; + +public class TestQueryOptionBuilderTransformResults extends BasicJavaClientREST { + + private static String dbName = "TestQueryOptionBuilderTransformResultsDB"; + private static String [] fNames = {"TestQueryOptionBuilderTransformResultsDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + } + + @After + public + void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + + @Test + public void testTransformResuleWithSnippetFunction() throws FileNotFoundException, XpathException, TransformerException + { + System.out.println("Running testTransformResuleWithSnippetFunction"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/trans-res-with-snip-func/", "XML"); + } + + // create query options manager + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create query options builder + QueryOptionsBuilder builder = new QueryOptionsBuilder(); + + // create query options handle + QueryOptionsHandle handle = new QueryOptionsHandle(); + + // build query options + handle.withConfiguration(builder.configure() + .returnMetrics(false) + .returnQtext(false)) + .withTransformResults(builder.snippetTransform(30, 4, 200, new QName("ns", "elem"))); + + // write query options + optionsMgr.writeOptions("TransformResuleWithSnippetFunction", handle); + + // read query option + StringHandle readHandle = new StringHandle(); + readHandle.setFormat(Format.JSON); + optionsMgr.readOptions("TransformResuleWithSnippetFunction", readHandle); + String output = readHandle.get(); + System.out.println(output); + + // create query manager + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition("TransformResuleWithSnippetFunction"); + querydef.setCriteria("Atlantic groundbreaking"); + + // create handle + StringHandle resultsHandle = new StringHandle(); + resultsHandle.setFormat(Format.JSON); + queryMgr.search(querydef, resultsHandle); + + // get the result + String resultDoc = resultsHandle.get(); + System.out.println(resultDoc); + + String expectedResult = "{\"snippet-format\":\"snippet\",\"total\":1,\"start\":1,\"page-length\":10,\"results\":[{\"index\":1,\"uri\":\"/trans-res-with-snip-func/constraint3.xml\""; + assertTrue("Result is wrong", resultDoc.contains(expectedResult)); + + // release client + client.release(); + } + + @Test + public void testTransformResuleWithEmptySnippetFunction() throws XpathException, TransformerException, SAXException, IOException + { + System.out.println("Running testTransformResuleWithEmptySnippetFunction"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/trans-res-with-emp-snip-func/", "XML"); + } + + // create query options manager + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create query options builder + QueryOptionsBuilder builder = new QueryOptionsBuilder(); + + // create query options handle + QueryOptionsHandle handle = new QueryOptionsHandle(); + + // build query options + handle.withConfiguration(builder.configure() + .returnMetrics(false) + .returnQtext(false)) + .withTransformResults(builder.emptySnippets()); + + // write query options + optionsMgr.writeOptions("TransformResuleWithEmptySnippetFunction", handle); + + // read query option + StringHandle readHandle = new StringHandle(); + readHandle.setFormat(Format.XML); + optionsMgr.readOptions("TransformResuleWithEmptySnippetFunction", readHandle); + String output = readHandle.get(); + System.out.println(output); + + // create query manager + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition("TransformResuleWithEmptySnippetFunction"); + querydef.setCriteria("Atlantic groundbreaking"); + + // create handle + StringHandle resultsHandle = new StringHandle(); + resultsHandle.setFormat(Format.XML); + queryMgr.search(querydef, resultsHandle); + + // get the result + String resultDoc = resultsHandle.get(); + System.out.println(resultDoc); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("/trans-res-with-emp-snip-func/constraint3.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); + //assertXpathEvaluatesTo("groundbreaking", "string(//*[local-name()='result']//*[local-name()='highlight'][2])", resultDoc); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionsHandle.java b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionsHandle.java index 6fe6064d3..f638d0d6c 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionsHandle.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionsHandle.java @@ -1,316 +1,305 @@ -package com.marklogic.javaclient; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; -import static org.junit.Assert.*; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import com.marklogic.client.query.QueryManager; - -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.admin.QueryOptionsManager; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.query.StringQueryDefinition; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.FileHandle; -import com.marklogic.client.io.Format; -import com.marklogic.client.io.QueryOptionsHandle; -import com.marklogic.client.io.ReaderHandle; -import com.marklogic.client.io.StringHandle; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.junit.*; -public class TestQueryOptionsHandle extends BasicJavaClientREST { - - private static String dbName = "TestQueryOptionsHandleDB"; - private static String [] fNames = {"TestQueryOptionsHandleDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort=8011; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - } - -@After - public void testCleanUp() throws Exception - { - clearDB(restPort); - System.out.println("Running clear script"); - } - - - -@SuppressWarnings("deprecation") -@Test public void testRoundtrippingQueryOptionPOJO() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRoundtrippingQueryOptionPOJO"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "valueConstraintWildCardOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create a manager for writing query options - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/pojo-query-option/", "XML"); - } - - // create handle - ReaderHandle handle = new ReaderHandle(); - - // write the files - BufferedReader docStream = new BufferedReader(new FileReader("src/test/java/com/marklogic/javaclient/queryoptions/" + queryOptionName)); - handle.set(docStream); - - // write the query options to the database - optionsMgr.writeOptions(queryOptionName, handle); - - //System.out.println("Write " + queryOptionName + " to database"); - System.out.println("Write " + queryOptionName + " to database"); - - // read query option with QueryOptionsHandle - QueryOptionsHandle readHandle = new QueryOptionsHandle(); - optionsMgr.readOptions(queryOptionName, readHandle); - String output = readHandle.toString(); - - // write back query option with QueryOptionsHandle - String queryOptionNamePOJO = "valueConstraintWildCardPOJOOpt.xml"; - optionsMgr.writeOptions(queryOptionNamePOJO, readHandle); - - // read POJO query option - optionsMgr.readOptions(queryOptionNamePOJO, readHandle); - String outputPOJO = readHandle.toString(); - - boolean isQueryOptionsSame = output.equals(outputPOJO); - assertTrue("Query options is not the same", isQueryOptionsSame); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionNamePOJO); - querydef.setCriteria("id:00*2 OR id:0??6"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testRoundtrippingQueryOptionPOJOAll() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRoundtrippingQueryOptionPOJOAll"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "appservicesConstraintCombinationOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create a manager for writing query options - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/pojo-query-option-all/", "XML"); - } - - // create handle - ReaderHandle handle = new ReaderHandle(); - - // write the files - BufferedReader docStream = new BufferedReader(new FileReader("src/test/java/com/marklogic/javaclient/queryoptions/" + queryOptionName)); - handle.set(docStream); - - // write the query options to the database - optionsMgr.writeOptions(queryOptionName, handle); - - //System.out.println("Write " + queryOptionName + " to database"); - System.out.println("Write " + queryOptionName + " to database"); - - // read query option with QueryOptionsHandle - QueryOptionsHandle readHandle = new QueryOptionsHandle(); - optionsMgr.readOptions(queryOptionName, readHandle); - String output = readHandle.toString(); - System.out.println(output); - System.out.println("============================"); - - // write back query option with QueryOptionsHandle - String queryOptionNamePOJO = "appservicesConstraintCombinationPOJOOpt.xml"; - optionsMgr.writeOptions(queryOptionNamePOJO, readHandle); - - // read POJO query option - optionsMgr.readOptions(queryOptionNamePOJO, readHandle); - String outputPOJO = readHandle.toString(); - System.out.println(outputPOJO); - - boolean isQueryOptionsSame = output.equals(outputPOJO); - assertTrue("Query options is not the same", isQueryOptionsSame); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionNamePOJO); - //querydef.setCriteria("(coll:set1 AND coll:set5) AND -intitle:memex AND (pop:high OR pop:medium) AND price:low AND id:**11 AND date:2005-01-01 AND (para:Bush AND -para:memex)"); - querydef.setCriteria("pop:high"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("3", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - //assertXpathEvaluatesTo("Vannevar Bush", "string(//*[local-name()='result'][1]//*[local-name()='title'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testRoundtrippingQueryOptionPOJOAllJSON() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRoundtrippingQueryOptionPOJOAllJSON"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "appservicesConstraintCombinationOpt.json"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create a manager for writing query options - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/pojo-query-option-all-json/", "XML"); - } - - // create handle - FileHandle handle = new FileHandle(new File("src/test/java/com/marklogic/javaclient/queryoptions/" + queryOptionName)); - handle.setFormat(Format.JSON); - - // write the files - //BufferedReader docStream = new BufferedReader(new FileReader("src/test/java/com/marklogic/javaclient/queryoptions/" + queryOptionName)); - //handle.set(docStream); - - - // write the query options to the database - optionsMgr.writeOptions(queryOptionName, handle); - - System.out.println("Write " + queryOptionName + " to database"); - - // read query option with StringHandle - StringHandle readHandle = new StringHandle(); - readHandle.setFormat(Format.JSON); - optionsMgr.readOptions(queryOptionName, readHandle); - String output = readHandle.toString(); - System.out.println(output); - System.out.println("============================"); - - // write back query option with StringHandle - String queryOptionNamePOJO = "appservicesConstraintCombinationPOJOOpt.json"; - readHandle.setFormat(Format.JSON); - optionsMgr.writeOptions(queryOptionNamePOJO, readHandle); - - // read POJO query option - readHandle.setFormat(Format.JSON); - optionsMgr.readOptions(queryOptionNamePOJO, readHandle); - String outputPOJO = readHandle.toString(); - System.out.println(outputPOJO); - - boolean isQueryOptionsSame = output.equals(outputPOJO); - assertTrue("Query options is not the same", isQueryOptionsSame); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionNamePOJO); - //querydef.setCriteria("(coll:set1 AND coll:set5) AND -intitle:memex AND (pop:high OR pop:medium) AND price:low AND id:**11 AND date:2005-01-01 AND (para:Bush AND -para:memex)"); - querydef.setCriteria("pop:high"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("3", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - //assertXpathEvaluatesTo("Vannevar Bush", "string(//*[local-name()='result'][1]//*[local-name()='title'])", resultDoc); - - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testJSONConverter() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testJSONConverter"); - - //String queryOptionName = "jsonConverterOpt.json"; - String queryOptionName = "queryValidationOpt.json"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create a manager for writing query options - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create handle - FileHandle handle = new FileHandle(new File("src/test/java/com/marklogic/javaclient/queryoptions/" + queryOptionName)); - handle.setFormat(Format.JSON); - - // write the query options to the database - optionsMgr.writeOptions(queryOptionName, handle); - - System.out.println("Write " + queryOptionName + " to database"); - - // read query option with QueryOptionsHandle - QueryOptionsHandle readHandle = new QueryOptionsHandle(); - optionsMgr.readOptions(queryOptionName, readHandle); - String output = readHandle.toString(); - System.out.println(output); - System.out.println("============================"); - - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } -} +package com.marklogic.javaclient; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; +import static org.junit.Assert.*; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import com.marklogic.client.query.QueryManager; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.admin.QueryOptionsManager; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.query.StringQueryDefinition; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.FileHandle; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.QueryOptionsHandle; +import com.marklogic.client.io.ReaderHandle; +import com.marklogic.client.io.StringHandle; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.*; + +public class TestQueryOptionsHandle extends BasicJavaClientREST { + private static String dbName = "TestQueryOptionsHandleDB"; + private static String [] fNames = {"TestQueryOptionsHandleDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort=8011; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + } + + @After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + + @Test + public void testRoundtrippingQueryOptionPOJO() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRoundtrippingQueryOptionPOJO"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "valueConstraintWildCardOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create a manager for writing query options + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/pojo-query-option/", "XML"); + } + + // create handle + ReaderHandle handle = new ReaderHandle(); + + // write the files + BufferedReader docStream = new BufferedReader(new FileReader("src/test/java/com/marklogic/javaclient/queryoptions/" + queryOptionName)); + handle.set(docStream); + + // write the query options to the database + optionsMgr.writeOptions(queryOptionName, handle); + + //System.out.println("Write " + queryOptionName + " to database"); + System.out.println("Write " + queryOptionName + " to database"); + + // read query option with QueryOptionsHandle + QueryOptionsHandle readHandle = new QueryOptionsHandle(); + optionsMgr.readOptions(queryOptionName, readHandle); + String output = readHandle.toString(); + + // write back query option with QueryOptionsHandle + String queryOptionNamePOJO = "valueConstraintWildCardPOJOOpt.xml"; + optionsMgr.writeOptions(queryOptionNamePOJO, readHandle); + + // read POJO query option + optionsMgr.readOptions(queryOptionNamePOJO, readHandle); + String outputPOJO = readHandle.toString(); + + boolean isQueryOptionsSame = output.equals(outputPOJO); + assertTrue("Query options is not the same", isQueryOptionsSame); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionNamePOJO); + querydef.setCriteria("id:00*2 OR id:0??6"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); + assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testRoundtrippingQueryOptionPOJOAll() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRoundtrippingQueryOptionPOJOAll"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "appservicesConstraintCombinationOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create a manager for writing query options + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/pojo-query-option-all/", "XML"); + } + + // create handle + ReaderHandle handle = new ReaderHandle(); + + // write the files + BufferedReader docStream = new BufferedReader(new FileReader("src/test/java/com/marklogic/javaclient/queryoptions/" + queryOptionName)); + handle.set(docStream); + + // write the query options to the database + optionsMgr.writeOptions(queryOptionName, handle); + + //System.out.println("Write " + queryOptionName + " to database"); + System.out.println("Write " + queryOptionName + " to database"); + + // read query option with QueryOptionsHandle + QueryOptionsHandle readHandle = new QueryOptionsHandle(); + optionsMgr.readOptions(queryOptionName, readHandle); + String output = readHandle.toString(); + System.out.println(output); + System.out.println("============================"); + + // write back query option with QueryOptionsHandle + String queryOptionNamePOJO = "appservicesConstraintCombinationPOJOOpt.xml"; + optionsMgr.writeOptions(queryOptionNamePOJO, readHandle); + + // read POJO query option + optionsMgr.readOptions(queryOptionNamePOJO, readHandle); + String outputPOJO = readHandle.toString(); + System.out.println(outputPOJO); + + boolean isQueryOptionsSame = output.equals(outputPOJO); + assertTrue("Query options is not the same", isQueryOptionsSame); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionNamePOJO); + //querydef.setCriteria("(coll:set1 AND coll:set5) AND -intitle:memex AND (pop:high OR pop:medium) AND price:low AND id:**11 AND date:2005-01-01 AND (para:Bush AND -para:memex)"); + querydef.setCriteria("pop:high"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + //System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("3", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + //assertXpathEvaluatesTo("Vannevar Bush", "string(//*[local-name()='result'][1]//*[local-name()='title'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testRoundtrippingQueryOptionPOJOAllJSON() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRoundtrippingQueryOptionPOJOAllJSON"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "appservicesConstraintCombinationOpt.json"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create a manager for writing query options + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/pojo-query-option-all-json/", "XML"); + } + + // create handle + FileHandle handle = new FileHandle(new File("src/test/java/com/marklogic/javaclient/queryoptions/" + queryOptionName)); + handle.setFormat(Format.JSON); + + // write the query options to the database + optionsMgr.writeOptions(queryOptionName, handle); + + System.out.println("Write " + queryOptionName + " to database"); + + // read query option with StringHandle + StringHandle readHandle = new StringHandle(); + readHandle.setFormat(Format.JSON); + optionsMgr.readOptions(queryOptionName, readHandle); + String output = readHandle.toString(); + System.out.println(output); + System.out.println("============================"); + + // write back query option with StringHandle + String queryOptionNamePOJO = "appservicesConstraintCombinationPOJOOpt.json"; + readHandle.setFormat(Format.JSON); + optionsMgr.writeOptions(queryOptionNamePOJO, readHandle); + + // read POJO query option + readHandle.setFormat(Format.JSON); + optionsMgr.readOptions(queryOptionNamePOJO, readHandle); + String outputPOJO = readHandle.toString(); + System.out.println(outputPOJO); + + boolean isQueryOptionsSame = output.equals(outputPOJO); + assertTrue("Query options is not the same", isQueryOptionsSame); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionNamePOJO); + //querydef.setCriteria("(coll:set1 AND coll:set5) AND -intitle:memex AND (pop:high OR pop:medium) AND price:low AND id:**11 AND date:2005-01-01 AND (para:Bush AND -para:memex)"); + querydef.setCriteria("pop:high"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("3", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testJSONConverter() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testJSONConverter"); + + //String queryOptionName = "jsonConverterOpt.json"; + String queryOptionName = "queryValidationOpt.json"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create a manager for writing query options + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create handle + FileHandle handle = new FileHandle(new File("src/test/java/com/marklogic/javaclient/queryoptions/" + queryOptionName)); + handle.setFormat(Format.JSON); + + // write the query options to the database + optionsMgr.writeOptions(queryOptionName, handle); + + System.out.println("Write " + queryOptionName + " to database"); + + // read query option with QueryOptionsHandle + QueryOptionsHandle readHandle = new QueryOptionsHandle(); + optionsMgr.readOptions(queryOptionName, readHandle); + String output = readHandle.toString(); + System.out.println(output); + System.out.println("============================"); + + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionsListHandle.java b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionsListHandle.java index e1c712170..c0a01df16 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionsListHandle.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionsListHandle.java @@ -1,50 +1,52 @@ -package com.marklogic.javaclient; - -import java.io.IOException; -import java.util.HashMap; - -import javax.xml.parsers.ParserConfigurationException; - -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.QueryOptionsListHandle; -import com.marklogic.javaclient.BasicJavaClientREST; -import org.junit.*; -public class TestQueryOptionsListHandle extends BasicJavaClientREST { - - private static String dbName = "QueryOptionsListHandleDB"; - private static String [] fNames = {"QueryOptionsListHandleDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - } - - -@SuppressWarnings("deprecation") -@Test public void testNPE() throws IOException, SAXException, ParserConfigurationException - { - System.out.println("Running testNPE"); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - QueryOptionsListHandle handle = new QueryOptionsListHandle(); - - HashMap map = handle.getValuesMap(); - - // release client - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } -} +package com.marklogic.javaclient; + +import java.io.IOException; +import java.util.HashMap; + +import javax.xml.parsers.ParserConfigurationException; + +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.QueryOptionsListHandle; +import com.marklogic.javaclient.BasicJavaClientREST; +import org.junit.*; + +public class TestQueryOptionsListHandle extends BasicJavaClientREST { + + private static String dbName = "QueryOptionsListHandleDB"; + private static String [] fNames = {"QueryOptionsListHandleDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + } + + @Test + public void testNPE() throws IOException, SAXException, ParserConfigurationException + { + System.out.println("Running testNPE"); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + QueryOptionsListHandle handle = new QueryOptionsListHandle(); + + HashMap map = handle.getValuesMap(); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestRangeConstraint.java b/test-complete/src/test/java/com/marklogic/javaclient/TestRangeConstraint.java index 08c6388fa..02e165a4f 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestRangeConstraint.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestRangeConstraint.java @@ -1,64 +1,65 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.IOException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.SearchHandle; -import com.marklogic.javaclient.BasicJavaClientREST; -import org.junit.*; -public class TestRangeConstraint extends BasicJavaClientREST { - static String filenames[] = {"bbq1.xml", "bbq2.xml", "bbq3.xml", "bbq4.xml", "bbq5.xml"}; - static String queryOptionName = "rangeConstraintOpt.xml"; - private static String dbName = "RangeConstraintDB"; - private static String [] fNames = {"RangeConstraintDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - addRangeElementIndex(dbName, "decimal", "http://example.com", "rating"); - } - - -@SuppressWarnings("deprecation") -@Test public void testElementRangeConstraint() throws IOException - { - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename:filenames) - { - writeDocumentReaderHandle(client, filename, "/range-constraint/", "XML"); - } - - - // write the query options to the database - setQueryOption(client, queryOptionName); - - // run the search - SearchHandle resultsHandle = runSearch(client, queryOptionName, "rating:5"); - - // search result - String searchResult = returnSearchResult(resultsHandle); - - String expectedSearchResult = "|Matched 1 locations in /range-constraint/bbq4.xml|Matched 1 locations in /range-constraint/bbq3.xml"; - System.out.println(searchResult); - - assertEquals("Search result difference", expectedSearchResult, searchResult); - - // release client - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.IOException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.SearchHandle; +import com.marklogic.javaclient.BasicJavaClientREST; +import org.junit.*; + +public class TestRangeConstraint extends BasicJavaClientREST { + static String filenames[] = {"bbq1.xml", "bbq2.xml", "bbq3.xml", "bbq4.xml", "bbq5.xml"}; + static String queryOptionName = "rangeConstraintOpt.xml"; + private static String dbName = "RangeConstraintDB"; + private static String [] fNames = {"RangeConstraintDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + addRangeElementIndex(dbName, "decimal", "http://example.com", "rating"); + } + + @Test + public void testElementRangeConstraint() throws IOException + { + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename:filenames) + { + writeDocumentReaderHandle(client, filename, "/range-constraint/", "XML"); + } + + // write the query options to the database + setQueryOption(client, queryOptionName); + + // run the search + SearchHandle resultsHandle = runSearch(client, queryOptionName, "rating:5"); + + // search result + String searchResult = returnSearchResult(resultsHandle); + + String expectedSearchResult = "|Matched 1 locations in /range-constraint/bbq4.xml|Matched 1 locations in /range-constraint/bbq3.xml"; + System.out.println(searchResult); + + assertEquals("Search result difference", expectedSearchResult, searchResult); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestRangeConstraintAbsoluteBucket.java b/test-complete/src/test/java/com/marklogic/javaclient/TestRangeConstraintAbsoluteBucket.java index 91ebb2935..f1fbaaa7f 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestRangeConstraintAbsoluteBucket.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestRangeConstraintAbsoluteBucket.java @@ -1,61 +1,62 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.IOException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.SearchHandle; -import com.marklogic.javaclient.BasicJavaClientREST; -import org.junit.*; -public class TestRangeConstraintAbsoluteBucket extends BasicJavaClientREST{ - static String filenames[] = {"bbq1.xml", "bbq2.xml", "bbq3.xml", "bbq4.xml", "bbq5.xml"}; - static String queryOptionName = "rangeAbsoluteBucketConstraintOpt.xml"; - private static String dbName = "RangeConstraintAbsBucketDB"; - private static String [] fNames = {"RangeConstraintAbsBucketDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - addRangeElementIndex(dbName, "int", "http://example.com", "scoville"); - } - - -@SuppressWarnings("deprecation") -@Test public void testRangeConstraintAbsoluteBucket() throws IOException - { - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename:filenames) - { - writeDocumentReaderHandle(client, filename, "/range-constraint-abs-bucket/", "XML"); - } - - // write the query options to the database - setQueryOption(client, queryOptionName); - - // run the search - SearchHandle resultsHandle = runSearch(client, queryOptionName, "heat:moderate"); - - // search result - String searchResult = returnSearchResult(resultsHandle); - - String expectedSearchResult = "|Matched 1 locations in /range-constraint-abs-bucket/bbq1.xml|Matched 1 locations in /range-constraint-abs-bucket/bbq3.xml|Matched 1 locations in /range-constraint-abs-bucket/bbq5.xml"; - assertEquals("Search result difference", expectedSearchResult, searchResult); - - // release client - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.IOException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.SearchHandle; +import com.marklogic.javaclient.BasicJavaClientREST; +import org.junit.*; +public class TestRangeConstraintAbsoluteBucket extends BasicJavaClientREST{ + static String filenames[] = {"bbq1.xml", "bbq2.xml", "bbq3.xml", "bbq4.xml", "bbq5.xml"}; + static String queryOptionName = "rangeAbsoluteBucketConstraintOpt.xml"; + private static String dbName = "RangeConstraintAbsBucketDB"; + private static String [] fNames = {"RangeConstraintAbsBucketDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + addRangeElementIndex(dbName, "int", "http://example.com", "scoville"); + } + + @Test + public void testRangeConstraintAbsoluteBucket() throws IOException + { + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename:filenames) + { + writeDocumentReaderHandle(client, filename, "/range-constraint-abs-bucket/", "XML"); + } + + // write the query options to the database + setQueryOption(client, queryOptionName); + + // run the search + SearchHandle resultsHandle = runSearch(client, queryOptionName, "heat:moderate"); + + // search result + String searchResult = returnSearchResult(resultsHandle); + + String expectedSearchResult = "|Matched 1 locations in /range-constraint-abs-bucket/bbq1.xml|Matched 1 locations in /range-constraint-abs-bucket/bbq3.xml|Matched 1 locations in /range-constraint-abs-bucket/bbq5.xml"; + assertEquals("Search result difference", expectedSearchResult, searchResult); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestRangeConstraintRelativeBucket.java b/test-complete/src/test/java/com/marklogic/javaclient/TestRangeConstraintRelativeBucket.java index 97ef122b1..56ec73dec 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestRangeConstraintRelativeBucket.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestRangeConstraintRelativeBucket.java @@ -1,59 +1,61 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.IOException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.SearchHandle; -import com.marklogic.javaclient.BasicJavaClientREST; -import org.junit.*; -public class TestRangeConstraintRelativeBucket extends BasicJavaClientREST { - static String filenames[] = {"bbq1.xml", "bbq2.xml", "bbq3.xml", "bbq4.xml", "bbq5.xml"}; - static String queryOptionName = "rangeRelativeBucketConstraintOpt.xml"; - private static String dbName = "RangeConstraintRelBucketDB"; - private static String [] fNames = {"RangeConstraintRelBucketDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - addRangeElementAttributeIndex(dbName, "dateTime", "http://example.com", "entry", "", "date"); - } - - -@SuppressWarnings("deprecation") -@Test public void testRangeConstraintRelativeBucket() throws IOException - { - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename:filenames) - { - writeDocumentReaderHandle(client, filename, "/range-constraint-rel-bucket/", "XML"); - } - - // write the query options to the database - setQueryOption(client, queryOptionName); - - // run the search - SearchHandle resultsHandle = runSearch(client, queryOptionName, "date:older"); - - // search result - String result = "Matched "+resultsHandle.getTotalResults(); - String expectedResult = "Matched 5"; - assertEquals("Document match difference", expectedResult, result); - - // release client - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.IOException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.SearchHandle; +import com.marklogic.javaclient.BasicJavaClientREST; +import org.junit.*; + +public class TestRangeConstraintRelativeBucket extends BasicJavaClientREST { + static String filenames[] = {"bbq1.xml", "bbq2.xml", "bbq3.xml", "bbq4.xml", "bbq5.xml"}; + static String queryOptionName = "rangeRelativeBucketConstraintOpt.xml"; + private static String dbName = "RangeConstraintRelBucketDB"; + private static String [] fNames = {"RangeConstraintRelBucketDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + addRangeElementAttributeIndex(dbName, "dateTime", "http://example.com", "entry", "", "date"); + } + + @Test + public void testRangeConstraintRelativeBucket() throws IOException + { + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename:filenames) + { + writeDocumentReaderHandle(client, filename, "/range-constraint-rel-bucket/", "XML"); + } + + // write the query options to the database + setQueryOption(client, queryOptionName); + + // run the search + SearchHandle resultsHandle = runSearch(client, queryOptionName, "date:older"); + + // search result + String result = "Matched "+resultsHandle.getTotalResults(); + String expectedResult = "Matched 5"; + assertEquals("Document match difference", expectedResult, result); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestRawAlert.java b/test-complete/src/test/java/com/marklogic/javaclient/TestRawAlert.java index 879c9968e..bc75dbe0c 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestRawAlert.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestRawAlert.java @@ -1,807 +1,798 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.Iterator; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.query.StringQueryDefinition; -import com.marklogic.client.query.StructuredQueryBuilder; -import com.marklogic.client.query.StructuredQueryDefinition; - -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.admin.TransformExtensionsManager; -import com.marklogic.client.alerting.RuleDefinition; -import com.marklogic.client.alerting.RuleDefinitionList; -import com.marklogic.client.alerting.RuleManager; -import com.marklogic.client.document.ServerTransform; -import com.marklogic.client.io.FileHandle; -import com.marklogic.client.io.Format; -import com.marklogic.client.io.InputStreamHandle; -import com.marklogic.client.io.StringHandle; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.junit.*; -public class TestRawAlert extends BasicJavaClientREST { - - private static String dbName = "TestRawAlertDB"; - private static String [] fNames = {"TestRawAlertDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort = 8011; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - } - -@After -public void testCleanUp() throws Exception -{ - clearDB(restPort); - System.out.println("Running clear script"); -} - -@SuppressWarnings("deprecation") -@Test public void testRawAlert() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawAlert"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/raw-alert/", "XML"); - } - - // create a manager for configuring rules - RuleManager ruleMgr = client.newRuleManager(); - - // get the rule - File file = new File("src/test/java/com/marklogic/javaclient/rules/alertRule1.xml"); - - // create a handle for the rule - FileHandle writeHandle = new FileHandle(file); - - // write the rule to the database - ruleMgr.writeRule("RULE-TEST-1", writeHandle); - - // create a manager for document search criteria - QueryManager queryMgr = client.newQueryManager(); - - // specify the search criteria for the documents - String criteria = "atlantic"; - StringQueryDefinition querydef = queryMgr.newStringDefinition(); - querydef.setCriteria(criteria); - - // create a manager for matching rules - RuleManager ruleMatchMgr = client.newRuleManager(); - - // match the rules against the documents qualified by the criteria - RuleDefinitionList matchedRules = ruleMatchMgr.match(querydef, new RuleDefinitionList()); - - System.out.println(matchedRules.size()); - - String expected = ""; - - // iterate over the matched rules - Iterator ruleItr = matchedRules.iterator(); - while (ruleItr.hasNext()) - { - RuleDefinition rule = ruleItr.next(); - System.out.println( - "document criteria "+criteria+" matched rule "+ - rule.getName()+" with metadata "+rule.getMetadata() - ); - - expected = expected + rule.getName() + " - " + rule.getMetadata() + " | "; - } - - System.out.println(expected); - - assertTrue("incorrect rule", expected.contains("RULE-TEST-1 - {rule-number=one} |")); - - // release client - client.release(); - } - - - -@SuppressWarnings("deprecation") -@Test public void testRawAlertUnmatched() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawAlertUnmatched"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/raw-alert/", "XML"); - } - - // create a manager for configuring rules - RuleManager ruleMgr = client.newRuleManager(); - - // get the rule - File file = new File("src/test/java/com/marklogic/javaclient/rules/alertRule1.xml"); - - // create a handle for the rule - FileHandle writeHandle = new FileHandle(file); - - // write the rule to the database - ruleMgr.writeRule("RULE-TEST-1", writeHandle); - - // create a manager for document search criteria - QueryManager queryMgr = client.newQueryManager(); - - // specify the search criteria for the documents - String criteria = "Memex"; // test case for unmatched rule - StringQueryDefinition querydef = queryMgr.newStringDefinition(); - querydef.setCriteria(criteria); - - // create a manager for matching rules - RuleManager ruleMatchMgr = client.newRuleManager(); - - // match the rules against the documents qualified by the criteria - RuleDefinitionList matchedRules = ruleMatchMgr.match(querydef, new RuleDefinitionList()); - - System.out.println(matchedRules.size()); - - assertEquals("incorrect matching rule", 0, matchedRules.size()); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testRawAlertMultipleRules() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawAlertMultipleRules"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/raw-alert/", "XML"); - } - - // create a manager for configuring rules - RuleManager ruleMgr = client.newRuleManager(); - - // create handle - InputStreamHandle ruleHandle1 = new InputStreamHandle(); - InputStreamHandle ruleHandle2 = new InputStreamHandle(); - - // get the rule file - InputStream inputStream1 = new FileInputStream("src/test/java/com/marklogic/javaclient/rules/alertRule1.xml"); - InputStream inputStream2 = new FileInputStream("src/test/java/com/marklogic/javaclient/rules/alertRule2.xml"); - - ruleHandle1.set(inputStream1); - ruleHandle2.set(inputStream2); - - // write the rule to the database - ruleMgr.writeRule("RULE-TEST-1", ruleHandle1); - ruleMgr.writeRule("RULE-TEST-2", ruleHandle2); - - // create a manager for document search criteria - QueryManager queryMgr = client.newQueryManager(); - - // specify the search criteria for the documents - String criteria = "atlantic"; - StringQueryDefinition querydef = queryMgr.newStringDefinition(); - querydef.setCriteria(criteria); - - // create a manager for matching rules - RuleManager ruleMatchMgr = client.newRuleManager(); - - // match the rules against the documents qualified by the criteria - RuleDefinitionList matchedRules = ruleMatchMgr.match(querydef, new RuleDefinitionList()); - - System.out.println(matchedRules.size()); - - String expected = ""; - - // iterate over the matched rules - Iterator ruleItr = matchedRules.iterator(); - while (ruleItr.hasNext()) - { - RuleDefinition rule = ruleItr.next(); - System.out.println( - "document criteria "+criteria+" matched rule "+ - rule.getName()+" with metadata "+rule.getMetadata() - ); - expected = expected + rule.getName() + " - " + rule.getMetadata() + " | "; - } - - System.out.println(expected); - - assertTrue("incorrect rules", expected.contains("RULE-TEST-1 - {rule-number=one}")&& expected.contains("RULE-TEST-2 - {rule-number=two}")); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testRawAlertUnmatchingRuleName() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawAlertUnmatchingRuleName"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/raw-alert/", "XML"); - } - - // create a manager for configuring rules - RuleManager ruleMgr = client.newRuleManager(); - - // create handle - InputStreamHandle ruleHandle1 = new InputStreamHandle(); - - // get the rule file - InputStream inputStream1 = new FileInputStream("src/test/java/com/marklogic/javaclient/rules/alertRule1.xml"); - - ruleHandle1.set(inputStream1); - - String exception = ""; - - // write the rule to the database - try - { - ruleMgr.writeRule("RULE-TEST-A", ruleHandle1); // test case for non-matching rule name - } catch(Exception e) - { - exception = e.toString(); - } - - String expectedException = "Invalid content: If provided, rule name in payload must match rule name in URL"; - - assertTrue("Exception is not thrown", exception.contains(expectedException)); - - // release client - client.release(); - } - - - -@SuppressWarnings("deprecation") -@Test public void testRawAlertJSON() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawAlertJSON"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/raw-alert/", "XML"); - } - - // create a manager for configuring rules - RuleManager ruleMgr = client.newRuleManager(); - - // get the rule - File file = new File("src/test/java/com/marklogic/javaclient/rules/alertRule1.json"); - - String ruleInJson = convertFileToString(file); - - // create a handle for the rule - StringHandle ruleHandle = new StringHandle(ruleInJson); - ruleHandle.setFormat(Format.JSON); - //FileHandle writeHandle = new FileHandle(file); - - // write the rule to the database - ruleMgr.writeRule("RULE-TEST-1-JSON", ruleHandle); - - // create a manager for document search criteria - QueryManager queryMgr = client.newQueryManager(); - - // specify the search criteria for the documents - String criteria = "atlantic"; - StringQueryDefinition querydef = queryMgr.newStringDefinition(); // test case with string def - querydef.setCriteria(criteria); - - // create query def - //StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); // tes case with structured query - //StructuredQueryDefinition termQuery1 = qb.term("Atlantic"); - - // create a manager for matching rules - RuleManager ruleMatchMgr = client.newRuleManager(); - - // match the rules against the documents qualified by the criteria - RuleDefinitionList matchedRules = ruleMatchMgr.match(querydef, new RuleDefinitionList()); - - System.out.println(matchedRules.size()); - - String expected= ""; - - // iterate over the matched rules - Iterator ruleItr = matchedRules.iterator(); - while (ruleItr.hasNext()) - { - RuleDefinition rule = ruleItr.next(); - System.out.println( - "document criteria matched rule "+ - rule.getName()+" with metadata "+rule.getMetadata() - ); - expected = expected + rule.getName() + " - " + rule.getMetadata() + " | "; - } - - System.out.println(expected); - - assertTrue("rule is not correct", expected.contains("RULE-TEST-1-JSON - {{http://marklogic.com/rest-api}rule-number=one json}")); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testRawAlertStructuredQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawAlertStructuredQuery"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/raw-alert/", "XML"); - } - - // create a manager for configuring rules - RuleManager ruleMgr = client.newRuleManager(); - - // get the rule - File file = new File("src/test/java/com/marklogic/javaclient/rules/alertRule1.xml"); - - // create a handle for the rule - FileHandle writeHandle = new FileHandle(file); - - // write the rule to the database - ruleMgr.writeRule("RULE-TEST-1", writeHandle); - - // create a manager for document search criteria - QueryManager queryMgr = client.newQueryManager(); - - // specify the search criteria for the documents - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); - StructuredQueryDefinition termQuery1 = qb.term("Atlantic"); - - // create a manager for matching rules - RuleManager ruleMatchMgr = client.newRuleManager(); - - // match the rules against the documents qualified by the criteria - RuleDefinitionList matchedRules = ruleMatchMgr.match(termQuery1, new RuleDefinitionList()); - - System.out.println(matchedRules.size()); // bug, should return 1 - - assertEquals("result count is not correct", 1, matchedRules.size()); - - // iterate over the matched rules - Iterator ruleItr = matchedRules.iterator(); - while (ruleItr.hasNext()) - { - RuleDefinition rule = ruleItr.next(); - System.out.println( - "document criteria matched rule "+ - rule.getName()+" with metadata "+rule.getMetadata() - ); - } - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testRawAlertStructuredQueryTransform() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawAlertStructuredQueryTransform"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/raw-alert/", "XML"); - } - - // create a manager for configuring rules - RuleManager ruleMgr = client.newRuleManager(); - - // get the rule - File file = new File("src/test/java/com/marklogic/javaclient/rules/alertRule1.xml"); - - // create a handle for the rule - FileHandle writeHandle = new FileHandle(file); - - // write the rule to the database - ruleMgr.writeRule("RULE-TEST-1", writeHandle); - - // create a manager for document search criteria - QueryManager queryMgr = client.newQueryManager(); - - // specify the search criteria for the documents - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); - StructuredQueryDefinition termQuery1 = qb.term("Atlantic"); - - // Write the rule in Modules database of Server - TransformExtensionsManager transformManager= client.newServerConfigManager().newTransformExtensionsManager(); - - File ruleTransform = new File("src/test/java/com/marklogic/javaclient/rules/rule-transform.xqy"); - transformManager.writeXQueryTransform("ruleTransform", new FileHandle(ruleTransform)); - - // create a manager for matching rules - RuleManager ruleMatchMgr = client.newRuleManager(); - - ServerTransform transform = new ServerTransform("ruleTransform"); - RuleDefinitionList matchedRules = ruleMatchMgr.match(termQuery1, 0L, QueryManager.DEFAULT_PAGE_LENGTH, new String[] {}, new RuleDefinitionList(), transform); - - System.out.println(matchedRules.size()); // bug, should return 1 - - assertEquals("result count is not correct", 1, matchedRules.size()); - - // iterate over the matched rules - Iterator ruleItr = matchedRules.iterator(); - while (ruleItr.hasNext()) - { - RuleDefinition rule = ruleItr.next(); - System.out.println( - "document criteria matched rule "+ - rule.getName()+" with metadata "+rule.getMetadata() - ); - } - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testRawAlertCandidateRules() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawAlertCandidateRules"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/raw-alert/", "XML"); - } - - // create a manager for configuring rules - RuleManager ruleMgr = client.newRuleManager(); - - // create handle - InputStreamHandle ruleHandle1 = new InputStreamHandle(); - InputStreamHandle ruleHandle2 = new InputStreamHandle(); - InputStreamHandle ruleHandle3 = new InputStreamHandle(); - - // get the rule file - InputStream inputStream1 = new FileInputStream("src/test/java/com/marklogic/javaclient/rules/alertRule1.xml"); - InputStream inputStream2 = new FileInputStream("src/test/java/com/marklogic/javaclient/rules/alertRule2.xml"); - InputStream inputStream3 = new FileInputStream("src/test/java/com/marklogic/javaclient/rules/alertRule3.xml"); - - ruleHandle1.set(inputStream1); - ruleHandle2.set(inputStream2); - ruleHandle3.set(inputStream3); - - // write the rule to the database - ruleMgr.writeRule("RULE-TEST-1", ruleHandle1); - ruleMgr.writeRule("RULE-TEST-2", ruleHandle2); - ruleMgr.writeRule("RULE-TEST-3", ruleHandle3); - - // get the json rule - File file = new File("src/test/java/com/marklogic/javaclient/rules/alertRule3.json"); - - String ruleInJson = convertFileToString(file); - - // create a handle for the rule - StringHandle ruleHandle4 = new StringHandle(ruleInJson); - ruleHandle4.setFormat(Format.JSON); - - // write the rule to the database - ruleMgr.writeRule("RULE-TEST-3-JSON", ruleHandle4); - - // create a manager for document search criteria - QueryManager queryMgr = client.newQueryManager(); - - // specify the search criteria for the documents - String criteria = "memex"; - StringQueryDefinition querydef = queryMgr.newStringDefinition(); - querydef.setCriteria(criteria); - - // create a manager for matching rules - RuleManager ruleMatchMgr = client.newRuleManager(); - - String[] candidateRules = {"RULE-TEST-1", "RULE-TEST-2", "RULE-TEST-3", "RULE-TEST-3-JSON"}; - - // match the rules against the documents qualified by the criteria - RuleDefinitionList matchedRules = ruleMatchMgr.match(querydef, 1,2, candidateRules, new RuleDefinitionList()); - - System.out.println(matchedRules.size()); - - String expected = ""; - - // iterate over the matched rules - Iterator ruleItr = matchedRules.iterator(); - while (ruleItr.hasNext()) - { - RuleDefinition rule = ruleItr.next(); - System.out.println( - "document criteria "+criteria+" matched rule "+ - rule.getName()+" with metadata "+rule.getMetadata() - ); - expected = expected + rule.getName() + " - " + rule.getMetadata() + " | "; - } - - System.out.println(expected); - - if(expected.equals("RULE-TEST-3 - {rule-number=three} | RULE-TEST-3-JSON - {{http://marklogic.com/rest-api}rule-number=three json} | ")) - { - assertTrue("rule is incorrect", expected.contains("RULE-TEST-3 - {rule-number=three} | RULE-TEST-3-JSON - {{http://marklogic.com/rest-api}rule-number=three json}")); - } - else if(expected.equals("RULE-TEST-3-JSON - {{http://marklogic.com/rest-api}rule-number=three json} | RULE-TEST-3 - {rule-number=three} | ")) - { - assertTrue("rule is incorrect", expected.contains("RULE-TEST-3-JSON - {{http://marklogic.com/rest-api}rule-number=three json} | RULE-TEST-3 - {rule-number=three}")); - } - else - { - assertTrue("there is no matching rule", false); - } - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testRawAlertCandidateRulesUnmatched() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawAlertCandidateRulesUnmatched"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/raw-alert/", "XML"); - } - - // create a manager for configuring rules - RuleManager ruleMgr = client.newRuleManager(); - - // create handle - InputStreamHandle ruleHandle1 = new InputStreamHandle(); - InputStreamHandle ruleHandle2 = new InputStreamHandle(); - InputStreamHandle ruleHandle3 = new InputStreamHandle(); - - // get the rule file - InputStream inputStream1 = new FileInputStream("src/test/java/com/marklogic/javaclient/rules/alertRule1.xml"); - InputStream inputStream2 = new FileInputStream("src/test/java/com/marklogic/javaclient/rules/alertRule2.xml"); - InputStream inputStream3 = new FileInputStream("src/test/java/com/marklogic/javaclient/rules/alertRule3.xml"); - - ruleHandle1.set(inputStream1); - ruleHandle2.set(inputStream2); - ruleHandle3.set(inputStream3); - - // write the rule to the database - ruleMgr.writeRule("RULE-TEST-1", ruleHandle1); - ruleMgr.writeRule("RULE-TEST-2", ruleHandle2); - ruleMgr.writeRule("RULE-TEST-3", ruleHandle3); - - // get the json rule - File file = new File("src/test/java/com/marklogic/javaclient/rules/alertRule3.json"); - - String ruleInJson = convertFileToString(file); - - // create a handle for the rule - StringHandle ruleHandle4 = new StringHandle(ruleInJson); - ruleHandle4.setFormat(Format.JSON); - - // write the rule to the database - ruleMgr.writeRule("RULE-TEST-3-JSON", ruleHandle4); - - // create a manager for document search criteria - QueryManager queryMgr = client.newQueryManager(); - - // specify the search criteria for the documents - String criteria = "atlantic"; - StringQueryDefinition querydef = queryMgr.newStringDefinition(); - querydef.setCriteria(criteria); - - // create a manager for matching rules - RuleManager ruleMatchMgr = client.newRuleManager(); - - String[] candidateRules = {"gar", "bar", "foo"}; - - // match the rules against the documents qualified by the criteria - - RuleDefinitionList matchedRules = ruleMatchMgr.match(querydef, 1, 2, candidateRules, new RuleDefinitionList()); - - System.out.println(matchedRules.size()); - - assertEquals("match rule is incorrect", 0, matchedRules.size()); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testRawAlertDocUris() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawAlertDocUris"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/raw-alert/", "XML"); - } - - // create a manager for configuring rules - RuleManager ruleMgr = client.newRuleManager(); - - // get the rule - File file = new File("src/test/java/com/marklogic/javaclient/rules/alertRule1.xml"); - - //String combinedQuery = convertFileToString(file); - - // create a handle for the rule - //StringHandle rawHandle = new StringHandle(combinedQuery); - FileHandle writeHandle = new FileHandle(file); - - // write the rule to the database - ruleMgr.writeRule("RULE-TEST-1", writeHandle); - - // specify the search criteria for the documents - String[] docUris = {"/raw-alert/constraint1.xml", - "/raw-alert/constraint2.xml", - "/raw-alert/constraint3.xml", - "/raw-alert/constraint4.xml", - "/raw-alert/constraint5.xml"}; - - // create a manager for matching rules - RuleManager ruleMatchMgr = client.newRuleManager(); - - // match the rules against the documents qualified by the criteria - RuleDefinitionList matchedRules = ruleMatchMgr.match(docUris, new RuleDefinitionList()); - - System.out.println(matchedRules.size()); - - String expected = ""; - - // iterate over the matched rules - Iterator ruleItr = matchedRules.iterator(); - while (ruleItr.hasNext()) - { - RuleDefinition rule = ruleItr.next(); - System.out.println( - "document criteria matched rule "+ - rule.getName()+" with metadata "+rule.getMetadata() - ); - expected = expected + rule.getName() + " - " + rule.getMetadata() + " | "; - } - - System.out.println(expected); - - assertTrue("rule is incorrect", expected.contains("RULE-TEST-1 - {rule-number=one}")); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testRawAlertDocPayload() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawAlertDocPayload"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/raw-alert/", "XML"); - } - - // create a manager for configuring rules - RuleManager ruleMgr = client.newRuleManager(); - - // get the rule - File file = new File("src/test/java/com/marklogic/javaclient/rules/alertRule2.xml"); - - // create a handle for the rule - FileHandle writeHandle = new FileHandle(file); - - // write the rule to the database - ruleMgr.writeRule("RULE-TEST-2", writeHandle); - - String filename = "constraint1.xml"; - - // get the file - File doc = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - String docContent = convertFileToString(doc); - StringHandle handle = new StringHandle(docContent); - - // create a manager for matching rules - RuleManager ruleMatchMgr = client.newRuleManager(); - - // match the rules against the documents qualified by the criteria - RuleDefinitionList matchedRules = ruleMatchMgr.match(handle, new RuleDefinitionList()); - - System.out.println(matchedRules.size()); - - String expected = ""; - - // iterate over the matched rules - Iterator ruleItr = matchedRules.iterator(); - while (ruleItr.hasNext()) - { - RuleDefinition rule = ruleItr.next(); - System.out.println( - "document criteria matched rule "+ - rule.getName()+" with metadata "+rule.getMetadata() - ); - expected = expected + rule.getName() + " - " + rule.getMetadata() + " | "; - } - - System.out.println(expected); - - assertTrue("rule is incorrect", expected.contains("RULE-TEST-2 - {rule-number=two}")); - - // release client - client.release(); - } - - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Iterator; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.StringQueryDefinition; +import com.marklogic.client.query.StructuredQueryBuilder; +import com.marklogic.client.query.StructuredQueryDefinition; + +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.admin.TransformExtensionsManager; +import com.marklogic.client.alerting.RuleDefinition; +import com.marklogic.client.alerting.RuleDefinitionList; +import com.marklogic.client.alerting.RuleManager; +import com.marklogic.client.document.ServerTransform; +import com.marklogic.client.io.FileHandle; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.InputStreamHandle; +import com.marklogic.client.io.StringHandle; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.*; + +public class TestRawAlert extends BasicJavaClientREST { + private static String dbName = "TestRawAlertDB"; + private static String [] fNames = {"TestRawAlertDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + } + + @After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + + @Test + public void testRawAlert() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawAlert"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/raw-alert/", "XML"); + } + + // create a manager for configuring rules + RuleManager ruleMgr = client.newRuleManager(); + + // get the rule + File file = new File("src/test/java/com/marklogic/javaclient/rules/alertRule1.xml"); + + // create a handle for the rule + FileHandle writeHandle = new FileHandle(file); + + // write the rule to the database + ruleMgr.writeRule("RULE-TEST-1", writeHandle); + + // create a manager for document search criteria + QueryManager queryMgr = client.newQueryManager(); + + // specify the search criteria for the documents + String criteria = "atlantic"; + StringQueryDefinition querydef = queryMgr.newStringDefinition(); + querydef.setCriteria(criteria); + + // create a manager for matching rules + RuleManager ruleMatchMgr = client.newRuleManager(); + + // match the rules against the documents qualified by the criteria + RuleDefinitionList matchedRules = ruleMatchMgr.match(querydef, new RuleDefinitionList()); + + System.out.println(matchedRules.size()); + + String expected = ""; + + // iterate over the matched rules + Iterator ruleItr = matchedRules.iterator(); + while (ruleItr.hasNext()) + { + RuleDefinition rule = ruleItr.next(); + System.out.println( + "document criteria "+criteria+" matched rule "+ + rule.getName()+" with metadata "+rule.getMetadata() + ); + + expected = expected + rule.getName() + " - " + rule.getMetadata() + " | "; + } + + System.out.println(expected); + + assertTrue("incorrect rule", expected.contains("RULE-TEST-1 - {rule-number=one} |")); + + // release client + client.release(); + } + + @Test + public void testRawAlertUnmatched() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawAlertUnmatched"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/raw-alert/", "XML"); + } + + // create a manager for configuring rules + RuleManager ruleMgr = client.newRuleManager(); + + // get the rule + File file = new File("src/test/java/com/marklogic/javaclient/rules/alertRule1.xml"); + + // create a handle for the rule + FileHandle writeHandle = new FileHandle(file); + + // write the rule to the database + ruleMgr.writeRule("RULE-TEST-1", writeHandle); + + // create a manager for document search criteria + QueryManager queryMgr = client.newQueryManager(); + + // specify the search criteria for the documents + String criteria = "Memex"; // test case for unmatched rule + StringQueryDefinition querydef = queryMgr.newStringDefinition(); + querydef.setCriteria(criteria); + + // create a manager for matching rules + RuleManager ruleMatchMgr = client.newRuleManager(); + + // match the rules against the documents qualified by the criteria + RuleDefinitionList matchedRules = ruleMatchMgr.match(querydef, new RuleDefinitionList()); + + System.out.println(matchedRules.size()); + + assertEquals("incorrect matching rule", 0, matchedRules.size()); + + // release client + client.release(); + } + + @Test + public void testRawAlertMultipleRules() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawAlertMultipleRules"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/raw-alert/", "XML"); + } + + // create a manager for configuring rules + RuleManager ruleMgr = client.newRuleManager(); + + // create handle + InputStreamHandle ruleHandle1 = new InputStreamHandle(); + InputStreamHandle ruleHandle2 = new InputStreamHandle(); + + // get the rule file + InputStream inputStream1 = new FileInputStream("src/test/java/com/marklogic/javaclient/rules/alertRule1.xml"); + InputStream inputStream2 = new FileInputStream("src/test/java/com/marklogic/javaclient/rules/alertRule2.xml"); + + ruleHandle1.set(inputStream1); + ruleHandle2.set(inputStream2); + + // write the rule to the database + ruleMgr.writeRule("RULE-TEST-1", ruleHandle1); + ruleMgr.writeRule("RULE-TEST-2", ruleHandle2); + + // create a manager for document search criteria + QueryManager queryMgr = client.newQueryManager(); + + // specify the search criteria for the documents + String criteria = "atlantic"; + StringQueryDefinition querydef = queryMgr.newStringDefinition(); + querydef.setCriteria(criteria); + + // create a manager for matching rules + RuleManager ruleMatchMgr = client.newRuleManager(); + + // match the rules against the documents qualified by the criteria + RuleDefinitionList matchedRules = ruleMatchMgr.match(querydef, new RuleDefinitionList()); + + System.out.println(matchedRules.size()); + + String expected = ""; + + // iterate over the matched rules + Iterator ruleItr = matchedRules.iterator(); + while (ruleItr.hasNext()) + { + RuleDefinition rule = ruleItr.next(); + System.out.println( + "document criteria "+criteria+" matched rule "+ + rule.getName()+" with metadata "+rule.getMetadata() + ); + expected = expected + rule.getName() + " - " + rule.getMetadata() + " | "; + } + + System.out.println(expected); + + assertTrue("incorrect rules", expected.contains("RULE-TEST-1 - {rule-number=one}")&& expected.contains("RULE-TEST-2 - {rule-number=two}")); + + // release client + client.release(); + } + + @Test + public void testRawAlertUnmatchingRuleName() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawAlertUnmatchingRuleName"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/raw-alert/", "XML"); + } + + // create a manager for configuring rules + RuleManager ruleMgr = client.newRuleManager(); + + // create handle + InputStreamHandle ruleHandle1 = new InputStreamHandle(); + + // get the rule file + InputStream inputStream1 = new FileInputStream("src/test/java/com/marklogic/javaclient/rules/alertRule1.xml"); + + ruleHandle1.set(inputStream1); + + String exception = ""; + + // write the rule to the database + try + { + ruleMgr.writeRule("RULE-TEST-A", ruleHandle1); // test case for non-matching rule name + } catch(Exception e) + { + exception = e.toString(); + } + + String expectedException = "Invalid content: If provided, rule name in payload must match rule name in URL"; + + assertTrue("Exception is not thrown", exception.contains(expectedException)); + + // release client + client.release(); + } + + @Test + public void testRawAlertJSON() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawAlertJSON"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/raw-alert/", "XML"); + } + + // create a manager for configuring rules + RuleManager ruleMgr = client.newRuleManager(); + + // get the rule + File file = new File("src/test/java/com/marklogic/javaclient/rules/alertRule1.json"); + + String ruleInJson = convertFileToString(file); + + // create a handle for the rule + StringHandle ruleHandle = new StringHandle(ruleInJson); + ruleHandle.setFormat(Format.JSON); + //FileHandle writeHandle = new FileHandle(file); + + // write the rule to the database + ruleMgr.writeRule("RULE-TEST-1-JSON", ruleHandle); + + // create a manager for document search criteria + QueryManager queryMgr = client.newQueryManager(); + + // specify the search criteria for the documents + String criteria = "atlantic"; + StringQueryDefinition querydef = queryMgr.newStringDefinition(); // test case with string def + querydef.setCriteria(criteria); + + // create query def + //StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); // tes case with structured query + //StructuredQueryDefinition termQuery1 = qb.term("Atlantic"); + + // create a manager for matching rules + RuleManager ruleMatchMgr = client.newRuleManager(); + + // match the rules against the documents qualified by the criteria + RuleDefinitionList matchedRules = ruleMatchMgr.match(querydef, new RuleDefinitionList()); + + System.out.println(matchedRules.size()); + + String expected= ""; + + // iterate over the matched rules + Iterator ruleItr = matchedRules.iterator(); + while (ruleItr.hasNext()) + { + RuleDefinition rule = ruleItr.next(); + System.out.println( + "document criteria matched rule "+ + rule.getName()+" with metadata "+rule.getMetadata() + ); + expected = expected + rule.getName() + " - " + rule.getMetadata() + " | "; + } + + System.out.println(expected); + + assertTrue("rule is not correct", expected.contains("RULE-TEST-1-JSON - {{http://marklogic.com/rest-api}rule-number=one json}")); + + // release client + client.release(); + } + + @Test + public void testRawAlertStructuredQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawAlertStructuredQuery"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/raw-alert/", "XML"); + } + + // create a manager for configuring rules + RuleManager ruleMgr = client.newRuleManager(); + + // get the rule + File file = new File("src/test/java/com/marklogic/javaclient/rules/alertRule1.xml"); + + // create a handle for the rule + FileHandle writeHandle = new FileHandle(file); + + // write the rule to the database + ruleMgr.writeRule("RULE-TEST-1", writeHandle); + + // create a manager for document search criteria + QueryManager queryMgr = client.newQueryManager(); + + // specify the search criteria for the documents + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); + StructuredQueryDefinition termQuery1 = qb.term("Atlantic"); + + // create a manager for matching rules + RuleManager ruleMatchMgr = client.newRuleManager(); + + // match the rules against the documents qualified by the criteria + RuleDefinitionList matchedRules = ruleMatchMgr.match(termQuery1, new RuleDefinitionList()); + + System.out.println(matchedRules.size()); // bug, should return 1 + + assertEquals("result count is not correct", 1, matchedRules.size()); + + // iterate over the matched rules + Iterator ruleItr = matchedRules.iterator(); + while (ruleItr.hasNext()) + { + RuleDefinition rule = ruleItr.next(); + System.out.println( + "document criteria matched rule "+ + rule.getName()+" with metadata "+rule.getMetadata() + ); + } + + // release client + client.release(); + } + + @Test + public void testRawAlertStructuredQueryTransform() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawAlertStructuredQueryTransform"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/raw-alert/", "XML"); + } + + // create a manager for configuring rules + RuleManager ruleMgr = client.newRuleManager(); + + // get the rule + File file = new File("src/test/java/com/marklogic/javaclient/rules/alertRule1.xml"); + + // create a handle for the rule + FileHandle writeHandle = new FileHandle(file); + + // write the rule to the database + ruleMgr.writeRule("RULE-TEST-1", writeHandle); + + // create a manager for document search criteria + QueryManager queryMgr = client.newQueryManager(); + + // specify the search criteria for the documents + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); + StructuredQueryDefinition termQuery1 = qb.term("Atlantic"); + + // Write the rule in Modules database of Server + TransformExtensionsManager transformManager= client.newServerConfigManager().newTransformExtensionsManager(); + + File ruleTransform = new File("src/test/java/com/marklogic/javaclient/rules/rule-transform.xqy"); + transformManager.writeXQueryTransform("ruleTransform", new FileHandle(ruleTransform)); + + // create a manager for matching rules + RuleManager ruleMatchMgr = client.newRuleManager(); + + ServerTransform transform = new ServerTransform("ruleTransform"); + RuleDefinitionList matchedRules = ruleMatchMgr.match(termQuery1, 0L, QueryManager.DEFAULT_PAGE_LENGTH, new String[] {}, new RuleDefinitionList(), transform); + + System.out.println(matchedRules.size()); // bug, should return 1 + + assertEquals("result count is not correct", 1, matchedRules.size()); + + // iterate over the matched rules + Iterator ruleItr = matchedRules.iterator(); + while (ruleItr.hasNext()) + { + RuleDefinition rule = ruleItr.next(); + System.out.println( + "document criteria matched rule "+ + rule.getName()+" with metadata "+rule.getMetadata() + ); + } + + // release client + client.release(); + } + + @Test + public void testRawAlertCandidateRules() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawAlertCandidateRules"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/raw-alert/", "XML"); + } + + // create a manager for configuring rules + RuleManager ruleMgr = client.newRuleManager(); + + // create handle + InputStreamHandle ruleHandle1 = new InputStreamHandle(); + InputStreamHandle ruleHandle2 = new InputStreamHandle(); + InputStreamHandle ruleHandle3 = new InputStreamHandle(); + + // get the rule file + InputStream inputStream1 = new FileInputStream("src/test/java/com/marklogic/javaclient/rules/alertRule1.xml"); + InputStream inputStream2 = new FileInputStream("src/test/java/com/marklogic/javaclient/rules/alertRule2.xml"); + InputStream inputStream3 = new FileInputStream("src/test/java/com/marklogic/javaclient/rules/alertRule3.xml"); + + ruleHandle1.set(inputStream1); + ruleHandle2.set(inputStream2); + ruleHandle3.set(inputStream3); + + // write the rule to the database + ruleMgr.writeRule("RULE-TEST-1", ruleHandle1); + ruleMgr.writeRule("RULE-TEST-2", ruleHandle2); + ruleMgr.writeRule("RULE-TEST-3", ruleHandle3); + + // get the json rule + File file = new File("src/test/java/com/marklogic/javaclient/rules/alertRule3.json"); + + String ruleInJson = convertFileToString(file); + + // create a handle for the rule + StringHandle ruleHandle4 = new StringHandle(ruleInJson); + ruleHandle4.setFormat(Format.JSON); + + // write the rule to the database + ruleMgr.writeRule("RULE-TEST-3-JSON", ruleHandle4); + + // create a manager for document search criteria + QueryManager queryMgr = client.newQueryManager(); + + // specify the search criteria for the documents + String criteria = "memex"; + StringQueryDefinition querydef = queryMgr.newStringDefinition(); + querydef.setCriteria(criteria); + + // create a manager for matching rules + RuleManager ruleMatchMgr = client.newRuleManager(); + + String[] candidateRules = {"RULE-TEST-1", "RULE-TEST-2", "RULE-TEST-3", "RULE-TEST-3-JSON"}; + + // match the rules against the documents qualified by the criteria + RuleDefinitionList matchedRules = ruleMatchMgr.match(querydef, 1,2, candidateRules, new RuleDefinitionList()); + + System.out.println(matchedRules.size()); + + String expected = ""; + + // iterate over the matched rules + Iterator ruleItr = matchedRules.iterator(); + while (ruleItr.hasNext()) + { + RuleDefinition rule = ruleItr.next(); + System.out.println( + "document criteria "+criteria+" matched rule "+ + rule.getName()+" with metadata "+rule.getMetadata() + ); + expected = expected + rule.getName() + " - " + rule.getMetadata() + " | "; + } + + System.out.println(expected); + + if(expected.equals("RULE-TEST-3 - {rule-number=three} | RULE-TEST-3-JSON - {{http://marklogic.com/rest-api}rule-number=three json} | ")) + { + assertTrue("rule is incorrect", expected.contains("RULE-TEST-3 - {rule-number=three} | RULE-TEST-3-JSON - {{http://marklogic.com/rest-api}rule-number=three json}")); + } + else if(expected.equals("RULE-TEST-3-JSON - {{http://marklogic.com/rest-api}rule-number=three json} | RULE-TEST-3 - {rule-number=three} | ")) + { + assertTrue("rule is incorrect", expected.contains("RULE-TEST-3-JSON - {{http://marklogic.com/rest-api}rule-number=three json} | RULE-TEST-3 - {rule-number=three}")); + } + else + { + assertTrue("there is no matching rule", false); + } + + // release client + client.release(); + } + + @Test + public void testRawAlertCandidateRulesUnmatched() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawAlertCandidateRulesUnmatched"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/raw-alert/", "XML"); + } + + // create a manager for configuring rules + RuleManager ruleMgr = client.newRuleManager(); + + // create handle + InputStreamHandle ruleHandle1 = new InputStreamHandle(); + InputStreamHandle ruleHandle2 = new InputStreamHandle(); + InputStreamHandle ruleHandle3 = new InputStreamHandle(); + + // get the rule file + InputStream inputStream1 = new FileInputStream("src/test/java/com/marklogic/javaclient/rules/alertRule1.xml"); + InputStream inputStream2 = new FileInputStream("src/test/java/com/marklogic/javaclient/rules/alertRule2.xml"); + InputStream inputStream3 = new FileInputStream("src/test/java/com/marklogic/javaclient/rules/alertRule3.xml"); + + ruleHandle1.set(inputStream1); + ruleHandle2.set(inputStream2); + ruleHandle3.set(inputStream3); + + // write the rule to the database + ruleMgr.writeRule("RULE-TEST-1", ruleHandle1); + ruleMgr.writeRule("RULE-TEST-2", ruleHandle2); + ruleMgr.writeRule("RULE-TEST-3", ruleHandle3); + + // get the json rule + File file = new File("src/test/java/com/marklogic/javaclient/rules/alertRule3.json"); + + String ruleInJson = convertFileToString(file); + + // create a handle for the rule + StringHandle ruleHandle4 = new StringHandle(ruleInJson); + ruleHandle4.setFormat(Format.JSON); + + // write the rule to the database + ruleMgr.writeRule("RULE-TEST-3-JSON", ruleHandle4); + + // create a manager for document search criteria + QueryManager queryMgr = client.newQueryManager(); + + // specify the search criteria for the documents + String criteria = "atlantic"; + StringQueryDefinition querydef = queryMgr.newStringDefinition(); + querydef.setCriteria(criteria); + + // create a manager for matching rules + RuleManager ruleMatchMgr = client.newRuleManager(); + + String[] candidateRules = {"gar", "bar", "foo"}; + + // match the rules against the documents qualified by the criteria + + RuleDefinitionList matchedRules = ruleMatchMgr.match(querydef, 1, 2, candidateRules, new RuleDefinitionList()); + + System.out.println(matchedRules.size()); + + assertEquals("match rule is incorrect", 0, matchedRules.size()); + + // release client + client.release(); + } + + @Test + public void testRawAlertDocUris() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawAlertDocUris"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/raw-alert/", "XML"); + } + + // create a manager for configuring rules + RuleManager ruleMgr = client.newRuleManager(); + + // get the rule + File file = new File("src/test/java/com/marklogic/javaclient/rules/alertRule1.xml"); + + //String combinedQuery = convertFileToString(file); + + // create a handle for the rule + //StringHandle rawHandle = new StringHandle(combinedQuery); + FileHandle writeHandle = new FileHandle(file); + + // write the rule to the database + ruleMgr.writeRule("RULE-TEST-1", writeHandle); + + // specify the search criteria for the documents + String[] docUris = {"/raw-alert/constraint1.xml", + "/raw-alert/constraint2.xml", + "/raw-alert/constraint3.xml", + "/raw-alert/constraint4.xml", + "/raw-alert/constraint5.xml"}; + + // create a manager for matching rules + RuleManager ruleMatchMgr = client.newRuleManager(); + + // match the rules against the documents qualified by the criteria + RuleDefinitionList matchedRules = ruleMatchMgr.match(docUris, new RuleDefinitionList()); + + System.out.println(matchedRules.size()); + + String expected = ""; + + // iterate over the matched rules + Iterator ruleItr = matchedRules.iterator(); + while (ruleItr.hasNext()) + { + RuleDefinition rule = ruleItr.next(); + System.out.println( + "document criteria matched rule "+ + rule.getName()+" with metadata "+rule.getMetadata() + ); + expected = expected + rule.getName() + " - " + rule.getMetadata() + " | "; + } + + System.out.println(expected); + + assertTrue("rule is incorrect", expected.contains("RULE-TEST-1 - {rule-number=one}")); + + // release client + client.release(); + } + + @Test + public void testRawAlertDocPayload() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawAlertDocPayload"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/raw-alert/", "XML"); + } + + // create a manager for configuring rules + RuleManager ruleMgr = client.newRuleManager(); + + // get the rule + File file = new File("src/test/java/com/marklogic/javaclient/rules/alertRule2.xml"); + + // create a handle for the rule + FileHandle writeHandle = new FileHandle(file); + + // write the rule to the database + ruleMgr.writeRule("RULE-TEST-2", writeHandle); + + String filename = "constraint1.xml"; + + // get the file + File doc = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + String docContent = convertFileToString(doc); + StringHandle handle = new StringHandle(docContent); + + // create a manager for matching rules + RuleManager ruleMatchMgr = client.newRuleManager(); + + // match the rules against the documents qualified by the criteria + RuleDefinitionList matchedRules = ruleMatchMgr.match(handle, new RuleDefinitionList()); + + System.out.println(matchedRules.size()); + + String expected = ""; + + // iterate over the matched rules + Iterator ruleItr = matchedRules.iterator(); + while (ruleItr.hasNext()) + { + RuleDefinition rule = ruleItr.next(); + System.out.println( + "document criteria matched rule "+ + rule.getName()+" with metadata "+rule.getMetadata() + ); + expected = expected + rule.getName() + " - " + rule.getMetadata() + " | "; + } + + System.out.println(expected); + + assertTrue("rule is incorrect", expected.contains("RULE-TEST-2 - {rule-number=two}")); + + // release client + client.release(); + } + + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQuery.java b/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQuery.java index d81af0a45..a65d66fc9 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQuery.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQuery.java @@ -1,769 +1,762 @@ -package com.marklogic.javaclient; - -import java.io.File; -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.query.RawCombinedQueryDefinition; - -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.admin.ServerConfigurationManager; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.DocumentMetadataHandle; -import com.marklogic.client.io.FileHandle; -import com.marklogic.client.io.Format; -import com.marklogic.client.io.StringHandle; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; -import static org.junit.Assert.*; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.junit.*; -public class TestRawCombinedQuery extends BasicJavaClientREST { - - private static String dbName = "TestRawCombinedQueryDB"; - private static String [] fNames = {"TestRawCombinedQueryDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort =8011; -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0],restServerName,8011); - setupAppServicesConstraint(dbName); - } -@After -public void testCleanUp() throws Exception -{ - clearDB(restPort); - System.out.println("Running clear script"); -} -@SuppressWarnings("deprecation") -@Test public void testBug22353() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testBug22353"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/raw-combined-query/", "XML"); - } - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOption.xml"); - - String combinedQuery = convertFileToString(file); - - // create a handle for the search criteria - StringHandle rawHandle = new StringHandle(combinedQuery); - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); - queryMgr.newStringDefinition("LinkResultDocumentsOpt.xml"); - - // create result handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - System.out.println("Mime Type : "+resultsHandle.getMimetype()); - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertEquals("application/xml",resultsHandle.getMimetype()); - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testRawCombinedQueryXML() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawCombinedQueryXML"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/raw-combined-query/", "XML"); - } - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOption.xml"); - - String combinedQuery = convertFileToString(file); - - // create a handle for the search criteria - StringHandle rawHandle = new StringHandle(combinedQuery); - //FileHandle rawHandle = new FileHandle(file); // bug 21107 - //rawHandle.setMimetype("application/xml"); - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); - - // create result handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testRawCombinedQueryXMLWithOptions() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawCombinedQueryXMLWithOptions"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "valueConstraintWithoutIndexSettingsAndNSOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.setQueryValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/raw-combined-query/", "XML"); - } - - setQueryOption(client, queryOptionName); - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryNoOption.xml"); - - // create a handle for the search criteria - FileHandle rawHandle = new FileHandle(file); - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle, queryOptionName); - - // create result handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testRawCombinedQueryXMLWithOverwriteOptions() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawCombinedQueryXMLWithOverwriteOptions"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "valueConstraintWithoutIndexSettingsAndNSOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/raw-combined-query/", "XML"); - } - - setQueryOption(client, queryOptionName); - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionCollectionOverwrite.xml"); - - // create a handle for the search criteria - FileHandle rawHandle = new FileHandle(file); - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle, queryOptionName); - - // create result handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("0011", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testRawCombinedQueryJSONWithOverwriteOptions() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawCombinedQueryJSONWithOverwriteOptions"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "valueConstraintWithoutIndexSettingsAndNSOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/raw-combined-query/", "XML"); - } - - setQueryOption(client, queryOptionName); - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionJSONOverwrite.json"); - - String combinedQuery = convertFileToString(file); - - // create a handle for the search criteria - StringHandle rawHandle = new StringHandle(combinedQuery); - //FileHandle rawHandle = new FileHandle(file); - //rawHandle.setMimetype("application/xml"); - rawHandle.setFormat(Format.JSON); - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle, queryOptionName); - - // create result handle - StringHandle resultsHandle = new StringHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - String resultDoc = resultsHandle.get(); - - System.out.println(resultDoc); - - assertTrue("document is not returned", resultDoc.contains("/raw-combined-query/constraint1.xml")); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testRawCombinedQueryJSON() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawCombinedQueryJSON"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/raw-combined-query/", "XML"); - } - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionJSON.json"); - - String combinedQuery = convertFileToString(file); - - // create a handle for the search criteria - StringHandle rawHandle = new StringHandle(combinedQuery); - //FileHandle rawHandle = new FileHandle(file); - //rawHandle.setMimetype("application/xml"); - rawHandle.setFormat(Format.JSON); - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); - - // create result handle - StringHandle resultsHandle = new StringHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - String resultDoc = resultsHandle.get(); - - System.out.println(resultDoc); - - assertTrue("document is not returned", resultDoc.contains("/raw-combined-query/constraint5.xml")); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testRawCombinedQueryWildcard() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawCombinedQueryWildcard"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/raw-combined-query/", "XML"); - } - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionWildcard.xml"); - - String combinedQuery = convertFileToString(file); - - // create a handle for the search criteria - StringHandle rawHandle = new StringHandle(combinedQuery); - //FileHandle rawHandle = new FileHandle(file); // bug 21107 - //rawHandle.setMimetype("application/xml"); - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); - - // create result handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testRawCombinedQueryCollection() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawCombinedQueryCollection"); - - String filename1 = "constraint1.xml"; - String filename2 = "constraint2.xml"; - String filename3 = "constraint3.xml"; - String filename4 = "constraint4.xml"; - String filename5 = "constraint5.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create and initialize a handle on the metadata - DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle4 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle5 = new DocumentMetadataHandle(); - - // set the metadata - metadataHandle1.getCollections().addAll("http://test.com/set1"); - metadataHandle1.getCollections().addAll("http://test.com/set5"); - metadataHandle2.getCollections().addAll("http://test.com/set1"); - metadataHandle3.getCollections().addAll("http://test.com/set3"); - metadataHandle4.getCollections().addAll("http://test.com/set3/set3-1"); - metadataHandle5.getCollections().addAll("http://test.com/set1"); - metadataHandle5.getCollections().addAll("http://test.com/set5"); - - // write docs - writeDocumentUsingInputStreamHandle(client, filename1, "/collection-constraint/", metadataHandle1, "XML"); - writeDocumentUsingInputStreamHandle(client, filename2, "/collection-constraint/", metadataHandle2, "XML"); - writeDocumentUsingInputStreamHandle(client, filename3, "/collection-constraint/", metadataHandle3, "XML"); - writeDocumentUsingInputStreamHandle(client, filename4, "/collection-constraint/", metadataHandle4, "XML"); - writeDocumentUsingInputStreamHandle(client, filename5, "/collection-constraint/", metadataHandle5, "XML"); - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionCollection.xml"); - - // create a handle for the search criteria - FileHandle rawHandle = new FileHandle(file); // bug 21107 - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); - - // create result handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("Vannevar Bush", "string(//*[local-name()='result'][1]//*[local-name()='title'])", resultDoc); - - // release client - client.release(); - } - - - - -@Test public void testRawCombinedQueryCombo() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawCombinedQueryCombo"); - - String filename1 = "constraint1.xml"; - String filename2 = "constraint2.xml"; - String filename3 = "constraint3.xml"; - String filename4 = "constraint4.xml"; - String filename5 = "constraint5.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create and initialize a handle on the metadata - DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle4 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle5 = new DocumentMetadataHandle(); - - // set the metadata - metadataHandle1.getCollections().addAll("http://test.com/set1"); - metadataHandle1.getCollections().addAll("http://test.com/set5"); - metadataHandle2.getCollections().addAll("http://test.com/set1"); - metadataHandle3.getCollections().addAll("http://test.com/set3"); - metadataHandle4.getCollections().addAll("http://test.com/set3/set3-1"); - metadataHandle5.getCollections().addAll("http://test.com/set1"); - metadataHandle5.getCollections().addAll("http://test.com/set5"); - - // write docs - writeDocumentUsingInputStreamHandle(client, filename1, "/collection-constraint/", metadataHandle1, "XML"); - writeDocumentUsingInputStreamHandle(client, filename2, "/collection-constraint/", metadataHandle2, "XML"); - writeDocumentUsingInputStreamHandle(client, filename3, "/collection-constraint/", metadataHandle3, "XML"); - writeDocumentUsingInputStreamHandle(client, filename4, "/collection-constraint/", metadataHandle4, "XML"); - writeDocumentUsingInputStreamHandle(client, filename5, "/collection-constraint/", metadataHandle5, "XML"); - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionCombo.xml"); - - // create a handle for the search criteria - FileHandle rawHandle = new FileHandle(file); // bug 21107 - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); - - // create result handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("Vannevar Bush", "string(//*[local-name()='result'][1]//*[local-name()='title'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testRawCombinedQueryField() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawCombinedQueryField"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/field-constraint/", "XML"); - } - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionField.xml"); - - // create a handle for the search criteria - FileHandle rawHandle = new FileHandle(file); // bug 21107 - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); - - // create result handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("memex", "string(//*[local-name()='result'][1]//*[local-name()='match'][1]//*[local-name()='highlight'])", resultDoc); - assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='match'][2]//*[local-name()='highlight'])", resultDoc); - assertXpathEvaluatesTo("Memex", "string(//*[local-name()='result'][1]//*[local-name()='match'][3]//*[local-name()='highlight'])", resultDoc); - assertXpathEvaluatesTo("Bush", "string(//*[local-name()='result'][2]//*[local-name()='match'][1]//*[local-name()='highlight'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testRawCombinedQueryPathIndex() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawCombinedQueryPathIndex"); - - String[] filenames = {"pathindex1.xml", "pathindex2.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/path-index-raw/", "XML"); - } - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionPathIndex.xml"); - - // create a handle for the search criteria - FileHandle rawHandle = new FileHandle(file); // bug 21107 - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); - - // create result handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("/path-index-raw/pathindex2.xml", "string(//*[local-name()='result'][1]//@*[local-name()='uri'])", resultDoc); - assertXpathEvaluatesTo("/path-index-raw/pathindex1.xml", "string(//*[local-name()='result'][2]//@*[local-name()='uri'])", resultDoc); - - // release client - client.release(); - } - - - -@Test public void testRawCombinedQueryComboJSON() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawCombinedQueryComboJSON"); - - String filename1 = "constraint1.xml"; - String filename2 = "constraint2.xml"; - String filename3 = "constraint3.xml"; - String filename4 = "constraint4.xml"; - String filename5 = "constraint5.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create and initialize a handle on the metadata - DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle4 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle5 = new DocumentMetadataHandle(); - - // set the metadata - metadataHandle1.getCollections().addAll("http://test.com/set1"); - metadataHandle1.getCollections().addAll("http://test.com/set5"); - metadataHandle2.getCollections().addAll("http://test.com/set1"); - metadataHandle3.getCollections().addAll("http://test.com/set3"); - metadataHandle4.getCollections().addAll("http://test.com/set3/set3-1"); - metadataHandle5.getCollections().addAll("http://test.com/set1"); - metadataHandle5.getCollections().addAll("http://test.com/set5"); - - // write docs - writeDocumentUsingInputStreamHandle(client, filename1, "/collection-constraint/", metadataHandle1, "XML"); - writeDocumentUsingInputStreamHandle(client, filename2, "/collection-constraint/", metadataHandle2, "XML"); - writeDocumentUsingInputStreamHandle(client, filename3, "/collection-constraint/", metadataHandle3, "XML"); - writeDocumentUsingInputStreamHandle(client, filename4, "/collection-constraint/", metadataHandle4, "XML"); - writeDocumentUsingInputStreamHandle(client, filename5, "/collection-constraint/", metadataHandle5, "XML"); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.setQueryValidation(false); - srvMgr.writeConfiguration(); - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionComboJSON.json"); - - String combinedQuery = convertFileToString(file); - - // create a handle for the search criteria - StringHandle rawHandle = new StringHandle(combinedQuery); - rawHandle.setFormat(Format.JSON); - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); - - // create result handle - StringHandle resultsHandle = new StringHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - String resultDoc = resultsHandle.get(); - - System.out.println(resultDoc); - - assertTrue("Returned result is not correct", resultDoc.contains("")); - - // release client - client.release(); - } - - -@Test public void testRawCombinedQueryFieldJSON() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawCombinedQueryFieldJSON"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/field-constraint/", "XML"); - } - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.setQueryValidation(false); - srvMgr.writeConfiguration(); - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionFieldJSON.json"); - - String combinedQuery = convertFileToString(file); - - // create a handle for the search criteria - StringHandle rawHandle = new StringHandle(combinedQuery); - rawHandle.setFormat(Format.JSON); - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); - - // create result handle - StringHandle resultsHandle = new StringHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - String resultDoc = resultsHandle.get(); - - System.out.println(resultDoc); - - assertTrue("total document returned is incorrect", resultDoc.contains("total=\"2\"")); - assertTrue("returned doc is incorrect", resultDoc.contains("uri=\"/field-constraint/constraint5.xml\"")); - assertTrue("returned doc is incorrect", resultDoc.contains("uri=\"/field-constraint/constraint1.xml\"")); - - // release client - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } -} +package com.marklogic.javaclient; + +import java.io.File; +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.RawCombinedQueryDefinition; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.admin.ServerConfigurationManager; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.DocumentMetadataHandle; +import com.marklogic.client.io.FileHandle; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.StringHandle; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; +import static org.junit.Assert.*; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.*; + +public class TestRawCombinedQuery extends BasicJavaClientREST { + private static String dbName = "TestRawCombinedQueryDB"; + private static String [] fNames = {"TestRawCombinedQueryDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0],restServerName,8011); + setupAppServicesConstraint(dbName); + } + @After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + + @Test + public void testBug22353() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testBug22353"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/raw-combined-query/", "XML"); + } + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOption.xml"); + + String combinedQuery = convertFileToString(file); + + // create a handle for the search criteria + StringHandle rawHandle = new StringHandle(combinedQuery); + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); + queryMgr.newStringDefinition("LinkResultDocumentsOpt.xml"); + + // create result handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println("Mime Type : "+resultsHandle.getMimetype()); + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertEquals("application/xml",resultsHandle.getMimetype()); + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testRawCombinedQueryXML() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawCombinedQueryXML"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/raw-combined-query/", "XML"); + } + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOption.xml"); + + String combinedQuery = convertFileToString(file); + + // create a handle for the search criteria + StringHandle rawHandle = new StringHandle(combinedQuery); + //FileHandle rawHandle = new FileHandle(file); // bug 21107 + //rawHandle.setMimetype("application/xml"); + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); + + // create result handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testRawCombinedQueryXMLWithOptions() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawCombinedQueryXMLWithOptions"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "valueConstraintWithoutIndexSettingsAndNSOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.setQueryValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/raw-combined-query/", "XML"); + } + + setQueryOption(client, queryOptionName); + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryNoOption.xml"); + + // create a handle for the search criteria + FileHandle rawHandle = new FileHandle(file); + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle, queryOptionName); + + // create result handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testRawCombinedQueryXMLWithOverwriteOptions() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawCombinedQueryXMLWithOverwriteOptions"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "valueConstraintWithoutIndexSettingsAndNSOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/raw-combined-query/", "XML"); + } + + setQueryOption(client, queryOptionName); + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionCollectionOverwrite.xml"); + + // create a handle for the search criteria + FileHandle rawHandle = new FileHandle(file); + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle, queryOptionName); + + // create result handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("0011", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testRawCombinedQueryJSONWithOverwriteOptions() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawCombinedQueryJSONWithOverwriteOptions"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "valueConstraintWithoutIndexSettingsAndNSOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/raw-combined-query/", "XML"); + } + + setQueryOption(client, queryOptionName); + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionJSONOverwrite.json"); + + String combinedQuery = convertFileToString(file); + + // create a handle for the search criteria + StringHandle rawHandle = new StringHandle(combinedQuery); + //FileHandle rawHandle = new FileHandle(file); + //rawHandle.setMimetype("application/xml"); + rawHandle.setFormat(Format.JSON); + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle, queryOptionName); + + // create result handle + StringHandle resultsHandle = new StringHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + String resultDoc = resultsHandle.get(); + + System.out.println(resultDoc); + + assertTrue("document is not returned", resultDoc.contains("/raw-combined-query/constraint1.xml")); + + // release client + client.release(); + } + + @Test + public void testRawCombinedQueryJSON() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawCombinedQueryJSON"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/raw-combined-query/", "XML"); + } + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionJSON.json"); + + String combinedQuery = convertFileToString(file); + + // create a handle for the search criteria + StringHandle rawHandle = new StringHandle(combinedQuery); + //FileHandle rawHandle = new FileHandle(file); + //rawHandle.setMimetype("application/xml"); + rawHandle.setFormat(Format.JSON); + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); + + // create result handle + StringHandle resultsHandle = new StringHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + String resultDoc = resultsHandle.get(); + + System.out.println(resultDoc); + + assertTrue("document is not returned", resultDoc.contains("/raw-combined-query/constraint5.xml")); + + // release client + client.release(); + } + + @Test + public void testRawCombinedQueryWildcard() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawCombinedQueryWildcard"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/raw-combined-query/", "XML"); + } + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionWildcard.xml"); + + String combinedQuery = convertFileToString(file); + + // create a handle for the search criteria + StringHandle rawHandle = new StringHandle(combinedQuery); + //FileHandle rawHandle = new FileHandle(file); // bug 21107 + //rawHandle.setMimetype("application/xml"); + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); + + // create result handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); + assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testRawCombinedQueryCollection() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawCombinedQueryCollection"); + + String filename1 = "constraint1.xml"; + String filename2 = "constraint2.xml"; + String filename3 = "constraint3.xml"; + String filename4 = "constraint4.xml"; + String filename5 = "constraint5.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle4 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle5 = new DocumentMetadataHandle(); + + // set the metadata + metadataHandle1.getCollections().addAll("http://test.com/set1"); + metadataHandle1.getCollections().addAll("http://test.com/set5"); + metadataHandle2.getCollections().addAll("http://test.com/set1"); + metadataHandle3.getCollections().addAll("http://test.com/set3"); + metadataHandle4.getCollections().addAll("http://test.com/set3/set3-1"); + metadataHandle5.getCollections().addAll("http://test.com/set1"); + metadataHandle5.getCollections().addAll("http://test.com/set5"); + + // write docs + writeDocumentUsingInputStreamHandle(client, filename1, "/collection-constraint/", metadataHandle1, "XML"); + writeDocumentUsingInputStreamHandle(client, filename2, "/collection-constraint/", metadataHandle2, "XML"); + writeDocumentUsingInputStreamHandle(client, filename3, "/collection-constraint/", metadataHandle3, "XML"); + writeDocumentUsingInputStreamHandle(client, filename4, "/collection-constraint/", metadataHandle4, "XML"); + writeDocumentUsingInputStreamHandle(client, filename5, "/collection-constraint/", metadataHandle5, "XML"); + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionCollection.xml"); + + // create a handle for the search criteria + FileHandle rawHandle = new FileHandle(file); // bug 21107 + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); + + // create result handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("Vannevar Bush", "string(//*[local-name()='result'][1]//*[local-name()='title'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testRawCombinedQueryCombo() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawCombinedQueryCombo"); + + String filename1 = "constraint1.xml"; + String filename2 = "constraint2.xml"; + String filename3 = "constraint3.xml"; + String filename4 = "constraint4.xml"; + String filename5 = "constraint5.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle4 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle5 = new DocumentMetadataHandle(); + + // set the metadata + metadataHandle1.getCollections().addAll("http://test.com/set1"); + metadataHandle1.getCollections().addAll("http://test.com/set5"); + metadataHandle2.getCollections().addAll("http://test.com/set1"); + metadataHandle3.getCollections().addAll("http://test.com/set3"); + metadataHandle4.getCollections().addAll("http://test.com/set3/set3-1"); + metadataHandle5.getCollections().addAll("http://test.com/set1"); + metadataHandle5.getCollections().addAll("http://test.com/set5"); + + // write docs + writeDocumentUsingInputStreamHandle(client, filename1, "/collection-constraint/", metadataHandle1, "XML"); + writeDocumentUsingInputStreamHandle(client, filename2, "/collection-constraint/", metadataHandle2, "XML"); + writeDocumentUsingInputStreamHandle(client, filename3, "/collection-constraint/", metadataHandle3, "XML"); + writeDocumentUsingInputStreamHandle(client, filename4, "/collection-constraint/", metadataHandle4, "XML"); + writeDocumentUsingInputStreamHandle(client, filename5, "/collection-constraint/", metadataHandle5, "XML"); + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionCombo.xml"); + + // create a handle for the search criteria + FileHandle rawHandle = new FileHandle(file); // bug 21107 + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); + + // create result handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("Vannevar Bush", "string(//*[local-name()='result'][1]//*[local-name()='title'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testRawCombinedQueryField() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawCombinedQueryField"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/field-constraint/", "XML"); + } + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionField.xml"); + + // create a handle for the search criteria + FileHandle rawHandle = new FileHandle(file); // bug 21107 + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); + + // create result handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("memex", "string(//*[local-name()='result'][1]//*[local-name()='match'][1]//*[local-name()='highlight'])", resultDoc); + assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='match'][2]//*[local-name()='highlight'])", resultDoc); + assertXpathEvaluatesTo("Memex", "string(//*[local-name()='result'][1]//*[local-name()='match'][3]//*[local-name()='highlight'])", resultDoc); + assertXpathEvaluatesTo("Bush", "string(//*[local-name()='result'][2]//*[local-name()='match'][1]//*[local-name()='highlight'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testRawCombinedQueryPathIndex() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawCombinedQueryPathIndex"); + + String[] filenames = {"pathindex1.xml", "pathindex2.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/path-index-raw/", "XML"); + } + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionPathIndex.xml"); + + // create a handle for the search criteria + FileHandle rawHandle = new FileHandle(file); // bug 21107 + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); + + // create result handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("/path-index-raw/pathindex2.xml", "string(//*[local-name()='result'][1]//@*[local-name()='uri'])", resultDoc); + assertXpathEvaluatesTo("/path-index-raw/pathindex1.xml", "string(//*[local-name()='result'][2]//@*[local-name()='uri'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testRawCombinedQueryComboJSON() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawCombinedQueryComboJSON"); + + String filename1 = "constraint1.xml"; + String filename2 = "constraint2.xml"; + String filename3 = "constraint3.xml"; + String filename4 = "constraint4.xml"; + String filename5 = "constraint5.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle4 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle5 = new DocumentMetadataHandle(); + + // set the metadata + metadataHandle1.getCollections().addAll("http://test.com/set1"); + metadataHandle1.getCollections().addAll("http://test.com/set5"); + metadataHandle2.getCollections().addAll("http://test.com/set1"); + metadataHandle3.getCollections().addAll("http://test.com/set3"); + metadataHandle4.getCollections().addAll("http://test.com/set3/set3-1"); + metadataHandle5.getCollections().addAll("http://test.com/set1"); + metadataHandle5.getCollections().addAll("http://test.com/set5"); + + // write docs + writeDocumentUsingInputStreamHandle(client, filename1, "/collection-constraint/", metadataHandle1, "XML"); + writeDocumentUsingInputStreamHandle(client, filename2, "/collection-constraint/", metadataHandle2, "XML"); + writeDocumentUsingInputStreamHandle(client, filename3, "/collection-constraint/", metadataHandle3, "XML"); + writeDocumentUsingInputStreamHandle(client, filename4, "/collection-constraint/", metadataHandle4, "XML"); + writeDocumentUsingInputStreamHandle(client, filename5, "/collection-constraint/", metadataHandle5, "XML"); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.setQueryValidation(false); + srvMgr.writeConfiguration(); + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionComboJSON.json"); + + String combinedQuery = convertFileToString(file); + + // create a handle for the search criteria + StringHandle rawHandle = new StringHandle(combinedQuery); + rawHandle.setFormat(Format.JSON); + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); + + // create result handle + StringHandle resultsHandle = new StringHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + String resultDoc = resultsHandle.get(); + + System.out.println(resultDoc); + + assertTrue("Returned result is not correct", resultDoc.contains("")); + + // release client + client.release(); + } + + @Test + public void testRawCombinedQueryFieldJSON() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawCombinedQueryFieldJSON"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/field-constraint/", "XML"); + } + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.setQueryValidation(false); + srvMgr.writeConfiguration(); + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionFieldJSON.json"); + + String combinedQuery = convertFileToString(file); + + // create a handle for the search criteria + StringHandle rawHandle = new StringHandle(combinedQuery); + rawHandle.setFormat(Format.JSON); + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); + + // create result handle + StringHandle resultsHandle = new StringHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + String resultDoc = resultsHandle.get(); + + System.out.println(resultDoc); + + assertTrue("total document returned is incorrect", resultDoc.contains("total=\"2\"")); + assertTrue("returned doc is incorrect", resultDoc.contains("uri=\"/field-constraint/constraint5.xml\"")); + assertTrue("returned doc is incorrect", resultDoc.contains("uri=\"/field-constraint/constraint1.xml\"")); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQueryGeo.java b/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQueryGeo.java index 8248d4afa..7c0cd6cf0 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQueryGeo.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQueryGeo.java @@ -1,355 +1,350 @@ -package com.marklogic.javaclient; - -import java.io.File; -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.query.RawCombinedQueryDefinition; - -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.query.StringQueryDefinition; -import com.marklogic.client.admin.ServerConfigurationManager; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.FileHandle; -import com.marklogic.client.io.Format; -import com.marklogic.client.io.StringHandle; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; -import static org.junit.Assert.*; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.junit.*; -public class TestRawCombinedQueryGeo extends BasicJavaClientREST { - - private static String dbName = "TestRawCombinedQueryGeoDB"; - private static String [] fNames = {"TestRawCombinedQueryGeoDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort =8011; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0],restServerName,8011); - setupAppServicesGeoConstraint(dbName); - } - -@After - public void testCleanUp() throws Exception - { - clearDB(restPort); - System.out.println("Running clear script"); - } - -@SuppressWarnings("deprecation") -@Test public void testRawCombinedQueryGeo() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawCombinedQueryGeo"); - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - loadGeoData(); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionGeo.xml"); - - // create a handle for the search criteria - FileHandle rawHandle = new FileHandle(file); // bug 21107 - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); - - // create result handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("karl_kara 12,5 12,5 12 5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testRawCombinedQueryGeoJSON() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawCombinedQueryGeoJSON"); - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - loadGeoData(); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionGeoJSON.json"); - - String combinedQuery = convertFileToString(file); - - // create a handle for the search criteria - StringHandle rawHandle = new StringHandle(combinedQuery); - rawHandle.setFormat(Format.JSON); - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); - - // create result handle - StringHandle resultsHandle = new StringHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - String resultDoc = resultsHandle.get(); - - System.out.println(resultDoc); - - assertTrue("total document returned is incorrect", resultDoc.contains("total=\"1\"")); - assertTrue("returned doc is incorrect", resultDoc.contains("uri=\"/geo-constraint/geo-constraint1.xml\"")); - assertTrue("matched text is incorrect", resultDoc.contains("karl_kara 12,5 12,5 12 5")); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testRawCombinedQueryGeoBoxAndWordJSON() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawCombinedQueryGeoBoxAndWordJSON"); - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - loadGeoData(); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionGeoBoxAndWordJSON.json"); - - String combinedQuery = convertFileToString(file); - - // create a handle for the search criteria - StringHandle rawHandle = new StringHandle(combinedQuery); - rawHandle.setFormat(Format.JSON); - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); - - // create result handle - StringHandle resultsHandle = new StringHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - String resultDoc = resultsHandle.get(); - - System.out.println(resultDoc); - - assertTrue("total document returned is incorrect", resultDoc.contains("total=\"1\"")); - assertTrue("returned doc is incorrect", resultDoc.contains("uri=\"/geo-constraint/geo-constraint20.xml\"")); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testRawCombinedQueryGeoCircle() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("testRawCombinedQueryGeoCircle"); - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - loadGeoData(); - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionGeoCircle.xml"); - - // create a handle for the search criteria - FileHandle rawHandle = new FileHandle(file); // bug 21107 - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); - - // create result handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("5", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("karl_kara 12,-5 12,-5 12 -5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("jack_kara 11,-5 11,-5 11 -5", "string(//*[local-name()='result'][2]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("karl_jill 12,-4 12,-4 12 -4", "string(//*[local-name()='result'][3]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("bill_kara 13,-5 13,-5 13 -5", "string(//*[local-name()='result'][4]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("karl_gale 12,-6 12,-6 12 -6", "string(//*[local-name()='result'][5]//*[local-name()='match'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testRawCombinedQueryGeoBox() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("testRawCombinedQueryGeoBox"); - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - loadGeoData(); - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionGeoBox.xml"); - - // create a handle for the search criteria - FileHandle rawHandle = new FileHandle(file); // bug 21107 - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); - - // create result handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("3", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("karl_kara 12,-5 12,-5 12 -5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("jack_kara 11,-5 11,-5 11 -5", "string(//*[local-name()='result'][2]//*[local-name()='match'])", resultDoc); - assertXpathEvaluatesTo("karl_jill 12,-4 12,-4 12 -4", "string(//*[local-name()='result'][3]//*[local-name()='match'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testRawCombinedQueryGeoBoxAndWord() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawCombinedQueryGeoBoxAndWord"); - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - loadGeoData(); - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionGeoBoxAndWord.xml"); - - // create a handle for the search criteria - FileHandle rawHandle = new FileHandle(file); // bug 21107 - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); - - // create result handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("/geo-constraint/geo-constraint20.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testRawCombinedQueryGeoPointAndWord() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawCombinedQueryGeoPointAndWord"); - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - for(int i = 1; i <= 9; i++) - { - writeDocumentUsingInputStreamHandle(client, "geo-constraint" + i + ".xml", "/geo-constraint/", "XML"); - } - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionGeoPointAndWord.xml"); - - // create a handle for the search criteria - FileHandle rawHandle = new FileHandle(file); // bug 21107 - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); - - // create result handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("/geo-constraint/geo-constraint8.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); - - // release client - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } -} +package com.marklogic.javaclient; + +import java.io.File; +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.RawCombinedQueryDefinition; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.admin.ServerConfigurationManager; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.FileHandle; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.StringHandle; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; +import static org.junit.Assert.*; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.*; + +public class TestRawCombinedQueryGeo extends BasicJavaClientREST { + private static String dbName = "TestRawCombinedQueryGeoDB"; + private static String [] fNames = {"TestRawCombinedQueryGeoDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort =8011; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0],restServerName,8011); + setupAppServicesGeoConstraint(dbName); + } + + @After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + + @Test + public void testRawCombinedQueryGeo() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawCombinedQueryGeo"); + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + loadGeoData(); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionGeo.xml"); + + // create a handle for the search criteria + FileHandle rawHandle = new FileHandle(file); // bug 21107 + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); + + // create result handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("karl_kara 12,5 12,5 12 5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testRawCombinedQueryGeoJSON() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawCombinedQueryGeoJSON"); + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + loadGeoData(); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionGeoJSON.json"); + + String combinedQuery = convertFileToString(file); + + // create a handle for the search criteria + StringHandle rawHandle = new StringHandle(combinedQuery); + rawHandle.setFormat(Format.JSON); + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); + + // create result handle + StringHandle resultsHandle = new StringHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + String resultDoc = resultsHandle.get(); + + System.out.println(resultDoc); + + assertTrue("total document returned is incorrect", resultDoc.contains("total=\"1\"")); + assertTrue("returned doc is incorrect", resultDoc.contains("uri=\"/geo-constraint/geo-constraint1.xml\"")); + assertTrue("matched text is incorrect", resultDoc.contains("karl_kara 12,5 12,5 12 5")); + + // release client + client.release(); + } + + @Test + public void testRawCombinedQueryGeoBoxAndWordJSON() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawCombinedQueryGeoBoxAndWordJSON"); + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + loadGeoData(); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionGeoBoxAndWordJSON.json"); + + String combinedQuery = convertFileToString(file); + + // create a handle for the search criteria + StringHandle rawHandle = new StringHandle(combinedQuery); + rawHandle.setFormat(Format.JSON); + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); + + // create result handle + StringHandle resultsHandle = new StringHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + String resultDoc = resultsHandle.get(); + + System.out.println(resultDoc); + + assertTrue("total document returned is incorrect", resultDoc.contains("total=\"1\"")); + assertTrue("returned doc is incorrect", resultDoc.contains("uri=\"/geo-constraint/geo-constraint20.xml\"")); + + // release client + client.release(); + } + + @Test + public void testRawCombinedQueryGeoCircle() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("testRawCombinedQueryGeoCircle"); + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + loadGeoData(); + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionGeoCircle.xml"); + + // create a handle for the search criteria + FileHandle rawHandle = new FileHandle(file); // bug 21107 + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); + + // create result handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("5", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("karl_kara 12,-5 12,-5 12 -5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); + assertXpathEvaluatesTo("jack_kara 11,-5 11,-5 11 -5", "string(//*[local-name()='result'][2]//*[local-name()='match'])", resultDoc); + assertXpathEvaluatesTo("karl_jill 12,-4 12,-4 12 -4", "string(//*[local-name()='result'][3]//*[local-name()='match'])", resultDoc); + assertXpathEvaluatesTo("bill_kara 13,-5 13,-5 13 -5", "string(//*[local-name()='result'][4]//*[local-name()='match'])", resultDoc); + assertXpathEvaluatesTo("karl_gale 12,-6 12,-6 12 -6", "string(//*[local-name()='result'][5]//*[local-name()='match'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testRawCombinedQueryGeoBox() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("testRawCombinedQueryGeoBox"); + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + loadGeoData(); + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionGeoBox.xml"); + + // create a handle for the search criteria + FileHandle rawHandle = new FileHandle(file); // bug 21107 + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); + + // create result handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("3", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("karl_kara 12,-5 12,-5 12 -5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); + assertXpathEvaluatesTo("jack_kara 11,-5 11,-5 11 -5", "string(//*[local-name()='result'][2]//*[local-name()='match'])", resultDoc); + assertXpathEvaluatesTo("karl_jill 12,-4 12,-4 12 -4", "string(//*[local-name()='result'][3]//*[local-name()='match'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testRawCombinedQueryGeoBoxAndWord() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawCombinedQueryGeoBoxAndWord"); + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + loadGeoData(); + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionGeoBoxAndWord.xml"); + + // create a handle for the search criteria + FileHandle rawHandle = new FileHandle(file); // bug 21107 + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); + + // create result handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("/geo-constraint/geo-constraint20.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testRawCombinedQueryGeoPointAndWord() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawCombinedQueryGeoPointAndWord"); + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(int i = 1; i <= 9; i++) + { + writeDocumentUsingInputStreamHandle(client, "geo-constraint" + i + ".xml", "/geo-constraint/", "XML"); + } + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionGeoPointAndWord.xml"); + + // create a handle for the search criteria + FileHandle rawHandle = new FileHandle(file); // bug 21107 + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); + + // create result handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("/geo-constraint/geo-constraint8.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestRawStructuredQuery.java b/test-complete/src/test/java/com/marklogic/javaclient/TestRawStructuredQuery.java index 57a23b3fa..c894ea24f 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestRawStructuredQuery.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestRawStructuredQuery.java @@ -1,556 +1,548 @@ -package com.marklogic.javaclient; - -import java.io.File; -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.query.RawCombinedQueryDefinition; -import com.marklogic.client.query.RawStructuredQueryDefinition; - -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.admin.ServerConfigurationManager; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.DocumentMetadataHandle; -import com.marklogic.client.io.FileHandle; -import com.marklogic.client.io.Format; -import com.marklogic.client.io.StringHandle; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; -import static org.junit.Assert.*; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.junit.*; -public class TestRawStructuredQuery extends BasicJavaClientREST { - - - private static String dbName = "TestRawStructuredQueryDB"; - private static String [] fNames = {"TestRawStructuredQueryDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort = 8011; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - System.out.println("after setup"); - } - -@After -public void testCleanUp() throws Exception -{ - clearDB(restPort); - System.out.println("Running clear script"); -} - -@Test public void testRawStructuredQueryXML() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawStructuredQueryXML"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/raw-combined-query/", "XML"); - } - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOption.xml"); - - // create a handle for the search criteria - FileHandle rawHandle = new FileHandle(file); - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawStructuredQueryDefinition querydef = queryMgr.newRawStructuredQueryDefinition(rawHandle); - - // create result handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - - - -@Test public void testRawStructuredQueryJSON() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawStructuredQueryJSON"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/raw-combined-query/", "XML"); - } - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionJSON.json"); - - String combinedQuery = convertFileToString(file); - - // create a handle for the search criteria - StringHandle rawHandle = new StringHandle(combinedQuery); - //FileHandle rawHandle = new FileHandle(file); - //rawHandle.setMimetype("application/xml"); - rawHandle.setFormat(Format.JSON); - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawStructuredQueryDefinition querydef = queryMgr.newRawStructuredQueryDefinition(rawHandle); - - // create result handle - StringHandle resultsHandle = new StringHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - String resultDoc = resultsHandle.get(); - - System.out.println(resultDoc); - - assertTrue("document is not returned", resultDoc.contains("/raw-combined-query/constraint5.xml")); - - // release client - client.release(); - } - - - -@Test public void testRawStructuredQueryXMLWithOptions() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawStructuredQueryXMLWithOptions"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "valueConstraintWithoutIndexSettingsAndNSOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/raw-combined-query/", "XML"); - } - - setQueryOption(client, queryOptionName); - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryNoOption.xml"); - - // create a handle for the search criteria - FileHandle rawHandle = new FileHandle(file); - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawStructuredQueryDefinition querydef = queryMgr.newRawStructuredQueryDefinition(rawHandle, queryOptionName); - - // create result handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - - - -@Test public void testRawStructuredQueryJSONWithOverwriteOptions() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawStructuredQueryJSONWithOverwriteOptions"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "valueConstraintWithoutIndexSettingsAndNSOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/raw-combined-query/", "XML"); - } - - setQueryOption(client, queryOptionName); - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionJSONOverwrite.json"); - - String combinedQuery = convertFileToString(file); - - // create a handle for the search criteria - StringHandle rawHandle = new StringHandle(combinedQuery); - //FileHandle rawHandle = new FileHandle(file); - //rawHandle.setMimetype("application/xml"); - rawHandle.setFormat(Format.JSON); - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawStructuredQueryDefinition querydef = queryMgr.newRawStructuredQueryDefinition(rawHandle, queryOptionName); - - // create result handle - StringHandle resultsHandle = new StringHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - String resultDoc = resultsHandle.get(); - - System.out.println(resultDoc); - - assertTrue("document is not returned", resultDoc.contains("/raw-combined-query/constraint1.xml")); - - // release client - client.release(); - } - - - -@Test public void testRawStructuredQueryCollection() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawStructuredQueryCollection"); - - String filename1 = "constraint1.xml"; - String filename2 = "constraint2.xml"; - String filename3 = "constraint3.xml"; - String filename4 = "constraint4.xml"; - String filename5 = "constraint5.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create and initialize a handle on the metadata - DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle4 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle5 = new DocumentMetadataHandle(); - - // set the metadata - metadataHandle1.getCollections().addAll("http://test.com/set1"); - metadataHandle1.getCollections().addAll("http://test.com/set5"); - metadataHandle2.getCollections().addAll("http://test.com/set1"); - metadataHandle3.getCollections().addAll("http://test.com/set3"); - metadataHandle4.getCollections().addAll("http://test.com/set3/set3-1"); - metadataHandle5.getCollections().addAll("http://test.com/set1"); - metadataHandle5.getCollections().addAll("http://test.com/set5"); - - // write docs - writeDocumentUsingInputStreamHandle(client, filename1, "/collection-constraint/", metadataHandle1, "XML"); - writeDocumentUsingInputStreamHandle(client, filename2, "/collection-constraint/", metadataHandle2, "XML"); - writeDocumentUsingInputStreamHandle(client, filename3, "/collection-constraint/", metadataHandle3, "XML"); - writeDocumentUsingInputStreamHandle(client, filename4, "/collection-constraint/", metadataHandle4, "XML"); - writeDocumentUsingInputStreamHandle(client, filename5, "/collection-constraint/", metadataHandle5, "XML"); - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionCollection.xml"); - - // create a handle for the search criteria - FileHandle rawHandle = new FileHandle(file); // bug 21107 - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawStructuredQueryDefinition querydef = queryMgr.newRawStructuredQueryDefinition(rawHandle); - - // create result handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("Vannevar Bush", "string(//*[local-name()='result'][1]//*[local-name()='title'])", resultDoc); - - // release client - client.release(); - } - - - - -@Test public void testRawStructuredQueryCombo() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawStructuredQueryCombo"); - - String filename1 = "constraint1.xml"; - String filename2 = "constraint2.xml"; - String filename3 = "constraint3.xml"; - String filename4 = "constraint4.xml"; - String filename5 = "constraint5.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create and initialize a handle on the metadata - DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle4 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle5 = new DocumentMetadataHandle(); - - // set the metadata - metadataHandle1.getCollections().addAll("http://test.com/set1"); - metadataHandle1.getCollections().addAll("http://test.com/set5"); - metadataHandle2.getCollections().addAll("http://test.com/set1"); - metadataHandle3.getCollections().addAll("http://test.com/set3"); - metadataHandle4.getCollections().addAll("http://test.com/set3/set3-1"); - metadataHandle5.getCollections().addAll("http://test.com/set1"); - metadataHandle5.getCollections().addAll("http://test.com/set5"); - - // write docs - writeDocumentUsingInputStreamHandle(client, filename1, "/collection-constraint/", metadataHandle1, "XML"); - writeDocumentUsingInputStreamHandle(client, filename2, "/collection-constraint/", metadataHandle2, "XML"); - writeDocumentUsingInputStreamHandle(client, filename3, "/collection-constraint/", metadataHandle3, "XML"); - writeDocumentUsingInputStreamHandle(client, filename4, "/collection-constraint/", metadataHandle4, "XML"); - writeDocumentUsingInputStreamHandle(client, filename5, "/collection-constraint/", metadataHandle5, "XML"); - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionCombo.xml"); - - // create a handle for the search criteria - FileHandle rawHandle = new FileHandle(file); // bug 21107 - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawStructuredQueryDefinition querydef = queryMgr.newRawStructuredQueryDefinition(rawHandle); - - // create result handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("Vannevar Bush", "string(//*[local-name()='result'][1]//*[local-name()='title'])", resultDoc); - - // release client - client.release(); - } - - - -@Test public void testRawStructuredQueryField() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawStructuredQueryField"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/field-constraint/", "XML"); - } - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionField.xml"); - - // create a handle for the search criteria - FileHandle rawHandle = new FileHandle(file); // bug 21107 - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawStructuredQueryDefinition querydef = queryMgr.newRawStructuredQueryDefinition(rawHandle); - - // create result handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("memex", "string(//*[local-name()='result'][1]//*[local-name()='match'][1]//*[local-name()='highlight'])", resultDoc); - assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='match'][2]//*[local-name()='highlight'])", resultDoc); - assertXpathEvaluatesTo("Memex", "string(//*[local-name()='result'][1]//*[local-name()='match'][3]//*[local-name()='highlight'])", resultDoc); - assertXpathEvaluatesTo("Bush", "string(//*[local-name()='result'][2]//*[local-name()='match'][1]//*[local-name()='highlight'])", resultDoc); - - // release client - client.release(); - } - - - -@Test public void testRawStructuredQueryPathIndex() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawStructuredQueryPathIndex"); - - String[] filenames = {"pathindex1.xml", "pathindex2.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/path-index-raw/", "XML"); - } - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionPathIndex.xml"); - - // create a handle for the search criteria - FileHandle rawHandle = new FileHandle(file); // bug 21107 - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawStructuredQueryDefinition querydef = queryMgr.newRawStructuredQueryDefinition(rawHandle); - - // create result handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("/path-index-raw/pathindex2.xml", "string(//*[local-name()='result'][1]//@*[local-name()='uri'])", resultDoc); - assertXpathEvaluatesTo("/path-index-raw/pathindex1.xml", "string(//*[local-name()='result'][2]//@*[local-name()='uri'])", resultDoc); - - // release client - client.release(); - } - - /*public void testRawStructuredQueryComboJSON() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testRawStructuredQueryComboJSON"); - - String filename1 = "constraint1.xml"; - String filename2 = "constraint2.xml"; - String filename3 = "constraint3.xml"; - String filename4 = "constraint4.xml"; - String filename5 = "constraint5.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create and initialize a handle on the metadata - DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle4 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle5 = new DocumentMetadataHandle(); - - // set the metadata - metadataHandle1.getCollections().addAll("http://test.com/set1"); - metadataHandle1.getCollections().addAll("http://test.com/set5"); - metadataHandle2.getCollections().addAll("http://test.com/set1"); - metadataHandle3.getCollections().addAll("http://test.com/set3"); - metadataHandle4.getCollections().addAll("http://test.com/set3/set3-1"); - metadataHandle5.getCollections().addAll("http://test.com/set1"); - metadataHandle5.getCollections().addAll("http://test.com/set5"); - - // write docs - writeDocumentUsingInputStreamHandle(client, filename1, "/collection-constraint/", metadataHandle1, "XML"); - writeDocumentUsingInputStreamHandle(client, filename2, "/collection-constraint/", metadataHandle2, "XML"); - writeDocumentUsingInputStreamHandle(client, filename3, "/collection-constraint/", metadataHandle3, "XML"); - writeDocumentUsingInputStreamHandle(client, filename4, "/collection-constraint/", metadataHandle4, "XML"); - writeDocumentUsingInputStreamHandle(client, filename5, "/collection-constraint/", metadataHandle5, "XML"); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionComboJSON.json"); - - String combinedQuery = convertFileToString(file); - - // create a handle for the search criteria - StringHandle rawHandle = new StringHandle(combinedQuery); - rawHandle.setFormat(Format.JSON); - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); - - // create result handle - StringHandle resultsHandle = new StringHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - String resultDoc = resultsHandle.get(); - - System.out.println(resultDoc); - - //assertTrue("document is not returned", resultDoc.contains("/raw-combined-query/constraint5.xml")); - - // release client - client.release(); - }*/ - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames,restServerName); - } -} +package com.marklogic.javaclient; + +import java.io.File; +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.RawStructuredQueryDefinition; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.admin.ServerConfigurationManager; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.DocumentMetadataHandle; +import com.marklogic.client.io.FileHandle; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.StringHandle; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; +import static org.junit.Assert.*; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.*; + +public class TestRawStructuredQuery extends BasicJavaClientREST { + private static String dbName = "TestRawStructuredQueryDB"; + private static String [] fNames = {"TestRawStructuredQueryDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + + @BeforeClass public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + System.out.println("after setup"); + } + + @After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + + @Test + public void testRawStructuredQueryXML() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawStructuredQueryXML"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/raw-combined-query/", "XML"); + } + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOption.xml"); + + // create a handle for the search criteria + FileHandle rawHandle = new FileHandle(file); + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawStructuredQueryDefinition querydef = queryMgr.newRawStructuredQueryDefinition(rawHandle); + + // create result handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testRawStructuredQueryJSON() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawStructuredQueryJSON"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/raw-combined-query/", "XML"); + } + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionJSON.json"); + + String combinedQuery = convertFileToString(file); + + // create a handle for the search criteria + StringHandle rawHandle = new StringHandle(combinedQuery); + //FileHandle rawHandle = new FileHandle(file); + //rawHandle.setMimetype("application/xml"); + rawHandle.setFormat(Format.JSON); + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawStructuredQueryDefinition querydef = queryMgr.newRawStructuredQueryDefinition(rawHandle); + + // create result handle + StringHandle resultsHandle = new StringHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + String resultDoc = resultsHandle.get(); + + System.out.println(resultDoc); + + assertTrue("document is not returned", resultDoc.contains("/raw-combined-query/constraint5.xml")); + + // release client + client.release(); + } + + @Test + public void testRawStructuredQueryXMLWithOptions() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawStructuredQueryXMLWithOptions"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "valueConstraintWithoutIndexSettingsAndNSOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/raw-combined-query/", "XML"); + } + + setQueryOption(client, queryOptionName); + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryNoOption.xml"); + + // create a handle for the search criteria + FileHandle rawHandle = new FileHandle(file); + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawStructuredQueryDefinition querydef = queryMgr.newRawStructuredQueryDefinition(rawHandle, queryOptionName); + + // create result handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testRawStructuredQueryJSONWithOverwriteOptions() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawStructuredQueryJSONWithOverwriteOptions"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "valueConstraintWithoutIndexSettingsAndNSOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/raw-combined-query/", "XML"); + } + + setQueryOption(client, queryOptionName); + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionJSONOverwrite.json"); + + String combinedQuery = convertFileToString(file); + + // create a handle for the search criteria + StringHandle rawHandle = new StringHandle(combinedQuery); + //FileHandle rawHandle = new FileHandle(file); + //rawHandle.setMimetype("application/xml"); + rawHandle.setFormat(Format.JSON); + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawStructuredQueryDefinition querydef = queryMgr.newRawStructuredQueryDefinition(rawHandle, queryOptionName); + + // create result handle + StringHandle resultsHandle = new StringHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + String resultDoc = resultsHandle.get(); + + System.out.println(resultDoc); + + assertTrue("document is not returned", resultDoc.contains("/raw-combined-query/constraint1.xml")); + + // release client + client.release(); + } + + @Test + public void testRawStructuredQueryCollection() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawStructuredQueryCollection"); + + String filename1 = "constraint1.xml"; + String filename2 = "constraint2.xml"; + String filename3 = "constraint3.xml"; + String filename4 = "constraint4.xml"; + String filename5 = "constraint5.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle4 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle5 = new DocumentMetadataHandle(); + + // set the metadata + metadataHandle1.getCollections().addAll("http://test.com/set1"); + metadataHandle1.getCollections().addAll("http://test.com/set5"); + metadataHandle2.getCollections().addAll("http://test.com/set1"); + metadataHandle3.getCollections().addAll("http://test.com/set3"); + metadataHandle4.getCollections().addAll("http://test.com/set3/set3-1"); + metadataHandle5.getCollections().addAll("http://test.com/set1"); + metadataHandle5.getCollections().addAll("http://test.com/set5"); + + // write docs + writeDocumentUsingInputStreamHandle(client, filename1, "/collection-constraint/", metadataHandle1, "XML"); + writeDocumentUsingInputStreamHandle(client, filename2, "/collection-constraint/", metadataHandle2, "XML"); + writeDocumentUsingInputStreamHandle(client, filename3, "/collection-constraint/", metadataHandle3, "XML"); + writeDocumentUsingInputStreamHandle(client, filename4, "/collection-constraint/", metadataHandle4, "XML"); + writeDocumentUsingInputStreamHandle(client, filename5, "/collection-constraint/", metadataHandle5, "XML"); + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionCollection.xml"); + + // create a handle for the search criteria + FileHandle rawHandle = new FileHandle(file); // bug 21107 + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawStructuredQueryDefinition querydef = queryMgr.newRawStructuredQueryDefinition(rawHandle); + + // create result handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("Vannevar Bush", "string(//*[local-name()='result'][1]//*[local-name()='title'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testRawStructuredQueryCombo() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawStructuredQueryCombo"); + + String filename1 = "constraint1.xml"; + String filename2 = "constraint2.xml"; + String filename3 = "constraint3.xml"; + String filename4 = "constraint4.xml"; + String filename5 = "constraint5.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle4 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle5 = new DocumentMetadataHandle(); + + // set the metadata + metadataHandle1.getCollections().addAll("http://test.com/set1"); + metadataHandle1.getCollections().addAll("http://test.com/set5"); + metadataHandle2.getCollections().addAll("http://test.com/set1"); + metadataHandle3.getCollections().addAll("http://test.com/set3"); + metadataHandle4.getCollections().addAll("http://test.com/set3/set3-1"); + metadataHandle5.getCollections().addAll("http://test.com/set1"); + metadataHandle5.getCollections().addAll("http://test.com/set5"); + + // write docs + writeDocumentUsingInputStreamHandle(client, filename1, "/collection-constraint/", metadataHandle1, "XML"); + writeDocumentUsingInputStreamHandle(client, filename2, "/collection-constraint/", metadataHandle2, "XML"); + writeDocumentUsingInputStreamHandle(client, filename3, "/collection-constraint/", metadataHandle3, "XML"); + writeDocumentUsingInputStreamHandle(client, filename4, "/collection-constraint/", metadataHandle4, "XML"); + writeDocumentUsingInputStreamHandle(client, filename5, "/collection-constraint/", metadataHandle5, "XML"); + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionCombo.xml"); + + // create a handle for the search criteria + FileHandle rawHandle = new FileHandle(file); // bug 21107 + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawStructuredQueryDefinition querydef = queryMgr.newRawStructuredQueryDefinition(rawHandle); + + // create result handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("Vannevar Bush", "string(//*[local-name()='result'][1]//*[local-name()='title'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testRawStructuredQueryField() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawStructuredQueryField"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/field-constraint/", "XML"); + } + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionField.xml"); + + // create a handle for the search criteria + FileHandle rawHandle = new FileHandle(file); // bug 21107 + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawStructuredQueryDefinition querydef = queryMgr.newRawStructuredQueryDefinition(rawHandle); + + // create result handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("memex", "string(//*[local-name()='result'][1]//*[local-name()='match'][1]//*[local-name()='highlight'])", resultDoc); + assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='match'][2]//*[local-name()='highlight'])", resultDoc); + assertXpathEvaluatesTo("Memex", "string(//*[local-name()='result'][1]//*[local-name()='match'][3]//*[local-name()='highlight'])", resultDoc); + assertXpathEvaluatesTo("Bush", "string(//*[local-name()='result'][2]//*[local-name()='match'][1]//*[local-name()='highlight'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testRawStructuredQueryPathIndex() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawStructuredQueryPathIndex"); + + String[] filenames = {"pathindex1.xml", "pathindex2.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/path-index-raw/", "XML"); + } + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionPathIndex.xml"); + + // create a handle for the search criteria + FileHandle rawHandle = new FileHandle(file); // bug 21107 + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawStructuredQueryDefinition querydef = queryMgr.newRawStructuredQueryDefinition(rawHandle); + + // create result handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("/path-index-raw/pathindex2.xml", "string(//*[local-name()='result'][1]//@*[local-name()='uri'])", resultDoc); + assertXpathEvaluatesTo("/path-index-raw/pathindex1.xml", "string(//*[local-name()='result'][2]//@*[local-name()='uri'])", resultDoc); + + // release client + client.release(); + } + + /*public void testRawStructuredQueryComboJSON() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testRawStructuredQueryComboJSON"); + + String filename1 = "constraint1.xml"; + String filename2 = "constraint2.xml"; + String filename3 = "constraint3.xml"; + String filename4 = "constraint4.xml"; + String filename5 = "constraint5.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle4 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle5 = new DocumentMetadataHandle(); + + // set the metadata + metadataHandle1.getCollections().addAll("http://test.com/set1"); + metadataHandle1.getCollections().addAll("http://test.com/set5"); + metadataHandle2.getCollections().addAll("http://test.com/set1"); + metadataHandle3.getCollections().addAll("http://test.com/set3"); + metadataHandle4.getCollections().addAll("http://test.com/set3/set3-1"); + metadataHandle5.getCollections().addAll("http://test.com/set1"); + metadataHandle5.getCollections().addAll("http://test.com/set5"); + + // write docs + writeDocumentUsingInputStreamHandle(client, filename1, "/collection-constraint/", metadataHandle1, "XML"); + writeDocumentUsingInputStreamHandle(client, filename2, "/collection-constraint/", metadataHandle2, "XML"); + writeDocumentUsingInputStreamHandle(client, filename3, "/collection-constraint/", metadataHandle3, "XML"); + writeDocumentUsingInputStreamHandle(client, filename4, "/collection-constraint/", metadataHandle4, "XML"); + writeDocumentUsingInputStreamHandle(client, filename5, "/collection-constraint/", metadataHandle5, "XML"); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOptionComboJSON.json"); + + String combinedQuery = convertFileToString(file); + + // create a handle for the search criteria + StringHandle rawHandle = new StringHandle(combinedQuery); + rawHandle.setFormat(Format.JSON); + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); + + // create result handle + StringHandle resultsHandle = new StringHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + String resultDoc = resultsHandle.get(); + + System.out.println(resultDoc); + + //assertTrue("document is not returned", resultDoc.contains("/raw-combined-query/constraint5.xml")); + + // release client + client.release(); + }*/ + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames,restServerName); + } +} From 0b4a3b385dfebd777990061f469be304e3f56eb9 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Thu, 21 Aug 2014 07:51:51 -0700 Subject: [PATCH 082/357] Code cleanup and formatting changes --- .../javaclient/TestReaderHandle.java | 633 ++++++------ .../javaclient/TestRequestLogger.java | 167 ++-- .../javaclient/TestResponseTransform.java | 400 ++++---- .../javaclient/TestRollbackTransaction.java | 629 ++++++------ .../javaclient/TestSSLConnection.java | 769 +++++++------- .../javaclient/TestSearchMultibyte.java | 329 +++--- .../javaclient/TestSearchMultipleForests.java | 160 +-- .../javaclient/TestSearchOnJSON.java | 396 ++++---- .../javaclient/TestSearchOnProperties.java | 663 ++++++------ .../javaclient/TestSearchOptions.java | 615 ++++++------ .../javaclient/TestSearchSuggestion.java | 616 ++++++------ .../TestServerAssignedDocumentURI.java | 271 +++-- .../javaclient/TestSourceHandle.java | 225 +++-- .../javaclient/TestStandaloneGeoQuery.java | 941 +++++++++--------- 14 files changed, 3364 insertions(+), 3450 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestReaderHandle.java b/test-complete/src/test/java/com/marklogic/javaclient/TestReaderHandle.java index a469cebc4..021a15382 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestReaderHandle.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestReaderHandle.java @@ -1,325 +1,308 @@ -package com.marklogic.javaclient; - -import java.io.*; - -import javax.xml.parsers.ParserConfigurationException; - -import org.custommonkey.xmlunit.XMLUnit; -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.ReaderHandle; - -import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; -import static org.junit.Assert.*; - -import org.junit.*; -public class TestReaderHandle extends BasicJavaClientREST { - - private static String dbName = "WriteReaderHandleDB"; - private static String [] fNames = {"WriteReaderHandleDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - } - - -@SuppressWarnings("deprecation") -@Test public void testXmlCRUD() throws FileNotFoundException, IOException, SAXException, ParserConfigurationException - { - System.out.println("Running testXmlCRUD"); - - String filename = "xml-original-test.xml"; - String uri = "/write-xml-readerhandle/"; - - XMLUnit.setIgnoreWhitespace(true); - XMLUnit.setNormalizeWhitespace(true); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write the doc - writeDocumentReaderHandle(client, filename, uri, "XML"); - - // read the document - ReaderHandle readHandle = readDocumentReaderHandle(client, uri + filename, "XML"); - - // access the document content - Reader fileRead = readHandle.get(); - - String readContent = convertReaderToString(fileRead); - - // get xml document for expected result - Document expectedDoc = expectedXMLDocument(filename); - - // convert actual string to xml doc - Document readDoc = convertStringToXMLDocument(readContent); - - assertXMLEqual("Write XML difference", expectedDoc, readDoc); - - // update the doc - // acquire the content for update - String updateFilename = "xml-updated-test.xml"; - updateDocumentReaderHandle(client, updateFilename, uri + filename, "XML"); - - // read the document - ReaderHandle updateHandle = readDocumentReaderHandle(client, uri + filename, "XML"); - - // access the document content - Reader fileReadUpdate = updateHandle.get(); - - String readContentUpdate = convertReaderToString(fileReadUpdate); - - // get xml document for expected result - Document expectedDocUpdate = expectedXMLDocument(updateFilename); - - // convert actual string to xml doc - Document readDocUpdate = convertStringToXMLDocument(readContentUpdate); - - assertXMLEqual("Write XML difference", expectedDocUpdate, readDocUpdate); - - - // delete the document - deleteDocument(client, uri + filename, "XML"); - - // read the deleted document - //assertFalse("Document is not deleted", isDocumentExist(client, "/write-xml-readerhandle/" + filename, "XML")); - - String exception = ""; - try - { - ReaderHandle deleteHandle = readDocumentReaderHandle(client, uri + filename, "XML"); - } catch (Exception e) { exception = e.toString(); } - - String expectedException = "Could not read non-existent document"; - boolean documentIsDeleted = exception.contains(expectedException); - assertTrue("Document is not deleted", documentIsDeleted); - - - - // release the client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testTextCRUD() throws FileNotFoundException, IOException - { - System.out.println("Running testTextCRUD"); - - String filename = "text-original.txt"; - String uri = "/write-text-readerhandle/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write the doc - writeDocumentReaderHandle(client, filename, uri, "Text"); - - // read the document - ReaderHandle readHandle = readDocumentReaderHandle(client, uri + filename, "Text"); - - // access the document content - Reader fileRead = readHandle.get(); - - String expectedContent = "hello world, welcome to java API"; - - String readContent = convertReaderToString(fileRead); - - assertEquals("Write Text document difference", expectedContent, readContent); - - // update the doc - // acquire the content for update - String updateFilename = "text-updated.txt"; - updateDocumentReaderHandle(client, updateFilename, uri + filename, "Text"); - - // read the document - ReaderHandle updateHandle = readDocumentReaderHandle(client, uri + filename, "Text"); - - // access the document content - Reader fileReadUpdate = updateHandle.get(); - - String readContentUpdate = convertReaderToString(fileReadUpdate); - - String expectedContentUpdate = "hello world, welcome to java API after new updates"; - - assertEquals("Update Text document difference", expectedContentUpdate, readContentUpdate); - - // delete the document - deleteDocument(client, uri + filename, "Text"); - - // read the deleted document - // assertFalse("Document is not deleted", isDocumentExist(client, "/write-text-readerhandle/" + filename, "Text")); - - String exception = ""; - try - { - ReaderHandle deleteHandle = readDocumentReaderHandle(client, uri + filename, "Text"); - } catch (Exception e) { exception = e.toString(); } - - String expectedException = "Could not read non-existent document"; - boolean documentIsDeleted = exception.contains(expectedException); - assertTrue("Document is not deleted", documentIsDeleted); - - - // release the client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testJsonCRUD() throws FileNotFoundException, IOException - { - System.out.println("Running testJsonCRUD"); - - String filename = "json-original.json"; - String uri = "/write-json-readerhandle/"; - - ObjectMapper mapper = new ObjectMapper(); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write the doc - writeDocumentReaderHandle(client, filename, uri, "JSON"); - - // read the document - ReaderHandle readHandle = readDocumentReaderHandle(client, uri + filename, "JSON"); - - // access the document content - Reader fileRead = readHandle.get(); - JsonNode readContent = mapper.readTree(fileRead); - - // get expected contents - JsonNode expectedContent = expectedJSONDocument(filename); - - assertTrue("Write JSON document difference", readContent.equals(expectedContent)); - - //assertEquals("Write JSON document difference", expectedContent, readContent); - - // update the doc - // acquire the content for update - String updateFilename = "json-updated.json"; - updateDocumentReaderHandle(client, updateFilename, uri + filename, "JSON"); - - // read the document - ReaderHandle updateHandle = readDocumentReaderHandle(client, uri + filename, "JSON"); - - // access the document content - Reader fileReadUpdate = updateHandle.get(); - JsonNode readContentUpdate = mapper.readTree(fileReadUpdate); - - // get expected contents - JsonNode expectedContentUpdate = expectedJSONDocument(updateFilename); - - assertTrue("Write JSON document difference", readContentUpdate.equals(expectedContentUpdate)); - - // delete the document - deleteDocument(client, uri + filename, "JSON"); - - // read the deleted document - //assertFalse("Document is not deleted", isDocumentExist(client, "/write-json-readerhandle/" + filename, "JSON")); - - String exception = ""; - try - { - ReaderHandle deleteHandle = readDocumentReaderHandle(client, uri + filename, "JSON"); - } catch (Exception e) { exception = e.toString(); } - - String expectedException = "Could not read non-existent document"; - boolean documentIsDeleted = exception.contains(expectedException); - assertTrue("Document is not deleted", documentIsDeleted); - - // release the client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testBinaryCRUD() throws IOException - { - String filename = "Pandakarlino.jpg"; - String uri = "/write-bin-filehandle/"; - - System.out.println("Running testBinaryCRUD"); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - writeDocumentReaderHandle(client, filename, uri, "Binary"); - - // read docs - ReaderHandle contentHandle = readDocumentReaderHandle(client, uri+filename, "Binary"); - - // get the contents - Reader fileRead = contentHandle.get(); - - // get the binary size - long size = convertReaderToString(fileRead).length(); - //long expectedSize = 17031; - - boolean expectedSize; - if(size >= 16800 && size <= 17200) - { - expectedSize = true; - assertTrue("Binary size difference", expectedSize); - } - - // update the doc - // acquire the content for update - String updateFilename = "mlfavicon.png"; - //String updateFilename = "JenoptikLogo.jpg"; - updateDocumentReaderHandle(client,updateFilename, uri+filename, "Binary"); - - - // read the document - ReaderHandle updateHandle = readDocumentReaderHandle(client, uri+filename, "Binary"); - - - // get the contents - Reader fileReadUpdate = updateHandle.get(); - - // get the binary size - long sizeUpdate = convertReaderToString(fileReadUpdate).length(); - //long expectedSizeUpdate = 56508; - - boolean expectedSizeUpdate; - if(sizeUpdate >= 55000 && sizeUpdate <= 57000) - { - expectedSizeUpdate = true; - assertTrue("Binary size difference", expectedSizeUpdate); - } - - //assertEquals("Binary size difference", expectedSizeUpdate, sizeUpdate); - - // delete the document - deleteDocument(client, uri + filename, "Binary"); - - // read the deleted document - //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "Binary")); - - String exception = ""; - try - { - ReaderHandle deleteHandle = readDocumentReaderHandle(client, uri + filename, "Binary"); - } catch (Exception e) { exception = e.toString(); } - - String expectedException = "com.marklogic.client.ResourceNotFoundException: Local message: Could not read non-existent document. Server Message: RESTAPI-NODOCUMENT: (err:FOER0000) Resource or document does not exist: category: content message: /write-bin-filehandle/Pandakarlino.jpg"; - assertEquals("Document is not deleted", expectedException, exception); - - - // release client - client.release(); - } -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } -} +package com.marklogic.javaclient; + +import java.io.*; + +import javax.xml.parsers.ParserConfigurationException; + +import org.custommonkey.xmlunit.XMLUnit; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.ReaderHandle; + +import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; +import static org.junit.Assert.*; + +import org.junit.*; + +public class TestReaderHandle extends BasicJavaClientREST { + private static String dbName = "WriteReaderHandleDB"; + private static String [] fNames = {"WriteReaderHandleDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + } + + @Test + public void testXmlCRUD() throws FileNotFoundException, IOException, SAXException, ParserConfigurationException + { + System.out.println("Running testXmlCRUD"); + + String filename = "xml-original-test.xml"; + String uri = "/write-xml-readerhandle/"; + + XMLUnit.setIgnoreWhitespace(true); + XMLUnit.setNormalizeWhitespace(true); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write the doc + writeDocumentReaderHandle(client, filename, uri, "XML"); + + // read the document + ReaderHandle readHandle = readDocumentReaderHandle(client, uri + filename, "XML"); + + // access the document content + Reader fileRead = readHandle.get(); + + String readContent = convertReaderToString(fileRead); + + // get xml document for expected result + Document expectedDoc = expectedXMLDocument(filename); + + // convert actual string to xml doc + Document readDoc = convertStringToXMLDocument(readContent); + + assertXMLEqual("Write XML difference", expectedDoc, readDoc); + + // update the doc + // acquire the content for update + String updateFilename = "xml-updated-test.xml"; + updateDocumentReaderHandle(client, updateFilename, uri + filename, "XML"); + + // read the document + ReaderHandle updateHandle = readDocumentReaderHandle(client, uri + filename, "XML"); + + // access the document content + Reader fileReadUpdate = updateHandle.get(); + + String readContentUpdate = convertReaderToString(fileReadUpdate); + + // get xml document for expected result + Document expectedDocUpdate = expectedXMLDocument(updateFilename); + + // convert actual string to xml doc + Document readDocUpdate = convertStringToXMLDocument(readContentUpdate); + + assertXMLEqual("Write XML difference", expectedDocUpdate, readDocUpdate); + + + // delete the document + deleteDocument(client, uri + filename, "XML"); + + // read the deleted document + String exception = ""; + try + { + readDocumentReaderHandle(client, uri + filename, "XML"); + } + catch (Exception e) { exception = e.toString(); } + + String expectedException = "Could not read non-existent document"; + boolean documentIsDeleted = exception.contains(expectedException); + assertTrue("Document is not deleted", documentIsDeleted); + + // release the client + client.release(); + } + + @Test + public void testTextCRUD() throws FileNotFoundException, IOException + { + System.out.println("Running testTextCRUD"); + + String filename = "text-original.txt"; + String uri = "/write-text-readerhandle/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write the doc + writeDocumentReaderHandle(client, filename, uri, "Text"); + + // read the document + ReaderHandle readHandle = readDocumentReaderHandle(client, uri + filename, "Text"); + + // access the document content + Reader fileRead = readHandle.get(); + + String expectedContent = "hello world, welcome to java API"; + + String readContent = convertReaderToString(fileRead); + + assertEquals("Write Text document difference", expectedContent, readContent); + + // update the doc + // acquire the content for update + String updateFilename = "text-updated.txt"; + updateDocumentReaderHandle(client, updateFilename, uri + filename, "Text"); + + // read the document + ReaderHandle updateHandle = readDocumentReaderHandle(client, uri + filename, "Text"); + + // access the document content + Reader fileReadUpdate = updateHandle.get(); + + String readContentUpdate = convertReaderToString(fileReadUpdate); + + String expectedContentUpdate = "hello world, welcome to java API after new updates"; + + assertEquals("Update Text document difference", expectedContentUpdate, readContentUpdate); + + // delete the document + deleteDocument(client, uri + filename, "Text"); + + // read the deleted document + String exception = ""; + try + { + readDocumentReaderHandle(client, uri + filename, "Text"); + } + catch (Exception e) { exception = e.toString(); } + + String expectedException = "Could not read non-existent document"; + boolean documentIsDeleted = exception.contains(expectedException); + assertTrue("Document is not deleted", documentIsDeleted); + + // release the client + client.release(); + } + + @Test + public void testJsonCRUD() throws FileNotFoundException, IOException + { + System.out.println("Running testJsonCRUD"); + + String filename = "json-original.json"; + String uri = "/write-json-readerhandle/"; + + ObjectMapper mapper = new ObjectMapper(); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write the doc + writeDocumentReaderHandle(client, filename, uri, "JSON"); + + // read the document + ReaderHandle readHandle = readDocumentReaderHandle(client, uri + filename, "JSON"); + + // access the document content + Reader fileRead = readHandle.get(); + JsonNode readContent = mapper.readTree(fileRead); + + // get expected contents + JsonNode expectedContent = expectedJSONDocument(filename); + + assertTrue("Write JSON document difference", readContent.equals(expectedContent)); + + // update the doc + // acquire the content for update + String updateFilename = "json-updated.json"; + updateDocumentReaderHandle(client, updateFilename, uri + filename, "JSON"); + + // read the document + ReaderHandle updateHandle = readDocumentReaderHandle(client, uri + filename, "JSON"); + + // access the document content + Reader fileReadUpdate = updateHandle.get(); + JsonNode readContentUpdate = mapper.readTree(fileReadUpdate); + + // get expected contents + JsonNode expectedContentUpdate = expectedJSONDocument(updateFilename); + + assertTrue("Write JSON document difference", readContentUpdate.equals(expectedContentUpdate)); + + // delete the document + deleteDocument(client, uri + filename, "JSON"); + + // read the deleted document + String exception = ""; + try + { + readDocumentReaderHandle(client, uri + filename, "JSON"); + } + catch (Exception e) { exception = e.toString(); } + + String expectedException = "Could not read non-existent document"; + boolean documentIsDeleted = exception.contains(expectedException); + assertTrue("Document is not deleted", documentIsDeleted); + + // release the client + client.release(); + } + + @Test + public void testBinaryCRUD() throws IOException + { + String filename = "Pandakarlino.jpg"; + String uri = "/write-bin-filehandle/"; + + System.out.println("Running testBinaryCRUD"); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + writeDocumentReaderHandle(client, filename, uri, "Binary"); + + // read docs + ReaderHandle contentHandle = readDocumentReaderHandle(client, uri+filename, "Binary"); + + // get the contents + Reader fileRead = contentHandle.get(); + + // get the binary size + long size = convertReaderToString(fileRead).length(); + //long expectedSize = 17031; + + boolean expectedSize; + if(size >= 16800 && size <= 17200) + { + expectedSize = true; + assertTrue("Binary size difference", expectedSize); + } + + // update the doc + // acquire the content for update + String updateFilename = "mlfavicon.png"; + updateDocumentReaderHandle(client,updateFilename, uri+filename, "Binary"); + + // read the document + ReaderHandle updateHandle = readDocumentReaderHandle(client, uri+filename, "Binary"); + + // get the contents + Reader fileReadUpdate = updateHandle.get(); + + // get the binary size + long sizeUpdate = convertReaderToString(fileReadUpdate).length(); + + boolean expectedSizeUpdate; + if(sizeUpdate >= 55000 && sizeUpdate <= 57000) + { + expectedSizeUpdate = true; + assertTrue("Binary size difference", expectedSizeUpdate); + } + + // delete the document + deleteDocument(client, uri + filename, "Binary"); + + // read the deleted document + String exception = ""; + try + { + readDocumentReaderHandle(client, uri + filename, "Binary"); + } + catch (Exception e) { exception = e.toString(); } + + String expectedException = "com.marklogic.client.ResourceNotFoundException: Local message: Could not read non-existent document. Server Message: RESTAPI-NODOCUMENT: (err:FOER0000) Resource or document does not exist: category: content message: /write-bin-filehandle/Pandakarlino.jpg"; + assertEquals("Document is not deleted", expectedException, exception); + + // release client + client.release(); + } + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestRequestLogger.java b/test-complete/src/test/java/com/marklogic/javaclient/TestRequestLogger.java index 578a75095..f1018be1a 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestRequestLogger.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestRequestLogger.java @@ -1,83 +1,84 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.File; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.util.RequestLogger; -import com.marklogic.client.Transaction; -import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.io.FileHandle; -import org.junit.*; -public class TestRequestLogger extends BasicJavaClientREST { - - private static String dbName = "TestRequestLoggerDB"; - private static String [] fNames = {"TestRequestLoggerDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - } - - -@SuppressWarnings("deprecation") -@Test public void testRequestLogger() - { - System.out.println("testRequestLogger"); - - String filename = "bbq1.xml"; - String uri = "/request-logger/"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create transaction - Transaction transaction = client.openTransaction(); - - // create a manager for XML documents - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - - // create an identifier for the document - String docId = uri + filename; - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - // create logger - RequestLogger logger = client.newLogger(System.out); - logger.setContentMax(RequestLogger.ALL_CONTENT); - - // start logging - docMgr.startLogging(logger); - - // write the document content - docMgr.write(docId, handle, transaction); - - // commit transaction - transaction.commit(); - - // stop logging - docMgr.stopLogging(); - - String expectedContentMax = "9223372036854775807"; - assertEquals("Content log is not equal", expectedContentMax, Long.toString(logger.getContentMax())); - - // release client - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.File; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.util.RequestLogger; +import com.marklogic.client.Transaction; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.io.FileHandle; +import org.junit.*; +public class TestRequestLogger extends BasicJavaClientREST { + + private static String dbName = "TestRequestLoggerDB"; + private static String [] fNames = {"TestRequestLoggerDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + } + + @Test + public void testRequestLogger() + { + System.out.println("testRequestLogger"); + + String filename = "bbq1.xml"; + String uri = "/request-logger/"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create transaction + Transaction transaction = client.openTransaction(); + + // create a manager for XML documents + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + + // create an identifier for the document + String docId = uri + filename; + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + // create logger + RequestLogger logger = client.newLogger(System.out); + logger.setContentMax(RequestLogger.ALL_CONTENT); + + // start logging + docMgr.startLogging(logger); + + // write the document content + docMgr.write(docId, handle, transaction); + + // commit transaction + transaction.commit(); + + // stop logging + docMgr.stopLogging(); + + String expectedContentMax = "9223372036854775807"; + assertEquals("Content log is not equal", expectedContentMax, Long.toString(logger.getContentMax())); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestResponseTransform.java b/test-complete/src/test/java/com/marklogic/javaclient/TestResponseTransform.java index de3d1d1f0..d25fc62a4 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestResponseTransform.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestResponseTransform.java @@ -1,200 +1,200 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.File; -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.query.RawCombinedQueryDefinition; - -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.admin.ExtensionMetadata; -import com.marklogic.client.admin.ServerConfigurationManager; -import com.marklogic.client.admin.TransformExtensionsManager; -import com.marklogic.client.document.ServerTransform; -import com.marklogic.client.io.FileHandle; -import com.marklogic.client.io.StringHandle; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.junit.*; -public class TestResponseTransform extends BasicJavaClientREST { - - private static String dbName = "TestResponseTransformDB"; - private static String [] fNames = {"TestResponseTransformDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - } - - -@SuppressWarnings("deprecation") -@Test public void testResponseTransform() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testResponseTransform"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/response-transform/", "XML"); - } - - // set the transform - // create a manager for transform extensions - TransformExtensionsManager transMgr = client.newServerConfigManager().newTransformExtensionsManager(); - - // specify metadata about the transform extension - ExtensionMetadata metadata = new ExtensionMetadata(); - metadata.setTitle("Search-Response-TO-HTML XSLT Transform"); - metadata.setDescription("This plugin transforms a Search Response document to HTML"); - metadata.setProvider("MarkLogic"); - metadata.setVersion("0.1"); - - // get the transform file - File transformFile = new File("src/test/java/com/marklogic/javaclient/transforms/search2html.xsl"); - - FileHandle transformHandle = new FileHandle(transformFile); - - // write the transform - transMgr.writeXSLTransform("search2html", transformHandle, metadata); - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOption.xml"); - - String combinedQuery = convertFileToString(file); - - // create a handle for the search criteria - StringHandle rawHandle = new StringHandle(combinedQuery); - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); - querydef.setResponseTransform(new ServerTransform("search2html")); - - // create result handle - StringHandle resultsHandle = new StringHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - String resultDoc = resultsHandle.get(); - - System.out.println(resultDoc); - - assertTrue("transform on title is not found", resultDoc.contains("Custom Search Results")); - assertTrue("transform on header is not found", resultDoc.contains("MyURI")); - assertTrue("transform on doc return is not found", resultDoc.contains("/response-transform/constraint5.xml")); - - transMgr.deleteTransform("search2html"); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testResponseTransformInvalid() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testResponseTransformInvalid"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/response-transform/", "XML"); - } - - // set the transform - // create a manager for transform extensions - TransformExtensionsManager transMgr = client.newServerConfigManager().newTransformExtensionsManager(); - - // specify metadata about the transform extension - ExtensionMetadata metadata = new ExtensionMetadata(); - metadata.setTitle("Search-Response-TO-HTML XSLT Transform"); - metadata.setDescription("This plugin transforms a Search Response document to HTML"); - metadata.setProvider("MarkLogic"); - metadata.setVersion("0.1"); - - // get the transform file - File transformFile = new File("src/test/java/com/marklogic/javaclient/transforms/search2html.xsl"); - - FileHandle transformHandle = new FileHandle(transformFile); - - // write the transform - transMgr.writeXSLTransform("search2html", transformHandle, metadata); - - // get the combined query - File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOption.xml"); - - String combinedQuery = convertFileToString(file); - - // create a handle for the search criteria - StringHandle rawHandle = new StringHandle(combinedQuery); - - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition based on the handle - RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); - querydef.setResponseTransform(new ServerTransform("foo")); - - // create result handle - StringHandle resultsHandle = new StringHandle(); - - String exception = ""; - - try - { - queryMgr.search(querydef, resultsHandle); - } catch(Exception e) - { - exception = e.toString(); - System.out.println(exception); - } - - String expectedException = "Local message: search failed: Bad Request. Server Message: XDMP-MODNOTFOUND: (err:XQST0059) Module /marklogic.rest.transform/foo/assets/transform.xqy not found"; - assertTrue("exception is not thrown", exception.contains(expectedException)); - //bug 22356 - assertTrue("Value should be null", resultsHandle.get()==null); - - transMgr.deleteTransform("search2html"); - - // release client - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames,restServerName); - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.RawCombinedQueryDefinition; + +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.admin.ExtensionMetadata; +import com.marklogic.client.admin.ServerConfigurationManager; +import com.marklogic.client.admin.TransformExtensionsManager; +import com.marklogic.client.document.ServerTransform; +import com.marklogic.client.io.FileHandle; +import com.marklogic.client.io.StringHandle; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.*; +public class TestResponseTransform extends BasicJavaClientREST { + + private static String dbName = "TestResponseTransformDB"; + private static String [] fNames = {"TestResponseTransformDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + } + + @Test + public void testResponseTransform() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testResponseTransform"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/response-transform/", "XML"); + } + + // set the transform + // create a manager for transform extensions + TransformExtensionsManager transMgr = client.newServerConfigManager().newTransformExtensionsManager(); + + // specify metadata about the transform extension + ExtensionMetadata metadata = new ExtensionMetadata(); + metadata.setTitle("Search-Response-TO-HTML XSLT Transform"); + metadata.setDescription("This plugin transforms a Search Response document to HTML"); + metadata.setProvider("MarkLogic"); + metadata.setVersion("0.1"); + + // get the transform file + File transformFile = new File("src/test/java/com/marklogic/javaclient/transforms/search2html.xsl"); + + FileHandle transformHandle = new FileHandle(transformFile); + + // write the transform + transMgr.writeXSLTransform("search2html", transformHandle, metadata); + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOption.xml"); + + String combinedQuery = convertFileToString(file); + + // create a handle for the search criteria + StringHandle rawHandle = new StringHandle(combinedQuery); + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); + querydef.setResponseTransform(new ServerTransform("search2html")); + + // create result handle + StringHandle resultsHandle = new StringHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + String resultDoc = resultsHandle.get(); + + System.out.println(resultDoc); + + assertTrue("transform on title is not found", resultDoc.contains("Custom Search Results")); + assertTrue("transform on header is not found", resultDoc.contains("MyURI")); + assertTrue("transform on doc return is not found", resultDoc.contains("/response-transform/constraint5.xml")); + + transMgr.deleteTransform("search2html"); + + // release client + client.release(); + } + + @Test + public void testResponseTransformInvalid() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testResponseTransformInvalid"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/response-transform/", "XML"); + } + + // set the transform + // create a manager for transform extensions + TransformExtensionsManager transMgr = client.newServerConfigManager().newTransformExtensionsManager(); + + // specify metadata about the transform extension + ExtensionMetadata metadata = new ExtensionMetadata(); + metadata.setTitle("Search-Response-TO-HTML XSLT Transform"); + metadata.setDescription("This plugin transforms a Search Response document to HTML"); + metadata.setProvider("MarkLogic"); + metadata.setVersion("0.1"); + + // get the transform file + File transformFile = new File("src/test/java/com/marklogic/javaclient/transforms/search2html.xsl"); + + FileHandle transformHandle = new FileHandle(transformFile); + + // write the transform + transMgr.writeXSLTransform("search2html", transformHandle, metadata); + + // get the combined query + File file = new File("src/test/java/com/marklogic/javaclient/combined/combinedQueryOption.xml"); + + String combinedQuery = convertFileToString(file); + + // create a handle for the search criteria + StringHandle rawHandle = new StringHandle(combinedQuery); + + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition based on the handle + RawCombinedQueryDefinition querydef = queryMgr.newRawCombinedQueryDefinition(rawHandle); + querydef.setResponseTransform(new ServerTransform("foo")); + + // create result handle + StringHandle resultsHandle = new StringHandle(); + + String exception = ""; + + try + { + queryMgr.search(querydef, resultsHandle); + } catch(Exception e) + { + exception = e.toString(); + System.out.println(exception); + } + + String expectedException = "Local message: search failed: Bad Request. Server Message: XDMP-MODNOTFOUND: (err:XQST0059) Module /marklogic.rest.transform/foo/assets/transform.xqy not found"; + assertTrue("exception is not thrown", exception.contains(expectedException)); + //bug 22356 + assertTrue("Value should be null", resultsHandle.get()==null); + + transMgr.deleteTransform("search2html"); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames,restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestRollbackTransaction.java b/test-complete/src/test/java/com/marklogic/javaclient/TestRollbackTransaction.java index 330fc5eed..918432722 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestRollbackTransaction.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestRollbackTransaction.java @@ -1,327 +1,302 @@ -package com.marklogic.javaclient; - -import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; -import static org.junit.Assert.*; - -import java.io.File; -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.document.DocumentManager; -import com.marklogic.client.io.Format; -import com.marklogic.client.Transaction; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.FileHandle; -import org.junit.*; -public class TestRollbackTransaction extends BasicJavaClientREST { - - private static String dbName = "TestRollbackTransactionDB"; - private static String [] fNames = {"TestRollbackTransactionDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - } - - - -@Test public void testRollbackDeleteDocument() throws ParserConfigurationException, SAXException, IOException - { - System.out.println("testRollbackDeleteDocument"); - - String filename = "bbq1.xml"; - String uri = "/tx-rollback/"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create transaction 1 - Transaction transaction1 = client.openTransaction(); - - // create a manager for document - DocumentManager docMgr = client.newDocumentManager(); - - // create an identifier for the document - String docId = uri + filename; - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - handle.setFormat(Format.XML); - - // write the document content - docMgr.write(docId, handle, transaction1); - - // commit transaction - transaction1.commit(); - - // create transaction 2 - Transaction transaction2 = client.openTransaction(); - - // delete document - docMgr.delete(docId, transaction2); - - // commit transaction - //transaction2.commit(); - - // rollback transaction - transaction2.rollback(); - - // read document - FileHandle readHandle = new FileHandle(); - docMgr.read(docId, readHandle); - File fileRead = readHandle.get(); - String readContent = convertFileToString(fileRead); - - // get xml document for expected result - Document expectedDoc = expectedXMLDocument(filename); - - // convert actual string to xml doc - Document readDoc = convertStringToXMLDocument(readContent); - - assertXMLEqual("Rollback on document delete failed", expectedDoc, readDoc); - - // release client - client.release(); - } - - - -@Test public void testRollbackUpdateDocument() throws ParserConfigurationException, SAXException, IOException - { - System.out.println("testRollbackUpdateDocument"); - - String filename = "json-original.json"; - String updateFilename = "json-updated.json"; - String uri = "/tx-rollback/"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create transaction 1 - Transaction transaction1 = client.openTransaction(); - - // create a manager for document - DocumentManager docMgr = client.newDocumentManager(); - - // create an identifier for the document - String docId = uri + filename; - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - handle.setFormat(Format.JSON); - - // write the document content - docMgr.write(docId, handle, transaction1); - - // commit transaction - transaction1.commit(); - - // create transaction 2 - Transaction transaction2 = client.openTransaction(); - - // update document - File updateFile = new File("src/test/java/com/marklogic/javaclient/data/" + updateFilename); - FileHandle updateHandle = new FileHandle(updateFile); - updateHandle.set(updateFile); - updateHandle.setFormat(Format.JSON); - docMgr.write(docId, updateHandle, transaction2); - - // commit transaction - //transaction2.commit(); - - // rollback transaction - transaction2.rollback(); - - ObjectMapper mapper = new ObjectMapper(); - - // read document - FileHandle readHandle = new FileHandle(); - docMgr.read(docId, readHandle); - File fileRead = readHandle.get(); - JsonNode readContent = mapper.readTree(fileRead); - - // get expected contents - JsonNode expectedContent = expectedJSONDocument(filename); - - assertTrue("Rollback on document update failed", readContent.equals(expectedContent)); - - // release client - client.release(); - } - - - -@Test public void testRollbackMetadata() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testRollbackMetadata"); - - String filename = "Simple_ScanTe.png"; - String uri = "/tx-rollback/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create transaction 1 - Transaction transaction1 = client.openTransaction(); - - // create doc manager - DocumentManager docMgr = client.newDocumentManager(); - - // create an identifier for the document - String docId = uri + filename; - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - handle.setFormat(Format.BINARY); - - // write the document content - docMgr.write(docId, handle, transaction1); - - // get the original metadata - Document docMetadata = getXMLMetadata("metadata-original.xml"); - - // create handle to write metadata - DOMHandle writeMetadataHandle = new DOMHandle(); - writeMetadataHandle.set(docMetadata); - - // write original metadata - docMgr.writeMetadata(docId, writeMetadataHandle, transaction1); - - // commit transaction - transaction1.commit(); - - // create transaction 2 - Transaction transaction2 = client.openTransaction(); - - // get the update metadata - Document docMetadataUpdate = getXMLMetadata("metadata-updated.xml"); - - // create handle for metadata update - DOMHandle writeMetadataHandleUpdate = new DOMHandle(); - writeMetadataHandleUpdate.set(docMetadataUpdate); - - // write updated metadata - docMgr.writeMetadata(docId, writeMetadataHandleUpdate, transaction2); - - // commit transaction2 - //transaction2.commit(); - - // rollback transaction2 - transaction2.rollback(); - - // create handle to read updated metadata - DOMHandle readMetadataHandleUpdate = new DOMHandle(); - - // read updated metadata - docMgr.readMetadata(docId, readMetadataHandleUpdate); - Document docReadMetadataUpdate = readMetadataHandleUpdate.get(); - - assertXpathEvaluatesTo("coll1", "string(//*[local-name()='collection'][1])", docReadMetadataUpdate); - assertXpathEvaluatesTo("coll2", "string(//*[local-name()='collection'][2])", docReadMetadataUpdate); - assertXpathEvaluatesTo("MarkLogic", "string(//*[local-name()='Author'])", docReadMetadataUpdate); - - // release the client - client.release(); - } - - - -@Test public void testNegative() throws ParserConfigurationException, SAXException, IOException - { - System.out.println("testNegative"); - - String filename = "bbq1.xml"; - String uri = "/tx-rollback/"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - // create transaction 1 - Transaction transaction1 = client.openTransaction(); - - // create a manager for document - DocumentManager docMgr = client.newDocumentManager(); - - // create an identifier for the document - String docId = uri + filename; - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - handle.setFormat(Format.XML); - - // write the document content - docMgr.write(docId, handle, transaction1); - - // commit transaction - transaction1.commit(); - - // create transaction 2 - Transaction transaction2 = client.openTransaction(); - - // delete document - docMgr.delete(docId, transaction2); - - // commit transaction - transaction2.commit(); - - String expectedException = "com.marklogic.client.FailedRequestException: Local message: transaction rollback failed: Bad Request. Server Message: XDMP-NOTXN"; - String exception = ""; - - // rollback transaction - try - { - transaction2.rollback(); - } catch(Exception e) { exception = e.toString(); }; - - assertTrue("Exception is not thrown", exception.contains(expectedException)); - - /* - // read document - FileHandle readHandle = new FileHandle(); - docMgr.read(docId, readHandle); - File fileRead = readHandle.get(); - String readContent = convertFileToString(fileRead); - - // get xml document for expected result - Document expectedDoc = expectedXMLDocument(filename); - - // convert actual string to xml doc - Document readDoc = convertStringToXMLDocument(readContent); - - assertXMLEqual("Rollback on document delete failed", expectedDoc, readDoc); - */ - - // release client - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} +package com.marklogic.javaclient; + +import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; +import static org.junit.Assert.*; + +import java.io.File; +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.DocumentManager; +import com.marklogic.client.io.Format; +import com.marklogic.client.Transaction; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.FileHandle; +import org.junit.*; + +public class TestRollbackTransaction extends BasicJavaClientREST { + + private static String dbName = "TestRollbackTransactionDB"; + private static String [] fNames = {"TestRollbackTransactionDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + } + + @Test + public void testRollbackDeleteDocument() throws ParserConfigurationException, SAXException, IOException + { + System.out.println("testRollbackDeleteDocument"); + + String filename = "bbq1.xml"; + String uri = "/tx-rollback/"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create transaction 1 + Transaction transaction1 = client.openTransaction(); + + // create a manager for document + DocumentManager docMgr = client.newDocumentManager(); + + // create an identifier for the document + String docId = uri + filename; + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + handle.setFormat(Format.XML); + + // write the document content + docMgr.write(docId, handle, transaction1); + + // commit transaction + transaction1.commit(); + + // create transaction 2 + Transaction transaction2 = client.openTransaction(); + + // delete document + docMgr.delete(docId, transaction2); + + transaction2.rollback(); + + // read document + FileHandle readHandle = new FileHandle(); + docMgr.read(docId, readHandle); + File fileRead = readHandle.get(); + String readContent = convertFileToString(fileRead); + + // get xml document for expected result + Document expectedDoc = expectedXMLDocument(filename); + + // convert actual string to xml doc + Document readDoc = convertStringToXMLDocument(readContent); + + assertXMLEqual("Rollback on document delete failed", expectedDoc, readDoc); + + // release client + client.release(); + } + + @Test + public void testRollbackUpdateDocument() throws ParserConfigurationException, SAXException, IOException + { + System.out.println("testRollbackUpdateDocument"); + + String filename = "json-original.json"; + String updateFilename = "json-updated.json"; + String uri = "/tx-rollback/"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create transaction 1 + Transaction transaction1 = client.openTransaction(); + + // create a manager for document + DocumentManager docMgr = client.newDocumentManager(); + + // create an identifier for the document + String docId = uri + filename; + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + handle.setFormat(Format.JSON); + + // write the document content + docMgr.write(docId, handle, transaction1); + + // commit transaction + transaction1.commit(); + + // create transaction 2 + Transaction transaction2 = client.openTransaction(); + + // update document + File updateFile = new File("src/test/java/com/marklogic/javaclient/data/" + updateFilename); + FileHandle updateHandle = new FileHandle(updateFile); + updateHandle.set(updateFile); + updateHandle.setFormat(Format.JSON); + docMgr.write(docId, updateHandle, transaction2); + + transaction2.rollback(); + + ObjectMapper mapper = new ObjectMapper(); + + // read document + FileHandle readHandle = new FileHandle(); + docMgr.read(docId, readHandle); + File fileRead = readHandle.get(); + JsonNode readContent = mapper.readTree(fileRead); + + // get expected contents + JsonNode expectedContent = expectedJSONDocument(filename); + + assertTrue("Rollback on document update failed", readContent.equals(expectedContent)); + + // release client + client.release(); + } + + @Test + public void testRollbackMetadata() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testRollbackMetadata"); + + String filename = "Simple_ScanTe.png"; + String uri = "/tx-rollback/"; + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create transaction 1 + Transaction transaction1 = client.openTransaction(); + + // create doc manager + DocumentManager docMgr = client.newDocumentManager(); + + // create an identifier for the document + String docId = uri + filename; + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + handle.setFormat(Format.BINARY); + + // write the document content + docMgr.write(docId, handle, transaction1); + + // get the original metadata + Document docMetadata = getXMLMetadata("metadata-original.xml"); + + // create handle to write metadata + DOMHandle writeMetadataHandle = new DOMHandle(); + writeMetadataHandle.set(docMetadata); + + // write original metadata + docMgr.writeMetadata(docId, writeMetadataHandle, transaction1); + + // commit transaction + transaction1.commit(); + + // create transaction 2 + Transaction transaction2 = client.openTransaction(); + + // get the update metadata + Document docMetadataUpdate = getXMLMetadata("metadata-updated.xml"); + + // create handle for metadata update + DOMHandle writeMetadataHandleUpdate = new DOMHandle(); + writeMetadataHandleUpdate.set(docMetadataUpdate); + + // write updated metadata + docMgr.writeMetadata(docId, writeMetadataHandleUpdate, transaction2); + + // commit transaction2 + //transaction2.commit(); + + // rollback transaction2 + transaction2.rollback(); + + // create handle to read updated metadata + DOMHandle readMetadataHandleUpdate = new DOMHandle(); + + // read updated metadata + docMgr.readMetadata(docId, readMetadataHandleUpdate); + Document docReadMetadataUpdate = readMetadataHandleUpdate.get(); + + assertXpathEvaluatesTo("coll1", "string(//*[local-name()='collection'][1])", docReadMetadataUpdate); + assertXpathEvaluatesTo("coll2", "string(//*[local-name()='collection'][2])", docReadMetadataUpdate); + assertXpathEvaluatesTo("MarkLogic", "string(//*[local-name()='Author'])", docReadMetadataUpdate); + + // release the client + client.release(); + } + + @Test + public void testNegative() throws ParserConfigurationException, SAXException, IOException + { + System.out.println("testNegative"); + + String filename = "bbq1.xml"; + String uri = "/tx-rollback/"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create transaction 1 + Transaction transaction1 = client.openTransaction(); + + // create a manager for document + DocumentManager docMgr = client.newDocumentManager(); + + // create an identifier for the document + String docId = uri + filename; + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + handle.setFormat(Format.XML); + + // write the document content + docMgr.write(docId, handle, transaction1); + + // commit transaction + transaction1.commit(); + + // create transaction 2 + Transaction transaction2 = client.openTransaction(); + + // delete document + docMgr.delete(docId, transaction2); + + // commit transaction + transaction2.commit(); + + String expectedException = "com.marklogic.client.FailedRequestException: Local message: transaction rollback failed: Bad Request. Server Message: XDMP-NOTXN"; + String exception = ""; + + // rollback transaction + try + { + transaction2.rollback(); + } catch(Exception e) { exception = e.toString(); }; + + assertTrue("Exception is not thrown", exception.contains(expectedException)); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestSSLConnection.java b/test-complete/src/test/java/com/marklogic/javaclient/TestSSLConnection.java index 8a26be3a0..8c3ac7303 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestSSLConnection.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestSSLConnection.java @@ -1,395 +1,374 @@ -package com.marklogic.javaclient; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; -import static org.junit.Assert.*; - -import java.io.FileNotFoundException; -import java.io.IOException; -import java.security.KeyManagementException; -import java.security.NoSuchAlgorithmException; -import java.security.cert.X509Certificate; - -import javax.net.ssl.SSLContext; -import javax.net.ssl.TrustManager; -import javax.net.ssl.X509TrustManager; -import javax.xml.namespace.QName; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.w3c.dom.Document; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.io.Format; -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.admin.QueryOptionsManager; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.DatabaseClientFactory.SSLHostnameVerifier; -import com.marklogic.client.admin.config.QueryOptionsBuilder; -import com.marklogic.client.query.StructuredQueryBuilder; -import com.marklogic.client.query.StructuredQueryDefinition; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.DocumentMetadataHandle; -import com.marklogic.client.io.QueryOptionsHandle; -import com.marklogic.client.io.StringHandle; - -import org.junit.*; -public class TestSSLConnection extends BasicJavaClientREST { - - private static String dbName = "TestSSLConnectionDB"; - private static String [] fNames = {"TestSSLConnectionDB-1"}; - private static String restServerName = "REST-Java-Client-API-SSL-Server"; - -@BeforeClass - public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0],restServerName,8012); - setupAppServicesConstraint(dbName); - } - - /* - * - - @SuppressWarnings("deprecation") - @Test public void testSSLConnection() throws NoSuchAlgorithmException, KeyManagementException, FileNotFoundException, XpathException - { - System.out.println("Running testSSLConnection"); - - // create a trust manager - // (note: a real application should verify certificates) - TrustManager naiveTrustMgr = new X509TrustManager() { - @Override - public void checkClientTrusted(X509Certificate[] chain, String authType) { - } - @Override - public void checkServerTrusted(X509Certificate[] chain, String authType) { - } - @Override - public X509Certificate[] getAcceptedIssuers() { - return new X509Certificate[0]; - } - }; - - // create an SSL context - SSLContext sslContext = SSLContext.getInstance("SSLv3"); - sslContext.init(null, new TrustManager[] { naiveTrustMgr }, null); - - String filename1 = "constraint1.xml"; - String filename2 = "constraint2.xml"; - String filename3 = "constraint3.xml"; - String filename4 = "constraint4.xml"; - String filename5 = "constraint5.xml"; - - // create the client - // (note: a real application should use a COMMON, STRICT, or implemented hostname verifier) - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8012, "rest-admin", "x", Authentication.DIGEST, sslContext, SSLHostnameVerifier.ANY); - - // create and initialize a handle on the metadata - DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle4 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle5 = new DocumentMetadataHandle(); - - // set the metadata - metadataHandle1.getCollections().addAll("http://test.com/set1"); - metadataHandle1.getCollections().addAll("http://test.com/set5"); - metadataHandle2.getCollections().addAll("http://test.com/set1"); - metadataHandle3.getCollections().addAll("http://test.com/set3"); - metadataHandle4.getCollections().addAll("http://test.com/set3/set3-1"); - metadataHandle5.getCollections().addAll("http://test.com/set1"); - metadataHandle5.getCollections().addAll("http://test.com/set5"); - - // write docs - writeDocumentUsingInputStreamHandle(client, filename1, "/ssl-connection/", metadataHandle1, "XML"); - writeDocumentUsingInputStreamHandle(client, filename2, "/ssl-connection/", metadataHandle2, "XML"); - writeDocumentUsingInputStreamHandle(client, filename3, "/ssl-connection/", metadataHandle3, "XML"); - writeDocumentUsingInputStreamHandle(client, filename4, "/ssl-connection/", metadataHandle4, "XML"); - writeDocumentUsingInputStreamHandle(client, filename5, "/ssl-connection/", metadataHandle5, "XML"); - - // create query options manager - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create query options builder - QueryOptionsBuilder builder = new QueryOptionsBuilder(); - - // create query options handle - QueryOptionsHandle handle = new QueryOptionsHandle(); - - // build query options - - handle.build( - builder.returnMetrics(false), - builder.returnQtext(false), - builder.debug(true), - builder.transformResults("raw"), - builder.constraint("id", - builder.value(builder.element("id"))), - builder.constraint("date", - builder.range(false, new QName("xs:date"), builder.element("http://purl.org/dc/elements/1.1/", "date"))), - builder.constraint("coll", - builder.collection(true, "http://test.com/")), - builder.constraint("para", - builder.word(builder.field("para"), - builder.termOption("case-insensitive"))), - builder.constraint("intitle", - builder.word(builder.element("title"))), - builder.constraint("price", - builder.range(false, new QName("xs:decimal"), - builder.element("http://cloudbank.com", "price"), - builder.attribute("amt"), - builder.bucket("high", "High", "120", null), - builder.bucket("medium", "Medium", "3", "14"), - builder.bucket("low", "Low", "0", "2"))), - builder.constraint("pop", - builder.range(true, new QName("xs:int"), - builder.element("popularity"), - builder.bucket("high", "High", "5", null), - builder.bucket("medium", "Medium", "3", "5"), - builder.bucket("low", "Low", "1", "3"))) - ); - - - // write query options - optionsMgr.writeOptions("AllConstraintsWithStructuredSearch", handle); - - // read query option - StringHandle readHandle = new StringHandle(); - readHandle.setFormat(Format.XML); - optionsMgr.readOptions("AllConstraintsWithStructuredSearch", readHandle); - String output = readHandle.get(); - System.out.println(output); - - // create query manager - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder("AllConstraintsWithStructuredSearch"); - StructuredQueryDefinition query1 = qb.and(qb.collectionConstraint("coll", "set1"), qb.collectionConstraint("coll", "set5")); - StructuredQueryDefinition query2 = qb.not(qb.wordConstraint("intitle", "memex")); - StructuredQueryDefinition query3 = qb.valueConstraint("id", "**11"); - StructuredQueryDefinition query4 = qb.rangeConstraint("date", StructuredQueryBuilder.Operator.EQ, "2005-01-01"); - StructuredQueryDefinition query5 = qb.and(qb.wordConstraint("para", "Bush"), qb.not(qb.wordConstraint("para", "memex"))); - StructuredQueryDefinition query6 = qb.rangeConstraint("price", StructuredQueryBuilder.Operator.EQ, "low"); - StructuredQueryDefinition query7 = qb.or(qb.rangeConstraint("pop", StructuredQueryBuilder.Operator.EQ, "high"), qb.rangeConstraint("pop", StructuredQueryBuilder.Operator.EQ, "medium")); - StructuredQueryDefinition queryFinal = qb.and(query1, query2, query3, query4, query5, query6, query7); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(queryFinal, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("Vannevar Bush", "string(//*[local-name()='result'][1]//*[local-name()='title'])", resultDoc); - - // release client - client.release(); - } - */ - - @SuppressWarnings("deprecation") - @Test public void testSSLConnectionInvalidPort() throws IOException, NoSuchAlgorithmException, KeyManagementException - { - System.out.println("Running testSSLConnectionInvalidPort"); - - String filename = "facebook-10443244874876159931"; - - // create a trust manager - // (note: a real application should verify certificates) - TrustManager naiveTrustMgr = new X509TrustManager() { - @Override - public void checkClientTrusted(X509Certificate[] chain, String authType) { - } - @Override - public void checkServerTrusted(X509Certificate[] chain, String authType) { - } - @Override - public X509Certificate[] getAcceptedIssuers() { - return new X509Certificate[0]; - } - }; - - // create an SSL context - SSLContext sslContext = SSLContext.getInstance("SSLv3"); - sslContext.init(null, new TrustManager[] { naiveTrustMgr }, null); - - // create the client - // (note: a real application should use a COMMON, STRICT, or implemented hostname verifier) - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8033, "rest-admin", "x", Authentication.DIGEST, sslContext, SSLHostnameVerifier.ANY); - - - String expectedException = "com.sun.jersey.api.client.ClientHandlerException: org.apache.http.conn.HttpHostConnectException: Connection to https://localhost:8033 refused"; - String exception = ""; - - // write doc - try - { - writeDocumentUsingStringHandle(client, filename, "/write-text-doc/", "Text"); - } - catch (Exception e) { exception = e.toString(); } - - assertEquals("Exception is not thrown", expectedException, exception); - - // release client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testSSLConnectionNonSSLServer() throws IOException, NoSuchAlgorithmException, KeyManagementException - { - System.out.println("Running testSSLConnectionNonSSLServer"); - - String filename = "facebook-10443244874876159931"; - - // create a trust manager - // (note: a real application should verify certificates) - TrustManager naiveTrustMgr = new X509TrustManager() { - @Override - public void checkClientTrusted(X509Certificate[] chain, String authType) { - } - @Override - public void checkServerTrusted(X509Certificate[] chain, String authType) { - } - @Override - public X509Certificate[] getAcceptedIssuers() { - return new X509Certificate[0]; - } - }; - - // create an SSL context - SSLContext sslContext = SSLContext.getInstance("SSLv3"); - sslContext.init(null, new TrustManager[] { naiveTrustMgr }, null); - - // create the client - // (note: a real application should use a COMMON, STRICT, or implemented hostname verifier) - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST, sslContext, SSLHostnameVerifier.ANY); - - - String expectedException = "com.sun.jersey.api.client.ClientHandlerException: javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated"; - String exception = ""; - - // write doc - try - { - writeDocumentUsingStringHandle(client, filename, "/write-text-doc/", "Text"); - } - catch (Exception e) { exception = e.toString(); } - - assertEquals("Exception is not thrown", expectedException, exception); - - // release client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testSSLConnectionInvalidPassword() throws IOException, NoSuchAlgorithmException, KeyManagementException - { - System.out.println("Running testSSLConnectionInvalidPassword"); - - String filename = "facebook-10443244874876159931"; - - // create a trust manager - // (note: a real application should verify certificates) - TrustManager naiveTrustMgr = new X509TrustManager() { - @Override - public void checkClientTrusted(X509Certificate[] chain, String authType) { - } - @Override - public void checkServerTrusted(X509Certificate[] chain, String authType) { - } - @Override - public X509Certificate[] getAcceptedIssuers() { - return new X509Certificate[0]; - } - }; - - // create an SSL context - SSLContext sslContext = SSLContext.getInstance("SSLv3"); - sslContext.init(null, new TrustManager[] { naiveTrustMgr }, null); - - // create the client - // (note: a real application should use a COMMON, STRICT, or implemented hostname verifier) - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8012, "rest-admin", "foo", Authentication.DIGEST, sslContext, SSLHostnameVerifier.ANY); - - - String expectedException = "FailedRequestException: Local message: write failed: Unauthorized"; - String exception = ""; - - // write doc - try - { - writeDocumentUsingStringHandle(client, filename, "/write-text-doc/", "Text"); - } - catch (Exception e) { exception = e.toString(); } - - System.out.println("Actual exception: " + exception); - boolean isExceptionThrown = exception.contains(expectedException); - - assertTrue("Exception is not thrown", isExceptionThrown); - - // release client - client.release(); - } - - - @SuppressWarnings("deprecation") - @Test public void testSSLConnectionInvalidUser() throws IOException, NoSuchAlgorithmException, KeyManagementException - { - System.out.println("Running testSSLConnectionInvalidUser"); - - String filename = "facebook-10443244874876159931"; - - // create a trust manager - // (note: a real application should verify certificates) - TrustManager naiveTrustMgr = new X509TrustManager() { - @Override - public void checkClientTrusted(X509Certificate[] chain, String authType) { - } - @Override - public void checkServerTrusted(X509Certificate[] chain, String authType) { - } - @Override - public X509Certificate[] getAcceptedIssuers() { - return new X509Certificate[0]; - } - }; - - // create an SSL context - SSLContext sslContext = SSLContext.getInstance("SSLv3"); - sslContext.init(null, new TrustManager[] { naiveTrustMgr }, null); - - // create the client - // (note: a real application should use a COMMON, STRICT, or implemented hostname verifier) - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8012, "MyFooUser", "x", Authentication.DIGEST, sslContext, SSLHostnameVerifier.ANY); - - - String expectedException = "FailedRequestException: Local message: write failed: Unauthorized"; - String exception = ""; - - // write doc - try - { - writeDocumentUsingStringHandle(client, filename, "/write-text-doc/", "Text"); - } - catch (Exception e) { exception = e.toString(); } - - boolean isExceptionThrown = exception.contains(expectedException); - - assertTrue("Exception is not thrown", isExceptionThrown); - - // release client - client.release(); - } - - @AfterClass - public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - ; - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.IOException; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.security.cert.X509Certificate; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.DatabaseClientFactory.SSLHostnameVerifier; +import org.junit.*; + +public class TestSSLConnection extends BasicJavaClientREST { + + private static String dbName = "TestSSLConnectionDB"; + private static String [] fNames = {"TestSSLConnectionDB-1"}; + private static String restServerName = "REST-Java-Client-API-SSL-Server"; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0],restServerName,8012); + setupAppServicesConstraint(dbName); + } + + /* + * + + @SuppressWarnings("deprecation") + @Test public void testSSLConnection() throws NoSuchAlgorithmException, KeyManagementException, FileNotFoundException, XpathException + { + System.out.println("Running testSSLConnection"); + + // create a trust manager + // (note: a real application should verify certificates) + TrustManager naiveTrustMgr = new X509TrustManager() { + @Override + public void checkClientTrusted(X509Certificate[] chain, String authType) { + } + @Override + public void checkServerTrusted(X509Certificate[] chain, String authType) { + } + @Override + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; + } + }; + + // create an SSL context + SSLContext sslContext = SSLContext.getInstance("SSLv3"); + sslContext.init(null, new TrustManager[] { naiveTrustMgr }, null); + + String filename1 = "constraint1.xml"; + String filename2 = "constraint2.xml"; + String filename3 = "constraint3.xml"; + String filename4 = "constraint4.xml"; + String filename5 = "constraint5.xml"; + + // create the client + // (note: a real application should use a COMMON, STRICT, or implemented hostname verifier) + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8012, "rest-admin", "x", Authentication.DIGEST, sslContext, SSLHostnameVerifier.ANY); + + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle4 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle5 = new DocumentMetadataHandle(); + + // set the metadata + metadataHandle1.getCollections().addAll("http://test.com/set1"); + metadataHandle1.getCollections().addAll("http://test.com/set5"); + metadataHandle2.getCollections().addAll("http://test.com/set1"); + metadataHandle3.getCollections().addAll("http://test.com/set3"); + metadataHandle4.getCollections().addAll("http://test.com/set3/set3-1"); + metadataHandle5.getCollections().addAll("http://test.com/set1"); + metadataHandle5.getCollections().addAll("http://test.com/set5"); + + // write docs + writeDocumentUsingInputStreamHandle(client, filename1, "/ssl-connection/", metadataHandle1, "XML"); + writeDocumentUsingInputStreamHandle(client, filename2, "/ssl-connection/", metadataHandle2, "XML"); + writeDocumentUsingInputStreamHandle(client, filename3, "/ssl-connection/", metadataHandle3, "XML"); + writeDocumentUsingInputStreamHandle(client, filename4, "/ssl-connection/", metadataHandle4, "XML"); + writeDocumentUsingInputStreamHandle(client, filename5, "/ssl-connection/", metadataHandle5, "XML"); + + // create query options manager + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create query options builder + QueryOptionsBuilder builder = new QueryOptionsBuilder(); + + // create query options handle + QueryOptionsHandle handle = new QueryOptionsHandle(); + + // build query options + + handle.build( + builder.returnMetrics(false), + builder.returnQtext(false), + builder.debug(true), + builder.transformResults("raw"), + builder.constraint("id", + builder.value(builder.element("id"))), + builder.constraint("date", + builder.range(false, new QName("xs:date"), builder.element("http://purl.org/dc/elements/1.1/", "date"))), + builder.constraint("coll", + builder.collection(true, "http://test.com/")), + builder.constraint("para", + builder.word(builder.field("para"), + builder.termOption("case-insensitive"))), + builder.constraint("intitle", + builder.word(builder.element("title"))), + builder.constraint("price", + builder.range(false, new QName("xs:decimal"), + builder.element("http://cloudbank.com", "price"), + builder.attribute("amt"), + builder.bucket("high", "High", "120", null), + builder.bucket("medium", "Medium", "3", "14"), + builder.bucket("low", "Low", "0", "2"))), + builder.constraint("pop", + builder.range(true, new QName("xs:int"), + builder.element("popularity"), + builder.bucket("high", "High", "5", null), + builder.bucket("medium", "Medium", "3", "5"), + builder.bucket("low", "Low", "1", "3"))) + ); + + + // write query options + optionsMgr.writeOptions("AllConstraintsWithStructuredSearch", handle); + + // read query option + StringHandle readHandle = new StringHandle(); + readHandle.setFormat(Format.XML); + optionsMgr.readOptions("AllConstraintsWithStructuredSearch", readHandle); + String output = readHandle.get(); + System.out.println(output); + + // create query manager + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder("AllConstraintsWithStructuredSearch"); + StructuredQueryDefinition query1 = qb.and(qb.collectionConstraint("coll", "set1"), qb.collectionConstraint("coll", "set5")); + StructuredQueryDefinition query2 = qb.not(qb.wordConstraint("intitle", "memex")); + StructuredQueryDefinition query3 = qb.valueConstraint("id", "**11"); + StructuredQueryDefinition query4 = qb.rangeConstraint("date", StructuredQueryBuilder.Operator.EQ, "2005-01-01"); + StructuredQueryDefinition query5 = qb.and(qb.wordConstraint("para", "Bush"), qb.not(qb.wordConstraint("para", "memex"))); + StructuredQueryDefinition query6 = qb.rangeConstraint("price", StructuredQueryBuilder.Operator.EQ, "low"); + StructuredQueryDefinition query7 = qb.or(qb.rangeConstraint("pop", StructuredQueryBuilder.Operator.EQ, "high"), qb.rangeConstraint("pop", StructuredQueryBuilder.Operator.EQ, "medium")); + StructuredQueryDefinition queryFinal = qb.and(query1, query2, query3, query4, query5, query6, query7); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(queryFinal, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("Vannevar Bush", "string(//*[local-name()='result'][1]//*[local-name()='title'])", resultDoc); + + // release client + client.release(); + } + */ + + @Test + public void testSSLConnectionInvalidPort() throws IOException, NoSuchAlgorithmException, KeyManagementException + { + System.out.println("Running testSSLConnectionInvalidPort"); + + String filename = "facebook-10443244874876159931"; + + // create a trust manager + // (note: a real application should verify certificates) + TrustManager naiveTrustMgr = new X509TrustManager() { + @Override + public void checkClientTrusted(X509Certificate[] chain, String authType) { + } + @Override + public void checkServerTrusted(X509Certificate[] chain, String authType) { + } + @Override + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; + } + }; + + // create an SSL context + SSLContext sslContext = SSLContext.getInstance("SSLv3"); + sslContext.init(null, new TrustManager[] { naiveTrustMgr }, null); + + // create the client + // (note: a real application should use a COMMON, STRICT, or implemented hostname verifier) + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8033, "rest-admin", "x", Authentication.DIGEST, sslContext, SSLHostnameVerifier.ANY); + + + String expectedException = "com.sun.jersey.api.client.ClientHandlerException: org.apache.http.conn.HttpHostConnectException: Connection to https://localhost:8033 refused"; + String exception = ""; + + // write doc + try + { + writeDocumentUsingStringHandle(client, filename, "/write-text-doc/", "Text"); + } + catch (Exception e) { exception = e.toString(); } + + assertEquals("Exception is not thrown", expectedException, exception); + + // release client + client.release(); + } + + @Test + public void testSSLConnectionNonSSLServer() throws IOException, NoSuchAlgorithmException, KeyManagementException + { + System.out.println("Running testSSLConnectionNonSSLServer"); + + String filename = "facebook-10443244874876159931"; + + // create a trust manager + // (note: a real application should verify certificates) + TrustManager naiveTrustMgr = new X509TrustManager() { + @Override + public void checkClientTrusted(X509Certificate[] chain, String authType) { + } + @Override + public void checkServerTrusted(X509Certificate[] chain, String authType) { + } + @Override + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; + } + }; + + // create an SSL context + SSLContext sslContext = SSLContext.getInstance("SSLv3"); + sslContext.init(null, new TrustManager[] { naiveTrustMgr }, null); + + // create the client + // (note: a real application should use a COMMON, STRICT, or implemented hostname verifier) + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST, sslContext, SSLHostnameVerifier.ANY); + + + String expectedException = "com.sun.jersey.api.client.ClientHandlerException: javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated"; + String exception = ""; + + // write doc + try + { + writeDocumentUsingStringHandle(client, filename, "/write-text-doc/", "Text"); + } + catch (Exception e) { exception = e.toString(); } + + assertEquals("Exception is not thrown", expectedException, exception); + + // release client + client.release(); + } + + @Test + public void testSSLConnectionInvalidPassword() throws IOException, NoSuchAlgorithmException, KeyManagementException + { + System.out.println("Running testSSLConnectionInvalidPassword"); + + String filename = "facebook-10443244874876159931"; + + // create a trust manager + // (note: a real application should verify certificates) + TrustManager naiveTrustMgr = new X509TrustManager() { + @Override + public void checkClientTrusted(X509Certificate[] chain, String authType) { + } + @Override + public void checkServerTrusted(X509Certificate[] chain, String authType) { + } + @Override + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; + } + }; + + // create an SSL context + SSLContext sslContext = SSLContext.getInstance("SSLv3"); + sslContext.init(null, new TrustManager[] { naiveTrustMgr }, null); + + // create the client + // (note: a real application should use a COMMON, STRICT, or implemented hostname verifier) + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8012, "rest-admin", "foo", Authentication.DIGEST, sslContext, SSLHostnameVerifier.ANY); + + + String expectedException = "FailedRequestException: Local message: write failed: Unauthorized"; + String exception = ""; + + // write doc + try + { + writeDocumentUsingStringHandle(client, filename, "/write-text-doc/", "Text"); + } + catch (Exception e) { exception = e.toString(); } + + System.out.println("Actual exception: " + exception); + boolean isExceptionThrown = exception.contains(expectedException); + + assertTrue("Exception is not thrown", isExceptionThrown); + + // release client + client.release(); + } + + @Test + public void testSSLConnectionInvalidUser() throws IOException, NoSuchAlgorithmException, KeyManagementException + { + System.out.println("Running testSSLConnectionInvalidUser"); + + String filename = "facebook-10443244874876159931"; + + // create a trust manager + // (note: a real application should verify certificates) + TrustManager naiveTrustMgr = new X509TrustManager() { + @Override + public void checkClientTrusted(X509Certificate[] chain, String authType) { + } + @Override + public void checkServerTrusted(X509Certificate[] chain, String authType) { + } + @Override + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; + } + }; + + // create an SSL context + SSLContext sslContext = SSLContext.getInstance("SSLv3"); + sslContext.init(null, new TrustManager[] { naiveTrustMgr }, null); + + // create the client + // (note: a real application should use a COMMON, STRICT, or implemented hostname verifier) + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8012, "MyFooUser", "x", Authentication.DIGEST, sslContext, SSLHostnameVerifier.ANY); + + + String expectedException = "FailedRequestException: Local message: write failed: Unauthorized"; + String exception = ""; + + // write doc + try + { + writeDocumentUsingStringHandle(client, filename, "/write-text-doc/", "Text"); + } + catch (Exception e) { exception = e.toString(); } + + boolean isExceptionThrown = exception.contains(expectedException); + + assertTrue("Exception is not thrown", isExceptionThrown); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestSearchMultibyte.java b/test-complete/src/test/java/com/marklogic/javaclient/TestSearchMultibyte.java index 716cab378..1d274d410 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestSearchMultibyte.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestSearchMultibyte.java @@ -1,165 +1,166 @@ -package com.marklogic.javaclient; - -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import com.marklogic.client.query.QueryManager; - -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.query.StringQueryDefinition; -import com.marklogic.client.io.DOMHandle; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.junit.*; -public class TestSearchMultibyte extends BasicJavaClientREST { - - private static String dbName = "TestSearchMultibyteDB"; - private static String [] fNames = {"TestSearchMultibyteDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort = 8011; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - } - -@After - public void testCleanUp() throws Exception - { - clearDB(restPort); - System.out.println("Running clear script"); - } - -@Test public void testSearchString() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testSearchString"); - - String[] filenames = {"multibyte1.xml", "multibyte2.xml", "multibyte3.xml"}; - String queryOptionName = "multibyteSearchOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/multibyte-search/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("mult-title:万里长城"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("/multibyte-search/multibyte1.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testSearchStringWithBucket() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testSearchStringWithBucket"); - - String[] filenames = {"multibyte1.xml", "multibyte2.xml", "multibyte3.xml"}; - String queryOptionName = "multibyteSearchOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/multibyte-search/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("mult-pop:medium"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("/multibyte-search/multibyte2.xml", "string(//*[local-name()='result'][1]//@*[local-name()='uri'])", resultDoc); - assertXpathEvaluatesTo("/multibyte-search/multibyte1.xml", "string(//*[local-name()='result'][2]//@*[local-name()='uri'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testSearchStringWithBucketAndWord() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testSearchStringWithBucketAndWord"); - - String[] filenames = {"multibyte1.xml", "multibyte2.xml", "multibyte3.xml"}; - String queryOptionName = "multibyteSearchOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/multibyte-search/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("mult-pop:medium AND mult-title:上海"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("/multibyte-search/multibyte2.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); - - // release client - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames,restServerName); - } +package com.marklogic.javaclient; + +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import com.marklogic.client.query.QueryManager; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.query.StringQueryDefinition; +import com.marklogic.client.io.DOMHandle; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.*; +public class TestSearchMultibyte extends BasicJavaClientREST { + + private static String dbName = "TestSearchMultibyteDB"; + private static String [] fNames = {"TestSearchMultibyteDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + } + + @After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + + @Test + public void testSearchString() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testSearchString"); + + String[] filenames = {"multibyte1.xml", "multibyte2.xml", "multibyte3.xml"}; + String queryOptionName = "multibyteSearchOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/multibyte-search/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("mult-title:万里长城"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("/multibyte-search/multibyte1.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testSearchStringWithBucket() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testSearchStringWithBucket"); + + String[] filenames = {"multibyte1.xml", "multibyte2.xml", "multibyte3.xml"}; + String queryOptionName = "multibyteSearchOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/multibyte-search/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("mult-pop:medium"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("/multibyte-search/multibyte2.xml", "string(//*[local-name()='result'][1]//@*[local-name()='uri'])", resultDoc); + assertXpathEvaluatesTo("/multibyte-search/multibyte1.xml", "string(//*[local-name()='result'][2]//@*[local-name()='uri'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testSearchStringWithBucketAndWord() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testSearchStringWithBucketAndWord"); + + String[] filenames = {"multibyte1.xml", "multibyte2.xml", "multibyte3.xml"}; + String queryOptionName = "multibyteSearchOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/multibyte-search/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("mult-pop:medium AND mult-title:上海"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("/multibyte-search/multibyte2.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames,restServerName); + } } \ No newline at end of file diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestSearchMultipleForests.java b/test-complete/src/test/java/com/marklogic/javaclient/TestSearchMultipleForests.java index 6589b2689..695af8096 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestSearchMultipleForests.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestSearchMultipleForests.java @@ -1,80 +1,80 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import com.marklogic.client.query.QueryManager; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.document.DocumentManager; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.query.StringQueryDefinition; -import com.marklogic.client.io.SearchHandle; -import com.marklogic.client.io.StringHandle; -import org.junit.*; -public class TestSearchMultipleForests extends BasicJavaClientREST { - - private static String dbName = "TestSearchMultipleForestsDB"; - private static String [] fNames = {"TestSearchMultipleForestsDB-1", "TestSearchMultipleForestsDB-2"}; - private static String restServerName = "REST-Java-Client-API-Server"; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - createForest(fNames[1],dbName); - setupAppServicesGeoConstraint(dbName); - } - - -@SuppressWarnings("deprecation") -@Test public void testSearchMultipleForest() throws IOException, SAXException, ParserConfigurationException, TransformerException - { - System.out.println("Running testSearchMultipleForest"); - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - DocumentManager docMgr = client.newDocumentManager(); - docMgr.setForestName("TestSearchMultipleForestsDB-1"); - StringHandle writeHandle1 = new StringHandle(); - for(int a = 1; a <= 10; a++ ) - { - writeHandle1.set("hello"); - docMgr.write("/forest-A/file" + a + ".xml", writeHandle1); - } - - docMgr.setForestName("TestSearchMultipleForestsDB-2"); - StringHandle writeHandle2 = new StringHandle(); - for(int b = 1; b <= 10; b++ ) - { - writeHandle1.set("hello"); - docMgr.write("/forest-B/file" + b + ".xml", writeHandle1); - } - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(""); - querydef.setCriteria(""); - - SearchHandle sHandle = new SearchHandle(); - queryMgr.search(querydef, sHandle); - System.out.println(sHandle.getTotalResults()); - assertTrue("Document count is incorrect", sHandle.getTotalResults() == 20); - - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import com.marklogic.client.query.QueryManager; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.document.DocumentManager; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.query.StringQueryDefinition; +import com.marklogic.client.io.SearchHandle; +import com.marklogic.client.io.StringHandle; +import org.junit.*; +public class TestSearchMultipleForests extends BasicJavaClientREST { + + private static String dbName = "TestSearchMultipleForestsDB"; + private static String [] fNames = {"TestSearchMultipleForestsDB-1", "TestSearchMultipleForestsDB-2"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + createForest(fNames[1],dbName); + setupAppServicesGeoConstraint(dbName); + } + + @Test + public void testSearchMultipleForest() throws IOException, SAXException, ParserConfigurationException, TransformerException + { + System.out.println("Running testSearchMultipleForest"); + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + DocumentManager docMgr = client.newDocumentManager(); + docMgr.setForestName("TestSearchMultipleForestsDB-1"); + StringHandle writeHandle1 = new StringHandle(); + for(int a = 1; a <= 10; a++ ) + { + writeHandle1.set("hello"); + docMgr.write("/forest-A/file" + a + ".xml", writeHandle1); + } + + docMgr.setForestName("TestSearchMultipleForestsDB-2"); + StringHandle writeHandle2 = new StringHandle(); + for(int b = 1; b <= 10; b++ ) + { + writeHandle1.set("hello"); + docMgr.write("/forest-B/file" + b + ".xml", writeHandle1); + } + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(""); + querydef.setCriteria(""); + + SearchHandle sHandle = new SearchHandle(); + queryMgr.search(querydef, sHandle); + System.out.println(sHandle.getTotalResults()); + assertTrue("Document count is incorrect", sHandle.getTotalResults() == 20); + + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestSearchOnJSON.java b/test-complete/src/test/java/com/marklogic/javaclient/TestSearchOnJSON.java index 80aafd5cd..03e0df046 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestSearchOnJSON.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestSearchOnJSON.java @@ -1,197 +1,199 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.IOException; -import java.io.InputStream; - -import javax.xml.parsers.ParserConfigurationException; - -import com.marklogic.client.query.QueryManager; - -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.io.Format; -import com.marklogic.client.admin.QueryOptionsManager; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.query.StringQueryDefinition; -import com.marklogic.client.io.DocumentMetadataHandle; -import com.marklogic.client.io.InputStreamHandle; -import com.marklogic.client.io.ReaderHandle; -import com.marklogic.client.io.StringHandle; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.junit.*; -public class TestSearchOnJSON extends BasicJavaClientREST { - - private static String dbName = "TestSearchOnJSONDB"; - private static String [] fNames = {"TestSearchOnJSONDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort = 8011; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0],restServerName,8011); - setupAppServicesConstraint(dbName); - } - -@After -public void testCleanUp() throws Exception -{ - clearDB(restPort); - System.out.println("Running clear script"); -} - -@SuppressWarnings("deprecation") -@Test public void testRoundtrippingQueryOption() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testRoundtrippingQueryOption"); - - String queryOptionName = "valueConstraintWildCardOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create a manager for writing query options - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create handle - ReaderHandle handle = new ReaderHandle(); - - // write the files - BufferedReader docStream = new BufferedReader(new FileReader("src/test/java/com/marklogic/javaclient/queryoptions/" + queryOptionName)); - handle.set(docStream); - - // write the query options to the database - optionsMgr.writeOptions(queryOptionName, handle); - - System.out.println("Write " + queryOptionName + " to database"); - - // read query option - StringHandle readHandle = new StringHandle(); - readHandle.setFormat(Format.JSON); - optionsMgr.readOptions(queryOptionName, readHandle); - - String output = readHandle.get(); - - System.out.println(output); - - String expectedOutput = "{\"options\":{\"return-metrics\":false, \"return-qtext\":false, \"debug\":true, \"transform-results\":{\"apply\":\"raw\"}, \"constraint\":[{\"name\":\"id\", \"value\":{\"element\":{\"ns\":\"\", \"name\":\"id\"}}}]}}"; - - assertEquals("query option in JSON is difference", expectedOutput, output.trim()); - - // create handle to write back option in json - String queryOptionNameJson = queryOptionName.replaceAll(".xml", ".json"); - StringHandle writeHandle = new StringHandle(); - writeHandle.set(output); - writeHandle.setFormat(Format.JSON); - optionsMgr.writeOptions(queryOptionNameJson, writeHandle); - System.out.println("Write " + queryOptionNameJson + " to database"); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testWithAllConstraintSearchResultinJSON() throws IOException, ParserConfigurationException, SAXException, XpathException - { - System.out.println("Running testWithAllConstraintSearchResultinJSON"); - - String filename1 = "constraint1.xml"; - String filename2 = "constraint2.xml"; - String filename3 = "constraint3.xml"; - String filename4 = "constraint4.xml"; - String filename5 = "constraint5.xml"; - String queryOptionName = "appservicesConstraintCombinationOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create and initialize a handle on the metadata - DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle4 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle5 = new DocumentMetadataHandle(); - - // set the metadata - metadataHandle1.getCollections().addAll("http://test.com/set1"); - metadataHandle1.getCollections().addAll("http://test.com/set5"); - metadataHandle2.getCollections().addAll("http://test.com/set1"); - metadataHandle3.getCollections().addAll("http://test.com/set3"); - metadataHandle4.getCollections().addAll("http://test.com/set3/set3-1"); - metadataHandle5.getCollections().addAll("http://test.com/set1"); - metadataHandle5.getCollections().addAll("http://test.com/set5"); - - // write docs - writeDocumentUsingInputStreamHandle(client, filename1, "/all-constraint-json/", metadataHandle1, "XML"); - writeDocumentUsingInputStreamHandle(client, filename2, "/all-constraint-json/", metadataHandle2, "XML"); - writeDocumentUsingInputStreamHandle(client, filename3, "/all-constraint-json/", metadataHandle3, "XML"); - writeDocumentUsingInputStreamHandle(client, filename4, "/all-constraint-json/", metadataHandle4, "XML"); - writeDocumentUsingInputStreamHandle(client, filename5, "/all-constraint-json/", metadataHandle5, "XML"); - - // create a manager for writing query options - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create handle - ReaderHandle handle = new ReaderHandle(); - - // write the files - BufferedReader docStream = new BufferedReader(new FileReader("src/test/java/com/marklogic/javaclient/queryoptions/" + queryOptionName)); - handle.set(docStream); - - // write the query options to the database - optionsMgr.writeOptions(queryOptionName, handle); - - System.out.println("Write " + queryOptionName + " to database"); - - // read query option - InputStreamHandle readHandle = new InputStreamHandle(); - readHandle.setFormat(Format.JSON); - optionsMgr.readOptions(queryOptionName, readHandle); - - InputStream output = readHandle.get(); - - // create handle to write back option in json - String queryOptionNameJson = queryOptionName.replaceAll(".xml", ".json"); - InputStreamHandle writeHandle = new InputStreamHandle(); - writeHandle.set(output); - writeHandle.setFormat(Format.JSON); - optionsMgr.writeOptions(queryOptionNameJson, writeHandle); - System.out.println("Write " + queryOptionNameJson + " to database"); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionNameJson); - querydef.setCriteria("(coll:set1 AND coll:set5) AND -intitle:memex AND (pop:high OR pop:medium) AND price:low AND id:**11 AND date:2005-01-01 AND (para:Bush AND -para:memex)"); - - // create handle - StringHandle resultsHandle = new StringHandle(); - resultsHandle.setFormat(Format.JSON); - queryMgr.search(querydef, resultsHandle); - - // get the result - String resultString = resultsHandle.get(); - - boolean isTotalCorrect = resultString.contains("\"total\":1"); - boolean isUriCorrect = resultString.contains("\"uri\":\"/all-constraint-json/constraint1.xml\""); - boolean isTitleCorrect = resultString.contains("Vannevar Bush"); - assertTrue("Returned total document is incorrect", isTotalCorrect); - assertTrue("Returned document URI is incorrect", isUriCorrect); - assertTrue("Returned document title is incorrect", isTitleCorrect); - - // release client - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; + +import javax.xml.parsers.ParserConfigurationException; + +import com.marklogic.client.query.QueryManager; + +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.io.Format; +import com.marklogic.client.admin.QueryOptionsManager; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.query.StringQueryDefinition; +import com.marklogic.client.io.DocumentMetadataHandle; +import com.marklogic.client.io.InputStreamHandle; +import com.marklogic.client.io.ReaderHandle; +import com.marklogic.client.io.StringHandle; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.*; + +public class TestSearchOnJSON extends BasicJavaClientREST { + + private static String dbName = "TestSearchOnJSONDB"; + private static String [] fNames = {"TestSearchOnJSONDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0],restServerName,8011); + setupAppServicesConstraint(dbName); + } + + @After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + + @Test + public void testRoundtrippingQueryOption() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testRoundtrippingQueryOption"); + + String queryOptionName = "valueConstraintWildCardOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create a manager for writing query options + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create handle + ReaderHandle handle = new ReaderHandle(); + + // write the files + BufferedReader docStream = new BufferedReader(new FileReader("src/test/java/com/marklogic/javaclient/queryoptions/" + queryOptionName)); + handle.set(docStream); + + // write the query options to the database + optionsMgr.writeOptions(queryOptionName, handle); + + System.out.println("Write " + queryOptionName + " to database"); + + // read query option + StringHandle readHandle = new StringHandle(); + readHandle.setFormat(Format.JSON); + optionsMgr.readOptions(queryOptionName, readHandle); + + String output = readHandle.get(); + + System.out.println(output); + + String expectedOutput = "{\"options\":{\"return-metrics\":false, \"return-qtext\":false, \"debug\":true, \"transform-results\":{\"apply\":\"raw\"}, \"constraint\":[{\"name\":\"id\", \"value\":{\"element\":{\"ns\":\"\", \"name\":\"id\"}}}]}}"; + + assertEquals("query option in JSON is difference", expectedOutput, output.trim()); + + // create handle to write back option in json + String queryOptionNameJson = queryOptionName.replaceAll(".xml", ".json"); + StringHandle writeHandle = new StringHandle(); + writeHandle.set(output); + writeHandle.setFormat(Format.JSON); + optionsMgr.writeOptions(queryOptionNameJson, writeHandle); + System.out.println("Write " + queryOptionNameJson + " to database"); + + // release client + client.release(); + } + + @Test + public void testWithAllConstraintSearchResultinJSON() throws IOException, ParserConfigurationException, SAXException, XpathException + { + System.out.println("Running testWithAllConstraintSearchResultinJSON"); + + String filename1 = "constraint1.xml"; + String filename2 = "constraint2.xml"; + String filename3 = "constraint3.xml"; + String filename4 = "constraint4.xml"; + String filename5 = "constraint5.xml"; + String queryOptionName = "appservicesConstraintCombinationOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle4 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle5 = new DocumentMetadataHandle(); + + // set the metadata + metadataHandle1.getCollections().addAll("http://test.com/set1"); + metadataHandle1.getCollections().addAll("http://test.com/set5"); + metadataHandle2.getCollections().addAll("http://test.com/set1"); + metadataHandle3.getCollections().addAll("http://test.com/set3"); + metadataHandle4.getCollections().addAll("http://test.com/set3/set3-1"); + metadataHandle5.getCollections().addAll("http://test.com/set1"); + metadataHandle5.getCollections().addAll("http://test.com/set5"); + + // write docs + writeDocumentUsingInputStreamHandle(client, filename1, "/all-constraint-json/", metadataHandle1, "XML"); + writeDocumentUsingInputStreamHandle(client, filename2, "/all-constraint-json/", metadataHandle2, "XML"); + writeDocumentUsingInputStreamHandle(client, filename3, "/all-constraint-json/", metadataHandle3, "XML"); + writeDocumentUsingInputStreamHandle(client, filename4, "/all-constraint-json/", metadataHandle4, "XML"); + writeDocumentUsingInputStreamHandle(client, filename5, "/all-constraint-json/", metadataHandle5, "XML"); + + // create a manager for writing query options + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create handle + ReaderHandle handle = new ReaderHandle(); + + // write the files + BufferedReader docStream = new BufferedReader(new FileReader("src/test/java/com/marklogic/javaclient/queryoptions/" + queryOptionName)); + handle.set(docStream); + + // write the query options to the database + optionsMgr.writeOptions(queryOptionName, handle); + + System.out.println("Write " + queryOptionName + " to database"); + + // read query option + InputStreamHandle readHandle = new InputStreamHandle(); + readHandle.setFormat(Format.JSON); + optionsMgr.readOptions(queryOptionName, readHandle); + + InputStream output = readHandle.get(); + + // create handle to write back option in json + String queryOptionNameJson = queryOptionName.replaceAll(".xml", ".json"); + InputStreamHandle writeHandle = new InputStreamHandle(); + writeHandle.set(output); + writeHandle.setFormat(Format.JSON); + optionsMgr.writeOptions(queryOptionNameJson, writeHandle); + System.out.println("Write " + queryOptionNameJson + " to database"); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionNameJson); + querydef.setCriteria("(coll:set1 AND coll:set5) AND -intitle:memex AND (pop:high OR pop:medium) AND price:low AND id:**11 AND date:2005-01-01 AND (para:Bush AND -para:memex)"); + + // create handle + StringHandle resultsHandle = new StringHandle(); + resultsHandle.setFormat(Format.JSON); + queryMgr.search(querydef, resultsHandle); + + // get the result + String resultString = resultsHandle.get(); + + boolean isTotalCorrect = resultString.contains("\"total\":1"); + boolean isUriCorrect = resultString.contains("\"uri\":\"/all-constraint-json/constraint1.xml\""); + boolean isTitleCorrect = resultString.contains("Vannevar Bush"); + assertTrue("Returned total document is incorrect", isTotalCorrect); + assertTrue("Returned document URI is incorrect", isUriCorrect); + assertTrue("Returned document title is incorrect", isTitleCorrect); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestSearchOnProperties.java b/test-complete/src/test/java/com/marklogic/javaclient/TestSearchOnProperties.java index 8c6374a7d..b181df24c 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestSearchOnProperties.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestSearchOnProperties.java @@ -1,333 +1,330 @@ -package com.marklogic.javaclient; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; -import static org.junit.Assert.*; - -import java.io.*; - -import java.util.Calendar; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import com.marklogic.client.query.QueryManager; -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.admin.QueryOptionsManager; -import com.marklogic.client.DatabaseClientFactory.Authentication; - -import com.marklogic.client.document.XMLDocumentManager; - -import com.marklogic.client.query.MatchDocumentSummary; -import com.marklogic.client.query.MatchLocation; -import com.marklogic.client.query.MatchSnippet; -import com.marklogic.client.query.StringQueryDefinition; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.FileHandle; -import com.marklogic.client.io.DocumentMetadataHandle; -import com.marklogic.client.io.SearchHandle; -import com.marklogic.client.io.StringHandle; -import org.junit.*; - -public class TestSearchOnProperties extends BasicJavaClientREST { - - private static String dbName = "SearchPropsDB"; - private static String [] fNames = {"SearchPropsDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - } - - -@SuppressWarnings("deprecation") -@Test public void testSearchOnProperties() throws IOException - { - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create the query options - StringBuilder builder = new StringBuilder(); - builder.append("\n"); - builder.append("\n"); - builder.append("properties\n"); - builder.append("\n"); - - // initialize a handle with the query options - StringHandle writeHandle = new StringHandle(builder.toString()); - - // write the query options to the database - optionsMgr.writeOptions("propSearchOpt", writeHandle); - - // acquire the content - File file = new File("xml-with-props.xml"); - file.delete(); - boolean success = file.createNewFile(); - if(success) - System.out.println("New file created on " + file.getAbsolutePath()); - else - System.out.println("Cannot create file"); - - BufferedWriter out = new BufferedWriter(new FileWriter(file)); - String content = - "\n" + - "\n" + - "\n" + - "Lisbon\n" + - "Portugal\n" + - "\n" + - "\n" + - "Madrid\n" + - "Spain\n" + - "\n" + - ""; - out.write(content); - out.close(); - - // create a manager for XML documents - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - - // create an identifier for the document - String docId = "/searchOnProps/"+file.getPath(); - - // create a handle on the content - FileHandle contentHandle = new FileHandle(file); - contentHandle.set(file); - - // create and initialize a handle on the metadata - DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); - metadataHandle.getProperties().put("reviewed", true); - metadataHandle.getProperties().put("notes", "embarcadero"); - metadataHandle.getProperties().put("number", 1234); - metadataHandle.getProperties().put("longnumber", 1234.455); - metadataHandle.getProperties().put("date", Calendar.getInstance()); - - // write the document content - docMgr.write(docId, metadataHandle, contentHandle); - - System.out.println("Write " + docId + " to database"); - - DOMHandle readHandle = new DOMHandle(); - - // read the metadata - docMgr.read(docId, metadataHandle, readHandle); - - // create a manager for searching - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition - StringQueryDefinition queryDef = queryMgr.newStringDefinition("propSearchOpt"); - queryDef.setCriteria("embarcadero"); - - // create a handle for the search results - SearchHandle resultsHandle = new SearchHandle(); - - // run the search - queryMgr.search(queryDef, resultsHandle); - - long totalResults = resultsHandle.getTotalResults(); - assertEquals("Total results difference", 1, totalResults); - - // iterate over the result documents - MatchDocumentSummary[] docSummaries = resultsHandle.getMatchResults(); - - int docSummaryLength = docSummaries.length; - assertEquals("Document summary list difference", 1, docSummaryLength); - - for (MatchDocumentSummary docSummary: docSummaries) { - String uri = docSummary.getUri(); - - // iterate over the match locations within a result document - MatchLocation[] locations = docSummary.getMatchLocations(); - assertEquals("Document location difference", "/searchOnProps/"+file.getPath(), uri); - - for (MatchLocation location: locations) { - - // iterate over the snippets at a match location - for (MatchSnippet snippet : location.getSnippets()) { - String highlightedText = ""; - boolean isHighlighted = snippet.isHighlighted(); - if(isHighlighted) - highlightedText = highlightedText + "["; - highlightedText = highlightedText + snippet.getText(); - highlightedText = highlightedText + "]"; - - assertEquals("Document highlight snippet difference", "[embarcadero]", highlightedText); - } - } - } - - // release the client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testSearchOnPropertiesFragment() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testSearchOnPropertiesFragment"); - - String filename1 = "property1.xml"; - String filename2 = "property2.xml"; - String filename3 = "property3.xml"; - String queryOptionName = "propertiesSearchWordOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create and initialize a handle on the metadata - DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); - - // set metadata properties - metadataHandle1.getProperties().put("city", "Tokyo"); - metadataHandle2.getProperties().put("city", "Shanghai"); - metadataHandle3.getProperties().put("city", "Tokyo"); - - // write docs - writeDocumentUsingInputStreamHandle(client, filename1, "/properties-search/", metadataHandle1, "XML"); - writeDocumentUsingInputStreamHandle(client, filename2, "/properties-search/", metadataHandle2, "XML"); - writeDocumentUsingInputStreamHandle(client, filename3, "/properties-search/", metadataHandle3, "XML"); - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("city-property:Shanghai"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("/properties-search/property2.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testSearchOnPropertiesBucket() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testSearchOnPropertiesBucket"); - - String filename1 = "property1.xml"; - String filename2 = "property2.xml"; - String filename3 = "property3.xml"; - String queryOptionName = "propertiesSearchWordOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create and initialize a handle on the metadata - DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); - - // set metadata properties - metadataHandle1.getProperties().put("popularity", 5); - metadataHandle2.getProperties().put("popularity", 9); - metadataHandle3.getProperties().put("popularity", 1); - - // write docs - writeDocumentUsingInputStreamHandle(client, filename1, "/properties-search/", metadataHandle1, "XML"); - writeDocumentUsingInputStreamHandle(client, filename2, "/properties-search/", metadataHandle2, "XML"); - writeDocumentUsingInputStreamHandle(client, filename3, "/properties-search/", metadataHandle3, "XML"); - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("pop:medium"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("/properties-search/property1.xml", "string(//*[local-name()='result'][1]//@*[local-name()='uri'])", resultDoc); - assertXpathEvaluatesTo("/properties-search/property2.xml", "string(//*[local-name()='result'][2]//@*[local-name()='uri'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testSearchOnPropertiesBucketAndWord() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testSearchOnPropertiesBucketAndWord"); - - String filename1 = "property1.xml"; - String filename2 = "property2.xml"; - String filename3 = "property3.xml"; - String queryOptionName = "propertiesSearchWordOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create and initialize a handle on the metadata - DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); - - // set metadata properties - metadataHandle1.getProperties().put("popularity", 5); - metadataHandle2.getProperties().put("popularity", 9); - metadataHandle3.getProperties().put("popularity", 1); - metadataHandle1.getProperties().put("city", "Shanghai is a good one"); - metadataHandle2.getProperties().put("city", "Tokyo is hot in the summer"); - metadataHandle3.getProperties().put("city", "The food in Seoul is similar in Shanghai"); - - // write docs - writeDocumentUsingInputStreamHandle(client, filename1, "/properties-search/", metadataHandle1, "XML"); - writeDocumentUsingInputStreamHandle(client, filename2, "/properties-search/", metadataHandle2, "XML"); - writeDocumentUsingInputStreamHandle(client, filename3, "/properties-search/", metadataHandle3, "XML"); - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("pop:medium AND city-property:Shanghai"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("/properties-search/property1.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); - - // release client - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} +package com.marklogic.javaclient; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; +import static org.junit.Assert.*; + +import java.io.*; + +import java.util.Calendar; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import com.marklogic.client.query.QueryManager; +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.admin.QueryOptionsManager; +import com.marklogic.client.DatabaseClientFactory.Authentication; + +import com.marklogic.client.document.XMLDocumentManager; + +import com.marklogic.client.query.MatchDocumentSummary; +import com.marklogic.client.query.MatchLocation; +import com.marklogic.client.query.MatchSnippet; +import com.marklogic.client.query.StringQueryDefinition; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.FileHandle; +import com.marklogic.client.io.DocumentMetadataHandle; +import com.marklogic.client.io.SearchHandle; +import com.marklogic.client.io.StringHandle; +import org.junit.*; + +public class TestSearchOnProperties extends BasicJavaClientREST { + + private static String dbName = "SearchPropsDB"; + private static String [] fNames = {"SearchPropsDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + } + + @Test + public void testSearchOnProperties() throws IOException + { + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create the query options + StringBuilder builder = new StringBuilder(); + builder.append("\n"); + builder.append("\n"); + builder.append("properties\n"); + builder.append("\n"); + + // initialize a handle with the query options + StringHandle writeHandle = new StringHandle(builder.toString()); + + // write the query options to the database + optionsMgr.writeOptions("propSearchOpt", writeHandle); + + // acquire the content + File file = new File("xml-with-props.xml"); + file.delete(); + boolean success = file.createNewFile(); + if(success) + System.out.println("New file created on " + file.getAbsolutePath()); + else + System.out.println("Cannot create file"); + + BufferedWriter out = new BufferedWriter(new FileWriter(file)); + String content = + "\n" + + "\n" + + "\n" + + "Lisbon\n" + + "Portugal\n" + + "\n" + + "\n" + + "Madrid\n" + + "Spain\n" + + "\n" + + ""; + out.write(content); + out.close(); + + // create a manager for XML documents + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + + // create an identifier for the document + String docId = "/searchOnProps/"+file.getPath(); + + // create a handle on the content + FileHandle contentHandle = new FileHandle(file); + contentHandle.set(file); + + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); + metadataHandle.getProperties().put("reviewed", true); + metadataHandle.getProperties().put("notes", "embarcadero"); + metadataHandle.getProperties().put("number", 1234); + metadataHandle.getProperties().put("longnumber", 1234.455); + metadataHandle.getProperties().put("date", Calendar.getInstance()); + + // write the document content + docMgr.write(docId, metadataHandle, contentHandle); + + System.out.println("Write " + docId + " to database"); + + DOMHandle readHandle = new DOMHandle(); + + // read the metadata + docMgr.read(docId, metadataHandle, readHandle); + + // create a manager for searching + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition + StringQueryDefinition queryDef = queryMgr.newStringDefinition("propSearchOpt"); + queryDef.setCriteria("embarcadero"); + + // create a handle for the search results + SearchHandle resultsHandle = new SearchHandle(); + + // run the search + queryMgr.search(queryDef, resultsHandle); + + long totalResults = resultsHandle.getTotalResults(); + assertEquals("Total results difference", 1, totalResults); + + // iterate over the result documents + MatchDocumentSummary[] docSummaries = resultsHandle.getMatchResults(); + + int docSummaryLength = docSummaries.length; + assertEquals("Document summary list difference", 1, docSummaryLength); + + for (MatchDocumentSummary docSummary: docSummaries) { + String uri = docSummary.getUri(); + + // iterate over the match locations within a result document + MatchLocation[] locations = docSummary.getMatchLocations(); + assertEquals("Document location difference", "/searchOnProps/"+file.getPath(), uri); + + for (MatchLocation location: locations) { + + // iterate over the snippets at a match location + for (MatchSnippet snippet : location.getSnippets()) { + String highlightedText = ""; + boolean isHighlighted = snippet.isHighlighted(); + if(isHighlighted) + highlightedText = highlightedText + "["; + highlightedText = highlightedText + snippet.getText(); + highlightedText = highlightedText + "]"; + + assertEquals("Document highlight snippet difference", "[embarcadero]", highlightedText); + } + } + } + + // release the client + client.release(); + } + + @Test + public void testSearchOnPropertiesFragment() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testSearchOnPropertiesFragment"); + + String filename1 = "property1.xml"; + String filename2 = "property2.xml"; + String filename3 = "property3.xml"; + String queryOptionName = "propertiesSearchWordOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); + + // set metadata properties + metadataHandle1.getProperties().put("city", "Tokyo"); + metadataHandle2.getProperties().put("city", "Shanghai"); + metadataHandle3.getProperties().put("city", "Tokyo"); + + // write docs + writeDocumentUsingInputStreamHandle(client, filename1, "/properties-search/", metadataHandle1, "XML"); + writeDocumentUsingInputStreamHandle(client, filename2, "/properties-search/", metadataHandle2, "XML"); + writeDocumentUsingInputStreamHandle(client, filename3, "/properties-search/", metadataHandle3, "XML"); + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("city-property:Shanghai"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("/properties-search/property2.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testSearchOnPropertiesBucket() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testSearchOnPropertiesBucket"); + + String filename1 = "property1.xml"; + String filename2 = "property2.xml"; + String filename3 = "property3.xml"; + String queryOptionName = "propertiesSearchWordOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); + + // set metadata properties + metadataHandle1.getProperties().put("popularity", 5); + metadataHandle2.getProperties().put("popularity", 9); + metadataHandle3.getProperties().put("popularity", 1); + + // write docs + writeDocumentUsingInputStreamHandle(client, filename1, "/properties-search/", metadataHandle1, "XML"); + writeDocumentUsingInputStreamHandle(client, filename2, "/properties-search/", metadataHandle2, "XML"); + writeDocumentUsingInputStreamHandle(client, filename3, "/properties-search/", metadataHandle3, "XML"); + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("pop:medium"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("/properties-search/property1.xml", "string(//*[local-name()='result'][1]//@*[local-name()='uri'])", resultDoc); + assertXpathEvaluatesTo("/properties-search/property2.xml", "string(//*[local-name()='result'][2]//@*[local-name()='uri'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testSearchOnPropertiesBucketAndWord() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testSearchOnPropertiesBucketAndWord"); + + String filename1 = "property1.xml"; + String filename2 = "property2.xml"; + String filename3 = "property3.xml"; + String queryOptionName = "propertiesSearchWordOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); + + // set metadata properties + metadataHandle1.getProperties().put("popularity", 5); + metadataHandle2.getProperties().put("popularity", 9); + metadataHandle3.getProperties().put("popularity", 1); + metadataHandle1.getProperties().put("city", "Shanghai is a good one"); + metadataHandle2.getProperties().put("city", "Tokyo is hot in the summer"); + metadataHandle3.getProperties().put("city", "The food in Seoul is similar in Shanghai"); + + // write docs + writeDocumentUsingInputStreamHandle(client, filename1, "/properties-search/", metadataHandle1, "XML"); + writeDocumentUsingInputStreamHandle(client, filename2, "/properties-search/", metadataHandle2, "XML"); + writeDocumentUsingInputStreamHandle(client, filename3, "/properties-search/", metadataHandle3, "XML"); + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("pop:medium AND city-property:Shanghai"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("/properties-search/property1.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestSearchOptions.java b/test-complete/src/test/java/com/marklogic/javaclient/TestSearchOptions.java index 0b0818a2c..e92ed8d6b 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestSearchOptions.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestSearchOptions.java @@ -1,309 +1,306 @@ -package com.marklogic.javaclient; - -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.query.QueryManager.QueryView; - -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.query.StringQueryDefinition; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.DocumentMetadataHandle; -import com.marklogic.client.io.DocumentMetadataHandle.Capability; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; -import static org.custommonkey.xmlunit.XMLAssert.assertXpathNotExists; -import static org.custommonkey.xmlunit.XMLAssert.assertXpathExists; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.junit.*; -public class TestSearchOptions extends BasicJavaClientREST { - - - private static String dbName = "TestSearchOptionsDB"; - private static String [] fNames = {"TestSearchOptionsDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0],restServerName,8011); - setupAppServicesConstraint(dbName); - } - -@After -public void testCleanUp() throws Exception -{ - clearDB(8011); - System.out.println("Running clear script"); -} - -@SuppressWarnings("deprecation") -@Test public void testReturnResultsFalse() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testReturnResultsFalse"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "searchReturnResultsFalseOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/return-results-false/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("intitle:1945"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='response']//@*[local-name()='total'])", resultDoc); - assertXpathNotExists("//*[local-name()='result']", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testSetViewMetadata() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testSetViewMetadata"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "setViewOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create and initialize a handle on the metadata - DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); - - // put metadata - metadataHandle.getCollections().addAll("my-collection"); - metadataHandle.getCollections().addAll("another-collection"); - metadataHandle.getPermissions().add("app-user", Capability.UPDATE, Capability.READ); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/return-setview-meta/", metadataHandle, "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - queryMgr.setView(QueryView.METADATA); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("pop:high"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("3", "string(//*[local-name()='response']//@*[local-name()='total'])", resultDoc); - assertXpathExists("//*[local-name()='metrics']", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testSetViewResults() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testSetViewResults"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "setViewOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/return-setview-results/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - queryMgr.setView(QueryView.RESULTS); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("pop:high"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("3", "string(//*[local-name()='response']//@*[local-name()='total'])", resultDoc); - assertXpathExists("//*[local-name()='result']", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testSetViewFacets() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testSetViewFacets"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "setViewOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/return-setview-facets/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - queryMgr.setView(QueryView.FACETS); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("pop:high"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("3", "string(//*[local-name()='response']//@*[local-name()='total'])", resultDoc); - assertXpathExists("//*[local-name()='facet']", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testSetViewDefault() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testSetViewDefault"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "setViewOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/return-setview-all/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - queryMgr.setView(QueryView.DEFAULT); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("pop:high"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("3", "string(//*[local-name()='response']//@*[local-name()='total'])", resultDoc); - assertXpathExists("//*[local-name()='result']", resultDoc); - assertXpathExists("//*[local-name()='facet']", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testSetViewAll() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testSetViewAll"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "setViewOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/return-setview-all/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - queryMgr.setView(QueryView.ALL); - - // create query def - StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); - querydef.setCriteria("pop:high"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(querydef, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("3", "string(//*[local-name()='response']//@*[local-name()='total'])", resultDoc); - assertXpathExists("//*[local-name()='result']", resultDoc); - assertXpathExists("//*[local-name()='facet']", resultDoc); - assertXpathExists("//*[local-name()='metrics']", resultDoc); - - // release client - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } -} +package com.marklogic.javaclient; + +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.QueryManager.QueryView; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.query.StringQueryDefinition; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.DocumentMetadataHandle; +import com.marklogic.client.io.DocumentMetadataHandle.Capability; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; +import static org.custommonkey.xmlunit.XMLAssert.assertXpathNotExists; +import static org.custommonkey.xmlunit.XMLAssert.assertXpathExists; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.*; +public class TestSearchOptions extends BasicJavaClientREST { + + + private static String dbName = "TestSearchOptionsDB"; + private static String [] fNames = {"TestSearchOptionsDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0],restServerName,8011); + setupAppServicesConstraint(dbName); + } + + @After + public void testCleanUp() throws Exception + { + clearDB(8011); + System.out.println("Running clear script"); + } + + @Test + public void testReturnResultsFalse() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testReturnResultsFalse"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "searchReturnResultsFalseOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/return-results-false/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("intitle:1945"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='response']//@*[local-name()='total'])", resultDoc); + assertXpathNotExists("//*[local-name()='result']", resultDoc); + + // release client + client.release(); + } + + @Test + public void testSetViewMetadata() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testSetViewMetadata"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "setViewOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); + + // put metadata + metadataHandle.getCollections().addAll("my-collection"); + metadataHandle.getCollections().addAll("another-collection"); + metadataHandle.getPermissions().add("app-user", Capability.UPDATE, Capability.READ); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/return-setview-meta/", metadataHandle, "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + queryMgr.setView(QueryView.METADATA); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("pop:high"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + //System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("3", "string(//*[local-name()='response']//@*[local-name()='total'])", resultDoc); + assertXpathExists("//*[local-name()='metrics']", resultDoc); + + // release client + client.release(); + } + + @Test + public void testSetViewResults() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testSetViewResults"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "setViewOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/return-setview-results/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + queryMgr.setView(QueryView.RESULTS); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("pop:high"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + //System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("3", "string(//*[local-name()='response']//@*[local-name()='total'])", resultDoc); + assertXpathExists("//*[local-name()='result']", resultDoc); + + // release client + client.release(); + } + + @Test + public void testSetViewFacets() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testSetViewFacets"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "setViewOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/return-setview-facets/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + queryMgr.setView(QueryView.FACETS); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("pop:high"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + //System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("3", "string(//*[local-name()='response']//@*[local-name()='total'])", resultDoc); + assertXpathExists("//*[local-name()='facet']", resultDoc); + + // release client + client.release(); + } + + @Test + public void testSetViewDefault() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testSetViewDefault"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "setViewOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/return-setview-all/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + queryMgr.setView(QueryView.DEFAULT); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("pop:high"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("3", "string(//*[local-name()='response']//@*[local-name()='total'])", resultDoc); + assertXpathExists("//*[local-name()='result']", resultDoc); + assertXpathExists("//*[local-name()='facet']", resultDoc); + + // release client + client.release(); + } + + @Test + public void testSetViewAll() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testSetViewAll"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "setViewOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/return-setview-all/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + queryMgr.setView(QueryView.ALL); + + // create query def + StringQueryDefinition querydef = queryMgr.newStringDefinition(queryOptionName); + querydef.setCriteria("pop:high"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(querydef, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("3", "string(//*[local-name()='response']//@*[local-name()='total'])", resultDoc); + assertXpathExists("//*[local-name()='result']", resultDoc); + assertXpathExists("//*[local-name()='facet']", resultDoc); + assertXpathExists("//*[local-name()='metrics']", resultDoc); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestSearchSuggestion.java b/test-complete/src/test/java/com/marklogic/javaclient/TestSearchSuggestion.java index 79cc32295..77940d361 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestSearchSuggestion.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestSearchSuggestion.java @@ -1,310 +1,306 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.FileNotFoundException; - -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.query.SuggestDefinition; -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; - -import org.junit.*; -public class TestSearchSuggestion extends BasicJavaClientREST { - - private static String dbName = "SearchSuggestionDB"; - private static String [] fNames = {"SearchSuggestionDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort = 8011; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - addRangeElementIndex(dbName, "string", "http://action/", "title", "http://marklogic.com/collation/"); - addRangeElementIndex(dbName, "string", "http://noun/", "title", "http://marklogic.com/collation/"); - } - -@After - public void testCleanUp() throws Exception - { - clearDB(restPort); - System.out.println("Running clear script"); - } - - -@SuppressWarnings("deprecation") -@Test public void testSearchSuggestion() throws FileNotFoundException - { - System.out.println("Running testSearchSuggestion"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "suggestionOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/ss/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - SuggestDefinition def = queryMgr.newSuggestDefinition("V", queryOptionName); - - String[] suggestions = queryMgr.suggest(def); - - for(int i = 0; i < suggestions.length; i++) - { - System.out.println(suggestions[i]); - } - - assertTrue("suggestion is wrong", suggestions[0].contains("Vannevar Bush")); - assertTrue("suggestion is wrong", suggestions[1].contains("Vannevar served")); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testSearchSuggestionWithSettersAndQuery() throws FileNotFoundException - { - System.out.println("Running testSearchSuggestionWithSettersAndQuery"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "suggestionOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/ss/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - SuggestDefinition def = queryMgr.newSuggestDefinition(); - def.setOptionsName(queryOptionName); - def.setStringCriteria("V"); - def.setQueryStrings("policymaker"); - def.setLimit(2); - - String[] suggestions = queryMgr.suggest(def); - - for(int i = 0; i < suggestions.length; i++) - { - System.out.println(suggestions[i]); - } - - assertTrue("suggestion is wrong", suggestions[0].contains("Vannevar served")); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testSearchSuggestionMultiByte() throws FileNotFoundException - { - System.out.println("Running testSearchSuggestionMultiByte"); - - String[] filenames = {"multibyte1.xml", "multibyte2.xml", "multibyte3.xml"}; - String queryOptionName = "suggestionOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/ss/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - SuggestDefinition def = queryMgr.newSuggestDefinition("上海", queryOptionName); - - String[] suggestions = queryMgr.suggest(def); - - for(int i = 0; i < suggestions.length; i++) - { - System.out.println(suggestions[i]); - } - - assertTrue("suggestion is wrong", suggestions[0].contains("上海")); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testSearchSuggestionOnAttribute() throws FileNotFoundException - { - System.out.println("Running testSearchSuggestionOnAttribute"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "suggestionOpt2.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/ss/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - SuggestDefinition def = queryMgr.newSuggestDefinition("12", queryOptionName); - //SuggestDefinition def = queryMgr.newSuggestDefinition(); - - String[] suggestions = queryMgr.suggest(def); - - for(int i = 0; i < suggestions.length; i++) - { - System.out.println(suggestions[i]); - } - - assertTrue("suggestion is wrong", suggestions[0].contains("12.34")); - assertTrue("suggestion is wrong", suggestions[1].contains("123.45")); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testSearchSuggestionWithNS() throws FileNotFoundException - { - System.out.println("Running testSearchSuggestionWithNS"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "suggestionOpt3.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/ss/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - SuggestDefinition def = queryMgr.newSuggestDefinition("2005-01-01", queryOptionName); - - String[] suggestions = queryMgr.suggest(def); - - for(int i = 0; i < suggestions.length; i++) - { - System.out.println(suggestions[i]); - } - - assertTrue("suggestion is wrong", suggestions[0].contains("2005-01-01")); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testSearchSuggestionWithNSNonDefault() throws FileNotFoundException - { - System.out.println("Running testSearchSuggestionWithNSNonDefault"); - - String[] filenames = {"suggestion1.xml"}; - String queryOptionName = "suggestionOpt4.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/ss/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - //SuggestDefinition def = queryMgr.newSuggestDefinition(); - SuggestDefinition def = queryMgr.newSuggestDefinition(); - def.setOptionsName(queryOptionName); - def.setStringCriteria("noun:a"); - - String[] suggestions = queryMgr.suggest(def); - - for(int i = 0; i < suggestions.length; i++) - { - System.out.println(suggestions[i]); - } - - assertTrue("suggestion is wrong", suggestions[0].contains("noun:actor")); - assertTrue("suggestion is wrong", suggestions[1].contains("noun:actress")); - assertTrue("suggestion is wrong", suggestions[2].contains("noun:apricott")); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testSearchSuggestionWithNSDefault() throws FileNotFoundException - { - System.out.println("Running testSearchSuggestionWithNSDefault"); - - String[] filenames = {"suggestion1.xml"}; - String queryOptionName = "suggestionOpt4.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/ss/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - SuggestDefinition def = queryMgr.newSuggestDefinition(); - def.setOptionsName(queryOptionName); - def.setStringCriteria("a"); - - String[] suggestions = queryMgr.suggest(def); - - for(int i = 0; i < suggestions.length; i++) - { - System.out.println(suggestions[i]); - } - - assertTrue("suggestion is wrong", suggestions[0].contains("act")); - assertTrue("suggestion is wrong", suggestions[1].contains("acting")); - - // release client - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.FileNotFoundException; + +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.SuggestDefinition; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; + +import org.junit.*; + +public class TestSearchSuggestion extends BasicJavaClientREST { + + private static String dbName = "SearchSuggestionDB"; + private static String [] fNames = {"SearchSuggestionDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + addRangeElementIndex(dbName, "string", "http://action/", "title", "http://marklogic.com/collation/"); + addRangeElementIndex(dbName, "string", "http://noun/", "title", "http://marklogic.com/collation/"); + } + + @After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + + @Test + public void testSearchSuggestion() throws FileNotFoundException + { + System.out.println("Running testSearchSuggestion"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "suggestionOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/ss/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + SuggestDefinition def = queryMgr.newSuggestDefinition("V", queryOptionName); + + String[] suggestions = queryMgr.suggest(def); + + for(int i = 0; i < suggestions.length; i++) + { + System.out.println(suggestions[i]); + } + + assertTrue("suggestion is wrong", suggestions[0].contains("Vannevar Bush")); + assertTrue("suggestion is wrong", suggestions[1].contains("Vannevar served")); + + // release client + client.release(); + } + + @Test + public void testSearchSuggestionWithSettersAndQuery() throws FileNotFoundException + { + System.out.println("Running testSearchSuggestionWithSettersAndQuery"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "suggestionOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/ss/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + SuggestDefinition def = queryMgr.newSuggestDefinition(); + def.setOptionsName(queryOptionName); + def.setStringCriteria("V"); + def.setQueryStrings("policymaker"); + def.setLimit(2); + + String[] suggestions = queryMgr.suggest(def); + + for(int i = 0; i < suggestions.length; i++) + { + System.out.println(suggestions[i]); + } + + assertTrue("suggestion is wrong", suggestions[0].contains("Vannevar served")); + + // release client + client.release(); + } + + @Test + public void testSearchSuggestionMultiByte() throws FileNotFoundException + { + System.out.println("Running testSearchSuggestionMultiByte"); + + String[] filenames = {"multibyte1.xml", "multibyte2.xml", "multibyte3.xml"}; + String queryOptionName = "suggestionOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/ss/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + SuggestDefinition def = queryMgr.newSuggestDefinition("上海", queryOptionName); + + String[] suggestions = queryMgr.suggest(def); + + for(int i = 0; i < suggestions.length; i++) + { + System.out.println(suggestions[i]); + } + + assertTrue("suggestion is wrong", suggestions[0].contains("上海")); + + // release client + client.release(); + } + + @Test + public void testSearchSuggestionOnAttribute() throws FileNotFoundException + { + System.out.println("Running testSearchSuggestionOnAttribute"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "suggestionOpt2.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/ss/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + SuggestDefinition def = queryMgr.newSuggestDefinition("12", queryOptionName); + //SuggestDefinition def = queryMgr.newSuggestDefinition(); + + String[] suggestions = queryMgr.suggest(def); + + for(int i = 0; i < suggestions.length; i++) + { + System.out.println(suggestions[i]); + } + + assertTrue("suggestion is wrong", suggestions[0].contains("12.34")); + assertTrue("suggestion is wrong", suggestions[1].contains("123.45")); + + // release client + client.release(); + } + + @Test + public void testSearchSuggestionWithNS() throws FileNotFoundException + { + System.out.println("Running testSearchSuggestionWithNS"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "suggestionOpt3.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/ss/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + SuggestDefinition def = queryMgr.newSuggestDefinition("2005-01-01", queryOptionName); + + String[] suggestions = queryMgr.suggest(def); + + for(int i = 0; i < suggestions.length; i++) + { + System.out.println(suggestions[i]); + } + + assertTrue("suggestion is wrong", suggestions[0].contains("2005-01-01")); + + // release client + client.release(); + } + + @Test + public void testSearchSuggestionWithNSNonDefault() throws FileNotFoundException + { + System.out.println("Running testSearchSuggestionWithNSNonDefault"); + + String[] filenames = {"suggestion1.xml"}; + String queryOptionName = "suggestionOpt4.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/ss/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + //SuggestDefinition def = queryMgr.newSuggestDefinition(); + SuggestDefinition def = queryMgr.newSuggestDefinition(); + def.setOptionsName(queryOptionName); + def.setStringCriteria("noun:a"); + + String[] suggestions = queryMgr.suggest(def); + + for(int i = 0; i < suggestions.length; i++) + { + System.out.println(suggestions[i]); + } + + assertTrue("suggestion is wrong", suggestions[0].contains("noun:actor")); + assertTrue("suggestion is wrong", suggestions[1].contains("noun:actress")); + assertTrue("suggestion is wrong", suggestions[2].contains("noun:apricott")); + + // release client + client.release(); + } + + @Test + public void testSearchSuggestionWithNSDefault() throws FileNotFoundException + { + System.out.println("Running testSearchSuggestionWithNSDefault"); + + String[] filenames = {"suggestion1.xml"}; + String queryOptionName = "suggestionOpt4.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/ss/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + SuggestDefinition def = queryMgr.newSuggestDefinition(); + def.setOptionsName(queryOptionName); + def.setStringCriteria("a"); + + String[] suggestions = queryMgr.suggest(def); + + for(int i = 0; i < suggestions.length; i++) + { + System.out.println(suggestions[i]); + } + + assertTrue("suggestion is wrong", suggestions[0].contains("act")); + assertTrue("suggestion is wrong", suggestions[1].contains("acting")); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestServerAssignedDocumentURI.java b/test-complete/src/test/java/com/marklogic/javaclient/TestServerAssignedDocumentURI.java index e7e8737f5..3b76a1227 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestServerAssignedDocumentURI.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestServerAssignedDocumentURI.java @@ -29,76 +29,75 @@ import com.marklogic.client.io.StringHandle; import org.junit.*; public class TestServerAssignedDocumentURI extends BasicJavaClientREST { - + private static String dbName = "TestServerAssignedDocumentUriDB"; private static String [] fNames = {"TestServerAssignedDocumentUri-1"}; private static String restServerName = "REST-Java-Client-API-Server"; - -@BeforeClass public static void setUp() throws Exception + + @BeforeClass + public static void setUp() throws Exception { System.out.println("In setup"); setupJavaRESTServer(dbName, fNames[0],restServerName,8011); } - -@SuppressWarnings("deprecation") -@Test public void testCreate() + @Test + public void testCreate() { System.out.println("Running testCreate"); - + String filename = "flipper.xml"; - + // connect the client DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - + // create doc manager XMLDocumentManager docMgr = client.newXMLDocumentManager(); - + // create template DocumentUriTemplate template = docMgr.newDocumentUriTemplate("xml"); template.withDirectory("/mytest/create/"); - + // get the file File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); // create a handle on the content FileHandle handle = new FileHandle(file); handle.set(file); - + // create doc DocumentDescriptor desc = docMgr.create(template, handle); - + // get the uri String docId = desc.getUri(); System.out.println(docId); - + String content = docMgr.read(desc, new StringHandle()).get(); System.out.println(content); - + assertTrue("document is not created", content.contains("Flipper")); - - // release the client - client.release(); - } + // release the client + client.release(); + } -@SuppressWarnings("deprecation") -@Test public void testCreateMultibyte() + @Test + public void testCreateMultibyte() { System.out.println("Running testCreateMultibyte"); - + String filename = "flipper.xml"; - + // connect the client DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - + // create doc manager XMLDocumentManager docMgr = client.newXMLDocumentManager(); - + // create template DocumentUriTemplate template = docMgr.newDocumentUriTemplate("xml"); template.withDirectory("/里/里/"); - + // get the file File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); @@ -107,197 +106,195 @@ public class TestServerAssignedDocumentURI extends BasicJavaClientREST { handle.set(file); String content = null; try { - // create doc - DocumentDescriptor desc = docMgr.create(template, handle); - - // get the uri - String docId = desc.getUri(); - System.out.println(docId); - - content = docMgr.read(desc, new StringHandle()).get(); - System.out.println(content); - - assertTrue("document is not created", content.contains("Flipper")); + // create doc + DocumentDescriptor desc = docMgr.create(template, handle); + + // get the uri + String docId = desc.getUri(); + System.out.println(docId); + + content = docMgr.read(desc, new StringHandle()).get(); + System.out.println(content); + + assertTrue("document is not created", content.contains("Flipper")); }catch(ResourceNotFoundException e){ System.out.println("Because of Special Characters in uri, it is throwing exception"); } - // release the client - client.release(); + // release the client + client.release(); } - -@SuppressWarnings("deprecation") -@Test public void testCreateInvalidURI() // should be failed + @Test + public void testCreateInvalidURI() // should be failed { System.out.println("Running testCreateInvalidURI"); - + String filename = "flipper.xml"; - + // connect the client DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - + // create doc manager XMLDocumentManager docMgr = client.newXMLDocumentManager(); - + // create template try{ - DocumentUriTemplate template = docMgr.newDocumentUriTemplate("/"); - template.withDirectory("/mytest/create/"); - - // get the file - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + DocumentUriTemplate template = docMgr.newDocumentUriTemplate("/"); + template.withDirectory("/mytest/create/"); - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - // create doc - DocumentDescriptor desc = docMgr.create(template, handle); - - // get the uri - String docId = desc.getUri(); - System.out.println(docId); + // get the file + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + // create doc + DocumentDescriptor desc = docMgr.create(template, handle); + + // get the uri + String docId = desc.getUri(); + System.out.println(docId); } catch(IllegalArgumentException i){ i.printStackTrace(); System.out.println("Expected Output"); } - // release the client - client.release(); + // release the client + client.release(); } - -@SuppressWarnings("deprecation") -@Test public void testCreateInvalidDirectory() // Should be failed + @Test + public void testCreateInvalidDirectory() // Should be failed { System.out.println("Running testCreateInvalidDirectory"); - + String filename = "flipper.xml"; - + // connect the client DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - + // create doc manager XMLDocumentManager docMgr = client.newXMLDocumentManager(); - try{ - // create template - DocumentUriTemplate template = docMgr.newDocumentUriTemplate("xml"); - template.withDirectory("/:?#[]@/"); - - // get the file - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + try{ + // create template + DocumentUriTemplate template = docMgr.newDocumentUriTemplate("xml"); + template.withDirectory("/:?#[]@/"); - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - // create doc - DocumentDescriptor desc = docMgr.create(template, handle); - - // get the uri - String docId = desc.getUri(); - System.out.println(docId); - } - catch(IllegalArgumentException i){ - i.printStackTrace(); - assertTrue("Expected error didnt came up",i.toString().contains("Directory is not valid: /:?#[]@/")); - } - // release the client - client.release(); + // get the file + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + // create doc + DocumentDescriptor desc = docMgr.create(template, handle); + + // get the uri + String docId = desc.getUri(); + System.out.println(docId); + } + catch(IllegalArgumentException i){ + i.printStackTrace(); + assertTrue("Expected error didnt came up",i.toString().contains("Directory is not valid: /:?#[]@/")); + } + // release the client + client.release(); } - -@SuppressWarnings("deprecation") -@Test public void testCreateWithTransformerTxMetadata() throws TransformerException, ParserConfigurationException, SAXException, IOException + @Test + public void testCreateWithTransformerTxMetadata() throws TransformerException, ParserConfigurationException, SAXException, IOException { System.out.println("Running testCreateWithTransformerTxMetadata"); - + // connect the client DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - + // get the doc Source source = new StreamSource("src/test/java/com/marklogic/javaclient/data/employee.xml"); - + // get the xsl File file = new File("src/test/java/com/marklogic/javaclient/data/employee-stylesheet.xsl"); - + String xsl = convertFileToString(file); - + // create transform TransformExtensionsManager extensionMgr = client.newServerConfigManager().newTransformExtensionsManager(); extensionMgr.writeXSLTransform("somename", new StringHandle().with(xsl)); ServerTransform transform = new ServerTransform("somename"); - + // release rest-admin client client.release(); - + // connect the rest-writer client DatabaseClient client1 = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - + // create a doc manager - XMLDocumentManager docMgr = client1.newXMLDocumentManager(); - + XMLDocumentManager docMgr = client1.newXMLDocumentManager(); + // create template - DocumentUriTemplate template = docMgr.newDocumentUriTemplate("xml"); + DocumentUriTemplate template = docMgr.newDocumentUriTemplate("xml"); template.withDirectory("/mytest/create/transformer/"); - - // create a handle on the content - SourceHandle handle = new SourceHandle(); - handle.set(source); - + + // create a handle on the content + SourceHandle handle = new SourceHandle(); + handle.set(source); + // get the original metadata Document docMetadata = getXMLMetadata("metadata-original.xml"); - + // create handle to write metadata DOMHandle writeMetadataHandle = new DOMHandle(); writeMetadataHandle.set(docMetadata); - - // create transaction - Transaction transaction = client1.openTransaction(); - - // create doc - DocumentDescriptor desc = docMgr.create(template, writeMetadataHandle, handle, transform, transaction); - + + // create transaction + Transaction transaction = client1.openTransaction(); + + // create doc + DocumentDescriptor desc = docMgr.create(template, writeMetadataHandle, handle, transform, transaction); + // get the uri String docId = desc.getUri(); System.out.println(docId); - + System.out.println("Before commit:"); - + String exception = ""; try { String content = docMgr.read(desc, new StringHandle()).get(); System.out.println(content); } catch (Exception e) - + { System.out.println(e); exception = e.toString(); } - + String expectedException = "com.marklogic.client.ResourceNotFoundException: Local message: Could not read non-existent document. Server Message: RESTAPI-NODOCUMENT"; assertTrue("Exception is not thrown", exception.contains(expectedException)); System.out.println("After commit:"); transaction.commit(); - + String content1 = docMgr.read(desc, new StringHandle()).get(); System.out.println(content1); - + assertTrue("document is not created", content1.contains("firstname")); - - // read metadata - String metadataContent = docMgr.readMetadata(docId, new StringHandle()).get(); - System.out.println(metadataContent); - - assertTrue("metadata is not created", metadataContent.contains("MarkLogic")); - - // release client - client1.release(); + + // read metadata + String metadataContent = docMgr.readMetadata(docId, new StringHandle()).get(); + System.out.println(metadataContent); + + assertTrue("metadata is not created", metadataContent.contains("MarkLogic")); + + // release client + client1.release(); } - -@AfterClass public static void tearDown() throws Exception + + @AfterClass + public static void tearDown() throws Exception { System.out.println("In tear down"); tearDownJavaRESTServer(dbName, fNames, restServerName); diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestSourceHandle.java b/test-complete/src/test/java/com/marklogic/javaclient/TestSourceHandle.java index 9f92b2b8d..f3a65bbf2 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestSourceHandle.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestSourceHandle.java @@ -1,114 +1,113 @@ -package com.marklogic.javaclient; - -import java.io.IOException; -import java.io.InputStream; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.Source; -import javax.xml.transform.TransformerException; - -import org.w3c.dom.Document; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.InputSourceHandle; -import com.marklogic.client.io.InputStreamHandle; -import com.marklogic.client.io.SourceHandle; -import com.marklogic.javaclient.BasicJavaClientREST; - -import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; -import static org.junit.Assert.*; - -import org.junit.*; -public class TestSourceHandle extends BasicJavaClientREST { - - private static String dbName = "SourceHandleDB"; - private static String [] fNames = {"SourceHandleDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - } - - -@SuppressWarnings("deprecation") -@Test public void testXmlCRUD() throws IOException, SAXException, ParserConfigurationException, TransformerException - { - String filename = "xml-original-test.xml"; - String uri = "/write-xml-sourcehandle/"; - - System.out.println("Running testXmlCRUD"); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - writeDocumentUsingInputStreamHandle(client, filename, uri, "XML"); - - // read docs - SourceHandle contentHandle = readDocumentUsingSourceHandle(client, uri + filename, "XML"); - - // get the contents - Source fileRead = contentHandle.get(); - - String readContent = convertSourceToString(fileRead); - - // get xml document for expected result - Document expectedDoc = expectedXMLDocument(filename); - - // convert actual string to xml doc - Document readDoc = convertStringToXMLDocument(readContent); - - assertXMLEqual("Write XML difference", expectedDoc, readDoc); - - // update the doc - // acquire the content for update - String updateFilename = "xml-updated-test.xml"; - updateDocumentUsingInputStreamHandle(client, updateFilename, uri + filename, "XML"); - - // read the document - SourceHandle updateHandle = readDocumentUsingSourceHandle(client, uri + filename, "XML"); - - // get the contents - Source fileReadUpdate = updateHandle.get(); - - String readContentUpdate = convertSourceToString(fileReadUpdate); - - // get xml document for expected result - Document expectedDocUpdate = expectedXMLDocument(updateFilename); - - // convert actual string to xml doc - Document readDocUpdate = convertStringToXMLDocument(readContentUpdate); - - assertXMLEqual("Write XML difference", expectedDocUpdate, readDocUpdate); - - // delete the document - deleteDocument(client, uri + filename, "XML"); - - // read the deleted document - String exception = ""; - try - { - InputStreamHandle deleteHandle = readDocumentUsingInputStreamHandle(client, uri + filename, "XML"); - } catch (Exception e) { exception = e.toString(); } - - String expectedException = "Could not read non-existent document"; - boolean documentIsDeleted = exception.contains(expectedException); - assertTrue("Document is not deleted", documentIsDeleted); - - // release client - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } +package com.marklogic.javaclient; + +import java.io.IOException; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.Source; +import javax.xml.transform.TransformerException; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.InputStreamHandle; +import com.marklogic.client.io.SourceHandle; +import com.marklogic.javaclient.BasicJavaClientREST; + +import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; +import static org.junit.Assert.*; + +import org.junit.*; + +public class TestSourceHandle extends BasicJavaClientREST { + + private static String dbName = "SourceHandleDB"; + private static String [] fNames = {"SourceHandleDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + } + + @Test + public void testXmlCRUD() throws IOException, SAXException, ParserConfigurationException, TransformerException + { + String filename = "xml-original-test.xml"; + String uri = "/write-xml-sourcehandle/"; + + System.out.println("Running testXmlCRUD"); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + writeDocumentUsingInputStreamHandle(client, filename, uri, "XML"); + + // read docs + SourceHandle contentHandle = readDocumentUsingSourceHandle(client, uri + filename, "XML"); + + // get the contents + Source fileRead = contentHandle.get(); + + String readContent = convertSourceToString(fileRead); + + // get xml document for expected result + Document expectedDoc = expectedXMLDocument(filename); + + // convert actual string to xml doc + Document readDoc = convertStringToXMLDocument(readContent); + + assertXMLEqual("Write XML difference", expectedDoc, readDoc); + + // update the doc + // acquire the content for update + String updateFilename = "xml-updated-test.xml"; + updateDocumentUsingInputStreamHandle(client, updateFilename, uri + filename, "XML"); + + // read the document + SourceHandle updateHandle = readDocumentUsingSourceHandle(client, uri + filename, "XML"); + + // get the contents + Source fileReadUpdate = updateHandle.get(); + + String readContentUpdate = convertSourceToString(fileReadUpdate); + + // get xml document for expected result + Document expectedDocUpdate = expectedXMLDocument(updateFilename); + + // convert actual string to xml doc + Document readDocUpdate = convertStringToXMLDocument(readContentUpdate); + + assertXMLEqual("Write XML difference", expectedDocUpdate, readDocUpdate); + + // delete the document + deleteDocument(client, uri + filename, "XML"); + + // read the deleted document + String exception = ""; + try + { + readDocumentUsingInputStreamHandle(client, uri + filename, "XML"); + } + catch (Exception e) { exception = e.toString(); } + + String expectedException = "Could not read non-existent document"; + boolean documentIsDeleted = exception.contains(expectedException); + assertTrue("Document is not deleted", documentIsDeleted); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + + } } \ No newline at end of file diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestStandaloneGeoQuery.java b/test-complete/src/test/java/com/marklogic/javaclient/TestStandaloneGeoQuery.java index 90efd977a..1a333057f 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestStandaloneGeoQuery.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestStandaloneGeoQuery.java @@ -1,477 +1,464 @@ -package com.marklogic.javaclient; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; -import static org.junit.Assert.*; - -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.admin.ServerConfigurationManager; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.query.StructuredQueryBuilder; -import com.marklogic.client.query.StructuredQueryDefinition; -import com.marklogic.client.query.StructuredQueryBuilder.FragmentScope; -import org.junit.*; -public class TestStandaloneGeoQuery extends BasicJavaClientREST { - - private static String dbName = "TestStandaloneGeoQueryDB"; - private static String [] fNames = {"TestStandaloneGeoQueryDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesGeoConstraint(dbName); - } - -@SuppressWarnings("deprecation") -@Test public void testBug22184() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testBug22184"); - - String[] filenames = {"geo-constraint1.xml", "geo-constraint2.xml", "geo-constraint3.xml", "geo-constraint4.xml", "geo-constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/standalone-geo-query/", "XML"); - } - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); - StructuredQueryDefinition t = qb.geospatial(qb.geoElement(qb.element("g-elem-pair"))); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(t, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - String result = convertXMLDocumentToString(resultDoc); - System.out.println(result); - assertTrue("Results are not proper", result.contains("start=\"1\" total=\"5\"")); - //assertXpathEvaluatesTo("5", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - //assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testStandaloneGeoElemQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testStandaloneGeoElemQuery"); - - String[] filenames = {"geo-constraint1.xml", "geo-constraint2.xml", "geo-constraint3.xml", "geo-constraint4.xml", "geo-constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/standalone-geo-query/", "XML"); - } - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); - StructuredQueryDefinition t = qb.geospatial(qb.geoElement(qb.element("g-elem-pair")),qb.point(12, 5)); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(t, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("5", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - //assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testStandaloneGeoElemPairQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testStandaloneGeoElemPairQuery"); - - String[] filenames = {"geo-constraint1.xml", "geo-constraint2.xml", "geo-constraint3.xml", "geo-constraint4.xml", "geo-constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/standalone-geo-query/", "XML"); - } - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); - StructuredQueryDefinition t = qb.geospatial(qb.geoElementPair(qb.element("g-elem-pair"), qb.element("lat"), qb.element("long")), qb.point(12, 5)); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(t, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - //assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testStandaloneGeoElemPairQueryEnhanced() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testStandaloneGeoElemPairQueryEnhanced"); - - String[] filenames = {"geo-constraint1.xml", "geo-constraint2.xml", "geo-constraint3.xml", "geo-constraint4.xml", "geo-constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/standalone-geo-query/", "XML"); - } - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); - //StructuredQueryDefinition t = qb.geospatial(qb.geoElementPair(qb.element("g-elem-pair"), qb.element("lat"), qb.element("long")), qb.point(12, 5)); - String[] options = {"coordinate-system=wgs84","units=miles"}; - StructuredQueryDefinition t = qb.geospatial(qb.geoElementPair(qb.element("g-elem-pair"), qb.element("lat"), qb.element("long")), FragmentScope.DOCUMENTS, options, qb.point(12, 5)); - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(t, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - //assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testStandaloneGeoElemChildQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testStandaloneGeoElemQuery"); - - String[] filenames = {"geo-constraint1.xml", "geo-constraint2.xml", "geo-constraint3.xml", "geo-constraint4.xml", "geo-constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/standalone-geo-query/", "XML"); - } - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); - StructuredQueryDefinition t = qb.geospatial(qb.geoElement(qb.element("g-elem-child-parent"), qb.element("g-elem-child-point")), qb.point(12, 5)); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(t, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - //assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testStandaloneGeoElemChildQueryEnhanced() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException -{ - System.out.println("Running testStandaloneGeoElemQueryEnhanced"); - - String[] filenames = {"geo-constraint1.xml", "geo-constraint2.xml", "geo-constraint3.xml", "geo-constraint4.xml", "geo-constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/standalone-geo-query/", "XML"); - } - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); - String[] options = {"coordinate-system=wgs84","units=miles"}; - StructuredQueryDefinition t = qb.geospatial(qb.geoElement(qb.element("g-elem-child-parent"), qb.element("g-elem-child-point")), FragmentScope.DOCUMENTS, options, qb.point(12, 5)); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(t, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - //assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); -} - - -@SuppressWarnings("deprecation") -@Test public void testStandaloneGeoAttrPairQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testStandaloneGeoAttrPairQuery"); - - String[] filenames = {"geo-constraint1.xml", "geo-constraint2.xml", "geo-constraint3.xml", "geo-constraint4.xml", "geo-constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/standalone-geo-query/", "XML"); - } - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); - StructuredQueryDefinition t = qb.geospatial(qb.geoAttributePair(qb.element("g-attr-pair"), qb.attribute("lat"), qb.attribute("long")), qb.point(12, 5)); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(t, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - //assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testStandaloneGeoAttrPairQueryBox() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testStandaloneGeoAttrPairQueryBox"); - - String[] filenames = {"geo-constraint1.xml", "geo-constraint2.xml", "geo-constraint3.xml", "geo-constraint4.xml", "geo-constraint5.xml","element-attribute-pair-geo-data.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/standalone-geo-query/", "XML"); - } - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); - StructuredQueryDefinition t = qb.geospatial(qb.geoAttributePair(qb.element("point"), qb.attribute("latitude"), qb.attribute("longitude")), qb.box(52,172,55,-163)); - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(t, resultsHandle); - // get the result - Document resultDoc = resultsHandle.get(); - System.out.println("Results of Box :"+ convertXMLDocumentToString(resultDoc)); - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertTrue("Result returned is wrong", convertXMLDocumentToString(resultDoc).contains("beijing city in china bangkok city in thailand norh pole place where Santa lives")); - - //Circle Query - QueryManager queryMgr1 = client.newQueryManager(); - // create query def - StructuredQueryBuilder qb1 = queryMgr.newStructuredQueryBuilder(); - StructuredQueryDefinition t1 = qb1.geospatial(qb1.geoAttributePair(qb1.element("point"), qb1.attribute("latitude"), qb1.attribute("longitude")), qb1.circle(qb1.point(53.90, -166.70),3)); - // create handle - DOMHandle resultsHandle1 = new DOMHandle(); - queryMgr1.search(t1, resultsHandle1); - // get the result - Document resultDoc1 = resultsHandle1.get(); - System.out.println("Results of Circle :"+ convertXMLDocumentToString(resultDoc1)); - assertTrue("Result returned is wrong", convertXMLDocumentToString(resultDoc1).contains("beijing city in china bangkok city in thailand norh pole place where Santa lives")); - //Polygon Query - QueryManager queryMgr2 = client.newQueryManager(); - // create query def - StructuredQueryBuilder qb2 = queryMgr.newStructuredQueryBuilder(); - StructuredQueryDefinition t2 = qb2.geospatial(qb2.geoAttributePair(qb2.element("point"), qb2.attribute("latitude"), qb2.attribute("longitude")), qb2.polygon(qb2.point(54,-165),qb2.point(52,-167),qb2.point(53,167),qb2.point(54,-165))); - // create handle - DOMHandle resultsHandle2 = new DOMHandle(); - queryMgr2.search(t2, resultsHandle2); - // get the result - Document resultDoc2 = resultsHandle2.get(); - System.out.println("Results of Polygon :"+ convertXMLDocumentToString(resultDoc2)); - assertTrue("Result returned is wrong", convertXMLDocumentToString(resultDoc2).contains("beijing city in china bangkok city in thailand norh pole place where Santa lives")); - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testStandaloneGeoAttrPairQueryWithOrAndNear() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testStandaloneGeoAttrPairQueryWithOr"); - - String[] filenames = {"geo-constraint1.xml", "geo-constraint2.xml", "geo-constraint3.xml", "geo-constraint4.xml", "geo-constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/standalone-geo-query/", "XML"); - } - - QueryManager queryMgr = client.newQueryManager(); - - // create OR query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); - StructuredQueryDefinition x = qb.geospatial(qb.geoAttributePair(qb.element("g-attr-pair"), qb.attribute("lat"), qb.attribute("long")), qb.point(12, 5)); - StructuredQueryDefinition y = qb.word(qb.element("name"), "karl_gale"); - StructuredQueryDefinition z = qb.or(x, y); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(z, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - System.out.println("Result of OR Query"+convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - //assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - - // create AND query def - StructuredQueryDefinition p = qb.geospatial(qb.geoAttributePair(qb.element("g-attr-pair"), qb.attribute("lat"), qb.attribute("long")), qb.point(12, 6)); - StructuredQueryDefinition q = qb.word(qb.element("name"), "karl_gale"); - StructuredQueryDefinition r = qb.and(p,q); - - // create handle - DOMHandle resultsHandle1 = new DOMHandle(); - queryMgr.search(r, resultsHandle1); - - // get the result - Document resultDoc1 = resultsHandle1.get(); - System.out.println("Results of AND Query"+convertXMLDocumentToString(resultDoc1)); - - assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - // release client - // create NEAR query def - StructuredQueryDefinition c = qb.near(z,r); - - // create handle - DOMHandle resultsHandle2 = new DOMHandle(); - queryMgr.search(c, resultsHandle2); - - // get the result - Document resultDoc2 = resultsHandle2.get(); - System.out.println("Results of NEAR Query"+convertXMLDocumentToString(resultDoc2)); - - - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } -} +package com.marklogic.javaclient; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; +import static org.junit.Assert.*; + +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.admin.ServerConfigurationManager; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.StructuredQueryBuilder; +import com.marklogic.client.query.StructuredQueryDefinition; +import com.marklogic.client.query.StructuredQueryBuilder.FragmentScope; +import org.junit.*; +public class TestStandaloneGeoQuery extends BasicJavaClientREST { + + private static String dbName = "TestStandaloneGeoQueryDB"; + private static String [] fNames = {"TestStandaloneGeoQueryDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesGeoConstraint(dbName); + } + + @Test + public void testBug22184() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testBug22184"); + + String[] filenames = {"geo-constraint1.xml", "geo-constraint2.xml", "geo-constraint3.xml", "geo-constraint4.xml", "geo-constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/standalone-geo-query/", "XML"); + } + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); + StructuredQueryDefinition t = qb.geospatial(qb.geoElement(qb.element("g-elem-pair"))); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(t, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + String result = convertXMLDocumentToString(resultDoc); + System.out.println(result); + assertTrue("Results are not proper", result.contains("start=\"1\" total=\"5\"")); + + // release client + client.release(); + } + + @Test + public void testStandaloneGeoElemQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testStandaloneGeoElemQuery"); + + String[] filenames = {"geo-constraint1.xml", "geo-constraint2.xml", "geo-constraint3.xml", "geo-constraint4.xml", "geo-constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/standalone-geo-query/", "XML"); + } + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); + StructuredQueryDefinition t = qb.geospatial(qb.geoElement(qb.element("g-elem-pair")),qb.point(12, 5)); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(t, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("5", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testStandaloneGeoElemPairQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testStandaloneGeoElemPairQuery"); + + String[] filenames = {"geo-constraint1.xml", "geo-constraint2.xml", "geo-constraint3.xml", "geo-constraint4.xml", "geo-constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/standalone-geo-query/", "XML"); + } + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); + StructuredQueryDefinition t = qb.geospatial(qb.geoElementPair(qb.element("g-elem-pair"), qb.element("lat"), qb.element("long")), qb.point(12, 5)); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(t, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testStandaloneGeoElemPairQueryEnhanced() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testStandaloneGeoElemPairQueryEnhanced"); + + String[] filenames = {"geo-constraint1.xml", "geo-constraint2.xml", "geo-constraint3.xml", "geo-constraint4.xml", "geo-constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/standalone-geo-query/", "XML"); + } + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); + //StructuredQueryDefinition t = qb.geospatial(qb.geoElementPair(qb.element("g-elem-pair"), qb.element("lat"), qb.element("long")), qb.point(12, 5)); + String[] options = {"coordinate-system=wgs84","units=miles"}; + StructuredQueryDefinition t = qb.geospatial(qb.geoElementPair(qb.element("g-elem-pair"), qb.element("lat"), qb.element("long")), FragmentScope.DOCUMENTS, options, qb.point(12, 5)); + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(t, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testStandaloneGeoElemChildQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testStandaloneGeoElemQuery"); + + String[] filenames = {"geo-constraint1.xml", "geo-constraint2.xml", "geo-constraint3.xml", "geo-constraint4.xml", "geo-constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/standalone-geo-query/", "XML"); + } + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); + StructuredQueryDefinition t = qb.geospatial(qb.geoElement(qb.element("g-elem-child-parent"), qb.element("g-elem-child-point")), qb.point(12, 5)); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(t, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testStandaloneGeoElemChildQueryEnhanced() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testStandaloneGeoElemQueryEnhanced"); + + String[] filenames = {"geo-constraint1.xml", "geo-constraint2.xml", "geo-constraint3.xml", "geo-constraint4.xml", "geo-constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/standalone-geo-query/", "XML"); + } + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); + String[] options = {"coordinate-system=wgs84","units=miles"}; + StructuredQueryDefinition t = qb.geospatial(qb.geoElement(qb.element("g-elem-child-parent"), qb.element("g-elem-child-point")), FragmentScope.DOCUMENTS, options, qb.point(12, 5)); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(t, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testStandaloneGeoAttrPairQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testStandaloneGeoAttrPairQuery"); + + String[] filenames = {"geo-constraint1.xml", "geo-constraint2.xml", "geo-constraint3.xml", "geo-constraint4.xml", "geo-constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/standalone-geo-query/", "XML"); + } + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); + StructuredQueryDefinition t = qb.geospatial(qb.geoAttributePair(qb.element("g-attr-pair"), qb.attribute("lat"), qb.attribute("long")), qb.point(12, 5)); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(t, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testStandaloneGeoAttrPairQueryBox() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testStandaloneGeoAttrPairQueryBox"); + + String[] filenames = {"geo-constraint1.xml", "geo-constraint2.xml", "geo-constraint3.xml", "geo-constraint4.xml", "geo-constraint5.xml","element-attribute-pair-geo-data.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/standalone-geo-query/", "XML"); + } + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); + StructuredQueryDefinition t = qb.geospatial(qb.geoAttributePair(qb.element("point"), qb.attribute("latitude"), qb.attribute("longitude")), qb.box(52,172,55,-163)); + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(t, resultsHandle); + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println("Results of Box :"+ convertXMLDocumentToString(resultDoc)); + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertTrue("Result returned is wrong", convertXMLDocumentToString(resultDoc).contains("beijing city in china bangkok city in thailand norh pole place where Santa lives")); + + //Circle Query + QueryManager queryMgr1 = client.newQueryManager(); + // create query def + StructuredQueryBuilder qb1 = queryMgr.newStructuredQueryBuilder(); + StructuredQueryDefinition t1 = qb1.geospatial(qb1.geoAttributePair(qb1.element("point"), qb1.attribute("latitude"), qb1.attribute("longitude")), qb1.circle(qb1.point(53.90, -166.70),3)); + // create handle + DOMHandle resultsHandle1 = new DOMHandle(); + queryMgr1.search(t1, resultsHandle1); + // get the result + Document resultDoc1 = resultsHandle1.get(); + System.out.println("Results of Circle :"+ convertXMLDocumentToString(resultDoc1)); + assertTrue("Result returned is wrong", convertXMLDocumentToString(resultDoc1).contains("beijing city in china bangkok city in thailand norh pole place where Santa lives")); + //Polygon Query + QueryManager queryMgr2 = client.newQueryManager(); + // create query def + StructuredQueryBuilder qb2 = queryMgr.newStructuredQueryBuilder(); + StructuredQueryDefinition t2 = qb2.geospatial(qb2.geoAttributePair(qb2.element("point"), qb2.attribute("latitude"), qb2.attribute("longitude")), qb2.polygon(qb2.point(54,-165),qb2.point(52,-167),qb2.point(53,167),qb2.point(54,-165))); + // create handle + DOMHandle resultsHandle2 = new DOMHandle(); + queryMgr2.search(t2, resultsHandle2); + // get the result + Document resultDoc2 = resultsHandle2.get(); + System.out.println("Results of Polygon :"+ convertXMLDocumentToString(resultDoc2)); + assertTrue("Result returned is wrong", convertXMLDocumentToString(resultDoc2).contains("beijing city in china bangkok city in thailand norh pole place where Santa lives")); + // release client + client.release(); + } + + @Test + public void testStandaloneGeoAttrPairQueryWithOrAndNear() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testStandaloneGeoAttrPairQueryWithOr"); + + String[] filenames = {"geo-constraint1.xml", "geo-constraint2.xml", "geo-constraint3.xml", "geo-constraint4.xml", "geo-constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/standalone-geo-query/", "XML"); + } + + QueryManager queryMgr = client.newQueryManager(); + + // create OR query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); + StructuredQueryDefinition x = qb.geospatial(qb.geoAttributePair(qb.element("g-attr-pair"), qb.attribute("lat"), qb.attribute("long")), qb.point(12, 5)); + StructuredQueryDefinition y = qb.word(qb.element("name"), "karl_gale"); + StructuredQueryDefinition z = qb.or(x, y); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(z, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println("Result of OR Query"+convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + + // create AND query def + StructuredQueryDefinition p = qb.geospatial(qb.geoAttributePair(qb.element("g-attr-pair"), qb.attribute("lat"), qb.attribute("long")), qb.point(12, 6)); + StructuredQueryDefinition q = qb.word(qb.element("name"), "karl_gale"); + StructuredQueryDefinition r = qb.and(p,q); + + // create handle + DOMHandle resultsHandle1 = new DOMHandle(); + queryMgr.search(r, resultsHandle1); + + // get the result + Document resultDoc1 = resultsHandle1.get(); + System.out.println("Results of AND Query"+convertXMLDocumentToString(resultDoc1)); + + assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + // release client + // create NEAR query def + StructuredQueryDefinition c = qb.near(z,r); + + // create handle + DOMHandle resultsHandle2 = new DOMHandle(); + queryMgr.search(c, resultsHandle2); + + // get the result + Document resultDoc2 = resultsHandle2.get(); + System.out.println("Results of NEAR Query"+convertXMLDocumentToString(resultDoc2)); + + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} From eb3b5f2c8ef711f60aedd3a88805664d05c566f3 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Thu, 21 Aug 2014 09:07:36 -0700 Subject: [PATCH 083/357] Code cleanup --- .../javaclient/TestStandaloneQuery.java | 948 +++++++------- .../javaclient/TestStringHandle.java | 532 ++++---- .../javaclient/TestStructuredQuery.java | 1097 ++++++++--------- .../TestStructuredQueryMildNot.java | 181 ++- .../javaclient/TestStructuredSearchGeo.java | 338 ++--- .../javaclient/TestTransformXMLWithXSLT.java | 188 +-- .../javaclient/TestValueConstraint.java | 449 +++---- .../javaclient/TestWordConstraint.java | 130 +- .../javaclient/TestWriteTextDoc.java | 77 +- .../javaclient/TestXMLDocumentRepair.java | 194 +-- .../javaclient/TestXMLEventReaderHandle.java | 240 ++-- .../javaclient/TestXMLMultiByte.java | 192 ++- .../javaclient/TestXMLStreamReaderHandle.java | 176 ++- .../com/marklogic/javaclient/ThreadClass.java | 92 +- .../marklogic/javaclient/ThreadSearch.java | 125 +- .../com/marklogic/javaclient/ThreadWrite.java | 101 +- 16 files changed, 2487 insertions(+), 2573 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestStandaloneQuery.java b/test-complete/src/test/java/com/marklogic/javaclient/TestStandaloneQuery.java index a4a284241..a1bc235b2 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestStandaloneQuery.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestStandaloneQuery.java @@ -1,477 +1,471 @@ -package com.marklogic.javaclient; - -import java.io.IOException; - -import javax.xml.namespace.QName; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.admin.ServerConfigurationManager; -import com.marklogic.client.query.StructuredQueryBuilder; -import com.marklogic.client.query.StructuredQueryBuilder.FragmentScope; -import com.marklogic.client.query.StructuredQueryBuilder.Operator; -import com.marklogic.client.query.StructuredQueryDefinition; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.DocumentMetadataHandle; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; -import static org.junit.Assert.*; - -import org.custommonkey.xmlunit.exceptions.XpathException; - -import org.junit.*; -public class TestStandaloneQuery extends BasicJavaClientREST { - - private static String dbName = "TestStandaloneQueryDB"; - private static String [] fNames = {"TestStandaloneQueryDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - } - - -@SuppressWarnings("deprecation") -@Test public void testStandaloneWordQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testStandaloneWordQuery"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/standalone-query/", "XML"); - } - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); - StructuredQueryDefinition t = qb.word(qb.element("id"), "0026"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(t, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - System.out.println("Output of Search : "+convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - //assertXpathEvaluatesTo("0026", "string(//*[local-name()='highlight'][1]//*[local-name()='id'])", resultDoc); - assertTrue("Proper result is not returned :", convertXMLDocumentToString(resultDoc).contains("0026")); - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testStandaloneWordQueryEnhanced() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testStandaloneWordQueryEnhanced"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/standalone-query/", "XML"); - } - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); - //StructuredQueryDefinition t = qb.word(qb.element(new QName("http://cloudbank.com", "price")), "0026"); - String[] options = {"case-insensitive","stemmed","unwildcarded"}; - StructuredQueryDefinition t = qb.word(qb.elementAttribute(qb.element(new QName("http://cloudbank.com", "price")), qb.attribute("amt")), FragmentScope.DOCUMENTS, options ,.025 , "123.45"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(t, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - String output=convertXMLDocumentToString(resultDoc); - System.out.println(output); - System.out.println("Search Result : " + resultDoc.getDocumentURI()); - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertTrue("Results are not proper",output.contains("uri=\"/standalone-query/constraint5.xml\"") ); - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testStandaloneRangeQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testStandaloneRangeQuery"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/standalone-query/", "XML"); - } - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); - StructuredQueryDefinition t = qb.range(qb.element("popularity"), "xs:integer", Operator.GE, 4); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(t, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("4", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - //assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testStandaloneRangeQueryEnhanced() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testStandaloneRangeQueryEnhanced"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/standalone-query/", "XML"); - } - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); - String collation = "http://marklogic.com/collation/en"; - StructuredQueryDefinition t =qb.range(qb.element("popularity"), "xs:integer", collation, Operator.GE, 4); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(t, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("4", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - //assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testStandaloneRangeQueryEnhanced1() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testStandaloneRangeQueryEnhanced1"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/standalone-query/", "XML"); - } - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); - String[] options = {"uncached","min-occurs=2"}; - StructuredQueryDefinition t =qb.range(qb.element("popularity"), "xs:integer", options, Operator.LE, 4); - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(t, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - //assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testStandaloneRangeQueryEnhanced2() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testStandaloneRangeQueryEnhanced2"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/standalone-query/", "XML"); - } - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); - String[] options = {"uncached","min-occurs=2"}; - String collation = "http://marklogic.com/collation/en"; - StructuredQueryDefinition t =qb.range(qb.element("popularity"), "xs:integer", collation, options, Operator.LT, 4); - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(t, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - //assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testStandaloneRangeQueryEnhanced3() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testStandaloneRangeQueryEnhanced3"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/standalone-query/", "XML"); - } - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); - String collation = "http://marklogic.com/collation/en"; - StructuredQueryDefinition t =qb.range(qb.element("popularity"), "xs:integer", collation, FragmentScope.DOCUMENTS, Operator.GT, 4); - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(t, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("3", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - //assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testStandaloneRangeQueryEnhanced4() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testStandaloneRangeQueryEnhanced4"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/standalone-query/", "XML"); - } - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); - String collation = "http://marklogic.com/collation/en"; - String[] options = {"uncached","min-occurs=2"}; - - StructuredQueryDefinition t =qb.range(qb.element("popularity"), "xs:integer", collation, FragmentScope.DOCUMENTS, options, Operator.NE, 4); - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(t, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("4", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - //assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testStandaloneValueQueryOnAttribute() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testStandaloneValueQueryOnAttribute"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/standalone-query/", "XML"); - } - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); - StructuredQueryDefinition t = qb.value(qb.elementAttribute(qb.element(new QName("http://cloudbank.com", "price")), qb.attribute("amt")), "0.1"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(t, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - //assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testStandaloneValueQueryOnAttributeEnhanced() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testStandaloneValueQueryOnAttributeEnhanced"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/standalone-query/", "XML"); - } - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); - //StructuredQueryDefinition t = qb.value(qb.elementAttribute(qb.element(new QName("http://cloudbank.com", "price")), qb.attribute("amt")), "0.1"); - String[] options = {"case-insensitive","stemmed","unwildcarded"}; - StructuredQueryDefinition t1 = qb.value(qb.elementAttribute(qb.element(new QName("http://cloudbank.com", "price")), qb.attribute("amt")), FragmentScope.DOCUMENTS, options , 3.0, "123.45"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(t1, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - String output = convertXMLDocumentToString(resultDoc); - System.out.println(output); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertTrue("The result is not proper", output.contains("/standalone-query/constraint5.xml")); - // release client - client.release(); - } -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} +package com.marklogic.javaclient; + +import java.io.IOException; + +import javax.xml.namespace.QName; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.admin.ServerConfigurationManager; +import com.marklogic.client.query.StructuredQueryBuilder; +import com.marklogic.client.query.StructuredQueryBuilder.FragmentScope; +import com.marklogic.client.query.StructuredQueryBuilder.Operator; +import com.marklogic.client.query.StructuredQueryDefinition; +import com.marklogic.client.io.DOMHandle; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; +import org.custommonkey.xmlunit.exceptions.XpathException; +import static org.junit.Assert.*; +import org.junit.*; + +public class TestStandaloneQuery extends BasicJavaClientREST { + + private static String dbName = "TestStandaloneQueryDB"; + private static String [] fNames = {"TestStandaloneQueryDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + } + + @Test + public void testStandaloneWordQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testStandaloneWordQuery"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/standalone-query/", "XML"); + } + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); + StructuredQueryDefinition t = qb.word(qb.element("id"), "0026"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(t, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println("Output of Search : "+convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertTrue("Proper result is not returned :", convertXMLDocumentToString(resultDoc).contains("0026")); + + // release client + client.release(); + } + + @Test + public void testStandaloneWordQueryEnhanced() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testStandaloneWordQueryEnhanced"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/standalone-query/", "XML"); + } + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); + //StructuredQueryDefinition t = qb.word(qb.element(new QName("http://cloudbank.com", "price")), "0026"); + String[] options = {"case-insensitive","stemmed","unwildcarded"}; + StructuredQueryDefinition t = qb.word(qb.elementAttribute(qb.element(new QName("http://cloudbank.com", "price")), qb.attribute("amt")), FragmentScope.DOCUMENTS, options ,.025 , "123.45"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(t, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + String output=convertXMLDocumentToString(resultDoc); + System.out.println(output); + System.out.println("Search Result : " + resultDoc.getDocumentURI()); + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertTrue("Results are not proper",output.contains("uri=\"/standalone-query/constraint5.xml\"") ); + + // release client + client.release(); + } + + @Test + public void testStandaloneRangeQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testStandaloneRangeQuery"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/standalone-query/", "XML"); + } + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); + StructuredQueryDefinition t = qb.range(qb.element("popularity"), "xs:integer", Operator.GE, 4); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(t, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("4", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testStandaloneRangeQueryEnhanced() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testStandaloneRangeQueryEnhanced"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/standalone-query/", "XML"); + } + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); + String collation = "http://marklogic.com/collation/en"; + StructuredQueryDefinition t =qb.range(qb.element("popularity"), "xs:integer", collation, Operator.GE, 4); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(t, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("4", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testStandaloneRangeQueryEnhanced1() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testStandaloneRangeQueryEnhanced1"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/standalone-query/", "XML"); + } + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); + String[] options = {"uncached","min-occurs=2"}; + StructuredQueryDefinition t =qb.range(qb.element("popularity"), "xs:integer", options, Operator.LE, 4); + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(t, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testStandaloneRangeQueryEnhanced2() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testStandaloneRangeQueryEnhanced2"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/standalone-query/", "XML"); + } + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); + String[] options = {"uncached","min-occurs=2"}; + String collation = "http://marklogic.com/collation/en"; + StructuredQueryDefinition t =qb.range(qb.element("popularity"), "xs:integer", collation, options, Operator.LT, 4); + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(t, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testStandaloneRangeQueryEnhanced3() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testStandaloneRangeQueryEnhanced3"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/standalone-query/", "XML"); + } + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); + String collation = "http://marklogic.com/collation/en"; + StructuredQueryDefinition t =qb.range(qb.element("popularity"), "xs:integer", collation, FragmentScope.DOCUMENTS, Operator.GT, 4); + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(t, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("3", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testStandaloneRangeQueryEnhanced4() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testStandaloneRangeQueryEnhanced4"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/standalone-query/", "XML"); + } + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); + String collation = "http://marklogic.com/collation/en"; + String[] options = {"uncached","min-occurs=2"}; + + StructuredQueryDefinition t =qb.range(qb.element("popularity"), "xs:integer", collation, FragmentScope.DOCUMENTS, options, Operator.NE, 4); + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(t, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("4", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testStandaloneValueQueryOnAttribute() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testStandaloneValueQueryOnAttribute"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/standalone-query/", "XML"); + } + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); + StructuredQueryDefinition t = qb.value(qb.elementAttribute(qb.element(new QName("http://cloudbank.com", "price")), qb.attribute("amt")), "0.1"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(t, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testStandaloneValueQueryOnAttributeEnhanced() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testStandaloneValueQueryOnAttributeEnhanced"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/standalone-query/", "XML"); + } + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(); + //StructuredQueryDefinition t = qb.value(qb.elementAttribute(qb.element(new QName("http://cloudbank.com", "price")), qb.attribute("amt")), "0.1"); + String[] options = {"case-insensitive","stemmed","unwildcarded"}; + StructuredQueryDefinition t1 = qb.value(qb.elementAttribute(qb.element(new QName("http://cloudbank.com", "price")), qb.attribute("amt")), FragmentScope.DOCUMENTS, options , 3.0, "123.45"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(t1, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + String output = convertXMLDocumentToString(resultDoc); + System.out.println(output); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertTrue("The result is not proper", output.contains("/standalone-query/constraint5.xml")); + + // release client + client.release(); + } + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestStringHandle.java b/test-complete/src/test/java/com/marklogic/javaclient/TestStringHandle.java index 2082c2e71..b5c4a5f47 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestStringHandle.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestStringHandle.java @@ -1,275 +1,257 @@ -package com.marklogic.javaclient; - -import java.io.File; -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; - -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.FileHandle; -import com.marklogic.client.io.StringHandle; -import com.marklogic.javaclient.BasicJavaClientREST; - -import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; -import static org.junit.Assert.*; - -import org.junit.*; -public class TestStringHandle extends BasicJavaClientREST { - - private static String dbName = "StringDB"; - private static String [] fNames = {"StringDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - } - - -@SuppressWarnings("deprecation") -@Test public void testXmlCRUD() throws IOException, SAXException, ParserConfigurationException - { - String filename = "xml-original-test.xml"; - String uri = "/write-xml-string/"; - - System.out.println("Running testXmlCRUD"); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - writeDocumentUsingStringHandle(client, filename, uri, "XML"); - - // read docs - StringHandle contentHandle = readDocumentUsingStringHandle(client, uri + filename, "XML"); - - // get the contents - // File fileRead = contentHandle.get(); - - String readContent = contentHandle.get(); - - // get xml document for expected result - Document expectedDoc = expectedXMLDocument(filename); - - // convert actual string to xml doc - Document readDoc = convertStringToXMLDocument(readContent); - - assertXMLEqual("Write XML difference", expectedDoc, readDoc); - - // update the doc - // acquire the content for update - String updateFilename = "xml-updated-test.xml"; - updateDocumentUsingStringHandle(client, updateFilename, uri + filename, "XML"); - - // read the document - StringHandle updateHandle = readDocumentUsingStringHandle(client, uri + filename, "XML"); - - // get the contents - String readContentUpdate = updateHandle.get(); - -// String readContentUpdate = convertFileToString(fileReadUpdate); - - // get xml document for expected result - Document expectedDocUpdate = expectedXMLDocument(updateFilename); - - // convert actual string to xml doc - Document readDocUpdate = convertStringToXMLDocument(readContentUpdate); - - assertXMLEqual("Write XML difference", expectedDocUpdate, readDocUpdate); - - // delete the document - deleteDocument(client, uri + filename, "XML"); - - // read the deleted document - String exception = ""; - try - { - FileHandle deleteHandle = readDocumentUsingFileHandle(client, uri + filename, "XML"); - } catch (Exception e) { exception = e.toString(); } - - String expectedException = "com.marklogic.client.ResourceNotFoundException: Local message: Could not read non-existent document. Server Message: RESTAPI-NODOCUMENT: (err:FOER0000) Resource or document does not exist: category: content message: /write-xml-string/xml-original-test.xml"; - assertEquals("Document is not deleted", expectedException, exception); - - //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "XML")); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testTextCRUD() throws IOException - { - String filename = "text-original.txt"; - String uri = "/write-text-stringhandle/"; - - System.out.println("Running testTextCRUD"); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - writeDocumentUsingStringHandle(client, filename, uri, "Text"); - - // read docs - StringHandle contentHandle = readDocumentUsingStringHandle(client, uri + filename, "Text"); - - // get the contents -// File fileRead = contentHandle.get(); - - String readContent = contentHandle.get(); - - String expectedContent = "hello world, welcome to java API"; - - assertEquals("Write Text difference", expectedContent.trim(), readContent.trim()); - - // update the doc - // acquire the content for update - String updateFilename = "text-updated.txt"; - updateDocumentUsingStringHandle(client, updateFilename, uri + filename, "Text"); - - // read the document - StringHandle updateHandle = readDocumentUsingStringHandle(client, uri + filename, "Text"); - - // get the contents -// File fileReadUpdate = updateHandle.get(); - - String readContentUpdate = updateHandle.get(); - - String expectedContentUpdate = "hello world, welcome to java API after new updates"; - - assertEquals("Write Text difference", expectedContentUpdate.trim(), readContentUpdate.toString().trim()); - - // delete the document - deleteDocument(client, uri + filename, "Text"); - - // read the deleted document - //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "Text")); - - String exception = ""; - try - { - FileHandle deleteHandle = readDocumentUsingFileHandle(client, uri + filename, "Text"); - } catch (Exception e) { exception = e.toString(); } - - String expectedException = "com.marklogic.client.ResourceNotFoundException: Local message: Could not read non-existent document. Server Message: RESTAPI-NODOCUMENT: (err:FOER0000) Resource or document does not exist: category: content message: /write-text-stringhandle/text-original.txt"; - assertEquals("Document is not deleted", expectedException, exception); - - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testJsonCRUD() throws IOException - { - String filename = "json-original.json"; - String uri = "/write-json-stringhandle/"; - - System.out.println("Running testJsonCRUD"); - - ObjectMapper mapper = new ObjectMapper(); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - writeDocumentUsingStringHandle(client, filename, uri, "JSON"); - - // read docs - StringHandle contentHandle = readDocumentUsingStringHandle(client, uri + filename, "JSON"); - - // get the contents -// File fileRead = contentHandle.get(); - -// JsonNode readContent = mapper.readTree(fileRead); - JsonNode readContent = mapper.readValue(contentHandle.get(),JsonNode.class); - - - // get expected contents - JsonNode expectedContent = expectedJSONDocument(filename); - - assertTrue("Write JSON document difference", readContent.equals(expectedContent)); - - // update the doc - // acquire the content for update - String updateFilename = "json-updated.json"; - updateDocumentUsingFileHandle(client, updateFilename, uri + filename, "JSON"); - - // read the document - FileHandle updateHandle = readDocumentUsingFileHandle(client, uri + filename, "JSON"); - - // get the contents - File fileReadUpdate = updateHandle.get(); - - JsonNode readContentUpdate = mapper.readTree(fileReadUpdate); - - // get expected contents - JsonNode expectedContentUpdate = expectedJSONDocument(updateFilename); - - assertTrue("Write JSON document difference", readContentUpdate.equals(expectedContentUpdate)); - - // delete the document - deleteDocument(client, uri + filename, "JSON"); - - // read the deleted document - //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "JSON")); - - String exception = ""; - try - { - FileHandle deleteHandle = readDocumentUsingFileHandle(client, uri + filename, "JSON"); - } catch (Exception e) { exception = e.toString(); } - - String expectedException = "com.marklogic.client.ResourceNotFoundException: Local message: Could not read non-existent document. Server Message: RESTAPI-NODOCUMENT: (err:FOER0000) Resource or document does not exist: category: content message: /write-json-stringhandle/json-original.json"; - assertEquals("Document is not deleted", expectedException, exception); - - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testBug22356() throws IOException, SAXException, ParserConfigurationException - { - System.out.println("Running testBug22356"); - - String filename = "xml-original-test.xml"; - String uri = "/write-xml-string/"; - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // read docs - StringHandle contentHandle = null; - try - { - // get the contents - String readContent = contentHandle.get(); - } - catch (NullPointerException e) { - System.out.println("Null pointer Exception is expected noy an Empty Value"); - e.toString(); - } - - // release client - client.release(); - } - - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } -} +package com.marklogic.javaclient; + +import java.io.File; +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.FileHandle; +import com.marklogic.client.io.StringHandle; +import com.marklogic.javaclient.BasicJavaClientREST; + +import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; +import static org.junit.Assert.*; +import org.junit.*; + +public class TestStringHandle extends BasicJavaClientREST { + + private static String dbName = "StringDB"; + private static String [] fNames = {"StringDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + } + + @Test + public void testXmlCRUD() throws IOException, SAXException, ParserConfigurationException + { + String filename = "xml-original-test.xml"; + String uri = "/write-xml-string/"; + + System.out.println("Running testXmlCRUD"); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + writeDocumentUsingStringHandle(client, filename, uri, "XML"); + + // read docs + StringHandle contentHandle = readDocumentUsingStringHandle(client, uri + filename, "XML"); + + // get the contents + // File fileRead = contentHandle.get(); + + String readContent = contentHandle.get(); + + // get xml document for expected result + Document expectedDoc = expectedXMLDocument(filename); + + // convert actual string to xml doc + Document readDoc = convertStringToXMLDocument(readContent); + + assertXMLEqual("Write XML difference", expectedDoc, readDoc); + + // update the doc + // acquire the content for update + String updateFilename = "xml-updated-test.xml"; + updateDocumentUsingStringHandle(client, updateFilename, uri + filename, "XML"); + + // read the document + StringHandle updateHandle = readDocumentUsingStringHandle(client, uri + filename, "XML"); + + // get the contents + String readContentUpdate = updateHandle.get(); + + // get xml document for expected result + Document expectedDocUpdate = expectedXMLDocument(updateFilename); + + // convert actual string to xml doc + Document readDocUpdate = convertStringToXMLDocument(readContentUpdate); + + assertXMLEqual("Write XML difference", expectedDocUpdate, readDocUpdate); + + // delete the document + deleteDocument(client, uri + filename, "XML"); + + // read the deleted document + String exception = ""; + try + { + readDocumentUsingFileHandle(client, uri + filename, "XML"); + } + catch (Exception e) { exception = e.toString(); } + + String expectedException = "com.marklogic.client.ResourceNotFoundException: Local message: Could not read non-existent document. Server Message: RESTAPI-NODOCUMENT: (err:FOER0000) Resource or document does not exist: category: content message: /write-xml-string/xml-original-test.xml"; + assertEquals("Document is not deleted", expectedException, exception); + + // release client + client.release(); + } + + @Test + public void testTextCRUD() throws IOException + { + String filename = "text-original.txt"; + String uri = "/write-text-stringhandle/"; + + System.out.println("Running testTextCRUD"); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + writeDocumentUsingStringHandle(client, filename, uri, "Text"); + + // read docs + StringHandle contentHandle = readDocumentUsingStringHandle(client, uri + filename, "Text"); + + // get the contents + // File fileRead = contentHandle.get(); + + String readContent = contentHandle.get(); + + String expectedContent = "hello world, welcome to java API"; + + assertEquals("Write Text difference", expectedContent.trim(), readContent.trim()); + + // update the doc + // acquire the content for update + String updateFilename = "text-updated.txt"; + updateDocumentUsingStringHandle(client, updateFilename, uri + filename, "Text"); + + // read the document + StringHandle updateHandle = readDocumentUsingStringHandle(client, uri + filename, "Text"); + + // get the contents + // File fileReadUpdate = updateHandle.get(); + + String readContentUpdate = updateHandle.get(); + + String expectedContentUpdate = "hello world, welcome to java API after new updates"; + + assertEquals("Write Text difference", expectedContentUpdate.trim(), readContentUpdate.toString().trim()); + + // delete the document + deleteDocument(client, uri + filename, "Text"); + + String exception = ""; + try + { + readDocumentUsingFileHandle(client, uri + filename, "Text"); + } + catch (Exception e) { exception = e.toString(); } + + String expectedException = "com.marklogic.client.ResourceNotFoundException: Local message: Could not read non-existent document. Server Message: RESTAPI-NODOCUMENT: (err:FOER0000) Resource or document does not exist: category: content message: /write-text-stringhandle/text-original.txt"; + assertEquals("Document is not deleted", expectedException, exception); + + // release client + client.release(); + } + + @Test + public void testJsonCRUD() throws IOException + { + String filename = "json-original.json"; + String uri = "/write-json-stringhandle/"; + + System.out.println("Running testJsonCRUD"); + + ObjectMapper mapper = new ObjectMapper(); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + writeDocumentUsingStringHandle(client, filename, uri, "JSON"); + + // read docs + StringHandle contentHandle = readDocumentUsingStringHandle(client, uri + filename, "JSON"); + + // get the contents + JsonNode readContent = mapper.readValue(contentHandle.get(),JsonNode.class); + + // get expected contents + JsonNode expectedContent = expectedJSONDocument(filename); + + assertTrue("Write JSON document difference", readContent.equals(expectedContent)); + + // update the doc + // acquire the content for update + String updateFilename = "json-updated.json"; + updateDocumentUsingFileHandle(client, updateFilename, uri + filename, "JSON"); + + // read the document + FileHandle updateHandle = readDocumentUsingFileHandle(client, uri + filename, "JSON"); + + // get the contents + File fileReadUpdate = updateHandle.get(); + + JsonNode readContentUpdate = mapper.readTree(fileReadUpdate); + + // get expected contents + JsonNode expectedContentUpdate = expectedJSONDocument(updateFilename); + + assertTrue("Write JSON document difference", readContentUpdate.equals(expectedContentUpdate)); + + // delete the document + deleteDocument(client, uri + filename, "JSON"); + + String exception = ""; + try + { + readDocumentUsingFileHandle(client, uri + filename, "JSON"); + } + catch (Exception e) { exception = e.toString(); } + + String expectedException = "com.marklogic.client.ResourceNotFoundException: Local message: Could not read non-existent document. Server Message: RESTAPI-NODOCUMENT: (err:FOER0000) Resource or document does not exist: category: content message: /write-json-stringhandle/json-original.json"; + assertEquals("Document is not deleted", expectedException, exception); + + // release client + client.release(); + } + + @Test + public void testBug22356() throws IOException, SAXException, ParserConfigurationException + { + System.out.println("Running testBug22356"); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // read docs + StringHandle contentHandle = null; + try + { + // get the contents + contentHandle.get(); + } + catch (NullPointerException e) { + System.out.println("Null pointer Exception is expected noy an Empty Value"); + e.toString(); + } + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestStructuredQuery.java b/test-complete/src/test/java/com/marklogic/javaclient/TestStructuredQuery.java index 87453d59d..1bca8df08 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestStructuredQuery.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestStructuredQuery.java @@ -1,567 +1,530 @@ - -package com.marklogic.javaclient; - -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.admin.ServerConfigurationManager; -import com.marklogic.client.query.StructuredQueryBuilder; -import com.marklogic.client.query.StructuredQueryDefinition; -import com.marklogic.client.query.StructuredQueryBuilder.Operator; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.DocumentMetadataHandle; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.junit.*; -public class TestStructuredQuery extends BasicJavaClientREST { - - private static String dbName = "TestStructuredQueryDB"; - private static String [] fNames = {"TestStructuredQueryDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - } -@After -public void testCleanUp() throws Exception -{ - clearDB(8011); - System.out.println("Running clear script"); -} - - -@Test public void testStructuredQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testStructuredQuery"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "valueConstraintWildCardOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/structured-query/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); - StructuredQueryDefinition t = qb.valueConstraint("id", "0026"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(t, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - - // release client - client.release(); - } - - -@Test public void testStructuredQueryJSON() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testStructuredQueryJSON"); - - String[] filenames = {"constraint1.json", "constraint2.json", "constraint3.json", "constraint4.json", "constraint5.json"}; - String queryOptionName = "valueConstraintWildCardOpt.json"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/structured-query/", "JSON"); - } - setJSONQueryOption(client, queryOptionName); -// setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create value query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); - StructuredQueryDefinition t = qb.value(qb.jsonProperty("popularity"),"4"); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(t, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - System.out.println(convertXMLDocumentToString(resultDoc)); - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("/structured-query/constraint2.json", "string(//*[local-name()='result'][1]//@*[local-name()='uri'])", resultDoc); - - //create new word query def - StructuredQueryDefinition t1 = qb.word(qb.jsonProperty("id"), "0012"); - // create handle - DOMHandle resultsHandle1 = new DOMHandle(); - queryMgr.search(t1, resultsHandle1); - - // get the result - Document resultDoc1 = resultsHandle1.get(); - System.out.println(convertXMLDocumentToString(resultDoc1)); - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc1); - assertXpathEvaluatesTo("/structured-query/constraint2.json", "string(//*[local-name()='result'][1]//@*[local-name()='uri'])", resultDoc1); - - //create new range word query def - StructuredQueryDefinition t2 = qb.range(qb.jsonProperty("price"), "xs:integer", Operator.GE, "0.1"); - // create handle - DOMHandle resultsHandle2 = new DOMHandle(); - queryMgr.search(t2, resultsHandle2); - - // get the result - Document resultDoc2 = resultsHandle2.get(); - System.out.println(convertXMLDocumentToString(resultDoc2)); - assertXpathEvaluatesTo("5", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc2); - - // release client - client.release(); - } - - - -@Test public void testValueConstraintWildCard() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testValueConstraintWildCard"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "valueConstraintWildCardOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/structured-query/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); - StructuredQueryDefinition valueConstraintQuery1 = qb.valueConstraint("id", "00*2"); - StructuredQueryDefinition valueConstraintQuery2 = qb.valueConstraint("id", "0??6"); - StructuredQueryDefinition orFinalQuery = qb.or(valueConstraintQuery1, valueConstraintQuery2); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(orFinalQuery, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); - - //String expectedSearchReport = "(cts:search(fn:collection(), cts:and-query(cts:or-query((cts:element-value-query(fn:QName(\"\", \"id\"), \"00*2\", (\"lang=en\"), 1), cts:element-value-query(fn:QName(\"\", \"id\"), \"0??6\", (\"lang=en\"), 1)), ()), (\"score-logtfidf\"), 1))[1 to 10]"; - - //assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); - - // release client - client.release(); - } - - - -@Test public void testAndNotQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testAndNotQuery"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "valueConstraintWildCardOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/structured-query-andnot/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); - StructuredQueryDefinition termQuery1 = qb.term("Atlantic"); - StructuredQueryDefinition termQuery2 = qb.term("Monthly"); - StructuredQueryDefinition termQuery3 = qb.term("Bush"); - StructuredQueryDefinition andQuery = qb.and(termQuery1, termQuery2); - StructuredQueryDefinition andNotFinalQuery = qb.andNot(andQuery, termQuery3); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(andNotFinalQuery, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("0113", "string(//*[local-name()='result']//*[local-name()='id'])", resultDoc); - //assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); - - //String expectedSearchReport = "(cts:search(fn:collection(), cts:and-query(cts:or-query((cts:element-value-query(fn:QName(\"\", \"id\"), \"00*2\", (\"lang=en\"), 1), cts:element-value-query(fn:QName(\"\", \"id\"), \"0??6\", (\"lang=en\"), 1)), ()), (\"score-logtfidf\"), 1))[1 to 10]"; - - //assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); - - // release client - client.release(); - } - - - -@Test public void testNearQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testNearQuery"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "valueConstraintWildCardOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/structured-query-near/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); - StructuredQueryDefinition termQuery1 = qb.term("Bush"); - StructuredQueryDefinition termQuery2 = qb.term("Atlantic"); - StructuredQueryDefinition nearQuery = qb.near(6, 1.0, StructuredQueryBuilder.Ordering.UNORDERED, termQuery1, termQuery2); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(nearQuery, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("0011", "string(//*[local-name()='result']//*[local-name()='id'])", resultDoc); - //assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); - - //String expectedSearchReport = "(cts:search(fn:collection(), cts:and-query(cts:or-query((cts:element-value-query(fn:QName(\"\", \"id\"), \"00*2\", (\"lang=en\"), 1), cts:element-value-query(fn:QName(\"\", \"id\"), \"0??6\", (\"lang=en\"), 1)), ()), (\"score-logtfidf\"), 1))[1 to 10]"; - - //assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); - - // release client - client.release(); - } - - - -@Test public void testDirectoryQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testDirectoryQuery"); - - String[] filenames1 = {"constraint1.xml", "constraint2.xml", "constraint3.xml"}; - String[] filenames2 = {"constraint4.xml", "constraint5.xml"}; - String queryOptionName = "valueConstraintWildCardOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename1 : filenames1) - { - writeDocumentUsingInputStreamHandle(client, filename1, "/dir1/dir2/", "XML"); - } - - // write docs - for(String filename2 : filenames2) - { - writeDocumentUsingInputStreamHandle(client, filename2, "/dir3/dir4/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); - StructuredQueryDefinition termQuery = qb.term("Memex"); - StructuredQueryDefinition dirQuery = qb.directory(true, "/dir3/"); - StructuredQueryDefinition andFinalQuery = qb.and(termQuery, dirQuery); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(andFinalQuery, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("0026", "string(//*[local-name()='result']//*[local-name()='id'])", resultDoc); - - //String expectedSearchReport = "(cts:search(fn:collection(), cts:and-query(cts:or-query((cts:element-value-query(fn:QName(\"\", \"id\"), \"00*2\", (\"lang=en\"), 1), cts:element-value-query(fn:QName(\"\", \"id\"), \"0??6\", (\"lang=en\"), 1)), ()), (\"score-logtfidf\"), 1))[1 to 10]"; - - //assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); - - // release client - client.release(); - } - - - -@Test public void testDocumentQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testDocumentQuery"); - - String[] filenames1 = {"constraint1.xml", "constraint2.xml", "constraint3.xml"}; - String[] filenames2 = {"constraint4.xml", "constraint5.xml"}; - String queryOptionName = "valueConstraintWildCardOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename1 : filenames1) - { - writeDocumentUsingInputStreamHandle(client, filename1, "/dir1/dir2/", "XML"); - } - - // write docs - for(String filename2 : filenames2) - { - writeDocumentUsingInputStreamHandle(client, filename2, "/dir3/dir4/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); - StructuredQueryDefinition termQuery = qb.term("Memex"); - StructuredQueryDefinition docQuery = qb.or(qb.document("/dir1/dir2/constraint2.xml"), qb.document("/dir3/dir4/constraint4.xml"), qb.document("/dir3/dir4/constraint5.xml")); - StructuredQueryDefinition andFinalQuery = qb.and(termQuery, docQuery); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(andFinalQuery, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); - - //String expectedSearchReport = "(cts:search(fn:collection(), cts:and-query(cts:or-query((cts:element-value-query(fn:QName(\"\", \"id\"), \"00*2\", (\"lang=en\"), 1), cts:element-value-query(fn:QName(\"\", \"id\"), \"0??6\", (\"lang=en\"), 1)), ()), (\"score-logtfidf\"), 1))[1 to 10]"; - - //assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); - - // release client - client.release(); - } - - - -@Test public void testCollectionQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testCollectionQuery"); - - String filename1 = "constraint1.xml"; - String filename2 = "constraint2.xml"; - String filename3 = "constraint3.xml"; - String filename4 = "constraint4.xml"; - String filename5 = "constraint5.xml"; - String queryOptionName = "valueConstraintWildCardOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // create and initialize a handle on the metadata - DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle4 = new DocumentMetadataHandle(); - DocumentMetadataHandle metadataHandle5 = new DocumentMetadataHandle(); - - // set the metadata - metadataHandle1.getCollections().addAll("http://test.com/set1"); - metadataHandle1.getCollections().addAll("http://test.com/set5"); - metadataHandle2.getCollections().addAll("http://test.com/set1"); - metadataHandle3.getCollections().addAll("http://test.com/set3"); - metadataHandle4.getCollections().addAll("http://test.com/set3/set3-1"); - metadataHandle5.getCollections().addAll("http://test.com/set1"); - metadataHandle5.getCollections().addAll("http://test.com/set5"); - - // write docs - writeDocumentUsingInputStreamHandle(client, filename1, "/collection-constraint/", metadataHandle1, "XML"); - writeDocumentUsingInputStreamHandle(client, filename2, "/collection-constraint/", metadataHandle2, "XML"); - writeDocumentUsingInputStreamHandle(client, filename3, "/collection-constraint/", metadataHandle3, "XML"); - writeDocumentUsingInputStreamHandle(client, filename4, "/collection-constraint/", metadataHandle4, "XML"); - writeDocumentUsingInputStreamHandle(client, filename5, "/collection-constraint/", metadataHandle5, "XML"); - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); - StructuredQueryDefinition termQuery = qb.term("Memex"); - StructuredQueryDefinition collQuery = qb.or(qb.collection("http://test.com/set1"), qb.collection("http://test.com/set3")); - StructuredQueryDefinition andFinalQuery = qb.and(termQuery, collQuery); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(andFinalQuery, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - //System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); - - //String expectedSearchReport = "(cts:search(fn:collection(), cts:and-query(cts:or-query((cts:element-value-query(fn:QName(\"\", \"id\"), \"00*2\", (\"lang=en\"), 1), cts:element-value-query(fn:QName(\"\", \"id\"), \"0??6\", (\"lang=en\"), 1)), ()), (\"score-logtfidf\"), 1))[1 to 10]"; - - //assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); - - // release client - client.release(); - } - - - -@Test public void testContainerConstraintQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testContainerConstraintQuery"); - - String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; - String queryOptionName = "containerConstraintOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/structured-query-container/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); - StructuredQueryDefinition containerQuery = qb.containerConstraint("title-contain", qb.term("Bush")); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(containerQuery, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - //assertXpathEvaluatesTo("0011", "string(//*[local-name()='result']//*[local-name()='id'])", resultDoc); - //assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); - - //String expectedSearchReport = "(cts:search(fn:collection(), cts:and-query(cts:or-query((cts:element-value-query(fn:QName(\"\", \"id\"), \"00*2\", (\"lang=en\"), 1), cts:element-value-query(fn:QName(\"\", \"id\"), \"0??6\", (\"lang=en\"), 1)), ()), (\"score-logtfidf\"), 1))[1 to 10]"; - - //assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); - - // release client - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } -} + +package com.marklogic.javaclient; + +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.admin.ServerConfigurationManager; +import com.marklogic.client.query.StructuredQueryBuilder; +import com.marklogic.client.query.StructuredQueryDefinition; +import com.marklogic.client.query.StructuredQueryBuilder.Operator; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.DocumentMetadataHandle; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.*; +public class TestStructuredQuery extends BasicJavaClientREST { + + private static String dbName = "TestStructuredQueryDB"; + private static String [] fNames = {"TestStructuredQueryDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + } + + @After + public void testCleanUp() throws Exception + { + clearDB(8011); + System.out.println("Running clear script"); + } + + @Test + public void testStructuredQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testStructuredQuery"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "valueConstraintWildCardOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/structured-query/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); + StructuredQueryDefinition t = qb.valueConstraint("id", "0026"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(t, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testStructuredQueryJSON() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testStructuredQueryJSON"); + + String[] filenames = {"constraint1.json", "constraint2.json", "constraint3.json", "constraint4.json", "constraint5.json"}; + String queryOptionName = "valueConstraintWildCardOpt.json"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/structured-query/", "JSON"); + } + setJSONQueryOption(client, queryOptionName); + // setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create value query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); + StructuredQueryDefinition t = qb.value(qb.jsonProperty("popularity"),"4"); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(t, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println(convertXMLDocumentToString(resultDoc)); + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("/structured-query/constraint2.json", "string(//*[local-name()='result'][1]//@*[local-name()='uri'])", resultDoc); + + //create new word query def + StructuredQueryDefinition t1 = qb.word(qb.jsonProperty("id"), "0012"); + // create handle + DOMHandle resultsHandle1 = new DOMHandle(); + queryMgr.search(t1, resultsHandle1); + + // get the result + Document resultDoc1 = resultsHandle1.get(); + System.out.println(convertXMLDocumentToString(resultDoc1)); + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc1); + assertXpathEvaluatesTo("/structured-query/constraint2.json", "string(//*[local-name()='result'][1]//@*[local-name()='uri'])", resultDoc1); + + //create new range word query def + StructuredQueryDefinition t2 = qb.range(qb.jsonProperty("price"), "xs:integer", Operator.GE, "0.1"); + // create handle + DOMHandle resultsHandle2 = new DOMHandle(); + queryMgr.search(t2, resultsHandle2); + + // get the result + Document resultDoc2 = resultsHandle2.get(); + System.out.println(convertXMLDocumentToString(resultDoc2)); + assertXpathEvaluatesTo("5", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc2); + + // release client + client.release(); + } + + @Test + public void testValueConstraintWildCard() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testValueConstraintWildCard"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "valueConstraintWildCardOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/structured-query/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); + StructuredQueryDefinition valueConstraintQuery1 = qb.valueConstraint("id", "00*2"); + StructuredQueryDefinition valueConstraintQuery2 = qb.valueConstraint("id", "0??6"); + StructuredQueryDefinition orFinalQuery = qb.or(valueConstraintQuery1, valueConstraintQuery2); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(orFinalQuery, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); + assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testAndNotQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testAndNotQuery"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "valueConstraintWildCardOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/structured-query-andnot/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); + StructuredQueryDefinition termQuery1 = qb.term("Atlantic"); + StructuredQueryDefinition termQuery2 = qb.term("Monthly"); + StructuredQueryDefinition termQuery3 = qb.term("Bush"); + StructuredQueryDefinition andQuery = qb.and(termQuery1, termQuery2); + StructuredQueryDefinition andNotFinalQuery = qb.andNot(andQuery, termQuery3); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(andNotFinalQuery, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("0113", "string(//*[local-name()='result']//*[local-name()='id'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testNearQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testNearQuery"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "valueConstraintWildCardOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/structured-query-near/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); + StructuredQueryDefinition termQuery1 = qb.term("Bush"); + StructuredQueryDefinition termQuery2 = qb.term("Atlantic"); + StructuredQueryDefinition nearQuery = qb.near(6, 1.0, StructuredQueryBuilder.Ordering.UNORDERED, termQuery1, termQuery2); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(nearQuery, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("0011", "string(//*[local-name()='result']//*[local-name()='id'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testDirectoryQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testDirectoryQuery"); + + String[] filenames1 = {"constraint1.xml", "constraint2.xml", "constraint3.xml"}; + String[] filenames2 = {"constraint4.xml", "constraint5.xml"}; + String queryOptionName = "valueConstraintWildCardOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename1 : filenames1) + { + writeDocumentUsingInputStreamHandle(client, filename1, "/dir1/dir2/", "XML"); + } + + // write docs + for(String filename2 : filenames2) + { + writeDocumentUsingInputStreamHandle(client, filename2, "/dir3/dir4/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); + StructuredQueryDefinition termQuery = qb.term("Memex"); + StructuredQueryDefinition dirQuery = qb.directory(true, "/dir3/"); + StructuredQueryDefinition andFinalQuery = qb.and(termQuery, dirQuery); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(andFinalQuery, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("0026", "string(//*[local-name()='result']//*[local-name()='id'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testDocumentQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testDocumentQuery"); + + String[] filenames1 = {"constraint1.xml", "constraint2.xml", "constraint3.xml"}; + String[] filenames2 = {"constraint4.xml", "constraint5.xml"}; + String queryOptionName = "valueConstraintWildCardOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename1 : filenames1) + { + writeDocumentUsingInputStreamHandle(client, filename1, "/dir1/dir2/", "XML"); + } + + // write docs + for(String filename2 : filenames2) + { + writeDocumentUsingInputStreamHandle(client, filename2, "/dir3/dir4/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); + StructuredQueryDefinition termQuery = qb.term("Memex"); + StructuredQueryDefinition docQuery = qb.or(qb.document("/dir1/dir2/constraint2.xml"), qb.document("/dir3/dir4/constraint4.xml"), qb.document("/dir3/dir4/constraint5.xml")); + StructuredQueryDefinition andFinalQuery = qb.and(termQuery, docQuery); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(andFinalQuery, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); + assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testCollectionQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testCollectionQuery"); + + String filename1 = "constraint1.xml"; + String filename2 = "constraint2.xml"; + String filename3 = "constraint3.xml"; + String filename4 = "constraint4.xml"; + String filename5 = "constraint5.xml"; + String queryOptionName = "valueConstraintWildCardOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle1 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle2 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle3 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle4 = new DocumentMetadataHandle(); + DocumentMetadataHandle metadataHandle5 = new DocumentMetadataHandle(); + + // set the metadata + metadataHandle1.getCollections().addAll("http://test.com/set1"); + metadataHandle1.getCollections().addAll("http://test.com/set5"); + metadataHandle2.getCollections().addAll("http://test.com/set1"); + metadataHandle3.getCollections().addAll("http://test.com/set3"); + metadataHandle4.getCollections().addAll("http://test.com/set3/set3-1"); + metadataHandle5.getCollections().addAll("http://test.com/set1"); + metadataHandle5.getCollections().addAll("http://test.com/set5"); + + // write docs + writeDocumentUsingInputStreamHandle(client, filename1, "/collection-constraint/", metadataHandle1, "XML"); + writeDocumentUsingInputStreamHandle(client, filename2, "/collection-constraint/", metadataHandle2, "XML"); + writeDocumentUsingInputStreamHandle(client, filename3, "/collection-constraint/", metadataHandle3, "XML"); + writeDocumentUsingInputStreamHandle(client, filename4, "/collection-constraint/", metadataHandle4, "XML"); + writeDocumentUsingInputStreamHandle(client, filename5, "/collection-constraint/", metadataHandle5, "XML"); + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); + StructuredQueryDefinition termQuery = qb.term("Memex"); + StructuredQueryDefinition collQuery = qb.or(qb.collection("http://test.com/set1"), qb.collection("http://test.com/set3")); + StructuredQueryDefinition andFinalQuery = qb.and(termQuery, collQuery); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(andFinalQuery, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); + assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testContainerConstraintQuery() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testContainerConstraintQuery"); + + String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"}; + String queryOptionName = "containerConstraintOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/structured-query-container/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); + StructuredQueryDefinition containerQuery = qb.containerConstraint("title-contain", qb.term("Bush")); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(containerQuery, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestStructuredQueryMildNot.java b/test-complete/src/test/java/com/marklogic/javaclient/TestStructuredQueryMildNot.java index 6b287d182..df6c72fcf 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestStructuredQueryMildNot.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestStructuredQueryMildNot.java @@ -1,93 +1,88 @@ -package com.marklogic.javaclient; - -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.admin.ServerConfigurationManager; -import com.marklogic.client.query.StructuredQueryBuilder; -import com.marklogic.client.query.StructuredQueryDefinition; -import com.marklogic.client.io.DOMHandle; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.junit.*; -public class TestStructuredQueryMildNot extends BasicJavaClientREST { - - private static String dbName = "TestStructuredQueryMildNotDB"; - private static String [] fNames = {"TestStructuredQueryMildNotDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesConstraint(dbName); - } - - -@SuppressWarnings("deprecation") -@Test public void testStructuredQueryMildNot() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testStructuredQueryMildNot"); - - String[] filenames = {"mildnot1.xml"}; - String queryOptionName = "mildNotOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // set query option validation to true - ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.readConfiguration(); - srvMgr.setQueryOptionValidation(true); - srvMgr.writeConfiguration(); - - // write docs - for(String filename : filenames) - { - writeDocumentUsingInputStreamHandle(client, filename, "/structured-query-mild-not/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); - StructuredQueryDefinition termQuery1 = qb.term("summer"); - StructuredQueryDefinition termQuery2 = qb.term("time"); - StructuredQueryDefinition notInFinalQuery = qb.notIn(termQuery1, termQuery2); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(notInFinalQuery, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - System.out.println(convertXMLDocumentToString(resultDoc)); - - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - //assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc); - //assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc); - //String expectedSearchReport = "(cts:search(fn:collection(), cts:and-query(cts:or-query((cts:element-value-query(fn:QName(\"\", \"id\"), \"00*2\", (\"lang=en\"), 1), cts:element-value-query(fn:QName(\"\", \"id\"), \"0??6\", (\"lang=en\"), 1)), ()), (\"score-logtfidf\"), 1))[1 to 10]"; - - //assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc); - - // release client - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } -} +package com.marklogic.javaclient; + +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.admin.ServerConfigurationManager; +import com.marklogic.client.query.StructuredQueryBuilder; +import com.marklogic.client.query.StructuredQueryDefinition; +import com.marklogic.client.io.DOMHandle; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.*; +public class TestStructuredQueryMildNot extends BasicJavaClientREST { + + private static String dbName = "TestStructuredQueryMildNotDB"; + private static String [] fNames = {"TestStructuredQueryMildNotDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesConstraint(dbName); + } + + @Test + public void testStructuredQueryMildNot() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testStructuredQueryMildNot"); + + String[] filenames = {"mildnot1.xml"}; + String queryOptionName = "mildNotOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // set query option validation to true + ServerConfigurationManager srvMgr = client.newServerConfigManager(); + srvMgr.readConfiguration(); + srvMgr.setQueryOptionValidation(true); + srvMgr.writeConfiguration(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/structured-query-mild-not/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); + StructuredQueryDefinition termQuery1 = qb.term("summer"); + StructuredQueryDefinition termQuery2 = qb.term("time"); + StructuredQueryDefinition notInFinalQuery = qb.notIn(termQuery1, termQuery2); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(notInFinalQuery, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println(convertXMLDocumentToString(resultDoc)); + + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestStructuredSearchGeo.java b/test-complete/src/test/java/com/marklogic/javaclient/TestStructuredSearchGeo.java index 0a77693ff..2dc9c6f7f 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestStructuredSearchGeo.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestStructuredSearchGeo.java @@ -1,169 +1,169 @@ -package com.marklogic.javaclient; - -import java.io.IOException; -import java.util.Collection; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import com.marklogic.client.query.QueryManager; -import org.w3c.dom.Document; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.query.StructuredQueryBuilder; -import com.marklogic.client.query.StructuredQueryDefinition; -import com.marklogic.client.util.EditableNamespaceContext; -import com.marklogic.client.io.DOMHandle; - -import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; -import static org.junit.Assert.*; - -import org.custommonkey.xmlunit.exceptions.XpathException; -import org.junit.*; -public class TestStructuredSearchGeo extends BasicJavaClientREST { - - private static String dbName = "TestStructuredSearchGeoDB"; - private static String [] fNames = {"TestStructuredSearchGeoDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - setupAppServicesGeoConstraint(dbName); - } - - -@SuppressWarnings("deprecation") -@Test public void testTestStructuredSearchGeo() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testTestStructuredSearchGeo"); - - String queryOptionName = "geoConstraintOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(int i = 1; i <= 7; i++) - { - writeDocumentUsingInputStreamHandle(client, "geo-constraint" + i + ".xml", "/geo-constraint/", "XML"); - } - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); - StructuredQueryDefinition geoElementConstraintQuery = qb.geospatialConstraint("geo-elem", qb.point(12, 5)); - StructuredQueryDefinition termQuery = qb.term("bill_kara"); - StructuredQueryDefinition finalOrQuery = qb.or(geoElementConstraintQuery, termQuery); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(finalOrQuery, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - - assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - //assertXpathEvaluatesTo("karl_kara 12,5 12,5 12 5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); - - // release client - client.release(); - } - - -@SuppressWarnings("deprecation") -@Test public void testTestStructuredSearchGeoBox() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testTestStructuredSearchGeoBox"); - - String queryOptionName = "geoConstraintOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - loadGeoData(); - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); - - StructuredQueryDefinition geoElementConstraintQuery = qb.geospatialConstraint("geo-elem-child", qb.box(-12,-5,-11,-4)); - StructuredQueryDefinition termQuery = qb.term("karl_kara"); - StructuredQueryDefinition finalAndQuery = qb.and(geoElementConstraintQuery, termQuery); - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(finalAndQuery, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - System.out.println("Output : " + convertXMLDocumentToString(resultDoc)); - assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - assertXpathEvaluatesTo("/geo-constraint/geo-constraint2.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testTestStructuredSearchGeoBoxAndPath() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException - { - System.out.println("Running testTestStructuredSearchGeoBoxAndPath" + "This test is for Bug : 22071 & 22136"); - - String queryOptionName = "geoConstraintOpt.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - loadGeoData(); - - setQueryOption(client, queryOptionName); - - QueryManager queryMgr = client.newQueryManager(); - - // create query def - StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); - - StructuredQueryDefinition geoQuery = qb.geospatial(qb.geoPath(qb.pathIndex("/doc/g-elem-point")), qb.box(-12,-5,-11,-4)); - Collection nameSpaceCollection = qb.getNamespaces().getAllPrefixes(); - assertEquals("getNamespace failed ",false, nameSpaceCollection.isEmpty()); - for(String prefix : nameSpaceCollection){ - System.out.println("Prefixes : "+prefix); - System.out.println(qb.getNamespaces().getNamespaceURI(prefix)); - if (qb.getNamespaces().getNamespaceURI(prefix).contains("http://www.w3.org/2001/XMLSchema")) - { - EditableNamespaceContext namespaces = new EditableNamespaceContext(); - namespaces.put("new", "http://www.marklogic.com"); - qb.setNamespaces(namespaces); - System.out.println(qb.getNamespaces().getNamespaceURI("new")); - } - } - - // create handle - DOMHandle resultsHandle = new DOMHandle(); - queryMgr.search(geoQuery, resultsHandle); - - // get the result - Document resultDoc = resultsHandle.get(); - System.out.println("Output : " + convertXMLDocumentToString(resultDoc)); - assertXpathEvaluatesTo("4", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); - - // release client - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } -} +package com.marklogic.javaclient; + +import java.io.IOException; +import java.util.Collection; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import com.marklogic.client.query.QueryManager; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.query.StructuredQueryBuilder; +import com.marklogic.client.query.StructuredQueryDefinition; +import com.marklogic.client.util.EditableNamespaceContext; +import com.marklogic.client.io.DOMHandle; + +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; +import static org.junit.Assert.*; + +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.*; +public class TestStructuredSearchGeo extends BasicJavaClientREST { + + private static String dbName = "TestStructuredSearchGeoDB"; + private static String [] fNames = {"TestStructuredSearchGeoDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + setupAppServicesGeoConstraint(dbName); + } + + @Test + public void testTestStructuredSearchGeo() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testTestStructuredSearchGeo"); + + String queryOptionName = "geoConstraintOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(int i = 1; i <= 7; i++) + { + writeDocumentUsingInputStreamHandle(client, "geo-constraint" + i + ".xml", "/geo-constraint/", "XML"); + } + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); + StructuredQueryDefinition geoElementConstraintQuery = qb.geospatialConstraint("geo-elem", qb.point(12, 5)); + StructuredQueryDefinition termQuery = qb.term("bill_kara"); + StructuredQueryDefinition finalOrQuery = qb.or(geoElementConstraintQuery, termQuery); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(finalOrQuery, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + + assertXpathEvaluatesTo("2", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + //assertXpathEvaluatesTo("karl_kara 12,5 12,5 12 5", "string(//*[local-name()='result'][1]//*[local-name()='match'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testTestStructuredSearchGeoBox() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testTestStructuredSearchGeoBox"); + + String queryOptionName = "geoConstraintOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + loadGeoData(); + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); + + StructuredQueryDefinition geoElementConstraintQuery = qb.geospatialConstraint("geo-elem-child", qb.box(-12,-5,-11,-4)); + StructuredQueryDefinition termQuery = qb.term("karl_kara"); + StructuredQueryDefinition finalAndQuery = qb.and(geoElementConstraintQuery, termQuery); + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(finalAndQuery, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println("Output : " + convertXMLDocumentToString(resultDoc)); + assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + assertXpathEvaluatesTo("/geo-constraint/geo-constraint2.xml", "string(//*[local-name()='result']//@*[local-name()='uri'])", resultDoc); + + // release client + client.release(); + } + + @Test + public void testTestStructuredSearchGeoBoxAndPath() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException + { + System.out.println("Running testTestStructuredSearchGeoBoxAndPath" + "This test is for Bug : 22071 & 22136"); + + String queryOptionName = "geoConstraintOpt.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + loadGeoData(); + + setQueryOption(client, queryOptionName); + + QueryManager queryMgr = client.newQueryManager(); + + // create query def + StructuredQueryBuilder qb = queryMgr.newStructuredQueryBuilder(queryOptionName); + + StructuredQueryDefinition geoQuery = qb.geospatial(qb.geoPath(qb.pathIndex("/doc/g-elem-point")), qb.box(-12,-5,-11,-4)); + Collection nameSpaceCollection = qb.getNamespaces().getAllPrefixes(); + assertEquals("getNamespace failed ",false, nameSpaceCollection.isEmpty()); + for(String prefix : nameSpaceCollection){ + System.out.println("Prefixes : "+prefix); + System.out.println(qb.getNamespaces().getNamespaceURI(prefix)); + if (qb.getNamespaces().getNamespaceURI(prefix).contains("http://www.w3.org/2001/XMLSchema")) + { + EditableNamespaceContext namespaces = new EditableNamespaceContext(); + namespaces.put("new", "http://www.marklogic.com"); + qb.setNamespaces(namespaces); + System.out.println(qb.getNamespaces().getNamespaceURI("new")); + } + } + + // create handle + DOMHandle resultsHandle = new DOMHandle(); + queryMgr.search(geoQuery, resultsHandle); + + // get the result + Document resultDoc = resultsHandle.get(); + System.out.println("Output : " + convertXMLDocumentToString(resultDoc)); + assertXpathEvaluatesTo("4", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestTransformXMLWithXSLT.java b/test-complete/src/test/java/com/marklogic/javaclient/TestTransformXMLWithXSLT.java index 429d70e8a..af632cfeb 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestTransformXMLWithXSLT.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestTransformXMLWithXSLT.java @@ -1,93 +1,95 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.*; -import java.util.Scanner; - -import javax.xml.transform.OutputKeys; -import javax.xml.transform.Source; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.stream.StreamSource; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.FileHandle; -import com.marklogic.client.io.SourceHandle; -import org.junit.*; - -public class TestTransformXMLWithXSLT extends BasicJavaClientREST { - - - @BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServerWithDB( "REST-Java-Client-API-Server-withDB", 8015); - - } - - @SuppressWarnings("deprecation") - @Test public void testWriteXMLWithXSLTransform() throws TransformerException, FileNotFoundException - { - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8015, "rest-writer", "x", Authentication.DIGEST); - - // get the doc - Source source = new StreamSource("src/test/java/com/marklogic/javaclient/data/employee.xml"); - - // get the xslt - Source xsl = new StreamSource("src/test/java/com/marklogic/javaclient/data/employee-stylesheet.xsl"); - - // create transformer - TransformerFactory factory = TransformerFactory.newInstance(); - Transformer transformer = factory.newTransformer(xsl); - transformer.setOutputProperty(OutputKeys.INDENT, "yes"); - - // create a doc manager - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - - // create an identifier for the document - String docId = "/example/trans/transform.xml"; - - // create a handle on the content - SourceHandle handle = new SourceHandle(); - handle.set(source); - - // set the transformer - handle.setTransformer(transformer); - - // write the document content - docMgr.write(docId, handle); - - System.out.println("Write " + docId + " to database"); - - // create a handle on the content - FileHandle readHandle = new FileHandle(); - - // read the document - docMgr.read(docId, readHandle); - - // access the document content - File fileRead = readHandle.get(); - - Scanner scanner = new Scanner(fileRead).useDelimiter("\\Z"); - String readContent = scanner.next(); -// String transformedContent = readContent.replaceAll("^name$", "firstname"); -// assertEquals("XML document write difference", transformedContent, readContent); - assertTrue("check document from DB has name element changed",readContent.contains("firstname")); - scanner.close(); - - // release client - client.release(); - scanner.close(); - } - @AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - deleteRESTServerWithDB("REST-Java-Client-API-Server-withDB"); - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.*; +import java.util.Scanner; + +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.stream.StreamSource; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.FileHandle; +import com.marklogic.client.io.SourceHandle; +import org.junit.*; + +public class TestTransformXMLWithXSLT extends BasicJavaClientREST { + + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServerWithDB( "REST-Java-Client-API-Server-withDB", 8015); + + } + + @Test + public void testWriteXMLWithXSLTransform() throws TransformerException, FileNotFoundException + { + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8015, "rest-writer", "x", Authentication.DIGEST); + + // get the doc + Source source = new StreamSource("src/test/java/com/marklogic/javaclient/data/employee.xml"); + + // get the xslt + Source xsl = new StreamSource("src/test/java/com/marklogic/javaclient/data/employee-stylesheet.xsl"); + + // create transformer + TransformerFactory factory = TransformerFactory.newInstance(); + Transformer transformer = factory.newTransformer(xsl); + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + + // create a doc manager + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + + // create an identifier for the document + String docId = "/example/trans/transform.xml"; + + // create a handle on the content + SourceHandle handle = new SourceHandle(); + handle.set(source); + + // set the transformer + handle.setTransformer(transformer); + + // write the document content + docMgr.write(docId, handle); + + System.out.println("Write " + docId + " to database"); + + // create a handle on the content + FileHandle readHandle = new FileHandle(); + + // read the document + docMgr.read(docId, readHandle); + + // access the document content + File fileRead = readHandle.get(); + + Scanner scanner = new Scanner(fileRead).useDelimiter("\\Z"); + String readContent = scanner.next(); + // String transformedContent = readContent.replaceAll("^name$", "firstname"); + // assertEquals("XML document write difference", transformedContent, readContent); + assertTrue("check document from DB has name element changed",readContent.contains("firstname")); + scanner.close(); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + deleteRESTServerWithDB("REST-Java-Client-API-Server-withDB"); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestValueConstraint.java b/test-complete/src/test/java/com/marklogic/javaclient/TestValueConstraint.java index c03b1284f..d6ea42403 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestValueConstraint.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestValueConstraint.java @@ -1,223 +1,226 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.BufferedReader; -import java.io.FileNotFoundException; -import java.io.FileReader; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.admin.QueryOptionsManager; -import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.query.MatchDocumentSummary; -import com.marklogic.client.query.MatchLocation; -import com.marklogic.client.query.StringQueryDefinition; -import com.marklogic.client.io.ReaderHandle; -import com.marklogic.client.io.SearchHandle; -import com.marklogic.client.io.StringHandle; - -import org.junit.*; -public class TestValueConstraint extends BasicJavaClientREST { - - static final private String[] filenames = {"value-constraint-doc.xml", "value-constraint-doc2.xml"}; - - private static String dbName = "ValueConstraintDB"; - private static String [] fNames = {"ValueConstraintDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - private static int restPort = 8011; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - } -@After - public void testCleanUp() throws Exception - { - clearDB(restPort); - System.out.println("Running clear script"); - } - -@SuppressWarnings("deprecation") -@Test public void testElementValueConstraint() throws FileNotFoundException - { - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create doc manager - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - - String docId = null; - - // create handle - ReaderHandle handle = new ReaderHandle(); - - // write the files - for (String filename: filenames) - { - // acquire the content - BufferedReader docStream = new BufferedReader(new FileReader("src/test/java/com/marklogic/javaclient/data/" + filename)); - - // create an identifier for the document - docId = "/value-constraint/" + filename; - - handle.set(docStream); - - // write the document content - docMgr.write(docId, handle); - - System.out.println("Write " + docId + " to database"); - } - - // create a manager for writing query options - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create the query options - StringBuilder builder = new StringBuilder(); - builder.append("\n"); - builder.append("\n"); - builder.append(" \n"); - builder.append(" \n"); - builder.append(" \n"); - builder.append(" \n"); - builder.append(" \n"); - builder.append(" \n"); - builder.append(" \n"); - builder.append(" \n"); - builder.append(" \n"); - builder.append(" \n"); - builder.append(" \n"); - builder.append("\n"); - - // initialize a handle with the query options - StringHandle writeHandle = new StringHandle(builder.toString()); - - // write the query options to the database - optionsMgr.writeOptions("valueConstraintOpt", writeHandle); - - // create a manager for searching - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition - StringQueryDefinition querydef = queryMgr.newStringDefinition("valueConstraintOpt"); - querydef.setCriteria("my-element-value:\"Indiana Jones\""); - - // create a handle for the search results - SearchHandle resultsHandle = new SearchHandle(); - - // run the search - queryMgr.search(querydef, resultsHandle); - - // iterate over the result documents - MatchDocumentSummary[] docSummaries = resultsHandle.getMatchResults(); - String searchMatch = ""; - for (MatchDocumentSummary docSummary: docSummaries) { - String uri = docSummary.getUri(); - - // iterate over the match locations within a result document - MatchLocation[] locations = docSummary.getMatchLocations(); - searchMatch = "Matched " + locations.length + " locations in "+ uri; - } - - String expectedSearchMatch = "Matched 1 locations in /value-constraint/value-constraint-doc2.xml"; - assertEquals("Search match difference", expectedSearchMatch, searchMatch); - - // release client - client.release(); - } - -@SuppressWarnings("deprecation") -@Test public void testAttributeValueConstraint() throws FileNotFoundException - { - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // create doc manager - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - - // create doc id - String docId = null; - - // create handle - ReaderHandle handle = new ReaderHandle(); - - // write the files - for (String filename: filenames) - { - // acquire the content - BufferedReader docStream = new BufferedReader(new FileReader("src/test/java/com/marklogic/javaclient/data/" + filename)); - - // create an identifier for the document - docId = "/value-constraint/" + filename; - - handle.set(docStream); - - // write the document content - docMgr.write(docId, handle); - - System.out.println("Write " + docId + " to database"); - } - // create a manager for writing query options - QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); - - // create the query options - StringBuilder builder = new StringBuilder(); - builder.append("\n"); - builder.append("\n"); - builder.append(" \n"); - builder.append(" \n"); - builder.append(" \n"); - builder.append(" \n"); - builder.append(" \n"); - builder.append(" \n"); - builder.append(" \n"); - builder.append(" \n"); - builder.append(" \n"); - builder.append(" \n"); - builder.append(" \n"); - builder.append("\n"); - - // initialize a handle with the query options - StringHandle writeHandle = new StringHandle(builder.toString()); - - // write the query options to the database - optionsMgr.writeOptions("valueConstraintOpt", writeHandle); - - // create a manager for searching - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition - StringQueryDefinition querydef = queryMgr.newStringDefinition("valueConstraintOpt"); - querydef.setCriteria("my-attribute-value:2005"); - - // create a handle for the search results - SearchHandle resultsHandle = new SearchHandle(); - - // run the search - queryMgr.search(querydef, resultsHandle); - - // iterate over the result documents - MatchDocumentSummary[] docSummaries = resultsHandle.getMatchResults(); - String searchMatch = ""; - for (MatchDocumentSummary docSummary: docSummaries) { - String uri = docSummary.getUri(); - - // iterate over the match locations within a result document - MatchLocation[] locations = docSummary.getMatchLocations(); - searchMatch = "Matched " + locations.length + " locations in "+ uri; - } - - String expectedSearchMatch = "Matched 1 locations in /value-constraint/value-constraint-doc.xml"; - assertEquals("Search match difference", expectedSearchMatch, searchMatch); - - // release client - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.admin.QueryOptionsManager; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.query.MatchDocumentSummary; +import com.marklogic.client.query.MatchLocation; +import com.marklogic.client.query.StringQueryDefinition; +import com.marklogic.client.io.ReaderHandle; +import com.marklogic.client.io.SearchHandle; +import com.marklogic.client.io.StringHandle; + +import org.junit.*; +public class TestValueConstraint extends BasicJavaClientREST { + + static final private String[] filenames = {"value-constraint-doc.xml", "value-constraint-doc2.xml"}; + + private static String dbName = "ValueConstraintDB"; + private static String [] fNames = {"ValueConstraintDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + } + + @After + public void testCleanUp() throws Exception + { + clearDB(restPort); + System.out.println("Running clear script"); + } + + @Test + public void testElementValueConstraint() throws FileNotFoundException + { + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create doc manager + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + + String docId = null; + + // create handle + ReaderHandle handle = new ReaderHandle(); + + // write the files + for (String filename: filenames) + { + // acquire the content + BufferedReader docStream = new BufferedReader(new FileReader("src/test/java/com/marklogic/javaclient/data/" + filename)); + + // create an identifier for the document + docId = "/value-constraint/" + filename; + + handle.set(docStream); + + // write the document content + docMgr.write(docId, handle); + + System.out.println("Write " + docId + " to database"); + } + + // create a manager for writing query options + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create the query options + StringBuilder builder = new StringBuilder(); + builder.append("\n"); + builder.append("\n"); + builder.append(" \n"); + builder.append(" \n"); + builder.append(" \n"); + builder.append(" \n"); + builder.append(" \n"); + builder.append(" \n"); + builder.append(" \n"); + builder.append(" \n"); + builder.append(" \n"); + builder.append(" \n"); + builder.append(" \n"); + builder.append("\n"); + + // initialize a handle with the query options + StringHandle writeHandle = new StringHandle(builder.toString()); + + // write the query options to the database + optionsMgr.writeOptions("valueConstraintOpt", writeHandle); + + // create a manager for searching + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition + StringQueryDefinition querydef = queryMgr.newStringDefinition("valueConstraintOpt"); + querydef.setCriteria("my-element-value:\"Indiana Jones\""); + + // create a handle for the search results + SearchHandle resultsHandle = new SearchHandle(); + + // run the search + queryMgr.search(querydef, resultsHandle); + + // iterate over the result documents + MatchDocumentSummary[] docSummaries = resultsHandle.getMatchResults(); + String searchMatch = ""; + for (MatchDocumentSummary docSummary: docSummaries) { + String uri = docSummary.getUri(); + + // iterate over the match locations within a result document + MatchLocation[] locations = docSummary.getMatchLocations(); + searchMatch = "Matched " + locations.length + " locations in "+ uri; + } + + String expectedSearchMatch = "Matched 1 locations in /value-constraint/value-constraint-doc2.xml"; + assertEquals("Search match difference", expectedSearchMatch, searchMatch); + + // release client + client.release(); + } + + @Test + public void testAttributeValueConstraint() throws FileNotFoundException + { + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // create doc manager + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + + // create doc id + String docId = null; + + // create handle + ReaderHandle handle = new ReaderHandle(); + + // write the files + for (String filename: filenames) + { + // acquire the content + BufferedReader docStream = new BufferedReader(new FileReader("src/test/java/com/marklogic/javaclient/data/" + filename)); + + // create an identifier for the document + docId = "/value-constraint/" + filename; + + handle.set(docStream); + + // write the document content + docMgr.write(docId, handle); + + System.out.println("Write " + docId + " to database"); + } + // create a manager for writing query options + QueryOptionsManager optionsMgr = client.newServerConfigManager().newQueryOptionsManager(); + + // create the query options + StringBuilder builder = new StringBuilder(); + builder.append("\n"); + builder.append("\n"); + builder.append(" \n"); + builder.append(" \n"); + builder.append(" \n"); + builder.append(" \n"); + builder.append(" \n"); + builder.append(" \n"); + builder.append(" \n"); + builder.append(" \n"); + builder.append(" \n"); + builder.append(" \n"); + builder.append(" \n"); + builder.append("\n"); + + // initialize a handle with the query options + StringHandle writeHandle = new StringHandle(builder.toString()); + + // write the query options to the database + optionsMgr.writeOptions("valueConstraintOpt", writeHandle); + + // create a manager for searching + QueryManager queryMgr = client.newQueryManager(); + + // create a search definition + StringQueryDefinition querydef = queryMgr.newStringDefinition("valueConstraintOpt"); + querydef.setCriteria("my-attribute-value:2005"); + + // create a handle for the search results + SearchHandle resultsHandle = new SearchHandle(); + + // run the search + queryMgr.search(querydef, resultsHandle); + + // iterate over the result documents + MatchDocumentSummary[] docSummaries = resultsHandle.getMatchResults(); + String searchMatch = ""; + for (MatchDocumentSummary docSummary: docSummaries) { + String uri = docSummary.getUri(); + + // iterate over the match locations within a result document + MatchLocation[] locations = docSummary.getMatchLocations(); + searchMatch = "Matched " + locations.length + " locations in "+ uri; + } + + String expectedSearchMatch = "Matched 1 locations in /value-constraint/value-constraint-doc.xml"; + assertEquals("Search match difference", expectedSearchMatch, searchMatch); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestWordConstraint.java b/test-complete/src/test/java/com/marklogic/javaclient/TestWordConstraint.java index 500703da4..e8c27edf2 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestWordConstraint.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestWordConstraint.java @@ -1,64 +1,66 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.IOException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.SearchHandle; -import com.marklogic.javaclient.BasicJavaClientREST; -import org.junit.*; -public class TestWordConstraint extends BasicJavaClientREST { - - static String filenames[] = {"word-constraint-doc1.xml", "word-constraint-doc2.xml"}; - static String queryOptionName = "wordConstraintOpt.xml"; - private static String dbName = "WordConstraintDB"; - private static String [] fNames = {"WordConstraintDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0],restServerName,8011); - } - -@SuppressWarnings("deprecation") -@Test public void testElementWordConstraint() throws IOException - { - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - // write docs - for(String filename:filenames) - { - writeDocumentReaderHandle(client, filename, "/word-constraint/", "XML"); - } - - - // write the query options to the database - setQueryOption(client, queryOptionName); - - // run the search - SearchHandle resultsHandle = runSearch(client, queryOptionName, "my-element-word:paris"); - - // search result - String searchResult = returnSearchResult(resultsHandle); - - String expectedSearchResult = "|Matched 1 locations in /word-constraint/word-constraint-doc1.xml"; - - System.out.println(searchResult); - - assertEquals("Search result difference", expectedSearchResult, searchResult); - - // release client - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } -} - +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.IOException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.SearchHandle; +import com.marklogic.javaclient.BasicJavaClientREST; +import org.junit.*; +public class TestWordConstraint extends BasicJavaClientREST { + + static String filenames[] = {"word-constraint-doc1.xml", "word-constraint-doc2.xml"}; + static String queryOptionName = "wordConstraintOpt.xml"; + private static String dbName = "WordConstraintDB"; + private static String [] fNames = {"WordConstraintDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0],restServerName,8011); + } + + @Test + public void testElementWordConstraint() throws IOException + { + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + // write docs + for(String filename:filenames) + { + writeDocumentReaderHandle(client, filename, "/word-constraint/", "XML"); + } + + + // write the query options to the database + setQueryOption(client, queryOptionName); + + // run the search + SearchHandle resultsHandle = runSearch(client, queryOptionName, "my-element-word:paris"); + + // search result + String searchResult = returnSearchResult(resultsHandle); + + String expectedSearchResult = "|Matched 1 locations in /word-constraint/word-constraint-doc1.xml"; + + System.out.println(searchResult); + + assertEquals("Search result difference", expectedSearchResult, searchResult); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } +} + diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestWriteTextDoc.java b/test-complete/src/test/java/com/marklogic/javaclient/TestWriteTextDoc.java index c208bbe36..c77b73949 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestWriteTextDoc.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestWriteTextDoc.java @@ -1,37 +1,40 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; - -import com.marklogic.client.document.TextDocumentManager; -import com.marklogic.client.io.StringHandle; -import org.junit.*; -public class TestWriteTextDoc extends BasicJavaClientREST -{ - - @BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServerWithDB( "REST-Java-Client-API-Server-withDB", 8015); - - } - - @SuppressWarnings("deprecation") - @Test public void testWriteTextDoc() - { - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "admin", "admin", Authentication.DIGEST); - - String docId = "/foo/test/myFoo.txt"; - TextDocumentManager docMgr = client.newTextDocumentManager(); - docMgr.write(docId, new StringHandle().with("This is so foo")); - assertEquals("Text document write difference", "This is so foo", docMgr.read(docId, new StringHandle()).get()); - } - @AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - deleteRESTServerWithDB("REST-Java-Client-API-Server-withDB"); - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; + +import com.marklogic.client.document.TextDocumentManager; +import com.marklogic.client.io.StringHandle; +import org.junit.*; +public class TestWriteTextDoc extends BasicJavaClientREST +{ + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServerWithDB( "REST-Java-Client-API-Server-withDB", 8015); + + } + + @Test + public void testWriteTextDoc() + { + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "admin", "admin", Authentication.DIGEST); + + String docId = "/foo/test/myFoo.txt"; + TextDocumentManager docMgr = client.newTextDocumentManager(); + docMgr.write(docId, new StringHandle().with("This is so foo")); + assertEquals("Text document write difference", "This is so foo", docMgr.read(docId, new StringHandle()).get()); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + deleteRESTServerWithDB("REST-Java-Client-API-Server-withDB"); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestXMLDocumentRepair.java b/test-complete/src/test/java/com/marklogic/javaclient/TestXMLDocumentRepair.java index d0c0e9d4c..6a3a682dc 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestXMLDocumentRepair.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestXMLDocumentRepair.java @@ -1,96 +1,98 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.*; - -import java.io.*; -import java.util.Scanner; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.document.XMLDocumentManager.DocumentRepair; -import com.marklogic.client.io.FileHandle; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import org.junit.*; -public class TestXMLDocumentRepair extends BasicJavaClientREST { - @BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServerWithDB( "REST-Java-Client-API-Server-withDB", 8015); - - } - - @SuppressWarnings("deprecation") - @Test public void testXMLDocumentRepairFull() throws IOException - { - // acquire the content - File file = new File("repairXMLFull.xml"); - file.delete(); - boolean success = file.createNewFile(); - if(success) - System.out.println("New file created on " + file.getAbsolutePath()); - else - System.out.println("Cannot create file"); - - BufferedWriter out = new BufferedWriter(new FileWriter(file)); - - String xmlContent = - "\n" + - "\n" + - "

This is bold and italic within the paragraph.

\n" + - "

This is bold and italic within the paragraph.

\n" + - "

This is bold and italic within the paragraph.

\n" + - "
"; - - String repairedContent = - "\n" + - "\n" + - "

This is bold and italic within the paragraph.

\n" + - "

This is bold and italic within the paragraph.

\n" + - "

This is bold and italic within the paragraph.

\n" + - "
"; - - out.write(xmlContent); - out.close(); - - // create database client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // create doc id - String docId = "/repair/xml/" + file.getName(); - - // create document manager - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - - // set document repair - docMgr.setDocumentRepair(DocumentRepair.FULL); - - // create a handle on the content - FileHandle handle = new FileHandle(file); - handle.set(file); - - // write the document content - docMgr.write(docId, handle); - - System.out.println("Write " + docId + " to database"); - - // read the document - docMgr.read(docId, handle); - - // access the document content - File fileRead = handle.get(); - - Scanner scanner = new Scanner(fileRead).useDelimiter("\\Z"); - String readContent = scanner.next(); - assertEquals("XML document write difference", repairedContent, readContent); - scanner.close(); - - // release the client - client.release(); - } - @AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - deleteRESTServerWithDB("REST-Java-Client-API-Server-withDB"); - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.*; +import java.util.Scanner; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.document.XMLDocumentManager.DocumentRepair; +import com.marklogic.client.io.FileHandle; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import org.junit.*; +public class TestXMLDocumentRepair extends BasicJavaClientREST { + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServerWithDB( "REST-Java-Client-API-Server-withDB", 8015); + + } + + @Test + public void testXMLDocumentRepairFull() throws IOException + { + // acquire the content + File file = new File("repairXMLFull.xml"); + file.delete(); + boolean success = file.createNewFile(); + if(success) + System.out.println("New file created on " + file.getAbsolutePath()); + else + System.out.println("Cannot create file"); + + BufferedWriter out = new BufferedWriter(new FileWriter(file)); + + String xmlContent = + "\n" + + "\n" + + "

This is bold and italic within the paragraph.

\n" + + "

This is bold and italic within the paragraph.

\n" + + "

This is bold and italic within the paragraph.

\n" + + "
"; + + String repairedContent = + "\n" + + "\n" + + "

This is bold and italic within the paragraph.

\n" + + "

This is bold and italic within the paragraph.

\n" + + "

This is bold and italic within the paragraph.

\n" + + "
"; + + out.write(xmlContent); + out.close(); + + // create database client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // create doc id + String docId = "/repair/xml/" + file.getName(); + + // create document manager + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + + // set document repair + docMgr.setDocumentRepair(DocumentRepair.FULL); + + // create a handle on the content + FileHandle handle = new FileHandle(file); + handle.set(file); + + // write the document content + docMgr.write(docId, handle); + + System.out.println("Write " + docId + " to database"); + + // read the document + docMgr.read(docId, handle); + + // access the document content + File fileRead = handle.get(); + + Scanner scanner = new Scanner(fileRead).useDelimiter("\\Z"); + String readContent = scanner.next(); + assertEquals("XML document write difference", repairedContent, readContent); + scanner.close(); + + // release the client + client.release(); + } + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + deleteRESTServerWithDB("REST-Java-Client-API-Server-withDB"); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestXMLEventReaderHandle.java b/test-complete/src/test/java/com/marklogic/javaclient/TestXMLEventReaderHandle.java index d5f98cbb7..7d82eea8d 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestXMLEventReaderHandle.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestXMLEventReaderHandle.java @@ -1,124 +1,118 @@ -package com.marklogic.javaclient; - -import java.io.IOException; -import java.io.InputStream; -import java.io.Reader; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.stream.XMLEventReader; -import javax.xml.stream.XMLStreamException; -import javax.xml.transform.TransformerException; - -import org.custommonkey.xmlunit.XMLUnit; -import org.w3c.dom.Document; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.ReaderHandle; -import com.marklogic.client.io.XMLEventReaderHandle; -import com.marklogic.javaclient.BasicJavaClientREST; - -import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; -import static org.junit.Assert.*; - -import org.junit.*; -public class TestXMLEventReaderHandle extends BasicJavaClientREST { - - private static String dbName = "XMLEventReaderHandleDB"; - private static String [] fNames = {"XMLEventReaderHandleDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - } - -@SuppressWarnings("deprecation") -@Test public void testXmlCRUD() throws IOException, SAXException, ParserConfigurationException, TransformerException, XMLStreamException - { - String filename = "xml-original-test.xml"; - String uri = "/write-xml-XMLEventReaderHandle/"; - - System.out.println("Running testXmlCRUD"); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write the doc - writeDocumentReaderHandle(client, filename, uri, "XML"); - - // read the document - XMLEventReaderHandle readHandle = readDocumentUsingXMLEventReaderHandle(client, uri + filename, "XML"); - - // access the document content - XMLEventReader fileRead = readHandle.get(); - - String readContent = convertXMLEventReaderToString(fileRead); - String readContentCrop = readContent.substring(0, readContent.length()-11); - System.out.println(readContentCrop); - - // get xml document for expected result - Document expectedDoc = expectedXMLDocument(filename); - - String expectedContent = convertXMLDocumentToString(expectedDoc); - System.out.println(expectedContent); - // convert actual string to xml doc - Document readDoc = convertStringToXMLDocument(readContentCrop); - - assertXMLEqual("Write XML difference", expectedDoc,readDoc); - - // update the doc - // acquire the content for update - String updateFilename = "xml-updated-test.xml"; - updateDocumentReaderHandle(client, updateFilename, uri + filename, "XML"); - - // read the document - XMLEventReaderHandle updateHandle = readDocumentUsingXMLEventReaderHandle(client, uri + filename, "XML"); - - // access the document content - XMLEventReader fileReadUpdate = updateHandle.get(); - - String readContentUpdate = convertXMLEventReaderToString(fileReadUpdate); - String readContentUpdateCrop = readContentUpdate.substring(0,readContentUpdate.length()-11); - // get xml document for expected result - Document expectedDocUpdate = expectedXMLDocument(updateFilename); - - // convert actual string to xml doc - Document readDocUpdate = convertStringToXMLDocument(readContentUpdateCrop); - - assertXMLEqual("Write XML difference", expectedDocUpdate, readDocUpdate); - - - // delete the document - deleteDocument(client, uri + filename, "XML"); - - // read the deleted document - //assertFalse("Document is not deleted", isDocumentExist(client, "/write-xml-readerhandle/" + filename, "XML")); - - String exception = ""; - try - { - ReaderHandle deleteHandle = readDocumentReaderHandle(client, uri + filename, "XML"); - } catch (Exception e) { exception = e.toString(); } - - String expectedException = "Could not read non-existent document"; - boolean documentIsDeleted = exception.contains(expectedException); - assertTrue("Document is not deleted", documentIsDeleted); - - - - // release the client - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - - } +package com.marklogic.javaclient; + +import java.io.IOException; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.stream.XMLEventReader; +import javax.xml.stream.XMLStreamException; +import javax.xml.transform.TransformerException; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.ReaderHandle; +import com.marklogic.client.io.XMLEventReaderHandle; +import com.marklogic.javaclient.BasicJavaClientREST; + +import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; +import static org.junit.Assert.*; + +import org.junit.*; +public class TestXMLEventReaderHandle extends BasicJavaClientREST { + + private static String dbName = "XMLEventReaderHandleDB"; + private static String [] fNames = {"XMLEventReaderHandleDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + } + + @Test + public void testXmlCRUD() throws IOException, SAXException, ParserConfigurationException, TransformerException, XMLStreamException + { + String filename = "xml-original-test.xml"; + String uri = "/write-xml-XMLEventReaderHandle/"; + + System.out.println("Running testXmlCRUD"); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write the doc + writeDocumentReaderHandle(client, filename, uri, "XML"); + + // read the document + XMLEventReaderHandle readHandle = readDocumentUsingXMLEventReaderHandle(client, uri + filename, "XML"); + + // access the document content + XMLEventReader fileRead = readHandle.get(); + + String readContent = convertXMLEventReaderToString(fileRead); + String readContentCrop = readContent.substring(0, readContent.length()-11); + System.out.println(readContentCrop); + + // get xml document for expected result + Document expectedDoc = expectedXMLDocument(filename); + + String expectedContent = convertXMLDocumentToString(expectedDoc); + System.out.println(expectedContent); + // convert actual string to xml doc + Document readDoc = convertStringToXMLDocument(readContentCrop); + + assertXMLEqual("Write XML difference", expectedDoc,readDoc); + + // update the doc + // acquire the content for update + String updateFilename = "xml-updated-test.xml"; + updateDocumentReaderHandle(client, updateFilename, uri + filename, "XML"); + + // read the document + XMLEventReaderHandle updateHandle = readDocumentUsingXMLEventReaderHandle(client, uri + filename, "XML"); + + // access the document content + XMLEventReader fileReadUpdate = updateHandle.get(); + + String readContentUpdate = convertXMLEventReaderToString(fileReadUpdate); + String readContentUpdateCrop = readContentUpdate.substring(0,readContentUpdate.length()-11); + // get xml document for expected result + Document expectedDocUpdate = expectedXMLDocument(updateFilename); + + // convert actual string to xml doc + Document readDocUpdate = convertStringToXMLDocument(readContentUpdateCrop); + + assertXMLEqual("Write XML difference", expectedDocUpdate, readDocUpdate); + + // delete the document + deleteDocument(client, uri + filename, "XML"); + + // read the deleted document + //assertFalse("Document is not deleted", isDocumentExist(client, "/write-xml-readerhandle/" + filename, "XML")); + + String exception = ""; + try + { + readDocumentReaderHandle(client, uri + filename, "XML"); + } + catch (Exception e) { exception = e.toString(); } + + String expectedException = "Could not read non-existent document"; + boolean documentIsDeleted = exception.contains(expectedException); + assertTrue("Document is not deleted", documentIsDeleted); + + // release the client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } } \ No newline at end of file diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestXMLMultiByte.java b/test-complete/src/test/java/com/marklogic/javaclient/TestXMLMultiByte.java index 422a82499..65210e30a 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestXMLMultiByte.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestXMLMultiByte.java @@ -1,104 +1,88 @@ -package com.marklogic.javaclient; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.io.StringReader; -import java.io.StringWriter; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.OutputKeys; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; - -import org.w3c.dom.Document; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.DOMHandle; -import com.marklogic.client.io.InputStreamHandle; -import com.marklogic.javaclient.BasicJavaClientREST; - -import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; -import org.custommonkey.xmlunit.XMLUnit; -import org.junit.*; -public class TestXMLMultiByte extends BasicJavaClientREST { - - private static String dbName = "XMLMultiByteDB"; - private static String [] fNames = {"XMLMultiByteDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,8011); - } - -@SuppressWarnings("deprecation") -@Test public void testXmlMultibyte() throws IOException, SAXException, ParserConfigurationException, TransformerException - { - String filename = "multibyte-original.xml"; - String uri = "/write-xml-multibyte/"; - - System.out.println("Running testXmlMultibyte"); - - XMLUnit.setIgnoreWhitespace(true); - XMLUnit.setNormalizeWhitespace(true); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write docs - writeDocumentUsingInputStreamHandle(client, filename, uri, "XML"); - - // read docs - DOMHandle contentHandle = readDocumentUsingDOMHandle(client, uri + filename, "XML"); - - // get the contents - Document readDoc = contentHandle.get(); - - // get xml document for expected result - Document expectedDoc = expectedXMLDocument(filename); - - assertXMLEqual("Write XML difference", expectedDoc, readDoc); - - // update the doc - // acquire the content for update - String updateFilename = "multibyte-updated.xml"; - updateDocumentUsingInputStreamHandle(client, updateFilename, uri + filename, "XML"); - - // read the document - DOMHandle updateHandle = readDocumentUsingDOMHandle(client, uri + filename, "XML"); - - // get the contents - Document readDocUpdate = updateHandle.get(); - - // get xml document for expected result - Document expectedDocUpdate = expectedXMLDocument(updateFilename); - - assertXMLEqual("Write XML difference", expectedDocUpdate, readDocUpdate); - - // delete the document - deleteDocument(client, uri + filename, "XML"); - - // read the deleted document - //assertFalse("Document is not deleted", isDocumentExist(client, uri + filename, "XML")); - - // release client - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames,restServerName); - } -} +package com.marklogic.javaclient; + +import java.io.IOException; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.javaclient.BasicJavaClientREST; + +import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; +import org.custommonkey.xmlunit.XMLUnit; +import org.junit.*; +public class TestXMLMultiByte extends BasicJavaClientREST { + + private static String dbName = "XMLMultiByteDB"; + private static String [] fNames = {"XMLMultiByteDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,8011); + } + + @Test + public void testXmlMultibyte() throws IOException, SAXException, ParserConfigurationException, TransformerException + { + String filename = "multibyte-original.xml"; + String uri = "/write-xml-multibyte/"; + + System.out.println("Running testXmlMultibyte"); + + XMLUnit.setIgnoreWhitespace(true); + XMLUnit.setNormalizeWhitespace(true); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + writeDocumentUsingInputStreamHandle(client, filename, uri, "XML"); + + // read docs + DOMHandle contentHandle = readDocumentUsingDOMHandle(client, uri + filename, "XML"); + + // get the contents + Document readDoc = contentHandle.get(); + + // get xml document for expected result + Document expectedDoc = expectedXMLDocument(filename); + + assertXMLEqual("Write XML difference", expectedDoc, readDoc); + + // update the doc + // acquire the content for update + String updateFilename = "multibyte-updated.xml"; + updateDocumentUsingInputStreamHandle(client, updateFilename, uri + filename, "XML"); + + // read the document + DOMHandle updateHandle = readDocumentUsingDOMHandle(client, uri + filename, "XML"); + + // get the contents + Document readDocUpdate = updateHandle.get(); + + // get xml document for expected result + Document expectedDocUpdate = expectedXMLDocument(updateFilename); + + assertXMLEqual("Write XML difference", expectedDocUpdate, readDocUpdate); + + // delete the document + deleteDocument(client, uri + filename, "XML"); + + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames,restServerName); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestXMLStreamReaderHandle.java b/test-complete/src/test/java/com/marklogic/javaclient/TestXMLStreamReaderHandle.java index c5219f45c..fdbb25920 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestXMLStreamReaderHandle.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestXMLStreamReaderHandle.java @@ -1,94 +1,84 @@ -package com.marklogic.javaclient; - -import java.io.IOException; -import java.io.InputStream; -import java.io.Reader; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.stream.XMLEventReader; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamReader; -import javax.xml.transform.TransformerException; - -import org.custommonkey.xmlunit.XMLUnit; -import org.w3c.dom.Document; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.ReaderHandle; -import com.marklogic.client.io.XMLStreamReaderHandle; -import com.marklogic.javaclient.BasicJavaClientREST; - -import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; -import static org.junit.Assert.*; - -import org.junit.*; -public class TestXMLStreamReaderHandle extends BasicJavaClientREST { - - - private static String dbName = "XMLStreamReaderHandleDB"; - private static String [] fNames = {"XMLStreamReaderHandleDB-1"}; - private static String restServerName = "REST-Java-Client-API-Server"; - -@BeforeClass public static void setUp() throws Exception - { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0],restServerName,8011); - } - -@SuppressWarnings("deprecation") -@Test public void testXmlCRUD() throws IOException, SAXException, ParserConfigurationException, TransformerException, XMLStreamException - { - String filename = "xml-original-test.xml"; - String uri = "/write-xml-XMLStreamReaderHandle/"; - - System.out.println("Running testXmlCRUD"); - - // connect the client - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - // write the doc - writeDocumentReaderHandle(client, filename, uri, "XML"); - - // read the document - XMLStreamReaderHandle readHandle = readDocumentUsingXMLStreamReaderHandle(client, uri + filename, "XML"); - - // access the document content - XMLStreamReader fileRead = readHandle.get(); - String readContent = convertXMLStreamReaderToString(fileRead); - - // get xml document for expected result - Document expectedDoc = expectedXMLDocument(filename); - String expectedContent = convertXMLDocumentToString(expectedDoc); - expectedContent = "null"+expectedContent.substring(expectedContent.indexOf("")+6, expectedContent.indexOf("")); - assertEquals("Write XML difference", expectedContent,readContent); - - // delete the document - deleteDocument(client, uri + filename, "XML"); - - // read the deleted document - //assertFalse("Document is not deleted", isDocumentExist(client, "/write-xml-readerhandle/" + filename, "XML")); - - String exception = ""; - try - { - ReaderHandle deleteHandle = readDocumentReaderHandle(client, uri + filename, "XML"); - } catch (Exception e) { exception = e.toString(); } - - String expectedException = "Could not read non-existent document"; - boolean documentIsDeleted = exception.contains(expectedException); - assertTrue("Document is not deleted", documentIsDeleted); - - // release the client - client.release(); - } - -@AfterClass public static void tearDown() throws Exception - { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames,restServerName); - } +package com.marklogic.javaclient; + +import java.io.IOException; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.transform.TransformerException; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.ReaderHandle; +import com.marklogic.client.io.XMLStreamReaderHandle; +import com.marklogic.javaclient.BasicJavaClientREST; + +import static org.junit.Assert.*; + +import org.junit.*; +public class TestXMLStreamReaderHandle extends BasicJavaClientREST { + private static String dbName = "XMLStreamReaderHandleDB"; + private static String [] fNames = {"XMLStreamReaderHandleDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + @BeforeClass public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0],restServerName,8011); + } + + @Test + public void testXmlCRUD() throws IOException, SAXException, ParserConfigurationException, TransformerException, XMLStreamException + { + String filename = "xml-original-test.xml"; + String uri = "/write-xml-XMLStreamReaderHandle/"; + + System.out.println("Running testXmlCRUD"); + + // connect the client + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write the doc + writeDocumentReaderHandle(client, filename, uri, "XML"); + + // read the document + XMLStreamReaderHandle readHandle = readDocumentUsingXMLStreamReaderHandle(client, uri + filename, "XML"); + + // access the document content + XMLStreamReader fileRead = readHandle.get(); + String readContent = convertXMLStreamReaderToString(fileRead); + + // get xml document for expected result + Document expectedDoc = expectedXMLDocument(filename); + String expectedContent = convertXMLDocumentToString(expectedDoc); + expectedContent = "null"+expectedContent.substring(expectedContent.indexOf("")+6, expectedContent.indexOf("")); + assertEquals("Write XML difference", expectedContent,readContent); + + // delete the document + deleteDocument(client, uri + filename, "XML"); + + String exception = ""; + try + { + readDocumentReaderHandle(client, uri + filename, "XML"); + } + catch (Exception e) { exception = e.toString(); } + + String expectedException = "Could not read non-existent document"; + boolean documentIsDeleted = exception.contains(expectedException); + assertTrue("Document is not deleted", documentIsDeleted); + + // release the client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames,restServerName); + } } \ No newline at end of file diff --git a/test-complete/src/test/java/com/marklogic/javaclient/ThreadClass.java b/test-complete/src/test/java/com/marklogic/javaclient/ThreadClass.java index 864869b5c..8869ab21a 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/ThreadClass.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/ThreadClass.java @@ -1,47 +1,45 @@ -package com.marklogic.javaclient; - -import org.junit.BeforeClass; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.document.TextDocumentManager; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.StringHandle; - -public class ThreadClass extends Thread{ - - String msg; - - public void run() - { - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); - - TextDocumentManager docMgr = client.newTextDocumentManager(); - - for(int i=1; i<=5; i++) - { - System.out.println("Writing document from: "+ msg); - - if(msg == "Thread A") - { - // write docs - String docId = "/multithread-content-A/filename" + i + ".txt"; - docMgr.write(docId, new StringHandle().with("This is so foo")); - } - else if(msg == "Thread B") - { - // write docs - String docId = "/multithread-content-B/filename" + i + ".txt"; - docMgr.write(docId, new StringHandle().with("This is so foo")); - } - } - - // release client - client.release(); - } - - ThreadClass(String mg) - { - msg=mg; - } -} +package com.marklogic.javaclient; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.document.TextDocumentManager; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.StringHandle; + +public class ThreadClass extends Thread{ + + String msg; + + public void run() + { + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST); + + TextDocumentManager docMgr = client.newTextDocumentManager(); + + for(int i=1; i<=5; i++) + { + System.out.println("Writing document from: "+ msg); + + if(msg == "Thread A") + { + // write docs + String docId = "/multithread-content-A/filename" + i + ".txt"; + docMgr.write(docId, new StringHandle().with("This is so foo")); + } + else if(msg == "Thread B") + { + // write docs + String docId = "/multithread-content-B/filename" + i + ".txt"; + docMgr.write(docId, new StringHandle().with("This is so foo")); + } + } + + // release client + client.release(); + } + + ThreadClass(String mg) + { + msg=mg; + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/ThreadSearch.java b/test-complete/src/test/java/com/marklogic/javaclient/ThreadSearch.java index 38f230620..0f0f6d007 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/ThreadSearch.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/ThreadSearch.java @@ -1,62 +1,63 @@ -package com.marklogic.javaclient; - -import java.util.Random; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.query.QueryManager; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.query.StringQueryDefinition; -import com.marklogic.client.io.SearchHandle; - -public class ThreadSearch extends Thread { - - String msg; - long totalResultsArray[] = new long[10]; - long totalAllResults = 0; - - public void run() - { - long totalResults = 0; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-reader", "x", Authentication.DIGEST); - - QueryManager queryMgr = client.newQueryManager(); - StringQueryDefinition querydef = queryMgr.newStringDefinition(null); - - // create handle - SearchHandle resultsHandle = new SearchHandle(); - - for(int i=1; i<=10; i++) - { - System.out.println("Searching document " + i + " from: " + msg); - - // search docs - querydef.setCriteria("alert"); - queryMgr.search(querydef, resultsHandle); - - totalResults = resultsHandle.getTotalResults(); - - System.out.println("Results: " + totalResults + " documents returned"); - - totalResultsArray[i-1] = totalResults; - - totalAllResults = totalAllResults + totalResults; - - Random rand = new Random(); - int r = rand.nextInt(3000) + 1000; - - try { - Thread.sleep(r); - } catch (InterruptedException e) { e.printStackTrace(); } - } - - // release client - client.release(); - } - - ThreadSearch(String mg) - { - msg=mg; - } -} +package com.marklogic.javaclient; + +import java.util.Random; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.query.StringQueryDefinition; +import com.marklogic.client.io.SearchHandle; + +public class ThreadSearch extends Thread { + + String msg; + long totalResultsArray[] = new long[10]; + long totalAllResults = 0; + + public void run() + { + long totalResults = 0; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-reader", "x", Authentication.DIGEST); + + QueryManager queryMgr = client.newQueryManager(); + StringQueryDefinition querydef = queryMgr.newStringDefinition(null); + + // create handle + SearchHandle resultsHandle = new SearchHandle(); + + for(int i=1; i<=10; i++) + { + System.out.println("Searching document " + i + " from: " + msg); + + // search docs + querydef.setCriteria("alert"); + queryMgr.search(querydef, resultsHandle); + + totalResults = resultsHandle.getTotalResults(); + + System.out.println("Results: " + totalResults + " documents returned"); + + totalResultsArray[i-1] = totalResults; + + totalAllResults = totalAllResults + totalResults; + + Random rand = new Random(); + int r = rand.nextInt(3000) + 1000; + + try { + Thread.sleep(r); + } + catch (InterruptedException e) { e.printStackTrace(); } + } + + // release client + client.release(); + } + + ThreadSearch(String mg) + { + msg=mg; + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/ThreadWrite.java b/test-complete/src/test/java/com/marklogic/javaclient/ThreadWrite.java index 1500a61f2..b7253f8fb 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/ThreadWrite.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/ThreadWrite.java @@ -1,50 +1,51 @@ -package com.marklogic.javaclient; - -import java.io.File; -import java.util.Random; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.io.FileHandle; - -public class ThreadWrite extends Thread{ - - String msg; - - public void run() - { - String filename = "flipper.xml"; - - DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); - - File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); - - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - - for(int i=1; i<=15; i++) - { - System.out.println("Writing document " + i + " from: " + msg); - - // write docs - String docId = "/multithread-write/filename" + i + ".xml"; - docMgr.write(docId, new FileHandle().with(file)); - - Random rand = new Random(); - int r = rand.nextInt(2000) + 1000; - - try { - Thread.sleep(r); - } catch (InterruptedException e) { e.printStackTrace(); } - } - - // release client - client.release(); - } - - ThreadWrite(String mg) - { - msg=mg; - } -} +package com.marklogic.javaclient; + +import java.io.File; +import java.util.Random; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.io.FileHandle; + +public class ThreadWrite extends Thread{ + + String msg; + + public void run() + { + String filename = "flipper.xml"; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + File file = new File("src/test/java/com/marklogic/javaclient/data/" + filename); + + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + + for(int i=1; i<=15; i++) + { + System.out.println("Writing document " + i + " from: " + msg); + + // write docs + String docId = "/multithread-write/filename" + i + ".xml"; + docMgr.write(docId, new FileHandle().with(file)); + + Random rand = new Random(); + int r = rand.nextInt(2000) + 1000; + + try { + Thread.sleep(r); + } + catch (InterruptedException e) { e.printStackTrace(); } + } + + // release client + client.release(); + } + + ThreadWrite(String mg) + { + msg=mg; + } +} From 835269abc8a346e1f849246d9656d16dae71b725 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Thu, 21 Aug 2014 09:08:34 -0700 Subject: [PATCH 084/357] Code cleanup --- .../javaclient/TestBulkWriteMetadata1.java | 419 ++++----- .../javaclient/TestBulkWriteMetadata2.java | 827 +++++++++--------- 2 files changed, 625 insertions(+), 621 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata1.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata1.java index 7a39de14c..3e840d64d 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata1.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata1.java @@ -59,21 +59,21 @@ * */ public class TestBulkWriteMetadata1 extends BasicJavaClientREST { - private static String dbName = "TestBulkWriteDefaultMetadataDB"; private static String [] fNames = {"TestBulkWriteDefaultMetadataDB-1"}; private static String restServerName = "REST-Java-Client-API-Server"; private static int restPort = 8011; private DatabaseClient client ; + /** * @throws java.lang.Exception */ @BeforeClass public static void setUpBeforeClass() throws Exception { - System.out.println("In Setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); - createRESTUser("app-user", "password", "rest-writer","rest-reader" ); - + System.out.println("In Setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + createRESTUser("app-user", "password", "rest-writer","rest-reader" ); + } /** @@ -92,7 +92,7 @@ public static void tearDownAfterClass() throws Exception { @Before public void setUp() throws Exception { // create new connection for each test below - client = DatabaseClientFactory.newClient("localhost", restPort, "app-user", "password", Authentication.DIGEST); + client = DatabaseClientFactory.newClient("localhost", restPort, "app-user", "password", Authentication.DIGEST); } /** @@ -101,226 +101,233 @@ public void setUp() throws Exception { @After public void tearDown() throws Exception { System.out.println("Running clear script"); - // release client - client.release(); + // release client + client.release(); } - + public DocumentMetadataHandle setMetadata(){ - // create and initialize a handle on the metadata + // create and initialize a handle on the metadata DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); - metadataHandle.getCollections().addAll("my-collection1","my-collection2"); - metadataHandle.getPermissions().add("app-user", Capability.UPDATE, Capability.READ); - metadataHandle.getProperties().put("reviewed", true); - metadataHandle.getProperties().put("myString", "foo"); - metadataHandle.getProperties().put("myInteger", 10); - metadataHandle.getProperties().put("myDecimal", 34.56678); - metadataHandle.getProperties().put("myCalendar", Calendar.getInstance().get(Calendar.YEAR)); - metadataHandle.setQuality(23); - return metadataHandle; + metadataHandle.getCollections().addAll("my-collection1","my-collection2"); + metadataHandle.getPermissions().add("app-user", Capability.UPDATE, Capability.READ); + metadataHandle.getProperties().put("reviewed", true); + metadataHandle.getProperties().put("myString", "foo"); + metadataHandle.getProperties().put("myInteger", 10); + metadataHandle.getProperties().put("myDecimal", 34.56678); + metadataHandle.getProperties().put("myCalendar", Calendar.getInstance().get(Calendar.YEAR)); + metadataHandle.setQuality(23); + return metadataHandle; } + public void validateMetadata(DocumentMetadataHandle mh){ + // get metadata values + DocumentProperties properties = mh.getProperties(); + DocumentPermissions permissions = mh.getPermissions(); + DocumentCollections collections = mh.getCollections(); - // get metadata values - DocumentProperties properties = mh.getProperties(); - DocumentPermissions permissions = mh.getPermissions(); - DocumentCollections collections = mh.getCollections(); - - // Properties - String actualProperties = getDocumentPropertiesString(properties); - - assertTrue("Document properties difference in size value", actualProperties.contains("size:5")); - assertTrue("Document property reviewed not found or not correct", actualProperties.contains("reviewed:true")); - assertTrue("Document property myInteger not found or not correct", actualProperties.contains("myInteger:10")); - assertTrue("Document property myDecimal not found or not correct", actualProperties.contains("myDecimal:34.56678")); - assertTrue("Document property myCalendar not found or not correct", actualProperties.contains("myCalendar:2014")); - assertTrue("Document property myString not found or not correct", actualProperties.contains("myString:foo")); - - // Permissions - String actualPermissions = getDocumentPermissionsString(permissions); - assertTrue("Document permissions difference in size value", actualPermissions.contains("size:3")); - assertTrue("Document permissions difference in rest-reader permission", actualPermissions.contains("rest-reader:[READ]")); - assertTrue("Document permissions difference in rest-writer permission", actualPermissions.contains("rest-writer:[UPDATE]")); - assertTrue("Document permissions difference in app-user permissions", - (actualPermissions.contains("app-user:[UPDATE, READ]") || actualPermissions.contains("app-user:[READ, UPDATE]"))); - - // Collections - String actualCollections = getDocumentCollectionsString(collections); - assertTrue("Document collections difference in size value", actualCollections.contains("size:2")); - assertTrue("my-collection1 not found", actualCollections.contains("my-collection1")); - assertTrue("my-collection2 not found", actualCollections.contains("my-collection2")); + // Properties + String actualProperties = getDocumentPropertiesString(properties); + System.out.println("Returned properties: " + actualProperties); + + assertTrue("Document properties difference in size value", actualProperties.contains("size:5")); + assertTrue("Document property reviewed not found or not correct", actualProperties.contains("reviewed:true")); + assertTrue("Document property myInteger not found or not correct", actualProperties.contains("myInteger:10")); + assertTrue("Document property myDecimal not found or not correct", actualProperties.contains("myDecimal:34.56678")); + assertTrue("Document property myCalendar not found or not correct", actualProperties.contains("myCalendar:2014")); + assertTrue("Document property myString not found or not correct", actualProperties.contains("myString:foo")); + + // Permissions + String actualPermissions = getDocumentPermissionsString(permissions); + System.out.println("Returned permissions: " + actualPermissions); + + assertTrue("Document permissions difference in size value", actualPermissions.contains("size:3")); + assertTrue("Document permissions difference in rest-reader permission", actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue("Document permissions difference in app-user permissions", + (actualPermissions.contains("app-user:[UPDATE, READ]") || actualPermissions.contains("app-user:[READ, UPDATE]"))); + + // Collections + String actualCollections = getDocumentCollectionsString(collections); + System.out.println("Returned collections: " + actualCollections); + + assertTrue("Document collections difference in size value", actualCollections.contains("size:2")); + assertTrue("my-collection1 not found", actualCollections.contains("my-collection1")); + assertTrue("my-collection2 not found", actualCollections.contains("my-collection2")); } @Test public void testWriteMultipleTextDocWithDefaultMetadata() - { - String docId[] = {"/foo/test/myFoo1.txt","/foo/test/myFoo2.txt","/foo/test/myFoo3.txt"}; - - TextDocumentManager docMgr = client.newTextDocumentManager(); - - DocumentWriteSet writeset =docMgr.newWriteSet(); - // put metadata - DocumentMetadataHandle mh = setMetadata(); - - writeset.addDefault(mh); - writeset.add(docId[0], new StringHandle().with("This is so foo1")); - writeset.add(docId[1], new StringHandle().with("This is so foo2")); - writeset.add(docId[2], new StringHandle().with("This is so foo3")); - docMgr.write(writeset); - DocumentPage page = docMgr.read(docId); - - while(page.hasNext()){ - DocumentRecord rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh); - validateMetadata(mh); - } - validateMetadata(mh); - } + { + String docId[] = {"/foo/test/myFoo1.txt","/foo/test/myFoo2.txt","/foo/test/myFoo3.txt"}; + + TextDocumentManager docMgr = client.newTextDocumentManager(); + + DocumentWriteSet writeset =docMgr.newWriteSet(); + // put metadata + DocumentMetadataHandle mh = setMetadata(); + + writeset.addDefault(mh); + writeset.add(docId[0], new StringHandle().with("This is so foo1")); + writeset.add(docId[1], new StringHandle().with("This is so foo2")); + writeset.add(docId[2], new StringHandle().with("This is so foo3")); + docMgr.write(writeset); + DocumentPage page = docMgr.read(docId); + + while(page.hasNext()){ + DocumentRecord rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh); + validateMetadata(mh); + } + validateMetadata(mh); + } + @Test public void testWriteMultipleXMLDocWithDefaultMetadata() throws Exception - { - String docId[] = {"/foo/test/Foo1.xml","/foo/test/Foo2.xml","/foo/test/Foo3.xml"}; - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - // put metadata - DocumentMetadataHandle mh = setMetadata(); - - writeset.addDefault(mh); - writeset.add(docId[0], new DOMHandle(getDocumentContent("This is so foo1"))); - writeset.add(docId[1], new DOMHandle().with(getDocumentContent("This is so foo2"))); - writeset.add(docId[2], new DOMHandle().with(getDocumentContent("This is so foo3"))); - - docMgr.write(writeset); - - DocumentPage page = docMgr.read(docId); - - while(page.hasNext()){ - DocumentRecord rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh); - validateMetadata(mh); - } - validateMetadata(mh); - } + { + String docId[] = {"/foo/test/Foo1.xml","/foo/test/Foo2.xml","/foo/test/Foo3.xml"}; + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + // put metadata + DocumentMetadataHandle mh = setMetadata(); + + writeset.addDefault(mh); + writeset.add(docId[0], new DOMHandle(getDocumentContent("This is so foo1"))); + writeset.add(docId[1], new DOMHandle().with(getDocumentContent("This is so foo2"))); + writeset.add(docId[2], new DOMHandle().with(getDocumentContent("This is so foo3"))); + + docMgr.write(writeset); + + DocumentPage page = docMgr.read(docId); + + while(page.hasNext()){ + DocumentRecord rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh); + validateMetadata(mh); + } + validateMetadata(mh); + } + @Test public void testWriteMultipleBinaryDocWithDefaultMetadata() throws Exception - { - String docId[] = {"Pandakarlino.jpg","mlfavicon.png"}; - - BinaryDocumentManager docMgr = client.newBinaryDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - // put metadata - DocumentMetadataHandle mh = setMetadata(); - - writeset.addDefault(mh); - File file1= null,file2=null; - file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); - FileHandle handle1 = new FileHandle(file1); - writeset.add("/1/"+docId[0],handle1.withFormat(Format.BINARY)); - writeset.add("/2/"+docId[0],handle1.withFormat(Format.BINARY)); - file2 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[1]); - FileHandle handle2 = new FileHandle(file2); - writeset.add("/1/"+docId[1],handle2.withFormat(Format.BINARY)); - writeset.add("/2/"+docId[1],handle2.withFormat(Format.BINARY)); - - docMgr.write(writeset); - String uris[] = new String[102]; - int j=0; - for(int i =1;i<3;i++){ - uris[i]="/"+i+"/"+docId[j]; - uris[i]="/"+i+"/"+docId[j+1]; - } - DocumentPage page = docMgr.read(uris); - - while(page.hasNext()){ - DocumentRecord rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh); - System.out.println(rec.getUri()); - validateMetadata(mh); - } - validateMetadata(mh); - } + { + String docId[] = {"Pandakarlino.jpg","mlfavicon.png"}; + + BinaryDocumentManager docMgr = client.newBinaryDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + // put metadata + DocumentMetadataHandle mh = setMetadata(); + + writeset.addDefault(mh); + File file1= null,file2=null; + file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); + FileHandle handle1 = new FileHandle(file1); + writeset.add("/1/"+docId[0],handle1.withFormat(Format.BINARY)); + writeset.add("/2/"+docId[0],handle1.withFormat(Format.BINARY)); + file2 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[1]); + FileHandle handle2 = new FileHandle(file2); + writeset.add("/1/"+docId[1],handle2.withFormat(Format.BINARY)); + writeset.add("/2/"+docId[1],handle2.withFormat(Format.BINARY)); + + docMgr.write(writeset); + String uris[] = new String[102]; + int j=0; + for(int i =1;i<3;i++){ + uris[i]="/"+i+"/"+docId[j]; + uris[i]="/"+i+"/"+docId[j+1]; + } + DocumentPage page = docMgr.read(uris); + + while(page.hasNext()){ + DocumentRecord rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh); + System.out.println(rec.getUri()); + validateMetadata(mh); + } + validateMetadata(mh); + } @Test public void testWriteMultipleJSONDocsWithDefaultMetadata() throws Exception - { - String docId[] = {"/a.json","/b.json","/c.json"}; - String json1 = new String("{\"animal\":\"dog\", \"says\":\"woof\"}"); - String json2 = new String("{\"animal\":\"cat\", \"says\":\"meow\"}"); - String json3 = new String("{\"animal\":\"rat\", \"says\":\"keek\"}"); - Reader strReader = new StringReader(json1); - JSONDocumentManager docMgr = client.newJSONDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - // put metadata - DocumentMetadataHandle mh = setMetadata(); - - writeset.addDefault(mh); - writeset.add(docId[0],new ReaderHandle(strReader).withFormat(Format.JSON)); - writeset.add(docId[1],new ReaderHandle(new StringReader(json2))); - writeset.add(docId[2],new ReaderHandle(new StringReader(json3))); - - docMgr.write(writeset); - - DocumentPage page = docMgr.read(docId); - - while(page.hasNext()){ - DocumentRecord rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh); - System.out.println(rec.getUri()); - validateMetadata(mh); - } - validateMetadata(mh); - } + { + String docId[] = {"/a.json","/b.json","/c.json"}; + String json1 = new String("{\"animal\":\"dog\", \"says\":\"woof\"}"); + String json2 = new String("{\"animal\":\"cat\", \"says\":\"meow\"}"); + String json3 = new String("{\"animal\":\"rat\", \"says\":\"keek\"}"); + Reader strReader = new StringReader(json1); + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + // put metadata + DocumentMetadataHandle mh = setMetadata(); + + writeset.addDefault(mh); + writeset.add(docId[0],new ReaderHandle(strReader).withFormat(Format.JSON)); + writeset.add(docId[1],new ReaderHandle(new StringReader(json2))); + writeset.add(docId[2],new ReaderHandle(new StringReader(json3))); + + docMgr.write(writeset); + + DocumentPage page = docMgr.read(docId); + + while(page.hasNext()){ + DocumentRecord rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh); + System.out.println(rec.getUri()); + validateMetadata(mh); + } + validateMetadata(mh); + } + @Test public void testWriteGenericDocMgrWithDefaultMetadata() throws Exception - { - String docId[] = {"Pandakarlino.jpg","mlfavicon.png"}; - + { + String docId[] = {"Pandakarlino.jpg","mlfavicon.png"}; + GenericDocumentManager docMgr = client.newDocumentManager(); DocumentWriteSet writeset =docMgr.newWriteSet(); - // put metadata - DocumentMetadataHandle mh = setMetadata(); - - writeset.addDefault(mh); - File file1= null; - file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); - FileInputStream fis = new FileInputStream(file1); - InputStreamHandle handle1 = new InputStreamHandle(fis); - handle1.setFormat(Format.BINARY); - - writeset.add("/generic/Pandakarlino.jpg",handle1); - - 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",jh); - - String foo1 = "This is foo1 of byte Array"; - byte[] ba = foo1.getBytes(); - BytesHandle bh = new BytesHandle(ba); - bh.setFormat(Format.TEXT); - - writeset.add("/generic/foo1.txt",bh); - - DOMSource ds = new DOMSource(getDocumentContent("This is so foo1")); - SourceHandle sh = new SourceHandle(); - sh.set(ds); - sh.setFormat(Format.XML); - - writeset.add("/generic/foo.xml",sh); - - docMgr.write(writeset); - - DocumentPage page = docMgr.read("/generic/Pandakarlino.jpg","/generic/dog.json","/generic/foo1.txt","/generic/foo.xml"); - - while(page.hasNext()){ - DocumentRecord rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh); - System.out.println(rec.getUri()); - validateMetadata(mh); - } - validateMetadata(mh); - } + // put metadata + DocumentMetadataHandle mh = setMetadata(); + + writeset.addDefault(mh); + File file1= null; + file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); + FileInputStream fis = new FileInputStream(file1); + InputStreamHandle handle1 = new InputStreamHandle(fis); + handle1.setFormat(Format.BINARY); + + writeset.add("/generic/Pandakarlino.jpg",handle1); + + 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",jh); + + String foo1 = "This is foo1 of byte Array"; + byte[] ba = foo1.getBytes(); + BytesHandle bh = new BytesHandle(ba); + bh.setFormat(Format.TEXT); + writeset.add("/generic/foo1.txt",bh); + + DOMSource ds = new DOMSource(getDocumentContent("This is so foo1")); + SourceHandle sh = new SourceHandle(); + sh.set(ds); + sh.setFormat(Format.XML); + + writeset.add("/generic/foo.xml",sh); + + docMgr.write(writeset); + + DocumentPage page = docMgr.read("/generic/Pandakarlino.jpg","/generic/dog.json","/generic/foo1.txt","/generic/foo.xml"); + + while(page.hasNext()){ + DocumentRecord rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh); + System.out.println(rec.getUri()); + validateMetadata(mh); + } + validateMetadata(mh); + } } diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata2.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata2.java index bfadc6fb1..f7de60ce6 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata2.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteMetadata2.java @@ -69,11 +69,11 @@ public class TestBulkWriteMetadata2 extends BasicJavaClientREST{ */ @BeforeClass public static void setUpBeforeClass() throws Exception { - System.out.println("In Setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); - createRESTUser("app-user", "password","rest-writer","rest-reader" ); - createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); - setMaintainLastModified(dbName, true); + System.out.println("In Setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + createRESTUser("app-user", "password","rest-writer","rest-reader" ); + createRESTUserWithPermissions("usr1", "password",getPermissionNode("flexrep-eval",Capability.READ),getCollectionNode("http://permission-collections/"), "rest-writer","rest-reader" ); + setMaintainLastModified(dbName, true); } /** @@ -93,7 +93,7 @@ public static void tearDownAfterClass() throws Exception { @Before public void setUp() throws Exception { // create new connection for each test below - client = DatabaseClientFactory.newClient("localhost", restPort, "usr1", "password", Authentication.DIGEST); + client = DatabaseClientFactory.newClient("localhost", restPort, "usr1", "password", Authentication.DIGEST); } /** @@ -102,450 +102,447 @@ public void setUp() throws Exception { @After public void tearDown() throws Exception { System.out.println("Running clear script"); - // release client - client.release(); + // release client + client.release(); } - + public DocumentMetadataHandle setMetadata(){ - // create and initialize a handle on the metadata + // create and initialize a handle on the metadata DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); - metadataHandle.getCollections().addAll("my-collection1","my-collection2"); - metadataHandle.getPermissions().add("app-user", Capability.UPDATE, Capability.READ); - metadataHandle.getProperties().put("reviewed", true); - metadataHandle.getProperties().put("myString", "foo"); - metadataHandle.getProperties().put("myInteger", 10); - metadataHandle.getProperties().put("myDecimal", 34.56678); - metadataHandle.getProperties().put("myCalendar", Calendar.getInstance().get(Calendar.YEAR)); - metadataHandle.setQuality(23); - return metadataHandle; + metadataHandle.getCollections().addAll("my-collection1","my-collection2"); + metadataHandle.getPermissions().add("app-user", Capability.UPDATE, Capability.READ); + metadataHandle.getProperties().put("reviewed", true); + metadataHandle.getProperties().put("myString", "foo"); + metadataHandle.getProperties().put("myInteger", 10); + metadataHandle.getProperties().put("myDecimal", 34.56678); + metadataHandle.getProperties().put("myCalendar", Calendar.getInstance().get(Calendar.YEAR)); + metadataHandle.setQuality(23); + return metadataHandle; } + public void validateMetadata(DocumentMetadataHandle mh){ + // get metadata values + DocumentProperties properties = mh.getProperties(); + DocumentPermissions permissions = mh.getPermissions(); + DocumentCollections collections = mh.getCollections(); + + // Properties + // String expectedProperties = "size:5|reviewed:true|myInteger:10|myDecimal:34.56678|myCalendar:2014|myString:foo|"; + String actualProperties = getDocumentPropertiesString(properties); + boolean result = actualProperties.contains("size:6|"); + assertTrue("Document properties count", result); + + // Permissions + String actualPermissions = getDocumentPermissionsString(permissions); + System.out.println(actualPermissions); - // get metadata values - DocumentProperties properties = mh.getProperties(); - DocumentPermissions permissions = mh.getPermissions(); - DocumentCollections collections = mh.getCollections(); - - // Properties - // String expectedProperties = "size:5|reviewed:true|myInteger:10|myDecimal:34.56678|myCalendar:2014|myString:foo|"; - String actualProperties = getDocumentPropertiesString(properties); - boolean result = actualProperties.contains("size:6|"); - assertTrue("Document properties count", result); - - // Permissions - String actualPermissions = getDocumentPermissionsString(permissions); - System.out.println(actualPermissions); - - assertTrue("Document permissions difference in size value", actualPermissions.contains("size:4")); - assertTrue("Document permissions difference in flexrep-eval permission", actualPermissions.contains("flexrep-eval:[READ]")); - assertTrue("Document permissions difference in rest-reader permission", actualPermissions.contains("rest-reader:[READ]")); - assertTrue("Document permissions difference in rest-writer permission", actualPermissions.contains("rest-writer:[UPDATE]")); - assertTrue("Document permissions difference in app-user permissions", - (actualPermissions.contains("app-user:[UPDATE, READ]") || actualPermissions.contains("app-user:[READ, UPDATE]"))); - - // Collections - String actualCollections = getDocumentCollectionsString(collections); - System.out.println(collections); - - assertTrue("Document collections difference in size value", actualCollections.contains("size:2")); - assertTrue("my-collection1 not found", actualCollections.contains("my-collection1")); - assertTrue("my-collection2 not found", actualCollections.contains("my-collection2")); + assertTrue("Document permissions difference in size value", actualPermissions.contains("size:4")); + assertTrue("Document permissions difference in flexrep-eval permission", actualPermissions.contains("flexrep-eval:[READ]")); + assertTrue("Document permissions difference in rest-reader permission", actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue("Document permissions difference in app-user permissions", + (actualPermissions.contains("app-user:[UPDATE, READ]") || actualPermissions.contains("app-user:[READ, UPDATE]"))); + + // Collections + String actualCollections = getDocumentCollectionsString(collections); + System.out.println(collections); + + assertTrue("Document collections difference in size value", actualCollections.contains("size:2")); + assertTrue("my-collection1 not found", actualCollections.contains("my-collection1")); + assertTrue("my-collection2 not found", actualCollections.contains("my-collection2")); } public void validateDefaultMetadata(DocumentMetadataHandle mh){ + // get metadata values + DocumentProperties properties = mh.getProperties(); + DocumentPermissions permissions = mh.getPermissions(); + DocumentCollections collections = mh.getCollections(); + + // Properties + String actualProperties = getDocumentPropertiesString(properties); + boolean result =actualProperties.contains("size:1|"); + System.out.println(actualProperties +result); + assertTrue("Document default last modified properties count1?", result); + - // get metadata values - DocumentProperties properties = mh.getProperties(); - DocumentPermissions permissions = mh.getPermissions(); - DocumentCollections collections = mh.getCollections(); - - // Properties - String actualProperties = getDocumentPropertiesString(properties); - boolean result =actualProperties.contains("size:1|"); - System.out.println(actualProperties +result); - assertTrue("Document default last modified properties count1?", result); - - - // Permissions - String actualPermissions = getDocumentPermissionsString(permissions); - - assertTrue("Document permissions difference in size value", actualPermissions.contains("size:3")); - assertTrue("Document permissions difference in flexrep-eval permission", actualPermissions.contains("flexrep-eval:[READ]")); - assertTrue("Document permissions difference in rest-reader permission", actualPermissions.contains("rest-reader:[READ]")); - assertTrue("Document permissions difference in rest-writer permission", actualPermissions.contains("rest-writer:[UPDATE]")); - - // Collections - String expectedCollections = "size:1|http://permission-collections/|"; - String actualCollections = getDocumentCollectionsString(collections); - - assertEquals("Document collections difference", expectedCollections, actualCollections); -// System.out.println(actualPermissions); + // Permissions + String actualPermissions = getDocumentPermissionsString(permissions); + + assertTrue("Document permissions difference in size value", actualPermissions.contains("size:3")); + assertTrue("Document permissions difference in flexrep-eval permission", actualPermissions.contains("flexrep-eval:[READ]")); + assertTrue("Document permissions difference in rest-reader permission", actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", actualPermissions.contains("rest-writer:[UPDATE]")); + + // Collections + String expectedCollections = "size:1|http://permission-collections/|"; + String actualCollections = getDocumentCollectionsString(collections); + + assertEquals("Document collections difference", expectedCollections, actualCollections); } + @Test public void testWriteMultipleTextDocWithDefaultMetadata2() - { + { DocumentMetadataHandle mh1,mh2; - String docId[] = {"/foo/test/myFoo1.txt","/foo/test/myFoo2.txt","/foo/test/myFoo3.txt", - "/foo/test/myFoo4.txt","/foo/test/myFoo5.txt","/foo/test/myFoo6.txt", - "/foo/test/myFoo7.txt","/foo/test/myFoo8.txt","/foo/test/myFoo9.txt"}; - - TextDocumentManager docMgr = client.newTextDocumentManager(); - - DocumentWriteSet writeset =docMgr.newWriteSet(); - - writeset.add(docId[0], new StringHandle().with("This is so foo1")); - writeset.add(docId[1], new StringHandle().with("This is so foo2")); - writeset.add(docId[2], new StringHandle().with("This is so foo3")); - docMgr.write(writeset); - //Default system property document set - System.out.println("Assert if document set doesnt show default properties"); - DocumentPage page = docMgr.read(docId); - mh1=new DocumentMetadataHandle(); - while(page.hasNext()){ - DocumentRecord rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh1); - validateDefaultMetadata(mh1); - } - validateDefaultMetadata(mh1); - - //Adding document specific properties in document set2 - - System.out.println("Assert if document 5 show default properties"); - - writeset =docMgr.newWriteSet(); - // put metadata - mh2 = setMetadata(); - - writeset.add(docId[3], new StringHandle().with("This is so foo4")); - writeset.add(docId[4],mh2, new StringHandle().with("This is so foo5")); - writeset.add(docId[5], new StringHandle().with("This is so foo6")); - docMgr.write(writeset); - - - page = docMgr.read(docId[4]); - mh2=new DocumentMetadataHandle(); - DocumentRecord rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh2); - validateMetadata(mh2); - page = docMgr.read(docId[3]); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh2); - validateDefaultMetadata(mh2); - page = docMgr.read(docId[5]); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh2); - validateDefaultMetadata(mh2); - - System.out.println("Assert if documents 8 and 9 show default properties and if 7 doesnt show default"); - - writeset =docMgr.newWriteSet(); - // put metadata - mh2 = setMetadata(); - - writeset.add(docId[6], new StringHandle().with("This is so foo4")); - writeset.addDefault(mh2); - writeset.add(docId[7], new StringHandle().with("This is so foo5")); - writeset.add(docId[8], new StringHandle().with("This is so foo6")); - docMgr.write(writeset); - - page = docMgr.read(docId[6]); - mh2=new DocumentMetadataHandle(); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh2); - validateDefaultMetadata(mh2); - - page = docMgr.read(docId[7]); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh2); - validateMetadata(mh2); - - page = docMgr.read(docId[8]); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh2); - validateMetadata(mh2); - - - } + String docId[] = {"/foo/test/myFoo1.txt","/foo/test/myFoo2.txt","/foo/test/myFoo3.txt", + "/foo/test/myFoo4.txt","/foo/test/myFoo5.txt","/foo/test/myFoo6.txt", + "/foo/test/myFoo7.txt","/foo/test/myFoo8.txt","/foo/test/myFoo9.txt"}; + + TextDocumentManager docMgr = client.newTextDocumentManager(); + + DocumentWriteSet writeset =docMgr.newWriteSet(); + + writeset.add(docId[0], new StringHandle().with("This is so foo1")); + writeset.add(docId[1], new StringHandle().with("This is so foo2")); + writeset.add(docId[2], new StringHandle().with("This is so foo3")); + docMgr.write(writeset); + //Default system property document set + System.out.println("Assert if document set doesnt show default properties"); + DocumentPage page = docMgr.read(docId); + mh1=new DocumentMetadataHandle(); + while(page.hasNext()){ + DocumentRecord rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh1); + validateDefaultMetadata(mh1); + } + validateDefaultMetadata(mh1); + + //Adding document specific properties in document set2 + + System.out.println("Assert if document 5 show default properties"); + + writeset =docMgr.newWriteSet(); + // put metadata + mh2 = setMetadata(); + + writeset.add(docId[3], new StringHandle().with("This is so foo4")); + writeset.add(docId[4],mh2, new StringHandle().with("This is so foo5")); + writeset.add(docId[5], new StringHandle().with("This is so foo6")); + docMgr.write(writeset); + + + page = docMgr.read(docId[4]); + mh2=new DocumentMetadataHandle(); + DocumentRecord rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh2); + validateMetadata(mh2); + page = docMgr.read(docId[3]); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh2); + validateDefaultMetadata(mh2); + page = docMgr.read(docId[5]); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh2); + validateDefaultMetadata(mh2); + + System.out.println("Assert if documents 8 and 9 show default properties and if 7 doesnt show default"); + + writeset =docMgr.newWriteSet(); + // put metadata + mh2 = setMetadata(); + + writeset.add(docId[6], new StringHandle().with("This is so foo4")); + writeset.addDefault(mh2); + writeset.add(docId[7], new StringHandle().with("This is so foo5")); + writeset.add(docId[8], new StringHandle().with("This is so foo6")); + docMgr.write(writeset); + + page = docMgr.read(docId[6]); + mh2=new DocumentMetadataHandle(); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh2); + validateDefaultMetadata(mh2); + + page = docMgr.read(docId[7]); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh2); + validateMetadata(mh2); + + page = docMgr.read(docId[8]); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh2); + validateMetadata(mh2); + } + @Test public void testWriteMultipleXMLDocWithDefaultMetadata2() throws Exception - { + { + // put metadata + DocumentMetadataHandle mh1,mh2; + String docId[] = {"/foo/test/Foo1.xml","/foo/test/Foo2.xml","/foo/test/Foo3.xml", + "/foo/test/Foo4.xml","/foo/test/Foo5.xml","/foo/test/Foo6.xml", + "/foo/test/Foo7.xml","/foo/test/Foo8.xml","/foo/test/Foo8.xml"}; + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + + DocumentDescriptor docdisc = docMgr.newDescriptor("test1"); + + writeset.add(docdisc, new DOMHandle(getDocumentContent("This is so foo1"))); + writeset.add(docId[1], new DOMHandle().with(getDocumentContent("This is so foo2"))); + writeset.add(docId[2], new DOMHandle().with(getDocumentContent("This is so foo3"))); + + docMgr.write(writeset); + + DocumentPage page = docMgr.read(docId); + DocumentRecord rec; + mh1 = new DocumentMetadataHandle(); + while(page.hasNext()){ + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh1); + validateDefaultMetadata(mh1); + } + validateDefaultMetadata(mh1); + // put metadata - DocumentMetadataHandle mh1,mh2; - String docId[] = {"/foo/test/Foo1.xml","/foo/test/Foo2.xml","/foo/test/Foo3.xml", - "/foo/test/Foo4.xml","/foo/test/Foo5.xml","/foo/test/Foo6.xml", - "/foo/test/Foo7.xml","/foo/test/Foo8.xml","/foo/test/Foo8.xml"}; - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - - DocumentDescriptor docdisc = docMgr.newDescriptor("test1"); - - writeset.add(docdisc, new DOMHandle(getDocumentContent("This is so foo1"))); - writeset.add(docId[1], new DOMHandle().with(getDocumentContent("This is so foo2"))); - writeset.add(docId[2], new DOMHandle().with(getDocumentContent("This is so foo3"))); - - docMgr.write(writeset); - - DocumentPage page = docMgr.read(docId); - DocumentRecord rec; - mh1 = new DocumentMetadataHandle(); - while(page.hasNext()){ - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh1); - validateDefaultMetadata(mh1); - } - validateDefaultMetadata(mh1); - - // put metadata - mh2 = setMetadata(); - writeset =docMgr.newWriteSet(); - - writeset.add(docId[3], new DOMHandle(getDocumentContent("This is so foo4"))); - writeset.addDefault(mh2); - writeset.add(docId[4], new DOMHandle().with(getDocumentContent("This is so foo5"))); - writeset.add(docId[5], new DOMHandle().with(getDocumentContent("This is so foo6"))); - docMgr.write(writeset); - - page = docMgr.read(docId[3]); + mh2 = setMetadata(); + writeset =docMgr.newWriteSet(); + + writeset.add(docId[3], new DOMHandle(getDocumentContent("This is so foo4"))); + writeset.addDefault(mh2); + writeset.add(docId[4], new DOMHandle().with(getDocumentContent("This is so foo5"))); + writeset.add(docId[5], new DOMHandle().with(getDocumentContent("This is so foo6"))); + docMgr.write(writeset); + + page = docMgr.read(docId[3]); mh2=new DocumentMetadataHandle(); rec = page.next(); docMgr.readMetadata(rec.getUri(), mh2); validateDefaultMetadata(mh2); - + page = docMgr.read(docId[4]); rec = page.next(); docMgr.readMetadata(rec.getUri(), mh2); validateMetadata(mh2); - - page = docMgr.read(docId[5]); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh2); - validateMetadata(mh2); - //Overwriting the existing properties - mh2 = setMetadata(); - writeset =docMgr.newWriteSet(); - writeset.add(docId[3], mh2,new DOMHandle(getDocumentContent("This is so foo4"))); - docMgr.write(writeset); - - page = docMgr.read(docId[3]); - mh2=new DocumentMetadataHandle(); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh2); - validateMetadata(mh2); - - } + + page = docMgr.read(docId[5]); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh2); + validateMetadata(mh2); + //Overwriting the existing properties + mh2 = setMetadata(); + writeset =docMgr.newWriteSet(); + writeset.add(docId[3], mh2,new DOMHandle(getDocumentContent("This is so foo4"))); + docMgr.write(writeset); + + page = docMgr.read(docId[3]); + mh2=new DocumentMetadataHandle(); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh2); + validateMetadata(mh2); + } + @Test public void testWriteMultipleBinaryDocWithDefaultMetadata2() throws Exception - { - String docId[] = {"Pandakarlino.jpg","mlfavicon.png"}; - - BinaryDocumentManager docMgr = client.newBinaryDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - // put metadata - DocumentMetadataHandle mh = setMetadata(); - - File file1= null,file2=null; - file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); - FileHandle handle1 = new FileHandle(file1); - writeset.add("/1/"+docId[0],handle1.withFormat(Format.BINARY)); - - writeset.add("/2/"+docId[0],new DocumentMetadataHandle().withQuality(5),handle1.withFormat(Format.BINARY)); - - file2 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[1]); - FileHandle handle2 = new FileHandle(file2); - writeset.add("/1/"+docId[1],new DocumentMetadataHandle().withCollections("collection1"),handle2.withFormat(Format.BINARY)); - - writeset.addDefault(mh); - - writeset.add("/2/"+docId[1],handle2.withFormat(Format.BINARY)); - docMgr.write(writeset); - - - DocumentPage page = docMgr.read("/1/Pandakarlino.jpg"); - DocumentRecord rec; - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh); -// System.out.println(rec.getUri()+mh.getQuality()); - assertEquals("default quality",0,mh.getQuality()); - validateDefaultMetadata(mh); - page = docMgr.read("/2/Pandakarlino.jpg"); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh); - assertEquals(" quality",5,mh.getQuality()); - - assertTrue("default collections reset",mh.getCollections().isEmpty()); - - page = docMgr.read("/1/mlfavicon.png"); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh); - assertEquals("default quality",0,mh.getQuality()); -// System.out.println(rec.getUri()+mh.getCollections().isEmpty()); - assertFalse("default collections reset",mh.getCollections().isEmpty()); - - page = docMgr.read("/2/mlfavicon.png"); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh); - validateMetadata(mh); -} + { + String docId[] = {"Pandakarlino.jpg","mlfavicon.png"}; + + BinaryDocumentManager docMgr = client.newBinaryDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + // put metadata + DocumentMetadataHandle mh = setMetadata(); + + File file1= null,file2=null; + file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); + FileHandle handle1 = new FileHandle(file1); + writeset.add("/1/"+docId[0],handle1.withFormat(Format.BINARY)); + + writeset.add("/2/"+docId[0],new DocumentMetadataHandle().withQuality(5),handle1.withFormat(Format.BINARY)); + + file2 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[1]); + FileHandle handle2 = new FileHandle(file2); + writeset.add("/1/"+docId[1],new DocumentMetadataHandle().withCollections("collection1"),handle2.withFormat(Format.BINARY)); + + writeset.addDefault(mh); + + writeset.add("/2/"+docId[1],handle2.withFormat(Format.BINARY)); + docMgr.write(writeset); + + + DocumentPage page = docMgr.read("/1/Pandakarlino.jpg"); + DocumentRecord rec; + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh); + // System.out.println(rec.getUri()+mh.getQuality()); + assertEquals("default quality",0,mh.getQuality()); + validateDefaultMetadata(mh); + page = docMgr.read("/2/Pandakarlino.jpg"); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh); + assertEquals(" quality",5,mh.getQuality()); + + assertTrue("default collections reset",mh.getCollections().isEmpty()); + + page = docMgr.read("/1/mlfavicon.png"); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh); + assertEquals("default quality",0,mh.getQuality()); + // System.out.println(rec.getUri()+mh.getCollections().isEmpty()); + assertFalse("default collections reset",mh.getCollections().isEmpty()); + + page = docMgr.read("/2/mlfavicon.png"); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh); + validateMetadata(mh); + } /* * This is test is made up for the github issue 25 */ -@Test + @Test public void testWriteMultipleJSONDocsWithDefaultMetadata2() throws Exception - { + { // Synthesize input content - StringHandle doc1 = new StringHandle( - "{\"number\": 1}").withFormat(Format.JSON); - StringHandle doc2 = new StringHandle( - "{\"number\": 2}").withFormat(Format.JSON); - StringHandle doc3 = new StringHandle( - "{\"number\": 3}").withFormat(Format.JSON); - StringHandle doc4 = new StringHandle( - "{\"number\": 4}").withFormat(Format.JSON); - StringHandle doc5 = new StringHandle( - "{\"number\": 5}").withFormat(Format.JSON); - - // Synthesize input metadata - DocumentMetadataHandle defaultMetadata1 = - new DocumentMetadataHandle().withQuality(1); - DocumentMetadataHandle defaultMetadata2 = - new DocumentMetadataHandle().withQuality(2); - DocumentMetadataHandle docSpecificMetadata = - new DocumentMetadataHandle().withCollections("mySpecificCollection"); - - // Create and build up the batch - JSONDocumentManager jdm = client.newJSONDocumentManager(); - DocumentWriteSet batch = jdm.newWriteSet(); - - // use system default metadata - batch.add("doc1.json", doc1); // system default metadata - - // using batch default metadata - batch.addDefault(defaultMetadata1); - batch.add("doc2.json", doc2); // batch default metadata - batch.add("doc3.json", docSpecificMetadata, doc3); - batch.add("doc4.json", doc4); // batch default metadata - - // replace batch default metadata with new metadata - batch.addDefault(defaultMetadata2); - batch.add("doc5.json", doc5); // batch default - - // Execute the write operation - jdm.write(batch); - DocumentPage page ; - DocumentRecord rec; - // Check the results - // Doc1 should have the system default quality of 0 - page = jdm.read("doc1.json"); - DocumentMetadataHandle mh = new DocumentMetadataHandle(); - rec = page.next(); - jdm.readMetadata(rec.getUri(), mh); - validateDefaultMetadata(mh); - assertEquals("default quality",0,mh.getQuality()); - - // Doc2 should use the first batch default metadata, with quality 1 - page = jdm.read("doc2.json"); - rec = page.next(); - jdm.readMetadata(rec.getUri(), mh); - System.out.print(mh.getCollections().isEmpty()); - assertEquals("default quality",1,mh.getQuality()); - assertTrue("default collections reset",mh.getCollections().isEmpty()); - - // Doc3 should have the system default document quality (0) because quality - // was not included in the document-specific metadata. It should be in the - // collection "mySpecificCollection", from the document-specific metadata. - - page = jdm.read("doc3.json"); - rec = page.next(); - jdm.readMetadata(rec.getUri(), mh); - assertEquals("default quality",0,mh.getQuality()); - assertEquals("default collection must change","[mySpecificCollection]",mh.getCollections().toString()); - - DocumentMetadataHandle doc3Metadata = - jdm.readMetadata("doc3.json", new DocumentMetadataHandle()); - System.out.println("doc3 quality: Expected=0, Actual=" + doc3Metadata.getPermissions()); - System.out.print("doc3 collections: Expected: myCollection, Actual="); - for (String collection : doc3Metadata.getCollections()) { - System.out.print(collection + " "); - } - System.out.println(); - - // Doc 4 should also use the 1st batch default metadata, with quality 1 - page = jdm.read("doc4.json"); - rec = page.next(); - jdm.readMetadata(rec.getUri(), mh); - assertEquals("default quality",1,mh.getQuality()); - assertTrue("default collections reset",mh.getCollections().isEmpty()); - // Doc5 should use the 2nd batch default metadata, with quality 2 - page = jdm.read("doc5.json"); - rec = page.next(); - jdm.readMetadata(rec.getUri(), mh); - assertEquals("default quality",2,mh.getQuality()); - - } + StringHandle doc1 = new StringHandle( + "{\"number\": 1}").withFormat(Format.JSON); + StringHandle doc2 = new StringHandle( + "{\"number\": 2}").withFormat(Format.JSON); + StringHandle doc3 = new StringHandle( + "{\"number\": 3}").withFormat(Format.JSON); + StringHandle doc4 = new StringHandle( + "{\"number\": 4}").withFormat(Format.JSON); + StringHandle doc5 = new StringHandle( + "{\"number\": 5}").withFormat(Format.JSON); + + // Synthesize input metadata + DocumentMetadataHandle defaultMetadata1 = + new DocumentMetadataHandle().withQuality(1); + DocumentMetadataHandle defaultMetadata2 = + new DocumentMetadataHandle().withQuality(2); + DocumentMetadataHandle docSpecificMetadata = + new DocumentMetadataHandle().withCollections("mySpecificCollection"); + + // Create and build up the batch + JSONDocumentManager jdm = client.newJSONDocumentManager(); + DocumentWriteSet batch = jdm.newWriteSet(); + + // use system default metadata + batch.add("doc1.json", doc1); // system default metadata + + // using batch default metadata + batch.addDefault(defaultMetadata1); + batch.add("doc2.json", doc2); // batch default metadata + batch.add("doc3.json", docSpecificMetadata, doc3); + batch.add("doc4.json", doc4); // batch default metadata + + // replace batch default metadata with new metadata + batch.addDefault(defaultMetadata2); + batch.add("doc5.json", doc5); // batch default + + // Execute the write operation + jdm.write(batch); + DocumentPage page ; + DocumentRecord rec; + // Check the results + // Doc1 should have the system default quality of 0 + page = jdm.read("doc1.json"); + DocumentMetadataHandle mh = new DocumentMetadataHandle(); + rec = page.next(); + jdm.readMetadata(rec.getUri(), mh); + validateDefaultMetadata(mh); + assertEquals("default quality",0,mh.getQuality()); + + // Doc2 should use the first batch default metadata, with quality 1 + page = jdm.read("doc2.json"); + rec = page.next(); + jdm.readMetadata(rec.getUri(), mh); + System.out.print(mh.getCollections().isEmpty()); + assertEquals("default quality",1,mh.getQuality()); + assertTrue("default collections reset",mh.getCollections().isEmpty()); + + // Doc3 should have the system default document quality (0) because quality + // was not included in the document-specific metadata. It should be in the + // collection "mySpecificCollection", from the document-specific metadata. + + page = jdm.read("doc3.json"); + rec = page.next(); + jdm.readMetadata(rec.getUri(), mh); + assertEquals("default quality",0,mh.getQuality()); + assertEquals("default collection must change","[mySpecificCollection]",mh.getCollections().toString()); + + DocumentMetadataHandle doc3Metadata = + jdm.readMetadata("doc3.json", new DocumentMetadataHandle()); + System.out.println("doc3 quality: Expected=0, Actual=" + doc3Metadata.getPermissions()); + System.out.print("doc3 collections: Expected: myCollection, Actual="); + for (String collection : doc3Metadata.getCollections()) { + System.out.print(collection + " "); + } + System.out.println(); + + // Doc 4 should also use the 1st batch default metadata, with quality 1 + page = jdm.read("doc4.json"); + rec = page.next(); + jdm.readMetadata(rec.getUri(), mh); + assertEquals("default quality",1,mh.getQuality()); + assertTrue("default collections reset",mh.getCollections().isEmpty()); + // Doc5 should use the 2nd batch default metadata, with quality 2 + page = jdm.read("doc5.json"); + rec = page.next(); + jdm.readMetadata(rec.getUri(), mh); + assertEquals("default quality",2,mh.getQuality()); + + } + @Test public void testWriteGenericDocMgrWithDefaultMetadata() throws Exception - { - String docId[] = {"Sega-4MB.jpg"}; - + { + String docId[] = {"Sega-4MB.jpg"}; + GenericDocumentManager docMgr = client.newDocumentManager(); DocumentWriteSet writeset =docMgr.newWriteSet(); - // put metadata - DocumentMetadataHandle mh = setMetadata(); - - writeset.addDefault(mh); // Adding default metadata to the entire batch - File file1= null; - file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); - FileInputStream fis = new FileInputStream(file1); - InputStreamHandle handle1 = new InputStreamHandle(fis); - handle1.setFormat(Format.BINARY); - - writeset.add("/generic/Pandakarlino.jpg",handle1); // This document implicitly gets the default metadata that we added in the begining - - 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 DocumentMetadataHandle().withQuality(10),jh); //This document suppose to get the in scope metadata quality and should set collections to system default - - String foo1 = "This is foo1 of byte Array"; - byte[] ba = foo1.getBytes(); - BytesHandle bh = new BytesHandle(ba); - bh.setFormat(Format.TEXT); - - writeset.add("/generic/foo1.txt",bh); - - DOMSource ds = new DOMSource(getDocumentContent("This is so foo1")); - SourceHandle sh = new SourceHandle(); - sh.set(ds); - sh.setFormat(Format.XML); - DocumentMetadataHandle mh2 = new DocumentMetadataHandle(); - writeset.add("/generic/foo.xml", mh2.withCollections("genericCollection"), sh); //This document should over write the system default and default collection list and get document specific collection - - docMgr.write(writeset); - - DocumentPage page = docMgr.read("/generic/Pandakarlino.jpg","/generic/foo1.txt"); - - while(page.hasNext()){ - DocumentRecord rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh); - System.out.println(rec.getUri()); - validateMetadata(mh); - } - validateMetadata(mh); - page = docMgr.read("/generic/dog.json"); - DocumentRecord rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh); - assertEquals("default quality",10,mh.getQuality()); - assertTrue("default collections missing",mh.getCollections().isEmpty()); - - page = docMgr.read("/generic/foo.xml"); - rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh); - assertEquals("default quality",0,mh.getQuality()); - assertEquals("default collection must change","[genericCollection]",mh.getCollections().toString()); - - } + // put metadata + DocumentMetadataHandle mh = setMetadata(); + + writeset.addDefault(mh); // Adding default metadata to the entire batch + File file1= null; + file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); + FileInputStream fis = new FileInputStream(file1); + InputStreamHandle handle1 = new InputStreamHandle(fis); + handle1.setFormat(Format.BINARY); + + writeset.add("/generic/Pandakarlino.jpg",handle1); // This document implicitly gets the default metadata that we added in the begining + + 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 DocumentMetadataHandle().withQuality(10),jh); //This document suppose to get the in scope metadata quality and should set collections to system default + String foo1 = "This is foo1 of byte Array"; + byte[] ba = foo1.getBytes(); + BytesHandle bh = new BytesHandle(ba); + bh.setFormat(Format.TEXT); + + writeset.add("/generic/foo1.txt",bh); + + DOMSource ds = new DOMSource(getDocumentContent("This is so foo1")); + SourceHandle sh = new SourceHandle(); + sh.set(ds); + sh.setFormat(Format.XML); + DocumentMetadataHandle mh2 = new DocumentMetadataHandle(); + writeset.add("/generic/foo.xml", mh2.withCollections("genericCollection"), sh); //This document should over write the system default and default collection list and get document specific collection + + docMgr.write(writeset); + + DocumentPage page = docMgr.read("/generic/Pandakarlino.jpg","/generic/foo1.txt"); + + while(page.hasNext()){ + DocumentRecord rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh); + System.out.println(rec.getUri()); + validateMetadata(mh); + } + validateMetadata(mh); + page = docMgr.read("/generic/dog.json"); + DocumentRecord rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh); + assertEquals("default quality",10,mh.getQuality()); + assertTrue("default collections missing",mh.getCollections().isEmpty()); + + page = docMgr.read("/generic/foo.xml"); + rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh); + assertEquals("default quality",0,mh.getQuality()); + assertEquals("default collection must change","[genericCollection]",mh.getCollections().toString()); + } } From f04df6f53193a8e852f07ede5d7a21fe22da9d34 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Fri, 22 Aug 2014 17:43:00 -0700 Subject: [PATCH 085/357] Using JSONAssert and updated compielr deps for that --- test-complete/build.gradle | 1 + .../TestQueryOptionBuilderSortOrder.java | 66 ++----------------- 2 files changed, 8 insertions(+), 59 deletions(-) diff --git a/test-complete/build.gradle b/test-complete/build.gradle index 0b968210d..541de1726 100644 --- a/test-complete/build.gradle +++ b/test-complete/build.gradle @@ -21,5 +21,6 @@ dependencies { compile('com.marklogic:client-api-java:3.0-SNAPSHOT') compile('xmlunit:xmlunit:1.5') compile('junit:junit:4.11') + compile('org.skyscreamer:jsonassert:1.2.3') } diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderSortOrder.java b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderSortOrder.java index ea11403d1..46452836e 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderSortOrder.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderSortOrder.java @@ -30,6 +30,8 @@ import com.marklogic.client.io.QueryOptionsHandle; import com.marklogic.client.io.StringHandle; +import org.skyscreamer.jsonassert.*; +import org.json.JSONException; import org.junit.*; public class TestQueryOptionBuilderSortOrder extends BasicJavaClientREST { @@ -121,7 +123,7 @@ public void testSortOrderDescendingScore() throws FileNotFoundException, XpathEx } @Test - public void testSortOrderPrimaryDescScoreSecondaryAscDate() throws FileNotFoundException, XpathException, TransformerException, IOException + public void testSortOrderPrimaryDescScoreSecondaryAscDate() throws FileNotFoundException, XpathException, TransformerException, IOException, JSONException { System.out.println("Running testSortOrderPrimaryDescScoreSecondaryAscDate"); @@ -160,64 +162,10 @@ public void testSortOrderPrimaryDescScoreSecondaryAscDate() throws FileNotFoundE optionsMgr.readOptions("SortOrderPrimaryDescScoreSecondaryAscDate", readHandle); String output = readHandle.get(); System.out.println(output + "testSortOrderPrimaryDescScoreSecondaryAscDate"); - - // ----------- Validate options node ---------------------- - ObjectMapper mapper = new ObjectMapper(); - JsonNode optionsContent = mapper.readTree(output); - assertNotNull(optionsContent); - System.out.println("JSON output: " + optionsContent); - - JsonNode optionsNode = optionsContent.get("options"); - assertNotNull(optionsNode); - - JsonNode metrics = optionsNode.get("return-metrics"); - assertNotNull(metrics); - assertEquals(metrics.booleanValue(), false); - - JsonNode qtext = optionsNode.get("return-qtext"); - assertNotNull(qtext); - assertEquals(qtext.booleanValue(), false); - - JsonNode sortOrders = optionsNode.get("sort-order"); - assertNotNull(sortOrders); - System.out.println(optionsContent.get("options").get("sort-order")); - - assertTrue(sortOrders.isArray()); - for (final JsonNode sortOrder : sortOrders) { - assertNotNull(sortOrder.get("direction")); - String direction = sortOrder.get("direction").textValue(); - - if (direction.equals("descending")) { - assertTrue(sortOrder.has("score")); - assertTrue(sortOrder.get("score").isNull()); - } - else if (direction.equals("ascending")) { - assertTrue(sortOrder.has("type")); - assertEquals(sortOrder.get("type").textValue(), "xs:date"); - - JsonNode element = sortOrder.get("element"); - assertNotNull(element); - - JsonNode name = element.get("name"); - assertNotNull(name); - assertEquals(name.textValue(), "date"); - - JsonNode ns = element.get("ns"); - assertNotNull(ns); - assertEquals(ns.textValue(), "http://purl.org/dc/elements/1.1/"); - } - else { - assertTrue("Found an unexpected object", false); - } - } - - JsonNode transformResults = optionsNode.get("transform-results"); - assertNotNull(transformResults); - - JsonNode apply = transformResults.get("apply"); - assertNotNull(apply); - assertEquals(apply.textValue(), "raw"); - + String expected = "{\"options\":{\"return-metrics\":false, \"return-qtext\":false, \"sort-order\":[{\"direction\":\"descending\", \"score\":null}, {\"direction\":\"ascending\", \"type\":\"xs:date\", \"element\":{\"name\":\"date\", \"ns\":\"http://purl.org/dc/elements/1.1/\"}}], \"transform-results\":{\"apply\":\"raw\"}}}"; + + JSONAssert.assertEquals(expected, output, false); + // ----------- Validate search using options node created ---------------------- // create query manager QueryManager queryMgr = client.newQueryManager(); From fdd66eed08eac84956a6c6eb76a300897b0a4f00 Mon Sep 17 00:00:00 2001 From: sammefford Date: Mon, 25 Aug 2014 11:27:56 -0600 Subject: [PATCH 086/357] test more variable types --- .../com/marklogic/client/impl/XdbcEval.java | 71 +++++++++++++++++-- 1 file changed, 67 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/marklogic/client/impl/XdbcEval.java b/src/main/java/com/marklogic/client/impl/XdbcEval.java index ea8c9cca0..837067fbc 100644 --- a/src/main/java/com/marklogic/client/impl/XdbcEval.java +++ b/src/main/java/com/marklogic/client/impl/XdbcEval.java @@ -19,13 +19,76 @@ public static void main(String[] args) { client.register(feature); // set up multipart response parsing client.register(MultiPartFeature.class); + // let's see what types we're getting + String xquery = + "declare variable $myvar1 external;" + + "declare variable $myvar2 external;" + + "declare variable $myvar3 external;" + + "declare variable $myvar4 external;" + + "declare variable $myvar5 external;" + + "declare variable $myvar6 external;" + + "declare function local:getType($var) {" + + "typeswitch($var) " + + "case attribute() return 'attribute()'" + + "case comment() return 'comment()'" + + //"case document() return 'document()'" + + "case element() return 'element()'" + + "case node() return 'node()'" + + //"case processingInstruction() return 'processingInstruction()'" + + "case xs:base64Binary return 'xs:base64Binary'" + + "case xs:boolean return 'xs:boolean'" + + "case xs:byte return 'xs:byte'" + + "case xs:date return 'xs:date'" + + "case xs:dateTime return 'xs:dateTime'" + + "case xs:dayTimeDuration return 'xs:dayTimeDuration'" + + "case xs:short return 'xs:short'" + + "case xs:integer return 'xs:integer'" + + "case xs:int return 'xs:int'" + + "case xs:long return 'xs:long'" + + "case xs:decimal return 'xs:decimal'" + + "case xs:float return 'xs:float'" + + "case xs:double return 'xs:double'" + + "case xs:duration return 'xs:duration'" + + "case xs:unsignedInt return 'xs:unsignedInt'" + + "case xs:unsignedLong return 'xs:unsignedLong'" + + "case xs:unsignedShort return 'xs:unsignedShort'" + + "case xs:string return 'xs:string'" + + "case xs:time return 'xs:time'" + + "case xs:yearMonthDuration return 'xs:yearMonthDuration'" + + "case xs:anyAtomicType return 'xs:anyAtomicType'" + + "case xs:anySimpleType return 'xs:anySimpleType'" + + "case item() return 'item()'" + + "default return 'unknown'" + + "};" + + "for $var at $i in ($myvar1, $myvar2, $myvar3, $myvar4, $myvar5, $myvar6) " + + "return 'type' || $i || ': ' || local:getType($var)"; // set up x-www-form-urlencoded request params Form params = new Form() - .param("xquery", "declare variable $myvar external;\"Hello+World: \"||$myvar") + .param("xquery", xquery) .param("evn0", "") - .param("evl0", "myvar") - .param("evt0", "xs:string") - .param("evv0", "monsters") + .param("evl0", "myvar1") + .param("evt0", "xs:int") + .param("evv0", "1") + .param("evn1", "") + .param("evl1", "myvar2") + .param("evt1", "xs:float") + .param("evv1", "1.1") + .param("evn2", "") + .param("evl2", "myvar3") + .param("evt2", "xs:decimal") + .param("evv2", "1.1") + .param("evn3", "") + .param("evl3", "myvar4") + .param("evt3", "xs:double") + .param("evv3", "1.1") + .param("evn4", "") + .param("evl4", "myvar5") + .param("evt4", "xs:long") + .param("evv4", "999999999999999999") + .param("evn5", "") + .param("evl5", "myvar6") + .param("evt5", "xs:string") + .param("evv5", "test") .param("locale", "en_US") .param("tzoffset", "-21600") .param("dbname", "java-unittest"); From a5118cfcf80d8895816cb6e927e74782e2b22d4f Mon Sep 17 00:00:00 2001 From: Charles Greer Date: Fri, 22 Aug 2014 15:19:54 -0700 Subject: [PATCH 087/357] issue #89 close input streams --- .../client/extra/dom4j/DOM4JHandle.java | 7 +++++ .../client/extra/gson/GSONHandle.java | 8 +++++ .../client/extra/jdom/JDOMHandle.java | 6 ++++ .../marklogic/client/extra/xom/XOMHandle.java | 6 ++++ .../com/marklogic/client/io/DOMHandle.java | 11 ++++--- .../com/marklogic/client/io/JAXBHandle.java | 6 ++++ .../client/io/JacksonDatabindHandle.java | 9 ++++-- .../marklogic/client/io/JacksonHandle.java | 6 ++++ .../client/io/QueryOptionsHandle.java | 8 ++++- .../com/marklogic/client/io/TuplesHandle.java | 19 ++++++++---- .../com/marklogic/client/io/ValuesHandle.java | 19 ++++++++---- .../marklogic/client/io/ValuesListHandle.java | 12 ++++++-- .../client/test/JacksonHandleTest.java | 30 +++++++++++++++++++ 13 files changed, 126 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/marklogic/client/extra/dom4j/DOM4JHandle.java b/src/main/java/com/marklogic/client/extra/dom4j/DOM4JHandle.java index fda7fea47..1a1781c2e 100644 --- a/src/main/java/com/marklogic/client/extra/dom4j/DOM4JHandle.java +++ b/src/main/java/com/marklogic/client/extra/dom4j/DOM4JHandle.java @@ -222,7 +222,14 @@ protected void receiveContent(InputStream content) { throw new MarkLogicIOException(e); } catch (DocumentException e) { throw new MarkLogicIOException(e); + } finally { + try { + content.close(); + } catch (IOException e) { + // ignore. + } } + } @Override diff --git a/src/main/java/com/marklogic/client/extra/gson/GSONHandle.java b/src/main/java/com/marklogic/client/extra/gson/GSONHandle.java index 8ef18fc02..03843e535 100644 --- a/src/main/java/com/marklogic/client/extra/gson/GSONHandle.java +++ b/src/main/java/com/marklogic/client/extra/gson/GSONHandle.java @@ -16,6 +16,7 @@ package com.marklogic.client.extra.gson; import java.io.ByteArrayInputStream; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; @@ -182,8 +183,15 @@ protected void receiveContent(InputStream content) { throw new MarkLogicIOException(e); } catch (UnsupportedEncodingException e) { throw new MarkLogicIOException(e); + } finally { + try { + content.close(); + } catch (IOException e) { + // ignore. + } } + } @Override protected String sendContent() { diff --git a/src/main/java/com/marklogic/client/extra/jdom/JDOMHandle.java b/src/main/java/com/marklogic/client/extra/jdom/JDOMHandle.java index 2da541300..8e994902c 100644 --- a/src/main/java/com/marklogic/client/extra/jdom/JDOMHandle.java +++ b/src/main/java/com/marklogic/client/extra/jdom/JDOMHandle.java @@ -223,6 +223,12 @@ protected void receiveContent(InputStream content) { throw new MarkLogicIOException(e); } catch (IOException e) { throw new MarkLogicIOException(e); + } finally { + try { + content.close(); + } catch (IOException e) { + // ignore. + } } } diff --git a/src/main/java/com/marklogic/client/extra/xom/XOMHandle.java b/src/main/java/com/marklogic/client/extra/xom/XOMHandle.java index 8bdfe624d..0ea606e9d 100644 --- a/src/main/java/com/marklogic/client/extra/xom/XOMHandle.java +++ b/src/main/java/com/marklogic/client/extra/xom/XOMHandle.java @@ -203,6 +203,12 @@ protected void receiveContent(InputStream content) { throw new MarkLogicIOException(e); } catch (IOException e) { throw new MarkLogicIOException(e); + } finally { + try { + content.close(); + } catch (IOException e) { + // ignore. + } } } diff --git a/src/main/java/com/marklogic/client/io/DOMHandle.java b/src/main/java/com/marklogic/client/io/DOMHandle.java index 61b794da1..65075ea72 100644 --- a/src/main/java/com/marklogic/client/io/DOMHandle.java +++ b/src/main/java/com/marklogic/client/io/DOMHandle.java @@ -389,13 +389,16 @@ protected void receiveContent(InputStream content) { domInput.setByteStream(content); this.content = parser.parse(domInput); - content.close(); - } catch (IOException e) { - logger.error("Failed to parse DOM document from input stream",e); - throw new MarkLogicInternalException(e); } catch (ParserConfigurationException e) { logger.error("Failed to parse DOM document from input stream",e); throw new MarkLogicInternalException(e); + } finally { + try { + content.close(); + } catch (IOException e) { + //ignore + } + } } @Override diff --git a/src/main/java/com/marklogic/client/io/JAXBHandle.java b/src/main/java/com/marklogic/client/io/JAXBHandle.java index 23be0f9ba..53372e5fa 100644 --- a/src/main/java/com/marklogic/client/io/JAXBHandle.java +++ b/src/main/java/com/marklogic/client/io/JAXBHandle.java @@ -278,6 +278,12 @@ protected void receiveContent(InputStream content) { } catch (UnsupportedEncodingException e) { logger.error("Failed to unmarshall object read from database document",e); throw new MarkLogicIOException(e); + } finally { + try { + content.close(); + } catch (IOException e) { + // ignore. + } } } @Override diff --git a/src/main/java/com/marklogic/client/io/JacksonDatabindHandle.java b/src/main/java/com/marklogic/client/io/JacksonDatabindHandle.java index 4f0dd2791..e245c2ae7 100644 --- a/src/main/java/com/marklogic/client/io/JacksonDatabindHandle.java +++ b/src/main/java/com/marklogic/client/io/JacksonDatabindHandle.java @@ -138,8 +138,13 @@ protected void receiveContent(InputStream content) { throw new MarkLogicIOException(e); } catch (IOException e) { throw new MarkLogicIOException(e); - } - + } finally { + try { + content.close(); + } catch (IOException e) { + // ignore. + } + } } @Override protected boolean hasContent() { diff --git a/src/main/java/com/marklogic/client/io/JacksonHandle.java b/src/main/java/com/marklogic/client/io/JacksonHandle.java index 866479a10..502ff7c34 100644 --- a/src/main/java/com/marklogic/client/io/JacksonHandle.java +++ b/src/main/java/com/marklogic/client/io/JacksonHandle.java @@ -134,6 +134,12 @@ protected void receiveContent(InputStream content) { throw new MarkLogicIOException(e); } catch (IOException e) { throw new MarkLogicIOException(e); + } finally { + try { + content.close(); + } catch (IOException e) { + // ignore. + } } } diff --git a/src/main/java/com/marklogic/client/io/QueryOptionsHandle.java b/src/main/java/com/marklogic/client/io/QueryOptionsHandle.java index e78c2c76f..75c491aed 100644 --- a/src/main/java/com/marklogic/client/io/QueryOptionsHandle.java +++ b/src/main/java/com/marklogic/client/io/QueryOptionsHandle.java @@ -1038,7 +1038,13 @@ protected void receiveContent(InputStream content) { } catch (UnsupportedEncodingException e) { // This can't actually happen...stupid checked exceptions throw new MarkLogicBindingException(e); - } + } finally { + try { + content.close(); + } catch (IOException e) { + // ignore. + } + } } protected OutputStreamSender sendContent() { diff --git a/src/main/java/com/marklogic/client/io/TuplesHandle.java b/src/main/java/com/marklogic/client/io/TuplesHandle.java index 4040ed8b3..996ecaf50 100644 --- a/src/main/java/com/marklogic/client/io/TuplesHandle.java +++ b/src/main/java/com/marklogic/client/io/TuplesHandle.java @@ -15,6 +15,7 @@ */ package com.marklogic.client.io; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; @@ -24,19 +25,19 @@ import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; -import com.marklogic.client.query.AggregateResult; -import com.marklogic.client.query.ValuesMetrics; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.marklogic.client.MarkLogicBindingException; import com.marklogic.client.MarkLogicIOException; -import com.marklogic.client.query.Tuple; import com.marklogic.client.impl.TuplesBuilder; -import com.marklogic.client.query.TuplesResults; -import com.marklogic.client.query.ValuesDefinition; import com.marklogic.client.io.marker.OperationNotSupported; import com.marklogic.client.io.marker.TuplesReadHandle; +import com.marklogic.client.query.AggregateResult; +import com.marklogic.client.query.Tuple; +import com.marklogic.client.query.TuplesResults; +import com.marklogic.client.query.ValuesDefinition; +import com.marklogic.client.query.ValuesMetrics; /** * A TuplesHandle represents a set of tuples returned by a query on the server. @@ -109,7 +110,13 @@ protected void receiveContent(InputStream content) { } catch (JAXBException e) { logger.error("Failed to unmarshall tuples",e); throw new MarkLogicIOException(e); - } + } finally { + try { + content.close(); + } catch (IOException e) { + // ignore. + } + } } /** diff --git a/src/main/java/com/marklogic/client/io/ValuesHandle.java b/src/main/java/com/marklogic/client/io/ValuesHandle.java index ee026ff54..0638dc9aa 100644 --- a/src/main/java/com/marklogic/client/io/ValuesHandle.java +++ b/src/main/java/com/marklogic/client/io/ValuesHandle.java @@ -15,6 +15,7 @@ */ package com.marklogic.client.io; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; @@ -24,19 +25,19 @@ import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; -import com.marklogic.client.query.AggregateResult; -import com.marklogic.client.query.ValuesMetrics; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.marklogic.client.MarkLogicBindingException; import com.marklogic.client.MarkLogicIOException; -import com.marklogic.client.query.CountedDistinctValue; import com.marklogic.client.impl.ValuesBuilder; -import com.marklogic.client.query.ValuesDefinition; -import com.marklogic.client.query.ValuesResults; import com.marklogic.client.io.marker.OperationNotSupported; import com.marklogic.client.io.marker.ValuesReadHandle; +import com.marklogic.client.query.AggregateResult; +import com.marklogic.client.query.CountedDistinctValue; +import com.marklogic.client.query.ValuesDefinition; +import com.marklogic.client.query.ValuesMetrics; +import com.marklogic.client.query.ValuesResults; /** * A ValuesHandle represents a list of values or of tuples @@ -114,7 +115,13 @@ protected void receiveContent(InputStream content) { } catch (JAXBException e) { logger.error("Failed to unmarshall values",e); throw new MarkLogicIOException(e); - } + } finally { + try { + content.close(); + } catch (IOException e) { + // ignore. + } + } } /** diff --git a/src/main/java/com/marklogic/client/io/ValuesListHandle.java b/src/main/java/com/marklogic/client/io/ValuesListHandle.java index f0048af71..6c202be7e 100644 --- a/src/main/java/com/marklogic/client/io/ValuesListHandle.java +++ b/src/main/java/com/marklogic/client/io/ValuesListHandle.java @@ -15,6 +15,7 @@ */ package com.marklogic.client.io; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; @@ -30,9 +31,9 @@ import com.marklogic.client.MarkLogicBindingException; import com.marklogic.client.MarkLogicIOException; import com.marklogic.client.impl.ValuesListBuilder; -import com.marklogic.client.query.ValuesListResults; import com.marklogic.client.io.marker.OperationNotSupported; import com.marklogic.client.io.marker.ValuesListReadHandle; +import com.marklogic.client.query.ValuesListResults; /** * A ValuesListHandle represents a list of available named lexicon configurations @@ -127,7 +128,14 @@ protected void receiveContent(InputStream content) { } catch (JAXBException e) { logger.error("Failed to unmarshall values list",e); throw new MarkLogicIOException(e); - } + } finally { + try { + content.close(); + } catch (IOException e) { + // ignore. + } + } + } /** diff --git a/src/test/java/com/marklogic/client/test/JacksonHandleTest.java b/src/test/java/com/marklogic/client/test/JacksonHandleTest.java index 3402b7fdb..8d492cc9d 100644 --- a/src/test/java/com/marklogic/client/test/JacksonHandleTest.java +++ b/src/test/java/com/marklogic/client/test/JacksonHandleTest.java @@ -15,9 +15,13 @@ */ package com.marklogic.client.test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import java.io.IOException; + import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; @@ -28,6 +32,12 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import com.marklogic.client.document.JSONDocumentManager; import com.marklogic.client.io.JacksonHandle; +import com.marklogic.client.io.SearchHandle; +import com.marklogic.client.query.MatchDocumentSummary; +import com.marklogic.client.query.MatchLocation; +import com.marklogic.client.query.QueryDefinition; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.StructuredQueryBuilder; import com.marklogic.client.test.Common; public class JacksonHandleTest { @@ -80,4 +90,24 @@ public void testReadWrite() { // delete the document docMgr.delete(docId); } + + @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. + } + } + } + } + From 1aa5434b919b8f097a93671f05e10593ad35e86c Mon Sep 17 00:00:00 2001 From: sammefford Date: Thu, 28 Aug 2014 15:18:35 -0600 Subject: [PATCH 088/357] add some javadocs --- .../com/marklogic/client/pojo/PojoRepository.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/java/com/marklogic/client/pojo/PojoRepository.java b/src/main/java/com/marklogic/client/pojo/PojoRepository.java index a3548dbd6..e5f82f37e 100644 --- a/src/main/java/com/marklogic/client/pojo/PojoRepository.java +++ b/src/main/java/com/marklogic/client/pojo/PojoRepository.java @@ -28,14 +28,26 @@ public interface PojoRepository { public void write(T entity, Transaction transaction); public void write(T entity, Transaction transaction, String... collections); + /** @return true if a document exists in the database with the id */ public boolean exists(ID id); + /** @return the number of documents of type T persisted in the database */ public long count(); + + /** @return the number of documents of type T persisted in the database with at least + * one of the criteria collections*/ public long count(String... collection); + + /** @return the number of documents of type T persisted in the database which match + * the query */ public long count(QueryDefinition query); + /** Deletes from the database the documents with the corresponding ids */ public void delete(ID... ids); + + /** Deletes from the database all documents of type T persisted by the pojo facade */ public void deleteAll(); + /* REST API does not currently support DELETE /search with multiple collection arguments public void deleteAll(String... collections); */ From 492cf596139240ee711b186de5e7be64f17f04fc Mon Sep 17 00:00:00 2001 From: sammefford Date: Fri, 29 Aug 2014 11:37:22 -0600 Subject: [PATCH 089/357] add javadocs for containerQuery methods --- .../marklogic/client/pojo/PojoQueryBuilder.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java b/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java index 7b44372f1..25fbffe56 100644 --- a/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java +++ b/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java @@ -108,10 +108,23 @@ * QueryDefinition query = countriesQb.value("continent", "EU"); } */ public interface PojoQueryBuilder { + + /** @return a query matching pojos of type T containing the pojoField with contents + * or children matching the specified query */ public StructuredQueryDefinition containerQuery(String pojoField, StructuredQueryDefinition query); + + /** @return a query matching pojos of type T with children matching the specified query */ public StructuredQueryDefinition containerQuery(StructuredQueryDefinition query); - public PojoQueryBuilder containerQuery(String pojoField); + + /** Use this method to provide a query builder that can query a nested object within your pojo. + * All other PojoQueryBuilder methods create queries for direct children of T which are native + * types. If a child of T is an object, and you need to query one of its children, this method + * provides you a query builder that is specific to that child object. To query further levels of + * nested objects you may use this method on the each returned PojoQueryBuilder which represents + * one level deeper. + * @return a PojoQueryBuilder for nested pojos of the type corresponding with pojoField */ + public PojoQueryBuilder containerQuery(String pojoField); public StructuredQueryBuilder.GeospatialIndex geoPair(String latitudeFieldName, String longitudeFieldName); /** From 8a4ddabbc61a793539ce87cf55f45be45415adaf Mon Sep 17 00:00:00 2001 From: sravan Date: Thu, 4 Sep 2014 10:32:38 -0700 Subject: [PATCH 090/357] adding a new test for POJO --- .../com/marklogic/javaclient/Artifact.java | 35 +++ .../com/marklogic/javaclient/Company.java | 37 +++ .../javaclient/TestPOJOReadWrite1.java | 260 ++++++++++++++++++ 3 files changed, 332 insertions(+) create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/Artifact.java create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/Company.java create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/TestPOJOReadWrite1.java diff --git a/test-complete/src/test/java/com/marklogic/javaclient/Artifact.java b/test-complete/src/test/java/com/marklogic/javaclient/Artifact.java new file mode 100644 index 000000000..137337e7b --- /dev/null +++ b/test-complete/src/test/java/com/marklogic/javaclient/Artifact.java @@ -0,0 +1,35 @@ +package com.marklogic.javaclient; +import com.marklogic.client.pojo.annotation.Id; +public class Artifact { + @Id + public long id; + private String name; + private Company manufacturer; + // @IndexClassField + private int inventory; + + public long getId() { + return id; + } + public Artifact setId(long id) { + this.id= id; return this; + } + public String getName() { + return name; + } + public Artifact setName(String name) { + this.name = name; return this; + } + public Company getManufacturer() { + return manufacturer; + } + public Artifact setManufacturer(Company manufacturer) { + this.manufacturer= manufacturer; return this; + } + public int getInventory() { + return inventory; + } + public Artifact setInventory(int inventory) { + this.inventory= inventory; return this; + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/Company.java b/test-complete/src/test/java/com/marklogic/javaclient/Company.java new file mode 100644 index 000000000..bc06630d1 --- /dev/null +++ b/test-complete/src/test/java/com/marklogic/javaclient/Company.java @@ -0,0 +1,37 @@ +package com.marklogic.javaclient; + +public class Company { +// @IndexClassField + private String name; + private String website; + // @GeospatialLatitude + private double latitude; +// @GeospatialLongitude + private double longitude; + + public String getName() { + return name; + } + public Company setName(String name) { + this.name = name; return this; + } + public String getWebsite() { + return website; + } + public Company setWebsite(String website) { + this.website = website; return this; + } + public double getLatitude() { + return latitude; + } + public Company setLatitude(double latitude) { + this.latitude = latitude; return this; + } + public double getLongitude() { + return longitude; + } + public Company setLongitude(double longitude) { + this.longitude = longitude; return this; + } + +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOReadWrite1.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOReadWrite1.java new file mode 100644 index 000000000..becda2958 --- /dev/null +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOReadWrite1.java @@ -0,0 +1,260 @@ +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.util.Iterator; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.pojo.PojoPage; +import com.marklogic.client.pojo.PojoRepository; + +public class TestPOJOReadWrite1 extends BasicJavaClientREST { + private static String dbName = "TestPOJORWDB"; + private static String [] fNames = {"TestPOJORWDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client ; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + System.out.println("In setup"); +// setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + System.out.println("In tear down" ); +// tearDownJavaRESTServer(dbName, fNames, restServerName); + } + + @Before + public void setUp() throws Exception { + client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST); + } + + @After + public void tearDown() throws Exception { + // release client + client.release(); + } + + public Artifact getArtifact(int counter){ + Company acme = new Company(); + acme.setName("Acme "+counter+", Inc."); + acme.setWebsite("http://www.acme"+counter+".com"); + acme.setLatitude(41.998+counter); + acme.setLongitude(-87.966+counter); + Artifact cogs = new Artifact(); + cogs.setId(counter); + cogs.setName("Cogs "+counter); + cogs.setManufacturer(acme); + cogs.setInventory(1000+counter); + + return cogs; + } + public void validateArtifact(Artifact art) + { + assertNotNull("Artifact object should never be Null",art); + assertNotNull("Id should never be Null",art.id); + assertTrue("Inventry is always greater than 1000", art.getInventory()>1000); + } + //This test is to persist a simple design model objects in ML, read from ML, delete all + @Test + public void testPOJOWrite() { + PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); + products.deleteAll(); + //Load more than 100 objects + for(int i=1;i<112;i++){ + products.write(this.getArtifact(i)); + } + assertEquals("Total number of object recods",111, products.count()); + for(long i=1;i<112;i++){ + assertTrue("Product id "+i+" does not exist",products.exists(i)); + this.validateArtifact(products.read(i)); + } + products.deleteAll(); + for(long i=1;i<112;i++){ + assertFalse("Product id exists ?",products.exists(i)); + } + + } + //This test is to persist objects into different collections, read documents based on Id and delete single object based on Id + @Test + public void testPOJOWriteWithCollection() { + PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); + //Load more than 110 objects into different collections + products.deleteAll(); + for(int i=112;i<222;i++){ + if(i%2==0){ + products.write(this.getArtifact(i),"even","numbers"); + } + else { + products.write(this.getArtifact(i),"odd","numbers"); + } + } + assertEquals("Total number of object recods",110, products.count("numbers")); + assertEquals("Collection even count",55,products.count("even")); + assertEquals("Collection odd count",55,products.count("odd")); + for(long i=112;i<222;i++){ + // validate all the records inserted are readable + assertTrue("Product id "+i+" does not exist",products.exists(i)); + this.validateArtifact(products.read(i)); + } + products.delete((long)112); + assertFalse("Product id 112 exists ?",products.exists((long)112)); + products.deleteAll(); + //see any document exists + for(long i=112;i<222;i++){ + assertFalse("Product id "+i+" exists ?",products.exists(i)); + } + //see if it complains when there are no records + products.delete((long)112); + products.deleteAll(); + } + //This test is to read objects into pojo page based on Ids + // until #103 is resolved @Test + public void testPOJOWriteWithPojoPage() { + PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); + //Load more than 110 objects into different collections + products.deleteAll(); + Long[] ids= new Long[111]; + int j=0; + for(int i=222;i<333;i++){ + ids[j] =(long) i;j++; + if(i%2==0){ + products.write(this.getArtifact(i),"even","numbers"); + } + else { + products.write(this.getArtifact(i),"odd","numbers"); + } + } + assertEquals("Total number of object recods",111, products.count("numbers")); + assertEquals("Collection even count",56,products.count("even")); + assertEquals("Collection odd count",55,products.count("odd")); + + System.out.println("Default Page length setting on docMgr :"+products.getPageLength()); + assertEquals("Default setting for page length",50,products.getPageLength()); + products.setPageLength(100); +// assertEquals("explicit setting for page length",1,products.getPageLength()); + PojoPage p= products.read(ids); + // test for page methods + //Issue- assertEquals("Number of records",1,p.size()); + System.out.println("Page size"+p.size()); +// assertEquals("Starting record in first page ",1,p.getStart()); + System.out.println("Starting record in first page "+p.getStart()); + +// assertEquals("Total number of estimated results:",111,p.getTotalSize()); + System.out.println("Total number of estimated results:"+p.getTotalSize()); +// assertEquals("Total number of estimated pages :",111,p.getTotalPages()); + System.out.println("Total number of estimated pages :"+p.getTotalPages()); + assertFalse("Is this First page :",p.isFirstPage());//this is bug + assertFalse("Is this Last page :",p.isLastPage()); + assertTrue("Is this First page has content:",p.hasContent()); + // Need the Issue #75 to be fixed + assertTrue("Is first page has previous page ?",p.hasPreviousPage()); + long pageNo=1,count=0; + do{ + count=0; + if(pageNo >1){ + assertFalse("Is this first Page", p.isFirstPage()); + assertTrue("Is page has previous page ?",p.hasPreviousPage()); + } + Iterator itr = p.iterator(); + while(itr.hasNext()){ + this.validateArtifact(p.iterator().next()); + count++; + } +// assertEquals("document count", p.size(),count); + System.out.println("Is this Last page :"+p.hasContent()+p.isLastPage()); + pageNo = pageNo + p.getPageSize(); + }while((p.isLastPage()) && p.hasContent()); + assertTrue("page count is 111 ",pageNo == p.getTotalPages()); + assertTrue("Page has previous page ?",p.hasPreviousPage()); + assertEquals("page size", 1,p.getPageSize()); + assertEquals("document count", 111,p.getTotalSize()); + assertFalse("Page has any records ?",p.hasContent()); + + + products.deleteAll(); + //see any document exists + for(long i=112;i<222;i++){ + assertFalse("Product id "+i+" exists ?",products.exists(i)); + } + //see if it complains when there are no records + } + @Test + public void testPOJOWriteWithPojoPageReadAll() { + PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); + //Load more than 110 objects into different collections + products.deleteAll(); + for(int i=222;i<333;i++){ + if(i%2==0){ + products.write(this.getArtifact(i),"even","numbers"); + } + else { + products.write(this.getArtifact(i),"odd","numbers"); + } + } + assertEquals("Total number of object recods",111, products.count("numbers")); + assertEquals("Collection even count",56,products.count("even")); + assertEquals("Collection odd count",55,products.count("odd")); + + System.out.println("Default Page length setting on docMgr :"+products.getPageLength()); + assertEquals("Default setting for page length",50,products.getPageLength()); + products.setPageLength(1); + assertEquals("explicit setting for page length",1,products.getPageLength()); + PojoPage p= products.readAll(1); + // test for page methods + assertEquals("Number of records",1,p.size()); + System.out.println("Page size"+p.size()); + assertEquals("Starting record in first page ",1,p.getStart()); + System.out.println("Starting record in first page "+p.getStart()); + + assertEquals("Total number of estimated results:",111,p.getTotalSize()); + System.out.println("Total number of estimated results:"+p.getTotalSize()); + assertEquals("Total number of estimated pages :",111,p.getTotalPages()); + System.out.println("Total number of estimated pages :"+p.getTotalPages()); + assertFalse("Is this First page :",p.isFirstPage());//this is bug + assertFalse("Is this Last page :",p.isLastPage()); + assertTrue("Is this First page has content:",p.hasContent()); + // Need the Issue #75 to be fixed + assertTrue("Is first page has previous page ?",p.hasPreviousPage()); + long pageNo=1,count=0; + do{ + count=0; + p= products.readAll(pageNo); + + if(pageNo >1){ + assertFalse("Is this first Page", p.isFirstPage()); + assertTrue("Is page has previous page ?",p.hasPreviousPage()); + } + + while(p.iterator().hasNext()){ + this.validateArtifact(p.iterator().next()); + count++; + } + assertEquals("document count", p.size(),count); + + pageNo = pageNo + p.getPageSize(); + }while(!(p.isLastPage()) && pageNo < p.getTotalSize()); + assertTrue("page count is 111 ",pageNo == p.getTotalPages()); + assertTrue("Page has previous page ?",p.hasPreviousPage()); + assertEquals("page size", 1,p.getPageSize()); + assertEquals("document count", 111,p.getTotalSize()); + + products.deleteAll(); + p= products.readAll(1); + assertFalse("Page has any records ?",p.hasContent()); + + + } +} From 8e13985f5e5f9a9867411906ab924c8c7fca1999 Mon Sep 17 00:00:00 2001 From: sravan Date: Thu, 4 Sep 2014 18:06:01 -0700 Subject: [PATCH 091/357] uncommented the setup and teardown --- .../java/com/marklogic/javaclient/TestPOJOReadWrite1.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOReadWrite1.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOReadWrite1.java index becda2958..e5038692d 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOReadWrite1.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOReadWrite1.java @@ -27,13 +27,13 @@ public class TestPOJOReadWrite1 extends BasicJavaClientREST { public static void setUpBeforeClass() throws Exception { // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); System.out.println("In setup"); -// setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); } @AfterClass public static void tearDownAfterClass() throws Exception { System.out.println("In tear down" ); -// tearDownJavaRESTServer(dbName, fNames, restServerName); + tearDownJavaRESTServer(dbName, fNames, restServerName); } @Before From d65dbdf4b5c48b74bb0465f6622389022bdf7b72 Mon Sep 17 00:00:00 2001 From: sravan Date: Fri, 5 Sep 2014 15:41:21 -0700 Subject: [PATCH 092/357] adding new test to test transaction for POJO --- .../TestPOJOReadWriteWithTransactions.java | 281 ++++++++++++++++++ 1 file changed, 281 insertions(+) create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/TestPOJOReadWriteWithTransactions.java diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOReadWriteWithTransactions.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOReadWriteWithTransactions.java new file mode 100644 index 000000000..1b5c6ee94 --- /dev/null +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOReadWriteWithTransactions.java @@ -0,0 +1,281 @@ +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.util.Iterator; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.Transaction; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.pojo.PojoPage; +import com.marklogic.client.pojo.PojoRepository; + +public class TestPOJOReadWriteWithTransactions extends BasicJavaClientREST{ + + private static String dbName = "TestPOJORWTransDB"; + private static String [] fNames = {"TestPOJORWTransDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client ; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + System.out.println("In tear down" ); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } + + @Before + public void setUp() throws Exception { + client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST); + } + + @After + public void tearDown() throws Exception { + // release client + client.release(); + } + + public Artifact getArtifact(int counter){ + Company acme = new Company(); + acme.setName("Acme "+counter+", Inc."); + acme.setWebsite("http://www.acme"+counter+".com"); + acme.setLatitude(41.998+counter); + acme.setLongitude(-87.966+counter); + Artifact cogs = new Artifact(); + cogs.setId(counter); + cogs.setName("Cogs "+counter); + cogs.setManufacturer(acme); + cogs.setInventory(1000+counter); + + return cogs; + } + public void validateArtifact(Artifact art) + { + assertNotNull("Artifact object should never be Null",art); + assertNotNull("Id should never be Null",art.id); + assertTrue("Inventry is always greater than 1000", art.getInventory()>1000); + } + //This test is to persist a simple design model objects in ML, read from ML, delete all + // Issue 104 for unable to have transaction in count,exists, delete methods + @Test + public void testPOJOWriteWithTransaction() throws Exception { + PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); + Transaction t= client.openTransaction(); + //Load more than 100 objects + try{ + for(int i=1;i<112;i++){ + products.write(this.getArtifact(i),t); + } +// assertEquals("Total number of object recods",111, products.count()); + for(long i=1;i<112;i++){ +// assertTrue("Product id "+i+" does not exist",products.exists(i)); + this.validateArtifact(products.read(i,t)); + } + + }catch(Exception e){ + throw e; + }finally{ + t.rollback(); + } + + for(long i=1;i<112;i++){ + assertFalse("Product id exists ?",products.exists(i)); + } + } + //This test is to persist objects into different collections, read documents based on Id and delete single object based on Id + @Test + public void testPOJOWriteWithTransCollection() throws Exception { + PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); + //Load more than 110 objects into different collections + Transaction t= client.openTransaction(); + try{ + for(int i=112;i<222;i++){ + if(i%2==0){ + products.write(this.getArtifact(i),t,"even","numbers"); + } + else { + products.write(this.getArtifact(i),t,"odd","numbers"); + } + } + }catch(Exception e){throw e;} + finally{t.commit();} + assertEquals("Total number of object recods",110, products.count("numbers")); + assertEquals("Collection even count",55,products.count("even")); + assertEquals("Collection odd count",55,products.count("odd")); + for(long i=112;i<222;i++){ + // validate all the records inserted are readable + assertTrue("Product id "+i+" does not exist",products.exists(i)); + this.validateArtifact(products.read(i)); + } +// t= client.openTransaction(); + + products.delete((long)112); + assertFalse("Product id 112 exists ?",products.exists((long)112)); + products.deleteAll(); + //see any document exists + for(long i=112;i<222;i++){ + assertFalse("Product id "+i+" exists ?",products.exists(i)); + } + //see if it complains when there are no records + products.delete((long)112); + products.deleteAll(); + } + //This test is to read objects into pojo page based on Ids + // until #103 is resolved @Test + public void testPOJOWriteWithPojoPage() { + PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); + //Load more than 110 objects into different collections + products.deleteAll(); + Long[] ids= new Long[111]; + int j=0; + for(int i=222;i<333;i++){ + ids[j] =(long) i;j++; + if(i%2==0){ + products.write(this.getArtifact(i),"even","numbers"); + } + else { + products.write(this.getArtifact(i),"odd","numbers"); + } + } + assertEquals("Total number of object recods",111, products.count("numbers")); + assertEquals("Collection even count",56,products.count("even")); + assertEquals("Collection odd count",55,products.count("odd")); + + System.out.println("Default Page length setting on docMgr :"+products.getPageLength()); + assertEquals("Default setting for page length",50,products.getPageLength()); + products.setPageLength(100); +// assertEquals("explicit setting for page length",1,products.getPageLength()); + PojoPage p= products.read(ids); + // test for page methods + //Issue- assertEquals("Number of records",1,p.size()); + System.out.println("Page size"+p.size()); +// assertEquals("Starting record in first page ",1,p.getStart()); + System.out.println("Starting record in first page "+p.getStart()); + +// assertEquals("Total number of estimated results:",111,p.getTotalSize()); + System.out.println("Total number of estimated results:"+p.getTotalSize()); +// assertEquals("Total number of estimated pages :",111,p.getTotalPages()); + System.out.println("Total number of estimated pages :"+p.getTotalPages()); + assertFalse("Is this First page :",p.isFirstPage());//this is bug + assertFalse("Is this Last page :",p.isLastPage()); + assertTrue("Is this First page has content:",p.hasContent()); + // Need the Issue #75 to be fixed + assertTrue("Is first page has previous page ?",p.hasPreviousPage()); + long pageNo=1,count=0; + do{ + count=0; + if(pageNo >1){ + assertFalse("Is this first Page", p.isFirstPage()); + assertTrue("Is page has previous page ?",p.hasPreviousPage()); + } + Iterator itr = p.iterator(); + while(itr.hasNext()){ + this.validateArtifact(p.iterator().next()); + count++; + } +// assertEquals("document count", p.size(),count); + System.out.println("Is this Last page :"+p.hasContent()+p.isLastPage()); + pageNo = pageNo + p.getPageSize(); + }while((p.isLastPage()) && p.hasContent()); + assertTrue("page count is 111 ",pageNo == p.getTotalPages()); + assertTrue("Page has previous page ?",p.hasPreviousPage()); + assertEquals("page size", 1,p.getPageSize()); + assertEquals("document count", 111,p.getTotalSize()); + assertFalse("Page has any records ?",p.hasContent()); + + + products.deleteAll(); + //see any document exists + for(long i=112;i<222;i++){ + assertFalse("Product id "+i+" exists ?",products.exists(i)); + } + //see if it complains when there are no records + } + @Test + public void testPOJOWriteWithPojoPageReadAll() throws Exception { + PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); + //Load more than 110 objects into different collections + Transaction t= client.openTransaction(); + PojoPage p; + try{ + for(int i=222;i<333;i++){ + if(i%2==0){ + products.write(this.getArtifact(i),t,"even","numbers"); + } + else { + products.write(this.getArtifact(i),t,"odd","numbers"); + } + } + //issue# 104 +// assertEquals("Total number of object recods",111, products.count("numbers")); +// assertEquals("Collection even count",56,products.count("even")); +// assertEquals("Collection odd count",55,products.count("odd")); + + System.out.println("Default Page length setting on docMgr :"+products.getPageLength()); + assertEquals("Default setting for page length",50,products.getPageLength()); + products.setPageLength(25); + assertEquals("explicit setting for page length",25,products.getPageLength()); + p= products.readAll(1,t); + // test for page methods + assertEquals("Number of records",25,p.size()); + System.out.println("Page size"+p.size()); + assertEquals("Starting record in first page ",1,p.getStart()); + System.out.println("Starting record in first page "+p.getStart()); + + assertEquals("Total number of estimated results:",111,p.getTotalSize()); + System.out.println("Total number of estimated results:"+p.getTotalSize()); + assertEquals("Total number of estimated pages :",5,p.getTotalPages()); + System.out.println("Total number of estimated pages :"+p.getTotalPages()); + assertTrue("Is this First page :",p.isFirstPage()); + assertFalse("Is this Last page :",p.isLastPage()); + assertTrue("Is this First page has content:",p.hasContent()); + // Need the Issue #75 to be fixed + assertTrue("Is first page has previous page ?",p.hasPreviousPage()); + long pageNo=1,count=0; + do{ + count=0; + p= products.readAll(pageNo,t); + + if(pageNo >1){ + assertFalse("Is this first Page", p.isFirstPage()); + assertTrue("Is page has previous page ?",p.hasPreviousPage()); + } + + while(p.iterator().hasNext()){ + this.validateArtifact(p.iterator().next()); + count++; + } + assertEquals("document count", p.size(),count); + + pageNo = pageNo + p.getPageSize(); + }while(!(p.isLastPage()) && pageNo < p.getTotalSize()); + + assertTrue("Page has previous page ?",p.hasPreviousPage()); + assertEquals("page size", 11,p.size()); + assertEquals("document count", 111,p.getTotalSize()); + }catch(Exception e){ + throw e; + }finally{ + t.rollback(); + } + p=products.readAll(1); + assertFalse("Page has any records ?",p.hasContent()); + + } + + +} From 4354e1dcfabf76c9f5fe54cdd0e31dca3b312f08 Mon Sep 17 00:00:00 2001 From: sravan Date: Mon, 8 Sep 2014 14:10:23 -0700 Subject: [PATCH 093/357] adding more tests for POJO --- .../javaclient/TestPOJOBasicSearch.java | 191 ++++++++++++++++++ .../TestPOJOReadWriteWithTransactions.java | 75 ++++++- 2 files changed, 265 insertions(+), 1 deletion(-) create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/TestPOJOBasicSearch.java diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOBasicSearch.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOBasicSearch.java new file mode 100644 index 000000000..e3c3fe70d --- /dev/null +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOBasicSearch.java @@ -0,0 +1,191 @@ +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.util.Iterator; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.pojo.PojoPage; +import com.marklogic.client.pojo.PojoRepository; + +public class TestPOJOBasicSearch extends BasicJavaClientREST { + private static String dbName = "TestPOJObasicSearchDB"; + private static String [] fNames = {"TestPOJObasicSearchDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client ; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + System.out.println("In tear down" ); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } + @Before + public void setUp() throws Exception { + client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST); + } + @After + public void tearDown() throws Exception { + // release client + client.release(); + } + + public Artifact getArtifact(int counter){ + Company acme = new Company(); + acme.setName("Acme "+counter+", Inc."); + acme.setWebsite("http://www.acme"+counter+".com"); + acme.setLatitude(41.998+counter); + acme.setLongitude(-87.966+counter); + Artifact cogs = new Artifact(); + cogs.setId(counter); + cogs.setName("Cogs "+counter); + cogs.setManufacturer(acme); + cogs.setInventory(1000+counter); + + return cogs; + } + public void validateArtifact(Artifact art) + { + assertNotNull("Artifact object should never be Null",art); + assertNotNull("Id should never be Null",art.id); + assertTrue("Inventry is always greater than 1000", art.getInventory()>1000); + } + + //This test is to search objects under different collections, read documents to validate + @Test + public void testPOJOSearchWithCollections() { + PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); + PojoPage p; + //Load more than 111 objects into different collections + for(int i=1;i<112;i++){ + if(i%2==0){ + products.write(this.getArtifact(i),"even","numbers"); + } + else { + products.write(this.getArtifact(i),"odd","numbers"); + } + } + assertEquals("Total number of object recods",111, products.count("numbers")); + assertEquals("Collection even count",55,products.count("even")); + assertEquals("Collection odd count",56,products.count("odd")); + + products.setPageLength(5); + long pageNo=1,count=0; + do{ + count =0; + p = products.search(pageNo, "even"); + while(p.iterator().hasNext()){ + Artifact a =p.iterator().next(); + validateArtifact(a); + assertTrue("Artifact Id is even", a.getId()%2==0); + count++; + } + assertEquals("Page size",count,p.size()); + pageNo=pageNo+p.getPageSize(); + }while(!p.isLastPage() && pageNo products = client.newPojoRepository(Artifact.class, Long.class); + //Load more than 110 objects into different collections + products.deleteAll(); + for(int i=222;i<333;i++){ + if(i%2==0){ + products.write(this.getArtifact(i),"even","numbers"); + } + else { + products.write(this.getArtifact(i),"odd","numbers"); + } + } + assertEquals("Total number of object recods",111, products.count("numbers")); + assertEquals("Collection even count",56,products.count("even")); + assertEquals("Collection odd count",55,products.count("odd")); + + System.out.println("Default Page length setting on docMgr :"+products.getPageLength()); + assertEquals("Default setting for page length",50,products.getPageLength()); + products.setPageLength(1); + assertEquals("explicit setting for page length",1,products.getPageLength()); + PojoPage p= products.search(1,"even","odd"); + // test for page methods + assertEquals("Number of records",1,p.size()); + System.out.println("Page size"+p.size()); + assertEquals("Starting record in first page ",1,p.getStart()); + System.out.println("Starting record in first page "+p.getStart()); + + assertEquals("Total number of estimated results:",111,p.getTotalSize()); + System.out.println("Total number of estimated results:"+p.getTotalSize()); + assertEquals("Total number of estimated pages :",111,p.getTotalPages()); + System.out.println("Total number of estimated pages :"+p.getTotalPages()); + assertFalse("Is this First page :",p.isFirstPage());//this is bug + assertFalse("Is this Last page :",p.isLastPage()); + assertTrue("Is this First page has content:",p.hasContent()); + // Need the Issue #75 to be fixed + assertTrue("Is first page has previous page ?",p.hasPreviousPage()); + long pageNo=1,count=0; + do{ + count=0; + p= products.search(pageNo,"numbers"); + + if(pageNo >1){ + assertFalse("Is this first Page", p.isFirstPage()); + assertTrue("Is page has previous page ?",p.hasPreviousPage()); + } + + while(p.iterator().hasNext()){ + this.validateArtifact(p.iterator().next()); + count++; + } + assertEquals("document count", p.size(),count); + + pageNo = pageNo + p.getPageSize(); + }while(!(p.isLastPage()) && pageNo < p.getTotalSize()); + assertTrue("page count is 111 ",pageNo == p.getTotalPages()); + assertTrue("Page has previous page ?",p.hasPreviousPage()); + assertEquals("page size", 1,p.getPageSize()); + assertEquals("document count", 111,p.getTotalSize()); + + products.deleteAll(); + p= products.readAll(1); + assertFalse("Page has any records ?",p.hasContent()); + + + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOReadWriteWithTransactions.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOReadWriteWithTransactions.java index 1b5c6ee94..8bbf28acc 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOReadWriteWithTransactions.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOReadWriteWithTransactions.java @@ -277,5 +277,78 @@ public void testPOJOWriteWithPojoPageReadAll() throws Exception { } - + @Test + public void testPOJOSearchWithCollectionsandTransaction() throws Exception { + PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); + PojoPage p; + Transaction t= client.openTransaction(); + //Load more than 111 objects into different collections + try{ + for(int i=1;i<112;i++){ + if(i%2==0){ + products.write(this.getArtifact(i),t,"even","numbers"); + } + else { + products.write(this.getArtifact(i),"odd","numbers"); + } + } + assertEquals("Total number of object recods",56, products.count("numbers")); + assertEquals("Collection even count",0,products.count("even")); + assertEquals("Collection odd count",56,products.count("odd")); + + products.setPageLength(10); + long pageNo=1,count=0; + do{ + count =0; + p = products.search(pageNo, "even"); + while(p.iterator().hasNext()){ + Artifact a =p.iterator().next(); + validateArtifact(a); + assertTrue("Artifact Id is even", a.getId()%2==0); + count++; + } + assertEquals("Page size",count,p.size()); + pageNo=pageNo+p.getPageSize(); + }while(!p.isLastPage() && pageNo Date: Tue, 9 Sep 2014 16:03:13 -0700 Subject: [PATCH 094/357] adiing new test for pojo search with regular query definitions, related changes --- .../marklogic/javaclient/ConnectedRESTQA.java | 34 +-- .../javaclient/TestPOJOWithStringQD.java | 248 ++++++++++++++++++ 2 files changed, 265 insertions(+), 17 deletions(-) create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithStringQD.java diff --git a/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java b/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java index 724d380ec..01ac7f574 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java @@ -476,21 +476,20 @@ public static void deleteRESTServerWithDB(String restServerName) { */ public static void deleteRESTServer(String restServerName) { try{ - associateRESTServerWithDB(restServerName,"Documents"); - // DefaultHttpClient client = new DefaultHttpClient(); - // - // client.getCredentialsProvider().setCredentials( - // new AuthScope("localhost", 8002), - // new UsernamePasswordCredentials("admin", "admin")); - // - // HttpDelete delete = new HttpDelete("http://localhost:8002/v1/rest-apis/"+restServerName+"&include=modules"); - // HttpResponse response = client.execute(delete); - // - // if(response.getStatusLine().getStatusCode()== 202){ - // Thread.sleep(3500); - // waitForServerRestart(); - // } - // else System.out.println("Server response "+response.getStatusLine().getStatusCode()); + DefaultHttpClient client = new DefaultHttpClient(); + + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + + HttpDelete delete = new HttpDelete("http://localhost:8002/v1/rest-apis/"+restServerName+"&include=modules"); + HttpResponse response = client.execute(delete); + + if(response.getStatusLine().getStatusCode()== 202){ + Thread.sleep(3500); + waitForServerRestart(); + } + else System.out.println("Server response "+response.getStatusLine().getStatusCode()); }catch (Exception e) { // writing error to Log System.out.println("Inside Deleting Rest server is throwing an error"); @@ -615,12 +614,12 @@ else if(response.getStatusLine().getStatusCode() == 200){ public static void tearDownJavaRESTServer(String dbName, String [] fNames, String restServerName) throws Exception{ try{ - deleteRESTServer(restServerName); + associateRESTServerWithDB(restServerName,"Documents"); }catch(Exception e){ System.out.println("From Deleting Rest server called funnction is throwing an error"); e.printStackTrace(); } - waitForServerRestart(); + try{ for(int i = 0; i < fNames.length; i++){ detachForest(dbName, fNames[i]); @@ -647,6 +646,7 @@ public static void tearDownJavaRESTServerWithDB(String restServerName) throws Ex try{ deleteRESTServerWithDB(restServerName); + waitForServerRestart(); }catch(Exception e){ e.printStackTrace(); } diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithStringQD.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithStringQD.java new file mode 100644 index 000000000..200e88f71 --- /dev/null +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithStringQD.java @@ -0,0 +1,248 @@ +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.fasterxml.jackson.databind.JsonNode; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.JacksonHandle; +import com.marklogic.client.io.SearchHandle; +import com.marklogic.client.io.StringHandle; +import com.marklogic.client.pojo.PojoPage; +import com.marklogic.client.pojo.PojoRepository; +import com.marklogic.client.query.MatchDocumentSummary; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.StringQueryDefinition; + +public class TestPOJOWithStringQD extends BasicJavaClientREST { + private static String dbName = "TestPOJOStringQDSearchDB"; + private static String [] fNames = {"TestPOJOStringQDSearchDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client ; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + System.out.println("In setup"); +// setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); +// addRangePathIndex(dbName, "string", "com.marklogic.javaclient.Artifact/name", "http://marklogic.com/collation/", "ignore"); +// addRangePathIndex(dbName, "string", "com.marklogic.javaclient.Artifact/manufacturer/com.marklogic.javaclient.Company/name", "http://marklogic.com/collation/", "ignore"); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + System.out.println("In tear down" ); +// tearDownJavaRESTServer(dbName, fNames, restServerName); + } + @Before + public void setUp() throws Exception { + client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST); + } + @After + public void tearDown() throws Exception { + // release client + client.release(); + } + + public Artifact getArtifact(int counter){ + + Artifact cogs = new Artifact(); + cogs.setId(counter); + cogs.setName("Cogs "+counter); + if(counter % 2 ==0){ + Company acme = new Company(); + acme.setName("Acme "+counter+", Inc."); + acme.setWebsite("http://www.acme"+counter+".com"); + acme.setLatitude(41.998+counter); + acme.setLongitude(-87.966+counter); + cogs.setManufacturer(acme); + + }else{ + Company widgets = new Company(); + widgets.setName("Widgets "+counter+", Inc."); + widgets.setWebsite("http://www.widgets"+counter+".com"); + widgets.setLatitude(41.998+counter); + widgets.setLongitude(-87.966+counter); + cogs.setManufacturer(widgets); + } + + cogs.setInventory(1000+counter); + return cogs; + } + public void validateArtifact(Artifact art) + { + assertNotNull("Artifact object should never be Null",art); + assertNotNull("Id should never be Null",art.id); + assertTrue("Inventry is always greater than 1000", art.getInventory()>1000); + } + public void loadSimplePojos(PojoRepository products) + { + for(int i=1;i<111;i++){ + if(i%2==0){ + products.write(this.getArtifact(i),"even","numbers"); + } + else { + products.write(this.getArtifact(i),"odd","numbers"); + } + } + } + @Test + public void testPOJOSearchWithoutSearchHandle() { + PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); + PojoPage p; + this.loadSimplePojos(products); + QueryManager queryMgr = client.newQueryManager(); + StringQueryDefinition qd = queryMgr.newStringDefinition(); + qd.setCriteria("Widgets"); + + products.setPageLength(11); + p = products.search(qd, 1); + assertEquals("total no of pages",5,p.getTotalPages()); + System.out.println(p.getTotalPages()); + long pageNo=1,count=0; + do{ + count =0; + + p = products.search(qd,pageNo); + + while(p.iterator().hasNext()){ + Artifact a =p.iterator().next(); + validateArtifact(a); + assertTrue("Artifact Id is odd", a.getId()%2!=0); + assertTrue("Company name contains widgets",a.getManufacturer().getName().contains("Widgets")); + count++; + // System.out.println(a.getId()+" "+a.getManufacturer().getName() +" "+count); + } + assertEquals("Page size",count,p.size()); + pageNo=pageNo+p.getPageSize(); + }while(!p.isLastPage() && pageNo products = client.newPojoRepository(Artifact.class, Long.class); + PojoPage p; + this.loadSimplePojos(products); + QueryManager queryMgr = client.newQueryManager(); + StringQueryDefinition qd = queryMgr.newStringDefinition(); + qd.setCriteria("Acme"); + SearchHandle results = new SearchHandle(); + products.setPageLength(11); + p = products.search(qd, 1,results); + assertEquals("total no of pages",5,p.getTotalPages()); + System.out.println(p.getTotalPages()); + long pageNo=1,count=0; + do{ + count =0; + p = products.search(qd,pageNo,results); + + while(p.iterator().hasNext()){ + Artifact a =p.iterator().next(); + validateArtifact(a); + assertTrue("Artifact Id is even", a.getId()%2==0); + assertTrue("Company name contains Acme",a.getManufacturer().getName().contains("Acme")); + count++; + // System.out.println(a.getId()+" "+a.getManufacturer().getName() +" "+count); + } + assertEquals("Page size",count,p.size()); + pageNo=pageNo+p.getPageSize(); + MatchDocumentSummary[] mds =results.getMatchResults(); + assertEquals("Size of the results summary",11,mds.length); + for(MatchDocumentSummary md:mds){ + assertTrue("every uri should contain the class name",md.getUri().contains("Artifact")); + } + String[] facetNames = results.getFacetNames(); + for(String fname:facetNames){ + System.out.println(fname); + } + assertEquals("Total resulr from search handle ",55,results.getTotalResults()); + assertTrue("Search Handle metric results ",results.getMetrics().getTotalTime()>0); + }while(!p.isLastPage() && pageNo products = client.newPojoRepository(Artifact.class, Long.class); + PojoPage p; + this.loadSimplePojos(products); + QueryManager queryMgr = client.newQueryManager(); + StringQueryDefinition qd = queryMgr.newStringDefinition(); + qd.setCriteria("cogs"); + JacksonHandle results = new JacksonHandle(); + p = products.search(qd, 1,results); + products.setPageLength(11); + assertEquals("total no of pages",3,p.getTotalPages()); + // System.out.println(p.getTotalPages()+results.get().toString()); + long pageNo=1,count=0; + do{ + count =0; + p = products.search(qd,pageNo,results); + + while(p.iterator().hasNext()){ + Artifact a =p.iterator().next(); + validateArtifact(a); + count++; + // System.out.println(a.getId()+" "+a.getManufacturer().getName() +" "+count); + } + assertEquals("Page size",count,p.size()); + pageNo=pageNo+p.getPageSize(); + + assertEquals("Page start from search handls vs page methods",results.get().get("start").asLong(),p.getStart() ); + assertEquals("Format in the search handle","json",results.get().withArray("results").get(1).path("format").asText()); + assertTrue("Uri in search handle contains Artifact",results.get().withArray("results").get(1).path("uri").asText().contains("Artifact")); + // System.out.println(results.get().toString()); + }while(!p.isLastPage() && pageNo products = client.newPojoRepository(Artifact.class, Long.class); + PojoPage p; + this.loadSimplePojos(products); + QueryManager queryMgr = client.newQueryManager(); + StringQueryDefinition qd = queryMgr.newStringDefinition(); + qd.setCriteria("cogs 2"); + StringHandle results = new StringHandle(); + JacksonHandle jh = new JacksonHandle(); + p = products.search(qd, 1,jh); + + long pageNo=1,count=0; + do{ + count =0; + p = products.search(qd,pageNo,results.withFormat(Format.JSON)); + + while(p.iterator().hasNext()){ + Artifact a =p.iterator().next(); + validateArtifact(a); + count++; + // System.out.println(a.getId()+" "+a.getManufacturer().getName() +" "+count); + } + assertEquals("Page total results",count,p.getTotalSize()); + pageNo=pageNo+p.getPageSize(); +// + System.out.println(results.get().toString()); + }while(!p.isLastPage() && pageNo Date: Tue, 9 Sep 2014 17:56:05 -0700 Subject: [PATCH 095/357] New Bulk read/write tests with optimistic locking, JacksonHandler. --- .../TestBulkReadWriteMetaDataChange.java | 315 ++++++++++++++ .../TestBulkReadWriteWithJacksonHandle.java | 391 ++++++++++++++++++ .../TestBulkWriteOptimisticLocking.java | 226 ++++++++++ 3 files changed, 932 insertions(+) create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteMetaDataChange.java create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonHandle.java create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteOptimisticLocking.java diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteMetaDataChange.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteMetaDataChange.java new file mode 100644 index 000000000..2fd6ec860 --- /dev/null +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteMetaDataChange.java @@ -0,0 +1,315 @@ +/** + * This tests meta-data changes for text documents. + */ +package com.marklogic.javaclient; + +import static org.junit.Assert.assertTrue; + +import java.util.Calendar; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.DocumentPage; +import com.marklogic.client.document.DocumentRecord; +import com.marklogic.client.document.DocumentWriteSet; +import com.marklogic.client.document.TextDocumentManager; +import com.marklogic.client.io.DocumentMetadataHandle; +import com.marklogic.client.io.DocumentMetadataHandle.Capability; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentCollections; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentPermissions; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentProperties; +import com.marklogic.client.io.StringHandle; + +/** + * + * This test is designed to update meta-data bulk writes and reads with one type of Manager + * and one content type text. + * + * TextDocumentManager + * + */ +public class TestBulkReadWriteMetaDataChange extends BasicJavaClientREST { + private static String dbName = "TestBulkReadWriteMetaDataChangeDB"; + private static String [] fNames = {"TestBulkReadWriteMetaDataChangeDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client ; + + /** + * @throws java.lang.Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + System.out.println("In Setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + createRESTUser("app-user", "password", "rest-writer","rest-reader" ); + + } + + /** + * @throws java.lang.Exception + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + System.out.println("In tear down" ); + tearDownJavaRESTServer(dbName, fNames, restServerName); + deleteRESTUser("app-user"); + } + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + // create new connection for each test below + client = DatabaseClientFactory.newClient("localhost", restPort, "app-user", "password", Authentication.DIGEST); + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + System.out.println("Running clear script"); + // release client + client.release(); + } + + public DocumentMetadataHandle setMetadata(){ + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); + metadataHandle.getCollections().addAll("my-collection1","my-collection2"); + metadataHandle.getPermissions().add("app-user", Capability.UPDATE, Capability.READ); + metadataHandle.getProperties().put("reviewed", true); + metadataHandle.getProperties().put("myString", "foo"); + metadataHandle.getProperties().put("myInteger", 10); + metadataHandle.getProperties().put("myDecimal", 34.56678); + metadataHandle.getProperties().put("myCalendar", Calendar.getInstance().get(Calendar.YEAR)); + metadataHandle.setQuality(23); + return metadataHandle; + } + + public DocumentMetadataHandle setUpdatedMetadataProperties(){ + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); + metadataHandle.getProperties().put("reviewed", false); + metadataHandle.getProperties().put("myString", "bar"); + metadataHandle.getProperties().put("myInteger", 20); + metadataHandle.getProperties().put("myDecimal", 3459.012678); + metadataHandle.getProperties().put("myCalendar", Calendar.getInstance().get(Calendar.YEAR)); + metadataHandle.setQuality(12); + return metadataHandle; + } + + public DocumentMetadataHandle setUpdatedMetadataCollections(){ + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle().withCollections("my-collection3","my-collection4"); + //metadataHandle.getCollections().addAll("my-collection1","my-collection2"); + metadataHandle.getPermissions().add("app-user", Capability.UPDATE, Capability.READ); + metadataHandle.getProperties().put("reviewed", true); + metadataHandle.getProperties().put("myString", "foo"); + metadataHandle.getProperties().put("myInteger", 10); + metadataHandle.getProperties().put("myDecimal", 34.56678); + metadataHandle.getProperties().put("myCalendar", Calendar.getInstance().get(Calendar.YEAR)); + metadataHandle.setQuality(23); + return metadataHandle; + } + + public void validateMetadata(DocumentMetadataHandle mh){ + // get metadata values + DocumentProperties properties = mh.getProperties(); + DocumentPermissions permissions = mh.getPermissions(); + DocumentCollections collections = mh.getCollections(); + + // Properties + String actualProperties = getDocumentPropertiesString(properties); + System.out.println("Returned properties: " + actualProperties); + + assertTrue("Document properties difference in size value", actualProperties.contains("size:5")); + assertTrue("Document property reviewed not found or not correct", actualProperties.contains("reviewed:true")); + assertTrue("Document property myInteger not found or not correct", actualProperties.contains("myInteger:10")); + assertTrue("Document property myDecimal not found or not correct", actualProperties.contains("myDecimal:34.56678")); + assertTrue("Document property myCalendar not found or not correct", actualProperties.contains("myCalendar:2014")); + assertTrue("Document property myString not found or not correct", actualProperties.contains("myString:foo")); + + // Permissions + String actualPermissions = getDocumentPermissionsString(permissions); + System.out.println("Returned permissions: " + actualPermissions); + + assertTrue("Document permissions difference in size value", actualPermissions.contains("size:3")); + assertTrue("Document permissions difference in rest-reader permission", actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue("Document permissions difference in app-user permissions", + (actualPermissions.contains("app-user:[UPDATE, READ]") || actualPermissions.contains("app-user:[READ, UPDATE]"))); + + // Collections + String actualCollections = getDocumentCollectionsString(collections); + System.out.println("Returned collections: " + actualCollections); + + assertTrue("Document collections difference in size value", actualCollections.contains("size:2")); + assertTrue("my-collection1 not found", actualCollections.contains("my-collection1")); + assertTrue("my-collection2 not found", actualCollections.contains("my-collection2")); + } + + public void validateUpdatedMetadataProperties(DocumentMetadataHandle mh){ + // get metadata values + DocumentProperties properties = mh.getProperties(); + DocumentPermissions permissions = mh.getPermissions(); + DocumentCollections collections = mh.getCollections(); + + // Properties + String actualProperties = getDocumentPropertiesString(properties); + System.out.println("Returned properties after Meta-data only update: " + actualProperties); + + assertTrue("Document properties difference in size value", actualProperties.contains("size:5")); + assertTrue("Document property reviewed not found or not correct", actualProperties.contains("reviewed:true")); + assertTrue("Document property myInteger not found or not correct", actualProperties.contains("myInteger:10")); + assertTrue("Document property myDecimal not found or not correct", actualProperties.contains("myDecimal:34.56678")); + assertTrue("Document property myCalendar not found or not correct", actualProperties.contains("myCalendar:2014")); + assertTrue("Document property myString not found or not correct", actualProperties.contains("myString:foo")); + + // Permissions + String actualPermissions = getDocumentPermissionsString(permissions); + System.out.println("Returned permissions: " + actualPermissions); + + assertTrue("Document permissions difference in size value", actualPermissions.contains("size:3")); + assertTrue("Document permissions difference in rest-reader permission", actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue("Document permissions difference in app-user permissions", + (actualPermissions.contains("app-user:[UPDATE, READ]") || actualPermissions.contains("app-user:[READ, UPDATE]"))); + + // Collections + String actualCollections = getDocumentCollectionsString(collections); + System.out.println("Returned collections: " + actualCollections); + + assertTrue("Document collections difference in size value", actualCollections.contains("size:2")); + assertTrue("my-collection1 not found", actualCollections.contains("my-collection1")); + assertTrue("my-collection2 not found", actualCollections.contains("my-collection2")); + } + + public void validateUpdatedMetadataCollections(DocumentMetadataHandle mh){ + // get metadata values + DocumentProperties properties = mh.getProperties(); + DocumentPermissions permissions = mh.getPermissions(); + DocumentCollections collections = mh.getCollections(); + + // Properties + String actualProperties = getDocumentPropertiesString(properties); + System.out.println("Returned properties: " + actualProperties); + + assertTrue("Document properties difference in size value", actualProperties.contains("size:5")); + assertTrue("Document property reviewed not found or not correct", actualProperties.contains("reviewed:true")); + assertTrue("Document property myInteger not found or not correct", actualProperties.contains("myInteger:10")); + assertTrue("Document property myDecimal not found or not correct", actualProperties.contains("myDecimal:34.56678")); + assertTrue("Document property myCalendar not found or not correct", actualProperties.contains("myCalendar:2014")); + assertTrue("Document property myString not found or not correct", actualProperties.contains("myString:foo")); + + // Permissions + String actualPermissions = getDocumentPermissionsString(permissions); + System.out.println("Returned permissions: " + actualPermissions); + + assertTrue("Document permissions difference in size value", actualPermissions.contains("size:3")); + assertTrue("Document permissions difference in rest-reader permission", actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue("Document permissions difference in app-user permissions", + (actualPermissions.contains("app-user:[UPDATE, READ]") || actualPermissions.contains("app-user:[READ, UPDATE]"))); + + // Collections + String actualCollections = getDocumentCollectionsString(collections); + System.out.println("Returned collections: " + actualCollections); + + assertTrue("Document collections difference in size value", actualCollections.contains("size:2")); + assertTrue("my-collection1 not found", actualCollections.contains("my-collection3")); + assertTrue("my-collection2 not found", actualCollections.contains("my-collection4")); + } + + /* + * This test verifies that properties do not change when new meta data is used in a bulk write set. + * Verified by reading individual documents + */ + + @Test(expected=com.marklogic.client.ForbiddenUserException.class) + public void testWriteMultipleTextDocWithChangedMetadataProperties() + { + String docId[] = {"/foo/test/myFoo1.txt","/foo/test/myFoo2.txt","/foo/test/myFoo3.txt"}; + + TextDocumentManager docMgr = client.newTextDocumentManager(); + + DocumentWriteSet writeset =docMgr.newWriteSet(); + // put metadata + DocumentMetadataHandle mh = setMetadata(); + + writeset.addDefault(mh); + writeset.add(docId[0], new StringHandle().with("This is so foo1")); + writeset.add(docId[1], new StringHandle().with("This is so foo2")); + writeset.add(docId[2], new StringHandle().with("This is so foo3")); + docMgr.write(writeset); + DocumentPage page = docMgr.read(docId); + + while(page.hasNext()){ + DocumentRecord rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh); + validateMetadata(mh); + } + validateMetadata(mh); + + // Add new meta-data + DocumentMetadataHandle mhUpdated = setUpdatedMetadataProperties(); + writeset.addDefault(mhUpdated); + + docMgr.write(writeset); + page = docMgr.read(docId); + + while(page.hasNext()){ + DocumentRecord rec = page.next(); + docMgr.readMetadata(rec.getUri(), mhUpdated); + validateUpdatedMetadataProperties(mhUpdated); + } + validateUpdatedMetadataProperties(mhUpdated); + } + + /* + * This test verifies that collections do change when new meta data is used in a bulk write set. + * Verified by reading individual documents + */ + @Test + public void testWriteMultipleTextDocWithChangedMetadataCollections() + { + String docId[] = {"/foo/test/myFoo4.txt","/foo/test/myFoo5.txt","/foo/test/myFoo6.txt"}; + TextDocumentManager docMgr = client.newTextDocumentManager(); + + DocumentWriteSet writeset = docMgr.newWriteSet(); + // put metadata + DocumentMetadataHandle mh = setMetadata(); + + writeset.addDefault(mh); + writeset.add(docId[0], new StringHandle().with("This is so foo4")); + writeset.add(docId[1], new StringHandle().with("This is so foo5")); + writeset.add(docId[2], new StringHandle().with("This is so foo6")); + docMgr.write(writeset); + + // Add new meta-data + DocumentMetadataHandle mhUpdated = setUpdatedMetadataCollections(); + + docMgr.writeMetadata(docId[0], mhUpdated); + docMgr.writeMetadata(docId[1], mhUpdated); + docMgr.writeMetadata(docId[2], mhUpdated); + + DocumentPage page = docMgr.read(docId); + DocumentMetadataHandle metadataHandleRead = new DocumentMetadataHandle(); + + while(page.hasNext()){ + DocumentRecord rec = page.next(); + docMgr.readMetadata(rec.getUri(), metadataHandleRead); + validateUpdatedMetadataCollections(metadataHandleRead); + } + validateUpdatedMetadataCollections(metadataHandleRead); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonHandle.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonHandle.java new file mode 100644 index 000000000..bbd1d0dcc --- /dev/null +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonHandle.java @@ -0,0 +1,391 @@ +package com.marklogic.javaclient; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.BufferedReader; +import java.io.Reader; +import java.io.StringReader; +import java.util.Calendar; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.DocumentPage; +import com.marklogic.client.document.DocumentRecord; +import com.marklogic.client.document.DocumentWriteSet; +import com.marklogic.client.document.JSONDocumentManager; +import com.marklogic.client.io.DocumentMetadataHandle; +import com.marklogic.client.io.DocumentMetadataHandle.Capability; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentCollections; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentPermissions; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentProperties; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.JacksonHandle; +import com.marklogic.client.io.ReaderHandle; + +/* + * This test is designed to to test all of bulk reads and write of JSON with JacksonHandle Manager by passing set of uris + * and also by descriptors. + */ + + + +public class TestBulkReadWriteWithJacksonHandle extends BasicJavaClientREST { + + private static final int BATCH_SIZE=100; + private static final String DIRECTORY ="/bulkread/"; + private static String dbName = "TestBulkReadWriteWithJacksonHandleDB"; + private static String [] fNames = {"TestBulkReadWriteWithJacksonHandleDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client ; + @BeforeClass + public static void setUp() throws Exception + { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + setupAppServicesConstraint(dbName); + + } + + @Before public void testSetup() throws Exception + { + // create new connection for each test below + client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST); + } + @After + public void testCleanUp() throws Exception + { + System.out.println("Running CleanUp script"); + // release client + client.release(); + + + } + + public DocumentMetadataHandle setMetadata(){ + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); + metadataHandle.getCollections().addAll("my-collection1","my-collection2"); + metadataHandle.getPermissions().add("app-user", Capability.UPDATE, Capability.READ); + metadataHandle.getProperties().put("reviewed", true); + metadataHandle.getProperties().put("myString", "foo"); + metadataHandle.getProperties().put("myInteger", 10); + metadataHandle.getProperties().put("myDecimal", 34.56678); + metadataHandle.getProperties().put("myCalendar", Calendar.getInstance().get(Calendar.YEAR)); + metadataHandle.setQuality(23); + return metadataHandle; + } + + public void validateMetadata(DocumentMetadataHandle mh){ + // get metadata values + DocumentProperties properties = mh.getProperties(); + DocumentPermissions permissions = mh.getPermissions(); + DocumentCollections collections = mh.getCollections(); + + // Properties + // String expectedProperties = "size:5|reviewed:true|myInteger:10|myDecimal:34.56678|myCalendar:2014|myString:foo|"; + String actualProperties = getDocumentPropertiesString(properties); + boolean result = actualProperties.contains("size:6|"); + assertTrue("Document properties count", result); + + // Permissions + String actualPermissions = getDocumentPermissionsString(permissions); + System.out.println(actualPermissions); + + assertTrue("Document permissions difference in size value", actualPermissions.contains("size:4")); + assertTrue("Document permissions difference in flexrep-eval permission", actualPermissions.contains("flexrep-eval:[READ]")); + assertTrue("Document permissions difference in rest-reader permission", actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue("Document permissions difference in app-user permissions", + (actualPermissions.contains("app-user:[UPDATE, READ]") || actualPermissions.contains("app-user:[READ, UPDATE]"))); + + // Collections + String actualCollections = getDocumentCollectionsString(collections); + System.out.println(collections); + + assertTrue("Document collections difference in size value", actualCollections.contains("size:2")); + assertTrue("my-collection1 not found", actualCollections.contains("my-collection1")); + assertTrue("my-collection2 not found", actualCollections.contains("my-collection2")); + } + + public void validateDefaultMetadata(DocumentMetadataHandle mh){ + // get metadata values + DocumentProperties properties = mh.getProperties(); + DocumentPermissions permissions = mh.getPermissions(); + DocumentCollections collections = mh.getCollections(); + + // Properties + String actualProperties = getDocumentPropertiesString(properties); + boolean result =actualProperties.contains("size:1|"); + System.out.println(actualProperties +result); + assertTrue("Document default last modified properties count1?", result); + + + // Permissions + String actualPermissions = getDocumentPermissionsString(permissions); + + assertTrue("Document permissions difference in size value", actualPermissions.contains("size:3")); + assertTrue("Document permissions difference in flexrep-eval permission", actualPermissions.contains("flexrep-eval:[READ]")); + assertTrue("Document permissions difference in rest-reader permission", actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", actualPermissions.contains("rest-writer:[UPDATE]")); + + // Collections + String expectedCollections = "size:1|http://permission-collections/|"; + String actualCollections = getDocumentCollectionsString(collections); + + assertEquals("Document collections difference", expectedCollections, actualCollections); + } + + @Test + public void testWriteMultipleJSONDocs() throws Exception + { + String docId[] = { "/a.json", "/b.json", "/c.json" }; + String json1 = new String("{\"animal\":\"dog\", \"says\":\"woof\"}"); + String json2 = new String("{\"animal\":\"cat\", \"says\":\"meow\"}"); + String json3 = new String("{\"animal\":\"rat\", \"says\":\"keek\"}"); + + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + DocumentWriteSet writeset = docMgr.newWriteSet(); + ObjectMapper mapper = new ObjectMapper(); + + JacksonHandle jacksonHandle1 = new JacksonHandle(); + JacksonHandle jacksonHandle2 = new JacksonHandle(); + JacksonHandle jacksonHandle3 = new JacksonHandle(); + + JsonNode dogNode = mapper.readTree(json1); + jacksonHandle1.set(dogNode); + jacksonHandle1.withFormat(Format.JSON); + + JsonNode catNode = mapper.readTree(json2); + jacksonHandle2.set(catNode); + jacksonHandle2.withFormat(Format.JSON); + + JsonNode ratNode = mapper.readTree(json3); + jacksonHandle3.set(ratNode); + jacksonHandle3.withFormat(Format.JSON); + + writeset.add(docId[0], jacksonHandle1); + writeset.add(docId[1], jacksonHandle2); + writeset.add(docId[2], jacksonHandle3); + + docMgr.write(writeset); + + JacksonHandle r1 = new JacksonHandle(); + docMgr.read(docId[0], r1); + //{\"animal\":\"dog\", \"says\":\"woof\"} The space after dog is lost + assertEquals(json1.trim(), r1.toString().trim()); + docMgr.read(docId[1], r1); + assertEquals("Json File Content" + docId[1], json2, r1.toString().trim()); + docMgr.read(docId[2], r1); + assertEquals("Json File Content" + docId[2], json3, r1.toString().trim()); + //bfr.close(); + } + + /* + * + * Use JacksonHandle to load json strings using bulk write set. + * Test Bulk Read to see you can read the document specific meta-data. + */ + + @Test + public void testWriteMultipleJSONDocsWithDefaultMetadata() throws Exception + { + String docId[] = {"/a.json","/b.json","/c.json"}; + String json1 = new String("{\"animal\":\"dog\", \"says\":\"woof\"}"); + String json2 = new String("{\"animal\":\"cat\", \"says\":\"meow\"}"); + String json3 = new String("{\"animal\":\"rat\", \"says\":\"keek\"}"); + + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + DocumentWriteSet writeset = docMgr.newWriteSet(); + // put metadata + DocumentMetadataHandle mh = setMetadata(); + + ObjectMapper mapper = new ObjectMapper(); + + JacksonHandle jacksonHandle1 = new JacksonHandle(); + JacksonHandle jacksonHandle2 = new JacksonHandle(); + JacksonHandle jacksonHandle3 = new JacksonHandle(); + + JsonNode dogNode = mapper.readTree(json1); + jacksonHandle1.set(dogNode); + jacksonHandle1.withFormat(Format.JSON); + + JsonNode catNode = mapper.readTree(json2); + jacksonHandle2.set(catNode); + jacksonHandle2.withFormat(Format.JSON); + + JsonNode ratNode = mapper.readTree(json3); + jacksonHandle3.set(ratNode); + jacksonHandle3.withFormat(Format.JSON); + + writeset.addDefault(mh); + writeset.add(docId[0], jacksonHandle1); + writeset.add(docId[1], jacksonHandle2); + writeset.add(docId[2], jacksonHandle3); + + docMgr.write(writeset); + + DocumentPage page = docMgr.read(docId); + + while(page.hasNext()){ + DocumentRecord rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh); + System.out.println(rec.getUri()); + validateMetadata(mh); + } + validateMetadata(mh); + } + + /* + * + * Use JacksonHandle to load json strings using bulk write set. + * Test Bulk Read to see you can read all the documents + */ + @Test + public void testWriteMultipleJSONDocsWithDefaultMetadata2() throws Exception + { + // Synthesize input content + String doc1 = new String("{\"animal\": \"cat\", \"says\": \"meow\"}"); + String doc2 = new String("{\"animal\": \"dog\", \"says\": \"bark\"}"); + String doc3 = new String("{\"animal\": \"eagle\", \"says\": \"squeak\"}"); + String doc4 = new String("{\"animal\": \"lion\", \"says\": \"roar\"}"); + String doc5 = new String("{\"animal\": \"man\", \"says\": \"hello\"}"); + + // Synthesize input metadata + DocumentMetadataHandle defaultMetadata1 = + new DocumentMetadataHandle().withQuality(1); + DocumentMetadataHandle defaultMetadata2 = + new DocumentMetadataHandle().withQuality(2); + DocumentMetadataHandle docSpecificMetadata = + new DocumentMetadataHandle().withCollections("mySpecificCollection"); + + // Create and build up the batch + JSONDocumentManager jdm = client.newJSONDocumentManager(); + DocumentWriteSet batch = jdm.newWriteSet(); + + ObjectMapper mapper = new ObjectMapper(); + + JacksonHandle jacksonHandle1 = new JacksonHandle(); + + JsonNode catNode = mapper.readTree(doc1); + jacksonHandle1.set(catNode); + jacksonHandle1.withFormat(Format.JSON); + + JacksonHandle jacksonHandle2 = new JacksonHandle(); + + JsonNode dogNode = mapper.readTree(doc2); + jacksonHandle2.set(dogNode); + jacksonHandle2.withFormat(Format.JSON); + + JacksonHandle jacksonHandle3 = new JacksonHandle(); + + JsonNode eagleNode = mapper.readTree(doc3); + jacksonHandle3.set(eagleNode); + jacksonHandle3.withFormat(Format.JSON); + + JacksonHandle jacksonHandle4 = new JacksonHandle(); + + JsonNode lionNode = mapper.readTree(doc4); + jacksonHandle4.set(lionNode); + jacksonHandle4.withFormat(Format.JSON); + + JacksonHandle jacksonHandle5 = new JacksonHandle(); + + JsonNode manNode = mapper.readTree(doc5); + jacksonHandle5.set(manNode); + jacksonHandle5.withFormat(Format.JSON); + + // use system default metadata + batch.add("doc1.json", jacksonHandle1); + + // using batch default metadata + batch.addDefault(defaultMetadata1); + batch.add("doc2.json", jacksonHandle2); // batch default metadata + batch.add("doc3.json", docSpecificMetadata, jacksonHandle3); + batch.add("doc4.json", jacksonHandle4); // batch default metadata + + // replace batch default metadata with new metadata + batch.addDefault(defaultMetadata2); + batch.add("doc5.json", jacksonHandle5); // batch default + + // Execute the write operation + jdm.write(batch); + DocumentPage page ; + DocumentRecord rec; + // Check the results + // Doc1 should have the system default quality of 0 + page = jdm.read("doc1.json"); + DocumentMetadataHandle mh = new DocumentMetadataHandle(); + rec = page.next(); + jdm.readMetadata(rec.getUri(), mh); + validateDefaultMetadata(mh); + assertEquals("default quality",0,mh.getQuality()); + + // Doc2 should use the first batch default metadata, with quality 1 + page = jdm.read("doc2.json"); + rec = page.next(); + jdm.readMetadata(rec.getUri(), mh); + System.out.print(mh.getCollections().isEmpty()); + assertEquals("default quality",1,mh.getQuality()); + assertTrue("default collections reset",mh.getCollections().isEmpty()); + + // Doc3 should have the system default document quality (0) because quality + // was not included in the document-specific metadata. It should be in the + // collection "mySpecificCollection", from the document-specific metadata. + + page = jdm.read("doc3.json"); + rec = page.next(); + jdm.readMetadata(rec.getUri(), mh); + assertEquals("default quality",0,mh.getQuality()); + assertEquals("default collection must change","[mySpecificCollection]",mh.getCollections().toString()); + + DocumentMetadataHandle doc3Metadata = + jdm.readMetadata("doc3.json", new DocumentMetadataHandle()); + System.out.println("doc3 quality: Expected=0, Actual=" + doc3Metadata.getPermissions()); + System.out.print("doc3 collections: Expected: myCollection, Actual="); + for (String collection : doc3Metadata.getCollections()) { + System.out.print(collection + " "); + } + System.out.println(); + + // Doc 4 should also use the 1st batch default metadata, with quality 1 + page = jdm.read("doc4.json"); + rec = page.next(); + jdm.readMetadata(rec.getUri(), mh); + assertEquals("default quality",1,mh.getQuality()); + assertTrue("default collections reset",mh.getCollections().isEmpty()); + // Doc5 should use the 2nd batch default metadata, with quality 2 + page = jdm.read("doc5.json"); + rec = page.next(); + jdm.readMetadata(rec.getUri(), mh); + assertEquals("default quality",2,mh.getQuality()); + + } + + @AfterClass + public static void tearDown() throws Exception + { + System.out.println("In tear down" ); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } + + public void validateRecord(DocumentRecord record,Format type) { + + assertNotNull("DocumentRecord should never be null", record); + assertNotNull("Document uri should never be null", record.getUri()); + assertTrue("Document uri should start with " + DIRECTORY, record.getUri().startsWith(DIRECTORY)); + assertEquals("All records are expected to be in same format", type, record.getFormat()); + + } +} \ No newline at end of file diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteOptimisticLocking.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteOptimisticLocking.java new file mode 100644 index 000000000..b9625becf --- /dev/null +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteOptimisticLocking.java @@ -0,0 +1,226 @@ +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.File; +import java.util.Calendar; +import java.util.Arrays; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.w3c.dom.Document; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.admin.ServerConfigurationManager; +import com.marklogic.client.admin.ServerConfigurationManager.UpdatePolicy; +import com.marklogic.client.document.DocumentDescriptor; +import com.marklogic.client.document.DocumentPage; +import com.marklogic.client.document.DocumentRecord; +import com.marklogic.client.document.DocumentWriteSet; +import com.marklogic.client.document.GenericDocumentManager; +import com.marklogic.client.document.TextDocumentManager; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.DocumentMetadataHandle; +import com.marklogic.client.io.FileHandle; +import com.marklogic.client.io.DocumentMetadataHandle.Capability; +import com.marklogic.client.io.StringHandle; + +/* + * This test is designed to to test optimistic locking simple bulk writes with different types of + * Managers and different content type like JSON,text,binary,XMl + * + * TextDocumentManager + * XMLDocumentManager + * BinaryDocumentManager + * JSONDocumentManager + * GenericDocumentManager + */ + +public class TestBulkWriteOptimisticLocking extends BasicJavaClientREST { + private static String dbName = "TestBulkWriteOptLockDB"; + private static String[] fNames = { "TestBulkWriteOptLockDB-1" }; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client; + + @BeforeClass + public static void setUp() throws Exception { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName, restPort); + } + + @Before + public void testSetup() throws Exception { + // create new connection for each test below + client = DatabaseClientFactory.newClient("localhost", restPort, + "rest-admin", "x", Authentication.DIGEST); + + // create server configuration manager + ServerConfigurationManager configMgr = client.newServerConfigManager(); + + // read the server configuration from the database + configMgr.readConfiguration(); + + // require content versions for updates and deletes + // use UpdatePolicy.VERSION_OPTIONAL to allow but not + // require identifier use. Use UpdatePolicy.MERGE_METADATA + // (the default) to deactive identifier use + configMgr.setUpdatePolicy(UpdatePolicy.VERSION_REQUIRED); + + // write the server configuration to the database + configMgr.writeConfiguration(); + + // release the client + // client.release(); + + } + + @After + public void testCleanUp() throws Exception { + System.out.println("Running clear script"); + // release client + client.release(); + } + + @AfterClass + public static void tearDown() throws Exception { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } + + public DocumentMetadataHandle setMetadata(){ + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); + metadataHandle.getCollections().addAll("my-collection1","my-collection2"); + metadataHandle.getPermissions().add("app-user", Capability.UPDATE, Capability.READ); + metadataHandle.getProperties().put("reviewed", true); + metadataHandle.getProperties().put("myString", "foo"); + metadataHandle.getProperties().put("myInteger", 10); + metadataHandle.getProperties().put("myDecimal", 34.56678); + metadataHandle.getProperties().put("myCalendar", Calendar.getInstance().get(Calendar.YEAR)); + metadataHandle.setQuality(23); + return metadataHandle; + } + + @Test + public void testWriteSingleOptimisticLocking() throws Exception { + + String bookFilename = "book.xml"; + String bookURI = "/book-xml-handle/"; + + String docId = bookURI + bookFilename; + GenericDocumentManager docMgr = client.newDocumentManager(); + + writeDocumentUsingInputStreamHandle(client, bookFilename, bookURI, + "XML"); + + // create a descriptor for versions of the document + DocumentDescriptor desc = docMgr.newDescriptor(docId); + + // provide a handle for updating the content of the document + FileHandle updateHandle = new FileHandle(); + + // read the document, capturing the initial version with the descriptor + docMgr.read(desc, updateHandle); + + long descriptorFirstVersion = desc.getVersion(); + System.out.println("created " + docId + " as version " + + descriptorFirstVersion); + + + // modify the document + Document document = expectedXMLDocument(bookFilename); + document.getDocumentElement().setAttribute("modified", "true"); + + // update the document, specifying the current version with the + // descriptor + // if the document changed after reading, write() throws an exception + docMgr.write(desc, updateHandle); + + // get the updated version without getting the content + desc = docMgr.exists(docId); + + long descriptorSecondVersion = desc.getVersion(); + System.out.println("updated " + docId + " as version " + + descriptorSecondVersion); + + // delete the document, specifying the current version with the + // descriptor + // if the document changed after exists(), delete() throws an exception + docMgr.delete(desc); + + // release the client + client.release(); + + assertTrue("The document descriptors are equal. They need to be different.", (descriptorFirstVersion != descriptorSecondVersion)); + } + + @Test + public void testWriteBulkOptimisticLocking() throws Exception { + + String nameId[] = {"property1.xml","property2.xml","property3.xml"}; + String docId[] = {"/opt/lock/property1.xml","/opt/lock/property2.xml","/opt/lock/property3.xml"}; + + TextDocumentManager docMgr = client.newTextDocumentManager(); + + // Write the documents. + DocumentWriteSet writeset = docMgr.newWriteSet(); + // Set meta-data + DocumentMetadataHandle mh = setMetadata(); + + writeset.addDefault(mh); + writeset.add(docId[0], new StringHandle().with("This is so foo 1")); + writeset.add(docId[1], new StringHandle().with("This is so foo 2")); + writeset.add(docId[2], new StringHandle().with("This is so foo 3")); + + docMgr.write(writeset); + + DocumentPage page = docMgr.read(docId); + + DocumentDescriptor[] docDescriptor = new DocumentDescriptor[3]; + long[] descOrigLongArray = new long[10]; + long[] descNewLongArray = new long[10]; + + // provide a handle for updating the content of the document + FileHandle updateHandle = null; + + for(int i=0;i<3;i++) { + + // create a descriptor for versions of the document + docDescriptor[i] = docMgr.newDescriptor(docId[i]); + // provide a handle for updating the content of the document + updateHandle = new FileHandle(); + // read the document, capturing the initial version with the descriptor + docMgr.read(docDescriptor[i], updateHandle); + descOrigLongArray[i] = docDescriptor[i].getVersion(); + + // modify the document + Document document = expectedXMLDocument(nameId[i]); + document.getDocumentElement().setAttribute("modified", "true"); + + + // if the document changed after reading, write() throws an exception + docMgr.write(docDescriptor[i], updateHandle); + + // get the updated version without getting the content + docDescriptor[i] = docMgr.exists(docId[i]); + descNewLongArray[i] = docDescriptor[i].getVersion(); + } + //docMgr.delete(desc); + assertFalse("The document descriptors are equal. They need to be different.",Arrays.equals(descOrigLongArray, descOrigLongArray)); + + // release the client + client.release(); + } + /* + * + * Use UpdatePolicy.VERSION_OPTIONAL and write test methods + * + */ +} \ No newline at end of file From 2f17c1936b90333e54a4c94271a674edccd0fdcd Mon Sep 17 00:00:00 2001 From: sravan Date: Tue, 9 Sep 2014 23:38:07 -0700 Subject: [PATCH 096/357] uncommenting the setup and tear down --- .../javaclient/TestPOJOWithStringQD.java | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithStringQD.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithStringQD.java index 200e88f71..8b36a7de6 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithStringQD.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithStringQD.java @@ -2,13 +2,17 @@ import static org.junit.Assert.*; +import java.io.IOException; + import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; import com.marklogic.client.DatabaseClient; import com.marklogic.client.DatabaseClientFactory; import com.marklogic.client.DatabaseClientFactory.Authentication; @@ -33,15 +37,15 @@ public class TestPOJOWithStringQD extends BasicJavaClientREST { public static void setUpBeforeClass() throws Exception { // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); System.out.println("In setup"); -// setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); -// addRangePathIndex(dbName, "string", "com.marklogic.javaclient.Artifact/name", "http://marklogic.com/collation/", "ignore"); -// addRangePathIndex(dbName, "string", "com.marklogic.javaclient.Artifact/manufacturer/com.marklogic.javaclient.Company/name", "http://marklogic.com/collation/", "ignore"); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + addRangePathIndex(dbName, "string", "com.marklogic.javaclient.Artifact/name", "http://marklogic.com/collation/", "ignore"); + addRangePathIndex(dbName, "string", "com.marklogic.javaclient.Artifact/manufacturer/com.marklogic.javaclient.Company/name", "http://marklogic.com/collation/", "ignore"); } @AfterClass public static void tearDownAfterClass() throws Exception { System.out.println("In tear down" ); -// tearDownJavaRESTServer(dbName, fNames, restServerName); + tearDownJavaRESTServer(dbName, fNames, restServerName); } @Before public void setUp() throws Exception { @@ -211,7 +215,7 @@ public void testPOJOSearchWithJacksonHandle() { assertEquals("total no of pages",10,p.getTotalPages()); } @Test - public void testPOJOSearchWithStringHandle() { + public void testPOJOSearchWithStringHandle() throws JsonProcessingException, IOException { PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); PojoPage p; this.loadSimplePojos(products); @@ -243,6 +247,12 @@ public void testPOJOSearchWithStringHandle() { assertTrue("String handle contains format",results.get().contains("\"format\":\"json\"")); // String expected= jh.get().toString(); // System.out.println(results.get().contains("\"format\":\"json\"")+ expected); + ObjectMapper mapper = new ObjectMapper(); + String expected= "{\"snippet-format\":\"snippet\",\"total\":1,\"start\":1,\"page-length\":50,\"results\":[{\"index\":1,\"uri\":\"com.marklogic.javaclient.Artifact/2.json\",\"path\":\"fn:doc(\\\"com.marklogic.javaclient.Artifact/2.json\\\")\",\"score\":55936,\"confidence\":0.4903799,\"fitness\":0.8035046,\"href\":\"/v1/documents?uri=com.marklogic.javaclient.Artifact%2F2.json\",\"mimetype\":\"application/json\",\"format\":\"json\",\"matches\":[{\"path\":\"fn:doc(\\\"com.marklogic.javaclient.Artifact/2.json\\\")\",\"match-text\":[]}]}],\"qtext\":\"cogs 2\",\"metrics\":{\"query-resolution-time\":\"PT0.004S\",\"snippet-resolution-time\":\"PT0S\",\"total-time\":\"PT0.005S\"}}"; + JsonNode expNode = mapper.readTree(expected).get("results").iterator().next().get("matches"); + JsonNode actNode = mapper.readTree(results.get()).get("results").iterator().next().get("matches"); +// System.out.println(expNode.equals(actNode)+"\n"+ expNode.toString()+"\n"+actNode.toString()); + assertTrue("matching the expected value of search handle",expNode.equals(actNode)); } } From 74c7252db97b9ab96ddd2eb09be534dd430cdb1a Mon Sep 17 00:00:00 2001 From: sammefford Date: Wed, 10 Sep 2014 09:39:37 -0600 Subject: [PATCH 097/357] unifished (not even compiling) beginnings of an eval api --- .../com/marklogic/client/DatabaseClient.java | 2 + .../client/document/DocumentPage.java | 6 +- .../marklogic/client/eval/IteratorPage.java | 25 ++ .../client/eval/JSONVariableSet.java | 31 +++ .../com/marklogic/client/eval/ServerEval.java | 45 +++ .../marklogic/client/eval/VariableSet.java | 22 ++ .../marklogic/client/eval/XMLVariableSet.java | 23 ++ .../com/marklogic/client/http/BodyPart.java | 64 +++++ .../client/http/MultiPartResponsePage.java | 21 ++ .../com/marklogic/client/impl/BasicPage.java | 12 +- .../client/impl/DocumentPageImpl.java | 10 - .../marklogic/client/io/DOMNodeHandle.java | 214 +++++++++++++++ .../client/io/JAXBDatatypeHandle.java | 258 ++++++++++++++++++ .../marklogic/client/io/XQueryTypeHandle.java | 222 +++++++++++++++ .../com/marklogic/client/test/EvalTest.java | 159 +++++++++++ 15 files changed, 1098 insertions(+), 16 deletions(-) create mode 100644 src/main/java/com/marklogic/client/eval/IteratorPage.java create mode 100644 src/main/java/com/marklogic/client/eval/JSONVariableSet.java create mode 100644 src/main/java/com/marklogic/client/eval/ServerEval.java create mode 100644 src/main/java/com/marklogic/client/eval/VariableSet.java create mode 100644 src/main/java/com/marklogic/client/eval/XMLVariableSet.java create mode 100644 src/main/java/com/marklogic/client/http/BodyPart.java create mode 100644 src/main/java/com/marklogic/client/http/MultiPartResponsePage.java create mode 100644 src/main/java/com/marklogic/client/io/DOMNodeHandle.java create mode 100644 src/main/java/com/marklogic/client/io/JAXBDatatypeHandle.java create mode 100644 src/main/java/com/marklogic/client/io/XQueryTypeHandle.java create mode 100644 src/test/java/com/marklogic/client/test/EvalTest.java diff --git a/src/main/java/com/marklogic/client/DatabaseClient.java b/src/main/java/com/marklogic/client/DatabaseClient.java index 7d8551f7f..b7f8a6661 100644 --- a/src/main/java/com/marklogic/client/DatabaseClient.java +++ b/src/main/java/com/marklogic/client/DatabaseClient.java @@ -24,6 +24,7 @@ import com.marklogic.client.document.JSONDocumentManager; import com.marklogic.client.document.TextDocumentManager; import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.eval.ServerEval; import com.marklogic.client.extensions.ResourceManager; import com.marklogic.client.admin.ExtensionMetadata; import com.marklogic.client.query.QueryManager; @@ -177,4 +178,5 @@ public T init(String resourceName, T resourceManager * @return the object implementing communication with the server */ public Object getClientImplementation(); + public ServerEval newServerEval(); } diff --git a/src/main/java/com/marklogic/client/document/DocumentPage.java b/src/main/java/com/marklogic/client/document/DocumentPage.java index 1735d6598..5ebedc26c 100644 --- a/src/main/java/com/marklogic/client/document/DocumentPage.java +++ b/src/main/java/com/marklogic/client/document/DocumentPage.java @@ -18,10 +18,6 @@ import com.marklogic.client.Page; import com.marklogic.client.io.marker.AbstractReadHandle; -public interface DocumentPage extends Iterable, Page { - public boolean hasNext(); - - public DocumentRecord next(); - +public interface DocumentPage extends IteratorPage { public T nextContent(T contentHandle); } diff --git a/src/main/java/com/marklogic/client/eval/IteratorPage.java b/src/main/java/com/marklogic/client/eval/IteratorPage.java new file mode 100644 index 000000000..69a81b20e --- /dev/null +++ b/src/main/java/com/marklogic/client/eval/IteratorPage.java @@ -0,0 +1,25 @@ +/* + * Copyright 2012-2014 MarkLogic Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.marklogic.client; + +import java.util.Iterator; + +public interface IteratorPage extends Iterator, Page { + public boolean hasNext(); + public T next(); +} + + diff --git a/src/main/java/com/marklogic/client/eval/JSONVariableSet.java b/src/main/java/com/marklogic/client/eval/JSONVariableSet.java new file mode 100644 index 000000000..290819cbd --- /dev/null +++ b/src/main/java/com/marklogic/client/eval/JSONVariableSet.java @@ -0,0 +1,31 @@ +/* + * Copyright 2012-2014 MarkLogic Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.marklogic.client.eval; + +public interface JSONVariableSet extends VariableSet { + public JSONVariableSet add(String name, JSONWriteHandle value); + public JSONVariableSet addAs(String name, Object value); + /** Convenience method since boolean is applicable to both JSON and XML + * and therefore cannot be registered with a default handle. */ + public JSONVariableSet add(String name, boolean value); + /** Convenience method since Number is applicable to both JSON and XML + * and therefore cannot be registered with a default handle. */ + public JSONVariableSet add(String name, Number value); + /** Convenience method since dateTime is applicable to both JSON and XML + * and therefore cannot be registered with a default handle. */ + public JSONVariableSet add(String name, java.util.Calendar value); +} + diff --git a/src/main/java/com/marklogic/client/eval/ServerEval.java b/src/main/java/com/marklogic/client/eval/ServerEval.java new file mode 100644 index 000000000..ba9580952 --- /dev/null +++ b/src/main/java/com/marklogic/client/eval/ServerEval.java @@ -0,0 +1,45 @@ +/* + * Copyright 2012-2014 MarkLogic Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.marklogic.client.eval; + +public interface ServerEval { + public T evalXQueryAs(Class responseType, TextWriteHandle xquery, VariableSet vars, + String database, Transaction transaction, TransactionMode mode); + public T evalJavascriptAs(Class responseType, TextWriteHandle xquery, JSONVariableSet vars, + String database, Transaction transaction, TransactionMode mode); + public T evalXQueryAs(Class responseType, String xquery, VariableSet vars, + String database, Transaction transaction, TransactionMode mode); + public T evalJavascriptAs(Class responseType, String xquery, JSONVariableSet vars, + String database, Transaction transaction, TransactionMode mode); + public H evalXQuery(H responseHandle, TextWriteHandle xquery, VariableSet vars, + String database, Transaction transaction, TransactionMode mode); + public H evalJavascript(H responseHandle, TextWriteHandle xquery, JSONVariableSet vars, + String database, Transaction transaction, TransactionMode mode); + public H evalXQuery(H responseHandle, String xquery, VariableSet vars, + String database, Transaction transaction, TransactionMode mode); + public H evalJavascript(H responseHandle, String xquery, JSONVariableSet vars, + String database, Transaction transaction, TransactionMode mode); + public MultiPartResponsePage evalXQuery(TextWriteHandle xquery, VariableSet vars, + String database, Transaction transaction, TransactionMode mode); + public MultiPartResponsePage evalJavascript(TextWriteHandle xquery, JSONVariableSet vars, + String database, Transaction transaction, TransactionMode mode); + public MultiPartResponsePage evalXQuery(String xquery, VariableSet vars, + String database, Transaction transaction, TransactionMode mode); + public MultiPartResponsePage evalJavascript(String xquery, JSONVariableSet vars, + String database, Transaction transaction, TransactionMode mode); + public JSONVariableSet newJSONVariableSet(); + public XMLVariableSet newXMLVariableSet(); +} diff --git a/src/main/java/com/marklogic/client/eval/VariableSet.java b/src/main/java/com/marklogic/client/eval/VariableSet.java new file mode 100644 index 000000000..da96bbc93 --- /dev/null +++ b/src/main/java/com/marklogic/client/eval/VariableSet.java @@ -0,0 +1,22 @@ +/* + * Copyright 2012-2014 MarkLogic Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.marklogic.client.eval; + +import com.marklogic.client.IteratorPage; + +/** Marker interface implemented by {@link XMLVariableSet} and {@link JSONVariableSet} + * so ServerEval can accept either type of VariableSet. */ +public interface VariableSet {} diff --git a/src/main/java/com/marklogic/client/eval/XMLVariableSet.java b/src/main/java/com/marklogic/client/eval/XMLVariableSet.java new file mode 100644 index 000000000..43a467780 --- /dev/null +++ b/src/main/java/com/marklogic/client/eval/XMLVariableSet.java @@ -0,0 +1,23 @@ +/* + * Copyright 2012-2014 MarkLogic Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.marklogic.client.eval; + +public interface XMLVariableSet extends VariableSet { + public XMLVariableSet add(String name, XMLWriteHandle value); + public XMLVariableSet addAs(String name, Object value); +} + + diff --git a/src/main/java/com/marklogic/client/http/BodyPart.java b/src/main/java/com/marklogic/client/http/BodyPart.java new file mode 100644 index 000000000..48d252616 --- /dev/null +++ b/src/main/java/com/marklogic/client/http/BodyPart.java @@ -0,0 +1,64 @@ +/* + * Copyright 2012-2014 MarkLogic Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.marklogic.client.http; + +import java.util.Map; + +public interface BodyPart { + //Map> getHeaders(); + /** + * If this part is JSON, could be any of the following types: + *
+     *   string
+     *   number
+     *   boolean
+     *   array
+     *   object
+     *   null
+     * 
+ * + * Or if this part is XML, could be any of the following types: + *
+     *   xs:anySimpleType,
+     *   xs:base64Binary,
+     *   xs:boolean,
+     *   xs:byte,
+     *   xs:date,
+     *   xs:dateTime,
+     *   xs:dayTimeDuration,
+     *   xs:decimal,
+     *   xs:double,
+     *   xs:duration,
+     *   xs:float,
+     *   xs:int,
+     *   xs:integer,
+     *   xs:long,
+     *   xs:short,
+     *   xs:string,
+     *   xs:time,
+     *   xs:unsignedInt,
+     *   xs:unsignedLong,
+     *   xs:unsignedShort,
+     *   xs:yearMonthDuration.
+     * 
+ */ + //public String getType(); <- what does XDBC return? + // maybe this instead? + // public String getMimeType(); + public H get(H handle); + public T getAs(Class clazz); +} + diff --git a/src/main/java/com/marklogic/client/http/MultiPartResponsePage.java b/src/main/java/com/marklogic/client/http/MultiPartResponsePage.java new file mode 100644 index 000000000..93e892e43 --- /dev/null +++ b/src/main/java/com/marklogic/client/http/MultiPartResponsePage.java @@ -0,0 +1,21 @@ +/* + * Copyright 2012-2014 MarkLogic Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.marklogic.client.http; + +import com.marklogic.client.IteratorPage; + +public interface MultiPartResponsePage extends IteratorPage { +} diff --git a/src/main/java/com/marklogic/client/impl/BasicPage.java b/src/main/java/com/marklogic/client/impl/BasicPage.java index 430f75e1f..c6835155d 100644 --- a/src/main/java/com/marklogic/client/impl/BasicPage.java +++ b/src/main/java/com/marklogic/client/impl/BasicPage.java @@ -18,8 +18,9 @@ import java.util.Iterator; import com.marklogic.client.Page; -public class BasicPage implements Page { +public class BasicPage implements IteratorPage { private Iterable iterable; + private Iterator iterator; private long start; private Long size = null; private long pageSize; @@ -30,6 +31,7 @@ protected BasicPage(Class type) { public BasicPage(Iterable iterable, long start, long pageSize, long totalSize) { this.iterable = iterable; + this.iterator = iterable.iterator(); this.start = start; this.pageSize = pageSize; this.totalSize = totalSize; @@ -39,6 +41,14 @@ public Iterator iterator() { return iterable.iterator(); } + public boolean hasNext() { + return iterator.hasNext(); + } + + public DocumentRecord next() { + return iterator.next(); + } + public long getStart() { return start; } diff --git a/src/main/java/com/marklogic/client/impl/DocumentPageImpl.java b/src/main/java/com/marklogic/client/impl/DocumentPageImpl.java index f4b52dee8..bcdf2256d 100644 --- a/src/main/java/com/marklogic/client/impl/DocumentPageImpl.java +++ b/src/main/java/com/marklogic/client/impl/DocumentPageImpl.java @@ -23,21 +23,11 @@ class DocumentPageImpl extends BasicPage implements DocumentPage { private Iterable iterable; - private Iterator iterator; public DocumentPageImpl(Iterable iterable, long start, long size, long pageSize, long totalSize) { super(iterable, start, pageSize, totalSize); this.setSize(size); this.iterable = iterable; - this.iterator = iterable.iterator(); - } - - public boolean hasNext() { - return iterator.hasNext(); - } - - public DocumentRecord next() { - return iterator.next(); } public T nextContent(T contentHandle) { diff --git a/src/main/java/com/marklogic/client/io/DOMNodeHandle.java b/src/main/java/com/marklogic/client/io/DOMNodeHandle.java new file mode 100644 index 000000000..b68a777b4 --- /dev/null +++ b/src/main/java/com/marklogic/client/io/DOMNodeHandle.java @@ -0,0 +1,214 @@ +/* + * Copyright 2012-2014 MarkLogic Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.marklogic.client.io; + +import org.w3c.dom.Node; +import javax.xml.parsers.DocumentBuilderFactory; + +/** + * Will register all subinterfaces of org.w3c.Node for convenience methods readAs and writeAs. + */ +public class DOMNodeHandle + extends BaseHandle + implements OutputStreamSender, BufferableHandle, ContentHandle, + XMLReadHandle, XMLWriteHandle +{ + private Node content; + private DocumentBuilderFactory factory; + + /** + * Creates a factory to create a DOMHandle instance for a DOM document. + * @return the factory + */ + static public ContentHandleFactory newFactory() { + return new ContentHandleFactory() { + @Override + public Class[] getHandledClasses() { + return null; + } + @Override + public boolean isHandled(Class type) { + return null; + } + @Override + public ContentHandle newHandle(Class type) { + return null; + } + }; + } + + public DOMNodeHandle() { + super(); + setResendable(true); + } + + /** + * Initializes the handle with a DOM Node for the content. + * @param content a DOM Node + */ + public DOMNodeHandle(Node content) { + this(); + set(content); + } + + /** + * Returns the DOM Node for the content. + * @return the DOM Node + */ + @Override + public Document get() { + return content; + } + /** + * Assigns a DOM Node document as the content. + * @param content a DOM Node + */ + @Override + public void set(Document content) { + this.content = content; + } + + @Override + public void fromBuffer(byte[] buffer) { + if (buffer == null || buffer.length == 0) + content = null; + else + receiveContent(new ByteArrayInputStream(buffer)); + } + @Override + public byte[] toBuffer() { + try { + if (content == null) + return null; + + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + write(buffer); + + return buffer.toByteArray(); + } catch (IOException e) { + throw new MarkLogicIOException(e); + } + } + + /** + * Returns the DOM Node as a string. + */ + @Override + public String toString() { + try { + return new String(toBuffer(),"UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new MarkLogicIOException(e); + } + } + /** + * Returns the factory for building DOM documents. + * @return the document factory + */ + public DocumentBuilderFactory getFactory() throws ParserConfigurationException { + if (factory == null) + factory = makeDocumentBuilderFactory(); + return factory; + } + /** + * Specifies the factory for building DOM documents. + * @param factory the document factory + */ + public void setFactory(DocumentBuilderFactory factory) { + this.factory = factory; + } + protected DocumentBuilderFactory makeDocumentBuilderFactory() throws ParserConfigurationException { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + factory.setValidating(false); + // TODO: XInclude + + return factory; + } + @Override + protected void receiveContent(InputStream content) { + if (content == null) { + this.content = null; + return; + } + + try { + if (logger.isInfoEnabled()) + logger.info("Parsing DOM document from input stream"); + + DocumentBuilderFactory factory = getFactory(); + if (factory == null) { + throw new MarkLogicInternalException("Failed to make DOM document builder factory"); + } + + DOMImplementationLS domImpl = (DOMImplementationLS) factory.newDocumentBuilder().getDOMImplementation(); + + LSParser parser = domImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); + if (resolver != null) { + parser.getDomConfig().setParameter("resource-resolver", resolver); + } + + LSInput domInput = domImpl.createLSInput(); + domInput.setEncoding("UTF-8"); + domInput.setByteStream(content); + + this.content = parser.parse(domInput); + content.close(); + } catch (IOException e) { + logger.error("Failed to parse DOM document from input stream",e); + throw new MarkLogicInternalException(e); + } catch (ParserConfigurationException e) { + logger.error("Failed to parse DOM document from input stream",e); + throw new MarkLogicInternalException(e); + } + } + @Override + protected OutputStreamSender sendContent() { + if (content == null) { + throw new IllegalStateException("No document to write"); + } + + return this; + } + @Override + public void write(OutputStream out) throws IOException { + try { + if (logger.isInfoEnabled()) + logger.info("Serializing DOM document to output stream"); + + DocumentBuilderFactory factory = getFactory(); + if (factory == null) { + throw new MarkLogicInternalException("Failed to make DOM document builder factory"); + } + + DOMImplementationLS domImpl = (DOMImplementationLS) factory.newDocumentBuilder().getDOMImplementation(); + LSOutput domOutput = domImpl.createLSOutput(); + domOutput.setEncoding("UTF-8"); + domOutput.setByteStream(out); + domImpl.createLSSerializer().write(content, domOutput); + } catch (DOMException e) { + logger.error("Failed to serialize DOM document to output stream",e); + throw new MarkLogicInternalException(e); + } catch (LSException e) { + logger.error("Failed to serialize DOM document to output stream",e); + throw new MarkLogicInternalException(e); + } catch (ParserConfigurationException e) { + logger.error("Failed to serialize DOM document to output stream",e); + throw new MarkLogicInternalException(e); + } + } +} + diff --git a/src/main/java/com/marklogic/client/io/JAXBDatatypeHandle.java b/src/main/java/com/marklogic/client/io/JAXBDatatypeHandle.java new file mode 100644 index 000000000..27376f38f --- /dev/null +++ b/src/main/java/com/marklogic/client/io/JAXBDatatypeHandle.java @@ -0,0 +1,258 @@ +/* + * Copyright 2012-2014 MarkLogic Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.marklogic.client.io; + +import org.w3c.dom.Node; +import javax.xml.parsers.DocumentBuilderFactory; + +/** + * Supports get/set of any type handled by javax.xml.bind.DatatypeConverter or for time- + * related types, javax.xml.datatype.DatatypeFactory. + */ +public class JAXBDatatypeHandle + extends BaseHandle + implements OutputStreamSender, BufferableHandle, ContentHandle, + XMLReadHandle, XMLWriteHandle +{ + private Node content; + private DocumentBuilderFactory factory; + + /** + * Creates a factory to create a DOMHandle instance for a DOM document. + * @return the factory + */ + static public ContentHandleFactory newFactory() { + return new ContentHandleFactory() { + @Override + public Class[] getHandledClasses() { + return null; + } + @Override + public boolean isHandled(Class type) { + return null; + } + @Override + public ContentHandle newHandle(Class type) { + return null; + } + }; + } + + public JAXBDatatypeHandle(Class clazz) { + super(); + setResendable(true); + } + + /** + * Initializes the handle with the content. + * @param xsType the localname part of the XML Schema type. The type must be one of: + *
+     *   anySimpleType
+     *   base64Binary
+     *   boolean
+     *   byte
+     *   date
+     *   dateTime
+     *   dayTimeDuration
+     *   decimal
+     *   double
+     *   duration
+     *   float
+     *   hexBinary
+     *   int
+     *   integer
+     *   long
+     *   QName
+     *   short
+     *   string
+     *   time
+     *   unsignedInt
+     *   unsignedLong
+     *   unsignedShort
+     *   yearMonthDuration
+     *   
+     * @param content the java object.  The type must be one of:
+     *   
+     *   String
+     *   byte[]
+     *   boolean
+     *   byte
+     *   double
+     *   float
+     *   int
+     *   BigInteger
+     *   long
+     *   javax.xml.namespace.QName
+     *   short
+     *   Calendar
+     *   javax.xml.datatype.Duration
+     *   javax.xml.datatype.XMLGregorianCalendar
+     *   
+ */ + public JAXBDatatypeHandle(QName xsType, T content) { + this(); + set(content); + } + + /** + * Returns the DOM Node for the content. + * @return the DOM Node + */ + @Override + public T get() { + return content; + } + /** + * Assigns a DOM Node document as the content. + * @param content a DOM Node + */ + @Override + public void set(T content) { + this.content = content; + } + + @Override + public void fromBuffer(byte[] buffer) { + if (buffer == null || buffer.length == 0) + content = null; + else + receiveContent(new ByteArrayInputStream(buffer)); + } + @Override + public byte[] toBuffer() { + try { + if (content == null) + return null; + + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + write(buffer); + + return buffer.toByteArray(); + } catch (IOException e) { + throw new MarkLogicIOException(e); + } + } + + /** + * Returns the DOM Node as a string. + */ + @Override + public String toString() { + try { + return new String(toBuffer(),"UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new MarkLogicIOException(e); + } + } + /** + * Returns the factory for building DOM documents. + * @return the document factory + */ + public DocumentBuilderFactory getFactory() throws ParserConfigurationException { + if (factory == null) + factory = makeDocumentBuilderFactory(); + return factory; + } + /** + * Specifies the factory for building DOM documents. + * @param factory the document factory + */ + public void setFactory(DocumentBuilderFactory factory) { + this.factory = factory; + } + protected DocumentBuilderFactory makeDocumentBuilderFactory() throws ParserConfigurationException { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + factory.setValidating(false); + // TODO: XInclude + + return factory; + } + @Override + protected void receiveContent(InputStream content) { + if (content == null) { + this.content = null; + return; + } + + try { + if (logger.isInfoEnabled()) + logger.info("Parsing DOM document from input stream"); + + DocumentBuilderFactory factory = getFactory(); + if (factory == null) { + throw new MarkLogicInternalException("Failed to make DOM document builder factory"); + } + + DOMImplementationLS domImpl = (DOMImplementationLS) factory.newDocumentBuilder().getDOMImplementation(); + + LSParser parser = domImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); + if (resolver != null) { + parser.getDomConfig().setParameter("resource-resolver", resolver); + } + + LSInput domInput = domImpl.createLSInput(); + domInput.setEncoding("UTF-8"); + domInput.setByteStream(content); + + this.content = parser.parse(domInput); + content.close(); + } catch (IOException e) { + logger.error("Failed to parse DOM document from input stream",e); + throw new MarkLogicInternalException(e); + } catch (ParserConfigurationException e) { + logger.error("Failed to parse DOM document from input stream",e); + throw new MarkLogicInternalException(e); + } + } + @Override + protected OutputStreamSender sendContent() { + if (content == null) { + throw new IllegalStateException("No document to write"); + } + + return this; + } + @Override + public void write(OutputStream out) throws IOException { + try { + if (logger.isInfoEnabled()) + logger.info("Serializing DOM document to output stream"); + + DocumentBuilderFactory factory = getFactory(); + if (factory == null) { + throw new MarkLogicInternalException("Failed to make DOM document builder factory"); + } + + DOMImplementationLS domImpl = (DOMImplementationLS) factory.newDocumentBuilder().getDOMImplementation(); + LSOutput domOutput = domImpl.createLSOutput(); + domOutput.setEncoding("UTF-8"); + domOutput.setByteStream(out); + domImpl.createLSSerializer().write(content, domOutput); + } catch (DOMException e) { + logger.error("Failed to serialize DOM document to output stream",e); + throw new MarkLogicInternalException(e); + } catch (LSException e) { + logger.error("Failed to serialize DOM document to output stream",e); + throw new MarkLogicInternalException(e); + } catch (ParserConfigurationException e) { + logger.error("Failed to serialize DOM document to output stream",e); + throw new MarkLogicInternalException(e); + } + } +} + + diff --git a/src/main/java/com/marklogic/client/io/XQueryTypeHandle.java b/src/main/java/com/marklogic/client/io/XQueryTypeHandle.java new file mode 100644 index 000000000..441f6a632 --- /dev/null +++ b/src/main/java/com/marklogic/client/io/XQueryTypeHandle.java @@ -0,0 +1,222 @@ +/* + * Copyright 2012-2014 MarkLogic Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.marklogic.client.io; + +/** + * Supports get/set of any type necessary for ServerEval but not handled by JAXBDatatypeHandle or DomNodeHandle. + * For now the only two types are java.net.URI and javax.xml.namespace.QName. + */ +public class XQueryTypeHandle + extends BaseHandle + implements OutputStreamSender, BufferableHandle, ContentHandle, + XMLReadHandle, XMLWriteHandle +{ + private T content; + + /** + * Creates a factory to create a DOMHandle instance for a DOM document. + * @return the factory + */ + static public ContentHandleFactory newFactory() { + return new ContentHandleFactory() { + @Override + public Class[] getHandledClasses() { + return null; + } + @Override + public boolean isHandled(Class type) { + return null; + } + @Override + public ContentHandle newHandle(Class type) { + return null; + } + }; + } + + public XQueryTypeHandle(Class clazz) { + super(); + setResendable(true); + } + + /** + * Initializes the handle with the content. + * @param xsType the localname part of the XML Schema type. The type must be one of: + *
+     *   Sequence
+     *   xs:anyURI
+     *   
+     * @param content the java object.  The type must be one of:
+     *   
+     *   java.util.List
+     *   java.net.URI
+     *   
+ */ + public XQueryTypeHandle(QName xsType, T content) { + this(); + set(content); + } + + /** + * Returns the DOM Node for the content. + * @return the DOM Node + */ + @Override + public T get() { + return content; + } + /** + * Assigns a DOM Node document as the content. + * @param content a DOM Node + */ + @Override + public void set(T content) { + this.content = content; + } + + @Override + public void fromBuffer(byte[] buffer) { + if (buffer == null || buffer.length == 0) + content = null; + else + receiveContent(new ByteArrayInputStream(buffer)); + } + @Override + public byte[] toBuffer() { + try { + if (content == null) + return null; + + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + write(buffer); + + return buffer.toByteArray(); + } catch (IOException e) { + throw new MarkLogicIOException(e); + } + } + + /** + * Returns the DOM Node as a string. + */ + @Override + public String toString() { + try { + return new String(toBuffer(),"UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new MarkLogicIOException(e); + } + } + /** + * Returns the factory for building DOM documents. + * @return the document factory + */ + public DocumentBuilderFactory getFactory() throws ParserConfigurationException { + if (factory == null) + factory = makeDocumentBuilderFactory(); + return factory; + } + /** + * Specifies the factory for building DOM documents. + * @param factory the document factory + */ + public void setFactory(DocumentBuilderFactory factory) { + this.factory = factory; + } + protected DocumentBuilderFactory makeDocumentBuilderFactory() throws ParserConfigurationException { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + factory.setValidating(false); + // TODO: XInclude + + return factory; + } + @Override + protected void receiveContent(InputStream content) { + if (content == null) { + this.content = null; + return; + } + + try { + if (logger.isInfoEnabled()) + logger.info("Parsing DOM document from input stream"); + + DocumentBuilderFactory factory = getFactory(); + if (factory == null) { + throw new MarkLogicInternalException("Failed to make DOM document builder factory"); + } + + DOMImplementationLS domImpl = (DOMImplementationLS) factory.newDocumentBuilder().getDOMImplementation(); + + LSParser parser = domImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); + if (resolver != null) { + parser.getDomConfig().setParameter("resource-resolver", resolver); + } + + LSInput domInput = domImpl.createLSInput(); + domInput.setEncoding("UTF-8"); + domInput.setByteStream(content); + + this.content = parser.parse(domInput); + content.close(); + } catch (IOException e) { + logger.error("Failed to parse DOM document from input stream",e); + throw new MarkLogicInternalException(e); + } catch (ParserConfigurationException e) { + logger.error("Failed to parse DOM document from input stream",e); + throw new MarkLogicInternalException(e); + } + } + @Override + protected OutputStreamSender sendContent() { + if (content == null) { + throw new IllegalStateException("No document to write"); + } + + return this; + } + @Override + public void write(OutputStream out) throws IOException { + try { + if (logger.isInfoEnabled()) + logger.info("Serializing DOM document to output stream"); + + DocumentBuilderFactory factory = getFactory(); + if (factory == null) { + throw new MarkLogicInternalException("Failed to make DOM document builder factory"); + } + + DOMImplementationLS domImpl = (DOMImplementationLS) factory.newDocumentBuilder().getDOMImplementation(); + LSOutput domOutput = domImpl.createLSOutput(); + domOutput.setEncoding("UTF-8"); + domOutput.setByteStream(out); + domImpl.createLSSerializer().write(content, domOutput); + } catch (DOMException e) { + logger.error("Failed to serialize DOM document to output stream",e); + throw new MarkLogicInternalException(e); + } catch (LSException e) { + logger.error("Failed to serialize DOM document to output stream",e); + throw new MarkLogicInternalException(e); + } catch (ParserConfigurationException e) { + logger.error("Failed to serialize DOM document to output stream",e); + throw new MarkLogicInternalException(e); + } + } +} + + + diff --git a/src/test/java/com/marklogic/client/test/EvalTest.java b/src/test/java/com/marklogic/client/test/EvalTest.java new file mode 100644 index 000000000..ce5f424b6 --- /dev/null +++ b/src/test/java/com/marklogic/client/test/EvalTest.java @@ -0,0 +1,159 @@ +/* + * Copyright 2012-2014 MarkLogic Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.marklogic.client.test; + +import java.util.Calendar; +import java.util.GregorianCalendar; +import java.util.SimpleTimeZone; + +import javax.xml.parsers.DocumentBuilderFactory; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import org.junit.FixMethodOrder; +import org.junit.runners.MethodSorters; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.marklogic.client.eval.JSONVariableSet; +import com.marklogic.client.eval.ServerEval; +import com.marklogic.client.eval.VariableSet; +import com.marklogic.client.io.JacksonHandle; +import com.marklogic.client.io.StringHandle; + + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class EvalTest { + @BeforeClass + public static void beforeClass() throws JAXBException { + Common.connect(); + //System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + } + @AfterClass + public static void afterClass() { + Common.release(); + } + + @Test + public void evalTest1 { + // hello world and response determined by implicit StringHandle which registered with String.class + String xquery = "'hello world'"; + ServerEval eval = Common.client.newServerEval(); + String response = eval.evalXQueryAs(String.class, xquery, null, null, null, TransactionMode.QUERY); + assertEquals("Return should be 'hello world'", "hello world", response); + + // hello world with a variable and response explicit set to StringHandle + xquery = "declare variable $planet external;" + + "'hello world from ' || $planet"; + VariableSet vars = eval.newXMLVariableSet().addAs("planet", "Mars"); + StringHandle strResponse = eval.evalXQuery(xquery, vars, new StringHandle(), null, null, TransactionMode.QUERY); + assertEquals("Return should be 'hello world from Mars'", "hello world from Mars", strResponse.get()); + + // accept and return each JSON variable type so use MultiPartResponsePage + String xquery = "declare variable $myString as xs:string external;" + + "declare variable $myArray as json:array external;" + + "declare variable $myObject as json:object external;" + + "declare variable $myBool as xs:boolean external;" + + "declare variable $myInteger as xs:integer external;" + + "declare variable $myDouble as xs:double external;" + + "declare variable $myDate as xs:date external;" + + "$myString, $myArray, $myObject, $myBool, $myInteger, $myDouble, $myDate"; + Calendar septFirst = new GregorianCalendar(2014, Calendar.SEPTEMBER, 1); + septFirst.setTimeZone(new SimpleTimeZone(0, "UTC")); + JSONVariableSet jsonVars = eval.newJSONVariableSet() + // String is mapped to implicitly use StringHandle + .addAs("myString", "Mars") + // ArrayNode extends JSONNode which is mapped to implicitly use JacksonHandle + .addAs("myArray", new ObjectMapper().createArrayNode().add("item1").add("item2")) + // ObjectNode extends JSONNode which is mapped to implicitly use JacksonHandle + .addAs("myObject", new ObjectMapper().createObjectNode().put("item1", "value1")) + // the rest use built-in methods of JSONVariableSet + .add ("myBool", true) + .add ("myInteger", 123) + .add ("myDouble", 1.1) + .add ("myDate", septFirst); + MultiPartResponsePage responses = eval.evalXQuery(xquery, jsonVars, null, null, TransactionMode.QUERY); + assertEquals("myString should = 'Mars'", "Mars", responses.next().getAs(String.class)); + assertEquals("myArray should = [\"item1\",\"item2\"]", "[\"item1\",\"item2\"]", + responses.next().getAs(JsonNode.class)); + assertEquals("myObject should = {\"item1\"=\"value1\"}", "{\"item1\"=\"value1\"}", + responses.next().getAs(JsonNode.class)); + JacksonHandle myBool = responses.next().get(new JacksonHandle()); + assertEquals("myBool should = true", true, myBool.get().asBoolean()); + assertEquals("myInteger should = 123", 123, responses.next().get(new JacksonHandle()).get().asInt()); + assertEquals("myDouble should = 1.1", 1.1, responses.next().get(new JacksonHandle()).get().asDouble()); + // the format what we get from javax.xml.datatype.XMLGregorianCalendar.toString() + assertEquals("myDate should = '2014-09-01T00:00:00'", "2014-09-01T00:00:00", + responses.next().get(new JAXBDatatypeHandle()).get()); + + + // accept and return each XML variable type so use MultiPartResponsePage + String xquery = "declare variable $myString as xs:string external;" + + "declare variable $myBinary as binary() external;" + + "declare variable $myComment as comment() external;" + + "declare variable $myDocument as document() external;" + + "declare variable $myDuration as xs:duration external;" + + "declare variable $myElement as element() external;" + + "declare variable $myNode as node() external;" + + "declare variable $myProcessingInstruction as processingInstruction() external;" + + "declare variable $myText as text() external;" + + "declare variable $myComment as comment() external;" + + "declare variable $myComment as comment() external;" + + "declare variable $myComment as comment() external;" + + "declare variable $myComment as comment() external;" + + "declare variable $myComment as comment() external;" + + "declare variable $myBool as xs:boolean external;" + + "declare variable $myInteger as xs:integer external;" + + "declare variable $myDouble as xs:double external;" + + "declare variable $myDate as xs:date external;" + + "$myString, $myArray, $myObject, $myBool, $myInteger, $myDouble, $myDate"; + Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); + XMLVariableSet jsonVars = eval.newXMLVariableSet() + // String is mapped to implicitly use StringHandle + .addAs("myString", "Mars") + .addAs("myBinary", byte[] { 1, 2, 3}) + .addAs("myComment", document.createComment("")) + .addAs("myDocument", document) + .add ("myDuration", new JAXBDatatypeHandle("xs:dayTimeDuration", "PT0H")) + .addAs("myElement", document.createElement("http://marklogic.com/appservices/search", "options")) + // ArrayNode extends XMLNode which is mapped to implicitly use JacksonHandle + .addAs("myArray", new JacksonHandle().getMapper().createArrayNode().add("item1").add("item2")) + // ObjectNode extends XMLNode which is mapped to implicitly use JacksonHandle + .addAs("myObject", new JacksonHandle().getMapper().createObjectNode().put("item1", "value1")) + // the rest use built-in methods of XMLVariableSet + .add ("myBool", true) + .add ("myInteger", 123) + .add ("myDouble", 1.1) + .add ("myDate", new GregorianCalendar(2014, Calendar.SEPTEMBER, 1)); + MultiPartResponsePage responses = eval.evalXQuery(xquery, jsonVars, null, null, TransactionMode.QUERY); + assertEquals("myString should = 'Mars'", "Mars", responses.next().getAs(String.class)); + assertEquals("myArray should = [\"item1\",\"item2\"]", "[\"item1\",\"item2\"]", + responses.next().getAs(JsonNode.class)); + assertEquals("myObject should = {\"item1\"=\"value1\"}", "{\"item1\"=\"value1\"}", + responses.next().getAs(JsonNode.class)); + JacksonHandle myBool = responses.next().get(new JacksonHandle()); + assertEquals("myBool should = true", true, myBool.get().asBoolean()); + assertEquals("myInteger should = 123", 123, responses.next().get(new JacksonHandle()).get().asInt()); + assertEquals("myDouble should = 1.1", 1.1, responses.next().get(new JacksonHandle()).get().asDouble()); + // the format what we get from java.util.Calendar.toString() + assertEquals("myDate should = 'Mon Sep 01 00:00:00 UDT 2014'", "Mon Sep 01 00:00:00 UDT 2014", + responses.next().get(new JAXBDatatypeHandle()).get()); + } + +} From ef911315982e6a29ef7c752f0f5e34433513c50d Mon Sep 17 00:00:00 2001 From: sravan Date: Wed, 10 Sep 2014 16:47:41 -0700 Subject: [PATCH 098/357] updated and added new scenarios --- .../TestPOJOReadWriteWithTransactions.java | 464 ++++++++++-------- .../javaclient/TestPOJOWithStringQD.java | 43 +- 2 files changed, 304 insertions(+), 203 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOReadWriteWithTransactions.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOReadWriteWithTransactions.java index 8bbf28acc..4432f3e01 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOReadWriteWithTransactions.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOReadWriteWithTransactions.java @@ -14,8 +14,11 @@ import com.marklogic.client.DatabaseClientFactory; import com.marklogic.client.Transaction; import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.SearchHandle; import com.marklogic.client.pojo.PojoPage; import com.marklogic.client.pojo.PojoRepository; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.StringQueryDefinition; public class TestPOJOReadWriteWithTransactions extends BasicJavaClientREST{ @@ -27,7 +30,7 @@ public class TestPOJOReadWriteWithTransactions extends BasicJavaClientREST{ @BeforeClass public static void setUpBeforeClass() throws Exception { - System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); +// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); System.out.println("In setup"); setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); } @@ -63,35 +66,35 @@ public Artifact getArtifact(int counter){ return cogs; } - public void validateArtifact(Artifact art) - { - assertNotNull("Artifact object should never be Null",art); - assertNotNull("Id should never be Null",art.id); - assertTrue("Inventry is always greater than 1000", art.getInventory()>1000); - } - //This test is to persist a simple design model objects in ML, read from ML, delete all + public void validateArtifact(Artifact art) + { + assertNotNull("Artifact object should never be Null",art); + assertNotNull("Id should never be Null",art.id); + assertTrue("Inventry is always greater than 1000", art.getInventory()>1000); + } + //This test is to persist a simple design model objects in ML, read from ML, delete all // Issue 104 for unable to have transaction in count,exists, delete methods - @Test + @Test public void testPOJOWriteWithTransaction() throws Exception { PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); Transaction t= client.openTransaction(); //Load more than 100 objects try{ - for(int i=1;i<112;i++){ - products.write(this.getArtifact(i),t); - } -// assertEquals("Total number of object recods",111, products.count()); - for(long i=1;i<112;i++){ -// assertTrue("Product id "+i+" does not exist",products.exists(i)); - this.validateArtifact(products.read(i,t)); - } - + for(int i=1;i<112;i++){ + products.write(this.getArtifact(i),t); + } + // assertEquals("Total number of object recods",111, products.count()); + for(long i=1;i<112;i++){ + // assertTrue("Product id "+i+" does not exist",products.exists(i)); + this.validateArtifact(products.read(i,t)); + } + }catch(Exception e){ throw e; }finally{ t.rollback(); } - + for(long i=1;i<112;i++){ assertFalse("Product id exists ?",products.exists(i)); } @@ -103,14 +106,14 @@ public void testPOJOWriteWithTransCollection() throws Exception { //Load more than 110 objects into different collections Transaction t= client.openTransaction(); try{ - for(int i=112;i<222;i++){ - if(i%2==0){ - products.write(this.getArtifact(i),t,"even","numbers"); - } - else { - products.write(this.getArtifact(i),t,"odd","numbers"); + for(int i=112;i<222;i++){ + if(i%2==0){ + products.write(this.getArtifact(i),t,"even","numbers"); + } + else { + products.write(this.getArtifact(i),t,"odd","numbers"); + } } - } }catch(Exception e){throw e;} finally{t.commit();} assertEquals("Total number of object recods",110, products.count("numbers")); @@ -121,8 +124,8 @@ public void testPOJOWriteWithTransCollection() throws Exception { assertTrue("Product id "+i+" does not exist",products.exists(i)); this.validateArtifact(products.read(i)); } -// t= client.openTransaction(); - + // t= client.openTransaction(); + products.delete((long)112); assertFalse("Product id 112 exists ?",products.exists((long)112)); products.deleteAll(); @@ -135,112 +138,42 @@ public void testPOJOWriteWithTransCollection() throws Exception { products.deleteAll(); } //This test is to read objects into pojo page based on Ids - // until #103 is resolved @Test - public void testPOJOWriteWithPojoPage() { - PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); - //Load more than 110 objects into different collections - products.deleteAll(); - Long[] ids= new Long[111]; - int j=0; - for(int i=222;i<333;i++){ - ids[j] =(long) i;j++; - if(i%2==0){ - products.write(this.getArtifact(i),"even","numbers"); - } - else { - products.write(this.getArtifact(i),"odd","numbers"); - } - } - assertEquals("Total number of object recods",111, products.count("numbers")); - assertEquals("Collection even count",56,products.count("even")); - assertEquals("Collection odd count",55,products.count("odd")); - - System.out.println("Default Page length setting on docMgr :"+products.getPageLength()); - assertEquals("Default setting for page length",50,products.getPageLength()); - products.setPageLength(100); -// assertEquals("explicit setting for page length",1,products.getPageLength()); - PojoPage p= products.read(ids); - // test for page methods - //Issue- assertEquals("Number of records",1,p.size()); - System.out.println("Page size"+p.size()); -// assertEquals("Starting record in first page ",1,p.getStart()); - System.out.println("Starting record in first page "+p.getStart()); - -// assertEquals("Total number of estimated results:",111,p.getTotalSize()); - System.out.println("Total number of estimated results:"+p.getTotalSize()); -// assertEquals("Total number of estimated pages :",111,p.getTotalPages()); - System.out.println("Total number of estimated pages :"+p.getTotalPages()); - assertFalse("Is this First page :",p.isFirstPage());//this is bug - assertFalse("Is this Last page :",p.isLastPage()); - assertTrue("Is this First page has content:",p.hasContent()); - // Need the Issue #75 to be fixed - assertTrue("Is first page has previous page ?",p.hasPreviousPage()); - long pageNo=1,count=0; - do{ - count=0; - if(pageNo >1){ - assertFalse("Is this first Page", p.isFirstPage()); - assertTrue("Is page has previous page ?",p.hasPreviousPage()); - } - Iterator itr = p.iterator(); - while(itr.hasNext()){ - this.validateArtifact(p.iterator().next()); - count++; - } -// assertEquals("document count", p.size(),count); - System.out.println("Is this Last page :"+p.hasContent()+p.isLastPage()); - pageNo = pageNo + p.getPageSize(); - }while((p.isLastPage()) && p.hasContent()); - assertTrue("page count is 111 ",pageNo == p.getTotalPages()); - assertTrue("Page has previous page ?",p.hasPreviousPage()); - assertEquals("page size", 1,p.getPageSize()); - assertEquals("document count", 111,p.getTotalSize()); - assertFalse("Page has any records ?",p.hasContent()); - - - products.deleteAll(); - //see any document exists - for(long i=112;i<222;i++){ - assertFalse("Product id "+i+" exists ?",products.exists(i)); - } - //see if it complains when there are no records - } - @Test - public void testPOJOWriteWithPojoPageReadAll() throws Exception { + // until #103 is resolved @Test + public void testPOJOWriteWithPojoPage() { PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); //Load more than 110 objects into different collections - Transaction t= client.openTransaction(); - PojoPage p; - try{ + products.deleteAll(); + Long[] ids= new Long[111]; + int j=0; for(int i=222;i<333;i++){ - if(i%2==0){ - products.write(this.getArtifact(i),t,"even","numbers"); + ids[j] =(long) i;j++; + if(i%2==0){ + products.write(this.getArtifact(i),"even","numbers"); } - else { - products.write(this.getArtifact(i),t,"odd","numbers"); + else { + products.write(this.getArtifact(i),"odd","numbers"); } } - //issue# 104 -// assertEquals("Total number of object recods",111, products.count("numbers")); -// assertEquals("Collection even count",56,products.count("even")); -// assertEquals("Collection odd count",55,products.count("odd")); - + assertEquals("Total number of object recods",111, products.count("numbers")); + assertEquals("Collection even count",56,products.count("even")); + assertEquals("Collection odd count",55,products.count("odd")); + System.out.println("Default Page length setting on docMgr :"+products.getPageLength()); assertEquals("Default setting for page length",50,products.getPageLength()); - products.setPageLength(25); - assertEquals("explicit setting for page length",25,products.getPageLength()); - p= products.readAll(1,t); + products.setPageLength(100); + // assertEquals("explicit setting for page length",1,products.getPageLength()); + PojoPage p= products.read(ids); // test for page methods - assertEquals("Number of records",25,p.size()); + //Issue- assertEquals("Number of records",1,p.size()); System.out.println("Page size"+p.size()); - assertEquals("Starting record in first page ",1,p.getStart()); + // assertEquals("Starting record in first page ",1,p.getStart()); System.out.println("Starting record in first page "+p.getStart()); - - assertEquals("Total number of estimated results:",111,p.getTotalSize()); + + // assertEquals("Total number of estimated results:",111,p.getTotalSize()); System.out.println("Total number of estimated results:"+p.getTotalSize()); - assertEquals("Total number of estimated pages :",5,p.getTotalPages()); + // assertEquals("Total number of estimated pages :",111,p.getTotalPages()); System.out.println("Total number of estimated pages :"+p.getTotalPages()); - assertTrue("Is this First page :",p.isFirstPage()); + assertFalse("Is this First page :",p.isFirstPage());//this is bug assertFalse("Is this Last page :",p.isLastPage()); assertTrue("Is this First page has content:",p.hasContent()); // Need the Issue #75 to be fixed @@ -248,25 +181,97 @@ public void testPOJOWriteWithPojoPageReadAll() throws Exception { long pageNo=1,count=0; do{ count=0; - p= products.readAll(pageNo,t); - if(pageNo >1){ assertFalse("Is this first Page", p.isFirstPage()); assertTrue("Is page has previous page ?",p.hasPreviousPage()); } - - while(p.iterator().hasNext()){ - this.validateArtifact(p.iterator().next()); - count++; - } - assertEquals("document count", p.size(),count); - - pageNo = pageNo + p.getPageSize(); - }while(!(p.isLastPage()) && pageNo < p.getTotalSize()); - + Iterator itr = p.iterator(); + while(itr.hasNext()){ + this.validateArtifact(p.iterator().next()); + count++; + } + // assertEquals("document count", p.size(),count); + System.out.println("Is this Last page :"+p.hasContent()+p.isLastPage()); + pageNo = pageNo + p.getPageSize(); + }while((p.isLastPage()) && p.hasContent()); + assertTrue("page count is 111 ",pageNo == p.getTotalPages()); assertTrue("Page has previous page ?",p.hasPreviousPage()); - assertEquals("page size", 11,p.size()); + assertEquals("page size", 1,p.getPageSize()); assertEquals("document count", 111,p.getTotalSize()); + assertFalse("Page has any records ?",p.hasContent()); + + + products.deleteAll(); + //see any document exists + for(long i=112;i<222;i++){ + assertFalse("Product id "+i+" exists ?",products.exists(i)); + } + //see if it complains when there are no records + } + @Test + public void testPOJOWriteWithPojoPageReadAll() throws Exception { + + PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); + //Load more than 110 objects into different collections + products.deleteAll(); + Transaction t= client.openTransaction(); + PojoPage p; + try{ + for(int i=222;i<333;i++){ + if(i%2==0){ + products.write(this.getArtifact(i),t,"even","numbers"); + } + else { + products.write(this.getArtifact(i),t,"odd","numbers"); + } + } + //issue# 104 + // assertEquals("Total number of object recods",111, products.count("numbers")); + // assertEquals("Collection even count",56,products.count("even")); + // assertEquals("Collection odd count",55,products.count("odd")); + + System.out.println("Default Page length setting on docMgr :"+products.getPageLength()); + assertEquals("Default setting for page length",50,products.getPageLength()); + products.setPageLength(25); + assertEquals("explicit setting for page length",25,products.getPageLength()); + p= products.readAll(1,t); + // test for page methods + assertEquals("Number of records",25,p.size()); + System.out.println("Page size"+p.size()); + assertEquals("Starting record in first page ",1,p.getStart()); + System.out.println("Starting record in first page "+p.getStart()); + + assertEquals("Total number of estimated results:",111,p.getTotalSize()); + System.out.println("Total number of estimated results:"+p.getTotalSize()); + assertEquals("Total number of estimated pages :",5,p.getTotalPages()); + System.out.println("Total number of estimated pages :"+p.getTotalPages()); + assertTrue("Is this First page :",p.isFirstPage()); + assertFalse("Is this Last page :",p.isLastPage()); + assertTrue("Is this First page has content:",p.hasContent()); + // Need the Issue #75 to be fixed + assertTrue("Is first page has previous page ?",p.hasPreviousPage()); + long pageNo=1,count=0; + do{ + count=0; + p= products.readAll(pageNo,t); + + if(pageNo >1){ + assertFalse("Is this first Page", p.isFirstPage()); + assertTrue("Is page has previous page ?",p.hasPreviousPage()); + } + + while(p.iterator().hasNext()){ + this.validateArtifact(p.iterator().next()); + count++; + } + assertEquals("document count", p.size(),count); + + pageNo = pageNo + p.getPageSize(); + }while(!(p.isLastPage()) && pageNo < p.getTotalSize()); + + assertTrue("Page has previous page ?",p.hasPreviousPage()); + assertEquals("page size", 11,p.size()); + assertEquals("document count", 111,p.getTotalSize()); }catch(Exception e){ throw e; }finally{ @@ -274,74 +279,149 @@ public void testPOJOWriteWithPojoPageReadAll() throws Exception { } p=products.readAll(1); assertFalse("Page has any records ?",p.hasContent()); - + } - @Test +// @Test public void testPOJOSearchWithCollectionsandTransaction() throws Exception { PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); PojoPage p; + products.deleteAll(); Transaction t= client.openTransaction(); //Load more than 111 objects into different collections try{ - for(int i=1;i<112;i++){ - if(i%2==0){ - products.write(this.getArtifact(i),t,"even","numbers"); - } - else { - products.write(this.getArtifact(i),"odd","numbers"); + for(int i=1;i<112;i++){ + if(i%2==0){ + products.write(this.getArtifact(i),t,"even","numbers"); + } + else { + products.write(this.getArtifact(i),"odd","numbers"); + } } + assertEquals("Total number of object recods",56, products.count("numbers")); + assertEquals("Collection even count",0,products.count("even")); + assertEquals("Collection odd count",56,products.count("odd")); + + products.setPageLength(10); + long pageNo=1,count=0; + do{ + count =0; + p = products.search(pageNo, "even"); + while(p.iterator().hasNext()){ + Artifact a =p.iterator().next(); + validateArtifact(a); + assertTrue("Artifact Id is even", a.getId()%2==0); + count++; + } + assertEquals("Page size",count,p.size()); + pageNo=pageNo+p.getPageSize(); + }while(!p.isLastPage() && pageNo products = client.newPojoRepository(Artifact.class, Long.class); + PojoPage p; + products.deleteAll(); + Transaction t= client.openTransaction(); + //Load more than 111 objects into different collections + try{ + for(int i=1;i<112;i++){ + if(i%2==0){ + products.write(this.getArtifact(i),t,"even","numbers"); + } + else { + products.write(this.getArtifact(i),"odd","numbers"); + } } -// assertEquals("Page size",count,p.size()); - pageNo=pageNo+p.getPageSize(); - - }while(!p.isLastPage() ); - - assertEquals("Total no of documents left",0,products.count()); + assertEquals("Total number of object recods",56, products.count("numbers")); + assertEquals("Collection even count",0,products.count("even")); + assertEquals("Collection odd count",56,products.count("odd")); + + products.setPageLength(10); + QueryManager queryMgr = client.newQueryManager(); + StringQueryDefinition qd = queryMgr.newStringDefinition(); + qd.setCriteria("Acme"); + long pageNo=1,count=0; + do{ + count =0; + p = products.search(qd,pageNo); + while(p.iterator().hasNext()){ + Artifact a =p.iterator().next(); + validateArtifact(a); + count++; + } + assertEquals("Page size",count,p.size()); + pageNo=pageNo+p.getPageSize(); + }while(!p.isLastPage() && pageNo products = client.newPojoRepository(Artifact.class, Long.class); @@ -221,7 +241,7 @@ public void testPOJOSearchWithStringHandle() throws JsonProcessingException, IOE this.loadSimplePojos(products); QueryManager queryMgr = client.newQueryManager(); StringQueryDefinition qd = queryMgr.newStringDefinition(); - qd.setCriteria("cogs 2"); + qd.setCriteria("5"); StringHandle results = new StringHandle(); JacksonHandle jh = new JacksonHandle(); p = products.search(qd, 1,jh); @@ -244,15 +264,16 @@ public void testPOJOSearchWithStringHandle() throws JsonProcessingException, IOE }while(!p.isLastPage() && pageNo Date: Thu, 11 Sep 2014 13:00:54 -0700 Subject: [PATCH 099/357] adding test for structured search for POJO --- .../javaclient/TestPOJOWithStrucQD.java | 287 ++++++++++++++++++ 1 file changed, 287 insertions(+) create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithStrucQD.java diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithStrucQD.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithStrucQD.java new file mode 100644 index 000000000..7cbc52c5b --- /dev/null +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithStrucQD.java @@ -0,0 +1,287 @@ +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.IOException; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.JacksonHandle; +import com.marklogic.client.io.SearchHandle; +import com.marklogic.client.io.StringHandle; +import com.marklogic.client.pojo.PojoPage; +import com.marklogic.client.pojo.PojoRepository; +import com.marklogic.client.query.MatchDocumentSummary; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.StringQueryDefinition; +import com.marklogic.client.query.StructuredQueryBuilder; +import com.marklogic.client.query.StructuredQueryBuilder.Operator; +import com.marklogic.client.query.StructuredQueryDefinition; + +public class TestPOJOWithStrucQD extends BasicJavaClientREST { + private static String dbName = "TestPOJOStrucQDSearchDB"; + private static String [] fNames = {"TestPOJOStrucQDSearchDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client ; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + addRangePathIndex(dbName, "long", "com.marklogic.javaclient.Artifact/inventory", "", "ignore"); + addRangePathIndex(dbName, "string", "com.marklogic.javaclient.Artifact/manufacturer/com.marklogic.javaclient.Company/name", "http://marklogic.com/collation/", "ignore"); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + System.out.println("In tear down" ); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } + @Before + public void setUp() throws Exception { + client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST); + } + @After + public void tearDown() throws Exception { + // release client + client.release(); + } + + public Artifact getArtifact(int counter){ + + Artifact cogs = new Artifact(); + cogs.setId(counter); + if( counter % 5 == 0){ + cogs.setName("Cogs special"); + if(counter % 2 ==0){ + Company acme = new Company(); + acme.setName("Acme special, Inc."); + acme.setWebsite("http://www.acme special.com"); + acme.setLatitude(41.998+counter); + acme.setLongitude(-87.966+counter); + cogs.setManufacturer(acme); + + }else{ + Company widgets = new Company(); + widgets.setName("Widgets counter Inc."); + widgets.setWebsite("http://www.widgets counter.com"); + widgets.setLatitude(41.998+counter); + widgets.setLongitude(-87.966+counter); + cogs.setManufacturer(widgets); + } + }else{ + cogs.setName("Cogs "+counter); + if(counter % 2 ==0){ + Company acme = new Company(); + acme.setName("Acme "+counter+", Inc."); + acme.setWebsite("http://www.acme"+counter+".com"); + acme.setLatitude(41.998+counter); + acme.setLongitude(-87.966+counter); + cogs.setManufacturer(acme); + + }else{ + Company widgets = new Company(); + widgets.setName("Widgets "+counter+", Inc."); + widgets.setWebsite("http://www.widgets"+counter+".com"); + widgets.setLatitude(41.998+counter); + widgets.setLongitude(-87.966+counter); + cogs.setManufacturer(widgets); + } + } + cogs.setInventory(1000+counter); + return cogs; + } + public void validateArtifact(Artifact art) + { + assertNotNull("Artifact object should never be Null",art); + assertNotNull("Id should never be Null",art.id); + assertTrue("Inventry is always greater than 1000", art.getInventory()>1000); + } + public void loadSimplePojos(PojoRepository products) + { + for(int i=1;i<111;i++){ + if(i%2==0){ + products.write(this.getArtifact(i),"even","numbers"); + } + else { + products.write(this.getArtifact(i),"odd","numbers"); + } + } + } + @Test + public void testPOJOSearchWithoutSearchHandle() { + PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); + PojoPage p; + this.loadSimplePojos(products); + + StructuredQueryBuilder qb = new StructuredQueryBuilder(); + StructuredQueryDefinition q1 =qb.andNot(qb.term("cogs"),qb.term("special")); + StructuredQueryDefinition qd = qb.and(q1,qb.collection("odd")); + products.setPageLength(11); + p = products.search(qd, 1); + assertEquals("total no of pages",4,p.getTotalPages()); +// System.out.println(p.getTotalPages()); + long pageNo=1,count=0; + do{ + count =0; + + p = products.search(qd,pageNo); + + while(p.iterator().hasNext()){ + Artifact a =p.iterator().next(); + validateArtifact(a); + assertFalse("Verifying document with special is not there",a.getId()%5==0); + assertTrue("Artifact Id is odd", a.getId()%2!=0); + assertTrue("Company name contains widgets",a.getManufacturer().getName().contains("Widgets")); + count++; + // System.out.println(a.getId()+" "+a.getManufacturer().getName() +" "+count); + } + assertEquals("Page size",count,p.size()); + pageNo=pageNo+p.getPageSize(); + }while(!p.isLastPage() && pageNo products = client.newPojoRepository(Artifact.class, Long.class); + PojoPage p; + this.loadSimplePojos(products); + + StructuredQueryBuilder qb = new StructuredQueryBuilder(); + StructuredQueryDefinition q1 =qb.range(qb.pathIndex("com.marklogic.javaclient.Artifact/inventory"), "xs:long",Operator.GT, 1010); + StructuredQueryDefinition qd = qb.and(q1,qb.range(qb.pathIndex("com.marklogic.javaclient.Artifact/inventory"), "xs:long",Operator.LE, 1110),qb.collection("even")); + SearchHandle results = new SearchHandle(); + products.setPageLength(10); + p = products.search(qd, 1,results); + assertEquals("total no of pages",5,p.getTotalPages()); + System.out.println(p.getTotalPages()); + long pageNo=1,count=0; + do{ + count =0; + p = products.search(qd,pageNo,results); + + while(p.iterator().hasNext()){ + Artifact a =p.iterator().next(); + validateArtifact(a); + assertTrue("Enventory lies between 1010 to 1110", a.getInventory()>1010 && a.getInventory()<=1110); + assertTrue("Artifact Id is even", a.getId()%2==0); + assertTrue("Company name contains Acme",a.getManufacturer().getName().contains("Acme")); + count++; + // System.out.println(a.getId()+" "+a.getManufacturer().getName() +" "+count); + } + assertEquals("Page size",count,p.size()); + pageNo=pageNo+p.getPageSize(); + MatchDocumentSummary[] mds =results.getMatchResults(); + assertEquals("Size of the results summary",10,mds.length); + for(MatchDocumentSummary md:mds){ + assertTrue("every uri should contain the class name",md.getUri().contains("Artifact")); + } + String[] facetNames = results.getFacetNames(); + for(String fname:facetNames){ + System.out.println(fname); + } + assertEquals("Total resulr from search handle ",50,results.getTotalResults()); + assertTrue("Search Handle metric results ",results.getMetrics().getTotalTime()>0); + }while(!p.isLastPage() && pageNo products = client.newPojoRepository(Artifact.class, Long.class); + PojoPage p; + this.loadSimplePojos(products); + ; + StructuredQueryBuilder qb = new StructuredQueryBuilder(); + StructuredQueryDefinition q1 =qb.containerQuery(qb.jsonProperty("name"),qb.term("special") ); + StructuredQueryDefinition qd = qb.and(q1,qb.word(qb.jsonProperty("name"), "acme")); + JacksonHandle results = new JacksonHandle(); + p = products.search(qd, 1,results); + products.setPageLength(11); + assertEquals("total no of pages",1,p.getTotalPages()); + System.out.println(p.getTotalPages()+results.get().toString()); + long pageNo=1,count=0; + do{ + count =0; + p = products.search(qd,pageNo,results); + + while(p.iterator().hasNext()){ + Artifact a =p.iterator().next(); + validateArtifact(a); + count++; + assertTrue("Manufacture name starts with acme",a.getManufacturer().getName().contains("Acme")); + assertTrue("Artifact name contains",a.getName().contains("special")); + + } + assertEquals("Page size",count,p.size()); + pageNo=pageNo+p.getPageSize(); + + assertEquals("Page start from search handls vs page methods",results.get().get("start").asLong(),p.getStart() ); + assertEquals("Format in the search handle","json",results.get().withArray("results").get(1).path("format").asText()); + assertTrue("Uri in search handle contains Artifact",results.get().withArray("results").get(1).path("uri").asText().contains("Artifact")); + // System.out.println(results.get().toString()); + }while(!p.isLastPage() && pageNo products = client.newPojoRepository(Artifact.class, Long.class); + PojoPage p; + this.loadSimplePojos(products); + StructuredQueryBuilder qb = new StructuredQueryBuilder(); + StructuredQueryDefinition qd =qb.value(qb.jsonProperty("id"), 5,10,15,20,25,30); +// StructuredQueryDefinition qd = qb.and(q1,qb.range(qb.pathIndex("com.marklogic.javaclient.Artifact/inventory"), "xs:long",Operator.LE, 1110),qb.collection("even")); + + StringHandle results = new StringHandle(); + JacksonHandle jh = new JacksonHandle(); + p = products.search(qd, 1,jh); + + long pageNo=1,count=0; + do{ + count =0; + p = products.search(qd,pageNo,results.withFormat(Format.JSON)); + + while(p.iterator().hasNext()){ + Artifact a =p.iterator().next(); + validateArtifact(a); + count++; + } + assertEquals("Page total results",count,p.getTotalSize()); + pageNo=pageNo+p.getPageSize(); +// System.out.println(results.get().toString()); + }while(!p.isLastPage() && pageNo Date: Thu, 11 Sep 2014 15:06:13 -0700 Subject: [PATCH 100/357] adding test for structured search for POJO --- .../javaclient/TestPOJOWithStrucQD.java | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithStrucQD.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithStrucQD.java index 7cbc52c5b..e54112522 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithStrucQD.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithStrucQD.java @@ -13,6 +13,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; import com.marklogic.client.DatabaseClient; import com.marklogic.client.DatabaseClientFactory; import com.marklogic.client.DatabaseClientFactory.Authentication; @@ -24,6 +25,7 @@ import com.marklogic.client.pojo.PojoRepository; import com.marklogic.client.query.MatchDocumentSummary; import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.RawStructuredQueryDefinition; import com.marklogic.client.query.StringQueryDefinition; import com.marklogic.client.query.StructuredQueryBuilder; import com.marklogic.client.query.StructuredQueryBuilder.Operator; @@ -282,6 +284,122 @@ public void testPOJOSearchWithStringHandle() throws JsonProcessingException, IOE assertEquals("Total search results resulted are ",6,actNode.asInt() ); } +@Test + public void testPOJOSearchWithRawXMLStructQD() { + PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); + PojoPage p; + this.loadSimplePojos(products); + + QueryManager queryMgr = client.newQueryManager(); + String rawXMLQuery = + ""+ + " "+ + "special"+ + ""+ + ""+ + "Acme"+ + " "+ + ""; + StringHandle rh = new StringHandle(rawXMLQuery); + RawStructuredQueryDefinition qd = + queryMgr.newRawStructuredQueryDefinition(rh); + JacksonHandle results = new JacksonHandle(); + p = products.search(qd, 1,results); + products.setPageLength(11); + assertEquals("total no of pages",1,p.getTotalPages()); + System.out.println(p.getTotalPages()+results.get().toString()); + long pageNo=1,count=0; + do{ + count =0; + p = products.search(qd,pageNo,results); + + while(p.iterator().hasNext()){ + Artifact a =p.iterator().next(); + validateArtifact(a); + count++; + assertTrue("Manufacture name starts with acme",a.getManufacturer().getName().contains("Acme")); + assertTrue("Artifact name contains",a.getName().contains("special")); + } + assertEquals("Page size",count,p.size()); + pageNo=pageNo+p.getPageSize(); + + assertEquals("Page start from search handls vs page methods",results.get().get("start").asLong(),p.getStart() ); + assertEquals("Format in the search handle","json",results.get().withArray("results").get(1).path("format").asText()); + assertTrue("Uri in search handle contains Artifact",results.get().withArray("results").get(1).path("uri").asText().contains("Artifact")); + // System.out.println(results.get().toString()); + }while(!p.isLastPage() && pageNo products = client.newPojoRepository(Artifact.class, Long.class); + PojoPage p; + this.loadSimplePojos(products); + QueryManager queryMgr = client.newQueryManager(); + JacksonHandle jh = new JacksonHandle(); + ObjectMapper mapper = new ObjectMapper(); + // constructing JSON representation of Raw JSON Structured Query + + ObjectNode txtNode = mapper.createObjectNode(); + txtNode.putArray("text").add("special"); + ObjectNode termQNode = mapper.createObjectNode(); + termQNode.set("term-query", txtNode); + ObjectNode queriesArrayNode = mapper.createObjectNode(); + queriesArrayNode.putArray("queries").add(termQNode); + + ObjectNode txtNode2 = mapper.createObjectNode(); + txtNode2.putArray("text").add("Widgets"); + ObjectNode termQNode2 = mapper.createObjectNode(); + termQNode2.set("term-query", txtNode2); + queriesArrayNode.withArray("queries").add(termQNode2); + + ObjectNode orQueryNode = mapper.createObjectNode(); + orQueryNode.set("and-query",queriesArrayNode ); + + ObjectNode queryArrayNode = mapper.createObjectNode(); + queryArrayNode.putArray("queries").add(orQueryNode); + ObjectNode mainNode = mapper.createObjectNode(); + mainNode.set("query", queryArrayNode); + jh.set(mainNode); + RawStructuredQueryDefinition qd = + queryMgr.newRawStructuredQueryDefinition(jh); + + JacksonHandle results = new JacksonHandle(); + p = products.search(qd, 1,results); + products.setPageLength(11); + assertEquals("total no of pages",1,p.getTotalPages()); + System.out.println(p.getTotalPages()+results.get().toString()); + long pageNo=1,count=0; + do{ + count =0; + p = products.search(qd,pageNo,results); + + while(p.iterator().hasNext()){ + Artifact a =p.iterator().next(); + validateArtifact(a); + count++; + assertTrue("Manufacture name starts with acme",a.getManufacturer().getName().contains("Widgets")); + assertTrue("Artifact name contains",a.getName().contains("special")); + + } + assertEquals("Page size",count,p.size()); + pageNo=pageNo+p.getPageSize(); + + assertEquals("Page start from search handls vs page methods",results.get().get("start").asLong(),p.getStart() ); + assertEquals("Format in the search handle","json",results.get().withArray("results").get(1).path("format").asText()); + assertTrue("Uri in search handle contains Artifact",results.get().withArray("results").get(1).path("uri").asText().contains("Artifact")); + // System.out.println(results.get().toString()); + }while(!p.isLastPage() && pageNo Date: Fri, 12 Sep 2014 09:37:45 -0600 Subject: [PATCH 101/357] still not compiling... --- src/main/java/com/marklogic/client/Page.java | 21 ++++++++++++++-- .../client/document/DocumentPage.java | 2 +- .../marklogic/client/eval/IteratorPage.java | 25 ------------------- .../marklogic/client/eval/VariableSet.java | 2 +- .../client/http/MultiPartResponsePage.java | 4 +-- .../com/marklogic/client/impl/BasicPage.java | 4 +-- .../com/marklogic/client/impl/XdbcEval.java | 10 ++++++-- 7 files changed, 33 insertions(+), 35 deletions(-) delete mode 100644 src/main/java/com/marklogic/client/eval/IteratorPage.java diff --git a/src/main/java/com/marklogic/client/Page.java b/src/main/java/com/marklogic/client/Page.java index 1ca918ec8..f561f8fea 100644 --- a/src/main/java/com/marklogic/client/Page.java +++ b/src/main/java/com/marklogic/client/Page.java @@ -16,12 +16,29 @@ package com.marklogic.client; import java.util.Iterator; +import java.util.Iterable; /** A generic interface for pagination through large sets of items of type <T>. */ -public interface Page extends Iterable { - /** An iterator over the items in this page. */ +public interface Page extends Iterator, Iterable { + /** A new iterator over the elements of type T in this Page. Each call to iterator() + * will create a new Iterator which will start at the first record in this Page. + * Iterators created via this method are indepentent of the internal iterator + * used for {@link hasNext()} and {@link next()}. + */ public Iterator iterator(); + /** Returns true if internal iterator has more elements. + * The internal iterator is separate from any new iterator created by calls to iterator(). + * @return true if the internal iterator has more elements + */ + public boolean hasNext(); + + /** Returns the next element in the internal iterator, which is separate + * from any new iterator created by calls to iterator(). + * @return the next element in the iteration + */ + public T next(); + /** The start position of this page within all possible items. For result sets * this is the position of the first result within the result set. * @return the start position diff --git a/src/main/java/com/marklogic/client/document/DocumentPage.java b/src/main/java/com/marklogic/client/document/DocumentPage.java index 5ebedc26c..b4295701a 100644 --- a/src/main/java/com/marklogic/client/document/DocumentPage.java +++ b/src/main/java/com/marklogic/client/document/DocumentPage.java @@ -18,6 +18,6 @@ import com.marklogic.client.Page; import com.marklogic.client.io.marker.AbstractReadHandle; -public interface DocumentPage extends IteratorPage { +public interface DocumentPage extends Page { public T nextContent(T contentHandle); } diff --git a/src/main/java/com/marklogic/client/eval/IteratorPage.java b/src/main/java/com/marklogic/client/eval/IteratorPage.java deleted file mode 100644 index 69a81b20e..000000000 --- a/src/main/java/com/marklogic/client/eval/IteratorPage.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright 2012-2014 MarkLogic Corporation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.marklogic.client; - -import java.util.Iterator; - -public interface IteratorPage extends Iterator, Page { - public boolean hasNext(); - public T next(); -} - - diff --git a/src/main/java/com/marklogic/client/eval/VariableSet.java b/src/main/java/com/marklogic/client/eval/VariableSet.java index da96bbc93..aef5ed482 100644 --- a/src/main/java/com/marklogic/client/eval/VariableSet.java +++ b/src/main/java/com/marklogic/client/eval/VariableSet.java @@ -15,7 +15,7 @@ */ package com.marklogic.client.eval; -import com.marklogic.client.IteratorPage; +import com.marklogic.client.Page; /** Marker interface implemented by {@link XMLVariableSet} and {@link JSONVariableSet} * so ServerEval can accept either type of VariableSet. */ diff --git a/src/main/java/com/marklogic/client/http/MultiPartResponsePage.java b/src/main/java/com/marklogic/client/http/MultiPartResponsePage.java index 93e892e43..7b34faa18 100644 --- a/src/main/java/com/marklogic/client/http/MultiPartResponsePage.java +++ b/src/main/java/com/marklogic/client/http/MultiPartResponsePage.java @@ -15,7 +15,7 @@ */ package com.marklogic.client.http; -import com.marklogic.client.IteratorPage; +import com.marklogic.client.Page; -public interface MultiPartResponsePage extends IteratorPage { +public interface MultiPartResponsePage extends Page { } diff --git a/src/main/java/com/marklogic/client/impl/BasicPage.java b/src/main/java/com/marklogic/client/impl/BasicPage.java index c6835155d..a56e43375 100644 --- a/src/main/java/com/marklogic/client/impl/BasicPage.java +++ b/src/main/java/com/marklogic/client/impl/BasicPage.java @@ -18,7 +18,7 @@ import java.util.Iterator; import com.marklogic.client.Page; -public class BasicPage implements IteratorPage { +public class BasicPage implements Page { private Iterable iterable; private Iterator iterator; private long start; @@ -45,7 +45,7 @@ public boolean hasNext() { return iterator.hasNext(); } - public DocumentRecord next() { + public T next() { return iterator.next(); } diff --git a/src/main/java/com/marklogic/client/impl/XdbcEval.java b/src/main/java/com/marklogic/client/impl/XdbcEval.java index 837067fbc..855000919 100644 --- a/src/main/java/com/marklogic/client/impl/XdbcEval.java +++ b/src/main/java/com/marklogic/client/impl/XdbcEval.java @@ -13,6 +13,9 @@ public class XdbcEval { public static void main(String[] args) { + System.setProperty("com.sun.xml.ws.transport.http.client.HttpTransportPipe.dump", "true"); + System.setProperty("com.sun.xml.ws.transport.local.LocalTransportPipe.dump", "true"); + System.setProperty("com.sun.xml.ws.util.pipe.StandaloneTubeAssembler.dump", "true"); Client client = ClientBuilder.newClient(); // set up digest auth HttpAuthenticationFeature feature = HttpAuthenticationFeature.digest("admin", "admin"); @@ -61,7 +64,10 @@ public static void main(String[] args) { "default return 'unknown'" + "};" + "for $var at $i in ($myvar1, $myvar2, $myvar3, $myvar4, $myvar5, $myvar6) " + - "return 'type' || $i || ': ' || local:getType($var)"; + "return (" + + "$var," + + "'type' || $i || ': ' || local:getType($var)" + + ")"; // set up x-www-form-urlencoded request params Form params = new Form() .param("xquery", xquery) @@ -101,7 +107,7 @@ public static void main(String[] args) { MultiPart body = res.readEntity(MultiPart.class); System.out.println("response=[" + res + "]"); for ( BodyPart part : body.getBodyParts() ) { - System.out.println("part body=[" + part.getEntityAs(String.class) + "]"); + System.out.println("part body2=[" + part.getEntityAs(String.class) + "]"); } } } From 82912c6895797c42c0951dd808bea6c8c6adb93b Mon Sep 17 00:00:00 2001 From: sammefford Date: Fri, 12 Sep 2014 09:39:28 -0600 Subject: [PATCH 102/357] add CONTRIBUTING --- CONTRIBUTING.md | 124 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 CONTRIBUTING.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 000000000..d800e6ce4 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,124 @@ += Contributor License Agreement ("Agreement") + +Thank you for your interest in an open source project administered by +the MarkLogic Corporation (the "Project"). This Agreement states the +terms under which you, the person listed below, may contribute to the +Project any software, modifications or additions, bug fixes, +documentation, or any other materials that you provide (whether +electronically, verbally, or in written form) to MarkLogic +Corporation ("MarkLogic" or "us") related to the Project (each a +"Contribution"). This license is for your protection as well as the +protection of MarkLogic and its licensees. This Agreement does not +otherwise change your rights to use your own Contributions for any +other purpose. Please complete the following information about you and +the Contributions and either (a) sign electronically by filling out +the form below or (b) sign and submit the form to the contact +information below. + +If you develop software or create software code as a part of your +employment, or if your employer is in the business of developing +software, or otherwise may claim rights in the Contributions, please +provide information about your employer's policy on contributing to +open source projects below, including the name of the supervisor to +contact in connection with such contributions. + +If you have questions about this Agreement, please contact us at +opensource@marklogic.com. + +.You and MarkLogic acknowledge and agree: +. Control of the Project. We, in our sole discretion, determine the + Contributions that will be included in the Project. You understand + that this Agreement, and your submission of any Contribution, does + not guarantee that any such Contributions will be included in the + Project. +. License Grants. You hereby grant to MarkLogic, a perpetual, + non-exclusive, irrevocable, worldwide, royalty-free and fully + paid-up, sublicenseable, and transferable license under all of your + relevant intellectual property rights (including copyright, patent, + trade secret, and any other rights), to use, copy, modify, prepare + derivative works of, distribute and publicly perform and publicly + display your Contributions on any licensing terms, in our sole + discretion, including without limitation: (a) the GNU General Public + License, the GNU Lesser General Public License, the Common Public + License, the MIT License, the Apache License, or the Berkeley + Software Distribution license; and (b) binary, proprietary, or + commercial licenses. You hereby grant to MarkLogic and to recipients + of the Project a perpetual, worldwide, non-exclusive, fully-paid up + and royalty-free, irrevocable patent license to make, have made, use, + offer to sell, sell, import, and otherwise transfer the Project, + where such license applies only to those patent claims licensable by + you that are necessarily infringed by your Contributions alone or by + combination of your Contributions with the Project. +. Ownership. Except for the licenses granted herein, you reserve all + right, title, and interest in and to your Contributions. +. Your Representations. You represent that you are able and entitled to + grant us the above license rights. You represent that your + Contributions are your original works of authorship, and to your + knowledge, no other person claims, or has the right to claim, any + right in any intellectual property right, including without + limitation invention or patent rights, related to your Contributions. + You also represent that you are not legally obligated, whether by + entering into an agreement or otherwise, in any way that conflicts + with the terms of the foregoing license grant. If your employer has + rights to intellectual property that you create, you represent that + you have received permission to make your Contributions on behalf of + that employer, or that your employer has waived such rights for your + Contributions. +. No Implied Warranties. MarkLogic acknowledges that, except as + explicitly described in this Agreement, your Contribution is + provided on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF + ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, + ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, + MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. + +== Complete and sign + +---- +Full name: ____________________________________________________________ + +Email address: ____________________________________________________________ + +Postal address: + + Street: _____________________________________________________ + + _____________________________________________________ + + City: _____________________________________________________ + + State or province: _____________________________________________________ + + Postal code: _____________________________________________________ + + Country: _____________________________________________________ + +Phone number: ____________________________________________________________ + + +Corporate contributor information: + + ____________________________________________________________ + + ____________________________________________________________ + + ____________________________________________________________ + + ____________________________________________________________ + + +Signature: ____________________________________________________________ + +---- + +== Signing Alternatives + +You may print the document and send the original to: + +MarkLogic Corporation + +Attn: Contract Administrator + +999 Skyway Road, + +San Carlos, CA 94070 + +USA + + +You may also send a scanned PDF of the form to +opensource@marklogic.com. From 1cebad7b05ca5522457db2e20c4cdc151bfa5653 Mon Sep 17 00:00:00 2001 From: Ajit George Date: Fri, 12 Sep 2014 09:32:08 -0700 Subject: [PATCH 103/357] Added additional methods to metadata tests and added JacksonParserHandle unit test. --- .../TestBulkReadWriteMetaDataChange.java | 222 ++++++++-- .../TestBulkReadWriteWithJacksonHandle.java | 174 ++++---- ...tBulkReadWriteWithJacksonParserHandle.java | 385 ++++++++++++++++++ 3 files changed, 651 insertions(+), 130 deletions(-) create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonParserHandle.java diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteMetaDataChange.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteMetaDataChange.java index 2fd6ec860..f3aeddde4 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteMetaDataChange.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteMetaDataChange.java @@ -15,7 +15,9 @@ import com.marklogic.client.DatabaseClient; import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.Transaction; import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.DocumentManager.Metadata; import com.marklogic.client.document.DocumentPage; import com.marklogic.client.document.DocumentRecord; import com.marklogic.client.document.DocumentWriteSet; @@ -25,6 +27,7 @@ import com.marklogic.client.io.DocumentMetadataHandle.DocumentCollections; import com.marklogic.client.io.DocumentMetadataHandle.DocumentPermissions; import com.marklogic.client.io.DocumentMetadataHandle.DocumentProperties; +import com.marklogic.client.io.Format; import com.marklogic.client.io.StringHandle; /** @@ -41,7 +44,7 @@ public class TestBulkReadWriteMetaDataChange extends BasicJavaClientREST { private static String restServerName = "REST-Java-Client-API-Server"; private static int restPort = 8011; private DatabaseClient client ; - + /** * @throws java.lang.Exception */ @@ -50,7 +53,6 @@ public static void setUpBeforeClass() throws Exception { System.out.println("In Setup"); setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); createRESTUser("app-user", "password", "rest-writer","rest-reader" ); - } /** @@ -95,10 +97,11 @@ public DocumentMetadataHandle setMetadata(){ metadataHandle.setQuality(23); return metadataHandle; } - - public DocumentMetadataHandle setUpdatedMetadataProperties(){ + + public DocumentMetadataHandle setUpdatedMetadataProperties() { // create and initialize a handle on the metadata DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); + metadataHandle.getProperties().put("reviewed", false); metadataHandle.getProperties().put("myString", "bar"); metadataHandle.getProperties().put("myInteger", 20); @@ -107,8 +110,8 @@ public DocumentMetadataHandle setUpdatedMetadataProperties(){ metadataHandle.setQuality(12); return metadataHandle; } - - public DocumentMetadataHandle setUpdatedMetadataCollections(){ + + public DocumentMetadataHandle setUpdatedMetadataCollections() { // create and initialize a handle on the metadata DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle().withCollections("my-collection3","my-collection4"); //metadataHandle.getCollections().addAll("my-collection1","my-collection2"); @@ -121,8 +124,8 @@ public DocumentMetadataHandle setUpdatedMetadataCollections(){ metadataHandle.setQuality(23); return metadataHandle; } - - public void validateMetadata(DocumentMetadataHandle mh){ + + public void validateMetadata(DocumentMetadataHandle mh) { // get metadata values DocumentProperties properties = mh.getProperties(); DocumentPermissions permissions = mh.getPermissions(); @@ -142,7 +145,7 @@ public void validateMetadata(DocumentMetadataHandle mh){ // Permissions String actualPermissions = getDocumentPermissionsString(permissions); System.out.println("Returned permissions: " + actualPermissions); - + assertTrue("Document permissions difference in size value", actualPermissions.contains("size:3")); assertTrue("Document permissions difference in rest-reader permission", actualPermissions.contains("rest-reader:[READ]")); assertTrue("Document permissions difference in rest-writer permission", actualPermissions.contains("rest-writer:[UPDATE]")); @@ -152,13 +155,13 @@ public void validateMetadata(DocumentMetadataHandle mh){ // Collections String actualCollections = getDocumentCollectionsString(collections); System.out.println("Returned collections: " + actualCollections); - + assertTrue("Document collections difference in size value", actualCollections.contains("size:2")); assertTrue("my-collection1 not found", actualCollections.contains("my-collection1")); assertTrue("my-collection2 not found", actualCollections.contains("my-collection2")); } - - public void validateUpdatedMetadataProperties(DocumentMetadataHandle mh){ + + public void validateUpdatedMetadataProperties(DocumentMetadataHandle mh) { // get metadata values DocumentProperties properties = mh.getProperties(); DocumentPermissions permissions = mh.getPermissions(); @@ -178,7 +181,7 @@ public void validateUpdatedMetadataProperties(DocumentMetadataHandle mh){ // Permissions String actualPermissions = getDocumentPermissionsString(permissions); System.out.println("Returned permissions: " + actualPermissions); - + assertTrue("Document permissions difference in size value", actualPermissions.contains("size:3")); assertTrue("Document permissions difference in rest-reader permission", actualPermissions.contains("rest-reader:[READ]")); assertTrue("Document permissions difference in rest-writer permission", actualPermissions.contains("rest-writer:[UPDATE]")); @@ -188,13 +191,13 @@ public void validateUpdatedMetadataProperties(DocumentMetadataHandle mh){ // Collections String actualCollections = getDocumentCollectionsString(collections); System.out.println("Returned collections: " + actualCollections); - + assertTrue("Document collections difference in size value", actualCollections.contains("size:2")); assertTrue("my-collection1 not found", actualCollections.contains("my-collection1")); assertTrue("my-collection2 not found", actualCollections.contains("my-collection2")); } - - public void validateUpdatedMetadataCollections(DocumentMetadataHandle mh){ + + public void validateUpdatedMetadataCollections(DocumentMetadataHandle mh) { // get metadata values DocumentProperties properties = mh.getProperties(); DocumentPermissions permissions = mh.getPermissions(); @@ -214,7 +217,7 @@ public void validateUpdatedMetadataCollections(DocumentMetadataHandle mh){ // Permissions String actualPermissions = getDocumentPermissionsString(permissions); System.out.println("Returned permissions: " + actualPermissions); - + assertTrue("Document permissions difference in size value", actualPermissions.contains("size:3")); assertTrue("Document permissions difference in rest-reader permission", actualPermissions.contains("rest-reader:[READ]")); assertTrue("Document permissions difference in rest-writer permission", actualPermissions.contains("rest-writer:[UPDATE]")); @@ -224,27 +227,27 @@ public void validateUpdatedMetadataCollections(DocumentMetadataHandle mh){ // Collections String actualCollections = getDocumentCollectionsString(collections); System.out.println("Returned collections: " + actualCollections); - + assertTrue("Document collections difference in size value", actualCollections.contains("size:2")); assertTrue("my-collection1 not found", actualCollections.contains("my-collection3")); assertTrue("my-collection2 not found", actualCollections.contains("my-collection4")); } - + /* - * This test verifies that properties do not change when new meta data is used in a bulk write set. - * Verified by reading individual documents - */ + * This test verifies that properties do not change when new meta data is used in a bulk write set. + * Verified by reading individual documents. User does not have permission to update the meta-data. + */ - @Test(expected=com.marklogic.client.ForbiddenUserException.class) - public void testWriteMultipleTextDocWithChangedMetadataProperties() - { + @Test(expected=com.marklogic.client.ForbiddenUserException.class) + public void testWriteMultipleTextDocWithChangedMetadataProperties() { String docId[] = {"/foo/test/myFoo1.txt","/foo/test/myFoo2.txt","/foo/test/myFoo3.txt"}; TextDocumentManager docMgr = client.newTextDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); + DocumentWriteSet writeset = docMgr.newWriteSet(); // put metadata DocumentMetadataHandle mh = setMetadata(); + DocumentMetadataHandle mhRead = null; writeset.addDefault(mh); writeset.add(docId[0], new StringHandle().with("This is so foo1")); @@ -255,33 +258,166 @@ public void testWriteMultipleTextDocWithChangedMetadataProperties() while(page.hasNext()){ DocumentRecord rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh); + docMgr.readMetadata(rec.getUri(), mhRead); validateMetadata(mh); } validateMetadata(mh); - + // Add new meta-data DocumentMetadataHandle mhUpdated = setUpdatedMetadataProperties(); writeset.addDefault(mhUpdated); - + docMgr.write(writeset); - page = docMgr.read(docId); + DocumentMetadataHandle mhUpd = null; - while(page.hasNext()){ - DocumentRecord rec = page.next(); - docMgr.readMetadata(rec.getUri(), mhUpdated); - validateUpdatedMetadataProperties(mhUpdated); + for(String docURI : docId){ + docMgr.readMetadata(docURI, mhUpd); + validateUpdatedMetadataProperties(mhUpd); } - validateUpdatedMetadataProperties(mhUpdated); + validateUpdatedMetadataProperties(mhUpd); } - + /* - * This test verifies that collections do change when new meta data is used in a bulk write set. - * Verified by reading individual documents - */ + * Purpose: To validate: DocumentManager::read(Transaction, uri....) + * This test verifies document meta-data reads from an open database transaction in the representation provided by the handle to call readMetadata. + * Verified by reading meta-data of individual document records from Document Page. + * read method performs the bulk read + */ + @Test + public void testBulkReadUsingMultipleUri() { + String docId[] = {"/foo/test/transactionURIFoo1.txt","/foo/test/transactionURIFoo2.txt","/foo/test/transactionURIFoo3.txt"}; + Transaction transaction = client.openTransaction(); + try { + TextDocumentManager docMgr = client.newTextDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); + DocumentWriteSet writeset = docMgr.newWriteSet(); + // put metadata + DocumentMetadataHandle mh = setMetadata(); + DocumentMetadataHandle mhRead = new DocumentMetadataHandle(); + + writeset.addDefault(mh); + writeset.add(docId[0], new StringHandle().with("This is so transactionURIFoo 1")); + writeset.add(docId[1], new StringHandle().with("This is so transactionURIFoo 2")); + writeset.add(docId[2], new StringHandle().with("This is so transactionURIFoo 3")); + docMgr.write(writeset, transaction); + transaction.commit(); + transaction = client.openTransaction(); + + DocumentPage page = docMgr.read(transaction, docId[0], docId[1], docId[2]); + + while(page.hasNext()){ + DocumentRecord rec = page.next(); + mhRead = rec.getMetadata(mhRead); + validateMetadata(mhRead); + } + + validateMetadata(mhRead); + } + catch(Exception exp) { + System.out.println(exp.getMessage()); + throw exp; + } + finally { + transaction.rollback(); + } + } + + + /* + * * Purpose: To validate: DocumentManager::readMetadata(uri, MetdataHandle, Transaction) + * This test verifies document meta-data reads from an open database transaction in the representation provided by the handle to call readMetadata. + * Verified by reading meta-data for individual documents. + */ + + @Test + public void testReadUsingMultipleUriAndMetadataHandleInTransaction() throws Exception { + String docId[] = {"/foo/test/multipleURIFoo1.txt","/foo/test/multipleURIFoo2.txt","/foo/test/multipleURIFoo3.txt"}; + Transaction transaction = client.openTransaction(); + try { + TextDocumentManager docMgr = client.newTextDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); + + DocumentWriteSet writeset = docMgr.newWriteSet(); + // put metadata + DocumentMetadataHandle mh = setMetadata(); + + writeset.addDefault(mh); + writeset.add(docId[0], new StringHandle().with("This is so multipleURI foo 1")); + writeset.add(docId[1], new StringHandle().with("This is so multipleURI foo 2")); + writeset.add(docId[2], new StringHandle().with("This is so multipleURI foo 3")); + docMgr.write(writeset, transaction); + transaction.commit(); + transaction = client.openTransaction(); + + DocumentMetadataHandle mhRead = new DocumentMetadataHandle(); + + for(String docStrId : docId) { + docMgr.readMetadata(docStrId, mhRead, transaction); + validateMetadata(mhRead); + } + validateMetadata(mhRead); + } + catch(Exception exp) { + System.out.println(exp.getMessage()); + throw exp; + } + finally { + transaction.rollback(); + } + } + + + /* + * * Purpose: To validate: DocumentManager readMetadata(String... uris) + * This test verifies document meta-data reads from an open database in the representation provided by the handle to call readMetadata. + * Verified by reading meta-data for individual documents. + */ + + @Test + public void testBulkReadMetadataUsingMultipleUriNoTransaction() throws Exception { + String docId[] = {"/foo/test/URIFoo1.txt","/foo/test/URIFoo2.txt","/foo/test/URIFoo3.txt"}; + + try { + TextDocumentManager docMgr = client.newTextDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); + + DocumentWriteSet writeset = docMgr.newWriteSet(); + // put metadata + DocumentMetadataHandle mh = setMetadata(); + + writeset.addDefault(mh); + writeset.add(docId[0], new StringHandle().with("This is so URI foo 1")); + writeset.add(docId[1], new StringHandle().with("This is so URI foo 2")); + writeset.add(docId[2], new StringHandle().with("This is so URI foo 3")); + docMgr.write(writeset); + + DocumentMetadataHandle mhRead = new DocumentMetadataHandle(); + + DocumentPage page = docMgr.readMetadata(docId[0], docId[1], docId[2]); + + while(page.hasNext()){ + DocumentRecord rec = page.next(); + rec.getMetadata(mhRead); + validateMetadata(mhRead); + } + + validateMetadata(mhRead); + } + catch(Exception exp) { + System.out.println(exp.getMessage()); + throw exp; + } + finally { + //transaction.rollback(); + } + } + + /* + * This test verifies that collections do change when new meta data is used in a bulk write set. + * Verified by reading individual documents + */ @Test - public void testWriteMultipleTextDocWithChangedMetadataCollections() - { + public void testWriteMultipleTextDocWithChangedMetadataCollections() { String docId[] = {"/foo/test/myFoo4.txt","/foo/test/myFoo5.txt","/foo/test/myFoo6.txt"}; TextDocumentManager docMgr = client.newTextDocumentManager(); @@ -294,10 +430,10 @@ public void testWriteMultipleTextDocWithChangedMetadataCollections() writeset.add(docId[1], new StringHandle().with("This is so foo5")); writeset.add(docId[2], new StringHandle().with("This is so foo6")); docMgr.write(writeset); - + // Add new meta-data DocumentMetadataHandle mhUpdated = setUpdatedMetadataCollections(); - + docMgr.writeMetadata(docId[0], mhUpdated); docMgr.writeMetadata(docId[1], mhUpdated); docMgr.writeMetadata(docId[2], mhUpdated); diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonHandle.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonHandle.java index bbd1d0dcc..81ec65835 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonHandle.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonHandle.java @@ -14,6 +14,7 @@ import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; +import org.skyscreamer.jsonassert.JSONAssert; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; @@ -24,6 +25,7 @@ import com.marklogic.client.document.DocumentRecord; import com.marklogic.client.document.DocumentWriteSet; import com.marklogic.client.document.JSONDocumentManager; +import com.marklogic.client.document.DocumentManager.Metadata; import com.marklogic.client.io.DocumentMetadataHandle; import com.marklogic.client.io.DocumentMetadataHandle.Capability; import com.marklogic.client.io.DocumentMetadataHandle.DocumentCollections; @@ -38,8 +40,6 @@ * and also by descriptors. */ - - public class TestBulkReadWriteWithJacksonHandle extends BasicJavaClientREST { private static final int BATCH_SIZE=100; @@ -49,31 +49,29 @@ public class TestBulkReadWriteWithJacksonHandle extends BasicJavaClientREST { private static String restServerName = "REST-Java-Client-API-Server"; private static int restPort = 8011; private DatabaseClient client ; - @BeforeClass + @BeforeClass public static void setUp() throws Exception { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); - setupAppServicesConstraint(dbName); + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + setupAppServicesConstraint(dbName); + + } + @Before public void testSetup() throws Exception + { + // create new connection for each test below + client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST); + } + @After + public void testCleanUp() throws Exception + { + System.out.println("Running CleanUp script"); + // release client + client.release(); } - - @Before public void testSetup() throws Exception - { - // create new connection for each test below - client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST); - } - @After - public void testCleanUp() throws Exception - { - System.out.println("Running CleanUp script"); - // release client - client.release(); - - - } - - public DocumentMetadataHandle setMetadata(){ + + public DocumentMetadataHandle setMetadata(){ // create and initialize a handle on the metadata DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); metadataHandle.getCollections().addAll("my-collection1","my-collection2"); @@ -86,8 +84,8 @@ public DocumentMetadataHandle setMetadata(){ metadataHandle.setQuality(23); return metadataHandle; } - - public void validateMetadata(DocumentMetadataHandle mh){ + + public void validateMetadata(DocumentMetadataHandle mh){ // get metadata values DocumentProperties properties = mh.getProperties(); DocumentPermissions permissions = mh.getPermissions(); @@ -96,15 +94,15 @@ public void validateMetadata(DocumentMetadataHandle mh){ // Properties // String expectedProperties = "size:5|reviewed:true|myInteger:10|myDecimal:34.56678|myCalendar:2014|myString:foo|"; String actualProperties = getDocumentPropertiesString(properties); - boolean result = actualProperties.contains("size:6|"); + boolean result = actualProperties.contains("size:5|"); assertTrue("Document properties count", result); // Permissions String actualPermissions = getDocumentPermissionsString(permissions); System.out.println(actualPermissions); - assertTrue("Document permissions difference in size value", actualPermissions.contains("size:4")); - assertTrue("Document permissions difference in flexrep-eval permission", actualPermissions.contains("flexrep-eval:[READ]")); + assertTrue("Document permissions difference in size value", actualPermissions.contains("size:3")); + //assertTrue("Document permissions difference in flexrep-eval permission", actualPermissions.contains("flexrep-eval:[READ]")); assertTrue("Document permissions difference in rest-reader permission", actualPermissions.contains("rest-reader:[READ]")); assertTrue("Document permissions difference in rest-writer permission", actualPermissions.contains("rest-writer:[UPDATE]")); assertTrue("Document permissions difference in app-user permissions", @@ -118,8 +116,8 @@ public void validateMetadata(DocumentMetadataHandle mh){ assertTrue("my-collection1 not found", actualCollections.contains("my-collection1")); assertTrue("my-collection2 not found", actualCollections.contains("my-collection2")); } - - public void validateDefaultMetadata(DocumentMetadataHandle mh){ + + public void validateDefaultMetadata(DocumentMetadataHandle mh){ // get metadata values DocumentProperties properties = mh.getProperties(); DocumentPermissions permissions = mh.getPermissions(); @@ -127,35 +125,36 @@ public void validateDefaultMetadata(DocumentMetadataHandle mh){ // Properties String actualProperties = getDocumentPropertiesString(properties); - boolean result =actualProperties.contains("size:1|"); + boolean result =actualProperties.contains("size:0|"); System.out.println(actualProperties +result); assertTrue("Document default last modified properties count1?", result); - // Permissions String actualPermissions = getDocumentPermissionsString(permissions); - assertTrue("Document permissions difference in size value", actualPermissions.contains("size:3")); - assertTrue("Document permissions difference in flexrep-eval permission", actualPermissions.contains("flexrep-eval:[READ]")); + assertTrue("Document permissions difference in size value", actualPermissions.contains("size:2")); + //assertTrue("Document permissions difference in flexrep-eval permission", actualPermissions.contains("flexrep-eval:[READ]")); assertTrue("Document permissions difference in rest-reader permission", actualPermissions.contains("rest-reader:[READ]")); assertTrue("Document permissions difference in rest-writer permission", actualPermissions.contains("rest-writer:[UPDATE]")); // Collections - String expectedCollections = "size:1|http://permission-collections/|"; + String expectedCollections = "size:0|"; String actualCollections = getDocumentCollectionsString(collections); assertEquals("Document collections difference", expectedCollections, actualCollections); } - - @Test - public void testWriteMultipleJSONDocs() throws Exception - { + + @Test + public void testWriteMultipleJSONDocs() throws Exception + { String docId[] = { "/a.json", "/b.json", "/c.json" }; String json1 = new String("{\"animal\":\"dog\", \"says\":\"woof\"}"); String json2 = new String("{\"animal\":\"cat\", \"says\":\"meow\"}"); String json3 = new String("{\"animal\":\"rat\", \"says\":\"keek\"}"); JSONDocumentManager docMgr = client.newJSONDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); + DocumentWriteSet writeset = docMgr.newWriteSet(); ObjectMapper mapper = new ObjectMapper(); @@ -183,21 +182,21 @@ public void testWriteMultipleJSONDocs() throws Exception JacksonHandle r1 = new JacksonHandle(); docMgr.read(docId[0], r1); - //{\"animal\":\"dog\", \"says\":\"woof\"} The space after dog is lost - assertEquals(json1.trim(), r1.toString().trim()); + JSONAssert.assertEquals(json1, r1.toString(), true); + docMgr.read(docId[1], r1); - assertEquals("Json File Content" + docId[1], json2, r1.toString().trim()); + JSONAssert.assertEquals(json2, r1.toString(), true); + docMgr.read(docId[2], r1); - assertEquals("Json File Content" + docId[2], json3, r1.toString().trim()); - //bfr.close(); - } - - /* - * - * Use JacksonHandle to load json strings using bulk write set. - * Test Bulk Read to see you can read the document specific meta-data. - */ - + JSONAssert.assertEquals(json3, r1.toString(), true); + } + + /* + * + * Use JacksonHandle to load json strings using bulk write set. + * Test Bulk Read to see you can read the document specific meta-data. + */ + @Test public void testWriteMultipleJSONDocsWithDefaultMetadata() throws Exception { @@ -205,26 +204,27 @@ public void testWriteMultipleJSONDocsWithDefaultMetadata() throws Exception String json1 = new String("{\"animal\":\"dog\", \"says\":\"woof\"}"); String json2 = new String("{\"animal\":\"cat\", \"says\":\"meow\"}"); String json3 = new String("{\"animal\":\"rat\", \"says\":\"keek\"}"); - + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); DocumentWriteSet writeset = docMgr.newWriteSet(); // put metadata DocumentMetadataHandle mh = setMetadata(); - + ObjectMapper mapper = new ObjectMapper(); - + JacksonHandle jacksonHandle1 = new JacksonHandle(); JacksonHandle jacksonHandle2 = new JacksonHandle(); JacksonHandle jacksonHandle3 = new JacksonHandle(); - + JsonNode dogNode = mapper.readTree(json1); jacksonHandle1.set(dogNode); jacksonHandle1.withFormat(Format.JSON); - + JsonNode catNode = mapper.readTree(json2); jacksonHandle2.set(catNode); jacksonHandle2.withFormat(Format.JSON); - + JsonNode ratNode = mapper.readTree(json3); jacksonHandle3.set(ratNode); jacksonHandle3.withFormat(Format.JSON); @@ -246,13 +246,13 @@ public void testWriteMultipleJSONDocsWithDefaultMetadata() throws Exception } validateMetadata(mh); } - + /* - * - * Use JacksonHandle to load json strings using bulk write set. - * Test Bulk Read to see you can read all the documents - */ - @Test + * + * Use JacksonHandle to load json strings using bulk write set. + * Test Bulk Read to see you can read all the documents + */ + @Test public void testWriteMultipleJSONDocsWithDefaultMetadata2() throws Exception { // Synthesize input content @@ -272,40 +272,41 @@ public void testWriteMultipleJSONDocsWithDefaultMetadata2() throws Exception // Create and build up the batch JSONDocumentManager jdm = client.newJSONDocumentManager(); + jdm.setMetadataCategories(Metadata.ALL); DocumentWriteSet batch = jdm.newWriteSet(); - + ObjectMapper mapper = new ObjectMapper(); - + JacksonHandle jacksonHandle1 = new JacksonHandle(); - + JsonNode catNode = mapper.readTree(doc1); jacksonHandle1.set(catNode); jacksonHandle1.withFormat(Format.JSON); - + JacksonHandle jacksonHandle2 = new JacksonHandle(); - + JsonNode dogNode = mapper.readTree(doc2); jacksonHandle2.set(dogNode); jacksonHandle2.withFormat(Format.JSON); - + JacksonHandle jacksonHandle3 = new JacksonHandle(); - + JsonNode eagleNode = mapper.readTree(doc3); jacksonHandle3.set(eagleNode); jacksonHandle3.withFormat(Format.JSON); - + JacksonHandle jacksonHandle4 = new JacksonHandle(); - + JsonNode lionNode = mapper.readTree(doc4); jacksonHandle4.set(lionNode); jacksonHandle4.withFormat(Format.JSON); - + JacksonHandle jacksonHandle5 = new JacksonHandle(); - + JsonNode manNode = mapper.readTree(doc5); jacksonHandle5.set(manNode); jacksonHandle5.withFormat(Format.JSON); - + // use system default metadata batch.add("doc1.json", jacksonHandle1); @@ -372,20 +373,19 @@ public void testWriteMultipleJSONDocsWithDefaultMetadata2() throws Exception assertEquals("default quality",2,mh.getQuality()); } - + @AfterClass public static void tearDown() throws Exception { - System.out.println("In tear down" ); - tearDownJavaRESTServer(dbName, fNames, restServerName); + System.out.println("In tear down" ); + tearDownJavaRESTServer(dbName, fNames, restServerName); } - public void validateRecord(DocumentRecord record,Format type) { - - assertNotNull("DocumentRecord should never be null", record); - assertNotNull("Document uri should never be null", record.getUri()); - assertTrue("Document uri should start with " + DIRECTORY, record.getUri().startsWith(DIRECTORY)); - assertEquals("All records are expected to be in same format", type, record.getFormat()); - - } + public void validateRecord(DocumentRecord record,Format type) { + + assertNotNull("DocumentRecord should never be null", record); + assertNotNull("Document uri should never be null", record.getUri()); + assertTrue("Document uri should start with " + DIRECTORY, record.getUri().startsWith(DIRECTORY)); + assertEquals("All records are expected to be in same format", type, record.getFormat()); + } } \ No newline at end of file diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonParserHandle.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonParserHandle.java new file mode 100644 index 000000000..95cbe5c4f --- /dev/null +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonParserHandle.java @@ -0,0 +1,385 @@ +package com.marklogic.javaclient; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayOutputStream; +import java.io.Reader; +import java.io.StringReader; +import java.util.Calendar; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.skyscreamer.jsonassert.JSONAssert; + +import com.fasterxml.jackson.core.JsonFactory; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.DocumentPage; +import com.marklogic.client.document.DocumentRecord; +import com.marklogic.client.document.DocumentWriteSet; +import com.marklogic.client.document.JSONDocumentManager; +import com.marklogic.client.document.DocumentManager.Metadata; +import com.marklogic.client.io.DocumentMetadataHandle; +import com.marklogic.client.io.DocumentMetadataHandle.Capability; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentCollections; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentPermissions; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentProperties; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.JacksonHandle; +import com.marklogic.client.io.JacksonParserHandle; +import com.marklogic.client.io.ReaderHandle; + +/* + * This test is designed to to test all of bulk reads and write of JSON with JacksonHandle Manager by passing set of uris + * and also by descriptors. + */ + +public class TestBulkReadWriteWithJacksonParserHandle extends + BasicJavaClientREST { + + private static final int BATCH_SIZE = 100; + private static final String DIRECTORY = "/bulkread/"; + private static String dbName = "TestBulkReadWriteWithJacksonParserDB"; + private static String[] fNames = { "TestBulkReadWriteWithJacksonParserDB-1" }; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client; + + @BeforeClass + public static void setUp() throws Exception { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName, restPort); + setupAppServicesConstraint(dbName); + + } + + @Before + public void testSetup() throws Exception { + // create new connection for each test below + client = DatabaseClientFactory.newClient("localhost", restPort, + "rest-admin", "x", Authentication.DIGEST); + } + + @After + public void testCleanUp() throws Exception { + System.out.println("Running CleanUp script"); + // release client + client.release(); + } + + public DocumentMetadataHandle setMetadata() { + // create and initialize a handle on the metadata + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); + metadataHandle.getCollections().addAll("my-collection1", + "my-collection2"); + metadataHandle.getPermissions().add("app-user", Capability.UPDATE, + Capability.READ); + metadataHandle.getProperties().put("reviewed", true); + metadataHandle.getProperties().put("myString", "foo"); + metadataHandle.getProperties().put("myInteger", 10); + metadataHandle.getProperties().put("myDecimal", 34.56678); + metadataHandle.getProperties().put("myCalendar", + Calendar.getInstance().get(Calendar.YEAR)); + metadataHandle.setQuality(23); + return metadataHandle; + } + + public void validateMetadata(DocumentMetadataHandle mh) { + // get metadata values + DocumentProperties properties = mh.getProperties(); + DocumentPermissions permissions = mh.getPermissions(); + DocumentCollections collections = mh.getCollections(); + + // Properties + // String expectedProperties = + // "size:5|reviewed:true|myInteger:10|myDecimal:34.56678|myCalendar:2014|myString:foo|"; + String actualProperties = getDocumentPropertiesString(properties); + boolean result = actualProperties.contains("size:5|"); + assertTrue("Document properties count", result); + + // Permissions + String actualPermissions = getDocumentPermissionsString(permissions); + System.out.println(actualPermissions); + + assertTrue("Document permissions difference in size value", + actualPermissions.contains("size:3")); + //assertTrue( + // "Document permissions difference in flexrep-eval permission", + // actualPermissions.contains("flexrep-eval:[READ]")); + assertTrue("Document permissions difference in rest-reader permission", + actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", + actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue( + "Document permissions difference in app-user permissions", + (actualPermissions.contains("app-user:[UPDATE, READ]") || actualPermissions + .contains("app-user:[READ, UPDATE]"))); + + // Collections + String actualCollections = getDocumentCollectionsString(collections); + System.out.println(collections); + + assertTrue("Document collections difference in size value", + actualCollections.contains("size:2")); + assertTrue("my-collection1 not found", + actualCollections.contains("my-collection1")); + assertTrue("my-collection2 not found", + actualCollections.contains("my-collection2")); + } + + public void validateDefaultMetadata(DocumentMetadataHandle mh){ + // get metadata values + DocumentProperties properties = mh.getProperties(); + DocumentPermissions permissions = mh.getPermissions(); + DocumentCollections collections = mh.getCollections(); + + // Properties + String actualProperties = getDocumentPropertiesString(properties); + boolean result =actualProperties.contains("size:0|"); + System.out.println(actualProperties +result); + assertTrue("Document default last modified properties count1?", result); + + // Permissions + String actualPermissions = getDocumentPermissionsString(permissions); + + assertTrue("Document permissions difference in size value", actualPermissions.contains("size:2")); + //assertTrue("Document permissions difference in flexrep-eval permission", actualPermissions.contains("flexrep-eval:[READ]")); + assertTrue("Document permissions difference in rest-reader permission", actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", actualPermissions.contains("rest-writer:[UPDATE]")); + + // Collections + String expectedCollections = "size:0|"; + String actualCollections = getDocumentCollectionsString(collections); + + assertEquals("Document collections difference", expectedCollections, actualCollections); + } + + @Test + public void testWriteMultipleJSONDocs() throws Exception { + String docId[] = { "/a.json", "/b.json", "/c.json" }; + String json1 = new String("{\"animal\":\"dog\", \"says\":\"woof\"}"); + String json2 = new String("{\"animal\":\"cat\", \"says\":\"meow\"}"); + String json3 = new String("{\"animal\":\"rat\", \"says\":\"keek\"}"); + + JsonFactory f = new JsonFactory(); + + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); + DocumentWriteSet writeset = docMgr.newWriteSet(); + + JacksonParserHandle jacksonParserHandle1 = new JacksonParserHandle(); + JacksonParserHandle jacksonParserHandle2 = new JacksonParserHandle(); + JacksonParserHandle jacksonParserHandle3 = new JacksonParserHandle(); + + jacksonParserHandle1.set(f.createJsonParser(json1)); + jacksonParserHandle2.set(f.createJsonParser(json2)); + jacksonParserHandle3.set(f.createJsonParser(json3)); + + writeset.add(docId[0], jacksonParserHandle1); + writeset.add(docId[1], jacksonParserHandle2); + writeset.add(docId[2], jacksonParserHandle3); + + docMgr.write(writeset); + + JacksonHandle r1 = new JacksonHandle(); + docMgr.read(docId[0], r1); + JSONAssert.assertEquals(json1, r1.toString(), true); + + docMgr.read(docId[1], r1); + JSONAssert.assertEquals(json2, r1.toString(), true); + + docMgr.read(docId[2], r1); + JSONAssert.assertEquals(json3, r1.toString(), true); + } + + /* + * + * Use JacksonHandle to load json strings using bulk write set. Test Bulk + * Read to see you can read the document specific meta-data. + */ + + @Test + public void testWriteMultipleJSONDocsWithDefaultMetadata() throws Exception { + String docId[] = { "/a.json", "/b.json", "/c.json" }; + String json1 = new String("{\"animal\":\"dog\", \"says\":\"woof\"}"); + String json2 = new String("{\"animal\":\"cat\", \"says\":\"meow\"}"); + String json3 = new String("{\"animal\":\"rat\", \"says\":\"keek\"}"); + + JsonFactory f = new JsonFactory(); + + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); + + DocumentWriteSet writeset = docMgr.newWriteSet(); + // put metadata + DocumentMetadataHandle mh = setMetadata(); + + JacksonParserHandle jacksonParserHandle1 = new JacksonParserHandle(); + JacksonParserHandle jacksonParserHandle2 = new JacksonParserHandle(); + JacksonParserHandle jacksonParserHandle3 = new JacksonParserHandle(); + + jacksonParserHandle1.set(f.createJsonParser(json1)); + jacksonParserHandle2.set(f.createJsonParser(json2)); + jacksonParserHandle3.set(f.createJsonParser(json3)); + + writeset.addDefault(mh); + writeset.add(docId[0], jacksonParserHandle1); + writeset.add(docId[1], jacksonParserHandle2); + writeset.add(docId[2], jacksonParserHandle3); + + docMgr.write(writeset); + + DocumentPage page = docMgr.read(docId); + + while (page.hasNext()) { + DocumentRecord rec = page.next(); + docMgr.readMetadata(rec.getUri(), mh); + System.out.println(rec.getUri()); + validateMetadata(mh); + } + validateMetadata(mh); + } + + /* + * + * Use JacksonHandle to load json strings using bulk write set. Test Bulk + * Read to see you can read all the documents + */ + @Test + public void testWriteMultipleJSONDocsWithDefaultMetadata2() + throws Exception { + // Synthesize input content + String doc1 = new String("{\"animal\": \"cat\", \"says\": \"meow\"}"); + String doc2 = new String("{\"animal\": \"dog\", \"says\": \"bark\"}"); + String doc3 = new String( + "{\"animal\": \"eagle\", \"says\": \"squeak\"}"); + String doc4 = new String("{\"animal\": \"lion\", \"says\": \"roar\"}"); + String doc5 = new String("{\"animal\": \"man\", \"says\": \"hello\"}"); + + // Synthesize input metadata + DocumentMetadataHandle defaultMetadata1 = new DocumentMetadataHandle() + .withQuality(1); + DocumentMetadataHandle defaultMetadata2 = new DocumentMetadataHandle() + .withQuality(2); + DocumentMetadataHandle docSpecificMetadata = new DocumentMetadataHandle() + .withCollections("mySpecificCollection"); + + // Create and build up the batch + JSONDocumentManager jdm = client.newJSONDocumentManager(); + jdm.setMetadataCategories(Metadata.ALL); + + DocumentWriteSet batch = jdm.newWriteSet(); + + JsonFactory f = new JsonFactory(); + + JacksonParserHandle jacksonParserHandle1 = new JacksonParserHandle(); + JacksonParserHandle jacksonParserHandle2 = new JacksonParserHandle(); + JacksonParserHandle jacksonParserHandle3 = new JacksonParserHandle(); + JacksonParserHandle jacksonParserHandle4 = new JacksonParserHandle(); + JacksonParserHandle jacksonParserHandle5 = new JacksonParserHandle(); + + jacksonParserHandle1.set(f.createJsonParser(doc1)); + jacksonParserHandle2.set(f.createJsonParser(doc2)); + jacksonParserHandle3.set(f.createJsonParser(doc3)); + jacksonParserHandle4.set(f.createJsonParser(doc4)); + jacksonParserHandle5.set(f.createJsonParser(doc5)); + + // use system default metadata + batch.add("doc1.json", jacksonParserHandle1); + + // using batch default metadata + batch.addDefault(defaultMetadata1); + batch.add("doc2.json", jacksonParserHandle2); // batch default metadata + batch.add("doc3.json", docSpecificMetadata, jacksonParserHandle3); + batch.add("doc4.json", jacksonParserHandle4); // batch default metadata + + // replace batch default metadata with new metadata + batch.addDefault(defaultMetadata2); + batch.add("doc5.json", jacksonParserHandle5); // batch default + + // Execute the write operation + jdm.write(batch); + DocumentPage page; + DocumentRecord rec; + // Check the results + // Doc1 should have the system default quality of 0 + page = jdm.read("doc1.json"); + DocumentMetadataHandle mh = new DocumentMetadataHandle(); + rec = page.next(); + jdm.readMetadata(rec.getUri(), mh); + validateDefaultMetadata(mh); + assertEquals("default quality", 0, mh.getQuality()); + + // Doc2 should use the first batch default metadata, with quality 1 + page = jdm.read("doc2.json"); + rec = page.next(); + jdm.readMetadata(rec.getUri(), mh); + System.out.print(mh.getCollections().isEmpty()); + assertEquals("default quality", 1, mh.getQuality()); + assertTrue("default collections reset", mh.getCollections().isEmpty()); + + // Doc3 should have the system default document quality (0) because + // quality + // was not included in the document-specific metadata. It should be in + // the + // collection "mySpecificCollection", from the document-specific + // metadata. + + page = jdm.read("doc3.json"); + rec = page.next(); + jdm.readMetadata(rec.getUri(), mh); + assertEquals("default quality", 0, mh.getQuality()); + assertEquals("default collection must change", + "[mySpecificCollection]", mh.getCollections().toString()); + + DocumentMetadataHandle doc3Metadata = jdm.readMetadata("doc3.json", + new DocumentMetadataHandle()); + System.out.println("doc3 quality: Expected=0, Actual=" + + doc3Metadata.getPermissions()); + System.out.print("doc3 collections: Expected: myCollection, Actual="); + for (String collection : doc3Metadata.getCollections()) { + System.out.print(collection + " "); + } + System.out.println(); + + // Doc 4 should also use the 1st batch default metadata, with quality 1 + page = jdm.read("doc4.json"); + rec = page.next(); + jdm.readMetadata(rec.getUri(), mh); + assertEquals("default quality", 1, mh.getQuality()); + assertTrue("default collections reset", mh.getCollections().isEmpty()); + // Doc5 should use the 2nd batch default metadata, with quality 2 + page = jdm.read("doc5.json"); + rec = page.next(); + jdm.readMetadata(rec.getUri(), mh); + assertEquals("default quality", 2, mh.getQuality()); + + } + + @AfterClass + public static void tearDown() throws Exception { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } + + public void validateRecord(DocumentRecord record, Format type) { + + assertNotNull("DocumentRecord should never be null", record); + assertNotNull("Document uri should never be null", record.getUri()); + assertTrue("Document uri should start with " + DIRECTORY, record + .getUri().startsWith(DIRECTORY)); + assertEquals("All records are expected to be in same format", type, + record.getFormat()); + + } +} \ No newline at end of file From 7a7208ad8674a000022bb1ac75fef599c55b55f7 Mon Sep 17 00:00:00 2001 From: sravan Date: Fri, 12 Sep 2014 10:12:55 -0700 Subject: [PATCH 104/357] added jackson pojo bulk write --- .../javaclient/TestBulkWriteSample1.java | 512 ++++++++++-------- 1 file changed, 279 insertions(+), 233 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteSample1.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteSample1.java index 9e07f1347..969546f37 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteSample1.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteSample1.java @@ -38,6 +38,8 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import org.skyscreamer.jsonassert.*; +import org.json.JSONException; import org.junit.*; import static org.junit.Assert.*; @@ -56,185 +58,185 @@ public class TestBulkWriteSample1 extends BasicJavaClientREST { - + private static String dbName = "TestBulkWriteSampleDB"; private static String [] fNames = {"TestBulkWriteSampleDB-1"}; private static String restServerName = "REST-Java-Client-API-Server"; private static int restPort = 8011; private DatabaseClient client ; - @BeforeClass + @BeforeClass public static void setUp() throws Exception { - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); - - //To enable client side http logging - -// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); -// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "debug"); -// setupAppServicesConstraint(dbName); + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + + //To enable client side http logging + + // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "debug"); + // setupAppServicesConstraint(dbName); } - - @Before public void testSetup() throws Exception - { - // create new connection for each test below - client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST); - } - @After - public void testCleanUp() throws Exception - { - System.out.println("Running clear script"); - // release client - client.release(); - - - } - /* - * This is cloned in github with tracking bug #27685 - * https://github.com/marklogic/java-client-api/issues/23 - * - * This test uses StringHandle to load 3 text documents, writes to database using bulk write set. - * Verified by reading individual documents - */ + + @Before public void testSetup() throws Exception + { + // create new connection for each test below + client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST); + } + @After + public void testCleanUp() throws Exception + { + System.out.println("Running clear script"); + // release client + client.release(); + + + } + /* + * This is cloned in github with tracking bug #27685 + * https://github.com/marklogic/java-client-api/issues/23 + * + * This test uses StringHandle to load 3 text documents, writes to database using bulk write set. + * Verified by reading individual documents + */ @Test public void testWriteMultipleTextDoc() - { - - String docId[] = {"/foo/test/myFoo1.txt","/foo/test/myFoo2.txt","/foo/test/myFoo3.txt"}; - - TextDocumentManager docMgr = client.newTextDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - - writeset.add(docId[0], new StringHandle().with("This is so foo1")); - writeset.add(docId[1], new StringHandle().with("This is so foo2")); - writeset.add(docId[2], new StringHandle().with("This is so foo3")); - - docMgr.write(writeset); - - assertEquals("Text document write difference", "This is so foo1", docMgr.read(docId[0], new StringHandle()).get()); - assertEquals("Text document write difference", "This is so foo2", docMgr.read(docId[1], new StringHandle()).get()); - assertEquals("Text document write difference", "This is so foo3", docMgr.read(docId[2], new StringHandle()).get()); - } + { + + String docId[] = {"/foo/test/myFoo1.txt","/foo/test/myFoo2.txt","/foo/test/myFoo3.txt"}; + + TextDocumentManager docMgr = client.newTextDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + + writeset.add(docId[0], new StringHandle().with("This is so foo1")); + writeset.add(docId[1], new StringHandle().with("This is so foo2")); + writeset.add(docId[2], new StringHandle().with("This is so foo3")); + + docMgr.write(writeset); + + assertEquals("Text document write difference", "This is so foo1", docMgr.read(docId[0], new StringHandle()).get()); + assertEquals("Text document write difference", "This is so foo2", docMgr.read(docId[1], new StringHandle()).get()); + assertEquals("Text document write difference", "This is so foo3", docMgr.read(docId[2], new StringHandle()).get()); + } /* - * This test uses DOMHandle to load 3 xml documents, writes to database using bulk write set. - * Verified by reading individual documents - */ + * This test uses DOMHandle to load 3 xml documents, writes to database using bulk write set. + * Verified by reading individual documents + */ @Test public void testWriteMultipleXMLDoc() throws Exception - { - - - String docId[] = {"/foo/test/Foo1.xml","/foo/test/Foo2.xml","/foo/test/Foo3.xml"}; - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - - writeset.add(docId[0], new DOMHandle(getDocumentContent("This is so foo1"))); - writeset.add(docId[1], new DOMHandle().with(getDocumentContent("This is so foo2"))); - writeset.add(docId[2], new DOMHandle().with(getDocumentContent("This is so foo3"))); - - docMgr.write(writeset); - - DOMHandle dh = new DOMHandle(); - docMgr.read(docId[0], dh); - - - assertEquals("xml document write difference", "This is so foo1",dh.get().getChildNodes().item(0).getTextContent()); - docMgr.read(docId[1], dh); - assertEquals("xml document write difference", "This is so foo2", dh.get().getChildNodes().item(0).getTextContent()); - docMgr.read(docId[2], dh); - assertEquals("xml document write difference", "This is so foo3", dh.get().getChildNodes().item(0).getTextContent()); - } + { + + + String docId[] = {"/foo/test/Foo1.xml","/foo/test/Foo2.xml","/foo/test/Foo3.xml"}; + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + + writeset.add(docId[0], new DOMHandle(getDocumentContent("This is so foo1"))); + writeset.add(docId[1], new DOMHandle().with(getDocumentContent("This is so foo2"))); + writeset.add(docId[2], new DOMHandle().with(getDocumentContent("This is so foo3"))); + + docMgr.write(writeset); + + DOMHandle dh = new DOMHandle(); + docMgr.read(docId[0], dh); + + + assertEquals("xml document write difference", "This is so foo1",dh.get().getChildNodes().item(0).getTextContent()); + docMgr.read(docId[1], dh); + assertEquals("xml document write difference", "This is so foo2", dh.get().getChildNodes().item(0).getTextContent()); + docMgr.read(docId[2], dh); + assertEquals("xml document write difference", "This is so foo3", dh.get().getChildNodes().item(0).getTextContent()); + } /* * This test uses FileHandle to load 3 binary documents with same URI, writes to database using bulk write set. - * Expecting an exception. + * Expecting an exception. */ @Test (expected = FailedRequestException.class) public void testWriteMultipleSameBinaryDoc() throws Exception - { - String docId[] = {"Pandakarlino.jpg","mlfavicon.png"}; - - BinaryDocumentManager docMgr = client.newBinaryDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - File file1= null; - file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); - FileHandle handle1 = new FileHandle(file1); - writeset.add("/1/"+docId[0],handle1.withFormat(Format.BINARY)); - writeset.add("/1/"+docId[0],handle1.withFormat(Format.BINARY)); - - docMgr.write(writeset); - - } + { + String docId[] = {"Pandakarlino.jpg","mlfavicon.png"}; + + BinaryDocumentManager docMgr = client.newBinaryDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + File file1= null; + file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); + FileHandle handle1 = new FileHandle(file1); + writeset.add("/1/"+docId[0],handle1.withFormat(Format.BINARY)); + writeset.add("/1/"+docId[0],handle1.withFormat(Format.BINARY)); + + docMgr.write(writeset); + + } /* * This test uses FileHandle to load 3 binary documents, writes to database using bulk write set. - * Verified by reading individual documents + * Verified by reading individual documents */ @Test public void testWriteMultipleBinaryDoc() throws Exception - { - String docId[] = {"Pandakarlino.jpg","mlfavicon.png"}; - - BinaryDocumentManager docMgr = client.newBinaryDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - File file1= null,file2=null; - file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); - FileHandle handle1 = new FileHandle(file1); - writeset.add("/1/"+docId[0],handle1.withFormat(Format.BINARY)); - writeset.add("/2/"+docId[0],handle1.withFormat(Format.BINARY)); - file2 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[1]); - FileHandle handle2 = new FileHandle(file2); - writeset.add("/1/"+docId[1],handle2.withFormat(Format.BINARY)); - writeset.add("/2/"+docId[1],handle2.withFormat(Format.BINARY)); - - docMgr.write(writeset); - long fsize1 = file1.length(),fsize2 = file2.length(); - - - FileHandle readHandle1 = new FileHandle(); - docMgr.read("/1/"+docId[0],readHandle1); - - FileHandle readHandle2 = new FileHandle(); - docMgr.read("/1/"+docId[1],readHandle2); - System.out.println(file1.getName()+":"+fsize1+" "+readHandle1.get().getName()+":"+readHandle1.get().length()); - System.out.println(file2.getName()+":"+fsize2+" "+readHandle2.get().getName()+":"+readHandle2.get().length()); - assertEquals("Size of the File 1"+docId[0],fsize1,readHandle1.get().length()); - assertEquals("Size of the File 1"+docId[1],fsize2,readHandle2.get().length()); - } + { + String docId[] = {"Pandakarlino.jpg","mlfavicon.png"}; -/* + BinaryDocumentManager docMgr = client.newBinaryDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + File file1= null,file2=null; + file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); + FileHandle handle1 = new FileHandle(file1); + writeset.add("/1/"+docId[0],handle1.withFormat(Format.BINARY)); + writeset.add("/2/"+docId[0],handle1.withFormat(Format.BINARY)); + file2 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[1]); + FileHandle handle2 = new FileHandle(file2); + writeset.add("/1/"+docId[1],handle2.withFormat(Format.BINARY)); + writeset.add("/2/"+docId[1],handle2.withFormat(Format.BINARY)); + + docMgr.write(writeset); + long fsize1 = file1.length(),fsize2 = file2.length(); + + + FileHandle readHandle1 = new FileHandle(); + docMgr.read("/1/"+docId[0],readHandle1); + + FileHandle readHandle2 = new FileHandle(); + docMgr.read("/1/"+docId[1],readHandle2); + System.out.println(file1.getName()+":"+fsize1+" "+readHandle1.get().getName()+":"+readHandle1.get().length()); + System.out.println(file2.getName()+":"+fsize2+" "+readHandle2.get().getName()+":"+readHandle2.get().length()); + assertEquals("Size of the File 1"+docId[0],fsize1,readHandle1.get().length()); + assertEquals("Size of the File 1"+docId[1],fsize2,readHandle2.get().length()); + } + + /* * This test uses ReaderHandle to load 3 JSON documents, writes to database using bulk write set. - * Verified by reading individual documents - - */ + * Verified by reading individual documents + + */ @Test public void testWriteMultipleJSONDocs() throws Exception - { - String docId[] = {"/a.json","/b.json","/c.json"}; - String json1 = new String("{\"animal\":\"dog\", \"says\":\"woof\"}"); - String json2 = new String("{\"animal\":\"cat\", \"says\":\"meow\"}"); - String json3 = new String("{\"animal\":\"rat\", \"says\":\"keek\"}"); - Reader strReader = new StringReader(json1); - JSONDocumentManager docMgr = client.newJSONDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); - writeset.add(docId[0],new ReaderHandle(strReader).withFormat(Format.JSON)); - writeset.add(docId[1],new ReaderHandle(new StringReader(json2))); - writeset.add(docId[2],new ReaderHandle(new StringReader(json3))); - - docMgr.write(writeset); - - ReaderHandle r1 = new ReaderHandle(); - docMgr.read(docId[0],r1); - BufferedReader bfr = new BufferedReader(r1.get()); - assertEquals(json1,bfr.readLine()); - docMgr.read(docId[1],r1); - assertEquals("Json File Content"+docId[1],json2,new BufferedReader(r1.get()).readLine()); - docMgr.read(docId[2],r1); - assertEquals("Json File Content"+docId[2],json3,new BufferedReader(r1.get()).readLine()); - bfr.close(); - } + { + String docId[] = {"/a.json","/b.json","/c.json"}; + String json1 = new String("{\"animal\":\"dog\", \"says\":\"woof\"}"); + String json2 = new String("{\"animal\":\"cat\", \"says\":\"meow\"}"); + String json3 = new String("{\"animal\":\"rat\", \"says\":\"keek\"}"); + Reader strReader = new StringReader(json1); + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + writeset.add(docId[0],new ReaderHandle(strReader).withFormat(Format.JSON)); + writeset.add(docId[1],new ReaderHandle(new StringReader(json2))); + writeset.add(docId[2],new ReaderHandle(new StringReader(json3))); + + docMgr.write(writeset); + + ReaderHandle r1 = new ReaderHandle(); + docMgr.read(docId[0],r1); + BufferedReader bfr = new BufferedReader(r1.get()); + assertEquals(json1,bfr.readLine()); + docMgr.read(docId[1],r1); + assertEquals("Json File Content"+docId[1],json2,new BufferedReader(r1.get()).readLine()); + docMgr.read(docId[2],r1); + assertEquals("Json File Content"+docId[2],json3,new BufferedReader(r1.get()).readLine()); + bfr.close(); + } @Test public void testWriteMultipleJAXBDocs() throws Exception - { + { String docId[] ={"/jaxb/iphone.xml","/jaxb/ipad.xml","/jaxb/ipod.xml"}; Product product1 = new Product(); product1.setName("iPhone"); @@ -249,96 +251,140 @@ public void testWriteMultipleJAXBDocs() throws Exception product3.setIndustry("Hardware"); product3.setDescription("Very cool Ipod"); JAXBContext context = JAXBContext.newInstance(Product.class); - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); -// JAXBHandle contentHandle = new JAXBHandle(context); -// contentHandle.set(product1); - writeset.add(docId[0],new JAXBHandle(context).with(product1)); - writeset.add(docId[1],new JAXBHandle(context).with(product2)); - writeset.add(docId[2],new JAXBHandle(context).with(product3)); - - docMgr.write(writeset); - - DOMHandle dh = new DOMHandle(); - docMgr.read(docId[0], dh); - - assertEquals("xml document write difference", "Very cool Iphone",dh.get().getChildNodes().item(0).getChildNodes().item(1).getTextContent()); - docMgr.read(docId[1], dh); - assertEquals("xml document write difference", "Very cool Ipad", dh.get().getChildNodes().item(0).getChildNodes().item(1).getTextContent()); - docMgr.read(docId[2], dh); - assertEquals("xml document write difference", "Very cool Ipod", dh.get().getChildNodes().item(0).getChildNodes().item(1).getTextContent()); - - } -/* - * This test uses GenericManager to load all different document types - * This test has a bug logged in github with tracking Issue#33 - * - */ + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + // JAXBHandle contentHandle = new JAXBHandle(context); + // contentHandle.set(product1); + writeset.add(docId[0],new JAXBHandle(context).with(product1)); + writeset.add(docId[1],new JAXBHandle(context).with(product2)); + writeset.add(docId[2],new JAXBHandle(context).with(product3)); + + docMgr.write(writeset); + + DOMHandle dh = new DOMHandle(); + docMgr.read(docId[0], dh); + + assertEquals("xml document write difference", "Very cool Iphone",dh.get().getChildNodes().item(0).getChildNodes().item(1).getTextContent()); + docMgr.read(docId[1], dh); + assertEquals("xml document write difference", "Very cool Ipad", dh.get().getChildNodes().item(0).getChildNodes().item(1).getTextContent()); + docMgr.read(docId[2], dh); + assertEquals("xml document write difference", "Very cool Ipod", dh.get().getChildNodes().item(0).getChildNodes().item(1).getTextContent()); + + } + /* + * This test uses GenericManager to load all different document types + * This test has a bug logged in github with tracking Issue#33 + * + */ @Test public void testWriteGenericDocMgr() throws Exception - { - String docId[] = {"Pandakarlino.jpg","mlfavicon.png"}; - + { + String docId[] = {"Pandakarlino.jpg","mlfavicon.png"}; + GenericDocumentManager docMgr = client.newDocumentManager(); DocumentWriteSet writeset =docMgr.newWriteSet(); - - File file1= null; - file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); - FileInputStream fis = new FileInputStream(file1); - InputStreamHandle handle1 = new InputStreamHandle(fis); - handle1.setFormat(Format.BINARY); - - writeset.add("/generic/"+docId[0],handle1); - - 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",jh); - - String foo1 = "This is foo1 of byte Array"; - byte[] ba = foo1.getBytes(); - BytesHandle bh = new BytesHandle(ba); - bh.setFormat(Format.TEXT); - - writeset.add("/generic/foo1.txt",bh); - - DOMSource ds = new DOMSource(getDocumentContent("This is so foo1")); - SourceHandle sh = new SourceHandle(); - sh.set(ds); - sh.setFormat(Format.XML); - - writeset.add("/generic/foo.xml",sh); - - docMgr.write(writeset); - - FileHandle rh = new FileHandle(); - - docMgr.read("/generic/"+docId[0],rh); - assertEquals("Size of the File /generic/"+docId[0],file1.length(),rh.get().length()); - System.out.println(rh.get().getName()+":"+rh.get().length()+"\n"); - - docMgr.read("/generic/foo.xml",rh); - BufferedReader br=new BufferedReader(new FileReader(rh.get())); - br.readLine(); - assertEquals("xml document write difference", "This is so foo1", br.readLine() ); - docMgr.read("/generic/foo1.txt",rh); - br.close(); - br=new BufferedReader(new FileReader(rh.get())); - assertEquals("txt document write difference", foo1, br.readLine() ); - br.close(); - docMgr.read("/generic/dog.json",rh); - br=new BufferedReader(new FileReader(rh.get())); - assertEquals("Json document write difference", "{\"animal\":\"dog\", \"says\":\"woof\"}", br.readLine() ); - br.close(); - fis.close(); - } + + File file1= null; + file1 = new File("src/test/java/com/marklogic/javaclient/data/" + docId[0]); + FileInputStream fis = new FileInputStream(file1); + InputStreamHandle handle1 = new InputStreamHandle(fis); + handle1.setFormat(Format.BINARY); + + writeset.add("/generic/"+docId[0],handle1); + + 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",jh); + + String foo1 = "This is foo1 of byte Array"; + byte[] ba = foo1.getBytes(); + BytesHandle bh = new BytesHandle(ba); + bh.setFormat(Format.TEXT); + + writeset.add("/generic/foo1.txt",bh); + + DOMSource ds = new DOMSource(getDocumentContent("This is so foo1")); + SourceHandle sh = new SourceHandle(); + sh.set(ds); + sh.setFormat(Format.XML); + + writeset.add("/generic/foo.xml",sh); + + docMgr.write(writeset); + + FileHandle rh = new FileHandle(); + + docMgr.read("/generic/"+docId[0],rh); + assertEquals("Size of the File /generic/"+docId[0],file1.length(),rh.get().length()); + System.out.println(rh.get().getName()+":"+rh.get().length()+"\n"); + + docMgr.read("/generic/foo.xml",rh); + BufferedReader br=new BufferedReader(new FileReader(rh.get())); + br.readLine(); + assertEquals("xml document write difference", "This is so foo1", br.readLine() ); + docMgr.read("/generic/foo1.txt",rh); + br.close(); + br=new BufferedReader(new FileReader(rh.get())); + assertEquals("txt document write difference", foo1, br.readLine() ); + br.close(); + docMgr.read("/generic/dog.json",rh); + br=new BufferedReader(new FileReader(rh.get())); + assertEquals("Json document write difference", "{\"animal\":\"dog\", \"says\":\"woof\"}", br.readLine() ); + br.close(); + fis.close(); + } + + @Test + public void testWriteMultipleJacksonPoJoDocs() throws Exception + { + String docId[] ={"/jack/iphone.json","/jack/ipad.json","/jack/ipod.json"}; + Product product1 = new Product(); + product1.setName("iPhone"); + product1.setIndustry("Hardware"); + product1.setDescription("Very cool Iphone"); + Product product2 = new Product(); + product2.setName("iPad"); + product2.setIndustry("Hardware"); + product2.setDescription("Very cool Ipad"); + Product product3 = new Product(); + product3.setName("iPod"); + product3.setIndustry("Hardware"); + product3.setDescription("Very cool Ipod"); + JacksonHandle writeHandle = new JacksonHandle(); + JsonNode writeDocument = writeHandle.getMapper().convertValue(product1, JsonNode.class); + writeHandle.set(writeDocument); + JsonNode writeDocument2 = writeHandle.getMapper().convertValue(product2, JsonNode.class); + JsonNode writeDocument3 = writeHandle.getMapper().convertValue(product3, JsonNode.class); + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + + writeset.add(docId[0],writeHandle); + writeset.add(docId[1],new JacksonHandle().with(writeDocument2)); + writeset.add(docId[2],new JacksonHandle().with(writeDocument3)); + docMgr.write(writeset); + + JacksonHandle jh = new JacksonHandle(); + docMgr.read(docId[0], jh); + String exp="{\"name\":\"iPhone\",\"industry\":\"Hardware\",\"description\":\"Very cool Iphone\"}"; + JSONAssert.assertEquals(exp,jh.get().toString() , false); + + docMgr.read(docId[1], jh); + exp="{\"name\":\"iPad\",\"industry\":\"Hardware\",\"description\":\"Very cool Ipad\"}"; + JSONAssert.assertEquals(exp,jh.get().toString() , false); + + docMgr.read(docId[2], jh); + exp="{\"name\":\"iPod\",\"industry\":\"Hardware\",\"description\":\"Very cool Ipod\"}"; + JSONAssert.assertEquals(exp,jh.get().toString() , false); + + } @AfterClass public static void tearDown() throws Exception { - System.out.println("In tear down" ); - tearDownJavaRESTServer(dbName, fNames, restServerName); + System.out.println("In tear down" ); + tearDownJavaRESTServer(dbName, fNames, restServerName); } } \ No newline at end of file From d79eda160b961bfe619cdfa5ac4190feb5c2f6cc Mon Sep 17 00:00:00 2001 From: sravan Date: Mon, 15 Sep 2014 19:19:12 -0700 Subject: [PATCH 105/357] updated with a test case for the issue 92 --- .../TestBulkReadWriteMetaDataChange.java | 85 ++++++++++++++++--- 1 file changed, 73 insertions(+), 12 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteMetaDataChange.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteMetaDataChange.java index f3aeddde4..aa0836687 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteMetaDataChange.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteMetaDataChange.java @@ -12,7 +12,9 @@ import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; +import org.skyscreamer.jsonassert.JSONAssert; +import com.fasterxml.jackson.databind.JsonNode; import com.marklogic.client.DatabaseClient; import com.marklogic.client.DatabaseClientFactory; import com.marklogic.client.Transaction; @@ -21,6 +23,7 @@ import com.marklogic.client.document.DocumentPage; import com.marklogic.client.document.DocumentRecord; import com.marklogic.client.document.DocumentWriteSet; +import com.marklogic.client.document.JSONDocumentManager; import com.marklogic.client.document.TextDocumentManager; import com.marklogic.client.io.DocumentMetadataHandle; import com.marklogic.client.io.DocumentMetadataHandle.Capability; @@ -28,6 +31,7 @@ import com.marklogic.client.io.DocumentMetadataHandle.DocumentPermissions; import com.marklogic.client.io.DocumentMetadataHandle.DocumentProperties; import com.marklogic.client.io.Format; +import com.marklogic.client.io.JacksonHandle; import com.marklogic.client.io.StringHandle; /** @@ -51,8 +55,9 @@ public class TestBulkReadWriteMetaDataChange extends BasicJavaClientREST { @BeforeClass public static void setUpBeforeClass() throws Exception { System.out.println("In Setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); - createRESTUser("app-user", "password", "rest-writer","rest-reader" ); + +// setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); +// createRESTUser("app-user", "password", "rest-writer","rest-reader" ); } /** @@ -61,8 +66,8 @@ public static void setUpBeforeClass() throws Exception { @AfterClass public static void tearDownAfterClass() throws Exception { System.out.println("In tear down" ); - tearDownJavaRESTServer(dbName, fNames, restServerName); - deleteRESTUser("app-user"); +// tearDownJavaRESTServer(dbName, fNames, restServerName); +// deleteRESTUser("app-user"); } /** @@ -238,7 +243,7 @@ public void validateUpdatedMetadataCollections(DocumentMetadataHandle mh) { * Verified by reading individual documents. User does not have permission to update the meta-data. */ - @Test(expected=com.marklogic.client.ForbiddenUserException.class) + @Test public void testWriteMultipleTextDocWithChangedMetadataProperties() { String docId[] = {"/foo/test/myFoo1.txt","/foo/test/myFoo2.txt","/foo/test/myFoo3.txt"}; @@ -247,28 +252,30 @@ public void testWriteMultipleTextDocWithChangedMetadataProperties() { DocumentWriteSet writeset = docMgr.newWriteSet(); // put metadata DocumentMetadataHandle mh = setMetadata(); - DocumentMetadataHandle mhRead = null; - + DocumentMetadataHandle mhRead = new DocumentMetadataHandle(); + writeset.addDefault(mh); writeset.add(docId[0], new StringHandle().with("This is so foo1")); writeset.add(docId[1], new StringHandle().with("This is so foo2")); writeset.add(docId[2], new StringHandle().with("This is so foo3")); docMgr.write(writeset); + StringHandle sh=docMgr.read(docId[0],new StringHandle()); + System.out.println(sh.get()); DocumentPage page = docMgr.read(docId); - while(page.hasNext()){ DocumentRecord rec = page.next(); + System.out.println(rec.getUri()); docMgr.readMetadata(rec.getUri(), mhRead); - validateMetadata(mh); + validateMetadata(mhRead); } - validateMetadata(mh); + validateMetadata(mhRead); // Add new meta-data DocumentMetadataHandle mhUpdated = setUpdatedMetadataProperties(); writeset.addDefault(mhUpdated); docMgr.write(writeset); - DocumentMetadataHandle mhUpd = null; + DocumentMetadataHandle mhUpd = new DocumentMetadataHandle() ; for(String docURI : docId){ docMgr.readMetadata(docURI, mhUpd); @@ -276,6 +283,60 @@ public void testWriteMultipleTextDocWithChangedMetadataProperties() { } validateUpdatedMetadataProperties(mhUpd); } + @Test + public void testWriteMultipleJacksonPoJoDocsWithMetadata() throws Exception + { + String docId[] ={"/jack/iphone.json","/jack/ipad.json","/jack/ipod.json"}; + Product product1 = new Product(); + product1.setName("iPhone"); + product1.setIndustry("Hardware"); + product1.setDescription("Very cool Iphone"); + Product product2 = new Product(); + product2.setName("iPad"); + product2.setIndustry("Hardware"); + product2.setDescription("Very cool Ipad"); + Product product3 = new Product(); + product3.setName("iPod"); + product3.setIndustry("Hardware"); + product3.setDescription("Very cool Ipod"); + + DocumentMetadataHandle mh = setMetadata(); + DocumentMetadataHandle mhRead = new DocumentMetadataHandle(); + + JacksonHandle writeHandle = new JacksonHandle(); + JsonNode writeDocument = writeHandle.getMapper().convertValue(product1, JsonNode.class); + writeHandle.set(writeDocument); + JsonNode writeDocument2 = writeHandle.getMapper().convertValue(product2, JsonNode.class); + JsonNode writeDocument3 = writeHandle.getMapper().convertValue(product3, JsonNode.class); + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + DocumentWriteSet writeset =docMgr.newWriteSet(); + + writeset.addDefault(mh); + writeset.add(docId[0],writeHandle); + writeset.add(docId[1],new JacksonHandle().with(writeDocument2)); + DocumentMetadataHandle mhUpdated = setUpdatedMetadataCollections(); + writeset.add(docId[2],mhUpdated,new JacksonHandle().with(writeDocument3)); + docMgr.write(writeset); + + JacksonHandle jh = new JacksonHandle(); + docMgr.read(docId[0], jh); + String exp="{\"name\":\"iPhone\",\"industry\":\"Hardware\",\"description\":\"Very cool Iphone\"}"; + JSONAssert.assertEquals(exp,jh.get().toString() , false); + docMgr.readMetadata(docId[0], mhRead); + validateMetadata(mhRead); + + docMgr.read(docId[1], jh); + exp="{\"name\":\"iPad\",\"industry\":\"Hardware\",\"description\":\"Very cool Ipad\"}"; + JSONAssert.assertEquals(exp,jh.get().toString() , false); + docMgr.readMetadata(docId[1], mhRead); + validateMetadata(mhRead); + + docMgr.read(docId[2], jh); + exp="{\"name\":\"iPod\",\"industry\":\"Hardware\",\"description\":\"Very cool Ipod\"}"; + JSONAssert.assertEquals(exp,jh.get().toString() , false); + docMgr.readMetadata(docId[2], mhRead); + this.validateUpdatedMetadataCollections(mhRead); + } /* * Purpose: To validate: DocumentManager::read(Transaction, uri....) @@ -284,7 +345,7 @@ public void testWriteMultipleTextDocWithChangedMetadataProperties() { * read method performs the bulk read */ @Test - public void testBulkReadUsingMultipleUri() { + public void testBulkReadUsingMultipleUri() throws Exception { String docId[] = {"/foo/test/transactionURIFoo1.txt","/foo/test/transactionURIFoo2.txt","/foo/test/transactionURIFoo3.txt"}; Transaction transaction = client.openTransaction(); try { From b7847b979f93b5c42ddc9fecde6de219f0490c15 Mon Sep 17 00:00:00 2001 From: sammefford Date: Tue, 16 Sep 2014 09:26:58 -0600 Subject: [PATCH 106/357] somehow this had the wrong version number committed --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index dd24ccff1..637b42985 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.marklogic client-api-java jar - 3.0.0-EA2 + 3.0-SNAPSHOT client-api-java http://developer.marklogic.com From b9f593761d402dfac732f966e755c4ccd7f11b08 Mon Sep 17 00:00:00 2001 From: sravan Date: Wed, 17 Sep 2014 00:49:41 -0700 Subject: [PATCH 107/357] adding POJO test for qbe,combined query definition search --- .../javaclient/TestPOJOwithQBEQueryDef.java | 309 ++++++++++++++++++ 1 file changed, 309 insertions(+) create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/TestPOJOwithQBEQueryDef.java diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOwithQBEQueryDef.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOwithQBEQueryDef.java new file mode 100644 index 000000000..dabd46583 --- /dev/null +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOwithQBEQueryDef.java @@ -0,0 +1,309 @@ +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.IOException; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.JacksonHandle; +import com.marklogic.client.io.SearchHandle; +import com.marklogic.client.io.StringHandle; +import com.marklogic.client.pojo.PojoPage; +import com.marklogic.client.pojo.PojoRepository; +import com.marklogic.client.query.MatchDocumentSummary; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.RawCombinedQueryDefinition; +import com.marklogic.client.query.RawQueryByExampleDefinition; +import com.marklogic.client.query.RawStructuredQueryDefinition; +import com.marklogic.client.query.StructuredQueryBuilder; +import com.marklogic.client.query.StructuredQueryDefinition; +import com.marklogic.client.query.StructuredQueryBuilder.Operator; + +public class TestPOJOwithQBEQueryDef extends BasicJavaClientREST { + private static String dbName = "TestPOJOqbeQDSearchDB"; + private static String [] fNames = {"TestPOJOqbeQDSearchDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client ; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { +// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + System.out.println("In setup"); +// setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); +// addRangePathIndex(dbName, "string", "com.marklogic.javaclient.Artifact/manufacturer/com.marklogic.javaclient.Company/name", "http://marklogic.com/collation/", "ignore"); +// BasicJavaClientREST.addRangeElementIndex(dbName, "long", "", "inventory"); + BasicJavaClientREST.addRangeElementIndex(dbName, "long", "", "id"); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + System.out.println("In tear down" ); +// tearDownJavaRESTServer(dbName, fNames, restServerName); + } + @Before + public void setUp() throws Exception { + client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST); + } + @After + public void tearDown() throws Exception { + // release client + client.release(); + } + + public Artifact getArtifact(int counter){ + + Artifact cogs = new Artifact(); + cogs.setId(counter); + if( counter % 5 == 0){ + cogs.setName("Cogs special"); + if(counter % 2 ==0){ + Company acme = new Company(); + acme.setName("Acme special, Inc."); + acme.setWebsite("http://www.acme special.com"); + acme.setLatitude(41.998+counter); + acme.setLongitude(-87.966+counter); + cogs.setManufacturer(acme); + + }else{ + Company widgets = new Company(); + widgets.setName("Widgets counter Inc."); + widgets.setWebsite("http://www.widgets counter.com"); + widgets.setLatitude(41.998+counter); + widgets.setLongitude(-87.966+counter); + cogs.setManufacturer(widgets); + } + }else{ + cogs.setName("Cogs "+counter); + if(counter % 2 ==0){ + Company acme = new Company(); + acme.setName("Acme "+counter+", Inc."); + acme.setWebsite("http://www.acme"+counter+".com"); + acme.setLatitude(41.998+counter); + acme.setLongitude(-87.966+counter); + cogs.setManufacturer(acme); + + }else{ + Company widgets = new Company(); + widgets.setName("Widgets "+counter+", Inc."); + widgets.setWebsite("http://www.widgets"+counter+".com"); + widgets.setLatitude(41.998+counter); + widgets.setLongitude(-87.966+counter); + cogs.setManufacturer(widgets); + } + } + cogs.setInventory(1000+counter); + return cogs; + } + public void validateArtifact(Artifact art) + { + assertNotNull("Artifact object should never be Null",art); + assertNotNull("Id should never be Null",art.id); + assertTrue("Inventry is always greater than 1000", art.getInventory()>1000); + } + public void loadSimplePojos(PojoRepository products) + { + for(int i=1;i<111;i++){ + if(i%2==0){ + products.write(this.getArtifact(i),"even","numbers"); + } + else { + products.write(this.getArtifact(i),"odd","numbers"); + } + } + } + @Test + public void testPOJOqbeSearchWithoutSearchHandle() { + PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); + PojoPage p; + this.loadSimplePojos(products); + QueryManager queryMgr = client.newQueryManager(); + String queryAsString = + "{\"$query\":{" + + "\"$and\":[{\"name\":{\"$word\":\"cogs\",\"$exact\": false}}]" + + ",\"$not\":[{\"name\":{\"$word\":\"special\",\"$exact\": false}}]" + + "}}"; + + RawQueryByExampleDefinition qd = queryMgr.newRawQueryByExampleDefinition(new StringHandle(queryAsString).withFormat(Format.JSON)); + qd.setCollections("odd"); + products.setPageLength(11); + p = products.search(qd, 1); + assertEquals("total no of pages",4,p.getTotalPages()); +// System.out.println(p.getTotalPages()); + long pageNo=1,count=0; + do{ + count =0; + + p = products.search(qd,pageNo); + + while(p.iterator().hasNext()){ + Artifact a =p.iterator().next(); + validateArtifact(a); + assertFalse("Verifying document with special is not there",a.getId()%5==0); + assertTrue("Artifact Id is odd", a.getId()%2!=0); + assertTrue("Company name contains widgets",a.getManufacturer().getName().contains("Widgets")); + count++; + // System.out.println(a.getId()+" "+a.getManufacturer().getName() +" "+count); + } + assertEquals("Page size",count,p.size()); + pageNo=pageNo+p.getPageSize(); + }while(!p.isLastPage() && pageNo products = client.newPojoRepository(Artifact.class, Long.class); + PojoPage p; + this.loadSimplePojos(products); + + QueryManager queryMgr = client.newQueryManager(); + String queryAsString = + "{\"$query\":{" + + "\"$and\":[{\"inventory\":{\"$gt\":1010}},{\"inventory\":{\"$le\":1110}}]" + + ",\"$filtered\": true}}"; + System.out.println(queryAsString); + RawQueryByExampleDefinition qd = queryMgr.newRawQueryByExampleDefinition(new StringHandle(queryAsString).withFormat(Format.JSON)); + qd.setCollections("even"); + SearchHandle results = new SearchHandle(); + products.setPageLength(10); + p = products.search(qd, 1,results); + assertEquals("total no of pages",5,p.getTotalPages()); + System.out.println(p.getTotalPages()); + long pageNo=1,count=0; + do{ + count =0; + p = products.search(qd,pageNo,results); + + while(p.iterator().hasNext()){ + Artifact a =p.iterator().next(); + validateArtifact(a); + assertTrue("Enventory lies between 1010 to 1110", a.getInventory()>1010 && a.getInventory()<=1110); + assertTrue("Artifact Id is even", a.getId()%2==0); + assertTrue("Company name contains Acme",a.getManufacturer().getName().contains("Acme")); + count++; + // System.out.println(a.getId()+" "+a.getManufacturer().getName() +" "+count); + } + assertEquals("Page size",count,p.size()); + pageNo=pageNo+p.getPageSize(); + MatchDocumentSummary[] mds =results.getMatchResults(); + assertEquals("Size of the results summary",10,mds.length); + for(MatchDocumentSummary md:mds){ + assertTrue("every uri should contain the class name",md.getUri().contains("Artifact")); + } + String[] facetNames = results.getFacetNames(); + for(String fname:facetNames){ + System.out.println(fname); + } +// assertEquals("Total results from search handle ",50,results.getTotalResults()); + assertTrue("Search Handle metric results ",results.getMetrics().getTotalTime()>0); + }while(!p.isLastPage() && pageNo products = client.newPojoRepository(Artifact.class, Long.class); + PojoPage p; + this.loadSimplePojos(products); + QueryManager queryMgr = client.newQueryManager(); + String queryAsString ="{\"search\":{\"query\":{\"and-query\":[" + + "{\"word-constraint-query\":{\"constraint-name\":\"pojo-name-field\", \"text\":\"Acme\"}}," + + "{\"word-constraint-query\":{\"constraint-name\":\"pojo-name-field\", \"text\":\"special\"}}]}," + + "\"options\":{\"constraint\":{\"name\":\"pojo-name-field\", \"word\":{\"json-property\":\"name\"}}}" + + "}}"; + + RawCombinedQueryDefinition qd = queryMgr.newRawCombinedQueryDefinition(new StringHandle(queryAsString).withFormat(Format.JSON)); + JacksonHandle results = new JacksonHandle(); + p = products.search(qd, 1,results); + products.setPageLength(11); + assertEquals("total no of pages",1,p.getTotalPages()); +// System.out.println(p.getTotalPages()+results.get().toString()); + long pageNo=1,count=0; + do{ + count =0; + p = products.search(qd,pageNo,results); + + while(p.iterator().hasNext()){ + Artifact a =p.iterator().next(); + validateArtifact(a); + count++; + assertTrue("Manufacture name starts with acme",a.getManufacturer().getName().contains("Acme")); + assertTrue("Artifact name contains",a.getName().contains("special")); + + } + assertEquals("Page size",count,p.size()); + pageNo=pageNo+p.getPageSize(); + + assertEquals("Page start from search handls vs page methods",results.get().get("start").asLong(),p.getStart() ); + assertEquals("Format in the search handle","json",results.get().withArray("results").get(1).path("format").asText()); + assertTrue("Uri in search handle contains Artifact",results.get().withArray("results").get(1).path("uri").asText().contains("Artifact")); + // System.out.println(results.get().toString()); + }while(!p.isLastPage() && pageNo products = client.newPojoRepository(Artifact.class, Long.class); + PojoPage p; + this.loadSimplePojos(products); +// StructuredQueryBuilder qb = new StructuredQueryBuilder(); +// StructuredQueryDefinition qd =qb.value(qb.jsonProperty("id"), 5,10,15,20,25,30); +// StructuredQueryDefinition qd = qb.and(q1,qb.range(qb.pathIndex("com.marklogic.javaclient.Artifact/inventory"), "xs:long",Operator.LE, 1110),qb.collection("even")); + QueryManager queryMgr = client.newQueryManager(); + String queryAsString = "{\"search\":{\"query\":{" + + "\"range-constraint-query\":{\"constraint-name\":\"id\", \"value\":[5,10,15,20,25,30]}}," + + "\"options\":{\"return-metrics\":false, \"constraint\":{\"name\":\"id\", \"range\":{\"type\": \"xs:string\",\"json-property\":\"id\"}}}" + + "}}"; + RawCombinedQueryDefinition qd = queryMgr.newRawCombinedQueryDefinition(new StringHandle(queryAsString).withFormat(Format.JSON)); + StringHandle results = new StringHandle(); + JacksonHandle jh = new JacksonHandle(); + p = products.search(qd, 1,jh); + + long pageNo=1,count=0; + do{ + count =0; + p = products.search(qd,pageNo,results.withFormat(Format.JSON)); + + while(p.iterator().hasNext()){ + Artifact a =p.iterator().next(); + validateArtifact(a); + count++; + } + assertEquals("Page total results",count,p.getTotalSize()); + pageNo=pageNo+p.getPageSize(); + System.out.println(results.get().toString()); + }while(!p.isLastPage() && pageNo Date: Wed, 17 Sep 2014 08:36:20 -0600 Subject: [PATCH 108/357] refactor based on feedback--only stubs for now, but compiling --- .../marklogic/client/eval/EvalBuilder.java | 32 +++++++++ .../com/marklogic/client/eval/EvalResult.java | 66 +++++++++++++++++++ .../marklogic/client/eval/EvalResults.java | 24 +++++++ .../client/eval/JavascriptEvalBuilder.java | 26 ++++++++ .../client/eval/XQueryEvalBuilder.java | 25 +++++++ 5 files changed, 173 insertions(+) create mode 100644 src/main/java/com/marklogic/client/eval/EvalBuilder.java create mode 100644 src/main/java/com/marklogic/client/eval/EvalResult.java create mode 100644 src/main/java/com/marklogic/client/eval/EvalResults.java create mode 100644 src/main/java/com/marklogic/client/eval/JavascriptEvalBuilder.java create mode 100644 src/main/java/com/marklogic/client/eval/XQueryEvalBuilder.java diff --git a/src/main/java/com/marklogic/client/eval/EvalBuilder.java b/src/main/java/com/marklogic/client/eval/EvalBuilder.java new file mode 100644 index 000000000..36e283c65 --- /dev/null +++ b/src/main/java/com/marklogic/client/eval/EvalBuilder.java @@ -0,0 +1,32 @@ +/* + * Copyright 2012-2014 MarkLogic Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.marklogic.client.eval; + +import com.marklogic.client.Transaction; +import com.marklogic.client.io.marker.AbstractWriteHandle; + +public interface EvalBuilder { + public EvalBuilder addVariable(String name, String value); + public EvalBuilder addVariable(String name, Number value); + public EvalBuilder addVariable(String name, Boolean value); + public EvalBuilder addVariable(String name, AbstractWriteHandle value); + /** Like other *As convenience methods throughout the API, the Object value + * is managed by the Handle registered for that Class. */ + public EvalBuilder addVariableAs(String name, Object value); + public EvalBuilder database(String database); + public EvalBuilder transaction(Transaction transaction); + public T build(); +} diff --git a/src/main/java/com/marklogic/client/eval/EvalResult.java b/src/main/java/com/marklogic/client/eval/EvalResult.java new file mode 100644 index 000000000..6eb7050ef --- /dev/null +++ b/src/main/java/com/marklogic/client/eval/EvalResult.java @@ -0,0 +1,66 @@ +/* + * Copyright 2012-2014 MarkLogic Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.marklogic.client.eval; + +import com.marklogic.client.io.marker.AbstractReadHandle; + +public interface EvalResult { + enum Type { + STRING, + NUMBER, + BOOLEAN, + ARRAY, + OBJECT, + NULL, + XDM_ATOMIC, + XDM_ATTRIBUTE, + XDM_BINARY, + XDM_COMMENT, + XDM_DOCUMENT, + XDM_DURATION, + XDM_ELEMENT, + XDM_ITEM, + XDM_NODE, + XDM_PROCESSINGINSTRUCTION, + XDM_SEQUENCE, + XDM_TEXT, + XDM_VALUE, + XDM_VARIABLE, + XS_ANYURI, + XS_BASE64BINARY, + XS_BOOLEAN, + XS_DATE, + XS_DATETIME, + XS_DAYTIMEDURATION, + XS_DECIMAL, + XS_DOUBLE, + XS_DURATION, + XS_FLOAT, + XS_GDAY, + XS_GMONTH, + XS_GMONTHDAY, + XS_GYEAR, + XS_GYEARMONTH, + XS_HEXBINARY, + XS_INTEGER, + XS_QNAME, + XS_STRING, + XS_TIME + }; + public Type getType(); + public H get(H handle); + public T getAs(Class clazz); +} diff --git a/src/main/java/com/marklogic/client/eval/EvalResults.java b/src/main/java/com/marklogic/client/eval/EvalResults.java new file mode 100644 index 000000000..9106cece1 --- /dev/null +++ b/src/main/java/com/marklogic/client/eval/EvalResults.java @@ -0,0 +1,24 @@ +/* + * Copyright 2012-2014 MarkLogic Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.marklogic.client.eval; + +import java.util.Iterator; + +public interface EvalResults extends Iterable, Iterator { + public Iterator iterator(); + public boolean hasNext(); + public EvalResult next(); +} diff --git a/src/main/java/com/marklogic/client/eval/JavascriptEvalBuilder.java b/src/main/java/com/marklogic/client/eval/JavascriptEvalBuilder.java new file mode 100644 index 000000000..6fb452dc7 --- /dev/null +++ b/src/main/java/com/marklogic/client/eval/JavascriptEvalBuilder.java @@ -0,0 +1,26 @@ +/* + * Copyright 2012-2014 MarkLogic Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.marklogic.client.eval; + +import com.marklogic.client.io.marker.TextWriteHandle; + +public interface JavascriptEvalBuilder extends EvalBuilder { + public EvalBuilder javascript(String javascript); + public EvalBuilder javascript(TextWriteHandle javascript); +} + + + diff --git a/src/main/java/com/marklogic/client/eval/XQueryEvalBuilder.java b/src/main/java/com/marklogic/client/eval/XQueryEvalBuilder.java new file mode 100644 index 000000000..156a738b3 --- /dev/null +++ b/src/main/java/com/marklogic/client/eval/XQueryEvalBuilder.java @@ -0,0 +1,25 @@ +/* + * Copyright 2012-2014 MarkLogic Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.marklogic.client.eval; + +import com.marklogic.client.io.marker.TextWriteHandle; + +public interface XQueryEvalBuilder extends EvalBuilder { + public XQueryEvalBuilder xquery(String xquery); + public XQueryEvalBuilder xquery(TextWriteHandle xquery); +} + + From 5ceafe1aad7a8b930fcb8695d178a763fa8d0706 Mon Sep 17 00:00:00 2001 From: sammefford Date: Wed, 17 Sep 2014 08:36:26 -0600 Subject: [PATCH 109/357] refactor based on feedback--only stubs for now, but compiling --- pom.xml | 26 ++- .../com/marklogic/client/DatabaseClient.java | 5 +- src/main/java/com/marklogic/client/Page.java | 1 - .../client/eval/JSONVariableSet.java | 2 + .../com/marklogic/client/eval/ServerEval.java | 31 +-- .../marklogic/client/eval/XMLVariableSet.java | 2 + .../com/marklogic/client/http/BodyPart.java | 3 +- .../com/marklogic/client/impl/BasicPage.java | 4 + .../client/impl/DatabaseClientImpl.java | 14 ++ .../client/impl/DocumentPageImpl.java | 2 +- .../marklogic/client/io/DOMNodeHandle.java | 45 +++- .../client/io/JAXBDatatypeHandle.java | 96 ++++++++- .../marklogic/client/io/XQueryTypeHandle.java | 203 ++++++++++-------- .../com/marklogic/client/test/EvalTest.java | 125 ++++++----- 14 files changed, 367 insertions(+), 192 deletions(-) diff --git a/pom.xml b/pom.xml index f0eecc899..2d9d5dad1 100644 --- a/pom.xml +++ b/pom.xml @@ -166,13 +166,35 @@ org.glassfish.jersey.core jersey-client - 2.11 + 2.12 org.glassfish.jersey.media jersey-media-multipart - 2.11 + 2.12 + + com.sun.jersey + jersey-client + 1.17 + + + com.sun.jersey.contribs + jersey-apache-client4 + 1.17 + + + com.sun.jersey.contribs + jersey-multipart + 1.17 + + ch.qos.logback logback-classic diff --git a/src/main/java/com/marklogic/client/DatabaseClient.java b/src/main/java/com/marklogic/client/DatabaseClient.java index b7f8a6661..5cb502fc8 100644 --- a/src/main/java/com/marklogic/client/DatabaseClient.java +++ b/src/main/java/com/marklogic/client/DatabaseClient.java @@ -24,7 +24,9 @@ import com.marklogic.client.document.JSONDocumentManager; import com.marklogic.client.document.TextDocumentManager; import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.eval.JavascriptEvalBuilder; import com.marklogic.client.eval.ServerEval; +import com.marklogic.client.eval.XQueryEvalBuilder; import com.marklogic.client.extensions.ResourceManager; import com.marklogic.client.admin.ExtensionMetadata; import com.marklogic.client.query.QueryManager; @@ -178,5 +180,6 @@ public T init(String resourceName, T resourceManager * @return the object implementing communication with the server */ public Object getClientImplementation(); - public ServerEval newServerEval(); + public XQueryEvalBuilder newXQueryEvalBuilder(); + public JavascriptEvalBuilder newJavascriptEvalBuilder(); } diff --git a/src/main/java/com/marklogic/client/Page.java b/src/main/java/com/marklogic/client/Page.java index f561f8fea..1f735ccfe 100644 --- a/src/main/java/com/marklogic/client/Page.java +++ b/src/main/java/com/marklogic/client/Page.java @@ -16,7 +16,6 @@ package com.marklogic.client; import java.util.Iterator; -import java.util.Iterable; /** A generic interface for pagination through large sets of items of type <T>. */ public interface Page extends Iterator, Iterable { diff --git a/src/main/java/com/marklogic/client/eval/JSONVariableSet.java b/src/main/java/com/marklogic/client/eval/JSONVariableSet.java index 290819cbd..18ab03cfd 100644 --- a/src/main/java/com/marklogic/client/eval/JSONVariableSet.java +++ b/src/main/java/com/marklogic/client/eval/JSONVariableSet.java @@ -15,6 +15,8 @@ */ package com.marklogic.client.eval; +import com.marklogic.client.io.marker.JSONWriteHandle; + public interface JSONVariableSet extends VariableSet { public JSONVariableSet add(String name, JSONWriteHandle value); public JSONVariableSet addAs(String name, Object value); diff --git a/src/main/java/com/marklogic/client/eval/ServerEval.java b/src/main/java/com/marklogic/client/eval/ServerEval.java index ba9580952..7b56a33c2 100644 --- a/src/main/java/com/marklogic/client/eval/ServerEval.java +++ b/src/main/java/com/marklogic/client/eval/ServerEval.java @@ -15,31 +15,10 @@ */ package com.marklogic.client.eval; +import com.marklogic.client.io.marker.AbstractReadHandle; + public interface ServerEval { - public T evalXQueryAs(Class responseType, TextWriteHandle xquery, VariableSet vars, - String database, Transaction transaction, TransactionMode mode); - public T evalJavascriptAs(Class responseType, TextWriteHandle xquery, JSONVariableSet vars, - String database, Transaction transaction, TransactionMode mode); - public T evalXQueryAs(Class responseType, String xquery, VariableSet vars, - String database, Transaction transaction, TransactionMode mode); - public T evalJavascriptAs(Class responseType, String xquery, JSONVariableSet vars, - String database, Transaction transaction, TransactionMode mode); - public H evalXQuery(H responseHandle, TextWriteHandle xquery, VariableSet vars, - String database, Transaction transaction, TransactionMode mode); - public H evalJavascript(H responseHandle, TextWriteHandle xquery, JSONVariableSet vars, - String database, Transaction transaction, TransactionMode mode); - public H evalXQuery(H responseHandle, String xquery, VariableSet vars, - String database, Transaction transaction, TransactionMode mode); - public H evalJavascript(H responseHandle, String xquery, JSONVariableSet vars, - String database, Transaction transaction, TransactionMode mode); - public MultiPartResponsePage evalXQuery(TextWriteHandle xquery, VariableSet vars, - String database, Transaction transaction, TransactionMode mode); - public MultiPartResponsePage evalJavascript(TextWriteHandle xquery, JSONVariableSet vars, - String database, Transaction transaction, TransactionMode mode); - public MultiPartResponsePage evalXQuery(String xquery, VariableSet vars, - String database, Transaction transaction, TransactionMode mode); - public MultiPartResponsePage evalJavascript(String xquery, JSONVariableSet vars, - String database, Transaction transaction, TransactionMode mode); - public JSONVariableSet newJSONVariableSet(); - public XMLVariableSet newXMLVariableSet(); + public T evalAs(Class responseType); + public H eval(H responseHandle); + public EvalResults eval(); } diff --git a/src/main/java/com/marklogic/client/eval/XMLVariableSet.java b/src/main/java/com/marklogic/client/eval/XMLVariableSet.java index 43a467780..30eeef6a4 100644 --- a/src/main/java/com/marklogic/client/eval/XMLVariableSet.java +++ b/src/main/java/com/marklogic/client/eval/XMLVariableSet.java @@ -15,6 +15,8 @@ */ package com.marklogic.client.eval; +import com.marklogic.client.io.marker.XMLWriteHandle; + public interface XMLVariableSet extends VariableSet { public XMLVariableSet add(String name, XMLWriteHandle value); public XMLVariableSet addAs(String name, Object value); diff --git a/src/main/java/com/marklogic/client/http/BodyPart.java b/src/main/java/com/marklogic/client/http/BodyPart.java index 48d252616..f0ac380e0 100644 --- a/src/main/java/com/marklogic/client/http/BodyPart.java +++ b/src/main/java/com/marklogic/client/http/BodyPart.java @@ -17,6 +17,8 @@ import java.util.Map; +import com.marklogic.client.io.marker.AbstractReadHandle; + public interface BodyPart { //Map> getHeaders(); /** @@ -61,4 +63,3 @@ public interface BodyPart { public H get(H handle); public T getAs(Class clazz); } - diff --git a/src/main/java/com/marklogic/client/impl/BasicPage.java b/src/main/java/com/marklogic/client/impl/BasicPage.java index a56e43375..db318fd26 100644 --- a/src/main/java/com/marklogic/client/impl/BasicPage.java +++ b/src/main/java/com/marklogic/client/impl/BasicPage.java @@ -40,6 +40,10 @@ public BasicPage(Iterable iterable, long start, long pageSize, long totalSize public Iterator iterator() { return iterable.iterator(); } + + protected Iterator internalIterator() { + return iterator; + } public boolean hasNext() { return iterator.hasNext(); diff --git a/src/main/java/com/marklogic/client/impl/DatabaseClientImpl.java b/src/main/java/com/marklogic/client/impl/DatabaseClientImpl.java index cea7ec161..322aeb9b9 100644 --- a/src/main/java/com/marklogic/client/impl/DatabaseClientImpl.java +++ b/src/main/java/com/marklogic/client/impl/DatabaseClientImpl.java @@ -30,6 +30,8 @@ import com.marklogic.client.document.JSONDocumentManager; import com.marklogic.client.query.QueryManager; import com.marklogic.client.util.RequestLogger; +import com.marklogic.client.eval.JavascriptEvalBuilder; +import com.marklogic.client.eval.XQueryEvalBuilder; import com.marklogic.client.extensions.ResourceManager; import com.marklogic.client.DatabaseClientFactory.HandleFactoryRegistry; import com.marklogic.client.admin.ServerConfigurationManager; @@ -182,4 +184,16 @@ public Object getClientImplementation() { public RESTServices getServices() { return services; } + + @Override + public XQueryEvalBuilder newXQueryEvalBuilder() { + // TODO Auto-generated method stub + return null; + } + + @Override + public JavascriptEvalBuilder newJavascriptEvalBuilder() { + // TODO Auto-generated method stub + return null; + } } diff --git a/src/main/java/com/marklogic/client/impl/DocumentPageImpl.java b/src/main/java/com/marklogic/client/impl/DocumentPageImpl.java index bcdf2256d..e5b2b6a46 100644 --- a/src/main/java/com/marklogic/client/impl/DocumentPageImpl.java +++ b/src/main/java/com/marklogic/client/impl/DocumentPageImpl.java @@ -31,6 +31,6 @@ public DocumentPageImpl(Iterable iterable, long start, long size } public T nextContent(T contentHandle) { - return iterator.next().getContent(contentHandle); + return internalIterator().next().getContent(contentHandle); } } diff --git a/src/main/java/com/marklogic/client/io/DOMNodeHandle.java b/src/main/java/com/marklogic/client/io/DOMNodeHandle.java index b68a777b4..6a37e9866 100644 --- a/src/main/java/com/marklogic/client/io/DOMNodeHandle.java +++ b/src/main/java/com/marklogic/client/io/DOMNodeHandle.java @@ -15,8 +15,33 @@ */ package com.marklogic.client.io; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.DOMException; import org.w3c.dom.Node; +import org.w3c.dom.ls.DOMImplementationLS; +import org.w3c.dom.ls.LSException; +import org.w3c.dom.ls.LSInput; +import org.w3c.dom.ls.LSOutput; +import org.w3c.dom.ls.LSParser; + +import com.marklogic.client.MarkLogicIOException; +import com.marklogic.client.MarkLogicInternalException; +import com.marklogic.client.io.marker.BufferableHandle; +import com.marklogic.client.io.marker.ContentHandle; +import com.marklogic.client.io.marker.ContentHandleFactory; +import com.marklogic.client.io.marker.XMLReadHandle; +import com.marklogic.client.io.marker.XMLWriteHandle; + import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; /** * Will register all subinterfaces of org.w3c.Node for convenience methods readAs and writeAs. @@ -26,7 +51,9 @@ public class DOMNodeHandle implements OutputStreamSender, BufferableHandle, ContentHandle, XMLReadHandle, XMLWriteHandle { - private Node content; + static final private Logger logger = LoggerFactory.getLogger(JAXBHandle.class); + + private T content; private DocumentBuilderFactory factory; /** @@ -41,7 +68,7 @@ public Class[] getHandledClasses() { } @Override public boolean isHandled(Class type) { - return null; + return (Boolean) null; } @Override public ContentHandle newHandle(Class type) { @@ -59,7 +86,7 @@ public DOMNodeHandle() { * Initializes the handle with a DOM Node for the content. * @param content a DOM Node */ - public DOMNodeHandle(Node content) { + public DOMNodeHandle(T content) { this(); set(content); } @@ -69,7 +96,7 @@ public DOMNodeHandle(Node content) { * @return the DOM Node */ @Override - public Document get() { + public T get() { return content; } /** @@ -77,7 +104,7 @@ public Document get() { * @param content a DOM Node */ @Override - public void set(Document content) { + public void set(T content) { this.content = content; } @@ -157,15 +184,15 @@ protected void receiveContent(InputStream content) { DOMImplementationLS domImpl = (DOMImplementationLS) factory.newDocumentBuilder().getDOMImplementation(); LSParser parser = domImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); - if (resolver != null) { - parser.getDomConfig().setParameter("resource-resolver", resolver); - } +// if (resolver != null) { +// parser.getDomConfig().setParameter("resource-resolver", resolver); +// } LSInput domInput = domImpl.createLSInput(); domInput.setEncoding("UTF-8"); domInput.setByteStream(content); - this.content = parser.parse(domInput); + this.content = (T) parser.parse(domInput); content.close(); } catch (IOException e) { logger.error("Failed to parse DOM document from input stream",e); diff --git a/src/main/java/com/marklogic/client/io/JAXBDatatypeHandle.java b/src/main/java/com/marklogic/client/io/JAXBDatatypeHandle.java index 27376f38f..112da8882 100644 --- a/src/main/java/com/marklogic/client/io/JAXBDatatypeHandle.java +++ b/src/main/java/com/marklogic/client/io/JAXBDatatypeHandle.java @@ -15,8 +15,36 @@ */ package com.marklogic.client.io; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; +import java.util.Calendar; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.DOMException; import org.w3c.dom.Node; +import org.w3c.dom.ls.DOMImplementationLS; +import org.w3c.dom.ls.LSException; +import org.w3c.dom.ls.LSInput; +import org.w3c.dom.ls.LSOutput; +import org.w3c.dom.ls.LSParser; + +import com.marklogic.client.MarkLogicIOException; +import com.marklogic.client.MarkLogicInternalException; +import com.marklogic.client.io.marker.BufferableHandle; +import com.marklogic.client.io.marker.ContentHandle; +import com.marklogic.client.io.marker.ContentHandleFactory; +import com.marklogic.client.io.marker.XMLReadHandle; +import com.marklogic.client.io.marker.XMLWriteHandle; + +import javax.xml.datatype.Duration; +import javax.xml.namespace.QName; import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; /** * Supports get/set of any type handled by javax.xml.bind.DatatypeConverter or for time- @@ -27,7 +55,41 @@ public class JAXBDatatypeHandle implements OutputStreamSender, BufferableHandle, ContentHandle, XMLReadHandle, XMLWriteHandle { - private Node content; + public static final DataType XS_ANYURI = new DataType(Calendar.class); + public static final DataType XS_BASE64BINARY = new DataType(Calendar.class); + public static final DataType XS_BOOLEAN = new DataType(Calendar.class); + public static final DataType XS_DATE = new DataType(Calendar.class); + public static final DataType XS_DATETIME = new DataType(Calendar.class); + public static final DataType XS_DAYTIMEDURATION = new DataType(Calendar.class); + public static final DataType XS_DECIMAL = new DataType(Calendar.class); + public static final DataType XS_DOUBLE = new DataType(Calendar.class); + public static final DataType XS_DURATION = new DataType(Duration.class); + public static final DataType XS_FLOAT = new DataType(Calendar.class); + public static final DataType XS_GDAY = new DataType(Calendar.class); + public static final DataType XS_GMONTH = new DataType(Calendar.class); + public static final DataType XS_GMONTHDAY = new DataType(Calendar.class); + public static final DataType XS_GYEAR = new DataType(Calendar.class); + public static final DataType XS_GYEARMONTH = new DataType(Calendar.class); + public static final DataType XS_HEXBINARY = new DataType(Calendar.class); + public static final DataType XS_INTEGER = new DataType(Calendar.class); + public static final DataType XS_QNAME = new DataType(Calendar.class); + public static final DataType XS_STRING = new DataType(Calendar.class); + public static final DataType XS_TIME = new DataType(Calendar.class); + + public static class DataType { + private Class clazz; + private DataType(Class clazz) { + this.clazz = clazz; + } + + public Class getMappedClass() { + return clazz; + } + }; + + static final private Logger logger = LoggerFactory.getLogger(JAXBHandle.class); + + private T content; private DocumentBuilderFactory factory; /** @@ -42,7 +104,7 @@ public Class[] getHandledClasses() { } @Override public boolean isHandled(Class type) { - return null; + return (Boolean) null; } @Override public ContentHandle newHandle(Class type) { @@ -56,6 +118,7 @@ public JAXBDatatypeHandle(Class clazz) { setResendable(true); } + /** * Initializes the handle with the content. * @param xsType the localname part of the XML Schema type. The type must be one of: @@ -102,11 +165,25 @@ public JAXBDatatypeHandle(Class clazz) { * javax.xml.datatype.XMLGregorianCalendar *
*/ - public JAXBDatatypeHandle(QName xsType, T content) { - this(); + public JAXBDatatypeHandle(DataType type) { + super(); + } + + public JAXBDatatypeHandle(DataType type, String content) { + super(); + set(convert(content)); + } + + public JAXBDatatypeHandle(T content) { + super(); set(content); } + public T convert(String content) { + // use DatatypeConverter or DatatypeFactory to convert + return null; + } + /** * Returns the DOM Node for the content. * @return the DOM Node @@ -200,15 +277,15 @@ protected void receiveContent(InputStream content) { DOMImplementationLS domImpl = (DOMImplementationLS) factory.newDocumentBuilder().getDOMImplementation(); LSParser parser = domImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); - if (resolver != null) { - parser.getDomConfig().setParameter("resource-resolver", resolver); - } +// if (resolver != null) { +// parser.getDomConfig().setParameter("resource-resolver", resolver); +// } LSInput domInput = domImpl.createLSInput(); domInput.setEncoding("UTF-8"); domInput.setByteStream(content); - this.content = parser.parse(domInput); + this.content = (T) parser.parse(domInput); content.close(); } catch (IOException e) { logger.error("Failed to parse DOM document from input stream",e); @@ -241,7 +318,7 @@ public void write(OutputStream out) throws IOException { LSOutput domOutput = domImpl.createLSOutput(); domOutput.setEncoding("UTF-8"); domOutput.setByteStream(out); - domImpl.createLSSerializer().write(content, domOutput); + domImpl.createLSSerializer().write((Node) content, domOutput); } catch (DOMException e) { logger.error("Failed to serialize DOM document to output stream",e); throw new MarkLogicInternalException(e); @@ -253,6 +330,7 @@ public void write(OutputStream out) throws IOException { throw new MarkLogicInternalException(e); } } + } diff --git a/src/main/java/com/marklogic/client/io/XQueryTypeHandle.java b/src/main/java/com/marklogic/client/io/XQueryTypeHandle.java index 441f6a632..caf3076f2 100644 --- a/src/main/java/com/marklogic/client/io/XQueryTypeHandle.java +++ b/src/main/java/com/marklogic/client/io/XQueryTypeHandle.java @@ -15,6 +15,37 @@ */ package com.marklogic.client.io; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; +import java.util.Calendar; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.DOMException; +import org.w3c.dom.Node; +import org.w3c.dom.ls.DOMImplementationLS; +import org.w3c.dom.ls.LSException; +import org.w3c.dom.ls.LSInput; +import org.w3c.dom.ls.LSOutput; +import org.w3c.dom.ls.LSParser; + +import com.marklogic.client.MarkLogicIOException; +import com.marklogic.client.MarkLogicInternalException; +import com.marklogic.client.io.marker.BufferableHandle; +import com.marklogic.client.io.marker.ContentHandle; +import com.marklogic.client.io.marker.ContentHandleFactory; +import com.marklogic.client.io.marker.XMLReadHandle; +import com.marklogic.client.io.marker.XMLWriteHandle; + +import javax.xml.datatype.Duration; +import javax.xml.namespace.QName; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + /** * Supports get/set of any type necessary for ServerEval but not handled by JAXBDatatypeHandle or DomNodeHandle. * For now the only two types are java.net.URI and javax.xml.namespace.QName. @@ -24,7 +55,9 @@ public class XQueryTypeHandle implements OutputStreamSender, BufferableHandle, ContentHandle, XMLReadHandle, XMLWriteHandle { - private T content; + static final private Logger logger = LoggerFactory.getLogger(JAXBHandle.class); + + private T content; /** * Creates a factory to create a DOMHandle instance for a DOM document. @@ -38,7 +71,7 @@ public Class[] getHandledClasses() { } @Override public boolean isHandled(Class type) { - return null; + return (Boolean) null; } @Override public ContentHandle newHandle(Class type) { @@ -66,7 +99,7 @@ public XQueryTypeHandle(Class clazz) { *
*/ public XQueryTypeHandle(QName xsType, T content) { - this(); + this((Class) content.getClass()); set(content); } @@ -120,66 +153,66 @@ public String toString() { throw new MarkLogicIOException(e); } } - /** - * Returns the factory for building DOM documents. - * @return the document factory - */ - public DocumentBuilderFactory getFactory() throws ParserConfigurationException { - if (factory == null) - factory = makeDocumentBuilderFactory(); - return factory; - } - /** - * Specifies the factory for building DOM documents. - * @param factory the document factory - */ - public void setFactory(DocumentBuilderFactory factory) { - this.factory = factory; - } - protected DocumentBuilderFactory makeDocumentBuilderFactory() throws ParserConfigurationException { - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - factory.setNamespaceAware(true); - factory.setValidating(false); - // TODO: XInclude - - return factory; - } +// /** +// * Returns the factory for building DOM documents. +// * @return the document factory +// */ +// public DocumentBuilderFactory getFactory() throws ParserConfigurationException { +// if (factory == null) +// factory = makeDocumentBuilderFactory(); +// return factory; +// } +// /** +// * Specifies the factory for building DOM documents. +// * @param factory the document factory +// */ +// public void setFactory(DocumentBuilderFactory factory) { +// this.factory = factory; +// } +// protected DocumentBuilderFactory makeDocumentBuilderFactory() throws ParserConfigurationException { +// DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); +// factory.setNamespaceAware(true); +// factory.setValidating(false); +// // TODO: XInclude +// +// return factory; +// } @Override protected void receiveContent(InputStream content) { - if (content == null) { - this.content = null; - return; - } - - try { - if (logger.isInfoEnabled()) - logger.info("Parsing DOM document from input stream"); - - DocumentBuilderFactory factory = getFactory(); - if (factory == null) { - throw new MarkLogicInternalException("Failed to make DOM document builder factory"); - } - - DOMImplementationLS domImpl = (DOMImplementationLS) factory.newDocumentBuilder().getDOMImplementation(); - - LSParser parser = domImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); - if (resolver != null) { - parser.getDomConfig().setParameter("resource-resolver", resolver); - } - - LSInput domInput = domImpl.createLSInput(); - domInput.setEncoding("UTF-8"); - domInput.setByteStream(content); - - this.content = parser.parse(domInput); - content.close(); - } catch (IOException e) { - logger.error("Failed to parse DOM document from input stream",e); - throw new MarkLogicInternalException(e); - } catch (ParserConfigurationException e) { - logger.error("Failed to parse DOM document from input stream",e); - throw new MarkLogicInternalException(e); - } +// if (content == null) { +// this.content = null; +// return; +// } +// +// try { +// if (logger.isInfoEnabled()) +// logger.info("Parsing DOM document from input stream"); +// +// DocumentBuilderFactory factory = getFactory(); +// if (factory == null) { +// throw new MarkLogicInternalException("Failed to make DOM document builder factory"); +// } +// +// DOMImplementationLS domImpl = (DOMImplementationLS) factory.newDocumentBuilder().getDOMImplementation(); +// +// LSParser parser = domImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); +// if (resolver != null) { +// parser.getDomConfig().setParameter("resource-resolver", resolver); +// } +// +// LSInput domInput = domImpl.createLSInput(); +// domInput.setEncoding("UTF-8"); +// domInput.setByteStream(content); +// +// this.content = parser.parse(domInput); +// content.close(); +// } catch (IOException e) { +// logger.error("Failed to parse DOM document from input stream",e); +// throw new MarkLogicInternalException(e); +// } catch (ParserConfigurationException e) { +// logger.error("Failed to parse DOM document from input stream",e); +// throw new MarkLogicInternalException(e); +// } } @Override protected OutputStreamSender sendContent() { @@ -191,30 +224,30 @@ protected OutputStreamSender sendContent() { } @Override public void write(OutputStream out) throws IOException { - try { - if (logger.isInfoEnabled()) - logger.info("Serializing DOM document to output stream"); - - DocumentBuilderFactory factory = getFactory(); - if (factory == null) { - throw new MarkLogicInternalException("Failed to make DOM document builder factory"); - } - - DOMImplementationLS domImpl = (DOMImplementationLS) factory.newDocumentBuilder().getDOMImplementation(); - LSOutput domOutput = domImpl.createLSOutput(); - domOutput.setEncoding("UTF-8"); - domOutput.setByteStream(out); - domImpl.createLSSerializer().write(content, domOutput); - } catch (DOMException e) { - logger.error("Failed to serialize DOM document to output stream",e); - throw new MarkLogicInternalException(e); - } catch (LSException e) { - logger.error("Failed to serialize DOM document to output stream",e); - throw new MarkLogicInternalException(e); - } catch (ParserConfigurationException e) { - logger.error("Failed to serialize DOM document to output stream",e); - throw new MarkLogicInternalException(e); - } +// try { +// if (logger.isInfoEnabled()) +// logger.info("Serializing DOM document to output stream"); +// +// DocumentBuilderFactory factory = getFactory(); +// if (factory == null) { +// throw new MarkLogicInternalException("Failed to make DOM document builder factory"); +// } +// +// DOMImplementationLS domImpl = (DOMImplementationLS) factory.newDocumentBuilder().getDOMImplementation(); +// LSOutput domOutput = domImpl.createLSOutput(); +// domOutput.setEncoding("UTF-8"); +// domOutput.setByteStream(out); +// domImpl.createLSSerializer().write(content, domOutput); +// } catch (DOMException e) { +// logger.error("Failed to serialize DOM document to output stream",e); +// throw new MarkLogicInternalException(e); +// } catch (LSException e) { +// logger.error("Failed to serialize DOM document to output stream",e); +// throw new MarkLogicInternalException(e); +// } catch (ParserConfigurationException e) { +// logger.error("Failed to serialize DOM document to output stream",e); +// throw new MarkLogicInternalException(e); +// } } } diff --git a/src/test/java/com/marklogic/client/test/EvalTest.java b/src/test/java/com/marklogic/client/test/EvalTest.java index ce5f424b6..80055415a 100644 --- a/src/test/java/com/marklogic/client/test/EvalTest.java +++ b/src/test/java/com/marklogic/client/test/EvalTest.java @@ -19,7 +19,9 @@ import java.util.GregorianCalendar; import java.util.SimpleTimeZone; +import javax.xml.datatype.Duration; import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -30,10 +32,15 @@ import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; +import org.w3c.dom.Document; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.marklogic.client.eval.EvalResults; import com.marklogic.client.eval.JSONVariableSet; import com.marklogic.client.eval.ServerEval; import com.marklogic.client.eval.VariableSet; +import com.marklogic.client.io.JAXBDatatypeHandle; import com.marklogic.client.io.JacksonHandle; import com.marklogic.client.io.StringHandle; @@ -41,7 +48,7 @@ @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class EvalTest { @BeforeClass - public static void beforeClass() throws JAXBException { + public static void beforeClass() { Common.connect(); //System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); } @@ -51,56 +58,58 @@ public static void afterClass() { } @Test - public void evalTest1 { + public void evalTest1() throws ParserConfigurationException { // hello world and response determined by implicit StringHandle which registered with String.class - String xquery = "'hello world'"; - ServerEval eval = Common.client.newServerEval(); - String response = eval.evalXQueryAs(String.class, xquery, null, null, null, TransactionMode.QUERY); + ServerEval query = Common.client.newXQueryEvalBuilder().xquery("'hello world'").build(); + String response = query.evalAs(String.class); assertEquals("Return should be 'hello world'", "hello world", response); // hello world with a variable and response explicit set to StringHandle - xquery = "declare variable $planet external;" + - "'hello world from ' || $planet"; - VariableSet vars = eval.newXMLVariableSet().addAs("planet", "Mars"); - StringHandle strResponse = eval.evalXQuery(xquery, vars, new StringHandle(), null, null, TransactionMode.QUERY); + query = Common.client.newXQueryEvalBuilder() + .xquery("declare variable $planet external;" + + "'hello world from ' || $planet") + .addVariable("planet", "Mars") + .build(); + StringHandle strResponse = query.eval(new StringHandle()); assertEquals("Return should be 'hello world from Mars'", "hello world from Mars", strResponse.get()); // accept and return each JSON variable type so use MultiPartResponsePage - String xquery = "declare variable $myString as xs:string external;" + - "declare variable $myArray as json:array external;" + - "declare variable $myObject as json:object external;" + - "declare variable $myBool as xs:boolean external;" + - "declare variable $myInteger as xs:integer external;" + - "declare variable $myDouble as xs:double external;" + - "declare variable $myDate as xs:date external;" + - "$myString, $myArray, $myObject, $myBool, $myInteger, $myDouble, $myDate"; Calendar septFirst = new GregorianCalendar(2014, Calendar.SEPTEMBER, 1); septFirst.setTimeZone(new SimpleTimeZone(0, "UTC")); - JSONVariableSet jsonVars = eval.newJSONVariableSet() - // String is mapped to implicitly use StringHandle - .addAs("myString", "Mars") + query = Common.client.newXQueryEvalBuilder() + .xquery("declare variable $myString as xs:string external;" + + "declare variable $myArray as json:array external;" + + "declare variable $myObject as json:object external;" + + "declare variable $myBool as xs:boolean external;" + + "declare variable $myInteger as xs:integer external;" + + "declare variable $myDouble as xs:double external;" + + "declare variable $myDate as xs:date external;" + + "$myString, $myArray, $myObject, $myBool, $myInteger, $myDouble, $myDate") + // String is directly supported in any EvalBuilder + .addVariable("myString", "Mars") // ArrayNode extends JSONNode which is mapped to implicitly use JacksonHandle - .addAs("myArray", new ObjectMapper().createArrayNode().add("item1").add("item2")) + .addVariableAs("myArray", new ObjectMapper().createArrayNode().add("item1").add("item2")) // ObjectNode extends JSONNode which is mapped to implicitly use JacksonHandle - .addAs("myObject", new ObjectMapper().createObjectNode().put("item1", "value1")) - // the rest use built-in methods of JSONVariableSet - .add ("myBool", true) - .add ("myInteger", 123) - .add ("myDouble", 1.1) - .add ("myDate", septFirst); - MultiPartResponsePage responses = eval.evalXQuery(xquery, jsonVars, null, null, TransactionMode.QUERY); - assertEquals("myString should = 'Mars'", "Mars", responses.next().getAs(String.class)); + .addVariableAs("myObject", new ObjectMapper().createObjectNode().put("item1", "value1")) + // the rest are auto-boxed by EvalBuilder.addVariable(String, Number) + .addVariable("myBool", true) + .addVariable("myInteger", 123) + .addVariable("myDouble", 1.1) + .addVariableAs("myDate", septFirst) + .build(); + EvalResults results = query.eval(); + assertEquals("myString should = 'Mars'", "Mars", results.next().getAs(String.class)); assertEquals("myArray should = [\"item1\",\"item2\"]", "[\"item1\",\"item2\"]", - responses.next().getAs(JsonNode.class)); + results.next().getAs(JsonNode.class)); assertEquals("myObject should = {\"item1\"=\"value1\"}", "{\"item1\"=\"value1\"}", - responses.next().getAs(JsonNode.class)); - JacksonHandle myBool = responses.next().get(new JacksonHandle()); + results.next().getAs(JsonNode.class)); + JacksonHandle myBool = results.next().get(new JacksonHandle()); assertEquals("myBool should = true", true, myBool.get().asBoolean()); - assertEquals("myInteger should = 123", 123, responses.next().get(new JacksonHandle()).get().asInt()); - assertEquals("myDouble should = 1.1", 1.1, responses.next().get(new JacksonHandle()).get().asDouble()); - // the format what we get from javax.xml.datatype.XMLGregorianCalendar.toString() + assertEquals("myInteger should = 123", 123, results.next().get(new JacksonHandle()).get().asInt()); + assertEquals("myDouble should = 1.1", 1.1, results.next().get(new JacksonHandle()).get().asDouble(), .001); + // the format we get from javax.xml.datatype.XMLGregorianCalendar.toString() assertEquals("myDate should = '2014-09-01T00:00:00'", "2014-09-01T00:00:00", - responses.next().get(new JAXBDatatypeHandle()).get()); + results.next().get(new JAXBDatatypeHandle(Calendar.class)).get().toString()); // accept and return each XML variable type so use MultiPartResponsePage @@ -124,36 +133,38 @@ public static void afterClass() { "declare variable $myDate as xs:date external;" + "$myString, $myArray, $myObject, $myBool, $myInteger, $myDouble, $myDate"; Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); - XMLVariableSet jsonVars = eval.newXMLVariableSet() + query = Common.client.newXQueryEvalBuilder() + .xquery(xquery) // String is mapped to implicitly use StringHandle - .addAs("myString", "Mars") - .addAs("myBinary", byte[] { 1, 2, 3}) - .addAs("myComment", document.createComment("")) - .addAs("myDocument", document) - .add ("myDuration", new JAXBDatatypeHandle("xs:dayTimeDuration", "PT0H")) - .addAs("myElement", document.createElement("http://marklogic.com/appservices/search", "options")) + .addVariableAs("myString", "Mars") + .addVariableAs("myBinary", new byte[] { 1, 2, 3}) + .addVariableAs("myComment", document.createComment("")) + .addVariableAs("myDocument", document) + .addVariable("myDuration", new JAXBDatatypeHandle(JAXBDatatypeHandle.XS_DURATION, "PT0H")) + .addVariableAs("myElement", document.createElementNS("http://marklogic.com/appservices/search", "options")) // ArrayNode extends XMLNode which is mapped to implicitly use JacksonHandle - .addAs("myArray", new JacksonHandle().getMapper().createArrayNode().add("item1").add("item2")) + .addVariableAs("myArray", new JacksonHandle().getMapper().createArrayNode().add("item1").add("item2")) // ObjectNode extends XMLNode which is mapped to implicitly use JacksonHandle - .addAs("myObject", new JacksonHandle().getMapper().createObjectNode().put("item1", "value1")) + .addVariableAs("myObject", new JacksonHandle().getMapper().createObjectNode().put("item1", "value1")) // the rest use built-in methods of XMLVariableSet - .add ("myBool", true) - .add ("myInteger", 123) - .add ("myDouble", 1.1) - .add ("myDate", new GregorianCalendar(2014, Calendar.SEPTEMBER, 1)); - MultiPartResponsePage responses = eval.evalXQuery(xquery, jsonVars, null, null, TransactionMode.QUERY); - assertEquals("myString should = 'Mars'", "Mars", responses.next().getAs(String.class)); + .addVariable("myBool", true) + .addVariable("myInteger", 123) + .addVariable("myDouble", 1.1) + .addVariableAs("myDate", new GregorianCalendar(2014, Calendar.SEPTEMBER, 1)) + .build(); + results = query.eval(); + assertEquals("myString should = 'Mars'", "Mars", results.next().getAs(String.class)); assertEquals("myArray should = [\"item1\",\"item2\"]", "[\"item1\",\"item2\"]", - responses.next().getAs(JsonNode.class)); + results.next().getAs(JsonNode.class)); assertEquals("myObject should = {\"item1\"=\"value1\"}", "{\"item1\"=\"value1\"}", - responses.next().getAs(JsonNode.class)); - JacksonHandle myBool = responses.next().get(new JacksonHandle()); + results.next().getAs(JsonNode.class)); + myBool = results.next().get(new JacksonHandle()); assertEquals("myBool should = true", true, myBool.get().asBoolean()); - assertEquals("myInteger should = 123", 123, responses.next().get(new JacksonHandle()).get().asInt()); - assertEquals("myDouble should = 1.1", 1.1, responses.next().get(new JacksonHandle()).get().asDouble()); + assertEquals("myInteger should = 123", 123, results.next().get(new JacksonHandle()).get().asInt()); + assertEquals("myDouble should = 1.1", 1.1, results.next().get(new JacksonHandle()).get().asDouble(), .001); // the format what we get from java.util.Calendar.toString() assertEquals("myDate should = 'Mon Sep 01 00:00:00 UDT 2014'", "Mon Sep 01 00:00:00 UDT 2014", - responses.next().get(new JAXBDatatypeHandle()).get()); + results.next().get(new JAXBDatatypeHandle(Calendar.class)).get().toString()); } } From 9b29ceed81de36f7a2edb8486e1ef71fd3eeccec Mon Sep 17 00:00:00 2001 From: sravan Date: Wed, 17 Sep 2014 10:37:07 -0700 Subject: [PATCH 110/357] adding POJO test for qbe,combined query definition search --- .../javaclient/TestPOJOwithQBEQueryDef.java | 89 +++++++++---------- 1 file changed, 41 insertions(+), 48 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOwithQBEQueryDef.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOwithQBEQueryDef.java index dabd46583..f1246b9a9 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOwithQBEQueryDef.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOwithQBEQueryDef.java @@ -41,18 +41,18 @@ public class TestPOJOwithQBEQueryDef extends BasicJavaClientREST { @BeforeClass public static void setUpBeforeClass() throws Exception { -// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); System.out.println("In setup"); -// setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); -// addRangePathIndex(dbName, "string", "com.marklogic.javaclient.Artifact/manufacturer/com.marklogic.javaclient.Company/name", "http://marklogic.com/collation/", "ignore"); -// BasicJavaClientREST.addRangeElementIndex(dbName, "long", "", "inventory"); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + + BasicJavaClientREST.addRangeElementIndex(dbName, "long", "", "inventory"); BasicJavaClientREST.addRangeElementIndex(dbName, "long", "", "id"); } @AfterClass public static void tearDownAfterClass() throws Exception { System.out.println("In tear down" ); -// tearDownJavaRESTServer(dbName, fNames, restServerName); + tearDownJavaRESTServer(dbName, fNames, restServerName); } @Before public void setUp() throws Exception { @@ -69,23 +69,23 @@ public Artifact getArtifact(int counter){ Artifact cogs = new Artifact(); cogs.setId(counter); if( counter % 5 == 0){ - cogs.setName("Cogs special"); - if(counter % 2 ==0){ - Company acme = new Company(); - acme.setName("Acme special, Inc."); - acme.setWebsite("http://www.acme special.com"); - acme.setLatitude(41.998+counter); - acme.setLongitude(-87.966+counter); - cogs.setManufacturer(acme); + cogs.setName("Cogs special"); + if(counter % 2 ==0){ + Company acme = new Company(); + acme.setName("Acme special, Inc."); + acme.setWebsite("http://www.acme special.com"); + acme.setLatitude(41.998+counter); + acme.setLongitude(-87.966+counter); + cogs.setManufacturer(acme); - }else{ - Company widgets = new Company(); - widgets.setName("Widgets counter Inc."); - widgets.setWebsite("http://www.widgets counter.com"); - widgets.setLatitude(41.998+counter); - widgets.setLongitude(-87.966+counter); - cogs.setManufacturer(widgets); - } + }else{ + Company widgets = new Company(); + widgets.setName("Widgets counter Inc."); + widgets.setWebsite("http://www.widgets counter.com"); + widgets.setLatitude(41.998+counter); + widgets.setLongitude(-87.966+counter); + cogs.setManufacturer(widgets); + } }else{ cogs.setName("Cogs "+counter); if(counter % 2 ==0){ @@ -133,16 +133,16 @@ public void testPOJOqbeSearchWithoutSearchHandle() { QueryManager queryMgr = client.newQueryManager(); String queryAsString = "{\"$query\":{" - + "\"$and\":[{\"name\":{\"$word\":\"cogs\",\"$exact\": false}}]" - + ",\"$not\":[{\"name\":{\"$word\":\"special\",\"$exact\": false}}]" - + "}}"; + + "\"$and\":[{\"name\":{\"$word\":\"cogs\",\"$exact\": false}}]" + + ",\"$not\":[{\"name\":{\"$word\":\"special\",\"$exact\": false}}]" + + "}}"; RawQueryByExampleDefinition qd = queryMgr.newRawQueryByExampleDefinition(new StringHandle(queryAsString).withFormat(Format.JSON)); qd.setCollections("odd"); products.setPageLength(11); p = products.search(qd, 1); assertEquals("total no of pages",4,p.getTotalPages()); -// System.out.println(p.getTotalPages()); + // System.out.println(p.getTotalPages()); long pageNo=1,count=0; do{ count =0; @@ -164,17 +164,17 @@ public void testPOJOqbeSearchWithoutSearchHandle() { assertEquals("page number after the loop",4,p.getPageNumber()); assertEquals("total no of pages",4,p.getTotalPages()); } -@Test + @Test public void testPOJOqbeSearchWithSearchHandle() { PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); PojoPage p; this.loadSimplePojos(products); - + QueryManager queryMgr = client.newQueryManager(); String queryAsString = "{\"$query\":{" - + "\"$and\":[{\"inventory\":{\"$gt\":1010}},{\"inventory\":{\"$le\":1110}}]" - + ",\"$filtered\": true}}"; + + "\"$and\":[{\"inventory\":{\"$gt\":1010}},{\"inventory\":{\"$le\":1110}}]" + + ",\"$filtered\": true}}"; System.out.println(queryAsString); RawQueryByExampleDefinition qd = queryMgr.newRawQueryByExampleDefinition(new StringHandle(queryAsString).withFormat(Format.JSON)); qd.setCollections("even"); @@ -208,7 +208,7 @@ public void testPOJOqbeSearchWithSearchHandle() { for(String fname:facetNames){ System.out.println(fname); } -// assertEquals("Total results from search handle ",50,results.getTotalResults()); + // assertEquals("Total results from search handle ",50,results.getTotalResults()); assertTrue("Search Handle metric results ",results.getMetrics().getTotalTime()>0); }while(!p.isLastPage() && pageNo products = client.newPojoRepository(Artifact.class, Long.class); PojoPage p; this.loadSimplePojos(products); -// StructuredQueryBuilder qb = new StructuredQueryBuilder(); -// StructuredQueryDefinition qd =qb.value(qb.jsonProperty("id"), 5,10,15,20,25,30); -// StructuredQueryDefinition qd = qb.and(q1,qb.range(qb.pathIndex("com.marklogic.javaclient.Artifact/inventory"), "xs:long",Operator.LE, 1110),qb.collection("even")); + QueryManager queryMgr = client.newQueryManager(); String queryAsString = "{\"search\":{\"query\":{" - + "\"range-constraint-query\":{\"constraint-name\":\"id\", \"value\":[5,10,15,20,25,30]}}," - + "\"options\":{\"return-metrics\":false, \"constraint\":{\"name\":\"id\", \"range\":{\"type\": \"xs:string\",\"json-property\":\"id\"}}}" - + "}}"; + + "\"range-constraint-query\":{\"constraint-name\":\"id\", \"value\":[5,10,15,20,25,30]}}," + + "\"options\":{\"return-metrics\":false, \"constraint\":{\"name\":\"id\", \"range\":{\"type\": \"xs:long\",\"json-property\":\"id\"}}}" + + "}}"; RawCombinedQueryDefinition qd = queryMgr.newRawCombinedQueryDefinition(new StringHandle(queryAsString).withFormat(Format.JSON)); + StringHandle results = new StringHandle(); JacksonHandle jh = new JacksonHandle(); p = products.search(qd, 1,jh); - + long pageNo=1,count=0; do{ count =0; @@ -290,20 +289,14 @@ public void testPOJOcombinedSearchforNumberWithStringHandle() throws JsonProcess } assertEquals("Page total results",count,p.getTotalSize()); pageNo=pageNo+p.getPageSize(); - System.out.println(results.get().toString()); + System.out.println(results.get().toString()); }while(!p.isLastPage() && pageNo Date: Wed, 17 Sep 2014 13:22:48 -0600 Subject: [PATCH 111/357] a minor API tweak that seems it will make things more intuitive --- src/main/java/com/marklogic/client/DatabaseClient.java | 3 +-- src/main/java/com/marklogic/client/eval/EvalBuilder.java | 4 ++-- .../com/marklogic/client/eval/JavascriptEvalBuilder.java | 4 ++-- src/main/java/com/marklogic/client/eval/ServerEval.java | 7 +++++++ .../com/marklogic/client/impl/DatabaseClientImpl.java | 9 ++------- src/test/java/com/marklogic/client/test/EvalTest.java | 8 ++++---- 6 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/marklogic/client/DatabaseClient.java b/src/main/java/com/marklogic/client/DatabaseClient.java index 5cb502fc8..49452a1f3 100644 --- a/src/main/java/com/marklogic/client/DatabaseClient.java +++ b/src/main/java/com/marklogic/client/DatabaseClient.java @@ -180,6 +180,5 @@ public T init(String resourceName, T resourceManager * @return the object implementing communication with the server */ public Object getClientImplementation(); - public XQueryEvalBuilder newXQueryEvalBuilder(); - public JavascriptEvalBuilder newJavascriptEvalBuilder(); + public ServerEval newServerEval(); } diff --git a/src/main/java/com/marklogic/client/eval/EvalBuilder.java b/src/main/java/com/marklogic/client/eval/EvalBuilder.java index 36e283c65..a8fdfa9ad 100644 --- a/src/main/java/com/marklogic/client/eval/EvalBuilder.java +++ b/src/main/java/com/marklogic/client/eval/EvalBuilder.java @@ -18,7 +18,7 @@ import com.marklogic.client.Transaction; import com.marklogic.client.io.marker.AbstractWriteHandle; -public interface EvalBuilder { +public interface EvalBuilder { public EvalBuilder addVariable(String name, String value); public EvalBuilder addVariable(String name, Number value); public EvalBuilder addVariable(String name, Boolean value); @@ -28,5 +28,5 @@ public interface EvalBuilder { public EvalBuilder addVariableAs(String name, Object value); public EvalBuilder database(String database); public EvalBuilder transaction(Transaction transaction); - public T build(); + public ServerEval build(); } diff --git a/src/main/java/com/marklogic/client/eval/JavascriptEvalBuilder.java b/src/main/java/com/marklogic/client/eval/JavascriptEvalBuilder.java index 6fb452dc7..e8e30b8e9 100644 --- a/src/main/java/com/marklogic/client/eval/JavascriptEvalBuilder.java +++ b/src/main/java/com/marklogic/client/eval/JavascriptEvalBuilder.java @@ -18,8 +18,8 @@ import com.marklogic.client.io.marker.TextWriteHandle; public interface JavascriptEvalBuilder extends EvalBuilder { - public EvalBuilder javascript(String javascript); - public EvalBuilder javascript(TextWriteHandle javascript); + public JavascriptEvalBuilder javascript(String javascript); + public JavascriptEvalBuilder javascript(TextWriteHandle javascript); } diff --git a/src/main/java/com/marklogic/client/eval/ServerEval.java b/src/main/java/com/marklogic/client/eval/ServerEval.java index 7b56a33c2..601c3dfd9 100644 --- a/src/main/java/com/marklogic/client/eval/ServerEval.java +++ b/src/main/java/com/marklogic/client/eval/ServerEval.java @@ -16,8 +16,15 @@ package com.marklogic.client.eval; import com.marklogic.client.io.marker.AbstractReadHandle; +import com.marklogic.client.io.marker.TextWriteHandle; public interface ServerEval { + public EvalBuilder xquery(String xquery); + public EvalBuilder xquery(TextWriteHandle xquery); + public EvalBuilder javascript(String javascript); + public EvalBuilder javascript(TextWriteHandle javascript); + public EvalBuilder xqueryModule(String modulePath); + public EvalBuilder javascriptModule(String modulePath); public T evalAs(Class responseType); public H eval(H responseHandle); public EvalResults eval(); diff --git a/src/main/java/com/marklogic/client/impl/DatabaseClientImpl.java b/src/main/java/com/marklogic/client/impl/DatabaseClientImpl.java index 322aeb9b9..43caad651 100644 --- a/src/main/java/com/marklogic/client/impl/DatabaseClientImpl.java +++ b/src/main/java/com/marklogic/client/impl/DatabaseClientImpl.java @@ -31,6 +31,7 @@ import com.marklogic.client.query.QueryManager; import com.marklogic.client.util.RequestLogger; import com.marklogic.client.eval.JavascriptEvalBuilder; +import com.marklogic.client.eval.ServerEval; import com.marklogic.client.eval.XQueryEvalBuilder; import com.marklogic.client.extensions.ResourceManager; import com.marklogic.client.DatabaseClientFactory.HandleFactoryRegistry; @@ -186,13 +187,7 @@ public RESTServices getServices() { } @Override - public XQueryEvalBuilder newXQueryEvalBuilder() { - // TODO Auto-generated method stub - return null; - } - - @Override - public JavascriptEvalBuilder newJavascriptEvalBuilder() { + public ServerEval newServerEval() { // TODO Auto-generated method stub return null; } diff --git a/src/test/java/com/marklogic/client/test/EvalTest.java b/src/test/java/com/marklogic/client/test/EvalTest.java index 80055415a..5943e5fcb 100644 --- a/src/test/java/com/marklogic/client/test/EvalTest.java +++ b/src/test/java/com/marklogic/client/test/EvalTest.java @@ -60,12 +60,12 @@ public static void afterClass() { @Test public void evalTest1() throws ParserConfigurationException { // hello world and response determined by implicit StringHandle which registered with String.class - ServerEval query = Common.client.newXQueryEvalBuilder().xquery("'hello world'").build(); + ServerEval query = Common.client.newServerEval().xquery("'hello world'").build(); String response = query.evalAs(String.class); assertEquals("Return should be 'hello world'", "hello world", response); // hello world with a variable and response explicit set to StringHandle - query = Common.client.newXQueryEvalBuilder() + query = Common.client.newServerEval() .xquery("declare variable $planet external;" + "'hello world from ' || $planet") .addVariable("planet", "Mars") @@ -76,7 +76,7 @@ public void evalTest1() throws ParserConfigurationException { // accept and return each JSON variable type so use MultiPartResponsePage Calendar septFirst = new GregorianCalendar(2014, Calendar.SEPTEMBER, 1); septFirst.setTimeZone(new SimpleTimeZone(0, "UTC")); - query = Common.client.newXQueryEvalBuilder() + query = Common.client.newServerEval() .xquery("declare variable $myString as xs:string external;" + "declare variable $myArray as json:array external;" + "declare variable $myObject as json:object external;" + @@ -133,7 +133,7 @@ public void evalTest1() throws ParserConfigurationException { "declare variable $myDate as xs:date external;" + "$myString, $myArray, $myObject, $myBool, $myInteger, $myDouble, $myDate"; Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); - query = Common.client.newXQueryEvalBuilder() + query = Common.client.newServerEval() .xquery(xquery) // String is mapped to implicitly use StringHandle .addVariableAs("myString", "Mars") From b47907b06d3a7a774c58730799d88e441c34637c Mon Sep 17 00:00:00 2001 From: sammefford Date: Wed, 17 Sep 2014 14:17:20 -0600 Subject: [PATCH 112/357] remove unused imports; qualify types in EvalTest --- src/main/java/com/marklogic/client/DatabaseClient.java | 2 -- .../com/marklogic/client/impl/DatabaseClientImpl.java | 2 -- src/test/java/com/marklogic/client/test/EvalTest.java | 8 ++------ 3 files changed, 2 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/marklogic/client/DatabaseClient.java b/src/main/java/com/marklogic/client/DatabaseClient.java index 49452a1f3..b7f8a6661 100644 --- a/src/main/java/com/marklogic/client/DatabaseClient.java +++ b/src/main/java/com/marklogic/client/DatabaseClient.java @@ -24,9 +24,7 @@ import com.marklogic.client.document.JSONDocumentManager; import com.marklogic.client.document.TextDocumentManager; import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.eval.JavascriptEvalBuilder; import com.marklogic.client.eval.ServerEval; -import com.marklogic.client.eval.XQueryEvalBuilder; import com.marklogic.client.extensions.ResourceManager; import com.marklogic.client.admin.ExtensionMetadata; import com.marklogic.client.query.QueryManager; diff --git a/src/main/java/com/marklogic/client/impl/DatabaseClientImpl.java b/src/main/java/com/marklogic/client/impl/DatabaseClientImpl.java index 43caad651..d410496a2 100644 --- a/src/main/java/com/marklogic/client/impl/DatabaseClientImpl.java +++ b/src/main/java/com/marklogic/client/impl/DatabaseClientImpl.java @@ -30,9 +30,7 @@ import com.marklogic.client.document.JSONDocumentManager; import com.marklogic.client.query.QueryManager; import com.marklogic.client.util.RequestLogger; -import com.marklogic.client.eval.JavascriptEvalBuilder; import com.marklogic.client.eval.ServerEval; -import com.marklogic.client.eval.XQueryEvalBuilder; import com.marklogic.client.extensions.ResourceManager; import com.marklogic.client.DatabaseClientFactory.HandleFactoryRegistry; import com.marklogic.client.admin.ServerConfigurationManager; diff --git a/src/test/java/com/marklogic/client/test/EvalTest.java b/src/test/java/com/marklogic/client/test/EvalTest.java index 5943e5fcb..26415fe03 100644 --- a/src/test/java/com/marklogic/client/test/EvalTest.java +++ b/src/test/java/com/marklogic/client/test/EvalTest.java @@ -24,8 +24,6 @@ import javax.xml.parsers.ParserConfigurationException; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; import org.junit.FixMethodOrder; import org.junit.runners.MethodSorters; @@ -37,9 +35,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.marklogic.client.eval.EvalResults; -import com.marklogic.client.eval.JSONVariableSet; import com.marklogic.client.eval.ServerEval; -import com.marklogic.client.eval.VariableSet; import com.marklogic.client.io.JAXBDatatypeHandle; import com.marklogic.client.io.JacksonHandle; import com.marklogic.client.io.StringHandle; @@ -109,7 +105,7 @@ public void evalTest1() throws ParserConfigurationException { assertEquals("myDouble should = 1.1", 1.1, results.next().get(new JacksonHandle()).get().asDouble(), .001); // the format we get from javax.xml.datatype.XMLGregorianCalendar.toString() assertEquals("myDate should = '2014-09-01T00:00:00'", "2014-09-01T00:00:00", - results.next().get(new JAXBDatatypeHandle(Calendar.class)).get().toString()); + results.next().get(new JAXBDatatypeHandle(Calendar.class)).get().toString()); // accept and return each XML variable type so use MultiPartResponsePage @@ -164,7 +160,7 @@ public void evalTest1() throws ParserConfigurationException { assertEquals("myDouble should = 1.1", 1.1, results.next().get(new JacksonHandle()).get().asDouble(), .001); // the format what we get from java.util.Calendar.toString() assertEquals("myDate should = 'Mon Sep 01 00:00:00 UDT 2014'", "Mon Sep 01 00:00:00 UDT 2014", - results.next().get(new JAXBDatatypeHandle(Calendar.class)).get().toString()); + results.next().get(new JAXBDatatypeHandle(Calendar.class)).get().toString()); } } From 6953497d98086c53f177a2e8828f82ab1c9e76a2 Mon Sep 17 00:00:00 2001 From: Ajit George Date: Wed, 17 Sep 2014 13:31:45 -0700 Subject: [PATCH 113/357] Corrected typos, comments and indents. --- .../TestBulkReadWriteMetaDataChange.java | 45 ++++++++----------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteMetaDataChange.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteMetaDataChange.java index aa0836687..41c965e3d 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteMetaDataChange.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteMetaDataChange.java @@ -30,7 +30,6 @@ import com.marklogic.client.io.DocumentMetadataHandle.DocumentCollections; import com.marklogic.client.io.DocumentMetadataHandle.DocumentPermissions; import com.marklogic.client.io.DocumentMetadataHandle.DocumentProperties; -import com.marklogic.client.io.Format; import com.marklogic.client.io.JacksonHandle; import com.marklogic.client.io.StringHandle; @@ -56,8 +55,8 @@ public class TestBulkReadWriteMetaDataChange extends BasicJavaClientREST { public static void setUpBeforeClass() throws Exception { System.out.println("In Setup"); -// setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); -// createRESTUser("app-user", "password", "rest-writer","rest-reader" ); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + createRESTUser("app-user", "password", "rest-writer","rest-reader" ); } /** @@ -66,8 +65,8 @@ public static void setUpBeforeClass() throws Exception { @AfterClass public static void tearDownAfterClass() throws Exception { System.out.println("In tear down" ); -// tearDownJavaRESTServer(dbName, fNames, restServerName); -// deleteRESTUser("app-user"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + deleteRESTUser("app-user"); } /** @@ -250,7 +249,7 @@ public void testWriteMultipleTextDocWithChangedMetadataProperties() { TextDocumentManager docMgr = client.newTextDocumentManager(); DocumentWriteSet writeset = docMgr.newWriteSet(); - // put metadata + // put meta-data DocumentMetadataHandle mh = setMetadata(); DocumentMetadataHandle mhRead = new DocumentMetadataHandle(); @@ -269,6 +268,7 @@ public void testWriteMultipleTextDocWithChangedMetadataProperties() { validateMetadata(mhRead); } validateMetadata(mhRead); + mhRead = null; // Add new meta-data DocumentMetadataHandle mhUpdated = setUpdatedMetadataProperties(); @@ -282,6 +282,7 @@ public void testWriteMultipleTextDocWithChangedMetadataProperties() { validateUpdatedMetadataProperties(mhUpd); } validateUpdatedMetadataProperties(mhUpd); + mhUpd = null; } @Test public void testWriteMultipleJacksonPoJoDocsWithMetadata() throws Exception @@ -309,7 +310,7 @@ public void testWriteMultipleJacksonPoJoDocsWithMetadata() throws Exception JsonNode writeDocument2 = writeHandle.getMapper().convertValue(product2, JsonNode.class); JsonNode writeDocument3 = writeHandle.getMapper().convertValue(product3, JsonNode.class); JSONDocumentManager docMgr = client.newJSONDocumentManager(); - DocumentWriteSet writeset =docMgr.newWriteSet(); + DocumentWriteSet writeset = docMgr.newWriteSet(); writeset.addDefault(mh); writeset.add(docId[0],writeHandle); @@ -352,7 +353,7 @@ public void testBulkReadUsingMultipleUri() throws Exception { TextDocumentManager docMgr = client.newTextDocumentManager(); docMgr.setMetadataCategories(Metadata.ALL); DocumentWriteSet writeset = docMgr.newWriteSet(); - // put metadata + // put meta-data DocumentMetadataHandle mh = setMetadata(); DocumentMetadataHandle mhRead = new DocumentMetadataHandle(); @@ -371,8 +372,8 @@ public void testBulkReadUsingMultipleUri() throws Exception { mhRead = rec.getMetadata(mhRead); validateMetadata(mhRead); } - validateMetadata(mhRead); + mhRead = null; } catch(Exception exp) { System.out.println(exp.getMessage()); @@ -399,7 +400,7 @@ public void testReadUsingMultipleUriAndMetadataHandleInTransaction() throws Exce docMgr.setMetadataCategories(Metadata.ALL); DocumentWriteSet writeset = docMgr.newWriteSet(); - // put metadata + // put meta-data DocumentMetadataHandle mh = setMetadata(); writeset.addDefault(mh); @@ -417,6 +418,7 @@ public void testReadUsingMultipleUriAndMetadataHandleInTransaction() throws Exce validateMetadata(mhRead); } validateMetadata(mhRead); + mhRead = null; } catch(Exception exp) { System.out.println(exp.getMessage()); @@ -429,7 +431,7 @@ public void testReadUsingMultipleUriAndMetadataHandleInTransaction() throws Exce /* - * * Purpose: To validate: DocumentManager readMetadata(String... uris) + * * Purpose: To validate DocumentManager readMetadata(String... uris) without Transaction * This test verifies document meta-data reads from an open database in the representation provided by the handle to call readMetadata. * Verified by reading meta-data for individual documents. */ @@ -437,22 +439,21 @@ public void testReadUsingMultipleUriAndMetadataHandleInTransaction() throws Exce @Test public void testBulkReadMetadataUsingMultipleUriNoTransaction() throws Exception { String docId[] = {"/foo/test/URIFoo1.txt","/foo/test/URIFoo2.txt","/foo/test/URIFoo3.txt"}; + DocumentMetadataHandle mhRead = new DocumentMetadataHandle(); - try { + TextDocumentManager docMgr = client.newTextDocumentManager(); docMgr.setMetadataCategories(Metadata.ALL); DocumentWriteSet writeset = docMgr.newWriteSet(); - // put metadata + // put meta-data DocumentMetadataHandle mh = setMetadata(); writeset.addDefault(mh); writeset.add(docId[0], new StringHandle().with("This is so URI foo 1")); writeset.add(docId[1], new StringHandle().with("This is so URI foo 2")); writeset.add(docId[2], new StringHandle().with("This is so URI foo 3")); - docMgr.write(writeset); - - DocumentMetadataHandle mhRead = new DocumentMetadataHandle(); + docMgr.write(writeset); DocumentPage page = docMgr.readMetadata(docId[0], docId[1], docId[2]); @@ -461,16 +462,8 @@ public void testBulkReadMetadataUsingMultipleUriNoTransaction() throws Exception rec.getMetadata(mhRead); validateMetadata(mhRead); } - validateMetadata(mhRead); - } - catch(Exception exp) { - System.out.println(exp.getMessage()); - throw exp; - } - finally { - //transaction.rollback(); - } + mhRead = null; } /* @@ -483,7 +476,7 @@ public void testWriteMultipleTextDocWithChangedMetadataCollections() { TextDocumentManager docMgr = client.newTextDocumentManager(); DocumentWriteSet writeset = docMgr.newWriteSet(); - // put metadata + // put meta-data DocumentMetadataHandle mh = setMetadata(); writeset.addDefault(mh); From a17904ab2382020fe50938433734a9b5a0d069cb Mon Sep 17 00:00:00 2001 From: Ajit George Date: Wed, 17 Sep 2014 13:40:25 -0700 Subject: [PATCH 114/357] Added test method to read files from filesystem. --- .../TestBulkReadWriteWithJacksonHandle.java | 118 +++++++++++++----- 1 file changed, 88 insertions(+), 30 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonHandle.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonHandle.java index 81ec65835..3d8aea980 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonHandle.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonHandle.java @@ -4,9 +4,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import java.io.BufferedReader; -import java.io.Reader; -import java.io.StringReader; +import java.io.File; import java.util.Calendar; import org.junit.After; @@ -21,11 +19,11 @@ import com.marklogic.client.DatabaseClient; import com.marklogic.client.DatabaseClientFactory; import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.DocumentManager.Metadata; import com.marklogic.client.document.DocumentPage; import com.marklogic.client.document.DocumentRecord; import com.marklogic.client.document.DocumentWriteSet; import com.marklogic.client.document.JSONDocumentManager; -import com.marklogic.client.document.DocumentManager.Metadata; import com.marklogic.client.io.DocumentMetadataHandle; import com.marklogic.client.io.DocumentMetadataHandle.Capability; import com.marklogic.client.io.DocumentMetadataHandle.DocumentCollections; @@ -33,7 +31,6 @@ import com.marklogic.client.io.DocumentMetadataHandle.DocumentProperties; import com.marklogic.client.io.Format; import com.marklogic.client.io.JacksonHandle; -import com.marklogic.client.io.ReaderHandle; /* * This test is designed to to test all of bulk reads and write of JSON with JacksonHandle Manager by passing set of uris @@ -42,7 +39,6 @@ public class TestBulkReadWriteWithJacksonHandle extends BasicJavaClientREST { - private static final int BATCH_SIZE=100; private static final String DIRECTORY ="/bulkread/"; private static String dbName = "TestBulkReadWriteWithJacksonHandleDB"; private static String [] fNames = {"TestBulkReadWriteWithJacksonHandleDB-1"}; @@ -72,7 +68,7 @@ public void testCleanUp() throws Exception } public DocumentMetadataHandle setMetadata(){ - // create and initialize a handle on the metadata + // create and initialize a handle on the meta-data DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); metadataHandle.getCollections().addAll("my-collection1","my-collection2"); metadataHandle.getPermissions().add("app-user", Capability.UPDATE, Capability.READ); @@ -86,7 +82,7 @@ public DocumentMetadataHandle setMetadata(){ } public void validateMetadata(DocumentMetadataHandle mh){ - // get metadata values + // get meta-data values DocumentProperties properties = mh.getProperties(); DocumentPermissions permissions = mh.getPermissions(); DocumentCollections collections = mh.getCollections(); @@ -118,7 +114,7 @@ public void validateMetadata(DocumentMetadataHandle mh){ } public void validateDefaultMetadata(DocumentMetadataHandle mh){ - // get metadata values + // get meta-data values DocumentProperties properties = mh.getProperties(); DocumentPermissions permissions = mh.getPermissions(); DocumentCollections collections = mh.getCollections(); @@ -180,20 +176,20 @@ public void testWriteMultipleJSONDocs() throws Exception docMgr.write(writeset); - JacksonHandle r1 = new JacksonHandle(); - docMgr.read(docId[0], r1); - JSONAssert.assertEquals(json1, r1.toString(), true); + JacksonHandle jacksonhandle = new JacksonHandle(); + docMgr.read(docId[0], jacksonhandle); + JSONAssert.assertEquals(json1, jacksonhandle.toString(), true); - docMgr.read(docId[1], r1); - JSONAssert.assertEquals(json2, r1.toString(), true); + docMgr.read(docId[1],jacksonhandle); + JSONAssert.assertEquals(json2, jacksonhandle.toString(), true); - docMgr.read(docId[2], r1); - JSONAssert.assertEquals(json3, r1.toString(), true); + docMgr.read(docId[2], jacksonhandle); + JSONAssert.assertEquals(json3, jacksonhandle.toString(), true); } /* * - * Use JacksonHandle to load json strings using bulk write set. + * Use JacksonHandle to load JSON strings using bulk write set. * Test Bulk Read to see you can read the document specific meta-data. */ @@ -208,7 +204,7 @@ public void testWriteMultipleJSONDocsWithDefaultMetadata() throws Exception JSONDocumentManager docMgr = client.newJSONDocumentManager(); docMgr.setMetadataCategories(Metadata.ALL); DocumentWriteSet writeset = docMgr.newWriteSet(); - // put metadata + // put meta-data DocumentMetadataHandle mh = setMetadata(); ObjectMapper mapper = new ObjectMapper(); @@ -249,7 +245,7 @@ public void testWriteMultipleJSONDocsWithDefaultMetadata() throws Exception /* * - * Use JacksonHandle to load json strings using bulk write set. + * Use JacksonHandle to load JSON strings using bulk write set. * Test Bulk Read to see you can read all the documents */ @Test @@ -262,7 +258,7 @@ public void testWriteMultipleJSONDocsWithDefaultMetadata2() throws Exception String doc4 = new String("{\"animal\": \"lion\", \"says\": \"roar\"}"); String doc5 = new String("{\"animal\": \"man\", \"says\": \"hello\"}"); - // Synthesize input metadata + // Synthesize input meta-data DocumentMetadataHandle defaultMetadata1 = new DocumentMetadataHandle().withQuality(1); DocumentMetadataHandle defaultMetadata2 = @@ -307,16 +303,16 @@ public void testWriteMultipleJSONDocsWithDefaultMetadata2() throws Exception jacksonHandle5.set(manNode); jacksonHandle5.withFormat(Format.JSON); - // use system default metadata + // use system default meta-data batch.add("doc1.json", jacksonHandle1); - // using batch default metadata + // using batch default meta-data batch.addDefault(defaultMetadata1); - batch.add("doc2.json", jacksonHandle2); // batch default metadata + batch.add("doc2.json", jacksonHandle2); // batch default meta-data batch.add("doc3.json", docSpecificMetadata, jacksonHandle3); - batch.add("doc4.json", jacksonHandle4); // batch default metadata + batch.add("doc4.json", jacksonHandle4); // batch default meta-data - // replace batch default metadata with new metadata + // replace batch default meta-data with new meta-data batch.addDefault(defaultMetadata2); batch.add("doc5.json", jacksonHandle5); // batch default @@ -333,7 +329,7 @@ public void testWriteMultipleJSONDocsWithDefaultMetadata2() throws Exception validateDefaultMetadata(mh); assertEquals("default quality",0,mh.getQuality()); - // Doc2 should use the first batch default metadata, with quality 1 + // Doc2 should use the first batch default meta-data, with quality 1 page = jdm.read("doc2.json"); rec = page.next(); jdm.readMetadata(rec.getUri(), mh); @@ -342,8 +338,8 @@ public void testWriteMultipleJSONDocsWithDefaultMetadata2() throws Exception assertTrue("default collections reset",mh.getCollections().isEmpty()); // Doc3 should have the system default document quality (0) because quality - // was not included in the document-specific metadata. It should be in the - // collection "mySpecificCollection", from the document-specific metadata. + // was not included in the document-specific meta-data. It should be in the + // collection "mySpecificCollection", from the document-specific meta-data. page = jdm.read("doc3.json"); rec = page.next(); @@ -360,19 +356,81 @@ public void testWriteMultipleJSONDocsWithDefaultMetadata2() throws Exception } System.out.println(); - // Doc 4 should also use the 1st batch default metadata, with quality 1 + // Doc 4 should also use the 1st batch default meta-data, with quality 1 page = jdm.read("doc4.json"); rec = page.next(); jdm.readMetadata(rec.getUri(), mh); assertEquals("default quality",1,mh.getQuality()); assertTrue("default collections reset",mh.getCollections().isEmpty()); - // Doc5 should use the 2nd batch default metadata, with quality 2 + // Doc5 should use the 2nd batch default meta-data, with quality 2 page = jdm.read("doc5.json"); rec = page.next(); jdm.readMetadata(rec.getUri(), mh); assertEquals("default quality",2,mh.getQuality()); } + + /* + * Purpose : To read JSON files from file system and write into DB. + * Use JacksonHandle to load JSON files using bulk write set. + * Test Bulk Read to see you can read the document specific meta-data. + */ + + @Test + public void testWriteMultiJSONFilesDefaultMetadata() throws Exception + { + String docId[] = {"/original.json","/updated.json","/constraint1.json"}; + String jsonFilename1 = "json-original.json"; + String jsonFilename2 = "json-updated.json"; + String jsonFilename3 = "constraint1.json"; + + File jsonFile1 = new File("src/test/java/com/marklogic/javaclient/data/" + jsonFilename1); + File jsonFile2 = new File("src/test/java/com/marklogic/javaclient/data/" + jsonFilename2); + File jsonFile3 = new File("src/test/java/com/marklogic/javaclient/data/" + jsonFilename3); + + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); + DocumentWriteSet writeset = docMgr.newWriteSet(); + // put meta-data + DocumentMetadataHandle mh = setMetadata(); + DocumentMetadataHandle mhRead = new DocumentMetadataHandle(); + + ObjectMapper mapper = new ObjectMapper(); + + JacksonHandle jacksonHandle1 = new JacksonHandle(); + JacksonHandle jacksonHandle2 = new JacksonHandle(); + JacksonHandle jacksonHandle3 = new JacksonHandle(); + + JsonNode originalNode = mapper.readTree(jsonFile1); + jacksonHandle1.set(originalNode); + jacksonHandle1.withFormat(Format.JSON); + + JsonNode updatedNode = mapper.readTree(jsonFile2); + jacksonHandle2.set(updatedNode); + jacksonHandle2.withFormat(Format.JSON); + + JsonNode constraintNode = mapper.readTree(jsonFile3); + jacksonHandle3.set(constraintNode); + jacksonHandle3.withFormat(Format.JSON); + + writeset.addDefault(mh); + writeset.add(docId[0], jacksonHandle1); + writeset.add(docId[1], jacksonHandle2); + writeset.add(docId[2], jacksonHandle3); + + docMgr.write(writeset); + + DocumentPage page = docMgr.read(docId); + + while(page.hasNext()){ + DocumentRecord rec = page.next(); + docMgr.readMetadata(rec.getUri(), mhRead); + System.out.println(rec.getUri()); + validateMetadata(mhRead); + } + validateMetadata(mhRead); + mhRead = null; + } @AfterClass public static void tearDown() throws Exception From 281afdc02ed17befd1cef7b520b9d17ae8caeedf Mon Sep 17 00:00:00 2001 From: Ajit George Date: Wed, 17 Sep 2014 13:45:34 -0700 Subject: [PATCH 115/357] Removed deprecated methods and added test method to verify newFactory() method. --- ...tBulkReadWriteWithJacksonParserHandle.java | 276 ++++++++++++++---- 1 file changed, 225 insertions(+), 51 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonParserHandle.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonParserHandle.java index 95cbe5c4f..45ea09828 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonParserHandle.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonParserHandle.java @@ -1,56 +1,63 @@ package com.marklogic.javaclient; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import java.io.ByteArrayOutputStream; -import java.io.Reader; -import java.io.StringReader; +import java.io.File; +import java.io.IOException; import java.util.Calendar; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.skyscreamer.jsonassert.JSONAssert; +import org.xml.sax.SAXException; import com.fasterxml.jackson.core.JsonFactory; -import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; import com.marklogic.client.DatabaseClient; import com.marklogic.client.DatabaseClientFactory; import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.DocumentManager.Metadata; import com.marklogic.client.document.DocumentPage; import com.marklogic.client.document.DocumentRecord; import com.marklogic.client.document.DocumentWriteSet; import com.marklogic.client.document.JSONDocumentManager; -import com.marklogic.client.document.DocumentManager.Metadata; +import com.marklogic.client.document.XMLDocumentManager; import com.marklogic.client.io.DocumentMetadataHandle; import com.marklogic.client.io.DocumentMetadataHandle.Capability; import com.marklogic.client.io.DocumentMetadataHandle.DocumentCollections; import com.marklogic.client.io.DocumentMetadataHandle.DocumentPermissions; import com.marklogic.client.io.DocumentMetadataHandle.DocumentProperties; +import com.marklogic.client.io.marker.ContentHandle; +import com.marklogic.client.io.marker.ContentHandleFactory; import com.marklogic.client.io.Format; +import com.marklogic.client.io.JacksonDatabindHandle; import com.marklogic.client.io.JacksonHandle; import com.marklogic.client.io.JacksonParserHandle; -import com.marklogic.client.io.ReaderHandle; +import com.marklogic.client.io.StringHandle; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.RawQueryByExampleDefinition; +import com.marklogic.client.query.QueryManager.QueryView; /* - * This test is designed to to test all of bulk reads and write of JSON with JacksonHandle Manager by passing set of uris + * This test is designed to to test all of bulk reads and write of JSON with JacksonParserHandle Manager by passing set of uris * and also by descriptors. */ public class TestBulkReadWriteWithJacksonParserHandle extends BasicJavaClientREST { - private static final int BATCH_SIZE = 100; private static final String DIRECTORY = "/bulkread/"; - private static String dbName = "TestBulkReadWriteWithJacksonParserDB"; - private static String[] fNames = { "TestBulkReadWriteWithJacksonParserDB-1" }; + private static String dbName = "TestBulkJacksonParserDB"; + private static String[] fNames = { "TestBulkJacksonParserDB-1" }; private static String restServerName = "REST-Java-Client-API-Server"; private static int restPort = 8011; private DatabaseClient client; @@ -58,6 +65,7 @@ public class TestBulkReadWriteWithJacksonParserHandle extends @BeforeClass public static void setUp() throws Exception { System.out.println("In setup"); + System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); setupJavaRESTServer(dbName, fNames[0], restServerName, restPort); setupAppServicesConstraint(dbName); @@ -78,7 +86,7 @@ public void testCleanUp() throws Exception { } public DocumentMetadataHandle setMetadata() { - // create and initialize a handle on the metadata + // create and initialize a handle on the meta-data DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); metadataHandle.getCollections().addAll("my-collection1", "my-collection2"); @@ -95,7 +103,7 @@ public DocumentMetadataHandle setMetadata() { } public void validateMetadata(DocumentMetadataHandle mh) { - // get metadata values + // get meta-data values DocumentProperties properties = mh.getProperties(); DocumentPermissions permissions = mh.getPermissions(); DocumentCollections collections = mh.getCollections(); @@ -138,7 +146,7 @@ public void validateMetadata(DocumentMetadataHandle mh) { } public void validateDefaultMetadata(DocumentMetadataHandle mh){ - // get metadata values + // get meta-data values DocumentProperties properties = mh.getProperties(); DocumentPermissions permissions = mh.getPermissions(); DocumentCollections collections = mh.getCollections(); @@ -163,7 +171,12 @@ public void validateDefaultMetadata(DocumentMetadataHandle mh){ assertEquals("Document collections difference", expectedCollections, actualCollections); } - + + /* + * This test verifies multiple JSON content can be written with no meta-data in bulk write set. + * Use JacksonParserHandle as content handler. + * Verified by reading individual documents. + */ @Test public void testWriteMultipleJSONDocs() throws Exception { String docId[] = { "/a.json", "/b.json", "/c.json" }; @@ -181,9 +194,9 @@ public void testWriteMultipleJSONDocs() throws Exception { JacksonParserHandle jacksonParserHandle2 = new JacksonParserHandle(); JacksonParserHandle jacksonParserHandle3 = new JacksonParserHandle(); - jacksonParserHandle1.set(f.createJsonParser(json1)); - jacksonParserHandle2.set(f.createJsonParser(json2)); - jacksonParserHandle3.set(f.createJsonParser(json3)); + jacksonParserHandle1.set(f.createParser(json1)); + jacksonParserHandle2.set(f.createParser(json2)); + jacksonParserHandle3.set(f.createParser(json3)); writeset.add(docId[0], jacksonParserHandle1); writeset.add(docId[1], jacksonParserHandle2); @@ -191,20 +204,69 @@ public void testWriteMultipleJSONDocs() throws Exception { docMgr.write(writeset); - JacksonHandle r1 = new JacksonHandle(); - docMgr.read(docId[0], r1); - JSONAssert.assertEquals(json1, r1.toString(), true); + //Using JacksonHandle to read back from database. + JacksonHandle jacksonhandle = new JacksonHandle(); + docMgr.read(docId[0], jacksonhandle); + JSONAssert.assertEquals(json1, jacksonhandle.toString(), true); - docMgr.read(docId[1], r1); - JSONAssert.assertEquals(json2, r1.toString(), true); + docMgr.read(docId[1], jacksonhandle); + JSONAssert.assertEquals(json2, jacksonhandle.toString(), true); - docMgr.read(docId[2], r1); - JSONAssert.assertEquals(json3, r1.toString(), true); + docMgr.read(docId[2], jacksonhandle); + JSONAssert.assertEquals(json3, jacksonhandle.toString(), true); + } + + /* + * This test verifies multiple JSON content can be written using newFactory method. + * Use JacksonParserHandle as content handler. + * Verified by reading individual documents. + */ + @Test + public void testWriteMultipleJSONDocsFromFactory() throws Exception { + String docId[] = { "/a.json", "/b.json", "/c.json" }; + String json1 = new String("{\"animal\":\"dog\", \"says\":\"woof\"}"); + String json2 = new String("{\"animal\":\"cat\", \"says\":\"meow\"}"); + String json3 = new String("{\"animal\":\"rat\", \"says\":\"keek\"}"); + + JsonFactory f = new JsonFactory(); + + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); + DocumentWriteSet writeset = docMgr.newWriteSet(); + + //Create a content Factory from JacksonDatabindHandle that will handle writes. + ContentHandleFactory ch = JacksonParserHandle.newFactory(); + + //Instantiate a handle. + JacksonParserHandle jacksonParserHandle1 = (JacksonParserHandle)ch.newHandle(JsonParser.class); + JacksonParserHandle jacksonParserHandle2 = (JacksonParserHandle)ch.newHandle(JsonParser.class); + JacksonParserHandle jacksonParserHandle3 = (JacksonParserHandle)ch.newHandle(JsonParser.class); + + jacksonParserHandle1.set(f.createParser(json1)); + jacksonParserHandle2.set(f.createParser(json2)); + jacksonParserHandle3.set(f.createParser(json3)); + + writeset.add(docId[0], jacksonParserHandle1); + writeset.add(docId[1], jacksonParserHandle2); + writeset.add(docId[2], jacksonParserHandle3); + + docMgr.write(writeset); + + //Using JacksonHandle to read back from database. + JacksonHandle jacksonhandle = new JacksonHandle(); + docMgr.read(docId[0], jacksonhandle); + JSONAssert.assertEquals(json1, jacksonhandle.toString(), true); + + docMgr.read(docId[1], jacksonhandle); + JSONAssert.assertEquals(json2, jacksonhandle.toString(), true); + + docMgr.read(docId[2], jacksonhandle); + JSONAssert.assertEquals(json3, jacksonhandle.toString(), true); } /* * - * Use JacksonHandle to load json strings using bulk write set. Test Bulk + * Use JacksonParserHandle to load JSON strings using bulk write set. Test Bulk * Read to see you can read the document specific meta-data. */ @@ -221,16 +283,17 @@ public void testWriteMultipleJSONDocsWithDefaultMetadata() throws Exception { docMgr.setMetadataCategories(Metadata.ALL); DocumentWriteSet writeset = docMgr.newWriteSet(); - // put metadata + // put meta-data DocumentMetadataHandle mh = setMetadata(); + DocumentMetadataHandle mhRead = new DocumentMetadataHandle(); JacksonParserHandle jacksonParserHandle1 = new JacksonParserHandle(); JacksonParserHandle jacksonParserHandle2 = new JacksonParserHandle(); JacksonParserHandle jacksonParserHandle3 = new JacksonParserHandle(); - jacksonParserHandle1.set(f.createJsonParser(json1)); - jacksonParserHandle2.set(f.createJsonParser(json2)); - jacksonParserHandle3.set(f.createJsonParser(json3)); + jacksonParserHandle1.set(f.createParser(json1)); + jacksonParserHandle2.set(f.createParser(json2)); + jacksonParserHandle3.set(f.createParser(json3)); writeset.addDefault(mh); writeset.add(docId[0], jacksonParserHandle1); @@ -243,16 +306,16 @@ public void testWriteMultipleJSONDocsWithDefaultMetadata() throws Exception { while (page.hasNext()) { DocumentRecord rec = page.next(); - docMgr.readMetadata(rec.getUri(), mh); + docMgr.readMetadata(rec.getUri(), mhRead); System.out.println(rec.getUri()); - validateMetadata(mh); + validateMetadata(mhRead); } - validateMetadata(mh); + validateMetadata(mhRead); } /* * - * Use JacksonHandle to load json strings using bulk write set. Test Bulk + * Use JacksonParserHandle to load JSON strings using bulk write set. Test Bulk * Read to see you can read all the documents */ @Test @@ -266,7 +329,7 @@ public void testWriteMultipleJSONDocsWithDefaultMetadata2() String doc4 = new String("{\"animal\": \"lion\", \"says\": \"roar\"}"); String doc5 = new String("{\"animal\": \"man\", \"says\": \"hello\"}"); - // Synthesize input metadata + // Synthesize input meta-data DocumentMetadataHandle defaultMetadata1 = new DocumentMetadataHandle() .withQuality(1); DocumentMetadataHandle defaultMetadata2 = new DocumentMetadataHandle() @@ -288,22 +351,22 @@ public void testWriteMultipleJSONDocsWithDefaultMetadata2() JacksonParserHandle jacksonParserHandle4 = new JacksonParserHandle(); JacksonParserHandle jacksonParserHandle5 = new JacksonParserHandle(); - jacksonParserHandle1.set(f.createJsonParser(doc1)); - jacksonParserHandle2.set(f.createJsonParser(doc2)); - jacksonParserHandle3.set(f.createJsonParser(doc3)); - jacksonParserHandle4.set(f.createJsonParser(doc4)); - jacksonParserHandle5.set(f.createJsonParser(doc5)); + jacksonParserHandle1.set(f.createParser(doc1)); + jacksonParserHandle2.set(f.createParser(doc2)); + jacksonParserHandle3.set(f.createParser(doc3)); + jacksonParserHandle4.set(f.createParser(doc4)); + jacksonParserHandle5.set(f.createParser(doc5)); - // use system default metadata + // use system default meta-data batch.add("doc1.json", jacksonParserHandle1); - // using batch default metadata + // using batch default meta-data batch.addDefault(defaultMetadata1); - batch.add("doc2.json", jacksonParserHandle2); // batch default metadata + batch.add("doc2.json", jacksonParserHandle2); // batch default meta-data batch.add("doc3.json", docSpecificMetadata, jacksonParserHandle3); - batch.add("doc4.json", jacksonParserHandle4); // batch default metadata + batch.add("doc4.json", jacksonParserHandle4); // batch default meta-data - // replace batch default metadata with new metadata + // replace batch default meta-data with new meta-data batch.addDefault(defaultMetadata2); batch.add("doc5.json", jacksonParserHandle5); // batch default @@ -320,7 +383,7 @@ public void testWriteMultipleJSONDocsWithDefaultMetadata2() validateDefaultMetadata(mh); assertEquals("default quality", 0, mh.getQuality()); - // Doc2 should use the first batch default metadata, with quality 1 + // Doc2 should use the first batch default meta-data, with quality 1 page = jdm.read("doc2.json"); rec = page.next(); jdm.readMetadata(rec.getUri(), mh); @@ -330,10 +393,10 @@ public void testWriteMultipleJSONDocsWithDefaultMetadata2() // Doc3 should have the system default document quality (0) because // quality - // was not included in the document-specific metadata. It should be in + // was not included in the document-specific meta-data. It should be in // the // collection "mySpecificCollection", from the document-specific - // metadata. + // meta-data. page = jdm.read("doc3.json"); rec = page.next(); @@ -352,20 +415,131 @@ public void testWriteMultipleJSONDocsWithDefaultMetadata2() } System.out.println(); - // Doc 4 should also use the 1st batch default metadata, with quality 1 + // Doc 4 should also use the 1st batch default meta-data, with quality 1 page = jdm.read("doc4.json"); rec = page.next(); jdm.readMetadata(rec.getUri(), mh); assertEquals("default quality", 1, mh.getQuality()); assertTrue("default collections reset", mh.getCollections().isEmpty()); - // Doc5 should use the 2nd batch default metadata, with quality 2 + // Doc5 should use the 2nd batch default meta-data, with quality 2 page = jdm.read("doc5.json"); rec = page.next(); jdm.readMetadata(rec.getUri(), mh); assertEquals("default quality", 2, mh.getQuality()); } + + /* + * Purpose : To read JSON files from file system and write into DB. + * Use JacksonParserHandle to load JSON files using bulk write set. + * Test Bulk Read to see you can read the document specific meta-data. + */ + + @Test + public void testWriteMultiJSONFilesDefaultMetadata() throws Exception + { + String docId[] = {"/original.json","/updated.json","/constraint1.json"}; + String jsonFilename1 = "json-original.json"; + String jsonFilename2 = "json-updated.json"; + String jsonFilename3 = "constraint1.json"; + + File jsonFile1 = new File("src/test/java/com/marklogic/javaclient/data/" + jsonFilename1); + File jsonFile2 = new File("src/test/java/com/marklogic/javaclient/data/" + jsonFilename2); + File jsonFile3 = new File("src/test/java/com/marklogic/javaclient/data/" + jsonFilename3); + + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); + DocumentWriteSet writeset = docMgr.newWriteSet(); + // put meta-data + DocumentMetadataHandle mh = setMetadata(); + DocumentMetadataHandle mhRead = new DocumentMetadataHandle(); + + JsonFactory f = new JsonFactory(); + + JacksonParserHandle jacksonParserHandle1 = new JacksonParserHandle(); + JacksonParserHandle jacksonParserHandle2 = new JacksonParserHandle(); + JacksonParserHandle jacksonParserHandle3 = new JacksonParserHandle(); + + jacksonParserHandle1.set(f.createParser(jsonFile1)); + jacksonParserHandle2.set(f.createParser(jsonFile2)); + jacksonParserHandle3.set(f.createParser(jsonFile3)); + + writeset.addDefault(mh); + writeset.add(docId[0], jacksonParserHandle1); + writeset.add(docId[1], jacksonParserHandle2); + writeset.add(docId[2], jacksonParserHandle3); + + docMgr.write(writeset); + + DocumentPage page = docMgr.read(docId); + + while(page.hasNext()){ + DocumentRecord rec = page.next(); + docMgr.readMetadata(rec.getUri(), mhRead); + System.out.println(rec.getUri()); + validateMetadata(mhRead); + } + validateMetadata(mhRead); + mhRead = null; + } + + /* + * Purpose: Bulk Search with JacksonParserHandle + * Use JacksonParserHandle to retrieve JSON bulk search result set. Test Bulk + * Search with JacksonParserHandle + */ + @Test + public void testBulkSearchQBEWithJSONResponseFormat() throws IOException, ParserConfigurationException, SAXException, TransformerException { + int count; + //Creating a xml document manager for bulk search + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + //using QBE for query definition and set the search criteria + + QueryManager queryMgr = client.newQueryManager(); + String queryAsString = "{\"$query\": { \"says\": {\"$word\":\"woof\",\"$exact\": false}}}"; + RawQueryByExampleDefinition qd = queryMgr.newRawQueryByExampleDefinition(new StringHandle(queryAsString).withFormat(Format.JSON)); + + // set document manager level settings for search response + docMgr.setPageLength(25); + docMgr.setSearchView(QueryView.RESULTS); + docMgr.setResponseFormat(Format.JSON); + + // Search for documents where content has bar and get first result record, get search handle on it,Use DOMHandle to read results + JacksonParserHandle sh = new JacksonParserHandle(); + DocumentPage page; + + long pageNo=1; + do{ + count=0; + page = docMgr.search(qd, pageNo,sh); + if(pageNo >1){ + assertFalse("Is this first Page", page.isFirstPage()); + assertTrue("Is page has previous page ?",page.hasPreviousPage()); + } + while(page.hasNext()){ + DocumentRecord rec = page.next(); + rec.getFormat(); + validateRecord(rec,Format.JSON); + System.out.println(rec.getContent(new StringHandle()).get().toString()); + count++; + } + + // Add additional asserts once JacksonParserHandle is ready to handle bulk Search set. + + //assertTrue("Page start in results and on page",sh.get().get("start").asLong() == page.getStart()); + assertEquals("document count", page.size(),count); + // assertEquals("Page Number #",pageNo,page.getPageNumber()); + pageNo = pageNo + page.getPageSize(); + }while(!page.isLastPage() && page.hasContent() ); + + assertEquals("page count is ",5,page.getTotalPages()); + assertTrue("Page has previous page ?",page.hasPreviousPage()); + assertEquals("page size", 25,page.getPageSize()); + assertEquals("document count", 102,page.getTotalSize()); + + } + @AfterClass public static void tearDown() throws Exception { System.out.println("In tear down"); From 1d09a08ea232851369f240f73ba902f98546b6cf Mon Sep 17 00:00:00 2001 From: Ajit George Date: Wed, 17 Sep 2014 13:49:21 -0700 Subject: [PATCH 116/357] New unit test for JacksonDataBindHandle content handler. --- .../TestBulkReadWriteWithJacksonDataBind.java | 382 ++++++++++++++++++ 1 file changed, 382 insertions(+) create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonDataBind.java diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonDataBind.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonDataBind.java new file mode 100644 index 000000000..35e55fc74 --- /dev/null +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonDataBind.java @@ -0,0 +1,382 @@ +package com.marklogic.javaclient; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.util.Calendar; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.fasterxml.jackson.core.JsonFactory; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.DocumentManager.Metadata; +import com.marklogic.client.document.DocumentRecord; +import com.marklogic.client.document.DocumentWriteSet; +import com.marklogic.client.document.JSONDocumentManager; +import com.marklogic.client.io.DocumentMetadataHandle; +import com.marklogic.client.io.DocumentMetadataHandle.Capability; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentCollections; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentPermissions; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentProperties; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.JacksonDatabindHandle; +import com.marklogic.client.io.marker.ContentHandleFactory; + +/* + * This test is designed to to test all of bulk reads and write of JSON with JacksonDataBindHandle Manager by passing set of uris + * and also by descriptors. + */ + +public class TestBulkReadWriteWithJacksonDataBind extends + BasicJavaClientREST { + private static final String DIRECTORY = "/bulkread/"; + private static String dbName = "TestBulkJacksonDataBindDB"; + private static String[] fNames = { "TestBulkJacksonDataBindDB-1" }; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client; + + @BeforeClass + public static void setUp() throws Exception { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName, restPort); + setupAppServicesConstraint(dbName); + } + + @Before + public void testSetup() throws Exception { + // create new connection for each test below + client = DatabaseClientFactory.newClient("localhost", restPort, + "rest-admin", "x", Authentication.DIGEST); + } + + @After + public void testCleanUp() throws Exception { + System.out.println("Running CleanUp script"); + // release client + client.release(); + } + + public DocumentMetadataHandle setMetadata() { + // create and initialize a handle on the meta-data + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); + metadataHandle.getCollections().addAll("my-collection1", + "my-collection2"); + metadataHandle.getPermissions().add("app-user", Capability.UPDATE, + Capability.READ); + metadataHandle.getProperties().put("reviewed", true); + metadataHandle.getProperties().put("myString", "foo"); + metadataHandle.getProperties().put("myInteger", 10); + metadataHandle.getProperties().put("myDecimal", 34.56678); + metadataHandle.getProperties().put("myCalendar", + Calendar.getInstance().get(Calendar.YEAR)); + metadataHandle.setQuality(23); + return metadataHandle; + } + + public void validateMetadata(DocumentMetadataHandle mh) { + // get metadata values + DocumentProperties properties = mh.getProperties(); + DocumentPermissions permissions = mh.getPermissions(); + DocumentCollections collections = mh.getCollections(); + + // Properties + // String expectedProperties = + // "size:5|reviewed:true|myInteger:10|myDecimal:34.56678|myCalendar:2014|myString:foo|"; + String actualProperties = getDocumentPropertiesString(properties); + boolean result = actualProperties.contains("size:5|"); + assertTrue("Document properties count", result); + + // Permissions + String actualPermissions = getDocumentPermissionsString(permissions); + System.out.println(actualPermissions); + + assertTrue("Document permissions difference in size value", + actualPermissions.contains("size:3")); + //assertTrue( + // "Document permissions difference in flexrep-eval permission", + // actualPermissions.contains("flexrep-eval:[READ]")); + assertTrue("Document permissions difference in rest-reader permission", + actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", + actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue( + "Document permissions difference in app-user permissions", + (actualPermissions.contains("app-user:[UPDATE, READ]") || actualPermissions + .contains("app-user:[READ, UPDATE]"))); + + // Collections + String actualCollections = getDocumentCollectionsString(collections); + System.out.println(collections); + + assertTrue("Document collections difference in size value", + actualCollections.contains("size:2")); + assertTrue("my-collection1 not found", + actualCollections.contains("my-collection1")); + assertTrue("my-collection2 not found", + actualCollections.contains("my-collection2")); + } + + public void validateDefaultMetadata(DocumentMetadataHandle mh){ + // get metadata values + DocumentProperties properties = mh.getProperties(); + DocumentPermissions permissions = mh.getPermissions(); + DocumentCollections collections = mh.getCollections(); + + // Properties + String actualProperties = getDocumentPropertiesString(properties); + boolean result =actualProperties.contains("size:0|"); + System.out.println(actualProperties +result); + assertTrue("Document default last modified properties count1?", result); + + // Permissions + String actualPermissions = getDocumentPermissionsString(permissions); + + assertTrue("Document permissions difference in size value", actualPermissions.contains("size:2")); + //assertTrue("Document permissions difference in flexrep-eval permission", actualPermissions.contains("flexrep-eval:[READ]")); + assertTrue("Document permissions difference in rest-reader permission", actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", actualPermissions.contains("rest-writer:[UPDATE]")); + + // Collections + String expectedCollections = "size:0|"; + String actualCollections = getDocumentCollectionsString(collections); + + assertEquals("Document collections difference", expectedCollections, actualCollections); + } + + /* + * This method is place holder to test JacksonDatabindHandle handling file writing / reading (Streams) + * Need to be completed once JacksonDatabindHandle has the necessary support. + */ + @Test + public void testWriteMultipleJSONFiles() throws Exception { + + String docId[] = {"/apple.json","/microsoft.json","/hp.json"}; + + // Work yet to be done + // Each of these files can contain multiple products. Need to Read these files and then the JSON content. + + String jsonFilename1 = "AppleProducts.json"; + String jsonFilename2 = "MicrosoftProducts.json"; + String jsonFilename3 = "HpProducts.json"; + + File jsonFile1 = new File("src/test/java/com/marklogic/javaclient/data/" + jsonFilename1); + File jsonFile2 = new File("src/test/java/com/marklogic/javaclient/data/" + jsonFilename2); + File jsonFile3 = new File("src/test/java/com/marklogic/javaclient/data/" + jsonFilename3); + + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); + DocumentWriteSet writeset = docMgr.newWriteSet(); + // put meta-data + DocumentMetadataHandle mh = setMetadata(); + DocumentMetadataHandle mhRead = new DocumentMetadataHandle(); + + JsonFactory f = new JsonFactory(); + + JacksonDatabindHandle handle1 = new JacksonDatabindHandle(File.class); + JacksonDatabindHandle handle2 = new JacksonDatabindHandle(File.class); + JacksonDatabindHandle handle3 = new JacksonDatabindHandle(File.class); + + // Add meta-data + writeset.addDefault(mh); + + handle1.set(jsonFile1); + handle2.set(jsonFile2); + handle3.set(jsonFile3); + + writeset.add(docId[0], handle1); + writeset.add(docId[1], handle2); + writeset.add(docId[2], handle3); + + docMgr.write(writeset); + + //Read it back into JacksonDatabindHandle Product + JacksonDatabindHandle handleRead = new JacksonDatabindHandle(Product.class); + + // Do we iterate individually ? + docMgr.read(docId[0], handleRead); + Product product1 = (Product) handleRead.get(); + + assertTrue("Did not return a iPhone 6", product1.getName().equalsIgnoreCase("iPhone 6")); + assertTrue("Did not return a Mobile Phone", product1.getIndustry().equalsIgnoreCase("Mobile Phone")); + assertTrue("Did not return a Mobile Phone", product1.getDescription().equalsIgnoreCase("New iPhone 6")); + + } + + @Test + public void testWriteMultipleJSONDocsFromStrings() throws Exception { + + String docId[] = { "/iphone.json", "/imac.json", "/ipad.json" }; + String json1 = new String("{ \"name\":\"iPhone 6\" , \"industry\":\"Mobile Phone\" , \"description\":\"New iPhone 6\"}"); + String json2 = new String("{ \"name\":\"iMac\" , \"industry\":\"Desktop\", \"description\":\"Air Book OS X\" }"); + String json3 = new String("{ \"name\":\"iPad\" , \"industry\":\"Tablet\", \"description\":\"iPad Mini\" }"); + + DocumentMetadataHandle mhRead = new DocumentMetadataHandle(); + + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); + DocumentWriteSet writeset = docMgr.newWriteSet(); + // put meta-data + DocumentMetadataHandle mh = setMetadata(); + + JacksonDatabindHandle handle1 = new JacksonDatabindHandle(String.class); + JacksonDatabindHandle handle2 = new JacksonDatabindHandle(String.class); + JacksonDatabindHandle handle3 = new JacksonDatabindHandle(String.class); + + writeset.addDefault(mh); + handle1.set(json1); + handle2.set(json2); + handle3.set(json3); + + writeset.add(docId[0], handle1); + writeset.add(docId[1], handle2); + writeset.add(docId[2], handle3); + + docMgr.write(writeset); + + //Read it back into JacksonDatabindHandle Product + JacksonDatabindHandle jacksonDBReadHandle = new JacksonDatabindHandle(Product.class); + docMgr.read(docId[0], jacksonDBReadHandle); + Product product1 = (Product) jacksonDBReadHandle.get(); + + assertTrue("Did not return a iPhone 6", product1.getName().equalsIgnoreCase("iPhone 6")); + assertTrue("Did not return a Mobile Phone", product1.getIndustry().equalsIgnoreCase("Mobile Phone")); + assertTrue("Did not return a Mobile Phone", product1.getDescription().equalsIgnoreCase("New iPhone 6")); + + docMgr.readMetadata(docId[0], mhRead); + validateMetadata(mhRead); + + docMgr.read(docId[1],jacksonDBReadHandle); + Product product2 = (Product) jacksonDBReadHandle.get(); + assertTrue("Did not return a iMac", product2.getName().equalsIgnoreCase("iMac")); + assertTrue("Did not return a Desktop", product2.getIndustry().equalsIgnoreCase("Desktop")); + assertTrue("Did not return a Air Book OS X", product2.getDescription().equalsIgnoreCase("Air Book OS X")); + + docMgr.readMetadata(docId[1], mhRead); + validateMetadata(mhRead); + + docMgr.read(docId[2], jacksonDBReadHandle); + Product product3 = (Product) jacksonDBReadHandle.get(); + assertTrue("Did not return a iPad", product3.getName().equalsIgnoreCase("iPad")); + assertTrue("Did not return a Tablet", product3.getIndustry().equalsIgnoreCase("Tablet")); + assertTrue("Did not return a iPad Mini", product3.getDescription().equalsIgnoreCase("iPad Mini")); + + docMgr.readMetadata(docId[2], mhRead); + validateMetadata(mhRead); + } + + /* + * Purpose: To test newFactory method with custom Pojo instances. + */ + @Test + public void testJacksonDataBindHandleFromFactory() throws Exception { + + String docId[] = { "/iphone.json", "/imac.json", "/ipad.json" }; + + // Create three custom POJO instances + + Product newProduct1 = new Product(); + newProduct1.setName("iPhone 6"); + newProduct1.setIndustry("Mobile Phone"); + newProduct1.setDescription("New iPhone 6"); + + Product newProduct2 = new Product(); + newProduct2.setName("iMac"); + newProduct2.setIndustry("Desktop"); + newProduct2.setDescription("Air Book OS X"); + + Product newProduct3 = new Product(); + newProduct3.setName("iPad"); + newProduct3.setIndustry("Tablet"); + newProduct3.setDescription("iPad Mini"); + + //Create a content Factory from JacksonDatabindHandle that will handle POJO class type. + ContentHandleFactory ch = JacksonDatabindHandle.newFactory(Product.class); + + //Instantiate a handle for each POJO instance. + JacksonDatabindHandle handle1 = (JacksonDatabindHandle) ch.newHandle(Product.class); + JacksonDatabindHandle handle2 = (JacksonDatabindHandle) ch.newHandle(Product.class); + JacksonDatabindHandle handle3 = (JacksonDatabindHandle) ch.newHandle(Product.class); + + //Assigns the custom POJO as the content. + handle1.set(newProduct1); + handle2.set(newProduct2); + handle3.set(newProduct3); + + //Specifies the format of the content. + handle1.withFormat(Format.JSON); + handle2.withFormat(Format.JSON); + handle3.withFormat(Format.JSON); + + DocumentMetadataHandle mhRead = new DocumentMetadataHandle(); + + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); + DocumentWriteSet writeset = docMgr.newWriteSet(); + // put meta-data + DocumentMetadataHandle mh = setMetadata(); + + writeset.addDefault(mh); + + writeset.add(docId[0], handle1); + writeset.add(docId[1], handle2); + writeset.add(docId[2], handle3); + + docMgr.write(writeset); + + //Read it back into JacksonDatabindHandle Product + JacksonDatabindHandle jacksonDBReadHandle = new JacksonDatabindHandle(Product.class); + docMgr.read(docId[0], jacksonDBReadHandle); + Product product1 = (Product) jacksonDBReadHandle.get(); + + assertTrue("Did not return a iPhone 6", product1.getName().equalsIgnoreCase("iPhone 6")); + assertTrue("Did not return a Mobile Phone", product1.getIndustry().equalsIgnoreCase("Mobile Phone")); + assertTrue("Did not return a Mobile Phone", product1.getDescription().equalsIgnoreCase("New iPhone 6")); + + docMgr.readMetadata(docId[0], mhRead); + validateMetadata(mhRead); + + docMgr.read(docId[1],jacksonDBReadHandle); + Product product2 = (Product) jacksonDBReadHandle.get(); + assertTrue("Did not return a iMac", product2.getName().equalsIgnoreCase("iMac")); + assertTrue("Did not return a Desktop", product2.getIndustry().equalsIgnoreCase("Desktop")); + assertTrue("Did not return a Air Book OS X", product2.getDescription().equalsIgnoreCase("Air Book OS X")); + + docMgr.readMetadata(docId[1], mhRead); + validateMetadata(mhRead); + + docMgr.read(docId[2], jacksonDBReadHandle); + Product product3 = (Product) jacksonDBReadHandle.get(); + assertTrue("Did not return a iPad", product3.getName().equalsIgnoreCase("iPad")); + assertTrue("Did not return a Tablet", product3.getIndustry().equalsIgnoreCase("Tablet")); + assertTrue("Did not return a iPad Mini", product3.getDescription().equalsIgnoreCase("iPad Mini")); + + docMgr.readMetadata(docId[2], mhRead); + validateMetadata(mhRead); + } + + @AfterClass + public static void tearDown() throws Exception { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } + + public void validateRecord(DocumentRecord record, Format type) { + + assertNotNull("DocumentRecord should never be null", record); + assertNotNull("Document uri should never be null", record.getUri()); + assertTrue("Document uri should start with " + DIRECTORY, record + .getUri().startsWith(DIRECTORY)); + assertEquals("All records are expected to be in same format", type, + record.getFormat()); + + } +} \ No newline at end of file From d04ef7a414ab77d603cee1e0aa677c7d37108c5c Mon Sep 17 00:00:00 2001 From: sammefford Date: Wed, 17 Sep 2014 16:45:52 -0600 Subject: [PATCH 117/357] merging this into ServerEvaluationManager.java --- .../marklogic/client/eval/EvalBuilder.java | 32 ------------------- 1 file changed, 32 deletions(-) delete mode 100644 src/main/java/com/marklogic/client/eval/EvalBuilder.java diff --git a/src/main/java/com/marklogic/client/eval/EvalBuilder.java b/src/main/java/com/marklogic/client/eval/EvalBuilder.java deleted file mode 100644 index a8fdfa9ad..000000000 --- a/src/main/java/com/marklogic/client/eval/EvalBuilder.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2012-2014 MarkLogic Corporation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.marklogic.client.eval; - -import com.marklogic.client.Transaction; -import com.marklogic.client.io.marker.AbstractWriteHandle; - -public interface EvalBuilder { - public EvalBuilder addVariable(String name, String value); - public EvalBuilder addVariable(String name, Number value); - public EvalBuilder addVariable(String name, Boolean value); - public EvalBuilder addVariable(String name, AbstractWriteHandle value); - /** Like other *As convenience methods throughout the API, the Object value - * is managed by the Handle registered for that Class. */ - public EvalBuilder addVariableAs(String name, Object value); - public EvalBuilder database(String database); - public EvalBuilder transaction(Transaction transaction); - public ServerEval build(); -} From cf6aafd7797150123aabc955ff1ff4dd4da5a4a0 Mon Sep 17 00:00:00 2001 From: sammefford Date: Wed, 17 Sep 2014 16:53:06 -0600 Subject: [PATCH 118/357] functionality moved into ServerEvaluationManager --- .../client/eval/JSONVariableSet.java | 33 ------------------- .../client/eval/JavascriptEvalBuilder.java | 26 --------------- .../marklogic/client/eval/VariableSet.java | 22 ------------- .../marklogic/client/eval/XMLVariableSet.java | 25 -------------- .../client/eval/XQueryEvalBuilder.java | 25 -------------- 5 files changed, 131 deletions(-) delete mode 100644 src/main/java/com/marklogic/client/eval/JSONVariableSet.java delete mode 100644 src/main/java/com/marklogic/client/eval/JavascriptEvalBuilder.java delete mode 100644 src/main/java/com/marklogic/client/eval/VariableSet.java delete mode 100644 src/main/java/com/marklogic/client/eval/XMLVariableSet.java delete mode 100644 src/main/java/com/marklogic/client/eval/XQueryEvalBuilder.java diff --git a/src/main/java/com/marklogic/client/eval/JSONVariableSet.java b/src/main/java/com/marklogic/client/eval/JSONVariableSet.java deleted file mode 100644 index 18ab03cfd..000000000 --- a/src/main/java/com/marklogic/client/eval/JSONVariableSet.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2012-2014 MarkLogic Corporation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.marklogic.client.eval; - -import com.marklogic.client.io.marker.JSONWriteHandle; - -public interface JSONVariableSet extends VariableSet { - public JSONVariableSet add(String name, JSONWriteHandle value); - public JSONVariableSet addAs(String name, Object value); - /** Convenience method since boolean is applicable to both JSON and XML - * and therefore cannot be registered with a default handle. */ - public JSONVariableSet add(String name, boolean value); - /** Convenience method since Number is applicable to both JSON and XML - * and therefore cannot be registered with a default handle. */ - public JSONVariableSet add(String name, Number value); - /** Convenience method since dateTime is applicable to both JSON and XML - * and therefore cannot be registered with a default handle. */ - public JSONVariableSet add(String name, java.util.Calendar value); -} - diff --git a/src/main/java/com/marklogic/client/eval/JavascriptEvalBuilder.java b/src/main/java/com/marklogic/client/eval/JavascriptEvalBuilder.java deleted file mode 100644 index e8e30b8e9..000000000 --- a/src/main/java/com/marklogic/client/eval/JavascriptEvalBuilder.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright 2012-2014 MarkLogic Corporation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.marklogic.client.eval; - -import com.marklogic.client.io.marker.TextWriteHandle; - -public interface JavascriptEvalBuilder extends EvalBuilder { - public JavascriptEvalBuilder javascript(String javascript); - public JavascriptEvalBuilder javascript(TextWriteHandle javascript); -} - - - diff --git a/src/main/java/com/marklogic/client/eval/VariableSet.java b/src/main/java/com/marklogic/client/eval/VariableSet.java deleted file mode 100644 index aef5ed482..000000000 --- a/src/main/java/com/marklogic/client/eval/VariableSet.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright 2012-2014 MarkLogic Corporation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.marklogic.client.eval; - -import com.marklogic.client.Page; - -/** Marker interface implemented by {@link XMLVariableSet} and {@link JSONVariableSet} - * so ServerEval can accept either type of VariableSet. */ -public interface VariableSet {} diff --git a/src/main/java/com/marklogic/client/eval/XMLVariableSet.java b/src/main/java/com/marklogic/client/eval/XMLVariableSet.java deleted file mode 100644 index 30eeef6a4..000000000 --- a/src/main/java/com/marklogic/client/eval/XMLVariableSet.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright 2012-2014 MarkLogic Corporation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.marklogic.client.eval; - -import com.marklogic.client.io.marker.XMLWriteHandle; - -public interface XMLVariableSet extends VariableSet { - public XMLVariableSet add(String name, XMLWriteHandle value); - public XMLVariableSet addAs(String name, Object value); -} - - diff --git a/src/main/java/com/marklogic/client/eval/XQueryEvalBuilder.java b/src/main/java/com/marklogic/client/eval/XQueryEvalBuilder.java deleted file mode 100644 index 156a738b3..000000000 --- a/src/main/java/com/marklogic/client/eval/XQueryEvalBuilder.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright 2012-2014 MarkLogic Corporation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.marklogic.client.eval; - -import com.marklogic.client.io.marker.TextWriteHandle; - -public interface XQueryEvalBuilder extends EvalBuilder { - public XQueryEvalBuilder xquery(String xquery); - public XQueryEvalBuilder xquery(TextWriteHandle xquery); -} - - From 9a1b35effe43516032d0b5a86ccc06f70be67d8d Mon Sep 17 00:00:00 2001 From: sammefford Date: Wed, 17 Sep 2014 16:56:00 -0600 Subject: [PATCH 119/357] rename ServerEval to ServerEvaluationManager and expand capability to include EvalBuilder --- .../com/marklogic/client/DatabaseClient.java | 4 +- .../client/eval/ServerEvaluationManager.java | 42 +++++++++++++++++++ .../client/impl/DatabaseClientImpl.java | 4 +- .../com/marklogic/client/test/EvalTest.java | 13 +++--- 4 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 src/main/java/com/marklogic/client/eval/ServerEvaluationManager.java diff --git a/src/main/java/com/marklogic/client/DatabaseClient.java b/src/main/java/com/marklogic/client/DatabaseClient.java index b7f8a6661..dd0355f66 100644 --- a/src/main/java/com/marklogic/client/DatabaseClient.java +++ b/src/main/java/com/marklogic/client/DatabaseClient.java @@ -24,7 +24,7 @@ import com.marklogic.client.document.JSONDocumentManager; import com.marklogic.client.document.TextDocumentManager; import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.eval.ServerEval; +import com.marklogic.client.eval.ServerEvaluationManager; import com.marklogic.client.extensions.ResourceManager; import com.marklogic.client.admin.ExtensionMetadata; import com.marklogic.client.query.QueryManager; @@ -178,5 +178,5 @@ public T init(String resourceName, T resourceManager * @return the object implementing communication with the server */ public Object getClientImplementation(); - public ServerEval newServerEval(); + public ServerEvaluationManager newServerEval(); } diff --git a/src/main/java/com/marklogic/client/eval/ServerEvaluationManager.java b/src/main/java/com/marklogic/client/eval/ServerEvaluationManager.java new file mode 100644 index 000000000..d30d0ca03 --- /dev/null +++ b/src/main/java/com/marklogic/client/eval/ServerEvaluationManager.java @@ -0,0 +1,42 @@ +/* + * Copyright 2012-2014 MarkLogic Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.marklogic.client.eval; + +import com.marklogic.client.Transaction; +import com.marklogic.client.io.marker.AbstractReadHandle; +import com.marklogic.client.io.marker.AbstractWriteHandle; +import com.marklogic.client.io.marker.TextWriteHandle; + +public interface ServerEvaluationManager { + public ServerEvaluationManager xquery(String xquery); + public ServerEvaluationManager xquery(TextWriteHandle xquery); + public ServerEvaluationManager javascript(String javascript); + public ServerEvaluationManager javascript(TextWriteHandle javascript); + public ServerEvaluationManager xqueryModule(String modulePath); + public ServerEvaluationManager javascriptModule(String modulePath); + public ServerEvaluationManager addVariable(String name, String value); + public ServerEvaluationManager addVariable(String name, Number value); + public ServerEvaluationManager addVariable(String name, Boolean value); + public ServerEvaluationManager addVariable(String name, AbstractWriteHandle value); + /** Like other *As convenience methods throughout the API, the Object value + * is managed by the Handle registered for that Class. */ + public ServerEvaluationManager addVariableAs(String name, Object value); + public ServerEvaluationManager database(String database); + public ServerEvaluationManager transaction(Transaction transaction); + public T evalAs(Class responseType); + public H eval(H responseHandle); + public EvalResults eval(); +} diff --git a/src/main/java/com/marklogic/client/impl/DatabaseClientImpl.java b/src/main/java/com/marklogic/client/impl/DatabaseClientImpl.java index d410496a2..e7e8bd516 100644 --- a/src/main/java/com/marklogic/client/impl/DatabaseClientImpl.java +++ b/src/main/java/com/marklogic/client/impl/DatabaseClientImpl.java @@ -30,7 +30,7 @@ import com.marklogic.client.document.JSONDocumentManager; import com.marklogic.client.query.QueryManager; import com.marklogic.client.util.RequestLogger; -import com.marklogic.client.eval.ServerEval; +import com.marklogic.client.eval.ServerEvaluationManager; import com.marklogic.client.extensions.ResourceManager; import com.marklogic.client.DatabaseClientFactory.HandleFactoryRegistry; import com.marklogic.client.admin.ServerConfigurationManager; @@ -185,7 +185,7 @@ public RESTServices getServices() { } @Override - public ServerEval newServerEval() { + public ServerEvaluationManager newServerEval() { // TODO Auto-generated method stub return null; } diff --git a/src/test/java/com/marklogic/client/test/EvalTest.java b/src/test/java/com/marklogic/client/test/EvalTest.java index 26415fe03..e98e32931 100644 --- a/src/test/java/com/marklogic/client/test/EvalTest.java +++ b/src/test/java/com/marklogic/client/test/EvalTest.java @@ -35,7 +35,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.marklogic.client.eval.EvalResults; -import com.marklogic.client.eval.ServerEval; +import com.marklogic.client.eval.ServerEvaluationManager; import com.marklogic.client.io.JAXBDatatypeHandle; import com.marklogic.client.io.JacksonHandle; import com.marklogic.client.io.StringHandle; @@ -56,7 +56,7 @@ public static void afterClass() { @Test public void evalTest1() throws ParserConfigurationException { // hello world and response determined by implicit StringHandle which registered with String.class - ServerEval query = Common.client.newServerEval().xquery("'hello world'").build(); + ServerEvaluationManager query = Common.client.newServerEval().xquery("'hello world'"); String response = query.evalAs(String.class); assertEquals("Return should be 'hello world'", "hello world", response); @@ -64,8 +64,7 @@ public void evalTest1() throws ParserConfigurationException { query = Common.client.newServerEval() .xquery("declare variable $planet external;" + "'hello world from ' || $planet") - .addVariable("planet", "Mars") - .build(); + .addVariable("planet", "Mars"); StringHandle strResponse = query.eval(new StringHandle()); assertEquals("Return should be 'hello world from Mars'", "hello world from Mars", strResponse.get()); @@ -91,8 +90,7 @@ public void evalTest1() throws ParserConfigurationException { .addVariable("myBool", true) .addVariable("myInteger", 123) .addVariable("myDouble", 1.1) - .addVariableAs("myDate", septFirst) - .build(); + .addVariableAs("myDate", septFirst); EvalResults results = query.eval(); assertEquals("myString should = 'Mars'", "Mars", results.next().getAs(String.class)); assertEquals("myArray should = [\"item1\",\"item2\"]", "[\"item1\",\"item2\"]", @@ -146,8 +144,7 @@ public void evalTest1() throws ParserConfigurationException { .addVariable("myBool", true) .addVariable("myInteger", 123) .addVariable("myDouble", 1.1) - .addVariableAs("myDate", new GregorianCalendar(2014, Calendar.SEPTEMBER, 1)) - .build(); + .addVariableAs("myDate", new GregorianCalendar(2014, Calendar.SEPTEMBER, 1)); results = query.eval(); assertEquals("myString should = 'Mars'", "Mars", results.next().getAs(String.class)); assertEquals("myArray should = [\"item1\",\"item2\"]", "[\"item1\",\"item2\"]", From a8b3b89176c4bcc2a717aa9b8038904cffa8b4d9 Mon Sep 17 00:00:00 2001 From: sammefford Date: Wed, 17 Sep 2014 16:57:39 -0600 Subject: [PATCH 120/357] allow STRING==XS_STRING and BOOLEAN=XS_BOOLEAN; add JS_ prefix on JS only types --- src/main/java/com/marklogic/client/eval/EvalResult.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/marklogic/client/eval/EvalResult.java b/src/main/java/com/marklogic/client/eval/EvalResult.java index 6eb7050ef..17f9a881d 100644 --- a/src/main/java/com/marklogic/client/eval/EvalResult.java +++ b/src/main/java/com/marklogic/client/eval/EvalResult.java @@ -20,10 +20,10 @@ public interface EvalResult { enum Type { STRING, - NUMBER, BOOLEAN, - ARRAY, - OBJECT, + JS_NUMBER, + JS_ARRAY, + JS_OBJECT, NULL, XDM_ATOMIC, XDM_ATTRIBUTE, @@ -41,7 +41,6 @@ enum Type { XDM_VARIABLE, XS_ANYURI, XS_BASE64BINARY, - XS_BOOLEAN, XS_DATE, XS_DATETIME, XS_DAYTIMEDURATION, @@ -57,7 +56,6 @@ enum Type { XS_HEXBINARY, XS_INTEGER, XS_QNAME, - XS_STRING, XS_TIME }; public Type getType(); From 48fc06158e7ba03bd807c05ec7f3a1d710ec1249 Mon Sep 17 00:00:00 2001 From: sammefford Date: Wed, 17 Sep 2014 16:57:58 -0600 Subject: [PATCH 121/357] rename ServerEval to ServerEvaluationManager and expand capability to include EvalBuilder --- .../com/marklogic/client/eval/ServerEval.java | 31 ------------------- 1 file changed, 31 deletions(-) delete mode 100644 src/main/java/com/marklogic/client/eval/ServerEval.java diff --git a/src/main/java/com/marklogic/client/eval/ServerEval.java b/src/main/java/com/marklogic/client/eval/ServerEval.java deleted file mode 100644 index 601c3dfd9..000000000 --- a/src/main/java/com/marklogic/client/eval/ServerEval.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2012-2014 MarkLogic Corporation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.marklogic.client.eval; - -import com.marklogic.client.io.marker.AbstractReadHandle; -import com.marklogic.client.io.marker.TextWriteHandle; - -public interface ServerEval { - public EvalBuilder xquery(String xquery); - public EvalBuilder xquery(TextWriteHandle xquery); - public EvalBuilder javascript(String javascript); - public EvalBuilder javascript(TextWriteHandle javascript); - public EvalBuilder xqueryModule(String modulePath); - public EvalBuilder javascriptModule(String modulePath); - public T evalAs(Class responseType); - public H eval(H responseHandle); - public EvalResults eval(); -} From e773e5bcaf1ff9d6b7340c76cdc18f6c5ddcf3c4 Mon Sep 17 00:00:00 2001 From: sammefford Date: Thu, 18 Sep 2014 09:11:01 -0600 Subject: [PATCH 122/357] add commented http wire debug switches for next time we need to see what's really happening --- .../com/marklogic/client/test/util/TestServerBootstrapper.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/java/com/marklogic/client/test/util/TestServerBootstrapper.java b/src/test/java/com/marklogic/client/test/util/TestServerBootstrapper.java index 78abe0c19..dcf07c050 100644 --- a/src/test/java/com/marklogic/client/test/util/TestServerBootstrapper.java +++ b/src/test/java/com/marklogic/client/test/util/TestServerBootstrapper.java @@ -113,6 +113,9 @@ private void installBootstrapExtension() throws IOException { public static void main(String[] args) throws ClientProtocolException, IOException { + //System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + //System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog"); + TestServerBootstrapper bootstrapper = new TestServerBootstrapper(); if ((args.length == 1) && (args[0].equals("teardown"))) { From 167b798aadee8f4dc91cbc5302bf002b2d7baedc Mon Sep 17 00:00:00 2001 From: sammefford Date: Thu, 18 Sep 2014 09:22:54 -0600 Subject: [PATCH 123/357] align this with dev--we're not upgrading jersey on this branch yet --- pom.xml | 48 +++++++++++++----------------------------------- 1 file changed, 13 insertions(+), 35 deletions(-) diff --git a/pom.xml b/pom.xml index 2d9d5dad1..b369976a4 100644 --- a/pom.xml +++ b/pom.xml @@ -164,41 +164,24 @@ - org.glassfish.jersey.core + com.sun.jersey jersey-client - 2.12 + 1.17 + + + com.sun.jersey.contribs + jersey-apache-client4 + 1.17 + + + com.sun.jersey.contribs + jersey-multipart + 1.17 - - org.glassfish.jersey.media - jersey-media-multipart - 2.12 - - - com.sun.jersey - jersey-client - 1.17 - - - com.sun.jersey.contribs - jersey-apache-client4 - 1.17 - - - com.sun.jersey.contribs - jersey-multipart - 1.17 - - ch.qos.logback logback-classic - 1.0.12 + 1.1.2 commons-logging @@ -220,11 +203,6 @@ mimepull 1.9.4 - - ch.qos.logback - logback-core - 1.0.12 - org.slf4j slf4j-api From 7503936066fdb8f1ca992d836d3f7b5bced64484 Mon Sep 17 00:00:00 2001 From: sammefford Date: Thu, 18 Sep 2014 09:36:09 -0600 Subject: [PATCH 124/357] oops, I need the new jersey libs for XdbcEval on this branch --- pom.xml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pom.xml b/pom.xml index b369976a4..fb94d327a 100644 --- a/pom.xml +++ b/pom.xml @@ -163,6 +163,16 @@ + + org.glassfish.jersey.core + jersey-client + 2.12 + + + org.glassfish.jersey.media + jersey-media-multipart + 2.12 + com.sun.jersey jersey-client From e59ced9e5f490860e8e0c9bac13cc4d41c9d317f Mon Sep 17 00:00:00 2001 From: sammefford Date: Thu, 18 Sep 2014 09:37:13 -0600 Subject: [PATCH 125/357] based on team feedback we're going with EvalResultIterator and EvalResult instead of MultiPartResponsePage and BodyPart --- .../com/marklogic/client/http/BodyPart.java | 65 ------------------- .../client/http/MultiPartResponsePage.java | 21 ------ 2 files changed, 86 deletions(-) delete mode 100644 src/main/java/com/marklogic/client/http/BodyPart.java delete mode 100644 src/main/java/com/marklogic/client/http/MultiPartResponsePage.java diff --git a/src/main/java/com/marklogic/client/http/BodyPart.java b/src/main/java/com/marklogic/client/http/BodyPart.java deleted file mode 100644 index f0ac380e0..000000000 --- a/src/main/java/com/marklogic/client/http/BodyPart.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright 2012-2014 MarkLogic Corporation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.marklogic.client.http; - -import java.util.Map; - -import com.marklogic.client.io.marker.AbstractReadHandle; - -public interface BodyPart { - //Map> getHeaders(); - /** - * If this part is JSON, could be any of the following types: - *
-     *   string
-     *   number
-     *   boolean
-     *   array
-     *   object
-     *   null
-     * 
- * - * Or if this part is XML, could be any of the following types: - *
-     *   xs:anySimpleType,
-     *   xs:base64Binary,
-     *   xs:boolean,
-     *   xs:byte,
-     *   xs:date,
-     *   xs:dateTime,
-     *   xs:dayTimeDuration,
-     *   xs:decimal,
-     *   xs:double,
-     *   xs:duration,
-     *   xs:float,
-     *   xs:int,
-     *   xs:integer,
-     *   xs:long,
-     *   xs:short,
-     *   xs:string,
-     *   xs:time,
-     *   xs:unsignedInt,
-     *   xs:unsignedLong,
-     *   xs:unsignedShort,
-     *   xs:yearMonthDuration.
-     * 
- */ - //public String getType(); <- what does XDBC return? - // maybe this instead? - // public String getMimeType(); - public H get(H handle); - public T getAs(Class clazz); -} diff --git a/src/main/java/com/marklogic/client/http/MultiPartResponsePage.java b/src/main/java/com/marklogic/client/http/MultiPartResponsePage.java deleted file mode 100644 index 7b34faa18..000000000 --- a/src/main/java/com/marklogic/client/http/MultiPartResponsePage.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright 2012-2014 MarkLogic Corporation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.marklogic.client.http; - -import com.marklogic.client.Page; - -public interface MultiPartResponsePage extends Page { -} From f1eefa5a95123cbb619846a79450dd02f02c1adf Mon Sep 17 00:00:00 2001 From: sammefford Date: Thu, 18 Sep 2014 09:37:13 -0600 Subject: [PATCH 126/357] based on team feedback we're going with EvalResultIterator and EvalResult instead of MultiPartResponsePage and BodyPart --- .../com/marklogic/client/http/BodyPart.java | 65 ------------------- .../client/http/MultiPartResponsePage.java | 21 ------ 2 files changed, 86 deletions(-) delete mode 100644 src/main/java/com/marklogic/client/http/BodyPart.java delete mode 100644 src/main/java/com/marklogic/client/http/MultiPartResponsePage.java diff --git a/src/main/java/com/marklogic/client/http/BodyPart.java b/src/main/java/com/marklogic/client/http/BodyPart.java deleted file mode 100644 index f0ac380e0..000000000 --- a/src/main/java/com/marklogic/client/http/BodyPart.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright 2012-2014 MarkLogic Corporation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.marklogic.client.http; - -import java.util.Map; - -import com.marklogic.client.io.marker.AbstractReadHandle; - -public interface BodyPart { - //Map> getHeaders(); - /** - * If this part is JSON, could be any of the following types: - *
-     *   string
-     *   number
-     *   boolean
-     *   array
-     *   object
-     *   null
-     * 
- * - * Or if this part is XML, could be any of the following types: - *
-     *   xs:anySimpleType,
-     *   xs:base64Binary,
-     *   xs:boolean,
-     *   xs:byte,
-     *   xs:date,
-     *   xs:dateTime,
-     *   xs:dayTimeDuration,
-     *   xs:decimal,
-     *   xs:double,
-     *   xs:duration,
-     *   xs:float,
-     *   xs:int,
-     *   xs:integer,
-     *   xs:long,
-     *   xs:short,
-     *   xs:string,
-     *   xs:time,
-     *   xs:unsignedInt,
-     *   xs:unsignedLong,
-     *   xs:unsignedShort,
-     *   xs:yearMonthDuration.
-     * 
- */ - //public String getType(); <- what does XDBC return? - // maybe this instead? - // public String getMimeType(); - public H get(H handle); - public T getAs(Class clazz); -} diff --git a/src/main/java/com/marklogic/client/http/MultiPartResponsePage.java b/src/main/java/com/marklogic/client/http/MultiPartResponsePage.java deleted file mode 100644 index 7b34faa18..000000000 --- a/src/main/java/com/marklogic/client/http/MultiPartResponsePage.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright 2012-2014 MarkLogic Corporation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.marklogic.client.http; - -import com.marklogic.client.Page; - -public interface MultiPartResponsePage extends Page { -} From 3ee3db619ff2c9bb509604f113570457b1b00fd6 Mon Sep 17 00:00:00 2001 From: sammefford Date: Thu, 18 Sep 2014 10:23:41 -0600 Subject: [PATCH 127/357] fix #69, use 'property' not 'field' on pojos --- .../client/impl/PojoQueryBuilderImpl.java | 148 +++++++++--------- .../client/impl/PojoRepositoryImpl.java | 20 +-- .../client/pojo/PojoQueryBuilder.java | 55 +++---- .../marklogic/client/pojo/PojoRepository.java | 2 +- .../marklogic/client/test/PojoFacadeTest.java | 4 +- 5 files changed, 113 insertions(+), 116 deletions(-) diff --git a/src/main/java/com/marklogic/client/impl/PojoQueryBuilderImpl.java b/src/main/java/com/marklogic/client/impl/PojoQueryBuilderImpl.java index 78ca38b76..9fdd1f628 100644 --- a/src/main/java/com/marklogic/client/impl/PojoQueryBuilderImpl.java +++ b/src/main/java/com/marklogic/client/impl/PojoQueryBuilderImpl.java @@ -44,165 +44,165 @@ public PojoQueryBuilderImpl(Class clazz, boolean wrapQueries) { this.wrapQueries = wrapQueries; } - private StructuredQueryBuilder.PathIndex pojoFieldPath(String pojoField) { - //return pathIndex("*[local-name()=\"" + classWrapper + "\"]/" + pojoField); - return pathIndex(classWrapper + "/" + pojoField); + private StructuredQueryBuilder.PathIndex pojoPropertyPath(String pojoProperty) { + //return pathIndex("*[local-name()=\"" + classWrapper + "\"]/" + pojoProperty); + return pathIndex(classWrapper + "/" + pojoProperty); } - public StructuredQueryDefinition containerQuery(String pojoField, StructuredQueryDefinition query) { + public StructuredQueryDefinition containerQuery(String pojoProperty, StructuredQueryDefinition query) { if ( wrapQueries ) { return super.containerQuery(jsonProperty(classWrapper), - super.containerQuery(jsonProperty(pojoField), query)); + super.containerQuery(jsonProperty(pojoProperty), query)); } else { - return super.containerQuery(jsonProperty(pojoField), query); + return super.containerQuery(jsonProperty(pojoProperty), query); } } @Override public StructuredQueryDefinition containerQuery(StructuredQueryDefinition query) { return super.containerQuery(jsonProperty(classWrapper), query); } - public PojoQueryBuilder containerQuery(String pojoField) { - return new PojoQueryBuilderImpl(getType(pojoField), true); + public PojoQueryBuilder containerQuery(String pojoProperty) { + return new PojoQueryBuilderImpl(getType(pojoProperty), true); } @Override public StructuredQueryBuilder.GeospatialIndex - geoPair(String latitudeFieldName, String longitudeFieldName) + geoPair(String latitudePropertyName, String longitudePropertyName) { - return geoElementPair(jsonProperty(classWrapper), jsonProperty(latitudeFieldName), jsonProperty(longitudeFieldName)); + return geoElementPair(jsonProperty(classWrapper), jsonProperty(latitudePropertyName), jsonProperty(longitudePropertyName)); } - public StructuredQueryBuilder.GeospatialIndex geoField(String pojoField) { - return geoElement(jsonProperty(pojoField)); + public StructuredQueryBuilder.GeospatialIndex geoProperty(String pojoProperty) { + return geoElement(jsonProperty(pojoProperty)); } - public StructuredQueryBuilder.GeospatialIndex geoPath(String pojoField) { - return geoPath(pojoFieldPath(pojoField)); + public StructuredQueryBuilder.GeospatialIndex geoPath(String pojoProperty) { + return geoPath(pojoPropertyPath(pojoProperty)); } - public StructuredQueryDefinition range(String pojoField, + public StructuredQueryDefinition range(String pojoProperty, StructuredQueryBuilder.Operator operator, Object... values) { - return range(pojoFieldPath(pojoField), getRangeIndexType(pojoField), operator, values); + return range(pojoPropertyPath(pojoProperty), getRangeIndexType(pojoProperty), operator, values); } - public StructuredQueryDefinition range(String pojoField, String[] options, + public StructuredQueryDefinition range(String pojoProperty, String[] options, StructuredQueryBuilder.Operator operator, Object... values) { - return range(pojoFieldPath(pojoField), getRangeIndexType(pojoField), options, + return range(pojoPropertyPath(pojoProperty), getRangeIndexType(pojoProperty), options, operator, values); } - public StructuredQueryDefinition value(String pojoField, String... values) { + public StructuredQueryDefinition value(String pojoProperty, String... values) { if ( wrapQueries ) { return super.containerQuery(jsonProperty(classWrapper), - value(jsonProperty(pojoField), values)); + value(jsonProperty(pojoProperty), values)); } else { - return value(jsonProperty(pojoField), values); + return value(jsonProperty(pojoProperty), values); } } - public StructuredQueryDefinition value(String pojoField, Boolean value) { + public StructuredQueryDefinition value(String pojoProperty, Boolean value) { if ( wrapQueries ) { return super.containerQuery(jsonProperty(classWrapper), - value(jsonProperty(pojoField), value)); + value(jsonProperty(pojoProperty), value)); } else { - return value(jsonProperty(pojoField), value); + return value(jsonProperty(pojoProperty), value); } } - public StructuredQueryDefinition value(String pojoField, Number... values) { + public StructuredQueryDefinition value(String pojoProperty, Number... values) { if ( wrapQueries ) { return super.containerQuery(jsonProperty(classWrapper), - value(jsonProperty(pojoField), values)); + value(jsonProperty(pojoProperty), values)); } else { - return value(jsonProperty(pojoField), values); + return value(jsonProperty(pojoProperty), values); } } - public StructuredQueryDefinition value(String pojoField, String[] options, + public StructuredQueryDefinition value(String pojoProperty, String[] options, double weight, String... values) { if ( wrapQueries ) { return super.containerQuery(jsonProperty(classWrapper), - value(jsonProperty(pojoField), null, options, weight, values)); + value(jsonProperty(pojoProperty), null, options, weight, values)); } else { - return value(jsonProperty(pojoField), null, options, weight, values); + return value(jsonProperty(pojoProperty), null, options, weight, values); } } - public StructuredQueryDefinition value(String pojoField, String[] options, + public StructuredQueryDefinition value(String pojoProperty, String[] options, double weight, Boolean value) { if ( wrapQueries ) { return super.containerQuery(jsonProperty(classWrapper), - value(jsonProperty(pojoField), null, options, weight, value)); + value(jsonProperty(pojoProperty), null, options, weight, value)); } else { - return value(jsonProperty(pojoField), null, options, weight, value); + return value(jsonProperty(pojoProperty), null, options, weight, value); } } - public StructuredQueryDefinition value(String pojoField, String[] options, + public StructuredQueryDefinition value(String pojoProperty, String[] options, double weight, Number... values) { if ( wrapQueries ) { return super.containerQuery(jsonProperty(classWrapper), - value(jsonProperty(pojoField), null, options, weight, values)); + value(jsonProperty(pojoProperty), null, options, weight, values)); } else { - return value(jsonProperty(pojoField), null, options, weight, values); + return value(jsonProperty(pojoProperty), null, options, weight, values); } } - public StructuredQueryDefinition word(String pojoField, String... words) { + public StructuredQueryDefinition word(String pojoProperty, String... words) { if ( wrapQueries ) { return super.containerQuery(jsonProperty(classWrapper), - super.word(jsonProperty(pojoField), words)); + super.word(jsonProperty(pojoProperty), words)); } else { - return super.word(jsonProperty(pojoField), words); + return super.word(jsonProperty(pojoProperty), words); } } - public StructuredQueryDefinition word(String pojoField, String[] options, + public StructuredQueryDefinition word(String pojoProperty, String[] options, double weight, String... words) { if ( wrapQueries ) { return super.containerQuery(jsonProperty(classWrapper), - super.word(jsonProperty(pojoField), null, options, weight, words)); + super.word(jsonProperty(pojoProperty), null, options, weight, words)); } else { - return super.word(jsonProperty(pojoField), null, options, weight, words); + return super.word(jsonProperty(pojoProperty), null, options, weight, words); } } - public String getRangeIndexType(String fieldName) { + public String getRangeIndexType(String propertyName) { // map java types to acceptable Range Index types - String type = rangeIndextypes.get(fieldName); + String type = rangeIndextypes.get(propertyName); if ( type == null ) { - Class fieldClass = getType(fieldName); - if ( String.class.isAssignableFrom(fieldClass) ) { + Class propertyClass = getType(propertyName); + if ( String.class.isAssignableFrom(propertyClass) ) { type = "xs:string"; - } else if ( Integer.TYPE.equals(fieldClass) ) { + } else if ( Integer.TYPE.equals(propertyClass) ) { type = "xs:int"; - } else if ( Long.TYPE.equals(fieldClass) ) { + } else if ( Long.TYPE.equals(propertyClass) ) { type = "xs:long"; - } else if ( Float.TYPE.equals(fieldClass) ) { + } else if ( Float.TYPE.equals(propertyClass) ) { type = "xs:float"; - } else if ( Double.TYPE.equals(fieldClass) ) { + } else if ( Double.TYPE.equals(propertyClass) ) { type = "xs:double"; - } else if ( Number.class.isAssignableFrom(fieldClass) ) { + } else if ( Number.class.isAssignableFrom(propertyClass) ) { type = "xs:decimal"; - } else if ( Date.class.isAssignableFrom(fieldClass) ) { + } else if ( Date.class.isAssignableFrom(propertyClass) ) { type = "xs:dateTime"; } if ( type == null ) { - throw new IllegalArgumentException("Field " + fieldName + " is not a native Java type"); + throw new IllegalArgumentException("Property " + propertyName + " is not a native Java type"); } - rangeIndextypes.put(fieldName, type); + rangeIndextypes.put(propertyName, type); } return type; } - public Class getType(String fieldName) { - Class fieldClass = types.get(fieldName); - if ( fieldClass == null ) { - // figure out the type of the java field - String initCapPojoField = fieldName.substring(0,1).toUpperCase() + - fieldName.substring(1); + public Class getType(String propertyName) { + Class propertyClass = types.get(propertyName); + if ( propertyClass == null ) { + // figure out the type of the java property + String initCapPojoProperty = propertyName.substring(0,1).toUpperCase() + + propertyName.substring(1); try { - fieldClass = clazz.getField(fieldName).getType(); + propertyClass = clazz.getField(propertyName).getType(); } catch(NoSuchFieldException e) { Method getMethod = null; try { - getMethod = clazz.getMethod("get" + initCapPojoField); + getMethod = clazz.getMethod("get" + initCapPojoProperty); } catch (NoSuchMethodException e2) { try { - getMethod = clazz.getMethod("is" + initCapPojoField); + getMethod = clazz.getMethod("is" + initCapPojoProperty); if ( ! Boolean.class.isAssignableFrom(getMethod.getReturnType()) ) { getMethod = null; } @@ -210,33 +210,33 @@ public Class getType(String fieldName) { } if ( getMethod != null ) { if ( Modifier.isStatic(getMethod.getModifiers()) ) { - throw new IllegalArgumentException("get" + initCapPojoField + + throw new IllegalArgumentException("get" + initCapPojoProperty + " cannot be static"); } - fieldClass = getMethod.getReturnType(); - if ( fieldClass == Void.TYPE ) { - throw new IllegalArgumentException("get" + initCapPojoField + + propertyClass = getMethod.getReturnType(); + if ( propertyClass == Void.TYPE ) { + throw new IllegalArgumentException("get" + initCapPojoProperty + " must not have return type void"); } } else { - String setMethodName = "set" + initCapPojoField; + String setMethodName = "set" + initCapPojoProperty; for ( Method method : clazz.getMethods() ) { if ( setMethodName.equals(method.getName()) ) { Class[] parameters = method.getParameterTypes(); if ( parameters != null && parameters.length == 1 ) { - fieldClass = parameters[0]; + propertyClass = parameters[0]; break; } } } } } - if ( fieldClass == null ) { - throw new IllegalArgumentException("field " + fieldName + " not found, get" + initCapPojoField + - " not found, and set" + initCapPojoField + " not found in class " + classWrapper); + if ( propertyClass == null ) { + throw new IllegalArgumentException("property " + propertyName + " not found, get" + initCapPojoProperty + + " not found, and set" + initCapPojoProperty + " not found in class " + classWrapper); } - types.put(fieldName, fieldClass); + types.put(propertyName, propertyClass); } - return fieldClass; + return propertyClass; } } diff --git a/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java b/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java index 0f5b8604e..275a8b2ca 100644 --- a/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java +++ b/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java @@ -64,8 +64,8 @@ public class PojoRepositoryImpl private JSONDocumentManager docMgr; private PojoQueryBuilder qb; private Method idMethod; - private Field idField; - private String idFieldName; + private Field idProperty; + private String idPropertyName; PojoRepositoryImpl(DatabaseClient client, Class entityClass) { this.client = client; @@ -80,7 +80,7 @@ public class PojoRepositoryImpl this(client, entityClass); this.idClass = idClass; findId(); - if ( idMethod == null && idField == null ) { + if ( idMethod == null && idProperty == null ) { throw new IllegalArgumentException("Your class " + entityClass.getName() + " does not have a method or field annotated with com.marklogic.client.pojo.annotation.Id"); } @@ -250,7 +250,7 @@ public void setSearchView(QueryView view) { docMgr.setSearchView(view); } - public void defineIdField(String fieldName) { + public void defineIdProperty(String propertyName) { } public DatabaseClient getDatabaseClient() { @@ -285,7 +285,7 @@ private String createUri(ID id) { } private void findId() { - if ( idMethod == null && idField == null ) { + if ( idMethod == null && idProperty == null ) { for ( Method method : entityClass.getDeclaredMethods() ) { if ( method.isAnnotationPresent(Id.class) ) { Class[] parameters = method.getParameterTypes(); @@ -298,7 +298,7 @@ private void findId() { Pattern pattern = Pattern.compile("^(get|is)(.)(.*)"); Matcher matcher = pattern.matcher(method.getName()); if ( matcher.matches() ) { - idFieldName = matcher.group(2).toLowerCase() + matcher.group(3); + idPropertyName = matcher.group(2).toLowerCase() + matcher.group(3); idMethod = method; break; } else { @@ -321,7 +321,7 @@ private void findId() { ", annotated with com.marklogic.client.pojo.annotation.Id " + " must be public"); } - idField = field; + idProperty = field; break; } } @@ -338,12 +338,12 @@ private ID getId(T entity) { throw new IllegalStateException("Error invoking " + entityClass.getName() + " method " + idMethod.getName(), e); } - } else if ( idField != null ) { + } else if ( idProperty != null ) { try { - return (ID) idField.get(entity); + return (ID) idProperty.get(entity); } catch (Exception e) { throw new IllegalStateException("Error retrieving " + entityClass.getName() + " field " + - idField.getName(), e); + idProperty.getName(), e); } } else { throw new IllegalArgumentException("Your class " + entityClass.getName() + diff --git a/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java b/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java index 25fbffe56..53ecd5a0e 100644 --- a/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java +++ b/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java @@ -28,10 +28,10 @@ * StructuredQueryBuilder while enabling queries across objects persisted using * {@link PojoRepository}. * - *

For methods which accept a "pojoField" argument and for {@link #geoField geoField}, we are refering to - * fields (or properties) appropriate for + *

For methods which accept a "pojoProperty" argument and for {@link #geoProperty geoProperty}, we are refering to + * properties appropriate for * JavaBeans, - * including fields accessible via public getters and setters, or public fields.

+ * including properties accessible via public getters and setters, or public fields.

* * *

Where StructuredQueryBuilder accepts StructuredQueryBuilder.TextIndex as a first argument @@ -41,7 +41,7 @@ * {@link #word(StructuredQueryBuilder.TextIndex, String...) word(TextIndex, String...)} * methods, * PojoQueryBuilder adds shortcut methods which accept as the first argument a String name of the - * pojoField. Similarly, PojoQueryBuilder accepts String pojoField arguments wherever + * pojoProperty. Similarly, PojoQueryBuilder accepts String pojoProperty arguments wherever * StructuredQueryBuilder accepts StructuredQueryBuilder.Element, * StructuredQueryBuilder.Attribute, and StructuredQueryBuilder.PathIndex * as arguments to @@ -71,14 +71,14 @@ * *

Similarly, without the pojo facade you might persist your pojos using * {@link com.marklogic.client.io.JAXBHandle JAXBHandle} and if they - * have a geoPosition field which is an object with latitude and longitude pojoFields + * have a geoPosition property which is an object with latitude and longitude pojoProperty's * (which persist as elements) you might query them thusly:

*
{@code    StructuredQueryBuilder sqb = new StructuredQueryBuilder();
  *    StructuredQueryBuilder.GeospatialIndex geoIdx = sqb.geoElementPair(
  *      sqb.element("geoPosition"), sqb.element("latitude"), sqb.element("longitude"));}
* *

But if you use {@link PojoRepository} to persist your pojos with a latitude and longitude - * pojoFields, you can query them more simply:

+ * pojoProperty's, you can query them more simply:

*
{@code    PojoQueryBuilder pqb = pojoRepository.getQueryBuilder();
  *    StructuredQueryBuilder.GeospatialIndex geoIdx = 
  *      pqb.geoPair("latitude", "longitude");}
@@ -99,8 +99,8 @@ * void setContinent(); * }
* - *

That is, you have a pojo class City with a field "country" of type - * Country, you could query fields on the nested country thusly:

+ *

That is, you have a pojo class City with a property "country" of type + * Country, you could query properties on the nested country thusly:

*
{@code    PojoRepository cities = 
  *      databaseClient.newPojoRepository(City.class, Integer.class);
  *    PojoQueryBuilder citiesQb = cities.getQueryBuilder();
@@ -109,9 +109,9 @@
  */
 public interface PojoQueryBuilder {
 
-    /** @return a query matching pojos of type T containing the pojoField with contents
+    /** @return a query matching pojos of type T containing the pojoProperty with contents
      *          or children matching the specified query */
-    public StructuredQueryDefinition containerQuery(String pojoField,
+    public StructuredQueryDefinition containerQuery(String pojoProperty,
         StructuredQueryDefinition query);
 
     /** @return a query matching pojos of type T with children matching the specified query */
@@ -123,35 +123,32 @@ public StructuredQueryDefinition containerQuery(String pojoField,
      * provides you a query builder that is specific to that child object.  To query further levels of 
      * nested objects you may use this method on the each returned PojoQueryBuilder which represents
      * one level deeper.
-     * @return a PojoQueryBuilder for nested pojos of the type corresponding with pojoField */
-    public PojoQueryBuilder          containerQuery(String pojoField);
+     * @return a PojoQueryBuilder for nested pojos of the type corresponding with pojoProperty */
+    public PojoQueryBuilder          containerQuery(String pojoProperty);
     public StructuredQueryBuilder.GeospatialIndex
-        geoPair(String latitudeFieldName, String longitudeFieldName);
+        geoPair(String latitudePropertyName, String longitudePropertyName);
     /**
-     * NOTE: Since the pojo facade abstracts away the persistence details, "field" here refers 
-     * to a pojoField like all other convenience methods in PojoQueryBuilder,not
-     * a MarkLogic field specified on the server.
-     * @param pojoField the name of a field (or getter or setter) on class T
+     * @param pojoProperty the name of a field or JavaBean property (accessed via getter or setter) on class T
      */
     public StructuredQueryBuilder.GeospatialIndex
-        geoField(String pojoField);
+        geoProperty(String pojoProperty);
     public StructuredQueryBuilder.GeospatialIndex
-        geoPath(String pojoField);
-    public StructuredQueryDefinition range(String pojoField,
+        geoPath(String pojoProperty);
+    public StructuredQueryDefinition range(String pojoProperty,
         StructuredQueryBuilder.Operator operator, Object... values);
-    public StructuredQueryDefinition range(String pojoField, String[] options,
+    public StructuredQueryDefinition range(String pojoProperty, String[] options,
         StructuredQueryBuilder.Operator operator, Object... values);
-    public StructuredQueryDefinition value(String pojoField, String... values);
-    public StructuredQueryDefinition value(String pojoField, Boolean value);
-    public StructuredQueryDefinition value(String pojoField, Number... values);
-    public StructuredQueryDefinition value(String pojoField, String[] options,
+    public StructuredQueryDefinition value(String pojoProperty, String... values);
+    public StructuredQueryDefinition value(String pojoProperty, Boolean value);
+    public StructuredQueryDefinition value(String pojoProperty, Number... values);
+    public StructuredQueryDefinition value(String pojoProperty, String[] options,
         double weight, String... values);
-    public StructuredQueryDefinition value(String pojoField, String[] options,
+    public StructuredQueryDefinition value(String pojoProperty, String[] options,
         double weight, Boolean value);
-    public StructuredQueryDefinition value(String pojoField, String[] options,
+    public StructuredQueryDefinition value(String pojoProperty, String[] options,
         double weight, Number... values);
-    public StructuredQueryDefinition word(String pojoField, String... words);
-    public StructuredQueryDefinition word(String pojoField, String[] options,
+    public StructuredQueryDefinition word(String pojoProperty, String... words);
+    public StructuredQueryDefinition word(String pojoProperty, String[] options,
         double weight, String... words);
 
     // All following method signatures copied from StructuredQueryBuilder since it has no interface we can extend
diff --git a/src/main/java/com/marklogic/client/pojo/PojoRepository.java b/src/main/java/com/marklogic/client/pojo/PojoRepository.java
index e5f82f37e..477911e84 100644
--- a/src/main/java/com/marklogic/client/pojo/PojoRepository.java
+++ b/src/main/java/com/marklogic/client/pojo/PojoRepository.java
@@ -71,7 +71,7 @@ public interface PojoRepository {
     public long getPageLength(); // default: 50
     public void setPageLength(long length);
     
-    public void defineIdField(String fieldName);
+    public void defineIdProperty(String fieldName);
  
     public DatabaseClient getDatabaseClient();
 }
diff --git a/src/test/java/com/marklogic/client/test/PojoFacadeTest.java b/src/test/java/com/marklogic/client/test/PojoFacadeTest.java
index 49042f3be..d2d4b98f2 100644
--- a/src/test/java/com/marklogic/client/test/PojoFacadeTest.java
+++ b/src/test/java/com/marklogic/client/test/PojoFacadeTest.java
@@ -194,7 +194,7 @@ public void testC_QueryPojos() throws Exception {
         // TODO: uncomment tests below once geospatial on JSON is implemented in server
         /*
         query = qb.geospatial(
-            qb.geoField("latLong"),
+            qb.geoProperty("latLong"),
             qb.circle(-34, -58, 1)
         );
         page = cities.search(query, 1);
@@ -292,7 +292,7 @@ public void testD_PojosWithChildren() throws Exception {
         query = qb.containerQuery(qb.term("SA"));
         assertEquals("Should find two cities", 61, cities.search(query, 1).getTotalSize());
 
-        // all countries containing the field "currencyName" with the term "peso"
+        // all countries containing the property "currencyName" with the term "peso"
         query = countriesQb.word("currencyName", "peso");
         assertEquals("Should find one city", 1, cities.search(query, 1).getTotalSize());
     }

From 8db955596f2a5147cf9d1c62f28d405a5d89d189 Mon Sep 17 00:00:00 2001
From: sammefford 
Date: Thu, 18 Sep 2014 10:27:08 -0600
Subject: [PATCH 128/357] fix part of #73, change containerQuery(String
 pojoProperty) to containerQueryBuilder(String pojoProperty)

---
 .../java/com/marklogic/client/impl/PojoQueryBuilderImpl.java    | 2 +-
 src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java   | 2 +-
 src/test/java/com/marklogic/client/test/PojoFacadeTest.java     | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/main/java/com/marklogic/client/impl/PojoQueryBuilderImpl.java b/src/main/java/com/marklogic/client/impl/PojoQueryBuilderImpl.java
index 9fdd1f628..11f020fad 100644
--- a/src/main/java/com/marklogic/client/impl/PojoQueryBuilderImpl.java
+++ b/src/main/java/com/marklogic/client/impl/PojoQueryBuilderImpl.java
@@ -61,7 +61,7 @@ public StructuredQueryDefinition containerQuery(String pojoProperty, StructuredQ
     public StructuredQueryDefinition containerQuery(StructuredQueryDefinition query) {
         return super.containerQuery(jsonProperty(classWrapper), query);
     }
-    public PojoQueryBuilder          containerQuery(String pojoProperty) {
+    public PojoQueryBuilder          containerQueryBuilder(String pojoProperty) {
         return new PojoQueryBuilderImpl(getType(pojoProperty), true);
     }
     @Override
diff --git a/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java b/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java
index 53ecd5a0e..a52a66656 100644
--- a/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java
+++ b/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java
@@ -124,7 +124,7 @@ public StructuredQueryDefinition containerQuery(String pojoProperty,
      * nested objects you may use this method on the each returned PojoQueryBuilder which represents
      * one level deeper.
      * @return a PojoQueryBuilder for nested pojos of the type corresponding with pojoProperty */
-    public PojoQueryBuilder          containerQuery(String pojoProperty);
+    public PojoQueryBuilder          containerQueryBuilder(String pojoProperty);
     public StructuredQueryBuilder.GeospatialIndex
         geoPair(String latitudePropertyName, String longitudePropertyName);
     /**
diff --git a/src/test/java/com/marklogic/client/test/PojoFacadeTest.java b/src/test/java/com/marklogic/client/test/PojoFacadeTest.java
index d2d4b98f2..5eba8f385 100644
--- a/src/test/java/com/marklogic/client/test/PojoFacadeTest.java
+++ b/src/test/java/com/marklogic/client/test/PojoFacadeTest.java
@@ -274,7 +274,7 @@ public void testD_PojosWithChildren() throws Exception {
         cities.write(buenosAires);
 
         PojoQueryBuilder qb = cities.getQueryBuilder();
-        PojoQueryBuilder countriesQb = qb.containerQuery("country");
+        PojoQueryBuilder countriesQb = qb.containerQueryBuilder("country");
         QueryDefinition query = countriesQb.value("continent", "EU");
         assertEquals("Should not find any countries", 0, cities.search(query, 1).getTotalSize());
 

From 4bc9a124275f0af3773151154b140b14fd51a5df Mon Sep 17 00:00:00 2001
From: sammefford 
Date: Thu, 18 Sep 2014 10:56:58 -0600
Subject: [PATCH 129/357] fix another piece of #73, add an interface to
 StruturedQueryBuilder and have PojoQueryBuilder extend that for better
 maintainability

---
 .../client/pojo/PojoQueryBuilder.java         | 67 +------------------
 .../client/query/StructuredQueryBuilder.java  |  2 +-
 2 files changed, 3 insertions(+), 66 deletions(-)

diff --git a/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java b/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java
index a52a66656..7833b6fe6 100644
--- a/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java
+++ b/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java
@@ -17,6 +17,7 @@
 
 import javax.xml.namespace.QName;
 
+import com.marklogic.client.query.CombinedQueryBuilder;
 import com.marklogic.client.query.RawStructuredQueryDefinition;
 import com.marklogic.client.query.StructuredQueryBuilder;
 import com.marklogic.client.query.StructuredQueryDefinition;
@@ -107,7 +108,7 @@
  *    PojoQueryBuilder countriesQb = citiesQb.containerQuery("country");
  *    QueryDefinition query = countriesQb.value("continent", "EU"); }
*/ -public interface PojoQueryBuilder { +public interface PojoQueryBuilder extends CombinedQueryBuilder { /** @return a query matching pojos of type T containing the pojoProperty with contents * or children matching the specified query */ @@ -150,69 +151,5 @@ public StructuredQueryDefinition value(String pojoProperty, String[] options, public StructuredQueryDefinition word(String pojoProperty, String... words); public StructuredQueryDefinition word(String pojoProperty, String[] options, double weight, String... words); - - // All following method signatures copied from StructuredQueryBuilder since it has no interface we can extend - public StructuredQueryDefinition and(StructuredQueryDefinition... queries); - public StructuredQueryDefinition andNot(StructuredQueryDefinition positive, StructuredQueryDefinition negative); - public StructuredQueryBuilder.Attribute attribute(QName qname); - public StructuredQueryBuilder.Attribute attribute(String name); - public StructuredQueryDefinition boost(StructuredQueryDefinition matchingQuery, StructuredQueryDefinition boostingQuery); - public StructuredQueryBuilder.Region box(double south, double west, double north, double east); - public RawStructuredQueryDefinition build(StructuredQueryDefinition... queries); - public StructuredQueryBuilder.Region circle(double latitude, double longitude, double radius); - public StructuredQueryBuilder.Region circle(StructuredQueryBuilder.Point center, double radius); - public StructuredQueryDefinition collection(String... uris); - public StructuredQueryDefinition collectionConstraint(String constraintName, String... uris); - public StructuredQueryDefinition containerConstraint(String constraintName, StructuredQueryDefinition query); - public StructuredQueryDefinition containerQuery(StructuredQueryBuilder.ContainerIndex index, StructuredQueryDefinition query); - public StructuredQueryDefinition customConstraint(String constraintName, String... text); - public StructuredQueryDefinition directory(boolean isInfinite, String... uris); - public StructuredQueryDefinition directory(int depth, String... uris); - public StructuredQueryDefinition document(String... uris); - public StructuredQueryDefinition documentFragment(StructuredQueryDefinition query); - public StructuredQueryBuilder.Element element(QName qname); - public StructuredQueryBuilder.Element element(String name); - public StructuredQueryBuilder.ElementAttribute elementAttribute(StructuredQueryBuilder.Element element, StructuredQueryBuilder.Attribute attribute); - public StructuredQueryDefinition elementConstraint(String constraintName, StructuredQueryDefinition query); - public StructuredQueryBuilder.Field field(String name); - public StructuredQueryBuilder.GeospatialIndex geoAttributePair(StructuredQueryBuilder.Element parent, StructuredQueryBuilder.Attribute lat, StructuredQueryBuilder.Attribute lon); - public StructuredQueryBuilder.GeospatialIndex geoElement(StructuredQueryBuilder.Element element); - public StructuredQueryBuilder.GeospatialIndex geoElement(StructuredQueryBuilder.Element parent, StructuredQueryBuilder.Element element); - public StructuredQueryBuilder.GeospatialIndex geoElementPair(StructuredQueryBuilder.Element parent, StructuredQueryBuilder.Element lat, StructuredQueryBuilder.Element lon); - public StructuredQueryBuilder.GeospatialIndex geoPath(StructuredQueryBuilder.PathIndex pathIndex); - public StructuredQueryDefinition geospatial(StructuredQueryBuilder.GeospatialIndex index, StructuredQueryBuilder.FragmentScope scope, String[] options, StructuredQueryBuilder.Region... regions); - public StructuredQueryDefinition geospatial(StructuredQueryBuilder.GeospatialIndex index, StructuredQueryBuilder.Region... regions); - public StructuredQueryDefinition geospatialConstraint(String constraintName, StructuredQueryBuilder.Region... regions); - public IterableNamespaceContext getNamespaces(); - public StructuredQueryDefinition locks(StructuredQueryDefinition query); - public StructuredQueryDefinition near(int distance, double weight, StructuredQueryBuilder.Ordering order, StructuredQueryDefinition... queries); - public StructuredQueryDefinition near(StructuredQueryDefinition... queries); - public StructuredQueryDefinition not(StructuredQueryDefinition query); - public StructuredQueryDefinition notIn(StructuredQueryDefinition positive, StructuredQueryDefinition negative); - public StructuredQueryDefinition or(StructuredQueryDefinition... queries); - public StructuredQueryBuilder.PathIndex pathIndex(String path); - public StructuredQueryBuilder.Region point(double latitude, double longitude); - public StructuredQueryBuilder.Region polygon(StructuredQueryBuilder.Point... points); - public StructuredQueryDefinition properties(StructuredQueryDefinition query); - public StructuredQueryDefinition propertiesConstraint(String constraintName, StructuredQueryDefinition query); - public StructuredQueryDefinition range(StructuredQueryBuilder.RangeIndex index, String type, String[] options, StructuredQueryBuilder.Operator operator, Object... values); - public StructuredQueryDefinition range(StructuredQueryBuilder.RangeIndex index, String type, String collation, String[] options, StructuredQueryBuilder.Operator operator, Object... values); - public StructuredQueryDefinition range(StructuredQueryBuilder.RangeIndex index, String type, String collation, StructuredQueryBuilder.FragmentScope scope, String[] options, StructuredQueryBuilder.Operator operator, Object... values); - public StructuredQueryDefinition range(StructuredQueryBuilder.RangeIndex index, String type, String collation, StructuredQueryBuilder.FragmentScope scope, StructuredQueryBuilder.Operator operator, Object... values); - public StructuredQueryDefinition range(StructuredQueryBuilder.RangeIndex index, String type, String collation, StructuredQueryBuilder.Operator operator, Object... values); - public StructuredQueryDefinition range(StructuredQueryBuilder.RangeIndex index, String type, StructuredQueryBuilder.Operator operator, Object... values); - public StructuredQueryDefinition rangeConstraint(String constraintName, StructuredQueryBuilder.Operator operator, String... values); - public String[] rangeOptions(String... options); - public void setNamespaces(IterableNamespaceContext namespaces); - public StructuredQueryDefinition term(double weight, String... terms); - public StructuredQueryDefinition term(String... terms); - public StructuredQueryDefinition value(StructuredQueryBuilder.TextIndex index, String... values); - public StructuredQueryDefinition value(StructuredQueryBuilder.TextIndex index, StructuredQueryBuilder.FragmentScope scope, String[] options, double weight, String... values); - public StructuredQueryDefinition valueConstraint(String constraintName, double weight, String... values); - public StructuredQueryDefinition valueConstraint(String constraintName, String... values); - public StructuredQueryDefinition word(StructuredQueryBuilder.TextIndex index, String... words); - public StructuredQueryDefinition word(StructuredQueryBuilder.TextIndex index, StructuredQueryBuilder.FragmentScope scope, String[] options, double weight, String... words); - public StructuredQueryDefinition wordConstraint(String constraintName, double weight, String... words); - public StructuredQueryDefinition wordConstraint(String constraintName, String... words); } diff --git a/src/main/java/com/marklogic/client/query/StructuredQueryBuilder.java b/src/main/java/com/marklogic/client/query/StructuredQueryBuilder.java index 2a839d6f4..8638f4691 100644 --- a/src/main/java/com/marklogic/client/query/StructuredQueryBuilder.java +++ b/src/main/java/com/marklogic/client/query/StructuredQueryBuilder.java @@ -57,7 +57,7 @@ * in a variable: *
StructuredQueryDefinition andQry = structuredQueryBuilder.and(... query definitions ...);
*/ -public class StructuredQueryBuilder { +public class StructuredQueryBuilder implements CombinedQueryBuilder { final static private String SEARCH_API_NS="http://marklogic.com/appservices/search"; /* From 56d8edd064b991e4c77268d914edffc8f1ee2abc Mon Sep 17 00:00:00 2001 From: sammefford Date: Thu, 18 Sep 2014 11:11:16 -0600 Subject: [PATCH 130/357] fix another piece of #73, address TODO on line 207 (actually line 218 now) --- .../java/com/marklogic/client/impl/PojoRepositoryImpl.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java b/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java index 275a8b2ca..eefeea023 100644 --- a/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java +++ b/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java @@ -218,10 +218,6 @@ public PojoPage search(QueryDefinition query, long start, SearchReadHandle se responseHandle.setQueryCriteria(query); } docMgr.setResponseFormat(searchBase.getFormat()); - } else { - // TODO: remove this once REST API only considers Content-type header - // (not format parameter) for expceted payload format - docMgr.setResponseFormat(Format.XML); } String tid = transaction == null ? null : transaction.getTransactionId(); From 3d8e179f22c9c5b5ecdce33430cd174389aac761 Mon Sep 17 00:00:00 2001 From: sammefford Date: Thu, 18 Sep 2014 11:55:50 -0600 Subject: [PATCH 131/357] fix another piece of #73, add an interface to StruturedQueryBuilder and have PojoQueryBuilder extend that for better maintainability --- .../client/query/CombinedQueryBuilder.java | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/main/java/com/marklogic/client/query/CombinedQueryBuilder.java diff --git a/src/main/java/com/marklogic/client/query/CombinedQueryBuilder.java b/src/main/java/com/marklogic/client/query/CombinedQueryBuilder.java new file mode 100644 index 000000000..b1d3f9129 --- /dev/null +++ b/src/main/java/com/marklogic/client/query/CombinedQueryBuilder.java @@ -0,0 +1,89 @@ +/* + * Copyright 2012-2014 MarkLogic Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.marklogic.client.query; + +import javax.xml.namespace.QName; + +import com.marklogic.client.query.StructuredQueryBuilder; +import com.marklogic.client.query.StructuredQueryDefinition; +import com.marklogic.client.util.IterableNamespaceContext; + +public interface CombinedQueryBuilder { + public StructuredQueryDefinition and(StructuredQueryDefinition... queries); + public StructuredQueryDefinition andNot(StructuredQueryDefinition positive, StructuredQueryDefinition negative); + public StructuredQueryBuilder.Attribute attribute(QName qname); + public StructuredQueryBuilder.Attribute attribute(String name); + public StructuredQueryDefinition boost(StructuredQueryDefinition matchingQuery, StructuredQueryDefinition boostingQuery); + public StructuredQueryBuilder.Region box(double south, double west, double north, double east); + public RawStructuredQueryDefinition build(StructuredQueryDefinition... queries); + public StructuredQueryBuilder.Region circle(double latitude, double longitude, double radius); + public StructuredQueryBuilder.Region circle(StructuredQueryBuilder.Point center, double radius); + public StructuredQueryDefinition collection(String... uris); + public StructuredQueryDefinition collectionConstraint(String constraintName, String... uris); + public StructuredQueryDefinition containerConstraint(String constraintName, StructuredQueryDefinition query); + public StructuredQueryDefinition containerQuery(StructuredQueryBuilder.ContainerIndex index, StructuredQueryDefinition query); + public StructuredQueryDefinition customConstraint(String constraintName, String... text); + public StructuredQueryDefinition directory(boolean isInfinite, String... uris); + public StructuredQueryDefinition directory(int depth, String... uris); + public StructuredQueryDefinition document(String... uris); + public StructuredQueryDefinition documentFragment(StructuredQueryDefinition query); + public StructuredQueryBuilder.Element element(QName qname); + public StructuredQueryBuilder.Element element(String name); + public StructuredQueryBuilder.ElementAttribute elementAttribute(StructuredQueryBuilder.Element element, StructuredQueryBuilder.Attribute attribute); + public StructuredQueryDefinition elementConstraint(String constraintName, StructuredQueryDefinition query); + public StructuredQueryBuilder.Field field(String name); + public StructuredQueryBuilder.GeospatialIndex geoAttributePair(StructuredQueryBuilder.Element parent, StructuredQueryBuilder.Attribute lat, StructuredQueryBuilder.Attribute lon); + public StructuredQueryBuilder.GeospatialIndex geoElement(StructuredQueryBuilder.Element element); + public StructuredQueryBuilder.GeospatialIndex geoElement(StructuredQueryBuilder.Element parent, StructuredQueryBuilder.Element element); + public StructuredQueryBuilder.GeospatialIndex geoElementPair(StructuredQueryBuilder.Element parent, StructuredQueryBuilder.Element lat, StructuredQueryBuilder.Element lon); + public StructuredQueryBuilder.GeospatialIndex geoPath(StructuredQueryBuilder.PathIndex pathIndex); + public StructuredQueryDefinition geospatial(StructuredQueryBuilder.GeospatialIndex index, StructuredQueryBuilder.FragmentScope scope, String[] options, StructuredQueryBuilder.Region... regions); + public StructuredQueryDefinition geospatial(StructuredQueryBuilder.GeospatialIndex index, StructuredQueryBuilder.Region... regions); + public StructuredQueryDefinition geospatialConstraint(String constraintName, StructuredQueryBuilder.Region... regions); + public IterableNamespaceContext getNamespaces(); + public StructuredQueryDefinition locks(StructuredQueryDefinition query); + public StructuredQueryDefinition near(int distance, double weight, StructuredQueryBuilder.Ordering order, StructuredQueryDefinition... queries); + public StructuredQueryDefinition near(StructuredQueryDefinition... queries); + public StructuredQueryDefinition not(StructuredQueryDefinition query); + public StructuredQueryDefinition notIn(StructuredQueryDefinition positive, StructuredQueryDefinition negative); + public StructuredQueryDefinition or(StructuredQueryDefinition... queries); + public StructuredQueryBuilder.PathIndex pathIndex(String path); + public StructuredQueryBuilder.Region point(double latitude, double longitude); + public StructuredQueryBuilder.Region polygon(StructuredQueryBuilder.Point... points); + public StructuredQueryDefinition properties(StructuredQueryDefinition query); + public StructuredQueryDefinition propertiesConstraint(String constraintName, StructuredQueryDefinition query); + public StructuredQueryDefinition range(StructuredQueryBuilder.RangeIndex index, String type, String[] options, StructuredQueryBuilder.Operator operator, Object... values); + public StructuredQueryDefinition range(StructuredQueryBuilder.RangeIndex index, String type, String collation, String[] options, StructuredQueryBuilder.Operator operator, Object... values); + public StructuredQueryDefinition range(StructuredQueryBuilder.RangeIndex index, String type, String collation, StructuredQueryBuilder.FragmentScope scope, String[] options, StructuredQueryBuilder.Operator operator, Object... values); + public StructuredQueryDefinition range(StructuredQueryBuilder.RangeIndex index, String type, String collation, StructuredQueryBuilder.FragmentScope scope, StructuredQueryBuilder.Operator operator, Object... values); + public StructuredQueryDefinition range(StructuredQueryBuilder.RangeIndex index, String type, String collation, StructuredQueryBuilder.Operator operator, Object... values); + public StructuredQueryDefinition range(StructuredQueryBuilder.RangeIndex index, String type, StructuredQueryBuilder.Operator operator, Object... values); + public StructuredQueryDefinition rangeConstraint(String constraintName, StructuredQueryBuilder.Operator operator, String... values); + public String[] rangeOptions(String... options); + public void setNamespaces(IterableNamespaceContext namespaces); + public StructuredQueryDefinition term(double weight, String... terms); + public StructuredQueryDefinition term(String... terms); + public StructuredQueryDefinition value(StructuredQueryBuilder.TextIndex index, String... values); + public StructuredQueryDefinition value(StructuredQueryBuilder.TextIndex index, StructuredQueryBuilder.FragmentScope scope, String[] options, double weight, String... values); + public StructuredQueryDefinition valueConstraint(String constraintName, double weight, String... values); + public StructuredQueryDefinition valueConstraint(String constraintName, String... values); + public StructuredQueryDefinition word(StructuredQueryBuilder.TextIndex index, String... words); + public StructuredQueryDefinition word(StructuredQueryBuilder.TextIndex index, StructuredQueryBuilder.FragmentScope scope, String[] options, double weight, String... words); + public StructuredQueryDefinition wordConstraint(String constraintName, double weight, String... words); + public StructuredQueryDefinition wordConstraint(String constraintName, String... words); +} + + From 25abcfa3e40bff4aec260e99483f48e05673e8f3 Mon Sep 17 00:00:00 2001 From: sammefford Date: Thu, 18 Sep 2014 11:57:25 -0600 Subject: [PATCH 132/357] fix another piece of #73, demonstrate use of JsonSerialize for numeric fields we want searchable --- .../marklogic/client/test/PojoFacadeTest.java | 65 ++++++++++++++++++- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/marklogic/client/test/PojoFacadeTest.java b/src/test/java/com/marklogic/client/test/PojoFacadeTest.java index 5eba8f385..513b6bcd6 100644 --- a/src/test/java/com/marklogic/client/test/PojoFacadeTest.java +++ b/src/test/java/com/marklogic/client/test/PojoFacadeTest.java @@ -28,12 +28,15 @@ import org.junit.BeforeClass; import org.junit.Test; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import com.marklogic.client.pojo.PojoPage; import com.marklogic.client.pojo.PojoRepository; import com.marklogic.client.query.QueryDefinition; import com.marklogic.client.query.StringQueryDefinition; import com.marklogic.client.query.StructuredQueryBuilder.Operator; import com.marklogic.client.pojo.PojoQueryBuilder; +import com.marklogic.client.pojo.annotation.Id; import com.marklogic.client.test.BulkReadWriteTest; import com.marklogic.client.test.BulkReadWriteTest.CityWriter; @@ -48,10 +51,11 @@ public class PojoFacadeTest { public static void beforeClass() { Common.connect(); cities = Common.client.newPojoRepository(City.class, Integer.class); - //System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); } @AfterClass public static void afterClass() { + cleanUp(); Common.release(); } @@ -297,8 +301,57 @@ public void testD_PojosWithChildren() throws Exception { assertEquals("Should find one city", 1, cities.search(query, 1).getTotalSize()); } + static public class Product1 { + @Id + public int id; + public String name; + } + + static public class Product2 { + @Id + @JsonSerialize(using=ToStringSerializer.class) + public int id; + public String name; + } + @Test - public void testE_DeletePojos() throws Exception { + public void testE_IndexNumberAsString() throws Exception { + // without the JsonSerialize annotation, this id indexes as a nubmer and is not searchable + Product1 widget1 = new Product1(); + widget1.id = 1001; + widget1.name = "widget1"; + PojoRepository products1 = Common.client.newPojoRepository(Product1.class, Integer.class); + products1.write(widget1); + + Product2 widget2 = new Product2(); + widget2.id = 2001; + widget2.name = "widget2"; + PojoRepository products2 = Common.client.newPojoRepository(Product2.class, Integer.class); + products2.write(widget2); + + StringQueryDefinition query = Common.client.newQueryManager().newStringDefinition(); + query.setCriteria("1001"); + PojoPage page1 = products1.search(query, 1); + assertEquals("Should not find the product by id", 0, page1.getTotalSize()); + + // though, of course, we can search on string field + query = Common.client.newQueryManager().newStringDefinition(); + query.setCriteria("widget1"); + PojoPage page2 = products1.search(query, 1); + assertEquals("Should find the product by name", 1, page2.getTotalSize()); + assertEquals("Should find the right product id", 1001, page2.next().id); + + // with the JsonSerialize annotation, the id is indexed as a string and therefore searchable + query = Common.client.newQueryManager().newStringDefinition(); + query.setCriteria("2001"); + PojoPage page3 = products2.search(query, 1); + assertEquals("Should find the product by id", 1, page3.getTotalSize()); + assertEquals("Should find the right product id", 2001, page3.next().id); + } + + + @Test + public void testF_DeletePojos() throws Exception { cities.delete(1185098, 2239076); StringQueryDefinition query = Common.client.newQueryManager().newStringDefinition(); query.setCriteria("Tungi OR Dalatando OR Chittagong"); @@ -318,4 +371,12 @@ private void validateCity(City city) { BulkReadWriteTest.validateChittagong(city); } } + + private static void cleanUp() { + PojoRepository products1 = Common.client.newPojoRepository(Product1.class, Integer.class); + PojoRepository products2 = Common.client.newPojoRepository(Product2.class, Integer.class); + products1.deleteAll(); + products2.deleteAll(); + cities.deleteAll(); + } } From 589f25ffc7d9b8fd84ff86295cb0d540333a5169 Mon Sep 17 00:00:00 2001 From: sammefford Date: Thu, 18 Sep 2014 13:10:57 -0600 Subject: [PATCH 133/357] oops, forgot to comment the debug setting --- src/test/java/com/marklogic/client/test/PojoFacadeTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/marklogic/client/test/PojoFacadeTest.java b/src/test/java/com/marklogic/client/test/PojoFacadeTest.java index 513b6bcd6..2f2178cbb 100644 --- a/src/test/java/com/marklogic/client/test/PojoFacadeTest.java +++ b/src/test/java/com/marklogic/client/test/PojoFacadeTest.java @@ -51,7 +51,7 @@ public class PojoFacadeTest { public static void beforeClass() { Common.connect(); cities = Common.client.newPojoRepository(City.class, Integer.class); - System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + //System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); } @AfterClass public static void afterClass() { From c1e0850237c81a2dc9629b36145afcc48cd0fe04 Mon Sep 17 00:00:00 2001 From: sammefford Date: Thu, 18 Sep 2014 15:04:29 -0600 Subject: [PATCH 134/357] fix #64, runtime database selection. Test failing until we finish eval implementation --- .../client/DatabaseClientFactory.java | 76 +++++++++++++++++-- .../marklogic/client/impl/JerseyServices.java | 7 +- .../marklogic/client/impl/RESTServices.java | 2 +- .../test/DatabaseClientFactoryTest.java | 18 ++++- 4 files changed, 90 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/marklogic/client/DatabaseClientFactory.java b/src/main/java/com/marklogic/client/DatabaseClientFactory.java index 8eb349ed7..307623933 100644 --- a/src/main/java/com/marklogic/client/DatabaseClientFactory.java +++ b/src/main/java/com/marklogic/client/DatabaseClientFactory.java @@ -207,7 +207,21 @@ private DatabaseClientFactory() { * @return a new client for making database requests */ static public DatabaseClient newClient(String host, int port) { - return newClient(host, port, null, null, null, null, null); + return newClient(host, port, null, null, null, null, null, null); + } + + /** + * Creates a client to access the database by means of a REST server + * without any authentication. Such clients can be convenient for + * experimentation but should not be used in production. + * + * @param host the host with the REST server + * @param port the port for the REST server + * @param database the database to access (default: configured database for the REST server) + * @return a new client for making database requests + */ + static public DatabaseClient newClient(String host, int port, String database) { + return newClient(host, port, database, null, null, null, null, null); } /** @@ -221,7 +235,21 @@ static public DatabaseClient newClient(String host, int port) { * @return a new client for making database requests */ static public DatabaseClient newClient(String host, int port, String user, String password, Authentication type) { - return newClient(host, port, user, password, type, null, null); + return newClient(host, port, null, user, password, type, null, null); + } + /** + * Creates a client to access the database by means of a REST server. + * + * @param host the host with the REST server + * @param port the port for the REST server + * @param database the database to access (default: configured database for the REST server) + * @param user the user with read, write, or administrative privileges + * @param password the password for the user + * @param type the type of authentication applied to the request + * @return a new client for making database requests + */ + static public DatabaseClient newClient(String host, int port, String database, String user, String password, Authentication type) { + return newClient(host, port, database, user, password, type, null, null); } /** * Creates a client to access the database by means of a REST server. @@ -235,7 +263,22 @@ static public DatabaseClient newClient(String host, int port, String user, Strin * @return a new client for making database requests */ static public DatabaseClient newClient(String host, int port, String user, String password, Authentication type, SSLContext context) { - return newClient(host, port, user, password, type, context, SSLHostnameVerifier.COMMON); + return newClient(host, port, null, user, password, type, context, SSLHostnameVerifier.COMMON); + } + /** + * Creates a client to access the database by means of a REST server. + * + * @param host the host with the REST server + * @param port the port for the REST server + * @param database the database to access (default: configured database for the REST server) + * @param user the user with read, write, or administrative privileges + * @param password the password for the user + * @param type the type of authentication applied to the request + * @param context the SSL context for authenticating with the server + * @return a new client for making database requests + */ + static public DatabaseClient newClient(String host, int port, String database, String user, String password, Authentication type, SSLContext context) { + return newClient(host, port, database, user, password, type, context, SSLHostnameVerifier.COMMON); } /** * Creates a client to access the database by means of a REST server. @@ -250,14 +293,32 @@ static public DatabaseClient newClient(String host, int port, String user, Strin * @return a new client for making database requests */ static public DatabaseClient newClient(String host, int port, String user, String password, Authentication type, SSLContext context, SSLHostnameVerifier verifier) { - DatabaseClientImpl client = newClientImpl(host, port, user, password, type, context, verifier); + DatabaseClientImpl client = newClientImpl(host, port, null, user, password, type, context, verifier); + client.setHandleRegistry(getHandleRegistry().copy()); + return client; + } + /** + * Creates a client to access the database by means of a REST server. + * + * @param host the host with the REST server + * @param port the port for the REST server + * @param database the database to access (default: configured database for the REST server) + * @param user the user with read, write, or administrative privileges + * @param password the password for the user + * @param type the type of authentication applied to the request + * @param context the SSL context for authenticating with the server + * @param verifier a callback for checking hostnames + * @return a new client for making database requests + */ + static public DatabaseClient newClient(String host, int port, String database, String user, String password, Authentication type, SSLContext context, SSLHostnameVerifier verifier) { + DatabaseClientImpl client = newClientImpl(host, port, database, user, password, type, context, verifier); client.setHandleRegistry(getHandleRegistry().copy()); return client; } - static private DatabaseClientImpl newClientImpl(String host, int port, String user, String password, Authentication type, SSLContext context, SSLHostnameVerifier verifier) { + static private DatabaseClientImpl newClientImpl(String host, int port, String database, String user, String password, Authentication type, SSLContext context, SSLHostnameVerifier verifier) { logger.debug("Creating new database client for server at "+host+":"+port); JerseyServices services = new JerseyServices(); - services.connect(host, port, user, password, type, context, verifier); + services.connect(host, port, database, user, password, type, context, verifier); if (clientConfigurator != null) { ((HttpClientConfigurator) clientConfigurator).configure( @@ -337,6 +398,7 @@ static public class Bean implements Serializable { private String host; private int port; + private String database; private String user; private String password; private Authentication authentication; @@ -510,7 +572,7 @@ public void registerDefaultHandles() { * @return a new client for making database requests */ public DatabaseClient newClient() { - DatabaseClientImpl client = newClientImpl(host, port, user, password, authentication, context, verifier); + DatabaseClientImpl client = newClientImpl(host, port, database, user, password, authentication, context, verifier); client.setHandleRegistry(getHandleRegistry().copy()); return client; diff --git a/src/main/java/com/marklogic/client/impl/JerseyServices.java b/src/main/java/com/marklogic/client/impl/JerseyServices.java index 76d3f7e9a..9940d4cfd 100644 --- a/src/main/java/com/marklogic/client/impl/JerseyServices.java +++ b/src/main/java/com/marklogic/client/impl/JerseyServices.java @@ -195,7 +195,7 @@ private FailedRequest extractErrorFields(ClientResponse response) { } @Override - public void connect(String host, int port, String user, String password, + public void connect(String host, int port, String database, String user, String password, Authentication authenType, SSLContext context, SSLHostnameVerifier verifier) { X509HostnameVerifier x509Verifier = null; @@ -215,10 +215,10 @@ else if (context != null) throw new IllegalArgumentException( "Null SSLContent but non-null SSLHostnameVerifier for client"); - connect(host, port, user, password, authenType, context, x509Verifier); + connect(host, port, database, user, password, authenType, context, x509Verifier); } - private void connect(String host, int port, String user, String password, + private void connect(String host, int port, String database, String user, String password, Authentication authenType, SSLContext context, X509HostnameVerifier verifier) { if (logger.isDebugEnabled()) @@ -327,6 +327,7 @@ private void connect(String host, int port, String user, String password, // new UsernamePasswordCredentials(user, password)); HttpParams httpParams = new BasicHttpParams(); + if (database != null) httpParams.setParameter("database", database); if (authenType != null) { List authpref = new ArrayList(); diff --git a/src/main/java/com/marklogic/client/impl/RESTServices.java b/src/main/java/com/marklogic/client/impl/RESTServices.java index 34b8b0873..030382e62 100644 --- a/src/main/java/com/marklogic/client/impl/RESTServices.java +++ b/src/main/java/com/marklogic/client/impl/RESTServices.java @@ -55,7 +55,7 @@ import com.sun.jersey.api.client.ClientResponse; public interface RESTServices { - public void connect(String host, int port, String user, String password, Authentication type, + public void connect(String host, int port, String database, String user, String password, Authentication type, SSLContext context, SSLHostnameVerifier verifier); public DatabaseClient getDatabaseClient(); public void setDatabaseClient(DatabaseClient client); diff --git a/src/test/java/com/marklogic/client/test/DatabaseClientFactoryTest.java b/src/test/java/com/marklogic/client/test/DatabaseClientFactoryTest.java index 26e26beba..4d1c0391a 100644 --- a/src/test/java/com/marklogic/client/test/DatabaseClientFactoryTest.java +++ b/src/test/java/com/marklogic/client/test/DatabaseClientFactoryTest.java @@ -15,14 +15,14 @@ */ package com.marklogic.client.test; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; import org.apache.http.client.HttpClient; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; +import com.marklogic.client.DatabaseClient; import com.marklogic.client.DatabaseClientFactory; import com.marklogic.client.DatabaseClientFactory.Authentication; import com.marklogic.client.extra.httpclient.HttpClientConfigurator; @@ -42,6 +42,20 @@ public void testConnectStringIntStringStringDigest() { assertNotNull("Factory could not create client with digest connection", Common.client); } + @Test + public void testRuntimeDatabaseSelection() { + DatabaseClient tmpClient = DatabaseClientFactory.newClient( + Common.HOST, Common.PORT, "Documents", Common.USERNAME, Common.PASSWORD, Authentication.DIGEST + ); + assertNotNull("Factory could not create client with digest connection", tmpClient); + String database = + tmpClient.newServerEval() + .xquery("xdmp:database-name(xdmp:database())") + .evalAs(String.class); + assertEquals("Runtime database should is wrong", "Documents", database); + tmpClient.release(); + } + @Test public void testConfigurator() { ConfiguratorImpl configurator = new ConfiguratorImpl(); From 2157272fb4b46290fa5205eeedf305cef5f173f6 Mon Sep 17 00:00:00 2001 From: sammefford Date: Fri, 19 Sep 2014 08:22:49 -0600 Subject: [PATCH 135/357] fix #108, allow DocumentPage to close underlying http connection --- src/main/java/com/marklogic/client/Page.java | 6 +- .../client/document/DocumentPage.java | 9 +- .../com/marklogic/client/impl/BasicPage.java | 8 +- .../client/impl/DocumentPageImpl.java | 36 -------- .../marklogic/client/impl/JerseyServices.java | 82 +++++++++++++------ .../client/impl/PojoRepositoryImpl.java | 3 +- .../com/marklogic/client/test/PageTest.java | 29 +++---- 7 files changed, 88 insertions(+), 85 deletions(-) delete mode 100644 src/main/java/com/marklogic/client/impl/DocumentPageImpl.java diff --git a/src/main/java/com/marklogic/client/Page.java b/src/main/java/com/marklogic/client/Page.java index 1f735ccfe..aa09facaf 100644 --- a/src/main/java/com/marklogic/client/Page.java +++ b/src/main/java/com/marklogic/client/Page.java @@ -19,10 +19,8 @@ /** A generic interface for pagination through large sets of items of type <T>. */ public interface Page extends Iterator, Iterable { - /** A new iterator over the elements of type T in this Page. Each call to iterator() - * will create a new Iterator which will start at the first record in this Page. - * Iterators created via this method are indepentent of the internal iterator - * used for {@link hasNext()} and {@link next()}. + /** The internal iterator of type T in this Page. This iterator is the same + * one used for {@link hasNext()} and {@link next()}. */ public Iterator iterator(); diff --git a/src/main/java/com/marklogic/client/document/DocumentPage.java b/src/main/java/com/marklogic/client/document/DocumentPage.java index b4295701a..42bd4f197 100644 --- a/src/main/java/com/marklogic/client/document/DocumentPage.java +++ b/src/main/java/com/marklogic/client/document/DocumentPage.java @@ -18,6 +18,13 @@ import com.marklogic.client.Page; import com.marklogic.client.io.marker.AbstractReadHandle; -public interface DocumentPage extends Page { +import java.io.Closeable; + +/** Allows iteration over documents in the page. When you finish with this instance + * you must call close() to free the underlying resources. */ +public interface DocumentPage extends Page, Closeable { + /** Convenience method combines the functionality of Page.next() and DocumentRecord.getContent(). */ public T nextContent(T contentHandle); + /** Frees the underlying resources, including the http connection. */ + public void close(); } diff --git a/src/main/java/com/marklogic/client/impl/BasicPage.java b/src/main/java/com/marklogic/client/impl/BasicPage.java index db318fd26..be994e962 100644 --- a/src/main/java/com/marklogic/client/impl/BasicPage.java +++ b/src/main/java/com/marklogic/client/impl/BasicPage.java @@ -19,7 +19,6 @@ import com.marklogic.client.Page; public class BasicPage implements Page { - private Iterable iterable; private Iterator iterator; private long start; private Long size = null; @@ -29,16 +28,15 @@ public class BasicPage implements Page { protected BasicPage(Class type) { } - public BasicPage(Iterable iterable, long start, long pageSize, long totalSize) { - this.iterable = iterable; - this.iterator = iterable.iterator(); + public BasicPage(Iterator iterator, long start, long pageSize, long totalSize) { + this.iterator = iterator; this.start = start; this.pageSize = pageSize; this.totalSize = totalSize; } public Iterator iterator() { - return iterable.iterator(); + return iterator; } protected Iterator internalIterator() { diff --git a/src/main/java/com/marklogic/client/impl/DocumentPageImpl.java b/src/main/java/com/marklogic/client/impl/DocumentPageImpl.java deleted file mode 100644 index e5b2b6a46..000000000 --- a/src/main/java/com/marklogic/client/impl/DocumentPageImpl.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2012-2014 MarkLogic Corporation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.marklogic.client.impl; - -import java.util.Iterator; - -import com.marklogic.client.document.DocumentPage; -import com.marklogic.client.document.DocumentRecord; -import com.marklogic.client.io.marker.AbstractReadHandle; - -class DocumentPageImpl extends BasicPage implements DocumentPage { - private Iterable iterable; - - public DocumentPageImpl(Iterable iterable, long start, long size, long pageSize, long totalSize) { - super(iterable, start, pageSize, totalSize); - this.setSize(size); - this.iterable = iterable; - } - - public T nextContent(T contentHandle) { - return internalIterator().next().getContent(contentHandle); - } -} diff --git a/src/main/java/com/marklogic/client/impl/JerseyServices.java b/src/main/java/com/marklogic/client/impl/JerseyServices.java index 9940d4cfd..034c76c16 100644 --- a/src/main/java/com/marklogic/client/impl/JerseyServices.java +++ b/src/main/java/com/marklogic/client/impl/JerseyServices.java @@ -15,7 +15,9 @@ */ package com.marklogic.client.impl; +import java.io.Closeable; import java.io.File; +import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; import java.io.Reader; @@ -25,6 +27,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.NoSuchElementException; import java.util.Properties; import java.util.Random; import java.util.Set; @@ -700,7 +703,7 @@ public DocumentPage getBulkDocuments(RequestLogger reqlog, boolean hasMetadata = categories != null && categories.size() > 0; JerseyResultIterator iterator = getBulkDocumentsImpl(reqlog, transactionId, categories, format, extraParams, withContent, uris); - return convertToDocumentPage(iterator, withContent, hasMetadata); + return new JerseyDocumentPage(iterator, withContent, hasMetadata); } @Override @@ -716,17 +719,40 @@ public DocumentPage getBulkDocuments(RequestLogger reqlog, JerseyResultIterator iterator = getBulkDocumentsImpl(reqlog, querydef, start, pageLength, transactionId, searchHandle, view, categories, format, extraParams); - return convertToDocumentPage(iterator, hasContent, hasMetadata); - } + return new JerseyDocumentPage(iterator, hasContent, hasMetadata); + } + + private class JerseyDocumentPage extends BasicPage implements DocumentPage { + private JerseyResultIterator iterator; + private boolean hasMetadata; + private boolean hasContent; + + JerseyDocumentPage(JerseyResultIterator iterator, boolean hasContent, boolean hasMetadata) { + super( + new ArrayList().iterator(), + iterator != null ? iterator.getStart() : 1, + iterator != null ? iterator.getPageSize() : 0, + iterator != null ? iterator.getTotalSize() : 0 + ); + if ( iterator == null ) { + setSize(0); + } else { + setSize(iterator.getSize()); + } + this.iterator = iterator; + this.hasContent = hasContent; + this.hasMetadata = hasMetadata; + } - private DocumentPage convertToDocumentPage(JerseyResultIterator iterator, boolean hasContent, - boolean hasMetadata) - { - ArrayList records = new ArrayList(); - if ( iterator == null ) { - return new DocumentPageImpl(records, 1, 0, 0, 0); - } - while (iterator.hasNext()) { + @Override + public boolean hasNext() { + if ( iterator == null ) return false; + return iterator.hasNext(); + } + + @Override + public DocumentRecord next() { + if ( iterator == null ) throw new NoSuchElementException("No documents available"); JerseyResult result = iterator.next(); DocumentRecord record; if ( hasContent && hasMetadata ) { @@ -742,10 +768,16 @@ record = new JerseyDocumentRecord(null, metadata); } else { throw new IllegalStateException("Should never have neither content nor metadata"); } - records.add(record); + return record; + } + + public T nextContent(T contentHandle) { + return next().getContent(contentHandle); + } + + public void close() { + if ( iterator != null ) iterator.close(); } - return new DocumentPageImpl(records, iterator.getStart(), - iterator.getSize(), iterator.getPageSize(), iterator.getTotalSize()); } private JerseyResultIterator getBulkDocumentsImpl(RequestLogger reqlog, @@ -4121,7 +4153,6 @@ private U makeResults( if ( response == null ) return null; MultiPart entity = response.hasEntity() ? response.getEntity(MultiPart.class) : null; - response.close(); if (entity == null) return null; return makeResults(clazz, reqlog, operation, entityType, entity.getBodyParts(), response); } @@ -4136,8 +4167,8 @@ private U makeResults( try { java.lang.reflect.Constructor constructor = - clazz.getConstructor(JerseyServices.class, RequestLogger.class, List.class); - JerseyResultIterator result = constructor.newInstance(this, reqlog, partList); + clazz.getConstructor(JerseyServices.class, RequestLogger.class, List.class, ClientResponse.class); + JerseyResultIterator result = constructor.newInstance(this, reqlog, partList, response); MultivaluedMap headers = response.getHeaders(); if (headers.containsKey("vnd.marklogic.start")) { result.setStart(Long.parseLong(headers.get("vnd.marklogic.start").get(0))); @@ -4286,10 +4317,10 @@ public class JerseyResultIterator { private long size = -1; private long pageSize = -1; private long totalSize = -1; + private ClientResponse closeable; public JerseyResultIterator(RequestLogger reqlog, - List partList, Class clazz) { - super(); + List partList, Class clazz, ClientResponse closeable) { this.clazz = clazz; if (partList != null && partList.size() > 0) { this.size = partList.size(); @@ -4297,6 +4328,7 @@ public JerseyResultIterator(RequestLogger reqlog, this.partQueue = new ConcurrentLinkedQueue( partList).iterator(); } + this.closeable = closeable; } public long getStart() { @@ -4370,6 +4402,9 @@ public void remove() { public void close() { partQueue = null; reqlog = null; + if ( closeable != null ) { + closeable.close(); + } } protected void finalize() throws Throwable { @@ -4383,17 +4418,18 @@ public class JerseyServiceResultIterator implements ServiceResultIterator { public JerseyServiceResultIterator(RequestLogger reqlog, - List partList) { - super(reqlog, partList, JerseyServiceResult.class); + List partList, ClientResponse closeable) { + super(reqlog, partList, JerseyServiceResult.class, closeable); } } public class DefaultJerseyResultIterator extends JerseyResultIterator + implements Iterator { public DefaultJerseyResultIterator(RequestLogger reqlog, - List partList) { - super(reqlog, partList, JerseyResult.class); + List partList, ClientResponse closeable) { + super(reqlog, partList, JerseyResult.class, closeable); } } diff --git a/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java b/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java index eefeea023..3202d47c6 100644 --- a/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java +++ b/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java @@ -167,8 +167,7 @@ public T read(ID id, Transaction transaction) { ids.add(id); PojoPage page = read(ids.toArray((ID[])new Serializable[0]), transaction); if ( page == null ) return null; - Iterator iterator = page.iterator(); - if ( iterator.hasNext() ) return iterator.next(); + if ( page.hasNext() ) return page.next(); return null; } public PojoPage read(ID[] ids) { diff --git a/src/test/java/com/marklogic/client/test/PageTest.java b/src/test/java/com/marklogic/client/test/PageTest.java index 94c3a03fa..7df4287c9 100644 --- a/src/test/java/com/marklogic/client/test/PageTest.java +++ b/src/test/java/com/marklogic/client/test/PageTest.java @@ -16,6 +16,7 @@ package com.marklogic.client.test; import java.util.HashSet; +import java.util.Iterator; import static org.junit.Assert.assertEquals; @@ -27,16 +28,16 @@ /** Implements com.marklogic.client.Page to test the default methods. **/ public class PageTest { - class TestPage extends BasicPage { - TestPage(Iterable iterable, long start, long pageSize, long totalSize) { - super(iterable, start, pageSize, totalSize); - } - } + class TestPage extends BasicPage { + TestPage(Iterator iterator, long start, long pageSize, long totalSize) { + super(iterator, start, pageSize, totalSize); + } + } @Test public void testTestPage() { - Iterable iterable = new HashSet(); + Iterator iterator = new HashSet().iterator(); - Page page = new TestPage(iterable, 1, 10, 100); + Page page = new TestPage(iterator, 1, 10, 100); assertEquals("Unexpected size", 10, page.size()); assertEquals("Unexpected totalPages", 10, page.getTotalPages()); assertEquals("Unexpected hasContent", true, page.hasContent()); @@ -45,7 +46,7 @@ public void testTestPage() { assertEquals("Unexpected isFirstPage", true, page.isFirstPage()); assertEquals("Unexpected isLastPage", false, page.isLastPage()); - page = new TestPage(iterable, 2, 10, 100); + page = new TestPage(iterator, 2, 10, 100); assertEquals("Unexpected size", 10, page.size()); assertEquals("Unexpected totalPages", 10, page.getTotalPages()); assertEquals("Unexpected hasContent", true, page.hasContent()); @@ -54,7 +55,7 @@ public void testTestPage() { assertEquals("Unexpected isFirstPage", true, page.isFirstPage()); assertEquals("Unexpected isLastPage", false, page.isLastPage()); - page = new TestPage(iterable, 10, 10, 100); + page = new TestPage(iterator, 10, 10, 100); assertEquals("Unexpected size", 10, page.size()); assertEquals("Unexpected totalPages", 10, page.getTotalPages()); assertEquals("Unexpected hasContent", true, page.hasContent()); @@ -63,7 +64,7 @@ public void testTestPage() { assertEquals("Unexpected isFirstPage", false, page.isFirstPage()); assertEquals("Unexpected isLastPage", false, page.isLastPage()); - page = new TestPage(iterable, 12, 10, 100); + page = new TestPage(iterator, 12, 10, 100); assertEquals("Unexpected size", 10, page.size()); assertEquals("Unexpected totalPages", 10, page.getTotalPages()); assertEquals("Unexpected hasContent", true, page.hasContent()); @@ -72,7 +73,7 @@ public void testTestPage() { assertEquals("Unexpected isFirstPage", false, page.isFirstPage()); assertEquals("Unexpected isLastPage", false, page.isLastPage()); - page = new TestPage(iterable, 22, 20, 100); + page = new TestPage(iterator, 22, 20, 100); assertEquals("Unexpected size", 20, page.size()); assertEquals("Unexpected totalPages", 5, page.getTotalPages()); assertEquals("Unexpected hasContent", true, page.hasContent()); @@ -81,7 +82,7 @@ public void testTestPage() { assertEquals("Unexpected isFirstPage", false, page.isFirstPage()); assertEquals("Unexpected isLastPage", false, page.isLastPage()); - page = new TestPage(iterable, 18, 20, 20); + page = new TestPage(iterator, 18, 20, 20); assertEquals("Unexpected size", 20, page.size()); assertEquals("Unexpected totalPages", 1, page.getTotalPages()); assertEquals("Unexpected hasContent", true, page.hasContent()); @@ -90,7 +91,7 @@ public void testTestPage() { assertEquals("Unexpected isFirstPage", true, page.isFirstPage()); assertEquals("Unexpected isLastPage", true, page.isLastPage()); - page = new TestPage(iterable, 905, 100, 990); + page = new TestPage(iterator, 905, 100, 990); assertEquals("Unexpected size", 90, page.size()); assertEquals("Unexpected totalPages", 10, page.getTotalPages()); assertEquals("Unexpected hasContent", true, page.hasContent()); @@ -98,5 +99,5 @@ public void testTestPage() { assertEquals("Unexpected pageNumber", 10, page.getPageNumber()); assertEquals("Unexpected isFirstPage", false, page.isFirstPage()); assertEquals("Unexpected isLastPage", true, page.isLastPage()); - } + } } From a2479ca986b261668782c5a35bbea4a08ab0e8d1 Mon Sep 17 00:00:00 2001 From: sammefford Date: Fri, 19 Sep 2014 09:33:00 -0600 Subject: [PATCH 136/357] implenting Iterator required a remove method which isn't appropriate for pages, so we'll just not implement Iterator --- src/main/java/com/marklogic/client/Page.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/marklogic/client/Page.java b/src/main/java/com/marklogic/client/Page.java index aa09facaf..76657b0fe 100644 --- a/src/main/java/com/marklogic/client/Page.java +++ b/src/main/java/com/marklogic/client/Page.java @@ -18,7 +18,7 @@ import java.util.Iterator; /** A generic interface for pagination through large sets of items of type <T>. */ -public interface Page extends Iterator, Iterable { +public interface Page extends Iterable { /** The internal iterator of type T in this Page. This iterator is the same * one used for {@link hasNext()} and {@link next()}. */ From 4d89e6ec7f548a9c8c1460acc55686b1184faaec Mon Sep 17 00:00:00 2001 From: sammefford Date: Mon, 29 Sep 2014 08:52:25 -0600 Subject: [PATCH 137/357] add commented debug line --- src/test/java/com/marklogic/client/test/KeyValueSearchTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test/java/com/marklogic/client/test/KeyValueSearchTest.java b/src/test/java/com/marklogic/client/test/KeyValueSearchTest.java index c86d2cc33..a117b1ae1 100644 --- a/src/test/java/com/marklogic/client/test/KeyValueSearchTest.java +++ b/src/test/java/com/marklogic/client/test/KeyValueSearchTest.java @@ -42,6 +42,8 @@ public class KeyValueSearchTest { public static void beforeClass() throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException { Common.connect(); + //System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + } @AfterClass From abd2d3df0f4041ff5d6f7d02c44d12ad1de5e8f7 Mon Sep 17 00:00:00 2001 From: sammefford Date: Mon, 29 Sep 2014 08:54:08 -0600 Subject: [PATCH 138/357] add commented debug line --- .../client/test/example/extension/GraphSPARQLExampleTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test/java/com/marklogic/client/test/example/extension/GraphSPARQLExampleTest.java b/src/test/java/com/marklogic/client/test/example/extension/GraphSPARQLExampleTest.java index 9fdc9ce2c..745db8e30 100644 --- a/src/test/java/com/marklogic/client/test/example/extension/GraphSPARQLExampleTest.java +++ b/src/test/java/com/marklogic/client/test/example/extension/GraphSPARQLExampleTest.java @@ -13,6 +13,8 @@ public class GraphSPARQLExampleTest { @Test public void testMain() { + //System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + boolean succeeded = false; try { GraphSPARQLExample.main(new String[0]); From 90bd04ff8064b1d92c70c4c57beddf52f6b841a9 Mon Sep 17 00:00:00 2001 From: sammefford Date: Mon, 29 Sep 2014 09:51:22 -0600 Subject: [PATCH 139/357] rename setResponseFormat to setNonDocumentFormat; default to null so server default for nonDocumentFormat (usually XML) can come through --- .../client/document/DocumentManager.java | 8 +++--- .../client/impl/DocumentManagerImpl.java | 22 +++++++-------- .../client/impl/JSONDocumentImpl.java | 1 - .../client/test/BulkReadWriteTest.java | 28 ++++++++++--------- .../client/test/QueryByExampleTest.java | 1 - 5 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/marklogic/client/document/DocumentManager.java b/src/main/java/com/marklogic/client/document/DocumentManager.java index a7b2fe2c3..f96d49cce 100644 --- a/src/main/java/com/marklogic/client/document/DocumentManager.java +++ b/src/main/java/com/marklogic/client/document/DocumentManager.java @@ -432,10 +432,10 @@ public T read(DocumentDescriptor desc, DocumentMetadataReadHandle public void setPageLength(long length); - public Format getResponseFormat(); - - public void setResponseFormat(Format format); - + public Format getNonDocumentFormat(); + + public void setNonDocumentFormat(Format nonDocumentFormat); + public QueryView getSearchView(); public void setSearchView(QueryView view); diff --git a/src/main/java/com/marklogic/client/impl/DocumentManagerImpl.java b/src/main/java/com/marklogic/client/impl/DocumentManagerImpl.java index 85afb88cc..70a6e6707 100644 --- a/src/main/java/com/marklogic/client/impl/DocumentManagerImpl.java +++ b/src/main/java/com/marklogic/client/impl/DocumentManagerImpl.java @@ -87,7 +87,7 @@ public boolean addAll(Collection c) { private String forestName; private long pageLength = DEFAULT_PAGE_LENGTH; private QueryView searchView = QueryView.RESULTS; - private Format responseFormat = Format.XML; + private Format nonDocumentFormat = null; DocumentManagerImpl(RESTServices services, Format contentFormat) { super(); @@ -344,7 +344,7 @@ public DocumentPage read(ServerTransform transform, Transaction transaction, (transaction == null) ? null : transaction.getTransactionId(), // the default for bulk is no metadata, which differs from the normal default of ALL isProcessedMetadataModified ? processedMetadata : null, - responseFormat, + nonDocumentFormat, mergeTransformParameters( (transform != null) ? transform : getReadTransform(), null @@ -384,9 +384,9 @@ public DocumentPage search(QueryDefinition querydef, long start, SearchReadHandl responseHandle.setHandleRegistry(getHandleRegistry()); responseHandle.setQueryCriteria(querydef); } - if ( responseFormat != searchBase.getFormat() ) { + if ( nonDocumentFormat != null && nonDocumentFormat != searchBase.getFormat() ) { throw new UnsupportedOperationException("The format supported by your handle:[" + - searchBase.getFormat() + "] does not match your setResponseFormat:[" + responseFormat + "]"); + searchBase.getFormat() + "] does not match the non-document format:[" + nonDocumentFormat + "]"); } } @@ -394,7 +394,7 @@ public DocumentPage search(QueryDefinition querydef, long start, SearchReadHandl // the default for bulk is no metadata, which differs from the normal default of ALL Set metadata = isProcessedMetadataModified ? processedMetadata : null; return services.getBulkDocuments( requestLogger, querydef, start, getPageLength(), - tid, searchHandle, searchView, metadata, responseFormat, null); + tid, searchHandle, searchView, metadata, nonDocumentFormat, null); } public long getPageLength() { @@ -413,16 +413,16 @@ public void setSearchView(QueryView view) { this.searchView = view; } - public Format getResponseFormat() { - return responseFormat; + public Format getNonDocumentFormat() { + return nonDocumentFormat; } - public void setResponseFormat(Format responseFormat) { - if ( responseFormat != Format.XML && responseFormat != Format.JSON ) { + public void setNonDocumentFormat(Format nonDocumentFormat) { + if ( nonDocumentFormat != Format.XML && nonDocumentFormat != Format.JSON ) { throw new UnsupportedOperationException("Only XML and JSON are valid response formats. You specified:[" + - responseFormat + "]"); + nonDocumentFormat + "]"); } - this.responseFormat = responseFormat; + this.nonDocumentFormat = nonDocumentFormat; } public DocumentWriteSet newWriteSet() { diff --git a/src/main/java/com/marklogic/client/impl/JSONDocumentImpl.java b/src/main/java/com/marklogic/client/impl/JSONDocumentImpl.java index 0754f5a86..f30f8e61c 100644 --- a/src/main/java/com/marklogic/client/impl/JSONDocumentImpl.java +++ b/src/main/java/com/marklogic/client/impl/JSONDocumentImpl.java @@ -30,7 +30,6 @@ public class JSONDocumentImpl JSONDocumentImpl(RESTServices services) { super(services,Format.JSON); - setResponseFormat(Format.JSON); } @Override diff --git a/src/test/java/com/marklogic/client/test/BulkReadWriteTest.java b/src/test/java/com/marklogic/client/test/BulkReadWriteTest.java index f007eec35..dd58d42a8 100644 --- a/src/test/java/com/marklogic/client/test/BulkReadWriteTest.java +++ b/src/test/java/com/marklogic/client/test/BulkReadWriteTest.java @@ -72,7 +72,7 @@ public class BulkReadWriteTest { public static void beforeClass() throws JAXBException { Common.connect(); context = JAXBContext.newInstance(City.class); - //System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); } @AfterClass public static void afterClass() { @@ -206,16 +206,20 @@ public void testD_JsonLoad() { writeSet.add("doc2.json", doc2Metadata, doc2); docMgr.write(writeSet); - - JacksonHandle content1 = new JacksonHandle(); - docMgr.read("doc1.json", content1); - JacksonHandle content2 = new JacksonHandle(); - DocumentMetadataHandle metadata2 = new DocumentMetadataHandle(); - docMgr.read("doc2.json", metadata2, content2); - - assertEquals("Failed to read document 1", "dog", content1.get().get("animal").textValue()); - assertEquals("Failed to read expected quality", 2, metadata2.getQuality()); - assertEquals("Failed to read document 2", "cat", content2.get().get("animal").textValue()); + + docMgr.setMetadataCategories(Metadata.QUALITY); + docMgr.setNonDocumentFormat(Format.JSON); + DocumentPage documents = docMgr.read("doc1.json", "doc2.json"); + for ( DocumentRecord record : documents ) { + JacksonHandle content = record.getContent(new JacksonHandle()); + JacksonHandle metadata = record.getMetadata(new JacksonHandle()); + if ( "doc1.json".equals(record.getUri()) ) { + assertEquals("Failed to read document 1", "dog", content.get().get("animal").textValue()); + } else if ( "doc2.json".equals(record.getUri()) ) { + assertEquals("Failed to read document 2", "cat", content.get().get("animal").textValue()); + assertEquals("Failed to read expected quality", 2, metadata.get().get("quality").intValue()); + } + } } private void validateRecord(DocumentRecord record) { @@ -320,8 +324,6 @@ public void testF_DefaultMetadata() { // Execute the write operation jdm.write(batch); - // need the "synthetic response" format to be XML - jdm.setResponseFormat(Format.XML); // Check the results assertEquals("Doc1 should have the system default quality of 0", 0, jdm.readMetadata("doc1.json", new DocumentMetadataHandle()).getQuality()); diff --git a/src/test/java/com/marklogic/client/test/QueryByExampleTest.java b/src/test/java/com/marklogic/client/test/QueryByExampleTest.java index 8e6bfb8c0..9dbb297f1 100644 --- a/src/test/java/com/marklogic/client/test/QueryByExampleTest.java +++ b/src/test/java/com/marklogic/client/test/QueryByExampleTest.java @@ -74,7 +74,6 @@ public void jsonQbe() { documents = jdm.search(query, 1, new JacksonHandle()); assertEquals("6 json documents should have matched", documents.getTotalSize(), 6); - jdm.setResponseFormat(Format.XML); documents = jdm.search(query, 1, new SearchHandle()); assertEquals("6 json documents should have matched", documents.getTotalSize(), 6); } From 0a87e197778d452e3afaed572fc7ed24d18e0659 Mon Sep 17 00:00:00 2001 From: sammefford Date: Mon, 29 Sep 2014 09:53:24 -0600 Subject: [PATCH 140/357] remove obsolete internalIterator method --- src/main/java/com/marklogic/client/impl/BasicPage.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/com/marklogic/client/impl/BasicPage.java b/src/main/java/com/marklogic/client/impl/BasicPage.java index be994e962..4fcbfef98 100644 --- a/src/main/java/com/marklogic/client/impl/BasicPage.java +++ b/src/main/java/com/marklogic/client/impl/BasicPage.java @@ -39,10 +39,6 @@ public Iterator iterator() { return iterator; } - protected Iterator internalIterator() { - return iterator; - } - public boolean hasNext() { return iterator.hasNext(); } From 3215c5c3f9c7fddc660d061ad4847baef304fe9a Mon Sep 17 00:00:00 2001 From: sammefford Date: Mon, 29 Sep 2014 09:55:37 -0600 Subject: [PATCH 141/357] set format to match handle format when search results are requested with bulk read; finish JerseyDocumentPage so it also works as Iterable --- .../marklogic/client/impl/JerseyServices.java | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/marklogic/client/impl/JerseyServices.java b/src/main/java/com/marklogic/client/impl/JerseyServices.java index 034c76c16..a0a0f796b 100644 --- a/src/main/java/com/marklogic/client/impl/JerseyServices.java +++ b/src/main/java/com/marklogic/client/impl/JerseyServices.java @@ -722,8 +722,9 @@ public DocumentPage getBulkDocuments(RequestLogger reqlog, return new JerseyDocumentPage(iterator, hasContent, hasMetadata); } - private class JerseyDocumentPage extends BasicPage implements DocumentPage { + private class JerseyDocumentPage extends BasicPage implements DocumentPage, Iterator { private JerseyResultIterator iterator; + private Iterator docRecordIterator; private boolean hasMetadata; private boolean hasContent; @@ -734,14 +735,19 @@ private class JerseyDocumentPage extends BasicPage implements Do iterator != null ? iterator.getPageSize() : 0, iterator != null ? iterator.getTotalSize() : 0 ); + this.iterator = iterator; + this.hasContent = hasContent; + this.hasMetadata = hasMetadata; if ( iterator == null ) { setSize(0); } else { setSize(iterator.getSize()); } - this.iterator = iterator; - this.hasContent = hasContent; - this.hasMetadata = hasMetadata; + } + + @Override + public Iterator iterator() { + return this; } @Override @@ -749,6 +755,10 @@ public boolean hasNext() { if ( iterator == null ) return false; return iterator.hasNext(); } + + public void remove() { + throw new UnsupportedOperationException(); + } @Override public DocumentRecord next() { @@ -812,6 +822,14 @@ private JerseyResultIterator getBulkDocumentsImpl(RequestLogger reqlog, if (pageLength > 0) params.add("pageLength", Long.toString(pageLength)); if (format != null) params.add("format", format.toString().toLowerCase()); if (transactionId != null) params.add("txid", transactionId); + if ( format == null && searchHandle != null ) { + HandleImplementation handleBase = HandleAccessor.as(searchHandle); + if ( Format.XML == handleBase.getFormat() ) { + params.add("format", "xml"); + } else if ( Format.JSON == handleBase.getFormat() ) { + params.add("format", "json"); + } + } JerseySearchRequest request = generateSearchRequest(reqlog, querydef, MultiPartMediaTypes.MULTIPART_MIXED, params); From 3f1dbaa411adc02bd10e141a4c99356a0cfa839e Mon Sep 17 00:00:00 2001 From: sammefford Date: Mon, 29 Sep 2014 11:36:54 -0600 Subject: [PATCH 142/357] remove containerQuery(StructuredQueryDefinition) because the only containers in pojos should instead use containerQueryBuilder(); also beginnings of #121, move term() methods into PojoQueryBuilder and update tests appropriately --- .../client/impl/PojoQueryBuilderImpl.java | 47 +++++++-- .../client/impl/PojoRepositoryImpl.java | 4 - .../client/pojo/PojoQueryBuilder.java | 5 +- .../client/query/StructuredQueryBuilder.java | 98 +++++++++---------- .../marklogic/client/test/PojoFacadeTest.java | 10 +- 5 files changed, 92 insertions(+), 72 deletions(-) diff --git a/src/main/java/com/marklogic/client/impl/PojoQueryBuilderImpl.java b/src/main/java/com/marklogic/client/impl/PojoQueryBuilderImpl.java index 11f020fad..ad3b6e3ab 100644 --- a/src/main/java/com/marklogic/client/impl/PojoQueryBuilderImpl.java +++ b/src/main/java/com/marklogic/client/impl/PojoQueryBuilderImpl.java @@ -17,6 +17,7 @@ import com.marklogic.client.pojo.PojoQueryBuilder; import com.marklogic.client.query.StructuredQueryBuilder; +import com.marklogic.client.query.StructuredQueryBuilder.TermQuery; import com.marklogic.client.query.StructuredQueryBuilder.TextIndex; import com.marklogic.client.query.StructuredQueryDefinition; @@ -24,6 +25,8 @@ import java.lang.reflect.Modifier; import java.util.Date; import java.util.HashMap; + +import javax.xml.stream.XMLStreamWriter; public class PojoQueryBuilderImpl extends StructuredQueryBuilder implements PojoQueryBuilder { private HashMap types = new HashMap(); @@ -57,12 +60,9 @@ public StructuredQueryDefinition containerQuery(String pojoProperty, StructuredQ return super.containerQuery(jsonProperty(pojoProperty), query); } } - @Override - public StructuredQueryDefinition containerQuery(StructuredQueryDefinition query) { - return super.containerQuery(jsonProperty(classWrapper), query); - } - public PojoQueryBuilder containerQueryBuilder(String pojoProperty) { - return new PojoQueryBuilderImpl(getType(pojoProperty), true); + @SuppressWarnings("unchecked") + public PojoQueryBuilder containerQueryBuilder(String pojoProperty, Class clazz) { + return new PojoQueryBuilderImpl((Class) getType(pojoProperty), true); } @Override public StructuredQueryBuilder.GeospatialIndex @@ -159,12 +159,39 @@ public StructuredQueryDefinition word(String pojoProperty, String[] options, return super.word(jsonProperty(pojoProperty), null, options, weight, words); } } + @SuppressWarnings("deprecation") + public TermQuery term(String... terms) { + return new PojoTermQuery(wrapQueries, null, terms); + } + @SuppressWarnings("deprecation") + public TermQuery term(double weight, String... terms) { + return new PojoTermQuery(wrapQueries, weight, terms); + + } + @SuppressWarnings("deprecation") + public class PojoTermQuery extends StructuredQueryBuilder.TermQuery { + private AbstractStructuredQuery query; + public PojoTermQuery(boolean wrapQueries, Double weight, String... terms) { + super(weight, terms); + TermQuery termQuery = new TermQuery(weight, terms); + if ( wrapQueries ) { + query = (AbstractStructuredQuery) containerQuery(jsonProperty(classWrapper), termQuery); + } else { + query = termQuery; + } + } + + @Override + public void innerSerialize(XMLStreamWriter serializer) throws Exception { + query.innerSerialize(serializer); + } + }; public String getRangeIndexType(String propertyName) { // map java types to acceptable Range Index types String type = rangeIndextypes.get(propertyName); if ( type == null ) { - Class propertyClass = getType(propertyName); + Class propertyClass = getType(propertyName); if ( String.class.isAssignableFrom(propertyClass) ) { type = "xs:string"; } else if ( Integer.TYPE.equals(propertyClass) ) { @@ -188,8 +215,8 @@ public String getRangeIndexType(String propertyName) { return type; } - public Class getType(String propertyName) { - Class propertyClass = types.get(propertyName); + public Class getType(String propertyName) { + Class propertyClass = types.get(propertyName); if ( propertyClass == null ) { // figure out the type of the java property String initCapPojoProperty = propertyName.substring(0,1).toUpperCase() + @@ -222,7 +249,7 @@ public Class getType(String propertyName) { String setMethodName = "set" + initCapPojoProperty; for ( Method method : clazz.getMethods() ) { if ( setMethodName.equals(method.getName()) ) { - Class[] parameters = method.getParameterTypes(); + Class[] parameters = method.getParameterTypes(); if ( parameters != null && parameters.length == 1 ) { propertyClass = parameters[0]; break; diff --git a/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java b/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java index 3202d47c6..2b0450576 100644 --- a/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java +++ b/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java @@ -72,7 +72,6 @@ public class PojoRepositoryImpl this.entityClass = entityClass; this.idClass = null; this.docMgr = client.newJSONDocumentManager(); - this.docMgr.setResponseFormat(Format.JSON); this.qb = new PojoQueryBuilderImpl(entityClass); } @@ -206,7 +205,6 @@ public PojoPage search(QueryDefinition query, long start, SearchReadHandle se return search(query, start, searchHandle, null); } public PojoPage search(QueryDefinition query, long start, SearchReadHandle searchHandle, Transaction transaction) { - Format docMgrFormat = docMgr.getResponseFormat(); if ( searchHandle != null ) { HandleImplementation searchBase = HandleAccessor.checkHandle(searchHandle, "search"); if (searchHandle instanceof SearchHandle) { @@ -216,12 +214,10 @@ public PojoPage search(QueryDefinition query, long start, SearchReadHandle se } responseHandle.setQueryCriteria(query); } - docMgr.setResponseFormat(searchBase.getFormat()); } String tid = transaction == null ? null : transaction.getTransactionId(); DocumentPage docPage = docMgr.search(wrapQuery(query), start, searchHandle, transaction); - docMgr.setResponseFormat(docMgrFormat); PojoPage pojoPage = new PojoPageImpl(docPage, entityClass); return pojoPage; } diff --git a/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java b/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java index 7833b6fe6..e4d16e4f7 100644 --- a/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java +++ b/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java @@ -115,9 +115,6 @@ public interface PojoQueryBuilder extends CombinedQueryBuilder { public StructuredQueryDefinition containerQuery(String pojoProperty, StructuredQueryDefinition query); - /** @return a query matching pojos of type T with children matching the specified query */ - public StructuredQueryDefinition containerQuery(StructuredQueryDefinition query); - /** Use this method to provide a query builder that can query a nested object within your pojo. * All other PojoQueryBuilder methods create queries for direct children of T which are native * types. If a child of T is an object, and you need to query one of its children, this method @@ -125,7 +122,7 @@ public StructuredQueryDefinition containerQuery(String pojoProperty, * nested objects you may use this method on the each returned PojoQueryBuilder which represents * one level deeper. * @return a PojoQueryBuilder for nested pojos of the type corresponding with pojoProperty */ - public PojoQueryBuilder containerQueryBuilder(String pojoProperty); + public PojoQueryBuilder containerQueryBuilder(String pojoProperty, Class clazz); public StructuredQueryBuilder.GeospatialIndex geoPair(String latitudePropertyName, String longitudePropertyName); /** diff --git a/src/main/java/com/marklogic/client/query/StructuredQueryBuilder.java b/src/main/java/com/marklogic/client/query/StructuredQueryBuilder.java index 8638f4691..2a118aa40 100644 --- a/src/main/java/com/marklogic/client/query/StructuredQueryBuilder.java +++ b/src/main/java/com/marklogic/client/query/StructuredQueryBuilder.java @@ -916,7 +916,7 @@ public CustomConstraintQuery customConstraint(String constraintName, String... t /* ************************************************************************************* */ // TODO IN A FUTURE RELEASE: remove the deprecated innerSerialize() method - private abstract class AbstractStructuredQuery + protected abstract class AbstractStructuredQuery extends AbstractQueryDefinition implements StructuredQueryDefinition { public AbstractStructuredQuery() { @@ -937,7 +937,7 @@ public String innerSerialize() { return extractQueryContent(serializeQueries(this)); } - abstract void innerSerialize(XMLStreamWriter serializer) throws Exception; + public abstract void innerSerialize(XMLStreamWriter serializer) throws Exception; } // TODO IN A FUTURE RELEASE: change the visibility of the deprecated @@ -964,7 +964,7 @@ public AndQuery(StructuredQueryDefinition... queries) { } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { writeQueryList(serializer, "and-query", convertQueries(queries)); } } @@ -988,7 +988,7 @@ public OrQuery(StructuredQueryDefinition... queries) { } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { writeQueryList(serializer, "or-query", convertQueries(queries)); } } @@ -1012,7 +1012,7 @@ public NotQuery(StructuredQueryDefinition query) { } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { writeQuery(serializer, "not-query", (AbstractStructuredQuery) query); } } @@ -1034,7 +1034,7 @@ public NotInQuery(StructuredQueryDefinition positive, StructuredQueryDefinition } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { serializer.writeStartElement("not-in-query"); writeQuery(serializer, "positive-query", (AbstractStructuredQuery) positive); writeQuery(serializer, "negative-query", (AbstractStructuredQuery) negative); @@ -1064,7 +1064,7 @@ public AndNotQuery(StructuredQueryDefinition positive, StructuredQueryDefinition } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { serializer.writeStartElement("and-not-query"); writeQuery(serializer, "positive-query", (AbstractStructuredQuery) positive); writeQuery(serializer, "negative-query", (AbstractStructuredQuery) negative); @@ -1089,7 +1089,7 @@ public BoostQuery(StructuredQueryDefinition matchingQuery, StructuredQueryDefini } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { serializer.writeStartElement("boost-query"); writeQuery(serializer, "matching-query", (AbstractStructuredQuery) matchingQuery); writeQuery(serializer, "boosting-query", (AbstractStructuredQuery) boostingQuery); @@ -1118,7 +1118,7 @@ public DocumentQuery(String... uris) { } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { serializer.writeStartElement("document-query"); writeTextList(serializer, "uri", uris); serializer.writeEndElement(); @@ -1147,7 +1147,7 @@ public TermQuery(Double weight, String... terms) { } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { serializer.writeStartElement("term-query"); writeTextList(serializer, "text", terms); writeText(serializer, "weight", weight); @@ -1185,7 +1185,7 @@ public NearQuery(Integer distance, Double weight, Ordering order, StructuredQuer } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { serializer.writeStartElement("near-query"); writeQueryList(serializer, queries); if (order != null) { @@ -1220,7 +1220,7 @@ public CollectionQuery(String... uris) { } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { serializer.writeStartElement("collection-query"); writeTextList(serializer, "uri", uris); serializer.writeEndElement(); @@ -1258,7 +1258,7 @@ public DirectoryQuery(Boolean isInfinite, String... uris) { } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { serializer.writeStartElement("directory-query"); if (depth != null) { serializer.writeAttribute("depth", Integer.toString(depth)); @@ -1289,7 +1289,7 @@ public DocumentFragmentQuery(StructuredQueryDefinition query) { } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { writeQuery(serializer, "document-fragment-query", (AbstractStructuredQuery) query); } } @@ -1314,7 +1314,7 @@ public PropertiesQuery(StructuredQueryDefinition query) { } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { writeQuery(serializer, "properties-query", (AbstractStructuredQuery) query); } } @@ -1339,7 +1339,7 @@ public LocksQuery(StructuredQueryDefinition query) { } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { writeQuery(serializer, "locks-query", (AbstractStructuredQuery) query); } } @@ -1367,7 +1367,7 @@ public ContainerConstraintQuery(String constraintName, StructuredQueryDefinition } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { serializer.writeStartElement("container-constraint-query"); writeText(serializer, "constraint-name", name); writeQuery(serializer, query); @@ -1397,7 +1397,7 @@ public ElementConstraintQuery(String constraintName, StructuredQueryDefinition q } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { serializer.writeStartElement("element-constraint-query"); writeText(serializer, "constraint-name", name); writeQuery(serializer, query); @@ -1427,7 +1427,7 @@ public PropertiesConstraintQuery(String constraintName, StructuredQueryDefinitio } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { serializer.writeStartElement("properties-constraint-query"); writeText(serializer, "constraint-name", name); writeQuery(serializer, query); @@ -1457,7 +1457,7 @@ public CollectionConstraintQuery(String constraintName, String... uris) { } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { serializer.writeStartElement("collection-constraint-query"); writeText(serializer, "constraint-name", name); writeTextList(serializer, "uri", uris); @@ -1494,7 +1494,7 @@ public ValueConstraintQuery(String constraintName, Double weight, String... valu } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { serializer.writeStartElement("value-constraint-query"); writeText(serializer, "constraint-name", name); writeTextList(serializer, "text", values); @@ -1532,7 +1532,7 @@ public WordConstraintQuery(String constraintName, Double weight, String... words } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { serializer.writeStartElement("word-constraint-query"); writeText(serializer, "constraint-name", name); writeTextList(serializer, "text", words); @@ -1565,7 +1565,7 @@ public RangeConstraintQuery(String constraintName, Operator operator, String... } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { serializer.writeStartElement("range-constraint-query"); writeText(serializer, "constraint-name", name); writeTextList(serializer, "value", values); @@ -1596,7 +1596,7 @@ public GeospatialConstraintQuery(String constraintName, Region... regions) { } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { serializer.writeStartElement("geospatial-constraint-query"); writeText(serializer, "constraint-name", name); for (Region region : regions) { @@ -1628,7 +1628,7 @@ public CustomConstraintQuery(String constraintName, String... terms) { } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { serializer.writeStartElement("custom-constraint-query"); writeText(serializer, "constraint-name", name); writeTextList(serializer, "text", terms); @@ -1645,7 +1645,7 @@ class ContainerQuery this.query = query; } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { serializer.writeStartElement("container-query"); ((IndexImpl) index).innerSerialize(serializer); writeQuery(serializer, query); @@ -1668,7 +1668,7 @@ abstract class TextQuery this.weight = weight; this.values = values; } - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { ((IndexImpl) index).innerSerialize(serializer); if (scope != null) { if (scope == FragmentScope.DOCUMENT) { @@ -1700,7 +1700,7 @@ class ValueQuery this.weight = weight; this.values = values; } - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { serializer.writeStartElement("value-query"); ((IndexImpl) index).innerSerialize(serializer); if (scope != null) { @@ -1739,7 +1739,7 @@ class WordQuery super(index, scope, options, weight, values); } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { serializer.writeStartElement("word-query"); super.innerSerialize(serializer); serializer.writeEndElement(); @@ -1773,7 +1773,7 @@ class RangeQuery } } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { serializer.writeStartElement("range-query"); if (type != null) { serializer.writeAttribute("type", type); @@ -1810,7 +1810,7 @@ class GeospatialQuery this.options = options; } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { String elemName = null; if (index instanceof GeoElementImpl) elemName = "geo-elem-query"; @@ -1840,8 +1840,8 @@ else if (index instanceof GeoPathImpl) /* ************************************************************************************* */ - abstract class IndexImpl { - abstract void innerSerialize(XMLStreamWriter serializer) throws Exception; + protected abstract class IndexImpl { + protected abstract void innerSerialize(XMLStreamWriter serializer) throws Exception; } class ElementImpl extends IndexImpl implements Element { String name; @@ -1853,7 +1853,7 @@ class ElementImpl extends IndexImpl implements Element { this.name = name; } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { serializeNamedIndex(serializer, "element", qname, name); } } @@ -1867,7 +1867,7 @@ class AttributeImpl extends IndexImpl implements Attribute { this.name = name; } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { serializeNamedIndex(serializer, "attribute", qname, name); } } @@ -1879,7 +1879,7 @@ class ElementAttributeImpl extends IndexImpl implements ElementAttribute { this.attribute = attribute; } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { ((IndexImpl) element).innerSerialize(serializer); ((IndexImpl) attribute).innerSerialize(serializer); } @@ -1890,7 +1890,7 @@ class FieldImpl extends IndexImpl implements Field { this.name = name; } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { serializer.writeStartElement("field"); serializer.writeAttribute("name", name); serializer.writeEndElement(); @@ -1902,7 +1902,7 @@ class JSONPropertyImpl extends IndexImpl implements JSONProperty { this.name = name; } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { writeText(serializer, "json-property", name); } } @@ -1912,7 +1912,7 @@ class PathIndexImpl extends IndexImpl implements PathIndex { this.path = path; } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { writeText(serializer, "path-index", path); } } @@ -1928,7 +1928,7 @@ class GeoElementImpl extends IndexImpl implements GeospatialIndex { this.parent = parent; } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { if (parent != null && parent instanceof ElementImpl) { ElementImpl parentImpl = (ElementImpl) parent; serializeNamedIndex(serializer, "parent", parentImpl.qname, parentImpl.name); @@ -1955,7 +1955,7 @@ class GeoElementPairImpl extends IndexImpl implements GeospatialIndex { this.lon = lon; } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { ElementImpl parentImpl = (ElementImpl) parent; ElementImpl latImpl = (ElementImpl) lat; ElementImpl lonImpl = (ElementImpl) lon; @@ -1974,7 +1974,7 @@ class GeoAttributePairImpl extends IndexImpl implements GeospatialIndex { this.lon = lon; } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { ElementImpl parentImpl = (ElementImpl) parent; AttributeImpl latImpl = (AttributeImpl) lat; AttributeImpl lonImpl = (AttributeImpl) lon; @@ -1990,7 +1990,7 @@ private class GeoPathImpl extends IndexImpl implements GeospatialIndex { this.pathIndex = pathIndex; } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { PathIndexImpl pathIndexImpl = (PathIndexImpl) pathIndex; pathIndexImpl.innerSerialize(serializer); ; } @@ -2014,7 +2014,7 @@ public interface Region { // TODO IN A FUTURE RELEASE: separate a public Point interface // from a package PointImpl class - abstract class RegionImpl { + protected abstract class RegionImpl { /** * @deprecated Returns the region as a partial string. This method will be removed in a future * release. @@ -2024,7 +2024,7 @@ abstract class RegionImpl { public String serialize() { return extractQueryContent(serializeRegions(this)); } - abstract void innerSerialize(XMLStreamWriter serializer) throws Exception; + protected abstract void innerSerialize(XMLStreamWriter serializer) throws Exception; } /** @@ -2054,7 +2054,7 @@ public double getLongitude() { } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { serializer.writeStartElement("point"); writeText(serializer, "latitude", String.valueOf(lat)); writeText(serializer, "longitude", String.valueOf(lon)); @@ -2085,7 +2085,7 @@ public Circle(double latitude, double longitude, double radius) { } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { serializer.writeStartElement("circle"); writeText(serializer, "radius", String.valueOf(radius)); center.innerSerialize(serializer); @@ -2113,7 +2113,7 @@ public Box(double south, double west, double north, double east) { } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { serializer.writeStartElement("box"); writeText(serializer, "south", String.valueOf(south)); writeText(serializer, "west", String.valueOf(west)); @@ -2140,7 +2140,7 @@ public Polygon(Point... points) { } @Override - void innerSerialize(XMLStreamWriter serializer) throws Exception { + public void innerSerialize(XMLStreamWriter serializer) throws Exception { serializer.writeStartElement("polygon"); for (Point point: points) { point.innerSerialize(serializer); diff --git a/src/test/java/com/marklogic/client/test/PojoFacadeTest.java b/src/test/java/com/marklogic/client/test/PojoFacadeTest.java index 2f2178cbb..4c9e2a01a 100644 --- a/src/test/java/com/marklogic/client/test/PojoFacadeTest.java +++ b/src/test/java/com/marklogic/client/test/PojoFacadeTest.java @@ -277,8 +277,8 @@ public void testD_PojosWithChildren() throws Exception { cities.write(abuDhabi); cities.write(buenosAires); - PojoQueryBuilder qb = cities.getQueryBuilder(); - PojoQueryBuilder countriesQb = qb.containerQueryBuilder("country"); + PojoQueryBuilder qb = cities.getQueryBuilder(); + PojoQueryBuilder countriesQb = qb.containerQueryBuilder("country", Country.class); QueryDefinition query = countriesQb.value("continent", "EU"); assertEquals("Should not find any countries", 0, cities.search(query, 1).getTotalSize()); @@ -289,12 +289,12 @@ public void testD_PojosWithChildren() throws Exception { assertEquals("Should find two cities", 2, cities.search(query, 1).getTotalSize()); // all countries containing the term SA - query = countriesQb.containerQuery(countriesQb.term("SA")); + query = countriesQb.term("SA"); assertEquals("Should find one city", 1, cities.search(query, 1).getTotalSize()); // all cities containing the term SA - query = qb.containerQuery(qb.term("SA")); - assertEquals("Should find two cities", 61, cities.search(query, 1).getTotalSize()); + query = qb.term("SA"); + assertEquals("Should find sixty one cities", 61, cities.search(query, 1).getTotalSize()); // all countries containing the property "currencyName" with the term "peso" query = countriesQb.word("currencyName", "peso"); From 55aabf3e06af6825068ff211defe200773d1b19d Mon Sep 17 00:00:00 2001 From: sammefford Date: Mon, 29 Sep 2014 14:38:41 -0600 Subject: [PATCH 143/357] fix #6, javascript resource extensions with a cookbook example --- .../cookbook/JavascriptResourceExtension.java | 192 ++++++++++++++++++ src/main/resources/scripts/helloWorld.sjs | 21 ++ 2 files changed, 213 insertions(+) create mode 100644 src/main/java/com/marklogic/client/example/cookbook/JavascriptResourceExtension.java create mode 100644 src/main/resources/scripts/helloWorld.sjs diff --git a/src/main/java/com/marklogic/client/example/cookbook/JavascriptResourceExtension.java b/src/main/java/com/marklogic/client/example/cookbook/JavascriptResourceExtension.java new file mode 100644 index 000000000..b965166e4 --- /dev/null +++ b/src/main/java/com/marklogic/client/example/cookbook/JavascriptResourceExtension.java @@ -0,0 +1,192 @@ +/* + * Copyright 2012-2014 MarkLogic Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.marklogic.client.example.cookbook; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.XMLConstants; +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.w3c.dom.Document; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.FailedRequestException; +import com.marklogic.client.ForbiddenUserException; +import com.marklogic.client.ResourceNotFoundException; +import com.marklogic.client.admin.ExtensionMetadata; +import com.marklogic.client.admin.MethodType; +import com.marklogic.client.admin.ResourceExtensionsManager; +import com.marklogic.client.admin.ResourceExtensionsManager.MethodParameters; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.example.cookbook.Util.ExampleProperties; +import com.marklogic.client.extensions.ResourceManager; +import com.marklogic.client.extensions.ResourceServices.ServiceResult; +import com.marklogic.client.extensions.ResourceServices.ServiceResultIterator; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.InputStreamHandle; +import com.marklogic.client.io.StringHandle; +import com.marklogic.client.io.XMLStreamReaderHandle; +import com.marklogic.client.util.RequestParameters; + +/** + * JavascriptResourceExtension installs an extension for managing spelling dictionary resources. + */ +public class JavascriptResourceExtension { + public static void main(String[] args) + throws IOException, ResourceNotFoundException, ForbiddenUserException, FailedRequestException { + run(Util.loadProperties()); + } + + // install and then use the resource extension + public static void run(ExampleProperties props) + throws IOException, ResourceNotFoundException, ForbiddenUserException, FailedRequestException { + //System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + + System.out.println("example: "+JavascriptResourceExtension.class.getName()); + + installResourceExtension(props.host, props.port, + props.adminUser, props.adminPassword, props.authType); + + useResource(props.host, props.port, + props.writerUser, props.writerPassword, props.authType); + + tearDownExample(props.host, props.port, + props.adminUser, props.adminPassword, props.authType); + } + + /** + * HelloWorld provides an example of a class that implements + * a resource extension client, exposing a method for each service. + * Typically, this class would be a top-level class. + */ + static public class HelloWorld extends ResourceManager { + static final public String NAME = "helloWorld"; + static final public ExtensionMetadata.ScriptLanguage scriptLanguage + = ExtensionMetadata.JAVASCRIPT; + private XMLDocumentManager docMgr; + + public HelloWorld(DatabaseClient client) { + super(); + + // a Resource Manager must be initialized by a Database Client + client.init(NAME, this, ExtensionMetadata.JAVASCRIPT); + + // the Dictionary Manager delegates some services to a document manager + docMgr = client.newXMLDocumentManager(); + } + + public String sayHello() { + RequestParameters params = new RequestParameters(); + params.add("service", "hello"); + params.add("planet", "Earth"); + + // specify the mime type for each expected document returned + String[] mimetypes = new String[] {"text/plain"}; + + // call the service + ServiceResultIterator resultItr = getServices().get(params, mimetypes); + + // iterate over the results + List responses = new ArrayList(); + StringHandle readHandle = new StringHandle(); + while (resultItr.hasNext()) { + ServiceResult result = resultItr.next(); + + // get the result content + result.getContent(readHandle); + responses.add(readHandle.get()); + } + + // release the iterator resources + resultItr.close(); + + return responses.get(0); + } + + } + + // install the resource extension on the server + public static void installResourceExtension(String host, int port, String user, String password, Authentication authType) throws IOException { + // create the client + DatabaseClient client = DatabaseClientFactory.newClient(host, port, user, password, authType); + + // use either shortcut or strong typed IO + installResourceExtension(client); + + // release the client + client.release(); + } + public static void installResourceExtension(DatabaseClient client) throws IOException { + // create a manager for resource extensions + ResourceExtensionsManager resourceMgr = client.newServerConfigManager().newResourceExtensionsManager(); + + // specify metadata about the resource extension + ExtensionMetadata metadata = new ExtensionMetadata(); + metadata.setTitle("Hello World Resource Services"); + metadata.setDescription("This resource extension is written in javascript"); + metadata.setProvider("MarkLogic"); + metadata.setVersion("0.1"); + metadata.setScriptLanguage(HelloWorld.scriptLanguage); + + // acquire the resource extension source code + InputStream sourceStream = Util.openStream( + "scripts"+File.separator+HelloWorld.NAME+".sjs"); + if (sourceStream == null) + throw new IOException("Could not read example resource extension"); + + // write the resource extension to the database + resourceMgr.writeServicesAs(HelloWorld.NAME, sourceStream, metadata, + new MethodParameters(MethodType.GET)); + + System.out.println("(Shortcut) Installed the resource extension on the server"); + } + + // use the resource manager + public static void useResource(String host, int port, String user, String password, Authentication authType) + throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException { + // create the client + DatabaseClient client = DatabaseClientFactory.newClient(host, port, user, password, authType); + + // create the resource extension client + HelloWorld hello = new HelloWorld(client); + + String response = hello.sayHello(); + System.out.println("Called hello worlds service, got response:["+ response + "]"); + + // release the client + client.release(); + } + + // clean up by deleting the example resource extension + public static void tearDownExample( + String host, int port, String user, String password, Authentication authType) { + DatabaseClient client = DatabaseClientFactory.newClient(host, port, user, password, authType); + + ResourceExtensionsManager resourceMgr = client.newServerConfigManager().newResourceExtensionsManager(); + + resourceMgr.deleteServices(HelloWorld.NAME); + + client.release(); + } +} diff --git a/src/main/resources/scripts/helloWorld.sjs b/src/main/resources/scripts/helloWorld.sjs new file mode 100644 index 000000000..7379ddd56 --- /dev/null +++ b/src/main/resources/scripts/helloWorld.sjs @@ -0,0 +1,21 @@ +// module that exports get, post, put and delete +function getMethod(context, params) { + //context.outputTypes = ["application/json"]; + //return { "response": "This is a JSON document" } + return "hello " + params["planet"]; +}; + +function postMethod(context, params, input) { +}; + +function putMethod(context, params, input) { +}; + +function deleteMethod(context, params) { +}; + +exports.get = getMethod; +exports.post = postMethod; +exports.put = putMethod; +exports.delete = deleteMethod; + From 08b6b414fbd2dd4db4ccc99828693bda6183758d Mon Sep 17 00:00:00 2001 From: sammefford Date: Mon, 29 Sep 2014 14:50:25 -0600 Subject: [PATCH 144/357] add convenience methods to get results as string, number, and boolean --- src/main/java/com/marklogic/client/eval/EvalResult.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/com/marklogic/client/eval/EvalResult.java b/src/main/java/com/marklogic/client/eval/EvalResult.java index 17f9a881d..22012f280 100644 --- a/src/main/java/com/marklogic/client/eval/EvalResult.java +++ b/src/main/java/com/marklogic/client/eval/EvalResult.java @@ -61,4 +61,7 @@ enum Type { public Type getType(); public H get(H handle); public T getAs(Class clazz); + public String getString(); + public Number getNumber(); + public boolean getBoolean(); } From a47ac670a3a42387b6b6c62b06ab1efcea43a3f5 Mon Sep 17 00:00:00 2001 From: sravan Date: Thu, 2 Oct 2014 08:09:02 -0700 Subject: [PATCH 145/357] adding new test for pojo builder value query --- .../TestPOJOQueryBuilderValueQuery.java | 226 ++++++++++++++++++ 1 file changed, 226 insertions(+) create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderValueQuery.java diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderValueQuery.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderValueQuery.java new file mode 100644 index 000000000..298dd5012 --- /dev/null +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderValueQuery.java @@ -0,0 +1,226 @@ +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.admin.QueryOptionsManager; +import com.marklogic.client.admin.ServerConfigurationManager; +import com.marklogic.client.document.DocumentManager; +import com.marklogic.client.io.JacksonHandle; +import com.marklogic.client.io.QueryOptionsHandle; +import com.marklogic.client.io.SearchHandle; +import com.marklogic.client.pojo.PojoPage; +import com.marklogic.client.pojo.PojoQueryBuilder; +import com.marklogic.client.pojo.PojoRepository; +import com.marklogic.client.query.QueryDefinition; + +public class TestPOJOQueryBuilderValueQuery extends BasicJavaClientREST { + + private static String dbName = "TestPOJOQueryBuilderValueSearchDB"; + private static String [] fNames = {"TestPOJOQueryBuilderValueSearchDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client ; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { +// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + BasicJavaClientREST.setDatabaseProperties(dbName, "trailing-wildcard-searches", true); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + System.out.println("In tear down" ); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } + @Before + public void setUp() throws Exception { + client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST); + setupServerRequestLogging(client,true); + + } + @After + public void tearDown() throws Exception { + // release client + client.release(); + setupServerRequestLogging(client,true); + } + + public Artifact getArtifact(int counter){ + + Artifact cogs = new Artifact(); + cogs.setId(counter); + if( counter % 5 == 0){ + cogs.setName("Cogs special"); + if(counter % 2 ==0){ + Company acme = new Company(); + acme.setName("Acme special, Inc."); + acme.setWebsite("http://www.acme special.com"); + acme.setLatitude(41.998+counter); + acme.setLongitude(-87.966+counter); + cogs.setManufacturer(acme); + + }else{ + Company widgets = new Company(); + widgets.setName("Widgets counter Inc."); + widgets.setWebsite("http://www.widgets counter.com"); + widgets.setLatitude(41.998+counter); + widgets.setLongitude(-87.966+counter); + cogs.setManufacturer(widgets); + } + }else{ + cogs.setName("Cogs "+counter); + if(counter % 2 ==0){ + Company acme = new Company(); + acme.setName("Acme "+counter+", Inc."); + acme.setWebsite("http://www.acme"+counter+".com"); + acme.setLatitude(41.998+counter); + acme.setLongitude(-87.966+counter); + cogs.setManufacturer(acme); + + }else{ + Company widgets = new Company(); + widgets.setName("Widgets "+counter+", Inc."); + widgets.setWebsite("http://www.widgets"+counter+".com"); + widgets.setLatitude(41.998+counter); + widgets.setLongitude(-87.966+counter); + cogs.setManufacturer(widgets); + } + } + cogs.setInventory(1000+counter); + return cogs; + } + public void validateArtifact(Artifact art) + { + assertNotNull("Artifact object should never be Null",art); + assertNotNull("Id should never be Null",art.id); + assertTrue("Inventry is always greater than 1000", art.getInventory()>1000); + } + public void loadSimplePojos(PojoRepository products) + { + for(int i=1;i<111;i++){ + if(i%2==0){ + products.write(this.getArtifact(i),"even","numbers"); + } + else { + products.write(this.getArtifact(i),"odd","numbers"); + } + } + } +// Below scenario is to test the value query with numbers return correct results + @Test + public void testPOJOValueSearchWithNumbers() { + PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); + PojoPage p; + + this.loadSimplePojos(products); + String[] searchOptions ={"case-sensitive","wildcarded","min-occurs=2"}; + PojoQueryBuilder qb = products.getQueryBuilder(); + Number[] searchIds = {5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100,105,110,115,120,121,122,123,124,125,126}; + QueryDefinition qd = qb.value("id",searchOptions,-1.0,searchIds); + + JacksonHandle jh = new JacksonHandle(); + products.setPageLength(5); + p = products.search(qd, 1,jh); + assertEquals("total no of pages",5,p.getTotalPages()); + System.out.println(jh.get().toString()); + long pageNo=1,count=0; + do{ + count =0; + p = products.search(qd,pageNo); + while(p.hasNext()){ + Artifact a =p.next(); + validateArtifact(a); + assertTrue("Verifying document id is part of the search ids",a.getId()%5==0); + count++; + } + assertEquals("Page size",count,p.size()); + pageNo=pageNo+p.getPageSize(); + }while(!p.isLastPage() && pageNo products = client.newPojoRepository(Artifact.class, Long.class); + PojoPage p; + this.loadSimplePojos(products); + String[] searchOptions ={"case-sensitive","wildcarded","min-occurs=2"}; + PojoQueryBuilder qb = products.getQueryBuilder(); + String[] searchNames = {"Acme spe*","Widgets spe*"}; + QueryDefinition qd = qb.value("name",searchOptions,100.0,searchNames); + JacksonHandle jh = new JacksonHandle(); + products.setPageLength(5); + p = products.search(qd, 1,jh); + + assertEquals("total no of pages",3,p.getTotalPages()); + System.out.println(jh.get().toString()); + long pageNo=1,count=0; + do{ + count =0; + p = products.search(qd,pageNo); + while(p.hasNext()){ + Artifact a =p.next(); + validateArtifact(a); + assertTrue("Verifying document id is part of the search ids",a.getId()%5==0); + count++; + System.out.println(a.getId()); + } + assertEquals("Page size",count,p.size()); + pageNo=pageNo+p.getPageSize(); + }while(!p.isLastPage() && pageNo<=p.getTotalSize()); + assertEquals("page number after the loop",3,p.getPageNumber()); + assertEquals("total no of pages",3,p.getTotalPages()); + assertEquals("page length from search handle",5,jh.get().path("page-length").asInt()); + assertEquals("Total results from search handle",11,jh.get().path("total").asInt()); + } +//Below scenario is verifying value query from PojoBuilder that matches to no document + //Issue 127 is logged for the below scenario + @Test + public void testPOJOValueSearchWithNoResults() { + PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); + PojoPage p; + this.loadSimplePojos(products); + String[] searchOptions ={"case-sensitive","wildcarded","min-occurs=2"}; + PojoQueryBuilder qb = products.getQueryBuilder(); + String[] searchNames = {"acme*"}; + QueryDefinition qd = qb.value("name",searchOptions,100.0,searchNames); + JacksonHandle jh = new JacksonHandle(); + products.setPageLength(5); + p = products.search(qd, 1,jh); + System.out.println(jh.get().toString()); + assertEquals("total no of pages",3,p.getTotalPages()); + + long pageNo=1,count=0; + do{ + count =0; + p = products.search(qd,pageNo); + while(p.hasNext()){ + Artifact a =p.next(); + validateArtifact(a); + assertTrue("Verifying document id is part of the search ids",a.getId()%5==0); + count++; + System.out.println(a.getId()); + } + assertEquals("Page size",count,p.size()); + pageNo=pageNo+p.getPageSize(); + }while(!p.isLastPage() && pageNo<=p.getTotalSize()); + assertEquals("page number after the loop",3,p.getPageNumber()); + assertEquals("total no of pages",3,p.getTotalPages()); + assertEquals("page length from search handle",5,jh.get().path("page-length").asInt()); + assertEquals("Total results from search handle",11,jh.get().path("total").asInt()); + } +} From f99e51c507e41c8e17acfd2bba5ba71441d00815 Mon Sep 17 00:00:00 2001 From: sravan Date: Thu, 2 Oct 2014 08:11:11 -0700 Subject: [PATCH 146/357] adding a method to enable serverside loging --- .../java/com/marklogic/javaclient/ConnectedRESTQA.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java b/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java index 01ac7f574..933ebf4e9 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java @@ -44,6 +44,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.admin.ServerConfigurationManager; import com.marklogic.client.io.DocumentMetadataHandle; import java.net.InetAddress; @@ -1316,5 +1318,10 @@ public static void setDefaultUser(String usr,String restServerName) throws Clien System.out.println(content); } } - + public static void setupServerRequestLogging(DatabaseClient client,boolean flag) throws Exception { + ServerConfigurationManager scm =client.newServerConfigManager(); + scm.readConfiguration(); + scm.setServerRequestLogging(flag); + scm.writeConfiguration(); + } } \ No newline at end of file From 47a7a175db8e7ed420b64cebcbd5b28841934aa9 Mon Sep 17 00:00:00 2001 From: sravan Date: Thu, 2 Oct 2014 08:33:20 -0700 Subject: [PATCH 147/357] adding new test for pojo builder value query --- .../TestPOJOQueryBuilderValueQuery.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderValueQuery.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderValueQuery.java index 298dd5012..8a8ff0ec3 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderValueQuery.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderValueQuery.java @@ -46,14 +46,14 @@ public static void tearDownAfterClass() throws Exception { @Before public void setUp() throws Exception { client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST); - setupServerRequestLogging(client,true); + } @After public void tearDown() throws Exception { // release client client.release(); - setupServerRequestLogging(client,true); + } public Artifact getArtifact(int counter){ @@ -190,10 +190,11 @@ public void testPOJOValueSearchWithStrings() { //Below scenario is verifying value query from PojoBuilder that matches to no document //Issue 127 is logged for the below scenario @Test - public void testPOJOValueSearchWithNoResults() { + public void testPOJOValueSearchWithNoResults() throws Exception { PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); PojoPage p; this.loadSimplePojos(products); + setupServerRequestLogging(client,true); String[] searchOptions ={"case-sensitive","wildcarded","min-occurs=2"}; PojoQueryBuilder qb = products.getQueryBuilder(); String[] searchNames = {"acme*"}; @@ -201,8 +202,9 @@ public void testPOJOValueSearchWithNoResults() { JacksonHandle jh = new JacksonHandle(); products.setPageLength(5); p = products.search(qd, 1,jh); + setupServerRequestLogging(client,false); System.out.println(jh.get().toString()); - assertEquals("total no of pages",3,p.getTotalPages()); + assertEquals("total no of pages",0,p.getTotalPages()); long pageNo=1,count=0; do{ @@ -218,9 +220,9 @@ public void testPOJOValueSearchWithNoResults() { assertEquals("Page size",count,p.size()); pageNo=pageNo+p.getPageSize(); }while(!p.isLastPage() && pageNo<=p.getTotalSize()); - assertEquals("page number after the loop",3,p.getPageNumber()); - assertEquals("total no of pages",3,p.getTotalPages()); - assertEquals("page length from search handle",5,jh.get().path("page-length").asInt()); - assertEquals("Total results from search handle",11,jh.get().path("total").asInt()); + assertEquals("page number after the loop",0,p.getPageNumber()); + assertEquals("total no of pages",0,p.getTotalPages()); + assertEquals("page length from search handle",0,jh.get().path("page-length").asInt()); + assertEquals("Total results from search handle",10,jh.get().path("total").asInt()); } } From 483432a36589ca6a2be3ab6563c1781579ad823f Mon Sep 17 00:00:00 2001 From: sravan Date: Thu, 2 Oct 2014 08:40:25 -0700 Subject: [PATCH 148/357] updated the test for a bug fix --- .../javaclient/TestBulkReadWriteWithJacksonParserHandle.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonParserHandle.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonParserHandle.java index 45ea09828..36c0b9d25 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonParserHandle.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonParserHandle.java @@ -503,7 +503,7 @@ public void testBulkSearchQBEWithJSONResponseFormat() throws IOException, Parser // set document manager level settings for search response docMgr.setPageLength(25); docMgr.setSearchView(QueryView.RESULTS); - docMgr.setResponseFormat(Format.JSON); + docMgr.setNonDocumentFormat(Format.JSON); // Search for documents where content has bar and get first result record, get search handle on it,Use DOMHandle to read results JacksonParserHandle sh = new JacksonParserHandle(); From 595d1301f5965cae180bec86ec802f6d3625a7b2 Mon Sep 17 00:00:00 2001 From: sravan Date: Thu, 2 Oct 2014 08:42:26 -0700 Subject: [PATCH 149/357] updated the test for a bug fix --- .../com/marklogic/javaclient/TestBulkSearchEWithQBE.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchEWithQBE.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchEWithQBE.java index 0a9309a82..d83c9d9c1 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchEWithQBE.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchEWithQBE.java @@ -167,7 +167,7 @@ public void testBulkSearchQBEWithXMLResponseFormat() throws IOException, ParserC // set document manager level settings for search response docMgr.setPageLength(25); docMgr.setSearchView(QueryView.RESULTS); - docMgr.setResponseFormat(Format.XML); + docMgr.setNonDocumentFormat(Format.XML); // Search for documents where content has bar and get first result record, get search handle on it,Use DOMHandle to read results DOMHandle dh = new DOMHandle(); @@ -216,7 +216,7 @@ public void testBulkSearchQBEWithJSONResponseFormat() throws IOException, Parser // set document manager level settings for search response docMgr.setPageLength(25); docMgr.setSearchView(QueryView.RESULTS); - docMgr.setResponseFormat(Format.JSON); + docMgr.setNonDocumentFormat(Format.JSON); // Search for documents where content has bar and get first result record, get search handle on it,Use DOMHandle to read results JacksonHandle sh = new JacksonHandle(); @@ -329,7 +329,7 @@ public void testBulkSearchQBEWithJSONCombinedQuery() throws IOException, ParserC // set document manager level settings for search response docMgr.setPageLength(25); docMgr.setSearchView(QueryView.RESULTS); - docMgr.setResponseFormat(Format.JSON); + docMgr.setNonDocumentFormat(Format.JSON); // Search for documents where content has woof and get first result record, JacksonHandle sh = new JacksonHandle(); From 2f93fe84d51e1ae36be6746096e0cdab17199a14 Mon Sep 17 00:00:00 2001 From: sravan Date: Thu, 2 Oct 2014 08:45:03 -0700 Subject: [PATCH 150/357] updated the test for a bug fix --- .../javaclient/TestBulkSearchWithStringQueryDef.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java index acc841e96..d32c44747 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java @@ -160,8 +160,8 @@ public void testBulkSearchSQDwithDifferentPageSizes() { System.out.println("Default Page length setting on docMgr :"+docMgr.getPageLength()); docMgr.setPageLength(1); docMgr.setSearchView(QueryView.RESULTS); - docMgr.setResponseFormat(Format.XML); - assertEquals("format set on document manager","XML",docMgr.getResponseFormat().toString()); + docMgr.setNonDocumentFormat(Format.XML); + assertEquals("format set on document manager","XML",docMgr.getNonDocumentFormat().toString()); assertEquals("Queryview set on document manager ","RESULTS" ,docMgr.getSearchView().toString()); assertEquals("Page length ",1,docMgr.getPageLength()); // Search for documents where content has bar and get first result record, get search handle on it @@ -218,7 +218,7 @@ public void testBulkSearchSQDwithWrongResponseFormat() throws Exception { QueryManager queryMgr = client.newQueryManager(); StringQueryDefinition qd = queryMgr.newStringDefinition(); qd.setCriteria("bar"); - docMgr.setResponseFormat(Format.JSON); + docMgr.setNonDocumentFormat(Format.JSON); SearchHandle results = new SearchHandle(); DocumentPage page= docMgr.search(qd, 1,results); MatchDocumentSummary[] summaries = results.getMatchResults(); @@ -244,7 +244,7 @@ public void testBulkSearchSQDwithResponseFormatandStringHandle() throws Exceptio qd.setCriteria("bar"); - docMgr.setResponseFormat(Format.JSON); + docMgr.setNonDocumentFormat(Format.JSON); docMgr.setSearchView(QueryView.METADATA); docMgr.setMetadataCategories(Metadata.PERMISSIONS); @@ -274,7 +274,7 @@ public void testBulkSearchSQDwithJSONResponseFormat() throws Exception{ QueryManager queryMgr = client.newQueryManager(); StringQueryDefinition qd = queryMgr.newStringDefinition(); qd.setCriteria("woof"); - docMgr.setResponseFormat(Format.JSON); + docMgr.setNonDocumentFormat(Format.JSON); docMgr.setSearchView(QueryView.FACETS); JacksonHandle jh = new JacksonHandle(); From b663e99e2b7d0b8ae748157bb7261c7f086119b1 Mon Sep 17 00:00:00 2001 From: sravan Date: Thu, 2 Oct 2014 08:58:29 -0700 Subject: [PATCH 151/357] updated the test for a bug fix --- .../javaclient/TestBulkSearchWithStrucQueryDef.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStrucQueryDef.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStrucQueryDef.java index 354a53923..de670262e 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStrucQueryDef.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStrucQueryDef.java @@ -149,8 +149,8 @@ public void testBulkSearchSQDwithDifferentPageSizes() throws IOException, Parser System.out.println("Default Page length setting on docMgr :"+docMgr.getPageLength()); docMgr.setPageLength(1); docMgr.setSearchView(QueryView.RESULTS); - docMgr.setResponseFormat(Format.XML); - assertEquals("format set on document manager","XML",docMgr.getResponseFormat().toString()); + docMgr.setNonDocumentFormat(Format.XML); + assertEquals("format set on document manager","XML",docMgr.getNonDocumentFormat().toString()); assertEquals("Queryview set on document manager ","RESULTS" ,docMgr.getSearchView().toString()); assertEquals("Page length ",1,docMgr.getPageLength()); // Search for documents where content has bar and get first result record, get search handle on it @@ -211,7 +211,7 @@ public void testBulkSearchSQDwithResponseFormatandStringHandle() throws Exceptio StructuredQueryDefinition qd = qb.and(qb.term("dog1","dog11")); queryMgr.search(qd, new SearchHandle()); - docMgr.setResponseFormat(Format.JSON); + docMgr.setNonDocumentFormat(Format.JSON); docMgr.setSearchView(QueryView.METADATA); docMgr.setMetadataCategories(Metadata.PERMISSIONS); @@ -239,7 +239,7 @@ public void testBulkSearchSQDwithJSONResponseFormat() throws Exception{ QueryManager queryMgr = client.newQueryManager(); StructuredQueryBuilder qb = new StructuredQueryBuilder(); StructuredQueryDefinition qd = qb.and(qb.term("woof")); - docMgr.setResponseFormat(Format.JSON); + docMgr.setNonDocumentFormat(Format.JSON); docMgr.setSearchView(QueryView.FACETS); JacksonHandle jh = new JacksonHandle(); @@ -409,7 +409,7 @@ public void testBulkSearchRawJSONStrucQD() throws Exception{ RawStructuredQueryDefinition qd = queryMgr.newRawStructuredQueryDefinition(jh); System.out.println(jh.get().toString()); - docMgr.setResponseFormat(Format.JSON); + docMgr.setNonDocumentFormat(Format.JSON); JacksonHandle results = new JacksonHandle(); DocumentPage page= docMgr.search(qd, 1,results); DocumentMetadataHandle mh = new DocumentMetadataHandle(); From c804e9daf86ce2f96ed015455b464694cafa03d4 Mon Sep 17 00:00:00 2001 From: Charles Greer Date: Wed, 1 Oct 2014 14:46:39 -0700 Subject: [PATCH 152/357] Error Format Fix for UberServer Change. Issue 94 --- .../admin/ServerConfigurationManager.java | 15 ------- .../marklogic/client/impl/JerseyServices.java | 2 +- .../impl/ServerConfigurationManagerImpl.java | 23 +---------- .../marklogic/client/io/JSONErrorParser.java | 4 +- .../client/test/FailedRequestTest.java | 41 +------------------ 5 files changed, 5 insertions(+), 80 deletions(-) diff --git a/src/main/java/com/marklogic/client/admin/ServerConfigurationManager.java b/src/main/java/com/marklogic/client/admin/ServerConfigurationManager.java index e2d7dd7e0..42d079f4f 100644 --- a/src/main/java/com/marklogic/client/admin/ServerConfigurationManager.java +++ b/src/main/java/com/marklogic/client/admin/ServerConfigurationManager.java @@ -19,7 +19,6 @@ import com.marklogic.client.ForbiddenUserException; import com.marklogic.client.ResourceNotFoundException; import com.marklogic.client.ResourceNotResendableException; -import com.marklogic.client.io.Format; /** * The ServerConfigurationManager reads and writes the configurable properties @@ -194,20 +193,6 @@ public void writeConfiguration() * @return a new manager for transform extensions */ public TransformExtensionsManager newTransformExtensionsManager(); - /** - * Sets the serialization format of MarkLogic errors. - * The Java API only parses errors reported in XML. JavaScript clients generally use JSON. - * While one REST server cannot support both kinds of clients, - * two REST servers can share one database. - * @param errorFormat either Format.JSON or Format.XML - */ - public void setErrorFormat(Format errorFormat); - /** - * Returns the configured error format for the REST server instance. Transparent to the Java API itself. - * @return either Format.JSON or Format.XML - */ - public Format getErrorFormat(); - /** * Creates a manager for writing and reading assets in the REST * server's modules database. diff --git a/src/main/java/com/marklogic/client/impl/JerseyServices.java b/src/main/java/com/marklogic/client/impl/JerseyServices.java index a0a0f796b..b7be7e392 100644 --- a/src/main/java/com/marklogic/client/impl/JerseyServices.java +++ b/src/main/java/com/marklogic/client/impl/JerseyServices.java @@ -126,7 +126,7 @@ public class JerseyServices implements RESTServices { static final private Logger logger = LoggerFactory .getLogger(JerseyServices.class); - static final String ERROR_NS = "http://marklogic.com/rest-api"; + static final String ERROR_NS = "http://marklogic.com/xdmp/error"; static final private String DOCUMENT_URI_PREFIX = "/documents?uri="; diff --git a/src/main/java/com/marklogic/client/impl/ServerConfigurationManagerImpl.java b/src/main/java/com/marklogic/client/impl/ServerConfigurationManagerImpl.java index db308f27a..217c531fb 100644 --- a/src/main/java/com/marklogic/client/impl/ServerConfigurationManagerImpl.java +++ b/src/main/java/com/marklogic/client/impl/ServerConfigurationManagerImpl.java @@ -28,19 +28,18 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.marklogic.client.DatabaseClientFactory.HandleFactoryRegistry; import com.marklogic.client.FailedRequestException; import com.marklogic.client.ForbiddenUserException; import com.marklogic.client.MarkLogicInternalException; import com.marklogic.client.ResourceNotFoundException; import com.marklogic.client.ResourceNotResendableException; -import com.marklogic.client.DatabaseClientFactory.HandleFactoryRegistry; import com.marklogic.client.admin.ExtensionLibrariesManager; import com.marklogic.client.admin.NamespacesManager; import com.marklogic.client.admin.QueryOptionsManager; import com.marklogic.client.admin.ResourceExtensionsManager; import com.marklogic.client.admin.ServerConfigurationManager; import com.marklogic.client.admin.TransformExtensionsManager; -import com.marklogic.client.io.Format; import com.marklogic.client.io.OutputStreamHandle; import com.marklogic.client.io.OutputStreamSender; @@ -58,7 +57,6 @@ class ServerConfigurationManagerImpl private Boolean serverRequestLogging; private Policy contentVersions; private UpdatePolicy updatePolicy; - private Format errorFormat; private RESTServices services; private HandleFactoryRegistry handleRegistry; @@ -120,8 +118,6 @@ public void readConfiguration() } else if ("update-policy".equals(localName)) { updatePolicy = Enum.valueOf(UpdatePolicy.class, reader.getElementText().toUpperCase().replace("-", "_")); - } else if ("error-format".equals(localName)) { - errorFormat = Format.valueOf(reader.getElementText().toUpperCase()); } } @@ -194,11 +190,6 @@ public void write(OutputStream out) throws IOException { serializer.writeCharacters(updatePolicy.name().toLowerCase().replace("_", "-")); serializer.writeEndElement(); } - if (errorFormat != null) { - serializer.writeStartElement(REST_API_NS, "error-format"); - serializer.writeCharacters(errorFormat.name().toLowerCase()); - serializer.writeEndElement(); - } // serializer.writeEndElement(); @@ -323,16 +314,4 @@ public TransformExtensionsManager newTransformExtensionsManager() { transformExtensionMgr.setHandleRegistry(getHandleRegistry()); return transformExtensionMgr; } - - @Override - public Format getErrorFormat() { - return errorFormat; - } - @Override - public void setErrorFormat(Format errorFormat) { - if (errorFormat == Format.JSON || errorFormat == Format.XML) - this.errorFormat = errorFormat; - else - throw new IllegalArgumentException("The only supported values for error format are JSON and XML."); - } } diff --git a/src/main/java/com/marklogic/client/io/JSONErrorParser.java b/src/main/java/com/marklogic/client/io/JSONErrorParser.java index 8a4da6621..237dffb12 100644 --- a/src/main/java/com/marklogic/client/io/JSONErrorParser.java +++ b/src/main/java/com/marklogic/client/io/JSONErrorParser.java @@ -24,10 +24,10 @@ public FailedRequest parseFailedRequest(int httpStatus, InputStream content) { Map> errorData; try { errorData = mapper.readValue(content, Map.class); - Map errorBody = errorData.get("error"); + Map errorBody = errorData.get("errorResponse"); failure.setStatusCode(httpStatus); failure.setStatusString(errorBody.get("status")); - failure.setMessageCode(errorBody.get("message-code")); + failure.setMessageCode(errorBody.get("messageCode")); failure.setMessageString(errorBody.get("message")); } catch (JsonParseException e1) { failure.setStatusCode(httpStatus); diff --git a/src/test/java/com/marklogic/client/test/FailedRequestTest.java b/src/test/java/com/marklogic/client/test/FailedRequestTest.java index aea5b3769..0ba6df4f9 100644 --- a/src/test/java/com/marklogic/client/test/FailedRequestTest.java +++ b/src/test/java/com/marklogic/client/test/FailedRequestTest.java @@ -39,23 +39,6 @@ @SuppressWarnings("deprecation") public class FailedRequestTest { - @Before - @After - public void setXMLErrors() - throws FailedRequestException, ResourceNotFoundException, ResourceNotResendableException, ForbiddenUserException { - - Common.connectAdmin(); - ServerConfigurationManager serverConfig = Common.client - .newServerConfigManager(); - serverConfig = Common.client.newServerConfigManager(); - - serverConfig.setErrorFormat(Format.XML); - serverConfig.writeConfiguration(); - - serverConfig.readConfiguration(); - - } - @Test public void testFailedRequest() throws FailedRequestException, ForbiddenUserException, ResourceNotFoundException, ResourceNotResendableException { @@ -67,7 +50,7 @@ public void testFailedRequest() mgr.writeOptions("testempty", new QueryOptionsHandle()); } catch (ForbiddenUserException e) { assertEquals( - "Local message: User is not allowed to write /config/query. Server Message: You do not have permission to this method and URL", + "Local message: User is not allowed to write /config/query. Server Message: You do not have permission to this method and URL.", e.getMessage()); assertEquals(403, e.getFailedRequest().getStatusCode()); assertEquals("Forbidden", e.getFailedRequest().getStatus()); @@ -100,28 +83,6 @@ public void testFailedRequest() } - @Test - public void testJSONFailedRequest() - throws FailedRequestException, ForbiddenUserException, ResourceNotFoundException, ResourceNotResendableException { - Common.connectAdmin(); - ServerConfigurationManager serverConfig = Common.client - .newServerConfigManager(); - - serverConfig.setErrorFormat(Format.JSON); - serverConfig.writeConfiguration(); - - serverConfig.readConfiguration(); - assertEquals(Format.JSON, serverConfig.getErrorFormat()); - - try { - serverConfig.setErrorFormat(Format.BINARY); - fail("Error format cannot be binary"); - } catch (IllegalArgumentException e) { - // pass - } - - testFailedRequest(); - } @Test public void testErrorOnNonREST() From 7068cde11064e19c61476e4bb46b19246fffea49 Mon Sep 17 00:00:00 2001 From: sammefford Date: Fri, 3 Oct 2014 12:53:52 -0600 Subject: [PATCH 153/357] #67, initial working implementation of eval ad-hoc queries (not invoke) --- .../com/marklogic/client/DatabaseClient.java | 4 +- .../com/marklogic/client/eval/EvalResult.java | 47 +-- ...alResults.java => EvalResultIterator.java} | 3 +- ...Manager.java => ServerEvaluationCall.java} | 30 +- .../client/impl/DatabaseClientImpl.java | 7 +- .../marklogic/client/impl/HandleAccessor.java | 48 +++ .../marklogic/client/impl/JerseyServices.java | 293 +++++++++++++- .../marklogic/client/impl/RESTServices.java | 10 +- .../client/impl/ServerEvaluationCallImpl.java | 158 ++++++++ .../java/com/marklogic/client/io/Format.java | 11 + .../com/marklogic/client/test/EvalTest.java | 359 +++++++++++++----- .../client/test/HandleAccessorTest.java | 52 +++ src/test/resources/hola.txt | 1 + 13 files changed, 864 insertions(+), 159 deletions(-) rename src/main/java/com/marklogic/client/eval/{EvalResults.java => EvalResultIterator.java} (87%) rename src/main/java/com/marklogic/client/eval/{ServerEvaluationManager.java => ServerEvaluationCall.java} (52%) create mode 100644 src/main/java/com/marklogic/client/impl/ServerEvaluationCallImpl.java create mode 100644 src/test/java/com/marklogic/client/test/HandleAccessorTest.java create mode 100644 src/test/resources/hola.txt diff --git a/src/main/java/com/marklogic/client/DatabaseClient.java b/src/main/java/com/marklogic/client/DatabaseClient.java index dd0355f66..9b4294d24 100644 --- a/src/main/java/com/marklogic/client/DatabaseClient.java +++ b/src/main/java/com/marklogic/client/DatabaseClient.java @@ -24,7 +24,7 @@ import com.marklogic.client.document.JSONDocumentManager; import com.marklogic.client.document.TextDocumentManager; import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.eval.ServerEvaluationManager; +import com.marklogic.client.eval.ServerEvaluationCall; import com.marklogic.client.extensions.ResourceManager; import com.marklogic.client.admin.ExtensionMetadata; import com.marklogic.client.query.QueryManager; @@ -178,5 +178,5 @@ public T init(String resourceName, T resourceManager * @return the object implementing communication with the server */ public Object getClientImplementation(); - public ServerEvaluationManager newServerEval(); + public ServerEvaluationCall newServerEval(); } diff --git a/src/main/java/com/marklogic/client/eval/EvalResult.java b/src/main/java/com/marklogic/client/eval/EvalResult.java index 22012f280..509c1d7d4 100644 --- a/src/main/java/com/marklogic/client/eval/EvalResult.java +++ b/src/main/java/com/marklogic/client/eval/EvalResult.java @@ -15,53 +15,22 @@ */ package com.marklogic.client.eval; +import com.marklogic.client.io.Format; import com.marklogic.client.io.marker.AbstractReadHandle; public interface EvalResult { enum Type { - STRING, - BOOLEAN, - JS_NUMBER, - JS_ARRAY, - JS_OBJECT, - NULL, - XDM_ATOMIC, - XDM_ATTRIBUTE, - XDM_BINARY, - XDM_COMMENT, - XDM_DOCUMENT, - XDM_DURATION, - XDM_ELEMENT, - XDM_ITEM, - XDM_NODE, - XDM_PROCESSINGINSTRUCTION, - XDM_SEQUENCE, - XDM_TEXT, - XDM_VALUE, - XDM_VARIABLE, - XS_ANYURI, - XS_BASE64BINARY, - XS_DATE, - XS_DATETIME, - XS_DAYTIMEDURATION, - XS_DECIMAL, - XS_DOUBLE, - XS_DURATION, - XS_FLOAT, - XS_GDAY, - XS_GMONTH, - XS_GMONTHDAY, - XS_GYEAR, - XS_GYEARMONTH, - XS_HEXBINARY, - XS_INTEGER, - XS_QNAME, - XS_TIME + XML, JSON, + STRING, BOOLEAN, NULL, OTHER, + ANYURI, BASE64BINARY, DATE, DATETIME, DECIMAL, DOUBLE, DURATION, + FLOAT, GDAY, GMONTH, GMONTHDAY, GYEAR, GYEARMONTH, HEXBINARY, INTEGER, QNAME, TIME, + ATTRIBUTE, BINARY, COMMENT, PROCESSINGINSTRUCTION, TEXTNODE }; public Type getType(); + public Format getFormat(); public H get(H handle); public T getAs(Class clazz); public String getString(); public Number getNumber(); - public boolean getBoolean(); + public Boolean getBoolean(); } diff --git a/src/main/java/com/marklogic/client/eval/EvalResults.java b/src/main/java/com/marklogic/client/eval/EvalResultIterator.java similarity index 87% rename from src/main/java/com/marklogic/client/eval/EvalResults.java rename to src/main/java/com/marklogic/client/eval/EvalResultIterator.java index 9106cece1..ac058d050 100644 --- a/src/main/java/com/marklogic/client/eval/EvalResults.java +++ b/src/main/java/com/marklogic/client/eval/EvalResultIterator.java @@ -17,8 +17,9 @@ import java.util.Iterator; -public interface EvalResults extends Iterable, Iterator { +public interface EvalResultIterator extends Iterable, Iterator { public Iterator iterator(); public boolean hasNext(); public EvalResult next(); + public void close(); } diff --git a/src/main/java/com/marklogic/client/eval/ServerEvaluationManager.java b/src/main/java/com/marklogic/client/eval/ServerEvaluationCall.java similarity index 52% rename from src/main/java/com/marklogic/client/eval/ServerEvaluationManager.java rename to src/main/java/com/marklogic/client/eval/ServerEvaluationCall.java index d30d0ca03..58cf732bb 100644 --- a/src/main/java/com/marklogic/client/eval/ServerEvaluationManager.java +++ b/src/main/java/com/marklogic/client/eval/ServerEvaluationCall.java @@ -19,24 +19,24 @@ import com.marklogic.client.io.marker.AbstractReadHandle; import com.marklogic.client.io.marker.AbstractWriteHandle; import com.marklogic.client.io.marker.TextWriteHandle; +import com.marklogic.client.util.EditableNamespaceContext; -public interface ServerEvaluationManager { - public ServerEvaluationManager xquery(String xquery); - public ServerEvaluationManager xquery(TextWriteHandle xquery); - public ServerEvaluationManager javascript(String javascript); - public ServerEvaluationManager javascript(TextWriteHandle javascript); - public ServerEvaluationManager xqueryModule(String modulePath); - public ServerEvaluationManager javascriptModule(String modulePath); - public ServerEvaluationManager addVariable(String name, String value); - public ServerEvaluationManager addVariable(String name, Number value); - public ServerEvaluationManager addVariable(String name, Boolean value); - public ServerEvaluationManager addVariable(String name, AbstractWriteHandle value); +public interface ServerEvaluationCall { + public ServerEvaluationCall xquery(String xquery); + public ServerEvaluationCall javascript(String javascript); + public ServerEvaluationCall modulePath(String modulePath); + public ServerEvaluationCall addVariable(String name, String value); + public ServerEvaluationCall addVariable(String name, Number value); + public ServerEvaluationCall addVariable(String name, Boolean value); + public ServerEvaluationCall addVariable(String name, AbstractWriteHandle value); /** Like other *As convenience methods throughout the API, the Object value * is managed by the Handle registered for that Class. */ - public ServerEvaluationManager addVariableAs(String name, Object value); - public ServerEvaluationManager database(String database); - public ServerEvaluationManager transaction(Transaction transaction); + public ServerEvaluationCall addVariableAs(String name, Object value); + public ServerEvaluationCall database(String database); + public ServerEvaluationCall transaction(Transaction transaction); + public ServerEvaluationCall addNamespace(String prefix, String namespaceURI); + public ServerEvaluationCall namespaceContext(EditableNamespaceContext namespaces); public T evalAs(Class responseType); public H eval(H responseHandle); - public EvalResults eval(); + public EvalResultIterator eval(); } diff --git a/src/main/java/com/marklogic/client/impl/DatabaseClientImpl.java b/src/main/java/com/marklogic/client/impl/DatabaseClientImpl.java index e7e8bd516..40b71fd8f 100644 --- a/src/main/java/com/marklogic/client/impl/DatabaseClientImpl.java +++ b/src/main/java/com/marklogic/client/impl/DatabaseClientImpl.java @@ -30,7 +30,7 @@ import com.marklogic.client.document.JSONDocumentManager; import com.marklogic.client.query.QueryManager; import com.marklogic.client.util.RequestLogger; -import com.marklogic.client.eval.ServerEvaluationManager; +import com.marklogic.client.eval.ServerEvaluationCall; import com.marklogic.client.extensions.ResourceManager; import com.marklogic.client.DatabaseClientFactory.HandleFactoryRegistry; import com.marklogic.client.admin.ServerConfigurationManager; @@ -185,8 +185,7 @@ public RESTServices getServices() { } @Override - public ServerEvaluationManager newServerEval() { - // TODO Auto-generated method stub - return null; + public ServerEvaluationCall newServerEval() { + return new ServerEvaluationCallImpl(services, getHandleRegistry()); } } diff --git a/src/main/java/com/marklogic/client/impl/HandleAccessor.java b/src/main/java/com/marklogic/client/impl/HandleAccessor.java index e2f2faaea..621cf0290 100644 --- a/src/main/java/com/marklogic/client/impl/HandleAccessor.java +++ b/src/main/java/com/marklogic/client/impl/HandleAccessor.java @@ -15,7 +15,17 @@ */ package com.marklogic.client.impl; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.io.UnsupportedEncodingException; + +import com.marklogic.client.MarkLogicIOException; import com.marklogic.client.io.BaseHandle; +import com.marklogic.client.io.OutputStreamSender; import com.marklogic.client.io.marker.AbstractReadHandle; import com.marklogic.client.io.marker.AbstractWriteHandle; @@ -59,4 +69,42 @@ static public boolean isResendable(W handle) { static public HandleImplementation as(Object handle) { return ((HandleImplementation) handle); } + static public String contentAsString(AbstractWriteHandle handle) { + try { + Object content = sendContent(handle); + if ( content == null ) return null; + String stringContent = null; + if (content instanceof String) { + stringContent = (String) content; + } else if (content instanceof OutputStreamSender) { + ByteArrayOutputStream bytesStream = new ByteArrayOutputStream(); + ((OutputStreamSender) content).write(bytesStream); + stringContent = bytesStream.toString("UTF-8"); + } else if ( content instanceof byte[] ) { + stringContent = new String((byte[]) content, "UTF-8"); + } else if ( content instanceof File ) { + content = new FileInputStream((File) content); + } + if ( content instanceof InputStream ) { + StringBuffer sb = new StringBuffer(); + Reader reader = new InputStreamReader((InputStream) content, "UTF-8"); + char[] cbuf = new char[8000]; + int charsRead = -1; + while ( (charsRead = reader.read(cbuf)) != -1 ) { + sb.append(cbuf, 0, charsRead); + } + stringContent = sb.toString(); + } + if ( content instanceof File ) { + ((FileInputStream) content).close(); + } + if ( stringContent == null ) { + throw new UnsupportedOperationException("contentAsString only supports handles with sendContent() " + + "of type String, OutputStreamSender, byte[], File, or InputStream"); + } + return stringContent; + } catch (Exception e) { + throw new MarkLogicIOException(e); + } + } } diff --git a/src/main/java/com/marklogic/client/impl/JerseyServices.java b/src/main/java/com/marklogic/client/impl/JerseyServices.java index b7be7e392..40e83013f 100644 --- a/src/main/java/com/marklogic/client/impl/JerseyServices.java +++ b/src/main/java/com/marklogic/client/impl/JerseyServices.java @@ -21,6 +21,9 @@ import java.io.InputStream; import java.io.PrintStream; import java.io.Reader; +import java.io.UnsupportedEncodingException; +import java.math.BigDecimal; +import java.net.URLEncoder; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; @@ -62,11 +65,16 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.marklogic.client.DatabaseClientFactory; import com.marklogic.client.DatabaseClientFactory.Authentication; import com.marklogic.client.DatabaseClientFactory.SSLHostnameVerifier; import com.marklogic.client.DatabaseClient; import com.marklogic.client.FailedRequestException; import com.marklogic.client.ForbiddenUserException; +import com.marklogic.client.MarkLogicIOException; import com.marklogic.client.MarkLogicInternalException; import com.marklogic.client.ResourceNotFoundException; import com.marklogic.client.ResourceNotResendableException; @@ -79,12 +87,19 @@ import com.marklogic.client.document.DocumentWriteSet; import com.marklogic.client.document.DocumentUriTemplate; import com.marklogic.client.document.ServerTransform; +import com.marklogic.client.eval.EvalResult; +import com.marklogic.client.eval.EvalResultIterator; +import com.marklogic.client.eval.ServerEvaluationCall; import com.marklogic.client.extensions.ResourceServices.ServiceResult; import com.marklogic.client.extensions.ResourceServices.ServiceResultIterator; import com.marklogic.client.io.Format; +import com.marklogic.client.io.JacksonParserHandle; import com.marklogic.client.io.OutputStreamSender; +import com.marklogic.client.io.JacksonHandle; +import com.marklogic.client.io.StringHandle; import com.marklogic.client.io.marker.AbstractReadHandle; import com.marklogic.client.io.marker.AbstractWriteHandle; +import com.marklogic.client.io.marker.ContentHandle; import com.marklogic.client.io.marker.DocumentMetadataReadHandle; import com.marklogic.client.io.marker.DocumentMetadataWriteHandle; import com.marklogic.client.io.marker.DocumentPatchHandle; @@ -105,6 +120,7 @@ import com.marklogic.client.query.ValueQueryDefinition; import com.marklogic.client.query.ValuesDefinition; import com.marklogic.client.query.ValuesListDefinition; +import com.marklogic.client.util.EditableNamespaceContext; import com.marklogic.client.util.RequestLogger; import com.marklogic.client.util.RequestParameters; import com.sun.jersey.api.client.ClientResponse; @@ -1789,19 +1805,22 @@ private Format getHeaderFormat(MultivaluedMap headers) { } private Format getHeaderFormat(BodyPart part) { + ContentDisposition contentDisposition = part.getContentDisposition(); if (part.getHeaders().containsKey("vnd.marklogic.document-format")) { - List values = part.getHeaders().get("vnd.marklogic.document-format"); - if (values != null) { - return Format.valueOf(values.get(0).toUpperCase()); + String value = part.getHeaders().getFirst("vnd.marklogic.document-format"); + if (value != null) { + return Format.valueOf(value.toUpperCase()); } - } else { - ContentDisposition contentDisposition = part.getContentDisposition(); - if ( contentDisposition != null ) { - Map parameters = contentDisposition.getParameters(); - if ( parameters != null && parameters.get("format") != null ) { - return Format.valueOf(parameters.get("format").toUpperCase()); - } + } else if ( contentDisposition != null ) { + Map parameters = contentDisposition.getParameters(); + if ( parameters != null && parameters.get("format") != null ) { + return Format.valueOf(parameters.get("format").toUpperCase()); } + } else if ( part.getHeaders().containsKey("Content-Type") ) { + String value = part.getHeaders().getFirst("Content-Type"); + if (value != null) { + return Format.getFromMimetype(value); + } } return null; } @@ -3544,6 +3563,241 @@ public R postBulkDocuments( output); } + public class JerseyEvalResultIterator implements EvalResultIterator { + private JerseyResultIterator iterator; + + JerseyEvalResultIterator(JerseyResultIterator iterator) { + this.iterator = iterator; + } + + @Override + public Iterator iterator() { + return this; + } + + @Override + public boolean hasNext() { + if ( iterator == null ) return false; + return iterator.hasNext(); + } + + public void remove() { + throw new UnsupportedOperationException(); + } + + @Override + public EvalResult next() { + if ( iterator == null ) throw new NoSuchElementException("No results available"); + JerseyResult jerseyResult = iterator.next(); + EvalResult result = new JerseyEvalResult(jerseyResult); + return result; + } + + public void close() { + if ( iterator != null ) iterator.close(); + } + } + public class JerseyEvalResult implements EvalResult { + private JerseyResult content; + + public JerseyEvalResult(JerseyResult content) { + this.content = content; + } + + @Override + public Format getFormat() { + return content.getFormat(); + } + + @Override + public EvalResult.Type getType() { + String contentType = content.getHeader("Content-Type"); + if ( contentType != null ) { + if ( "application/json".equals(contentType) ) { + return EvalResult.Type.JSON; + } else if ( "text/json".equals(contentType) ) { + return EvalResult.Type.JSON; + } else if ( "application/xml".equals(contentType) ) { + return EvalResult.Type.XML; + } else if ( "text/xml".equals(contentType) ) { + return EvalResult.Type.XML; + } + } + String xPrimitive = content.getHeader("X-Primitive"); + if ( xPrimitive == null ) { + return EvalResult.Type.NULL; + } else if ( "string".equals(xPrimitive) || "untypedAtomic".equals(xPrimitive) ) { + return EvalResult.Type.STRING; + } else if ( "boolean".equals(xPrimitive) ) { + return EvalResult.Type.BOOLEAN; + } else if ( "node()".equals(xPrimitive) ) { + String xPath = content.getHeader("X-Path"); + if ( xPath == null ) { + return EvalResult.Type.OTHER; + } else if ( xPath.endsWith("comment()") ) { + return EvalResult.Type.COMMENT; + } else if ( xPath.endsWith("processing-instruction()") ) { + return EvalResult.Type.PROCESSINGINSTRUCTION; + } else if ( content.getHeader("X-Attr") != null ) { + return EvalResult.Type.ATTRIBUTE; + } + } else if ( "text()".equals(xPrimitive) ) { + return EvalResult.Type.TEXTNODE; + } else if ( "binary()".equals(xPrimitive) ) { + return EvalResult.Type.BINARY; + } else if ( "duration".equals(xPrimitive) ) { + return EvalResult.Type.DURATION; + } else if ( "date".equals(xPrimitive) ) { + return EvalResult.Type.DATE; + } else if ( "anyURI".equals(xPrimitive) ) { + return EvalResult.Type.ANYURI; + } else if ( "hexBinary".equals(xPrimitive) ) { + return EvalResult.Type.HEXBINARY; + } else if ( "base64Binary".equals(xPrimitive) ) { + return EvalResult.Type.BASE64BINARY; + } else if ( "dateTime".equals(xPrimitive) ) { + return EvalResult.Type.DATETIME; + } else if ( "decimal".equals(xPrimitive) ) { + return EvalResult.Type.DECIMAL; + } else if ( "double".equals(xPrimitive) ) { + return EvalResult.Type.DOUBLE; + } else if ( "float".equals(xPrimitive) ) { + return EvalResult.Type.FLOAT; + } else if ( "gDay".equals(xPrimitive) ) { + return EvalResult.Type.GDAY; + } else if ( "gMonth".equals(xPrimitive) ) { + return EvalResult.Type.GMONTH; + } else if ( "gMonthDay".equals(xPrimitive) ) { + return EvalResult.Type.GMONTHDAY; + } else if ( "gYear".equals(xPrimitive) ) { + return EvalResult.Type.GYEAR; + } else if ( "gYearMonth".equals(xPrimitive) ) { + return EvalResult.Type.GYEARMONTH; + } else if ( "integer".equals(xPrimitive) ) { + return EvalResult.Type.INTEGER; + } else if ( "QName".equals(xPrimitive) ) { + return EvalResult.Type.QNAME; + } else if ( "time".equals(xPrimitive) ) { + return EvalResult.Type.TIME; + } + return EvalResult.Type.OTHER; + } + + @Override + public H get(H handle) { + return content.getContent(handle); + } + + @Override + public T getAs(Class clazz) { + if (clazz == null) throw new IllegalArgumentException("clazz cannot be null"); + + ContentHandle readHandle = DatabaseClientFactory.getHandleRegistry().makeHandle(clazz); + if ( readHandle == null ) return null; + readHandle = get(readHandle); + if ( readHandle == null ) return null; + return readHandle.get(); + } + + @Override + public String getString() { + return content.getEntityAs(String.class); + } + + @Override + public Number getNumber() { + if ( getType() == EvalResult.Type.DECIMAL ) return new BigDecimal(getString()); + else if ( getType() == EvalResult.Type.DOUBLE ) return new Double(getString()); + else if ( getType() == EvalResult.Type.FLOAT ) return new Float(getString()); + // MarkLogic integers can be much larger than Java integers, so we'll use Long instead + else if ( getType() == EvalResult.Type.INTEGER ) return new Long(getString()); + else return new BigDecimal(getString()); + } + + @Override + public Boolean getBoolean() { + return new Boolean(getString()); + } + + } + + @Override + public EvalResultIterator postEvalInvoke( + RequestLogger reqlog, String code, String modulePath, + ServerEvaluationCallImpl.Context context, + Map variables, EditableNamespaceContext namespaces, + String database, String transactionId) + throws ResourceNotFoundException, ResourceNotResendableException, + ForbiddenUserException, FailedRequestException { + String formUrlEncodedPayload; + String path; + RequestParameters params = new RequestParameters(); + try { + StringBuffer sb = new StringBuffer(); + if ( context == ServerEvaluationCallImpl.Context.ADHOC_XQUERY ) { + path = "eval"; + sb.append("xquery="); + sb.append(URLEncoder.encode(code, "UTF-8")); + } else if ( context == ServerEvaluationCallImpl.Context.ADHOC_JAVASCRIPT ) { + path = "eval"; + sb.append("javascript="); + sb.append(URLEncoder.encode(code, "UTF-8")); + } else if ( context == ServerEvaluationCallImpl.Context.INVOKE ) { + path = "invoke"; + params.put("module", modulePath); + } else { + throw new IllegalStateException("Invalid eval context: " + context); + } + if ( variables != null && variables.size() > 0 ) { + sb.append("&vars="); + ObjectNode vars = new ObjectMapper().createObjectNode(); + for ( String name : variables.keySet() ) { + Object valueObject = variables.get(name); + // replace any name starting with a namespace prefix with a Clark Notation QName + if ( namespaces != null ) { + for ( String prefix : namespaces.keySet() ) { + if ( name != null && prefix != null && + name.startsWith(prefix + ":") ) + { + name = "{" + namespaces.get(prefix) + "}" + + name.substring(prefix.length() + 1); + } + } + } + if ( valueObject == null ) vars.putNull(name); + else if ( valueObject instanceof BigDecimal ) vars.put(name, (BigDecimal) valueObject); + else if ( valueObject instanceof Double ) vars.put(name, (Double) valueObject); + else if ( valueObject instanceof Float ) vars.put(name, (Float) valueObject); + else if ( valueObject instanceof Integer ) vars.put(name, (Integer) valueObject); + else if ( valueObject instanceof Long ) vars.put(name, (Long) valueObject); + else if ( valueObject instanceof Short ) vars.put(name, (Short) valueObject); + else if ( valueObject instanceof Number ) vars.put(name, new BigDecimal(valueObject.toString())); + else if ( valueObject instanceof Boolean ) vars.put(name, (Boolean) valueObject); + else if ( valueObject instanceof String ) vars.put(name, (String) valueObject); + else if ( valueObject instanceof JacksonHandle ) { + vars.set(name, ((JacksonHandle) valueObject).get()); + } else if ( valueObject instanceof JacksonParserHandle ) { + vars.set(name, ((JacksonParserHandle) valueObject).get().readValueAs(JsonNode.class)); + } else if ( valueObject instanceof AbstractWriteHandle ) { + vars.put(name, HandleAccessor.contentAsString((AbstractWriteHandle) valueObject)); + } + } + sb.append(URLEncoder.encode(vars.toString(), "UTF-8")); + } + formUrlEncodedPayload = sb.toString(); + } catch (UnsupportedEncodingException e) { + throw new IllegalStateException("UTF-8 is unsupported", e); + } catch (IOException e) { + throw new MarkLogicIOException(e); + } + if ( database != null ) params.add("database", database); + if ( transactionId != null ) params.add("txid", transactionId); + StringHandle input = new StringHandle(formUrlEncodedPayload) + .withMimetype("application/x-www-form-urlencoded"); + return new JerseyEvalResultIterator( postIteratedResourceImpl(DefaultJerseyResultIterator.class, + reqlog, path, params, input) ); + } + @Override public ServiceResultIterator postIteratedResource(RequestLogger reqlog, String path, RequestParameters params, AbstractWriteHandle input, @@ -4259,6 +4513,7 @@ public class JerseyResult { private RequestLogger reqlog; private BodyPart part; private boolean extractedHeaders = false; + private MultivaluedMap headers = null; private String uri; private Format format; private String mimetype; @@ -4269,6 +4524,10 @@ public JerseyResult(RequestLogger reqlog, BodyPart part) { this.part = part; } + public T getEntityAs(Class clazz) { + return part.getEntityAs(clazz); + } + public R getContent(R handle) { if (part == null) throw new IllegalStateException("Content already retrieved"); @@ -4290,6 +4549,13 @@ public R getContent(R handle) { return handle; } + public T getContentAs(Class clazz) { + ContentHandle readHandle = DatabaseClientFactory.getHandleRegistry().makeHandle(clazz); + readHandle = getContent(readHandle); + if ( readHandle == null ) return null; + return readHandle.get(); + } + public String getUri() { extractHeaders(); return uri; @@ -4309,10 +4575,15 @@ public long getLength() { return length; } + public String getHeader(String name) { + extractHeaders(); + return headers.getFirst(name); + } + private void extractHeaders() { if (part == null || extractedHeaders) return; - MultivaluedMap headers = part.getHeaders(); + headers = part.getHeaders(); format = getHeaderFormat(part); mimetype = getHeaderMimetype(headers); length = getHeaderLength(headers); diff --git a/src/main/java/com/marklogic/client/impl/RESTServices.java b/src/main/java/com/marklogic/client/impl/RESTServices.java index 030382e62..f692d05d1 100644 --- a/src/main/java/com/marklogic/client/impl/RESTServices.java +++ b/src/main/java/com/marklogic/client/impl/RESTServices.java @@ -35,7 +35,10 @@ import com.marklogic.client.document.DocumentManager.Metadata; import com.marklogic.client.document.DocumentWriteSet; import com.marklogic.client.document.ServerTransform; +import com.marklogic.client.eval.EvalResultIterator; +import com.marklogic.client.eval.ServerEvaluationCall; import com.marklogic.client.extensions.ResourceServices.ServiceResultIterator; +import com.marklogic.client.impl.ServerEvaluationCallImpl.Context; import com.marklogic.client.io.Format; import com.marklogic.client.io.marker.AbstractReadHandle; import com.marklogic.client.io.marker.AbstractWriteHandle; @@ -50,6 +53,7 @@ import com.marklogic.client.query.SuggestDefinition; import com.marklogic.client.query.ValuesDefinition; import com.marklogic.client.query.ValuesListDefinition; +import com.marklogic.client.util.EditableNamespaceContext; import com.marklogic.client.util.RequestLogger; import com.marklogic.client.util.RequestParameters; import com.sun.jersey.api.client.ClientResponse; @@ -200,7 +204,11 @@ public ServiceResultIterator postIteratedResourc W[] input, String... outputMimetypes) throws ResourceNotFoundException, ResourceNotResendableException, ForbiddenUserException, FailedRequestException; - + public EvalResultIterator postEvalInvoke(RequestLogger reqlog, String code, String modulePath, + ServerEvaluationCallImpl.Context evalContext, Map variables, + EditableNamespaceContext namespaces, String database, String transactionId) + throws ResourceNotFoundException, ResourceNotResendableException, ForbiddenUserException, + FailedRequestException; public R deleteResource( RequestLogger reqlog, String path, RequestParameters params, R output) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException; diff --git a/src/main/java/com/marklogic/client/impl/ServerEvaluationCallImpl.java b/src/main/java/com/marklogic/client/impl/ServerEvaluationCallImpl.java new file mode 100644 index 000000000..1b496f649 --- /dev/null +++ b/src/main/java/com/marklogic/client/impl/ServerEvaluationCallImpl.java @@ -0,0 +1,158 @@ +/* + * Copyright 2012-2014 MarkLogic Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.marklogic.client.impl; + +import java.util.HashMap; + +import com.marklogic.client.DatabaseClientFactory.HandleFactoryRegistry; +import com.marklogic.client.Transaction; +import com.marklogic.client.eval.EvalResultIterator; +import com.marklogic.client.eval.ServerEvaluationCall; +import com.marklogic.client.io.marker.AbstractReadHandle; +import com.marklogic.client.io.marker.AbstractWriteHandle; +import com.marklogic.client.io.marker.ContentHandle; +import com.marklogic.client.io.marker.TextWriteHandle; +import com.marklogic.client.util.EditableNamespaceContext; + +public class ServerEvaluationCallImpl + extends AbstractLoggingManager + implements ServerEvaluationCall +{ + public enum Context { ADHOC_XQUERY, ADHOC_JAVASCRIPT, INVOKE }; + + private RESTServices services; + private HandleFactoryRegistry handleRegistry; + private String code; + private String modulePath; + private Context evalContext; + private String database; + private String transactionId; + private HashMap vars = new HashMap(); + private EditableNamespaceContext namespaceContext; + + public ServerEvaluationCallImpl(RESTServices services, HandleFactoryRegistry handleRegistry) { + this.services = services; + this.handleRegistry = handleRegistry; + } + + public ServerEvaluationCall xquery(String xquery) { + setContext(Context.ADHOC_XQUERY); + code = xquery; + return this; + } + + public ServerEvaluationCall javascript(String javascript) { + setContext(Context.ADHOC_JAVASCRIPT); + code = javascript; + return this; + } + + public ServerEvaluationCall modulePath(String modulePath) { + setContext(Context.INVOKE); + this.modulePath = modulePath; + return this; + } + + public ServerEvaluationCall addVariable(String name, String value) { + vars.put(name, value); + return this; + } + + public ServerEvaluationCall addVariable(String name, Number value) { + vars.put(name, value); + return this; + } + + public ServerEvaluationCall addVariable(String name, Boolean value) { + vars.put(name, value); + return this; + } + + public ServerEvaluationCall addVariable(String name, AbstractWriteHandle value) { + vars.put(name, value); + return this; + } + + /** Like other *As convenience methods throughout the API, the Object value + * is managed by the Handle registered for that Class. */ + public ServerEvaluationCall addVariableAs(String name, Object value) { + if (value == null) return this; + + Class as = value.getClass(); + AbstractWriteHandle writeHandle = null; + if (AbstractWriteHandle.class.isAssignableFrom(as)) { + writeHandle = (AbstractWriteHandle) value; + } else { + ContentHandle contentHandle = handleRegistry.makeHandle(as); + Utilities.setHandleContent(contentHandle, value); + writeHandle = contentHandle; + } + return addVariable(name, writeHandle); + } + + public ServerEvaluationCall database(String database) { + this.database = database; + return this; + } + + public ServerEvaluationCall transaction(Transaction transaction) { + if ( transaction != null ) this.transactionId = transaction.getTransactionId(); + return this; + } + + public T evalAs(Class responseType) { + if (responseType == null) throw new IllegalArgumentException("responseType cannot be null"); + + ContentHandle readHandle = handleRegistry.makeHandle(responseType); + if ( readHandle == null ) return null; + return eval(readHandle).get(); + } + + public H eval(H responseHandle) { + EvalResultIterator iterator = eval(); + if ( iterator == null || iterator.hasNext() == false ) return null; + return iterator.next().get(responseHandle); + } + + public EvalResultIterator eval() { + return services.postEvalInvoke(requestLogger, code, modulePath, evalContext, + vars, namespaceContext, database, transactionId); + } + + @Override + public ServerEvaluationCall addNamespace(String prefix, String namespaceURI) { + if ( namespaceContext == null ) namespaceContext = new EditableNamespaceContext(); + namespaceContext.put(prefix, namespaceURI); + return this; + } + + @Override + public ServerEvaluationCall namespaceContext(EditableNamespaceContext namespaces) { + this.namespaceContext = namespaces; + return this; + } + + private void setContext(Context context) { + if ( evalContext == null ) { + evalContext = context; + } else { + throw new IllegalStateException("You can only initialize the code to evaluate one time. " + + "That means only one call to the xquery, javascript, xqueryModule, or " + + "javascriptModule methods per ServerEvaluationCall."); + } + } +} + diff --git a/src/main/java/com/marklogic/client/io/Format.java b/src/main/java/com/marklogic/client/io/Format.java index 28b11d82d..4cb8d5d1e 100644 --- a/src/main/java/com/marklogic/client/io/Format.java +++ b/src/main/java/com/marklogic/client/io/Format.java @@ -62,4 +62,15 @@ public String getDefaultMimetype() { throw new MarkLogicInternalException("Unknown format "+this.toString()); } } + + public static Format getFromMimetype(String mimeType) { + if ( mimeType == null ) return UNKNOWN; + else if ( "application/xml".equals(mimeType) ) return XML; + else if ( "text/xml".equals(mimeType) ) return XML; + else if ( "application/json".equals(mimeType) ) return JSON; + else if ( "text/xml".equals(mimeType) ) return JSON; + else if ( "application/octet-stream".equals(mimeType) ) return BINARY; + else if ( mimeType.startsWith("text/") ) return TEXT; + else return UNKNOWN; + } } diff --git a/src/test/java/com/marklogic/client/test/EvalTest.java b/src/test/java/com/marklogic/client/test/EvalTest.java index e98e32931..a5a2c4026 100644 --- a/src/test/java/com/marklogic/client/test/EvalTest.java +++ b/src/test/java/com/marklogic/client/test/EvalTest.java @@ -15,15 +15,20 @@ */ package com.marklogic.client.test; +import java.io.IOException; +import java.math.BigInteger; import java.util.Calendar; import java.util.GregorianCalendar; -import java.util.SimpleTimeZone; +import java.util.TimeZone; -import javax.xml.datatype.Duration; +import javax.xml.bind.DatatypeConverter; +import javax.xml.datatype.DatatypeConfigurationException; +import javax.xml.datatype.DatatypeFactory; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertArrayEquals; import org.junit.FixMethodOrder; import org.junit.runners.MethodSorters; @@ -31,12 +36,16 @@ import org.junit.BeforeClass; import org.junit.Test; import org.w3c.dom.Document; +import org.xml.sax.SAXException; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import com.marklogic.client.eval.EvalResults; -import com.marklogic.client.eval.ServerEvaluationManager; -import com.marklogic.client.io.JAXBDatatypeHandle; +import com.marklogic.client.eval.EvalResult; +import com.marklogic.client.eval.EvalResultIterator; +import com.marklogic.client.eval.ServerEvaluationCall; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.Format; import com.marklogic.client.io.JacksonHandle; import com.marklogic.client.io.StringHandle; @@ -54,9 +63,9 @@ public static void afterClass() { } @Test - public void evalTest1() throws ParserConfigurationException { + public void evalTest1() throws ParserConfigurationException, DatatypeConfigurationException, JsonProcessingException, IOException, SAXException { // hello world and response determined by implicit StringHandle which registered with String.class - ServerEvaluationManager query = Common.client.newServerEval().xquery("'hello world'"); + ServerEvaluationCall query = Common.client.newServerEval().xquery("'hello world'"); String response = query.evalAs(String.class); assertEquals("Return should be 'hello world'", "hello world", response); @@ -69,95 +78,273 @@ public void evalTest1() throws ParserConfigurationException { assertEquals("Return should be 'hello world from Mars'", "hello world from Mars", strResponse.get()); // accept and return each JSON variable type so use MultiPartResponsePage - Calendar septFirst = new GregorianCalendar(2014, Calendar.SEPTEMBER, 1); - septFirst.setTimeZone(new SimpleTimeZone(0, "UTC")); + GregorianCalendar septFirst = new GregorianCalendar(TimeZone.getTimeZone("CET")); + septFirst.set(2014, Calendar.SEPTEMBER, 1, 0, 0, 0); + septFirst.set(Calendar.MILLISECOND, 0); query = Common.client.newServerEval() - .xquery("declare variable $myString as xs:string external;" + - "declare variable $myArray as json:array external;" + - "declare variable $myObject as json:object external;" + - "declare variable $myBool as xs:boolean external;" + - "declare variable $myInteger as xs:integer external;" + - "declare variable $myDouble as xs:double external;" + - "declare variable $myDate as xs:date external;" + - "$myString, $myArray, $myObject, $myBool, $myInteger, $myDouble, $myDate") - // String is directly supported in any EvalBuilder - .addVariable("myString", "Mars") - // ArrayNode extends JSONNode which is mapped to implicitly use JacksonHandle - .addVariableAs("myArray", new ObjectMapper().createArrayNode().add("item1").add("item2")) - // ObjectNode extends JSONNode which is mapped to implicitly use JacksonHandle - .addVariableAs("myObject", new ObjectMapper().createObjectNode().put("item1", "value1")) - // the rest are auto-boxed by EvalBuilder.addVariable(String, Number) - .addVariable("myBool", true) - .addVariable("myInteger", 123) - .addVariable("myDouble", 1.1) - .addVariableAs("myDate", septFirst); - EvalResults results = query.eval(); + .javascript("var myString;" + + "var myArray;" + + "var myObject;" + + "var myBool;" + + "var myInteger;" + + "var myDouble;" + + "var myDate;" + + "[myString, myArray, myObject, myBool, myInteger, myDouble, myDate]") + // String is directly supported in any EvalBuilder + .addVariable("myString", "Mars") + // ArrayNode extends JSONNode which is mapped to implicitly use JacksonHandle + .addVariableAs("myArray", new ObjectMapper().createArrayNode().add("item1").add("item2")) + // ObjectNode extends JSONNode which is mapped to implicitly use JacksonHandle + .addVariableAs("myObject", new ObjectMapper().createObjectNode().put("item1", "value1")) + // the rest are auto-boxed by EvalBuilder.addVariable(String, Number) + .addVariable("myBool", true) + .addVariable("myInteger", 123) + .addVariable("myDouble", 1.1) + .addVariable("myDate", + DatatypeFactory.newInstance().newXMLGregorianCalendar(septFirst).toString()); + EvalResultIterator results = query.eval(); assertEquals("myString should = 'Mars'", "Mars", results.next().getAs(String.class)); - assertEquals("myArray should = [\"item1\",\"item2\"]", "[\"item1\",\"item2\"]", - results.next().getAs(JsonNode.class)); - assertEquals("myObject should = {\"item1\"=\"value1\"}", "{\"item1\"=\"value1\"}", - results.next().getAs(JsonNode.class)); - JacksonHandle myBool = results.next().get(new JacksonHandle()); - assertEquals("myBool should = true", true, myBool.get().asBoolean()); - assertEquals("myInteger should = 123", 123, results.next().get(new JacksonHandle()).get().asInt()); - assertEquals("myDouble should = 1.1", 1.1, results.next().get(new JacksonHandle()).get().asDouble(), .001); - // the format we get from javax.xml.datatype.XMLGregorianCalendar.toString() - assertEquals("myDate should = '2014-09-01T00:00:00'", "2014-09-01T00:00:00", - results.next().get(new JAXBDatatypeHandle(Calendar.class)).get().toString()); - + assertEquals("myArray should = [\"item1\",\"item2\"]", + new ObjectMapper().readTree("[\"item1\",\"item2\"]"), + results.next().getAs(JsonNode.class)); + assertEquals("myObject should = {\"item1\":\"value1\"}", + new ObjectMapper().readTree("{\"item1\":\"value1\"}"), + results.next().getAs(JsonNode.class)); + assertEquals("myBool should = true", true, results.next().getBoolean()); + assertEquals("myInteger should = 123", 123, + results.next().getNumber().intValue()); + assertEquals("myDouble should = 1.1", 1.1, + results.next().getNumber().doubleValue(), .001); + // the same format we sent in (from javax.xml.datatype.XMLGregorianCalendar.toString()) + assertEquals("myDate should = '2014-09-01T00:00:00.000+02:00'", "2014-09-01T00:00:00.000+02:00", + results.next().getString()); // accept and return each XML variable type so use MultiPartResponsePage - String xquery = "declare variable $myString as xs:string external;" + - "declare variable $myBinary as binary() external;" + - "declare variable $myComment as comment() external;" + - "declare variable $myDocument as document() external;" + + String xquery = "declare namespace test='http://marklogic.com/test';" + + "declare variable $test:myString as xs:string external;" + + "declare variable $myArray as json:array external;" + + "declare variable $myObject as json:object external;" + + "declare variable $myAnyUri as xs:anyURI external;" + + "declare variable $myBinary as xs:hexBinary external;" + + "declare variable $myBase64Binary as xs:base64Binary external;" + + "declare variable $myHexBinary as xs:hexBinary external;" + "declare variable $myDuration as xs:duration external;" + - "declare variable $myElement as element() external;" + - "declare variable $myNode as node() external;" + - "declare variable $myProcessingInstruction as processingInstruction() external;" + - "declare variable $myText as text() external;" + - "declare variable $myComment as comment() external;" + - "declare variable $myComment as comment() external;" + - "declare variable $myComment as comment() external;" + - "declare variable $myComment as comment() external;" + - "declare variable $myComment as comment() external;" + + "declare variable $myQName as xs:QName external;" + + "declare variable $myDocument as xs:string external;" + + "declare variable $myAttribute as xs:string external;" + + "declare variable $myComment as xs:string external;" + + "declare variable $myElement as xs:string external;" + + "declare variable $myProcessingInstruction as xs:string external;" + + "declare variable $myText as xs:string external;" + "declare variable $myBool as xs:boolean external;" + "declare variable $myInteger as xs:integer external;" + + "declare variable $myBigInteger as xs:string external;" + + "declare variable $myDecimal as xs:decimal external;" + "declare variable $myDouble as xs:double external;" + + "declare variable $myFloat as xs:float external;" + + "declare variable $myGDay as xs:gDay external;" + + "declare variable $myGMonth as xs:gMonth external;" + + "declare variable $myGMonthDay as xs:gMonthDay external;" + + "declare variable $myGYear as xs:gYear external;" + + "declare variable $myGYearMonth as xs:gYearMonth external;" + "declare variable $myDate as xs:date external;" + - "$myString, $myArray, $myObject, $myBool, $myInteger, $myDouble, $myDate"; - Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); - query = Common.client.newServerEval() - .xquery(xquery) - // String is mapped to implicitly use StringHandle - .addVariableAs("myString", "Mars") - .addVariableAs("myBinary", new byte[] { 1, 2, 3}) - .addVariableAs("myComment", document.createComment("")) - .addVariableAs("myDocument", document) - .addVariable("myDuration", new JAXBDatatypeHandle(JAXBDatatypeHandle.XS_DURATION, "PT0H")) - .addVariableAs("myElement", document.createElementNS("http://marklogic.com/appservices/search", "options")) - // ArrayNode extends XMLNode which is mapped to implicitly use JacksonHandle - .addVariableAs("myArray", new JacksonHandle().getMapper().createArrayNode().add("item1").add("item2")) - // ObjectNode extends XMLNode which is mapped to implicitly use JacksonHandle - .addVariableAs("myObject", new JacksonHandle().getMapper().createObjectNode().put("item1", "value1")) - // the rest use built-in methods of XMLVariableSet - .addVariable("myBool", true) - .addVariable("myInteger", 123) - .addVariable("myDouble", 1.1) - .addVariableAs("myDate", new GregorianCalendar(2014, Calendar.SEPTEMBER, 1)); + "declare variable $myDateTime as xs:dateTime external;" + + "declare variable $myTime as xs:time external;" + + "declare variable $myNull external;" + + "let $myBinary := binary{$myBinary}" + + "let $myDocument := " + + " xdmp:unquote($myDocument) " + + "let $myAttribute := " + + " xdmp:unquote($myAttribute)/*/@* " + + "let $myComment := " + + " xdmp:unquote($myComment)/comment() " + + "let $myElement := " + + " xdmp:unquote($myElement)/element() " + + "let $myProcessingInstruction := " + + " xdmp:unquote($myProcessingInstruction)/processing-instruction() " + + "let $myText := text {$myText} " + + "let $myCtsQuery := cts:word-query('a') " + + "return ($test:myString, $myArray, $myObject, $myAnyUri, " + + "$myBinary, $myBase64Binary, $myHexBinary, " + + "$myDuration, $myQName, $myDocument, $myAttribute, $myComment, $myElement, " + + "$myProcessingInstruction, $myText, " + + "$myBool, $myInteger, $myBigInteger, $myDecimal, $myDouble, $myFloat, $myGDay, $myGMonth, $myGMonthDay, $myGYear, $myGYearMonth, $myDate, $myDateTime, $myTime, $myNull, $myCtsQuery)"; + Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder() + .parse(this.getClass().getClassLoader().getResourceAsStream("1-empty-1.0.xml")); + query = Common.client.newServerEval() + .xquery(xquery) + .addNamespace("myPrefix", "http://marklogic.com/test") + .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())) + .addVariable("myBase64Binary", DatatypeConverter.printBase64Binary(new byte[] {1, 2, 3})) + .addVariable("myHexBinary", DatatypeConverter.printHexBinary(new byte[] {1, 2, 3})) + .addVariable("myDuration", "P100D") + .addVariable("myDocument", new DOMHandle(document)) + .addVariable("myQName", "myPrefix:a") + .addVariable("myAttribute", "") + .addVariable("myComment", "") + .addVariable("myElement", "") + .addVariable("myProcessingInstruction", "") + .addVariable("myText", "a") + // the next three use built-in methods of ServerEvaluationCall + .addVariable("myBool", true) + .addVariable("myInteger", 1234567890123456789l) + .addVariable("myBigInteger", "123456789012345678901234567890") + .addVariable("myDecimal", "1111111111111111111.9999999999") + .addVariable("myDouble", 11111111111111111111.7777777777) + .addVariable("myFloat", 1.1) + .addVariable("myGDay", "---01") + .addVariable("myGMonth", "--01") + .addVariable("myGMonthDay", "--01-01") + .addVariable("myGYear", "1901") + .addVariable("myGYearMonth", "1901-01") + .addVariable("myDate", "2014-09-01") + .addVariable("myDateTime", + DatatypeFactory.newInstance().newXMLGregorianCalendar(septFirst).toString()) + .addVariable("myTime", "00:01:01") + .addVariable("myNull", (String) null); results = query.eval(); - assertEquals("myString should = 'Mars'", "Mars", results.next().getAs(String.class)); - assertEquals("myArray should = [\"item1\",\"item2\"]", "[\"item1\",\"item2\"]", - results.next().getAs(JsonNode.class)); - assertEquals("myObject should = {\"item1\"=\"value1\"}", "{\"item1\"=\"value1\"}", - results.next().getAs(JsonNode.class)); - myBool = results.next().get(new JacksonHandle()); - assertEquals("myBool should = true", true, myBool.get().asBoolean()); - assertEquals("myInteger should = 123", 123, results.next().get(new JacksonHandle()).get().asInt()); - assertEquals("myDouble should = 1.1", 1.1, results.next().get(new JacksonHandle()).get().asDouble(), .001); - // the format what we get from java.util.Calendar.toString() - assertEquals("myDate should = 'Mon Sep 01 00:00:00 UDT 2014'", "Mon Sep 01 00:00:00 UDT 2014", - results.next().get(new JAXBDatatypeHandle(Calendar.class)).get().toString()); + EvalResult result = results.next(); + assertEquals("myString should = 'Mars'", "Mars", result.getAs(String.class)); + assertEquals("myString should be Type.STRING", EvalResult.Type.STRING, result.getType()); + assertEquals("myString should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myArray should = [\"item1\",\"item2\"]", + new ObjectMapper().readTree("[\"item1\",\"item2\"]"), + result.getAs(JsonNode.class)); + assertEquals("myArray should be Type.JSON", EvalResult.Type.JSON, result.getType()); + assertEquals("myArray should be Format.JSON", Format.JSON, result.getFormat()); + result = results.next(); + assertEquals("myObject should = {\"item1\":\"value1\"}", + new ObjectMapper().readTree("{\"item1\":\"value1\"}"), + result.getAs(JsonNode.class)); + assertEquals("myObject should be Type.JSON", EvalResult.Type.JSON, result.getType()); + assertEquals("myObject should be Format.JSON", Format.JSON, result.getFormat()); + result = results.next(); + assertEquals("myAnyUri looks wrong", "http://marklogic.com/a", result.getString()); + assertEquals("myAnyUri should be Type.ANYURI", EvalResult.Type.ANYURI, result.getType()); + assertEquals("myAnyUri should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myBinary looks wrong", ",", result.getString()); + assertEquals("myBinary should be Type.BINARY", EvalResult.Type.BINARY, result.getType()); + assertEquals("myBinary should be Format.UNKNOWN", Format.UNKNOWN, result.getFormat()); + result = results.next(); + assertArrayEquals("myBase64Binary should = 1, 2, 3", new byte[] {1, 2, 3}, + DatatypeConverter.parseBase64Binary(result.getString())); + assertEquals("myBase64Binary should be Type.BASE64BINARY", EvalResult.Type.BASE64BINARY, result.getType()); + assertEquals("myBase64Binary should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertArrayEquals("myHexBinary should = 1, 2, 3", new byte[] {1, 2, 3}, + DatatypeConverter.parseHexBinary(result.getString())); + assertEquals("myHexBinary should be Type.HEXBINARY", EvalResult.Type.HEXBINARY, result.getType()); + assertEquals("myHexBinary should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myDuration should = P100D", "P100D", result.getString()); + assertEquals("myDuration should be Type.DURATION", EvalResult.Type.DURATION, result.getType()); + assertEquals("myDuration should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myQName doesn't look right", "myPrefix:a", result.getString()); + assertEquals("myQName should be Type.QNAME", EvalResult.Type.QNAME, result.getType()); + assertEquals("myQName should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myDocument doesn't look right", + "\n" + + "", + result.getString()); + assertEquals("myDocument should be Type.XML", EvalResult.Type.XML, result.getType()); + assertEquals("myDocument should be Format.XML", Format.XML, result.getFormat()); + result = results.next(); + assertEquals("myAttribute looks wrong", "a", result.getString()); + assertEquals("myAttribute should be Type.ATTRIBUTE", EvalResult.Type.ATTRIBUTE, result.getType()); + assertEquals("myAttribute should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myComment should = ", "", result.getString()); + assertEquals("myComment should be Type.COMMENT", EvalResult.Type.COMMENT, result.getType()); + assertEquals("myComment should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myElement should = ", "", result.getString()); + assertEquals("myElement should be Type.XML", EvalResult.Type.XML, result.getType()); + assertEquals("myElement should be Format.XML", Format.XML, result.getFormat()); + result = results.next(); + assertEquals("myProcessingInstruction should = ", "", result.getString()); + assertEquals("myProcessingInstruction should be Type.PROCESSINGINSTRUCTION", + EvalResult.Type.PROCESSINGINSTRUCTION, result.getType()); + assertEquals("myProcessingInstruction should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myText should = a", "a", result.getString()); + assertEquals("myText should be Type.TEXTNODE", EvalResult.Type.TEXTNODE, result.getType()); + assertEquals("myText should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myBool should = true", true, result.getBoolean()); + assertEquals("myBool should be Type.BOOLEAN", EvalResult.Type.BOOLEAN, result.getType()); + assertEquals("myBool should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myInteger should = 1234567890123456789l", 1234567890123456789l, + result.getNumber().longValue()); + assertEquals("myInteger should be Type.INTEGER", EvalResult.Type.INTEGER, result.getType()); + assertEquals("myInteger should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myBigInteger looks wrong", new BigInteger("123456789012345678901234567890"), + new BigInteger(result.getString())); + assertEquals("myBigInteger should be Type.STRING", EvalResult.Type.STRING, result.getType()); + assertEquals("myBigInteger should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myDecimal looks wrong", 1111111111111111111.9, + result.getNumber().doubleValue(), .001); + assertEquals("myDecimal should be Type.DECIMAL", EvalResult.Type.DECIMAL, result.getType()); + assertEquals("myDecimal should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myDouble looks wrong", 1.11111111111111E19, + result.getNumber().doubleValue(), .001); + assertEquals("myDouble should be Type.DOUBLE", EvalResult.Type.DOUBLE, result.getType()); + assertEquals("myDouble should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myFloat looks wrong", 1.1, result.getNumber().floatValue(), .001); + assertEquals("myFloat should be Type.FLOAT", EvalResult.Type.FLOAT, result.getType()); + assertEquals("myFloat should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myGDay looks wrong", "---01", result.getString()); + assertEquals("myGDay should be Type.GDAY", EvalResult.Type.GDAY, result.getType()); + assertEquals("myGDay should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myGMonth looks wrong", "--01", result.getString()); + assertEquals("myGMonth should be Type.GMONTH", EvalResult.Type.GMONTH, result.getType()); + assertEquals("myGMonth should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myGMonthDay looks wrong", "--01-01", result.getString()); + assertEquals("myGMonthDay should be Type.GMONTHDAY", EvalResult.Type.GMONTHDAY, result.getType()); + assertEquals("myGMonthDay should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myGYear looks wrong", "1901", result.getString()); + assertEquals("myGYear should be Type.GYEAR", EvalResult.Type.GYEAR, result.getType()); + assertEquals("myGYear should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myGYearMonth looks wrong", "1901-01", result.getString()); + assertEquals("myGYearMonth should be Type.GYEARMONTH", EvalResult.Type.GYEARMONTH, result.getType()); + assertEquals("myGYearMonth should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + // the lexical format MarkLogic uses to serialize a date + assertEquals("myDate should = '2014-09-01", "2014-09-01", result.getString()); + assertEquals("myDate should be Type.DATE", EvalResult.Type.DATE, result.getType()); + assertEquals("myDate should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + // the lexical format MarkLogic uses to serialize a dateTime + assertEquals("myDateTime should = '2014-09-01T00:00:00+02:00'", "2014-09-01T00:00:00+02:00", + result.getString()); + assertEquals("myDateTime should be Type.DATETIME", EvalResult.Type.DATETIME, result.getType()); + assertEquals("myDateTime should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myTime looks wrong", "00:01:01", result.getString()); + assertEquals("myTime should be Type.TIME", EvalResult.Type.TIME, result.getType()); + assertEquals("myTime should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myCtsQuery should be Type.OTHER", EvalResult.Type.OTHER, result.getType()); + assertEquals("myCtsQuery should be Format.TEXT", Format.TEXT, result.getFormat()); } } diff --git a/src/test/java/com/marklogic/client/test/HandleAccessorTest.java b/src/test/java/com/marklogic/client/test/HandleAccessorTest.java new file mode 100644 index 000000000..497b3cdfb --- /dev/null +++ b/src/test/java/com/marklogic/client/test/HandleAccessorTest.java @@ -0,0 +1,52 @@ +/* + * Copyright 2012-2014 MarkLogic Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.marklogic.client.test; + +import static org.junit.Assert.assertEquals; + +import java.io.File; +import java.io.IOException; +import java.io.StringReader; +import java.net.URISyntaxException; +import java.net.URL; + +import org.junit.Test; + +import com.marklogic.client.impl.HandleAccessor; +import com.marklogic.client.io.BytesHandle; +import com.marklogic.client.io.FileHandle; +import com.marklogic.client.io.InputStreamHandle; +import com.marklogic.client.io.ReaderHandle; +import com.marklogic.client.io.StringHandle; +public class HandleAccessorTest { + @Test + public void testContentAsString() throws URISyntaxException, IOException { + // I'm purposely using a string with a non-ascii character to test + // charset issues + String hola = "¡Hola!"; + assertEquals("String content mismatch", hola, + HandleAccessor.contentAsString(new StringHandle(hola))); + assertEquals("byte[] content mismatch", hola, + HandleAccessor.contentAsString(new BytesHandle(hola.getBytes()))); + URL filePath = this.getClass().getClassLoader().getResource("hola.txt"); + assertEquals("Reader content mismatch", hola, + HandleAccessor.contentAsString(new ReaderHandle(new StringReader(hola)))); + assertEquals("File content mismatch", hola, + HandleAccessor.contentAsString(new FileHandle(new File(filePath.toURI())))); + assertEquals("InputStream content mismatch", hola, + HandleAccessor.contentAsString(new InputStreamHandle(filePath.openStream()))); + } +} diff --git a/src/test/resources/hola.txt b/src/test/resources/hola.txt new file mode 100644 index 000000000..5a7d6eb57 --- /dev/null +++ b/src/test/resources/hola.txt @@ -0,0 +1 @@ +¡Hola! \ No newline at end of file From 760c2ef22794e40d933363a432704df55b854471 Mon Sep 17 00:00:00 2001 From: sammefford Date: Fri, 3 Oct 2014 14:42:42 -0600 Subject: [PATCH 154/357] finish testing runtime database selection now that eval is working --- .../marklogic/client/test/DatabaseClientFactoryTest.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/marklogic/client/test/DatabaseClientFactoryTest.java b/src/test/java/com/marklogic/client/test/DatabaseClientFactoryTest.java index 4d1c0391a..0ac47977f 100644 --- a/src/test/java/com/marklogic/client/test/DatabaseClientFactoryTest.java +++ b/src/test/java/com/marklogic/client/test/DatabaseClientFactoryTest.java @@ -31,6 +31,7 @@ public class DatabaseClientFactoryTest { @BeforeClass public static void beforeClass() { Common.connect(); + //System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); } @AfterClass public static void afterClass() { @@ -45,14 +46,15 @@ public void testConnectStringIntStringStringDigest() { @Test public void testRuntimeDatabaseSelection() { DatabaseClient tmpClient = DatabaseClientFactory.newClient( - Common.HOST, Common.PORT, "Documents", Common.USERNAME, Common.PASSWORD, Authentication.DIGEST + Common.HOST, Common.PORT, "Documents", Common.ADMIN_USERNAME, Common.ADMIN_PASSWORD, Authentication.DIGEST ); assertNotNull("Factory could not create client with digest connection", tmpClient); String database = tmpClient.newServerEval() + .database("Documents") .xquery("xdmp:database-name(xdmp:database())") .evalAs(String.class); - assertEquals("Runtime database should is wrong", "Documents", database); + assertEquals("Runtime database is wrong", "Documents", database); tmpClient.release(); } From 923b7d164075dd0eaf7d0ac725ee46d19798ab98 Mon Sep 17 00:00:00 2001 From: sammefford Date: Fri, 3 Oct 2014 15:47:03 -0600 Subject: [PATCH 155/357] support a couple more ways the server might indicate a binary --- .../java/com/marklogic/client/impl/JerseyServices.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/com/marklogic/client/impl/JerseyServices.java b/src/main/java/com/marklogic/client/impl/JerseyServices.java index 40e83013f..dca7e9a20 100644 --- a/src/main/java/com/marklogic/client/impl/JerseyServices.java +++ b/src/main/java/com/marklogic/client/impl/JerseyServices.java @@ -3621,6 +3621,14 @@ public EvalResult.Type getType() { return EvalResult.Type.XML; } else if ( "text/xml".equals(contentType) ) { return EvalResult.Type.XML; + } else if ( "application/x-unknown-content-type".equals(contentType) && + "binary()".equals(content.getHeader("X-Primitive")) ) + { + return EvalResult.Type.BINARY; + } else if ( "application/octet-stream".equals(contentType) && + "node()".equals(content.getHeader("X-Primitive")) ) + { + return EvalResult.Type.BINARY; } } String xPrimitive = content.getHeader("X-Primitive"); From e48e95a25acdc184c27d62bfa76a83c1d2712f64 Mon Sep 17 00:00:00 2001 From: sammefford Date: Fri, 3 Oct 2014 16:03:24 -0600 Subject: [PATCH 156/357] whitespace only: get rid of sporadic tabs --- .../client/impl/ServerEvaluationCallImpl.java | 80 +++++++++---------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/marklogic/client/impl/ServerEvaluationCallImpl.java b/src/main/java/com/marklogic/client/impl/ServerEvaluationCallImpl.java index 1b496f649..dcd004ca4 100644 --- a/src/main/java/com/marklogic/client/impl/ServerEvaluationCallImpl.java +++ b/src/main/java/com/marklogic/client/impl/ServerEvaluationCallImpl.java @@ -29,12 +29,12 @@ public class ServerEvaluationCallImpl extends AbstractLoggingManager - implements ServerEvaluationCall + implements ServerEvaluationCall { public enum Context { ADHOC_XQUERY, ADHOC_JAVASCRIPT, INVOKE }; - private RESTServices services; - private HandleFactoryRegistry handleRegistry; + private RESTServices services; + private HandleFactoryRegistry handleRegistry; private String code; private String modulePath; private Context evalContext; @@ -49,19 +49,19 @@ public ServerEvaluationCallImpl(RESTServices services, HandleFactoryRegistry han } public ServerEvaluationCall xquery(String xquery) { - setContext(Context.ADHOC_XQUERY); + setContext(Context.ADHOC_XQUERY); code = xquery; return this; } public ServerEvaluationCall javascript(String javascript) { - setContext(Context.ADHOC_JAVASCRIPT); + setContext(Context.ADHOC_JAVASCRIPT); code = javascript; return this; } public ServerEvaluationCall modulePath(String modulePath) { - setContext(Context.INVOKE); + setContext(Context.INVOKE); this.modulePath = modulePath; return this; } @@ -89,17 +89,17 @@ public ServerEvaluationCall addVariable(String name, AbstractWriteHandle value) /** Like other *As convenience methods throughout the API, the Object value * is managed by the Handle registered for that Class. */ public ServerEvaluationCall addVariableAs(String name, Object value) { - if (value == null) return this; - - Class as = value.getClass(); - AbstractWriteHandle writeHandle = null; - if (AbstractWriteHandle.class.isAssignableFrom(as)) { - writeHandle = (AbstractWriteHandle) value; - } else { - ContentHandle contentHandle = handleRegistry.makeHandle(as); - Utilities.setHandleContent(contentHandle, value); - writeHandle = contentHandle; - } + if (value == null) return this; + + Class as = value.getClass(); + AbstractWriteHandle writeHandle = null; + if (AbstractWriteHandle.class.isAssignableFrom(as)) { + writeHandle = (AbstractWriteHandle) value; + } else { + ContentHandle contentHandle = handleRegistry.makeHandle(as); + Utilities.setHandleContent(contentHandle, value); + writeHandle = contentHandle; + } return addVariable(name, writeHandle); } @@ -114,7 +114,7 @@ public ServerEvaluationCall transaction(Transaction transaction) { } public T evalAs(Class responseType) { - if (responseType == null) throw new IllegalArgumentException("responseType cannot be null"); + if (responseType == null) throw new IllegalArgumentException("responseType cannot be null"); ContentHandle readHandle = handleRegistry.makeHandle(responseType); if ( readHandle == null ) return null; @@ -128,31 +128,31 @@ public H eval(H responseHandle) { } public EvalResultIterator eval() { - return services.postEvalInvoke(requestLogger, code, modulePath, evalContext, + return services.postEvalInvoke(requestLogger, code, modulePath, evalContext, vars, namespaceContext, database, transactionId); } - @Override - public ServerEvaluationCall addNamespace(String prefix, String namespaceURI) { - if ( namespaceContext == null ) namespaceContext = new EditableNamespaceContext(); + @Override + public ServerEvaluationCall addNamespace(String prefix, String namespaceURI) { + if ( namespaceContext == null ) namespaceContext = new EditableNamespaceContext(); namespaceContext.put(prefix, namespaceURI); - return this; - } - - @Override - public ServerEvaluationCall namespaceContext(EditableNamespaceContext namespaces) { - this.namespaceContext = namespaces; - return this; - } - - private void setContext(Context context) { - if ( evalContext == null ) { - evalContext = context; - } else { - throw new IllegalStateException("You can only initialize the code to evaluate one time. " + - "That means only one call to the xquery, javascript, xqueryModule, or " + - "javascriptModule methods per ServerEvaluationCall."); - } - } + return this; + } + + @Override + public ServerEvaluationCall namespaceContext(EditableNamespaceContext namespaces) { + this.namespaceContext = namespaces; + return this; + } + + private void setContext(Context context) { + if ( evalContext == null ) { + evalContext = context; + } else { + throw new IllegalStateException("You can only initialize the code to evaluate one time. " + + "That means only one call to the xquery, javascript, xqueryModule, or " + + "javascriptModule methods per ServerEvaluationCall."); + } + } } From 35a40edd7e5c644ded2f99a45ae947059b90b3bf Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 3 Oct 2014 15:04:53 -0700 Subject: [PATCH 157/357] adding word query scenarios --- .../TestPOJOQueryBuilderValueQuery.java | 132 ++++++++++++++---- 1 file changed, 105 insertions(+), 27 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderValueQuery.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderValueQuery.java index 8a8ff0ec3..6bd54d193 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderValueQuery.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderValueQuery.java @@ -29,10 +29,10 @@ public class TestPOJOQueryBuilderValueQuery extends BasicJavaClientREST { private static String restServerName = "REST-Java-Client-API-Server"; private static int restPort = 8011; private DatabaseClient client ; - + @BeforeClass public static void setUpBeforeClass() throws Exception { -// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); System.out.println("In setup"); setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); BasicJavaClientREST.setDatabaseProperties(dbName, "trailing-wildcard-searches", true); @@ -46,14 +46,14 @@ public static void tearDownAfterClass() throws Exception { @Before public void setUp() throws Exception { client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST); - - + + } @After public void tearDown() throws Exception { // release client client.release(); - + } public Artifact getArtifact(int counter){ @@ -61,23 +61,23 @@ public Artifact getArtifact(int counter){ Artifact cogs = new Artifact(); cogs.setId(counter); if( counter % 5 == 0){ - cogs.setName("Cogs special"); - if(counter % 2 ==0){ - Company acme = new Company(); - acme.setName("Acme special, Inc."); - acme.setWebsite("http://www.acme special.com"); - acme.setLatitude(41.998+counter); - acme.setLongitude(-87.966+counter); - cogs.setManufacturer(acme); + cogs.setName("Cogs special"); + if(counter % 2 ==0){ + Company acme = new Company(); + acme.setName("Acme special, Inc."); + acme.setWebsite("http://www.acme special.com"); + acme.setLatitude(41.998+counter); + acme.setLongitude(-87.966+counter); + cogs.setManufacturer(acme); - }else{ - Company widgets = new Company(); - widgets.setName("Widgets counter Inc."); - widgets.setWebsite("http://www.widgets counter.com"); - widgets.setLatitude(41.998+counter); - widgets.setLongitude(-87.966+counter); - cogs.setManufacturer(widgets); - } + }else{ + Company widgets = new Company(); + widgets.setName("Widgets counter Inc."); + widgets.setWebsite("http://www.widgets counter.com"); + widgets.setLatitude(41.998+counter); + widgets.setLongitude(-87.966+counter); + cogs.setManufacturer(widgets); + } }else{ cogs.setName("Cogs "+counter); if(counter % 2 ==0){ @@ -117,18 +117,18 @@ public void loadSimplePojos(PojoRepository products) } } } -// Below scenario is to test the value query with numbers return correct results + // Below scenario is to test the value query with numbers return correct results @Test public void testPOJOValueSearchWithNumbers() { PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); PojoPage p; - + this.loadSimplePojos(products); String[] searchOptions ={"case-sensitive","wildcarded","min-occurs=2"}; PojoQueryBuilder qb = products.getQueryBuilder(); Number[] searchIds = {5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100,105,110,115,120,121,122,123,124,125,126}; QueryDefinition qd = qb.value("id",searchOptions,-1.0,searchIds); - + JacksonHandle jh = new JacksonHandle(); products.setPageLength(5); p = products.search(qd, 1,jh); @@ -165,7 +165,7 @@ public void testPOJOValueSearchWithStrings() { JacksonHandle jh = new JacksonHandle(); products.setPageLength(5); p = products.search(qd, 1,jh); - + assertEquals("total no of pages",3,p.getTotalPages()); System.out.println(jh.get().toString()); long pageNo=1,count=0; @@ -187,7 +187,7 @@ public void testPOJOValueSearchWithStrings() { assertEquals("page length from search handle",5,jh.get().path("page-length").asInt()); assertEquals("Total results from search handle",11,jh.get().path("total").asInt()); } -//Below scenario is verifying value query from PojoBuilder that matches to no document + //Below scenario is verifying value query from PojoBuilder that matches to no document //Issue 127 is logged for the below scenario @Test public void testPOJOValueSearchWithNoResults() throws Exception { @@ -205,7 +205,7 @@ public void testPOJOValueSearchWithNoResults() throws Exception { setupServerRequestLogging(client,false); System.out.println(jh.get().toString()); assertEquals("total no of pages",0,p.getTotalPages()); - + long pageNo=1,count=0; do{ count =0; @@ -225,4 +225,82 @@ public void testPOJOValueSearchWithNoResults() throws Exception { assertEquals("page length from search handle",0,jh.get().path("page-length").asInt()); assertEquals("Total results from search handle",10,jh.get().path("total").asInt()); } + + //Below scenario is to test word query without options + @Test + public void testPOJOWordQuerySearchWithoutOptions() { + PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); + PojoPage p; + this.loadSimplePojos(products); + // String[] searchOptions ={"case-sensitive","min-occurs=2"}; + PojoQueryBuilder qb = products.getQueryBuilder(); + String[] searchNames = {"counter","special"}; + QueryDefinition qd = qb.word("name",searchNames); + JacksonHandle jh = new JacksonHandle(); + products.setPageLength(5); + p = products.search(qd, 1,jh); + System.out.println(jh.get().toString()); + assertEquals("total no of pages",5,p.getTotalPages()); + + long pageNo=1,count=0; + do{ + count =0; + p = products.search(qd,pageNo); + while(p.hasNext()){ + Artifact a =p.next(); + validateArtifact(a); + assertTrue("Verifying document id is part of the search ids",a.getId()%5==0); + assertTrue("Verifying document has word counter",a.getManufacturer().getName().contains("counter")); + count++; + System.out.println(a.getId()); + } + assertEquals("Page size",count,p.size()); + pageNo=pageNo+p.getPageSize(); + }while(!p.isLastPage() && pageNo<=p.getTotalSize()); + assertEquals("page number after the loop",5,p.getPageNumber()); + assertEquals("total no of pages",5,p.getTotalPages()); + assertEquals("page length from search handle",5,jh.get().path("page-length").asInt()); + assertEquals("Total results from search handle",22,jh.get().path("total").asInt()); + } + + //Below scenario is verifying word query from PojoBuilder with options + + @Test + public void testPOJOWordSearchWithOptions() throws Exception { + PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); + PojoPage p; + this.loadSimplePojos(products); + setupServerRequestLogging(client,true); + String[] searchOptions ={"case-sensitive","wildcarded","min-occurs=2"}; + PojoQueryBuilder qb = products.getQueryBuilder(); + String[] searchNames = {"count*"}; + QueryDefinition qd = qb.word("name",searchOptions,0.0,searchNames); + JacksonHandle jh = new JacksonHandle(); + products.setPageLength(5); + p = products.search(qd, 1,jh); + setupServerRequestLogging(client,false); + System.out.println(jh.get().toString()); + assertEquals("total no of pages",3,p.getTotalPages()); + + long pageNo=1,count=0; + do{ + count =0; + p = products.search(qd,pageNo); + while(p.hasNext()){ + Artifact a =p.next(); + validateArtifact(a); + assertTrue("Verifying document id is part of the search ids",a.getId()%5==0); + assertTrue("Verifying document has word counter",a.getManufacturer().getName().contains("counter")); + count++; + + } + assertEquals("Page size",count,p.size()); + pageNo=pageNo+p.getPageSize(); + }while(!p.isLastPage() && pageNo<=p.getTotalSize()); + assertEquals("page number after the loop",3,p.getPageNumber()); + assertEquals("total no of pages",3,p.getTotalPages()); + assertEquals("page length from search handle",5,jh.get().path("page-length").asInt()); + assertEquals("Total results from search handle",11,jh.get().path("total").asInt()); + } + } From 539e9fabbbb3fe4e27768f087b98189756ec05a0 Mon Sep 17 00:00:00 2001 From: sammefford Date: Fri, 3 Oct 2014 16:07:33 -0600 Subject: [PATCH 158/357] I was forgetting to close the EvalResultIterator --- .../client/impl/ServerEvaluationCallImpl.java | 6 +- .../com/marklogic/client/test/EvalTest.java | 314 +++++++++--------- 2 files changed, 163 insertions(+), 157 deletions(-) diff --git a/src/main/java/com/marklogic/client/impl/ServerEvaluationCallImpl.java b/src/main/java/com/marklogic/client/impl/ServerEvaluationCallImpl.java index dcd004ca4..5fd7875f7 100644 --- a/src/main/java/com/marklogic/client/impl/ServerEvaluationCallImpl.java +++ b/src/main/java/com/marklogic/client/impl/ServerEvaluationCallImpl.java @@ -123,8 +123,10 @@ public T evalAs(Class responseType) { public H eval(H responseHandle) { EvalResultIterator iterator = eval(); - if ( iterator == null || iterator.hasNext() == false ) return null; - return iterator.next().get(responseHandle); + try { + if ( iterator == null || iterator.hasNext() == false ) return null; + return iterator.next().get(responseHandle); + } finally { iterator.close(); } } public EvalResultIterator eval() { diff --git a/src/test/java/com/marklogic/client/test/EvalTest.java b/src/test/java/com/marklogic/client/test/EvalTest.java index a5a2c4026..8e609f29f 100644 --- a/src/test/java/com/marklogic/client/test/EvalTest.java +++ b/src/test/java/com/marklogic/client/test/EvalTest.java @@ -103,27 +103,29 @@ public void evalTest1() throws ParserConfigurationException, DatatypeConfigurati .addVariable("myDate", DatatypeFactory.newInstance().newXMLGregorianCalendar(septFirst).toString()); EvalResultIterator results = query.eval(); - assertEquals("myString should = 'Mars'", "Mars", results.next().getAs(String.class)); - assertEquals("myArray should = [\"item1\",\"item2\"]", - new ObjectMapper().readTree("[\"item1\",\"item2\"]"), - results.next().getAs(JsonNode.class)); - assertEquals("myObject should = {\"item1\":\"value1\"}", - new ObjectMapper().readTree("{\"item1\":\"value1\"}"), - results.next().getAs(JsonNode.class)); - assertEquals("myBool should = true", true, results.next().getBoolean()); - assertEquals("myInteger should = 123", 123, - results.next().getNumber().intValue()); - assertEquals("myDouble should = 1.1", 1.1, - results.next().getNumber().doubleValue(), .001); - // the same format we sent in (from javax.xml.datatype.XMLGregorianCalendar.toString()) - assertEquals("myDate should = '2014-09-01T00:00:00.000+02:00'", "2014-09-01T00:00:00.000+02:00", - results.next().getString()); + try { + assertEquals("myString should = 'Mars'", "Mars", results.next().getAs(String.class)); + assertEquals("myArray should = [\"item1\",\"item2\"]", + new ObjectMapper().readTree("[\"item1\",\"item2\"]"), + results.next().getAs(JsonNode.class)); + assertEquals("myObject should = {\"item1\":\"value1\"}", + new ObjectMapper().readTree("{\"item1\":\"value1\"}"), + results.next().getAs(JsonNode.class)); + assertEquals("myBool should = true", true, results.next().getBoolean()); + assertEquals("myInteger should = 123", 123, + results.next().getNumber().intValue()); + assertEquals("myDouble should = 1.1", 1.1, + results.next().getNumber().doubleValue(), .001); + // the same format we sent in (from javax.xml.datatype.XMLGregorianCalendar.toString()) + assertEquals("myDate should = '2014-09-01T00:00:00.000+02:00'", "2014-09-01T00:00:00.000+02:00", + results.next().getString()); + } finally { results.close(); } // accept and return each XML variable type so use MultiPartResponsePage String xquery = "declare namespace test='http://marklogic.com/test';" + "declare variable $test:myString as xs:string external;" + - "declare variable $myArray as json:array external;" + - "declare variable $myObject as json:object external;" + + "declare variable $myArray as json:array external;" + + "declare variable $myObject as json:object external;" + "declare variable $myAnyUri as xs:anyURI external;" + "declare variable $myBinary as xs:hexBinary external;" + "declare variable $myBase64Binary as xs:base64Binary external;" + @@ -170,7 +172,7 @@ public void evalTest1() throws ParserConfigurationException, DatatypeConfigurati "$myProcessingInstruction, $myText, " + "$myBool, $myInteger, $myBigInteger, $myDecimal, $myDouble, $myFloat, $myGDay, $myGMonth, $myGMonthDay, $myGYear, $myGYearMonth, $myDate, $myDateTime, $myTime, $myNull, $myCtsQuery)"; Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder() - .parse(this.getClass().getClassLoader().getResourceAsStream("1-empty-1.0.xml")); + .parse(this.getClass().getClassLoader().getResourceAsStream("1-empty-1.0.xml")); query = Common.client.newServerEval() .xquery(xquery) .addNamespace("myPrefix", "http://marklogic.com/test") @@ -208,143 +210,145 @@ public void evalTest1() throws ParserConfigurationException, DatatypeConfigurati DatatypeFactory.newInstance().newXMLGregorianCalendar(septFirst).toString()) .addVariable("myTime", "00:01:01") .addVariable("myNull", (String) null); - results = query.eval(); - EvalResult result = results.next(); - assertEquals("myString should = 'Mars'", "Mars", result.getAs(String.class)); - assertEquals("myString should be Type.STRING", EvalResult.Type.STRING, result.getType()); - assertEquals("myString should be Format.TEXT", Format.TEXT, result.getFormat()); - result = results.next(); - assertEquals("myArray should = [\"item1\",\"item2\"]", - new ObjectMapper().readTree("[\"item1\",\"item2\"]"), - result.getAs(JsonNode.class)); - assertEquals("myArray should be Type.JSON", EvalResult.Type.JSON, result.getType()); - assertEquals("myArray should be Format.JSON", Format.JSON, result.getFormat()); - result = results.next(); - assertEquals("myObject should = {\"item1\":\"value1\"}", - new ObjectMapper().readTree("{\"item1\":\"value1\"}"), - result.getAs(JsonNode.class)); - assertEquals("myObject should be Type.JSON", EvalResult.Type.JSON, result.getType()); - assertEquals("myObject should be Format.JSON", Format.JSON, result.getFormat()); - result = results.next(); - assertEquals("myAnyUri looks wrong", "http://marklogic.com/a", result.getString()); - assertEquals("myAnyUri should be Type.ANYURI", EvalResult.Type.ANYURI, result.getType()); - assertEquals("myAnyUri should be Format.TEXT", Format.TEXT, result.getFormat()); - result = results.next(); - assertEquals("myBinary looks wrong", ",", result.getString()); - assertEquals("myBinary should be Type.BINARY", EvalResult.Type.BINARY, result.getType()); - assertEquals("myBinary should be Format.UNKNOWN", Format.UNKNOWN, result.getFormat()); - result = results.next(); - assertArrayEquals("myBase64Binary should = 1, 2, 3", new byte[] {1, 2, 3}, - DatatypeConverter.parseBase64Binary(result.getString())); - assertEquals("myBase64Binary should be Type.BASE64BINARY", EvalResult.Type.BASE64BINARY, result.getType()); - assertEquals("myBase64Binary should be Format.TEXT", Format.TEXT, result.getFormat()); - result = results.next(); - assertArrayEquals("myHexBinary should = 1, 2, 3", new byte[] {1, 2, 3}, - DatatypeConverter.parseHexBinary(result.getString())); - assertEquals("myHexBinary should be Type.HEXBINARY", EvalResult.Type.HEXBINARY, result.getType()); - assertEquals("myHexBinary should be Format.TEXT", Format.TEXT, result.getFormat()); - result = results.next(); - assertEquals("myDuration should = P100D", "P100D", result.getString()); - assertEquals("myDuration should be Type.DURATION", EvalResult.Type.DURATION, result.getType()); - assertEquals("myDuration should be Format.TEXT", Format.TEXT, result.getFormat()); - result = results.next(); - assertEquals("myQName doesn't look right", "myPrefix:a", result.getString()); - assertEquals("myQName should be Type.QNAME", EvalResult.Type.QNAME, result.getType()); - assertEquals("myQName should be Format.TEXT", Format.TEXT, result.getFormat()); - result = results.next(); - assertEquals("myDocument doesn't look right", - "\n" + - "", - result.getString()); - assertEquals("myDocument should be Type.XML", EvalResult.Type.XML, result.getType()); - assertEquals("myDocument should be Format.XML", Format.XML, result.getFormat()); - result = results.next(); - assertEquals("myAttribute looks wrong", "a", result.getString()); - assertEquals("myAttribute should be Type.ATTRIBUTE", EvalResult.Type.ATTRIBUTE, result.getType()); - assertEquals("myAttribute should be Format.TEXT", Format.TEXT, result.getFormat()); - result = results.next(); - assertEquals("myComment should = ", "", result.getString()); - assertEquals("myComment should be Type.COMMENT", EvalResult.Type.COMMENT, result.getType()); - assertEquals("myComment should be Format.TEXT", Format.TEXT, result.getFormat()); - result = results.next(); - assertEquals("myElement should = ", "", result.getString()); - assertEquals("myElement should be Type.XML", EvalResult.Type.XML, result.getType()); - assertEquals("myElement should be Format.XML", Format.XML, result.getFormat()); - result = results.next(); - assertEquals("myProcessingInstruction should = ", "", result.getString()); - assertEquals("myProcessingInstruction should be Type.PROCESSINGINSTRUCTION", - EvalResult.Type.PROCESSINGINSTRUCTION, result.getType()); - assertEquals("myProcessingInstruction should be Format.TEXT", Format.TEXT, result.getFormat()); - result = results.next(); - assertEquals("myText should = a", "a", result.getString()); - assertEquals("myText should be Type.TEXTNODE", EvalResult.Type.TEXTNODE, result.getType()); - assertEquals("myText should be Format.TEXT", Format.TEXT, result.getFormat()); - result = results.next(); - assertEquals("myBool should = true", true, result.getBoolean()); - assertEquals("myBool should be Type.BOOLEAN", EvalResult.Type.BOOLEAN, result.getType()); - assertEquals("myBool should be Format.TEXT", Format.TEXT, result.getFormat()); - result = results.next(); - assertEquals("myInteger should = 1234567890123456789l", 1234567890123456789l, - result.getNumber().longValue()); - assertEquals("myInteger should be Type.INTEGER", EvalResult.Type.INTEGER, result.getType()); - assertEquals("myInteger should be Format.TEXT", Format.TEXT, result.getFormat()); - result = results.next(); - assertEquals("myBigInteger looks wrong", new BigInteger("123456789012345678901234567890"), - new BigInteger(result.getString())); - assertEquals("myBigInteger should be Type.STRING", EvalResult.Type.STRING, result.getType()); - assertEquals("myBigInteger should be Format.TEXT", Format.TEXT, result.getFormat()); - result = results.next(); - assertEquals("myDecimal looks wrong", 1111111111111111111.9, - result.getNumber().doubleValue(), .001); - assertEquals("myDecimal should be Type.DECIMAL", EvalResult.Type.DECIMAL, result.getType()); - assertEquals("myDecimal should be Format.TEXT", Format.TEXT, result.getFormat()); - result = results.next(); - assertEquals("myDouble looks wrong", 1.11111111111111E19, - result.getNumber().doubleValue(), .001); - assertEquals("myDouble should be Type.DOUBLE", EvalResult.Type.DOUBLE, result.getType()); - assertEquals("myDouble should be Format.TEXT", Format.TEXT, result.getFormat()); - result = results.next(); - assertEquals("myFloat looks wrong", 1.1, result.getNumber().floatValue(), .001); - assertEquals("myFloat should be Type.FLOAT", EvalResult.Type.FLOAT, result.getType()); - assertEquals("myFloat should be Format.TEXT", Format.TEXT, result.getFormat()); - result = results.next(); - assertEquals("myGDay looks wrong", "---01", result.getString()); - assertEquals("myGDay should be Type.GDAY", EvalResult.Type.GDAY, result.getType()); - assertEquals("myGDay should be Format.TEXT", Format.TEXT, result.getFormat()); - result = results.next(); - assertEquals("myGMonth looks wrong", "--01", result.getString()); - assertEquals("myGMonth should be Type.GMONTH", EvalResult.Type.GMONTH, result.getType()); - assertEquals("myGMonth should be Format.TEXT", Format.TEXT, result.getFormat()); - result = results.next(); - assertEquals("myGMonthDay looks wrong", "--01-01", result.getString()); - assertEquals("myGMonthDay should be Type.GMONTHDAY", EvalResult.Type.GMONTHDAY, result.getType()); - assertEquals("myGMonthDay should be Format.TEXT", Format.TEXT, result.getFormat()); - result = results.next(); - assertEquals("myGYear looks wrong", "1901", result.getString()); - assertEquals("myGYear should be Type.GYEAR", EvalResult.Type.GYEAR, result.getType()); - assertEquals("myGYear should be Format.TEXT", Format.TEXT, result.getFormat()); - result = results.next(); - assertEquals("myGYearMonth looks wrong", "1901-01", result.getString()); - assertEquals("myGYearMonth should be Type.GYEARMONTH", EvalResult.Type.GYEARMONTH, result.getType()); - assertEquals("myGYearMonth should be Format.TEXT", Format.TEXT, result.getFormat()); - result = results.next(); - // the lexical format MarkLogic uses to serialize a date - assertEquals("myDate should = '2014-09-01", "2014-09-01", result.getString()); - assertEquals("myDate should be Type.DATE", EvalResult.Type.DATE, result.getType()); - assertEquals("myDate should be Format.TEXT", Format.TEXT, result.getFormat()); - result = results.next(); - // the lexical format MarkLogic uses to serialize a dateTime - assertEquals("myDateTime should = '2014-09-01T00:00:00+02:00'", "2014-09-01T00:00:00+02:00", - result.getString()); - assertEquals("myDateTime should be Type.DATETIME", EvalResult.Type.DATETIME, result.getType()); - assertEquals("myDateTime should be Format.TEXT", Format.TEXT, result.getFormat()); - result = results.next(); - assertEquals("myTime looks wrong", "00:01:01", result.getString()); - assertEquals("myTime should be Type.TIME", EvalResult.Type.TIME, result.getType()); - assertEquals("myTime should be Format.TEXT", Format.TEXT, result.getFormat()); - result = results.next(); - assertEquals("myCtsQuery should be Type.OTHER", EvalResult.Type.OTHER, result.getType()); - assertEquals("myCtsQuery should be Format.TEXT", Format.TEXT, result.getFormat()); + try { + results = query.eval(); + EvalResult result = results.next(); + assertEquals("myString should = 'Mars'", "Mars", result.getAs(String.class)); + assertEquals("myString should be Type.STRING", EvalResult.Type.STRING, result.getType()); + assertEquals("myString should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myArray should = [\"item1\",\"item2\"]", + new ObjectMapper().readTree("[\"item1\",\"item2\"]"), + result.getAs(JsonNode.class)); + assertEquals("myArray should be Type.JSON", EvalResult.Type.JSON, result.getType()); + assertEquals("myArray should be Format.JSON", Format.JSON, result.getFormat()); + result = results.next(); + assertEquals("myObject should = {\"item1\":\"value1\"}", + new ObjectMapper().readTree("{\"item1\":\"value1\"}"), + result.getAs(JsonNode.class)); + assertEquals("myObject should be Type.JSON", EvalResult.Type.JSON, result.getType()); + assertEquals("myObject should be Format.JSON", Format.JSON, result.getFormat()); + result = results.next(); + assertEquals("myAnyUri looks wrong", "http://marklogic.com/a", result.getString()); + assertEquals("myAnyUri should be Type.ANYURI", EvalResult.Type.ANYURI, result.getType()); + assertEquals("myAnyUri should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myBinary looks wrong", ",", result.getString()); + assertEquals("myBinary should be Type.BINARY", EvalResult.Type.BINARY, result.getType()); + assertEquals("myBinary should be Format.UNKNOWN", Format.UNKNOWN, result.getFormat()); + result = results.next(); + assertArrayEquals("myBase64Binary should = 1, 2, 3", new byte[] {1, 2, 3}, + DatatypeConverter.parseBase64Binary(result.getString())); + assertEquals("myBase64Binary should be Type.BASE64BINARY", EvalResult.Type.BASE64BINARY, result.getType()); + assertEquals("myBase64Binary should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertArrayEquals("myHexBinary should = 1, 2, 3", new byte[] {1, 2, 3}, + DatatypeConverter.parseHexBinary(result.getString())); + assertEquals("myHexBinary should be Type.HEXBINARY", EvalResult.Type.HEXBINARY, result.getType()); + assertEquals("myHexBinary should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myDuration should = P100D", "P100D", result.getString()); + assertEquals("myDuration should be Type.DURATION", EvalResult.Type.DURATION, result.getType()); + assertEquals("myDuration should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myQName doesn't look right", "myPrefix:a", result.getString()); + assertEquals("myQName should be Type.QNAME", EvalResult.Type.QNAME, result.getType()); + assertEquals("myQName should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myDocument doesn't look right", + "\n" + + "", + result.getString()); + assertEquals("myDocument should be Type.XML", EvalResult.Type.XML, result.getType()); + assertEquals("myDocument should be Format.XML", Format.XML, result.getFormat()); + result = results.next(); + assertEquals("myAttribute looks wrong", "a", result.getString()); + assertEquals("myAttribute should be Type.ATTRIBUTE", EvalResult.Type.ATTRIBUTE, result.getType()); + assertEquals("myAttribute should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myComment should = ", "", result.getString()); + assertEquals("myComment should be Type.COMMENT", EvalResult.Type.COMMENT, result.getType()); + assertEquals("myComment should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myElement should = ", "", result.getString()); + assertEquals("myElement should be Type.XML", EvalResult.Type.XML, result.getType()); + assertEquals("myElement should be Format.XML", Format.XML, result.getFormat()); + result = results.next(); + assertEquals("myProcessingInstruction should = ", "", result.getString()); + assertEquals("myProcessingInstruction should be Type.PROCESSINGINSTRUCTION", + EvalResult.Type.PROCESSINGINSTRUCTION, result.getType()); + assertEquals("myProcessingInstruction should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myText should = a", "a", result.getString()); + assertEquals("myText should be Type.TEXTNODE", EvalResult.Type.TEXTNODE, result.getType()); + assertEquals("myText should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myBool should = true", true, result.getBoolean()); + assertEquals("myBool should be Type.BOOLEAN", EvalResult.Type.BOOLEAN, result.getType()); + assertEquals("myBool should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myInteger should = 1234567890123456789l", 1234567890123456789l, + result.getNumber().longValue()); + assertEquals("myInteger should be Type.INTEGER", EvalResult.Type.INTEGER, result.getType()); + assertEquals("myInteger should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myBigInteger looks wrong", new BigInteger("123456789012345678901234567890"), + new BigInteger(result.getString())); + assertEquals("myBigInteger should be Type.STRING", EvalResult.Type.STRING, result.getType()); + assertEquals("myBigInteger should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myDecimal looks wrong", 1111111111111111111.9, + result.getNumber().doubleValue(), .001); + assertEquals("myDecimal should be Type.DECIMAL", EvalResult.Type.DECIMAL, result.getType()); + assertEquals("myDecimal should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myDouble looks wrong", 1.11111111111111E19, + result.getNumber().doubleValue(), .001); + assertEquals("myDouble should be Type.DOUBLE", EvalResult.Type.DOUBLE, result.getType()); + assertEquals("myDouble should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myFloat looks wrong", 1.1, result.getNumber().floatValue(), .001); + assertEquals("myFloat should be Type.FLOAT", EvalResult.Type.FLOAT, result.getType()); + assertEquals("myFloat should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myGDay looks wrong", "---01", result.getString()); + assertEquals("myGDay should be Type.GDAY", EvalResult.Type.GDAY, result.getType()); + assertEquals("myGDay should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myGMonth looks wrong", "--01", result.getString()); + assertEquals("myGMonth should be Type.GMONTH", EvalResult.Type.GMONTH, result.getType()); + assertEquals("myGMonth should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myGMonthDay looks wrong", "--01-01", result.getString()); + assertEquals("myGMonthDay should be Type.GMONTHDAY", EvalResult.Type.GMONTHDAY, result.getType()); + assertEquals("myGMonthDay should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myGYear looks wrong", "1901", result.getString()); + assertEquals("myGYear should be Type.GYEAR", EvalResult.Type.GYEAR, result.getType()); + assertEquals("myGYear should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myGYearMonth looks wrong", "1901-01", result.getString()); + assertEquals("myGYearMonth should be Type.GYEARMONTH", EvalResult.Type.GYEARMONTH, result.getType()); + assertEquals("myGYearMonth should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + // the lexical format MarkLogic uses to serialize a date + assertEquals("myDate should = '2014-09-01", "2014-09-01", result.getString()); + assertEquals("myDate should be Type.DATE", EvalResult.Type.DATE, result.getType()); + assertEquals("myDate should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + // the lexical format MarkLogic uses to serialize a dateTime + assertEquals("myDateTime should = '2014-09-01T00:00:00+02:00'", "2014-09-01T00:00:00+02:00", + result.getString()); + assertEquals("myDateTime should be Type.DATETIME", EvalResult.Type.DATETIME, result.getType()); + assertEquals("myDateTime should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myTime looks wrong", "00:01:01", result.getString()); + assertEquals("myTime should be Type.TIME", EvalResult.Type.TIME, result.getType()); + assertEquals("myTime should be Format.TEXT", Format.TEXT, result.getFormat()); + result = results.next(); + assertEquals("myCtsQuery should be Type.OTHER", EvalResult.Type.OTHER, result.getType()); + assertEquals("myCtsQuery should be Format.TEXT", Format.TEXT, result.getFormat()); + } finally { results.close(); } } } From 98312ef555b977882ed222a7a6683049bf86d0b9 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 3 Oct 2014 15:08:56 -0700 Subject: [PATCH 159/357] adding word query scenarios --- .../marklogic/javaclient/TestPOJOQueryBuilderValueQuery.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderValueQuery.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderValueQuery.java index 6bd54d193..7e6ad4e55 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderValueQuery.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderValueQuery.java @@ -250,7 +250,7 @@ public void testPOJOWordQuerySearchWithoutOptions() { Artifact a =p.next(); validateArtifact(a); assertTrue("Verifying document id is part of the search ids",a.getId()%5==0); - assertTrue("Verifying document has word counter",a.getManufacturer().getName().contains("counter")); + assertTrue("Verifying document has word counter",a.getManufacturer().getName().contains("counter")||a.getManufacturer().getName().contains("special")); count++; System.out.println(a.getId()); } From e5f28b4d0b7e2560adc7a35145ec7d6061d598ab Mon Sep 17 00:00:00 2001 From: skottam Date: Fri, 3 Oct 2014 16:10:20 -0700 Subject: [PATCH 160/357] method setErrorFormat() is removed from rest/java layer and default is json --- .../src/test/java/com/marklogic/javaclient/TestBug19389.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBug19389.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBug19389.java index d0f6b21cf..10e2a8646 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBug19389.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBug19389.java @@ -41,7 +41,8 @@ public void testBug19389() throws IOException, ParserConfigurationException, SAX // set error format to JSON ServerConfigurationManager srvMgr = client.newServerConfigManager(); - srvMgr.setErrorFormat(Format.JSON); +// depricated +// srvMgr.setErrorFormat(Format.JSON); srvMgr.writeConfiguration(); // create query options manager From d96caebac2a1092e30ae48468560be8ebf9c4e8d Mon Sep 17 00:00:00 2001 From: gvaidees Date: Sun, 5 Oct 2014 20:46:06 -0700 Subject: [PATCH 161/357] Added methods to create temporal axis and collection based on element range index --- .../marklogic/javaclient/ConnectedRESTQA.java | 2868 +++++++++-------- 1 file changed, 1542 insertions(+), 1326 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java b/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java index 933ebf4e9..6ea12fb49 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java @@ -1,1327 +1,1543 @@ -package com.marklogic.javaclient; -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.util.*; -import org.apache.http.auth.AuthScope; -import org.apache.http.auth.UsernamePasswordCredentials; -import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.methods.HttpDelete; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.client.methods.HttpPut; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.entity.FileEntity; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.NameValuePair; -import org.apache.http.message.BasicNameValuePair; -import org.apache.http.client.entity.*; - -import java.io.BufferedInputStream; -import java.io.BufferedReader; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.FileReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.io.Reader; -import java.io.StringReader; -import java.io.StringWriter; -import java.io.UnsupportedEncodingException; -import java.nio.channels.FileChannel; -import java.util.Iterator; -import java.util.Map; -import java.util.Scanner; -import java.util.Set; -import java.util.List; -import java.util.ArrayList; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ArrayNode; -import com.fasterxml.jackson.databind.node.ObjectNode; -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.admin.ServerConfigurationManager; -import com.marklogic.client.io.DocumentMetadataHandle; - -import java.net.InetAddress; - -import org.junit.Test; - -import static org.junit.Assert.*; - -public abstract class ConnectedRESTQA { - - /** - * Use Rest call to create a database. - * @param dbName - */ - - public static void createDB(String dbName) { - try { - - DefaultHttpClient client = new DefaultHttpClient(); - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); - - HttpPost post = new HttpPost("http://localhost:8002"+ "/manage/v2/databases?format=json"); - String JSONString = "[{\"name\":\""+ dbName + "\"}]"; - - post.addHeader("Content-type", "application/json"); - post.setEntity( new StringEntity(JSONString)); - - - HttpResponse response = client.execute(post); - HttpEntity respEntity = response.getEntity(); - - if (respEntity != null) { - // EntityUtils to get the response content - String content = EntityUtils.toString(respEntity); - System.out.println(content); - } - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); - } - } - /* - * - */ - public static void createForest(String fName,String dbName) { - try{ - DefaultHttpClient client = new DefaultHttpClient(); - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); - HttpPost post = new HttpPost("http://localhost:8002"+ "/manage/v2/forests?format=json"); - String hName =InetAddress.getLocalHost().getCanonicalHostName().toLowerCase(); - String JSONString = - "{\"database\":\""+ - dbName + - "\",\"forest-name\":\""+ - fName+ - "\",\"host\":\""+hName+"\"}" - ; - // System.out.println(JSONString); - post.addHeader("Content-type", "application/json"); - post.setEntity(new StringEntity(JSONString)); - - HttpResponse response = client.execute(post); - HttpEntity respEntity = response.getEntity(); - - if (respEntity != null) { - // EntityUtils to get the response content - String content = EntityUtils.toString(respEntity); - System.out.println(content); - } - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); - } - } - /* - * creating forests on different hosts - */ - public static void createForestonHost(String fName,String dbName,String hName) { - try{ - DefaultHttpClient client = new DefaultHttpClient(); - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); - HttpPost post = new HttpPost("http://localhost:8002"+ "/manage/v2/forests?format=json"); - String JSONString = - "{\"database\":\""+ - dbName + - "\",\"forest-name\":\""+ - fName+ - "\",\"host\":\""+hName+"\"}" - ; - // System.out.println(JSONString); - post.addHeader("Content-type", "application/json"); - post.setEntity(new StringEntity(JSONString)); - - HttpResponse response = client.execute(post); - HttpEntity respEntity = response.getEntity(); - - if (respEntity != null) { - // EntityUtils to get the response content - String content = EntityUtils.toString(respEntity); - System.out.println(content); - } - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); - } - } - - public static void assocRESTServer(String restServerName,String dbName,int restPort) { - try{ - DefaultHttpClient client = new DefaultHttpClient(); - - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); - - HttpPost post = new HttpPost("http://localhost:8002"+ "/v1/rest-apis?format=json"); - // - String JSONString = - "{ \"rest-api\": {\"database\":\""+ - dbName + - "\",\"name\":\""+ - restServerName + - "\",\"port\":\""+ - restPort+ - "\"}}"; - // System.out.println(JSONString); - post.addHeader("Content-type", "application/json"); - post.setEntity(new StringEntity(JSONString)); - - HttpResponse response = client.execute(post); - HttpEntity respEntity = response.getEntity(); - System.out.println(JSONString); - if (response.getStatusLine().getStatusCode() == 400) { - // EntityUtils to get the response content - - System.out.println("AppServer already exist, so changing the content database to new DB"); - associateRESTServerWithDB(restServerName,dbName); - } - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); - } - } - public static void associateRESTServerWithDB(String restServerName,String dbName)throws Exception{ - DefaultHttpClient client = new DefaultHttpClient(); - - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); - String body = "{\"content-database\": \""+dbName+"\",\"group-name\": \"Default\"}"; - - HttpPut put = new HttpPut("http://localhost:8002/manage/v2/servers/"+restServerName+"/properties?server-type=http"); - put.addHeader("Content-type", "application/json"); - put.setEntity(new StringEntity(body)); - - HttpResponse response2 = client.execute(put); - HttpEntity respEntity = response2.getEntity(); - if(respEntity != null){ - String content = EntityUtils.toString(respEntity); - System.out.println(content); - } - } - /* - * Creating RESTServer With default content and module database - */ - public static void createRESTServerWithDB(String restServerName,int restPort) { - try{ - DefaultHttpClient client = new DefaultHttpClient(); - - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); - HttpPost post = new HttpPost("http://localhost:8002"+ "/v1/rest-apis?format=json"); - // - String JSONString = - "{ \"rest-api\": {\"name\":\""+ - restServerName + - "\",\"port\":\""+ - restPort+ - "\"}}"; - //System.out.println(JSONString); - post.addHeader("Content-type", "application/json"); - post.setEntity(new StringEntity(JSONString)); - - HttpResponse response = client.execute(post); - HttpEntity respEntity = response.getEntity(); - - if (respEntity != null) { - // EntityUtils to get the response content - String content = EntityUtils.toString(respEntity); - System.out.println(content); - } - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); - } - } - - /* - * This function creates database,forests and REST server independently and attaches the database to the rest server - * - */ - public static void setupJavaRESTServer(String dbName, String fName, String restServerName, int restPort)throws Exception{ - - createDB(dbName); - createForest(fName,dbName); - Thread.sleep(1500); - assocRESTServer(restServerName, dbName,restPort); - createRESTUser("rest-admin","x","rest-admin"); - createRESTUser("rest-writer","x","rest-writer"); - createRESTUser("rest-reader","x","rest-reader"); - } - /* - * This function creates a REST server with default content DB, Module DB - */ - - public static void createRESTUser(String usrName, String pass, String... roleNames ){ - try{ - DefaultHttpClient client = new DefaultHttpClient(); - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); - HttpGet getrequest = new HttpGet("http://localhost:8002"+ "/manage/v2/users/"+usrName); - HttpResponse resp = client.execute(getrequest); - - if( resp.getStatusLine().getStatusCode() == 200) - { - System.out.println("User already exist"); - } - else { - System.out.println("User dont exist"); - client = new DefaultHttpClient(); - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); - - ObjectMapper mapper = new ObjectMapper(); - ObjectNode mainNode = mapper.createObjectNode(); - // ObjectNode childNode = mapper.createObjectNode(); - ArrayNode childArray = mapper.createArrayNode(); - mainNode.put("name",usrName); - mainNode.put("description", "user discription"); - mainNode.put("password", pass); - for(String rolename: roleNames) - childArray.add(rolename); - mainNode.withArray("role").addAll(childArray); - //System.out.println(type + mainNode.path("range-element-indexes").path("range-element-index").toString()); - System.out.println(mainNode.toString()); - HttpPost post = new HttpPost("http://localhost:8002"+ "/manage/v2/users?format=json"); - post.addHeader("Content-type", "application/json"); - post.setEntity(new StringEntity(mainNode.toString())); - - HttpResponse response = client.execute(post); - HttpEntity respEntity = response.getEntity(); - if( response.getStatusLine().getStatusCode() == 400) - { - System.out.println("User already exist"); - } - else if (respEntity != null) { - // EntityUtils to get the response content - String content = EntityUtils.toString(respEntity); - System.out.println(content); - } - else {System.out.print("No Proper Response");} - } - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); - } - } - - /* - * "permission": [ - { - "role-name": "dls-user", - "capability": "read" - } - */ - - public static ObjectNode getPermissionNode(String roleName, DocumentMetadataHandle.Capability... cap){ - ObjectMapper mapper= new ObjectMapper(); - ObjectNode mNode = mapper.createObjectNode(); - ArrayNode aNode = mapper.createArrayNode(); - - for(DocumentMetadataHandle.Capability c : cap){ - ObjectNode roleNode =mapper.createObjectNode(); - roleNode.put("role-name",roleName); - roleNode.put("capability", c.toString().toLowerCase()); - aNode.add(roleNode); - } - mNode.withArray("permission").addAll(aNode); - return mNode; - } - - /* - * "collection": -[ -"dadfasd", -"adfadsfads" -] - */ - public static ObjectNode getCollectionNode(String... collections){ - ObjectMapper mapper= new ObjectMapper(); - ObjectNode mNode = mapper.createObjectNode(); - ArrayNode aNode = mapper.createArrayNode(); - - for(String c : collections){ - aNode.add(c); - } - mNode.withArray("collection").addAll(aNode); - return mNode; - } - - public static void createRESTUserWithPermissions(String usrName, String pass,ObjectNode perm,ObjectNode colections, String... roleNames ){ - try{ - DefaultHttpClient client = new DefaultHttpClient(); - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); - HttpGet getrequest = new HttpGet("http://localhost:8002"+ "/manage/v2/users/"+usrName); - HttpResponse resp = client.execute(getrequest); - - if( resp.getStatusLine().getStatusCode() == 200) - { - System.out.println("User already exist"); - } - else { - System.out.println("User dont exist"); - client = new DefaultHttpClient(); - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); - - ObjectMapper mapper = new ObjectMapper(); - ObjectNode mainNode = mapper.createObjectNode(); - // ObjectNode childNode = mapper.createObjectNode(); - ArrayNode childArray = mapper.createArrayNode(); - mainNode.put("name",usrName); - mainNode.put("description", "user discription"); - mainNode.put("password", pass); - for(String rolename: roleNames) - childArray.add(rolename); - mainNode.withArray("role").addAll(childArray); - mainNode.setAll(perm); - mainNode.setAll(colections); - //System.out.println(type + mainNode.path("range-element-indexes").path("range-element-index").toString()); - System.out.println(mainNode.toString()); - HttpPost post = new HttpPost("http://localhost:8002"+ "/manage/v2/users?format=json"); - post.addHeader("Content-type", "application/json"); - post.setEntity(new StringEntity(mainNode.toString())); - - HttpResponse response = client.execute(post); - HttpEntity respEntity = response.getEntity(); - if( response.getStatusLine().getStatusCode() == 400) - { - System.out.println("Bad User creation request"); - } - else if (respEntity != null) { - // EntityUtils to get the response content - String content = EntityUtils.toString(respEntity); - System.out.println(content); - } - else {System.out.print("No Proper Response");} - } - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); - } - } - - public static void deleteRESTUser(String usrName){ - try{ - DefaultHttpClient client = new DefaultHttpClient(); - - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); - - HttpDelete delete = new HttpDelete("http://localhost:8002/manage/v2/users/"+usrName); - - HttpResponse response = client.execute(delete); - if(response.getStatusLine().getStatusCode()== 202){ - Thread.sleep(3500); - } - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); - } - - } - - public static void setupJavaRESTServerWithDB( String restServerName, int restPort)throws Exception{ - createRESTServerWithDB(restServerName, restPort); - createRESTUser("rest-admin","x","rest-admin"); - createRESTUser("rest-writer","x","rest-writer"); - createRESTUser("rest-reader","x","rest-reader"); - } - /* - * This function deletes the REST appserver along with attached content database and module database - */ - - - public static void deleteRESTServerWithDB(String restServerName) { - try{ - DefaultHttpClient client = new DefaultHttpClient(); - - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); - - HttpDelete delete = new HttpDelete("http://localhost:8002/v1/rest-apis/"+restServerName+"?include=content&include=modules"); - - HttpResponse response = client.execute(delete); - if(response.getStatusLine().getStatusCode()== 202){ - Thread.sleep(3500); - } - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); - } - } - /* - * - */ - public static void deleteRESTServer(String restServerName) { - try{ - DefaultHttpClient client = new DefaultHttpClient(); - - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); - - HttpDelete delete = new HttpDelete("http://localhost:8002/v1/rest-apis/"+restServerName+"&include=modules"); - HttpResponse response = client.execute(delete); - - if(response.getStatusLine().getStatusCode()== 202){ - Thread.sleep(3500); - waitForServerRestart(); - } - else System.out.println("Server response "+response.getStatusLine().getStatusCode()); - }catch (Exception e) { - // writing error to Log - System.out.println("Inside Deleting Rest server is throwing an error"); - e.printStackTrace(); - } - } - public static void detachForest(String dbName, String fName){ - try{ - DefaultHttpClient client = new DefaultHttpClient(); - - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); - - HttpPost post = new HttpPost("http://localhost:8002"+ "/manage/v2/forests/"+fName); - // - List urlParameters = new ArrayList(); - urlParameters.add(new BasicNameValuePair("state", "detach")); - urlParameters.add(new BasicNameValuePair("database", dbName)); - - post.setEntity(new UrlEncodedFormEntity(urlParameters)); - - HttpResponse response = client.execute(post); - HttpEntity respEntity = response.getEntity(); - - if (respEntity != null) { - // EntityUtils to get the response content - String content = EntityUtils.toString(respEntity); - System.out.println(content); - } - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); - } - } - /* - * Deleting a forest is a HTTP Delete request - * - */ - public static void deleteForest(String fName){ - try{ - DefaultHttpClient client = new DefaultHttpClient(); - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); - - HttpDelete delete = new HttpDelete("http://localhost:8002/manage/v2/forests/"+fName+"?level=full"); - client.execute(delete); - - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); - } - } - /* - * Deleting Database - * - */ - public static void deleteDB(String dbName){ - try{ - DefaultHttpClient client = new DefaultHttpClient(); - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); - - HttpDelete delete = new HttpDelete("http://localhost:8002/manage/v2/databases/"+dbName); - client.execute(delete); - - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); - } - } - - public static void clearDB(int port){ - try{ - DefaultHttpClient client = new DefaultHttpClient(); - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", port), - new UsernamePasswordCredentials("admin", "admin")); - String uri = "http://localhost:"+port+"/v1/search/"; - HttpDelete delete = new HttpDelete(uri); - client.execute(delete); - - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); - } - } - public static void waitForServerRestart() - { - try{ - int count = 0; - while(count <20){ - DefaultHttpClient client = new DefaultHttpClient(); - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8001), - new UsernamePasswordCredentials("admin", "admin")); - - count++; - try{ - HttpGet getrequest = new HttpGet("http://localhost:8001/admin/v1/timestamp"); - HttpResponse response = client.execute(getrequest); - if(response.getStatusLine().getStatusCode() == 503){Thread.sleep(4000);} - else if(response.getStatusLine().getStatusCode() == 200){ - break; - } - else { - System.out.println("Waiting for response from server, Trial :"+response.getStatusLine().getStatusCode()+count); - Thread.sleep(2000); - } - }catch(Exception e){Thread.sleep(2000);} - } - }catch(Exception e){ - System.out.println("Inside wait for server restart is throwing an error"); - e.printStackTrace(); - } - } - /* - * This function deletes rest server first and deletes forests and databases in separate calls - */ - public static void tearDownJavaRESTServer(String dbName, String [] fNames, String restServerName) throws Exception{ - - try{ - associateRESTServerWithDB(restServerName,"Documents"); - }catch(Exception e){ - System.out.println("From Deleting Rest server called funnction is throwing an error"); - e.printStackTrace(); - } - - try{ - for(int i = 0; i < fNames.length; i++){ - detachForest(dbName, fNames[i]); - } - }catch(Exception e){ - e.printStackTrace(); - } - - try{ - for(int i = 0; i < fNames.length; i++){ - deleteForest(fNames[i]); - } - }catch(Exception e){ - e.printStackTrace(); - } - - deleteDB(dbName); - } - - /* - * This function deletes rest server along with default forest and database - */ - public static void tearDownJavaRESTServerWithDB(String restServerName) throws Exception{ - - try{ - deleteRESTServerWithDB(restServerName); - waitForServerRestart(); - }catch(Exception e){ - e.printStackTrace(); - } - Thread.sleep(6000); - - } - - /* - * - * setting up AppServices configurations - * setting up database properties whose value is string - */ - public static void setDatabaseProperties(String dbName,String prop,String propValue ) throws IOException{ - InputStream jsonstream=null; - try{ - DefaultHttpClient client = new DefaultHttpClient(); - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); - HttpGet getrequest = new HttpGet("http://localhost:8002"+ "/manage/v2/databases/"+dbName+"/properties?format=json"); - HttpResponse response1 = client.execute(getrequest); - jsonstream =response1.getEntity().getContent(); - JsonNode jnode= new ObjectMapper().readTree(jsonstream); - if(!jnode.isNull()){ - ((ObjectNode)jnode).put(prop, propValue); - // System.out.println(jnode.toString()+"\n"+ response1.getStatusLine().getStatusCode()); - HttpPut put = new HttpPut("http://localhost:8002"+ "/manage/v2/databases/"+dbName+"/properties?format=json"); - put.addHeader("Content-type", "application/json"); - put.setEntity(new StringEntity(jnode.toString())); - - HttpResponse response2 = client.execute(put); - HttpEntity respEntity = response2.getEntity(); - if(respEntity != null){ - String content = EntityUtils.toString(respEntity); - System.out.println(content); - } - } - else{ - System.out.println("REST call for database properties returned NULL "); - } - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); - } - finally{ - if(jsonstream == null){} - else{ - jsonstream.close(); - } - } - } - - public static void setDatabaseProperties(String dbName,String prop,boolean propValue ) throws IOException{ - InputStream jsonstream=null; - try{ - DefaultHttpClient client = new DefaultHttpClient(); - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); - HttpGet getrequest = new HttpGet("http://localhost:8002"+ "/manage/v2/databases/"+dbName+"/properties?format=json"); - HttpResponse response1 = client.execute(getrequest); - jsonstream =response1.getEntity().getContent(); - JsonNode jnode= new ObjectMapper().readTree(jsonstream); - if(!jnode.isNull()){ - ((ObjectNode)jnode).put(prop, propValue) ; - // System.out.println(jnode.toString()+"\n"+ response1.getStatusLine().getStatusCode()); - HttpPut put = new HttpPut("http://localhost:8002"+ "/manage/v2/databases/"+dbName+"/properties?format=json"); - put.addHeader("Content-type", "application/json"); - put.setEntity(new StringEntity(jnode.toString())); - - HttpResponse response2 = client.execute(put); - HttpEntity respEntity = response2.getEntity(); - if(respEntity != null){ - String content = EntityUtils.toString(respEntity); - System.out.println(content); - } - } - else{ - System.out.println("REST call for database properties returned NULL "); - } - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); - } - finally{ - if(jsonstream == null){} - else{ - jsonstream.close(); - } - } - } - - /* - * This Method takes the root property name and object node under it - * if root propname exist and equals to null then it just add the object node under root property name else if it has an existing sub property name then it adds - * elements to that array - */ - public static void setDatabaseProperties(String dbName,String propName, ObjectNode objNode ) throws IOException{ - InputStream jsonstream=null; - try{ - DefaultHttpClient client = new DefaultHttpClient(); - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); - HttpGet getrequest = new HttpGet("http://localhost:8002"+ "/manage/v2/databases/"+dbName+"/properties?format=json"); - HttpResponse response1 = client.execute(getrequest); - jsonstream =response1.getEntity().getContent(); - ObjectMapper mapper = new ObjectMapper(); - JsonNode jnode= mapper.readTree(jsonstream); - if(!jnode.isNull()){ - - if(!jnode.has(propName)){ - ((ObjectNode)jnode).putArray(propName).addAll(objNode.withArray(propName)); - // System.out.println("when Node is null"+propName + objNode.toString()); - } - else{ - if(!jnode.path(propName).isArray()){ - System.out.println("property is not array"); - ((ObjectNode)jnode).putAll(objNode); - } - else{ - JsonNode member = jnode.withArray(propName); - if(objNode.path(propName).isArray()){ - ((ArrayNode)member).addAll(objNode.withArray(propName)); - // System.out.println("when Node is not null"+ propName + objNode.withArray(propName).toString()); - } - } - } - - HttpPut put = new HttpPut("http://localhost:8002"+ "/manage/v2/databases/"+dbName+"/properties?format=json"); - put.addHeader("Content-type", "application/json"); - put.setEntity(new StringEntity(jnode.toString())); - - HttpResponse response2 = client.execute(put); - HttpEntity respEntity = response2.getEntity(); - if(respEntity != null){ - String content = EntityUtils.toString(respEntity); - System.out.println(content); - } - } - else{ - System.out.println("REST call for database properties returned NULL \n"+jnode.toString()+"\n"+ response1.getStatusLine().getStatusCode()); - } - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); - } - finally{ - if(jsonstream == null){} - else{ - jsonstream.close(); - } - } - } - - public static void enableCollectionLexicon(String dbName) throws Exception{ - setDatabaseProperties(dbName,"collection-lexicon",true ); - } - /* - * "word-lexicons": [ - "http:\/\/marklogic.com\/collation\/" - ] - } - */ - public static void enableWordLexicon(String dbName) throws Exception{ - ObjectMapper mapper = new ObjectMapper(); - ObjectNode childNode = mapper.createObjectNode(); - ArrayNode childArray = mapper.createArrayNode(); - childArray.add("http://marklogic.com/collation/"); - childNode.putArray("word-lexicon").addAll(childArray); - setDatabaseProperties(dbName,"word-lexicons",childNode); - - } - public static void enableTrailingWildcardSearches(String dbName) throws Exception{ - setDatabaseProperties(dbName,"trailing-wildcard-searches",true ); - } - - public static void setMaintainLastModified(String dbName,boolean opt) throws Exception{ - setDatabaseProperties(dbName,"maintain-last-modified",opt); - } - public static void setAutomaticDirectoryCreation(String dbName, String opt) throws Exception{ - setDatabaseProperties(dbName,"directory-creation",opt); - } - /* - * This function constructs a range element index with default collation,range-value-positions and invalid values - * - */ - public static void addRangeElementIndex(String dbName, String type, String namespace, String localname) throws Exception{ - ObjectMapper mapper = new ObjectMapper(); - ObjectNode mainNode = mapper.createObjectNode(); - // ObjectNode childNode = mapper.createObjectNode(); - ArrayNode childArray = mapper.createArrayNode(); - ObjectNode childNodeObject = mapper.createObjectNode(); - childNodeObject.put( "scalar-type", type); - childNodeObject.put( "namespace-uri", namespace); - childNodeObject.put( "localname", localname); - childNodeObject.put( "collation", ""); - childNodeObject.put("range-value-positions", false); - childNodeObject.put("invalid-values", "reject"); - childArray.add(childNodeObject); - mainNode.putArray("range-element-index").addAll(childArray); - // mainNode.put("range-element-indexes", childNode); - // System.out.println(type + mainNode.path("range-element-indexes").path("range-element-index").toString()); - setDatabaseProperties(dbName,"range-element-index",mainNode); - - } - - - /* - * This is a overloaded function constructs a range element index with default range-value-positions and invalid values - * - */ - public static void addRangeElementIndex(String dbName, String type, String namespace, String localname, String collation) throws Exception{ - ObjectMapper mapper = new ObjectMapper(); - ObjectNode mainNode = mapper.createObjectNode(); - // ObjectNode childNode = mapper.createObjectNode(); - ArrayNode childArray = mapper.createArrayNode(); - ObjectNode childNodeObject = mapper.createObjectNode(); - childNodeObject.put( "scalar-type", type); - childNodeObject.put( "namespace-uri", namespace); - childNodeObject.put( "localname", localname); - childNodeObject.put( "collation", collation); - childNodeObject.put("range-value-positions", false); - childNodeObject.put("invalid-values", "reject"); - childArray.add(childNodeObject); - mainNode.putArray("range-element-index").addAll(childArray); - - // System.out.println(type + mainNode.path("range-element-indexes").path("range-element-index").toString()); - setDatabaseProperties(dbName,"range-element-index",mainNode); - - } - - /* - * "scalar-type": "int", - "collation": "", - "parent-namespace-uri": "", - "parent-localname": "test", - "namespace-uri": "", - "localname": "testAttr", - "range-value-positions": false, - "invalid-values": "reject" - */ - - public static void addRangeElementAttributeIndex(String dbName, String type, String parentnamespace, String parentlocalname, String namespace, String localname, String collation) throws Exception{ - ObjectMapper mapper = new ObjectMapper(); - // ObjectNode mainNode = mapper.createObjectNode(); - ObjectNode childNode = mapper.createObjectNode(); - ArrayNode childArray = mapper.createArrayNode(); - ObjectNode childNodeObject = mapper.createObjectNode(); - childNodeObject.put( "scalar-type", type); - childNodeObject.put( "collation", collation); - childNodeObject.put( "parent-namespace-uri", parentnamespace); - childNodeObject.put( "parent-localname", parentlocalname); - childNodeObject.put( "namespace-uri", namespace); - childNodeObject.put( "localname", localname); - - childNodeObject.put("range-value-positions", false); - childNodeObject.put("invalid-values", "reject"); - childArray.add(childNodeObject); - childNode.putArray("range-element-attribute-index").addAll(childArray); - - // mainNode.put("range-element-attribute-indexes", childNode); - // System.out.println(type + mainNode.path("range-element-attribute-indexes").path("range-element-attribute-index").toString()); - setDatabaseProperties(dbName,"range-element-attribute-index",childNode); - - } - /* - * Overloaded function with default collation - */ - public static void addRangeElementAttributeIndex(String dbName, String type, String parentnamespace, String parentlocalname, String namespace, String localname) throws Exception{ - ObjectMapper mapper = new ObjectMapper(); - //ObjectNode mainNode = mapper.createObjectNode(); - ObjectNode childNode = mapper.createObjectNode(); - ArrayNode childArray = mapper.createArrayNode(); - ObjectNode childNodeObject = mapper.createObjectNode(); - childNodeObject.put( "scalar-type", type); - childNodeObject.put( "collation", ""); - childNodeObject.put( "parent-namespace-uri", parentnamespace); - childNodeObject.put( "parent-localname", parentlocalname); - childNodeObject.put( "namespace-uri", namespace); - childNodeObject.put( "localname", localname); - - childNodeObject.put("range-value-positions", false); - childNodeObject.put("invalid-values", "reject"); - childArray.add(childNodeObject); - childNode.putArray("range-element-attribute-index").addAll(childArray); - // mainNode.put("range-element-attribute-indexes", childNode); - // System.out.println(type + mainNode.path("range-element-attribute-indexes").path("range-element-attribute-index").toString()); - setDatabaseProperties(dbName,"range-element-attribute-index",childNode); - - } - /* - * "range-path-indexes": { - "range-path-index": [ - { - "scalar-type": "string", - "collation": "http:\/\/marklogic.com\/collation\/", - "path-expression": "\/Employee\/fn", - "range-value-positions": false, - "invalid-values": "reject" - } - ] - } - */ - public static void addRangePathIndex(String dbName, String type, String pathexpr, String collation, String invalidValues) throws Exception{ - ObjectMapper mapper = new ObjectMapper(); - // ObjectNode mainNode = mapper.createObjectNode(); - ObjectNode childNode = mapper.createObjectNode(); - ArrayNode childArray = mapper.createArrayNode(); - ObjectNode childNodeObject = mapper.createObjectNode(); - childNodeObject.put( "scalar-type", type); - childNodeObject.put( "collation", collation); - childNodeObject.put( "path-expression", pathexpr); - childNodeObject.put("range-value-positions", false); - childNodeObject.put("invalid-values", invalidValues); - childArray.add(childNodeObject); - childNode.putArray("range-path-index").addAll(childArray); - // mainNode.put("range-path-indexes", childNode); - // System.out.println(type + mainNode.path("range-path-indexes").path("range-path-index").toString()); - setDatabaseProperties(dbName,"range-path-index",childNode); - - } - public static void addGeospatialElementIndexes(String dbName,String localname,String namespace,String coordinateSystem,String pointFormat,boolean rangeValuePositions,String invalidValues) throws Exception{ - ObjectMapper mapper = new ObjectMapper(); - // ObjectNode mainNode = mapper.createObjectNode(); - ObjectNode childNode = mapper.createObjectNode(); - ArrayNode childArray = mapper.createArrayNode(); - ObjectNode childNodeObject = mapper.createObjectNode(); - childNodeObject.put( "namespace-uri", namespace); - childNodeObject.put( "localname", localname); - childNodeObject.put( "coordinate-system", coordinateSystem); - childNodeObject.put("range-value-positions", false); - childNodeObject.put("invalid-values", invalidValues); - childNodeObject.put("point-format",pointFormat); - childArray.add(childNodeObject); - childNode.putArray("geospatial-element-index").addAll(childArray); - // mainNode.put("geospatial-element-indexes", childNode); - // System.out.println(type + mainNode.path("range-path-indexes").path("range-path-index").toString()); - setDatabaseProperties(dbName,"geospatial-element-index",childNode); - } - public static void addGeoSpatialElementChildIndexes(String dbName,String parentNamespaceUri,String parentLocalName,String namespace,String localname,String coordinateSystem,String pointFormat,boolean rangeValuePositions,String invalidValues) throws Exception{ - ObjectMapper mapper = new ObjectMapper(); - // ObjectNode mainNode = mapper.createObjectNode(); - ObjectNode childNode = mapper.createObjectNode(); - ArrayNode childArray = mapper.createArrayNode(); - ObjectNode childNodeObject = mapper.createObjectNode(); - childNodeObject.put( "parent-namespace-uri", parentNamespaceUri); - childNodeObject.put( "parent-localname", parentLocalName); - childNodeObject.put( "namespace-uri", namespace); - childNodeObject.put( "localname", localname); - childNodeObject.put( "coordinate-system", coordinateSystem); - childNodeObject.put("range-value-positions", false); - childNodeObject.put("invalid-values", invalidValues); - childNodeObject.put("point-format",pointFormat); - childArray.add(childNodeObject); - childNode.putArray("geospatial-element-child-index").addAll(childArray); - // mainNode.put("geospatial-element-child-indexes", childNode); - // System.out.println(type + mainNode.path("range-path-indexes").path("range-path-index").toString()); - setDatabaseProperties(dbName,"geospatial-element-child-index",childNode); - } - public static void addGeospatialElementPairIndexes(String dbName,String parentNamespaceUri,String parentLocalName,String latNamespace,String latLocalname,String longNamespace,String longLocalname,String coordinateSystem,boolean rangeValuePositions,String invalidValues) throws Exception{ - ObjectMapper mapper = new ObjectMapper(); - // ObjectNode mainNode = mapper.createObjectNode(); - ObjectNode childNode = mapper.createObjectNode(); - ArrayNode childArray = mapper.createArrayNode(); - ObjectNode childNodeObject = mapper.createObjectNode(); - childNodeObject.put( "parent-namespace-uri", parentNamespaceUri); - childNodeObject.put( "parent-localname", parentLocalName); - childNodeObject.put( "latitude-namespace-uri", latNamespace); - childNodeObject.put( "latitude-localname", latLocalname); - childNodeObject.put( "longitude-namespace-uri", latNamespace); - childNodeObject.put( "longitude-localname", longLocalname); - childNodeObject.put( "coordinate-system", coordinateSystem); - childNodeObject.put("range-value-positions", false); - childNodeObject.put("invalid-values", invalidValues); - childArray.add(childNodeObject); - childNode.putArray("geospatial-element-pair-index").addAll(childArray); - // mainNode.put("geospatial-element-pair-indexes", childNode); - // System.out.println(type + mainNode.path("range-path-indexes").path("range-path-index").toString()); - setDatabaseProperties(dbName,"geospatial-element-pair-index",childNode); - } - public static void addGeospatialElementAttributePairIndexes(String dbName,String parentNamespaceUri,String parentLocalName,String latNamespace,String latLocalname,String longNamespace,String longLocalname,String coordinateSystem,boolean rangeValuePositions,String invalidValues) throws Exception{ - ObjectMapper mapper = new ObjectMapper(); - // ObjectNode mainNode = mapper.createObjectNode(); - ObjectNode childNode = mapper.createObjectNode(); - ArrayNode childArray = mapper.createArrayNode(); - ObjectNode childNodeObject = mapper.createObjectNode(); - childNodeObject.put( "parent-namespace-uri", parentNamespaceUri); - childNodeObject.put( "parent-localname", parentLocalName); - childNodeObject.put( "latitude-namespace-uri", latNamespace); - childNodeObject.put( "latitude-localname", latLocalname); - childNodeObject.put( "longitude-namespace-uri", latNamespace); - childNodeObject.put( "longitude-localname", longLocalname); - childNodeObject.put( "coordinate-system", coordinateSystem); - childNodeObject.put("range-value-positions", false); - childNodeObject.put("invalid-values", invalidValues); - childArray.add(childNodeObject); - childNode.putArray("geospatial-element-attribute-pair-index").addAll(childArray); - // mainNode.put("geospatial-element-attribute-pair-indexes", childNode); - // System.out.println(type + mainNode.path("range-path-indexes").path("range-path-index").toString()); - setDatabaseProperties(dbName,"geospatial-element-attribute-pair-index",childNode); - } - public static void addGeospatialPathIndexes(String dbName,String pathExpression,String coordinateSystem,String pointFormat,boolean rangeValuePositions,String invalidValues) throws Exception{ - ObjectMapper mapper = new ObjectMapper(); - // ObjectNode mainNode = mapper.createObjectNode(); - ObjectNode childNode = mapper.createObjectNode(); - ArrayNode childArray = mapper.createArrayNode(); - ObjectNode childNodeObject = mapper.createObjectNode(); - childNodeObject.put( "path-expression", pathExpression); - childNodeObject.put( "coordinate-system", coordinateSystem); - childNodeObject.put("range-value-positions", false); - childNodeObject.put("invalid-values", invalidValues); - childNodeObject.put("point-format",pointFormat); - childArray.add(childNodeObject); - childNode.putArray("geospatial-path-index").addAll(childArray); - // mainNode.put("geospatial-path-indexes", childNode); - // System.out.println(type + mainNode.path("range-path-indexes").path("range-path-index").toString()); - setDatabaseProperties(dbName,"geospatial-path-index",childNode); - } - /* - * Add field will include root and it appends field to an existing fields - * "fields":{ - "field":[ - { - "field-name": "", - "include-root": true, - "included-elements": null, - "excluded-elements": null - } - , - { - "field-name": "para", - "include-root": false, - "included-elements": null, - "excluded-elements": null, - "tokenizer-overrides": null - } - ] - } - */ - public static void addField(String dbName, String fieldName) throws Exception{ - ObjectMapper mapper = new ObjectMapper(); - // ObjectNode mainNode = mapper.createObjectNode(); - ObjectNode childNode = mapper.createObjectNode(); - ArrayNode arrNode = mapper.createArrayNode(); - ObjectNode childNodeObject = mapper.createObjectNode(); - childNodeObject.put( "field-name", fieldName); - childNodeObject.put( "include-root", true); - childNodeObject.putNull( "included-elements"); - childNodeObject.putNull( "excluded-elements"); - childNodeObject.putNull( "tokenizer-overrides"); - arrNode.add(childNodeObject); - childNode.putArray("field").addAll(arrNode); - // mainNode.put("fields", childNode); - // System.out.println("Entered field to make it true"); - setDatabaseProperties(dbName,"field",childNode); - - } - public static void addFieldExcludeRoot(String dbName, String fieldName) throws Exception{ - ObjectMapper mapper = new ObjectMapper(); - // ObjectNode mainNode = mapper.createObjectNode(); - ObjectNode childNode = mapper.createObjectNode(); - ArrayNode arrNode = mapper.createArrayNode(); - ObjectNode childNodeObject = mapper.createObjectNode(); - childNodeObject.put( "field-name", fieldName); - childNodeObject.put( "include-root", false); - childNodeObject.putNull( "included-elements"); - childNodeObject.putNull( "excluded-elements"); - childNodeObject.putNull( "tokenizer-overrides"); - arrNode.add(childNodeObject); - childNode.putArray("field").addAll(arrNode); - // mainNode.put("fields", childNode); - // System.out.println( childNode.toString()); - setDatabaseProperties(dbName,"field",childNode); - - } - public static void addBuiltInGeoIndex (String dbName)throws Exception { - addGeospatialElementIndexes(dbName,"g-elem-point","","wgs84","point",false,"reject"); - addGeoSpatialElementChildIndexes(dbName,"","g-elem-child-parent","","g-elem-child-point","wgs84","point",false,"reject"); - addGeospatialElementPairIndexes(dbName,"","g-elem-pair","","lat","","long","wgs84",false,"reject"); - addGeospatialElementAttributePairIndexes(dbName,"","g-attr-pair","","lat","","long","wgs84",false,"reject"); - addGeospatialPathIndexes(dbName,"/doc/g-elem-point","wgs84","point",false,"ignore"); - } - - /* - This method is trying to add include element or exclude elements to the existing fields - * - */ - public static void setDatabaseFieldProperties(String dbName,String field_name, String propName, ObjectNode objNode ) throws IOException{ - InputStream jsonstream=null; - try{ - DefaultHttpClient client = new DefaultHttpClient(); - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); - HttpGet getrequest = new HttpGet("http://localhost:8002"+ "/manage/v2/databases/"+dbName+"/properties?format=json"); - HttpResponse response1 = client.execute(getrequest); - jsonstream =response1.getEntity().getContent(); - ObjectMapper mapper = new ObjectMapper(); - JsonNode jnode= mapper.readTree(jsonstream); - if(!jnode.isNull()&& jnode.has("field")){ - JsonNode fieldNode = jnode.withArray("field"); - Iterator fnode = fieldNode.elements(); - while(fnode.hasNext()) { - JsonNode fnchild =fnode.next(); - if((fnchild.path("field-name").asText()).equals(field_name)){ - // System.out.println("Hurray" +fnchild.has(propName)); - if(!fnchild.has(propName)){ - ((ObjectNode)fnchild).putArray(propName).addAll(objNode.withArray(propName)); - System.out.println("Adding child array include node" + jnode.toString()); - } - else{ - JsonNode member = fnchild.withArray(propName); - ((ArrayNode)member).addAll(objNode.withArray(propName)); - } - - } - } - - HttpPut put = new HttpPut("http://localhost:8002"+ "/manage/v2/databases/"+dbName+"/properties?format=json"); - put.addHeader("Content-type", "application/json"); - put.setEntity(new StringEntity(jnode.toString())); - - HttpResponse response2 = client.execute(put); - HttpEntity respEntity = response2.getEntity(); - if(respEntity != null){ - String content = EntityUtils.toString(respEntity); - System.out.println(content); - } - } - else{ - System.out.println("REST call for database properties returned NULL \n"+jnode.toString()+"\n"+ response1.getStatusLine().getStatusCode()); - } - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); - } - finally{ - if(jsonstream == null){} - else{ - jsonstream.close(); - } - } - } - - public static void includeElementField(String dbName, String field_name, String namespace, String elementName) throws Exception{ - ObjectMapper mapper = new ObjectMapper(); - // ObjectNode mainNode = mapper.createObjectNode(); - ObjectNode childNode = mapper.createObjectNode(); - ArrayNode arrNode = mapper.createArrayNode(); - ObjectNode childNodeObject = mapper.createObjectNode(); - childNodeObject.put( "namespace-uri", namespace); - childNodeObject.put( "localname", elementName); - childNodeObject.put("weight", 1.0); - arrNode.add(childNodeObject); - childNode.putArray("included-element").addAll(arrNode); - // mainNode.put("included-elements", childNode); - System.out.println( childNode.toString()); - setDatabaseFieldProperties(dbName,field_name,"included-element",childNode); - - } - public static void includeElementFieldWithWeight(String dbName, String field_name, String namespace, String elementName, double weight) throws Exception{ - - ObjectMapper mapper = new ObjectMapper(); - // ObjectNode mainNode = mapper.createObjectNode(); - ObjectNode childNode = mapper.createObjectNode(); - ArrayNode arrNode = mapper.createArrayNode(); - ObjectNode childNodeObject = mapper.createObjectNode(); - childNodeObject.put( "namespace-uri", namespace); - childNodeObject.put( "localname", elementName); - childNodeObject.put("weight", weight); - arrNode.add(childNodeObject); - childNode.putArray("included-element").addAll(arrNode); - // mainNode.put("included-elements", childNode); - // System.out.println( childNode.toString()); - setDatabaseFieldProperties(dbName,field_name,"included-element",childNode); - - } - public static void setupAppServicesConstraint(String dbName) throws Exception { - enableCollectionLexicon(dbName); - enableWordLexicon(dbName); - addRangeElementIndex(dbName, "date", "http://purl.org/dc/elements/1.1/", "date"); - addRangeElementIndex(dbName, "int", "", "popularity"); - addRangeElementIndex(dbName, "int", "http://test.tups.com", "rate"); - addRangeElementIndex(dbName, "decimal", "http://test.aggr.com", "score"); - addRangeElementIndex(dbName, "string", "", "title", "http://marklogic.com/collation/"); - addRangeElementAttributeIndex(dbName, "decimal", "http://cloudbank.com", "price", "", "amt", "http://marklogic.com/collation/"); - enableTrailingWildcardSearches(dbName); - addFieldExcludeRoot(dbName, "para"); - includeElementFieldWithWeight(dbName, "para", "", "p", 5); - addRangePathIndex(dbName, "string", "/Employee/fn", "http://marklogic.com/collation/", "ignore"); - addRangePathIndex(dbName, "int", "/root/popularity", "", "ignore"); - addRangePathIndex(dbName, "decimal", "//@amt", "", "ignore"); - } - public static void setupAppServicesGeoConstraint(String dbName) throws Exception { - enableCollectionLexicon(dbName); - addRangeElementIndex(dbName, "dateTime", "", "bday", "http://marklogic.com/collation/"); - addRangeElementIndex(dbName, "int", "", "height1", "http://marklogic.com/collation/"); - addRangeElementIndex(dbName, "int", "", "height2", "http://marklogic.com/collation/"); - addRangePathIndex(dbName, "string", "/doc/name", "http://marklogic.com/collation/", "ignore"); - addField(dbName, "description"); - includeElementField(dbName, "description", "", "description"); - addBuiltInGeoIndex(dbName); - - } - public static void loadBug18993(){ - try{ - DefaultHttpClient client = new DefaultHttpClient(); - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8011), - new UsernamePasswordCredentials("admin", "admin")); - String document ="a space b"; - String perm = "perm:rest-writer=read&perm:rest-writer=insert&perm:rest-writer=update&perm:rest-writer=execute"; - HttpPut put = new HttpPut("http://localhost:8011/v1/documents?uri=/a%20b&"+perm); - put.addHeader("Content-type", "application/xml"); - put.setEntity(new StringEntity(document)); - HttpResponse response = client.execute(put); - HttpEntity respEntity = response.getEntity(); - if(respEntity != null){ - String content = EntityUtils.toString(respEntity); - System.out.println(content); - } - }catch (Exception e) { - // writing error to Log - e.printStackTrace(); - } - - } - - public static void setAuthentication(String level,String restServerName) throws ClientProtocolException, IOException - { - DefaultHttpClient client = new DefaultHttpClient(); - - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); - String body = "{\"authentication\": \""+level+"\"}"; - - HttpPut put = new HttpPut("http://localhost:8002/manage/v2/servers/"+restServerName+"/properties?server-type=http&group-id=Default"); - put.addHeader("Content-type", "application/json"); - put.setEntity(new StringEntity(body)); - - HttpResponse response2 = client.execute(put); - HttpEntity respEntity = response2.getEntity(); - if(respEntity != null){ - String content = EntityUtils.toString(respEntity); - System.out.println(content); - } - } - public static void setDefaultUser(String usr,String restServerName) throws ClientProtocolException, IOException { - - DefaultHttpClient client = new DefaultHttpClient(); - - client.getCredentialsProvider().setCredentials( - new AuthScope("localhost", 8002), - new UsernamePasswordCredentials("admin", "admin")); - String body = "{\"default-user\": \""+usr+"\"}"; - - HttpPut put = new HttpPut("http://localhost:8002/manage/v2/servers/"+restServerName+"/properties?server-type=http&group-id=Default"); - put.addHeader("Content-type", "application/json"); - put.setEntity(new StringEntity(body)); - - HttpResponse response2 = client.execute(put); - HttpEntity respEntity = response2.getEntity(); - if(respEntity != null){ - String content = EntityUtils.toString(respEntity); - System.out.println(content); - } - } - public static void setupServerRequestLogging(DatabaseClient client,boolean flag) throws Exception { - ServerConfigurationManager scm =client.newServerConfigManager(); - scm.readConfiguration(); - scm.setServerRequestLogging(flag); - scm.writeConfiguration(); - } +package com.marklogic.javaclient; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.util.*; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.NameValuePair; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.client.entity.*; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Iterator; +import java.util.List; +import java.util.ArrayList; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.admin.ServerConfigurationManager; +import com.marklogic.client.io.DocumentMetadataHandle; + +import java.net.InetAddress; + +import org.json.JSONObject; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * @author gvaidees + * + */ +public abstract class ConnectedRESTQA { + + /** + * Use Rest call to create a database. + * @param dbName + */ + + public static void createDB(String dbName) { + try { + + DefaultHttpClient client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + + HttpPost post = new HttpPost("http://localhost:8002"+ "/manage/v2/databases?format=json"); + String JSONString = "[{\"name\":\""+ dbName + "\"}]"; + + post.addHeader("Content-type", "application/json"); + post.setEntity( new StringEntity(JSONString)); + + + HttpResponse response = client.execute(post); + HttpEntity respEntity = response.getEntity(); + + if (respEntity != null) { + // EntityUtils to get the response content + String content = EntityUtils.toString(respEntity); + System.out.println(content); + } + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } + } + /* + * + */ + public static void createForest(String fName,String dbName) { + try{ + DefaultHttpClient client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + HttpPost post = new HttpPost("http://localhost:8002"+ "/manage/v2/forests?format=json"); + String hName =InetAddress.getLocalHost().getCanonicalHostName().toLowerCase(); + String JSONString = + "{\"database\":\""+ + dbName + + "\",\"forest-name\":\""+ + fName+ + "\",\"host\":\""+hName+"\"}" + ; + // System.out.println(JSONString); + post.addHeader("Content-type", "application/json"); + post.setEntity(new StringEntity(JSONString)); + + HttpResponse response = client.execute(post); + HttpEntity respEntity = response.getEntity(); + + if (respEntity != null) { + // EntityUtils to get the response content + String content = EntityUtils.toString(respEntity); + System.out.println(content); + } + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } + } + /* + * creating forests on different hosts + */ + public static void createForestonHost(String fName,String dbName,String hName) { + try{ + DefaultHttpClient client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + HttpPost post = new HttpPost("http://localhost:8002"+ "/manage/v2/forests?format=json"); + String JSONString = + "{\"database\":\""+ + dbName + + "\",\"forest-name\":\""+ + fName+ + "\",\"host\":\""+hName+"\"}" + ; + // System.out.println(JSONString); + post.addHeader("Content-type", "application/json"); + post.setEntity(new StringEntity(JSONString)); + + HttpResponse response = client.execute(post); + HttpEntity respEntity = response.getEntity(); + + if (respEntity != null) { + // EntityUtils to get the response content + String content = EntityUtils.toString(respEntity); + System.out.println(content); + } + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } + } + + public static void assocRESTServer(String restServerName,String dbName,int restPort) { + try{ + DefaultHttpClient client = new DefaultHttpClient(); + + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + + HttpPost post = new HttpPost("http://localhost:8002"+ "/v1/rest-apis?format=json"); + // + String JSONString = + "{ \"rest-api\": {\"database\":\""+ + dbName + + "\",\"name\":\""+ + restServerName + + "\",\"port\":\""+ + restPort+ + "\"}}"; + // System.out.println(JSONString); + post.addHeader("Content-type", "application/json"); + post.setEntity(new StringEntity(JSONString)); + + HttpResponse response = client.execute(post); + System.out.println(JSONString); + if (response.getStatusLine().getStatusCode() == 400) { + // EntityUtils to get the response content + + System.out.println("AppServer already exist, so changing the content database to new DB"); + associateRESTServerWithDB(restServerName,dbName); + } + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } + } + public static void associateRESTServerWithDB(String restServerName,String dbName)throws Exception{ + DefaultHttpClient client = new DefaultHttpClient(); + + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + String body = "{\"content-database\": \""+dbName+"\",\"group-name\": \"Default\"}"; + + HttpPut put = new HttpPut("http://localhost:8002/manage/v2/servers/"+restServerName+"/properties?server-type=http"); + put.addHeader("Content-type", "application/json"); + put.setEntity(new StringEntity(body)); + + HttpResponse response2 = client.execute(put); + HttpEntity respEntity = response2.getEntity(); + if(respEntity != null){ + String content = EntityUtils.toString(respEntity); + System.out.println(content); + } + } + /* + * Creating RESTServer With default content and module database + */ + public static void createRESTServerWithDB(String restServerName,int restPort) { + try{ + DefaultHttpClient client = new DefaultHttpClient(); + + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + HttpPost post = new HttpPost("http://localhost:8002"+ "/v1/rest-apis?format=json"); + // + String JSONString = + "{ \"rest-api\": {\"name\":\""+ + restServerName + + "\",\"port\":\""+ + restPort+ + "\"}}"; + //System.out.println(JSONString); + post.addHeader("Content-type", "application/json"); + post.setEntity(new StringEntity(JSONString)); + + HttpResponse response = client.execute(post); + HttpEntity respEntity = response.getEntity(); + + if (respEntity != null) { + // EntityUtils to get the response content + String content = EntityUtils.toString(respEntity); + System.out.println(content); + } + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } + } + + /* + * This function creates database,forests and REST server independently and attaches the database to the rest server + * + */ + public static void setupJavaRESTServer(String dbName, String fName, String restServerName, int restPort)throws Exception{ + + createDB(dbName); + createForest(fName,dbName); + Thread.sleep(1500); + assocRESTServer(restServerName, dbName,restPort); + createRESTUser("rest-admin","x","rest-admin"); + createRESTUser("rest-writer","x","rest-writer"); + createRESTUser("rest-reader","x","rest-reader"); + } + /* + * This function creates a REST server with default content DB, Module DB + */ + + public static void createRESTUser(String usrName, String pass, String... roleNames ){ + try{ + DefaultHttpClient client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + HttpGet getrequest = new HttpGet("http://localhost:8002"+ "/manage/v2/users/"+usrName); + HttpResponse resp = client.execute(getrequest); + + if( resp.getStatusLine().getStatusCode() == 200) + { + System.out.println("User already exist"); + } + else { + System.out.println("User dont exist"); + client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + + ObjectMapper mapper = new ObjectMapper(); + ObjectNode mainNode = mapper.createObjectNode(); + // ObjectNode childNode = mapper.createObjectNode(); + ArrayNode childArray = mapper.createArrayNode(); + mainNode.put("name",usrName); + mainNode.put("description", "user discription"); + mainNode.put("password", pass); + for(String rolename: roleNames) + childArray.add(rolename); + mainNode.withArray("role").addAll(childArray); + //System.out.println(type + mainNode.path("range-element-indexes").path("range-element-index").toString()); + System.out.println(mainNode.toString()); + HttpPost post = new HttpPost("http://localhost:8002"+ "/manage/v2/users?format=json"); + post.addHeader("Content-type", "application/json"); + post.setEntity(new StringEntity(mainNode.toString())); + + HttpResponse response = client.execute(post); + HttpEntity respEntity = response.getEntity(); + if( response.getStatusLine().getStatusCode() == 400) + { + System.out.println("User already exist"); + } + else if (respEntity != null) { + // EntityUtils to get the response content + String content = EntityUtils.toString(respEntity); + System.out.println(content); + } + else {System.out.println("No Proper Response");} + } + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } + } + + /* + * "permission": [ + { + "role-name": "dls-user", + "capability": "read" + } + */ + + public static ObjectNode getPermissionNode(String roleName, DocumentMetadataHandle.Capability... cap){ + ObjectMapper mapper= new ObjectMapper(); + ObjectNode mNode = mapper.createObjectNode(); + ArrayNode aNode = mapper.createArrayNode(); + + for(DocumentMetadataHandle.Capability c : cap){ + ObjectNode roleNode =mapper.createObjectNode(); + roleNode.put("role-name",roleName); + roleNode.put("capability", c.toString().toLowerCase()); + aNode.add(roleNode); + } + mNode.withArray("permission").addAll(aNode); + return mNode; + } + + /* + * "collection": +[ +"dadfasd", +"adfadsfads" +] + */ + public static ObjectNode getCollectionNode(String... collections){ + ObjectMapper mapper= new ObjectMapper(); + ObjectNode mNode = mapper.createObjectNode(); + ArrayNode aNode = mapper.createArrayNode(); + + for(String c : collections){ + aNode.add(c); + } + mNode.withArray("collection").addAll(aNode); + return mNode; + } + + public static void createRESTUserWithPermissions(String usrName, String pass,ObjectNode perm,ObjectNode colections, String... roleNames ){ + try{ + DefaultHttpClient client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + HttpGet getrequest = new HttpGet("http://localhost:8002"+ "/manage/v2/users/"+usrName); + HttpResponse resp = client.execute(getrequest); + + if( resp.getStatusLine().getStatusCode() == 200) + { + System.out.println("User already exist"); + } + else { + System.out.println("User dont exist"); + client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + + ObjectMapper mapper = new ObjectMapper(); + ObjectNode mainNode = mapper.createObjectNode(); + // ObjectNode childNode = mapper.createObjectNode(); + ArrayNode childArray = mapper.createArrayNode(); + mainNode.put("name",usrName); + mainNode.put("description", "user discription"); + mainNode.put("password", pass); + for(String rolename: roleNames) + childArray.add(rolename); + mainNode.withArray("role").addAll(childArray); + mainNode.setAll(perm); + mainNode.setAll(colections); + //System.out.println(type + mainNode.path("range-element-indexes").path("range-element-index").toString()); + System.out.println(mainNode.toString()); + HttpPost post = new HttpPost("http://localhost:8002"+ "/manage/v2/users?format=json"); + post.addHeader("Content-type", "application/json"); + post.setEntity(new StringEntity(mainNode.toString())); + + HttpResponse response = client.execute(post); + HttpEntity respEntity = response.getEntity(); + if( response.getStatusLine().getStatusCode() == 400) + { + System.out.println("Bad User creation request"); + } + else if (respEntity != null) { + // EntityUtils to get the response content + String content = EntityUtils.toString(respEntity); + System.out.println(content); + } + else {System.out.println("No Proper Response");} + } + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } + } + + public static void deleteRESTUser(String usrName){ + try{ + DefaultHttpClient client = new DefaultHttpClient(); + + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + + HttpDelete delete = new HttpDelete("http://localhost:8002/manage/v2/users/"+usrName); + + HttpResponse response = client.execute(delete); + if(response.getStatusLine().getStatusCode()== 202){ + Thread.sleep(3500); + } + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } + + } + + public static void setupJavaRESTServerWithDB( String restServerName, int restPort)throws Exception{ + createRESTServerWithDB(restServerName, restPort); + createRESTUser("rest-admin","x","rest-admin"); + createRESTUser("rest-writer","x","rest-writer"); + createRESTUser("rest-reader","x","rest-reader"); + } + /* + * This function deletes the REST appserver along with attached content database and module database + */ + + + public static void deleteRESTServerWithDB(String restServerName) { + try{ + DefaultHttpClient client = new DefaultHttpClient(); + + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + + HttpDelete delete = new HttpDelete("http://localhost:8002/v1/rest-apis/"+restServerName+"?include=content&include=modules"); + + HttpResponse response = client.execute(delete); + if(response.getStatusLine().getStatusCode()== 202){ + Thread.sleep(3500); + } + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } + } + /* + * + */ + public static void deleteRESTServer(String restServerName) { + try{ + DefaultHttpClient client = new DefaultHttpClient(); + + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + + HttpDelete delete = new HttpDelete("http://localhost:8002/v1/rest-apis/"+restServerName+"&include=modules"); + HttpResponse response = client.execute(delete); + + if(response.getStatusLine().getStatusCode()== 202){ + Thread.sleep(3500); + waitForServerRestart(); + } + else System.out.println("Server response "+response.getStatusLine().getStatusCode()); + }catch (Exception e) { + // writing error to Log + System.out.println("Inside Deleting Rest server is throwing an error"); + e.printStackTrace(); + } + } + public static void detachForest(String dbName, String fName){ + try{ + DefaultHttpClient client = new DefaultHttpClient(); + + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + + HttpPost post = new HttpPost("http://localhost:8002"+ "/manage/v2/forests/"+fName); + // + List urlParameters = new ArrayList(); + urlParameters.add(new BasicNameValuePair("state", "detach")); + urlParameters.add(new BasicNameValuePair("database", dbName)); + + post.setEntity(new UrlEncodedFormEntity(urlParameters)); + + HttpResponse response = client.execute(post); + HttpEntity respEntity = response.getEntity(); + + if (respEntity != null) { + // EntityUtils to get the response content + String content = EntityUtils.toString(respEntity); + System.out.println(content); + } + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } + } + /* + * Deleting a forest is a HTTP Delete request + * + */ + public static void deleteForest(String fName){ + try{ + DefaultHttpClient client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + + HttpDelete delete = new HttpDelete("http://localhost:8002/manage/v2/forests/"+fName+"?level=full"); + client.execute(delete); + + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } + } + /* + * Deleting Database + * + */ + public static void deleteDB(String dbName){ + try{ + DefaultHttpClient client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + + HttpDelete delete = new HttpDelete("http://localhost:8002/manage/v2/databases/"+dbName); + client.execute(delete); + + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } + } + + public static void clearDB(int port){ + try{ + DefaultHttpClient client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", port), + new UsernamePasswordCredentials("admin", "admin")); + String uri = "http://localhost:"+port+"/v1/search/"; + HttpDelete delete = new HttpDelete(uri); + client.execute(delete); + + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } + } + public static void waitForServerRestart() + { + try{ + int count = 0; + while(count <20){ + DefaultHttpClient client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8001), + new UsernamePasswordCredentials("admin", "admin")); + + count++; + try{ + HttpGet getrequest = new HttpGet("http://localhost:8001/admin/v1/timestamp"); + HttpResponse response = client.execute(getrequest); + if(response.getStatusLine().getStatusCode() == 503){Thread.sleep(4000);} + else if(response.getStatusLine().getStatusCode() == 200){ + break; + } + else { + System.out.println("Waiting for response from server, Trial :"+response.getStatusLine().getStatusCode()+count); + Thread.sleep(2000); + } + }catch(Exception e){Thread.sleep(2000);} + } + }catch(Exception e){ + System.out.println("Inside wait for server restart is throwing an error"); + e.printStackTrace(); + } + } + /* + * This function deletes rest server first and deletes forests and databases in separate calls + */ + public static void tearDownJavaRESTServer(String dbName, String [] fNames, String restServerName) throws Exception{ + + try{ + associateRESTServerWithDB(restServerName,"Documents"); + }catch(Exception e){ + System.out.println("From Deleting Rest server called funnction is throwing an error"); + e.printStackTrace(); + } + + try{ + for(int i = 0; i < fNames.length; i++){ + detachForest(dbName, fNames[i]); + } + }catch(Exception e){ + e.printStackTrace(); + } + + try{ + for(int i = 0; i < fNames.length; i++){ + deleteForest(fNames[i]); + } + }catch(Exception e){ + e.printStackTrace(); + } + + deleteDB(dbName); + } + + /* + * This function deletes rest server along with default forest and database + */ + public static void tearDownJavaRESTServerWithDB(String restServerName) throws Exception{ + + try{ + deleteRESTServerWithDB(restServerName); + waitForServerRestart(); + }catch(Exception e){ + e.printStackTrace(); + } + Thread.sleep(6000); + + } + + /* + * + * setting up AppServices configurations + * setting up database properties whose value is string + */ + public static void setDatabaseProperties(String dbName,String prop,String propValue ) throws IOException{ + InputStream jsonstream=null; + try{ + DefaultHttpClient client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + HttpGet getrequest = new HttpGet("http://localhost:8002"+ "/manage/v2/databases/"+dbName+"/properties?format=json"); + HttpResponse response1 = client.execute(getrequest); + jsonstream =response1.getEntity().getContent(); + JsonNode jnode= new ObjectMapper().readTree(jsonstream); + if(!jnode.isNull()){ + ((ObjectNode)jnode).put(prop, propValue); + // System.out.println(jnode.toString()+"\n"+ response1.getStatusLine().getStatusCode()); + HttpPut put = new HttpPut("http://localhost:8002"+ "/manage/v2/databases/"+dbName+"/properties?format=json"); + put.addHeader("Content-type", "application/json"); + put.setEntity(new StringEntity(jnode.toString())); + + HttpResponse response2 = client.execute(put); + HttpEntity respEntity = response2.getEntity(); + if(respEntity != null){ + String content = EntityUtils.toString(respEntity); + System.out.println(content); + } + } + else{ + System.out.println("REST call for database properties returned NULL "); + } + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } + finally{ + if(jsonstream == null){} + else{ + jsonstream.close(); + } + } + } + + public static void setDatabaseProperties(String dbName,String prop,boolean propValue ) throws IOException{ + InputStream jsonstream=null; + try{ + DefaultHttpClient client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + HttpGet getrequest = new HttpGet("http://localhost:8002"+ "/manage/v2/databases/"+dbName+"/properties?format=json"); + HttpResponse response1 = client.execute(getrequest); + jsonstream =response1.getEntity().getContent(); + JsonNode jnode= new ObjectMapper().readTree(jsonstream); + if(!jnode.isNull()){ + ((ObjectNode)jnode).put(prop, propValue) ; + // System.out.println(jnode.toString()+"\n"+ response1.getStatusLine().getStatusCode()); + HttpPut put = new HttpPut("http://localhost:8002"+ "/manage/v2/databases/"+dbName+"/properties?format=json"); + put.addHeader("Content-type", "application/json"); + put.setEntity(new StringEntity(jnode.toString())); + + HttpResponse response2 = client.execute(put); + HttpEntity respEntity = response2.getEntity(); + if(respEntity != null){ + String content = EntityUtils.toString(respEntity); + System.out.println(content); + } + } + else{ + System.out.println("REST call for database properties returned NULL "); + } + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } + finally{ + if(jsonstream == null){} + else{ + jsonstream.close(); + } + } + } + + /* + * This Method takes the root property name and object node under it + * if root propname exist and equals to null then it just add the object node under root property name else if it has an existing sub property name then it adds + * elements to that array + */ + public static void setDatabaseProperties(String dbName,String propName, ObjectNode objNode ) throws IOException{ + InputStream jsonstream=null; + try{ + DefaultHttpClient client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + HttpGet getrequest = new HttpGet("http://localhost:8002"+ "/manage/v2/databases/"+dbName+"/properties?format=json"); + HttpResponse response1 = client.execute(getrequest); + jsonstream =response1.getEntity().getContent(); + ObjectMapper mapper = new ObjectMapper(); + JsonNode jnode= mapper.readTree(jsonstream); + if(!jnode.isNull()){ + + if(!jnode.has(propName)){ + ((ObjectNode)jnode).putArray(propName).addAll(objNode.withArray(propName)); + // System.out.println("when Node is null"+propName + objNode.toString()); + } + else{ + if(!jnode.path(propName).isArray()){ + System.out.println("property is not array"); + ((ObjectNode)jnode).putAll(objNode); + } + else{ + JsonNode member = jnode.withArray(propName); + if(objNode.path(propName).isArray()){ + ((ArrayNode)member).addAll(objNode.withArray(propName)); + // System.out.println("when Node is not null"+ propName + objNode.withArray(propName).toString()); + } + } + } + + HttpPut put = new HttpPut("http://localhost:8002"+ "/manage/v2/databases/"+dbName+"/properties?format=json"); + put.addHeader("Content-type", "application/json"); + put.setEntity(new StringEntity(jnode.toString())); + + HttpResponse response2 = client.execute(put); + HttpEntity respEntity = response2.getEntity(); + if(respEntity != null){ + String content = EntityUtils.toString(respEntity); + System.out.println(content); + } + } + else{ + System.out.println("REST call for database properties returned NULL \n"+jnode.toString()+"\n"+ response1.getStatusLine().getStatusCode()); + } + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } + finally{ + if(jsonstream == null){} + else{ + jsonstream.close(); + } + } + } + + public static void enableCollectionLexicon(String dbName) throws Exception{ + setDatabaseProperties(dbName,"collection-lexicon",true ); + } + /* + * "word-lexicons": [ + "http:\/\/marklogic.com\/collation\/" + ] + } + */ + public static void enableWordLexicon(String dbName) throws Exception{ + ObjectMapper mapper = new ObjectMapper(); + ObjectNode childNode = mapper.createObjectNode(); + ArrayNode childArray = mapper.createArrayNode(); + childArray.add("http://marklogic.com/collation/"); + childNode.putArray("word-lexicon").addAll(childArray); + setDatabaseProperties(dbName,"word-lexicons",childNode); + + } + public static void enableTrailingWildcardSearches(String dbName) throws Exception{ + setDatabaseProperties(dbName,"trailing-wildcard-searches",true ); + } + + public static void setMaintainLastModified(String dbName,boolean opt) throws Exception{ + setDatabaseProperties(dbName,"maintain-last-modified",opt); + } + public static void setAutomaticDirectoryCreation(String dbName, String opt) throws Exception{ + setDatabaseProperties(dbName,"directory-creation",opt); + } + /* + * This function constructs a range element index with default collation,range-value-positions and invalid values + * + */ + public static void addRangeElementIndex(String dbName, String type, String namespace, String localname) throws Exception{ + ObjectMapper mapper = new ObjectMapper(); + ObjectNode mainNode = mapper.createObjectNode(); + // ObjectNode childNode = mapper.createObjectNode(); + ArrayNode childArray = mapper.createArrayNode(); + ObjectNode childNodeObject = mapper.createObjectNode(); + childNodeObject.put( "scalar-type", type); + childNodeObject.put( "namespace-uri", namespace); + childNodeObject.put( "localname", localname); + childNodeObject.put( "collation", ""); + childNodeObject.put("range-value-positions", false); + childNodeObject.put("invalid-values", "reject"); + childArray.add(childNodeObject); + mainNode.putArray("range-element-index").addAll(childArray); + // mainNode.put("range-element-indexes", childNode); + // System.out.println(type + mainNode.path("range-element-indexes").path("range-element-index").toString()); + setDatabaseProperties(dbName,"range-element-index",mainNode); + + } + + + /* + * This is a overloaded function constructs a range element index with default range-value-positions and invalid values + * + */ + public static void addRangeElementIndex(String dbName, String type, String namespace, String localname, String collation) throws Exception{ + ObjectMapper mapper = new ObjectMapper(); + ObjectNode mainNode = mapper.createObjectNode(); + // ObjectNode childNode = mapper.createObjectNode(); + ArrayNode childArray = mapper.createArrayNode(); + ObjectNode childNodeObject = mapper.createObjectNode(); + childNodeObject.put( "scalar-type", type); + childNodeObject.put( "namespace-uri", namespace); + childNodeObject.put( "localname", localname); + childNodeObject.put( "collation", collation); + childNodeObject.put("range-value-positions", false); + childNodeObject.put("invalid-values", "reject"); + childArray.add(childNodeObject); + mainNode.putArray("range-element-index").addAll(childArray); + + // System.out.println(type + mainNode.path("range-element-indexes").path("range-element-index").toString()); + setDatabaseProperties(dbName,"range-element-index",mainNode); + + } + + /* + * "scalar-type": "int", + "collation": "", + "parent-namespace-uri": "", + "parent-localname": "test", + "namespace-uri": "", + "localname": "testAttr", + "range-value-positions": false, + "invalid-values": "reject" + */ + + public static void addRangeElementAttributeIndex(String dbName, String type, String parentnamespace, String parentlocalname, String namespace, String localname, String collation) throws Exception{ + ObjectMapper mapper = new ObjectMapper(); + // ObjectNode mainNode = mapper.createObjectNode(); + ObjectNode childNode = mapper.createObjectNode(); + ArrayNode childArray = mapper.createArrayNode(); + ObjectNode childNodeObject = mapper.createObjectNode(); + childNodeObject.put( "scalar-type", type); + childNodeObject.put( "collation", collation); + childNodeObject.put( "parent-namespace-uri", parentnamespace); + childNodeObject.put( "parent-localname", parentlocalname); + childNodeObject.put( "namespace-uri", namespace); + childNodeObject.put( "localname", localname); + + childNodeObject.put("range-value-positions", false); + childNodeObject.put("invalid-values", "reject"); + childArray.add(childNodeObject); + childNode.putArray("range-element-attribute-index").addAll(childArray); + + // mainNode.put("range-element-attribute-indexes", childNode); + // System.out.println(type + mainNode.path("range-element-attribute-indexes").path("range-element-attribute-index").toString()); + setDatabaseProperties(dbName,"range-element-attribute-index",childNode); + + } + /* + * Overloaded function with default collation + */ + public static void addRangeElementAttributeIndex(String dbName, String type, String parentnamespace, String parentlocalname, String namespace, String localname) throws Exception{ + ObjectMapper mapper = new ObjectMapper(); + //ObjectNode mainNode = mapper.createObjectNode(); + ObjectNode childNode = mapper.createObjectNode(); + ArrayNode childArray = mapper.createArrayNode(); + ObjectNode childNodeObject = mapper.createObjectNode(); + childNodeObject.put( "scalar-type", type); + childNodeObject.put( "collation", ""); + childNodeObject.put( "parent-namespace-uri", parentnamespace); + childNodeObject.put( "parent-localname", parentlocalname); + childNodeObject.put( "namespace-uri", namespace); + childNodeObject.put( "localname", localname); + + childNodeObject.put("range-value-positions", false); + childNodeObject.put("invalid-values", "reject"); + childArray.add(childNodeObject); + childNode.putArray("range-element-attribute-index").addAll(childArray); + // mainNode.put("range-element-attribute-indexes", childNode); + // System.out.println(type + mainNode.path("range-element-attribute-indexes").path("range-element-attribute-index").toString()); + setDatabaseProperties(dbName,"range-element-attribute-index",childNode); + + } + /* + * "range-path-indexes": { + "range-path-index": [ + { + "scalar-type": "string", + "collation": "http:\/\/marklogic.com\/collation\/", + "path-expression": "\/Employee\/fn", + "range-value-positions": false, + "invalid-values": "reject" + } + ] + } + */ + public static void addRangePathIndex(String dbName, String type, String pathexpr, String collation, String invalidValues) throws Exception{ + ObjectMapper mapper = new ObjectMapper(); + // ObjectNode mainNode = mapper.createObjectNode(); + ObjectNode childNode = mapper.createObjectNode(); + ArrayNode childArray = mapper.createArrayNode(); + ObjectNode childNodeObject = mapper.createObjectNode(); + childNodeObject.put( "scalar-type", type); + childNodeObject.put( "collation", collation); + childNodeObject.put( "path-expression", pathexpr); + childNodeObject.put("range-value-positions", false); + childNodeObject.put("invalid-values", invalidValues); + childArray.add(childNodeObject); + childNode.putArray("range-path-index").addAll(childArray); + // mainNode.put("range-path-indexes", childNode); + // System.out.println(type + mainNode.path("range-path-indexes").path("range-path-index").toString()); + setDatabaseProperties(dbName,"range-path-index",childNode); + + } + public static void addGeospatialElementIndexes(String dbName,String localname,String namespace,String coordinateSystem,String pointFormat,boolean rangeValuePositions,String invalidValues) throws Exception{ + ObjectMapper mapper = new ObjectMapper(); + // ObjectNode mainNode = mapper.createObjectNode(); + ObjectNode childNode = mapper.createObjectNode(); + ArrayNode childArray = mapper.createArrayNode(); + ObjectNode childNodeObject = mapper.createObjectNode(); + childNodeObject.put( "namespace-uri", namespace); + childNodeObject.put( "localname", localname); + childNodeObject.put( "coordinate-system", coordinateSystem); + childNodeObject.put("range-value-positions", false); + childNodeObject.put("invalid-values", invalidValues); + childNodeObject.put("point-format",pointFormat); + childArray.add(childNodeObject); + childNode.putArray("geospatial-element-index").addAll(childArray); + // mainNode.put("geospatial-element-indexes", childNode); + // System.out.println(type + mainNode.path("range-path-indexes").path("range-path-index").toString()); + setDatabaseProperties(dbName,"geospatial-element-index",childNode); + } + public static void addGeoSpatialElementChildIndexes(String dbName,String parentNamespaceUri,String parentLocalName,String namespace,String localname,String coordinateSystem,String pointFormat,boolean rangeValuePositions,String invalidValues) throws Exception{ + ObjectMapper mapper = new ObjectMapper(); + // ObjectNode mainNode = mapper.createObjectNode(); + ObjectNode childNode = mapper.createObjectNode(); + ArrayNode childArray = mapper.createArrayNode(); + ObjectNode childNodeObject = mapper.createObjectNode(); + childNodeObject.put( "parent-namespace-uri", parentNamespaceUri); + childNodeObject.put( "parent-localname", parentLocalName); + childNodeObject.put( "namespace-uri", namespace); + childNodeObject.put( "localname", localname); + childNodeObject.put( "coordinate-system", coordinateSystem); + childNodeObject.put("range-value-positions", false); + childNodeObject.put("invalid-values", invalidValues); + childNodeObject.put("point-format",pointFormat); + childArray.add(childNodeObject); + childNode.putArray("geospatial-element-child-index").addAll(childArray); + // mainNode.put("geospatial-element-child-indexes", childNode); + // System.out.println(type + mainNode.path("range-path-indexes").path("range-path-index").toString()); + setDatabaseProperties(dbName,"geospatial-element-child-index",childNode); + } + public static void addGeospatialElementPairIndexes(String dbName,String parentNamespaceUri,String parentLocalName,String latNamespace,String latLocalname,String longNamespace,String longLocalname,String coordinateSystem,boolean rangeValuePositions,String invalidValues) throws Exception{ + ObjectMapper mapper = new ObjectMapper(); + // ObjectNode mainNode = mapper.createObjectNode(); + ObjectNode childNode = mapper.createObjectNode(); + ArrayNode childArray = mapper.createArrayNode(); + ObjectNode childNodeObject = mapper.createObjectNode(); + childNodeObject.put( "parent-namespace-uri", parentNamespaceUri); + childNodeObject.put( "parent-localname", parentLocalName); + childNodeObject.put( "latitude-namespace-uri", latNamespace); + childNodeObject.put( "latitude-localname", latLocalname); + childNodeObject.put( "longitude-namespace-uri", latNamespace); + childNodeObject.put( "longitude-localname", longLocalname); + childNodeObject.put( "coordinate-system", coordinateSystem); + childNodeObject.put("range-value-positions", false); + childNodeObject.put("invalid-values", invalidValues); + childArray.add(childNodeObject); + childNode.putArray("geospatial-element-pair-index").addAll(childArray); + // mainNode.put("geospatial-element-pair-indexes", childNode); + // System.out.println(type + mainNode.path("range-path-indexes").path("range-path-index").toString()); + setDatabaseProperties(dbName,"geospatial-element-pair-index",childNode); + } + public static void addGeospatialElementAttributePairIndexes(String dbName,String parentNamespaceUri,String parentLocalName,String latNamespace,String latLocalname,String longNamespace,String longLocalname,String coordinateSystem,boolean rangeValuePositions,String invalidValues) throws Exception{ + ObjectMapper mapper = new ObjectMapper(); + // ObjectNode mainNode = mapper.createObjectNode(); + ObjectNode childNode = mapper.createObjectNode(); + ArrayNode childArray = mapper.createArrayNode(); + ObjectNode childNodeObject = mapper.createObjectNode(); + childNodeObject.put( "parent-namespace-uri", parentNamespaceUri); + childNodeObject.put( "parent-localname", parentLocalName); + childNodeObject.put( "latitude-namespace-uri", latNamespace); + childNodeObject.put( "latitude-localname", latLocalname); + childNodeObject.put( "longitude-namespace-uri", latNamespace); + childNodeObject.put( "longitude-localname", longLocalname); + childNodeObject.put( "coordinate-system", coordinateSystem); + childNodeObject.put("range-value-positions", false); + childNodeObject.put("invalid-values", invalidValues); + childArray.add(childNodeObject); + childNode.putArray("geospatial-element-attribute-pair-index").addAll(childArray); + // mainNode.put("geospatial-element-attribute-pair-indexes", childNode); + // System.out.println(type + mainNode.path("range-path-indexes").path("range-path-index").toString()); + setDatabaseProperties(dbName,"geospatial-element-attribute-pair-index",childNode); + } + public static void addGeospatialPathIndexes(String dbName,String pathExpression,String coordinateSystem,String pointFormat,boolean rangeValuePositions,String invalidValues) throws Exception{ + ObjectMapper mapper = new ObjectMapper(); + // ObjectNode mainNode = mapper.createObjectNode(); + ObjectNode childNode = mapper.createObjectNode(); + ArrayNode childArray = mapper.createArrayNode(); + ObjectNode childNodeObject = mapper.createObjectNode(); + childNodeObject.put( "path-expression", pathExpression); + childNodeObject.put( "coordinate-system", coordinateSystem); + childNodeObject.put("range-value-positions", false); + childNodeObject.put("invalid-values", invalidValues); + childNodeObject.put("point-format",pointFormat); + childArray.add(childNodeObject); + childNode.putArray("geospatial-path-index").addAll(childArray); + // mainNode.put("geospatial-path-indexes", childNode); + // System.out.println(type + mainNode.path("range-path-indexes").path("range-path-index").toString()); + setDatabaseProperties(dbName,"geospatial-path-index",childNode); + } + /* + * Add field will include root and it appends field to an existing fields + * "fields":{ + "field":[ + { + "field-name": "", + "include-root": true, + "included-elements": null, + "excluded-elements": null + } + , + { + "field-name": "para", + "include-root": false, + "included-elements": null, + "excluded-elements": null, + "tokenizer-overrides": null + } + ] + } + */ + public static void addField(String dbName, String fieldName) throws Exception{ + ObjectMapper mapper = new ObjectMapper(); + // ObjectNode mainNode = mapper.createObjectNode(); + ObjectNode childNode = mapper.createObjectNode(); + ArrayNode arrNode = mapper.createArrayNode(); + ObjectNode childNodeObject = mapper.createObjectNode(); + childNodeObject.put( "field-name", fieldName); + childNodeObject.put( "include-root", true); + childNodeObject.putNull( "included-elements"); + childNodeObject.putNull( "excluded-elements"); + childNodeObject.putNull( "tokenizer-overrides"); + arrNode.add(childNodeObject); + childNode.putArray("field").addAll(arrNode); + // mainNode.put("fields", childNode); + // System.out.println("Entered field to make it true"); + setDatabaseProperties(dbName,"field",childNode); + + } + public static void addFieldExcludeRoot(String dbName, String fieldName) throws Exception{ + ObjectMapper mapper = new ObjectMapper(); + // ObjectNode mainNode = mapper.createObjectNode(); + ObjectNode childNode = mapper.createObjectNode(); + ArrayNode arrNode = mapper.createArrayNode(); + ObjectNode childNodeObject = mapper.createObjectNode(); + childNodeObject.put( "field-name", fieldName); + childNodeObject.put( "include-root", false); + childNodeObject.putNull( "included-elements"); + childNodeObject.putNull( "excluded-elements"); + childNodeObject.putNull( "tokenizer-overrides"); + arrNode.add(childNodeObject); + childNode.putArray("field").addAll(arrNode); + // mainNode.put("fields", childNode); + // System.out.println( childNode.toString()); + setDatabaseProperties(dbName,"field",childNode); + + } + public static void addBuiltInGeoIndex (String dbName)throws Exception { + addGeospatialElementIndexes(dbName,"g-elem-point","","wgs84","point",false,"reject"); + addGeoSpatialElementChildIndexes(dbName,"","g-elem-child-parent","","g-elem-child-point","wgs84","point",false,"reject"); + addGeospatialElementPairIndexes(dbName,"","g-elem-pair","","lat","","long","wgs84",false,"reject"); + addGeospatialElementAttributePairIndexes(dbName,"","g-attr-pair","","lat","","long","wgs84",false,"reject"); + addGeospatialPathIndexes(dbName,"/doc/g-elem-point","wgs84","point",false,"ignore"); + } + + /* + This method is trying to add include element or exclude elements to the existing fields + * + */ + public static void setDatabaseFieldProperties(String dbName,String field_name, String propName, ObjectNode objNode ) throws IOException{ + InputStream jsonstream=null; + try{ + DefaultHttpClient client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + HttpGet getrequest = new HttpGet("http://localhost:8002"+ "/manage/v2/databases/"+dbName+"/properties?format=json"); + HttpResponse response1 = client.execute(getrequest); + jsonstream =response1.getEntity().getContent(); + ObjectMapper mapper = new ObjectMapper(); + JsonNode jnode= mapper.readTree(jsonstream); + if(!jnode.isNull()&& jnode.has("field")){ + JsonNode fieldNode = jnode.withArray("field"); + Iterator fnode = fieldNode.elements(); + while(fnode.hasNext()) { + JsonNode fnchild =fnode.next(); + if((fnchild.path("field-name").asText()).equals(field_name)){ + // System.out.println("Hurray" +fnchild.has(propName)); + if(!fnchild.has(propName)){ + ((ObjectNode)fnchild).putArray(propName).addAll(objNode.withArray(propName)); + System.out.println("Adding child array include node" + jnode.toString()); + } + else{ + JsonNode member = fnchild.withArray(propName); + ((ArrayNode)member).addAll(objNode.withArray(propName)); + } + + } + } + + HttpPut put = new HttpPut("http://localhost:8002"+ "/manage/v2/databases/"+dbName+"/properties?format=json"); + put.addHeader("Content-type", "application/json"); + put.setEntity(new StringEntity(jnode.toString())); + + HttpResponse response2 = client.execute(put); + HttpEntity respEntity = response2.getEntity(); + if(respEntity != null){ + String content = EntityUtils.toString(respEntity); + System.out.println(content); + } + } + else{ + System.out.println("REST call for database properties returned NULL \n"+jnode.toString()+"\n"+ response1.getStatusLine().getStatusCode()); + } + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } + finally{ + if(jsonstream == null){} + else{ + jsonstream.close(); + } + } + } + + public static void includeElementField(String dbName, String field_name, String namespace, String elementName) throws Exception{ + ObjectMapper mapper = new ObjectMapper(); + // ObjectNode mainNode = mapper.createObjectNode(); + ObjectNode childNode = mapper.createObjectNode(); + ArrayNode arrNode = mapper.createArrayNode(); + ObjectNode childNodeObject = mapper.createObjectNode(); + childNodeObject.put( "namespace-uri", namespace); + childNodeObject.put( "localname", elementName); + childNodeObject.put("weight", 1.0); + arrNode.add(childNodeObject); + childNode.putArray("included-element").addAll(arrNode); + // mainNode.put("included-elements", childNode); + System.out.println( childNode.toString()); + setDatabaseFieldProperties(dbName,field_name,"included-element",childNode); + + } + public static void includeElementFieldWithWeight(String dbName, String field_name, String namespace, String elementName, double weight) throws Exception{ + + ObjectMapper mapper = new ObjectMapper(); + // ObjectNode mainNode = mapper.createObjectNode(); + ObjectNode childNode = mapper.createObjectNode(); + ArrayNode arrNode = mapper.createArrayNode(); + ObjectNode childNodeObject = mapper.createObjectNode(); + childNodeObject.put( "namespace-uri", namespace); + childNodeObject.put( "localname", elementName); + childNodeObject.put("weight", weight); + arrNode.add(childNodeObject); + childNode.putArray("included-element").addAll(arrNode); + // mainNode.put("included-elements", childNode); + // System.out.println( childNode.toString()); + setDatabaseFieldProperties(dbName,field_name,"included-element",childNode); + + } + public static void setupAppServicesConstraint(String dbName) throws Exception { + enableCollectionLexicon(dbName); + enableWordLexicon(dbName); + addRangeElementIndex(dbName, "date", "http://purl.org/dc/elements/1.1/", "date"); + addRangeElementIndex(dbName, "int", "", "popularity"); + addRangeElementIndex(dbName, "int", "http://test.tups.com", "rate"); + addRangeElementIndex(dbName, "decimal", "http://test.aggr.com", "score"); + addRangeElementIndex(dbName, "string", "", "title", "http://marklogic.com/collation/"); + addRangeElementAttributeIndex(dbName, "decimal", "http://cloudbank.com", "price", "", "amt", "http://marklogic.com/collation/"); + enableTrailingWildcardSearches(dbName); + addFieldExcludeRoot(dbName, "para"); + includeElementFieldWithWeight(dbName, "para", "", "p", 5); + addRangePathIndex(dbName, "string", "/Employee/fn", "http://marklogic.com/collation/", "ignore"); + addRangePathIndex(dbName, "int", "/root/popularity", "", "ignore"); + addRangePathIndex(dbName, "decimal", "//@amt", "", "ignore"); + } + public static void setupAppServicesGeoConstraint(String dbName) throws Exception { + enableCollectionLexicon(dbName); + addRangeElementIndex(dbName, "dateTime", "", "bday", "http://marklogic.com/collation/"); + addRangeElementIndex(dbName, "int", "", "height1", "http://marklogic.com/collation/"); + addRangeElementIndex(dbName, "int", "", "height2", "http://marklogic.com/collation/"); + addRangePathIndex(dbName, "string", "/doc/name", "http://marklogic.com/collation/", "ignore"); + addField(dbName, "description"); + includeElementField(dbName, "description", "", "description"); + addBuiltInGeoIndex(dbName); + + } + + /* + * Create a temporal axis based on 2 element range indexes, for start and end values (for system or valid axis) + * @dbName Database Name + * @axisName Axis Name (name of axis that needs to be created) + * @namespaceStart Namespace for 'start' element range index + * @localnameStart Local name for 'start' element range index + * @namespaceEnd Namespace for 'end' element range index + * @localnameEnd Local name for 'end' element range index + */ + public static void addElementRangeIndexTemporalAxis(String dbName, String axisName, + String namespaceStart, String localnameStart, String namespaceEnd, String localnameEnd) throws Exception + { + ObjectMapper mapper = new ObjectMapper(); + ObjectNode rootNode = mapper.createObjectNode(); + + rootNode.put( "axis-name", axisName); + + // Set axis start + ObjectNode axisStart = mapper.createObjectNode(); + ObjectNode elementReferenceStart = mapper.createObjectNode(); + elementReferenceStart.put("namespace-uri", namespaceStart); + elementReferenceStart.put("localname", localnameStart); + elementReferenceStart.put("scalar-type", "dateTime"); + + axisStart.set("element-reference", elementReferenceStart); + rootNode.set("axis-start", axisStart); + + // Set axis ens + ObjectNode axisEnd = mapper.createObjectNode(); + ObjectNode elementReferenceEnd = mapper.createObjectNode(); + elementReferenceEnd.put("namespace-uri", namespaceStart); + elementReferenceEnd.put("localname", localnameStart); + elementReferenceEnd.put("scalar-type", "dateTime"); + + axisEnd.set("element-reference", elementReferenceEnd); + rootNode.set("axis-end", axisEnd); + + System.out.println(rootNode.toString()); + + + /*** + JSONObject rootNode = new JSONObject(jsonOrderedMap); + + + + JSONObject axisStart = new JSONObject(); + JSONObject elementReferenceStart = new JSONObject(); + + elementReferenceStart.put("namespace-uri", namespaceStart); + elementReferenceStart.put("localname", localnameStart); + elementReferenceStart.put("scalar-type", "dateTime"); + + axisStart.put("element-reference", elementReferenceStart); + + JSONObject axisEnd = new JSONObject(); + JSONObject elementReferenceEnd = new JSONObject(); + + elementReferenceEnd.put("namespace-uri", namespaceEnd); + elementReferenceEnd.put("localname", localnameEnd); + elementReferenceEnd.put("scalar-type", "dateTime"); + + axisEnd.put("element-reference", elementReferenceEnd); + + rootNode.put("axis-end", axisEnd); + rootNode.put("axis-start", axisStart); + rootNode.put( "axis-name", axisName); + + System.out.println(rootNode.toString()); + ***/ + + + DefaultHttpClient client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + + HttpPost post = new HttpPost("http://localhost:8002/manage/v2/databases/"+ dbName + "/temporal/axes?format=json"); + + post.addHeader("Content-type", "application/json"); + post.addHeader("accept", "application/json"); + post.setEntity(new StringEntity(rootNode.toString())); + + HttpResponse response = client.execute(post); + HttpEntity respEntity = response.getEntity(); + if( response.getStatusLine().getStatusCode() == 400) + { + HttpEntity entity = response.getEntity(); + String responseString = EntityUtils.toString(entity, "UTF-8"); + System.out.println(responseString); + } + else if (respEntity != null) { + // EntityUtils to get the response content + String content = EntityUtils.toString(respEntity); + System.out.println(content); + + System.out.println("Temporal axis: " + axisName + " created"); + System.out.println("=============================================================="); + } + else { + System.out.println("No Proper Response"); + } + } + + /* + * Delete a temporal axis + * @dbName Database Name + * @axisName Axis Name + */ + public static void deleteElementRangeIndexTemporalAxis(String dbName, String axisName) throws Exception + { + DefaultHttpClient client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + + HttpDelete del = new HttpDelete("http://localhost:8002/manage/v2/databases/"+ dbName + "/temporal/axes/" + axisName + "?format=json"); + + del.addHeader("Content-type", "application/json"); + del.addHeader("accept", "application/json"); + + HttpResponse response = client.execute(del); + HttpEntity respEntity = response.getEntity(); + if( response.getStatusLine().getStatusCode() == 400) + { + HttpEntity entity = response.getEntity(); + String responseString = EntityUtils.toString(entity, "UTF-8"); + System.out.println(responseString); + } + else if (respEntity != null) { + // EntityUtils to get the response content + String content = EntityUtils.toString(respEntity); + System.out.println(content); + } + else { + System.out.println("Axis: " + axisName + " deleted"); + System.out.println("=============================================================="); + } + } + + + /* + * Create a temporal collection + * @dbName Database Name + * @collectionName Collection Name (name of temporal collection that needs to be created) + * @systemAxisName Name of System axis + * @validAxisName Name of Valid axis + */ + public static void addElementRangeIndexTemporalCollection(String dbName, String collectionName, String systemAxisName, String validAxisName) + throws Exception + { + ObjectMapper mapper = new ObjectMapper(); + ObjectNode rootNode = mapper.createObjectNode(); + + rootNode.put( "collection-name", collectionName); + rootNode.put( "system-axis", systemAxisName); + rootNode.put( "valid-axis", validAxisName); + + System.out.println(rootNode.toString()); + + + DefaultHttpClient client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + + HttpPost post = new HttpPost("http://localhost:8002/manage/v2/databases/"+ dbName + "/temporal/collections?format=json"); + + post.addHeader("Content-type", "application/json"); + post.addHeader("accept", "application/json"); + post.setEntity(new StringEntity(rootNode.toString())); + + HttpResponse response = client.execute(post); + HttpEntity respEntity = response.getEntity(); + if( response.getStatusLine().getStatusCode() == 400) + { + HttpEntity entity = response.getEntity(); + String responseString = EntityUtils.toString(entity, "UTF-8"); + System.out.println(responseString); + } + else if (respEntity != null) { + // EntityUtils to get the response content + String content = EntityUtils.toString(respEntity); + System.out.println(content); + + System.out.println("Temporal collection: " + collectionName + " created"); + System.out.println("=============================================================="); + } + else { + System.out.println("No Proper Response"); + } + + } + + /* + * Delete a temporal collection + * @dbName Database Name + * @collectionName Collection Name + */ + public static void deleteElementRangeIndexTemporalCollection(String dbName, String collectionName) throws Exception + { + DefaultHttpClient client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + + HttpDelete del = new HttpDelete("http://localhost:8002/manage/v2/databases/"+ dbName + "/temporal/collections?collection=" + collectionName + "&format=json"); + + del.addHeader("Content-type", "application/json"); + del.addHeader("accept", "application/json"); + + HttpResponse response = client.execute(del); + HttpEntity respEntity = response.getEntity(); + if( response.getStatusLine().getStatusCode() == 400) + { + HttpEntity entity = response.getEntity(); + String responseString = EntityUtils.toString(entity, "UTF-8"); + System.out.println(responseString); + } + else if (respEntity != null) { + // EntityUtils to get the response content + String content = EntityUtils.toString(respEntity); + System.out.println(content); + } + else { + System.out.println("Collection: " + collectionName + " deleted"); + System.out.println("=============================================================="); + } + } + + + public static void loadBug18993(){ + try{ + DefaultHttpClient client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8011), + new UsernamePasswordCredentials("admin", "admin")); + String document ="a space b"; + String perm = "perm:rest-writer=read&perm:rest-writer=insert&perm:rest-writer=update&perm:rest-writer=execute"; + HttpPut put = new HttpPut("http://localhost:8011/v1/documents?uri=/a%20b&"+perm); + put.addHeader("Content-type", "application/xml"); + put.setEntity(new StringEntity(document)); + HttpResponse response = client.execute(put); + HttpEntity respEntity = response.getEntity(); + if(respEntity != null){ + String content = EntityUtils.toString(respEntity); + System.out.println(content); + } + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } + + } + + public static void setAuthentication(String level,String restServerName) throws ClientProtocolException, IOException + { + DefaultHttpClient client = new DefaultHttpClient(); + + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + String body = "{\"authentication\": \""+level+"\"}"; + + HttpPut put = new HttpPut("http://localhost:8002/manage/v2/servers/"+restServerName+"/properties?server-type=http&group-id=Default"); + put.addHeader("Content-type", "application/json"); + put.setEntity(new StringEntity(body)); + + HttpResponse response2 = client.execute(put); + HttpEntity respEntity = response2.getEntity(); + if(respEntity != null){ + String content = EntityUtils.toString(respEntity); + System.out.println(content); + } + } + public static void setDefaultUser(String usr,String restServerName) throws ClientProtocolException, IOException { + + DefaultHttpClient client = new DefaultHttpClient(); + + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + String body = "{\"default-user\": \""+usr+"\"}"; + + HttpPut put = new HttpPut("http://localhost:8002/manage/v2/servers/"+restServerName+"/properties?server-type=http&group-id=Default"); + put.addHeader("Content-type", "application/json"); + put.setEntity(new StringEntity(body)); + + HttpResponse response2 = client.execute(put); + HttpEntity respEntity = response2.getEntity(); + if(respEntity != null){ + String content = EntityUtils.toString(respEntity); + System.out.println(content); + } + } + public static void setupServerRequestLogging(DatabaseClient client,boolean flag) throws Exception { + ServerConfigurationManager scm =client.newServerConfigManager(); + scm.readConfiguration(); + scm.setServerRequestLogging(flag); + scm.writeConfiguration(); + } } \ No newline at end of file From 38cc28092e290cd3d0e63f17a9bdd7619baca810 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Sun, 5 Oct 2014 20:47:02 -0700 Subject: [PATCH 162/357] First checkin for bitemporal. Creating ERI and axis and collection --- .../marklogic/javaclient/TestBiTemporal.java | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/TestBiTemporal.java diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBiTemporal.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBiTemporal.java new file mode 100644 index 000000000..86a5d0d2a --- /dev/null +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBiTemporal.java @@ -0,0 +1,71 @@ +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.util.Iterator; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.Transaction; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.SearchHandle; +import com.marklogic.client.pojo.PojoPage; +import com.marklogic.client.pojo.PojoRepository; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.StringQueryDefinition; + +public class TestBiTemporal extends BasicJavaClientREST{ + + private static String dbName = "TestBiTemporalJava"; + private static String [] fNames = {"TestBiTemporalJava-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client ; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { +// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + ConnectedRESTQA.addRangeElementIndex(dbName, "dateTime", "", "javaERISystemStart"); + ConnectedRESTQA.addRangeElementIndex(dbName, "dateTime", "", "javaERISystemEnd"); + ConnectedRESTQA.addRangeElementIndex(dbName, "dateTime", "", "javaERIValidStart"); + ConnectedRESTQA.addRangeElementIndex(dbName, "dateTime", "", "javaERIValidEnd"); + + // Temporal axis must be created before temporal collection associated with those axes is created + ConnectedRESTQA.addElementRangeIndexTemporalAxis(dbName, "javaERISystem", "", "javaERISystemStart", "", "javaERISystemEnd"); + ConnectedRESTQA.addElementRangeIndexTemporalAxis(dbName, "javaERIValid", "", "javaERIValidStart", "", "javaERIValidEnd"); + ConnectedRESTQA.addElementRangeIndexTemporalCollection(dbName, "javaERITemporalCollection", "javaERISystem", "javaERIValid"); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + System.out.println("In tear down" ); + + // Temporal collection needs to be delete before temporal axis associated with it can be deleted + ConnectedRESTQA.deleteElementRangeIndexTemporalCollection(dbName, "javaERITemporalCollection"); + ConnectedRESTQA.deleteElementRangeIndexTemporalAxis(dbName, "javaERIValid"); + ConnectedRESTQA.deleteElementRangeIndexTemporalAxis(dbName, "javaERISystem"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } + + @Before + public void setUp() throws Exception { + client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST); + } + + @After + public void tearDown() throws Exception { + // release client + client.release(); + } + + @Test + public void test() { } +} From c08c12ba2a2d0db1470e3e393714b458c853f135 Mon Sep 17 00:00:00 2001 From: Ajit George Date: Mon, 6 Oct 2014 09:40:45 -0700 Subject: [PATCH 163/357] Test POJO read with special characters --- .../javaclient/TestPOJOSpecialCharRead.java | 660 ++++++++++++++++++ 1 file changed, 660 insertions(+) create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/TestPOJOSpecialCharRead.java diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOSpecialCharRead.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOSpecialCharRead.java new file mode 100644 index 000000000..c7a3edbe7 --- /dev/null +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOSpecialCharRead.java @@ -0,0 +1,660 @@ +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.util.Iterator; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.pojo.PojoPage; +import com.marklogic.client.pojo.PojoRepository; +import com.marklogic.client.pojo.annotation.Id; +import com.marklogic.client.query.StringQueryDefinition; + +/* + * Purpose : To test the following special cases for PojoRepository class methods + * POJO instance @Id field value with Negative numbers. + * POJO instance @Id field value with default values. + * POJO instance @Id field value with nulls. + * POJO instance @Id field value with special characters. + * POJO instance @Id field value with Unicode characters. + */ + +public class TestPOJOSpecialCharRead extends BasicJavaClientREST { + private static String dbName = "TestPOJOSpecialCharSearchDB"; + private static String [] fNames = {"TestPOJOSpecialCharSearchDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + + private long negativeId = -1L; + private static int restPort = 8011; + private DatabaseClient client; + + /* + * This class is similar to the Artifact class. It is used to test special characters using the name field + * which has been annotated with @Id. Note: Artifact class has id field annotated. + */ + public static class SpecialArtifact { + @Id + public String name; + public long id; + private Company manufacturer; + private int inventory; + + public long getId() { + return id; + } + public SpecialArtifact setId(long id) { + this.id= id; return this; + } + public String getName() { + return name; + } + public SpecialArtifact setName(String name) { + this.name = name; return this; + } + public Company getManufacturer() { + return manufacturer; + } + public SpecialArtifact setManufacturer(Company manufacturer) { + this.manufacturer= manufacturer; return this; + } + public int getInventory() { + return inventory; + } + public SpecialArtifact setInventory(int inventory) { + this.inventory= inventory; return this; + } + } + + /* + * This class is similar to the SpecialArtifact class. It is used to test special characters using the + * getter and setter methods which has been annotated with @Id. + */ + public static class SpArtifactWithGetSetId { + + private String name; + private long id; + private Company manufacturer; + private int inventory; + + @Id + public long getId() { + return id; + } + + @Id + public SpArtifactWithGetSetId setId(long id) { + this.id= id; return this; + } + public String getName() { + return name; + } + public SpArtifactWithGetSetId setName(String name) { + this.name = name; return this; + } + public Company getManufacturer() { + return manufacturer; + } + public SpArtifactWithGetSetId setManufacturer(Company manufacturer) { + this.manufacturer= manufacturer; return this; + } + public int getInventory() { + return inventory; + } + public SpArtifactWithGetSetId setInventory(int inventory) { + this.inventory= inventory; return this; + } + } + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + System.out.println("In tear down" ); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } + @Before + public void setUp() throws Exception { + client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST); + } + @After + public void tearDown() throws Exception { + // release client + client.release(); + } + + public long getNegativeId() { + return negativeId; + } + + public void setNegativeId(long longNegativeId) { + negativeId = longNegativeId; + } + + // Decrement and return the current negative Id. + public long getOneNegativeLongId() { + long lTemp = getNegativeId(); + setNegativeId(lTemp - 1); + return lTemp == -1 ? -1: lTemp; + } + + public Artifact setArtifact(long counter) { + Company acme = new Company(); + acme.setName("Acme Inc."); + acme.setWebsite("http://www.acme.com"); + acme.setLatitude(41.998); + acme.setLongitude(-87.966); + Artifact cogs = new Artifact(); + cogs.setId(counter); + cogs.setName("Cogs"); + cogs.setManufacturer(acme); + cogs.setInventory(1000); + + return cogs; + } + + //Sets the SpecialArtifact POJO object based on the @Id on name field + public SpecialArtifact setSpecialArtifact(long counter) { + Company acme = new Company(); + acme.setName("Acme Inc."); + acme.setWebsite("http://www.acme.com"); + acme.setLatitude(41.998); + acme.setLongitude(-87.966); + SpecialArtifact cogs = new SpecialArtifact(); + cogs.setId(counter); + cogs.setName("Cogs"); + cogs.setManufacturer(acme); + cogs.setInventory(1000); + + return cogs; + } + + /* + * This method is used to test POJO Repository read / search with default values. + * This method does not set the Id field in the Artifact class explicitly. + * Hence Id field should be holding 0L as the default value. + * Test should pass 0L and POJORepository read should handle this request just fine. + */ + public Artifact setArtifactWithDefault() { + Company acme = new Company(); + acme.setName("Acme Inc."); + acme.setWebsite("http://www.acme.com"); + acme.setLatitude(41.998); + acme.setLongitude(-87.966); + Artifact cogs = new Artifact(); + //cogs.setId(counter); + cogs.setName("Cogs"); + cogs.setManufacturer(acme); + cogs.setInventory(1000); + + return cogs; + } + + /* + * This method is used to test POJO Repository read / search with null values. + * This method set the manufacturer field in the Artifact class to null explicitly. + * Cannot set any of the Id fields to null due to Java language defaults. + */ + public Artifact setArtifactWithNull(long counter) { + + Artifact cogs = new Artifact(); + cogs.setId(counter); + cogs.setName("Cogs"); + cogs.setManufacturer(null); + cogs.setInventory(1000); + + return cogs; + } + + /* + * This method is used to test POJO Repository read / search with special characters in the SpecialArtifact name. + * Test should pass in the Name field and POJORepository read should handle this request just fine. + */ + public SpecialArtifact setSpecialArtifactWithSpecialCharacter(long counter, String specialArtifactName) { + Company acme = new Company(); + acme.setName(specialArtifactName); + acme.setWebsite("http://www.acme.com"); + acme.setLatitude(41.998); + acme.setLongitude(-87.966); + SpecialArtifact cogs = new SpecialArtifact(); + cogs.setId(counter); + cogs.setName(specialArtifactName); + cogs.setManufacturer(acme); + cogs.setInventory(1000); + + return cogs; + } + + //Sets the SpecialArtifact POJO object based on the @Id on get and set methods + public SpArtifactWithGetSetId setSpArtifactonMethod(long counter, String specialArtifactName) { + Company acme = new Company(); + acme.setName(specialArtifactName); + acme.setWebsite("http://www.acme.com"); + acme.setLatitude(41.998); + acme.setLongitude(-87.966); + SpArtifactWithGetSetId cogs = new SpArtifactWithGetSetId(); + cogs.setId(counter); + cogs.setName(specialArtifactName); + cogs.setManufacturer(acme); + cogs.setInventory(1000); + + return cogs; + } + + /* + * This method is used when there is a need to validate one read and search. + */ + public void validateArtifact(Artifact artifact, long longId) { + assertNotNull("Artifact object should never be Null", artifact); + assertNotNull("Id should never be Null",artifact.id); + assertEquals("Id of the object is ",longId, artifact.getId()); + assertEquals("Name of the object is ","Cogs", artifact.getName()); + assertEquals("Inventory of the object is ",1000, artifact.getInventory()); + assertEquals("Company name of the object is ","Acme Inc.", artifact.getManufacturer().getName()); + assertEquals("Web site of the object is ","http://www.acme.com", artifact.getManufacturer().getWebsite()); + assertEquals(-87.966, artifact.getManufacturer().getLongitude(), 0.00); + assertEquals(41.998, artifact.getManufacturer().getLatitude(), 0.00); + } + + /* + * This method is used when there is a need to validate one read and search with default Id field value. + * Verify that artifact.getId() returns 0L. + */ + public void validateArtifactWithDefault(Artifact artifact) { + assertNotNull("Artifact object should never be Null", artifact); + assertNotNull("Id should never be Null",artifact.id); + assertEquals("Id of the object is ",0, artifact.getId()); + assertEquals("Name of the object is ","Cogs", artifact.getName()); + assertEquals("Inventory of the object is ",1000, artifact.getInventory()); + assertEquals("Company name of the object is ","Acme Inc.", artifact.getManufacturer().getName()); + assertEquals("Web site of the object is ","http://www.acme.com", artifact.getManufacturer().getWebsite()); + assertEquals(-87.966, artifact.getManufacturer().getLongitude(), 0.00); + assertEquals(41.998, artifact.getManufacturer().getLatitude(), 0.00); + } + + /* + * This method is used when there is a need to validate one read and search with null. + * This test verifies that the POJORepository can write an object that has some parts not yet created/defined etc.. + */ + public void validateArtifactWithNull(Artifact artifact, long longId) { + assertNotNull("Artifact object should never be Null", artifact); + assertNotNull("Id should never be Null",artifact.id); + assertEquals("Id of the object is ",longId, artifact.getId()); + assertEquals("Name of the object is ","Cogs", artifact.getName()); + assertEquals("Inventory of the object is ",1000, artifact.getInventory()); + assertNull(artifact.getManufacturer()); + } + + /* + * This method is used when there is a need to validate one read and search with special characters in SpecialArtifact Name field. + * + */ + public void validateSpecialArtifactWithSpecialCharacter(SpecialArtifact artifact, String artifactName, long longId) { + assertNotNull("Artifact object should never be Null", artifact); + assertNotNull("Id should never be Null",artifact.id); + assertEquals("Id of the object is ",longId, artifact.getId()); + assertEquals("Name of the object is ",artifactName, artifact.getName()); + assertEquals("Inventory of the object is ",1000, artifact.getInventory()); + assertEquals("Company name of the object is ",artifactName, artifact.getManufacturer().getName()); + assertEquals("Web site of the object is ","http://www.acme.com", artifact.getManufacturer().getWebsite()); + assertEquals(-87.966, artifact.getManufacturer().getLongitude(), 0.00); + assertEquals(41.998, artifact.getManufacturer().getLatitude(), 0.00); + } + + /* + * This method is used when there is a need to validate one read and search with special characters in SpArtifactWithGetSetId Name field. + * + */ + public void validateSpArtifactWithSpecialCharacter(SpArtifactWithGetSetId artifact, String artifactName, long longId) { + assertNotNull("Artifact object should never be Null", artifact); + assertNotNull("Id should never be Null",artifact.id); + assertEquals("Id of the object is ", longId, artifact.getId()); + assertEquals("Name of the object is ",artifactName, artifact.getName()); + assertEquals("Inventory of the object is ",1000, artifact.getInventory()); + assertEquals("Company name of the object is ",artifactName, artifact.getManufacturer().getName()); + assertEquals("Web site of the object is ","http://www.acme.com", artifact.getManufacturer().getWebsite()); + assertEquals(-87.966, artifact.getManufacturer().getLongitude(), 0.00); + assertEquals(41.998, artifact.getManufacturer().getLatitude(), 0.00); +} + /* + * This method is used when there is a need to validate multiple reads and searches. + */ + public void validateArtifactTwo(Artifact artifact, long longId) { + assertNotNull("Artifact object should never be Null", artifact); + assertNotNull("Id should never be Null",artifact.id); + assertEquals("Id of the object is ",longId, artifact.getId()); + assertEquals("Name of the object is ","Cogs -2000", artifact.getName()); + assertEquals("Inventory of the object is ",1000, artifact.getInventory()); + assertEquals("Company name of the object is ","Acme -2000, Inc.", artifact.getManufacturer().getName()); + assertEquals("Web site of the object is ","http://www.acme-2000.com", artifact.getManufacturer().getWebsite()); + assertEquals(-87.966, artifact.getManufacturer().getLongitude(), 0.00); + assertEquals(41.998, artifact.getManufacturer().getLatitude(), 0.00); + } + + /* + * Purpose : This test is to validate exist method with Negative number + * POJO instance @Id field value with Negative numbers. + */ + + @Test + public void testPOJORepoExistWithNegativeId() { + PojoRepository pojoReposProducts = client.newPojoRepository(Artifact.class, Long.class); + long longId = getOneNegativeLongId(); + //Load two objects into database + pojoReposProducts.write(this.setArtifact(longId),"odd","numbers"); + + assertTrue(pojoReposProducts.exists(longId)); + } + + /* + * Purpose : This test is to validate read documents using read(Id) + * POJO instance @Id field value with Negative numbers. + */ + + @Test + public void testPOJORepoReadWithNegativeId() { + PojoRepository pojoReposProducts = client.newPojoRepository(Artifact.class, Long.class); + + //Load two objects into database + long longId = getOneNegativeLongId(); + pojoReposProducts.write(this.setArtifact(longId),"odd","numbers"); + + // Validate the artifact read back. + Artifact artifact = pojoReposProducts.read(longId); + validateArtifact(artifact, longId); + } + + /* + * Purpose : This test is to validate delete documents using delete(Id) + * POJO instance @Id field value with Negative numbers. + */ + + @Test + public void testPOJORepoDeleteWithNegativeId() { + PojoRepository pojoReposProducts = client.newPojoRepository(Artifact.class, Long.class); + + //Load two objects into database + long longId = getOneNegativeLongId(); + pojoReposProducts.write(this.setArtifact(longId),"odd","numbers"); + + // Delete the object + pojoReposProducts.delete(longId); + + // Validate the artifact read back. + Artifact artifact = pojoReposProducts.read(longId); + assertNull(artifact); + } + + /* + * Purpose : This test is to validate delete documents using delete(ID....) + * POJO instance @Id field value with Negative numbers. + */ + + @Test + public void testPOJORepoDeleteWithNegativeIdArray() { + long longId1 = getOneNegativeLongId(); + long longId2 = getOneNegativeLongId(); + + Long[] pojoReposProductsIdLongArray = {longId1, longId2}; + + PojoRepository pojoReposProducts = client.newPojoRepository(Artifact.class, Long.class); + PojoPage pojoArtifactPage; + + // Load the object into database + pojoReposProducts.write(this.setArtifact(longId1),"odd","numbers"); + pojoReposProducts.write(this.setArtifact(longId2),"even","numbers"); + + // Validate the artifacts read back. + pojoArtifactPage = pojoReposProducts.read(pojoReposProductsIdLongArray); + + System.out.println("The count of items in this page "+ pojoArtifactPage.size()); + assertEquals("The count of items in this page ", 2, pojoArtifactPage.size()); + + pojoReposProducts.delete(longId1,longId2); + + // Validate the artifacts read back is zero. + pojoArtifactPage = pojoReposProducts.read(pojoReposProductsIdLongArray); + assertEquals("Total number of object records", 0, pojoArtifactPage.getPageSize()); + } + + /* + * Purpose : This test is to validate read documents using read(ID[]) + * POJO instance @Id field value with Negative numbers. + */ + + @Test + public void testPOJORepoReadWithNegativeIdArray() { + + long longId1 = getOneNegativeLongId(); + long longId2 = getOneNegativeLongId(); + + Long[] pojoReposProductsIdLongArray = {longId1, longId2}; + + PojoRepository pojoReposProducts = client.newPojoRepository(Artifact.class, Long.class); + PojoPage pojoArtifactPage; + + // Load the object into database + pojoReposProducts.write(this.setArtifact(longId1),"odd","numbers"); + pojoReposProducts.write(this.setArtifact(longId2),"even","numbers"); + + // Validate the artifacts read back. + pojoArtifactPage = pojoReposProducts.read(pojoReposProductsIdLongArray); + + System.out.println("The count of items in this page "+ pojoArtifactPage.size()); + assertEquals("The count of items in this page ", 2, pojoArtifactPage.size()); + + System.out.println("The number of pages covering all possible items "+ pojoArtifactPage.getTotalPages()); + assertEquals("The number of pages covering all possible items ", 1, pojoArtifactPage.getTotalPages()); + + // Not sure about this feature. Should page number at the last or at the start. O/P is 2 now. + System.out.println("The page number within the count of all possible pages "+ pojoArtifactPage.getPageNumber()); + assertEquals("The page number within the count of all possible pages ", 1, pojoArtifactPage.getPageNumber()); + + // Not sure about this feature. User cannot set the max # of items allowed in one page. O/P is -1 now. + System.out.println("The page size which is the maximum number of items allowed in one page "+ pojoArtifactPage.getPageSize()); + //assertEquals("The page size which is the maximum number of items allowed in one page ", 1, pojoArtifactPage.getPageSize()); + + //Not sure about this feature. Start position of this page within all possible items. O/P is -1 now. + System.out.println("The start position of this page within all possible items "+ pojoArtifactPage.getStart()); + //assertEquals("The start position of this page within all possible items ", 1, pojoArtifactPage.getStart()); + + //Not sure about this feature. O/P is -1 now. + System.out.println("The total count (potentially an estimate) of all possible items in the set "+ pojoArtifactPage.getTotalSize()); + //assertEquals("The total count (potentially an estimate) of all possible items in the set ", 1, pojoArtifactPage.getTotalSize()); + + Iterator itr = pojoArtifactPage.iterator(); + Artifact artifact = null; + while(itr.hasNext()) { + // To validate read of multiple objects returned we need to branch based on Artifact ID. + artifact = pojoArtifactPage.iterator().next(); + if (artifact.getId() == longId1) + validateArtifact(artifact, longId1); + else if (artifact.getId() == longId2) + validateArtifactTwo(artifact, longId2); + } + + System.out.println("Is this Last page :"+ pojoArtifactPage.hasContent() + pojoArtifactPage.isLastPage()); + } + + /* + * Purpose : This test is to validate read documents using read(ID) with default value. + * POJO instance @Id field value with Negative numbers. + * Artifact class instance's Id field should be holding the 0L value. + */ + @Test + public void testPOJORepoReadWithDefaultId() { + PojoRepository pojoReposProducts = client.newPojoRepository(Artifact.class, Long.class); + + // Load the object into database + pojoReposProducts.write(this.setArtifactWithDefault(),"odd","numbers"); + + // Validate the artifact read back. + Artifact artifact = pojoReposProducts.read(0L); + validateArtifactWithDefault(artifact); + } + + /* + * Purpose : This test is to validate read documents using read(ID) with null value. + * POJO instance @Id field value with Negative numbers. + * Artifact class instance's Id field should be holding a valid value. + * The manufacturer value is set to null. Id field values cannot set to null due to Java language defaults. + */ + @Test + public void testPOJORepoReadWithNull() { + PojoRepository pojoReposProducts = client.newPojoRepository(Artifact.class, Long.class); + + // Load the object into database + long longId = getOneNegativeLongId(); + pojoReposProducts.write(this.setArtifactWithNull(longId),"odd","numbers"); + + // Validate the artifact read back. + Artifact artifact = pojoReposProducts.read(longId); + validateArtifactWithNull(artifact, longId); + } + + /* + * Purpose : This test is to validate read documents using read(Id) + * POJO instance @Id field value with Special Characters - Single quote. + */ + + @Test + public void testPOJORepoReadWithSingleQuotes() { + PojoRepository pojoReposProductsString = client.newPojoRepository(SpecialArtifact.class, String.class); + + // Load the object into database + long longId = getOneNegativeLongId(); + String artifactName = new String("Acme\'s Inc."); + pojoReposProductsString.write(this.setSpecialArtifactWithSpecialCharacter(longId, artifactName),"odd","numbers"); + + // Validate the artifact read back. + SpecialArtifact artifact1 = pojoReposProductsString.read(artifactName); + validateSpecialArtifactWithSpecialCharacter(artifact1, artifactName, longId); + } + + /* + * Purpose : This test is to validate read documents using read(Id) + * POJO instance @Id field value with Special Characters - Double quote. + */ + + @Test + public void testPOJORepoReadWithDoubleQuotes() { + PojoRepository pojoReposProductsString = client.newPojoRepository(SpecialArtifact.class, String.class); + + // Load the object into database + long longId = getOneNegativeLongId(); + String artifactName = new String("Acme\"s Inc."); + pojoReposProductsString.write(this.setSpecialArtifactWithSpecialCharacter(longId, artifactName),"odd","numbers"); + + // Validate the artifact read back. + SpecialArtifact artifact1 = pojoReposProductsString.read(artifactName); + validateSpecialArtifactWithSpecialCharacter(artifact1, artifactName, longId); + } + + /* + * Purpose : This test is to validate read documents using read(Id) + * POJO instance @Id field value with Special Characters - Double Asteriks. + */ + + @Test + public void testPOJORepoReadWithDoubleAsteriks() { + PojoRepository pojoReposProductsString = client.newPojoRepository(SpecialArtifact.class, String.class); + + // Load the object into database + long longId = getOneNegativeLongId(); + String artifactName = new String("Acmes ** Inc."); + pojoReposProductsString.write(this.setSpecialArtifactWithSpecialCharacter(longId, artifactName),"odd","numbers"); + + // Validate the artifact read back. + SpecialArtifact artifact1 = pojoReposProductsString.read(artifactName); + validateSpecialArtifactWithSpecialCharacter(artifact1, artifactName, longId); + } + + /* + * Purpose : This test is to validate exist and read documents using exist(id) and read(Id) + * POJO instance @Id field value with Special Characters - UTF-8. + * Uses SpecialArtifact class which has @Id on the name methods. + */ + + @Test + public void testPOJORepoReadWithUTF8() { + PojoRepository pojoReposProductsString = client.newPojoRepository(SpecialArtifact.class, String.class); + + // Load the object into database + long longId = getOneNegativeLongId(); + String artifactName = new String("mult-title:万里长城"); + pojoReposProductsString.write(this.setSpecialArtifactWithSpecialCharacter(longId, artifactName),"odd","numbers"); + + assertTrue(pojoReposProductsString.exists(artifactName)); + + // Validate the artifact read back. + SpecialArtifact artifact1 = pojoReposProductsString.read(artifactName); + validateSpecialArtifactWithSpecialCharacter(artifact1, artifactName, longId); + } + + /* + * Purpose : This test is to validate read documents using read(Id) + * POJO instance @Id on getter and setter methods instead of @Id on a property with Negative numbers. + * Uses SpArtifactWithGetSetId class which has private members with public get and set methods. + * The @Id annotation is on these get and set method. + */ + + @Test + public void testPOJORepoReadWithNegativeIdOnMethods() { + PojoRepository pojoReposProducts = client.newPojoRepository(SpArtifactWithGetSetId.class, Long.class); + + //Load two objects into database + long longId = getOneNegativeLongId(); + String artifactName = new String("mult-title:万里长城"); + pojoReposProducts.write(this.setSpArtifactonMethod(longId, artifactName),"odd","numbers"); + + // Validate the artifact read back. + SpArtifactWithGetSetId artifact = pojoReposProducts.read(longId); + validateSpArtifactWithSpecialCharacter(artifact, artifactName, longId); + } + + + /* + * Purpose : This test is to validate search documents using Special Characters. + * search(QueryDefinition query, long start) + * Special Characters - UTF-8 is used. + * Uses SpecialArtifact class which has @Id on the name methods. + */ + + @Test + public void testPOJORepoSearchWithUTF8NoTransaction() { + PojoRepository pojoReposProductsString = client.newPojoRepository(SpecialArtifact.class, String.class); + + SpecialArtifact searchSpArtifact = null; + + // Load the object into database + long longId = getOneNegativeLongId(); + String artifactName = new String("万里长城" + longId); + pojoReposProductsString.write(this.setSpecialArtifactWithSpecialCharacter(longId, artifactName),"odd","numbers"); + + assertTrue(pojoReposProductsString.exists(artifactName)); + + // Validate the artifact search. + StringQueryDefinition query = client.newQueryManager().newStringDefinition(); + query.setCriteria(artifactName); + + PojoPage pojoSpecialArtifactPage = pojoReposProductsString.search(query, 1); + Iterator iter = pojoSpecialArtifactPage.iterator(); + while ( iter.hasNext() ) { + searchSpArtifact = iter.next(); + validateSpecialArtifactWithSpecialCharacter(searchSpArtifact, artifactName, longId); + } + } + +} From 28c789868f903ad731c1bad9160c62cb77c8f3aa Mon Sep 17 00:00:00 2001 From: sammefford Date: Wed, 8 Oct 2014 14:01:17 -0600 Subject: [PATCH 164/357] we're not ready to upgrade jersey yet, so commenting out new version as it causes test failures --- pom.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pom.xml b/pom.xml index fb94d327a..51e2968dd 100644 --- a/pom.xml +++ b/pom.xml @@ -163,6 +163,7 @@ + com.sun.jersey jersey-client From 217e07d46638669a48b9d18e3da04315b5dd0374 Mon Sep 17 00:00:00 2001 From: sammefford Date: Wed, 8 Oct 2014 14:02:56 -0600 Subject: [PATCH 165/357] XdbcEval.java has served its purpose, eval is now implemented against the REST api instead --- .../com/marklogic/client/impl/XdbcEval.java | 113 ------------------ 1 file changed, 113 deletions(-) delete mode 100644 src/main/java/com/marklogic/client/impl/XdbcEval.java diff --git a/src/main/java/com/marklogic/client/impl/XdbcEval.java b/src/main/java/com/marklogic/client/impl/XdbcEval.java deleted file mode 100644 index 855000919..000000000 --- a/src/main/java/com/marklogic/client/impl/XdbcEval.java +++ /dev/null @@ -1,113 +0,0 @@ -package com.marklogic.client.impl; - -import javax.ws.rs.client.Client; -import javax.ws.rs.client.ClientBuilder; -import javax.ws.rs.client.Entity; -import javax.ws.rs.core.Form; -import javax.ws.rs.core.Form; -import javax.ws.rs.core.Response; -import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; -import org.glassfish.jersey.media.multipart.BodyPart; -import org.glassfish.jersey.media.multipart.MultiPart; -import org.glassfish.jersey.media.multipart.MultiPartFeature; - -public class XdbcEval { - public static void main(String[] args) { - System.setProperty("com.sun.xml.ws.transport.http.client.HttpTransportPipe.dump", "true"); - System.setProperty("com.sun.xml.ws.transport.local.LocalTransportPipe.dump", "true"); - System.setProperty("com.sun.xml.ws.util.pipe.StandaloneTubeAssembler.dump", "true"); - Client client = ClientBuilder.newClient(); - // set up digest auth - HttpAuthenticationFeature feature = HttpAuthenticationFeature.digest("admin", "admin"); - client.register(feature); - // set up multipart response parsing - client.register(MultiPartFeature.class); - // let's see what types we're getting - String xquery = - "declare variable $myvar1 external;" + - "declare variable $myvar2 external;" + - "declare variable $myvar3 external;" + - "declare variable $myvar4 external;" + - "declare variable $myvar5 external;" + - "declare variable $myvar6 external;" + - "declare function local:getType($var) {" + - "typeswitch($var) " + - "case attribute() return 'attribute()'" + - "case comment() return 'comment()'" + - //"case document() return 'document()'" + - "case element() return 'element()'" + - "case node() return 'node()'" + - //"case processingInstruction() return 'processingInstruction()'" + - "case xs:base64Binary return 'xs:base64Binary'" + - "case xs:boolean return 'xs:boolean'" + - "case xs:byte return 'xs:byte'" + - "case xs:date return 'xs:date'" + - "case xs:dateTime return 'xs:dateTime'" + - "case xs:dayTimeDuration return 'xs:dayTimeDuration'" + - "case xs:short return 'xs:short'" + - "case xs:integer return 'xs:integer'" + - "case xs:int return 'xs:int'" + - "case xs:long return 'xs:long'" + - "case xs:decimal return 'xs:decimal'" + - "case xs:float return 'xs:float'" + - "case xs:double return 'xs:double'" + - "case xs:duration return 'xs:duration'" + - "case xs:unsignedInt return 'xs:unsignedInt'" + - "case xs:unsignedLong return 'xs:unsignedLong'" + - "case xs:unsignedShort return 'xs:unsignedShort'" + - "case xs:string return 'xs:string'" + - "case xs:time return 'xs:time'" + - "case xs:yearMonthDuration return 'xs:yearMonthDuration'" + - "case xs:anyAtomicType return 'xs:anyAtomicType'" + - "case xs:anySimpleType return 'xs:anySimpleType'" + - "case item() return 'item()'" + - "default return 'unknown'" + - "};" + - "for $var at $i in ($myvar1, $myvar2, $myvar3, $myvar4, $myvar5, $myvar6) " + - "return (" + - "$var," + - "'type' || $i || ': ' || local:getType($var)" + - ")"; - // set up x-www-form-urlencoded request params - Form params = new Form() - .param("xquery", xquery) - .param("evn0", "") - .param("evl0", "myvar1") - .param("evt0", "xs:int") - .param("evv0", "1") - .param("evn1", "") - .param("evl1", "myvar2") - .param("evt1", "xs:float") - .param("evv1", "1.1") - .param("evn2", "") - .param("evl2", "myvar3") - .param("evt2", "xs:decimal") - .param("evv2", "1.1") - .param("evn3", "") - .param("evl3", "myvar4") - .param("evt3", "xs:double") - .param("evv3", "1.1") - .param("evn4", "") - .param("evl4", "myvar5") - .param("evt4", "xs:long") - .param("evv4", "999999999999999999") - .param("evn5", "") - .param("evl5", "myvar6") - .param("evt5", "xs:string") - .param("evv5", "test") - .param("locale", "en_US") - .param("tzoffset", "-21600") - .param("dbname", "java-unittest"); - // make request - Response res = client.target("http://localhost:8020/eval") - .request() - .buildPost(Entity.form(params)) - .invoke(); - // read response as multipart even though there's only one part - MultiPart body = res.readEntity(MultiPart.class); - System.out.println("response=[" + res + "]"); - for ( BodyPart part : body.getBodyParts() ) { - System.out.println("part body2=[" + part.getEntityAs(String.class) + "]"); - } - } -} From 1029ba25d2c6073819e198d76b12d3f635ec1209 Mon Sep 17 00:00:00 2001 From: sammefford Date: Wed, 8 Oct 2014 14:27:20 -0600 Subject: [PATCH 166/357] handle case where total isn't returned with search results --- src/main/java/com/marklogic/client/io/SearchHandle.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/marklogic/client/io/SearchHandle.java b/src/main/java/com/marklogic/client/io/SearchHandle.java index edc6109d1..9ecf05d45 100644 --- a/src/main/java/com/marklogic/client/io/SearchHandle.java +++ b/src/main/java/com/marklogic/client/io/SearchHandle.java @@ -1035,7 +1035,9 @@ private void handleTop(XMLEventReader reader, StartElement element) throws XMLSt private void handleResponse(XMLEventReader reader, StartElement element) throws XMLStreamException { tempSnippetType = getAttribute(element, "snippet-format"); - tempTotalResults = Long.parseLong(getAttribute(element, "total")); + if ( getAttribute(element, "total") != null ) { + tempTotalResults = Long.parseLong(getAttribute(element, "total")); + } tempPageLength = Integer.parseInt(getAttribute(element, "page-length")); tempStart = Long.parseLong(getAttribute(element, "start")); From 154dd38c2ce15363dfcadfcf5fcef2f0418157b0 Mon Sep 17 00:00:00 2001 From: sammefford Date: Wed, 8 Oct 2014 17:15:19 -0600 Subject: [PATCH 167/357] fix #121, separate PojoQueryBuilder from StructuredQueryBuilder --- .../client/impl/PojoQueryBuilderImpl.java | 30 ++++++++-- .../client/pojo/PojoQueryBuilder.java | 57 +++++++++++++++++-- .../client/query/StructuredQueryBuilder.java | 2 +- .../marklogic/client/test/PojoFacadeTest.java | 2 +- 4 files changed, 79 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/marklogic/client/impl/PojoQueryBuilderImpl.java b/src/main/java/com/marklogic/client/impl/PojoQueryBuilderImpl.java index ad3b6e3ab..053dec13c 100644 --- a/src/main/java/com/marklogic/client/impl/PojoQueryBuilderImpl.java +++ b/src/main/java/com/marklogic/client/impl/PojoQueryBuilderImpl.java @@ -76,17 +76,27 @@ public StructuredQueryBuilder.GeospatialIndex geoProperty(String pojoProperty) { public StructuredQueryBuilder.GeospatialIndex geoPath(String pojoProperty) { return geoPath(pojoPropertyPath(pojoProperty)); } + @Override + public StructuredQueryDefinition geospatial(GeospatialIndex index, + String[] options, Region... regions) { + return super.geospatial(index, StructuredQueryBuilder.FragmentScope.DOCUMENTS, + options, regions); + } + @Override public StructuredQueryDefinition range(String pojoProperty, - StructuredQueryBuilder.Operator operator, Object... values) + PojoQueryBuilder.Operator operator, Object... values) { - return range(pojoPropertyPath(pojoProperty), getRangeIndexType(pojoProperty), operator, values); + return range(pojoPropertyPath(pojoProperty), getRangeIndexType(pojoProperty), + convertOperator(operator), values); } + @Override public StructuredQueryDefinition range(String pojoProperty, String[] options, - StructuredQueryBuilder.Operator operator, Object... values) + PojoQueryBuilder.Operator operator, Object... values) { return range(pojoPropertyPath(pojoProperty), getRangeIndexType(pojoProperty), options, - operator, values); + convertOperator(operator), values); } + public StructuredQueryDefinition value(String pojoProperty, String... values) { if ( wrapQueries ) { return super.containerQuery(jsonProperty(classWrapper), @@ -159,6 +169,18 @@ public StructuredQueryDefinition word(String pojoProperty, String[] options, return super.word(jsonProperty(pojoProperty), null, options, weight, words); } } + private StructuredQueryBuilder.Operator convertOperator(PojoQueryBuilder.Operator operator) { + switch (operator) { + case LT: return StructuredQueryBuilder.Operator.LT; + case LE: return StructuredQueryBuilder.Operator.LE; + case GT: return StructuredQueryBuilder.Operator.GT; + case GE: return StructuredQueryBuilder.Operator.GE; + case EQ: return StructuredQueryBuilder.Operator.EQ; + case NE: return StructuredQueryBuilder.Operator.NE; + default: throw new IllegalStateException("Unsupported Operator: " + operator); + } + } + @SuppressWarnings("deprecation") public TermQuery term(String... terms) { return new PojoTermQuery(wrapQueries, null, terms); diff --git a/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java b/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java index e4d16e4f7..9d33d2820 100644 --- a/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java +++ b/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java @@ -17,13 +17,14 @@ import javax.xml.namespace.QName; -import com.marklogic.client.query.CombinedQueryBuilder; import com.marklogic.client.query.RawStructuredQueryDefinition; import com.marklogic.client.query.StructuredQueryBuilder; import com.marklogic.client.query.StructuredQueryDefinition; import com.marklogic.client.util.IterableNamespaceContext; -/** Extends StructuredQueryBuilder with convenience methods specific to working with pojos. +/** Specific to pojos yet similar to StructuredQueryBuilder, this class generates structured queries. + * It adds convenience methods specific to working with pojos and does not replicate + * StructuredQueryBuilder methods that don't make sense for pojos. * The goal of {@link com.marklogic.client.pojo the pojo facade} is to simplify working with * custom pojos. PojoQueryBuilder keeps all the powerful queries available via * StructuredQueryBuilder while enabling queries across objects persisted using @@ -108,8 +109,13 @@ * PojoQueryBuilder countriesQb = citiesQb.containerQuery("country"); * QueryDefinition query = countriesQb.value("continent", "EU"); } */ -public interface PojoQueryBuilder extends CombinedQueryBuilder { +public interface PojoQueryBuilder { + /** Copied directly from {@link StructuredQueryBuilder#Operator StructuredQuerybuilder.Operator}**/ + public enum Operator { + LT, LE, GT, GE, EQ, NE; + } + /** @return a query matching pojos of type T containing the pojoProperty with contents * or children matching the specified query */ public StructuredQueryDefinition containerQuery(String pojoProperty, @@ -130,12 +136,12 @@ public StructuredQueryDefinition containerQuery(String pojoProperty, */ public StructuredQueryBuilder.GeospatialIndex geoProperty(String pojoProperty); - public StructuredQueryBuilder.GeospatialIndex +public StructuredQueryBuilder.GeospatialIndex geoPath(String pojoProperty); public StructuredQueryDefinition range(String pojoProperty, - StructuredQueryBuilder.Operator operator, Object... values); + PojoQueryBuilder.Operator operator, Object... values); public StructuredQueryDefinition range(String pojoProperty, String[] options, - StructuredQueryBuilder.Operator operator, Object... values); + PojoQueryBuilder.Operator operator, Object... values); public StructuredQueryDefinition value(String pojoProperty, String... values); public StructuredQueryDefinition value(String pojoProperty, Boolean value); public StructuredQueryDefinition value(String pojoProperty, Number... values); @@ -148,5 +154,44 @@ public StructuredQueryDefinition value(String pojoProperty, String[] options, public StructuredQueryDefinition word(String pojoProperty, String... words); public StructuredQueryDefinition word(String pojoProperty, String[] options, double weight, String... words); + + /** Copied directly from {@link StructuredQueryBuilder#and StructuredQuerybuilder.and}**/ + public StructuredQueryDefinition and(StructuredQueryDefinition... queries); + /** Copied directly from {@link StructuredQueryBuilder#andNot StructuredQuerybuilder.andNot}**/ + public StructuredQueryDefinition andNot(StructuredQueryDefinition positive, StructuredQueryDefinition negative); + /** Copied directly from {@link StructuredQueryBuilder#boost StructuredQuerybuilder.boost}**/ + public StructuredQueryDefinition boost(StructuredQueryDefinition matchingQuery, StructuredQueryDefinition boostingQuery); + /** Copied directly from {@link StructuredQueryBuilder#box StructuredQuerybuilder.box}**/ + public StructuredQueryBuilder.Region box(double south, double west, double north, double east); + /** Copied directly from {@link StructuredQueryBuilder#build StructuredQuerybuilder.build}**/ + public RawStructuredQueryDefinition build(StructuredQueryDefinition... queries); + /** Copied directly from {@link StructuredQueryBuilder#circle(double, double, double) StructuredQuerybuilder.circle(double, double, double)}**/ + public StructuredQueryBuilder.Region circle(double latitude, double longitude, double radius); + /** Copied directly from {@link StructuredQueryBuilder#circle(StructuredQueryBuilder.Point, double) StructuredQuerybuilder.circle(StructuredQueryBuilder.Point, double)}**/ + public StructuredQueryBuilder.Region circle(StructuredQueryBuilder.Point center, double radius); + /** Copied directly from {@link StructuredQueryBuilder#collection(String...) StructuredQuerybuilder.collection(String...)}**/ + public StructuredQueryDefinition collection(String... uris); + /** Copied from {@link StructuredQueryBuilder#geospatial(StructuredQueryBuilder.GeospatialIndex, StructuredQueryBuilder.FragmentScope, String[], StructuredQueryBuilder.Region...) StructuredQuerybuilder.geospatial(StructuredQueryBuilder.GeospatialIndex, StructuredQueryBuilder.FragmentScope, String[], StructuredQueryBuilder.Region...)} but without StructuredQueryBuilder.FragmentScope**/ + public StructuredQueryDefinition geospatial(StructuredQueryBuilder.GeospatialIndex index, String[] options, StructuredQueryBuilder.Region... regions); + /** Copied directly from {@link StructuredQueryBuilder#geospatial(StructuredQueryBuilder.GeospatialIndex, StructuredQueryBuilder.Region...) StructuredQuerybuilder.geospatial(StructuredQueryBuilder.GeospatialIndex, StructuredQueryBuilder.Region...)}**/ + public StructuredQueryDefinition geospatial(StructuredQueryBuilder.GeospatialIndex index, StructuredQueryBuilder.Region... regions); + /** Copied directly from {@link StructuredQueryBuilder#near(int, double, StructuredQueryBuilder.Ordering, StructuredQueryDefinition...) StructuredQuerybuilder.near(int, double, StructuredQueryBuilder.Ordering, StructuredQueryDefinition...)}**/ + public StructuredQueryDefinition near(int distance, double weight, StructuredQueryBuilder.Ordering order, StructuredQueryDefinition... queries); + /** Copied directly from {@link StructuredQueryBuilder#near(StructuredQueryDefinition...) StructuredQuerybuilder.near(StructuredQueryDefinition...)}**/ + public StructuredQueryDefinition near(StructuredQueryDefinition... queries); + /** Copied directly from {@link StructuredQueryBuilder#not StructuredQuerybuilder.not}**/ + public StructuredQueryDefinition not(StructuredQueryDefinition query); + /** Copied directly from {@link StructuredQueryBuilder#notIn StructuredQuerybuilder.and}**/ + public StructuredQueryDefinition notIn(StructuredQueryDefinition positive, StructuredQueryDefinition negative); + /** Copied directly from {@link StructuredQueryBuilder#or StructuredQuerybuilder.and}**/ + public StructuredQueryDefinition or(StructuredQueryDefinition... queries); + /** Copied directly from {@link StructuredQueryBuilder#point StructuredQuerybuilder.point}**/ + public StructuredQueryBuilder.Region point(double latitude, double longitude); + /** Copied directly from {@link StructuredQueryBuilder#polygon StructuredQuerybuilder.polygon}**/ + public StructuredQueryBuilder.Region polygon(StructuredQueryBuilder.Point... points); + /** Copied directly from {@link StructuredQueryBuilder#term(double, String...) StructuredQuerybuilder.term(double, String...)}**/ + public StructuredQueryDefinition term(double weight, String... terms); + /** Copied directly from {@link StructuredQueryBuilder#term(String...) StructuredQuerybuilder.term(String...)}**/ + public StructuredQueryDefinition term(String... terms); } diff --git a/src/main/java/com/marklogic/client/query/StructuredQueryBuilder.java b/src/main/java/com/marklogic/client/query/StructuredQueryBuilder.java index 2a118aa40..72ded5bfe 100644 --- a/src/main/java/com/marklogic/client/query/StructuredQueryBuilder.java +++ b/src/main/java/com/marklogic/client/query/StructuredQueryBuilder.java @@ -57,7 +57,7 @@ * in a variable: *
StructuredQueryDefinition andQry = structuredQueryBuilder.and(... query definitions ...);
*/ -public class StructuredQueryBuilder implements CombinedQueryBuilder { +public class StructuredQueryBuilder { final static private String SEARCH_API_NS="http://marklogic.com/appservices/search"; /* diff --git a/src/test/java/com/marklogic/client/test/PojoFacadeTest.java b/src/test/java/com/marklogic/client/test/PojoFacadeTest.java index 4c9e2a01a..834e12460 100644 --- a/src/test/java/com/marklogic/client/test/PojoFacadeTest.java +++ b/src/test/java/com/marklogic/client/test/PojoFacadeTest.java @@ -34,7 +34,7 @@ import com.marklogic.client.pojo.PojoRepository; import com.marklogic.client.query.QueryDefinition; import com.marklogic.client.query.StringQueryDefinition; -import com.marklogic.client.query.StructuredQueryBuilder.Operator; +import com.marklogic.client.pojo.PojoQueryBuilder.Operator; import com.marklogic.client.pojo.PojoQueryBuilder; import com.marklogic.client.pojo.annotation.Id; import com.marklogic.client.test.BulkReadWriteTest; From 6672fdf40690abc5405b11b242f862210f35f141 Mon Sep 17 00:00:00 2001 From: skottam Date: Wed, 8 Oct 2014 16:55:54 -0700 Subject: [PATCH 168/357] adding test for container query, range query --- .../TestPOJOQueryBuilderContainerQuery.java | 301 ++++++++++++++++++ 1 file changed, 301 insertions(+) create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderContainerQuery.java diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderContainerQuery.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderContainerQuery.java new file mode 100644 index 000000000..933b2efc9 --- /dev/null +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderContainerQuery.java @@ -0,0 +1,301 @@ +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.JacksonHandle; +import com.marklogic.client.pojo.PojoPage; +import com.marklogic.client.pojo.PojoQueryBuilder; +import com.marklogic.client.pojo.PojoRepository; +import com.marklogic.client.query.QueryDefinition; +import com.marklogic.client.query.StructuredQueryBuilder; +import com.marklogic.client.query.StructuredQueryBuilder.Operator; + +public class TestPOJOQueryBuilderContainerQuery extends BasicJavaClientREST { + + private static String dbName = "TestPOJOQueryBuilderContainerQuerySearchDB"; + private static String [] fNames = {"TestPOJOQueryBuilderContainserQuerySearchDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client ; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { +// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + BasicJavaClientREST.setDatabaseProperties(dbName, "trailing-wildcard-searches", true); + BasicJavaClientREST.setDatabaseProperties(dbName, "word-positions", true); + BasicJavaClientREST.setDatabaseProperties(dbName, "element-word-positions", true); + BasicJavaClientREST.addRangePathIndex(dbName, "long", "com.marklogic.javaclient.Artifact/inventory", "", "reject",true); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + System.out.println("In tear down" ); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } + @Before + public void setUp() throws Exception { + client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST); + + + } + @After + public void tearDown() throws Exception { + // release client + client.release(); + + } + + public Artifact getArtifact(int counter){ + + Artifact cogs = new Artifact(); + cogs.setId(counter); + if( counter % 5 == 0){ + cogs.setName("Cogs special"); + if(counter % 2 ==0){ + Company acme = new Company(); + acme.setName("Acme special, Inc."); + acme.setWebsite("http://www.acme special.com"); + acme.setLatitude(41.998+counter); + acme.setLongitude(-87.966+counter); + cogs.setManufacturer(acme); + + }else{ + Company widgets = new Company(); + widgets.setName("Widgets counter Inc."); + widgets.setWebsite("http://www.widgets counter.com"); + widgets.setLatitude(41.998+counter); + widgets.setLongitude(-87.966+counter); + cogs.setManufacturer(widgets); + } + }else{ + cogs.setName("Cogs "+counter); + if(counter % 2 ==0){ + Company acme = new Company(); + acme.setName("Acme "+counter+", Inc."); + acme.setWebsite("http://www.acme"+counter+".com"); + acme.setLatitude(41.998+counter); + acme.setLongitude(-87.966+counter); + cogs.setManufacturer(acme); + + }else{ + Company widgets = new Company(); + widgets.setName("Widgets "+counter+", Inc."); + widgets.setWebsite("http://www.widgets"+counter+".com"); + widgets.setLatitude(41.998+counter); + widgets.setLongitude(-87.966+counter); + cogs.setManufacturer(widgets); + } + } + cogs.setInventory(1000+counter); + return cogs; + } + public void validateArtifact(Artifact art) + { + assertNotNull("Artifact object should never be Null",art); + assertNotNull("Id should never be Null",art.id); + assertTrue("Inventry is always greater than 1000", art.getInventory()>1000); + } + public void loadSimplePojos(PojoRepository products) + { + for(int i=1;i<111;i++){ + if(i%2==0){ + products.write(this.getArtifact(i),"even","numbers"); + } + else { + products.write(this.getArtifact(i),"odd","numbers"); + } + } + } + // Below scenario is to test the ContainerQuery with term query + @Test + public void testPOJOContainerQuerySearchWithWord() { + PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); + PojoPage p; + this.loadSimplePojos(products); + String[] searchOptions ={"case-sensitive","wildcarded","min-occurs=2"}; + PojoQueryBuilder qb = products.getQueryBuilder(); + QueryDefinition qd = qb.containerQuery("manufacturer", qb.term("counter")); + JacksonHandle jh = new JacksonHandle(); + products.setPageLength(5); + p = products.search(qd, 1,jh); + assertEquals("total no of pages",3,p.getTotalPages()); + System.out.println(jh.get().toString()); + + long pageNo=1,count=0; + do{ + count =0; + p = products.search(qd,pageNo); + while(p.hasNext()){ + Artifact a =p.next(); + validateArtifact(a); + assertTrue("Verifying document id is part of the search ids",a.getId()%5==0); + assertTrue("Verifying Manufacurer has term counter",a.getManufacturer().getName().contains("counter")); + count++; + System.out.println(a.getManufacturer().getName()); + } + assertEquals("Page size",count,p.size()); + pageNo=pageNo+p.getPageSize(); + }while(!p.isLastPage() && pageNo<=p.getTotalSize()); + assertEquals("page number after the loop",3,p.getPageNumber()); + assertEquals("total no of pages",3,p.getTotalPages()); + assertEquals("page length from search handle",5,jh.get().path("page-length").asInt()); + assertEquals("Total results from search handle",11,jh.get().path("total").asInt()); + + } + //Below scenario is to test container query builder with wild card options in word query + @Test + public void testPOJOwordSearchWithContainerQueryBuilder() { + PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); + PojoPage p; + this.loadSimplePojos(products); + String[] searchOptions ={"case-sensitive","wildcarded","max-occurs=1"}; + PojoQueryBuilder qb = products.getQueryBuilder(); + String[] searchNames = {"special"}; + QueryDefinition qd =qb.containerQueryBuilder("manufacturer", Company.class).word("name",searchOptions,1.0,searchNames); + + JacksonHandle jh = new JacksonHandle(); + products.setPageLength(11); + p = products.search(qd, 1,jh); +; + System.out.println(jh.get().toString()); + long pageNo=1,count=0; + do{ + count =0; + p = products.search(qd,pageNo); + while(p.hasNext()){ + Artifact a =p.next(); + validateArtifact(a); + assertTrue("Verifying document id is part of the search ids",a.getId()%5==0); + count++; + System.out.println(a.getName()); + } + assertEquals("Page size",count,p.size()); + pageNo=pageNo+p.getPageSize(); + }while(!p.isLastPage() && pageNo<=p.getTotalSize()); + assertEquals("page number after the loop",1,p.getPageNumber()); + assertEquals("total no of pages",1,p.getTotalPages()); + assertEquals("page length from search handle",11,jh.get().path("page-length").asInt()); + assertEquals("Total results from search handle",11,jh.get().path("total").asInt()); + } + + //Below scenario is verifying range query from PojoBuilder + + @Test + public void testPOJORangeSearch() throws Exception { + PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); + PojoPage p; + this.loadSimplePojos(products); + PojoQueryBuilder qb = products.getQueryBuilder(); + QueryDefinition qd = qb.range("inventory", Operator.GE,1055); + JacksonHandle jh = new JacksonHandle(); + products.setPageLength(56); + p = products.search(qd, 1,jh); + assertEquals("total no of pages",1,p.getTotalPages()); + + long pageNo=1,count=0; + do{ + count =0; + p = products.search(qd,pageNo); + while(p.hasNext()){ + Artifact a =p.next(); + validateArtifact(a); + assertTrue("Verifying document id is part of the search ids",a.getId()>=55); + count++; + } + assertEquals("Page size",count,p.size()); + pageNo=pageNo+p.getPageSize(); + }while(!p.isLastPage() && pageNo<=p.getTotalSize()); + assertEquals("page number after the loop",1,p.getPageNumber()); + assertEquals("total no of pages",1,p.getTotalPages()); + assertEquals("page length from search handle",56,jh.get().path("page-length").asInt()); + assertEquals("Total results from search handle",56,jh.get().path("total").asInt()); + } + + //Below scenario is to test range query with options + @Test + public void testPOJORangeQuerySearchWithOptions() { + PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); + PojoPage p; + this.loadSimplePojos(products); + String[] searchOptions ={"uncached","min-occurs=1"}; + PojoQueryBuilder qb = products.getQueryBuilder(); + String[] searchNames = {"counter","special"}; + QueryDefinition qd = qb.range("inventory",searchOptions,Operator.LE,1054); + JacksonHandle jh = new JacksonHandle(); + products.setPageLength(55); + p = products.search(qd, 1,jh); + System.out.println(jh.get().toString()); + assertEquals("total no of pages",1,p.getTotalPages()); + + long pageNo=1,count=0; + do{ + count =0; + p = products.search(qd,pageNo); + while(p.hasNext()){ + Artifact a =p.next(); + validateArtifact(a); + assertTrue("Verifying document id is part of the search ids",a.getId()<=54); + count++; + } + assertEquals("Page size",count,p.size()); + pageNo=pageNo+p.getPageSize(); + }while(!p.isLastPage() && pageNo<=p.getTotalSize()); + assertEquals("page number after the loop",1,p.getPageNumber()); + assertEquals("total no of pages",1,p.getTotalPages()); + assertEquals("page length from search handle",55,jh.get().path("page-length").asInt()); + assertEquals("Total results from search handle",54,jh.get().path("total").asInt()); + } + + //Below scenario is verifying and query with all pojo builder methods + + @Test + public void testPOJOWordSearchWithOptions() throws Exception { + PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); + PojoPage p; + this.loadSimplePojos(products); + + String[] searchOptions ={"case-sensitive","wildcarded","max-occurs=1"}; + String[] rangeOptions ={"uncached","min-occurs=1"}; + PojoQueryBuilder qb = products.getQueryBuilder(); + String[] searchNames = {"Acm*"}; + QueryDefinition qd = qb.and(qb.andNot(qb.word("name",searchOptions, 1.0,searchNames), + qb.containerQueryBuilder("manufacturer", Company.class).value("name","Acme special, Inc.") ), + qb.range("inventory",rangeOptions,Operator.LT,1101)); + + JacksonHandle jh = new JacksonHandle(); + products.setPageLength(25); + p = products.search(qd, 1,jh); + System.out.println(jh.get().toString()); + long pageNo=1,count=0; + do{ + count =0; + p = products.search(qd,pageNo); + while(p.hasNext()){ + Artifact a =p.next(); + validateArtifact(a); + assertTrue("Verifying document id is part of the search ids",a.getId()<1101); + assertFalse("Verifying document has word counter",a.getManufacturer().getName().contains("special")); + count++; + + } + assertEquals("Page size",count,p.size()); + pageNo=pageNo+p.getPageSize(); + }while(!p.isLastPage() && pageNo<=p.getTotalSize()); + assertEquals("page number after the loop",2,p.getPageNumber()); + assertEquals("total no of pages",2,p.getTotalPages()); + assertEquals("page length from search handle",25,jh.get().path("page-length").asInt()); + assertEquals("Total results from search handle",40,jh.get().path("total").asInt()); + } + +} From e8a5c8c178383f484e69e6bce00c063478deabe6 Mon Sep 17 00:00:00 2001 From: sammefford Date: Thu, 9 Oct 2014 11:50:26 -0600 Subject: [PATCH 169/357] uncomment geo path query since it's working now; demonstrate work-around to run filtered searches; remove unused city objects --- .../marklogic/client/test/PojoFacadeTest.java | 38 ++++++++++--------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/test/java/com/marklogic/client/test/PojoFacadeTest.java b/src/test/java/com/marklogic/client/test/PojoFacadeTest.java index 834e12460..96520fb13 100644 --- a/src/test/java/com/marklogic/client/test/PojoFacadeTest.java +++ b/src/test/java/com/marklogic/client/test/PojoFacadeTest.java @@ -34,6 +34,7 @@ import com.marklogic.client.pojo.PojoRepository; import com.marklogic.client.query.QueryDefinition; import com.marklogic.client.query.StringQueryDefinition; +import com.marklogic.client.query.StructuredQueryBuilder; import com.marklogic.client.pojo.PojoQueryBuilder.Operator; import com.marklogic.client.pojo.PojoQueryBuilder; import com.marklogic.client.pojo.annotation.Id; @@ -102,7 +103,7 @@ public void testC_QueryPojos() throws Exception { Iterator iterator = page.iterator(); int numRead = 0; while ( iterator.hasNext() ) { - City city = iterator.next(); + iterator.next(); numRead++; } assertEquals("Failed to find number of records expected", 3, numRead); @@ -115,7 +116,7 @@ public void testC_QueryPojos() throws Exception { iterator = page.iterator(); numRead = 0; while ( iterator.hasNext() ) { - City city = iterator.next(); + iterator.next(); numRead++; } assertEquals("Failed to find number of records expected", 3, numRead); @@ -178,7 +179,7 @@ public void testC_QueryPojos() throws Exception { iterator = page.iterator(); numRead = 0; while ( iterator.hasNext() ) { - City city = iterator.next(); + iterator.next(); numRead++; } assertEquals("Failed to find number of records expected", 50, numRead); @@ -189,34 +190,34 @@ public void testC_QueryPojos() throws Exception { iterator = page.iterator(); numRead = 0; while ( iterator.hasNext() ) { - City city = iterator.next(); + iterator.next(); numRead++; } assertEquals("Failed to find number of records expected", 21, numRead); assertEquals("PojoPage failed to report number of records expected", numRead, page.size()); - // TODO: uncomment tests below once geospatial on JSON is implemented in server - /* - query = qb.geospatial( - qb.geoProperty("latLong"), - qb.circle(-34, -58, 1) + // the default options are unfiltered, which I don't want in this case, so the + // work-around is to use stored options which are filtered + StructuredQueryBuilder sqb = Common.client.newQueryManager().newStructuredQueryBuilder("facets"); + query = sqb.geospatial( + qb.geoPath("latLong"), + qb.circle(-34, -58, 100) ); page = cities.search(query, 1); iterator = page.iterator(); numRead = 0; while ( iterator.hasNext() ) { - @SuppressWarnings("unused") - City city = iterator.next(); + iterator.next(); numRead++; } - // this currently doesn't work even in the cts:search layer - // when this works we'll find out how many we expect - assertEquals("Failed to find number of records expected", -1, numRead); + assertEquals("Failed to find number of records expected", 4, numRead); assertEquals("PojoPage failed to report number of records expected", numRead, page.size()); + // TODO: uncomment tests below once https://bugtrack.marklogic.com/29731 is fixed + /* query = qb.geospatial( - qb.geoPath("latLong"), - qb.circle(-34, -58, 100) + qb.geoProperty("latLong"), + qb.circle(-34, -58, 1) ); page = cities.search(query, 1); iterator = page.iterator(); @@ -226,11 +227,12 @@ public void testC_QueryPojos() throws Exception { City city = iterator.next(); numRead++; } - // this currently doesn't work even in the cts:search layer + // this currently doesn't work in the search:search layer // when this works we'll find out how many we expect assertEquals("Failed to find number of records expected", -1, numRead); assertEquals("PojoPage failed to report number of records expected", numRead, page.size()); + query = qb.geospatial( qb.geoPair("latitude", "longitude"), qb.circle(-34, -58, 100) @@ -243,7 +245,7 @@ public void testC_QueryPojos() throws Exception { City city = iterator.next(); numRead++; } - // this currently doesn't work even in the cts:search layer + // this currently doesn't work even in the search:search layer // when this works we'll find out how many we expect assertEquals("Failed to find number of records expected", -1, numRead); assertEquals("PojoPage failed to report number of records expected", numRead, page.size()); From d8044562bb82fc458d6ef0f8f656c9ff6c37f68a Mon Sep 17 00:00:00 2001 From: skottam Date: Thu, 9 Oct 2014 11:04:23 -0700 Subject: [PATCH 170/357] updated with functions that can enable positions in range index and getting bootstrap host from properties endpoint --- .../marklogic/javaclient/ConnectedRESTQA.java | 49 ++++++++++++++++++- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java b/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java index 6ea12fb49..9a9ee557b 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java @@ -75,6 +75,44 @@ public static void createDB(String dbName) { e.printStackTrace(); } } + public static String getBootStrapHostFromML() { + InputStream jstream=null; + try{ + DefaultHttpClient client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + HttpGet getrequest = new HttpGet("http://localhost:8002/manage/v2/properties?format=json"); + HttpResponse resp = client.execute(getrequest); + jstream =resp.getEntity().getContent(); + JsonNode jnode= new ObjectMapper().readTree(jstream); + String propName ="bootstrap-host"; + if(!jnode.isNull()){ + + if(jnode.has(propName)){ +// System.out.println(jnode.withArray(propName).get(0).asText()); + return jnode.withArray(propName).get(0).asText(); + } + else{ + System.out.println("Missing "+propName+" field from properties end point so sending java conanical host name\n"+jnode.toString()); + return InetAddress.getLocalHost().getCanonicalHostName().toLowerCase(); + } + } + else{ + System.out.println("Rest endpoint returns empty stream"); + return InetAddress.getLocalHost().getCanonicalHostName().toLowerCase(); + } + + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + + return "localhost"; + } + finally{ + jstream =null; + } + } /* * */ @@ -85,7 +123,8 @@ public static void createForest(String fName,String dbName) { new AuthScope("localhost", 8002), new UsernamePasswordCredentials("admin", "admin")); HttpPost post = new HttpPost("http://localhost:8002"+ "/manage/v2/forests?format=json"); - String hName =InetAddress.getLocalHost().getCanonicalHostName().toLowerCase(); +// System.out.println( getBootStrapHostFromML()); + String hName = getBootStrapHostFromML(); String JSONString = "{\"database\":\""+ dbName + @@ -822,6 +861,9 @@ public static void setAutomaticDirectoryCreation(String dbName, String opt) thro * */ public static void addRangeElementIndex(String dbName, String type, String namespace, String localname) throws Exception{ + addRangeElementIndex( dbName, type, namespace, localname, false); + } + public static void addRangeElementIndex(String dbName, String type, String namespace, String localname,boolean positions) throws Exception{ ObjectMapper mapper = new ObjectMapper(); ObjectNode mainNode = mapper.createObjectNode(); // ObjectNode childNode = mapper.createObjectNode(); @@ -831,7 +873,7 @@ public static void addRangeElementIndex(String dbName, String type, String name childNodeObject.put( "namespace-uri", namespace); childNodeObject.put( "localname", localname); childNodeObject.put( "collation", ""); - childNodeObject.put("range-value-positions", false); + childNodeObject.put("range-value-positions", positions); childNodeObject.put("invalid-values", "reject"); childArray.add(childNodeObject); mainNode.putArray("range-element-index").addAll(childArray); @@ -939,6 +981,9 @@ public static void addRangeElementAttributeIndex(String dbName, String type, Str } */ public static void addRangePathIndex(String dbName, String type, String pathexpr, String collation, String invalidValues) throws Exception{ + addRangePathIndex( dbName, type, pathexpr, collation, invalidValues, false); + } + public static void addRangePathIndex(String dbName, String type, String pathexpr, String collation, String invalidValues,boolean positions) throws Exception{ ObjectMapper mapper = new ObjectMapper(); // ObjectNode mainNode = mapper.createObjectNode(); ObjectNode childNode = mapper.createObjectNode(); From ea281bd62c797b95aa2bae3f4424ec1aa7ad4c43 Mon Sep 17 00:00:00 2001 From: skottam Date: Thu, 9 Oct 2014 13:40:55 -0700 Subject: [PATCH 171/357] updating the test after the bug fix --- .../javaclient/TestPOJOQueryBuilderContainerQuery.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderContainerQuery.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderContainerQuery.java index 933b2efc9..ec5faf23a 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderContainerQuery.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderContainerQuery.java @@ -16,8 +16,7 @@ import com.marklogic.client.pojo.PojoQueryBuilder; import com.marklogic.client.pojo.PojoRepository; import com.marklogic.client.query.QueryDefinition; -import com.marklogic.client.query.StructuredQueryBuilder; -import com.marklogic.client.query.StructuredQueryBuilder.Operator; +import com.marklogic.client.pojo.PojoQueryBuilder.Operator; public class TestPOJOQueryBuilderContainerQuery extends BasicJavaClientREST { From 92586ab52aaad75954cb22361469d3b6526d069e Mon Sep 17 00:00:00 2001 From: sammefford Date: Thu, 18 Sep 2014 09:22:54 -0600 Subject: [PATCH 172/357] align this with dev--we're not upgrading jersey on this branch yet --- pom.xml | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 51e2968dd..ad606de64 100644 --- a/pom.xml +++ b/pom.xml @@ -165,9 +165,19 @@ test1",convertXMLDocumentToString(dh.get())); + }else{ + assertEquals("element node ","",convertXMLDocumentToString(dh.get())); + } + } + else if(er.getType().equals(Type.JSON)){ + JacksonHandle jh = new JacksonHandle(); + jh=er.get(jh); +// System.out.println("Type JSON :"+jh.get().toString()); + assertTrue("document has object?",jh.get().has("test")); + } + else if(er.getType().equals(Type.TEXTNODE)){ + assertTrue("document contains",er.getAs(String.class).equals("test1")); +// System.out.println("type txt node :"+er.getAs(String.class)); + + }else if(er.getType().equals(Type.BINARY)){ + FileHandle fh = new FileHandle(); + fh=er.get(fh); +// System.out.println("type binary :"+fh.get().length()); + assertEquals("files size",2,fh.get().length()); + }else if(er.getType().equals(Type.BOOLEAN)){ + assertTrue("Documents exist?",er.getBoolean()); +// System.out.println("type boolean:"+er.getBoolean()); + } + else if(er.getType().equals(Type.INTEGER)){ +// System.out.println("type Integer: "+er.getNumber().longValue()); + assertEquals("count of documents ",31,er.getNumber().intValue()); + } + else if(er.getType().equals(Type.STRING)){ + //There is git issue 152 + assertEquals("String?","xml",er.getString()); + System.out.println("type string: "+er.getString()); + }else if(er.getType().equals(Type.NULL)){ + //There is git issue 151 +// assertNull(er.getAs(String.class)); + System.out.println("Testing is empty sequence is NUll?"+er.getAs(String.class)); + }else if(er.getType().equals(Type.OTHER)){ + //There is git issue 151 + System.out.println("Testing is Others? "+er.getAs(String.class)); +// assertEquals("Returns OTHERs","xdmp:forest-restart#1",er.getString()); + + }else if(er.getType().equals(Type.ANYURI)){ +// System.out.println("Testing is AnyUri? "+er.getAs(String.class)); + assertEquals("Returns me a uri :","test1.xml",er.getAs(String.class)); + + }else if(er.getType().equals(Type.DATE)){ +// System.out.println("Testing is DATE? "+er.getAs(String.class)); + assertEquals("Returns me a date :","2002-03-07-07:00",er.getAs(String.class)); + }else if(er.getType().equals(Type.DATETIME)){ +// System.out.println("Testing is DATETIME? "+er.getAs(String.class)); + assertEquals("Returns me a dateTime :","2010-01-06T18:13:50.874-07:00",er.getAs(String.class)); + + }else if(er.getType().equals(Type.DECIMAL)){ +// System.out.println("Testing is Decimal? "+er.getAs(String.class)); + assertEquals("Returns me a Decimal :","10.5",er.getAs(String.class)); + + }else if(er.getType().equals(Type.DOUBLE)){ +// System.out.println("Testing is Double? "+er.getAs(String.class)); + assertEquals(1.0471975511966,er.getNumber().doubleValue(),0); + + }else if(er.getType().equals(Type.DURATION)){ + System.out.println("Testing is Duration? "+er.getAs(String.class)); +// assertEquals("Returns me a Duration :",0.4903562,er.getNumber().floatValue()); + }else if(er.getType().equals(Type.FLOAT)){ +// System.out.println("Testing is Float? "+er.getAs(String.class)); + assertEquals(20,er.getNumber().floatValue(),0); + }else if(er.getType().equals(Type.GDAY)){ +// System.out.println("Testing is GDay? "+er.getAs(String.class)); + assertEquals("Returns me a GDAY :","---01",er.getAs(String.class)); + }else if(er.getType().equals(Type.GMONTH)){ +// System.out.println("Testing is GMonth "+er.getAs(String.class)); + assertEquals("Returns me a GMONTH :","--01",er.getAs(String.class)); + }else if(er.getType().equals(Type.GMONTHDAY)){ +// System.out.println("Testing is GMonthDay? "+er.getAs(String.class)); + assertEquals("Returns me a GMONTHDAY :","--12-25-14:00",er.getAs(String.class)); + }else if(er.getType().equals(Type.GYEAR)){ +// System.out.println("Testing is GYear? "+er.getAs(String.class)); + assertEquals("Returns me a GYEAR :","2005-12:00",er.getAs(String.class)); + }else if(er.getType().equals(Type.GYEARMONTH)){ +// System.out.println("Testing is GYearMonth?1976-02 "+er.getAs(String.class)); + assertEquals("Returns me a GYEARMONTH :","1976-02",er.getAs(String.class)); + }else if(er.getType().equals(Type.HEXBINARY)){ +// System.out.println("Testing is HEXBINARY? "+er.getAs(String.class)); + assertEquals("Returns me a HEXBINARY :","BEEF",er.getAs(String.class)); + }else if(er.getType().equals(Type.QNAME)){ +// System.out.println("Testing is QNAME integer"+er.getAs(String.class)); + assertEquals("Returns me a QNAME :","integer",er.getAs(String.class)); + }else if(er.getType().equals(Type.TIME)){ +// System.out.println("Testing is TIME? "+er.getAs(String.class)); + assertEquals("Returns me a TIME :","10:00:00",er.getAs(String.class)); + }else if(er.getType().equals(Type.ATTRIBUTE)){ +// System.out.println("Testing is ATTRIBUTE? "+er.getAs(String.class)); + assertEquals("Returns me a ATTRIBUTE :","attribute",er.getAs(String.class)); + + }else if(er.getType().equals(Type.PROCESSINGINSTRUCTION)){ +// System.out.println("Testing is ProcessingInstructions? "+er.getAs(String.class)); + assertEquals("Returns me a PROCESSINGINSTRUCTION :","",er.getAs(String.class)); + }else if(er.getType().equals(Type.COMMENT)){ +// System.out.println("Testing is Comment node? "+er.getAs(String.class)); + assertEquals("Returns me a COMMENT :","",er.getAs(String.class)); + }else if(er.getType().equals(Type.BASE64BINARY)){ +// System.out.println("Testing is Base64Binary "+er.getAs(String.class)); + assertEquals("Returns me a BASE64BINARY :","DEADBEEF",er.getAs(String.class)); + }else{ + System.out.println("Got something which is not belongs to anytype we support "+er.getAs(String.class)); + assertFalse("getting in else part, missing a type ",true); + } + } + } + + //This test intended to verify a simple xquery for inserting and reading documents of different formats and returning boolean,string number types + @Test + public void testXqueryReturningDifferentAutomicTypes() throws Exception { + String insertXML = "xdmp:document-insert(\"test1.xml\",test1)"; + String insertJSON = "xdmp:document-insert(\"test2.json\",object-node {\"test\":\"hello\"})"; + String insertTXT = "xdmp:document-insert(\"/test3.txt\",text { \"This is a text document.\" })"; + String insertBinary="xdmp:document-insert(\"test4.bin\",binary {\"ABCD\"})"; + String query1 = "fn:exists(fn:doc())"; + String query2 = "fn:count(fn:doc())"; + String query3 = "xdmp:database-name(xdmp:database())"; + String readDoc ="fn:doc()"; + + boolean response = client.newServerEval().xquery(insertXML).eval().hasNext(); +// System.out.printlnln(response); + assertFalse("Insert query return empty sequence",response); + response = client.newServerEval().xquery(insertJSON).eval().hasNext(); +// System.out.printlnln(response); + assertFalse("Insert query return empty sequence",response); + response = client.newServerEval().xquery(insertTXT).eval().hasNext(); +// System.out.printlnln(response); + assertFalse("Insert query return empty sequence",response); + response = client.newServerEval().xquery(insertBinary).eval().hasNext(); +// System.out.printlnln(response); + assertFalse("Insert query return empty sequence",response); + boolean response1 = client.newServerEval().xquery(query1).eval().next().getBoolean(); +// System.out.printlnln(response1); + assertTrue("Documents exist?",response1); + int response2 = client.newServerEval().xquery(query2).eval().next().getNumber().intValue(); +// System.out.printlnln(response2); + assertEquals("count of documents ",4,response2); + String response3 = client.newServerEval().xquery(query3).evalAs(String.class); +// System.out.printlnln(response3); + assertEquals("Content database?",dbName,response3); + ServerEvaluationCall evl = client.newServerEval(); + EvalResultIterator evr = evl.xquery(readDoc).eval(); + while(evr.hasNext()) + { + EvalResult er =evr.next(); + if(er.getType().equals(Type.XML)){ + DOMHandle dh = new DOMHandle(); + dh=er.get(dh); + assertEquals("document has content","test1",convertXMLDocumentToString(dh.get())); + } + else if(er.getType().equals(Type.JSON)){ + JacksonHandle jh = new JacksonHandle(); + jh=er.get(jh); + assertTrue("document has object?",jh.get().has("test")); + } + else if(er.getType().equals(Type.TEXTNODE)){ + assertTrue("document contains",er.getAs(String.class).equals("This is a text document.")); + + }else if(er.getType().equals(Type.BINARY)){ + FileHandle fh = new FileHandle(); + fh=er.get(fh); + assertEquals("files size",2,fh.get().length()); + + }else{ + System.out.println("Something went wrong"); + } + + } + } + //This test is intended to test eval(T handle), passing input stream handle with xqueries that retruns different types, formats + @Test + public void testXqueryReturningDifferentTypesAndFormatsWithHandle() throws Exception { + + InputStream inputStream = new FileInputStream("src/test/java/com/marklogic/javaclient/data/xqueries.txt"); + InputStreamHandle ish = new InputStreamHandle(); + ish.set(inputStream); + try{ + EvalResultIterator evr = client.newServerEval().xquery(ish).eval(); + this.validateReturnTypes(evr); + }catch(Exception e){ + throw e; + } + finally{ + inputStream.close(); + } + } + //Test is intended to test different types of variable passed to xquery from java and check return node types,data types + @Test + public void testXqueryDifferentVariableTypes() throws Exception { + + DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + InputSource is = new InputSource(); + is.setCharacterStream(new StringReader("test1")); + Document doc = db.parse(is); + String jsonNode = "{ \"a\" : {\"obj\": \"value\"}, \"b\" : \"s0\",\"c1\" : 1,\"c2\" : 2,\"d\" : null,\"f\" : true,\"g\" : [\"s1\", \"s2\", \"s3\" ]}"; + System.out.println(this.convertXMLDocumentToString(doc)); + try{ + String query1 = "declare namespace test=\"http://marklogic.com/test\";" + +"declare variable $test:myString as xs:string external;" + +"declare variable $myBool as xs:boolean external;" + +"declare variable $myInteger as xs:integer external;" + +"declare variable $myDecimal as xs:decimal external;" + +"declare variable $myDouble as xs:double external;" + +"declare variable $myFloat as xs:float external;" + +"declare variable $myXmlNode as xs:string external;" + +"declare variable $myNull external;" + +"( $test:myString, $myBool,$myInteger,$myDecimal,$myDouble,$myFloat, document{ xdmp:unquote($myXmlNode) }," + +"xdmp:unquote($myXmlNode)//comment(),xdmp:unquote($myXmlNode)//text(),xdmp:unquote($myXmlNode)//*," + +"xdmp:unquote($myXmlNode)/@attr, xdmp:unquote($myXmlNode)//processing-instruction())"; + + ServerEvaluationCall evl= client.newServerEval().xquery(query1); + evl.addNamespace("test", "http://marklogic.com/test") + .addVariable("test:myString", "xml") + .addVariable("myBool", true).addVariable("myInteger", (int)31) + .addVariable("myDecimal", 10.5).addVariable("myDouble", 1.0471975511966) + .addVariable("myFloat",20).addVariableAs("myXmlNode",new DOMHandle(doc)) + .addVariableAs("myNull",(String) null); + EvalResultIterator evr = evl.eval(); + this.validateReturnTypes(evr); + String query2 = "declare variable $myArray as json:array external;" + +"declare variable $myObject as json:object external;" + +"declare variable $myJsonNode as xs:string external;" + +"($myArray,$myObject,xdmp:unquote($myJsonNode)/a,xdmp:unquote($myJsonNode)/b,xdmp:unquote($myJsonNode)/c1," + +"xdmp:unquote($myJsonNode)/d,xdmp:unquote($myJsonNode)/f,xdmp:unquote($myJsonNode)/g )"; + evl= client.newServerEval().xquery(query2); + evl.addVariableAs("myArray", new ObjectMapper().createArrayNode().add(1).add(2).add(3)) + .addVariableAs("myObject", new ObjectMapper().createObjectNode().put("foo", "v1").putNull("testNull")) + .addVariableAs("myXmlNode",new DOMHandle(doc) ).addVariableAs("myNull", null) + .addVariableAs("myJsonNode", new StringHandle(jsonNode).withFormat(Format.JSON)); + evr = evl.eval(); + while(evr.hasNext()) + { + EvalResult er =evr.next(); + if(er.getType().equals(Type.JSON)){ + JacksonHandle jh = new JacksonHandle(); + jh=er.get(jh); + + if(jh.get().isArray()){ +// System.out.println("Type Array :"+jh.get().toString()); + assertEquals("array value at index 0 ",1,jh.get().get(0).asInt()); + assertEquals("array value at index 1 ",2,jh.get().get(1).asInt()); + assertEquals("array value at index 2 ",3,jh.get().get(2).asInt()); + } + else if(jh.get().isObject()){ + System.out.println("Type Object :"+jh.get().toString()); + if(jh.get().has("foo")){ + assertNull("this object also has null node",jh.get().get("testNull").textValue()); + }else if(jh.get().has("obj")) + { + assertEquals("Value of the object is ","value",jh.get().get("obj").asText()); + }else{ + assertFalse("getting a wrong object ",true); + } + + } + else if(jh.get().isNumber()){ +// System.out.println("Type Number :"+jh.get().toString()); + assertEquals("Number value",1,jh.get().asInt()); + } + else if(jh.get().isNull()){ +// System.out.println("Type Null :"+jh.get().toString()); + assertNull("Returned Null",jh.get().textValue()); + } + else if(jh.get().isBoolean()){ +// System.out.println("Type boolean :"+jh.get().toString()); + assertTrue("Boolean value returned false",jh.get().asBoolean()); + } + else{ +// System.out.println("Running into different types than expected"); + assertFalse("Running into different types than expected",true); + } + + } + else if(er.getType().equals(Type.TEXTNODE)){ + assertTrue("document contains",er.getAs(String.class).contains("s")); +// System.out.println("type txt node :"+er.getAs(String.class)); + }else{ + System.out.println("No corresponding type found for :"+er.getType()); + } + } + + }catch(Exception e){ + throw e; + } + } + + + @Test(expected = java.lang.IllegalStateException.class) + public void testMultipleXqueryfnOnServerEval() { + String insertQuery = "xdmp:document-insert(\"test1.xml\",test1)"; + String query1 = "fn:exists(fn:doc())"; + String query2 = "fn:count(fn:doc())"; + String query3 = "xdmp:database-name(xdmp:database())"; + + boolean response1 = client.newServerEval().xquery(query1).xquery(insertQuery).eval().next().getBoolean(); + System.out.println(response1); + int response2 = client.newServerEval().xquery(query2).eval().next().getNumber().intValue(); + System.out.println(response2); + String response3 = client.newServerEval().xquery(query3).evalAs(String.class); + System.out.println(response3); + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/data/xqueries.txt b/test-complete/src/test/java/com/marklogic/javaclient/data/xqueries.txt new file mode 100644 index 000000000..8e7ada888 --- /dev/null +++ b/test-complete/src/test/java/com/marklogic/javaclient/data/xqueries.txt @@ -0,0 +1,112 @@ + +xdmp:document-insert("test1.xml",test1); +xdmp:document-insert("test2.json",object-node {"test":"hello"}); +xdmp:document-insert("/test3.txt",text { "test1" }); +xdmp:document-insert("test4.bin",binary {"ABCD"}); + +(:returns sequence:) +fn:doc(); + +(:returns boolean:) +fn:exists(fn:doc()), + +(:returns unsignedLong +fn:count(fn:doc()), +:) +(:returns String +xdmp:database-name(xdmp:database()); +:) +(:returns attribute:) +fn:doc("test1.xml")//@attr; + +(:returns anyURI:) +fn:base-uri(fn:doc()/*[@attr eq "attribute"]); + +(:returns Double:) +math:acos(.5); + +(:returns NULL:) +(); + +(:returns OTHER:) +xdmp:functions()[2]; + +(: returns XML document :) +cts:search(fn:doc(), cts:directory-query(("/"),"1")); + +(:returns DATE:) +fn:adjust-date-to-timezone(xs:date("2002-03-07")); + +(:returns DATETIME:) +xdmp:parse-dateTime("[Y0001]-[M01]-[D01]T[h01]:[m01]:[s01].[f1][Z]","2010-01-06T17:13:50.873594-08:00"); + +(:returns DECIMAL:) +fn:seconds-from-time(xs:time("13:20:10.5")); + +(:returns DURATION:) +fn:timezone-from-date(xs:date("2000-06-12Z")); + +(:returns FLOAT:) + cts:circle-radius(cts:circle(20, cts:point(37.655983, -122.425525))); + +(:returns Integer +xdmp:octal-to-integer("12345670"); +:) + +(:returns HEXBINARY:) +data(xdmp:subbinary(binary { xs:hexBinary("DEADBEEF") }, 3, 2)); + +(:returns INTEGER:) +fn:day-from-date(xs:date("1999-05-31-05:00")); + +(:returns QNAME:) +xdmp:type(10); + +(:returns NCName:) +let $qn := fn:QName("http://www.w3.org/XML/1998/namespace", "lang") +return +fn:prefix-from-QName(fn:node-name( attribute {$qn} {"en"})); + + +(:returns TIME:) +fn:adjust-time-to-timezone(xs:time("10:00:00"), ()); + +(:returns BINARY:) +(xdmp:subbinary(binary { xs:hexBinary("DEADBEEF") }, 3, 2)); + + +(:returns TEXTNODE :) +doc('/test3.xml')//text(); + +(: returns element NODE:) + +for $name in doc('test1.xml')//text() +return (element {lower-case($name)} {''}); + +(:returns COMMENT:) +let $cmt:= +return +$cmt//comment(); + +(:returns PROCESSINGINSTRUCTION:) +let $cmt:= +return +$cmt//processing-instruction(); + +(:returns GDAY:) +xs:gDay("---01"); + +(:returns GMONTH:) +xs:gMonth("--01"); + +(:returns GMONTHDAY:) +xs:gMonthDay("--12-25-14:00"); + +(:returns GYEAR:) +xs:gYear("2005-12:00"); + +(:returns GYEARMONTH:) +xs:gYearMonth("1976-02"); + +(:returns BASE64BINARY:) + xs:base64Binary("DEADBEEF"); \ No newline at end of file From c705e4eed1397c74e7766e45f9d76355c45f0ead Mon Sep 17 00:00:00 2001 From: sammefford Date: Fri, 24 Oct 2014 08:10:40 -0600 Subject: [PATCH 223/357] fix #13, default connections in examples to 8000, document that we no longer need to create a REST server before running the examples --- src/main/resources/Example.properties | 2 +- src/main/resources/example/README.txt | 18 ++++-------------- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/src/main/resources/Example.properties b/src/main/resources/Example.properties index c519792d4..3160ec3b1 100644 --- a/src/main/resources/Example.properties +++ b/src/main/resources/Example.properties @@ -6,5 +6,5 @@ example.writer_password=x example.admin_user=rest-admin example.admin_password=x example.host=localhost -example.port=8012 +example.port=8000 example.authentication_type=digest diff --git a/src/main/resources/example/README.txt b/src/main/resources/example/README.txt index 4ac80cd82..ed1b2d822 100644 --- a/src/main/resources/example/README.txt +++ b/src/main/resources/example/README.txt @@ -1,5 +1,5 @@ MarkLogic Java API Examples -October 2013 +October 2014 == Cookbook ============= @@ -8,16 +8,9 @@ for common tasks using the MarkLogic Java API. Before using the examples, perform the following actions: -1. Install MarkLogic 7 for your chosen platform. +1. Install MarkLogic 8 for your chosen platform. -2. Set up a new REST API instance for your database. You can use Information -Studio at: - - http://YOUR_HOST:8000/appservices/ - - to create a new RESTful Server for a database. - -3. Edit Example.properties to specify the connection parameters for the +2. Edit Example.properties to specify the connection parameters for the REST API instance. You must identify users for the rest-writer and rest-admin roles. To @@ -27,9 +20,7 @@ REST API instance. If you make the user name the same as the role name and set the password to "x" you will not need to modify those values in the - properties file. Change the example.port property to - the port number of the REST Server you created in step 2 of these - instructions. + properties file. The examples illustrate the following common tasks: @@ -44,7 +35,6 @@ The examples illustrate the following common tasks: * DocumentFormats work with binary, JSON, text, and XML documents * DocumentOutputStream supply content while writing to the database * JAXBDocument write and read a POJO using JAXB -* KeyValueSearch search for documents based on values * QueryOptions configure search with saved options * StringSearch search with string criteria and saved options * StructuredSearch search with a criteria structure and saved From 98ecd1523f2d427a115f2b69a558e6dc1020cc83 Mon Sep 17 00:00:00 2001 From: skottam Date: Fri, 24 Oct 2014 10:20:36 -0700 Subject: [PATCH 224/357] adding test for issue 156 --- .../marklogic/javaclient/TestEvalXquery.java | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestEvalXquery.java b/test-complete/src/test/java/com/marklogic/javaclient/TestEvalXquery.java index cdf0729e6..700b5d549 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestEvalXquery.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestEvalXquery.java @@ -8,6 +8,7 @@ import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.TransformerException; import org.json.JSONObject; @@ -52,14 +53,14 @@ public class TestEvalXquery extends BasicJavaClientREST { @BeforeClass public static void setUpBeforeClass() throws Exception { System.out.println("In setup"); -// setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); } @AfterClass public static void tearDownAfterClass() throws Exception { System.out.println("In tear down" ); -// tearDownJavaRESTServer(dbName, fNames, restServerName); + tearDownJavaRESTServer(dbName, fNames, restServerName); } @Before @@ -390,4 +391,29 @@ public void testMultipleXqueryfnOnServerEval() { String response3 = client.newServerEval().xquery(query3).evalAs(String.class); System.out.println(response3); } + //Issue 156 exist for this + @Test(expected = com.marklogic.client.FailedRequestException.class) + public void testXqueryWithExtVarAsNode() throws Exception { + DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + InputSource is = new InputSource(); + is.setCharacterStream(new StringReader(" test1")); + Document doc = db.parse(is); + try{ + String query1 = "declare variable $myXmlNode as node() external;" + +"document{ xdmp:unquote($myXmlNode) }"; + ServerEvaluationCall evl= client.newServerEval().xquery(query1); + evl.addVariableAs("myXmlNode",new DOMHandle(doc)); + EvalResultIterator evr = evl.eval(); + while(evr.hasNext()) + { + EvalResult er =evr.next(); + DOMHandle dh = new DOMHandle(); + dh=er.get(dh); +// System.out.println("Type XML :"+convertXMLDocumentToString(dh.get())); + assertEquals("document has content","test1",convertXMLDocumentToString(dh.get())); + } + }catch(Exception e){ + throw e; + } + } } From a07b37f0e6bbdfc3dba22a8a559225ab6b71bd9d Mon Sep 17 00:00:00 2001 From: Ajit George Date: Fri, 24 Oct 2014 12:52:42 -0700 Subject: [PATCH 225/357] Added the changes requested in the review meeting. --- .../TestPOJOWithDocsStoredByOthers.java | 1130 +++++++++-------- 1 file changed, 604 insertions(+), 526 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithDocsStoredByOthers.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithDocsStoredByOthers.java index 03c84f9e2..9635f563b 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithDocsStoredByOthers.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithDocsStoredByOthers.java @@ -1,526 +1,604 @@ -package com.marklogic.javaclient; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -import java.util.Calendar; - -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.MarkLogicIOException; -import com.marklogic.client.document.DocumentManager.Metadata; -import com.marklogic.client.document.DocumentPatchBuilder; -import com.marklogic.client.document.DocumentWriteSet; -import com.marklogic.client.document.JSONDocumentManager; -import com.marklogic.client.io.DocumentMetadataHandle; -import com.marklogic.client.io.DocumentMetadataHandle.Capability; -import com.marklogic.client.io.DocumentMetadataHandle.DocumentCollections; -import com.marklogic.client.io.DocumentMetadataHandle.DocumentPermissions; -import com.marklogic.client.io.DocumentMetadataHandle.DocumentProperties; -import com.marklogic.client.io.Format; -import com.marklogic.client.io.JacksonHandle; -import com.marklogic.client.pojo.PojoRepository; -import com.marklogic.client.pojo.annotation.Id; -import com.marklogic.javaclient.TestPOJOMissingIdGetSetMethod.SmallArtifactMissingGetter; - -/* - * This class has test methods that check POJO's ability to read and report errors - * (negative test cases). - * - * NOTE: If this class is renamed, then variable docId (URI) of the POJO documents needs to changed accordingly. - * NOTE: Also change the type value present within the json string to proper class name. - */ -public class TestPOJOWithDocsStoredByOthers extends BasicJavaClientREST { - - private static String dbName = "TestPOJOWithDocsStoredByOthersDB"; - private static String[] fNames = { "TestPOJOWithDocsStoredByOthersDB-1" }; - private static String restServerName = "REST-Java-Client-API-Server"; - - private static int restPort = 8011; - private DatabaseClient client; - - /* - * This class is used as a POJO class to read documents stored as a JSON. - * Class member name has been annotated with @Id. - * annotated. - */ - public static class SmallArtifact { - @Id - public String name; - private long id; - private int inventory; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public long getId() { - return id; - } - - public void setId(long id) { - this.id = id; - } - - public int getInventory() { - return inventory; - } - - public void setInventory(int inventory) { - this.inventory = inventory; - } - } - - /* - * This class is used as a POJO class to read documents stored as a POJO. - * Class member name has been annotated with @Id in the super class. - * Has an additional string member. - */ - public static class SmallArtifactSuper extends SmallArtifact { - private String originCountry; - - public String getOriginCountry() { - return originCountry; - } - - public void setOriginCountry(String originCountry) { - this.originCountry = originCountry; - } - - } - - /* - * This class is used as a POJO class to read documents stored as a POJO. - * Class member name has been annotated with @Id in the super class and also in this class. - * Has an additional string member. - */ - public static class SmallArtifactSuperAndSub extends SmallArtifact { - @Id - public String name; - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - private String originCountry; - - public String getOriginCountry() { - return originCountry; - } - - public void setOriginCountry(String originCountry) { - this.originCountry = originCountry; - } - - } - - /* - * This class is used as a POJO class to read documents stored as a JSON. - * Used to test different access specifiers - * Private Class member name has been annotated with @Id. - * annotated. - */ - public static class SmallArtifactPrivate { - private String name; - private long id; - private int inventory; - @Id - public String getName() { - return name; - } - @Id - public void setName(String name) { - this.name = name; - } - - public long getId() { - return id; - } - - public void setId(long id) { - this.id = id; - } - - public int getInventory() { - return inventory; - } - - public void setInventory(int inventory) { - this.inventory = inventory; - } - } - - /* - * This class is used as a POJO class to read documents stored as a JSON. - * Used to test different access specifiers - * Private Class member name has been annotated with @Id. - * annotated. - */ - public static class SmallArtifactPublic extends SmallArtifactPrivate { - public String name; - private String originCountry; - - @Id - public String getName() { - return name; - } - - @Id - public void setName(String name) { - this.name = name; - } - - - public String getOriginCountry() { - return originCountry; - } - - public void setOriginCountry(String originCountry) { - this.originCountry = originCountry; - } - } - - @BeforeClass - public static void setUpBeforeClass() throws Exception { - // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", - // "debug"); - System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName, restPort); - } - - @AfterClass - public static void tearDownAfterClass() throws Exception { - System.out.println("In tear down"); - tearDownJavaRESTServer(dbName, fNames, restServerName); - } - - @Before - public void setUp() throws Exception { - client = DatabaseClientFactory.newClient("localhost", restPort, - "rest-admin", "x", Authentication.DIGEST); - } - - @After - public void tearDown() throws Exception { - // release client - client.release(); - } - - public DocumentMetadataHandle setMetadata() { - // create and initialize a handle on the meta-data - DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); - metadataHandle.getCollections().addAll("my-collection1", - "my-collection2"); - metadataHandle.getPermissions().add("app-user", Capability.UPDATE, - Capability.READ); - metadataHandle.getProperties().put("reviewed", true); - metadataHandle.getProperties().put("myString", "foo"); - metadataHandle.getProperties().put("myInteger", 10); - metadataHandle.getProperties().put("myDecimal", 34.56678); - metadataHandle.getProperties().put("myCalendar", - Calendar.getInstance().get(Calendar.YEAR)); - metadataHandle.setQuality(23); - return metadataHandle; - } - - /* - * This method is used when there is a need to validate SmallArtifact. - */ - public void validateSmallArtifact(SmallArtifact artifact) { - assertNotNull("Artifact object should never be Null", artifact); - assertNotNull("Id should never be Null", artifact.id); - assertEquals("Id of the object is ", -99, artifact.getId()); - assertEquals("Name of the object is ", "SmallArtifact", - artifact.getName()); - assertEquals("Inventory of the object is ", 1000, - artifact.getInventory()); - } - - /* - * This method is used when there is a need to validate SmallArtifactSuper to test annotation only in super class. - */ - public void validateSmallArtifactSuper(SmallArtifactSuper artifact) { - assertNotNull("Artifact object should never be Null", artifact); - assertNotNull("Id should never be Null", artifact.getId()); - assertEquals("Id of the object is ", -99, artifact.getId()); - assertEquals("Name of the object is ", "SmallArtifact", - artifact.getName()); - assertEquals("Inventory of the object is ", 1000, - artifact.getInventory()); - assertEquals("Country of origin of the object is ", "USA", - artifact.getOriginCountry()); - } - - /* - * This method is used when there is a need to validate SmallArtifactSuperAndSub to test annotation in super class - * and in sub class. - */ - public void validateSmallArtifactSuperAndSub(SmallArtifactSuperAndSub artifact) { - assertNotNull("Artifact object should never be Null", artifact); - assertNotNull("Id should never be Null", artifact.getId()); - assertEquals("Id of the object is ", -100, artifact.getId()); - assertEquals("Name of the object is ", "SmallArtifact", - artifact.getName()); - assertEquals("Inventory of the object is ", 1000, - artifact.getInventory()); - assertEquals("Country of origin of the object is ", "USA", - artifact.getOriginCountry()); - } - - /* - * This method is used when there is a need to validate SmallArtifactPublic to test annotation in super class - * and in sub class. - */ - public void validateSmallArtifactDiffAccessSpec(SmallArtifactPublic artifact) { - assertNotNull("Artifact object should never be Null", artifact); - assertNotNull("Id should never be Null", artifact.getId()); - assertEquals("Id of the object is ", -100, artifact.getId()); - assertEquals("Name of the object is ", "SmallArtifact", - artifact.getName()); - assertEquals("Inventory of the object is ", 1000, - artifact.getInventory()); - assertEquals("Country of origin of the object is ", "USA", - artifact.getOriginCountry()); - } - - - /* - * Purpose : This test is to validate read documents with valid POJO - * specific URI and has invalid POJO collection Uses SmallArtifact class - * which has @Id on the name methods. Test result expectations are: read - * should return a null since the POJO internal collection and document's - * are different. - * - * Current results (10/13/2014) are: - * java.lang.IllegalArgumentException: Invalid type id 'junk' (for id type 'Id.class'): no such class found - * Issue 136 might solve this also. - */ - - @Test - public void testPOJOReadDocStoredWithInvalidCollection() throws Exception { - - String docId[] = { "com.marklogic.javaclient.TestPOJOWithDocsStoredByOthers$SmallArtifact/SmallArtifact.json" }; - String json1 = new String( - "{\"junk\":" - + "{\"name\": \"SmallArtifact\",\"id\": -99, \"inventory\": 1000}}"); - JSONDocumentManager docMgr = client.newJSONDocumentManager(); - docMgr.setMetadataCategories(Metadata.ALL); - DocumentWriteSet writeset = docMgr.newWriteSet(); - // put meta-data - DocumentMetadataHandle mh = setMetadata(); - - ObjectMapper mapper = new ObjectMapper(); - - JacksonHandle jacksonHandle1 = new JacksonHandle(); - - JsonNode junkNode = mapper.readTree(json1); - jacksonHandle1.set(junkNode); - jacksonHandle1.withFormat(Format.JSON); - - writeset.addDefault(mh); - writeset.add(docId[0], jacksonHandle1); - - docMgr.write(writeset); - - PojoRepository pojoReposSmallArtifact = client - .newPojoRepository(SmallArtifact.class, String.class); - String artifactName = new String("SmallArtifact"); - - SmallArtifact artifact1 = pojoReposSmallArtifact.read(artifactName); - validateSmallArtifact(artifact1); - } - - /* - * Purpose : This test is to validate read documents with valid POJO - * specific URI, and type and has missing POJO fields Uses SmallArtifact - * class which has @Id on the name methods. Inventory bean property is - * missing from document insert. Test result expectations: The POJO object - * returned should be defaulting to Java default for the type (int) for - * inventory field. - */ - @Test - public void testPOJOReadDocStoredWithNoBeanProperty() throws Exception { - - String docId[] = { "com.marklogic.javaclient.TestPOJOWithDocsStoredByOthers$SmallArtifact/SmallArtifact.json" }; - String json1 = new String( - "{\"com.marklogic.javaclient.TestPOJOWithDocsStoredByOthers$SmallArtifact\":" - + "{\"name\": \"SmallArtifact\",\"id\": -99}}"); - - JSONDocumentManager docMgr = client.newJSONDocumentManager(); - docMgr.setMetadataCategories(Metadata.ALL); - DocumentWriteSet writeset = docMgr.newWriteSet(); - // put meta-data - DocumentMetadataHandle mh = setMetadata(); - - ObjectMapper mapper = new ObjectMapper(); - - JacksonHandle jacksonHandle1 = new JacksonHandle(); - - JsonNode noInventoryNode = mapper.readTree(json1); - jacksonHandle1.set(noInventoryNode); - jacksonHandle1.withFormat(Format.JSON); - - writeset.addDefault(mh); - writeset.add(docId[0], jacksonHandle1); - - docMgr.write(writeset); - - PojoRepository pojoReposSmallArtifact = client - .newPojoRepository(SmallArtifact.class, String.class); - String artifactName = new String("SmallArtifact"); - - // Validate the SmallArtifact read back. - SmallArtifact artifact = pojoReposSmallArtifact.read(artifactName); - assertNotNull("Artifact object should never be Null", artifact); - assertNotNull("Id should never be Null", artifact.id); - assertEquals("Id of the object is ", -99, artifact.getId()); - assertEquals("Name of the object is ", "SmallArtifact", - artifact.getName()); - assertEquals("Inventory of the object is ", 0, artifact.getInventory()); - } - - /* - * Purpose : This test is to validate read documents with valid POJO - * specific URI and has invalid data-types for one of the bean property. - * Uses SmallArtifact class which has @Id on the name methods. Test result - * expectations are: read should return exception. - * Field inventory has a String - */ - - @Test(/*expected=MarkLogicIOException.class*/) - public void testPOJOReadDocStoredWithInvalidDataType() throws Exception { - - String docId[] = { "com.marklogic.javaclient.TestPOJOWithDocsStoredByOthers$SmallArtifact/SmallArtifact.json" }; - String json1 = new String( - "{\"com.marklogic.javaclient.TestPOJOWithDocsStoredByOthers$SmallArtifact\":" - + "{\"name\": \"SmallArtifact\",\"id\": -99, \"inventory\": \"String\"}}"); - JSONDocumentManager docMgr = client.newJSONDocumentManager(); - docMgr.setMetadataCategories(Metadata.ALL); - DocumentWriteSet writeset = docMgr.newWriteSet(); - // put meta-data - DocumentMetadataHandle mh = setMetadata(); - // add to POJO URI collection. - mh.getCollections().addAll("com.marklogic.javaclient.TestPOJOWithDocsStoredByOthers$SmallArtifact/SmallArtifact.json", - "com.marklogic.javaclient.TestPOJOWithDocsStoredByOthers$SmallArtifact/SmallArtifact.json"); - - ObjectMapper mapper = new ObjectMapper(); - - JacksonHandle jacksonHandle1 = new JacksonHandle(); - - JsonNode invalidDataTypeNode = mapper.readTree(json1); - jacksonHandle1.set(invalidDataTypeNode); - jacksonHandle1.withFormat(Format.JSON); - - writeset.addDefault(mh); - writeset.add(docId[0], jacksonHandle1); - - docMgr.write(writeset); - - PojoRepository pojoReposSmallArtifact = client - .newPojoRepository(SmallArtifact.class, String.class); - String artifactName = new String("SmallArtifact"); - - @SuppressWarnings("unused") - SmallArtifact artifact1 = pojoReposSmallArtifact.read(artifactName); - } - - /* - * Purpose : This test is to validate Test creating an @id in super class only - * SmallArtifactSuper class which has @Id only on the name class member of the super class SmallArtifact. - * - * Current results (10/13/2014) are: - * java.lang.IllegalArgumentException: - * Your class com.marklogic.javaclient.TestPOJOWithDocsStoredByOthers$SmallArtifactSuper does not have a method or field annotated with com.marklogic.client.pojo.annotation.Id - * - */ - @Test - public void testPOJOWriteReadSuper() throws Exception { - - PojoRepository pojoReposSmallArtifact = client - .newPojoRepository(SmallArtifactSuper.class, String.class); - String artifactName = new String("SmallArtifact"); - - SmallArtifactSuper art = new SmallArtifactSuper(); - art.setId(0); - art.setInventory(1000); - art.setName(artifactName); - art.setOriginCountry("USA"); - - // Load the object into database - pojoReposSmallArtifact.write(art,"com.marklogic.javaclient.TestPOJOMissingIdGetSetMethod$SmallArtifactSuper/SmallArtifactSuper.json"); - - SmallArtifactSuper artifact1 = pojoReposSmallArtifact.read(artifactName); - validateSmallArtifactSuper(artifact1); - } - - /* - * Purpose : This test is to validate Test creating an @id in super class and sub class. - * Both SmallArtifactSuperAndSub and SmallArtifact class have a similar @Id on the name class member. - * - * Current results (10/13/2014) are: Read works fine. - * - */ - @Test - public void testPOJOWriteReadSuperAndSub() throws Exception { - - PojoRepository pojoReposSmallArtifact = client - .newPojoRepository(SmallArtifactSuperAndSub.class, String.class); - String artifactName = new String("SmallArtifact"); - - SmallArtifactSuperAndSub art = new SmallArtifactSuperAndSub(); - art.setId(-100); - art.setInventory(1000); - art.setName(artifactName); - art.setOriginCountry("USA"); - - // Load the object into database - pojoReposSmallArtifact.write(art,"com.marklogic.javaclient.TestPOJOMissingIdGetSetMethod$SmallArtifactSuper/SmallArtifactSuper.json"); - - SmallArtifactSuperAndSub artifact1 = pojoReposSmallArtifact.read(artifactName); - validateSmallArtifactSuperAndSub(artifact1); - } - - /* - * Purpose : This test is to validate creating an @id in super class and sub class that has different - * access specifiers. - * Both SmallArtifactSuperAndSub and SmallArtifact class have a similar @Id on the name class member. - * - * Current results (10/13/2014) are: Read works fine. - * - */ - @Test - public void testPOJOWriteReadDiffAccessSpecifiers() throws Exception { - - PojoRepository pojoReposSmallArtifact = client - .newPojoRepository(SmallArtifactPublic.class, String.class); - String artifactName = new String("SmallArtifact"); - - SmallArtifactPublic art = new SmallArtifactPublic(); - art.setId(-100); - art.setInventory(1000); - art.setName(artifactName); - art.setOriginCountry("USA"); - - // Load the object into database - pojoReposSmallArtifact.write(art,"com.marklogic.javaclient.TestPOJOMissingIdGetSetMethod$SmallArtifactSuper/SmallArtifactSuper.json"); - - SmallArtifactPublic artifact1 = pojoReposSmallArtifact.read(artifactName); - validateSmallArtifactDiffAccessSpec(artifact1); - } -} +package com.marklogic.javaclient; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.Calendar; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.MarkLogicIOException; +import com.marklogic.client.document.DocumentManager.Metadata; +import com.marklogic.client.document.DocumentPatchBuilder; +import com.marklogic.client.document.DocumentWriteSet; +import com.marklogic.client.document.JSONDocumentManager; +import com.marklogic.client.io.DocumentMetadataHandle; +import com.marklogic.client.io.DocumentMetadataHandle.Capability; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentCollections; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentPermissions; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentProperties; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.JacksonHandle; +import com.marklogic.client.pojo.PojoRepository; +import com.marklogic.client.pojo.annotation.Id; +import com.marklogic.javaclient.TestPOJOMissingIdGetSetMethod.SmallArtifactMissingGetter; + +public class TestPOJOWithDocsStoredByOthers extends BasicJavaClientREST { + + private static String dbName = "TestPOJOWithDocsStoredByOthersDB"; + private static String[] fNames = { "TestPOJOWithDocsStoredByOthersDB-1" }; + private static String restServerName = "REST-Java-Client-API-Server"; + + private static int restPort = 8011; + private DatabaseClient client; + + /* + * This class is used as a POJO class to read documents stored as a JSON. + * Class member name has been annotated with @Id. + * annotated. + */ + public static class SmallArtifactIdInSuper { + @Id + public String name; + private long id; + private int inventory; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public int getInventory() { + return inventory; + } + + public void setInventory(int inventory) { + this.inventory = inventory; + } + } + + /* + * This class is used as a POJO class to read documents stored as a POJO. + * Class member name has been annotated with @Id in the super class. + * Has an additional string member. + */ + public static class SmallArtifactNoId extends SmallArtifactIdInSuper { + private String originCountry; + + public String getOriginCountry() { + return originCountry; + } + + public void setOriginCountry(String originCountry) { + this.originCountry = originCountry; + } + + } + + /* + * This class is used as a POJO class to read documents stored as a POJO. + * Class member name has been annotated with @Id in the super class and also in this class. + * Has an additional string member. + */ + public static class SmallArtifactIdInSuperAndSub extends SmallArtifactIdInSuper { + @Id + public String name; + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + private String originCountry; + + public String getOriginCountry() { + return originCountry; + } + + public void setOriginCountry(String originCountry) { + this.originCountry = originCountry; + } + + } + + /* + * This class is used as a POJO class to read documents stored as a JSON. + * Used to test different access specifiers + * Private Class member name has been annotated with @Id. + * annotated. + */ + public static class SmallArtifactPrivate { + private String name; + private long id; + private int inventory; + @Id + public String getName() { + return name; + } + @Id + public void setName(String name) { + this.name = name; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public int getInventory() { + return inventory; + } + + public void setInventory(int inventory) { + this.inventory = inventory; + } + } + + /* + * This class is used as a POJO class to read documents stored as a JSON. + * Used to test different access specifiers + * Private Class member name has been annotated with @Id. + * annotated. + */ + public static class SmallArtifactPublic extends SmallArtifactPrivate { + public String name; + private String originCountry; + + @Id + public String getName() { + return name; + } + + @Id + public void setName(String name) { + this.name = name; + } + + + public String getOriginCountry() { + return originCountry; + } + + public void setOriginCountry(String originCountry) { + this.originCountry = originCountry; + } + } + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", + // "debug"); + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName, restPort); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } + + @Before + public void setUp() throws Exception { + client = DatabaseClientFactory.newClient("localhost", restPort, + "rest-admin", "x", Authentication.DIGEST); + } + + @After + public void tearDown() throws Exception { + // release client + client.release(); + } + + public DocumentMetadataHandle setMetadata() { + // create and initialize a handle on the meta-data + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); + metadataHandle.getCollections().addAll("my-collection1", + "my-collection2"); + metadataHandle.getPermissions().add("app-user", Capability.UPDATE, + Capability.READ); + metadataHandle.getProperties().put("reviewed", true); + metadataHandle.getProperties().put("myString", "foo"); + metadataHandle.getProperties().put("myInteger", 10); + metadataHandle.getProperties().put("myDecimal", 34.56678); + metadataHandle.getProperties().put("myCalendar", + Calendar.getInstance().get(Calendar.YEAR)); + metadataHandle.setQuality(23); + return metadataHandle; + } + + /* + * This method is used when there is a need to validate SmallArtifact. + */ + public void validateSmallArtifact(SmallArtifactIdInSuper artifact) { + assertNotNull("Artifact object should never be Null", artifact); + assertNotNull("Id should never be Null", artifact.id); + assertEquals("Id of the object is ", -99, artifact.getId()); + assertEquals("Name of the object is ", "SmallArtifact", + artifact.getName()); + assertEquals("Inventory of the object is ", 1000, + artifact.getInventory()); + } + + /* + * This method is used when there is a need to validate SmallArtifactSuper to test annotation only in super class. + */ + public void validateSmallArtifactSuper(SmallArtifactNoId artifact) { + assertNotNull("Artifact object should never be Null", artifact); + assertNotNull("Id should never be Null", artifact.getId()); + assertEquals("Id of the object is ", -99, artifact.getId()); + assertEquals("Name of the object is ", "SmallArtifactInSuperOnly", + artifact.getName()); + assertEquals("Inventory of the object is ", 1000, + artifact.getInventory()); + assertEquals("Country of origin of the object is ", "USA", + artifact.getOriginCountry()); + } + + /* + * This method is used when there is a need to validate SmallArtifactSuperAndSub to test annotation in super class + * and in sub class. + */ + public void validateSmallArtifactSuperAndSub(SmallArtifactIdInSuperAndSub artifact) { + assertNotNull("Artifact object should never be Null", artifact); + assertNotNull("Id should never be Null", artifact.getId()); + assertEquals("Id of the object is ", -100, artifact.getId()); + assertEquals("Name of the object is ", "SmallArtifactInSuperAndSub", + artifact.getName()); + assertEquals("Inventory of the object is ", 1000, + artifact.getInventory()); + assertEquals("Country of origin of the object is ", "USA", + artifact.getOriginCountry()); + } + + /* + * This method is used when there is a need to validate SmallArtifactPublic to test annotation in super class + * and in sub class. + */ + public void validateSmallArtifactDiffAccessSpec(SmallArtifactPublic artifact) { + assertNotNull("Artifact object should never be Null", artifact); + assertNotNull("Id should never be Null", artifact.getId()); + assertEquals("Id of the object is ", -100, artifact.getId()); + assertEquals("Name of the object is ", "SmallArtifactDiffAccess", + artifact.getName()); + assertEquals("Inventory of the object is ", 1000, + artifact.getInventory()); + assertEquals("Country of origin of the object is ", "USA", + artifact.getOriginCountry()); + } + + + /* + * This method is used when there is a need to validate SmallArtifactIdInSuper to test annotation in super class + * and in sub class. + */ + public void validateSubObjReferencedbySuperClassvariable(SmallArtifactIdInSuper artifact) { + assertNotNull("Artifact object should never be Null", artifact); + assertNotNull("Id should never be Null", artifact.getId()); + assertEquals("Id of the object is ", -100, artifact.getId()); + assertEquals("Name of the object is ", "SmallArtifactNoId", + artifact.getName()); + assertEquals("Inventory of the object is ", 1000, + artifact.getInventory()); + } + + /* + * This method is used when there is a need to validate SmallArtifactIdInSuperAndSub to test annotation in super class + * and in sub class. + */ + public void validateSubObjReferencedbySuperClassvariableOne(SmallArtifactIdInSuperAndSub artifact) { + assertNotNull("Artifact object should never be Null", artifact); + assertNotNull("Id should never be Null", artifact.getId()); + assertEquals("Id of the object is ", -100, artifact.getId()); + assertEquals("Name of the object is ", "SmallArtifactIdInSuperAndSub", + artifact.getName()); + assertEquals("Inventory of the object is ", 1000, + artifact.getInventory()); + } + + /* + * Purpose : This test is to validate read documents with valid POJO + * specific URI and has invalid POJO collection Uses SmallArtifact class + * which has @Id on the name methods. Test result expectations are: read + * should return a null since the POJO internal content and document's + * are different. + * + * Current results (10/13/2014) are: + * java.lang.IllegalArgumentException: Invalid type id 'junk' (for id type 'Id.class'): no such class found + * Issue 136 might solve this also. + */ + + @Test + public void testPOJOReadDocStoredWithInvalidContent() throws Exception { + + String docId[] = { "com.marklogic.javaclient.TestPOJOWithDocsStoredByOthers$SmallArtifactIdInSuper/SmallArtifactIdInSuper.json" }; + String json1 = new String( + "{\"junk\":" + + "{\"name\": \"SmallArtifactIdInSuper\",\"id\": -99, \"inventory\": 1000}}"); + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); + DocumentWriteSet writeset = docMgr.newWriteSet(); + // put meta-data + DocumentMetadataHandle mh = setMetadata(); + + ObjectMapper mapper = new ObjectMapper(); + + JacksonHandle jacksonHandle1 = new JacksonHandle(); + + JsonNode junkNode = mapper.readTree(json1); + jacksonHandle1.set(junkNode); + jacksonHandle1.withFormat(Format.JSON); + + writeset.addDefault(mh); + writeset.add(docId[0], jacksonHandle1); + + docMgr.write(writeset); + + PojoRepository pojoReposSmallArtifact = client + .newPojoRepository(SmallArtifactIdInSuper.class, String.class); + String artifactName = new String("SmallArtifactIdInSuper"); + + SmallArtifactIdInSuper artifact1 = pojoReposSmallArtifact.read(artifactName); + validateSmallArtifact(artifact1); + } + + /* + * Purpose : This test is to validate read documents with valid POJO + * specific URI, and type and has missing POJO fields Uses SmallArtifact + * class which has @Id on the name methods. Inventory bean property is + * missing from document insert. Test result expectations: The POJO object + * returned should be defaulting to Java default for the type (int) for + * inventory field. + */ + @Test + public void testPOJOReadDocStoredWithNoBeanProperty() throws Exception { + + String docId[] = { "com.marklogic.javaclient.TestPOJOWithDocsStoredByOthers$SmallArtifactIdInSuper/SmallArtifactIdInSuper.json" }; + String json1 = new String( + "{\"com.marklogic.javaclient.TestPOJOWithDocsStoredByOthers$SmallArtifactIdInSuper\":" + + "{\"name\": \"SmallArtifactIdInSuper\",\"id\": -99}}"); + + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); + DocumentWriteSet writeset = docMgr.newWriteSet(); + // put meta-data + DocumentMetadataHandle mh = setMetadata(); + + ObjectMapper mapper = new ObjectMapper(); + + JacksonHandle jacksonHandle1 = new JacksonHandle(); + + JsonNode noInventoryNode = mapper.readTree(json1); + jacksonHandle1.set(noInventoryNode); + jacksonHandle1.withFormat(Format.JSON); + + writeset.addDefault(mh); + writeset.add(docId[0], jacksonHandle1); + + docMgr.write(writeset); + + PojoRepository pojoReposSmallArtifact = client + .newPojoRepository(SmallArtifactIdInSuper.class, String.class); + String artifactName = new String("SmallArtifactIdInSuper"); + + // Validate the SmallArtifactIdInSuper read back. + SmallArtifactIdInSuper artifact = pojoReposSmallArtifact.read(artifactName); + assertNotNull("Artifact object should never be Null", artifact); + assertNotNull("Id should never be Null", artifact.id); + assertEquals("Id of the object is ", -99, artifact.getId()); + assertEquals("Name of the object is ", "SmallArtifactIdInSuper", + artifact.getName()); + assertEquals("Inventory of the object is ", 0, artifact.getInventory()); + } + + /* + * Purpose : This test is to validate read documents with valid POJO + * specific URI and has invalid data-types for one of the bean property. + * Uses SmallArtifact class which has @Id on the name methods. Test result + * expectations are: read should return exception. + * Field inventory has a String + */ + + @Test(/*expected=MarkLogicIOException.class*/) + public void testPOJOReadDocStoredWithInvalidDataType() throws Exception { + + String docId[] = { "com.marklogic.javaclient.TestPOJOWithDocsStoredByOthers$SmallArtifact/SmallArtifact.json" }; + String json1 = new String( + "{\"com.marklogic.javaclient.TestPOJOWithDocsStoredByOthers$SmallArtifact\":" + + "{\"name\": \"SmallArtifact\",\"id\": -99, \"inventory\": \"String\"}}"); + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); + DocumentWriteSet writeset = docMgr.newWriteSet(); + // put meta-data + DocumentMetadataHandle mh = setMetadata(); + // add to POJO URI collection. + mh.getCollections().addAll("com.marklogic.javaclient.TestPOJOWithDocsStoredByOthers$SmallArtifact/SmallArtifact.json", + "com.marklogic.javaclient.TestPOJOWithDocsStoredByOthers$SmallArtifact/SmallArtifact.json"); + + ObjectMapper mapper = new ObjectMapper(); + + JacksonHandle jacksonHandle1 = new JacksonHandle(); + + JsonNode invalidDataTypeNode = mapper.readTree(json1); + jacksonHandle1.set(invalidDataTypeNode); + jacksonHandle1.withFormat(Format.JSON); + + writeset.addDefault(mh); + writeset.add(docId[0], jacksonHandle1); + + docMgr.write(writeset); + + PojoRepository pojoReposSmallArtifact = client + .newPojoRepository(SmallArtifactIdInSuper.class, String.class); + String artifactName = new String("SmallArtifact"); + + @SuppressWarnings("unused") + SmallArtifactIdInSuper artifact1 = pojoReposSmallArtifact.read(artifactName); + } + + /* + * Purpose : This test is to validate Test creating an @id in super class only + * SmallArtifactSuper class which has @Id only on the name class member of the super class SmallArtifact. + * + * Current results (10/13/2014) are: + * java.lang.IllegalArgumentException: + * Your class com.marklogic.javaclient.TestPOJOWithDocsStoredByOthers$SmallArtifactSuper does not have a method or field annotated with com.marklogic.client.pojo.annotation.Id + * + */ + @Test + public void testPOJOWriteReadSuper() throws Exception { + + PojoRepository pojoReposSmallArtifact = client + .newPojoRepository(SmallArtifactNoId.class, String.class); + String artifactName = new String("SmallArtifactInSuperOnly"); + + SmallArtifactNoId art = new SmallArtifactNoId(); + art.setId(0); + art.setInventory(1000); + art.setName(artifactName); + art.setOriginCountry("USA"); + + // Load the object into database + pojoReposSmallArtifact.write(art,"com.marklogic.javaclient.TestPOJOMissingIdGetSetMethod$SmallArtifactSuper/SmallArtifactSuper.json"); + + SmallArtifactNoId artifact1 = pojoReposSmallArtifact.read(artifactName); + validateSmallArtifactSuper(artifact1); + } + + /* + * Purpose : This test is to validate Test creating an @id in super class and sub class. + * Both SmallArtifactSuperAndSub and SmallArtifact class have a similar @Id on the name class member. + * + * Current results (10/13/2014) are: Read works fine. + * + */ + @Test + public void testPOJOWriteReadSuperAndSub() throws Exception { + + PojoRepository pojoReposSmallArtifact = client + .newPojoRepository(SmallArtifactIdInSuperAndSub.class, String.class); + String artifactName = new String("SmallArtifactInSuperAndSub"); + + SmallArtifactIdInSuperAndSub art = new SmallArtifactIdInSuperAndSub(); + art.setId(-100); + art.setInventory(1000); + art.setName(artifactName); + art.setOriginCountry("USA"); + + // Load the object into database + pojoReposSmallArtifact.write(art,"com.marklogic.javaclient.TestPOJOMissingIdGetSetMethod$SmallArtifactSuper/SmallArtifactSuper.json"); + + SmallArtifactIdInSuperAndSub artifact1 = pojoReposSmallArtifact.read(artifactName); + validateSmallArtifactSuperAndSub(artifact1); + } + + /* + * Purpose : This test is to validate creating an @id in super class and sub class that has different + * access specifiers. + * Both SmallArtifactSuperAndSub and SmallArtifact class have a similar @Id on the name class member. + * + * Current results (10/13/2014) are: Read works fine. + * + */ + @Test + public void testPOJOWriteReadDiffAccessSpecifiers() throws Exception { + + PojoRepository pojoReposSmallArtifact = client + .newPojoRepository(SmallArtifactPublic.class, String.class); + String artifactName = new String("SmallArtifactDiffAccess"); + + SmallArtifactPublic art = new SmallArtifactPublic(); + art.setId(-100); + art.setInventory(1000); + art.setName(artifactName); + art.setOriginCountry("USA"); + + // Load the object into database + pojoReposSmallArtifact.write(art,"com.marklogic.javaclient.TestPOJOMissingIdGetSetMethod$SmallArtifactSuper/SmallArtifactSuper.json"); + + SmallArtifactPublic artifact1 = pojoReposSmallArtifact.read(artifactName); + validateSmallArtifactDiffAccessSpec(artifact1); + } + + /* + * Purpose : This test is to validate creating an sub class which is references by a Super class variable type. + * Both SmallArtifactIdInSuper and SmallArtifactNoId classes are used. + * POJO repository cannot read back the sub class. + * + * PojoRepository is on the super class. + */ + + @Test + public void testPOJOSubObjReferencedBySuperClassVariable() throws Exception { + + PojoRepository pojoReposSmallArtifact = client + .newPojoRepository(SmallArtifactIdInSuper.class, String.class); + String artifactName = new String("SmallArtifactNoId"); + + SmallArtifactIdInSuper art = new SmallArtifactNoId(); + art.setId(-100); + art.setInventory(1000); + art.setName(artifactName); + + // Load the object into database + pojoReposSmallArtifact.write(art,"SubClassObjectReferencedBySuperClassVariable"); + + // POJO repository cannot read back the sub class. Compiler complains. + SmallArtifactIdInSuper artifact1 = pojoReposSmallArtifact.read(artifactName); + validateSubObjReferencedbySuperClassvariable(artifact1); + } + + /* + * Purpose : This test is to validate creating an sub class which is references by a Super class variable type. + * Both SmallArtifactIdInSuper and SmallArtifactNoId classes are used. + * This is a variation of testPOJOSubObjReferencedBySuperClassVariable() + * + * PojoRepository is on the sub class. + */ + + @Test + public void testPOJOSubObjReferencedBySuperClassVariableOne() throws Exception { + + PojoRepository pojoReposSmallArtifact = client + .newPojoRepository(SmallArtifactIdInSuperAndSub.class, String.class); + String artifactName = new String("SmallArtifactIdInSuperAndSub"); + + SmallArtifactIdInSuper art = new SmallArtifactIdInSuperAndSub(); + art.setId(-100); + art.setInventory(1000); + art.setName(artifactName); + + // Load the object into database + // POJO repository cannot write using super class reference class. Needs an explicit cast else compiler complains. + pojoReposSmallArtifact.write((SmallArtifactIdInSuperAndSub)art,"SubClassObjectReferencedBySuperClassVariableOne"); + + SmallArtifactIdInSuperAndSub artifact1 = pojoReposSmallArtifact.read(artifactName); + validateSubObjReferencedbySuperClassvariableOne(artifact1); + } + +} From 6b0513a3235a0207e323b8656805c27c8d8c3529 Mon Sep 17 00:00:00 2001 From: Ajit George Date: Fri, 24 Oct 2014 13:01:58 -0700 Subject: [PATCH 226/357] New test class to validate Jackson Annotations (Without Serialize). --- .../TestJacksonAnnotationsTest.java | 536 ++++++++++++++++++ 1 file changed, 536 insertions(+) create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/TestJacksonAnnotationsTest.java diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestJacksonAnnotationsTest.java b/test-complete/src/test/java/com/marklogic/javaclient/TestJacksonAnnotationsTest.java new file mode 100644 index 000000000..910782cf9 --- /dev/null +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestJacksonAnnotationsTest.java @@ -0,0 +1,536 @@ +package com.marklogic.javaclient; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.Calendar; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonValue; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.DocumentMetadataHandle; +import com.marklogic.client.io.DocumentMetadataHandle.Capability; +import com.marklogic.client.pojo.PojoRepository; +import com.marklogic.client.pojo.annotation.Id; + +/* + * This test is designed to to test available Jackson Annotations. + * Separate POJO classes with aJackson annotation will be used. + * refer to each static class for further description of its purpose. + */ + +public class TestJacksonAnnotationsTest extends BasicJavaClientREST { + + private static String dbName = "TestJacksonAnnotationsTestDB"; + private static String[] fNames = { "TestJacksonAnnotationsTest-1" }; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client; + private long negativeId = -1L; + + /* + * This class is similar to the SmallArtifact class. It is used to test + * Jackson Annotation + * + * @JsonIgnoreProperties on inventory field. The field is ignored at class + * level. + * + * Class member name has been annotated with @Id. Expected result: Field + * inventory should not be serialized. + */ + @JsonIgnoreProperties(value = { "inventory" }) + public static class IgnoreProperties { + + @Id + public String name; + private long id; + private int inventory; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public int getInventory() { + return inventory; + } + + public void setInventory(int inventory) { + this.inventory = inventory; + } + } + + /* + * This class is similar to the IgnoreProperties class. It is used to test + * Jackson Annotation + * + * @JsonIgnore on inventory field. The field is ignored at field level. + * + * Class member name has been annotated with @Id. Expected result: Field + * inventory should not be serialized. + */ + + public static class IgnoreFields { + + @Id + public String name; + private long id; + @JsonIgnore + private int inventory; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public int getInventory() { + return inventory; + } + + public void setInventory(int inventory) { + this.inventory = inventory; + } + } + + /* + * This class is similar to the IgnoreProperties class. It is used to test + * Jackson Annotation + * + * @JsonProperty on different fields. + * + * Class member name has been annotated with @Id. property to serialize when + * applied to getter method. property to de-serialize when applied to setter + * method. Expected result: Id field is not serialized / de-serialized. + * Expected Results: @JsonProperty overrides @JsonIgnore. Refer to inventory + * field. + */ + + public static class JsonPropertyCheck { + + @Id + public String name; + @JsonIgnore + private long id; + @JsonIgnore + private int inventory; + private String batch; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + // property to serialize when applied to getter method. + @JsonProperty + public int getInventory() { + return inventory; + } + + // property to de-serialize when applied to setter method. + @JsonProperty + public void setInventory(int inventory) { + this.inventory = inventory; + } + + // property to serialize when applied to getter method. + @JsonProperty + public String getBatch() { + return batch; + } + + // property to de-serialize when applied to setter method. + @JsonProperty + public void setBatch(String batch) { + this.batch = batch; + } + } + + /* + * This class is similar to the JsonPropertyCheck class. It is used to test + * Jackson Annotation + * + * @JsonValue on a method whose return String value is used to produce JSON + * value serialization. + * + * Class member name has been annotated with @Id. property to serialize when + * applied to getter method. property to de-serialize when applied to setter + * method. Expected result: Id field is not serialized / de-serialized. + * Expected Results: @JsonProperty overrides @JsonIgnore. Refer to inventory + * field. + */ + + public static class JsonValueCheck { + + @Id + public String name; + private long id; + private int inventory; + private String batch; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + // property to serialize when applied to getter method. + + public int getInventory() { + return inventory; + } + + public void setInventory(int inventory) { + this.inventory = inventory; + } + + public String getBatch() { + return batch; + } + + public void setBatch(String batch) { + this.batch = batch; + } + + @JsonValue + public String jsonValueSerialized() { + return new String( + "JsonValueCheck class is a customized serialied one"); + } + } + + public long getNegativeId() { + return negativeId; + } + + public void setNegativeId(long longNegativeId) { + negativeId = longNegativeId; + } + + // Decrement and return the current negative Id. + public long getOneNegativeLongId() { + long lTemp = getNegativeId(); + setNegativeId(lTemp - 1); + return lTemp == -1 ? -1 : lTemp; + } + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", + // "debug"); + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName, restPort); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + System.out.println("In tear down"); + tearDownJavaRESTServer(dbName, fNames, restServerName); + } + + @Before + public void setUp() throws Exception { + client = DatabaseClientFactory.newClient("localhost", restPort, + "rest-admin", "x", Authentication.DIGEST); + } + + @After + public void tearDown() throws Exception { + // release client + client.release(); + } + + public DocumentMetadataHandle setMetadata() { + // create and initialize a handle on the meta-data + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); + metadataHandle.getCollections().addAll("my-collection1", + "my-collection2"); + metadataHandle.getPermissions().add("app-user", Capability.UPDATE, + Capability.READ); + metadataHandle.getProperties().put("reviewed", true); + metadataHandle.getProperties().put("myString", "foo"); + metadataHandle.getProperties().put("myInteger", 10); + metadataHandle.getProperties().put("myDecimal", 34.56678); + metadataHandle.getProperties().put("myCalendar", + Calendar.getInstance().get(Calendar.YEAR)); + metadataHandle.setQuality(23); + return metadataHandle; + } + + /* + * This method is used when there is a need to validate IgnoreProperties. + */ + public void validateIgnoreProperties(IgnoreProperties artifact, long temp) { + assertNotNull("Artifact object should never be Null", artifact); + assertNotNull("Id should never be Null", artifact.id); + assertEquals("Id of the object is ", temp, artifact.getId()); + assertEquals("Name of the object is ", "IgnoreProperties", + artifact.getName()); + assertEquals("Inventory of the object is ", 0, artifact.getInventory()); + } + + /* + * This method is used when there is a need to validate IgnoreFields. + */ + public void validateIgnoreFields(IgnoreFields artifact, long temp) { + assertNotNull("Artifact object should never be Null", artifact); + assertNotNull("Id should never be Null", artifact.id); + assertEquals("Id of the object is ", temp, artifact.getId()); + assertEquals("Name of the object is ", "IgnoreFields", + artifact.getName()); + assertEquals("Inventory of the object is ", 0, artifact.getInventory()); + } + + /* + * This method is used when there is a need to validate JsonPropertyCheck. + * Id field is not serialized and hence its default return value should be 0 + */ + public void validateJsonPropertyCheck(JsonPropertyCheck artifact, long temp) { + assertNotNull("Artifact object should never be Null", artifact); + assertNotNull("Id should never be Null", artifact.id); + assertEquals("Id of the object is ", 0, artifact.getId()); + assertEquals("Name of the object is ", "JsonProperty", + artifact.getName()); + assertEquals("Batch Name of the object is ", "Batch One", + artifact.getBatch()); + assertEquals("Inventory of the object is ", 1000, + artifact.getInventory()); + } + + /* + * Purpose : This test is to validate @JsonIgnoreProperties with valid POJO. + * Uses IgnoreProperties class which has @Id on the name methods. Test + * result expectations are: Java Default 0L is read back into the object. + */ + + @Test + public void testPOJOJsonIgnoreProperties() throws Exception { + + String artifactName = "IgnoreProperties"; + + PojoRepository pojoReposIgnoreProperties = client + .newPojoRepository(IgnoreProperties.class, String.class); + + IgnoreProperties artifactOrig = new IgnoreProperties(); + long tempId = getOneNegativeLongId(); + + artifactOrig.setId(tempId); + artifactOrig.setInventory(1000); + artifactOrig.setName(artifactName); + + // Write the object to the database. + pojoReposIgnoreProperties.write(artifactOrig, "IgnoreProperties"); + + IgnoreProperties artifact1 = pojoReposIgnoreProperties + .read(artifactName); + validateIgnoreProperties(artifact1, tempId); + } + + /* + * Purpose : This test is to validate @JsonIgnoreProperties with valid POJO + * instance. Uses IgnoreProperties class which has @Id on the name methods. + * Test result expectations are: Mapper's write should not contain field - + * inventory. + * + * Test method testPOJOJsonIgnoreProperties does a database store and then a + * read back and in that the Java Default 0 is read back into the object. + */ + @Test + public void testPOJOJsonIgnorePropertiesObject() throws Exception { + + ObjectMapper mapper = new ObjectMapper(); + + IgnoreProperties artifact1 = new IgnoreProperties(); + String artifactName = mapper.writeValueAsString(artifact1); + // Field inventory should not be available. + assertFalse(artifactName.contains("inventory")); + + } + + /* + * Purpose : This test is to validate @JsonIgnore with valid POJO. Uses + * IgnorFields class which has @Id on the name methods. Test result + * expectations are: Java Default 0L is read back into the object. + */ + + @Test + public void testPOJOJsonIgnoreField() throws Exception { + + String artifactName = "IgnoreFields"; + + PojoRepository pojoReposIgnoreProperties = client + .newPojoRepository(IgnoreFields.class, String.class); + + IgnoreFields artifactOrig = new IgnoreFields(); + long tempId = getOneNegativeLongId(); + + artifactOrig.setId(tempId); + artifactOrig.setInventory(1000); + artifactOrig.setName(artifactName); + + // Write the object to the database. + pojoReposIgnoreProperties.write(artifactOrig, "IgnoreFields"); + + IgnoreFields artifact1 = pojoReposIgnoreProperties.read(artifactName); + validateIgnoreFields(artifact1, tempId); + } + + /* + * Purpose : This test is to validate @JsonIgnore with valid POJO instance. + * Uses IgnoreFields class which has @Id on the name methods. Test result + * expectations are: Mapper's write should not contain field - inventory. + * + * Test method testPOJOJsonIgnoreFields does a database store and then a + * read back and in that the Java Default 0 is read back into the object. + */ + @Test + public void testPOJOJsonIgnoreFieldsObject() throws Exception { + + ObjectMapper mapper = new ObjectMapper(); + + IgnoreFields artifact1 = new IgnoreFields(); + String artifactName = mapper.writeValueAsString(artifact1); + // Field inventory should not be available. + assertFalse(artifactName.contains("inventory")); + } + + /* + * Purpose : This test is to validate @JsonProperty with valid POJO. Uses + * JsonPropertyCheck class which has @Id on the name methods. Test result + * expectations are: Java Default 0L is read back into the object. + * + * Field id is not annotation. Its Getter/Setter are also not annotated. + * Hence that field is not serialized nor de-serialized. + */ + + @Test + public void testPOJOJsonProperty() throws Exception { + + String artifactName = "JsonProperty"; + + PojoRepository pojoReposIgnoreProperties = client + .newPojoRepository(JsonPropertyCheck.class, String.class); + + JsonPropertyCheck artifactOrig = new JsonPropertyCheck(); + long tempId = getOneNegativeLongId(); + + artifactOrig.setId(tempId); + // Id is not serilized. + artifactOrig.setInventory(1000); + artifactOrig.setName(artifactName); + artifactOrig.setBatch("Batch One"); + + // Write the object to the database. + pojoReposIgnoreProperties.write(artifactOrig, "JasonProperty"); + + JsonPropertyCheck artifact1 = pojoReposIgnoreProperties + .read(artifactName); + validateJsonPropertyCheck(artifact1, tempId); + } + + /* + * Purpose : This test is to validate @JsonIgnore with valid POJO instance. + * Uses JsonPropertyCheck class which has @Id on the name methods. Test + * result expectations are: Mapper's write should not contain field - id. + * + * Test method testPOJOJsonIgnoreFields does a database store and then a + * read back and in that the Java Default 0 is read back into the object. + */ + @Test + public void testPOJOJsonJsonPropertyObject() throws Exception { + + ObjectMapper mapper = new ObjectMapper(); + String instanceName = "JsonProperty"; + long tempId = getOneNegativeLongId(); + + JsonPropertyCheck artifactOrig = new JsonPropertyCheck(); + artifactOrig.setId(tempId); + artifactOrig.setInventory(1000); + artifactOrig.setName(instanceName); + artifactOrig.setBatch("Batch One"); + + String artifactName = mapper.writeValueAsString(artifactOrig); + // Field inventory should not be available. + assertFalse(artifactName.contains("id")); + } + + /* + * Purpose : This test is to validate @JsonValue with valid POJO instance. + * Uses JsonValueCheck class which has @Id on the name methods. Test result + * expectations are: Mapper's write should serialize customized strings as + * JSON. + */ + @Test + public void testPOJOJsonJsonValueObject() throws Exception { + + ObjectMapper mapper = new ObjectMapper(); + String instanceName = "JsonValue"; + long tempId = getOneNegativeLongId(); + + JsonValueCheck artifactOrig = new JsonValueCheck(); + artifactOrig.setId(tempId); + artifactOrig.setInventory(1000); + artifactOrig.setName(instanceName); + artifactOrig.setBatch("Batch One"); + + String artifactName = mapper.writeValueAsString(artifactOrig); + // Assert that the customized string is serialized JSON string. + assertTrue(artifactName + .contains("JsonValueCheck class is a customized serialied one")); + } +} From f1e9d83dbac1b0b543993b6dcf534a6aa90e9ee5 Mon Sep 17 00:00:00 2001 From: sammefford Date: Fri, 24 Oct 2014 16:54:19 -0600 Subject: [PATCH 227/357] avoid NPE when handle comes back null --- .../com/marklogic/client/impl/ServerEvaluationCallImpl.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/marklogic/client/impl/ServerEvaluationCallImpl.java b/src/main/java/com/marklogic/client/impl/ServerEvaluationCallImpl.java index 3b1638a54..66f939a62 100644 --- a/src/main/java/com/marklogic/client/impl/ServerEvaluationCallImpl.java +++ b/src/main/java/com/marklogic/client/impl/ServerEvaluationCallImpl.java @@ -136,7 +136,9 @@ public T evalAs(Class responseType) { ContentHandle readHandle = handleRegistry.makeHandle(responseType); if ( readHandle == null ) return null; - return eval(readHandle).get(); + readHandle = eval(readHandle); + if ( readHandle == null ) return null; + return readHandle.get(); } @Override From 494ca1a60765e8709102435acd268ddbd2ecf210 Mon Sep 17 00:00:00 2001 From: sammefford Date: Fri, 24 Oct 2014 16:55:20 -0600 Subject: [PATCH 228/357] fix #129, send along database parameter to REST at least for create, exists, write, read, read bulk, patch, search, and delete --- .../marklogic/client/impl/JerseyServices.java | 11 ++- .../test/DatabaseClientFactoryTest.java | 97 ++++++++++++++++++- 2 files changed, 103 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/marklogic/client/impl/JerseyServices.java b/src/main/java/com/marklogic/client/impl/JerseyServices.java index e1418c2a2..3b93db717 100644 --- a/src/main/java/com/marklogic/client/impl/JerseyServices.java +++ b/src/main/java/com/marklogic/client/impl/JerseyServices.java @@ -1737,6 +1737,9 @@ private MultivaluedMap makeDocumentParams(String uri, } } addEncodedParam(docParams, "uri", uri); + if ( database != null ) { + addEncodedParam(docParams, "database", database); + } if (categories == null || categories.size() == 0) { docParams.add("category", "content"); } else { @@ -1993,6 +1996,10 @@ private T search(RequestLogger reqlog, Class as, QueryDefinition queryDef private JerseySearchRequest generateSearchRequest(RequestLogger reqlog, QueryDefinition queryDef, String mimetype, MultivaluedMap params) { + if ( database != null ) { + if ( params == null ) params = new MultivaluedMapImpl(); + addEncodedParam(params, "database", database); + } return new JerseySearchRequest(reqlog, queryDef, mimetype, params); } @@ -4375,7 +4382,9 @@ private WebResource.Builder makeBuilder(String path, MultivaluedMap params, Object inputMimetype, Object outputMimetype) { if ( params == null ) params = new MultivaluedMapImpl(); - if ( database != null ) params.add("database", database); + if ( database != null ) { + addEncodedParam(params, "database", database); + } WebResource resource = connection.path(path).queryParams(params); WebResource.Builder builder = resource.getRequestBuilder(); diff --git a/src/test/java/com/marklogic/client/test/DatabaseClientFactoryTest.java b/src/test/java/com/marklogic/client/test/DatabaseClientFactoryTest.java index 8ba0e9391..556b67644 100644 --- a/src/test/java/com/marklogic/client/test/DatabaseClientFactoryTest.java +++ b/src/test/java/com/marklogic/client/test/DatabaseClientFactoryTest.java @@ -16,22 +16,41 @@ package com.marklogic.client.test; import static org.junit.Assert.*; +import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; + +import java.io.IOException; import org.apache.http.client.HttpClient; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; +import org.xml.sax.SAXException; import com.marklogic.client.DatabaseClient; import com.marklogic.client.DatabaseClientFactory; import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.ResourceNotFoundException; +import com.marklogic.client.document.DocumentDescriptor; +import com.marklogic.client.document.DocumentManager; +import com.marklogic.client.document.DocumentPage; +import com.marklogic.client.document.DocumentPatchBuilder; +import com.marklogic.client.document.DocumentUriTemplate; +import com.marklogic.client.document.GenericDocumentManager; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.eval.ServerEvaluationCall; import com.marklogic.client.extra.httpclient.HttpClientConfigurator; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.StringHandle; +import com.marklogic.client.io.marker.DocumentPatchHandle; +import com.marklogic.client.query.MatchDocumentSummary; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.StringQueryDefinition; public class DatabaseClientFactoryTest { @BeforeClass public static void beforeClass() { - Common.connect(); - //System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + Common.connectEval(); + System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); } @AfterClass public static void afterClass() { @@ -44,17 +63,87 @@ public void testConnectStringIntStringStringDigest() { } @Test - public void testRuntimeDatabaseSelection() { + public void testRuntimeDatabaseSelection() throws SAXException, IOException { DatabaseClient tmpClient = DatabaseClientFactory.newClient( Common.HOST, Common.PORT, "Triggers", Common.EVAL_USERNAME, Common.EVAL_PASSWORD, Authentication.DIGEST ); - assertNotNull("Factory could not create client with digest connection", tmpClient); + assertNotNull("Factory could not create client", tmpClient); String database = tmpClient.newServerEval() .xquery("xdmp:database-name(xdmp:database())") .evalAs(String.class); assertEquals("Runtime database is wrong", "Triggers", database); tmpClient.release(); + + tmpClient = DatabaseClientFactory.newClient( + Common.HOST, Common.PORT, "Documents", Common.EVAL_USERNAME, Common.EVAL_PASSWORD, Authentication.DIGEST + ); + assertNotNull("Factory could not create client", tmpClient); + XMLDocumentManager runtimeDbDocMgr = tmpClient.newXMLDocumentManager(); + // test that doc creation happens in the Documents db + String docContents = "
hello"; + DocumentUriTemplate template = runtimeDbDocMgr.newDocumentUriTemplate(".xml").withDirectory("/test/"); + StringHandle handle = new StringHandle(docContents).withFormat(Format.XML); + DocumentDescriptor createDesc = runtimeDbDocMgr.create(template, handle); + String docUri = createDesc.getUri(); + // a reusable server eval call from the java-unittest db which just gets the doc contents in the Documents db + ServerEvaluationCall getHello = + Common.client.newServerEval() + .javascript("xdmp.eval('fn.doc(\"" + docUri + "\")', " + + "null, {database:xdmp.database(\"Documents\")})"); + // make sure we can see the doc + String value = getHello.evalAs(String.class); + assertXMLEqual("Doc contents incorrect", docContents, value); + + // test that the doc exists via the DocumentManager api + DocumentDescriptor existsDesc = runtimeDbDocMgr.exists(docUri); + assertNotNull("Doc " + docUri + " should exist in the Documents db", existsDesc); + + // test overwriting the contents of the doc + String docContents2 = "hello2"; + runtimeDbDocMgr.write(docUri, new StringHandle(docContents2).withFormat(Format.XML)); + + // use read to make sure the doc got the update + String value2 = runtimeDbDocMgr.readAs(docUri, String.class); + assertXMLEqual("Doc contents incorrect", docContents2, value2); + + // test searching + QueryManager runtimeDbQueryMgr = tmpClient.newQueryManager(); + StringQueryDefinition query = runtimeDbQueryMgr.newStringDefinition(); + query.setCriteria("hello2"); + query.setDirectory("/test/"); + MatchDocumentSummary match = runtimeDbQueryMgr.findOne(query); + assertNotNull("Should get a doc back", match); + assertEquals("URL doesn't match", docUri, match.getUri()); + + // test patching + String newValue = "new value"; + String docContents3 = "" + newValue + ""; + DocumentPatchBuilder patchBldr = runtimeDbDocMgr.newPatchBuilder(); + patchBldr.replaceValue("/a", newValue); + DocumentPatchHandle patchHandle = patchBldr.build(); + runtimeDbDocMgr.patch(docUri, patchHandle); + + // use bulk read to make sure the doc got the update + DocumentPage docs = runtimeDbDocMgr.read(docUri); + assertNotNull("Should get a doc back", docs); + assertTrue("Should get a doc back", docs.hasNext()); + String value3 = docs.next().getContent(new StringHandle()).get(); + assertXMLEqual("Doc contents incorrect", docContents3, value3); + + // test deleting the doc + runtimeDbDocMgr.delete(docUri); + boolean deleted = false; + try { + runtimeDbDocMgr.readAs(docUri, String.class); + } catch (ResourceNotFoundException e) { + deleted = true; + } + assertTrue("Doc still exists", deleted); + String value4 = getHello.evalAs(String.class); + assertEquals("Eval response wrong", null, value4); + + tmpClient.release(); } @Test From 80ec1b625082f6124efec061be96a5c158b94030 Mon Sep 17 00:00:00 2001 From: skottam Date: Sat, 25 Oct 2014 10:10:12 -0700 Subject: [PATCH 229/357] adding a create/delete userPrevilage method, running the test as non admin user --- .../marklogic/javaclient/ConnectedRESTQA.java | 127 +++++++++++++++++- .../marklogic/javaclient/TestEvalXquery.java | 10 +- 2 files changed, 128 insertions(+), 9 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java b/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java index 6089851aa..038298321 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java @@ -28,6 +28,7 @@ import com.marklogic.client.DatabaseClient; import com.marklogic.client.admin.ServerConfigurationManager; import com.marklogic.client.io.DocumentMetadataHandle; +import com.marklogic.client.io.DocumentMetadataHandle.Capability; import java.net.InetAddress; @@ -207,12 +208,18 @@ public static void assocRESTServer(String restServerName,String dbName,int restP post.setEntity(new StringEntity(JSONString)); HttpResponse response = client.execute(post); - System.out.println(JSONString); +// System.out.println(JSONString); if (response.getStatusLine().getStatusCode() == 400) { // EntityUtils to get the response content - - System.out.println("AppServer already exist, so changing the content database to new DB"); - associateRESTServerWithDB(restServerName,dbName); + System.out.println("AppServer already exist"); + if(dbName.equals("Documents")){ + System.out.println("and Context database is Documents DB"); + } + else{ + System.out.println("and changing context database to "+dbName); + associateRESTServerWithDB(restServerName,dbName); + } + } }catch (Exception e) { // writing error to Log @@ -288,8 +295,96 @@ public static void setupJavaRESTServer(String dbName, String fName, String restS createRESTUser("rest-writer","x","rest-writer"); createRESTUser("rest-reader","x","rest-reader"); } + public static void setupJavaRESTServer(String dbName, String fName, String restServerName, int restPort,boolean attachRestContextDB)throws Exception{ + + createDB(dbName); + createForest(fName,dbName); + Thread.sleep(1500); + if(attachRestContextDB){ + assocRESTServer(restServerName, dbName,restPort); + } + else{ + assocRESTServer(restServerName, "Documents",restPort); + } + createRESTUser("rest-admin","x","rest-admin"); + createRESTUser("rest-writer","x","rest-writer"); + createRESTUser("rest-reader","x","rest-reader"); + } + + /*Create a role with given privilages + * + */ + public static void createUserRolesWithPrevilages(String roleName, String... privNames ){ + try{ + DefaultHttpClient client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + HttpGet getrequest = new HttpGet("http://localhost:8002/manage/v2/roles/"+roleName); + HttpResponse resp = client.execute(getrequest); + + if( resp.getStatusLine().getStatusCode() == 200) + { + System.out.println("Role already exist"); + } + else { + System.out.println("Role dont exist, will create now"); + String[] roleNames ={"rest-reader","rest-writer"}; + client = new DefaultHttpClient(); + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + + ObjectMapper mapper = new ObjectMapper(); + ObjectNode mainNode = mapper.createObjectNode(); + + ArrayNode roleArray = mapper.createArrayNode(); + ArrayNode privArray = mapper.createArrayNode(); + ArrayNode permArray = mapper.createArrayNode(); + mainNode.put("name",roleName); + mainNode.put("description", "role discription"); + + for(String rolename: roleNames) + roleArray.add(rolename); + mainNode.withArray("role").addAll(roleArray); + for(String privName: privNames){ + ObjectNode privNode = mapper.createObjectNode(); + privNode.put("privilege-name", privName); + privNode.put("action", "http://marklogic.com/xdmp/privileges/"+privName.replace(":", "-")); + privNode.put("kind", "execute"); + privArray.add(privNode); + } + mainNode.withArray("privilege").addAll(privArray); + permArray.add(getPermissionNode(roleNames[0],Capability.READ).get("permission").get(0)); + permArray.add(getPermissionNode(roleNames[1],Capability.READ).get("permission").get(0)); + permArray.add(getPermissionNode(roleNames[1],Capability.EXECUTE).get("permission").get(0)); + permArray.add(getPermissionNode(roleNames[1],Capability.UPDATE).get("permission").get(0)); + mainNode.withArray("permission").addAll(permArray); + System.out.println(mainNode.toString()); + HttpPost post = new HttpPost("http://localhost:8002"+ "/manage/v2/roles?format=json"); + post.addHeader("Content-type", "application/json"); + post.setEntity(new StringEntity(mainNode.toString())); + + HttpResponse response = client.execute(post); + HttpEntity respEntity = response.getEntity(); + if( response.getStatusLine().getStatusCode() == 400) + { + System.out.println("creation of role got a problem"); + } + else if (respEntity != null) { + // EntityUtils to get the response content + String content = EntityUtils.toString(respEntity); + System.out.println(content); + } + else {System.out.println("No Proper Response");} + } + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } + } /* - * This function creates a REST server with default content DB, Module DB + * This function creates a REST user with given roles */ public static void createRESTUser(String usrName, String pass, String... roleNames ){ @@ -466,7 +561,26 @@ public static void deleteRESTUser(String usrName){ } } + public static void deleteUserRole(String roleName){ + try{ + DefaultHttpClient client = new DefaultHttpClient(); + + client.getCredentialsProvider().setCredentials( + new AuthScope("localhost", 8002), + new UsernamePasswordCredentials("admin", "admin")); + + HttpDelete delete = new HttpDelete("http://localhost:8002/manage/v2/roles/"+roleName); + HttpResponse response = client.execute(delete); + if(response.getStatusLine().getStatusCode()== 202){ + Thread.sleep(3500); + } + }catch (Exception e) { + // writing error to Log + e.printStackTrace(); + } + + } public static void setupJavaRESTServerWithDB( String restServerName, int restPort)throws Exception{ createRESTServerWithDB(restServerName, restPort); createRESTUser("rest-admin","x","rest-admin"); @@ -635,7 +749,7 @@ else if(response.getStatusLine().getStatusCode() == 200){ } } /* - * This function deletes rest server first and deletes forests and databases in separate calls + * This function move rest server first to documents and deletes forests and databases in separate calls */ public static void tearDownJavaRESTServer(String dbName, String [] fNames, String restServerName) throws Exception{ @@ -664,6 +778,7 @@ public static void tearDownJavaRESTServer(String dbName, String [] fNames, Strin deleteDB(dbName); } + /* * This function deletes rest server along with default forest and database diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestEvalXquery.java b/test-complete/src/test/java/com/marklogic/javaclient/TestEvalXquery.java index 700b5d549..10a56bb48 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestEvalXquery.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestEvalXquery.java @@ -54,18 +54,22 @@ public class TestEvalXquery extends BasicJavaClientREST { public static void setUpBeforeClass() throws Exception { System.out.println("In setup"); setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); -// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + TestEvalXquery.createUserRolesWithPrevilages("test-eval", "xdbc:eval","any-uri"); + TestEvalXquery.createRESTUser("eval-user", "x", "test-eval"); + System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); } @AfterClass public static void tearDownAfterClass() throws Exception { System.out.println("In tear down" ); tearDownJavaRESTServer(dbName, fNames, restServerName); + TestEvalXquery.deleteRESTUser("eval-user"); + TestEvalXquery.deleteUserRole("test-eval"); } @Before public void setUp() throws Exception { - client = DatabaseClientFactory.newClient("localhost", restPort,"admin", "admin", Authentication.DIGEST); + client = DatabaseClientFactory.newClient("localhost", restPort,"eval-user", "x", Authentication.DIGEST); } @After @@ -391,7 +395,7 @@ public void testMultipleXqueryfnOnServerEval() { String response3 = client.newServerEval().xquery(query3).evalAs(String.class); System.out.println(response3); } - //Issue 156 exist for this + //Issue 156 exist for this, have test cases where you can pass, element node, text node, binary node as an external variable @Test(expected = com.marklogic.client.FailedRequestException.class) public void testXqueryWithExtVarAsNode() throws Exception { DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); From f7e7482ff09aaf6eb52b660ddaa4e43647510304 Mon Sep 17 00:00:00 2001 From: sammefford Date: Mon, 27 Oct 2014 10:42:59 -0600 Subject: [PATCH 230/357] remove javadoc warnings from bad links annotations --- src/main/java/com/marklogic/client/Page.java | 2 +- .../marklogic/client/pojo/PojoQueryBuilder.java | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/marklogic/client/Page.java b/src/main/java/com/marklogic/client/Page.java index 76657b0fe..e7fb260e4 100644 --- a/src/main/java/com/marklogic/client/Page.java +++ b/src/main/java/com/marklogic/client/Page.java @@ -20,7 +20,7 @@ /** A generic interface for pagination through large sets of items of type <T>. */ public interface Page extends Iterable { /** The internal iterator of type T in this Page. This iterator is the same - * one used for {@link hasNext()} and {@link next()}. + * one used for {@link #hasNext()} and {@link #next()}. */ public Iterator iterator(); diff --git a/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java b/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java index 975d6b85b..44510d9f9 100644 --- a/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java +++ b/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java @@ -39,26 +39,26 @@ * *

Where StructuredQueryBuilder accepts StructuredQueryBuilder.TextIndex as a first argument * to - * {@link #value(StructuredQueryBuilder.TextIndex, String...) value(TextIndex, String...)} + * {@link StructuredQueryBuilder#value(StructuredQueryBuilder.TextIndex, String...) value(TextIndex, String...)} * and - * {@link #word(StructuredQueryBuilder.TextIndex, String...) word(TextIndex, String...)} + * {@link StructuredQueryBuilder#word(StructuredQueryBuilder.TextIndex, String...) word(TextIndex, String...)} * methods, * PojoQueryBuilder adds shortcut methods which accept as the first argument a String name of the * pojoProperty. Similarly, PojoQueryBuilder accepts String pojoProperty arguments wherever * StructuredQueryBuilder accepts StructuredQueryBuilder.Element, * StructuredQueryBuilder.Attribute, and StructuredQueryBuilder.PathIndex * as arguments to - * {@link #geoAttributePair(StructuredQueryBuilder.Element, StructuredQueryBuilder.Attribute, + * {@link StructuredQueryBuilder#geoAttributePair(StructuredQueryBuilder.Element, StructuredQueryBuilder.Attribute, * StructuredQueryBuilder.Attribute) * geoAttributePair(Element, Attribute, Attribute)}, - * {@link #geoElement(StructuredQueryBuilder.Element) + * {@link StructuredQueryBuilder#geoElement(StructuredQueryBuilder.Element) * geoElement(Element)}, - * {@link #geoElement(StructuredQueryBuilder.Element, StructuredQueryBuilder.Element) + * {@link StructuredQueryBuilder#geoElement(StructuredQueryBuilder.Element, StructuredQueryBuilder.Element) * geoElement(Element, Element)}, - * {@link #geoElementPair(StructuredQueryBuilder.Element, StructuredQueryBuilder.Element, + * {@link StructuredQueryBuilder#geoElementPair(StructuredQueryBuilder.Element, StructuredQueryBuilder.Element, * StructuredQueryBuilder.Element) * geoElementPair(Element, Element, Element)}, - * {@link #geoPath(StructuredQueryBuilder.PathIndex) + * {@link StructuredQueryBuilder#geoPath(StructuredQueryBuilder.PathIndex) * geoPath(PathIndex)} *

* @@ -112,7 +112,7 @@ */ public interface PojoQueryBuilder { - /** Copied directly from {@link StructuredQueryBuilder#Operator StructuredQuerybuilder.Operator}**/ + /** Copied directly from {@link com.marklogic.client.query.StructuredQueryBuilder.Operator}**/ public enum Operator { LT, LE, GT, GE, EQ, NE; } From b9523dcb54979316a6bb9a365d3bddfa4bc3f547 Mon Sep 17 00:00:00 2001 From: sammefford Date: Mon, 27 Oct 2014 10:54:49 -0600 Subject: [PATCH 231/357] fix #96, add PojoQueryDefinition marker interface for more clarity on which QueryDefinition types Pojo Facade can accept --- .../client/impl/CombinedQueryDefinition.java | 3 ++- .../client/impl/PojoQueryBuilderImpl.java | 20 +++++++++---------- .../client/impl/PojoRepositoryImpl.java | 18 ++++++++--------- .../client/pojo/PojoQueryBuilder.java | 2 +- .../marklogic/client/pojo/PojoRepository.java | 11 +++++----- .../client/query/StringQueryDefinition.java | 5 +++-- .../query/StructuredQueryDefinition.java | 19 +++++++++--------- .../marklogic/client/test/PojoFacadeTest.java | 6 +++--- 8 files changed, 42 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/marklogic/client/impl/CombinedQueryDefinition.java b/src/main/java/com/marklogic/client/impl/CombinedQueryDefinition.java index 728767135..631b91094 100644 --- a/src/main/java/com/marklogic/client/impl/CombinedQueryDefinition.java +++ b/src/main/java/com/marklogic/client/impl/CombinedQueryDefinition.java @@ -17,9 +17,10 @@ import com.marklogic.client.query.QueryDefinition; import com.marklogic.client.query.ValueQueryDefinition; +import com.marklogic.client.pojo.PojoQueryDefinition; public interface CombinedQueryDefinition - extends QueryDefinition, ValueQueryDefinition + extends QueryDefinition, ValueQueryDefinition, PojoQueryDefinition { /** * Returns the combined query definition as a serialized XML string. diff --git a/src/main/java/com/marklogic/client/impl/PojoQueryBuilderImpl.java b/src/main/java/com/marklogic/client/impl/PojoQueryBuilderImpl.java index 3cc7b91ae..ca8c96160 100644 --- a/src/main/java/com/marklogic/client/impl/PojoQueryBuilderImpl.java +++ b/src/main/java/com/marklogic/client/impl/PojoQueryBuilderImpl.java @@ -15,24 +15,22 @@ */ package com.marklogic.client.impl; -import com.marklogic.client.io.Format; -import com.marklogic.client.io.StringHandle; -import com.marklogic.client.pojo.PojoQueryBuilder; -import com.marklogic.client.query.QueryDefinition; -import com.marklogic.client.query.StructuredQueryBuilder; -import com.marklogic.client.query.StructuredQueryBuilder.TermQuery; -import com.marklogic.client.query.StructuredQueryBuilder.TextIndex; -import com.marklogic.client.query.StructuredQueryDefinition; - import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.Date; import java.util.HashMap; import javax.xml.stream.XMLStreamWriter; + +import com.marklogic.client.io.Format; +import com.marklogic.client.io.StringHandle; +import com.marklogic.client.pojo.PojoQueryBuilder; +import com.marklogic.client.pojo.PojoQueryDefinition; +import com.marklogic.client.query.StructuredQueryBuilder; +import com.marklogic.client.query.StructuredQueryDefinition; public class PojoQueryBuilderImpl extends StructuredQueryBuilder implements PojoQueryBuilder { - private HashMap types = new HashMap(); + private HashMap> types = new HashMap>(); private HashMap rangeIndextypes = new HashMap(); private Class clazz; private String classWrapper; @@ -212,7 +210,7 @@ public void innerSerialize(XMLStreamWriter serializer) throws Exception { } } - public QueryDefinition filteredQuery(StructuredQueryDefinition query) { + public PojoQueryDefinition filteredQuery(StructuredQueryDefinition query) { CombinedQueryBuilder cqb = new CombinedQueryBuilderImpl(); StringHandle options = new StringHandle( "filtered") diff --git a/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java b/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java index 2b0450576..d441f0d04 100644 --- a/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java +++ b/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java @@ -30,8 +30,8 @@ import com.marklogic.client.pojo.PojoQueryBuilder; import com.marklogic.client.pojo.PojoRepository; import com.marklogic.client.pojo.annotation.Id; +import com.marklogic.client.pojo.PojoQueryDefinition; import com.marklogic.client.query.DeleteQueryDefinition; -import com.marklogic.client.query.QueryDefinition; import com.marklogic.client.query.QueryManager; import com.marklogic.client.query.QueryManager.QueryView; import com.marklogic.client.query.StructuredQueryDefinition; @@ -114,7 +114,7 @@ public boolean exists(ID id) { } public long count() { - return count((QueryDefinition) null); + return count((PojoQueryDefinition) null); } public long count(String... collections) { @@ -123,9 +123,9 @@ public long count(String... collections) { return count(qb.collection(collections)); } } - return count((QueryDefinition) null); + return count((PojoQueryDefinition) null); } - public long count(QueryDefinition query) { + public long count(PojoQueryDefinition query) { long pageLength = getPageLength(); setPageLength(0); PojoPage page = search(query, 1); @@ -195,16 +195,16 @@ public PojoPage search(long start, Transaction transaction, String... collect return search(qb.collection(collections), start, null, transaction); } - public PojoPage search(QueryDefinition query, long start) { + public PojoPage search(PojoQueryDefinition query, long start) { return search(query, start, null, null); } - public PojoPage search(QueryDefinition query, long start, Transaction transaction) { + public PojoPage search(PojoQueryDefinition query, long start, Transaction transaction) { return search(query, start, null, transaction); } - public PojoPage search(QueryDefinition query, long start, SearchReadHandle searchHandle) { + public PojoPage search(PojoQueryDefinition query, long start, SearchReadHandle searchHandle) { return search(query, start, searchHandle, null); } - public PojoPage search(QueryDefinition query, long start, SearchReadHandle searchHandle, Transaction transaction) { + public PojoPage search(PojoQueryDefinition query, long start, SearchReadHandle searchHandle, Transaction transaction) { if ( searchHandle != null ) { HandleImplementation searchBase = HandleAccessor.checkHandle(searchHandle, "search"); if (searchHandle instanceof SearchHandle) { @@ -248,7 +248,7 @@ public DatabaseClient getDatabaseClient() { return client; } - private QueryDefinition wrapQuery(QueryDefinition query) { + private PojoQueryDefinition wrapQuery(PojoQueryDefinition query) { if ( query == null ) { return qb.collection(entityClass.getName()); } else { diff --git a/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java b/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java index 44510d9f9..9dd054b03 100644 --- a/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java +++ b/src/main/java/com/marklogic/client/pojo/PojoQueryBuilder.java @@ -201,7 +201,7 @@ public StructuredQueryDefinition word(String pojoProperty, String[] options, * @return a QueryDefinition that can be used with PojoRepository.search() * (a REST combined query under the hood) */ - public QueryDefinition filteredQuery(StructuredQueryDefinition query); + public PojoQueryDefinition filteredQuery(StructuredQueryDefinition query); } diff --git a/src/main/java/com/marklogic/client/pojo/PojoRepository.java b/src/main/java/com/marklogic/client/pojo/PojoRepository.java index 477911e84..ee749be0a 100644 --- a/src/main/java/com/marklogic/client/pojo/PojoRepository.java +++ b/src/main/java/com/marklogic/client/pojo/PojoRepository.java @@ -18,7 +18,6 @@ import com.marklogic.client.DatabaseClient; import com.marklogic.client.Transaction; import com.marklogic.client.io.marker.SearchReadHandle; -import com.marklogic.client.query.QueryDefinition; import java.io.Serializable; @@ -40,7 +39,7 @@ public interface PojoRepository { /** @return the number of documents of type T persisted in the database which match * the query */ - public long count(QueryDefinition query); + public long count(PojoQueryDefinition query); /** Deletes from the database the documents with the corresponding ids */ public void delete(ID... ids); @@ -61,10 +60,10 @@ public interface PojoRepository { public PojoPage search(long start, String... collections); public PojoPage search(long start, Transaction transaction, String... collections); - public PojoPage search(QueryDefinition query, long start); - public PojoPage search(QueryDefinition query, long start, Transaction transaction); - public PojoPage search(QueryDefinition query, long start, SearchReadHandle searchHandle); - public PojoPage search(QueryDefinition query, long start, SearchReadHandle searchHandle, Transaction transaction); + public PojoPage search(PojoQueryDefinition query, long start); + public PojoPage search(PojoQueryDefinition query, long start, Transaction transaction); + public PojoPage search(PojoQueryDefinition query, long start, SearchReadHandle searchHandle); + public PojoPage search(PojoQueryDefinition query, long start, SearchReadHandle searchHandle, Transaction transaction); public PojoQueryBuilder getQueryBuilder(); diff --git a/src/main/java/com/marklogic/client/query/StringQueryDefinition.java b/src/main/java/com/marklogic/client/query/StringQueryDefinition.java index 66d218e37..16c804923 100644 --- a/src/main/java/com/marklogic/client/query/StringQueryDefinition.java +++ b/src/main/java/com/marklogic/client/query/StringQueryDefinition.java @@ -15,12 +15,13 @@ */ package com.marklogic.client.query; +import com.marklogic.client.pojo.PojoQueryDefinition; /** * A StringQueryDefinition represents the criteria associated with a simple string query. */ public interface StringQueryDefinition - extends QueryDefinition, ValueQueryDefinition + extends QueryDefinition, ValueQueryDefinition, PojoQueryDefinition { /** * Returns the query criteria, that is the query string. @@ -36,7 +37,7 @@ public interface StringQueryDefinition /** * Sets the query criteria as a query string and returns the query - * definition as a fluent convenience. + * definition as a fluent convenience. * @param criteria The query string. * @return This query definition. */ diff --git a/src/main/java/com/marklogic/client/query/StructuredQueryDefinition.java b/src/main/java/com/marklogic/client/query/StructuredQueryDefinition.java index 88a09cbbe..4b0132b32 100644 --- a/src/main/java/com/marklogic/client/query/StructuredQueryDefinition.java +++ b/src/main/java/com/marklogic/client/query/StructuredQueryDefinition.java @@ -15,20 +15,21 @@ */ package com.marklogic.client.query; - +import com.marklogic.client.pojo.PojoQueryDefinition; /** * A StructuredQueryDefinition represents a structured query. * * Instances of this interface are produced by StructuredQueryBuilder. */ -public interface StructuredQueryDefinition extends QueryDefinition, - ValueQueryDefinition { - /** - * Returns the structured query definition as a serialized XML string. - * - * @return The serialized definition. - */ - public String serialize(); +public interface StructuredQueryDefinition + extends QueryDefinition, ValueQueryDefinition, PojoQueryDefinition +{ + /** + * Returns the structured query definition as a serialized XML string. + * + * @return The serialized definition. + */ + public String serialize(); } diff --git a/src/test/java/com/marklogic/client/test/PojoFacadeTest.java b/src/test/java/com/marklogic/client/test/PojoFacadeTest.java index cc6aa5457..703c396dc 100644 --- a/src/test/java/com/marklogic/client/test/PojoFacadeTest.java +++ b/src/test/java/com/marklogic/client/test/PojoFacadeTest.java @@ -32,7 +32,7 @@ import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import com.marklogic.client.pojo.PojoPage; import com.marklogic.client.pojo.PojoRepository; -import com.marklogic.client.query.QueryDefinition; +import com.marklogic.client.pojo.PojoQueryDefinition; import com.marklogic.client.query.StringQueryDefinition; import com.marklogic.client.query.StructuredQueryBuilder; import com.marklogic.client.pojo.PojoQueryBuilder.Operator; @@ -111,7 +111,7 @@ public void testC_QueryPojos() throws Exception { PojoQueryBuilder qb = cities.getQueryBuilder(); - QueryDefinition query = qb.term("Tungi", "Dalatando", "Chittagong"); + PojoQueryDefinition query = qb.term("Tungi", "Dalatando", "Chittagong"); page = cities.search(query, 1); iterator = page.iterator(); numRead = 0; @@ -309,7 +309,7 @@ public void testD_PojosWithChildren() throws Exception { PojoQueryBuilder qb = cities.getQueryBuilder(); PojoQueryBuilder countriesQb = qb.containerQueryBuilder("country", Country.class); - QueryDefinition query = countriesQb.value("continent", "EU"); + PojoQueryDefinition query = countriesQb.value("continent", "EU"); assertEquals("Should not find any countries", 0, cities.search(query, 1).getTotalSize()); query = countriesQb.value("continent", "AS"); From 071448b2be25ca7b0ec683e46c17157f0dd71b53 Mon Sep 17 00:00:00 2001 From: sammefford Date: Mon, 27 Oct 2014 10:55:04 -0600 Subject: [PATCH 232/357] fix #96, add PojoQueryDefinition marker interface for more clarity on which QueryDefinition types Pojo Facade can accept --- .../client/pojo/PojoQueryDefinition.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/main/java/com/marklogic/client/pojo/PojoQueryDefinition.java diff --git a/src/main/java/com/marklogic/client/pojo/PojoQueryDefinition.java b/src/main/java/com/marklogic/client/pojo/PojoQueryDefinition.java new file mode 100644 index 000000000..8631b8bc6 --- /dev/null +++ b/src/main/java/com/marklogic/client/pojo/PojoQueryDefinition.java @@ -0,0 +1,28 @@ +/* + * Copyright 2012-2014 MarkLogic Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.marklogic.client.pojo; + +import com.marklogic.client.query.QueryDefinition; + +/** + * A marker interface identifying QueryDefinition types compatible with + * {@link PojoRepository#search(PojoQueryDefinition, long) PojoRepository.search} + * @see PojoRepository#search(PojoQueryDefinition, long) + * @see PojoRepository#search(PojoQueryDefinition, long, Transaction) + * @see PojoRepository#search(PojoQueryDefinition, long, SearchReadHandle) + * @see PojoRepository#search(PojoQueryDefinition, long, SearchReadHandle, Transaction) + */ +public interface PojoQueryDefinition extends QueryDefinition {} From b6541a55ddbb2e6d14c0a2bc0409608a9a43110e Mon Sep 17 00:00:00 2001 From: sammefford Date: Mon, 27 Oct 2014 13:31:32 -0600 Subject: [PATCH 233/357] fix #97, deprecate KeyValueDefinition and related KeyLocator, ElementLocator, and ValueLocator interfaces --- .../example/cookbook/AllCookbookExamples.java | 1 - .../example/cookbook/KeyValueSearch.java | 128 ------------------ .../client/query/ElementLocator.java | 4 + .../marklogic/client/query/KeyLocator.java | 4 + .../client/query/KeyValueQueryDefinition.java | 20 ++- .../marklogic/client/query/QueryManager.java | 20 +++ .../query/RawQueryByExampleDefinition.java | 17 ++- .../marklogic/client/query/ValueLocator.java | 4 + .../example/cookbook/KeyValueSearchTest.java | 38 ------ 9 files changed, 67 insertions(+), 169 deletions(-) delete mode 100644 src/main/java/com/marklogic/client/example/cookbook/KeyValueSearch.java delete mode 100644 src/test/java/com/marklogic/client/test/example/cookbook/KeyValueSearchTest.java diff --git a/src/main/java/com/marklogic/client/example/cookbook/AllCookbookExamples.java b/src/main/java/com/marklogic/client/example/cookbook/AllCookbookExamples.java index 1a690d054..90db2d1aa 100644 --- a/src/main/java/com/marklogic/client/example/cookbook/AllCookbookExamples.java +++ b/src/main/java/com/marklogic/client/example/cookbook/AllCookbookExamples.java @@ -47,7 +47,6 @@ public static void main(String[] args) DocumentFormats.run( props ); DocumentOutputStream.run( props ); JAXBDocument.run( props ); - KeyValueSearch.run( props ); QueryOptions.run( props ); StringSearch.run( props ); StructuredSearch.run( props ); diff --git a/src/main/java/com/marklogic/client/example/cookbook/KeyValueSearch.java b/src/main/java/com/marklogic/client/example/cookbook/KeyValueSearch.java deleted file mode 100644 index 5362ef0f7..000000000 --- a/src/main/java/com/marklogic/client/example/cookbook/KeyValueSearch.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Copyright 2012-2014 MarkLogic Corporation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.marklogic.client.example.cookbook; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; - -import javax.xml.namespace.QName; - -import com.marklogic.client.DatabaseClient; -import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.example.cookbook.Util.ExampleProperties; -import com.marklogic.client.io.InputStreamHandle; -import com.marklogic.client.io.SearchHandle; -import com.marklogic.client.query.KeyValueQueryDefinition; -import com.marklogic.client.query.MatchDocumentSummary; -import com.marklogic.client.query.MatchLocation; -import com.marklogic.client.query.MatchSnippet; -import com.marklogic.client.query.QueryManager; - -/** - * KeyValueSearch illustrates searching for documents and iterating over results - * with simple pairs of element names and values. - */ -public class KeyValueSearch { - static final private String[] filenames = {"curbappeal.xml", "flipper.xml", "justintime.xml"}; - - public static void main(String[] args) throws IOException { - run(Util.loadProperties()); - } - - public static void run(ExampleProperties props) throws IOException { - System.out.println("example: "+KeyValueSearch.class.getName()); - - // create the client - DatabaseClient client = DatabaseClientFactory.newClient( - props.host, props.port, props.writerUser, props.writerPassword, - props.authType); - - setUpExample(client); - - // create a manager for searching - QueryManager queryMgr = client.newQueryManager(); - - // create a search definition - KeyValueQueryDefinition querydef = queryMgr.newKeyValueDefinition(); - querydef.put(queryMgr.newElementLocator(new QName("industry")), "Real Estate"); - - // create a handle for the search results - SearchHandle resultsHandle = new SearchHandle(); - - // run the search - queryMgr.search(querydef, resultsHandle); - - System.out.println("Matched "+resultsHandle.getTotalResults()+ - " documents with 'industry' value of 'Real Estate'\n"); - - // iterate over the result documents - MatchDocumentSummary[] docSummaries = resultsHandle.getMatchResults(); - System.out.println("Listing "+docSummaries.length+" documents:\n"); - for (MatchDocumentSummary docSummary: docSummaries) { - - // iterate over the match locations within a result document - MatchLocation[] locations = docSummary.getMatchLocations(); - System.out.println("Matched "+locations.length+" locations in "+docSummary.getUri()+":"); - for (MatchLocation location: locations) { - - // iterate over the snippets at a match location - for (MatchSnippet snippet : location.getSnippets()) { - boolean isHighlighted = snippet.isHighlighted(); - - if (isHighlighted) - System.out.print("["); - System.out.print(snippet.getText()); - if (isHighlighted) - System.out.print("]"); - } - System.out.println(); - } - } - - tearDownExample(client); - - // release the client - client.release(); - } - - // set up by writing the document content and options used in the example query - public static void setUpExample(DatabaseClient client) throws IOException { - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - - InputStreamHandle contentHandle = new InputStreamHandle(); - - for (String filename: filenames) { - InputStream docStream = Util.openStream("data"+File.separator+filename); - if (docStream == null) - throw new IOException("Could not read document example"); - - contentHandle.set(docStream); - - docMgr.write("/example/"+filename, contentHandle); - } - } - - // clean up by deleting the documents and options used in the example query - public static void tearDownExample(DatabaseClient client) { - XMLDocumentManager docMgr = client.newXMLDocumentManager(); - - for (String filename: filenames) { - docMgr.delete("/example/"+filename); - } - } -} diff --git a/src/main/java/com/marklogic/client/query/ElementLocator.java b/src/main/java/com/marklogic/client/query/ElementLocator.java index 0997c786b..b82cfa805 100644 --- a/src/main/java/com/marklogic/client/query/ElementLocator.java +++ b/src/main/java/com/marklogic/client/query/ElementLocator.java @@ -18,10 +18,14 @@ import javax.xml.namespace.QName; /** + * @deprecated Use {@link RawQueryByExampleDefinition Query By Example} instead for easy-to-write and much more full-featured key/value search. + *

+ * * An Element Locator specifies an element or element and attribute * containing a value as part of a KeyValueQueryDefinition. * The element is required. The attribute is optional. */ +@Deprecated public interface ElementLocator extends ValueLocator { /** * Returns the name of the element containing the attribute diff --git a/src/main/java/com/marklogic/client/query/KeyLocator.java b/src/main/java/com/marklogic/client/query/KeyLocator.java index 595aaf232..69c6659b3 100644 --- a/src/main/java/com/marklogic/client/query/KeyLocator.java +++ b/src/main/java/com/marklogic/client/query/KeyLocator.java @@ -16,9 +16,13 @@ package com.marklogic.client.query; /** + * @deprecated Use {@link RawQueryByExampleDefinition Query By Example} instead for easy-to-write and much more full-featured key/value search. + *

+ * * A Key Locator specifies a JSON key containing a value as part * of a KeyValueQueryDefinition. */ +@Deprecated public interface KeyLocator extends ValueLocator { /** * Returns a JSON key. diff --git a/src/main/java/com/marklogic/client/query/KeyValueQueryDefinition.java b/src/main/java/com/marklogic/client/query/KeyValueQueryDefinition.java index 5104d12e5..db51e4cb7 100644 --- a/src/main/java/com/marklogic/client/query/KeyValueQueryDefinition.java +++ b/src/main/java/com/marklogic/client/query/KeyValueQueryDefinition.java @@ -18,7 +18,25 @@ import java.util.Map; /** - * A KeyValueQueryDefinition is a query definition suitable for use in performing a key/value query. + * @deprecated Use Query By Example instead for easy-to-write and much more full-featured key/value search. + * + * For instance: + *
{@code
+ *QueryManager queryMgr = databaseClient.newQueryManager();
+ *String rawXMLQuery = "{ \"$query\": { \"author\": \"Mark Twain\" } }";
+ *StringHandle qbeHandle = new StringHandle(rawXMLQuery).withFormat(Format.JSON);
+ *RawQueryByExampleDefinition query = queryMgr.newRawQueryByExampleDefinition(qbeHandle, "myoptions");
+ *SearchHandle resultsHandle = queryMgr.search(query, new SearchHandle());
+ *}
+ * + * @see QueryManager#newRawCombinedQueryDefinitionAs(Format, Object) + * @see QueryManager#newRawCombinedQueryDefinitionAs(Format, Object, String) + * @see QueryManager#newRawQueryByExampleDefinition(StructureWriteHandle) + * @see QueryManager#newRawQueryByExampleDefinition(StructureWriteHandle, String) + * @see MarkLogic Java Application Developer's Guide + * > Searching + * > Prototype a Query Using Query By Example */ +@Deprecated public interface KeyValueQueryDefinition extends QueryDefinition, Map { } diff --git a/src/main/java/com/marklogic/client/query/QueryManager.java b/src/main/java/com/marklogic/client/query/QueryManager.java index 64e832408..977cb2452 100644 --- a/src/main/java/com/marklogic/client/query/QueryManager.java +++ b/src/main/java/com/marklogic/client/query/QueryManager.java @@ -116,19 +116,27 @@ public enum QueryView { public StringQueryDefinition newStringDefinition(String optionsName); /** + * @deprecated Use {@link RawQueryByExampleDefinition Query By Example} instead for easy-to-write and much more full-featured key/value search. + *

+ * * Creates a query definition based on a locator such as a JSON key, * element name, or element and attribute name and the default query * options. * @return the key-value query definition */ + @Deprecated public KeyValueQueryDefinition newKeyValueDefinition(); /** + * @deprecated Use {@link RawQueryByExampleDefinition Query By Example} instead for easy-to-write and much more full-featured key/value search. + *

+ * * Creates a query definition based on a locator and on named * query options saved previously. * @param optionsName the name of the query options * @return the key-value query definition */ + @Deprecated public KeyValueQueryDefinition newKeyValueDefinition(String optionsName); /** @@ -208,27 +216,39 @@ public enum QueryView { public ValuesListDefinition newValuesListDefinition(String optionsName); /** + * @deprecated Use {@link RawQueryByExampleDefinition Query By Example} instead for easy-to-write and much more full-featured key/value search. + *

+ * * Creates a locator for a key-value query based on an element name, * which may have namespace. * @param element the element name * @return the locator for a key-value query */ + @Deprecated public ElementLocator newElementLocator(QName element); /** + * @deprecated Use {@link RawQueryByExampleDefinition Query By Example} instead for easy-to-write and much more full-featured key/value search. + *

+ * * Creates a locator for a key-value query based on an element name * and attribute name, either or both of which may have a namespace. * @param element the element name * @param attribute the attribute name * @return the locator for a key-value query */ + @Deprecated public ElementLocator newElementLocator(QName element, QName attribute); /** + * @deprecated Use {@link RawQueryByExampleDefinition Query By Example} instead for easy-to-write and much more full-featured key/value search. + *

+ * * Creates a locator for a key-value query based on a JSON key. * @param key the JSON key * @return the locator for a key-value query */ + @Deprecated public KeyLocator newKeyLocator(String key); /** diff --git a/src/main/java/com/marklogic/client/query/RawQueryByExampleDefinition.java b/src/main/java/com/marklogic/client/query/RawQueryByExampleDefinition.java index 84cf2525d..1198a2d1a 100644 --- a/src/main/java/com/marklogic/client/query/RawQueryByExampleDefinition.java +++ b/src/main/java/com/marklogic/client/query/RawQueryByExampleDefinition.java @@ -19,7 +19,22 @@ /** * A RawQueryByExampleDefinition provides access to a simple - * query by example in an JSON or XML representation. + * query by example in an JSON or XML representation. For instance: + *
{@code
+ *QueryManager queryMgr = databaseClient.newQueryManager();
+ *String rawXMLQuery = "{ \"$query\": { \"author\": \"Mark Twain\" } }";
+ *StringHandle qbeHandle = new StringHandle(rawXMLQuery).withFormat(Format.JSON);
+ *RawQueryByExampleDefinition query = queryMgr.newRawQueryByExampleDefinition(qbeHandle, "myoptions");
+ *SearchHandle resultsHandle = queryMgr.search(query, new SearchHandle());
+ *}
+ * + * @see QueryManager#newRawCombinedQueryDefinitionAs(Format, Object) + * @see QueryManager#newRawCombinedQueryDefinitionAs(Format, Object, String) + * @see QueryManager#newRawQueryByExampleDefinition(StructureWriteHandle) + * @see QueryManager#newRawQueryByExampleDefinition(StructureWriteHandle, String) + * @see MarkLogic Java Application Developer's Guide + * > Searching + * > Prototype a Query Using Query By Example */ public interface RawQueryByExampleDefinition extends RawQueryDefinition { /** diff --git a/src/main/java/com/marklogic/client/query/ValueLocator.java b/src/main/java/com/marklogic/client/query/ValueLocator.java index 1f950f886..66cb2b36a 100644 --- a/src/main/java/com/marklogic/client/query/ValueLocator.java +++ b/src/main/java/com/marklogic/client/query/ValueLocator.java @@ -16,8 +16,12 @@ package com.marklogic.client.query; /** + * @deprecated Use {@link RawQueryByExampleDefinition Query By Example} instead for easy-to-write and much more full-featured key/value search. + *

+ * * A Locator identifying a JSON key, element, or element and attribute * containing a value as part of a KeyValueQueryDefinition. */ +@Deprecated public interface ValueLocator { } diff --git a/src/test/java/com/marklogic/client/test/example/cookbook/KeyValueSearchTest.java b/src/test/java/com/marklogic/client/test/example/cookbook/KeyValueSearchTest.java deleted file mode 100644 index 7a2042a37..000000000 --- a/src/test/java/com/marklogic/client/test/example/cookbook/KeyValueSearchTest.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2012-2014 MarkLogic Corporation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.marklogic.client.test.example.cookbook; - -import static org.junit.Assert.assertTrue; - -import java.io.IOException; - -import org.junit.Test; - -import com.marklogic.client.example.cookbook.KeyValueSearch; - -public class KeyValueSearchTest { - @Test - public void testMain() { - boolean succeeded = false; - try { - KeyValueSearch.main(new String[0]); - succeeded = true; - } catch (IOException e) { - e.printStackTrace(); - } - assertTrue("KeyValueSearch example failed", succeeded); - } -} From 042b8804c05af3f0f6641275ed70298876f4ab5a Mon Sep 17 00:00:00 2001 From: sammefford Date: Mon, 27 Oct 2014 13:58:09 -0600 Subject: [PATCH 234/357] it appears port 8000 can't yet find all modules: https://bugtrack.marklogic.com/28663 --- src/main/resources/Example.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/Example.properties b/src/main/resources/Example.properties index 3160ec3b1..c519792d4 100644 --- a/src/main/resources/Example.properties +++ b/src/main/resources/Example.properties @@ -6,5 +6,5 @@ example.writer_password=x example.admin_user=rest-admin example.admin_password=x example.host=localhost -example.port=8000 +example.port=8012 example.authentication_type=digest From f887eaf1d47e4c2b8599a32a91f942afedcb383d Mon Sep 17 00:00:00 2001 From: sammefford Date: Mon, 27 Oct 2014 14:17:38 -0600 Subject: [PATCH 235/357] fix #98, move marker interfaces to public Jackson*Handle classes, not JacksonBaseHandle --- .../client/impl/JacksonBaseHandle.java | 4 --- .../client/io/JacksonDatabindHandle.java | 26 ++++++++++++++----- .../marklogic/client/io/JacksonHandle.java | 18 ++++++++++--- .../client/io/JacksonParserHandle.java | 23 ++++++++-------- 4 files changed, 46 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/marklogic/client/impl/JacksonBaseHandle.java b/src/main/java/com/marklogic/client/impl/JacksonBaseHandle.java index 0f0fc50f9..f68b91356 100644 --- a/src/main/java/com/marklogic/client/impl/JacksonBaseHandle.java +++ b/src/main/java/com/marklogic/client/impl/JacksonBaseHandle.java @@ -46,10 +46,6 @@ public abstract class JacksonBaseHandle extends BaseHandle - implements OutputStreamSender, BufferableHandle, JSONReadHandle, JSONWriteHandle, - TextReadHandle, TextWriteHandle, - XMLReadHandle, XMLWriteHandle, - StructureReadHandle, StructureWriteHandle { private ObjectMapper mapper; diff --git a/src/main/java/com/marklogic/client/io/JacksonDatabindHandle.java b/src/main/java/com/marklogic/client/io/JacksonDatabindHandle.java index e245c2ae7..cbb2906ad 100644 --- a/src/main/java/com/marklogic/client/io/JacksonDatabindHandle.java +++ b/src/main/java/com/marklogic/client/io/JacksonDatabindHandle.java @@ -15,7 +15,6 @@ */ package com.marklogic.client.io; -import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -27,11 +26,19 @@ import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; -import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.marklogic.client.MarkLogicIOException; +import com.marklogic.client.io.marker.BufferableHandle; import com.marklogic.client.io.marker.ContentHandle; import com.marklogic.client.io.marker.ContentHandleFactory; +import com.marklogic.client.io.marker.JSONReadHandle; +import com.marklogic.client.io.marker.JSONWriteHandle; +import com.marklogic.client.io.marker.StructureReadHandle; +import com.marklogic.client.io.marker.StructureWriteHandle; +import com.marklogic.client.io.marker.TextReadHandle; +import com.marklogic.client.io.marker.TextWriteHandle; +import com.marklogic.client.io.marker.XMLReadHandle; +import com.marklogic.client.io.marker.XMLWriteHandle; import com.marklogic.client.impl.JacksonBaseHandle; /** @@ -41,9 +48,14 @@ */ public class JacksonDatabindHandle extends JacksonBaseHandle - implements ContentHandle + implements ContentHandle, + OutputStreamSender, BufferableHandle, + JSONReadHandle, JSONWriteHandle, + TextReadHandle, TextWriteHandle, + XMLReadHandle, XMLWriteHandle, + StructureReadHandle, StructureWriteHandle { - private Class contentClass; + private Class contentClass; private T content; /** @@ -82,7 +94,7 @@ public JacksonDatabindHandle(Class contentClass) { * Provides a handle on JSON content as a Jackson tree. * @param content the JSON root node of the tree. */ - public JacksonDatabindHandle(T content) { + public JacksonDatabindHandle(T content) { this((Class) content.getClass()); set(content); } @@ -93,7 +105,7 @@ public JacksonDatabindHandle(T content) { * @param format the format of the content * @return this handle */ - public JacksonDatabindHandle withFormat(Format format) { + public JacksonDatabindHandle withFormat(Format format) { setFormat(format); return this; } @@ -182,7 +194,7 @@ public boolean isHandled(Class type) { @Override public ContentHandle newHandle(Class type) { if ( ! isHandled(type) ) return null; - JacksonDatabindHandle handle = new JacksonDatabindHandle(type); + JacksonDatabindHandle handle = new JacksonDatabindHandle(type); if ( mapper != null ) handle.setMapper(mapper); return handle; } diff --git a/src/main/java/com/marklogic/client/io/JacksonHandle.java b/src/main/java/com/marklogic/client/io/JacksonHandle.java index 502ff7c34..e1f57b41f 100644 --- a/src/main/java/com/marklogic/client/io/JacksonHandle.java +++ b/src/main/java/com/marklogic/client/io/JacksonHandle.java @@ -15,7 +15,6 @@ */ package com.marklogic.client.io; -import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -25,10 +24,18 @@ import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonNode; - import com.marklogic.client.MarkLogicIOException; +import com.marklogic.client.io.marker.BufferableHandle; import com.marklogic.client.io.marker.ContentHandle; import com.marklogic.client.io.marker.ContentHandleFactory; +import com.marklogic.client.io.marker.JSONReadHandle; +import com.marklogic.client.io.marker.JSONWriteHandle; +import com.marklogic.client.io.marker.StructureReadHandle; +import com.marklogic.client.io.marker.StructureWriteHandle; +import com.marklogic.client.io.marker.TextReadHandle; +import com.marklogic.client.io.marker.TextWriteHandle; +import com.marklogic.client.io.marker.XMLReadHandle; +import com.marklogic.client.io.marker.XMLWriteHandle; import com.marklogic.client.impl.JacksonBaseHandle; /** @@ -38,7 +45,12 @@ */ public class JacksonHandle extends JacksonBaseHandle - implements ContentHandle + implements ContentHandle, + OutputStreamSender, BufferableHandle, + JSONReadHandle, JSONWriteHandle, + TextReadHandle, TextWriteHandle, + XMLReadHandle, XMLWriteHandle, + StructureReadHandle, StructureWriteHandle { private JsonNode content; diff --git a/src/main/java/com/marklogic/client/io/JacksonParserHandle.java b/src/main/java/com/marklogic/client/io/JacksonParserHandle.java index 28e0a6e50..84901dba5 100644 --- a/src/main/java/com/marklogic/client/io/JacksonParserHandle.java +++ b/src/main/java/com/marklogic/client/io/JacksonParserHandle.java @@ -15,29 +15,26 @@ */ package com.marklogic.client.io; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.io.OutputStream; -import java.io.OutputStreamWriter; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.databind.ObjectMapper; - import com.marklogic.client.MarkLogicIOException; +import com.marklogic.client.impl.JacksonBaseHandle; +import com.marklogic.client.io.marker.BufferableHandle; import com.marklogic.client.io.marker.ContentHandle; import com.marklogic.client.io.marker.ContentHandleFactory; -import com.marklogic.client.io.marker.GenericReadHandle; import com.marklogic.client.io.marker.JSONReadHandle; -import com.marklogic.client.io.marker.OperationNotSupported; +import com.marklogic.client.io.marker.JSONWriteHandle; import com.marklogic.client.io.marker.StructureReadHandle; +import com.marklogic.client.io.marker.StructureWriteHandle; import com.marklogic.client.io.marker.TextReadHandle; +import com.marklogic.client.io.marker.TextWriteHandle; import com.marklogic.client.io.marker.XMLReadHandle; -import com.marklogic.client.impl.JacksonBaseHandle; +import com.marklogic.client.io.marker.XMLWriteHandle; /** * An adapter for using the streaming capabilities of the Jackson Open Source library. @@ -46,9 +43,13 @@ // TODO: add link to jackson streaming documentation public class JacksonParserHandle extends JacksonBaseHandle - implements ContentHandle + implements ContentHandle, + OutputStreamSender, BufferableHandle, + JSONReadHandle, JSONWriteHandle, + TextReadHandle, TextWriteHandle, + XMLReadHandle, XMLWriteHandle, + StructureReadHandle, StructureWriteHandle { - private ObjectMapper mapper; private JsonParser parser = null; private InputStream content = null; From d596df959efa089b201dfe89c2f0f882d09fcc57 Mon Sep 17 00:00:00 2001 From: sammefford Date: Mon, 27 Oct 2014 15:38:45 -0600 Subject: [PATCH 236/357] fix #98, move marker interfaces to public Jackson*Handle classes, not JacksonBaseHandle --- .../marklogic/client/impl/JacksonBaseHandle.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/marklogic/client/impl/JacksonBaseHandle.java b/src/main/java/com/marklogic/client/impl/JacksonBaseHandle.java index f68b91356..acde5d764 100644 --- a/src/main/java/com/marklogic/client/impl/JacksonBaseHandle.java +++ b/src/main/java/com/marklogic/client/impl/JacksonBaseHandle.java @@ -45,7 +45,8 @@ import com.marklogic.client.io.marker.XMLWriteHandle; public abstract class JacksonBaseHandle - extends BaseHandle + extends BaseHandle + implements OutputStreamSender { private ObjectMapper mapper; @@ -81,14 +82,14 @@ public void setMapper(ObjectMapper mapper) { } public abstract void set(T content); - @Override + public void fromBuffer(byte[] buffer) { if (buffer == null || buffer.length == 0) set(null); else receiveContent(new ByteArrayInputStream(buffer)); } - @Override + public byte[] toBuffer() { try { if ( ! hasContent() ) @@ -106,7 +107,7 @@ public byte[] toBuffer() { /** * Returns the JSON as a string. */ - @Override + public String toString() { try { return new String(toBuffer(),"UTF-8"); @@ -115,11 +116,11 @@ public String toString() { } } - @Override + protected Class receiveAs() { return InputStream.class; } - @Override + protected OutputStreamSender sendContent() { if ( ! hasContent() ) { throw new IllegalStateException("No document to write"); From c41e313600f6a9271f4a96c54973c39f642e6640 Mon Sep 17 00:00:00 2001 From: sammefford Date: Mon, 27 Oct 2014 16:29:53 -0600 Subject: [PATCH 237/357] fix another piece of #73, reuse ObjectMapper --- .../client/impl/JacksonBaseHandle.java | 1 + .../client/impl/PojoRepositoryImpl.java | 20 ++++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/marklogic/client/impl/JacksonBaseHandle.java b/src/main/java/com/marklogic/client/impl/JacksonBaseHandle.java index acde5d764..c027c4ad0 100644 --- a/src/main/java/com/marklogic/client/impl/JacksonBaseHandle.java +++ b/src/main/java/com/marklogic/client/impl/JacksonBaseHandle.java @@ -62,6 +62,7 @@ protected JacksonBaseHandle() { public ObjectMapper getMapper() { if (mapper == null) { mapper = new ObjectMapper(); + // if we don't do the next two lines Jackson will automatically close our streams which is undesirable mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); mapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false); } diff --git a/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java b/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java index d441f0d04..8e90d6566 100644 --- a/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java +++ b/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java @@ -36,6 +36,8 @@ import com.marklogic.client.query.QueryManager.QueryView; import com.marklogic.client.query.StructuredQueryDefinition; import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.Serializable; @@ -66,7 +68,20 @@ public class PojoRepositoryImpl private Method idMethod; private Field idProperty; private String idPropertyName; - + private ObjectMapper objectMapper = new ObjectMapper() + // if we don't do the next two lines Jackson will automatically close our streams which is undesirable + .configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false) + .configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false) + // enableDefaultTyping just means include types in the serialized output + // we need this to do strongly-typed queries + .enableDefaultTyping( + // ObjectMapper.DefaultTyping.NON_FINAL means that typing in serialized output + // for all non-final types except the "natural" types (String, Boolean, Integer, Double), + // which can be correctly inferred from JSON; as well as for all arrays of non-final types. + ObjectMapper.DefaultTyping.NON_FINAL, + // JsonTypeInfo.As.WRAPPER_OBJECT means add a type wrapper around the data so then + // our strongly-typed queries can use parent-child scoped queries or path index queries + JsonTypeInfo.As.WRAPPER_OBJECT); PojoRepositoryImpl(DatabaseClient client, Class entityClass) { this.client = client; this.entityClass = entityClass; @@ -97,8 +112,7 @@ public void write(T entity, Transaction transaction) { public void write(T entity, Transaction transaction, String... collections) { if ( entity == null ) return; JacksonDatabindHandle contentHandle = new JacksonDatabindHandle(entity); - contentHandle.getMapper().enableDefaultTyping( - ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_OBJECT); + contentHandle.setMapper(objectMapper); DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); metadataHandle = metadataHandle.withCollections(entityClass.getName()); if ( collections != null && collections.length > 0 ) { From 5fddd647c617f3cc81c80ef4d68d6206becb7216 Mon Sep 17 00:00:00 2001 From: sammefford Date: Mon, 27 Oct 2014 16:40:41 -0600 Subject: [PATCH 238/357] fix #160, rename current-query to lsqt-query --- .../marklogic/client/query/StructuredQueryBuilder.java | 10 +++++----- .../java/com/marklogic/client/test/BitemporalTest.java | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/marklogic/client/query/StructuredQueryBuilder.java b/src/main/java/com/marklogic/client/query/StructuredQueryBuilder.java index 1b5d00bde..6e205816b 100644 --- a/src/main/java/com/marklogic/client/query/StructuredQueryBuilder.java +++ b/src/main/java/com/marklogic/client/query/StructuredQueryBuilder.java @@ -1956,13 +1956,13 @@ public void innerSerialize(XMLStreamWriter serializer) throws Exception { } } - class TemporalCurrentQuery + class TemporalLsqtQuery extends AbstractStructuredQuery { private String temporalCollection; private String formattedTimestamp = null; private double weight; private String[] options; - TemporalCurrentQuery(String temporalCollection, Calendar timestamp, double weight, String[] options) { + TemporalLsqtQuery(String temporalCollection, Calendar timestamp, double weight, String[] options) { this.temporalCollection = temporalCollection; if ( timestamp != null ) { this.formattedTimestamp = DatatypeConverter.printDateTime(timestamp); @@ -1972,7 +1972,7 @@ class TemporalCurrentQuery } @Override public void innerSerialize(XMLStreamWriter serializer) throws Exception { - serializer.writeStartElement("current-query"); + serializer.writeStartElement("lsqt-query"); writeText(serializer, "temporal-collection", temporalCollection); if ( formattedTimestamp != null ) { writeText(serializer, "timestamp", formattedTimestamp); @@ -2766,10 +2766,10 @@ public StructuredQueryDefinition temporalPeriodCompare(Axis axis1, TemporalOpera return new TemporalPeriodCompareQuery(axis1, operator, axis2, options); } - public StructuredQueryDefinition temporalCurrent(String temporalCollection, Calendar timestamp, + public StructuredQueryDefinition temporalLsqtQuery(String temporalCollection, Calendar timestamp, double weight, String... options) { if ( temporalCollection == null ) throw new IllegalArgumentException("temporalCollection cannot be null"); - return new TemporalCurrentQuery(temporalCollection, timestamp, weight, options); + return new TemporalLsqtQuery(temporalCollection, timestamp, weight, options); } } diff --git a/src/test/java/com/marklogic/client/test/BitemporalTest.java b/src/test/java/com/marklogic/client/test/BitemporalTest.java index 4ccd9b414..c602f633e 100644 --- a/src/test/java/com/marklogic/client/test/BitemporalTest.java +++ b/src/test/java/com/marklogic/client/test/BitemporalTest.java @@ -117,7 +117,7 @@ public void test1() { DocumentPage termQueryResults = docMgr.search(termQuery, start); assertEquals("Wrong number of results", 4, termQueryResults.size()); - StructuredQueryDefinition currentQuery = sqb.temporalCurrent(temporalCollection, null, 1); + StructuredQueryDefinition currentQuery = sqb.temporalLsqtQuery(temporalCollection, null, 1); StructuredQueryDefinition currentDocQuery = sqb.and(termQuery, currentQuery); DocumentPage currentDocQueryResults = docMgr.search(currentDocQuery, start); assertEquals("Wrong number of results", 4, currentDocQueryResults.size()); From 890ffe95bb0f62296cf96af2dc97f22251a9f5f1 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Mon, 27 Oct 2014 17:10:34 -0700 Subject: [PATCH 239/357] First checkin for bitemporal testing --- .../marklogic/javaclient/TestBiTemporal.java | 2908 ++++++++++++++++- 1 file changed, 2890 insertions(+), 18 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBiTemporal.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBiTemporal.java index 86a5d0d2a..3e0f02607 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBiTemporal.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBiTemporal.java @@ -2,23 +2,60 @@ import static org.junit.Assert.*; +import java.io.File; +import java.util.Calendar; +import java.util.HashMap; import java.util.Iterator; +import javax.xml.bind.DatatypeConverter; +import javax.xml.parsers.DocumentBuilderFactory; + import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.ls.DOMImplementationLS; +import com.fasterxml.jackson.core.JsonFactory; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; import com.marklogic.client.DatabaseClient; import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.Transaction; import com.marklogic.client.DatabaseClientFactory.Authentication; -import com.marklogic.client.io.SearchHandle; -import com.marklogic.client.pojo.PojoPage; -import com.marklogic.client.pojo.PojoRepository; +import com.marklogic.client.Transaction; +import com.marklogic.client.admin.ExtensionMetadata; +import com.marklogic.client.admin.TransformExtensionsManager; +import com.marklogic.client.document.DocumentDescriptor; +import com.marklogic.client.document.DocumentPage; +import com.marklogic.client.document.DocumentRecord; +import com.marklogic.client.document.DocumentUriTemplate; +import com.marklogic.client.document.DocumentWriteSet; +import com.marklogic.client.document.GenericDocumentManager; +import com.marklogic.client.document.JSONDocumentManager; +import com.marklogic.client.document.ServerTransform; +import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.document.DocumentManager.Metadata; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.DocumentMetadataHandle; +import com.marklogic.client.io.FileHandle; +import com.marklogic.client.io.JacksonDatabindHandle; +import com.marklogic.client.io.DocumentMetadataHandle.Capability; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentCollections; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentPermissions; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentProperties; +import com.marklogic.client.io.Format; import com.marklogic.client.query.QueryManager; import com.marklogic.client.query.StringQueryDefinition; +import com.marklogic.client.query.StructuredQueryBuilder; +import com.marklogic.client.query.StructuredQueryDefinition; +import com.marklogic.client.query.StructuredQueryBuilder.TemporalOperator; + +// BUGS: 30098, 30092, public class TestBiTemporal extends BasicJavaClientREST{ @@ -28,31 +65,57 @@ public class TestBiTemporal extends BasicJavaClientREST{ private static int restPort = 8011; private DatabaseClient client ; + private final static String dateTimeDataTypeString = "dateTime"; + + private final static String systemStartERIName = "javaSystemStartERI"; + private final static String systemEndERIName = "javaSystemEndERI"; + private final static String validStartERIName = "javaValidStartERI"; + private final static String validEndERIName = "javaValidEndERI"; + + private final static String axisSystemName = "javaERISystemAxis"; + private final static String axisValidName = "javaERIValidAxis"; + + private final static String temporalCollectionName = "javaERITemporalCollection"; + + private final static String systemNodeName = "System"; + private final static String validNodeName = "Valid"; + private final static String addressNodeName = "Address"; + private final static String uriNodeName = "uri"; + + private final static String latestCollectionName = "latest"; + private final static String updateCollectionName = "updateCollection"; + private final static String insertCollectionName = "insertCollection"; + + @BeforeClass public static void setUpBeforeClass() throws Exception { -// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + System.out.println("In setup"); setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); - ConnectedRESTQA.addRangeElementIndex(dbName, "dateTime", "", "javaERISystemStart"); - ConnectedRESTQA.addRangeElementIndex(dbName, "dateTime", "", "javaERISystemEnd"); - ConnectedRESTQA.addRangeElementIndex(dbName, "dateTime", "", "javaERIValidStart"); - ConnectedRESTQA.addRangeElementIndex(dbName, "dateTime", "", "javaERIValidEnd"); + + ConnectedRESTQA.addRangeElementIndex(dbName, dateTimeDataTypeString, "", systemStartERIName); + ConnectedRESTQA.addRangeElementIndex(dbName, dateTimeDataTypeString, "", systemEndERIName); + ConnectedRESTQA.addRangeElementIndex(dbName, dateTimeDataTypeString, "", validStartERIName); + ConnectedRESTQA.addRangeElementIndex(dbName, dateTimeDataTypeString, "", validEndERIName); // Temporal axis must be created before temporal collection associated with those axes is created - ConnectedRESTQA.addElementRangeIndexTemporalAxis(dbName, "javaERISystem", "", "javaERISystemStart", "", "javaERISystemEnd"); - ConnectedRESTQA.addElementRangeIndexTemporalAxis(dbName, "javaERIValid", "", "javaERIValidStart", "", "javaERIValidEnd"); - ConnectedRESTQA.addElementRangeIndexTemporalCollection(dbName, "javaERITemporalCollection", "javaERISystem", "javaERIValid"); + ConnectedRESTQA.addElementRangeIndexTemporalAxis(dbName, axisSystemName, "", systemStartERIName, "", systemEndERIName); + ConnectedRESTQA.addElementRangeIndexTemporalAxis(dbName, axisValidName, "", validStartERIName, "", validEndERIName); + ConnectedRESTQA.addElementRangeIndexTemporalCollection(dbName, temporalCollectionName, axisSystemName, axisValidName); } @AfterClass public static void tearDownAfterClass() throws Exception { System.out.println("In tear down" ); + + // Delete database first. Otherwise axis and collection cannot be deleted + tearDownJavaRESTServer(dbName, fNames, restServerName); // Temporal collection needs to be delete before temporal axis associated with it can be deleted - ConnectedRESTQA.deleteElementRangeIndexTemporalCollection(dbName, "javaERITemporalCollection"); - ConnectedRESTQA.deleteElementRangeIndexTemporalAxis(dbName, "javaERIValid"); - ConnectedRESTQA.deleteElementRangeIndexTemporalAxis(dbName, "javaERISystem"); - tearDownJavaRESTServer(dbName, fNames, restServerName); + ConnectedRESTQA.deleteElementRangeIndexTemporalCollection("Documents", temporalCollectionName); + ConnectedRESTQA.deleteElementRangeIndexTemporalAxis("Documents", axisValidName); + ConnectedRESTQA.deleteElementRangeIndexTemporalAxis("Documents", axisSystemName); + } @Before @@ -62,10 +125,2819 @@ public void setUp() throws Exception { @After public void tearDown() throws Exception { - // release client + clearDB(restPort); client.release(); } + + public DocumentMetadataHandle setMetadata(boolean update) { + // create and initialize a handle on the meta-data + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); + + if (update) { + metadataHandle.getCollections().addAll("updateCollection"); + metadataHandle.getProperties().put("published", true); + + metadataHandle.getPermissions().add("app-user", Capability.UPDATE, + Capability.READ); + + metadataHandle.setQuality(99); + } + else { + metadataHandle.getCollections().addAll("insertCollection"); + metadataHandle.getProperties().put("reviewed", true); + + metadataHandle.getPermissions().add("app-user", Capability.UPDATE, + Capability.READ, Capability.EXECUTE); + + metadataHandle.setQuality(11); + } + + // metadataHandle.getPermissions().add("app-user", Capability.UPDATE, + // Capability.READ); + metadataHandle.getProperties().put("myString", "foo"); + metadataHandle.getProperties().put("myInteger", 10); + metadataHandle.getProperties().put("myDecimal", 34.56678); + metadataHandle.getProperties().put("myCalendar", + Calendar.getInstance().get(Calendar.YEAR)); + + return metadataHandle; + } + + public void validateMetadata(DocumentMetadataHandle mh) { + // get metadata values + DocumentProperties properties = mh.getProperties(); + DocumentPermissions permissions = mh.getPermissions(); + DocumentCollections collections = mh.getCollections(); + int quality = mh.getQuality(); + + // Properties + // String expectedProperties = + // "size:5|reviewed:true|myInteger:10|myDecimal:34.56678|myCalendar:2014|myString:foo|"; + String actualProperties = getDocumentPropertiesString(properties); + boolean result = actualProperties.contains("size:5|"); + assertTrue("Document properties count", result); + + // Permissions + String actualPermissions = getDocumentPermissionsString(permissions); + System.out.println(actualPermissions); + + /*** + assertTrue("Document permissions difference in size value", + actualPermissions.contains("size:3")); + + assertTrue("Document permissions difference in rest-reader permission", + actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", + actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue( + "Document permissions difference in app-user permissions", + (actualPermissions.contains("app-user:[UPDATE, READ]") || actualPermissions + .contains("app-user:[READ, UPDATE]"))); + ***/ + + /*** + // Collections + String actualCollections = getDocumentCollectionsString(collections); + System.out.println(collections); + + assertTrue("Document collections difference in size value", + actualCollections.contains("size:2")); + assertTrue("my-collection1 not found", + actualCollections.contains("my-collection1")); + assertTrue("my-collection2 not found", + actualCollections.contains("my-collection2")); + ***/ + } + + public void validateDefaultMetadata(DocumentMetadataHandle mh) { + // get metadata values + DocumentProperties properties = mh.getProperties(); + DocumentPermissions permissions = mh.getPermissions(); + DocumentCollections collections = mh.getCollections(); + + // Properties + String actualProperties = getDocumentPropertiesString(properties); + boolean result =actualProperties.contains("size:0|"); + System.out.println(actualProperties +result); + assertTrue("Document default last modified properties count1?", result); + + // Permissions + String actualPermissions = getDocumentPermissionsString(permissions); + + System.out.println("Permissions: " + actualPermissions); + + assertTrue("Document permissions difference in size value", actualPermissions.contains("size:2")); + //assertTrue("Document permissions difference in flexrep-eval permission", actualPermissions.contains("flexrep-eval:[READ]")); + assertTrue("Document permissions difference in rest-reader permission", actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", actualPermissions.contains("rest-writer:[UPDATE]")); + + /*** + // Collections + String expectedCollections = "size:0|"; + String actualCollections = getDocumentCollectionsString(collections); + + assertEquals("Document collections difference", expectedCollections, actualCollections); + ***/ + } + + // This covers passing transforms and descriptor + private void insertXMLSingleDocument(String docId, String transformName) throws Exception { + System.out.println("Inside insertXMLSingleDocument"); + + DOMHandle handle = getXMLDocumentHandle( + "2001-01-01T00:00:00", "2011-12-31T23:59:59", "999 Skyway Park - XML", docId); + + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); + + // put meta-data + DocumentDescriptor desc = docMgr.newDescriptor(docId); + DocumentMetadataHandle mh = setMetadata(false); + + if (transformName != null) { + TransformExtensionsManager transMgr = + client.newServerConfigManager().newTransformExtensionsManager(); + ExtensionMetadata metadata = new ExtensionMetadata(); + metadata.setTitle("Adding Element xquery Transform"); + metadata.setDescription("This plugin transforms an XML document by adding Element to root node"); + metadata.setProvider("MarkLogic"); + metadata.setVersion("0.1"); + // get the transform file + File transformFile = new File("src/test/java/com/marklogic/javaclient/transforms/" + transformName + ".xqy"); + FileHandle transformHandle = new FileHandle(transformFile); + transMgr.writeXQueryTransform(transformName, transformHandle, metadata); + ServerTransform transformer = new ServerTransform(transformName); + transformer.put("name", "Lang"); + transformer.put("value", "English"); + + docMgr.write(desc, mh, handle, transformer, null, temporalCollectionName, null); + } + else { + docMgr.write(desc, mh, handle, null, null, temporalCollectionName, null); + + // docMgr.write(docId, mh, handle, null, null, temporalCollectionName, DatatypeConverter.parseDateTime("2004-06-015T00:00:01")); + } + } + + // This covers passing transforms and descriptor + private void updateXMLSingleDocument(String docId, String transformName) throws Exception { + System.out.println("Inside updateXMLSingleDocument"); + + // Update the document + DOMHandle handle = getXMLDocumentHandle( + "2003-01-01T00:00:00", "2008-12-31T23:59:59", "1999 Skyway Park - Updated - XML", docId); + + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); + + DocumentDescriptor desc = docMgr.newDescriptor(docId); + DocumentMetadataHandle mh = setMetadata(true); // cannot set properties during update + + if (transformName != null) { + TransformExtensionsManager transMgr = + client.newServerConfigManager().newTransformExtensionsManager(); + ExtensionMetadata metadata = new ExtensionMetadata(); + metadata.setTitle("Adding Element xquery Transform"); + metadata.setDescription("This plugin transforms an XML document by adding Element to root node"); + metadata.setProvider("MarkLogic"); + metadata.setVersion("0.1"); + // get the transform file + File transformFile = new File("src/test/java/com/marklogic/javaclient/transforms/" + transformName + ".xqy"); + FileHandle transformHandle = new FileHandle(transformFile); + transMgr.writeXQueryTransform(transformName, transformHandle, metadata); + ServerTransform transformer = new ServerTransform(transformName); + transformer.put("name", "Lang"); + transformer.put("value", "English"); + + docMgr.write(desc, mh, handle, transformer, null, temporalCollectionName, null); + } + else { + docMgr.write(desc, mh, handle, null, null, temporalCollectionName, null); + } + } + + // This covers passing descriptor + public void deleteXMLSingleDocument(String docId) throws Exception { + + System.out.println("Inside deleteXMLSingleDocument"); + + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + + // docMgr.delete(docId, null, temporalCollectionName, DatatypeConverter.parseDateTime("2014-06-015T00:00:01")); + DocumentDescriptor desc = docMgr.newDescriptor(docId); + docMgr.delete(desc, null, temporalCollectionName, null); + } + + private DOMHandle getXMLDocumentHandle( + String startValidTime, String endValidTime, String address, String uri) throws Exception { + + Document domDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); + Element root = domDocument.createElement("root"); + + // System start and End time + Node systemNode = root.appendChild(domDocument.createElement("system")); + systemNode.appendChild(domDocument.createElement(systemStartERIName)); + systemNode.appendChild(domDocument.createElement(systemEndERIName)); + + // Valid start and End time + Node validNode = root.appendChild(domDocument.createElement("valid")); + + Node validStartNode = validNode.appendChild(domDocument.createElement(validStartERIName)); + validStartNode.appendChild(domDocument.createTextNode(startValidTime)); + validNode.appendChild(validStartNode); + + Node validEndNode = validNode.appendChild(domDocument.createElement(validEndERIName)); + validEndNode.appendChild(domDocument.createTextNode(endValidTime)); + validNode.appendChild(validEndNode); + + // Address + Node addressNode = root.appendChild(domDocument.createElement("Address")); + addressNode.appendChild(domDocument.createTextNode(address)); + + // uri + Node uriNode = root.appendChild(domDocument.createElement("uri")); + uriNode.appendChild(domDocument.createTextNode(uri)); + domDocument.appendChild(root); + + String domString = ((DOMImplementationLS) DocumentBuilderFactory + .newInstance() + .newDocumentBuilder() + .getDOMImplementation() + ).createLSSerializer().writeToString(domDocument); + + System.out.println(domString); + + DOMHandle handle = new DOMHandle().with(domDocument); + + return handle; + } + + public void insertJSONSingleDocument( + String docId, Transaction transaction) throws Exception { + + insertJSONSingleDocument(docId, transaction, null); + } + + public void insertJSONSingleDocument( + String docId, Transaction transaction, java.util.Calendar systemTime) throws Exception { + + System.out.println("Inside insertJSONSingleDocument"); + + JacksonDatabindHandle handle = getJSONDocumentHandle( + "2001-01-01T00:00:00", "2011-12-31T23:59:59", "999 Skyway Park - JSON", docId); + + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); + + // put meta-data + DocumentMetadataHandle mh = setMetadata(false); + + docMgr.write(docId, mh, handle, null, transaction, temporalCollectionName, systemTime); + } + + public void updateJSONSingleDocument( + String docId, Transaction transaction) throws Exception { + + updateJSONSingleDocument(docId, transaction, null); + } + + public void updateJSONSingleDocument( + String docId, Transaction transaction, java.util.Calendar systemTime) throws Exception { + + System.out.println("Inside updateJSONSingleDocumentString"); + + // Update the temporal document + JacksonDatabindHandle handle = getJSONDocumentHandle( + "2003-01-01T00:00:00", "2008-12-31T23:59:59", "1999 Skyway Park - Updated - JSON", docId); + + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); + DocumentMetadataHandle mh = setMetadata(true); // cannot set properties during update + docMgr.write(docId, mh, handle, null, transaction, temporalCollectionName, systemTime); + } + + public void deleteJSONSingleDocument( + String docId, Transaction transaction) throws Exception { + deleteJSONSingleDocument(docId, transaction, null); + } + + public void deleteJSONSingleDocument( + String docId, Transaction transaction, java.util.Calendar systemTime) throws Exception { + + System.out.println("Inside deleteJSONSingleDocument"); + + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + + // docMgr.delete(docId, null, temporalCollectionName, DatatypeConverter.parseDateTime("2014-06-015T00:00:01")); + docMgr.delete(docId, transaction, temporalCollectionName, systemTime); + } + + + + private JacksonDatabindHandle getJSONDocumentHandle( + String startValidTime, String endValidTime, String address, String uri) throws Exception { + + // Setup for JSON document + /** + * + { + "System": { + systemStartERIName : "", + systemEndERIName : "", + }, + "Valid": { + validStartERIName: "2001-01-01T00:00:00", + validEndERIName: "2011-12-31T23:59:59" + }, + "Address": "999 Skyway Park", + "uri": "javaSingleDoc1.json" + } + */ + + ObjectMapper mapper = new ObjectMapper(); + ObjectNode rootNode = mapper.createObjectNode(); + + // Set system time values + ObjectNode system = mapper.createObjectNode(); + + system.put(systemStartERIName, ""); + system.put(systemEndERIName, ""); + rootNode.set(systemNodeName, system); + + // Set valid time values + ObjectNode valid = mapper.createObjectNode(); + + valid.put(validStartERIName, startValidTime); + valid.put(validEndERIName, endValidTime); + rootNode.set(validNodeName, valid); + + // Set Address + rootNode.put(addressNodeName, address); + + // Set uri + rootNode.put(uriNodeName, uri); + + System.out.println(rootNode.toString()); + + JacksonDatabindHandle handle = new JacksonDatabindHandle(ObjectNode.class).withFormat(Format.JSON); + handle.set(rootNode); + + return handle; + } + + @Test + public void testInsertXMLSingleDocumentUsingTemplate() throws Exception { + System.out.println("Inside testInsertXMLSingleDocumentUsingDescriptor"); + + String docId = "javaSingleXMLDoc.xml"; + DOMHandle handle = getXMLDocumentHandle( + "2001-01-01T00:00:00", "2011-12-31T23:59:59", "777 Skyway Park - XML", docId); + + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); + + // Create document using using document template + String dirName = "/java/bitemporal/"; + String fileSuffix = "xml"; + DocumentUriTemplate template = docMgr.newDocumentUriTemplate(fileSuffix); + template.setDirectory(dirName); + DocumentMetadataHandle mh = setMetadata(false); + + docMgr.create(template, mh, handle, null, null, temporalCollectionName, null); + + // Make sure there are no documents associated with "latest" collection + QueryManager queryMgr = client.newQueryManager(); + StructuredQueryBuilder sqb = queryMgr.newStructuredQueryBuilder(); + + String[] collections = {latestCollectionName, "insertCollection"}; + StructuredQueryDefinition termQuery = sqb.collection(collections); + + long start = 1; + docMgr = client.newXMLDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); // Get all metadata + DocumentPage termQueryResults = docMgr.search(termQuery, start); + + + long count = 0; + while (termQueryResults.hasNext()) { + ++count; + DocumentRecord record = termQueryResults.next(); + + String uri = record.getUri(); + System.out.println("URI = " + uri); + + if (!uri.contains(dirName) && !uri.contains(fileSuffix)) { + assertFalse("Uri name does not have the right prefix or suffix", true); + } + } + + System.out.println("Number of results = " + count); + assertEquals("Wrong number of results", 1, count); + + System.out.println("Done"); + } + + @Test + public void testInsertAndUpdateXMLSingleDocumentUsingInvalidTransform() throws Exception { + + System.out.println("Inside testXMLWriteSingleDocument"); + String docId = "javaSingleXMLDoc.xml"; + + insertXMLSingleDocument(docId, null); + boolean exceptionThrown = false; + try { + updateXMLSingleDocument(docId, "add-element-xquery-invalid-bitemp-transform"); + } + catch (com.marklogic.client.FailedRequestException ex) { + exceptionThrown = true; + System.out.println(ex.getFailedRequest().getStatusCode()); + System.out.println(ex.getFailedRequest().getMessageCode()); + + // BUG: Right now this returns 500 error. Bug is open to fix this + // assert(ex.getFailedRequest().getMessageCode().equals("TEMPORAL-SYSTEMTIME-BACKWARDS")); + // assert(ex.getFailedRequest().getStatusCode() == 400); + } + + assertTrue("Exception not thrown for invalid transform", exceptionThrown); + } + + + @Test + // This test validates the following - + // 1. Inserts, updates and delete and and also makes sure number of documents in + // doc uri collection, latest collection are accurate after those operations. Do this + // for more than one document URI (we do this with JSON and XML) + // 2. Make sure things are correct with transforms + public void testConsolidated() throws Exception { + + System.out.println("Inside testXMLConsolidated"); + String xmlDocId = "javaSingleXMLDoc.xml"; + + //============================================================================= + // Check insert works + //============================================================================= + // Insert XML document + insertXMLSingleDocument(xmlDocId, "add-element-xquery-transform"); // Transforming during insert + + // Verify that the document was inserted + XMLDocumentManager xmlDocMgr = client.newXMLDocumentManager(); + DocumentPage readResults = xmlDocMgr.read(xmlDocId); + System.out.println("Number of results = " + readResults.size()); + assertEquals("Wrong number of results", 1, readResults.size()); + + // Now insert a JSON document + String jsonDocId = "javaSingleJSONDoc.json"; + insertJSONSingleDocument(jsonDocId, null); + + // Verify that the document was inserted + JSONDocumentManager jsonDocMgr = client.newJSONDocumentManager(); + readResults = jsonDocMgr.read(jsonDocId); + System.out.println("Number of results = " + readResults.size()); + assertEquals("Wrong number of results", 1, readResults.size()); + + //============================================================================= + // Check update works + //============================================================================= + // Update XML document + updateXMLSingleDocument(xmlDocId, null); // Not transforming during update + + // Make sure there are 2 documents in latest collection + QueryManager queryMgr = client.newQueryManager(); + StructuredQueryBuilder sqb = queryMgr. newStructuredQueryBuilder(); + StructuredQueryDefinition termQuery = sqb.collection(latestCollectionName); + + long start = 1; + DocumentPage termQueryResults = xmlDocMgr.search(termQuery, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 2, termQueryResults.getTotalSize()); + + // Make sure there are 4 documents in xmlDocId collection with term XML + queryMgr = client.newQueryManager(); + sqb = queryMgr.newStructuredQueryBuilder(); + termQuery = sqb.and( + sqb.and(sqb.term("XML"), sqb.collection(xmlDocId))); + + start = 1; + termQueryResults = xmlDocMgr.search(termQuery, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 4, termQueryResults.getTotalSize()); + + // Make sure transform on insert worked + while (termQueryResults.hasNext()) { + DocumentRecord record = termQueryResults.next(); + System.out.println("URI = " +record.getUri()); + + if (record.getFormat() != Format.XML) { + assertFalse("Format is not JSON: " + Format.JSON, true); + } + else { + + DOMHandle recordHandle = new DOMHandle(); + record.getContent(recordHandle); + + String content = recordHandle.toString(); + System.out.println("Content = " + content); + + // Check if transform worked. We did transform only with XML document + if ((content.contains("2001-01-01T00:00:00") && content.contains("2011-12-31T23:59:59") && record.getFormat() != Format.XML) + && (!content.contains("new-element") || !content.contains("2007-12-31T23:59:59"))) { + assertFalse("Transform did not work", true); + } + else { + System.out.println("Transform Worked!"); + } + } + } + + // Update JSON document + updateJSONSingleDocument(jsonDocId, null); + + // Make sure there are still 2 documents in latest collection + queryMgr = client.newQueryManager(); + sqb = queryMgr.newStructuredQueryBuilder(); + termQuery = sqb.collection(latestCollectionName); + + start = 1; + termQueryResults = xmlDocMgr.search(termQuery, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 2, termQueryResults.getTotalSize()); + + // Docu URIs in latest collection must be the same as the one as the original document + while (termQueryResults.hasNext()) { + DocumentRecord record = termQueryResults.next(); + + String uri = record.getUri(); + System.out.println("URI = " + uri); + + if (!uri.equals(xmlDocId) && !uri.equals(jsonDocId)) { + assertFalse("URIs are not what is expected", true); + } + } + + // Make sure there are 4 documents in jsonDocId collection + queryMgr = client.newQueryManager(); + sqb = queryMgr.newStructuredQueryBuilder(); + termQuery = sqb.collection(jsonDocId); + + start = 1; + termQueryResults = xmlDocMgr.search(termQuery, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 4, termQueryResults.getTotalSize()); + + // Make sure there are 8 documents in temporal collection + queryMgr = client.newQueryManager(); + sqb = queryMgr.newStructuredQueryBuilder(); + termQuery = sqb.collection(temporalCollectionName); + + start = 1; + termQueryResults = xmlDocMgr.search(termQuery, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 8, termQueryResults.getTotalSize()); + + // Make sure there are 8 documents in total. Use string search for this + queryMgr = client.newQueryManager(); + StringQueryDefinition stringQD = queryMgr.newStringDefinition(); + stringQD.setCriteria(""); + + start = 1; + termQueryResults = xmlDocMgr.search(stringQD, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 8, termQueryResults.getTotalSize()); + + //============================================================================= + // Check delete works + //============================================================================= + // Delete one of the document + deleteXMLSingleDocument(xmlDocId); + + // Make sure there are still 4 documents in xmlDocId collection + queryMgr = client.newQueryManager(); + sqb = queryMgr.newStructuredQueryBuilder(); + termQuery = sqb.collection(xmlDocId); + + start = 1; + termQueryResults = xmlDocMgr.search(termQuery, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 4, termQueryResults.getTotalSize()); + + // Make sure there is one document with xmlDocId uri + XMLDocumentManager docMgr = client.newXMLDocumentManager(); + readResults = docMgr.read(xmlDocId); + + System.out.println("Number of results = " + readResults.size()); + assertEquals("Wrong number of results", 1, readResults.size()); + + // Make sure there is only 1 document in latest collection + queryMgr = client.newQueryManager(); + sqb = queryMgr.newStructuredQueryBuilder(); + termQuery = sqb.collection(latestCollectionName); + + start = 1; + termQueryResults = xmlDocMgr.search(termQuery, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 1, termQueryResults.getTotalSize()); + + // Docu URIs in latest collection must be the same as the one as the original document + while (termQueryResults.hasNext()) { + DocumentRecord record = termQueryResults.next(); + + String uri = record.getUri(); + System.out.println("URI = " + uri); + + if (!uri.equals(jsonDocId)) { + assertFalse("URIs are not what is expected", true); + } + } + + // Make sure there are 8 documents in temporal collection + queryMgr = client.newQueryManager(); + sqb = queryMgr.newStructuredQueryBuilder(); + termQuery = sqb.collection(temporalCollectionName); + + start = 1; + termQueryResults = xmlDocMgr.search(termQuery, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 8, termQueryResults.getTotalSize()); + } + + @Test + public void testJSONConsolidated() throws Exception { + + System.out.println("Inside testJSONConsolidated"); + + String docId = "javaSingleJSONDoc.json"; + insertJSONSingleDocument(docId, null); + + // Verify that the document was inserted + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + DocumentPage readResults = docMgr.read(docId); + + System.out.println("Number of results = " + readResults.size()); + assertEquals("Wrong number of results", 1, readResults.size()); + + DocumentRecord latestDoc = readResults.next(); + System.out.println("URI after insert = " + latestDoc.getUri()); + assertEquals("Document uri wrong after insert", docId, latestDoc.getUri()); + + + // Check if properties have been set. User XML DOcument Manager since properties are written as XML + JacksonDatabindHandle contentHandle = new JacksonDatabindHandle(ObjectNode.class); + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); + docMgr.read (docId, metadataHandle, contentHandle); + + validateMetadata(metadataHandle); + + //================================================================ + // Update the document + updateJSONSingleDocument(docId, null); + + // Verify that the document was updated + // Make sure there is 1 document in latest collection + QueryManager queryMgr = client.newQueryManager(); + StructuredQueryBuilder sqb = queryMgr.newStructuredQueryBuilder(); + StructuredQueryDefinition termQuery = sqb.collection(latestCollectionName); + long start = 1; + DocumentPage termQueryResults = docMgr.search(termQuery, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 1, termQueryResults.getTotalSize()); + + // Docu URIs in latest collection must be the same as the one as the original document + while (termQueryResults.hasNext()) { + DocumentRecord record = termQueryResults.next(); + + String uri = record.getUri(); + System.out.println("URI = " + uri); + + if (!uri.equals(docId)) { + assertFalse("URIs are not what is expected", true); + } + } + + // Make sure there are 4 documents in jsonDocId collection + queryMgr = client.newQueryManager(); + sqb = queryMgr.newStructuredQueryBuilder(); + termQuery = sqb.collection(docId); + + start = 1; + termQueryResults = docMgr.search(termQuery, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 4, termQueryResults.getTotalSize()); + + // Make sure there are 4 documents in temporal collection + queryMgr = client.newQueryManager(); + sqb = queryMgr.newStructuredQueryBuilder(); + termQuery = sqb.collection(temporalCollectionName); + + start = 1; + termQueryResults = docMgr.search(termQuery, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 4, termQueryResults.getTotalSize()); + + // Make sure there are 4 documents in total. Use string search for this + queryMgr = client.newQueryManager(); + StringQueryDefinition stringQD = queryMgr.newStringDefinition(); + stringQD.setCriteria(""); + + start = 1; + docMgr.setMetadataCategories(Metadata.ALL); + termQueryResults = docMgr.search(stringQD, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 4, termQueryResults.getTotalSize()); + + while (termQueryResults.hasNext()) { + DocumentRecord record = termQueryResults.next(); + System.out.println("URI = " + record.getUri()); + + metadataHandle = new DocumentMetadataHandle(); + record.getMetadata(metadataHandle); + Iterator resCollections = metadataHandle.getCollections().iterator(); + while (resCollections.hasNext()) { + String collection = resCollections.next(); + System.out.println("Collection = " + collection); + + if (!collection.equals(docId) && !collection.equals(docId) && !collection.equals(latestCollectionName) && + !collection.equals(updateCollectionName) && !collection.equals(insertCollectionName) && + !collection.equals(temporalCollectionName)) { + assertFalse("Collection not what is expected: " + collection, true); + } + } + + if (record.getFormat() != Format.JSON) { + assertFalse("Format is not JSON: " + Format.JSON, true); + } + else { + JacksonDatabindHandle recordHandle = new JacksonDatabindHandle(ObjectNode.class); + record.getContent (recordHandle); + System.out.println("Content = " + recordHandle.toString()); + } + } + + //============================================================================= + // Check delete works + //============================================================================= + // Delete one of the document + deleteJSONSingleDocument(docId, null); + + // Make sure there are still 4 documents in docId collection + queryMgr = client.newQueryManager(); + sqb = queryMgr.newStructuredQueryBuilder(); + termQuery = sqb.collection(docId); + + start = 1; + termQueryResults = docMgr.search(termQuery, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 4, termQueryResults.getTotalSize()); + + // Make sure there is one document with docId uri + docMgr = client.newJSONDocumentManager(); + readResults = docMgr.read(docId); + + System.out.println("Number of results = " + readResults.size()); + assertEquals("Wrong number of results", 1, readResults.size()); + + // Make sure there are no documents in latest collection + queryMgr = client.newQueryManager(); + sqb = queryMgr.newStructuredQueryBuilder(); + termQuery = sqb.collection(latestCollectionName); + + start = 1; + termQueryResults = docMgr.search(termQuery, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 0, termQueryResults.getTotalSize()); + + // Make sure there are 4 documents in temporal collection + queryMgr = client.newQueryManager(); + sqb = queryMgr.newStructuredQueryBuilder(); + termQuery = sqb.collection(temporalCollectionName); + + start = 1; + termQueryResults = docMgr.search(termQuery, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 4, termQueryResults.getTotalSize()); + + // Make sure there are 4 documents in total. Use string search for this + queryMgr = client.newQueryManager(); + + start = 1; + termQueryResults = docMgr.search(stringQD, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 4, termQueryResults.getTotalSize()); + } + + @Test + // Test if system time can be set. We do this with insert, update and delete + public void testSystemTime() throws Exception { + + System.out.println("Inside testSystemTime"); + + String docId = "javaSingleJSONDoc.json"; + + Calendar firstInsertTime = DatatypeConverter.parseDateTime("2010-01-01T00:00:01"); + insertJSONSingleDocument(docId, null, firstInsertTime); + + // Verify that the document was inserted + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + JacksonDatabindHandle recordHandle = new JacksonDatabindHandle(ObjectNode.class); + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); + docMgr.read (docId, metadataHandle, recordHandle); + DocumentPage readResults = docMgr.read(docId); + + System.out.println("Number of results = " + readResults.size()); + assertEquals("Wrong number of results", 1, readResults.size()); // 2, because includes content and metadata + + DocumentRecord record = readResults.next(); + System.out.println("URI after insert = " + record.getUri()); + assertEquals("Document uri wrong after insert", docId, record.getUri()); + System.out.println("Content = " + recordHandle.toString()); + + // Make sure System start time was what was set ("2010-01-01T00:00:01") + if (record.getFormat() != Format.JSON) { + assertFalse("Invalid document format: " + record.getFormat(), true); + } + else { + JsonFactory factory = new JsonFactory(); + ObjectMapper mapper = new ObjectMapper(factory); + TypeReference> typeRef + = new TypeReference>() {}; + + HashMap docObject = mapper.readValue(recordHandle.toString(), typeRef); + + @SuppressWarnings("unchecked") + HashMap validNode = (HashMap)(docObject.get(systemNodeName)); + + String systemStartDate = (String)validNode.get(systemStartERIName); + String systemEndDate = (String)validNode.get(systemEndERIName); + System.out.println("systemStartDate = " + systemStartDate); + System.out.println("systemEndDate = " + systemEndDate); + + assertTrue("System start date check failed", (systemStartDate.contains("2010-01-01T00:00:01"))); + assertTrue("System end date check failed", (systemEndDate.contains("9999-12-31T23:59:59"))); + + // Validate collections + Iterator resCollections = metadataHandle.getCollections().iterator(); + while (resCollections.hasNext()) { + String collection = resCollections.next(); + System.out.println("Collection = " + collection); + + if (!collection.equals(docId) && + !collection.equals(insertCollectionName) && + !collection.equals(temporalCollectionName) && + !collection.equals(latestCollectionName)) { + assertFalse("Collection not what is expected: " + collection, true); + } + } + + // Validate permissions + DocumentPermissions permissions = metadataHandle.getPermissions(); + System.out.println("Permissions: " + permissions); + + String actualPermissions = getDocumentPermissionsString(permissions); + System.out.println("actualPermissions: " + actualPermissions); + + assertTrue("Document permissions difference in size value", + actualPermissions.contains("size:3")); + + assertTrue("Document permissions difference in rest-reader permission", + actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", + actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue("Document permissions difference in app-user permission", + (actualPermissions.contains("app-user:[") && actualPermissions.contains("READ") && + actualPermissions.contains("UPDATE") && actualPermissions.contains("EXECUTE"))); + + // Validate quality + int quality = metadataHandle.getQuality(); + System.out.println("Quality: " + quality); + assertEquals(quality, 11); + + validateMetadata(metadataHandle); + } + + //============================================================================= + // Check update works + //============================================================================= + Calendar updateTime = DatatypeConverter.parseDateTime("2011-01-01T00:00:01"); + updateJSONSingleDocument(docId, null, updateTime); + + // Verify that the document was updated + // Make sure there is 1 document in latest collection + QueryManager queryMgr = client.newQueryManager(); + StructuredQueryBuilder sqb = queryMgr.newStructuredQueryBuilder(); + StructuredQueryDefinition termQuery = sqb.collection(latestCollectionName); + long start = 1; + DocumentPage termQueryResults = docMgr.search(termQuery, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 1, termQueryResults.getTotalSize()); + + // Document URIs in latest collection must be the same as the one as the original document + while (termQueryResults.hasNext()) { + record = termQueryResults.next(); + + String uri = record.getUri(); + System.out.println("URI = " + uri); + + if (!uri.equals(docId)) { + assertFalse("URIs are not what is expected", true); + } + } + + // Make sure there are 4 documents in jsonDocId collection + queryMgr = client.newQueryManager(); + sqb = queryMgr.newStructuredQueryBuilder(); + termQuery = sqb.collection(docId); + + start = 1; + termQueryResults = docMgr.search(termQuery, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 4, termQueryResults.getTotalSize()); + + // Make sure there are 4 documents in temporal collection + queryMgr = client.newQueryManager(); + sqb = queryMgr.newStructuredQueryBuilder(); + termQuery = sqb.collection(temporalCollectionName); + + start = 1; + termQueryResults = docMgr.search(termQuery, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 4, termQueryResults.getTotalSize()); + + // Make sure there are 4 documents in total. Use string search for this + queryMgr = client.newQueryManager(); + StringQueryDefinition stringQD = queryMgr.newStringDefinition(); + stringQD.setCriteria(""); + + start = 1; + docMgr.setMetadataCategories(Metadata.ALL); + termQueryResults = docMgr.search(stringQD, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 4, termQueryResults.getTotalSize()); + + while (termQueryResults.hasNext()) { + record = termQueryResults.next(); + System.out.println("URI = " + record.getUri()); + + metadataHandle = new DocumentMetadataHandle(); + record.getMetadata(metadataHandle); + + if (record.getFormat() != Format.JSON) { + assertFalse("Format is not JSON: " + Format.JSON, true); + } + else { + // Make sure that system and valid times are what is expected + recordHandle = new JacksonDatabindHandle(ObjectNode.class); + record.getContent (recordHandle); + System.out.println("Content = " + recordHandle.toString()); + + JsonFactory factory = new JsonFactory(); + ObjectMapper mapper = new ObjectMapper(factory); + TypeReference> typeRef + = new TypeReference>() {}; + + HashMap docObject = mapper.readValue(recordHandle.toString(), typeRef); + + @SuppressWarnings("unchecked") + HashMap systemNode = (HashMap)(docObject.get(systemNodeName)); + + String systemStartDate = (String)systemNode.get(systemStartERIName); + String systemEndDate = (String)systemNode.get(systemEndERIName); + System.out.println("systemStartDate = " + systemStartDate); + System.out.println("systemEndDate = " + systemEndDate); + + @SuppressWarnings("unchecked") + HashMap validNode = (HashMap)(docObject.get(validNodeName)); + + String validStartDate = (String)validNode.get(validStartERIName); + String validEndDate = (String)validNode.get(validEndERIName); + System.out.println("validStartDate = " + validStartDate); + System.out.println("validEndDate = " + validEndDate); + + // Permissions + DocumentPermissions permissions = metadataHandle.getPermissions(); + System.out.println("Permissions: " + permissions); + + String actualPermissions = getDocumentPermissionsString(permissions); + System.out.println("actualPermissions: " + actualPermissions); + + int quality = metadataHandle.getQuality(); + System.out.println("Quality: " + quality); + + if (validStartDate.contains("2003-01-01T00:00:00") && validEndDate.contains("2008-12-31T23:59:59")) { + assertTrue("System start date check failed", (systemStartDate.contains("2011-01-01T00:00:01"))); + assertTrue("System start date check failed", (systemEndDate.contains("9999-12-31T23:59:59"))); + + Iterator resCollections = metadataHandle.getCollections().iterator(); + while (resCollections.hasNext()) { + String collection = resCollections.next(); + System.out.println("Collection = " + collection); + + if (!collection.equals(docId) && + !collection.equals(updateCollectionName) && + !collection.equals(temporalCollectionName)) { + assertFalse("Collection not what is expected: " + collection, true); + } + } + + assertTrue("Properties should be empty", metadataHandle.getProperties().isEmpty()); + + assertTrue("Document permissions difference in size value", + actualPermissions.contains("size:3")); + + assertTrue("Document permissions difference in rest-reader permission", + actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", + actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue("Document permissions difference in app-user permission", + (actualPermissions.contains("app-user:[") && actualPermissions.contains("READ") && + actualPermissions.contains("UPDATE"))); + assertFalse("Document permissions difference in app-user permission", actualPermissions.contains("EXECUTE")); + + assertEquals(quality, 99); + } + + if (validStartDate.contains("2001-01-01T00:00:00") && validEndDate.contains("2003-01-01T00:00:00")) { + assertTrue("System start date check failed", (systemStartDate.contains("2011-01-01T00:00:01"))); + assertTrue("System start date check failed", (systemEndDate.contains("9999-12-31T23:59:59"))); + + Iterator resCollections = metadataHandle.getCollections().iterator(); + while (resCollections.hasNext()) { + String collection = resCollections.next(); + System.out.println("Collection = " + collection); + + if (!collection.equals(docId) && + !collection.equals(insertCollectionName) && + !collection.equals(temporalCollectionName)) { + assertFalse("Collection not what is expected: " + collection, true); + } + } + + assertTrue("Properties should be empty", metadataHandle.getProperties().isEmpty()); + + assertTrue("Document permissions difference in size value", + actualPermissions.contains("size:3")); + + assertTrue("Document permissions difference in rest-reader permission", + actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", + actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue("Document permissions difference in app-user permission", + (actualPermissions.contains("app-user:[") && actualPermissions.contains("READ") && + actualPermissions.contains("UPDATE") && actualPermissions.contains("EXECUTE"))); + + // BUG: Returns a 99, when it should be 11 + // assertEquals(quality, 11); + } + + if (validStartDate.contains("2008-12-31T23:59:59") && validEndDate.contains("2011-12-31T23:59:59")) { + // This is the latest document + assertTrue("System start date check failed", (systemStartDate.contains("2011-01-01T00:00:01"))); + assertTrue("System start date check failed", (systemEndDate.contains("9999-12-31T23:59:59"))); + assertTrue("URI should be the doc uri ", record.getUri().equals(docId)); + + Iterator resCollections = metadataHandle.getCollections().iterator(); + while (resCollections.hasNext()) { + String collection = resCollections.next(); + System.out.println("Collection = " + collection); + + if (!collection.equals(docId) && + !collection.equals(insertCollectionName) && + !collection.equals(temporalCollectionName) && + !collection.equals(latestCollectionName)) { + assertFalse("Collection not what is expected: " + collection, true); + } + } + + + assertTrue("Document permissions difference in size value", + actualPermissions.contains("size:3")); + + assertTrue("Document permissions difference in rest-reader permission", + actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", + actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue("Document permissions difference in app-user permission", + (actualPermissions.contains("app-user:[") && actualPermissions.contains("READ") && + actualPermissions.contains("UPDATE") && actualPermissions.contains("EXECUTE"))); + + // BUG: Returns a 99, when it should be 11 + // assertEquals(quality, 11); + + validateMetadata(metadataHandle); + } + + if (validStartDate.contains("2001-01-01T00:00:00") && validEndDate.contains("2011-12-31T23:59:59")) { + assertTrue("System start date check failed", (systemStartDate.contains("2010-01-01T00:00:01"))); + assertTrue("System start date check failed", (systemEndDate.contains("2011-01-01T00:00:01"))); + + Iterator resCollections = metadataHandle.getCollections().iterator(); + while (resCollections.hasNext()) { + String collection = resCollections.next(); + System.out.println("Collection = " + collection); + + if (!collection.equals(docId) && + !collection.equals(insertCollectionName) && + !collection.equals(temporalCollectionName)) { + assertFalse("Collection not what is expected: " + collection, true); + } + } + + assertTrue("Properties should be empty", metadataHandle.getProperties().isEmpty()); + + assertTrue("Document permissions difference in size value", + actualPermissions.contains("size:3")); + + assertTrue("Document permissions difference in rest-reader permission", + actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", + actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue("Document permissions difference in app-user permission", + (actualPermissions.contains("app-user:[") && actualPermissions.contains("READ") && + actualPermissions.contains("UPDATE") && actualPermissions.contains("EXECUTE"))); + + // BUG: Returns a 99, when it should be 11 + // assertEquals(quality, 11); + } + } + } + + //============================================================================= + // Check delete works + //============================================================================= + // Delete one of the document + Calendar deleteTime = DatatypeConverter.parseDateTime("2012-01-01T00:00:01"); + deleteJSONSingleDocument(docId, null, deleteTime); + + // Make sure there are still 4 documents in docId collection + queryMgr = client.newQueryManager(); + sqb = queryMgr.newStructuredQueryBuilder(); + termQuery = sqb.collection(docId); + + start = 1; + termQueryResults = docMgr.search(termQuery, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 4, termQueryResults.getTotalSize()); + + // Make sure there is one document with docId uri + docMgr = client.newJSONDocumentManager(); + readResults = docMgr.read(docId); + + System.out.println("Number of results = " + readResults.size()); + assertEquals("Wrong number of results", 1, readResults.size()); + + // Make sure there are no documents in latest collection + queryMgr = client.newQueryManager(); + sqb = queryMgr.newStructuredQueryBuilder(); + termQuery = sqb.collection(latestCollectionName); + + start = 1; + termQueryResults = docMgr.search(termQuery, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 0, termQueryResults.getTotalSize()); + + // Make sure there are 4 documents in temporal collection + queryMgr = client.newQueryManager(); + sqb = queryMgr.newStructuredQueryBuilder(); + termQuery = sqb.collection(temporalCollectionName); + + start = 1; + docMgr.setMetadataCategories(Metadata.ALL); + termQueryResults = docMgr.search(termQuery, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 4, termQueryResults.getTotalSize()); + + while (termQueryResults.hasNext()) { + record = termQueryResults.next(); + System.out.println("URI = " + record.getUri()); + + metadataHandle = new DocumentMetadataHandle(); + record.getMetadata(metadataHandle); + + if (record.getFormat() != Format.JSON) { + assertFalse("Format is not JSON: " + Format.JSON, true); + } + else { + // Make sure that system and valid times are what is expected + recordHandle = new JacksonDatabindHandle(ObjectNode.class); + record.getContent (recordHandle); + System.out.println("Content = " + recordHandle.toString()); + + JsonFactory factory = new JsonFactory(); + ObjectMapper mapper = new ObjectMapper(factory); + TypeReference> typeRef + = new TypeReference>() {}; + + HashMap docObject = mapper.readValue(recordHandle.toString(), typeRef); + + @SuppressWarnings("unchecked") + HashMap systemNode = (HashMap)(docObject.get(systemNodeName)); + + String systemStartDate = (String)systemNode.get(systemStartERIName); + String systemEndDate = (String)systemNode.get(systemEndERIName); + System.out.println("systemStartDate = " + systemStartDate); + System.out.println("systemEndDate = " + systemEndDate); + + @SuppressWarnings("unchecked") + HashMap validNode = (HashMap)(docObject.get(validNodeName)); + + String validStartDate = (String)validNode.get(validStartERIName); + String validEndDate = (String)validNode.get(validEndERIName); + System.out.println("validStartDate = " + validStartDate); + System.out.println("validEndDate = " + validEndDate); + + // Permissions + DocumentPermissions permissions = metadataHandle.getPermissions(); + System.out.println("Permissions: " + permissions); + + String actualPermissions = getDocumentPermissionsString(permissions); + System.out.println("actualPermissions: " + actualPermissions); + + int quality = metadataHandle.getQuality(); + System.out.println("Quality: " + quality); + + if (validStartDate.contains("2003-01-01T00:00:00") && validEndDate.contains("2008-12-31T23:59:59")) { + assertTrue("System start date check failed", (systemStartDate.contains("2011-01-01T00:00:01"))); + assertTrue("System start date check failed", (systemEndDate.contains("2012-01-01T00:00:01"))); + + Iterator resCollections = metadataHandle.getCollections().iterator(); + while (resCollections.hasNext()) { + String collection = resCollections.next(); + System.out.println("Collection = " + collection); + + if (!collection.equals(docId) && + !collection.equals(updateCollectionName) && + !collection.equals(temporalCollectionName)) { + assertFalse("Collection not what is expected: " + collection, true); + } + } + + assertTrue("Properties should be empty", metadataHandle.getProperties().isEmpty()); + + assertTrue("Document permissions difference in size value", + actualPermissions.contains("size:3")); + + assertTrue("Document permissions difference in rest-reader permission", + actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", + actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue("Document permissions difference in app-user permission", + (actualPermissions.contains("app-user:[") && actualPermissions.contains("READ") && + actualPermissions.contains("UPDATE"))); + assertFalse("Document permissions difference in app-user permission", actualPermissions.contains("EXECUTE")); + + // BUG: Returns a 0, when it should be 99 + // assertEquals(quality, 99); + } + + if (validStartDate.contains("2001-01-01T00:00:00") && validEndDate.contains("2003-01-01T00:00:00")) { + assertTrue("System start date check failed", (systemStartDate.contains("2011-01-01T00:00:01"))); + assertTrue("System start date check failed", (systemEndDate.contains("2012-01-01T00:00:01"))); + + Iterator resCollections = metadataHandle.getCollections().iterator(); + while (resCollections.hasNext()) { + String collection = resCollections.next(); + System.out.println("Collection = " + collection); + + if (!collection.equals(docId) && + !collection.equals(insertCollectionName) && + !collection.equals(temporalCollectionName)) { + assertFalse("Collection not what is expected: " + collection, true); + } + } + + assertTrue("Properties should be empty", metadataHandle.getProperties().isEmpty()); + + assertTrue("Document permissions difference in size value", + actualPermissions.contains("size:3")); + + assertTrue("Document permissions difference in rest-reader permission", + actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", + actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue("Document permissions difference in app-user permission", + (actualPermissions.contains("app-user:[") && actualPermissions.contains("READ") && + actualPermissions.contains("UPDATE") && actualPermissions.contains("EXECUTE"))); + + // BUG: Returns a 0, when it should be 11 + // assertEquals(quality, 11); + } + + if (validStartDate.contains("2008-12-31T23:59:59") && validEndDate.contains("2011-12-31T23:59:59")) { + assertTrue("System start date check failed", (systemStartDate.contains("2011-01-01T00:00:01"))); + assertTrue("System start date check failed", (systemEndDate.contains("2012-01-01T00:00:01"))); + + assertTrue("URI should be the doc uri ", record.getUri().equals(docId)); + + // Document should not be in latest collection + Iterator resCollections = metadataHandle.getCollections().iterator(); + while (resCollections.hasNext()) { + String collection = resCollections.next(); + System.out.println("Collection = " + collection); + + if (!collection.equals(docId) && + !collection.equals(insertCollectionName) && + !collection.equals(temporalCollectionName)) { + assertFalse("Collection not what is expected: " + collection, true); + } + } + + assertTrue("Document permissions difference in size value", + actualPermissions.contains("size:3")); + + assertTrue("Document permissions difference in rest-reader permission", + actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", + actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue("Document permissions difference in app-user permission", + (actualPermissions.contains("app-user:[") && actualPermissions.contains("READ") && + actualPermissions.contains("UPDATE") && actualPermissions.contains("EXECUTE"))); + + // BUG: Returns a 0, when it should be 11 + // assertEquals(quality, 11); + + // Properties should still be associated this document + // BUG ... This should be commented out when properties are not deleted when document is deleted + // validateMetadata(metadataHandle); + } + + if (validStartDate.contains("2001-01-01T00:00:00") && validEndDate.contains("2011-12-31T23:59:59")) { + assertTrue("System start date check failed", (systemStartDate.contains("2010-01-01T00:00:01"))); + assertTrue("System start date check failed", (systemEndDate.contains("2011-01-01T00:00:01"))); + Iterator resCollections = metadataHandle.getCollections().iterator(); + while (resCollections.hasNext()) { + String collection = resCollections.next(); + System.out.println("Collection = " + collection); + + if (!collection.equals(docId) && + !collection.equals(insertCollectionName) && + !collection.equals(temporalCollectionName)) { + assertFalse("Collection not what is expected: " + collection, true); + } + } + + assertTrue("Properties should be empty", metadataHandle.getProperties().isEmpty()); + + assertTrue("Document permissions difference in size value", + actualPermissions.contains("size:3")); + + assertTrue("Document permissions difference in rest-reader permission", + actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", + actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue("Document permissions difference in app-user permission", + (actualPermissions.contains("app-user:[") && actualPermissions.contains("READ") && + actualPermissions.contains("UPDATE") && actualPermissions.contains("EXECUTE"))); + + // BUG: Returns a 0, when it should be 11 + // assertEquals(quality, 11); + } + } + } + + // Make sure there are 4 documents in total. Use string search for this + queryMgr = client.newQueryManager(); + + start = 1; + termQueryResults = docMgr.search(stringQD, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 4, termQueryResults.getTotalSize()); + } @Test - public void test() { } + public void testReadingSpecificVersionBasedOnTime() throws Exception { + + /*** + System.out.println("Inside testReadingSpecificVersionBasedOnTime"); + + String docId = "javaSingleJSONDoc.json"; + + Calendar firstInsertTime = DatatypeConverter.parseDateTime("2010-01-01T00:00:01"); + insertJSONSingleDocument(docId, null, firstInsertTime); + + // Verify that the document was inserted + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + JacksonDatabindHandle recordHandle = new JacksonDatabindHandle(ObjectNode.class); + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); + docMgr.read (docId, metadataHandle, recordHandle, null, null, temporalCollectionName, firstInsertTime); + DocumentPage readResults = docMgr.read(docId); + + System.out.println("Number of results = " + readResults.size()); + assertEquals("Wrong number of results", 1, readResults.size()); // 2, because includes content and metadata + + DocumentRecord record = readResults.next(); + System.out.println("URI after insert = " + record.getUri()); + assertEquals("Document uri wrong after insert", docId, record.getUri()); + + //============================================================================= + // Check update works + //============================================================================= + Calendar updateTime = DatatypeConverter.parseDateTime("2011-01-01T00:00:01"); + updateJSONSingleDocument(docId, null, updateTime); + + // Verify that the document was updated + // Make sure there is 1 document in latest collection + QueryManager queryMgr = client.newQueryManager(); + StructuredQueryBuilder sqb = queryMgr.newStructuredQueryBuilder(); + StructuredQueryDefinition termQuery = sqb.collection(latestCollectionName); + long start = 1; + DocumentPage termQueryResults = docMgr.search(termQuery, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 1, termQueryResults.getTotalSize()); + + // Document URIs in latest collection must be the same as the one as the original document + while (termQueryResults.hasNext()) { + record = termQueryResults.next(); + + String uri = record.getUri(); + System.out.println("URI = " + uri); + + if (!uri.equals(docId)) { + assertFalse("URIs are not what is expected", true); + } + } + + // Make sure there are 4 documents in jsonDocId collection + queryMgr = client.newQueryManager(); + sqb = queryMgr.newStructuredQueryBuilder(); + termQuery = sqb.collection(docId); + + start = 1; + termQueryResults = docMgr.search(termQuery, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 4, termQueryResults.getTotalSize()); + + // Make sure there are 4 documents in temporal collection + queryMgr = client.newQueryManager(); + sqb = queryMgr.newStructuredQueryBuilder(); + termQuery = sqb.collection(temporalCollectionName); + + start = 1; + termQueryResults = docMgr.search(termQuery, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 4, termQueryResults.getTotalSize()); + + // Make sure there are 4 documents in total. Use string search for this + queryMgr = client.newQueryManager(); + StringQueryDefinition stringQD = queryMgr.newStringDefinition(); + stringQD.setCriteria(""); + + start = 1; + docMgr.setMetadataCategories(Metadata.ALL); + termQueryResults = docMgr.search(stringQD, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 4, termQueryResults.getTotalSize()); + + while (termQueryResults.hasNext()) { + record = termQueryResults.next(); + System.out.println("URI = " + record.getUri()); + + metadataHandle = new DocumentMetadataHandle(); + record.getMetadata(metadataHandle); + + if (record.getFormat() != Format.JSON) { + assertFalse("Format is not JSON: " + Format.JSON, true); + } + else { + // Make sure that system and valid times are what is expected + recordHandle = new JacksonDatabindHandle(ObjectNode.class); + record.getContent (recordHandle); + System.out.println("Content = " + recordHandle.toString()); + + JsonFactory factory = new JsonFactory(); + ObjectMapper mapper = new ObjectMapper(factory); + TypeReference> typeRef + = new TypeReference>() {}; + + HashMap docObject = mapper.readValue(recordHandle.toString(), typeRef); + + @SuppressWarnings("unchecked") + HashMap systemNode = (HashMap)(docObject.get(systemNodeName)); + + String systemStartDate = (String)systemNode.get(systemStartERIName); + String systemEndDate = (String)systemNode.get(systemEndERIName); + System.out.println("systemStartDate = " + systemStartDate); + System.out.println("systemEndDate = " + systemEndDate); + + @SuppressWarnings("unchecked") + HashMap validNode = (HashMap)(docObject.get(validNodeName)); + + String validStartDate = (String)validNode.get(validStartERIName); + String validEndDate = (String)validNode.get(validEndERIName); + System.out.println("validStartDate = " + validStartDate); + System.out.println("validEndDate = " + validEndDate); + + // Permissions + DocumentPermissions permissions = metadataHandle.getPermissions(); + System.out.println("Permissions: " + permissions); + + String actualPermissions = getDocumentPermissionsString(permissions); + System.out.println("actualPermissions: " + actualPermissions); + + int quality = metadataHandle.getQuality(); + System.out.println("Quality: " + quality); + + if (validStartDate.contains("2003-01-01T00:00:00") && validEndDate.contains("2008-12-31T23:59:59")) { + assertTrue("System start date check failed", (systemStartDate.contains("2011-01-01T00:00:01"))); + assertTrue("System start date check failed", (systemEndDate.contains("9999-12-31T23:59:59"))); + + Iterator resCollections = metadataHandle.getCollections().iterator(); + while (resCollections.hasNext()) { + String collection = resCollections.next(); + System.out.println("Collection = " + collection); + + if (!collection.equals(docId) && + !collection.equals(updateCollectionName) && + !collection.equals(temporalCollectionName)) { + assertFalse("Collection not what is expected: " + collection, true); + } + } + + assertTrue("Properties should be empty", metadataHandle.getProperties().isEmpty()); + + assertTrue("Document permissions difference in size value", + actualPermissions.contains("size:3")); + + assertTrue("Document permissions difference in rest-reader permission", + actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", + actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue("Document permissions difference in app-user permission", + (actualPermissions.contains("app-user:[") && actualPermissions.contains("READ") && + actualPermissions.contains("UPDATE"))); + assertFalse("Document permissions difference in app-user permission", actualPermissions.contains("EXECUTE")); + + assertEquals(quality, 99); + } + + if (validStartDate.contains("2001-01-01T00:00:00") && validEndDate.contains("2003-01-01T00:00:00")) { + assertTrue("System start date check failed", (systemStartDate.contains("2011-01-01T00:00:01"))); + assertTrue("System start date check failed", (systemEndDate.contains("9999-12-31T23:59:59"))); + + Iterator resCollections = metadataHandle.getCollections().iterator(); + while (resCollections.hasNext()) { + String collection = resCollections.next(); + System.out.println("Collection = " + collection); + + if (!collection.equals(docId) && + !collection.equals(insertCollectionName) && + !collection.equals(temporalCollectionName)) { + assertFalse("Collection not what is expected: " + collection, true); + } + } + + assertTrue("Properties should be empty", metadataHandle.getProperties().isEmpty()); + + assertTrue("Document permissions difference in size value", + actualPermissions.contains("size:3")); + + assertTrue("Document permissions difference in rest-reader permission", + actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", + actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue("Document permissions difference in app-user permission", + (actualPermissions.contains("app-user:[") && actualPermissions.contains("READ") && + actualPermissions.contains("UPDATE") && actualPermissions.contains("EXECUTE"))); + + // BUG: Returns a 99, when it should be 11 + // assertEquals(quality, 11); + } + + if (validStartDate.contains("2008-12-31T23:59:59") && validEndDate.contains("2011-12-31T23:59:59")) { + // This is the latest document + assertTrue("System start date check failed", (systemStartDate.contains("2011-01-01T00:00:01"))); + assertTrue("System start date check failed", (systemEndDate.contains("9999-12-31T23:59:59"))); + assertTrue("URI should be the doc uri ", record.getUri().equals(docId)); + + Iterator resCollections = metadataHandle.getCollections().iterator(); + while (resCollections.hasNext()) { + String collection = resCollections.next(); + System.out.println("Collection = " + collection); + + if (!collection.equals(docId) && + !collection.equals(insertCollectionName) && + !collection.equals(temporalCollectionName) && + !collection.equals(latestCollectionName)) { + assertFalse("Collection not what is expected: " + collection, true); + } + } + + + assertTrue("Document permissions difference in size value", + actualPermissions.contains("size:3")); + + assertTrue("Document permissions difference in rest-reader permission", + actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", + actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue("Document permissions difference in app-user permission", + (actualPermissions.contains("app-user:[") && actualPermissions.contains("READ") && + actualPermissions.contains("UPDATE") && actualPermissions.contains("EXECUTE"))); + + // BUG: Returns a 99, when it should be 11 + // assertEquals(quality, 11); + + validateMetadata(metadataHandle); + } + + if (validStartDate.contains("2001-01-01T00:00:00") && validEndDate.contains("2011-12-31T23:59:59")) { + assertTrue("System start date check failed", (systemStartDate.contains("2010-01-01T00:00:01"))); + assertTrue("System start date check failed", (systemEndDate.contains("2011-01-01T00:00:01"))); + + Iterator resCollections = metadataHandle.getCollections().iterator(); + while (resCollections.hasNext()) { + String collection = resCollections.next(); + System.out.println("Collection = " + collection); + + if (!collection.equals(docId) && + !collection.equals(insertCollectionName) && + !collection.equals(temporalCollectionName)) { + assertFalse("Collection not what is expected: " + collection, true); + } + } + + assertTrue("Properties should be empty", metadataHandle.getProperties().isEmpty()); + + assertTrue("Document permissions difference in size value", + actualPermissions.contains("size:3")); + + assertTrue("Document permissions difference in rest-reader permission", + actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", + actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue("Document permissions difference in app-user permission", + (actualPermissions.contains("app-user:[") && actualPermissions.contains("READ") && + actualPermissions.contains("UPDATE") && actualPermissions.contains("EXECUTE"))); + + // BUG: Returns a 99, when it should be 11 + // assertEquals(quality, 11); + } + } + } + + //============================================================================= + // Check delete works + //============================================================================= + // Delete one of the document + Calendar deleteTime = DatatypeConverter.parseDateTime("2012-01-01T00:00:01"); + deleteJSONSingleDocument(docId, null, deleteTime); + + // Make sure there are still 4 documents in docId collection + queryMgr = client.newQueryManager(); + sqb = queryMgr.newStructuredQueryBuilder(); + termQuery = sqb.collection(docId); + + start = 1; + termQueryResults = docMgr.search(termQuery, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 4, termQueryResults.getTotalSize()); + + // Make sure there is one document with docId uri + docMgr = client.newJSONDocumentManager(); + readResults = docMgr.read(docId); + + System.out.println("Number of results = " + readResults.size()); + assertEquals("Wrong number of results", 1, readResults.size()); + + // Make sure there are no documents in latest collection + queryMgr = client.newQueryManager(); + sqb = queryMgr.newStructuredQueryBuilder(); + termQuery = sqb.collection(latestCollectionName); + + start = 1; + termQueryResults = docMgr.search(termQuery, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 0, termQueryResults.getTotalSize()); + + // Make sure there are 4 documents in temporal collection + queryMgr = client.newQueryManager(); + sqb = queryMgr.newStructuredQueryBuilder(); + termQuery = sqb.collection(temporalCollectionName); + + start = 1; + docMgr.setMetadataCategories(Metadata.ALL); + termQueryResults = docMgr.search(termQuery, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 4, termQueryResults.getTotalSize()); + + while (termQueryResults.hasNext()) { + record = termQueryResults.next(); + System.out.println("URI = " + record.getUri()); + + metadataHandle = new DocumentMetadataHandle(); + record.getMetadata(metadataHandle); + + if (record.getFormat() != Format.JSON) { + assertFalse("Format is not JSON: " + Format.JSON, true); + } + else { + // Make sure that system and valid times are what is expected + recordHandle = new JacksonDatabindHandle(ObjectNode.class); + record.getContent (recordHandle); + System.out.println("Content = " + recordHandle.toString()); + + JsonFactory factory = new JsonFactory(); + ObjectMapper mapper = new ObjectMapper(factory); + TypeReference> typeRef + = new TypeReference>() {}; + + HashMap docObject = mapper.readValue(recordHandle.toString(), typeRef); + + @SuppressWarnings("unchecked") + HashMap systemNode = (HashMap)(docObject.get(systemNodeName)); + + String systemStartDate = (String)systemNode.get(systemStartERIName); + String systemEndDate = (String)systemNode.get(systemEndERIName); + System.out.println("systemStartDate = " + systemStartDate); + System.out.println("systemEndDate = " + systemEndDate); + + @SuppressWarnings("unchecked") + HashMap validNode = (HashMap)(docObject.get(validNodeName)); + + String validStartDate = (String)validNode.get(validStartERIName); + String validEndDate = (String)validNode.get(validEndERIName); + System.out.println("validStartDate = " + validStartDate); + System.out.println("validEndDate = " + validEndDate); + + // Permissions + DocumentPermissions permissions = metadataHandle.getPermissions(); + System.out.println("Permissions: " + permissions); + + String actualPermissions = getDocumentPermissionsString(permissions); + System.out.println("actualPermissions: " + actualPermissions); + + int quality = metadataHandle.getQuality(); + System.out.println("Quality: " + quality); + + if (validStartDate.contains("2003-01-01T00:00:00") && validEndDate.contains("2008-12-31T23:59:59")) { + assertTrue("System start date check failed", (systemStartDate.contains("2011-01-01T00:00:01"))); + assertTrue("System start date check failed", (systemEndDate.contains("2012-01-01T00:00:01"))); + + Iterator resCollections = metadataHandle.getCollections().iterator(); + while (resCollections.hasNext()) { + String collection = resCollections.next(); + System.out.println("Collection = " + collection); + + if (!collection.equals(docId) && + !collection.equals(updateCollectionName) && + !collection.equals(temporalCollectionName)) { + assertFalse("Collection not what is expected: " + collection, true); + } + } + + assertTrue("Properties should be empty", metadataHandle.getProperties().isEmpty()); + + assertTrue("Document permissions difference in size value", + actualPermissions.contains("size:3")); + + assertTrue("Document permissions difference in rest-reader permission", + actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", + actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue("Document permissions difference in app-user permission", + (actualPermissions.contains("app-user:[") && actualPermissions.contains("READ") && + actualPermissions.contains("UPDATE"))); + assertFalse("Document permissions difference in app-user permission", actualPermissions.contains("EXECUTE")); + + // BUG: Returns a 0, when it should be 99 + // assertEquals(quality, 99); + } + + if (validStartDate.contains("2001-01-01T00:00:00") && validEndDate.contains("2003-01-01T00:00:00")) { + assertTrue("System start date check failed", (systemStartDate.contains("2011-01-01T00:00:01"))); + assertTrue("System start date check failed", (systemEndDate.contains("2012-01-01T00:00:01"))); + + Iterator resCollections = metadataHandle.getCollections().iterator(); + while (resCollections.hasNext()) { + String collection = resCollections.next(); + System.out.println("Collection = " + collection); + + if (!collection.equals(docId) && + !collection.equals(insertCollectionName) && + !collection.equals(temporalCollectionName)) { + assertFalse("Collection not what is expected: " + collection, true); + } + } + + assertTrue("Properties should be empty", metadataHandle.getProperties().isEmpty()); + + assertTrue("Document permissions difference in size value", + actualPermissions.contains("size:3")); + + assertTrue("Document permissions difference in rest-reader permission", + actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", + actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue("Document permissions difference in app-user permission", + (actualPermissions.contains("app-user:[") && actualPermissions.contains("READ") && + actualPermissions.contains("UPDATE") && actualPermissions.contains("EXECUTE"))); + + // BUG: Returns a 0, when it should be 11 + // assertEquals(quality, 11); + } + + if (validStartDate.contains("2008-12-31T23:59:59") && validEndDate.contains("2011-12-31T23:59:59")) { + assertTrue("System start date check failed", (systemStartDate.contains("2011-01-01T00:00:01"))); + assertTrue("System start date check failed", (systemEndDate.contains("2012-01-01T00:00:01"))); + + assertTrue("URI should be the doc uri ", record.getUri().equals(docId)); + + // Document should not be in latest collection + Iterator resCollections = metadataHandle.getCollections().iterator(); + while (resCollections.hasNext()) { + String collection = resCollections.next(); + System.out.println("Collection = " + collection); + + if (!collection.equals(docId) && + !collection.equals(insertCollectionName) && + !collection.equals(temporalCollectionName)) { + assertFalse("Collection not what is expected: " + collection, true); + } + } + + assertTrue("Document permissions difference in size value", + actualPermissions.contains("size:3")); + + assertTrue("Document permissions difference in rest-reader permission", + actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", + actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue("Document permissions difference in app-user permission", + (actualPermissions.contains("app-user:[") && actualPermissions.contains("READ") && + actualPermissions.contains("UPDATE") && actualPermissions.contains("EXECUTE"))); + + // BUG: Returns a 0, when it should be 11 + // assertEquals(quality, 11); + + // Properties should still be associated this document + // BUG ... This should be commented out when properties are not deleted when document is deleted + // validateMetadata(metadataHandle); + } + + if (validStartDate.contains("2001-01-01T00:00:00") && validEndDate.contains("2011-12-31T23:59:59")) { + assertTrue("System start date check failed", (systemStartDate.contains("2010-01-01T00:00:01"))); + assertTrue("System start date check failed", (systemEndDate.contains("2011-01-01T00:00:01"))); + Iterator resCollections = metadataHandle.getCollections().iterator(); + while (resCollections.hasNext()) { + String collection = resCollections.next(); + System.out.println("Collection = " + collection); + + if (!collection.equals(docId) && + !collection.equals(insertCollectionName) && + !collection.equals(temporalCollectionName)) { + assertFalse("Collection not what is expected: " + collection, true); + } + } + + assertTrue("Properties should be empty", metadataHandle.getProperties().isEmpty()); + + assertTrue("Document permissions difference in size value", + actualPermissions.contains("size:3")); + + assertTrue("Document permissions difference in rest-reader permission", + actualPermissions.contains("rest-reader:[READ]")); + assertTrue("Document permissions difference in rest-writer permission", + actualPermissions.contains("rest-writer:[UPDATE]")); + assertTrue("Document permissions difference in app-user permission", + (actualPermissions.contains("app-user:[") && actualPermissions.contains("READ") && + actualPermissions.contains("UPDATE") && actualPermissions.contains("EXECUTE"))); + + // BUG: Returns a 0, when it should be 11 + // assertEquals(quality, 11); + } + } + } + + // Make sure there are 4 documents in total. Use string search for this + queryMgr = client.newQueryManager(); + + start = 1; + termQueryResults = docMgr.search(stringQD, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 4, termQueryResults.getTotalSize()); + ***/ + } + + + + + + + + + + + + + + + + + + + @Test + public void testSystemTimeUsingInvalidTime() throws Exception { + + System.out.println("Inside testSystemTime"); + + String docId = "javaSingleJSONDoc.json"; + + Calendar firstInsertTime = DatatypeConverter.parseDateTime("2010-01-01T00:00:01"); + insertJSONSingleDocument(docId, null, firstInsertTime); + + // Update by passing a system time that is less than previous one + Calendar updateTime = DatatypeConverter.parseDateTime("2010-01-01T00:00:00"); + + boolean exceptionThrown = false; + try { + updateJSONSingleDocument(docId, null, updateTime); + } + catch (com.marklogic.client.FailedRequestException ex) { + System.out.println(ex.getMessage()); + + assert(ex.getFailedRequest().getMessageCode().equals("TEMPORAL-SYSTEMTIME-BACKWARDS")); + assert(ex.getFailedRequest().getStatusCode() == 400); + + exceptionThrown = true; + } + + assertTrue("Exception not thrown during invalid update of system time", exceptionThrown); + + // Delete by passing invalid time + Calendar deleteTime = DatatypeConverter.parseDateTime("2010-01-01T00:00:00"); + + exceptionThrown = false; + try { + deleteJSONSingleDocument(docId, null, deleteTime); + } + catch (com.marklogic.client.FailedRequestException ex) { + System.out.println(ex.getMessage()); + + assert(ex.getFailedRequest().getMessageCode().equals("TEMPORAL-SYSTEMTIME-BACKWARDS")); + assert(ex.getFailedRequest().getStatusCode() == 400); + + exceptionThrown = true; + } + } + + @Test + public void testTransactionCommit() throws Exception { + + System.out.println("Inside testTransactionCommit"); + + String docId = "javaSingleJSONDoc.json"; + + Transaction transaction = client.openTransaction("Transaction for BiTemporal"); + try { + insertJSONSingleDocument(docId, transaction); + } + catch (Exception ex) { + transaction.rollback(); + + assertTrue("insertJSONSingleDocument failed in testTransactionCommit", false); + } + + // Verify that the document was inserted + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + DocumentPage readResults = docMgr.read(transaction, docId); + + System.out.println("Number of results = " + readResults.size()); + if (readResults.size() != 1) { + transaction.rollback(); + + assertEquals("Wrong number of results", 1, readResults.size()); + } + + DocumentRecord latestDoc = readResults.next(); + System.out.println("URI after insert = " + latestDoc.getUri()); + + if (!docId.equals(latestDoc.getUri())) { + transaction.rollback(); + + assertEquals("Document uri wrong after insert", docId, latestDoc.getUri()); + } + + try { + updateJSONSingleDocument(docId, transaction); + } + catch (Exception ex) { + transaction.rollback(); + + assertTrue("updateJSONSingleDocument failed in testTransactionCommit", false); + } + + QueryManager queryMgr = client.newQueryManager(); + StructuredQueryBuilder sqb = queryMgr.newStructuredQueryBuilder(); + StructuredQueryDefinition termQuery = sqb.collection(latestCollectionName); + + long start = 1; + DocumentPage termQueryResults = docMgr.search(termQuery, start, transaction); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + + if ( termQueryResults.getTotalSize() != 1) { + transaction.rollback(); + + assertEquals("Wrong number of results", 1, termQueryResults.getTotalSize()); + } + + // There should be 1 document in docId collection + queryMgr = client.newQueryManager(); + sqb = queryMgr.newStructuredQueryBuilder(); + termQuery = sqb.collection(docId); + + start = 1; + termQueryResults = docMgr.search(termQuery, start, transaction); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + + if (termQueryResults.getTotalSize() != 4) { + transaction.rollback(); + + assertEquals("Wrong number of results", 4, termQueryResults.getTotalSize()); + } + + try { + deleteJSONSingleDocument(docId, transaction); + } + catch (Exception ex) { + transaction.rollback(); + + assertTrue("deleteJSONSingleDocument failed in testTransactionCommit", false); + } + + // There should be no documents in latest collection + queryMgr = client.newQueryManager(); + sqb = queryMgr.newStructuredQueryBuilder(); + termQuery = sqb.collection(latestCollectionName); + + start = 1; + termQueryResults = docMgr.search(termQuery, start, transaction); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + + if ( termQueryResults.getTotalSize() != 0) { + transaction.rollback(); + + assertEquals("Wrong number of results", 0, termQueryResults.getTotalSize()); + } + + transaction.commit(); + + // There should still be no documents in latest collection + queryMgr = client.newQueryManager(); + sqb = queryMgr.newStructuredQueryBuilder(); + termQuery = sqb.collection(latestCollectionName); + + start = 1; + termQueryResults = docMgr.search(termQuery, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + assertEquals("Wrong number of results", 0, termQueryResults.getTotalSize()); + } + + @Test + public void testTransactionRollback() throws Exception { + + System.out.println("Inside testTransaction"); + + String docId = "javaSingleJSONDoc.json"; + + Transaction transaction = client.openTransaction("Transaction for BiTemporal"); + + try { + insertJSONSingleDocument(docId, transaction); + } + catch (Exception ex) { + transaction.rollback(); + + assertTrue("insertJSONSingleDocument failed in testTransactionRollback", false); + } + + // Verify that the document was inserted + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + DocumentPage readResults = docMgr.read(transaction, docId); + + System.out.println("Number of results = " + readResults.size()); + if (readResults.size() != 1) { + transaction.rollback(); + + assertEquals("Wrong number of results", 1, readResults.size()); + } + + DocumentRecord latestDoc = readResults.next(); + System.out.println("URI after insert = " + latestDoc.getUri()); + if (!docId.equals(latestDoc.getUri())) { + transaction.rollback(); + + assertEquals("Document uri wrong after insert", docId, latestDoc.getUri()); + } + + try { + updateJSONSingleDocument(docId, transaction); + } + catch (Exception ex) { + transaction.rollback(); + + assertTrue("updateJSONSingleDocument failed in testTransactionRollback", false); + } + + // Verify that the document is visible and count is 4 + // Fetch documents associated with a search term (such as XML) in Address element + QueryManager queryMgr = client.newQueryManager(); + StructuredQueryBuilder sqb = queryMgr.newStructuredQueryBuilder(); + + StructuredQueryDefinition termQuery = sqb.collection(docId); + + long start = 1; + DocumentPage termQueryResults = docMgr.search(termQuery, start, transaction); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + if (termQueryResults.getTotalSize() != 4) { + transaction.rollback(); + + assertEquals("Wrong number of results", 4, termQueryResults.getTotalSize()); + } + + transaction.rollback(); + + // Verify that the document is not there after rollback + readResults = docMgr.read(docId); + + System.out.println("Number of results = " + readResults.size()); + assertEquals("Wrong number of results", 0, readResults.size()); + + //======================================================================= + // Now try rollback with delete + System.out.println("Test Rollback after delete"); + docId = "javaSingleJSONDocForDelete.json"; + + transaction = client.openTransaction("Transaction Rollback for BiTemporal Delete"); + + try { + insertJSONSingleDocument(docId, transaction); + } + catch (Exception ex) { + transaction.rollback(); + + assertTrue("insertJSONSingleDocument failed in testTransactionRollback", false); + } + + // Verify that the document was inserted + docMgr = client.newJSONDocumentManager(); + readResults = docMgr.read(transaction, docId); + + System.out.println("Number of results = " + readResults.size()); + if (readResults.size() != 1) { + transaction.rollback(); + + assertEquals("Wrong number of results", 1, readResults.size()); + } + + latestDoc = readResults.next(); + System.out.println("URI after insert = " + latestDoc.getUri()); + if (!docId.equals(latestDoc.getUri())) { + transaction.rollback(); + + assertEquals("Document uri wrong after insert", docId, latestDoc.getUri()); + } + + try { + deleteJSONSingleDocument(docId, transaction); + } + catch (Exception ex) { + transaction.rollback(); + + assertTrue("deleteJSONSingleDocument failed in testTransactionRollback", false); + } + + // Verify that the document is visible and count is 1 + // Fetch documents associated with a search term (such as XML) in Address element + queryMgr = client.newQueryManager(); + sqb = queryMgr.newStructuredQueryBuilder(); + + termQuery = sqb.collection(docId); + + start = 1; + termQueryResults = docMgr.search(termQuery, start, transaction); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + if (termQueryResults.getTotalSize() != 1) { + transaction.rollback(); + + assertEquals("Wrong number of results", 1, termQueryResults.getTotalSize()); + } + + transaction.rollback(); + + // Verify that the document was rolled back and count is 0 + readResults = docMgr.read(docId); + + System.out.println("Number of results = " + readResults.size()); + assertEquals("Wrong number of results", 0, readResults.size()); + + System.out.println("Done"); + } + + @Test + public void testPeriodRangeQuerySingleAxisBasedOnALNContains() throws Exception { + // Read documents based on document URI and ALN Contains. We are just looking for count of documents to be correct + + String docId = "javaSingleJSONDoc.json"; + insertJSONSingleDocument(docId, null); + updateJSONSingleDocument(docId, null); + + // Fetch documents associated with a search term (such as XML) in Address element + QueryManager queryMgr = client.newQueryManager(); + StructuredQueryBuilder sqb = queryMgr.newStructuredQueryBuilder(); + + StructuredQueryDefinition termQuery = sqb.collection(docId); + + StructuredQueryBuilder.Axis validAxis = sqb.axis(axisValidName); + Calendar start1 = DatatypeConverter.parseDateTime("2001-01-01T00:00:01"); + Calendar end1 = DatatypeConverter.parseDateTime("2011-12-31T23:59:58"); + StructuredQueryBuilder.Period period1 = sqb.period(start1, end1); + StructuredQueryDefinition periodQuery = sqb.and(termQuery, + sqb.temporalPeriodRange(validAxis, TemporalOperator.ALN_CONTAINS, period1)); + + long start = 1; + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); // Get all metadata + DocumentPage termQueryResults = docMgr.search(periodQuery, start); + + long count = 0; + while (termQueryResults.hasNext()) { + ++count; + DocumentRecord record = termQueryResults.next(); + System.out.println("URI = " + record.getUri()); + + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); + record.getMetadata(metadataHandle); + Iterator resCollections = metadataHandle.getCollections().iterator(); + while (resCollections.hasNext()) { + System.out.println("Collection = " + resCollections.next()); + } + + if (record.getFormat() == Format.XML) { + DOMHandle recordHandle = new DOMHandle(); + record.getContent (recordHandle); + System.out.println("Content = " + recordHandle.toString()); + } + else { + JacksonDatabindHandle recordHandle = new JacksonDatabindHandle(ObjectNode.class); + record.getContent (recordHandle); + System.out.println("Content = " + recordHandle.toString()); + + JsonFactory factory = new JsonFactory(); + ObjectMapper mapper = new ObjectMapper(factory); + TypeReference> typeRef + = new TypeReference>() {}; + + HashMap docObject = mapper.readValue(recordHandle.toString(), typeRef); + + @SuppressWarnings("unchecked") + HashMap systemNode = (HashMap)(docObject.get(systemNodeName)); + + String systemStartDate = (String)systemNode.get(systemStartERIName); + String systemEndDate = (String)systemNode.get(systemEndERIName); + System.out.println("systemStartDate = " + systemStartDate); + System.out.println("systemEndDate = " + systemEndDate); + + @SuppressWarnings("unchecked") + HashMap validNode = (HashMap)(docObject.get(validNodeName)); + + String validStartDate = (String)validNode.get(validStartERIName); + String validEndDate = (String)validNode.get(validEndERIName); + System.out.println("validStartDate = " + validStartDate); + System.out.println("validEndDate = " + validEndDate); + + assertTrue("Valid start date check failed", + (validStartDate.equals("2001-01-01T00:00:00") && validEndDate.equals("2011-12-31T23:59:59"))); + } + } + + System.out.println("Number of results using SQB = " + count); + assertEquals("Wrong number of results", 1, count); + } + + @Test + public void testPeriodRangeQueryMultiplesAxesBasedOnALNContains() throws Exception { + // Read documents based on document URI and ALN_OVERLAPS. We are just looking + // for count of documents to be correct + + String docId = "javaSingleJSONDoc.json"; + insertJSONSingleDocument(docId, null); + updateJSONSingleDocument(docId, null); + + // Fetch documents associated with a search term (such as XML) in Address element + QueryManager queryMgr = client.newQueryManager(); + StructuredQueryBuilder sqb = queryMgr.newStructuredQueryBuilder(); + + StructuredQueryDefinition termQuery = sqb.collection(docId); + + StructuredQueryBuilder.Axis validAxis1 = sqb.axis(axisValidName); + Calendar start1 = DatatypeConverter.parseDateTime("2001-01-01T00:00:01"); + Calendar end1 = DatatypeConverter.parseDateTime("2011-12-31T23:59:58"); + StructuredQueryBuilder.Period period1 = sqb.period(start1, end1); + + StructuredQueryBuilder.Axis validAxis2 = sqb.axis(axisValidName); + Calendar start2 = DatatypeConverter.parseDateTime("2003-01-01T00:00:01"); + Calendar end2 = DatatypeConverter.parseDateTime("2008-12-31T23:59:58"); + StructuredQueryBuilder.Period period2 = sqb.period(start2, end2); + + StructuredQueryBuilder.Axis[] axes = new StructuredQueryBuilder.Axis[]{validAxis1, validAxis2}; + StructuredQueryBuilder.Period[] periods = new StructuredQueryBuilder.Period[]{period1, period2}; + + StructuredQueryDefinition periodQuery = sqb.and(termQuery, + sqb.temporalPeriodRange(axes, TemporalOperator.ALN_CONTAINS, periods)); + + // Note that the query will be done for every axis across every period. And the results will be an OR of + // the result of each of the query done for every axis across every period + long start = 1; + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); // Get all metadata + DocumentPage termQueryResults = docMgr.search(periodQuery, start); + + long count = 0; + while (termQueryResults.hasNext()) { + ++count; + DocumentRecord record = termQueryResults.next(); + System.out.println("URI = " + record.getUri()); + + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); + record.getMetadata(metadataHandle); + Iterator resCollections = metadataHandle.getCollections().iterator(); + while (resCollections.hasNext()) { + System.out.println("Collection = " + resCollections.next()); + } + + if (record.getFormat() != Format.JSON) { + assertFalse("Invalid document format: " + record.getFormat(), true); + } + else { + JacksonDatabindHandle recordHandle = new JacksonDatabindHandle(ObjectNode.class); + record.getContent (recordHandle); + System.out.println("Content = " + recordHandle.toString()); + + JsonFactory factory = new JsonFactory(); + ObjectMapper mapper = new ObjectMapper(factory); + TypeReference> typeRef + = new TypeReference>() {}; + + HashMap docObject = mapper.readValue(recordHandle.toString(), typeRef); + + @SuppressWarnings("unchecked") + HashMap validNode = (HashMap)(docObject.get(validNodeName)); + // HashMap validNode = (HashMap)(docObject.get(validNodeName)); + + String validStartDate = (String)validNode.get(validStartERIName); + String validEndDate = (String)validNode.get(validEndERIName); + System.out.println("validStartDate = " + validStartDate); + System.out.println("validEndDate = " + validEndDate); + + assertTrue("Valid start date check failed", (validStartDate.equals("2001-01-01T00:00:00") || validStartDate.equals("2003-01-01T00:00:00"))); + assertTrue("Valid end date check failed", (validEndDate.equals("2011-12-31T23:59:59") || validEndDate.equals("2008-12-31T23:59:59"))); + } + } + + System.out.println("Number of results using SQB = " + count); + assertEquals("Wrong number of results", 2, count); + } + + @Test + public void testPeriodCompareQuerySingleAxisBasedOnALNContains() throws Exception { + // Read documents based on document URI and ALN Contains. We are just looking for count of documents to be correct + + String docId = "javaSingleJSONDoc.json"; + + Calendar insertTime = DatatypeConverter.parseDateTime("2005-01-01T00:00:01"); + insertJSONSingleDocument(docId, null, insertTime); + + Calendar updateTime = DatatypeConverter.parseDateTime("2010-01-01T00:00:01"); + updateJSONSingleDocument(docId, null, updateTime); + + // Fetch documents associated with a search term (such as XML) in Address element + QueryManager queryMgr = client.newQueryManager(); + StructuredQueryBuilder sqb = queryMgr.newStructuredQueryBuilder(); + + StructuredQueryBuilder.Axis validAxis = sqb.axis(axisValidName); + StructuredQueryBuilder.Axis systemAxis = sqb.axis(axisSystemName); + + StructuredQueryDefinition termQuery = sqb.collection(docId); + StructuredQueryDefinition periodQuery = sqb.and(termQuery, + sqb.temporalPeriodCompare(validAxis, TemporalOperator.ALN_CONTAINS, systemAxis)); + + long start = 1; + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); // Get all metadata + DocumentPage termQueryResults = docMgr.search(periodQuery, start); + + long count = 0; + while (termQueryResults.hasNext()) { + ++count; + DocumentRecord record = termQueryResults.next(); + System.out.println("URI = " + record.getUri()); + + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); + record.getMetadata(metadataHandle); + Iterator resCollections = metadataHandle.getCollections().iterator(); + while (resCollections.hasNext()) { + System.out.println("Collection = " + resCollections.next()); + } + + if (record.getFormat() == Format.XML) { + DOMHandle recordHandle = new DOMHandle(); + record.getContent (recordHandle); + System.out.println("Content = " + recordHandle.toString()); + } + else { + JacksonDatabindHandle recordHandle = new JacksonDatabindHandle(ObjectNode.class); + record.getContent (recordHandle); + System.out.println("Content = " + recordHandle.toString()); + + JsonFactory factory = new JsonFactory(); + ObjectMapper mapper = new ObjectMapper(factory); + TypeReference> typeRef + = new TypeReference>() {}; + + HashMap docObject = mapper.readValue(recordHandle.toString(), typeRef); + + @SuppressWarnings("unchecked") + HashMap systemNode = (HashMap)(docObject.get(systemNodeName)); + + String systemStartDate = (String)systemNode.get(systemStartERIName); + String systemEndDate = (String)systemNode.get(systemEndERIName); + System.out.println("systemStartDate = " + systemStartDate); + System.out.println("systemEndDate = " + systemEndDate); + + @SuppressWarnings("unchecked") + HashMap validNode = (HashMap)(docObject.get(validNodeName)); + + String validStartDate = (String)validNode.get(validStartERIName); + String validEndDate = (String)validNode.get(validEndERIName); + System.out.println("validStartDate = " + validStartDate); + System.out.println("validEndDate = " + validEndDate); + + assertTrue("Valid start date check failed", + (validStartDate.contains("2001-01-01T00:00:00") && validEndDate.contains("2011-12-31T23:59:59"))); + + assertTrue("System start date check failed", + (systemStartDate.contains("2005-01-01T00:00:01") && !systemEndDate.contains("2010-01-01T00:00:01"))); + + } + } + + System.out.println("Number of results using SQB = " + count); + assertEquals("Wrong number of results", 1, count); + } + + @Test + public void testLsqtQuery() throws Exception { + // Read documents based on document URI and ALN Contains. We are just looking for count of documents to be correct + String docId = "javaSingleJSONDoc.json"; + + Calendar insertTime = DatatypeConverter.parseDateTime("2005-01-01T00:00:01"); + insertJSONSingleDocument(docId, null, insertTime); + + Calendar updateTime = DatatypeConverter.parseDateTime("2010-01-01T00:00:01"); + updateJSONSingleDocument(docId, null, updateTime); + + // Fetch documents associated with a search term (such as XML) in Address element + QueryManager queryMgr = client.newQueryManager(); + StructuredQueryBuilder sqb = queryMgr.newStructuredQueryBuilder(); + + Calendar queryTime = DatatypeConverter.parseDateTime("2015-01-01T00:00:01"); + StructuredQueryDefinition periodQuery = sqb.temporalLsqtQuery(temporalCollectionName, + queryTime, 0, new String[]{}); + + long start = 1; + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); // Get all metadata + DocumentPage termQueryResults = docMgr.search(periodQuery, start); + + long count = 0; + while (termQueryResults.hasNext()) { + ++count; + DocumentRecord record = termQueryResults.next(); + System.out.println("URI = " + record.getUri()); + + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); + record.getMetadata(metadataHandle); + Iterator resCollections = metadataHandle.getCollections().iterator(); + while (resCollections.hasNext()) { + System.out.println("Collection = " + resCollections.next()); + } + + if (record.getFormat() == Format.XML) { + DOMHandle recordHandle = new DOMHandle(); + record.getContent (recordHandle); + System.out.println("Content = " + recordHandle.toString()); + } + else { + JacksonDatabindHandle recordHandle = new JacksonDatabindHandle(ObjectNode.class); + record.getContent (recordHandle); + System.out.println("Content = " + recordHandle.toString()); + + JsonFactory factory = new JsonFactory(); + ObjectMapper mapper = new ObjectMapper(factory); + TypeReference> typeRef + = new TypeReference>() {}; + + HashMap docObject = mapper.readValue(recordHandle.toString(), typeRef); + + @SuppressWarnings("unchecked") + HashMap systemNode = (HashMap)(docObject.get(systemNodeName)); + + String systemStartDate = (String)systemNode.get(systemStartERIName); + String systemEndDate = (String)systemNode.get(systemEndERIName); + System.out.println("systemStartDate = " + systemStartDate); + System.out.println("systemEndDate = " + systemEndDate); + + @SuppressWarnings("unchecked") + HashMap validNode = (HashMap)(docObject.get(validNodeName)); + + String validStartDate = (String)validNode.get(validStartERIName); + String validEndDate = (String)validNode.get(validEndERIName); + System.out.println("validStartDate = " + validStartDate); + System.out.println("validEndDate = " + validEndDate); + + // assertTrue("Valid start date check failed", + // (validStartDate.equals("2008-12-31T23:59:59") && validEndDate.equals("2011-12-31T23:59:59")) || + // (validStartDate.equals("2003-01-01T00:00:00") && validEndDate.equals("2008-12-31T23:59:59")) || + // (validStartDate.equals("2001-01-01T00:00:00") && validEndDate.equals("2003-01-01T00:00:00"))); + } + } + + System.out.println("Number of results using SQB = " + count); + assertEquals("Wrong number of results", 3, count); + } + + @Test + // Negative test + public void testInsertJSONDocumentUsingXMLExtension() throws Exception { + // Now insert a JSON document + String jsonDocId = "javaSingleJSONDoc.xml"; + + boolean exceptionThrown = false; + try { + insertJSONSingleDocument(jsonDocId, null); + } + catch (com.marklogic.client.FailedRequestException ex) { + exceptionThrown = true; + System.out.println(ex.getFailedRequest().getStatusCode()); + System.out.println(ex.getFailedRequest().getMessageCode()); + + // BUG: Right now this returns 500 error. Bug is open to fix this + // assert(ex.getFailedRequest().getMessageCode().equals("TEMPORAL-SYSTEMTIME-BACKWARDS")); + // assert(ex.getFailedRequest().getStatusCode() == 400); + } + + assertTrue("Exception not thrown for invalid transform", exceptionThrown); + } + + @Test + // Negative test + public void testInsertJSONDocumentUsingNonExistingTemporalCollection() throws Exception { + // Now insert a JSON document + String jsonDocId = "javaSingleJSONDoc.json"; + + boolean exceptionThrown = false; + + System.out.println("Inside testInsertJSONDocumentUsingNonExistingTemporalCollection"); + + JacksonDatabindHandle handle = getJSONDocumentHandle( + "2001-01-01T00:00:00", "2011-12-31T23:59:59", "999 Skyway Park - JSON", jsonDocId); + + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); + + // put meta-data + DocumentMetadataHandle mh = setMetadata(false); + + try { + docMgr.write(jsonDocId, mh, handle, null, null, "invalidCollection", null); + } + catch (com.marklogic.client.FailedRequestException ex) { + exceptionThrown = true; + System.out.println(ex.getFailedRequest().getStatusCode()); + System.out.println(ex.getFailedRequest().getMessageCode()); + + // BUG: Right now this returns 500 error. Bug is open to fix this + // assert(ex.getFailedRequest().getMessageCode().equals("TEMPORAL-SYSTEMTIME-BACKWARDS")); + // assert(ex.getFailedRequest().getStatusCode() == 400); + } + + assertTrue("Exception not thrown for invalid temporal collection", exceptionThrown); + } + + @Test + // Negative test + public void testDocumentUsingCollectionNamedLatest() throws Exception { + // Now insert a JSON document + String jsonDocId = "javaSingleJSONDoc.json"; + + boolean exceptionThrown = false; + + System.out.println("Inside testDocumentUsingCollectionNamedLatest"); + + JacksonDatabindHandle handle = getJSONDocumentHandle( + "2001-01-01T00:00:00", "2011-12-31T23:59:59", "999 Skyway Park - JSON", jsonDocId); + + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); + + // put meta-data + DocumentMetadataHandle mh = setMetadata(false); + + try { + docMgr.write(jsonDocId, mh, handle, null, null, latestCollectionName, null); + } + catch (com.marklogic.client.FailedRequestException ex) { + exceptionThrown = true; + System.out.println(ex.getFailedRequest().getStatusCode()); + System.out.println(ex.getFailedRequest().getMessageCode()); + + // BUG: Right now this returns 500 error. Bug is open to fix this + // assert(ex.getFailedRequest().getMessageCode().equals("TEMPORAL-SYSTEMTIME-BACKWARDS")); + // assert(ex.getFailedRequest().getStatusCode() == 400); + } + + assertTrue("Exception not thrown for invalid temporal collection", exceptionThrown); + } + + + @Test + // Negative test .. check if this is needed + public void testInsertDocumentUsingDocumentURIAsCollectionName() throws Exception { + // Now insert a JSON document + String jsonDocId = "javaSingleJSONDoc.json"; + + System.out.println("Inside testInserDocumentUsingDocumentURIAsCollectionName"); + + // First Create collection a collection with same name as doci URI + ConnectedRESTQA.addElementRangeIndexTemporalCollection(dbName, jsonDocId, axisSystemName, axisValidName); + + // Insert a document called as insertJSONSingleDocument + insertJSONSingleDocument(jsonDocId, null); + + JacksonDatabindHandle handle = getJSONDocumentHandle( + "2001-01-01T00:00:00", "2011-12-31T23:59:59", "999 Skyway Park - JSON", jsonDocId); + + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); + + // put meta-data + DocumentMetadataHandle mh = setMetadata(false); + + boolean exceptionThrown = false; + try { + docMgr.write(jsonDocId, mh, handle, null, null, jsonDocId, null); + } + catch (com.marklogic.client.FailedRequestException ex) { + exceptionThrown = true; + System.out.println(ex.getFailedRequest().getStatusCode()); + System.out.println(ex.getFailedRequest().getMessageCode()); + + // BUG: Right now this returns 500 error. Bug is open to fix this + // assert(ex.getFailedRequest().getMessageCode().equals("TEMPORAL-SYSTEMTIME-BACKWARDS")); + // assert(ex.getFailedRequest().getStatusCode() == 400); + } + + ConnectedRESTQA.deleteElementRangeIndexTemporalCollection("Documents", jsonDocId); + + // Looks like this should fail. But is not failing + // assertTrue("Exception not thrown for invalid temporal collection", exceptionThrown); + } + + public void bulkWrite() throws Exception { + + GenericDocumentManager docMgr = client.newDocumentManager(); + // docMgr.setMetadataCategories(Metadata.ALL); + DocumentWriteSet writeset = docMgr.newWriteSet(); + + // put meta-data + DocumentMetadataHandle mh = setMetadata(false); + writeset.addDefault(mh); + + // Setup for JSON document + /** + * + { + "System": { + systemStartERIName : "", + systemEndERIName : "", + }, + "Valid": { + validStartERIName: "2001-01-01T00:00:00", + validEndERIName: "2011-12-31T59:59:59" + }, + "Address": "999 Skyway Park", + "uri": "javaDoc1.json" + } + */ + + String docId[] = { "javaDoc1.json", "javaDoc2.xml"}; + ObjectMapper mapper = new ObjectMapper(); + ObjectNode rootNode = mapper.createObjectNode(); + + // Set system time values + ObjectNode system = mapper.createObjectNode(); + + system.put(systemStartERIName, ""); + system.put(systemEndERIName, ""); + rootNode.set(systemNodeName, system); + + // Set valid time values + ObjectNode valid = mapper.createObjectNode(); + + valid.put(validStartERIName, "2001-01-01T00:00:00"); + valid.put(validEndERIName, "2011-12-31T59:59:59"); + rootNode.set(validNodeName, valid); + + // Set Address + rootNode.put(addressNodeName, "999 Skyway Park"); + + // Set uri + rootNode.put(uriNodeName, docId[0]); + + System.out.println(rootNode.toString()); + + JacksonDatabindHandle handle1 = new JacksonDatabindHandle(ObjectNode.class).withFormat(Format.JSON); + handle1.set(rootNode); + writeset.add(docId[0], handle1); + + // Setup for XML document + /** + + + + + + + 2001-01-01T00:00:00 + 2011-12-31T59:59:59 + + 888 SKyway Park" + javaDoc2.xml + + */ + + Document domDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); + Element root = domDocument.createElement("root"); + + // System start and End time + Node systemNode = root.appendChild(domDocument.createElement("system")); + systemNode.appendChild(domDocument.createElement(systemStartERIName)); + systemNode.appendChild(domDocument.createElement(systemEndERIName)); + + // Valid start and End time + Node validNode = root.appendChild(domDocument.createElement("valid")); + + Node validStartNode = validNode.appendChild(domDocument.createElement(validStartERIName)); + validStartNode.appendChild(domDocument.createTextNode("2001-01-01T00:00:00")); + validNode.appendChild(validStartNode); + + Node validEndNode = validNode.appendChild(domDocument.createElement(validEndERIName)); + validEndNode.appendChild(domDocument.createTextNode("2011-12-31T59:59:59")); + validNode.appendChild(validEndNode); + + // Address + Node addressNode = root.appendChild(domDocument.createElement("Address")); + addressNode.appendChild(domDocument.createTextNode("888 SKyway Park")); + + // Address + Node uriNode = root.appendChild(domDocument.createElement("uri")); + uriNode.appendChild(domDocument.createTextNode(docId[1])); + domDocument.appendChild(root); + + String domString = ((DOMImplementationLS) DocumentBuilderFactory + .newInstance() + .newDocumentBuilder() + .getDOMImplementation() + ).createLSSerializer().writeToString(domDocument); + + System.out.println(domString); + + writeset.add(docId[1], new DOMHandle().with(domDocument)); + docMgr.write(writeset); + + + /*** + assertTrue("Did not return a iPhone 6", product1.getName().equalsIgnoreCase("iPhone 6")); + assertTrue("Did not return a Mobile Phone", product1.getIndustry().equalsIgnoreCase("Mobile Phone")); + assertTrue("Did not return a Mobile Phone", product1.getDescription().equalsIgnoreCase("New iPhone 6")); + + docMgr.readMetadata(docId[0], mhRead); + validateMetadata(mhRead); + + docMgr.read(docId[1],jacksonDBReadHandle); + Product product2 = (Product) jacksonDBReadHandle.get(); + assertTrue("Did not return a iMac", product2.getName().equalsIgnoreCase("iMac")); + assertTrue("Did not return a Desktop", product2.getIndustry().equalsIgnoreCase("Desktop")); + assertTrue("Did not return a Air Book OS X", product2.getDescription().equalsIgnoreCase("Air Book OS X")); + + docMgr.readMetadata(docId[1], mhRead); + validateMetadata(mhRead); + + docMgr.read(docId[2], jacksonDBReadHandle); + Product product3 = (Product) jacksonDBReadHandle.get(); + assertTrue("Did not return a iPad", product3.getName().equalsIgnoreCase("iPad")); + assertTrue("Did not return a Tablet", product3.getIndustry().equalsIgnoreCase("Tablet")); + assertTrue("Did not return a iPad Mini", product3.getDescription().equalsIgnoreCase("iPad Mini")); + + docMgr.readMetadata(docId[2], mhRead); + validateMetadata(mhRead); + ***/ + } } From 77549e146b6319b346a636e83f2f2c66960fcd02 Mon Sep 17 00:00:00 2001 From: gvaidees Date: Mon, 27 Oct 2014 17:12:36 -0700 Subject: [PATCH 240/357] Support for bitemporal --- .../marklogic/javaclient/ConnectedRESTQA.java | 51 +++++++------------ 1 file changed, 19 insertions(+), 32 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java b/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java index 6089851aa..dd82a1472 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java @@ -1297,6 +1297,25 @@ public static void setupAppServicesGeoConstraint(String dbName) throws Exception public static void addElementRangeIndexTemporalAxis(String dbName, String axisName, String namespaceStart, String localnameStart, String namespaceEnd, String localnameEnd) throws Exception { + /** + { + "axis-name": "eri-json-system", + "axis-start": { + "element-reference": { + "namespace-uri": "", + "localname": "eri-system-start", + "scalar-type": "dateTime" + } + }, + "axis-end": { + "element-reference": { + "namespace-uri": "", + "localname": "eri-system-end", + "scalar-type": "dateTime" + } + } + } + */ ObjectMapper mapper = new ObjectMapper(); ObjectNode rootNode = mapper.createObjectNode(); @@ -1324,38 +1343,6 @@ public static void addElementRangeIndexTemporalAxis(String dbName, String axisNa System.out.println(rootNode.toString()); - - /*** - JSONObject rootNode = new JSONObject(jsonOrderedMap); - - - - JSONObject axisStart = new JSONObject(); - JSONObject elementReferenceStart = new JSONObject(); - - elementReferenceStart.put("namespace-uri", namespaceStart); - elementReferenceStart.put("localname", localnameStart); - elementReferenceStart.put("scalar-type", "dateTime"); - - axisStart.put("element-reference", elementReferenceStart); - - JSONObject axisEnd = new JSONObject(); - JSONObject elementReferenceEnd = new JSONObject(); - - elementReferenceEnd.put("namespace-uri", namespaceEnd); - elementReferenceEnd.put("localname", localnameEnd); - elementReferenceEnd.put("scalar-type", "dateTime"); - - axisEnd.put("element-reference", elementReferenceEnd); - - rootNode.put("axis-end", axisEnd); - rootNode.put("axis-start", axisStart); - rootNode.put( "axis-name", axisName); - - System.out.println(rootNode.toString()); - ***/ - - DefaultHttpClient client = new DefaultHttpClient(); client.getCredentialsProvider().setCredentials( new AuthScope("localhost", 8002), From 7a6dbb88a16699fbecdb2913986ff0f3e8aa03dc Mon Sep 17 00:00:00 2001 From: sammefford Date: Tue, 28 Oct 2014 10:06:25 -0600 Subject: [PATCH 241/357] fix another piece of #73, default date serialization to ISO 8601 format, though currently forcing timezone to UTC --- .../client/impl/PojoRepositoryImpl.java | 51 +++++++++++++++++++ .../marklogic/client/test/PojoFacadeTest.java | 42 ++++++++++++++- 2 files changed, 92 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java b/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java index 8e90d6566..0ecd0fb11 100644 --- a/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java +++ b/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java @@ -38,20 +38,29 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.BeanDescription; +import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationConfig; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.introspect.AnnotatedParameter; +import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition; import java.io.Serializable; import java.io.UnsupportedEncodingException; +import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.net.URLEncoder; +import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; +import java.util.TimeZone; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -68,10 +77,17 @@ public class PojoRepositoryImpl private Method idMethod; private Field idProperty; private String idPropertyName; + private static final String ISO_8601_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"; + private static SimpleDateFormat simpleDateFormat8601 = new SimpleDateFormat(ISO_8601_FORMAT); + static { simpleDateFormat8601.setTimeZone(TimeZone.getTimeZone("UTC")); } private ObjectMapper objectMapper = new ObjectMapper() // if we don't do the next two lines Jackson will automatically close our streams which is undesirable .configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false) .configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false) + // we do the next two so dates are written in xs:dateTime format + // which makes them ready for range indexes in MarkLogic Server + .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) + .setDateFormat(simpleDateFormat8601) // enableDefaultTyping just means include types in the serialized output // we need this to do strongly-typed queries .enableDefaultTyping( @@ -290,6 +306,41 @@ private String createUri(ID id) { } private void findId() { + if ( idMethod == null && idProperty == null ) { + SerializationConfig serializationConfig = objectMapper.getSerializationConfig(); + JavaType javaType = serializationConfig.constructType(entityClass); + BeanDescription beanDescription = serializationConfig.introspect(javaType); + List properties = beanDescription.findProperties(); + for ( BeanPropertyDefinition property : properties ) { + /* Constructor parameters don't work because they give us no value accessor + if ( property.hasConstructorParameter() ) { + AnnotatedParameter parameter = property.getConstructorParameter(); + if ( parameter.getAnnotation(Id.class) != null ) { + idPropertyName = property.getName(); + } + } + */ + if ( property.hasField() ) { + Field field = property.getField().getAnnotated(); + if ( field.getAnnotation(Id.class) != null ) { + idPropertyName = property.getName(); + idProperty = field; + break; + } + } + if ( property.hasGetter() ) { + Method getter = property.getGetter().getAnnotated(); + if ( getter.getAnnotation(Id.class) != null ) { + idPropertyName = property.getName(); + idMethod = getter; + break; + } + } + // setter only doesn't work because it gives us no value accessor + } + } + // Jackson's introspect approach should find it, but our old approach below + // gives some helpful errors if ( idMethod == null && idProperty == null ) { for ( Method method : entityClass.getDeclaredMethods() ) { if ( method.isAnnotationPresent(Id.class) ) { diff --git a/src/test/java/com/marklogic/client/test/PojoFacadeTest.java b/src/test/java/com/marklogic/client/test/PojoFacadeTest.java index 703c396dc..e0a7060c3 100644 --- a/src/test/java/com/marklogic/client/test/PojoFacadeTest.java +++ b/src/test/java/com/marklogic/client/test/PojoFacadeTest.java @@ -20,7 +20,10 @@ import static org.junit.Assert.assertTrue; import java.util.Arrays; +import java.util.Calendar; +import java.util.GregorianCalendar; import java.util.Iterator; +import java.util.TimeZone; import org.junit.FixMethodOrder; import org.junit.runners.MethodSorters; @@ -40,6 +43,7 @@ import com.marklogic.client.pojo.annotation.Id; import com.marklogic.client.test.BulkReadWriteTest; import com.marklogic.client.test.BulkReadWriteTest.CityWriter; +import com.marklogic.client.test.PojoFacadeTest.TimeTest; import static com.marklogic.client.test.BulkReadWriteTest.DIRECTORY; @@ -379,9 +383,43 @@ public void testE_IndexNumberAsString() throws Exception { assertEquals("Should find the right product id", 2001, page3.next().id); } + public static class TimeTest { + @Id public String id; + public Calendar timeTest; + + public TimeTest() {} + public TimeTest(String id, Calendar timeTest) { + this.id = id; + this.timeTest = timeTest; + } + } + + @Test + public void testF_DateTime() { + PojoRepository times = Common.client.newPojoRepository(TimeTest.class, String.class); + + GregorianCalendar septFirst = new GregorianCalendar(TimeZone.getTimeZone("CET")); + septFirst.set(2014, Calendar.SEPTEMBER, 1, 12, 0, 0); + + TimeTest timeTest1 = new TimeTest("1", septFirst); + times.write(timeTest1); + + TimeTest timeTest1FromDb = times.read("1"); + assertEquals("Times should be equal", timeTest1.timeTest.getTime().getTime(), + timeTest1FromDb.timeTest.getTime().getTime()); + + GregorianCalendar septFirstGMT = new GregorianCalendar(TimeZone.getTimeZone("GMT")); + septFirstGMT.set(2014, Calendar.SEPTEMBER, 1, 12, 0, 0); + + TimeTest timeTest2 = new TimeTest("2", septFirstGMT); + times.write(timeTest2); + + TimeTest timeTest2FromDb = times.read("2"); + assertEquals("Times should be equal", timeTest2.timeTest, timeTest2FromDb.timeTest); + } @Test - public void testF_DeletePojos() throws Exception { + public void testG_DeletePojos() throws Exception { cities.delete(1185098, 2239076); StringQueryDefinition query = Common.client.newQueryManager().newStringDefinition(); query.setCriteria("Tungi OR Dalatando OR Chittagong"); @@ -407,6 +445,8 @@ private static void cleanUp() { PojoRepository products2 = Common.client.newPojoRepository(Product2.class, Integer.class); products1.deleteAll(); products2.deleteAll(); + PojoRepository timeTests = Common.client.newPojoRepository(TimeTest.class, String.class); + timeTests.deleteAll(); cities.deleteAll(); } } From e49ece7cf492d3f145c9924dcda37097b48e16a0 Mon Sep 17 00:00:00 2001 From: skottam Date: Tue, 28 Oct 2014 09:15:02 -0700 Subject: [PATCH 242/357] update with modulePath scenario --- .../marklogic/javaclient/TestEvalXquery.java | 46 ++++++++++++++++++- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestEvalXquery.java b/test-complete/src/test/java/com/marklogic/javaclient/TestEvalXquery.java index 10a56bb48..7f5edfe8d 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestEvalXquery.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestEvalXquery.java @@ -24,16 +24,19 @@ import com.marklogic.client.DatabaseClient; import com.marklogic.client.DatabaseClientFactory; import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.DocumentManager; import com.marklogic.client.eval.EvalResult; import com.marklogic.client.eval.EvalResult.Type; import com.marklogic.client.eval.EvalResultIterator; import com.marklogic.client.eval.ServerEvaluationCall; import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.DocumentMetadataHandle; import com.marklogic.client.io.FileHandle; import com.marklogic.client.io.Format; import com.marklogic.client.io.InputStreamHandle; import com.marklogic.client.io.JacksonHandle; import com.marklogic.client.io.StringHandle; +import com.marklogic.client.io.DocumentMetadataHandle.Capability; @@ -54,9 +57,9 @@ public class TestEvalXquery extends BasicJavaClientREST { public static void setUpBeforeClass() throws Exception { System.out.println("In setup"); setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); - TestEvalXquery.createUserRolesWithPrevilages("test-eval", "xdbc:eval","any-uri"); + TestEvalXquery.createUserRolesWithPrevilages("test-eval", "xdbc:eval","any-uri","xdbc:invoke"); TestEvalXquery.createRESTUser("eval-user", "x", "test-eval"); - System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); +// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); } @AfterClass @@ -270,6 +273,7 @@ public void testXqueryReturningDifferentTypesAndFormatsWithHandle() throws Excep InputStream inputStream = new FileInputStream("src/test/java/com/marklogic/javaclient/data/xqueries.txt"); InputStreamHandle ish = new InputStreamHandle(); ish.set(inputStream); + try{ EvalResultIterator evr = client.newServerEval().xquery(ish).eval(); this.validateReturnTypes(evr); @@ -420,4 +424,42 @@ public void testXqueryWithExtVarAsNode() throws Exception { throw e; } } + + //Issue 156 , have test cases where you can pass, element node, text node, binary node, json object, json array as an external variable + @Test + public void testXqueryInvokeModuleRetDiffTypes() throws Exception { + + InputStream inputStream=null; + DatabaseClient moduleClient = DatabaseClientFactory.newClient("localhost", restPort,(restServerName+"-modules"),"admin", "admin", Authentication.DIGEST); + try{ + inputStream = new FileInputStream("src/test/java/com/marklogic/javaclient/data/xquery-modules-with-diff-variable-types.xqy"); + InputStreamHandle ish = new InputStreamHandle(); + ish.set(inputStream); + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); + metadataHandle.getPermissions().add("test-eval", Capability.UPDATE, Capability.READ,Capability.EXECUTE); + DocumentManager dm = moduleClient.newDocumentManager(); + dm.write("/data/xquery-modules-with-diff-variable-types.xqy",metadataHandle,ish); + DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + InputSource is = new InputSource(); + is.setCharacterStream(new StringReader("test1")); + Document doc = db.parse(is); + ServerEvaluationCall evl = client.newServerEval().modulePath("/data/xquery-modules-with-diff-variable-types.xqy"); + evl.addNamespace("test", "http://marklogic.com/test") + .addVariable("test:myString", "xml") + .addVariable("myBool", true).addVariable("myInteger", (int)31) + .addVariable("myDecimal", 10.5).addVariable("myDouble", 1.0471975511966) + .addVariable("myFloat",20).addVariableAs("myXmlNode",new DOMHandle(doc)) + .addVariableAs("myNull",(String) null); + EvalResultIterator evr = evl.eval(); + this.validateReturnTypes(evr); + + }catch(Exception e){ + throw e; + } + finally{ + if(inputStream != null) {inputStream.close();} + moduleClient.release(); + } + + } } From c06ae75bcb7c7f3c207bd4cb43b68093af8d965a Mon Sep 17 00:00:00 2001 From: Justin Makeig Date: Tue, 28 Oct 2014 11:39:33 -0700 Subject: [PATCH 243/357] Fixes #157 Adds Support section to README. --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index a31b21d59..90408eb10 100644 --- a/README.md +++ b/README.md @@ -52,3 +52,5 @@ You might want to skip the tests until you have configured a test database and R > mvn package -Dmaven.test.skip=true +## Support +The MarkLogic Java Client API is maintained by MarkLogic Engineering and distributed under the [Apache 2.0 license](https://github.com/marklogic/java-client-api/blob/master/LICENSE). It is designed for use in production applications with MarkLogic Server. Everyone is encouraged to file bug reports, feature requests, and pull requests through GitHub. This input is critical and will be carefully considered, but we can’t promise a specific resolution or timeframe for any request. In addition, MarkLogic provides technical support for [release tags](https://github.com/marklogic/java-client-api/releases) of the Java Client API to licensed customers under the terms outlined in the [Support Handbook](http://www.marklogic.com/files/Mark_Logic_Support_Handbook.pdf). For more information or to sign up for support, visit [help.marklogic.com](http://help.marklogic.com). From 9557208df1e1d6f8d181cf1dc82bf326141be543 Mon Sep 17 00:00:00 2001 From: Charles Greer Date: Tue, 28 Oct 2014 12:44:10 -0700 Subject: [PATCH 244/357] Update Java API for bug:29501 transaction change --- .../marklogic/client/impl/JerseyServices.java | 34 ++----------------- .../client/test/ConditionalDocumentTest.java | 2 +- 2 files changed, 3 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/marklogic/client/impl/JerseyServices.java b/src/main/java/com/marklogic/client/impl/JerseyServices.java index 3b93db717..75fdc4ad6 100644 --- a/src/main/java/com/marklogic/client/impl/JerseyServices.java +++ b/src/main/java/com/marklogic/client/impl/JerseyServices.java @@ -492,7 +492,6 @@ public void deleteDocument(RequestLogger reqlog, DocumentDescriptor desc, WebResource.Builder builder = addVersionHeader(desc, webResource.getRequestBuilder(), "If-Match"); - builder = addTransactionCookie(builder, transactionId); ClientResponse response = null; ClientResponse.Status status = null; @@ -620,7 +619,6 @@ private boolean getDocumentImpl(RequestLogger reqlog, WebResource.Builder builder = makeDocumentResource( makeDocumentParams(uri, categories, transactionId, extraParams)) .accept(mimetype); - builder = addTransactionCookie(builder, transactionId); if (extraParams != null && extraParams.containsKey("range")) builder = builder.header("range", extraParams.get("range").get(0)); @@ -897,7 +895,6 @@ private boolean getDocumentImpl(RequestLogger reqlog, docParams.add("format", metadataFormat); WebResource.Builder builder = makeDocumentResource(docParams).getRequestBuilder(); - builder = addTransactionCookie(builder, transactionId); builder = addVersionHeader(desc, builder, "If-None-Match"); MediaType multipartType = Boundary.addBoundary(MultiPartMediaTypes.MULTIPART_MIXED_TYPE); @@ -1051,7 +1048,6 @@ public ClientResponse headImpl(RequestLogger reqlog, String uri, transactionId); WebResource.Builder builder = webResource.getRequestBuilder(); - builder = addTransactionCookie(builder, transactionId); ClientResponse response = null; ClientResponse.Status status = null; @@ -1237,7 +1233,6 @@ private void putPostDocumentImpl(RequestLogger reqlog, String method, DocumentDe WebResource.Builder builder = webResource.type( (mimetype != null) ? mimetype : MediaType.WILDCARD); - builder = addTransactionCookie(builder, transactionId); if (uri != null) { builder = addVersionHeader(desc, builder, "If-Match"); } @@ -1394,7 +1389,6 @@ private void putPostDocumentImpl(RequestLogger reqlog, String method, DocumentDe makeDocumentParams(uri, categories, transactionId, extraParams, true); WebResource.Builder builder = makeDocumentResource(docParams).getRequestBuilder(); - builder = addTransactionCookie(builder, transactionId); if (uri != null) { builder = addVersionHeader(desc, builder, "If-Match"); } @@ -1632,7 +1626,6 @@ private void completeTransaction(String transactionId, String result) .queryParams(transParams); WebResource.Builder builder = webResource.getRequestBuilder(); - builder = addTransactionCookie(builder, transactionId); ClientResponse response = null; ClientResponse.Status status = null; @@ -1898,22 +1891,6 @@ private void updateVersion(DocumentDescriptor descriptor, descriptor.setVersion(version); } - private WebResource.Builder addTransactionCookie( - WebResource.Builder builder, String transactionId) { - if (transactionId != null) { - int pos = transactionId.indexOf("_"); - if (pos != -1) { - String hostId = transactionId.substring(0, pos); - builder.cookie(new Cookie("HostId", hostId)); - } else { - throw new IllegalArgumentException( - "transaction id without host id separator: "+transactionId - ); - } - } - return builder; - } - private WebResource.Builder addVersionHeader(DocumentDescriptor desc, WebResource.Builder builder, String name) { if (desc != null && desc instanceof DocumentDescriptorImpl @@ -2138,9 +2115,6 @@ else if (payloadFormat == Format.JSON && "xml".equals(params.getFirst("format")) + queryDef.getClass().getName()); } - if (params.containsKey("txid")) { - builder = addTransactionCookie(builder, params.getFirst("txid")); - } } ClientResponse getResponse() { @@ -2233,7 +2207,6 @@ public void deleteSearch(RequestLogger reqlog, DeleteQueryDefinition queryDef, WebResource webResource = connection.path("search").queryParams(params); WebResource.Builder builder = webResource.getRequestBuilder(); - builder = addTransactionCookie(builder, transactionId); ClientResponse response = null; ClientResponse.Status status = null; @@ -2396,9 +2369,8 @@ public T values(Class as, ValuesDefinition valDef, String mimetype, uri += "/" + valDef.getName(); } - WebResource.Builder builder = connection.path(uri) - .queryParams(docParams).accept(mimetype); - builder = addTransactionCookie(builder, transactionId); + WebResource.Builder builder = connection.path(uri).queryParams(docParams).accept(mimetype); + ClientResponse response = null; ClientResponse.Status status = null; @@ -2477,7 +2449,6 @@ public T valuesList(Class as, ValuesListDefinition valDef, WebResource.Builder builder = connection.path(uri) .queryParams(docParams).accept(mimetype); - builder = addTransactionCookie(builder, transactionId); ClientResponse response = null; ClientResponse.Status status = null; @@ -2546,7 +2517,6 @@ public T optionsList(Class as, String mimetype, String transactionId) WebResource.Builder builder = connection.path(uri) .queryParams(docParams).accept(mimetype); - builder = addTransactionCookie(builder, transactionId); ClientResponse response = null; ClientResponse.Status status = null; diff --git a/src/test/java/com/marklogic/client/test/ConditionalDocumentTest.java b/src/test/java/com/marklogic/client/test/ConditionalDocumentTest.java index 0feb708ff..d6ae745fa 100644 --- a/src/test/java/com/marklogic/client/test/ConditionalDocumentTest.java +++ b/src/test/java/com/marklogic/client/test/ConditionalDocumentTest.java @@ -190,7 +190,7 @@ public void testConditional() throws SAXException, IOException, } assertTrue("Delete with no version succeeded", ex != null); assertEquals("Delete with no version had misleading message", - "Local message: Content version required to delete document. Server Message: You do not have permission to this method and URL", + "Local message: Content version required to delete document. Server Message: You do not have permission to this method and URL.", ex.getMessage()); From 3852f98277c9257c6cb19401aecf9837e0da9fee Mon Sep 17 00:00:00 2001 From: skottam Date: Tue, 28 Oct 2014 15:16:20 -0700 Subject: [PATCH 245/357] updated the tests with PojoQueryDefinition for issue 96 --- .../TestPOJOQueryBuilderContainerQuery.java | 11 +- .../TestPOJOQueryBuilderGeoQueries.java | 192 ++++++++++++++++++ .../TestPOJOQueryBuilderValueQuery.java | 11 +- .../javaclient/TestPOJOwithQBEQueryDef.java | 9 +- 4 files changed, 209 insertions(+), 14 deletions(-) create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderGeoQueries.java diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderContainerQuery.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderContainerQuery.java index ec5faf23a..c0a7971c1 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderContainerQuery.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderContainerQuery.java @@ -14,6 +14,7 @@ import com.marklogic.client.io.JacksonHandle; import com.marklogic.client.pojo.PojoPage; import com.marklogic.client.pojo.PojoQueryBuilder; +import com.marklogic.client.pojo.PojoQueryDefinition; import com.marklogic.client.pojo.PojoRepository; import com.marklogic.client.query.QueryDefinition; import com.marklogic.client.pojo.PojoQueryBuilder.Operator; @@ -124,7 +125,7 @@ public void testPOJOContainerQuerySearchWithWord() { this.loadSimplePojos(products); String[] searchOptions ={"case-sensitive","wildcarded","min-occurs=2"}; PojoQueryBuilder qb = products.getQueryBuilder(); - QueryDefinition qd = qb.containerQuery("manufacturer", qb.term("counter")); + PojoQueryDefinition qd = qb.containerQuery("manufacturer", qb.term("counter")); JacksonHandle jh = new JacksonHandle(); products.setPageLength(5); p = products.search(qd, 1,jh); @@ -161,7 +162,7 @@ public void testPOJOwordSearchWithContainerQueryBuilder() { String[] searchOptions ={"case-sensitive","wildcarded","max-occurs=1"}; PojoQueryBuilder qb = products.getQueryBuilder(); String[] searchNames = {"special"}; - QueryDefinition qd =qb.containerQueryBuilder("manufacturer", Company.class).word("name",searchOptions,1.0,searchNames); + PojoQueryDefinition qd =qb.containerQueryBuilder("manufacturer", Company.class).word("name",searchOptions,1.0,searchNames); JacksonHandle jh = new JacksonHandle(); products.setPageLength(11); @@ -196,7 +197,7 @@ public void testPOJORangeSearch() throws Exception { PojoPage p; this.loadSimplePojos(products); PojoQueryBuilder qb = products.getQueryBuilder(); - QueryDefinition qd = qb.range("inventory", Operator.GE,1055); + PojoQueryDefinition qd = qb.range("inventory", Operator.GE,1055); JacksonHandle jh = new JacksonHandle(); products.setPageLength(56); p = products.search(qd, 1,jh); @@ -230,7 +231,7 @@ public void testPOJORangeQuerySearchWithOptions() { String[] searchOptions ={"uncached","min-occurs=1"}; PojoQueryBuilder qb = products.getQueryBuilder(); String[] searchNames = {"counter","special"}; - QueryDefinition qd = qb.range("inventory",searchOptions,Operator.LE,1054); + PojoQueryDefinition qd = qb.range("inventory",searchOptions,Operator.LE,1054); JacksonHandle jh = new JacksonHandle(); products.setPageLength(55); p = products.search(qd, 1,jh); @@ -268,7 +269,7 @@ public void testPOJOWordSearchWithOptions() throws Exception { String[] rangeOptions ={"uncached","min-occurs=1"}; PojoQueryBuilder qb = products.getQueryBuilder(); String[] searchNames = {"Acm*"}; - QueryDefinition qd = qb.and(qb.andNot(qb.word("name",searchOptions, 1.0,searchNames), + PojoQueryDefinition qd = qb.and(qb.andNot(qb.word("name",searchOptions, 1.0,searchNames), qb.containerQueryBuilder("manufacturer", Company.class).value("name","Acme special, Inc.") ), qb.range("inventory",rangeOptions,Operator.LT,1101)); diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderGeoQueries.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderGeoQueries.java new file mode 100644 index 000000000..452fc2b40 --- /dev/null +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderGeoQueries.java @@ -0,0 +1,192 @@ +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.io.JacksonHandle; +import com.marklogic.client.pojo.PojoPage; +import com.marklogic.client.pojo.PojoQueryBuilder; +import com.marklogic.client.pojo.PojoQueryDefinition; +import com.marklogic.client.pojo.PojoRepository; +import com.marklogic.client.query.QueryDefinition; + +public class TestPOJOQueryBuilderGeoQueries extends BasicJavaClientREST { + + private static String dbName = "TestPOJOQueryBuilderGeoQuerySearchDB"; + private static String [] fNames = {"TestPOJOQueryBuilderGeoQuerySearchDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client ; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + System.out.println("In setup"); +// setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); +// BasicJavaClientREST.setDatabaseProperties(dbName, "trailing-wildcard-searches", true); +// BasicJavaClientREST.setDatabaseProperties(dbName, "word-positions", true); +// BasicJavaClientREST.setDatabaseProperties(dbName, "element-word-positions", true); +// BasicJavaClientREST.addRangePathIndex(dbName, "long", "com.marklogic.javaclient.Artifact/manufacturer", "", "reject",true); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + System.out.println("In tear down" ); +// tearDownJavaRESTServer(dbName, fNames, restServerName); + } + @Before + public void setUp() throws Exception { + client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST); + } + @After + public void tearDown() throws Exception { + // release client + client.release(); + + } + + public Artifact getArtifact(int counter){ + + Artifact cogs = new Artifact(); + cogs.setId(counter); + if( counter % 5 == 0){ + cogs.setName("Cogs special"); + if(counter % 2 ==0){ + Company acme = new Company(); + acme.setName("Acme special, Inc."); + acme.setWebsite("http://www.acme special.com"); + acme.setLatitude(41.998+counter); + acme.setLongitude(-87.966+counter); + cogs.setManufacturer(acme); + + }else{ + Company widgets = new Company(); + widgets.setName("Widgets counter Inc."); + widgets.setWebsite("http://www.widgets counter.com"); + widgets.setLatitude(41.998+counter); + widgets.setLongitude(-87.966+counter); + cogs.setManufacturer(widgets); + } + }else{ + cogs.setName("Cogs "+counter); + if(counter % 2 ==0){ + Company acme = new Company(); + acme.setName("Acme "+counter+", Inc."); + acme.setWebsite("http://www.acme"+counter+".com"); + acme.setLatitude(41.998+counter); + acme.setLongitude(-87.966+counter); + cogs.setManufacturer(acme); + + }else{ + Company widgets = new Company(); + widgets.setName("Widgets "+counter+", Inc."); + widgets.setWebsite("http://www.widgets"+counter+".com"); + widgets.setLatitude(41.998+counter); + widgets.setLongitude(-87.966+counter); + cogs.setManufacturer(widgets); + } + } + cogs.setInventory(1000+counter); + return cogs; + } + public void validateArtifact(Artifact art) + { + assertNotNull("Artifact object should never be Null",art); + assertNotNull("Id should never be Null",art.id); + assertTrue("Inventry is always greater than 1000", art.getInventory()>1000); + } + public void loadSimplePojos(PojoRepository products) + { + for(int i=1;i<111;i++){ + if(i%2==0){ + products.write(this.getArtifact(i),"even","numbers"); + } + else { + products.write(this.getArtifact(i),"odd","numbers"); + } + } + } + // Below scenario is to test the geoPair -130 + @Test + public void testPOJOGeoQuerySearchWithGeoPair() { + PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); + PojoPage p; + this.loadSimplePojos(products); + String[] searchOptions ={"case-sensitive","wildcarded","min-occurs=2"}; + PojoQueryBuilder qb = products.getQueryBuilder(); + PojoQueryBuilder containerQb = qb.containerQueryBuilder("manufacturer", Company.class); + PojoQueryDefinition qd =containerQb.geospatial(containerQb.geoPair("latitude", "longitude"),containerQb.circle(51.998, -77.966, 1)); + + JacksonHandle jh = new JacksonHandle(); + products.setPageLength(5); + p = products.search(qd, 1,jh); + assertEquals("total no of pages",3,p.getTotalPages()); + System.out.println(jh.get().toString()); + + long pageNo=1,count=0; + do{ + count =0; + p = products.search(qd,pageNo); + while(p.hasNext()){ + Artifact a =p.next(); + validateArtifact(a); + assertTrue("Verifying document id is part of the search ids",a.getId()%5==0); + assertTrue("Verifying Manufacurer has term counter",a.getManufacturer().getName().contains("counter")); + count++; + System.out.println(a.getManufacturer().getName()); + } + assertEquals("Page size",count,p.size()); + pageNo=pageNo+p.getPageSize(); + }while(!p.isLastPage() && pageNo<=p.getTotalSize()); + assertEquals("page number after the loop",3,p.getPageNumber()); + assertEquals("total no of pages",3,p.getTotalPages()); + assertEquals("page length from search handle",5,jh.get().path("page-length").asInt()); + assertEquals("Total results from search handle",11,jh.get().path("total").asInt()); + + } + @Test + public void testPOJOGeoQuerySearchWithGeoPath() { + PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); + PojoPage p; + this.loadSimplePojos(products); + String[] searchOptions ={"case-sensitive","wildcarded","min-occurs=2"}; + PojoQueryBuilder qb = products.getQueryBuilder(); + PojoQueryBuilder containerQb = qb.containerQueryBuilder("manufacturer", Company.class); + PojoQueryDefinition qd =containerQb.geospatial(containerQb.geoProperty("latLong")); + + JacksonHandle jh = new JacksonHandle(); + products.setPageLength(5); + p = products.search(qd, 1,jh); + assertEquals("total no of pages",3,p.getTotalPages()); + System.out.println(jh.get().toString()); + + long pageNo=1,count=0; + do{ + count =0; + p = products.search(qd,pageNo); + while(p.hasNext()){ + Artifact a =p.next(); + validateArtifact(a); + assertTrue("Verifying document id is part of the search ids",a.getId()%5==0); + assertTrue("Verifying Manufacurer has term counter",a.getManufacturer().getName().contains("counter")); + count++; + System.out.println(a.getManufacturer().getName()); + } + assertEquals("Page size",count,p.size()); + pageNo=pageNo+p.getPageSize(); + }while(!p.isLastPage() && pageNo<=p.getTotalSize()); + assertEquals("page number after the loop",3,p.getPageNumber()); + assertEquals("total no of pages",3,p.getTotalPages()); + assertEquals("page length from search handle",5,jh.get().path("page-length").asInt()); + assertEquals("Total results from search handle",11,jh.get().path("total").asInt()); + + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderValueQuery.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderValueQuery.java index 7e6ad4e55..30ce212a5 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderValueQuery.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderValueQuery.java @@ -19,6 +19,7 @@ import com.marklogic.client.io.SearchHandle; import com.marklogic.client.pojo.PojoPage; import com.marklogic.client.pojo.PojoQueryBuilder; +import com.marklogic.client.pojo.PojoQueryDefinition; import com.marklogic.client.pojo.PojoRepository; import com.marklogic.client.query.QueryDefinition; @@ -127,7 +128,7 @@ public void testPOJOValueSearchWithNumbers() { String[] searchOptions ={"case-sensitive","wildcarded","min-occurs=2"}; PojoQueryBuilder qb = products.getQueryBuilder(); Number[] searchIds = {5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100,105,110,115,120,121,122,123,124,125,126}; - QueryDefinition qd = qb.value("id",searchOptions,-1.0,searchIds); + PojoQueryDefinition qd = qb.value("id",searchOptions,-1.0,searchIds); JacksonHandle jh = new JacksonHandle(); products.setPageLength(5); @@ -161,7 +162,7 @@ public void testPOJOValueSearchWithStrings() { String[] searchOptions ={"case-sensitive","wildcarded","min-occurs=2"}; PojoQueryBuilder qb = products.getQueryBuilder(); String[] searchNames = {"Acme spe*","Widgets spe*"}; - QueryDefinition qd = qb.value("name",searchOptions,100.0,searchNames); + PojoQueryDefinition qd = qb.value("name",searchOptions,100.0,searchNames); JacksonHandle jh = new JacksonHandle(); products.setPageLength(5); p = products.search(qd, 1,jh); @@ -198,7 +199,7 @@ public void testPOJOValueSearchWithNoResults() throws Exception { String[] searchOptions ={"case-sensitive","wildcarded","min-occurs=2"}; PojoQueryBuilder qb = products.getQueryBuilder(); String[] searchNames = {"acme*"}; - QueryDefinition qd = qb.value("name",searchOptions,100.0,searchNames); + PojoQueryDefinition qd = qb.value("name",searchOptions,100.0,searchNames); JacksonHandle jh = new JacksonHandle(); products.setPageLength(5); p = products.search(qd, 1,jh); @@ -235,7 +236,7 @@ public void testPOJOWordQuerySearchWithoutOptions() { // String[] searchOptions ={"case-sensitive","min-occurs=2"}; PojoQueryBuilder qb = products.getQueryBuilder(); String[] searchNames = {"counter","special"}; - QueryDefinition qd = qb.word("name",searchNames); + PojoQueryDefinition qd = qb.word("name",searchNames); JacksonHandle jh = new JacksonHandle(); products.setPageLength(5); p = products.search(qd, 1,jh); @@ -274,7 +275,7 @@ public void testPOJOWordSearchWithOptions() throws Exception { String[] searchOptions ={"case-sensitive","wildcarded","min-occurs=2"}; PojoQueryBuilder qb = products.getQueryBuilder(); String[] searchNames = {"count*"}; - QueryDefinition qd = qb.word("name",searchOptions,0.0,searchNames); + PojoQueryDefinition qd = qb.word("name",searchOptions,0.0,searchNames); JacksonHandle jh = new JacksonHandle(); products.setPageLength(5); p = products.search(qd, 1,jh); diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOwithQBEQueryDef.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOwithQBEQueryDef.java index cf485f8c8..5441a992f 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOwithQBEQueryDef.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOwithQBEQueryDef.java @@ -22,6 +22,7 @@ import com.marklogic.client.io.SearchHandle; import com.marklogic.client.io.StringHandle; import com.marklogic.client.pojo.PojoPage; +import com.marklogic.client.pojo.PojoQueryDefinition; import com.marklogic.client.pojo.PojoRepository; import com.marklogic.client.query.MatchDocumentSummary; import com.marklogic.client.query.QueryManager; @@ -137,7 +138,7 @@ public void testPOJOqbeSearchWithoutSearchHandle() { + ",\"$not\":[{\"name\":{\"$word\":\"special\",\"$exact\": false}}]" + "}}"; - RawQueryByExampleDefinition qd = queryMgr.newRawQueryByExampleDefinition(new StringHandle(queryAsString).withFormat(Format.JSON)); + PojoQueryDefinition qd = (PojoQueryDefinition)queryMgr.newRawQueryByExampleDefinition(new StringHandle(queryAsString).withFormat(Format.JSON)); qd.setCollections("odd"); products.setPageLength(11); p = products.search(qd, 1); @@ -176,7 +177,7 @@ public void testPOJOqbeSearchWithSearchHandle() { + "\"$and\":[{\"inventory\":{\"$gt\":1010}},{\"inventory\":{\"$le\":1110}}]" + ",\"$filtered\": true}}"; System.out.println(queryAsString); - RawQueryByExampleDefinition qd = queryMgr.newRawQueryByExampleDefinition(new StringHandle(queryAsString).withFormat(Format.JSON)); + PojoQueryDefinition qd = (PojoQueryDefinition)queryMgr.newRawQueryByExampleDefinition(new StringHandle(queryAsString).withFormat(Format.JSON)); qd.setCollections("even"); SearchHandle results = new SearchHandle(); products.setPageLength(10); @@ -228,7 +229,7 @@ public void testPOJOCombinedSearchWithJacksonHandle() { + "\"options\":{\"constraint\":{\"name\":\"pojo-name-field\", \"word\":{\"json-property\":\"name\"}}}" + "}}"; - RawCombinedQueryDefinition qd = queryMgr.newRawCombinedQueryDefinition(new StringHandle(queryAsString).withFormat(Format.JSON)); + PojoQueryDefinition qd = (PojoQueryDefinition)queryMgr.newRawCombinedQueryDefinition(new StringHandle(queryAsString).withFormat(Format.JSON)); JacksonHandle results = new JacksonHandle(); p = products.search(qd, 1,results); products.setPageLength(11); @@ -272,7 +273,7 @@ public void testPOJOcombinedSearchforNumberWithStringHandle() throws JsonProcess + "\"range-constraint-query\":{\"constraint-name\":\"id\", \"value\":[5,10,15,20,25,30]}}," + "\"options\":{\"return-metrics\":false, \"constraint\":{\"name\":\"id\", \"range\":{\"type\": \"xs:long\",\"json-property\":\"id\"}}}" + "}}"; - RawCombinedQueryDefinition qd = queryMgr.newRawCombinedQueryDefinition(new StringHandle(queryAsString).withFormat(Format.JSON)); + PojoQueryDefinition qd = (PojoQueryDefinition)queryMgr.newRawCombinedQueryDefinition(new StringHandle(queryAsString).withFormat(Format.JSON)); StringHandle results = new StringHandle(); JacksonHandle jh = new JacksonHandle(); From 570cf34f53d997cee049fb6acf502678eee9323e Mon Sep 17 00:00:00 2001 From: gvaidees Date: Tue, 28 Oct 2014 21:54:48 -0700 Subject: [PATCH 246/357] Minor additions to checks being made. Removed comments associated with bugs (after bug has been fixed) --- .../marklogic/javaclient/TestBiTemporal.java | 145 ++++++++++++++---- 1 file changed, 113 insertions(+), 32 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBiTemporal.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBiTemporal.java index 3e0f02607..050081428 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBiTemporal.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBiTemporal.java @@ -55,8 +55,6 @@ import com.marklogic.client.query.StructuredQueryDefinition; import com.marklogic.client.query.StructuredQueryBuilder.TemporalOperator; -// BUGS: 30098, 30092, - public class TestBiTemporal extends BasicJavaClientREST{ private static String dbName = "TestBiTemporalJava"; @@ -90,6 +88,8 @@ public class TestBiTemporal extends BasicJavaClientREST{ @BeforeClass public static void setUpBeforeClass() throws Exception { + System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + System.out.println("In setup"); setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); @@ -487,7 +487,7 @@ private JacksonDatabindHandle getJSONDocumentHandle( @Test public void testInsertXMLSingleDocumentUsingTemplate() throws Exception { - System.out.println("Inside testInsertXMLSingleDocumentUsingDescriptor"); + System.out.println("Inside testInsertXMLSingleDocumentUsingTemplate"); String docId = "javaSingleXMLDoc.xml"; DOMHandle handle = getXMLDocumentHandle( @@ -851,6 +851,14 @@ public void testJSONConsolidated() throws Exception { metadataHandle = new DocumentMetadataHandle(); record.getMetadata(metadataHandle); Iterator resCollections = metadataHandle.getCollections().iterator(); + + if (record.getUri().equals(docId)) { + // Must belong to latest collection as well. So, count must be 4 + assert(resCollections.equals(4)); + } + else { + assert(resCollections.equals(3)); + } while (resCollections.hasNext()) { String collection = resCollections.next(); System.out.println("Collection = " + collection); @@ -860,6 +868,11 @@ public void testJSONConsolidated() throws Exception { !collection.equals(temporalCollectionName)) { assertFalse("Collection not what is expected: " + collection, true); } + + if (collection.equals(latestCollectionName)) { + // If there is a latest collection, docId must match the URI + assert(record.getUri().equals(docId)); + } } if (record.getFormat() != Format.JSON) { @@ -1183,8 +1196,7 @@ record = termQueryResults.next(); (actualPermissions.contains("app-user:[") && actualPermissions.contains("READ") && actualPermissions.contains("UPDATE") && actualPermissions.contains("EXECUTE"))); - // BUG: Returns a 99, when it should be 11 - // assertEquals(quality, 11); + assertEquals(quality, 11); } if (validStartDate.contains("2008-12-31T23:59:59") && validEndDate.contains("2011-12-31T23:59:59")) { @@ -1218,8 +1230,7 @@ record = termQueryResults.next(); (actualPermissions.contains("app-user:[") && actualPermissions.contains("READ") && actualPermissions.contains("UPDATE") && actualPermissions.contains("EXECUTE"))); - // BUG: Returns a 99, when it should be 11 - // assertEquals(quality, 11); + assertEquals(quality, 11); validateMetadata(metadataHandle); } @@ -1253,8 +1264,7 @@ record = termQueryResults.next(); (actualPermissions.contains("app-user:[") && actualPermissions.contains("READ") && actualPermissions.contains("UPDATE") && actualPermissions.contains("EXECUTE"))); - // BUG: Returns a 99, when it should be 11 - // assertEquals(quality, 11); + assertEquals(quality, 11); } } } @@ -1383,8 +1393,7 @@ record = termQueryResults.next(); actualPermissions.contains("UPDATE"))); assertFalse("Document permissions difference in app-user permission", actualPermissions.contains("EXECUTE")); - // BUG: Returns a 0, when it should be 99 - // assertEquals(quality, 99); + assertEquals(quality, 99); } if (validStartDate.contains("2001-01-01T00:00:00") && validEndDate.contains("2003-01-01T00:00:00")) { @@ -1416,8 +1425,7 @@ record = termQueryResults.next(); (actualPermissions.contains("app-user:[") && actualPermissions.contains("READ") && actualPermissions.contains("UPDATE") && actualPermissions.contains("EXECUTE"))); - // BUG: Returns a 0, when it should be 11 - // assertEquals(quality, 11); + assertEquals(quality, 11); } if (validStartDate.contains("2008-12-31T23:59:59") && validEndDate.contains("2011-12-31T23:59:59")) { @@ -1450,8 +1458,7 @@ record = termQueryResults.next(); (actualPermissions.contains("app-user:[") && actualPermissions.contains("READ") && actualPermissions.contains("UPDATE") && actualPermissions.contains("EXECUTE"))); - // BUG: Returns a 0, when it should be 11 - // assertEquals(quality, 11); + assertEquals(quality, 11); // Properties should still be associated this document // BUG ... This should be commented out when properties are not deleted when document is deleted @@ -1486,8 +1493,7 @@ record = termQueryResults.next(); (actualPermissions.contains("app-user:[") && actualPermissions.contains("READ") && actualPermissions.contains("UPDATE") && actualPermissions.contains("EXECUTE"))); - // BUG: Returns a 0, when it should be 11 - // assertEquals(quality, 11); + assertEquals(quality, 11); } } } @@ -1696,7 +1702,6 @@ record = termQueryResults.next(); (actualPermissions.contains("app-user:[") && actualPermissions.contains("READ") && actualPermissions.contains("UPDATE") && actualPermissions.contains("EXECUTE"))); - // BUG: Returns a 99, when it should be 11 // assertEquals(quality, 11); } @@ -1731,7 +1736,6 @@ record = termQueryResults.next(); (actualPermissions.contains("app-user:[") && actualPermissions.contains("READ") && actualPermissions.contains("UPDATE") && actualPermissions.contains("EXECUTE"))); - // BUG: Returns a 99, when it should be 11 // assertEquals(quality, 11); validateMetadata(metadataHandle); @@ -1766,7 +1770,6 @@ record = termQueryResults.next(); (actualPermissions.contains("app-user:[") && actualPermissions.contains("READ") && actualPermissions.contains("UPDATE") && actualPermissions.contains("EXECUTE"))); - // BUG: Returns a 99, when it should be 11 // assertEquals(quality, 11); } } @@ -1896,7 +1899,6 @@ record = termQueryResults.next(); actualPermissions.contains("UPDATE"))); assertFalse("Document permissions difference in app-user permission", actualPermissions.contains("EXECUTE")); - // BUG: Returns a 0, when it should be 99 // assertEquals(quality, 99); } @@ -1929,7 +1931,6 @@ record = termQueryResults.next(); (actualPermissions.contains("app-user:[") && actualPermissions.contains("READ") && actualPermissions.contains("UPDATE") && actualPermissions.contains("EXECUTE"))); - // BUG: Returns a 0, when it should be 11 // assertEquals(quality, 11); } @@ -1963,7 +1964,6 @@ record = termQueryResults.next(); (actualPermissions.contains("app-user:[") && actualPermissions.contains("READ") && actualPermissions.contains("UPDATE") && actualPermissions.contains("EXECUTE"))); - // BUG: Returns a 0, when it should be 11 // assertEquals(quality, 11); // Properties should still be associated this document @@ -1999,7 +1999,6 @@ record = termQueryResults.next(); (actualPermissions.contains("app-user:[") && actualPermissions.contains("READ") && actualPermissions.contains("UPDATE") && actualPermissions.contains("EXECUTE"))); - // BUG: Returns a 0, when it should be 11 // assertEquals(quality, 11); } } @@ -2078,6 +2077,7 @@ public void testSystemTimeUsingInvalidTime() throws Exception { } @Test + // BUG: REST API bug around transactions fails this test public void testTransactionCommit() throws Exception { System.out.println("Inside testTransactionCommit"); @@ -2114,6 +2114,15 @@ public void testTransactionCommit() throws Exception { assertEquals("Document uri wrong after insert", docId, latestDoc.getUri()); } + // Make sure document is not visible to any other transaction + readResults = docMgr.read(docId); + System.out.println("Number of results = " + readResults.size()); + if (readResults.size() != 0) { + transaction.rollback(); + + assertEquals("Wrong number of results", 1, readResults.size()); + } + try { updateJSONSingleDocument(docId, transaction); } @@ -2137,7 +2146,7 @@ public void testTransactionCommit() throws Exception { assertEquals("Wrong number of results", 1, termQueryResults.getTotalSize()); } - // There should be 1 document in docId collection + // There should be 4 documents in docId collection queryMgr = client.newQueryManager(); sqb = queryMgr.newStructuredQueryBuilder(); termQuery = sqb.collection(docId); @@ -2151,6 +2160,22 @@ public void testTransactionCommit() throws Exception { assertEquals("Wrong number of results", 4, termQueryResults.getTotalSize()); } + + // Search for documents using doc uri collection and no transaction object passed. + // There should be 0 documents in docId collection + queryMgr = client.newQueryManager(); + sqb = queryMgr.newStructuredQueryBuilder(); + termQuery = sqb.collection(docId); + + start = 1; + termQueryResults = docMgr.search(termQuery, start); + System.out.println("Number of results = " + termQueryResults.getTotalSize()); + + if (termQueryResults.getTotalSize() != 0) { + transaction.rollback(); + + assertEquals("Wrong number of results", 0, termQueryResults.getTotalSize()); + } try { deleteJSONSingleDocument(docId, transaction); @@ -2190,6 +2215,7 @@ public void testTransactionCommit() throws Exception { } @Test + // BUG: REST API bug around transactions fails this test public void testTransactionRollback() throws Exception { System.out.println("Inside testTransaction"); @@ -2573,7 +2599,7 @@ public void testPeriodCompareQuerySingleAxisBasedOnALNContains() throws Exceptio (validStartDate.contains("2001-01-01T00:00:00") && validEndDate.contains("2011-12-31T23:59:59"))); assertTrue("System start date check failed", - (systemStartDate.contains("2005-01-01T00:00:01") && !systemEndDate.contains("2010-01-01T00:00:01"))); + (systemStartDate.contains("2005-01-01T00:00:01") && systemEndDate.contains("2010-01-01T00:00:01"))); } } @@ -2584,6 +2610,8 @@ public void testPeriodCompareQuerySingleAxisBasedOnALNContains() throws Exceptio @Test public void testLsqtQuery() throws Exception { + + System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); // Read documents based on document URI and ALN Contains. We are just looking for count of documents to be correct String docId = "javaSingleJSONDoc.json"; @@ -2659,6 +2687,7 @@ public void testLsqtQuery() throws Exception { } } + // BUG. I believe Java API is doing a get instead of a POST that returns all documents in doc uri collection System.out.println("Number of results using SQB = " + count); assertEquals("Wrong number of results", 3, count); } @@ -2755,10 +2784,9 @@ public void testDocumentUsingCollectionNamedLatest() throws Exception { assertTrue("Exception not thrown for invalid temporal collection", exceptionThrown); } - @Test - // Negative test .. check if this is needed + // Negative test. Doc URI Id is the same as the temporal collection name public void testInsertDocumentUsingDocumentURIAsCollectionName() throws Exception { // Now insert a JSON document String jsonDocId = "javaSingleJSONDoc.json"; @@ -2789,15 +2817,68 @@ public void testInsertDocumentUsingDocumentURIAsCollectionName() throws Exceptio System.out.println(ex.getFailedRequest().getStatusCode()); System.out.println(ex.getFailedRequest().getMessageCode()); - // BUG: Right now this returns 500 error. Bug is open to fix this - // assert(ex.getFailedRequest().getMessageCode().equals("TEMPORAL-SYSTEMTIME-BACKWARDS")); - // assert(ex.getFailedRequest().getStatusCode() == 400); + assert(ex.getFailedRequest().getMessageCode().equals("TEMPORAL-URIALREADYEXISTS")); + assert(ex.getFailedRequest().getStatusCode() == 400); } ConnectedRESTQA.deleteElementRangeIndexTemporalCollection("Documents", jsonDocId); - // Looks like this should fail. But is not failing + assertTrue("Exception not thrown for invalid temporal collection", exceptionThrown); + } + + @Test + // Negative test. Doc URI Id is the same as the temporal collection name + // BUG 30173 exists for this non failure. + public void testCreateCollectionUsingSameNameAsDocURI() throws Exception { + // Now insert a JSON document + String jsonDocId = "javaSingleJSONDoc.json"; + + System.out.println("Inside testInserDocumentUsingDocumentURIAsCollectionName"); + + // Insert a document called as insertJSONSingleDocument + insertJSONSingleDocument(jsonDocId, null); + + boolean exceptionThrown = false; + try { + // Create collection a collection with same name as doci URI + ConnectedRESTQA.addElementRangeIndexTemporalCollection(dbName, jsonDocId, axisSystemName, axisValidName); + } + catch (com.marklogic.client.FailedRequestException ex) { + exceptionThrown = true; + System.out.println(ex.getFailedRequest().getStatusCode()); + System.out.println(ex.getFailedRequest().getMessageCode()); + + // assert(ex.getFailedRequest().getMessageCode().equals("TEMPORAL-URIALREADYEXISTS")); + // assert(ex.getFailedRequest().getStatusCode() == 400); + } // assertTrue("Exception not thrown for invalid temporal collection", exceptionThrown); + + // Create a document using the collection created above + // So, we are creating a document under a collection whose name is the same as an existing temporal doc uri + JacksonDatabindHandle handle = getJSONDocumentHandle( + "2001-01-01T00:00:00", "2011-12-31T23:59:59", "999 Skyway Park - JSON", jsonDocId); + + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); + + // put meta-data + DocumentMetadataHandle mh = setMetadata(false); + + exceptionThrown = false; + try { + docMgr.write("1.json", mh, handle, null, null, jsonDocId, null); + } + catch (com.marklogic.client.FailedRequestException ex) { + exceptionThrown = true; + System.out.println(ex.getFailedRequest().getStatusCode()); + System.out.println(ex.getFailedRequest().getMessageCode()); + + // assert(ex.getFailedRequest().getMessageCode().equals("TEMPORAL-URIALREADYEXISTS")); + // assert(ex.getFailedRequest().getStatusCode() == 400); + } + + ConnectedRESTQA.deleteElementRangeIndexTemporalCollection("Documents", jsonDocId); + assertTrue("Exception not thrown for invalid temporal collection", exceptionThrown); } public void bulkWrite() throws Exception { From 7ae26326e4fdcfa7e2418559531c7952603527b2 Mon Sep 17 00:00:00 2001 From: sammefford Date: Wed, 29 Oct 2014 11:38:04 -0600 Subject: [PATCH 247/357] fix #167 --- src/main/java/com/marklogic/client/impl/FailedRequest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/marklogic/client/impl/FailedRequest.java b/src/main/java/com/marklogic/client/impl/FailedRequest.java index 3e30d4206..352542a33 100644 --- a/src/main/java/com/marklogic/client/impl/FailedRequest.java +++ b/src/main/java/com/marklogic/client/impl/FailedRequest.java @@ -99,14 +99,14 @@ public static FailedRequest getFailedRequest(int httpStatus, MediaType contentTy FailedRequest failure; // by default XML is supported - if (contentType.equals(MediaType.APPLICATION_XML_TYPE)) { + if (contentType.isCompatible(MediaType.APPLICATION_XML_TYPE)) { FailedRequestParser xmlParser = new FailedRequestXMLParser(); failure = xmlParser.parseFailedRequest(httpStatus, content); } - else if (contentType.equals(MediaType.APPLICATION_JSON_TYPE)) { + else if (contentType.isCompatible(MediaType.APPLICATION_JSON_TYPE)) { failure = jsonFailedRequest(httpStatus, content); } else { From 81c33d2ca6733e74992b2f3b38cc8be86d514858 Mon Sep 17 00:00:00 2001 From: sammefford Date: Wed, 29 Oct 2014 16:11:46 -0600 Subject: [PATCH 248/357] refinement of #67--support new handling for XML node() types available from server --- .../marklogic/client/impl/JerseyServices.java | 17 ++++------- .../com/marklogic/client/test/EvalTest.java | 8 ++--- src/test/resources/evaltest.xqy | 29 +++++++------------ 3 files changed, 20 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/marklogic/client/impl/JerseyServices.java b/src/main/java/com/marklogic/client/impl/JerseyServices.java index 75fdc4ad6..c7c53bcbd 100644 --- a/src/main/java/com/marklogic/client/impl/JerseyServices.java +++ b/src/main/java/com/marklogic/client/impl/JerseyServices.java @@ -3627,17 +3627,12 @@ public EvalResult.Type getType() { return EvalResult.Type.STRING; } else if ( "boolean".equals(xPrimitive) ) { return EvalResult.Type.BOOLEAN; - } else if ( "node()".equals(xPrimitive) ) { - String xPath = content.getHeader("X-Path"); - if ( xPath == null ) { - return EvalResult.Type.OTHER; - } else if ( xPath.endsWith("comment()") ) { - return EvalResult.Type.COMMENT; - } else if ( xPath.endsWith("processing-instruction()") ) { - return EvalResult.Type.PROCESSINGINSTRUCTION; - } else if ( content.getHeader("X-Attr") != null ) { - return EvalResult.Type.ATTRIBUTE; - } + } else if ( "attribute()".equals(xPrimitive) ) { + return EvalResult.Type.ATTRIBUTE; + } else if ( "comment()".equals(xPrimitive) ) { + return EvalResult.Type.COMMENT; + } else if ( "processing-instruction()".equals(xPrimitive) ) { + return EvalResult.Type.PROCESSINGINSTRUCTION; } else if ( "text()".equals(xPrimitive) ) { return EvalResult.Type.TEXTNODE; } else if ( "binary()".equals(xPrimitive) ) { diff --git a/src/test/java/com/marklogic/client/test/EvalTest.java b/src/test/java/com/marklogic/client/test/EvalTest.java index bd7987ad0..e6b04b87b 100644 --- a/src/test/java/com/marklogic/client/test/EvalTest.java +++ b/src/test/java/com/marklogic/client/test/EvalTest.java @@ -57,7 +57,7 @@ public class EvalTest { @BeforeClass public static void beforeClass() { Common.connectEval(); - //System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); } @AfterClass public static void afterClass() { @@ -167,9 +167,9 @@ private void runAndTest(ServerEvaluationCall call) .addVariable("myDuration", "P100D") .addVariable("myDocument", new DOMHandle(document)) .addVariable("myQName", "myPrefix:a") - .addVariable("myAttribute", "") + //.addVariable("myAttribute", "") .addVariable("myComment", "") - .addVariable("myElement", "") + .addVariable("myElement", "") .addVariable("myProcessingInstruction", "") .addVariable("myText", "a") // the next three use built-in methods of ServerEvaluationCall @@ -249,7 +249,7 @@ private void runAndTest(ServerEvaluationCall call) assertEquals("myComment should be Type.COMMENT", EvalResult.Type.COMMENT, result.getType()); assertEquals("myComment should be Format.TEXT", Format.TEXT, result.getFormat()); result = results.next(); - assertEquals("myElement should = ", "", result.getString()); + assertEquals("myElement looks wrong", "", result.getString()); assertEquals("myElement should be Type.XML", EvalResult.Type.XML, result.getType()); assertEquals("myElement should be Format.XML", Format.XML, result.getFormat()); result = results.next(); diff --git a/src/test/resources/evaltest.xqy b/src/test/resources/evaltest.xqy index d38fbcba8..c01913261 100644 --- a/src/test/resources/evaltest.xqy +++ b/src/test/resources/evaltest.xqy @@ -3,17 +3,17 @@ declare variable $test:myString as xs:string external; declare variable $myArray as json:array external; declare variable $myObject as json:object external; declare variable $myAnyUri as xs:anyURI external; -declare variable $myBinary as xs:hexBinary external; +declare variable $myBinary as binary() external; declare variable $myBase64Binary as xs:base64Binary external; declare variable $myHexBinary as xs:hexBinary external; declare variable $myDuration as xs:duration external; declare variable $myQName as xs:QName external; -declare variable $myDocument as xs:string external; -declare variable $myAttribute as xs:string external; -declare variable $myComment as xs:string external; -declare variable $myElement as xs:string external; -declare variable $myProcessingInstruction as xs:string external; -declare variable $myText as xs:string external; +declare variable $myDocument as document-node() external; +declare variable $myComment as document-node() external; +(: declare variable $myComment as comment() external; :) +declare variable $myElement as element() external; +declare variable $myProcessingInstruction as document-node() external; +declare variable $myText as text() external; declare variable $myBool as xs:boolean external; declare variable $myInteger as xs:integer external; declare variable $myBigInteger as xs:string external; @@ -29,18 +29,9 @@ declare variable $myDate as xs:date external; declare variable $myDateTime as xs:dateTime external; declare variable $myTime as xs:time external; declare variable $myNull external; -let $myBinary := binary{$myBinary} -let $myDocument := - xdmp:unquote($myDocument) -let $myAttribute := - xdmp:unquote($myAttribute)/*/@* -let $myComment := - xdmp:unquote($myComment)/comment() -let $myElement := - xdmp:unquote($myElement)/element() -let $myProcessingInstruction := - xdmp:unquote($myProcessingInstruction)/processing-instruction() -let $myText := text {$myText} +let $myAttribute := $myElement/@* +let $myComment := $myComment/comment() +let $myProcessingInstruction := $myProcessingInstruction/processing-instruction() let $myCtsQuery := cts:word-query('a') return ( $test:myString, $myArray, $myObject, $myAnyUri, From 075c383481ef0049f098a6f258ee156797265708 Mon Sep 17 00:00:00 2001 From: sammefford Date: Wed, 29 Oct 2014 16:13:37 -0600 Subject: [PATCH 249/357] support new error format for eval--was getting NPE --- .../marklogic/client/impl/FailedRequest.java | 46 +++++++++++++------ .../client/test/FailedRequestTest.java | 4 +- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/marklogic/client/impl/FailedRequest.java b/src/main/java/com/marklogic/client/impl/FailedRequest.java index 352542a33..10d129260 100644 --- a/src/main/java/com/marklogic/client/impl/FailedRequest.java +++ b/src/main/java/com/marklogic/client/impl/FailedRequest.java @@ -24,6 +24,7 @@ import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; +import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import com.marklogic.client.io.JSONErrorParser; @@ -65,19 +66,38 @@ public FailedRequest parseFailedRequest(int httpStatus, InputStream is) { try { builder = factory.newDocumentBuilder(); Document doc = builder.parse(is); - String statusCode = doc - .getElementsByTagNameNS(JerseyServices.ERROR_NS, - "status-code").item(0).getTextContent(); - failure.setStatusCode(Integer.parseInt(statusCode)); - failure.setStatusString(doc - .getElementsByTagNameNS(JerseyServices.ERROR_NS, - "status").item(0).getTextContent()); - failure.setMessageCode(doc - .getElementsByTagNameNS(JerseyServices.ERROR_NS, - "message-code").item(0).getTextContent()); - failure.setMessageString(doc - .getElementsByTagNameNS(JerseyServices.ERROR_NS, - "message").item(0).getTextContent()); + String statusCode = null; + NodeList statusCodes = doc.getElementsByTagNameNS(JerseyServices.ERROR_NS, "status-code"); + if ( statusCodes != null && statusCodes.getLength() > 0 ) { + statusCode = statusCodes.item(0).getTextContent(); + } + if ( statusCode != null ) { + failure.setStatusCode(Integer.parseInt(statusCode)); + } else { + failure.setStatusCode(httpStatus); + } + NodeList statuses = doc.getElementsByTagNameNS(JerseyServices.ERROR_NS, "status"); + if ( statuses != null && statuses.getLength() > 0 ) { + failure.setStatusString( statuses.item(0).getTextContent() ); + } + NodeList messageCodes = doc.getElementsByTagNameNS(JerseyServices.ERROR_NS, "message-code"); + if ( messageCodes != null && messageCodes.getLength() > 0 ) { + failure.setMessageCode( messageCodes.item(0).getTextContent() ); + } + // the following is for eval errors + String formatString = null; + NodeList formatStrings = doc.getElementsByTagNameNS(JerseyServices.ERROR_NS, "format-string"); + if ( formatStrings != null && formatStrings.getLength() > 0 ) { + formatString = formatStrings.item(0).getTextContent(); + } + if ( formatString != null ) { + failure.setMessageString(formatString); + } else { + NodeList messageStrings = doc.getElementsByTagNameNS(JerseyServices.ERROR_NS, "message"); + if ( messageStrings != null && messageStrings.getLength() > 0 ) { + failure.setMessageString( messageStrings.item(0).getTextContent() ); + } + } } catch (ParserConfigurationException e) { failure.setStatusCode(httpStatus); failure.setMessageString("Request failed. Unable to parse server error."); diff --git a/src/test/java/com/marklogic/client/test/FailedRequestTest.java b/src/test/java/com/marklogic/client/test/FailedRequestTest.java index 0ba6df4f9..416fcd3ab 100644 --- a/src/test/java/com/marklogic/client/test/FailedRequestTest.java +++ b/src/test/java/com/marklogic/client/test/FailedRequestTest.java @@ -18,8 +18,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; -import org.junit.After; -import org.junit.Before; import org.junit.Test; import com.marklogic.client.DatabaseClient; @@ -33,7 +31,6 @@ import com.marklogic.client.admin.ServerConfigurationManager; import com.marklogic.client.admin.config.QueryOptions.Facets; import com.marklogic.client.admin.config.QueryOptionsBuilder; -import com.marklogic.client.io.Format; import com.marklogic.client.io.QueryOptionsHandle; @SuppressWarnings("deprecation") @@ -43,6 +40,7 @@ public class FailedRequestTest { public void testFailedRequest() throws FailedRequestException, ForbiddenUserException, ResourceNotFoundException, ResourceNotResendableException { Common.connect(); + //System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); QueryOptionsManager mgr = Common.client.newServerConfigManager() .newQueryOptionsManager(); From 8ef8e08745b609e44cc5bf0c8526cb6bf5d4c656 Mon Sep 17 00:00:00 2001 From: skottam Date: Wed, 29 Oct 2014 15:55:36 -0700 Subject: [PATCH 250/357] adding tests for eval java script --- .../javaclient/TestEvalJavaScript.java | 483 ++++++++++++++++++ .../javaclient/data/javascriptQueries.sjs | 1 + 2 files changed, 484 insertions(+) create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/TestEvalJavaScript.java create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/data/javascriptQueries.sjs diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestEvalJavaScript.java b/test-complete/src/test/java/com/marklogic/javaclient/TestEvalJavaScript.java new file mode 100644 index 000000000..ed6a34564 --- /dev/null +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestEvalJavaScript.java @@ -0,0 +1,483 @@ +package com.marklogic.javaclient; + +import static org.junit.Assert.*; + +import java.io.FileInputStream; +import java.io.InputStream; +import java.io.StringReader; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; + +import org.json.JSONObject; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; +import org.w3c.dom.Document; +import org.xml.sax.InputSource; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.marklogic.client.DatabaseClient; +import com.marklogic.client.DatabaseClientFactory; +import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.document.DocumentManager; +import com.marklogic.client.eval.EvalResult; +import com.marklogic.client.eval.EvalResult.Type; +import com.marklogic.client.eval.EvalResultIterator; +import com.marklogic.client.eval.ServerEvaluationCall; +import com.marklogic.client.io.DOMHandle; +import com.marklogic.client.io.DocumentMetadataHandle; +import com.marklogic.client.io.FileHandle; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.InputStreamHandle; +import com.marklogic.client.io.JacksonHandle; +import com.marklogic.client.io.StringHandle; +import com.marklogic.client.io.DocumentMetadataHandle.Capability; + + + +/* + * This test is meant for javascript to + * verify the eval api can handle all the formats of documents + * verify eval api can handle all the return types + * Verify eval takes all kind of variables + */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class TestEvalJavaScript extends BasicJavaClientREST { + private static String dbName = "TestEvalJavaScriptDB"; + private static String [] fNames = {"TestEvalJavaScriptDB-1"}; + private static String restServerName = "REST-Java-Client-API-Server"; + private static int restPort = 8011; + private DatabaseClient client ; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + System.out.println("In setup"); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + TestEvalXquery.createUserRolesWithPrevilages("test-eval", "xdbc:eval","any-uri","xdbc:invoke"); + TestEvalXquery.createRESTUser("eval-user", "x", "test-eval"); +// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + System.out.println("In tear down" ); + tearDownJavaRESTServer(dbName, fNames, restServerName); + TestEvalXquery.deleteRESTUser("eval-user"); + TestEvalXquery.deleteUserRole("test-eval"); + } + + @Before + public void setUp() throws Exception { + client = DatabaseClientFactory.newClient("localhost", restPort,"eval-user", "x", Authentication.DIGEST); + } + + @After + public void tearDown() throws Exception { + System.out.println("Running CleanUp script"); + // release client + client.release(); + } +/* + This method is validating all the return values from java script + */ + void validateReturnTypes(EvalResultIterator evr) throws Exception{ + + while(evr.hasNext()) + { + EvalResult er =evr.next(); + if(er.getType().equals(Type.XML)){ + DOMHandle dh = new DOMHandle(); + dh=er.get(dh); + if(dh.get().getDocumentElement().hasChildNodes()){ +// System.out.println("Type XML :"+convertXMLDocumentToString(dh.get())); + assertEquals("document has content","test1",convertXMLDocumentToString(dh.get())); + }else{ + assertEquals("element node ","",convertXMLDocumentToString(dh.get())); + } + } + else if(er.getType().equals(Type.JSON)){ + + JacksonHandle jh = new JacksonHandle(); + jh=er.get(jh); + + if(jh.get().isArray()){ + System.out.println("Type Array :"+jh.get().toString()); + assertEquals("array value at index 0 ",1,jh.get().get(0).asInt()); + assertEquals("array value at index 1 ",2,jh.get().get(1).asInt()); + assertEquals("array value at index 2 ",3,jh.get().get(2).asInt()); + } + else if(jh.get().isObject()){ + System.out.println("Type Object :"+jh.get().toString()); + if(jh.get().has("foo")){ + assertNull("this object also has null node",jh.get().get("testNull").textValue()); + }else if(jh.get().has("obj")) + { + assertEquals("Value of the object is ","value",jh.get().get("obj").asText()); + }else{ + assertFalse("getting a wrong object ",true); + } + + } + else if(jh.get().isNumber()){ + System.out.println("Type Number :"+jh.get().toString()); + assertEquals("Number value",1,jh.get().asInt()); + } + else if(jh.get().isNull()){ + System.out.println("Type Null :"+jh.get().toString()); + assertNull("Returned Null",jh.get().textValue()); + } + else if(jh.get().isBoolean()){ + System.out.println("Type boolean :"+jh.get().toString()); + assertTrue("Boolean value returned false",jh.get().asBoolean()); + } + else{ +// System.out.println("Running into different types than expected"); + assertFalse("Running into different types than expected",true); + } + + + + } + else if(er.getType().equals(Type.TEXTNODE)){ + assertTrue("document contains",er.getAs(String.class).equals("test1")); +// System.out.println("type txt node :"+er.getAs(String.class)); + + }else if(er.getType().equals(Type.BINARY)){ + FileHandle fh = new FileHandle(); + fh=er.get(fh); +// System.out.println("type binary :"+fh.get().length()); + assertEquals("files size",2,fh.get().length()); + }else if(er.getType().equals(Type.BOOLEAN)){ + assertTrue("Documents exist?",er.getBoolean()); +// System.out.println("type boolean:"+er.getBoolean()); + } + else if(er.getType().equals(Type.INTEGER)){ +// System.out.println("type Integer: "+er.getNumber().longValue()); + assertEquals("count of documents ",31,er.getNumber().intValue()); + } + else if(er.getType().equals(Type.STRING)){ + //There is git issue 152 + assertEquals("String?","xml",er.getString()); + System.out.println("type string: "+er.getString()); + }else if(er.getType().equals(Type.NULL)){ + //There is git issue 151 +// assertNull(er.getAs(String.class)); + System.out.println("Testing is empty sequence is NUll?"+er.getAs(String.class)); + }else if(er.getType().equals(Type.OTHER)){ + //There is git issue 151 + System.out.println("Testing is Others? "+er.getAs(String.class)); +// assertEquals("Returns OTHERs","xdmp:forest-restart#1",er.getString()); + + }else if(er.getType().equals(Type.ANYURI)){ +// System.out.println("Testing is AnyUri? "+er.getAs(String.class)); + assertEquals("Returns me a uri :","test1.xml",er.getAs(String.class)); + + }else if(er.getType().equals(Type.DATE)){ +// System.out.println("Testing is DATE? "+er.getAs(String.class)); + assertEquals("Returns me a date :","2002-03-07-07:00",er.getAs(String.class)); + }else if(er.getType().equals(Type.DATETIME)){ +// System.out.println("Testing is DATETIME? "+er.getAs(String.class)); + assertEquals("Returns me a dateTime :","2010-01-06T18:13:50.874-07:00",er.getAs(String.class)); + + }else if(er.getType().equals(Type.DECIMAL)){ +// System.out.println("Testing is Decimal? "+er.getAs(String.class)); + assertEquals("Returns me a Decimal :","10.5",er.getAs(String.class)); + + }else if(er.getType().equals(Type.DOUBLE)){ +// System.out.println("Testing is Double? "+er.getAs(String.class)); + assertEquals(1.0471975511966,er.getNumber().doubleValue(),0); + + }else if(er.getType().equals(Type.DURATION)){ + System.out.println("Testing is Duration? "+er.getAs(String.class)); +// assertEquals("Returns me a Duration :",0.4903562,er.getNumber().floatValue()); + }else if(er.getType().equals(Type.FLOAT)){ +// System.out.println("Testing is Float? "+er.getAs(String.class)); + assertEquals(20,er.getNumber().floatValue(),0); + }else if(er.getType().equals(Type.GDAY)){ +// System.out.println("Testing is GDay? "+er.getAs(String.class)); + assertEquals("Returns me a GDAY :","---01",er.getAs(String.class)); + }else if(er.getType().equals(Type.GMONTH)){ +// System.out.println("Testing is GMonth "+er.getAs(String.class)); + assertEquals("Returns me a GMONTH :","--01",er.getAs(String.class)); + }else if(er.getType().equals(Type.GMONTHDAY)){ +// System.out.println("Testing is GMonthDay? "+er.getAs(String.class)); + assertEquals("Returns me a GMONTHDAY :","--12-25-14:00",er.getAs(String.class)); + }else if(er.getType().equals(Type.GYEAR)){ +// System.out.println("Testing is GYear? "+er.getAs(String.class)); + assertEquals("Returns me a GYEAR :","2005-12:00",er.getAs(String.class)); + }else if(er.getType().equals(Type.GYEARMONTH)){ +// System.out.println("Testing is GYearMonth?1976-02 "+er.getAs(String.class)); + assertEquals("Returns me a GYEARMONTH :","1976-02",er.getAs(String.class)); + }else if(er.getType().equals(Type.HEXBINARY)){ +// System.out.println("Testing is HEXBINARY? "+er.getAs(String.class)); + assertEquals("Returns me a HEXBINARY :","BEEF",er.getAs(String.class)); + }else if(er.getType().equals(Type.QNAME)){ +// System.out.println("Testing is QNAME integer"+er.getAs(String.class)); + assertEquals("Returns me a QNAME :","integer",er.getAs(String.class)); + }else if(er.getType().equals(Type.TIME)){ +// System.out.println("Testing is TIME? "+er.getAs(String.class)); + assertEquals("Returns me a TIME :","10:00:00",er.getAs(String.class)); + }else if(er.getType().equals(Type.ATTRIBUTE)){ +// System.out.println("Testing is ATTRIBUTE? "+er.getAs(String.class)); + assertEquals("Returns me a ATTRIBUTE :","attribute",er.getAs(String.class)); + + }else if(er.getType().equals(Type.PROCESSINGINSTRUCTION)){ +// System.out.println("Testing is ProcessingInstructions? "+er.getAs(String.class)); + assertEquals("Returns me a PROCESSINGINSTRUCTION :","",er.getAs(String.class)); + }else if(er.getType().equals(Type.COMMENT)){ +// System.out.println("Testing is Comment node? "+er.getAs(String.class)); + assertEquals("Returns me a COMMENT :","",er.getAs(String.class)); + }else if(er.getType().equals(Type.BASE64BINARY)){ +// System.out.println("Testing is Base64Binary "+er.getAs(String.class)); + assertEquals("Returns me a BASE64BINARY :","DEADBEEF",er.getAs(String.class)); + }else{ + System.out.println("Got something which is not belongs to anytype we support "+er.getAs(String.class)); + assertFalse("getting in else part, missing a type ",true); + } + } + } + + //This test intended to verify a simple JS query for inserting and reading documents of different formats and returning boolean,string number types + + @Test + public void testJSReturningDifferentTypesOrder1() throws Exception { + String insertXML = "declareUpdate();" + + "var x = new NodeBuilder();" + +"x.startDocument();" + +"x.startElement(\"foo\");" + +"x.addText(\"test1\");" + +"x.endElement();" + + "x.endDocument();" + +"xdmp.documentInsert(\"test1.xml\",x.toNode())"; + String insertJSON = "declareUpdate();xdmp.documentInsert(\"test2.json\", {\"test\":\"hello\"})"; + String insertTXT = "declareUpdate();var txt= new NodeBuilder();txt.addText(\"This is a text document.\");xdmp.documentInsert(\"/test3.txt\",txt.toNode() )"; + String insertBinary="declareUpdate();var binary= new NodeBuilder();binary.addBinary(\"ABCD\");xdmp.documentInsert(\"test4.bin\",binary.toNode())"; + String query1 = "fn.exists(fn.doc())"; + String query2 = "fn.count(fn.doc())"; + String query3 = "xdmp.databaseName(xdmp.database())"; + String readDoc ="fn.doc()"; + System.out.println(insertJSON); + System.out.println(insertXML); + System.out.println(insertTXT); + System.out.println(insertBinary); + boolean response = client.newServerEval().javascript(insertXML).eval().hasNext(); +// System.out.printlnln(response); + assertFalse("Insert query return empty sequence",response); + response = client.newServerEval().javascript(insertJSON).eval().hasNext(); +// System.out.printlnln(response); + assertFalse("Insert query return empty sequence",response); + response = client.newServerEval().javascript(insertTXT).eval().hasNext(); +// System.out.printlnln(response); + assertFalse("Insert query return empty sequence",response); + response = client.newServerEval().javascript(insertBinary).eval().hasNext(); +// System.out.printlnln(response); + assertFalse("Insert query return empty sequence",response); + boolean response1 = client.newServerEval().javascript(query1).eval().next().getBoolean(); +// System.out.printlnln(response1); + assertTrue("Documents exist?",response1); + int response2 = client.newServerEval().javascript(query2).eval().next().getNumber().intValue(); +// System.out.printlnln(response2); + assertEquals("count of documents ",4,response2); + String response3 = client.newServerEval().javascript(query3).evalAs(String.class); +// System.out.printlnln(response3); + assertEquals("Content database?",dbName,response3); + ServerEvaluationCall evl = client.newServerEval(); + EvalResultIterator evr = evl.javascript(readDoc).eval(); + while(evr.hasNext()) + { + EvalResult er =evr.next(); + if(er.getType().equals(Type.XML)){ + DOMHandle dh = new DOMHandle(); + dh=er.get(dh); + assertEquals("document has content","test1",convertXMLDocumentToString(dh.get())); + } + else if(er.getType().equals(Type.JSON)){ + JacksonHandle jh = new JacksonHandle(); + jh=er.get(jh); + assertTrue("document has object?",jh.get().has("test")); + } + else if(er.getType().equals(Type.TEXTNODE)){ + assertTrue("document contains",er.getAs(String.class).equals("This is a text document.")); + + }else if(er.getType().equals(Type.BINARY)){ + FileHandle fh = new FileHandle(); + fh=er.get(fh); + assertEquals("files size",2,fh.get().length()); + + }else{ + System.out.println("Something went wrong"); + } + + } + } + //This test is intended to test eval(T handle), passing input stream handle with xqueries that retruns different types, formats + @Test + public void testJSReturningDifferentTypesOrder2() throws Exception { + + InputStream inputStream = new FileInputStream("src/test/java/com/marklogic/javaclient/data/javascriptQueries.sjs"); + InputStreamHandle ish = new InputStreamHandle(); + ish.set(inputStream); + + try{ + EvalResultIterator evr = client.newServerEval().javascript(ish).eval(); + while(evr.hasNext()) + { + EvalResult er =evr.next(); + if(er.getType().equals(Type.XML)){ + DOMHandle dh = new DOMHandle(); + dh=er.get(dh); + assertEquals("document has content","test1",convertXMLDocumentToString(dh.get())); + } + else if(er.getType().equals(Type.JSON)){ + JacksonHandle jh = new JacksonHandle(); + jh=er.get(jh); + assertTrue("document has object?",jh.get().has("test")); + } + else if(er.getType().equals(Type.TEXTNODE)){ + assertTrue("document contains",er.getAs(String.class).equals("This is a text document.")); + + }else if(er.getType().equals(Type.BINARY)){ + FileHandle fh = new FileHandle(); + fh=er.get(fh); + assertEquals("files size",2,fh.get().length()); + + }else{ + System.out.println("Something went wrong"); + } + + } + }catch(Exception e){ + throw e; + } + finally{ + inputStream.close(); + } + } + //Test is intended to test different types of variable passed to javascript from java and check return types,data types, there is a bug log against REST 30209 + @Test + public void testJSDifferentVariableTypes() throws Exception { + + DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + InputSource is = new InputSource(); + is.setCharacterStream(new StringReader("test1")); + Document doc = db.parse(is); + String jsonNode = "{ \"a\" : {\"obj\": \"value\"}, \"b\" : \"s0\",\"c1\" : 1,\"c2\" : 2,\"d\" : null,\"f\" : true,\"g\" : [\"s1\", \"s2\", \"s3\" ]}"; + System.out.println(this.convertXMLDocumentToString(doc)); + try{ + String query1 = " var results = [];" + +"var myString;" + +"var myBool ;" + +"var myInteger;" + +"var myDecimal;" + +"var myDouble ;" + +"var myFloat;" + +"var myJsonObject;" + +"var myNull;" + + "var myJsonArray;" + + "var myJsonNull;" + + "results.push(myString,myBool,myInteger,myDecimal,myDouble,myFloat,myNull,myJsonObject,myJsonArray,myJsonNull);" + +"xdmp.arrayValues(results)"; + + ServerEvaluationCall evl= client.newServerEval().javascript(query1); + evl.addVariable("myString", "xml") + .addVariable("myBool", true).addVariable("myInteger", (int)31) + .addVariable("myDecimal", 10.5).addVariable("myDouble", 1.0471975511966) + .addVariable("myFloat",20).addVariableAs("myJsonObject",new ObjectMapper().createObjectNode().put("foo", "v1").putNull("testNull")) + .addVariableAs("myNull",(String) null).addVariableAs("myJsonArray",new ObjectMapper().createArrayNode().add(1).add(2).add(3)) + .addVariableAs("myJsonNull",new ObjectMapper().createObjectNode().nullNode() ); + System.out.println(query1); + EvalResultIterator evr = evl.eval(); + this.validateReturnTypes(evr); + + }catch(Exception e){ + throw e; + } + } + + + @Test(expected = java.lang.IllegalStateException.class) + public void testMultipleJSfnOnServerEval() { + String insertQuery = "xdmp.document-insert(\"test1.xml\",test1)"; + String query1 = "fn.exists(fn:doc())"; + String query2 = "fn.count(fn:doc())"; + String query3 = "xdmp.database-name(xdmp.database())"; + + boolean response1 = client.newServerEval().javascript(query1).javascript(insertQuery).eval().next().getBoolean(); + System.out.println(response1); + int response2 = client.newServerEval().xquery(query2).eval().next().getNumber().intValue(); + System.out.println(response2); + String response3 = client.newServerEval().xquery(query3).evalAs(String.class); + System.out.println(response3); + } + + + //Issue 30209 ,external variable passing is not working so I have test cases where it test to see we can invoke a module + @Test + public void testJSReturningDifferentTypesOrder3fromModules() throws Exception { + + InputStream inputStream=null; + DatabaseClient moduleClient = DatabaseClientFactory.newClient("localhost", restPort,(restServerName+"-modules"),"admin", "admin", Authentication.DIGEST); + try{ + inputStream = new FileInputStream("src/test/java/com/marklogic/javaclient/data/javascriptQueries.sjs"); + InputStreamHandle ish = new InputStreamHandle(); + ish.set(inputStream); + DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); + metadataHandle.getPermissions().add("test-eval", Capability.UPDATE, Capability.READ,Capability.EXECUTE); + DocumentManager dm = moduleClient.newDocumentManager(); + dm.write("/data/javascriptQueries.sjs",metadataHandle,ish); + DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + InputSource is = new InputSource(); + is.setCharacterStream(new StringReader("test1")); + Document doc = db.parse(is); + ServerEvaluationCall evl = client.newServerEval().modulePath("/data/javascriptQueries.sjs"); +// evl.addNamespace("test", "http://marklogic.com/test") +// .addVariable("test:myString", "xml") +// .addVariable("myBool", true).addVariable("myInteger", (int)31) +// .addVariable("myDecimal", 10.5).addVariable("myDouble", 1.0471975511966) +// .addVariable("myFloat",20).addVariableAs("myXmlNode",new DOMHandle(doc)) +// .addVariableAs("myNull",(String) null); + EvalResultIterator evr = evl.eval(); + while(evr.hasNext()) + { + EvalResult er =evr.next(); + if(er.getType().equals(Type.XML)){ + DOMHandle dh = new DOMHandle(); + dh=er.get(dh); + assertEquals("document has content","test1",convertXMLDocumentToString(dh.get())); + } + else if(er.getType().equals(Type.JSON)){ + JacksonHandle jh = new JacksonHandle(); + jh=er.get(jh); + assertTrue("document has object?",jh.get().has("test")); + } + else if(er.getType().equals(Type.TEXTNODE)){ + assertTrue("document contains",er.getAs(String.class).equals("This is a text document.")); + + }else if(er.getType().equals(Type.BINARY)){ + FileHandle fh = new FileHandle(); + fh=er.get(fh); + assertEquals("files size",2,fh.get().length()); + + }else{ + System.out.println("Something went wrong"); + } + + } + + }catch(Exception e){ + throw e; + } + finally{ + if(inputStream != null) {inputStream.close();} + moduleClient.release(); + } + + } +} diff --git a/test-complete/src/test/java/com/marklogic/javaclient/data/javascriptQueries.sjs b/test-complete/src/test/java/com/marklogic/javaclient/data/javascriptQueries.sjs new file mode 100644 index 000000000..b8a78cf23 --- /dev/null +++ b/test-complete/src/test/java/com/marklogic/javaclient/data/javascriptQueries.sjs @@ -0,0 +1 @@ +fn.doc() \ No newline at end of file From 159b19552ed657b3986a6bef6828193837ef5cfb Mon Sep 17 00:00:00 2001 From: Ajit George Date: Wed, 29 Oct 2014 16:53:47 -0700 Subject: [PATCH 251/357] Added test cases to check patch builder methods on Json documents. --- .../javaclient/TestPartialUpdate.java | 232 +++++++++++++++++- 1 file changed, 231 insertions(+), 1 deletion(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java index 5287f1f54..1f7f28fd6 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java @@ -26,7 +26,10 @@ import com.marklogic.client.io.StringHandle; import com.marklogic.client.io.DocumentMetadataHandle.Capability; import com.marklogic.client.io.marker.DocumentPatchHandle; + +import org.json.JSONException; import org.junit.*; +import org.skyscreamer.jsonassert.JSONAssert; public class TestPartialUpdate extends BasicJavaClientREST { private static String dbName = "TestPartialUpdateDB"; @@ -842,7 +845,234 @@ public void testPartialUpdateCardinality() throws IOException // release client client.release(); - } + } + + /* Purpose: This test is used to validate all of the patch builder functions on a JSON + * document using JSONPath expressions. + * + * Function tested: replaceValue. + */ + @Test + public void testPartialUpdateReplaceValueJSON() throws IOException, JSONException + { + System.out.println("Running testPartialUpdateReplaceValueJSON"); + + String[] filenames = {"json-original.json"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "JSON"); + } + + String docId = "/partial-update/json-original.json"; + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); + patchBldr.pathLanguage(PathLanguage.JSONPATH); + + //Replace the third employee's first name. Change it to Jack. Issue #161 - Using filters causes Bad Request Exceptions. + patchBldr.replaceValue("$.employees[2].firstName", "Jack"); + + DocumentPatchHandle patchHandle = patchBldr.build(); + docMgr.patch(docId, patchHandle); + + String content = docMgr.read(docId, new StringHandle()).get(); + + System.out.println(content); + + String exp="{\"employees\": [{\"firstName\":\"John\", \"lastName\":\"Doe\"}," + + "{\"firstName\":\"Ann\", \"lastName\":\"Smith\"}," + + "{\"firstName\":\"Jack\", \"lastName\":\"Foo\"}]}"; + JSONAssert.assertEquals(exp, content, false); + + // release client + client.release(); + } + + /* Purpose: This test is used to validate all of the patch builder functions on a JSON + * document using JSONPath expressions. + * + * Functions tested : replaceFragment. + */ + @Test + public void testPartialUpdateReplaceFragmentJSON() throws IOException, JSONException + { + System.out.println("Running testPartialUpdateReplaceValueJSON"); + + String[] filenames = {"json-original.json"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "JSON"); + } + + String docId = "/partial-update/json-original.json"; + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); + patchBldr.pathLanguage(PathLanguage.JSONPATH); + + //Replace the third employee. Issue #161 - Using filters causes Bad Request Exceptions. + patchBldr.replaceFragment("$.employees[2]", "{\"firstName\":\"Albert\", \"lastName\":\"Einstein\"}"); + + DocumentPatchHandle patchHandle = patchBldr.build(); + docMgr.patch(docId, patchHandle); + + String content = docMgr.read(docId, new StringHandle()).get(); + + System.out.println(content); + + String exp="{\"employees\": [{\"firstName\":\"John\", \"lastName\":\"Doe\"}," + + "{\"firstName\":\"Ann\", \"lastName\":\"Smith\"}," + + "{\"firstName\":\"Albert\", \"lastName\":\"Einstein\"}]}"; + JSONAssert.assertEquals(exp, content, false); + + // release client + client.release(); + } + + /* Purpose: This test is used to validate all of the patch builder functions on a JSON + * document using JSONPath expressions. + * + * Functions tested : replaceInsertFragment. An new fragment is inserted when unknown index is used. + */ + @Test + public void testPartialUpdateReplaceInsertFragmentNewJSON() throws IOException, JSONException + { + System.out.println("Running testPartialUpdateReplaceInsertFragmentExistingJSON"); + + String[] filenames = {"json-original.json"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "JSON"); + } + + String docId = "/partial-update/json-original.json"; + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); + patchBldr.pathLanguage(PathLanguage.JSONPATH); + + //Mark an unknown location in argument 1, and then insert new node relative to argument 2. + patchBldr.replaceInsertFragment("$.employees[3]", "$.employees[0]", Position.BEFORE,"{\"firstName\":\"Albert\", \"lastName\":\"Einstein\"}"); + + DocumentPatchHandle patchHandle = patchBldr.build(); + docMgr.patch(docId, patchHandle); + + String content = docMgr.read(docId, new StringHandle()).get(); + + System.out.println(content); + + String exp="{\"employees\": [{\"firstName\":\"John\", \"lastName\":\"Doe\"}," + + "{\"firstName\":\"Ann\", \"lastName\":\"Smith\"}," + + "{\"firstName\":\"Albert\", \"lastName\":\"Einstein\"}," + + "{\"firstName\":\"Bob\", \"lastName\":\"Foo\"}]}"; + JSONAssert.assertEquals(exp, content, false); + + // release client + client.release(); + } + + /* Purpose: This test is used to validate all of the patch builder functions on a JSON + * document using JSONPath expressions. + * + * Functions tested : replaceInsertFragment. An existing fragment replaced with another fragment. + */ + @Test + public void testPartialUpdateReplaceInsertFragmentExistingJSON() throws IOException, JSONException + { + System.out.println("Running testPartialUpdateReplaceInsertFragmentExistingJSON"); + + String[] filenames = {"json-original.json"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "JSON"); + } + + String docId = "/partial-update/json-original.json"; + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); + patchBldr.pathLanguage(PathLanguage.JSONPATH); + + //Replace the third employee. Issue #161 - Using filters causes Bad Request Exceptions. + patchBldr.replaceInsertFragment("$.employees[2]", "$.employees[2]", Position.LAST_CHILD,"{\"firstName\":\"Albert\", \"lastName\":\"Einstein\"}"); + + DocumentPatchHandle patchHandle = patchBldr.build(); + docMgr.patch(docId, patchHandle); + + String content = docMgr.read(docId, new StringHandle()).get(); + + System.out.println(content); + + String exp="{\"employees\": [{\"firstName\":\"John\", \"lastName\":\"Doe\"}," + + "{\"firstName\":\"Ann\", \"lastName\":\"Smith\"}," + + "{\"firstName\":\"Albert\", \"lastName\":\"Einstein\"}]}"; + JSONAssert.assertEquals(exp, content, false); + + // release client + client.release(); + } + + /* Purpose: This test is used to validate all of the patch builder functions on a JSON + * document using JSONPath expressions. + * + * Functions tested are: delete, replaceApply, replaceFragment, replaceInsertFragment and replaceValue. + */ + + /* Purpose: This test is used to validate all of the patch builder functions on a JSON + * document using JSONPath expressions. + * + * Function tested: delete. + */ + @Test + public void testPartialUpdateDeleteJSON() throws IOException, JSONException + { + System.out.println("Running testPartialUpdateReplaceValueJSON"); + + String[] filenames = {"json-original.json"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "JSON"); + } + + String docId = "/partial-update/json-original.json"; + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder(); + patchBldr.pathLanguage(PathLanguage.JSONPATH); + + //Delete the third employee's first name. Issue #161 - Using filters causes Bad Request Exceptions. + patchBldr.delete("$.employees[2].firstName", DocumentMetadataPatchBuilder.Cardinality.ZERO_OR_MORE); + + DocumentPatchHandle patchHandle = patchBldr.build(); + docMgr.patch(docId, patchHandle); + + String content = docMgr.read(docId, new StringHandle()).get(); + + System.out.println(content); + + String exp="{\"employees\": [{\"firstName\":\"John\", \"lastName\":\"Doe\"}," + + "{\"firstName\":\"Ann\", \"lastName\":\"Smith\"}," + + "{\"lastName\":\"Foo\"}]}"; + JSONAssert.assertEquals(exp, content, false); + + // release client + client.release(); + } @AfterClass public static void tearDown() throws Exception From ef185ca32c709cf8d7807ead62584b418b5099df Mon Sep 17 00:00:00 2001 From: Ajit George Date: Wed, 29 Oct 2014 16:54:57 -0700 Subject: [PATCH 252/357] Removed comments. --- .../java/com/marklogic/javaclient/TestPartialUpdate.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java index 1f7f28fd6..50e18d7b9 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java @@ -1024,12 +1024,6 @@ public void testPartialUpdateReplaceInsertFragmentExistingJSON() throws IOExcept client.release(); } - /* Purpose: This test is used to validate all of the patch builder functions on a JSON - * document using JSONPath expressions. - * - * Functions tested are: delete, replaceApply, replaceFragment, replaceInsertFragment and replaceValue. - */ - /* Purpose: This test is used to validate all of the patch builder functions on a JSON * document using JSONPath expressions. * From 125f1d01e5f8992fdb55a8d5bdbe8f1fc27e156e Mon Sep 17 00:00:00 2001 From: skottam Date: Thu, 30 Oct 2014 10:49:38 -0700 Subject: [PATCH 253/357] updating the pojoquerydefinition --- .../marklogic/javaclient/TestPOJOWithStrucQD.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithStrucQD.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithStrucQD.java index 940840c0c..d11115b1e 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithStrucQD.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithStrucQD.java @@ -22,6 +22,7 @@ import com.marklogic.client.io.SearchHandle; import com.marklogic.client.io.StringHandle; import com.marklogic.client.pojo.PojoPage; +import com.marklogic.client.pojo.PojoQueryDefinition; import com.marklogic.client.pojo.PojoRepository; import com.marklogic.client.query.MatchDocumentSummary; import com.marklogic.client.query.QueryManager; @@ -165,7 +166,7 @@ public void testPOJOSearchWithSearchHandle() { StructuredQueryBuilder qb = new StructuredQueryBuilder(); StructuredQueryDefinition q1 =qb.range(qb.pathIndex("com.marklogic.javaclient.Artifact/inventory"), "xs:long",Operator.GT, 1010); - StructuredQueryDefinition qd = qb.and(q1,qb.range(qb.pathIndex("com.marklogic.javaclient.Artifact/inventory"), "xs:long",Operator.LE, 1110),qb.collection("even")); + PojoQueryDefinition qd = qb.and(q1,qb.range(qb.pathIndex("com.marklogic.javaclient.Artifact/inventory"), "xs:long",Operator.LE, 1110),qb.collection("even")); SearchHandle results = new SearchHandle(); products.setPageLength(10); p = products.search(qd, 1,results); @@ -212,7 +213,7 @@ public void testPOJOSearchWithJacksonHandle() { ; StructuredQueryBuilder qb = new StructuredQueryBuilder(); StructuredQueryDefinition q1 =qb.containerQuery(qb.jsonProperty("name"),qb.term("special") ); - StructuredQueryDefinition qd = qb.and(q1,qb.word(qb.jsonProperty("name"), "acme")); + PojoQueryDefinition qd = qb.and(q1,qb.word(qb.jsonProperty("name"), "acme")); JacksonHandle results = new JacksonHandle(); p = products.search(qd, 1,results); products.setPageLength(11); @@ -251,7 +252,7 @@ public void testPOJOSearchWithStringHandle() throws JsonProcessingException, IOE PojoPage p; this.loadSimplePojos(products); StructuredQueryBuilder qb = new StructuredQueryBuilder(); - StructuredQueryDefinition qd =qb.value(qb.jsonProperty("id"), 5,10,15,20,25,30); + PojoQueryDefinition qd =qb.value(qb.jsonProperty("id"), 5,10,15,20,25,30); // StructuredQueryDefinition qd = qb.and(q1,qb.range(qb.pathIndex("com.marklogic.javaclient.Artifact/inventory"), "xs:long",Operator.LE, 1110),qb.collection("even")); StringHandle results = new StringHandle(); @@ -303,8 +304,8 @@ public void testPOJOSearchWithRawXMLStructQD() { " "+ ""; StringHandle rh = new StringHandle(rawXMLQuery); - RawStructuredQueryDefinition qd = - queryMgr.newRawStructuredQueryDefinition(rh); + PojoQueryDefinition qd = + (PojoQueryDefinition)queryMgr.newRawStructuredQueryDefinition(rh); JacksonHandle results = new JacksonHandle(); p = products.search(qd, 1,results); products.setPageLength(11); @@ -368,8 +369,8 @@ public void testPOJOSearchWithRawJSONStructQD() { ObjectNode mainNode = mapper.createObjectNode(); mainNode.set("query", queryArrayNode); jh.set(mainNode); - RawStructuredQueryDefinition qd = - queryMgr.newRawStructuredQueryDefinition(jh); + PojoQueryDefinition qd = + (PojoQueryDefinition)queryMgr.newRawStructuredQueryDefinition(jh); JacksonHandle results = new JacksonHandle(); p = products.search(qd, 1,results); From ccdef5077ec2023415de8e1b87608417607c7d76 Mon Sep 17 00:00:00 2001 From: sammefford Date: Thu, 30 Oct 2014 17:04:19 -0600 Subject: [PATCH 254/357] fix #103--on bulk read DocumentPage was not getting initialized all the way --- .../marklogic/client/impl/JerseyServices.java | 8 ++++- .../client/test/BulkReadWriteTest.java | 30 +++++++++++++++---- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/marklogic/client/impl/JerseyServices.java b/src/main/java/com/marklogic/client/impl/JerseyServices.java index c7c53bcbd..108be5b22 100644 --- a/src/main/java/com/marklogic/client/impl/JerseyServices.java +++ b/src/main/java/com/marklogic/client/impl/JerseyServices.java @@ -820,8 +820,14 @@ private JerseyResultIterator getBulkDocumentsImpl(RequestLogger reqlog, for (String uri: uris) { params.add("uri", uri); } - return getIteratedResourceImpl(DefaultJerseyResultIterator.class, + JerseyResultIterator iterator = getIteratedResourceImpl(DefaultJerseyResultIterator.class, reqlog, path, params, MultiPartMediaTypes.MULTIPART_MIXED); + if ( iterator.getStart() == -1 ) iterator.setStart(1); + if ( iterator.getSize() != -1 ) { + if ( iterator.getPageSize() == -1 ) iterator.setPageSize(iterator.getSize()); + if ( iterator.getTotalSize() == -1 ) iterator.setTotalSize(iterator.getSize()); + } + return iterator; } private JerseyResultIterator getBulkDocumentsImpl(RequestLogger reqlog, diff --git a/src/test/java/com/marklogic/client/test/BulkReadWriteTest.java b/src/test/java/com/marklogic/client/test/BulkReadWriteTest.java index 73adeb862..6f1cead7b 100644 --- a/src/test/java/com/marklogic/client/test/BulkReadWriteTest.java +++ b/src/test/java/com/marklogic/client/test/BulkReadWriteTest.java @@ -161,12 +161,22 @@ public void testB_BulkRead() { DocumentPage page = docMgr.read(DIRECTORY + "1016670.xml", DIRECTORY + "108410.xml", DIRECTORY + "1205733.xml"); int numRead = 0; - while ( page.hasNext() ) { - DocumentRecord record = page.next(); + for ( DocumentRecord record : page ) { validateRecord(record); numRead++; } + assertEquals("Should have results", true, page.hasContent()); assertEquals("Failed to read number of records expected", 3, numRead); + assertEquals("Failed to report number of records expected", 3, page.size()); + assertEquals("No previous page", false, page.hasPreviousPage()); + assertEquals("Only one page", false, page.hasNextPage()); + assertEquals("Only one page", true, page.isFirstPage()); + assertEquals("Only one page", true, page.isLastPage()); + assertEquals("Wrong page", 1, page.getPageNumber()); + assertEquals("Wrong page size", 3, page.getPageSize()); + assertEquals("Wrong start", 1, page.getStart()); + assertEquals("Wrong totalPages", 1, page.getTotalPages()); + assertEquals("Wrong estimate", 3, page.getTotalSize()); } @Test @@ -177,14 +187,24 @@ public void testC_BulkSearch() { int pageLength = 100; docMgr.setPageLength(pageLength); DocumentPage page = docMgr.search(new StructuredQueryBuilder().directory(1, DIRECTORY), 1, searchHandle); - //DocumentPage page = docMgr.search(new StructuredQueryBuilder().directory(1, DIRECTORY), 1); - while ( page.hasNext() ) { - DocumentRecord record = page.next(); + for ( DocumentRecord record : page ) { validateRecord(record); } assertEquals("Failed to find number of records expected", RECORDS_EXPECTED, page.getTotalSize()); assertEquals("SearchHandle failed to report number of records expected", RECORDS_EXPECTED, searchHandle.getTotalResults()); assertEquals("SearchHandle failed to report pageLength expected", pageLength, searchHandle.getPageLength()); + assertEquals("Should have results", true, page.hasContent()); + int expected = RECORDS_EXPECTED > pageLength ? pageLength : RECORDS_EXPECTED; + assertEquals("Failed to report number of records expected", expected, page.size()); + assertEquals("No previous page", false, page.hasPreviousPage()); + assertEquals("Only one page", RECORDS_EXPECTED > pageLength, page.hasNextPage()); + assertEquals("Only one page", true, page.isFirstPage()); + assertEquals("Only one page", page.hasNextPage() == false, page.isLastPage()); + assertEquals("Wrong page", 1, page.getPageNumber()); + assertEquals("Wrong page size", pageLength, page.getPageSize()); + assertEquals("Wrong start", 1, page.getStart()); + double totalPagesExpected = Math.ceil((double) RECORDS_EXPECTED/(double) pageLength); + assertEquals("Wrong totalPages", totalPagesExpected, page.getTotalPages(), .01); } //public void testMixedLoad() { From 9df4176e4d6b8a8ceb39c79fb666e1780f07d6c9 Mon Sep 17 00:00:00 2001 From: sammefford Date: Thu, 30 Oct 2014 17:05:15 -0600 Subject: [PATCH 255/357] fix #75, calculation was assuming 0-based indexes on pages, but we're doing 1-based --- src/main/java/com/marklogic/client/impl/BasicPage.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/marklogic/client/impl/BasicPage.java b/src/main/java/com/marklogic/client/impl/BasicPage.java index 4fcbfef98..662afa81c 100644 --- a/src/main/java/com/marklogic/client/impl/BasicPage.java +++ b/src/main/java/com/marklogic/client/impl/BasicPage.java @@ -103,7 +103,7 @@ public boolean hasNextPage() { } public boolean hasPreviousPage() { - return getPageNumber() > 0; + return getPageNumber() > 1; } public long getPageNumber() { From 254146a0da9a4705666688241f2fb040a7363b3d Mon Sep 17 00:00:00 2001 From: sammefford Date: Thu, 30 Oct 2014 17:08:46 -0600 Subject: [PATCH 256/357] uncomment javascript eval assertions now that I know we can user xdmp.arrayValues() --- .../com/marklogic/client/test/EvalTest.java | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/test/java/com/marklogic/client/test/EvalTest.java b/src/test/java/com/marklogic/client/test/EvalTest.java index e6b04b87b..1d0013654 100644 --- a/src/test/java/com/marklogic/client/test/EvalTest.java +++ b/src/test/java/com/marklogic/client/test/EvalTest.java @@ -91,7 +91,7 @@ public void evalTest1() throws ParserConfigurationException, DatatypeConfigurati "var myInteger;" + "var myDouble;" + "var myDate;" + - "[myString, myArray, myObject, myBool, myInteger, myDouble, myDate]") + "xdmp.arrayValues([myString, myArray, myObject, myBool, myInteger, myDouble, myDate])") // String is directly supported in any EvalBuilder .addVariable("myString", "Mars") // ArrayNode extends JSONNode which is mapped to implicitly use JacksonHandle @@ -106,23 +106,22 @@ public void evalTest1() throws ParserConfigurationException, DatatypeConfigurati DatatypeFactory.newInstance().newXMLGregorianCalendar(septFirst).toString()); EvalResultIterator results = query.eval(); try { - // TOOD: uncomment the following when we fix http://bugtrack.marklogic.com/29735 -// assertEquals("myString should = 'Mars'", "Mars", results.next().getAs(String.class)); -// assertEquals("myArray should = [\"item1\",\"item2\"]", -// new ObjectMapper().readTree("[\"item1\",\"item2\"]"), -// results.next().getAs(JsonNode.class)); -// assertEquals("myObject should = {\"item1\":\"value1\"}", -// new ObjectMapper().readTree("{\"item1\":\"value1\"}"), -// results.next().getAs(JsonNode.class)); -// assertEquals("myBool should = true", true, results.next().getBoolean()); -// assertEquals("myInteger should = 123", 123, -// results.next().getNumber().intValue()); -// assertEquals("myDouble should = 1.1", 1.1, -// results.next().getNumber().doubleValue(), .001); -// // the same format we sent in (from javax.xml.datatype.XMLGregorianCalendar.toString()) -// assertEquals("myDate should = '2014-09-01T00:00:00.000+02:00'", "2014-09-01T00:00:00.000+02:00", -// results.next().getString()); - } finally { results.close(); } + assertEquals("myString looks wrong", "Mars", results.next().getAs(String.class)); + assertEquals("myArray looks wrong", + new ObjectMapper().readTree("[\"item1\",\"item2\"]"), + results.next().getAs(JsonNode.class)); + assertEquals("myObject looks wrong", + new ObjectMapper().readTree("{\"item1\":\"value1\"}"), + results.next().getAs(JsonNode.class)); + assertEquals("myBool looks wrong", true, results.next().getBoolean()); + assertEquals("myInteger looks wrong", 123, + results.next().getNumber().intValue()); + assertEquals("myDouble looks wrong", 1.1, + results.next().getNumber().doubleValue(), .001); + // the same format we sent in (from javax.xml.datatype.XMLGregorianCalendar.toString()) + assertEquals("myDate looks wrong", "2014-09-01T00:00:00.000+02:00", + results.next().getString()); + } finally { results.close(); } // accept and return each XML variable type so use MultiPartResponsePage InputStreamHandle xquery = new InputStreamHandle( From f34ccf1c239605271c511e63aba4d6b15e51a5fb Mon Sep 17 00:00:00 2001 From: sammefford Date: Thu, 30 Oct 2014 17:11:23 -0600 Subject: [PATCH 257/357] looks like for now we need xdmp:eval-in to use REST eval https://bugtrack.marklogic.com/30090 --- src/test/resources/bootstrap.xqy | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test/resources/bootstrap.xqy b/src/test/resources/bootstrap.xqy index aeaba0f91..242a8c23b 100644 --- a/src/test/resources/bootstrap.xqy +++ b/src/test/resources/bootstrap.xqy @@ -441,6 +441,8 @@ declare function bootstrap:security-config() { 'sec:privilege-add-roles("http://marklogic.com/xdmp/privileges/xdbc-eval", "execute", "rest-evaluator")'), bootstrap:security-eval( 'sec:privilege-add-roles("http://marklogic.com/xdmp/privileges/xdbc-eval-in", "execute", "rest-evaluator")'), + bootstrap:security-eval( + 'sec:privilege-add-roles("http://marklogic.com/xdmp/privileges/xdmp-eval-in", "execute", "rest-evaluator")'), bootstrap:security-eval( 'sec:privilege-add-roles("http://marklogic.com/xdmp/privileges/xdbc-invoke", "execute", "rest-evaluator")'), bootstrap:security-eval( From 26360ae19bd2db33512c2c901f80e2876c6bb0e1 Mon Sep 17 00:00:00 2001 From: sammefford Date: Thu, 30 Oct 2014 22:47:39 -0600 Subject: [PATCH 258/357] improve error for #93, attempt to make it clear that you can't deserialize an empty class, remove some unused methods --- .../client/MarkLogicBindingException.java | 5 + .../client/impl/PojoRepositoryImpl.java | 36 ++++- .../marklogic/client/pojo/PojoRepository.java | 4 - .../marklogic/client/test/PojoFacadeTest.java | 134 ++++++++++++++++-- 4 files changed, 162 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/marklogic/client/MarkLogicBindingException.java b/src/main/java/com/marklogic/client/MarkLogicBindingException.java index 8d27c8d96..8b8b350f7 100644 --- a/src/main/java/com/marklogic/client/MarkLogicBindingException.java +++ b/src/main/java/com/marklogic/client/MarkLogicBindingException.java @@ -29,7 +29,12 @@ public class MarkLogicBindingException extends RuntimeException { static final private Logger logger = LoggerFactory .getLogger(MarkLogicBindingException.class); + public MarkLogicBindingException(String message, Throwable e) { + super(message, e); + } + public MarkLogicBindingException(Exception e) { + super(e); e.printStackTrace(); } diff --git a/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java b/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java index 0ecd0fb11..e98eb7863 100644 --- a/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java +++ b/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java @@ -16,6 +16,7 @@ package com.marklogic.client.impl; import com.marklogic.client.DatabaseClient; +import com.marklogic.client.MarkLogicBindingException; import com.marklogic.client.MarkLogicInternalException; import com.marklogic.client.Transaction; import com.marklogic.client.document.DocumentWriteSet; @@ -35,11 +36,13 @@ import com.marklogic.client.query.QueryManager; import com.marklogic.client.query.QueryManager.QueryView; import com.marklogic.client.query.StructuredQueryDefinition; +import com.sun.jersey.api.client.ClientHandlerException; import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.BeanDescription; import com.fasterxml.jackson.databind.JavaType; +import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationConfig; import com.fasterxml.jackson.databind.SerializationFeature; @@ -136,9 +139,31 @@ public void write(T entity, Transaction transaction, String... collections) { } DocumentWriteSet writeSet = docMgr.newWriteSet(); writeSet.add(createUri(entity), metadataHandle, contentHandle); - docMgr.write(writeSet, transaction); + try { + docMgr.write(writeSet, transaction); + } catch(ClientHandlerException e) { + checkForEmptyBeans(e); + throw e; + } } + private void checkForEmptyBeans(Throwable e) { + Throwable cause = e.getCause(); + if ( cause != null ) { + if ( cause instanceof JsonMappingException && + cause.getMessage() != null && + cause.getMessage().contains("SerializationFeature.FAIL_ON_EMPTY_BEANS") ) + { + throw new MarkLogicBindingException( + "Each of your pojo beans and descendent beans must have public fields or paired get/set methods", + cause); + } else { + checkForEmptyBeans(cause); + } + } + } + + public boolean exists(ID id) { return docMgr.exists(createUri(id)) != null; } @@ -271,11 +296,12 @@ public void setSearchView(QueryView view) { docMgr.setSearchView(view); } - public void defineIdProperty(String propertyName) { + public ObjectMapper getObjectMapper() { + return objectMapper; } - - public DatabaseClient getDatabaseClient() { - return client; + + public void setObjectMapper(ObjectMapper objectMapper) { + this.objectMapper = objectMapper; } private PojoQueryDefinition wrapQuery(PojoQueryDefinition query) { diff --git a/src/main/java/com/marklogic/client/pojo/PojoRepository.java b/src/main/java/com/marklogic/client/pojo/PojoRepository.java index ee749be0a..e3b98decd 100644 --- a/src/main/java/com/marklogic/client/pojo/PojoRepository.java +++ b/src/main/java/com/marklogic/client/pojo/PojoRepository.java @@ -69,8 +69,4 @@ public interface PojoRepository { public long getPageLength(); // default: 50 public void setPageLength(long length); - - public void defineIdProperty(String fieldName); - - public DatabaseClient getDatabaseClient(); } diff --git a/src/test/java/com/marklogic/client/test/PojoFacadeTest.java b/src/test/java/com/marklogic/client/test/PojoFacadeTest.java index e0a7060c3..0ffefbd89 100644 --- a/src/test/java/com/marklogic/client/test/PojoFacadeTest.java +++ b/src/test/java/com/marklogic/client/test/PojoFacadeTest.java @@ -15,10 +15,9 @@ */ package com.marklogic.client.test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; +import java.io.IOException; import java.util.Arrays; import java.util.Calendar; import java.util.GregorianCalendar; @@ -31,22 +30,33 @@ import org.junit.BeforeClass; import org.junit.Test; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; +import com.marklogic.client.document.DocumentPage; +import com.marklogic.client.document.DocumentRecord; +import com.marklogic.client.document.DocumentWriteSet; +import com.marklogic.client.document.TextDocumentManager; +import com.marklogic.client.io.Format; +import com.marklogic.client.io.SearchHandle; +import com.marklogic.client.io.StringHandle; import com.marklogic.client.pojo.PojoPage; import com.marklogic.client.pojo.PojoRepository; import com.marklogic.client.pojo.PojoQueryDefinition; +import com.marklogic.client.query.MatchDocumentSummary; +import com.marklogic.client.query.QueryManager; +import com.marklogic.client.query.QueryManager.QueryView; import com.marklogic.client.query.StringQueryDefinition; import com.marklogic.client.query.StructuredQueryBuilder; import com.marklogic.client.pojo.PojoQueryBuilder.Operator; import com.marklogic.client.pojo.PojoQueryBuilder; import com.marklogic.client.pojo.annotation.Id; +import com.marklogic.client.impl.PojoRepositoryImpl; import com.marklogic.client.test.BulkReadWriteTest; import com.marklogic.client.test.BulkReadWriteTest.CityWriter; import com.marklogic.client.test.PojoFacadeTest.TimeTest; -import static com.marklogic.client.test.BulkReadWriteTest.DIRECTORY; - @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class PojoFacadeTest { private static final int MAX_TO_WRITE = 100; @@ -56,7 +66,7 @@ public class PojoFacadeTest { public static void beforeClass() { Common.connect(); cities = Common.client.newPojoRepository(City.class, Integer.class); - //System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); } @AfterClass public static void afterClass() { @@ -79,6 +89,112 @@ public void setNumRecords(int numRecords) { } } + public interface Citizen{ + public String getNationality(); + public void setNationality(String countryName); + } + public static class Address { + public String streetName,state,country; + public long zip; + public double lattitude,longitude; + public void setStreetName(String stName){ + this.streetName=stName; + } + public void setState(String stateName){ + this.state=stateName; + } + public void setCountry(String countryName){ + this.country=countryName; + } + public void setZip(long zipCode){ + this.zip=zipCode; + } + public void setLattitude(double lat){ + this.lattitude=lat; + } + public void setLongitude(double longitude){ + this.longitude=longitude; + } + } + public static class Person implements Citizen { + String name,emailId,nationality; + long phone; + Address address; + public void setName(String name){this.name=name;} + public void setAddress(Address address){this.address = address;} + public void setEmailId(String emailId){ this.emailId = emailId;} + public void setPhone(long ph){this.phone=ph;} + + @Override + public void setNationality(String countryName){ + this.nationality= countryName; + } + @Override + public String getNationality(){ + return this.nationality; + } + public String getName(){ + return this.name; + } + public Address getAddress(){ + return this.address; + } + public String getEmailId(){ + return this.emailId; + } + public long getPhone(){ + return this.phone; + } + } + public enum std_status{senior,junior,fresher }; + public static class Student extends Person{ + @Id + public long studId; + std_status classStatus; + + public void setStudId(long id){ + this.studId=id; + } + public void setclassStatus(std_status classStatus){ + this.classStatus = classStatus; + } + public std_status getclassStatus(){ + return this.classStatus; + } + public long getStudId(){ + return this.studId; + } + } + + @Test + public void testIssue_93() throws Exception { + PojoRepository students = Common.client.newPojoRepository(Student.class, Long.class); + long id=1; + Student stud = new Student(); + stud.setName("Student1"); + stud.setStudId(id); + Address adr = new Address(); + adr.setCountry("USA"); + adr.setState("CA"); + adr.setZip(94070); + adr.setStreetName("1 tassman"); + stud.setAddress(adr); + stud.setEmailId("stud@gmail.com"); + stud.setPhone(6602345); + stud.setNationality("Indian"); + stud.setclassStatus(std_status.junior); + + ObjectMapper objectMapper = ((PojoRepositoryImpl) students).getObjectMapper(); + String value = objectMapper.writeValueAsString(stud); + Student stud2 = objectMapper.readValue(value, Student.class); + students.write(stud, "students"); + + Student student1 = students.read(id); + assertEquals("Student id", student1.getStudId(), stud.getStudId()); + assertEquals("Zip code", student1.getAddress().zip, stud.getAddress().zip); + assertEquals("class status", student1.getclassStatus(), stud.getclassStatus()); + } + @Test public void testA_LoadPojos() throws Exception { BulkReadWriteTest.loadCities(new PojoCityWriter()); @@ -384,7 +500,7 @@ public void testE_IndexNumberAsString() throws Exception { } public static class TimeTest { - @Id public String id; + @Id public String id; public Calendar timeTest; public TimeTest() {} @@ -406,7 +522,7 @@ public void testF_DateTime() { TimeTest timeTest1FromDb = times.read("1"); assertEquals("Times should be equal", timeTest1.timeTest.getTime().getTime(), - timeTest1FromDb.timeTest.getTime().getTime()); + timeTest1FromDb.timeTest.getTime().getTime()); GregorianCalendar septFirstGMT = new GregorianCalendar(TimeZone.getTimeZone("GMT")); septFirstGMT.set(2014, Calendar.SEPTEMBER, 1, 12, 0, 0); @@ -447,6 +563,8 @@ private static void cleanUp() { products2.deleteAll(); PojoRepository timeTests = Common.client.newPojoRepository(TimeTest.class, String.class); timeTests.deleteAll(); + PojoRepository students = Common.client.newPojoRepository(Student.class, Long.class); + students.deleteAll(); cities.deleteAll(); } } From 23b7cebd5ef3a034608d51fea53bf34e556ed23a Mon Sep 17 00:00:00 2001 From: sammefford Date: Fri, 31 Oct 2014 09:48:02 -0600 Subject: [PATCH 259/357] fix #104, add transaction overload to delete, count, exists methods; clean up imports; add override annotations; add missing javadocs --- .../client/impl/PojoRepositoryImpl.java | 157 ++++++++++++------ .../marklogic/client/pojo/PojoRepository.java | 94 ++++++++++- 2 files changed, 199 insertions(+), 52 deletions(-) diff --git a/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java b/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java index e98eb7863..f0d2dc3a3 100644 --- a/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java +++ b/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java @@ -15,57 +15,51 @@ */ package com.marklogic.client.impl; +import java.io.Serializable; +import java.io.UnsupportedEncodingException; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.net.URLEncoder; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.TimeZone; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.BeanDescription; +import com.fasterxml.jackson.databind.JavaType; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationConfig; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition; import com.marklogic.client.DatabaseClient; import com.marklogic.client.MarkLogicBindingException; import com.marklogic.client.MarkLogicInternalException; import com.marklogic.client.Transaction; +import com.marklogic.client.document.DocumentPage; import com.marklogic.client.document.DocumentWriteSet; import com.marklogic.client.document.JSONDocumentManager; -import com.marklogic.client.document.DocumentPage; import com.marklogic.client.io.DocumentMetadataHandle; -import com.marklogic.client.io.Format; import com.marklogic.client.io.JacksonDatabindHandle; import com.marklogic.client.io.SearchHandle; import com.marklogic.client.io.marker.SearchReadHandle; import com.marklogic.client.pojo.PojoPage; import com.marklogic.client.pojo.PojoQueryBuilder; +import com.marklogic.client.pojo.PojoQueryDefinition; import com.marklogic.client.pojo.PojoRepository; import com.marklogic.client.pojo.annotation.Id; -import com.marklogic.client.pojo.PojoQueryDefinition; import com.marklogic.client.query.DeleteQueryDefinition; import com.marklogic.client.query.QueryManager; import com.marklogic.client.query.QueryManager.QueryView; -import com.marklogic.client.query.StructuredQueryDefinition; import com.sun.jersey.api.client.ClientHandlerException; -import com.fasterxml.jackson.annotation.JsonTypeInfo; -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.databind.BeanDescription; -import com.fasterxml.jackson.databind.JavaType; -import com.fasterxml.jackson.databind.JsonMappingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationConfig; -import com.fasterxml.jackson.databind.SerializationFeature; -import com.fasterxml.jackson.databind.introspect.AnnotatedParameter; -import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition; - -import java.io.Serializable; -import java.io.UnsupportedEncodingException; -import java.lang.annotation.Annotation; -import java.lang.reflect.Method; -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; -import java.net.URLEncoder; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Set; -import java.util.TimeZone; -import java.util.regex.Matcher; -import java.util.regex.Pattern; public class PojoRepositoryImpl implements PojoRepository @@ -74,11 +68,13 @@ public class PojoRepositoryImpl private DatabaseClient client; private Class entityClass; + @SuppressWarnings("unused") private Class idClass; private JSONDocumentManager docMgr; private PojoQueryBuilder qb; private Method idMethod; private Field idProperty; + @SuppressWarnings("unused") private String idPropertyName; private static final String ISO_8601_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"; private static SimpleDateFormat simpleDateFormat8601 = new SimpleDateFormat(ISO_8601_FORMAT); @@ -119,18 +115,22 @@ public class PojoRepositoryImpl } } + @Override public void write(T entity) { - write(entity, null, null); + write(entity, null, (String[]) null); } + @Override public void write(T entity, String... collections) { write(entity, null, collections); } + @Override public void write(T entity, Transaction transaction) { - write(entity, transaction, null); + write(entity, transaction, (String[]) null); } + @Override public void write(T entity, Transaction transaction, String... collections) { if ( entity == null ) return; - JacksonDatabindHandle contentHandle = new JacksonDatabindHandle(entity); + JacksonDatabindHandle contentHandle = new JacksonDatabindHandle(entity); contentHandle.setMapper(objectMapper); DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); metadataHandle = metadataHandle.withCollections(entityClass.getName()); @@ -164,42 +164,81 @@ private void checkForEmptyBeans(Throwable e) { } + @Override public boolean exists(ID id) { return docMgr.exists(createUri(id)) != null; } + @Override + public boolean exists(ID id, Transaction transaction) { + return docMgr.exists(createUri(id), transaction) != null; + } + + @Override public long count() { - return count((PojoQueryDefinition) null); + return count((PojoQueryDefinition) null, null); } + @Override public long count(String... collections) { + return count((PojoQueryDefinition) null, null); + } + + @Override + public long count(PojoQueryDefinition query) { + return count((PojoQueryDefinition) null, null); + } + + @Override + public long count(Transaction transaction) { + return count((PojoQueryDefinition) null, transaction); + } + + @Override + public long count(String[] collections, Transaction transaction) { if ( collections != null && collections.length > 0 ) { if ( collections.length > 1 || collections[0] != null ) { - return count(qb.collection(collections)); + return count(qb.collection(collections), transaction); } } - return count((PojoQueryDefinition) null); + return count((PojoQueryDefinition) null, transaction); } - public long count(PojoQueryDefinition query) { + + @Override + public long count(PojoQueryDefinition query, Transaction transaction) { long pageLength = getPageLength(); setPageLength(0); - PojoPage page = search(query, 1); + PojoPage page = search(query, 1, transaction); setPageLength(pageLength); return page.getTotalSize(); } - + + @Override public void delete(ID... ids) { + delete(ids, null); + } + + @Override + public void delete(ID[] ids, Transaction transaction) { for ( ID id : ids ) { - docMgr.delete(createUri(id)); + docMgr.delete(createUri(id), transaction); } } + + @Override public void deleteAll() { + deleteAll(null); + } + + @Override + public void deleteAll(Transaction transaction) { QueryManager queryMgr = client.newQueryManager(); DeleteQueryDefinition deleteQuery = queryMgr.newDeleteDefinition(); deleteQuery.setCollections(entityClass.getName()); - queryMgr.delete(deleteQuery); + queryMgr.delete(deleteQuery, transaction); } /* REST API does not currently support DELETE /search with multiple collection arguments + @Override public void deleteAll(String... collections) { if ( collections == null || collections.length == 0 ) { throw new IllegalArgumentException("You must specify at least one collection"); @@ -213,77 +252,92 @@ public void deleteAll(String... collections) { } */ + @Override public T read(ID id) { return read(id, null); } + @Override public T read(ID id, Transaction transaction) { ArrayList ids = new ArrayList(); ids.add(id); + @SuppressWarnings("unchecked") PojoPage page = read(ids.toArray((ID[])new Serializable[0]), transaction); if ( page == null ) return null; if ( page.hasNext() ) return page.next(); return null; } + @Override public PojoPage read(ID[] ids) { return read(ids, null); } + @Override public PojoPage read(ID[] ids, Transaction transaction) { ArrayList uris = new ArrayList(); for ( ID id : ids ) { uris.add(createUri(id)); } DocumentPage docPage = (DocumentPage) docMgr.read(transaction, uris.toArray(new String[0])); - PojoPage pojoPage = new PojoPageImpl(docPage, entityClass); + PojoPage pojoPage = new PojoPageImpl(docPage, entityClass); return pojoPage; } + @Override public PojoPage readAll(long start) { return search(null, start, null, null); } + @Override public PojoPage readAll(long start, Transaction transaction) { return search(null, start, null, transaction); } + @Override public PojoPage search(long start, String... collections) { return search(qb.collection(collections), start, null, null); } + @Override public PojoPage search(long start, Transaction transaction, String... collections) { return search(qb.collection(collections), start, null, transaction); } + @Override public PojoPage search(PojoQueryDefinition query, long start) { return search(query, start, null, null); } + @Override public PojoPage search(PojoQueryDefinition query, long start, Transaction transaction) { return search(query, start, null, transaction); } + @Override public PojoPage search(PojoQueryDefinition query, long start, SearchReadHandle searchHandle) { return search(query, start, searchHandle, null); } + @Override public PojoPage search(PojoQueryDefinition query, long start, SearchReadHandle searchHandle, Transaction transaction) { if ( searchHandle != null ) { - HandleImplementation searchBase = HandleAccessor.checkHandle(searchHandle, "search"); + HandleAccessor.checkHandle(searchHandle, "search"); if (searchHandle instanceof SearchHandle) { SearchHandle responseHandle = (SearchHandle) searchHandle; if ( docMgr instanceof DocumentManagerImpl ) { - responseHandle.setHandleRegistry(((DocumentManagerImpl) docMgr).getHandleRegistry()); + responseHandle.setHandleRegistry(((DocumentManagerImpl) docMgr).getHandleRegistry()); } responseHandle.setQueryCriteria(query); } } - String tid = transaction == null ? null : transaction.getTransactionId(); DocumentPage docPage = docMgr.search(wrapQuery(query), start, searchHandle, transaction); - PojoPage pojoPage = new PojoPageImpl(docPage, entityClass); + PojoPage pojoPage = new PojoPageImpl(docPage, entityClass); return pojoPage; } - public PojoQueryBuilder getQueryBuilder() { + @Override + public PojoQueryBuilder getQueryBuilder() { return qb; } + @Override public long getPageLength() { return docMgr.getPageLength(); } + @Override public void setPageLength(long length) { docMgr.setPageLength(length); } @@ -370,7 +424,7 @@ private void findId() { if ( idMethod == null && idProperty == null ) { for ( Method method : entityClass.getDeclaredMethods() ) { if ( method.isAnnotationPresent(Id.class) ) { - Class[] parameters = method.getParameterTypes(); + Class[] parameters = method.getParameterTypes(); if ( ! Modifier.isPublic(method.getModifiers()) ) { throw new IllegalStateException("Your getter method, " + method.getName() + ", annotated with com.marklogic.client.pojo.annotation.Id " + @@ -411,6 +465,7 @@ private void findId() { } } + @SuppressWarnings("unchecked") private ID getId(T entity) { findId(); if ( idMethod != null ) { diff --git a/src/main/java/com/marklogic/client/pojo/PojoRepository.java b/src/main/java/com/marklogic/client/pojo/PojoRepository.java index e3b98decd..f451e1288 100644 --- a/src/main/java/com/marklogic/client/pojo/PojoRepository.java +++ b/src/main/java/com/marklogic/client/pojo/PojoRepository.java @@ -15,38 +15,130 @@ */ package com.marklogic.client.pojo; -import com.marklogic.client.DatabaseClient; import com.marklogic.client.Transaction; import com.marklogic.client.io.marker.SearchReadHandle; import java.io.Serializable; +/** PojoRepository is the central class for the Pojo Facade. It supports CRUD operations + * and search. Each PojoRepository instance operates on only one pojo class. Create new + * PojoRepository instances based on your custom pojo type like so: + *
    public class MyClass {
+ *        {@literal @}Id
+ *        public Integer getMyId() {
+ *            ...
+ *        }
+ *        public void setMyId(Integer integer) {
+ *            ...
+ *        }
+ *        ...
+ *    }
+ *    ...
+ *    DatabaseClient client = ...;
+ *    PojoRepository myClassRepo = 
+ *        client.newPojoRepository(MyClass.class, Integer);
+ * + * Where MyClass is your custom pojo type, and myId is the bean property of type Integer + * marked with the + * {@literal @}{@link com.marklogic.client.pojo.annotation.Id Id annotation}. The + * {@literal @}Id annotaiton can be attached to a public field or a public getter or a + * public setter. The bean property marked with {@literal @}Id must be a native type or + * {@link java.io.Serializable} class and must contain an + * identifier value unique across all persisted instances of that + * type or the instance will overwrite the persisted instance with the same identifier. + * + * The current implementation of the Pojo Facade uses + *
Jackson databind for serialization + * and deserialization to json. Thus only classes which can be serialized and deserialized + * directly by Jackson can be serialized by the Pojo Facade. Every bean property + * including the one marked with {@literal @}Id must either expose a public field or both a public + * getter and a public setter. To test if your class can be directly serialized and + * deserialized by Jackson, perform the following: + *
    ObjectMapper objectMapper = new ObjectMapper();
+ *    String value = objectMapper.writeValueAsString(myObjectIn);
+ *    MyClass myObjectOut = objectMapper.readValue(value, MyClass.class);
+ * + * If that works but the configured objectMapper in the Pojo Facade is different and not + * working, you can troubleshoot by directly accessing the objectMapper used by the Pojo + * Facade using an unsupported internal method attached to the current implementataion: + * com.marklogic.client.impl.PojoRepositoryImpl. + *
    ObjectMapper objectMapper = ((PojoRepositoryImpl) myClassRepo).getObjectMapper();
+ * + * If your class has properties which are classes (non-native types) they will be automatically + * serialized and deserialized, but cannot be written, read, or searched directly. If you + * wish to directly write, read, or search another class, create a new instance of + * PojoRepository specific to that class. + */ public interface PojoRepository { + /** Write this instance to the database. Uses the field marked with {@literal @}Id + * annotation to generate a unique uri for the document. Adds a collection with the + * fully qualified class name. Uses a particular configuration of + * {@link com.fasterxml.jackson.databind.ObjectMapper ObjectMapper} to generate the + * serialized JSON format. + */ public void write(T entity); + /** Does everything in {@link #write(Object) write(T)} but also adds your collections to the + * persisted instance. + */ public void write(T entity, String... collections); + /** Does everything in {@link #write(Object) write(T)} but in your + * + * multi-statement transaction context. + */ public void write(T entity, Transaction transaction); + /** Does everything in {@link #write(Object) write(T)} but also adds your collections to the + * persisted instance and performs the write in your + * + * multi-statement transaction context. + * . + */ public void write(T entity, Transaction transaction, String... collections); /** @return true if a document exists in the database with the id */ public boolean exists(ID id); + /** @return in the context of transaction, true if a document exists in the database with + * the id */ + public boolean exists(ID id, Transaction transaction); + /** @return the number of documents of type T persisted in the database */ public long count(); + /** @return in the context of transaction, the number of documents of type T persisted in + * the database */ + public long count(Transaction transaction); + /** @return the number of documents of type T persisted in the database with at least * one of the criteria collections*/ public long count(String... collection); + /** @return in the context of transaction, the number of documents of type T persisted in + * the database with at least one of the criteria collections*/ + public long count(String[] collections, Transaction transaction); + /** @return the number of documents of type T persisted in the database which match * the query */ public long count(PojoQueryDefinition query); + /** @return in the context of transaction, the number of documents of type T persisted in the + * database which match the query */ + public long count(PojoQueryDefinition query, Transaction transaction); + /** Deletes from the database the documents with the corresponding ids */ public void delete(ID... ids); + /** As part of transaction, deletes from the database the documents with the corresponding ids */ + public void delete(ID[] ids, Transaction transaction); + /** Deletes from the database all documents of type T persisted by the pojo facade */ public void deleteAll(); + /** As part of transaction, deletes from the database all documents of type T persisted by + * the pojo facade */ + public void deleteAll(Transaction transaction); + /* REST API does not currently support DELETE /search with multiple collection arguments public void deleteAll(String... collections); */ From 7caf783fe95e2b85ad6e6eaf5e698a3c7a56a5d9 Mon Sep 17 00:00:00 2001 From: sammefford Date: Mon, 3 Nov 2014 12:02:59 -0700 Subject: [PATCH 260/357] new REST API requires the exports in caps --- src/main/resources/scripts/helloWorld.sjs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/resources/scripts/helloWorld.sjs b/src/main/resources/scripts/helloWorld.sjs index 7379ddd56..40b29032f 100644 --- a/src/main/resources/scripts/helloWorld.sjs +++ b/src/main/resources/scripts/helloWorld.sjs @@ -14,8 +14,8 @@ function putMethod(context, params, input) { function deleteMethod(context, params) { }; -exports.get = getMethod; -exports.post = postMethod; -exports.put = putMethod; -exports.delete = deleteMethod; +exports.GET = getMethod; +exports.POST = postMethod; +exports.PUT = putMethod; +exports.DELETE = deleteMethod; From 288f7738cf76986c0429f16ec9cee570c1146664 Mon Sep 17 00:00:00 2001 From: sammefford Date: Mon, 3 Nov 2014 12:06:08 -0700 Subject: [PATCH 261/357] I noticed I was getting results when I had set pageLength to 0 (used in PojoRepositoryImpl.count()). That's a bug fixed by this change. --- src/main/java/com/marklogic/client/impl/JerseyServices.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/marklogic/client/impl/JerseyServices.java b/src/main/java/com/marklogic/client/impl/JerseyServices.java index 108be5b22..80bb82459 100644 --- a/src/main/java/com/marklogic/client/impl/JerseyServices.java +++ b/src/main/java/com/marklogic/client/impl/JerseyServices.java @@ -841,7 +841,7 @@ private JerseyResultIterator getBulkDocumentsImpl(RequestLogger reqlog, addCategoryParams(categories, params, withContent); if (searchHandle != null && view != null) params.add("view", view.toString().toLowerCase()); if (start > 1) params.add("start", Long.toString(start)); - if (pageLength > 0) params.add("pageLength", Long.toString(pageLength)); + if (pageLength >= 0) params.add("pageLength", Long.toString(pageLength)); if (format != null) params.add("format", format.toString().toLowerCase()); if (transactionId != null) params.add("txid", transactionId); if ( format == null && searchHandle != null ) { From 489370c278d3a27d4d16c9477d9d1fe06698ebdd Mon Sep 17 00:00:00 2001 From: sammefford Date: Mon, 3 Nov 2014 12:07:25 -0700 Subject: [PATCH 262/357] fix #168--allow javascript resource extensions and set content-type accordingly --- .../client/impl/ResourceExtensionsImpl.java | 13 ++++++- .../cookbook/JavascriptExtensionTest.java | 39 +++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/marklogic/client/test/example/cookbook/JavascriptExtensionTest.java diff --git a/src/main/java/com/marklogic/client/impl/ResourceExtensionsImpl.java b/src/main/java/com/marklogic/client/impl/ResourceExtensionsImpl.java index 5629d0165..6c2d6d878 100644 --- a/src/main/java/com/marklogic/client/impl/ResourceExtensionsImpl.java +++ b/src/main/java/com/marklogic/client/impl/ResourceExtensionsImpl.java @@ -23,6 +23,7 @@ import com.marklogic.client.DatabaseClientFactory.HandleFactoryRegistry; import com.marklogic.client.admin.ExtensionMetadata; +import com.marklogic.client.admin.ExtensionMetadata.ScriptLanguage; import com.marklogic.client.admin.ResourceExtensionsManager; import com.marklogic.client.io.Format; import com.marklogic.client.io.marker.ContentHandle; @@ -134,7 +135,7 @@ public T readServices(String resourceName, T sourceHa sourceBase.receiveContent( services.getValue(requestLogger, "config/resources", resourceName, true, - "application/xquery", sourceBase.receiveAs()) + sourceBase.getMimetype(), sourceBase.receiveAs()) ); return sourceHandle; @@ -192,9 +193,17 @@ public void writeServices( } } } + String contentType = null; + if ( metadata.getScriptLanguage() == null ) { + throw new IllegalArgumentException("scriptLanguage cannot be null"); + } else if ( metadata.getScriptLanguage() == ScriptLanguage.JAVASCRIPT ) { + contentType = "application/vnd.marklogic-javascript"; + } else if ( metadata.getScriptLanguage() == ScriptLanguage.XQUERY ) { + contentType = "application/xquery"; + } services.putValue(requestLogger, "config/resources", resourceName, extraParams, - "application/xquery", sourceBase); + contentType, sourceBase); } @Override diff --git a/src/test/java/com/marklogic/client/test/example/cookbook/JavascriptExtensionTest.java b/src/test/java/com/marklogic/client/test/example/cookbook/JavascriptExtensionTest.java new file mode 100644 index 000000000..b1ab1af53 --- /dev/null +++ b/src/test/java/com/marklogic/client/test/example/cookbook/JavascriptExtensionTest.java @@ -0,0 +1,39 @@ +/* + * Copyright 2012-2014 MarkLogic Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.marklogic.client.test.example.cookbook; + +import static org.junit.Assert.assertTrue; + +import java.io.IOException; + +import org.junit.Test; + +import com.marklogic.client.example.cookbook.JavascriptResourceExtension; + +public class JavascriptExtensionTest { + @Test + public void testMain() { + //System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + boolean succeeded = false; + try { + JavascriptResourceExtension.main(new String[0]); + succeeded = true; + } catch (IOException e) { + e.printStackTrace(); + } + assertTrue("JavascriptResourceExtension example failed", succeeded); + } +} From 4e75d8a051d4f36527c4e358c7fb6bf1e694149c Mon Sep 17 00:00:00 2001 From: sammefford Date: Mon, 3 Nov 2014 17:03:15 -0700 Subject: [PATCH 263/357] fix #119--disable Java 8 javadoc linter so we can have

and

 tags in our javadocs without a failed build

---
 pom.xml | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/pom.xml b/pom.xml
index ad606de64..ff4e1e0a2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -45,6 +45,10 @@
 					public
 					${javadoc-title}
 					
+					
+					-Xdoclint:none
 				
 				
 					

From 9bd2a679aa29696508f309137c49eaa62bdd7f42 Mon Sep 17 00:00:00 2001
From: gvaidees 
Date: Mon, 3 Nov 2014 17:52:24 -0800
Subject: [PATCH 264/357] Added overloaded methods for create/write and delete
 to not take systemTime as an argument

---
 .../bitemporal/TemporalDocumentManager.java   |  222 +-
 .../client/impl/DocumentManagerImpl.java      | 2286 +++++++++--------
 2 files changed, 1315 insertions(+), 1193 deletions(-)

diff --git a/src/main/java/com/marklogic/client/bitemporal/TemporalDocumentManager.java b/src/main/java/com/marklogic/client/bitemporal/TemporalDocumentManager.java
index 9146d7c4f..74391f25c 100644
--- a/src/main/java/com/marklogic/client/bitemporal/TemporalDocumentManager.java
+++ b/src/main/java/com/marklogic/client/bitemporal/TemporalDocumentManager.java
@@ -1,94 +1,128 @@
-/*
- * Copyright 2012-2014 MarkLogic Corporation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *    http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.marklogic.client.bitemporal;
-
-import com.marklogic.client.FailedRequestException;
-import com.marklogic.client.ForbiddenUserException;
-import com.marklogic.client.ResourceNotFoundException;
-import com.marklogic.client.Transaction;
-import com.marklogic.client.document.DocumentDescriptor;
-import com.marklogic.client.document.DocumentPage;
-import com.marklogic.client.document.DocumentUriTemplate;
-import com.marklogic.client.document.ServerTransform;
-import com.marklogic.client.io.marker.AbstractReadHandle;
-import com.marklogic.client.io.marker.AbstractWriteHandle;
-import com.marklogic.client.io.marker.DocumentMetadataReadHandle;
-import com.marklogic.client.io.marker.DocumentMetadataWriteHandle;
-
-public interface TemporalDocumentManager {
-    public DocumentDescriptor create(DocumentUriTemplate template,
-        DocumentMetadataWriteHandle metadataHandle,
-        W contentHandle,
-        ServerTransform transform,
-        Transaction transaction,
-        String temporalCollection,
-        java.util.Calendar systemTime)
-    throws ForbiddenUserException, FailedRequestException;
- 
-    public void write(DocumentDescriptor desc,
-        DocumentMetadataWriteHandle metadataHandle,
-        W contentHandle,
-        ServerTransform transform,
-        Transaction transaction,
-        String temporalCollection,
-        java.util.Calendar systemTime)
-    throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException;
- 
-    public void write(String docId,
-        DocumentMetadataWriteHandle metadataHandle,
-        W contentHandle,
-        ServerTransform transform,
-        Transaction transaction,
-        String temporalCollection,
-        java.util.Calendar systemTime)
-    throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException;
- 
-    /*
-    public  T read(DocumentDescriptor desc,
-        DocumentMetadataReadHandle metadataHandle,
-        T  contentHandle,
-        ServerTransform transform,
-        Transaction transaction,
-        String temporalCollection)
-    throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException;
- 
-    public  T read(String docId,
-        DocumentMetadataReadHandle metadataHandle,
-        T contentHandle,
-        ServerTransform transform,
-        Transaction transaction,
-        String temporalCollection)
-    throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException;
- 
-    public DocumentPage read(ServerTransform transform,
-        Transaction transaction,
-        String temporalCollection,
-        String[] uris)
-    throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException;
-    */
- 
-    public void delete(DocumentDescriptor desc,
-        Transaction transaction,
-        String temporalCollection,
-        java.util.Calendar systemTime)
-    throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException;
- 
-    public void delete(String docId,
-        Transaction transaction,
-        String temporalCollection,
-        java.util.Calendar systemTime)
-    throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException;
-}
+/*
+ * Copyright 2012-2014 MarkLogic Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.marklogic.client.bitemporal;
+
+import com.marklogic.client.FailedRequestException;
+import com.marklogic.client.ForbiddenUserException;
+import com.marklogic.client.ResourceNotFoundException;
+import com.marklogic.client.Transaction;
+import com.marklogic.client.document.DocumentDescriptor;
+import com.marklogic.client.document.DocumentUriTemplate;
+import com.marklogic.client.document.ServerTransform;
+import com.marklogic.client.io.marker.AbstractReadHandle;
+import com.marklogic.client.io.marker.AbstractWriteHandle;
+import com.marklogic.client.io.marker.DocumentMetadataWriteHandle;
+
+public interface TemporalDocumentManager {
+	public DocumentDescriptor create(DocumentUriTemplate template,
+      DocumentMetadataWriteHandle metadataHandle,
+      W contentHandle,
+      ServerTransform transform,
+      Transaction transaction,
+      String temporalCollection)
+  throws ForbiddenUserException, FailedRequestException;
+
+  public void write(DocumentDescriptor desc,
+      DocumentMetadataWriteHandle metadataHandle,
+      W contentHandle,
+      ServerTransform transform,
+      Transaction transaction,
+      String temporalCollection)
+  throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException;
+
+  public void write(String docId,
+      DocumentMetadataWriteHandle metadataHandle,
+      W contentHandle,
+      ServerTransform transform,
+      Transaction transaction,
+      String temporalCollection)
+  throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException;
+  
+  public void delete(DocumentDescriptor desc,
+      Transaction transaction,
+      String temporalCollection)
+  throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException;
+
+  public void delete(String docId,
+      Transaction transaction,
+      String temporalCollection)
+  throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException;
+	
+	  // The following methods take a system time which is an advanced concept in bitemporal feature. 
+    public DocumentDescriptor create(DocumentUriTemplate template,
+        DocumentMetadataWriteHandle metadataHandle,
+        W contentHandle,
+        ServerTransform transform,
+        Transaction transaction,
+        String temporalCollection,
+        java.util.Calendar systemTime)
+    throws ForbiddenUserException, FailedRequestException;
+ 
+    public void write(DocumentDescriptor desc,
+        DocumentMetadataWriteHandle metadataHandle,
+        W contentHandle,
+        ServerTransform transform,
+        Transaction transaction,
+        String temporalCollection,
+        java.util.Calendar systemTime)
+    throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException;
+ 
+    public void write(String docId,
+        DocumentMetadataWriteHandle metadataHandle,
+        W contentHandle,
+        ServerTransform transform,
+        Transaction transaction,
+        String temporalCollection,
+        java.util.Calendar systemTime)
+    throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException;
+    
+    public void delete(DocumentDescriptor desc,
+        Transaction transaction,
+        String temporalCollection,
+        java.util.Calendar systemTime)
+    throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException;
+ 
+    public void delete(String docId,
+        Transaction transaction,
+        String temporalCollection,
+        java.util.Calendar systemTime)
+    throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException;
+    
+    /*
+    public  T read(DocumentDescriptor desc,
+        DocumentMetadataReadHandle metadataHandle,
+        T  contentHandle,
+        ServerTransform transform,
+        Transaction transaction,
+        String temporalCollection)
+    throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException;
+ 
+    public  T read(String docId,
+        DocumentMetadataReadHandle metadataHandle,
+        T contentHandle,
+        ServerTransform transform,
+        Transaction transaction,
+        String temporalCollection)
+    throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException;
+ 
+    public DocumentPage read(ServerTransform transform,
+        Transaction transaction,
+        String temporalCollection,
+        String[] uris)
+    throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException;
+    */
+ 
+}
diff --git a/src/main/java/com/marklogic/client/impl/DocumentManagerImpl.java b/src/main/java/com/marklogic/client/impl/DocumentManagerImpl.java
index 45a5cde1f..3bc1e3ebe 100644
--- a/src/main/java/com/marklogic/client/impl/DocumentManagerImpl.java
+++ b/src/main/java/com/marklogic/client/impl/DocumentManagerImpl.java
@@ -1,1099 +1,1187 @@
-/*
- * Copyright 2012-2014 MarkLogic Corporation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *    http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.marklogic.client.impl;
-
-import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Set;
-
-import javax.xml.bind.DatatypeConverter;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.fasterxml.jackson.databind.JsonNode;
-import com.marklogic.client.DatabaseClientFactory.HandleFactoryRegistry;
-import com.marklogic.client.FailedRequestException;
-import com.marklogic.client.ForbiddenUserException;
-import com.marklogic.client.ResourceNotFoundException;
-import com.marklogic.client.Transaction;
-import com.marklogic.client.bitemporal.TemporalDocumentManager;
-import com.marklogic.client.document.DocumentDescriptor;
-import com.marklogic.client.document.DocumentManager;
-import com.marklogic.client.document.DocumentMetadataPatchBuilder;
-import com.marklogic.client.document.DocumentUriTemplate;
-import com.marklogic.client.document.DocumentPage;
-import com.marklogic.client.document.DocumentRecord;
-import com.marklogic.client.document.DocumentWriteOperation;
-import com.marklogic.client.document.DocumentWriteSet;
-import com.marklogic.client.document.ServerTransform;
-import com.marklogic.client.impl.DocumentMetadataPatchBuilderImpl.DocumentPatchHandleImpl;
-import com.marklogic.client.io.Format;
-import com.marklogic.client.io.marker.AbstractReadHandle;
-import com.marklogic.client.io.marker.AbstractWriteHandle;
-import com.marklogic.client.io.marker.ContentHandle;
-import com.marklogic.client.io.marker.DocumentMetadataReadHandle;
-import com.marklogic.client.io.marker.DocumentMetadataWriteHandle;
-import com.marklogic.client.io.marker.DocumentPatchHandle;
-import com.marklogic.client.io.marker.SearchReadHandle;
-import com.marklogic.client.io.JacksonHandle;
-import com.marklogic.client.io.SearchHandle;
-import com.marklogic.client.query.QueryDefinition;
-import com.marklogic.client.query.QueryManager.QueryView;
-import com.marklogic.client.util.RequestParameters;
-
-abstract class DocumentManagerImpl
-    extends AbstractLoggingManager
-    implements DocumentManager, TemporalDocumentManager
-{
-    static final private long DEFAULT_PAGE_LENGTH = 50;
-
-	static final private Logger logger = LoggerFactory.getLogger(DocumentManagerImpl.class);
-
-    private boolean isProcessedMetadataModified = false;
-	final private Set processedMetadata = new HashSet() {
-        public boolean add(Metadata e) {
-            isProcessedMetadataModified = true;
-            return super.add(e);
-        }
-        public boolean addAll(Collection c) {
-            isProcessedMetadataModified = true;
-            return super.addAll(c);
-        }
-    };
-    {
-        processedMetadata.add(Metadata.ALL);
-        // we need to know if the user modifies after us
-        isProcessedMetadataModified = false;
-    }
-        
-
-	private RESTServices          services;
-	private Format                contentFormat;
-	private HandleFactoryRegistry handleRegistry;
-	private ServerTransform       readTransform;
-	private ServerTransform       writeTransform;
-    private String                forestName;
-    private long                  pageLength = DEFAULT_PAGE_LENGTH;
-    private QueryView searchView = QueryView.RESULTS;
-    private Format nonDocumentFormat = null;
-
-	DocumentManagerImpl(RESTServices services, Format contentFormat) {
-		super();
-		this.services       = services;
-		this.contentFormat  = contentFormat;
-	}
-
-	RESTServices getServices() {
-		return services;
-	}
-	void setServices(RESTServices services) {
-		this.services = services;
-	}
-
-	HandleFactoryRegistry getHandleRegistry() {
-		return handleRegistry;
-	}
-	void setHandleRegistry(HandleFactoryRegistry handleRegistry) {
-		this.handleRegistry = handleRegistry;
-	}
-
-	@Override
-    public Format getContentFormat() {
-    	return contentFormat;
-    }
-
-    // select categories of metadata to read, write, or reset
-	@Override
-    public void setMetadataCategories(Set categories) {
-		clearMetadataCategories();
-		processedMetadata.addAll(categories);
-    }
-	@Override
-    public void setMetadataCategories(Metadata... categories) {
-		clearMetadataCategories();
-    	for (Metadata category: categories)
-    		processedMetadata.add(category);
-    }
-	@Override
-    public Set getMetadataCategories() {
-    	return processedMetadata;
-    }
-	@Override
-    public void clearMetadataCategories() {
-   		processedMetadata.clear();
-    }
-
-
-	@Override
-	public DocumentDescriptor exists(String uri) throws ForbiddenUserException, FailedRequestException {
-		return exists(uri, null);
-    }
-	@Override
-	public DocumentDescriptor exists(String uri, Transaction transaction) throws ForbiddenUserException, FailedRequestException {
-		return services.head(requestLogger, uri, (transaction == null) ? null : transaction.getTransactionId());
-	}
-
-	// shortcut readers
-	@Override
-    public  T readAs(String uri, Class as)
-	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		return readAs(uri, null, as, null);
-	}
-	@Override
-	public  T readAs(String uri, Class as, ServerTransform transform)
-	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		return readAs(uri, null, as, transform);
-	}
-	@Override
-	public  T readAs(String uri, DocumentMetadataReadHandle metadataHandle, Class as)
-	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		return readAs(uri, metadataHandle, as, null);
-	}
-	@Override
-    public  T readAs(String uri, DocumentMetadataReadHandle metadataHandle, Class as, ServerTransform transform)
-	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-			ContentHandle handle = getHandleRegistry().makeHandle(as);
-
-			if (null == read(uri, metadataHandle, castAbstractReadHandle(as, handle), transform)) {
-				return null;
-			}
-
-			return handle.get();
-	}
-	R castAbstractReadHandle(Class as, AbstractReadHandle handle) {
-		try {
-			@SuppressWarnings("unchecked")
-			R readHandle = (R) handle;
-			return readHandle;
-		} catch(ClassCastException e) {
-			throw new IllegalArgumentException(
-					"Handle "+handle.getClass().getName()+
-					" cannot be used in the context to read "+as.getName()
-					);
-		}
-	}
-
-	// strongly typed readers
-	@Override
-	public  T read(String uri, T contentHandle)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		return read(uri, null, contentHandle, null, null);
-	}
-	@Override
-	public  T read(String uri, T contentHandle, ServerTransform transform)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		return read(uri, null, contentHandle, transform, null);
-	}
-	@Override
-	public  T read(String uri, DocumentMetadataReadHandle metadataHandle, T contentHandle)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		return read(uri, metadataHandle, contentHandle, null, null);
-	}
-	@Override
-	public  T read(String uri, DocumentMetadataReadHandle metadataHandle, T contentHandle, ServerTransform transform)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		return read(uri, metadataHandle, contentHandle, transform, null);
-	}
-	@Override
-	public  T read(String uri, T contentHandle, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		return read(uri, null, contentHandle, null, transaction);
-	}
-	@Override
-	public  T read(String uri, T contentHandle, ServerTransform transform, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		return read(uri, null, contentHandle, transform, transaction);
-	}
-	@Override
-	public  T read(String uri, DocumentMetadataReadHandle metadataHandle, T contentHandle, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		return read(uri, metadataHandle, contentHandle, null, transaction);
-	}
-	@Override
-	public  T read(String uri, DocumentMetadataReadHandle metadataHandle, T contentHandle, ServerTransform transform, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-//		return read(new DocumentDescriptorImpl(docId, true), metadataHandle, contentHandle, transform, transaction, null, null);
-		return read(new DocumentDescriptorImpl(uri, true), metadataHandle, contentHandle, transform, transaction, null, getReadParams());
-	}
-	/*
-	@Override
-	public  T read(String docId,
-			DocumentMetadataReadHandle metadataHandle,
-			T contentHandle,
-			ServerTransform transform,
-			Transaction transaction,
-			String temporalCollection)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException
-	{
-		return read(new DocumentDescriptorImpl(docId, true), metadataHandle, contentHandle, transform, 
-			transaction, temporalCollection, null);
-	}
-	*/
-
-	@Override
-    public  T read(DocumentDescriptor desc, T contentHandle)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		return read(desc, null, contentHandle, null, null);
-	}
-
-	@Override
-    public  T read(DocumentDescriptor desc, T contentHandle, ServerTransform transform)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		return read(desc, null, contentHandle, transform, null);
-	}
-	@Override
-	public  T read(DocumentDescriptor desc, DocumentMetadataReadHandle metadataHandle, T contentHandle)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		return read(desc, metadataHandle, contentHandle, null, null);
-	}
-	@Override
-	public  T read(DocumentDescriptor desc, DocumentMetadataReadHandle metadataHandle, T contentHandle, ServerTransform transform)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		return read(desc, metadataHandle, contentHandle, transform, null);
-	}
-	@Override
-	public  T read(DocumentDescriptor desc, T contentHandle, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		return read(desc, null, contentHandle, null, transaction);
-	}
-	@Override
-	public  T read(DocumentDescriptor desc, T contentHandle, ServerTransform transform, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		return read(desc, null, contentHandle, transform, transaction);
-	}
-	@Override
-	public  T read(DocumentDescriptor desc, DocumentMetadataReadHandle metadataHandle, T contentHandle, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		return read(desc, metadataHandle, contentHandle, null, transaction);
-	}
-	@Override
-	public  T read(DocumentDescriptor desc, DocumentMetadataReadHandle metadataHandle, T contentHandle, ServerTransform transform, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		return read(desc, metadataHandle, contentHandle, transform, transaction, null, getReadParams());
-	}
-	/*
-	@Override
-	public  T read(DocumentDescriptor desc,
-			DocumentMetadataReadHandle metadataHandle,
-			T  contentHandle,
-			ServerTransform transform,
-			Transaction transaction,
-			String temporalCollection)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException
-	{
-		return read(desc, metadataHandle, contentHandle, transform, transaction, temporalCollection, getReadParams());
-	}
-	*/
-
-	@SuppressWarnings("rawtypes")
-	public  T read(DocumentDescriptor desc,
-		DocumentMetadataReadHandle metadataHandle,
-		T  contentHandle,
-		ServerTransform transform,
-		Transaction transaction,
-		String temporalCollection, 
-		RequestParameters extraParams)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException
-	{
-		if (desc == null)
-			throw new IllegalArgumentException("Attempt to call read with null DocumentDescriptor");
-
-		if (logger.isInfoEnabled())
-			logger.info("Reading metadata and content for {}", desc.getUri());
-
-		if (metadataHandle != null) {
-			HandleImplementation metadataBase = HandleAccessor.checkHandle(metadataHandle, "metadata");
-			Format metadataFormat = metadataBase.getFormat();
-			if (metadataFormat == null || (metadataFormat != Format.JSON && metadataFormat != Format.XML)) {
-				if (logger.isWarnEnabled())
-					logger.warn("Unsupported metadata format {}, using XML",metadataFormat.name());
-				metadataBase.setFormat(Format.XML);
-			}
-		}
-
-		checkContentFormat(contentHandle);
-
-		extraParams = addTemporalParams(extraParams, temporalCollection, null);
-
-		boolean wasModified = services.getDocument(
-				requestLogger,
-				desc, 
-				(transaction != null) ? transaction.getTransactionId() : null,
-				(metadataHandle != null) ? processedMetadata : null,
-				mergeTransformParameters(
-						(transform != null) ? transform : getReadTransform(),
-						extraParams
-						),
-				metadataHandle,
-				contentHandle
-				);
-
-		// TODO: after response, reset metadata and set flag
-
-		return wasModified ? contentHandle : null;
-	}
-
-	@Override
-	public DocumentPage read(String... uris) {
-		return read(null, null, uris);
-	}
-
-	@Override
-	public DocumentPage read(Transaction transaction, String... uris) {
-		return read(null, transaction, uris);
-	}
-
-	@Override
-	public DocumentPage read(ServerTransform transform, String... uris) {
-		return read(transform, null, uris);
-	}
-
-	@Override
-	public DocumentPage read(ServerTransform transform, Transaction transaction, String... uris) {
-		boolean withContent = true;
-		return read(transform, transaction, withContent, null, uris);
-	}
-
-	/*
-	@Override
-	public DocumentPage read(ServerTransform transform,
-		Transaction transaction,
-		String temporalCollection,
-		String[] uris)
-	{
-		boolean withContent = true;
-		return read(transform, transaction, withContent, temporalCollection, uris);
-	}
-	*/
-	public DocumentPage read(ServerTransform transform,
-		Transaction transaction,
-		boolean withContent,
-		String temporalCollection,
-		String[] uris)
-	{
-		if (uris == null || uris.length == 0)
-			throw new IllegalArgumentException("Attempt to call read with no uris");
-
-		if (logger.isInfoEnabled())
-			logger.info("Reading metadata and content for multiple uris beginning with {}", uris[0]);
-
-		RequestParameters extraParams = addTemporalParams(new RequestParameters(), temporalCollection, null);
-
-        return services.getBulkDocuments(
-            requestLogger,
-            (transaction == null) ? null : transaction.getTransactionId(),
-            // the default for bulk is no metadata, which differs from the normal default of ALL
-            isProcessedMetadataModified ? processedMetadata : null,
-            nonDocumentFormat,
-            mergeTransformParameters(
-                    (transform != null) ? transform : getReadTransform(),
-                    extraParams
-            ),
-            withContent,
-            uris);
-   	}
-
-	public DocumentPage readMetadata(String... uris) {
-		boolean withContent = false;
-		return read(null, null, withContent, null, uris);
-	}
-
-	public DocumentPage readMetadata(Transaction transaction, String... uris) {
-		boolean withContent = false;
-		return read(null, transaction, withContent, null, uris);
-	}
-
-	public DocumentPage search(QueryDefinition querydef, long start) {
-		return search(querydef, start, null, null);
-	}
-
-	public DocumentPage search(QueryDefinition querydef, long start, SearchReadHandle searchHandle) {
-		return search(querydef, start, searchHandle, null);
-	}
-
-	public DocumentPage search(QueryDefinition querydef, long start, Transaction transaction) {
-		return search(querydef, start, null, transaction);
-	}
-
-	public DocumentPage search(QueryDefinition querydef, long start, SearchReadHandle searchHandle, Transaction transaction) {
-
-        if ( searchHandle != null ) {
-            HandleImplementation searchBase = HandleAccessor.checkHandle(searchHandle, "search");
-            if (searchHandle instanceof SearchHandle) {
-                SearchHandle responseHandle = (SearchHandle) searchHandle;
-                responseHandle.setHandleRegistry(getHandleRegistry());
-                responseHandle.setQueryCriteria(querydef);
-            }
-            if ( nonDocumentFormat != null && nonDocumentFormat != searchBase.getFormat() ) {
-                throw new UnsupportedOperationException("The format supported by your handle:[" + 
-                    searchBase.getFormat() + "] does not match the non-document format:[" + nonDocumentFormat + "]");
-            }
-        }
-
-        String tid = transaction == null ? null : transaction.getTransactionId();
-        // the default for bulk is no metadata, which differs from the normal default of ALL
-        Set metadata = isProcessedMetadataModified ? processedMetadata : null;
-        return services.getBulkDocuments( requestLogger, querydef, start, getPageLength(), 
-            tid, searchHandle, searchView, metadata, nonDocumentFormat, null);
-	}
-
-    public long getPageLength() {
-        return pageLength;
-    }
-
-    public void setPageLength(long length) {
-        this.pageLength = length;
-    }
-
-    public QueryView getSearchView() {
-        return searchView;
-    }
-
-    public void setSearchView(QueryView view) {
-        this.searchView = view;
-    }
-
-    public Format getNonDocumentFormat() {
-        return nonDocumentFormat;
-    }
-
-    public void setNonDocumentFormat(Format nonDocumentFormat) {
-        if ( nonDocumentFormat != Format.XML && nonDocumentFormat != Format.JSON ) {
-            throw new UnsupportedOperationException("Only XML and JSON are valid response formats.  You specified:[" + 
-                nonDocumentFormat + "]");
-        }
-        this.nonDocumentFormat = nonDocumentFormat;
-    }
-
-	public DocumentWriteSet newWriteSet() {
-		return new DocumentWriteSetImpl();
-	}
-
-	public void write(DocumentWriteSet writeSet) {
-        write(writeSet, null, null);
-	}
-
-	public void write(DocumentWriteSet writeSet, ServerTransform transform) {
-        write(writeSet, transform, null);
-	}
-
-	public void write(DocumentWriteSet writeSet, Transaction transaction) {
-        write(writeSet, null, transaction);
-	}
- 
-	public void write(DocumentWriteSet writeSet, ServerTransform transform, Transaction transaction) {
-		Format defaultFormat = contentFormat;
-		services.postBulkDocuments(
-            requestLogger,
-            writeSet,
-            (transform != null) ? transform : getWriteTransform(),
-            (transaction == null) ? null : transaction.getTransactionId(),
-			defaultFormat,
-			null);
-	}
-
-	// shortcut writers
-	@Override
-    public void writeAs(String uri, Object content)
-	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		writeAs(uri, null, content, null);
-    }
-	@Override
-    public void writeAs(String uri, Object content, ServerTransform transform)
-	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		writeAs(uri, null, content, transform);
-    }
-	@Override
-    public void writeAs(String uri, DocumentMetadataWriteHandle metadataHandle, Object content)
-	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		writeAs(uri, metadataHandle, content, null);
-    }
-	@Override
-    public void writeAs(String uri, DocumentMetadataWriteHandle metadataHandle, Object content, ServerTransform transform)
-	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		if (content == null) {
-			throw new IllegalArgumentException("no content to write");
-		}
-
-		Class as = content.getClass();
-
-		W writeHandle = null;
-		if (AbstractWriteHandle.class.isAssignableFrom(as)) {
-			AbstractWriteHandle handle = (AbstractWriteHandle) content;
-			writeHandle = castAbstractWriteHandle(null, handle);			
-		} else {
-			ContentHandle handle = getHandleRegistry().makeHandle(as);
-			Utilities.setHandleContent(handle, content);
-			writeHandle = castAbstractWriteHandle(as, handle);			
-		}
-
-		write(uri, metadataHandle, writeHandle, transform);			
-	}
-	W castAbstractWriteHandle(Class as, AbstractWriteHandle handle) {
-		try {
-			@SuppressWarnings("unchecked")
-			W writeHandle = (W) handle;
-			return writeHandle;
-		} catch(ClassCastException e) {
-			if (as == null) {
-				throw new IllegalArgumentException(
-						"Handle "+handle.getClass().getName()+
-						" cannot be used in the context for writing"
-						);
-			}
-			throw new IllegalArgumentException(
-					"Handle "+handle.getClass().getName()+
-					" cannot be used in the context to write "+as.getName()
-					);
-		}
-	}
-
-	// strongly typed writers
-	@Override
-	public void write(String uri, W contentHandle)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(uri, null, contentHandle, null, null, null, null);
-	}
-	@Override
-	public void write(String uri, W contentHandle, ServerTransform transform)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(uri, null, contentHandle, transform, null, null, null);
-	}
-	@Override
-	public void write(String uri, DocumentMetadataWriteHandle metadata, W contentHandle)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(uri, metadata, contentHandle, null, null, null, null);
-	}
-	@Override
-	public void write(String uri, DocumentMetadataWriteHandle metadata, W contentHandle, ServerTransform transform)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(uri, metadata, contentHandle, transform, null, null, null);
-	}
-	@Override
-	public void write(String uri, W contentHandle, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(uri, null, contentHandle, null, transaction, null, null);
-	}
-	@Override
-	public void write(String uri, W contentHandle, ServerTransform transform, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(uri, null, contentHandle, transform, transaction, null, null);
-	}
-	@Override
-	public void write(String uri, DocumentMetadataWriteHandle metadataHandle, W contentHandle, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(uri, metadataHandle, contentHandle, null, transaction, null, null);
-	}
-	@Override
-	public void write(String uri, DocumentMetadataWriteHandle metadataHandle, W contentHandle, ServerTransform transform, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(uri, metadataHandle, contentHandle, transform, transaction, null, null);
-	}
-	@Override
-    public void write(String uri,
-        DocumentMetadataWriteHandle metadataHandle,
-        W contentHandle,
-        ServerTransform transform,
-        Transaction transaction,
-        String temporalCollection,
-        Calendar systemTime)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException
-    {
-		write(uri, metadataHandle, contentHandle, transform, transaction, temporalCollection, systemTime, getWriteParams());
-    }
-    public void write(String uri,
-        DocumentMetadataWriteHandle metadataHandle,
-        W contentHandle,
-        ServerTransform transform,
-        Transaction transaction,
-        String temporalCollection,
-        Calendar systemTime,
-		RequestParameters extraParams)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException
-    {
-		write(new DocumentDescriptorImpl(uri, true), metadataHandle, contentHandle, transform, 
-			transaction, temporalCollection, systemTime, extraParams);
-    }
- 
-	@Override
-	public void write(DocumentDescriptor desc, W contentHandle)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(desc, null, contentHandle, null, null, null, null);
-	}
-	@Override
-	public void write(DocumentDescriptor desc, W contentHandle, ServerTransform transform)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(desc, null, contentHandle, transform, null, null, null);
-	}
-	@Override
-	public void write(DocumentDescriptor desc, DocumentMetadataWriteHandle metadata, W contentHandle)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(desc, metadata, contentHandle, null, null, null, null);
-	}
-	@Override
-	public void write(DocumentDescriptor desc, DocumentMetadataWriteHandle metadata, W contentHandle, ServerTransform transform)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(desc, metadata, contentHandle, transform, null, null, null);
-	}
-	@Override
-	public void write(DocumentDescriptor desc, W contentHandle, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(desc, null, contentHandle, null, transaction, null, null);
-	}
-	@Override
-	public void write(DocumentDescriptor desc, W contentHandle, ServerTransform transform, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(desc, null, contentHandle, transform, transaction, null, null);
-	}
-	@Override
-	public void write(DocumentDescriptor desc, DocumentMetadataWriteHandle metadataHandle, W contentHandle, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(desc, metadataHandle, contentHandle, null, transaction, null, null);
-	}
-	@Override
-	public void write(DocumentDescriptor desc, DocumentMetadataWriteHandle metadataHandle, W contentHandle, ServerTransform transform, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(desc, metadataHandle, contentHandle, transform, transaction, null, null);
-	}
-	@Override
-    public void write(DocumentDescriptor desc,
-        DocumentMetadataWriteHandle metadataHandle,
-        W contentHandle,
-        ServerTransform transform,
-        Transaction transaction,
-        String temporalCollection,
-        java.util.Calendar systemTime)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException
-    {
-		write(desc, metadataHandle, contentHandle, transform, transaction, temporalCollection, 
-			systemTime, getWriteParams());
-	}
-	
-	@SuppressWarnings("rawtypes")
-    public void write(DocumentDescriptor desc,
-        DocumentMetadataWriteHandle metadataHandle,
-        W contentHandle,
-        ServerTransform transform,
-        Transaction transaction,
-        String temporalCollection,
-        java.util.Calendar systemTime,
-		RequestParameters extraParams)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException
-    {
-		if (desc == null)
-			throw new IllegalArgumentException("Writing document with null identifier");
-
-		if (logger.isInfoEnabled())
-			logger.info("Writing content for {}",desc.getUri());
-
-		if (metadataHandle != null) {
-			HandleImplementation metadataBase = HandleAccessor.checkHandle(metadataHandle, "metadata");
-			Format metadataFormat = metadataBase.getFormat();
-			if (metadataFormat == null || (metadataFormat != Format.JSON && metadataFormat != Format.XML)) {
-				if (logger.isWarnEnabled())
-					logger.warn("Unsupported metadata format {}, using XML",metadataFormat.name());
-				metadataBase.setFormat(Format.XML);
-			}
-		}
-
-		checkContentFormat(contentHandle);
-
-		extraParams = addTemporalParams(extraParams, temporalCollection, systemTime);
-
-		services.putDocument(
-				requestLogger,
-				desc,
-				(transaction == null) ? null : transaction.getTransactionId(),
-				(metadataHandle != null) ? processedMetadata : null,
-				mergeTransformParameters(
-						(transform != null) ? transform : getWriteTransform(),
-						extraParams
-						),
-				metadataHandle,
-				contentHandle
-				);
-	}
-
-	@Override
-	public void delete(String uri) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		delete(uri, null);
-	}
-	@Override
-	public void delete(String uri, Transaction transaction) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		delete(new DocumentDescriptorImpl(uri, true), transaction);
-	}
-	@Override
-    public void delete(DocumentDescriptor desc) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		delete(desc, null, null, null);
-    }
-	@Override
-    public void delete(DocumentDescriptor desc, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		delete(desc, transaction, null, null);
-	}
-	@Override
-	public void delete(String uri,
-			Transaction transaction,
-			String temporalCollection,
-			java.util.Calendar systemTime)
-	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException
-	{
-		delete(new DocumentDescriptorImpl(uri, true), transaction, temporalCollection, systemTime);
-	}
-	@Override
-	public void delete(DocumentDescriptor desc,
-			Transaction transaction,
-			String temporalCollection,
-			java.util.Calendar systemTime)
-	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException
-	{
-		if (desc == null)
-			throw new IllegalArgumentException("Deleting document with null identifier");
-
-		if (logger.isInfoEnabled())
-			logger.info("Deleting {}",desc.getUri());
-
-		RequestParameters extraParams = addTemporalParams(new RequestParameters(), temporalCollection, systemTime);
-
-		services.deleteDocument(requestLogger, desc, (transaction == null) ? null : transaction.getTransactionId(), 
-			null, extraParams);
-    }
-
-	// shortcut creators
-	@Override
-    public DocumentDescriptor createAs(DocumentUriTemplate template, Object content)
-	throws ForbiddenUserException, FailedRequestException {
-		return createAs(template, null, content, null);
-    }
-	@Override
-    public DocumentDescriptor createAs(DocumentUriTemplate template, Object content, ServerTransform transform)
-	throws ForbiddenUserException, FailedRequestException {
-		return createAs(template, null, content, transform);
-    }
-	@Override
-    public DocumentDescriptor createAs(DocumentUriTemplate template, DocumentMetadataWriteHandle metadataHandle, Object content)
-	throws ForbiddenUserException, FailedRequestException {
-		return createAs(template, metadataHandle, content, null);
-    }
-	@Override
-    public DocumentDescriptor createAs(DocumentUriTemplate template, DocumentMetadataWriteHandle metadataHandle, Object content, ServerTransform transform)
-	throws ForbiddenUserException, FailedRequestException {
-		Class as = content.getClass();
-		W writeHandle = null;
-		if (AbstractWriteHandle.class.isAssignableFrom(as)) {
-			AbstractWriteHandle handle = (AbstractWriteHandle) content;
-			writeHandle = castAbstractWriteHandle(null, handle);			
-		} else {
-			ContentHandle handle = getHandleRegistry().makeHandle(as);
-			Utilities.setHandleContent(handle, content);
-			writeHandle = castAbstractWriteHandle(as, handle);			
-		}
-		return create(template, metadataHandle, writeHandle, transform);
-	}
-
-	// strongly typed creators
-	@Override
-	public DocumentDescriptor create(DocumentUriTemplate template, W contentHandle)
-	throws ForbiddenUserException, FailedRequestException {
-		return create(template, null, contentHandle, null, null, null, null);
-	}
-	@Override
-	public DocumentDescriptor create(DocumentUriTemplate template, W contentHandle, 
-			ServerTransform transform)
-	throws ForbiddenUserException, FailedRequestException {
-		return create(template, null, contentHandle, transform, null, null, null);
-	}
-	@Override
-	public DocumentDescriptor create(DocumentUriTemplate template, W contentHandle,
-			Transaction transaction)
-	throws ForbiddenUserException, FailedRequestException {
-		return create(template, null, contentHandle, null, transaction, null, null);
-	}
-	@Override
-	public DocumentDescriptor create(DocumentUriTemplate template, W contentHandle,
-			ServerTransform transform, Transaction transaction)
-	throws ForbiddenUserException, FailedRequestException {
-		return create(template, null, contentHandle, transform, transaction, null, null);
-	}
-	@Override
-	public DocumentDescriptor create(DocumentUriTemplate template, DocumentMetadataWriteHandle metadataHandle,
-			W contentHandle)
-	throws ForbiddenUserException, FailedRequestException {
-		return create(template, metadataHandle, contentHandle, null, null, null, null);
-	}
-	@Override
-	public DocumentDescriptor create(DocumentUriTemplate template, DocumentMetadataWriteHandle metadataHandle,
-			W contentHandle, ServerTransform transform)
-	throws ForbiddenUserException, FailedRequestException {
-		return create(template, metadataHandle, contentHandle, transform, null, null, null);
-	}
-	@Override
-	public DocumentDescriptor create(DocumentUriTemplate template, DocumentMetadataWriteHandle metadataHandle,
-			W contentHandle, Transaction transaction)
-	throws ForbiddenUserException, FailedRequestException {
-		return create(template, metadataHandle, contentHandle, null, transaction, null, null);
-	}
-	@Override
-	public DocumentDescriptor create(DocumentUriTemplate template, DocumentMetadataWriteHandle metadataHandle,
-			W contentHandle, ServerTransform transform, Transaction transaction)
-	throws ForbiddenUserException, FailedRequestException {
-		return create(template, metadataHandle, contentHandle, transform, transaction, null, null);
-	}
-	@Override
-    public DocumentDescriptor create(DocumentUriTemplate template,
-        DocumentMetadataWriteHandle metadataHandle,
-        W contentHandle,
-        ServerTransform transform,
-        Transaction transaction,
-        String temporalCollection,
-        java.util.Calendar systemTime)
-	throws ForbiddenUserException, FailedRequestException
-    {
-		return create(template, metadataHandle, contentHandle, transform, transaction, 
-			temporalCollection, systemTime, getWriteParams());
-    }
-	@SuppressWarnings("rawtypes")
-    public DocumentDescriptor create(DocumentUriTemplate template,
-        DocumentMetadataWriteHandle metadataHandle,
-        W contentHandle,
-        ServerTransform transform,
-        Transaction transaction,
-        String temporalCollection,
-        java.util.Calendar systemTime,
-		RequestParameters extraParams)
-    {
-		if (logger.isInfoEnabled())
-			logger.info("Creating content");
-
-		if (metadataHandle != null) {
-			HandleImplementation metadataBase = HandleAccessor.checkHandle(metadataHandle, "metadata");
-			Format metadataFormat = metadataBase.getFormat();
-			if (metadataFormat == null || (metadataFormat != Format.JSON && metadataFormat != Format.XML)) {
-				if (logger.isWarnEnabled())
-					logger.warn("Unsupported metadata format {}, using XML",metadataFormat.name());
-				metadataBase.setFormat(Format.XML);
-			}
-		}
-
-		extraParams = addTemporalParams(extraParams, temporalCollection, systemTime);
-
-		checkContentFormat(contentHandle);
-
-		return services.postDocument(
-				requestLogger,
-				template,
-				(transaction == null) ? null : transaction.getTransactionId(),
-				(metadataHandle != null) ? processedMetadata : null,
-				mergeTransformParameters(
-						(transform != null) ? transform : getWriteTransform(),
-						extraParams
-						),
-				metadataHandle,
-				contentHandle
-				);
-	}
-
-	@Override
-	public void patchAs(String uri, Object patch)
-	throws ForbiddenUserException, FailedRequestException {
-		if (patch == null) {
-			throw new IllegalArgumentException("no patch to apply");
-		}
-
-		Class as = patch.getClass();
-
-		DocumentPatchHandle patchHandle = null;
-		if (DocumentPatchHandle.class.isAssignableFrom(as)) {
-			patchHandle = (DocumentPatchHandle) patch;
-		} else {
-			ContentHandle handle = getHandleRegistry().makeHandle(as);
-			if (!DocumentPatchHandle.class.isAssignableFrom(handle.getClass())) {
-				throw new IllegalArgumentException(
-						"Handle "+handle.getClass().getName()+
-						" cannot be used to apply patch as "+as.getName()
-						);
-			}
-			Utilities.setHandleContent(handle, patch);
-			patchHandle = (DocumentPatchHandle) handle;
-		}
-
-		patch(uri, patchHandle);
-	}
-
-	@Override
-	public void patch(String uri, DocumentPatchHandle patch)
-	throws ForbiddenUserException, FailedRequestException {
-		patch(uri, patch, null);
-	}
-	@Override
-	public void patch(String uri, DocumentPatchHandle patch, Transaction transaction)
-	throws ForbiddenUserException, FailedRequestException {
-		patch(new DocumentDescriptorImpl(uri, true), patch, transaction);
-	}
-	@Override
-	public void patch(DocumentDescriptor desc, DocumentPatchHandle patch)
-	throws ForbiddenUserException, FailedRequestException {
-		patch(desc, patch, null);
-	}
-	@Override
-	public void patch(DocumentDescriptor desc, DocumentPatchHandle patch, Transaction transaction)
-	throws ForbiddenUserException, FailedRequestException {
-		if (logger.isInfoEnabled())
-			logger.info("Patching document");
-
-		DocumentPatchHandleImpl builtPatch = 
-			(patch instanceof DocumentPatchHandleImpl) ?
-			(DocumentPatchHandleImpl) patch : null;
-		services.patchDocument(
-				requestLogger,
-				desc,
-				(transaction == null) ? null : transaction.getTransactionId(),
-				(builtPatch != null) ?
-						builtPatch.getMetadata() : processedMetadata,
-				(builtPatch != null) ?
-						builtPatch.isOnContent() : true,
-				patch
-				);
-	}
-
-	@Override
-    public  T readMetadata(String uri, T metadataHandle) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		return readMetadata(uri, metadataHandle, null);
-    }
-	@Override
-    public  T readMetadata(String uri, T metadataHandle, Transaction transaction) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		read(uri, metadataHandle, null, transaction);
-
-		return metadataHandle;
-    }
-
-	@Override
-    public void writeMetadata(String uri, DocumentMetadataWriteHandle metadataHandle) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		writeMetadata(uri, metadataHandle, null);
-    }
-	@Override
-    public void writeMetadata(String uri, DocumentMetadataWriteHandle metadataHandle, Transaction transaction) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		write(uri, metadataHandle, (W) null, transaction);
-    }
-
-	@Override
-    public void writeDefaultMetadata(String uri) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		writeDefaultMetadata(uri, null);
-    }
-	@Override
-    public void writeDefaultMetadata(String uri, Transaction transaction) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		if (uri == null)
-			throw new IllegalArgumentException("Resetting document metadata with null identifier");
-
-		if (logger.isInfoEnabled())
-			logger.info("Resetting metadata for {}",uri);
-
-		services.deleteDocument(requestLogger, new DocumentDescriptorImpl(uri, true), 
-				(transaction == null) ? null : transaction.getTransactionId(), processedMetadata, getWriteParams());
-    }
-
-	@Override
-    public ServerTransform getReadTransform() {
-    	return readTransform;
-    }
-	@Override
-    public void setReadTransform(ServerTransform transform) {
-    	this.readTransform = transform;
-    }
-
-	@Override
-    public ServerTransform getWriteTransform() {
-    	return writeTransform;
-    }
-	@Override
-    public void setWriteTransform(ServerTransform transform) {
-    	this.writeTransform = transform;
-    }
-
-	@Override
-    public String getForestName() {
-    	return forestName;
-    }
-	@Override
-    public void setForestName(String forestName) {
-    	this.forestName = forestName;
-    }
-
-	@Override
-	public DocumentDescriptor newDescriptor(String uri) {
-		return new DocumentDescriptorImpl(uri, false);
-	}
-
-	@Override
-    public DocumentUriTemplate newDocumentUriTemplate(String extension) {
-		return new DocumentUriTemplateImpl(extension);
-	}
-
-	@Override
-    public DocumentMetadataPatchBuilder newPatchBuilder(Format pathFormat) {
-    	return new DocumentMetadataPatchBuilderImpl(pathFormat);
-    }
-
-	private void checkContentFormat(Object contentHandle) {
-		checkContentFormat(HandleAccessor.checkHandle(contentHandle, "content"));
-	}
-	@SuppressWarnings("rawtypes")
-	private void checkContentFormat(HandleImplementation contentBase) {
-		if (contentBase == null)
-			return;
-
-		if (contentFormat != null && contentFormat != Format.UNKNOWN) {
-			Format currFormat = contentBase.getFormat();
-			if (currFormat != contentFormat) {
-				contentBase.setFormat(contentFormat);
-				if (currFormat != Format.UNKNOWN)
-					contentBase.setMimetype(contentFormat.getDefaultMimetype());
-			}
-		}
-	}
-	protected RequestParameters mergeTransformParameters(ServerTransform transform, RequestParameters extraParams) {
-		if (transform == null)
-			return extraParams;
-
-		if (extraParams == null)
-			extraParams = new RequestParameters();
-
-		transform.merge(extraParams);
-
-		return extraParams;
-	}
-
-	// hooks for extension
-	protected RequestParameters getReadParams() {
-		return null;
-	}
-	protected RequestParameters getWriteParams() {
-		return null;
-	}
-
-	protected RequestParameters addTemporalParams(RequestParameters params, String temporalCollection, Calendar systemTime) {
-		if ( params == null ) params = new RequestParameters();
-		if ( temporalCollection != null ) params.add("temporal-collection", temporalCollection);
-		if ( systemTime != null ) {
-			String formattedSystemTime = DatatypeConverter.printDateTime(systemTime);
-			params.add("system-time", formattedSystemTime);
-		}
-		return params;
-	}
-}
+/*
+ * Copyright 2012-2014 MarkLogic Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.marklogic.client.impl;
+
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.xml.bind.DatatypeConverter;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.marklogic.client.DatabaseClientFactory.HandleFactoryRegistry;
+import com.marklogic.client.FailedRequestException;
+import com.marklogic.client.ForbiddenUserException;
+import com.marklogic.client.ResourceNotFoundException;
+import com.marklogic.client.Transaction;
+import com.marklogic.client.bitemporal.TemporalDocumentManager;
+import com.marklogic.client.document.DocumentDescriptor;
+import com.marklogic.client.document.DocumentManager;
+import com.marklogic.client.document.DocumentMetadataPatchBuilder;
+import com.marklogic.client.document.DocumentUriTemplate;
+import com.marklogic.client.document.DocumentPage;
+import com.marklogic.client.document.DocumentRecord;
+import com.marklogic.client.document.DocumentWriteOperation;
+import com.marklogic.client.document.DocumentWriteSet;
+import com.marklogic.client.document.ServerTransform;
+import com.marklogic.client.impl.DocumentMetadataPatchBuilderImpl.DocumentPatchHandleImpl;
+import com.marklogic.client.io.Format;
+import com.marklogic.client.io.marker.AbstractReadHandle;
+import com.marklogic.client.io.marker.AbstractWriteHandle;
+import com.marklogic.client.io.marker.ContentHandle;
+import com.marklogic.client.io.marker.DocumentMetadataReadHandle;
+import com.marklogic.client.io.marker.DocumentMetadataWriteHandle;
+import com.marklogic.client.io.marker.DocumentPatchHandle;
+import com.marklogic.client.io.marker.SearchReadHandle;
+import com.marklogic.client.io.JacksonHandle;
+import com.marklogic.client.io.SearchHandle;
+import com.marklogic.client.query.QueryDefinition;
+import com.marklogic.client.query.QueryManager.QueryView;
+import com.marklogic.client.util.RequestParameters;
+
+abstract class DocumentManagerImpl
+    extends AbstractLoggingManager
+    implements DocumentManager, TemporalDocumentManager
+{
+    static final private long DEFAULT_PAGE_LENGTH = 50;
+
+	static final private Logger logger = LoggerFactory.getLogger(DocumentManagerImpl.class);
+
+    private boolean isProcessedMetadataModified = false;
+	final private Set processedMetadata = new HashSet() {
+        public boolean add(Metadata e) {
+            isProcessedMetadataModified = true;
+            return super.add(e);
+        }
+        public boolean addAll(Collection c) {
+            isProcessedMetadataModified = true;
+            return super.addAll(c);
+        }
+    };
+    {
+        processedMetadata.add(Metadata.ALL);
+        // we need to know if the user modifies after us
+        isProcessedMetadataModified = false;
+    }
+        
+
+	private RESTServices          services;
+	private Format                contentFormat;
+	private HandleFactoryRegistry handleRegistry;
+	private ServerTransform       readTransform;
+	private ServerTransform       writeTransform;
+    private String                forestName;
+    private long                  pageLength = DEFAULT_PAGE_LENGTH;
+    private QueryView searchView = QueryView.RESULTS;
+    private Format nonDocumentFormat = null;
+
+	DocumentManagerImpl(RESTServices services, Format contentFormat) {
+		super();
+		this.services       = services;
+		this.contentFormat  = contentFormat;
+	}
+
+	RESTServices getServices() {
+		return services;
+	}
+	void setServices(RESTServices services) {
+		this.services = services;
+	}
+
+	HandleFactoryRegistry getHandleRegistry() {
+		return handleRegistry;
+	}
+	void setHandleRegistry(HandleFactoryRegistry handleRegistry) {
+		this.handleRegistry = handleRegistry;
+	}
+
+	@Override
+    public Format getContentFormat() {
+    	return contentFormat;
+    }
+
+    // select categories of metadata to read, write, or reset
+	@Override
+    public void setMetadataCategories(Set categories) {
+		clearMetadataCategories();
+		processedMetadata.addAll(categories);
+    }
+	@Override
+    public void setMetadataCategories(Metadata... categories) {
+		clearMetadataCategories();
+    	for (Metadata category: categories)
+    		processedMetadata.add(category);
+    }
+	@Override
+    public Set getMetadataCategories() {
+    	return processedMetadata;
+    }
+	@Override
+    public void clearMetadataCategories() {
+   		processedMetadata.clear();
+    }
+
+
+	@Override
+	public DocumentDescriptor exists(String uri) throws ForbiddenUserException, FailedRequestException {
+		return exists(uri, null);
+    }
+	@Override
+	public DocumentDescriptor exists(String uri, Transaction transaction) throws ForbiddenUserException, FailedRequestException {
+		return services.head(requestLogger, uri, (transaction == null) ? null : transaction.getTransactionId());
+	}
+
+	// shortcut readers
+	@Override
+    public  T readAs(String uri, Class as)
+	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
+		return readAs(uri, null, as, null);
+	}
+	@Override
+	public  T readAs(String uri, Class as, ServerTransform transform)
+	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
+		return readAs(uri, null, as, transform);
+	}
+	@Override
+	public  T readAs(String uri, DocumentMetadataReadHandle metadataHandle, Class as)
+	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
+		return readAs(uri, metadataHandle, as, null);
+	}
+	@Override
+    public  T readAs(String uri, DocumentMetadataReadHandle metadataHandle, Class as, ServerTransform transform)
+	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
+			ContentHandle handle = getHandleRegistry().makeHandle(as);
+
+			if (null == read(uri, metadataHandle, castAbstractReadHandle(as, handle), transform)) {
+				return null;
+			}
+
+			return handle.get();
+	}
+	R castAbstractReadHandle(Class as, AbstractReadHandle handle) {
+		try {
+			@SuppressWarnings("unchecked")
+			R readHandle = (R) handle;
+			return readHandle;
+		} catch(ClassCastException e) {
+			throw new IllegalArgumentException(
+					"Handle "+handle.getClass().getName()+
+					" cannot be used in the context to read "+as.getName()
+					);
+		}
+	}
+
+	// strongly typed readers
+	@Override
+	public  T read(String uri, T contentHandle)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		return read(uri, null, contentHandle, null, null);
+	}
+	@Override
+	public  T read(String uri, T contentHandle, ServerTransform transform)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		return read(uri, null, contentHandle, transform, null);
+	}
+	@Override
+	public  T read(String uri, DocumentMetadataReadHandle metadataHandle, T contentHandle)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		return read(uri, metadataHandle, contentHandle, null, null);
+	}
+	@Override
+	public  T read(String uri, DocumentMetadataReadHandle metadataHandle, T contentHandle, ServerTransform transform)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		return read(uri, metadataHandle, contentHandle, transform, null);
+	}
+	@Override
+	public  T read(String uri, T contentHandle, Transaction transaction)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		return read(uri, null, contentHandle, null, transaction);
+	}
+	@Override
+	public  T read(String uri, T contentHandle, ServerTransform transform, Transaction transaction)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		return read(uri, null, contentHandle, transform, transaction);
+	}
+	@Override
+	public  T read(String uri, DocumentMetadataReadHandle metadataHandle, T contentHandle, Transaction transaction)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		return read(uri, metadataHandle, contentHandle, null, transaction);
+	}
+	@Override
+	public  T read(String uri, DocumentMetadataReadHandle metadataHandle, T contentHandle, ServerTransform transform, Transaction transaction)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+//		return read(new DocumentDescriptorImpl(docId, true), metadataHandle, contentHandle, transform, transaction, null, null);
+		return read(new DocumentDescriptorImpl(uri, true), metadataHandle, contentHandle, transform, transaction, null, getReadParams());
+	}
+	/*
+	@Override
+	public  T read(String docId,
+			DocumentMetadataReadHandle metadataHandle,
+			T contentHandle,
+			ServerTransform transform,
+			Transaction transaction,
+			String temporalCollection)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException
+	{
+		return read(new DocumentDescriptorImpl(docId, true), metadataHandle, contentHandle, transform, 
+			transaction, temporalCollection, null);
+	}
+	*/
+
+	@Override
+    public  T read(DocumentDescriptor desc, T contentHandle)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		return read(desc, null, contentHandle, null, null);
+	}
+
+	@Override
+    public  T read(DocumentDescriptor desc, T contentHandle, ServerTransform transform)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		return read(desc, null, contentHandle, transform, null);
+	}
+	@Override
+	public  T read(DocumentDescriptor desc, DocumentMetadataReadHandle metadataHandle, T contentHandle)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		return read(desc, metadataHandle, contentHandle, null, null);
+	}
+	@Override
+	public  T read(DocumentDescriptor desc, DocumentMetadataReadHandle metadataHandle, T contentHandle, ServerTransform transform)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		return read(desc, metadataHandle, contentHandle, transform, null);
+	}
+	@Override
+	public  T read(DocumentDescriptor desc, T contentHandle, Transaction transaction)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		return read(desc, null, contentHandle, null, transaction);
+	}
+	@Override
+	public  T read(DocumentDescriptor desc, T contentHandle, ServerTransform transform, Transaction transaction)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		return read(desc, null, contentHandle, transform, transaction);
+	}
+	@Override
+	public  T read(DocumentDescriptor desc, DocumentMetadataReadHandle metadataHandle, T contentHandle, Transaction transaction)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		return read(desc, metadataHandle, contentHandle, null, transaction);
+	}
+	@Override
+	public  T read(DocumentDescriptor desc, DocumentMetadataReadHandle metadataHandle, T contentHandle, ServerTransform transform, Transaction transaction)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		return read(desc, metadataHandle, contentHandle, transform, transaction, null, getReadParams());
+	}
+	/*
+	@Override
+	public  T read(DocumentDescriptor desc,
+			DocumentMetadataReadHandle metadataHandle,
+			T  contentHandle,
+			ServerTransform transform,
+			Transaction transaction,
+			String temporalCollection)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException
+	{
+		return read(desc, metadataHandle, contentHandle, transform, transaction, temporalCollection, getReadParams());
+	}
+	*/
+
+	@SuppressWarnings("rawtypes")
+	public  T read(DocumentDescriptor desc,
+		DocumentMetadataReadHandle metadataHandle,
+		T  contentHandle,
+		ServerTransform transform,
+		Transaction transaction,
+		String temporalCollection, 
+		RequestParameters extraParams)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException
+	{
+		if (desc == null)
+			throw new IllegalArgumentException("Attempt to call read with null DocumentDescriptor");
+
+		if (logger.isInfoEnabled())
+			logger.info("Reading metadata and content for {}", desc.getUri());
+
+		if (metadataHandle != null) {
+			HandleImplementation metadataBase = HandleAccessor.checkHandle(metadataHandle, "metadata");
+			Format metadataFormat = metadataBase.getFormat();
+			if (metadataFormat == null || (metadataFormat != Format.JSON && metadataFormat != Format.XML)) {
+				if (logger.isWarnEnabled())
+					logger.warn("Unsupported metadata format {}, using XML",metadataFormat.name());
+				metadataBase.setFormat(Format.XML);
+			}
+		}
+
+		checkContentFormat(contentHandle);
+
+		extraParams = addTemporalParams(extraParams, temporalCollection, null);
+
+		boolean wasModified = services.getDocument(
+				requestLogger,
+				desc, 
+				(transaction != null) ? transaction.getTransactionId() : null,
+				(metadataHandle != null) ? processedMetadata : null,
+				mergeTransformParameters(
+						(transform != null) ? transform : getReadTransform(),
+						extraParams
+						),
+				metadataHandle,
+				contentHandle
+				);
+
+		// TODO: after response, reset metadata and set flag
+
+		return wasModified ? contentHandle : null;
+	}
+
+	@Override
+	public DocumentPage read(String... uris) {
+		return read(null, null, uris);
+	}
+
+	@Override
+	public DocumentPage read(Transaction transaction, String... uris) {
+		return read(null, transaction, uris);
+	}
+
+	@Override
+	public DocumentPage read(ServerTransform transform, String... uris) {
+		return read(transform, null, uris);
+	}
+
+	@Override
+	public DocumentPage read(ServerTransform transform, Transaction transaction, String... uris) {
+		boolean withContent = true;
+		return read(transform, transaction, withContent, null, uris);
+	}
+
+	/*
+	@Override
+	public DocumentPage read(ServerTransform transform,
+		Transaction transaction,
+		String temporalCollection,
+		String[] uris)
+	{
+		boolean withContent = true;
+		return read(transform, transaction, withContent, temporalCollection, uris);
+	}
+	*/
+	public DocumentPage read(ServerTransform transform,
+		Transaction transaction,
+		boolean withContent,
+		String temporalCollection,
+		String[] uris)
+	{
+		if (uris == null || uris.length == 0)
+			throw new IllegalArgumentException("Attempt to call read with no uris");
+
+		if (logger.isInfoEnabled())
+			logger.info("Reading metadata and content for multiple uris beginning with {}", uris[0]);
+
+		RequestParameters extraParams = addTemporalParams(new RequestParameters(), temporalCollection, null);
+
+        return services.getBulkDocuments(
+            requestLogger,
+            (transaction == null) ? null : transaction.getTransactionId(),
+            // the default for bulk is no metadata, which differs from the normal default of ALL
+            isProcessedMetadataModified ? processedMetadata : null,
+            nonDocumentFormat,
+            mergeTransformParameters(
+                    (transform != null) ? transform : getReadTransform(),
+                    extraParams
+            ),
+            withContent,
+            uris);
+   	}
+
+	public DocumentPage readMetadata(String... uris) {
+		boolean withContent = false;
+		return read(null, null, withContent, null, uris);
+	}
+
+	public DocumentPage readMetadata(Transaction transaction, String... uris) {
+		boolean withContent = false;
+		return read(null, transaction, withContent, null, uris);
+	}
+
+	public DocumentPage search(QueryDefinition querydef, long start) {
+		return search(querydef, start, null, null);
+	}
+
+	public DocumentPage search(QueryDefinition querydef, long start, SearchReadHandle searchHandle) {
+		return search(querydef, start, searchHandle, null);
+	}
+
+	public DocumentPage search(QueryDefinition querydef, long start, Transaction transaction) {
+		return search(querydef, start, null, transaction);
+	}
+
+	public DocumentPage search(QueryDefinition querydef, long start, SearchReadHandle searchHandle, Transaction transaction) {
+
+        if ( searchHandle != null ) {
+            HandleImplementation searchBase = HandleAccessor.checkHandle(searchHandle, "search");
+            if (searchHandle instanceof SearchHandle) {
+                SearchHandle responseHandle = (SearchHandle) searchHandle;
+                responseHandle.setHandleRegistry(getHandleRegistry());
+                responseHandle.setQueryCriteria(querydef);
+            }
+            if ( nonDocumentFormat != null && nonDocumentFormat != searchBase.getFormat() ) {
+                throw new UnsupportedOperationException("The format supported by your handle:[" + 
+                    searchBase.getFormat() + "] does not match the non-document format:[" + nonDocumentFormat + "]");
+            }
+        }
+
+        String tid = transaction == null ? null : transaction.getTransactionId();
+        // the default for bulk is no metadata, which differs from the normal default of ALL
+        Set metadata = isProcessedMetadataModified ? processedMetadata : null;
+        return services.getBulkDocuments( requestLogger, querydef, start, getPageLength(), 
+            tid, searchHandle, searchView, metadata, nonDocumentFormat, null);
+	}
+
+    public long getPageLength() {
+        return pageLength;
+    }
+
+    public void setPageLength(long length) {
+        this.pageLength = length;
+    }
+
+    public QueryView getSearchView() {
+        return searchView;
+    }
+
+    public void setSearchView(QueryView view) {
+        this.searchView = view;
+    }
+
+    public Format getNonDocumentFormat() {
+        return nonDocumentFormat;
+    }
+
+    public void setNonDocumentFormat(Format nonDocumentFormat) {
+        if ( nonDocumentFormat != Format.XML && nonDocumentFormat != Format.JSON ) {
+            throw new UnsupportedOperationException("Only XML and JSON are valid response formats.  You specified:[" + 
+                nonDocumentFormat + "]");
+        }
+        this.nonDocumentFormat = nonDocumentFormat;
+    }
+
+	public DocumentWriteSet newWriteSet() {
+		return new DocumentWriteSetImpl();
+	}
+
+	public void write(DocumentWriteSet writeSet) {
+        write(writeSet, null, null);
+	}
+
+	public void write(DocumentWriteSet writeSet, ServerTransform transform) {
+        write(writeSet, transform, null);
+	}
+
+	public void write(DocumentWriteSet writeSet, Transaction transaction) {
+        write(writeSet, null, transaction);
+	}
+ 
+	public void write(DocumentWriteSet writeSet, ServerTransform transform, Transaction transaction) {
+		Format defaultFormat = contentFormat;
+		services.postBulkDocuments(
+            requestLogger,
+            writeSet,
+            (transform != null) ? transform : getWriteTransform(),
+            (transaction == null) ? null : transaction.getTransactionId(),
+			defaultFormat,
+			null);
+	}
+
+	// shortcut writers
+	@Override
+    public void writeAs(String uri, Object content)
+	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
+		writeAs(uri, null, content, null);
+    }
+	@Override
+    public void writeAs(String uri, Object content, ServerTransform transform)
+	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
+		writeAs(uri, null, content, transform);
+    }
+	@Override
+    public void writeAs(String uri, DocumentMetadataWriteHandle metadataHandle, Object content)
+	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
+		writeAs(uri, metadataHandle, content, null);
+    }
+	@Override
+    public void writeAs(String uri, DocumentMetadataWriteHandle metadataHandle, Object content, ServerTransform transform)
+	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
+		if (content == null) {
+			throw new IllegalArgumentException("no content to write");
+		}
+
+		Class as = content.getClass();
+
+		W writeHandle = null;
+		if (AbstractWriteHandle.class.isAssignableFrom(as)) {
+			AbstractWriteHandle handle = (AbstractWriteHandle) content;
+			writeHandle = castAbstractWriteHandle(null, handle);			
+		} else {
+			ContentHandle handle = getHandleRegistry().makeHandle(as);
+			Utilities.setHandleContent(handle, content);
+			writeHandle = castAbstractWriteHandle(as, handle);			
+		}
+
+		write(uri, metadataHandle, writeHandle, transform);			
+	}
+	W castAbstractWriteHandle(Class as, AbstractWriteHandle handle) {
+		try {
+			@SuppressWarnings("unchecked")
+			W writeHandle = (W) handle;
+			return writeHandle;
+		} catch(ClassCastException e) {
+			if (as == null) {
+				throw new IllegalArgumentException(
+						"Handle "+handle.getClass().getName()+
+						" cannot be used in the context for writing"
+						);
+			}
+			throw new IllegalArgumentException(
+					"Handle "+handle.getClass().getName()+
+					" cannot be used in the context to write "+as.getName()
+					);
+		}
+	}
+
+	// strongly typed writers
+	@Override
+	public void write(String uri, W contentHandle)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		write(uri, null, contentHandle, null, null, null, null);
+	}
+	@Override
+	public void write(String uri, W contentHandle, ServerTransform transform)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		write(uri, null, contentHandle, transform, null, null, null);
+	}
+	@Override
+	public void write(String uri, DocumentMetadataWriteHandle metadata, W contentHandle)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		write(uri, metadata, contentHandle, null, null, null, null);
+	}
+	@Override
+	public void write(String uri, DocumentMetadataWriteHandle metadata, W contentHandle, ServerTransform transform)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		write(uri, metadata, contentHandle, transform, null, null, null);
+	}
+	@Override
+	public void write(String uri, W contentHandle, Transaction transaction)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		write(uri, null, contentHandle, null, transaction, null, null);
+	}
+	@Override
+	public void write(String uri, W contentHandle, ServerTransform transform, Transaction transaction)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		write(uri, null, contentHandle, transform, transaction, null, null);
+	}
+	@Override
+	public void write(String uri, DocumentMetadataWriteHandle metadataHandle, W contentHandle, Transaction transaction)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		write(uri, metadataHandle, contentHandle, null, transaction, null, null);
+	}
+	@Override
+	public void write(String uri, DocumentMetadataWriteHandle metadataHandle, W contentHandle, ServerTransform transform, Transaction transaction)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		write(uri, metadataHandle, contentHandle, transform, transaction, null, null);
+	}
+
+	@Override
+    public void write(String uri,
+        DocumentMetadataWriteHandle metadataHandle,
+        W contentHandle,
+        ServerTransform transform,
+        Transaction transaction,
+        String temporalCollection)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException
+    {
+		write(uri, metadataHandle, contentHandle, transform, transaction, temporalCollection, null, getWriteParams());
+    }
+	@Override
+    public void write(String uri,
+        DocumentMetadataWriteHandle metadataHandle,
+        W contentHandle,
+        ServerTransform transform,
+        Transaction transaction,
+        String temporalCollection,
+        Calendar systemTime)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException
+    {
+		write(uri, metadataHandle, contentHandle, transform, transaction, temporalCollection, systemTime, getWriteParams());
+    }
+
+	  public void write(String uri,
+	      DocumentMetadataWriteHandle metadataHandle,
+	      W contentHandle,
+	      ServerTransform transform,
+	      Transaction transaction,
+	      String temporalCollection,
+		RequestParameters extraParams)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException
+	  {
+		write(new DocumentDescriptorImpl(uri, true), metadataHandle, contentHandle, transform, 
+			transaction, temporalCollection, null, extraParams);
+	  }
+	
+    public void write(String uri,
+        DocumentMetadataWriteHandle metadataHandle,
+        W contentHandle,
+        ServerTransform transform,
+        Transaction transaction,
+        String temporalCollection,
+        Calendar systemTime,
+		RequestParameters extraParams)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException
+    {
+		write(new DocumentDescriptorImpl(uri, true), metadataHandle, contentHandle, transform, 
+			transaction, temporalCollection, systemTime, extraParams);
+    }
+ 
+	@Override
+	public void write(DocumentDescriptor desc, W contentHandle)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		write(desc, null, contentHandle, null, null, null, null);
+	}
+	@Override
+	public void write(DocumentDescriptor desc, W contentHandle, ServerTransform transform)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		write(desc, null, contentHandle, transform, null, null, null);
+	}
+	@Override
+	public void write(DocumentDescriptor desc, DocumentMetadataWriteHandle metadata, W contentHandle)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		write(desc, metadata, contentHandle, null, null, null, null);
+	}
+	@Override
+	public void write(DocumentDescriptor desc, DocumentMetadataWriteHandle metadata, W contentHandle, ServerTransform transform)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		write(desc, metadata, contentHandle, transform, null, null, null);
+	}
+	@Override
+	public void write(DocumentDescriptor desc, W contentHandle, Transaction transaction)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		write(desc, null, contentHandle, null, transaction, null, null);
+	}
+	@Override
+	public void write(DocumentDescriptor desc, W contentHandle, ServerTransform transform, Transaction transaction)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		write(desc, null, contentHandle, transform, transaction, null, null);
+	}
+	@Override
+	public void write(DocumentDescriptor desc, DocumentMetadataWriteHandle metadataHandle, W contentHandle, Transaction transaction)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		write(desc, metadataHandle, contentHandle, null, transaction, null, null);
+	}
+	@Override
+	public void write(DocumentDescriptor desc, DocumentMetadataWriteHandle metadataHandle, W contentHandle, ServerTransform transform, Transaction transaction)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
+		write(desc, metadataHandle, contentHandle, transform, transaction, null, null);
+	}
+
+	@Override
+    public void write(DocumentDescriptor desc,
+        DocumentMetadataWriteHandle metadataHandle,
+        W contentHandle,
+        ServerTransform transform,
+        Transaction transaction,
+        String temporalCollection)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException
+    {
+		write(desc, metadataHandle, contentHandle, transform, transaction, temporalCollection, 
+			null, getWriteParams());
+	}
+	@Override
+    public void write(DocumentDescriptor desc,
+        DocumentMetadataWriteHandle metadataHandle,
+        W contentHandle,
+        ServerTransform transform,
+        Transaction transaction,
+        String temporalCollection,
+        java.util.Calendar systemTime)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException
+    {
+		write(desc, metadataHandle, contentHandle, transform, transaction, temporalCollection, 
+			systemTime, getWriteParams());
+	}
+
+	public void write(DocumentDescriptor desc,
+        DocumentMetadataWriteHandle metadataHandle,
+        W contentHandle,
+        ServerTransform transform,
+        Transaction transaction,
+        String temporalCollection,
+		RequestParameters extraParams) 
+				throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException
+			{
+		write(desc, metadataHandle, contentHandle, transform, transaction, temporalCollection, null, extraParams);
+	}
+	
+	@SuppressWarnings("rawtypes")
+    public void write(DocumentDescriptor desc,
+        DocumentMetadataWriteHandle metadataHandle,
+        W contentHandle,
+        ServerTransform transform,
+        Transaction transaction,
+        String temporalCollection,
+        java.util.Calendar systemTime,
+		RequestParameters extraParams)
+	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException
+    {
+		if (desc == null)
+			throw new IllegalArgumentException("Writing document with null identifier");
+
+		if (logger.isInfoEnabled())
+			logger.info("Writing content for {}",desc.getUri());
+
+		if (metadataHandle != null) {
+			HandleImplementation metadataBase = HandleAccessor.checkHandle(metadataHandle, "metadata");
+			Format metadataFormat = metadataBase.getFormat();
+			if (metadataFormat == null || (metadataFormat != Format.JSON && metadataFormat != Format.XML)) {
+				if (logger.isWarnEnabled())
+					logger.warn("Unsupported metadata format {}, using XML",metadataFormat.name());
+				metadataBase.setFormat(Format.XML);
+			}
+		}
+
+		checkContentFormat(contentHandle);
+
+		extraParams = addTemporalParams(extraParams, temporalCollection, systemTime);
+
+		services.putDocument(
+				requestLogger,
+				desc,
+				(transaction == null) ? null : transaction.getTransactionId(),
+				(metadataHandle != null) ? processedMetadata : null,
+				mergeTransformParameters(
+						(transform != null) ? transform : getWriteTransform(),
+						extraParams
+						),
+				metadataHandle,
+				contentHandle
+				);
+	}
+
+	@Override
+	public void delete(String uri) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
+		delete(uri, null);
+	}
+	@Override
+	public void delete(String uri, Transaction transaction) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
+		delete(new DocumentDescriptorImpl(uri, true), transaction);
+	}
+	@Override
+    public void delete(DocumentDescriptor desc) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
+		delete(desc, null, null, null);
+    }
+	@Override
+    public void delete(DocumentDescriptor desc, Transaction transaction)
+	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
+		delete(desc, transaction, null, null);
+	}
+
+	@Override
+	public void delete(String uri,
+			Transaction transaction,
+			String temporalCollection)
+	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException
+	{
+		delete(new DocumentDescriptorImpl(uri, true), transaction, temporalCollection, null);
+	}
+	@Override
+	public void delete(String uri,
+			Transaction transaction,
+			String temporalCollection,
+			java.util.Calendar systemTime)
+	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException
+	{
+		delete(new DocumentDescriptorImpl(uri, true), transaction, temporalCollection, systemTime);
+	}
+	@Override
+	public void delete(DocumentDescriptor desc,
+			Transaction transaction,
+			String temporalCollection)
+	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
+		delete(desc, transaction, temporalCollection, null);
+	}
+	@Override
+	public void delete(DocumentDescriptor desc,
+			Transaction transaction,
+			String temporalCollection,
+			java.util.Calendar systemTime)
+	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException
+	{
+		if (desc == null)
+			throw new IllegalArgumentException("Deleting document with null identifier");
+
+		if (logger.isInfoEnabled())
+			logger.info("Deleting {}",desc.getUri());
+
+		RequestParameters extraParams = addTemporalParams(new RequestParameters(), temporalCollection, systemTime);
+
+		services.deleteDocument(requestLogger, desc, (transaction == null) ? null : transaction.getTransactionId(), 
+			null, extraParams);
+    }
+
+	// shortcut creators
+	@Override
+    public DocumentDescriptor createAs(DocumentUriTemplate template, Object content)
+	throws ForbiddenUserException, FailedRequestException {
+		return createAs(template, null, content, null);
+    }
+	@Override
+    public DocumentDescriptor createAs(DocumentUriTemplate template, Object content, ServerTransform transform)
+	throws ForbiddenUserException, FailedRequestException {
+		return createAs(template, null, content, transform);
+    }
+	@Override
+    public DocumentDescriptor createAs(DocumentUriTemplate template, DocumentMetadataWriteHandle metadataHandle, Object content)
+	throws ForbiddenUserException, FailedRequestException {
+		return createAs(template, metadataHandle, content, null);
+    }
+	@Override
+    public DocumentDescriptor createAs(DocumentUriTemplate template, DocumentMetadataWriteHandle metadataHandle, Object content, ServerTransform transform)
+	throws ForbiddenUserException, FailedRequestException {
+		Class as = content.getClass();
+		W writeHandle = null;
+		if (AbstractWriteHandle.class.isAssignableFrom(as)) {
+			AbstractWriteHandle handle = (AbstractWriteHandle) content;
+			writeHandle = castAbstractWriteHandle(null, handle);			
+		} else {
+			ContentHandle handle = getHandleRegistry().makeHandle(as);
+			Utilities.setHandleContent(handle, content);
+			writeHandle = castAbstractWriteHandle(as, handle);			
+		}
+		return create(template, metadataHandle, writeHandle, transform);
+	}
+
+	// strongly typed creators
+	@Override
+	public DocumentDescriptor create(DocumentUriTemplate template, W contentHandle)
+	throws ForbiddenUserException, FailedRequestException {
+		return create(template, null, contentHandle, null, null, null, null);
+	}
+	@Override
+	public DocumentDescriptor create(DocumentUriTemplate template, W contentHandle, 
+			ServerTransform transform)
+	throws ForbiddenUserException, FailedRequestException {
+		return create(template, null, contentHandle, transform, null, null, null);
+	}
+	@Override
+	public DocumentDescriptor create(DocumentUriTemplate template, W contentHandle,
+			Transaction transaction)
+	throws ForbiddenUserException, FailedRequestException {
+		return create(template, null, contentHandle, null, transaction, null, null);
+	}
+	@Override
+	public DocumentDescriptor create(DocumentUriTemplate template, W contentHandle,
+			ServerTransform transform, Transaction transaction)
+	throws ForbiddenUserException, FailedRequestException {
+		return create(template, null, contentHandle, transform, transaction, null, null);
+	}
+	@Override
+	public DocumentDescriptor create(DocumentUriTemplate template, DocumentMetadataWriteHandle metadataHandle,
+			W contentHandle)
+	throws ForbiddenUserException, FailedRequestException {
+		return create(template, metadataHandle, contentHandle, null, null, null, null);
+	}
+	@Override
+	public DocumentDescriptor create(DocumentUriTemplate template, DocumentMetadataWriteHandle metadataHandle,
+			W contentHandle, ServerTransform transform)
+	throws ForbiddenUserException, FailedRequestException {
+		return create(template, metadataHandle, contentHandle, transform, null, null, null);
+	}
+	@Override
+	public DocumentDescriptor create(DocumentUriTemplate template, DocumentMetadataWriteHandle metadataHandle,
+			W contentHandle, Transaction transaction)
+	throws ForbiddenUserException, FailedRequestException {
+		return create(template, metadataHandle, contentHandle, null, transaction, null, null);
+	}
+	@Override
+	public DocumentDescriptor create(DocumentUriTemplate template, DocumentMetadataWriteHandle metadataHandle,
+			W contentHandle, ServerTransform transform, Transaction transaction)
+	throws ForbiddenUserException, FailedRequestException {
+		return create(template, metadataHandle, contentHandle, transform, transaction, null, null);
+	}
+	@Override
+  public DocumentDescriptor create(DocumentUriTemplate template,
+      DocumentMetadataWriteHandle metadataHandle,
+      W contentHandle,
+      ServerTransform transform,
+      Transaction transaction,
+      String temporalCollection)
+throws ForbiddenUserException, FailedRequestException
+  {
+	return create(template, metadataHandle, contentHandle, transform, transaction, 
+		temporalCollection, null, getWriteParams());
+  }
+	@Override
+    public DocumentDescriptor create(DocumentUriTemplate template,
+        DocumentMetadataWriteHandle metadataHandle,
+        W contentHandle,
+        ServerTransform transform,
+        Transaction transaction,
+        String temporalCollection,
+        java.util.Calendar systemTime)
+	throws ForbiddenUserException, FailedRequestException
+    {
+		return create(template, metadataHandle, contentHandle, transform, transaction, 
+			temporalCollection, systemTime, getWriteParams());
+    }
+  public DocumentDescriptor create(DocumentUriTemplate template,
+      DocumentMetadataWriteHandle metadataHandle,
+      W contentHandle,
+      ServerTransform transform,
+      Transaction transaction,
+      String temporalCollection,
+	RequestParameters extraParams) {
+		return create(template, metadataHandle, contentHandle, transform, transaction, temporalCollection, null, extraParams);
+	}
+	@SuppressWarnings("rawtypes")
+    public DocumentDescriptor create(DocumentUriTemplate template,
+        DocumentMetadataWriteHandle metadataHandle,
+        W contentHandle,
+        ServerTransform transform,
+        Transaction transaction,
+        String temporalCollection,
+        java.util.Calendar systemTime,
+		RequestParameters extraParams)
+    {
+		if (logger.isInfoEnabled())
+			logger.info("Creating content");
+
+		if (metadataHandle != null) {
+			HandleImplementation metadataBase = HandleAccessor.checkHandle(metadataHandle, "metadata");
+			Format metadataFormat = metadataBase.getFormat();
+			if (metadataFormat == null || (metadataFormat != Format.JSON && metadataFormat != Format.XML)) {
+				if (logger.isWarnEnabled())
+					logger.warn("Unsupported metadata format {}, using XML",metadataFormat.name());
+				metadataBase.setFormat(Format.XML);
+			}
+		}
+
+		extraParams = addTemporalParams(extraParams, temporalCollection, systemTime);
+
+		checkContentFormat(contentHandle);
+
+		return services.postDocument(
+				requestLogger,
+				template,
+				(transaction == null) ? null : transaction.getTransactionId(),
+				(metadataHandle != null) ? processedMetadata : null,
+				mergeTransformParameters(
+						(transform != null) ? transform : getWriteTransform(),
+						extraParams
+						),
+				metadataHandle,
+				contentHandle
+				);
+	}
+
+	@Override
+	public void patchAs(String uri, Object patch)
+	throws ForbiddenUserException, FailedRequestException {
+		if (patch == null) {
+			throw new IllegalArgumentException("no patch to apply");
+		}
+
+		Class as = patch.getClass();
+
+		DocumentPatchHandle patchHandle = null;
+		if (DocumentPatchHandle.class.isAssignableFrom(as)) {
+			patchHandle = (DocumentPatchHandle) patch;
+		} else {
+			ContentHandle handle = getHandleRegistry().makeHandle(as);
+			if (!DocumentPatchHandle.class.isAssignableFrom(handle.getClass())) {
+				throw new IllegalArgumentException(
+						"Handle "+handle.getClass().getName()+
+						" cannot be used to apply patch as "+as.getName()
+						);
+			}
+			Utilities.setHandleContent(handle, patch);
+			patchHandle = (DocumentPatchHandle) handle;
+		}
+
+		patch(uri, patchHandle);
+	}
+
+	@Override
+	public void patch(String uri, DocumentPatchHandle patch)
+	throws ForbiddenUserException, FailedRequestException {
+		patch(uri, patch, null);
+	}
+	@Override
+	public void patch(String uri, DocumentPatchHandle patch, Transaction transaction)
+	throws ForbiddenUserException, FailedRequestException {
+		patch(new DocumentDescriptorImpl(uri, true), patch, transaction);
+	}
+	@Override
+	public void patch(DocumentDescriptor desc, DocumentPatchHandle patch)
+	throws ForbiddenUserException, FailedRequestException {
+		patch(desc, patch, null);
+	}
+	@Override
+	public void patch(DocumentDescriptor desc, DocumentPatchHandle patch, Transaction transaction)
+	throws ForbiddenUserException, FailedRequestException {
+		if (logger.isInfoEnabled())
+			logger.info("Patching document");
+
+		DocumentPatchHandleImpl builtPatch = 
+			(patch instanceof DocumentPatchHandleImpl) ?
+			(DocumentPatchHandleImpl) patch : null;
+		services.patchDocument(
+				requestLogger,
+				desc,
+				(transaction == null) ? null : transaction.getTransactionId(),
+				(builtPatch != null) ?
+						builtPatch.getMetadata() : processedMetadata,
+				(builtPatch != null) ?
+						builtPatch.isOnContent() : true,
+				patch
+				);
+	}
+
+	@Override
+    public  T readMetadata(String uri, T metadataHandle) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
+		return readMetadata(uri, metadataHandle, null);
+    }
+	@Override
+    public  T readMetadata(String uri, T metadataHandle, Transaction transaction) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
+		read(uri, metadataHandle, null, transaction);
+
+		return metadataHandle;
+    }
+
+	@Override
+    public void writeMetadata(String uri, DocumentMetadataWriteHandle metadataHandle) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
+		writeMetadata(uri, metadataHandle, null);
+    }
+	@Override
+    public void writeMetadata(String uri, DocumentMetadataWriteHandle metadataHandle, Transaction transaction) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
+		write(uri, metadataHandle, (W) null, transaction);
+    }
+
+	@Override
+    public void writeDefaultMetadata(String uri) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
+		writeDefaultMetadata(uri, null);
+    }
+	@Override
+    public void writeDefaultMetadata(String uri, Transaction transaction) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
+		if (uri == null)
+			throw new IllegalArgumentException("Resetting document metadata with null identifier");
+
+		if (logger.isInfoEnabled())
+			logger.info("Resetting metadata for {}",uri);
+
+		services.deleteDocument(requestLogger, new DocumentDescriptorImpl(uri, true), 
+				(transaction == null) ? null : transaction.getTransactionId(), processedMetadata, getWriteParams());
+    }
+
+	@Override
+    public ServerTransform getReadTransform() {
+    	return readTransform;
+    }
+	@Override
+    public void setReadTransform(ServerTransform transform) {
+    	this.readTransform = transform;
+    }
+
+	@Override
+    public ServerTransform getWriteTransform() {
+    	return writeTransform;
+    }
+	@Override
+    public void setWriteTransform(ServerTransform transform) {
+    	this.writeTransform = transform;
+    }
+
+	@Override
+    public String getForestName() {
+    	return forestName;
+    }
+	@Override
+    public void setForestName(String forestName) {
+    	this.forestName = forestName;
+    }
+
+	@Override
+	public DocumentDescriptor newDescriptor(String uri) {
+		return new DocumentDescriptorImpl(uri, false);
+	}
+
+	@Override
+    public DocumentUriTemplate newDocumentUriTemplate(String extension) {
+		return new DocumentUriTemplateImpl(extension);
+	}
+
+	@Override
+    public DocumentMetadataPatchBuilder newPatchBuilder(Format pathFormat) {
+    	return new DocumentMetadataPatchBuilderImpl(pathFormat);
+    }
+
+	private void checkContentFormat(Object contentHandle) {
+		checkContentFormat(HandleAccessor.checkHandle(contentHandle, "content"));
+	}
+	@SuppressWarnings("rawtypes")
+	private void checkContentFormat(HandleImplementation contentBase) {
+		if (contentBase == null)
+			return;
+
+		if (contentFormat != null && contentFormat != Format.UNKNOWN) {
+			Format currFormat = contentBase.getFormat();
+			if (currFormat != contentFormat) {
+				contentBase.setFormat(contentFormat);
+				if (currFormat != Format.UNKNOWN)
+					contentBase.setMimetype(contentFormat.getDefaultMimetype());
+			}
+		}
+	}
+	protected RequestParameters mergeTransformParameters(ServerTransform transform, RequestParameters extraParams) {
+		if (transform == null)
+			return extraParams;
+
+		if (extraParams == null)
+			extraParams = new RequestParameters();
+
+		transform.merge(extraParams);
+
+		return extraParams;
+	}
+
+	// hooks for extension
+	protected RequestParameters getReadParams() {
+		return null;
+	}
+	protected RequestParameters getWriteParams() {
+		return null;
+	}
+
+	protected RequestParameters addTemporalParams(RequestParameters params, String temporalCollection, Calendar systemTime) {
+		if ( params == null ) params = new RequestParameters();
+		if ( temporalCollection != null ) params.add("temporal-collection", temporalCollection);
+		if ( systemTime != null ) {
+			String formattedSystemTime = DatatypeConverter.printDateTime(systemTime);
+			params.add("system-time", formattedSystemTime);
+		}
+		return params;
+	}
+}

From 0ca9df7f3ecf7a82eb895ba9508ff36782a201ac Mon Sep 17 00:00:00 2001
From: sammefford 
Date: Tue, 4 Nov 2014 09:51:12 -0700
Subject: [PATCH 265/357] fix #174--my previous fix for #119 made Java 8 work
 but broke prior versions of Java which don't recognize the -Xdoclint flag

---
 pom.xml | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/pom.xml b/pom.xml
index ff4e1e0a2..03b11392f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -45,10 +45,6 @@
 					public
 					${javadoc-title}
 					
-					
-					-Xdoclint:none
 				
 				
 					
@@ -165,6 +161,20 @@
         
 		
 	
+	
+		
+			Java 8
+			
+				1.8
+			
+			
+				
+				-Xdoclint:none
+			
+		
+	
 	
 		
         
-
+
+
   test1",convertXMLDocumentToString(dh.get()));
-				 }else{
-					 assertEquals("element node ","",convertXMLDocumentToString(dh.get()));
-				 }
-			 }
-			 else if(er.getType().equals(Type.JSON)){
+			if(er.getType().equals(Type.JSON)){
 				 
 					 JacksonHandle jh = new JacksonHandle();
 					 jh=er.get(jh);
@@ -159,7 +149,7 @@ else if(er.getType().equals(Type.TEXTNODE)){
 //				 System.out.println("type boolean:"+er.getBoolean());
 			 }
 			 else if(er.getType().equals(Type.INTEGER)){
-//				 System.out.println("type Integer: "+er.getNumber().longValue());
+				 System.out.println("type Integer: "+er.getNumber().longValue());
 				 assertEquals("count of documents ",31,er.getNumber().intValue()); 
 			 }
 			 else if(er.getType().equals(Type.STRING)){
@@ -188,7 +178,7 @@ else if(er.getType().equals(Type.STRING)){
 				 
 			 }else if(er.getType().equals(Type.DECIMAL)){
 //				 System.out.println("Testing is Decimal? "+er.getAs(String.class));
-				 assertEquals("Returns me a Decimal :","10.5",er.getAs(String.class));
+				 assertEquals("Returns me a Decimal :","1.0471975511966",er.getAs(String.class));
 				 
 			 }else if(er.getType().equals(Type.DOUBLE)){
 //				 System.out.println("Testing is Double? "+er.getAs(String.class));
@@ -317,7 +307,7 @@ else if(er.getType().equals(Type.TEXTNODE)){
 		 
 	 }
 	}
-	//This test is intended to test eval(T handle), passing input stream handle with xqueries that retruns different types, formats
+	//This test is intended to test eval(T handle), passing input stream handle with javascript that retruns different types, formats
 	@Test
 	public void testJSReturningDifferentTypesOrder2() throws Exception {
 	
@@ -376,22 +366,23 @@ public void testJSDifferentVariableTypes() throws Exception {
 						+"var myBool ;"
 						+"var myInteger;"
 						+"var myDecimal;"
-						+"var myDouble ;"
-						+"var myFloat;"
 						+"var myJsonObject;"
 						+"var myNull;"
 						+ "var myJsonArray;"
 						+ "var myJsonNull;"
-						+ "results.push(myString,myBool,myInteger,myDecimal,myDouble,myFloat,myNull,myJsonObject,myJsonArray,myJsonNull);"
+						+ "results.push(myString,myBool,myInteger,myDecimal,myJsonObject,myJsonArray);"
 						+"xdmp.arrayValues(results)";
 		
 		ServerEvaluationCall evl= client.newServerEval().javascript(query1);
 		evl.addVariable("myString", "xml")
-		.addVariable("myBool", true).addVariable("myInteger", (int)31)
-		.addVariable("myDecimal", 10.5).addVariable("myDouble", 1.0471975511966)
-		.addVariable("myFloat",20).addVariableAs("myJsonObject",new ObjectMapper().createObjectNode().put("foo", "v1").putNull("testNull"))
-		.addVariableAs("myNull",(String) null).addVariableAs("myJsonArray",new ObjectMapper().createArrayNode().add(1).add(2).add(3))
-		.addVariableAs("myJsonNull",new ObjectMapper().createObjectNode().nullNode() );
+		.addVariable("myBool", true)
+		.addVariable("myInteger", (int)31)
+		.addVariable("myDecimal", (double)1.0471975511966)
+		.addVariableAs("myJsonObject",new ObjectMapper().createObjectNode().put("foo", "v1").putNull("testNull"))
+		.addVariableAs("myNull",(String) null)
+		.addVariableAs("myJsonArray",new ObjectMapper().createArrayNode().add(1).add(2).add(3))
+		.addVariableAs("myJsonNull",new ObjectMapper().createObjectNode().nullNode() )
+		;
 		System.out.println(query1);
 		EvalResultIterator evr = evl.eval();
 		this.validateReturnTypes(evr);

From 148101a57a7d50a75abf2be10e19210731184e05 Mon Sep 17 00:00:00 2001
From: sammefford 
Date: Wed, 5 Nov 2014 16:12:55 -0700
Subject: [PATCH 283/357] fix #181--throw ResourceNotFoundException when
 nothing found on bulk read calls, add negative tests to validate that
 scenario

---
 .../marklogic/client/impl/JerseyServices.java |   5 +
 .../marklogic/client/pojo/PojoRepository.java |  90 ++++++----
 .../client/test/BulkReadWriteTest.java        |  25 +++
 .../marklogic/client/test/PojoFacadeTest.java | 154 ++++++++----------
 4 files changed, 156 insertions(+), 118 deletions(-)

diff --git a/src/main/java/com/marklogic/client/impl/JerseyServices.java b/src/main/java/com/marklogic/client/impl/JerseyServices.java
index 62e8caa00..895eba642 100644
--- a/src/main/java/com/marklogic/client/impl/JerseyServices.java
+++ b/src/main/java/com/marklogic/client/impl/JerseyServices.java
@@ -831,6 +831,11 @@ private JerseyResultIterator getBulkDocumentsImpl(RequestLogger reqlog,
 		}
 		JerseyResultIterator iterator = getIteratedResourceImpl(DefaultJerseyResultIterator.class,
 			reqlog, path, params, MultiPartMediaTypes.MULTIPART_MIXED);
+		if ( iterator == null ) {
+			StringBuffer uriString = new StringBuffer();
+			for ( String uri : uris ) uriString.append(" \"" + uri + "\"");
+			throw new ResourceNotFoundException("Could not find any documents with uris:" + uriString);
+		}
 		if ( iterator.getStart() == -1 ) iterator.setStart(1);
 		if ( iterator.getSize() != -1 ) {
 			if ( iterator.getPageSize() == -1 ) iterator.setPageSize(iterator.getSize());
diff --git a/src/main/java/com/marklogic/client/pojo/PojoRepository.java b/src/main/java/com/marklogic/client/pojo/PojoRepository.java
index f451e1288..5cdc70388 100644
--- a/src/main/java/com/marklogic/client/pojo/PojoRepository.java
+++ b/src/main/java/com/marklogic/client/pojo/PojoRepository.java
@@ -15,6 +15,9 @@
  */
 package com.marklogic.client.pojo;
 
+import com.marklogic.client.FailedRequestException;
+import com.marklogic.client.ForbiddenUserException;
+import com.marklogic.client.ResourceNotFoundException;
 import com.marklogic.client.Transaction;
 import com.marklogic.client.io.marker.SearchReadHandle;
 
@@ -78,84 +81,113 @@ public interface PojoRepository {
      *  {@link com.fasterxml.jackson.databind.ObjectMapper ObjectMapper} to generate the
      *  serialized JSON format.
      */
-    public void write(T entity);
+    public void write(T entity)
+    	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
     /** Does everything in {@link #write(Object) write(T)} but also adds your collections to the 
      * persisted instance.
      */
-    public void write(T entity, String... collections);
+    public void write(T entity, String... collections)
+    	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
     /** Does everything in {@link #write(Object) write(T)} but in your 
      * 
      * multi-statement transaction context.
      */
-    public void write(T entity, Transaction transaction);
+    public void write(T entity, Transaction transaction)
+    	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
     /** Does everything in {@link #write(Object) write(T)} but also adds your collections to the 
      * persisted instance and performs the write in your
      * 
      * multi-statement transaction context.
      * .
      */
-    public void write(T entity, Transaction transaction, String... collections);
+    public void write(T entity, Transaction transaction, String... collections)
+    	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
 
     /** @return true if a document exists in the database with the id */
-    public boolean exists(ID id);
+    public boolean exists(ID id)
+    	throws ForbiddenUserException, FailedRequestException;
 
     /** @return in the context of transaction, true if a document exists in the database with 
      * the id */
-    public boolean exists(ID id, Transaction transaction);
+    public boolean exists(ID id, Transaction transaction)
+    	throws ForbiddenUserException, FailedRequestException;
 
     /** @return the number of documents of type T persisted in the database */
-    public long count();
+    public long count()
+    	throws ForbiddenUserException, FailedRequestException;
 
     /** @return in the context of transaction, the number of documents of type T persisted in 
      * the database */
-    public long count(Transaction transaction);
+    public long count(Transaction transaction)
+    	throws ForbiddenUserException, FailedRequestException;
 
     /** @return the number of documents of type T persisted in the database with at least 
      * one of the criteria collections*/
-    public long count(String... collection);
+    public long count(String... collection)
+    	throws ForbiddenUserException, FailedRequestException;
 
     /** @return in the context of transaction, the number of documents of type T persisted in 
      * the database with at least one of the criteria collections*/
-    public long count(String[] collections, Transaction transaction);
+    public long count(String[] collections, Transaction transaction)
+    	throws ForbiddenUserException, FailedRequestException;
 
     /** @return the number of documents of type T persisted in the database which match
      * the query */
-    public long count(PojoQueryDefinition query);
+    public long count(PojoQueryDefinition query)
+    	throws ForbiddenUserException, FailedRequestException;
   
     /** @return in the context of transaction, the number of documents of type T persisted in the 
      * database which match the query */
-    public long count(PojoQueryDefinition query, Transaction transaction);
+    public long count(PojoQueryDefinition query, Transaction transaction)
+    	throws ForbiddenUserException, FailedRequestException;
   
     /** Deletes from the database the documents with the corresponding ids */
-    public void delete(ID... ids);
+    public void delete(ID... ids)
+    	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
 
     /** As part of transaction, deletes from the database the documents with the corresponding ids */
-    public void delete(ID[] ids, Transaction transaction);
+    public void delete(ID[] ids, Transaction transaction)
+    	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
 
     /** Deletes from the database all documents of type T persisted by the pojo facade */
-    public void deleteAll();
+    public void deleteAll()
+    	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
 
     /** As part of transaction, deletes from the database all documents of type T persisted by 
      * the pojo facade */
-    public void deleteAll(Transaction transaction);
+    public void deleteAll(Transaction transaction)
+    	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
 
     /* REST API does not currently support DELETE /search with multiple collection arguments
-    public void deleteAll(String... collections);
+    public void deleteAll(String... collections)
+    	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
     */
   
-    public T read(ID id);
-    public T read(ID id, Transaction transaction);
-    public PojoPage read(ID[] ids);
-    public PojoPage read(ID[] ids, Transaction transaction);
-    public PojoPage readAll(long start);
-    public PojoPage readAll(long start, Transaction transaction);
+    public T read(ID id)
+    	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
+    public T read(ID id, Transaction transaction)
+    	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
+    public PojoPage read(ID[] ids)
+    	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
+    public PojoPage read(ID[] ids, Transaction transaction)
+    	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
+    public PojoPage readAll(long start)
+    	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
+    public PojoPage readAll(long start, Transaction transaction)
+    	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
  
-    public PojoPage search(long start, String... collections);
-    public PojoPage search(long start, Transaction transaction, String... collections);
-    public PojoPage search(PojoQueryDefinition query, long start);
-    public PojoPage search(PojoQueryDefinition query, long start, Transaction transaction);
-    public PojoPage search(PojoQueryDefinition query, long start, SearchReadHandle searchHandle);
-    public PojoPage search(PojoQueryDefinition query, long start, SearchReadHandle searchHandle, Transaction transaction);
+    public PojoPage search(long start, String... collections)
+    	throws ForbiddenUserException, FailedRequestException;
+    public PojoPage search(long start, Transaction transaction, String... collections)
+    	throws ForbiddenUserException, FailedRequestException;
+    public PojoPage search(PojoQueryDefinition query, long start)
+    	throws ForbiddenUserException, FailedRequestException;
+    public PojoPage search(PojoQueryDefinition query, long start, Transaction transaction)
+    	throws ForbiddenUserException, FailedRequestException;
+    public PojoPage search(PojoQueryDefinition query, long start, SearchReadHandle searchHandle)
+    	throws ForbiddenUserException, FailedRequestException;
+    public PojoPage search(PojoQueryDefinition query, long start, SearchReadHandle searchHandle, Transaction transaction)
+    	throws ForbiddenUserException, FailedRequestException;
  
     public PojoQueryBuilder getQueryBuilder();
 
diff --git a/src/test/java/com/marklogic/client/test/BulkReadWriteTest.java b/src/test/java/com/marklogic/client/test/BulkReadWriteTest.java
index ec8f9c703..e6a25f79a 100644
--- a/src/test/java/com/marklogic/client/test/BulkReadWriteTest.java
+++ b/src/test/java/com/marklogic/client/test/BulkReadWriteTest.java
@@ -41,6 +41,7 @@
 import com.marklogic.client.DatabaseClient;
 import com.marklogic.client.DatabaseClientFactory;
 import com.marklogic.client.DatabaseClientFactory.Authentication;
+import com.marklogic.client.ResourceNotFoundException;
 import com.marklogic.client.Transaction;
 import com.marklogic.client.document.DocumentDescriptor;
 import com.marklogic.client.document.DocumentManager.Metadata;
@@ -184,6 +185,30 @@ public void testB_BulkRead() {
         assertEquals("Wrong start", 1, page.getStart());
         assertEquals("Wrong totalPages", 1, page.getTotalPages());
         assertEquals("Wrong estimate", 3, page.getTotalSize());
+
+        // test reading a valid plus a non-existent document
+        page = docMgr.read(DIRECTORY + "1016670.xml", "nonExistant.doc");
+        assertEquals("Should have results", true, page.hasContent());
+        assertEquals("Failed to report number of records expected", 1, page.size());
+        assertEquals("Wrong only doc", DIRECTORY + "1016670.xml", page.next().getUri());
+
+        // test reading multiple non-existent documents
+        boolean exceptionThrown = false;
+        try {
+            docMgr.read("nonExistant.doc", "nonExistant2.doc");
+        } catch (ResourceNotFoundException e) {
+            exceptionThrown = true;
+        }
+        assertTrue("ResourceNotFoundException should have been thrown", exceptionThrown);
+
+        // test reading a non-existent document (not actually a bulk operation)
+        exceptionThrown = false;
+        try {
+            docMgr.read("nonExistant.doc", new StringHandle());
+        } catch (ResourceNotFoundException e) {
+            exceptionThrown = true;
+        }
+        assertTrue("ResourceNotFoundException should have been thrown", exceptionThrown);
     }
 
     @Test
diff --git a/src/test/java/com/marklogic/client/test/PojoFacadeTest.java b/src/test/java/com/marklogic/client/test/PojoFacadeTest.java
index 0ffefbd89..50e329f33 100644
--- a/src/test/java/com/marklogic/client/test/PojoFacadeTest.java
+++ b/src/test/java/com/marklogic/client/test/PojoFacadeTest.java
@@ -15,47 +15,35 @@
  */
 package com.marklogic.client.test;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 
-import java.io.IOException;
 import java.util.Arrays;
 import java.util.Calendar;
 import java.util.GregorianCalendar;
-import java.util.Iterator;
 import java.util.TimeZone;
 
-import org.junit.FixMethodOrder;
-import org.junit.runners.MethodSorters;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
+import org.junit.FixMethodOrder;
 import org.junit.Test;
+import org.junit.runners.MethodSorters;
 
-import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
 import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
-import com.marklogic.client.document.DocumentPage;
-import com.marklogic.client.document.DocumentRecord;
-import com.marklogic.client.document.DocumentWriteSet;
-import com.marklogic.client.document.TextDocumentManager;
-import com.marklogic.client.io.Format;
-import com.marklogic.client.io.SearchHandle;
-import com.marklogic.client.io.StringHandle;
+import com.marklogic.client.ResourceNotFoundException;
+import com.marklogic.client.impl.PojoRepositoryImpl;
 import com.marklogic.client.pojo.PojoPage;
-import com.marklogic.client.pojo.PojoRepository;
+import com.marklogic.client.pojo.PojoQueryBuilder;
+import com.marklogic.client.pojo.PojoQueryBuilder.Operator;
 import com.marklogic.client.pojo.PojoQueryDefinition;
-import com.marklogic.client.query.MatchDocumentSummary;
-import com.marklogic.client.query.QueryManager;
-import com.marklogic.client.query.QueryManager.QueryView;
+import com.marklogic.client.pojo.PojoRepository;
+import com.marklogic.client.pojo.annotation.Id;
 import com.marklogic.client.query.StringQueryDefinition;
 import com.marklogic.client.query.StructuredQueryBuilder;
-import com.marklogic.client.pojo.PojoQueryBuilder.Operator;
-import com.marklogic.client.pojo.PojoQueryBuilder;
-import com.marklogic.client.pojo.annotation.Id;
-import com.marklogic.client.impl.PojoRepositoryImpl;
-import com.marklogic.client.test.BulkReadWriteTest;
 import com.marklogic.client.test.BulkReadWriteTest.CityWriter;
-import com.marklogic.client.test.PojoFacadeTest.TimeTest;
 
 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
 public class PojoFacadeTest {
@@ -184,9 +172,10 @@ public void testIssue_93() throws Exception {
         stud.setNationality("Indian");
         stud.setclassStatus(std_status.junior);
 
-        ObjectMapper objectMapper = ((PojoRepositoryImpl) students).getObjectMapper();
+        @SuppressWarnings("rawtypes")
+		ObjectMapper objectMapper = ((PojoRepositoryImpl) students).getObjectMapper();
         String value = objectMapper.writeValueAsString(stud);
-        Student stud2 = objectMapper.readValue(value, Student.class);
+        objectMapper.readValue(value, Student.class);
         students.write(stud, "students");
 
         Student student1 = students.read(id);
@@ -203,29 +192,58 @@ public void testA_LoadPojos() throws Exception {
     @Test
     public void testB_ReadPojos() throws Exception {
         PojoPage page = cities.read(new Integer[]{1185098, 2239076, 1205733});
-        Iterator iterator = page.iterator();
         int numRead = 0;
-        while ( iterator.hasNext() ) {
-            City city = iterator.next();
+        for ( City city : page ) {
             validateCity(city);
             numRead++;
         }
         assertEquals("Failed to read number of records expected", 3, numRead);
         assertEquals("PojoPage failed to report number of records expected", numRead, page.size());
+
+        // test reading a valid plus a non-existent document
+        page = cities.read(new Integer[]{1185098, -1});
+        assertEquals("Should have results", true, page.hasContent());
+        assertEquals("Failed to report number of records expected", 1, page.size());
+        assertEquals("Wrong only doc", 1185098, page.next().getGeoNameId());
+
+        // test reading only a non-existent document
+        boolean exceptionThrown = false;
+        try {
+            cities.read(-1);
+        } catch (ResourceNotFoundException e) {
+            exceptionThrown = true;
+        }
+        assertTrue("ResourceNotFoundException should have been thrown", exceptionThrown);
+
+        // test reading multiple non-existent documents
+        exceptionThrown = false;
+        try {
+            cities.read(new Integer[]{-1, -2});
+        } catch (ResourceNotFoundException e) {
+            exceptionThrown = true;
+        }
+        assertTrue("ResourceNotFoundException should have been thrown", exceptionThrown);
     }
 
-    @Test
+    @SuppressWarnings("unused")
+	@Test
     // the geo queries below currently don't work yet because underlying layers are not yet ready
     public void testC_QueryPojos() throws Exception {
+		// first test a search that matches nothing
         StringQueryDefinition stringQuery = Common.client.newQueryManager().newStringDefinition();
-        stringQuery.setCriteria("Tungi OR Dalatando OR Chittagong");
+        stringQuery.setCriteria("nonExistentStrangeWord");
         PojoPage page = cities.search(stringQuery, 1);
-        Iterator iterator = page.iterator();
         int numRead = 0;
-        while ( iterator.hasNext() ) {
-            iterator.next();
-            numRead++;
-        }
+        for ( City city : page ) numRead++;
+        assertEquals("Failed to find number of records expected", 0, numRead);
+        assertEquals("PojoPage failed to report number of records expected", numRead, page.size());
+
+
+        stringQuery = Common.client.newQueryManager().newStringDefinition();
+        stringQuery.setCriteria("Tungi OR Dalatando OR Chittagong");
+        page = cities.search(stringQuery, 1);
+        numRead = 0;
+        for ( City city : page ) numRead++;
         assertEquals("Failed to find number of records expected", 3, numRead);
         assertEquals("PojoPage failed to report number of records expected", numRead, page.size());
 
@@ -233,12 +251,8 @@ public void testC_QueryPojos() throws Exception {
         PojoQueryBuilder qb = cities.getQueryBuilder();
         PojoQueryDefinition query = qb.term("Tungi", "Dalatando", "Chittagong");
         page = cities.search(query, 1);
-        iterator = page.iterator();
         numRead = 0;
-        while ( iterator.hasNext() ) {
-            iterator.next();
-            numRead++;
-        }
+        for ( City city : page ) numRead++;
         assertEquals("Failed to find number of records expected", 3, numRead);
         assertEquals("PojoPage failed to report number of records expected", numRead, page.size());
 
@@ -258,7 +272,7 @@ public void testC_QueryPojos() throws Exception {
         query = qb.filteredQuery(qb.word("asciiName", new String[] {"wildcarded"}, 1, "Chittagong*"));
         page = cities.search(query, 1);
         numRead = 0;
-        while ( numRead < page.size() ) numRead++;
+        for ( City city : page ) numRead++;
         assertEquals("Failed to find number of records expected", 1, numRead);
         assertEquals("PojoPage failed to report number of records expected", numRead, page.size());
 
@@ -267,21 +281,15 @@ public void testC_QueryPojos() throws Exception {
         StructuredQueryBuilder sqb = Common.client.newQueryManager().newStructuredQueryBuilder("filtered");
         query = sqb.and(qb.word("asciiName", new String[] {"wildcarded"}, 1, "Chittagong*"));
         page = cities.search(query, 1);
-        iterator = page.iterator();
         numRead = 0;
-        while ( iterator.hasNext() ) {
-            iterator.next();
-            numRead++;
-        }
+        for ( City city : page ) numRead++;
         assertEquals("Failed to find number of records expected", 1, numRead);
         assertEquals("PojoPage failed to report number of records expected", numRead, page.size());
 
         query = qb.value("continent", "AF");
         page = cities.search(query, 1);
-        iterator = page.iterator();
         numRead = 0;
-        while ( iterator.hasNext() ) {
-            City city = iterator.next();
+        for ( City city : page ) {
             assertEquals("Wrong continent", "AF", city.getContinent());
             numRead++;
         }
@@ -290,10 +298,8 @@ public void testC_QueryPojos() throws Exception {
 
         query = qb.containerQuery("alternateNames", qb.term("San", "Santo"));
         page = cities.search(query, 1);
-        iterator = page.iterator();
         numRead = 0;
-        while ( iterator.hasNext() ) {
-            City city = iterator.next();
+        for ( City city : page ) {
             String alternateNames = Arrays.asList(city.getAlternateNames()).toString();
             assertTrue("Should contain San", alternateNames.contains("San"));
             numRead++;
@@ -304,10 +310,8 @@ public void testC_QueryPojos() throws Exception {
         // test numeric (integer) values
         query = qb.value("population", 374801);
         page = cities.search(query, 1);
-        iterator = page.iterator();
         numRead = 0;
-        while ( iterator.hasNext() ) {
-            City city = iterator.next();
+        for ( City city : page ) {
             assertEquals("Wrong City", "Tirana", city.getName());
             numRead++;
         }
@@ -317,10 +321,8 @@ public void testC_QueryPojos() throws Exception {
         // test numeric (fractional) values
         query = qb.and(qb.value("latitude", -34.72418), qb.value("longitude", -58.25265));
         page = cities.search(query, 1);
-        iterator = page.iterator();
         numRead = 0;
-        while ( iterator.hasNext() ) {
-            City city = iterator.next();
+        for ( City city : page ) {
             assertEquals("Wrong City", "Quilmes", city.getName());
             numRead++;
         }
@@ -330,23 +332,15 @@ public void testC_QueryPojos() throws Exception {
         // test null values
         query = qb.value("country", new String[] {null});
         page = cities.search(query, 1);
-        iterator = page.iterator();
         numRead = 0;
-        while ( iterator.hasNext() ) {
-            iterator.next();
-            numRead++;
-        }
+        for ( City city : page ) numRead++;
         assertEquals("Failed to find number of records expected", 50, numRead);
         assertEquals("PojoPage failed to report number of records expected", numRead, page.size());
 
         query = qb.range("population", Operator.LT, 350000);
         page = cities.search(query, 1);
-        iterator = page.iterator();
         numRead = 0;
-        while ( iterator.hasNext() ) {
-            iterator.next();
-            numRead++;
-        }
+        for ( City city : page ) numRead++;
         assertEquals("Failed to find number of records expected", 21, numRead);
         assertEquals("PojoPage failed to report number of records expected", numRead, page.size());
 
@@ -355,12 +349,8 @@ public void testC_QueryPojos() throws Exception {
             qb.circle(-34, -58, 100)
         );
         page = cities.search(query, 1);
-        iterator = page.iterator();
         numRead = 0;
-        while ( iterator.hasNext() ) {
-            iterator.next();
-            numRead++;
-        }
+        for ( City city : page ) numRead++;
         assertEquals("Failed to find number of records expected", 4, numRead);
         assertEquals("PojoPage failed to report number of records expected", numRead, page.size());
 
@@ -369,15 +359,8 @@ public void testC_QueryPojos() throws Exception {
             qb.circle(-34, -58, 100)
         );
         page = cities.search(query, 1);
-        iterator = page.iterator();
         numRead = 0;
-        while ( iterator.hasNext() ) {
-            @SuppressWarnings("unused")
-            City city = iterator.next();
-            numRead++;
-        }
-        // this currently doesn't work in the search:search layer
-        // when this works we'll find out how many we expect
+        for ( City city : page ) numRead++;
         assertEquals("Failed to find number of records expected", 4, numRead);
         assertEquals("PojoPage failed to report number of records expected", numRead, page.size());
 
@@ -387,15 +370,8 @@ public void testC_QueryPojos() throws Exception {
             qb.circle(-34, -58, 100)
         );
         page = cities.search(query, 1);
-        iterator = page.iterator();
         numRead = 0;
-        while ( iterator.hasNext() ) {
-            @SuppressWarnings("unused")
-            City city = iterator.next();
-            numRead++;
-        }
-        // this currently doesn't work even in the search:search layer
-        // when this works we'll find out how many we expect
+        for ( City city : page ) numRead++;
         assertEquals("Failed to find number of records expected", 4, numRead);
         assertEquals("PojoPage failed to report number of records expected", numRead, page.size());
     }

From aa23ce95370d133988cf8bbb8362f31cb5f742f6 Mon Sep 17 00:00:00 2001
From: sammefford 
Date: Wed, 5 Nov 2014 16:19:57 -0700
Subject: [PATCH 284/357] oops, I don't want to leave wire trace on

---
 src/test/java/com/marklogic/client/test/PojoFacadeTest.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/test/java/com/marklogic/client/test/PojoFacadeTest.java b/src/test/java/com/marklogic/client/test/PojoFacadeTest.java
index 50e329f33..5b1b26e62 100644
--- a/src/test/java/com/marklogic/client/test/PojoFacadeTest.java
+++ b/src/test/java/com/marklogic/client/test/PojoFacadeTest.java
@@ -54,7 +54,7 @@ public class PojoFacadeTest {
     public static void beforeClass() {
         Common.connect();
         cities = Common.client.newPojoRepository(City.class, Integer.class);
-        System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug");
+        //System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug");
     }
     @AfterClass
     public static void afterClass() {

From 73c5bb273fa129457528b22ca242bf966e06c83e Mon Sep 17 00:00:00 2001
From: Ajit George 
Date: Wed, 5 Nov 2014 15:22:08 -0800
Subject: [PATCH 285/357] New test classes

---
 .../ArtifactIndexedOnStringSub.java           | 22 ++++++++++
 .../ArtifactIndexedUnSupportedDataType.java   | 41 +++++++++++++++++++
 .../ArtifactMultipleIndexedOnInt.java         | 22 ++++++++++
 3 files changed, 85 insertions(+)
 create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/ArtifactIndexedOnStringSub.java
 create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/ArtifactIndexedUnSupportedDataType.java
 create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/ArtifactMultipleIndexedOnInt.java

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/ArtifactIndexedOnStringSub.java b/test-complete/src/test/java/com/marklogic/javaclient/ArtifactIndexedOnStringSub.java
new file mode 100644
index 000000000..e630ff078
--- /dev/null
+++ b/test-complete/src/test/java/com/marklogic/javaclient/ArtifactIndexedOnStringSub.java
@@ -0,0 +1,22 @@
+package com.marklogic.javaclient;
+
+import com.marklogic.client.pojo.annotation.PathIndexProperty;
+import com.marklogic.client.pojo.annotation.PathIndexProperty.ScalarType;
+
+/* This class is used to test range path index creation, when the Annotation is
+ * in the super class and sub class adds an additional annotation entry.
+ * 
+ * Used to test annotation in a hierarchy. 
+ */
+public class ArtifactIndexedOnStringSub extends ArtifactIndexedOnString {
+	@PathIndexProperty(scalarType = ScalarType.DOUBLE)
+	public double artifactWeight;
+
+	public double getArtifactWeight() {
+		return artifactWeight;
+	}
+
+	public ArtifactIndexedOnStringSub setArtifactWeight(double artifactWeight) {
+		this.artifactWeight = artifactWeight; return this;
+	}
+}
diff --git a/test-complete/src/test/java/com/marklogic/javaclient/ArtifactIndexedUnSupportedDataType.java b/test-complete/src/test/java/com/marklogic/javaclient/ArtifactIndexedUnSupportedDataType.java
new file mode 100644
index 000000000..1afd94063
--- /dev/null
+++ b/test-complete/src/test/java/com/marklogic/javaclient/ArtifactIndexedUnSupportedDataType.java
@@ -0,0 +1,41 @@
+package com.marklogic.javaclient;
+
+import com.marklogic.client.pojo.annotation.Id;
+import com.marklogic.client.pojo.annotation.PathIndexProperty;
+import com.marklogic.client.pojo.annotation.PathIndexProperty.ScalarType;
+
+public class ArtifactIndexedUnSupportedDataType {
+	@Id
+    public long id;
+    private String name;
+    
+    //Note: Any Type other than supported ones in ScalarType class will have compile issues.
+    @PathIndexProperty(scalarType = ScalarType.STRING)
+    public Company manufacturer;
+    private int inventory;
+  
+    public long getId() {
+        return id;
+    }
+    public ArtifactIndexedUnSupportedDataType setId(long id) {
+        this.id= id; return this;
+    }
+    public String getName() {
+        return name;
+    }
+    public ArtifactIndexedUnSupportedDataType setName(String name) {
+        this.name = name; return this;
+    }
+    public Company getManufacturer() {
+        return manufacturer;
+    }
+    public ArtifactIndexedUnSupportedDataType setManufacturer(Company manufacturer) {
+        this.manufacturer= manufacturer; return this;
+    }
+    public int getInventory() {
+        return inventory;
+    }
+    public ArtifactIndexedUnSupportedDataType setInventory(int inventory) {
+        this.inventory= inventory; return this;
+    }
+}
diff --git a/test-complete/src/test/java/com/marklogic/javaclient/ArtifactMultipleIndexedOnInt.java b/test-complete/src/test/java/com/marklogic/javaclient/ArtifactMultipleIndexedOnInt.java
new file mode 100644
index 000000000..a66cbb9ab
--- /dev/null
+++ b/test-complete/src/test/java/com/marklogic/javaclient/ArtifactMultipleIndexedOnInt.java
@@ -0,0 +1,22 @@
+package com.marklogic.javaclient;
+
+import com.marklogic.client.pojo.annotation.PathIndexProperty;
+import com.marklogic.client.pojo.annotation.PathIndexProperty.ScalarType;
+/* This class is used to test range path index creation, when the Annotation is
+ * in the super class and also in sub class on the same class property with same data type.
+ * 
+ * Used to test annotation with collisions in a hierarchy. 
+ */
+
+public class ArtifactMultipleIndexedOnInt extends ArtifactIndexedOnInt {
+	@PathIndexProperty(scalarType = ScalarType.INT)
+	private int inventory;
+
+	public int getInventory() {
+		return inventory;
+	}
+
+	public ArtifactMultipleIndexedOnInt setInventory(int inventory) {
+		this.inventory = inventory; return this;
+	}
+}

From 62c79fc867750b06fb82612283f245606b8e6157 Mon Sep 17 00:00:00 2001
From: Ajit George 
Date: Wed, 5 Nov 2014 15:24:10 -0800
Subject: [PATCH 286/357] New test methods

---
 .../TestAutomatedPathRangeIndex.java          | 153 ++++++++++++++++--
 1 file changed, 141 insertions(+), 12 deletions(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestAutomatedPathRangeIndex.java b/test-complete/src/test/java/com/marklogic/javaclient/TestAutomatedPathRangeIndex.java
index 3bee79781..8d35f0f38 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/TestAutomatedPathRangeIndex.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestAutomatedPathRangeIndex.java
@@ -18,11 +18,14 @@
 import org.junit.BeforeClass;
 import org.junit.Test;
 
+import com.fasterxml.jackson.core.JsonParser;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.marklogic.client.DatabaseClient;
 import com.marklogic.client.DatabaseClientFactory;
 import com.marklogic.client.DatabaseClientFactory.Authentication;
+import com.marklogic.client.pojo.annotation.PathIndexProperty;
+import com.marklogic.client.pojo.annotation.PathIndexProperty.ScalarType;
 import com.marklogic.client.pojo.util.GenerateIndexConfig;
 
 /*
@@ -33,6 +36,7 @@
  * daytimeduration* - This needs Java SE 8. Not tested in this class
  * URI
  * Numerals as Strings*
+ * 
  */
 
 public class TestAutomatedPathRangeIndex extends BasicJavaClientREST {
@@ -42,7 +46,7 @@ public class TestAutomatedPathRangeIndex extends BasicJavaClientREST {
 
 	private static int restPort = 8011;
 	private DatabaseClient client;
-
+	
 	/*
 	 * This class is similar to the Artifact class. It is used to test path
 	 * range index using the name field which has been annotated with @Id also.
@@ -57,8 +61,8 @@ public static void setUpBeforeClass() throws Exception {
 
 	@AfterClass
 	public static void tearDownAfterClass() throws Exception {
-		System.out.println("In tear down");
-		//tearDownJavaRESTServer(dbName, fNames, restServerName);
+		System.out.println("In tear down");	    
+		tearDownJavaRESTServer(dbName, fNames, restServerName);
 	}
 
 	@Before
@@ -72,13 +76,13 @@ public void tearDown() throws Exception {
 		 //release client
 		 client.release();
 	}
-
+	
 	/*
 	 * This Method takes the property name and value strings and verifies if
 	 * propName exist and then extracts it from the response.
 	 * The propValue string needs to be available in the extracted JsonNode's path-expression property.
 	 */
-	public static void validateRangePathIndexInDatabase(String dbName, String propName, String propValue) throws IOException {
+	public static void validateRangePathIndexInDatabase(String propName, String propValue) throws IOException {
 		InputStream jsonstream = null;
 		boolean propFound = false;
 		String propertyAvailable = null;
@@ -123,7 +127,7 @@ public static void validateRangePathIndexInDatabase(String dbName, String propNa
 	 * propName exist and then extracts it from the response.
 	 * The propValue string needs to be available in the extracted JsonNode's path-expression property.
 	 */
-	public static void validateMultipleRangePathIndexInDatabase(String dbName, String propName, String[] propValue) throws IOException {
+	public static void validateMultipleRangePathIndexInDatabase(String propName, String[] propValue) throws IOException {
 		InputStream jsonstream = null;
 		boolean propFound = false;
 		String propertyAvailable1 = null;
@@ -188,7 +192,7 @@ public void testArtifactIndexedOnInt() throws Exception {
 			if (!jnode.isNull()) {
 				setPathRangeIndexInDatabase(dbName, jnode);
 				succeeded = true;
-				validateRangePathIndexInDatabase(dbName, "range-path-index", "com.marklogic.javaclient.ArtifactIndexedOnInt/inventory");
+				validateRangePathIndexInDatabase("range-path-index", "com.marklogic.javaclient.ArtifactIndexedOnInt/inventory");
 			} else {
 				assertTrue(
 						"testArtifactIndexedOnInt - No Json node available to insert into database",
@@ -222,7 +226,7 @@ public void testArtifactIndexedOnString() throws Exception {
 			if (!jnode.isNull()) {
 				setPathRangeIndexInDatabase(dbName, jnode);
 				succeeded = true;
-				validateRangePathIndexInDatabase(dbName, "range-path-index", "com.marklogic.javaclient.ArtifactIndexedOnString/name");
+				validateRangePathIndexInDatabase("range-path-index", "com.marklogic.javaclient.ArtifactIndexedOnString/name");
 			} else {
 				assertTrue(
 						"testArtifactIndexedOnString - No Json node available to insert into database",
@@ -256,7 +260,7 @@ public void testArtifactIndexedOnDateTime() throws Exception {
 			if (!jnode.isNull()) {
 				setPathRangeIndexInDatabase(dbName, jnode);
 				succeeded = true;
-				validateRangePathIndexInDatabase(dbName, "range-path-index", "com.marklogic.javaclient.ArtifactIndexedOnDateTime/expiryDate");
+				validateRangePathIndexInDatabase("range-path-index", "com.marklogic.javaclient.ArtifactIndexedOnDateTime/expiryDate");
 			} else {
 				assertTrue(
 						"testArtifactIndexedOnDateTime - No Json node available to insert into database",
@@ -290,7 +294,7 @@ public void testArtifactIndexedOnAnyURI() throws Exception {
 			if (!jnode.isNull()) {
 				setPathRangeIndexInDatabase(dbName, jnode);
 				succeeded = true;
-				validateRangePathIndexInDatabase(dbName, "range-path-index", "com.marklogic.javaclient.ArtifactIndexedOnUri/artifactUri");
+				validateRangePathIndexInDatabase("range-path-index", "com.marklogic.javaclient.ArtifactIndexedOnUri/artifactUri");
 			} else {
 				assertTrue(
 						"testArtifactIndexedOnUri - No Json node available to insert into database",
@@ -324,7 +328,7 @@ public void testArtifactIndexedOnIntAsString() throws Exception {
 			if (!jnode.isNull()) {
 				setPathRangeIndexInDatabase(dbName, jnode);
 				succeeded = true;
-				validateRangePathIndexInDatabase(dbName, "range-path-index", "com.marklogic.javaclient.ArtifactIndexedOnIntAsString/inventory");
+				validateRangePathIndexInDatabase("range-path-index", "com.marklogic.javaclient.ArtifactIndexedOnIntAsString/inventory");
 			} else {
 				assertTrue(
 						"testArtifactIndexedOnIntAsString - No Json node available to insert into database",
@@ -361,7 +365,7 @@ public void testArtifactIndexedOnMultipleFields() throws Exception {
 			if (!jnode.isNull()) {
 				setPathRangeIndexInDatabase(dbName, jnode);
 				succeeded = true;
-				validateMultipleRangePathIndexInDatabase(dbName, "range-path-index", propValueStrArray);
+				validateMultipleRangePathIndexInDatabase("range-path-index", propValueStrArray);
 			} else {
 				assertTrue(
 						"testArtifactIndexedOnIntAsString - No Json node available to insert into database",
@@ -378,4 +382,129 @@ public void testArtifactIndexedOnMultipleFields() throws Exception {
 			}
 		}
 	}
+	
+	@Test
+	public void testArtifactIndexedOnStringInSuperClass() throws Exception {
+		boolean succeeded = false;
+		File jsonFile = null;
+		try {
+			GenerateIndexConfig.main(new String[] { "-classes",
+					"com.marklogic.javaclient.ArtifactIndexedOnStringSub",
+					"-file", "TestAutomatedPathRangeIndexStringInSuperClass.json" });
+
+			jsonFile = new File("TestAutomatedPathRangeIndexStringInSuperClass.json");
+			//Array to hold the range path index values. Refer to the class for the annotated class members.
+			String[] propValueStrArray = {"com.marklogic.javaclient.ArtifactIndexedOnStringSub/name",
+					                      "com.marklogic.javaclient.ArtifactIndexedOnStringSub/artifactWeight"};			
+			ObjectMapper mapper = new ObjectMapper();
+			JsonNode jnode = mapper.readValue(jsonFile, JsonNode.class);
+
+			if (!jnode.isNull()) {
+				setPathRangeIndexInDatabase(dbName, jnode);
+				succeeded = true;
+				validateMultipleRangePathIndexInDatabase("range-path-index", propValueStrArray);
+			} else {
+				assertTrue(
+						"testArtifactIndexedOnStringInSuperClass - No Json node available to insert into database",
+						succeeded);
+			}
+
+		} catch (IOException e) {
+			e.printStackTrace();
+		} finally {
+			try {
+				jsonFile.delete();
+			} catch (Exception e) {
+				e.printStackTrace();
+			}
+		}
+	}
+	
+	@Test
+	public void testArtifactMultipleIndexedOnInt() throws Exception {
+		boolean succeeded = false;
+		File jsonFile = null;
+		try {
+			GenerateIndexConfig.main(new String[] { "-classes",
+					"com.marklogic.javaclient.ArtifactMultipleIndexedOnInt",
+					"-file", "TestAutomatedPathRangeMultipleIndexOnInt.json" });
+
+			jsonFile = new File("TestAutomatedPathRangeMultipleIndexOnInt.json");
+			ObjectMapper mapper = new ObjectMapper();
+			JsonNode jnode = mapper.readValue(jsonFile, JsonNode.class);
+
+			if (!jnode.isNull()) {
+				setPathRangeIndexInDatabase(dbName, jnode);
+				succeeded = true;
+				validateRangePathIndexInDatabase("range-path-index", "com.marklogic.javaclient.ArtifactMultipleIndexedOnInt/inventory");
+			} else {
+				assertTrue(
+						"testArtifactMultipleIndexedOnInt - No Json node available to insert into database",
+						succeeded);
+			}
+
+		} catch (IOException e) {
+			e.printStackTrace();
+		} finally {
+			try {
+				jsonFile.delete();
+			} catch (Exception e) {
+				e.printStackTrace();
+			}
+		}
+	}
+	
+
+	/*
+	 * Test for unsupported data type: This was meant as a negative test case.
+	 * The annotation declared on user defined data type will ONLY accept types
+	 * from scalarType class. Assigning a scalarType.Company to the class member
+	 * in the annotation like below, WILL NOT COMPILE:
+	 * 
+	 * @PathIndexProperty(scalarType = ScalarType.Company) public Company
+	 * manufacture;
+	 * 
+	 * However, for the following, we can declare for example for class property
+	 * Company (which is user defined type), the scalarType.STRING in the class.
+	 * 
+	 * @PathIndexProperty(scalarType = ScalarType.STRING) public Company
+	 * manufacture;
+	 * 
+	 * MarkLogic server accepts this range path index and this was verified
+	 * manually through the MarkLogic administration GUI on the database also. 
+	 */
+	
+	@Test
+	public void testArtifactIndexedOnUnSupportedAsString() throws Exception {
+		boolean succeeded = false;
+		File jsonFile = null;
+		try {
+			GenerateIndexConfig.main(new String[] { "-classes",
+					"com.marklogic.javaclient.ArtifactIndexedUnSupportedDataType",
+					"-file", "TestArtifactIndexedOnUnSupportedAsString.json" });
+
+			jsonFile = new File("TestArtifactIndexedOnUnSupportedAsString.json");
+			ObjectMapper mapper = new ObjectMapper();
+			JsonNode jnode = mapper.readValue(jsonFile, JsonNode.class);
+
+			if (!jnode.isNull()) {
+				setPathRangeIndexInDatabase(dbName, jnode);
+				succeeded = true;
+				validateRangePathIndexInDatabase("range-path-index", "com.marklogic.javaclient.ArtifactIndexedUnSupportedDataType/manufacturer");
+			} else {
+				assertTrue(
+						"testArtifactIndexedOnUnSupportedAsString - No Json node available to insert into database",
+						succeeded);
+			}
+
+		} catch (IOException e) {
+			e.printStackTrace();
+		} finally {
+			try {
+				jsonFile.delete();
+			} catch (Exception e) {
+				e.printStackTrace();
+			}
+		}
+	}
 }

From e218f203c356f4e0069c915536caf224dad9e411 Mon Sep 17 00:00:00 2001
From: gvaidees 
Date: Thu, 6 Nov 2014 10:40:39 -0800
Subject: [PATCH 287/357] Changes to accomodate boot strap host changes

---
 .../marklogic/javaclient/ConnectedRESTQA.java | 54 ++++++++++++++++++-
 1 file changed, 52 insertions(+), 2 deletions(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java b/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java
index e06f17190..42e512bd3 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java
@@ -91,8 +91,8 @@ public static String getBootStrapHostFromML() {
 		if(!jnode.isNull()){
 
 			if(jnode.has(propName)){
-//				System.out.println(jnode.withArray(propName).get(0).asText());
-			return jnode.withArray(propName).get(0).asText();
+			System.out.println("Bootstrap Host: " + jnode.withArray(propName).get(0).get("bootstrap-host-name").asText());
+			return jnode.withArray(propName).get(0).get("bootstrap-host-name").asText();
 			}
 			else{
 				System.out.println("Missing "+propName+" field from properties end point so sending java conanical host name\n"+jnode.toString());
@@ -1580,6 +1580,56 @@ else if (respEntity != null) {
 
 	}
 
+	/*
+	 * Create a temporal collection
+	 * @dbName Database Name
+	 * @collectionName Collection Name (name of temporal collection that needs to be created)
+	 * @systemAxisName Name of System axis
+	 * @validAxisName Name of Valid axis
+	 */
+	public static void updateTemporalCollectionForLSQT(String dbName, String collectionName, boolean enable) 
+			throws Exception
+	{
+		ObjectMapper mapper = new ObjectMapper();
+		ObjectNode rootNode = mapper.createObjectNode();
+		rootNode.put( "lsqt-enabled", enable);
+
+		System.out.println(rootNode.toString());
+
+
+		DefaultHttpClient client = new DefaultHttpClient();
+		client.getCredentialsProvider().setCredentials(
+				new AuthScope("localhost", 8002),
+				new UsernamePasswordCredentials("admin", "admin"));
+
+		HttpPut put = new HttpPut("http://localhost:8002/manage/v2/databases/"+ dbName + "/temporal/collections/lsqt/properties?collection=" + collectionName);
+
+		put.addHeader("Content-type", "application/json");
+		put.addHeader("accept", "application/json");
+		put.setEntity(new StringEntity(rootNode.toString()));
+
+		HttpResponse response = client.execute(put);
+		HttpEntity respEntity = response.getEntity();
+		if( response.getStatusLine().getStatusCode() == 400)
+		{
+			HttpEntity entity = response.getEntity();
+			String responseString = EntityUtils.toString(entity, "UTF-8");
+			System.out.println(responseString);
+		}
+		else if (respEntity != null) {
+			// EntityUtils to get the response content
+			String content =  EntityUtils.toString(respEntity);
+			System.out.println(content);
+			
+			System.out.println("Temporal collection: " + collectionName + " created");
+			System.out.println("==============================================================");
+		}
+		else {
+			System.out.println("No Proper Response");
+		}
+
+	}
+
 	/*
 	 * Delete a temporal collection
 	 * @dbName Database Name

From e0e8c36395b185199f5116bcf8b655905881728b Mon Sep 17 00:00:00 2001
From: gvaidees 
Date: Thu, 6 Nov 2014 10:44:29 -0800
Subject: [PATCH 288/357] Changes to accomodate signatures for TemporalDocument
 Manager

---
 .../marklogic/javaclient/TestBiTemporal.java  | 601 ++++++++++--------
 1 file changed, 320 insertions(+), 281 deletions(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBiTemporal.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBiTemporal.java
index 050081428..809ae1a6b 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/TestBiTemporal.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBiTemporal.java
@@ -74,6 +74,7 @@ public class TestBiTemporal extends BasicJavaClientREST{
 	private final static String axisValidName = "javaERIValidAxis";
 	
 	private final static String temporalCollectionName = "javaERITemporalCollection";
+	private final static String temporalLsqtCollectionName = "javaERILsqtTemporalCollection";
 	
 	private final static String systemNodeName = "System";
 	private final static String validNodeName = "Valid";
@@ -102,6 +103,8 @@ public static void setUpBeforeClass() throws Exception {
 		ConnectedRESTQA.addElementRangeIndexTemporalAxis(dbName, axisSystemName, "", systemStartERIName, "", systemEndERIName);
 		ConnectedRESTQA.addElementRangeIndexTemporalAxis(dbName, axisValidName, "", validStartERIName, "", validEndERIName);
 		ConnectedRESTQA.addElementRangeIndexTemporalCollection(dbName, temporalCollectionName, axisSystemName, axisValidName);
+		ConnectedRESTQA.addElementRangeIndexTemporalCollection(dbName, temporalLsqtCollectionName, axisSystemName, axisValidName);
+		ConnectedRESTQA.updateTemporalCollectionForLSQT(dbName, temporalLsqtCollectionName, true);
 	}
 
 	@AfterClass
@@ -112,6 +115,7 @@ public static void tearDownAfterClass() throws Exception {
 		tearDownJavaRESTServer(dbName, fNames, restServerName);
 		
 		// Temporal collection needs to be delete before temporal axis associated with it can be deleted
+		ConnectedRESTQA.deleteElementRangeIndexTemporalCollection("Documents", temporalLsqtCollectionName);
 		ConnectedRESTQA.deleteElementRangeIndexTemporalCollection("Documents", temporalCollectionName);
 		ConnectedRESTQA.deleteElementRangeIndexTemporalAxis("Documents", axisValidName);
 		ConnectedRESTQA.deleteElementRangeIndexTemporalAxis("Documents", axisSystemName);
@@ -241,7 +245,8 @@ public void validateDefaultMetadata(DocumentMetadataHandle mh) {
 	}
 	
 	// This covers passing transforms and descriptor
-	private void insertXMLSingleDocument(String docId, String transformName) throws Exception { 
+	private void insertXMLSingleDocument(String temporalCollection,
+			String docId, String transformName) throws Exception { 
 		System.out.println("Inside insertXMLSingleDocument");
 
 		DOMHandle handle = getXMLDocumentHandle(
@@ -270,17 +275,18 @@ private void insertXMLSingleDocument(String docId, String transformName) throws
 			transformer.put("name", "Lang");
 			transformer.put("value", "English");
 
-  		docMgr.write(desc, mh, handle, transformer, null, temporalCollectionName, null);
+  		docMgr.write(desc, mh, handle, transformer, null, temporalCollection);
 		}
 		else {
-      docMgr.write(desc, mh, handle, null, null, temporalCollectionName, null);
+      docMgr.write(desc, mh, handle, null, null, temporalCollection);
   		
       // docMgr.write(docId, mh, handle, null, null, temporalCollectionName, DatatypeConverter.parseDateTime("2004-06-015T00:00:01"));
 		}
 	}
 
 	// This covers passing transforms and descriptor
-	private void updateXMLSingleDocument(String docId, String transformName) throws Exception { 
+	private void updateXMLSingleDocument(String temporalCollection,
+			String docId, String transformName) throws Exception { 
 		System.out.println("Inside updateXMLSingleDocument");
 
 		// Update the document
@@ -309,15 +315,15 @@ private void updateXMLSingleDocument(String docId, String transformName) throws
 			transformer.put("name", "Lang");
 			transformer.put("value", "English");
 
-  		docMgr.write(desc, mh, handle, transformer, null, temporalCollectionName, null);
+  		docMgr.write(desc, mh, handle, transformer, null, temporalCollection);
 		}
 		else {
-		  docMgr.write(desc, mh, handle, null, null, temporalCollectionName, null);
+		  docMgr.write(desc, mh, handle, null, null, temporalCollection);
 		}
   }
 
 	// This covers passing descriptor
-	public void deleteXMLSingleDocument(String docId) throws Exception { 
+	public void deleteXMLSingleDocument(String temporalCollection, String docId) throws Exception { 
 
 		System.out.println("Inside deleteXMLSingleDocument");		
 				
@@ -325,7 +331,7 @@ public void deleteXMLSingleDocument(String docId) throws Exception {
 
     // docMgr.delete(docId, null, temporalCollectionName, DatatypeConverter.parseDateTime("2014-06-015T00:00:01"));
 		DocumentDescriptor desc = docMgr.newDescriptor(docId);
-    docMgr.delete(desc, null, temporalCollectionName, null);    
+    docMgr.delete(desc, null, temporalCollection, null);    
 	}
 	
 	private DOMHandle getXMLDocumentHandle(
@@ -372,14 +378,21 @@ private DOMHandle getXMLDocumentHandle(
 		return handle;
 	}
 
-	public void insertJSONSingleDocument(
-			String docId, Transaction transaction) throws Exception { 
+	public void insertJSONSingleDocument(String temporalCollection,
+			String docId, Transaction transaction, java.util.Calendar systemTime) throws Exception { 
+
+		insertJSONSingleDocument(temporalCollection, docId, null, transaction, systemTime);
+	}
+
+	public void insertJSONSingleDocument(String temporalCollection,
+			String docId, String transformName) throws Exception { 
 
-		insertJSONSingleDocument(docId, transaction, null);
+		insertJSONSingleDocument(temporalCollection, docId, transformName, null, null);
 	}
 	
-	public void insertJSONSingleDocument(
-			String docId, Transaction transaction, java.util.Calendar systemTime) throws Exception { 
+	public void insertJSONSingleDocument(String temporalCollection,
+			String docId, String transformName,
+			Transaction transaction, java.util.Calendar systemTime) throws Exception { 
 
 		System.out.println("Inside insertJSONSingleDocument");
 		
@@ -390,18 +403,48 @@ public void insertJSONSingleDocument(
     docMgr.setMetadataCategories(Metadata.ALL);
 
 		// put meta-data
-		DocumentMetadataHandle mh = setMetadata(false);				
+		DocumentMetadataHandle mh = setMetadata(false);
 		
-    docMgr.write(docId, mh, handle, null, transaction, temporalCollectionName, systemTime);		
+		if (transformName != null) {
+			TransformExtensionsManager transMgr = 
+					client.newServerConfigManager().newTransformExtensionsManager();
+			ExtensionMetadata metadata = new ExtensionMetadata();
+			metadata.setTitle("Adding sjs Transform");
+			metadata.setDescription("This plugin adds 2 properties to JSON document");
+			metadata.setProvider("MarkLogic");
+			metadata.setVersion("0.1");
+			// get the transform file
+			File transformFile = new File("src/test/java/com/marklogic/javaclient/transforms/" + transformName + ".js");
+			FileHandle transformHandle = new FileHandle(transformFile);
+			transMgr.writeJavascriptTransform(transformName, transformHandle, metadata);
+			ServerTransform transformer = new ServerTransform(transformName);
+			transformer.put("name", "Lang");
+			transformer.put("value", "English");
+
+			if (systemTime != null) {
+  		  docMgr.write(docId, mh, handle, transformer, null, temporalCollection, systemTime, null);
+			}
+			else {
+  		  docMgr.write(docId, mh, handle, transformer, null, temporalCollection);
+			}
+		}
+		else {
+			if (systemTime != null) {
+			  docMgr.write(docId, mh, handle, null, transaction, temporalCollection, systemTime, null);
+			}
+			else {
+			  docMgr.write(docId, mh, handle, null, transaction, temporalCollection);
+			}
+		}
 	}
 
-	public void updateJSONSingleDocument(
-			String docId, Transaction transaction) throws Exception { 
+	public void updateJSONSingleDocument(String temporalCollection,
+			String docId) throws Exception { 
 
-		updateJSONSingleDocument(docId, transaction, null);
+		updateJSONSingleDocument(temporalCollection, docId, null, null);
 	}
 	
-	public void updateJSONSingleDocument(
+	public void updateJSONSingleDocument(String temporalCollection,
 			String docId, Transaction transaction, java.util.Calendar systemTime) throws Exception { 
 
 		System.out.println("Inside updateJSONSingleDocumentString");
@@ -413,23 +456,28 @@ public void updateJSONSingleDocument(
 		JSONDocumentManager docMgr = client.newJSONDocumentManager();
 		docMgr.setMetadataCategories(Metadata.ALL);
 		DocumentMetadataHandle mh = setMetadata(true);  // cannot set properties during update
-		docMgr.write(docId, mh, handle, null, transaction, temporalCollectionName, systemTime);
+		docMgr.write(docId, mh, handle, null, transaction, temporalCollection, systemTime, null);
 	}
 
-	public void deleteJSONSingleDocument(
+	public void deleteJSONSingleDocument(String temporalCollection,
 			String docId, Transaction transaction) throws Exception { 
-		deleteJSONSingleDocument(docId, transaction, null);
+		deleteJSONSingleDocument(temporalCollection, docId, transaction, null);
 	}
 
-	public void deleteJSONSingleDocument(
+	public void deleteJSONSingleDocument(String temporalCollection,
 			String docId, Transaction transaction, java.util.Calendar systemTime) throws Exception { 
 
 		System.out.println("Inside deleteJSONSingleDocument");		
 				
 		JSONDocumentManager docMgr = client.newJSONDocumentManager();
 
-    // docMgr.delete(docId, null, temporalCollectionName, DatatypeConverter.parseDateTime("2014-06-015T00:00:01"));
-    docMgr.delete(docId, transaction, temporalCollectionName, systemTime);    
+		// Doing the logic here to exercise the overloaded methods 
+		if (systemTime != null) {
+      docMgr.delete(docId, transaction, temporalCollection, systemTime);
+		}
+		else {
+      docMgr.delete(docId, transaction, temporalCollection);
+		}
 	}
 
 
@@ -503,7 +551,7 @@ public void testInsertXMLSingleDocumentUsingTemplate() throws Exception {
     template.setDirectory(dirName);
 		DocumentMetadataHandle mh = setMetadata(false);
 		
-    docMgr.create(template, mh, handle, null, null, temporalCollectionName, null);
+    docMgr.create(template, mh, handle, null, null, temporalCollectionName);
 
 		// Make sure there are no documents associated with "latest" collection
 		QueryManager queryMgr = client.newQueryManager();
@@ -543,10 +591,10 @@ public void testInsertAndUpdateXMLSingleDocumentUsingInvalidTransform() throws E
 		System.out.println("Inside testXMLWriteSingleDocument");
 		String docId = "javaSingleXMLDoc.xml";
 		
-		insertXMLSingleDocument(docId, null);
+		insertXMLSingleDocument(temporalCollectionName, docId, null);
 		boolean exceptionThrown = false;
 		try {
-		  updateXMLSingleDocument(docId, "add-element-xquery-invalid-bitemp-transform");
+		  updateXMLSingleDocument(temporalCollectionName, docId, "add-element-xquery-invalid-bitemp-transform");
 		}
 		catch (com.marklogic.client.FailedRequestException ex) {
 			exceptionThrown = true;		
@@ -577,7 +625,7 @@ public void testConsolidated() throws Exception {
 		// Check insert works
 		//=============================================================================		
 		// Insert XML document
-		insertXMLSingleDocument(xmlDocId, "add-element-xquery-transform");  // Transforming during insert
+		insertXMLSingleDocument(temporalCollectionName, xmlDocId, "add-element-xquery-transform");  // Transforming during insert
 		
     // Verify that the document was inserted 
 		XMLDocumentManager xmlDocMgr = client.newXMLDocumentManager();
@@ -587,7 +635,7 @@ public void testConsolidated() throws Exception {
 		
 		// Now insert a JSON document
 		String jsonDocId = "javaSingleJSONDoc.json";
-		insertJSONSingleDocument(jsonDocId, null);
+		insertJSONSingleDocument(temporalCollectionName, jsonDocId, null);
 		
     // Verify that the document was inserted 
 		JSONDocumentManager jsonDocMgr = client.newJSONDocumentManager();
@@ -599,7 +647,7 @@ public void testConsolidated() throws Exception {
 		// Check update works
 		//=============================================================================
 		// Update XML document
-		updateXMLSingleDocument(xmlDocId, null);   // Not transforming during update
+		updateXMLSingleDocument(temporalCollectionName, xmlDocId, null);   // Not transforming during update
 
 		// Make sure there are 2 documents in latest collection
 		QueryManager queryMgr = client.newQueryManager();
@@ -650,7 +698,7 @@ public void testConsolidated() throws Exception {
 	    }
 		 
 			// Update JSON document
-			updateJSONSingleDocument(jsonDocId, null);
+			updateJSONSingleDocument(temporalCollectionName, jsonDocId);
 	
 			// Make sure there are still 2 documents in latest collection
 			queryMgr = client.newQueryManager();
@@ -708,7 +756,7 @@ public void testConsolidated() throws Exception {
 			// Check delete works
 			//=============================================================================
 			// Delete one of the document
-			deleteXMLSingleDocument(xmlDocId);
+			deleteXMLSingleDocument(temporalCollectionName, xmlDocId);
 	
 			// Make sure there are still 4 documents in xmlDocId collection
 			queryMgr = client.newQueryManager();
@@ -766,7 +814,7 @@ public void testJSONConsolidated() throws Exception {
 		System.out.println("Inside testJSONConsolidated");
 		
 		String docId = "javaSingleJSONDoc.json";
-		insertJSONSingleDocument(docId, null);
+		insertJSONSingleDocument(temporalCollectionName, docId, null);
 		
     // Verify that the document was inserted 
 		JSONDocumentManager docMgr = client.newJSONDocumentManager();
@@ -789,7 +837,7 @@ public void testJSONConsolidated() throws Exception {
 		
 		//================================================================
 		// Update the document
-		updateJSONSingleDocument(docId, null);
+		updateJSONSingleDocument(temporalCollectionName, docId);
 		
     // Verify that the document was updated 
 		// Make sure there is 1 document in latest collection
@@ -889,7 +937,7 @@ public void testJSONConsolidated() throws Exception {
 		// Check delete works
 		//=============================================================================
 		// Delete one of the document
-		deleteJSONSingleDocument(docId, null);
+		deleteJSONSingleDocument(temporalCollectionName, docId, null);
 		
 		// Make sure there are still 4 documents in docId collection
 		queryMgr = client.newQueryManager();
@@ -946,7 +994,7 @@ public void testSystemTime() throws Exception {
 		String docId = "javaSingleJSONDoc.json";
 		
 		Calendar firstInsertTime = DatatypeConverter.parseDateTime("2010-01-01T00:00:01");
-		insertJSONSingleDocument(docId, null, firstInsertTime);
+		insertJSONSingleDocument(temporalLsqtCollectionName, docId, null, null, firstInsertTime);
 		
     // Verify that the document was inserted 
 		JSONDocumentManager docMgr = client.newJSONDocumentManager();
@@ -994,7 +1042,7 @@ public void testSystemTime() throws Exception {
   			
   			if (!collection.equals(docId) &&
   					!collection.equals(insertCollectionName) && 
-  					!collection.equals(temporalCollectionName) &&
+  					!collection.equals(temporalLsqtCollectionName) &&
   					!collection.equals(latestCollectionName)) {
   				assertFalse("Collection not what is expected: " + collection, true);
   			}
@@ -1030,7 +1078,7 @@ public void testSystemTime() throws Exception {
 		// Check update works
 		//=============================================================================
 		Calendar updateTime = DatatypeConverter.parseDateTime("2011-01-01T00:00:01");
-		updateJSONSingleDocument(docId, null, updateTime);
+		updateJSONSingleDocument(temporalLsqtCollectionName, docId, null, updateTime);
 		
     // Verify that the document was updated 
 		// Make sure there is 1 document in latest collection
@@ -1067,7 +1115,7 @@ record = termQueryResults.next();
 		// Make sure there are 4 documents in temporal collection
 		queryMgr = client.newQueryManager();
 		sqb = queryMgr.newStructuredQueryBuilder();
-		termQuery = sqb.collection(temporalCollectionName);
+		termQuery = sqb.collection(temporalLsqtCollectionName);
 
 		start = 1;
 		termQueryResults = docMgr.search(termQuery, start);
@@ -1145,7 +1193,7 @@ record = termQueryResults.next();
       			
       			if (!collection.equals(docId) &&
       					!collection.equals(updateCollectionName) && 
-      					!collection.equals(temporalCollectionName)) {
+      					!collection.equals(temporalLsqtCollectionName)) {
       				assertFalse("Collection not what is expected: " + collection, true);
       			}
       		}
@@ -1178,7 +1226,7 @@ record = termQueryResults.next();
       			
       			if (!collection.equals(docId) &&
       					!collection.equals(insertCollectionName) && 
-      					!collection.equals(temporalCollectionName)) {
+      					!collection.equals(temporalLsqtCollectionName)) {
       				assertFalse("Collection not what is expected: " + collection, true);
       			}
       		}
@@ -1212,7 +1260,7 @@ record = termQueryResults.next();
       			
       			if (!collection.equals(docId) &&
       					!collection.equals(insertCollectionName) && 
-      					!collection.equals(temporalCollectionName) &&
+      					!collection.equals(temporalLsqtCollectionName) &&
       					!collection.equals(latestCollectionName)) {
       				assertFalse("Collection not what is expected: " + collection, true);
       			}
@@ -1246,7 +1294,7 @@ record = termQueryResults.next();
       			
       			if (!collection.equals(docId) &&
       					!collection.equals(insertCollectionName) && 
-      					!collection.equals(temporalCollectionName)) {
+      					!collection.equals(temporalLsqtCollectionName)) {
       				assertFalse("Collection not what is expected: " + collection, true);
       			}
       		}      		
@@ -1274,7 +1322,7 @@ record = termQueryResults.next();
 		//=============================================================================
 		// Delete one of the document
 		Calendar deleteTime = DatatypeConverter.parseDateTime("2012-01-01T00:00:01");
-		deleteJSONSingleDocument(docId, null, deleteTime);
+		deleteJSONSingleDocument(temporalLsqtCollectionName, docId, null, deleteTime);
 		
 		// Make sure there are still 4 documents in docId collection
 		queryMgr = client.newQueryManager();
@@ -1306,7 +1354,7 @@ record = termQueryResults.next();
 		// Make sure there are 4 documents in temporal collection
 		queryMgr = client.newQueryManager();
 		sqb = queryMgr.newStructuredQueryBuilder();
-		termQuery = sqb.collection(temporalCollectionName);
+		termQuery = sqb.collection(temporalLsqtCollectionName);
 
 		start = 1;
 		docMgr.setMetadataCategories(Metadata.ALL);
@@ -1374,7 +1422,7 @@ record = termQueryResults.next();
       			
       			if (!collection.equals(docId) &&
       					!collection.equals(updateCollectionName) && 
-      					!collection.equals(temporalCollectionName)) {
+      					!collection.equals(temporalLsqtCollectionName)) {
       				assertFalse("Collection not what is expected: " + collection, true);
       			}
       		}
@@ -1407,7 +1455,7 @@ record = termQueryResults.next();
       			
       			if (!collection.equals(docId) &&
       					!collection.equals(insertCollectionName) && 
-      					!collection.equals(temporalCollectionName)) {
+      					!collection.equals(temporalLsqtCollectionName)) {
       				assertFalse("Collection not what is expected: " + collection, true);
       			}
       		}
@@ -1442,7 +1490,7 @@ record = termQueryResults.next();
       			
       			if (!collection.equals(docId) &&
       					!collection.equals(insertCollectionName) && 
-      					!collection.equals(temporalCollectionName)) {
+      					!collection.equals(temporalLsqtCollectionName)) {
       				assertFalse("Collection not what is expected: " + collection, true);
       			}
       		}
@@ -1475,7 +1523,7 @@ record = termQueryResults.next();
       			
       			if (!collection.equals(docId) &&
       					!collection.equals(insertCollectionName) && 
-      					!collection.equals(temporalCollectionName)) {
+      					!collection.equals(temporalLsqtCollectionName)) {
       				assertFalse("Collection not what is expected: " + collection, true);
       			}
       		}      		
@@ -1516,7 +1564,7 @@ public void testReadingSpecificVersionBasedOnTime() throws Exception {
 		String docId = "javaSingleJSONDoc.json";
 		
 		Calendar firstInsertTime = DatatypeConverter.parseDateTime("2010-01-01T00:00:01");
-		insertJSONSingleDocument(docId, null, firstInsertTime);
+		insertJSONSingleDocument(docId, firstInsertTime);
 		
     // Verify that the document was inserted 
 		JSONDocumentManager docMgr = client.newJSONDocumentManager();
@@ -1536,7 +1584,7 @@ public void testReadingSpecificVersionBasedOnTime() throws Exception {
 		// Check update works
 		//=============================================================================
 		Calendar updateTime = DatatypeConverter.parseDateTime("2011-01-01T00:00:01");
-		updateJSONSingleDocument(docId, null, updateTime);
+		updateJSONSingleDocument(temporalCollectionName, docId, null, updateTime);
 		
     // Verify that the document was updated 
 		// Make sure there is 1 document in latest collection
@@ -2014,39 +2062,22 @@ record = termQueryResults.next();
 		***/
 	}
 	
-	
-	
-	
-	
-	
-	
-	
-	
-	
-	
-	
-	
-	
-	
-	
-	
-	
 	@Test
 	public void testSystemTimeUsingInvalidTime() throws Exception { 
 
-		System.out.println("Inside testSystemTime");
+		System.out.println("Inside testSystemTimeUsingInvalidTime");
 		
 		String docId = "javaSingleJSONDoc.json";
 		
 		Calendar firstInsertTime = DatatypeConverter.parseDateTime("2010-01-01T00:00:01");
-		insertJSONSingleDocument(docId, null, firstInsertTime);
+		insertJSONSingleDocument(temporalLsqtCollectionName, docId, null, null, firstInsertTime);
 		
 		// Update by passing a system time that is less than previous one
 		Calendar updateTime = DatatypeConverter.parseDateTime("2010-01-01T00:00:00");
 		
 		boolean exceptionThrown = false;
 		try {
-		  updateJSONSingleDocument(docId, null, updateTime);
+		  updateJSONSingleDocument(temporalLsqtCollectionName, docId, null, updateTime);
 		}
 		catch (com.marklogic.client.FailedRequestException ex) {
 			System.out.println(ex.getMessage());
@@ -2064,7 +2095,7 @@ public void testSystemTimeUsingInvalidTime() throws Exception {
 
 		exceptionThrown = false;
 		try {
-			deleteJSONSingleDocument(docId, null, deleteTime);
+			deleteJSONSingleDocument(temporalLsqtCollectionName, docId, null, deleteTime);
 		}
 		catch (com.marklogic.client.FailedRequestException ex) {
 			System.out.println(ex.getMessage());
@@ -2076,147 +2107,131 @@ public void testSystemTimeUsingInvalidTime() throws Exception {
 		}		
 	}
 	
-	@Test
+	// @Test
 	// BUG: REST API bug around transactions fails this test
-	public void testTransactionCommit() throws Exception { 
+	public void TransactionCommit() throws Exception { 
 
 		System.out.println("Inside testTransactionCommit");
 		
 		String docId = "javaSingleJSONDoc.json";
 		
 		Transaction transaction = client.openTransaction("Transaction for BiTemporal");
-		try {
-		  insertJSONSingleDocument(docId, transaction);
-		}
-		catch (Exception ex) {
-			transaction.rollback();
+		try {					
+	    // Verify that the document was inserted 
+			JSONDocumentManager docMgr = client.newJSONDocumentManager();
+			DocumentPage readResults = docMgr.read(transaction, docId); 
+	
+			System.out.println("Number of results = " + readResults.size());
+			if (readResults.size() != 1) {
+				transaction.rollback();
+				
+				assertEquals("Wrong number of results", 1, readResults.size());
+			}
 			
-			assertTrue("insertJSONSingleDocument failed in testTransactionCommit", false);
-		}
-		
-    // Verify that the document was inserted 
-		JSONDocumentManager docMgr = client.newJSONDocumentManager();
-		DocumentPage readResults = docMgr.read(transaction, docId); 
-
-		System.out.println("Number of results = " + readResults.size());
-		if (readResults.size() != 1) {
-			transaction.rollback();
+			DocumentRecord latestDoc = readResults.next();
+			System.out.println("URI after insert = " + latestDoc.getUri());
 			
-			assertEquals("Wrong number of results", 1, readResults.size());
-		}
-		
-		DocumentRecord latestDoc = readResults.next();
-		System.out.println("URI after insert = " + latestDoc.getUri());
-		
-		if (!docId.equals(latestDoc.getUri())) {
-			transaction.rollback();
+			if (!docId.equals(latestDoc.getUri())) {
+				transaction.rollback();
+				
+				assertEquals("Document uri wrong after insert", docId, latestDoc.getUri());
+			}
 			
-			assertEquals("Document uri wrong after insert", docId, latestDoc.getUri());
-		}
-		
-		// Make sure document is not visible to any other transaction
-		readResults = docMgr.read(docId); 
-		System.out.println("Number of results = " + readResults.size());
-		if (readResults.size() != 0) {
-			transaction.rollback();
+			// Make sure document is not visible to any other transaction
+			readResults = docMgr.read(docId); 
+			System.out.println("Number of results = " + readResults.size());
+			if (readResults.size() != 0) {
+				transaction.rollback();
+				
+				assertEquals("Wrong number of results", 1, readResults.size());
+			}
 			
-			assertEquals("Wrong number of results", 1, readResults.size());
-		}
-		
-		try {
-	  	updateJSONSingleDocument(docId, transaction);		
-		}
-		catch (Exception ex) {
-			transaction.rollback();
+		  updateJSONSingleDocument(temporalCollectionName, docId, transaction, null);		
+	
+			QueryManager queryMgr = client.newQueryManager();
+			StructuredQueryBuilder sqb = queryMgr.newStructuredQueryBuilder();
+			StructuredQueryDefinition termQuery = sqb.collection(latestCollectionName);
 			
-			assertTrue("updateJSONSingleDocument failed in testTransactionCommit", false);
-		}
-
-		QueryManager queryMgr = client.newQueryManager();
-		StructuredQueryBuilder sqb = queryMgr.newStructuredQueryBuilder();
-		StructuredQueryDefinition termQuery = sqb.collection(latestCollectionName);
-		
-		long start = 1;
-		DocumentPage termQueryResults = docMgr.search(termQuery, start, transaction);
-		System.out.println("Number of results = " + termQueryResults.getTotalSize());
-		
-		if ( termQueryResults.getTotalSize() != 1) {
-			transaction.rollback();
+			long start = 1;
+			DocumentPage termQueryResults = docMgr.search(termQuery, start, transaction);
+			System.out.println("Number of results = " + termQueryResults.getTotalSize());
 			
-			assertEquals("Wrong number of results", 1, termQueryResults.getTotalSize());
-		}
-
-		// There should be 4 documents in docId collection
-		queryMgr = client.newQueryManager();
-		sqb = queryMgr.newStructuredQueryBuilder();
-		termQuery = sqb.collection(docId);
-		
-		start = 1;
-		termQueryResults = docMgr.search(termQuery, start, transaction);
-		System.out.println("Number of results = " + termQueryResults.getTotalSize());
-
-		if (termQueryResults.getTotalSize() != 4) {
-			transaction.rollback();
+			if ( termQueryResults.getTotalSize() != 1) {
+				transaction.rollback();
+				
+				assertEquals("Wrong number of results", 1, termQueryResults.getTotalSize());
+			}
+	
+			// There should be 4 documents in docId collection
+			queryMgr = client.newQueryManager();
+			sqb = queryMgr.newStructuredQueryBuilder();
+			termQuery = sqb.collection(docId);
 			
-			assertEquals("Wrong number of results", 4, termQueryResults.getTotalSize());
-		}
-		
-		// Search for documents using doc uri collection and no transaction object passed.
-		// There should be 0 documents in docId collection
-		queryMgr = client.newQueryManager();
-		sqb = queryMgr.newStructuredQueryBuilder();
-		termQuery = sqb.collection(docId);
-		
-		start = 1;
-		termQueryResults = docMgr.search(termQuery, start);
-		System.out.println("Number of results = " + termQueryResults.getTotalSize());
-
-		if (termQueryResults.getTotalSize() != 0) {
-			transaction.rollback();
+			start = 1;
+			termQueryResults = docMgr.search(termQuery, start, transaction);
+			System.out.println("Number of results = " + termQueryResults.getTotalSize());
+	
+			if (termQueryResults.getTotalSize() != 4) {
+				transaction.rollback();
+				
+				assertEquals("Wrong number of results", 4, termQueryResults.getTotalSize());
+			}
 			
+			// Search for documents using doc uri collection and no transaction object passed.
+			// There should be 0 documents in docId collection
+			queryMgr = client.newQueryManager();
+			sqb = queryMgr.newStructuredQueryBuilder();
+			termQuery = sqb.collection(docId);
+			
+			start = 1;
+			termQueryResults = docMgr.search(termQuery, start);
+			System.out.println("Number of results = " + termQueryResults.getTotalSize());
+	
+			if (termQueryResults.getTotalSize() != 0) {
+				transaction.rollback();
+				
+				assertEquals("Wrong number of results", 0, termQueryResults.getTotalSize());
+			}
+			
+		 deleteJSONSingleDocument(temporalCollectionName, docId, transaction);
+	
+			// There should be no documents in latest collection
+			queryMgr = client.newQueryManager();
+			sqb = queryMgr.newStructuredQueryBuilder();
+			termQuery = sqb.collection(latestCollectionName);
+			
+			start = 1;
+			termQueryResults = docMgr.search(termQuery, start, transaction);
+			System.out.println("Number of results = " + termQueryResults.getTotalSize());
+	
+			if ( termQueryResults.getTotalSize() != 0) {
+				transaction.rollback();
+				
+				assertEquals("Wrong number of results", 0, termQueryResults.getTotalSize());
+			}
+			
+			transaction.commit();
+			
+			// There should still be no documents in latest collection
+			queryMgr = client.newQueryManager();
+			sqb = queryMgr.newStructuredQueryBuilder();
+			termQuery = sqb.collection(latestCollectionName);
+			
+			start = 1;
+			termQueryResults = docMgr.search(termQuery, start);
+			System.out.println("Number of results = " + termQueryResults.getTotalSize());
 			assertEquals("Wrong number of results", 0, termQueryResults.getTotalSize());
 		}
-
-		try {
-	  	deleteJSONSingleDocument(docId, transaction);		
-		}
 		catch (Exception ex) {
 			transaction.rollback();
 			
-			assertTrue("deleteJSONSingleDocument failed in testTransactionCommit", false);
-		}
-
-		// There should be no documents in latest collection
-		queryMgr = client.newQueryManager();
-		sqb = queryMgr.newStructuredQueryBuilder();
-		termQuery = sqb.collection(latestCollectionName);
-		
-		start = 1;
-		termQueryResults = docMgr.search(termQuery, start, transaction);
-		System.out.println("Number of results = " + termQueryResults.getTotalSize());
-
-		if ( termQueryResults.getTotalSize() != 0) {
-			transaction.rollback();
-			
-			assertEquals("Wrong number of results", 0, termQueryResults.getTotalSize());
+			assertTrue("testTransactionCommit failed", false);
 		}
-		
-		transaction.commit();
-		
-		// There should still be no documents in latest collection
-		queryMgr = client.newQueryManager();
-		sqb = queryMgr.newStructuredQueryBuilder();
-		termQuery = sqb.collection(latestCollectionName);
-		
-		start = 1;
-		termQueryResults = docMgr.search(termQuery, start);
-		System.out.println("Number of results = " + termQueryResults.getTotalSize());
-		assertEquals("Wrong number of results", 0, termQueryResults.getTotalSize());
 	}
 	
-	@Test
+	// @Test
 	// BUG: REST API bug around transactions fails this test
-	public void testTransactionRollback() throws Exception { 
+	public void TransactionRollback() throws Exception { 
 
 		System.out.println("Inside testTransaction");
 		
@@ -2225,7 +2240,7 @@ public void testTransactionRollback() throws Exception {
 		Transaction transaction = client.openTransaction("Transaction for BiTemporal");
 		
 		try {
-	  	insertJSONSingleDocument(docId, transaction);
+	  	insertJSONSingleDocument(temporalCollectionName, docId, null, transaction, null);
 		}
 		catch (Exception ex) {
 			transaction.rollback();
@@ -2253,7 +2268,7 @@ public void testTransactionRollback() throws Exception {
 		}
 		
 		try {
-		  updateJSONSingleDocument(docId, transaction);
+		  updateJSONSingleDocument(temporalCollectionName, docId, transaction, null);
 		}
 		catch (Exception ex) {
 			transaction.rollback();
@@ -2293,7 +2308,7 @@ public void testTransactionRollback() throws Exception {
 		transaction = client.openTransaction("Transaction Rollback for BiTemporal Delete");
 		
 		try {
-	  	insertJSONSingleDocument(docId, transaction);
+	  	insertJSONSingleDocument(temporalCollectionName, docId, null, transaction, null);
 		}
 		catch (Exception ex) {
 			transaction.rollback();
@@ -2321,7 +2336,7 @@ public void testTransactionRollback() throws Exception {
 		}
 		
 		try {
-		  deleteJSONSingleDocument(docId, transaction);
+		  deleteJSONSingleDocument(temporalCollectionName, docId, transaction);
 		}
 		catch (Exception ex) {
 			transaction.rollback();
@@ -2361,8 +2376,8 @@ public void testPeriodRangeQuerySingleAxisBasedOnALNContains() throws Exception
 		// Read documents based on document URI and ALN Contains. We are just looking for count of documents to be correct
 
 		String docId = "javaSingleJSONDoc.json";
-		insertJSONSingleDocument(docId, null);
-		updateJSONSingleDocument(docId, null);
+		insertJSONSingleDocument(temporalCollectionName, docId, null);
+		updateJSONSingleDocument(temporalCollectionName, docId);
 		
 		// Fetch documents associated with a search term (such as XML) in Address element 
 		QueryManager queryMgr = client.newQueryManager();
@@ -2443,8 +2458,8 @@ public void testPeriodRangeQueryMultiplesAxesBasedOnALNContains() throws Excepti
 		// for count of documents to be correct
 
 		String docId = "javaSingleJSONDoc.json";
-		insertJSONSingleDocument(docId, null);
-		updateJSONSingleDocument(docId, null);
+		insertJSONSingleDocument(temporalCollectionName, docId, null);
+		updateJSONSingleDocument(temporalCollectionName, docId);
 		
 		// Fetch documents associated with a search term (such as XML) in Address element 
 		QueryManager queryMgr = client.newQueryManager();
@@ -2528,10 +2543,10 @@ public void testPeriodCompareQuerySingleAxisBasedOnALNContains() throws Exceptio
 		String docId = "javaSingleJSONDoc.json";
 		
 		Calendar insertTime = DatatypeConverter.parseDateTime("2005-01-01T00:00:01");
-		insertJSONSingleDocument(docId, null, insertTime);
+		insertJSONSingleDocument(temporalCollectionName, docId, null, null, insertTime);
 
 		Calendar updateTime = DatatypeConverter.parseDateTime("2010-01-01T00:00:01");
-		updateJSONSingleDocument(docId, null, updateTime);
+		updateJSONSingleDocument(temporalCollectionName, docId, null, updateTime);
 		
 		// Fetch documents associated with a search term (such as XML) in Address element 
 		QueryManager queryMgr = client.newQueryManager();
@@ -2612,84 +2627,108 @@ public void testPeriodCompareQuerySingleAxisBasedOnALNContains() throws Exceptio
 	public void testLsqtQuery() throws Exception {
 
 		System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug");
-		// Read documents based on document URI and ALN Contains. We are just looking for count of documents to be correct
-		String docId = "javaSingleJSONDoc.json";
-		
-		Calendar insertTime = DatatypeConverter.parseDateTime("2005-01-01T00:00:01");
-		insertJSONSingleDocument(docId, null, insertTime);
-
-		Calendar updateTime = DatatypeConverter.parseDateTime("2010-01-01T00:00:01");
-		updateJSONSingleDocument(docId, null, updateTime);
 		
-		// Fetch documents associated with a search term (such as XML) in Address element 
-		QueryManager queryMgr = client.newQueryManager();
-		StructuredQueryBuilder sqb = queryMgr.newStructuredQueryBuilder();
-
-		Calendar queryTime = DatatypeConverter.parseDateTime("2015-01-01T00:00:01");
-		StructuredQueryDefinition periodQuery = sqb.temporalLsqtQuery(temporalCollectionName, 
-				queryTime, 0, new String[]{});
+		ConnectedRESTQA.updateTemporalCollectionForLSQT(dbName, temporalCollectionName, true);
 		
-		long start = 1;
-		JSONDocumentManager docMgr = client.newJSONDocumentManager();
-		docMgr.setMetadataCategories(Metadata.ALL);	// Get all metadata
-		DocumentPage termQueryResults = docMgr.search(periodQuery, start);
+		try {
+			// Read documents based on document URI and ALN Contains. We are just looking for count of documents to be correct
+			String docId = "javaSingleJSONDoc.json";
+			
+			Calendar insertTime = DatatypeConverter.parseDateTime("2005-01-01T00:00:01");
+			insertJSONSingleDocument(temporalCollectionName, docId, null, null, insertTime);
+	
+			Calendar updateTime = DatatypeConverter.parseDateTime("2010-01-01T00:00:01");
+			updateJSONSingleDocument(temporalCollectionName, docId, null, updateTime);
+			
+			// Fetch documents associated with a search term (such as XML) in Address element 
+			QueryManager queryMgr = client.newQueryManager();
+			StructuredQueryBuilder sqb = queryMgr.newStructuredQueryBuilder();
+	
+			Calendar queryTime = DatatypeConverter.parseDateTime("2007-01-01T00:00:01");
+			StructuredQueryDefinition periodQuery = sqb.temporalLsqtQuery(temporalCollectionName, 
+					queryTime, 0, new String[]{});
+			
+			long start = 1;
+			JSONDocumentManager docMgr = client.newJSONDocumentManager();
+			docMgr.setMetadataCategories(Metadata.ALL);	// Get all metadata
+			DocumentPage termQueryResults = docMgr.search(periodQuery, start);
+			
+			long count = 0;
+	    while (termQueryResults.hasNext()) {
+	    	++count;
+	    	DocumentRecord record = termQueryResults.next();
+	  		System.out.println("URI = " + record.getUri());
+	  		
+	  		DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle();
+	  		record.getMetadata(metadataHandle);
+	  		Iterator resCollections = metadataHandle.getCollections().iterator();
+	  		while (resCollections.hasNext()) {
+	  			System.out.println("Collection = " + resCollections.next()); 
+	  		}
+	
+	  		if (record.getFormat() == Format.XML) {
+	  			DOMHandle recordHandle = new DOMHandle(); 				
+	  			record.getContent (recordHandle);
+		  	  System.out.println("Content = " + recordHandle.toString());   		
+	  		}
+	  		else {
+		  		JacksonDatabindHandle recordHandle =  new JacksonDatabindHandle(ObjectNode.class);  				
+		  		record.getContent (recordHandle);
+		  	  System.out.println("Content = " + recordHandle.toString()); 
+	
+		  	  JsonFactory factory = new JsonFactory(); 
+		      ObjectMapper mapper = new ObjectMapper(factory);
+		      TypeReference> typeRef 
+		              = new TypeReference>() {};
+	
+		      HashMap docObject = mapper.readValue(recordHandle.toString(), typeRef); 
+		      
+		      @SuppressWarnings("unchecked")
+		      HashMap systemNode = (HashMap)(docObject.get(systemNodeName));
+		      
+		      String systemStartDate = (String)systemNode.get(systemStartERIName);
+		      String systemEndDate = (String)systemNode.get(systemEndERIName);
+		      System.out.println("systemStartDate = " + systemStartDate);
+		      System.out.println("systemEndDate = " + systemEndDate);
+	
+		      @SuppressWarnings("unchecked")
+		      HashMap validNode = (HashMap)(docObject.get(validNodeName));
+		      
+		      String validStartDate = (String)validNode.get(validStartERIName);
+		      String validEndDate = (String)validNode.get(validEndERIName);
+		      System.out.println("validStartDate = " + validStartDate);
+		      System.out.println("validEndDate = " + validEndDate);
+	
+		      // assertTrue("Valid start date check failed", 
+		      //		(validStartDate.equals("2008-12-31T23:59:59") && validEndDate.equals("2011-12-31T23:59:59")) ||
+		      //		(validStartDate.equals("2003-01-01T00:00:00") && validEndDate.equals("2008-12-31T23:59:59")) ||
+		      //		(validStartDate.equals("2001-01-01T00:00:00") && validEndDate.equals("2003-01-01T00:00:00")));	     
+	  		}
+	    }
+			
+	    // BUG. I believe Java API is doing a get instead of a POST that returns all documents in doc uri collection
+			System.out.println("Number of results using SQB = " + count);
+			assertEquals("Wrong number of results", 3, count);
+	  }
+		catch (Exception ex) {
+			throw ex;
+		}
 		
-		long count = 0;
-    while (termQueryResults.hasNext()) {
-    	++count;
-    	DocumentRecord record = termQueryResults.next();
-  		System.out.println("URI = " + record.getUri());
-  		
-  		DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle();
-  		record.getMetadata(metadataHandle);
-  		Iterator resCollections = metadataHandle.getCollections().iterator();
-  		while (resCollections.hasNext()) {
-  			System.out.println("Collection = " + resCollections.next()); 
-  		}
-
-  		if (record.getFormat() == Format.XML) {
-  			DOMHandle recordHandle = new DOMHandle(); 				
-  			record.getContent (recordHandle);
-	  	  System.out.println("Content = " + recordHandle.toString());   		
-  		}
-  		else {
-	  		JacksonDatabindHandle recordHandle =  new JacksonDatabindHandle(ObjectNode.class);  				
-	  		record.getContent (recordHandle);
-	  	  System.out.println("Content = " + recordHandle.toString()); 
-
-	  	  JsonFactory factory = new JsonFactory(); 
-	      ObjectMapper mapper = new ObjectMapper(factory);
-	      TypeReference> typeRef 
-	              = new TypeReference>() {};
-
-	      HashMap docObject = mapper.readValue(recordHandle.toString(), typeRef); 
-	      
-	      @SuppressWarnings("unchecked")
-	      HashMap systemNode = (HashMap)(docObject.get(systemNodeName));
-	      
-	      String systemStartDate = (String)systemNode.get(systemStartERIName);
-	      String systemEndDate = (String)systemNode.get(systemEndERIName);
-	      System.out.println("systemStartDate = " + systemStartDate);
-	      System.out.println("systemEndDate = " + systemEndDate);
-
-	      @SuppressWarnings("unchecked")
-	      HashMap validNode = (HashMap)(docObject.get(validNodeName));
-	      
-	      String validStartDate = (String)validNode.get(validStartERIName);
-	      String validEndDate = (String)validNode.get(validEndERIName);
-	      System.out.println("validStartDate = " + validStartDate);
-	      System.out.println("validEndDate = " + validEndDate);
+		finally {
+			ConnectedRESTQA.updateTemporalCollectionForLSQT(dbName, temporalCollectionName, false);
+		}
+	}
 
-	      // assertTrue("Valid start date check failed", 
-	      //		(validStartDate.equals("2008-12-31T23:59:59") && validEndDate.equals("2011-12-31T23:59:59")) ||
-	      //		(validStartDate.equals("2003-01-01T00:00:00") && validEndDate.equals("2008-12-31T23:59:59")) ||
-	      //		(validStartDate.equals("2001-01-01T00:00:00") && validEndDate.equals("2003-01-01T00:00:00")));	     
-  		}
-    }
+	@Test
+	// Negative test
+	public void testJSONTransforms() throws Exception {
+		// Now insert a JSON document
+		System.out.println("In testJSONTransforms .. testing JSON transforms");
+		String jsonDocId = "javaSingleJSONDoc.json";
 		
-    // BUG. I believe Java API is doing a get instead of a POST that returns all documents in doc uri collection
-		System.out.println("Number of results using SQB = " + count);
-		assertEquals("Wrong number of results", 3, count);		
+		insertJSONSingleDocument(temporalCollectionName, jsonDocId, "timestampTransform");
+
+		System.out.println("Out testJSONTransforms .. testing JSON transforms");	
 	}
 	
 	@Test
@@ -2700,7 +2739,7 @@ public void testInsertJSONDocumentUsingXMLExtension() throws Exception {
 		
 		boolean exceptionThrown = false;
 		try {
-		  insertJSONSingleDocument(jsonDocId, null);
+		  insertJSONSingleDocument(temporalCollectionName, jsonDocId, null);
 		}
 		catch (com.marklogic.client.FailedRequestException ex) {
 			exceptionThrown = true;		
@@ -2712,7 +2751,7 @@ public void testInsertJSONDocumentUsingXMLExtension() throws Exception {
 			// assert(ex.getFailedRequest().getStatusCode() == 400);
 		}
 		
-		assertTrue("Exception not thrown for invalid transform", exceptionThrown);	
+		assertTrue("Exception not thrown for invalid extension", exceptionThrown);	
 	}
 
 	@Test
@@ -2735,7 +2774,7 @@ public void testInsertJSONDocumentUsingNonExistingTemporalCollection() throws Ex
 		DocumentMetadataHandle mh = setMetadata(false);				
 
 	  try {
-	    docMgr.write(jsonDocId, mh, handle, null, null, "invalidCollection", null);		
+	    docMgr.write(jsonDocId, mh, handle, null, null, "invalidCollection");		
 		}
 		catch (com.marklogic.client.FailedRequestException ex) {
 			exceptionThrown = true;		
@@ -2770,7 +2809,7 @@ public void testDocumentUsingCollectionNamedLatest() throws Exception {
 		DocumentMetadataHandle mh = setMetadata(false);				
 
 	  try {
-	    docMgr.write(jsonDocId, mh, handle, null, null, latestCollectionName, null);		
+	    docMgr.write(jsonDocId, mh, handle, null, null, latestCollectionName);		
 		}
 		catch (com.marklogic.client.FailedRequestException ex) {
 			exceptionThrown = true;		
@@ -2797,7 +2836,7 @@ public void testInsertDocumentUsingDocumentURIAsCollectionName() throws Exceptio
 		ConnectedRESTQA.addElementRangeIndexTemporalCollection(dbName, jsonDocId, axisSystemName, axisValidName);
 		
 		// Insert a document called as insertJSONSingleDocument
-		insertJSONSingleDocument(jsonDocId, null);
+		insertJSONSingleDocument(temporalCollectionName, jsonDocId, null);
 		
 		JacksonDatabindHandle handle = getJSONDocumentHandle(
 				"2001-01-01T00:00:00", "2011-12-31T23:59:59", "999 Skyway Park - JSON", jsonDocId);
@@ -2810,7 +2849,7 @@ public void testInsertDocumentUsingDocumentURIAsCollectionName() throws Exceptio
 
 		boolean exceptionThrown = false;
 	  try {
-	    docMgr.write(jsonDocId, mh, handle, null, null, jsonDocId, null);		
+	    docMgr.write(jsonDocId, mh, handle, null, null, jsonDocId);		
 		}
 		catch (com.marklogic.client.FailedRequestException ex) {
 			exceptionThrown = true;		
@@ -2836,7 +2875,7 @@ public void testCreateCollectionUsingSameNameAsDocURI() throws Exception {
 		System.out.println("Inside testInserDocumentUsingDocumentURIAsCollectionName");
 		
 		// Insert a document called as insertJSONSingleDocument
-		insertJSONSingleDocument(jsonDocId, null);
+		insertJSONSingleDocument(temporalCollectionName, jsonDocId, null);
 		
 		boolean exceptionThrown = false;
 	  try {
@@ -2866,7 +2905,7 @@ public void testCreateCollectionUsingSameNameAsDocURI() throws Exception {
 
 		exceptionThrown = false;
 	  try {
-	    docMgr.write("1.json", mh, handle, null, null, jsonDocId, null);		
+	    docMgr.write("1.json", mh, handle, null, null, jsonDocId);		
 		}
 		catch (com.marklogic.client.FailedRequestException ex) {
 			exceptionThrown = true;		

From 4c0cff98badba1806b7463b9665b940370098a45 Mon Sep 17 00:00:00 2001
From: sammefford 
Date: Thu, 6 Nov 2014 13:19:21 -0700
Subject: [PATCH 289/357] fix #80--intermingled writers were not flushing and
 therefore behaving inconsitently on different streams in eclipse vs maven

---
 .../java/com/marklogic/client/alerting/RuleDefinition.java     | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/main/java/com/marklogic/client/alerting/RuleDefinition.java b/src/main/java/com/marklogic/client/alerting/RuleDefinition.java
index 03d67da66..01019dfeb 100644
--- a/src/main/java/com/marklogic/client/alerting/RuleDefinition.java
+++ b/src/main/java/com/marklogic/client/alerting/RuleDefinition.java
@@ -262,12 +262,15 @@ public void write(OutputStream out) throws IOException {
 					"description", RequestConstants.RESTAPI_NS);
 			serializer.writeCharacters(getDescription());
 			serializer.writeEndElement();
+			serializer.flush();
 
 			// logger.debug("Send: " + new String(queryPayload));
 			XMLEventWriter eventWriter = factory.createXMLEventWriter(out);
 			for (XMLEvent event : this.queryPayload) {
 				eventWriter.add(event);
 			}
+			eventWriter.flush();
+			out.flush();
 
 			writeMetadataElement(serializer);
 

From 36c3338e6914bf1c2a863cd29623488bb2f3f099 Mon Sep 17 00:00:00 2001
From: skottam 
Date: Thu, 6 Nov 2014 13:56:24 -0800
Subject: [PATCH 290/357] adding test for client connection with run time db

---
 .../marklogic/javaclient/ConnectedRESTQA.java | 23 ++++++
 .../javaclient/TestRuntimeDBselection.java    | 76 +++++++++++++++++++
 2 files changed, 99 insertions(+)
 create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/TestRuntimeDBselection.java

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java b/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java
index fd699ca4c..7972f2bd7 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java
@@ -245,6 +245,29 @@ public static void associateRESTServerWithDB(String restServerName,String dbName
 			System.out.println(content);
 		}
 	}
+	/*
+	 * Associate REST server with default user 
+	 * this is created for the sake of runtime DB selection
+	 */
+	public static void associateRESTServerWithDefaultUser(String restServerName,String userName,String authType)throws Exception{
+		DefaultHttpClient client = new DefaultHttpClient();
+
+		client.getCredentialsProvider().setCredentials(
+				new AuthScope("localhost", 8002),
+				new UsernamePasswordCredentials("admin", "admin"));
+		String  body = "{ \"default-user\":\""+userName+"\",\"authentication\": \""+authType+"\",\"group-name\": \"Default\"}";
+
+		HttpPut put = new HttpPut("http://localhost:8002/manage/v2/servers/"+restServerName+"/properties?server-type=http");
+		put.addHeader("Content-type", "application/json");
+		put.setEntity(new StringEntity(body));
+
+		HttpResponse response2 = client.execute(put);
+		HttpEntity respEntity = response2.getEntity();
+		if(respEntity != null){
+			String content =  EntityUtils.toString(respEntity);
+			System.out.println(content);
+		}
+	}
 	/*
 	 * Creating RESTServer With default content and module database
 	 */
diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestRuntimeDBselection.java b/test-complete/src/test/java/com/marklogic/javaclient/TestRuntimeDBselection.java
new file mode 100644
index 000000000..1bbc335eb
--- /dev/null
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestRuntimeDBselection.java
@@ -0,0 +1,76 @@
+package com.marklogic.javaclient;
+
+import static org.junit.Assert.*;
+
+import javax.annotation.Resource.AuthenticationType;
+
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import com.marklogic.client.DatabaseClient;
+import com.marklogic.client.DatabaseClientFactory;
+import com.marklogic.client.DatabaseClientFactory.Authentication;
+import com.marklogic.client.FailedRequestException;
+
+public class TestRuntimeDBselection extends BasicJavaClientREST {
+	private static String dbName = "TestRuntimeDB";
+	private static String [] fNames = {"TestRuntimeDB-1"};
+	private static String restServerName = "REST-Java-Client-API-Server";
+	private static int restPort = 8011;
+	private  DatabaseClient client ;
+	@BeforeClass
+	public static void setUpBeforeClass() throws Exception {
+		System.out.println("In setup");
+		 setupJavaRESTServer(dbName, fNames[0], restServerName,restPort,false);
+	     createUserRolesWithPrevilages("test-eval","xdbc:eval", "xdbc:eval-in","xdmp:eval-in","any-uri","xdbc:invoke");
+	     createRESTUser("eval-user", "x", "test-eval","rest-admin","rest-writer","rest-reader");
+	    
+		 System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug");
+	}
+
+	@AfterClass
+	public static void tearDownAfterClass() throws Exception {
+		System.out.println("In tear down" );
+		tearDownJavaRESTServer(dbName, fNames, restServerName);
+		deleteRESTUser("eval-user");
+		deleteUserRole("test-eval");
+	}
+
+	@Test
+	public void testRuntimeDBclientWithDefaultUser() throws Exception {
+		associateRESTServerWithDefaultUser(restServerName,"eval-user","application-level");
+		client = DatabaseClientFactory.newClient("localhost", restPort,dbName);
+		String insertJSON = "xdmp:document-insert(\"test2.json\",object-node {\"test\":\"hello\"})";
+		client.newServerEval().xquery(insertJSON).eval();
+		String query1 = "fn:count(fn:doc())";
+		int response = client.newServerEval().xquery(query1).eval().next().getNumber().intValue();
+		assertEquals("count of documents ",1,response);
+		String query2 ="declareUpdate();xdmp.documentDelete(\"test2.json\");";
+		client.newServerEval().javascript(query2).eval();
+		int response2 = client.newServerEval().xquery(query1).eval().next().getNumber().intValue();
+		assertEquals("count of documents ",0,response2);
+		client.release();
+		associateRESTServerWithDefaultUser(restServerName,"nobody","digest");
+	}
+	//Issue 184 exists 
+	@Test(expected=FailedRequestException.class)
+	public void testRuntimeDBclientWithDifferentAuthType() throws Exception {
+		
+		client = DatabaseClientFactory.newClient("localhost", restPort,dbName,"eval-user","x",Authentication.BASIC);
+		String insertJSON = "xdmp:document-insert(\"test2.json\",object-node {\"test\":\"hello\"})";
+		client.newServerEval().xquery(insertJSON).eval();
+		String query1 = "fn:count(fn:doc())";
+		int response = client.newServerEval().xquery(query1).eval().next().getNumber().intValue();
+		assertEquals("count of documents ",1,response);
+		String query2 ="declareUpdate();xdmp.documentDelete(\"test2.json\");";
+		client.newServerEval().javascript(query2).eval();
+		int response2 = client.newServerEval().xquery(query1).eval().next().getNumber().intValue();
+		assertEquals("count of documents ",0,response2);
+		client.release();
+		
+	}
+
+}

From 836285e7aa80a65de73089e6e69f742a72de2039 Mon Sep 17 00:00:00 2001
From: sammefford 
Date: Thu, 6 Nov 2014 15:00:05 -0700
Subject: [PATCH 291/357] whitespace only: get rid of unwanted tabs

---
 .../marklogic/client/pojo/PojoRepository.java | 58 +++++++++----------
 1 file changed, 29 insertions(+), 29 deletions(-)

diff --git a/src/main/java/com/marklogic/client/pojo/PojoRepository.java b/src/main/java/com/marklogic/client/pojo/PojoRepository.java
index 5cdc70388..3233f7bb6 100644
--- a/src/main/java/com/marklogic/client/pojo/PojoRepository.java
+++ b/src/main/java/com/marklogic/client/pojo/PojoRepository.java
@@ -82,18 +82,18 @@ public interface PojoRepository {
      *  serialized JSON format.
      */
     public void write(T entity)
-    	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
+        throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
     /** Does everything in {@link #write(Object) write(T)} but also adds your collections to the 
      * persisted instance.
      */
     public void write(T entity, String... collections)
-    	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
+        throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
     /** Does everything in {@link #write(Object) write(T)} but in your 
      * 
      * multi-statement transaction context.
      */
     public void write(T entity, Transaction transaction)
-    	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
+        throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
     /** Does everything in {@link #write(Object) write(T)} but also adds your collections to the 
      * persisted instance and performs the write in your
      * 
@@ -101,93 +101,93 @@ public void write(T entity, Transaction transaction)
      * .
      */
     public void write(T entity, Transaction transaction, String... collections)
-    	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
+        throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
 
     /** @return true if a document exists in the database with the id */
     public boolean exists(ID id)
-    	throws ForbiddenUserException, FailedRequestException;
+        throws ForbiddenUserException, FailedRequestException;
 
     /** @return in the context of transaction, true if a document exists in the database with 
      * the id */
     public boolean exists(ID id, Transaction transaction)
-    	throws ForbiddenUserException, FailedRequestException;
+        throws ForbiddenUserException, FailedRequestException;
 
     /** @return the number of documents of type T persisted in the database */
     public long count()
-    	throws ForbiddenUserException, FailedRequestException;
+        throws ForbiddenUserException, FailedRequestException;
 
     /** @return in the context of transaction, the number of documents of type T persisted in 
      * the database */
     public long count(Transaction transaction)
-    	throws ForbiddenUserException, FailedRequestException;
+        throws ForbiddenUserException, FailedRequestException;
 
     /** @return the number of documents of type T persisted in the database with at least 
      * one of the criteria collections*/
     public long count(String... collection)
-    	throws ForbiddenUserException, FailedRequestException;
+        throws ForbiddenUserException, FailedRequestException;
 
     /** @return in the context of transaction, the number of documents of type T persisted in 
      * the database with at least one of the criteria collections*/
     public long count(String[] collections, Transaction transaction)
-    	throws ForbiddenUserException, FailedRequestException;
+        throws ForbiddenUserException, FailedRequestException;
 
     /** @return the number of documents of type T persisted in the database which match
      * the query */
     public long count(PojoQueryDefinition query)
-    	throws ForbiddenUserException, FailedRequestException;
+        throws ForbiddenUserException, FailedRequestException;
   
     /** @return in the context of transaction, the number of documents of type T persisted in the 
      * database which match the query */
     public long count(PojoQueryDefinition query, Transaction transaction)
-    	throws ForbiddenUserException, FailedRequestException;
+        throws ForbiddenUserException, FailedRequestException;
   
     /** Deletes from the database the documents with the corresponding ids */
     public void delete(ID... ids)
-    	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
+        throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
 
     /** As part of transaction, deletes from the database the documents with the corresponding ids */
     public void delete(ID[] ids, Transaction transaction)
-    	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
+        throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
 
     /** Deletes from the database all documents of type T persisted by the pojo facade */
     public void deleteAll()
-    	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
+        throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
 
     /** As part of transaction, deletes from the database all documents of type T persisted by 
      * the pojo facade */
     public void deleteAll(Transaction transaction)
-    	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
+        throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
 
     /* REST API does not currently support DELETE /search with multiple collection arguments
     public void deleteAll(String... collections)
-    	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
+        throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
     */
   
     public T read(ID id)
-    	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
+        throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
     public T read(ID id, Transaction transaction)
-    	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
+        throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
     public PojoPage read(ID[] ids)
-    	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
+        throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
     public PojoPage read(ID[] ids, Transaction transaction)
-    	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
+        throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
     public PojoPage readAll(long start)
-    	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
+        throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
     public PojoPage readAll(long start, Transaction transaction)
-    	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
+        throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
  
     public PojoPage search(long start, String... collections)
-    	throws ForbiddenUserException, FailedRequestException;
+        throws ForbiddenUserException, FailedRequestException;
     public PojoPage search(long start, Transaction transaction, String... collections)
-    	throws ForbiddenUserException, FailedRequestException;
+        throws ForbiddenUserException, FailedRequestException;
     public PojoPage search(PojoQueryDefinition query, long start)
-    	throws ForbiddenUserException, FailedRequestException;
+        throws ForbiddenUserException, FailedRequestException;
     public PojoPage search(PojoQueryDefinition query, long start, Transaction transaction)
-    	throws ForbiddenUserException, FailedRequestException;
+        throws ForbiddenUserException, FailedRequestException;
     public PojoPage search(PojoQueryDefinition query, long start, SearchReadHandle searchHandle)
-    	throws ForbiddenUserException, FailedRequestException;
+        throws ForbiddenUserException, FailedRequestException;
     public PojoPage search(PojoQueryDefinition query, long start, SearchReadHandle searchHandle, Transaction transaction)
-    	throws ForbiddenUserException, FailedRequestException;
+        throws ForbiddenUserException, FailedRequestException;
  
     public PojoQueryBuilder getQueryBuilder();
 

From 96827ace278ba7f55602a31bd341489f950ee0cc Mon Sep 17 00:00:00 2001
From: sammefford 
Date: Thu, 6 Nov 2014 15:09:35 -0700
Subject: [PATCH 292/357] clarify that the eval methods throw certain runtime
 exceptions

---
 .../client/eval/ServerEvaluationCall.java          | 11 ++++++++---
 .../client/impl/ServerEvaluationCallImpl.java      | 14 +++++++++++---
 2 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/src/main/java/com/marklogic/client/eval/ServerEvaluationCall.java b/src/main/java/com/marklogic/client/eval/ServerEvaluationCall.java
index b1e619ae7..2747361bf 100644
--- a/src/main/java/com/marklogic/client/eval/ServerEvaluationCall.java
+++ b/src/main/java/com/marklogic/client/eval/ServerEvaluationCall.java
@@ -15,6 +15,8 @@
  */
 package com.marklogic.client.eval;
 
+import com.marklogic.client.FailedRequestException;
+import com.marklogic.client.ForbiddenUserException;
 import com.marklogic.client.Transaction;
 import com.marklogic.client.io.marker.AbstractReadHandle;
 import com.marklogic.client.io.marker.AbstractWriteHandle;
@@ -37,7 +39,10 @@ public interface ServerEvaluationCall {
     public ServerEvaluationCall transaction(Transaction transaction);
     public ServerEvaluationCall addNamespace(String prefix, String namespaceURI);
     public ServerEvaluationCall namespaceContext(EditableNamespaceContext namespaces);
-    public  T evalAs(Class responseType);
-    public  H eval(H responseHandle);
-    public EvalResultIterator eval();
+    public  T evalAs(Class responseType)
+        throws ForbiddenUserException, FailedRequestException;
+    public  H eval(H responseHandle)
+        throws ForbiddenUserException, FailedRequestException;
+    public EvalResultIterator eval()
+        throws ForbiddenUserException, FailedRequestException;
 }
diff --git a/src/main/java/com/marklogic/client/impl/ServerEvaluationCallImpl.java b/src/main/java/com/marklogic/client/impl/ServerEvaluationCallImpl.java
index 66f939a62..3509c408f 100644
--- a/src/main/java/com/marklogic/client/impl/ServerEvaluationCallImpl.java
+++ b/src/main/java/com/marklogic/client/impl/ServerEvaluationCallImpl.java
@@ -18,6 +18,8 @@
 import java.util.HashMap;
 
 import com.marklogic.client.DatabaseClientFactory.HandleFactoryRegistry;
+import com.marklogic.client.FailedRequestException;
+import com.marklogic.client.ForbiddenUserException;
 import com.marklogic.client.Transaction;
 import com.marklogic.client.eval.EvalResultIterator;
 import com.marklogic.client.eval.ServerEvaluationCall;
@@ -131,7 +133,9 @@ public ServerEvaluationCall transaction(Transaction transaction) {
     }
 
     @Override
-    public  T evalAs(Class responseType) {
+    public  T evalAs(Class responseType) 
+        throws ForbiddenUserException, FailedRequestException
+    {
         if (responseType == null) throw new IllegalArgumentException("responseType cannot be null");
 
         ContentHandle readHandle = handleRegistry.makeHandle(responseType);
@@ -142,7 +146,9 @@ public  T evalAs(Class responseType) {
     }
 
     @Override
-    public  H eval(H responseHandle) {
+    public  H eval(H responseHandle) 
+        throws ForbiddenUserException, FailedRequestException
+    {
         EvalResultIterator iterator = eval();
         try {
             if ( iterator == null || iterator.hasNext() == false ) return null;
@@ -151,7 +157,9 @@ public  H eval(H responseHandle) {
     }
 
     @Override
-    public EvalResultIterator eval() {
+    public EvalResultIterator eval() 
+        throws ForbiddenUserException, FailedRequestException
+    {
         return services.postEvalInvoke(requestLogger, code, modulePath, evalContext,
             vars, namespaceContext, transactionId);
     }

From 8d5ac68a30a67c05967d7db3b90631f6d2dec99a Mon Sep 17 00:00:00 2001
From: sammefford 
Date: Thu, 6 Nov 2014 15:30:30 -0700
Subject: [PATCH 293/357] fix #151--support OTHER (not NULL) EvalResult.Type
 for responses with no X-Primitive header

---
 src/main/java/com/marklogic/client/impl/JerseyServices.java | 4 +++-
 src/test/java/com/marklogic/client/test/EvalTest.java       | 5 ++++-
 src/test/resources/evaltest.xqy                             | 3 ++-
 3 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/main/java/com/marklogic/client/impl/JerseyServices.java b/src/main/java/com/marklogic/client/impl/JerseyServices.java
index 895eba642..215e9c021 100644
--- a/src/main/java/com/marklogic/client/impl/JerseyServices.java
+++ b/src/main/java/com/marklogic/client/impl/JerseyServices.java
@@ -3642,7 +3642,7 @@ public EvalResult.Type getType() {
 			}
 			String xPrimitive = content.getHeader("X-Primitive");
 			if ( xPrimitive == null ) {
-				return EvalResult.Type.NULL;
+				return EvalResult.Type.OTHER;
 			} else if ( "string".equals(xPrimitive) || "untypedAtomic".equals(xPrimitive) ) {
 				return EvalResult.Type.STRING;
 			} else if ( "boolean".equals(xPrimitive) ) {
@@ -3691,6 +3691,8 @@ public EvalResult.Type getType() {
 				return EvalResult.Type.QNAME;
 			} else if ( "time".equals(xPrimitive) ) {
 				return EvalResult.Type.TIME;
+			} else if ( "null".equals(xPrimitive) ) {
+				return EvalResult.Type.NULL;
 			}
 			return EvalResult.Type.OTHER;
 		}
diff --git a/src/test/java/com/marklogic/client/test/EvalTest.java b/src/test/java/com/marklogic/client/test/EvalTest.java
index 1d0013654..257f10df2 100644
--- a/src/test/java/com/marklogic/client/test/EvalTest.java
+++ b/src/test/java/com/marklogic/client/test/EvalTest.java
@@ -57,7 +57,7 @@ public class EvalTest {
     @BeforeClass
     public static void beforeClass() {
         Common.connectEval();
-        System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug");
+        //System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug");
     }
     @AfterClass
     public static void afterClass() {
@@ -326,6 +326,9 @@ private void runAndTest(ServerEvaluationCall call)
             result = results.next();
             assertEquals("myCtsQuery should be Type.OTHER", EvalResult.Type.OTHER, result.getType());
             assertEquals("myCtsQuery should be Format.TEXT", Format.TEXT, result.getFormat());
+            result = results.next();
+            assertEquals("myFunction should be Type.OTHER", EvalResult.Type.OTHER, result.getType());
+            assertEquals("myFunction should be Format.TEXT", Format.TEXT, result.getFormat());
         } finally { results.close(); }
     }
 
diff --git a/src/test/resources/evaltest.xqy b/src/test/resources/evaltest.xqy
index c01913261..2470016b3 100644
--- a/src/test/resources/evaltest.xqy
+++ b/src/test/resources/evaltest.xqy
@@ -33,11 +33,12 @@ let $myAttribute             := $myElement/@*
 let $myComment               := $myComment/comment() 
 let $myProcessingInstruction := $myProcessingInstruction/processing-instruction() 
 let $myCtsQuery := cts:word-query('a') 
+let $myFunction := xdmp:functions()[2]
 return (
     $test:myString, $myArray, $myObject, $myAnyUri, 
     $myBinary, $myBase64Binary, $myHexBinary, $myDuration, $myQName,
     $myDocument, $myAttribute, $myComment, $myElement, $myProcessingInstruction, $myText, 
     $myBool, $myInteger, $myBigInteger, $myDecimal, $myDouble, $myFloat,
     $myGDay, $myGMonth, $myGMonthDay, $myGYear, $myGYearMonth, $myDate, $myDateTime, $myTime,
-    $myNull, $myCtsQuery
+    $myNull, $myCtsQuery, $myFunction
 )

From 670cad9dc2ed16b93e04e2bd62bfefa877045ff6 Mon Sep 17 00:00:00 2001
From: skottam 
Date: Thu, 6 Nov 2014 15:00:14 -0800
Subject: [PATCH 294/357] adding files

---
 ...query-modules-with-diff-variable-types.xqy | 32 +++++++++++++++++++
 1 file changed, 32 insertions(+)
 create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/data/xquery-modules-with-diff-variable-types.xqy

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/data/xquery-modules-with-diff-variable-types.xqy b/test-complete/src/test/java/com/marklogic/javaclient/data/xquery-modules-with-diff-variable-types.xqy
new file mode 100644
index 000000000..120e5d6eb
--- /dev/null
+++ b/test-complete/src/test/java/com/marklogic/javaclient/data/xquery-modules-with-diff-variable-types.xqy
@@ -0,0 +1,32 @@
+declare namespace test='http://marklogic.com/test';
+declare variable $test:myString as xs:string external;
+declare variable $myXmlNode as xs:string external;
+(:declare variable $myJsonNode as xs:string external;:)
+declare variable $myBool as xs:boolean external;
+declare variable $myInteger as xs:integer external;
+declare variable $myDecimal as xs:decimal external;
+declare variable $myDouble as xs:double external;
+declare variable $myFloat as xs:float external;
+declare variable $myNull  external;
+
+(
+    $test:myString,
+      $myBool,
+     document{ xdmp:unquote($myXmlNode) },
+     xdmp:unquote($myXmlNode)//comment(),
+     xdmp:unquote($myXmlNode)//text(),
+     xdmp:unquote($myXmlNode)//*,
+     xdmp:unquote($myXmlNode)/@attr,    
+    xdmp:unquote($myXmlNode)//processing-instruction(),
+     (:$myNull,
+     xdmp:unquote($myJsonNode)/a,
+     xdmp:unquote($myJsonNode)/b,
+     xdmp:unquote($myJsonNode)/c1,
+     xdmp:unquote($myJsonNode)/d,
+     xdmp:unquote($myJsonNode)/f,
+     xdmp:unquote($myJsonNode)/g, :)
+     $myInteger,  
+     $myDecimal, 
+     $myDouble, 
+     $myFloat
+    )
\ No newline at end of file

From 96c9c57199e04421bcbfee3941f1a15a0e6b19ea Mon Sep 17 00:00:00 2001
From: skottam 
Date: Thu, 6 Nov 2014 16:36:44 -0800
Subject: [PATCH 295/357] adding test for client connection with run time db

---
 .../com/marklogic/javaclient/TestRuntimeDBselection.java    | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestRuntimeDBselection.java b/test-complete/src/test/java/com/marklogic/javaclient/TestRuntimeDBselection.java
index 1bbc335eb..d4e2baf43 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/TestRuntimeDBselection.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestRuntimeDBselection.java
@@ -56,9 +56,9 @@ public void testRuntimeDBclientWithDefaultUser() throws Exception {
 		associateRESTServerWithDefaultUser(restServerName,"nobody","digest");
 	}
 	//Issue 184 exists 
-	@Test(expected=FailedRequestException.class)
+	@Test
 	public void testRuntimeDBclientWithDifferentAuthType() throws Exception {
-		
+		associateRESTServerWithDefaultUser(restServerName,"nobody","basic");
 		client = DatabaseClientFactory.newClient("localhost", restPort,dbName,"eval-user","x",Authentication.BASIC);
 		String insertJSON = "xdmp:document-insert(\"test2.json\",object-node {\"test\":\"hello\"})";
 		client.newServerEval().xquery(insertJSON).eval();
@@ -70,7 +70,7 @@ public void testRuntimeDBclientWithDifferentAuthType() throws Exception {
 		int response2 = client.newServerEval().xquery(query1).eval().next().getNumber().intValue();
 		assertEquals("count of documents ",0,response2);
 		client.release();
-		
+		associateRESTServerWithDefaultUser(restServerName,"nobody","digest");
 	}
 
 }

From 58d39ec17d5162738f1a5938fcacc990d511f9e5 Mon Sep 17 00:00:00 2001
From: gvaidees 
Date: Thu, 6 Nov 2014 17:51:25 -0800
Subject: [PATCH 296/357] Minor fixes to ensure correctness based on latest
 build. (testLSQTQuery will still fail)

---
 .../marklogic/javaclient/ConnectedRESTQA.java |   7 +-
 .../marklogic/javaclient/TestBiTemporal.java  | 362 ++++++------------
 2 files changed, 121 insertions(+), 248 deletions(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java b/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java
index fd699ca4c..ec6b01b78 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java
@@ -1593,10 +1593,15 @@ public static void updateTemporalCollectionForLSQT(String dbName, String collect
 		ObjectMapper mapper = new ObjectMapper();
 		ObjectNode rootNode = mapper.createObjectNode();
 		rootNode.put( "lsqt-enabled", enable);
+		
+		// Set system time values
+		ObjectNode automation = mapper.createObjectNode();
+		automation.put("enabled", true);
+		
+		rootNode.set("automation", automation);
 
 		System.out.println(rootNode.toString());
 
-
 		DefaultHttpClient client = new DefaultHttpClient();
 		client.getCredentialsProvider().setCredentials(
 				new AuthScope("localhost", 8002),
diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBiTemporal.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBiTemporal.java
index 809ae1a6b..8c8c57cfe 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/TestBiTemporal.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBiTemporal.java
@@ -990,6 +990,7 @@ public void testJSONConsolidated() throws Exception {
 	public void testSystemTime() throws Exception { 
 
 		System.out.println("Inside testSystemTime");
+		ConnectedRESTQA.updateTemporalCollectionForLSQT(dbName, temporalLsqtCollectionName, true);
 		
 		String docId = "javaSingleJSONDoc.json";
 		
@@ -2066,6 +2067,7 @@ record = termQueryResults.next();
 	public void testSystemTimeUsingInvalidTime() throws Exception { 
 
 		System.out.println("Inside testSystemTimeUsingInvalidTime");
+		ConnectedRESTQA.updateTemporalCollectionForLSQT(dbName, temporalLsqtCollectionName, true);
 		
 		String docId = "javaSingleJSONDoc.json";
 		
@@ -2105,11 +2107,13 @@ public void testSystemTimeUsingInvalidTime() throws Exception {
 			
 			exceptionThrown = true;
 		}		
+		
+		assertTrue("Exception not thrown for invalid extension", exceptionThrown);
 	}
 	
-	// @Test
+	@Test
 	// BUG: REST API bug around transactions fails this test
-	public void TransactionCommit() throws Exception { 
+	public void testTransactionCommit() throws Exception { 
 
 		System.out.println("Inside testTransactionCommit");
 		
@@ -2117,6 +2121,8 @@ public void TransactionCommit() throws Exception {
 		
 		Transaction transaction = client.openTransaction("Transaction for BiTemporal");
 		try {					
+		  insertJSONSingleDocument(temporalCollectionName, docId, null, transaction, null);
+		  
 	    // Verify that the document was inserted 
 			JSONDocumentManager docMgr = client.newJSONDocumentManager();
 			DocumentPage readResults = docMgr.read(transaction, docId); 
@@ -2138,13 +2144,14 @@ public void TransactionCommit() throws Exception {
 			}
 			
 			// Make sure document is not visible to any other transaction
-			readResults = docMgr.read(docId); 
-			System.out.println("Number of results = " + readResults.size());
-			if (readResults.size() != 0) {
-				transaction.rollback();
-				
-				assertEquals("Wrong number of results", 1, readResults.size());
+			boolean exceptionThrown = false;
+			try {
+			  readResults = docMgr.read(docId); 
+			}
+			catch (Exception ex) {
+				exceptionThrown = true;
 			}
+			assertTrue("Exception not thrown during read using no transaction handle", exceptionThrown);	
 			
 		  updateJSONSingleDocument(temporalCollectionName, docId, transaction, null);		
 	
@@ -2229,9 +2236,9 @@ public void TransactionCommit() throws Exception {
 		}
 	}
 	
-	// @Test
+	@Test
 	// BUG: REST API bug around transactions fails this test
-	public void TransactionRollback() throws Exception { 
+	public void testTransactionRollback() throws Exception { 
 
 		System.out.println("Inside testTransaction");
 		
@@ -2295,10 +2302,15 @@ public void TransactionRollback() throws Exception {
 		transaction.rollback();
 
     // Verify that the document is not there after rollback
-		readResults = docMgr.read(docId); 
+		boolean exceptionThrown = false;
+		try {
+		  readResults = docMgr.read(docId); 
+		}
+		catch (Exception ex) {
+			exceptionThrown = true;
+		}
 
-		System.out.println("Number of results = " + readResults.size());
-		assertEquals("Wrong number of results", 0, readResults.size());
+		assertTrue("Exception not thrown during read on non-existing uri", exceptionThrown);	
 		
 		//=======================================================================
 		// Now try rollback with delete
@@ -2363,10 +2375,13 @@ public void TransactionRollback() throws Exception {
 		transaction.rollback();
 		
 	  // Verify that the document was rolled back and count is 0
-		readResults = docMgr.read(docId); 
-
-		System.out.println("Number of results = " + readResults.size());
-		assertEquals("Wrong number of results", 0, readResults.size());
+		exceptionThrown = false;
+		try {
+		  readResults = docMgr.read(docId); 
+		}
+		catch (Exception ex) {
+			exceptionThrown = true;
+		}
 		
 		System.out.println("Done");
 	}
@@ -2541,12 +2556,13 @@ public void testPeriodCompareQuerySingleAxisBasedOnALNContains() throws Exceptio
 		// Read documents based on document URI and ALN Contains. We are just looking for count of documents to be correct
 
 		String docId = "javaSingleJSONDoc.json";
+		ConnectedRESTQA.updateTemporalCollectionForLSQT(dbName, temporalLsqtCollectionName, true);
 		
 		Calendar insertTime = DatatypeConverter.parseDateTime("2005-01-01T00:00:01");
-		insertJSONSingleDocument(temporalCollectionName, docId, null, null, insertTime);
+		insertJSONSingleDocument(temporalLsqtCollectionName, docId, null, null, insertTime);
 
 		Calendar updateTime = DatatypeConverter.parseDateTime("2010-01-01T00:00:01");
-		updateJSONSingleDocument(temporalCollectionName, docId, null, updateTime);
+		updateJSONSingleDocument(temporalLsqtCollectionName, docId, null, updateTime);
 		
 		// Fetch documents associated with a search term (such as XML) in Address element 
 		QueryManager queryMgr = client.newQueryManager();
@@ -2628,95 +2644,88 @@ public void testLsqtQuery() throws Exception {
 
 		System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug");
 		
-		ConnectedRESTQA.updateTemporalCollectionForLSQT(dbName, temporalCollectionName, true);
+		ConnectedRESTQA.updateTemporalCollectionForLSQT(dbName, temporalLsqtCollectionName, true);
 		
-		try {
-			// Read documents based on document URI and ALN Contains. We are just looking for count of documents to be correct
-			String docId = "javaSingleJSONDoc.json";
-			
-			Calendar insertTime = DatatypeConverter.parseDateTime("2005-01-01T00:00:01");
-			insertJSONSingleDocument(temporalCollectionName, docId, null, null, insertTime);
-	
-			Calendar updateTime = DatatypeConverter.parseDateTime("2010-01-01T00:00:01");
-			updateJSONSingleDocument(temporalCollectionName, docId, null, updateTime);
-			
-			// Fetch documents associated with a search term (such as XML) in Address element 
-			QueryManager queryMgr = client.newQueryManager();
-			StructuredQueryBuilder sqb = queryMgr.newStructuredQueryBuilder();
-	
-			Calendar queryTime = DatatypeConverter.parseDateTime("2007-01-01T00:00:01");
-			StructuredQueryDefinition periodQuery = sqb.temporalLsqtQuery(temporalCollectionName, 
-					queryTime, 0, new String[]{});
-			
-			long start = 1;
-			JSONDocumentManager docMgr = client.newJSONDocumentManager();
-			docMgr.setMetadataCategories(Metadata.ALL);	// Get all metadata
-			DocumentPage termQueryResults = docMgr.search(periodQuery, start);
-			
-			long count = 0;
-	    while (termQueryResults.hasNext()) {
-	    	++count;
-	    	DocumentRecord record = termQueryResults.next();
-	  		System.out.println("URI = " + record.getUri());
-	  		
-	  		DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle();
-	  		record.getMetadata(metadataHandle);
-	  		Iterator resCollections = metadataHandle.getCollections().iterator();
-	  		while (resCollections.hasNext()) {
-	  			System.out.println("Collection = " + resCollections.next()); 
-	  		}
-	
-	  		if (record.getFormat() == Format.XML) {
-	  			DOMHandle recordHandle = new DOMHandle(); 				
-	  			record.getContent (recordHandle);
-		  	  System.out.println("Content = " + recordHandle.toString());   		
-	  		}
-	  		else {
-		  		JacksonDatabindHandle recordHandle =  new JacksonDatabindHandle(ObjectNode.class);  				
-		  		record.getContent (recordHandle);
-		  	  System.out.println("Content = " + recordHandle.toString()); 
-	
-		  	  JsonFactory factory = new JsonFactory(); 
-		      ObjectMapper mapper = new ObjectMapper(factory);
-		      TypeReference> typeRef 
-		              = new TypeReference>() {};
-	
-		      HashMap docObject = mapper.readValue(recordHandle.toString(), typeRef); 
-		      
-		      @SuppressWarnings("unchecked")
-		      HashMap systemNode = (HashMap)(docObject.get(systemNodeName));
-		      
-		      String systemStartDate = (String)systemNode.get(systemStartERIName);
-		      String systemEndDate = (String)systemNode.get(systemEndERIName);
-		      System.out.println("systemStartDate = " + systemStartDate);
-		      System.out.println("systemEndDate = " + systemEndDate);
-	
-		      @SuppressWarnings("unchecked")
-		      HashMap validNode = (HashMap)(docObject.get(validNodeName));
-		      
-		      String validStartDate = (String)validNode.get(validStartERIName);
-		      String validEndDate = (String)validNode.get(validEndERIName);
-		      System.out.println("validStartDate = " + validStartDate);
-		      System.out.println("validEndDate = " + validEndDate);
-	
-		      // assertTrue("Valid start date check failed", 
-		      //		(validStartDate.equals("2008-12-31T23:59:59") && validEndDate.equals("2011-12-31T23:59:59")) ||
-		      //		(validStartDate.equals("2003-01-01T00:00:00") && validEndDate.equals("2008-12-31T23:59:59")) ||
-		      //		(validStartDate.equals("2001-01-01T00:00:00") && validEndDate.equals("2003-01-01T00:00:00")));	     
-	  		}
-	    }
-			
-	    // BUG. I believe Java API is doing a get instead of a POST that returns all documents in doc uri collection
-			System.out.println("Number of results using SQB = " + count);
-			assertEquals("Wrong number of results", 3, count);
-	  }
-		catch (Exception ex) {
-			throw ex;
-		}
+		// Read documents based on document URI and ALN Contains. We are just looking for count of documents to be correct
+		String docId = "javaSingleJSONDoc.json";
 		
-		finally {
-			ConnectedRESTQA.updateTemporalCollectionForLSQT(dbName, temporalCollectionName, false);
-		}
+		Calendar insertTime = DatatypeConverter.parseDateTime("2005-01-01T00:00:01");
+		insertJSONSingleDocument(temporalLsqtCollectionName, docId, null, null, insertTime);
+
+		Calendar updateTime = DatatypeConverter.parseDateTime("2010-01-01T00:00:01");
+		updateJSONSingleDocument(temporalLsqtCollectionName, docId, null, updateTime);
+		
+		Thread.sleep(2000);
+		
+		// Fetch documents associated with a search term (such as XML) in Address element 
+		QueryManager queryMgr = client.newQueryManager();
+		StructuredQueryBuilder sqb = queryMgr.newStructuredQueryBuilder();
+
+		Calendar queryTime = DatatypeConverter.parseDateTime("2007-01-01T00:00:01");
+		StructuredQueryDefinition periodQuery = sqb.temporalLsqtQuery(temporalLsqtCollectionName, 
+				queryTime, 0, new String[]{});
+		
+		long start = 1;
+		JSONDocumentManager docMgr = client.newJSONDocumentManager();
+		docMgr.setMetadataCategories(Metadata.ALL);	// Get all metadata
+		DocumentPage termQueryResults = docMgr.search(periodQuery, start);
+		
+		long count = 0;
+    while (termQueryResults.hasNext()) {
+    	++count;
+    	DocumentRecord record = termQueryResults.next();
+  		System.out.println("URI = " + record.getUri());
+  		
+  		DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle();
+  		record.getMetadata(metadataHandle);
+  		Iterator resCollections = metadataHandle.getCollections().iterator();
+  		while (resCollections.hasNext()) {
+  			System.out.println("Collection = " + resCollections.next()); 
+  		}
+
+  		if (record.getFormat() == Format.XML) {
+  			DOMHandle recordHandle = new DOMHandle(); 				
+  			record.getContent (recordHandle);
+	  	  System.out.println("Content = " + recordHandle.toString());   		
+  		}
+  		else {
+	  		JacksonDatabindHandle recordHandle =  new JacksonDatabindHandle(ObjectNode.class);  				
+	  		record.getContent (recordHandle);
+	  	  System.out.println("Content = " + recordHandle.toString()); 
+
+	  	  JsonFactory factory = new JsonFactory(); 
+	      ObjectMapper mapper = new ObjectMapper(factory);
+	      TypeReference> typeRef 
+	              = new TypeReference>() {};
+
+	      HashMap docObject = mapper.readValue(recordHandle.toString(), typeRef); 
+	      
+	      @SuppressWarnings("unchecked")
+	      HashMap systemNode = (HashMap)(docObject.get(systemNodeName));
+	      
+	      String systemStartDate = (String)systemNode.get(systemStartERIName);
+	      String systemEndDate = (String)systemNode.get(systemEndERIName);
+	      System.out.println("systemStartDate = " + systemStartDate);
+	      System.out.println("systemEndDate = " + systemEndDate);
+
+	      @SuppressWarnings("unchecked")
+	      HashMap validNode = (HashMap)(docObject.get(validNodeName));
+	      
+	      String validStartDate = (String)validNode.get(validStartERIName);
+	      String validEndDate = (String)validNode.get(validEndERIName);
+	      System.out.println("validStartDate = " + validStartDate);
+	      System.out.println("validEndDate = " + validEndDate);
+
+	      // assertTrue("Valid start date check failed", 
+	      //		(validStartDate.equals("2008-12-31T23:59:59") && validEndDate.equals("2011-12-31T23:59:59")) ||
+	      //		(validStartDate.equals("2003-01-01T00:00:00") && validEndDate.equals("2008-12-31T23:59:59")) ||
+	      //		(validStartDate.equals("2001-01-01T00:00:00") && validEndDate.equals("2003-01-01T00:00:00")));	     
+  		}
+    }
+		
+    // BUG. I believe Java API is doing a get instead of a POST that returns all documents in doc uri collection
+		System.out.println("Number of results using SQB = " + count);
+		assertEquals("Wrong number of results", 1, count);
 	}
 
 	@Test
@@ -2919,145 +2928,4 @@ public void testCreateCollectionUsingSameNameAsDocURI() throws Exception {
 		ConnectedRESTQA.deleteElementRangeIndexTemporalCollection("Documents", jsonDocId);
 		assertTrue("Exception not thrown for invalid temporal collection", exceptionThrown);	
 	}
-	
-	public void bulkWrite() throws Exception { 
-		
-    GenericDocumentManager docMgr = client.newDocumentManager();
-    // docMgr.setMetadataCategories(Metadata.ALL);
-		DocumentWriteSet writeset = docMgr.newWriteSet();
-
-		// put meta-data
-		DocumentMetadataHandle mh = setMetadata(false);
-		writeset.addDefault(mh);	
-		
-		// Setup for JSON document
-		/**
-		 * 
-		 {
-		 		"System": {
-		 			systemStartERIName : "",
-		 			systemEndERIName : "",
-		 		},
-		 		"Valid": {
-		 			validStartERIName: "2001-01-01T00:00:00",
-		 			validEndERIName: "2011-12-31T59:59:59"
-		 		},
-		 		"Address": "999 Skyway Park",
-		 		"uri": "javaDoc1.json"
-		 }
-		 */
-		
-		String docId[] = { "javaDoc1.json",  "javaDoc2.xml"};
-		ObjectMapper mapper = new ObjectMapper();
-		ObjectNode rootNode = mapper.createObjectNode();
-
-		// Set system time values
-		ObjectNode system = mapper.createObjectNode();
-		
-		system.put(systemStartERIName, "");
-		system.put(systemEndERIName, "");
-		rootNode.set(systemNodeName, system);
-
-		// Set valid time values
-		ObjectNode valid = mapper.createObjectNode();
-		
-		valid.put(validStartERIName, "2001-01-01T00:00:00");
-		valid.put(validEndERIName, "2011-12-31T59:59:59");
-		rootNode.set(validNodeName, valid);
-		
-		// Set Address
-		rootNode.put(addressNodeName, "999 Skyway Park");
-		
-		// Set uri
-		rootNode.put(uriNodeName, docId[0]);
-		
-		System.out.println(rootNode.toString());
-
-		JacksonDatabindHandle handle1 = new JacksonDatabindHandle(ObjectNode.class).withFormat(Format.JSON);		
-		handle1.set(rootNode);
-	  writeset.add(docId[0], handle1);
-	  
-		// Setup for XML document
-		/**
-		 
-		   
-		     
-		     
-		   
-		   
-		     2001-01-01T00:00:00
-		     2011-12-31T59:59:59
-		   
-		   888 SKyway Park"
-		   javaDoc2.xml
-		 
-		 */
-		
-		Document domDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
-		Element root = domDocument.createElement("root");
-		
-		// System start and End time
-		Node systemNode = root.appendChild(domDocument.createElement("system"));
-		systemNode.appendChild(domDocument.createElement(systemStartERIName));
-		systemNode.appendChild(domDocument.createElement(systemEndERIName));
-
-		// Valid start and End time
-		Node validNode = root.appendChild(domDocument.createElement("valid"));
-		
-		Node validStartNode = validNode.appendChild(domDocument.createElement(validStartERIName));
-		validStartNode.appendChild(domDocument.createTextNode("2001-01-01T00:00:00"));
-		validNode.appendChild(validStartNode);
-
-		Node validEndNode = validNode.appendChild(domDocument.createElement(validEndERIName));
-		validEndNode.appendChild(domDocument.createTextNode("2011-12-31T59:59:59"));
-		validNode.appendChild(validEndNode);
-		
-		// Address
-		Node addressNode = root.appendChild(domDocument.createElement("Address"));
-		addressNode.appendChild(domDocument.createTextNode("888 SKyway Park"));
-
-		// Address
-		Node uriNode = root.appendChild(domDocument.createElement("uri"));
-		uriNode.appendChild(domDocument.createTextNode(docId[1]));
-		domDocument.appendChild(root);
-
-		String domString = ((DOMImplementationLS) DocumentBuilderFactory
-				.newInstance()
-				.newDocumentBuilder()
-				.getDOMImplementation()
-				).createLSSerializer().writeToString(domDocument);
-
-		System.out.println(domString);
-
-		writeset.add(docId[1], new DOMHandle().with(domDocument));
-    docMgr.write(writeset);
-    
-				
-		/***
-		assertTrue("Did not return a iPhone 6", product1.getName().equalsIgnoreCase("iPhone 6"));
-		assertTrue("Did not return a Mobile Phone", product1.getIndustry().equalsIgnoreCase("Mobile Phone"));
-		assertTrue("Did not return a Mobile Phone", product1.getDescription().equalsIgnoreCase("New iPhone 6"));
-		
-		docMgr.readMetadata(docId[0], mhRead);
-		validateMetadata(mhRead);					
-		
-		docMgr.read(docId[1],jacksonDBReadHandle);
-		Product product2 = (Product) jacksonDBReadHandle.get();
-		assertTrue("Did not return a iMac", product2.getName().equalsIgnoreCase("iMac"));
-		assertTrue("Did not return a Desktop", product2.getIndustry().equalsIgnoreCase("Desktop"));
-		assertTrue("Did not return a Air Book OS X", product2.getDescription().equalsIgnoreCase("Air Book OS X"));
-		
-		docMgr.readMetadata(docId[1], mhRead);
-		validateMetadata(mhRead);			
-		
-		docMgr.read(docId[2], jacksonDBReadHandle);
-		Product product3 = (Product) jacksonDBReadHandle.get();
-		assertTrue("Did not return a iPad", product3.getName().equalsIgnoreCase("iPad"));
-		assertTrue("Did not return a Tablet", product3.getIndustry().equalsIgnoreCase("Tablet"));
-		assertTrue("Did not return a iPad Mini", product3.getDescription().equalsIgnoreCase("iPad Mini"));
-		
-		docMgr.readMetadata(docId[2], mhRead);
-		validateMetadata(mhRead);		
-		***/
-	}
 }

From 75fc94d408a70f3449ce5b9d60b228f7a5d977c3 Mon Sep 17 00:00:00 2001
From: gvaidees 
Date: Thu, 6 Nov 2014 22:22:47 -0800
Subject: [PATCH 297/357] Making the right calls to write overloaded methods.
 Same with create. IF extraParams is not passed, extract properties from
 binary document. All write method go through this logic by calling the right
 write and create method

---
 .../client/impl/DocumentManagerImpl.java      | 38 +++++++++----------
 1 file changed, 19 insertions(+), 19 deletions(-)

diff --git a/src/main/java/com/marklogic/client/impl/DocumentManagerImpl.java b/src/main/java/com/marklogic/client/impl/DocumentManagerImpl.java
index a0573a1c2..a67fff5cf 100644
--- a/src/main/java/com/marklogic/client/impl/DocumentManagerImpl.java
+++ b/src/main/java/com/marklogic/client/impl/DocumentManagerImpl.java
@@ -568,42 +568,42 @@ W castAbstractWriteHandle(Class as, AbstractWriteHandle handle) {
 	@Override
 	public void write(String uri, W contentHandle)
 	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(uri, null, contentHandle, null, null, null, null);
+		write(uri, null, contentHandle, null, null, null);
 	}
 	@Override
 	public void write(String uri, W contentHandle, ServerTransform transform)
 	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(uri, null, contentHandle, transform, null, null, null);
+		write(uri, null, contentHandle, transform, null);
 	}
 	@Override
 	public void write(String uri, DocumentMetadataWriteHandle metadata, W contentHandle)
 	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(uri, metadata, contentHandle, null, null, null, null);
+		write(uri, metadata, contentHandle, null, null);
 	}
 	@Override
 	public void write(String uri, DocumentMetadataWriteHandle metadata, W contentHandle, ServerTransform transform)
 	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(uri, metadata, contentHandle, transform, null, null, null);
+		write(uri, metadata, contentHandle, transform, null, null);
 	}
 	@Override
 	public void write(String uri, W contentHandle, Transaction transaction)
 	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(uri, null, contentHandle, null, transaction, null, null);
+		write(uri, null, contentHandle, null, transaction, null);
 	}
 	@Override
 	public void write(String uri, W contentHandle, ServerTransform transform, Transaction transaction)
 	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(uri, null, contentHandle, transform, transaction, null, null);
+		write(uri, null, contentHandle, transform, transaction, null);
 	}
 	@Override
 	public void write(String uri, DocumentMetadataWriteHandle metadataHandle, W contentHandle, Transaction transaction)
 	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(uri, metadataHandle, contentHandle, null, transaction, null, null);
+		write(uri, metadataHandle, contentHandle, null, transaction, null);
 	}
 	@Override
 	public void write(String uri, DocumentMetadataWriteHandle metadataHandle, W contentHandle, ServerTransform transform, Transaction transaction)
 	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(uri, metadataHandle, contentHandle, transform, transaction, null, null);
+		write(uri, metadataHandle, contentHandle, transform, transaction, null);
 	}
 
 	@Override
@@ -768,12 +768,12 @@ public void delete(String uri, Transaction transaction) throws ResourceNotFoundE
 	}
 	@Override
     public void delete(DocumentDescriptor desc) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		delete(desc, null, null, null);
+		delete(desc, null, null);
     }
 	@Override
     public void delete(DocumentDescriptor desc, Transaction transaction)
 	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		delete(desc, transaction, null, null);
+		delete(desc, transaction, null);
 	}
 
 	@Override
@@ -782,7 +782,7 @@ public void delete(String uri,
 			String temporalCollection)
 	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException
 	{
-		delete(new DocumentDescriptorImpl(uri, true), transaction, temporalCollection, null);
+		delete(new DocumentDescriptorImpl(uri, true), transaction, temporalCollection);
 	}
 	@Override
 	public void delete(String uri,
@@ -855,49 +855,49 @@ public DocumentDescriptor createAs(DocumentUriTemplate template, DocumentMetadat
 	@Override
 	public DocumentDescriptor create(DocumentUriTemplate template, W contentHandle)
 	throws ForbiddenUserException, FailedRequestException {
-		return create(template, null, contentHandle, null, null, null, null);
+		return create(template, null, contentHandle, null, null, null);
 	}
 	@Override
 	public DocumentDescriptor create(DocumentUriTemplate template, W contentHandle, 
 			ServerTransform transform)
 	throws ForbiddenUserException, FailedRequestException {
-		return create(template, null, contentHandle, transform, null, null, null);
+		return create(template, null, contentHandle, transform, null, null);
 	}
 	@Override
 	public DocumentDescriptor create(DocumentUriTemplate template, W contentHandle,
 			Transaction transaction)
 	throws ForbiddenUserException, FailedRequestException {
-		return create(template, null, contentHandle, null, transaction, null, null);
+		return create(template, null, contentHandle, null, transaction, null);
 	}
 	@Override
 	public DocumentDescriptor create(DocumentUriTemplate template, W contentHandle,
 			ServerTransform transform, Transaction transaction)
 	throws ForbiddenUserException, FailedRequestException {
-		return create(template, null, contentHandle, transform, transaction, null, null);
+		return create(template, null, contentHandle, transform, transaction, null);
 	}
 	@Override
 	public DocumentDescriptor create(DocumentUriTemplate template, DocumentMetadataWriteHandle metadataHandle,
 			W contentHandle)
 	throws ForbiddenUserException, FailedRequestException {
-		return create(template, metadataHandle, contentHandle, null, null, null, null);
+		return create(template, metadataHandle, contentHandle, null, null, null);
 	}
 	@Override
 	public DocumentDescriptor create(DocumentUriTemplate template, DocumentMetadataWriteHandle metadataHandle,
 			W contentHandle, ServerTransform transform)
 	throws ForbiddenUserException, FailedRequestException {
-		return create(template, metadataHandle, contentHandle, transform, null, null, null);
+		return create(template, metadataHandle, contentHandle, transform, null, null);
 	}
 	@Override
 	public DocumentDescriptor create(DocumentUriTemplate template, DocumentMetadataWriteHandle metadataHandle,
 			W contentHandle, Transaction transaction)
 	throws ForbiddenUserException, FailedRequestException {
-		return create(template, metadataHandle, contentHandle, null, transaction, null, null);
+		return create(template, metadataHandle, contentHandle, null, transaction, null);
 	}
 	@Override
 	public DocumentDescriptor create(DocumentUriTemplate template, DocumentMetadataWriteHandle metadataHandle,
 			W contentHandle, ServerTransform transform, Transaction transaction)
 	throws ForbiddenUserException, FailedRequestException {
-		return create(template, metadataHandle, contentHandle, transform, transaction, null, null);
+		return create(template, metadataHandle, contentHandle, transform, transaction, null);
 	}
 	@Override
   public DocumentDescriptor create(DocumentUriTemplate template,

From 04d18f3ffb9a18c36cc08773f7ba758e0d1b797b Mon Sep 17 00:00:00 2001
From: skottam 
Date: Thu, 6 Nov 2014 22:45:54 -0800
Subject: [PATCH 298/357] updated the key with cts:score-order

---
 .../javaclient/TestAppServicesValueConstraint.java     | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesValueConstraint.java b/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesValueConstraint.java
index 09d37eeda..371147d38 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesValueConstraint.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestAppServicesValueConstraint.java
@@ -75,7 +75,7 @@ public void testWildcard() throws IOException, ParserConfigurationException, SAX
 		assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc);
 		assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc);
 	    
-		String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:element-value-query(fn:QName(\"\",\"id\"), \"00*2\", (\"lang=en\"), 1), cts:element-value-query(fn:QName(\"\",\"id\"), \"0??6\", (\"lang=en\"), 1))), (\"score-logtfidf\"), 1))[1 to 10]";
+		String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:element-value-query(fn:QName(\"\",\"id\"), \"00*2\", (\"lang=en\"), 1), cts:element-value-query(fn:QName(\"\",\"id\"), \"0??6\", (\"lang=en\"), 1)), ()), (\"score-logtfidf\",cts:score-order(\"descending\")), 1))[1 to 10]";
 		
 		assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc);
 		
@@ -121,7 +121,7 @@ public void testGoogleStyleGrammar() throws IOException, ParserConfigurationExce
 		assertXpathEvaluatesTo("0113", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc);
 		assertXpathEvaluatesTo("0026", "string(//*[local-name()='result'][2]//*[local-name()='id'])", resultDoc);
 	    
-		String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:element-value-query(fn:QName(\"\",\"title\"), \"The memex\", (\"lang=en\"), 1), cts:and-query((cts:element-value-query(fn:QName(\"\",\"id\"), \"0113\", (\"lang=en\"), 1), cts:element-value-query(fn:QName(\"http://purl.org/dc/elements/1.1/\",\"date\"), \"2007-03-03\", (\"lang=en\"), 1)), ()))), (\"score-logtfidf\"), 1))[1 to 10]";
+		String expectedSearchReport = "(cts:search(fn:collection(), cts:or-query((cts:element-value-query(fn:QName(\"\",\"title\"), \"The memex\", (\"lang=en\"), 1), cts:and-query((cts:element-value-query(fn:QName(\"\",\"id\"), \"0113\", (\"lang=en\"), 1), cts:element-value-query(fn:QName(\"http://purl.org/dc/elements/1.1/\",\"date\"), \"2007-03-03\", (\"lang=en\"), 1)), ())), ()), (\"score-logtfidf\",cts:score-order(\"descending\")), 1))[1 to 10]";
 		
 		assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc);
 		
@@ -164,7 +164,7 @@ public void testWithoutIndexSettingsAndNS() throws IOException, ParserConfigurat
 		assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc);
 		assertXpathEvaluatesTo("0012", "string(//*[local-name()='result'][1]//*[local-name()='id'])", resultDoc);
 	    
-		String expectedSearchReport = "(cts:search(fn:collection(), cts:element-value-query(fn:QName(\"\",\"id\"), \"0012\", (\"lang=en\"), 1), (\"score-logtfidf\"), 1))[1 to 10]";
+		String expectedSearchReport = "(cts:search(fn:collection(), cts:element-value-query(fn:QName(\"\",\"id\"), \"0012\", (\"lang=en\"), 1), (\"score-logtfidf\",cts:score-order(\"descending\")), 1))[1 to 10]";
 		
 		assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc);
 		
@@ -206,7 +206,7 @@ public void testWithIndexSettingsAndNS() throws IOException, ParserConfiguration
 		assertXpathEvaluatesTo("1", "string(//*[local-name()='result'][last()]//@*[local-name()='index'])", resultDoc);
 		assertXpathEvaluatesTo("2007-03-03", "string(//*[local-name()='result'][1]//*[local-name()='date'])", resultDoc);
 	    
-		String expectedSearchReport = "(cts:search(fn:collection(), cts:element-value-query(fn:QName(\"http://purl.org/dc/elements/1.1/\",\"date\"), \"2007-03-03\", (\"lang=en\"), 1), (\"score-logtfidf\"), 1))[1 to 10]";
+		String expectedSearchReport = "(cts:search(fn:collection(), cts:element-value-query(fn:QName(\"http://purl.org/dc/elements/1.1/\",\"date\"), \"2007-03-03\", (\"lang=en\"), 1), (\"score-logtfidf\",cts:score-order(\"descending\")), 1))[1 to 10]";
 		
 		assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc);
 		
@@ -250,7 +250,7 @@ public void testSpaceSeparated() throws IOException, ParserConfigurationExceptio
 		assertXpathEvaluatesTo("Vannevar Bush", "string(//*[local-name()='result']//*[local-name()='title'])", resultDoc);
 		assertXpathEvaluatesTo("0.1", "string(//*[local-name()='result']//@*[local-name()='amt'])", resultDoc);
 	    
-		String expectedSearchReport = "(cts:search(fn:collection(), cts:and-query((cts:element-value-query(fn:QName(\"\",\"title\"), \"Vannevar Bush\", (\"lang=en\"), 1), cts:element-attribute-value-query(fn:QName(\"http://cloudbank.com\",\"price\"), fn:QName(\"\",\"amt\"), \"0.1\", (\"lang=en\"), 1)), ()), (\"score-logtfidf\"), 1))[1 to 10]";
+		String expectedSearchReport = "(cts:search(fn:collection(), cts:and-query((cts:element-value-query(fn:QName(\"\",\"title\"), \"Vannevar Bush\", (\"lang=en\"), 1), cts:element-attribute-value-query(fn:QName(\"http://cloudbank.com\",\"price\"), fn:QName(\"\",\"amt\"), \"0.1\", (\"lang=en\"), 1)), ()), (\"score-logtfidf\",cts:score-order(\"descending\")), 1))[1 to 10]";
 		
 		assertXpathEvaluatesTo(expectedSearchReport, "string(//*[local-name()='report'])", resultDoc);
 		

From 71a718d55ac8b6612dc5a0349a0c59e0f98da11f Mon Sep 17 00:00:00 2001
From: skottam 
Date: Fri, 7 Nov 2014 08:13:05 -0800
Subject: [PATCH 299/357] updated the test by correcting the key to not to look
 for total

---
 .../test/java/com/marklogic/javaclient/TestSearchOptions.java | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestSearchOptions.java b/test-complete/src/test/java/com/marklogic/javaclient/TestSearchOptions.java
index e92ed8d6b..b19b396d0 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/TestSearchOptions.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestSearchOptions.java
@@ -125,9 +125,9 @@ public void testSetViewMetadata() throws IOException, ParserConfigurationExcepti
 
 		// get the result
 		Document resultDoc = resultsHandle.get();
-		//System.out.println(convertXMLDocumentToString(resultDoc));
+		System.out.println(convertXMLDocumentToString(resultDoc));
 
-		assertXpathEvaluatesTo("3", "string(//*[local-name()='response']//@*[local-name()='total'])", resultDoc);
+		assertXpathEvaluatesTo("1", "string(//*[local-name()='response']//@*[local-name()='start'])", resultDoc);
 		assertXpathExists("//*[local-name()='metrics']", resultDoc);
 
 		// release client

From efcc2ddf7f2cf98f7f4471b06695883c2a8cb4eb Mon Sep 17 00:00:00 2001
From: skottam 
Date: Fri, 7 Nov 2014 09:22:42 -0800
Subject: [PATCH 300/357] updating the test with the fact that we can not use
 QBE with POJO

---
 .../marklogic/javaclient/TestPOJOwithQBEQueryDef.java    | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOwithQBEQueryDef.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOwithQBEQueryDef.java
index 5441a992f..52bb6f920 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOwithQBEQueryDef.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOwithQBEQueryDef.java
@@ -45,7 +45,6 @@ public static void setUpBeforeClass() throws Exception {
 		//		System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug");
 		System.out.println("In setup");
 		setupJavaRESTServer(dbName, fNames[0], restServerName,restPort);
-
 		BasicJavaClientREST.addRangeElementIndex(dbName, "long", "", "inventory");
 		BasicJavaClientREST.addRangeElementIndex(dbName, "long", "", "id");
 	}
@@ -126,7 +125,7 @@ public void loadSimplePojos(PojoRepository products)
 			}
 		}
 	}
-	@Test
+	@Test(expected=ClassCastException.class)
 	public void testPOJOqbeSearchWithoutSearchHandle() {
 		PojoRepository products = client.newPojoRepository(Artifact.class, Long.class);
 		PojoPage p;
@@ -165,7 +164,7 @@ public void testPOJOqbeSearchWithoutSearchHandle() {
 		assertEquals("page number after the loop",4,p.getPageNumber());
 		assertEquals("total no of pages",4,p.getTotalPages());
 	}
-	@Test
+	@Test(expected=ClassCastException.class)
 	public void testPOJOqbeSearchWithSearchHandle() {
 		PojoRepository products = client.newPojoRepository(Artifact.class, Long.class);
 		PojoPage p;
@@ -217,7 +216,7 @@ public void testPOJOqbeSearchWithSearchHandle() {
 		assertEquals("page number after the loop",5,p.getPageNumber());
 		assertEquals("total no of pages",5,p.getTotalPages());
 	}
-	@Test
+	@Test(expected=ClassCastException.class)
 	public void testPOJOCombinedSearchWithJacksonHandle() {
 		PojoRepository products = client.newPojoRepository(Artifact.class, Long.class);
 		PojoPage p;
@@ -262,7 +261,7 @@ public void testPOJOCombinedSearchWithJacksonHandle() {
 		assertEquals("total no of pages",1,p.getTotalPages());
 	}
 	//Searching for Id as Number in JSON using range query 
-	@Test
+	@Test(expected=ClassCastException.class)
 	public void testPOJOcombinedSearchforNumberWithStringHandle() throws JsonProcessingException, IOException {
 		PojoRepository products = client.newPojoRepository(Artifact.class, Long.class);
 		PojoPage p;

From ff790f7d0ea4cd5efe63ebee554c6bb8f33e2ad6 Mon Sep 17 00:00:00 2001
From: skottam 
Date: Fri, 7 Nov 2014 09:27:21 -0800
Subject: [PATCH 301/357] updating the test with the fact that we can not use
 QBE with POJO

---
 .../java/com/marklogic/javaclient/TestPOJOWithStrucQD.java    | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithStrucQD.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithStrucQD.java
index d11115b1e..4afdf92ee 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithStrucQD.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithStrucQD.java
@@ -286,7 +286,7 @@ public void testPOJOSearchWithStringHandle() throws JsonProcessingException, IOE
 
 		assertEquals("Total search results resulted are ",6,actNode.asInt() );
 	}
-	@Test
+	@Test(expected = ClassCastException.class)
 	public void testPOJOSearchWithRawXMLStructQD() {
 		PojoRepository products = client.newPojoRepository(Artifact.class, Long.class);
 		PojoPage p;
@@ -337,7 +337,7 @@ public void testPOJOSearchWithRawXMLStructQD() {
 		assertEquals("page number after the loop",1,p.getPageNumber());
 		assertEquals("total no of pages",1,p.getTotalPages());
 	}
-	@Test
+	@Test(expected = ClassCastException.class)
 	public void testPOJOSearchWithRawJSONStructQD() {
 		PojoRepository products = client.newPojoRepository(Artifact.class, Long.class);
 		PojoPage p;

From 6ea8d78abb6e3dfc0b8148b5f576971f05f62075 Mon Sep 17 00:00:00 2001
From: sammefford 
Date: Fri, 7 Nov 2014 14:24:23 -0700
Subject: [PATCH 302/357] oops, I wasn't passing along collections if you
 called count(String... collections)

---
 src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java b/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java
index f0d2dc3a3..0dc08c211 100644
--- a/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java
+++ b/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java
@@ -181,7 +181,7 @@ public long count() {
 
     @Override
     public long count(String... collections) {
-        return count((PojoQueryDefinition) null, null);
+        return count(collections, null);
     }
 
     @Override

From 9378939fa673be29131037c734b81e768e4dd421 Mon Sep 17 00:00:00 2001
From: Ajit George 
Date: Fri, 7 Nov 2014 14:20:19 -0800
Subject: [PATCH 303/357] New DateTime Test case

---
 .../javaclient/TestJacksonDateTimeFormat.java | 370 ++++++++++++++++++
 1 file changed, 370 insertions(+)
 create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/TestJacksonDateTimeFormat.java

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestJacksonDateTimeFormat.java b/test-complete/src/test/java/com/marklogic/javaclient/TestJacksonDateTimeFormat.java
new file mode 100644
index 000000000..f18a790c1
--- /dev/null
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestJacksonDateTimeFormat.java
@@ -0,0 +1,370 @@
+package com.marklogic.javaclient;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.util.Calendar;
+import java.util.TimeZone;
+
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.skyscreamer.jsonassert.JSONAssert;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.marklogic.client.DatabaseClient;
+import com.marklogic.client.DatabaseClientFactory;
+import com.marklogic.client.DatabaseClientFactory.Authentication;
+import com.marklogic.client.document.DocumentManager.Metadata;
+import com.marklogic.client.document.DocumentWriteSet;
+import com.marklogic.client.document.JSONDocumentManager;
+import com.marklogic.client.io.Format;
+import com.marklogic.client.io.JacksonHandle;
+import com.marklogic.client.pojo.PojoRepository;
+import com.marklogic.client.pojo.annotation.Id;
+
+/*
+ * Purpose : To test the date-time data type.
+ * 
+ */
+
+public class TestJacksonDateTimeFormat extends BasicJavaClientREST {
+	private static String dbName = "TestJacksonDateTimeFormatDB";
+	private static String [] fNames = {"TestJacksonDateTimeFormatDB-1"};
+	private static String restServerName = "REST-Java-Client-API-Server";
+		
+	private long artifactId = 1L;
+	private static int restPort = 8011;
+	private  DatabaseClient client;
+	
+	 /*
+	  * This class is used to test writing and reading date-time
+	  * Class member  expiryDate has been annotated with @Id.
+	  */
+	 public static class SpecialArtifactDateTime {	 
+		 @Id
+		 public Calendar expiryDate;
+
+		 private String name;
+		 private long id;
+		 private Company manufacturer;
+		 private int inventory;
+
+		 public Calendar getExpiryDate() {
+			 return expiryDate;
+		 }
+		 public SpecialArtifactDateTime setExpiryDate(Calendar expiryDate) {
+			 this.expiryDate = expiryDate; return this;
+		 }
+
+		 public long getId() {
+			 return id;
+		 }
+		 public SpecialArtifactDateTime setId(long id) {
+			 this.id= id; return this;
+		 }
+		 public String getName() {
+			 return name;
+		 }
+		 public SpecialArtifactDateTime setName(String name) {
+			 this.name = name; return this;
+		 }
+		 public Company getManufacturer() {
+			 return manufacturer;
+		 }
+		 public SpecialArtifactDateTime setManufacturer(Company manufacturer) {
+			 this.manufacturer= manufacturer; return this;
+		 }
+		 public int getInventory() {
+			 return inventory;
+		 }
+		 public SpecialArtifactDateTime setInventory(int inventory) {
+			 this.inventory= inventory; return this;
+		 }
+	 }
+	 
+	 /*
+	  * This class is used to test writing and reading date-time.
+	  * ObjectMapper's support for date time is used for handle date time formats.
+	  * Class member name has been annotated with @Id.
+	  * 
+	  */
+	 public static class SpArtifactDateTimeObjMapper {	 
+		 
+		 @Id
+		 public String name;
+		 private Calendar expiryDate;
+		 private long id;
+		 private Company manufacturer;
+		 private int inventory;
+
+		 public Calendar getExpiryDate() {
+			 return expiryDate;
+		 }
+		 public SpArtifactDateTimeObjMapper setExpiryDate(Calendar expiryDate) {
+			 this.expiryDate = expiryDate; return this;
+		 }
+
+		 public long getId() {
+			 return id;
+		 }
+		 public SpArtifactDateTimeObjMapper setId(long id) {
+			 this.id= id; return this;
+		 }
+		 public String getName() {
+			 return name;
+		 }
+		 public SpArtifactDateTimeObjMapper setName(String name) {
+			 this.name = name; return this;
+		 }
+		 public Company getManufacturer() {
+			 return manufacturer;
+		 }
+		 public SpArtifactDateTimeObjMapper setManufacturer(Company manufacturer) {
+			 this.manufacturer= manufacturer; return this;
+		 }
+		 public int getInventory() {
+			 return inventory;
+		 }
+		 public SpArtifactDateTimeObjMapper setInventory(int inventory) {
+			 this.inventory= inventory; return this;
+		 }
+	 }
+	 
+	 public long getArtifactId() {
+		 return artifactId;
+	 }
+
+	 public void setArtifactId(long artifactId) {
+		 this.artifactId = artifactId;
+	 }
+
+	@BeforeClass
+	public static void setUpBeforeClass() throws Exception {
+		//		System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug");
+		System.out.println("In setup");
+		setupJavaRESTServer(dbName, fNames[0], restServerName,restPort);
+	}
+
+	@AfterClass
+	public static void tearDownAfterClass() throws Exception {
+		System.out.println("In tear down" );
+		tearDownJavaRESTServer(dbName, fNames, restServerName);
+	}
+	@Before
+	public void setUp() throws Exception {
+		client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST);
+	}
+	@After
+	public void tearDown() throws Exception {
+		// release client
+		client.release();
+	}
+
+	// Increment and return the current Id.
+	public  long getOneLongId() {
+		long lTemp = getArtifactId();
+		setArtifactId(lTemp + 1);
+		return lTemp == 1 ? 1: lTemp;
+	}
+	
+	/*
+	 * This method is used to test POJO Repository read / search with date time.
+	 * Test should pass in the date-time field and POJORepository read should handle this request just fine.
+	 */
+	public SpecialArtifactDateTime setSpecialArtifactWithDateTime(long counter, String specialArtifactName, Calendar datetime) {
+		Company acme = new Company();
+		acme.setName(specialArtifactName);
+		acme.setWebsite("http://www.acme.com");
+		acme.setLatitude(41.998);
+		acme.setLongitude(-87.966);
+		SpecialArtifactDateTime cogs = new SpecialArtifactDateTime();
+		cogs.setId(counter);
+		cogs.setName(specialArtifactName);
+		cogs.setManufacturer(acme);
+		cogs.setInventory(1000);
+		cogs.setExpiryDate(datetime);
+		
+		return cogs;
+	}
+	
+	
+	/*
+	 * This method is used to test POJO Repository read / search with date time in the SpArtifactDateTimeObjMapper name.
+	 * Test should pass in the Name field and POJORepository read should handle this request just fine.
+	 * ObjectMapper should handle the date time formats.
+	 */
+	public SpArtifactDateTimeObjMapper setSpArtifactDateTimeObjMapper(long counter, String specialArtifactName, Calendar datetime) {
+		Company acme = new Company();
+		acme.setName(specialArtifactName);
+		acme.setWebsite("http://www.acme.com");
+		acme.setLatitude(41.998);
+		acme.setLongitude(-87.966);
+		SpArtifactDateTimeObjMapper cogs = new SpArtifactDateTimeObjMapper();
+		cogs.setId(counter);
+		cogs.setName(specialArtifactName);
+		cogs.setManufacturer(acme);
+		cogs.setInventory(1000);
+		cogs.setExpiryDate(datetime);
+		
+		return cogs;
+	}
+		
+	/*
+	 * This method is used when there is a need to validate one read and search with 
+	 * date-time in SpecialArtifactDateTime class
+	 * 
+	 */
+	public void validateSpecialArtifactDateTime(SpecialArtifactDateTime artifact, String artifactName, long longId, Calendar datetime) {
+		System.out.println("Argumnt : " + datetime.toString());
+		System.out.println("Jackson POJO : " + artifact.getExpiryDate().toString());
+		
+		assertNotNull("Artifact object should never be Null", artifact);
+		assertNotNull("Id should never be Null",artifact.id);
+		assertEquals("Id of the object is ",longId, artifact.getId());
+		assertEquals("Name of the object is ",artifactName, artifact.getName());
+		assertEquals("Inventory of the object is ",1000, artifact.getInventory());
+		assertEquals("Company name of the object is ",artifactName, artifact.getManufacturer().getName());
+		assertEquals("Web site of the object is ","http://www.acme.com", artifact.getManufacturer().getWebsite());
+		//Validate the calendar object's field, instead of object or string comparisions.
+		assertEquals("Expiry date: MONTH ",datetime.get(Calendar.MONTH), artifact.getExpiryDate().get(Calendar.MONTH));
+		assertEquals("Expiry date: DAY_OF_MONTH ",datetime.get(Calendar.DAY_OF_MONTH), artifact.getExpiryDate().get(Calendar.DAY_OF_MONTH));
+		assertEquals("Expiry date: YEAR ",datetime.get(Calendar.YEAR), artifact.getExpiryDate().get(Calendar.YEAR));
+		assertEquals("Expiry date: HOUR ",datetime.get(Calendar.HOUR), artifact.getExpiryDate().get(Calendar.HOUR));
+		assertEquals("Expiry date: MINUTE ",datetime.get(Calendar.MINUTE), artifact.getExpiryDate().get(Calendar.MINUTE));
+		assertEquals("Expiry date: SECOND ",datetime.get(Calendar.SECOND), artifact.getExpiryDate().get(Calendar.SECOND));
+		assertEquals(-87.966, artifact.getManufacturer().getLongitude(), 0.00);
+		assertEquals(41.998, artifact.getManufacturer().getLatitude(), 0.00);
+	}
+	
+	
+	/*
+	 * This method is used when there is a need to validate one read and search with 
+	 * date-time in SpArtifactDateTimeObjMapper class
+	 * 
+	 */
+	public void validateSpArtifactDateTimeObjMapper(SpArtifactDateTimeObjMapper artifact, String artifactName, long longId, Calendar datetime) {
+		System.out.println("Argumnt : " + datetime.toString());
+		System.out.println("Jackson POJO : " + artifact.getExpiryDate().toString());
+		
+		assertNotNull("Artifact object should never be Null", artifact);
+		assertNotNull("Id should never be Null",artifact.id);
+		assertEquals("Id of the object is ",longId, artifact.getId());
+		assertEquals("Name of the object is ",artifactName, artifact.getName());
+		assertEquals("Inventory of the object is ",1000, artifact.getInventory());
+		assertEquals("Company name of the object is ",artifactName, artifact.getManufacturer().getName());
+		assertEquals("Web site of the object is ","http://www.acme.com", artifact.getManufacturer().getWebsite());
+		//Validate the calendar object's field, instead of object or string comparisions.
+		assertEquals("Expiry date: MONTH ",datetime.get(Calendar.MONTH), artifact.getExpiryDate().get(Calendar.MONTH));
+		assertEquals("Expiry date: DAY_OF_MONTH ",datetime.get(Calendar.DAY_OF_MONTH), artifact.getExpiryDate().get(Calendar.DAY_OF_MONTH));
+		assertEquals("Expiry date: YEAR ",datetime.get(Calendar.YEAR), artifact.getExpiryDate().get(Calendar.YEAR));
+		assertEquals("Expiry date: HOUR ",datetime.get(Calendar.HOUR), artifact.getExpiryDate().get(Calendar.HOUR));
+		assertEquals("Expiry date: MINUTE ",datetime.get(Calendar.MINUTE), artifact.getExpiryDate().get(Calendar.MINUTE));
+		assertEquals("Expiry date: SECOND ",datetime.get(Calendar.SECOND), artifact.getExpiryDate().get(Calendar.SECOND));
+		assertEquals(-87.966, artifact.getManufacturer().getLongitude(), 0.00);
+		assertEquals(41.998, artifact.getManufacturer().getLatitude(), 0.00);
+	}
+	
+	/*
+	 * To verify that a JSON String with datetime string can be written and read back using Jackson.
+	 */
+	@Test
+	public void testWriteJSONDocsWithDateTimeAsString() throws Exception 
+	{
+		String docId[] = { "/datetime.json" };
+		String jsonDate = new String("{\"expiryDate\": {\"java.util.GregorianCalendar\": \"2014-11-06,13:00\"}}");
+		
+
+		JSONDocumentManager docMgr = client.newJSONDocumentManager();
+		docMgr.setMetadataCategories(Metadata.ALL);
+		
+		DocumentWriteSet writeset = docMgr.newWriteSet();
+		ObjectMapper mapper = new ObjectMapper();
+
+		JacksonHandle jacksonHandle1 = new JacksonHandle();
+
+		JsonNode dateNode = mapper.readTree(jsonDate);
+		jacksonHandle1.set(dateNode);
+		jacksonHandle1.withFormat(Format.JSON);
+
+		writeset.add(docId[0], jacksonHandle1);
+		docMgr.write(writeset);
+
+		JacksonHandle jacksonhandle = new JacksonHandle();
+		docMgr.read(docId[0], jacksonhandle);
+		JSONAssert.assertEquals(jsonDate, jacksonhandle.toString(), true);
+	}
+	
+	/*
+     * Purpose : This test is to validate read documents using read(Id)
+     * POJO instance @Id field value with date-time.
+     */
+    
+	@Test
+	public void testPOJORepoReadWithDateTime() {
+		PojoRepository pojoReposProducts = client.newPojoRepository(SpecialArtifactDateTime.class, Calendar.class);		
+		
+		//Load object into database
+		long longId = this.getOneLongId();
+		// Create a calendar object with GMT Time Zone, since Jackson by default uses it.
+		// You can set TZ in ObjectMapper also.
+		Calendar calTime = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
+		
+		String artifactName = new String("Acme");
+		pojoReposProducts.write(this.setSpecialArtifactWithDateTime(longId, artifactName, calTime),"odd","numbers");		
+			
+		// Validate the artifact read back.
+		SpecialArtifactDateTime artifact = pojoReposProducts.read(calTime);
+		validateSpecialArtifactDateTime(artifact, artifactName, longId, calTime);
+	}
+	
+	/*
+	 * Delete using date-time
+	 */
+	@Test
+	public void testPOJORepoDeleteDateTime() {
+		PojoRepository pojoReposProducts = client.newPojoRepository(SpecialArtifactDateTime.class, Calendar.class);		
+		
+		//Load object into database
+		long longId = this.getOneLongId();
+		// Create a calendar object with GMT Time Zone, since Jackson by default uses it.
+		// You can set TZ in ObjectMapper also.
+		Calendar calTime = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
+		
+		String artifactName = new String("Acme");
+		pojoReposProducts.write(this.setSpecialArtifactWithDateTime(longId, artifactName, calTime),"odd","numbers");		
+			
+		// Validate the artifact read back.
+		SpecialArtifactDateTime artifact = pojoReposProducts.read(calTime);
+		validateSpecialArtifactDateTime(artifact, artifactName, longId, calTime);
+		
+		pojoReposProducts.delete(calTime);
+		// Validate the artifact read back.
+		long count = pojoReposProducts.count();
+			
+		assertEquals("Artifact with calendar as Id found - Delete did not work",0L, count);		
+	}
+	
+	@Test
+	public void testReadWriteDateTimeObjectWithMapper() {
+		PojoRepository pojoReposProducts = client.newPojoRepository(SpArtifactDateTimeObjMapper.class, String.class);		
+		
+		//Load object into database
+		long longId = this.getOneLongId();
+		// Create a calendar object with GMT Time Zone, since Jackson by default uses it.
+		// You can set TZ in ObjectMapper also.
+		Calendar calTime = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
+		
+		String artifactName = new String("Acme");
+		pojoReposProducts.write(this.setSpArtifactDateTimeObjMapper(longId, artifactName, calTime),"odd","numbers");		
+			
+		// Validate the artifact read back.
+		SpArtifactDateTimeObjMapper artifact = pojoReposProducts.read(artifactName);
+		validateSpArtifactDateTimeObjMapper(artifact, artifactName, longId, calTime);
+	}
+	
+	// Additional methods usingObjectmapper and File Streams with jacksonDataBind need to be here.artifactDel
+}

From bda34e9f2d4d2aa7bfa5b1da5e36bddd69fc408e Mon Sep 17 00:00:00 2001
From: gvaidees 
Date: Fri, 7 Nov 2014 14:48:17 -0800
Subject: [PATCH 304/357] Fix for bug 30372

---
 .../java/com/marklogic/client/impl/DocumentManagerImpl.java   | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/main/java/com/marklogic/client/impl/DocumentManagerImpl.java b/src/main/java/com/marklogic/client/impl/DocumentManagerImpl.java
index a67fff5cf..f449e4c0b 100644
--- a/src/main/java/com/marklogic/client/impl/DocumentManagerImpl.java
+++ b/src/main/java/com/marklogic/client/impl/DocumentManagerImpl.java
@@ -573,12 +573,12 @@ public void write(String uri, W contentHandle)
 	@Override
 	public void write(String uri, W contentHandle, ServerTransform transform)
 	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(uri, null, contentHandle, transform, null);
+		write(uri, null, contentHandle, transform, null, null);
 	}
 	@Override
 	public void write(String uri, DocumentMetadataWriteHandle metadata, W contentHandle)
 	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(uri, metadata, contentHandle, null, null);
+		write(uri, metadata, contentHandle, null, null, null);
 	}
 	@Override
 	public void write(String uri, DocumentMetadataWriteHandle metadata, W contentHandle, ServerTransform transform)

From ef6161022635ae0c07c0821b5068d8cf1b298999 Mon Sep 17 00:00:00 2001
From: gvaidees 
Date: Fri, 7 Nov 2014 16:36:20 -0800
Subject: [PATCH 305/357] Formatted the file to be 2 spaces and tabs as space

---
 .../client/impl/DocumentManagerImpl.java      | 2220 +++++++++--------
 1 file changed, 1170 insertions(+), 1050 deletions(-)

diff --git a/src/main/java/com/marklogic/client/impl/DocumentManagerImpl.java b/src/main/java/com/marklogic/client/impl/DocumentManagerImpl.java
index f449e4c0b..3b09f2dd0 100644
--- a/src/main/java/com/marklogic/client/impl/DocumentManagerImpl.java
+++ b/src/main/java/com/marklogic/client/impl/DocumentManagerImpl.java
@@ -58,1095 +58,1215 @@
 import com.marklogic.client.util.RequestParameters;
 
 abstract class DocumentManagerImpl
-    extends AbstractLoggingManager
-    implements DocumentManager, TemporalDocumentManager
-{
-    static final private long DEFAULT_PAGE_LENGTH = 50;
-
-	static final private Logger logger = LoggerFactory.getLogger(DocumentManagerImpl.class);
-
-    private boolean isProcessedMetadataModified = false;
-	final private Set processedMetadata = new HashSet() {
-        public boolean add(Metadata e) {
-            isProcessedMetadataModified = true;
-            return super.add(e);
-        }
-        public boolean addAll(Collection c) {
-            isProcessedMetadataModified = true;
-            return super.addAll(c);
-        }
-    };
-    {
-        processedMetadata.add(Metadata.ALL);
-        // we need to know if the user modifies after us
-        isProcessedMetadataModified = false;
-    }
-        
-
-	private RESTServices          services;
-	private Format                contentFormat;
-	private HandleFactoryRegistry handleRegistry;
-	private ServerTransform       readTransform;
-	private ServerTransform       writeTransform;
-    private String                forestName;
-    private long                  pageLength = DEFAULT_PAGE_LENGTH;
-    private QueryView searchView = QueryView.RESULTS;
-    private Format nonDocumentFormat = null;
-
-	DocumentManagerImpl(RESTServices services, Format contentFormat) {
-		super();
-		this.services       = services;
-		this.contentFormat  = contentFormat;
-	}
-
-	RESTServices getServices() {
-		return services;
-	}
-	void setServices(RESTServices services) {
-		this.services = services;
-	}
-
-	HandleFactoryRegistry getHandleRegistry() {
-		return handleRegistry;
-	}
-	void setHandleRegistry(HandleFactoryRegistry handleRegistry) {
-		this.handleRegistry = handleRegistry;
-	}
-
-	@Override
-    public Format getContentFormat() {
-    	return contentFormat;
+    extends AbstractLoggingManager implements DocumentManager,
+    TemporalDocumentManager {
+  static final private long DEFAULT_PAGE_LENGTH = 50;
+
+  static final private Logger logger = LoggerFactory
+      .getLogger(DocumentManagerImpl.class);
+
+  private boolean isProcessedMetadataModified = false;
+  final private Set processedMetadata = new HashSet() {
+    public boolean add(Metadata e) {
+      isProcessedMetadataModified = true;
+      return super.add(e);
     }
 
-    // select categories of metadata to read, write, or reset
-	@Override
-    public void setMetadataCategories(Set categories) {
-		clearMetadataCategories();
-		processedMetadata.addAll(categories);
-    }
-	@Override
-    public void setMetadataCategories(Metadata... categories) {
-		clearMetadataCategories();
-    	for (Metadata category: categories)
-    		processedMetadata.add(category);
-    }
-	@Override
-    public Set getMetadataCategories() {
-    	return processedMetadata;
-    }
-	@Override
-    public void clearMetadataCategories() {
-   		processedMetadata.clear();
+    public boolean addAll(Collection c) {
+      isProcessedMetadataModified = true;
+      return super.addAll(c);
     }
+  };
+  {
+    processedMetadata.add(Metadata.ALL);
+    // we need to know if the user modifies after us
+    isProcessedMetadataModified = false;
+  }
 
+  private RESTServices services;
+  private Format contentFormat;
+  private HandleFactoryRegistry handleRegistry;
+  private ServerTransform readTransform;
+  private ServerTransform writeTransform;
+  private String forestName;
+  private long pageLength = DEFAULT_PAGE_LENGTH;
+  private QueryView searchView = QueryView.RESULTS;
+  private Format nonDocumentFormat = null;
+
+  DocumentManagerImpl(RESTServices services, Format contentFormat) {
+    super();
+    this.services = services;
+    this.contentFormat = contentFormat;
+  }
 
-	@Override
-	public DocumentDescriptor exists(String uri) throws ForbiddenUserException, FailedRequestException {
-		return exists(uri, null);
-    }
-	@Override
-	public DocumentDescriptor exists(String uri, Transaction transaction) throws ForbiddenUserException, FailedRequestException {
-		return services.head(requestLogger, uri, (transaction == null) ? null : transaction.getTransactionId());
-	}
-
-	// shortcut readers
-	@Override
-    public  T readAs(String uri, Class as)
-	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		return readAs(uri, null, as, null);
-	}
-	@Override
-	public  T readAs(String uri, Class as, ServerTransform transform)
-	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		return readAs(uri, null, as, transform);
-	}
-	@Override
-	public  T readAs(String uri, DocumentMetadataReadHandle metadataHandle, Class as)
-	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		return readAs(uri, metadataHandle, as, null);
-	}
-	@Override
-    public  T readAs(String uri, DocumentMetadataReadHandle metadataHandle, Class as, ServerTransform transform)
-	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-			ContentHandle handle = getHandleRegistry().makeHandle(as);
-
-			if (null == read(uri, metadataHandle, castAbstractReadHandle(as, handle), transform)) {
-				return null;
-			}
-
-			return handle.get();
-	}
-	R castAbstractReadHandle(Class as, AbstractReadHandle handle) {
-		try {
-			@SuppressWarnings("unchecked")
-			R readHandle = (R) handle;
-			return readHandle;
-		} catch(ClassCastException e) {
-			throw new IllegalArgumentException(
-					"Handle "+handle.getClass().getName()+
-					" cannot be used in the context to read "+as.getName()
-					);
-		}
-	}
-
-	// strongly typed readers
-	@Override
-	public  T read(String uri, T contentHandle)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		return read(uri, null, contentHandle, null, null);
-	}
-	@Override
-	public  T read(String uri, T contentHandle, ServerTransform transform)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		return read(uri, null, contentHandle, transform, null);
-	}
-	@Override
-	public  T read(String uri, DocumentMetadataReadHandle metadataHandle, T contentHandle)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		return read(uri, metadataHandle, contentHandle, null, null);
-	}
-	@Override
-	public  T read(String uri, DocumentMetadataReadHandle metadataHandle, T contentHandle, ServerTransform transform)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		return read(uri, metadataHandle, contentHandle, transform, null);
-	}
-	@Override
-	public  T read(String uri, T contentHandle, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		return read(uri, null, contentHandle, null, transaction);
-	}
-	@Override
-	public  T read(String uri, T contentHandle, ServerTransform transform, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		return read(uri, null, contentHandle, transform, transaction);
-	}
-	@Override
-	public  T read(String uri, DocumentMetadataReadHandle metadataHandle, T contentHandle, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		return read(uri, metadataHandle, contentHandle, null, transaction);
-	}
-	@Override
-	public  T read(String uri, DocumentMetadataReadHandle metadataHandle, T contentHandle, ServerTransform transform, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-//		return read(new DocumentDescriptorImpl(docId, true), metadataHandle, contentHandle, transform, transaction, null, null);
-		return read(new DocumentDescriptorImpl(uri, true), metadataHandle, contentHandle, transform, transaction, null, getReadParams());
-	}
-	/*
-	@Override
-	public  T read(String docId,
-			DocumentMetadataReadHandle metadataHandle,
-			T contentHandle,
-			ServerTransform transform,
-			Transaction transaction,
-			String temporalCollection)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException
-	{
-		return read(new DocumentDescriptorImpl(docId, true), metadataHandle, contentHandle, transform, 
-			transaction, temporalCollection, null);
-	}
-	*/
-
-	@Override
-    public  T read(DocumentDescriptor desc, T contentHandle)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		return read(desc, null, contentHandle, null, null);
-	}
-
-	@Override
-    public  T read(DocumentDescriptor desc, T contentHandle, ServerTransform transform)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		return read(desc, null, contentHandle, transform, null);
-	}
-	@Override
-	public  T read(DocumentDescriptor desc, DocumentMetadataReadHandle metadataHandle, T contentHandle)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		return read(desc, metadataHandle, contentHandle, null, null);
-	}
-	@Override
-	public  T read(DocumentDescriptor desc, DocumentMetadataReadHandle metadataHandle, T contentHandle, ServerTransform transform)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		return read(desc, metadataHandle, contentHandle, transform, null);
-	}
-	@Override
-	public  T read(DocumentDescriptor desc, T contentHandle, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		return read(desc, null, contentHandle, null, transaction);
-	}
-	@Override
-	public  T read(DocumentDescriptor desc, T contentHandle, ServerTransform transform, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		return read(desc, null, contentHandle, transform, transaction);
-	}
-	@Override
-	public  T read(DocumentDescriptor desc, DocumentMetadataReadHandle metadataHandle, T contentHandle, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		return read(desc, metadataHandle, contentHandle, null, transaction);
-	}
-	@Override
-	public  T read(DocumentDescriptor desc, DocumentMetadataReadHandle metadataHandle, T contentHandle, ServerTransform transform, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		return read(desc, metadataHandle, contentHandle, transform, transaction, null, getReadParams());
-	}
-	/*
-	@Override
-	public  T read(DocumentDescriptor desc,
-			DocumentMetadataReadHandle metadataHandle,
-			T  contentHandle,
-			ServerTransform transform,
-			Transaction transaction,
-			String temporalCollection)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException
-	{
-		return read(desc, metadataHandle, contentHandle, transform, transaction, temporalCollection, getReadParams());
-	}
-	*/
-
-	@SuppressWarnings("rawtypes")
-	public  T read(DocumentDescriptor desc,
-		DocumentMetadataReadHandle metadataHandle,
-		T  contentHandle,
-		ServerTransform transform,
-		Transaction transaction,
-		String temporalCollection, 
-		RequestParameters extraParams)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException
-	{
-		if (desc == null)
-			throw new IllegalArgumentException("Attempt to call read with null DocumentDescriptor");
-
-		if (logger.isInfoEnabled())
-			logger.info("Reading metadata and content for {}", desc.getUri());
-
-		if (metadataHandle != null) {
-			HandleImplementation metadataBase = HandleAccessor.checkHandle(metadataHandle, "metadata");
-			Format metadataFormat = metadataBase.getFormat();
-			if (metadataFormat == null || (metadataFormat != Format.JSON && metadataFormat != Format.XML)) {
-				if (logger.isWarnEnabled())
-					logger.warn("Unsupported metadata format {}, using XML",metadataFormat.name());
-				metadataBase.setFormat(Format.XML);
-			}
-		}
-
-		checkContentFormat(contentHandle);
-
-		extraParams = addTemporalParams(extraParams, temporalCollection, null);
-
-		boolean wasModified = services.getDocument(
-				requestLogger,
-				desc, 
-				(transaction != null) ? transaction.getTransactionId() : null,
-				(metadataHandle != null) ? processedMetadata : null,
-				mergeTransformParameters(
-						(transform != null) ? transform : getReadTransform(),
-						extraParams
-						),
-				metadataHandle,
-				contentHandle
-				);
-
-		// TODO: after response, reset metadata and set flag
-
-		return wasModified ? contentHandle : null;
-	}
-
-	@Override
-	public DocumentPage read(String... uris) {
-		return read(null, null, uris);
-	}
-
-	@Override
-	public DocumentPage read(Transaction transaction, String... uris) {
-		return read(null, transaction, uris);
-	}
-
-	@Override
-	public DocumentPage read(ServerTransform transform, String... uris) {
-		return read(transform, null, uris);
-	}
-
-	@Override
-	public DocumentPage read(ServerTransform transform, Transaction transaction, String... uris) {
-		boolean withContent = true;
-		return read(transform, transaction, withContent, null, uris);
-	}
-
-	/*
-	@Override
-	public DocumentPage read(ServerTransform transform,
-		Transaction transaction,
-		String temporalCollection,
-		String[] uris)
-	{
-		boolean withContent = true;
-		return read(transform, transaction, withContent, temporalCollection, uris);
-	}
-	*/
-	public DocumentPage read(ServerTransform transform,
-		Transaction transaction,
-		boolean withContent,
-		String temporalCollection,
-		String[] uris)
-	{
-		if (uris == null || uris.length == 0)
-			throw new IllegalArgumentException("Attempt to call read with no uris");
-
-		if (logger.isInfoEnabled())
-			logger.info("Reading metadata and content for multiple uris beginning with {}", uris[0]);
-
-		RequestParameters extraParams = addTemporalParams(new RequestParameters(), temporalCollection, null);
-
-        return services.getBulkDocuments(
-            requestLogger,
-            (transaction == null) ? null : transaction.getTransactionId(),
-            // the default for bulk is no metadata, which differs from the normal default of ALL
-            isProcessedMetadataModified ? processedMetadata : null,
-            nonDocumentFormat,
-            mergeTransformParameters(
-                    (transform != null) ? transform : getReadTransform(),
-                    extraParams
-            ),
-            withContent,
-            uris);
-   	}
-
-	public DocumentPage readMetadata(String... uris) {
-		boolean withContent = false;
-		return read(null, null, withContent, null, uris);
-	}
-
-	public DocumentPage readMetadata(Transaction transaction, String... uris) {
-		boolean withContent = false;
-		return read(null, transaction, withContent, null, uris);
-	}
-
-	public DocumentPage search(QueryDefinition querydef, long start) {
-		return search(querydef, start, null, null);
-	}
-
-	public DocumentPage search(QueryDefinition querydef, long start, SearchReadHandle searchHandle) {
-		return search(querydef, start, searchHandle, null);
-	}
-
-	public DocumentPage search(QueryDefinition querydef, long start, Transaction transaction) {
-		return search(querydef, start, null, transaction);
-	}
-
-	public DocumentPage search(QueryDefinition querydef, long start, SearchReadHandle searchHandle, Transaction transaction) {
-
-        if ( searchHandle != null ) {
-            HandleImplementation searchBase = HandleAccessor.checkHandle(searchHandle, "search");
-            if (searchHandle instanceof SearchHandle) {
-                SearchHandle responseHandle = (SearchHandle) searchHandle;
-                responseHandle.setHandleRegistry(getHandleRegistry());
-                responseHandle.setQueryCriteria(querydef);
-            }
-            if ( nonDocumentFormat != null && nonDocumentFormat != searchBase.getFormat() ) {
-                throw new UnsupportedOperationException("The format supported by your handle:[" + 
-                    searchBase.getFormat() + "] does not match the non-document format:[" + nonDocumentFormat + "]");
-            }
-        }
-
-        String tid = transaction == null ? null : transaction.getTransactionId();
-        // the default for bulk is no metadata, which differs from the normal default of ALL
-        Set metadata = isProcessedMetadataModified ? processedMetadata : null;
-        return services.getBulkDocuments( requestLogger, querydef, start, getPageLength(), 
-            tid, searchHandle, searchView, metadata, nonDocumentFormat, null);
-	}
-
-    public long getPageLength() {
-        return pageLength;
-    }
+  RESTServices getServices() {
+    return services;
+  }
 
-    public void setPageLength(long length) {
-        this.pageLength = length;
-    }
+  void setServices(RESTServices services) {
+    this.services = services;
+  }
 
-    public QueryView getSearchView() {
-        return searchView;
-    }
+  HandleFactoryRegistry getHandleRegistry() {
+    return handleRegistry;
+  }
 
-    public void setSearchView(QueryView view) {
-        this.searchView = view;
-    }
+  void setHandleRegistry(HandleFactoryRegistry handleRegistry) {
+    this.handleRegistry = handleRegistry;
+  }
 
-    public Format getNonDocumentFormat() {
-        return nonDocumentFormat;
-    }
+  @Override
+  public Format getContentFormat() {
+    return contentFormat;
+  }
 
-    public void setNonDocumentFormat(Format nonDocumentFormat) {
-        if ( nonDocumentFormat != Format.XML && nonDocumentFormat != Format.JSON ) {
-            throw new UnsupportedOperationException("Only XML and JSON are valid response formats.  You specified:[" + 
-                nonDocumentFormat + "]");
-        }
-        this.nonDocumentFormat = nonDocumentFormat;
-    }
+  // select categories of metadata to read, write, or reset
+  @Override
+  public void setMetadataCategories(Set categories) {
+    clearMetadataCategories();
+    processedMetadata.addAll(categories);
+  }
+
+  @Override
+  public void setMetadataCategories(Metadata... categories) {
+    clearMetadataCategories();
+    for (Metadata category : categories)
+      processedMetadata.add(category);
+  }
+
+  @Override
+  public Set getMetadataCategories() {
+    return processedMetadata;
+  }
 
-	public DocumentWriteSet newWriteSet() {
-		return new DocumentWriteSetImpl();
-	}
-
-	public void write(DocumentWriteSet writeSet) {
-        write(writeSet, null, null);
-	}
-
-	public void write(DocumentWriteSet writeSet, ServerTransform transform) {
-        write(writeSet, transform, null);
-	}
-
-	public void write(DocumentWriteSet writeSet, Transaction transaction) {
-        write(writeSet, null, transaction);
-	}
- 
-	public void write(DocumentWriteSet writeSet, ServerTransform transform, Transaction transaction) {
-		Format defaultFormat = contentFormat;
-		services.postBulkDocuments(
-            requestLogger,
-            writeSet,
-            (transform != null) ? transform : getWriteTransform(),
-            (transaction == null) ? null : transaction.getTransactionId(),
-			defaultFormat,
-			null);
-	}
-
-	// shortcut writers
-	@Override
-    public void writeAs(String uri, Object content)
-	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		writeAs(uri, null, content, null);
+  @Override
+  public void clearMetadataCategories() {
+    processedMetadata.clear();
+  }
+
+  @Override
+  public DocumentDescriptor exists(String uri) throws ForbiddenUserException,
+      FailedRequestException {
+    return exists(uri, null);
+  }
+
+  @Override
+  public DocumentDescriptor exists(String uri, Transaction transaction)
+      throws ForbiddenUserException, FailedRequestException {
+    return services.head(requestLogger, uri, (transaction == null) ? null
+        : transaction.getTransactionId());
+  }
+
+  // shortcut readers
+  @Override
+  public  T readAs(String uri, Class as)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    return readAs(uri, null, as, null);
+  }
+
+  @Override
+  public  T readAs(String uri, Class as, ServerTransform transform)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    return readAs(uri, null, as, transform);
+  }
+
+  @Override
+  public  T readAs(String uri, DocumentMetadataReadHandle metadataHandle,
+      Class as) throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    return readAs(uri, metadataHandle, as, null);
+  }
+
+  @Override
+  public  T readAs(String uri, DocumentMetadataReadHandle metadataHandle,
+      Class as, ServerTransform transform) throws ResourceNotFoundException,
+      ForbiddenUserException, FailedRequestException {
+    ContentHandle handle = getHandleRegistry().makeHandle(as);
+
+    if (null == read(uri, metadataHandle, castAbstractReadHandle(as, handle),
+        transform)) {
+      return null;
     }
-	@Override
-    public void writeAs(String uri, Object content, ServerTransform transform)
-	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		writeAs(uri, null, content, transform);
+
+    return handle.get();
+  }
+
+  R castAbstractReadHandle(Class as, AbstractReadHandle handle) {
+    try {
+      @SuppressWarnings("unchecked")
+      R readHandle = (R) handle;
+      return readHandle;
+    } catch (ClassCastException e) {
+      throw new IllegalArgumentException("Handle "
+          + handle.getClass().getName()
+          + " cannot be used in the context to read " + as.getName());
     }
-	@Override
-    public void writeAs(String uri, DocumentMetadataWriteHandle metadataHandle, Object content)
-	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		writeAs(uri, metadataHandle, content, null);
+  }
+
+  // strongly typed readers
+  @Override
+  public  T read(String uri, T contentHandle)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    return read(uri, null, contentHandle, null, null);
+  }
+
+  @Override
+  public  T read(String uri, T contentHandle,
+      ServerTransform transform) throws ResourceNotFoundException,
+      ForbiddenUserException, FailedRequestException {
+    return read(uri, null, contentHandle, transform, null);
+  }
+
+  @Override
+  public  T read(String uri,
+      DocumentMetadataReadHandle metadataHandle, T contentHandle)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    return read(uri, metadataHandle, contentHandle, null, null);
+  }
+
+  @Override
+  public  T read(String uri,
+      DocumentMetadataReadHandle metadataHandle, T contentHandle,
+      ServerTransform transform) throws ResourceNotFoundException,
+      ForbiddenUserException, FailedRequestException {
+    return read(uri, metadataHandle, contentHandle, transform, null);
+  }
+
+  @Override
+  public  T read(String uri, T contentHandle,
+      Transaction transaction) throws ResourceNotFoundException,
+      ForbiddenUserException, FailedRequestException {
+    return read(uri, null, contentHandle, null, transaction);
+  }
+
+  @Override
+  public  T read(String uri, T contentHandle,
+      ServerTransform transform, Transaction transaction)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    return read(uri, null, contentHandle, transform, transaction);
+  }
+
+  @Override
+  public  T read(String uri,
+      DocumentMetadataReadHandle metadataHandle, T contentHandle,
+      Transaction transaction) throws ResourceNotFoundException,
+      ForbiddenUserException, FailedRequestException {
+    return read(uri, metadataHandle, contentHandle, null, transaction);
+  }
+
+  @Override
+  public  T read(String uri,
+      DocumentMetadataReadHandle metadataHandle, T contentHandle,
+      ServerTransform transform, Transaction transaction)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    // return read(new DocumentDescriptorImpl(docId, true), metadataHandle,
+    // contentHandle, transform, transaction, null, null);
+    return read(new DocumentDescriptorImpl(uri, true), metadataHandle,
+        contentHandle, transform, transaction, null, getReadParams());
+  }
+
+  /*
+   * @Override public  T read(String docId,
+   * DocumentMetadataReadHandle metadataHandle, T contentHandle, ServerTransform
+   * transform, Transaction transaction, String temporalCollection) throws
+   * ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
+   * return read(new DocumentDescriptorImpl(docId, true), metadataHandle,
+   * contentHandle, transform, transaction, temporalCollection, null); }
+   */
+
+  @Override
+  public  T read(DocumentDescriptor desc, T contentHandle)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    return read(desc, null, contentHandle, null, null);
+  }
+
+  @Override
+  public  T read(DocumentDescriptor desc, T contentHandle,
+      ServerTransform transform) throws ResourceNotFoundException,
+      ForbiddenUserException, FailedRequestException {
+    return read(desc, null, contentHandle, transform, null);
+  }
+
+  @Override
+  public  T read(DocumentDescriptor desc,
+      DocumentMetadataReadHandle metadataHandle, T contentHandle)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    return read(desc, metadataHandle, contentHandle, null, null);
+  }
+
+  @Override
+  public  T read(DocumentDescriptor desc,
+      DocumentMetadataReadHandle metadataHandle, T contentHandle,
+      ServerTransform transform) throws ResourceNotFoundException,
+      ForbiddenUserException, FailedRequestException {
+    return read(desc, metadataHandle, contentHandle, transform, null);
+  }
+
+  @Override
+  public  T read(DocumentDescriptor desc, T contentHandle,
+      Transaction transaction) throws ResourceNotFoundException,
+      ForbiddenUserException, FailedRequestException {
+    return read(desc, null, contentHandle, null, transaction);
+  }
+
+  @Override
+  public  T read(DocumentDescriptor desc, T contentHandle,
+      ServerTransform transform, Transaction transaction)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    return read(desc, null, contentHandle, transform, transaction);
+  }
+
+  @Override
+  public  T read(DocumentDescriptor desc,
+      DocumentMetadataReadHandle metadataHandle, T contentHandle,
+      Transaction transaction) throws ResourceNotFoundException,
+      ForbiddenUserException, FailedRequestException {
+    return read(desc, metadataHandle, contentHandle, null, transaction);
+  }
+
+  @Override
+  public  T read(DocumentDescriptor desc,
+      DocumentMetadataReadHandle metadataHandle, T contentHandle,
+      ServerTransform transform, Transaction transaction)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    return read(desc, metadataHandle, contentHandle, transform, transaction,
+        null, getReadParams());
+  }
+
+  /*
+   * @Override public  T read(DocumentDescriptor desc,
+   * DocumentMetadataReadHandle metadataHandle, T contentHandle, ServerTransform
+   * transform, Transaction transaction, String temporalCollection) throws
+   * ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
+   * return read(desc, metadataHandle, contentHandle, transform, transaction,
+   * temporalCollection, getReadParams()); }
+   */
+
+  @SuppressWarnings("rawtypes")
+  public  T read(DocumentDescriptor desc,
+      DocumentMetadataReadHandle metadataHandle, T contentHandle,
+      ServerTransform transform, Transaction transaction,
+      String temporalCollection, RequestParameters extraParams)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    if (desc == null)
+      throw new IllegalArgumentException(
+          "Attempt to call read with null DocumentDescriptor");
+
+    if (logger.isInfoEnabled())
+      logger.info("Reading metadata and content for {}", desc.getUri());
+
+    if (metadataHandle != null) {
+      HandleImplementation metadataBase = HandleAccessor.checkHandle(
+          metadataHandle, "metadata");
+      Format metadataFormat = metadataBase.getFormat();
+      if (metadataFormat == null
+          || (metadataFormat != Format.JSON && metadataFormat != Format.XML)) {
+        if (logger.isWarnEnabled())
+          logger.warn("Unsupported metadata format {}, using XML",
+              metadataFormat.name());
+        metadataBase.setFormat(Format.XML);
+      }
     }
-	@Override
-    public void writeAs(String uri, DocumentMetadataWriteHandle metadataHandle, Object content, ServerTransform transform)
-	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		if (content == null) {
-			throw new IllegalArgumentException("no content to write");
-		}
-
-		Class as = content.getClass();
-
-		W writeHandle = null;
-		if (AbstractWriteHandle.class.isAssignableFrom(as)) {
-			AbstractWriteHandle handle = (AbstractWriteHandle) content;
-			writeHandle = castAbstractWriteHandle(null, handle);			
-		} else {
-			ContentHandle handle = getHandleRegistry().makeHandle(as);
-			Utilities.setHandleContent(handle, content);
-			writeHandle = castAbstractWriteHandle(as, handle);			
-		}
-
-		write(uri, metadataHandle, writeHandle, transform);			
-	}
-	W castAbstractWriteHandle(Class as, AbstractWriteHandle handle) {
-		try {
-			@SuppressWarnings("unchecked")
-			W writeHandle = (W) handle;
-			return writeHandle;
-		} catch(ClassCastException e) {
-			if (as == null) {
-				throw new IllegalArgumentException(
-						"Handle "+handle.getClass().getName()+
-						" cannot be used in the context for writing"
-						);
-			}
-			throw new IllegalArgumentException(
-					"Handle "+handle.getClass().getName()+
-					" cannot be used in the context to write "+as.getName()
-					);
-		}
-	}
-
-	// strongly typed writers
-	@Override
-	public void write(String uri, W contentHandle)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(uri, null, contentHandle, null, null, null);
-	}
-	@Override
-	public void write(String uri, W contentHandle, ServerTransform transform)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(uri, null, contentHandle, transform, null, null);
-	}
-	@Override
-	public void write(String uri, DocumentMetadataWriteHandle metadata, W contentHandle)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(uri, metadata, contentHandle, null, null, null);
-	}
-	@Override
-	public void write(String uri, DocumentMetadataWriteHandle metadata, W contentHandle, ServerTransform transform)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(uri, metadata, contentHandle, transform, null, null);
-	}
-	@Override
-	public void write(String uri, W contentHandle, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(uri, null, contentHandle, null, transaction, null);
-	}
-	@Override
-	public void write(String uri, W contentHandle, ServerTransform transform, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(uri, null, contentHandle, transform, transaction, null);
-	}
-	@Override
-	public void write(String uri, DocumentMetadataWriteHandle metadataHandle, W contentHandle, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(uri, metadataHandle, contentHandle, null, transaction, null);
-	}
-	@Override
-	public void write(String uri, DocumentMetadataWriteHandle metadataHandle, W contentHandle, ServerTransform transform, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(uri, metadataHandle, contentHandle, transform, transaction, null);
-	}
-
-	@Override
-    public void write(String uri,
-        DocumentMetadataWriteHandle metadataHandle,
-        W contentHandle,
-        ServerTransform transform,
-        Transaction transaction,
-        String temporalCollection)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException
-    {
-		write(uri, metadataHandle, contentHandle, transform, transaction, temporalCollection, null, getWriteParams());
+
+    checkContentFormat(contentHandle);
+
+    extraParams = addTemporalParams(extraParams, temporalCollection, null);
+
+    boolean wasModified = services.getDocument(
+        requestLogger,
+        desc,
+        (transaction != null) ? transaction.getTransactionId() : null,
+        (metadataHandle != null) ? processedMetadata : null,
+        mergeTransformParameters((transform != null) ? transform
+            : getReadTransform(), extraParams), metadataHandle, contentHandle);
+
+    // TODO: after response, reset metadata and set flag
+
+    return wasModified ? contentHandle : null;
+  }
+
+  @Override
+  public DocumentPage read(String... uris) {
+    return read(null, null, uris);
+  }
+
+  @Override
+  public DocumentPage read(Transaction transaction, String... uris) {
+    return read(null, transaction, uris);
+  }
+
+  @Override
+  public DocumentPage read(ServerTransform transform, String... uris) {
+    return read(transform, null, uris);
+  }
+
+  @Override
+  public DocumentPage read(ServerTransform transform, Transaction transaction,
+      String... uris) {
+    boolean withContent = true;
+    return read(transform, transaction, withContent, null, uris);
+  }
+
+  /*
+   * @Override public DocumentPage read(ServerTransform transform, Transaction
+   * transaction, String temporalCollection, String[] uris) { boolean
+   * withContent = true; return read(transform, transaction, withContent,
+   * temporalCollection, uris); }
+   */
+  public DocumentPage read(ServerTransform transform, Transaction transaction,
+      boolean withContent, String temporalCollection, String[] uris) {
+    if (uris == null || uris.length == 0)
+      throw new IllegalArgumentException("Attempt to call read with no uris");
+
+    if (logger.isInfoEnabled())
+      logger.info(
+          "Reading metadata and content for multiple uris beginning with {}",
+          uris[0]);
+
+    RequestParameters extraParams = addTemporalParams(new RequestParameters(),
+        temporalCollection, null);
+
+    return services.getBulkDocuments(
+        requestLogger,
+        (transaction == null) ? null : transaction.getTransactionId(),
+        // the default for bulk is no metadata, which differs from the normal
+        // default of ALL
+        isProcessedMetadataModified ? processedMetadata : null,
+        nonDocumentFormat,
+        mergeTransformParameters((transform != null) ? transform
+            : getReadTransform(), extraParams), withContent, uris);
+  }
+
+  public DocumentPage readMetadata(String... uris) {
+    boolean withContent = false;
+    return read(null, null, withContent, null, uris);
+  }
+
+  public DocumentPage readMetadata(Transaction transaction, String... uris) {
+    boolean withContent = false;
+    return read(null, transaction, withContent, null, uris);
+  }
+
+  public DocumentPage search(QueryDefinition querydef, long start) {
+    return search(querydef, start, null, null);
+  }
+
+  public DocumentPage search(QueryDefinition querydef, long start,
+      SearchReadHandle searchHandle) {
+    return search(querydef, start, searchHandle, null);
+  }
+
+  public DocumentPage search(QueryDefinition querydef, long start,
+      Transaction transaction) {
+    return search(querydef, start, null, transaction);
+  }
+
+  public DocumentPage search(QueryDefinition querydef, long start,
+      SearchReadHandle searchHandle, Transaction transaction) {
+
+    if (searchHandle != null) {
+      HandleImplementation searchBase = HandleAccessor.checkHandle(
+          searchHandle, "search");
+      if (searchHandle instanceof SearchHandle) {
+        SearchHandle responseHandle = (SearchHandle) searchHandle;
+        responseHandle.setHandleRegistry(getHandleRegistry());
+        responseHandle.setQueryCriteria(querydef);
+      }
+      if (nonDocumentFormat != null
+          && nonDocumentFormat != searchBase.getFormat()) {
+        throw new UnsupportedOperationException(
+            "The format supported by your handle:[" + searchBase.getFormat()
+                + "] does not match the non-document format:["
+                + nonDocumentFormat + "]");
+      }
     }
-	
-	  public void write(String uri,
-	      DocumentMetadataWriteHandle metadataHandle,
-	      W contentHandle,
-	      ServerTransform transform,
-	      Transaction transaction,
-	      String temporalCollection,
-		RequestParameters extraParams)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException
-	  {
-		write(new DocumentDescriptorImpl(uri, true), metadataHandle, contentHandle, transform, 
-			transaction, temporalCollection, null, extraParams);
-	  }
-
-		@Override
-    public void write(String uri,
-        DocumentMetadataWriteHandle metadataHandle,
-        W contentHandle,
-        ServerTransform transform,
-        Transaction transaction,
-        String temporalCollection,
-        Calendar systemTime,
-		RequestParameters extraParams)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException
-    {
-		write(new DocumentDescriptorImpl(uri, true), metadataHandle, contentHandle, transform, 
-			transaction, temporalCollection, systemTime, extraParams);
+
+    String tid = transaction == null ? null : transaction.getTransactionId();
+    // the default for bulk is no metadata, which differs from the normal
+    // default of ALL
+    Set metadata = isProcessedMetadataModified ? processedMetadata
+        : null;
+    return services.getBulkDocuments(requestLogger, querydef, start,
+        getPageLength(), tid, searchHandle, searchView, metadata,
+        nonDocumentFormat, null);
+  }
+
+  public long getPageLength() {
+    return pageLength;
+  }
+
+  public void setPageLength(long length) {
+    this.pageLength = length;
+  }
+
+  public QueryView getSearchView() {
+    return searchView;
+  }
+
+  public void setSearchView(QueryView view) {
+    this.searchView = view;
+  }
+
+  public Format getNonDocumentFormat() {
+    return nonDocumentFormat;
+  }
+
+  public void setNonDocumentFormat(Format nonDocumentFormat) {
+    if (nonDocumentFormat != Format.XML && nonDocumentFormat != Format.JSON) {
+      throw new UnsupportedOperationException(
+          "Only XML and JSON are valid response formats.  You specified:["
+              + nonDocumentFormat + "]");
     }
- 
-	@Override
-	public void write(DocumentDescriptor desc, W contentHandle)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(desc, null, contentHandle, null, null, null, null);
-	}
-	@Override
-	public void write(DocumentDescriptor desc, W contentHandle, ServerTransform transform)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(desc, null, contentHandle, transform, null, null, null);
-	}
-	@Override
-	public void write(DocumentDescriptor desc, DocumentMetadataWriteHandle metadata, W contentHandle)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(desc, metadata, contentHandle, null, null, null, null);
-	}
-	@Override
-	public void write(DocumentDescriptor desc, DocumentMetadataWriteHandle metadata, W contentHandle, ServerTransform transform)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(desc, metadata, contentHandle, transform, null, null, null);
-	}
-	@Override
-	public void write(DocumentDescriptor desc, W contentHandle, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(desc, null, contentHandle, null, transaction, null, null);
-	}
-	@Override
-	public void write(DocumentDescriptor desc, W contentHandle, ServerTransform transform, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(desc, null, contentHandle, transform, transaction, null, null);
-	}
-	@Override
-	public void write(DocumentDescriptor desc, DocumentMetadataWriteHandle metadataHandle, W contentHandle, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(desc, metadataHandle, contentHandle, null, transaction, null, null);
-	}
-	@Override
-	public void write(DocumentDescriptor desc, DocumentMetadataWriteHandle metadataHandle, W contentHandle, ServerTransform transform, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException {
-		write(desc, metadataHandle, contentHandle, transform, transaction, null, null);
-	}
-
-	@Override
-    public void write(DocumentDescriptor desc,
-        DocumentMetadataWriteHandle metadataHandle,
-        W contentHandle,
-        ServerTransform transform,
-        Transaction transaction,
-        String temporalCollection)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException
-    {
-		write(desc, metadataHandle, contentHandle, transform, transaction, temporalCollection, 
-			null, getWriteParams());
-	}
-	
-	public void write(DocumentDescriptor desc,
-        DocumentMetadataWriteHandle metadataHandle,
-        W contentHandle,
-        ServerTransform transform,
-        Transaction transaction,
-        String temporalCollection,
-		RequestParameters extraParams) 
-				throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException
-			{
-		write(desc, metadataHandle, contentHandle, transform, transaction, temporalCollection, null, extraParams);
-	}
-	
-	@SuppressWarnings("rawtypes")
-	@Override
-    public void write(DocumentDescriptor desc,
-        DocumentMetadataWriteHandle metadataHandle,
-        W contentHandle,
-        ServerTransform transform,
-        Transaction transaction,
-        String temporalCollection,
-        java.util.Calendar systemTime,
-		RequestParameters extraParams)
-	throws ResourceNotFoundException, ForbiddenUserException,  FailedRequestException
-    {
-		if (desc == null)
-			throw new IllegalArgumentException("Writing document with null identifier");
-
-		if (logger.isInfoEnabled())
-			logger.info("Writing content for {}",desc.getUri());
-
-		if (metadataHandle != null) {
-			HandleImplementation metadataBase = HandleAccessor.checkHandle(metadataHandle, "metadata");
-			Format metadataFormat = metadataBase.getFormat();
-			if (metadataFormat == null || (metadataFormat != Format.JSON && metadataFormat != Format.XML)) {
-				if (logger.isWarnEnabled())
-					logger.warn("Unsupported metadata format {}, using XML",metadataFormat.name());
-				metadataBase.setFormat(Format.XML);
-			}
-		}
-
-		checkContentFormat(contentHandle);
-
-		extraParams = addTemporalParams(extraParams, temporalCollection, systemTime);
-
-		services.putDocument(
-				requestLogger,
-				desc,
-				(transaction == null) ? null : transaction.getTransactionId(),
-				(metadataHandle != null) ? processedMetadata : null,
-				mergeTransformParameters(
-						(transform != null) ? transform : getWriteTransform(),
-						extraParams
-						),
-				metadataHandle,
-				contentHandle
-				);
-	}
-
-	@Override
-	public void delete(String uri) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		delete(uri, null);
-	}
-	@Override
-	public void delete(String uri, Transaction transaction) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		delete(new DocumentDescriptorImpl(uri, true), transaction);
-	}
-	@Override
-    public void delete(DocumentDescriptor desc) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		delete(desc, null, null);
+    this.nonDocumentFormat = nonDocumentFormat;
+  }
+
+  public DocumentWriteSet newWriteSet() {
+    return new DocumentWriteSetImpl();
+  }
+
+  public void write(DocumentWriteSet writeSet) {
+    write(writeSet, null, null);
+  }
+
+  public void write(DocumentWriteSet writeSet, ServerTransform transform) {
+    write(writeSet, transform, null);
+  }
+
+  public void write(DocumentWriteSet writeSet, Transaction transaction) {
+    write(writeSet, null, transaction);
+  }
+
+  public void write(DocumentWriteSet writeSet, ServerTransform transform,
+      Transaction transaction) {
+    Format defaultFormat = contentFormat;
+    services.postBulkDocuments(requestLogger, writeSet,
+        (transform != null) ? transform : getWriteTransform(),
+        (transaction == null) ? null : transaction.getTransactionId(),
+        defaultFormat, null);
+  }
+
+  // shortcut writers
+  @Override
+  public void writeAs(String uri, Object content)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    writeAs(uri, null, content, null);
+  }
+
+  @Override
+  public void writeAs(String uri, Object content, ServerTransform transform)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    writeAs(uri, null, content, transform);
+  }
+
+  @Override
+  public void writeAs(String uri, DocumentMetadataWriteHandle metadataHandle,
+      Object content) throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    writeAs(uri, metadataHandle, content, null);
+  }
+
+  @Override
+  public void writeAs(String uri, DocumentMetadataWriteHandle metadataHandle,
+      Object content, ServerTransform transform)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    if (content == null) {
+      throw new IllegalArgumentException("no content to write");
     }
-	@Override
-    public void delete(DocumentDescriptor desc, Transaction transaction)
-	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		delete(desc, transaction, null);
-	}
-
-	@Override
-	public void delete(String uri,
-			Transaction transaction,
-			String temporalCollection)
-	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException
-	{
-		delete(new DocumentDescriptorImpl(uri, true), transaction, temporalCollection);
-	}
-	@Override
-	public void delete(String uri,
-			Transaction transaction,
-			String temporalCollection,
-			java.util.Calendar systemTime)
-	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException
-	{
-		delete(new DocumentDescriptorImpl(uri, true), transaction, temporalCollection, systemTime);
-	}
-	@Override
-	public void delete(DocumentDescriptor desc,
-			Transaction transaction,
-			String temporalCollection)
-	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		delete(desc, transaction, temporalCollection, null);
-	}
-	@Override
-	public void delete(DocumentDescriptor desc,
-			Transaction transaction,
-			String temporalCollection,
-			java.util.Calendar systemTime)
-	throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException
-	{
-		if (desc == null)
-			throw new IllegalArgumentException("Deleting document with null identifier");
-
-		if (logger.isInfoEnabled())
-			logger.info("Deleting {}",desc.getUri());
-
-		RequestParameters extraParams = addTemporalParams(new RequestParameters(), temporalCollection, systemTime);
-
-		services.deleteDocument(requestLogger, desc, (transaction == null) ? null : transaction.getTransactionId(), 
-			null, extraParams);
+
+    Class as = content.getClass();
+
+    W writeHandle = null;
+    if (AbstractWriteHandle.class.isAssignableFrom(as)) {
+      AbstractWriteHandle handle = (AbstractWriteHandle) content;
+      writeHandle = castAbstractWriteHandle(null, handle);
+    } else {
+      ContentHandle handle = getHandleRegistry().makeHandle(as);
+      Utilities.setHandleContent(handle, content);
+      writeHandle = castAbstractWriteHandle(as, handle);
     }
 
-	// shortcut creators
-	@Override
-    public DocumentDescriptor createAs(DocumentUriTemplate template, Object content)
-	throws ForbiddenUserException, FailedRequestException {
-		return createAs(template, null, content, null);
+    write(uri, metadataHandle, writeHandle, transform);
+  }
+
+  W castAbstractWriteHandle(Class as, AbstractWriteHandle handle) {
+    try {
+      @SuppressWarnings("unchecked")
+      W writeHandle = (W) handle;
+      return writeHandle;
+    } catch (ClassCastException e) {
+      if (as == null) {
+        throw new IllegalArgumentException("Handle "
+            + handle.getClass().getName()
+            + " cannot be used in the context for writing");
+      }
+      throw new IllegalArgumentException("Handle "
+          + handle.getClass().getName()
+          + " cannot be used in the context to write " + as.getName());
     }
-	@Override
-    public DocumentDescriptor createAs(DocumentUriTemplate template, Object content, ServerTransform transform)
-	throws ForbiddenUserException, FailedRequestException {
-		return createAs(template, null, content, transform);
+  }
+
+  // strongly typed writers
+  @Override
+  public void write(String uri, W contentHandle)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    write(uri, null, contentHandle, null, null, null);
+  }
+
+  @Override
+  public void write(String uri, W contentHandle, ServerTransform transform)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    write(uri, null, contentHandle, transform, null, null);
+  }
+
+  @Override
+  public void write(String uri, DocumentMetadataWriteHandle metadata,
+      W contentHandle) throws ResourceNotFoundException,
+      ForbiddenUserException, FailedRequestException {
+    write(uri, metadata, contentHandle, null, null, null);
+  }
+
+  @Override
+  public void write(String uri, DocumentMetadataWriteHandle metadata,
+      W contentHandle, ServerTransform transform)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    write(uri, metadata, contentHandle, transform, null, null);
+  }
+
+  @Override
+  public void write(String uri, W contentHandle, Transaction transaction)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    write(uri, null, contentHandle, null, transaction, null);
+  }
+
+  @Override
+  public void write(String uri, W contentHandle, ServerTransform transform,
+      Transaction transaction) throws ResourceNotFoundException,
+      ForbiddenUserException, FailedRequestException {
+    write(uri, null, contentHandle, transform, transaction, null);
+  }
+
+  @Override
+  public void write(String uri, DocumentMetadataWriteHandle metadataHandle,
+      W contentHandle, Transaction transaction)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    write(uri, metadataHandle, contentHandle, null, transaction, null);
+  }
+
+  @Override
+  public void write(String uri, DocumentMetadataWriteHandle metadataHandle,
+      W contentHandle, ServerTransform transform, Transaction transaction)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    write(uri, metadataHandle, contentHandle, transform, transaction, null);
+  }
+
+  @Override
+  public void write(String uri, DocumentMetadataWriteHandle metadataHandle,
+      W contentHandle, ServerTransform transform, Transaction transaction,
+      String temporalCollection) throws ResourceNotFoundException,
+      ForbiddenUserException, FailedRequestException {
+    write(uri, metadataHandle, contentHandle, transform, transaction,
+        temporalCollection, null, getWriteParams());
+  }
+
+  public void write(String uri, DocumentMetadataWriteHandle metadataHandle,
+      W contentHandle, ServerTransform transform, Transaction transaction,
+      String temporalCollection, RequestParameters extraParams)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    write(new DocumentDescriptorImpl(uri, true), metadataHandle, contentHandle,
+        transform, transaction, temporalCollection, null, extraParams);
+  }
+
+  @Override
+  public void write(String uri, DocumentMetadataWriteHandle metadataHandle,
+      W contentHandle, ServerTransform transform, Transaction transaction,
+      String temporalCollection, Calendar systemTime,
+      RequestParameters extraParams) throws ResourceNotFoundException,
+      ForbiddenUserException, FailedRequestException {
+    write(new DocumentDescriptorImpl(uri, true), metadataHandle, contentHandle,
+        transform, transaction, temporalCollection, systemTime, extraParams);
+  }
+
+  @Override
+  public void write(DocumentDescriptor desc, W contentHandle)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    write(desc, null, contentHandle, null, null, null, null);
+  }
+
+  @Override
+  public void write(DocumentDescriptor desc, W contentHandle,
+      ServerTransform transform) throws ResourceNotFoundException,
+      ForbiddenUserException, FailedRequestException {
+    write(desc, null, contentHandle, transform, null, null, null);
+  }
+
+  @Override
+  public void write(DocumentDescriptor desc,
+      DocumentMetadataWriteHandle metadata, W contentHandle)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    write(desc, metadata, contentHandle, null, null, null, null);
+  }
+
+  @Override
+  public void write(DocumentDescriptor desc,
+      DocumentMetadataWriteHandle metadata, W contentHandle,
+      ServerTransform transform) throws ResourceNotFoundException,
+      ForbiddenUserException, FailedRequestException {
+    write(desc, metadata, contentHandle, transform, null, null, null);
+  }
+
+  @Override
+  public void write(DocumentDescriptor desc, W contentHandle,
+      Transaction transaction) throws ResourceNotFoundException,
+      ForbiddenUserException, FailedRequestException {
+    write(desc, null, contentHandle, null, transaction, null, null);
+  }
+
+  @Override
+  public void write(DocumentDescriptor desc, W contentHandle,
+      ServerTransform transform, Transaction transaction)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    write(desc, null, contentHandle, transform, transaction, null, null);
+  }
+
+  @Override
+  public void write(DocumentDescriptor desc,
+      DocumentMetadataWriteHandle metadataHandle, W contentHandle,
+      Transaction transaction) throws ResourceNotFoundException,
+      ForbiddenUserException, FailedRequestException {
+    write(desc, metadataHandle, contentHandle, null, transaction, null, null);
+  }
+
+  @Override
+  public void write(DocumentDescriptor desc,
+      DocumentMetadataWriteHandle metadataHandle, W contentHandle,
+      ServerTransform transform, Transaction transaction)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    write(desc, metadataHandle, contentHandle, transform, transaction, null,
+        null);
+  }
+
+  @Override
+  public void write(DocumentDescriptor desc,
+      DocumentMetadataWriteHandle metadataHandle, W contentHandle,
+      ServerTransform transform, Transaction transaction,
+      String temporalCollection) throws ResourceNotFoundException,
+      ForbiddenUserException, FailedRequestException {
+    write(desc, metadataHandle, contentHandle, transform, transaction,
+        temporalCollection, null, getWriteParams());
+  }
+
+  public void write(DocumentDescriptor desc,
+      DocumentMetadataWriteHandle metadataHandle, W contentHandle,
+      ServerTransform transform, Transaction transaction,
+      String temporalCollection, RequestParameters extraParams)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    write(desc, metadataHandle, contentHandle, transform, transaction,
+        temporalCollection, null, extraParams);
+  }
+
+  @SuppressWarnings("rawtypes")
+  @Override
+  public void write(DocumentDescriptor desc,
+      DocumentMetadataWriteHandle metadataHandle, W contentHandle,
+      ServerTransform transform, Transaction transaction,
+      String temporalCollection, java.util.Calendar systemTime,
+      RequestParameters extraParams) throws ResourceNotFoundException,
+      ForbiddenUserException, FailedRequestException {
+    if (desc == null)
+      throw new IllegalArgumentException(
+          "Writing document with null identifier");
+
+    if (logger.isInfoEnabled())
+      logger.info("Writing content for {}", desc.getUri());
+
+    if (metadataHandle != null) {
+      HandleImplementation metadataBase = HandleAccessor.checkHandle(
+          metadataHandle, "metadata");
+      Format metadataFormat = metadataBase.getFormat();
+      if (metadataFormat == null
+          || (metadataFormat != Format.JSON && metadataFormat != Format.XML)) {
+        if (logger.isWarnEnabled())
+          logger.warn("Unsupported metadata format {}, using XML",
+              metadataFormat.name());
+        metadataBase.setFormat(Format.XML);
+      }
     }
-	@Override
-    public DocumentDescriptor createAs(DocumentUriTemplate template, DocumentMetadataWriteHandle metadataHandle, Object content)
-	throws ForbiddenUserException, FailedRequestException {
-		return createAs(template, metadataHandle, content, null);
+
+    checkContentFormat(contentHandle);
+
+    extraParams = addTemporalParams(extraParams, temporalCollection, systemTime);
+
+    services.putDocument(
+        requestLogger,
+        desc,
+        (transaction == null) ? null : transaction.getTransactionId(),
+        (metadataHandle != null) ? processedMetadata : null,
+        mergeTransformParameters((transform != null) ? transform
+            : getWriteTransform(), extraParams), metadataHandle, contentHandle);
+  }
+
+  @Override
+  public void delete(String uri) throws ResourceNotFoundException,
+      ForbiddenUserException, FailedRequestException {
+    delete(uri, null);
+  }
+
+  @Override
+  public void delete(String uri, Transaction transaction)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    delete(new DocumentDescriptorImpl(uri, true), transaction);
+  }
+
+  @Override
+  public void delete(DocumentDescriptor desc) throws ResourceNotFoundException,
+      ForbiddenUserException, FailedRequestException {
+    delete(desc, null, null);
+  }
+
+  @Override
+  public void delete(DocumentDescriptor desc, Transaction transaction)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    delete(desc, transaction, null);
+  }
+
+  @Override
+  public void delete(String uri, Transaction transaction,
+      String temporalCollection) throws ResourceNotFoundException,
+      ForbiddenUserException, FailedRequestException {
+    delete(new DocumentDescriptorImpl(uri, true), transaction,
+        temporalCollection);
+  }
+
+  @Override
+  public void delete(String uri, Transaction transaction,
+      String temporalCollection, java.util.Calendar systemTime)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    delete(new DocumentDescriptorImpl(uri, true), transaction,
+        temporalCollection, systemTime);
+  }
+
+  @Override
+  public void delete(DocumentDescriptor desc, Transaction transaction,
+      String temporalCollection) throws ResourceNotFoundException,
+      ForbiddenUserException, FailedRequestException {
+    delete(desc, transaction, temporalCollection, null);
+  }
+
+  @Override
+  public void delete(DocumentDescriptor desc, Transaction transaction,
+      String temporalCollection, java.util.Calendar systemTime)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    if (desc == null)
+      throw new IllegalArgumentException(
+          "Deleting document with null identifier");
+
+    if (logger.isInfoEnabled())
+      logger.info("Deleting {}", desc.getUri());
+
+    RequestParameters extraParams = addTemporalParams(new RequestParameters(),
+        temporalCollection, systemTime);
+
+    services.deleteDocument(requestLogger, desc, (transaction == null) ? null
+        : transaction.getTransactionId(), null, extraParams);
+  }
+
+  // shortcut creators
+  @Override
+  public DocumentDescriptor createAs(DocumentUriTemplate template,
+      Object content) throws ForbiddenUserException, FailedRequestException {
+    return createAs(template, null, content, null);
+  }
+
+  @Override
+  public DocumentDescriptor createAs(DocumentUriTemplate template,
+      Object content, ServerTransform transform) throws ForbiddenUserException,
+      FailedRequestException {
+    return createAs(template, null, content, transform);
+  }
+
+  @Override
+  public DocumentDescriptor createAs(DocumentUriTemplate template,
+      DocumentMetadataWriteHandle metadataHandle, Object content)
+      throws ForbiddenUserException, FailedRequestException {
+    return createAs(template, metadataHandle, content, null);
+  }
+
+  @Override
+  public DocumentDescriptor createAs(DocumentUriTemplate template,
+      DocumentMetadataWriteHandle metadataHandle, Object content,
+      ServerTransform transform) throws ForbiddenUserException,
+      FailedRequestException {
+    Class as = content.getClass();
+    W writeHandle = null;
+    if (AbstractWriteHandle.class.isAssignableFrom(as)) {
+      AbstractWriteHandle handle = (AbstractWriteHandle) content;
+      writeHandle = castAbstractWriteHandle(null, handle);
+    } else {
+      ContentHandle handle = getHandleRegistry().makeHandle(as);
+      Utilities.setHandleContent(handle, content);
+      writeHandle = castAbstractWriteHandle(as, handle);
     }
-	@Override
-    public DocumentDescriptor createAs(DocumentUriTemplate template, DocumentMetadataWriteHandle metadataHandle, Object content, ServerTransform transform)
-	throws ForbiddenUserException, FailedRequestException {
-		Class as = content.getClass();
-		W writeHandle = null;
-		if (AbstractWriteHandle.class.isAssignableFrom(as)) {
-			AbstractWriteHandle handle = (AbstractWriteHandle) content;
-			writeHandle = castAbstractWriteHandle(null, handle);			
-		} else {
-			ContentHandle handle = getHandleRegistry().makeHandle(as);
-			Utilities.setHandleContent(handle, content);
-			writeHandle = castAbstractWriteHandle(as, handle);			
-		}
-		return create(template, metadataHandle, writeHandle, transform);
-	}
-
-	// strongly typed creators
-	@Override
-	public DocumentDescriptor create(DocumentUriTemplate template, W contentHandle)
-	throws ForbiddenUserException, FailedRequestException {
-		return create(template, null, contentHandle, null, null, null);
-	}
-	@Override
-	public DocumentDescriptor create(DocumentUriTemplate template, W contentHandle, 
-			ServerTransform transform)
-	throws ForbiddenUserException, FailedRequestException {
-		return create(template, null, contentHandle, transform, null, null);
-	}
-	@Override
-	public DocumentDescriptor create(DocumentUriTemplate template, W contentHandle,
-			Transaction transaction)
-	throws ForbiddenUserException, FailedRequestException {
-		return create(template, null, contentHandle, null, transaction, null);
-	}
-	@Override
-	public DocumentDescriptor create(DocumentUriTemplate template, W contentHandle,
-			ServerTransform transform, Transaction transaction)
-	throws ForbiddenUserException, FailedRequestException {
-		return create(template, null, contentHandle, transform, transaction, null);
-	}
-	@Override
-	public DocumentDescriptor create(DocumentUriTemplate template, DocumentMetadataWriteHandle metadataHandle,
-			W contentHandle)
-	throws ForbiddenUserException, FailedRequestException {
-		return create(template, metadataHandle, contentHandle, null, null, null);
-	}
-	@Override
-	public DocumentDescriptor create(DocumentUriTemplate template, DocumentMetadataWriteHandle metadataHandle,
-			W contentHandle, ServerTransform transform)
-	throws ForbiddenUserException, FailedRequestException {
-		return create(template, metadataHandle, contentHandle, transform, null, null);
-	}
-	@Override
-	public DocumentDescriptor create(DocumentUriTemplate template, DocumentMetadataWriteHandle metadataHandle,
-			W contentHandle, Transaction transaction)
-	throws ForbiddenUserException, FailedRequestException {
-		return create(template, metadataHandle, contentHandle, null, transaction, null);
-	}
-	@Override
-	public DocumentDescriptor create(DocumentUriTemplate template, DocumentMetadataWriteHandle metadataHandle,
-			W contentHandle, ServerTransform transform, Transaction transaction)
-	throws ForbiddenUserException, FailedRequestException {
-		return create(template, metadataHandle, contentHandle, transform, transaction, null);
-	}
-	@Override
+    return create(template, metadataHandle, writeHandle, transform);
+  }
+
+  // strongly typed creators
+  @Override
+  public DocumentDescriptor create(DocumentUriTemplate template, W contentHandle)
+      throws ForbiddenUserException, FailedRequestException {
+    return create(template, null, contentHandle, null, null, null);
+  }
+
+  @Override
   public DocumentDescriptor create(DocumentUriTemplate template,
-      DocumentMetadataWriteHandle metadataHandle,
-      W contentHandle,
-      ServerTransform transform,
-      Transaction transaction,
-      String temporalCollection)
-throws ForbiddenUserException, FailedRequestException
-  {
-	return create(template, metadataHandle, contentHandle, transform, transaction, 
-		temporalCollection, null, getWriteParams());
+      W contentHandle, ServerTransform transform)
+      throws ForbiddenUserException, FailedRequestException {
+    return create(template, null, contentHandle, transform, null, null);
   }
-	
+
+  @Override
   public DocumentDescriptor create(DocumentUriTemplate template,
-      DocumentMetadataWriteHandle metadataHandle,
-      W contentHandle,
-      ServerTransform transform,
-      Transaction transaction,
-      String temporalCollection,
-	RequestParameters extraParams) {
-		return create(template, metadataHandle, contentHandle, transform, transaction, temporalCollection, null, extraParams);
-	}
-	@SuppressWarnings("rawtypes")
-    public DocumentDescriptor create(DocumentUriTemplate template,
-        DocumentMetadataWriteHandle metadataHandle,
-        W contentHandle,
-        ServerTransform transform,
-        Transaction transaction,
-        String temporalCollection,
-        java.util.Calendar systemTime,
-		RequestParameters extraParams)
-    {
-		if (logger.isInfoEnabled())
-			logger.info("Creating content");
-
-		if (metadataHandle != null) {
-			HandleImplementation metadataBase = HandleAccessor.checkHandle(metadataHandle, "metadata");
-			Format metadataFormat = metadataBase.getFormat();
-			if (metadataFormat == null || (metadataFormat != Format.JSON && metadataFormat != Format.XML)) {
-				if (logger.isWarnEnabled())
-					logger.warn("Unsupported metadata format {}, using XML",metadataFormat.name());
-				metadataBase.setFormat(Format.XML);
-			}
-		}
-
-		extraParams = addTemporalParams(extraParams, temporalCollection, systemTime);
-
-		checkContentFormat(contentHandle);
-
-		return services.postDocument(
-				requestLogger,
-				template,
-				(transaction == null) ? null : transaction.getTransactionId(),
-				(metadataHandle != null) ? processedMetadata : null,
-				mergeTransformParameters(
-						(transform != null) ? transform : getWriteTransform(),
-						extraParams
-						),
-				metadataHandle,
-				contentHandle
-				);
-	}
-
-	@Override
-	public void patchAs(String uri, Object patch)
-	throws ForbiddenUserException, FailedRequestException {
-		if (patch == null) {
-			throw new IllegalArgumentException("no patch to apply");
-		}
-
-		Class as = patch.getClass();
-
-		DocumentPatchHandle patchHandle = null;
-		if (DocumentPatchHandle.class.isAssignableFrom(as)) {
-			patchHandle = (DocumentPatchHandle) patch;
-		} else {
-			ContentHandle handle = getHandleRegistry().makeHandle(as);
-			if (!DocumentPatchHandle.class.isAssignableFrom(handle.getClass())) {
-				throw new IllegalArgumentException(
-						"Handle "+handle.getClass().getName()+
-						" cannot be used to apply patch as "+as.getName()
-						);
-			}
-			Utilities.setHandleContent(handle, patch);
-			patchHandle = (DocumentPatchHandle) handle;
-		}
-
-		patch(uri, patchHandle);
-	}
-
-	@Override
-	public void patch(String uri, DocumentPatchHandle patch)
-	throws ForbiddenUserException, FailedRequestException {
-		patch(uri, patch, null);
-	}
-	@Override
-	public void patch(String uri, DocumentPatchHandle patch, Transaction transaction)
-	throws ForbiddenUserException, FailedRequestException {
-		patch(new DocumentDescriptorImpl(uri, true), patch, transaction);
-	}
-	@Override
-	public void patch(DocumentDescriptor desc, DocumentPatchHandle patch)
-	throws ForbiddenUserException, FailedRequestException {
-		patch(desc, patch, null);
-	}
-	@Override
-	public void patch(DocumentDescriptor desc, DocumentPatchHandle patch, Transaction transaction)
-	throws ForbiddenUserException, FailedRequestException {
-		if (logger.isInfoEnabled())
-			logger.info("Patching document");
-
-		DocumentPatchHandleImpl builtPatch = 
-			(patch instanceof DocumentPatchHandleImpl) ?
-			(DocumentPatchHandleImpl) patch : null;
-		services.patchDocument(
-				requestLogger,
-				desc,
-				(transaction == null) ? null : transaction.getTransactionId(),
-				(builtPatch != null) ?
-						builtPatch.getMetadata() : processedMetadata,
-				(builtPatch != null) ?
-						builtPatch.isOnContent() : true,
-				patch
-				);
-	}
-
-	@Override
-    public  T readMetadata(String uri, T metadataHandle) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		return readMetadata(uri, metadataHandle, null);
-    }
-	@Override
-    public  T readMetadata(String uri, T metadataHandle, Transaction transaction) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		read(uri, metadataHandle, null, transaction);
+      W contentHandle, Transaction transaction) throws ForbiddenUserException,
+      FailedRequestException {
+    return create(template, null, contentHandle, null, transaction, null);
+  }
 
-		return metadataHandle;
-    }
+  @Override
+  public DocumentDescriptor create(DocumentUriTemplate template,
+      W contentHandle, ServerTransform transform, Transaction transaction)
+      throws ForbiddenUserException, FailedRequestException {
+    return create(template, null, contentHandle, transform, transaction, null);
+  }
 
-	@Override
-    public void writeMetadata(String uri, DocumentMetadataWriteHandle metadataHandle) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		writeMetadata(uri, metadataHandle, null);
-    }
-	@Override
-    public void writeMetadata(String uri, DocumentMetadataWriteHandle metadataHandle, Transaction transaction) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		write(uri, metadataHandle, (W) null, transaction);
-    }
+  @Override
+  public DocumentDescriptor create(DocumentUriTemplate template,
+      DocumentMetadataWriteHandle metadataHandle, W contentHandle)
+      throws ForbiddenUserException, FailedRequestException {
+    return create(template, metadataHandle, contentHandle, null, null, null);
+  }
 
-	@Override
-    public void writeDefaultMetadata(String uri) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		writeDefaultMetadata(uri, null);
-    }
-	@Override
-    public void writeDefaultMetadata(String uri, Transaction transaction) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
-		if (uri == null)
-			throw new IllegalArgumentException("Resetting document metadata with null identifier");
+  @Override
+  public DocumentDescriptor create(DocumentUriTemplate template,
+      DocumentMetadataWriteHandle metadataHandle, W contentHandle,
+      ServerTransform transform) throws ForbiddenUserException,
+      FailedRequestException {
+    return create(template, metadataHandle, contentHandle, transform, null,
+        null);
+  }
 
-		if (logger.isInfoEnabled())
-			logger.info("Resetting metadata for {}",uri);
+  @Override
+  public DocumentDescriptor create(DocumentUriTemplate template,
+      DocumentMetadataWriteHandle metadataHandle, W contentHandle,
+      Transaction transaction) throws ForbiddenUserException,
+      FailedRequestException {
+    return create(template, metadataHandle, contentHandle, null, transaction,
+        null);
+  }
 
-		services.deleteDocument(requestLogger, new DocumentDescriptorImpl(uri, true), 
-				(transaction == null) ? null : transaction.getTransactionId(), processedMetadata, getWriteParams());
-    }
+  @Override
+  public DocumentDescriptor create(DocumentUriTemplate template,
+      DocumentMetadataWriteHandle metadataHandle, W contentHandle,
+      ServerTransform transform, Transaction transaction)
+      throws ForbiddenUserException, FailedRequestException {
+    return create(template, metadataHandle, contentHandle, transform,
+        transaction, null);
+  }
 
-	@Override
-    public ServerTransform getReadTransform() {
-    	return readTransform;
-    }
-	@Override
-    public void setReadTransform(ServerTransform transform) {
-    	this.readTransform = transform;
-    }
+  @Override
+  public DocumentDescriptor create(DocumentUriTemplate template,
+      DocumentMetadataWriteHandle metadataHandle, W contentHandle,
+      ServerTransform transform, Transaction transaction,
+      String temporalCollection) throws ForbiddenUserException,
+      FailedRequestException {
+    return create(template, metadataHandle, contentHandle, transform,
+        transaction, temporalCollection, null, getWriteParams());
+  }
 
-	@Override
-    public ServerTransform getWriteTransform() {
-    	return writeTransform;
-    }
-	@Override
-    public void setWriteTransform(ServerTransform transform) {
-    	this.writeTransform = transform;
+  public DocumentDescriptor create(DocumentUriTemplate template,
+      DocumentMetadataWriteHandle metadataHandle, W contentHandle,
+      ServerTransform transform, Transaction transaction,
+      String temporalCollection, RequestParameters extraParams) {
+    return create(template, metadataHandle, contentHandle, transform,
+        transaction, temporalCollection, null, extraParams);
+  }
+
+  @SuppressWarnings("rawtypes")
+  public DocumentDescriptor create(DocumentUriTemplate template,
+      DocumentMetadataWriteHandle metadataHandle, W contentHandle,
+      ServerTransform transform, Transaction transaction,
+      String temporalCollection, java.util.Calendar systemTime,
+      RequestParameters extraParams) {
+    if (logger.isInfoEnabled())
+      logger.info("Creating content");
+
+    if (metadataHandle != null) {
+      HandleImplementation metadataBase = HandleAccessor.checkHandle(
+          metadataHandle, "metadata");
+      Format metadataFormat = metadataBase.getFormat();
+      if (metadataFormat == null
+          || (metadataFormat != Format.JSON && metadataFormat != Format.XML)) {
+        if (logger.isWarnEnabled())
+          logger.warn("Unsupported metadata format {}, using XML",
+              metadataFormat.name());
+        metadataBase.setFormat(Format.XML);
+      }
     }
 
-	@Override
-    public String getForestName() {
-    	return forestName;
+    extraParams = addTemporalParams(extraParams, temporalCollection, systemTime);
+
+    checkContentFormat(contentHandle);
+
+    return services.postDocument(
+        requestLogger,
+        template,
+        (transaction == null) ? null : transaction.getTransactionId(),
+        (metadataHandle != null) ? processedMetadata : null,
+        mergeTransformParameters((transform != null) ? transform
+            : getWriteTransform(), extraParams), metadataHandle, contentHandle);
+  }
+
+  @Override
+  public void patchAs(String uri, Object patch) throws ForbiddenUserException,
+      FailedRequestException {
+    if (patch == null) {
+      throw new IllegalArgumentException("no patch to apply");
     }
-	@Override
-    public void setForestName(String forestName) {
-    	this.forestName = forestName;
+
+    Class as = patch.getClass();
+
+    DocumentPatchHandle patchHandle = null;
+    if (DocumentPatchHandle.class.isAssignableFrom(as)) {
+      patchHandle = (DocumentPatchHandle) patch;
+    } else {
+      ContentHandle handle = getHandleRegistry().makeHandle(as);
+      if (!DocumentPatchHandle.class.isAssignableFrom(handle.getClass())) {
+        throw new IllegalArgumentException("Handle "
+            + handle.getClass().getName()
+            + " cannot be used to apply patch as " + as.getName());
+      }
+      Utilities.setHandleContent(handle, patch);
+      patchHandle = (DocumentPatchHandle) handle;
     }
 
-	@Override
-	public DocumentDescriptor newDescriptor(String uri) {
-		return new DocumentDescriptorImpl(uri, false);
-	}
+    patch(uri, patchHandle);
+  }
+
+  @Override
+  public void patch(String uri, DocumentPatchHandle patch)
+      throws ForbiddenUserException, FailedRequestException {
+    patch(uri, patch, null);
+  }
 
-	@Override
-    public DocumentUriTemplate newDocumentUriTemplate(String extension) {
-		return new DocumentUriTemplateImpl(extension);
-	}
+  @Override
+  public void patch(String uri, DocumentPatchHandle patch,
+      Transaction transaction) throws ForbiddenUserException,
+      FailedRequestException {
+    patch(new DocumentDescriptorImpl(uri, true), patch, transaction);
+  }
 
-	@Override
-    public DocumentMetadataPatchBuilder newPatchBuilder(Format pathFormat) {
-    	return new DocumentMetadataPatchBuilderImpl(pathFormat);
+  @Override
+  public void patch(DocumentDescriptor desc, DocumentPatchHandle patch)
+      throws ForbiddenUserException, FailedRequestException {
+    patch(desc, patch, null);
+  }
+
+  @Override
+  public void patch(DocumentDescriptor desc, DocumentPatchHandle patch,
+      Transaction transaction) throws ForbiddenUserException,
+      FailedRequestException {
+    if (logger.isInfoEnabled())
+      logger.info("Patching document");
+
+    DocumentPatchHandleImpl builtPatch = (patch instanceof DocumentPatchHandleImpl) ? (DocumentPatchHandleImpl) patch
+        : null;
+    services.patchDocument(requestLogger, desc, (transaction == null) ? null
+        : transaction.getTransactionId(),
+        (builtPatch != null) ? builtPatch.getMetadata() : processedMetadata,
+        (builtPatch != null) ? builtPatch.isOnContent() : true, patch);
+  }
+
+  @Override
+  public  T readMetadata(String uri,
+      T metadataHandle) throws ResourceNotFoundException,
+      ForbiddenUserException, FailedRequestException {
+    return readMetadata(uri, metadataHandle, null);
+  }
+
+  @Override
+  public  T readMetadata(String uri,
+      T metadataHandle, Transaction transaction)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    read(uri, metadataHandle, null, transaction);
+
+    return metadataHandle;
+  }
+
+  @Override
+  public void writeMetadata(String uri,
+      DocumentMetadataWriteHandle metadataHandle)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    writeMetadata(uri, metadataHandle, null);
+  }
+
+  @Override
+  public void writeMetadata(String uri,
+      DocumentMetadataWriteHandle metadataHandle, Transaction transaction)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    write(uri, metadataHandle, (W) null, transaction);
+  }
+
+  @Override
+  public void writeDefaultMetadata(String uri)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    writeDefaultMetadata(uri, null);
+  }
+
+  @Override
+  public void writeDefaultMetadata(String uri, Transaction transaction)
+      throws ResourceNotFoundException, ForbiddenUserException,
+      FailedRequestException {
+    if (uri == null)
+      throw new IllegalArgumentException(
+          "Resetting document metadata with null identifier");
+
+    if (logger.isInfoEnabled())
+      logger.info("Resetting metadata for {}", uri);
+
+    services.deleteDocument(requestLogger,
+        new DocumentDescriptorImpl(uri, true), (transaction == null) ? null
+            : transaction.getTransactionId(), processedMetadata,
+        getWriteParams());
+  }
+
+  @Override
+  public ServerTransform getReadTransform() {
+    return readTransform;
+  }
+
+  @Override
+  public void setReadTransform(ServerTransform transform) {
+    this.readTransform = transform;
+  }
+
+  @Override
+  public ServerTransform getWriteTransform() {
+    return writeTransform;
+  }
+
+  @Override
+  public void setWriteTransform(ServerTransform transform) {
+    this.writeTransform = transform;
+  }
+
+  @Override
+  public String getForestName() {
+    return forestName;
+  }
+
+  @Override
+  public void setForestName(String forestName) {
+    this.forestName = forestName;
+  }
+
+  @Override
+  public DocumentDescriptor newDescriptor(String uri) {
+    return new DocumentDescriptorImpl(uri, false);
+  }
+
+  @Override
+  public DocumentUriTemplate newDocumentUriTemplate(String extension) {
+    return new DocumentUriTemplateImpl(extension);
+  }
+
+  @Override
+  public DocumentMetadataPatchBuilder newPatchBuilder(Format pathFormat) {
+    return new DocumentMetadataPatchBuilderImpl(pathFormat);
+  }
+
+  private void checkContentFormat(Object contentHandle) {
+    checkContentFormat(HandleAccessor.checkHandle(contentHandle, "content"));
+  }
+
+  @SuppressWarnings("rawtypes")
+  private void checkContentFormat(HandleImplementation contentBase) {
+    if (contentBase == null)
+      return;
+
+    if (contentFormat != null && contentFormat != Format.UNKNOWN) {
+      Format currFormat = contentBase.getFormat();
+      if (currFormat != contentFormat) {
+        contentBase.setFormat(contentFormat);
+        if (currFormat != Format.UNKNOWN)
+          contentBase.setMimetype(contentFormat.getDefaultMimetype());
+      }
     }
+  }
+
+  protected RequestParameters mergeTransformParameters(
+      ServerTransform transform, RequestParameters extraParams) {
+    if (transform == null)
+      return extraParams;
+
+    if (extraParams == null)
+      extraParams = new RequestParameters();
 
-	private void checkContentFormat(Object contentHandle) {
-		checkContentFormat(HandleAccessor.checkHandle(contentHandle, "content"));
-	}
-	@SuppressWarnings("rawtypes")
-	private void checkContentFormat(HandleImplementation contentBase) {
-		if (contentBase == null)
-			return;
-
-		if (contentFormat != null && contentFormat != Format.UNKNOWN) {
-			Format currFormat = contentBase.getFormat();
-			if (currFormat != contentFormat) {
-				contentBase.setFormat(contentFormat);
-				if (currFormat != Format.UNKNOWN)
-					contentBase.setMimetype(contentFormat.getDefaultMimetype());
-			}
-		}
-	}
-	protected RequestParameters mergeTransformParameters(ServerTransform transform, RequestParameters extraParams) {
-		if (transform == null)
-			return extraParams;
-
-		if (extraParams == null)
-			extraParams = new RequestParameters();
-
-		transform.merge(extraParams);
-
-		return extraParams;
-	}
-
-	// hooks for extension
-	protected RequestParameters getReadParams() {
-		return null;
-	}
-	protected RequestParameters getWriteParams() {
-		return null;
-	}
-
-	protected RequestParameters addTemporalParams(RequestParameters params, String temporalCollection, Calendar systemTime) {
-		if ( params == null ) params = new RequestParameters();
-		if ( temporalCollection != null ) params.add("temporal-collection", temporalCollection);
-		if ( systemTime != null ) {
-			String formattedSystemTime = DatatypeConverter.printDateTime(systemTime);
-			params.add("system-time", formattedSystemTime);
-		}
-		return params;
-	}
+    transform.merge(extraParams);
+
+    return extraParams;
+  }
+
+  // hooks for extension
+  protected RequestParameters getReadParams() {
+    return null;
+  }
+
+  protected RequestParameters getWriteParams() {
+    return null;
+  }
+
+  protected RequestParameters addTemporalParams(RequestParameters params,
+      String temporalCollection, Calendar systemTime) {
+    if (params == null)
+      params = new RequestParameters();
+    if (temporalCollection != null)
+      params.add("temporal-collection", temporalCollection);
+    if (systemTime != null) {
+      String formattedSystemTime = DatatypeConverter.printDateTime(systemTime);
+      params.add("system-time", formattedSystemTime);
+    }
+    return params;
+  }
 }

From ce1dec195ed61e01f9313c4cb892ebc8d44e5d34 Mon Sep 17 00:00:00 2001
From: Ajit George 
Date: Fri, 7 Nov 2014 17:11:41 -0800
Subject: [PATCH 306/357] Support for Uber

---
 .../javaclient/TestPartialUpdate.java         | 51 ++++++++++---------
 .../TestQueryOptionBuilderSortOrder.java      | 13 +++--
 2 files changed, 37 insertions(+), 27 deletions(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java
index 50e18d7b9..58c1c4b76 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java
@@ -36,6 +36,8 @@ public class TestPartialUpdate extends BasicJavaClientREST {
 	private static String [] fNames = {"TestPartialUpdateDB-1"};
 	private static String restServerName = "REST-Java-Client-API-Server";
 	private static int restPort=8011;
+	// Additional port to test for Uber port
+    private static int uberPort = 8000;
 
 	@BeforeClass
 	public  static void setUp() throws Exception 
@@ -44,6 +46,9 @@ public  static void setUp() throws Exception
 		System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug");
 		setupJavaRESTServer(dbName, fNames[0], restServerName,8011);
 		setupAppServicesConstraint(dbName);
+		
+		createUserRolesWithPrevilages("test-eval","xdbc:eval", "xdbc:eval-in","xdmp:eval-in","any-uri","xdbc:invoke");
+	    createRESTUser("eval-user", "x", "test-eval","rest-admin","rest-writer","rest-reader");
 	}
 
 	@After
@@ -60,7 +65,7 @@ public void testPartialUpdateXML() throws IOException
 
 		String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"};
 
-		DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
+		DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
 
 		// write docs
 		for(String filename : filenames)
@@ -93,7 +98,7 @@ public void testPartialUpdateJSON() throws IOException
 
 		String[] filenames = {"json-original.json"};
 
-		DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
+		DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
 
 		// write docs
 		for(String filename : filenames)
@@ -133,7 +138,7 @@ public void testPartialUpdateContent() throws IOException
 
 		String filename = "constraint1.xml";
 
-		DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
+		DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
 
 		// write docs
 		writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML");
@@ -223,7 +228,7 @@ public void testPartialUpdateDeletePath() throws IOException
 	{	
 		System.out.println("Running testPartialUpdateDeletePath");
 
-		DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
+		DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
 
 		// write docs
 		String filename = "constraint1.xml";
@@ -263,7 +268,7 @@ public void testPartialUpdateDeletePath() throws IOException
 	@Test	
 	public void testPartialUpdateFragments() throws Exception{
 		System.out.println("Running testPartialUpdateFragments");
-		DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
+		DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
 
 		// write docs
 		String filename = "constraint1.xml";
@@ -298,7 +303,7 @@ public void testPartialUpdateFragments() throws Exception{
 	@Test	
 	public void testPartialUpdateInsertFragments() throws Exception{
 		System.out.println("Running testPartialUpdateInsertFragments");
-		DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
+		DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
 
 		// write docs
 		String filename = "constraint1.xml";
@@ -331,7 +336,7 @@ public void testPartialUpdateInsertFragments() throws Exception{
 	@Test	
 	public void testPartialUpdateInsertExistingFragments() throws Exception{
 		System.out.println("Running testPartialUpdateInsertExistingFragments");
-		DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
+		DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
 
 		// write docs
 		String filename = "constraint1.xml";
@@ -364,7 +369,7 @@ public void testPartialUpdateInsertExistingFragments() throws Exception{
 	@Test	
 	public void testPartialUpdateReplaceApply() throws Exception{
 		System.out.println("Running testPartialUpdateReplaceApply");
-		DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST);
+		DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8000, "rest-admin", "x", Authentication.DIGEST);
 		ExtensionLibrariesManager libsMgr =  client.newServerConfigManager().newExtensionLibrariesManager();
 
 		libsMgr.write("/ext/patch/custom-lib.xqy", new FileHandle(new File("src/test/java/com/marklogic/javaclient/data/custom-lib.xqy")).withFormat(Format.TEXT));
@@ -417,7 +422,7 @@ public void testPartialUpdateReplaceApply() throws Exception{
 	@Test	 
 	public void testPartialUpdateCombination() throws Exception{
 		System.out.println("Running testPartialUpdateCombination");
-		DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
+		DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
 
 		// write docs
 		String filename = "constraint1.xml";
@@ -446,7 +451,7 @@ public void testPartialUpdateCombination() throws Exception{
 	@Test	
 	public void testPartialUpdateCombinationTransc() throws Exception{
 		System.out.println("Running testPartialUpdateCombination");
-		DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
+		DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
 		Transaction t = client.openTransaction("Transac");
 		// write docs
 		String filename = "constraint1.xml";
@@ -481,7 +486,7 @@ public void testPartialUpdateCombinationTransc() throws Exception{
 	@Test	
 	public void testPartialUpdateCombinationTranscRevert() throws Exception{
 		System.out.println("Running testPartialUpdateCombinationTranscRevert");
-		DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
+		DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
 		// write docs
 		String[] filenames = {"constraint1.xml", "constraint2.xml"};
 		for(String filename : filenames)
@@ -524,7 +529,7 @@ public void testPartialUpdateCombinationTranscRevert() throws Exception{
 	@Test	
 	public void testPartialUpdateCombinationJSON() throws Exception{
 		System.out.println("Running testPartialUpdateCombinationJSON");
-		DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
+		DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
 
 		// write docs
 		String[] filenames = {"json-original.json"};
@@ -566,7 +571,7 @@ public void testPartialUpdateCombinationJSON() throws Exception{
 	@Test	
 	public void testPartialUpdateMetadata() throws Exception{
 		System.out.println("Running testPartialUpdateMetadata");
-		DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
+		DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
 
 		// write docs
 		String filename = "constraint1.xml";
@@ -638,7 +643,7 @@ public void testPartialUpdateXMLDscriptor() throws IOException
 
 		String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"};
 
-		DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
+		DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
 
 		// write docs
 		for(String filename : filenames)
@@ -675,7 +680,7 @@ public void testPartialUpdateJSONDescriptor() throws IOException
 
 		String[] filenames = {"json-original.json"};
 
-		DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
+		DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
 
 		// write docs
 		for(String filename : filenames)
@@ -722,7 +727,7 @@ public void testPartialUpdateXMLDscriptorTranc() throws IOException
 
 		String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"};
 
-		DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
+		DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
 
 		// write docs
 		for(String filename : filenames)
@@ -761,7 +766,7 @@ public void testPartialUpdateJSONDescriptorTranc() throws IOException
 
 		String[] filenames = {"json-original.json"};
 
-		DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
+		DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
 
 		// write docs
 		for(String filename : filenames)
@@ -806,7 +811,7 @@ public void testPartialUpdateCardinality() throws IOException
 
 		String filename = "constraint1.xml";
 
-		DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
+		DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
 
 		// write docs
 		writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "XML");
@@ -859,7 +864,7 @@ public void testPartialUpdateReplaceValueJSON() throws IOException, JSONExceptio
 
 		String[] filenames = {"json-original.json"};
 
-		DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
+		DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
 
 		// write docs
 		for(String filename : filenames)
@@ -903,7 +908,7 @@ public void testPartialUpdateReplaceFragmentJSON() throws IOException, JSONExcep
 
 		String[] filenames = {"json-original.json"};
 
-		DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
+		DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
 
 		// write docs
 		for(String filename : filenames)
@@ -947,7 +952,7 @@ public void testPartialUpdateReplaceInsertFragmentNewJSON() throws IOException,
 
 		String[] filenames = {"json-original.json"};
 
-		DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
+		DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
 
 		// write docs
 		for(String filename : filenames)
@@ -992,7 +997,7 @@ public void testPartialUpdateReplaceInsertFragmentExistingJSON() throws IOExcept
 
 		String[] filenames = {"json-original.json"};
 
-		DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
+		DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
 
 		// write docs
 		for(String filename : filenames)
@@ -1036,7 +1041,7 @@ public void testPartialUpdateDeleteJSON() throws IOException, JSONException
 
 		String[] filenames = {"json-original.json"};
 
-		DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
+		DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
 
 		// write docs
 		for(String filename : filenames)
diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderSortOrder.java b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderSortOrder.java
index 46452836e..0482aca3f 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderSortOrder.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestQueryOptionBuilderSortOrder.java
@@ -40,6 +40,8 @@ public class TestQueryOptionBuilderSortOrder extends BasicJavaClientREST {
 	private static String [] fNames = {"TestQueryOptionBuilderSortOrderDB-1"};
 	private static String restServerName = "REST-Java-Client-API-Server";
 	private static int restPort = 8011;
+	// Additional port to test for Uber port
+	private static int uberPort = 8000;
 
 	@BeforeClass	
 	public static void setUp() throws Exception 
@@ -47,6 +49,9 @@ public static void setUp() throws Exception
 		System.out.println("In setup");
 		setupJavaRESTServer(dbName, fNames[0], restServerName,8011);
 		setupAppServicesConstraint(dbName);
+		
+		createUserRolesWithPrevilages("test-eval","xdbc:eval", "xdbc:eval-in","xdmp:eval-in","any-uri","xdbc:invoke");
+	    createRESTUser("eval-user", "x", "test-eval","rest-admin","rest-writer","rest-reader");
 	}
 
 	@After
@@ -63,7 +68,7 @@ public void testSortOrderDescendingScore() throws FileNotFoundException, XpathEx
 
 		String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"};
 
-		DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST);
+		DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName,"eval-user", "x", Authentication.DIGEST);
 
 		// write docs
 		for(String filename : filenames)
@@ -129,7 +134,7 @@ public void testSortOrderPrimaryDescScoreSecondaryAscDate() throws FileNotFoundE
 
 		String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"};
 
-		DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST);
+		DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
 
 		// write docs
 		for(String filename : filenames)
@@ -198,7 +203,7 @@ public void testMultipleSortOrder() throws FileNotFoundException, XpathException
 
 		String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"};
 
-		DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST);
+		DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
 
 		// write docs
 		for(String filename : filenames)
@@ -344,7 +349,7 @@ public void testSortOrderAttribute() throws FileNotFoundException, XpathExceptio
 
 		String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"};
 
-		DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST);
+		DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
 
 		// write docs
 		for(String filename : filenames)

From aa68dda47f3c5c9a174027900e1b30a50aad789b Mon Sep 17 00:00:00 2001
From: skottam 
Date: Fri, 7 Nov 2014 20:20:35 -0800
Subject: [PATCH 307/357] updating the test after issue 169

---
 .../javaclient/TestEvalwithRunTimeDBnTransactions.java     | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestEvalwithRunTimeDBnTransactions.java b/test-complete/src/test/java/com/marklogic/javaclient/TestEvalwithRunTimeDBnTransactions.java
index e87c16710..e2f40ab07 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/TestEvalwithRunTimeDBnTransactions.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestEvalwithRunTimeDBnTransactions.java
@@ -69,7 +69,7 @@ public void setUp() throws Exception {
 	public void tearDown() throws Exception {
 		client.release();
 	}
-//issue 169, loop the eval query more than 150 times and should not stuck
+// loop the eval query more than 150 times and should not stuck
 	@Test
 	public void test1MultipleEvalQueries() throws Exception {
 	
@@ -84,7 +84,8 @@ public void test1MultipleEvalQueries() throws Exception {
 	    docMgr.write("/binary4mbdoc",handle1);
 	    String query = "declare variable $myInteger as xs:integer external;"
 	    		+ "(fn:doc()/binary(),$myInteger,xdmp:database-name(xdmp:database()))";
-	    for(int i=0 ; i<=20;i++){
+	   long sizeOfBinary =docMgr.read("/binary4mbdoc",new InputStreamHandle()).getByteLength();
+	    for(int i=0 ; i<=330;i++){
 	    	ServerEvaluationCall evl= client.newServerEval().xquery(query);
 	    	evl.addVariable("myInteger", (int)i);
 	    	EvalResultIterator evr = evl.eval();
@@ -94,7 +95,7 @@ public void test1MultipleEvalQueries() throws Exception {
 					 assertEquals("itration number",i,er.getNumber().intValue());
 				 }else if(er.getType().equals(Type.BINARY)){
 					 FileHandle readHandle1 = new FileHandle();
-					 assertEquals("size of the binary ",er.get(readHandle1).get().length(),docMgr.read("/binary4mbdoc",new InputStreamHandle()).getByteLength());
+					 assertEquals("size of the binary ",er.get(readHandle1).get().length(),sizeOfBinary);
 					 
 				 }else if(er.getType().equals(Type.STRING)){
 					 assertEquals("database name ","TestEvalXqueryWithTransDB",er.getString());

From d09ee94b0d9f2fbe564ca6bf7fac28fa704161f2 Mon Sep 17 00:00:00 2001
From: gvaidees 
Date: Sat, 8 Nov 2014 23:26:07 -0800
Subject: [PATCH 308/357] Changes to accomodate management API changes (name
 for user creation is user-name and for db creation is database-name

---
 .../java/com/marklogic/javaclient/ConnectedRESTQA.java    | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java b/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java
index ab0791c56..17eb5f5ab 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java
@@ -57,7 +57,7 @@ public static void createDB(String dbName)	{
 					new UsernamePasswordCredentials("admin", "admin"));
 
 			HttpPost post = new HttpPost("http://localhost:8002"+ "/manage/v2/databases?format=json");
-			String JSONString = "[{\"name\":\""+ dbName + "\"}]";
+			String JSONString = "[{\"database-name\":\""+ dbName + "\"}]";
 
 			post.addHeader("Content-type", "application/json");
 			post.setEntity( new StringEntity(JSONString));
@@ -364,7 +364,7 @@ public static void createUserRolesWithPrevilages(String roleName, String... priv
 				ArrayNode roleArray = mapper.createArrayNode();
 				ArrayNode privArray = mapper.createArrayNode();
 				ArrayNode permArray = mapper.createArrayNode();
-				mainNode.put("name",roleName);
+				mainNode.put("role-name",roleName);
 				mainNode.put("description", "role discription");
 				
 				for(String rolename: roleNames)
@@ -434,7 +434,7 @@ public static void createRESTUser(String usrName, String pass, String... roleNam
 				ObjectNode mainNode = mapper.createObjectNode();
 				//			ObjectNode childNode = mapper.createObjectNode();
 				ArrayNode childArray = mapper.createArrayNode();
-				mainNode.put("name",usrName);
+				mainNode.put("user-name",usrName);
 				mainNode.put("description", "user discription");
 				mainNode.put("password", pass);
 				for(String rolename: roleNames)
@@ -531,7 +531,7 @@ public static void createRESTUserWithPermissions(String usrName, String pass,Obj
 				ObjectNode mainNode = mapper.createObjectNode();
 				//			ObjectNode childNode = mapper.createObjectNode();
 				ArrayNode childArray = mapper.createArrayNode();
-				mainNode.put("name",usrName);
+				mainNode.put("user-name",usrName);
 				mainNode.put("description", "user discription");
 				mainNode.put("password", pass);
 				for(String rolename: roleNames)

From 48c11f7dc04f409e7f625893574eed8334f5ece9 Mon Sep 17 00:00:00 2001
From: skottam 
Date: Sun, 9 Nov 2014 23:04:49 -0800
Subject: [PATCH 309/357] updated with a test for issue 141

---
 .../javaclient/TestRuntimeDBselection.java    | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestRuntimeDBselection.java b/test-complete/src/test/java/com/marklogic/javaclient/TestRuntimeDBselection.java
index d4e2baf43..8c4118cc8 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/TestRuntimeDBselection.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestRuntimeDBselection.java
@@ -14,6 +14,7 @@
 import com.marklogic.client.DatabaseClientFactory;
 import com.marklogic.client.DatabaseClientFactory.Authentication;
 import com.marklogic.client.FailedRequestException;
+import com.marklogic.client.ForbiddenUserException;
 
 public class TestRuntimeDBselection extends BasicJavaClientREST {
 	private static String dbName = "TestRuntimeDB";
@@ -72,5 +73,21 @@ public void testRuntimeDBclientWithDifferentAuthType() throws Exception {
 		client.release();
 		associateRESTServerWithDefaultUser(restServerName,"nobody","digest");
 	}
-
+	//issue 141 user with no privileges for eval
+	@Test(expected=FailedRequestException.class)
+	public void testRuntimeDBclientWithNoPrivUser() throws Exception {
+		
+		client = DatabaseClientFactory.newClient("localhost", restPort,dbName,"rest-admin","x",Authentication.BASIC);
+		String insertJSON = "xdmp:document-insert(\"test2.json\",object-node {\"test\":\"hello\"})";
+		client.newServerEval().xquery(insertJSON).eval();
+		String query1 = "fn:count(fn:doc())";
+		int response = client.newServerEval().xquery(query1).eval().next().getNumber().intValue();
+		assertEquals("count of documents ",1,response);
+		String query2 ="declareUpdate();xdmp.documentDelete(\"test2.json\");";
+		client.newServerEval().javascript(query2).eval();
+		int response2 = client.newServerEval().xquery(query1).eval().next().getNumber().intValue();
+		assertEquals("count of documents ",0,response2);
+		client.release();
+		
+	}
 }

From b88e14e1b67813132e7e9dff872ebfe1dbc6c67f Mon Sep 17 00:00:00 2001
From: skottam 
Date: Mon, 10 Nov 2014 09:41:37 -0800
Subject: [PATCH 310/357] updated with a test for an issue 151

---
 .../test/java/com/marklogic/javaclient/TestEvalXquery.java    | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestEvalXquery.java b/test-complete/src/test/java/com/marklogic/javaclient/TestEvalXquery.java
index 1eaf4d9a3..689e75765 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/TestEvalXquery.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestEvalXquery.java
@@ -132,8 +132,8 @@ else if(er.getType().equals(Type.STRING)){
 				 System.out.println("Testing is empty sequence is NUll?"+er.getAs(String.class));
 			 }else if(er.getType().equals(Type.OTHER)){
 				//There is git issue 151
-				 System.out.println("Testing is Others? "+er.getAs(String.class));
-//				 assertEquals("Returns OTHERs","xdmp:forest-restart#1",er.getString());
+//				 System.out.println("Testing is Others? "+er.getAs(String.class));
+				  assertTrue("Returns OTHERs",(er.getString().contains("xdmp:forest-restart#1") || er.getString().contains("PT0S")));
 				 
 			 }else if(er.getType().equals(Type.ANYURI)){
 //				 System.out.println("Testing is AnyUri? "+er.getAs(String.class));

From e55dd3fc1a0e1c56ce1d5417d81856fad9f5db85 Mon Sep 17 00:00:00 2001
From: skottam 
Date: Mon, 10 Nov 2014 11:22:37 -0800
Subject: [PATCH 311/357] updated with a test for an issue 151

---
 .../marklogic/javaclient/TestJSResourceExtensions.java | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestJSResourceExtensions.java b/test-complete/src/test/java/com/marklogic/javaclient/TestJSResourceExtensions.java
index ad58641ef..72d5d4c16 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/TestJSResourceExtensions.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestJSResourceExtensions.java
@@ -207,12 +207,12 @@ public void test1GetAllResourceServices() throws Exception {
 		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\"}";
+		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\"}";
+		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\"}";
+		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(expected, tjs.getJSON(), false);
 	
 		JSONAssert.assertEquals(expectedResponse, tjs.deleteJSON("helloJS.json"), false);
@@ -239,14 +239,14 @@ public void test2GetAllResourceServicesMultipleTimes() throws Exception {
 		assertEquals("Total documents loaded are",150,jh.get().get("document-count").intValue());
 
 		String expAftrPut ="{\"argument1\":\"hello\", \"argument2\":\"Earth\", \"content\":\"This is a JSON document\", \"array\":[1, 2, 3], \"response\":[200, \"OK\"], \"outputTypes\":\"application/json\"}";
-		String expected ="{\"argument1\":\"helloJS.json\", \"argument2\":\"Earth\", \"database-name\":\"TestJSResourceExtensionDB\", \"document-count\":0, \"content\":\"This is a JSON document\", \"document-content\":null, \"response\":[200, \"OK\"], \"outputTypes\":\"application/json\"}";
+		String expected ="{\"argument1\":\"helloJS.json\", \"argument2\":\"Earth\", \"database-name\":\"TestJSResourceExtensionDB\", \"document-count\":0, \"content\":\"This is a JSON document\", \"document-content\":null, \"response\":[200, \"OK\"], \"outputTypes\":[\"application/json\"]}";
 //		verify by reading all the documents to see put and post services correctly inserted documents and delete them
 	    for(int j=0;j<150;j++){
 	    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);
 	    }
-//		System.out.println(tjs.getJSON());
+		System.out.println(tjs.getJSON("helloJS.json"));
 		JSONAssert.assertEquals(expected, tjs.getJSON("helloJS.json"), false);
 
 	}

From c44a589b79361fb98a326bbf65b2968b59706a6e Mon Sep 17 00:00:00 2001
From: sammefford 
Date: Mon, 10 Nov 2014 12:44:30 -0700
Subject: [PATCH 312/357] temporary fix for #185 while we wait for
 https://bugtrack.marklogic.com/30470

---
 .../java/com/marklogic/client/impl/PojoRepositoryImpl.java     | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java b/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java
index 0dc08c211..7e5f56055 100644
--- a/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java
+++ b/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java
@@ -207,7 +207,8 @@ public long count(String[] collections, Transaction transaction) {
     @Override
     public long count(PojoQueryDefinition query, Transaction transaction) {
         long pageLength = getPageLength();
-        setPageLength(0);
+        // set below to 0 when we get a fix for https://bugtrack.marklogic.com/30470
+        setPageLength(1);
         PojoPage page = search(query, 1, transaction);
         setPageLength(pageLength);
         return page.getTotalSize();

From 8cdd3cb1981cc34794cf5c5eaa41d1f195d8c7bd Mon Sep 17 00:00:00 2001
From: skottam 
Date: Mon, 10 Nov 2014 12:04:19 -0800
Subject: [PATCH 313/357] updated with a test for an issue 75

---
 .../TestPOJOReadWriteWithTransactions.java    | 74 +++++++++++--------
 1 file changed, 42 insertions(+), 32 deletions(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOReadWriteWithTransactions.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOReadWriteWithTransactions.java
index 4432f3e01..e35c99deb 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOReadWriteWithTransactions.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOReadWriteWithTransactions.java
@@ -83,9 +83,9 @@ public void testPOJOWriteWithTransaction() throws Exception {
 			for(int i=1;i<112;i++){
 				products.write(this.getArtifact(i),t);
 			}
-			//		assertEquals("Total number of object recods",111, products.count());
+			assertEquals("Total number of object recods",111, products.count(t));
 			for(long i=1;i<112;i++){
-				//			assertTrue("Product id "+i+" does not exist",products.exists(i));
+				assertTrue("Product id "+i+" does not exist",products.exists(i,t));
 				this.validateArtifact(products.read(i,t));
 			}
 
@@ -116,19 +116,27 @@ public void testPOJOWriteWithTransCollection() throws Exception {
 			}
 		}catch(Exception e){throw e;}
 		finally{t.commit();}
-		assertEquals("Total number of object recods",110, products.count("numbers"));
-		assertEquals("Collection even count",55,products.count("even"));
-		assertEquals("Collection odd count",55,products.count("odd"));
+//		assertEquals("Total number of object recods",110, products.count("numbers"));
+//		assertEquals("Collection even count",55,products.count("even"));
+//		assertEquals("Collection odd count",55,products.count("odd"));
 		for(long i=112;i<222;i++){
 			// validate all the records inserted are readable
 			assertTrue("Product id "+i+" does not exist",products.exists(i));
 			this.validateArtifact(products.read(i));
 		}
-		//		t= client.openTransaction();
-
-		products.delete((long)112);
-		assertFalse("Product id 112 exists ?",products.exists((long)112));
-		products.deleteAll();
+		Transaction t2= client.openTransaction();
+		try{
+		Long[] ids = {(long)112,(long)113};
+		products.delete(ids,t2);
+		assertFalse("Product id 112 exists ?",products.exists((long)112,t2));
+//		assertTrue("Product id 112 exists ?",products.exists((long)112));
+		products.deleteAll(t2);
+		for(long i=112;i<222;i++){
+			assertFalse("Product id "+i+" exists ?",products.exists(i,t2));
+//			assertTrue("Product id "+i+" exists ?",products.exists(i));
+		}
+		}catch(Exception e){throw e;}
+		finally{t2.commit();}
 		//see any document exists
 		for(long i=112;i<222;i++){
 			assertFalse("Product id "+i+" exists ?",products.exists(i));
@@ -138,7 +146,8 @@ public void testPOJOWriteWithTransCollection() throws Exception {
 		products.deleteAll();
 	}
 	//This test is to read objects into pojo page based on Ids 
-	// until #103 is resolved	@Test
+	// until #103 is resolved	
+	@Test
 	public void testPOJOWriteWithPojoPage() {
 		PojoRepository products = client.newPojoRepository(Artifact.class, Long.class);
 		//Load more than 110 objects into different collections
@@ -154,33 +163,35 @@ public void testPOJOWriteWithPojoPage() {
 				products.write(this.getArtifact(i),"odd","numbers");
 			}
 		}
-		assertEquals("Total number of object recods",111, products.count("numbers"));
-		assertEquals("Collection even count",56,products.count("even"));
-		assertEquals("Collection odd count",55,products.count("odd"));
+//		assertEquals("Total number of object recods",111, products.count("numbers"));
+//		assertEquals("Collection even count",56,products.count("even"));
+//		assertEquals("Collection odd count",55,products.count("odd"));
 
 		System.out.println("Default Page length setting on docMgr :"+products.getPageLength());
 		assertEquals("Default setting for page length",50,products.getPageLength());
 		products.setPageLength(100);
-		//			assertEquals("explicit setting for page length",1,products.getPageLength());
-		PojoPage p= products.read(ids);
+		assertEquals("explicit setting for page length",100,products.getPageLength());
+		PojoPage p= products.search(1, "numbers");
 		// test for page methods
-		//Issue-	assertEquals("Number of records",1,p.size());
-		System.out.println("Page size"+p.size());
-		//			assertEquals("Starting record in first page ",1,p.getStart());
-		System.out.println("Starting record in first page "+p.getStart());
-
-		//			assertEquals("Total number of estimated results:",111,p.getTotalSize());
-		System.out.println("Total number of estimated results:"+p.getTotalSize());
-		//			assertEquals("Total number of estimated pages :",111,p.getTotalPages());
-		System.out.println("Total number of estimated pages :"+p.getTotalPages());
-		assertFalse("Is this First page :",p.isFirstPage());//this is bug
+		assertEquals("Number of records",100,p.size());
+//		System.out.println("Page size"+p.size());
+		assertEquals("Starting record in first page ",1,p.getStart());
+//		System.out.println("Starting record in first page "+p.getStart());
+
+		assertEquals("Total number of estimated results:",111,p.getTotalSize());
+//		System.out.println("Total number of estimated results:"+p.getTotalSize());
+		assertEquals("Total number of estimated pages :",2,p.getTotalPages());
+//		System.out.println("Total number of estimated pages :"+p.getTotalPages());
+		System.out.println("is this firstPage or LastPage:"+p.isFirstPage()+"  "+p.isLastPage()+"has  previous page"+p.hasPreviousPage());
+		assertTrue("Is this First page :",p.isFirstPage());//this is bug
 		assertFalse("Is this Last page :",p.isLastPage());
 		assertTrue("Is this First page has content:",p.hasContent());
 		//		Need the Issue #75 to be fixed  
-		assertTrue("Is first page has previous page ?",p.hasPreviousPage());
+		assertFalse("Is first page has previous page ?",p.hasPreviousPage());
 		long pageNo=1,count=0;
 		do{
 			count=0;
+			p= products.search(pageNo, "numbers");
 			if(pageNo >1){ 
 				assertFalse("Is this first Page", p.isFirstPage());
 				assertTrue("Is page has previous page ?",p.hasPreviousPage());
@@ -191,14 +202,13 @@ public void testPOJOWriteWithPojoPage() {
 				count++;
 			}
 			//			assertEquals("document count", p.size(),count);
-			System.out.println("Is this Last page :"+p.hasContent()+p.isLastPage());
+			System.out.println("Is this Last page :"+p.hasContent()+p.isLastPage()+p.getPageNumber());
 			pageNo = pageNo + p.getPageSize();
-		}while((p.isLastPage()) && p.hasContent());
-		assertTrue("page count is 111 ",pageNo == p.getTotalPages());
+		}while(pageNo< p.getTotalSize());
 		assertTrue("Page has previous page ?",p.hasPreviousPage());
-		assertEquals("page size", 1,p.getPageSize());
+		assertEquals("page size", 11,p.size());
 		assertEquals("document count", 111,p.getTotalSize());
-		assertFalse("Page has any records ?",p.hasContent());
+		assertTrue("Page has any records ?",p.hasContent());
 
 
 		products.deleteAll();

From a6d75aea0ec89a94ca2ba124472810ee40e960d8 Mon Sep 17 00:00:00 2001
From: Ajit George 
Date: Mon, 10 Nov 2014 13:55:25 -0800
Subject: [PATCH 314/357] Fixed tests. refer to Git issue #188.

---
 .../javaclient/TestPOJOSpecialCharRead.java   | 33 +++++++++----------
 1 file changed, 15 insertions(+), 18 deletions(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOSpecialCharRead.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOSpecialCharRead.java
index f6490c77c..869e7be61 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOSpecialCharRead.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOSpecialCharRead.java
@@ -17,6 +17,7 @@
 import com.marklogic.client.DatabaseClient;
 import com.marklogic.client.DatabaseClientFactory;
 import com.marklogic.client.DatabaseClientFactory.Authentication;
+import com.marklogic.client.ResourceNotFoundException;
 import com.marklogic.client.pojo.PojoPage;
 import com.marklogic.client.pojo.PojoRepository;
 import com.marklogic.client.pojo.annotation.Id;
@@ -391,10 +392,10 @@ public void validateArtifactTwo(Artifact artifact, long longId) {
 		assertNotNull("Artifact object should never be Null", artifact);
 		assertNotNull("Id should never be Null",artifact.id);
 		assertEquals("Id of the object is ",longId, artifact.getId());
-		assertEquals("Name of the object is ","Cogs -2000", artifact.getName());
+		assertEquals("Name of the object is ","Cogs", artifact.getName());
 		assertEquals("Inventory of the object is ",1000, artifact.getInventory());
-		assertEquals("Company name of the object is ","Acme -2000, Inc.", artifact.getManufacturer().getName());
-		assertEquals("Web site of the object is ","http://www.acme-2000.com", artifact.getManufacturer().getWebsite());
+		assertEquals("Company name of the object is ","Acme Inc.", artifact.getManufacturer().getName());
+		assertEquals("Web site of the object is ","http://www.acme.com", artifact.getManufacturer().getWebsite());
 		assertEquals(-87.966, artifact.getManufacturer().getLongitude(), 0.00);
 		assertEquals(41.998, artifact.getManufacturer().getLatitude(), 0.00);
 	}
@@ -456,9 +457,10 @@ public void testPOJORepoReadWithNegativeId() {
 	/*
      * Purpose : This test is to validate delete documents using delete(Id)
      * POJO instance @Id field value with Negative numbers.
+     * Expect ResourceNotFoundException when there are no URI found. As per Git # 188.
      */
     
-	@Test
+	@Test(expected=ResourceNotFoundException.class)
 	public void testPOJORepoDeleteWithNegativeId() {
 		PojoRepository pojoReposProducts = client.newPojoRepository(Artifact.class, Long.class);		
 		
@@ -471,15 +473,15 @@ public void testPOJORepoDeleteWithNegativeId() {
 		
 		// Validate the artifact read back.
 		Artifact artifact = pojoReposProducts.read(longId);
-		assertNull(artifact);				
 	}
 	
 	/*
      * Purpose : This test is to validate delete documents using delete(ID....)
      * POJO instance @Id field value with Negative numbers.
+     * Expect ResourceNotFoundException when there are no URI found. As per Git # 188.
      */
 
-	@Test
+	@Test(expected=ResourceNotFoundException.class)
 	public void testPOJORepoDeleteWithNegativeIdArray() {
 		long longId1 = getOneNegativeLongId();
 		long longId2 = getOneNegativeLongId();
@@ -502,8 +504,7 @@ public void testPOJORepoDeleteWithNegativeIdArray() {
 		pojoReposProducts.delete(longId1,longId2);
 		
 		// Validate the artifacts read back is zero.
-		pojoArtifactPage = pojoReposProducts.read(pojoReposProductsIdLongArray);
-		assertEquals("Total number of object records", 0, pojoArtifactPage.getPageSize());		
+		pojoArtifactPage = pojoReposProducts.read(pojoReposProductsIdLongArray);		
 	}
 	
 	/*
@@ -534,22 +535,18 @@ public void testPOJORepoReadWithNegativeIdArray() {
 		
 		System.out.println("The number of pages covering all possible items "+ pojoArtifactPage.getTotalPages());
 		assertEquals("The number of pages covering all possible items ", 1, pojoArtifactPage.getTotalPages());
-		
-		// Not sure about this feature. Should page number at the last or at the start. O/P is 2 now.
+				
 		System.out.println("The page number within the count of all possible pages "+ pojoArtifactPage.getPageNumber());
 		assertEquals("The page number within the count of all possible pages ", 1, pojoArtifactPage.getPageNumber());
-		
-		// Not sure about this feature. User cannot set the max # of items allowed in one page. O/P is -1 now.
+				
 		System.out.println("The page size which is the maximum number of items allowed in one page "+ pojoArtifactPage.getPageSize());
-		//assertEquals("The page size which is the maximum number of items allowed in one page ", 1, pojoArtifactPage.getPageSize());
-		
-		//Not sure about this feature. Start position of this page within all possible items. O/P is -1 now.
+		assertEquals("The page size which is the maximum number of items allowed in one page ", 2, pojoArtifactPage.getPageSize());
+				
 		System.out.println("The start position of this page within all possible items "+ pojoArtifactPage.getStart());
-		//assertEquals("The start position of this page within all possible items ", 1, pojoArtifactPage.getStart());
+		assertEquals("The start position of this page within all possible items ", 1, pojoArtifactPage.getStart());
 		
-		//Not sure about this feature. O/P is -1 now.
 		System.out.println("The total count (potentially an estimate) of all possible items in the set "+ pojoArtifactPage.getTotalSize());
-		//assertEquals("The total count (potentially an estimate) of all possible items in the set ", 1, pojoArtifactPage.getTotalSize());
+		assertEquals("The total count (potentially an estimate) of all possible items in the set ", 2, pojoArtifactPage.getTotalSize());
 					
 		Iterator itr = pojoArtifactPage.iterator();
 		Artifact artifact = null;

From 4799bdca7fc84a59117b2b0ee47ac728b720f106 Mon Sep 17 00:00:00 2001
From: Ajit George 
Date: Mon, 10 Nov 2014 14:02:39 -0800
Subject: [PATCH 315/357] Changed test methods to handle
 ResourceNotFoundException

---
 .../java/com/marklogic/javaclient/TestPOJOSpecialCharRead.java | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOSpecialCharRead.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOSpecialCharRead.java
index 869e7be61..36e378ba7 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOSpecialCharRead.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOSpecialCharRead.java
@@ -471,7 +471,8 @@ public void testPOJORepoDeleteWithNegativeId() {
 		// Delete the object
 		pojoReposProducts.delete(longId);
 		
-		// Validate the artifact read back.
+		// Validate the artifact read back. ResourceNotFoundException will be thrown.
+		@SuppressWarnings("unused")
 		Artifact artifact = pojoReposProducts.read(longId);
 	}
 	

From 130df53eeffc4a8ce62e4e5321bfd3e5c5929c28 Mon Sep 17 00:00:00 2001
From: skottam 
Date: Mon, 10 Nov 2014 14:03:21 -0800
Subject: [PATCH 316/357] updated with a test for an issue 75

---
 .../com/marklogic/javaclient/TestDatabaseClientConnection.java  | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestDatabaseClientConnection.java b/test-complete/src/test/java/com/marklogic/javaclient/TestDatabaseClientConnection.java
index 2e09081de..4c625c5e5 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/TestDatabaseClientConnection.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestDatabaseClientConnection.java
@@ -52,7 +52,7 @@ public void testReleasedClient() throws IOException
 			stringException = "Client is not available - " + e;
 		}
 		
-		String expectedException = "Client is not available - java.lang.NullPointerException";
+		String expectedException = "Client is not available - java.lang.IllegalStateException: You cannot use this connected object anymore--connection has already been released";
 		assertEquals("Exception is not thrown", expectedException, stringException);
 	}
 

From f34ff69564f1555a548b6a62cb0e94855aab8941 Mon Sep 17 00:00:00 2001
From: skottam 
Date: Mon, 10 Nov 2014 14:18:08 -0800
Subject: [PATCH 317/357] updated with a test for an issue 78

---
 .../javaclient/TestBulkSearchWithStringQueryDef.java         | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java
index 74b2721be..506058790 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java
@@ -173,7 +173,10 @@ public void testBulkSearchSQDwithDifferentPageSizes() {
 		assertEquals("Total number of estimated results:",101,page.getTotalSize());
 		assertEquals("Total number of estimated pages :",101,page.getTotalPages());
 		// till the issue #78 get fixed
-		assertFalse("Is this First page :",page.isFirstPage());//this is bug
+		System.out.println("Is this First page :"+page.isFirstPage()+page.getPageNumber());//this is bug
+		System.out.println("Is this Last page :"+page.isLastPage());
+		System.out.println("Is this First page has content:"+page.hasContent());
+		assertTrue("Is this First page :",page.isFirstPage());//this is bug
 		assertFalse("Is this Last page :",page.isLastPage());
 		assertTrue("Is this First page has content:",page.hasContent());
 		//		Need the Issue #75 to be fixed  

From a1d9bd63d135945629dcb417720592f440756456 Mon Sep 17 00:00:00 2001
From: Ajit George 
Date: Mon, 10 Nov 2014 15:41:01 -0800
Subject: [PATCH 318/357] Changed test methods to handle Uber Port and fixes,
 added read binary method BasicJavaClientREST

---
 .../javaclient/BasicJavaClientREST.java       | 74 ++++++++++++++++---
 .../marklogic/javaclient/TestBytesHandle.java | 19 +++--
 2 files changed, 76 insertions(+), 17 deletions(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/BasicJavaClientREST.java b/test-complete/src/test/java/com/marklogic/javaclient/BasicJavaClientREST.java
index dd0fcf164..82c594aa3 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/BasicJavaClientREST.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/BasicJavaClientREST.java
@@ -675,6 +675,49 @@ public void updateDocumentReaderHandle(DatabaseClient client, String filename, S
 	    
 	    docStream.close();
 	}
+	
+	  /* Read a binary file, and return its contents as an array of bytes.
+	   *
+	   */ 
+	
+	  byte[] readBinaryFile(String binaryFile)
+	  {
+		System.out.println("Read binary file : " + binaryFile);
+	    
+		File file = new File(binaryFile);
+	    System.out.println("File size is : " + file.length());
+	    
+	    byte[] byteArray = new byte[(int)file.length()];
+	    
+	    try {
+	      InputStream input = null;
+	      try {
+	        int totalBytesRead = 0;
+	        input = new BufferedInputStream(new FileInputStream(file));
+	        while(totalBytesRead < byteArray.length){
+	          int bytesRemaining = byteArray.length - totalBytesRead;
+	          //input.read() returns -1, 0, or more :
+	          int bytesRead = input.read(byteArray, totalBytesRead, bytesRemaining); 
+	          if (bytesRead > 0){
+	            totalBytesRead = totalBytesRead + bytesRead;
+	          }
+	        }
+	        
+	        System.out.println("Number of bytes read: " + totalBytesRead);
+	      }
+	      finally {
+	    	  System.out.println("Closing input stream.");
+	        input.close();
+	      }
+	    }
+	    catch (FileNotFoundException fnfex) {
+	    	 System.out.println("File not found Exception.");
+	    }
+	    catch (IOException ioex) {
+	    	 System.out.println(ioex);
+	    }
+	    return byteArray;
+	  }
 
 	/**
 	 * Read document using ReaderHandle
@@ -1070,19 +1113,30 @@ public void updateDocumentUsingByteHandle(DatabaseClient client, String filename
 	{   
 	    // create doc manager
 	    DocumentManager docMgr = null;
+	    byte[] contentInByte = null;
+	    // variable to hold QA data directory path
+	    String dirpath = new String("src/test/java/com/marklogic/javaclient/data/");
+	    
 	    docMgr = documentManagerSelector(client, docMgr, type);	
-				
-	    // acquire the content
-	    FileReader content = new FileReader("src/test/java/com/marklogic/javaclient/data/" + filename);
-	    //String contentInString = new String(content);
-	    BufferedReader br = new BufferedReader(content);
-	    String readContent = "";
-	    String line = null;
-	    while ((line = br.readLine()) != null)
+	    
+	    if(type.equalsIgnoreCase("BINARY"))
+	    {
+	    	contentInByte = readBinaryFile(dirpath + filename);
+	    }
+	    else
+	    {				
+	    	// acquire the content
+	    	FileReader content = new FileReader(dirpath + filename);
+	    	//String contentInString = new String(content);
+	    	BufferedReader br = new BufferedReader(content);
+	    	String readContent = "";
+	    	String line = null;
+	    	while ((line = br.readLine()) != null)
 	    		readContent = readContent + line; 
-	    br.close();
+	    	br.close();
 
-	    byte[] contentInByte = (byte[])readContent.getBytes();
+	    	contentInByte = (byte[])readContent.getBytes();
+	    }
 	    // create an identifier for the document
 	    String docId = uri;
 	    // create a handle on the content
diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBytesHandle.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBytesHandle.java
index 53adeaf94..3147aeaec 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/TestBytesHandle.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBytesHandle.java
@@ -23,10 +23,15 @@ public class TestBytesHandle extends BasicJavaClientREST{
 private static String dbName = "BytesHandleDB";
 private static String [] fNames = {"BytesHandleDB-1"};
 private static String restServerName = "REST-Java-Client-API-Server";
+//Additional port to test for Uber port
+private static int uberPort = 8000;
+
 @BeforeClass
 public static void setUp() throws Exception{
 	System.out.println("In setup");
 	setupJavaRESTServer(dbName, fNames[0], restServerName,8011);
+	createUserRolesWithPrevilages("test-eval","xdbc:eval", "xdbc:eval-in","xdmp:eval-in","any-uri","xdbc:invoke");
+    createRESTUser("eval-user", "x", "test-eval","rest-admin","rest-writer","rest-reader");
 	}
 
 @Test
@@ -39,10 +44,10 @@ public void testXmlCRUD() throws IOException , SAXException, ParserConfiguration
 	XMLUnit.setNormalizeWhitespace(true);
 	
 	// connect the client
-	DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x",Authentication.DIGEST);
+	DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x",Authentication.DIGEST);
 	
 	// write docs
-	writeDocumentUsingBytesHandle(client, filename, uri, null,"XML");//***********
+	writeDocumentUsingBytesHandle(client, filename, uri, null,"XML");
 	
 	//read docs
 	BytesHandle contentHandle = readDocumentUsingBytesHandle(client, uri + filename,"XML");
@@ -101,7 +106,7 @@ public void testTextCRUD() throws IOException, ParserConfigurationException, SAX
 	System.out.println("Runing test TextCRUD");
 	
 	// connect the client
-	DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
+	DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
 	
 	// write docs
 	writeDocumentUsingBytesHandle(client, filename, uri, "Text");
@@ -161,7 +166,7 @@ public void testJsonCRUD() throws IOException, ParserConfigurationException, SAX
 	ObjectMapper mapper = new ObjectMapper();
 	
 	// connect the client
-	DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
+	DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
 
 	// write docs
 	writeDocumentUsingBytesHandle(client, filename, uri, "JSON");
@@ -220,7 +225,7 @@ public void testBinaryCRUD() throws IOException, ParserConfigurationException, S
 	System.out.println("Running testBinaryCRUD");
 
 	// connect the client
-	DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
+	DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
 	
 	// write docs
 	writeDocumentUsingBytesHandle(client, filename, uri, "Binary");
@@ -250,8 +255,8 @@ public void testBinaryCRUD() throws IOException, ParserConfigurationException, S
     
     // get the binary size
 	long sizeUpdate = getBinarySizeFromByte(fileReadUpdate);
-	long expectedSizeUpdate = 3290;
-	//long expectedSizeUpdate = 3322;
+	//long expectedSizeUpdate = 3290;
+	long expectedSizeUpdate = 3322;
 	assertEquals("Binary size difference", expectedSizeUpdate, sizeUpdate);
 	
 	// delete the document

From 9c7b56135bc90355342fa431392a4a3a76429e30 Mon Sep 17 00:00:00 2001
From: sammefford 
Date: Mon, 10 Nov 2014 16:50:11 -0700
Subject: [PATCH 319/357] fix #78--isFirstPage and getPageNumber were
 calculating incorrectly

---
 .../com/marklogic/client/impl/BasicPage.java  |  5 +-
 .../client/test/BulkReadWriteTest.java        | 82 +++++++++++++++++++
 .../com/marklogic/client/test/PageTest.java   | 22 ++++-
 3 files changed, 106 insertions(+), 3 deletions(-)

diff --git a/src/main/java/com/marklogic/client/impl/BasicPage.java b/src/main/java/com/marklogic/client/impl/BasicPage.java
index 662afa81c..64fa0642d 100644
--- a/src/main/java/com/marklogic/client/impl/BasicPage.java
+++ b/src/main/java/com/marklogic/client/impl/BasicPage.java
@@ -107,7 +107,10 @@ public boolean hasPreviousPage() {
     }
 
     public long getPageNumber() {
-        return (long) Math.floor((double) start / (double) getPageSize()) + 1;
+        double _start = (double) start;
+        double _pageSize = (double) getPageSize();
+        if ( _start % _pageSize == 0 ) return new Double(_start / _pageSize).longValue();
+        else return (long) Math.floor(_start / _pageSize) + 1;
     }
 
     public boolean isFirstPage() {
diff --git a/src/test/java/com/marklogic/client/test/BulkReadWriteTest.java b/src/test/java/com/marklogic/client/test/BulkReadWriteTest.java
index e6a25f79a..411156d28 100644
--- a/src/test/java/com/marklogic/client/test/BulkReadWriteTest.java
+++ b/src/test/java/com/marklogic/client/test/BulkReadWriteTest.java
@@ -29,6 +29,7 @@
 import javax.xml.bind.JAXBException;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
@@ -60,9 +61,12 @@
 import com.marklogic.client.io.SearchHandle;
 import com.marklogic.client.io.StringHandle;
 import com.marklogic.client.query.DeleteQueryDefinition;
+import com.marklogic.client.query.MatchDocumentSummary;
 import com.marklogic.client.query.QueryDefinition;
 import com.marklogic.client.query.QueryManager;
+import com.marklogic.client.query.StringQueryDefinition;
 import com.marklogic.client.query.StructuredQueryBuilder;
+import com.marklogic.client.query.QueryManager.QueryView;
 
 /** Loads data from cities15000.txt which contains every city above 15000 people, and adds
  * data from countryInfo.txt.
@@ -426,6 +430,84 @@ public void testF_DefaultMetadata() {
         }
     }
 
+    @Test
+    public void test_78() {
+        String DIRECTORY ="/test_78/";
+        int BATCH_SIZE=10;
+        int count =1;
+        TextDocumentManager docMgr = Common.client.newTextDocumentManager();
+        DocumentWriteSet writeset =docMgr.newWriteSet();
+        for(int i =0;i<11;i++){
+            writeset.add(DIRECTORY+"Textfoo"+i+".txt", new StringHandle().with("bar can be foo"+i));
+            if(count%BATCH_SIZE == 0){
+                docMgr.write(writeset);
+                writeset = docMgr.newWriteSet();
+            }
+            count++;
+        }
+        if(count%BATCH_SIZE > 0){
+            docMgr.write(writeset);
+        }
+        //using QueryManger for query definition and set the search criteria
+        QueryManager queryMgr = Common.client.newQueryManager();
+        try {
+	        StringQueryDefinition qd = queryMgr.newStringDefinition();
+	        qd.setCriteria("bar");
+	        qd.setDirectory(DIRECTORY);
+	        // set  document manager level settings for search response
+	        System.out.println("Default Page length setting on docMgr :"+docMgr.getPageLength());
+	        docMgr.setPageLength(1);
+	        docMgr.setSearchView(QueryView.RESULTS);
+	        docMgr.setNonDocumentFormat(Format.XML);
+	        assertEquals("format set on document manager","XML",docMgr.getNonDocumentFormat().toString());
+	        assertEquals("Queryview set on document manager ","RESULTS" ,docMgr.getSearchView().toString());
+	        assertEquals("Page length ",1,docMgr.getPageLength());
+	        // Search for documents where content has bar and get first result record, get search handle on it
+	        SearchHandle sh = new SearchHandle();
+	        DocumentPage page= docMgr.search(qd, 1);
+	        // test for page methods
+	        assertEquals("Number of records",1,page.size());
+	        assertEquals("Starting record in first page ",1,page.getStart());
+	        assertEquals("Total number of estimated results:",11,page.getTotalSize());
+	        assertEquals("Total number of estimated pages :",11,page.getTotalPages());
+	        assertTrue("Is this First page :",page.isFirstPage());
+	        assertFalse("Is this Last page :",page.isLastPage());
+	        assertTrue("Is this First page has content:",page.hasContent());
+	        assertFalse("Is first page has previous page ?",page.hasPreviousPage());
+	        //      
+	        long start=1;
+	        do{
+	            count=0;
+	            page = docMgr.search(qd, start,sh);
+	            if(start >1){ 
+	                assertFalse("Is this first Page", page.isFirstPage());
+	                assertTrue("Is page has previous page ?",page.hasPreviousPage());
+	            }
+	            while(page.hasNext()){
+	                page.next();
+	                count++;
+	            }
+	            MatchDocumentSummary[] mds= sh.getMatchResults();
+	            assertEquals("Matched document count",1,mds.length);
+	            //since we set the query view to get only results, facet count supposed be 0
+	            assertEquals("Matched Facet count",0,sh.getFacetNames().length);
+	
+	            assertEquals("document count", page.size(),count);
+	            if (!page.isLastPage()) start = start + page.getPageSize();
+	        }while(!page.isLastPage());
+	        assertEquals("page count is 11 ",start, page.getTotalPages());
+	        assertTrue("Page has previous page ?",page.hasPreviousPage());
+	        assertEquals("page size", 1,page.getPageSize());
+	        assertEquals("document count", 11,page.getTotalSize());
+	        page= docMgr.search(qd, 12);
+	        assertFalse("Page has any records ?",page.hasContent());
+    	} finally {
+	        DeleteQueryDefinition deleteQuery = queryMgr.newDeleteDefinition();
+	        deleteQuery.setDirectory(DIRECTORY);
+	        queryMgr.delete(deleteQuery);
+        }
+    }
+
     @Test
     public void test_171() throws Exception{
         DatabaseClient client = DatabaseClientFactory.newClient(
diff --git a/src/test/java/com/marklogic/client/test/PageTest.java b/src/test/java/com/marklogic/client/test/PageTest.java
index 7df4287c9..4fe1aeb55 100644
--- a/src/test/java/com/marklogic/client/test/PageTest.java
+++ b/src/test/java/com/marklogic/client/test/PageTest.java
@@ -46,6 +46,15 @@ public void testTestPage() {
         assertEquals("Unexpected isFirstPage", true, page.isFirstPage());
         assertEquals("Unexpected isLastPage", false, page.isLastPage());
 
+        page = new TestPage(iterator, 1, 1, 100);
+        assertEquals("Unexpected size", 1, page.size());
+        assertEquals("Unexpected totalPages", 100, page.getTotalPages());
+        assertEquals("Unexpected hasContent", true, page.hasContent());
+        assertEquals("Unexpected hasNextPage", true, page.hasNextPage());
+        assertEquals("Unexpected pageNumber", 1, page.getPageNumber());
+        assertEquals("Unexpected isFirstPage", true, page.isFirstPage());
+        assertEquals("Unexpected isLastPage", false, page.isLastPage());
+        
         page = new TestPage(iterator, 2, 10, 100);
         assertEquals("Unexpected size", 10, page.size());
         assertEquals("Unexpected totalPages", 10, page.getTotalPages());
@@ -60,8 +69,8 @@ public void testTestPage() {
         assertEquals("Unexpected totalPages", 10, page.getTotalPages());
         assertEquals("Unexpected hasContent", true, page.hasContent());
         assertEquals("Unexpected hasNextPage", true, page.hasNextPage());
-        assertEquals("Unexpected pageNumber", 2, page.getPageNumber());
-        assertEquals("Unexpected isFirstPage", false, page.isFirstPage());
+        assertEquals("Unexpected pageNumber", 1, page.getPageNumber());
+        assertEquals("Unexpected isFirstPage", true, page.isFirstPage());
         assertEquals("Unexpected isLastPage", false, page.isLastPage());
 
         page = new TestPage(iterator, 12, 10, 100);
@@ -73,6 +82,15 @@ public void testTestPage() {
         assertEquals("Unexpected isFirstPage", false, page.isFirstPage());
         assertEquals("Unexpected isLastPage", false, page.isLastPage());
 
+        page = new TestPage(iterator, 20, 20, 100);
+        assertEquals("Unexpected size", 20, page.size());
+        assertEquals("Unexpected totalPages", 5, page.getTotalPages());
+        assertEquals("Unexpected hasContent", true, page.hasContent());
+        assertEquals("Unexpected hasNextPage", true, page.hasNextPage());
+        assertEquals("Unexpected pageNumber", 1, page.getPageNumber());
+        assertEquals("Unexpected isFirstPage", true, page.isFirstPage());
+        assertEquals("Unexpected isLastPage", false, page.isLastPage());
+
         page = new TestPage(iterator, 22, 20, 100);
         assertEquals("Unexpected size", 20, page.size());
         assertEquals("Unexpected totalPages", 5, page.getTotalPages());

From a228b77b0b0e4d43c3682b68528469f77ed3d606 Mon Sep 17 00:00:00 2001
From: Ajit George 
Date: Mon, 10 Nov 2014 16:53:44 -0800
Subject: [PATCH 320/357] Changed test methods to handle exceptions for invalid
 datatypes and JSON content

---
 .../TestPOJOWithDocsStoredByOthers.java           | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithDocsStoredByOthers.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithDocsStoredByOthers.java
index 9635f563b..8a0644d3a 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithDocsStoredByOthers.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithDocsStoredByOthers.java
@@ -16,21 +16,16 @@
 import com.marklogic.client.DatabaseClient;
 import com.marklogic.client.DatabaseClientFactory;
 import com.marklogic.client.DatabaseClientFactory.Authentication;
-import com.marklogic.client.MarkLogicIOException;
+import com.marklogic.client.ResourceNotFoundException;
 import com.marklogic.client.document.DocumentManager.Metadata;
-import com.marklogic.client.document.DocumentPatchBuilder;
 import com.marklogic.client.document.DocumentWriteSet;
 import com.marklogic.client.document.JSONDocumentManager;
 import com.marklogic.client.io.DocumentMetadataHandle;
 import com.marklogic.client.io.DocumentMetadataHandle.Capability;
-import com.marklogic.client.io.DocumentMetadataHandle.DocumentCollections;
-import com.marklogic.client.io.DocumentMetadataHandle.DocumentPermissions;
-import com.marklogic.client.io.DocumentMetadataHandle.DocumentProperties;
 import com.marklogic.client.io.Format;
 import com.marklogic.client.io.JacksonHandle;
 import com.marklogic.client.pojo.PojoRepository;
 import com.marklogic.client.pojo.annotation.Id;
-import com.marklogic.javaclient.TestPOJOMissingIdGetSetMethod.SmallArtifactMissingGetter;
 
 public class TestPOJOWithDocsStoredByOthers extends BasicJavaClientREST {
 
@@ -251,7 +246,7 @@ public void validateSmallArtifact(SmallArtifactIdInSuper artifact) {
 	public void validateSmallArtifactSuper(SmallArtifactNoId artifact) {
 		assertNotNull("Artifact object should never be Null", artifact);
 		assertNotNull("Id should never be Null", artifact.getId());
-		assertEquals("Id of the object is ", -99, artifact.getId());
+		assertEquals("Id of the object is ", 0, artifact.getId());
 		assertEquals("Name of the object is ", "SmallArtifactInSuperOnly",
 				artifact.getName());
 		assertEquals("Inventory of the object is ", 1000,
@@ -333,7 +328,7 @@ public void validateSubObjReferencedbySuperClassvariableOne(SmallArtifactIdInSup
 	 * Issue 136 might solve this also.
 	 */
 
-	@Test
+	@Test(expected=IllegalArgumentException.class)
 	public void testPOJOReadDocStoredWithInvalidContent() throws Exception {
 
 		String docId[] = { "com.marklogic.javaclient.TestPOJOWithDocsStoredByOthers$SmallArtifactIdInSuper/SmallArtifactIdInSuper.json" };
@@ -420,11 +415,11 @@ public void testPOJOReadDocStoredWithNoBeanProperty() throws Exception {
 	 * Purpose : This test is to validate read documents with valid POJO
 	 * specific URI and has invalid data-types for one of the bean property.
 	 * Uses SmallArtifact class which has @Id on the name methods. Test result
-	 * expectations are: read should return exception.
+	 * expectations are: read should return ResourceNotFoundException exception.
 	 * Field inventory has a String 
 	 */
 
-	@Test(/*expected=MarkLogicIOException.class*/)
+	@Test(expected=ResourceNotFoundException.class)
 	public void testPOJOReadDocStoredWithInvalidDataType() throws Exception {
 
 		String docId[] = { "com.marklogic.javaclient.TestPOJOWithDocsStoredByOthers$SmallArtifact/SmallArtifact.json" };

From 9f14e339929988c1cd4eb988f48295d90358c941 Mon Sep 17 00:00:00 2001
From: skottam 
Date: Mon, 10 Nov 2014 17:21:43 -0800
Subject: [PATCH 321/357] updated with a test for an issue 107

---
 .../javaclient/TestBulkReadSample1.java       | 71 +++++++++++++++----
 1 file changed, 56 insertions(+), 15 deletions(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadSample1.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadSample1.java
index e4e1e2d76..986df7efe 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadSample1.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadSample1.java
@@ -1,7 +1,6 @@
 package com.marklogic.javaclient;
 
 import java.io.File;
-
 import java.util.HashMap;
 
 import com.marklogic.client.DatabaseClient;
@@ -18,7 +17,6 @@
 import com.marklogic.client.io.Format;
 import com.marklogic.client.io.StringHandle;
 import com.marklogic.client.io.DOMHandle;
-
 import com.marklogic.client.io.JacksonHandle;
 
 
@@ -27,6 +25,7 @@
 import com.fasterxml.jackson.databind.ObjectMapper;
 
 import org.junit.*;
+import org.junit.runners.MethodSorters;
 
 import static org.junit.Assert.*;
 
@@ -41,7 +40,7 @@
  */
 
 
-
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
 public class TestBulkReadSample1 extends BasicJavaClientREST  {
 
 	private static final int BATCH_SIZE=100;
@@ -80,7 +79,7 @@ public  void testCleanUp() throws Exception
      * Test Bulk Read to see you can read all the documents?
      */
 	@Test  
-	public void testReadMultipleTextDoc() 
+	public void test1ReadMultipleTextDoc() 
 	  {
 		int count=1;
 	    TextDocumentManager docMgr = client.newTextDocumentManager();
@@ -116,7 +115,7 @@ public void testReadMultipleTextDoc()
     * Verified by reading individual documents
     */
 @Test  
-	public void testReadMultipleXMLDoc() throws Exception  
+	public void test2ReadMultipleXMLDoc() throws Exception  
 	  {
 		int count=1;
 	    XMLDocumentManager docMgr = client.newXMLDocumentManager();
@@ -158,7 +157,7 @@ public void testReadMultipleXMLDoc() throws Exception
      * 
 	 */
 	@Test 
-	public void testReadMultipleBinaryDoc() throws Exception  
+	public void test3ReadMultipleBinaryDoc() throws Exception  
 	  {
 		 String docId[] = {"Sega-4MB.jpg"};
 		 int count=1;
@@ -213,7 +212,7 @@ public void testReadMultipleBinaryDoc() throws Exception
 	 * This test has a bug logged in github with tracking Issue#33
  */
 	@Test
-	public void testWriteMultipleJSONDocs() throws Exception  
+	public void test4WriteMultipleJSONDocs() throws Exception  
 	  {
 		 int count=1;	 
  		 JSONDocumentManager docMgr = client.newJSONDocumentManager();
@@ -257,13 +256,16 @@ public void testWriteMultipleJSONDocs() throws Exception
    
 
 	    }
+	
+	
+
 /*
  * This test uses GenericManager to load all different document types
  * This test has a bug logged in github with tracking Issue#33
  * 
  */
 @Test 
-	public void testWriteGenericDocMgr() throws Exception  
+	public void test5WriteGenericDocMgr() throws Exception  
 	  {
 		
 		GenericDocumentManager docMgr = client.newDocumentManager();
@@ -281,13 +283,7 @@ public void testWriteGenericDocMgr() throws Exception
 		  }
 //		 for(String uri:uris){System.out.println(uri);}
 		  DocumentPage page = docMgr.read(uris);
-		  if(!page.hasNext()){
-			testReadMultipleTextDoc();
-			testReadMultipleXMLDoc();
-			testReadMultipleBinaryDoc();
-			testWriteMultipleJSONDocs();
-			page = docMgr.read(uris);
-			}
+		
 		  while(page.hasNext()){
 		    	DocumentRecord rec = page.next();
 		     	switch(rec.getFormat())
@@ -307,6 +303,51 @@ public void testWriteGenericDocMgr() throws Exception
 		 assertEquals("binary document count", 25,countJpg);
 		 assertEquals("Json document count", 25,countJson);
 	    }
+//test for Issue# 107
+@Test 
+public void test6CloseMethodforReadMultipleDoc() throws Exception  
+  {
+	 int count=1;	
+	 DocumentPage page;
+	 JSONDocumentManager docMgr = client.newJSONDocumentManager();
+	 DocumentWriteSet writeset =docMgr.newWriteSet();
+	 
+	 HashMap map= new HashMap();
+	for(int j=0;j<102;j++){ 
+	 for(int i =0;i<10;i++){
+	 JsonNode jn = new ObjectMapper().readTree("{\"animal"+i+"\":\"dog"+i+"\", \"says\":\"woof\"}");
+	 JacksonHandle jh = new JacksonHandle();
+	 jh.set(jn);
+	 writeset.add(DIRECTORY+j+"/dog"+i+".json",jh);
+	 map.put(DIRECTORY+j+"/dog"+i+".json", jn.toString());
+	   } 
+	    docMgr.write(writeset);
+	    writeset = docMgr.newWriteSet();
+	 }
+	 
+	 String uris[] = new String[100];
+	 for(int j=0;j<102;j++){ 
+	 for(int i =0;i<10;i++){
+		 uris[i]=DIRECTORY+j+"/dog"+i+".json";
+	 }
+	 count=0;
+	 page = docMgr.read(uris);
+	
+	 DocumentRecord rec;
+	 JacksonHandle jh = new JacksonHandle();
+	 while(page.hasNext()){
+    	rec = page.next();
+    	validateRecord(rec,Format.JSON);
+    	rec.getContent(jh);
+    	assertEquals("Comparing the content :",map.get(rec.getUri()),jh.get().toString());
+    	count++;
+  } page.close();
+//	 validateRecord(rec,Format.JSON);
+ assertEquals("document count", 10,count);
+	 }
+
+  }
+
 	@AfterClass
 	public static void tearDown() throws Exception
 	{

From ca5c66183f95fbbe418bd2fb86559bebc8c87a22 Mon Sep 17 00:00:00 2001
From: skottam 
Date: Mon, 10 Nov 2014 17:43:13 -0800
Subject: [PATCH 322/357] updated with a test for an issue 84

---
 .../javaclient/TestBulkSearchWithStrucQueryDef.java    | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStrucQueryDef.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStrucQueryDef.java
index de670262e..617e5239b 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStrucQueryDef.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStrucQueryDef.java
@@ -247,21 +247,21 @@ public void testBulkSearchSQDwithJSONResponseFormat() throws Exception{
 
 		//		System.out.println(jh.get().toString());
 		assertTrue("Searh response has entry for facets",jh.get().has("facets"));
-		assertTrue("Searh response has entry for facets",jh.get().has("results"));//Issue 84 is tracking this
-		assertTrue("Searh response has entry for facets",jh.get().has("metrics"));
+		assertFalse("Searh response has entry for facets",jh.get().has("results"));//Issue 84 is tracking this
+		assertFalse("Searh response has entry for facets",jh.get().has("metrics"));
 
 		docMgr.setSearchView(QueryView.RESULTS);
 		docMgr.search(qd, 1,jh);
 
 		assertFalse("Searh response has entry for facets",jh.get().has("facets"));
 		assertTrue("Searh response has entry for facets",jh.get().has("results"));
-		assertTrue("Searh response has entry for facets",jh.get().has("metrics"));//Issue 84 is tracking this
+		assertFalse("Searh response has entry for facets",jh.get().has("metrics"));//Issue 84 is tracking this
 
 		docMgr.setSearchView(QueryView.METADATA);
 		docMgr.search(qd, 1,jh);
 
 		assertFalse("Searh response has entry for facets",jh.get().has("facets"));
-		assertTrue("Searh response has entry for facets",jh.get().has("results"));
+		assertFalse("Searh response has entry for facets",jh.get().has("results"));
 		assertTrue("Searh response has entry for facets",jh.get().has("metrics"));
 
 		docMgr.setSearchView(QueryView.ALL);
@@ -318,7 +318,7 @@ public void testBulkSearchSQDwithTransactionsandDOMHandle() throws Exception{
 		finally{t.rollback();}
 
 		docMgr.search(qd, 1,results);
-		assertEquals("Total search results after rollback are ","0",results.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total").getNodeValue());
+		assertNull("Total search results after rollback are ",results.get().getElementsByTagNameNS("*", "response").item(0).getAttributes().getNamedItem("total"));
 
 	}
 	//This test is to verify RAW XML structured Query

From afac625023a85423382db482558d769b3749d492 Mon Sep 17 00:00:00 2001
From: skottam 
Date: Mon, 10 Nov 2014 18:13:05 -0800
Subject: [PATCH 323/357] updated with a test for an issue 78

---
 .../javaclient/TestBulkSearchWithStringQueryDef.java          | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java
index 506058790..d53bc3dc4 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkSearchWithStringQueryDef.java
@@ -180,7 +180,7 @@ public void testBulkSearchSQDwithDifferentPageSizes() {
 		assertFalse("Is this Last page :",page.isLastPage());
 		assertTrue("Is this First page has content:",page.hasContent());
 		//		Need the Issue #75 to be fixed  
-		assertTrue("Is first page has previous page ?",page.hasPreviousPage());
+		assertFalse("Is first page has previous page ?",page.hasPreviousPage());
 		//		
 		long pageNo=1;
 		do{
@@ -206,7 +206,7 @@ public void testBulkSearchSQDwithDifferentPageSizes() {
 			//			assertEquals("Page Number #",pageNo,page.getPageNumber());
 			pageNo = pageNo + page.getPageSize();
 		}while(!page.isLastPage());
-		assertTrue("page count is 101 ",pageNo == page.getTotalPages());
+//		assertTrue("page count is 101 ",pageNo == page.getTotalPages());
 		assertTrue("Page has previous page ?",page.hasPreviousPage());
 		assertEquals("page size", 1,page.getPageSize());
 		assertEquals("document count", 101,page.getTotalSize());

From 0a4fce534254908774a8e5d40cba56461f7236ca Mon Sep 17 00:00:00 2001
From: skottam 
Date: Mon, 10 Nov 2014 19:01:33 -0800
Subject: [PATCH 324/357] updated with a test key

---
 .../javaclient/TestRawCombinedQuery.java      | 30 +++++++++----------
 1 file changed, 15 insertions(+), 15 deletions(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQuery.java b/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQuery.java
index a65d66fc9..87252187f 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQuery.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestRawCombinedQuery.java
@@ -27,7 +27,8 @@
 
 import org.custommonkey.xmlunit.exceptions.XpathException;
 import org.junit.*;
-
+import org.junit.runners.MethodSorters;
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
 public class TestRawCombinedQuery extends BasicJavaClientREST {
 	private static String dbName = "TestRawCombinedQueryDB";
 	private static String [] fNames = {"TestRawCombinedQueryDB-1"};
@@ -101,7 +102,7 @@ public void testBug22353() throws IOException, ParserConfigurationException, SAX
 	}
 
 	@Test	
-	public void testRawCombinedQueryXML() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException
+	public void test1RawCombinedQueryXML() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException
 	{	
 		System.out.println("Running testRawCombinedQueryXML");
 
@@ -153,7 +154,7 @@ public void testRawCombinedQueryXML() throws IOException, ParserConfigurationExc
 	}
 
 	@Test	
-	public void testRawCombinedQueryXMLWithOptions() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException
+	public void test2RawCombinedQueryXMLWithOptions() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException
 	{	
 		System.out.println("Running testRawCombinedQueryXMLWithOptions");
 
@@ -205,7 +206,7 @@ public void testRawCombinedQueryXMLWithOptions() throws IOException, ParserConfi
 	}
 
 	@Test	
-	public void testRawCombinedQueryXMLWithOverwriteOptions() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException
+	public void test3RawCombinedQueryXMLWithOverwriteOptions() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException
 	{	
 		System.out.println("Running testRawCombinedQueryXMLWithOverwriteOptions");
 
@@ -256,7 +257,7 @@ public void testRawCombinedQueryXMLWithOverwriteOptions() throws IOException, Pa
 	}
 
 	@Test	
-	public void testRawCombinedQueryJSONWithOverwriteOptions() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException
+	public void test4RawCombinedQueryJSONWithOverwriteOptions() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException
 	{	
 		System.out.println("Running testRawCombinedQueryJSONWithOverwriteOptions");
 
@@ -311,7 +312,7 @@ public void testRawCombinedQueryJSONWithOverwriteOptions() throws IOException, P
 	}
 
 	@Test	
-	public void testRawCombinedQueryJSON() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException
+	public void test5RawCombinedQueryJSON() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException
 	{	
 		System.out.println("Running testRawCombinedQueryJSON");
 
@@ -363,7 +364,7 @@ public void testRawCombinedQueryJSON() throws IOException, ParserConfigurationEx
 	}
 
 	@Test	
-	public void testRawCombinedQueryWildcard() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException
+	public void test6RawCombinedQueryWildcard() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException
 	{	
 		System.out.println("Running testRawCombinedQueryWildcard");
 
@@ -416,7 +417,7 @@ public void testRawCombinedQueryWildcard() throws IOException, ParserConfigurati
 	}
 
 	@Test	
-	public void testRawCombinedQueryCollection() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException
+	public void test7RawCombinedQueryCollection() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException
 	{	
 		System.out.println("Running testRawCombinedQueryCollection");
 
@@ -479,7 +480,7 @@ public void testRawCombinedQueryCollection() throws IOException, ParserConfigura
 	}
 
 	@Test	
-	public void testRawCombinedQueryCombo() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException
+	public void test8RawCombinedQueryCombo() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException
 	{	
 		System.out.println("Running testRawCombinedQueryCombo");
 
@@ -542,7 +543,7 @@ public void testRawCombinedQueryCombo() throws IOException, ParserConfigurationE
 	}
 
 	@Test	
-	public void testRawCombinedQueryField() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException
+	public void test9RawCombinedQueryField() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException
 	{	
 		System.out.println("Running testRawCombinedQueryField");
 
@@ -587,7 +588,7 @@ public void testRawCombinedQueryField() throws IOException, ParserConfigurationE
 	}
 
 	@Test	
-	public void testRawCombinedQueryPathIndex() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException
+	public void test10RawCombinedQueryPathIndex() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException
 	{	
 		System.out.println("Running testRawCombinedQueryPathIndex");
 
@@ -630,7 +631,7 @@ public void testRawCombinedQueryPathIndex() throws IOException, ParserConfigurat
 	}
 
 	@Test	
-	public void testRawCombinedQueryComboJSON() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException
+	public void test11RawCombinedQueryComboJSON() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException
 	{	
 		System.out.println("Running testRawCombinedQueryComboJSON");
 
@@ -694,15 +695,14 @@ public void testRawCombinedQueryComboJSON() throws IOException, ParserConfigurat
 		String resultDoc = resultsHandle.get();
 
 		System.out.println(resultDoc);
-
-		assertTrue("Returned result is not correct", resultDoc.contains(""));
+		assertTrue("Returned result is not correct", resultDoc.contains(""));
 
 		// release client
 		client.release();		
 	}
 
 	@Test	
-	public void testRawCombinedQueryFieldJSON() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException
+	public void test12RawCombinedQueryFieldJSON() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException
 	{	
 		System.out.println("Running testRawCombinedQueryFieldJSON");
 

From fe0ce23665a29cbe73358ab654f8b2c19702fe2e Mon Sep 17 00:00:00 2001
From: Ajit George 
Date: Tue, 11 Nov 2014 12:51:23 -0800
Subject: [PATCH 325/357] Changes to the class as per reolution in Git #138

---
 .../javaclient/TestPOJOMissingIdGetSetMethod.java  | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOMissingIdGetSetMethod.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOMissingIdGetSetMethod.java
index 941133ae2..cf6ab6fae 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOMissingIdGetSetMethod.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOMissingIdGetSetMethod.java
@@ -212,11 +212,11 @@ public void validateMissingArtifactGetter(SmallArtifactMissingGetter artifact) {
 	/*
 	 * This method is used when there is a need to validate SmallArtifactMissingSetter.
 	 */
-	public void validateMissingArtifactSetter(SmallArtifactMissingSetter artifact) {
+	public void validateMissingArtifactSetter(SmallArtifactMissingSetter artifact, String name) {
 		assertNotNull("Artifact object should never be Null", artifact);
 		assertNotNull("Id should never be Null", artifact.id);
 		assertEquals("Id of the object is ", -99, artifact.getId());
-		assertEquals("Name of the object is ", "SmallArtifact",
+		assertEquals("Name of the object is ", name,
 				artifact.getName());
 		assertEquals("Inventory of the object is ", 1000,
 				artifact.getInventory());
@@ -250,7 +250,7 @@ public void testPOJOReadMissingSetter() throws Exception {
 		String docId[] = { "com.marklogic.javaclient.TestPOJOMissingIdGetSetMethod$SmallArtifactMissingSetter/SmallArtifactMissingSetter.json" };
 		String json1 = new String(
 				"{\"com.marklogic.javaclient.TestPOJOMissingIdGetSetMethod$SmallArtifactMissingSetter\":"
-						+ "{\"name\": \"SmallArtifact\",\"id\": -99, \"inventory\": 1000}}");
+						+ "{\"name\": \"SmallArtifactMissingSetter\",\"id\": -99, \"inventory\": 1000}}");
 		JSONDocumentManager docMgr = client.newJSONDocumentManager();
 		docMgr.setMetadataCategories(Metadata.ALL);
 		DocumentWriteSet writeset = docMgr.newWriteSet();
@@ -275,10 +275,10 @@ public void testPOJOReadMissingSetter() throws Exception {
 
 		PojoRepository pojoReposSmallArtifact = client
 				.newPojoRepository(SmallArtifactMissingSetter.class, String.class);
-		String artifactName = new String("SmallArtifact");
+		String artifactName = new String("SmallArtifactMissingSetter");
 
 		SmallArtifactMissingSetter artifact1 = pojoReposSmallArtifact.read(artifactName);
-		validateMissingArtifactSetter(artifact1);
+		validateMissingArtifactSetter(artifact1, artifactName);
 	}
 
 	/*
@@ -372,7 +372,7 @@ public void testPOJOWriteReadMissingSetter() throws Exception {
 		
 		PojoRepository pojoReposSmallArtifact = client
 				.newPojoRepository(SmallArtifactMissingSetter.class, String.class);
-		String artifactName = new String("SmallArtifact");
+		String artifactName = new String("SmallArtifactMissingSetter");
 
 		SmallArtifactMissingSetter art = new SmallArtifactMissingSetter();
 		art.setId(-99);
@@ -383,7 +383,7 @@ public void testPOJOWriteReadMissingSetter() throws Exception {
 		pojoReposSmallArtifact.write(art,"com.marklogic.javaclient.TestPOJOMissingIdGetSetMethod$SmallArtifactMissingSetter/SmallArtifactMissingSetter.json");
 						
 		SmallArtifactMissingSetter artifact1 = pojoReposSmallArtifact.read(artifactName);
-		validateMissingArtifactSetter(artifact1);	
+		validateMissingArtifactSetter(artifact1, artifactName);	
 	}
 	
 	/*

From f7519a653e3f0192f28dacff1c6f97a12c498521 Mon Sep 17 00:00:00 2001
From: skottam 
Date: Tue, 11 Nov 2014 14:30:09 -0800
Subject: [PATCH 326/357] updated with a test key

---
 .../java/com/marklogic/javaclient/TestPOJOReadWrite1.java     | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOReadWrite1.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOReadWrite1.java
index e5038692d..e31d2953d 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOReadWrite1.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOReadWrite1.java
@@ -223,11 +223,11 @@ public void testPOJOWriteWithPojoPageReadAll() {
 		System.out.println("Total number of estimated results:"+p.getTotalSize());
 		assertEquals("Total number of estimated pages :",111,p.getTotalPages());
 		System.out.println("Total number of estimated pages :"+p.getTotalPages());
-		assertFalse("Is this First page :",p.isFirstPage());//this is bug
+		assertTrue("Is this First page :",p.isFirstPage());
 		assertFalse("Is this Last page :",p.isLastPage());
 		assertTrue("Is this First page has content:",p.hasContent());
 		//		Need the Issue #75 to be fixed  
-		assertTrue("Is first page has previous page ?",p.hasPreviousPage());
+		assertFalse("Is first page has previous page ?",p.hasPreviousPage());
 		long pageNo=1,count=0;
 		do{
 			count=0;

From 5e84391c256eb98d5e4dc5a64f590cc3b39d719f Mon Sep 17 00:00:00 2001
From: sammefford 
Date: Tue, 11 Nov 2014 15:40:51 -0700
Subject: [PATCH 327/357] fix #137--finish transition to allow @Id annotation
 of setter methods

---
 .../client/impl/PojoRepositoryImpl.java       | 34 +++++++++++++++----
 1 file changed, 27 insertions(+), 7 deletions(-)

diff --git a/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java b/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java
index 7e5f56055..58e4428ce 100644
--- a/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java
+++ b/src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java
@@ -64,6 +64,7 @@
 public class PojoRepositoryImpl
     implements PojoRepository
 {
+    private static final Pattern getterPattern = Pattern.compile("^(get|is)(.)(.*)");
     private final String EXTENSION = ".json";
 
     private DatabaseClient client;
@@ -416,6 +417,14 @@ private void findId() {
                         idMethod = getter;
                         break;
                     }
+                    if ( property.hasSetter() ) {
+                        Method setter = property.getSetter().getAnnotated();
+                        if ( setter.getAnnotation(Id.class) != null ) {
+                            idPropertyName = property.getName();
+                            idMethod = getter;
+                            break;
+                        }
+                    }
                 }
                 // setter only doesn't work because it gives us no value accessor
             }
@@ -427,26 +436,37 @@ private void findId() {
                 if ( method.isAnnotationPresent(Id.class) ) {
                     Class[] parameters = method.getParameterTypes();
                     if ( ! Modifier.isPublic(method.getModifiers()) ) {
-                        throw new IllegalStateException("Your getter method, " + method.getName() +
+                        throw new IllegalStateException("Your method, " + method.getName() +
                             ", annotated with com.marklogic.client.pojo.annotation.Id " + 
                             " must be public");
                     }
                     if ( parameters == null || parameters.length == 0 ) {
-                        Pattern pattern = Pattern.compile("^(get|is)(.)(.*)");
-                        Matcher matcher = pattern.matcher(method.getName());
+                        Matcher matcher = getterPattern.matcher(method.getName());
                         if ( matcher.matches() ) {
                             idPropertyName = matcher.group(2).toLowerCase() + matcher.group(3);
                             idMethod = method;
                             break;
                         } else {
-                            throw new IllegalStateException("Your getter method, " + method.getName() +
+                            throw new IllegalStateException("Your no-args method, " + method.getName() +
                                 ", annotated with com.marklogic.client.pojo.annotation.Id " + 
                                 " must be a proper getter method and begin with \"get\" or \"is\"");
                         }
                     } else {
-                        throw new IllegalStateException("Your getter method, " + method.getName() +
-                            ", annotated with com.marklogic.client.pojo.annotation.Id " + 
-                            " must not require any arguments");
+                        Matcher getterMatcher = getterPattern.matcher(method.getName());
+                        if ( getterMatcher.matches() ) {
+                            throw new IllegalStateException("Your getter method, " + method.getName() +
+                                ", annotated with com.marklogic.client.pojo.annotation.Id " + 
+                                " must not require any arguments");
+                        } else if ( method.getName().startsWith("set") ) {
+                            throw new MarkLogicInternalException("Your setter method, " + method.getName() +
+                                ", annotated with com.marklogic.client.pojo.annotation.Id " +
+                                "was not found by Jackson for some reason.  Please report this to " +
+                                "MarkLogic support.");
+                        } else {
+                            throw new IllegalStateException("Your setter method, " + method.getName() +
+                                ", annotated with com.marklogic.client.pojo.annotation.Id " + 
+                                " must be a proper setter method (beginning with \"set\")");
+                        }
                     }
                 }
             }

From 6fb98a81fcb9e4444cc3c3bea04a4037d132ed7a Mon Sep 17 00:00:00 2001
From: Ajit George 
Date: Wed, 12 Nov 2014 09:44:11 -0800
Subject: [PATCH 328/357] Adding Uber functionality on transformations tests

---
 .../javaclient/TestBulkWriteWithTransformations.java        | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteWithTransformations.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteWithTransformations.java
index 2208fc213..9c37bf2d8 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteWithTransformations.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkWriteWithTransformations.java
@@ -40,6 +40,8 @@ public class TestBulkWriteWithTransformations extends BasicJavaClientREST{
 	private static String restServerName = "REST-Java-Client-API-Server";
 	private static int restPort = 8011;
 	private  DatabaseClient client ;
+	// Additional port to test for Uber port
+    private static int uberPort = 8000;
 
 	@BeforeClass
 	public static void setUpBeforeClass() throws Exception {
@@ -58,7 +60,9 @@ public static void tearDownAfterClass() throws Exception {
 	public void setUp() throws Exception {
 //		 System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug");
 		// create new connection for each test below
-		client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST);
+		createUserRolesWithPrevilages("test-eval","xdbc:eval", "xdbc:eval-in","xdmp:eval-in","any-uri","xdbc:invoke");
+	    createRESTUser("eval-user", "x", "test-eval","rest-admin","rest-writer","rest-reader");
+		client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST);
 	}
 
 	@After

From a3fb117906ee5ce7543344b1dfc5f131572f1779 Mon Sep 17 00:00:00 2001
From: Ajit George 
Date: Wed, 12 Nov 2014 10:25:02 -0800
Subject: [PATCH 329/357] Fixes for Git issue 137

---
 .../TestPOJOMissingIdGetSetMethod.java        | 33 ++++---------------
 1 file changed, 6 insertions(+), 27 deletions(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOMissingIdGetSetMethod.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOMissingIdGetSetMethod.java
index cf6ab6fae..f71e93901 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOMissingIdGetSetMethod.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOMissingIdGetSetMethod.java
@@ -154,8 +154,6 @@ public void setInventory(int inventory) {
 
 	@BeforeClass
 	public static void setUpBeforeClass() throws Exception {
-		// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire",
-		// "debug");
 		System.out.println("In setup");
 		setupJavaRESTServer(dbName, fNames[0], restServerName, restPort);
 	}
@@ -239,9 +237,6 @@ public void validateMissingArtifactGetSet(SmallArtifactMissGetSet artifact) {
 	 * Purpose : This test is to validate read documents stored with JacksonHandle with valid POJO
 	 * specific URI. Uses SmallArtifactMissingSetter class
 	 * which has @Id only on the setter method. 
-	 * 
-	 * Current results (10/13/2014) are: read returns a null
-	 * 	 
 	 */
 
 	@Test
@@ -284,12 +279,7 @@ public void testPOJOReadMissingSetter() throws Exception {
 	/*
 	 * Purpose : This test is to validate read documents stored with JacksonHandle with valid POJO
 	 * specific URI. Uses SmallArtifactMissingGetter class
-	 * which has @Id only on the setter method. 
-	 * 
-	 * Current results (10/13/2014) are: 
-	 * java.lang.IllegalArgumentException: Your getter method, setName, annotated with 
-	 * com.marklogic.client.pojo.annotation.Id must not require any arguments.
-	 * Issue 136 might solve this also.
+	 * which has @Id only on the setter method.
 	 */
 
 	@Test
@@ -332,12 +322,7 @@ public void testPOJOReadMissingGetter() throws Exception {
 	/*
 	 * Purpose : This test is to validate read documents with valid POJO
 	 * specific URI. Uses SmallArtifactMissingGetter class
-	 * which has @Id only on the setter method. 
-	 * 
-	 * Current results (10/13/2014) are: 
-	 * java.lang.IllegalArgumentException: Your getter method, setName, annotated with 
-	 * com.marklogic.client.pojo.annotation.Id must not require any arguments.
-	 * Issue 136 might solve this also.
+	 * which has @Id only on the setter method.
 	 */
 	@Test
 	public void testPOJOWriteReadMissingGetter() throws Exception {
@@ -347,7 +332,7 @@ public void testPOJOWriteReadMissingGetter() throws Exception {
 		String artifactName = new String("SmallArtifact");
 
 		SmallArtifactMissingGetter art = new SmallArtifactMissingGetter();
-		art.setId(0);
+		art.setId(-99L);
 		art.setInventory(1000);
 		art.setName(artifactName);
 		
@@ -361,10 +346,7 @@ public void testPOJOWriteReadMissingGetter() throws Exception {
 	/*
 	 * Purpose : This test is to validate read documents stored with valid POJO
 	 * specific URI. Uses SmallArtifactMissingSetter class
-	 * which has @Id only on the setter method. 
-	 * 
-	 * Current results (10/13/2014) are: Works fine
-	 * 	 
+	 * which has @Id only on the setter method.
 	 */
 
 	@Test
@@ -375,7 +357,7 @@ public void testPOJOWriteReadMissingSetter() throws Exception {
 		String artifactName = new String("SmallArtifactMissingSetter");
 
 		SmallArtifactMissingSetter art = new SmallArtifactMissingSetter();
-		art.setId(-99);
+		art.setId(-99L);
 		art.setInventory(1000);
 		art.setName(artifactName);
 		
@@ -389,10 +371,7 @@ public void testPOJOWriteReadMissingSetter() throws Exception {
 	/*
 	 * Purpose : This test is to validate read documents stored with JacksonHandle with valid POJO
 	 * specific URI. Uses SmallArtifactMissGetSet class
-	 * which has no @Id on any of its class members. 
-	 * 
-	 * Current results (10/13/2014) are: IllegalArgumentException
-	 * 	 
+	 * which has no @Id on any of its class members.
 	 */
 
 	@Test(expected=IllegalArgumentException.class)

From cf5733cdef9846d30dae066cc202f812d73ab8d6 Mon Sep 17 00:00:00 2001
From: Ajit George 
Date: Wed, 12 Nov 2014 13:30:44 -0800
Subject: [PATCH 330/357] Fixed TestBulkReadWriteWithJacksonDataBind.java
 regression

---
 .../TestBulkReadWriteWithJacksonDataBind.java | 65 ++++++++-----------
 1 file changed, 27 insertions(+), 38 deletions(-)

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonDataBind.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonDataBind.java
index 35e55fc74..610d36961 100644
--- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonDataBind.java
+++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonDataBind.java
@@ -4,7 +4,6 @@
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
-import java.io.File;
 import java.util.Calendar;
 
 import org.junit.After;
@@ -13,7 +12,6 @@
 import org.junit.BeforeClass;
 import org.junit.Test;
 
-import com.fasterxml.jackson.core.JsonFactory;
 import com.marklogic.client.DatabaseClient;
 import com.marklogic.client.DatabaseClientFactory;
 import com.marklogic.client.DatabaseClientFactory.Authentication;
@@ -154,61 +152,52 @@ public void validateDefaultMetadata(DocumentMetadataHandle mh){
 
 	/*
 	 * This method is place holder to test JacksonDatabindHandle handling file writing / reading (Streams)
-	 * Need to be completed once JacksonDatabindHandle has the necessary support.
+	 *
 	 */
 	@Test
 	public void testWriteMultipleJSONFiles() throws Exception {
 		
-		String docId[] = {"/apple.json","/microsoft.json","/hp.json"};
+		String docId = "/";
 		
-		// Work yet to be done
-		// Each of these files can contain multiple products. Need to Read these files and then the JSON content.
-		
-		String jsonFilename1 = "AppleProducts.json";
-		String jsonFilename2 = "MicrosoftProducts.json";
-		String jsonFilename3 = "HpProducts.json";
-				
-		File jsonFile1 = new File("src/test/java/com/marklogic/javaclient/data/" + jsonFilename1);
-		File jsonFile2 = new File("src/test/java/com/marklogic/javaclient/data/" + jsonFilename2);
-		File jsonFile3 = new File("src/test/java/com/marklogic/javaclient/data/" + jsonFilename3);
+		//These files need to be in src/test/java/com/marklogic/javaclient/data/ folder.
+		String jsonFilename1 = "product-apple.json";
+		String jsonFilename2 = "product-microsoft.json";
+		String jsonFilename3 = "product-hp.json";
 
 		JSONDocumentManager docMgr = client.newJSONDocumentManager();
 		docMgr.setMetadataCategories(Metadata.ALL);
-		DocumentWriteSet writeset = docMgr.newWriteSet();
 		// put meta-data
 		DocumentMetadataHandle mh = setMetadata();
-		DocumentMetadataHandle mhRead = new DocumentMetadataHandle();
-
-		JsonFactory f = new JsonFactory();	
 		
-		JacksonDatabindHandle handle1 = new JacksonDatabindHandle(File.class);
-		JacksonDatabindHandle handle2 = new JacksonDatabindHandle(File.class);
-		JacksonDatabindHandle handle3 = new JacksonDatabindHandle(File.class);
-		
-		// Add meta-data
-		writeset.addDefault(mh);
-		
-		handle1.set(jsonFile1);
-		handle2.set(jsonFile2);
-		handle3.set(jsonFile3);
-		
-		writeset.add(docId[0], handle1);
-		writeset.add(docId[1], handle2);
-		writeset.add(docId[2], handle3);
-
-		docMgr.write(writeset);
+		//Read from file system 3 files and write them into the database.
+		writeDocumentUsingOutputStreamHandle(client, jsonFilename1, docId, mh, "JSON");
+		writeDocumentUsingOutputStreamHandle(client, jsonFilename2, docId, mh, "JSON");
+		writeDocumentUsingOutputStreamHandle(client, jsonFilename3, docId, mh, "JSON");
 		
 		//Read it back into JacksonDatabindHandle Product
 		JacksonDatabindHandle handleRead = new JacksonDatabindHandle(Product.class);
 		
-		// Do we iterate individually ? 
-		docMgr.read(docId[0], handleRead);
+		// Read into JacksonDatabindHandle
+		docMgr.read(docId+jsonFilename1, handleRead);
 		Product product1 = (Product) handleRead.get();
+		
+		docMgr.read(docId+jsonFilename2, handleRead);
+		Product product2 = (Product) handleRead.get();
+		
+		docMgr.read(docId+jsonFilename3, handleRead);
+		Product product3 = (Product) handleRead.get();
 				
 		assertTrue("Did not return a iPhone 6", product1.getName().equalsIgnoreCase("iPhone 6"));
-		assertTrue("Did not return a Mobile Phone", product1.getIndustry().equalsIgnoreCase("Mobile Phone"));
-		assertTrue("Did not return a Mobile Phone", product1.getDescription().equalsIgnoreCase("New iPhone 6"));
+		assertTrue("Did not return a Mobile Phone", product1.getIndustry().equalsIgnoreCase("Mobile Hardware"));
+		assertTrue("Did not return a Mobile Phone", product1.getDescription().equalsIgnoreCase("Bending Iphone"));
+		
+		assertTrue("Did not return a iPhone 6", product2.getName().equalsIgnoreCase("Windows 10"));
+		assertTrue("Did not return a Mobile Phone", product2.getIndustry().equalsIgnoreCase("Software"));
+		assertTrue("Did not return a Mobile Phone", product2.getDescription().equalsIgnoreCase("OS Server"));
 		
+		assertTrue("Did not return a iPhone 6", product3.getName().equalsIgnoreCase("Elite Book"));
+		assertTrue("Did not return a Mobile Phone", product3.getIndustry().equalsIgnoreCase("PC Hardware"));
+		assertTrue("Did not return a Mobile Phone", product3.getDescription().equalsIgnoreCase("Very cool laptop"));		
 	}
 	
 	@Test

From fea71848ee11609bf46b8421afb4a039e7559d41 Mon Sep 17 00:00:00 2001
From: Ajit George 
Date: Wed, 12 Nov 2014 13:34:27 -0800
Subject: [PATCH 331/357]  Adding json files to support
 TestBulkReadWriteWithJacksonDataBind.java

---
 .../test/java/com/marklogic/javaclient/data/product-apple.json   | 1 +
 .../src/test/java/com/marklogic/javaclient/data/product-hp.json  | 1 +
 .../java/com/marklogic/javaclient/data/product-microsoft.json    | 1 +
 3 files changed, 3 insertions(+)
 create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/data/product-apple.json
 create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/data/product-hp.json
 create mode 100644 test-complete/src/test/java/com/marklogic/javaclient/data/product-microsoft.json

diff --git a/test-complete/src/test/java/com/marklogic/javaclient/data/product-apple.json b/test-complete/src/test/java/com/marklogic/javaclient/data/product-apple.json
new file mode 100644
index 000000000..06b9458a4
--- /dev/null
+++ b/test-complete/src/test/java/com/marklogic/javaclient/data/product-apple.json
@@ -0,0 +1 @@
+{"name":"iPhone 6","industry":"Mobile Hardware","description":"Bending Iphone"}
\ No newline at end of file
diff --git a/test-complete/src/test/java/com/marklogic/javaclient/data/product-hp.json b/test-complete/src/test/java/com/marklogic/javaclient/data/product-hp.json
new file mode 100644
index 000000000..7d2b0d7a4
--- /dev/null
+++ b/test-complete/src/test/java/com/marklogic/javaclient/data/product-hp.json
@@ -0,0 +1 @@
+{"name":"Elite Book","industry":"PC Hardware","description":"Very cool laptop"}
\ No newline at end of file
diff --git a/test-complete/src/test/java/com/marklogic/javaclient/data/product-microsoft.json b/test-complete/src/test/java/com/marklogic/javaclient/data/product-microsoft.json
new file mode 100644
index 000000000..1ce5465f3
--- /dev/null
+++ b/test-complete/src/test/java/com/marklogic/javaclient/data/product-microsoft.json
@@ -0,0 +1 @@
+{"name":"Windows 10","industry":"Software","description":"OS Server"}
\ No newline at end of file

From 1533f01933485101e6c58d6a42bea0550ff5fa19 Mon Sep 17 00:00:00 2001
From: sammefford 
Date: Wed, 12 Nov 2014 15:58:45 -0700
Subject: [PATCH 332/357] fix misspelled privilieges

---
 src/main/java/com/marklogic/client/DatabaseClient.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/main/java/com/marklogic/client/DatabaseClient.java b/src/main/java/com/marklogic/client/DatabaseClient.java
index 06dd7c1f9..b676e410d 100644
--- a/src/main/java/com/marklogic/client/DatabaseClient.java
+++ b/src/main/java/com/marklogic/client/DatabaseClient.java
@@ -107,7 +107,7 @@ public interface DatabaseClient {
      * Creates a manager for configuring the REST server for the database. The
      * ServerConfigurationManager can persist query options and transforms or
      * set properties of the server. The application must have rest-admin
-     * privilieges to use the ServerConfigurationManager.
+     * privileges to use the ServerConfigurationManager.
      * 
      * @return	a manager for the server properties or administrative resources
      */

From c208dbba7af8fe352c6d1be6674fe34b19ea6453 Mon Sep 17 00:00:00 2001
From: sammefford 
Date: Wed, 12 Nov 2014 15:59:25 -0700
Subject: [PATCH 333/357] add javadocs

---
 .../pojo/annotation/GeospatialLatitude.java   | 54 +++++++++++++++++++
 .../pojo/annotation/GeospatialLongitude.java  | 54 +++++++++++++++++++
 .../GeospatialPathIndexProperty.java          | 40 ++++++++++++++
 .../marklogic/client/pojo/annotation/Id.java  | 45 ++++++++++++++++
 .../pojo/annotation/PathIndexProperty.java    | 43 +++++++++++++++
 5 files changed, 236 insertions(+)

diff --git a/src/main/java/com/marklogic/client/pojo/annotation/GeospatialLatitude.java b/src/main/java/com/marklogic/client/pojo/annotation/GeospatialLatitude.java
index 7bee1fc89..a5370ea6f 100644
--- a/src/main/java/com/marklogic/client/pojo/annotation/GeospatialLatitude.java
+++ b/src/main/java/com/marklogic/client/pojo/annotation/GeospatialLatitude.java
@@ -18,6 +18,60 @@
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 
+/** Use this annotation in combination with {@link GeospatialLongitude} on a
+ * sibling property to specify that a Geospatial Element Pair Index should
+ * be created for these pojo properties. The value should follow the rules
+ * for any Geospatial Element Pair Index in MarkLogic Server with coordinate
+ * system "wgs84".
+ *
+ * This annotation can be associated with a public field:
+ * 
    import com.marklogic.client.pojo.annotation.GeospatialLatitude;
+ *    import com.marklogic.client.pojo.annotation.GeospatialLongitude;
+ *    public class MyClass {
+ *      {@literal @}GeospatialLatitude
+ *      public String latitude;
+ *      {@literal @}GeospatialLongitude
+ *      public String longitude;
+ *    }
+ * + * or with a public getter method: + *
+ *    public class MyClass {
+ *      private String latitude;
+ *      private String longitude;
+ *      {@literal @}GeospatialLatitude
+ *      public String getLatitude() {
+ *        return latitude;
+ *      }
+ *      {@literal @}GeospatialLongitude
+ *      public String getLongitude() {
+ *        return longitude;
+ *      }
+ *      // ... setter methods ...
+ *    }
+* + * or with a public setter method: + *
+ *    public class MyClass {
+ *      private String latitude;
+ *      private String longitude;
+ *
+ *      // ... getter methods ...
+ *
+ *      {@literal @}GeospatialLatitude
+ *      public void setLatitude(String latitude) {
+ *        this.latitude = latitude;
+ *      }
+ *      {@literal @}GeospatialLongitude
+ *      public void setLongitude(String longitude) {
+ *        this.longitude = longitude;
+ *      }
+ *    }
+ * Run + * {@link com.marklogic.client.pojo.util.GenerateIndexConfig} to generate + * a package that can be used by administrators to create the indexes in + * MarkLogic Server. + */ @Retention(RetentionPolicy.RUNTIME) public @interface GeospatialLatitude { } diff --git a/src/main/java/com/marklogic/client/pojo/annotation/GeospatialLongitude.java b/src/main/java/com/marklogic/client/pojo/annotation/GeospatialLongitude.java index 54d5c9190..468ba127f 100644 --- a/src/main/java/com/marklogic/client/pojo/annotation/GeospatialLongitude.java +++ b/src/main/java/com/marklogic/client/pojo/annotation/GeospatialLongitude.java @@ -18,6 +18,60 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +/** Use this annotation in combination with {@link GeospatialLatitude} on a + * sibling property to specify that a Geospatial Element Pair Index should + * be created for these pojo properties. The value should follow the rules + * for any Geospatial Element Pair Index in MarkLogic Server with coordinate + * system "wgs84". + * + * This annotation can be associated with a public field: + *
    import com.marklogic.client.pojo.annotation.GeospatialLatitude;
+ *    import com.marklogic.client.pojo.annotation.GeospatialLongitude;
+ *    public class MyClass {
+ *      {@literal @}GeospatialLatitude
+ *      public String latitude;
+ *      {@literal @}GeospatialLongitude
+ *      public String longitude;
+ *    }
+ * + * or with a public getter method: + *
+ *    public class MyClass {
+ *      private String latitude;
+ *      private String longitude;
+ *      {@literal @}GeospatialLatitude
+ *      public String getLatitude() {
+ *        return latitude;
+ *      }
+ *      {@literal @}GeospatialLongitude
+ *      public String getLongitude() {
+ *        return longitude;
+ *      }
+ *      // ... setter methods ...
+ *    }
+* + * or with a public setter method: + *
+ *    public class MyClass {
+ *      private String latitude;
+ *      private String longitude;
+ *
+ *      // ... getter methods ...
+ *
+ *      {@literal @}GeospatialLatitude
+ *      public void setLatitude(String latitude) {
+ *        this.latitude = latitude;
+ *      }
+ *      {@literal @}GeospatialLongitude
+ *      public void setLongitude(String longitude) {
+ *        this.longitude = longitude;
+ *      }
+ *    }
+ * Run + * {@link com.marklogic.client.pojo.util.GenerateIndexConfig} to generate + * a package that can be used by administrators to create the indexes in + * MarkLogic Server. + */ @Retention(RetentionPolicy.RUNTIME) public @interface GeospatialLongitude { } diff --git a/src/main/java/com/marklogic/client/pojo/annotation/GeospatialPathIndexProperty.java b/src/main/java/com/marklogic/client/pojo/annotation/GeospatialPathIndexProperty.java index 1dd3ac339..becaa3a81 100644 --- a/src/main/java/com/marklogic/client/pojo/annotation/GeospatialPathIndexProperty.java +++ b/src/main/java/com/marklogic/client/pojo/annotation/GeospatialPathIndexProperty.java @@ -18,6 +18,46 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +/** Use this annotation to specify that a Geospatial Path Index should + * be created for this pojo property. The value should follow the rules + * for any Geospatial Path Index in MarkLogic Server with coordinate + * system "wgs84" and point format "point". Specifically, the value + * should be in "{latititude} {longitude}" point format where {latitude} + * and {longitude} are numeric geospatial wgs84 position values. + * + * This annotation can be associated with a public field: + *
    import com.marklogic.client.pojo.annotation.GeospatialPathIndexProperty;
+ *    public class MyClass {
+ *      {@literal @}GeospatialPathIndexProperty
+ *      public String myGeoProperty;
+ *    }
+ * + * or with a public getter method: + *
+ *    public class MyClass {
+ *      private String myGeoProperty;
+ *      {@literal @}GeospatialPathIndexProperty
+ *      public String getMyGeoProperty() {
+ *        return myGeoProperty;
+ *      }
+ *      // ... setter methods ...
+ *    }
+* + * or with a public setter method: + *
+ *    public class MyClass {
+ *      private String myGeoProperty;
+ *      // ... getter methods ...
+ *      {@literal @}GeospatialPathIndexProperty
+ *      public void setMyGeoProperty(String myGeoProperty) {
+ *        this.myGeoProperty = myGeoProperty;
+ *      }
+ *    }
+ * Run + * {@link com.marklogic.client.pojo.util.GenerateIndexConfig} to generate + * a package that can be used by administrators to create the indexes in + * MarkLogic Server. + */ @Retention(RetentionPolicy.RUNTIME) public @interface GeospatialPathIndexProperty { } diff --git a/src/main/java/com/marklogic/client/pojo/annotation/Id.java b/src/main/java/com/marklogic/client/pojo/annotation/Id.java index 42cf1dfda..894146b98 100644 --- a/src/main/java/com/marklogic/client/pojo/annotation/Id.java +++ b/src/main/java/com/marklogic/client/pojo/annotation/Id.java @@ -17,7 +17,52 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import com.marklogic.client.pojo.PojoRepository; +/** Use this annotation to specify the Id property for each pojo class. + * To work properly with {@link PojoRepository#write PojoRepository.write}, + * each pojo class must have one and only one property annotated with + * the Id annotation. The property annotated with Id is used to + * generate a unique URI in MarkLogic Server + * for each persisted instance, and thus should be a property with a + * unique value for each instance.

+ * + * This annotation can be associated with a public field: + *
    import com.marklogic.client.pojo.annotation.Id;
+ *    public class MyClass {
+ *      {@literal @}Id
+ *      public Long myId;
+ *    }
+ * + * or with a public getter method: + *
+ *    public class MyClass {
+ *      private Long myId;
+ *      {@literal @}Id
+ *      public Long getMyId() {
+ *        return myId;
+ *      }
+ *      // ... setter methods ...
+ *    }
+* + * or with a public setter method: + *
+ *    public class MyClass {
+ *      private Long myId;
+ *
+ *      // ... getter methods ...
+ *
+ *      {@literal @}Id
+ *      public void setMyId(Long myId) {
+ *        this.myId = myId;
+ *      }
+ *    }
+ * + * This annotation is used only at + * runtime to generate unique uris, so there is no need to run + * {@link com.marklogic.client.pojo.util.GenerateIndexConfig} to do + * anything with this annotation. + */ @Retention(RetentionPolicy.RUNTIME) public @interface Id { } diff --git a/src/main/java/com/marklogic/client/pojo/annotation/PathIndexProperty.java b/src/main/java/com/marklogic/client/pojo/annotation/PathIndexProperty.java index 7c2b8bac1..faf2be140 100644 --- a/src/main/java/com/marklogic/client/pojo/annotation/PathIndexProperty.java +++ b/src/main/java/com/marklogic/client/pojo/annotation/PathIndexProperty.java @@ -18,6 +18,49 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +/** Use this annotation to specify that a Path Index (required for range + * queries) should be created for this pojo property. A path index is + * used rather than an element range index because we can restrict matches + * to only properties that are direct children of this class type. We + * do this by including the class name in the path specification on the + * path index. + * + * This annotation can be associated with a public field: + *
    import com.marklogic.client.pojo.annotation.PathIndexProperty;
+ *    public class MyClass {
+ *      {@literal @}PathIndexProperty
+ *      public Long myLongValue;
+ *    }
+ * + * or with a public getter method: + *
+ *    public class MyClass {
+ *      private Long myLongValue;
+ *      {@literal @}PathIndexProperty
+ *      public Long getMyLongValue() {
+ *        return myLongValue;
+ *      }
+ *      // ... setter methods ...
+ *    }
+* + * or with a public setter method: + *
+ *    public class MyClass {
+ *      private Long myLongValue;
+ *
+ *      // ... getter methods ...
+ *
+ *      {@literal @}PathIndexProperty
+ *      public void setMyLongValue(Long myLongValue) {
+ *        this.myLongValue = myLongValue;
+ *      }
+ *    }
+ * Run + * {@link com.marklogic.client.pojo.util.GenerateIndexConfig} to generate + * a package that can be used by administrators to create the indexes in + * MarkLogic Server. + * @see com.marklogic.client.pojo.PojoQueryBuilder#range + */ @Retention(RetentionPolicy.RUNTIME) public @interface PathIndexProperty { ScalarType scalarType(); From 7870573e54f8ae799a4c3271aecb032a0cdc1d4d Mon Sep 17 00:00:00 2001 From: sammefford Date: Wed, 12 Nov 2014 16:01:19 -0700 Subject: [PATCH 334/357] fix #189--handle case where pageSize=0 --- .../java/com/marklogic/client/impl/BasicPage.java | 8 +++++++- .../java/com/marklogic/client/test/PageTest.java | 12 +++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/marklogic/client/impl/BasicPage.java b/src/main/java/com/marklogic/client/impl/BasicPage.java index 64fa0642d..b0474e71e 100644 --- a/src/main/java/com/marklogic/client/impl/BasicPage.java +++ b/src/main/java/com/marklogic/client/impl/BasicPage.java @@ -81,7 +81,9 @@ public BasicPage setSize(long size) { public long size() { if ( size != null ) return size.longValue(); - if ( hasNextPage() ) { + if ( getPageSize() == 0 ) { + return 0; + } else if ( hasNextPage() ) { return getPageSize(); } else if ((getTotalSize() % getPageSize()) == 0) { return getPageSize(); @@ -91,6 +93,7 @@ public long size() { } public long getTotalPages() { + if ( getPageSize() == 0 ) return 0; return (long) Math.ceil((double) getTotalSize() / (double) getPageSize()); } @@ -107,6 +110,7 @@ public boolean hasPreviousPage() { } public long getPageNumber() { + if ( getPageSize() == 0 ) return 0; double _start = (double) start; double _pageSize = (double) getPageSize(); if ( _start % _pageSize == 0 ) return new Double(_start / _pageSize).longValue(); @@ -114,10 +118,12 @@ public long getPageNumber() { } public boolean isFirstPage() { + if ( getPageSize() == 0 ) return true; return getPageNumber() == 1; } public boolean isLastPage() { + if ( getPageSize() == 0 ) return true; return getPageNumber() == getTotalPages(); } } diff --git a/src/test/java/com/marklogic/client/test/PageTest.java b/src/test/java/com/marklogic/client/test/PageTest.java index 4fe1aeb55..f2b4ae4c6 100644 --- a/src/test/java/com/marklogic/client/test/PageTest.java +++ b/src/test/java/com/marklogic/client/test/PageTest.java @@ -54,7 +54,17 @@ public void testTestPage() { assertEquals("Unexpected pageNumber", 1, page.getPageNumber()); assertEquals("Unexpected isFirstPage", true, page.isFirstPage()); assertEquals("Unexpected isLastPage", false, page.isLastPage()); - + + page = new TestPage(iterator, 10, 0, 101); + assertEquals("Unexpected size", 0, page.size()); + assertEquals("Unexpected totalSize", 101, page.getTotalSize()); + assertEquals("Unexpected totalPages", 0, page.getTotalPages()); + assertEquals("Unexpected hasContent", false, page.hasContent()); + assertEquals("Unexpected hasNextPage", false, page.hasNextPage()); + assertEquals("Unexpected pageNumber", 0, page.getPageNumber()); + assertEquals("Unexpected isFirstPage", true, page.isFirstPage()); + assertEquals("Unexpected isLastPage", true, page.isLastPage()); + page = new TestPage(iterator, 2, 10, 100); assertEquals("Unexpected size", 10, page.size()); assertEquals("Unexpected totalPages", 10, page.getTotalPages()); From 5fad3ee96ad8dacd0f0fb0e0fa6c8b80793d7a07 Mon Sep 17 00:00:00 2001 From: skottam Date: Wed, 12 Nov 2014 15:11:20 -0800 Subject: [PATCH 335/357] updated test to have filtered query --- .../TestPOJOQueryBuilderValueQuery.java | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderValueQuery.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderValueQuery.java index 30ce212a5..f5feae445 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderValueQuery.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderValueQuery.java @@ -33,16 +33,16 @@ public class TestPOJOQueryBuilderValueQuery extends BasicJavaClientREST { @BeforeClass public static void setUpBeforeClass() throws Exception { - // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); +// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); - BasicJavaClientREST.setDatabaseProperties(dbName, "trailing-wildcard-searches", true); +// setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); +// BasicJavaClientREST.setDatabaseProperties(dbName, "trailing-wildcard-searches", true); } @AfterClass public static void tearDownAfterClass() throws Exception { System.out.println("In tear down" ); - tearDownJavaRESTServer(dbName, fNames, restServerName); +// tearDownJavaRESTServer(dbName, fNames, restServerName); } @Before public void setUp() throws Exception { @@ -159,15 +159,15 @@ public void testPOJOValueSearchWithStrings() { PojoRepository products = client.newPojoRepository(Artifact.class, Long.class); PojoPage p; this.loadSimplePojos(products); - String[] searchOptions ={"case-sensitive","wildcarded","min-occurs=2"}; + String[] searchOptions ={"case-sensitive","wildcarded","min-occurs=1"}; PojoQueryBuilder qb = products.getQueryBuilder(); - String[] searchNames = {"Acme spe*","Widgets spe*"}; - PojoQueryDefinition qd = qb.value("name",searchOptions,100.0,searchNames); + String[] searchNames = {"Acme spe* *","Widgets spe* *"}; + PojoQueryDefinition qd =qb.filteredQuery(qb.value("name",searchOptions,100.0,searchNames)); JacksonHandle jh = new JacksonHandle(); products.setPageLength(5); p = products.search(qd, 1,jh); - assertEquals("total no of pages",3,p.getTotalPages()); +// assertEquals("total no of pages",3,p.getTotalPages()); since page methods are estimates System.out.println(jh.get().toString()); long pageNo=1,count=0; do{ @@ -177,16 +177,15 @@ public void testPOJOValueSearchWithStrings() { Artifact a =p.next(); validateArtifact(a); assertTrue("Verifying document id is part of the search ids",a.getId()%5==0); + assertTrue("Verifying document name has Acme/Wigets special",a.getManufacturer().getName().contains("Acme special")||a.getManufacturer().getName().contains("Widgets special")); count++; System.out.println(a.getId()); } assertEquals("Page size",count,p.size()); pageNo=pageNo+p.getPageSize(); }while(!p.isLastPage() && pageNo<=p.getTotalSize()); - assertEquals("page number after the loop",3,p.getPageNumber()); - assertEquals("total no of pages",3,p.getTotalPages()); - assertEquals("page length from search handle",5,jh.get().path("page-length").asInt()); - assertEquals("Total results from search handle",11,jh.get().path("total").asInt()); + assertEquals("page length from search handle",5,jh.get().path("results").size()); +// assertEquals("Total results from search handle",11,jh.get().path("total").asInt()); } //Below scenario is verifying value query from PojoBuilder that matches to no document //Issue 127 is logged for the below scenario @@ -199,7 +198,7 @@ public void testPOJOValueSearchWithNoResults() throws Exception { String[] searchOptions ={"case-sensitive","wildcarded","min-occurs=2"}; PojoQueryBuilder qb = products.getQueryBuilder(); String[] searchNames = {"acme*"}; - PojoQueryDefinition qd = qb.value("name",searchOptions,100.0,searchNames); + PojoQueryDefinition qd =qb.filteredQuery( qb.value("name",searchOptions,100.0,searchNames)); JacksonHandle jh = new JacksonHandle(); products.setPageLength(5); p = products.search(qd, 1,jh); @@ -272,21 +271,21 @@ public void testPOJOWordSearchWithOptions() throws Exception { PojoPage p; this.loadSimplePojos(products); setupServerRequestLogging(client,true); - String[] searchOptions ={"case-sensitive","wildcarded","min-occurs=2"}; + String[] searchOptions ={"case-sensitive","wildcarded","min-occurs=1"}; PojoQueryBuilder qb = products.getQueryBuilder(); String[] searchNames = {"count*"}; - PojoQueryDefinition qd = qb.word("name",searchOptions,0.0,searchNames); + PojoQueryDefinition qd = qb.filteredQuery(qb.word("name",searchOptions,0.0,searchNames)); JacksonHandle jh = new JacksonHandle(); products.setPageLength(5); p = products.search(qd, 1,jh); setupServerRequestLogging(client,false); System.out.println(jh.get().toString()); - assertEquals("total no of pages",3,p.getTotalPages()); +// assertEquals("total no of pages",3,p.getTotalPages()); long pageNo=1,count=0; do{ count =0; - p = products.search(qd,pageNo); + p = products.search(qd,pageNo,jh); while(p.hasNext()){ Artifact a =p.next(); validateArtifact(a); @@ -295,13 +294,13 @@ public void testPOJOWordSearchWithOptions() throws Exception { count++; } + System.out.println(jh.get().toString()); assertEquals("Page size",count,p.size()); pageNo=pageNo+p.getPageSize(); }while(!p.isLastPage() && pageNo<=p.getTotalSize()); assertEquals("page number after the loop",3,p.getPageNumber()); assertEquals("total no of pages",3,p.getTotalPages()); assertEquals("page length from search handle",5,jh.get().path("page-length").asInt()); - assertEquals("Total results from search handle",11,jh.get().path("total").asInt()); - } + } } From e449e31e28c0f9d90e41f69ad2f211f89ff51fd4 Mon Sep 17 00:00:00 2001 From: sammefford Date: Wed, 12 Nov 2014 16:36:49 -0700 Subject: [PATCH 336/357] update javadoc to emphasize that getTotalSize is an estimate and getPageNumber is based on that estimate and thus may change as the estimate is updated to be more accurate --- src/main/java/com/marklogic/client/Page.java | 43 +++++++++++++++----- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/marklogic/client/Page.java b/src/main/java/com/marklogic/client/Page.java index e7fb260e4..830cd3862 100644 --- a/src/main/java/com/marklogic/client/Page.java +++ b/src/main/java/com/marklogic/client/Page.java @@ -46,10 +46,20 @@ public interface Page extends Iterable { * @return the page size */ public long getPageSize(); - /** The total count (potentially an estimate) of all possible items in the set. - * For result sets this is the number of items within the result set. - * For search result sets this is the estimated number of matching items. - * @return the total count of possible items */ + /** The total count (most likely an + *
estimate) of all + * possible items in the set. If this number is larger than getPageSize() + * then hasNextPage() should be true and you most likely can retrieve + * additional pages to get the remaining available items in the set. + * For result sets this is the number of items within the result set. + * For search result sets this is the estimated number of matching items. + * That means you may see this number change as you paginate through a + * search result set and the server updates the estimate with something + * more accurate. + * @return the total count of possible items + * @see xdmp:estimate + * @see search:estimate + */ public long getTotalSize(); /** The count of items in this page, which is always less than getPageSize(). @@ -60,9 +70,16 @@ public interface Page extends Iterable { public long size(); - /** The number of pages covering all possible items. - * @return the number of pages. Literally, - *
{@code (long) Math.ceil((double) getTotalSize() / (double) getPageSize()); }
+ /** The number of pages covering all possible items. Since this is calculated + * based on {@link #getTotalSize()}, it is often an + * estimate + * just like getTotalSize(). + * That means you may see this number change as you paginate through a search + * result set and the server updates the estimate with something more accurate. + * @return the number of pages. In pseudo-code: + *
{@code if ( getPageSize() == 0 ) return 0;
+     *Math.ceil( getTotalSize() /  getPageSize() ); 
+     * }
*/ public long getTotalPages(); @@ -81,16 +98,20 @@ public interface Page extends Iterable { */ public boolean hasPreviousPage(); - /** The page number within the count of all possible pages. - * @return {@code (long) Math.floor((double) start / (double) getPageSize()) + 1; } + /** The page number within the count of all possible pages. + * @return the page number. In pseudo-code: + *
{@code if ( getPageSize() == 0 ) return 0;
+     *if ( getStart() % getPageSize() == 0 ) return getStart() / getPageSize();
+     *else return Math.floor(getStart() / getPageSize()) + 1;
+     * }
*/ public long getPageNumber(); - /** @return true if {@code getPageNumber() == 1 } + /** @return true if {@code getPageSize()==0 or getPageNumber()==1 } */ public boolean isFirstPage(); - /** @return true if {@code getPageNumber() == getTotalPages() } + /** @return true if {@code getPageSize()==0 or getPageNumber()==getTotalPages() } */ public boolean isLastPage(); } From cb5f72944e85ab5b5eb870e0b7cc608da6dcc1b5 Mon Sep 17 00:00:00 2001 From: skottam Date: Wed, 12 Nov 2014 16:00:48 -0800 Subject: [PATCH 337/357] updated test to have filtered query --- .../TestPOJOQueryBuilderContainerQuery.java | 40 ++++++++++--------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderContainerQuery.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderContainerQuery.java index c0a7971c1..06d798b93 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderContainerQuery.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderContainerQuery.java @@ -31,17 +31,17 @@ public class TestPOJOQueryBuilderContainerQuery extends BasicJavaClientREST { public static void setUpBeforeClass() throws Exception { // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); System.out.println("In setup"); - setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); - BasicJavaClientREST.setDatabaseProperties(dbName, "trailing-wildcard-searches", true); - BasicJavaClientREST.setDatabaseProperties(dbName, "word-positions", true); - BasicJavaClientREST.setDatabaseProperties(dbName, "element-word-positions", true); - BasicJavaClientREST.addRangePathIndex(dbName, "long", "com.marklogic.javaclient.Artifact/inventory", "", "reject",true); +// setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); +// BasicJavaClientREST.setDatabaseProperties(dbName, "trailing-wildcard-searches", true); +// BasicJavaClientREST.setDatabaseProperties(dbName, "word-positions", true); +// BasicJavaClientREST.setDatabaseProperties(dbName, "element-word-positions", true); +// BasicJavaClientREST.addRangePathIndex(dbName, "long", "com.marklogic.javaclient.Artifact/inventory", "", "reject",true); } @AfterClass public static void tearDownAfterClass() throws Exception { System.out.println("In tear down" ); - tearDownJavaRESTServer(dbName, fNames, restServerName); +// tearDownJavaRESTServer(dbName, fNames, restServerName); } @Before public void setUp() throws Exception { @@ -162,14 +162,14 @@ public void testPOJOwordSearchWithContainerQueryBuilder() { String[] searchOptions ={"case-sensitive","wildcarded","max-occurs=1"}; PojoQueryBuilder qb = products.getQueryBuilder(); String[] searchNames = {"special"}; - PojoQueryDefinition qd =qb.containerQueryBuilder("manufacturer", Company.class).word("name",searchOptions,1.0,searchNames); + PojoQueryDefinition qd =qb.filteredQuery(qb.containerQueryBuilder("manufacturer", Company.class).word("name",searchOptions,1.0,searchNames)); JacksonHandle jh = new JacksonHandle(); products.setPageLength(11); p = products.search(qd, 1,jh); ; System.out.println(jh.get().toString()); - long pageNo=1,count=0; + long pageNo=1,count=0,total=0; do{ count =0; p = products.search(qd,pageNo); @@ -177,16 +177,17 @@ public void testPOJOwordSearchWithContainerQueryBuilder() { Artifact a =p.next(); validateArtifact(a); assertTrue("Verifying document id is part of the search ids",a.getId()%5==0); - count++; + assertTrue("Verifying name is part of the search",a.getManufacturer().getName().contains("special")); + count++;total++; System.out.println(a.getName()); } assertEquals("Page size",count,p.size()); pageNo=pageNo+p.getPageSize(); }while(!p.isLastPage() && pageNo<=p.getTotalSize()); - assertEquals("page number after the loop",1,p.getPageNumber()); - assertEquals("total no of pages",1,p.getTotalPages()); +// assertEquals("page number after the loop",1,p.getPageNumber()); +// assertEquals("total no of pages",1,p.getTotalPages()); assertEquals("page length from search handle",11,jh.get().path("page-length").asInt()); - assertEquals("Total results from search handle",11,jh.get().path("total").asInt()); + assertEquals("Total results from search handle",11,total); } //Below scenario is verifying range query from PojoBuilder @@ -269,15 +270,15 @@ public void testPOJOWordSearchWithOptions() throws Exception { String[] rangeOptions ={"uncached","min-occurs=1"}; PojoQueryBuilder qb = products.getQueryBuilder(); String[] searchNames = {"Acm*"}; - PojoQueryDefinition qd = qb.and(qb.andNot(qb.word("name",searchOptions, 1.0,searchNames), + PojoQueryDefinition qd = qb.filteredQuery(qb.and(qb.andNot(qb.word("name",searchOptions, 1.0,searchNames), qb.containerQueryBuilder("manufacturer", Company.class).value("name","Acme special, Inc.") ), - qb.range("inventory",rangeOptions,Operator.LT,1101)); + qb.range("inventory",rangeOptions,Operator.LT,1101))); JacksonHandle jh = new JacksonHandle(); products.setPageLength(25); p = products.search(qd, 1,jh); System.out.println(jh.get().toString()); - long pageNo=1,count=0; + long pageNo=1,count=0,total=0; do{ count =0; p = products.search(qd,pageNo); @@ -286,16 +287,17 @@ public void testPOJOWordSearchWithOptions() throws Exception { validateArtifact(a); assertTrue("Verifying document id is part of the search ids",a.getId()<1101); assertFalse("Verifying document has word counter",a.getManufacturer().getName().contains("special")); - count++; + count++;total++; } assertEquals("Page size",count,p.size()); pageNo=pageNo+p.getPageSize(); }while(!p.isLastPage() && pageNo<=p.getTotalSize()); - assertEquals("page number after the loop",2,p.getPageNumber()); - assertEquals("total no of pages",2,p.getTotalPages()); + System.out.println(pageNo); + assertEquals("page has results",25,jh.get().path("results").size()); + assertEquals("Page no after the loop",51,pageNo); assertEquals("page length from search handle",25,jh.get().path("page-length").asInt()); - assertEquals("Total results from search handle",40,jh.get().path("total").asInt()); + assertEquals("Total results from search handle",40,total); } } From 7c479803b60004ee0a15a529f4fa5925e81c9e0c Mon Sep 17 00:00:00 2001 From: skottam Date: Wed, 12 Nov 2014 16:01:35 -0800 Subject: [PATCH 338/357] updated test to have filtered query --- .../javaclient/TestPOJOQueryBuilderContainerQuery.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderContainerQuery.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderContainerQuery.java index 06d798b93..1b1ac2ceb 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderContainerQuery.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOQueryBuilderContainerQuery.java @@ -31,17 +31,17 @@ public class TestPOJOQueryBuilderContainerQuery extends BasicJavaClientREST { public static void setUpBeforeClass() throws Exception { // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); System.out.println("In setup"); -// setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); + setupJavaRESTServer(dbName, fNames[0], restServerName,restPort); // BasicJavaClientREST.setDatabaseProperties(dbName, "trailing-wildcard-searches", true); // BasicJavaClientREST.setDatabaseProperties(dbName, "word-positions", true); // BasicJavaClientREST.setDatabaseProperties(dbName, "element-word-positions", true); -// BasicJavaClientREST.addRangePathIndex(dbName, "long", "com.marklogic.javaclient.Artifact/inventory", "", "reject",true); + BasicJavaClientREST.addRangePathIndex(dbName, "long", "com.marklogic.javaclient.Artifact/inventory", "", "reject",true); } @AfterClass public static void tearDownAfterClass() throws Exception { System.out.println("In tear down" ); -// tearDownJavaRESTServer(dbName, fNames, restServerName); + tearDownJavaRESTServer(dbName, fNames, restServerName); } @Before public void setUp() throws Exception { From 96d4fed08c1d5d0e23a2c8afb580478205d8832a Mon Sep 17 00:00:00 2001 From: Ajit George Date: Thu, 13 Nov 2014 13:46:44 -0800 Subject: [PATCH 339/357] Added test case for patch update issue 132 --- .../javaclient/TestPartialUpdate.java | 122 ++++++++++++++++-- 1 file changed, 113 insertions(+), 9 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java index 58c1c4b76..ee74c5877 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java @@ -1,36 +1,42 @@ package com.marklogic.javaclient; -import static org.junit.Assert.*; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import java.io.File; import java.io.IOException; +import org.json.JSONException; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.skyscreamer.jsonassert.JSONAssert; + import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import com.marklogic.client.DatabaseClient; import com.marklogic.client.DatabaseClientFactory; -import com.marklogic.client.Transaction; import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.Transaction; import com.marklogic.client.admin.ExtensionLibrariesManager; import com.marklogic.client.document.DocumentDescriptor; import com.marklogic.client.document.DocumentMetadataPatchBuilder; +import com.marklogic.client.document.DocumentMetadataPatchBuilder.Cardinality; import com.marklogic.client.document.DocumentPatchBuilder; import com.marklogic.client.document.DocumentPatchBuilder.PathLanguage; +import com.marklogic.client.document.DocumentPatchBuilder.Position; import com.marklogic.client.document.DocumentUriTemplate; import com.marklogic.client.document.JSONDocumentManager; import com.marklogic.client.document.XMLDocumentManager; -import com.marklogic.client.document.DocumentMetadataPatchBuilder.Cardinality; -import com.marklogic.client.document.DocumentPatchBuilder.Position; +import com.marklogic.client.io.DocumentMetadataHandle; +import com.marklogic.client.io.DocumentMetadataHandle.Capability; +import com.marklogic.client.io.DocumentMetadataHandle.DocumentCollections; import com.marklogic.client.io.FileHandle; import com.marklogic.client.io.Format; import com.marklogic.client.io.StringHandle; -import com.marklogic.client.io.DocumentMetadataHandle.Capability; import com.marklogic.client.io.marker.DocumentPatchHandle; -import org.json.JSONException; -import org.junit.*; -import org.skyscreamer.jsonassert.JSONAssert; - public class TestPartialUpdate extends BasicJavaClientREST { private static String dbName = "TestPartialUpdateDB"; private static String [] fNames = {"TestPartialUpdateDB-1"}; @@ -1073,6 +1079,104 @@ public void testPartialUpdateDeleteJSON() throws IOException, JSONException client.release(); } + /* Purpose: This test is used to validate Git issue 132. + * Apply a patch to existing collections or permissions on a document using JSONPath expressions. + * + * Functions tested : replaceInsertFragment. An new fragment is inserted when unknown index is used. + */ + @Test + public void testMetaDataUpdateJSON() throws IOException, JSONException + { + System.out.println("Running testPartialUpdateReplaceInsertFragmentExistingJSON"); + + String[] filenames = {"json-original.json"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST); + DocumentMetadataHandle mhRead = new DocumentMetadataHandle(); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "JSON"); + } + + String docId = "/partial-update/json-original.json"; + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + DocumentMetadataPatchBuilder patchBldr = docMgr.newPatchBuilder(Format.JSON); + + //Adding the initial meta-data, since there are none. + patchBldr.addCollection("JSONPatch1", "JSONPatch3"); + patchBldr.addPermission("test-eval", DocumentMetadataHandle.Capability.READ, DocumentMetadataHandle.Capability.EXECUTE); + + DocumentMetadataPatchBuilder.PatchHandle patchHandle = patchBldr.build(); + docMgr.patch(docId, patchHandle); + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + String content = docMgr.read(docId, new StringHandle()).get(); + + System.out.println(content); + String exp="{\"employees\": [{\"firstName\":\"John\", \"lastName\":\"Doe\"}," + + "{\"firstName\":\"Ann\", \"lastName\":\"Smith\"}," + + "{\"lastName\":\"Foo\"}]}"; + JSONAssert.assertEquals(exp, content, false); + + // Validate the changed meta-data. + docMgr.readMetadata(docId, mhRead); + + // Collections + DocumentCollections collections = mhRead.getCollections(); + String actualCollections = getDocumentCollectionsString(collections); + System.out.println("Returned collections: " + actualCollections); + + assertTrue("Document collections difference in size value", actualCollections.contains("size:2")); + assertTrue("JSONPatch1 not found", actualCollections.contains("JSONPatch1")); + assertTrue("JSONPatch3 not found", actualCollections.contains("JSONPatch3")); + + //Construct a Patch From Raw JSON + /* This is the JSON Format of meta-data for a document: Used for debugging and JSON Path estimation. + { + "collections" : [ string ], + "permissions" : [ + { + "role-name" : string, + "capabilities" : [ string ] + } + ], + "properties" : { + property-name : property-value + }, + "quality" : integer + } + */ + + /* This is the format for INSERT patch. Refer to Guides. + { "patch": [ + { "insert": { + "context": "$.parent.child1", + "position": "before", + "content": { "INSERT1": "INSERTED1" } + }}, + */ + + /* This is the current meta-data in JSON format - For debugging purpose + {"collections":["JSONPatch1","JSONPatch3"], + "permissions":[{"role-name":"rest-writer", + "capabilities":["execute","read","update"]},{"role-name":"test-eval", + "capabilities":["execute","read"]},{"role-name":"rest-reader", + "capabilities":["read"]}], + "properties":{},"quality":0}*/ + + //String str = new String("{\"patch\": [{ \"insert\": {\"context\": \"collections\",\"position\": \"before\",\"content\": { \"shapes\":\"squares\" }}}]}"); + + // release client + client.release(); + } + @AfterClass public static void tearDown() throws Exception { From 38218caa3747f531a1df8ff57ba0354438e8a968 Mon Sep 17 00:00:00 2001 From: skottam Date: Thu, 13 Nov 2014 15:38:34 -0800 Subject: [PATCH 340/357] updated the test after the chris fix for null values --- .../java/com/marklogic/javaclient/TestEvalJavaScript.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestEvalJavaScript.java b/test-complete/src/test/java/com/marklogic/javaclient/TestEvalJavaScript.java index 89f0ec66b..00ad25d97 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestEvalJavaScript.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestEvalJavaScript.java @@ -62,7 +62,7 @@ public static void setUpBeforeClass() throws Exception { setupJavaRESTServer(dbName, fNames[0], restServerName,restPort,false); TestEvalXquery.createUserRolesWithPrevilages("test-eval", "xdbc:eval", "xdbc:eval-in","xdmp:eval-in","xdbc:invoke-in","any-uri","xdbc:invoke"); TestEvalXquery.createRESTUser("eval-user", "x", "test-eval"); -// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); + System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); } @AfterClass @@ -370,7 +370,7 @@ public void testJSDifferentVariableTypes() throws Exception { +"var myNull;" + "var myJsonArray;" + "var myJsonNull;" - + "results.push(myString,myBool,myInteger,myDecimal,myJsonObject,myJsonArray);" + + "results.push(myString,myBool,myInteger,myDecimal,myJsonObject,myJsonArray,myNull);" +"xdmp.arrayValues(results)"; ServerEvaluationCall evl= client.newServerEval().javascript(query1); @@ -383,7 +383,7 @@ public void testJSDifferentVariableTypes() throws Exception { .addVariableAs("myJsonArray",new ObjectMapper().createArrayNode().add(1).add(2).add(3)) .addVariableAs("myJsonNull",new ObjectMapper().createObjectNode().nullNode() ) ; - System.out.println(query1); + System.out.println(new ObjectMapper().createObjectNode().nullNode().toString()); EvalResultIterator evr = evl.eval(); this.validateReturnTypes(evr); From e12040631f2b51d302a5f42099c3c2fbbaef2b2c Mon Sep 17 00:00:00 2001 From: Ajit George Date: Thu, 13 Nov 2014 17:26:35 -0800 Subject: [PATCH 341/357] Fixed regression --- ...tBulkReadWriteWithJacksonParserHandle.java | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonParserHandle.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonParserHandle.java index 36c0b9d25..11f82d594 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonParserHandle.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonParserHandle.java @@ -55,7 +55,7 @@ public class TestBulkReadWriteWithJacksonParserHandle extends BasicJavaClientREST { - private static final String DIRECTORY = "/bulkread/"; + private static final String DIRECTORY = "/"; private static String dbName = "TestBulkJacksonParserDB"; private static String[] fNames = { "TestBulkJacksonParserDB-1" }; private static String restServerName = "REST-Java-Client-API-Server"; @@ -491,6 +491,30 @@ public void testWriteMultiJSONFilesDefaultMetadata() throws Exception @Test public void testBulkSearchQBEWithJSONResponseFormat() throws IOException, ParserConfigurationException, SAXException, TransformerException { int count; + String docId[] = { "/a.json", "/b.json", "/c.json" }; + String json1 = new String("{\"animal\":\"dog\", \"says\":\"woof\"}"); + String json2 = new String("{\"animal\":\"cat\", \"says\":\"meow\"}"); + String json3 = new String("{\"animal\":\"rat\", \"says\":\"keek\"}"); + + JsonFactory f = new JsonFactory(); + + JSONDocumentManager docMgr1 = client.newJSONDocumentManager(); + docMgr1.setMetadataCategories(Metadata.ALL); + DocumentWriteSet writeset = docMgr1.newWriteSet(); + + JacksonParserHandle jacksonParserHandle1 = new JacksonParserHandle(); + JacksonParserHandle jacksonParserHandle2 = new JacksonParserHandle(); + JacksonParserHandle jacksonParserHandle3 = new JacksonParserHandle(); + + jacksonParserHandle1.set(f.createParser(json1)); + jacksonParserHandle2.set(f.createParser(json2)); + jacksonParserHandle3.set(f.createParser(json3)); + + writeset.add(docId[0], jacksonParserHandle1); + writeset.add(docId[1], jacksonParserHandle2); + writeset.add(docId[2], jacksonParserHandle3); + // Write to database. + docMgr1.write(writeset); //Creating a xml document manager for bulk search XMLDocumentManager docMgr = client.newXMLDocumentManager(); @@ -533,11 +557,10 @@ public void testBulkSearchQBEWithJSONResponseFormat() throws IOException, Parser pageNo = pageNo + page.getPageSize(); }while(!page.isLastPage() && page.hasContent() ); - assertEquals("page count is ",5,page.getTotalPages()); - assertTrue("Page has previous page ?",page.hasPreviousPage()); + assertEquals("page count is ",1,page.getTotalPages()); + assertFalse("Page has previous page ?",page.hasPreviousPage()); assertEquals("page size", 25,page.getPageSize()); - assertEquals("document count", 102,page.getTotalSize()); - + assertEquals("document count", 1,page.getTotalSize()); } @AfterClass From 59e194654f4e28f7457ba8a8c9ecf8300528aa3f Mon Sep 17 00:00:00 2001 From: sammefford Date: Fri, 14 Nov 2014 14:58:27 -0700 Subject: [PATCH 342/357] add lots of javadocs; clean up some existing javadocs; remove some commented code --- .../bitemporal/TemporalDocumentManager.java | 167 ++++++++++++++---- .../client/document/DocumentManager.java | 147 ++++++++++++++- .../client/impl/JacksonBaseHandle.java | 13 -- .../client/io/JacksonDatabindHandle.java | 53 ++++-- .../client/io/JacksonParserHandle.java | 30 +++- 5 files changed, 347 insertions(+), 63 deletions(-) diff --git a/src/main/java/com/marklogic/client/bitemporal/TemporalDocumentManager.java b/src/main/java/com/marklogic/client/bitemporal/TemporalDocumentManager.java index 6778e8f12..ecc3a7ec4 100644 --- a/src/main/java/com/marklogic/client/bitemporal/TemporalDocumentManager.java +++ b/src/main/java/com/marklogic/client/bitemporal/TemporalDocumentManager.java @@ -20,6 +20,7 @@ import com.marklogic.client.ResourceNotFoundException; import com.marklogic.client.Transaction; import com.marklogic.client.document.DocumentDescriptor; +import com.marklogic.client.document.DocumentManager; import com.marklogic.client.document.DocumentUriTemplate; import com.marklogic.client.document.ServerTransform; import com.marklogic.client.io.marker.AbstractReadHandle; @@ -28,7 +29,22 @@ import com.marklogic.client.util.RequestParameters; public interface TemporalDocumentManager { - public DocumentDescriptor create(DocumentUriTemplate template, + /** + * Just like {@link DocumentManager#create(DocumentUriTemplate, DocumentMetadataWriteHandle, + * AbstractWriteHandle, ServerTransform, Transaction) create} but create document + * in a temporalCollection, which will enforce all the rules of + * + * bitemporal data management. + * @param template the template for constructing the document uri + * @param metadataHandle a handle for writing the metadata of the document + * @param content an IO representation of the document content + * @param transform a server transform to modify the document content + * @param transaction an open transaction under which the document may have been created or deleted + * @param temporalCollection the name of the temporal collection existing in the database into + * which this document should be written + * @return the database uri that identifies the created document + */ + public DocumentDescriptor create(DocumentUriTemplate template, DocumentMetadataWriteHandle metadataHandle, W contentHandle, ServerTransform transform, @@ -36,6 +52,20 @@ public DocumentDescriptor create(DocumentUriTemplate template, String temporalCollection) throws ForbiddenUserException, FailedRequestException; + /** + * Just like {@link DocumentManager#write(DocumentDescriptor, DocumentMetadataWriteHandle, + * AbstractWriteHandle, ServerTransform, Transaction) write} but write document + * in a temporalCollection, which will enforce all the rules of + * + * bitemporal data management. + * @param desc a descriptor for the URI identifier, format, and mimetype of the document + * @param metadataHandle a handle for writing the metadata of the document + * @param content an IO representation of the document content + * @param transform a server transform to modify the document content + * @param transaction an open transaction under which the document may have been created or deleted + * @param temporalCollection the name of the temporal collection existing in the database into + * which this document should be written + */ public void write(DocumentDescriptor desc, DocumentMetadataWriteHandle metadataHandle, W contentHandle, @@ -44,6 +74,20 @@ public void write(DocumentDescriptor desc, String temporalCollection) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException; + /** + * Just like {@link DocumentManager#write(String, DocumentMetadataWriteHandle, + * AbstractWriteHandle, ServerTransform, Transaction) write} but write document + * in a temporalCollection, which will enforce all the rules of + * + * bitemporal data management. + * @param docId the URI identifier for the document + * @param metadataHandle a handle for writing the metadata of the document + * @param content an IO representation of the document content + * @param transform a server transform to modify the document content + * @param transaction an open transaction under which the document may have been created or deleted + * @param temporalCollection the name of the temporal collection existing in the database into + * which this document should be written + */ public void write(String docId, DocumentMetadataWriteHandle metadataHandle, W contentHandle, @@ -51,18 +95,54 @@ public void write(String docId, Transaction transaction, String temporalCollection) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException; - + + /** + * Just like {@link DocumentManager#delete(DocumentDescriptor, Transaction) delete} but delete + * document in a temporalCollection, which will enforce all the rules of + * + * bitemporal data management. + * @param desc a descriptor for the URI identifier, format, and mimetype of the document + * @param transaction an open transaction under which the document may have been created or deleted + * @param temporalCollection the name of the temporal collection existing in the database in + * which this document should be marked as deleted + */ public void delete(DocumentDescriptor desc, Transaction transaction, String temporalCollection) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException; + /** + * Just like {@link DocumentManager#delete(String, Transaction) delete} but delete + * document in a temporalCollection, which will enforce all the rules of + * + * bitemporal data management. + * @param docId the URI identifier for the document + * @param transaction an open transaction under which the document may have been created or deleted + * @param temporalCollection the name of the temporal collection existing in the database in + * which this document should be marked as deleted + */ public void delete(String docId, Transaction transaction, String temporalCollection) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException; - - // The following methods take a system time which is an advanced concept in bitemporal feature. + + // The following methods take a system time which is an advanced concept in bitemporal feature. + //TODO: remove all extraParams + /** + * Just like {@link #create(DocumentUriTemplate, DocumentMetadataWriteHandle, + * AbstractWriteHandle, ServerTransform, Transaction, String) create} but create document + * at a specific system time + * @param template the template for constructing the document uri + * @param metadataHandle a handle for writing the metadata of the document + * @param content an IO representation of the document content + * @param transform a server transform to modify the document content + * @param transaction an open transaction under which the document may have been created or deleted + * @param temporalCollection the name of the temporal collection existing in the database into + * which this document should be written + * @param systemTime the application-specified system time with which this document will be marked + * @param extraParams this param is going away in 8.0-1 + * @return the database uri that identifies the created document + */ public DocumentDescriptor create(DocumentUriTemplate template, DocumentMetadataWriteHandle metadataHandle, W contentHandle, @@ -72,7 +152,21 @@ public DocumentDescriptor create(DocumentUriTemplate template, java.util.Calendar systemTime, RequestParameters extraParams) throws ForbiddenUserException, FailedRequestException; - + + /** + * Just like {@link #write(DocumentDescriptor, DocumentMetadataWriteHandle, + * AbstractWriteHandle, ServerTransform, Transaction, String) write} but write document + * at a specific system time + * @param desc a descriptor for the URI identifier, format, and mimetype of the document + * @param metadataHandle a handle for writing the metadata of the document + * @param content an IO representation of the document content + * @param transform a server transform to modify the document content + * @param transaction an open transaction under which the document may have been created or deleted + * @param temporalCollection the name of the temporal collection existing in the database into + * which this document should be written + * @param systemTime the application-specified system time with which this document will be marked + * @param extraParams this param is going away in 8.0-1 + */ public void write(DocumentDescriptor desc, DocumentMetadataWriteHandle metadataHandle, W contentHandle, @@ -82,7 +176,21 @@ public void write(DocumentDescriptor desc, java.util.Calendar systemTime, RequestParameters extraParams) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException; - + + /** + * Just like {@link #write(String, DocumentMetadataWriteHandle, + * AbstractWriteHandle, ServerTransform, Transaction, String) write} but write document + * at a specific system time + * @param docId the URI identifier for the document + * @param metadataHandle a handle for writing the metadata of the document + * @param content an IO representation of the document content + * @param transform a server transform to modify the document content + * @param transaction an open transaction under which the document may have been created or deleted + * @param temporalCollection the name of the temporal collection existing in the database into + * which this document should be written + * @param systemTime the application-specified system time with which this document will be marked + * @param extraParams this param is going away in 8.0-1 + */ public void write(String docId, DocumentMetadataWriteHandle metadataHandle, W contentHandle, @@ -92,41 +200,34 @@ public void write(String docId, java.util.Calendar systemTime, RequestParameters extraParams) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException; - + + /** + * Just like {@link #delete(DocumentDescriptor, Transaction, String) delete} but delete + * document at a specified system time + * @param desc a descriptor for the URI identifier, format, and mimetype of the document + * @param transaction an open transaction under which the document may have been created or deleted + * @param temporalCollection the name of the temporal collection existing in the database in + * which this document should be marked as deleted + * @param systemTime the application-specified system time with which this document will be marked + */ public void delete(DocumentDescriptor desc, Transaction transaction, String temporalCollection, java.util.Calendar systemTime) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException; - + + /** + * Just like {@link #delete(String, Transaction, String) delete} but delete + * document at a specified system time + * @param docId the URI identifier for the document + * @param transaction an open transaction under which the document may have been created or deleted + * @param temporalCollection the name of the temporal collection existing in the database in + * which this document should be marked as deleted + * @param systemTime the application-specified system time with which this document will be marked + */ public void delete(String docId, Transaction transaction, String temporalCollection, java.util.Calendar systemTime) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException; - - /* - public T read(DocumentDescriptor desc, - DocumentMetadataReadHandle metadataHandle, - T contentHandle, - ServerTransform transform, - Transaction transaction, - String temporalCollection) - throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException; - - public T read(String docId, - DocumentMetadataReadHandle metadataHandle, - T contentHandle, - ServerTransform transform, - Transaction transaction, - String temporalCollection) - throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException; - - public DocumentPage read(ServerTransform transform, - Transaction transaction, - String temporalCollection, - String[] uris) - throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException; - */ - } diff --git a/src/main/java/com/marklogic/client/document/DocumentManager.java b/src/main/java/com/marklogic/client/document/DocumentManager.java index f96d49cce..a0c644041 100644 --- a/src/main/java/com/marklogic/client/document/DocumentManager.java +++ b/src/main/java/com/marklogic/client/document/DocumentManager.java @@ -29,7 +29,10 @@ import com.marklogic.client.io.marker.DocumentMetadataWriteHandle; import com.marklogic.client.io.marker.DocumentPatchHandle; import com.marklogic.client.io.marker.SearchReadHandle; +import com.marklogic.client.document.DocumentPage; +import com.marklogic.client.document.DocumentRecord; import com.marklogic.client.query.QueryDefinition; +import com.marklogic.client.query.QueryManager; import com.marklogic.client.query.QueryManager.QueryView; /** @@ -416,38 +419,178 @@ public T read(DocumentDescriptor desc, DocumentMetadataReadHandle */ public DocumentPage read(ServerTransform transform, Transaction transaction, String... uris); + /** + * Reads from the database the metadata for a list of documents matching the + * provided uris. Allows iteration across the metadata for matching documents + * (only if setMetadataCategories has been called to request metadata). To find + * out how many of your uris matched, call the + * {@link DocumentPage#size() DocumentPage.size()} method. + * + * @param uris the database uris identifying documents + * @return the DocumentPage of metadata from matching documents + */ public DocumentPage readMetadata(String... uris); + /** + * Reads from the database the metadata for a list of documents matching the + * provided uris. Allows iteration across the metadata for matching documents + * (only if setMetadataCategories has been called to request metadata). To find + * out how many of your uris matched, call the + * {@link DocumentPage#size() DocumentPage.size()} method. + * + * @param transaction the transaction in which this read is participating + * @param uris the database uris identifying documents + * @return the DocumentPage of metadata from matching documents + */ public DocumentPage readMetadata(Transaction transaction, String... uris); + /** + * Just like {@link QueryManager#search(QueryDefinition, SearchReadHandle, long) QueryManager.search} + * but return complete documents via iterable DocumentPage. Retrieves up to getPageLength() + * documents in each DocumentPage. If setMetadataCategories has + * been called, populates metadata for each result in the format specified by + * {@link #setNonDocumentFormat setNonDocumentFormat}. + * @param querydef the definition of query criteria and query options + * @param start the offset of the first document in the page (where 1 is the first result) + * @return the DocumentPage of matching documents and metadata + */ public DocumentPage search(QueryDefinition querydef, long start); + /** + * Just like {@link QueryManager#search(QueryDefinition, SearchReadHandle, long, Transaction) QueryManager.search} + * but return complete documents via iterable DocumentPage. Retrieves up to getPageLength() + * documents in each DocumentPage. If setMetadataCategories has + * been called, populates metadata for each result in the format specified by + * {@link #setNonDocumentFormat setNonDocumentFormat}. + * @param querydef the definition of query criteria and query options + * @param start the offset of the first document in the page (where 1 is the first result) + * @param transaction an open transaction for matching documents + * @return the DocumentPage of matching documents and metadata + */ public DocumentPage search(QueryDefinition querydef, long start, Transaction transaction); + /** + * Just like {@link QueryManager#search(QueryDefinition, SearchReadHandle, long) QueryManager.search} + * but return complete documents via iterable DocumentPage. Retrieves up to getPageLength() + * documents in each DocumentPage. If searchHandle is not null, + * requests a search response and populates searchHandle with it. If setMetadataCategories has + * been called, populates metadata for each result in the format specified by + * {@link #setNonDocumentFormat setNonDocumentFormat}. + * @param querydef the definition of query criteria and query options + * @param start the offset of the first document in the page (where 1 is the first result) + * @param searchHandle a handle for reading the search response which will include view types + * specified by {@link #setSearchView setSearchView} and format specified by + * {@link #setNonDocumentFormat setNonDocumentFormat} + * @return the DocumentPage of matching documents and metadata + */ public DocumentPage search(QueryDefinition querydef, long start, SearchReadHandle searchHandle); + /** + * Just like {@link QueryManager#search(QueryDefinition, SearchReadHandle, long, Transaction)} + * but return complete documents via iterable DocumentPage. Retrieves up to getPageLength() + * documents in each DocumentPage. If searchHandle is not null, + * requests a search response and populates searchHandle with it. If setMetadataCategories has + * been called, populates metadata for each result in the format specified by + * {@link #setNonDocumentFormat setNonDocumentFormat}. + * @param querydef the definition of query criteria and query options + * @param start the offset of the first document in the page (where 1 is the first result) + * @param searchHandle a handle for reading the search response which will include view types + * specified by {@link #setSearchView setSearchView} and format specified by + * {@link #setNonDocumentFormat setNonDocumentFormat} + * @param transaction an open transaction for matching documents + * @return the DocumentPage of matching documents and metadata + */ public DocumentPage search(QueryDefinition querydef, long start, SearchReadHandle searchHandle, Transaction transaction); + /** Get the maximum number of records to return in a page from calls to {@link #search search} + * @return the maximum number of records to return in a page from calls to + * {@link #search search} */ public long getPageLength(); + /** + * Specifies the maximum number of documents that can appear in any page of the query results, + * overriding any maximum specified in the query options. + * @param length the maximum number of records to return in a page from calls to + * {@link #search search} + */ public void setPageLength(long length); + /** + * Returns the format (if set) for the search response from + * {@link #search(QueryDefinition, long, SearchReadHandle) search} and + * metadata available from {@link DocumentRecord#getMetadata(DocumentMetadataReadHandle) + * DocumentPage.next().getMetadata(handle)} (assuming + * {@link #setMetadataCategories setMetadataCategories} has been called + * to request specific metadata). If setNonDocumentFormat has not been called, + * the server default format will be used. + * @return the format, if set, null otherwise + */ public Format getNonDocumentFormat(); - + + /** + * Specifies the format for the search response from + * {@link #search(QueryDefinition, long, SearchReadHandle) search} and + * metadata available from {@link DocumentRecord#getMetadata(DocumentMetadataReadHandle) + * DocumentPage.next().getMetadata(handle)} (assuming + * {@link #setMetadataCategories setMetadataCategories} has been called + * to request specific metadata). If setNonDocumentFormat is not is called, + * the server default format will be used. + * @param nonDocumentFormat the format to use + */ public void setNonDocumentFormat(Format nonDocumentFormat); - + + /** + * Returns the view types included in a SearchReadHandle populated by calls to + * {@link #search(QueryDefinition, long, SearchReadHandle) search} + * @return the view types included in a SearchReadHandle populated by calls to + * {@link #search(QueryDefinition, long, SearchReadHandle) search} + */ public QueryView getSearchView(); + /** + * Specifies the view types included in a SearchReadHandle populated by calls to + * {@link #search(QueryDefinition, long, SearchReadHandle) search} + * @param view the view types included in a SearchReadHandle populated by calls to + * {@link #search(QueryDefinition, long, SearchReadHandle) search} + */ public void setSearchView(QueryView view); public DocumentWriteSet newWriteSet(); + /** + * Write a set of documents and metadata to the server via REST API bulk capabilities. + * @param writeSet the set of documents and metadata to write + * @see REST API -> Reading + * and Writing Multiple Documents + */ public void write(DocumentWriteSet writeSet); + /** + * Write a set of documents and metadata to the server via REST API bulk capabilities. + * @param writeSet the set of documents and metadata to write + * @param transform a server transform to modify the contents of each document + * @see REST API -> Reading + * and Writing Multiple Documents + */ public void write(DocumentWriteSet writeSet, ServerTransform transform); + /** + * Write a set of documents and metadata to the server via REST API bulk capabilities. + * @param writeSet the set of documents and metadata to write + * @param transaction an open transaction under which the documents will be written + * @see REST API -> Reading + * and Writing Multiple Documents + */ public void write(DocumentWriteSet writeSet, Transaction transaction); + /** + * Write a set of documents and metadata to the server via REST API bulk capabilities. + * @param writeSet the set of documents and metadata to write + * @param transform a server transform to modify the contents of each document + * @param transaction an open transaction under which the documents will be written + * @see REST API -> Reading + * and Writing Multiple Documents + */ public void write(DocumentWriteSet writeSet, ServerTransform transform, Transaction transaction); /** diff --git a/src/main/java/com/marklogic/client/impl/JacksonBaseHandle.java b/src/main/java/com/marklogic/client/impl/JacksonBaseHandle.java index c027c4ad0..f7d66f121 100644 --- a/src/main/java/com/marklogic/client/impl/JacksonBaseHandle.java +++ b/src/main/java/com/marklogic/client/impl/JacksonBaseHandle.java @@ -55,10 +55,6 @@ protected JacksonBaseHandle() { super.setFormat(Format.JSON); } - /** - * Returns the mapper used to construct node objects from JSON. - * @return the JSON mapper. - */ public ObjectMapper getMapper() { if (mapper == null) { mapper = new ObjectMapper(); @@ -69,15 +65,6 @@ public ObjectMapper getMapper() { return mapper; } - /** - * Enables clients to use any mapper, including databinding mappers for formats other than JSON. - * Use at your own risk! Note that you may want to configure your mapper as we do to not close - * streams which we may need to reuse if we have to resend a network request: - * - * mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); - * mapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false); - * - **/ public void setMapper(ObjectMapper mapper) { this.mapper = mapper; } diff --git a/src/main/java/com/marklogic/client/io/JacksonDatabindHandle.java b/src/main/java/com/marklogic/client/io/JacksonDatabindHandle.java index cbb2906ad..267811cca 100644 --- a/src/main/java/com/marklogic/client/io/JacksonDatabindHandle.java +++ b/src/main/java/com/marklogic/client/io/JacksonDatabindHandle.java @@ -42,9 +42,9 @@ import com.marklogic.client.impl.JacksonBaseHandle; /** - * An adapter for using the Jackson Open Source library for JSON; represents - * JSON content as a Jackson JsonNode for reading or writing. Enables reading and - * writing JSON documents, JSON structured search, and other JSON input and output. + * An adapter for using the Jackson Open Source library for JSON; represents + * JSON content for reading or writing as objects of the specified POJO class. + * Enables reading and writing JSON directly to or from POJOs. */ public class JacksonDatabindHandle extends JacksonBaseHandle @@ -83,7 +83,7 @@ static public ContentHandleFactory newFactory(ObjectMapper mapper, Class... p /** * Specify the type of content this JacksonDatabindHandle will manage. * - * @param contentClass the class of your custom Pojo for databinding + * @param contentClass the class of your custom POJO for databinding */ public JacksonDatabindHandle(Class contentClass) { super(); @@ -91,8 +91,8 @@ public JacksonDatabindHandle(Class contentClass) { setResendable(true); } /** - * Provides a handle on JSON content as a Jackson tree. - * @param content the JSON root node of the tree. + * Provides a handle on POJO content. + * @param content the POJO which should be serialized */ public JacksonDatabindHandle(T content) { this((Class) content.getClass()); @@ -111,31 +111,58 @@ public JacksonDatabindHandle withFormat(Format format) { } /** - * Returns the root node of the JSON tree. - * @return the JSON root node. + * Returns the content. + * @return the content you provided if you called {@link #JacksonDatabindHandle(Object)} + * or {@link #set(Object)} or if the content is being de-serialized, a pojo of + * the specified type populated with the data */ @Override public T get() { return content; } /** - * Assigns your custom Pojo as the content. - * @param content your custom Pojo + * Assigns your custom POJO as the content. + * @param content your custom POJO */ @Override public void set(T content) { this.content = content; } /** - * Assigns a JSON tree as the content and returns the handle. - * @param content the JSON root node. - * @return the handle on the JSON tree. + * Assigns a your custom POJO as the content and returns the handle. + * @param content your custom POJO + * @return the handle */ public JacksonDatabindHandle with(T content) { set(content); return this; } + /** + * Provides access to the ObjectMapper used internally so you can configure + * it to fit your JSON. + * @return the ObjectMapper instance + */ + @Override + public ObjectMapper getMapper() { return super.getMapper(); } + /** + * Enables clients to specify their own ObjectMapper instance, including databinding mappers + * for formats other than JSON. + * For example:
{@code
+     *ObjectMapper mapper = new CsvMapper();
+     *mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
+     *mapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
+     *handle.setMapper(mapper);
+     * }
+ * + * Use at your own risk! Note that you most likely want to set to false the two options we + * demonstrate above (JsonGenerator.Feature.AUTO_CLOSE_TARGET and JsonParser.Feature.AUTO_CLOSE_SOURCE) + * as we do so your mapper will not close streams which we may need to reuse if we have to + * resend a network request. + **/ + @Override + public void setMapper(ObjectMapper mapper) { super.setMapper(mapper); } + @Override protected void receiveContent(InputStream content) { if (content == null) diff --git a/src/main/java/com/marklogic/client/io/JacksonParserHandle.java b/src/main/java/com/marklogic/client/io/JacksonParserHandle.java index 84901dba5..e65daf9d3 100644 --- a/src/main/java/com/marklogic/client/io/JacksonParserHandle.java +++ b/src/main/java/com/marklogic/client/io/JacksonParserHandle.java @@ -38,9 +38,9 @@ /** * An adapter for using the streaming capabilities of the Jackson Open Source library. - * Enables low-level reading and writing of JSON documents. + * Enables low-level efficient reading and writing of JSON documents. + * @see Jackson Streaming API */ -// TODO: add link to jackson streaming documentation public class JacksonParserHandle extends JacksonBaseHandle implements ContentHandle, @@ -98,6 +98,7 @@ public JacksonParserHandle withFormat(Format format) { /** * JsonParser allows streaming access to content as it arrives. + * @return the JsonParser over the content (usually received from the server) */ public JsonParser get() { if ( parser == null ) { @@ -126,6 +127,31 @@ public void set(JsonParser parser) { } } + /** + * Provides access to the ObjectMapper used internally so you can configure + * it to fit your JSON. + * @return the ObjectMapper instance + */ + @Override + public ObjectMapper getMapper() { return super.getMapper(); } + /** + * Enables clients to specify their own ObjectMapper instance, including databinding mappers + * for formats other than JSON. + * For example:
{@code
+     *ObjectMapper mapper = new CsvMapper();
+     *mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
+     *mapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
+     *handle.setMapper(mapper);
+     * }
+ * + * Use at your own risk! Note that you most likely want to set to false the two options we + * demonstrate above (JsonGenerator.Feature.AUTO_CLOSE_TARGET and JsonParser.Feature.AUTO_CLOSE_SOURCE) + * as we do so your mapper will not close streams which we may need to reuse if we have to + * resend a network request. + **/ + @Override + public void setMapper(ObjectMapper mapper) { super.setMapper(mapper); } + @Override protected void receiveContent(InputStream content) { this.content = content; From 948eca7c9af7c46d0fc77e2be2fb1483669f8ef8 Mon Sep 17 00:00:00 2001 From: Ajit George Date: Fri, 14 Nov 2014 14:42:34 -0800 Subject: [PATCH 343/357] Comments and test cases for Git issue 89 --- .../TestBulkReadWriteWithJacksonDataBind.java | 167 ++++++++++++++++-- .../TestPOJOWithDocsStoredByOthers.java | 4 +- 2 files changed, 156 insertions(+), 15 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonDataBind.java b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonDataBind.java index 610d36961..b54ce3685 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonDataBind.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestBulkReadWriteWithJacksonDataBind.java @@ -5,6 +5,8 @@ import static org.junit.Assert.assertTrue; import java.util.Calendar; +import java.util.HashMap; +import java.util.Map; import org.junit.After; import org.junit.AfterClass; @@ -16,6 +18,7 @@ import com.marklogic.client.DatabaseClientFactory; import com.marklogic.client.DatabaseClientFactory.Authentication; import com.marklogic.client.document.DocumentManager.Metadata; +import com.marklogic.client.document.DocumentPage; import com.marklogic.client.document.DocumentRecord; import com.marklogic.client.document.DocumentWriteSet; import com.marklogic.client.document.JSONDocumentManager; @@ -26,6 +29,7 @@ import com.marklogic.client.io.DocumentMetadataHandle.DocumentProperties; import com.marklogic.client.io.Format; import com.marklogic.client.io.JacksonDatabindHandle; +import com.marklogic.client.io.JacksonHandle; import com.marklogic.client.io.marker.ContentHandleFactory; /* @@ -35,12 +39,25 @@ public class TestBulkReadWriteWithJacksonDataBind extends BasicJavaClientREST { - private static final String DIRECTORY = "/bulkread/"; + private static final String DIRECTORY = "/"; private static String dbName = "TestBulkJacksonDataBindDB"; private static String[] fNames = { "TestBulkJacksonDataBindDB-1" }; private static String restServerName = "REST-Java-Client-API-Server"; private static int restPort = 8011; private DatabaseClient client; + + public static class ContentCheck + { + String content; + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + } @BeforeClass public static void setUp() throws Exception { @@ -261,7 +278,7 @@ public void testWriteMultipleJSONDocsFromStrings() throws Exception { docMgr.readMetadata(docId[2], mhRead); validateMetadata(mhRead); } - + /* * Purpose: To test newFactory method with custom Pojo instances. */ @@ -351,21 +368,145 @@ public void testJacksonDataBindHandleFromFactory() throws Exception { docMgr.readMetadata(docId[2], mhRead); validateMetadata(mhRead); } + + /* + * Purpose: To test Git Issue # 89. + * Issue Description: If you read more than 100 JSON objects, the Client API stops reading them. + * + * Use one Jackson Handles instance. + */ + @Test + public void testSingleJacksonHandlerHundredJsonDocs() throws Exception { + + JacksonHandle jh = new JacksonHandle(); + jh.withFormat(Format.JSON); + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); + docMgr.setNonDocumentFormat(Format.JSON); + DocumentWriteSet writeset = docMgr.newWriteSet(); + // put meta-data + DocumentMetadataHandle mh = setMetadata(); + writeset.addDefault(mh); + + JacksonDatabindHandle handle1 = new JacksonDatabindHandle(String.class); + + Map jsonMap = new HashMap(); + String[] uris = new String[150]; + + String dir = new String("/"); + String mapDocId = null; + StringBuffer mapDocContent = new StringBuffer(); + for (int i=0;i<102;i++) + { + mapDocId = dir + Integer.toString(i); + mapDocContent.append("{\"content\":\""); + mapDocContent.append(Integer.toString(i)); + mapDocContent.append("\"}"); + + jsonMap.put(mapDocId, mapDocContent.toString()); + + handle1.set(mapDocContent.toString()); + writeset.add(mapDocId, handle1); + + uris[i] = mapDocId; + + mapDocContent.setLength(0); + mapDocId = null; + docMgr.write(writeset); + writeset.clear(); + } + + int count=0; + + DocumentPage page = docMgr.read(uris); + DocumentRecord rec; + + while(page.hasNext()){ + rec = page.next(); + + assertNotNull("DocumentRecord should never be null", rec); + assertNotNull("Document uri should never be null", rec.getUri()); + assertTrue("Document uri should start with " + DIRECTORY, rec.getUri().startsWith(DIRECTORY)); + + rec.getContent(jh); + //Verify the contents: comparing Map with JacksonHandle's. + assertEquals("Comparing the content :",jsonMap.get(rec.getUri()),jh.get().toString()); + count++; + } + assertEquals("document count", 102,count); + + } + /* + * Purpose: To test Git Issue # 89. + * Issue Description: If you read more than 100 JSON objects, the Client API stops reading them. + * + * Use multiple Jackson Handle instances. + */ + @Test + public void testMultipleJacksonHandleHundredJsonDocs1() throws Exception { + + JSONDocumentManager docMgr = client.newJSONDocumentManager(); + docMgr.setMetadataCategories(Metadata.ALL); + docMgr.setNonDocumentFormat(Format.JSON); + DocumentWriteSet writeset = docMgr.newWriteSet(); + // put meta-data + DocumentMetadataHandle mh = setMetadata(); + writeset.addDefault(mh); + + JacksonDatabindHandle handle1 = new JacksonDatabindHandle(String.class); + + Map jsonMap = new HashMap(); + String[] uris = new String[150]; + + String dir = new String("/"); + String mapDocId = null; + StringBuffer mapDocContent = new StringBuffer(); + for (int i=0;i<102;i++) + { + mapDocId = dir + Integer.toString(i); + mapDocContent.append("{\"content\":\""); + mapDocContent.append(Integer.toString(i)); + mapDocContent.append("\"}"); + + jsonMap.put(mapDocId, mapDocContent.toString()); + + handle1.set(mapDocContent.toString()); + writeset.add(mapDocId, handle1); + + uris[i] = mapDocId; + + mapDocContent.setLength(0); + mapDocId = null; + docMgr.write(writeset); + writeset.clear(); + } + + int count=0; + + DocumentPage page = docMgr.read(uris); + DocumentRecord rec; + + JacksonHandle jh = new JacksonHandle(); + jh.withFormat(Format.JSON); + while(page.hasNext()){ + rec = page.next(); + + assertNotNull("DocumentRecord should never be null", rec); + assertNotNull("Document uri should never be null", rec.getUri()); + assertTrue("Document uri should start with " + DIRECTORY, rec.getUri().startsWith(DIRECTORY)); + + rec.getContent(jh); + //Verify the contents: comparing Map with JacksonHandle's. + assertEquals("Comparing the content :",jsonMap.get(rec.getUri()),jh.get().toString()); + count++; + } + assertEquals("document count", 102,count); + + } @AfterClass public static void tearDown() throws Exception { System.out.println("In tear down"); tearDownJavaRESTServer(dbName, fNames, restServerName); } - - public void validateRecord(DocumentRecord record, Format type) { - - assertNotNull("DocumentRecord should never be null", record); - assertNotNull("Document uri should never be null", record.getUri()); - assertTrue("Document uri should start with " + DIRECTORY, record - .getUri().startsWith(DIRECTORY)); - assertEquals("All records are expected to be in same format", type, - record.getFormat()); - - } } \ No newline at end of file diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithDocsStoredByOthers.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithDocsStoredByOthers.java index 8a0644d3a..f56a3f8e7 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithDocsStoredByOthers.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPOJOWithDocsStoredByOthers.java @@ -541,7 +541,7 @@ public void testPOJOWriteReadDiffAccessSpecifiers() throws Exception { } /* - * Purpose : This test is to validate creating an sub class which is references by a Super class variable type. + * Purpose : This test is to validate creating an sub class which is referenced by a Super class variable type. * Both SmallArtifactIdInSuper and SmallArtifactNoId classes are used. * POJO repository cannot read back the sub class. * @@ -569,7 +569,7 @@ public void testPOJOSubObjReferencedBySuperClassVariable() throws Exception { } /* - * Purpose : This test is to validate creating an sub class which is references by a Super class variable type. + * Purpose : This test is to validate creating an sub class which is referenced by a Super class variable type. * Both SmallArtifactIdInSuper and SmallArtifactNoId classes are used. * This is a variation of testPOJOSubObjReferencedBySuperClassVariable() * From 90855296cc77f66784e9020d896d385d0f9ae246 Mon Sep 17 00:00:00 2001 From: Ajit George Date: Fri, 14 Nov 2014 18:06:05 -0800 Subject: [PATCH 344/357] Fixed 3 of the 4 regressions --- .../com/marklogic/javaclient/TestPartialUpdate.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java index ee74c5877..06ace8023 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java @@ -122,8 +122,9 @@ public void testPartialUpdateJSON() throws IOException fragmentNode = mapper.createObjectNode(); fragmentNode.put("insertedKey", 9); String fragment = mapper.writeValueAsString(fragmentNode); - - patchBldr.insertFragment("$.employees", Position.LAST_CHILD, fragment); + + String jsonpath = new String("$.employees[2]"); + patchBldr.insertFragment(jsonpath, Position.AFTER, fragment); DocumentPatchHandle patchHandle = patchBldr.build(); docMgr.patch(docId, patchHandle); @@ -535,7 +536,7 @@ public void testPartialUpdateCombinationTranscRevert() throws Exception{ @Test public void testPartialUpdateCombinationJSON() throws Exception{ System.out.println("Running testPartialUpdateCombinationJSON"); - DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST); + DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST); // write docs String[] filenames = {"json-original.json"}; @@ -573,7 +574,7 @@ public void testPartialUpdateCombinationJSON() throws Exception{ client.release(); } - + @Test public void testPartialUpdateMetadata() throws Exception{ System.out.println("Running testPartialUpdateMetadata"); @@ -711,7 +712,7 @@ public void testPartialUpdateJSONDescriptor() throws IOException fragmentNode.put("insertedKey", 9); String fragment = mapper.writeValueAsString(fragmentNode); - patchBldr.insertFragment("$.employees", Position.LAST_CHILD, fragment); + patchBldr.insertFragment("$.employees[2]", Position.AFTER, fragment); DocumentPatchHandle patchHandle = patchBldr.build(); docMgr.patch(desc, patchHandle); @@ -795,7 +796,7 @@ public void testPartialUpdateJSONDescriptorTranc() throws IOException fragmentNode.put("insertedKey", 9); String fragment = mapper.writeValueAsString(fragmentNode); patchBldr.pathLanguage(PathLanguage.JSONPATH); - patchBldr.insertFragment("$.employees", Position.LAST_CHILD, fragment); + patchBldr.insertFragment("$.employees[2]", Position.AFTER, fragment); DocumentPatchHandle patchHandle = patchBldr.build(); // Transaction t = client.openTransaction("Tranc"); docMgr.patch(desc, patchHandle);//,t); From 743688a52e142a219dbb54450e20512c5c33f6fd Mon Sep 17 00:00:00 2001 From: sammefford Date: Mon, 17 Nov 2014 14:35:49 -0700 Subject: [PATCH 345/357] make multi-line curl command work --- .../com/marklogic/client/pojo/util/GenerateIndexConfig.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/marklogic/client/pojo/util/GenerateIndexConfig.java b/src/main/java/com/marklogic/client/pojo/util/GenerateIndexConfig.java index b2f5aba1a..1a7df92cd 100644 --- a/src/main/java/com/marklogic/client/pojo/util/GenerateIndexConfig.java +++ b/src/main/java/com/marklogic/client/pojo/util/GenerateIndexConfig.java @@ -46,9 +46,9 @@ * -classes "com.marklogic.client.test.City com.marklogic.client.test.Country" * -file cityIndexes.json * - * curl -i --digest --user admin:admin - * -H 'Content-Type: application/json' - * -d '@cityIndexes.json' + * curl -i --digest --user admin:admin \ + * -H 'Content-Type: application/json' \ + * -d '@cityIndexes.json' \ * -X PUT 'http://localhost:8002/manage/LATEST/databases/java-unittest/properties' *
*/ From 12892e722f8d120f9c5c03c50da79b93197eade0 Mon Sep 17 00:00:00 2001 From: sammefford Date: Mon, 17 Nov 2014 15:35:06 -0700 Subject: [PATCH 346/357] add javadocs --- .../client/document/DocumentRecord.java | 36 ++++++++++++++ .../document/DocumentWriteOperation.java | 48 ++++++++++++++++++- .../client/document/DocumentWriteSet.java | 35 ++++++++++++++ 3 files changed, 118 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/marklogic/client/document/DocumentRecord.java b/src/main/java/com/marklogic/client/document/DocumentRecord.java index 21568678f..524a13011 100644 --- a/src/main/java/com/marklogic/client/document/DocumentRecord.java +++ b/src/main/java/com/marklogic/client/document/DocumentRecord.java @@ -19,13 +19,49 @@ import com.marklogic.client.io.marker.AbstractReadHandle; import com.marklogic.client.io.marker.DocumentMetadataReadHandle; +/** Each DocumentRecord represents one document, its uri, format, mime-type + * and possibly its metadata (collections, properties, quality, and permissions). + * Whether metadata is included depends on whether it was requested in the call + * sent to the server. For example, to request collections metadata: + *
{@code
+ *JSONDocumentManager docMgr = databaseClient.newJSONDocumentManager();
+ *docMgr.setNonDocumentFormat(Format.XML);
+ *docMgr.setMetadataCategories(Metadata.COLLECTIONS);
+ *DocumentPage documents = docMgr.read("doc1.json", "doc2.json");
+ *for ( DocumentRecord record : documents ) {
+ *    String uri = record.getUri();
+ *    JacksonHandle content = record.getContent(new JacksonHandle());
+ *    DocumentMetadataHandle metadata = record.getMetadata(new DocumentMetadataHandle());
+ *    DocumentCollections collections = metadata.getCollections();
+ *    // ... do something ...
+ *}
+ *  }
+ */ public interface DocumentRecord { + /** Returns the uri (unique identifier) of the document in the server */ public String getUri(); + /** Returns the format of the document in the server */ public Format getFormat(); + /** Returns the mime-type ("Content-Type" header) of the document as specified by + * the server (uses the server's mime-type mapping for file extensions) */ public String getMimetype(); + /** + * Given a handle, populates the handle with the structured metadata directly from + * the REST API. Depending on the nonDocumentFormat set on the DocumentManager, + * this will be XML or JSON format. If the nonDocumentFormat is XML, you can use + * DocumentMetadataHandle which offers convenient metadata access methods. + * @see + * REST API Dev Guide -> Working with Metadata + */ public T getMetadata(T metadataHandle); + + /** + * Given a handle, populates the handle with the document contents directly from + * the server (or the transformed contents if a ServerTransform was used). Use + * a handle that is appropriate for the {@link #getFormat format} of this document. + */ public T getContent(T contentHandle); } diff --git a/src/main/java/com/marklogic/client/document/DocumentWriteOperation.java b/src/main/java/com/marklogic/client/document/DocumentWriteOperation.java index 20734f00f..05f32c8ed 100644 --- a/src/main/java/com/marklogic/client/document/DocumentWriteOperation.java +++ b/src/main/java/com/marklogic/client/document/DocumentWriteOperation.java @@ -18,14 +18,60 @@ import com.marklogic.client.io.marker.AbstractWriteHandle; import com.marklogic.client.io.marker.DocumentMetadataWriteHandle; +/** A reflection of the write operations queued by calls to add, + * {@link DocumentWriteSet#add add}, {@link DocumentWriteSet#addDefault addDefault}, or + * {@link DocumentWriteSet#disableDefault disableDefault}. + */ public interface DocumentWriteOperation { - public enum OperationType { METADATA_DEFAULT, DISABLE_METADATA_DEFAULT, DOCUMENT_WRITE }; + public enum OperationType { + /** This write operation (REST API mime part) sets the defaults for the + * rest of the request. + * @see + * REST API Guide -> Constructing a Metadata Part + */ + METADATA_DEFAULT, + /** This write operation (REST API mime part) clears the defaults for the + * rest of the request. While this removes defaults set previously on the + * request, this does not completely restore server-side defaults. For + * more information see the + * + * REST API Guide -> Example: Reverting to System Default Metadata + */ + DISABLE_METADATA_DEFAULT, + /** This write operation (REST API mime part) creates or overwrites + * one document and/or document metadata. + * @see + * REST API Guide -> Constructing a Content Part + * @see + * REST API Guide -> Constructing a Metadata Part + * @see + * REST API Guide -> Understanding When Metadata is Preserved or Replaced + */ + DOCUMENT_WRITE + }; + /** Returns the {@link DocumentWriteOperation.OperationType} set implicitly by your call to + * {@link DocumentWriteSet#add add}, {@link DocumentWriteSet#addDefault addDefault}, or + * {@link DocumentWriteSet#disableDefault disableDefault}. + */ public OperationType getOperationType(); + // The uri for this document, whether set explicitly or received from the + // server after a write with a DocumentDescriptor. + /** The uri for this document if set explicitly by your call to + * {@link DocumentWriteSet#add(String, AbstractWriteHandle) add(String, ...)} + */ public String getUri(); + /** The handle with the metadata as set by your call to + * {@link DocumentWriteSet#add(String, DocumentMetadataWriteHandle, AbstractWriteHandle) add} or + * {@link DocumentWriteSet#add(DocumentDescriptor, DocumentMetadataWriteHandle, AbstractWriteHandle) add}. + */ public DocumentMetadataWriteHandle getMetadata(); + /** The handle with the content as set by your call to + * {@link DocumentWriteSet#add(String, AbstractWriteHandle) add} or + * {@link DocumentWriteSet#add(DocumentDescriptor, AbstractWriteHandle) add}. + */ public AbstractWriteHandle getContent(); } diff --git a/src/main/java/com/marklogic/client/document/DocumentWriteSet.java b/src/main/java/com/marklogic/client/document/DocumentWriteSet.java index d83a9d53d..27a7bfed3 100644 --- a/src/main/java/com/marklogic/client/document/DocumentWriteSet.java +++ b/src/main/java/com/marklogic/client/document/DocumentWriteSet.java @@ -20,17 +20,52 @@ import java.util.Set; +/** + * Builds a set of {@link DocumentWriteOperation DocumentWriteOperations} to be sent + * to the server through the REST API as a bulk write request. + * @see DocumentManager#write(DocumentWriteSet) + * @see + * REST API Guide -> Writing Multiple Documents + */ public interface DocumentWriteSet extends Set { + /** Sets the default metadata for this write set for all documents added after this call */ public DocumentWriteSet addDefault(DocumentMetadataWriteHandle metadataHandle); + /** Removes the default metadata for this write set for all documents added after this call */ public DocumentWriteSet disableDefault(); + /** + * Adds to this write set a document with the given docId (server uri) + * and contents provided by the handle + * @param docId the URI identifier for the document + * @param contentHandle a handle for writing the content of the document + */ public DocumentWriteSet add(String docId, AbstractWriteHandle contentHandle); + /** + * Adds to this write set a document with the given docId (server uri), + * metadata, and contents provided by the handle + * @param docId the URI identifier for the document + * @param metadataHandle a handle for writing the metadata of the document + * @param contentHandle a handle for writing the content of the document + */ public DocumentWriteSet add(String docId, DocumentMetadataWriteHandle metadataHandle, AbstractWriteHandle contentHandle); + /** + * Adds to this write set a document with the given uri template, and + * contents provided by the handle + * @param desc a descriptor for the URI identifier, format, and mimetype of the document + * @param contentHandle a handle for writing the content of the document + */ public DocumentWriteSet add(DocumentDescriptor desc, AbstractWriteHandle contentHandle); + /** + * Adds to this write set a document with the given uri template, metadata, + * and contents provided by the handle + * @param desc a descriptor for the URI identifier, format, and mimetype of the document + * @param metadataHandle a handle for writing the metadata of the document + * @param contentHandle a handle for writing the content of the document + */ public DocumentWriteSet add(DocumentDescriptor desc, DocumentMetadataWriteHandle metadataHandle, AbstractWriteHandle contentHandle); } From 61ddaf8b138a861046ff613416473eb8e9bc50a4 Mon Sep 17 00:00:00 2001 From: sammefford Date: Mon, 17 Nov 2014 16:34:34 -0700 Subject: [PATCH 347/357] add javadocs for new temporal methods --- .../client/query/StructuredQueryBuilder.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/src/main/java/com/marklogic/client/query/StructuredQueryBuilder.java b/src/main/java/com/marklogic/client/query/StructuredQueryBuilder.java index e86e4cc2f..1104ef931 100644 --- a/src/main/java/com/marklogic/client/query/StructuredQueryBuilder.java +++ b/src/main/java/com/marklogic/client/query/StructuredQueryBuilder.java @@ -2707,6 +2707,13 @@ private EditableNamespaceContext makeNamespaces() { return newNamespaces; } + /** + * The Allen and ISO SQL 2011 temporal operators available for use in + * {@link #temporalPeriodRange temporalPeriodRange} + * or {@link #temporalPeriodCompare temporalPeriodCompare} queries. + * @see + * Temporal Developer's Guide -> Period Comparison Operators + */ public enum TemporalOperator { ALN_EQUALS, ALN_CONTAINS, @@ -2729,17 +2736,49 @@ public enum TemporalOperator { ISO_IMM_PRECEDES, ISO_EQUALS; }; + /** + * An axis for use in {@link #temporalPeriodRange temporalPeriodRange} + * or {@link #temporalPeriodCompare temporalPeriodCompare} queries. + */ public interface Axis {}; + /** + * A temporal period for use in {@link #temporalPeriodRange temporalPeriodRange} + * queries. + */ public interface Period {}; + /** + * Identify an axis for use in {@link #temporalPeriodRange temporalPeriodRange} + * or {@link #temporalPeriodCompare temporalPeriodCompare} queries. + * @param name the name of the axis as configured in the server + */ public StructuredQueryBuilder.Axis axis(String name) { return new TemporalAxis(name); } + /** + * Construct a temporal period for use in {@link #temporalPeriodRange temporalPeriodRange} + * queries. + * @param start the start date/time for this period + * @param end the end date/time for this period + */ public StructuredQueryBuilder.Period period(Calendar start, Calendar end) { return new TemporalPeriod(start, end); } + /** + * Matches documents that have a value in the specified axis that matches the specified + * period using the specified operator. + * @param axis the axis of document temporal values used to determine which documents have + * values that match this query + * @param operator the operator used to determine if values in the axis match the specified period + * @param period the period considered using the operator + * @param options string options from the list for + * cts:period-range-query calls + * @see cts:period-range-query + * @see + * Structured Queries: period-range-query + */ public StructuredQueryDefinition temporalPeriodRange(Axis axis, TemporalOperator operator, Period period, String... options) { @@ -2748,6 +2787,18 @@ public StructuredQueryDefinition temporalPeriodRange(Axis axis, TemporalOperator return temporalPeriodRange(new Axis[] {axis}, operator, new Period[] {period}, options); } + /** + * @param axes the set of axes of document temporal values used to determine which documents have + * values that match this query + * @param operator the operator used to determine if values in the axis match the specified period + * @param periods the periods considered using the operator. When multiple periods are specified, + * the query matches if a value matches any period. + * @param options string options from the list for + * cts:period-range-query calls + * @see cts:period-range-query + * @see + * Structured Queries: period-range-query + */ public StructuredQueryDefinition temporalPeriodRange(Axis[] axes, TemporalOperator operator, Period[] periods, String... options) { @@ -2757,6 +2808,19 @@ public StructuredQueryDefinition temporalPeriodRange(Axis[] axes, TemporalOperat return new TemporalPeriodRangeQuery(axes, operator, periods, options); } + /** + * Matches documents that have a relevant pair of period values. Values from axis1 must match + * values from axis2 using the specified operator. + * @param axis1 the first axis of document temporal values + * values that match this query + * @param operator the operator used to determine if values in the axis match the specified period + * @param axis2 the second axis of document temporal values + * @param options string options from the list for + * cts:period-compare-query calls + * @see cts:period-compare-query + * @see + * Structured Queries: period-compare-query + */ public StructuredQueryDefinition temporalPeriodCompare(Axis axis1, TemporalOperator operator, Axis axis2, String... options) { @@ -2766,6 +2830,17 @@ public StructuredQueryDefinition temporalPeriodCompare(Axis axis1, TemporalOpera return new TemporalPeriodCompareQuery(axis1, operator, axis2, options); } + /** + * Matches documents with LSQT prior to timestamp + * @param temporalCollection the temporal collection to query + * @param timestamp documents with lsqt prior to this timestamp will match + * @param weight the weight for for this query + * @param options string options from the list for + * cts:lsqt-query calls + * @see cts:lsqt-query + * @see + * Structured Queries: lsqt-query + */ public StructuredQueryDefinition temporalLsqtQuery(String temporalCollection, Calendar timestamp, double weight, String... options) { From 4659442d01a96725045a472f31866c6ca48d286c Mon Sep 17 00:00:00 2001 From: sammefford Date: Mon, 17 Nov 2014 16:35:48 -0700 Subject: [PATCH 348/357] fix javadoc where content should have been contentHandle --- .../client/bitemporal/TemporalDocumentManager.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/marklogic/client/bitemporal/TemporalDocumentManager.java b/src/main/java/com/marklogic/client/bitemporal/TemporalDocumentManager.java index ecc3a7ec4..80bc50842 100644 --- a/src/main/java/com/marklogic/client/bitemporal/TemporalDocumentManager.java +++ b/src/main/java/com/marklogic/client/bitemporal/TemporalDocumentManager.java @@ -37,7 +37,7 @@ public interface TemporalDocumentManager. * @param template the template for constructing the document uri * @param metadataHandle a handle for writing the metadata of the document - * @param content an IO representation of the document content + * @param contentHandle an IO representation of the document content * @param transform a server transform to modify the document content * @param transaction an open transaction under which the document may have been created or deleted * @param temporalCollection the name of the temporal collection existing in the database into @@ -60,7 +60,7 @@ public DocumentDescriptor create(DocumentUriTemplate template, * bitemporal data management. * @param desc a descriptor for the URI identifier, format, and mimetype of the document * @param metadataHandle a handle for writing the metadata of the document - * @param content an IO representation of the document content + * @param contentHandle an IO representation of the document content * @param transform a server transform to modify the document content * @param transaction an open transaction under which the document may have been created or deleted * @param temporalCollection the name of the temporal collection existing in the database into @@ -82,7 +82,7 @@ public void write(DocumentDescriptor desc, * bitemporal data management. * @param docId the URI identifier for the document * @param metadataHandle a handle for writing the metadata of the document - * @param content an IO representation of the document content + * @param contentHandle an IO representation of the document content * @param transform a server transform to modify the document content * @param transaction an open transaction under which the document may have been created or deleted * @param temporalCollection the name of the temporal collection existing in the database into @@ -134,7 +134,7 @@ public void delete(String docId, * at a specific system time * @param template the template for constructing the document uri * @param metadataHandle a handle for writing the metadata of the document - * @param content an IO representation of the document content + * @param contentHandle an IO representation of the document content * @param transform a server transform to modify the document content * @param transaction an open transaction under which the document may have been created or deleted * @param temporalCollection the name of the temporal collection existing in the database into @@ -159,7 +159,7 @@ public DocumentDescriptor create(DocumentUriTemplate template, * at a specific system time * @param desc a descriptor for the URI identifier, format, and mimetype of the document * @param metadataHandle a handle for writing the metadata of the document - * @param content an IO representation of the document content + * @param contentHandle an IO representation of the document content * @param transform a server transform to modify the document content * @param transaction an open transaction under which the document may have been created or deleted * @param temporalCollection the name of the temporal collection existing in the database into @@ -183,7 +183,7 @@ public void write(DocumentDescriptor desc, * at a specific system time * @param docId the URI identifier for the document * @param metadataHandle a handle for writing the metadata of the document - * @param content an IO representation of the document content + * @param contentHandle an IO representation of the document content * @param transform a server transform to modify the document content * @param transaction an open transaction under which the document may have been created or deleted * @param temporalCollection the name of the temporal collection existing in the database into From 988f5bd97aeb4fda631275619aa99a63fd6e5cfd Mon Sep 17 00:00:00 2001 From: sammefford Date: Mon, 17 Nov 2014 16:39:31 -0700 Subject: [PATCH 349/357] oops, adding those overrides for docs purpose also meant I needed the import of ObjectMapper --- src/main/java/com/marklogic/client/io/JacksonParserHandle.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/marklogic/client/io/JacksonParserHandle.java b/src/main/java/com/marklogic/client/io/JacksonParserHandle.java index e65daf9d3..25f4b6b39 100644 --- a/src/main/java/com/marklogic/client/io/JacksonParserHandle.java +++ b/src/main/java/com/marklogic/client/io/JacksonParserHandle.java @@ -22,6 +22,7 @@ import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.ObjectMapper; import com.marklogic.client.MarkLogicIOException; import com.marklogic.client.impl.JacksonBaseHandle; import com.marklogic.client.io.marker.BufferableHandle; From a53a73f6ba407ab96742a04b1bb306419bc53c8f Mon Sep 17 00:00:00 2001 From: Ajit George Date: Mon, 17 Nov 2014 16:06:53 -0800 Subject: [PATCH 350/357] Test cases for Git #94 and regression fix to Jsonpath in patch combination test method --- .../javaclient/TestPartialUpdate.java | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java b/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java index 06ace8023..4e1b8a797 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java @@ -18,6 +18,7 @@ import com.marklogic.client.DatabaseClient; import com.marklogic.client.DatabaseClientFactory; import com.marklogic.client.DatabaseClientFactory.Authentication; +import com.marklogic.client.FailedRequestException; import com.marklogic.client.Transaction; import com.marklogic.client.admin.ExtensionLibrariesManager; import com.marklogic.client.document.DocumentDescriptor; @@ -96,6 +97,28 @@ public void testPartialUpdateXML() throws IOException // release client client.release(); } + + /* Used to test Git issue # 94 along with uber-app server. use a bad user to authenticate client. + * Should be throwing FailedRequestException Exception. + * Message : Local message: write failed: Unauthorized. Server Message: Unauthorized + */ + @Test(expected=FailedRequestException.class) + public void testJSONParserException() throws IOException + { + System.out.println("Running testPartialUpdateJSON"); + + String[] filenames = {"json-original.json"}; + + DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "bad-eval-user", "x", Authentication.DIGEST); + + // write docs + for(String filename : filenames) + { + writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "JSON"); + } + // release client + client.release(); + } @Test public void testPartialUpdateJSON() throws IOException @@ -533,6 +556,10 @@ public void testPartialUpdateCombinationTranscRevert() throws Exception{ } + /* We have Git issue #199 that tracks multiple patch on same JSONPath index. + * This test uses different path index. This test was modified to account for the + * correct path index elements. + */ @Test public void testPartialUpdateCombinationJSON() throws Exception{ System.out.println("Running testPartialUpdateCombinationJSON"); @@ -558,7 +585,8 @@ public void testPartialUpdateCombinationJSON() throws Exception{ fragmentNode = mapper.createObjectNode(); fragmentNode.put("insertedKey", 9); String fragment = mapper.writeValueAsString(fragmentNode); - patchBldr.insertFragment("$.employees", Position.LAST_CHILD, fragment).delete("$.employees[2]").replaceApply("$.employees[1].firstName", patchBldr.call().concatenateAfter("Hi")); + // Original - patchBldr.insertFragment("$.employees", Position.LAST_CHILD, fragment).delete("$.employees[2]").replaceApply("$.employees[1].firstName", patchBldr.call().concatenateAfter("Hi")); + patchBldr.insertFragment("$.employees[0]", Position.AFTER, fragment).delete("$.employees[2]").replaceApply("$.employees[1].firstName", patchBldr.call().concatenateAfter("Hi")); DocumentPatchHandle patchHandle = patchBldr.build(); docMgr.patch(docId, patchHandle); @@ -566,7 +594,7 @@ public void testPartialUpdateCombinationJSON() throws Exception{ System.out.println("After" + content); - assertTrue("fragment is not inserted", content.contains("{\"insertedKey\":9}]")); + assertTrue("fragment is not inserted", content.contains("{\"insertedKey\":9}")); assertTrue("fragment is not inserted", content.contains("{\"firstName\":\"AnnHi\", \"lastName\":\"Smith\"}")); assertFalse("fragment is not deleted",content.contains("{\"firstName\":\"Bob\", \"lastName\":\"Foo\"}")); From 47f28a488cd99564899a618fbb244a10d3f1155f Mon Sep 17 00:00:00 2001 From: Ajit George Date: Tue, 18 Nov 2014 08:42:44 -0800 Subject: [PATCH 351/357] Regression fix --- .../marklogic/javaclient/TestJacksonDateTimeFormat.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestJacksonDateTimeFormat.java b/test-complete/src/test/java/com/marklogic/javaclient/TestJacksonDateTimeFormat.java index f18a790c1..416e90f75 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestJacksonDateTimeFormat.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestJacksonDateTimeFormat.java @@ -342,6 +342,13 @@ public void testPOJORepoDeleteDateTime() { validateSpecialArtifactDateTime(artifact, artifactName, longId, calTime); pojoReposProducts.delete(calTime); + // Introduce a wait for the document to be deleted. + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } // Validate the artifact read back. long count = pojoReposProducts.count(); From 7acd5847f71a6f65d1303cce9dce61f2f91aa5b6 Mon Sep 17 00:00:00 2001 From: sammefford Date: Tue, 18 Nov 2014 13:59:43 -0700 Subject: [PATCH 352/357] updating version --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 03b11392f..b8c083da7 100644 --- a/pom.xml +++ b/pom.xml @@ -4,11 +4,11 @@ com.marklogic client-api-java jar - 3.0-SNAPSHOT + 3.0.0-EA3 client-api-java http://developer.marklogic.com - ${project.name} 2.0 ${maven.build.timestamp} + ${project.name} 3.0.0 yyyy-MM-dd UTF-8 github From cf693c80c0eba850fd73864f6fb6c5a22c00bf8b Mon Sep 17 00:00:00 2001 From: skottam Date: Thu, 20 Nov 2014 10:41:58 -0800 Subject: [PATCH 353/357] updated after bug fix 30595 --- .../test/java/com/marklogic/javaclient/TestEvalJavaScript.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-complete/src/test/java/com/marklogic/javaclient/TestEvalJavaScript.java b/test-complete/src/test/java/com/marklogic/javaclient/TestEvalJavaScript.java index 00ad25d97..b67ad6eef 100644 --- a/test-complete/src/test/java/com/marklogic/javaclient/TestEvalJavaScript.java +++ b/test-complete/src/test/java/com/marklogic/javaclient/TestEvalJavaScript.java @@ -370,7 +370,7 @@ public void testJSDifferentVariableTypes() throws Exception { +"var myNull;" + "var myJsonArray;" + "var myJsonNull;" - + "results.push(myString,myBool,myInteger,myDecimal,myJsonObject,myJsonArray,myNull);" + + "results.push(myString,myBool,myInteger,myDecimal,myJsonObject,myJsonArray,myNull,myJsonNull);" +"xdmp.arrayValues(results)"; ServerEvaluationCall evl= client.newServerEval().javascript(query1); From 6b938ae1afd376cf09f71810942394bff1c73b10 Mon Sep 17 00:00:00 2001 From: sammefford Date: Fri, 21 Nov 2014 11:09:14 -0700 Subject: [PATCH 354/357] more javadocs --- .../com/marklogic/client/eval/EvalResult.java | 45 ++++ .../client/eval/EvalResultIterator.java | 3 + .../client/eval/ServerEvaluationCall.java | 170 +++++++++++- .../marklogic/client/pojo/PojoRepository.java | 244 ++++++++++++++++-- 4 files changed, 434 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/marklogic/client/eval/EvalResult.java b/src/main/java/com/marklogic/client/eval/EvalResult.java index 509c1d7d4..dc78221bf 100644 --- a/src/main/java/com/marklogic/client/eval/EvalResult.java +++ b/src/main/java/com/marklogic/client/eval/EvalResult.java @@ -18,7 +18,18 @@ import com.marklogic.client.io.Format; import com.marklogic.client.io.marker.AbstractReadHandle; +/** Represents the content and metadata of a result from a call to + * {@link ServerEvaluationCall#eval() eval}. + * Also includes some convenience methods for deserializing the result + * as a String, Number, Boolean, AbstractReadHandle, or an object of + * any Class which is registered by a ContentHandle. + */ public interface EvalResult { + /** Convenience enum useful for strongly-typed comparison to ensure + * the result is of the type expected. This list captures most of + * the types expected from the X-Primitive header. + * @see REST GUIDE -> eval endpoint + */ enum Type { XML, JSON, STRING, BOOLEAN, NULL, OTHER, @@ -26,11 +37,45 @@ enum Type { FLOAT, GDAY, GMONTH, GMONTHDAY, GYEAR, GYEARMONTH, HEXBINARY, INTEGER, QNAME, TIME, ATTRIBUTE, BINARY, COMMENT, PROCESSINGINSTRUCTION, TEXTNODE }; + /** The {@link Type} of this result from the X-Primitive header + * provided by the REST API eval endpoint. + * @see REST GUIDE -> eval endpoint + * @return the type of this result + */ public Type getType(); + + /** The format of this result from the Content-Type header + * provided by the REST API eval endpoint. + * @see REST GUIDE -> eval endpoint + * @return the format of this result + */ public Format getFormat(); + + /** Use a handle to retrieve the contents of this result. + * @param handle the handle to populate with the contents of this result + * @return the handle populated with the contents of this result + */ public H get(H handle); + + /** An io shortcut method to get the contents as an object + * of the specified type, which must be a registered type. + * @param clazz the Class (type). This type must be registered by an io handle. + * @return an instance of the requested type populated with the contents of this result + */ public T getAs(Class clazz); + + /** This result directly as a string. + * @return this result as a string + */ public String getString(); + + /** This result parsed to a Number. + * @return this result as a number + */ public Number getNumber(); + + /** This result parsed to a Boolean. + * @return a boolean as generated by new Boolean({@link #getString getString()}) + */ public Boolean getBoolean(); } diff --git a/src/main/java/com/marklogic/client/eval/EvalResultIterator.java b/src/main/java/com/marklogic/client/eval/EvalResultIterator.java index ac058d050..761bc53b0 100644 --- a/src/main/java/com/marklogic/client/eval/EvalResultIterator.java +++ b/src/main/java/com/marklogic/client/eval/EvalResultIterator.java @@ -17,6 +17,9 @@ import java.util.Iterator; +/** An Iterator to walk through all results returned from calls to + * {@link ServerEvaluationCall#eval()}. + */ public interface EvalResultIterator extends Iterable, Iterator { public Iterator iterator(); public boolean hasNext(); diff --git a/src/main/java/com/marklogic/client/eval/ServerEvaluationCall.java b/src/main/java/com/marklogic/client/eval/ServerEvaluationCall.java index 2747361bf..47539b70f 100644 --- a/src/main/java/com/marklogic/client/eval/ServerEvaluationCall.java +++ b/src/main/java/com/marklogic/client/eval/ServerEvaluationCall.java @@ -15,6 +15,7 @@ */ package com.marklogic.client.eval; +import com.marklogic.client.DatabaseClientFactory; import com.marklogic.client.FailedRequestException; import com.marklogic.client.ForbiddenUserException; import com.marklogic.client.Transaction; @@ -23,26 +24,189 @@ import com.marklogic.client.io.marker.TextWriteHandle; import com.marklogic.client.util.EditableNamespaceContext; +/** + * ServerEvaluationCall uses a fluent builder-style API to collect the parameters + * for a server-side {@link #xquery xquery} or {@link #javascript javascript} eval or + * invoke ({@link #modulePath modulePath}) call. ServerEvaluationCall also + * conveniently has the eval* methods which execute those calls and return the + * results. You must call one and only one of the following methods: xquery, + * javascript, or modulePath. The xquery + * and javascript methods initialize this call for server-side eval and accept + * source code as a String or a TextWriteHandle (in case you are streaming the + * source code from the file system, a URL, or other source that is most easily + * accessed via io handles). The modulePath method initializes this call for server- + * side invoke given the path to a module previously installed on the server. + * + * Here is a simple "hello world" junit example:
{@code
+ *String javascript = "'hello world'";
+ *String response = client.newServerEval()
+ *    .javascript(javascript)
+ *    .evalAs(String.class);
+ *assertEquals("hello world", response);
+ *}
+ * or in xquery:
{@code
+ *String xquery = "'hello world'";
+ *String response = client.newServerEval()
+ *    .xquery(xquery)
+ *    .evalAs(String.class);
+ *assertEquals("hello world", response);
+ *}
+ * + * Variables can be added with the addVariable methods. + * {@link #addVariable(String, AbstractWriteHandle) addVariable(String, AbstractWriteHandle)} + * allows you to pass complex JSON or XML values directly from io handles. + * {@link #addVariableAs(String, Object) addVariableAs(String, Object)} + * follows the + * shortcut pattern which maps objects by type to the appropriate handle. + * For simpler atomic values, convenience addVariable methods are provided for + * String, Number, and Boolean types. + * + * Here is a simple "hello solar system" example with a variable:
{@code
+ *String javascript = "var planet;'hello solar system from ' + planet";
+ *String response = client.newServerEval()
+ *    .javascript(javascript)
+ *    .addVariable("planet", "Mars)
+ *    .evalAs(String.class);
+ *assertEquals( "hello solar system from Mars", response);
+ *}
+ * or in xquery:
{@code
+ *String xquery = "declare variable $planet external;'hello solar system from ' || $planet";
+ *String response = client.newServerEval()
+ *    .xquery(xquery)
+ *    .addVariable("planet", "Mars)
+ *    .evalAs(String.class);
+ *assertEquals( "hello solar system from Mars", response);
+ * }
+ * + * Each call can be executed within a {@link #transaction transaction}, within a + * {@link DatabaseClientFactory#newClient(String, int, String) particular database}, + * and with particular {@link #namespaceContext namespaces} available for expansion + * of prefixed variable names. + * + * Each call can be executed with only one expected response of a particular + * {@link #evalAs type} or {@link #eval(AbstractReadHandle) handle type}. Or calls can be executed + * with {@link #eval() multiple responses expected}. + */ public interface ServerEvaluationCall { + /** Initialize this server-side eval with xquery-syntax source code. + * @param xquery the xquery-syntax source code to eval on the server + * @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder + */ public ServerEvaluationCall xquery(String xquery); + + /** Initialize this server-side eval with xquery-syntax source code. + * @param xquery a handle containing the xquery-syntax source code to eval on the server + * @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder + */ public ServerEvaluationCall xquery(TextWriteHandle xquery); + + /** Initialize this server-side eval with javascript-syntax source code. + * @param javascript the javascript-syntax source code to eval on the server + * @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder + */ public ServerEvaluationCall javascript(String javascript); - public ServerEvaluationCall javascript(TextWriteHandle xquery); + + /** Initialize this server-side eval call with javascript-syntax source code. + * @param javascript a handle containing the javascript-syntax source code to eval on the server + * @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder + */ + public ServerEvaluationCall javascript(TextWriteHandle javascript); + + /** Initialize this server-side invoke call with a path to the module to invoke. + * @param modulePath a path to a module previously installed in the server + * @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder + */ public ServerEvaluationCall modulePath(String modulePath); + + /** Set a variable name-value pair to pass to the code executing server-side. + * @param name the variable name, including a namespace prefix if the prefix is + * mapped to a uri in the {@link #namespaceContext namespace context} + * @param value the atomic variable value + * @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder + */ public ServerEvaluationCall addVariable(String name, String value); + + /** Set a variable name-value pair to pass to the code executing server-side. + * @param name the variable name, including a namespace prefix if the prefix is + * mapped to a uri in the {@link #namespaceContext namespace context} + * @param value the atomic variable value + * @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder + */ public ServerEvaluationCall addVariable(String name, Number value); + + /** Set a variable name-value pair to pass to the code executing server-side. + * @param name the variable name, including a namespace prefix if the prefix is + * mapped to a uri in the {@link #namespaceContext namespace context} + * @param value the atomic variable value + * @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder + */ public ServerEvaluationCall addVariable(String name, Boolean value); + + /** Set a variable name-value pair to pass to the code executing server-side. + * @param name the variable name, including a namespace prefix if the prefix is + * mapped to a uri in the {@link #namespaceContext namespace context} + * @param value the handle containing the variable value, most likely XML or JSON + * @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder + */ public ServerEvaluationCall addVariable(String name, AbstractWriteHandle value); - /** Like other *As convenience methods throughout the API, the Object value - * is managed by the Handle registered for that Class. */ + + /** Convenience method to set a variable of a type mapped to an io handle. Like other + * + * *As convenience methods throughout the API, the Object value + * is provided by the handle registered for that Class. + * + * @param name the variable name, including a namespace prefix if the prefix is + * mapped to a uri in the {@link #namespaceContext namespace context} + * @param value the handle containing the variable value + * @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder + */ public ServerEvaluationCall addVariableAs(String name, Object value); + + /** Initialize this call with a transaction under which server-side execution should occur. + * @param transaction the open transaction under which to run this call in the server + * @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder + */ public ServerEvaluationCall transaction(Transaction transaction); + + /** Add a single namespace prefix-to-uri mapping to the namespace context. + * @param prefix the prefix for this mapping + * @param namespaceURI the uri for this mapping + * @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder + */ public ServerEvaluationCall addNamespace(String prefix, String namespaceURI); + + /** Initialize this call with namespaces so variables with prefixes can be sent with + * their prefixes translated to uris that will match the uris in the code to be + * executed on the server. + * @param namespaces a namespace context specifying the mapping from prefixes to namespaces + * @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder + */ public ServerEvaluationCall namespaceContext(EditableNamespaceContext namespaces); + + /** Conveneince method to get the response serialized to a particular type by an io handle. + * Like other + * *As convenience methods throughout the API, the return value + * is provided by the handle registered for the provided responseType. + * + * @param responseType the type desired for the response. Must be a Class regiestered + * to a handle. + * @return the result deserialized by the implicit handle mapped to this responseType + */ public T evalAs(Class responseType) throws ForbiddenUserException, FailedRequestException; + + /** Provides the single result of the server-side eval or invoke call, wrapped in an io + * handle. + * @param responseHandle the type of handle appropriate for the expected single result + * @return the handle which wraps the response + */ public H eval(H responseHandle) throws ForbiddenUserException, FailedRequestException; + + /** Provides all results returned by the server-side eval or invoke call. + * @return an EvalResultIterator which provides access to all the results returned by + * the server-side eval or invoke call. + */ public EvalResultIterator eval() throws ForbiddenUserException, FailedRequestException; } diff --git a/src/main/java/com/marklogic/client/pojo/PojoRepository.java b/src/main/java/com/marklogic/client/pojo/PojoRepository.java index 3233f7bb6..89ab5be08 100644 --- a/src/main/java/com/marklogic/client/pojo/PojoRepository.java +++ b/src/main/java/com/marklogic/client/pojo/PojoRepository.java @@ -20,6 +20,8 @@ import com.marklogic.client.ResourceNotFoundException; import com.marklogic.client.Transaction; import com.marklogic.client.io.marker.SearchReadHandle; +import com.marklogic.client.pojo.annotation.Id; +import com.marklogic.client.query.QueryManager; import java.io.Serializable; @@ -43,7 +45,7 @@ * * Where MyClass is your custom pojo type, and myId is the bean property of type Integer * marked with the - * {@literal @}{@link com.marklogic.client.pojo.annotation.Id Id annotation}. The + * {@literal @}{@link Id Id annotation}. The * {@literal @}Id annotaiton can be attached to a public field or a public getter or a * public setter. The bean property marked with {@literal @}Id must be a native type or * {@link java.io.Serializable} class and must contain an @@ -78,66 +80,117 @@ public interface PojoRepository { /** Write this instance to the database. Uses the field marked with {@literal @}Id * annotation to generate a unique uri for the document. Adds a collection with the * fully qualified class name. Uses a particular configuration of - * {@link com.fasterxml.jackson.databind.ObjectMapper ObjectMapper} to generate the - * serialized JSON format. + * {@link com.fasterxml.jackson.databind.ObjectMapper ObjectMapper} to generate the + * serialized JSON format. + * @param entity your pojo instance of the type managed by this PojoRepository */ public void write(T entity) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException; /** Does everything in {@link #write(Object) write(T)} but also adds your collections to the * persisted instance. + * @param entity your pojo instance of the type managed by this PojoRepository + * @param collections the collections to add to this instance in the database */ public void write(T entity, String... collections) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException; /** Does everything in {@link #write(Object) write(T)} but in your * * multi-statement transaction context. + * @param entity your pojo instance of the type managed by this PojoRepository + * @param transaction the open transaction in which to write this instance */ public void write(T entity, Transaction transaction) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException; - /** Does everything in {@link #write(Object) write(T)} but also adds your collections to the - * persisted instance and performs the write in your + /** Does everything in {@link #write(Object) write(T)} but also adds your + * collections to the persisted instance and performs the write in your * * multi-statement transaction context. * . + * @param entity your pojo instance of the type managed by this PojoRepository + * @param transaction the open transaction in which to write this instance + * @param collections the collections to add to this instance in the database */ public void write(T entity, Transaction transaction, String... collections) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException; - /** @return true if a document exists in the database with the id */ + /** True if a document exists in the database with the specified id + * @param id the unique identifier of the pojo (the value of the field annotated with + * {@literal @}{@link Id Id}) + * @return true if a document exists in the database with the specified id + */ public boolean exists(ID id) throws ForbiddenUserException, FailedRequestException; - /** @return in the context of transaction, true if a document exists in the database with - * the id */ + /** True if in the context of transaction, a document exists in the database with + * the specified id + * @param id the unique identifier of the pojo (the value of the field annotated with + * {@literal @}{@link Id Id}) + * @param transaction the transaction in which this exists check is participating + * (Will open a read lock on the document. The read lock is released when the + * transaction is committed or rolled back.) + * @return true if in the context of transaction, a document exists in the database + * with the specified id + */ public boolean exists(ID id, Transaction transaction) throws ForbiddenUserException, FailedRequestException; - /** @return the number of documents of type T persisted in the database */ + /** The number of documents of the type managed by this PojoRepository persisted + * in the database + * @return The number of documents of the type + * managed by this PojoRepository persisted in the database + */ public long count() throws ForbiddenUserException, FailedRequestException; - /** @return in the context of transaction, the number of documents of type T persisted in - * the database */ + /** In the context of transaction, the number of documents of the type managed by + * this PojoRepository persisted in the database + * @param transaction the transaction in which this count is participating + * (Will open a read lock on all matched documents. The read lock is released when the + * transaction is committed or rolled back.) + * @return in the context of transaction, the number of documents of the type managed + * by this PojoRepository persisted in the database + */ public long count(Transaction transaction) throws ForbiddenUserException, FailedRequestException; - /** @return the number of documents of type T persisted in the database with at least - * one of the criteria collections*/ - public long count(String... collection) + /** The number of documents of the type managed by this PojoRepository persisted in + * the database with at least one of the criteria collections + * @param collections matches must belong to at least one of the specified collections + * @return the number of documents of the type managed + * by this PojoRepository persisted in the database with at least one of the + * criteria collections + */ + public long count(String... collections) throws ForbiddenUserException, FailedRequestException; - /** @return in the context of transaction, the number of documents of type T persisted in - * the database with at least one of the criteria collections*/ + /** In the context of transaction, the number of documents of the type managed by + * this PojoRepository persisted in the database with at least one of the criteria + * collections + * @param collections matches must belong to at least one of the specified collections + * @param transaction the transaction in which this count is participating + * (Will open a read lock on all matched documents. The read lock is released when the + * transaction is committed or rolled back.) + * @return in the context of transaction, the number of documents of the type managed + * by this PojoRepository persisted in the database with at least one of the + * criteria collections + */ public long count(String[] collections, Transaction transaction) throws ForbiddenUserException, FailedRequestException; - /** @return the number of documents of type T persisted in the database which match + /** @return the number of documents of the type managed by this PojoRepository persisted in the database which match * the query */ public long count(PojoQueryDefinition query) throws ForbiddenUserException, FailedRequestException; - /** @return in the context of transaction, the number of documents of type T persisted in the - * database which match the query */ + /** In the context of transaction, the number of documents of the type managed by + * this PojoRepository persisted in the database which match the query + * @param query the query which results much match (queries are run unfiltered by default) + * @param transaction the transaction in which this count is participating + * (Will open a read lock on all matched documents. The read lock is released when the + * transaction is committed or rolled back.) + * @return in the context of transaction, the number of documents of the type managed + * by this PojoRepository persisted in the database which match the query + */ public long count(PojoQueryDefinition query, Transaction transaction) throws ForbiddenUserException, FailedRequestException; @@ -149,11 +202,11 @@ public void delete(ID... ids) public void delete(ID[] ids, Transaction transaction) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException; - /** Deletes from the database all documents of type T persisted by the pojo facade */ + /** Deletes from the database all documents of the type managed by this PojoRepositoryof type T persisted by the pojo facade */ public void deleteAll() throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException; - /** As part of transaction, deletes from the database all documents of type T persisted by + /** As part of transaction, deletes from the database all documents of the type managed by this PojoRepositoryof type T persisted by * the pojo facade */ public void deleteAll(Transaction transaction) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException; @@ -162,35 +215,176 @@ public void deleteAll(Transaction transaction) public void deleteAll(String... collections) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException; */ - + + /** Read one persisted pojo by id and unmarshall its data into a new pojo instance. + * @param id the unique identifier of the pojo (the value of the field annotated with + * {@literal @}{@link Id Id}) + * @return an instance of the correct type populated with the persisted data + * from the database + */ public T read(ID id) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException; + /** Within an open transaction, read one persisted pojo by id and unmarshall its data + * into a new pojo instance. + * @param id the unique identifier of the pojo (the value of the field annotated with + * {@literal @}{@link Id Id}) + * @param transaction the transaction in which this read is participating + * (will open a read lock on each document matched that is released when the + * transaction is committed or rolled back) + * @return an instance of the correct type populated with the persisted data + * from the database + */ public T read(ID id, Transaction transaction) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException; + /** Read multiple persisted pojos by id and unmarshall their data into new pojo instances. + * If at least one instance is found but others are not, ignores the instances not found. + * While this returns a PojoPage, the PageSize will match the number of instances found, + * and will ignore getPageLength(). To paginate, send a smaller set of ids at a time. + * @param ids the unique identifiers of the pojos (the values of the field annotated with + * {@literal @}{@link Id Id}) + * @return a set of instances of the correct type populated with the persisted data. + * Since this call produces a finite set, only one page is returned and therefore + * PojoPage pagination methods will not be helpful as they would be from calls to search. + */ public PojoPage read(ID[] ids) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException; + /** Within an open transaction, + * read one page of persisted pojos and unmarshall their data into new pojo instances. + * If at least one instance is found but others are not, ignores the instances not found. + * While this returns a PojoPage, the PageSize will match the number of instances found, + * and will ignore getPageLength(). To paginate, send a smaller set of ids at a time. + * @param ids the unique identifiers of the pojos (the values of the field annotated with + * {@literal @}{@link Id Id}) + * @param transaction the transaction in which this read is participating + * (will open a read lock on each document matched that is released when the + * transaction is committed or rolled back) + * @return a page of instances of the correct type populated with the persisted data. + * Since this call may match a large set, only one page of {@link #getPageLength()} + * is returned just like calls to {@link #search(PojoQueryDefinition, long) search} + */ public PojoPage read(ID[] ids, Transaction transaction) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException; - public PojoPage readAll(long start) + /** Read one page of persisted pojos of the type managed by this + * PojoRepository and unmarshall their data into new pojo instances. + * @param start the offset of the first document in the page (where 1 is the first result) + * @return a page with a maximum of {@link #getPageLength()} instances of the correct + * type populated with the persisted data. + * Since this call may match a large set, only one page of {@link #getPageLength()} + * is returned just like calls to {@link #search(PojoQueryDefinition, long) search}. + */ public PojoPage readAll(long start) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException; + /** Within an open transaction, read one page of persisted pojos of the type managed by this + * PojoRepository and unmarshall their data into new pojo instances. + * @param start the offset of the first document in the page (where 1 is the first result) + * @param transaction the transaction in which this read is participating + * (Will open a read lock on each document matched. The read lock is released when the + * transaction is committed or rolled back.) + * @return a page with a maximum of {@link #getPageLength()} instances of the correct + * type populated with the persisted data. + * Since this call may match a large set, only one page of {@link #getPageLength()} + * is returned just like calls to {@link #search(PojoQueryDefinition, long) search}. + */ public PojoPage readAll(long start, Transaction transaction) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException; - + + /** Find all persisted pojos of the type managed by this + * PojoRepository also in one of the specified collections and unmarshall their data + * into new pojo instances. + * @param start the offset of the first document in the page (where 1 is the first result) + * @param collections matches must belong to at least one of the specified collections + * @return a page with a maximum of {@link #getPageLength()} instances of the correct + * type populated with the persisted data. + * Since this call may match a large set, only one page of {@link #getPageLength()} + * is returned and the total estimated number of results is available from + * {@link PojoPage#getTotalSize() PojoPage.getTotalSize()}. + */ public PojoPage search(long start, String... collections) throws ForbiddenUserException, FailedRequestException; + /** Within an open transaction, find all persisted pojos of the type managed by this + * PojoRepository also in one of the specified collections and unmarshall their data + * into new pojo instances. + * @param start the offset of the first document in the page (where 1 is the first result) + * @param transaction the transaction in which this search is participating + * (Will open a read lock on each document matched. The read lock is released when the + * transaction is committed or rolled back.) + * @param collections matches must belong to at least one of the specified collections + * @return a page with a maximum of {@link #getPageLength()} instances of the correct + * type populated with the persisted data. + * Since this call may match a large set, only one page of {@link #getPageLength()} + * is returned and the total estimated number of results is available from + * {@link PojoPage#getTotalSize() PojoPage.getTotalSize()}. + */ public PojoPage search(long start, Transaction transaction, String... collections) throws ForbiddenUserException, FailedRequestException; + /** Within an open transaction, search persisted pojos of the type managed by this + * PojoRepository for matches to this query and unmarshall their data into new pojo instances. + * If matches are returned which do not meet all the criteria, you may need to create + * appropriate indexes in the server to run your query unfiltered or run your query filtered by + * wrapping your query with {@link PojoQueryBuilder#filteredQuery filteredQuery}. + * @param query the query which results much match (queries are run unfiltered by default) + * @param start the offset of the first document in the page (where 1 is the first result) + * @return a page with a maximum of {@link #getPageLength()} instances of the correct + * type populated with the persisted data. + * Since this call may match a large set, only one page of {@link #getPageLength()} + * is returned and the total estimated number of results is available from + * {@link PojoPage#getTotalSize() PojoPage.getTotalSize()}. + */ public PojoPage search(PojoQueryDefinition query, long start) throws ForbiddenUserException, FailedRequestException; + /** Within an open transaction, search persisted pojos of the type managed by this + * PojoRepository for matches to this query and unmarshall their data into new pojo instances. + * If matches are returned which do not meet all the criteria, you may need to create + * appropriate indexes in the server to run your query unfiltered or run your query filtered by + * wrapping your query with {@link PojoQueryBuilder#filteredQuery filteredQuery}. + * @param query the query which results much match (queries are run unfiltered by default) + * @param start the offset of the first document in the page (where 1 is the first result) + * @param transaction the transaction in which this search is participating + * (Will open a read lock on each document matched. The read lock is released when the + * transaction is committed or rolled back.) + * @return a page with a maximum of {@link #getPageLength()} instances of the correct + * type populated with the persisted data. + * Since this call may match a large set, only one page of {@link #getPageLength()} + * is returned and the total estimated number of results is available from + * {@link PojoPage#getTotalSize() PojoPage.getTotalSize()}. + */ public PojoPage search(PojoQueryDefinition query, long start, Transaction transaction) throws ForbiddenUserException, FailedRequestException; public PojoPage search(PojoQueryDefinition query, long start, SearchReadHandle searchHandle) throws ForbiddenUserException, FailedRequestException; + /** Within an open transaction, search persisted pojos of the type managed by this + * PojoRepository for matches to this query and unmarshall their data into new pojo instances. + * If matches are returned which do not meet all the criteria, you may need to create + * appropriate indexes in the server to run your query unfiltered or run your query filtered by + * wrapping your query with {@link PojoQueryBuilder#filteredQuery filteredQuery}. + * @param query the query which results much match (queries are run unfiltered by default) + * @param start the offset of the first document in the page (where 1 is the first result) + * @param searchHandle the handle to populate with a search results payload equivalent to + * one returned by + * {@link QueryManager#search(QueryDefinition, SearchReadHandle, long, Transaction) + * QueryManager.search} + * @param transaction the transaction in which this search is participating + * (Will open a read lock on each document matched. The read lock is released when the + * transaction is committed or rolled back.) + * @return a page with a maximum of {@link #getPageLength()} instances of the correct + * type populated with the persisted data. + * Since this call may match a large set, only one page of {@link #getPageLength()} + * is returned and the total estimated number of results is available from + * {@link PojoPage#getTotalSize() PojoPage.getTotalSize()}. + */ public PojoPage search(PojoQueryDefinition query, long start, SearchReadHandle searchHandle, Transaction transaction) throws ForbiddenUserException, FailedRequestException; + /** Get a PojoQueryBuilder for the type managed by this PojoRepository. + * @return a PojoQueryBuilder for the type managed by this PojoRepository + */ public PojoQueryBuilder getQueryBuilder(); - public long getPageLength(); // default: 50 + /** The number of instances per page returned when calling {@link #readAll readAll} or + * {@link #search search} (Default: 50). + */ + public long getPageLength(); + /** Set the number of instances per page returned when calling {@link #readAll readAll} or + * {@link #search search}. + */ public void setPageLength(long length); } From 12ecdfd4f4c8206965aeb39c8416ea2dd5ec2843 Mon Sep 17 00:00:00 2001 From: sammefford Date: Fri, 21 Nov 2014 15:59:10 -0700 Subject: [PATCH 355/357] revert 7644654ca9011c67079517187abaabe18e225f3d because the server has decided to stop adding the trailing newline --- src/test/java/com/marklogic/client/test/AlertingTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/marklogic/client/test/AlertingTest.java b/src/test/java/com/marklogic/client/test/AlertingTest.java index a95781a02..5d1de3df7 100644 --- a/src/test/java/com/marklogic/client/test/AlertingTest.java +++ b/src/test/java/com/marklogic/client/test/AlertingTest.java @@ -309,7 +309,7 @@ public void testJSONRuleDefinitions() BytesHandle bHandle = ruleManager.readRule("javatestrule", new BytesHandle().withFormat(Format.JSON)); assertEquals( - "{\"rule\":{\"name\":\"javatestrule\", \"description\":\"rule to demonstrate REST alerting\", \"search\":{\"qtext\":[\"favorited:true\"], \"options\":{\"constraint\":[{\"name\":\"favorited\", \"value\":{\"element\":{\"ns\":\"\", \"name\":\"favorited\"}}}]}}, \"rule-metadata\":null}}\n", + "{\"rule\":{\"name\":\"javatestrule\", \"description\":\"rule to demonstrate REST alerting\", \"search\":{\"qtext\":[\"favorited:true\"], \"options\":{\"constraint\":[{\"name\":\"favorited\", \"value\":{\"element\":{\"ns\":\"\", \"name\":\"favorited\"}}}]}}, \"rule-metadata\":null}}", new String(bHandle.get())); ruleManager.delete("javatestrule"); From 65591dc8adaabde0f440bef136602c8d8fb9e8ed Mon Sep 17 00:00:00 2001 From: sammefford Date: Wed, 19 Nov 2014 12:03:20 -0700 Subject: [PATCH 356/357] fix #202--test was failing with maven (though not with eclipse). The problem is the test needed to be explicit about charset. --- .../java/com/marklogic/client/test/HandleAccessorTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/marklogic/client/test/HandleAccessorTest.java b/src/test/java/com/marklogic/client/test/HandleAccessorTest.java index 497b3cdfb..5971e3ed3 100644 --- a/src/test/java/com/marklogic/client/test/HandleAccessorTest.java +++ b/src/test/java/com/marklogic/client/test/HandleAccessorTest.java @@ -22,6 +22,7 @@ import java.io.StringReader; import java.net.URISyntaxException; import java.net.URL; +import java.nio.charset.Charset; import org.junit.Test; @@ -37,10 +38,11 @@ public void testContentAsString() throws URISyntaxException, IOException { // I'm purposely using a string with a non-ascii character to test // charset issues String hola = "¡Hola!"; + System.out.println("Default Java Charset: " + Charset.defaultCharset()); assertEquals("String content mismatch", hola, HandleAccessor.contentAsString(new StringHandle(hola))); assertEquals("byte[] content mismatch", hola, - HandleAccessor.contentAsString(new BytesHandle(hola.getBytes()))); + HandleAccessor.contentAsString(new BytesHandle(hola.getBytes("UTF-8")))); URL filePath = this.getClass().getClassLoader().getResource("hola.txt"); assertEquals("Reader content mismatch", hola, HandleAccessor.contentAsString(new ReaderHandle(new StringReader(hola)))); From d6e2c7e633a793860609b201b4a2d68c15924181 Mon Sep 17 00:00:00 2001 From: Sravan Kottam Date: Tue, 25 Nov 2014 14:44:31 -0800 Subject: [PATCH 357/357] updated git plugin version to 0.10 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index b8c083da7..b38f1c1ac 100644 --- a/pom.xml +++ b/pom.xml @@ -138,7 +138,7 @@ com.github.github site-maven-plugin - 0.9 + 0.10 Maven artifacts for ${project.version} true