/*
 * 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.apache.camel.component.graphql;

import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.http.handler.AuthenticationValidationHandler;
import org.apache.hc.core5.http.impl.bootstrap.HttpServer;
import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap;
import org.junit.jupiter.api.Test;

import static org.apache.camel.component.http.HttpMethods.POST;

/**
 * Verify 'AuthClientConfigurer' instances registered with Basic credentials.
 */
public class GraphqlBasicAuthenticationTest extends BaseGraphqlTest {

    private final String user = "camel";
    private final String password = "password";
    private HttpServer localServer;

    @Override
    public void setupResources() throws Exception {
        localServer = ServerBootstrap.bootstrap()
                .setCanonicalHostName("localhost").setHttpProcessor(getBasicHttpProcessor())
                .register("/graphql",
                        new AuthenticationValidationHandler(
                                POST.name(), null, null, getExpectedContent(), user, password))
                .create();
        localServer.start();
    }

    @Override
    public void cleanupResources() {
        if (localServer != null) {
            localServer.stop();
        }
    }

    // verify that an endpoint configured with the correct credentials passes authentication
    @Test
    public void shouldPassAuthentication() {
        Exchange exchange = template.request("direct:start1", exchange1 -> {
        });

        assertExchange(exchange);
    }

    // verify that an endpoint configured with wrong credentials fails authentication
    @Test
    public void shouldFailAuthorisation() {
        Exchange exchange = template.request("direct:start2", exchange1 -> {
        });

        assertUnauthorizedResponse(exchange);
    }

    @Override
    protected RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {
            // multiple routes to verify registration of multiple configurers
            @Override
            public void configure() {
                from("direct:start1")
                        .to("graphql://http://localhost:" + localServer.getLocalPort()
                            + "/graphql?query={books{id name}}"
                            + "&username=" + user + "&password=" + password);

                from("direct:start2")
                        .to("graphql://http://localhost:" + localServer.getLocalPort()
                            + "/graphql?query={books{id name}}"
                            + "&username=" + user + "&password=wrongPassword");
            }
        };
    }
}
