From 35ba1a6f765f72a1d91a5456c7556e6274f8c3a9 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Thu, 3 Jan 2013 15:09:43 +0100 Subject: o Moving Jenkins XML objects and parsing to it's own package. o Removing fields from JenkinsBuild that's read from the XML later on. --- .../io/trygvis/esper/testing/AbstractEntity.java | 0 src/main/java/io/trygvis/esper/testing/Config.java | 10 +- src/main/java/io/trygvis/esper/testing/Daos.java | 6 + .../java/io/trygvis/esper/testing/EntityRef.java | 18 +- src/main/java/io/trygvis/esper/testing/Util.java | 0 .../io/trygvis/esper/testing/core/TablePoller.java | 0 .../testing/core/badge/UnbreakablePoller.java | 0 .../io/trygvis/esper/testing/core/db/BuildDao.java | 9 + .../io/trygvis/esper/testing/core/db/FileDao.java | 14 ++ .../trygvis/esper/testing/core/db/PersonDao.java | 49 ++++- .../trygvis/esper/testing/core/db/PersonDto.java | 4 +- .../testing/core/jenkins/JenkinsBuildPoller.java | 52 ++++- .../esper/testing/jenkins/JenkinsBuildDto.java | 16 +- .../esper/testing/jenkins/JenkinsBuildXml.java | 201 -------------------- .../esper/testing/jenkins/JenkinsClient.java | 50 +---- .../trygvis/esper/testing/jenkins/JenkinsDao.java | 28 +-- .../trygvis/esper/testing/jenkins/JenkinsJob.java | 2 + .../esper/testing/jenkins/JenkinsJobXml.java | 88 --------- .../esper/testing/jenkins/JenkinsServerActor.java | 5 +- .../esper/testing/jenkins/JenkinsServerDto.java | 4 + .../esper/testing/jenkins/JenkinsServerOld.java | 1 + .../esper/testing/jenkins/xml/JenkinsBuildXml.java | 210 +++++++++++++++++++++ .../esper/testing/jenkins/xml/JenkinsEntryXml.java | 17 ++ .../esper/testing/jenkins/xml/JenkinsJobXml.java | 88 +++++++++ .../esper/testing/jenkins/xml/JenkinsXml.java | 31 +++ .../esper/testing/util/EasyX509TrustManager.java | 0 .../io/trygvis/esper/testing/util/XmlParser.java | 8 +- .../trygvis/esper/testing/util/sql/ResultSetF.java | 0 .../io/trygvis/esper/testing/util/sql/SqlF.java | 0 .../trygvis/esper/testing/util/sql/SqlOption.java | 22 +++ .../io/trygvis/esper/testing/util/sql/SqlP0.java | 7 + .../esper/testing/web/resource/CoreResource.java | 2 +- .../testing/web/resource/JenkinsResource.java | 26 ++- 33 files changed, 574 insertions(+), 394 deletions(-) mode change 100644 => 100755 src/main/java/io/trygvis/esper/testing/AbstractEntity.java mode change 100644 => 100755 src/main/java/io/trygvis/esper/testing/Config.java mode change 100644 => 100755 src/main/java/io/trygvis/esper/testing/Daos.java mode change 100644 => 100755 src/main/java/io/trygvis/esper/testing/EntityRef.java mode change 100644 => 100755 src/main/java/io/trygvis/esper/testing/Util.java mode change 100644 => 100755 src/main/java/io/trygvis/esper/testing/core/TablePoller.java mode change 100644 => 100755 src/main/java/io/trygvis/esper/testing/core/badge/UnbreakablePoller.java mode change 100644 => 100755 src/main/java/io/trygvis/esper/testing/core/db/BuildDao.java mode change 100644 => 100755 src/main/java/io/trygvis/esper/testing/core/db/FileDao.java mode change 100644 => 100755 src/main/java/io/trygvis/esper/testing/core/db/PersonDao.java mode change 100644 => 100755 src/main/java/io/trygvis/esper/testing/core/db/PersonDto.java mode change 100644 => 100755 src/main/java/io/trygvis/esper/testing/core/jenkins/JenkinsBuildPoller.java mode change 100644 => 100755 src/main/java/io/trygvis/esper/testing/jenkins/JenkinsBuildDto.java delete mode 100644 src/main/java/io/trygvis/esper/testing/jenkins/JenkinsBuildXml.java mode change 100644 => 100755 src/main/java/io/trygvis/esper/testing/jenkins/JenkinsClient.java mode change 100644 => 100755 src/main/java/io/trygvis/esper/testing/jenkins/JenkinsDao.java mode change 100644 => 100755 src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJob.java delete mode 100644 src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJobXml.java mode change 100644 => 100755 src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerActor.java mode change 100644 => 100755 src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerDto.java mode change 100644 => 100755 src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerOld.java create mode 100755 src/main/java/io/trygvis/esper/testing/jenkins/xml/JenkinsBuildXml.java create mode 100755 src/main/java/io/trygvis/esper/testing/jenkins/xml/JenkinsEntryXml.java create mode 100755 src/main/java/io/trygvis/esper/testing/jenkins/xml/JenkinsJobXml.java create mode 100755 src/main/java/io/trygvis/esper/testing/jenkins/xml/JenkinsXml.java mode change 100644 => 100755 src/main/java/io/trygvis/esper/testing/util/EasyX509TrustManager.java mode change 100644 => 100755 src/main/java/io/trygvis/esper/testing/util/XmlParser.java mode change 100644 => 100755 src/main/java/io/trygvis/esper/testing/util/sql/ResultSetF.java mode change 100644 => 100755 src/main/java/io/trygvis/esper/testing/util/sql/SqlF.java mode change 100644 => 100755 src/main/java/io/trygvis/esper/testing/util/sql/SqlOption.java create mode 100755 src/main/java/io/trygvis/esper/testing/util/sql/SqlP0.java mode change 100644 => 100755 src/main/java/io/trygvis/esper/testing/web/resource/CoreResource.java mode change 100644 => 100755 src/main/java/io/trygvis/esper/testing/web/resource/JenkinsResource.java (limited to 'src/main/java/io/trygvis/esper') diff --git a/src/main/java/io/trygvis/esper/testing/AbstractEntity.java b/src/main/java/io/trygvis/esper/testing/AbstractEntity.java old mode 100644 new mode 100755 diff --git a/src/main/java/io/trygvis/esper/testing/Config.java b/src/main/java/io/trygvis/esper/testing/Config.java old mode 100644 new mode 100755 index d1bc2bd..e184d46 --- a/src/main/java/io/trygvis/esper/testing/Config.java +++ b/src/main/java/io/trygvis/esper/testing/Config.java @@ -41,6 +41,7 @@ public class Config { } } + public final String appName; public final GitoriousConfig gitorious; public final long nexusUpdateInterval; @@ -53,8 +54,10 @@ public class Config { private BoneCPDataSource dataSource; - public Config(GitoriousConfig gitorious, long nexusUpdateInterval, long jenkinsUpdateInterval, String databaseUrl, - String databaseUsername, String databasePassword) { + public Config(String appName, GitoriousConfig gitorious, + long nexusUpdateInterval, long jenkinsUpdateInterval, + String databaseUrl, String databaseUsername, String databasePassword) { + this.appName = appName; this.gitorious = gitorious; this.nexusUpdateInterval = nexusUpdateInterval; this.jenkinsUpdateInterval = jenkinsUpdateInterval; @@ -73,7 +76,8 @@ public class Config { properties.load(inputStream); } - return new Config(GitoriousConfig.fromProperties(properties), + return new Config(appName, + GitoriousConfig.fromProperties(properties), getProperty(properties, "nexus.updateInterval").bind(parseInt).valueE("Missing required property: nexus.updateInterval") * 1000, getProperty(properties, "jenkins.updateInterval").bind(parseInt).valueE("Missing required property: jenkins.updateInterval") * 1000, trimToNull(properties.getProperty("database.url")), diff --git a/src/main/java/io/trygvis/esper/testing/Daos.java b/src/main/java/io/trygvis/esper/testing/Daos.java old mode 100644 new mode 100755 index 142efd6..1a7cb0b --- a/src/main/java/io/trygvis/esper/testing/Daos.java +++ b/src/main/java/io/trygvis/esper/testing/Daos.java @@ -25,6 +25,7 @@ public class Daos implements Closeable { } private final Connection connection; + public final FileDao fileDao; public final GitoriousEventDao gitoriousEventDao; public final GitoriousProjectDao gitoriousProjectDao; public final GitoriousRepositoryDao gitoriousRepositoryDao; @@ -37,6 +38,7 @@ public class Daos implements Closeable { public Daos(Connection c) throws SQLException { this.connection = c; this.seq = counter++; + fileDao = new FileDao(c); gitoriousEventDao = new GitoriousEventDao(c); gitoriousProjectDao = new GitoriousProjectDao(c); gitoriousRepositoryDao = new GitoriousRepositoryDao(c); @@ -58,4 +60,8 @@ public class Daos implements Closeable { public void commit() throws SQLException { connection.commit(); } + + public void rollback() throws SQLException { + connection.rollback(); + } } diff --git a/src/main/java/io/trygvis/esper/testing/EntityRef.java b/src/main/java/io/trygvis/esper/testing/EntityRef.java old mode 100644 new mode 100755 index a4d25cd..cafcdd5 --- a/src/main/java/io/trygvis/esper/testing/EntityRef.java +++ b/src/main/java/io/trygvis/esper/testing/EntityRef.java @@ -3,6 +3,8 @@ package io.trygvis.esper.testing; import java.sql.*; import java.util.*; +import static io.trygvis.esper.testing.EntityRef.JenkinsBuildRef.*; + public abstract class EntityRef { public final UUID uuid; public final String type; @@ -22,20 +24,22 @@ public abstract class EntityRef { UUID uuid = UUID.fromString(rs.getString(i)); switch (type) { - case "jenkins": - return new JenkinsRef(uuid); + case JENKINS_BUILD_REF: + return new JenkinsBuildRef(uuid); default: throw new SQLException("Unknown reference type: " + type); } } - public static class JenkinsRef extends EntityRef { - private JenkinsRef(UUID uuid) { - super(uuid, "jenkins"); + public static class JenkinsBuildRef extends EntityRef { + public static final String JENKINS_BUILD_REF = "jenkins-build"; + + private JenkinsBuildRef(UUID uuid) { + super(uuid, JENKINS_BUILD_REF); } } - public static JenkinsRef jenkinsRef(UUID uuid) { - return new JenkinsRef(uuid); + public static JenkinsBuildRef jenkinsBuildRef(UUID uuid) { + return new JenkinsBuildRef(uuid); } } diff --git a/src/main/java/io/trygvis/esper/testing/Util.java b/src/main/java/io/trygvis/esper/testing/Util.java old mode 100644 new mode 100755 diff --git a/src/main/java/io/trygvis/esper/testing/core/TablePoller.java b/src/main/java/io/trygvis/esper/testing/core/TablePoller.java old mode 100644 new mode 100755 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 old mode 100644 new mode 100755 diff --git a/src/main/java/io/trygvis/esper/testing/core/db/BuildDao.java b/src/main/java/io/trygvis/esper/testing/core/db/BuildDao.java old mode 100644 new mode 100755 index 416faf6..c7923d0 --- a/src/main/java/io/trygvis/esper/testing/core/db/BuildDao.java +++ b/src/main/java/io/trygvis/esper/testing/core/db/BuildDao.java @@ -81,6 +81,15 @@ public class BuildDao { } } + public SqlOption findBuildByReference(EntityRef ref) throws SQLException { + try (PreparedStatement s = c.prepareStatement("SELECT uuid FROM build WHERE reference_type=? AND reference_uuid=?")) { + int i = 1; + s.setString(i++, ref.type); + s.setString(i, ref.uuid.toString()); + return fromRs(s.executeQuery()).map(getUuid); + } + } + public List selectBuildsByPerson(UUID person, PageRequest page) throws SQLException { try (PreparedStatement s = c.prepareStatement("SELECT " + BUILD + " FROM build b, build_participant bp WHERE bp.person=? AND b.uuid = bp.build ORDER BY created_date LIMIT ? OFFSET ?")) { int i = 1; diff --git a/src/main/java/io/trygvis/esper/testing/core/db/FileDao.java b/src/main/java/io/trygvis/esper/testing/core/db/FileDao.java old mode 100644 new mode 100755 index c418970..affe1c8 --- a/src/main/java/io/trygvis/esper/testing/core/db/FileDao.java +++ b/src/main/java/io/trygvis/esper/testing/core/db/FileDao.java @@ -1,10 +1,13 @@ package io.trygvis.esper.testing.core.db; +import io.trygvis.esper.testing.util.sql.*; + import java.io.*; import java.net.*; import java.sql.*; import java.util.*; +import static io.trygvis.esper.testing.util.sql.SqlOption.*; import static java.lang.System.*; public class FileDao { @@ -27,4 +30,15 @@ public class FileDao { return uuid; } } + + public SqlOption load(UUID uuid) throws SQLException { + try (PreparedStatement s = c.prepareStatement("SELECT data FROM file WHERE uuid=?")) { + s.setString(1, uuid.toString()); + ResultSet rs = s.executeQuery(); + if(!rs.next()) { + return none(); + } + return some(rs.getBinaryStream(1)); + } + } } 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 old mode 100644 new mode 100755 index 62c4f2b..96569dd --- a/src/main/java/io/trygvis/esper/testing/core/db/PersonDao.java +++ b/src/main/java/io/trygvis/esper/testing/core/db/PersonDao.java @@ -14,7 +14,7 @@ import static java.lang.System.*; public class PersonDao { private final Connection c; - public static final String PERSON = "uuid, created_date, name"; + public static final String PERSON = "uuid, created_date, name, mail"; public static final SqlF person = new SqlF() { public PersonDto apply(ResultSet rs) throws SQLException { @@ -22,6 +22,7 @@ public class PersonDao { return new PersonDto( UUID.fromString(rs.getString(i++)), new DateTime(rs.getTimestamp(i++).getTime()), + rs.getString(i++), rs.getString(i)); } }; @@ -63,6 +64,19 @@ public class PersonDao { // Person // ----------------------------------------------------------------------- + public UUID insertPerson(String mail, String name) throws SQLException { + try (PreparedStatement s = c.prepareStatement("INSERT INTO person(" + PERSON + ") VALUES(?, ?, ?, ?)")) { + UUID uuid = UUID.randomUUID(); + int i = 1; + s.setString(i++, uuid.toString()); + s.setTimestamp(i++, new Timestamp(currentTimeMillis())); + s.setString(i++, name); + s.setString(i, mail); + s.executeUpdate(); + return uuid; + } + } + public SqlOption selectPerson(UUID uuid) throws SQLException { try (PreparedStatement s = c.prepareStatement("SELECT " + PERSON + " FROM person WHERE uuid=?")) { int i = 1; @@ -71,7 +85,15 @@ public class PersonDao { } } - public List selectPerson(PageRequest pageRequest) throws SQLException { + public SqlOption selectPersonByMail(String mail) throws SQLException { + try (PreparedStatement s = c.prepareStatement("SELECT " + PERSON + " FROM person WHERE mail=?")) { + int i = 1; + s.setString(i, mail); + return fromRs(s.executeQuery()).map(person); + } + } + + public List selectPersons(PageRequest pageRequest) throws SQLException { try (PreparedStatement s = c.prepareStatement("SELECT " + PERSON + " FROM person ORDER BY created_date, name LIMIT ? OFFSET ?")) { int i = 1; s.setInt(i++, pageRequest.count.orSome(10)); @@ -94,6 +116,29 @@ public class PersonDao { } } + // ----------------------------------------------------------------------- + // Person Jenkins User + // ----------------------------------------------------------------------- + + public void insertPersonJenkinsUser(UUID person, UUID jenkinsUser) throws SQLException { + try (PreparedStatement s = c.prepareStatement("INSERT INTO person_jenkins_user(person, jenkins_user) VALUES(?, ?)")) { + int i = 1; + s.setString(i++, person.toString()); + s.setString(i, jenkinsUser.toString()); + s.executeUpdate(); + } + } + + public boolean hasPersonJenkinsUser(UUID person, UUID jenkinsUser) throws SQLException { + try (PreparedStatement s = c.prepareStatement("SELECT 1 FROM person_jenkins_user WHERE person=? AND jenkins_user=?")) { + int i = 1; + s.setString(i++, person.toString()); + s.setString(i, jenkinsUser.toString()); + ResultSet rs = s.executeQuery(); + return rs.next(); + } + } + // ----------------------------------------------------------------------- // Badge // ----------------------------------------------------------------------- diff --git a/src/main/java/io/trygvis/esper/testing/core/db/PersonDto.java b/src/main/java/io/trygvis/esper/testing/core/db/PersonDto.java old mode 100644 new mode 100755 index c643fc7..6dbea74 --- a/src/main/java/io/trygvis/esper/testing/core/db/PersonDto.java +++ b/src/main/java/io/trygvis/esper/testing/core/db/PersonDto.java @@ -7,9 +7,11 @@ import java.util.*; public class PersonDto extends AbstractEntity { public final String name; + public final String mail; - public PersonDto(UUID uuid, DateTime createdDate, String name) { + public PersonDto(UUID uuid, DateTime createdDate, String name, String mail) { super(uuid, createdDate); this.name = name; + this.mail = mail; } } 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 old mode 100644 new mode 100755 index bfcd013..3270db1 --- a/src/main/java/io/trygvis/esper/testing/core/jenkins/JenkinsBuildPoller.java +++ b/src/main/java/io/trygvis/esper/testing/core/jenkins/JenkinsBuildPoller.java @@ -1,22 +1,28 @@ package io.trygvis.esper.testing.core.jenkins; import com.jolbox.bonecp.*; +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.jenkins.*; +import io.trygvis.esper.testing.jenkins.xml.*; +import io.trygvis.esper.testing.util.*; import io.trygvis.esper.testing.util.sql.*; +import org.joda.time.*; import org.slf4j.*; +import java.io.*; import java.sql.*; import java.util.*; import static fj.data.Option.*; import static io.trygvis.esper.testing.Config.*; -import static io.trygvis.esper.testing.EntityRef.*; +import static io.trygvis.esper.testing.EntityRef.jenkinsBuildRef; public class JenkinsBuildPoller implements TablePoller.NewRowCallback { - Logger logger = LoggerFactory.getLogger(getClass()); + private final Logger logger = LoggerFactory.getLogger(getClass()); + private final XmlParser xmlParser = new XmlParser(); public static void main(String[] args) throws Exception { String pollerName = "jenkins_build"; @@ -32,17 +38,43 @@ public class JenkinsBuildPoller implements TablePoller.NewRowCallback(pollerName, tableName, columnNames, some("array_length(users, 1) > 0"), f, callback).work(dataSource); } - public void process(Connection c, JenkinsBuildDto jenkinsBuild) throws SQLException { + public void process(Connection c, final JenkinsBuildDto jenkinsBuild) throws SQLException { Daos daos = new Daos(c); - BuildDao buildDao = daos.buildDao; - PersonDao personDao = daos.personDao; + final BuildDao buildDao = daos.buildDao; - UUID uuid = buildDao.insertBuild(jenkinsBuild.timestamp, "SUCCESS".equals(jenkinsBuild.result), jenkinsRef(jenkinsBuild.uuid)); + SqlOption file = daos.fileDao.load(jenkinsBuild.file); + + if (file.isNone()) { + logger.warn("File unavailable: " + jenkinsBuild.file); + return; + } + + Option o = file.toFj(). + bind(xmlParser.parseDocument). + bind(JenkinsBuildXml.parse); + + if (o.isNone()) { + logger.warn("Unable to parse and process xml: " + jenkinsBuild.file); + return; + } + + final JenkinsBuildXml jenkinsBuildXml = o.some(); + + SqlOption uuidBuildO = buildDao.findBuildByReference(jenkinsBuild.toRef()); + + UUID uuidBuild = uuidBuildO.getOrElse(new SqlP0() { + public UUID apply() throws SQLException { + return buildDao.insertBuild( + new DateTime(jenkinsBuildXml.timestamp), + "SUCCESS".equals(jenkinsBuildXml.result.orSome("")), + jenkinsBuildRef(jenkinsBuild.uuid)); + } + }); int knownPersons = 0, unknownPersons = 0; for (UUID user : jenkinsBuild.users) { - SqlOption personO = personDao.selectPersonByJenkinsUuid(user); + SqlOption personO = daos.personDao.selectPersonByJenkinsUuid(user); // This happens if no one has claimed the user id. if (personO.isNone()) { @@ -54,10 +86,12 @@ public class JenkinsBuildPoller implements TablePoller.NewRowCallback result; - - public final int duration; - - public final long timestamp; - - public final Option changeSet; - - JenkinsBuildXml(URI url, int number, Option result, int duration, long timestamp, Option changeSet) { - this.url = url; - this.number = number; - this.result = result; - this.duration = duration; - this.timestamp = timestamp; - this.changeSet = changeSet; - } - - public static Option parse(Element root) { - Option url = childText(root, "url").bind(Util.parseUri); - Option number = childText(root, "number").bind(Util.parseInt); - Option result = childText(root, "result"); - Option duration = childText(root, "duration").bind(Util.parseInt); - Option timestamp = childText(root, "timestamp").bind(Util.parseLong); - - if (url.isNone()) { - logger.warn("Missing required field: "); - return none(); - } - if (number.isNone()) { - logger.warn("Missing required field: "); - return none(); - } - if (duration.isNone()) { - logger.warn("Missing required field: "); - return none(); - } - if (timestamp.isNone()) { - logger.warn("Missing required field: "); - return none(); - } - - Option changeSet = none(); - Element e = root.getChild("changeSet"); - if (e != null) { - changeSet = ChangeSetXml.parse(e); - } - - return some(new JenkinsBuildXml(url.some(), number.some(), result, duration.some(), timestamp.some(), changeSet)); - } - - public static class ChangeSetXml { - public final List items; - - public final Option revision; - - public ChangeSetXml(List items, Option revision) { - this.items = items; - this.revision = revision; - } - - public static Option parse(Element changeSet) { - - List items = new ArrayList<>(somes(iterableList(changeSet.getChildren("item")).map(parseChangeSetItem)).toCollection()); - - Option revision = Option.fromNull(changeSet.getChild("revision")).bind(parseRevision); - - return some(new ChangeSetXml(items, revision)); - } - } - - public static class ChangeSetItemXml { - public final String commitId; - - public final DateTime date; - - public final String msg; - - public final Option author; - - public ChangeSetItemXml(String commitId, DateTime date, String msg, Option author) { - this.commitId = commitId; - this.date = date; - this.msg = msg; - this.author = author; - } - - private static final F> parseDate = new F>() { - // This variant is used by git - DateTimeFormatter parser = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss Z"); - // and this by subversion - DateTimeFormatter parser2 = ISODateTimeFormat.dateTime(); - - public Option f(String s) { - try { - return some(parser.parseDateTime(s)); - } catch (IllegalArgumentException e) { - try { - return some(parser2.parseDateTime(s)); - } catch (IllegalArgumentException e2) { - return none(); - } - } - } - }; - - public static final F> parseChangeSetItem = new F>() { - public Option f(Element item) { - Option commitId = childText(item, "commitId"); - Option date = childText(item, "date").bind(parseDate); - Option msg = childText(item, "msg"); - - if (commitId.isNone() || date.isNone() || msg.isNone()) { - return none(); - } - - Option author = fromNull(item.getChild("author")).bind(AuthorXml.parseAuthorXml); - - return some(new ChangeSetItemXml(commitId.some(), date.some(), msg.some(), author)); - } - }; - } - - public static class AuthorXml { - public final String absoluteUrl; - public final String fullName; - - public AuthorXml(String absoluteUrl, String fullName) { - this.absoluteUrl = absoluteUrl; - this.fullName = fullName; - } - - public static final F> parseAuthorXml = new F>() { - public Option f(Element element) { - Option absoluteUrl = childText(element, "absoluteUrl"); - Option fullName = childText(element, "fullName"); - - if(absoluteUrl.isNone() || fullName.isNone()) { - return none(); - } - - return some(new AuthorXml(absoluteUrl.some(), fullName.some())); - } - }; - } - - public static class RevisionXml { - public final String module; - - public final int revision; - - public RevisionXml(String module, int revision) { - this.module = module; - this.revision = revision; - } - - public static final F> parseRevision = new F>() { - public Option f(Element e) { - Option module = childText(e, "module"); - Option revision = childText(e, "revision").bind(parseInt); - - if (module.isNone() || revision.isNone()) { - return none(); - } - - return some(new RevisionXml(module.some(), revision.some())); - } - }; - } -} diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsClient.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsClient.java old mode 100644 new mode 100755 index 40ce661..24ea6d9 --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsClient.java +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsClient.java @@ -2,7 +2,8 @@ package io.trygvis.esper.testing.jenkins; import fj.*; import fj.data.*; -import io.trygvis.esper.testing.jenkins.JenkinsJobXml.*; +import io.trygvis.esper.testing.jenkins.xml.*; +import io.trygvis.esper.testing.jenkins.xml.JenkinsJobXml.*; import io.trygvis.esper.testing.util.*; import org.apache.abdera.*; import org.apache.abdera.model.*; @@ -18,10 +19,10 @@ import java.net.*; import java.util.*; import java.util.List; -import static fj.P.p; +import static fj.P.*; import static fj.data.Option.*; -import static io.trygvis.esper.testing.util.HttpClient.inputStreamOnly; -import static java.lang.System.currentTimeMillis; +import static io.trygvis.esper.testing.util.HttpClient.*; +import static java.lang.System.*; import static org.apache.commons.lang.StringUtils.*; public class JenkinsClient { @@ -147,7 +148,7 @@ public class JenkinsClient { case "mavenModuleSetBuild": case "mavenBuild": case "freeStyleBuild": - return JenkinsBuildXml.parse(root).map(new F>() { + return JenkinsBuildXml.parse(root.getDocument()).map(new F>() { public P2 f(JenkinsBuildXml x) { return p(x, d.some()._2()); } @@ -157,43 +158,4 @@ public class JenkinsClient { return Option.none(); } } - -} - -class JenkinsEntryXml { - public final String id; - public final DateTime timestamp; - public final URI url; - - JenkinsEntryXml(String id, DateTime timestamp, URI url) { - this.id = id; - this.timestamp = timestamp; - this.url = url; - } -} - -class JenkinsXml { - public final Option nodeName; - public final Option nodeDescription; - public final Option description; - public final List jobs; - - JenkinsXml(Option nodeName, Option nodeDescription, Option description, List jobs) { - this.nodeName = nodeName; - this.nodeDescription = nodeDescription; - this.description = description; - this.jobs = jobs; - } - - public static class JobXml { - public final String name; - public final String url; - public final String color; - - JobXml(String name, String url, String color) { - this.name = name; - this.url = url; - this.color = color; - } - } } diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsDao.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsDao.java old mode 100644 new mode 100755 index e94c7fd..aec9ab3 --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsDao.java +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsDao.java @@ -1,19 +1,19 @@ package io.trygvis.esper.testing.jenkins; import fj.data.*; +import io.trygvis.esper.testing.jenkins.xml.*; import io.trygvis.esper.testing.util.sql.*; import org.joda.time.*; import java.net.*; import java.sql.*; -import java.util.*; import java.util.List; +import java.util.*; -import static fj.data.Option.*; -import static io.trygvis.esper.testing.Util.toList; -import static io.trygvis.esper.testing.Util.toUuidArray; -import static io.trygvis.esper.testing.util.sql.ResultSetF.getInt; -import static io.trygvis.esper.testing.util.sql.SqlOption.fromRs; +import static fj.data.Option.fromNull; +import static io.trygvis.esper.testing.Util.*; +import static io.trygvis.esper.testing.util.sql.ResultSetF.*; +import static io.trygvis.esper.testing.util.sql.SqlOption.*; import static java.lang.System.*; public class JenkinsDao { @@ -53,7 +53,7 @@ public class JenkinsDao { } }; - public static final String JENKINS_BUILD = "uuid, created_date, job, file, entry_id, url, result, number, duration, timestamp, users"; + public static final String JENKINS_BUILD = "uuid, created_date, job, file, entry_id, url, users"; public static final SqlF jenkinsBuild = new SqlF() { public JenkinsBuildDto apply(ResultSet rs) throws SQLException { @@ -65,10 +65,6 @@ public class JenkinsDao { UUID.fromString(rs.getString(i++)), rs.getString(i++), URI.create(rs.getString(i++)), - rs.getString(i++), - rs.getInt(i++), - rs.getInt(i++), - new DateTime(rs.getTimestamp(i++).getTime()), toUuidArray(rs, i)); } }; @@ -191,8 +187,8 @@ public class JenkinsDao { } } - public UUID insertBuild(UUID job, UUID file, String entryId, URI url, String result, int number, int duration, long timestamp, UUID[] users) throws SQLException { - try (PreparedStatement s = c.prepareStatement("INSERT INTO jenkins_build(" + JENKINS_BUILD + ") VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) { + public UUID insertBuild(UUID job, UUID file, String entryId, URI url, UUID[] users) throws SQLException { + try (PreparedStatement s = c.prepareStatement("INSERT INTO jenkins_build(" + JENKINS_BUILD + ") VALUES(?, ?, ?, ?, ?, ?, ?)")) { UUID uuid = UUID.randomUUID(); int i = 1; s.setString(i++, uuid.toString()); @@ -200,11 +196,7 @@ public class JenkinsDao { s.setString(i++, job.toString()); s.setString(i++, file.toString()); s.setString(i++, entryId); - s.setString(i++, url.toASCIIString()); - s.setString(i++, result); - s.setInt(i++, number); - s.setInt(i++, duration); - s.setTimestamp(i++, new Timestamp(timestamp)); + s.setString(i, url.toASCIIString()); s.setArray(i, c.createArrayOf("varchar", users)); s.executeUpdate(); diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJob.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJob.java old mode 100644 new mode 100755 index 5ca449e..e59a953 --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJob.java +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJob.java @@ -2,6 +2,8 @@ package io.trygvis.esper.testing.jenkins; import fj.*; import fj.data.*; +import io.trygvis.esper.testing.jenkins.xml.JenkinsJobXml; + import static fj.data.Option.*; import static io.trygvis.esper.testing.jenkins.JenkinsClient.apiXml; import static java.lang.System.currentTimeMillis; diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJobXml.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJobXml.java deleted file mode 100644 index ca661b8..0000000 --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJobXml.java +++ /dev/null @@ -1,88 +0,0 @@ -package io.trygvis.esper.testing.jenkins; - -import java.net.URI; - -import org.jdom2.Element; - -import fj.F; -import fj.data.Option; -import io.trygvis.esper.testing.Util; - -import static fj.data.Option.some; -import static io.trygvis.esper.testing.Util.child; -import static io.trygvis.esper.testing.Util.childText; - -class JenkinsJobXml { - enum JenkinsJobType { - FREE_STYLE, MAVEN_MODULE_SET, MAVEN_MODULE, MATRIX, MATRIX_CONFIGURATION - } - - public final JenkinsJobType type; - public final Option description; - public final Option displayName; - public final Option name; - public final URI url; - public final Option color; - public final boolean buildable; - public final Option lastBuild; - public final Option lastCompletedBuild; - public final Option lastFailedBuild; - public final Option lastSuccessfulBuild; - public final Option lastUnsuccessfulBuild; - - protected JenkinsJobXml(JenkinsJobType type, Option description, Option displayName, - Option name, URI url, Option color, boolean buildable, - Option lastBuild, Option lastCompletedBuild, - Option lastFailedBuild, Option lastSuccessfulBuild, - Option lastUnsuccessfulBuild) { - this.type = type; - this.description = description; - this.displayName = displayName; - this.name = name; - this.url = url; - this.color = color; - this.buildable = buildable; - this.lastBuild = lastBuild; - this.lastCompletedBuild = lastCompletedBuild; - this.lastFailedBuild = lastFailedBuild; - this.lastSuccessfulBuild = lastSuccessfulBuild; - this.lastUnsuccessfulBuild = lastUnsuccessfulBuild; - } - - static class BuildXml { - public final int number; - public final URI url; - public static F> buildXml = new F>() { - public Option f(Element element) { - Option number = childText(element, "number").bind(Util.parseInt); - Option url = childText(element, "url").bind(Util.parseUri); - - if (number.isNone() || url.isNone()) { - return Option.none(); - } - - return some(new BuildXml(number.some(), url.some())); - } - }; - - BuildXml(int number, URI url) { - this.number = number; - this.url = url; - } - } - - public static JenkinsJobXml parse(URI url, JenkinsJobType type, Element root) { - return new JenkinsJobXml(type, - childText(root, "description"), - childText(root, "displayName"), - childText(root, "name"), - childText(root, "url").bind(Util.parseUri).orSome(url), - childText(root, "color"), - childText(root, "buildable").bind(Util.parseBoolean).orSome(false), - child(root, "lastBuild").bind(BuildXml.buildXml), - child(root, "lastCompletedBuild").bind(BuildXml.buildXml), - child(root, "lastFailedBuild").bind(BuildXml.buildXml), - child(root, "lastSuccessfulBuild").bind(BuildXml.buildXml), - child(root, "lastUnsuccessfulBuild").bind(BuildXml.buildXml)); - } -} diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerActor.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerActor.java old mode 100644 new mode 100755 index c34f422..00d55c2 --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerActor.java +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerActor.java @@ -3,6 +3,7 @@ package io.trygvis.esper.testing.jenkins; import fj.*; import fj.data.*; import io.trygvis.esper.testing.core.db.*; +import io.trygvis.esper.testing.jenkins.xml.*; import io.trygvis.esper.testing.util.object.*; import io.trygvis.esper.testing.util.sql.*; import org.slf4j.*; @@ -153,10 +154,6 @@ public class JenkinsServerActor implements TransactionalActor { buildXmlFile, entry.id, build.url, - result, - build.number, - build.duration, - build.timestamp, users.toArray(new UUID[users.size()])); logger.info("Build inserted: {}, #users={} item #{}/{}", uuid, users.size(), i, list.size()); diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerDto.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerDto.java old mode 100644 new mode 100755 index 3cf9e7c..c603506 --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerDto.java +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerDto.java @@ -15,4 +15,8 @@ public class JenkinsServerDto extends AbstractEntity { this.url = url; this.enabled = enabled; } + + public URI userUrl(String id) { + return URI.create(url.toASCIIString() + "/users/" + id); + } } diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerOld.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerOld.java old mode 100644 new mode 100755 index e21f336..38c088a --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerOld.java +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerOld.java @@ -5,6 +5,7 @@ import fj.data.*; import static fj.data.Option.*; import static io.trygvis.esper.testing.jenkins.JenkinsClient.apiXml; +import io.trygvis.esper.testing.jenkins.xml.JenkinsXml; import io.trygvis.esper.testing.util.object.*; import org.joda.time.*; diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/xml/JenkinsBuildXml.java b/src/main/java/io/trygvis/esper/testing/jenkins/xml/JenkinsBuildXml.java new file mode 100755 index 0000000..506407e --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/jenkins/xml/JenkinsBuildXml.java @@ -0,0 +1,210 @@ +package io.trygvis.esper.testing.jenkins.xml; + +import java.net.URI; +import java.util.ArrayList; +import java.util.List; + +import org.jdom2.Document; +import org.jdom2.Element; +import org.joda.time.DateTime; +import org.joda.time.format.DateTimeFormat; +import org.joda.time.format.DateTimeFormatter; +import org.joda.time.format.ISODateTimeFormat; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import fj.F; +import fj.data.Option; +import io.trygvis.esper.testing.Util; + +import static fj.data.List.iterableList; +import static fj.data.Option.*; +import static io.trygvis.esper.testing.Util.childText; +import static io.trygvis.esper.testing.Util.parseInt; +import static io.trygvis.esper.testing.jenkins.xml.JenkinsBuildXml.ChangeSetItemXml.parseChangeSetItem; +import static io.trygvis.esper.testing.jenkins.xml.JenkinsBuildXml.RevisionXml.parseRevision; + +public class JenkinsBuildXml { + private static final Logger logger = LoggerFactory.getLogger(JenkinsBuildXml.class); + + public final URI url; + + public final int number; + + public final Option result; + + public final int duration; + + public final long timestamp; + + public final Option changeSet; + + JenkinsBuildXml(URI url, int number, Option result, int duration, long timestamp, Option changeSet) { + this.url = url; + this.number = number; + this.result = result; + this.duration = duration; + this.timestamp = timestamp; + this.changeSet = changeSet; + } + + public static F> parse = new F>() { + public Option f(Document document) { + return parse(document); + } + }; + + public static Option parse(Document doc) { + Element root = doc.getRootElement(); + + Option url = childText(root, "url").bind(Util.parseUri); + Option number = childText(root, "number").bind(Util.parseInt); + Option result = childText(root, "result"); + Option duration = childText(root, "duration").bind(Util.parseInt); + Option timestamp = childText(root, "timestamp").bind(Util.parseLong); + + if (url.isNone()) { + logger.warn("Missing required field: "); + return none(); + } + if (number.isNone()) { + logger.warn("Missing required field: "); + return none(); + } + if (duration.isNone()) { + logger.warn("Missing required field: "); + return none(); + } + if (timestamp.isNone()) { + logger.warn("Missing required field: "); + return none(); + } + + Option changeSet = none(); + Element e = root.getChild("changeSet"); + if (e != null) { + changeSet = ChangeSetXml.parse(e); + } + + return some(new JenkinsBuildXml(url.some(), number.some(), result, duration.some(), timestamp.some(), changeSet)); + } + + public static class ChangeSetXml { + public final List items; + + public final Option revision; + + public ChangeSetXml(List items, Option revision) { + this.items = items; + this.revision = revision; + } + + public static Option parse(Element changeSet) { + + List items = new ArrayList<>(Option.somes(iterableList(changeSet.getChildren("item")).map(parseChangeSetItem)).toCollection()); + + Option revision = Option.fromNull(changeSet.getChild("revision")).bind(parseRevision); + + return some(new ChangeSetXml(items, revision)); + } + } + + public static class ChangeSetItemXml { + public final String commitId; + + public final DateTime date; + + public final String msg; + + public final Option author; + + public ChangeSetItemXml(String commitId, DateTime date, String msg, Option author) { + this.commitId = commitId; + this.date = date; + this.msg = msg; + this.author = author; + } + + private static final F> parseDate = new F>() { + // This variant is used by git + DateTimeFormatter parser = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss Z"); + // and this by subversion + DateTimeFormatter parser2 = ISODateTimeFormat.dateTime(); + + public Option f(String s) { + try { + return some(parser.parseDateTime(s)); + } catch (IllegalArgumentException e) { + try { + return some(parser2.parseDateTime(s)); + } catch (IllegalArgumentException e2) { + return none(); + } + } + } + }; + + public static final F> parseChangeSetItem = new F>() { + public Option f(Element item) { + Option commitId = childText(item, "commitId"); + Option date = childText(item, "date").bind(parseDate); + Option msg = childText(item, "msg"); + + if (commitId.isNone() || date.isNone() || msg.isNone()) { + return none(); + } + + Option author = fromNull(item.getChild("author")).bind(AuthorXml.parseAuthorXml); + + return some(new ChangeSetItemXml(commitId.some(), date.some(), msg.some(), author)); + } + }; + } + + public static class AuthorXml { + public final String absoluteUrl; + public final String fullName; + + public AuthorXml(String absoluteUrl, String fullName) { + this.absoluteUrl = absoluteUrl; + this.fullName = fullName; + } + + public static final F> parseAuthorXml = new F>() { + public Option f(Element element) { + Option absoluteUrl = childText(element, "absoluteUrl"); + Option fullName = childText(element, "fullName"); + + if(absoluteUrl.isNone() || fullName.isNone()) { + return none(); + } + + return some(new AuthorXml(absoluteUrl.some(), fullName.some())); + } + }; + } + + public static class RevisionXml { + public final String module; + + public final int revision; + + public RevisionXml(String module, int revision) { + this.module = module; + this.revision = revision; + } + + public static final F> parseRevision = new F>() { + public Option f(Element e) { + Option module = childText(e, "module"); + Option revision = childText(e, "revision").bind(parseInt); + + if (module.isNone() || revision.isNone()) { + return none(); + } + + return some(new RevisionXml(module.some(), revision.some())); + } + }; + } +} diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/xml/JenkinsEntryXml.java b/src/main/java/io/trygvis/esper/testing/jenkins/xml/JenkinsEntryXml.java new file mode 100755 index 0000000..c801ccf --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/jenkins/xml/JenkinsEntryXml.java @@ -0,0 +1,17 @@ +package io.trygvis.esper.testing.jenkins.xml; + +import java.net.URI; + +import org.joda.time.DateTime; + +public class JenkinsEntryXml { + public final String id; + public final DateTime timestamp; + public final URI url; + + public JenkinsEntryXml(String id, DateTime timestamp, URI url) { + this.id = id; + this.timestamp = timestamp; + this.url = url; + } +} diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/xml/JenkinsJobXml.java b/src/main/java/io/trygvis/esper/testing/jenkins/xml/JenkinsJobXml.java new file mode 100755 index 0000000..eb4c874 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/jenkins/xml/JenkinsJobXml.java @@ -0,0 +1,88 @@ +package io.trygvis.esper.testing.jenkins.xml; + +import java.net.URI; + +import org.jdom2.Element; + +import fj.F; +import fj.data.Option; +import io.trygvis.esper.testing.Util; + +import static fj.data.Option.some; +import static io.trygvis.esper.testing.Util.child; +import static io.trygvis.esper.testing.Util.childText; + +public class JenkinsJobXml { + public enum JenkinsJobType { + FREE_STYLE, MAVEN_MODULE_SET, MAVEN_MODULE, MATRIX, MATRIX_CONFIGURATION + } + + public final JenkinsJobType type; + public final Option description; + public final Option displayName; + public final Option name; + public final URI url; + public final Option color; + public final boolean buildable; + public final Option lastBuild; + public final Option lastCompletedBuild; + public final Option lastFailedBuild; + public final Option lastSuccessfulBuild; + public final Option lastUnsuccessfulBuild; + + protected JenkinsJobXml(JenkinsJobType type, Option description, Option displayName, + Option name, URI url, Option color, boolean buildable, + Option lastBuild, Option lastCompletedBuild, + Option lastFailedBuild, Option lastSuccessfulBuild, + Option lastUnsuccessfulBuild) { + this.type = type; + this.description = description; + this.displayName = displayName; + this.name = name; + this.url = url; + this.color = color; + this.buildable = buildable; + this.lastBuild = lastBuild; + this.lastCompletedBuild = lastCompletedBuild; + this.lastFailedBuild = lastFailedBuild; + this.lastSuccessfulBuild = lastSuccessfulBuild; + this.lastUnsuccessfulBuild = lastUnsuccessfulBuild; + } + + static class BuildXml { + public final int number; + public final URI url; + public static F> buildXml = new F>() { + public Option f(Element element) { + Option number = childText(element, "number").bind(Util.parseInt); + Option url = childText(element, "url").bind(Util.parseUri); + + if (number.isNone() || url.isNone()) { + return Option.none(); + } + + return some(new BuildXml(number.some(), url.some())); + } + }; + + BuildXml(int number, URI url) { + this.number = number; + this.url = url; + } + } + + public static JenkinsJobXml parse(URI url, JenkinsJobType type, Element root) { + return new JenkinsJobXml(type, + childText(root, "description"), + childText(root, "displayName"), + childText(root, "name"), + childText(root, "url").bind(Util.parseUri).orSome(url), + childText(root, "color"), + childText(root, "buildable").bind(Util.parseBoolean).orSome(false), + child(root, "lastBuild").bind(BuildXml.buildXml), + child(root, "lastCompletedBuild").bind(BuildXml.buildXml), + child(root, "lastFailedBuild").bind(BuildXml.buildXml), + child(root, "lastSuccessfulBuild").bind(BuildXml.buildXml), + child(root, "lastUnsuccessfulBuild").bind(BuildXml.buildXml)); + } +} diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/xml/JenkinsXml.java b/src/main/java/io/trygvis/esper/testing/jenkins/xml/JenkinsXml.java new file mode 100755 index 0000000..d599484 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/jenkins/xml/JenkinsXml.java @@ -0,0 +1,31 @@ +package io.trygvis.esper.testing.jenkins.xml; + +import java.util.List; + +import fj.data.Option; + +public class JenkinsXml { + public final Option nodeName; + public final Option nodeDescription; + public final Option description; + public final List jobs; + + public JenkinsXml(Option nodeName, Option nodeDescription, Option description, List jobs) { + this.nodeName = nodeName; + this.nodeDescription = nodeDescription; + this.description = description; + this.jobs = jobs; + } + + public static class JobXml { + public final String name; + public final String url; + public final String color; + + public JobXml(String name, String url, String color) { + this.name = name; + this.url = url; + this.color = color; + } + } +} diff --git a/src/main/java/io/trygvis/esper/testing/util/EasyX509TrustManager.java b/src/main/java/io/trygvis/esper/testing/util/EasyX509TrustManager.java old mode 100644 new mode 100755 diff --git a/src/main/java/io/trygvis/esper/testing/util/XmlParser.java b/src/main/java/io/trygvis/esper/testing/util/XmlParser.java old mode 100644 new mode 100755 index 624785c..a1bda95 --- a/src/main/java/io/trygvis/esper/testing/util/XmlParser.java +++ b/src/main/java/io/trygvis/esper/testing/util/XmlParser.java @@ -1,5 +1,6 @@ package io.trygvis.esper.testing.util; +import fj.F; import fj.data.*; import org.h2.util.*; import org.jdom2.*; @@ -25,8 +26,13 @@ public class XmlParser { xmlInputFactory = XMLInputFactory.newFactory(); } - public Option parseDocument(InputStream stream) { + public F> parseDocument = new F>() { + public Option f(InputStream inputStream) { + return parseDocument(inputStream); + } + }; + public Option parseDocument(InputStream stream) { try { if (debugXml) { // TODO: Pretty print 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 old mode 100644 new mode 100755 diff --git a/src/main/java/io/trygvis/esper/testing/util/sql/SqlF.java b/src/main/java/io/trygvis/esper/testing/util/sql/SqlF.java old mode 100644 new mode 100755 diff --git a/src/main/java/io/trygvis/esper/testing/util/sql/SqlOption.java b/src/main/java/io/trygvis/esper/testing/util/sql/SqlOption.java old mode 100644 new mode 100755 index 286a872..43fb440 --- a/src/main/java/io/trygvis/esper/testing/util/sql/SqlOption.java +++ b/src/main/java/io/trygvis/esper/testing/util/sql/SqlOption.java @@ -1,5 +1,7 @@ package io.trygvis.esper.testing.util.sql; +import fj.data.*; + import java.sql.*; public abstract class SqlOption { @@ -45,6 +47,10 @@ public abstract class SqlOption { public abstract A getOrElse(A a); + public abstract A getOrElse(SqlP0 p) throws SQLException; + + public abstract Option toFj(); + public static SqlOption fromNull(A a) { if (a != null) { return some(a); @@ -74,6 +80,14 @@ public abstract class SqlOption { return a; } + public A getOrElse(SqlP0 p) throws SQLException { + return p.apply(); + } + + public Option toFj() { + return Option.none(); + } + public String toString() { return "None"; } @@ -102,6 +116,14 @@ public abstract class SqlOption { return this.a; } + public A getOrElse(SqlP0 p) throws SQLException { + return a; + } + + public Option toFj() { + return Option.some(a); + } + public String toString() { return "Some(" + a + ")"; } diff --git a/src/main/java/io/trygvis/esper/testing/util/sql/SqlP0.java b/src/main/java/io/trygvis/esper/testing/util/sql/SqlP0.java new file mode 100755 index 0000000..23c48d1 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/util/sql/SqlP0.java @@ -0,0 +1,7 @@ +package io.trygvis.esper.testing.util.sql; + +import java.sql.*; + +public abstract class SqlP0 { + public abstract A apply() throws SQLException; +} diff --git a/src/main/java/io/trygvis/esper/testing/web/resource/CoreResource.java b/src/main/java/io/trygvis/esper/testing/web/resource/CoreResource.java old mode 100644 new mode 100755 index 9b22674..7548a32 --- a/src/main/java/io/trygvis/esper/testing/web/resource/CoreResource.java +++ b/src/main/java/io/trygvis/esper/testing/web/resource/CoreResource.java @@ -40,7 +40,7 @@ public class CoreResource extends AbstractResource { return da.inTransaction(new DatabaseAccess.DaosCallback>() { public List run(Daos daos) throws SQLException { List list = new ArrayList<>(); - for (PersonDto person : daos.personDao.selectPerson(pageRequest)) { + for (PersonDto person : daos.personDao.selectPersons(pageRequest)) { list.add(getPersonJson(daos, person)); } return list; diff --git a/src/main/java/io/trygvis/esper/testing/web/resource/JenkinsResource.java b/src/main/java/io/trygvis/esper/testing/web/resource/JenkinsResource.java old mode 100644 new mode 100755 index 292be71..aab108b --- a/src/main/java/io/trygvis/esper/testing/web/resource/JenkinsResource.java +++ b/src/main/java/io/trygvis/esper/testing/web/resource/JenkinsResource.java @@ -1,17 +1,21 @@ package io.trygvis.esper.testing.web.resource; +import fj.*; +import fj.data.*; import io.trygvis.esper.testing.*; import io.trygvis.esper.testing.jenkins.*; +import io.trygvis.esper.testing.jenkins.xml.*; +import io.trygvis.esper.testing.util.*; import io.trygvis.esper.testing.util.sql.*; import io.trygvis.esper.testing.web.*; import org.joda.time.*; -import javax.ws.rs.*; -import javax.ws.rs.core.*; import java.net.*; import java.sql.*; import java.util.*; import java.util.List; +import javax.ws.rs.*; +import javax.ws.rs.core.*; @Path("/resource/jenkins") public class JenkinsResource extends AbstractResource { @@ -102,6 +106,8 @@ public class JenkinsResource extends AbstractResource { protected abstract T run() throws SQLException; + private final XmlParser xmlParser = new XmlParser(); + public T run(Daos daos) throws SQLException { this.daos = daos; return run(); @@ -120,22 +126,30 @@ public class JenkinsResource extends AbstractResource { } }; - protected SqlF getJenkinsJobJson = new SqlF() { + protected SqlF getJenkinsJobJson = new SqlF() { public JenkinsJobJson apply(JenkinsJobDto job) throws SQLException { return new JenkinsJobJson(job.uuid, job.createdDate, job.server, job.displayName.toNull()); } }; - protected SqlF getJenkinsJobJsonDetail = new SqlF() { + protected SqlF getJenkinsJobJsonDetail = new SqlF() { public JenkinsJobJson apply(JenkinsJobDto job) throws SQLException { int buildCount = daos.jenkinsDao.selectBuildCountByJob(job.uuid); return new JenkinsJobJson(job.uuid, job.createdDate, job.server, job.displayName.toNull(), buildCount); } }; - protected SqlF getJenkinsBuildJson = new SqlF() { + protected SqlF getJenkinsBuildJson = new SqlF() { public JenkinsBuildJson apply(JenkinsBuildDto dto) throws SQLException { - return new JenkinsBuildJson(dto.uuid, dto.createdDate, dto.result); + Option result = daos.fileDao.load(dto.file).toFj(). + bind(xmlParser.parseDocument). + bind(JenkinsBuildXml.parse).map(new F() { + public String f(JenkinsBuildXml xml) { + return xml.result.orSome("unknown"); + } + }); + + return new JenkinsBuildJson(dto.uuid, dto.createdDate, result.orSome("unknown")); } }; } -- cgit v1.2.3