Skip to content

Commit 1e62f25

Browse files
authored
Merge branch 'main' into dependabot/maven/main/plugins-a6570b422e
2 parents 7ca6fd3 + 97bc3b5 commit 1e62f25

File tree

2 files changed

+97
-6
lines changed

2 files changed

+97
-6
lines changed

core/pom.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@
3030
</developers>
3131
<properties>
3232
<project.rootdir>${project.basedir}/../</project.rootdir>
33-
<coverage.complexity>52%</coverage.complexity>
34-
<coverage.line>64%</coverage.line>
35-
<coverage.instruction>62%</coverage.instruction>
36-
<coverage.branch>47%</coverage.branch>
37-
<coverage.method>65%</coverage.method>
38-
<coverage.class>80%</coverage.class>
33+
<coverage.complexity>62%</coverage.complexity>
34+
<coverage.line>76%</coverage.line>
35+
<coverage.instruction>76%</coverage.instruction>
36+
<coverage.branch>60%</coverage.branch>
37+
<coverage.method>74%</coverage.method>
38+
<coverage.class>90%</coverage.class>
3939
</properties>
4040

4141
<dependencies>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.sap.ai.sdk.core.common;
2+
3+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
4+
import static org.mockito.ArgumentMatchers.any;
5+
import static org.mockito.Mockito.doThrow;
6+
import static org.mockito.Mockito.spy;
7+
import static org.mockito.Mockito.when;
8+
9+
import com.fasterxml.jackson.annotation.JsonProperty;
10+
import java.io.IOException;
11+
import lombok.Data;
12+
import lombok.SneakyThrows;
13+
import lombok.experimental.StandardException;
14+
import org.apache.hc.core5.http.ContentType;
15+
import org.apache.hc.core5.http.HttpEntity;
16+
import org.apache.hc.core5.http.io.entity.StringEntity;
17+
import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
18+
import org.junit.jupiter.api.Test;
19+
20+
class ClientResponseHandlerTest {
21+
22+
static class MyResponse {}
23+
24+
@Data
25+
static class MyError implements ClientError {
26+
@JsonProperty("message")
27+
private String message;
28+
}
29+
30+
@StandardException
31+
static class MyException extends ClientException {}
32+
33+
@Test
34+
public void testParseErrorAndThrow() {
35+
var sut = new ClientResponseHandler<>(MyResponse.class, MyError.class, MyException::new);
36+
37+
MyException cause = new MyException("Something wrong");
38+
39+
assertThatThrownBy(() -> sut.parseErrorAndThrow("{\"message\":\"foobar\"}", cause))
40+
.isInstanceOf(MyException.class)
41+
.hasMessage("Something wrong and error message: 'foobar'")
42+
.hasCause(cause);
43+
44+
assertThatThrownBy(() -> sut.parseErrorAndThrow("{\"foo\":\"bar\"}", cause))
45+
.isInstanceOf(MyException.class)
46+
.hasMessage("Something wrong and error message: ''")
47+
.hasCause(cause);
48+
49+
assertThatThrownBy(() -> sut.parseErrorAndThrow("<message>foobar</message>", cause))
50+
.isEqualTo(cause);
51+
}
52+
53+
@SneakyThrows
54+
@Test
55+
public void testBuildExceptionAndThrow() {
56+
var sut = new ClientResponseHandler<>(MyResponse.class, MyError.class, MyException::new);
57+
58+
HttpEntity entityWithNetworkIssues = spy(new StringEntity(""));
59+
doThrow(new IOException("Network issues")).when(entityWithNetworkIssues).writeTo(any());
60+
doThrow(new IOException("Network issues")).when(entityWithNetworkIssues).getContent();
61+
62+
var response = spy(new BasicClassicHttpResponse(400, "Bad Request"));
63+
when(response.getEntity())
64+
.thenReturn(null)
65+
.thenReturn(entityWithNetworkIssues)
66+
.thenReturn(new StringEntity("", ContentType.APPLICATION_JSON))
67+
.thenReturn(new StringEntity("<html>oh", ContentType.TEXT_HTML))
68+
.thenReturn(new StringEntity("{\"message\":\"foobar\"}", ContentType.APPLICATION_JSON));
69+
70+
assertThatThrownBy(() -> sut.buildExceptionAndThrow(response))
71+
.isInstanceOf(MyException.class)
72+
.hasMessage("Request failed with status 400 Bad Request")
73+
.hasNoCause();
74+
assertThatThrownBy(() -> sut.buildExceptionAndThrow(response))
75+
.isInstanceOf(MyException.class)
76+
.hasMessage("Request failed with status 400 Bad Request")
77+
.hasNoCause();
78+
assertThatThrownBy(() -> sut.buildExceptionAndThrow(response))
79+
.isInstanceOf(MyException.class)
80+
.hasMessage("Request failed with status 400 Bad Request")
81+
.hasNoCause();
82+
assertThatThrownBy(() -> sut.buildExceptionAndThrow(response))
83+
.isInstanceOf(MyException.class)
84+
.hasMessage("Request failed with status 400 Bad Request")
85+
.hasNoCause();
86+
assertThatThrownBy(() -> sut.buildExceptionAndThrow(response))
87+
.isInstanceOf(MyException.class)
88+
.hasMessage("Request failed with status 400 Bad Request and error message: 'foobar'")
89+
.hasCause(new MyException("Request failed with status 400 Bad Request"));
90+
}
91+
}

0 commit comments

Comments
 (0)