com.amazonaws.http.SdkHttpMetadata Java Examples

The following examples show how to use com.amazonaws.http.SdkHttpMetadata. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KinesisMockWriteTest.java    From beam with Apache License 2.0 5 votes vote down vote up
private AmazonKinesis getMockedAmazonKinesisClient() {
  int statusCode = isExistingStream ? 200 : 404;
  SdkHttpMetadata httpMetadata = mock(SdkHttpMetadata.class);
  when(httpMetadata.getHttpStatusCode()).thenReturn(statusCode);

  DescribeStreamResult streamResult = mock(DescribeStreamResult.class);
  when(streamResult.getSdkHttpMetadata()).thenReturn(httpMetadata);

  AmazonKinesis client = mock(AmazonKinesis.class);
  when(client.describeStream(any(String.class))).thenReturn(streamResult);

  return client;
}
 
Example #2
Source File: AmazonKinesisMock.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public DescribeStreamResult describeStream(String streamName, String exclusiveStartShardId) {
  if (rateLimitDescribeStream-- > 0) {
    throw new LimitExceededException("DescribeStream rate limit exceeded");
  }
  int nextShardId = 0;
  if (exclusiveStartShardId != null) {
    nextShardId = parseInt(exclusiveStartShardId) + 1;
  }
  boolean hasMoreShards = nextShardId + 1 < shardedData.size();

  List<Shard> shards = new ArrayList<>();
  if (nextShardId < shardedData.size()) {
    shards.add(new Shard().withShardId(Integer.toString(nextShardId)));
  }

  HttpResponse response = new HttpResponse(null, null);
  response.setStatusCode(200);
  DescribeStreamResult result = new DescribeStreamResult();
  result.setSdkHttpMetadata(SdkHttpMetadata.from(response));
  result.withStreamDescription(
      new StreamDescription()
          .withHasMoreShards(hasMoreShards)
          .withShards(shards)
          .withStreamName(streamName));
  return result;
}
 
Example #3
Source File: AwsCoders.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public void encode(SdkHttpMetadata value, OutputStream outStream)
    throws CoderException, IOException {
  STATUS_CODE_CODER.encode(value.getHttpStatusCode(), outStream);
  if (includeHeaders) {
    HEADERS_ENCODER.encode(value.getHttpHeaders(), outStream);
  }
}
 
Example #4
Source File: AwsCoders.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public SdkHttpMetadata decode(InputStream inStream) throws CoderException, IOException {
  final int httpStatusCode = STATUS_CODE_CODER.decode(inStream);
  HttpResponse httpResponse = new HttpResponse(null, null);
  httpResponse.setStatusCode(httpStatusCode);
  if (includeHeaders) {
    Optional.ofNullable(HEADERS_ENCODER.decode(inStream))
        .ifPresent(
            headers ->
                headers.keySet().forEach(k -> httpResponse.addHeader(k, headers.get(k))));
  }
  return SdkHttpMetadata.from(httpResponse);
}
 
Example #5
Source File: PublishResultCodersTest.java    From beam with Apache License 2.0 5 votes vote down vote up
private PublishResult buildFullPublishResult() {
  PublishResult publishResult = new PublishResult().withMessageId(UUID.randomUUID().toString());
  publishResult.setSdkResponseMetadata(
      new ResponseMetadata(
          ImmutableMap.of(ResponseMetadata.AWS_REQUEST_ID, UUID.randomUUID().toString())));
  HttpResponse httpResponse = new HttpResponse(null, null);
  httpResponse.setStatusCode(200);
  httpResponse.addHeader("Content-Type", "application/json");
  publishResult.setSdkHttpMetadata(SdkHttpMetadata.from(httpResponse));
  return publishResult;
}
 
Example #6
Source File: SnsIOTest.java    From beam with Apache License 2.0 5 votes vote down vote up
private static AmazonSNS getAmazonSnsMockSuccess() {
  final AmazonSNS amazonSNS = Mockito.mock(AmazonSNS.class);
  configureAmazonSnsMock(amazonSNS);

  final PublishResult result = Mockito.mock(PublishResult.class);
  final SdkHttpMetadata metadata = Mockito.mock(SdkHttpMetadata.class);
  Mockito.when(metadata.getHttpHeaders()).thenReturn(new HashMap<>());
  Mockito.when(metadata.getHttpStatusCode()).thenReturn(200);
  Mockito.when(result.getSdkHttpMetadata()).thenReturn(metadata);
  Mockito.when(result.getMessageId()).thenReturn(UUID.randomUUID().toString());
  Mockito.when(amazonSNS.publish(Mockito.any())).thenReturn(result);
  return amazonSNS;
}
 
