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