Search in sources :

Example 1 with PhonebookSql

use of com.github.fabriciofx.cactoos.jdbc.phonebook.sql.PhonebookSql in project cactoos-jdbc by fabriciofx.

the class PhonebookTest method addContact.

@Test
public void addContact() throws Exception {
    try (Servers servers = new Servers(new ServerH2(new ScriptSql(new ResourceOf(new Joined("/", "com/github/fabriciofx/cactoos/jdbc/phonebook", "phonebook-h2.sql")))), new ServerPgsql(new ScriptSql(new ResourceOf(new Joined("/", "com/github/fabriciofx/cactoos/jdbc/phonebook", "phonebook-pgsql.sql")))))) {
        for (final Session session : servers.sessions()) {
            final Phonebook phonebook = new PhonebookSql(session);
            final Contact contact = phonebook.contact(new MapOf<String, String>(new MapEntry<>("name", "Donald Knuth")));
            contact.phones().add(new MapOf<String, String>(new MapEntry<>("number", "99991234"), new MapEntry<>("carrier", "TIM")));
            contact.phones().add(new MapOf<String, String>(new MapEntry<>("number", "98812564"), new MapEntry<>("carrier", "Oi")));
            MatcherAssert.assertThat(XhtmlMatchers.xhtml(contact.about()), XhtmlMatchers.hasXPaths("/contact/name[text()='Donald Knuth']", "/contact/phones/phone/number[text()='99991234']", "/contact/phones/phone/carrier[text()='TIM']", "/contact/phones/phone/number[text()='98812564']", "/contact/phones/phone/carrier[text()='Oi']"));
        }
    }
}
Also used : ServerH2(com.github.fabriciofx.cactoos.jdbc.server.ServerH2) MapEntry(org.cactoos.map.MapEntry) PhonebookSql(com.github.fabriciofx.cactoos.jdbc.phonebook.sql.PhonebookSql) Servers(com.github.fabriciofx.cactoos.jdbc.Servers) ServerPgsql(com.github.fabriciofx.cactoos.jdbc.server.ServerPgsql) Contact(com.github.fabriciofx.cactoos.jdbc.phonebook.Contact) ScriptSql(com.github.fabriciofx.cactoos.jdbc.script.ScriptSql) Phonebook(com.github.fabriciofx.cactoos.jdbc.phonebook.Phonebook) ResourceOf(org.cactoos.io.ResourceOf) Joined(org.cactoos.text.Joined) Session(com.github.fabriciofx.cactoos.jdbc.Session) Test(org.junit.Test)

Example 2 with PhonebookSql

use of com.github.fabriciofx.cactoos.jdbc.phonebook.sql.PhonebookSql in project cactoos-jdbc by fabriciofx.

the class StatementTransactionTest method rollback.

@Test
public void rollback() throws Exception {
    final Transacted transacted = new Transacted(new SessionNoAuth(new SourceH2("unsafedb")));
    new ScriptSql(new ResourceOf("com/github/fabriciofx/cactoos/jdbc/phonebook/phonebook-h2.sql")).run(transacted);
    final Phonebook phonebook = new PhonebookSql(transacted);
    final String name = "Frank Miller";
    try {
        new StatementTransaction<>(transacted, () -> {
            final Contact contact = phonebook.contact(new MapOf<String, String>(new MapEntry<>("name", name)));
            contact.phones().add(new MapOf<String, String>(new MapEntry<>("number", "99991234"), new MapEntry<>("carrier", "TIM")));
            throw new IllegalStateException("Rollback");
        }).result();
    } catch (final IllegalStateException ex) {
    }
    MatcherAssert.assertThat("Can't perform a transaction rollback", StreamSupport.stream(phonebook.filter(name).spliterator(), false).count(), Matchers.equalTo(0L));
}
Also used : PhonebookSql(com.github.fabriciofx.cactoos.jdbc.phonebook.sql.PhonebookSql) Contact(com.github.fabriciofx.cactoos.jdbc.phonebook.Contact) SourceH2(com.github.fabriciofx.cactoos.jdbc.source.SourceH2) ScriptSql(com.github.fabriciofx.cactoos.jdbc.script.ScriptSql) Phonebook(com.github.fabriciofx.cactoos.jdbc.phonebook.Phonebook) ResourceOf(org.cactoos.io.ResourceOf) MapOf(org.cactoos.map.MapOf) Transacted(com.github.fabriciofx.cactoos.jdbc.session.Transacted) SessionNoAuth(com.github.fabriciofx.cactoos.jdbc.session.SessionNoAuth) Test(org.junit.Test)

Example 3 with PhonebookSql

use of com.github.fabriciofx.cactoos.jdbc.phonebook.sql.PhonebookSql in project cactoos-jdbc by fabriciofx.

the class PhonebookTest method renameContact.

