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)'