1 package dst.ass3.messaging.impl;
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;
8 import java.util.concurrent.TimeUnit;
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;
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;
27 public class RequestGatewayTest {
29 private static final Logger LOG = LoggerFactory.getLogger(RequestGatewayTest.class);
32 public RabbitResource rabbit = new RabbitResource();
35 public Timeout timeout = new Timeout(10, TimeUnit.SECONDS);
37 private IMessagingFactory factory;
38 private IQueueManager queueManager;
39 private IRequestGateway requestGateway;
42 public void setUp() throws Exception {
43 factory = new MessagingFactory();
44 queueManager = factory.createQueueManager();
45 requestGateway = factory.createRequestGateway();
51 public void tearDown() throws Exception {
52 queueManager.tearDown();
54 requestGateway.close();
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");
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);
72 LOG.info("Taking request from queue {}", Constants.QUEUE_DOCUMENT);
73 Message m1 = rabbit.getClient().receive(Constants.QUEUE_DOCUMENT, 1000);
74 assertThat(m1, notNullValue());
76 LOG.info("Taking request from queue {}", Constants.QUEUE_DOCUMENT);
77 Message m2 = rabbit.getClient().receive(Constants.QUEUE_DOCUMENT, 1000);
78 assertThat(m2, notNullValue());
80 LOG.info("Taking request from queue {}", Constants.QUEUE_VIDEO);
81 Message m3 = rabbit.getClient().receive(Constants.QUEUE_VIDEO, 1000);
82 assertThat(m3, notNullValue());
84 assertThat("Expected queue to be empty as no request for that type were issued",
85 rabbit.getClient().receive(Constants.QUEUE_QUIZ, 1000), nullValue());
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");
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);
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\"}"));
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\"}"));
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\"}"));