package com.absontheweb.bookshop.i18n.controller;

import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;

import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;

import com.absontheweb.bookshop.i18n.model.Language;
import com.absontheweb.bookshop.test.AbstractLiveTest;


public class I18nControllerLiveTest extends AbstractLiveTest {

	@Test
	public void testGetMessages() throws Exception {
		addVar("language", "IT");
		
		ResponseEntity<Map<String,String>> responseEntity = restClient.exchange(
				BASE_API_URL.concat("/i18n/messages/{language}"),
				HttpMethod.GET, new HttpEntity<Map<String,String>>(securityHeaders), 
				new ParameterizedTypeReference<Map<String,String>>() {}, 
				urlVars());
		
		Map<String,String> messages = responseEntity.getBody();

		assertThat(messages,is(notNullValue()));
		assertThat(messages.size(),is(greaterThan(2)));
    	assertThat(messages.get("languageName"), is("Italiano"));
	}
	
	
	@Test
	public void testGetLanguages() throws Exception {
		
		ResponseEntity<List<Language>> responseEntity = restClient.exchange(
				BASE_API_URL.concat("/i18n/languages"),
				HttpMethod.GET, new HttpEntity<List<Language>>(securityHeaders), 
				new ParameterizedTypeReference<List<Language>>() {}, urlVars());
		
		List<Language> languages = responseEntity.getBody();

		assertThat(languages,is(notNullValue()));
		assertThat(languages.size(),is(2));
		Language lang = languages.get(0);
    	assertThat(lang.getName(), is("English"));
    	assertThat(lang.getLocale(), is("EN"));
    	assertThat(lang.getFlagUrl(), is("http://localhost:8080/assets/EN_flag.png"));
		lang = languages.get(1);
    	assertThat(lang.getName(), is("Italiano"));
    	assertThat(lang.getLocale(), is("IT"));
    	assertThat(lang.getFlagUrl(), is("http://localhost:8080/assets/IT_flag.png"));
	}
	

}

@@ -44,16 +44,72 @@
 oller;%0A%0A
+import static org.hamcrest.Matchers.containsInAnyOrder;%0A
 import s
@@ -1779,173 +1779,82 @@
 ;%0A%09%09
-Language lang = languages.get(0);%0A    %09assertThat(lang.getName(), is(%22English%22));%0A    %09assertThat(lang.getLocale(), is(%22EN%22));%0A    %09assertThat(lang.getFlagUrl(), is(
+assertThat(languages, containsInAnyOrder(%0A%09%09%09%09new Language(%22English%22,%22EN%22,
 %22htt
@@ -1896,170 +1896,43 @@
 ng%22)
-);%0A%09%09lang = languages.get(1);%0A    %09assertThat(lang.getName(), is(%22Italiano%22));%0A    %09assertThat(lang.getLocale(), is(%22IT%22));%0A    %09assertThat(lang.getFlagUrl(), is(
+,%0A%09%09%09%09new Language(%22Italiano%22,%22IT%22,
 %22htt
@@ -1971,16 +1971,20 @@
 g.png%22))
+%0A%09%09)
 ;%0A%09%7D%0A%09%0A%0A

Update I18nControllerLiveTest.java