package com.springsource.greenhouse.settings;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

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

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.social.account.Account;
import org.springframework.ui.ExtendedModelMap;

import com.springsource.greenhouse.database.GreenhouseTestDatabaseBuilder;

public class SettingsControllerTest {

	private EmbeddedDatabase db;

	private JdbcTemplate jdbcTemplate;
	
	private SettingsController controller; 
	
    @Before
    public void setup() {
		db = new GreenhouseTestDatabaseBuilder().member().connectedApp().testData(getClass()).getDatabase();
    	jdbcTemplate = new JdbcTemplate(db);
    	controller = new SettingsController(jdbcTemplate);
    }
    
    @After
    public void destroy() {
    	db.shutdown();
    }

    @Test
    public void settingsPage() {
    	ExtendedModelMap model = new ExtendedModelMap();
    	controller.settingsPage(new Account(1L, "Joe", "Schmoe", "joe@schmoe.com", "joe", "file://pic.jpg"), model);
    	List<Map<String, Object>> apps = (List<Map<String, Object>>) model.get("apps");
    	assertNotNull(apps);   
    	assertEquals(1, apps.size());
    	assertEquals("Greenhouse for the iPhone", apps.get(0).get("name"));
    	assertEquals("authme", apps.get(0).get("accessToken"));
    }
    
    @Test
    public void disconnectApp() {
    	assertEquals("redirect:/settings", controller.disconnectApp("authme", new Account(1L, "Joe", "Schmoe", "joe@schmoe.com", "joe", "file://pic.jpg")));
    	assertEquals(0, jdbcTemplate.queryForInt("select count(*) from ConnectedApp"));
    }
	
}

@@ -992,16 +992,40 @@
  %7B%0A    %09
+if (db != null) %7B%0A    %09%09
 db.shutd
@@ -1027,24 +1027,31 @@
 shutdown();%0A
+    %09%7D%0A
     %7D%0A%0A    @

test database builder