ProjectJsonParser implements JsonParser<Project> { @Override public Project parse(JSONObject json) throws JSONException { URI self = JsonParseUtil.getSelfUri(json); final BasicUser lead = JsonParseUtil.parseBasicUser(json.getJSONObject("lead")); final String key = json.getString("key"); final String name = JsonParseUtil.getOptionalString(json, "name"); final String urlStr = JsonParseUtil.getOptionalString(json, "url"); URI uri; try { uri = urlStr == null || "".equals(urlStr) ? null : new URI(urlStr); } catch (URISyntaxException e) { uri = null; } String description = JsonParseUtil.getOptionalString(json, "description"); if ("".equals(description)) { description = null; } final Collection<Version> versions = JsonParseUtil.parseJsonArray(json.getJSONArray("versions"), versionJsonParser); final Collection<BasicComponent> components = JsonParseUtil.parseJsonArray(json.getJSONArray("components"), componentJsonParser); return new Project(self, key, name, description, lead, uri, versions, components); }  @Override Project parse(JSONObject json);  }
@Test public void testParse() throws Exception { final Project project = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/project/valid.json")); assertEquals(TestUtil.toUri("http: assertEquals("This is my description here.\r\nAnother line.", project.getDescription()); assertEquals(TestConstants.USER_ADMIN, project.getLead()); assertEquals("http: assertEquals("TST", project.getKey()); assertThat(project.getVersions(), hasOnlyElements(TestConstants.VERSION_1, TestConstants.VERSION_1_1)); assertThat(project.getComponents(), hasOnlyElements(TestConstants.BCOMPONENT_A, TestConstants.BCOMPONENT_B)); assertNull(project.getName()); }
@Test public void testParseProjectWithNoUrl() throws JSONException { final Project project = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/project/project-no-url.json")); assertEquals("MYT", project.getKey()); assertNull(project.getUri()); assertNull(project.getDescription()); }
@Test public void testParseProjectInJira4x4() throws JSONException { final Project project = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/project/project-jira-4-4.json")); assertEquals("TST", project.getKey()); assertEquals(new DateMidnight(2010, 8, 25).toInstant(), Iterables.getLast(project.getVersions()).getReleaseDate().toInstant()); assertEquals("Test Project", project.getName()); }