Parser { public List<Data> getModels(final InputStream inputStream) throws ConnectException { try { final String json = CharStreams.toString(new InputStreamReader(inputStream, Charsets.UTF_8)); List<Data> data = Collections.EMPTY_LIST; if (json.length() > 0) { JSONArray jsonArray = new JSONObject(json).optJSONArray("data"); if (jsonArray != null && jsonArray.length() > 0) { data = parseArray(jsonArray); } } return data; } catch (Exception e) { throw new ConnectException(new ParserException(e)); } }  List<Data> getModels(final InputStream inputStream);  }
@Test public void shouldReturnData() throws ConnectException { assertEquals(Collections.EMPTY_LIST, mParser.getModels(new ByteArrayInputStream("".toString().getBytes(Charsets.UTF_8)))); assertEquals(Collections.EMPTY_LIST, mParser.getModels(new ByteArrayInputStream("{}".toString().getBytes(Charsets.UTF_8)))); assertEquals(Collections.EMPTY_LIST, mParser.getModels(new ByteArrayInputStream("{data:[]}".toString().getBytes(Charsets.UTF_8)))); }
@Test public void shouldThrowParserException() { try { mParser.getModels(new ByteArrayInputStream("{".toString().getBytes(Charsets.UTF_8))); Assert.fail("Expected exception."); } catch (ConnectException exception) { assertTrue(exception.getCause() instanceof ParserException); } }