/*
 * 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.launchpad.webapp.integrationtest.indexing;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.commons.testing.integration.HttpTestBase;
import org.apache.sling.testing.tools.retry.RetryLoop;
import org.apache.sling.testing.tools.retry.RetryLoop.Condition;

/**
 * The <tt>FullTextIndexingTest</tt> verifies that a PDF file which is uploaded will have its contents indexed and
 * available for full-text searches
 * 
 */
public class FullTextIndexingTest extends HttpTestBase {

    private String folderName;
    private String fileName = "lorem-ipsum.pdf";

    public void testUploadedPdfIsIndexed() throws IOException, JSONException {

        String localPath = "/integration-test/indexing/" + fileName;
        InputStream resourceToUpload = getClass().getResourceAsStream(localPath);
        if (resourceToUpload == null)
            throw new IllegalArgumentException("No resource to upload found at " + localPath);

        testClient.mkdir(WEBDAV_BASE_URL + "/" + folderName);
        testClient.upload(WEBDAV_BASE_URL + "/" + folderName + "/" + fileName, resourceToUpload);

        final String fullTextSearchParameter = "Excepteur";
        final String queryUrl = WEBDAV_BASE_URL + "/content.query.json?queryType=xpath&statement="
                + URLEncoder.encode("/jcr:root/" + folderName 
                + "//*[jcr:contains(.,'" + fullTextSearchParameter+ "')]", "UTF-8");

        new RetryLoop(new Condition() {

            public boolean isTrue() throws Exception {
                String result = getContent(queryUrl, CONTENT_TYPE_JSON);

                JSONArray results = new JSONArray(result);

                if (results.length() == 0) {
                    return false;
                }

                String expectedPath = SERVLET_CONTEXT + "/" + folderName + "/" + fileName + "/jcr:content";

                for (int i = 0; i < results.length(); i++) {
                    JSONObject object = results.getJSONObject(i);
                    if (expectedPath.equals(object.getString("jcr:path"))) {
                        return true;
                    }
                }

                return false;
            }

            public String getDescription() {
                return "A document containing '" + fullTextSearchParameter + "' is found under /" + folderName;
            }
        }, 10, 50);
    }

    protected void setUp() throws Exception {
        super.setUp();

        folderName = getClass().getSimpleName();
        testClient.delete(WEBDAV_BASE_URL + "/" + folderName);
    }

    protected void tearDown() throws Exception {

        testClient.delete(WEBDAV_BASE_URL + "/" + folderName);
        super.tearDown();
    }
}

@@ -1164,20 +1164,16 @@
 HttpTest
-Base
 ;%0Aimport
@@ -1285,16 +1285,88 @@
 ndition;
+%0Aimport org.junit.After;%0Aimport org.junit.Before;%0Aimport org.junit.Test;
 %0A%0A/**%0A *
@@ -1513,16 +1513,45 @@
 searches
+, in several different paths.
 %0A * %0A */
@@ -1589,30 +1589,57 @@
 est 
-extends HttpTestBase %7B
+%7B%0A%0A    private final HttpTest H = new HttpTest();
 %0A%0A  
@@ -1713,24 +1713,34 @@
 psum.pdf%22;%0A%0A
+    @Test%0A
     public v
@@ -2092,33 +2092,38 @@
 Path);%0A%0A        
-t
+H.getT
 estClient.mkdir(
@@ -2119,15 +2119,26 @@
 ient
+()
 .mkdir(
+HttpTest.
 WEBD
@@ -2170,33 +2170,38 @@
 rName);%0A        
-t
+H.getT
 estClient.upload
@@ -2197,16 +2197,18 @@
 ient
+()
 .upload(
 WEBD
@@ -2203,16 +2203,25 @@
 .upload(
+HttpTest.
 WEBDAV_B
@@ -2377,16 +2377,25 @@
 ryUrl = 
+HttpTest.
 WEBDAV_B
@@ -2730,16 +2730,18 @@
 esult = 
+H.
 getConte
@@ -2753,16 +2753,25 @@
 eryUrl, 
+HttpTest.
 CONTENT_
@@ -2979,16 +2979,25 @@
 dPath = 
+HttpTest.
 SERVLET_
@@ -3584,33 +3584,42 @@
 %0A    %7D%0A%0A    
-protected
+@Before%0A    public
  void setUp(
@@ -3647,21 +3647,17 @@
         
-super
+H
 .setUp()
@@ -3654,25 +3654,24 @@
  H.setUp();%0A
-%0A
         fold
@@ -3707,33 +3707,38 @@
 Name();%0A        
-t
+H.getT
 estClient.delete
@@ -3726,32 +3726,34 @@
 etTestClient
+()
 .delete(
 WEBDAV_BASE_
@@ -3732,32 +3732,41 @@
 Client().delete(
+HttpTest.
 WEBDAV_BASE_URL 
@@ -3801,17 +3801,25 @@
     
-protected
+@After%0A    public
  voi
@@ -3850,25 +3850,24 @@
 ption %7B%0A
-%0A
         
 testClie
@@ -3858,17 +3858,22 @@
         
-t
+H.getT
 estClien
@@ -3873,24 +3873,26 @@
 stClient
+()
 .delete(
 WEBDAV_B
@@ -3883,16 +3883,25 @@
 .delete(
+HttpTest.
 WEBDAV_B
@@ -3941,13 +3941,9 @@
     
-super
+H
 .tea
@@ -3958,9 +3958,8 @@
 %0A    %7D%0A%7D
-%0A

Prepare for Parameterized test