]> git.somenet.org - pub/jan/dst18.git/blob - ass3-messaging/src/test/java/dst/ass3/messaging/impl/RequestGatewayTest.java
Make ElasticityControllerTest more tolernat to edge cases
[pub/jan/dst18.git] / ass3-messaging / src / test / java / dst / ass3 / messaging / impl / RequestGatewayTest.java
1 package dst.ass3.messaging.impl;
2
3 import static org.hamcrest.CoreMatchers.equalTo;
4 import static org.hamcrest.CoreMatchers.notNullValue;
5 import static org.hamcrest.CoreMatchers.nullValue;
6 import static org.junit.Assert.assertThat;
7
8 import java.util.concurrent.TimeUnit;
9
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Rule;
13 import org.junit.Test;
14 import org.junit.rules.Timeout;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17 import org.springframework.amqp.core.Message;
18
19 import dst.ass3.messaging.Constants;
20 import dst.ass3.messaging.IMessagingFactory;
21 import dst.ass3.messaging.IQueueManager;
22 import dst.ass3.messaging.IRequestGateway;
23 import dst.ass3.messaging.RabbitResource;
24 import dst.ass3.messaging.RequestType;
25 import dst.ass3.messaging.UploadRequest;
26
27 public class RequestGatewayTest {
28
29     private static final Logger LOG = LoggerFactory.getLogger(RequestGatewayTest.class);
30
31     @Rule
32     public RabbitResource rabbit = new RabbitResource();
33
34     @Rule
35     public Timeout timeout = new Timeout(10, TimeUnit.SECONDS);
36
37     private IMessagingFactory factory;
38     private IQueueManager queueManager;
39     private IRequestGateway requestGateway;
40
41     @Before
42     public void setUp() throws Exception {
43         factory = new MessagingFactory();
44         queueManager = factory.createQueueManager();
45         requestGateway = factory.createRequestGateway();
46
47         queueManager.setUp();
48     }
49
50     @After
51     public void tearDown() throws Exception {
52         queueManager.tearDown();
53
54         requestGateway.close();
55         queueManager.close();
56         factory.close();
57     }
58
59     @Test
60     public void uploadRequest_routesRequestsToCorrectQueues() throws Exception {
61         UploadRequest r1 = new UploadRequest("id1", RequestType.DOCUMENT, "urn:material:document:1");
62         UploadRequest r2 = new UploadRequest("id2", RequestType.DOCUMENT, "urn:material:document:2");
63         UploadRequest r3 = new UploadRequest("id3", RequestType.VIDEO, "urn:material:video:1");
64
65         LOG.info("Sending request {}", r1);
66         requestGateway.uploadRequest(r1);
67         LOG.info("Sending request {}", r2);
68         requestGateway.uploadRequest(r2);
69         LOG.info("Sending request {}", r3);
70         requestGateway.uploadRequest(r3);
71
72         LOG.info("Taking request from queue {}", Constants.QUEUE_DOCUMENT);
73         Message m1 = rabbit.getClient().receive(Constants.QUEUE_DOCUMENT, 1000);
74         assertThat(m1, notNullValue());
75
76         LOG.info("Taking request from queue {}", Constants.QUEUE_DOCUMENT);
77         Message m2 = rabbit.getClient().receive(Constants.QUEUE_DOCUMENT, 1000);
78         assertThat(m2, notNullValue());
79
80         LOG.info("Taking request from queue {}", Constants.QUEUE_VIDEO);
81         Message m3 = rabbit.getClient().receive(Constants.QUEUE_VIDEO, 1000);
82         assertThat(m3, notNullValue());
83
84         assertThat("Expected queue to be empty as no request for that type were issued",
85                 rabbit.getClient().receive(Constants.QUEUE_QUIZ, 1000), nullValue());
86     }
87
88     @Test
89     public void uploadRequest_serializesIntoJsonFormat() throws Exception {
90         UploadRequest r1 = new UploadRequest("id1", RequestType.QUIZ, "urn:material:quiz:1");
91         UploadRequest r2 = new UploadRequest("id2", RequestType.DOCUMENT, "urn:material:document:1");
92         UploadRequest r3 = new UploadRequest("id3", RequestType.VIDEO, "urn:material:video:1");
93
94         LOG.info("Sending request {}", r1);
95         requestGateway.uploadRequest(r1);
96         LOG.info("Sending request {}", r2);
97         requestGateway.uploadRequest(r2);
98         LOG.info("Sending request {}", r3);
99         requestGateway.uploadRequest(r3);
100
101         LOG.info("Taking request from queue {}", Constants.QUEUE_QUIZ);
102         Message m1 = rabbit.getClient().receive(Constants.QUEUE_QUIZ, 1000);
103         assertThat(m1, notNullValue());
104         assertThat(new String(m1.getBody()),
105                 equalTo("{\"id\":\"id1\",\"type\":\"QUIZ\",\"urn\":\"urn:material:quiz:1\"}"));
106
107         LOG.info("Taking request from queue {}", Constants.QUEUE_DOCUMENT);
108         Message m2 = rabbit.getClient().receive(Constants.QUEUE_DOCUMENT, 1000);
109         assertThat(m2, notNullValue());
110         assertThat(new String(m2.getBody()),
111                 equalTo("{\"id\":\"id2\",\"type\":\"DOCUMENT\",\"urn\":\"urn:material:document:1\"}"));
112
113         LOG.info("Taking request from queue {}", Constants.QUEUE_VIDEO);
114         Message m3 = rabbit.getClient().receive(Constants.QUEUE_VIDEO, 1000);
115         assertThat(m3, notNullValue());
116         assertThat(new String(m3.getBody()),
117                 equalTo("{\"id\":\"id3\",\"type\":\"VIDEO\",\"urn\":\"urn:material:video:1\"}"));
118     }
119 }