Skip to content

Commit c3f6003

Browse files
committed
Merge branch 'newRegressionTests' of https://github.com/marklogic/java-client-api into newRegressionTests
2 parents 77549e1 + 80ec1b6 commit c3f6003

File tree

2 files changed

+128
-9
lines changed

2 files changed

+128
-9
lines changed

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

Lines changed: 121 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.marklogic.client.DatabaseClient;
2929
import com.marklogic.client.admin.ServerConfigurationManager;
3030
import com.marklogic.client.io.DocumentMetadataHandle;
31+
import com.marklogic.client.io.DocumentMetadataHandle.Capability;
3132

3233
import java.net.InetAddress;
3334

@@ -207,12 +208,18 @@ public static void assocRESTServer(String restServerName,String dbName,int restP
207208
post.setEntity(new StringEntity(JSONString));
208209

209210
HttpResponse response = client.execute(post);
210-
System.out.println(JSONString);
211+
// System.out.println(JSONString);
211212
if (response.getStatusLine().getStatusCode() == 400) {
212213
// EntityUtils to get the response content
213-
214-
System.out.println("AppServer already exist, so changing the content database to new DB");
215-
associateRESTServerWithDB(restServerName,dbName);
214+
System.out.println("AppServer already exist");
215+
if(dbName.equals("Documents")){
216+
System.out.println("and Context database is Documents DB");
217+
}
218+
else{
219+
System.out.println("and changing context database to "+dbName);
220+
associateRESTServerWithDB(restServerName,dbName);
221+
}
222+
216223
}
217224
}catch (Exception e) {
218225
// writing error to Log
@@ -288,8 +295,96 @@ public static void setupJavaRESTServer(String dbName, String fName, String restS
288295
createRESTUser("rest-writer","x","rest-writer");
289296
createRESTUser("rest-reader","x","rest-reader");
290297
}
298+
public static void setupJavaRESTServer(String dbName, String fName, String restServerName, int restPort,boolean attachRestContextDB)throws Exception{
299+
300+
createDB(dbName);
301+
createForest(fName,dbName);
302+
Thread.sleep(1500);
303+
if(attachRestContextDB){
304+
assocRESTServer(restServerName, dbName,restPort);
305+
}
306+
else{
307+
assocRESTServer(restServerName, "Documents",restPort);
308+
}
309+
createRESTUser("rest-admin","x","rest-admin");
310+
createRESTUser("rest-writer","x","rest-writer");
311+
createRESTUser("rest-reader","x","rest-reader");
312+
}
313+
314+
/*Create a role with given privilages
315+
*
316+
*/
317+
public static void createUserRolesWithPrevilages(String roleName, String... privNames ){
318+
try{
319+
DefaultHttpClient client = new DefaultHttpClient();
320+
client.getCredentialsProvider().setCredentials(
321+
new AuthScope("localhost", 8002),
322+
new UsernamePasswordCredentials("admin", "admin"));
323+
HttpGet getrequest = new HttpGet("http://localhost:8002/manage/v2/roles/"+roleName);
324+
HttpResponse resp = client.execute(getrequest);
325+
326+
if( resp.getStatusLine().getStatusCode() == 200)
327+
{
328+
System.out.println("Role already exist");
329+
}
330+
else {
331+
System.out.println("Role dont exist, will create now");
332+
String[] roleNames ={"rest-reader","rest-writer"};
333+
client = new DefaultHttpClient();
334+
client.getCredentialsProvider().setCredentials(
335+
new AuthScope("localhost", 8002),
336+
new UsernamePasswordCredentials("admin", "admin"));
337+
338+
ObjectMapper mapper = new ObjectMapper();
339+
ObjectNode mainNode = mapper.createObjectNode();
340+
341+
ArrayNode roleArray = mapper.createArrayNode();
342+
ArrayNode privArray = mapper.createArrayNode();
343+
ArrayNode permArray = mapper.createArrayNode();
344+
mainNode.put("name",roleName);
345+
mainNode.put("description", "role discription");
346+
347+
for(String rolename: roleNames)
348+
roleArray.add(rolename);
349+
mainNode.withArray("role").addAll(roleArray);
350+
for(String privName: privNames){
351+
ObjectNode privNode = mapper.createObjectNode();
352+
privNode.put("privilege-name", privName);
353+
privNode.put("action", "http://marklogic.com/xdmp/privileges/"+privName.replace(":", "-"));
354+
privNode.put("kind", "execute");
355+
privArray.add(privNode);
356+
}
357+
mainNode.withArray("privilege").addAll(privArray);
358+
permArray.add(getPermissionNode(roleNames[0],Capability.READ).get("permission").get(0));
359+
permArray.add(getPermissionNode(roleNames[1],Capability.READ).get("permission").get(0));
360+
permArray.add(getPermissionNode(roleNames[1],Capability.EXECUTE).get("permission").get(0));
361+
permArray.add(getPermissionNode(roleNames[1],Capability.UPDATE).get("permission").get(0));
362+
mainNode.withArray("permission").addAll(permArray);
363+
System.out.println(mainNode.toString());
364+
HttpPost post = new HttpPost("http://localhost:8002"+ "/manage/v2/roles?format=json");
365+
post.addHeader("Content-type", "application/json");
366+
post.setEntity(new StringEntity(mainNode.toString()));
367+
368+
HttpResponse response = client.execute(post);
369+
HttpEntity respEntity = response.getEntity();
370+
if( response.getStatusLine().getStatusCode() == 400)
371+
{
372+
System.out.println("creation of role got a problem");
373+
}
374+
else if (respEntity != null) {
375+
// EntityUtils to get the response content
376+
String content = EntityUtils.toString(respEntity);
377+
System.out.println(content);
378+
}
379+
else {System.out.println("No Proper Response");}
380+
}
381+
}catch (Exception e) {
382+
// writing error to Log
383+
e.printStackTrace();
384+
}
385+
}
291386
/*
292-
* This function creates a REST server with default content DB, Module DB
387+
* This function creates a REST user with given roles
293388
*/
294389

295390
public static void createRESTUser(String usrName, String pass, String... roleNames ){
@@ -466,7 +561,26 @@ public static void deleteRESTUser(String usrName){
466561
}
467562

468563
}
564+
public static void deleteUserRole(String roleName){
565+
try{
566+
DefaultHttpClient client = new DefaultHttpClient();
567+
568+
client.getCredentialsProvider().setCredentials(
569+
new AuthScope("localhost", 8002),
570+
new UsernamePasswordCredentials("admin", "admin"));
571+
572+
HttpDelete delete = new HttpDelete("http://localhost:8002/manage/v2/roles/"+roleName);
469573

574+
HttpResponse response = client.execute(delete);
575+
if(response.getStatusLine().getStatusCode()== 202){
576+
Thread.sleep(3500);
577+
}
578+
}catch (Exception e) {
579+
// writing error to Log
580+
e.printStackTrace();
581+
}
582+
583+
}
470584
public static void setupJavaRESTServerWithDB( String restServerName, int restPort)throws Exception{
471585
createRESTServerWithDB(restServerName, restPort);
472586
createRESTUser("rest-admin","x","rest-admin");
@@ -635,7 +749,7 @@ else if(response.getStatusLine().getStatusCode() == 200){
635749
}
636750
}
637751
/*
638-
* This function deletes rest server first and deletes forests and databases in separate calls
752+
* This function move rest server first to documents and deletes forests and databases in separate calls
639753
*/
640754
public static void tearDownJavaRESTServer(String dbName, String [] fNames, String restServerName) throws Exception{
641755

@@ -664,6 +778,7 @@ public static void tearDownJavaRESTServer(String dbName, String [] fNames, Strin
664778

665779
deleteDB(dbName);
666780
}
781+
667782

668783
/*
669784
* This function deletes rest server along with default forest and database

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,22 @@ public class TestEvalXquery extends BasicJavaClientREST {
5454
public static void setUpBeforeClass() throws Exception {
5555
System.out.println("In setup");
5656
setupJavaRESTServer(dbName, fNames[0], restServerName,restPort);
57-
// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug");
57+
TestEvalXquery.createUserRolesWithPrevilages("test-eval", "xdbc:eval","any-uri");
58+
TestEvalXquery.createRESTUser("eval-user", "x", "test-eval");
59+
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug");
5860
}
5961

6062
@AfterClass
6163
public static void tearDownAfterClass() throws Exception {
6264
System.out.println("In tear down" );
6365
tearDownJavaRESTServer(dbName, fNames, restServerName);
66+
TestEvalXquery.deleteRESTUser("eval-user");
67+
TestEvalXquery.deleteUserRole("test-eval");
6468
}
6569

6670
@Before
6771
public void setUp() throws Exception {
68-
client = DatabaseClientFactory.newClient("localhost", restPort,"admin", "admin", Authentication.DIGEST);
72+
client = DatabaseClientFactory.newClient("localhost", restPort,"eval-user", "x", Authentication.DIGEST);
6973
}
7074

7175
@After
@@ -391,7 +395,7 @@ public void testMultipleXqueryfnOnServerEval() {
391395
String response3 = client.newServerEval().xquery(query3).evalAs(String.class);
392396
System.out.println(response3);
393397
}
394-
//Issue 156 exist for this
398+
//Issue 156 exist for this, have test cases where you can pass, element node, text node, binary node as an external variable
395399
@Test(expected = com.marklogic.client.FailedRequestException.class)
396400
public void testXqueryWithExtVarAsNode() throws Exception {
397401
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();

0 commit comments

Comments
 (0)