Skip to content

Commit 4f3e3c7

Browse files
committed
Merge remote-tracking branch 'origin/newRegressionTests' into dev
2 parents e07dbc4 + 670cad9 commit 4f3e3c7

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

test-complete/src/test/java/com/marklogic/javaclient/ConnectedRESTQA.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,29 @@ public static void associateRESTServerWithDB(String restServerName,String dbName
245245
System.out.println(content);
246246
}
247247
}
248+
/*
249+
* Associate REST server with default user
250+
* this is created for the sake of runtime DB selection
251+
*/
252+
public static void associateRESTServerWithDefaultUser(String restServerName,String userName,String authType)throws Exception{
253+
DefaultHttpClient client = new DefaultHttpClient();
254+
255+
client.getCredentialsProvider().setCredentials(
256+
new AuthScope("localhost", 8002),
257+
new UsernamePasswordCredentials("admin", "admin"));
258+
String body = "{ \"default-user\":\""+userName+"\",\"authentication\": \""+authType+"\",\"group-name\": \"Default\"}";
259+
260+
HttpPut put = new HttpPut("http://localhost:8002/manage/v2/servers/"+restServerName+"/properties?server-type=http");
261+
put.addHeader("Content-type", "application/json");
262+
put.setEntity(new StringEntity(body));
263+
264+
HttpResponse response2 = client.execute(put);
265+
HttpEntity respEntity = response2.getEntity();
266+
if(respEntity != null){
267+
String content = EntityUtils.toString(respEntity);
268+
System.out.println(content);
269+
}
270+
}
248271
/*
249272
* Creating RESTServer With default content and module database
250273
*/
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.marklogic.javaclient;
2+
3+
import static org.junit.Assert.*;
4+
5+
import javax.annotation.Resource.AuthenticationType;
6+
7+
import org.junit.After;
8+
import org.junit.AfterClass;
9+
import org.junit.Before;
10+
import org.junit.BeforeClass;
11+
import org.junit.Test;
12+
13+
import com.marklogic.client.DatabaseClient;
14+
import com.marklogic.client.DatabaseClientFactory;
15+
import com.marklogic.client.DatabaseClientFactory.Authentication;
16+
import com.marklogic.client.FailedRequestException;
17+
18+
public class TestRuntimeDBselection extends BasicJavaClientREST {
19+
private static String dbName = "TestRuntimeDB";
20+
private static String [] fNames = {"TestRuntimeDB-1"};
21+
private static String restServerName = "REST-Java-Client-API-Server";
22+
private static int restPort = 8011;
23+
private DatabaseClient client ;
24+
@BeforeClass
25+
public static void setUpBeforeClass() throws Exception {
26+
System.out.println("In setup");
27+
setupJavaRESTServer(dbName, fNames[0], restServerName,restPort,false);
28+
createUserRolesWithPrevilages("test-eval","xdbc:eval", "xdbc:eval-in","xdmp:eval-in","any-uri","xdbc:invoke");
29+
createRESTUser("eval-user", "x", "test-eval","rest-admin","rest-writer","rest-reader");
30+
31+
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug");
32+
}
33+
34+
@AfterClass
35+
public static void tearDownAfterClass() throws Exception {
36+
System.out.println("In tear down" );
37+
tearDownJavaRESTServer(dbName, fNames, restServerName);
38+
deleteRESTUser("eval-user");
39+
deleteUserRole("test-eval");
40+
}
41+
42+
@Test
43+
public void testRuntimeDBclientWithDefaultUser() throws Exception {
44+
associateRESTServerWithDefaultUser(restServerName,"eval-user","application-level");
45+
client = DatabaseClientFactory.newClient("localhost", restPort,dbName);
46+
String insertJSON = "xdmp:document-insert(\"test2.json\",object-node {\"test\":\"hello\"})";
47+
client.newServerEval().xquery(insertJSON).eval();
48+
String query1 = "fn:count(fn:doc())";
49+
int response = client.newServerEval().xquery(query1).eval().next().getNumber().intValue();
50+
assertEquals("count of documents ",1,response);
51+
String query2 ="declareUpdate();xdmp.documentDelete(\"test2.json\");";
52+
client.newServerEval().javascript(query2).eval();
53+
int response2 = client.newServerEval().xquery(query1).eval().next().getNumber().intValue();
54+
assertEquals("count of documents ",0,response2);
55+
client.release();
56+
associateRESTServerWithDefaultUser(restServerName,"nobody","digest");
57+
}
58+
//Issue 184 exists
59+
@Test(expected=FailedRequestException.class)
60+
public void testRuntimeDBclientWithDifferentAuthType() throws Exception {
61+
62+
client = DatabaseClientFactory.newClient("localhost", restPort,dbName,"eval-user","x",Authentication.BASIC);
63+
String insertJSON = "xdmp:document-insert(\"test2.json\",object-node {\"test\":\"hello\"})";
64+
client.newServerEval().xquery(insertJSON).eval();
65+
String query1 = "fn:count(fn:doc())";
66+
int response = client.newServerEval().xquery(query1).eval().next().getNumber().intValue();
67+
assertEquals("count of documents ",1,response);
68+
String query2 ="declareUpdate();xdmp.documentDelete(\"test2.json\");";
69+
client.newServerEval().javascript(query2).eval();
70+
int response2 = client.newServerEval().xquery(query1).eval().next().getNumber().intValue();
71+
assertEquals("count of documents ",0,response2);
72+
client.release();
73+
74+
}
75+
76+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
declare namespace test='http://marklogic.com/test';
2+
declare variable $test:myString as xs:string external;
3+
declare variable $myXmlNode as xs:string external;
4+
(:declare variable $myJsonNode as xs:string external;:)
5+
declare variable $myBool as xs:boolean external;
6+
declare variable $myInteger as xs:integer external;
7+
declare variable $myDecimal as xs:decimal external;
8+
declare variable $myDouble as xs:double external;
9+
declare variable $myFloat as xs:float external;
10+
declare variable $myNull external;
11+
12+
(
13+
$test:myString,
14+
$myBool,
15+
document{ xdmp:unquote($myXmlNode) },
16+
xdmp:unquote($myXmlNode)//comment(),
17+
xdmp:unquote($myXmlNode)//text(),
18+
xdmp:unquote($myXmlNode)//*,
19+
xdmp:unquote($myXmlNode)/@attr,
20+
xdmp:unquote($myXmlNode)//processing-instruction(),
21+
(:$myNull,
22+
xdmp:unquote($myJsonNode)/a,
23+
xdmp:unquote($myJsonNode)/b,
24+
xdmp:unquote($myJsonNode)/c1,
25+
xdmp:unquote($myJsonNode)/d,
26+
xdmp:unquote($myJsonNode)/f,
27+
xdmp:unquote($myJsonNode)/g, :)
28+
$myInteger,
29+
$myDecimal,
30+
$myDouble,
31+
$myFloat
32+
)

0 commit comments

Comments
 (0)