Search in sources :

Example 1 with ListSubscriptionsByTopicResult

use of com.amazonaws.services.sns.model.ListSubscriptionsByTopicResult in project Synapse-Stack-Builder by Sage-Bionetworks.

the class StackInstanceNotificationSetupTest method testSetupNotificationTopics.

@Test
public void testSetupNotificationTopics() {
    String topicArn = "arn:123";
    String protocol = Constants.TOPIC_SUBSCRIBE_PROTOCOL_EMAIL;
    String endpoint = config.getRDSAlertSubscriptionEndpoint();
    CreateTopicRequest expectedTopic = new CreateTopicRequest();
    expectedTopic.setName(config.getRDSAlertTopicName());
    CreateTopicResult expectedResult = new CreateTopicResult().withTopicArn(topicArn);
    when(mockClient.createTopic(expectedTopic)).thenReturn(expectedResult);
    ListSubscriptionsByTopicRequest tRequest = new ListSubscriptionsByTopicRequest().withTopicArn(topicArn);
    Subscription expected = new Subscription().withEndpoint(endpoint).withProtocol(protocol);
    ListSubscriptionsByTopicResult result = new ListSubscriptionsByTopicResult().withSubscriptions(expected);
    when(mockClient.listSubscriptionsByTopic(tRequest)).thenReturn(result);
    // Make the call
    setup.setupResources();
    verify(mockClient, times(1)).createTopic(expectedTopic);
    // Make sure it was set the resources
    assertEquals("The expected topic was not set in the resoruces", expectedResult.getTopicArn(), resources.getStackInstanceNotificationTopicArn());
}
Also used : CreateTopicResult(com.amazonaws.services.sns.model.CreateTopicResult) ListSubscriptionsByTopicResult(com.amazonaws.services.sns.model.ListSubscriptionsByTopicResult) ListSubscriptionsByTopicRequest(com.amazonaws.services.sns.model.ListSubscriptionsByTopicRequest) CreateTopicRequest(com.amazonaws.services.sns.model.CreateTopicRequest) Subscription(com.amazonaws.services.sns.model.Subscription) Test(org.junit.Test)

Example 2 with ListSubscriptionsByTopicResult

use of com.amazonaws.services.sns.model.ListSubscriptionsByTopicResult in project Synapse-Stack-Builder by Sage-Bionetworks.

the class NotificationUtilsTest method testCreateSubscriptionAlreadyExists.

@Test
public void testCreateSubscriptionAlreadyExists() {
    String topicArn = "arn:123";
    String protocol = "email";
    String endpoint = "testing@domain.com";
    ListSubscriptionsByTopicRequest tRequest = new ListSubscriptionsByTopicRequest().withTopicArn(topicArn);
    Subscription expected = new Subscription().withEndpoint(endpoint).withProtocol(protocol);
    ListSubscriptionsByTopicResult result = new ListSubscriptionsByTopicResult().withSubscriptions(expected);
    when(mockClient.listSubscriptionsByTopic(tRequest)).thenReturn(result);
    // This should not call create because it already exists
    SubscribeRequest expectedRequest = new SubscribeRequest();
    expectedRequest.setTopicArn(topicArn);
    expectedRequest.setProtocol(protocol);
    expectedRequest.setEndpoint(endpoint);
    Subscription sub = NotificationUtils.createSubScription(mockClient, topicArn, protocol, endpoint);
    assertEquals(expected, sub);
    // Subscribe should not have been called!
    verify(mockClient, times(0)).subscribe(expectedRequest);
}
Also used : SubscribeRequest(com.amazonaws.services.sns.model.SubscribeRequest) ListSubscriptionsByTopicResult(com.amazonaws.services.sns.model.ListSubscriptionsByTopicResult) ListSubscriptionsByTopicRequest(com.amazonaws.services.sns.model.ListSubscriptionsByTopicRequest) Subscription(com.amazonaws.services.sns.model.Subscription) Test(org.junit.Test)

Example 3 with ListSubscriptionsByTopicResult

use of com.amazonaws.services.sns.model.ListSubscriptionsByTopicResult in project Synapse-Stack-Builder by Sage-Bionetworks.

the class NotificationUtilsTest method testfindSubscriptionFound.

