@Test public void representTest() throws Exception { final String query = ""; final String osId = "guid"; sut.setPathOverride("/resources/stream/guid/query/keywords/test"); final Request request = mockery.mock(Request.class); final Map<String, Object> attributes = new HashMap<String, Object>(); attributes.put("query", query); attributes.put("mode", "query"); attributes.put("openSocialId", osId); mockery.checking(new Expectations() { { allowing(request).getAttributes(); will(returnValue(attributes)); oneOf(principalDao).execute("guid"); will(returnValue(principal)); oneOf(serviceActionController).execute(with(any(ServiceActionContext.class)), with(equal(action))); will(returnValue(results)); } }); sut.initParams(request); Representation actual = sut.represent(null); JSONObject json = JSONObject.fromObject(actual.getText()); assertEquals("OK", json.getString("status")); JSONArray activityArray = json.getJSONArray("activities"); assertNotNull("Activity array is null and should not be", activityArray); assertEquals("Activity array should contain 1 item and does not", 1, activityArray.size()); assertEquals("actorDisplayName", activityArray.getJSONObject(0).getString("actorDisplayName")); assertEquals("actorUniqueId", activityArray.getJSONObject(0).getString("actorUniqueIdentifier")); assertEquals("PERSON", activityArray.getJSONObject(0).getString("actorType")); assertEquals("origActorDisplayName", activityArray.getJSONObject(0).getString("originalActorDisplayName")); assertEquals("origActorUniqueId", activityArray.getJSONObject(0).getString("originalActorUniqueIdentifier")); assertEquals("PERSON", activityArray.getJSONObject(0).getString("originalActorType")); assertEquals("SHARE", activityArray.getJSONObject(0).getString("verb")); assertEquals("NOTE", activityArray.getJSONObject(0).getString("baseObjectType")); assertEquals("my content", activityArray.getJSONObject(0).getString("content")); mockery.assertIsSatisfied(); }
@SuppressWarnings("unchecked") @Override public Representation represent(final Variant variant) throws ResourceException { if (log.isDebugEnabled()) { log.debug("Path: " + getPath()); } JSONObject responseJson = new JSONObject(); String status = GOOD_STATUS; try { JSONObject queryJson = null; if (mode.equals("query")) { int start = (null == callback) ? 5 : 7; queryJson = requestParser.parseRequest(getPath(), start); } else if (mode.equals("saved")) { Stream stream = streamMapper.execute(new FindByIdRequest("Stream", streamId)); if (stream == null) { throw new Exception("Unknown saved stream."); } queryJson = JSONObject.fromObject(stream.getRequest()); } else { throw new Exception("Unknown request mode."); } if (log.isDebugEnabled()) { log.debug("Making request using: " + queryJson); } PrincipalActionContext ac = new ServiceActionContext(queryJson.toString(), principalDao.execute(openSocialId)); PagedSet<ActivityDTO> activities = (PagedSet<ActivityDTO>) serviceActionController.execute( ac, action); responseJson.put("query", queryJson.getJSONObject("query")); DateFormatter dateFormatter = new DateFormatter(new Date()); JSONArray jsonActivities = new JSONArray(); for (ActivityDTO activity : activities.getPagedSet()) { AvatarUrlGenerator actorUrlGen = new AvatarUrlGenerator(EntityType.PERSON); JSONObject jsonActivity = new JSONObject(); jsonActivity.put("commentCount", activity.getCommentCount()); jsonActivity.put("likeCount", activity.getLikeCount()); jsonActivity.put("destinationDisplayName", activity.getDestinationStream().getDisplayName()); jsonActivity.put("destinationUniqueIdentifier", activity.getDestinationStream().getUniqueIdentifier()); jsonActivity.put("destinationType", activity.getDestinationStream().getType()); jsonActivity.put("actorAvatarPath", actorUrlGen.getSmallAvatarUrl(activity.getActor().getAvatarId())); jsonActivity.put("actorDisplayName", activity.getActor().getDisplayName()); jsonActivity.put("actorUniqueIdentifier", activity.getActor().getUniqueIdentifier()); jsonActivity.put("actorType", activity.getActor().getType()); jsonActivity.put("verb", activity.getVerb()); jsonActivity.put("postedTimeAgo", dateFormatter.timeAgo(activity.getPostedTime())); jsonActivity.put("baseObjectType", activity.getBaseObjectType().toString()); jsonActivity.put("activityId", activity.getId()); jsonActivity.put("originalActorAvatarPath", actorUrlGen.getSmallAvatarUrl(activity.getOriginalActor().getAvatarId())); jsonActivity.put("originalActorActivityId", activity.getOriginalActor().getAvatarId()); jsonActivity.put("originalActorDisplayName", activity.getOriginalActor().getDisplayName()); jsonActivity.put("originalActorUniqueIdentifier", activity.getOriginalActor().getUniqueIdentifier()); jsonActivity.put("originalActorType", activity.getOriginalActor().getType()); for (String key : activity.getBaseObjectProperties().keySet()) { jsonActivity.put(key, activity.getBaseObjectProperties().get(key)); } jsonActivities.add(jsonActivity); } responseJson.put("activities", jsonActivities); } catch (Exception ex) { status = "Error: " + ex.toString(); } log.debug(status); responseJson.put("status", status); String jsString = responseJson.toString(); if (null != callback) { jsString = callback + "(" + jsString + ")"; } Representation rep = new StringRepresentation(jsString, MediaType.TEXT_PLAIN); rep.setExpirationDate(new Date(0L)); return rep; }
StreamResource extends SmpResource { @SuppressWarnings("unchecked") @Override public Representation represent(final Variant variant) throws ResourceException { if (log.isDebugEnabled()) { log.debug("Path: " + getPath()); } JSONObject responseJson = new JSONObject(); String status = GOOD_STATUS; try { JSONObject queryJson = null; if (mode.equals("query")) { int start = (null == callback) ? 5 : 7; queryJson = requestParser.parseRequest(getPath(), start); } else if (mode.equals("saved")) { Stream stream = streamMapper.execute(new FindByIdRequest("Stream", streamId)); if (stream == null) { throw new Exception("Unknown saved stream."); } queryJson = JSONObject.fromObject(stream.getRequest()); } else { throw new Exception("Unknown request mode."); } if (log.isDebugEnabled()) { log.debug("Making request using: " + queryJson); } PrincipalActionContext ac = new ServiceActionContext(queryJson.toString(), principalDao.execute(openSocialId)); PagedSet<ActivityDTO> activities = (PagedSet<ActivityDTO>) serviceActionController.execute( ac, action); responseJson.put("query", queryJson.getJSONObject("query")); DateFormatter dateFormatter = new DateFormatter(new Date()); JSONArray jsonActivities = new JSONArray(); for (ActivityDTO activity : activities.getPagedSet()) { AvatarUrlGenerator actorUrlGen = new AvatarUrlGenerator(EntityType.PERSON); JSONObject jsonActivity = new JSONObject(); jsonActivity.put("commentCount", activity.getCommentCount()); jsonActivity.put("likeCount", activity.getLikeCount()); jsonActivity.put("destinationDisplayName", activity.getDestinationStream().getDisplayName()); jsonActivity.put("destinationUniqueIdentifier", activity.getDestinationStream().getUniqueIdentifier()); jsonActivity.put("destinationType", activity.getDestinationStream().getType()); jsonActivity.put("actorAvatarPath", actorUrlGen.getSmallAvatarUrl(activity.getActor().getAvatarId())); jsonActivity.put("actorDisplayName", activity.getActor().getDisplayName()); jsonActivity.put("actorUniqueIdentifier", activity.getActor().getUniqueIdentifier()); jsonActivity.put("actorType", activity.getActor().getType()); jsonActivity.put("verb", activity.getVerb()); jsonActivity.put("postedTimeAgo", dateFormatter.timeAgo(activity.getPostedTime())); jsonActivity.put("baseObjectType", activity.getBaseObjectType().toString()); jsonActivity.put("activityId", activity.getId()); jsonActivity.put("originalActorAvatarPath", actorUrlGen.getSmallAvatarUrl(activity.getOriginalActor().getAvatarId())); jsonActivity.put("originalActorActivityId", activity.getOriginalActor().getAvatarId()); jsonActivity.put("originalActorDisplayName", activity.getOriginalActor().getDisplayName()); jsonActivity.put("originalActorUniqueIdentifier", activity.getOriginalActor().getUniqueIdentifier()); jsonActivity.put("originalActorType", activity.getOriginalActor().getType()); for (String key : activity.getBaseObjectProperties().keySet()) { jsonActivity.put(key, activity.getBaseObjectProperties().get(key)); } jsonActivities.add(jsonActivity); } responseJson.put("activities", jsonActivities); } catch (Exception ex) { status = "Error: " + ex.toString(); } log.debug(status); responseJson.put("status", status); String jsString = responseJson.toString(); if (null != callback) { jsString = callback + "(" + jsString + ")"; } Representation rep = new StringRepresentation(jsString, MediaType.TEXT_PLAIN); rep.setExpirationDate(new Date(0L)); return rep; } }
StreamResource extends SmpResource { @SuppressWarnings("unchecked") @Override public Representation represent(final Variant variant) throws ResourceException { if (log.isDebugEnabled()) { log.debug("Path: " + getPath()); } JSONObject responseJson = new JSONObject(); String status = GOOD_STATUS; try { JSONObject queryJson = null; if (mode.equals("query")) { int start = (null == callback) ? 5 : 7; queryJson = requestParser.parseRequest(getPath(), start); } else if (mode.equals("saved")) { Stream stream = streamMapper.execute(new FindByIdRequest("Stream", streamId)); if (stream == null) { throw new Exception("Unknown saved stream."); } queryJson = JSONObject.fromObject(stream.getRequest()); } else { throw new Exception("Unknown request mode."); } if (log.isDebugEnabled()) { log.debug("Making request using: " + queryJson); } PrincipalActionContext ac = new ServiceActionContext(queryJson.toString(), principalDao.execute(openSocialId)); PagedSet<ActivityDTO> activities = (PagedSet<ActivityDTO>) serviceActionController.execute( ac, action); responseJson.put("query", queryJson.getJSONObject("query")); DateFormatter dateFormatter = new DateFormatter(new Date()); JSONArray jsonActivities = new JSONArray(); for (ActivityDTO activity : activities.getPagedSet()) { AvatarUrlGenerator actorUrlGen = new AvatarUrlGenerator(EntityType.PERSON); JSONObject jsonActivity = new JSONObject(); jsonActivity.put("commentCount", activity.getCommentCount()); jsonActivity.put("likeCount", activity.getLikeCount()); jsonActivity.put("destinationDisplayName", activity.getDestinationStream().getDisplayName()); jsonActivity.put("destinationUniqueIdentifier", activity.getDestinationStream().getUniqueIdentifier()); jsonActivity.put("destinationType", activity.getDestinationStream().getType()); jsonActivity.put("actorAvatarPath", actorUrlGen.getSmallAvatarUrl(activity.getActor().getAvatarId())); jsonActivity.put("actorDisplayName", activity.getActor().getDisplayName()); jsonActivity.put("actorUniqueIdentifier", activity.getActor().getUniqueIdentifier()); jsonActivity.put("actorType", activity.getActor().getType()); jsonActivity.put("verb", activity.getVerb()); jsonActivity.put("postedTimeAgo", dateFormatter.timeAgo(activity.getPostedTime())); jsonActivity.put("baseObjectType", activity.getBaseObjectType().toString()); jsonActivity.put("activityId", activity.getId()); jsonActivity.put("originalActorAvatarPath", actorUrlGen.getSmallAvatarUrl(activity.getOriginalActor().getAvatarId())); jsonActivity.put("originalActorActivityId", activity.getOriginalActor().getAvatarId()); jsonActivity.put("originalActorDisplayName", activity.getOriginalActor().getDisplayName()); jsonActivity.put("originalActorUniqueIdentifier", activity.getOriginalActor().getUniqueIdentifier()); jsonActivity.put("originalActorType", activity.getOriginalActor().getType()); for (String key : activity.getBaseObjectProperties().keySet()) { jsonActivity.put(key, activity.getBaseObjectProperties().get(key)); } jsonActivities.add(jsonActivity); } responseJson.put("activities", jsonActivities); } catch (Exception ex) { status = "Error: " + ex.toString(); } log.debug(status); responseJson.put("status", status); String jsString = responseJson.toString(); if (null != callback) { jsString = callback + "(" + jsString + ")"; } Representation rep = new StringRepresentation(jsString, MediaType.TEXT_PLAIN); rep.setExpirationDate(new Date(0L)); return rep; } StreamResource(final ServiceAction inAction, final ActionController inServiceActionController,
            final DomainMapper<String, Principal> inPrincipalDao, final FindByIdMapper<Stream> inStreamMapper,
            final RestletQueryRequestParser inRequestParser); }
