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 com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.ObjectMapper;
27 import org.junit.Test;
28 import org.mockito.Mockito;
29 import org.onap.so.apihandler.common.ErrorNumbers;
30 import org.onap.so.apihandlerinfra.exceptions.*;
32 import org.apache.http.HttpStatus;
33 import javax.ws.rs.core.Response;
36 import java.io.IOException;
38 import static org.hamcrest.MatcherAssert.assertThat;
39 import static org.junit.Assert.assertEquals;
40 import static org.mockito.Matchers.anyObject;
41 import static org.hamcrest.core.StringStartsWith.startsWith;
43 public class ApiExceptionMapperTest {
45 ApiExceptionMapper mapper = new ApiExceptionMapper();
49 public void testObjectMapperError() throws JsonProcessingException {
50 ObjectMapper mockedMapper = Mockito.mock(ObjectMapper.class);
51 Mockito.when(mockedMapper.writeValueAsString(anyObject())).thenThrow(JsonProcessingException.class);
52 ValidateException validateException = new ValidateException.Builder("Test", 0 , null).build();
53 ApiExceptionMapper mockedException = Mockito.spy(new ApiExceptionMapper());
54 Mockito.doReturn(mockedMapper).when(mockedException).createObjectMapper();
55 Response resp = mockedException.toResponse((ApiException) validateException);
57 /// assertEquals(resp.getStatus(), HttpStatus.SC_BAD_REQUEST);
58 assertThat(resp.getEntity().toString(),startsWith("Exception in buildServiceErrorResponse writing exceptionType to string"));
62 public void testValidateResponse(){
63 ValidateException validateException = new ValidateException.Builder("Test Message", HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).build();
64 Response resp = mapper.toResponse((ApiException) validateException);
66 assertEquals(resp.getStatus(), HttpStatus.SC_BAD_REQUEST);
70 public void testBPMNFailureResponse(){
71 BPMNFailureException bpmnException = new BPMNFailureException.Builder("Test Message", HttpStatus.SC_NOT_FOUND, ErrorNumbers.SVC_BAD_PARAMETER).build();
72 Response resp = mapper.toResponse((ApiException) bpmnException);
74 assertEquals(resp.getStatus(), HttpStatus.SC_NOT_FOUND);
77 public void testClientConnectionResponse(){
78 ClientConnectionException clientConnectionException = new ClientConnectionException.Builder("test", HttpStatus.SC_INTERNAL_SERVER_ERROR,ErrorNumbers.SVC_BAD_PARAMETER).build();
79 Response resp = mapper.toResponse((ApiException) clientConnectionException);
81 assertEquals(resp.getStatus(), HttpStatus.SC_INTERNAL_SERVER_ERROR);
84 public void testVFModuleResponse() {
85 VfModuleNotFoundException vfModuleException = new VfModuleNotFoundException.Builder("Test Message", HttpStatus.SC_CONFLICT,ErrorNumbers.SVC_BAD_PARAMETER).build();
86 Response resp = mapper.toResponse((ApiException) vfModuleException);
88 assertEquals(resp.getStatus(), HttpStatus.SC_CONFLICT);
91 public void testDuplicateRequestResponse() throws IOException {
92 DuplicateRequestException duplicateRequestException = new DuplicateRequestException.Builder("Test1", "Test2","Test3","Test4", HttpStatus.SC_BAD_GATEWAY,ErrorNumbers.SVC_BAD_PARAMETER).build();
93 Response resp = mapper.toResponse((ApiException) duplicateRequestException);
95 assertEquals(resp.getStatus(), HttpStatus.SC_BAD_GATEWAY);