Skip to content

Commit

Permalink
Support unix style timestamp (Azure#1239)
Browse files Browse the repository at this point in the history
  • Loading branch information
jianghaolu authored and fearthecowboy committed Jul 11, 2016
1 parent ba86230 commit 6226c15
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Long> getUnixTime() throws ErrorException, IOException;
ServiceResponse<DateTime> getUnixTime() throws ErrorException, IOException;

/**
* Get datetime encoded as Unix time value.
Expand All @@ -225,7 +226,7 @@ public interface Ints {
* @throws IllegalArgumentException thrown if callback is null
* @return the {@link ServiceCall} object
*/
ServiceCall getUnixTimeAsync(final ServiceCallback<Long> serviceCallback) throws IllegalArgumentException;
ServiceCall getUnixTimeAsync(final ServiceCallback<DateTime> serviceCallback) throws IllegalArgumentException;

/**
* Put datetime encoded as Unix time.
Expand All @@ -235,7 +236,7 @@ public interface Ints {
* @throws IOException exception thrown from serialization/deserialization
* @return the {@link ServiceResponse} object if successful.
*/
ServiceResponse<Void> putUnixTimeDate(long intBody) throws ErrorException, IOException;
ServiceResponse<Void> putUnixTimeDate(DateTime intBody) throws ErrorException, IOException;

/**
* Put datetime encoded as Unix time.
Expand All @@ -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<Void> serviceCallback) throws IllegalArgumentException;
ServiceCall putUnixTimeDateAsync(DateTime intBody, final ServiceCallback<Void> 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<Long> getInvalidUnixTime() throws ErrorException, IOException;
ServiceResponse<DateTime> getInvalidUnixTime() throws ErrorException, IOException;

/**
* Get invalid Unix time value.
Expand All @@ -263,16 +264,16 @@ public interface Ints {
* @throws IllegalArgumentException thrown if callback is null
* @return the {@link ServiceCall} object
*/
ServiceCall getInvalidUnixTimeAsync(final ServiceCallback<Long> serviceCallback) throws IllegalArgumentException;
ServiceCall getInvalidUnixTimeAsync(final ServiceCallback<DateTime> 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<Long> getNullUnixTime() throws ErrorException, IOException;
ServiceResponse<DateTime> getNullUnixTime() throws ErrorException, IOException;

/**
* Get null Unix time value.
Expand All @@ -281,6 +282,6 @@ public interface Ints {
* @throws IllegalArgumentException thrown if callback is null
* @return the {@link ServiceCall} object
*/
ServiceCall getNullUnixTimeAsync(final ServiceCallback<Long> serviceCallback) throws IllegalArgumentException;
ServiceCall getNullUnixTimeAsync(final ServiceCallback<DateTime> serviceCallback) throws IllegalArgumentException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -575,11 +577,16 @@ private ServiceResponse<Void> putMin64Delegate(Response<ResponseBody> 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<Long> getUnixTime() throws ErrorException, IOException {
public ServiceResponse<DateTime> getUnixTime() throws ErrorException, IOException {
Call<ResponseBody> call = service.getUnixTime();
return getUnixTimeDelegate(call.execute());
ServiceResponse<Long> response = getUnixTimeDelegate(call.execute());
DateTime body = null;
if (response.getBody() != null) {
body = new DateTime(response.getBody() * 1000L, DateTimeZone.UTC);
}
return new ServiceResponse<DateTime>(body, response.getResponse());
}

/**
Expand All @@ -589,17 +596,22 @@ public ServiceResponse<Long> getUnixTime() throws ErrorException, IOException {
* @throws IllegalArgumentException thrown if callback is null
* @return the {@link Call} object
*/
public ServiceCall getUnixTimeAsync(final ServiceCallback<Long> serviceCallback) throws IllegalArgumentException {
public ServiceCall getUnixTimeAsync(final ServiceCallback<DateTime> serviceCallback) throws IllegalArgumentException {
if (serviceCallback == null) {
throw new IllegalArgumentException("ServiceCallback is required for async calls.");
}
Call<ResponseBody> call = service.getUnixTime();
final ServiceCall serviceCall = new ServiceCall(call);
call.enqueue(new ServiceResponseCallback<Long>(serviceCallback) {
call.enqueue(new ServiceResponseCallback<DateTime>(serviceCallback) {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
serviceCallback.success(getUnixTimeDelegate(response));
ServiceResponse<Long> result = getUnixTimeDelegate(response);
DateTime body = null;
if (result.getBody() != null) {
body = new DateTime(result.getBody() * 1000L, DateTimeZone.UTC);
}
serviceCallback.success(new ServiceResponse<DateTime>(body, result.getResponse()));
} catch (ErrorException | IOException exception) {
serviceCallback.failure(exception);
}
Expand All @@ -623,8 +635,9 @@ private ServiceResponse<Long> getUnixTimeDelegate(Response<ResponseBody> respons
* @throws IOException exception thrown from serialization/deserialization
* @return the {@link ServiceResponse} object if successful.
*/
public ServiceResponse<Void> putUnixTimeDate(long intBody) throws ErrorException, IOException {
Call<ResponseBody> call = service.putUnixTimeDate(intBody);
public ServiceResponse<Void> putUnixTimeDate(DateTime intBody) throws ErrorException, IOException {
Long intBodyConverted = intBody.toDateTime(DateTimeZone.UTC).getMillis() / 1000;
Call<ResponseBody> call = service.putUnixTimeDate(intBodyConverted);
return putUnixTimeDateDelegate(call.execute());
}

Expand All @@ -636,11 +649,12 @@ public ServiceResponse<Void> putUnixTimeDate(long intBody) throws ErrorException
* @throws IllegalArgumentException thrown if callback is null
* @return the {@link Call} object
*/
public ServiceCall putUnixTimeDateAsync(long intBody, final ServiceCallback<Void> serviceCallback) throws IllegalArgumentException {
public ServiceCall putUnixTimeDateAsync(DateTime intBody, final ServiceCallback<Void> serviceCallback) throws IllegalArgumentException {
if (serviceCallback == null) {
throw new IllegalArgumentException("ServiceCallback is required for async calls.");
}
Call<ResponseBody> call = service.putUnixTimeDate(intBody);
Long intBodyConverted = intBody.toDateTime(DateTimeZone.UTC).getMillis() / 1000;
Call<ResponseBody> call = service.putUnixTimeDate(intBodyConverted);
final ServiceCall serviceCall = new ServiceCall(call);
call.enqueue(new ServiceResponseCallback<Void>(serviceCallback) {
@Override
Expand All @@ -667,11 +681,16 @@ private ServiceResponse<Void> putUnixTimeDateDelegate(Response<ResponseBody> 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<Long> getInvalidUnixTime() throws ErrorException, IOException {
public ServiceResponse<DateTime> getInvalidUnixTime() throws ErrorException, IOException {
Call<ResponseBody> call = service.getInvalidUnixTime();
return getInvalidUnixTimeDelegate(call.execute());
ServiceResponse<Long> response = getInvalidUnixTimeDelegate(call.execute());
DateTime body = null;
if (response.getBody() != null) {
body = new DateTime(response.getBody() * 1000L, DateTimeZone.UTC);
}
return new ServiceResponse<DateTime>(body, response.getResponse());
}

/**
Expand All @@ -681,17 +700,22 @@ public ServiceResponse<Long> getInvalidUnixTime() throws ErrorException, IOExcep
* @throws IllegalArgumentException thrown if callback is null
* @return the {@link Call} object
*/
public ServiceCall getInvalidUnixTimeAsync(final ServiceCallback<Long> serviceCallback) throws IllegalArgumentException {
public ServiceCall getInvalidUnixTimeAsync(final ServiceCallback<DateTime> serviceCallback) throws IllegalArgumentException {
if (serviceCallback == null) {
throw new IllegalArgumentException("ServiceCallback is required for async calls.");
}
Call<ResponseBody> call = service.getInvalidUnixTime();
final ServiceCall serviceCall = new ServiceCall(call);
call.enqueue(new ServiceResponseCallback<Long>(serviceCallback) {
call.enqueue(new ServiceResponseCallback<DateTime>(serviceCallback) {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
serviceCallback.success(getInvalidUnixTimeDelegate(response));
ServiceResponse<Long> result = getInvalidUnixTimeDelegate(response);
DateTime body = null;
if (result.getBody() != null) {
body = new DateTime(result.getBody() * 1000L, DateTimeZone.UTC);
}
serviceCallback.success(new ServiceResponse<DateTime>(body, result.getResponse()));
} catch (ErrorException | IOException exception) {
serviceCallback.failure(exception);
}
Expand All @@ -712,11 +736,16 @@ private ServiceResponse<Long> getInvalidUnixTimeDelegate(Response<ResponseBody>
*
* @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<Long> getNullUnixTime() throws ErrorException, IOException {
public ServiceResponse<DateTime> getNullUnixTime() throws ErrorException, IOException {
Call<ResponseBody> call = service.getNullUnixTime();
return getNullUnixTimeDelegate(call.execute());
ServiceResponse<Long> response = getNullUnixTimeDelegate(call.execute());
DateTime body = null;
if (response.getBody() != null) {
body = new DateTime(response.getBody() * 1000L, DateTimeZone.UTC);
}
return new ServiceResponse<DateTime>(body, response.getResponse());
}

/**
Expand All @@ -726,17 +755,22 @@ public ServiceResponse<Long> getNullUnixTime() throws ErrorException, IOExceptio
* @throws IllegalArgumentException thrown if callback is null
* @return the {@link Call} object
*/
public ServiceCall getNullUnixTimeAsync(final ServiceCallback<Long> serviceCallback) throws IllegalArgumentException {
public ServiceCall getNullUnixTimeAsync(final ServiceCallback<DateTime> serviceCallback) throws IllegalArgumentException {
if (serviceCallback == null) {
throw new IllegalArgumentException("ServiceCallback is required for async calls.");
}
Call<ResponseBody> call = service.getNullUnixTime();
final ServiceCall serviceCall = new ServiceCall(call);
call.enqueue(new ServiceResponseCallback<Long>(serviceCallback) {
call.enqueue(new ServiceResponseCallback<DateTime>(serviceCallback) {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
serviceCallback.success(getNullUnixTimeDelegate(response));
ServiceResponse<Long> result = getNullUnixTimeDelegate(response);
DateTime body = null;
if (result.getBody() != null) {
body = new DateTime(result.getBody() * 1000L, DateTimeZone.UTC);
}
serviceCallback.success(new ServiceResponse<DateTime>(body, result.getResponse()));
} catch (ErrorException | IOException exception) {
serviceCallback.failure(exception);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ public interface Paths {
* @throws IOException exception thrown from serialization/deserialization
* @return the {@link ServiceResponse} object if successful.
*/
ServiceResponse<Void> unixTimeUrl(long unixTimeUrlPath) throws ErrorException, IOException;
ServiceResponse<Void> unixTimeUrl(DateTime unixTimeUrlPath) throws ErrorException, IOException;

/**
* Get the date 2016-04-13 encoded value as '1460505600' (Unix time).
Expand All @@ -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<Void> serviceCallback) throws IllegalArgumentException;
ServiceCall unixTimeUrlAsync(DateTime unixTimeUrlPath, final ServiceCallback<Void> serviceCallback) throws IllegalArgumentException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1431,8 +1432,9 @@ private ServiceResponse<Void> arrayCsvInPathDelegate(Response<ResponseBody> resp
* @throws IOException exception thrown from serialization/deserialization
* @return the {@link ServiceResponse} object if successful.
*/
public ServiceResponse<Void> unixTimeUrl(long unixTimeUrlPath) throws ErrorException, IOException {
Call<ResponseBody> call = service.unixTimeUrl(unixTimeUrlPath);
public ServiceResponse<Void> unixTimeUrl(DateTime unixTimeUrlPath) throws ErrorException, IOException {
Long unixTimeUrlPathConverted = unixTimeUrlPath.toDateTime(DateTimeZone.UTC).getMillis() / 1000;
Call<ResponseBody> call = service.unixTimeUrl(unixTimeUrlPathConverted);
return unixTimeUrlDelegate(call.execute());
}

Expand All @@ -1444,11 +1446,12 @@ public ServiceResponse<Void> unixTimeUrl(long unixTimeUrlPath) throws ErrorExcep
* @throws IllegalArgumentException thrown if callback is null
* @return the {@link Call} object
*/
public ServiceCall unixTimeUrlAsync(long unixTimeUrlPath, final ServiceCallback<Void> serviceCallback) throws IllegalArgumentException {
public ServiceCall unixTimeUrlAsync(DateTime unixTimeUrlPath, final ServiceCallback<Void> serviceCallback) throws IllegalArgumentException {
if (serviceCallback == null) {
throw new IllegalArgumentException("ServiceCallback is required for async calls.");
}
Call<ResponseBody> call = service.unixTimeUrl(unixTimeUrlPath);
Long unixTimeUrlPathConverted = unixTimeUrlPath.toDateTime(DateTimeZone.UTC).getMillis() / 1000;
Call<ResponseBody> call = service.unixTimeUrl(unixTimeUrlPathConverted);
final ServiceCall serviceCall = new ServiceCall(call);
call.enqueue(new ServiceResponseCallback<Void>(serviceCallback) {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -80,7 +85,8 @@ public void getUnderflowInt64() throws Exception {
public void putMax32() throws Exception {
client.ints().putMax32Async(Integer.MAX_VALUE, new ServiceCallback<Void>() {
@Override
public void failure(Throwable t) { }
public void failure(Throwable t) {
}

@Override
public void success(ServiceResponse<Void> response) {
Expand Down Expand Up @@ -111,7 +117,8 @@ public void success(ServiceResponse<Void> response) {
public void putMin32() throws Exception {
client.ints().putMin32Async(Integer.MIN_VALUE, new ServiceCallback<Void>() {
@Override
public void failure(Throwable t) { }
public void failure(Throwable t) {
}

@Override
public void success(ServiceResponse<Void> response) {
Expand All @@ -137,4 +144,31 @@ public void success(ServiceResponse<Void> 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);
}
}
Loading

0 comments on commit 6226c15

Please sign in to comment.