Adding new E2E tests for EventGrid trigger and output bindings for Java

Fix the JSON Parsing issue
This commit is contained in:
Varad Meru [gmail] 2020-07-23 12:59:10 -07:00 коммит произвёл Ahmed El Sayed
Родитель e6bf3ad25d
Коммит c7917da6c3
11 изменённых файлов: 294 добавлений и 4 удалений

3
.gitignore поставляемый
Просмотреть файл

@ -270,3 +270,6 @@ pkg/
/Azure.Functions.Cli.zip
/Azure.Functions.Cli/
/version.txt
# Mac specific gitignore
.DS_Store

Просмотреть файл

@ -50,6 +50,8 @@ steps:
BrokerList": $(BrokerList)
ConfluentCloudUsername: $(ConfluentCloudUsername)
ConfluentCloudPassword: $(ConfluentCloudPassword)
AzureWebJobsEventGridOutputBindingTopicUriString: $(AzureWebJobsEventGridOutputBindingTopicUriString)
AzureWebJobsEventGridOutputBindingTopicKeyString: $(AzureWebJobsEventGridOutputBindingTopicKeyString)
displayName: 'Build & Run tests for java 8'
continueOnError: false
- pwsh: |
@ -69,6 +71,8 @@ steps:
BrokerList": $(BrokerList)
ConfluentCloudUsername: $(ConfluentCloudUsername)
ConfluentCloudPassword: $(ConfluentCloudPassword)
AzureWebJobsEventGridOutputBindingTopicUriString: $(AzureWebJobsEventGridOutputBindingTopicUriString)
AzureWebJobsEventGridOutputBindingTopicKeyString: $(AzureWebJobsEventGridOutputBindingTopicKeyString)
displayName: 'Build & Run tests for java 8 Customer jar loaded first'
continueOnError: false
- pwsh: |
@ -99,6 +103,8 @@ steps:
BrokerList": $(BrokerList)
ConfluentCloudUsername: $(ConfluentCloudUsername)
ConfluentCloudPassword: $(ConfluentCloudPassword)
AzureWebJobsEventGridOutputBindingTopicUriString: $(AzureWebJobsEventGridOutputBindingTopicUriString)
AzureWebJobsEventGridOutputBindingTopicKeyString: $(AzureWebJobsEventGridOutputBindingTopicKeyString)
displayName: 'Build & Run tests for java 11'
continueOnError: false
- task: CopyFiles@2

Просмотреть файл

@ -51,6 +51,8 @@ steps:
BrokerList": $(BrokerList)
ConfluentCloudUsername: $(ConfluentCloudUsername)
ConfluentCloudPassword: $(ConfluentCloudPassword)
AzureWebJobsEventGridOutputBindingTopicUriString: $(AzureWebJobsEventGridOutputBindingTopicUriString)
AzureWebJobsEventGridOutputBindingTopicKeyString: $(AzureWebJobsEventGridOutputBindingTopicKeyString)
displayName: 'running tests'
continueOnError: true
- pwsh: |

Просмотреть файл

@ -24,6 +24,8 @@ steps:
BrokerList": $(BrokerList)
ConfluentCloudUsername: $(ConfluentCloudUsername)
ConfluentCloudPassword: $(ConfluentCloudPassword)
AzureWebJobsEventGridOutputBindingTopicUriString: $(AzureWebJobsEventGridOutputBindingTopicUriString)
AzureWebJobsEventGridOutputBindingTopicKeyString: $(AzureWebJobsEventGridOutputBindingTopicKeyString)
displayName: 'running tests'
continueOnError: true
- task: CopyFiles@2

Просмотреть файл

@ -9,6 +9,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="2.1.1" />
<PackageReference Include="Microsoft.Azure.EventHubs" Version="2.2.1" />
<PackageReference Include="Microsoft.Azure.Management.EventGrid" Version="6.0.0" />
<PackageReference Include="Microsoft.Azure.EventGrid" Version="3.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />

Просмотреть файл

@ -56,6 +56,9 @@ namespace Azure.Functions.Java.Tests.E2E
// Kafka
public static string OutputStringOneKafkaQueueName = "test-kafka-output-cardinality-one-java";
// EventGrid
public static string EventGridStorageOutputBindingQueueName = "test-eventgrid-output-binding-queue-java";
// Settings
public static string EventHubsConnectionStringSenderSetting = Environment.GetEnvironmentVariable("AzureWebJobsEventHubSender");
public static string EventHubsConnectionStringSenderSetting2 = Environment.GetEnvironmentVariable("AzureWebJobsEventHubSender_2");