StreamResource extends SmpResource { @SuppressWarnings("unchecked") @Override public Representation represent(final Variant variant) throws ResourceException { if (log.isDebugEnabled()) { log.debug("Path: " + getPath()); } JSONObject responseJson = new JSONObject(); String status = GOOD_STATUS; try { JSONObject queryJson = null; if (mode.equals("query")) { int start = (null == callback) ? 5 : 7; queryJson = requestParser.parseRequest(getPath(), start); } else if (mode.equals("saved")) { Stream stream = streamMapper.execute(new FindByIdRequest("Stream", streamId)); if (stream == null) { throw new Exception("Unknown saved stream."); } queryJson = JSONObject.fromObject(stream.getRequest()); } else { throw new Exception("Unknown request mode."); } if (log.isDebugEnabled()) { log.debug("Making request using: " + queryJson); } PrincipalActionContext ac = new ServiceActionContext(queryJson.toString(), principalDao.execute(openSocialId)); PagedSet<ActivityDTO> activities = (PagedSet<ActivityDTO>) serviceActionController.execute( ac, action); responseJson.put("query", queryJson.getJSONObject("query")); DateFormatter dateFormatter = new DateFormatter(new Date()); JSONArray jsonActivities = new JSONArray(); for (ActivityDTO activity : activities.getPagedSet()) { AvatarUrlGenerator actorUrlGen = new AvatarUrlGenerator(EntityType.PERSON); JSONObject jsonActivity = new JSONObject(); jsonActivity.put("commentCount", activity.getCommentCount()); jsonActivity.put("likeCount", activity.getLikeCount()); jsonActivity.put("destinationDisplayName", activity.getDestinationStream().getDisplayName()); jsonActivity.put("destinationUniqueIdentifier", activity.getDestinationStream().getUniqueIdentifier()); jsonActivity.put("destinationType", activity.getDestinationStream().getType()); jsonActivity.put("actorAvatarPath", actorUrlGen.getSmallAvatarUrl(activity.getActor().getAvatarId())); jsonActivity.put("actorDisplayName", activity.getActor().getDisplayName()); jsonActivity.put("actorUniqueIdentifier", activity.getActor().getUniqueIdentifier()); jsonActivity.put("actorType", activity.getActor().getType()); jsonActivity.put("verb", activity.getVerb()); jsonActivity.put("postedTimeAgo", dateFormatter.timeAgo(activity.getPostedTime())); jsonActivity.put("baseObjectType", activity.getBaseObjectType().toString()); jsonActivity.put("activityId", activity.getId()); jsonActivity.put("originalActorAvatarPath", actorUrlGen.getSmallAvatarUrl(activity.getOriginalActor().getAvatarId())); jsonActivity.put("originalActorActivityId", activity.getOriginalActor().getAvatarId()); jsonActivity.put("originalActorDisplayName", activity.getOriginalActor().getDisplayName()); jsonActivity.put("originalActorUniqueIdentifier", activity.getOriginalActor().getUniqueIdentifier()); jsonActivity.put("originalActorType", activity.getOriginalActor().getType()); for (String key : activity.getBaseObjectProperties().keySet()) { jsonActivity.put(key, activity.getBaseObjectProperties().get(key)); } jsonActivities.add(jsonActivity); } responseJson.put("activities", jsonActivities); } catch (Exception ex) { status = "Error: " + ex.toString(); } log.debug(status); responseJson.put("status", status); String jsString = responseJson.toString(); if (null != callback) { jsString = callback + "(" + jsString + ")"; } Representation rep = new StringRepresentation(jsString, MediaType.TEXT_PLAIN); rep.setExpirationDate(new Date(0L)); return rep; } StreamResource(final ServiceAction inAction, final ActionController inServiceActionController,
            final DomainMapper<String, Principal> inPrincipalDao, final FindByIdMapper<Stream> inStreamMapper,
            final RestletQueryRequestParser inRequestParser); @SuppressWarnings("unchecked") @Override Representation represent(final Variant variant); void setPathOverride(final String inPathOverride); String getPath(); }
