@Test public void testRepresent() throws ResourceException, IOException { final Variant variant = context.mock(Variant.class); setupCommonInitExpectations("abc"); final DisplayEntityModelView femv = new DisplayEntityModelView(); femv.setDisplayName("NameGoesHere"); femv.setType(EntityType.PERSON); femv.setUniqueKey("KeyGoesHere"); femv.setStreamScopeId(1L); final ArrayList<DisplayEntityModelView> results = new ArrayList<DisplayEntityModelView>(1); results.add(femv); SecurityContext originalContext = SecurityContextHolder.getContext(); SecurityContextHolder.setContext(securityContext); context.checking(new Expectations() { { oneOf(securityContext).getAuthentication(); will(returnValue(auth)); oneOf(auth).getName(); will(returnValue("name")); oneOf(entitiesDAO).execute(with(any(GetEntitiesByPrefixRequest.class))); will(returnValue(results)); } }); sut.init(restContext, request, response); Representation actual = sut.represent(variant); assertEquals("MediaType should be application/json", MediaType.APPLICATION_JSON, actual.getMediaType()); JSONObject json = JSONObject.fromObject(actual.getText()); JSONArray jsonResults = json.getJSONArray(EntitiesByPrefixResource.ENTITIES_KEY); assertNotNull("JSON text isn't right", jsonResults); assertEquals(1, results.size()); SecurityContextHolder.setContext(originalContext); context.assertIsSatisfied(); }
@Override public Representation represent(final Variant variant) throws ResourceException { try { if (targetString == null || targetString.length() == 0) { log.info("TargetString arguement null or not set." + " No search attempted."); return new StringRepresentation(""); } String acctId = SecurityContextHolder.getContext().getAuthentication().getName(); List<DisplayEntityModelView> results = entitiesDAO.execute(new GetEntitiesByPrefixRequest(acctId.trim() .toLowerCase(), targetString)); JSONObject json = new JSONObject(); JSONArray jsonEntities = new JSONArray(); for (DisplayEntityModelView femv : results) { if (!femv.isAccountLocked()) { jsonEntities.add(convertDisplayEntityModelViewToJSON(femv)); } } json.put(ENTITIES_KEY, jsonEntities); log.debug("EntitiesByPrefixResource: json = " + json.toString()); Representation rep = new StringRepresentation(json.toString(), MediaType.APPLICATION_JSON); rep.setExpirationDate(new Date(0L)); return rep; } catch (Exception ex) { log.error("Error occurred retrieving entity.", ex); throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Error occurred retrieving entity.", ex); } }
EntitiesByPrefixResource extends WritableResource { @Override public Representation represent(final Variant variant) throws ResourceException { try { if (targetString == null || targetString.length() == 0) { log.info("TargetString arguement null or not set." + " No search attempted."); return new StringRepresentation(""); } String acctId = SecurityContextHolder.getContext().getAuthentication().getName(); List<DisplayEntityModelView> results = entitiesDAO.execute(new GetEntitiesByPrefixRequest(acctId.trim() .toLowerCase(), targetString)); JSONObject json = new JSONObject(); JSONArray jsonEntities = new JSONArray(); for (DisplayEntityModelView femv : results) { if (!femv.isAccountLocked()) { jsonEntities.add(convertDisplayEntityModelViewToJSON(femv)); } } json.put(ENTITIES_KEY, jsonEntities); log.debug("EntitiesByPrefixResource: json = " + json.toString()); Representation rep = new StringRepresentation(json.toString(), MediaType.APPLICATION_JSON); rep.setExpirationDate(new Date(0L)); return rep; } catch (Exception ex) { log.error("Error occurred retrieving entity.", ex); throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Error occurred retrieving entity.", ex); } } }
EntitiesByPrefixResource extends WritableResource { @Override public Representation represent(final Variant variant) throws ResourceException { try { if (targetString == null || targetString.length() == 0) { log.info("TargetString arguement null or not set." + " No search attempted."); return new StringRepresentation(""); } String acctId = SecurityContextHolder.getContext().getAuthentication().getName(); List<DisplayEntityModelView> results = entitiesDAO.execute(new GetEntitiesByPrefixRequest(acctId.trim() .toLowerCase(), targetString)); JSONObject json = new JSONObject(); JSONArray jsonEntities = new JSONArray(); for (DisplayEntityModelView femv : results) { if (!femv.isAccountLocked()) { jsonEntities.add(convertDisplayEntityModelViewToJSON(femv)); } } json.put(ENTITIES_KEY, jsonEntities); log.debug("EntitiesByPrefixResource: json = " + json.toString()); Representation rep = new StringRepresentation(json.toString(), MediaType.APPLICATION_JSON); rep.setExpirationDate(new Date(0L)); return rep; } catch (Exception ex) { log.error("Error occurred retrieving entity.", ex); throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Error occurred retrieving entity.", ex); } }  }
EntitiesByPrefixResource extends WritableResource { @Override public Representation represent(final Variant variant) throws ResourceException { try { if (targetString == null || targetString.length() == 0) { log.info("TargetString arguement null or not set." + " No search attempted."); return new StringRepresentation(""); } String acctId = SecurityContextHolder.getContext().getAuthentication().getName(); List<DisplayEntityModelView> results = entitiesDAO.execute(new GetEntitiesByPrefixRequest(acctId.trim() .toLowerCase(), targetString)); JSONObject json = new JSONObject(); JSONArray jsonEntities = new JSONArray(); for (DisplayEntityModelView femv : results) { if (!femv.isAccountLocked()) { jsonEntities.add(convertDisplayEntityModelViewToJSON(femv)); } } json.put(ENTITIES_KEY, jsonEntities); log.debug("EntitiesByPrefixResource: json = " + json.toString()); Representation rep = new StringRepresentation(json.toString(), MediaType.APPLICATION_JSON); rep.setExpirationDate(new Date(0L)); return rep; } catch (Exception ex) { log.error("Error occurred retrieving entity.", ex); throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Error occurred retrieving entity.", ex); } }  void setEntitiesDAO(final SearchPeopleAndGroupsByPrefix inEntitiesDAO); @Override Representation represent(final Variant variant); }
EntitiesByPrefixResource extends WritableResource { @Override public Representation represent(final Variant variant) throws ResourceException { try { if (targetString == null || targetString.length() == 0) { log.info("TargetString arguement null or not set." + " No search attempted."); return new StringRepresentation(""); } String acctId = SecurityContextHolder.getContext().getAuthentication().getName(); List<DisplayEntityModelView> results = entitiesDAO.execute(new GetEntitiesByPrefixRequest(acctId.trim() .toLowerCase(), targetString)); JSONObject json = new JSONObject(); JSONArray jsonEntities = new JSONArray(); for (DisplayEntityModelView femv : results) { if (!femv.isAccountLocked()) { jsonEntities.add(convertDisplayEntityModelViewToJSON(femv)); } } json.put(ENTITIES_KEY, jsonEntities); log.debug("EntitiesByPrefixResource: json = " + json.toString()); Representation rep = new StringRepresentation(json.toString(), MediaType.APPLICATION_JSON); rep.setExpirationDate(new Date(0L)); return rep; } catch (Exception ex) { log.error("Error occurred retrieving entity.", ex); throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Error occurred retrieving entity.", ex); } }  void setEntitiesDAO(final SearchPeopleAndGroupsByPrefix inEntitiesDAO); @Override Representation represent(final Variant variant); static final String ITEM_NAMES_KEY; }