package ch.lambdaj;

import static ch.lambdaj.group.Groups.*;
import static ch.lambdaj.Lambda.*;
import static ch.lambdaj.Util.*;
import static ch.lambdaj.mock.ExposureBy.*;
import static java.util.Arrays.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

import java.util.*;

import org.jmock.*;
import org.junit.*;

import ch.lambdaj.mock.*;

public class GroupByTest {
	private Mockery context = new Mockery();

	@Before
	public void setUp() {
		FexFrance = new Exposure() {
			{
				setCountryName("France");
				setInsuredName("Fex France");
				setCountryIso("FR");
				setCountryFlag("/flags/fr.jpg");
			}
		};

		FexCanada = new Exposure() {
			{
				setCountryName("Canada");
				setInsuredName("Fex Canada");
				setCountryIso("CA");
				setCountryFlag("/flags/ca.jpg");
			}
		};

		exposures = asList(FexFrance, FexCanada);
	}

	private Exposure FexFrance, FexCanada;

	private List<Exposure> exposures;

	@Test
	public void testGroupByInsuredName() {
		String string = toJsonString(group(exposures, by(Exposure).insuredName()));

		assertThat(string, containsString("\"insuredName\":\"Fex France"));
		assertThat(string, containsString("\"insuredName\":\"Fex Canada"));
	}

	@Test
	public void testGroupByInsuredNameAndCountryName() {
		String string = toJsonString(group(exposures, by(Exposure).insuredName().countryName()));
		assertThat(string, containsString("\"insuredName\":\"Fex France\",\"children\":"));
		assertThat(string, containsString("\"countryName\":\"France\",\"children\":"));
		assertThat(string, containsString("\"insuredName\":\"Fex Canada\",\"children\":"));
		assertThat(string, containsString("\"countryName\":\"Canada\",\"children\":"));
	}

	@Test
	public void testGroupByInsuredNameAsExposures() {
		String string = toJsonString(group(exposures, by(Exposure).insuredName().asExposures()));
	}

	@Test
	public void testGroupByCountryName() {
		String string = toJsonString(group(exposures, by(Exposure).countryName().asInsureds().headCountryIso()));
		System.out.println(string);
		
		string = cleanJsonString(string);
		assertThat(string, containsString("insureds:[{countryName:France,insuredName:Fex France,countryFlag:/flags/fr.jpg,countryIso:FR}]"));
		assertThat(string, containsString("insureds:[{countryName:Canada,insuredName:Fex Canada,countryFlag:/flags/ca.jpg,countryIso:CA}]"));
		
		string = cleanJsonString(string, cleanJsonString(toJsonString(FexFrance)));
		string = cleanJsonString(string, cleanJsonString(toJsonString(FexCanada)));
		assertThat(string, containsString("countryIso:FR"));
		assertThat(string, containsString("countryIso:CA"));
	}

	@Test
	public void testGroupByCountryNameAndInsuredName() {
		String string = toJsonString(group(exposures, by(Exposure).countryName().insuredName()));
	}

	@Test
	public void testGroupByCountryNameAsCountries() {
		String string = toJsonString(group(exposures, by(Exposure).countryName().asCountries()));
	}

	@Test
	public void testGroupByCountryNameAsExposures() {
		String string = toJsonString(group(exposures, by(Exposure).countryName().asExposures()));
	}
}
@@ -1356,24 +1356,25 @@
 ryName()));%0A
+%0A
 %09%09assertThat
@@ -2014,41 +2014,11 @@
 ));%0A
-%09%09System.out.println(string);%0A
 %09%09%0A
+
 %09%09st
@@ -2062,32 +2062,38 @@
 ertThat(string, 
+allOf(
 containsString(%22
@@ -2107,27 +2107,66 @@
 s:%5B%7B
-countryName:France,
+%22), containsString(%22countryName:France%22), containsString(%22
 insu
@@ -2183,17 +2183,35 @@
 x France
+%22)
 ,
+containsString(%22
 countryF
@@ -2223,25 +2223,43 @@
 flags/fr.jpg
+%22)
 ,
+containsString(%22
 countryIso:F
@@ -2259,19 +2259,18 @@
 ryIso:FR
-%7D%5D
 %22
+)
 ));%0A%09%09as
@@ -2278,32 +2278,38 @@
 ertThat(string, 
+allOf(
 containsString(%22
@@ -2323,27 +2323,66 @@
 s:%5B%7B
-countryName:Canada,
+%22), containsString(%22countryName:Canada%22), containsString(%22
 insu
@@ -2399,17 +2399,35 @@
 x Canada
+%22)
 ,
+containsString(%22
 countryF
@@ -2441,15 +2441,33 @@
 ags/
-ca
+fr
 .jpg
+%22)
 ,
+containsString(%22
 coun
@@ -2477,13 +2477,12 @@
 Iso:
-CA%7D%5D%22
+FR%22)
 ));%0A

update test substring separatly to avoid different order of render via json lib