/**
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.web.patient;

import junit.framework.Assert;

import org.junit.Test;
import org.openmrs.test.Verifies;
import org.openmrs.web.controller.patient.PatientDashboardGraphController;
import org.openmrs.web.controller.patient.PatientGraphData;
import org.openmrs.web.test.BaseWebContextSensitiveTest;
import org.springframework.ui.ModelMap;

import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * Test for graphs on the patient dashboard
 */
public class PatientDashboardGraphControllerTest extends BaseWebContextSensitiveTest {
	
	/**
	 * Test getting a concept by name and by partial name.
	 * 
	 * @see {@link PatientDashboardGraphController#showGraphData(Integer, Integer, ModelMap)}
	 */
	@Test
	@Verifies(value = "return json data with observation details and critical values for the concept", method = "showGraphData(Integer, Integer, ModelMap)")
	public void shouldReturnJSONWithPatientObservationDetails() throws Exception {
		executeDataSet("org/openmrs/api/include/ObsServiceTest-initial.xml");
		PatientDashboardGraphController controller = new PatientDashboardGraphController();
		
		long firstObsDate = new GregorianCalendar(2006, Calendar.FEBRUARY, 9).getTimeInMillis();
		long secondObsDate = new GregorianCalendar(2006, Calendar.FEBRUARY, 10).getTimeInMillis();
		
		ModelMap map = new ModelMap();
		controller.showGraphData(2, 1, map);
		PatientGraphData graph = (PatientGraphData) map.get("graph");
		String expectedData = String
		        .format(
		            "{\"absolute\":{\"high\":50.0,\"low\":2.0},\"critical\":{\"high\":null,\"low\":null},\"name\":\"Some concept name\",\"normal\":{\"high\":null,\"low\":null},\"data\":[[%d,2.0],[%d,1.0]],\"units\":\"\"}",
		            secondObsDate, firstObsDate);
		Assert.assertEquals(expectedData, graph.toString());
	}
	
	/**
	 * Test the path of the form for rendering the json data
	 * 
	 * @see {@link PatientDashboardGraphController#showGraphData(Integer, Integer, ModelMap)}
	 */
	@Test
	@Verifies(value = "return form for rendering the json data", method = "showGraphData(Integer, Integer, ModelMap)")
	public void shouldDisplayPatientDashboardGraphForm() throws Exception {
		executeDataSet("org/openmrs/api/include/ObsServiceTest-initial.xml");
		Assert.assertEquals("patientGraphJsonForm", new PatientDashboardGraphController()
		        .showGraphData(2, 1, new ModelMap()));
	}
}

@@ -599,23 +599,103 @@
 ort 
-junit.framework
+org.codehaus.jackson.JsonNode;%0D%0Aimport org.codehaus.jackson.map.ObjectMapper;%0D%0Aimport org.junit
 .Ass
@@ -696,26 +696,24 @@
 it.Assert;%0D%0A
-%0D%0A
 import org.j
@@ -1061,16 +1061,44 @@
 lendar;%0D
+%0Aimport java.util.Iterator;%0D
 %0A%0D%0A/**%0D%0A
@@ -2142,16 +2142,20 @@
 aph%22);%0D%0A
+%09%09%0D%0A
 %09%09String
@@ -2467,60 +2467,448 @@
 %0D%0A%09%09
-Assert.assertEquals(expectedData, graph.toString());
+%0D%0A%09%09ObjectMapper mapper = new ObjectMapper();%0D%0A%09%09JsonNode expectedJson = mapper.readTree(expectedData);%0D%0A%09%09JsonNode actualJson = mapper.readTree(graph.toString());%0D%0A%09%09%0D%0A%09%09Assert.assertEquals(expectedJson.size(), actualJson.size());%0D%0A%09%09for (Iterator%3CString%3E fieldNames = expectedJson.getFieldNames(); fieldNames.hasNext();) %7B%0D%0A%09%09%09String field = fieldNames.next();%0D%0A%09%09%09Assert.assertEquals(expectedJson.get(field), actualJson.get(field));%0D%0A%09%09%7D
 %0D%0A%09%7D

Update test to work with unordered JSON. Fixes TRUNK-4592