JSONParser { public static JsonNode getJSONFromURL(final URL url, final int connectTimeout) throws IOException { return getJSONFromURL(url, null, connectTimeout); } private  JSONParser(); static JsonNode getJSONFromURL(final URL url, final int connectTimeout); static JsonNode getJSONFromURL(final URL url, final String expectedETag, final int connectTimeout); static final ObjectMapper MAPPER; }
@Test(expected = IOException.class) public void testBadURL() throws IOException { final URL url = PowerMock.createMock(URL.class); url.openConnection(); PowerMock.expectLastCall().andThrow(new IOException()); replayAll(); JSONParser.getJSONFromURL(url, ImageIngester.DEFAULT_CONNECT_TIMEOUT); }
@Test public void testgetJSONFromURL() { PowerMock.mockStatic(NetworkUtils.class); try { final URL url = PowerMock.createMock(URL.class); NetworkUtils.getDataFromURL(url, null, ImageIngester.DEFAULT_CONNECT_TIMEOUT); PowerMock.expectLastCall().andReturn(ROOT_JSON.getBytes()); replayAll(); final ObjectMapper mapper = new ObjectMapper(); final JsonNode json = mapper.readTree(ROOT_JSON); final JsonNode result = JSONParser.getJSONFromURL(url, ImageIngester.DEFAULT_CONNECT_TIMEOUT); assertEquals(json, result); } catch (final IOException e) { fail(); } }
@Test(expected = IOException.class) public void testInvalidURL() throws IOException { JSONParser.getJSONFromURL(new URL("invalidURL"), ImageIngester.DEFAULT_CONNECT_TIMEOUT); fail("URL should be invalid"); }
@Test(expected = IOException.class) public void testNotJSON() throws IOException { PowerMock.mockStatic(NetworkUtils.class); final String notJSON = "I am not json"; final URL url = PowerMock.createMock(URL.class); InputStream is = null; try { is = new ByteArrayInputStream(notJSON.getBytes()); NetworkUtils.getDataFromURL(url, null, ImageIngester.DEFAULT_CONNECT_TIMEOUT); PowerMock.expectLastCall().andReturn(notJSON.getBytes()); replayAll(); JSONParser.getJSONFromURL(url, null, ImageIngester.DEFAULT_CONNECT_TIMEOUT); } finally { if (is != null) { is.close(); } } fail("Source is invalid json"); }
@Test(expected = NullPointerException.class) public void testNull() { try { JSONParser.getJSONFromURL(null, ImageIngester.DEFAULT_CONNECT_TIMEOUT); } catch (final IOException e) { fail(e.getMessage()); } }
@Test(expected = NullPointerException.class) public void testNullURL() { try { JSONParser.getJSONFromURL(null, null, ImageIngester.DEFAULT_CONNECT_TIMEOUT); } catch (final IOException e) { fail("URL is null"); } }