package com.expidev.gcmapp.json;

import com.expidev.gcmapp.model.Training;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.List;

/**
 * Created by matthewfrederick on 2/23/15.
 */
public class TrainingJsonParserTest extends TestCase
{
    public void testParseTrainingDetails() throws Exception
    {
        List<Training> trainings = TrainingJsonParser.parseTrainings(testTrainingDetails());
        Assert.assertNotNull(trainings);
        Assert.assertEquals(trainings.size(), 2);
    }
    
    private JSONArray testTrainingDetails() throws JSONException
    {
        return new JSONArray("[" +
            "{" +
                "\"Id\":16," +
                "\"ministry_id\":\"770ffd2c-d6ac-11e3-9e38-12725f8f377c\"," +
                "\"name\":\"Harare Ops\"," +
                "\"date\":\"2015-01-21\"," +
                "\"type\":\"MC2\"," +
                "\"mcc\":\"gcm\"," +
                "\"latitude\":null," +
                "\"longitude\":null," +
                "\"last_updated\":null," +
                "\"gcm_training_completions\":[" +
                "{" +
                    "\"Id\":16," +
                    "\"phase\":1," +
                    "\"number_completed\":11," +
                    "\"date\":\"2015-01-21\"," +
                    "\"training_id\":16," +
                    "\"last_updated\":null" +
                "}]" +
            "}," +
            "{" +
                "\"Id\":28," +
                "\"ministry_id\":\"770ffd2c-d6ac-11e3-9e38-12725f8f377c\"," +
                "\"name\":\"test\"," +
                "\"date\":\"2015-02-27\"," +
                "\"type\":\"T4T\"," +
                "\"mcc\":\"gcm\"," +
                "\"latitude\":17.167196888127869," +
                "\"longitude\":-88.787483762611373," +
                "\"last_updated\":null," +
                "\"gcm_training_completions\":[]" +
            "}]");        
    }
}

@@ -23,24 +23,191 @@
 mapp.json;%0A%0A
+import android.content.Context;%0Aimport android.test.InstrumentationTestCase;%0Aimport android.test.RenamingDelegatingContext;%0A%0Aimport com.expidev.gcmapp.db.TrainingDao;%0A
 import com.e
@@ -272,41 +272,59 @@
 rt;%0A
+%0A
 import 
-junit.framework.TestCase;%0A
+org.ccci.gto.android.common.db.AbstractDao;
 %0Aimp
@@ -502,266 +502,1330 @@
 nds 
-TestCase%0A%7B%0A    public void testParseTrainingDetails() throws Exception%0A    %7B%0A        List%3CTraining%3E trainings = TrainingJsonParser.parseTrainings(testTrainingDetails());%0A        Assert.assertNotNull(trainings);%0A        Assert.assertEquals(trainings.size(), 2
+InstrumentationTestCase%0A%7B%0A    Context context;%0A%0A    @Override%0A    public void setUp() throws Exception%0A    %7B%0A        super.setUp();%0A        context = new RenamingDelegatingContext(getInstrumentation().getTargetContext().getApplicationContext(), %22test_%22);%0A    %7D%0A%0A    public void testParseTrainingDetails() throws Exception%0A    %7B%0A        List%3CTraining%3E trainings = TrainingJsonParser.parseTrainings(testTrainingDetails());%0A        Assert.assertNotNull(trainings);%0A        Assert.assertEquals(trainings.size(), 2);%0A%0A        TrainingDao trainingDao = TrainingDao.getInstance(context);%0A        final AbstractDao.Transaction tx = trainingDao.newTransaction();%0A        try %7B%0A            tx.begin();%0A%0A            // save trainings%0A            for (final Training training : trainings) %7B%0A                trainingDao.saveTraining(training);%0A            %7D%0A%0A            tx.setSuccessful();%0A        %7D finally %7B%0A            tx.end();%0A        %7D%0A        %0A        trainings = null;%0A        %0A        try%0A        %7B%0A            tx.begin();%0A            %0A            trainings = trainingDao.getAllMinistryTraining(%22770ffd2c-d6ac-11e3-9e38-12725f8f377c%22);%0A            tx.setSuccessful();%0A        %7D finally%0A        %7B%0A            tx.end();%0A        %7D%0A        %0A        Assert.assertNotNull(trainings);%0A        Assert.assertEquals(2, trainings.size()
 );%0A 

create test that saves and retrieves training