/*
 * ====================================================================
 * 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.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */
package org.apache.hc.client5.http.impl.cache;

import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import org.apache.hc.client5.http.HeadersMatcher;
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.apache.hc.client5.http.cache.HttpCacheEntry;
import org.apache.hc.client5.http.cache.RequestCacheControl;
import org.apache.hc.client5.http.cache.ResponseCacheControl;
import org.apache.hc.client5.http.utils.DateUtils;
import org.apache.hc.client5.http.validator.ETag;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.message.BasicHeader;
import org.apache.hc.core5.http.message.MessageSupport;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class TestConditionalRequestBuilder {

    private ConditionalRequestBuilder impl;

    @BeforeEach
    void setUp() {
        impl = new ConditionalRequestBuilder();
    }

    @Test
    void testMakeConditionalWithLastModified() {
        final String theMethod = "GET";
        final String theUri = "/theuri";
        final String lastModified = "this is my last modified date";

        final SimpleHttpRequest basicRequest = new SimpleHttpRequest(theMethod, theUri);
        basicRequest.addHeader("Accept-Encoding", "gzip");

        final Header[] headers = new Header[] {
                new BasicHeader("Date", DateUtils.formatStandardDate(Instant.now())),
                new BasicHeader("Last-Modified", lastModified) };

        final HttpCacheEntry cacheEntry = HttpTestUtils.makeCacheEntry(headers);
        final ResponseCacheControl cacheControl = ResponseCacheControl.builder().build();
        final SimpleHttpRequest newRequest = impl.makeConditional(cacheControl, basicRequest, cacheEntry);

        Assertions.assertEquals(theMethod, newRequest.getMethod());
        Assertions.assertEquals(theUri, newRequest.getRequestUri());
        Assertions.assertEquals(2, newRequest.getHeaders().length);

        HeadersMatcher.assertSame(
                newRequest.getHeaders(),
                new BasicHeader("Accept-Encoding", "gzip"),
                new BasicHeader("If-Modified-Since", lastModified));
    }

    @Test
    void testConditionalRequestForEntryWithLastModifiedAndEtagIncludesBothAsValidators() {
        final Instant now = Instant.now();
        final Instant tenSecondsAgo = now.minusSeconds(10);
        final Instant twentySecondsAgo = now.plusSeconds(20);
        final String lmDate = DateUtils.formatStandardDate(twentySecondsAgo);
        final String etag = "\"etag\"";
        final Header[] headers = {
            new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
            new BasicHeader("Last-Modified", lmDate),
            new BasicHeader("ETag", etag)
        };
        final SimpleHttpRequest origRequest = new SimpleHttpRequest("GET", "/");
        final HttpCacheEntry cacheEntry = HttpTestUtils.makeCacheEntry(headers);
        final ResponseCacheControl cacheControl = ResponseCacheControl.builder().build();
        final HttpRequest result = impl.makeConditional(cacheControl, origRequest, cacheEntry);
        Assertions.assertEquals(lmDate,
                result.getFirstHeader("If-Modified-Since").getValue());
        Assertions.assertEquals(etag,
                result.getFirstHeader("If-None-Match").getValue());
    }

    @Test
    void testMakeConditionalWithETag() {
        final String theMethod = "GET";
        final String theUri = "/theuri";
        final String theETag = "\"this is my eTag\"";

        final SimpleHttpRequest origRequest = new SimpleHttpRequest(theMethod, theUri);
        origRequest.addHeader("Accept-Encoding", "gzip");

        final Instant now = Instant.now();

        final Header[] headers = new Header[] {
                new BasicHeader("Date", DateUtils.formatStandardDate(now)),
                new BasicHeader("Last-Modified", DateUtils.formatStandardDate(now)),
                new BasicHeader("ETag", theETag) };

        final HttpCacheEntry cacheEntry = HttpTestUtils.makeCacheEntry(headers);

        final ResponseCacheControl cacheControl = ResponseCacheControl.builder().build();
        final HttpRequest newRequest = impl.makeConditional(cacheControl, origRequest, cacheEntry);

        Assertions.assertEquals(theMethod, newRequest.getMethod());
        Assertions.assertEquals(theUri, newRequest.getRequestUri());

        HeadersMatcher.assertSame(
                newRequest.getHeaders(),
                new BasicHeader("Accept-Encoding", "gzip"),
                new BasicHeader("If-None-Match", theETag),
                new BasicHeader("If-Modified-Since", DateUtils.formatStandardDate(now)));
    }

    @Test
    void testCacheEntryWithMustRevalidateDoesEndToEndRevalidation() {
        final SimpleHttpRequest origRequest = new SimpleHttpRequest("GET","/");
        final Instant now = Instant.now();
        final Instant elevenSecondsAgo = now.minusSeconds(11);
        final Instant tenSecondsAgo = now.minusSeconds(10);
        final Instant nineSecondsAgo = now.plusSeconds(9);

        final Header[] cacheEntryHeaders = new Header[] {
                new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
                new BasicHeader("ETag", "\"etag\"")
        };
        final HttpCacheEntry cacheEntry = HttpTestUtils.makeCacheEntry(elevenSecondsAgo, nineSecondsAgo, cacheEntryHeaders);

        final ResponseCacheControl responseCacheControl = ResponseCacheControl.builder()
                .setMaxAge(5)
                .setMustRevalidate(true)
                .build();
        final HttpRequest result = impl.makeConditional(responseCacheControl, origRequest, cacheEntry);

        final RequestCacheControl requestCacheControl = CacheControlHeaderParser.INSTANCE.parse(result);
        Assertions.assertEquals(0, requestCacheControl.getMaxAge());
    }

    @Test
    void testCacheEntryWithProxyRevalidateDoesEndToEndRevalidation() {
        final SimpleHttpRequest origRequest = new SimpleHttpRequest("GET", "/");
        final Instant now = Instant.now();
        final Instant elevenSecondsAgo = now.minusSeconds(11);
        final Instant tenSecondsAgo = now.minusSeconds(10);
        final Instant nineSecondsAgo = now.plusSeconds(9);

        final Header[] cacheEntryHeaders = new Header[] {
                new BasicHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo)),
                new BasicHeader("ETag", "\"etag\"")
        };
        final HttpCacheEntry cacheEntry = HttpTestUtils.makeCacheEntry(elevenSecondsAgo, nineSecondsAgo, cacheEntryHeaders);

        final ResponseCacheControl responseCacheControl = ResponseCacheControl.builder()
                .setMaxAge(5)
                .setProxyRevalidate(true)
                .build();
        final HttpRequest result = impl.makeConditional(responseCacheControl, origRequest, cacheEntry);

        final RequestCacheControl requestCacheControl = CacheControlHeaderParser.INSTANCE.parse(result);
        Assertions.assertEquals(0, requestCacheControl.getMaxAge());
    }

    @Test
    void testBuildUnconditionalRequestUsesGETMethod() {
        final SimpleHttpRequest request = new SimpleHttpRequest("GET", "/");
        final SimpleHttpRequest result = impl.makeUnconditional(request);
        Assertions.assertEquals("GET", result.getMethod());
    }

    @Test
    void testBuildUnconditionalRequestUsesRequestUri() {
        final String uri = "/theURI";
        final SimpleHttpRequest request = new SimpleHttpRequest("GET", uri);
        final SimpleHttpRequest result = impl.makeUnconditional(request);
        Assertions.assertEquals(uri, result.getRequestUri());
    }

    @Test
    void testBuildUnconditionalRequestAddsCacheControlNoCache() {
        final SimpleHttpRequest request = new SimpleHttpRequest("GET", "/");
        final SimpleHttpRequest result = impl.makeUnconditional(request);
        final RequestCacheControl requestCacheControl = CacheControlHeaderParser.INSTANCE.parse(result);
        Assertions.assertTrue(requestCacheControl.isNoCache());
    }

    @Test
    void testBuildUnconditionalRequestDoesNotUseIfRange() {
        final SimpleHttpRequest request = new SimpleHttpRequest("GET", "/");
        request.addHeader("If-Range","\"etag\"");
        final SimpleHttpRequest result = impl.makeUnconditional(request);
        Assertions.assertNull(result.getFirstHeader("If-Range"));
    }

    @Test
    void testBuildUnconditionalRequestDoesNotUseIfMatch() {
        final SimpleHttpRequest request = new SimpleHttpRequest("GET", "/");
        request.addHeader("If-Match","\"etag\"");
        final SimpleHttpRequest result = impl.makeUnconditional(request);
        Assertions.assertNull(result.getFirstHeader("If-Match"));
    }

    @Test
    void testBuildUnconditionalRequestDoesNotUseIfNoneMatch() {
        final SimpleHttpRequest request = new SimpleHttpRequest("GET", "/");
        request.addHeader("If-None-Match","\"etag\"");
        final SimpleHttpRequest result = impl.makeUnconditional(request);
        Assertions.assertNull(result.getFirstHeader("If-None-Match"));
    }

    @Test
    void testBuildUnconditionalRequestDoesNotUseIfUnmodifiedSince() {
        final SimpleHttpRequest request = new SimpleHttpRequest("GET", "/");
        request.addHeader("If-Unmodified-Since", DateUtils.formatStandardDate(Instant.now()));
        final SimpleHttpRequest result = impl.makeUnconditional(request);
        Assertions.assertNull(result.getFirstHeader("If-Unmodified-Since"));
    }

    @Test
    void testBuildUnconditionalRequestDoesNotUseIfModifiedSince() {
        final SimpleHttpRequest request = new SimpleHttpRequest("GET", "/");
        request.addHeader("If-Modified-Since", DateUtils.formatStandardDate(Instant.now()));
        final SimpleHttpRequest result = impl.makeUnconditional(request);
        Assertions.assertNull(result.getFirstHeader("If-Modified-Since"));
    }

    @Test
    void testBuildUnconditionalRequestCarriesOtherRequestHeaders() {
        final SimpleHttpRequest request = new SimpleHttpRequest("GET", "/");
        request.addHeader("User-Agent","MyBrowser/1.0");
        final SimpleHttpRequest result = impl.makeUnconditional(request);
        Assertions.assertEquals("MyBrowser/1.0",
                result.getFirstHeader("User-Agent").getValue());
    }

    @Test
    void testMakeConditionalFromVariants() {
        final ETag etag1 = new ETag("123");
        final ETag etag2 = new ETag("456");
        final ETag etag3 = new ETag("789");

        final List<ETag> variantEntries = Arrays.asList(etag1, etag2, etag3);

        final SimpleHttpRequest request = new SimpleHttpRequest("GET", "/");
        final SimpleHttpRequest conditional = impl.makeConditionalRequestVariants(request, variantEntries);


        final Iterator<String> it = MessageSupport.iterateTokens(conditional, HttpHeaders.IF_NONE_MATCH);
        final List<ETag> etags = new ArrayList<>();
        while (it.hasNext()) {
            etags.add(ETag.parse(it.next()));
        }
        Assertions.assertEquals(
                new HashSet<>(Arrays.asList(etag1, etag2, etag3)),
                new HashSet<>(etags));
    }

}
