// Modified or written by Ex Machina SAGL for inclusion with lambdaj.
// Copyright (c) 2009 Mario Fusco, Luca Marrocco.
// Licensed under the Apache License, Version 2.0 (the "License")

package ch.lambdaj.to.group;

import static ch.lambdaj.group.Groups.*;
import static ch.lambdaj.Lambda.*;
import static ch.lambdaj.Strings.*;
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.*;

/**
 * @author Mario Fusco
 * @author Luca Marrocco
 */
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 = json(group(exposures, by(Exposure).insuredName()));
		System.out.println(string);
		string = removeApos(string);
		assertThat(string, containsString("insuredName:Fex France"));
		assertThat(string, containsString("insuredName:Fex Canada"));
	}

	@Test
	public void testGroupByInsuredNameAndCountryName() {
		String string = json(group(exposures, by(Exposure).insuredName().countryName()));
		string = removeApos(string);
		
		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 = json(group(exposures, by(Exposure).insuredName().asExposures()));
	}

	@Test
	public void testGroupByCountryName() {
		String string = json(group(exposures, by(Exposure).countryName().asInsureds().headCountryIso()));

		string = removeApos(string);
		assertThat(string, containsString("insureds:[{"));
		assertThat(string, allOf(containsString("countryName:France"), containsString("insuredName:Fex France"), containsString("countryFlag:/flags/fr.jpg"), containsString("countryIso:FR")));
		assertThat(string, allOf(containsString("countryName:Canada"), containsString("insuredName:Fex Canada"), containsString("countryFlag:/flags/fr.jpg"), containsString("countryIso:FR")));

		string = removeString(string, removeApos(json(FexFrance)));
		string = removeString(string, removeApos(json(FexCanada)));
		assertThat(string, containsString("countryIso:FR"));
		assertThat(string, containsString("countryIso:CA"));
	}

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

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

	@Test
	public void testGroupByCountryNameAsExposures() {
		String string = json(group(exposures, by(Exposure).countryName().asExposures()));
		string = removeApos(string);
	}
}
@@ -1313,38 +1313,8 @@
 ));%0A
-%09%09System.out.println(string);%0A
 %09%09st
@@ -1332,32 +1332,33 @@
 veApos(string);%0A
+%0A
 %09%09assertThat(str
@@ -1610,32 +1610,33 @@
 ountryName()));%0A
+%0A
 %09%09string = remov
@@ -1656,46 +1656,281 @@
 ;%0A%09%09
-%0A%09%09assertThat(string, containsString(%22
+string = removeString(string, removeApos(json(FexFrance)));%0A%09%09string = removeString(string, removeApos(json(FexCanada)));%0A%09%09string = removeString(string, %22%22);%0A%09%09%0A%09%09System.out.println(string);%0A%09%09assertThat(string, containsString(%22children:%5B%7Bchildren:%5B%5D,countryName:France%7D%5D,
 insu
@@ -1943,34 +1943,25 @@
 e:Fex France
-,children:
+%7D
 %22));%0A%09%09asser
@@ -1982,32 +1982,44 @@
 containsString(%22
+children:%5B%5D,
 countryName:Fran
@@ -2020,26 +2020,16 @@
 e:France
-,children:
 %22));%0A%09%09a
@@ -2054,32 +2054,76 @@
 containsString(%22
+children:%5B%7Bchildren:%5B%5D,countryName:Canada%7D%5D,
 insuredName:Fex 
@@ -2124,34 +2124,25 @@
 e:Fex Canada
-,children:
+%7D
 %22));%0A%09%09asser
@@ -2163,32 +2163,44 @@
 containsString(%22
+children:%5B%5D,
 countryName:Cana
@@ -2205,18 +2205,8 @@
 nada
-,children:
 %22));

update simplify string to compare results