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