From 62d1f256497ce8bf751937f8eba4c74aacc008c9 Mon Sep 17 00:00:00 2001 From: Sumit Kumar Date: Wed, 16 Apr 2025 15:06:44 +0530 Subject: [PATCH 1/3] E2E Tests of "MySql Extensions for Azure Function" on java platform --- .../Constants.cs | 3 + .../MySqlEndToEndTests.cs | 44 +++++++++++++ endtoendtests/local.settings.json | 1 + endtoendtests/pom.xml | 6 ++ .../functions/endtoend/MySqlTriggerTests.java | 62 +++++++++++++++++++ .../official/jobs/run-e2e-tests-linux.yml | 1 + .../official/jobs/run-e2e-tests-windows.yml | 1 + 7 files changed, 118 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..552d8cc1 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..acc00b0f 100644 --- a/endtoendtests/pom.xml +++ b/endtoendtests/pom.xml @@ -24,6 +24,7 @@ 1.18.0 3.1.0 2.1.0 + 1.0.2 1.0.0-beta.1 azure-functions-java-endtoendtests westus @@ -69,6 +70,11 @@ com.microsoft.azure.functions azure-functions-java-library-sql ${azure.functions.java.library.sql.version} + + + com.microsoft.azure.functions + azure-functions-java-library-mysql + ${azure.functions.java.library.mysql.version} com.microsoft 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..7335d2ae --- /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 + "}"; + } + } +} 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..936999c8 100644 --- a/eng/ci/templates/official/jobs/run-e2e-tests-linux.yml +++ b/eng/ci/templates/official/jobs/run-e2e-tests-linux.yml @@ -109,6 +109,7 @@ jobs: AzureWebJobsStorage: $(AzureWebJobsStorage) AzureWebJobsCosmosDBConnectionString: $(AzureWebJobsCosmosDBConnectionString) AzureWebJobsSqlConnectionString: $(AzureWebJobsSqlConnectionString) + AzureWebJobsMySqlConnectionString: $(AzureWebJobsMySqlConnectionString) AzureWebJobsServiceBus: $(AzureWebJobsServiceBus) AzureWebJobsEventHubReceiver: $(AzureWebJobsEventHubReceiver) AzureWebJobsEventHubSender_2: $(AzureWebJobsEventHubSender_2) 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..0c130319 100644 --- a/eng/ci/templates/official/jobs/run-e2e-tests-windows.yml +++ b/eng/ci/templates/official/jobs/run-e2e-tests-windows.yml @@ -94,6 +94,7 @@ jobs: AzureWebJobsStorage: $(AzureWebJobsStorage) AzureWebJobsCosmosDBConnectionString: $(AzureWebJobsCosmosDBConnectionString) AzureWebJobsSqlConnectionString: $(AzureWebJobsSqlConnectionString) + AzureWebJobsMySqlConnectionString: $(AzureWebJobsMySqlConnectionString) AzureWebJobsServiceBus: $(AzureWebJobsServiceBus) AzureWebJobsEventHubReceiver: $(AzureWebJobsEventHubReceiver) AzureWebJobsEventHubSender_2: $(AzureWebJobsEventHubSender_2) From 2fddb3436d23c71240b628b1c80bb7a021bd4bca Mon Sep 17 00:00:00 2001 From: Sumit Kumar Date: Thu, 17 Apr 2025 11:03:34 +0530 Subject: [PATCH 2/3] Revert "E2E Tests of "MySql Extensions for Azure Function" on java platform" This reverts commit 62d1f256497ce8bf751937f8eba4c74aacc008c9. --- .../Constants.cs | 3 - .../MySqlEndToEndTests.cs | 44 ------------- endtoendtests/local.settings.json | 1 - endtoendtests/pom.xml | 6 -- .../functions/endtoend/MySqlTriggerTests.java | 62 ------------------- .../official/jobs/run-e2e-tests-linux.yml | 1 - .../official/jobs/run-e2e-tests-windows.yml | 1 - 7 files changed, 118 deletions(-) delete mode 100644 endtoendtests/Azure.Functions.Java.Tests.E2E/Azure.Functions.Java.Tests.E2E/MySqlEndToEndTests.cs delete 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 552d8cc1..4a0ecb63 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,8 +96,5 @@ 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 deleted file mode 100644 index c84f1180..00000000 --- a/endtoendtests/Azure.Functions.Java.Tests.E2E/Azure.Functions.Java.Tests.E2E/MySqlEndToEndTests.cs +++ /dev/null @@ -1,44 +0,0 @@ -// 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 c526abab..5d4d5201 100644 --- a/endtoendtests/local.settings.json +++ b/endtoendtests/local.settings.json @@ -18,7 +18,6 @@ "AzureWebJobsEventGridOutputBindingTopicUriString": "", "AzureWebJobsEventGridOutputBindingTopicKeyString": "", "AzureWebJobsSqlConnectionString": "", - "AzureWebJobsMySqlConnectionString": "", "FUNCTIONS_WORKER_RUNTIME": "java" } } diff --git a/endtoendtests/pom.xml b/endtoendtests/pom.xml index acc00b0f..fce74258 100644 --- a/endtoendtests/pom.xml +++ b/endtoendtests/pom.xml @@ -24,7 +24,6 @@ 1.18.0 3.1.0 2.1.0 - 1.0.2 1.0.0-beta.1 azure-functions-java-endtoendtests westus @@ -70,11 +69,6 @@ com.microsoft.azure.functions azure-functions-java-library-sql ${azure.functions.java.library.sql.version} - - - com.microsoft.azure.functions - azure-functions-java-library-mysql - ${azure.functions.java.library.mysql.version} com.microsoft 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 deleted file mode 100644 index 7335d2ae..00000000 --- a/endtoendtests/src/main/java/com/microsoft/azure/functions/endtoend/MySqlTriggerTests.java +++ /dev/null @@ -1,62 +0,0 @@ -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 + "}"; - } - } -} 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 936999c8..368007c9 100644 --- a/eng/ci/templates/official/jobs/run-e2e-tests-linux.yml +++ b/eng/ci/templates/official/jobs/run-e2e-tests-linux.yml @@ -109,7 +109,6 @@ jobs: AzureWebJobsStorage: $(AzureWebJobsStorage) AzureWebJobsCosmosDBConnectionString: $(AzureWebJobsCosmosDBConnectionString) AzureWebJobsSqlConnectionString: $(AzureWebJobsSqlConnectionString) - AzureWebJobsMySqlConnectionString: $(AzureWebJobsMySqlConnectionString) AzureWebJobsServiceBus: $(AzureWebJobsServiceBus) AzureWebJobsEventHubReceiver: $(AzureWebJobsEventHubReceiver) AzureWebJobsEventHubSender_2: $(AzureWebJobsEventHubSender_2) 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 0c130319..df27ff58 100644 --- a/eng/ci/templates/official/jobs/run-e2e-tests-windows.yml +++ b/eng/ci/templates/official/jobs/run-e2e-tests-windows.yml @@ -94,7 +94,6 @@ jobs: AzureWebJobsStorage: $(AzureWebJobsStorage) AzureWebJobsCosmosDBConnectionString: $(AzureWebJobsCosmosDBConnectionString) AzureWebJobsSqlConnectionString: $(AzureWebJobsSqlConnectionString) - AzureWebJobsMySqlConnectionString: $(AzureWebJobsMySqlConnectionString) AzureWebJobsServiceBus: $(AzureWebJobsServiceBus) AzureWebJobsEventHubReceiver: $(AzureWebJobsEventHubReceiver) AzureWebJobsEventHubSender_2: $(AzureWebJobsEventHubSender_2) From bcfcc8541db0fbd19284f1d18ed59aa6ee80485a Mon Sep 17 00:00:00 2001 From: Sumit Kumar Date: Thu, 17 Apr 2025 11:04:43 +0530 Subject: [PATCH 3/3] update changes to emulated-tests-windows.yml --- .../Constants.cs | 3 + .../MySqlEndToEndTests.cs | 44 +++++++++++++ emulatedtests/pom.xml | 1 + .../functions/endtoend/MySqlTriggerTests.java | 62 +++++++++++++++++++ .../jobs/run-emulated-tests-linux.yml | 2 + .../jobs/run-emulated-tests-windows.yml | 2 + 6 files changed, 114 insertions(+) create mode 100644 emulatedtests/Azure.Functions.Java.Tests.E2E/Azure.Functions.Java.Tests.E2E/MySqlEndToEndTests.cs create mode 100644 emulatedtests/src/main/java/com/microsoft/azure/functions/endtoend/MySqlTriggerTests.java diff --git a/emulatedtests/Azure.Functions.Java.Tests.E2E/Azure.Functions.Java.Tests.E2E/Constants.cs b/emulatedtests/Azure.Functions.Java.Tests.E2E/Azure.Functions.Java.Tests.E2E/Constants.cs index be60e9ea..22561723 100644 --- a/emulatedtests/Azure.Functions.Java.Tests.E2E/Azure.Functions.Java.Tests.E2E/Constants.cs +++ b/emulatedtests/Azure.Functions.Java.Tests.E2E/Azure.Functions.Java.Tests.E2E/Constants.cs @@ -29,5 +29,8 @@ public static class Constants // Xunit Fixtures and Collections public const string FunctionAppCollectionName = "FunctionAppCollection"; + + // MySql tests + public static string MySqlConnectionStringSetting = Environment.GetEnvironmentVariable("AzureWebJobsMySqlConnectionString"); } } diff --git a/emulatedtests/Azure.Functions.Java.Tests.E2E/Azure.Functions.Java.Tests.E2E/MySqlEndToEndTests.cs b/emulatedtests/Azure.Functions.Java.Tests.E2E/Azure.Functions.Java.Tests.E2E/MySqlEndToEndTests.cs new file mode 100644 index 00000000..c84f1180 --- /dev/null +++ b/emulatedtests/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/emulatedtests/pom.xml b/emulatedtests/pom.xml index 6557ce14..77e8f34b 100644 --- a/emulatedtests/pom.xml +++ b/emulatedtests/pom.xml @@ -23,6 +23,7 @@ 1.8 1.37.1 3.1.0 + 1.0.2 2.1.0 1.0.0-beta.1 azure-functions-java-emulatedtests diff --git a/emulatedtests/src/main/java/com/microsoft/azure/functions/endtoend/MySqlTriggerTests.java b/emulatedtests/src/main/java/com/microsoft/azure/functions/endtoend/MySqlTriggerTests.java new file mode 100644 index 00000000..7335d2ae --- /dev/null +++ b/emulatedtests/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 + "}"; + } + } +} diff --git a/eng/ci/templates/jobs/run-emulated-tests-linux.yml b/eng/ci/templates/jobs/run-emulated-tests-linux.yml index bb6b5add..26417225 100644 --- a/eng/ci/templates/jobs/run-emulated-tests-linux.yml +++ b/eng/ci/templates/jobs/run-emulated-tests-linux.yml @@ -122,6 +122,7 @@ jobs: arguments: '--filter "Category!=SdkTypes"' env: JAVA_HOME: $(JavaHome) + AzureWebJobsMySqlConnectionString: $(AzureWebJobsMySqlConnectionString) AzureWebJobsStorage: "UseDevelopmentStorage=true" displayName: 'Build & Run tests' continueOnError: false @@ -166,6 +167,7 @@ jobs: emulatedtests/Azure.Functions.Java.Tests.E2E/Azure.Functions.Java.Tests.E2E/Azure.Functions.Java.Tests.E2E.csproj env: JAVA_HOME: $(JavaHome) + AzureWebJobsMySqlConnectionString: $(AzureWebJobsMySqlConnectionString) AzureWebJobsStorage: "UseDevelopmentStorage=true" ENABLE_SDK_TYPES: "true" displayName: 'Build & Run tests' diff --git a/eng/ci/templates/jobs/run-emulated-tests-windows.yml b/eng/ci/templates/jobs/run-emulated-tests-windows.yml index 0c32775e..b65282d9 100644 --- a/eng/ci/templates/jobs/run-emulated-tests-windows.yml +++ b/eng/ci/templates/jobs/run-emulated-tests-windows.yml @@ -106,6 +106,7 @@ jobs: arguments: '--filter "Category!=SdkTypes"' env: JAVA_HOME: $(JavaHome) + AzureWebJobsMySqlConnectionString: $(AzureWebJobsMySqlConnectionString) AzureWebJobsStorage: "UseDevelopmentStorage=true" ENABLE_SDK_TYPES: "true" displayName: 'Build & Run tests' @@ -150,6 +151,7 @@ jobs: emulatedtests/Azure.Functions.Java.Tests.E2E/Azure.Functions.Java.Tests.E2E/Azure.Functions.Java.Tests.E2E.csproj env: JAVA_HOME: $(JavaHome) + AzureWebJobsMySqlConnectionString: $(AzureWebJobsMySqlConnectionString) AzureWebJobsStorage: "UseDevelopmentStorage=true" ENABLE_SDK_TYPES: "true" displayName: 'Build & Run tests (SDK types)'