Example #7
Source File: SnsIOTest.java    From beam with Apache License 2.0 5 votes vote down vote up
private static void configureAmazonSnsMock(AmazonSNS amazonSNS) {
  final GetTopicAttributesResult result = Mockito.mock(GetTopicAttributesResult.class);
  final SdkHttpMetadata metadata = Mockito.mock(SdkHttpMetadata.class);
  Mockito.when(metadata.getHttpHeaders()).thenReturn(new HashMap<>());
  Mockito.when(metadata.getHttpStatusCode()).thenReturn(200);
  Mockito.when(result.getSdkHttpMetadata()).thenReturn(metadata);
  Mockito.when(amazonSNS.getTopicAttributes(Mockito.anyString())).thenReturn(result);
}
 
Example #8
Source File: AwsCodersTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testSdkHttpMetadataDecodeEncodeEquals() throws Exception {
  SdkHttpMetadata value = buildSdkHttpMetadata();
  SdkHttpMetadata clone = CoderUtils.clone(AwsCoders.sdkHttpMetadata(), value);
  assertThat(clone.getHttpStatusCode(), equalTo(value.getHttpStatusCode()));
  assertThat(clone.getHttpHeaders(), equalTo(value.getHttpHeaders()));
}
 
Example #9
Source File: AwsCodersTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testSdkHttpMetadataWithoutHeadersDecodeEncodeEquals() throws Exception {
  SdkHttpMetadata value = buildSdkHttpMetadata();
  SdkHttpMetadata clone = CoderUtils.clone(AwsCoders.sdkHttpMetadataWithoutHeaders(), value);
  assertThat(clone.getHttpStatusCode(), equalTo(value.getHttpStatusCode()));
  assertThat(clone.getHttpHeaders().isEmpty(), equalTo(true));
}
 
Example #10
Source File: AWSAppAutoScalingClientTest.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
@Test
public void deleteScalingPolicyIsIdempotent() {
    String jobId = UUID.randomUUID().toString();
    String policyId = UUID.randomUUID().toString();

    AWSApplicationAutoScalingAsync clientAsync = mock(AWSApplicationAutoScalingAsync.class);
    AWSAppScalingConfig config = mock(AWSAppScalingConfig.class);
    AWSAppAutoScalingClient autoScalingClient = new AWSAppAutoScalingClient(clientAsync, config, new NoopRegistry());

    // delete happens successfully on the first attempt
    AtomicBoolean isDeleted = new AtomicBoolean(false);
    when(clientAsync.deleteScalingPolicyAsync(any(), any())).thenAnswer(invocation -> {
        DeleteScalingPolicyRequest request = invocation.getArgument(0);
        AsyncHandler<DeleteScalingPolicyRequest, DeleteScalingPolicyResult> handler = invocation.getArgument(1);

        if (isDeleted.get()) {
            ObjectNotFoundException notFoundException = new ObjectNotFoundException(policyId + " does not exist");
            handler.onError(notFoundException);
            return Future.failed(notFoundException);
        }

        DeleteScalingPolicyResult resultSuccess = new DeleteScalingPolicyResult();
        HttpResponse successResponse = new HttpResponse(null, null);
        successResponse.setStatusCode(200);
        resultSuccess.setSdkHttpMetadata(SdkHttpMetadata.from(successResponse));

        isDeleted.set(true);
        handler.onSuccess(request, resultSuccess);
        return Future.successful(resultSuccess);
    });

    AssertableSubscriber<Void> firstCall = autoScalingClient.deleteScalingPolicy(policyId, jobId).test();
    firstCall.awaitTerminalEvent(2, TimeUnit.SECONDS);
    firstCall.assertNoErrors();
    firstCall.assertCompleted();
    verify(clientAsync, times(1)).deleteScalingPolicyAsync(any(), any());

    // second should complete fast when NotFound and not retry with exponential backoff
    AssertableSubscriber<Void> secondCall = autoScalingClient.deleteScalingPolicy(policyId, jobId).test();
    secondCall.awaitTerminalEvent(2, TimeUnit.SECONDS);
    secondCall.assertNoErrors();
    secondCall.assertCompleted();
    verify(clientAsync, times(2)).deleteScalingPolicyAsync(any(), any());
}
 
