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

Exception handler test coverage added #128

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.HashMap;
import java.util.Map;

import org.apache.catalina.connector.ClientAbortException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
Expand All @@ -36,6 +35,7 @@
import jakarta.servlet.http.HttpServletRequest;

@RestControllerAdvice
@Order(ValidationExceptionMapper.ORDER + 1)
public class ApplicationExceptionMapper {

private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationExceptionMapper.class);
Expand All @@ -53,16 +53,7 @@ public class ApplicationExceptionMapper {
EXCEPTION_STATUS_MAP.put(NoResourceFoundException.class, HttpStatus.NOT_FOUND);
}

@ExceptionHandler(ClientAbortException.class)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted my prev change - I believe we should rather ignore it from alerts than changing the error logs here or something.
Added test coverage for the existing exception handler.

@Order(ValidationExceptionMapper.ORDER + 1)
public void handleClientAborted(
HttpServletRequest request, ClientAbortException clientAbortException) {
logException(
request, clientAbortException); // socket closed, cannot return any error response
}

@ExceptionHandler(Throwable.class)
@Order(ValidationExceptionMapper.ORDER + 2)
public ResponseEntity<ErrorResponse> handleAll(HttpServletRequest request, Throwable th) {
logException(request, th);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright 2024 Conductor Authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.netflix.conductor.rest.controllers;

import java.util.Collections;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import com.netflix.conductor.model.TaskModel;

import com.fasterxml.jackson.databind.ObjectMapper;

import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

public class ApplicationExceptionMapperTest {

private QueueAdminResource queueAdminResource;

private MockMvc mockMvc;

private static MockedStatic<LoggerFactory> mockLoggerFactory;
private static final Logger logger = mock(Logger.class);

@Before
public void before() {
mockLoggerFactory = Mockito.mockStatic(LoggerFactory.class);
when(LoggerFactory.getLogger(ApplicationExceptionMapper.class)).thenReturn(logger);

this.queueAdminResource = mock(QueueAdminResource.class);
this.mockMvc =
MockMvcBuilders.standaloneSetup(this.queueAdminResource)
.setControllerAdvice(new ApplicationExceptionMapper())
.build();
}

@After
public void after() {
mockLoggerFactory.close();
}

@Test
public void testException() throws Exception {
var exception = new Exception();
// pick a method that raises a generic exception
doThrow(exception).when(this.queueAdminResource).update(any(), any(), any(), any());

// verify we do send an error response
this.mockMvc
.perform(
MockMvcRequestBuilders.post(
"/api/queue/update/workflowId/taskRefName/{status}",
TaskModel.Status.SKIPPED)
.contentType(MediaType.APPLICATION_JSON)
.content(
new ObjectMapper()
.writeValueAsString(Collections.emptyMap())))
.andDo(print())
.andExpect(status().is5xxServerError());
// verify the error was logged
verify(logger)
.error(
"Error {} url: '{}'",
"Exception",
"/api/queue/update/workflowId/taskRefName/SKIPPED",
exception);
verifyNoMoreInteractions(logger);
}
}