|
1 | 1 | package dev.tomr.hcloud.service.action; |
2 | 2 |
|
| 3 | +import dev.tomr.hcloud.HetznerCloud; |
| 4 | +import dev.tomr.hcloud.http.HetznerCloudHttpClient; |
| 5 | +import dev.tomr.hcloud.http.RequestVerb; |
| 6 | +import dev.tomr.hcloud.http.model.Action; |
| 7 | +import dev.tomr.hcloud.http.model.ActionWrapper; |
| 8 | +import dev.tomr.hcloud.http.model.Error; |
| 9 | +import org.junit.jupiter.api.DisplayName; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.mockito.MockedStatic; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | +import java.util.Date; |
| 15 | +import java.util.List; |
| 16 | +import java.util.concurrent.CompletableFuture; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.*; |
| 19 | +import static org.mockito.Mockito.*; |
| 20 | + |
3 | 21 | public class ActionServiceTest { |
| 22 | + |
| 23 | + @Test |
| 24 | + @DisplayName("Wait for action to complete returns a CompletableFuture with the finished action when the progress is 100 and finished has a date string") |
| 25 | + void waitForActionToCompleteReturnsWhenProgressIs100() throws IOException, InterruptedException, IllegalAccessException { |
| 26 | + HetznerCloud hetznerCloud = mock(HetznerCloud.class); |
| 27 | + HetznerCloudHttpClient hetznerCloudHttpClient = mock(HetznerCloudHttpClient.class); |
| 28 | + |
| 29 | + try (MockedStatic<HetznerCloud> hetznerCloudMockedStatic = mockStatic(HetznerCloud.class); |
| 30 | + MockedStatic<HetznerCloudHttpClient> hetznerCloudHttpClientMockedStatic = mockStatic(HetznerCloudHttpClient.class)) { |
| 31 | + |
| 32 | + hetznerCloudHttpClientMockedStatic.when(HetznerCloudHttpClient::getInstance).thenReturn(hetznerCloudHttpClient); |
| 33 | + hetznerCloudMockedStatic.when(HetznerCloud::getInstance).thenReturn(hetznerCloud); |
| 34 | + |
| 35 | + when(hetznerCloud.getHttpDetails()).thenReturn(List.of("http://host/", "key1234")); |
| 36 | + |
| 37 | + Action returnedAction = new Action(); |
| 38 | + returnedAction.setProgress(100); |
| 39 | + returnedAction.setFinished(new Date().toString()); |
| 40 | + returnedAction.setId(1); |
| 41 | + returnedAction.setCommand("delete_resource"); |
| 42 | + |
| 43 | + Action originalAction = new Action(); |
| 44 | + originalAction.setId(1); |
| 45 | + originalAction.setCommand("delete_resource"); |
| 46 | + |
| 47 | + when(hetznerCloudHttpClient.sendHttpRequest(any(), anyString(), any(RequestVerb.class), anyString())).thenReturn(new ActionWrapper(returnedAction)); |
| 48 | + |
| 49 | + ActionService actionService = new ActionService(); |
| 50 | + |
| 51 | + CompletableFuture<Action> actionCompletableFuture = actionService.waitForActionToComplete(originalAction); |
| 52 | + |
| 53 | + assertEquals(1, actionCompletableFuture.join().getId()); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + @DisplayName("Wait for action to complete returns a CompletableFuture with the finished action after trying again when the action is not finished the first time") |
| 59 | + void waitForActionToCompleteReturnsACompletableFutureWithTheFinishedActionWithRetries() throws IOException, InterruptedException, IllegalAccessException { |
| 60 | + HetznerCloud hetznerCloud = mock(HetznerCloud.class); |
| 61 | + HetznerCloudHttpClient hetznerCloudHttpClient = mock(HetznerCloudHttpClient.class); |
| 62 | + |
| 63 | + try (MockedStatic<HetznerCloud> hetznerCloudMockedStatic = mockStatic(HetznerCloud.class); |
| 64 | + MockedStatic<HetznerCloudHttpClient> hetznerCloudHttpClientMockedStatic = mockStatic(HetznerCloudHttpClient.class)) { |
| 65 | + |
| 66 | + hetznerCloudHttpClientMockedStatic.when(HetznerCloudHttpClient::getInstance).thenReturn(hetznerCloudHttpClient); |
| 67 | + hetznerCloudMockedStatic.when(HetznerCloud::getInstance).thenReturn(hetznerCloud); |
| 68 | + |
| 69 | + when(hetznerCloud.getHttpDetails()).thenReturn(List.of("http://host/", "key1234")); |
| 70 | + |
| 71 | + Action returnedAction = new Action(); |
| 72 | + returnedAction.setProgress(100); |
| 73 | + returnedAction.setFinished(new Date().toString()); |
| 74 | + returnedAction.setId(1); |
| 75 | + returnedAction.setCommand("delete_resource"); |
| 76 | + |
| 77 | + Action unfinishedAction = new Action(); |
| 78 | + unfinishedAction.setProgress(50); |
| 79 | + unfinishedAction.setId(1); |
| 80 | + unfinishedAction.setCommand("delete_resource"); |
| 81 | + |
| 82 | + Action originalAction = new Action(); |
| 83 | + originalAction.setId(1); |
| 84 | + originalAction.setCommand("delete_resource"); |
| 85 | + |
| 86 | + when(hetznerCloudHttpClient.sendHttpRequest(any(), anyString(), any(RequestVerb.class), anyString())).thenReturn( |
| 87 | + new ActionWrapper(unfinishedAction), |
| 88 | + new ActionWrapper(unfinishedAction), |
| 89 | + new ActionWrapper(returnedAction)); |
| 90 | + |
| 91 | + ActionService actionService = new ActionService(); |
| 92 | + |
| 93 | + CompletableFuture<Action> actionCompletableFuture = actionService.waitForActionToComplete(originalAction); |
| 94 | + |
| 95 | + assertEquals(1, actionCompletableFuture.join().getId()); |
| 96 | + verify(hetznerCloudHttpClient, times(3)).sendHttpRequest(any(), anyString(), any(RequestVerb.class), anyString()); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + @DisplayName("Wait for action to complete returns a CompletableFuture with the finished action after the first http request is 100 on progress, but not 'finished', the second is") |
| 102 | + void waitForActionToCompleteReturnsACompletableFutureWithTheFinishedActionWithABadReturnFirstTryGoodSecond() throws IOException, InterruptedException, IllegalAccessException { |
| 103 | + HetznerCloud hetznerCloud = mock(HetznerCloud.class); |
| 104 | + HetznerCloudHttpClient hetznerCloudHttpClient = mock(HetznerCloudHttpClient.class); |
| 105 | + |
| 106 | + try (MockedStatic<HetznerCloud> hetznerCloudMockedStatic = mockStatic(HetznerCloud.class); |
| 107 | + MockedStatic<HetznerCloudHttpClient> hetznerCloudHttpClientMockedStatic = mockStatic(HetznerCloudHttpClient.class)) { |
| 108 | + |
| 109 | + hetznerCloudHttpClientMockedStatic.when(HetznerCloudHttpClient::getInstance).thenReturn(hetznerCloudHttpClient); |
| 110 | + hetznerCloudMockedStatic.when(HetznerCloud::getInstance).thenReturn(hetznerCloud); |
| 111 | + |
| 112 | + when(hetznerCloud.getHttpDetails()).thenReturn(List.of("http://host/", "key1234")); |
| 113 | + |
| 114 | + Action returnedAction = new Action(); |
| 115 | + returnedAction.setProgress(100); |
| 116 | + returnedAction.setFinished(new Date().toString()); |
| 117 | + returnedAction.setId(1); |
| 118 | + returnedAction.setCommand("delete_resource"); |
| 119 | + |
| 120 | + Action unfinishedAction = new Action(); |
| 121 | + unfinishedAction.setProgress(100); |
| 122 | + unfinishedAction.setId(1); |
| 123 | + unfinishedAction.setCommand("delete_resource"); |
| 124 | + |
| 125 | + Action originalAction = new Action(); |
| 126 | + originalAction.setId(1); |
| 127 | + originalAction.setCommand("delete_resource"); |
| 128 | + |
| 129 | + when(hetznerCloudHttpClient.sendHttpRequest(any(), anyString(), any(RequestVerb.class), anyString())).thenReturn( |
| 130 | + new ActionWrapper(unfinishedAction), |
| 131 | + new ActionWrapper(returnedAction)); |
| 132 | + |
| 133 | + ActionService actionService = new ActionService(); |
| 134 | + |
| 135 | + CompletableFuture<Action> actionCompletableFuture = actionService.waitForActionToComplete(originalAction); |
| 136 | + |
| 137 | + assertEquals(1, actionCompletableFuture.join().getId()); |
| 138 | + verify(hetznerCloudHttpClient, times(2)).sendHttpRequest(any(), anyString(), any(RequestVerb.class), anyString()); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + @DisplayName("Wait for action to complete throws a Runtime exception when the Hetzner Action has the error field present") |
| 144 | + void waitForActionToCompleteThrowsRuntimeWhenHetznerReturnsAnError() throws IOException, InterruptedException, IllegalAccessException { |
| 145 | + HetznerCloud hetznerCloud = mock(HetznerCloud.class); |
| 146 | + HetznerCloudHttpClient hetznerCloudHttpClient = mock(HetznerCloudHttpClient.class); |
| 147 | + |
| 148 | + try (MockedStatic<HetznerCloud> hetznerCloudMockedStatic = mockStatic(HetznerCloud.class); |
| 149 | + MockedStatic<HetznerCloudHttpClient> hetznerCloudHttpClientMockedStatic = mockStatic(HetznerCloudHttpClient.class)) { |
| 150 | + |
| 151 | + hetznerCloudHttpClientMockedStatic.when(HetznerCloudHttpClient::getInstance).thenReturn(hetznerCloudHttpClient); |
| 152 | + hetznerCloudMockedStatic.when(HetznerCloud::getInstance).thenReturn(hetznerCloud); |
| 153 | + |
| 154 | + when(hetznerCloud.getHttpDetails()).thenReturn(List.of("http://host/", "key1234")); |
| 155 | + |
| 156 | + |
| 157 | + Action errorAction = new Action(); |
| 158 | + errorAction.setId(1); |
| 159 | + errorAction.setCommand("delete_resource"); |
| 160 | + errorAction.setError(new Error("HETZNER_01", "This is the error from Hetzner")); |
| 161 | + |
| 162 | + Action originalAction = new Action(); |
| 163 | + originalAction.setId(1); |
| 164 | + originalAction.setCommand("delete_resource"); |
| 165 | + |
| 166 | + when(hetznerCloudHttpClient.sendHttpRequest(any(), anyString(), any(RequestVerb.class), anyString())).thenReturn(new ActionWrapper(errorAction)); |
| 167 | + |
| 168 | + ActionService actionService = new ActionService(); |
| 169 | + |
| 170 | + RuntimeException runtimeException = assertThrows(RuntimeException.class, () -> actionService.waitForActionToComplete(originalAction).join()); |
| 171 | + |
| 172 | + assertTrue(runtimeException.getMessage().contains("HETZNER_01")); |
| 173 | + assertTrue(runtimeException.getMessage().contains("This is the error from Hetzner")); |
| 174 | + } |
| 175 | + } |
4 | 176 | } |
0 commit comments