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 d4c30be61a0cbdd14cf41ae5b43f98e1021b2152..a25ba93ea6b27aa7805d53bb96eb9f585b944f1f 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 0000000000000000000000000000000000000000..b5533616fabb8df7814522ab43fcf70f6b5f7f2b --- /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 0000000000000000000000000000000000000000..38137338c77dae5989069fe6e5977d6ae7532f88 --- /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 0000000000000000000000000000000000000000..d48cbef2d0f95f9bf8a1a9ee240522c7c0d9fc01 --- /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 021cc17d22803ea24f0cdeec17bea522823d3614..b43a19ae21c74a0ae1f3f4c7369a95939caf9dea 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 0000000000000000000000000000000000000000..5649ab02c19c53adb92df25f6f61afc52e7bb138 --- /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")); + } +}