Containerization feature of SO
[so.git] / common / src / test / java / org / onap / so / web / exceptions / RuntimeExceptionMapperTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 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.web.exceptions;
22
23 import static org.hamcrest.CoreMatchers.equalTo;
24 import static org.hamcrest.CoreMatchers.instanceOf;
25 import static org.hamcrest.CoreMatchers.is;
26 import static org.hamcrest.CoreMatchers.nullValue;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertThat;
29
30 import javax.ws.rs.NotFoundException;
31 import javax.ws.rs.core.Response;
32 import javax.ws.rs.core.Response.Status;
33
34 import org.junit.AfterClass;
35 import org.junit.Test;
36 import org.onap.so.logger.MsoAlarmLogger;
37
38 public class RuntimeExceptionMapperTest {
39
40         
41         @AfterClass
42         public static void tearDown() {
43                 MsoAlarmLogger logger = new MsoAlarmLogger();
44                 logger.resetAppender();
45         }
46         
47         @Test
48         public void testResponse() {
49                 
50                 RuntimeExceptionMapper mapper = new RuntimeExceptionMapper();
51                 
52                 Response r = mapper.toResponse(new RuntimeException("This is the run time exception message"));
53                 
54                 assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), r.getStatus());
55                 assertThat(r.getEntity(), instanceOf(ExceptionResponse.class));
56                 assertThat(((ExceptionResponse)r.getEntity()).getMessage(), equalTo("Unexpected Internal Exception"));
57
58         }
59         
60         @Test
61         public void preserve404ExceptionForForwarding() {
62                 
63                 RuntimeExceptionMapper mapper = new RuntimeExceptionMapper();
64                 
65                 Response r = mapper.toResponse(new NotFoundException("not found"));
66                 
67                 assertEquals(Status.NOT_FOUND.getStatusCode(), r.getStatus());
68                 assertThat(r.getEntity(), is(nullValue()));
69         }
70         
71 }