package io.syndesis.qe.utils;

import org.apache.activemq.ActiveMQConnectionFactory;

import cz.xtf.jms.JmsClient;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.LocalPortForward;
import lombok.extern.slf4j.Slf4j;
import org.apache.qpid.jms.JmsConnectionFactory;

@Slf4j
public class JmsClientManager implements AutoCloseable {

    private String jmsUrl;
    private int jmsPort;
    private String jmsPodName = "broker-amq";
    private String jmsUser = "amq";
    private String jmsPass = "topSecret";
    private String protocol;
    private LocalPortForward jmsLocalPortForward = null;
    private JmsClient jmsClient;

    public JmsClientManager(String protocol) {
        initValues(protocol);
    }

    private void initValues(String protocol) {
        this.protocol = protocol;
        switch (protocol) {
            case "tcp":
            case "openwire":
                jmsPort = 61616;
                jmsUrl = "tcp://127.0.0.1:" + jmsPort;
                break;
            case "amqp":
                jmsPort = 5672;
                jmsUrl = "amqp://127.0.0.1:" + jmsPort;
                break;
        }
    }

    public JmsClient getClient() {

        if (jmsLocalPortForward == null || !jmsLocalPortForward.isAlive()) {
            //can be the same pod twice forwarded to different ports? YES
            Pod  pod = OpenShiftUtils.xtf().getAnyPod("app", jmsPodName);
            log.info("POD NAME: *{}*", pod.getMetadata().getName());
            jmsLocalPortForward = OpenShiftUtils.portForward(pod, jmsPort, jmsPort);
        }
        return this.initClient();
    }

    public void close() {
        if (jmsClient != null) {
            jmsClient.disconnect();
        }
        TestUtils.terminateLocalPortForward(jmsLocalPortForward);
    }

    private JmsClient initClient() {
        if (jmsClient == null) {
            switch (protocol) {
                case "openwire":
                    ActiveMQConnectionFactory jmsFactory = new ActiveMQConnectionFactory();
                    jmsFactory.setBrokerURL(jmsUrl);
                    jmsFactory.setUserName(jmsUser);
                    jmsFactory.setPassword(jmsPass);
                    jmsClient = new JmsClient(jmsFactory);
                case "amqp":
                    JmsConnectionFactory jmsConnectionFactory = new JmsConnectionFactory(jmsUser, jmsPass, jmsUrl);
                    jmsClient = new JmsClient(jmsConnectionFactory);
            }
        }
        return jmsClient;
    }
}

@@ -1364,17 +1364,16 @@
     Pod 
- 
 pod = Op
@@ -1421,16 +1421,16 @@
 dName);%0A
+
         
@@ -1717,24 +1717,54 @@
 sconnect();%0A
+            jmsClient = null;%0A
         %7D%0A  
@@ -1928,32 +1928,60 @@
 ch (protocol) %7B%0A
+                case %22tcp%22:%0A
                 
@@ -2299,32 +2299,59 @@
 nt(jmsFactory);%0A
+                    break;%0A
                 
@@ -2471,32 +2471,32 @@
 sPass, jmsUrl);%0A
-
                 
@@ -2540,32 +2540,59 @@
 ectionFactory);%0A
+                    break;%0A
             %7D%0A  

Fix switch construction in JmsManager (#471)