/*
 * 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.ahc.ws;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.AvailablePortFinder;
import org.apache.camel.test.junit5.CamelTestSupport;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 *
 */
public class WsProducerConsumerTest extends CamelTestSupport {

    protected static final String TEST_MESSAGE = "Hello World!";
    protected static final String TEST_CONNECTED_MESSAGE = "Connected!";
    protected static final int PORT = AvailablePortFinder.getNextAvailable();

    private static final Logger LOG = LoggerFactory.getLogger(WsProducerConsumerTest.class);

    protected Server server;

    protected List<Object> messages;

    public void startTestServer() throws Exception {
        // start a simple websocket echo service
        server = new Server(PORT);
        Connector connector = new ServerConnector(server);
        server.addConnector(connector);

        ServletContextHandler ctx = new ServletContextHandler();
        ctx.setContextPath("/");
        ctx.addServlet(TestServletFactory.class.getName(), "/*");

        server.setHandler(ctx);

        server.start();
        assertTrue(server.isStarted());
    }

    public void stopTestServer() throws Exception {
        server.stop();
        server.destroy();
    }

    @Override
    @BeforeEach
    public void setUp() throws Exception {
        startTestServer();
        super.setUp();
    }

    @Override
    @AfterEach
    public void tearDown() throws Exception {
        super.tearDown();
        stopTestServer();
    }

    @Test
    public void testTwoRoutes() throws Exception {
        MockEndpoint mock = getMockEndpoint("mock:result");
        mock.expectedBodiesReceived(TEST_MESSAGE);

        template.sendBody("direct:input", TEST_MESSAGE);

        mock.assertIsSatisfied();
    }

    @Test
    public void testTwoRoutesRestartConsumer() throws Exception {
        MockEndpoint mock = getMockEndpoint("mock:result");
        mock.expectedBodiesReceived(TEST_MESSAGE);

        template.sendBody("direct:input", TEST_MESSAGE);

        mock.assertIsSatisfied();

        resetMocks();

        LOG.info("Restarting bar route");

        boolean stopped = context.getRouteController().stopRoute("bar", 500, TimeUnit.MILLISECONDS, true);
        assertTrue(stopped, "The route should have stopped within the specified time");
        context.getRouteController().startRoute("bar");

        mock.expectedBodiesReceived(TEST_MESSAGE);

        template.sendBody("direct:input", TEST_MESSAGE);

        mock.assertIsSatisfied();
    }

    @Test
    public void testTwoRoutesRestartProducer() throws Exception {
        MockEndpoint mock = getMockEndpoint("mock:result");
        mock.expectedBodiesReceived(TEST_MESSAGE);

        template.sendBody("direct:input", TEST_MESSAGE);

        mock.assertIsSatisfied();

        resetMocks();

        LOG.info("Restarting foo route");

        boolean stopped = context.getRouteController().stopRoute("foo", 500, TimeUnit.MILLISECONDS, true);
        assertTrue(stopped, "The route should have stopped within the specified time");

        context.getRouteController().startRoute("foo");

        mock.expectedBodiesReceived(TEST_MESSAGE);

        template.sendBody("direct:input", TEST_MESSAGE);

        mock.assertIsSatisfied();
    }

    @Test
    public void testRestartServer() throws Exception {
        MockEndpoint mock = getMockEndpoint("mock:restart-result");
        mock.expectedBodiesReceived(TEST_CONNECTED_MESSAGE);

        mock.assertIsSatisfied();

        LOG.info("Restarting Test Server");
        stopTestServer();
        startTestServer();

        resetMocks();

        mock.expectedBodiesReceived(TEST_CONNECTED_MESSAGE);

        mock.assertIsSatisfied();
    }

    @Override
    protected RouteBuilder[] createRouteBuilders() {
        RouteBuilder[] rbs = new RouteBuilder[3];
        rbs[0] = new RouteBuilder() {
            public void configure() {
                from("direct:input").routeId("foo")
                        .to("ahc-ws://localhost:" + PORT);
            }
        };
        rbs[1] = new RouteBuilder() {
            public void configure() {
                from("ahc-ws://localhost:" + PORT).routeId("bar")
                        .to("mock:result");
            }
        };
        rbs[2] = new RouteBuilder() {
            public void configure() {
                from("ahc-ws://localhost:" + PORT + "/restart").routeId("restart")
                        .to("mock:restart-result");
            }
        };
        return rbs;
    }
}

@@ -4852,32 +4852,54 @@
 rtIsSatisfied();
+%0A        resetMocks();
 %0A%0A        LOG.in
@@ -4982,39 +4982,16 @@
 ver();%0A%0A
-        resetMocks();%0A%0A
         

Fix regression in WsProducerConsumerTest (#7448)