]> git.somenet.org - pub/jan/dst18.git/blob - ass3-messaging/src/test/java/dst/ass3/messaging/impl/QueueManagerTest.java
GITOLITE.txt
[pub/jan/dst18.git] / ass3-messaging / src / test / java / dst / ass3 / messaging / impl / QueueManagerTest.java
1 package dst.ass3.messaging.impl;
2
3 import static dst.ass3.messaging.Constants.RMQ_VHOST;
4 import static dst.ass3.messaging.Constants.TOPIC_EXCHANGE;
5 import static dst.ass3.messaging.Constants.WORK_QUEUES;
6 import static org.hamcrest.CoreMatchers.*;
7 import static org.junit.Assert.assertThat;
8
9 import java.io.IOException;
10 import java.util.Arrays;
11 import java.util.List;
12 import java.util.concurrent.TimeUnit;
13 import java.util.stream.Collectors;
14
15 import org.junit.After;
16 import org.junit.Before;
17 import org.junit.Rule;
18 import org.junit.Test;
19 import org.junit.rules.Timeout;
20 import org.springframework.amqp.core.Exchange;
21 import org.springframework.amqp.core.Queue;
22
23 import dst.ass3.messaging.IMessagingFactory;
24 import dst.ass3.messaging.IQueueManager;
25 import dst.ass3.messaging.RabbitResource;
26
27 public class QueueManagerTest {
28
29     @Rule
30     public RabbitResource rabbit = new RabbitResource();
31
32     @Rule
33     public Timeout timeout = new Timeout(10, TimeUnit.SECONDS);
34
35     private IMessagingFactory factory = new MessagingFactory();
36     private IQueueManager queueManager;
37
38     @Before
39     public void setUp() throws Exception {
40         factory = new MessagingFactory();
41         queueManager = factory.createQueueManager();
42     }
43
44     @After
45     public void tearDown() throws Exception {
46         try {
47             queueManager.close();
48         } catch (IOException e) {
49             // ignore
50         }
51     }
52
53     @Test
54     public void setUp_createsQueues() throws Exception {
55         queueManager.setUp();
56
57         try {
58             List<Queue> queues = rabbit.getManager().getQueues();
59             assertThat(queues.size(), not(0));
60
61             // make sure all work queues exist
62             List<String> queueNames = queues.stream().map(Queue::getName).collect(Collectors.toList());
63             Arrays.stream(WORK_QUEUES)
64                     .forEach(wq -> assertThat(queueNames, hasItem(wq)));
65         } finally {
66             queueManager.tearDown();
67         }
68     }
69
70     @Test
71     public void setUp_createsExchange() throws Exception {
72         queueManager.setUp();
73         try {
74             Exchange exchange = rabbit.getManager().getExchange(RMQ_VHOST, TOPIC_EXCHANGE);
75             assertThat(exchange, notNullValue());
76         } finally {
77             queueManager.tearDown();
78         }
79     }
80
81     @Test
82     public void tearDown_removesQueues() throws Exception {
83         queueManager.setUp();
84         queueManager.tearDown();
85         List<Queue> queues = rabbit.getManager().getQueues();
86         List<String> queueNames = queues.stream().map(Queue::getName).collect(Collectors.toList());
87         Arrays.stream(WORK_QUEUES)
88                 .forEach(wq -> assertThat(queueNames, not(hasItem(wq))));
89     }
90
91     @Test
92     public void tearDown_removesExchange() throws Exception {
93         queueManager.setUp();
94         queueManager.tearDown();
95         Exchange exchange = rabbit.getManager().getExchange(RMQ_VHOST, TOPIC_EXCHANGE);
96         assertThat(exchange, nullValue());
97     }
98 }