Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(core): Add unit test for ClientResponseHandler class #251

Merged
merged 3 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<properties>
<project.rootdir>${project.basedir}/../</project.rootdir>
<coverage.complexity>60%</coverage.complexity>
<coverage.line>69%</coverage.line>
<coverage.line>83%</coverage.line>
<coverage.instruction>68%</coverage.instruction>
<coverage.branch>52%</coverage.branch>
<coverage.method>73%</coverage.method>
newtork marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.sap.ai.sdk.core.commons;
newtork marked this conversation as resolved.
Show resolved Hide resolved

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.io.IOException;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.experimental.StandardException;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
import org.junit.jupiter.api.Test;

class ClientResponseHandlerTest {

static class MyResponse {}

@Data
static class MyError implements ClientError {
@JsonProperty("message")
private String message;
}

@StandardException
static class MyException extends ClientException {}

@Test
public void testParseErrorAndThrow() {
var sut = new ClientResponseHandler<>(MyResponse.class, MyError.class, MyException::new);

MyException cause = new MyException("Something wrong");

assertThatThrownBy(() -> sut.parseErrorAndThrow("{\"message\":\"foobar\"}", cause))
.isInstanceOf(MyException.class)
.hasMessage("Something wrong and error message: 'foobar'")
.hasCause(cause);

assertThatThrownBy(() -> sut.parseErrorAndThrow("{\"foo\":\"bar\"}", cause))
.isInstanceOf(MyException.class)
.hasMessage("Something wrong and error message: ''")
.hasCause(cause);

assertThatThrownBy(() -> sut.parseErrorAndThrow("<message>foobar</message>", cause))
.isInstanceOf(MyException.class)
newtork marked this conversation as resolved.
Show resolved Hide resolved
.hasMessage("Something wrong")
.hasNoCause();
}

@SneakyThrows
@Test
public void testBuildExceptionAndThrow() {
var sut = new ClientResponseHandler<>(MyResponse.class, MyError.class, MyException::new);

HttpEntity entityWithNetworkIssues = spy(new StringEntity(""));
doThrow(new IOException("Network issues")).when(entityWithNetworkIssues).writeTo(any());
doThrow(new IOException("Network issues")).when(entityWithNetworkIssues).getContent();

var response = spy(new BasicClassicHttpResponse(400, "Bad Request"));
when(response.getEntity())
.thenReturn(null)
.thenReturn(entityWithNetworkIssues)
.thenReturn(new StringEntity("", ContentType.APPLICATION_JSON))
.thenReturn(new StringEntity("<html>oh", ContentType.TEXT_HTML))
.thenReturn(new StringEntity("{\"message\":\"foobar\"}", ContentType.APPLICATION_JSON));

assertThatThrownBy(() -> sut.buildExceptionAndThrow(response))
.isInstanceOf(MyException.class)
.hasMessage("Request failed with status 400 Bad Request")
.hasNoCause();
assertThatThrownBy(() -> sut.buildExceptionAndThrow(response))
.isInstanceOf(MyException.class)
.hasMessage("Request failed with status 400 Bad Request")
.hasNoCause();
assertThatThrownBy(() -> sut.buildExceptionAndThrow(response))
.isInstanceOf(MyException.class)
.hasMessage("Request failed with status 400 Bad Request")
.hasNoCause();
assertThatThrownBy(() -> sut.buildExceptionAndThrow(response))
.isInstanceOf(MyException.class)
.hasMessage("Request failed with status 400 Bad Request")
.hasNoCause();
assertThatThrownBy(() -> sut.buildExceptionAndThrow(response))
.isInstanceOf(MyException.class)
.hasMessage("Request failed with status 400 Bad Request and error message: 'foobar'")
.hasCause(new MyException("Request failed with status 400 Bad Request"));
}
}
Loading