package com.google.appengine.codelab;
import static org.junit.Assert.*;
import java.util.Iterator;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.PreparedQuery;
public class SessaoTests extends BaseTest {
@Before
public void setUp() throws Exception {
super.setUp();
Localidade.createOrUpdateLocalidade("1", new Float(-13.58), new Float(-51.85),
"40 Pine St.", "Houston", "TX", "USA", "79754");
Cliente.createOrUpdateCliente("1", "joao", "Joao", "Costa", "11 3245-8873",
"134.543.544-98", "joao@gmail.com", "1");
Cliente.createOrUpdateCliente("2", "jose", "JOSE", "SILVA", "31 8735-3245",
"514.543.343-43", "jose@hotmail.com", "1");
Cliente.createOrUpdateCliente("3", "maria", "MARIA", "BENEDITA", "21 4344-4256",
"654.123.546-63", "maria@yahoo.com.br", "1");
SistemaOperacional sis = new SistemaOperacional();
sis.createOrUpdate(1, "Windows", "8", "0");
sis.createOrUpdate(2, "MacOs", "10.7", "0");
sis.createOrUpdate(3, "iOS", "6.1", "1");
Navegador nav = new Navegador();
nav.createOrUpdate(1, "Chrome", "27.0");
nav.createOrUpdate(2, "Firefox", "21.0");
nav.createOrUpdate(3, "Internet Explorer", "9.0");
Sessao ses = new Sessao();
ses.createOrUpdate(1, "1", 1, 1, "20130601", "01:30:00");
ses.createOrUpdate(2, "2", 2, 2, "20130602", "02:30:00");
ses.createOrUpdate(3, "3", 3, 3, "20130603", "03:30:00");
}
@After
public void tearDown() throws Exception {
Iterator<Entity> iterator = (new Sessao()).getAll().asIterator();
// Delete all entities created during tests
while (iterator.hasNext()) {
Entity entity = iterator.next();
Util.deleteEntity(entity.getKey());
}
super.tearDown();
}
@Test
public final void testCreateOrUpdate() {
Sessao ses = new Sessao();
// Check inserted values
assertEquals(3, ses.getAll().countEntities(FetchOptions.Builder.withDefaults()));
// Insert new Sessao
ses.createOrUpdate(4, "1", 2, 3, "20130304", "04:30:00");
assertEquals(4, ses.getAll().countEntities(FetchOptions.Builder.withDefaults()));
// Update existing Sessao
ses.createOrUpdate(4, "1", 2, 2, "20130304", "04:30:00");
assertEquals(4, ses.getAll().countEntities(FetchOptions.Builder.withDefaults()));
// Check if value was updated
Entity sessao = ses.get(4);
assertEquals(2l, sessao.getProperty("nav_id"));
}
@Test
public final void testGetAll() {
Sessao ses = new Sessao();
// Check inserted values
assertEquals(3, ses.getAll().countEntities(FetchOptions.Builder.withDefaults()));
}
@Test
public final void testGet() {
Entity sessao = (new Sessao()).get(1);
// Check all properties
assertEquals("1", sessao.getProperty("cliente_id"));
assertEquals(1l, sessao.getProperty("sis_id"));
assertEquals(1l, sessao.getProperty("nav_id"));
assertEquals("20130601", sessao.getProperty("ses_data"));
assertEquals("01:30:00", sessao.getProperty("ses_hora"));
}
@Test
public final void testGetBySisId() {
Sessao ses = new Sessao();
// Check returned query
PreparedQuery sessaoById = ses.getBySisId(1);
assertEquals(1, sessaoById.countEntities(FetchOptions.Builder.withDefaults()));
// Check all properties
Entity sessao = sessaoById.asList(FetchOptions.Builder.withDefaults()).get(0);
assertEquals("1", sessao.getProperty("cliente_id"));
assertEquals(1l, sessao.getProperty("sis_id"));
assertEquals(1l, sessao.getProperty("nav_id"));
assertEquals("20130601", sessao.getProperty("ses_data"));
assertEquals("01:30:00", sessao.getProperty("ses_hora"));
}
@Test
public final void testDelete() {
Sessao ses = new Sessao();
// Check inserted values
assertEquals(3, ses.getAll().countEntities(FetchOptions.Builder.withDefaults()));
// Delete one value
ses.delete(3);
assertEquals(2, ses.getAll().countEntities(FetchOptions.Builder.withDefaults()));
}
}