e666df34f960284e0842119c1e2cf6992e7c7bb6
[so.git] / mso-api-handlers / mso-api-handler-common / src / test / java / org / onap / so / apihandlerinfra / exceptions / ApiExceptionMapperTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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.onap.so.apihandlerinfra.exceptions;
22
23
24 import static org.hamcrest.CoreMatchers.equalTo;
25 import static org.hamcrest.MatcherAssert.assertThat;
26 import static org.hamcrest.core.StringStartsWith.startsWith;
27 import static org.junit.Assert.assertEquals;
28 import static org.mockito.Matchers.any;
29 import static org.mockito.Matchers.anyObject;
30 import static org.mockito.Matchers.eq;
31 import static org.mockito.Mockito.doReturn;
32 import static org.mockito.Mockito.times;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.when;
35
36 import java.io.IOException;
37 import java.io.Writer;
38 import java.util.Arrays;
39 import java.util.List;
40
41 import javax.ws.rs.core.HttpHeaders;
42 import javax.ws.rs.core.MediaType;
43 import javax.ws.rs.core.Response;
44 import javax.xml.bind.JAXBException;
45 import javax.xml.bind.Marshaller;
46
47 import org.apache.http.HttpStatus;
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 import org.mockito.InjectMocks;
52 import org.mockito.Mock;
53 import org.mockito.Mockito;
54 import org.mockito.runners.MockitoJUnitRunner;
55 import org.onap.so.apihandler.common.ErrorNumbers;
56 import org.onap.so.apihandlerinfra.exceptions.ApiException;
57 import org.onap.so.apihandlerinfra.exceptions.ApiExceptionMapper;
58 import org.onap.so.apihandlerinfra.exceptions.BPMNFailureException;
59 import org.onap.so.apihandlerinfra.exceptions.ClientConnectionException;
60 import org.onap.so.apihandlerinfra.exceptions.DuplicateRequestException;
61 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
62 import org.onap.so.apihandlerinfra.exceptions.VfModuleNotFoundException;
63
64 import com.fasterxml.jackson.core.JsonProcessingException;
65 import com.fasterxml.jackson.databind.ObjectMapper;
66
67
68 @RunWith(MockitoJUnitRunner.class)
69 public class ApiExceptionMapperTest {
70
71         @Mock
72         private HttpHeaders headers;
73         @Mock
74         private Marshaller marshaller;
75         
76         @InjectMocks
77     ApiExceptionMapper mapper = new ApiExceptionMapper();
78
79
80         @Before
81         public void setUp() {
82                 when(headers.getAcceptableMediaTypes()).thenReturn(Arrays.asList(MediaType.APPLICATION_JSON_TYPE));
83         }
84     @Test
85     public void testObjectMapperError() throws JsonProcessingException {
86         ObjectMapper mockedMapper = Mockito.mock(ObjectMapper.class);
87         Mockito.when(mockedMapper.writeValueAsString(anyObject())).thenThrow(JsonProcessingException.class);
88         ValidateException validateException = new ValidateException.Builder("Test", 0 , null).build();
89         ApiExceptionMapper mockedException = Mockito.spy(mapper);
90         Mockito.doReturn(mockedMapper).when(mockedException).createObjectMapper();
91         Response resp = mockedException.toResponse((ApiException) validateException);
92
93        /// assertEquals(resp.getStatus(), HttpStatus.SC_BAD_REQUEST);
94         assertThat(resp.getEntity().toString(),startsWith("Exception in buildServiceErrorResponse writing exceptionType to string"));
95     }
96
97     @Test
98     public void testValidateResponse(){
99         ValidateException validateException = new ValidateException.Builder("Test Message", HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).build();
100         Response resp = mapper.toResponse((ApiException) validateException);
101
102         assertEquals(resp.getStatus(), HttpStatus.SC_BAD_REQUEST);
103     }
104
105     @Test
106     public void testBPMNFailureResponse(){
107         BPMNFailureException bpmnException = new BPMNFailureException.Builder("Test Message", HttpStatus.SC_NOT_FOUND, ErrorNumbers.SVC_BAD_PARAMETER).build();
108         Response resp = mapper.toResponse((ApiException) bpmnException);
109
110         assertEquals(resp.getStatus(), HttpStatus.SC_NOT_FOUND);
111     }
112     @Test
113     public void testClientConnectionResponse(){
114         ClientConnectionException clientConnectionException = new ClientConnectionException.Builder("test", HttpStatus.SC_INTERNAL_SERVER_ERROR,ErrorNumbers.SVC_BAD_PARAMETER).build();
115         Response resp =  mapper.toResponse((ApiException) clientConnectionException);
116
117         assertEquals(resp.getStatus(), HttpStatus.SC_INTERNAL_SERVER_ERROR);
118     }
119     @Test
120     public void testVFModuleResponse() {
121         VfModuleNotFoundException vfModuleException = new VfModuleNotFoundException.Builder("Test Message", HttpStatus.SC_CONFLICT,ErrorNumbers.SVC_BAD_PARAMETER).build();
122         Response resp =  mapper.toResponse((ApiException) vfModuleException);
123
124         assertEquals(resp.getStatus(), HttpStatus.SC_CONFLICT);
125     }
126     @Test
127     public void testDuplicateRequestResponse() throws IOException {
128         DuplicateRequestException duplicateRequestException = new DuplicateRequestException.Builder("Test1", "Test2","Test3","Test4", HttpStatus.SC_BAD_GATEWAY,ErrorNumbers.SVC_BAD_PARAMETER).build();
129         Response resp =  mapper.toResponse((ApiException) duplicateRequestException);
130
131         assertEquals(resp.getStatus(), HttpStatus.SC_BAD_GATEWAY);
132     }
133     
134     @Test
135     public void verifyXMLPath() throws JAXBException {
136                 when(headers.getAcceptableMediaTypes()).thenReturn(Arrays.asList(MediaType.APPLICATION_XML_TYPE));
137         BPMNFailureException bpmnException = new BPMNFailureException.Builder("Test Message", HttpStatus.SC_NOT_FOUND, ErrorNumbers.SVC_BAD_PARAMETER).build();
138         ApiExceptionMapper mapperSpy = Mockito.spy(mapper);
139         doReturn(marshaller).when(mapperSpy).getMarshaller();
140         Response resp =  mapperSpy.toResponse((ApiException) bpmnException);
141         verify(marshaller, times(1)).marshal(any(Object.class), any(Writer.class));
142     }
143     
144     @Test
145     public void verifyMediaType() {
146         ApiExceptionMapper mapperSpy = Mockito.spy(mapper);
147         BPMNFailureException bpmnException = new BPMNFailureException.Builder("Test Message", HttpStatus.SC_NOT_FOUND, ErrorNumbers.SVC_BAD_PARAMETER).build();
148                 when(headers.getAcceptableMediaTypes()).thenReturn(Arrays.asList(MediaType.APPLICATION_XML_TYPE.withCharset("UTF-8")));
149         mapperSpy.toResponse(bpmnException);
150         verify(mapperSpy, times(1)).buildServiceErrorResponse(any(String.class), any(String.class), any(List.class), eq(MediaType.APPLICATION_XML_TYPE));
151                 when(headers.getAcceptableMediaTypes()).thenReturn(Arrays.asList(MediaType.APPLICATION_JSON_TYPE.withCharset("UTF-8")));
152         mapperSpy = Mockito.spy(mapper);
153         mapperSpy.toResponse(bpmnException);
154         verify(mapperSpy, times(1)).buildServiceErrorResponse(any(String.class), any(String.class), any(List.class), eq(MediaType.APPLICATION_JSON_TYPE));
155                 when(headers.getAcceptableMediaTypes()).thenReturn(null);
156         mapperSpy = Mockito.spy(mapper);
157         mapperSpy.toResponse(bpmnException);
158         verify(mapperSpy, times(1)).buildServiceErrorResponse(any(String.class), any(String.class), any(List.class), eq(MediaType.APPLICATION_JSON_TYPE));
159     }
160 }