Springboot 2.0 upgrade
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / onap / so / client / ResponseExceptionMapperImplTest.java
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.client;
22
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
25
26 import java.io.ByteArrayInputStream;
27 import java.io.UnsupportedEncodingException;
28 import java.nio.charset.StandardCharsets;
29
30 import javax.ws.rs.BadRequestException;
31 import javax.ws.rs.ForbiddenException;
32 import javax.ws.rs.InternalServerErrorException;
33 import javax.ws.rs.NotAcceptableException;
34 import javax.ws.rs.NotAllowedException;
35 import javax.ws.rs.NotAuthorizedException;
36 import javax.ws.rs.NotFoundException;
37 import javax.ws.rs.NotSupportedException;
38 import javax.ws.rs.WebApplicationException;
39 import javax.ws.rs.core.Response;
40 import javax.ws.rs.core.Response.Status;
41
42 import org.junit.Rule;
43 import org.junit.Test;
44 import org.junit.rules.ExpectedException;
45 import org.junit.runner.RunWith;
46
47 import junitparams.JUnitParamsRunner;
48 import junitparams.Parameters;
49
50 @RunWith(JUnitParamsRunner.class)
51 public class ResponseExceptionMapperImplTest{
52
53         private static final ResponseExceptionMapperImpl mapper = new ResponseExceptionMapperImpl();
54
55         public static Object[][] statusesAndCorrespondingExceptions() {
56                 return new Object[][]{
57                         {Status.BAD_REQUEST, BadRequestException.class},
58                         {Status.UNAUTHORIZED, NotAuthorizedException.class},
59                         {Status.FORBIDDEN, ForbiddenException.class},
60                         {Status.NOT_FOUND, NotFoundException.class},
61                         {Status.METHOD_NOT_ALLOWED, NotAllowedException.class},
62                         {Status.NOT_ACCEPTABLE, NotAcceptableException.class},
63                         {Status.PRECONDITION_FAILED, PreconditionFailedException.class},
64                         {Status.UNSUPPORTED_MEDIA_TYPE, NotSupportedException.class},
65                         {Status.INTERNAL_SERVER_ERROR, InternalServerErrorException.class},
66                         {Status.SERVICE_UNAVAILABLE, WebApplicationException.class},
67                         {Status.BAD_GATEWAY, WebApplicationException.class},
68                 };
69         }
70
71         @Rule
72         public ExpectedException expectedExceptionTest = ExpectedException.none();
73
74         @Test
75         @Parameters(method = "statusesAndCorrespondingExceptions")
76         public void shouldThrowExceptionWhenStatusIsNotOk(Status status, Class<Exception> expectedException) {
77                 // given
78                 Response response = createMockResponse(status);
79                 // when, then
80                 expectedExceptionTest.expect(expectedException);
81                 mapper.map(response);
82         }  
83
84         @Test
85         public void shouldNotThrowExceptionWhenStatusIsOk() {
86                 // given
87                 Response response = createMockResponse(Status.OK);
88                 // when, then
89                 expectedExceptionTest.none();
90                 mapper.map(response);      
91         }
92         
93     @Test
94     public void shouldThrowExceptionWithCustomMessageWhenResponseHasEntity() throws UnsupportedEncodingException {
95         // given
96         Response response = createMockResponse(Status.BAD_REQUEST);
97         when(response.hasEntity()).thenReturn(true);
98         when(response.getEntity()).thenReturn(new ByteArrayInputStream("test message".getBytes(StandardCharsets.UTF_8)));
99         
100         expectedExceptionTest.expect(BadRequestException.class);
101         expectedExceptionTest.expectMessage("test message");
102         mapper.map(response);      
103     }
104
105     @Test
106     public void shouldThrowExceptionWithDefaultMessageWhenResponseHasNoEntity() {
107         // given
108         Response response = createMockResponse(Status.BAD_REQUEST);
109         when(response.hasEntity()).thenReturn(false);
110         // when, then
111         expectedExceptionTest.expect(BadRequestException.class);
112         expectedExceptionTest.expectMessage("");
113         mapper.map(response);      
114     }
115         
116         private static Response createMockResponse(Status status) {
117                 Response responseContext = mock(Response.class);
118                 when(responseContext.getStatusInfo()).thenReturn(status);
119                 when(responseContext.getStatus()).thenReturn(status.getStatusCode());
120                 return responseContext;
121         }
122 }