|
| 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 | +} |
0 commit comments