@Test
public void testfindSubscriptionFound() {
    String topicArn = "arn:123";
    String protocol = "email";
    String endpoint = "testing@domain.com";
    ListSubscriptionsByTopicRequest tRequest = new ListSubscriptionsByTopicRequest().withTopicArn(topicArn);
    Subscription expected = new Subscription().withEndpoint(endpoint).withProtocol(protocol);
    ListSubscriptionsByTopicResult result = new ListSubscriptionsByTopicResult().withSubscriptions(expected);
    when(mockClient.listSubscriptionsByTopic(tRequest)).thenReturn(result);
    // For this case it should not be found
    Subscription sub = NotificationUtils.findSubscription(mockClient, topicArn, protocol, endpoint);
    assertEquals(expected, sub);
}
Also used : ListSubscriptionsByTopicResult(com.amazonaws.services.sns.model.ListSubscriptionsByTopicResult) ListSubscriptionsByTopicRequest(com.amazonaws.services.sns.model.ListSubscriptionsByTopicRequest) Subscription(com.amazonaws.services.sns.model.Subscription) Test(org.junit.Test)

Example 4 with ListSubscriptionsByTopicResult

use of com.amazonaws.services.sns.model.ListSubscriptionsByTopicResult in project Synapse-Stack-Builder by Sage-Bionetworks.

the class BuildStackMainTest method before.

@Before
public void before() throws IOException {
    inputProps = TestHelper.createInputProperties("dev");
    InputConfiguration config = TestHelper.createTestConfig("dev");
    defaultProps = TestHelper.createDefaultProperties();
    clientFactory = new MockAmazonClientFactory();
    AmazonS3Client mockS3Client = clientFactory.createS3Client();
    AmazonEC2Client mockEC2Client = clientFactory.createEC2Client();
    AmazonSNSClient mockSNSnsClient = clientFactory.createSNSClient();
    AmazonRDSClient mockRdsClient = clientFactory.createRDSClient();
    // Write the default properties.
    when(mockS3Client.getObject(any(GetObjectRequest.class), any(File.class))).thenAnswer(new Answer<ObjectMetadata>() {

        public ObjectMetadata answer(InvocationOnMock invocation) throws Throwable {
            // Write the property file
            File file = (File) invocation.getArguments()[1];
            FileWriter writer = new FileWriter(file);
            try {
                defaultProps.store(writer, "test generated");
            } finally {
                writer.close();
            }
            return new ObjectMetadata();
        }
    });
    // Return a valid EC2 security group.
    DescribeSecurityGroupsRequest dsgr = new DescribeSecurityGroupsRequest().withGroupNames(config.getElasticSecurityGroupName());
    when(mockEC2Client.describeSecurityGroups(dsgr)).thenReturn(new DescribeSecurityGroupsResult().withSecurityGroups(new SecurityGroup().withGroupName(config.getElasticSecurityGroupName())));
    // Return a valid topic
    String topicArn = "some:arn";
    when(mockSNSnsClient.createTopic(new CreateTopicRequest(config.getRDSAlertTopicName()))).thenReturn(new CreateTopicResult().withTopicArn(topicArn));
    when(mockSNSnsClient.listSubscriptionsByTopic(new ListSubscriptionsByTopicRequest(topicArn))).thenReturn(new ListSubscriptionsByTopicResult().withSubscriptions(new Subscription()));
    // return a valid group
    when(mockRdsClient.describeDBParameterGroups(new DescribeDBParameterGroupsRequest().withDBParameterGroupName(config.getDatabaseParameterGroupName()))).thenReturn(new DescribeDBParameterGroupsResult().withDBParameterGroups(new DBParameterGroup().withDBParameterGroupName(config.getDatabaseParameterGroupName())));
    when(mockRdsClient.describeDBParameters(new DescribeDBParametersRequest().withDBParameterGroupName(config.getDatabaseParameterGroupName()))).thenReturn(new DescribeDBParametersResult().withParameters(new Parameter().withParameterName(Constants.DB_PARAM_KEY_SLOW_QUERY_LOG)).withParameters(new Parameter().withParameterName(Constants.DB_PARAM_KEY_LONG_QUERY_TIME)));
}
Also used : DescribeSecurityGroupsRequest(com.amazonaws.services.ec2.model.DescribeSecurityGroupsRequest) DescribeDBParameterGroupsResult(com.amazonaws.services.rds.model.DescribeDBParameterGroupsResult) FileWriter(java.io.FileWriter) AmazonSNSClient(com.amazonaws.services.sns.AmazonSNSClient) CreateTopicResult(com.amazonaws.services.sns.model.CreateTopicResult) DescribeDBParametersRequest(com.amazonaws.services.rds.model.DescribeDBParametersRequest) DescribeDBParameterGroupsRequest(com.amazonaws.services.rds.model.DescribeDBParameterGroupsRequest) DescribeSecurityGroupsResult(com.amazonaws.services.ec2.model.DescribeSecurityGroupsResult) Subscription(com.amazonaws.services.sns.model.Subscription) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) DBParameterGroup(com.amazonaws.services.rds.model.DBParameterGroup) ListSubscriptionsByTopicRequest(com.amazonaws.services.sns.model.ListSubscriptionsByTopicRequest) DescribeDBParametersResult(com.amazonaws.services.rds.model.DescribeDBParametersResult) CreateTopicRequest(com.amazonaws.services.sns.model.CreateTopicRequest) MockAmazonClientFactory(org.sagebionetworks.factory.MockAmazonClientFactory) AmazonRDSClient(com.amazonaws.services.rds.AmazonRDSClient) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup) InputConfiguration(org.sagebionetworks.stack.config.InputConfiguration) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ListSubscriptionsByTopicResult(com.amazonaws.services.sns.model.ListSubscriptionsByTopicResult) Parameter(com.amazonaws.services.rds.model.Parameter) File(java.io.File) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) Before(org.junit.Before)

