use of com.azure.android.core.serde.jackson.JacksonSerder in project azure-sdk-for-android by Azure.
the class HttpResponseMapperTests method base64EncodedContent.
@Test
public void base64EncodedContent() throws Throwable {
Class<Base64EncodedContentMethods> clazz = Base64EncodedContentMethods.class;
Method base64UrlMethod = clazz.getDeclaredMethod("base64Url", Callback.class);
HttpResponseMapper mapperBase64 = new HttpResponseMapper(base64UrlMethod, extractCallbackType(base64UrlMethod), logger);
JacksonSerder jacksonSerder = new JacksonSerder();
final String valueToEncode = "hello azure android";
// aGVsbG8gYXp1cmUgYW5kcm9pZA== (plain bytes).
final byte[] base64EncodedBytes = Base64Url.encode(valueToEncode.getBytes()).encodedBytes();
MockHttpResponse httpResponseBase64EncodedBytes0 = new MockHttpResponse(HttpMethod.GET, "https://raw.host.com", 200, new HttpHeaders(), base64EncodedBytes);
Response<byte[]> restResponseBase64DecodedBytes0 = (Response<byte[]>) mapperBase64.map(httpResponseBase64EncodedBytes0, jacksonSerder);
byte[] decodedBytes0 = restResponseBase64DecodedBytes0.getValue();
assertNotNull(decodedBytes0);
assertArrayEquals(valueToEncode.getBytes(), decodedBytes0);
// "aGVsbG8gYXp1cmUgYW5kcm9pZA==" (bytes wrapped in a string, so a valid json string).
byte[] base64EncodedBytesAsJsonString = new byte[base64EncodedBytes.length + 2];
System.arraycopy(base64EncodedBytes, 0, base64EncodedBytesAsJsonString, 1, base64EncodedBytes.length);
base64EncodedBytesAsJsonString[0] = '"';
base64EncodedBytesAsJsonString[base64EncodedBytesAsJsonString.length - 1] = '"';
MockHttpResponse httpResponseBase64EncodedBytes1 = new MockHttpResponse(HttpMethod.GET, "https://raw.host.com", 200, new HttpHeaders(), base64EncodedBytesAsJsonString);
Response<byte[]> restResponseBase64DecodedBytes1 = (Response<byte[]>) mapperBase64.map(httpResponseBase64EncodedBytes1, jacksonSerder);
byte[] decodedBytes1 = restResponseBase64DecodedBytes1.getValue();
assertNotNull(decodedBytes1);
assertArrayEquals(valueToEncode.getBytes(), decodedBytes1);
}
use of com.azure.android.core.serde.jackson.JacksonSerder in project azure-sdk-for-android by Azure.
the class HttpResponseMapperTests method anyError.
@Test
public void anyError() throws Throwable {
Class<AnyErrorMethods> clazz = AnyErrorMethods.class;
Method getAnyErrorMethod = clazz.getDeclaredMethod("getAnyError", Callback.class);
HttpResponseMapper mapper = new HttpResponseMapper(getAnyErrorMethod, extractCallbackType(getAnyErrorMethod), logger);
MockHttpResponse httpResponse = new MockHttpResponse(HttpMethod.GET, "https://raw.host.com", 409, new HttpHeaders(), "Unknown error".getBytes());
HttpResponseException ex = null;
try {
mapper.map(httpResponse, new JacksonSerder());
} catch (HttpResponseException e) {
ex = e;
}
assertNotNull(ex);
assertNotNull(ex.getMessage());
assertTrue(ex.getMessage().contains("Unknown error"));
}
use of com.azure.android.core.serde.jackson.JacksonSerder in project azure-sdk-for-android by Azure.
the class HttpResponseMapperTests method unixTimeEncodedContentList.
@Test
public void unixTimeEncodedContentList() throws Throwable {
Class<UnixTimeEncodedContentMethods> clazz = UnixTimeEncodedContentMethods.class;
Method unixTimeListMethod = clazz.getDeclaredMethod("unixTimeList", Callback.class);
HttpResponseMapper mapperUnixTime = new HttpResponseMapper(unixTimeListMethod, extractCallbackType(unixTimeListMethod), logger);
JacksonSerder jacksonSerder = new JacksonSerder();
OffsetDateTime offsetDateTime0 = OffsetDateTime.parse("1980-01-01T10:00:00Z");
OffsetDateTime offsetDateTime1 = OffsetDateTime.parse("1981-01-01T10:00:00Z");
List<Integer> wireUnixTimeList = new ArrayList<>();
wireUnixTimeList.add(Integer.parseInt(new UnixTime(offsetDateTime0).toString()));
wireUnixTimeList.add(Integer.parseInt(new UnixTime(offsetDateTime1).toString()));
String wireUnixTimeJsonList = jacksonSerder.serialize(wireUnixTimeList, SerdeEncoding.JSON);
MockHttpResponse httpResponseOffsetDateTimeList = new MockHttpResponse(HttpMethod.GET, "https://raw.host.com", 200, new HttpHeaders(), wireUnixTimeJsonList.getBytes());
Response<List<OffsetDateTime>> restResponseBase64OffsetDataTimeList = (Response<List<OffsetDateTime>>) mapperUnixTime.map(httpResponseOffsetDateTimeList, jacksonSerder);
List<OffsetDateTime> dateTimeListReceived = restResponseBase64OffsetDataTimeList.getValue();
assertNotNull(dateTimeListReceived);
assertEquals(2, dateTimeListReceived.size());
assertEquals(0, dateTimeListReceived.get(0).compareTo(offsetDateTime0));
assertEquals(0, dateTimeListReceived.get(1).compareTo(offsetDateTime1));
}
use of com.azure.android.core.serde.jackson.JacksonSerder in project azure-sdk-for-android by Azure.
the class HttpResponseMapperTests method streamAndBytes.
@Test
public void streamAndBytes() throws Throwable {
byte[] wireBytes = new byte[20];
new Random().nextBytes(wireBytes);
Class<StreamAndBytesMethods> clazz = StreamAndBytesMethods.class;
final Method photoStreamMethod = clazz.getDeclaredMethod("photoStream", Callback.class);
HttpResponseMapper mapper1 = new HttpResponseMapper(photoStreamMethod, extractCallbackType(photoStreamMethod), logger);
MockHttpResponse httpResponse1 = new MockHttpResponse(HttpMethod.GET, "https://raw.host.com", 200, new HttpHeaders(), wireBytes);
Response<InputStream> restStreamResponse = (Response<InputStream>) mapper1.map(httpResponse1, new JacksonSerder());
InputStream photoStream = restStreamResponse.getValue();
assertNotNull(photoStream);
assertFalse(httpResponse1.isClosed());
assertArrayEquals(wireBytes, streamToBytes(photoStream));
final Method photoBytesMethod = clazz.getDeclaredMethod("photoBytes", Callback.class);
HttpResponseMapper mapper2 = new HttpResponseMapper(photoBytesMethod, extractCallbackType(photoBytesMethod), logger);
MockHttpResponse httpResponse2 = new MockHttpResponse(HttpMethod.GET, "https://raw.host.com", 200, new HttpHeaders(), wireBytes);
Response<byte[]> restBytesResponse = (Response<byte[]>) mapper2.map(httpResponse2, new JacksonSerder());
byte[] photoBytes = restBytesResponse.getValue();
assertNotNull(photoBytes);
assertFalse(httpResponse2.isClosed());
assertArrayEquals(wireBytes, photoBytes);
}
use of com.azure.android.core.serde.jackson.JacksonSerder in project azure-sdk-for-android by Azure.
the class HttpResponseMapperTests method unixTimeEncodedContentMap.
@Test
public void unixTimeEncodedContentMap() throws Throwable {
Class<UnixTimeEncodedContentMethods> clazz = UnixTimeEncodedContentMethods.class;
Method unixTimeMapMethod = clazz.getDeclaredMethod("unixTimeMap", Callback.class);
HttpResponseMapper mapperDateTimeRfc1123 = new HttpResponseMapper(unixTimeMapMethod, extractCallbackType(unixTimeMapMethod), logger);
JacksonSerder jacksonSerder = new JacksonSerder();
OffsetDateTime offsetDateTime0 = OffsetDateTime.parse("1980-01-01T10:00:00Z");
OffsetDateTime offsetDateTime1 = OffsetDateTime.parse("1981-01-01T10:00:00Z");
Map<String, Integer> wireUnixTimeMap = new HashMap<>();
wireUnixTimeMap.put("v0", Integer.parseInt(new UnixTime(offsetDateTime0).toString()));
wireUnixTimeMap.put("v1", Integer.parseInt(new UnixTime(offsetDateTime1).toString()));
String wireDateTimeRfc1123JsonMap = jacksonSerder.serialize(wireUnixTimeMap, SerdeEncoding.JSON);
MockHttpResponse httpResponseDateTimeOffsetMap = new MockHttpResponse(HttpMethod.GET, "https://raw.host.com", 200, new HttpHeaders(), wireDateTimeRfc1123JsonMap.getBytes());
Response<Map<String, OffsetDateTime>> restResponseOffsetDataTimeMap = (Response<Map<String, OffsetDateTime>>) mapperDateTimeRfc1123.map(httpResponseDateTimeOffsetMap, jacksonSerder);
Map<String, OffsetDateTime> dateTimeMapReceived = restResponseOffsetDataTimeMap.getValue();
assertNotNull(dateTimeMapReceived);
assertEquals(2, dateTimeMapReceived.size());
assertEquals(0, dateTimeMapReceived.get("v0").compareTo(offsetDateTime0));
assertEquals(0, dateTimeMapReceived.get("v1").compareTo(offsetDateTime1));
}
Aggregations