topicArn = getTopic(1, USR.USER1);
logger.info("Created topic " + topicArn + ", now setting attributes");
AddPermissionRequest addPermissionRequest = new AddPermissionRequest();
addPermissionRequest.setTopicArn(topicArn);
addPermissionRequest.setActionNames(Arrays.asList("SetTopicAttributes", "Publish"));
addPermissionRequest.setLabel("P1");
addPermissionRequest.setAWSAccountIds(Arrays.asList(user2.getUserId()));
cns1.addPermission(addPermissionRequest);
SetTopicAttributesRequest setTopicAttributesRequest = new SetTopicAttributesRequest();
setTopicAttributesRequest.setAttributeName("DisplayName");
setTopicAttributesRequest.setAttributeValue("NewDisplayName");
setTopicAttributesRequest.setTopicArn(topicArn);
cns2.setTopicAttributes(setTopicAttributesRequest);
GetTopicAttributesRequest getTopicAttributesRequest = new GetTopicAttributesRequest();
getTopicAttributesRequest.setTopicArn(topicArn);
GetTopicAttributesResult result = cns1.getTopicAttributes(getTopicAttributesRequest);
assertTrue("Expected display name NewDisplayName, instead found " + result.getAttributes().get("DisplayName"), result.getAttributes().get("DisplayName").equals("NewDisplayName"));
RemovePermissionRequest removePermissionRequest = new RemovePermissionRequest();
removePermissionRequest.setTopicArn(topicArn);
removePermissionRequest.setLabel("P1");
cns1.removePermission(removePermissionRequest);
logger.info("Now trying to do things without permission");
try {
setTopicAttributesRequest = new SetTopicAttributesRequest();
setTopicAttributesRequest.setAttributeName("DisplayName");
setTopicAttributesRequest.setAttributeValue("NewDisplayName2");
setTopicAttributesRequest.setTopicArn(topicArn);
cns2.setTopicAttributes(setTopicAttributesRequest);
fail("missing expected exception");
} catch (Exception ex) {
assertTrue(ex.getMessage().contains("don't have permission"));
}
// test publish permission
try {
PublishRequest publishRequest = new PublishRequest();
publishRequest.setMessage("hello world!!!");
publishRequest.setTopicArn(topicArn);
cns2.publish(publishRequest);
fail("missing expected exception");
} catch (Exception ex) {
assertTrue(ex.getMessage().contains("don't have permission"));
}
addPermissionRequest = new AddPermissionRequest();
addPermissionRequest.setTopicArn(topicArn);
addPermissionRequest.setActionNames(Arrays.asList("Publish"));
addPermissionRequest.setLabel("P2");
addPermissionRequest.setAWSAccountIds(Arrays.asList(user2.getUserId()));
cns1.addPermission(addPermissionRequest);
PublishRequest publishRequest = new PublishRequest();
publishRequest.setMessage("hello world!!!");
publishRequest.setTopicArn(topicArn);
cns2.publish(publishRequest);
removePermissionRequest = new RemovePermissionRequest();
removePermissionRequest.setTopicArn(topicArn);
removePermissionRequest.setLabel("P2");
cns1.removePermission(removePermissionRequest);
try {
publishRequest = new PublishRequest();
publishRequest.setMessage("hello world!!!");
publishRequest.setTopicArn(topicArn);
cns2.publish(publishRequest);
fail("missing expected exception");
} catch (Exception ex) {
assertTrue(ex.getMessage().contains("don't have permission"));
}
// test add permission
addPermissionRequest = new AddPermissionRequest();
addPermissionRequest.setTopicArn(topicArn);
addPermissionRequest.setActionNames(Arrays.asList("AddPermission"));
addPermissionRequest.setLabel("P3");
addPermissionRequest.setAWSAccountIds(Arrays.asList(user2.getUserId()));
cns1.addPermission(addPermissionRequest);
addPermissionRequest = new AddPermissionRequest();
addPermissionRequest.setTopicArn(topicArn);
addPermissionRequest.setActionNames(Arrays.asList("Publish"));
addPermissionRequest.setLabel("P4");
addPermissionRequest.setAWSAccountIds(Arrays.asList(user3.getUserId()));
cns2.addPermission(addPermissionRequest);
publishRequest = new PublishRequest();
publishRequest.setMessage("hello world!!!");
publishRequest.setTopicArn(topicArn);
cns3.publish(publishRequest);
// try some invalid stuff
try {
addPermissionRequest = new AddPermissionRequest();
addPermissionRequest.setTopicArn(topicArn);
addPermissionRequest.setActionNames(Arrays.asList("AddBoris"));
addPermissionRequest.setLabel("P5");
addPermissionRequest.setAWSAccountIds(Arrays.asList(user2.getUserId()));
cns1.addPermission(addPermissionRequest);
fail("missing expected exception");
} catch (Exception ex) {
assertTrue(ex.getMessage().contains("Invalid action parameter"));
}
try {
addPermissionRequest = new AddPermissionRequest();
addPermissionRequest.setTopicArn(topicArn);
addPermissionRequest.setActionNames(Arrays.asList(""));
addPermissionRequest.setLabel("P6");
addPermissionRequest.setAWSAccountIds(Arrays.asList(user2.getUserId()));
cns1.addPermission(addPermissionRequest);
fail("missing expected exception");
} catch (Exception ex) {
assertTrue(ex.getMessage().contains("Blank action parameter"));
}