Skip to content

Commit

Permalink
Fixed the time marshalling issue when CBOR is disabled. See #1023
Browse files Browse the repository at this point in the history
  • Loading branch information
zoewangg committed Jan 22, 2019
1 parent 19e5043 commit ec0d95c
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changes/next-release/bugfix-AWSSDKforJavav2-626d4ef.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"category": "AWS SDK for Java v2",
"type": "bugfix",
"description": "Fixed the time marshalling issue when CBOR is disabled. See [#1023](https://github.com/aws/aws-sdk-java-v2/issues/1023)"
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package software.amazon.awssdk.protocols.cbor;

import java.util.Collections;
import java.util.HashMap;
import java.util.EnumMap;
import java.util.Map;
import software.amazon.awssdk.annotations.SdkProtectedApi;
import software.amazon.awssdk.core.SdkSystemSetting;
Expand Down Expand Up @@ -75,7 +75,13 @@ protected StructuredJsonFactory getSdkFactory() {
*/
@Override
protected Map<MarshallLocation, TimestampFormatTrait.Format> getDefaultTimestampFormats() {
Map<MarshallLocation, TimestampFormatTrait.Format> formats = new HashMap<>();

// If Cbor is disabled, getting the default timestamp format from parent class
if (!isCborEnabled()) {
return super.getDefaultTimestampFormats();
}

Map<MarshallLocation, TimestampFormatTrait.Format> formats = new EnumMap<>(MarshallLocation.class);
formats.put(MarshallLocation.HEADER, TimestampFormatTrait.Format.RFC_822);
formats.put(MarshallLocation.PAYLOAD, TimestampFormatTrait.Format.UNIX_TIMESTAMP_MILLIS);
return Collections.unmodifiableMap(formats);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.protocols.cbor;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static software.amazon.awssdk.core.SdkSystemSetting.CBOR_ENABLED;
import static software.amazon.awssdk.core.traits.TimestampFormatTrait.Format.RFC_822;
import static software.amazon.awssdk.core.traits.TimestampFormatTrait.Format.UNIX_TIMESTAMP;
import static software.amazon.awssdk.core.traits.TimestampFormatTrait.Format.UNIX_TIMESTAMP_MILLIS;

import java.util.Map;
import org.junit.BeforeClass;
import org.junit.Test;
import software.amazon.awssdk.core.protocol.MarshallLocation;
import software.amazon.awssdk.core.traits.TimestampFormatTrait;

public class AwsCborProtocolFactoryTest {

private static AwsCborProtocolFactory factory;

@BeforeClass
public static void setup() {
factory = AwsCborProtocolFactory.builder().build();
}

@Test
public void defaultTimestampFormats_cborEnabled() {
Map<MarshallLocation, TimestampFormatTrait.Format> defaultTimestampFormats = factory.getDefaultTimestampFormats();
assertThat(defaultTimestampFormats.get(MarshallLocation.HEADER)).isEqualTo(RFC_822);
assertThat(defaultTimestampFormats.get(MarshallLocation.PAYLOAD)).isEqualTo(UNIX_TIMESTAMP_MILLIS);
}

@Test
public void defaultTimestampFormats_cborDisabled() {
System.setProperty(CBOR_ENABLED.property(), "false");
try {
Map<MarshallLocation, TimestampFormatTrait.Format> defaultTimestampFormats = factory.getDefaultTimestampFormats();
assertThat(defaultTimestampFormats.get(MarshallLocation.HEADER)).isEqualTo(RFC_822);
assertThat(defaultTimestampFormats.get(MarshallLocation.PAYLOAD)).isEqualTo(UNIX_TIMESTAMP);
} finally {
System.clearProperty(CBOR_ENABLED.property());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.services.kinesis;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static org.assertj.core.api.Assertions.assertThat;
import static software.amazon.awssdk.core.SdkSystemSetting.CBOR_ENABLED;

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import java.net.URI;
import java.time.Instant;
import java.util.List;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.kinesis.model.Record;

@RunWith(MockitoJUnitRunner.class)
public class DateTimeUnmarshallingTest {

private KinesisClient client;

@Rule
public WireMockRule wireMock = new WireMockRule(0);

@Before
public void setup() {
System.setProperty(CBOR_ENABLED.property(), "false");
client = KinesisClient.builder()
.endpointOverride(URI.create("http://localhost:" + wireMock.port()))
.credentialsProvider(() -> AwsBasicCredentials.create("test", "test"))
.region(Region.US_EAST_1)
.build();
}

@Test
public void cborDisabled_dateUnmarshalling_shouldSucceed() {
String content = content();
int length = content.getBytes().length;
Instant instant = Instant.ofEpochMilli(1548118964772L);

stubFor(post(anyUrl())
.willReturn(aResponse().withStatus(200)
.withHeader("Content-Length", String.valueOf(length))
.withBody(content)));


List<Record> records = client.getRecords(b -> b.shardIterator("test")).records();

assertThat(records).isNotEmpty();
assertThat(records.get(0).approximateArrivalTimestamp()).isEqualTo(instant);
}

private String content() {
return "{\n"
+ " \"MillisBehindLatest\": 0,\n"
+ " \"NextShardIterator\": \"test\",\n"
+ " \"Records\": [{\n"
+ " \"ApproximateArrivalTimestamp\": 1.548118964772E9,\n"
+ " \"Data\": \"U2VlIE5vIEV2aWw=\",\n"
+ " \"PartitionKey\": \"foobar\",\n"
+ " \"SequenceNumber\": \"12345678\"\n"
+ " }]\n"
+ "}";
}
}

0 comments on commit ec0d95c

Please sign in to comment.