ComponentJsonParser implements JsonParser<Component> { @Override public Component parse(JSONObject json) throws JSONException { final BasicComponent basicComponent = BasicComponentJsonParser.parseBasicComponent(json); final JSONObject leadJson = json.optJSONObject("lead"); final BasicUser lead = leadJson != null ? JsonParseUtil.parseBasicUser(leadJson) : null; final String assigneeTypeStr = JsonParseUtil.getOptionalString(json, "assigneeType"); final Component.AssigneeInfo assigneeInfo; if (assigneeTypeStr != null) { final AssigneeType assigneeType = parseAssigneeType(assigneeTypeStr); final JSONObject assigneeJson = json.optJSONObject("assignee"); final BasicUser assignee = assigneeJson != null ? JsonParseUtil.parseBasicUser(assigneeJson) : null; final AssigneeType realAssigneeType = parseAssigneeType(json.getString("realAssigneeType")); final JSONObject realAssigneeJson = json.optJSONObject("realAssignee"); final BasicUser realAssignee = realAssigneeJson != null ? JsonParseUtil.parseBasicUser(realAssigneeJson) : null; final boolean isAssigneeTypeValid = json.getBoolean("isAssigneeTypeValid"); assigneeInfo = new Component.AssigneeInfo(assignee, assigneeType, realAssignee, realAssigneeType, isAssigneeTypeValid); } else { assigneeInfo = null; } return new Component(basicComponent.getSelf(), basicComponent.getName(), basicComponent.getDescription(), lead, assigneeInfo); }  @Override Component parse(JSONObject json);  }
@Test public void testParseBasicComponent() throws Exception { BasicComponentJsonParser parser = new BasicComponentJsonParser(); final BasicComponent component = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/component/basic-valid.json")); assertEquals(new URI("http: assertEquals("Component A", component.getName()); assertEquals("this is some description of component A", component.getDescription()); }
@Test public void testParseBasicComponentWithNoDescription() throws Exception { BasicComponentJsonParser parser = new BasicComponentJsonParser(); final BasicComponent component = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/component/basic-no-description-valid.json")); assertEquals(new URI("http: assertEquals("Component A", component.getName()); assertNull(component.getDescription()); }
@Test public void testParseComponent() throws Exception { ComponentJsonParser parser = new ComponentJsonParser(); final Component component = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/component/complete-valid.json")); assertEquals(new URI("http: assertEquals("Component B", component.getName()); assertEquals(TestConstants.USER1, component.getLead()); assertEquals("another description", component.getDescription()); }
@Test public void testParseComponenWithNoLead() throws Exception { ComponentJsonParser parser = new ComponentJsonParser(); final Component component = parser.parse(ResourceUtil.getJsonObjectFromResource("/json/component/complete-no-lead-valid.json")); assertEquals(new URI("http: assertEquals("Component B", component.getName()); assertNull(component.getLead()); assertEquals("another description", component.getDescription()); }