95daf2e501b3b95dc20ae8ab63e7a84f3cedcbfe
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.apihandlerinfra;
22
23
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;
28
29 import java.io.IOException;
30
31 import javax.ws.rs.core.Response;
32
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;
44
45 import com.fasterxml.jackson.core.JsonProcessingException;
46 import com.fasterxml.jackson.databind.ObjectMapper;
47
48 public class ApiExceptionMapperTest extends BaseTest {
49
50     ApiExceptionMapper mapper = new ApiExceptionMapper();
51
52
53     @Test
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);
61
62        /// assertEquals(resp.getStatus(), HttpStatus.SC_BAD_REQUEST);
63         assertThat(resp.getEntity().toString(),startsWith("Exception in buildServiceErrorResponse writing exceptionType to string"));
64     }
65
66     @Test
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);
70
71         assertEquals(resp.getStatus(), HttpStatus.SC_BAD_REQUEST);
72     }
73
74     @Test
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);
78
79         assertEquals(resp.getStatus(), HttpStatus.SC_NOT_FOUND);
80     }
81     @Test
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);
85
86         assertEquals(resp.getStatus(), HttpStatus.SC_INTERNAL_SERVER_ERROR);
87     }
88     @Test
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);
92
93         assertEquals(resp.getStatus(), HttpStatus.SC_CONFLICT);
94     }
95     @Test
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);
99
100         assertEquals(resp.getStatus(), HttpStatus.SC_BAD_GATEWAY);
101     }
102 }