Skip to content

Commit 0fe7556

Browse files
committed
Tests for mysql extensions
1 parent 5d29dea commit 0fe7556

File tree

7 files changed

+117
-0
lines changed

7 files changed

+117
-0
lines changed

azure-pipelines-e2e-integration-tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ jobs:
149149
JAVA_HOME: $(JavaHome)
150150
AzureWebJobsStorage: $(AzureWebJobsStorage)
151151
AzureWebJobsCosmosDBConnectionString: $(AzureWebJobsCosmosDBConnectionString)
152+
AzureWebJobsMySqlConnectionString: $(AzureWebJobsMySqlConnectionString)
152153
AzureWebJobsSqlConnectionString: $(AzureWebJobsSqlConnectionString)
153154
AzureWebJobsServiceBus: $(AzureWebJobsServiceBus)
154155
AzureWebJobsEventHubSender_2: $(AzureWebJobsEventHubSender_2)

azure-pipelines.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ jobs:
215215
JAVA_HOME: $(JavaHome)
216216
AzureWebJobsStorage: $(AzureWebJobsStorage)
217217
AzureWebJobsCosmosDBConnectionString: $(AzureWebJobsCosmosDBConnectionString)
218+
AzureWebJobsMySqlConnectionString: $(AzureWebJobsMySqlConnectionString)
218219
AzureWebJobsSqlConnectionString: $(AzureWebJobsSqlConnectionString)
219220
AzureWebJobsServiceBus: $(AzureWebJobsServiceBus)
220221
AzureWebJobsEventHubReceiver: $(AzureWebJobsEventHubReceiver)

endtoendtests/Azure.Functions.Java.Tests.E2E/Azure.Functions.Java.Tests.E2E/Constants.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,8 @@ public static class Constants
9696

9797
// SQL Binding tests
9898
public static string SqlConnectionStringSetting = Environment.GetEnvironmentVariable("AzureWebJobsSqlConnectionString");
99+
100+
// MySql tests
101+
public static string MySqlConnectionStringSetting = Environment.GetEnvironmentVariable("AzureWebJobsMySqlConnectionString");
99102
}
100103
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using Newtonsoft.Json;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Net;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
using Xunit;
11+
12+
namespace Azure.Functions.Java.Tests.E2E
13+
{
14+
[Collection(Constants.FunctionAppCollectionName)]
15+
public class MySqlEndToEndTests
16+
{
17+
private readonly FunctionAppFixture _fixture;
18+
19+
public MySqlEndToEndTests(FunctionAppFixture fixture)
20+
{
21+
this._fixture = fixture;
22+
}
23+
24+
[Fact]
25+
public async Task MySqlInput_Output_Succeeds()
26+
{
27+
TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1);
28+
int id = (int) t.TotalSeconds;
29+
var product = new Dictionary<string, object>()
30+
{
31+
{ "ProductId", id },
32+
{ "Name", "test" },
33+
{ "Cost", 100 }
34+
};
35+
36+
var productString = JsonConvert.SerializeObject(product);
37+
// Insert row into Products table using MySqlOutput
38+
Assert.True(await Utilities.InvokeHttpTriggerPost("AddProduct", productString, HttpStatusCode.OK));
39+
40+
// Read row from Products table using MySqlInput
41+
Assert.True(await Utilities.InvokeHttpTrigger("GetProducts", "/" + id.ToString(), HttpStatusCode.OK, productString));
42+
}
43+
}
44+
}

endtoendtests/local.settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"AzureWebJobsEventGridOutputBindingTopicUriString": "",
1919
"AzureWebJobsEventGridOutputBindingTopicKeyString": "",
2020
"AzureWebJobsSqlConnectionString": "",
21+
"AzureWebJobsMySqlConnectionString": "",
2122
"FUNCTIONS_WORKER_RUNTIME": "java"
2223
}
2324
}