Просмотреть файл

@ -0,0 +1,106 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using Newtonsoft.Json.Linq;
using System;
using System.Net;
using System.Threading.Tasks;
using Xunit;
namespace Azure.Functions.Java.Tests.E2E
{
[Collection(Constants.FunctionAppCollectionName)]
public class EventGridEndToEndTests
{
private readonly FunctionAppFixture _fixture;
public EventGridEndToEndTests(FunctionAppFixture fixture)
{
_fixture = fixture;
}
[Fact]
public async Task TestEventGridTrigger()
{
/* Testing EventGrid Triggers
*
* To test EventGrid triggers - we send in a http message to function with
* EventGrid trigger and expect a 202 back. The header specifies EG that
* it is a `Notification`.
*/
string json = @"{
'topic': ' /subscriptions/SomSub/resourcegroups/SomeRG/providers/Microsoft.EventHub/namespaces/testeventhub',
'subject': 'eventhubs/test',
'eventType': 'captureFileCreated',
'eventTime': '2017-07-14T23:10:27.7689666Z',
'id': '7b11c4ce-0000-0000-9999-1730e766f126',
'data': {
'fileUrl': 'https://test.blob.core.windows.net/debugging/testblob.txt',
'fileType': 'AzureBlockBlob',
'partitionId': '1',
'sizeInBytes': 0,
'eventCount': 0,
'firstSequenceNumber': -1,
'lastSequenceNumber': -1,
'firstEnqueueTime': '0001-01-01T00:00:00',
'lastEnqueueTime': '0001-01-01T00:00:00'
},
'dataVersion': '',
'metadataVersion': '1'
}";
// Create json for body
var content = JObject.Parse(json);
Console.WriteLine($"TestEventGridTrigger's JSON Content: {content}");
Assert.True(await Utilities.InvokeEventGridTrigger("EventGridTriggerJava", content));
}
[Fact]
public async Task TestEventGridOutputBinding()
{
/* Testing EventGrid output binding
*
* EventGrid function `EventGridOutputBindingJava` is
* triggered using the http trigger, passing in a guid to it, and an
* event is sent to EventGrid. It forwards the event to queue and we
* read the queue contents.
*
* Verify that the following event grid and queue are created:
* - Constants.EventGridOutputBindingTopicUriSetting (Azure Devops will have the information for this).
* - Constants.EventGridStorageOutputBindingQueueName
*
* Also verify that the queue is subscribed to the events in that eventgrid.
*/
string expectedEventId = Guid.NewGuid().ToString();
try
{
// clearing up queue for event grid to send event to.
await SetupQueue(Constants.EventGridStorageOutputBindingQueueName);
Assert.True(await Utilities.InvokeHttpTrigger("EventGridOutputBindingJava",
$"?&testuuid={expectedEventId}", HttpStatusCode.OK, null));
var queueMessage = await StorageHelpers.ReadFromQueue(
Constants.EventGridStorageOutputBindingQueueName);
Assert.Contains(expectedEventId, queueMessage);
}
finally
{
// Clear queue for next run
await StorageHelpers.ClearQueue(Constants.EventGridStorageOutputBindingQueueName);
}
}
private static async Task SetupQueue(string queueName)
{
//Clear queue
await StorageHelpers.ClearQueue(queueName);
//Set up and trigger
await StorageHelpers.CreateQueue(queueName);
}
}
}

Просмотреть файл

@ -0,0 +1,45 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Azure.EventGrid;
using Microsoft.Azure.EventGrid.Models;
namespace Azure.Functions.Java.Tests.E2E.Helpers
{
public class EventGridHelpers
{
private static EventGridClient _eventGridClient;
private static string topicHostname = null;
public EventGridHelpers(string eventGridTopicEndpoint, string eventGridTopicKey)
{
TopicCredentials topicCredentials = new TopicCredentials(eventGridTopicKey);
_eventGridClient = new EventGridClient(topicCredentials);
topicHostname = new Uri(eventGridTopicEndpoint).Host;
}
public async Task SendEventAsync(string eventId, string subject,
JObject data, string eventType, DateTime eventDateTime, string dataVersion)
{
List<EventGridEvent> events = new List<EventGridEvent>();
var eventGridEvent = new EventGridEvent(eventId, subject, data,
eventType, eventDateTime, dataVersion);
events.Add(eventGridEvent);
await _eventGridClient.PublishEventsAsync(topicHostname, events);
}
public static async Task SendEventsAsync(List<EventGridEvent> eventGridEvents)
{
await _eventGridClient.PublishEventsAsync(topicHostname, eventGridEvents);
}
}
}

