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")) {