@Test public void TestE2EuserWithInvalidRole() throws Exception { // Used this test to verify ResourceNotFoundException when sjs module is installed with incorrect doc URI System.out.println("Running TestE2EuserWithInvalidRole"); SecurityContext secContext = new DatabaseClientFactory.DigestAuthContext("secondApiUser", "ap1U53r"); DatabaseClient dbSecondClient = DatabaseClientFactory.newClient(host, port, secContext, getConnType()); String msg; try { Float responseBack1 = TestE2EIntegerParaReturnDouble.on(dbSecondClient).TestE2EItemPriceErrorCond(10, 50); } catch (Exception ex) { msg = ex.toString(); System.out.println("Exception response from the Client API call is " + ex.toString()); assertTrue("Expected exception type not returned", msg.contains("com.marklogic.client.ResourceNotFoundException")); assertTrue("Expected exception returned", msg.contains( "Could not POST at /ext/TestE2EIntegerParaReturnDouble/TestE2EIntegerParamReturnDoubleErrorCond.sjs")); } finally { } }
@Test public void TestE2EUnAuthorizedUser() throws Exception { System.out.println("Running TestE2EUnAuthorizedUser"); associateRESTServerWithDefaultUser(serverName, "security", "digest"); SecurityContext secContext = new DatabaseClientFactory.DigestAuthContext("ForbiddenUser", "ap1U53r"); DatabaseClient dbForbiddenclient = DatabaseClientFactory.newClient(host, port, secContext, getConnType()); String msg; try { Float responseBack1 = TestE2EIntegerParaReturnDouble.on(dbForbiddenclient).TestE2EItemPriceErrorCond(10, 50); } catch (Exception ex) { msg = ex.toString(); System.out.println("Exception response from the Client API call is " + ex.toString()); assertTrue("Expected exception type not returned", msg.contains("com.marklogic.client.FailedRequestException")); assertTrue("Expected exception returned", msg.contains( "failed to POST at /ext/TestE2EIntegerParamReturnDouble/TestE2EIntegerParamReturnDoubleErrorCond.sjs")); } finally { associateRESTServerWithDefaultUser(serverName, "nobody", "digest"); } }
static private SecurityContext makeSecurityContext(String user, String password, Authentication type, SSLContext context, SSLHostnameVerifier verifier) { if ( Authentication.BASIC == type ) { return new BasicAuthContext(user, password) .withSSLContext(context) .withSSLHostnameVerifier(verifier); } else if ( Authentication.DIGEST == type ) { return new DigestAuthContext(user, password) .withSSLContext(context) .withSSLHostnameVerifier(verifier); } else { throw new IllegalStateException("makeSecurityContext should only be called with BASIC or DIGEST Authentication"); } }
@Test public void testPartialUpdateXMLDscriptor() throws IOException { System.out.println("Running testPartialUpdateXMLDescriptor"); String[] filenames = { "constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml" }; SecurityContext secContext = new DatabaseClientFactory.DigestAuthContext("eval-user", "x"); DatabaseClient client = DatabaseClientFactory.newClient(appServerHostname, uberPort, dbName, secContext, getConnType()); // 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, "<modified>2013-03-21</modified>"); DocumentPatchHandle patchHandle = patchBldr.build(); docMgr.patch(desc, patchHandle); waitForPropertyPropagate(); String content = docMgr.read(docId, new StringHandle()).get(); System.out.println("After" + content); assertTrue("fragment is not inserted", content.contains("<modified>2013-03-21</modified></root>")); // release client client.release(); }
@Test public void testInvalidUserAuth() { // create the client DatabaseClient client = DatabaseClientFactory.newClient( Common.HOST, Common.PORT, new DigestAuthContext("MyFooUser", "x")); String expectedException = "com.marklogic.client.FailedRequestException: " + "Local message: write failed: Unauthorized. Server Message: Unauthorized"; String exception = ""; String docId = "/example/text.txt"; TextDocumentManager docMgr = client.newTextDocumentManager(); try { // make use of the client connection so we get an auth error StringHandle handle = new StringHandle(); handle.set("A simple text document"); docMgr.write(docId, handle); // the next line will only run if write doesn't throw an exception docMgr.delete(docId); } catch (FailedRequestException e) { exception = e.toString(); } finally { client.release(); } assertEquals(expectedException, exception); } }
@Test public void testPartialUpdateInsertExistingFragments() throws Exception { System.out.println("Running testPartialUpdateInsertExistingFragments"); SecurityContext secContext = new DatabaseClientFactory.DigestAuthContext("eval-user", "x"); DatabaseClient client = DatabaseClientFactory.newClient(appServerHostname, uberPort, dbName, secContext, getConnType()); // 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, "<foo>LastChild</foo>"); patchBldr.replaceInsertFragment("/root/id", "/root", Position.BEFORE, "<foo>Before</foo>"); patchBldr.replaceInsertFragment("/root/p", "/root", Position.AFTER, "<foo>After</foo>"); DocumentPatchHandle patchHandle = patchBldr.build(); xmlDocMgr.patch(docId, patchHandle); waitForPropertyPropagate(); String content = xmlDocMgr.read(docId, new StringHandle()).get(); System.out.println(content); assertTrue("replaceInsertFragment Failed at Position.LAST_CHILD", content.contains("<foo>LastChild</foo>")); assertTrue("replaceInsertFragment Failed at Position.BEFORE", content.contains("<foo>Before</foo>")); assertTrue("replaceInsertFragment Failed at Position.AFTER", content.contains("<foo>After</foo>")); // release client client.release(); }
@Test public void testPartialUpdateReplaceInsertFragmentExistingJSON() throws IOException, JSONException { System.out.println("Running testPartialUpdateReplaceInsertFragmentExistingJSON"); String[] filenames = { "json-original.json" }; SecurityContext secContext = new DatabaseClientFactory.DigestAuthContext("eval-user", "x"); DatabaseClient client = DatabaseClientFactory.newClient(appServerHostname, uberPort, dbName, secContext, getConnType()); // 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); waitForPropertyPropagate(); 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(); }
static public void cleanUp() { DatabaseClient client = DatabaseClientFactory.newClient(Common.HOST, Common.PORT, new DigestAuthContext(Common.SERVER_ADMIN_USER, Common.SERVER_ADMIN_PASS)); try { QueryManager queryMgr = client.newQueryManager(); queryMgr.setPageLength(1000); QueryDefinition query = queryMgr.newStringDefinition(); query.setCollections(temporalCollection); // DeleteQueryDefinition deleteQuery = client.newQueryManager().newDeleteDefinition(); // deleteQuery.setCollections(temporalCollection); // client.newQueryManager().delete(deleteQuery); SearchHandle handle = queryMgr.search(query, new SearchHandle()); MatchDocumentSummary[] docs = handle.getMatchResults(); for ( MatchDocumentSummary doc : docs ) { if ( ! (temporalCollection + ".lsqt").equals(doc.getUri()) ) { client.newXMLDocumentManager().delete(doc.getUri()); } } } finally { client.release(); } } }
@Ignore public void testErrorOnNonREST() throws ForbiddenUserException { DatabaseClient badClient = DatabaseClientFactory.newClient(Common.HOST, 8001, new DigestAuthContext(Common.USER, Common.PASS)); ServerConfigurationManager serverConfig = badClient .newServerConfigManager(); try { serverConfig.readConfiguration(); } catch (FailedRequestException e) { assertEquals( "Local message: config/properties read failed: Not Found. Server Message: Server (not a REST instance?) did not respond with an expected REST Error message.", e.getMessage()); assertEquals(404, e.getFailedRequest().getStatusCode()); assertEquals("UNKNOWN", e.getFailedRequest().getStatus()); } finally { badClient.release(); } }
@BeforeClass public static void setUpBeforeClass() throws Exception { createRESTServerWithDB(server, port); createRESTUser("portal", "seekrit", "admin", "rest-admin", "rest-writer", "rest-reader"); associateRESTServerWithDB(setupServer, "Security"); SecurityContext secContext = new DatabaseClientFactory.DigestAuthContext("admin", "admin"); secClient = DatabaseClientFactory.newClient(host, setupPort, secContext, getConnType()); createCACert(); createCertTemplate(); createHostTemplate(); createClientCert("portal"); convertToHTTPS(); generateP12("portal"); createClientCert("blah"); generateP12("blah"); addCA(); associateRESTServerWithDB(server, "Documents"); }
@Test(expected = FailedRequestException.class) public void testJSONParserException() throws IOException { System.out.println("Running testPartialUpdateJSON"); String[] filenames = { "json-original.json" }; SecurityContext secContext = new DatabaseClientFactory.DigestAuthContext("bad-eval-user", "x"); DatabaseClient client = DatabaseClientFactory.newClient(appServerHostname, uberPort, dbName, secContext, getConnType()); // write docs for (String filename : filenames) { writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "JSON"); } // release client client.release(); }
@Test public void testConfigurator() { ConfiguratorImpl configurator = new ConfiguratorImpl(); DatabaseClientFactory.addConfigurator(configurator); DatabaseClient client = DatabaseClientFactory.newClient( Common.HOST, Common.PORT, new DigestAuthContext(Common.USER, Common.PASS)); try { assertTrue("Factory did not apply custom configurator", configurator.isConfigured); OkHttpClient okClient = (OkHttpClient) client.getClientImplementation(); assertEquals(testConnectTimeoutMillis, okClient.connectTimeoutMillis()); } finally { client.release(); } }
public static DatabaseClient newReadOnlyClient() { return DatabaseClientFactory.newClient( Common.HOST, Common.PORT, new DigestAuthContext(Common.READ_ONLY_USER, Common.READ_ONLY_PASS), CONNECTION_TYPE); }
public static DatabaseClient newClient(String databaseName) { return DatabaseClientFactory.newClient(Common.HOST, Common.PORT, databaseName, new DatabaseClientFactory.DigestAuthContext(Common.USER, Common.PASS), CONNECTION_TYPE); } public static DatabaseClient newAdminClient() {
@Before public void setUp() throws KeyManagementException, NoSuchAlgorithmException, Exception { // create new connection for each test below 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"); if (isLBHost()) client = getDatabaseClient("eval-user", "x", getConnType()); else { SecurityContext secContext = new DatabaseClientFactory.DigestAuthContext("eval-user", "x"); client = DatabaseClientFactory.newClient(appServerHostname, uberPort, dbName, secContext, getConnType()); } }
public static DatabaseClient newServerAdminClient() { return DatabaseClientFactory.newClient( Common.HOST, Common.PORT, new DigestAuthContext(Common.SERVER_ADMIN_USER, Common.SERVER_ADMIN_PASS), CONNECTION_TYPE); } public static DatabaseClient newEvalClient() {
private DatabaseClientFactory.Bean makeClientBeanFactory() { DatabaseClientFactory.Bean clientFactoryBean = new DatabaseClientFactory.Bean(); DatabaseClientFactory.DigestAuthContext clientFactoryContext = new DatabaseClientFactory.DigestAuthContext(Common.USER, Common.PASS); clientFactoryBean.setHost(Common.HOST); clientFactoryBean.setPort(Common.PORT); clientFactoryBean.setSecurityContext(clientFactoryContext); return clientFactoryBean; }
public static DatabaseClient newEvalClient(String databaseName) { return DatabaseClientFactory.newClient( Common.HOST, Common.PORT, databaseName, new DigestAuthContext(Common.EVAL_USER, Common.EVAL_PASS), CONNECTION_TYPE); } public static DatabaseClient newReadOnlyClient() {
@Test public void testWriteTextDoc() { SecurityContext secContext = new DatabaseClientFactory.DigestAuthContext("admin", "admin"); DatabaseClient client = DatabaseClientFactory.newClient(appServerHostname, 8015, secContext, getConnType()); 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()); }
public static DatabaseClient newAdminClient() { return DatabaseClientFactory.newClient( Common.HOST, Common.PORT, new DigestAuthContext(Common.REST_ADMIN_USER, Common.REST_ADMIN_PASS), CONNECTION_TYPE); } public static DatabaseClient newServerAdminClient() {