// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.restapi;

import org.apache.http.client.utils.URIBuilder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Optional;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

public class RestUriTest {

    URI createUri(String path, String query) throws URISyntaxException {
        return new URIBuilder()
	    .addParameter("foo", "bar")
	    .setHost("host")
	    .setScheme("http")
	    .setPort(666)
	    .setPath(path)
	    .setCustomQuery(query)
	    .setFragment("fargment").build();
    }

    @Rule
	public ExpectedException thrown= ExpectedException.none();

    @Test
	public void testBasic() throws Exception {
        RestUri restUri = new RestUri(createUri("/document/v1/namespace/doctype/docid/myid", "query"));
        assertThat(restUri.getDocId(), is("myid"));
        assertThat(restUri.getDocumentType(), is("doctype"));
        assertThat(restUri.getNamespace(), is("namespace"));
        assertThat(restUri.getGroup(), is(Optional.<RestUri.Group>empty()));
        assertThat(restUri.generateFullId(), is("id:namespace:doctype::myid"));
    }

    @Test
    public void encodingSlashes() throws Exception {
        // Try with slashes encoded.
        final String id = " !\"øæåp/:;&,.:;'1Q";
        String encodedId = URLEncoder.encode(id, StandardCharsets.UTF_8.name());
        RestUri restUri = new RestUri(URI.create("/document/v1/namespace/doctype/docid/" + encodedId));
        assertThat(restUri.getDocId(), is(id));
        assertThat(restUri.getDocumentType(), is("doctype"));
        assertThat(restUri.getNamespace(), is("namespace"));
        assertThat(restUri.getGroup(), is(Optional.<RestUri.Group>empty()));
        assertThat(restUri.generateFullId(), is("id:namespace:doctype::" + id));
    }

    @Test
    public void encodingSlashes2() throws Exception {
        // This will decode the slashes.
        final String id = " !\"øæåp/:;&,.:;'1Q ";
        RestUri restUri = new RestUri(createUri("/document/v1/namespace/doctype/docid/" + id, "query"));
        assertThat(restUri.getDocId(), is(id));
        assertThat(restUri.getDocumentType(), is("doctype"));
        assertThat(restUri.getNamespace(), is("namespace"));
        assertThat(restUri.getGroup(), is(Optional.<RestUri.Group>empty()));
        assertThat(restUri.generateFullId(), is("id:namespace:doctype::" + id));
    }


    @Test
    public void testVisit() throws Exception {
        RestUri restUri = new RestUri(createUri("/document/v1/namespace/doctype/docid/", "query"));
        assertThat(restUri.getDocId(), is(""));
        assertThat(restUri.getDocumentType(), is("doctype"));
        assertThat(restUri.getNamespace(), is("namespace"));
        assertThat(restUri.getGroup(), is(Optional.<RestUri.Group>empty()));
        assertThat(restUri.generateFullId(), is("id:namespace:doctype::"));
    }

    @Test
	public void testOneSlashTooMuchWhichIsFine() throws Exception {
        RestUri restUri = new RestUri(createUri("/document/v1/namespace/doctype/docid/myid:342:23/wrong", ""));
        assertThat(restUri.getDocId(), is("myid:342:23/wrong"));
    }

    @Test
	public void testGroupG() throws Exception {
        RestUri restUri = new RestUri(createUri("/document/v1/namespace/doctype/group/group/myid", ""));
        assertThat(restUri.getDocId(), is("myid"));
        assertThat(restUri.getDocumentType(), is("doctype"));
        assertThat(restUri.getGroup().get().name, is('g'));
        assertThat(restUri.getGroup().get().value, is("group"));
        assertThat(restUri.generateFullId(), is("id:namespace:doctype:g=group:myid"));
    }

    @Test
	public void testGroupN() throws Exception {
        RestUri restUri = new RestUri(createUri("/document/v1/namespace/doctype/number/group/myid", ""));
        assertThat(restUri.getGroup().get().name, is('n'));
        assertThat(restUri.getGroup().get().value, is("group"));
    }

    @Test
	public void testGroupUnknown() throws Exception {
        thrown.expect(RestApiException.class);
        new RestUri(createUri("/document/v1/namespace/doctype/Q/myid", ""));
    }

}

@@ -4404,10 +4404,609 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void testDocIdAsIs() throws Exception %7B%0A        RestUri restUri = new RestUri(new URI(%22/document/v1/test/newsarticle/docid/http://vn.news.yahoo.com/gi-th-ng-t-n-ng-khoa-h-205000458.html%22).normalize());%0A        assertThat(restUri.getNamespace(), is(%22test%22));%0A        assertThat(restUri.getDocumentType(), is(%22newsarticle%22));%0A        assertThat(restUri.getDocId(), is(%22http://vn.news.yahoo.com/gi-th-ng-t-n-ng-khoa-h-205000458.html%22));%0A        assertThat(restUri.generateFullId(), is(%22id:test:newsarticle::http://vn.news.yahoo.com/gi-th-ng-t-n-ng-khoa-h-205000458.html%22));%0A    %7D%0A%0A
 %7D%0A

Add a failing unit test for consecutive slahes in uri