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