Skip to content
Snippets Groups Projects
Commit cfed368c authored by Solomon Ayalew's avatar Solomon Ayalew
Browse files

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
parent 1bf3f8d2
No related branches found
No related tags found
1 merge request!437merge code to gitlab
...@@ -59,6 +59,7 @@ public class AwsPubsubRequestBodyExtractor implements IPubsubRequestBodyExtracto ...@@ -59,6 +59,7 @@ public class AwsPubsubRequestBodyExtractor implements IPubsubRequestBodyExtracto
if (this.messageContent == null) { if (this.messageContent == null) {
this.messageContent = this.extractPubsubMessageFromRequestBody(); this.messageContent = this.extractPubsubMessageFromRequestBody();
} }
return this.messageContent.getAttributes(); return this.messageContent.getAttributes();
} }
...@@ -70,10 +71,14 @@ public class AwsPubsubRequestBodyExtractor implements IPubsubRequestBodyExtracto ...@@ -70,10 +71,14 @@ public class AwsPubsubRequestBodyExtractor implements IPubsubRequestBodyExtracto
} }
public String extractNotificationIdFromRequestBody() { public String extractNotificationIdFromRequestBody() {
JsonElement subscription = null;
if (this.root == null) { if (this.root == null) {
this.root = this.extractRootJsonElementFromRequestBody(); this.root = this.extractRootJsonElementFromRequestBody();
} }
JsonElement subscription = this.root.get("subscription");
if(root.has("subscription"))
subscription = this.root.get("subscription");
if (subscription == null) { if (subscription == null) {
throw new AppException(HttpStatus.BAD_REQUEST.value(), INVALID_PUBSUB_MESSAGE, "Subscription object not found"); throw new AppException(HttpStatus.BAD_REQUEST.value(), INVALID_PUBSUB_MESSAGE, "Subscription object not found");
} }
...@@ -92,14 +97,16 @@ public class AwsPubsubRequestBodyExtractor implements IPubsubRequestBodyExtracto ...@@ -92,14 +97,16 @@ public class AwsPubsubRequestBodyExtractor implements IPubsubRequestBodyExtracto
this.root = this.extractRootJsonElementFromRequestBody(); this.root = this.extractRootJsonElementFromRequestBody();
} }
JsonElement message = this.root.get("message"); JsonElement message = this.root.get("message");
if (message == null) { if (message == null) {
throw new AppException(HttpStatus.BAD_REQUEST.value(), INVALID_PUBSUB_MESSAGE, "Message object not found"); throw new AppException(HttpStatus.BAD_REQUEST.value(), INVALID_PUBSUB_MESSAGE, "Message object not found");
} }
MessageContent content = GSON.fromJson(message.toString(), MessageContent.class); MessageContent content = GSON.fromJson(message.toString(), MessageContent.class);
Map<String, String> attributes = content.getAttributes(); Map<String, String> attributes = content.getAttributes();
if (attributes == null || attributes.isEmpty()) { if (attributes == null || attributes.isEmpty()) {
log.error("Incorrect Message: " + message );
throw new AppException(HttpStatus.BAD_REQUEST.value(), INVALID_PUBSUB_MESSAGE, "Attribute map not found"); throw new AppException(HttpStatus.BAD_REQUEST.value(), INVALID_PUBSUB_MESSAGE, "Attribute map not found");
} }
String data = content.getData(); String data = content.getData();
......
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());
}
}
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");
}
}
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());
}
}
...@@ -25,7 +25,7 @@ import org.mockito.junit.MockitoJUnitRunner; ...@@ -25,7 +25,7 @@ import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; 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.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.notification.provider.aws.impl.AwsPubsubRequestBodyExtractor; import org.opengroup.osdu.notification.provider.aws.impl.AwsPubsubRequestBodyExtractor;
...@@ -38,16 +38,12 @@ import java.io.StringReader; ...@@ -38,16 +38,12 @@ import java.io.StringReader;
import java.util.Map; import java.util.Map;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class AwsPubsubRequestBodyExtractorTest { public class AwsPubsubRequestBodyExtractorTest {
@Mock @Mock
private DpsHeaders dpsHeaders; private DpsHeaders dpsHeaders;
@Mock @Mock
private JaxRsDpsLog logger; private JaxRsDpsLog logger;
...@@ -58,10 +54,7 @@ public class AwsPubsubRequestBodyExtractorTest { ...@@ -58,10 +54,7 @@ public class AwsPubsubRequestBodyExtractorTest {
private AwsPubsubRequestBodyExtractor service; private AwsPubsubRequestBodyExtractor service;
@Before @Before
public void init() { public void init() { }
}
@Test @Test
...@@ -87,9 +80,6 @@ public class AwsPubsubRequestBodyExtractorTest { ...@@ -87,9 +80,6 @@ public class AwsPubsubRequestBodyExtractorTest {
String receivedData = service.extractDataFromRequestBody(); String receivedData = service.extractDataFromRequestBody();
Assert.assertEquals(expectedData,receivedData); Assert.assertEquals(expectedData,receivedData);
} }
...@@ -116,11 +106,69 @@ public class AwsPubsubRequestBodyExtractorTest { ...@@ -116,11 +106,69 @@ public class AwsPubsubRequestBodyExtractorTest {
String receivedData = service.extractNotificationIdFromRequestBody(); String receivedData = service.extractNotificationIdFromRequestBody();
Assert.assertEquals(expectedData,receivedData); 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 @Test
public void should_returnValidAttributes_FromRequestBody() throws IOException { public void should_returnValidAttributes_FromRequestBody() throws IOException {
String stringRequest = "{\n" + String stringRequest = "{\n" +
...@@ -138,8 +186,6 @@ public class AwsPubsubRequestBodyExtractorTest { ...@@ -138,8 +186,6 @@ public class AwsPubsubRequestBodyExtractorTest {
BufferedReader reader = new BufferedReader(new StringReader(stringRequest)); BufferedReader reader = new BufferedReader(new StringReader(stringRequest));
Mockito.when(request.getReader()).thenReturn(reader); Mockito.when(request.getReader()).thenReturn(reader);
String expectedData = "de12345";
service = new AwsPubsubRequestBodyExtractor(request); service = new AwsPubsubRequestBodyExtractor(request);
// Act // Act
Map<String, String> receivedAttributes = service.extractAttributesFromRequestBody(); Map<String, String> receivedAttributes = service.extractAttributesFromRequestBody();
...@@ -147,8 +193,5 @@ public class AwsPubsubRequestBodyExtractorTest { ...@@ -147,8 +193,5 @@ public class AwsPubsubRequestBodyExtractorTest {
// Asset // Asset
Assert.assertEquals("39137f49-123-456", receivedAttributes.get("correlation-id")); Assert.assertEquals("39137f49-123-456", receivedAttributes.get("correlation-id"));
Assert.assertEquals("opendes", receivedAttributes.get("data-partition-id")); Assert.assertEquals("opendes", receivedAttributes.get("data-partition-id"));
} }
} }
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"));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment