// 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()));
		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);
		string = removeString(string, removeApos(json(FexFrance)));
		string = removeString(string, removeApos(json(FexCanada)));
		string = removeString(string, "");
		
		System.out.println(string);
		assertThat(string, containsString("children:[{children:[],countryName:France}],insuredName:Fex France}"));
		assertThat(string, containsString("children:[],countryName:France"));
		assertThat(string, containsString("children:[{children:[],countryName:Canada}],insuredName:Fex Canada}"));
		assertThat(string, containsString("children:[],countryName:Canada"));
	}

	@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);
	}
}
@@ -1895,65 +1895,69 @@
 en:%5B
-%7Bchildren:%5B%5D,
+%5D%22));%0A%09%09assertThat(string, 
 co
-u
 nt
-ryName:France%7D%5D,insured
+ainsString(%22country
 Name:
-Fex 
 France
-%7D
 %22));
@@ -1998,32 +1998,24 @@
 ng(%22
-children:%5B%5D,country
+insured
 Name:
+Fex 
 Fran
@@ -2063,74 +2063,25 @@
 g(%22c
-hildren:%5B%7Bchildren:%5B%5D,countryName:Canada%7D%5D,insuredName:Fex Canada%7D
+ountryName:Canada
 %22));
@@ -2122,32 +2122,24 @@
 ng(%22
-children:%5B%5D,country
+insured
 Name:
+Fex 
 Cana

test fixed