2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.aai.rest;
22 import com.fasterxml.jackson.core.JsonLocation;
23 import com.fasterxml.jackson.core.JsonParseException;
24 import com.fasterxml.jackson.databind.JsonMappingException;
25 import com.sun.istack.SAXParseException2;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.InjectMocks;
29 import org.mockito.Mock;
30 import org.mockito.MockitoAnnotations;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.ws.rs.WebApplicationException;
34 import javax.ws.rs.core.HttpHeaders;
35 import javax.ws.rs.core.MediaType;
36 import javax.ws.rs.core.MultivaluedHashMap;
37 import javax.ws.rs.core.Response;
38 import java.util.ArrayList;
39 import java.util.List;
40 import java.util.UUID;
42 import static org.junit.Assert.*;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.when;
46 public class ExceptionHandlerTest {
48 protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json");
51 private HttpHeaders httpHeaders;
54 private HttpServletRequest request;
57 private ExceptionHandler handler = new ExceptionHandler();
61 MockitoAnnotations.initMocks(this);
63 MultivaluedHashMap headersMultiMap = new MultivaluedHashMap<>();
65 headersMultiMap.add("X-FromAppId", "JUNIT");
66 headersMultiMap.add("X-TransactionId", UUID.randomUUID().toString());
67 headersMultiMap.add("Real-Time", "true");
68 headersMultiMap.add("Accept", "application/json");
69 headersMultiMap.add("aai-request-context", "");
71 List<MediaType> outputMediaTypes = new ArrayList<>();
72 outputMediaTypes.add(APPLICATION_JSON);
73 when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes);
74 when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap);
78 public void testConversionOfWebApplicationResponse() throws Exception {
80 Exception exception = new WebApplicationException();
81 Response response = handler.toResponse(exception);
83 assertNotNull(response);
84 assertNull(response.getEntity());
85 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
89 public void testConversionOfWebApplicationResponseWhenUmarshalExceptionResultBadRequest() throws Exception {
91 SAXParseException2 mockSaxParseException = mock(SAXParseException2.class);
92 Exception exception = new WebApplicationException(mockSaxParseException);
93 Response response = handler.toResponse(exception);
95 assertNotNull(response);
96 assertNotNull(response.getEntity());
97 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
101 public void testConversionWhenJsonParseExceptionResultBadRequest() throws Exception {
103 JsonLocation jsonLocation = mock(JsonLocation.class);
104 Exception exception = new JsonParseException("", jsonLocation);
105 Response response = handler.toResponse(exception);
107 assertNotNull(response);
108 assertNotNull(response.getEntity());
109 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
113 public void testConversionWhenJsonMappingExceptionResultBadRequest() throws Exception {
115 JsonLocation jsonLocation = mock(JsonLocation.class);
116 Exception exception = new JsonMappingException("", jsonLocation);
117 Response response = handler.toResponse(exception);
119 assertNotNull(response);
120 assertNotNull(response.getEntity());
121 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
125 public void testConversionWhenUnknownExceptionResultBadRequest() throws Exception {
127 Exception exception = mock(Exception.class);
128 Response response = handler.toResponse(exception);
130 when(request.getMethod()).thenReturn("GET");
132 assertNotNull(response);
133 assertNotNull(response.getEntity());
134 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
140 public void testConversionWhenUnknownExceptionResultBadRequestForXmlResponseType() throws Exception {
142 List<MediaType> outputMediaTypes = new ArrayList<>();
143 outputMediaTypes.add(MediaType.valueOf("application/xml"));
144 when(request.getMethod()).thenReturn("GET");
145 when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes);
147 Exception exception = mock(Exception.class);
148 Response response = handler.toResponse(exception);
150 assertNotNull(response);
151 assertNotNull(response.getEntity());
152 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());