1 /* 2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"). 5 * You may not use this file except in compliance with the License. 6 * A copy of the License is located at 7 * 8 * http://aws.amazon.com/apache2.0 9 * 10 * or in the "license" file accompanying this file. This file is distributed 11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 * express or implied. See the License for the specific language governing 13 * permissions and limitations under the License. 14 */ 15 16 package software.amazon.awssdk.modulepath.tests.mocktests; 17 18 import java.io.ByteArrayInputStream; 19 import java.util.concurrent.CompletionException; 20 import org.slf4j.Logger; 21 import org.slf4j.LoggerFactory; 22 import software.amazon.awssdk.awscore.exception.AwsServiceException; 23 import software.amazon.awssdk.http.AbortableInputStream; 24 import software.amazon.awssdk.http.HttpExecuteResponse; 25 import software.amazon.awssdk.http.SdkHttpResponse; 26 27 /** 28 * Base classes to mock sync and async api calls. 29 */ 30 public abstract class BaseMockApiCall { 31 32 private static final Logger logger = LoggerFactory.getLogger(BaseMockApiCall.class); 33 34 protected final MockHttpClient mockHttpClient; 35 protected final MockAyncHttpClient mockAyncHttpClient; 36 private final String protocol; 37 BaseMockApiCall(String protocol)38 public BaseMockApiCall(String protocol) { 39 this.protocol = protocol; 40 this.mockHttpClient = new MockHttpClient(); 41 this.mockAyncHttpClient = new MockAyncHttpClient(); 42 } 43 successfulApiCall()44 public void successfulApiCall() { 45 logger.info("stubing successful api call for {} protocol", protocol); 46 mockHttpClient.stubNextResponse(successResponse(protocol)); 47 runnable().run(); 48 mockHttpClient.reset(); 49 } 50 failedApiCall()51 public void failedApiCall() { 52 logger.info("stubing failed api call for {} protocol", protocol); 53 mockHttpClient.stubNextResponse(errorResponse(protocol)); 54 55 try { 56 runnable().run(); 57 } catch (AwsServiceException e) { 58 logger.info("Received expected service exception", e.getMessage()); 59 } 60 61 mockHttpClient.reset(); 62 } 63 successfulAsyncApiCall()64 public void successfulAsyncApiCall() { 65 logger.info("stubing successful async api call for {} protocol", protocol); 66 mockAyncHttpClient.stubNextResponse(successResponse(protocol)); 67 asyncRunnable().run(); 68 mockAyncHttpClient.reset(); 69 } 70 failedAsyncApiCall()71 public void failedAsyncApiCall() { 72 logger.info("stubing failed async api call for {} protocol", protocol); 73 mockAyncHttpClient.stubNextResponse(errorResponse(protocol)); 74 75 try { 76 asyncRunnable().run(); 77 } catch (CompletionException e) { 78 if (e.getCause() instanceof AwsServiceException) { 79 logger.info("expected service exception {}", e.getMessage()); 80 } else { 81 throw new RuntimeException("Unexpected exception is thrown"); 82 } 83 } 84 85 mockAyncHttpClient.reset(); 86 } 87 runnable()88 abstract Runnable runnable(); 89 asyncRunnable()90 abstract Runnable asyncRunnable(); 91 successResponse(String protocol)92 private HttpExecuteResponse successResponse(String protocol) { 93 return HttpExecuteResponse.builder() 94 .response(SdkHttpResponse.builder() 95 .statusCode(200) 96 .build()) 97 .responseBody(generateContent(protocol)) 98 .build(); 99 } 100 errorResponse(String protocol)101 private HttpExecuteResponse errorResponse(String protocol) { 102 return HttpExecuteResponse.builder() 103 .response(SdkHttpResponse.builder() 104 .statusCode(500) 105 .build()) 106 .responseBody(generateContent(protocol)) 107 .build(); 108 } 109 generateContent(String protocol)110 private AbortableInputStream generateContent(String protocol) { 111 String content; 112 switch (protocol) { 113 case "xml": 114 content = "<foo></foo>"; 115 break; 116 default: 117 case "json": 118 content = "{}"; 119 } 120 121 return AbortableInputStream.create(new ByteArrayInputStream(content.getBytes())); 122 } 123 } 124