MarketDataSnapshotListResource { @GET @Produces(MediaType.APPLICATION_JSON) public String getMarketDataSnapshotList() { MarketDataSnapshotSearchRequest snapshotSearchRequest = new MarketDataSnapshotSearchRequest(); snapshotSearchRequest.setIncludeData(false); Multimap<String, ManageableMarketDataSnapshot> snapshotsByBasisView = LinkedListMultimap.create(); for (MarketDataSnapshotDocument doc : MarketDataSnapshotSearchIterator.iterable(_snapshotMaster, snapshotSearchRequest)) { ManageableMarketDataSnapshot snapshot = doc.getSnapshot(); if (snapshot.getUniqueId() == null) { s_logger.warn("Ignoring snapshot with null unique identifier {}", snapshot.getName()); continue; } if (StringUtils.isBlank(snapshot.getName())) { s_logger.warn("Ignoring snapshot {} with no name", snapshot.getUniqueId()); continue; } if (s_guidPattern.matcher(snapshot.getName()).find()) { s_logger.debug("Ignoring snapshot which appears to have an auto-generated name: {}", snapshot.getName()); continue; } String basisViewName = snapshot.getBasisViewName() != null ? snapshot.getBasisViewName() : "unknown"; snapshotsByBasisView.put(basisViewName, snapshot); } List<Map<String, Object>> basisViewSnapshotList = new ArrayList<Map<String, Object>>(); for (String basisViewName : snapshotsByBasisView.keySet()) { Collection<ManageableMarketDataSnapshot> viewSnapshots = snapshotsByBasisView.get(basisViewName); List<Map<String, Object>> snapshotsList = new ArrayList<Map<String, Object>>(viewSnapshots.size()); for (ManageableMarketDataSnapshot viewSnapshot : viewSnapshots) { Map<String, Object> snapshotMap = ImmutableMap.<String, Object>of(ID, viewSnapshot.getUniqueId(), NAME, viewSnapshot.getName()); snapshotsList.add(snapshotMap); } basisViewSnapshotList.add(ImmutableMap.of(BASIS_VIEW_NAME, basisViewName, SNAPSHOTS, snapshotsList)); } return new JSONArray(basisViewSnapshotList).toString(); } MarketDataSnapshotListResource(MarketDataSnapshotMaster snapshotMaster); @GET @Produces(MediaType.APPLICATION_JSON) String getMarketDataSnapshotList(); @GET @Produces(MediaType.APPLICATION_JSON) @Path("{snapshotId}") String getMarketDataSnapshotHistory(@PathParam("snapshotId") String snapshotId);  }
@Test public void getJson() throws JSONException { MarketDataSnapshotListResource resource = createResource( createSnapshot("basisView1", "snap1", UniqueId.of("Tst", "1")), createSnapshot("basisView1", "snap2", UniqueId.of("Tst", "2")), createSnapshot("basisView2", "snap3", UniqueId.of("Tst", "3"))); String json = resource.getMarketDataSnapshotList(); JSONArray jsonArray = new JSONArray(json); String expected = "[{\"basisViewName\": \"basisView1\", \"snapshots\": [{\"id\": \"Tst~1\", \"name\": \"snap1\"}, {\"id\": \"Tst~2\", \"name\": \"snap2\"}]}, " + " {\"basisViewName\": \"basisView2\", \"snapshots\": [{\"id\": \"Tst~3\", \"name\": \"snap3\"}]}]"; JSONArray expectedArray = new JSONArray(expected); assertEquals(expectedArray.toString(), jsonArray.toString()); }
@Test public void snapshotWithBlankNameNotReturned() throws JSONException { MarketDataSnapshotListResource resource = createResource( createSnapshot("basisView", "", UniqueId.of("Tst", "2"))); JSONArray json = new JSONArray(resource.getMarketDataSnapshotList()); assertEquals(0, json.length()); }
@Test public void snapshotWithNullIdNotReturned() throws JSONException { MarketDataSnapshotListResource resource = createResource( createSnapshot("basisView", "snap", null)); JSONArray json = new JSONArray(resource.getMarketDataSnapshotList()); assertEquals(0, json.length()); }
@Test public void snapshotWithAutoGeneratedNameNotReturned() throws JSONException { MarketDataSnapshotListResource resource = createResource( createSnapshot("basisView", "{12345678-1234-abcd-cdef-0123456789ab}", UniqueId.of("Tst", "1")), createSnapshot("basisView", "12345678-1234-abcd-cdef-0123456789ab", UniqueId.of("Tst", "2"))); JSONArray json = new JSONArray(resource.getMarketDataSnapshotList()); assertEquals(0, json.length()); }