private List<String> processFields(BaseRequestItem request, List<String> defaultList) { List<String> value = request.getListParameter(BaseRequestItem.FIELDS); return ((value == null || value.size() == 0) ? defaultList : value); } }
public Set<String> getFields() { return getFields(Collections.<String>emptySet()); }
public String getParameter(String paramName, String defaultValue) { String param = getParameter(paramName); if (param == null) { return defaultValue; } return param; }
@Test public void testParseCommaSeparatedList() throws Exception { request.setParameter("fields", "huey,dewey,louie"); assertEquals(Lists.newArrayList("huey", "dewey", "louie"), request.getListParameter("fields")); }
@Test public void testFields() throws Exception { request.setParameter("fields", ""); assertEquals(Sets.<String>newHashSet(), request.getFields()); request.setParameter("fields", "happy,sad,grumpy"); assertEquals(Sets.newHashSet("happy", "sad", "grumpy"), request.getFields()); }
@Test public void testJSONConstructor() throws Exception { request = new BaseRequestItem(new JSONObject('{' + "userId:john.doe," + "groupId:@self," + "fields:[huey,dewey,louie]" + '}'), null, FAKE_TOKEN, converter, converter); assertEquals(Lists.newArrayList("huey", "dewey", "louie"), request.getListParameter("fields")); }
@Test public void testGetAppId() throws Exception { request.setParameter("appId", "100"); assertEquals("100", request.getAppId()); request.setParameter("appId", "@app"); assertEquals(FAKE_TOKEN.getAppId(), request.getAppId()); }
@Test public void testSortOrder() throws Exception { request.setParameter("sortOrder", null); assertEquals(SortOrder.ascending, request.getSortOrder()); request.setParameter("sortOrder", "descending"); assertEquals(SortOrder.descending, request.getSortOrder()); }
@Test public void testCount() throws Exception { request.setParameter("count", null); assertEquals(RequestItem.DEFAULT_COUNT, request.getCount()); request.setParameter("count", "5"); assertEquals(5, request.getCount()); }
public Map<String, GadgetsHandlerApi.BaseResponse> execute(BaseRequestItem request) { Set<String> gadgetUrls = ImmutableSet.copyOf(request.getListParameter(Param.IDS.getName())); if (gadgetUrls.isEmpty()) { return ImmutableMap.of(); if (Strings.isNullOrEmpty(request.getParameter(Param.CONTAINER.getName()))) { throw new ProtocolException(HttpServletResponse.SC_BAD_REQUEST, "Missing container for request.");
@Operation(httpMethods = {"POST","GET"}, path = "/invalidate") public void invalidate(BaseRequestItem request) { if (StringUtils.isEmpty(request.getToken().getAppId()) && StringUtils.isEmpty(request.getToken().getAppUrl())) { throw new ProtocolException(HttpServletResponse.SC_BAD_REQUEST, "Cannot invalidate content without specifying application"); request.getToken().getAuthenticationMode()); List<String> keys = request.getListParameter(KEYS_PARAM); Set<String> userIds = Sets.newHashSet(); Set<Uri> resources = Sets.newHashSet(); if (!StringUtils.isEmpty(request.getToken().getViewerId())) { userIds.add(request.getToken().getViewerId()); if (!isBackendInvalidation && !key.equals(request.getToken().getViewerId())) { throw new ProtocolException(HttpServletResponse.SC_BAD_REQUEST, "Cannot invalidate the content for a user other than the viewer from a gadget."); invalidation.invalidateApplicationResources(resources, request.getToken()); invalidation.invalidateUserResources(userIds, request.getToken());
@Test public void testStartIndex() throws Exception { request.setParameter("startIndex", null); assertEquals(RequestItem.DEFAULT_START_INDEX, request.getStartIndex()); request.setParameter("startIndex", "5"); assertEquals(5, request.getStartIndex()); }
@Test public void testAttributes() throws Exception { assertNull(request.getAttribute("undefined")); request.setAttribute("test", "value"); assertEquals("value", request.getAttribute("test")); request.setAttribute("test", null); assertNull(request.getAttribute("undefined")); }
@Test public void testGetInvalidJsonTypedParameter() throws Exception { request.setParameter("anykey", "{name: 'Bob"); int code = 0; try { request.getTypedParameter("anykey", InputData.class); } catch(ProtocolException e) { code = e.getCode(); } assertEquals(HttpServletResponse.SC_BAD_REQUEST, code); }
public void setAttribute(String val, Object obj) { Preconditions.checkNotNull(val); if (obj == null) { getAttributeMap().remove(val); } else { getAttributeMap().put(val, obj); } }
@Before public void setUp() throws Exception { FAKE_TOKEN.setAppId("12345"); FAKE_TOKEN.setOwnerId("someowner"); FAKE_TOKEN.setViewerId("someowner"); converter = new BeanJsonConverter(Guice.createInjector()); request = new BaseRequestItem( Maps.<String,String[]>newHashMap(), FAKE_TOKEN, converter, converter); }
@Test public void testParseCommaSeparatedList() throws Exception { request.setParameter("fields", "huey,dewey,louie"); assertEquals(Lists.newArrayList("huey", "dewey", "louie"), request.getListParameter("fields")); }
@Test public void testFields() throws Exception { request.setParameter("fields", ""); assertEquals(Sets.<String>newHashSet(), request.getFields()); request.setParameter("fields", "happy,sad,grumpy"); assertEquals(Sets.newHashSet("happy", "sad", "grumpy"), request.getFields()); }
@Test public void testJSONConstructor() throws Exception { request = new BaseRequestItem(new JSONObject('{' + "userId:john.doe," + "groupId:@self," + "fields:[huey,dewey,louie]" + '}'), null, FAKE_TOKEN, converter, converter); assertEquals(Lists.newArrayList("huey", "dewey", "louie"), request.getListParameter("fields")); }
@Test public void testGetAppId() throws Exception { request.setParameter("appId", "100"); assertEquals("100", request.getAppId()); request.setParameter("appId", "@app"); assertEquals(FAKE_TOKEN.getAppId(), request.getAppId()); }