diff options
| author | Trygve Laugstøl <trygvis@inamo.no> | 2012-12-22 19:33:23 +0100 | 
|---|---|---|
| committer | Trygve Laugstøl <trygvis@inamo.no> | 2012-12-22 19:33:23 +0100 | 
| commit | 78e7933c0af1d11d9476c5ee213c64ad87066f55 (patch) | |
| tree | b7d7f908bf24c5ce000dac9b6ff0dfb63c30b5ee /src | |
| parent | c8c863ce36f57954369a0b4a15e6c5e720f03f87 (diff) | |
| download | esper-testing-78e7933c0af1d11d9476c5ee213c64ad87066f55.tar.gz esper-testing-78e7933c0af1d11d9476c5ee213c64ad87066f55.tar.bz2 esper-testing-78e7933c0af1d11d9476c5ee213c64ad87066f55.tar.xz esper-testing-78e7933c0af1d11d9476c5ee213c64ad87066f55.zip | |
o First badge: UNBREAKABLE.
Diffstat (limited to 'src')
17 files changed, 538 insertions, 60 deletions
| diff --git a/src/main/java/io/trygvis/esper/testing/Daos.java b/src/main/java/io/trygvis/esper/testing/Daos.java index 967dae4..142efd6 100644 --- a/src/main/java/io/trygvis/esper/testing/Daos.java +++ b/src/main/java/io/trygvis/esper/testing/Daos.java @@ -29,7 +29,8 @@ public class Daos implements Closeable {      public final GitoriousProjectDao gitoriousProjectDao;      public final GitoriousRepositoryDao gitoriousRepositoryDao;      public final JenkinsDao jenkinsDao; -    public final CoreDao coreDao; +    public final PersonDao personDao; +    public final BuildDao buildDao;      public final int seq;      public static int counter = 1; @@ -40,7 +41,8 @@ public class Daos implements Closeable {          gitoriousProjectDao = new GitoriousProjectDao(c);          gitoriousRepositoryDao = new GitoriousRepositoryDao(c);          jenkinsDao = new JenkinsDao(c); -        coreDao = new CoreDao(c); +        personDao = new PersonDao(c); +        buildDao = new BuildDao(c);      }      public void close() throws IOException { diff --git a/src/main/java/io/trygvis/esper/testing/EntityRef.java b/src/main/java/io/trygvis/esper/testing/EntityRef.java index 6ed4147..a4d25cd 100644 --- a/src/main/java/io/trygvis/esper/testing/EntityRef.java +++ b/src/main/java/io/trygvis/esper/testing/EntityRef.java @@ -1,5 +1,6 @@  package io.trygvis.esper.testing; +import java.sql.*;  import java.util.*;  public abstract class EntityRef { @@ -11,6 +12,23 @@ public abstract class EntityRef {          this.type = type;      } +    public static EntityRef fromRs(ResultSet rs, int i) throws SQLException { +        String type = rs.getString(i++); + +        if (type == null) { +            throw new SQLException("reference type was null."); +        } + +        UUID uuid = UUID.fromString(rs.getString(i)); + +        switch (type) { +            case "jenkins": +                return new JenkinsRef(uuid); +            default: +                throw new SQLException("Unknown reference type: " + type); +        } +    } +      public static class JenkinsRef extends EntityRef {          private JenkinsRef(UUID uuid) {              super(uuid, "jenkins"); diff --git a/src/main/java/io/trygvis/esper/testing/core/TablePoller.java b/src/main/java/io/trygvis/esper/testing/core/TablePoller.java index 062f5e7..4c31ce5 100644 --- a/src/main/java/io/trygvis/esper/testing/core/TablePoller.java +++ b/src/main/java/io/trygvis/esper/testing/core/TablePoller.java @@ -21,6 +21,8 @@ public class TablePoller<A> {      private final SqlF<ResultSet, A> f;      private final NewRowCallback<A> callback; +    private boolean testMode; +      public TablePoller(String pollerName, String tableName, String columnNames, Option<String> filter, SqlF<ResultSet, A> f, NewRowCallback<A> callback) {          this.pollerName = pollerName;          this.tableName = tableName; @@ -69,17 +71,31 @@ public class TablePoller<A> {                      logger.debug("No new rows.");                  } -                Thread.sleep(10 * 1000); +//                if (testMode) { +//                    logger.info("TEST MODE: rolling back"); +//                    c.rollback(); +//                }                  dao.insertOrUpdate(o.isNone(), seq, new Timestamp(start), currentTimeMillis() - start, null); +                start = currentTimeMillis();                  c.commit(); +                long end = currentTimeMillis(); + +                logger.info("COMMIT performed in {}ms", end - start); + +                Thread.sleep(10 * 1000);              }          }      } +    public TablePoller testMode(boolean testMode) { +        this.testMode = testMode; +        return this; +    } +      public static interface NewRowCallback<A> { -        void process(Connection c, A A) throws SQLException; +        void process(Connection c, A A) throws Exception;      }      private class TablePollerDao { diff --git a/src/main/java/io/trygvis/esper/testing/core/Unbreakable.java b/src/main/java/io/trygvis/esper/testing/core/Unbreakable.java deleted file mode 100644 index 1200516..0000000 --- a/src/main/java/io/trygvis/esper/testing/core/Unbreakable.java +++ /dev/null @@ -1,4 +0,0 @@ -package io.trygvis.esper.testing.core; - -public class Unbreakable { -} diff --git a/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakableBadge.java b/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakableBadge.java new file mode 100644 index 0000000..a4f1e54 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakableBadge.java @@ -0,0 +1,14 @@ +package io.trygvis.esper.testing.core.badge; + +class UnbreakableBadge { +    // Configuration for this badge +    public static final int LEVEL_1_COUNT = 10; +    public static final int LEVEL_2_COUNT = 20; +    public static final int LEVEL_3_COUNT = 50; + +    public final int level; + +    UnbreakableBadge(int level) { +        this.level = level; +    } +} diff --git a/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakableBadgeProgress.java b/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakableBadgeProgress.java new file mode 100644 index 0000000..871be25 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakableBadgeProgress.java @@ -0,0 +1,57 @@ +package io.trygvis.esper.testing.core.badge; + +import fj.*; +import fj.data.*; +import io.trygvis.esper.testing.core.db.*; + +import java.util.*; + +import static fj.P.p; +import static fj.data.Option.some; + +class UnbreakableBadgeProgress { +    public final UUID person; +    public final int count; + +    private UnbreakableBadgeProgress(UUID person, int count) { +        this.person = person; +        this.count = count; +    } + +    @SuppressWarnings("UnusedDeclaration") +    private UnbreakableBadgeProgress() { +        person = null; +        count = -1; +    } + +    public static UnbreakableBadgeProgress initial(UUID person) { +        return new UnbreakableBadgeProgress(person, 0); +    } + +    public P2<UnbreakableBadgeProgress, Option<UnbreakableBadge>> onBuild(BuildDto build) { +        if (!build.success) { +            return p(initial(person), Option.<UnbreakableBadge>none()); +        } + +        int count = this.count + 1; + +        if (count == UnbreakableBadge.LEVEL_3_COUNT) { +            return p(initial(person), some(new UnbreakableBadge(3))); +        } + +        if (count == UnbreakableBadge.LEVEL_2_COUNT) { +            return p(new UnbreakableBadgeProgress(person, count), some(new UnbreakableBadge(2))); +        } + +        if (count == UnbreakableBadge.LEVEL_1_COUNT) { +            return p(new UnbreakableBadgeProgress(person, count), some(new UnbreakableBadge(1))); +        } + +        return p(new UnbreakableBadgeProgress(person, count), Option.<UnbreakableBadge>none()); +    } + +    @Override +    public String toString() { +        return "UnbreakableBadgeProgress{person=" + person + ", count=" + count + '}'; +    } +} diff --git a/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakablePoller.java b/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakablePoller.java new file mode 100644 index 0000000..38baa73 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakablePoller.java @@ -0,0 +1,109 @@ +package io.trygvis.esper.testing.core.badge; + +import com.jolbox.bonecp.*; +import fj.*; +import fj.data.*; +import io.trygvis.esper.testing.*; +import io.trygvis.esper.testing.core.*; +import io.trygvis.esper.testing.core.db.*; +import io.trygvis.esper.testing.util.sql.*; +import org.codehaus.jackson.map.*; +import org.slf4j.*; + +import java.io.*; +import java.sql.*; +import java.util.List; +import java.util.*; + +import static io.trygvis.esper.testing.Config.*; +import static io.trygvis.esper.testing.core.db.PersonBadgeDto.Type.*; + +public class UnbreakablePoller implements TablePoller.NewRowCallback<BuildDto> { +    Logger logger = LoggerFactory.getLogger(getClass()); + +    private static final ObjectMapper objectMapper = new ObjectMapper(); + +    public static void main(String[] args) throws Exception { +        String pollerName = "unbreakable"; +        String tableName = "build"; +        String columnNames = BuildDao.BUILD; +        SqlF<ResultSet, BuildDto> f = BuildDao.build; +        TablePoller.NewRowCallback<BuildDto> callback = new UnbreakablePoller(); + +        Config config = loadFromDisk(); + +        BoneCPDataSource dataSource = config.createBoneCp(); + +        new TablePoller<>(pollerName, tableName, columnNames, Option.<String>none(), f, callback). +                testMode(true). +                work(dataSource); +    } + +    public void process(Connection c, BuildDto build) throws SQLException { +        Daos daos = new Daos(c); + +        List<UUID> persons = daos.buildDao.selectPersonsFromBuildParticipant(build.uuid); +        logger.info("Processing build={}, #persons={}", build.uuid, persons.size()); + +        for (UUID person : persons) { +            logger.info("person={}", person); + +            SqlOption<PersonBadgeProgressDto> o = daos.personDao.selectBadgeProgress(person, UNBREAKABLE); + +            UnbreakableBadgeProgress badge; + +            if (o.isNone()) { +                badge = UnbreakableBadgeProgress.initial(person); +                logger.info("New badge progress"); +                String state = serialize(badge); +                daos.personDao.insertBadgeProgress(person, UNBREAKABLE, state); +                continue; +            } + +            String state = o.get().state; +            try { +                badge = objectMapper.readValue(state, UnbreakableBadgeProgress.class); +            } catch (IOException e) { +                logger.error("Could not de-serialize badge state: {}", state); +                throw new RuntimeException(e); +            } + +            logger.info("Existing badge progress: count={}", person, badge.count); + +            P2<UnbreakableBadgeProgress, Option<UnbreakableBadge>> p = badge.onBuild(build); + +            badge = p._1(); + +            logger.info("New badge progress: count={}", person, badge.count); + +            if (p._2().isSome()) { +                UnbreakableBadge b = p._2().some(); + +                logger.info("New unbreakable badge: person={}, level={}", person, b.level); + +                SqlOption<PersonBadgeDto> option = daos.personDao.selectBadge(person, UNBREAKABLE, b.level); + +                if (option.isNone()) { +                    daos.personDao.insertBadge(person, UNBREAKABLE, b.level, 1); +                } else { +                    daos.personDao.incrementBadgeCount(person, UNBREAKABLE, b.level); +                } +            } + +            state = serialize(badge); + +            daos.personDao.updateBadgeProgress(person, UNBREAKABLE, state); +        } +    } + +    private String serialize(UnbreakableBadgeProgress badge) { +        try { +            CharArrayWriter writer = new CharArrayWriter(); +            objectMapper.writeValue(writer, badge); +            return writer.toString(); +        } catch (IOException e) { +            logger.error("Could not serialize badge.", e); +            throw new RuntimeException(e); +        } +    } +} diff --git a/src/main/java/io/trygvis/esper/testing/core/db/CoreDao.java b/src/main/java/io/trygvis/esper/testing/core/db/BuildDao.java index 5eab2c9..24ecfd3 100644 --- a/src/main/java/io/trygvis/esper/testing/core/db/CoreDao.java +++ b/src/main/java/io/trygvis/esper/testing/core/db/BuildDao.java @@ -7,46 +7,30 @@ import org.joda.time.*;  import java.sql.*;  import java.util.*; -import static io.trygvis.esper.testing.util.sql.SqlOption.*; +import static io.trygvis.esper.testing.util.sql.ResultSetF.*;  import static java.lang.System.*; -public class CoreDao { +public class BuildDao {      private final Connection c; -    public static final String PERSON = "uuid, created_date, name"; +    public static final String BUILD = "uuid, created_date, timestamp, success, reference_type, reference_uuid"; -    public static final SqlF<ResultSet, PersonDto> person = new SqlF<ResultSet, PersonDto>() { -        public PersonDto apply(ResultSet rs) throws SQLException { +    public static final SqlF<ResultSet, BuildDto> build = new SqlF<ResultSet, BuildDto>() { +        public BuildDto apply(ResultSet rs) throws SQLException {              int i = 1; -            return new PersonDto( +            return new BuildDto(                      UUID.fromString(rs.getString(i++)),                      new DateTime(rs.getTimestamp(i++).getTime()), -                    rs.getString(i)); +                    new DateTime(rs.getTimestamp(i++).getTime()), +                    rs.getBoolean(i++), +                    EntityRef.fromRs(rs, i));          }      }; -    public static final String BUILD = "uuid, created_date, timestamp, success, reference_type, reference_uuid"; - -    public CoreDao(Connection c) { +    public BuildDao(Connection c) {          this.c = c;      } -    public SqlOption<PersonDto> selectPerson(String id) throws SQLException { -        try (PreparedStatement s = c.prepareStatement("SELECT " + PERSON + " FROM person WHERE id=?")) { -            int i = 1; -            s.setString(i, id); -            return fromRs(s.executeQuery()).map(person); -        } -    } - -    public SqlOption<PersonDto> selectPersonByJenkinsUuid(UUID jenkinsUser) throws SQLException { -        try (PreparedStatement s = c.prepareStatement("SELECT " + PERSON + " FROM person WHERE uuid=(SELECT person FROM person_jenkins_user WHERE jenkins_user=?)")) { -            int i = 1; -            s.setString(i, jenkinsUser.toString()); -            return fromRs(s.executeQuery()).map(person); -        } -    } -      public UUID insertBuild(DateTime timestamp, boolean success, EntityRef ref) throws SQLException {          try (PreparedStatement s = c.prepareStatement("INSERT INTO build(" + BUILD + ") VALUES(?, ?, ?, ?, ?, ?)")) {              UUID uuid = UUID.randomUUID(); @@ -70,4 +54,12 @@ public class CoreDao {              s.executeUpdate();          }      } + +    public List<UUID> selectPersonsFromBuildParticipant(UUID build) throws SQLException { +        try (PreparedStatement s = c.prepareStatement("SELECT person FROM build_participant WHERE build=?")) { +            int i = 1; +            s.setString(i, build.toString()); +            return Util.toList(s, getUuid); +        } +    }  } diff --git a/src/main/java/io/trygvis/esper/testing/core/db/BuildDto.java b/src/main/java/io/trygvis/esper/testing/core/db/BuildDto.java index 5cc6b0a..305c93a 100644 --- a/src/main/java/io/trygvis/esper/testing/core/db/BuildDto.java +++ b/src/main/java/io/trygvis/esper/testing/core/db/BuildDto.java @@ -5,17 +5,15 @@ import org.joda.time.*;  import java.util.*; -class BuildDto extends AbstractEntity { +public class BuildDto extends AbstractEntity {      public final DateTime timestamp;      public final boolean success; -    public final UUID referenceUuid; -    public final String referenceType; +    public final EntityRef ref; -    BuildDto(UUID uuid, DateTime createdDate, DateTime timestamp, boolean success, UUID referenceUuid, String referenceType) { +    public BuildDto(UUID uuid, DateTime createdDate, DateTime timestamp, boolean success, EntityRef ref) {          super(uuid, createdDate);          this.timestamp = timestamp;          this.success = success; -        this.referenceUuid = referenceUuid; -        this.referenceType = referenceType; +        this.ref = ref;      }  } diff --git a/src/main/java/io/trygvis/esper/testing/core/db/PersonBadgeDto.java b/src/main/java/io/trygvis/esper/testing/core/db/PersonBadgeDto.java new file mode 100644 index 0000000..5bbe159 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/core/db/PersonBadgeDto.java @@ -0,0 +1,25 @@ +package io.trygvis.esper.testing.core.db; + +import io.trygvis.esper.testing.*; +import org.joda.time.*; + +import java.util.*; + +public class PersonBadgeDto extends AbstractEntity { +    public enum Type { +        UNBREAKABLE +    } + +    public final UUID person; +    public final Type type; +    public final int level; +    public final int count; + +    public PersonBadgeDto(UUID uuid, DateTime createdDate, UUID person, Type type, int level, int count) { +        super(uuid, createdDate); +        this.person = person; +        this.type = type; +        this.level = level; +        this.count = count; +    } +} diff --git a/src/main/java/io/trygvis/esper/testing/core/db/PersonBadgeProgressDto.java b/src/main/java/io/trygvis/esper/testing/core/db/PersonBadgeProgressDto.java new file mode 100644 index 0000000..e572bb2 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/core/db/PersonBadgeProgressDto.java @@ -0,0 +1,20 @@ +package io.trygvis.esper.testing.core.db; + +import io.trygvis.esper.testing.*; +import org.joda.time.*; + +import java.util.*; + +public class PersonBadgeProgressDto extends AbstractEntity { + +    public final UUID person; +    public final String badge; +    public final String state; + +    public PersonBadgeProgressDto(UUID uuid, DateTime createdDate, UUID person, String badge, String state) { +        super(uuid, createdDate); +        this.person = person; +        this.badge = badge; +        this.state = state; +    } +} diff --git a/src/main/java/io/trygvis/esper/testing/core/db/PersonDao.java b/src/main/java/io/trygvis/esper/testing/core/db/PersonDao.java new file mode 100644 index 0000000..2cb6e6a --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/core/db/PersonDao.java @@ -0,0 +1,154 @@ +package io.trygvis.esper.testing.core.db; + +import io.trygvis.esper.testing.util.sql.*; +import org.joda.time.*; + +import java.sql.*; +import java.util.*; + +import static io.trygvis.esper.testing.util.sql.SqlOption.*; +import static java.lang.System.*; + +public class PersonDao { +    private final Connection c; + +    public static final String PERSON = "uuid, created_date, name"; + +    public static final SqlF<ResultSet, PersonDto> person = new SqlF<ResultSet, PersonDto>() { +        public PersonDto apply(ResultSet rs) throws SQLException { +            int i = 1; +            return new PersonDto( +                    UUID.fromString(rs.getString(i++)), +                    new DateTime(rs.getTimestamp(i++).getTime()), +                    rs.getString(i)); +        } +    }; + +    public static final String PERSON_BADGE = "uuid, created_date, person, name, level, count"; + +    public static final SqlF<ResultSet, PersonBadgeDto> personBadge = new SqlF<ResultSet, PersonBadgeDto>() { +        public PersonBadgeDto apply(ResultSet rs) throws SQLException { +            int i = 1; +            return new PersonBadgeDto( +                    UUID.fromString(rs.getString(i++)), +                    new DateTime(rs.getTimestamp(i++).getTime()), +                    UUID.fromString(rs.getString(i++)), +                    PersonBadgeDto.Type.valueOf(rs.getString(i++)), +                    rs.getInt(i), +                    rs.getInt(i)); +        } +    }; + +    public static final String PERSON_BADGE_PROGRESS = "uuid, created_date, person, badge, state"; + +    public static final SqlF<ResultSet, PersonBadgeProgressDto> personBadgeProgress = new SqlF<ResultSet, PersonBadgeProgressDto>() { +        public PersonBadgeProgressDto apply(ResultSet rs) throws SQLException { +            int i = 1; +            return new PersonBadgeProgressDto( +                    UUID.fromString(rs.getString(i++)), +                    new DateTime(rs.getTimestamp(i++).getTime()), +                    UUID.fromString(rs.getString(i++)), +                    rs.getString(i++), +                    rs.getString(i)); +        } +    }; + +    public PersonDao(Connection c) { +        this.c = c; +    } + +    // ----------------------------------------------------------------------- +    // Person +    // ----------------------------------------------------------------------- + +    public SqlOption<PersonDto> selectPerson(String id) throws SQLException { +        try (PreparedStatement s = c.prepareStatement("SELECT " + PERSON + " FROM person WHERE id=?")) { +            int i = 1; +            s.setString(i, id); +            return fromRs(s.executeQuery()).map(person); +        } +    } + +    public SqlOption<PersonDto> selectPersonByJenkinsUuid(UUID jenkinsUser) throws SQLException { +        try (PreparedStatement s = c.prepareStatement("SELECT " + PERSON + " FROM person WHERE uuid=(SELECT person FROM person_jenkins_user WHERE jenkins_user=?)")) { +            int i = 1; +            s.setString(i, jenkinsUser.toString()); +            return fromRs(s.executeQuery()).map(person); +        } +    } + +    // ----------------------------------------------------------------------- +    // Badge +    // ----------------------------------------------------------------------- + +    public UUID insertBadge(UUID person, PersonBadgeDto.Type type, int level, int count) throws SQLException { +        try (PreparedStatement s = c.prepareStatement("INSERT INTO person_badge(" + PERSON_BADGE + ") VALUES(?, ?, ?, ?, ?, ?)")) { +            UUID uuid = UUID.randomUUID(); +            int i = 1; +            s.setString(i++, uuid.toString()); +            s.setTimestamp(i++, new Timestamp(currentTimeMillis())); +            s.setString(i++, person.toString()); +            s.setString(i++, type.toString()); +            s.setInt(i++, level); +            s.setInt(i, count); +            s.executeUpdate(); +            return uuid; +        } +    } + +    public void incrementBadgeCount(UUID person, PersonBadgeDto.Type type, int level) throws SQLException { +        try (PreparedStatement s = c.prepareStatement("UPDATE person_badge SET count=count+1 WHERE person=? AND name=? AND level=?")) { +            int i = 1; +            s.setString(i++, person.toString()); +            s.setString(i++, type.toString()); +            s.setInt(i, level); +            s.executeUpdate(); +        } +    } + +    public SqlOption<PersonBadgeDto> selectBadge(UUID person, PersonBadgeDto.Type type, int level) throws SQLException { +        try (PreparedStatement s = c.prepareStatement("SELECT " + PERSON_BADGE + " FROM person_badge WHERE person=? AND name=? AND level=?")) { +            int i = 1; +            s.setString(i++, person.toString()); +            s.setString(i++, type.toString()); +            s.setInt(i, level); +            return fromRs(s.executeQuery()).map(personBadge); +        } +    } + +    // ----------------------------------------------------------------------- +    // Badge Progress +    // ----------------------------------------------------------------------- + +    public SqlOption<PersonBadgeProgressDto> selectBadgeProgress(UUID person, PersonBadgeDto.Type type) throws SQLException { +        try (PreparedStatement s = c.prepareStatement("SELECT " + PERSON_BADGE_PROGRESS + " FROM person_badge_progress WHERE person=? AND badge=?")) { +            int i = 1; +            s.setString(i++, person.toString()); +            s.setString(i, type.toString()); +            return fromRs(s.executeQuery()).map(personBadgeProgress); +        } +    } + +    public void insertBadgeProgress(UUID person, PersonBadgeDto.Type type, String state) throws SQLException { +        try (PreparedStatement s = c.prepareStatement("INSERT INTO person_badge_progress (" + PERSON_BADGE_PROGRESS + ") VALUES(?, ?, ?, ?, ?)")) { +            UUID uuid = UUID.randomUUID(); +            int i = 1; +            s.setString(i++, uuid.toString()); +            s.setTimestamp(i++, new Timestamp(currentTimeMillis())); +            s.setString(i++, person.toString()); +            s.setString(i++, type.toString()); +            s.setString(i, state); +            s.executeUpdate(); +        } +    } + +    public void updateBadgeProgress(UUID person, PersonBadgeDto.Type type, String state) throws SQLException { +        try (PreparedStatement s = c.prepareStatement("UPDATE person_badge_progress SET state=? WHERE person=? AND badge=?")) { +            int i = 1; +            s.setString(i++, state); +            s.setString(i++, person.toString()); +            s.setString(i, type.toString()); +            s.executeUpdate(); +        } +    } +} diff --git a/src/main/java/io/trygvis/esper/testing/core/jenkins/JenkinsBuildPoller.java b/src/main/java/io/trygvis/esper/testing/core/jenkins/JenkinsBuildPoller.java index 90a4ef4..9f22166 100644 --- a/src/main/java/io/trygvis/esper/testing/core/jenkins/JenkinsBuildPoller.java +++ b/src/main/java/io/trygvis/esper/testing/core/jenkins/JenkinsBuildPoller.java @@ -11,9 +11,9 @@ import org.slf4j.*;  import java.sql.*;  import java.util.*; -import static fj.data.Option.some; +import static fj.data.Option.*;  import static io.trygvis.esper.testing.Config.*; -import static io.trygvis.esper.testing.EntityRef.jenkinsRef; +import static io.trygvis.esper.testing.EntityRef.*;  public class JenkinsBuildPoller implements TablePoller.NewRowCallback<JenkinsBuildDto> {      Logger logger = LoggerFactory.getLogger(getClass()); @@ -34,22 +34,30 @@ public class JenkinsBuildPoller implements TablePoller.NewRowCallback<JenkinsBui      public void process(Connection c, JenkinsBuildDto jenkinsBuild) throws SQLException {          Daos daos = new Daos(c); -        CoreDao coreDao = daos.coreDao; +        BuildDao buildDao = daos.buildDao; +        PersonDao personDao = daos.personDao; -        UUID uuid = coreDao.insertBuild(jenkinsBuild.timestamp, "SUCCESS".equals(jenkinsBuild.result), jenkinsRef(jenkinsBuild.uuid)); -        logger.info("Created build uuid={}", uuid); +        UUID uuid = buildDao.insertBuild(jenkinsBuild.timestamp, "SUCCESS".equals(jenkinsBuild.result), jenkinsRef(jenkinsBuild.uuid)); + +        int knownPersons = 0, unknownPersons = 0;          for (UUID user : jenkinsBuild.users) { -            SqlOption<PersonDto> personO = coreDao.selectPersonByJenkinsUuid(user); +            SqlOption<PersonDto> personO = personDao.selectPersonByJenkinsUuid(user);              // This happens if no one has claimed the user id. -            if(personO.isNone()) { +            if (personO.isNone()) { +                unknownPersons++;                  continue;              } +            knownPersons++; +              UUID person = personO.get().uuid;              logger.info("Created build participant, person={}", person); -            coreDao.insertBuildParticipant(uuid, person); +            buildDao.insertBuildParticipant(uuid, person);          } + +        logger.info("Created build uuid={}, #participants={}, #knownPersons={}, #unknonwnPersons={}", uuid, +                jenkinsBuild.users.length, knownPersons, unknownPersons);      }  } diff --git a/src/main/java/io/trygvis/esper/testing/util/sql/ResultSetF.java b/src/main/java/io/trygvis/esper/testing/util/sql/ResultSetF.java index 9e42242..7169372 100644 --- a/src/main/java/io/trygvis/esper/testing/util/sql/ResultSetF.java +++ b/src/main/java/io/trygvis/esper/testing/util/sql/ResultSetF.java @@ -1,6 +1,7 @@  package io.trygvis.esper.testing.util.sql;  import java.sql.*; +import java.util.*;  public class ResultSetF {      public static final SqlF<ResultSet, Integer> getInt = new SqlF<ResultSet, Integer>() { @@ -15,4 +16,10 @@ public class ResultSetF {              return rs.wasNull() ? null : i;          }      }; + +    public static final SqlF<ResultSet, UUID> getUuid = new SqlF<ResultSet, UUID>() { +        public UUID apply(ResultSet rs) throws SQLException { +            return UUID.fromString(rs.getString(1)); +        } +    };  } diff --git a/src/main/resources/ddl-core.sql b/src/main/resources/ddl-core.sql index 9dcdd09..a244236 100644 --- a/src/main/resources/ddl-core.sql +++ b/src/main/resources/ddl-core.sql @@ -2,6 +2,7 @@ BEGIN;  DROP TABLE IF EXISTS build_participant;  DROP TABLE IF EXISTS build; +DROP SEQUENCE IF EXISTS build_seq;  DROP TABLE IF EXISTS person_badge_progress;  DROP TABLE IF EXISTS person_badge;  DROP TABLE IF EXISTS person_jenkins_user; @@ -18,9 +19,9 @@ CREATE TABLE table_poller_status (  );  CREATE TABLE person ( -  uuid          CHAR(36)  NOT NULL, -  created_date  TIMESTAMP NOT NULL, -  name          VARCHAR(100), +  uuid         CHAR(36)  NOT NULL, +  created_date TIMESTAMP NOT NULL, +  name         VARCHAR(100),    CONSTRAINT pk_person PRIMARY KEY (uuid)  ); @@ -33,27 +34,44 @@ CREATE TABLE person_jenkins_user (    CONSTRAINT fk_person_jenkins_user__jenkins_user FOREIGN KEY (jenkins_user) REFERENCES jenkins_user (uuid)  ); +-- TODO: create a table with all badges? +  -- Badges received  CREATE TABLE person_badge ( -  uuid         CHAR(36)  NOT NULL, -  created_date TIMESTAMP NOT NULL, -  CONSTRAINT pk_person_badge PRIMARY KEY (uuid) +  uuid         CHAR(36)     NOT NULL, +  created_date TIMESTAMP    NOT NULL, + +  person       CHAR(36)     NOT NULL, +  name         VARCHAR(100) NOT NULL, +  level        INT          NOT NULL, +  count        INT          NOT NULL, + +  CONSTRAINT pk_person_badge PRIMARY KEY (uuid), +  CONSTRAINT uq_person_badge__person__name__level UNIQUE (person, name, level), +  CONSTRAINT fk_person_badge__person FOREIGN KEY (person) REFERENCES person (uuid)  );  -- Badges the person is working on  CREATE TABLE person_badge_progress ( -  uuid         CHAR(36)     NOT NULL, -  created_date TIMESTAMP    NOT NULL, +  uuid         CHAR(36)      NOT NULL, +  created_date TIMESTAMP     NOT NULL, -  name         VARCHAR(100) NOT NULL, +  person       CHAR(36)      NOT NULL, +  badge        VARCHAR(100)  NOT NULL, +  state        VARCHAR(8000) NOT NULL, -  CONSTRAINT pk_person_badge_progress PRIMARY KEY (uuid) +  CONSTRAINT pk_person_badge_progress PRIMARY KEY (uuid), +  CONSTRAINT fk_person_badge_progress__person FOREIGN KEY (person) REFERENCES person (uuid), +  CONSTRAINT uq_person_badge_progress__person_badge UNIQUE (person, badge)  ); +CREATE SEQUENCE build_seq; +  CREATE TABLE build (    uuid           CHAR(36)     NOT NULL,    created_date   TIMESTAMP    NOT NULL, +  seq            INT          NOT NULL DEFAULT nextval('build_seq'),    timestamp      TIMESTAMP    NOT NULL,    success        BOOL         NOT NULL, @@ -67,8 +85,8 @@ CREATE TABLE build_participant (    build  CHAR(36) NOT NULL,    person CHAR(36) NOT NULL,    CONSTRAINT pk_build_participant PRIMARY KEY (build, person), -  CONSTRAINT fk_build_participant_build FOREIGN KEY (build) REFERENCES build (uuid), -  CONSTRAINT fk_build_participant_person FOREIGN KEY (person) REFERENCES person (uuid) +  CONSTRAINT fk_build_participant__build FOREIGN KEY (build) REFERENCES build (uuid), +  CONSTRAINT fk_build_participant__person FOREIGN KEY (person) REFERENCES person (uuid)  );  COMMIT; diff --git a/src/main/resources/ddl-jenkins.sql b/src/main/resources/ddl-jenkins.sql index 94bfc4e..2a57ad6 100644 --- a/src/main/resources/ddl-jenkins.sql +++ b/src/main/resources/ddl-jenkins.sql @@ -30,6 +30,8 @@ CREATE TABLE jenkins_job (    CONSTRAINT uq_jenkins_job__url UNIQUE (url)  ); +CREATE SEQUENCE jenkins_build_seq; +  CREATE TABLE jenkins_build (    uuid         CHAR(36)      NOT NULL,    created_date TIMESTAMP     NOT NULL, diff --git a/src/test/java/io/trygvis/esper/testing/core/badge/UnbreakableBadgeProgressTest.java b/src/test/java/io/trygvis/esper/testing/core/badge/UnbreakableBadgeProgressTest.java new file mode 100644 index 0000000..42ca98d --- /dev/null +++ b/src/test/java/io/trygvis/esper/testing/core/badge/UnbreakableBadgeProgressTest.java @@ -0,0 +1,42 @@ +package io.trygvis.esper.testing.core.badge; + +import fj.*; +import fj.data.*; +import io.trygvis.esper.testing.core.db.*; +import junit.framework.*; +import org.joda.time.*; + +import java.util.*; +import java.util.List; + +import static java.util.UUID.*; + +public class UnbreakableBadgeProgressTest extends TestCase { +    UUID uuid = randomUUID(); + +    public void testBadge() { +        BuildDto build = new BuildDto(uuid, new DateTime(), new DateTime(), true, null); + +        UUID person = randomUUID(); + +        UnbreakableBadgeProgress p = UnbreakableBadgeProgress.initial(person); + +        List<UnbreakableBadge> badges = new ArrayList<>(); + +        for (int i = 0; i < 55; i++) { +            P2<UnbreakableBadgeProgress, Option<UnbreakableBadge>> p2 = p.onBuild(build); + +            if (p2._2().isSome()) { +                badges.add(p2._2().some()); +            } + +            p = p2._1(); +        } + +        assertEquals(5, p.count); +        assertEquals(3, badges.size()); +        assertEquals(1, badges.get(0).level); +        assertEquals(2, badges.get(1).level); +        assertEquals(3, badges.get(2).level); +    } +} | 
