### Question:
SecurityErrorCheck implements ErrorCheck { @Override public ResponseEntity<ApiMessageView> checkError(HttpServletRequest request, Throwable exc) { if (exc instanceof ZuulException && (exc.getCause() instanceof AuthenticationException)) { ApiMessageView messageView = null; Throwable cause = exc.getCause(); if (cause instanceof TokenExpireException) { messageView = messageService.createMessage("org.zowe.apiml.gateway.security.expiredToken").mapToView(); } else if (cause instanceof TokenNotValidException) { messageView = messageService.createMessage("org.zowe.apiml.gateway.security.invalidToken").mapToView(); } else if (cause instanceof BadCredentialsException) { messageView = messageService.createMessage("org.zowe.apiml.security.login.invalidCredentials", ErrorUtils.getGatewayUri(request) ).mapToView(); } return ResponseEntity.status(HttpStatus.UNAUTHORIZED).contentType(MediaType.APPLICATION_JSON_UTF8).body(messageView); } return null; }  @Override ResponseEntity<ApiMessageView> checkError(HttpServletRequest request, Throwable exc);  }### Answer:
@Test void shouldReturnCauseMessageWhenTokenExpireException() { MockHttpServletRequest request = new MockHttpServletRequest(); TokenExpireException tokenExpireException = new TokenExpireException("TOKEN_EXPIRE"); AuthenticationException exception = new TokenExpireException(tokenExpireException.getMessage(), tokenExpireException); ZuulException exc = new ZuulException(exception, HttpStatus.GATEWAY_TIMEOUT.value(), String.valueOf(exception)); request.setAttribute(ErrorUtils.ATTR_ERROR_EXCEPTION, exc); ResponseEntity<ApiMessageView> actualResponse = securityErrorCheck.checkError(request, exc); assertNotNull(actualResponse); assertEquals(HttpStatus.UNAUTHORIZED, actualResponse.getStatusCode()); List<ApiMessage> actualMessageList = actualResponse.getBody().getMessages(); assertThat(actualMessageList, hasItem(new ApiMessage("org.zowe.apiml.gateway.security.expiredToken", MessageType.ERROR, "ZWEAG103E", "Token is expired"))); }
@Test void shouldReturnCauseMessageWhenTokenNotValidException() { MockHttpServletRequest request = new MockHttpServletRequest(); TokenNotValidException tokenNotValidException = new TokenNotValidException("TOKEN_NOT_VALID"); AuthenticationException exception = new TokenNotValidException(tokenNotValidException.getMessage(), tokenNotValidException); ZuulException exc = new ZuulException(exception, HttpStatus.GATEWAY_TIMEOUT.value(), String.valueOf(exception)); request.setAttribute(ErrorUtils.ATTR_ERROR_EXCEPTION, exc); ResponseEntity<ApiMessageView> actualResponse = securityErrorCheck.checkError(request, exc); assertNotNull(actualResponse); assertEquals(HttpStatus.UNAUTHORIZED, actualResponse.getStatusCode()); assertNotNull(actualResponse.getBody()); List<ApiMessage> actualMessageList = actualResponse.getBody().getMessages(); assertThat(actualMessageList, hasItem(new ApiMessage("org.zowe.apiml.gateway.security.invalidToken", MessageType.ERROR, "ZWEAG102E", "Token is not valid"))); }
@Test void shouldReturnCauseMessageWhenBadCredentialsException() { MockHttpServletRequest request = new MockHttpServletRequest(); BadCredentialsException badCredentialsException = new BadCredentialsException("CREDENTIALS_NOT_VALID"); AuthenticationException exception = new BadCredentialsException(badCredentialsException.getMessage(), badCredentialsException); ZuulException exc = new ZuulException(exception, HttpStatus.GATEWAY_TIMEOUT.value(), String.valueOf(exception)); request.setAttribute(ErrorUtils.ATTR_ERROR_EXCEPTION, exc); ResponseEntity<ApiMessageView> actualResponse = securityErrorCheck.checkError(request, exc); assertNotNull(actualResponse); assertEquals(HttpStatus.UNAUTHORIZED, actualResponse.getStatusCode()); assertNotNull(actualResponse.getBody()); List<ApiMessage> actualMessageList = actualResponse.getBody().getMessages(); assertThat(actualMessageList, hasItem(new ApiMessage("org.zowe.apiml.security.login.invalidCredentials", MessageType.ERROR, "ZWEAG120E", "Invalid username or password for URL 'null'"))); }