Просмотреть файл

@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
@ -7,6 +7,8 @@ using System.Threading.Tasks;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using Newtonsoft.Json.Linq;
namespace Azure.Functions.Java.Tests.E2E
{
@ -55,5 +57,31 @@ namespace Azure.Functions.Java.Tests.E2E
}
return true;
}
public static async Task<bool> InvokeEventGridTrigger(string functionName, JObject jsonContent, HttpStatusCode expectedStatusCode=HttpStatusCode.Accepted)
{
string uri = $"runtime/webhooks/eventgrid?functionName={functionName}";
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, uri);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Add("aeg-event-type", "Notification");
request.Content = new StringContent(
jsonContent.ToString(),
Encoding.UTF8,
"application/json"
);
var httpClient = new HttpClient {
BaseAddress = new Uri(Constants.FunctionsHostUrl)
};
var response = await httpClient.SendAsync(request);
if (expectedStatusCode != response.StatusCode) {
Console.WriteLine($"InvokeEventGridTrigger: expectedStatusCode : {expectedStatusCode}, actualMessage : {response.StatusCode}");
return false;
}
return true;
}
}
}

Просмотреть файл

@ -15,6 +15,8 @@
"SBTopicSubName":"",
"SBQueueName":"",
"AzureWebJobsCosmosDBConnectionString":"",
"AzureWebJobsEventGridOutputBindingTopicUriString": "",
"AzureWebJobsEventGridOutputBindingTopicKeyString": "",
"FUNCTIONS_WORKER_RUNTIME": "java"
}
}

Просмотреть файл

@ -2,6 +2,7 @@ package com.microsoft.azure.functions.endtoend;
import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.*;
import java.util.*;
/**
* Azure Functions with Event Grid trigger.
@ -12,10 +13,100 @@ public class EventGridTriggerTests {
*/
@FunctionName("EventGridTriggerJava")
public void eventGridHandler(
@EventGridTrigger(name = "eventgrid") String message,
@EventGridTrigger(name = "eventgrid") String eventContent,
final ExecutionContext context
) {
context.getLogger().info("Java Event Grid trigger function executed.");
context.getLogger().info(message);
context.getLogger().info(eventContent);
}
/**
* This function will be invoked when a http trigger is received, and sends a custom event to Event Grid.
*/
@FunctionName("EventGridOutputBindingJava")
public HttpResponseMessage eventGridOutputBinding(
@HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST},
authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
@EventGridOutput(name = "outputEvent",
topicEndpointUri = "AzureWebJobsEventGridOutputBindingTopicUriString",
topicKeySetting = "AzureWebJobsEventGridOutputBindingTopicKeyString") OutputBinding<EventGridEvent> outputEvent,
final ExecutionContext context
) {
context.getLogger().info("Java HTTP trigger processed a request.");
// Parse query parameter
String query = request.getQueryParameters().get("testuuid");
String message = request.getBody().orElse(query);
context.getLogger().info("testuuid:" + message);
final EventGridEvent eventGridOutputDocument = new EventGridEvent();
eventGridOutputDocument.setId("test-id");
eventGridOutputDocument.setEventType("test-event-1");
eventGridOutputDocument.setEventTime("2020-01-31T10:10:10+00:00");
eventGridOutputDocument.setDataVersion("1.0");
eventGridOutputDocument.setSubject("test-subject");
eventGridOutputDocument.setData("test-uuid: " + message);
outputEvent.setValue(eventGridOutputDocument);
return request.createResponseBuilder(HttpStatus.OK).build();
}
}
class EventGridEvent {
private String id;
private String eventType;
private String subject;
private String eventTime;
private String dataVersion;
private String data;
public String getId() {
return id;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public String getDataVersion() {
return dataVersion;
}
public void setDataVersion(String dataVersion) {
this.dataVersion = dataVersion;
}
public String getEventTime() {
return eventTime;
}
public void setEventTime(String eventTime) {
this.eventTime = eventTime;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getEventType() {
return eventType;
}
public void setEventType(String eventType) {
this.eventType = eventType;
}
public void setId(String id) {
this.id = id;
}
}