1a846da65ad367034b9a7eca55a5801463dc5e03
[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.exceptions;
22
23
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;
63
64
65 @RunWith(MockitoJUnitRunner.class)
66 public class ApiExceptionMapperTest {
67
68     @Mock
69     private HttpHeaders headers;
70     @Mock
71     private Marshaller marshaller;
72
73     @InjectMocks
74     ApiExceptionMapper mapper = new ApiExceptionMapper();
75
76     @Test
77     public void testValidateResponse() {
78         ValidateException validateException =
79                 new ValidateException.Builder("Test Message", HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER)
80                         .build();
81         Response resp = mapper.toResponse((ApiException) validateException);
82
83         assertEquals(resp.getStatus(), HttpStatus.SC_BAD_REQUEST);
84     }
85
86     @Test
87     public void testBPMNFailureResponse() {
88         BPMNFailureException bpmnException = new BPMNFailureException.Builder("Test Message", HttpStatus.SC_NOT_FOUND,
89                 ErrorNumbers.SVC_BAD_PARAMETER).build();
90         Response resp = mapper.toResponse((ApiException) bpmnException);
91
92         assertEquals(resp.getStatus(), HttpStatus.SC_NOT_FOUND);
93     }
94
95     @Test
96     public void testClientConnectionResponse() {
97         ClientConnectionException clientConnectionException = new ClientConnectionException.Builder("test",
98                 HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_BAD_PARAMETER).build();
99         Response resp = mapper.toResponse((ApiException) clientConnectionException);
100
101         assertEquals(resp.getStatus(), HttpStatus.SC_INTERNAL_SERVER_ERROR);
102     }
103
104     @Test
105     public void testVFModuleResponse() {
106         VfModuleNotFoundException vfModuleException = new VfModuleNotFoundException.Builder("Test Message",
107                 HttpStatus.SC_CONFLICT, ErrorNumbers.SVC_BAD_PARAMETER).build();
108         Response resp = mapper.toResponse((ApiException) vfModuleException);
109
110         assertEquals(resp.getStatus(), HttpStatus.SC_CONFLICT);
111     }
112
113     @Test
114     public void testDuplicateRequestResponse() throws IOException {
115         DuplicateRequestException duplicateRequestException = new DuplicateRequestException.Builder("Test1", "Test2",
116                 "Test3", "Test4", HttpStatus.SC_BAD_GATEWAY, ErrorNumbers.SVC_BAD_PARAMETER).build();
117         Response resp = mapper.toResponse((ApiException) duplicateRequestException);
118
119         assertEquals(resp.getStatus(), HttpStatus.SC_BAD_GATEWAY);
120     }
121 }