/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2019 Yegor Bugayenko
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package org.takes.servlet;

import java.util.Collections;
import java.util.NoSuchElementException;
import org.cactoos.list.ListOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.collection.IsIterableContainingInAnyOrder;
import org.hamcrest.core.IsCollectionContaining;
import org.hamcrest.core.IsEqual;
import org.junit.Rule;
import org.junit.jupiter.api.Test;
import org.junit.rules.ExpectedException;
import org.takes.rq.RqFake;
import org.takes.rq.RqMethod;
import org.takes.rq.RqWithHeaders;

/**
 * Test case for {@link  HttpServletRequestFake}.
 *
 * @since 1.15
 */
public final class HttpServletRequestFakeTest {
    /**
     * A rule for handling an exception.
     */
    @Rule
    public final ExpectedException exception = ExpectedException.none();

    @Test
    public void failsIfAHeaderIsNotFound() {
        this.exception.expect(NoSuchElementException.class);
        this.exception.expectMessage("Value of header foo not found");
        new HttpServletRequestFake(
            new RqWithHeaders(
                new RqFake(),
                "MyHeader: theValue",
                "MyOtherHeader: aValue"
            )
        ).getHeaders("foo");
    }

    @Test
    public void containsAHeaderAndItsValue() {
        MatcherAssert.assertThat(
            "Can't get the headers",
            Collections.list(
                new HttpServletRequestFake(
                    new RqWithHeaders(
                        new RqFake(),
                        "TestHeader: someValue",
                        "SomeHeader: testValue"
                    )
                ).getHeaders("testheader")
            ),
            new IsCollectionContaining<>(
                new IsEqual<>("someValue")
            )
        );
    }

    @SuppressWarnings("unchecked")
    @Test
    public void containsAllHeadersNames() {
        MatcherAssert.assertThat(
            "Can't get the header names",
            Collections.list(
                new HttpServletRequestFake(
                    new RqWithHeaders(
                        new RqFake(),
                        "AnyHeader: anyValue",
                        "CrazyHeader: crazyValue"
                    )
                ).getHeaderNames()
            ),
            new IsIterableContainingInAnyOrder<>(
                new ListOf<>(
                    new IsEqual<>("host"),
                    new IsEqual<>("crazyheader"),
                    new IsEqual<>("anyheader")
                )
            )
        );
    }

    @Test
    public void defaultMethodIsGet() {
        MatcherAssert.assertThat(
            "Can't get the request method",
            new HttpServletRequestFake(new RqFake()).getMethod(),
            new IsEqual<>(RqMethod.GET)
        );
    }
}