[ 
https://issues.apache.org/jira/browse/SLING-7509?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16381980#comment-16381980
 ] 

ASF GitHub Bot commented on SLING-7509:
---------------------------------------

volteanu commented on a change in pull request #5: SLING-7509 - Add QueryClient
URL: 
https://github.com/apache/sling-org-apache-sling-testing-clients/pull/5#discussion_r171555313
 
 

 ##########
 File path: 
src/test/java/org/apache/sling/testing/clients/query/QueryClientTest.java
 ##########
 @@ -1,43 +1,166 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations 
under
+ * the License.
+ */
 package org.apache.sling.testing.clients.query;
 
+import org.apache.http.HttpException;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.NameValuePair;
+import org.apache.http.client.utils.URLEncodedUtils;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.message.BasicHttpEntityEnclosingRequest;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.HttpRequestHandler;
 import org.apache.sling.testing.clients.ClientException;
-import org.junit.Ignore;
+import org.apache.sling.testing.clients.HttpServerRule;
+import org.codehaus.jackson.JsonNode;
+import org.junit.Assert;
+import org.junit.ClassRule;
 import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-import java.net.URI;
-
-import static org.junit.Assert.*;
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.util.List;
 
-/**
- * Ignored since these cannot be executed without a running instance
- */
-@Ignore
 public class QueryClientTest {
-    QueryClient client;
+    private static final Logger LOG = 
LoggerFactory.getLogger(QueryClientTest.class);
+
+    private static final String QUERY_PATH = "/system/testing/query"; // same 
as in QueryServlet
+    private static final String BUNDLE_PATH = 
"/system/console/bundles/org.apache.sling.testing.clients.query";
+    private static final String QUERY_RESPONSE = "{\"total\": 1234,\"time\": 
1}";
+    private static final String EXPLAIN_RESPONSE = "{\"plan\": \"some 
plan\",\"time\": 1}";
+    private static final String JSON_BUNDLE = "{\n" +
+            "  \"status\": \"Bundle information: 546 bundles in total, 537 
bundles active, 8 bundles active fragments, 1 bundle resolved.\",\n" +
+            "  \"s\": [\n" +
+            "    546,\n" +
+            "    537,\n" +
+            "    8,\n" +
+            "    1,\n" +
+            "    0\n" +
+            "  ],\n" +
+            "  \"data\": [\n" +
+            "    {\n" +
+            "      \"id\": 560,\n" +
+            "      \"name\": \"Query servlet for testing\",\n" +
+            "      \"fragment\": false,\n" +
+            "      \"stateRaw\": 32,\n" +
+            "      \"state\": \"Active\",\n" +
+            "      \"version\": \"1.0.0\",\n" +
+            "      \"symbolicName\": 
\"org.apache.sling.testing.clients.query\",\n" +
+            "      \"category\": \"\"\n" +
+            "    }\n" +
+            "  ]\n" +
+            "}";
+
+    @ClassRule
+    public static HttpServerRule httpServer = new HttpServerRule() {
+        @Override
+        protected void registerHandlers() throws IOException {
+
+            // Normal query request
+            serverBootstrap.registerHandler(QUERY_PATH, new 
HttpRequestHandler() {
+                @Override
+                public void handle(HttpRequest request, HttpResponse response, 
HttpContext context) throws HttpException, IOException {
+                        List<NameValuePair> parameters = URLEncodedUtils.parse(
+                                request.getRequestLine().getUri(), 
Charset.defaultCharset());
+
+                        for (NameValuePair parameter : parameters) {
+                            if (parameter.getName().equals("explain") && 
!parameter.getValue().equals("false")) {
+                                response.setEntity(new 
StringEntity(EXPLAIN_RESPONSE));
+                                return;
+                            }
+                        }
+
+                    response.setEntity(new StringEntity(QUERY_RESPONSE));
+                }
+            });
+
+            // Install servlet
+            serverBootstrap.registerHandler("/system/console/bundles", new 
HttpRequestHandler() {
+                @Override
+                public void handle(HttpRequest request, HttpResponse response, 
HttpContext context) throws HttpException, IOException {
+                    // is install (post) or checking status (get)
+                    if (request instanceof BasicHttpEntityEnclosingRequest) {
+                        response.setStatusCode(302);
+                    } else {
+                        response.setStatusCode(200);
+                    }
+                }
+            });
+
+            // Check bundle status
+            serverBootstrap.registerHandler(BUNDLE_PATH + ".json", new 
HttpRequestHandler() {
+                @Override
+                public void handle(HttpRequest request, HttpResponse response, 
HttpContext context) throws HttpException, IOException {
+                    response.setEntity(new StringEntity(JSON_BUNDLE));
+                }
+            });
+
+            // Uninstall bundle
+            serverBootstrap.registerHandler(BUNDLE_PATH, new 
HttpRequestHandler() {
+                @Override
+                public void handle(HttpRequest request, HttpResponse response, 
HttpContext context) throws HttpException, IOException {
+                        response.setStatusCode(200);
+                }
+            });
+        }
+    };
+
+    private static QueryClient client;
 
     public QueryClientTest() throws ClientException {
-        client = new QueryClient(URI.create("http://localhost:8080";), "admin", 
"admin");
+        client = new QueryClient(httpServer.getURI(), "admin", "admin");
+        // for testing an already running instance
+        // client = new 
QueryClient(java.net.URI.create("http://localhost:8080";), "admin", "admin");
     }
 
     @Test