endtoendtests/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<azure.functions.maven.plugin.version>1.18.0</azure.functions.maven.plugin.version>
2525
<azure.functions.java.library.version>3.1.0</azure.functions.java.library.version>
2626
<azure.functions.java.library.sql.version>2.1.0</azure.functions.java.library.sql.version>
27+
<azure.functions.java.library.mysql.version>1.0.0</azure.functions.java.library.mysql.version>
2728
<durabletask.azure.functions>1.0.0-beta.1</durabletask.azure.functions>
2829
<functionAppName>azure-functions-java-endtoendtests</functionAppName>
2930
<functionAppRegion>westus</functionAppRegion>
@@ -69,6 +70,11 @@
6970
<groupId>com.microsoft.azure.functions</groupId>
7071
<artifactId>azure-functions-java-library-sql</artifactId>
7172
<version>${azure.functions.java.library.sql.version}</version>
73+
</dependency>
74+
<dependency>
75+
<groupId>com.microsoft.azure.functions</groupId>
76+
<artifactId>azure-functions-java-library-mysql</artifactId>
77+
<version>${azure.functions.java.library.mysql.version}</version>
7278
</dependency>
7379
<dependency>
7480
<groupId>com.microsoft</groupId>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.microsoft.azure.functions.endtoend;
2+
3+
import com.microsoft.azure.functions.annotation.*;
4+
import com.google.gson.Gson;
5+
import com.microsoft.azure.functions.*;
6+
import com.microsoft.azure.functions.HttpMethod;
7+
import com.microsoft.azure.functions.mysql.annotation.CommandType;
8+
import com.microsoft.azure.functions.mysql.annotation.MySqlInput;
9+
import com.microsoft.azure.functions.mysql.annotation.MySqlOutput;
10+
11+
import java.io.IOException;
12+
import java.lang.reflect.Array;
13+
import java.util.*;
14+
15+
/**
16+
* Azure Functions with Azure MySql Database.
17+
*/
18+
public class MySqlTriggerTests {
19+
20+
@FunctionName("GetProducts")
21+
public HttpResponseMessage GetProducts(@HttpTrigger(name = "req", methods = { HttpMethod.GET,
22+
HttpMethod.POST }, route = "getproducts/{productid}", authLevel = AuthorizationLevel.ANONYMOUS)
23+
HttpRequestMessage<Optional<String>> request,
24+
@MySqlInput(name = "products", commandText = "SELECT TOP 1 * FROM Products WHERE ProductId = @ProductId",
25+
commandType = CommandType.Text, parameters = "@ProductId={productid}",
26+
connectionStringSetting = "AzureWebJobsMySqlConnectionString") Product[] products,
27+
final ExecutionContext context) {
28+
29+
context.getLogger().info("Java HTTP trigger processed a MySql Input Binding request.");
30+
31+
if (products.length != 0) {
32+
return request.createResponseBuilder(HttpStatus.OK).body(products[0].toString()).build();
33+
} else {
34+
return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
35+
.body("Did not find expected product in table Products").build();
36+
}
37+
}
38+
39+
@FunctionName("AddProduct")
40+
public HttpResponseMessage AddProduct(@HttpTrigger(name = "req", methods = { HttpMethod.GET,
41+
HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
42+
@MySqlOutput(name = "product", tableName = "Products", connectionStringSetting = "AzureWebJobsMySqlConnectionString") OutputBinding<Product> product,
43+
final ExecutionContext context) {
44+
context.getLogger().info("Java HTTP trigger processed a MySql Output Binding request.");
45+
46+
String json = request.getBody().get();
47+
product.setValue(new Gson().fromJson(json, Product.class));
48+
49+
return request.createResponseBuilder(HttpStatus.OK).body(product).build();
50+
}
51+
52+
public class Product {
53+
public int ProductId;
54+
public String Name;
55+
public int Cost;
56+
57+
public String toString() {
58+
return "{\"ProductId\":" + ProductId + ",\"Name\":\"" + Name + "\",\"Cost\":" + Cost + "}";
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)