Skip to content

Commit acd5627

Browse files
committed
Initial
1 parent e173db5 commit acd5627

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<properties>
3232
<project.rootdir>${project.basedir}/../</project.rootdir>
3333
<coverage.complexity>60%</coverage.complexity>
34-
<coverage.line>69%</coverage.line>
34+
<coverage.line>83%</coverage.line>
3535
<coverage.instruction>68%</coverage.instruction>
3636
<coverage.branch>52%</coverage.branch>
3737
<coverage.method>73%</coverage.method>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.sap.ai.sdk.core.commons;
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+
.isInstanceOf(MyException.class)
51+
.hasMessage("Something wrong")
52+
.hasNoCause();
53+
}
54+
55+
@SneakyThrows
56+
@Test
57+
public void testBuildExceptionAndThrow() {
58+
var sut = new ClientResponseHandler<>(MyResponse.class, MyError.class, MyException::new);
59+
60+
HttpEntity entityWithNetworkIssues = spy(new StringEntity(""));
61+
doThrow(new IOException("Network issues")).when(entityWithNetworkIssues).writeTo(any());
62+
doThrow(new IOException("Network issues")).when(entityWithNetworkIssues).getContent();
63+
64+
var response = spy(new BasicClassicHttpResponse(400, "Bad Request"));
65+
when(response.getEntity())
66+
.thenReturn(null)
67+
.thenReturn(entityWithNetworkIssues)
68+
.thenReturn(new StringEntity("", ContentType.APPLICATION_JSON))
69+
.thenReturn(new StringEntity("<html>oh", ContentType.TEXT_HTML))
70+
.thenReturn(new StringEntity("{\"message\":\"foobar\"}", ContentType.APPLICATION_JSON));
71+
72+
assertThatThrownBy(() -> sut.buildExceptionAndThrow(response))
73+
.isInstanceOf(MyException.class)
74+
.hasMessage("Request failed with status 400 Bad Request")
75+
.hasNoCause();
76+
assertThatThrownBy(() -> sut.buildExceptionAndThrow(response))
77+
.isInstanceOf(MyException.class)
78+
.hasMessage("Request failed with status 400 Bad Request")
79+
.hasNoCause();
80+
assertThatThrownBy(() -> sut.buildExceptionAndThrow(response))
81+
.isInstanceOf(MyException.class)
82+
.hasMessage("Request failed with status 400 Bad Request")
83+
.hasNoCause();
84+
assertThatThrownBy(() -> sut.buildExceptionAndThrow(response))
85+
.isInstanceOf(MyException.class)
86+
.hasMessage("Request failed with status 400 Bad Request")
87+
.hasNoCause();
88+
assertThatThrownBy(() -> sut.buildExceptionAndThrow(response))
89+
.isInstanceOf(MyException.class)
90+
.hasMessage("Request failed with status 400 Bad Request and error message: 'foobar'")
91+
.hasCause(new MyException("Request failed with status 400 Bad Request"));
92+
}
93+
}

0 commit comments

Comments
 (0)