-    public void testInstallServlet() throws ClientException {
+    public void testInstallServlet() throws ClientException, 
InterruptedException {
         client.installServlet();
     }
 
     @Test
-    public void testDoQuery() throws ClientException {
-        System.out.println(client.doQuery("SELECT * FROM [nt:file]",
-                QueryClient.QueryType.SQL2));
+    public void testDoQuery() throws ClientException, InterruptedException {
+        JsonNode response = client.doQuery("SELECT * FROM [nt:file] WHERE 
ISDESCENDANTNODE([/etc/])",
+//        JsonNode response = client.doQuery("SELECT * FROM [cq:Tag] WHERE 
ISDESCENDANTNODE([/etc/])",
+                QueryClient.QueryType.SQL2);
+        LOG.info(response.toString());
+        Assert.assertNotEquals(0, response.get("total").getLongValue());
+    }
+
+    @Test
+    public void testDoCount() throws ClientException, InterruptedException {
+        long results = client.doCount("SELECT * FROM [nt:file] WHERE 
ISDESCENDANTNODE([/etc/])",
+                QueryClient.QueryType.SQL2);
+        LOG.info("results={}", results);
+        Assert.assertNotEquals(0, results);
     }
 
     @Test
-    public void testDoCount() throws ClientException {
-        System.out.println(client.doCount("SELECT * FROM [nt:file]",
-                QueryClient.QueryType.SQL2));
+    public void testGetPlan() throws ClientException, InterruptedException {
+        String plan = client.getPlan("SELECT * FROM [nt:file] WHERE 
ISDESCENDANTNODE([/etc/])",
+                QueryClient.QueryType.SQL2);
+        LOG.info("plan={}", plan);
 
 Review comment:
   Put smth there, but I don't want to have a rigid assertion to keep it 
working on a real installation, where the plan can vary. The fact that 
`getPlan` doesn't throw an Exception is already a good validator.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> Add QueryClient
> ---------------
>
>                 Key: SLING-7509
>                 URL: https://issues.apache.org/jira/browse/SLING-7509
>             Project: Sling
>          Issue Type: New Feature
>          Components: Apache Sling Testing Clients
>            Reporter: Valentin Olteanu
>            Priority: Major
>
> Currently, there is no way to run queries in sling using the clients. This is 
> needed in several tests to search content and assert the effects of a feature.
>  
> The solution proposed in 
> [https://github.com/apache/sling-org-apache-sling-testing-clients/pull/5] 
>  * installs a custom query servlet
>  * runs the query in any of the supported format
>  * returns results as json



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to