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.ArgumentMatchers.anyObject;
 
  30 import static org.mockito.ArgumentMatchers.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 import java.io.IOException;
 
  36 import java.io.Writer;
 
  37 import java.util.Arrays;
 
  38 import java.util.List;
 
  39 import javax.ws.rs.core.HttpHeaders;
 
  40 import javax.ws.rs.core.MediaType;
 
  41 import javax.ws.rs.core.Response;
 
  42 import javax.xml.bind.JAXBException;
 
  43 import javax.xml.bind.Marshaller;
 
  44 import org.apache.http.HttpStatus;
 
  45 import org.junit.Before;
 
  46 import org.junit.Test;
 
  47 import org.junit.runner.RunWith;
 
  48 import org.mockito.ArgumentMatchers;
 
  49 import org.mockito.InjectMocks;
 
  50 import org.mockito.Mock;
 
  51 import org.mockito.Mockito;
 
  52 import org.mockito.junit.MockitoJUnitRunner;
 
  53 import org.onap.so.apihandler.common.ErrorNumbers;
 
  54 import org.onap.so.apihandlerinfra.exceptions.ApiException;
 
  55 import org.onap.so.apihandlerinfra.exceptions.ApiExceptionMapper;
 
  56 import org.onap.so.apihandlerinfra.exceptions.BPMNFailureException;
 
  57 import org.onap.so.apihandlerinfra.exceptions.ClientConnectionException;
 
  58 import org.onap.so.apihandlerinfra.exceptions.DuplicateRequestException;
 
  59 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
 
  60 import org.onap.so.apihandlerinfra.exceptions.VfModuleNotFoundException;
 
  61 import com.fasterxml.jackson.core.JsonProcessingException;
 
  62 import com.fasterxml.jackson.databind.ObjectMapper;
 
  65 @RunWith(MockitoJUnitRunner.class)
 
  66 public class ApiExceptionMapperTest {
 
  69     private HttpHeaders headers;
 
  71     private Marshaller marshaller;
 
  74     ApiExceptionMapper mapper = new ApiExceptionMapper();
 
  79         when(headers.getAcceptableMediaTypes()).thenReturn(Arrays.asList(MediaType.APPLICATION_JSON_TYPE));
 
  83     public void testObjectMapperError() throws JsonProcessingException {
 
  84         ObjectMapper mockedMapper = Mockito.mock(ObjectMapper.class);
 
  85         Mockito.when(mockedMapper.writeValueAsString(anyObject())).thenThrow(JsonProcessingException.class);
 
  86         ValidateException validateException = new ValidateException.Builder("Test", 0, null).build();
 
  87         ApiExceptionMapper mockedException = Mockito.spy(mapper);
 
  88         Mockito.doReturn(mockedMapper).when(mockedException).createObjectMapper();
 
  89         Response resp = mockedException.toResponse((ApiException) validateException);
 
  91         /// assertEquals(resp.getStatus(), HttpStatus.SC_BAD_REQUEST);
 
  92         assertThat(resp.getEntity().toString(),
 
  93                 startsWith("Exception in buildServiceErrorResponse writing exceptionType to string"));
 
  97     public void testValidateResponse() {
 
  98         ValidateException validateException =
 
  99                 new ValidateException.Builder("Test Message", HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER)
 
 101         Response resp = mapper.toResponse((ApiException) validateException);
 
 103         assertEquals(resp.getStatus(), HttpStatus.SC_BAD_REQUEST);
 
 107     public void testBPMNFailureResponse() {
 
 108         BPMNFailureException bpmnException = new BPMNFailureException.Builder("Test Message", HttpStatus.SC_NOT_FOUND,
 
 109                 ErrorNumbers.SVC_BAD_PARAMETER).build();
 
 110         Response resp = mapper.toResponse((ApiException) bpmnException);
 
 112         assertEquals(resp.getStatus(), HttpStatus.SC_NOT_FOUND);
 
 116     public void testClientConnectionResponse() {
 
 117         ClientConnectionException clientConnectionException = new ClientConnectionException.Builder("test",
 
 118                 HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_BAD_PARAMETER).build();
 
 119         Response resp = mapper.toResponse((ApiException) clientConnectionException);
 
 121         assertEquals(resp.getStatus(), HttpStatus.SC_INTERNAL_SERVER_ERROR);
 
 125     public void testVFModuleResponse() {
 
 126         VfModuleNotFoundException vfModuleException = new VfModuleNotFoundException.Builder("Test Message",
 
 127                 HttpStatus.SC_CONFLICT, ErrorNumbers.SVC_BAD_PARAMETER).build();
 
 128         Response resp = mapper.toResponse((ApiException) vfModuleException);
 
 130         assertEquals(resp.getStatus(), HttpStatus.SC_CONFLICT);
 
 134     public void testDuplicateRequestResponse() throws IOException {
 
 135         DuplicateRequestException duplicateRequestException = new DuplicateRequestException.Builder("Test1", "Test2",
 
 136                 "Test3", "Test4", HttpStatus.SC_BAD_GATEWAY, ErrorNumbers.SVC_BAD_PARAMETER).build();
 
 137         Response resp = mapper.toResponse((ApiException) duplicateRequestException);
 
 139         assertEquals(resp.getStatus(), HttpStatus.SC_BAD_GATEWAY);
 
 143     public void verifyXMLPath() throws JAXBException {
 
 144         when(headers.getAcceptableMediaTypes()).thenReturn(Arrays.asList(MediaType.APPLICATION_XML_TYPE));
 
 145         BPMNFailureException bpmnException = new BPMNFailureException.Builder("Test Message", HttpStatus.SC_NOT_FOUND,
 
 146                 ErrorNumbers.SVC_BAD_PARAMETER).build();
 
 147         ApiExceptionMapper mapperSpy = Mockito.spy(mapper);
 
 148         doReturn(marshaller).when(mapperSpy).getMarshaller();
 
 149         Response resp = mapperSpy.toResponse((ApiException) bpmnException);
 
 150         verify(marshaller, times(1)).marshal(any(Object.class), any(Writer.class));
 
 154     public void verifyMediaType() {
 
 155         ApiExceptionMapper mapperSpy = Mockito.spy(mapper);
 
 156         BPMNFailureException bpmnException = new BPMNFailureException.Builder("Test Message", HttpStatus.SC_NOT_FOUND,
 
 157                 ErrorNumbers.SVC_BAD_PARAMETER).build();
 
 158         when(headers.getAcceptableMediaTypes())
 
 159                 .thenReturn(Arrays.asList(MediaType.APPLICATION_XML_TYPE.withCharset("UTF-8")));
 
 160         mapperSpy.toResponse(bpmnException);
 
 161         verify(mapperSpy, times(1)).buildServiceErrorResponse(any(String.class), any(String.class),
 
 162                 ArgumentMatchers.isNull(), eq(MediaType.APPLICATION_XML_TYPE));
 
 163         when(headers.getAcceptableMediaTypes())
 
 164                 .thenReturn(Arrays.asList(MediaType.APPLICATION_JSON_TYPE.withCharset("UTF-8")));
 
 165         mapperSpy = Mockito.spy(mapper);
 
 166         mapperSpy.toResponse(bpmnException);
 
 167         verify(mapperSpy, times(1)).buildServiceErrorResponse(any(String.class), any(String.class),
 
 168                 ArgumentMatchers.isNull(), eq(MediaType.APPLICATION_JSON_TYPE));
 
 169         when(headers.getAcceptableMediaTypes()).thenReturn(null);
 
 170         mapperSpy = Mockito.spy(mapper);
 
 171         mapperSpy.toResponse(bpmnException);
 
 172         verify(mapperSpy, times(1)).buildServiceErrorResponse(any(String.class), any(String.class),
 
 173                 ArgumentMatchers.isNull(), eq(MediaType.APPLICATION_JSON_TYPE));