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"));
    }
	
}

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() {
    	if (db != null) {
    		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"));
    }
	
}

test database builder