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.ArgumentMatchers.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;
37 import java.io.IOException;
38 import java.io.Writer;
39 import java.util.Arrays;
40 import java.util.List;
42 import javax.ws.rs.core.HttpHeaders;
43 import javax.ws.rs.core.MediaType;
44 import javax.ws.rs.core.Response;
45 import javax.xml.bind.JAXBException;
46 import javax.xml.bind.Marshaller;
48 import org.apache.http.HttpStatus;
49 import org.junit.Before;
50 import org.junit.Test;
51 import org.junit.runner.RunWith;
52 import org.mockito.ArgumentMatchers;
53 import org.mockito.InjectMocks;
54 import org.mockito.Mock;
55 import org.mockito.Mockito;
56 import org.mockito.junit.MockitoJUnitRunner;
57 import org.onap.so.apihandler.common.ErrorNumbers;
58 import org.onap.so.apihandlerinfra.exceptions.ApiException;
59 import org.onap.so.apihandlerinfra.exceptions.ApiExceptionMapper;
60 import org.onap.so.apihandlerinfra.exceptions.BPMNFailureException;
61 import org.onap.so.apihandlerinfra.exceptions.ClientConnectionException;
62 import org.onap.so.apihandlerinfra.exceptions.DuplicateRequestException;
63 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
64 import org.onap.so.apihandlerinfra.exceptions.VfModuleNotFoundException;
66 import com.fasterxml.jackson.core.JsonProcessingException;
67 import com.fasterxml.jackson.databind.ObjectMapper;
70 @RunWith(MockitoJUnitRunner.class)
71 public class ApiExceptionMapperTest {
74 private HttpHeaders headers;
76 private Marshaller marshaller;
79 ApiExceptionMapper mapper = new ApiExceptionMapper();
84 when(headers.getAcceptableMediaTypes()).thenReturn(Arrays.asList(MediaType.APPLICATION_JSON_TYPE));
87 public void testObjectMapperError() throws JsonProcessingException {
88 ObjectMapper mockedMapper = Mockito.mock(ObjectMapper.class);
89 Mockito.when(mockedMapper.writeValueAsString(anyObject())).thenThrow(JsonProcessingException.class);
90 ValidateException validateException = new ValidateException.Builder("Test", 0 , null).build();
91 ApiExceptionMapper mockedException = Mockito.spy(mapper);
92 Mockito.doReturn(mockedMapper).when(mockedException).createObjectMapper();
93 Response resp = mockedException.toResponse((ApiException) validateException);
95 /// assertEquals(resp.getStatus(), HttpStatus.SC_BAD_REQUEST);
96 assertThat(resp.getEntity().toString(),startsWith("Exception in buildServiceErrorResponse writing exceptionType to string"));
100 public void testValidateResponse(){
101 ValidateException validateException = new ValidateException.Builder("Test Message", HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).build();
102 Response resp = mapper.toResponse((ApiException) validateException);
104 assertEquals(resp.getStatus(), HttpStatus.SC_BAD_REQUEST);
108 public void testBPMNFailureResponse(){
109 BPMNFailureException bpmnException = new BPMNFailureException.Builder("Test Message", HttpStatus.SC_NOT_FOUND, ErrorNumbers.SVC_BAD_PARAMETER).build();
110 Response resp = mapper.toResponse((ApiException) bpmnException);
112 assertEquals(resp.getStatus(), HttpStatus.SC_NOT_FOUND);
115 public void testClientConnectionResponse(){
116 ClientConnectionException clientConnectionException = new ClientConnectionException.Builder("test", HttpStatus.SC_INTERNAL_SERVER_ERROR,ErrorNumbers.SVC_BAD_PARAMETER).build();
117 Response resp = mapper.toResponse((ApiException) clientConnectionException);
119 assertEquals(resp.getStatus(), HttpStatus.SC_INTERNAL_SERVER_ERROR);
122 public void testVFModuleResponse() {
123 VfModuleNotFoundException vfModuleException = new VfModuleNotFoundException.Builder("Test Message", HttpStatus.SC_CONFLICT,ErrorNumbers.SVC_BAD_PARAMETER).build();
124 Response resp = mapper.toResponse((ApiException) vfModuleException);
126 assertEquals(resp.getStatus(), HttpStatus.SC_CONFLICT);
129 public void testDuplicateRequestResponse() throws IOException {
130 DuplicateRequestException duplicateRequestException = new DuplicateRequestException.Builder("Test1", "Test2","Test3","Test4", HttpStatus.SC_BAD_GATEWAY,ErrorNumbers.SVC_BAD_PARAMETER).build();
131 Response resp = mapper.toResponse((ApiException) duplicateRequestException);
133 assertEquals(resp.getStatus(), HttpStatus.SC_BAD_GATEWAY);
137 public void verifyXMLPath() throws JAXBException {
138 when(headers.getAcceptableMediaTypes()).thenReturn(Arrays.asList(MediaType.APPLICATION_XML_TYPE));
139 BPMNFailureException bpmnException = new BPMNFailureException.Builder("Test Message", HttpStatus.SC_NOT_FOUND, ErrorNumbers.SVC_BAD_PARAMETER).build();
140 ApiExceptionMapper mapperSpy = Mockito.spy(mapper);
141 doReturn(marshaller).when(mapperSpy).getMarshaller();
142 Response resp = mapperSpy.toResponse((ApiException) bpmnException);
143 verify(marshaller, times(1)).marshal(any(Object.class), any(Writer.class));
147 public void verifyMediaType() {
148 ApiExceptionMapper mapperSpy = Mockito.spy(mapper);
149 BPMNFailureException bpmnException = new BPMNFailureException.Builder("Test Message", HttpStatus.SC_NOT_FOUND, ErrorNumbers.SVC_BAD_PARAMETER).build();
150 when(headers.getAcceptableMediaTypes()).thenReturn(Arrays.asList(MediaType.APPLICATION_XML_TYPE.withCharset("UTF-8")));
151 mapperSpy.toResponse(bpmnException);
152 verify(mapperSpy, times(1)).buildServiceErrorResponse(any(String.class), any(String.class), ArgumentMatchers.isNull(), eq(MediaType.APPLICATION_XML_TYPE));
153 when(headers.getAcceptableMediaTypes()).thenReturn(Arrays.asList(MediaType.APPLICATION_JSON_TYPE.withCharset("UTF-8")));
154 mapperSpy = Mockito.spy(mapper);
155 mapperSpy.toResponse(bpmnException);
156 verify(mapperSpy, times(1)).buildServiceErrorResponse(any(String.class), any(String.class), ArgumentMatchers.isNull(), eq(MediaType.APPLICATION_JSON_TYPE));
157 when(headers.getAcceptableMediaTypes()).thenReturn(null);
158 mapperSpy = Mockito.spy(mapper);
159 mapperSpy.toResponse(bpmnException);
160 verify(mapperSpy, times(1)).buildServiceErrorResponse(any(String.class), any(String.class),ArgumentMatchers.isNull(), eq(MediaType.APPLICATION_JSON_TYPE));