From fc66a2c2647dd1c26d3e3291b3da53bd0ff5a536 Mon Sep 17 00:00:00 2001 From: Susanta Biswas Date: Thu, 19 Jun 2025 13:57:48 +0530 Subject: [PATCH 1/4] MySql Java E2E tests --- .../Constants.cs | 3 + .../MySqlEndToEndTests.cs | 44 +++++++++++++ endtoendtests/local.settings.json | 1 + endtoendtests/pom.xml | 1 + .../functions/endtoend/MySqlTriggerTests.java | 62 +++++++++++++++++++ .../official/jobs/run-e2e-tests-linux.yml | 1 + .../official/jobs/run-e2e-tests-windows.yml | 1 + 7 files changed, 113 insertions(+) create mode 100644 endtoendtests/Azure.Functions.Java.Tests.E2E/Azure.Functions.Java.Tests.E2E/MySqlEndToEndTests.cs create mode 100644 endtoendtests/src/main/java/com/microsoft/azure/functions/endtoend/MySqlTriggerTests.java 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..c84f1180 --- /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("AddProduct", productString, HttpStatusCode.OK)); + + // Read row from Products table using MySqlInput + Assert.True(await Utilities.InvokeHttpTrigger("GetProducts", "/" + 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..173fa41b 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 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..7a047a9f --- /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("GetProducts") + public HttpResponseMessage GetProducts(@HttpTrigger(name = "req", methods = { HttpMethod.GET, + HttpMethod.POST }, route = "getproducts/{productid}", authLevel = AuthorizationLevel.ANONYMOUS) + HttpRequestMessage> request, + @MySqlInput(name = "products", commandText = "SELECT TOP 1 * 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("AddProduct") + 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 From 205f84f53ae1ee08503a782bfaac731016ed46cc Mon Sep 17 00:00:00 2001 From: Susanta Biswas Date: Fri, 20 Jun 2025 11:42:50 +0530 Subject: [PATCH 2/4] added mysql dependency --- endtoendtests/pom.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/endtoendtests/pom.xml b/endtoendtests/pom.xml index 173fa41b..2ef18467 100644 --- a/endtoendtests/pom.xml +++ b/endtoendtests/pom.xml @@ -113,6 +113,11 @@ spring-cloud-starter-function-web provided + + com.microsoft.azure.functions + azure-functions-java-library-mysql + ${azure.functions.java.library.mysql.version} + From 52addd1d34b775334bc1a002b761ec7686944fb9 Mon Sep 17 00:00:00 2001 From: Susanta Biswas Date: Fri, 20 Jun 2025 11:43:19 +0530 Subject: [PATCH 3/4] updated mysql function name --- .../Azure.Functions.Java.Tests.E2E/MySqlEndToEndTests.cs | 4 ++-- .../microsoft/azure/functions/endtoend/MySqlTriggerTests.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) 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 index c84f1180..4d5e46fb 100644 --- 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 @@ -35,10 +35,10 @@ public async Task MySqlInput_Output_Succeeds() var productString = JsonConvert.SerializeObject(product); // Insert a row into Products table using MySqlOutput - Assert.True(await Utilities.InvokeHttpTriggerPost("AddProduct", productString, HttpStatusCode.OK)); + Assert.True(await Utilities.InvokeHttpTriggerPost("AddProductMySql", productString, HttpStatusCode.OK)); // Read row from Products table using MySqlInput - Assert.True(await Utilities.InvokeHttpTrigger("GetProducts", "/" + id.ToString(), HttpStatusCode.OK, productString)); + Assert.True(await Utilities.InvokeHttpTrigger("GetProductsMySql", "/" + id.ToString(), HttpStatusCode.OK, productString)); } } } \ No newline at end of file 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 index 7a047a9f..c6d0f29d 100644 --- a/endtoendtests/src/main/java/com/microsoft/azure/functions/endtoend/MySqlTriggerTests.java +++ b/endtoendtests/src/main/java/com/microsoft/azure/functions/endtoend/MySqlTriggerTests.java @@ -18,7 +18,7 @@ */ public class MySqlTriggerTests { - @FunctionName("GetProducts") + @FunctionName("GetProductsMySql") public HttpResponseMessage GetProducts(@HttpTrigger(name = "req", methods = { HttpMethod.GET, HttpMethod.POST }, route = "getproducts/{productid}", authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage> request, @@ -37,7 +37,7 @@ public HttpResponseMessage GetProducts(@HttpTrigger(name = "req", methods = { Ht } } - @FunctionName("AddProduct") + @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, From f91c136f65b19b0e9d641b17c6a4474ec1f07ab1 Mon Sep 17 00:00:00 2001 From: Susanta Biswas Date: Fri, 20 Jun 2025 14:47:36 +0530 Subject: [PATCH 4/4] fix: get query --- .../microsoft/azure/functions/endtoend/MySqlTriggerTests.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index c6d0f29d..1f71bdf1 100644 --- a/endtoendtests/src/main/java/com/microsoft/azure/functions/endtoend/MySqlTriggerTests.java +++ b/endtoendtests/src/main/java/com/microsoft/azure/functions/endtoend/MySqlTriggerTests.java @@ -22,7 +22,7 @@ public class MySqlTriggerTests { public HttpResponseMessage GetProducts(@HttpTrigger(name = "req", methods = { HttpMethod.GET, HttpMethod.POST }, route = "getproducts/{productid}", authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage> request, - @MySqlInput(name = "products", commandText = "SELECT TOP 1 * FROM Products WHERE ProductId = @ProductId", + @MySqlInput(name = "products", commandText = "SELECT * FROM Products WHERE ProductId = @ProductId", commandType = CommandType.Text, parameters = "@ProductId={productid}", connectionStringSetting = "AzureWebJobsMySqlConnectionString") Product[] products, final ExecutionContext context) {