DynamoDBMissionWorker implements Callable<Map<Integer, String>> { public static Map<Integer, String> getSolJSON(final URL url, final int connectTimeout) throws IOException { final Map<Integer, String> map = new HashMap<Integer, String>(); final JsonNode manifest = JSONParser.getJSONFromURL(url, connectTimeout); if (!manifest.has(RESOURCE_TYPE_KEY) || !SUPPORTED_TYPES.contains(manifest.get(RESOURCE_TYPE_KEY).asText())) { throw new IllegalArgumentException("Manifest version verification failed"); } if (!manifest.has(SOLS_LIST_KEY)) { throw new IllegalArgumentException("Manifest does not contain a sol list"); } final ArrayNode sols = (ArrayNode) manifest.get(SOLS_LIST_KEY); for (int i = 0; i < sols.size(); i++) { final JsonNode sol = sols.path(i); if (sol.has(SOL_ID_KEY) && sol.has(SOL_URL_KEY)) { final Integer solID = sol.get(SOL_ID_KEY).asInt(); final String solURL = sol.get(SOL_URL_KEY).asText(); if (solID != null && solURL != null) { map.put(solID, solURL); } else { LOGGER.warning("Sol contains unexpected values: " + sol); } } else { LOGGER.warning("Sol missing required keys: "); } } return map; } DynamoDBMissionWorker(final String manifestURL, final int connectTimeout); static Map<Integer, String> getSolJSON(final URL url, final int connectTimeout); @Override Map<Integer, String> call();  }
@Test public void testGetSolJSON() { final URL url = PowerMock.createMock(URL.class); PowerMock.mockStatic(JSONUtils.class); PowerMock.mockStatic(NetworkUtils.class); String manifest = null; try { manifest = WorkerTestUtils.readFile(IMAGE_RESOURCE_FILE); } catch (final IOException e1) { fail("Could not read file: " + IMAGE_RESOURCE_FILE); } try { NetworkUtils.getDataFromURL(url, null, ImageIngester.DEFAULT_CONNECT_TIMEOUT); PowerMock.expectLastCall().andReturn(manifest.getBytes()); PowerMock.replayAll(); final Map<Integer, String> actual = DynamoDBMissionWorker.getSolJSON(url, ImageIngester.DEFAULT_CONNECT_TIMEOUT); assertEquals(EXPECTED_MAP, actual); } catch (final IOException e) { fail(e.getMessage()); } }
@Test public void testInvalidVersion() { expectedEx.expect(IllegalArgumentException.class); expectedEx.expectMessage("version verification failed"); final URL url = PowerMock.createMock(URL.class); PowerMock.mockStatic(JSONUtils.class); PowerMock.mockStatic(NetworkUtils.class); String manifest = null; try { manifest = WorkerTestUtils.readFile(INVALID_VERSION_FILE); } catch (final IOException e) { fail("Could not read file: " + INVALID_VERSION_FILE); } try { NetworkUtils.getDataFromURL(url, null, ImageIngester.DEFAULT_CONNECT_TIMEOUT); PowerMock.expectLastCall().andReturn(manifest.getBytes()); PowerMock.replayAll(); DynamoDBMissionWorker.getSolJSON(url, ImageIngester.DEFAULT_CONNECT_TIMEOUT); } catch (final IOException e) { fail(e.getMessage()); } fail("Version is incorrect"); }
@Test public void testMissingSolID() { final Logger logger = Logger.getLogger(DynamoDBMissionWorker.class.getName()); Handler handler; final ByteArrayOutputStream os = new ByteArrayOutputStream(); final URL url = PowerMock.createMock(URL.class); PowerMock.mockStatic(JSONUtils.class); PowerMock.mockStatic(NetworkUtils.class); String manifest = null; try { manifest = WorkerTestUtils.readFile(MISSING_SOL_ID_FILE); } catch (final IOException e) { fail("Could not read file: " + MISSING_SOL_ID_FILE); } try { NetworkUtils.getDataFromURL(url, null, ImageIngester.DEFAULT_CONNECT_TIMEOUT); PowerMock.expectLastCall().andReturn(manifest.getBytes()); PowerMock.replayAll(); handler = new StreamHandler(os, new SimpleFormatter()); logger.setUseParentHandlers(false); logger.addHandler(handler); DynamoDBMissionWorker.getSolJSON(url, ImageIngester.DEFAULT_CONNECT_TIMEOUT); handler.flush(); assertTrue(os.toString().contains("WARNING: Sol missing required keys")); } catch (final IOException e) { fail(e.getMessage()); } finally { try { os.close(); } catch (final IOException e) { fail(e.getMessage()); } } }
@Test public void testMissingSolList() { expectedEx.expect(IllegalArgumentException.class); expectedEx.expectMessage("does not contain a sol list"); final URL url = PowerMock.createMock(URL.class); PowerMock.mockStatic(JSONUtils.class); PowerMock.mockStatic(NetworkUtils.class); String manifest = null; try { manifest = WorkerTestUtils.readFile(MISSING_SOL_LIST_FILE); } catch (final IOException e) { fail("Could not read file: " + MISSING_SOL_LIST_FILE); } try { NetworkUtils.getDataFromURL(url, null, ImageIngester.DEFAULT_CONNECT_TIMEOUT); PowerMock.expectLastCall().andReturn(manifest.getBytes()); PowerMock.replayAll(); DynamoDBMissionWorker.getSolJSON(url, ImageIngester.DEFAULT_CONNECT_TIMEOUT); } catch (final IOException e) { fail(e.getMessage()); } fail("Missing sol list"); }
@Test public void testMissingSolURL() { final Logger logger = Logger.getLogger(DynamoDBMissionWorker.class.getName()); Handler handler; final ByteArrayOutputStream os = new ByteArrayOutputStream(); final URL url = PowerMock.createMock(URL.class); PowerMock.mockStatic(JSONUtils.class); PowerMock.mockStatic(NetworkUtils.class); String manifest = null; try { manifest = WorkerTestUtils.readFile(MISSING_SOL_URL_FILE); } catch (final IOException e) { fail("Could not read file: " + MISSING_SOL_URL_FILE); } try { NetworkUtils.getDataFromURL(url, null, ImageIngester.DEFAULT_CONNECT_TIMEOUT); PowerMock.expectLastCall().andReturn(manifest.getBytes()); PowerMock.replayAll(); handler = new StreamHandler(os, new SimpleFormatter()); logger.setUseParentHandlers(false); logger.addHandler(handler); DynamoDBMissionWorker.getSolJSON(url, ImageIngester.DEFAULT_CONNECT_TIMEOUT); handler.flush(); assertTrue(os.toString().contains("WARNING: Sol missing required keys")); } catch (final IOException e) { fail(e.getMessage()); } finally { try { os.close(); } catch (final IOException e) { fail(e.getMessage()); } } }