From cfed368cf142147ea8a7fc1b063241c90e83e013 Mon Sep 17 00:00:00 2001 From: Solomon Ayalew <solxget@amazon.com> Date: Fri, 1 Sep 2023 20:24:58 +0000 Subject: [PATCH] Squashed commit of the following commit 86bcf15f Author: Solomon Ayalew <solxget@amazon.com> Date: Fri Sep 01 2023 11:40:39 GMT-0700 (Pacific Daylight Time) adding more testes --- .../impl/AwsPubsubRequestBodyExtractor.java | 11 ++- .../provider/aws/AwsAppPropertiesTest.java | 37 +++++++++ .../aws/AwsGoogleServiceAccountImplTest.java | 21 +++++ .../aws/AwsPubSubHandshakeHandlerTest.java | 23 ++++++ .../AwsPubsubRequestBodyExtractorTest.java | 77 +++++++++++++++---- .../aws/AwsServiceAccountValidatorTest.java | 26 +++++++ 6 files changed, 176 insertions(+), 19 deletions(-) create mode 100644 provider/notification-aws/src/test/java/org/opengroup/osdu/notification/provider/aws/AwsAppPropertiesTest.java create mode 100644 provider/notification-aws/src/test/java/org/opengroup/osdu/notification/provider/aws/AwsGoogleServiceAccountImplTest.java create mode 100644 provider/notification-aws/src/test/java/org/opengroup/osdu/notification/provider/aws/AwsPubSubHandshakeHandlerTest.java create mode 100644 provider/notification-aws/src/test/java/org/opengroup/osdu/notification/provider/aws/AwsServiceAccountValidatorTest.java diff --git a/provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/impl/AwsPubsubRequestBodyExtractor.java b/provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/impl/AwsPubsubRequestBodyExtractor.java index d4c30be61..a25ba93ea 100644 --- a/provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/impl/AwsPubsubRequestBodyExtractor.java +++ b/provider/notification-aws/src/main/java/org/opengroup/osdu/notification/provider/aws/impl/AwsPubsubRequestBodyExtractor.java @@ -59,6 +59,7 @@ public class AwsPubsubRequestBodyExtractor implements IPubsubRequestBodyExtracto if (this.messageContent == null) { this.messageContent = this.extractPubsubMessageFromRequestBody(); } + return this.messageContent.getAttributes(); } @@ -70,10 +71,14 @@ public class AwsPubsubRequestBodyExtractor implements IPubsubRequestBodyExtracto } public String extractNotificationIdFromRequestBody() { + JsonElement subscription = null; if (this.root == null) { this.root = this.extractRootJsonElementFromRequestBody(); } - JsonElement subscription = this.root.get("subscription"); + + if(root.has("subscription")) + subscription = this.root.get("subscription"); + if (subscription == null) { throw new AppException(HttpStatus.BAD_REQUEST.value(), INVALID_PUBSUB_MESSAGE, "Subscription object not found"); } @@ -92,14 +97,16 @@ public class AwsPubsubRequestBodyExtractor implements IPubsubRequestBodyExtracto this.root = this.extractRootJsonElementFromRequestBody(); } JsonElement message = this.root.get("message"); + if (message == null) { throw new AppException(HttpStatus.BAD_REQUEST.value(), INVALID_PUBSUB_MESSAGE, "Message object not found"); } + MessageContent content = GSON.fromJson(message.toString(), MessageContent.class); Map<String, String> attributes = content.getAttributes(); + if (attributes == null || attributes.isEmpty()) { - log.error("Incorrect Message: " + message ); throw new AppException(HttpStatus.BAD_REQUEST.value(), INVALID_PUBSUB_MESSAGE, "Attribute map not found"); } String data = content.getData(); diff --git a/provider/notification-aws/src/test/java/org/opengroup/osdu/notification/provider/aws/AwsAppPropertiesTest.java b/provider/notification-aws/src/test/java/org/opengroup/osdu/notification/provider/aws/AwsAppPropertiesTest.java new file mode 100644 index 000000000..b5533616f --- /dev/null +++ b/provider/notification-aws/src/test/java/org/opengroup/osdu/notification/provider/aws/AwsAppPropertiesTest.java @@ -0,0 +1,37 @@ +package org.opengroup.osdu.notification.provider.aws; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.junit.MockitoJUnitRunner; +import org.opengroup.osdu.notification.provider.aws.impl.AwsAppProperties; +import org.powermock.reflect.Whitebox; + + +@RunWith(MockitoJUnitRunner.class) +public class AwsAppPropertiesTest { + + @InjectMocks + AwsAppProperties awsAppProperties; + + @Before + public void initTest() { + Whitebox.setInternalState(awsAppProperties, "authorizeAPI", "authorizeAPI"); + Whitebox.setInternalState(awsAppProperties, "registerAPI", "registerAPI"); + } + + @Test + public void getAuthorizeAPIReturnsAuthorizeAPI() { + String authorizeAPI = "authorizeAPI"; + assertEquals(authorizeAPI, awsAppProperties.getAuthorizeAPI()); + } + + @Test + public void getRegisterAPIReturnsAuthorizeAPI() { + String registerAPI = "registerAPI"; + assertEquals(registerAPI, awsAppProperties.getRegisterAPI()); + } +} diff --git a/provider/notification-aws/src/test/java/org/opengroup/osdu/notification/provider/aws/AwsGoogleServiceAccountImplTest.java b/provider/notification-aws/src/test/java/org/opengroup/osdu/notification/provider/aws/AwsGoogleServiceAccountImplTest.java new file mode 100644 index 000000000..38137338c --- /dev/null +++ b/provider/notification-aws/src/test/java/org/opengroup/osdu/notification/provider/aws/AwsGoogleServiceAccountImplTest.java @@ -0,0 +1,21 @@ +package org.opengroup.osdu.notification.provider.aws; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.junit.MockitoJUnitRunner; +import org.opengroup.osdu.notification.provider.aws.impl.AwsGoogleServiceAccountImpl; +import sun.reflect.generics.reflectiveObjects.NotImplementedException; + +@RunWith(MockitoJUnitRunner.class) +public class AwsGoogleServiceAccountImplTest { + + @InjectMocks + AwsGoogleServiceAccountImpl awsGoogleServiceAccountImpl; + + + @Test(expected = NotImplementedException.class) + public void getIdTokenThorwsNotImplementedException() { + awsGoogleServiceAccountImpl.getIdToken("keyString", "audience"); + } +} diff --git a/provider/notification-aws/src/test/java/org/opengroup/osdu/notification/provider/aws/AwsPubSubHandshakeHandlerTest.java b/provider/notification-aws/src/test/java/org/opengroup/osdu/notification/provider/aws/AwsPubSubHandshakeHandlerTest.java new file mode 100644 index 000000000..d48cbef2d --- /dev/null +++ b/provider/notification-aws/src/test/java/org/opengroup/osdu/notification/provider/aws/AwsPubSubHandshakeHandlerTest.java @@ -0,0 +1,23 @@ +package org.opengroup.osdu.notification.provider.aws; + +import static org.junit.Assert.assertNull; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.junit.MockitoJUnitRunner; +import org.opengroup.osdu.notification.provider.aws.impl.AwsPubSubHandshakeHandler; + + +@RunWith(MockitoJUnitRunner.class) +public class AwsPubSubHandshakeHandlerTest { + + @InjectMocks + AwsPubSubHandshakeHandler AwsPubSubHandshakeHandler; + + + @Test + public void getIdTokenReturnsNull() { + assertNull( AwsPubSubHandshakeHandler.getHandshakeResponse()); + } +} diff --git a/provider/notification-aws/src/test/java/org/opengroup/osdu/notification/provider/aws/AwsPubsubRequestBodyExtractorTest.java b/provider/notification-aws/src/test/java/org/opengroup/osdu/notification/provider/aws/AwsPubsubRequestBodyExtractorTest.java index 021cc17d2..b43a19ae2 100644 --- a/provider/notification-aws/src/test/java/org/opengroup/osdu/notification/provider/aws/AwsPubsubRequestBodyExtractorTest.java +++ b/provider/notification-aws/src/test/java/org/opengroup/osdu/notification/provider/aws/AwsPubsubRequestBodyExtractorTest.java @@ -25,7 +25,7 @@ import org.mockito.junit.MockitoJUnitRunner; import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; - +import org.opengroup.osdu.core.common.model.http.AppException; import org.opengroup.osdu.core.common.model.http.DpsHeaders; import org.opengroup.osdu.notification.provider.aws.impl.AwsPubsubRequestBodyExtractor; @@ -38,16 +38,12 @@ import java.io.StringReader; import java.util.Map; - - @RunWith(MockitoJUnitRunner.class) public class AwsPubsubRequestBodyExtractorTest { @Mock private DpsHeaders dpsHeaders; - - @Mock private JaxRsDpsLog logger; @@ -58,10 +54,7 @@ public class AwsPubsubRequestBodyExtractorTest { private AwsPubsubRequestBodyExtractor service; @Before - public void init() { - - - } + public void init() { } @Test @@ -87,9 +80,6 @@ public class AwsPubsubRequestBodyExtractorTest { String receivedData = service.extractDataFromRequestBody(); Assert.assertEquals(expectedData,receivedData); - - - } @@ -116,11 +106,69 @@ public class AwsPubsubRequestBodyExtractorTest { String receivedData = service.extractNotificationIdFromRequestBody(); Assert.assertEquals(expectedData,receivedData); + } + + @Test(expected = AppException.class) + public void extractNotificationIdFromRequestBodyThrowsAppExceptionOnNullSubscription() throws IOException, AppException { + String stringRequest = "{\n" + + "\t\"Type\": \"Notification\",\n" + + "\t\"message\": {\n" + + "\t\"attributes\": {\n" + + "\t\"correlation-id\": \"39137f49-67d6-4001-a6aa-15521ef4f49e\",\n" + + "\t\"data-partition-id\": \"" + TestUtils.getDataPartitionId() + "\"\n" + + "\t},\n" + + "\t\"data\": \"dGVzdERhdGE=\",\n" + + "\t\"messageId\": \"136969346945\"\n" + + "\t}\n" + + "}\n"; + BufferedReader reader = new BufferedReader(new StringReader(stringRequest)); + Mockito.when(request.getReader()).thenReturn(reader); + service = new AwsPubsubRequestBodyExtractor(request); + service.extractNotificationIdFromRequestBody(); + } + + @Test(expected = AppException.class) + public void extractAttributesFromRequestBodyThrowsAppExceptionOnNullAttributes() throws IOException, AppException { + String stringRequest = "{\n" + + "\t\"Type\": \"Notification\",\n" + + "\t\"message\": {\n" + + "\t\"attributes\": {\n" + + "\t},\n" + + "\t\"data\": \"dGVzdERhdGE=\",\n" + + "\t\"messageId\": \"136969346945\"\n" + + "\t},\n" + + "\t\"subscription\": \""+ "de12345" +"\"\n" + + "}\n"; + BufferedReader reader = new BufferedReader(new StringReader(stringRequest)); + Mockito.when(request.getReader()).thenReturn(reader); + service = new AwsPubsubRequestBodyExtractor(request); + service.extractAttributesFromRequestBody(); } + + + @Test(expected = AppException.class) + public void extractAttributesFromRequestBodyThrowsAppExceptionOnNullDataPartitionId() throws IOException, AppException { + String stringRequest = "{\n" + + "\t\"Type\": \"Notification\",\n" + + "\t\"message\": {\n" + + "\t\"attributes\": {\n" + + "\t\"correlation-id\": \"39137f49-67d6-4001-a6aa-15521ef4f49e\"\n" + + "\t},\n" + + "\t\"data\": \"dGVzdERhdGE=\",\n" + + "\t\"messageId\": \"136969346945\"\n" + + "\t},\n" + + "\t\"subscription\": \""+ "de12345" +"\"\n" + + "}\n"; + BufferedReader reader = new BufferedReader(new StringReader(stringRequest)); + Mockito.when(request.getReader()).thenReturn(reader); + service = new AwsPubsubRequestBodyExtractor(request); + service.extractAttributesFromRequestBody(); + } + @Test public void should_returnValidAttributes_FromRequestBody() throws IOException { String stringRequest = "{\n" + @@ -138,8 +186,6 @@ public class AwsPubsubRequestBodyExtractorTest { BufferedReader reader = new BufferedReader(new StringReader(stringRequest)); Mockito.when(request.getReader()).thenReturn(reader); - String expectedData = "de12345"; - service = new AwsPubsubRequestBodyExtractor(request); // Act Map<String, String> receivedAttributes = service.extractAttributesFromRequestBody(); @@ -147,8 +193,5 @@ public class AwsPubsubRequestBodyExtractorTest { // Asset Assert.assertEquals("39137f49-123-456", receivedAttributes.get("correlation-id")); Assert.assertEquals("opendes", receivedAttributes.get("data-partition-id")); - - - } } diff --git a/provider/notification-aws/src/test/java/org/opengroup/osdu/notification/provider/aws/AwsServiceAccountValidatorTest.java b/provider/notification-aws/src/test/java/org/opengroup/osdu/notification/provider/aws/AwsServiceAccountValidatorTest.java new file mode 100644 index 000000000..5649ab02c --- /dev/null +++ b/provider/notification-aws/src/test/java/org/opengroup/osdu/notification/provider/aws/AwsServiceAccountValidatorTest.java @@ -0,0 +1,26 @@ +package org.opengroup.osdu.notification.provider.aws; + +import static org.junit.Assert.assertFalse; + +import org.junit.runner.RunWith; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.junit.MockitoJUnitRunner; +import org.opengroup.osdu.notification.provider.aws.impl.AwsServiceAccountValidator; + +@RunWith(MockitoJUnitRunner.class) +public class AwsServiceAccountValidatorTest { + + @InjectMocks + AwsServiceAccountValidator awsServiceAccountValidator; + + @Test + public void awsServiceAccountValidatorReturnsFalse() { + assertFalse(awsServiceAccountValidator.isValidPublisherServiceAccount("jwt")); + } + + @Test + public void isValidServiceAccountReturnsFalse() { + assertFalse(awsServiceAccountValidator.isValidServiceAccount("jwt", "userIdentity", "audiences")); + } +} -- GitLab