/*
 * 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.eclipse.aether.transport.http;

import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class UriUtilsTest {

    private String resolve(URI base, String ref) {
        return UriUtils.resolve(base, URI.create(ref)).toString();
    }

    @Test
    public void testResolveBaseEmptyPath() {
        URI base = URI.create("http://host");
        assertEquals("http://host/file.jar", resolve(base, "file.jar"));
        assertEquals("http://host/dir/file.jar", resolve(base, "dir/file.jar"));
        assertEquals("http://host?arg=val", resolve(base, "?arg=val"));
        assertEquals("http://host/file?arg=val", resolve(base, "file?arg=val"));
        assertEquals("http://host/dir/file?arg=val", resolve(base, "dir/file?arg=val"));
    }

    @Test
    public void testResolveBaseRootPath() {
        URI base = URI.create("http://host/");
        assertEquals("http://host/file.jar", resolve(base, "file.jar"));
        assertEquals("http://host/dir/file.jar", resolve(base, "dir/file.jar"));
        assertEquals("http://host/?arg=val", resolve(base, "?arg=val"));
        assertEquals("http://host/file?arg=val", resolve(base, "file?arg=val"));
        assertEquals("http://host/dir/file?arg=val", resolve(base, "dir/file?arg=val"));
    }

    @Test
    public void testResolveBasePathTrailingSlash() {
        URI base = URI.create("http://host/sub/dir/");
        assertEquals("http://host/sub/dir/file.jar", resolve(base, "file.jar"));
        assertEquals("http://host/sub/dir/dir/file.jar", resolve(base, "dir/file.jar"));
        assertEquals("http://host/sub/dir/?arg=val", resolve(base, "?arg=val"));
        assertEquals("http://host/sub/dir/file?arg=val", resolve(base, "file?arg=val"));
        assertEquals("http://host/sub/dir/dir/file?arg=val", resolve(base, "dir/file?arg=val"));
    }

    @Test
    public void testResolveBasePathNoTrailingSlash() {
        URI base = URI.create("http://host/sub/d%20r");
        assertEquals("http://host/sub/d%20r/file.jar", resolve(base, "file.jar"));
        assertEquals("http://host/sub/d%20r/dir/file.jar", resolve(base, "dir/file.jar"));
        assertEquals("http://host/sub/d%20r?arg=val", resolve(base, "?arg=val"));
        assertEquals("http://host/sub/d%20r/file?arg=val", resolve(base, "file?arg=val"));
        assertEquals("http://host/sub/d%20r/dir/file?arg=val", resolve(base, "dir/file?arg=val"));
    }

    private List<URI> getDirs(String base, String uri) {
        return UriUtils.getDirectories((base != null) ? URI.create(base) : null, URI.create(uri));
    }

    private void assertUris(List<URI> actual, String... expected) {
        List<String> uris = new ArrayList<>(actual.size());
        for (URI uri : actual) {
            uris.add(uri.toString());
        }
        assertEquals(Arrays.asList(expected), uris);
    }

    @Test
    public void testGetDirectoriesNoBase() {
        List<URI> parents = getDirs(null, "http://host/repo/sub/dir/file.jar");
        assertUris(parents, "http://host/repo/sub/dir/", "http://host/repo/sub/", "http://host/repo/");

        parents = getDirs(null, "http://host/repo/sub/dir/?file.jar");
        assertUris(parents, "http://host/repo/sub/dir/", "http://host/repo/sub/", "http://host/repo/");

        parents = getDirs(null, "http://host/");
        assertUris(parents);
    }

    @Test
    public void testGetDirectoriesExplicitBaseTrailingSlash() {
        List<URI> parents = getDirs("http://host/repo/", "http://host/repo/sub/dir/file.jar");
        assertUris(parents, "http://host/repo/sub/dir/", "http://host/repo/sub/");

        parents = getDirs("http://host/repo/", "http://host/repo/sub/dir/?file.jar");
        assertUris(parents, "http://host/repo/sub/dir/", "http://host/repo/sub/");

        parents = getDirs("http://host/repo/", "http://host/");
        assertUris(parents);
    }

    @Test
    public void testGetDirectoriesExplicitBaseNoTrailingSlash() {
        List<URI> parents = getDirs("http://host/repo", "http://host/repo/sub/dir/file.jar");
        assertUris(parents, "http://host/repo/sub/dir/", "http://host/repo/sub/");

        parents = getDirs("http://host/repo", "http://host/repo/sub/dir/?file.jar");
        assertUris(parents, "http://host/repo/sub/dir/", "http://host/repo/sub/");

        parents = getDirs("http://host/repo", "http://host/");
        assertUris(parents);
    }
}
