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.onap.so.apihandlerinfra.exceptions;
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;
36 import java.io.IOException;
37 import java.io.Writer;
38 import java.util.Arrays;
39 import java.util.List;
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;
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;
64 import com.fasterxml.jackson.core.JsonProcessingException;
65 import com.fasterxml.jackson.databind.ObjectMapper;
68 @RunWith(MockitoJUnitRunner.class)
69 public class ApiExceptionMapperTest {
72 private HttpHeaders headers;
74 private Marshaller marshaller;
77 ApiExceptionMapper mapper = new ApiExceptionMapper();
82 when(headers.getAcceptableMediaTypes()).thenReturn(Arrays.asList(MediaType.APPLICATION_JSON_TYPE));
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);
93 /// assertEquals(resp.getStatus(), HttpStatus.SC_BAD_REQUEST);
94 assertThat(resp.getEntity().toString(),startsWith("Exception in buildServiceErrorResponse writing exceptionType to string"));
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);
102 assertEquals(resp.getStatus(), HttpStatus.SC_BAD_REQUEST);
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);
110 assertEquals(resp.getStatus(), HttpStatus.SC_NOT_FOUND);
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);
117 assertEquals(resp.getStatus(), HttpStatus.SC_INTERNAL_SERVER_ERROR);
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);
124 assertEquals(resp.getStatus(), HttpStatus.SC_CONFLICT);
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);
131 assertEquals(resp.getStatus(), HttpStatus.SC_BAD_GATEWAY);
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));
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));