View Javadoc

1   package com.atlassian.messagequeue.internal.lifecycle;
2   
3   import com.google.common.base.Charsets;
4   import com.google.common.io.Resources;
5   import org.junit.Before;
6   import org.junit.Rule;
7   import org.junit.Test;
8   import org.mockito.junit.MockitoJUnit;
9   import org.mockito.junit.MockitoRule;
10  
11  import java.io.IOException;
12  import java.net.URL;
13  import java.util.Optional;
14  
15  import static org.hamcrest.Matchers.is;
16  import static org.junit.Assert.assertFalse;
17  import static org.junit.Assert.assertThat;
18  import static org.junit.Assert.assertTrue;
19  
20  public class JacksonNotificationDeserializerTest {
21      @Rule
22      public MockitoRule rule = MockitoJUnit.rule();
23  
24      private static final URL INSTANCE_TERMINATING_SNS_NOTIFICATION_URL = Resources.getResource("lifecycle-message-instance-terminating.json");
25      private static final URL INSTANCE_ACTIVE_SNS_NOTIFICATION_URL = Resources.getResource("lifecycle-message-instance-active.json");
26      private static final URL AWS_CLOUDFORMATION_SNS_NOTIFICATION_URL = Resources.getResource("aws-cloudformation.json");
27  
28      private NotificationDeserializer notificationDeserializer;
29  
30      @Before
31      public void setUp() throws Exception {
32          notificationDeserializer = new JacksonNotificationDeserializer();
33      }
34  
35      @Test
36      public void deserializeInstanceTerminatingLifecycleNotification() throws Exception {
37          final String notificationPayload = getSnsNotificationMessage(INSTANCE_TERMINATING_SNS_NOTIFICATION_URL);
38  
39          final Optional<InstanceLifecycleNotification> instanceLifecycleNotification =
40                  notificationDeserializer.deserialize(notificationPayload, InstanceLifecycleNotification.class);
41  
42          assertTrue(instanceLifecycleNotification.isPresent());
43          assertThat(instanceLifecycleNotification.get().getLifecycleTransition(), is("autoscaling:EC2_INSTANCE_TERMINATING"));
44          assertThat(instanceLifecycleNotification.get().getLifecycleHookName(), is("dloeng-amq-test-2-ddev-1-0-c8d0c20-2016-11-25-03-46-utc-q3tn3d2lqbfmbqoa-WebServerGroupLifecycleHook-1AYGW5SA0I9RR"));
45          assertThat(instanceLifecycleNotification.get().getEc2InstanceId(), is("i-0a86cd00c3193cc5c"));
46          assertThat(instanceLifecycleNotification.get().getAutoScalingGroupName(), is("dloeng-amq-test-2-ddev-1-0-c8d0c20-2016-11-25-03-46-utc-q3tn3d2lqbfmbqoa-WebServerGroup-BLUCTWKJ3LG0"));
47          assertThat(instanceLifecycleNotification.get().getLifecycleActionToken(), is("547cbed5-e4f7-4310-886b-a920b3814f1c"));
48      }
49  
50      @Test
51      public void deserializeInstanceActiveLifecycleNotification() throws Exception {
52          final String notificationPayload = getSnsNotificationMessage(INSTANCE_ACTIVE_SNS_NOTIFICATION_URL);
53  
54          final Optional<InstanceLifecycleNotification> instanceLifecycleNotification =
55                  notificationDeserializer.deserialize(notificationPayload, InstanceLifecycleNotification.class);
56  
57          assertTrue(instanceLifecycleNotification.isPresent());
58          assertThat(instanceLifecycleNotification.get().getLifecycleTransition(), is("micros:ACTIVE"));
59      }
60  
61      @Test
62      public void deserializeAwsCloudFormationNotification() throws Exception {
63          final String notificationPayload = getSnsNotificationMessage(AWS_CLOUDFORMATION_SNS_NOTIFICATION_URL);
64  
65          final Optional<InstanceLifecycleNotification> instanceLifecycleNotification =
66                  notificationDeserializer.deserialize(notificationPayload, InstanceLifecycleNotification.class);
67  
68          assertFalse(instanceLifecycleNotification.isPresent());
69      }
70  
71      @Test
72      public void deserializeNotificationWithInvalidJson() throws Exception {
73          final Optional<InstanceLifecycleNotification> instanceLifecycleNotification =
74                  notificationDeserializer.deserialize("foobar", InstanceLifecycleNotification.class);
75  
76          assertFalse(instanceLifecycleNotification.isPresent());
77      }
78  
79      @Test
80      public void deserializeEmptyNotification() throws Exception {
81          final Optional<InstanceLifecycleNotification> instanceLifecycleNotification =
82                  notificationDeserializer.deserialize("", InstanceLifecycleNotification.class);
83  
84          assertFalse(instanceLifecycleNotification.isPresent());
85      }
86  
87      @Test
88      public void deserializeNullNotification() throws Exception {
89          final Optional<InstanceLifecycleNotification> instanceLifecycleNotification =
90                  notificationDeserializer.deserialize(null, InstanceLifecycleNotification.class);
91  
92          assertFalse(instanceLifecycleNotification.isPresent());
93      }
94  
95      private String getSnsNotificationMessage(URL instanceTerminatingNotificationUrl) throws IOException {
96          return notificationDeserializer.deserialize(Resources.toString(instanceTerminatingNotificationUrl, Charsets.UTF_8), SNSNotification.class)
97                  .orElseThrow(() -> new AssertionError("Unexpected error encountered deserializing SNS notification"))
98                  .getMessage();
99      }
100 }