/*
 * Licensed to David Pilato (the "Author") under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Author 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 fr.pilato.elasticsearch.crawler.fs.test.integration;

import fr.pilato.elasticsearch.crawler.fs.FsCrawlerImpl;
import fr.pilato.elasticsearch.crawler.fs.meta.settings.TimeValue;
import fr.pilato.elasticsearch.crawler.fs.rest.ServerStatusResponse;
import fr.pilato.elasticsearch.crawler.fs.rest.UploadResponse;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
import org.junit.Test;

import javax.ws.rs.core.MediaType;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class FsCrawlerRestIT extends AbstractRestITCase {

    @Test
    public void testCallRoot() {
        ServerStatusResponse status = restCall("/", ServerStatusResponse.class);
        assertThat(status.getVersion(), is(FsCrawlerImpl.properties.getProperty("fscrawler.version")));
        assertThat(status.getElasticsearch(), notNullValue());
    }

    @Test
    public void testAllDocuments() throws Exception {
        String url = getUrl("documents");
        Path from = Paths.get(url);
        Files.walk(from)
                .filter(path -> Files.isRegularFile(path))
                .forEach(path -> {
                    UploadResponse response = uploadFile(path);
                    assertThat(response.getFilename(), is(path.getFileName().toString()));
                });
        Long numFiles = Files.list(from).count();

        // We wait until we have all docs
        countTestHelper(REST_INDEX, null, numFiles.intValue(), null, TimeValue.timeValueMinutes(1));
    }

    private static final Map<String, Object> debugOption = new HashMap<>();

    static {
        debugOption.put("debug", true);
        debugOption.put("simulate", true);
    }

    public static UploadResponse uploadFile(Path file) {
        assertThat(Files.exists(file), is(true));

        // MediaType of the body part will be derived from the file.
        FileDataBodyPart filePart = new FileDataBodyPart("file", file.toFile(), MediaType.APPLICATION_OCTET_STREAM_TYPE);
        FormDataMultiPart mp = new FormDataMultiPart();
        mp.bodyPart(filePart);

        if (staticLogger.isDebugEnabled()) {
            staticLogger.debug("Rest response: {}", restCall("/_upload", mp, String.class, debugOption));
        }

        return restCall("/_upload", mp, UploadResponse.class, Collections.emptyMap());
    }
}

@@ -1236,24 +1236,54 @@
 taBodyPart;%0A
+import org.junit.BeforeClass;%0A
 import org.j
@@ -1698,16 +1698,245 @@
 Case %7B%0A%0A
+    @BeforeClass%0A    public static void removeOldIndex() throws Exception %7B%0A        staticLogger.info(%22 -%3E Removing existing index %5Bfscrawler_rest_tests%5D%22);%0A        elasticsearchClient.deleteIndex(%22fscrawler_rest_tests%22);%0A    %7D%0A%0A
     @Tes

Remove existing index when starting REST tests