@Test public void testRepresent() throws ResourceException, IOException { final Variant variant = context.mock(Variant.class); final int maxResults = 10; setupCommonInitExpectations(BackgroundItemType.SKILL, "targetString"); setupCommonRepresentExpectations(BackgroundItemType.SKILL, "targetString"); 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 results = json.getJSONArray(BackgroundResource.ITEM_NAMES_KEY); assertNotNull("JSON text isn't right", results); assertEquals("JSON text isn't right", maxResults, results.size()); context.assertIsSatisfied(); }
@Override public Representation represent(final Variant variant) throws ResourceException { if (backgroundType == null || (backgroundType == BackgroundItemType.NOT_SET) || targetString == null || targetString.length() == 0) { log.info("BackgroundType or TargetString arguements were null or not set." + " No search attempted."); return new StringRepresentation(""); } List<String> results = backgroundMapper.findBackgroundItemNamesByType( backgroundType, targetString, DEFAULT_MAX_RESULTS); if (log.isDebugEnabled()) { log.debug("Search with targetString: " + targetString + " and backgroundType: " + backgroundType + " returned " + results.size() + " results."); } JSONObject json = new JSONObject(); JSONArray jsonResults = new JSONArray(); for (String result : results) { jsonResults.add(result); } json.put(ITEM_NAMES_KEY, jsonResults); if (log.isDebugEnabled()) { log.debug("Search with targetString: " + targetString + " and backgroundType: " + backgroundType + " generated JSON: " + json.toString()); } Representation rep = new StringRepresentation(json.toString(), MediaType.APPLICATION_JSON); rep.setExpirationDate(new Date(0L)); return rep; }
BackgroundResource extends SmpResource { @Override public Representation represent(final Variant variant) throws ResourceException { if (backgroundType == null || (backgroundType == BackgroundItemType.NOT_SET) || targetString == null || targetString.length() == 0) { log.info("BackgroundType or TargetString arguements were null or not set." + " No search attempted."); return new StringRepresentation(""); } List<String> results = backgroundMapper.findBackgroundItemNamesByType( backgroundType, targetString, DEFAULT_MAX_RESULTS); if (log.isDebugEnabled()) { log.debug("Search with targetString: " + targetString + " and backgroundType: " + backgroundType + " returned " + results.size() + " results."); } JSONObject json = new JSONObject(); JSONArray jsonResults = new JSONArray(); for (String result : results) { jsonResults.add(result); } json.put(ITEM_NAMES_KEY, jsonResults); if (log.isDebugEnabled()) { log.debug("Search with targetString: " + targetString + " and backgroundType: " + backgroundType + " generated JSON: " + json.toString()); } Representation rep = new StringRepresentation(json.toString(), MediaType.APPLICATION_JSON); rep.setExpirationDate(new Date(0L)); return rep; } }
BackgroundResource extends SmpResource { @Override public Representation represent(final Variant variant) throws ResourceException { if (backgroundType == null || (backgroundType == BackgroundItemType.NOT_SET) || targetString == null || targetString.length() == 0) { log.info("BackgroundType or TargetString arguements were null or not set." + " No search attempted."); return new StringRepresentation(""); } List<String> results = backgroundMapper.findBackgroundItemNamesByType( backgroundType, targetString, DEFAULT_MAX_RESULTS); if (log.isDebugEnabled()) { log.debug("Search with targetString: " + targetString + " and backgroundType: " + backgroundType + " returned " + results.size() + " results."); } JSONObject json = new JSONObject(); JSONArray jsonResults = new JSONArray(); for (String result : results) { jsonResults.add(result); } json.put(ITEM_NAMES_KEY, jsonResults); if (log.isDebugEnabled()) { log.debug("Search with targetString: " + targetString + " and backgroundType: " + backgroundType + " generated JSON: " + json.toString()); } Representation rep = new StringRepresentation(json.toString(), MediaType.APPLICATION_JSON); rep.setExpirationDate(new Date(0L)); return rep; }  }
BackgroundResource extends SmpResource { @Override public Representation represent(final Variant variant) throws ResourceException { if (backgroundType == null || (backgroundType == BackgroundItemType.NOT_SET) || targetString == null || targetString.length() == 0) { log.info("BackgroundType or TargetString arguements were null or not set." + " No search attempted."); return new StringRepresentation(""); } List<String> results = backgroundMapper.findBackgroundItemNamesByType( backgroundType, targetString, DEFAULT_MAX_RESULTS); if (log.isDebugEnabled()) { log.debug("Search with targetString: " + targetString + " and backgroundType: " + backgroundType + " returned " + results.size() + " results."); } JSONObject json = new JSONObject(); JSONArray jsonResults = new JSONArray(); for (String result : results) { jsonResults.add(result); } json.put(ITEM_NAMES_KEY, jsonResults); if (log.isDebugEnabled()) { log.debug("Search with targetString: " + targetString + " and backgroundType: " + backgroundType + " generated JSON: " + json.toString()); } Representation rep = new StringRepresentation(json.toString(), MediaType.APPLICATION_JSON); rep.setExpirationDate(new Date(0L)); return rep; }  void setBackgroundMapper(final BackgroundMapper inBackgroundMapper); BackgroundItemType getbackgroundType(); void setBackgroundType(final BackgroundItemType inBackgroundType); @Override Representation represent(final Variant variant); }
BackgroundResource extends SmpResource { @Override public Representation represent(final Variant variant) throws ResourceException { if (backgroundType == null || (backgroundType == BackgroundItemType.NOT_SET) || targetString == null || targetString.length() == 0) { log.info("BackgroundType or TargetString arguements were null or not set." + " No search attempted."); return new StringRepresentation(""); } List<String> results = backgroundMapper.findBackgroundItemNamesByType( backgroundType, targetString, DEFAULT_MAX_RESULTS); if (log.isDebugEnabled()) { log.debug("Search with targetString: " + targetString + " and backgroundType: " + backgroundType + " returned " + results.size() + " results."); } JSONObject json = new JSONObject(); JSONArray jsonResults = new JSONArray(); for (String result : results) { jsonResults.add(result); } json.put(ITEM_NAMES_KEY, jsonResults); if (log.isDebugEnabled()) { log.debug("Search with targetString: " + targetString + " and backgroundType: " + backgroundType + " generated JSON: " + json.toString()); } Representation rep = new StringRepresentation(json.toString(), MediaType.APPLICATION_JSON); rep.setExpirationDate(new Date(0L)); return rep; }  void setBackgroundMapper(final BackgroundMapper inBackgroundMapper); BackgroundItemType getbackgroundType(); void setBackgroundType(final BackgroundItemType inBackgroundType); @Override Representation represent(final Variant variant); static final String ITEM_NAMES_KEY; }