GetViewsHandler extends ActionHandler { public void handleAction(final ActionParameters params) throws ActionException { params.requireLoggedInUser(); final long userId = params.getUser().getId(); final String type = params.getHttpParam(ViewTypes.VIEW_TYPE, ViewTypes.USER); final List<View> views = viewService.getViewsForUser(userId); for (View view : views) { if (view.getType() == null) { view.setType(ViewTypes.USER); } } final List<JSONObject> viewsAsJsonObjects = views.stream() .filter(v -> type.equalsIgnoreCase(v.getType())) .map(v -> viewToJSONObject(v)) .filter(Optional::isPresent) .map(Optional::get) .collect(Collectors.toList()); final JSONArray viewArray = new JSONArray(viewsAsJsonObjects); final JSONObject ret = JSONHelper.createJSONObject(KEY_VIEWS, viewArray); ResponseHelper.writeResponse(params, ret); }  void setViewService(final ViewService service); void init(); void handleAction(final ActionParameters params); static final String KEY_DESCRIPTION; static final String KEY_ISPUBLIC; static final String KEY_ISDEFAULT; static final String KEY_PUBDOMAIN; static final String KEY_VIEWS; static final String KEY_METADATA; static final String KEY_SRSNAME; }
@Test(expected = ActionDeniedException.class) public void tesGetWithGuest() throws Exception { doReturn(Collections.emptyList()).when(viewService).getViewsForUser(anyLong()); Map<String, String> parameters = new HashMap<String, String>(); parameters.put(ViewTypes.VIEW_TYPE, ViewTypes.USER); final ActionParameters params = createActionParams(parameters); assertEquals("Parameter is set correctly", ViewTypes.USER, params.getHttpParam(ViewTypes.VIEW_TYPE)); verifyResponseNotWritten(params); handler.handleAction(params); }
@Test public void testEmptyViews() throws Exception { doReturn(Collections.emptyList()).when(viewService).getViewsForUser(anyLong()); Map<String, String> parameters = new HashMap<String, String>(); parameters.put(ViewTypes.VIEW_TYPE, ViewTypes.USER); final ActionParameters params = createActionParams(parameters,getLoggedInUser()); assertEquals("Parameter is set correctly", ViewTypes.USER, params.getHttpParam(ViewTypes.VIEW_TYPE)); verifyResponseNotWritten(params); handler.handleAction(params); verifyResponseWritten(params); verifyResponseContent(ResourceHelper.readJSONResource("GetViewsHandlerTest-empty-views.json", this)); }
@Test(expected = ActionDeniedException.class) public void testWithNoParamsGuest() throws Exception { doReturn(Collections.emptyList()).when(viewService).getViewsForUser(anyLong()); final ActionParameters params = createActionParams(); verifyResponseNotWritten(params); handler.handleAction(params); }
@Test public void testWithNoParams() throws Exception { doReturn(Collections.emptyList()).when(viewService).getViewsForUser(anyLong()); final ActionParameters params = createActionParams(getLoggedInUser()); verifyResponseNotWritten(params); handler.handleAction(params); verifyResponseWritten(params); verifyResponseContent(ResourceHelper.readJSONResource("GetViewsHandlerTest-empty-views.json", this)); }
@Test(expected = ActionDeniedException.class) public void testViewListingGuest() throws Exception { final List<View> views = new ArrayList<View>(); views.add(ViewTestHelper.createMockView("framework.mapfull")); doReturn(views).when(viewService).getViewsForUser(anyLong()); Map<String, String> parameters = new HashMap<String, String>(); parameters.put(ViewTypes.VIEW_TYPE, ViewTypes.USER); final ActionParameters params = createActionParams(parameters); assertEquals("Parameter is set correctly", ViewTypes.USER, params.getHttpParam(ViewTypes.VIEW_TYPE)); verifyResponseNotWritten(params); handler.handleAction(params); }
@Test public void testViewListing() throws Exception { final List<View> views = new ArrayList<View>(); views.add(ViewTestHelper.createMockView("framework.mapfull")); doReturn(views).when(viewService).getViewsForUser(anyLong()); Map<String, String> parameters = new HashMap<String, String>(); parameters.put(ViewTypes.VIEW_TYPE, ViewTypes.USER); final ActionParameters params = createActionParams(parameters,getLoggedInUser()); assertEquals("Parameter is set correctly", ViewTypes.USER, params.getHttpParam(ViewTypes.VIEW_TYPE)); verifyResponseNotWritten(params); handler.handleAction(params); verifyResponseWritten(params); verifyResponseContent(ResourceHelper.readJSONResource("GetViewsHandlerTest-has-views.json", this)); }
@Test(expected = ActionDeniedException.class) public void testViewListingWithNoParamsGuest() throws Exception { final List<View> views = new ArrayList<View>(); views.add(ViewTestHelper.createMockView("framework.mapfull")); doReturn(views).when(viewService).getViewsForUser(anyLong()); final ActionParameters params = createActionParams(); verifyResponseNotWritten(params); handler.handleAction(params); }
@Test public void testViewListingWithNoParams() throws Exception { final List<View> views = new ArrayList<View>(); views.add(ViewTestHelper.createMockView("framework.mapfull")); doReturn(views).when(viewService).getViewsForUser(anyLong()); final ActionParameters params = createActionParams(getLoggedInUser()); verifyResponseNotWritten(params); handler.handleAction(params); verifyResponseWritten(params); verifyResponseContent(ResourceHelper.readJSONResource("GetViewsHandlerTest-has-views.json", this)); }