Skip to content

Commit 4bd5143

Browse files
authored
Fix AddProductColumnTypesTest for Java (Azure#644)
* use string instead of timestamp * enable AddProductColumnTypesTest java
1 parent 1ec003d commit 4bd5143

File tree

5 files changed

+40
-35
lines changed

5 files changed

+40
-35
lines changed

docs/SetupGuide_Java.md

+7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- [Array](#array)
2020
- [Single Row](#single-row)
2121
- [Trigger Binding](#trigger-binding)
22+
- [Known Issues](#known-issues)
2223

2324
## Setup Function App
2425

@@ -421,3 +422,9 @@ Note: This tutorial requires that a SQL database is setup as shown in [Create a
421422
## Trigger Binding
422423
423424
> Trigger binding support is only available for in-proc C# functions at present.
425+
426+
## Known Issues
427+
The [Azure Functions Java worker](https://github.com/Azure/azure-functions-java-worker) uses the [GSON library](https://github.com/google/gson) to serialize and deserialize data. Since we are unable to customize the GSON serializer in the Java worker, there are limitations with the default GSON serializer settings.
428+
- GSON is unable to parse `DATE` and `TIME` values from the SQL table as `java.sql.Date` and `java.sql.Time` types. The current workaround is to use String. Tracking issue: https://github.com/Azure/azure-functions-sql-extension/issues/422.
429+
- On Linux, `java.sql.Timestamp` type gets serialized with an extra comma, causing the upsertion to fail. The current workaround is to use String. Tracking issue: https://github.com/Azure/azure-functions-sql-extension/issues/521.
430+

test/Integration/SqlOutputBindingIntegrationTests.cs

-2
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,6 @@ public void AddProductArrayTest(SupportedLanguages lang)
110110
/// <param name="lang">The language to run the test against</param>
111111
[Theory]
112112
[SqlInlineData()]
113-
// Tracking issue here: https://github.com/Azure/azure-functions-sql-extension/issues/521
114-
[UnsupportedLanguages(SupportedLanguages.Java)]
115113
public void AddProductColumnTypesTest(SupportedLanguages lang)
116114
{
117115
this.StartFunctionHost(nameof(AddProductColumnTypes), lang, true);

test/Integration/test-java/src/main/java/com/function/AddProductColumnTypes.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ public HttpResponseMessage run(
4949
(short)1,
5050
0.1,
5151
0.1,
52-
new Timestamp(System.currentTimeMillis()),
53-
new Timestamp(System.currentTimeMillis()),
54-
new Timestamp(System.currentTimeMillis()),
55-
new Timestamp(System.currentTimeMillis()),
56-
new Timestamp(System.currentTimeMillis()),
52+
new Timestamp(System.currentTimeMillis()).toString(),
53+
new Timestamp(System.currentTimeMillis()).toString(),
54+
new Timestamp(System.currentTimeMillis()).toString(),
55+
new Timestamp(System.currentTimeMillis()).toString(),
56+
new Timestamp(System.currentTimeMillis()).toString(),
5757
new Time(System.currentTimeMillis()).toString(),
5858
"test",
5959
"test",

test/Integration/test-java/src/main/java/com/function/Common/ProductColumnTypes.java

+17-18
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package com.function.Common;
88

99
import java.math.BigDecimal;
10-
import java.sql.Timestamp;
1110

1211
public class ProductColumnTypes {
1312
private int ProductId;
@@ -21,11 +20,11 @@ public class ProductColumnTypes {
2120
private short TinyInt;
2221
private double FloatType;
2322
private double Real;
24-
private Timestamp Date;
25-
private Timestamp Datetime;
26-
private Timestamp Datetime2;
27-
private Timestamp DatetimeOffset;
28-
private Timestamp SmallDatetime;
23+
private String Date;
24+
private String Datetime;
25+
private String Datetime2;
26+
private String DatetimeOffset;
27+
private String SmallDatetime;
2928
private String Time;
3029
private String CharType;
3130
private String Varchar;
@@ -36,8 +35,8 @@ public class ProductColumnTypes {
3635

3736

3837
public ProductColumnTypes(int productId, long bigInt, boolean bit, BigDecimal decimalType, BigDecimal money,
39-
BigDecimal numeric, short smallInt, BigDecimal smallMoney, short tinyInt, double floatType, double real, Timestamp date,
40-
Timestamp datetime, Timestamp datetime2, Timestamp datetimeOffset, Timestamp smallDatetime, String time, String charType,
38+
BigDecimal numeric, short smallInt, BigDecimal smallMoney, short tinyInt, double floatType, double real, String date,
39+
String datetime, String datetime2, String datetimeOffset, String smallDatetime, String time, String charType,
4140
String varchar, String nchar, String nvarchar, String binary, String varbinary) {
4241
ProductId = productId;
4342
BigInt = bigInt;
@@ -148,43 +147,43 @@ public void setReal(double real) {
148147
Real = real;
149148
}
150149

151-
public Timestamp getDate() {
150+
public String getDate() {
152151
return Date;
153152
}
154153

155-
public void setDate(Timestamp date) {
154+
public void setDate(String date) {
156155
Date = date;
157156
}
158157

159-
public Timestamp getDatetime() {
158+
public String getDatetime() {
160159
return Datetime;
161160
}
162161

163-
public void setDatetime(Timestamp datetime) {
162+
public void setDatetime(String datetime) {
164163
Datetime = datetime;
165164
}
166165

167-
public Timestamp getDatetime2() {
166+
public String getDatetime2() {
168167
return Datetime2;
169168
}
170169

171-
public void setDatetime2(Timestamp datetime2) {
170+
public void setDatetime2(String datetime2) {
172171
Datetime2 = datetime2;
173172
}
174173

175-
public Timestamp getDatetimeOffset() {
174+
public String getDatetimeOffset() {
176175
return DatetimeOffset;
177176
}
178177

179-
public void setDatetimeOffset(Timestamp datetimeOffset) {
178+
public void setDatetimeOffset(String datetimeOffset) {
180179
DatetimeOffset = datetimeOffset;
181180
}
182181

183-
public Timestamp getSmallDatetime() {
182+
public String getSmallDatetime() {
184183
return SmallDatetime;
185184
}
186185

187-
public void setSmallDatetime(Timestamp smallDatetime) {
186+
public void setSmallDatetime(String smallDatetime) {
188187
SmallDatetime = smallDatetime;
189188
}
190189

test/Integration/test-java/src/main/java/com/function/GetProductsColumnTypesSerialization.java

+11-10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.microsoft.azure.functions.annotation.HttpTrigger;
2020
import com.microsoft.azure.functions.sql.annotation.SQLInput;
2121

22+
import java.text.ParseException;
2223
import java.text.SimpleDateFormat;
2324
import java.util.Optional;
2425
import java.util.logging.Level;
@@ -40,22 +41,22 @@ public HttpResponseMessage run(
4041
commandType = "Text",
4142
connectionStringSetting = "SqlConnectionString")
4243
ProductColumnTypes[] products,
43-
ExecutionContext context) throws JsonProcessingException {
44+
ExecutionContext context) throws JsonProcessingException, ParseException {
4445

4546
ObjectMapper mapper = new ObjectMapper();
4647
SimpleDateFormat df = new SimpleDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSSXXX");
4748
mapper.setDateFormat(df);
4849
for (ProductColumnTypes product : products) {
4950
// Convert the datetimes to UTC (Java worker returns the datetimes in local timezone)
50-
long date = product.getDate().getTime();
51-
long datetime = product.getDatetime().getTime();
52-
long datetime2 = product.getDatetime2().getTime();
53-
long smallDateTime = product.getSmallDatetime().getTime();
54-
int offset = Calendar.getInstance().getTimeZone().getOffset(product.getDatetime().getTime());
55-
product.setDate(new Timestamp(date - offset));
56-
product.setDatetime(new Timestamp(datetime - offset));
57-
product.setDatetime2(new Timestamp(datetime2 - offset));
58-
product.setSmallDatetime(new Timestamp(smallDateTime - offset));
51+
long date = df.parse(product.getDate()).getTime();
52+
long datetime = df.parse(product.getDatetime()).getTime();
53+
long datetime2 = df.parse(product.getDatetime2()).getTime();
54+
long smallDateTime = df.parse(product.getSmallDatetime()).getTime();
55+
int offset = Calendar.getInstance().getTimeZone().getOffset(df.parse(product.getDatetime()).getTime());
56+
product.setDate(new Timestamp(date - offset).toString());
57+
product.setDatetime(new Timestamp(datetime - offset).toString());
58+
product.setDatetime2(new Timestamp(datetime2 - offset).toString());
59+
product.setSmallDatetime(new Timestamp(smallDateTime - offset).toString());
5960
context.getLogger().log(Level.INFO, mapper.writeValueAsString(product));
6061
}
6162
return request.createResponseBuilder(HttpStatus.OK).header("Content-Type", "application/json").body(mapper.writeValueAsString(products)).build();

0 commit comments

Comments
 (0)