-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Better logging when JSON cannot be parsed (#1106)
- Loading branch information
Showing
2 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.box.sdk; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.mockito.Mockito.doReturn; | ||
import static org.mockito.Mockito.doThrow; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import com.eclipsesource.json.ParseException; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.HttpURLConnection; | ||
import org.junit.Test; | ||
|
||
public class BoxJSONResponseTest { | ||
|
||
@Test | ||
public void whenJsonCannotBeParseJsonIsInException() throws IOException { | ||
HttpURLConnection mockedConnection = mock(HttpURLConnection.class); | ||
InputStream inputStream = new ByteArrayInputStream("Not a Json".getBytes(UTF_8)); | ||
doReturn(200).when(mockedConnection).getResponseCode(); | ||
doReturn(inputStream).when(mockedConnection).getInputStream(); | ||
try { | ||
new BoxJSONResponse(mockedConnection).getJSON(); | ||
} catch (Exception e) { | ||
assertThat(e.getMessage(), is("Error parsing JSON:\nNot a Json")); | ||
assertThat(e.getCause().getClass(), is(ParseException.class)); | ||
} | ||
} | ||
|
||
@Test | ||
public void whenOtherExceptionIsThrownItIsRethrown() throws IOException { | ||
HttpURLConnection mockedConnection = mock(HttpURLConnection.class); | ||
doReturn(200).when(mockedConnection).getResponseCode(); | ||
doThrow(new RuntimeException("Some random exception")).when(mockedConnection).getInputStream(); | ||
try { | ||
new BoxJSONResponse(mockedConnection).getJSON(); | ||
} catch (Exception e) { | ||
assertThat(e.getMessage(), is("Some random exception")); | ||
assertThat(e.getClass(), is(RuntimeException.class)); | ||
} | ||
} | ||
} |