1 package com.atlassian.messagequeue.internal.sqs.yaml;
2
3 import com.atlassian.messagequeue.MessageRunnerKey;
4 import com.atlassian.messagequeue.internal.sqs.SQSConfig;
5 import org.junit.Rule;
6 import org.junit.Test;
7 import org.junit.rules.ExpectedException;
8
9 import java.io.BufferedReader;
10 import java.io.InputStreamReader;
11 import java.util.function.Predicate;
12 import java.util.stream.Collectors;
13 import java.util.stream.IntStream;
14
15 import static org.hamcrest.Matchers.contains;
16 import static org.hamcrest.Matchers.containsInAnyOrder;
17 import static org.hamcrest.Matchers.containsString;
18 import static org.hamcrest.Matchers.equalTo;
19 import static org.junit.Assert.assertThat;
20
21 public class SQSYamlConfigParserTest {
22
23 @Rule
24 public ExpectedException expectedException = ExpectedException.none();
25
26 @Test
27 public void testValidConfig() throws Exception {
28 final SQSConfig config = parse("yaml/config.yml");
29 assertThat(config.getDefaultQueue().getQueueName(), equalTo("queue1"));
30
31 assertThat(config.getOutboundQueueNameMappings().keySet(),
32 containsInAnyOrder(IntStream.range(1, 6).mapToObj(i -> MessageRunnerKey.of("messagekey.MessageRunnerKey" + i)).toArray(MessageRunnerKey[]::new)));
33
34 assertThat(config.getInboundQueueMappings().keySet(), contains("workerGroup1", "workerGroup2"));
35 }
36
37 @Test
38 public void testNoQueueInConfig() throws Exception {
39 expectException("Invalid SQS config: there must be at least 1 queue");
40
41 parse("yaml/no-queue-config.yml");
42 }
43
44 @Test
45 public void testInvalidQueueNameInConfig() throws Exception {
46 expectException("Invalid SQS queue name: invalid");
47
48 parse("yaml/invalid-queue-name-config.yml", queueName -> !queueName.equals("invalid"));
49 }
50
51 @Test
52 public void testNoDefaultQueueConfig() throws Exception {
53 expectException("Invalid SQS config: there must be exactly 1 default queue");
54
55 parse("yaml/no-default-queue-config.yml");
56 }
57
58 @Test
59 public void testMoreThanOneDefaultQueueConfig() throws Exception {
60 expectException("Invalid SQS config: there must be exactly 1 default queue");
61
62 parse("yaml/more-than-one-default-queues-config.yml");
63 }
64
65 @Test
66 public void testNoWorkerGroupConfig() throws Exception {
67 expectException("Invalid SQS config: there must be at least 1 worker group");
68
69 parse("yaml/no-worker-group-config.yml");
70 }
71
72 @Test
73 public void testNoCorePoolSizeConfig() throws Exception {
74 expectException("Invalid SQS consumer queue config: corePoolSize must be greater than 0");
75
76 parse("yaml/no-core-pool-size-config.yml");
77 }
78
79 @Test
80 public void testNoMaxPoolSizeConfig() throws Exception {
81 expectException("Invalid SQS consumer queue config: maxPoolSize must be greater than 0");
82
83 parse("yaml/no-max-pool-size-config.yml");
84 }
85
86 @Test
87 public void testNoTimeoutConfig() throws Exception {
88 expectException("Invalid SQS consumer queue config: visibilityTimeoutExtensionPeriod must be greater than 0");
89
90 parse("yaml/no-timeout-config.yml");
91 }
92
93 @Test
94 public void testQueueNotBelongToWorkerGroupConfig() throws Exception {
95 expectException("Invalid SQS config: queues(queue3) must belong to at least 1 worker group");
96
97 parse("yaml/queue-not-belong-to-worker-group-config.yml");
98 }
99
100 @Test
101 public void testQueueNotBelongToWorkerGroupConfig2() throws Exception {
102 expectException("Invalid SQS config: queues(queue3, queue4) must belong to at least 1 worker group");
103
104 parse("yaml/queue-not-belong-to-worker-group-config-2.yml");
105 }
106
107
108 private SQSConfig parse(String configFile) {
109 return parse(configFile, x -> true);
110
111 }
112
113 private SQSConfig parse(String configFile, Predicate<String> queueNameVadilator) {
114 try (BufferedReader buffer = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(configFile)))) {
115 final String content = buffer.lines().collect(Collectors.joining("\n"));
116 final SQSYamlConfigParser sqsYamlConfigParser = new SQSYamlConfigParser();
117 return sqsYamlConfigParser.parse(() -> content, queueNameVadilator, x -> x);
118 } catch (Exception e) {
119 throw new RuntimeException(e);
120 }
121 }
122
123 private void expectException(String message) {
124 expectedException.expect(RuntimeException.class);
125 expectedException.expectMessage(containsString(message));
126 }
127 }