Skip to content

Commit f77384c

Browse files
committed
DATAGO-108479: Forgot about adding coverage for "Name does not resolve" scenario.
1 parent 92da9a7 commit f77384c

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

service/application/src/main/java/com/solace/maas/ep/event/management/agent/command/SempGetCommandManager.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ public void execute(Command command, SempApiProvider sempApiProvider) {
6666
if (SempEntityType.solaceClientProfileName.name().equals(entityType) &&
6767
isResourceNotFoundError(e)) {
6868
handleClientProfileNotFound(command);
69-
} else if (isConnectTimeoutError(e)) {
69+
} else if (isConnectTimeoutError(e) || isNameResolutionError(e)) {
7070
log.warn(SEMP_COMMAND_NOT_EXECUTED_SUCCESSFULLY, supportedSempCommand(), e);
7171
setCommandError(command, e);
7272
} else {
7373
log.error(SEMP_COMMAND_NOT_EXECUTED_SUCCESSFULLY, supportedSempCommand(), e);
7474
setCommandError(command, e);
7575
}
7676
} catch (Exception e) {
77-
if (isConnectTimeoutError(e)) {
77+
if (isConnectTimeoutError(e) || isNameResolutionError(e)) {
7878
log.warn(SEMP_COMMAND_NOT_EXECUTED_SUCCESSFULLY, supportedSempCommand(), e);
7979
setCommandError(command, e);
8080
} else {
@@ -115,6 +115,32 @@ private boolean isConnectTimeoutError(Exception e) {
115115
return false;
116116
}
117117

118+
/**
119+
* Checks if the exception represents a name resolution error.
120+
* This method examines the exception message and cause chain for DNS resolution failures.
121+
*/
122+
private boolean isNameResolutionError(Exception e) {
123+
// Check the exception message for name resolution indicators
124+
String message = e.getMessage();
125+
if (message != null && message.contains("Name does not resolve")) {
126+
return true;
127+
}
128+
129+
// Check the cause chain for UnknownHostException with name resolution errors
130+
Throwable cause = e.getCause();
131+
while (cause != null) {
132+
if (cause instanceof java.net.UnknownHostException) {
133+
String causeMessage = cause.getMessage();
134+
if (causeMessage != null && causeMessage.contains("Name does not resolve")) {
135+
return true;
136+
}
137+
}
138+
cause = cause.getCause();
139+
}
140+
141+
return false;
142+
}
143+
118144
private void handleClientProfileNotFound(Command command) {
119145
String resourceName = extractClientProfileName(command);
120146
String errorMessage = String.format("Named client profile not found on the event broker: %s", resourceName);

service/application/src/test/java/com/solace/maas/ep/event/management/agent/commandManager/SempGetCommandManagerTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.io.IOException;
2323
import java.net.SocketTimeoutException;
24+
import java.net.UnknownHostException;
2425
import java.nio.charset.StandardCharsets;
2526
import java.nio.file.Files;
2627
import java.nio.file.Path;
@@ -289,6 +290,50 @@ void withGeneralExceptionConnectTimeout() throws ApiException {
289290
executeTestWithException(generalException);
290291
}
291292

293+
@Test
294+
void withNameResolutionApiException() throws ApiException {
295+
// Create an ApiException with UnknownHostException as cause containing "Name does not resolve"
296+
UnknownHostException nameResolutionException = new UnknownHostException("msg-solace-test.lcag-cmlz-n.lhgroup.de: Name does not resolve");
297+
ApiException apiException = new ApiException(nameResolutionException);
298+
299+
executeTestWithException(apiException);
300+
}
301+
302+
@Test
303+
void withNameResolutionExceptionMessage() throws ApiException {
304+
// Create an ApiException with "Name does not resolve" in the message
305+
ApiException apiException = new ApiException("java.net.UnknownHostException: msg-solace-test.lcag-cmlz-n.lhgroup.de: Name does not resolve");
306+
307+
executeTestWithException(apiException);
308+
}
309+
310+
@Test
311+
void withNestedNameResolutionException() throws ApiException {
312+
// Create nested exception with UnknownHostException deep in the cause chain
313+
UnknownHostException nameResolutionException = new UnknownHostException("host.example.com: Name does not resolve");
314+
RuntimeException wrapperException = new RuntimeException("Wrapper exception", nameResolutionException);
315+
ApiException apiException = new ApiException(wrapperException);
316+
317+
executeTestWithException(apiException);
318+
}
319+
320+
@Test
321+
void withNonNameResolutionUnknownHostException() throws ApiException {
322+
// Create UnknownHostException with different message (not name resolution)
323+
UnknownHostException unknownHostException = new UnknownHostException("No route to host");
324+
ApiException apiException = new ApiException(unknownHostException);
325+
326+
executeTestWithException(apiException);
327+
}
328+
329+
@Test
330+
void withGeneralExceptionNameResolution() throws ApiException {
331+
// Create a general Exception (not ApiException) with name resolution error
332+
RuntimeException generalException = new RuntimeException("Connection failed: Name does not resolve");
333+
334+
executeTestWithException(generalException);
335+
}
336+
292337
private void executeTestWithException(Exception exception) throws ApiException {
293338
ClientProfileApi clientProfileApi = Mockito.mock(ClientProfileApi.class);
294339
when(sempApiProvider.getClientProfileApi()).thenReturn(clientProfileApi);

0 commit comments

Comments
 (0)