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();