2268abd885e9db28762eb2005685357aa956a76d
[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.junit.Assert.assertEquals;
25 import java.io.IOException;
26 import javax.ws.rs.core.HttpHeaders;
27 import javax.ws.rs.core.Response;
28 import javax.xml.bind.Marshaller;
29 import org.apache.http.HttpStatus;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.InjectMocks;
33 import org.mockito.Mock;
34 import org.mockito.junit.MockitoJUnitRunner;
35 import org.onap.so.apihandler.common.ErrorNumbers;
36
37
38 @RunWith(MockitoJUnitRunner.class)
39 public class ApiExceptionMapperTest {
40
41     @Mock
42     private HttpHeaders headers;
43     @Mock
44     private Marshaller marshaller;
45
46     @InjectMocks
47     ApiExceptionMapper mapper = new ApiExceptionMapper();
48
49     @Test
50     public void testValidateResponse() {
51         ValidateException validateException =
52                 new ValidateException.Builder("Test Message", HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER)
53                         .build();
54         Response resp = mapper.toResponse((ApiException) validateException);
55
56         assertEquals(resp.getStatus(), HttpStatus.SC_BAD_REQUEST);
57     }
58
59     @Test
60     public void testBPMNFailureResponse() {
61         BPMNFailureException bpmnException = new BPMNFailureException.Builder("Test Message", HttpStatus.SC_NOT_FOUND,
62                 ErrorNumbers.SVC_BAD_PARAMETER).build();
63         Response resp = mapper.toResponse((ApiException) bpmnException);
64
65         assertEquals(resp.getStatus(), HttpStatus.SC_NOT_FOUND);
66     }
67
68     @Test
69     public void testClientConnectionResponse() {
70         ClientConnectionException clientConnectionException = new ClientConnectionException.Builder("test",
71                 HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_BAD_PARAMETER).build();
72         Response resp = mapper.toResponse((ApiException) clientConnectionException);
73
74         assertEquals(resp.getStatus(), HttpStatus.SC_INTERNAL_SERVER_ERROR);
75     }
76
77     @Test
78     public void testVFModuleResponse() {
79         VfModuleNotFoundException vfModuleException = new VfModuleNotFoundException.Builder("Test Message",
80                 HttpStatus.SC_CONFLICT, ErrorNumbers.SVC_BAD_PARAMETER).build();
81         Response resp = mapper.toResponse((ApiException) vfModuleException);
82
83         assertEquals(resp.getStatus(), HttpStatus.SC_CONFLICT);
84     }
85
86     @Test
87     public void testDuplicateRequestResponse() throws IOException {
88         DuplicateRequestException duplicateRequestException = new DuplicateRequestException.Builder("Test1", "Test2",
89                 "Test3", "Test4", HttpStatus.SC_BAD_GATEWAY, ErrorNumbers.SVC_BAD_PARAMETER).build();
90         Response resp = mapper.toResponse((ApiException) duplicateRequestException);
91
92         assertEquals(resp.getStatus(), HttpStatus.SC_BAD_GATEWAY);
93     }
94 }