From 6226c15ea4a35e109db7f907107f57eba8c8119d Mon Sep 17 00:00:00 2001 From: Jianghao Lu Date: Mon, 11 Jul 2016 14:20:14 -0700 Subject: [PATCH] Support unix style timestamp (#1239) --- .../main/java/fixtures/bodyinteger/Ints.java | 23 +++--- .../bodyinteger/implementation/IntsImpl.java | 78 +++++++++++++------ .../src/main/java/fixtures/url/Paths.java | 4 +- .../url/implementation/PathsImpl.java | 11 ++- .../bodyinteger/IntOperationsTests.java | 38 ++++++++- .../test/java/fixtures/url/PathsTests.java | 6 ++ .../TypeModels/ParameterModel.cs | 13 ++++ .../TypeModels/PrimaryTypeModel.cs | 10 +++ .../AutoRest.Java/TypeModels/ResponseModel.cs | 4 + 9 files changed, 146 insertions(+), 41 deletions(-) diff --git a/src/generator/AutoRest.Java.Tests/src/main/java/fixtures/bodyinteger/Ints.java b/src/generator/AutoRest.Java.Tests/src/main/java/fixtures/bodyinteger/Ints.java index 011e32b76a614..7be85f65e257f 100644 --- a/src/generator/AutoRest.Java.Tests/src/main/java/fixtures/bodyinteger/Ints.java +++ b/src/generator/AutoRest.Java.Tests/src/main/java/fixtures/bodyinteger/Ints.java @@ -15,6 +15,7 @@ import com.microsoft.rest.ServiceResponse; import fixtures.bodyinteger.models.ErrorException; import java.io.IOException; +import org.joda.time.DateTime; /** * An instance of this class provides access to all the operations defined @@ -214,9 +215,9 @@ public interface Ints { * * @throws ErrorException exception thrown from REST call * @throws IOException exception thrown from serialization/deserialization - * @return the long object wrapped in {@link ServiceResponse} if successful. + * @return the DateTime object wrapped in {@link ServiceResponse} if successful. */ - ServiceResponse getUnixTime() throws ErrorException, IOException; + ServiceResponse getUnixTime() throws ErrorException, IOException; /** * Get datetime encoded as Unix time value. @@ -225,7 +226,7 @@ public interface Ints { * @throws IllegalArgumentException thrown if callback is null * @return the {@link ServiceCall} object */ - ServiceCall getUnixTimeAsync(final ServiceCallback serviceCallback) throws IllegalArgumentException; + ServiceCall getUnixTimeAsync(final ServiceCallback serviceCallback) throws IllegalArgumentException; /** * Put datetime encoded as Unix time. @@ -235,7 +236,7 @@ public interface Ints { * @throws IOException exception thrown from serialization/deserialization * @return the {@link ServiceResponse} object if successful. */ - ServiceResponse putUnixTimeDate(long intBody) throws ErrorException, IOException; + ServiceResponse putUnixTimeDate(DateTime intBody) throws ErrorException, IOException; /** * Put datetime encoded as Unix time. @@ -245,16 +246,16 @@ public interface Ints { * @throws IllegalArgumentException thrown if callback is null * @return the {@link ServiceCall} object */ - ServiceCall putUnixTimeDateAsync(long intBody, final ServiceCallback serviceCallback) throws IllegalArgumentException; + ServiceCall putUnixTimeDateAsync(DateTime intBody, final ServiceCallback serviceCallback) throws IllegalArgumentException; /** * Get invalid Unix time value. * * @throws ErrorException exception thrown from REST call * @throws IOException exception thrown from serialization/deserialization - * @return the long object wrapped in {@link ServiceResponse} if successful. + * @return the DateTime object wrapped in {@link ServiceResponse} if successful. */ - ServiceResponse getInvalidUnixTime() throws ErrorException, IOException; + ServiceResponse getInvalidUnixTime() throws ErrorException, IOException; /** * Get invalid Unix time value. @@ -263,16 +264,16 @@ public interface Ints { * @throws IllegalArgumentException thrown if callback is null * @return the {@link ServiceCall} object */ - ServiceCall getInvalidUnixTimeAsync(final ServiceCallback serviceCallback) throws IllegalArgumentException; + ServiceCall getInvalidUnixTimeAsync(final ServiceCallback serviceCallback) throws IllegalArgumentException; /** * Get null Unix time value. * * @throws ErrorException exception thrown from REST call * @throws IOException exception thrown from serialization/deserialization - * @return the long object wrapped in {@link ServiceResponse} if successful. + * @return the DateTime object wrapped in {@link ServiceResponse} if successful. */ - ServiceResponse getNullUnixTime() throws ErrorException, IOException; + ServiceResponse getNullUnixTime() throws ErrorException, IOException; /** * Get null Unix time value. @@ -281,6 +282,6 @@ public interface Ints { * @throws IllegalArgumentException thrown if callback is null * @return the {@link ServiceCall} object */ - ServiceCall getNullUnixTimeAsync(final ServiceCallback serviceCallback) throws IllegalArgumentException; + ServiceCall getNullUnixTimeAsync(final ServiceCallback serviceCallback) throws IllegalArgumentException; } diff --git a/src/generator/AutoRest.Java.Tests/src/main/java/fixtures/bodyinteger/implementation/IntsImpl.java b/src/generator/AutoRest.Java.Tests/src/main/java/fixtures/bodyinteger/implementation/IntsImpl.java index e1227c38f67c2..609b2376e381e 100644 --- a/src/generator/AutoRest.Java.Tests/src/main/java/fixtures/bodyinteger/implementation/IntsImpl.java +++ b/src/generator/AutoRest.Java.Tests/src/main/java/fixtures/bodyinteger/implementation/IntsImpl.java @@ -21,6 +21,8 @@ import fixtures.bodyinteger.models.ErrorException; import java.io.IOException; import okhttp3.ResponseBody; +import org.joda.time.DateTime; +import org.joda.time.DateTimeZone; import retrofit2.Call; import retrofit2.http.Body; import retrofit2.http.GET; @@ -575,11 +577,16 @@ private ServiceResponse putMin64Delegate(Response response) * * @throws ErrorException exception thrown from REST call * @throws IOException exception thrown from serialization/deserialization - * @return the long object wrapped in {@link ServiceResponse} if successful. + * @return the DateTime object wrapped in {@link ServiceResponse} if successful. */ - public ServiceResponse getUnixTime() throws ErrorException, IOException { + public ServiceResponse getUnixTime() throws ErrorException, IOException { Call call = service.getUnixTime(); - return getUnixTimeDelegate(call.execute()); + ServiceResponse response = getUnixTimeDelegate(call.execute()); + DateTime body = null; + if (response.getBody() != null) { + body = new DateTime(response.getBody() * 1000L, DateTimeZone.UTC); + } + return new ServiceResponse(body, response.getResponse()); } /** @@ -589,17 +596,22 @@ public ServiceResponse getUnixTime() throws ErrorException, IOException { * @throws IllegalArgumentException thrown if callback is null * @return the {@link Call} object */ - public ServiceCall getUnixTimeAsync(final ServiceCallback serviceCallback) throws IllegalArgumentException { + public ServiceCall getUnixTimeAsync(final ServiceCallback serviceCallback) throws IllegalArgumentException { if (serviceCallback == null) { throw new IllegalArgumentException("ServiceCallback is required for async calls."); } Call call = service.getUnixTime(); final ServiceCall serviceCall = new ServiceCall(call); - call.enqueue(new ServiceResponseCallback(serviceCallback) { + call.enqueue(new ServiceResponseCallback(serviceCallback) { @Override public void onResponse(Call call, Response response) { try { - serviceCallback.success(getUnixTimeDelegate(response)); + ServiceResponse result = getUnixTimeDelegate(response); + DateTime body = null; + if (result.getBody() != null) { + body = new DateTime(result.getBody() * 1000L, DateTimeZone.UTC); + } + serviceCallback.success(new ServiceResponse(body, result.getResponse())); } catch (ErrorException | IOException exception) { serviceCallback.failure(exception); } @@ -623,8 +635,9 @@ private ServiceResponse getUnixTimeDelegate(Response respons * @throws IOException exception thrown from serialization/deserialization * @return the {@link ServiceResponse} object if successful. */ - public ServiceResponse putUnixTimeDate(long intBody) throws ErrorException, IOException { - Call call = service.putUnixTimeDate(intBody); + public ServiceResponse putUnixTimeDate(DateTime intBody) throws ErrorException, IOException { + Long intBodyConverted = intBody.toDateTime(DateTimeZone.UTC).getMillis() / 1000; + Call call = service.putUnixTimeDate(intBodyConverted); return putUnixTimeDateDelegate(call.execute()); } @@ -636,11 +649,12 @@ public ServiceResponse putUnixTimeDate(long intBody) throws ErrorException * @throws IllegalArgumentException thrown if callback is null * @return the {@link Call} object */ - public ServiceCall putUnixTimeDateAsync(long intBody, final ServiceCallback serviceCallback) throws IllegalArgumentException { + public ServiceCall putUnixTimeDateAsync(DateTime intBody, final ServiceCallback serviceCallback) throws IllegalArgumentException { if (serviceCallback == null) { throw new IllegalArgumentException("ServiceCallback is required for async calls."); } - Call call = service.putUnixTimeDate(intBody); + Long intBodyConverted = intBody.toDateTime(DateTimeZone.UTC).getMillis() / 1000; + Call call = service.putUnixTimeDate(intBodyConverted); final ServiceCall serviceCall = new ServiceCall(call); call.enqueue(new ServiceResponseCallback(serviceCallback) { @Override @@ -667,11 +681,16 @@ private ServiceResponse putUnixTimeDateDelegate(Response res * * @throws ErrorException exception thrown from REST call * @throws IOException exception thrown from serialization/deserialization - * @return the long object wrapped in {@link ServiceResponse} if successful. + * @return the DateTime object wrapped in {@link ServiceResponse} if successful. */ - public ServiceResponse getInvalidUnixTime() throws ErrorException, IOException { + public ServiceResponse getInvalidUnixTime() throws ErrorException, IOException { Call call = service.getInvalidUnixTime(); - return getInvalidUnixTimeDelegate(call.execute()); + ServiceResponse response = getInvalidUnixTimeDelegate(call.execute()); + DateTime body = null; + if (response.getBody() != null) { + body = new DateTime(response.getBody() * 1000L, DateTimeZone.UTC); + } + return new ServiceResponse(body, response.getResponse()); } /** @@ -681,17 +700,22 @@ public ServiceResponse getInvalidUnixTime() throws ErrorException, IOExcep * @throws IllegalArgumentException thrown if callback is null * @return the {@link Call} object */ - public ServiceCall getInvalidUnixTimeAsync(final ServiceCallback serviceCallback) throws IllegalArgumentException { + public ServiceCall getInvalidUnixTimeAsync(final ServiceCallback serviceCallback) throws IllegalArgumentException { if (serviceCallback == null) { throw new IllegalArgumentException("ServiceCallback is required for async calls."); } Call call = service.getInvalidUnixTime(); final ServiceCall serviceCall = new ServiceCall(call); - call.enqueue(new ServiceResponseCallback(serviceCallback) { + call.enqueue(new ServiceResponseCallback(serviceCallback) { @Override public void onResponse(Call call, Response response) { try { - serviceCallback.success(getInvalidUnixTimeDelegate(response)); + ServiceResponse result = getInvalidUnixTimeDelegate(response); + DateTime body = null; + if (result.getBody() != null) { + body = new DateTime(result.getBody() * 1000L, DateTimeZone.UTC); + } + serviceCallback.success(new ServiceResponse(body, result.getResponse())); } catch (ErrorException | IOException exception) { serviceCallback.failure(exception); } @@ -712,11 +736,16 @@ private ServiceResponse getInvalidUnixTimeDelegate(Response * * @throws ErrorException exception thrown from REST call * @throws IOException exception thrown from serialization/deserialization - * @return the long object wrapped in {@link ServiceResponse} if successful. + * @return the DateTime object wrapped in {@link ServiceResponse} if successful. */ - public ServiceResponse getNullUnixTime() throws ErrorException, IOException { + public ServiceResponse getNullUnixTime() throws ErrorException, IOException { Call call = service.getNullUnixTime(); - return getNullUnixTimeDelegate(call.execute()); + ServiceResponse response = getNullUnixTimeDelegate(call.execute()); + DateTime body = null; + if (response.getBody() != null) { + body = new DateTime(response.getBody() * 1000L, DateTimeZone.UTC); + } + return new ServiceResponse(body, response.getResponse()); } /** @@ -726,17 +755,22 @@ public ServiceResponse getNullUnixTime() throws ErrorException, IOExceptio * @throws IllegalArgumentException thrown if callback is null * @return the {@link Call} object */ - public ServiceCall getNullUnixTimeAsync(final ServiceCallback serviceCallback) throws IllegalArgumentException { + public ServiceCall getNullUnixTimeAsync(final ServiceCallback serviceCallback) throws IllegalArgumentException { if (serviceCallback == null) { throw new IllegalArgumentException("ServiceCallback is required for async calls."); } Call call = service.getNullUnixTime(); final ServiceCall serviceCall = new ServiceCall(call); - call.enqueue(new ServiceResponseCallback(serviceCallback) { + call.enqueue(new ServiceResponseCallback(serviceCallback) { @Override public void onResponse(Call call, Response response) { try { - serviceCallback.success(getNullUnixTimeDelegate(response)); + ServiceResponse result = getNullUnixTimeDelegate(response); + DateTime body = null; + if (result.getBody() != null) { + body = new DateTime(result.getBody() * 1000L, DateTimeZone.UTC); + } + serviceCallback.success(new ServiceResponse(body, result.getResponse())); } catch (ErrorException | IOException exception) { serviceCallback.failure(exception); } diff --git a/src/generator/AutoRest.Java.Tests/src/main/java/fixtures/url/Paths.java b/src/generator/AutoRest.Java.Tests/src/main/java/fixtures/url/Paths.java index 1d714c57531cc..468fd300dc792 100644 --- a/src/generator/AutoRest.Java.Tests/src/main/java/fixtures/url/Paths.java +++ b/src/generator/AutoRest.Java.Tests/src/main/java/fixtures/url/Paths.java @@ -510,7 +510,7 @@ public interface Paths { * @throws IOException exception thrown from serialization/deserialization * @return the {@link ServiceResponse} object if successful. */ - ServiceResponse unixTimeUrl(long unixTimeUrlPath) throws ErrorException, IOException; + ServiceResponse unixTimeUrl(DateTime unixTimeUrlPath) throws ErrorException, IOException; /** * Get the date 2016-04-13 encoded value as '1460505600' (Unix time). @@ -520,6 +520,6 @@ public interface Paths { * @throws IllegalArgumentException thrown if callback is null * @return the {@link ServiceCall} object */ - ServiceCall unixTimeUrlAsync(long unixTimeUrlPath, final ServiceCallback serviceCallback) throws IllegalArgumentException; + ServiceCall unixTimeUrlAsync(DateTime unixTimeUrlPath, final ServiceCallback serviceCallback) throws IllegalArgumentException; } diff --git a/src/generator/AutoRest.Java.Tests/src/main/java/fixtures/url/implementation/PathsImpl.java b/src/generator/AutoRest.Java.Tests/src/main/java/fixtures/url/implementation/PathsImpl.java index b7d8301bfee9c..6d3d307d019bc 100644 --- a/src/generator/AutoRest.Java.Tests/src/main/java/fixtures/url/implementation/PathsImpl.java +++ b/src/generator/AutoRest.Java.Tests/src/main/java/fixtures/url/implementation/PathsImpl.java @@ -27,6 +27,7 @@ import okhttp3.ResponseBody; import org.apache.commons.codec.binary.Base64; import org.joda.time.DateTime; +import org.joda.time.DateTimeZone; import org.joda.time.LocalDate; import retrofit2.Call; import retrofit2.http.GET; @@ -1431,8 +1432,9 @@ private ServiceResponse arrayCsvInPathDelegate(Response resp * @throws IOException exception thrown from serialization/deserialization * @return the {@link ServiceResponse} object if successful. */ - public ServiceResponse unixTimeUrl(long unixTimeUrlPath) throws ErrorException, IOException { - Call call = service.unixTimeUrl(unixTimeUrlPath); + public ServiceResponse unixTimeUrl(DateTime unixTimeUrlPath) throws ErrorException, IOException { + Long unixTimeUrlPathConverted = unixTimeUrlPath.toDateTime(DateTimeZone.UTC).getMillis() / 1000; + Call call = service.unixTimeUrl(unixTimeUrlPathConverted); return unixTimeUrlDelegate(call.execute()); } @@ -1444,11 +1446,12 @@ public ServiceResponse unixTimeUrl(long unixTimeUrlPath) throws ErrorExcep * @throws IllegalArgumentException thrown if callback is null * @return the {@link Call} object */ - public ServiceCall unixTimeUrlAsync(long unixTimeUrlPath, final ServiceCallback serviceCallback) throws IllegalArgumentException { + public ServiceCall unixTimeUrlAsync(DateTime unixTimeUrlPath, final ServiceCallback serviceCallback) throws IllegalArgumentException { if (serviceCallback == null) { throw new IllegalArgumentException("ServiceCallback is required for async calls."); } - Call call = service.unixTimeUrl(unixTimeUrlPath); + Long unixTimeUrlPathConverted = unixTimeUrlPath.toDateTime(DateTimeZone.UTC).getMillis() / 1000; + Call call = service.unixTimeUrl(unixTimeUrlPathConverted); final ServiceCall serviceCall = new ServiceCall(call); call.enqueue(new ServiceResponseCallback(serviceCallback) { @Override diff --git a/src/generator/AutoRest.Java.Tests/src/test/java/fixtures/bodyinteger/IntOperationsTests.java b/src/generator/AutoRest.Java.Tests/src/test/java/fixtures/bodyinteger/IntOperationsTests.java index 1204413d5d8d9..1d3a38093616b 100644 --- a/src/generator/AutoRest.Java.Tests/src/test/java/fixtures/bodyinteger/IntOperationsTests.java +++ b/src/generator/AutoRest.Java.Tests/src/test/java/fixtures/bodyinteger/IntOperationsTests.java @@ -3,6 +3,9 @@ import com.fasterxml.jackson.core.JsonParseException; import com.microsoft.rest.ServiceCallback; import com.microsoft.rest.ServiceResponse; + +import org.joda.time.DateTime; +import org.joda.time.DateTimeZone; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; @@ -12,6 +15,8 @@ import fixtures.bodyinteger.implementation.AutoRestIntegerTestServiceImpl; +import static org.junit.Assert.fail; + public class IntOperationsTests { private static AutoRestIntegerTestService client; private CountDownLatch lock = new CountDownLatch(1); @@ -80,7 +85,8 @@ public void getUnderflowInt64() throws Exception { public void putMax32() throws Exception { client.ints().putMax32Async(Integer.MAX_VALUE, new ServiceCallback() { @Override - public void failure(Throwable t) { } + public void failure(Throwable t) { + } @Override public void success(ServiceResponse response) { @@ -111,7 +117,8 @@ public void success(ServiceResponse response) { public void putMin32() throws Exception { client.ints().putMin32Async(Integer.MIN_VALUE, new ServiceCallback() { @Override - public void failure(Throwable t) { } + public void failure(Throwable t) { + } @Override public void success(ServiceResponse response) { @@ -137,4 +144,31 @@ public void success(ServiceResponse response) { }); Assert.assertTrue(lock.await(1000, TimeUnit.MILLISECONDS)); } + + @Test + public void getUnixTime() throws Exception { + DateTime result = client.ints().getUnixTime().getBody(); + Assert.assertEquals(new DateTime(2016, 4, 13, 0, 0, 0, DateTimeZone.UTC), result); + } + + @Test + public void putUnixTimeDate() throws Exception { + client.ints().putUnixTimeDate(new DateTime(2016, 4, 13, 0, 0, 0, DateTimeZone.UTC)); + } + + @Test + public void getInvalidUnixTime() throws Exception { + try { + client.ints().getInvalidUnixTime(); + fail(); + } catch (JsonParseException e) { + Assert.assertTrue(e.getMessage().contains("Unexpected character")); + } + } + + @Test + public void getNullUnixTime() throws Exception { + DateTime result = client.ints().getNullUnixTime().getBody(); + Assert.assertNull(result); + } } diff --git a/src/generator/AutoRest.Java.Tests/src/test/java/fixtures/url/PathsTests.java b/src/generator/AutoRest.Java.Tests/src/test/java/fixtures/url/PathsTests.java index d24e3079c4074..be3c2f4954005 100644 --- a/src/generator/AutoRest.Java.Tests/src/test/java/fixtures/url/PathsTests.java +++ b/src/generator/AutoRest.Java.Tests/src/test/java/fixtures/url/PathsTests.java @@ -1,5 +1,6 @@ package fixtures.url; +import org.joda.time.DateTime; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; @@ -155,4 +156,9 @@ public void arrayCsvInPath() throws Exception { client.getPathsOperations().arrayCsvInPath(arrayPath); } */ + + @Test + public void unixTimeUrl() throws Exception { + client.paths().unixTimeUrl(DateTime.parse("2016-04-13T00:00:00Z")); + } } diff --git a/src/generator/AutoRest.Java/TypeModels/ParameterModel.cs b/src/generator/AutoRest.Java/TypeModels/ParameterModel.cs index 542bdbe27faef..bde271ed77f16 100644 --- a/src/generator/AutoRest.Java/TypeModels/ParameterModel.cs +++ b/src/generator/AutoRest.Java/TypeModels/ParameterModel.cs @@ -113,6 +113,19 @@ private string convertClientTypeToWireType(ITypeModel wireType, string source, s builder.Outdent().AppendLine("}"); } } + else if (wireType.IsPrimaryType(KnownPrimaryType.UnixTime)) + { + if (!IsRequired) + { + builder.AppendLine("Long {0} = {1};", target, wireType.DefaultValue(_method)) + .AppendLine("if ({0} != null) {{", source).Indent(); + } + builder.AppendLine("{0}{1} = {2}.toDateTime(DateTimeZone.UTC).getMillis() / 1000;", IsRequired ? "Long " : "", target, source); + if (!IsRequired) + { + builder.Outdent().AppendLine("}"); + } + } else if (wireType.IsPrimaryType(KnownPrimaryType.Stream)) { if (!IsRequired) diff --git a/src/generator/AutoRest.Java/TypeModels/PrimaryTypeModel.cs b/src/generator/AutoRest.Java/TypeModels/PrimaryTypeModel.cs index 3c1d8bc1d9643..3191b2d698abe 100644 --- a/src/generator/AutoRest.Java/TypeModels/PrimaryTypeModel.cs +++ b/src/generator/AutoRest.Java/TypeModels/PrimaryTypeModel.cs @@ -60,6 +60,10 @@ public ITypeModel ParameterVariant { return new PrimaryTypeModel(KnownPrimaryType.DateTime); } + else if (Type == KnownPrimaryType.UnixTime) + { + return new PrimaryTypeModel(KnownPrimaryType.DateTime); + } else if (Type == KnownPrimaryType.Stream) { return new PrimaryTypeModel(KnownPrimaryType.ByteArray); @@ -79,6 +83,10 @@ public ITypeModel ResponseVariant { return new PrimaryTypeModel(KnownPrimaryType.DateTime); } + else if (Type == KnownPrimaryType.UnixTime) + { + return new PrimaryTypeModel(KnownPrimaryType.DateTime); + } else { return this; @@ -200,6 +208,8 @@ private void Initialize(PrimaryType primaryType) else if (primaryType.Type == KnownPrimaryType.UnixTime) { Name = "long"; + _imports.Add("org.joda.time.DateTime"); + _imports.Add("org.joda.time.DateTimeZone"); } else if (primaryType.Type == KnownPrimaryType.Uuid) { diff --git a/src/generator/AutoRest.Java/TypeModels/ResponseModel.cs b/src/generator/AutoRest.Java/TypeModels/ResponseModel.cs index 9ace5ac1984e0..e79b35ad736cc 100644 --- a/src/generator/AutoRest.Java/TypeModels/ResponseModel.cs +++ b/src/generator/AutoRest.Java/TypeModels/ResponseModel.cs @@ -279,6 +279,10 @@ private string convertToClientType(ITypeModel type, string source, string target { return target + " = " + source + ".getDateTime();"; } + else if (type.IsPrimaryType(KnownPrimaryType.UnixTime)) + { + return target + " = new DateTime(" + source + " * 1000L, DateTimeZone.UTC);"; + } else { return target + " = " + source + ";";