/**
 * 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.activemq.usecases;

import java.net.URI;

import jakarta.jms.Connection;
import jakarta.jms.Destination;
import jakarta.jms.Session;
import jakarta.jms.Topic;
import jakarta.jms.TopicSubscriber;

import javax.management.ObjectName;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.network.DiscoveryNetworkConnector;
import org.apache.activemq.network.NetworkConnector;
import org.apache.activemq.network.NetworkTestSupport;
import org.apache.activemq.util.Wait;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.junit.experimental.categories.Category;
import org.apache.activemq.test.annotations.ParallelTest;

/**
 * Tests durable topic subscriptions inside a network of brokers.
 *
 *
 *
 */
@Category(ParallelTest.class)
public class DurableSubInBrokerNetworkTest extends NetworkTestSupport {

    private static final Logger LOG = LoggerFactory.getLogger(DurableSubInBrokerNetworkTest.class);
    // protected BrokerService localBroker;
    private final String subName = "Subscriber1";
    private final String subName2 = "Subscriber2";
    private final String topicName = "TEST.FOO";

    protected void setUp() throws Exception {
        useJmx=true;
        super.setUp();

        URI ncUri = new URI("static:(" + connector.getConnectUri().toString() + ")");
        NetworkConnector nc = new DiscoveryNetworkConnector(ncUri);
        nc.setDuplex(true);
        remoteBroker.addNetworkConnector(nc);
        nc.start();

        assertTrue("Network bridge did not establish in time",
                Wait.waitFor(() -> !nc.activeBridges().isEmpty(), 15000, 100));
    }

    protected void tearDown() throws Exception {
        if (remoteBroker.isStarted()) {
            remoteBroker.stop();
            remoteBroker.waitUntilStopped();
        }
        if (broker.isStarted()) {
            broker.stop();
            broker.waitUntilStopped();
        }
        super.tearDown();
    }


    /**
     * Creates a durable topic subscription, checks that it is propagated
     * in the broker network, removes the subscription and checks that
     * the subscription is removed from remote broker as well.
     *  
     * @throws Exception
     */
    public void testDurableSubNetwork() throws Exception {
        LOG.info("testDurableSubNetwork started.");

        // create durable sub
        ActiveMQConnectionFactory fact = new ActiveMQConnectionFactory(connector.getConnectUri().toString());
        Connection conn = fact.createConnection();
        conn.setClientID("clientID1");
        Session session = conn.createSession(false, 1);
        Destination dest = session.createTopic(topicName);
        TopicSubscriber sub = session.createDurableSubscriber((Topic)dest, subName);
        LOG.info("Durable subscription of name " + subName + "created.");

        // query durable sub on local and remote broker
        // raise an error if not found
        assertTrue("Durable subscription not found in local broker",
                Wait.waitFor(() -> foundSubInLocalBroker(subName), 15000, 100));
        assertTrue("Durable subscription not propagated to remote broker",
                Wait.waitFor(() -> foundSubInRemoteBrokerByTopicName(topicName), 15000, 100));

        // unsubscribe from durable sub
        sub.close();
        session.unsubscribe(subName);
        LOG.info("Unsubscribed from durable subscription.");

        // query durable sub on local and remote broker
        // raise an error if its not removed from both brokers
        assertTrue("Durable subscription not removed from local broker",
                Wait.waitFor(() -> !foundSubInLocalBroker(subName), 15000, 100));
        assertTrue("Durable subscription not unregistered on remote broker",
                Wait.waitFor(() -> !foundSubInRemoteBrokerByTopicName(topicName), 15000, 100));


    }

    public void testTwoDurableSubsInNetworkWithUnsubscribe() throws Exception{

        // create 1st durable sub to topic TEST.FOO
        ActiveMQConnectionFactory fact = new ActiveMQConnectionFactory(connector.getConnectUri().toString());
        Connection conn = fact.createConnection();
        conn.setClientID("clientID1");
        Session session = conn.createSession(false, 1);
        Destination dest = session.createTopic(topicName);
        TopicSubscriber sub = session.createDurableSubscriber((Topic)dest, subName);
        LOG.info("Durable subscription of name " + subName + "created.");
        TopicSubscriber sub2 = session.createDurableSubscriber((Topic) dest, subName2);
        LOG.info("Durable subscription of name " + subName2 + "created.");

        // query durable sub on local and remote broker
        // raise an error if not found
        assertTrue("Subscription1 not found in local broker",
                Wait.waitFor(() -> foundSubInLocalBroker(subName), 15000, 100));
        assertTrue("Subscription2 not found in local broker",
                Wait.waitFor(() -> foundSubInLocalBroker(subName2), 15000, 100));
        assertTrue("Durable subscription not propagated to remote broker",
                Wait.waitFor(() -> foundSubInRemoteBrokerByTopicName(topicName), 15000, 100));

        // unsubscribe from durable sub
        sub.close();
        session.unsubscribe(subName);
        LOG.info("Unsubscribed from durable subscription.");

        // query durable sub on local and remote broker
        assertTrue("Subscription1 not removed from local broker",
                Wait.waitFor(() -> !foundSubInLocalBroker(subName), 15000, 100));
        assertTrue("Subscription2 should still be in local broker",
                Wait.waitFor(() -> foundSubInLocalBroker(subName2), 15000, 100));
        assertTrue("Durable subscription should still be on remote broker",
                Wait.waitFor(() -> foundSubInRemoteBrokerByTopicName(topicName), 15000, 100));

        sub2.close();
        session.unsubscribe(subName2);

        assertTrue("Subscription2 not removed from local broker",
                Wait.waitFor(() -> !foundSubInLocalBroker(subName2), 15000, 100));
        assertTrue("Durable subscription not unregistered on remote broker",
                Wait.waitFor(() -> !foundSubInRemoteBrokerByTopicName(topicName), 15000, 100));

    }

    private boolean foundSubInRemoteBrokerByTopicName(String topicName) throws Exception {
        boolean foundSub = false;
        ObjectName[] subs = remoteBroker.getAdminView().getDurableTopicSubscribers();
        for (int i=0 ; i<subs.length; i++) {
            if (subs[i].toString().contains("destinationName=" + topicName))
                foundSub = true;
        }
        return foundSub;
    }

    private boolean foundSubInLocalBroker(String subName) throws Exception {
        boolean foundSub = false;
        ObjectName[] subs = broker.getAdminView().getDurableTopicSubscribers();

        for (int i=0 ; i<subs.length; i++) {
            if (subs[i].toString().contains(subName))
                foundSub = true;
        }
        return foundSub;
    }
}
