2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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=========================================================
21 package org.openecomp.aai.rest;
23 import com.fasterxml.jackson.core.JsonLocation;
24 import com.fasterxml.jackson.core.JsonParseException;
25 import com.fasterxml.jackson.databind.JsonMappingException;
26 import com.sun.istack.SAXParseException2;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.InjectMocks;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.ws.rs.WebApplicationException;
35 import javax.ws.rs.core.HttpHeaders;
36 import javax.ws.rs.core.MediaType;
37 import javax.ws.rs.core.MultivaluedHashMap;
38 import javax.ws.rs.core.Response;
39 import java.util.ArrayList;
40 import java.util.List;
41 import java.util.UUID;
43 import static org.junit.Assert.*;
44 import static org.mockito.Mockito.mock;
45 import static org.mockito.Mockito.when;
47 public class ExceptionHandlerTest {
49 protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json");
52 private HttpHeaders httpHeaders;
55 private HttpServletRequest request;
58 private ExceptionHandler handler = new ExceptionHandler();
62 MockitoAnnotations.initMocks(this);
64 MultivaluedHashMap headersMultiMap = new MultivaluedHashMap<>();
66 headersMultiMap.add("X-FromAppId", "JUNIT");
67 headersMultiMap.add("X-TransactionId", UUID.randomUUID().toString());
68 headersMultiMap.add("Real-Time", "true");
69 headersMultiMap.add("Accept", "application/json");
70 headersMultiMap.add("aai-request-context", "");
72 List<MediaType> outputMediaTypes = new ArrayList<>();
73 outputMediaTypes.add(APPLICATION_JSON);
74 when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes);
75 when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap);
79 public void testConversionOfWebApplicationResponse() throws Exception {
81 Exception exception = new WebApplicationException();
82 Response response = handler.toResponse(exception);
84 assertNotNull(response);
85 assertNull(response.getEntity());
86 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
90 public void testConversionOfWebApplicationResponseWhenUmarshalExceptionResultBadRequest() throws Exception {
92 SAXParseException2 mockSaxParseException = mock(SAXParseException2.class);
93 Exception exception = new WebApplicationException(mockSaxParseException);
94 Response response = handler.toResponse(exception);
96 assertNotNull(response);
97 assertNotNull(response.getEntity());
98 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
102 public void testConversionWhenJsonParseExceptionResultBadRequest() throws Exception {
104 JsonLocation jsonLocation = mock(JsonLocation.class);
105 Exception exception = new JsonParseException("", jsonLocation);
106 Response response = handler.toResponse(exception);
108 assertNotNull(response);
109 assertNotNull(response.getEntity());
110 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
114 public void testConversionWhenJsonMappingExceptionResultBadRequest() throws Exception {
116 JsonLocation jsonLocation = mock(JsonLocation.class);
117 Exception exception = new JsonMappingException("", jsonLocation);
118 Response response = handler.toResponse(exception);
120 assertNotNull(response);
121 assertNotNull(response.getEntity());
122 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
126 public void testConversionWhenUnknownExceptionResultBadRequest() throws Exception {
128 Exception exception = mock(Exception.class);
129 Response response = handler.toResponse(exception);
131 when(request.getMethod()).thenReturn("GET");
133 assertNotNull(response);
134 assertNotNull(response.getEntity());
135 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
141 public void testConversionWhenUnknownExceptionResultBadRequestForXmlResponseType() throws Exception {
143 List<MediaType> outputMediaTypes = new ArrayList<>();
144 outputMediaTypes.add(MediaType.valueOf("application/xml"));
145 when(request.getMethod()).thenReturn("GET");
146 when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes);
148 Exception exception = mock(Exception.class);
149 Response response = handler.toResponse(exception);
151 assertNotNull(response);
152 assertNotNull(response.getEntity());
153 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());