Search in sources :

Example 1 with DiscussionData

use of org.edx.mobile.model.course.DiscussionData in project edx-app-android by edx.

the class ApiTests method verifyModelParsing.

/**
 * Verifies the parsing of course structure json to {@link CourseComponent} model class.
 *
 * @param component Parsed {@link CourseComponent} model.
 */
private void verifyModelParsing(CourseComponent component) throws IOException, JSONException {
    JSONObject jsonObj;
    jsonObj = new JSONObject(MockDataUtil.getMockResponse("get_course_structure"));
    jsonObj = jsonObj.getJSONObject("blocks");
    jsonObj = jsonObj.getJSONObject(component.getId());
    assertNotNull(jsonObj);
    // Not using the getDisplayName function below, because it returns a placeholder text
    // when the display_name field's value is empty.
    assertEquals(jsonObj.getString("display_name"), component.getInternalName());
    assertEquals(jsonObj.getBoolean("graded"), component.isGraded());
    assertEquals(jsonObj.getString("student_view_url"), component.getBlockUrl());
    assertEquals(jsonObj.getBoolean("student_view_multi_device"), component.isMultiDevice());
    assertEquals(jsonObj.getString("lms_web_url"), component.getWebUrl());
    // Type specific validations
    Gson gson = new Gson();
    switch(component.getType()) {
        case VIDEO:
            {
                JSONObject dataObj = jsonObj.getJSONObject("student_view_data");
                // Our current parser checks the existence of these fields to determine the type to convert into
                assertTrue(dataObj.has("encoded_videos") || dataObj.has("transcripts"));
                String dataRawJson = dataObj.toString();
                assertTrue(component instanceof VideoBlockModel);
                VideoBlockModel model = (VideoBlockModel) component;
                VideoData expected = gson.fromJson(dataRawJson, VideoData.class);
                assertEquals(expected, model.getData());
                break;
            }
        case DISCUSSION:
            {
                JSONObject dataObj = jsonObj.getJSONObject("student_view_data");
                // Our current parser checks the existence of these fields to determine the type to convert into
                assertTrue(dataObj.has("topic_id"));
                String dataRawJson = dataObj.toString();
                assertTrue(component instanceof DiscussionBlockModel);
                DiscussionBlockModel model = (DiscussionBlockModel) component;
                DiscussionData expected = gson.fromJson(dataRawJson, DiscussionData.class);
                assertEquals(expected, model.getData());
                break;
            }
    }
}
Also used : DiscussionData(org.edx.mobile.model.course.DiscussionData) JSONObject(org.json.JSONObject) VideoData(org.edx.mobile.model.course.VideoData) Gson(com.google.gson.Gson) VideoBlockModel(org.edx.mobile.model.course.VideoBlockModel) DiscussionBlockModel(org.edx.mobile.model.course.DiscussionBlockModel)

Aggregations

Gson (com.google.gson.Gson)1 DiscussionBlockModel (org.edx.mobile.model.course.DiscussionBlockModel)1 DiscussionData (org.edx.mobile.model.course.DiscussionData)1 VideoBlockModel (org.edx.mobile.model.course.VideoBlockModel)1 VideoData (org.edx.mobile.model.course.VideoData)1 JSONObject (org.json.JSONObject)1