diff --git a/endtoendtests/Azure.Functions.Java.Tests.E2E/Azure.Functions.Java.Tests.E2E/Constants.cs b/endtoendtests/Azure.Functions.Java.Tests.E2E/Azure.Functions.Java.Tests.E2E/Constants.cs index 4a0ecb63..d1708169 100644 --- a/endtoendtests/Azure.Functions.Java.Tests.E2E/Azure.Functions.Java.Tests.E2E/Constants.cs +++ b/endtoendtests/Azure.Functions.Java.Tests.E2E/Azure.Functions.Java.Tests.E2E/Constants.cs @@ -96,5 +96,8 @@ public static class Constants // SQL Binding tests public static string SqlConnectionStringSetting = Environment.GetEnvironmentVariable("AzureWebJobsSqlConnectionString"); + + // MySql tests + public static string MySqlConnectionStringSetting = Environment.GetEnvironmentVariable("AzureWebJobsMySqlConnectionString"); } } diff --git a/endtoendtests/Azure.Functions.Java.Tests.E2E/Azure.Functions.Java.Tests.E2E/MySqlEndToEndTests.cs b/endtoendtests/Azure.Functions.Java.Tests.E2E/Azure.Functions.Java.Tests.E2E/MySqlEndToEndTests.cs new file mode 100644 index 00000000..4d5e46fb --- /dev/null +++ b/endtoendtests/Azure.Functions.Java.Tests.E2E/Azure.Functions.Java.Tests.E2E/MySqlEndToEndTests.cs @@ -0,0 +1,44 @@ +// 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; +using System; +using System.Collections.Generic; +using System.Net; +using System.Threading; +using System.Threading.Tasks; +using Xunit; + +namespace Azure.Functions.Java.Tests.E2E +{ + [Collection(Constants.FunctionAppCollectionName)] + public class MySqlEndToEndTests + { + private readonly FunctionAppFixture _fixture; + + public MySqlEndToEndTests(FunctionAppFixture fixture) + { + this._fixture = fixture; + } + + [Fact] + public async Task MySqlInput_Output_Succeeds() + { + TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1); + int id = (int) t.TotalSeconds; + var product = new Dictionary() + { + { "ProductId", id }, + { "Name", "test" }, + { "Cost", 100 } + }; + + var productString = JsonConvert.SerializeObject(product); + // Insert a row into Products table using MySqlOutput + Assert.True(await Utilities.InvokeHttpTriggerPost("AddProductMySql", productString, HttpStatusCode.OK)); + + // Read row from Products table using MySqlInput + Assert.True(await Utilities.InvokeHttpTrigger("GetProductsMySql", "/" + id.ToString(), HttpStatusCode.OK, productString)); + } + } +} \ No newline at end of file diff --git a/endtoendtests/local.settings.json b/endtoendtests/local.settings.json index 5d4d5201..c526abab 100644 --- a/endtoendtests/local.settings.json +++ b/endtoendtests/local.settings.json @@ -18,6 +18,7 @@ "AzureWebJobsEventGridOutputBindingTopicUriString": "", "AzureWebJobsEventGridOutputBindingTopicKeyString": "", "AzureWebJobsSqlConnectionString": "", + "AzureWebJobsMySqlConnectionString": "", "FUNCTIONS_WORKER_RUNTIME": "java" } } diff --git a/endtoendtests/pom.xml b/endtoendtests/pom.xml index fce74258..2ef18467 100644 --- a/endtoendtests/pom.xml +++ b/endtoendtests/pom.xml @@ -25,6 +25,7 @@ 3.1.0 2.1.0 1.0.0-beta.1 + 1.0.2 azure-functions-java-endtoendtests westus java-functions-group @@ -112,6 +113,11 @@ spring-cloud-starter-function-web provided + + com.microsoft.azure.functions + azure-functions-java-library-mysql + ${azure.functions.java.library.mysql.version} + diff --git a/endtoendtests/src/main/java/com/microsoft/azure/functions/endtoend/MySqlTriggerTests.java b/endtoendtests/src/main/java/com/microsoft/azure/functions/endtoend/MySqlTriggerTests.java new file mode 100644 index 00000000..1f71bdf1 --- /dev/null +++ b/endtoendtests/src/main/java/com/microsoft/azure/functions/endtoend/MySqlTriggerTests.java @@ -0,0 +1,62 @@ +package com.microsoft.azure.functions.endtoend; + +import com.microsoft.azure.functions.annotation.*; +import com.google.gson.Gson; +import com.microsoft.azure.functions.*; +import com.microsoft.azure.functions.HttpMethod; +import com.microsoft.azure.functions.mysql.annotation.CommandType; +import com.microsoft.azure.functions.mysql.annotation.MySqlInput; +import com.microsoft.azure.functions.mysql.annotation.MySqlOutput; +import com.microsoft.azure.functions.mysql.annotation.MySqlTrigger; + +import java.io.IOException; +import java.lang.reflect.Array; +import java.util.*; + +/** + * Azure Functions with Azure MySql Database. + */ +public class MySqlTriggerTests { + + @FunctionName("GetProductsMySql") + public HttpResponseMessage GetProducts(@HttpTrigger(name = "req", methods = { HttpMethod.GET, + HttpMethod.POST }, route = "getproducts/{productid}", authLevel = AuthorizationLevel.ANONYMOUS) + HttpRequestMessage> request, + @MySqlInput(name = "products", commandText = "SELECT * FROM Products WHERE ProductId = @ProductId", + commandType = CommandType.Text, parameters = "@ProductId={productid}", + connectionStringSetting = "AzureWebJobsMySqlConnectionString") Product[] products, + final ExecutionContext context) { + + context.getLogger().info("Java HTTP trigger processed a MySql Input Binding request."); + + if (products.length != 0) { + return request.createResponseBuilder(HttpStatus.OK).body(products[0].toString()).build(); + } else { + return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR) + .body("Did not find expected product in table Products").build(); + } + } + + @FunctionName("AddProductMySql") + public HttpResponseMessage AddProduct(@HttpTrigger(name = "req", methods = { HttpMethod.GET, + HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage> request, + @MySqlOutput(name = "product", commandText = "Products", connectionStringSetting = "AzureWebJobsMySqlConnectionString") OutputBinding product, + final ExecutionContext context) { + context.getLogger().info("Java HTTP trigger processed a MySql Output Binding request."); + + String json = request.getBody().get(); + product.setValue(new Gson().fromJson(json, Product.class)); + + return request.createResponseBuilder(HttpStatus.OK).body(product).build(); + } + + public class Product { + public int ProductId; + public String Name; + public int Cost; + + public String toString() { + return "{\"ProductId\":" + ProductId + ",\"Name\":\"" + Name + "\",\"Cost\":" + Cost + "}"; + } + } +} \ No newline at end of file diff --git a/eng/ci/templates/official/jobs/run-e2e-tests-linux.yml b/eng/ci/templates/official/jobs/run-e2e-tests-linux.yml index 368007c9..52b9c76a 100644 --- a/eng/ci/templates/official/jobs/run-e2e-tests-linux.yml +++ b/eng/ci/templates/official/jobs/run-e2e-tests-linux.yml @@ -126,5 +126,6 @@ jobs: ApplicationInsightAPIKey: $(ApplicationInsightAPIKey) ApplicationInsightAPPID: $(ApplicationInsightAPPID) ApplicationInsightAgentVersion: $(ApplicationInsightAgentVersion) + AzureWebJobsMySqlConnectionString: $(AzureWebJobsMySqlConnectionString) displayName: 'Build & Run tests' continueOnError: false \ No newline at end of file diff --git a/eng/ci/templates/official/jobs/run-e2e-tests-windows.yml b/eng/ci/templates/official/jobs/run-e2e-tests-windows.yml index df27ff58..dc8342ce 100644 --- a/eng/ci/templates/official/jobs/run-e2e-tests-windows.yml +++ b/eng/ci/templates/official/jobs/run-e2e-tests-windows.yml @@ -111,5 +111,6 @@ jobs: ApplicationInsightAPIKey: $(ApplicationInsightAPIKey) ApplicationInsightAPPID: $(ApplicationInsightAPPID) ApplicationInsightAgentVersion: $(ApplicationInsightAgentVersion) + AzureWebJobsMySqlConnectionString: $(AzureWebJobsMySqlConnectionString) displayName: 'Build & Run tests' continueOnError: false \ No newline at end of file