]> git.basschouten.com Git - openhab-addons.git/blob
47bee39d1743529092ed7f5c96eda64d7676e3f3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mielecloud.internal.webservice.retry;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.ArgumentMatchers.*;
17 import static org.mockito.Mockito.*;
18
19 import java.util.Objects;
20 import java.util.function.Consumer;
21 import java.util.function.Supplier;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.junit.jupiter.api.Test;
26 import org.junit.jupiter.api.extension.ExtendWith;
27 import org.mockito.Mock;
28 import org.mockito.invocation.InvocationOnMock;
29 import org.mockito.junit.jupiter.MockitoExtension;
30 import org.mockito.stubbing.Answer;
31 import org.openhab.binding.mielecloud.internal.util.MockUtil;
32
33 /**
34  * @author Björn Lange - Initial contribution
35  */
36 @ExtendWith(MockitoExtension.class)
37 @NonNullByDefault
38 public class RetryStrategyCombinerTest {
39     private static final String STRING_CONSTANT = "Some String";
40
41     private final RetryStrategy first = mock(RetryStrategy.class);
42     private final RetryStrategy second = mock(RetryStrategy.class);
43
44     @Mock
45     @Nullable
46     private Supplier<@Nullable String> supplier;
47     @Mock
48     @Nullable
49     private Consumer<Exception> consumer;
50
51     private Supplier<@Nullable String> getSupplier() {
52         assertNotNull(supplier);
53         return Objects.requireNonNull(supplier);
54     }
55
56     private Consumer<Exception> getConsumer() {
57         assertNotNull(consumer);
58         return Objects.requireNonNull(consumer);
59     }
60
61     @SuppressWarnings("unchecked")
62     @Test
63     public void testPerformRetryableOperationInvokesRetryStrategiesInCorrectOrder() {
64         // given:
65         when(first.<@Nullable String> performRetryableOperation(any(Supplier.class), any()))
66                 .thenAnswer(new Answer<@Nullable String>() {
67                     @Override
68                     @Nullable
69                     public String answer(@Nullable InvocationOnMock invocation) throws Throwable {
70                         Supplier<String> inner = MockUtil.requireNonNull(invocation).getArgument(0);
71                         return inner.get();
72                     }
73                 });
74         when(second.<@Nullable String> performRetryableOperation(any(Supplier.class), any()))
75                 .thenAnswer(new Answer<@Nullable String>() {
76                     @Override
77                     @Nullable
78                     public String answer(@Nullable InvocationOnMock invocation) throws Throwable {
79                         Supplier<String> inner = MockUtil.requireNonNull(invocation).getArgument(0);
80                         return inner.get();
81                     }
82                 });
83         when(getSupplier().get()).thenReturn(STRING_CONSTANT);
84
85         RetryStrategyCombiner combiner = new RetryStrategyCombiner(first, second);
86
87         // when:
88         String result = combiner.performRetryableOperation(getSupplier(), getConsumer());
89
90         // then:
91         assertEquals(STRING_CONSTANT, result);
92         verify(first).performRetryableOperation(any(Supplier.class), eq(getConsumer()));
93         verify(second).performRetryableOperation(any(Supplier.class), eq(getConsumer()));
94         verify(getSupplier()).get();
95         verifyNoMoreInteractions(first, second, getSupplier());
96         verifyNoInteractions(getConsumer());
97     }
98 }