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

Removed stray changed

parent 0968794d
No related branches found
No related tags found
1 merge request!38Changes in IT to match Infra
Pipeline #14760 failed
......@@ -16,8 +16,10 @@ package org.opengroup.osdu.notification.provider.azure.pubsub;
import com.google.gson.JsonObject;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.notification.provider.interfaces.IPubsubHandshakeHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
@Component
......@@ -25,9 +27,11 @@ public class EventGridHandshakeHandler implements IPubsubHandshakeHandler {
private EventGridRequestBodyExtractor eventGridRequestBodyExtractor;
private JaxRsDpsLog logger;
@Autowired
EventGridHandshakeHandler(JaxRsDpsLog logger, EventGridRequestBodyExtractor eventGridRequestBodyExtractor) {
this.logger = logger;
this.eventGridRequestBodyExtractor = eventGridRequestBodyExtractor;
}
......
......@@ -19,6 +19,7 @@ import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.notification.provider.azure.models.HandshakeRequestData;
import org.opengroup.osdu.notification.provider.azure.models.NotificationData;
......@@ -32,9 +33,7 @@ import org.springframework.web.context.annotation.RequestScope;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;
......@@ -44,6 +43,7 @@ import java.util.stream.Stream;
@Component
@RequestScope
public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtractor {
private static final String INVALID_EVENTGRID_MESSAGE = "Invalid Event Grid Message";
private static final String SUBSCRIPTION_ID = "Aeg-Subscription-Name";
private static final String EVENTGRID_VALIDATION_EVENT = "Microsoft.EventGrid.SubscriptionValidationEvent";
private static final Gson GSON = new Gson();
......@@ -51,6 +51,7 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto
private final JsonObject root = null;
private final HttpServletRequest httpServletRequest;
private final JaxRsDpsLog logger;
private final NotificationRequest notificationRequest;
private NotificationData notificationData;
......@@ -58,27 +59,22 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto
private boolean isHandshakeRequest;
@Autowired
public EventGridRequestBodyExtractor(HttpServletRequest httpServletRequest) {
public EventGridRequestBodyExtractor(HttpServletRequest httpServletRequest, JaxRsDpsLog log) {
this.httpServletRequest = httpServletRequest;
this.logger = log;
this.notificationRequest = extractNotificationRequestFromHttpRequest();
}
/**
* 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(isHandshakeRequest) {
// Adding default values. These are added for handshake request as Handshakes can't have custom values
// Having correlation-id and data-partition-id is mandatory for each request
// TODO: there should be a clean way of getting a partition. As of now, since everything is hardcoded for opendes,
// this will not break the system
HashMap<String, String> map= new HashMap<String, String> () ;
map.put("correlation-id", UUID.randomUUID().toString());
map.put("data-partition-id", "opendes");
return map;
logger.error("Invalid Event Grid Message. Is a handshake request");
return null;
}
return this.notificationData.getAttributes();
}
......@@ -86,10 +82,12 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto
/**
* 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(isHandshakeRequest) {
logger.error("Invalid Event Grid Message. Is a handshake request");
return null;
}
return new String(Base64.getDecoder().decode(notificationData.getData()));
......@@ -98,11 +96,13 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto
/**
* Extracts the notificationId from the request that are filled in by EventGrid.
*
* @throws AppException
* @return Request NotificationId String.
*/
public String extractNotificationIdFromRequestBody() {
String subscriptionId = httpServletRequest.getHeader(SUBSCRIPTION_ID);
if (Strings.isNullOrEmpty(subscriptionId)) {
logger.error("Invalid Event Grid Message. Subscription Id is null or empty");
throw new AppException(HttpStatus.BAD_REQUEST.value(), "Invalid Event Grid Message", "Subscription ID not found");
}
return subscriptionId;
......@@ -111,6 +111,7 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto
/**
* Checks if the request is for handshake.
*
* @throws AppException
* @return Request Type Boolean
*/
public boolean isHandshakeRequest() {
......@@ -120,10 +121,12 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto
/**
* Return ValidationCode
*
* @throws AppException
* @return Request Type Boolean
*/
public String getValidationCodeForHandshake() {
if(!isHandshakeRequest) {
logger.error("Invalid Event Grid Message. Is not a handshake request");
return null;
}
return this.handshakeRequestData.getValidationCode();
......@@ -135,6 +138,7 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto
*
* The attributes are validated.
*
* @throws AppException
* @return NotificationRequest Object
*/
private NotificationRequest extractNotificationRequestFromHttpRequest() {
......@@ -154,6 +158,7 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto
extractNotificationData(notificationRequest);
}
} catch (Exception e) {
logger.error("Invalid Event Grid Message. %s", e.getMessage());
throw new AppException(HttpStatus.BAD_REQUEST.value(), "Request payload parsing error",
"Unable to parse request payload.", "Request contents are null or empty");
}
......
......@@ -17,8 +17,10 @@ package org.opengroup.osdu.notification.provider.azure;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
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.notification.provider.azure.pubsub.EventGridRequestBodyExtractor;
import org.springframework.http.HttpStatus;
......@@ -30,6 +32,7 @@ import java.io.StringReader;
import java.util.Map;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
......@@ -40,6 +43,9 @@ public class EventGridRequestBodyExtractorTest {
@Mock
private HttpServletRequest httpServletRequest;
@Mock
private JaxRsDpsLog log;
@Test
public void should_returnTrue_isHandshakeRequest() throws IOException {
// Set up
......@@ -59,7 +65,7 @@ public class EventGridRequestBodyExtractorTest {
" }]";
BufferedReader reader = new BufferedReader(new StringReader(validHandshakeRequestRoot));
when(this.httpServletRequest.getReader()).thenReturn(reader);
sut = new EventGridRequestBodyExtractor(httpServletRequest);
sut = new EventGridRequestBodyExtractor(httpServletRequest, log);
// Act
boolean response = this.sut.isHandshakeRequest();
......@@ -90,7 +96,7 @@ public class EventGridRequestBodyExtractorTest {
try{
// Act
sut = new EventGridRequestBodyExtractor(httpServletRequest);
sut = new EventGridRequestBodyExtractor(httpServletRequest, log);
// Assert
fail("Should Throw Exception");
......@@ -122,7 +128,7 @@ public class EventGridRequestBodyExtractorTest {
try{
// Act
sut = new EventGridRequestBodyExtractor(httpServletRequest);
sut = new EventGridRequestBodyExtractor(httpServletRequest, log);
// Asset
fail("Should Throw Exception");
......@@ -157,7 +163,7 @@ public class EventGridRequestBodyExtractorTest {
try{
// Act
sut = new EventGridRequestBodyExtractor(httpServletRequest);
sut = new EventGridRequestBodyExtractor(httpServletRequest, log);
// Asset
fail("Should Throw Exception");
......@@ -192,7 +198,7 @@ public class EventGridRequestBodyExtractorTest {
try{
// Act
sut = new EventGridRequestBodyExtractor(httpServletRequest);
sut = new EventGridRequestBodyExtractor(httpServletRequest, log);
// Asset
fail("Should Throw Exception");
......@@ -229,7 +235,7 @@ public class EventGridRequestBodyExtractorTest {
BufferedReader reader = new BufferedReader(new StringReader(vaidRequestRoot));
when(this.httpServletRequest.getReader()).thenReturn(reader);
sut = new EventGridRequestBodyExtractor(httpServletRequest);
sut = new EventGridRequestBodyExtractor(httpServletRequest, log);
// Act
String receivedData = this.sut.extractDataFromRequestBody();
......@@ -240,7 +246,7 @@ public class EventGridRequestBodyExtractorTest {
@Test
public void should_returnValidAttributes_extractDataFromRequestBody() throws IOException {
String validRequestRoot = "[{\n" +
String vaidRequestRoot = "[{\n" +
" \"id\": \"2425\",\n" +
" \"eventType\": \"recordInserted\",\n" +
" \"subject\": \"myapp/vehicles/motorcycles\",\n" +
......@@ -257,9 +263,9 @@ public class EventGridRequestBodyExtractorTest {
" \"eventTime\": \"2020-08-14T18:04:12+00:00\",\n" +
" \"topic\": \"/subscriptions/asdf/resourceGroups/komakkar-OSDU-RG/providers/Microsoft.EventGrid/topics/recordChanged\"\n" +
" }]";
BufferedReader reader = new BufferedReader(new StringReader(validRequestRoot));
BufferedReader reader = new BufferedReader(new StringReader(vaidRequestRoot));
when(this.httpServletRequest.getReader()).thenReturn(reader);
sut = new EventGridRequestBodyExtractor(httpServletRequest);
sut = new EventGridRequestBodyExtractor(httpServletRequest, log);
// Act
Map<String, String> observedAttributes = this.sut.extractAttributesFromRequestBody();
......@@ -289,7 +295,7 @@ public class EventGridRequestBodyExtractorTest {
String expectedResponse = "testValidationCode";
BufferedReader reader = new BufferedReader(new StringReader(validHandshakeRequestRoot));
when(this.httpServletRequest.getReader()).thenReturn(reader);
sut = new EventGridRequestBodyExtractor(httpServletRequest);
sut = new EventGridRequestBodyExtractor(httpServletRequest, log);
// Act
String observedResponse = this.sut.getValidationCodeForHandshake();
......@@ -322,7 +328,7 @@ public class EventGridRequestBodyExtractorTest {
String expectedResponse = null;
BufferedReader reader = new BufferedReader(new StringReader(validHandshakeRequestRoot));
when(this.httpServletRequest.getReader()).thenReturn(reader);
sut = new EventGridRequestBodyExtractor(httpServletRequest);
sut = new EventGridRequestBodyExtractor(httpServletRequest, log);
// Act
String observedResponse = this.sut.getValidationCodeForHandshake();
......@@ -332,7 +338,7 @@ public class EventGridRequestBodyExtractorTest {
}
@Test
public void should_getattributesHandshakeRequest_extractDataFromRequestBody() throws IOException {
public void should_throwWhenHandshakeRequest_extractDataFromRequestBody() throws IOException {
String inVaidRequestRoot = " [{\n" +
" \"id\": \"testId\",\n" +
" \"topic\": \"testTopic\",\n" +
......@@ -348,14 +354,12 @@ public class EventGridRequestBodyExtractorTest {
" }]";
BufferedReader reader = new BufferedReader(new StringReader(inVaidRequestRoot));
when(this.httpServletRequest.getReader()).thenReturn(reader);
sut = new EventGridRequestBodyExtractor(httpServletRequest);
sut = new EventGridRequestBodyExtractor(httpServletRequest, log);
// Act
Map<String, String> observedAttributes = this.sut.extractAttributesFromRequestBody();
// Assert
Assert.assertNotNull(observedAttributes.get("correlation-id"));
Assert.assertEquals(observedAttributes.get("data-partition-id"),"opendes");
Assert.assertNull(observedAttributes);
}
@Test
......@@ -381,7 +385,7 @@ public class EventGridRequestBodyExtractorTest {
BufferedReader reader = new BufferedReader(new StringReader(vaidRequestRoot));
when(this.httpServletRequest.getReader()).thenReturn(reader);
when(this.httpServletRequest.getHeader("Aeg-Subscription-Name")).thenReturn("NotificationId");
sut = new EventGridRequestBodyExtractor(httpServletRequest);
sut = new EventGridRequestBodyExtractor(httpServletRequest, log);
// Act
String observed = sut.extractNotificationIdFromRequestBody();
......
// Copyright © Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
......@@ -21,11 +22,13 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
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.notification.provider.azure.pubsub.EventGridHandshakeHandler;
import org.opengroup.osdu.notification.provider.azure.pubsub.EventGridRequestBodyExtractor;
import org.springframework.http.HttpStatus;
import java.io.IOException;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.when;
......@@ -35,6 +38,9 @@ public class EventGridHandshakeHandlerTest {
@Mock
private EventGridRequestBodyExtractor eventGridRequestBodyExtractor;
@Mock
private JaxRsDpsLog logger;
@InjectMocks
@Spy
private EventGridHandshakeHandler sut;
......
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