@Test public void represent() throws ResourceException, IOException { Variant variant = context.mock(Variant.class); final List<Recommendation> recos = buildRecommendationList(); final List<String> openSocialIds = new ArrayList<String>(); openSocialIds.add(SUBJECT_OPENSOCIAL_ID); openSocialIds.add(AUTHOR_OPENSOCIAL_ID); openSocialIds.add(AUTHOR_OPENSOCIAL_ID); openSocialIds.add(AUTHOR_OPENSOCIAL_ID); context.checking(new Expectations() { { oneOf(recoMapper).findBySubjectOpenSocialId(SUBJECT_OPENSOCIAL_ID, 5); will(returnValue(recos)); allowing(personMapper).findPeopleByOpenSocialIds(openSocialIds); will(returnValue(people)); } }); Representation actual = sut.represent(variant); assertEquals(MediaType.APPLICATION_JSON, actual.getMediaType()); JSONObject json = JSONObject.fromObject(actual.getText()); JSONArray recommendations = json.getJSONArray(RecommendationsCollectionResource.RECOMMENDATIONS_KEY); assertEquals(recos.size(), recommendations.size()); context.assertIsSatisfied(); }
@Override public Representation represent(final Variant variant) throws ResourceException { log.debug("Getting recommendations for user with OS ID: " + openSocialId); List<Recommendation> recommendations; if (maxResults > 0) { recommendations = getRecommendationMapper().findBySubjectOpenSocialId(openSocialId, maxResults); } else { recommendations = getRecommendationMapper().findBySubjectOpenSocialId(openSocialId); } log.debug(recommendations.size() + " recommendations found"); JSONObject json = new JSONObject(); JSONArray recos = new JSONArray(); if (null != recommendations && recommendations.size() > 0) { Map<String, Person> people = getPeopleInfoForRecommendations(recommendations); log.debug("There are " + people.size() + " Person objects available for recommendations."); for (Recommendation reco : recommendations) { recos.add(convertRecoToJSON(reco, people.get(reco.getAuthorOpenSocialId()), people.get(reco.getSubjectOpenSocialId()))); } } json.put(RECOMMENDATIONS_KEY, recos); Representation rep = new StringRepresentation(json.toString(), MediaType.APPLICATION_JSON); rep.setExpirationDate(new Date(0L)); return rep; }
RecommendationsCollectionResource extends RecommendationsResource { @Override public Representation represent(final Variant variant) throws ResourceException { log.debug("Getting recommendations for user with OS ID: " + openSocialId); List<Recommendation> recommendations; if (maxResults > 0) { recommendations = getRecommendationMapper().findBySubjectOpenSocialId(openSocialId, maxResults); } else { recommendations = getRecommendationMapper().findBySubjectOpenSocialId(openSocialId); } log.debug(recommendations.size() + " recommendations found"); JSONObject json = new JSONObject(); JSONArray recos = new JSONArray(); if (null != recommendations && recommendations.size() > 0) { Map<String, Person> people = getPeopleInfoForRecommendations(recommendations); log.debug("There are " + people.size() + " Person objects available for recommendations."); for (Recommendation reco : recommendations) { recos.add(convertRecoToJSON(reco, people.get(reco.getAuthorOpenSocialId()), people.get(reco.getSubjectOpenSocialId()))); } } json.put(RECOMMENDATIONS_KEY, recos); Representation rep = new StringRepresentation(json.toString(), MediaType.APPLICATION_JSON); rep.setExpirationDate(new Date(0L)); return rep; } }
RecommendationsCollectionResource extends RecommendationsResource { @Override public Representation represent(final Variant variant) throws ResourceException { log.debug("Getting recommendations for user with OS ID: " + openSocialId); List<Recommendation> recommendations; if (maxResults > 0) { recommendations = getRecommendationMapper().findBySubjectOpenSocialId(openSocialId, maxResults); } else { recommendations = getRecommendationMapper().findBySubjectOpenSocialId(openSocialId); } log.debug(recommendations.size() + " recommendations found"); JSONObject json = new JSONObject(); JSONArray recos = new JSONArray(); if (null != recommendations && recommendations.size() > 0) { Map<String, Person> people = getPeopleInfoForRecommendations(recommendations); log.debug("There are " + people.size() + " Person objects available for recommendations."); for (Recommendation reco : recommendations) { recos.add(convertRecoToJSON(reco, people.get(reco.getAuthorOpenSocialId()), people.get(reco.getSubjectOpenSocialId()))); } } json.put(RECOMMENDATIONS_KEY, recos); Representation rep = new StringRepresentation(json.toString(), MediaType.APPLICATION_JSON); rep.setExpirationDate(new Date(0L)); return rep; }  }
RecommendationsCollectionResource extends RecommendationsResource { @Override public Representation represent(final Variant variant) throws ResourceException { log.debug("Getting recommendations for user with OS ID: " + openSocialId); List<Recommendation> recommendations; if (maxResults > 0) { recommendations = getRecommendationMapper().findBySubjectOpenSocialId(openSocialId, maxResults); } else { recommendations = getRecommendationMapper().findBySubjectOpenSocialId(openSocialId); } log.debug(recommendations.size() + " recommendations found"); JSONObject json = new JSONObject(); JSONArray recos = new JSONArray(); if (null != recommendations && recommendations.size() > 0) { Map<String, Person> people = getPeopleInfoForRecommendations(recommendations); log.debug("There are " + people.size() + " Person objects available for recommendations."); for (Recommendation reco : recommendations) { recos.add(convertRecoToJSON(reco, people.get(reco.getAuthorOpenSocialId()), people.get(reco.getSubjectOpenSocialId()))); } } json.put(RECOMMENDATIONS_KEY, recos); Representation rep = new StringRepresentation(json.toString(), MediaType.APPLICATION_JSON); rep.setExpirationDate(new Date(0L)); return rep; }  @Override Representation represent(final Variant variant); @Override void acceptRepresentation(final Representation entity); }
RecommendationsCollectionResource extends RecommendationsResource { @Override public Representation represent(final Variant variant) throws ResourceException { log.debug("Getting recommendations for user with OS ID: " + openSocialId); List<Recommendation> recommendations; if (maxResults > 0) { recommendations = getRecommendationMapper().findBySubjectOpenSocialId(openSocialId, maxResults); } else { recommendations = getRecommendationMapper().findBySubjectOpenSocialId(openSocialId); } log.debug(recommendations.size() + " recommendations found"); JSONObject json = new JSONObject(); JSONArray recos = new JSONArray(); if (null != recommendations && recommendations.size() > 0) { Map<String, Person> people = getPeopleInfoForRecommendations(recommendations); log.debug("There are " + people.size() + " Person objects available for recommendations."); for (Recommendation reco : recommendations) { recos.add(convertRecoToJSON(reco, people.get(reco.getAuthorOpenSocialId()), people.get(reco.getSubjectOpenSocialId()))); } } json.put(RECOMMENDATIONS_KEY, recos); Representation rep = new StringRepresentation(json.toString(), MediaType.APPLICATION_JSON); rep.setExpirationDate(new Date(0L)); return rep; }  @Override Representation represent(final Variant variant); @Override void acceptRepresentation(final Representation entity);  }