Skip to content

Commit

Permalink
refactor(amazon_sqs_subscriber): moved AmazonSQSSubscriberSpec (groov…
Browse files Browse the repository at this point in the history
…y) unit test to AmazonSQSSubscriberTest (java)
  • Loading branch information
aman-agrawal committed Jul 4, 2024
1 parent d97ff35 commit 82c9b04
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 92 deletions.
1 change: 1 addition & 0 deletions echo-pubsub-aws/echo-pubsub-aws.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ dependencies {
implementation "io.spinnaker.kork:kork-pubsub-aws"
implementation "com.netflix.spectator:spectator-api"
implementation "javax.validation:validation-api"
testImplementation "org.mockito:mockito-inline"
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.netflix.spinnaker.echo.pubsub.PubsubMessageHandler;
import com.netflix.spinnaker.echo.pubsub.model.PubsubSubscriber;
import com.netflix.spinnaker.echo.pubsub.utils.NodeIdentity;
import com.netflix.spinnaker.kork.annotations.VisibleForTesting;
import com.netflix.spinnaker.kork.aws.ARN;
import com.netflix.spinnaker.kork.pubsub.aws.PubSubUtils;
import java.io.IOException;
Expand Down Expand Up @@ -211,7 +212,8 @@ private void handleMessage(Message message) {
}
}

private String unmarshalMessageBody(String messageBody) {
@VisibleForTesting
String unmarshalMessageBody(String messageBody) {
String messagePayload = messageBody;
try {
NotificationMessageWrapper wrapper =
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Copyright 2018 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.spinnaker.echo.pubsub.aws;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

import com.amazonaws.services.sns.AmazonSNS;
import com.amazonaws.services.sqs.AmazonSQS;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.spectator.api.Registry;
import com.netflix.spinnaker.echo.config.AmazonPubsubProperties;
import com.netflix.spinnaker.echo.jackson.EchoObjectMapper;
import com.netflix.spinnaker.echo.pubsub.PubsubMessageHandler;
import com.netflix.spinnaker.kork.aws.ARN;
import java.io.IOException;
import java.util.Collections;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AmazonSQSSubscriberTest {

@Mock private AmazonSNS amazonSNS;

@Mock private AmazonSQS amazonSQS;

@Mock private PubsubMessageHandler pubsubMessageHandler;

@Mock private Registry registry;

private ARN queueARN = new ARN("arn:aws:sqs:us-west-2:100:queueName");
private ARN topicARN = new ARN("arn:aws:sns:us-west-2:100:topicName");

private AmazonPubsubProperties.AmazonPubsubSubscription subscription =
new AmazonPubsubProperties.AmazonPubsubSubscription(
"aws_events", topicARN.getArn(), queueARN.getArn(), "", null, null, 3600);

private ObjectMapper objectMapper = EchoObjectMapper.getInstance();

private SQSSubscriber subject;

@BeforeEach
void setUp() throws Exception {
MockitoAnnotations.openMocks(this);
subject =
new SQSSubscriber(
objectMapper,
subscription,
pubsubMessageHandler,
amazonSNS,
amazonSQS,
() -> true,
registry);
}

@Test
void shouldUnmarshalSNSNotificationMessage() throws JsonProcessingException {
// Given
String payload =
"{\n"
+ " \"Records\": [\n"
+ " {\n"
+ " \"eventVersion\": \"2.0\",\n"
+ " \"eventSource\": \"aws:s3\",\n"
+ " \"awsRegion\": \"us-west-2\",\n"
+ " \"eventName\": \"ObjectCreated:Put\",\n"
+ " \"s3\": {\n"
+ " \"s3SchemaVersion\": \"1.0\",\n"
+ " \"configurationId\": \"prestaging_front50_events\",\n"
+ " \"bucket\": {\n"
+ " \"name\": \"us-west-2.spinnaker-prod\",\n"
+ " \"ownerIdentity\": {\n"
+ " \"principalId\": \"A2TW6LBRCW9VEM\"\n"
+ " },\n"
+ " \"arn\": \"arn:aws:s3:::us-west-2.spinnaker-prod\"\n"
+ " },\n"
+ " \"object\": {\n"
+ " \"key\": \"prestaging/front50/pipelines/31ef9c67-1d67-474f-a653-ac4b94c90817/pipeline-metadata.json\",\n"
+ " \"versionId\": \"8eyu4_RfV8EUqTnClhkKfLK5V4El_mIW\"\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ " ]\n"
+ "}";

NotificationMessageWrapper notificationMessage =
new NotificationMessageWrapper(
"Notification",
"4444-ffff",
"arn:aws:sns:us-west-2:100:topicName",
"Amazon S3 Notification",
payload,
Collections.emptyMap());

String snsMessage = objectMapper.writeValueAsString(notificationMessage);

try (MockedStatic<LoggerFactory> mockedLoggerFactory =
Mockito.mockStatic(LoggerFactory.class)) {
Logger mockLogger = mock(Logger.class);
mockedLoggerFactory
.when(() -> LoggerFactory.getLogger(SQSSubscriber.class))
.thenReturn(mockLogger);

// When
String result = subject.unmarshalMessageBody(snsMessage);

// Then
verify(mockLogger, never()).error(anyString(), anyString(), any(IOException.class));
assertEquals(payload, result);
}
}
}

0 comments on commit 82c9b04

Please sign in to comment.