Skip to content

Commit 3a0eea7

Browse files
committed
fix(wait): linter issues
Signed-off-by: Alexander Dahmen <[email protected]>
1 parent 5d4d5ff commit 3a0eea7

File tree

5 files changed

+26
-101
lines changed

5 files changed

+26
-101
lines changed

core/src/main/java/cloud/stackit/sdk/core/oapierror/GenericOpenAPIError.java renamed to core/src/main/java/cloud/stackit/sdk/core/oapierror/GenericOpenAPIException.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@
44
import java.nio.charset.StandardCharsets;
55
import java.util.HashMap;
66

7-
public class GenericOpenAPIError extends ApiException {
7+
public class GenericOpenAPIException extends ApiException {
88

99
// When a response has a bad status, this limits the number of characters that are shown from
1010
// the response Body
1111
public static int ApiErrorMaxCharacterLimit = 500;
1212

13-
private int statusCode;
13+
private final int statusCode;
1414
private byte[] body;
15-
private String errorMessage;
15+
private final String errorMessage;
1616
private Object model;
1717

18-
public GenericOpenAPIError(ApiException e) {
18+
public GenericOpenAPIException(ApiException e) {
1919
this.statusCode = e.getCode();
2020
this.errorMessage = e.getMessage();
2121
}
2222

23-
public GenericOpenAPIError(int statusCode, String errorMessage) {
23+
public GenericOpenAPIException(int statusCode, String errorMessage) {
2424
this(statusCode, errorMessage, null, new HashMap<>());
2525
}
2626

27-
public GenericOpenAPIError(int statusCode, String errorMessage, byte[] body, Object model) {
27+
public GenericOpenAPIException(int statusCode, String errorMessage, byte[] body, Object model) {
2828
this.statusCode = statusCode;
2929
this.errorMessage = errorMessage;
3030
this.body = body;

core/src/main/java/cloud/stackit/sdk/core/wait/AsyncActionHandler.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package cloud.stackit.sdk.core.wait;
22

33
import cloud.stackit.sdk.core.exception.ApiException;
4-
import cloud.stackit.sdk.core.oapierror.GenericOpenAPIError;
4+
import cloud.stackit.sdk.core.oapierror.GenericOpenAPIException;
55
import java.net.HttpURLConnection;
66
import java.util.Arrays;
77
import java.util.HashSet;
88
import java.util.Set;
99
import java.util.concurrent.Callable;
1010
import java.util.concurrent.TimeUnit;
11+
import java.util.concurrent.TimeoutException;
1112

1213
public class AsyncActionHandler<T> {
1314
public static final Set<Integer> RetryHttpErrorStatusCodes =
@@ -111,7 +112,7 @@ public T waitWithContext() throws Exception {
111112
while (System.currentTimeMillis() - startTime < timeoutMillis) {
112113
AsyncActionResult<T> result = checkFn.call();
113114
if (result.error != null) { // error present
114-
ErrorResult errorResult = handleError(retryTempErrorCounter, result.error);
115+
ErrorResult errorResult = handleException(retryTempErrorCounter, result.error);
115116
retryTempErrorCounter = errorResult.retryTempErrorCounter;
116117
if (retryTempErrorCounter == tempErrRetryLimit) {
117118
throw errorResult.getError();
@@ -130,13 +131,13 @@ public T waitWithContext() throws Exception {
130131
throw new InterruptedException("Wait operation was interrupted.");
131132
}
132133
}
133-
throw new Exception(TimoutErrorMessage);
134+
throw new TimeoutException(TimoutErrorMessage);
134135
}
135136

136-
private ErrorResult handleError(int retryTempErrorCounter, Exception err) {
137-
if (err instanceof ApiException) {
138-
ApiException apiException = (ApiException) err;
139-
GenericOpenAPIError oapiErr = new GenericOpenAPIError(apiException);
137+
private ErrorResult handleException(int retryTempErrorCounter, Exception exception) {
138+
if (exception instanceof ApiException) {
139+
ApiException apiException = (ApiException) exception;
140+
GenericOpenAPIException oapiErr = new GenericOpenAPIException(apiException);
140141
// Some APIs may return temporary errors and the request should be retried
141142
if (!RetryHttpErrorStatusCodes.contains(oapiErr.getStatusCode())) {
142143
return new ErrorResult(retryTempErrorCounter, oapiErr);
@@ -151,7 +152,7 @@ private ErrorResult handleError(int retryTempErrorCounter, Exception err) {
151152
retryTempErrorCounter++;
152153
// If it's not a GenericOpenAPIError, handle it differently
153154
return new ErrorResult(
154-
retryTempErrorCounter, new Exception(NonGenericAPIErrorMessage, err));
155+
retryTempErrorCounter, new Exception(NonGenericAPIErrorMessage, exception));
155156
}
156157
}
157158

core/src/test/java/cloud/stackit/sdk/core/wait/AsyncWaitHandlerTest.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
public class AsyncWaitHandlerTest {
1818

1919
// Helper class for testing
20-
public class ApiHelper {
20+
public static class ApiHelper {
2121

2222
private final String response = "APIResponse";
2323

@@ -61,7 +61,7 @@ void testNonGenericOpenAPIError() throws Exception {
6161
handler.setTimeout(40, TimeUnit.MILLISECONDS);
6262
handler.setTempErrRetryLimit(2);
6363

64-
Exception thrown = assertThrows(Exception.class, () -> handler.waitWithContext(), "");
64+
Exception thrown = assertThrows(Exception.class, handler::waitWithContext, "");
6565
assertEquals(thrown.getMessage(), handler.NonGenericAPIErrorMessage);
6666
}
6767

@@ -77,7 +77,7 @@ void testOpenAPIErrorNotInList() throws Exception {
7777
handler.setTimeout(40, TimeUnit.MILLISECONDS);
7878
handler.setTempErrRetryLimit(2);
7979

80-
Exception thrown = assertThrows(Exception.class, () -> handler.waitWithContext(), "");
80+
Exception thrown = assertThrows(Exception.class, handler::waitWithContext, "");
8181
assertEquals(thrown.getMessage(), handler.TimoutErrorMessage);
8282
}
8383

@@ -95,10 +95,7 @@ void testOpenAPIErrorTimeoutBadGateway() throws Exception {
9595
handler.setTempErrRetryLimit(2);
9696

9797
Exception thrown =
98-
assertThrows(
99-
Exception.class,
100-
() -> handler.waitWithContext(),
101-
apiException.getMessage());
98+
assertThrows(Exception.class, handler::waitWithContext, apiException.getMessage());
10299
assertEquals(thrown.getMessage(), handler.TemporaryErrorMessage);
103100
}
104101

@@ -116,10 +113,7 @@ void testOpenAPIErrorTimeoutGatewayTimeout() throws Exception {
116113
handler.setTempErrRetryLimit(2);
117114

118115
Exception thrown =
119-
assertThrows(
120-
Exception.class,
121-
() -> handler.waitWithContext(),
122-
apiException.getMessage());
116+
assertThrows(Exception.class, handler::waitWithContext, apiException.getMessage());
123117
assertEquals(thrown.getMessage(), handler.TemporaryErrorMessage);
124118
}
125119
}

services/resourcemanager/src/main/java/cloud/stackit/sdk/resourcemanager/wait/ResourcemanagerWait.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package cloud.stackit.sdk.resourcemanager.wait;
22

33
import cloud.stackit.sdk.core.exception.ApiException;
4-
import cloud.stackit.sdk.core.oapierror.GenericOpenAPIError;
4+
import cloud.stackit.sdk.core.oapierror.GenericOpenAPIException;
55
import cloud.stackit.sdk.core.wait.AsyncActionHandler;
66
import cloud.stackit.sdk.core.wait.AsyncActionHandler.AsyncActionResult;
77
import cloud.stackit.sdk.resourcemanager.api.ResourceManagerApi;
@@ -13,7 +13,7 @@
1313

1414
public class ResourcemanagerWait {
1515
/**
16-
* createProjectWaitHandler will wait for project creation. Uses the deault values for
16+
* createProjectWaitHandler will wait for project creation. Uses the default values for
1717
* sleepBeforeWait (1 min) and timeout (45 min).
1818
*
1919
* @param apiClient
@@ -40,16 +40,6 @@ public static AsyncActionHandler<GetProjectResponse> createProjectWaitHandler(
4040
() -> {
4141
try {
4242
GetProjectResponse p = apiClient.getProject(containerId, false);
43-
if (p.getContainerId() == null || p.getLifecycleState() == null) {
44-
return new AsyncActionResult<>(
45-
false,
46-
null,
47-
new Exception(
48-
"Creation failed: response invalid for container id "
49-
+ containerId
50-
+ ". Container ID or LifecycleState missing."));
51-
}
52-
5343
if (p.getContainerId().equals(containerId)
5444
&& p.getLifecycleState().equals(LifecycleState.ACTIVE)) {
5545
return new AsyncActionResult<>(true, p, null);
@@ -103,9 +93,6 @@ public static AsyncActionHandler<Void> deleteProjectWaitHandler(
10393
() -> {
10494
try {
10595
GetProjectResponse p = apiClient.getProject(containerId, false);
106-
if (p.getContainerId() == null || p.getLifecycleState() == null) {
107-
return new AsyncActionResult<>(true, null, null);
108-
}
10996

11097
if (p.getContainerId().equals(containerId)
11198
&& p.getLifecycleState().equals(LifecycleState.DELETING)) {
@@ -117,7 +104,7 @@ public static AsyncActionHandler<Void> deleteProjectWaitHandler(
117104
return new AsyncActionResult<>(false, null, null);
118105

119106
} catch (ApiException e) {
120-
GenericOpenAPIError oapiErr = new GenericOpenAPIError(e);
107+
GenericOpenAPIException oapiErr = new GenericOpenAPIException(e);
121108
if (oapiErr.getStatusCode() == HttpURLConnection.HTTP_NOT_FOUND
122109
|| oapiErr.getStatusCode() == HttpURLConnection.HTTP_FORBIDDEN) {
123110
// Resource is gone, so deletion is complete

services/resourcemanager/src/test/java/cloud/stackit/sdk/resourcemanager/ResourcemanagerWaitTestmanagerWaitTest.java

Lines changed: 3 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -79,27 +79,7 @@ void testCreateProjectTimeout() throws Exception {
7979
handler.setThrottle(10, TimeUnit.MILLISECONDS);
8080
handler.setTimeout(500, TimeUnit.MILLISECONDS);
8181

82-
assertThrows(Exception.class, () -> handler.waitWithContext(), handler.TimoutErrorMessage);
83-
}
84-
85-
// Non GenericOpenAPIError error
86-
@Test
87-
void testCreateProjectNonOpenAPIError() throws Exception {
88-
// Return containerId == null and LifecycleState == CREATING
89-
GetProjectResponse creatingResponse = new GetProjectResponse();
90-
creatingResponse.setContainerId(null);
91-
creatingResponse.setLifecycleState(null);
92-
when(apiClient.getProject(containerId, false)).thenReturn(creatingResponse);
93-
94-
AsyncActionHandler<GetProjectResponse> handler =
95-
ResourcemanagerWait.createProjectWaitHandler(apiClient, containerId);
96-
handler.setSleepBeforeWait(0, TimeUnit.SECONDS);
97-
handler.setThrottle(10, TimeUnit.MILLISECONDS);
98-
handler.setTimeout(100, TimeUnit.MILLISECONDS);
99-
handler.setTempErrRetryLimit(2);
100-
101-
Exception thrown = assertThrows(Exception.class, () -> handler.waitWithContext(), "");
102-
assertEquals(thrown.getMessage(), handler.NonGenericAPIErrorMessage);
82+
assertThrows(Exception.class, handler::waitWithContext, handler.TimoutErrorMessage);
10383
}
10484

10585
// GenericOpenAPIError not in RetryHttpErrorStatusCodes
@@ -117,10 +97,7 @@ void testCreateProjectOpenAPIError() throws Exception {
11797
handler.setTempErrRetryLimit(2);
11898

11999
Exception thrown =
120-
assertThrows(
121-
Exception.class,
122-
() -> handler.waitWithContext(),
123-
apiException.getMessage());
100+
assertThrows(Exception.class, handler::waitWithContext, apiException.getMessage());
124101
assertEquals(thrown.getMessage(), handler.TimoutErrorMessage);
125102
}
126103

@@ -139,10 +116,7 @@ void testOpenAPIErrorTimeoutBadGateway() throws Exception {
139116
handler.setTempErrRetryLimit(2);
140117

141118
Exception thrown =
142-
assertThrows(
143-
Exception.class,
144-
() -> handler.waitWithContext(),
145-
apiException.getMessage());
119+
assertThrows(Exception.class, handler::waitWithContext, apiException.getMessage());
146120
assertEquals(thrown.getMessage(), handler.TemporaryErrorMessage);
147121
}
148122

@@ -199,37 +173,6 @@ void testDeleteProjectSuccessDeleting() throws Exception {
199173
verify(apiClient, times(2)).getProject(containerId, false);
200174
}
201175

202-
@Test
203-
void testDeleteProjectSuccessContainerIdNull() throws Exception {
204-
// First call returns "ACTIVE", second call returns containerId = null
205-
GetProjectResponse activeResponse = new GetProjectResponse();
206-
activeResponse.setContainerId(containerId);
207-
activeResponse.setLifecycleState(LifecycleState.ACTIVE);
208-
209-
GetProjectResponse deletingResponse = new GetProjectResponse();
210-
deletingResponse.setContainerId(null);
211-
deletingResponse.setLifecycleState(LifecycleState.DELETING);
212-
213-
AtomicInteger callCount = new AtomicInteger(0);
214-
when(apiClient.getProject(containerId, false))
215-
.thenAnswer(
216-
invocation -> {
217-
if (callCount.getAndIncrement() < 1) {
218-
return activeResponse;
219-
}
220-
return deletingResponse;
221-
});
222-
223-
AsyncActionHandler<Void> handler =
224-
ResourcemanagerWait.deleteProjectWaitHandler(apiClient, containerId);
225-
handler.setSleepBeforeWait(0, TimeUnit.SECONDS);
226-
handler.setThrottle(10, TimeUnit.MILLISECONDS);
227-
handler.setTimeout(2, TimeUnit.SECONDS);
228-
229-
handler.waitWithContext();
230-
verify(apiClient, times(2)).getProject(containerId, false);
231-
}
232-
233176
@Test
234177
void testDeleteProjectSuccessNotFoundExc() throws Exception {
235178
// Trigger API Exception

0 commit comments

Comments
 (0)