SavedSnapshotController { @RequestMapping(method = RequestMethod.GET, value = "/{id}") public SavedSnapshot get(@PathVariable("id") final long id) { if(getValidIds().contains(id)) { return Optional.ofNullable(service.getDashboardSearch(id)) .orElseThrow(() -> new IllegalArgumentException("Configured ID " + id + " does not match any known Saved Snapshot")); } else { throw new IllegalArgumentException("Saved Snapshot ID " + id + " is not in the dashboards configuration file"); } } @SuppressWarnings("TypeMayBeWeakened") @Autowired  SavedSnapshotController(final SavedSearchService<SavedSnapshot, Builder> service,
                                   final ConfigService<DashboardConfig> dashboardConfigService); @RequestMapping(method = RequestMethod.GET) Set<SavedSnapshot> getAll(); @RequestMapping(value = GET_SHARED, method = RequestMethod.GET) Set<SavedSnapshot> getShared(); @RequestMapping(method = RequestMethod.POST) SavedSnapshot create(
            @RequestBody final SavedQuery query
    ); @RequestMapping(value = "{id}", method = RequestMethod.PUT) SavedSnapshot update(
            @PathVariable("id") final long id,
            @RequestBody final SavedSnapshot snapshot
    ); @RequestMapping(value = "shared/{id}", method = RequestMethod.PUT) SavedSnapshot updateShared(
            @PathVariable("id") final long id,
            @RequestBody final SavedSnapshot snapshot
    ); @RequestMapping(value = "{id}", method = RequestMethod.DELETE) void delete(
            @PathVariable("id") final long id
    ); @RequestMapping(method = RequestMethod.GET, value = "/{id}") SavedSnapshot get(@PathVariable("id") final long id);  }
@Test public void get() { when(dashboardConfig.getDashboards()).thenReturn(mockSnapshotDashboard); when(savedSnapshotService.getDashboardSearch(any(long.class))).thenReturn(savedSnapshot); savedSnapshotController.get(savedSnapshot.getId()); verify(savedSnapshotService).getDashboardSearch(savedSnapshot.getId()); }
@Test public void getWhenSearchDoesNotExist() throws Exception { when(dashboardConfig.getDashboards()).thenReturn(mockSnapshotDashboard); when(savedSnapshotService.getDashboardSearch(any(long.class))).thenReturn(null); try { savedSnapshotController.get(savedSnapshot.getId()); fail("Call to get() was expected to throw exception"); } catch (final IllegalArgumentException e) { verify(savedSnapshotService).getDashboardSearch(savedSnapshot.getId()); assertThat("Exception has the correct message", e.getMessage(), is("Configured ID " + savedSnapshot.getId() + " does not match any known Saved Snapshot")); } }