|
2 | 2 |
|
3 | 3 | import static org.assertj.core.api.Assertions.assertThat; |
4 | 4 | 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 | 5 |
|
9 | 6 | import java.nio.charset.StandardCharsets; |
10 | 7 |
|
11 | | -import org.apache.http.Header; |
12 | 8 | import org.apache.http.HttpResponse; |
13 | 9 | import org.apache.http.HttpStatus; |
14 | | -import org.apache.http.StatusLine; |
| 10 | +import org.apache.http.HttpVersion; |
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; |
20 | 16 | import com.sap.cloud.sdk.datamodel.odata.client.exception.ODataResponseException; |
21 | 17 | import com.sap.cloud.sdk.datamodel.odata.client.exception.ODataServiceErrorException; |
22 | 18 |
|
| 19 | +import lombok.Getter; |
| 20 | + |
23 | 21 | class ODataHealthyResponseValidatorTest |
24 | 22 | { |
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 | | - } |
| 23 | + private static final ODataRequestGeneric REQUEST = |
| 24 | + new ODataRequestRead("service-path", "EntitySet", null, ODataProtocol.V2); |
40 | 25 |
|
41 | 26 | @Test |
42 | 27 | void testSuccess() |
43 | 28 | { |
| 29 | + final ODataRequestResult odataResult = new ODataRequestResult() |
| 30 | + { |
| 31 | + @Getter |
| 32 | + private final ODataRequestGeneric oDataRequest = REQUEST; |
| 33 | + |
| 34 | + @Getter |
| 35 | + private final HttpResponse httpResponse = |
| 36 | + new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); |
| 37 | + }; |
| 38 | + |
44 | 39 | ODataHealthyResponseValidator.requireHealthyResponse(odataResult); |
45 | 40 | } |
46 | 41 |
|
47 | 42 | @Test |
48 | 43 | void testNotFound() |
49 | 44 | { |
50 | | - when(httpResponseStatusLine.getStatusCode()).thenReturn(HttpStatus.SC_NOT_FOUND); |
| 45 | + final ODataRequestResult odataResult = new ODataRequestResult() |
| 46 | + { |
| 47 | + @Getter |
| 48 | + private final ODataRequestGeneric oDataRequest = REQUEST; |
| 49 | + |
| 50 | + @Getter |
| 51 | + private final HttpResponse httpResponse = |
| 52 | + new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_NOT_FOUND, "Not Found"); |
| 53 | + }; |
51 | 54 |
|
52 | 55 | assertThatExceptionOfType(ODataResponseException.class) |
53 | 56 | .isThrownBy(() -> ODataHealthyResponseValidator.requireHealthyResponse(odataResult)) |
@@ -78,8 +81,23 @@ void testODataError() |
78 | 81 | } |
79 | 82 | """; |
80 | 83 |
|
81 | | - when(httpResponseStatusLine.getStatusCode()).thenReturn(HttpStatus.SC_INTERNAL_SERVER_ERROR); |
82 | | - when(httpResponse.getEntity()).thenReturn(new StringEntity(odata_error_json, StandardCharsets.UTF_8)); |
| 84 | + final ODataRequestResult odataResult = new ODataRequestResult() |
| 85 | + { |
| 86 | + @Getter |
| 87 | + private final ODataRequestGeneric oDataRequest = REQUEST; |
| 88 | + |
| 89 | + @Getter |
| 90 | + private final HttpResponse httpResponse = |
| 91 | + new BasicHttpResponse( |
| 92 | + HttpVersion.HTTP_1_1, |
| 93 | + HttpStatus.SC_INTERNAL_SERVER_ERROR, |
| 94 | + "Internal Server Error") |
| 95 | + { |
| 96 | + { |
| 97 | + setEntity(new StringEntity(odata_error_json, StandardCharsets.UTF_8)); |
| 98 | + } |
| 99 | + }; |
| 100 | + }; |
83 | 101 |
|
84 | 102 | assertThatExceptionOfType(ODataServiceErrorException.class) |
85 | 103 | .isThrownBy(() -> ODataHealthyResponseValidator.requireHealthyResponse(odataResult)) |
|
0 commit comments