|
1 | 1 | package com.sap.cloud.sdk.datamodel.odata.client.request; |
2 | 2 |
|
| 3 | +import static org.apache.http.HttpVersion.HTTP_1_1; |
3 | 4 | import static org.assertj.core.api.Assertions.assertThat; |
4 | 5 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
5 | | -import static org.mockito.Mockito.lenient; |
6 | | -import static org.mockito.Mockito.mock; |
7 | | -import static org.mockito.Mockito.when; |
8 | 6 |
|
9 | 7 | import java.nio.charset.StandardCharsets; |
10 | 8 |
|
11 | | -import org.apache.http.Header; |
12 | 9 | import org.apache.http.HttpResponse; |
13 | 10 | import org.apache.http.HttpStatus; |
14 | | -import org.apache.http.StatusLine; |
15 | 11 | import org.apache.http.entity.StringEntity; |
16 | | -import org.junit.jupiter.api.BeforeEach; |
| 12 | +import org.apache.http.message.BasicHttpResponse; |
17 | 13 | import org.junit.jupiter.api.Test; |
18 | 14 |
|
19 | 15 | import com.sap.cloud.sdk.datamodel.odata.client.ODataProtocol; |
|
22 | 18 |
|
23 | 19 | class ODataHealthyResponseValidatorTest |
24 | 20 | { |
25 | | - private final ODataRequestResult odataResult = mock(ODataRequestResult.class); |
26 | | - private final ODataRequestGeneric odataRequest = mock(ODataRequestGeneric.class); |
27 | | - private final HttpResponse httpResponse = mock(HttpResponse.class); |
28 | | - private final StatusLine httpResponseStatusLine = mock(StatusLine.class); |
29 | | - |
30 | | - @BeforeEach |
31 | | - void adjustMocks() |
32 | | - { |
33 | | - lenient().when(odataRequest.getProtocol()).thenReturn(ODataProtocol.V2); |
34 | | - lenient().when(odataResult.getHttpResponse()).thenReturn(httpResponse); |
35 | | - lenient().when(odataResult.getODataRequest()).thenReturn(odataRequest); |
36 | | - lenient().when(httpResponse.getStatusLine()).thenReturn(httpResponseStatusLine); |
37 | | - lenient().when(httpResponse.getAllHeaders()).thenReturn(new Header[0]); |
38 | | - lenient().when(httpResponseStatusLine.getStatusCode()).thenReturn(HttpStatus.SC_OK); |
39 | | - } |
| 21 | + private static final ODataRequestGeneric REQUEST = |
| 22 | + new ODataRequestRead("service-path", "EntitySet", null, ODataProtocol.V2); |
40 | 23 |
|
41 | 24 | @Test |
42 | 25 | void testSuccess() |
43 | 26 | { |
| 27 | + final HttpResponse httpResponse = new BasicHttpResponse(HTTP_1_1, HttpStatus.SC_OK, "OK"); |
| 28 | + final ODataRequestResult odataResult = new ODataRequestResultGeneric(REQUEST, httpResponse); |
| 29 | + |
44 | 30 | ODataHealthyResponseValidator.requireHealthyResponse(odataResult); |
45 | 31 | } |
46 | 32 |
|
47 | 33 | @Test |
48 | 34 | void testNotFound() |
49 | 35 | { |
50 | | - when(httpResponseStatusLine.getStatusCode()).thenReturn(HttpStatus.SC_NOT_FOUND); |
| 36 | + final HttpResponse httpResponse = new BasicHttpResponse(HTTP_1_1, HttpStatus.SC_NOT_FOUND, "Not Found"); |
| 37 | + final ODataRequestResult odataResult = new ODataRequestResultGeneric(REQUEST, httpResponse); |
51 | 38 |
|
52 | 39 | assertThatExceptionOfType(ODataResponseException.class) |
53 | 40 | .isThrownBy(() -> ODataHealthyResponseValidator.requireHealthyResponse(odataResult)) |
@@ -78,8 +65,9 @@ void testODataError() |
78 | 65 | } |
79 | 66 | """; |
80 | 67 |
|
81 | | - when(httpResponseStatusLine.getStatusCode()).thenReturn(HttpStatus.SC_INTERNAL_SERVER_ERROR); |
82 | | - when(httpResponse.getEntity()).thenReturn(new StringEntity(odata_error_json, StandardCharsets.UTF_8)); |
| 68 | + final HttpResponse httpResponse = new BasicHttpResponse(HTTP_1_1, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Oh!"); |
| 69 | + httpResponse.setEntity(new StringEntity(odata_error_json, StandardCharsets.UTF_8)); |
| 70 | + final ODataRequestResult odataResult = new ODataRequestResultGeneric(REQUEST, httpResponse); |
83 | 71 |
|
84 | 72 | assertThatExceptionOfType(ODataServiceErrorException.class) |
85 | 73 | .isThrownBy(() -> ODataHealthyResponseValidator.requireHealthyResponse(odataResult)) |
|
0 commit comments