fixup! Add a null check before reading GitHub response

pull/563/head
Igor Vinokur 2023-09-13 14:43:19 +00:00
parent 68d18e4e40
commit c3b3d1ffc5
1 changed files with 23 additions and 0 deletions

View File

@ -21,6 +21,8 @@ import static java.net.HttpURLConnection.HTTP_BAD_GATEWAY;
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertEqualsNoOrder;
import static org.testng.Assert.assertFalse;
@ -33,10 +35,13 @@ import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.common.Slf4jNotifier;
import com.google.common.net.HttpHeaders;
import java.lang.reflect.Field;
import java.net.http.HttpClient;
import java.net.http.HttpResponse;
import org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException;
import org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException;
import org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException;
import org.eclipse.che.commons.lang.Pair;
import org.mockito.Mockito;
import org.mockito.testng.MockitoTestNGListener;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
@ -260,6 +265,24 @@ public class GithubApiClientTest {
client.getUser("token");
}
@Test(
expectedExceptions = ScmBadRequestException.class,
expectedExceptionsMessageRegExp = "Unrecognised error")
public void shouldThrowExceptionWithUnrecognisedErrorIfResponseBodyIsNull() throws Exception {
// given
HttpClient httpClient = Mockito.mock(HttpClient.class);
HttpResponse response = Mockito.mock(HttpResponse.class);
Field declaredField = GithubApiClient.class.getDeclaredField("httpClient");
declaredField.setAccessible(true);
declaredField.set(client, httpClient);
when(httpClient.send(any(), any())).thenReturn(response);
when(response.body()).thenReturn(null);
when(response.statusCode()).thenReturn(HTTP_BAD_REQUEST);
// when
client.getUser("token");
}
@Test(
expectedExceptions = ScmCommunicationException.class,
expectedExceptionsMessageRegExp =