StreamResource extends SmpResource { @SuppressWarnings("unchecked") @Override public Representation represent(final Variant variant) throws ResourceException { if (log.isDebugEnabled()) { log.debug("Path: " + getPath()); } JSONObject responseJson = new JSONObject(); String status = GOOD_STATUS; try { JSONObject queryJson = null; if (mode.equals("query")) { int start = (null == callback) ? 5 : 7; queryJson = requestParser.parseRequest(getPath(), start); } else if (mode.equals("saved")) { Stream stream = streamMapper.execute(new FindByIdRequest("Stream", streamId)); if (stream == null) { throw new Exception("Unknown saved stream."); } queryJson = JSONObject.fromObject(stream.getRequest()); } else { throw new Exception("Unknown request mode."); } if (log.isDebugEnabled()) { log.debug("Making request using: " + queryJson); } PrincipalActionContext ac = new ServiceActionContext(queryJson.toString(), principalDao.execute(openSocialId)); PagedSet<ActivityDTO> activities = (PagedSet<ActivityDTO>) serviceActionController.execute( ac, action); responseJson.put("query", queryJson.getJSONObject("query")); DateFormatter dateFormatter = new DateFormatter(new Date()); JSONArray jsonActivities = new JSONArray(); for (ActivityDTO activity : activities.getPagedSet()) { AvatarUrlGenerator actorUrlGen = new AvatarUrlGenerator(EntityType.PERSON); JSONObject jsonActivity = new JSONObject(); jsonActivity.put("commentCount", activity.getCommentCount()); jsonActivity.put("likeCount", activity.getLikeCount()); jsonActivity.put("destinationDisplayName", activity.getDestinationStream().getDisplayName()); jsonActivity.put("destinationUniqueIdentifier", activity.getDestinationStream().getUniqueIdentifier()); jsonActivity.put("destinationType", activity.getDestinationStream().getType()); jsonActivity.put("actorAvatarPath", actorUrlGen.getSmallAvatarUrl(activity.getActor().getAvatarId())); jsonActivity.put("actorDisplayName", activity.getActor().getDisplayName()); jsonActivity.put("actorUniqueIdentifier", activity.getActor().getUniqueIdentifier()); jsonActivity.put("actorType", activity.getActor().getType()); jsonActivity.put("verb", activity.getVerb()); jsonActivity.put("postedTimeAgo", dateFormatter.timeAgo(activity.getPostedTime())); jsonActivity.put("baseObjectType", activity.getBaseObjectType().toString()); jsonActivity.put("activityId", activity.getId()); jsonActivity.put("originalActorAvatarPath", actorUrlGen.getSmallAvatarUrl(activity.getOriginalActor().getAvatarId())); jsonActivity.put("originalActorActivityId", activity.getOriginalActor().getAvatarId()); jsonActivity.put("originalActorDisplayName", activity.getOriginalActor().getDisplayName()); jsonActivity.put("originalActorUniqueIdentifier", activity.getOriginalActor().getUniqueIdentifier()); jsonActivity.put("originalActorType", activity.getOriginalActor().getType()); for (String key : activity.getBaseObjectProperties().keySet()) { jsonActivity.put(key, activity.getBaseObjectProperties().get(key)); } jsonActivities.add(jsonActivity); } responseJson.put("activities", jsonActivities); } catch (Exception ex) { status = "Error: " + ex.toString(); } log.debug(status); responseJson.put("status", status); String jsString = responseJson.toString(); if (null != callback) { jsString = callback + "(" + jsString + ")"; } Representation rep = new StringRepresentation(jsString, MediaType.TEXT_PLAIN); rep.setExpirationDate(new Date(0L)); return rep; } StreamResource(final ServiceAction inAction, final ActionController inServiceActionController,
            final DomainMapper<String, Principal> inPrincipalDao, final FindByIdMapper<Stream> inStreamMapper,
            final RestletQueryRequestParser inRequestParser); @SuppressWarnings("unchecked") @Override Representation represent(final Variant variant); void setPathOverride(final String inPathOverride); String getPath();  }