This repository has been archived by the owner on Nov 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Example.java
executable file
·297 lines (211 loc) · 9.9 KB
/
Example.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/**
* Copyright 2012 Comcast Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import org.apache.log4j.Logger;
import org.json.JSONObject;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.sns.AmazonSNSClient;
import com.amazonaws.services.sns.model.CreateTopicRequest;
import com.amazonaws.services.sns.model.CreateTopicResult;
import com.amazonaws.services.sns.model.DeleteTopicRequest;
import com.amazonaws.services.sns.model.PublishRequest;
import com.amazonaws.services.sns.model.SubscribeRequest;
import com.amazonaws.services.sns.model.SubscribeResult;
import com.amazonaws.services.sqs.AmazonSQSClient;
import com.amazonaws.services.sqs.model.AddPermissionRequest;
import com.amazonaws.services.sqs.model.CreateQueueRequest;
import com.amazonaws.services.sqs.model.DeleteMessageRequest;
import com.amazonaws.services.sqs.model.DeleteQueueRequest;
import com.amazonaws.services.sqs.model.Message;
import com.amazonaws.services.sqs.model.ReceiveMessageRequest;
import com.amazonaws.services.sqs.model.ReceiveMessageResult;
import com.comcast.cmb.common.util.Util;
import com.comcast.cmb.test.tools.CMBTestingConstants;
public class Example {
private static Logger logger = Logger.getLogger(Example.class);
private final static String QUEUE_PREFIX = "TSTQ_";
public static String getArnForQueueUrl(String url) {
if (url == null) {
return null;
}
String elements[] = url.split("/");
if (elements.length != 5) {
return null;
}
String arn = "arn:cmb:cqs:" + "ccp" + ":" + elements[3] + ":" + elements[4];
return arn;
}
public static String httpGet(String url) throws IOException {
URL confirmationEndpoint = new URL(url);
URLConnection conn = confirmationEndpoint.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
String response = "";
while ((inputLine = in.readLine()) != null) {
response += inputLine;
}
logger.info(response);
in.close();
return response;
}
public static void main(String [ ] args) {
try {
Util.initLog4jTest();
//TODO: set user id and credentials for two distinct users
// user "cqs_test_1"
BasicAWSCredentials user1Credentials = new BasicAWSCredentials("<access_key>", "<secret_key>");
// user "cqs_test_2"
//String user2Id = "<user_id>";
String user2Id = "389653920093";
BasicAWSCredentials user2Credentials = new BasicAWSCredentials("<access_key>", "<secret_key>");
// service urls
//TODO: add service URLs
//String cqsServerUrl = "http://<host>:<port>";
//String cnsServerUrl = "http://<host>:<port>";
String cqsServerUrl = "http://localhost:6059";
String cnsServerUrl = "http://localhost:6061";
// initialize service
AmazonSQSClient sqs = new AmazonSQSClient(user1Credentials);
sqs.setEndpoint(cqsServerUrl);
AmazonSNSClient sns = new AmazonSNSClient(user2Credentials);
sns.setEndpoint(cnsServerUrl);
// create queue
Random randomGenerator = new Random();
String queueName = QUEUE_PREFIX + randomGenerator.nextLong();
HashMap<String, String> attributeParams = new HashMap<String, String>();
CreateQueueRequest createQueueRequest = new CreateQueueRequest(queueName);
createQueueRequest.setAttributes(attributeParams);
String queueUrl = sqs.createQueue(createQueueRequest).getQueueUrl();
AddPermissionRequest addPermissionRequest = new AddPermissionRequest();
addPermissionRequest.setQueueUrl(queueUrl);
addPermissionRequest.setActions(Arrays.asList("SendMessage"));
addPermissionRequest.setLabel(UUID.randomUUID().toString());
addPermissionRequest.setAWSAccountIds(Arrays.asList(user2Id));
sqs.addPermission(addPermissionRequest);
// create topic
String topicName = "TSTT" + randomGenerator.nextLong();
CreateTopicRequest createTopicRequest = new CreateTopicRequest(topicName);
CreateTopicResult createTopicResult = sns.createTopic(createTopicRequest);
String topicArn = createTopicResult.getTopicArn();
// subscribe and confirm cqs endpoint
SubscribeRequest subscribeRequest = new SubscribeRequest();
String queueArn = getArnForQueueUrl(queueUrl);
subscribeRequest.setEndpoint(queueArn);
subscribeRequest.setProtocol("cqs");
subscribeRequest.setTopicArn(topicArn);
SubscribeResult subscribeResult = sns.subscribe(subscribeRequest);
String subscriptionArn = subscribeResult.getSubscriptionArn();
if (subscriptionArn.equals("pending confirmation")) {
Thread.sleep(500);
ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest();
receiveMessageRequest.setQueueUrl(queueUrl);
receiveMessageRequest.setMaxNumberOfMessages(1);
ReceiveMessageResult receiveMessageResult = sqs.receiveMessage(receiveMessageRequest);
List<Message> messages = receiveMessageResult.getMessages();
if (messages != null && messages.size() == 1) {
JSONObject o = new JSONObject(messages.get(0).getBody());
if (!o.has("SubscribeURL")) {
throw new Exception("message is not a confirmation messsage");
}
String subscriptionUrl = o.getString("SubscribeURL");
httpGet(subscriptionUrl);
DeleteMessageRequest deleteMessageRequest = new DeleteMessageRequest();
deleteMessageRequest.setReceiptHandle(messages.get(0).getReceiptHandle());
deleteMessageRequest.setQueueUrl(queueUrl);
sqs.deleteMessage(deleteMessageRequest);
} else {
throw new Exception("no confirmation message found");
}
}
// publish and receive message
PublishRequest publishRequest = new PublishRequest();
String messageText = "quamvis sint sub aqua, sub aqua maledicere temptant";
publishRequest.setMessage(messageText);
publishRequest.setSubject("unit test message");
publishRequest.setTopicArn(topicArn);
sns.publish(publishRequest);
Thread.sleep(500);
ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest();
receiveMessageRequest.setQueueUrl(queueUrl);
receiveMessageRequest.setMaxNumberOfMessages(1);
ReceiveMessageResult receiveMessageResult = sqs.receiveMessage(receiveMessageRequest);
List<Message> messages = receiveMessageResult.getMessages();
if (messages != null && messages.size() == 1) {
String messageBody = messages.get(0).getBody();
if (!messageBody.contains(messageText)) {
throw new Exception("message text not found");
}
DeleteMessageRequest deleteMessageRequest = new DeleteMessageRequest();
deleteMessageRequest.setReceiptHandle(messages.get(0).getReceiptHandle());
deleteMessageRequest.setQueueUrl(queueUrl);
sqs.deleteMessage(deleteMessageRequest);
} else {
throw new Exception("no messages found");
}
// subscribe and confirm http endpoint
String id = randomGenerator.nextLong() + "";
String endPointUrl = CMBTestingConstants.HTTP_ENDPOINT_BASE_URL + "recv/" + id;
String lastMessageUrl = CMBTestingConstants.HTTP_ENDPOINT_BASE_URL + "info/" + id + "?showLast=true";
subscribeRequest = new SubscribeRequest();
subscribeRequest.setEndpoint(endPointUrl);
subscribeRequest.setProtocol("http");
subscribeRequest.setTopicArn(topicArn);
subscribeResult = sns.subscribe(subscribeRequest);
subscriptionArn = subscribeResult.getSubscriptionArn();
if (subscriptionArn.equals("pending confirmation")) {
Thread.sleep(500);
String response = httpGet(lastMessageUrl);
JSONObject o = new JSONObject(response);
if (!o.has("SubscribeURL")) {
throw new Exception("message is not a confirmation messsage");
}
String subscriptionUrl = o.getString("SubscribeURL");
response = httpGet(subscriptionUrl);
}
// publish and receive message
publishRequest = new PublishRequest();
publishRequest.setMessage(messageText);
publishRequest.setSubject("unit test message");
publishRequest.setTopicArn(topicArn);
sns.publish(publishRequest);
Thread.sleep(500);
String response = httpGet(lastMessageUrl);
if (response != null && response.length() > 0) {
if (!response.contains(messageText)) {
throw new Exception("message text not found");
}
} else {
throw new Exception("no messages found");
}
// delete queue and topic
DeleteTopicRequest deleteTopicRequest = new DeleteTopicRequest(topicArn);
sns.deleteTopic(deleteTopicRequest);
sqs.deleteQueue(new DeleteQueueRequest(queueUrl));
System.out.println("OK");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}