Example #11
Source File: AWSAppAutoScalingClientTest.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
@Test
public void deleteScalableTargetIsIdempotent() {
    String jobId = UUID.randomUUID().toString();
    String policyId = UUID.randomUUID().toString();

    AWSApplicationAutoScalingAsync clientAsync = mock(AWSApplicationAutoScalingAsync.class);
    AWSAppScalingConfig config = mock(AWSAppScalingConfig.class);
    AWSAppAutoScalingClient autoScalingClient = new AWSAppAutoScalingClient(clientAsync, config, new NoopRegistry());

    AtomicBoolean isDeleted = new AtomicBoolean(false);
    when(clientAsync.deregisterScalableTargetAsync(any(), any())).thenAnswer(invocation -> {
        DeregisterScalableTargetRequest request = invocation.getArgument(0);
        AsyncHandler<DeregisterScalableTargetRequest, DeregisterScalableTargetResult> handler = invocation.getArgument(1);
        if (isDeleted.get()) {
            ObjectNotFoundException notFoundException = new ObjectNotFoundException(policyId + " does not exist");
            handler.onError(notFoundException);
            return Future.failed(notFoundException);
        }

        DeregisterScalableTargetResult resultSuccess = new DeregisterScalableTargetResult();
        HttpResponse successResponse = new HttpResponse(null, null);
        successResponse.setStatusCode(200);
        resultSuccess.setSdkHttpMetadata(SdkHttpMetadata.from(successResponse));

        isDeleted.set(true);
        handler.onSuccess(request, resultSuccess);
        return Future.successful(resultSuccess);
    });

    AssertableSubscriber<Void> firstCall = autoScalingClient.deleteScalableTarget(jobId).test();
    firstCall.awaitTerminalEvent(2, TimeUnit.SECONDS);
    firstCall.assertNoErrors();
    firstCall.assertCompleted();
    verify(clientAsync, times(1)).deregisterScalableTargetAsync(any(), any());

    // second should complete fast when NotFound and not retry with exponential backoff
    AssertableSubscriber<Void> secondCall = autoScalingClient.deleteScalableTarget(jobId).test();
    secondCall.awaitTerminalEvent(2, TimeUnit.SECONDS);
    secondCall.assertNoErrors();
    secondCall.assertCompleted();
    verify(clientAsync, times(2)).deregisterScalableTargetAsync(any(), any());
}
 
Example #12
Source File: PublishResultCoders.java    From beam with Apache License 2.0 4 votes vote down vote up
private PublishResultCoder(
    Coder<ResponseMetadata> responseMetadataEncoder,
    Coder<SdkHttpMetadata> sdkHttpMetadataCoder) {
  this.responseMetadataEncoder = responseMetadataEncoder;
  this.sdkHttpMetadataCoder = sdkHttpMetadataCoder;
}
 
Example #13
Source File: AwsCodersTest.java    From beam with Apache License 2.0 4 votes vote down vote up
private SdkHttpMetadata buildSdkHttpMetadata() {
  HttpResponse httpResponse = new HttpResponse(null, null);
  httpResponse.setStatusCode(200);
  httpResponse.addHeader("Content-Type", "application/json");
  return SdkHttpMetadata.from(httpResponse);
}
 
Example #14
Source File: AwsCoders.java    From beam with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a new coder for SdkHttpMetadata.
 *
 * @return the SdkHttpMetadata coder
 */
public static Coder<SdkHttpMetadata> sdkHttpMetadata() {
  return new SdkHttpMetadataCoder(true);
}
 
Example #15
Source File: AwsCoders.java    From beam with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a new coder for SdkHttpMetadata that does not serialize the response headers.
 *
 * @return the SdkHttpMetadata coder
 */
public static Coder<SdkHttpMetadata> sdkHttpMetadataWithoutHeaders() {
  return new SdkHttpMetadataCoder(false);
}