/** * Checks if the constructed base-uri matches the base-uri of added documents. * * @throws Exception exception */ @Test public void baseUri() throws Exception { final String find = "base-uri(collection('" + NAME + '/' + DIR + "xmark.xml'))"; try (final QueryProcessor qp = new QueryProcessor(find, context)) { assertEquals(NAME + '/' + FILES[1], qp.execute().toString()); } }
/** * Test digest authentication. * * @throws Exception exception */ @Test public void digest() throws Exception { // correct credentials try (final QueryProcessor qp = new QueryProcessor( _HTTP_SEND_REQUEST.args( "<http:request xmlns:http='http://expath.org/ns/http-client' method='GET' " + "send-authorization='true' auth-method='Digest' username='******' password='******' " + "href='" + REST_ROOT + "'/>"), ctx)) { checkResponse(qp.value(), 2, HttpURLConnection.HTTP_OK); } // wrong credentials try (final QueryProcessor qp = new QueryProcessor( _HTTP_SEND_REQUEST.args( "<http:request xmlns:http='http://expath.org/ns/http-client' method='GET' " + "send-authorization='true' auth-method='Digest' username='******' password='******' " + "href='" + REST_ROOT + "?query=()'/>") + "[. instance of node()][@status = '401']", ctx)) { checkResponse(qp.value(), 1, HttpURLConnection.HTTP_UNAUTHORIZED); } }
/** * Finds documents in path. * * @throws Exception exception */ @Test public void findDocs() throws Exception { final String find = "collection('" + NAME + "/test/zipped') "; try (final QueryProcessor qp = new QueryProcessor(find, context)) { assertEquals(4, qp.execute().size()); } }
/** * Test query for stripping existing namespaces. * * @throws Exception exception */ @Test public void stripNS() throws Exception { final IO io = IO.get("<a xmlns:a='a'><b><c/><c/><c/></b></a>"); try (final QueryProcessor qp = new QueryProcessor("/*:a/*:b", context).context(new DBNode(io))) { final ANode sub = (ANode) qp.iter().next(); DataBuilder.stripNS(sub, token("a"), context); } }
/** Tests the specified instance. */ @Test public void test() { final StringBuilder sb = new StringBuilder(); int fail = 0; for (final Object[] qu : queries) { final boolean correct = qu.length == 3; final String query = qu[correct ? 2 : 1].toString(); final Value cmp = correct ? (Value) qu[1] : null; final QueryProcessor qp = new QueryProcessor(query, context); try { final Value val = qp.value(); if (!correct || !new DeepCompare().equal(val, cmp)) { sb.append("[" + qu[0] + "] " + query); String s = correct && cmp.size() != 1 ? "#" + cmp.size() : ""; sb.append("\n[E" + s + "] "); if (correct) { final String cp = cmp.toString(); sb.append('\''); sb.append(cp.length() > 1000 ? cp.substring(0, 1000) + "..." : cp); sb.append('\''); } else { sb.append("error"); } final TokenBuilder types = new TokenBuilder(); for (final Item it : val) types.add(it.type.toString()).add(" "); s = val.size() == 1 ? "" : "#" + val.size(); sb.append("\n[F" + s + "] '" + val + "', " + types + details() + '\n'); ++fail; } } catch (final Exception ex) { final String msg = ex.getMessage(); if (correct || msg == null || msg.contains("mailman")) { final String cp = correct && cmp.data() != null ? cmp.toString() : "()"; sb.append( "[" + qu[0] + "] " + query + "\n[E] " + cp + "\n[F] " + (msg == null ? Util.className(ex) : msg.replaceAll("\r\n?|\n", " ")) + ' ' + details() + '\n'); ex.printStackTrace(); ++fail; } } finally { qp.close(); } } if (fail != 0) fail(fail + " Errors. [E] = expected, [F] = found:\n" + sb.toString().trim()); }
/** * Tests an erroneous query. * * @throws Exception exception */ @Test public void error() throws Exception { try (final QueryProcessor qp = new QueryProcessor( _HTTP_SEND_REQUEST.args("<http:request method='get'/>", RESTURL + "unknown") + "[1]/@status/data()", ctx)) { assertEquals("404", qp.value().serialize().toString()); } }
/** * Finds single doc. * * @throws Exception exception */ @Test public void findDoc() throws Exception { final String find = "for $x in collection('" + NAME + '/' + DIR + "xmark.xml') " + "where $x//location contains text 'uzbekistan' " + "return $x"; try (final QueryProcessor qp = new QueryProcessor(find, context)) { assertEquals(1, qp.execute().size()); } }
/** * Test sending of HTTP PUT requests. * * @throws Exception exception */ @Test public void put() throws Exception { try (final QueryProcessor qp = new QueryProcessor( _HTTP_SEND_REQUEST.args( "<http:request method='put' status-only='true'>" + "<http:body media-type='text/xml'>" + BOOKS + "</http:body>" + "</http:request>", RESTURL), ctx)) { checkResponse(qp.value(), 1, HttpURLConnection.HTTP_CREATED); } }
/** Test query. Detects malformed namespace hierarchy. */ @Test @Ignore public void xuty0004() { final String query = "declare variable $input-context external;" + "let $source as node()* := (" + " <status>on leave</status>," + " <!-- for 6 months -->" + " )," + " $target := $input-context/works[1]/employee[1]" + "return insert nodes $source into $target"; try (final QueryProcessor qp = new QueryProcessor(query, context)) { qp.value(); } catch (final QueryException ex) { assertEquals("XUTY0004", ex.error().code); } fail("should throw XUTY0004"); }
/** * Test sending of HTTP GET requests. * * @throws Exception exception */ @Test public void postGet() throws Exception { // GET1 - just send a GET request try (final QueryProcessor qp = new QueryProcessor( _HTTP_SEND_REQUEST.args("<http:request method='get' href='" + REST_ROOT + "'/>"), ctx)) { final Value v = qp.value(); checkResponse(v, 2, HttpURLConnection.HTTP_OK); assertEquals(NodeType.DOC, v.itemAt(1).type); } // GET2 - with override-media-type='text/plain' try (final QueryProcessor qp = new QueryProcessor( _HTTP_SEND_REQUEST.args( "<http:request method='get' override-media-type='text/plain'/>", REST_ROOT), ctx)) { final Value v = qp.value(); checkResponse(v, 2, HttpURLConnection.HTTP_OK); assertEquals(AtomType.STR, v.itemAt(1).type); } // Get3 - with status-only='true' try (final QueryProcessor qp = new QueryProcessor( _HTTP_SEND_REQUEST.args("<http:request method='get' status-only='true'/>", REST_ROOT), ctx)) { checkResponse(qp.value(), 1, HttpURLConnection.HTTP_OK); } }
/** * Test sending of HTTP DELETE requests. * * @throws Exception exception */ @Test public void postDelete() throws Exception { // add document to be deleted try (final QueryProcessor qp = new QueryProcessor( _HTTP_SEND_REQUEST.args( "<http:request method='put'>" + "<http:body media-type='text/xml'><ToBeDeleted/></http:body>" + "</http:request>", RESTURL), ctx)) { qp.value(); } // DELETE try (final QueryProcessor qp = new QueryProcessor( _HTTP_SEND_REQUEST.args("<http:request method='delete' status-only='true'/>", RESTURL), ctx)) { checkResponse(qp.value(), 1, HttpURLConnection.HTTP_OK); } }
/** * Test sending of HTTP POST requests. * * @throws Exception exception */ @Test public void putPost() throws Exception { // PUT - query try (final QueryProcessor qp = new QueryProcessor( _HTTP_SEND_REQUEST.args( "<http:request method='put' status-only='true'>" + "<http:body media-type='text/xml'>" + BOOKS + "</http:body>" + "</http:request>", RESTURL), ctx)) { checkResponse(qp.value(), 1, HttpURLConnection.HTTP_CREATED); } // POST - query try (final QueryProcessor qp = new QueryProcessor( _HTTP_SEND_REQUEST.args( "<http:request method='post'>" + "<http:body media-type='application/xml'>" + "<query xmlns='" + Prop.URL + "/rest'>" + "<text><![CDATA[<x>1</x>]]></text>" + "</query>" + "</http:body>" + "</http:request>", RESTURL), ctx)) { checkResponse(qp.value(), 2, HttpURLConnection.HTTP_OK); } // Execute the same query but with content set from $bodies try (final QueryProcessor qp = new QueryProcessor( _HTTP_SEND_REQUEST.args( "<http:request method='post'>" + "<http:body media-type='application/xml'/>" + "</http:request>", RESTURL, "<query xmlns='" + Prop.URL + "/rest'>" + "<text><![CDATA[<x>1</x>]]></text>" + "</query>"), ctx)) { checkResponse(qp.value(), 2, HttpURLConnection.HTTP_OK); } }