Update the license for 2017-2018 license
[aai/traversal.git] / aai-traversal / src / test / java / org / onap / aai / rest / ExceptionHandlerTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.aai.rest;
21
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;
31
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;
41
42 import static org.junit.Assert.*;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.when;
45
46 public class ExceptionHandlerTest {
47
48     protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json");
49
50     @Mock
51     private HttpHeaders httpHeaders;
52
53     @Mock
54     private HttpServletRequest request;
55
56     @InjectMocks
57     private ExceptionHandler handler = new ExceptionHandler();
58
59     @Before
60     public void setup(){
61         MockitoAnnotations.initMocks(this);
62
63         MultivaluedHashMap headersMultiMap     = new MultivaluedHashMap<>();
64
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", "");
70
71         List<MediaType> outputMediaTypes = new ArrayList<>();
72         outputMediaTypes.add(APPLICATION_JSON);
73         when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes);
74         when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap);
75     }
76
77     @Test
78     public void testConversionOfWebApplicationResponse() throws Exception {
79
80         Exception exception = new WebApplicationException();
81         Response response = handler.toResponse(exception);
82
83         assertNotNull(response);
84         assertNull(response.getEntity());
85         assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),  response.getStatus());
86     }
87
88     @Test
89     public void testConversionOfWebApplicationResponseWhenUmarshalExceptionResultBadRequest() throws Exception {
90
91         SAXParseException2 mockSaxParseException = mock(SAXParseException2.class);
92         Exception exception = new WebApplicationException(mockSaxParseException);
93         Response response = handler.toResponse(exception);
94
95         assertNotNull(response);
96         assertNotNull(response.getEntity());
97         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(),  response.getStatus());
98     }
99
100     @Test
101     public void testConversionWhenJsonParseExceptionResultBadRequest() throws Exception {
102
103         JsonLocation jsonLocation = mock(JsonLocation.class);
104         Exception exception = new JsonParseException("", jsonLocation);
105         Response response = handler.toResponse(exception);
106
107         assertNotNull(response);
108         assertNotNull(response.getEntity());
109         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(),  response.getStatus());
110     }
111
112     @Test
113     public void testConversionWhenJsonMappingExceptionResultBadRequest() throws Exception {
114
115         JsonLocation jsonLocation = mock(JsonLocation.class);
116         Exception exception = new JsonMappingException("", jsonLocation);
117         Response response = handler.toResponse(exception);
118
119         assertNotNull(response);
120         assertNotNull(response.getEntity());
121         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(),  response.getStatus());
122     }
123
124     @Test
125     public void testConversionWhenUnknownExceptionResultBadRequest() throws Exception {
126
127         Exception exception = mock(Exception.class);
128         Response response = handler.toResponse(exception);
129
130         when(request.getMethod()).thenReturn("GET");
131
132         assertNotNull(response);
133         assertNotNull(response.getEntity());
134         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(),  response.getStatus());
135
136
137     }
138
139     @Test
140     public void testConversionWhenUnknownExceptionResultBadRequestForXmlResponseType() throws Exception {
141
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);
146
147         Exception exception = mock(Exception.class);
148         Response response = handler.toResponse(exception);
149
150         assertNotNull(response);
151         assertNotNull(response.getEntity());
152         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(),  response.getStatus());
153     }
154 }