Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DTS-38481:Read exception should fail processing | <Release 10.1.0> #1180

Merged
merged 15 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ public String getDataFromServer(URL url, Optional<Connection> connectionOptional
request.setRequestProperty("Authorization", "Basic " + encodeCredentialsToBase64(username, password)); // NOSONAR
}
request.connect();
int responseCode = request.getResponseCode();
// process the client error
processClientError(connectionOptional, responseCode, request);
StringBuilder sb = new StringBuilder();
try (InputStream in = (InputStream) request.getContent();
BufferedReader inReader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))) {
Expand All @@ -174,6 +177,42 @@ public String getDataFromServer(URL url, Optional<Connection> connectionOptional
return sb.toString();
}

/**
* Method to process client error and update the connection broken flag
*
* @param connectionOptional
* connectionOptional
* @param responseCode
* responseCode
* @param request
* request
* @throws IOException
* throw IO Error
*/
private void processClientError(Optional<Connection> connectionOptional, int responseCode,
HttpURLConnection request) throws IOException {
if (responseCode >= 400 && responseCode < 500) {
// Read error message from the server
InputStream errorStream = request.getErrorStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(errorStream));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
request.disconnect();
// flagging the breaking connection
connectionOptional.ifPresent(connection -> {
String errMsg = ClientErrorMessageEnum.fromValue(responseCode).getReasonPhrase();
processorToolConnectionService.updateBreakingConnection(connection.getId(), errMsg);
});
// Throw an exception with the error message
log.error("Exception when reading from server {}", response);
throw new IOException(String.format("Error: %d - %s", responseCode, response));
}
}

/**
*
* @param connectionOptional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.util.Map;
import java.util.Optional;

import org.bson.types.ObjectId;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.joda.time.DateTime;
Expand Down Expand Up @@ -225,8 +226,10 @@ public void getVersionTest() throws IOException, ParseException {
HttpURLConnection mockedConnection = Mockito.mock(HttpURLConnection.class);
String responseData = "Sample response data";
InputStream inputStream = new ByteArrayInputStream(responseData.getBytes(StandardCharsets.UTF_8));
List<ProjectVersion> versions = jiraCommonService.getVersion(projectConfFieldMapping1, krb5Client);
Assert.assertEquals(0, versions.size());
assertThrows(IOException.class, () -> {
jiraCommonService.getVersion(projectConfFieldMapping1, krb5Client);
});
// Assert.assertEquals(0, versions.size());
}
// @Test(expected = RestClientException.class)
// public void getVersionTest1() throws IOException, ParseException {
Expand Down Expand Up @@ -495,4 +498,31 @@ public void testSaveSearchDetailsInContext() {
// Assert
verify(mockExecutionContext).putInt(JiraConstants.TOTAL_ISSUES, totalIssues);
verify(mockExecutionContext).putInt(JiraConstants.PAGE_START, pageStart);
}

@Test
public void testProcessClientError() throws IOException {
// Arrange
int responseCode = 404;
String errorMessage = "Not Found";

HttpURLConnection mockConnection = Mockito.mock(HttpURLConnection.class);
when(mockConnection.getResponseCode()).thenReturn(responseCode);
when(mockConnection.getErrorStream()).thenReturn(new ByteArrayInputStream(errorMessage.getBytes(StandardCharsets.UTF_8)));

URL mockUrl = Mockito.mock(URL.class);
when(mockUrl.openConnection()).thenReturn(mockConnection);

Connection mockConnectionObject = Mockito.mock(Connection.class);
when(mockConnectionObject.getId()).thenReturn(new ObjectId("668517f812811950be19353f"));
Optional<Connection> connectionOptional = Optional.of(mockConnectionObject);

// Act
Exception exception = assertThrows(IOException.class, () -> {
jiraCommonService.getDataFromServer(mockUrl, connectionOptional);
});

// Assert
String expectedMessage = "Error: 404 - Not Found";
assertEquals(expectedMessage, exception.getMessage());
}}
Loading