JSONResultParser { public List<PropertyValue> parse(final String json) { LOG.trace("Parsing json: {}", json); final List<PropertyValue> result = new ArrayList<PropertyValue>(); final List<PropertyValue> tmp = new ArrayList<PropertyValue>(); final JSONObject obj = (JSONObject) JSONSerializer.toJSON(json); final JSONObject head = obj.getJSONObject("head"); final JSONArray vars = head.getJSONArray("vars"); final JSONArray bindings = obj.getJSONObject("results").getJSONArray("bindings"); final EntityType formattype = new EntityType("format", "Represents a file format"); if (bindings.size() == 0) { this.valuesExisted = false; } else { this.valuesExisted = true; } int newFormats = 0; for (int i = 0; i < bindings.size(); i++) { final JSONObject binding = bindings.getJSONObject(i); final boolean process = this.processDispatcher.process(binding.toString()); if (process) { String entityName = binding.getJSONObject(vars.getString(0)).getString("value"); String mime = null; String version = null; newFormats++; LOG.trace("parsing values for format: '{}'", entityName); final Entity format = new Entity(formattype, entityName); for (int j = 1; j < vars.size(); j++) { final String name = vars.getString(j); final PropertyValue value = this.getPropertyValue(binding, name, formattype); if (value != null) { LOG.trace("parsed value for property '{}': '{}'", value.getProperty().getName(), value.getValue()); value.setEntity(format); tmp.add(value); if (value.getProperty().getName().equals("mime")) { mime = (String) value.getValue(); } if (value.getProperty().getName().equals("version")) { version = (String) value.getValue(); } } } this.updateEntityName(format, mime, version); for (PropertyValue pv : tmp) { this.addResult(result, pv); } tmp.clear(); } } LOG.debug("Processed {} new json bindings", newFormats); return result; } JSONResultParser(final ResultProcessingDispatcher dispatcher); List<PropertyValue> parse(final String json); boolean shouldContinueCrawl();  }
@Test public void onValidResponse() throws Exception { final String response = this.getResponse(true); final List<PropertyValue> parse = this.parser.parse(response); Assert.assertNotNull(parse); Assert.assertFalse(parse.isEmpty()); }
@Test(expected = JSONException.class) public void onNonValidResponse() throws Exception { final String response = this.getResponse(false); this.parser.parse(response); Assert.fail("This code should not have been reached"); }