Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add java trigger samples #793

Merged
merged 6 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion samples/samples-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<dependency>
<groupId>com.microsoft.azure.functions</groupId>
<artifactId>azure-functions-java-library-sql</artifactId>
<version>0.1.1</version>
<version>2.0.0-preview</version>
</dependency>

<dependency>
Expand All @@ -36,6 +36,12 @@
<version>2.13.4.2</version>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>

<!-- Test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/

package com.function.Common;

import com.google.gson.annotations.SerializedName;

public enum SqlChangeOperation {
@SerializedName("0")
Insert,
@SerializedName("1")
Update,
@SerializedName("2")
Delete
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/

package com.function.Common;

public class SqlChangeProduct {
private SqlChangeOperation Operation;
private Product Item;

public SqlChangeProduct() {
}

public SqlChangeProduct(SqlChangeOperation operation, Product item) {
this.Operation = operation;
this.Item = item;
}

public SqlChangeOperation getOperation() {
return Operation;
}

public void setOperation(SqlChangeOperation operation) {
this.Operation = operation;
}

public Product getItem() {
return Item;
}

public void setItem(Product item) {
this.Item = item;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import com.microsoft.azure.functions.sql.annotation.SQLInput;
import com.microsoft.azure.functions.sql.annotation.CommandType;
import com.microsoft.azure.functions.sql.annotation.SQLOutput;

import java.util.Optional;
Expand All @@ -36,7 +37,7 @@ public HttpResponseMessage run(
@SQLInput(
name = "products",
commandText = "SELECT * FROM Products WHERE Cost = @Cost",
commandType = "Text",
commandType = CommandType.Text,
parameters = "@Cost={cost}",
connectionStringSetting = "SqlConnectionString")
Product[] products,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import com.microsoft.azure.functions.sql.annotation.CommandType;
import com.microsoft.azure.functions.sql.annotation.SQLInput;

import java.util.Optional;
Expand All @@ -30,7 +31,7 @@ public HttpResponseMessage run(
@SQLInput(
name = "productNames",
commandText = "SELECT * FROM ProductNames",
commandType = "Text",
commandType = CommandType.Text,
connectionStringSetting = "SqlConnectionString")
ProductName[] products) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import com.microsoft.azure.functions.sql.annotation.CommandType;
import com.microsoft.azure.functions.sql.annotation.SQLInput;

import java.util.Optional;
Expand All @@ -30,7 +31,7 @@ public HttpResponseMessage run(
@SQLInput(
name = "products",
commandText = "SELECT * FROM Products WHERE Cost = @Cost",
commandType = "Text",
commandType = CommandType.Text,
parameters = "@Cost={cost}",
connectionStringSetting = "SqlConnectionString")
Product[] products) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import com.microsoft.azure.functions.sql.annotation.CommandType;
import com.microsoft.azure.functions.sql.annotation.SQLInput;

import java.util.Optional;
Expand All @@ -34,14 +35,14 @@ public HttpResponseMessage run(
@SQLInput(
name = "products",
commandText = "SELECT * FROM Products WHERE Cost = @Cost",
commandType = "Text",
commandType = CommandType.Text,
parameters = "@Cost={cost}",
connectionStringSetting = "SqlConnectionString")
Product[] products,
@SQLInput(
name = "productsWithIdentity",
commandText = "SELECT * FROM ProductsWithIdentity WHERE Cost = @Cost",
commandType = "Text",
commandType = CommandType.Text,
parameters = "@Cost={cost}",
connectionStringSetting = "SqlConnectionString")
Product[] productsWithIdentity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import com.microsoft.azure.functions.sql.annotation.CommandType;
import com.microsoft.azure.functions.sql.annotation.SQLInput;

import java.util.Optional;
Expand All @@ -30,7 +31,7 @@ public HttpResponseMessage run(
@SQLInput(
name = "products",
commandText = "SELECT * FROM Products WHERE Cost = @Cost and Name = @Name",
commandType = "Text",
commandType = CommandType.Text,
parameters = "@Cost={cost},@Name=",
connectionStringSetting = "SqlConnectionString")
Product[] products) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import com.microsoft.azure.functions.sql.annotation.CommandType;
import com.microsoft.azure.functions.sql.annotation.SQLInput;

import java.util.Optional;
Expand All @@ -30,7 +31,7 @@ public HttpResponseMessage run(
@SQLInput(
name = "products",
commandText = "SelectProductsCost",
commandType = "StoredProcedure",
commandType = CommandType.StoredProcedure,
parameters = "@Cost={cost}",
connectionStringSetting = "SqlConnectionString")
Product[] products) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import com.microsoft.azure.functions.sql.annotation.CommandType;
import com.microsoft.azure.functions.sql.annotation.SQLInput;

import java.util.Optional;
Expand All @@ -30,7 +31,7 @@ public HttpResponseMessage run(
@SQLInput(
name = "products",
commandText = "%Sp_SelectCost%",
commandType = "StoredProcedure",
commandType = CommandType.StoredProcedure,
parameters = "@Cost=%ProductCost%",
connectionStringSetting = "SqlConnectionString")
Product[] products) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/

package com.function;

import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.sql.annotation.SQLTrigger;
import com.function.Common.SqlChangeProduct;
import com.google.gson.Gson;

import java.util.logging.Level;

public class ProductsTrigger {
@FunctionName("ProductsTrigger")
public void run(
@SQLTrigger(
name = "changes",
tableName = "[dbo].[Products]",
connectionStringSetting = "SqlConnectionString")
SqlChangeProduct[] changes,
ExecutionContext context) {

context.getLogger().log(Level.INFO, "SQL Changes: " + new Gson().toJson(changes));
}
}
26 changes: 13 additions & 13 deletions test/Integration/SqlTriggerBindingIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public SqlTriggerBindingIntegrationTests(ITestOutputHelper output = null) : base
/// </summary>
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.Java, SupportedLanguages.CSharpscript)]
[UnsupportedLanguages(SupportedLanguages.CSharpscript)]
public async Task SingleOperationTriggerTest(SupportedLanguages lang)
{
this.SetChangeTrackingForTable("Products");
Expand Down Expand Up @@ -80,7 +80,7 @@ await this.WaitForProductChanges(
/// </summary>
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.Java, SupportedLanguages.CSharpscript)]
[UnsupportedLanguages(SupportedLanguages.CSharpscript)]
public async Task BatchSizeOverrideTriggerTest(SupportedLanguages lang)
{
// Use enough items to require 4 batches to be processed but then
Expand Down Expand Up @@ -124,7 +124,7 @@ await this.WaitForProductChanges(
/// </summary>
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.Java, SupportedLanguages.CSharpscript)]
[UnsupportedLanguages(SupportedLanguages.CSharpscript)]
public async Task MaxBatchSizeOverrideTriggerTest(SupportedLanguages lang)
{
// Use enough items to require 4 batches to be processed but then
Expand Down Expand Up @@ -168,7 +168,7 @@ await this.WaitForProductChanges(
/// </summary>
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.Java, SupportedLanguages.OutOfProc, SupportedLanguages.CSharpscript)]
[UnsupportedLanguages(SupportedLanguages.OutOfProc, SupportedLanguages.CSharpscript)]
public async Task PollingIntervalOverrideTriggerTest(SupportedLanguages lang)
{
const int firstId = 1;
Expand Down Expand Up @@ -211,7 +211,7 @@ await this.WaitForProductChanges(
/// </summary>
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.Java, SupportedLanguages.CSharpscript)]
[UnsupportedLanguages(SupportedLanguages.CSharpscript)]
public async Task MultiOperationTriggerTest(SupportedLanguages lang)
{
int firstId = 1;
Expand Down Expand Up @@ -293,7 +293,7 @@ await this.WaitForProductChanges(
/// </summary>
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.Java, SupportedLanguages.OutOfProc, SupportedLanguages.CSharpscript)]
[UnsupportedLanguages(SupportedLanguages.OutOfProc, SupportedLanguages.CSharpscript)]
public async Task MultiFunctionTriggerTest(SupportedLanguages lang)
{
const string Trigger1Changes = "Trigger1 Changes: ";
Expand Down Expand Up @@ -420,7 +420,7 @@ public async Task MultiFunctionTriggerTest(SupportedLanguages lang)
/// </summary>
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.Java, SupportedLanguages.CSharpscript)]
[UnsupportedLanguages(SupportedLanguages.CSharpscript)]
public async Task MultiHostTriggerTest(SupportedLanguages lang)
{
this.SetChangeTrackingForTable("Products");
Expand Down Expand Up @@ -472,7 +472,7 @@ await this.WaitForProductChanges(
/// </summary>
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.Java, SupportedLanguages.OutOfProc, SupportedLanguages.CSharpscript)]
[UnsupportedLanguages(SupportedLanguages.OutOfProc, SupportedLanguages.CSharpscript)]
public void TableNotPresentTriggerTest(SupportedLanguages lang)
{
this.StartFunctionHostAndWaitForError(
Expand All @@ -487,7 +487,7 @@ public void TableNotPresentTriggerTest(SupportedLanguages lang)
/// </summary>
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.Java, SupportedLanguages.OutOfProc, SupportedLanguages.CSharpscript)]
[UnsupportedLanguages(SupportedLanguages.OutOfProc, SupportedLanguages.CSharpscript)]
public void PrimaryKeyNotCreatedTriggerTest(SupportedLanguages lang)
{
this.StartFunctionHostAndWaitForError(
Expand All @@ -503,7 +503,7 @@ public void PrimaryKeyNotCreatedTriggerTest(SupportedLanguages lang)
/// </summary>
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.Java, SupportedLanguages.OutOfProc, SupportedLanguages.CSharpscript)]
[UnsupportedLanguages(SupportedLanguages.OutOfProc, SupportedLanguages.CSharpscript)]
public void ReservedPrimaryKeyColumnNamesTriggerTest(SupportedLanguages lang)
{
this.StartFunctionHostAndWaitForError(
Expand All @@ -519,7 +519,7 @@ public void ReservedPrimaryKeyColumnNamesTriggerTest(SupportedLanguages lang)
/// </summary>
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.Java, SupportedLanguages.OutOfProc, SupportedLanguages.CSharpscript)]
[UnsupportedLanguages(SupportedLanguages.OutOfProc, SupportedLanguages.CSharpscript)]
public void UnsupportedColumnTypesTriggerTest(SupportedLanguages lang)
{
this.StartFunctionHostAndWaitForError(
Expand All @@ -535,7 +535,7 @@ public void UnsupportedColumnTypesTriggerTest(SupportedLanguages lang)
/// </summary>
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.Java, SupportedLanguages.CSharpscript)]
[UnsupportedLanguages(SupportedLanguages.CSharpscript)]
public void ChangeTrackingNotEnabledTriggerTest(SupportedLanguages lang)
{
this.StartFunctionHostAndWaitForError(
Expand Down Expand Up @@ -570,7 +570,7 @@ public async void GetMetricsTest()
/// </summary>
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.Java, SupportedLanguages.CSharpscript)]
[UnsupportedLanguages(SupportedLanguages.CSharpscript)]
public void UnsupportedDatabaseThrows(SupportedLanguages lang)
{
// Change database compat level to unsupported version
Expand Down
33 changes: 33 additions & 0 deletions test/Integration/test-java/ProductsTriggerWithValidation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/

package com.function;

import com.function.Common.SqlChangeProduct;
import com.google.gson.Gson;
import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.sql.annotation.SQLTrigger;

import java.util.logging.Level;

public class ProductsTriggerWithValidation {
@FunctionName("ProductsTriggerWithValidation")
public void run(
@SQLTrigger(
name = "changes",
tableName = "[dbo].[Products]",
connectionStringSetting = "SqlConnectionString")
SqlChangeProduct[] changes,
ExecutionContext context) throws Exception {

int expectedMaxBatchSize = Integer.parseInt(System.getenv("TEST_EXPECTED_MAX_BATCH_SIZE"));
if (expectedMaxBatchSize != changes.length) {
throw new Exception("Invalid max batch size, got " + changes.length + " changes but expected " + expectedMaxBatchSize);
}
context.getLogger().log(Level.INFO, "SQL Changes: " + new Gson().toJson(changes));
}
}
Loading