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.core.waiters; 17 18 import static org.assertj.core.api.Assertions.assertThat; 19 import static org.assertj.core.api.Assertions.assertThatThrownBy; 20 21 import java.util.function.Predicate; 22 import org.testng.annotations.Test; 23 24 25 public class WaiterAcceptorTest { 26 private static final String STRING_TO_MATCH = "foobar"; 27 private static final Predicate<String> STRING_PREDICATE = s -> s.equals(STRING_TO_MATCH); 28 private static final Predicate<Throwable> EXCEPTION_PREDICATE = s -> s.getMessage().equals(STRING_TO_MATCH); 29 30 @Test successOnResponseAcceptor()31 public void successOnResponseAcceptor() { 32 WaiterAcceptor<String> objectWaiterAcceptor = WaiterAcceptor 33 .successOnResponseAcceptor(STRING_PREDICATE); 34 35 assertThat(objectWaiterAcceptor.matches(STRING_TO_MATCH)).isTrue(); 36 assertThat(objectWaiterAcceptor.matches("blah")).isFalse(); 37 assertThat(objectWaiterAcceptor.waiterState()).isEqualTo(WaiterState.SUCCESS); 38 assertThat(objectWaiterAcceptor.message()).isEmpty(); 39 } 40 41 @Test errorOnResponseAcceptor()42 public void errorOnResponseAcceptor() { 43 WaiterAcceptor<String> objectWaiterAcceptor = WaiterAcceptor 44 .errorOnResponseAcceptor(STRING_PREDICATE); 45 46 assertThat(objectWaiterAcceptor.matches(STRING_TO_MATCH)).isTrue(); 47 assertThat(objectWaiterAcceptor.matches("blah")).isFalse(); 48 assertThat(objectWaiterAcceptor.waiterState()).isEqualTo(WaiterState.FAILURE); 49 assertThat(objectWaiterAcceptor.message()).isEmpty(); 50 } 51 52 @Test errorOnResponseAcceptorWithMsg()53 public void errorOnResponseAcceptorWithMsg() { 54 WaiterAcceptor<String> objectWaiterAcceptor = WaiterAcceptor 55 .errorOnResponseAcceptor(STRING_PREDICATE, "wrong response"); 56 57 assertThat(objectWaiterAcceptor.matches(STRING_TO_MATCH)).isTrue(); 58 assertThat(objectWaiterAcceptor.matches("blah")).isFalse(); 59 assertThat(objectWaiterAcceptor.waiterState()).isEqualTo(WaiterState.FAILURE); 60 assertThat(objectWaiterAcceptor.message()).contains("wrong response"); 61 } 62 63 @Test errorOnResponseAcceptor_nullMsg_shouldThrowException()64 public void errorOnResponseAcceptor_nullMsg_shouldThrowException() { 65 assertThatThrownBy(() -> WaiterAcceptor 66 .errorOnResponseAcceptor(STRING_PREDICATE, null)).hasMessageContaining("message must not be null"); 67 } 68 69 @Test errorOnResponseAcceptor_nullPredicate_shouldThrowException()70 public void errorOnResponseAcceptor_nullPredicate_shouldThrowException() { 71 assertThatThrownBy(() -> WaiterAcceptor 72 .errorOnResponseAcceptor(null)).hasMessageContaining("responsePredicate must not be null"); 73 } 74 75 @Test successOnExceptionAcceptor()76 public void successOnExceptionAcceptor() { 77 WaiterAcceptor<String> objectWaiterAcceptor = WaiterAcceptor 78 .successOnExceptionAcceptor(EXCEPTION_PREDICATE); 79 80 assertThat(objectWaiterAcceptor.matches(new RuntimeException(STRING_TO_MATCH))).isTrue(); 81 assertThat(objectWaiterAcceptor.matches(new RuntimeException("blah"))).isFalse(); 82 assertThat(objectWaiterAcceptor.waiterState()).isEqualTo(WaiterState.SUCCESS); 83 assertThat(objectWaiterAcceptor.message()).isEmpty(); 84 } 85 86 @Test errorOnExceptionAcceptor()87 public void errorOnExceptionAcceptor() { 88 WaiterAcceptor<String> objectWaiterAcceptor = WaiterAcceptor 89 .errorOnExceptionAcceptor(EXCEPTION_PREDICATE); 90 91 assertThat(objectWaiterAcceptor.matches(new RuntimeException(STRING_TO_MATCH))).isTrue(); 92 assertThat(objectWaiterAcceptor.matches(new RuntimeException("blah"))).isFalse(); 93 assertThat(objectWaiterAcceptor.waiterState()).isEqualTo(WaiterState.FAILURE); 94 assertThat(objectWaiterAcceptor.message()).isEmpty(); 95 } 96 97 @Test retryOnExceptionAcceptor()98 public void retryOnExceptionAcceptor() { 99 WaiterAcceptor<String> objectWaiterAcceptor = WaiterAcceptor 100 .retryOnExceptionAcceptor(EXCEPTION_PREDICATE); 101 102 assertThat(objectWaiterAcceptor.matches(new RuntimeException(STRING_TO_MATCH))).isTrue(); 103 assertThat(objectWaiterAcceptor.matches(new RuntimeException("blah"))).isFalse(); 104 assertThat(objectWaiterAcceptor.waiterState()).isEqualTo(WaiterState.RETRY); 105 assertThat(objectWaiterAcceptor.message()).isEmpty(); 106 } 107 } 108