b98c47490a89b3679da4b820a4512df17fb66a04
[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 com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26
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.*;
31
32 import org.apache.http.HttpStatus;
33 import javax.ws.rs.core.Response;
34
35
36 import java.io.IOException;
37
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;
42
43 public class ApiExceptionMapperTest {
44
45     ApiExceptionMapper mapper = new ApiExceptionMapper();
46
47
48     @Test
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);
56
57        /// assertEquals(resp.getStatus(), HttpStatus.SC_BAD_REQUEST);
58         assertThat(resp.getEntity().toString(),startsWith("Exception in buildServiceErrorResponse writing exceptionType to string"));
59     }
60
61     @Test
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);
65
66         assertEquals(resp.getStatus(), HttpStatus.SC_BAD_REQUEST);
67     }
68
69     @Test
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);
73
74         assertEquals(resp.getStatus(), HttpStatus.SC_NOT_FOUND);
75     }
76     @Test
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);
80
81         assertEquals(resp.getStatus(), HttpStatus.SC_INTERNAL_SERVER_ERROR);
82     }
83     @Test
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);
87
88         assertEquals(resp.getStatus(), HttpStatus.SC_CONFLICT);
89     }
90     @Test
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);
94
95         assertEquals(resp.getStatus(), HttpStatus.SC_BAD_GATEWAY);
96     }
97 }