Example 5 with ListSubscriptionsByTopicResult

use of com.amazonaws.services.sns.model.ListSubscriptionsByTopicResult in project Synapse-Stack-Builder by Sage-Bionetworks.

the class NotificationUtils method findSubscription.

/**
 * This is a pain-in-the-butt way to determine if a subscription already exists
 * @param topicArn
 * @param protocol
 * @param endpoint
 * @return
 */
public static Subscription findSubscription(AmazonSNSClient client, String topicArn, String protocol, String endpoint) {
    // Fill this list with all of the pages
    List<Subscription> fullList = new LinkedList<Subscription>();
    ListSubscriptionsByTopicResult subList = client.listSubscriptionsByTopic(new ListSubscriptionsByTopicRequest().withTopicArn(topicArn));
    fullList.addAll(subList.getSubscriptions());
    while (subList.getNextToken() != null) {
        subList = client.listSubscriptionsByTopic(new ListSubscriptionsByTopicRequest().withTopicArn(topicArn));
        fullList.addAll(subList.getSubscriptions());
    }
    // Scan the full list for this results
    for (Subscription sub : fullList) {
        if (protocol.equals(sub.getProtocol()) && endpoint.equals(sub.getEndpoint())) {
            return sub;
        }
    }
    // Did not find it
    return null;
}
Also used : ListSubscriptionsByTopicResult(com.amazonaws.services.sns.model.ListSubscriptionsByTopicResult) ListSubscriptionsByTopicRequest(com.amazonaws.services.sns.model.ListSubscriptionsByTopicRequest) Subscription(com.amazonaws.services.sns.model.Subscription) LinkedList(java.util.LinkedList)

Aggregations

ListSubscriptionsByTopicRequest (com.amazonaws.services.sns.model.ListSubscriptionsByTopicRequest)8 ListSubscriptionsByTopicResult (com.amazonaws.services.sns.model.ListSubscriptionsByTopicResult)8 Subscription (com.amazonaws.services.sns.model.Subscription)8 Test (org.junit.Test)5 CreateTopicResult (com.amazonaws.services.sns.model.CreateTopicResult)3 CreateTopicRequest (com.amazonaws.services.sns.model.CreateTopicRequest)2 SubscribeRequest (com.amazonaws.services.sns.model.SubscribeRequest)2 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)1 DescribeSecurityGroupsRequest (com.amazonaws.services.ec2.model.DescribeSecurityGroupsRequest)1 DescribeSecurityGroupsResult (com.amazonaws.services.ec2.model.DescribeSecurityGroupsResult)1 SecurityGroup (com.amazonaws.services.ec2.model.SecurityGroup)1 AmazonRDSClient (com.amazonaws.services.rds.AmazonRDSClient)1 DBParameterGroup (com.amazonaws.services.rds.model.DBParameterGroup)1 DescribeDBParameterGroupsRequest (com.amazonaws.services.rds.model.DescribeDBParameterGroupsRequest)1 DescribeDBParameterGroupsResult (com.amazonaws.services.rds.model.DescribeDBParameterGroupsResult)1 DescribeDBParametersRequest (com.amazonaws.services.rds.model.DescribeDBParametersRequest)1 DescribeDBParametersResult (com.amazonaws.services.rds.model.DescribeDBParametersResult)1 Parameter (com.amazonaws.services.rds.model.Parameter)1 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)1 GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)1