Fasel/fasel-jfx/src/test/java/de/vanitasvitae/fasel/database/AccountDatabaseTest.java

70 lines
2.5 KiB
Java

package de.vanitasvitae.fasel.database;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNotNull;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import de.vanitasvitae.fasel.TestUtils;
import de.vanitasvitae.fasel.db.DatabaseService;
import de.vanitasvitae.fasel.db.H2DatabaseCreator;
import de.vanitasvitae.fasel.db.databases.AccountDatabase;
import de.vanitasvitae.fasel.db.entity.Account;
import de.vanitasvitae.fasel.db.entity.Contact;
import de.vanitasvitae.fasel.db.entity.XmppEntity;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class AccountDatabaseTest {
private final AccountDatabase database;
public AccountDatabaseTest() throws SQLException {
File file = TestUtils.getTempDir("accounttest_");
DatabaseService.setDatabaseCreator(new H2DatabaseCreator(file));
database = new AccountDatabase();
}
@Before
public void populate() throws SQLException {
Account account = new Account("alice", "wonderland.lit","secret");
database.getAccountDao().createIfNotExists(account);
assertEquals(account, database.getAccountDao().getAccounts().get(0));
Contact contact = new Contact("bob@builder.tv", "Bob the Builder");
contact.setAccount(account);
database.getContactDao().createIfNotExists(contact);
contact = new Contact("buggy@builder.tv", "Buggy");
contact.setAccount(account);
database.getContactDao().createIfNotExists(contact);
contact = new Contact("buggy@builder.tv", "Buggy");
contact.setAccount(account);
database.getContactDao().createIfNotExists(contact);
database.getXmppEntityDao().queryForAll().forEach(System.out::println);
assertEquals(2, database.getContactDao().getContactsOf(account).size());
}
@Test
public void queryContactTest() throws SQLException {
Account account = database.getAccountDao().getAccounts().get(0);
List<Contact> contacts = database.getContactDao().getAllContacts();
contacts.forEach(contact -> System.out.println(contact.toString()));
assertEquals(2, database.getContactDao().getAllContacts().size());
assertEquals(2, database.getContactDao().getContactsOf(account).size());
assertEquals(1, database.getContactDao().getContactsByJid("bob@builder.tv").size());
}
@After
public void closeConnection() throws IOException {
database.getConnectionSource().close();
}
}