-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add end to end test for SQL Bindings (#742)
* add sql e2e test * remove sql trigger test * check sql input result * update sql java library version to 2.1.0 * remove sql trigger function * remove GetProducts2 * Updating library version. Fixes merge conflict --------- Co-authored-by: Lucy Zhang <luczhan@microsoft.com> Co-authored-by: Shreyas Gopalakrishna <shreyasg@microsoft.com> Co-authored-by: Shreyas Gopalakrishna <11889130+shreyas-gopalakrishna@users.noreply.github.com>
- Loading branch information
1 parent
a298511
commit bccab20
Showing
8 changed files
with
148 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...ndtests/Azure.Functions.Java.Tests.E2E/Azure.Functions.Java.Tests.E2E/SqlEndToEndTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 SqlEndToEndTests | ||
{ | ||
private readonly FunctionAppFixture _fixture; | ||
|
||
public SqlEndToEndTests(FunctionAppFixture fixture) | ||
{ | ||
this._fixture = fixture; | ||
} | ||
|
||
[Fact] | ||
public async Task SqlInput_Output_Succeeds() | ||
{ | ||
TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1); | ||
int id = (int) t.TotalSeconds; | ||
var product = new Dictionary<string, object>() | ||
{ | ||
{ "ProductId", id }, | ||
{ "Name", "test" }, | ||
{ "Cost", 100 } | ||
}; | ||
|
||
var productString = JsonConvert.SerializeObject(product); | ||
// Insert row into Products table using SqlOutput | ||
Assert.True(await Utilities.InvokeHttpTriggerPost("AddProduct", productString, HttpStatusCode.OK)); | ||
|
||
// Read row from Products table using SqlInput | ||
Assert.True(await Utilities.InvokeHttpTrigger("GetProducts", "/" + id.ToString(), HttpStatusCode.OK, productString)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
endtoendtests/src/main/java/com/microsoft/azure/functions/endtoend/SqlTriggerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.sql.annotation.CommandType; | ||
import com.microsoft.azure.functions.sql.annotation.SQLInput; | ||
import com.microsoft.azure.functions.sql.annotation.SQLOutput; | ||
import com.microsoft.azure.functions.sql.annotation.SQLTrigger; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Array; | ||
import java.util.*; | ||
|
||
/** | ||
* Azure Functions with Azure SQL DB. | ||
*/ | ||
public class SqlTriggerTests { | ||
|
||
@FunctionName("GetProducts") | ||
public HttpResponseMessage GetProducts(@HttpTrigger(name = "req", methods = { HttpMethod.GET, | ||
HttpMethod.POST }, route = "getproducts/{productid}", authLevel = AuthorizationLevel.ANONYMOUS) | ||
HttpRequestMessage<Optional<String>> request, | ||
@SQLInput(name = "products", commandText = "SELECT TOP 1 * FROM Products WHERE ProductId = @ProductId", | ||
commandType = CommandType.Text, parameters = "@ProductId={productid}", | ||
connectionStringSetting = "AzureWebJobsSqlConnectionString") Product[] products, | ||
final ExecutionContext context) { | ||
|
||
context.getLogger().info("Java HTTP trigger processed a 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<Optional<String>> request, | ||
@SQLOutput(name = "product", commandText = "Products", connectionStringSetting = "AzureWebJobsSqlConnectionString") OutputBinding<Product> product, | ||
final ExecutionContext context) { | ||
context.getLogger().info("Java HTTP trigger processed a 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 + "}"; | ||
} | ||
} | ||
} |