@Test
public void renameContact() throws Exception {
    try (Servers servers = new Servers(new ServerH2(new ScriptSql(new ResourceOf(new Joined("/", "com/github/fabriciofx/cactoos/jdbc/phonebook", "phonebook-h2.sql")))), new ServerPgsql(new ScriptSql(new ResourceOf(new Joined("/", "com/github/fabriciofx/cactoos/jdbc/phonebook", "phonebook-pgsql.sql")))))) {
        for (final Session session : servers.sessions()) {
            final Phonebook phonebook = new PhonebookSql(session);
            final Contact contact = phonebook.filter("maria").iterator().next();
            contact.update(new MapOf<String, String>(new MapEntry<>("name", "Maria Lima")));
            MatcherAssert.assertThat(XhtmlMatchers.xhtml(new PhonebookSql(session).filter("maria").iterator().next().about()), XhtmlMatchers.hasXPaths("/contact/name[text()='Maria Lima']"));
        }
    }
}
Also used : ServerH2(com.github.fabriciofx.cactoos.jdbc.server.ServerH2) MapEntry(org.cactoos.map.MapEntry) PhonebookSql(com.github.fabriciofx.cactoos.jdbc.phonebook.sql.PhonebookSql) Servers(com.github.fabriciofx.cactoos.jdbc.Servers) ServerPgsql(com.github.fabriciofx.cactoos.jdbc.server.ServerPgsql) Contact(com.github.fabriciofx.cactoos.jdbc.phonebook.Contact) ScriptSql(com.github.fabriciofx.cactoos.jdbc.script.ScriptSql) Phonebook(com.github.fabriciofx.cactoos.jdbc.phonebook.Phonebook) ResourceOf(org.cactoos.io.ResourceOf) Joined(org.cactoos.text.Joined) Session(com.github.fabriciofx.cactoos.jdbc.Session) Test(org.junit.Test)

Example 4 with PhonebookSql

use of com.github.fabriciofx.cactoos.jdbc.phonebook.sql.PhonebookSql in project cactoos-jdbc by fabriciofx.

the class StatementTransactionTest method commit.

@Test
public void commit() throws Exception {
    final Transacted transacted = new Transacted(new SessionNoAuth(new SourceH2("safedb")));
    new ScriptSql(new ResourceOf("com/github/fabriciofx/cactoos/jdbc/phonebook/phonebook-h2.sql")).run(transacted);
    MatcherAssert.assertThat("Can't perform a transaction commit", XhtmlMatchers.xhtml(new ResultAsValue<>(new StatementTransaction<>(transacted, () -> {
        final Phonebook phonebook = new PhonebookSql(transacted);
        final Contact contact = phonebook.contact(new MapOf<String, String>(new MapEntry<>("name", "Albert Einstein")));
        contact.phones().add(new MapOf<String, String>(new MapEntry<>("number", "99991234"), new MapEntry<>("carrier", "TIM")));
        contact.phones().add(new MapOf<String, String>(new MapEntry<>("number", "98812564"), new MapEntry<>("carrier", "Oi")));
        return contact.about();
    })).value()), XhtmlMatchers.hasXPaths("/contact/name[text()='Albert Einstein']", "/contact/phones/phone/number[text()='99991234']", "/contact/phones/phone/carrier[text()='TIM']", "/contact/phones/phone/number[text()='98812564']", "/contact/phones/phone/carrier[text()='Oi']"));
}
Also used : MapEntry(org.cactoos.map.MapEntry) PhonebookSql(com.github.fabriciofx.cactoos.jdbc.phonebook.sql.PhonebookSql) Contact(com.github.fabriciofx.cactoos.jdbc.phonebook.Contact) SourceH2(com.github.fabriciofx.cactoos.jdbc.source.SourceH2) ScriptSql(com.github.fabriciofx.cactoos.jdbc.script.ScriptSql) Phonebook(com.github.fabriciofx.cactoos.jdbc.phonebook.Phonebook) ResourceOf(org.cactoos.io.ResourceOf) Transacted(com.github.fabriciofx.cactoos.jdbc.session.Transacted) SessionNoAuth(com.github.fabriciofx.cactoos.jdbc.session.SessionNoAuth) Test(org.junit.Test)

Aggregations

Contact (com.github.fabriciofx.cactoos.jdbc.phonebook.Contact)4 Phonebook (com.github.fabriciofx.cactoos.jdbc.phonebook.Phonebook)4 PhonebookSql (com.github.fabriciofx.cactoos.jdbc.phonebook.sql.PhonebookSql)4 ScriptSql (com.github.fabriciofx.cactoos.jdbc.script.ScriptSql)4 ResourceOf (org.cactoos.io.ResourceOf)4 Test (org.junit.Test)4 MapEntry (org.cactoos.map.MapEntry)3 Servers (com.github.fabriciofx.cactoos.jdbc.Servers)2 Session (com.github.fabriciofx.cactoos.jdbc.Session)2 ServerH2 (com.github.fabriciofx.cactoos.jdbc.server.ServerH2)2 ServerPgsql (com.github.fabriciofx.cactoos.jdbc.server.ServerPgsql)2 SessionNoAuth (com.github.fabriciofx.cactoos.jdbc.session.SessionNoAuth)2 Transacted (com.github.fabriciofx.cactoos.jdbc.session.Transacted)2 SourceH2 (com.github.fabriciofx.cactoos.jdbc.source.SourceH2)2 Joined (org.cactoos.text.Joined)2 MapOf (org.cactoos.map.MapOf)1