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;
24 import static org.hamcrest.MatcherAssert.assertThat;
25 import static org.hamcrest.core.StringStartsWith.startsWith;
26 import static org.junit.Assert.assertEquals;
27 import static org.mockito.Matchers.anyObject;
29 import java.io.IOException;
31 import javax.ws.rs.core.Response;
33 import org.apache.http.HttpStatus;
34 import org.junit.Test;
35 import org.mockito.Mockito;
36 import org.onap.so.apihandler.common.ErrorNumbers;
37 import org.onap.so.apihandlerinfra.exceptions.ApiException;
38 import org.onap.so.apihandlerinfra.exceptions.ApiExceptionMapper;
39 import org.onap.so.apihandlerinfra.exceptions.BPMNFailureException;
40 import org.onap.so.apihandlerinfra.exceptions.ClientConnectionException;
41 import org.onap.so.apihandlerinfra.exceptions.DuplicateRequestException;
42 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
43 import org.onap.so.apihandlerinfra.exceptions.VfModuleNotFoundException;
45 import com.fasterxml.jackson.core.JsonProcessingException;
46 import com.fasterxml.jackson.databind.ObjectMapper;
48 public class ApiExceptionMapperTest extends BaseTest {
50 ApiExceptionMapper mapper = new ApiExceptionMapper();
54 public void testObjectMapperError() throws JsonProcessingException {
55 ObjectMapper mockedMapper = Mockito.mock(ObjectMapper.class);
56 Mockito.when(mockedMapper.writeValueAsString(anyObject())).thenThrow(JsonProcessingException.class);
57 ValidateException validateException = new ValidateException.Builder("Test", 0 , null).build();
58 ApiExceptionMapper mockedException = Mockito.spy(new ApiExceptionMapper());
59 Mockito.doReturn(mockedMapper).when(mockedException).createObjectMapper();
60 Response resp = mockedException.toResponse((ApiException) validateException);
62 /// assertEquals(resp.getStatus(), HttpStatus.SC_BAD_REQUEST);
63 assertThat(resp.getEntity().toString(),startsWith("Exception in buildServiceErrorResponse writing exceptionType to string"));
67 public void testValidateResponse(){
68 ValidateException validateException = new ValidateException.Builder("Test Message", HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).build();
69 Response resp = mapper.toResponse((ApiException) validateException);
71 assertEquals(resp.getStatus(), HttpStatus.SC_BAD_REQUEST);
75 public void testBPMNFailureResponse(){
76 BPMNFailureException bpmnException = new BPMNFailureException.Builder("Test Message", HttpStatus.SC_NOT_FOUND, ErrorNumbers.SVC_BAD_PARAMETER).build();
77 Response resp = mapper.toResponse((ApiException) bpmnException);
79 assertEquals(resp.getStatus(), HttpStatus.SC_NOT_FOUND);
82 public void testClientConnectionResponse(){
83 ClientConnectionException clientConnectionException = new ClientConnectionException.Builder("test", HttpStatus.SC_INTERNAL_SERVER_ERROR,ErrorNumbers.SVC_BAD_PARAMETER).build();
84 Response resp = mapper.toResponse((ApiException) clientConnectionException);
86 assertEquals(resp.getStatus(), HttpStatus.SC_INTERNAL_SERVER_ERROR);
89 public void testVFModuleResponse() {
90 VfModuleNotFoundException vfModuleException = new VfModuleNotFoundException.Builder("Test Message", HttpStatus.SC_CONFLICT,ErrorNumbers.SVC_BAD_PARAMETER).build();
91 Response resp = mapper.toResponse((ApiException) vfModuleException);
93 assertEquals(resp.getStatus(), HttpStatus.SC_CONFLICT);
96 public void testDuplicateRequestResponse() throws IOException {
97 DuplicateRequestException duplicateRequestException = new DuplicateRequestException.Builder("Test1", "Test2","Test3","Test4", HttpStatus.SC_BAD_GATEWAY,ErrorNumbers.SVC_BAD_PARAMETER).build();
98 Response resp = mapper.toResponse((ApiException) duplicateRequestException);
100 assertEquals(resp.getStatus(), HttpStatus.SC_BAD_GATEWAY);