package pl.ciruk.blog.jms;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import pl.ciruk.blog.mq.MQConfiguration;

import javax.inject.Inject;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import static org.awaitility.Awaitility.await;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {MockDestinationIntegrationTest.TestConfiguration.class})
@EnableAutoConfiguration
@EnableJms
public class MockDestinationIntegrationTest {
    @Inject
    private JmsTemplate jmsTemplate;

    @Inject
    private MockDestination<String> mockDestination;

    @Test
    public void shouldPrintTwoReceivedMessagesAndRespondToOne() throws Exception {
        mockDestination
                .whenReceived(messsage -> messsage.equals("Ala"))
                .thenSend("ResponseQueue", message -> message + " Ma kota");
        mockDestination
                .whenReceived(messsage -> messsage.startsWith("A"))
                .thenConsume(message -> System.out.println("Received: " + message));

        await().atLeast(2, TimeUnit.SECONDS);
        jmsTemplate.convertAndSend("RequestQueue", "Ala");

        await().atLeast(2, TimeUnit.SECONDS);
        jmsTemplate.convertAndSend("RequestQueue", "Arka");

        System.out.println(jmsTemplate.receiveAndConvert("ResponseQueue"));
    }

    @Configuration
    static class TestConfiguration extends MQConfiguration {
        List<String> receivedMessages = new CopyOnWriteArrayList<>();

        @Bean
        public Gateway<String> mockDestination(JmsTemplate jmsTemplate) {
            return new MockDestination<>(jmsTemplate, Function.identity());
        }
    }

}

@@ -559,81 +559,8 @@
 ct;%0A
-import java.util.List;%0Aimport java.util.concurrent.CopyOnWriteArrayList;%0A
 impo
@@ -634,56 +634,8 @@
 n;%0A%0A
-import static org.awaitility.Awaitility.await;%0A%0A
 @Run
@@ -825,16 +825,200 @@
 nTest %7B%0A
+    private static final String FIRST_MESSAGE = %22Alice%22;%0A    private static final String SECOND_MESSAGE = %22Another%22;%0A    private static final String RESPONSE_QUEUE = %22ResponseQueue%22;%0A%0A
     @Inj
@@ -1268,33 +1268,32 @@
 henReceived(mess
-s
 age -%3E messsage.
@@ -1283,25 +1283,24 @@
 sage -%3E mess
-s
 age.equals(%22
@@ -1298,21 +1298,29 @@
 .equals(
-%22Ala%22
+FIRST_MESSAGE
 ))%0A     
@@ -1340,31 +1340,30 @@
 henSend(
-%22ResponseQueue%22
+RESPONSE_QUEUE
 , messag
@@ -1383,15 +1383,20 @@
 + %22 
-Ma kota
+acknowledged
 %22);%0A
@@ -1453,17 +1453,16 @@
 ved(mess
-s
 age -%3E m
@@ -1464,17 +1464,16 @@
  -%3E mess
-s
 age.star
@@ -1583,54 +1583,68 @@
     
-await().atLeast(2, TimeUnit.SECONDS
+send(FIRST_MESSAGE);%0A        send(SECOND_MESSAGE
 );%0A
+%0A
         
 jmsT
@@ -1643,59 +1643,126 @@
     
-jmsTemplate.convertAndSend(%22RequestQueue%22, %22Ala%22);%0A
+System.out.println(jmsTemplate.receiveAndConvert(RESPONSE_QUEUE));%0A    %7D%0A%0A    private void send(String firstMessage) %7B
 %0A   
@@ -1770,43 +1770,20 @@
     
-a
 wait
-().atLeast(2, TimeUnit.SECONDS
+AMoment(
 );%0A 
@@ -1836,93 +1836,202 @@
 e%22, 
-%22Arka%22
+firstMessage
 );%0A
-%0A
     
+%7D%0A%0A
     
-System.out.println(jmsTemplate.receiveAndConvert(%22ResponseQueue%22));
+private void waitAMoment() %7B%0A        try %7B%0A            TimeUnit.SECONDS.sleep(1);%0A        %7D catch (InterruptedException e) %7B%0A            Thread.interrupted();%0A        %7D
 %0A   
@@ -2118,79 +2118,8 @@
 n %7B%0A
-        List%3CString%3E receivedMessages = new CopyOnWriteArrayList%3C%3E();%0A%0A
     
@@ -2136,23 +2136,16 @@
         
-public 
 Gateway%3C

Clean out mock destination example