]> WPIA git - gigi.git/commitdiff
Adding domain Object (+ testcase)
authorFelix Dörre <felix@dogcraft.de>
Sat, 26 Jul 2014 13:24:18 +0000 (15:24 +0200)
committerFelix Dörre <felix@dogcraft.de>
Sat, 26 Jul 2014 13:24:18 +0000 (15:24 +0200)
doc/tableStructure.sql
src/org/cacert/gigi/Domain.java [new file with mode: 0644]
src/org/cacert/gigi/User.java
tests/org/cacert/gigi/TestDomain.java [new file with mode: 0644]

index d3cf2471a5ea2dd5f564e5c867bf9c5c2763bbb1..b266a39850db0a49157d0ff67d73cda0c7ab351e 100644 (file)
@@ -31,6 +31,20 @@ CREATE TABLE `users` (
 ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
 
 
+DROP TABLE IF EXISTS `domain`;
+CREATE TABLE `domain` (
+  `id` int(11) NOT NULL AUTO_INCREMENT,
+  `memid` int(11) NOT NULL,
+  `domain` varchar(255) NOT NULL,
+  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+  `modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
+  `deleted` datetime DEFAULT NULL,
+  PRIMARY KEY (`id`),
+  KEY `memid` (`memid`),
+  KEY `domain` (`domain`),
+  KEY `stats_domains_deleted` (`deleted`)
+) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=latin1;
+
 DROP TABLE IF EXISTS `email`;
 CREATE TABLE `email` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
diff --git a/src/org/cacert/gigi/Domain.java b/src/org/cacert/gigi/Domain.java
new file mode 100644 (file)
index 0000000..2aca793
--- /dev/null
@@ -0,0 +1,110 @@
+package org.cacert.gigi;
+
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import org.cacert.gigi.database.DatabaseConnection;
+
+public class Domain {
+       User owner;
+       String suffix;
+       int id;
+
+       public Domain(int id) throws SQLException {
+               PreparedStatement ps = DatabaseConnection.getInstance().prepare(
+                       "SELECT memid, domain FROM `domain` WHERE id=? AND deleted IS NULL");
+               ps.setInt(1, id);
+
+               ResultSet rs = ps.executeQuery();
+               if (!rs.next()) {
+                       throw new IllegalArgumentException("Invalid email id " + id);
+               }
+               this.id = id;
+               owner = User.getById(rs.getInt(1));
+               suffix = rs.getString(2);
+               rs.close();
+       }
+
+       public Domain(User owner, String suffix) throws GigiApiException {
+               this.owner = owner;
+               this.suffix = suffix;
+
+       }
+
+       private static void checkInsert(String suffix) throws GigiApiException {
+               try {
+                       PreparedStatement ps = DatabaseConnection
+                               .getInstance()
+                               .prepare(
+                                       "SELECT 1 FROM `domain` WHERE (domain=RIGHT(?,LENGTH(domain))  OR RIGHT(domain,LENGTH(?))=?) AND deleted IS NULL");
+                       ps.setString(1, suffix);
+                       ps.setString(2, suffix);
+                       ps.setString(3, suffix);
+                       ResultSet rs = ps.executeQuery();
+                       boolean existed = rs.next();
+                       rs.close();
+                       if (existed) {
+                               throw new GigiApiException("Domain could not be inserted. Domain is already valid.");
+                       }
+               } catch (SQLException e) {
+                       throw new GigiApiException(e);
+               }
+       }
+
+       public void insert() throws GigiApiException {
+               if (id != 0) {
+                       throw new GigiApiException("already inserted.");
+               }
+               synchronized (Domain.class) {
+                       checkInsert(suffix);
+                       try {
+                               PreparedStatement ps = DatabaseConnection.getInstance().prepare(
+                                       "INSERT INTO `domain` SET memid=?, domain=?");
+                               ps.setInt(1, owner.getId());
+                               ps.setString(2, suffix);
+                               ps.execute();
+                               id = DatabaseConnection.lastInsertId(ps);
+                       } catch (SQLException e) {
+                               throw new GigiApiException(e);
+                       }
+               }
+       }
+
+       public void delete() throws GigiApiException {
+               if (id == 0) {
+                       throw new GigiApiException("not inserted.");
+               }
+               try {
+                       PreparedStatement ps = DatabaseConnection.getInstance().prepare(
+                               "UPDATE `domain` SET deleted=CURRENT_TIMESTAMP WHERE id=?");
+                       ps.setInt(1, id);
+                       ps.execute();
+               } catch (SQLException e) {
+                       throw new GigiApiException(e);
+               }
+       }
+
+       public User getOwner() {
+               return owner;
+       }
+
+       public int getId() {
+               return id;
+       }
+
+       public String getSuffix() {
+               return suffix;
+       }
+
+       public static Domain getById(int id) throws IllegalArgumentException {
+               // TODO cache
+               try {
+                       Domain e = new Domain(id);
+                       return e;
+               } catch (SQLException e) {
+                       throw new IllegalArgumentException(e);
+               }
+       }
+
+}
index a2768de11feefa3903f2e26c6d1779eb9350a4f1..4cf318228bf311e679aa4b21840d8031bf92e89b 100644 (file)
@@ -269,6 +269,31 @@ public class User {
                return null;
        }
 
+       public Domain[] getDomains() {
+               try {
+                       PreparedStatement ps = DatabaseConnection.getInstance().prepare(
+                               "SELECT id FROM domain WHERE memid=? AND deleted IS NULL");
+                       ps.setInt(1, id);
+                       ResultSet rs = ps.executeQuery();
+                       rs.last();
+                       int count = rs.getRow();
+                       Domain[] data = new Domain[count];
+                       rs.beforeFirst();
+                       for (int i = 0; i < data.length; i++) {
+                               if (!rs.next()) {
+                                       throw new Error("Internal sql api violation.");
+                               }
+                               data[i] = Domain.getById(rs.getInt(1));
+                       }
+                       rs.close();
+                       return data;
+               } catch (SQLException e) {
+                       e.printStackTrace();
+               }
+
+               return null;
+       }
+
        public void updateDefaultEmail(EmailAddress newMail) throws GigiApiException {
                try {
                        EmailAddress[] adrs = getEmails();
diff --git a/tests/org/cacert/gigi/TestDomain.java b/tests/org/cacert/gigi/TestDomain.java
new file mode 100644 (file)
index 0000000..3eecd03
--- /dev/null
@@ -0,0 +1,99 @@
+package org.cacert.gigi;
+
+import static org.junit.Assert.*;
+
+import org.cacert.gigi.testUtils.ManagedTest;
+import org.junit.Test;
+
+public class TestDomain extends ManagedTest {
+       private User us;
+
+       public TestDomain() {
+               int uid = createVerifiedUser("fn", "ln", createUniqueName() + "pr@test-email.de", TEST_PASSWORD);
+               us = User.getById(uid);
+       }
+
+       @Test
+       public void testDomain() throws InterruptedException, GigiApiException {
+               assertEquals(0, us.getDomains().length);
+               Domain d = new Domain(us, "v1.example.org");
+               assertEquals(0, d.getId());
+               d.insert();
+               Domain[] domains = us.getDomains();
+               assertEquals(1, domains.length);
+               assertEquals("v1.example.org", domains[0].getSuffix());
+               assertEquals(domains[0].getOwner().getId(), us.getId());
+               assertNotEquals(0, domains[0].getId());
+               assertNotEquals(0, d.getId());
+               assertEquals(d.getId(), domains[0].getId());
+
+               Domain d2 = new Domain(us, "v2.example.org");
+               assertEquals(0, d2.getId());
+               d2.insert();
+
+               domains = us.getDomains();
+               assertEquals(2, domains.length);
+               assertEquals("v2.example.org", domains[1].getSuffix());
+               assertEquals(domains[0].getOwner().getId(), us.getId());
+               assertEquals(domains[1].getOwner().getId(), us.getId());
+               assertNotEquals(0, domains[0].getId());
+               assertNotEquals(0, d.getId());
+               assertEquals(d.getId(), domains[0].getId());
+
+       }
+
+       @Test
+       public void testDoubleDomain() throws InterruptedException, GigiApiException {
+               Domain d = new Domain(us, "dub.example.org");
+               d.insert();
+               try {
+                       Domain d2 = new Domain(us, "dub.example.org");
+                       d2.insert();
+                       fail("expected exception");
+               } catch (GigiApiException e) {
+                       // expected
+               }
+       }
+
+       @Test
+       public void testDoubleDomainDelete() throws InterruptedException, GigiApiException {
+               Domain d = new Domain(us, "del.example.org");
+               d.insert();
+               d.delete();
+               Domain d2 = new Domain(us, "del.example.org");
+               d2.insert();
+       }
+
+       @Test
+       public void testDoubleDomainPrefix() throws InterruptedException, GigiApiException {
+               Domain d = new Domain(us, "pref.aexample.org");
+               d.insert();
+               Domain d2 = new Domain(us, "a.pref.aexample.org");
+               try {
+                       d2.insert();
+                       fail("expected exception");
+               } catch (GigiApiException e) {
+                       // expected
+               }
+               Domain d3 = new Domain(us, "aexample.org");
+               try {
+                       d3.insert();
+                       fail("expected exception");
+               } catch (GigiApiException e) {
+                       // expected
+               }
+       }
+
+       @Test
+       public void testDoubleInsertDomain() throws InterruptedException, GigiApiException {
+               Domain d = new Domain(us, "dins.example.org");
+               d.insert();
+               try {
+                       d.insert();
+                       fail("expected exception");
+               } catch (GigiApiException e) {
+                       // expected
+               }
+       }
+
+}