Skip to content
Snippets Groups Projects
Commit bbc4c727 authored by Komal Makkar's avatar Komal Makkar
Browse files

Added tests and some utils

parent 2a6dd09d
No related branches found
No related tags found
1 merge request!12Pubsub Azure Implementation.
Pipeline #5780 failed
Showing
with 686 additions and 90 deletions
......@@ -87,7 +87,7 @@ public class PubsubEndpoint {
@PreAuthorize("@authorizationFilter.hasAnyPermission('" + Config.OPS + "', '" + Config.PUBSUB + "')")
public ResponseEntity recordChanged() throws Exception {
if(this.pubsubRequestBodyExtractor.isHandshakeRequest()) {
this.pubsubHandshakeHandler.getHandshakeResponse();
return ResponseEntity.ok(this.pubsubHandshakeHandler.getHandshakeResponse());
}
String notificationId = this.pubsubRequestBodyExtractor.extractNotificationIdFromRequestBody();
......
......@@ -14,52 +14,39 @@
package org.opengroup.osdu.notification.provider.azure.pubsub;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.notification.provider.azure.util.RequestAttributesDispatcher;
import org.opengroup.osdu.notification.pubsub.IPubsubHandshakeHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Component
@Lazy
public class EventGridHandshakeHandler implements IPubsubHandshakeHandler {
private JsonObject root = null;
@Autowired
private HttpServletRequest request;
private RequestAttributesDispatcher requestDispatcher;
/**
* Extract Handshake response string form Handshake request.
*
* TODO: Check if there is a need to verify subscription name with
* registration service, before verifying the endpoint.
* @return validation string
*/
@Override
public String getHandshakeResponse() {
if (this.root == null) {
this.root = this.extractRootJsonElementFromRequestBody();
}
String response = null;
String response = null;
try {
JsonObject data = (JsonObject) this.root.get("data");
JsonObject requestRoot = requestDispatcher.getRequestRoot();
JsonObject data = (JsonObject) requestRoot.get("data");
String validationCode = data.get("validationCode").getAsString();
JsonObject jsonResponse = new JsonObject();
jsonResponse.addProperty("ValidationResponse", validationCode);
response = validationCode.toString();
response = jsonResponse.toString();
} catch (Exception exception) {
throw new AppException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Request payload parsing error",
"Unable to parse request payload.", exception);
......@@ -67,29 +54,4 @@ public class EventGridHandshakeHandler implements IPubsubHandshakeHandler {
return response;
}
/**
* Utility method to extract root element from the request.
*
* @return Root JsonObject
*/
private JsonObject extractRootJsonElementFromRequestBody() {
try {
JsonParser jsonParser = new JsonParser();
BufferedReader reader = request.getReader();
Stream<String> lines = reader.lines();
String requestBody = lines.collect(Collectors.joining("\n"));
JsonArray requestArray = (JsonArray) jsonParser.parse(requestBody);
JsonElement rootElement = requestArray.get(0);
if (!(rootElement instanceof JsonObject)) {
throw new AppException(HttpStatus.BAD_REQUEST.value(), "RequestBody is not JsonObject.",
"Request Body should be JsonObject to be processed.");
}
return rootElement.getAsJsonObject();
} catch (IOException e) {
throw new AppException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Request payload parsing error",
"Unable to parse request payload.", e);
}
}
}
......@@ -18,40 +18,44 @@ import com.google.common.base.Strings;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.notification.models.MessageContent;
import org.opengroup.osdu.notification.provider.azure.util.RequestAttributesDispatcher;
import org.opengroup.osdu.notification.pubsub.IPubsubRequestBodyExtractor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.context.annotation.RequestScope;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
// TODO : Make error messages as constants.
// TODO : Add TracePoints to exception logs so that monitoring is done well.
@Component
@RequestScope
public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtractor {
private static final String INVALID_PUBSUB_MESSAGE = "Invalid pubsub message";
private static final String INVALID_EVENTGRID_MESSAGE = "Invalid Event Grid Message";
private static final String EVENTGRID_VALIDATION_EVENT = "Microsoft.EventGrid.SubscriptionValidationEvent";
private static final Gson GSON = new Gson();
private MessageContent messageContent;
private JsonObject root = null;
@Autowired
private HttpServletRequest request;
private RequestAttributesDispatcher requestDispatcher;
@Autowired
private JaxRsDpsLog log;
/**
* Extracts the attributes from the request that are filled in by publisher of the message.
*
* @throws AppException
* @return Request Attributes Map
*/
public Map<String, String> extractAttributesFromRequestBody() {
if (this.messageContent == null) {
this.messageContent = this.extractPubsubMessageFromRequestBody();
......@@ -59,6 +63,12 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto
return this.messageContent.getAttributes();
}
/**
* Extracts the data from the request that are filled in by publisher of the message,
*
* @throws AppException
* @return Request Data String
*/
public String extractDataFromRequestBody() {
if (this.messageContent == null) {
this.messageContent = this.extractPubsubMessageFromRequestBody();
......@@ -66,49 +76,70 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto
return this.messageContent.getData();
}
/**
* Extracts the notificationId from the request that are filled in by EventGrid.
*
* @throws AppException
* @return Request NotificationId String.
*/
public String extractNotificationIdFromRequestBody() {
return this.requestDispatcher.getRequestSubscriptionId();
}
/**
* Checks if the request is for handshake.
*
* @throws AppException
* @return Request Type Boolean
*/
public boolean isHandshakeRequest() {
if (this.root == null) {
this.root = this.extractRootJsonElementFromRequestBody();
this.root = this.requestDispatcher.getRequestRoot();
}
try {
JsonElement data = this.root.get("eventType");
if (EVENTGRID_VALIDATION_EVENT.equals(data.getAsString())) {
return true;
}
} catch (Exception exception) {
throw new AppException(HttpStatus.BAD_REQUEST.value(), "RequestBody is not JsonObject.",
"Request Body should be JsonObject to be processed.");
JsonElement data = this.root.get("eventType");
if (EVENTGRID_VALIDATION_EVENT.equals(data.getAsString())) {
return true;
}
return false;
}
/**
* Utility method that wxtracts the core content in the request.
* This is what the publisher will publish.
*
* @throws AppException
* @return Message Content Object
*/
private MessageContent extractPubsubMessageFromRequestBody() {
/* if (this.root == null) {
this.root = this.extractRootJsonElementFromRequestBody();
if (this.root == null) {
this.root = this.requestDispatcher.getRequestRoot();
}
JsonElement message = this.root.get("message");
JsonElement message = this.root.get("data");
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_EVENTGRID_MESSAGE, "message object not found");
}
MessageContent content = GSON.fromJson(message.toString(), MessageContent.class);
String eventTime = this.root.getAsJsonObject().get("eventTime").getAsString();
if (Strings.isNullOrEmpty(eventTime)) {
throw new AppException(HttpStatus.BAD_REQUEST.value(), INVALID_EVENTGRID_MESSAGE, "Event time not found");
}
content.setPublishTime(eventTime);
Map<String, String> attributes = content.getAttributes();
if (attributes == null || attributes.isEmpty()) {
log.error("Incorrect Message: " + message.toString() );
throw new AppException(HttpStatus.BAD_REQUEST.value(), INVALID_PUBSUB_MESSAGE, "attribute map not found");
throw new AppException(HttpStatus.BAD_REQUEST.value(), INVALID_EVENTGRID_MESSAGE, "attribute map not found");
}
String data = content.getData();
if (Strings.isNullOrEmpty(data)) {
throw new AppException(HttpStatus.BAD_REQUEST.value(), INVALID_PUBSUB_MESSAGE, "data field not found");
throw new AppException(HttpStatus.BAD_REQUEST.value(), INVALID_EVENTGRID_MESSAGE, "data field not found");
}
Map<String, String> lowerCase = new HashMap<>();
attributes.forEach((key, value) -> lowerCase.put(key.toLowerCase(), value));
if (Strings.isNullOrEmpty(attributes.get("data-partition-id"))) {
throw new AppException(HttpStatus.BAD_REQUEST.value(), INVALID_PUBSUB_MESSAGE,
throw new AppException(HttpStatus.BAD_REQUEST.value(), INVALID_EVENTGRID_MESSAGE,
"No tenant information from pubsub message.");
}
content.setAttributes(lowerCase);
......@@ -116,24 +147,8 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto
String decoded = new String(Base64.getDecoder().decode(data));
content.setData(decoded);
return content;*/
return content;
}
private JsonObject extractRootJsonElementFromRequestBody() {
try {
JsonParser jsonParser = new JsonParser();
BufferedReader reader = request.getReader();
Stream<String> lines = reader.lines();
String requestBody = lines.collect(Collectors.joining("\n"));
JsonElement rootElement = jsonParser.parse(requestBody);
if (!(rootElement instanceof JsonObject)) {
throw new AppException(HttpStatus.BAD_REQUEST.value(), "RequestBody is not JsonObject.",
"Request Body should be JsonObject to be processed.");
}
return rootElement.getAsJsonObject();
} catch (IOException e) {
throw new AppException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Request payload parsing error",
"Unable to parse request payload.", e);
}
}
}
// Copyright © Microsoft Corporation
//
// 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 org.opengroup.osdu.notification.provider.azure.util;
import com.google.common.base.Strings;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.context.annotation.RequestScope;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Component
@RequestScope
public class RequestAttributesDispatcher {
private JsonObject requestRoot;
@Autowired
private HttpServletRequest request;
/**
* Extracts Request's root.
*
* @throws AppException
* @throws IOException
* @return Request Root JsonObject
*/
public JsonObject getRequestRoot(){
if(requestRoot == null) {
try {
JsonArray requestArray = getRequest();
JsonElement rootElement = requestArray.get(0);
if (!(rootElement instanceof JsonObject)) {
throw new AppException(HttpStatus.BAD_REQUEST.value(), "RequestBody is not JsonObject.",
"Request Body should be JsonObject to be processed.");
}
this.requestRoot = rootElement.getAsJsonObject();
} catch (Exception e) {
throw new AppException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Request payload parsing error",
"Unable to parse request payload.", e);
}
}
return requestRoot;
}
/**
* Extracts Subscription Name from the header.
*
* @throws AppException
* @throws IOException
* @return Sunscription Name String
*/
public String getRequestSubscriptionId(){
String subscriptionId = request.getHeader("Aeg-Subscription-Name");
if (Strings.isNullOrEmpty(subscriptionId)) {
throw new AppException(HttpStatus.BAD_REQUEST.value(), "Invalid Event Grid Message", "Subscription ID not found");
}
return subscriptionId;
}
/**
* Utility method to read the request's body as stream and
* convert it into JsonArray
* @throws AppException
* @throws IOException
* @return Request JsonArray
*/
private JsonArray getRequest() throws IOException {
JsonParser jsonParser = new JsonParser();
JsonArray requestArray;
try {
BufferedReader reader = request.getReader();
Stream<String> lines = reader.lines();
String requestBody = lines.collect(Collectors.joining("\n"));
requestArray = (JsonArray) jsonParser.parse(requestBody);
} catch (Exception e){
throw new AppException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Request payload parsing error",
"Unable to read from the request.", e);
}
return requestArray;
}
}
// Copyright © Microsoft Corporation
//
// 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 org.opengroup.osdu.notification.pubsub;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.notification.provider.azure.pubsub.EventGridHandshakeHandler;
import org.opengroup.osdu.notification.provider.azure.util.RequestAttributesDispatcher;
import org.springframework.http.HttpStatus;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class EventGridHandshakeHandlerTest {
@Mock
RequestAttributesDispatcher requestAttributesDispatcherMock;
@InjectMocks
@Spy
private EventGridHandshakeHandler sut;
@Test
public void should_returnValidResponse_getHandshakeResponse() {
// Set up
String validHandshakeRequestRoot =
" {\n" +
" \"id\": \"testId\",\n" +
" \"topic\": \"testTopic\",\n" +
" \"subject\": \"\",\n" +
" \"data\": {\n" +
" \"validationCode\": \"testValidationCode\",\n" +
" \"validationUrl\": \"testURL\"\n" +
" },\n" +
" \"eventType\": \"Microsoft.EventGrid.SubscriptionValidationEvent\",\n" +
" \"eventTime\": \"2020-08-14T11:18:55.9278057Z\",\n" +
" \"metadataVersion\": \"1\",\n" +
" \"dataVersion\": \"2\"\n" +
" }";
JsonObject requestRoot = new JsonParser().parse(validHandshakeRequestRoot).getAsJsonObject();
when(this.requestAttributesDispatcherMock.getRequestRoot()).thenReturn(requestRoot);
String expectedResponse = "{\"ValidationResponse\":\"testValidationCode\"}";
// Act
String observedResponse = this.sut.getHandshakeResponse();
// Assert
Assert.assertEquals(observedResponse, expectedResponse);
}
@Test
public void should_throwWhenDataIsMissing_getHandshakeResponse() {
// Set up
String validHandshakeRequestRoot =
" {\n" +
" \"id\": \"testId\",\n" +
" \"topic\": \"testTopic\",\n" +
" \"subject\": \"\",\n" +
" \"eventType\": \"Microsoft.EventGrid.SubscriptionValidationEvent\",\n" +
" \"eventTime\": \"2020-08-14T11:18:55.9278057Z\",\n" +
" \"metadataVersion\": \"1\",\n" +
" \"dataVersion\": \"2\"\n" +
" }";
JsonObject requestRoot = new JsonParser().parse(validHandshakeRequestRoot).getAsJsonObject();
when(this.requestAttributesDispatcherMock.getRequestRoot()).thenReturn(requestRoot);
try {
// Act
this.sut.getHandshakeResponse();
// Asset
fail("Should Throw Exception");
} catch (
AppException appException){
Assert.assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), appException.getError().getCode());
Assert.assertEquals("Unable to parse request payload." , appException.getError().getMessage());
} catch (Exception exception) {
fail("Should Throw AppException");
}
}
}
// Copyright © Microsoft Corporation
//
// 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 org.opengroup.osdu.notification.pubsub;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.notification.provider.azure.pubsub.EventGridRequestBodyExtractor;
import org.opengroup.osdu.notification.provider.azure.util.RequestAttributesDispatcher;
import org.springframework.http.HttpStatus;
import java.util.Map;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class EventGridRequestBodyExtractorTest {
private static final String INVALID_EVENTGRID_MESSAGE = "Invalid Event Grid Message";
@Mock
RequestAttributesDispatcher requestAttributesDispatcherMock;
@InjectMocks
@Spy
private EventGridRequestBodyExtractor sut;
@Test
public void should_returntrue_isHandshakeRequest() {
// Set up
String validHandshakeRequestRoot =
" {\n" +
" \"id\": \"testId\",\n" +
" \"topic\": \"testTopic\",\n" +
" \"subject\": \"\",\n" +
" \"data\": {\n" +
" \"validationCode\": \"testValidationCode\",\n" +
" \"validationUrl\": \"testURL\"\n" +
" },\n" +
" \"eventType\": \"Microsoft.EventGrid.SubscriptionValidationEvent\",\n" +
" \"eventTime\": \"2020-08-14T11:18:55.9278057Z\",\n" +
" \"metadataVersion\": \"1\",\n" +
" \"dataVersion\": \"2\"\n" +
" }";
JsonObject requestRoot = new JsonParser().parse(validHandshakeRequestRoot).getAsJsonObject();
when(this.requestAttributesDispatcherMock.getRequestRoot()).thenReturn(requestRoot);
// Act
boolean response = this.sut.isHandshakeRequest();
// Assert
Assert.assertTrue(response);
}
@Test
public void should_returnfalse_isHandshakeRequest() {
//SetUp
String invalidHandshakeRequestRoot =
" {\n" +
" \"id\": \"testId\",\n" +
" \"topic\": \"testTopic\",\n" +
" \"subject\": \"\",\n" +
" \"data\": {\n" +
" \"validationUrl\": \"testURL\"\n" +
" },\n" +
" \"eventType\": \"SubscriptionValidationEvent\",\n" +
" \"eventTime\": \"2020-08-14T11:18:55.9278057Z\",\n" +
" \"metadataVersion\": \"1\",\n" +
" \"dataVersion\": \"2\"\n" +
" }";
JsonObject requestRoot = new JsonParser().parse(invalidHandshakeRequestRoot).getAsJsonObject();
when(this.requestAttributesDispatcherMock.getRequestRoot()).thenReturn(requestRoot);
// Act
boolean response = this.sut.isHandshakeRequest();
// Assert
Assert.assertFalse(response);
}
@Test
public void should_throwWhenMessageIsMissing_extractDataFromRequestBody() {
// SetUp
String requestRootWithoutData = "{\n" +
" \"id\": \"2425\",\n" +
" \"eventType\": \"recordInserted\",\n" +
" \"subject\": \"myapp/vehicles/motorcycles\",\n" +
" \"dataVersion\": \"1.0\",\n" +
" \"metadataVersion\": \"1\",\n" +
" \"eventTime\": \"2020-08-14T18:04:12+00:00\",\n" +
" \"topic\": \"testTopic\"\n" +
" }";
JsonObject requestRoot = new JsonParser().parse(requestRootWithoutData).getAsJsonObject();
when(this.requestAttributesDispatcherMock.getRequestRoot()).thenReturn(requestRoot);
try{
// Act
this.sut.extractDataFromRequestBody();
// Asset
fail("Should Throw Exception");
} catch (AppException appException){
Assert.assertEquals(HttpStatus.BAD_REQUEST.value(), appException.getError().getCode());
Assert.assertEquals("message object not found", appException.getError().getMessage());
} catch (Exception exception) {
fail("Should Throw AppException");
}
}
@Test
public void should_throwWhenAttributesAreMissing_extractDataFromRequestBody() {
String requestRootWithoutAttributes = "{\n" +
" \"id\": \"2425\",\n" +
" \"eventType\": \"recordInserted\",\n" +
" \"subject\": \"myapp/vehicles/motorcycles\",\n" +
" \"data\": {\n" +
" \"data\": \"W3sia2luZCI6InRlc3RraW5kIiwiaWQiOiJ0ZXN0aWQiLCJvcGVyYXRpb250eXBlIjoiY3JlYXRlIn0seyJraW5kIjoidGVzdGtpbmQyIiwiaWQiOiJ0ZXN0aWQyIiwib3BlcmF0aW9udHlwZSI6InVwZGF0ZSJ9XQ\",\n" +
" \"messageId\": \"136969346945\"\n" +
" },\n" +
" \"dataVersion\": \"1.0\",\n" +
" \"metadataVersion\": \"1\",\n" +
" \"eventTime\": \"2020-08-14T18:04:12+00:00\",\n" +
" \"topic\": \"/subscriptions/c99e2bf3-1777-412b-baba-d823676589c2/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" +
" }";
JsonObject requestRoot = new JsonParser().parse(requestRootWithoutAttributes).getAsJsonObject();
when(this.requestAttributesDispatcherMock.getRequestRoot()).thenReturn(requestRoot);
try{
// Act
this.sut.extractDataFromRequestBody();
// Asset
fail("Should Throw Exception");
} catch (AppException appException){
Assert.assertEquals(HttpStatus.BAD_REQUEST.value(), appException.getError().getCode());
Assert.assertEquals("attribute map not found", appException.getError().getMessage());
} catch (Exception exception) {
//fail("Should Throw AppException");
}
}
@Test
public void should_throwWhenDataFiledIsMissing_extractDataFromRequestBody() {
String requestRootWithoutData = "{\n" +
" \"id\": \"2425\",\n" +
" \"eventType\": \"recordInserted\",\n" +
" \"subject\": \"myapp/vehicles/motorcycles\",\n" +
" \"data\": {\n" +
" \"attributes\": {\n" +
" \"correlation-id\": \"39137f49-67d6-4001-a6aa-15521ef4f49e\",\n" +
" \"data-partition-id\": \"opendes \"\n" +
" },\n" +
" \"messageId\": \"136969346945\"\n" +
" },\n" +
" \"dataVersion\": \"1.0\",\n" +
" \"metadataVersion\": \"1\",\n" +
" \"eventTime\": \"2020-08-14T18:04:12+00:00\",\n" +
" \"topic\": \"/subscriptions/c99e2bf3-1777-412b-baba-d823676589c2/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" +
" }";
JsonObject requestRoot = new JsonParser().parse(requestRootWithoutData).getAsJsonObject();
when(this.requestAttributesDispatcherMock.getRequestRoot()).thenReturn(requestRoot);
try{
// Act
this.sut.extractDataFromRequestBody();
// Asset
fail("Should Throw Exception");
} catch (AppException appException){
Assert.assertEquals(HttpStatus.BAD_REQUEST.value(), appException.getError().getCode());
Assert.assertEquals("data field not found", appException.getError().getMessage());
} catch (Exception exception) {
fail("Should Throw AppException");
}
}
@Test
public void should_throwWhenPartitionIdIsMissing_extractDataFromRequestBody() {
String requestRootWithoutDataPartitionId = "{\n" +
" \"id\": \"2425\",\n" +
" \"eventType\": \"recordInserted\",\n" +
" \"subject\": \"myapp/vehicles/motorcycles\",\n" +
" \"data\": {\n" +
" \"attributes\": {\n" +
" \"correlation-id\": \"39137f49-67d6-4001-a6aa-15521ef4f49e\"\n" +
" },\n" +
" \"data\": \"W3sia2luZCI6InRlc3RraW5kIiwiaWQiOiJ0ZXN0aWQiLCJvcGVyYXRpb250eXBlIjoiY3JlYXRlIn0seyJraW5kIjoidGVzdGtpbmQyIiwiaWQiOiJ0ZXN0aWQyIiwib3BlcmF0aW9udHlwZSI6InVwZGF0ZSJ9XQ\",\n"+
" \"messageId\": \"136969346945\"\n" +
" },\n" +
" \"dataVersion\": \"1.0\",\n" +
" \"metadataVersion\": \"1\",\n" +
" \"eventTime\": \"2020-08-14T18:04:12+00:00\",\n" +
" \"topic\": \"/subscriptions/c99e2bf3-1777-412b-baba-d823676589c2/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" +
" }";
JsonObject requestRoot = new JsonParser().parse(requestRootWithoutDataPartitionId).getAsJsonObject();
when(this.requestAttributesDispatcherMock.getRequestRoot()).thenReturn(requestRoot);
try{
// Act
this.sut.extractDataFromRequestBody();
// Asset
fail("Should Throw Exception");
} catch (AppException appException){
Assert.assertEquals(HttpStatus.BAD_REQUEST.value(), appException.getError().getCode());
//Assert.assertEquals("No tenant information from pubsub message.", appException.getError().getMessage());
} catch (Exception exception) {
fail("Should Throw AppException");
}
}
@Test
public void should_returnValidData_extractDataFromRequestBody() {
String vaidRequestRoot = "{\n" +
" \"id\": \"2425\",\n" +
" \"eventType\": \"recordInserted\",\n" +
" \"subject\": \"myapp/vehicles/motorcycles\",\n" +
" \"data\": {\n" +
" \"attributes\": {\n" +
" \"correlation-id\": \"39137f49-67d6-4001-a6aa-15521ef4f49e\",\n" +
" \"data-partition-id\": \"opendes \"\n" +
" },\n" +
" \"data\": \"W3sia2luZCI6InRlc3RraW5kIiwiaWQiOiJ0ZXN0aWQiLCJvcGVyYXRpb250eXBlIjoiY3JlYXRlIn0seyJraW5kIjoidGVzdGtpbmQyIiwiaWQiOiJ0ZXN0aWQyIiwib3BlcmF0aW9udHlwZSI6InVwZGF0ZSJ9XQ\",\n" +
" \"messageId\": \"136969346945\"\n" +
" },\n" +
" \"dataVersion\": \"1.0\",\n" +
" \"metadataVersion\": \"1\",\n" +
" \"eventTime\": \"2020-08-14T18:04:12+00:00\",\n" +
" \"topic\": \"/subscriptions/c99e2bf3-1777-412b-baba-d823676589c2/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" +
" }";
String expectedData = "[{\"kind\":\"testkind\",\"id\":\"testid\",\"operationtype\":\"create\"},{\"kind\":\"testkind2\",\"id\":\"testid2\",\"operationtype\":\"update\"}]";
JsonObject requestRoot = new JsonParser().parse(vaidRequestRoot).getAsJsonObject();
when(this.requestAttributesDispatcherMock.getRequestRoot()).thenReturn(requestRoot);
// Act
String recievedData = this.sut.extractDataFromRequestBody();
// Asset
Assert.assertEquals(expectedData,recievedData);
}
@Test
public void should_returnValidAttributes_extractDataFromRequestBody() {
String vaidRequestRoot = "{\n" +
" \"id\": \"2425\",\n" +
" \"eventType\": \"recordInserted\",\n" +
" \"subject\": \"myapp/vehicles/motorcycles\",\n" +
" \"data\": {\n" +
" \"attributes\": {\n" +
" \"correlation-id\": \"39137f49-67d6-4001-a6aa-15521ef4f49e\",\n" +
" \"data-partition-id\": \"opendes\"\n" +
" },\n" +
" \"data\": \"W3sia2luZCI6InRlc3RraW5kIiwiaWQiOiJ0ZXN0aWQiLCJvcGVyYXRpb250eXBlIjoiY3JlYXRlIn0seyJraW5kIjoidGVzdGtpbmQyIiwiaWQiOiJ0ZXN0aWQyIiwib3BlcmF0aW9udHlwZSI6InVwZGF0ZSJ9XQ\",\n" +
" \"messageId\": \"136969346945\"\n" +
" },\n" +
" \"dataVersion\": \"1.0\",\n" +
" \"metadataVersion\": \"1\",\n" +
" \"eventTime\": \"2020-08-14T18:04:12+00:00\",\n" +
" \"topic\": \"/subscriptions/c99e2bf3-1777-412b-baba-d823676589c2/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" +
" }";
JsonObject requestRoot = new JsonParser().parse(vaidRequestRoot).getAsJsonObject();
when(this.requestAttributesDispatcherMock.getRequestRoot()).thenReturn(requestRoot);
// Act
Map<String, String> observedAttributes = this.sut.extractAttributesFromRequestBody();
// Asset
Assert.assertEquals(observedAttributes.get("correlation-id"),"39137f49-67d6-4001-a6aa-15521ef4f49e");
Assert.assertEquals(observedAttributes.get("data-partition-id"),"opendes");
}
}
// Copyright © Microsoft Corporation
//
// 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 org.opengroup.osdu.notification.util;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.notification.provider.azure.util.RequestAttributesDispatcher;
import org.springframework.http.HttpStatus;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
@RunWith(MockitoJUnitRunner.class)
public class RequestAttributesDispatcherTest {
@Mock
private HttpServletRequest requestMock;
@InjectMocks
@Spy
private RequestAttributesDispatcher sut;
@Before
public void setup() {
initMocks(this);
}
@Test
public void should_throwWhenSubscriptionIdMissing_getRequestSubscriptionId() {
when(this.requestMock.getHeader("Aeg-Subscription-Name")).thenReturn(null);
try{
this.sut.getRequestSubscriptionId();
fail("Should Throw Exception");
} catch (AppException appException){
Assert.assertEquals(HttpStatus.BAD_REQUEST.value(), appException.getError().getCode());
Assert.assertEquals("Subscription ID not found", appException.getError().getMessage());
} catch (Exception exception) {
fail("Should Throw AppException");
}
}
@Test
public void should_throwWhenRequestRoot_getRequestRoot() throws IOException {
String invalidRequest = "{\n" +
" \"id\": \"e094dffa-9c15-4491-8312-98c73b699908\",\n" +
" \"topic\": \"/subscriptions/c99e2bf3-1777-412b-baba-d823676589c2/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\",\n" +
" \"subject\": \"\",\n" +
" \"data\": {\n" +
" \"validationCode\": \"6ECAD1AE-3451-4618-9D5D-EC3DD968885D\",\n" +
" \"validationUrl\": \"https://rp-westus2.eventgrid.azure.net:553/eventsubscriptions/eventgridtest/validate?id=6ECAD1AE-3451-4618-9D5D-EC3DD968885D&t=2020-08-14T11:31:24.7804184Z&apiVersion=2020-04-01-preview&token=YX71OvPJuWTTOVnhqaThLXU8gWqx6FkTkJSwoyK9%2fBE%3d\"\n" +
" },\n" +
" \"eventType\": \"Microsoft.EventGrid.SubscriptionValidationEvent\",\n" +
" \"eventTime\": \"2020-08-14T11:31:24.7804184Z\",\n" +
" \"metadataVersion\": \"1\",\n" +
" \"dataVersion\": \"2\"\n" +
"}";
when(this.requestMock.getReader()).thenReturn(
new BufferedReader(new StringReader(invalidRequest)));
try {
this.sut.getRequestRoot();
// Assert
fail("Should Throw Exception");
} catch (AppException appException) {
Assert.assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), appException.getError().getCode());
Assert.assertEquals("Unable to parse request payload.", appException.getError().getMessage());
} catch (Exception exception) {
fail("Should Throw AppException");
}
}
@Test
public void should_throwWhenArrayNotFound_getRequestRoot() throws IOException {
String invalidRequest = "[]";
when(this.requestMock.getReader()).thenReturn(
new BufferedReader(new StringReader(invalidRequest)));
try {
this.sut.getRequestRoot();
// Assert
fail("Should Throw Exception");
} catch (AppException appException) {
Assert.assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), appException.getError().getCode());
Assert.assertEquals("Unable to parse request payload.", appException.getError().getMessage());
} catch (Exception exception) {
fail("Should Throw AppException");
}
}
}
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