/*
 * Copyright 2010-2013 Ning, Inc.
 * Copyright 2014 Groupon, Inc
 * Copyright 2014 The Billing Project, LLC
 *
 * The Billing Project 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.killbill.billing.jaxrs;

import org.killbill.billing.jaxrs.resources.AccountResource;
import org.killbill.billing.jaxrs.util.JaxrsUriBuilder;
import org.testng.annotations.Test;

import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.net.URI;
import java.util.UUID;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;

public class TestBuildResponse {
    @Test(groups = "fast", description = "Tests Uri Builder with Path Like URL and Root location")
    public void testUriBuilderWithPathLikeUrlAndRoot() throws Exception {
        UUID objectId = UUID.randomUUID();

        final UriInfo uriInfo = mock(UriInfo.class);
        URI uri = URI.create("http://localhost:8080");
        when(uriInfo.getBaseUri()).thenReturn(uri);

        JaxrsUriBuilder uriBuilder = new JaxrsUriBuilder();
        Response response = uriBuilder.buildResponse(uriInfo, AccountResource.class, "getAccount", objectId, true);

        assertEquals(response.getMetadata().get("Location").get(0), "/1.0/kb/accounts/" + objectId.toString());
    }

    @Test(groups = "fast", description = "Tests Uri Builder with Path Like URL and non root Location")
    public void testUriBuilderWithPathLikeUrlAndNonRoot() throws Exception {
        UUID objectId = UUID.randomUUID();

        final UriInfo uriInfo = mock(UriInfo.class);
        URI uri = URI.create("http://localhost:8080/killbill");
        when(uriInfo.getBaseUri()).thenReturn(uri);

        JaxrsUriBuilder uriBuilder = new JaxrsUriBuilder();
        Response response = uriBuilder.buildResponse(uriInfo, AccountResource.class, "getAccount", objectId, true);

        assertEquals(response.getMetadata().get("Location").get(0), "/killbill/1.0/kb/accounts/" + objectId.toString());
    }

    @Test(groups = "fast", description = "Tests Uri Builder without Path Like URL and root Location")
    public void testUriBuilderWithoutPathLikeUrlAndRoot() throws Exception {
        UUID objectId = UUID.randomUUID();

        final UriInfo uriInfo = mock(UriInfo.class);
        URI uri = URI.create("http://localhost:8080");
        when(uriInfo.getBaseUri()).thenReturn(uri);
        when(uriInfo.getAbsolutePath()).thenReturn(uri);

        JaxrsUriBuilder uriBuilder = new JaxrsUriBuilder();
        Response response = uriBuilder.buildResponse(uriInfo, AccountResource.class, "getAccount", objectId, false);

        assertEquals(response.getMetadata().get("Location").get(0).toString(), uri.toString() + "/1.0/kb/accounts/" + objectId.toString());
    }

    @Test(groups = "fast", description = "Tests Uri Builder without Path Like URL and root Location")
    public void testUriBuilderWithoutPathLikeUrlAndNonRoot() throws Exception {
        UUID objectId = UUID.randomUUID();

        final UriInfo uriInfo = mock(UriInfo.class);
        URI uri = URI.create("http://localhost:8080/killbill");
        when(uriInfo.getBaseUri()).thenReturn(uri);
        when(uriInfo.getAbsolutePath()).thenReturn(uri);

        JaxrsUriBuilder uriBuilder = new JaxrsUriBuilder();
        Response response = uriBuilder.buildResponse(uriInfo, AccountResource.class, "getAccount", objectId, false);

        assertEquals(response.getMetadata().get("Location").get(0).toString(), uri.toString() + "/1.0/kb/accounts/" + objectId.toString());
    }
}
