diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e1e3c15 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +.gradle +build +.idea +*.iml +*.ipr +*.iws +.DS_Store +.env +ci/variables.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..eb0d7c5 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,149 @@ +dist: trusty +sudo: required +notifications: + email: false +env: + - RELEASE_TAG="release-$TRAVIS_BUILD_NUMBER" +stages: + - build and publish + - deploy + - migrate +if: tag IS blank + +jobs: + include: + - stage: build and publish + language: java + jdk: openjdk11 + addons: + mariadb: '10.2' + install: skip + before_script: + - mysql -uroot < databases/create_databases.sql + - ./gradlew testMigrate + script: + - APPLICATION=allocations + - ./gradlew clean :applications:${APPLICATION}-server:build + before_deploy: + - git config --local user.name "Travis CI" + - git config --local user.email "travis@example.com" + - git tag -f $RELEASE_TAG + deploy: &github + provider: releases + api_key: $GITHUB_OAUTH_TOKEN + file: + - "applications/${APPLICATION}-server/build/libs/${APPLICATION}-server.jar" + skip_cleanup: true + + - stage: build and publish + language: java + jdk: openjdk11 + addons: + mariadb: '10.2' + install: skip + before_script: + - mysql -uroot < databases/create_databases.sql + - ./gradlew testMigrate + script: + - APPLICATION=backlog + - ./gradlew clean :applications:${APPLICATION}-server:build + before_deploy: + - git config --local user.name "Travis CI" + - git config --local user.email "travis@example.com" + - git tag -f $RELEASE_TAG + deploy: + <<: *github + + - stage: build and publish + language: java + jdk: openjdk11 + addons: + mariadb: '10.2' + install: skip + before_script: + - mysql -uroot < databases/create_databases.sql + - ./gradlew testMigrate + script: + - APPLICATION=registration + - ./gradlew clean :applications:${APPLICATION}-server:build + before_deploy: + - git config --local user.name "Travis CI" + - git config --local user.email "travis@example.com" + - git tag -f $RELEASE_TAG + deploy: + <<: *github + + - stage: build and publish + language: java + jdk: openjdk11 + addons: + mariadb: '10.2' + install: skip + before_script: + - mysql -uroot < databases/create_databases.sql + - ./gradlew testMigrate + script: + - APPLICATION=timesheets + - ./gradlew clean :applications:${APPLICATION}-server:build + before_deploy: + - git config --local user.name "Travis CI" + - git config --local user.email "travis@example.com" + - git tag -f $RELEASE_TAG + deploy: + <<: *github + + - stage: deploy + language: bash + script: + - echo "Downloading allocations server $RELEASE_TAG" + - wget -P applications/allocations-server/build/libs https://github.com/$GITHUB_USERNAME/pal-tracker-distributed/releases/download/$RELEASE_TAG/allocations-server.jar + before_deploy: + - cp manifest-allocations.yml manifest.yml + - echo "Deploying allocations server $RELEASE_TAG" + deploy: &cloudfoundry + provider: cloudfoundry + api: $CF_API_URL + username: $CF_USERNAME + password: $CF_PASSWORD + organization: $CF_ORG + space: $CF_SPACE + - stage: deploy + language: bash + script: + - echo "Downloading backlog server $RELEASE_TAG" + - wget -P applications/backlog-server/build/libs https://github.com/$GITHUB_USERNAME/pal-tracker-distributed/releases/download/$RELEASE_TAG/backlog-server.jar + before_deploy: + - cp manifest-backlog.yml manifest.yml + - echo "Deploying backlog server $RELEASE_TAG" + deploy: + <<: *cloudfoundry + - stage: deploy + language: bash + script: + - echo "Downloading registration server $RELEASE_TAG" + - wget -P applications/registration-server/build/libs https://github.com/$GITHUB_USERNAME/pal-tracker-distributed/releases/download/$RELEASE_TAG/registration-server.jar + before_deploy: + - cp manifest-registration.yml manifest.yml + - echo "Deploying registration server $RELEASE_TAG" + deploy: + <<: *cloudfoundry + - stage: deploy + language: bash + script: + - echo "Downloading timesheets server $RELEASE_TAG" + - wget -P applications/timesheets-server/build/libs https://github.com/$GITHUB_USERNAME/pal-tracker-distributed/releases/download/$RELEASE_TAG/timesheets-server.jar + before_deploy: + - cp manifest-timesheets.yml manifest.yml + - echo "Deploying timesheets server $RELEASE_TAG" + deploy: + <<: *cloudfoundry + - stage: migrate + language: java + before_install: + - wget -q -O - https://packages.cloudfoundry.org/debian/cli.cloudfoundry.org.key | sudo apt-key add - + - echo "deb https://packages.cloudfoundry.org/debian stable main" | sudo tee /etc/apt/sources.list.d/cloudfoundry-cli.list + - sudo apt-get update + - sudo apt-get install cf-cli + script: + - cf login -a $CF_API_URL -u $CF_USERNAME -p $CF_PASSWORD -o $CF_ORG -s $CF_SPACE + - ./gradlew cfMigrate diff --git a/applications/allocations-server/build.gradle b/applications/allocations-server/build.gradle new file mode 100644 index 0000000..f73756a --- /dev/null +++ b/applications/allocations-server/build.gradle @@ -0,0 +1,5 @@ +apply from: "$projectDir/../server.gradle" + +dependencies { + compile project(":components:allocations") +} diff --git a/applications/allocations-server/src/main/java/io/pivotal/pal/tracker/allocations/App.java b/applications/allocations-server/src/main/java/io/pivotal/pal/tracker/allocations/App.java new file mode 100644 index 0000000..b034229 --- /dev/null +++ b/applications/allocations-server/src/main/java/io/pivotal/pal/tracker/allocations/App.java @@ -0,0 +1,29 @@ +package io.pivotal.pal.tracker.allocations; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.web.client.RestOperations; + +import java.util.TimeZone; + + +@SpringBootApplication +@ComponentScan({"io.pivotal.pal.tracker.allocations", "io.pivotal.pal.tracker.restsupport"}) +public class App { + + public static void main(String[] args) { + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + SpringApplication.run(App.class, args); + } + + @Bean + ProjectClient projectClient( + RestOperations restOperations, + @Value("${registration.server.endpoint}") String registrationEndpoint + ) { + return new ProjectClient(restOperations, registrationEndpoint); + } +} diff --git a/applications/allocations-server/src/main/resources/application.properties b/applications/allocations-server/src/main/resources/application.properties new file mode 100644 index 0000000..57cbd07 --- /dev/null +++ b/applications/allocations-server/src/main/resources/application.properties @@ -0,0 +1,6 @@ +spring.application.name=allocations-server + +server.port=8081 +spring.datasource.username=tracker +spring.datasource.url=jdbc:mysql://localhost:3306/tracker_allocations_dev?useSSL=false&useTimezone=true&serverTimezone=UTC&useLegacyDatetimeCode=false +registration.server.endpoint=http://localhost:8083 diff --git a/applications/allocations-server/src/test/java/test/pivotal/pal/tracker/allocations/AllocationsAppTest.java b/applications/allocations-server/src/test/java/test/pivotal/pal/tracker/allocations/AllocationsAppTest.java new file mode 100644 index 0000000..4577f49 --- /dev/null +++ b/applications/allocations-server/src/test/java/test/pivotal/pal/tracker/allocations/AllocationsAppTest.java @@ -0,0 +1,19 @@ +package test.pivotal.pal.tracker.allocations; + +import io.pivotal.pal.tracker.allocations.App; +import org.junit.Test; +import org.springframework.web.client.RestTemplate; + +import static org.assertj.core.api.Assertions.assertThat; + +public class AllocationsAppTest { + + @Test + public void embedded() { + App.main(new String[]{}); + + String response = new RestTemplate().getForObject("http://localhost:8181/allocations?projectId=0", String.class); + + assertThat(response).isEqualTo("[]"); + } +} diff --git a/applications/allocations-server/src/test/resources/application.properties b/applications/allocations-server/src/test/resources/application.properties new file mode 100644 index 0000000..9d25a30 --- /dev/null +++ b/applications/allocations-server/src/test/resources/application.properties @@ -0,0 +1,6 @@ +spring.application.name=allocations-server + +server.port=8181 +spring.datasource.username=tracker +spring.datasource.url=jdbc:mysql://localhost:3306/tracker_allocations_test?useSSL=false&useTimezone=true&serverTimezone=UTC&useLegacyDatetimeCode=false +registration.server.endpoint=http://localhost:8883 diff --git a/applications/backlog-server/build.gradle b/applications/backlog-server/build.gradle new file mode 100644 index 0000000..954e9c7 --- /dev/null +++ b/applications/backlog-server/build.gradle @@ -0,0 +1,5 @@ +apply from: "$projectDir/../server.gradle" + +dependencies { + compile project(":components:backlog") +} diff --git a/applications/backlog-server/src/main/java/io/pivotal/pal/tracker/backlog/App.java b/applications/backlog-server/src/main/java/io/pivotal/pal/tracker/backlog/App.java new file mode 100644 index 0000000..832ff65 --- /dev/null +++ b/applications/backlog-server/src/main/java/io/pivotal/pal/tracker/backlog/App.java @@ -0,0 +1,29 @@ +package io.pivotal.pal.tracker.backlog; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.web.client.RestOperations; + +import java.util.TimeZone; + + +@SpringBootApplication +@ComponentScan({"io.pivotal.pal.tracker.backlog", "io.pivotal.pal.tracker.restsupport"}) +public class App { + + public static void main(String[] args) { + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + SpringApplication.run(App.class, args); + } + + @Bean + ProjectClient projectClient( + RestOperations restOperations, + @Value("${registration.server.endpoint}") String registrationEndpoint + ) { + return new ProjectClient(restOperations, registrationEndpoint); + } +} diff --git a/applications/backlog-server/src/main/resources/application.properties b/applications/backlog-server/src/main/resources/application.properties new file mode 100644 index 0000000..8ea7201 --- /dev/null +++ b/applications/backlog-server/src/main/resources/application.properties @@ -0,0 +1,6 @@ +spring.application.name=backlog-server + +server.port=8082 +spring.datasource.username=tracker +spring.datasource.url=jdbc:mysql://localhost:3306/tracker_backlog_dev?useSSL=false&useTimezone=true&serverTimezone=UTC&useLegacyDatetimeCode=false +registration.server.endpoint=http://localhost:8083 diff --git a/applications/backlog-server/src/test/java/test/pivotal/pal/tracker/backlog/BacklogAppTest.java b/applications/backlog-server/src/test/java/test/pivotal/pal/tracker/backlog/BacklogAppTest.java new file mode 100644 index 0000000..dea2c61 --- /dev/null +++ b/applications/backlog-server/src/test/java/test/pivotal/pal/tracker/backlog/BacklogAppTest.java @@ -0,0 +1,19 @@ +package test.pivotal.pal.tracker.backlog; + +import io.pivotal.pal.tracker.backlog.App; +import org.junit.Test; +import org.springframework.web.client.RestTemplate; + +import static org.assertj.core.api.Assertions.assertThat; + +public class BacklogAppTest { + + @Test + public void embedded() { + App.main(new String[]{}); + + String response = new RestTemplate().getForObject("http://localhost:8181/stories?projectId=0", String.class); + + assertThat(response).isEqualTo("[]"); + } +} diff --git a/applications/backlog-server/src/test/resources/application.properties b/applications/backlog-server/src/test/resources/application.properties new file mode 100644 index 0000000..8b43acf --- /dev/null +++ b/applications/backlog-server/src/test/resources/application.properties @@ -0,0 +1,6 @@ +spring.application.name=backlog-server + +server.port=8181 +spring.datasource.username=tracker +spring.datasource.url=jdbc:mysql://localhost:3306/tracker_backlog_test?useSSL=false&useTimezone=true&serverTimezone=UTC&useLegacyDatetimeCode=false +registration.server.endpoint=http://localhost:8883 diff --git a/applications/registration-server/build.gradle b/applications/registration-server/build.gradle new file mode 100644 index 0000000..8b1cfbe --- /dev/null +++ b/applications/registration-server/build.gradle @@ -0,0 +1,7 @@ +apply from: "$projectDir/../server.gradle" + +dependencies { + compile project(":components:accounts") + compile project(":components:projects") + compile project(":components:users") +} diff --git a/applications/registration-server/src/main/java/io/pivotal/pal/tracker/registration/App.java b/applications/registration-server/src/main/java/io/pivotal/pal/tracker/registration/App.java new file mode 100644 index 0000000..d2d0ba2 --- /dev/null +++ b/applications/registration-server/src/main/java/io/pivotal/pal/tracker/registration/App.java @@ -0,0 +1,23 @@ +package io.pivotal.pal.tracker.registration; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; + +import java.util.TimeZone; + + +@SpringBootApplication +@ComponentScan({ + "io.pivotal.pal.tracker.accounts", + "io.pivotal.pal.tracker.restsupport", + "io.pivotal.pal.tracker.projects", + "io.pivotal.pal.tracker.users", + "io.pivotal.pal.tracker.registration" +}) +public class App { + public static void main(String[] args) { + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + SpringApplication.run(App.class, args); + } +} diff --git a/applications/registration-server/src/main/resources/application.properties b/applications/registration-server/src/main/resources/application.properties new file mode 100644 index 0000000..ff88e76 --- /dev/null +++ b/applications/registration-server/src/main/resources/application.properties @@ -0,0 +1,6 @@ +spring.application.name=registration-server + +server.port=8083 +spring.datasource.username=tracker +spring.datasource.url=jdbc:mysql://localhost:3306/tracker_registration_dev?useSSL=false&useTimezone=true&serverTimezone=UTC&useLegacyDatetimeCode=false +registration.server.endpoint=http://localhost:8083 diff --git a/applications/registration-server/src/test/java/test/pivotal/pal/tracker/registration/RegistrationAppTest.java b/applications/registration-server/src/test/java/test/pivotal/pal/tracker/registration/RegistrationAppTest.java new file mode 100644 index 0000000..8b54159 --- /dev/null +++ b/applications/registration-server/src/test/java/test/pivotal/pal/tracker/registration/RegistrationAppTest.java @@ -0,0 +1,22 @@ +package test.pivotal.pal.tracker.registration; + +import io.pivotal.pal.tracker.registration.App; +import org.junit.Test; +import org.springframework.web.client.RestTemplate; + +import static org.assertj.core.api.Assertions.assertThat; + +public class RegistrationAppTest { + + @Test + public void embedded() { + App.main(new String[]{}); + + RestTemplate restTemplate = new RestTemplate(); + + assertThat(restTemplate.getForObject("http://localhost:8181/accounts?ownerId=0", String.class)).isEqualTo("[]"); + assertThat(restTemplate.getForObject("http://localhost:8181/projects?accountId=0", String.class)).isEqualTo("[]"); + assertThat(restTemplate.getForObject("http://localhost:8181/projects/0", String.class)).isEqualTo(null); + assertThat(restTemplate.getForObject("http://localhost:8181/users/0", String.class)).isEqualTo(null); + } +} diff --git a/applications/registration-server/src/test/resources/application.properties b/applications/registration-server/src/test/resources/application.properties new file mode 100644 index 0000000..ff6f375 --- /dev/null +++ b/applications/registration-server/src/test/resources/application.properties @@ -0,0 +1,6 @@ +spring.application.name=registration-server + +server.port=8181 +spring.datasource.username=tracker +spring.datasource.url=jdbc:mysql://localhost:3306/tracker_registration_test?useSSL=false&useTimezone=true&serverTimezone=UTC&useLegacyDatetimeCode=false +registration.server.endpoint=http://localhost:8883 diff --git a/applications/server.gradle b/applications/server.gradle new file mode 100644 index 0000000..7da92d9 --- /dev/null +++ b/applications/server.gradle @@ -0,0 +1,14 @@ +apply plugin: "org.springframework.boot" +apply plugin: "io.spring.dependency-management" + +dependencies { + compile project(":components:rest-support") + + compile "org.springframework.boot:spring-boot-starter-web" + + compile "com.zaxxer:HikariCP:$hikariVersion" + compile "mysql:mysql-connector-java:$mysqlVersion" + compile "ch.qos.logback:logback-classic:$logbackVersion" + + testCompile project(":components:test-support") +} diff --git a/applications/timesheets-server/build.gradle b/applications/timesheets-server/build.gradle new file mode 100644 index 0000000..0926483 --- /dev/null +++ b/applications/timesheets-server/build.gradle @@ -0,0 +1,5 @@ +apply from: "$projectDir/../server.gradle" + +dependencies { + compile project(":components:timesheets") +} diff --git a/applications/timesheets-server/src/main/java/io/pivotal/pal/tracker/timesheets/App.java b/applications/timesheets-server/src/main/java/io/pivotal/pal/tracker/timesheets/App.java new file mode 100644 index 0000000..9df368a --- /dev/null +++ b/applications/timesheets-server/src/main/java/io/pivotal/pal/tracker/timesheets/App.java @@ -0,0 +1,29 @@ +package io.pivotal.pal.tracker.timesheets; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.web.client.RestOperations; + +import java.util.TimeZone; + + +@SpringBootApplication +@ComponentScan({"io.pivotal.pal.tracker.timesheets", "io.pivotal.pal.tracker.restsupport"}) +public class App { + + public static void main(String[] args) { + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + SpringApplication.run(App.class, args); + } + + @Bean + ProjectClient projectClient( + RestOperations restOperations, + @Value("${registration.server.endpoint}") String registrationEndpoint + ) { + return new ProjectClient(restOperations, registrationEndpoint); + } +} diff --git a/applications/timesheets-server/src/main/resources/application.properties b/applications/timesheets-server/src/main/resources/application.properties new file mode 100644 index 0000000..606bf9d --- /dev/null +++ b/applications/timesheets-server/src/main/resources/application.properties @@ -0,0 +1,6 @@ +spring.application.name=timesheets-server + +server.port=8084 +spring.datasource.username=tracker +spring.datasource.url=jdbc:mysql://localhost:3306/tracker_timesheets_dev?useSSL=false&useTimezone=true&serverTimezone=UTC&useLegacyDatetimeCode=false +registration.server.endpoint=http://localhost:8083 diff --git a/applications/timesheets-server/src/test/java/test/pivotal/pal/tracker/timesheets/TimesheetsAppTest.java b/applications/timesheets-server/src/test/java/test/pivotal/pal/tracker/timesheets/TimesheetsAppTest.java new file mode 100644 index 0000000..b6eeae9 --- /dev/null +++ b/applications/timesheets-server/src/test/java/test/pivotal/pal/tracker/timesheets/TimesheetsAppTest.java @@ -0,0 +1,19 @@ +package test.pivotal.pal.tracker.timesheets; + +import io.pivotal.pal.tracker.timesheets.App; +import org.junit.Test; +import org.springframework.web.client.RestTemplate; + +import static org.assertj.core.api.Assertions.assertThat; + +public class TimesheetsAppTest { + + @Test + public void embedded() { + App.main(new String[]{}); + + String response = new RestTemplate().getForObject("http://localhost:8181/time-entries?userId=0", String.class); + + assertThat(response).isEqualTo("[]"); + } +} diff --git a/applications/timesheets-server/src/test/resources/application.properties b/applications/timesheets-server/src/test/resources/application.properties new file mode 100644 index 0000000..928f52e --- /dev/null +++ b/applications/timesheets-server/src/test/resources/application.properties @@ -0,0 +1,6 @@ +spring.application.name=timesheets-server + +server.port=8181 +spring.datasource.username=tracker +spring.datasource.url=jdbc:mysql://localhost:3306/tracker_timesheets_test?useSSL=false&useTimezone=true&serverTimezone=UTC&useLegacyDatetimeCode=false +registration.server.endpoint=http://localhost:8883 diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..2058cb6 --- /dev/null +++ b/build.gradle @@ -0,0 +1,59 @@ +import io.pivotal.pal.tracker.gradlebuild.DependenciesGraphPlugin + +buildscript { + ext { + springBootVersion = "2.0.6.RELEASE" + springVersion = "5.0.9.RELEASE" + mysqlVersion = "8.0.13" + jacksonVersion = "2.9.7" + slf4jVersion = "1.7.25" + mockitoVersion = "2.23.0" + assertJVersion = "3.11.1" + hikariVersion = "3.1.0" + logbackVersion = "1.2.3" + junitVersion = "4.12" + okhttpVersion = "3.12.0" + jsonPathVersion = "2.4.0" + } + + repositories { + mavenCentral() + jcenter() + } + + dependencies { + classpath "org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion" + classpath "mysql:mysql-connector-java:$mysqlVersion" + } +} + +apply plugin: DependenciesGraphPlugin + +subprojects { + group "io.pivotal.pal.tracker" + + apply plugin: "java" + defaultTasks "clean", "build" + + repositories { + mavenCentral() + jcenter() + } + + dependencies { + compile "com.fasterxml.jackson.core:jackson-core:$jacksonVersion" + compile "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion" + compile "com.fasterxml.jackson.core:jackson-annotations:$jacksonVersion" + compile "org.slf4j:slf4j-api:$slf4jVersion" + + testCompile "junit:junit:$junitVersion" + testCompile "org.mockito:mockito-core:$mockitoVersion" + testCompile "org.assertj:assertj-core:$assertJVersion" + } + + test { + testLogging { + exceptionFormat = 'full' + } + } +} diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle new file mode 100644 index 0000000..93f622c --- /dev/null +++ b/buildSrc/build.gradle @@ -0,0 +1,7 @@ +repositories { + mavenCentral() +} + +dependencies { + compile "org.flywaydb:flyway-gradle-plugin:5.2.1" +} diff --git a/buildSrc/src/main/groovy/io/pivotal/pal/tracker/gradlebuild/CfMigrationPlugin.groovy b/buildSrc/src/main/groovy/io/pivotal/pal/tracker/gradlebuild/CfMigrationPlugin.groovy new file mode 100644 index 0000000..59810ef --- /dev/null +++ b/buildSrc/src/main/groovy/io/pivotal/pal/tracker/gradlebuild/CfMigrationPlugin.groovy @@ -0,0 +1,123 @@ +package io.pivotal.pal.tracker.gradlebuild + +import groovy.json.JsonSlurper +import org.flywaydb.gradle.FlywayExtension +import org.flywaydb.gradle.task.FlywayMigrateTask +import org.flywaydb.gradle.task.FlywayRepairTask +import org.gradle.api.Plugin +import org.gradle.api.Project + +class CfMigrationPlugin implements Plugin { + private final static int TUNNEL_PORT = 63306 + private static final String KEY_NAME = 'flyway-migration-key' + + @Override + void apply(Project project) { + Process tunnelProcess = null + Map credentials = null + + project.with { + afterEvaluate { + def databases = project.extensions.findByType(DatabasesExtension) + def appName = databases.cfApp + def databaseInstanceName = databases.cfDatabase + + task( "acquireCredentials") { + doLast { + println "Acquiring database credentials" + credentials = acquireMysqlCredentials(databaseInstanceName) + } + } + + task("openTunnel") { + dependsOn "acquireCredentials" + doLast { + println "Opening Tunnel for $appName" + Thread.start { + tunnelProcess = "cf ssh -N -L ${TUNNEL_PORT}:${credentials['hostname']}:${credentials['port']} $appName".execute() + } + + waitForTunnelConnectivity() + } + } + + task("closeTunnel") { + doLast { + println "Closing Tunnel" + tunnelProcess?.destroyForcibly() + } + } + + task("cfMigrate", type: FlywayMigrateTask, group: "Migration") { + dependsOn "openTunnel" + finalizedBy "closeTunnel" + doFirst { extension = buildFlywayExtension(project, credentials) } + } + + task("cfRepair", type: FlywayRepairTask, group: "Migration") { + dependsOn "openTunnel" + finalizedBy "closeTunnel" + doFirst { extension = buildFlywayExtension(project, credentials) } + } + } + } + } + + private static void waitForTunnelConnectivity() { + int remainingAttempts = 20 + while (remainingAttempts > 0) { + remainingAttempts-- + try { + new Socket('localhost', TUNNEL_PORT).close() + remainingAttempts = 0 + } catch (ConnectException e) { + println "Waiting for tunnel ($remainingAttempts attempts remaining)" + sleep 1_000L + } + } + } + + private static def buildFlywayExtension(Project project, Map credentials) { + def extension = new FlywayExtension() + + extension.user = credentials['username'] + extension.password = credentials['password'] + def sslParam = '' + switch (credentials['jdbcUrl']) { + case ~/.*\buseSSL=false\b.*/: + sslParam = '?useSSL=false' + break + case ~/.*\buseSSL=true\b.*/: + sslParam = '?useSSL=true' + break + } + extension.url = "jdbc:mysql://127.0.0.1:${TUNNEL_PORT}/${credentials['name']}${sslParam}" + + extension.locations = ["filesystem:$project.projectDir/migrations"] + return extension + } + + // Some services store their credentials in credhub, so they are + // not available in VCAP_SERVICES seen by clients. Therefore, we + // create a service key and then obtain the database credentials + // from that value. Key creation appears idempotent, so there + // is no need to check for prior existence. + private static Map acquireMysqlCredentials(databaseInstanceName) { + execute(['cf', 'create-service-key', databaseInstanceName, KEY_NAME]) + + def serviceKeyJson = execute(['cf', 'service-key', databaseInstanceName, KEY_NAME]) + .replaceFirst(/(?s)^[^{]*/, '') + + return new JsonSlurper().parseText(serviceKeyJson) as Map + } + + private static String execute(List args) { + println "Executing command: ${args.join(' ')}" + def process = args.execute() + def output = process.text + process.waitFor() + + println "Result of command: ${output}" + return output + } +} diff --git a/buildSrc/src/main/groovy/io/pivotal/pal/tracker/gradlebuild/DatabasesExtension.groovy b/buildSrc/src/main/groovy/io/pivotal/pal/tracker/gradlebuild/DatabasesExtension.groovy new file mode 100644 index 0000000..b04d0a2 --- /dev/null +++ b/buildSrc/src/main/groovy/io/pivotal/pal/tracker/gradlebuild/DatabasesExtension.groovy @@ -0,0 +1,8 @@ +package io.pivotal.pal.tracker.gradlebuild + +class DatabasesExtension { + String devDatabase + String testDatabase + String cfDatabase + String cfApp +} diff --git a/buildSrc/src/main/groovy/io/pivotal/pal/tracker/gradlebuild/DependenciesGraphPlugin.groovy b/buildSrc/src/main/groovy/io/pivotal/pal/tracker/gradlebuild/DependenciesGraphPlugin.groovy new file mode 100644 index 0000000..eebd1f0 --- /dev/null +++ b/buildSrc/src/main/groovy/io/pivotal/pal/tracker/gradlebuild/DependenciesGraphPlugin.groovy @@ -0,0 +1,66 @@ +package io.pivotal.pal.tracker.gradlebuild + +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.artifacts.Configuration +import org.gradle.api.artifacts.UnknownConfigurationException +import org.gradle.api.tasks.Delete +import org.gradle.api.tasks.Exec + +class DependenciesGraphPlugin implements Plugin { + + @Override + void apply(Project project) { + + project.with { + task("clean", type: Delete) { + delete "build" + } + + task("dependenciesGraphDot") { + mustRunAfter "clean" + group = "DependenciesGraph" + description = "Generate DOT file" + + def graphBuildDir = "build/dependenciesGraph" + def dotFile = file "$graphBuildDir/graph.dot" + + doLast { + delete graphBuildDir + mkdir graphBuildDir + + dotFile << "digraph dependencies {\n" + + subprojects.forEach { Project subProject -> + if (isProjectExcluded(subProject)) { + return + } + + try { + Configuration compileConfig = subProject.configurations["compile"] + + compileConfig + .dependencies + .grep { it.respondsTo("getDependencyProject") && !isProjectExcluded(it) } + .forEach { dotFile << """ "$subProject.name" -> "$it.dependencyProject.name"\n""" } + } catch (UnknownConfigurationException ignored) { + } + } + + dotFile << "}\n" + } + } + + task("dependenciesGraph", dependsOn: "dependenciesGraphDot", type: Exec) { + workingDir "$buildDir/dependenciesGraph" + commandLine "dot", "-O", "-Tpng", "graph.dot" + group = "DependenciesGraph" + description = "Generate PNG file" + } + } + } + + private static boolean isProjectExcluded(def project) { + return project.name.contains("support") + } +} diff --git a/buildSrc/src/main/groovy/io/pivotal/pal/tracker/gradlebuild/LocalMigrationPlugin.groovy b/buildSrc/src/main/groovy/io/pivotal/pal/tracker/gradlebuild/LocalMigrationPlugin.groovy new file mode 100644 index 0000000..da7a43a --- /dev/null +++ b/buildSrc/src/main/groovy/io/pivotal/pal/tracker/gradlebuild/LocalMigrationPlugin.groovy @@ -0,0 +1,46 @@ +package io.pivotal.pal.tracker.gradlebuild + +import org.flywaydb.gradle.FlywayExtension +import org.flywaydb.gradle.task.FlywayCleanTask +import org.flywaydb.gradle.task.FlywayMigrateTask +import org.flywaydb.gradle.task.FlywayRepairTask +import org.gradle.api.Plugin +import org.gradle.api.Project + +class LocalMigrationPlugin implements Plugin { + + @Override + void apply(Project project) { + + project.with { + def databases = new DatabasesExtension() + + extensions.add("flyway", new FlywayExtension()) + extensions.add("databases", databases) + + afterEvaluate { + addDbTask(project, "dev", databases.devDatabase) + addDbTask(project, "test", databases.testDatabase) + } + } + } + + private static addDbTask(Project project, String name, String dbName) { + def flywayExtension = buildFlywayExtension(project, dbName) + + project.task("${name}Migrate", type: FlywayMigrateTask, group: "Migration") { extension = flywayExtension } + project.task("${name}Clean", type: FlywayCleanTask, group: "Migration") { extension = flywayExtension } + project.task("${name}Repair", type: FlywayRepairTask, group: "Migration") { extension = flywayExtension } + } + + private static FlywayExtension buildFlywayExtension(Project project, String dbName) { + def ext = new FlywayExtension() + ext.with { + url = "jdbc:mysql://localhost:3306/$dbName?useSSL=false&serverTimezone=UTC" + user = "tracker" + outOfOrder = false + locations = ["filesystem:${project.projectDir}"] + } + return ext + } +} diff --git a/components/accounts/build.gradle b/components/accounts/build.gradle new file mode 100644 index 0000000..0eaa050 --- /dev/null +++ b/components/accounts/build.gradle @@ -0,0 +1,7 @@ +dependencies { + compile project(":components:rest-support") + compile project(":components:users") + compile "org.springframework:spring-jdbc:$springVersion" + + testCompile project(":components:test-support") +} diff --git a/components/accounts/src/main/java/io/pivotal/pal/tracker/accounts/AccountController.java b/components/accounts/src/main/java/io/pivotal/pal/tracker/accounts/AccountController.java new file mode 100644 index 0000000..3aa8161 --- /dev/null +++ b/components/accounts/src/main/java/io/pivotal/pal/tracker/accounts/AccountController.java @@ -0,0 +1,39 @@ +package io.pivotal.pal.tracker.accounts; + +import io.pivotal.pal.tracker.accounts.data.AccountDataGateway; +import io.pivotal.pal.tracker.accounts.data.AccountRecord; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +import static io.pivotal.pal.tracker.accounts.AccountInfo.accountInfoBuilder; +import static java.util.stream.Collectors.toList; + +@RestController +public class AccountController { + + private final AccountDataGateway gateway; + + public AccountController(AccountDataGateway gateway) { + this.gateway = gateway; + } + + @GetMapping("/accounts") + public List list(@RequestParam long ownerId) { + return gateway.findAllByOwnerId(ownerId) + .stream() + .map(this::present) + .collect(toList()); + } + + private AccountInfo present(AccountRecord record) { + return accountInfoBuilder() + .id(record.id) + .ownerId(record.ownerId) + .name(record.name) + .info("account info") + .build(); + } +} diff --git a/components/accounts/src/main/java/io/pivotal/pal/tracker/accounts/AccountInfo.java b/components/accounts/src/main/java/io/pivotal/pal/tracker/accounts/AccountInfo.java new file mode 100644 index 0000000..0dda9c1 --- /dev/null +++ b/components/accounts/src/main/java/io/pivotal/pal/tracker/accounts/AccountInfo.java @@ -0,0 +1,88 @@ +package io.pivotal.pal.tracker.accounts; + +public class AccountInfo { + + public final long id; + public final long ownerId; + public final String name; + public final String info; + + private AccountInfo() { // for jackson + this(accountInfoBuilder()); + } + + private AccountInfo(Builder builder) { + id = builder.id; + ownerId = builder.ownerId; + name = builder.name; + info = builder.info; + } + + public static Builder accountInfoBuilder() { + return new Builder(); + } + + public static class Builder { + private long id; + private long ownerId; + private String name; + private String info; + + public AccountInfo build() { + return new AccountInfo(this); + } + + public Builder id(long id) { + this.id = id; + return this; + } + + public Builder ownerId(long ownerId) { + this.ownerId = ownerId; + return this; + } + + public Builder name(String name) { + this.name = name; + return this; + } + + public Builder info(String info) { + this.info = info; + return this; + } + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + AccountInfo that = (AccountInfo) o; + + if (id != that.id) return false; + if (ownerId != that.ownerId) return false; + if (name != null ? !name.equals(that.name) : that.name != null) + return false; + return info != null ? info.equals(that.info) : that.info == null; + } + + @Override + public int hashCode() { + int result = (int) (id ^ (id >>> 32)); + result = 31 * result + (int) (ownerId ^ (ownerId >>> 32)); + result = 31 * result + (name != null ? name.hashCode() : 0); + result = 31 * result + (info != null ? info.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "AccountInfo{" + + "id=" + id + + ", ownerId=" + ownerId + + ", name='" + name + '\'' + + ", info='" + info + '\'' + + '}'; + } +} diff --git a/components/accounts/src/main/java/io/pivotal/pal/tracker/accounts/RegistrationController.java b/components/accounts/src/main/java/io/pivotal/pal/tracker/accounts/RegistrationController.java new file mode 100644 index 0000000..5234f64 --- /dev/null +++ b/components/accounts/src/main/java/io/pivotal/pal/tracker/accounts/RegistrationController.java @@ -0,0 +1,22 @@ +package io.pivotal.pal.tracker.accounts; + +import io.pivotal.pal.tracker.users.UserInfo; +import io.pivotal.pal.tracker.users.data.UserRecord; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class RegistrationController { + private final RegistrationService service; + + public RegistrationController(RegistrationService service) { + this.service = service; + } + + @PostMapping("/registration") + public UserInfo create(@RequestBody RegistrationForm form) { + UserRecord record = service.createUserWithAccount(form.name); + return new UserInfo(record.id, record.name, "registration info"); + } +} diff --git a/components/accounts/src/main/java/io/pivotal/pal/tracker/accounts/RegistrationForm.java b/components/accounts/src/main/java/io/pivotal/pal/tracker/accounts/RegistrationForm.java new file mode 100644 index 0000000..bc269a0 --- /dev/null +++ b/components/accounts/src/main/java/io/pivotal/pal/tracker/accounts/RegistrationForm.java @@ -0,0 +1,36 @@ +package io.pivotal.pal.tracker.accounts; + +public class RegistrationForm { + + public final String name; + + public RegistrationForm(String name) { + this.name = name; + } + + private RegistrationForm() { + this(null); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + RegistrationForm that = (RegistrationForm) o; + + return name != null ? name.equals(that.name) : that.name == null; + } + + @Override + public int hashCode() { + return name != null ? name.hashCode() : 0; + } + + @Override + public String toString() { + return "RegistrationForm{" + + "name='" + name + '\'' + + '}'; + } +} diff --git a/components/accounts/src/main/java/io/pivotal/pal/tracker/accounts/RegistrationService.java b/components/accounts/src/main/java/io/pivotal/pal/tracker/accounts/RegistrationService.java new file mode 100644 index 0000000..87b5eb0 --- /dev/null +++ b/components/accounts/src/main/java/io/pivotal/pal/tracker/accounts/RegistrationService.java @@ -0,0 +1,26 @@ +package io.pivotal.pal.tracker.accounts; + +import io.pivotal.pal.tracker.accounts.data.AccountDataGateway; +import io.pivotal.pal.tracker.users.data.UserDataGateway; +import io.pivotal.pal.tracker.users.data.UserRecord; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +public class RegistrationService { + + private final UserDataGateway userDataGateway; + private final AccountDataGateway accountDataGateway; + + public RegistrationService(UserDataGateway userDataGateway, AccountDataGateway accountDataGateway) { + this.userDataGateway = userDataGateway; + this.accountDataGateway = accountDataGateway; + } + + @Transactional + public UserRecord createUserWithAccount(String name) { + UserRecord user = userDataGateway.create(name); + accountDataGateway.create(user.id, String.format("%s's account", name)); + return user; + } +} diff --git a/components/accounts/src/main/java/io/pivotal/pal/tracker/accounts/data/AccountDataGateway.java b/components/accounts/src/main/java/io/pivotal/pal/tracker/accounts/data/AccountDataGateway.java new file mode 100644 index 0000000..607d277 --- /dev/null +++ b/components/accounts/src/main/java/io/pivotal/pal/tracker/accounts/data/AccountDataGateway.java @@ -0,0 +1,54 @@ +package io.pivotal.pal.tracker.accounts.data; + +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.jdbc.support.GeneratedKeyHolder; +import org.springframework.jdbc.support.KeyHolder; +import org.springframework.stereotype.Repository; + +import javax.sql.DataSource; +import java.sql.PreparedStatement; +import java.util.List; + +import static io.pivotal.pal.tracker.accounts.data.AccountRecord.accountRecordBuilder; +import static java.sql.Statement.RETURN_GENERATED_KEYS; + +@Repository +public class AccountDataGateway { + private final JdbcTemplate jdbcTemplate; + + public AccountDataGateway(DataSource dataSource) { + this.jdbcTemplate = new JdbcTemplate(dataSource); + } + + public AccountRecord create(long ownerId, String name) { + KeyHolder keyholder = new GeneratedKeyHolder(); + + jdbcTemplate.update(connection -> { + PreparedStatement ps = connection.prepareStatement( + "insert into accounts (owner_id, name) values (?, ?)", RETURN_GENERATED_KEYS + ); + + ps.setLong(1, ownerId); + ps.setString(2, name); + return ps; + }, keyholder); + + long id = keyholder.getKey().longValue(); + + return jdbcTemplate.queryForObject("select id, owner_id, name from accounts where id = ?", rowMapper, id); + } + + public List findAllByOwnerId(long ownerId) { + return jdbcTemplate.query( + "select id, owner_id, name from accounts where owner_id = ? order by name desc limit 1", + rowMapper, ownerId + ); + } + + private RowMapper rowMapper = (rs, num) -> accountRecordBuilder() + .id(rs.getLong("id")) + .ownerId(rs.getLong("owner_id")) + .name(rs.getString("name")) + .build(); +} diff --git a/components/accounts/src/main/java/io/pivotal/pal/tracker/accounts/data/AccountRecord.java b/components/accounts/src/main/java/io/pivotal/pal/tracker/accounts/data/AccountRecord.java new file mode 100644 index 0000000..8864f4d --- /dev/null +++ b/components/accounts/src/main/java/io/pivotal/pal/tracker/accounts/data/AccountRecord.java @@ -0,0 +1,72 @@ +package io.pivotal.pal.tracker.accounts.data; + +public class AccountRecord { + + public final long id; + public final long ownerId; + public final String name; + + private AccountRecord(Builder builder) { + this.id = builder.id; + this.ownerId = builder.ownerId; + this.name = builder.name; + } + + public static Builder accountRecordBuilder() { + return new Builder(); + } + + public static class Builder { + private long id; + private long ownerId; + private String name; + + public AccountRecord build() { + return new AccountRecord(this); + } + + public Builder id(long id) { + this.id = id; + return this; + } + + public Builder ownerId(long ownerId) { + this.ownerId = ownerId; + return this; + } + + public Builder name(String name) { + this.name = name; + return this; + } + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + AccountRecord that = (AccountRecord) o; + + if (id != that.id) return false; + if (ownerId != that.ownerId) return false; + return name != null ? name.equals(that.name) : that.name == null; + } + + @Override + public int hashCode() { + int result = (int) (id ^ (id >>> 32)); + result = 31 * result + (int) (ownerId ^ (ownerId >>> 32)); + result = 31 * result + (name != null ? name.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "AccountRecord{" + + "id=" + id + + ", ownerId=" + ownerId + + ", name='" + name + '\'' + + '}'; + } +} diff --git a/components/accounts/src/test/java/test/pivotal/pal/tracker/accounts/AccountControllerTest.java b/components/accounts/src/test/java/test/pivotal/pal/tracker/accounts/AccountControllerTest.java new file mode 100644 index 0000000..9c4d6b0 --- /dev/null +++ b/components/accounts/src/test/java/test/pivotal/pal/tracker/accounts/AccountControllerTest.java @@ -0,0 +1,45 @@ +package test.pivotal.pal.tracker.accounts; + +import io.pivotal.pal.tracker.accounts.AccountController; +import io.pivotal.pal.tracker.accounts.AccountInfo; +import io.pivotal.pal.tracker.accounts.data.AccountDataGateway; +import io.pivotal.pal.tracker.accounts.data.AccountRecord; +import org.junit.Test; + +import java.util.List; + +import static io.pivotal.pal.tracker.accounts.AccountInfo.accountInfoBuilder; +import static io.pivotal.pal.tracker.accounts.data.AccountRecord.accountRecordBuilder; +import static java.util.Collections.singletonList; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.*; + +public class AccountControllerTest { + + private AccountDataGateway gateway = mock(AccountDataGateway.class); + private AccountController controller = new AccountController(gateway); + + @Test + public void testList() { + AccountRecord recordToFind = accountRecordBuilder() + .id(13L) + .ownerId(2L) + .name("Some Name") + .build(); + doReturn(singletonList(recordToFind)).when(gateway).findAllByOwnerId(anyLong()); + + + List result = controller.list(13); + + + verify(gateway).findAllByOwnerId(13L); + assertThat(result).containsExactly(accountInfoBuilder() + .id(13L) + .ownerId(2L) + .name("Some Name") + .info("account info") + .build() + ); + } +} diff --git a/components/accounts/src/test/java/test/pivotal/pal/tracker/accounts/RegistrationControllerTest.java b/components/accounts/src/test/java/test/pivotal/pal/tracker/accounts/RegistrationControllerTest.java new file mode 100644 index 0000000..9b0a7d3 --- /dev/null +++ b/components/accounts/src/test/java/test/pivotal/pal/tracker/accounts/RegistrationControllerTest.java @@ -0,0 +1,31 @@ +package test.pivotal.pal.tracker.accounts; + +import io.pivotal.pal.tracker.accounts.RegistrationController; +import io.pivotal.pal.tracker.accounts.RegistrationForm; +import io.pivotal.pal.tracker.accounts.RegistrationService; +import io.pivotal.pal.tracker.users.UserInfo; +import io.pivotal.pal.tracker.users.data.UserRecord; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +public class RegistrationControllerTest { + + private RegistrationService registrationService = mock(RegistrationService.class); + private RegistrationController registrationController = new RegistrationController(registrationService); + + @Test + public void create() { + UserRecord userRecord = new UserRecord(24L, "Billy"); + doReturn(userRecord).when(registrationService).createUserWithAccount(any()); + + + UserInfo result = registrationController.create(new RegistrationForm("Billy")); + + + verify(registrationService).createUserWithAccount("Billy"); + assertThat(result).isEqualTo(new UserInfo(24L, "Billy", "registration info")); + } +} diff --git a/components/accounts/src/test/java/test/pivotal/pal/tracker/accounts/RegistrationServiceTest.java b/components/accounts/src/test/java/test/pivotal/pal/tracker/accounts/RegistrationServiceTest.java new file mode 100644 index 0000000..2b66392 --- /dev/null +++ b/components/accounts/src/test/java/test/pivotal/pal/tracker/accounts/RegistrationServiceTest.java @@ -0,0 +1,32 @@ +package test.pivotal.pal.tracker.accounts; + +import io.pivotal.pal.tracker.accounts.RegistrationService; +import io.pivotal.pal.tracker.accounts.data.AccountDataGateway; +import io.pivotal.pal.tracker.users.data.UserDataGateway; +import io.pivotal.pal.tracker.users.data.UserRecord; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.*; + +public class RegistrationServiceTest { + private UserDataGateway userDataGateway = mock(UserDataGateway.class); + private AccountDataGateway accountDataGateway = mock(AccountDataGateway.class); + private RegistrationService service = new RegistrationService(userDataGateway, accountDataGateway); + + @Test + public void testCreateUserWithAccount() { + UserRecord createdUser = new UserRecord(22L, "Some User"); + doReturn(createdUser).when(userDataGateway).create("Some User"); + + + UserRecord result = service.createUserWithAccount("Some User"); + + + verify(userDataGateway).create("Some User"); + verify(accountDataGateway).create(22L, "Some User's account"); + + UserRecord expectedResult = new UserRecord(22L, "Some User"); + assertThat(result).isEqualTo(expectedResult); + } +} diff --git a/components/accounts/src/test/java/test/pivotal/pal/tracker/accounts/data/AccountDataGatewayTest.java b/components/accounts/src/test/java/test/pivotal/pal/tracker/accounts/data/AccountDataGatewayTest.java new file mode 100644 index 0000000..e9fce58 --- /dev/null +++ b/components/accounts/src/test/java/test/pivotal/pal/tracker/accounts/data/AccountDataGatewayTest.java @@ -0,0 +1,62 @@ +package test.pivotal.pal.tracker.accounts.data; + +import io.pivotal.pal.tracker.accounts.data.AccountDataGateway; +import io.pivotal.pal.tracker.accounts.data.AccountRecord; +import io.pivotal.pal.tracker.testsupport.TestScenarioSupport; +import org.junit.Before; +import org.junit.Test; +import org.springframework.jdbc.core.JdbcTemplate; + +import java.util.List; +import java.util.Map; + +import static io.pivotal.pal.tracker.accounts.data.AccountRecord.accountRecordBuilder; +import static org.assertj.core.api.Assertions.assertThat; + +public class AccountDataGatewayTest { + + private TestScenarioSupport testScenarioSupport = new TestScenarioSupport("tracker_registration_test"); + private JdbcTemplate template = testScenarioSupport.template; + private AccountDataGateway gateway = new AccountDataGateway(testScenarioSupport.dataSource); + + @Before + public void setup() { + template.execute("DELETE FROM projects;"); + template.execute("DELETE FROM accounts;"); + template.execute("DELETE FROM users;"); + } + + @Test + public void testCreate() { + template.execute("insert into users (id, name) values (12, 'Jack')"); + + + AccountRecord created = gateway.create(12L, "anAccount"); + + + assertThat(created.id).isNotNull(); + assertThat(created.name).isEqualTo("anAccount"); + assertThat(created.ownerId).isEqualTo(12); + + Map persisted = template.queryForMap("SELECT * FROM accounts WHERE id = ?", created.id); + assertThat(persisted.get("name")).isEqualTo("anAccount"); + assertThat(persisted.get("owner_id")).isEqualTo(12L); + } + + @Test + public void testFindBy() { + template.execute("insert into users (id, name) values (12, 'Jack')"); + template.execute("insert into accounts (id, owner_id, name) values (1, 12, 'anAccount')"); + + + List result = gateway.findAllByOwnerId(12L); + + + assertThat(result).containsExactly(accountRecordBuilder() + .id(1L) + .ownerId(12L) + .name("anAccount") + .build() + ); + } +} diff --git a/components/allocations/build.gradle b/components/allocations/build.gradle new file mode 100644 index 0000000..4526bf7 --- /dev/null +++ b/components/allocations/build.gradle @@ -0,0 +1,6 @@ +dependencies { + compile project(":components:rest-support") + compile "org.springframework:spring-jdbc:$springVersion" + + testCompile project(":components:test-support") +} diff --git a/components/allocations/src/main/java/io/pivotal/pal/tracker/allocations/AllocationController.java b/components/allocations/src/main/java/io/pivotal/pal/tracker/allocations/AllocationController.java new file mode 100644 index 0000000..21318a5 --- /dev/null +++ b/components/allocations/src/main/java/io/pivotal/pal/tracker/allocations/AllocationController.java @@ -0,0 +1,75 @@ +package io.pivotal.pal.tracker.allocations; + +import io.pivotal.pal.tracker.allocations.data.AllocationDataGateway; +import io.pivotal.pal.tracker.allocations.data.AllocationFields; +import io.pivotal.pal.tracker.allocations.data.AllocationRecord; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.time.LocalDate; +import java.util.List; + +import static io.pivotal.pal.tracker.allocations.AllocationInfo.allocationInfoBuilder; +import static io.pivotal.pal.tracker.allocations.data.AllocationFields.allocationFieldsBuilder; +import static java.util.stream.Collectors.toList; + +@RestController +@RequestMapping("/allocations") +public class AllocationController { + + private final AllocationDataGateway gateway; + private final ProjectClient client; + + public AllocationController(AllocationDataGateway gateway, ProjectClient client) { + this.gateway = gateway; + this.client = client; + } + + + @PostMapping + public ResponseEntity create(@RequestBody AllocationForm form) { + + if (projectIsActive(form.projectId)) { + AllocationRecord record = gateway.create(formToFields(form)); + return new ResponseEntity<>(present(record), HttpStatus.CREATED); + } + + return new ResponseEntity<>(HttpStatus.SERVICE_UNAVAILABLE); + } + + @GetMapping + public List list(@RequestParam long projectId) { + return gateway.findAllByProjectId(projectId) + .stream() + .map(this::present) + .collect(toList()); + } + + + private boolean projectIsActive(long projectId) { + ProjectInfo project = client.getProject(projectId); + + return project != null && project.active; + } + + private AllocationFields formToFields(AllocationForm form) { + return allocationFieldsBuilder() + .projectId(form.projectId) + .userId(form.userId) + .firstDay(LocalDate.parse(form.firstDay)) + .lastDay(LocalDate.parse(form.lastDay)) + .build(); + } + + private AllocationInfo present(AllocationRecord record) { + return allocationInfoBuilder() + .id(record.id) + .projectId(record.projectId) + .userId(record.userId) + .firstDay(record.firstDay.toString()) + .lastDay(record.lastDay.toString()) + .info("allocation info") + .build(); + } +} diff --git a/components/allocations/src/main/java/io/pivotal/pal/tracker/allocations/AllocationForm.java b/components/allocations/src/main/java/io/pivotal/pal/tracker/allocations/AllocationForm.java new file mode 100644 index 0000000..35117be --- /dev/null +++ b/components/allocations/src/main/java/io/pivotal/pal/tracker/allocations/AllocationForm.java @@ -0,0 +1,89 @@ +package io.pivotal.pal.tracker.allocations; + +public class AllocationForm { + + public final long projectId; + public final long userId; + public final String firstDay; + public final String lastDay; + + private AllocationForm() { // for jackson + this(allocationFormBuilder()); + } + + public AllocationForm(Builder builder) { + projectId = builder.projectId; + userId = builder.userId; + firstDay = builder.firstDay; + lastDay = builder.lastDay; + } + + public static Builder allocationFormBuilder() { + return new Builder(); + } + + + public static class Builder { + private long projectId; + private long userId; + private String firstDay; + private String lastDay; + + public AllocationForm build() { + return new AllocationForm(this); + } + + public Builder projectId(long projectId) { + this.projectId = projectId; + return this; + } + + public Builder userId(long userId) { + this.userId = userId; + return this; + } + + public Builder firstDay(String firstDay) { + this.firstDay = firstDay; + return this; + } + + public Builder lastDay(String lastDay) { + this.lastDay = lastDay; + return this; + } + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + AllocationForm that = (AllocationForm) o; + + if (projectId != that.projectId) return false; + if (userId != that.userId) return false; + if (firstDay != null ? !firstDay.equals(that.firstDay) : that.firstDay != null) + return false; + return lastDay != null ? lastDay.equals(that.lastDay) : that.lastDay == null; + } + + @Override + public int hashCode() { + int result = (int) (projectId ^ (projectId >>> 32)); + result = 31 * result + (int) (userId ^ (userId >>> 32)); + result = 31 * result + (firstDay != null ? firstDay.hashCode() : 0); + result = 31 * result + (lastDay != null ? lastDay.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "AllocationForm{" + + "projectId=" + projectId + + ", userId=" + userId + + ", firstDay='" + firstDay + '\'' + + ", lastDay='" + lastDay + '\'' + + '}'; + } +} diff --git a/components/allocations/src/main/java/io/pivotal/pal/tracker/allocations/AllocationInfo.java b/components/allocations/src/main/java/io/pivotal/pal/tracker/allocations/AllocationInfo.java new file mode 100644 index 0000000..cdbce0b --- /dev/null +++ b/components/allocations/src/main/java/io/pivotal/pal/tracker/allocations/AllocationInfo.java @@ -0,0 +1,112 @@ +package io.pivotal.pal.tracker.allocations; + +public class AllocationInfo { + + public final long id; + public final long projectId; + public final long userId; + public final String firstDay; + public final String lastDay; + public final String info; + + private AllocationInfo() { // for jackson + this(allocationInfoBuilder()); + } + + public AllocationInfo(Builder builder) { + id = builder.id; + projectId = builder.projectId; + userId = builder.userId; + firstDay = builder.firstDay; + lastDay = builder.lastDay; + info = builder.info; + } + + public static Builder allocationInfoBuilder() { + return new Builder(); + } + + + public static class Builder { + private long id; + private long projectId; + private long userId; + private String firstDay; + private String lastDay; + private String info; + + public AllocationInfo build() { + return new AllocationInfo(this); + } + + public Builder id(long id) { + this.id = id; + return this; + } + + public Builder projectId(long projectId) { + this.projectId = projectId; + return this; + } + + public Builder userId(long userId) { + this.userId = userId; + return this; + } + + public Builder firstDay(String firstDay) { + this.firstDay = firstDay; + return this; + } + + public Builder lastDay(String lastDay) { + this.lastDay = lastDay; + return this; + } + + public Builder info(String info) { + this.info = info; + return this; + } + } + + @Override + public String toString() { + return "AllocationInfo{" + + "id=" + id + + ", projectId=" + projectId + + ", userId=" + userId + + ", firstDay=" + firstDay + + ", lastDay=" + lastDay + + ", info='" + info + '\'' + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + AllocationInfo info1 = (AllocationInfo) o; + + if (id != info1.id) return false; + if (projectId != info1.projectId) return false; + if (userId != info1.userId) return false; + if (firstDay != null ? !firstDay.equals(info1.firstDay) : info1.firstDay != null) + return false; + if (lastDay != null ? !lastDay.equals(info1.lastDay) : info1.lastDay != null) + return false; + return info != null ? info.equals(info1.info) : info1.info == null; + } + + @Override + public int hashCode() { + int result = (int) (id ^ (id >>> 32)); + result = 31 * result + (int) (projectId ^ (projectId >>> 32)); + result = 31 * result + (int) (userId ^ (userId >>> 32)); + result = 31 * result + (firstDay != null ? firstDay.hashCode() : 0); + result = 31 * result + (lastDay != null ? lastDay.hashCode() : 0); + result = 31 * result + (info != null ? info.hashCode() : 0); + return result; + } +} diff --git a/components/allocations/src/main/java/io/pivotal/pal/tracker/allocations/ProjectClient.java b/components/allocations/src/main/java/io/pivotal/pal/tracker/allocations/ProjectClient.java new file mode 100644 index 0000000..2358dab --- /dev/null +++ b/components/allocations/src/main/java/io/pivotal/pal/tracker/allocations/ProjectClient.java @@ -0,0 +1,18 @@ +package io.pivotal.pal.tracker.allocations; + +import org.springframework.web.client.RestOperations; + +public class ProjectClient { + + private final RestOperations restOperations; + private final String registrationServerEndpoint; + + public ProjectClient(RestOperations restOperations, String registrationServerEndpoint) { + this.restOperations= restOperations; + this.registrationServerEndpoint = registrationServerEndpoint; + } + + public ProjectInfo getProject(long projectId) { + return restOperations.getForObject(registrationServerEndpoint + "/projects/" + projectId, ProjectInfo.class); + } +} diff --git a/components/allocations/src/main/java/io/pivotal/pal/tracker/allocations/ProjectInfo.java b/components/allocations/src/main/java/io/pivotal/pal/tracker/allocations/ProjectInfo.java new file mode 100644 index 0000000..3728382 --- /dev/null +++ b/components/allocations/src/main/java/io/pivotal/pal/tracker/allocations/ProjectInfo.java @@ -0,0 +1,37 @@ +package io.pivotal.pal.tracker.allocations; + +public class ProjectInfo { + + public final boolean active; + + private ProjectInfo() { + this(false); + } + + public ProjectInfo(boolean active) { + this.active = active; + } + + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + ProjectInfo that = (ProjectInfo) o; + + return active == that.active; + } + + @Override + public int hashCode() { + return (active ? 1 : 0); + } + + @Override + public String toString() { + return "ProjectInfo{" + + "active=" + active + + '}'; + } +} diff --git a/components/allocations/src/main/java/io/pivotal/pal/tracker/allocations/data/AllocationDataGateway.java b/components/allocations/src/main/java/io/pivotal/pal/tracker/allocations/data/AllocationDataGateway.java new file mode 100644 index 0000000..f996307 --- /dev/null +++ b/components/allocations/src/main/java/io/pivotal/pal/tracker/allocations/data/AllocationDataGateway.java @@ -0,0 +1,67 @@ +package io.pivotal.pal.tracker.allocations.data; + +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.jdbc.support.GeneratedKeyHolder; +import org.springframework.jdbc.support.KeyHolder; +import org.springframework.stereotype.Repository; + +import javax.sql.DataSource; +import java.sql.Date; +import java.sql.PreparedStatement; +import java.util.List; + +import static java.sql.Statement.RETURN_GENERATED_KEYS; + +@Repository +public class AllocationDataGateway { + + private JdbcTemplate jdbcTemplate; + + public AllocationDataGateway(DataSource dataSource) { + this.jdbcTemplate = new JdbcTemplate(dataSource); + } + + + public AllocationRecord create(AllocationFields fields) { + KeyHolder keyHolder = new GeneratedKeyHolder(); + + jdbcTemplate.update(connection -> { + PreparedStatement ps = connection.prepareStatement( + "insert into allocations (project_id, user_id, first_day, last_day) values (?, ?, ?, ?)", RETURN_GENERATED_KEYS + ); + + ps.setLong(1, fields.projectId); + ps.setLong(2, fields.userId); + ps.setDate(3, Date.valueOf(fields.firstDay)); + ps.setDate(4, Date.valueOf(fields.lastDay)); + return ps; + }, keyHolder); + + return find(keyHolder.getKey().longValue()); + } + + public List findAllByProjectId(Long projectId) { + return jdbcTemplate.query( + "select id, project_id, user_id, first_day, last_day from allocations where project_id = ? order by first_day", + rowMapper, projectId + ); + } + + + private AllocationRecord find(long id) { + return jdbcTemplate.queryForObject( + "select id, project_id, user_id, first_day, last_day from allocations where id = ?", + rowMapper, id + ); + } + + private RowMapper rowMapper = + (rs, rowNum) -> AllocationRecord.allocationRecordBuilder() + .id(rs.getLong("id")) + .projectId(rs.getLong("project_id")) + .userId(rs.getLong("user_id")) + .firstDay(rs.getDate("first_day").toLocalDate()) + .lastDay(rs.getDate("last_day").toLocalDate()) + .build(); +} diff --git a/components/allocations/src/main/java/io/pivotal/pal/tracker/allocations/data/AllocationFields.java b/components/allocations/src/main/java/io/pivotal/pal/tracker/allocations/data/AllocationFields.java new file mode 100644 index 0000000..61f73fb --- /dev/null +++ b/components/allocations/src/main/java/io/pivotal/pal/tracker/allocations/data/AllocationFields.java @@ -0,0 +1,87 @@ +package io.pivotal.pal.tracker.allocations.data; + +import java.time.LocalDate; + +public class AllocationFields { + + public final long projectId; + public final long userId; + public final LocalDate firstDay; + public final LocalDate lastDay; + + public AllocationFields(Builder builder) { + projectId = builder.projectId; + userId = builder.userId; + firstDay = builder.firstDay; + lastDay = builder.lastDay; + } + + public static Builder allocationFieldsBuilder() { + return new Builder(); + } + + public static class Builder { + private long projectId; + private long userId; + private LocalDate firstDay; + private LocalDate lastDay; + + public AllocationFields build() { + return new AllocationFields(this); + } + + public Builder projectId(long projectId) { + this.projectId = projectId; + return this; + } + + public Builder userId(long userId) { + this.userId = userId; + return this; + } + + public Builder firstDay(LocalDate firstDay) { + this.firstDay = firstDay; + return this; + } + + public Builder lastDay(LocalDate lastDay) { + this.lastDay = lastDay; + return this; + } + } + + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + AllocationFields that = (AllocationFields) o; + + if (projectId != that.projectId) return false; + if (userId != that.userId) return false; + if (firstDay != null ? !firstDay.equals(that.firstDay) : that.firstDay != null) + return false; + return lastDay != null ? lastDay.equals(that.lastDay) : that.lastDay == null; + } + + @Override + public int hashCode() { + int result = (int) (projectId ^ (projectId >>> 32)); + result = 31 * result + (int) (userId ^ (userId >>> 32)); + result = 31 * result + (firstDay != null ? firstDay.hashCode() : 0); + result = 31 * result + (lastDay != null ? lastDay.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "AllocationFields{" + + "projectId=" + projectId + + ", userId=" + userId + + ", firstDay=" + firstDay + + ", lastDay=" + lastDay + + '}'; + } +} diff --git a/components/allocations/src/main/java/io/pivotal/pal/tracker/allocations/data/AllocationRecord.java b/components/allocations/src/main/java/io/pivotal/pal/tracker/allocations/data/AllocationRecord.java new file mode 100644 index 0000000..6381534 --- /dev/null +++ b/components/allocations/src/main/java/io/pivotal/pal/tracker/allocations/data/AllocationRecord.java @@ -0,0 +1,98 @@ +package io.pivotal.pal.tracker.allocations.data; + +import java.time.LocalDate; + +public class AllocationRecord { + + public final long id; + public final long projectId; + public final long userId; + public final LocalDate firstDay; + public final LocalDate lastDay; + + public AllocationRecord(Builder builder) { + id = builder.id; + projectId = builder.projectId; + userId = builder.userId; + firstDay = builder.firstDay; + lastDay = builder.lastDay; + } + + public static Builder allocationRecordBuilder() { + return new Builder(); + } + + public static class Builder { + private long id; + private long projectId; + private long userId; + private LocalDate firstDay; + private LocalDate lastDay; + + public AllocationRecord build() { + return new AllocationRecord(this); + } + + public Builder id(long id) { + this.id = id; + return this; + } + + public Builder projectId(long projectId) { + this.projectId = projectId; + return this; + } + + public Builder userId(long userId) { + this.userId = userId; + return this; + } + + public Builder firstDay(LocalDate firstDay) { + this.firstDay = firstDay; + return this; + } + + public Builder lastDay(LocalDate lastDay) { + this.lastDay = lastDay; + return this; + } + } + + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + AllocationRecord that = (AllocationRecord) o; + + if (id != that.id) return false; + if (projectId != that.projectId) return false; + if (userId != that.userId) return false; + if (firstDay != null ? !firstDay.equals(that.firstDay) : that.firstDay != null) + return false; + return lastDay != null ? lastDay.equals(that.lastDay) : that.lastDay == null; + } + + @Override + public int hashCode() { + int result = (int) (id ^ (id >>> 32)); + result = 31 * result + (int) (projectId ^ (projectId >>> 32)); + result = 31 * result + (int) (userId ^ (userId >>> 32)); + result = 31 * result + (firstDay != null ? firstDay.hashCode() : 0); + result = 31 * result + (lastDay != null ? lastDay.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "AllocationRecord{" + + "id=" + id + + ", projectId=" + projectId + + ", userId=" + userId + + ", firstDay=" + firstDay + + ", lastDay=" + lastDay + + '}'; + } +} diff --git a/components/allocations/src/test/java/test/pivotal/pal/tracker/allocations/AllocationControllerTest.java b/components/allocations/src/test/java/test/pivotal/pal/tracker/allocations/AllocationControllerTest.java new file mode 100644 index 0000000..4618004 --- /dev/null +++ b/components/allocations/src/test/java/test/pivotal/pal/tracker/allocations/AllocationControllerTest.java @@ -0,0 +1,93 @@ +package test.pivotal.pal.tracker.allocations; + +import io.pivotal.pal.tracker.allocations.*; +import io.pivotal.pal.tracker.allocations.data.AllocationDataGateway; +import io.pivotal.pal.tracker.allocations.data.AllocationRecord; +import org.junit.Test; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +import java.time.LocalDate; +import java.util.List; + +import static java.util.Arrays.asList; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.*; +import static test.pivotal.pal.tracker.allocations.TestBuilders.*; + + +public class AllocationControllerTest { + + private AllocationDataGateway allocationDataGateway = mock(AllocationDataGateway.class); + private ProjectClient client = mock(ProjectClient.class); + private AllocationController allocationsController = new AllocationController(allocationDataGateway, client); + + + @Test + public void testCreate() { + AllocationRecord record = testAllocationRecordBuilder() + .id(20L) + .projectId(31L) + .firstDay(LocalDate.parse("2016-02-20")) + .build(); + doReturn(record).when(allocationDataGateway).create(any()); + doReturn(new ProjectInfo(true)).when(client).getProject(anyLong()); + + + AllocationForm form = testAllocationFormBuilder() + .projectId(31L) + .firstDay("2016-02-20") + .build(); + ResponseEntity response = allocationsController.create(form); + + + verify(allocationDataGateway).create(testAllocationFieldsBuilder() + .projectId(31L) + .firstDay(LocalDate.parse("2016-02-20")) + .build() + ); + verify(client).getProject(31L); + + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CREATED); + assertThat(response.getBody()).isEqualTo(testAllocationInfoBuilder() + .id(20L) + .projectId(31L) + .firstDay("2016-02-20") + .build() + ); + } + + @Test + public void testCreate_WhenProjectIsNotActive() { + doReturn(new ProjectInfo(false)).when(client).getProject(anyLong()); + + AllocationForm form = testAllocationFormBuilder().build(); + + + ResponseEntity response = allocationsController.create(form); + + + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.SERVICE_UNAVAILABLE); + } + + @Test + public void testList() { + List records = asList( + testAllocationRecordBuilder().id(12L).build(), + testAllocationRecordBuilder().id(13L).build() + ); + doReturn(records).when(allocationDataGateway).findAllByProjectId(anyLong()); + + + List result = allocationsController.list(13); + + + verify(allocationDataGateway).findAllByProjectId(13L); + assertThat(result).containsExactlyInAnyOrder( + testAllocationInfoBuilder().id(12L).build(), + testAllocationInfoBuilder().id(13L).build() + ); + } +} diff --git a/components/allocations/src/test/java/test/pivotal/pal/tracker/allocations/AllocationDataGatewayTest.java b/components/allocations/src/test/java/test/pivotal/pal/tracker/allocations/AllocationDataGatewayTest.java new file mode 100644 index 0000000..8c7e5ef --- /dev/null +++ b/components/allocations/src/test/java/test/pivotal/pal/tracker/allocations/AllocationDataGatewayTest.java @@ -0,0 +1,74 @@ +package test.pivotal.pal.tracker.allocations; + +import io.pivotal.pal.tracker.allocations.data.AllocationDataGateway; +import io.pivotal.pal.tracker.allocations.data.AllocationFields; +import io.pivotal.pal.tracker.allocations.data.AllocationRecord; +import io.pivotal.pal.tracker.testsupport.TestScenarioSupport; +import org.junit.Before; +import org.junit.Test; +import org.springframework.jdbc.core.JdbcTemplate; + +import java.sql.Timestamp; +import java.time.LocalDate; +import java.util.List; +import java.util.Map; + +import static io.pivotal.pal.tracker.allocations.data.AllocationFields.allocationFieldsBuilder; +import static io.pivotal.pal.tracker.allocations.data.AllocationRecord.allocationRecordBuilder; +import static org.assertj.core.api.Assertions.assertThat; + +public class AllocationDataGatewayTest { + + private TestScenarioSupport testScenarioSupport = new TestScenarioSupport("tracker_allocations_test"); + private JdbcTemplate template = testScenarioSupport.template; + private AllocationDataGateway gateway = new AllocationDataGateway(testScenarioSupport.dataSource); + + @Before + public void setup() { + template.execute("delete from allocations;"); + } + + @Test + public void testCreate() { + AllocationFields fields = allocationFieldsBuilder() + .projectId(22L) + .userId(12L) + .firstDay(LocalDate.parse("2016-01-13")) + .lastDay(LocalDate.parse("2016-09-17")) + .build(); + + + AllocationRecord created = gateway.create(fields); + + + assertThat(created.id).isNotNull(); + assertThat(created.projectId).isEqualTo(22L); + assertThat(created.userId).isEqualTo(12L); + assertThat(created.firstDay).isEqualTo(LocalDate.parse("2016-01-13")); + assertThat(created.lastDay).isEqualTo(LocalDate.parse("2016-09-17")); + + Map persisted = template.queryForMap("select * from allocations WHERE id = ?", created.id); + + assertThat(persisted.get("project_id")).isEqualTo(22L); + assertThat(persisted.get("user_id")).isEqualTo(12L); + assertThat(persisted.get("first_day")).isEqualTo(Timestamp.valueOf("2016-01-13 00:00:00")); + assertThat(persisted.get("last_day")).isEqualTo(Timestamp.valueOf("2016-09-17 00:00:00")); + } + + @Test + public void testFindAllByProjectId() { + template.execute("insert into allocations (id, project_id, user_id, first_day, last_day) values (97336, 22, 12, '2016-01-13', '2016-09-17')"); + + + List result = gateway.findAllByProjectId(22L); + + + assertThat(result).containsExactly(allocationRecordBuilder() + .id(97336L) + .projectId(22L) + .userId(12L) + .firstDay(LocalDate.parse("2016-01-13")) + .lastDay(LocalDate.parse("2016-09-17")) + .build()); + } +} diff --git a/components/allocations/src/test/java/test/pivotal/pal/tracker/allocations/TestBuilders.java b/components/allocations/src/test/java/test/pivotal/pal/tracker/allocations/TestBuilders.java new file mode 100644 index 0000000..0caedc7 --- /dev/null +++ b/components/allocations/src/test/java/test/pivotal/pal/tracker/allocations/TestBuilders.java @@ -0,0 +1,51 @@ +package test.pivotal.pal.tracker.allocations; + +import io.pivotal.pal.tracker.allocations.AllocationForm; +import io.pivotal.pal.tracker.allocations.AllocationInfo; +import io.pivotal.pal.tracker.allocations.data.AllocationFields; +import io.pivotal.pal.tracker.allocations.data.AllocationRecord; + +import java.time.LocalDate; + +import static io.pivotal.pal.tracker.allocations.AllocationForm.allocationFormBuilder; +import static io.pivotal.pal.tracker.allocations.AllocationInfo.allocationInfoBuilder; +import static io.pivotal.pal.tracker.allocations.data.AllocationFields.allocationFieldsBuilder; +import static io.pivotal.pal.tracker.allocations.data.AllocationRecord.allocationRecordBuilder; + +public class TestBuilders { + + public static AllocationRecord.Builder testAllocationRecordBuilder() { + return allocationRecordBuilder() + .id(12L) + .projectId(13L) + .userId(14L) + .firstDay(LocalDate.parse("2016-02-22")) + .lastDay(LocalDate.parse("2017-02-23")); + } + + public static AllocationFields.Builder testAllocationFieldsBuilder() { + return allocationFieldsBuilder() + .projectId(13L) + .userId(14L) + .firstDay(LocalDate.parse("2016-02-22")) + .lastDay(LocalDate.parse("2017-02-23")); + } + + public static AllocationForm.Builder testAllocationFormBuilder() { + return allocationFormBuilder() + .projectId(13L) + .userId(14L) + .firstDay("2016-02-22") + .lastDay("2017-02-23"); + } + + public static AllocationInfo.Builder testAllocationInfoBuilder() { + return allocationInfoBuilder() + .id(12L) + .projectId(13L) + .userId(14L) + .firstDay("2016-02-22") + .lastDay("2017-02-23") + .info("allocation info"); + } +} diff --git a/components/backlog/build.gradle b/components/backlog/build.gradle new file mode 100644 index 0000000..4526bf7 --- /dev/null +++ b/components/backlog/build.gradle @@ -0,0 +1,6 @@ +dependencies { + compile project(":components:rest-support") + compile "org.springframework:spring-jdbc:$springVersion" + + testCompile project(":components:test-support") +} diff --git a/components/backlog/src/main/java/io/pivotal/pal/tracker/backlog/ProjectClient.java b/components/backlog/src/main/java/io/pivotal/pal/tracker/backlog/ProjectClient.java new file mode 100644 index 0000000..3cdd67d --- /dev/null +++ b/components/backlog/src/main/java/io/pivotal/pal/tracker/backlog/ProjectClient.java @@ -0,0 +1,18 @@ +package io.pivotal.pal.tracker.backlog; + +import org.springframework.web.client.RestOperations; + +public class ProjectClient { + + private final RestOperations restOperations; + private final String endpoint; + + public ProjectClient(RestOperations restOperations, String registrationServerEndpoint) { + this.restOperations = restOperations; + this.endpoint = registrationServerEndpoint; + } + + public ProjectInfo getProject(long projectId) { + return restOperations.getForObject(endpoint + "/projects/" + projectId, ProjectInfo.class); + } +} diff --git a/components/backlog/src/main/java/io/pivotal/pal/tracker/backlog/ProjectInfo.java b/components/backlog/src/main/java/io/pivotal/pal/tracker/backlog/ProjectInfo.java new file mode 100644 index 0000000..9f40610 --- /dev/null +++ b/components/backlog/src/main/java/io/pivotal/pal/tracker/backlog/ProjectInfo.java @@ -0,0 +1,37 @@ +package io.pivotal.pal.tracker.backlog; + +public class ProjectInfo { + + public final boolean active; + + private ProjectInfo() { + this(false); + } + + public ProjectInfo(boolean active) { + this.active = active; + } + + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + ProjectInfo that = (ProjectInfo) o; + + return active == that.active; + } + + @Override + public int hashCode() { + return (active ? 1 : 0); + } + + @Override + public String toString() { + return "ProjectInfo{" + + "active=" + active + + '}'; + } +} diff --git a/components/backlog/src/main/java/io/pivotal/pal/tracker/backlog/StoryController.java b/components/backlog/src/main/java/io/pivotal/pal/tracker/backlog/StoryController.java new file mode 100644 index 0000000..fc5c11f --- /dev/null +++ b/components/backlog/src/main/java/io/pivotal/pal/tracker/backlog/StoryController.java @@ -0,0 +1,66 @@ +package io.pivotal.pal.tracker.backlog; + +import io.pivotal.pal.tracker.backlog.data.StoryDataGateway; +import io.pivotal.pal.tracker.backlog.data.StoryFields; +import io.pivotal.pal.tracker.backlog.data.StoryRecord; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +import static io.pivotal.pal.tracker.backlog.StoryInfo.storyInfoBuilder; +import static io.pivotal.pal.tracker.backlog.data.StoryFields.storyFieldsBuilder; +import static java.util.stream.Collectors.toList; + +@RestController +@RequestMapping("/stories") +public class StoryController { + private final StoryDataGateway gateway; + private final ProjectClient client; + + public StoryController(StoryDataGateway gateway, ProjectClient client) { + this.gateway = gateway; + this.client = client; + } + + + @PostMapping + public ResponseEntity create(@RequestBody StoryForm form) { + if (projectIsActive(form.projectId)) { + StoryRecord record = gateway.create(mapToFields(form)); + return new ResponseEntity<>(present(record), HttpStatus.CREATED); + } + + return new ResponseEntity<>(HttpStatus.SERVICE_UNAVAILABLE); + } + + @GetMapping + public List list(@RequestParam long projectId) { + return gateway.findAllByProjectId(projectId).stream() + .map(this::present) + .collect(toList()); + } + + + private boolean projectIsActive(long projectId) { + ProjectInfo project = client.getProject(projectId); + return project != null && project.active; + } + + private StoryFields mapToFields(StoryForm form) { + return storyFieldsBuilder() + .projectId(form.projectId) + .name(form.name) + .build(); + } + + private StoryInfo present(StoryRecord record) { + return storyInfoBuilder() + .id(record.id) + .projectId(record.projectId) + .name(record.name) + .info("story info") + .build(); + } +} diff --git a/components/backlog/src/main/java/io/pivotal/pal/tracker/backlog/StoryForm.java b/components/backlog/src/main/java/io/pivotal/pal/tracker/backlog/StoryForm.java new file mode 100644 index 0000000..4c26ab2 --- /dev/null +++ b/components/backlog/src/main/java/io/pivotal/pal/tracker/backlog/StoryForm.java @@ -0,0 +1,66 @@ +package io.pivotal.pal.tracker.backlog; + +public class StoryForm { + + public final long projectId; + public final String name; + + private StoryForm() { + this(storyFormBuilder()); + } + + private StoryForm(Builder builder) { + projectId = builder.projectId; + name = builder.name; + } + + public static Builder storyFormBuilder() { + return new Builder(); + } + + + public static class Builder { + private long projectId; + private String name; + + public StoryForm build() { + return new StoryForm(this); + } + + public Builder projectId(long projectId) { + this.projectId = projectId; + return this; + } + + public Builder name(String name) { + this.name = name; + return this; + } + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + StoryForm storyForm = (StoryForm) o; + + if (projectId != storyForm.projectId) return false; + return name != null ? name.equals(storyForm.name) : storyForm.name == null; + } + + @Override + public int hashCode() { + int result = (int) (projectId ^ (projectId >>> 32)); + result = 31 * result + (name != null ? name.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "StoryForm{" + + "projectId=" + projectId + + ", name='" + name + '\'' + + '}'; + } +} diff --git a/components/backlog/src/main/java/io/pivotal/pal/tracker/backlog/StoryInfo.java b/components/backlog/src/main/java/io/pivotal/pal/tracker/backlog/StoryInfo.java new file mode 100644 index 0000000..b5bbb19 --- /dev/null +++ b/components/backlog/src/main/java/io/pivotal/pal/tracker/backlog/StoryInfo.java @@ -0,0 +1,89 @@ +package io.pivotal.pal.tracker.backlog; + +public class StoryInfo { + + public final long id; + public final long projectId; + public final String name; + public final String info; + + private StoryInfo() { + this(storyInfoBuilder()); + } + + private StoryInfo(Builder builder) { + id = builder.id; + projectId = builder.projectId; + name = builder.name; + info = builder.info; + } + + public static Builder storyInfoBuilder() { + return new Builder(); + } + + + public static class Builder { + private long id; + private long projectId; + private String name; + private String info; + + public StoryInfo build() { + return new StoryInfo(this); + } + + public Builder id(long id) { + this.id = id; + return this; + } + + public Builder projectId(long projectId) { + this.projectId = projectId; + return this; + } + + public Builder name(String name) { + this.name = name; + return this; + } + + public Builder info(String info) { + this.info = info; + return this; + } + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + StoryInfo storyInfo = (StoryInfo) o; + + if (id != storyInfo.id) return false; + if (projectId != storyInfo.projectId) return false; + if (name != null ? !name.equals(storyInfo.name) : storyInfo.name != null) + return false; + return info != null ? info.equals(storyInfo.info) : storyInfo.info == null; + } + + @Override + public int hashCode() { + int result = (int) (id ^ (id >>> 32)); + result = 31 * result + (int) (projectId ^ (projectId >>> 32)); + result = 31 * result + (name != null ? name.hashCode() : 0); + result = 31 * result + (info != null ? info.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "StoryInfo{" + + "id=" + id + + ", projectId=" + projectId + + ", name='" + name + '\'' + + ", info='" + info + '\'' + + '}'; + } +} diff --git a/components/backlog/src/main/java/io/pivotal/pal/tracker/backlog/data/StoryDataGateway.java b/components/backlog/src/main/java/io/pivotal/pal/tracker/backlog/data/StoryDataGateway.java new file mode 100644 index 0000000..5c28edb --- /dev/null +++ b/components/backlog/src/main/java/io/pivotal/pal/tracker/backlog/data/StoryDataGateway.java @@ -0,0 +1,61 @@ +package io.pivotal.pal.tracker.backlog.data; + +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.jdbc.support.GeneratedKeyHolder; +import org.springframework.jdbc.support.KeyHolder; +import org.springframework.stereotype.Repository; + +import javax.sql.DataSource; +import java.sql.PreparedStatement; +import java.util.List; + +import static io.pivotal.pal.tracker.backlog.data.StoryRecord.storyRecordBuilder; +import static java.sql.Statement.RETURN_GENERATED_KEYS; + +@Repository +public class StoryDataGateway { + private final JdbcTemplate jdbcTemplate; + + public StoryDataGateway(DataSource dataSource) { + this.jdbcTemplate = new JdbcTemplate(dataSource); + } + + public StoryRecord create(StoryFields fields) { + KeyHolder keyHolder = new GeneratedKeyHolder(); + + jdbcTemplate.update(connection -> { + PreparedStatement ps = connection.prepareStatement( + "insert into stories (project_id, name) values (?, ?)", RETURN_GENERATED_KEYS + ); + + ps.setLong(1, fields.projectId); + ps.setString(2, fields.name); + return ps; + }, keyHolder); + + return find(keyHolder.getKey().longValue()); + } + + public List findAllByProjectId(Long projectId) { + return jdbcTemplate.query( + "select id, project_id, name from stories where project_id = ?", + rowMapper, projectId + ); + } + + + private StoryRecord find(long id) { + return jdbcTemplate.queryForObject( + "select id, project_id, name from stories where id = ?", + rowMapper, id + ); + } + + private RowMapper rowMapper + = (rs, num) -> storyRecordBuilder() + .id(rs.getLong("id")) + .projectId(rs.getLong("project_id")) + .name(rs.getString("name")) + .build(); +} diff --git a/components/backlog/src/main/java/io/pivotal/pal/tracker/backlog/data/StoryFields.java b/components/backlog/src/main/java/io/pivotal/pal/tracker/backlog/data/StoryFields.java new file mode 100644 index 0000000..ce36232 --- /dev/null +++ b/components/backlog/src/main/java/io/pivotal/pal/tracker/backlog/data/StoryFields.java @@ -0,0 +1,61 @@ +package io.pivotal.pal.tracker.backlog.data; + +public class StoryFields { + + public final long projectId; + public final String name; + + private StoryFields(Builder builder) { + projectId = builder.projectId; + name = builder.name; + } + + public static Builder storyFieldsBuilder() { + return new Builder(); + } + + public static class Builder { + private long projectId; + private String name; + + public StoryFields build() { + return new StoryFields(this); + } + + public Builder projectId(long projectId) { + this.projectId = projectId; + return this; + } + + public Builder name(String name) { + this.name = name; + return this; + } + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + StoryFields that = (StoryFields) o; + + if (projectId != that.projectId) return false; + return name != null ? name.equals(that.name) : that.name == null; + } + + @Override + public int hashCode() { + int result = (int) (projectId ^ (projectId >>> 32)); + result = 31 * result + (name != null ? name.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "StoryFields{" + + "projectId=" + projectId + + ", name='" + name + '\'' + + '}'; + } +} diff --git a/components/backlog/src/main/java/io/pivotal/pal/tracker/backlog/data/StoryRecord.java b/components/backlog/src/main/java/io/pivotal/pal/tracker/backlog/data/StoryRecord.java new file mode 100644 index 0000000..ad98d9a --- /dev/null +++ b/components/backlog/src/main/java/io/pivotal/pal/tracker/backlog/data/StoryRecord.java @@ -0,0 +1,72 @@ +package io.pivotal.pal.tracker.backlog.data; + +public class StoryRecord { + + public final long id; + public final long projectId; + public final String name; + + private StoryRecord(Builder builder) { + id = builder.id; + projectId = builder.projectId; + name = builder.name; + } + + public static Builder storyRecordBuilder() { + return new Builder(); + } + + public static class Builder { + private long id; + private long projectId; + private String name; + + public StoryRecord build() { + return new StoryRecord(this); + } + + public Builder id(long id) { + this.id = id; + return this; + } + + public Builder projectId(long projectId) { + this.projectId = projectId; + return this; + } + + public Builder name(String name) { + this.name = name; + return this; + } + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + StoryRecord that = (StoryRecord) o; + + if (id != that.id) return false; + if (projectId != that.projectId) return false; + return name != null ? name.equals(that.name) : that.name == null; + } + + @Override + public int hashCode() { + int result = (int) (id ^ (id >>> 32)); + result = 31 * result + (int) (projectId ^ (projectId >>> 32)); + result = 31 * result + (name != null ? name.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "StoryRecord{" + + "id=" + id + + ", projectId=" + projectId + + ", name='" + name + '\'' + + '}'; + } +} diff --git a/components/backlog/src/test/java/test/pivotal/pal/tracker/backlog/StoryControllerTest.java b/components/backlog/src/test/java/test/pivotal/pal/tracker/backlog/StoryControllerTest.java new file mode 100644 index 0000000..2405438 --- /dev/null +++ b/components/backlog/src/test/java/test/pivotal/pal/tracker/backlog/StoryControllerTest.java @@ -0,0 +1,98 @@ +package test.pivotal.pal.tracker.backlog; + +import io.pivotal.pal.tracker.backlog.*; +import io.pivotal.pal.tracker.backlog.data.StoryDataGateway; +import io.pivotal.pal.tracker.backlog.data.StoryRecord; +import org.junit.Test; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +import java.util.List; + +import static io.pivotal.pal.tracker.backlog.StoryForm.storyFormBuilder; +import static io.pivotal.pal.tracker.backlog.StoryInfo.storyInfoBuilder; +import static io.pivotal.pal.tracker.backlog.data.StoryFields.storyFieldsBuilder; +import static io.pivotal.pal.tracker.backlog.data.StoryRecord.storyRecordBuilder; +import static java.util.Arrays.asList; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.*; +import static test.pivotal.pal.tracker.backlog.TestBuilders.*; + +public class StoryControllerTest { + + private StoryDataGateway storyDataGateway = mock(StoryDataGateway.class); + private ProjectClient client = mock(ProjectClient.class); + private StoryController storyController = new StoryController(storyDataGateway, client); + + @Test + public void testCreate() { + StoryRecord record = storyRecordBuilder() + .id(4L) + .projectId(3L) + .name("Something Fun") + .build(); + + doReturn(record).when(storyDataGateway).create( + storyFieldsBuilder().projectId(3L).name("Something Fun").build() + ); + + doReturn(new ProjectInfo(true)).when(client).getProject(anyLong()); + + StoryForm form = storyFormBuilder() + .projectId(3L) + .name("Something Fun") + .build(); + + + ResponseEntity response = storyController.create(form); + + + verify(client).getProject(3L); + + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CREATED); + assertThat(response.getBody()).isEqualTo(storyInfoBuilder() + .id(4L) + .projectId(3L) + .name("Something Fun") + .info("story info") + .build() + ); + } + + @Test + public void testFailedCreate() { + doReturn(new ProjectInfo(false)).when(client).getProject(anyLong()); + + StoryForm form = testStoryFormBuilder() + .projectId(3L) + .build(); + + + ResponseEntity response = storyController.create(form); + + + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.SERVICE_UNAVAILABLE); + } + + @Test + public void testList() { + List records = asList( + testStoryRecordBuilder().id(12L).build(), + testStoryRecordBuilder().id(13L).build() + ); + + doReturn(records).when(storyDataGateway).findAllByProjectId(anyLong()); + + + List result = storyController.list(13); + + + verify(storyDataGateway).findAllByProjectId(13L); + + assertThat(result).containsExactlyInAnyOrder( + testStoryInfoBuilder().id(12L).build(), + testStoryInfoBuilder().id(13L).build() + ); + } +} diff --git a/components/backlog/src/test/java/test/pivotal/pal/tracker/backlog/TestBuilders.java b/components/backlog/src/test/java/test/pivotal/pal/tracker/backlog/TestBuilders.java new file mode 100644 index 0000000..0e786d4 --- /dev/null +++ b/components/backlog/src/test/java/test/pivotal/pal/tracker/backlog/TestBuilders.java @@ -0,0 +1,41 @@ +package test.pivotal.pal.tracker.backlog; + +import io.pivotal.pal.tracker.backlog.StoryForm; +import io.pivotal.pal.tracker.backlog.StoryInfo; +import io.pivotal.pal.tracker.backlog.data.StoryFields; +import io.pivotal.pal.tracker.backlog.data.StoryRecord; + +import static io.pivotal.pal.tracker.backlog.StoryForm.storyFormBuilder; +import static io.pivotal.pal.tracker.backlog.StoryInfo.storyInfoBuilder; +import static io.pivotal.pal.tracker.backlog.data.StoryFields.storyFieldsBuilder; +import static io.pivotal.pal.tracker.backlog.data.StoryRecord.storyRecordBuilder; + +public class TestBuilders { + + public static StoryRecord.Builder testStoryRecordBuilder() { + return storyRecordBuilder() + .id(4L) + .projectId(3L) + .name("Something Fun"); + } + + public static StoryFields.Builder testStoryFieldsBuilder() { + return storyFieldsBuilder() + .projectId(3L) + .name("Something Fun"); + } + + public static StoryInfo.Builder testStoryInfoBuilder() { + return storyInfoBuilder() + .id(4L) + .projectId(3L) + .name("Something Fun") + .info("story info"); + } + + public static StoryForm.Builder testStoryFormBuilder() { + return storyFormBuilder() + .projectId(3L) + .name("Something Fun"); + } +} diff --git a/components/backlog/src/test/java/test/pivotal/pal/tracker/backlog/data/StoryDataGatewayTest.java b/components/backlog/src/test/java/test/pivotal/pal/tracker/backlog/data/StoryDataGatewayTest.java new file mode 100644 index 0000000..e6d705b --- /dev/null +++ b/components/backlog/src/test/java/test/pivotal/pal/tracker/backlog/data/StoryDataGatewayTest.java @@ -0,0 +1,66 @@ +package test.pivotal.pal.tracker.backlog.data; + +import io.pivotal.pal.tracker.backlog.data.StoryDataGateway; +import io.pivotal.pal.tracker.backlog.data.StoryFields; +import io.pivotal.pal.tracker.backlog.data.StoryRecord; +import io.pivotal.pal.tracker.testsupport.TestScenarioSupport; +import org.junit.Before; +import org.junit.Test; +import org.springframework.jdbc.core.JdbcTemplate; + +import java.util.List; +import java.util.Map; + +import static io.pivotal.pal.tracker.backlog.data.StoryFields.storyFieldsBuilder; +import static io.pivotal.pal.tracker.backlog.data.StoryRecord.storyRecordBuilder; +import static org.assertj.core.api.Assertions.assertThat; + +public class StoryDataGatewayTest { + + private TestScenarioSupport testScenarioSupport = new TestScenarioSupport("tracker_backlog_test"); + private JdbcTemplate template = testScenarioSupport.template; + private StoryDataGateway gateway = new StoryDataGateway(testScenarioSupport.dataSource); + + @Before + public void setUp() throws Exception { + template.execute("DELETE FROM stories;"); + } + + @Test + public void testCreate() { + StoryFields fields = storyFieldsBuilder() + .projectId(22L) + .name("aStory") + .build(); + + + StoryRecord created = gateway.create(fields); + + + assertThat(created.id).isNotNull(); + assertThat(created.name).isEqualTo("aStory"); + assertThat(created.projectId).isEqualTo(22L); + + Map persisted = template.queryForMap("select * from stories where id = ?", created.id); + + assertThat(persisted.get("project_id")).isEqualTo(22L); + assertThat(persisted.get("name")).isEqualTo("aStory"); + } + + @Test + public void testFindBy() { + template.execute("insert into stories (id, project_id, name) values (1346, 22, 'aStory')"); + + + List result = gateway.findAllByProjectId(22L); + + + assertThat(result).containsExactly( + storyRecordBuilder() + .id(1346L) + .projectId(22L) + .name("aStory") + .build() + ); + } +} diff --git a/components/build.gradle b/components/build.gradle new file mode 100644 index 0000000..06f3dca --- /dev/null +++ b/components/build.gradle @@ -0,0 +1,2 @@ +subprojects { +} \ No newline at end of file diff --git a/components/projects/build.gradle b/components/projects/build.gradle new file mode 100644 index 0000000..4526bf7 --- /dev/null +++ b/components/projects/build.gradle @@ -0,0 +1,6 @@ +dependencies { + compile project(":components:rest-support") + compile "org.springframework:spring-jdbc:$springVersion" + + testCompile project(":components:test-support") +} diff --git a/components/projects/src/main/java/io/pivotal/pal/tracker/projects/ProjectController.java b/components/projects/src/main/java/io/pivotal/pal/tracker/projects/ProjectController.java new file mode 100644 index 0000000..03855ab --- /dev/null +++ b/components/projects/src/main/java/io/pivotal/pal/tracker/projects/ProjectController.java @@ -0,0 +1,69 @@ +package io.pivotal.pal.tracker.projects; + +import io.pivotal.pal.tracker.projects.data.ProjectDataGateway; +import io.pivotal.pal.tracker.projects.data.ProjectFields; +import io.pivotal.pal.tracker.projects.data.ProjectRecord; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +import static io.pivotal.pal.tracker.projects.ProjectInfo.projectInfoBuilder; +import static io.pivotal.pal.tracker.projects.data.ProjectFields.projectFieldsBuilder; +import static java.util.stream.Collectors.toList; + +@RestController +@RequestMapping("/projects") +public class ProjectController { + + private final ProjectDataGateway gateway; + + public ProjectController(ProjectDataGateway gateway) { + this.gateway = gateway; + } + + @PostMapping + public ResponseEntity create(@RequestBody ProjectForm form) { + ProjectRecord record = gateway.create(formToFields(form)); + return new ResponseEntity<>(present(record), HttpStatus.CREATED); + } + + @GetMapping + public List list(@RequestParam long accountId) { + return gateway.findAllByAccountId(accountId) + .stream() + .map(this::present) + .collect(toList()); + } + + @GetMapping("/{projectId}") + public ProjectInfo get(@PathVariable long projectId) { + ProjectRecord record = gateway.find(projectId); + + if (record != null) { + return present(record); + } + + return null; + } + + + private ProjectFields formToFields(ProjectForm form) { + return projectFieldsBuilder() + .accountId(form.accountId) + .name(form.name) + .active(form.active) + .build(); + } + + private ProjectInfo present(ProjectRecord record) { + return projectInfoBuilder() + .id(record.id) + .accountId(record.accountId) + .name(record.name) + .active(record.active) + .info("project info") + .build(); + } +} diff --git a/components/projects/src/main/java/io/pivotal/pal/tracker/projects/ProjectForm.java b/components/projects/src/main/java/io/pivotal/pal/tracker/projects/ProjectForm.java new file mode 100644 index 0000000..a29ef8a --- /dev/null +++ b/components/projects/src/main/java/io/pivotal/pal/tracker/projects/ProjectForm.java @@ -0,0 +1,76 @@ +package io.pivotal.pal.tracker.projects; + +public class ProjectForm { + + public final long accountId; + public final String name; + public final boolean active; + + private ProjectForm() { + this(projectFormBuilder()); + } + + public ProjectForm(Builder builder) { + accountId = builder.accountId; + name = builder.name; + active = builder.active; + } + + public static Builder projectFormBuilder() { + return new Builder(); + } + + public static class Builder { + private long accountId; + private String name; + private boolean active; + + public ProjectForm build() { + return new ProjectForm(this); + } + + public Builder accountId(long accountId) { + this.accountId = accountId; + return this; + } + + public Builder name(String name) { + this.name = name; + return this; + } + + public Builder active(boolean active) { + this.active = active; + return this; + } + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + ProjectForm that = (ProjectForm) o; + + if (accountId != that.accountId) return false; + if (active != that.active) return false; + return name != null ? name.equals(that.name) : that.name == null; + } + + @Override + public int hashCode() { + int result = (int) (accountId ^ (accountId >>> 32)); + result = 31 * result + (name != null ? name.hashCode() : 0); + result = 31 * result + (active ? 1 : 0); + return result; + } + + @Override + public String toString() { + return "ProjectForm{" + + "accountId=" + accountId + + ", name='" + name + '\'' + + ", active=" + active + + '}'; + } +} diff --git a/components/projects/src/main/java/io/pivotal/pal/tracker/projects/ProjectInfo.java b/components/projects/src/main/java/io/pivotal/pal/tracker/projects/ProjectInfo.java new file mode 100644 index 0000000..3819206 --- /dev/null +++ b/components/projects/src/main/java/io/pivotal/pal/tracker/projects/ProjectInfo.java @@ -0,0 +1,99 @@ +package io.pivotal.pal.tracker.projects; + +public class ProjectInfo { + + public final long id; + public final long accountId; + public final String name; + public final boolean active; + public final String info; + + private ProjectInfo() { + this(projectInfoBuilder()); + } + + public ProjectInfo(Builder builder) { + id = builder.id; + accountId = builder.accountId; + name = builder.name; + active = builder.active; + info = builder.info; + } + + public static Builder projectInfoBuilder() { + return new Builder(); + } + + public static class Builder { + private long id; + private long accountId; + private String name; + private boolean active; + private String info; + + public ProjectInfo build() { + return new ProjectInfo(this); + } + + public Builder id(long id) { + this.id = id; + return this; + } + + public Builder accountId(long accountId) { + this.accountId = accountId; + return this; + } + + public Builder name(String name) { + this.name = name; + return this; + } + + public Builder active(boolean active) { + this.active = active; + return this; + } + + public Builder info(String info) { + this.info = info; + return this; + } + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + ProjectInfo that = (ProjectInfo) o; + + if (id != that.id) return false; + if (accountId != that.accountId) return false; + if (active != that.active) return false; + if (name != null ? !name.equals(that.name) : that.name != null) + return false; + return info != null ? info.equals(that.info) : that.info == null; + } + + @Override + public int hashCode() { + int result = (int) (id ^ (id >>> 32)); + result = 31 * result + (int) (accountId ^ (accountId >>> 32)); + result = 31 * result + (name != null ? name.hashCode() : 0); + result = 31 * result + (active ? 1 : 0); + result = 31 * result + (info != null ? info.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "ProjectInfo{" + + "id=" + id + + ", accountId=" + accountId + + ", name='" + name + '\'' + + ", active=" + active + + ", info='" + info + '\'' + + '}'; + } +} diff --git a/components/projects/src/main/java/io/pivotal/pal/tracker/projects/data/ProjectDataGateway.java b/components/projects/src/main/java/io/pivotal/pal/tracker/projects/data/ProjectDataGateway.java new file mode 100644 index 0000000..af13ae2 --- /dev/null +++ b/components/projects/src/main/java/io/pivotal/pal/tracker/projects/data/ProjectDataGateway.java @@ -0,0 +1,69 @@ +package io.pivotal.pal.tracker.projects.data; + +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.jdbc.support.GeneratedKeyHolder; +import org.springframework.jdbc.support.KeyHolder; +import org.springframework.stereotype.Repository; + +import javax.sql.DataSource; +import java.sql.PreparedStatement; +import java.util.List; + +import static io.pivotal.pal.tracker.projects.data.ProjectRecord.projectRecordBuilder; +import static java.sql.Statement.RETURN_GENERATED_KEYS; + +@Repository +public class ProjectDataGateway { + + private final JdbcTemplate jdbcTemplate; + + public ProjectDataGateway(DataSource dataSource) { + this.jdbcTemplate = new JdbcTemplate(dataSource); + } + + + public ProjectRecord create(ProjectFields fields) { + KeyHolder keyholder = new GeneratedKeyHolder(); + + jdbcTemplate.update(connection -> { + PreparedStatement ps = connection.prepareStatement( + "insert into projects (account_id, name, active) values (?, ?, ?)", RETURN_GENERATED_KEYS); + ps.setLong(1, fields.accountId); + ps.setString(2, fields.name); + ps.setBoolean(3, true); + return ps; + }, keyholder); + + return find(keyholder.getKey().longValue()); + } + + public List findAllByAccountId(Long accountId) { + return jdbcTemplate.query( + "select id, account_id, name, active from projects where account_id = ? order by name asc", + rowMapper, accountId + ); + } + + public ProjectRecord find(long id) { + List list = jdbcTemplate.query( + "select id, account_id, name, active from projects where id = ? order by name asc", + rowMapper, id + ); + + if (list.isEmpty()) { + return null; + } + + return list.get(0); + } + + + private RowMapper rowMapper = + (rs, num) -> projectRecordBuilder() + .id(rs.getLong("id")) + .accountId(rs.getLong("account_id")) + .name(rs.getString("name")) + .active(rs.getBoolean("active")) + .build(); +} diff --git a/components/projects/src/main/java/io/pivotal/pal/tracker/projects/data/ProjectFields.java b/components/projects/src/main/java/io/pivotal/pal/tracker/projects/data/ProjectFields.java new file mode 100644 index 0000000..0be1274 --- /dev/null +++ b/components/projects/src/main/java/io/pivotal/pal/tracker/projects/data/ProjectFields.java @@ -0,0 +1,74 @@ +package io.pivotal.pal.tracker.projects.data; + +public class ProjectFields { + + public final long accountId; + public final String name; + public final boolean active; + + private ProjectFields(Builder builder) { + accountId = builder.accountId; + name = builder.name; + active = builder.active; + } + + public static Builder projectFieldsBuilder() { + return new Builder(); + } + + public static class Builder { + + private long accountId; + private String name; + private boolean active; + + public ProjectFields build() { + return new ProjectFields(this); + } + + public Builder accountId(long accountId) { + this.accountId = accountId; + return this; + } + + public Builder name(String name) { + this.name = name; + return this; + } + + public Builder active(boolean active) { + this.active = active; + return this; + } + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + ProjectFields that = (ProjectFields) o; + + if (accountId != that.accountId) return false; + if (active != that.active) return false; + return name != null ? name.equals(that.name) : that.name == null; + } + + @Override + public int hashCode() { + int result = (int) (accountId ^ (accountId >>> 32)); + result = 31 * result + (name != null ? name.hashCode() : 0); + result = 31 * result + (active ? 1 : 0); + return result; + } + + @Override + public String toString() { + return "ProjectFields{" + + "accountId=" + accountId + + ", name='" + name + '\'' + + ", active=" + active + + '}'; + } +} + diff --git a/components/projects/src/main/java/io/pivotal/pal/tracker/projects/data/ProjectRecord.java b/components/projects/src/main/java/io/pivotal/pal/tracker/projects/data/ProjectRecord.java new file mode 100644 index 0000000..14e3950 --- /dev/null +++ b/components/projects/src/main/java/io/pivotal/pal/tracker/projects/data/ProjectRecord.java @@ -0,0 +1,84 @@ +package io.pivotal.pal.tracker.projects.data; + +public class ProjectRecord { + + public final long id; + public final long accountId; + public final String name; + public final boolean active; + + private ProjectRecord(Builder builder) { + id = builder.id; + accountId = builder.accountId; + name = builder.name; + active = builder.active; + } + + public static Builder projectRecordBuilder() { + return new Builder(); + } + + public static class Builder { + private long id; + private long accountId; + private String name; + private boolean active; + + public ProjectRecord build() { + return new ProjectRecord(this); + } + + public Builder id(long id) { + this.id = id; + return this; + } + + public Builder accountId(long accountId) { + this.accountId = accountId; + return this; + } + + public Builder name(String name) { + this.name = name; + return this; + } + + public Builder active(boolean active) { + this.active = active; + return this; + } + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + ProjectRecord that = (ProjectRecord) o; + + if (id != that.id) return false; + if (accountId != that.accountId) return false; + if (active != that.active) return false; + return name != null ? name.equals(that.name) : that.name == null; + } + + @Override + public int hashCode() { + int result = (int) (id ^ (id >>> 32)); + result = 31 * result + (int) (accountId ^ (accountId >>> 32)); + result = 31 * result + (name != null ? name.hashCode() : 0); + result = 31 * result + (active ? 1 : 0); + return result; + } + + @Override + public String toString() { + return "ProjectRecord{" + + "id=" + id + + ", accountId=" + accountId + + ", name='" + name + '\'' + + ", active=" + active + + '}'; + } +} + diff --git a/components/projects/src/test/java/test/pivotal/pal/tracker/projects/ProjectControllerTest.java b/components/projects/src/test/java/test/pivotal/pal/tracker/projects/ProjectControllerTest.java new file mode 100644 index 0000000..a956f2e --- /dev/null +++ b/components/projects/src/test/java/test/pivotal/pal/tracker/projects/ProjectControllerTest.java @@ -0,0 +1,81 @@ +package test.pivotal.pal.tracker.projects; + +import io.pivotal.pal.tracker.projects.ProjectController; +import io.pivotal.pal.tracker.projects.ProjectInfo; +import io.pivotal.pal.tracker.projects.data.ProjectDataGateway; +import io.pivotal.pal.tracker.projects.data.ProjectRecord; +import org.junit.Test; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +import java.util.List; + +import static java.util.Arrays.asList; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.*; +import static test.pivotal.pal.tracker.projects.TestBuilders.*; + + +public class ProjectControllerTest { + + private ProjectDataGateway gateway = mock(ProjectDataGateway.class); + private ProjectController controller = new ProjectController(gateway); + + @Test + public void testCreate() { + ProjectRecord record = testProjectRecordBuilder().build(); + doReturn(record).when(gateway).create(any()); + + + ResponseEntity result = controller.create(testProjectFormBuilder().build()); + + + verify(gateway).create(testProjectFieldsBuilder().build()); + assertThat(result.getStatusCode()).isEqualTo(HttpStatus.CREATED); + assertThat(result.getBody()).isEqualTo(testProjectInfoBuilder().build()); + } + + @Test + public void testList() { + List records = asList( + testProjectRecordBuilder().id(12).build(), + testProjectRecordBuilder().id(13).build() + ); + doReturn(records).when(gateway).findAllByAccountId(anyLong()); + + + List result = controller.list(23); + + + verify(gateway).findAllByAccountId(23L); + assertThat(result).containsExactlyInAnyOrder( + testProjectInfoBuilder().id(12).build(), + testProjectInfoBuilder().id(13).build() + ); + } + + @Test + public void testGet() { + ProjectRecord record = testProjectRecordBuilder().id(99).build(); + doReturn(record).when(gateway).find(anyLong()); + + + ProjectInfo result = controller.get(99); + + + verify(gateway).find(99); + assertThat(result).isEqualTo(testProjectInfoBuilder().id(99).build()); + } + + @Test + public void testGet_WithNull() { + doReturn(null).when(gateway).find(anyLong()); + + ProjectInfo result = controller.get(88); + + verify(gateway).find(88); + assertThat(result).isNull(); + } +} diff --git a/components/projects/src/test/java/test/pivotal/pal/tracker/projects/ProjectDataGatewayTest.java b/components/projects/src/test/java/test/pivotal/pal/tracker/projects/ProjectDataGatewayTest.java new file mode 100644 index 0000000..a11e5d1 --- /dev/null +++ b/components/projects/src/test/java/test/pivotal/pal/tracker/projects/ProjectDataGatewayTest.java @@ -0,0 +1,79 @@ +package test.pivotal.pal.tracker.projects; + +import io.pivotal.pal.tracker.projects.data.ProjectDataGateway; +import io.pivotal.pal.tracker.projects.data.ProjectFields; +import io.pivotal.pal.tracker.projects.data.ProjectRecord; +import io.pivotal.pal.tracker.testsupport.TestScenarioSupport; +import org.junit.Before; +import org.junit.Test; +import org.springframework.jdbc.core.JdbcTemplate; + +import java.util.List; +import java.util.Map; + +import static io.pivotal.pal.tracker.projects.data.ProjectFields.projectFieldsBuilder; +import static io.pivotal.pal.tracker.projects.data.ProjectRecord.projectRecordBuilder; +import static org.assertj.core.api.Assertions.assertThat; + +public class ProjectDataGatewayTest { + + private TestScenarioSupport testScenarioSupport = new TestScenarioSupport("tracker_registration_test"); + private JdbcTemplate template = testScenarioSupport.template; + private ProjectDataGateway gateway = new ProjectDataGateway(testScenarioSupport.dataSource); + + @Before + public void setUp() throws Exception { + template.execute("DELETE FROM projects;"); + template.execute("DELETE FROM accounts;"); + template.execute("DELETE FROM users;"); + } + + @Test + public void testCreate() { + template.execute("insert into users (id, name) values (12, 'Jack')"); + template.execute("insert into accounts (id, owner_id, name) values (1, 12, 'anAccount')"); + + ProjectFields fields = projectFieldsBuilder().accountId(1).name("aProject").build(); + ProjectRecord created = gateway.create(fields); + + + assertThat(created.id).isNotNull(); + assertThat(created.name).isEqualTo("aProject"); + assertThat(created.accountId).isEqualTo(1L); + + Map persisted = template.queryForMap("SELECT * FROM projects WHERE id = ?", created.id); + + assertThat(persisted.get("name")).isEqualTo("aProject"); + assertThat(persisted.get("account_id")).isEqualTo(1L); + } + + @Test + public void testFindAllByAccountId() { + template.execute("insert into users (id, name) values (12, 'Jack')"); + template.execute("insert into accounts (id, owner_id, name) values (1, 12, 'anAccount')"); + template.execute("insert into projects (id, account_id, name) values (22, 1, 'aProject')"); + + + List result = gateway.findAllByAccountId(1L); + + + assertThat(result).containsExactlyInAnyOrder( + projectRecordBuilder().id(22L).accountId(1L).name("aProject").active(true).build() + ); + } + + @Test + public void testFind() { + template.execute("insert into users (id, name) values (12, 'Jack')"); + template.execute("insert into accounts (id, owner_id, name) values (1, 12, 'anAccount')"); + template.execute("insert into projects (id, account_id, name, active) values (22, 1, 'aProject', true)"); + + + ProjectRecord foundRecord = gateway.find(22L); + + + assertThat(foundRecord).isEqualTo( + projectRecordBuilder().id(22L).accountId(1L).name("aProject").active(true).build() + ); + } +} diff --git a/components/projects/src/test/java/test/pivotal/pal/tracker/projects/TestBuilders.java b/components/projects/src/test/java/test/pivotal/pal/tracker/projects/TestBuilders.java new file mode 100644 index 0000000..00dda82 --- /dev/null +++ b/components/projects/src/test/java/test/pivotal/pal/tracker/projects/TestBuilders.java @@ -0,0 +1,45 @@ +package test.pivotal.pal.tracker.projects; + +import io.pivotal.pal.tracker.projects.ProjectForm; +import io.pivotal.pal.tracker.projects.ProjectInfo; +import io.pivotal.pal.tracker.projects.data.ProjectFields; +import io.pivotal.pal.tracker.projects.data.ProjectRecord; + +import static io.pivotal.pal.tracker.projects.ProjectForm.projectFormBuilder; +import static io.pivotal.pal.tracker.projects.ProjectInfo.projectInfoBuilder; +import static io.pivotal.pal.tracker.projects.data.ProjectFields.projectFieldsBuilder; +import static io.pivotal.pal.tracker.projects.data.ProjectRecord.projectRecordBuilder; + +public class TestBuilders { + + public static ProjectRecord.Builder testProjectRecordBuilder() { + return projectRecordBuilder() + .id(9L) + .accountId(23L) + .name("MyInfo") + .active(true); + } + + public static ProjectInfo.Builder testProjectInfoBuilder() { + return projectInfoBuilder() + .id(9L) + .accountId(23L) + .name("MyInfo") + .active(true) + .info("project info"); + } + + public static ProjectFields.Builder testProjectFieldsBuilder() { + return projectFieldsBuilder() + .accountId(23L) + .name("MyInfo") + .active(true); + } + + public static ProjectForm.Builder testProjectFormBuilder() { + return projectFormBuilder() + .accountId(23L) + .name("MyInfo") + .active(true); + } +} diff --git a/components/rest-support/build.gradle b/components/rest-support/build.gradle new file mode 100644 index 0000000..6c827eb --- /dev/null +++ b/components/rest-support/build.gradle @@ -0,0 +1,4 @@ +dependencies { + compile "org.springframework:spring-web:$springVersion" + compile "org.springframework:spring-context:$springVersion" +} diff --git a/components/rest-support/src/main/java/io/pivotal/pal/tracker/restsupport/RestConfig.java b/components/rest-support/src/main/java/io/pivotal/pal/tracker/restsupport/RestConfig.java new file mode 100644 index 0000000..3120f4c --- /dev/null +++ b/components/rest-support/src/main/java/io/pivotal/pal/tracker/restsupport/RestConfig.java @@ -0,0 +1,25 @@ +package io.pivotal.pal.tracker.restsupport; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestOperations; +import org.springframework.web.client.RestTemplate; + + +@Configuration +public class RestConfig { + + @Bean + public RestOperations restOperations() { + return new RestTemplate(); + } + + @Bean + public ObjectMapper objectMapper() { + ObjectMapper mapper = new ObjectMapper(); + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + return mapper; + } +} diff --git a/components/rest-support/src/main/java/io/pivotal/pal/tracker/restsupport/SpringDefaultController.java b/components/rest-support/src/main/java/io/pivotal/pal/tracker/restsupport/SpringDefaultController.java new file mode 100644 index 0000000..cc6889b --- /dev/null +++ b/components/rest-support/src/main/java/io/pivotal/pal/tracker/restsupport/SpringDefaultController.java @@ -0,0 +1,13 @@ +package io.pivotal.pal.tracker.restsupport; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class SpringDefaultController { + + @GetMapping("/") + public String defaultRoute() { + return "Noop!"; + } +} diff --git a/components/test-support/build.gradle b/components/test-support/build.gradle new file mode 100644 index 0000000..156619b --- /dev/null +++ b/components/test-support/build.gradle @@ -0,0 +1,6 @@ +dependencies { + compile project(":components:rest-support") + compile "org.springframework:spring-jdbc:$springVersion" + compile "mysql:mysql-connector-java:$mysqlVersion" + compile "junit:junit:$junitVersion" +} diff --git a/components/test-support/src/main/java/io/pivotal/pal/tracker/testsupport/TestDataSourceFactory.java b/components/test-support/src/main/java/io/pivotal/pal/tracker/testsupport/TestDataSourceFactory.java new file mode 100644 index 0000000..cd99c55 --- /dev/null +++ b/components/test-support/src/main/java/io/pivotal/pal/tracker/testsupport/TestDataSourceFactory.java @@ -0,0 +1,18 @@ +package io.pivotal.pal.tracker.testsupport; + +import com.mysql.cj.jdbc.MysqlDataSource; + +import javax.sql.DataSource; + + +public class TestDataSourceFactory { + + public static DataSource create(String name) { + MysqlDataSource dataSource = new MysqlDataSource(); + + dataSource.setUrl("jdbc:mysql://localhost:3306/" + name + "?useSSL=false&useTimezone=true&serverTimezone=UTC&useLegacyDatetimeCode=false"); + dataSource.setUser("tracker"); + + return dataSource; + } +} diff --git a/components/test-support/src/main/java/io/pivotal/pal/tracker/testsupport/TestScenarioSupport.java b/components/test-support/src/main/java/io/pivotal/pal/tracker/testsupport/TestScenarioSupport.java new file mode 100644 index 0000000..fcb840f --- /dev/null +++ b/components/test-support/src/main/java/io/pivotal/pal/tracker/testsupport/TestScenarioSupport.java @@ -0,0 +1,33 @@ +package io.pivotal.pal.tracker.testsupport; + +import org.springframework.jdbc.core.JdbcTemplate; + +import javax.sql.DataSource; +import java.util.TimeZone; + +public class TestScenarioSupport { + + public final JdbcTemplate template; + public final DataSource dataSource; + + public TestScenarioSupport(String dbName) { + dataSource = TestDataSourceFactory.create(dbName); + template = new JdbcTemplate(dataSource); + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + } + + public static void clearAllDatabases() { + clearTables("tracker_allocations_test", "allocations"); + clearTables("tracker_backlog_test", "stories"); + clearTables("tracker_registration_test", "projects", "accounts", "users"); + clearTables("tracker_timesheets_test", "time_entries"); + } + + private static void clearTables(String dbName, String... tableNames) { + JdbcTemplate template = new JdbcTemplate(TestDataSourceFactory.create(dbName)); + + for (String tableName : tableNames) { + template.execute("delete from " + tableName); + } + } +} diff --git a/components/timesheets/build.gradle b/components/timesheets/build.gradle new file mode 100644 index 0000000..4526bf7 --- /dev/null +++ b/components/timesheets/build.gradle @@ -0,0 +1,6 @@ +dependencies { + compile project(":components:rest-support") + compile "org.springframework:spring-jdbc:$springVersion" + + testCompile project(":components:test-support") +} diff --git a/components/timesheets/src/main/java/io/pivotal/pal/tracker/timesheets/ProjectClient.java b/components/timesheets/src/main/java/io/pivotal/pal/tracker/timesheets/ProjectClient.java new file mode 100644 index 0000000..bdbdb5b --- /dev/null +++ b/components/timesheets/src/main/java/io/pivotal/pal/tracker/timesheets/ProjectClient.java @@ -0,0 +1,18 @@ +package io.pivotal.pal.tracker.timesheets; + +import org.springframework.web.client.RestOperations; + +public class ProjectClient { + + private final RestOperations restOperations; + private final String endpoint; + + public ProjectClient(RestOperations restOperations, String registrationServerEndpoint) { + this.restOperations = restOperations; + this.endpoint = registrationServerEndpoint; + } + + public ProjectInfo getProject(long projectId) { + return restOperations.getForObject(endpoint + "/projects/" + projectId, ProjectInfo.class); + } +} diff --git a/components/timesheets/src/main/java/io/pivotal/pal/tracker/timesheets/ProjectInfo.java b/components/timesheets/src/main/java/io/pivotal/pal/tracker/timesheets/ProjectInfo.java new file mode 100644 index 0000000..0c08b3f --- /dev/null +++ b/components/timesheets/src/main/java/io/pivotal/pal/tracker/timesheets/ProjectInfo.java @@ -0,0 +1,37 @@ +package io.pivotal.pal.tracker.timesheets; + +public class ProjectInfo { + + public final boolean active; + + private ProjectInfo() { + this(false); + } + + public ProjectInfo(boolean active) { + this.active = active; + } + + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + ProjectInfo that = (ProjectInfo) o; + + return active == that.active; + } + + @Override + public int hashCode() { + return (active ? 1 : 0); + } + + @Override + public String toString() { + return "ProjectInfo{" + + "active=" + active + + '}'; + } +} diff --git a/components/timesheets/src/main/java/io/pivotal/pal/tracker/timesheets/TimeEntryController.java b/components/timesheets/src/main/java/io/pivotal/pal/tracker/timesheets/TimeEntryController.java new file mode 100644 index 0000000..a69f6a0 --- /dev/null +++ b/components/timesheets/src/main/java/io/pivotal/pal/tracker/timesheets/TimeEntryController.java @@ -0,0 +1,71 @@ +package io.pivotal.pal.tracker.timesheets; + +import io.pivotal.pal.tracker.timesheets.data.TimeEntryDataGateway; +import io.pivotal.pal.tracker.timesheets.data.TimeEntryFields; +import io.pivotal.pal.tracker.timesheets.data.TimeEntryRecord; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.time.LocalDate; +import java.util.List; + +import static io.pivotal.pal.tracker.timesheets.TimeEntryInfo.timeEntryInfoBuilder; +import static io.pivotal.pal.tracker.timesheets.data.TimeEntryFields.timeEntryFieldsBuilder; +import static java.util.stream.Collectors.toList; + +@RestController +@RequestMapping("/time-entries") +public class TimeEntryController { + + private final TimeEntryDataGateway gateway; + private final ProjectClient client; + + public TimeEntryController(TimeEntryDataGateway gateway, ProjectClient client) { + this.gateway = gateway; + this.client = client; + } + + + @PostMapping + public ResponseEntity create(@RequestBody TimeEntryForm form) { + if (projectIsActive(form.projectId)) { + TimeEntryRecord record = gateway.create(mapToFields(form)); + return new ResponseEntity<>(present(record), HttpStatus.CREATED); + } + return new ResponseEntity<>(HttpStatus.SERVICE_UNAVAILABLE); + } + + @GetMapping + public List list(@RequestParam long userId) { + return gateway.findAllByUserId(userId).stream() + .map(this::present) + .collect(toList()); + } + + + private TimeEntryInfo present(TimeEntryRecord record) { + return timeEntryInfoBuilder() + .id(record.id) + .projectId(record.projectId) + .userId(record.userId) + .date(record.date.toString()) + .hours(record.hours) + .info("time entry info") + .build(); + } + + private TimeEntryFields mapToFields(TimeEntryForm form) { + return timeEntryFieldsBuilder() + .projectId(form.projectId) + .userId(form.userId) + .date(LocalDate.parse(form.date)) + .hours(form.hours) + .build(); + } + + private boolean projectIsActive(long projectId) { + ProjectInfo project = client.getProject(projectId); + return project != null && project.active; + } +} diff --git a/components/timesheets/src/main/java/io/pivotal/pal/tracker/timesheets/TimeEntryForm.java b/components/timesheets/src/main/java/io/pivotal/pal/tracker/timesheets/TimeEntryForm.java new file mode 100644 index 0000000..96006cd --- /dev/null +++ b/components/timesheets/src/main/java/io/pivotal/pal/tracker/timesheets/TimeEntryForm.java @@ -0,0 +1,92 @@ +package io.pivotal.pal.tracker.timesheets; + +public class TimeEntryForm { + public final long projectId; + public final long userId; + public final String date; + public final int hours; + + private TimeEntryForm() { // for jackson + this(timeEntryFormBuilder()); + } + + private TimeEntryForm(Builder builder) { + projectId = builder.projectId; + userId = builder.userId; + date = builder.date; + hours = builder.hours; + } + + public static Builder timeEntryFormBuilder() { + return new Builder(); + } + + public static class Builder { + private long id; + private long projectId; + private long userId; + private String date; + private int hours; + + public Builder id(long id) { + this.id = id; + return this; + } + + public Builder projectId(long projectId) { + this.projectId = projectId; + return this; + } + + public Builder userId(long userId) { + this.userId = userId; + return this; + } + + public Builder date(String date) { + this.date = date; + return this; + } + + public Builder hours(Integer hours) { + this.hours = hours; + return this; + } + + public TimeEntryForm build() { + return new TimeEntryForm(this); + } + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + TimeEntryForm that = (TimeEntryForm) o; + + if (projectId != that.projectId) return false; + if (userId != that.userId) return false; + if (hours != that.hours) return false; + return date != null ? date.equals(that.date) : that.date == null; + } + + @Override + public int hashCode() { + int result = (int) (projectId ^ (projectId >>> 32)); + result = 31 * result + (int) (userId ^ (userId >>> 32)); + result = 31 * result + (date != null ? date.hashCode() : 0); + result = 31 * result + hours; + return result; + } + + @Override + public String toString() { + return "TimeEntryForm{" + + "projectId=" + projectId + + ", userId=" + userId + + ", date='" + date + '\'' + + ", hours=" + hours + + '}'; + } +} diff --git a/components/timesheets/src/main/java/io/pivotal/pal/tracker/timesheets/TimeEntryInfo.java b/components/timesheets/src/main/java/io/pivotal/pal/tracker/timesheets/TimeEntryInfo.java new file mode 100644 index 0000000..df7b306 --- /dev/null +++ b/components/timesheets/src/main/java/io/pivotal/pal/tracker/timesheets/TimeEntryInfo.java @@ -0,0 +1,109 @@ +package io.pivotal.pal.tracker.timesheets; + +public class TimeEntryInfo { + public final long id; + public final long projectId; + public final long userId; + public final String date; + public final int hours; + public final String info; + + public TimeEntryInfo() { // for jackson + this(timeEntryInfoBuilder()); + } + + private TimeEntryInfo(Builder builder) { + id = builder.id; + projectId = builder.projectId; + userId = builder.userId; + date = builder.date; + hours = builder.hours; + info = builder.info; + } + + public static Builder timeEntryInfoBuilder() { + return new Builder(); + } + + public static class Builder { + private long id; + private long projectId; + private long userId; + private String date; + private int hours; + private String info; + + public Builder id(long id) { + this.id = id; + return this; + } + + public Builder projectId(long projectId) { + this.projectId = projectId; + return this; + } + + public Builder userId(long userId) { + this.userId = userId; + return this; + } + + public Builder date(String date) { + this.date = date; + return this; + } + + public Builder hours(int hours) { + this.hours = hours; + return this; + } + + public Builder info(String info) { + this.info = info; + return this; + } + + public TimeEntryInfo build() { + return new TimeEntryInfo(this); + } + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + TimeEntryInfo that = (TimeEntryInfo) o; + + if (id != that.id) return false; + if (projectId != that.projectId) return false; + if (userId != that.userId) return false; + if (hours != that.hours) return false; + if (date != null ? !date.equals(that.date) : that.date != null) + return false; + return info != null ? info.equals(that.info) : that.info == null; + } + + @Override + public int hashCode() { + int result = (int) (id ^ (id >>> 32)); + result = 31 * result + (int) (projectId ^ (projectId >>> 32)); + result = 31 * result + (int) (userId ^ (userId >>> 32)); + result = 31 * result + (date != null ? date.hashCode() : 0); + result = 31 * result + hours; + result = 31 * result + (info != null ? info.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "TimeEntryInfo{" + + "id=" + id + + ", projectId=" + projectId + + ", userId=" + userId + + ", date=" + date + + ", hours=" + hours + + ", info='" + info + '\'' + + '}'; + } +} diff --git a/components/timesheets/src/main/java/io/pivotal/pal/tracker/timesheets/data/TimeEntryDataGateway.java b/components/timesheets/src/main/java/io/pivotal/pal/tracker/timesheets/data/TimeEntryDataGateway.java new file mode 100644 index 0000000..2e569d2 --- /dev/null +++ b/components/timesheets/src/main/java/io/pivotal/pal/tracker/timesheets/data/TimeEntryDataGateway.java @@ -0,0 +1,66 @@ +package io.pivotal.pal.tracker.timesheets.data; + +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.jdbc.support.GeneratedKeyHolder; +import org.springframework.jdbc.support.KeyHolder; +import org.springframework.stereotype.Repository; + +import javax.sql.DataSource; +import java.sql.Date; +import java.sql.PreparedStatement; +import java.util.List; + +import static io.pivotal.pal.tracker.timesheets.data.TimeEntryRecord.timeEntryRecordBuilder; +import static java.sql.Statement.RETURN_GENERATED_KEYS; + +@Repository +public class TimeEntryDataGateway { + + private JdbcTemplate jdbcTemplate; + + public TimeEntryDataGateway(DataSource dataSource) { + this.jdbcTemplate = new JdbcTemplate(dataSource); + } + + public TimeEntryRecord create(TimeEntryFields fields) { + KeyHolder keyHolder = new GeneratedKeyHolder(); + + jdbcTemplate.update( + connection -> { + PreparedStatement ps = connection.prepareStatement( + "insert into time_entries (project_id, user_id, date, hours) values (?, ?, ?, ?)", RETURN_GENERATED_KEYS); + ps.setLong(1, fields.projectId); + ps.setLong(2, fields.userId); + ps.setDate(3, Date.valueOf(fields.date)); + ps.setInt(4, fields.hours); + return ps; + }, keyHolder); + + return find(keyHolder.getKey().longValue()); + } + + public List findAllByUserId(long userId) { + return jdbcTemplate.query( + "select id, project_id, user_id, date, hours from time_entries where user_id = ?", + rowMapper, userId + ); + } + + + private TimeEntryRecord find(long id) { + return jdbcTemplate.queryForObject( + "select id, project_id, user_id, date, hours from time_entries where id = ?", + rowMapper, id + ); + } + + private RowMapper rowMapper = (rs, num) -> + timeEntryRecordBuilder() + .id(rs.getLong("id")) + .projectId(rs.getLong("project_id")) + .userId(rs.getLong("user_id")) + .date(rs.getDate("date").toLocalDate()) + .hours(rs.getInt("hours")) + .build(); +} diff --git a/components/timesheets/src/main/java/io/pivotal/pal/tracker/timesheets/data/TimeEntryFields.java b/components/timesheets/src/main/java/io/pivotal/pal/tracker/timesheets/data/TimeEntryFields.java new file mode 100644 index 0000000..fe698a8 --- /dev/null +++ b/components/timesheets/src/main/java/io/pivotal/pal/tracker/timesheets/data/TimeEntryFields.java @@ -0,0 +1,86 @@ +package io.pivotal.pal.tracker.timesheets.data; + +import java.time.LocalDate; + +public class TimeEntryFields { + + public final long projectId; + public final long userId; + public final LocalDate date; + public final int hours; + + private TimeEntryFields(Builder builder) { + projectId = builder.projectId; + userId = builder.userId; + date = builder.date; + hours = builder.hours; + } + + public static Builder timeEntryFieldsBuilder() { + return new Builder(); + } + + public static class Builder { + + private long projectId; + private long userId; + private LocalDate date; + private int hours; + + public TimeEntryFields build() { + return new TimeEntryFields(this); + } + + public Builder projectId(long projectId) { + this.projectId = projectId; + return this; + } + + public Builder userId(long userId) { + this.userId = userId; + return this; + } + + public Builder date(LocalDate date) { + this.date = date; + return this; + } + + public Builder hours(int hours) { + this.hours = hours; + return this; + } + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + TimeEntryFields that = (TimeEntryFields) o; + + if (projectId != that.projectId) return false; + if (userId != that.userId) return false; + if (hours != that.hours) return false; + return date != null ? date.equals(that.date) : that.date == null; + } + + @Override + public int hashCode() { + int result = (int) (projectId ^ (projectId >>> 32)); + result = 31 * result + (int) (userId ^ (userId >>> 32)); + result = 31 * result + (date != null ? date.hashCode() : 0); + result = 31 * result + hours; + return result; + } + + @Override + public String toString() { + return "TimeEntryFields{" + + "projectId=" + projectId + + ", userId=" + userId + + ", date=" + date + + ", hours=" + hours + + '}'; + } +} diff --git a/components/timesheets/src/main/java/io/pivotal/pal/tracker/timesheets/data/TimeEntryRecord.java b/components/timesheets/src/main/java/io/pivotal/pal/tracker/timesheets/data/TimeEntryRecord.java new file mode 100644 index 0000000..4c81890 --- /dev/null +++ b/components/timesheets/src/main/java/io/pivotal/pal/tracker/timesheets/data/TimeEntryRecord.java @@ -0,0 +1,97 @@ +package io.pivotal.pal.tracker.timesheets.data; + +import java.time.LocalDate; + +public class TimeEntryRecord { + + public final long id; + public final long projectId; + public final long userId; + public final LocalDate date; + public final int hours; + + private TimeEntryRecord(Builder builder) { + id = builder.id; + projectId = builder.projectId; + userId = builder.userId; + date = builder.date; + hours = builder.hours; + } + + public static Builder timeEntryRecordBuilder() { + return new Builder(); + } + + public static class Builder { + + private long id; + private long projectId; + private long userId; + private LocalDate date; + private int hours; + + public TimeEntryRecord build() { + return new TimeEntryRecord(this); + } + + public Builder id(long id) { + this.id = id; + return this; + } + + public Builder projectId(long projectId) { + this.projectId = projectId; + return this; + } + + public Builder userId(long userId) { + this.userId = userId; + return this; + } + + public Builder date(LocalDate date) { + this.date = date; + return this; + } + + public Builder hours(int hours) { + this.hours = hours; + return this; + } + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + TimeEntryRecord that = (TimeEntryRecord) o; + + if (id != that.id) return false; + if (projectId != that.projectId) return false; + if (userId != that.userId) return false; + if (hours != that.hours) return false; + return date != null ? date.equals(that.date) : that.date == null; + } + + @Override + public int hashCode() { + int result = (int) (id ^ (id >>> 32)); + result = 31 * result + (int) (projectId ^ (projectId >>> 32)); + result = 31 * result + (int) (userId ^ (userId >>> 32)); + result = 31 * result + (date != null ? date.hashCode() : 0); + result = 31 * result + hours; + return result; + } + + @Override + public String toString() { + return "TimeEntryRecord{" + + "id=" + id + + ", projectId=" + projectId + + ", userId=" + userId + + ", date=" + date + + ", hours=" + hours + + '}'; + } +} diff --git a/components/timesheets/src/test/java/test/pivotal/pal/tracker/timesheets/TestBuilders.java b/components/timesheets/src/test/java/test/pivotal/pal/tracker/timesheets/TestBuilders.java new file mode 100644 index 0000000..aaf395c --- /dev/null +++ b/components/timesheets/src/test/java/test/pivotal/pal/tracker/timesheets/TestBuilders.java @@ -0,0 +1,51 @@ +package test.pivotal.pal.tracker.timesheets; + +import io.pivotal.pal.tracker.timesheets.TimeEntryForm; +import io.pivotal.pal.tracker.timesheets.TimeEntryInfo; +import io.pivotal.pal.tracker.timesheets.data.TimeEntryFields; +import io.pivotal.pal.tracker.timesheets.data.TimeEntryRecord; + +import java.time.LocalDate; + +import static io.pivotal.pal.tracker.timesheets.TimeEntryForm.timeEntryFormBuilder; +import static io.pivotal.pal.tracker.timesheets.TimeEntryInfo.timeEntryInfoBuilder; +import static io.pivotal.pal.tracker.timesheets.data.TimeEntryFields.timeEntryFieldsBuilder; +import static io.pivotal.pal.tracker.timesheets.data.TimeEntryRecord.timeEntryRecordBuilder; + +public class TestBuilders { + + public static TimeEntryRecord.Builder testTimeEntryRecordBuilder() { + return timeEntryRecordBuilder() + .id(11) + .projectId(12) + .userId(13) + .date(LocalDate.parse("2017-09-19")) + .hours(20); + } + + public static TimeEntryFields.Builder testTimeEntryFieldsBuilder() { + return timeEntryFieldsBuilder() + .projectId(12) + .userId(13) + .date(LocalDate.parse("2017-09-19")) + .hours(20); + } + + public static TimeEntryForm.Builder testTimeEntryFormBuilder() { + return timeEntryFormBuilder() + .projectId(12) + .userId(13) + .date("2017-09-19") + .hours(20); + } + + public static TimeEntryInfo.Builder testTimeEntryInfoBuilder() { + return timeEntryInfoBuilder() + .id(11) + .projectId(12) + .userId(13) + .date("2017-09-19") + .hours(20) + .info("time entry info"); + } +} diff --git a/components/timesheets/src/test/java/test/pivotal/pal/tracker/timesheets/TimeEntryControllerTest.java b/components/timesheets/src/test/java/test/pivotal/pal/tracker/timesheets/TimeEntryControllerTest.java new file mode 100644 index 0000000..ddc70c5 --- /dev/null +++ b/components/timesheets/src/test/java/test/pivotal/pal/tracker/timesheets/TimeEntryControllerTest.java @@ -0,0 +1,78 @@ +package test.pivotal.pal.tracker.timesheets; + +import io.pivotal.pal.tracker.timesheets.*; +import io.pivotal.pal.tracker.timesheets.data.TimeEntryDataGateway; +import io.pivotal.pal.tracker.timesheets.data.TimeEntryFields; +import io.pivotal.pal.tracker.timesheets.data.TimeEntryRecord; +import org.junit.Test; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +import java.util.List; + +import static java.util.Arrays.asList; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.*; +import static test.pivotal.pal.tracker.timesheets.TestBuilders.*; + + +public class TimeEntryControllerTest { + + private TimeEntryDataGateway gateway = mock(TimeEntryDataGateway.class); + private ProjectClient client = mock(ProjectClient.class); + private TimeEntryController controller = new TimeEntryController(gateway, client); + + + @Test + public void testCreate() { + TimeEntryRecord record = testTimeEntryRecordBuilder().projectId(12).build(); + TimeEntryFields fields = testTimeEntryFieldsBuilder().projectId(12).build(); + TimeEntryForm form = testTimeEntryFormBuilder().projectId(12).build(); + + doReturn(record).when(gateway).create(fields); + doReturn(new ProjectInfo(true)).when(client).getProject(anyLong()); + + + ResponseEntity result = controller.create(form); + + + verify(client).getProject(12L); + assertThat(result.getStatusCode()).isEqualTo(HttpStatus.CREATED); + assertThat(result.getBody()).isEqualTo(testTimeEntryInfoBuilder().projectId(12).build()); + } + + @Test + public void testCreate_WhenFailed() { + doReturn(new ProjectInfo(false)).when(client).getProject(anyLong()); + + + ResponseEntity result = controller.create(testTimeEntryFormBuilder().projectId(12).build()); + + + assertThat(result.getStatusCode()).isEqualTo(HttpStatus.SERVICE_UNAVAILABLE); + } + + @Test + public void testList() { + List records = asList( + testTimeEntryRecordBuilder().id(10).build(), + testTimeEntryRecordBuilder().id(11).build(), + testTimeEntryRecordBuilder().id(12).build() + ); + doReturn(records).when(gateway).findAllByUserId(anyLong()); + int userId = 210; + + + List result = controller.list(userId); + + + verify(gateway).findAllByUserId(userId); + + assertThat(result).containsExactlyInAnyOrder( + testTimeEntryInfoBuilder().id(10).build(), + testTimeEntryInfoBuilder().id(11).build(), + testTimeEntryInfoBuilder().id(12).build() + ); + } +} diff --git a/components/timesheets/src/test/java/test/pivotal/pal/tracker/timesheets/TimeEntryDataGatewayTest.java b/components/timesheets/src/test/java/test/pivotal/pal/tracker/timesheets/TimeEntryDataGatewayTest.java new file mode 100644 index 0000000..bea2719 --- /dev/null +++ b/components/timesheets/src/test/java/test/pivotal/pal/tracker/timesheets/TimeEntryDataGatewayTest.java @@ -0,0 +1,75 @@ +package test.pivotal.pal.tracker.timesheets; + +import io.pivotal.pal.tracker.testsupport.TestScenarioSupport; +import io.pivotal.pal.tracker.timesheets.data.TimeEntryDataGateway; +import io.pivotal.pal.tracker.timesheets.data.TimeEntryFields; +import io.pivotal.pal.tracker.timesheets.data.TimeEntryRecord; +import org.junit.Before; +import org.junit.Test; +import org.springframework.jdbc.core.JdbcTemplate; + +import java.sql.Timestamp; +import java.time.LocalDate; +import java.util.List; +import java.util.Map; + +import static io.pivotal.pal.tracker.timesheets.data.TimeEntryFields.timeEntryFieldsBuilder; +import static io.pivotal.pal.tracker.timesheets.data.TimeEntryRecord.timeEntryRecordBuilder; +import static org.assertj.core.api.Assertions.assertThat; + +public class TimeEntryDataGatewayTest { + + private TestScenarioSupport testScenarioSupport = new TestScenarioSupport("tracker_timesheets_test"); + private JdbcTemplate template = testScenarioSupport.template; + private TimeEntryDataGateway gateway = new TimeEntryDataGateway(testScenarioSupport.dataSource); + + + @Before + public void setUp() throws Exception { + template.execute("DELETE FROM time_entries;"); + } + + @Test + public void testCreate() { + TimeEntryFields fields = timeEntryFieldsBuilder() + .projectId(22L) + .userId(12L) + .date(LocalDate.parse("2016-02-28")) + .hours(8) + .build(); + TimeEntryRecord created = gateway.create(fields); + + + assertThat(created.id).isNotNull(); + assertThat(created.projectId).isEqualTo(22L); + assertThat(created.userId).isEqualTo(12L); + assertThat(created.date).isEqualTo(LocalDate.parse("2016-02-28")); + assertThat(created.hours).isEqualTo(8); + + Map persisted = template.queryForMap("SELECT * FROM time_entries WHERE id = ?", created.id); + + assertThat(persisted.get("project_id")).isEqualTo(22L); + assertThat(persisted.get("user_id")).isEqualTo(12L); + assertThat(persisted.get("date")).isEqualTo(Timestamp.valueOf("2016-02-28 00:00:00")); + assertThat(persisted.get("hours")).isEqualTo(8); + } + + @Test + public void testFindAllByUserId() { + template.execute("insert into time_entries (id, project_id, user_id, date, hours) values (2346, 22, 12, '2016-01-13', 8)"); + + + List result = gateway.findAllByUserId(12L); + + + assertThat(result).containsExactlyInAnyOrder( + timeEntryRecordBuilder() + .id(2346L) + .projectId(22L) + .userId(12L) + .date(LocalDate.parse("2016-01-13")) + .hours(8) + .build() + ); + } +} diff --git a/components/users/build.gradle b/components/users/build.gradle new file mode 100644 index 0000000..faf294b --- /dev/null +++ b/components/users/build.gradle @@ -0,0 +1,5 @@ +dependencies { + compile "org.springframework:spring-jdbc:$springVersion" + compile project(":components:rest-support") + testCompile project(":components:test-support") +} diff --git a/components/users/src/main/java/io/pivotal/pal/tracker/users/UserController.java b/components/users/src/main/java/io/pivotal/pal/tracker/users/UserController.java new file mode 100644 index 0000000..8e92af7 --- /dev/null +++ b/components/users/src/main/java/io/pivotal/pal/tracker/users/UserController.java @@ -0,0 +1,31 @@ +package io.pivotal.pal.tracker.users; + +import io.pivotal.pal.tracker.users.data.UserDataGateway; +import io.pivotal.pal.tracker.users.data.UserRecord; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/users") +public class UserController { + + private final UserDataGateway gateway; + + public UserController(UserDataGateway gateway) { + this.gateway = gateway; + } + + + @GetMapping("/{userId}") + public UserInfo show(@PathVariable long userId) { + UserRecord record = gateway.find(userId); + + if (record == null) { + return null; + } + + return new UserInfo(record.id, record.name, "user info"); + } +} diff --git a/components/users/src/main/java/io/pivotal/pal/tracker/users/UserInfo.java b/components/users/src/main/java/io/pivotal/pal/tracker/users/UserInfo.java new file mode 100644 index 0000000..a1b1675 --- /dev/null +++ b/components/users/src/main/java/io/pivotal/pal/tracker/users/UserInfo.java @@ -0,0 +1,49 @@ +package io.pivotal.pal.tracker.users; + +public class UserInfo { + + public final long id; + public final String name; + public final String info; + + public UserInfo(long id, String name, String info) { + this.id = id; + this.name = name; + this.info = info; + } + + private UserInfo() { + this(0, null, null); + } + + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + UserInfo userInfo = (UserInfo) o; + + if (id != userInfo.id) return false; + if (name != null ? !name.equals(userInfo.name) : userInfo.name != null) + return false; + return info != null ? info.equals(userInfo.info) : userInfo.info == null; + } + + @Override + public int hashCode() { + int result = (int) (id ^ (id >>> 32)); + result = 31 * result + (name != null ? name.hashCode() : 0); + result = 31 * result + (info != null ? info.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "UserInfo{" + + "id=" + id + + ", name='" + name + '\'' + + ", info='" + info + '\'' + + '}'; + } +} diff --git a/components/users/src/main/java/io/pivotal/pal/tracker/users/data/UserDataGateway.java b/components/users/src/main/java/io/pivotal/pal/tracker/users/data/UserDataGateway.java new file mode 100644 index 0000000..ccd7bac --- /dev/null +++ b/components/users/src/main/java/io/pivotal/pal/tracker/users/data/UserDataGateway.java @@ -0,0 +1,50 @@ +package io.pivotal.pal.tracker.users.data; + +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.jdbc.support.GeneratedKeyHolder; +import org.springframework.jdbc.support.KeyHolder; +import org.springframework.stereotype.Repository; + +import javax.sql.DataSource; +import java.sql.PreparedStatement; +import java.util.List; + +import static java.sql.Statement.RETURN_GENERATED_KEYS; + +@Repository +public class UserDataGateway { + + private final JdbcTemplate jdbcTemplate; + + public UserDataGateway(DataSource dataSource) { + this.jdbcTemplate = new JdbcTemplate(dataSource); + } + + + public UserRecord create(String name) { + KeyHolder keyholder = new GeneratedKeyHolder(); + + jdbcTemplate.update(connection -> { + PreparedStatement ps = connection.prepareStatement("insert into users (name) values (?)", RETURN_GENERATED_KEYS); + ps.setString(1, name); + return ps; + }, keyholder); + + return find(keyholder.getKey().longValue()); + } + + public UserRecord find(long id) { + List list = jdbcTemplate.query("select id, name from users where id = ? limit 1", rowMapper, id); + + if (list.isEmpty()) { + return null; + } + + return list.get(0); + } + + + private RowMapper rowMapper = + (rs, num) -> new UserRecord(rs.getLong("id"), rs.getString("name")); +} diff --git a/components/users/src/main/java/io/pivotal/pal/tracker/users/data/UserRecord.java b/components/users/src/main/java/io/pivotal/pal/tracker/users/data/UserRecord.java new file mode 100644 index 0000000..9e769b1 --- /dev/null +++ b/components/users/src/main/java/io/pivotal/pal/tracker/users/data/UserRecord.java @@ -0,0 +1,39 @@ +package io.pivotal.pal.tracker.users.data; + +public class UserRecord { + + public final long id; + public final String name; + + public UserRecord(long id, String name) { + this.id = id; + this.name = name; + } + + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + UserRecord that = (UserRecord) o; + + if (id != that.id) return false; + return name != null ? name.equals(that.name) : that.name == null; + } + + @Override + public int hashCode() { + int result = (int) (id ^ (id >>> 32)); + result = 31 * result + (name != null ? name.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "UserRecord{" + + "id=" + id + + ", name='" + name + '\'' + + '}'; + } +} diff --git a/components/users/src/test/java/test/pivotal/pal/tracker/users/UserControllerTest.java b/components/users/src/test/java/test/pivotal/pal/tracker/users/UserControllerTest.java new file mode 100644 index 0000000..22bdca4 --- /dev/null +++ b/components/users/src/test/java/test/pivotal/pal/tracker/users/UserControllerTest.java @@ -0,0 +1,27 @@ +package test.pivotal.pal.tracker.users; + +import io.pivotal.pal.tracker.users.UserController; +import io.pivotal.pal.tracker.users.UserInfo; +import io.pivotal.pal.tracker.users.data.UserDataGateway; +import io.pivotal.pal.tracker.users.data.UserRecord; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.*; + +public class UserControllerTest { + + private UserDataGateway gateway = mock(UserDataGateway.class); + private UserController controller = new UserController(gateway); + + @Test + public void testShow() { + doReturn(new UserRecord(3L, "Some User")).when(gateway).find(anyLong()); + + UserInfo result = controller.show(3); + + verify(gateway).find(3L); + assertThat(result).isEqualTo(new UserInfo(3L, "Some User", "user info")); + } +} diff --git a/components/users/src/test/java/test/pivotal/pal/tracker/users/data/UserDataGatewayTest.java b/components/users/src/test/java/test/pivotal/pal/tracker/users/data/UserDataGatewayTest.java new file mode 100644 index 0000000..2f24120 --- /dev/null +++ b/components/users/src/test/java/test/pivotal/pal/tracker/users/data/UserDataGatewayTest.java @@ -0,0 +1,57 @@ +package test.pivotal.pal.tracker.users.data; + + +import io.pivotal.pal.tracker.testsupport.TestScenarioSupport; +import io.pivotal.pal.tracker.users.data.UserDataGateway; +import io.pivotal.pal.tracker.users.data.UserRecord; +import org.junit.Before; +import org.junit.Test; +import org.springframework.jdbc.core.JdbcTemplate; + +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +public class UserDataGatewayTest { + + private TestScenarioSupport testScenarioSupport = new TestScenarioSupport("tracker_registration_test"); + private JdbcTemplate template = testScenarioSupport.template; + private UserDataGateway gateway = new UserDataGateway(testScenarioSupport.dataSource); + + + @Before + public void setUp() throws Exception { + template.execute("DELETE FROM projects;"); + template.execute("DELETE FROM accounts;"); + template.execute("DELETE FROM users;"); + } + + @Test + public void testCreate() { + UserRecord createdUser = gateway.create("aUser"); + + + assertThat(createdUser.id).isGreaterThan(0); + assertThat(createdUser.name).isEqualTo("aUser"); + + Map persistedFields = template.queryForMap("SELECT id, name FROM users WHERE id = ?", createdUser.id); + assertThat(persistedFields.get("id")).isEqualTo(createdUser.id); + assertThat(persistedFields.get("name")).isEqualTo(createdUser.name); + } + + @Test + public void testFind() { + template.execute("INSERT INTO users(id, name) VALUES (42346, 'aName'), (42347, 'anotherName'), (42348, 'andAnotherName')"); + + + UserRecord record = gateway.find(42347L); + + + assertThat(record).isEqualTo(new UserRecord(42347L, "anotherName")); + } + + @Test + public void testFind_WhenNotFound() { + assertThat(gateway.find(42347L)).isNull(); + } +} diff --git a/databases/allocations-database/build.gradle b/databases/allocations-database/build.gradle new file mode 100644 index 0000000..641d39b --- /dev/null +++ b/databases/allocations-database/build.gradle @@ -0,0 +1,6 @@ +databases { + devDatabase = "tracker_allocations_dev" + testDatabase = "tracker_allocations_test" + cfDatabase = "tracker-allocations-database" + cfApp = "tracker-allocations" +} diff --git a/databases/allocations-database/migrations/V1__initial_schema.sql b/databases/allocations-database/migrations/V1__initial_schema.sql new file mode 100644 index 0000000..b08ab3e --- /dev/null +++ b/databases/allocations-database/migrations/V1__initial_schema.sql @@ -0,0 +1,11 @@ +create table allocations ( + id bigint(20) not null auto_increment, + project_id bigint(20), + user_id bigint(20), + first_day datetime, + last_day datetime, + + primary key (id) +) +engine = innodb +default charset = utf8; diff --git a/databases/backlog-database/build.gradle b/databases/backlog-database/build.gradle new file mode 100644 index 0000000..4b7cec9 --- /dev/null +++ b/databases/backlog-database/build.gradle @@ -0,0 +1,6 @@ +databases { + devDatabase = "tracker_backlog_dev" + testDatabase = "tracker_backlog_test" + cfDatabase = "tracker-backlog-database" + cfApp = "tracker-backlog" +} diff --git a/databases/backlog-database/migrations/V1__initial_schema.sql b/databases/backlog-database/migrations/V1__initial_schema.sql new file mode 100644 index 0000000..f83716d --- /dev/null +++ b/databases/backlog-database/migrations/V1__initial_schema.sql @@ -0,0 +1,9 @@ +create table stories ( + id bigint(20) not null auto_increment, + project_id bigint(20), + name VARCHAR(255), + + primary key (id) +) +engine = innodb +default charset = utf8; diff --git a/databases/build.gradle b/databases/build.gradle new file mode 100644 index 0000000..81469e0 --- /dev/null +++ b/databases/build.gradle @@ -0,0 +1,7 @@ +import io.pivotal.pal.tracker.gradlebuild.CfMigrationPlugin +import io.pivotal.pal.tracker.gradlebuild.LocalMigrationPlugin + +subprojects { + apply plugin: LocalMigrationPlugin + apply plugin: CfMigrationPlugin +} diff --git a/databases/create_databases.sql b/databases/create_databases.sql new file mode 100644 index 0000000..f9b793a --- /dev/null +++ b/databases/create_databases.sql @@ -0,0 +1,21 @@ +DROP DATABASE IF EXISTS tracker_allocations_dev; +DROP DATABASE IF EXISTS tracker_backlog_dev; +DROP DATABASE IF EXISTS tracker_registration_dev; +DROP DATABASE IF EXISTS tracker_timesheets_dev; +DROP DATABASE IF EXISTS tracker_allocations_test; +DROP DATABASE IF EXISTS tracker_backlog_test; +DROP DATABASE IF EXISTS tracker_registration_test; +DROP DATABASE IF EXISTS tracker_timesheets_test; + +CREATE USER IF NOT EXISTS 'tracker'@'localhost' + identified by ''; +GRANT ALL PRIVILEGES ON *.* TO 'tracker' @'localhost'; + +CREATE DATABASE tracker_allocations_dev; +CREATE DATABASE tracker_backlog_dev; +CREATE DATABASE tracker_registration_dev; +CREATE DATABASE tracker_timesheets_dev; +CREATE DATABASE tracker_allocations_test; +CREATE DATABASE tracker_backlog_test; +CREATE DATABASE tracker_registration_test; +CREATE DATABASE tracker_timesheets_test; diff --git a/databases/registration-database/build.gradle b/databases/registration-database/build.gradle new file mode 100644 index 0000000..e065f2a --- /dev/null +++ b/databases/registration-database/build.gradle @@ -0,0 +1,6 @@ +databases { + devDatabase = "tracker_registration_dev" + testDatabase = "tracker_registration_test" + cfDatabase = "tracker-registration-database" + cfApp = "tracker-registration" +} diff --git a/databases/registration-database/migrations/V1__initial_schema.sql b/databases/registration-database/migrations/V1__initial_schema.sql new file mode 100644 index 0000000..03e3d00 --- /dev/null +++ b/databases/registration-database/migrations/V1__initial_schema.sql @@ -0,0 +1,34 @@ +create table users ( + id bigint(20) not null auto_increment, + name VARCHAR(255), + + primary key (id), + unique key name (name) +) +engine = innodb +default charset = utf8; + +create table accounts ( + id bigint(20) not null auto_increment, + owner_id bigint(20), + name VARCHAR(255), + + primary key (id), + unique key name (name), + constraint foreign key (owner_id) references users (id) +) +engine = innodb +default charset = utf8; + +create table projects ( + id bigint(20) not null auto_increment, + account_id bigint(20), + name VARCHAR(255), + active bit(1) not null default b'1', + + primary key (id), + unique key name (name), + constraint foreign key (account_id) references accounts (id) +) +engine = innodb +default charset = utf8; diff --git a/databases/timesheets-database/build.gradle b/databases/timesheets-database/build.gradle new file mode 100644 index 0000000..db86817 --- /dev/null +++ b/databases/timesheets-database/build.gradle @@ -0,0 +1,6 @@ +databases { + devDatabase = "tracker_timesheets_dev" + testDatabase = "tracker_timesheets_test" + cfDatabase = "tracker-timesheets-database" + cfApp = "tracker-timesheets" +} diff --git a/databases/timesheets-database/migrations/V1__initial_schema.sql b/databases/timesheets-database/migrations/V1__initial_schema.sql new file mode 100644 index 0000000..f57f955 --- /dev/null +++ b/databases/timesheets-database/migrations/V1__initial_schema.sql @@ -0,0 +1,11 @@ +create table time_entries ( + id bigint(20) not null auto_increment, + project_id bigint(20), + user_id bigint(20), + date datetime, + hours int, + + primary key (id) +) +engine = innodb +default charset = utf8; diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..2e2e36f --- /dev/null +++ b/gradle.properties @@ -0,0 +1,4 @@ +org.gradle.workers.max=8 +# Need this to keep Travis CI happy on builds +# or you get an out of memory error. +org.gradle.jvmargs=-Xmx3g diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..457aad0 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..ae45383 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.3-all.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..af6708f --- /dev/null +++ b/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..0f8d593 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/integration-test/build.gradle b/integration-test/build.gradle new file mode 100644 index 0000000..68212ac --- /dev/null +++ b/integration-test/build.gradle @@ -0,0 +1,10 @@ +dependencies { + testCompile project(":components:test-support") + testCompile "com.squareup.okhttp3:okhttp:$okhttpVersion" + testCompile "com.jayway.jsonpath:json-path:$jsonPathVersion" +} + +test.dependsOn ":applications:allocations-server:assemble" +test.dependsOn ":applications:backlog-server:assemble" +test.dependsOn ":applications:registration-server:assemble" +test.dependsOn ":applications:timesheets-server:assemble" diff --git a/integration-test/src/test/java/test/pivotal/pal/tracker/FlowTest.java b/integration-test/src/test/java/test/pivotal/pal/tracker/FlowTest.java new file mode 100644 index 0000000..f21899c --- /dev/null +++ b/integration-test/src/test/java/test/pivotal/pal/tracker/FlowTest.java @@ -0,0 +1,159 @@ +package test.pivotal.pal.tracker; + + +import com.jayway.jsonpath.JsonPath; +import com.jayway.jsonpath.PathNotFoundException; +import io.pivotal.pal.tracker.testsupport.TestScenarioSupport; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import test.pivotal.pal.tracker.support.ApplicationServer; +import test.pivotal.pal.tracker.support.HttpClient; +import test.pivotal.pal.tracker.support.Response; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Fail.fail; +import static test.pivotal.pal.tracker.support.MapBuilder.jsonMapBuilder; + +public class FlowTest { + + private final HttpClient httpClient = new HttpClient(); + private final String workingDir = System.getProperty("user.dir"); + + private ApplicationServer registrationServer = new ApplicationServer(workingDir + "/../applications/registration-server/build/libs/registration-server.jar", "8883"); + private ApplicationServer allocationsServer = new ApplicationServer(workingDir + "/../applications/allocations-server/build/libs/allocations-server.jar", "8881"); + private ApplicationServer backlogServer = new ApplicationServer(workingDir + "/../applications/backlog-server/build/libs/backlog-server.jar", "8882"); + private ApplicationServer timesheetsServer = new ApplicationServer(workingDir + "/../applications/timesheets-server/build/libs/timesheets-server.jar", "8884"); + + private String registrationServerUrl(String path) { + return "http://localhost:8883" + path; + } + + private String allocationsServerUrl(String path) { + return "http://localhost:8881" + path; + } + + private String backlogServerUrl(String path) { + return "http://localhost:8882" + path; + } + + private String timesheetsServerUrl(String path) { + return "http://localhost:8884" + path; + } + + private long findResponseId(Response response) { + try { + return JsonPath.parse(response.body).read("$.id", Long.class); + } catch (PathNotFoundException e) { + try { + return JsonPath.parse(response.body).read("$[0].id", Long.class); + } catch (PathNotFoundException e1) { + fail("Could not find id in response body. Response was: \n" + response); + return -1; + } + } + } + + + @Before + public void setup() throws Exception { + registrationServer.startWithDatabaseName("tracker_registration_test"); + allocationsServer.startWithDatabaseName("tracker_allocations_test"); + backlogServer.startWithDatabaseName("tracker_backlog_test"); + timesheetsServer.startWithDatabaseName("tracker_timesheets_test"); + ApplicationServer.waitOnPorts("8881", "8882", "8883", "8884"); + TestScenarioSupport.clearAllDatabases(); + } + + @After + public void tearDown() { + registrationServer.stop(); + allocationsServer.stop(); + backlogServer.stop(); + timesheetsServer.stop(); + } + + @Test + public void testBasicFlow() throws Exception { + Response response; + + response = httpClient.get(registrationServerUrl("/")); + assertThat(response.body).isEqualTo("Noop!"); + + response = httpClient.post(registrationServerUrl("/registration"), jsonMapBuilder() + .put("name", "aUser") + .build() + ); + long createdUserId = findResponseId(response); + assertThat(createdUserId).isGreaterThan(0); + + response = httpClient.get(registrationServerUrl("/users/" + createdUserId)); + assertThat(response.body).isNotNull().isNotEmpty(); + + response = httpClient.get(registrationServerUrl("/accounts?ownerId=" + createdUserId)); + long createdAccountId = findResponseId(response); + assertThat(createdAccountId).isGreaterThan(0); + + response = httpClient.post(registrationServerUrl("/projects"), jsonMapBuilder() + .put("accountId", createdAccountId) + .put("name", "aProject") + .build() + ); + long createdProjectId = findResponseId(response); + assertThat(createdProjectId).isGreaterThan(0); + + response = httpClient.get(registrationServerUrl("/projects?accountId=" + createdAccountId)); + assertThat(findResponseId(response)).isEqualTo(createdProjectId); + + + response = httpClient.get(allocationsServerUrl("/")); + assertThat(response.body).isEqualTo("Noop!"); + + response = httpClient.post( + allocationsServerUrl("/allocations"), jsonMapBuilder() + .put("projectId", createdProjectId) + .put("userId", createdUserId) + .put("firstDay", "2015-05-17") + .put("lastDay", "2015-05-26") + .build() + ); + + long createdAllocationId = findResponseId(response); + assertThat(createdAllocationId).isGreaterThan(0); + + response = httpClient.get(allocationsServerUrl("/allocations?projectId=" + createdProjectId)); + assertThat(findResponseId(response)).isEqualTo(createdAllocationId); + + + response = httpClient.get(backlogServerUrl("/")); + assertThat(response.body).isEqualTo("Noop!"); + + response = httpClient.post(backlogServerUrl("/stories"), jsonMapBuilder() + .put("projectId", createdProjectId) + .put("name", "A story") + .build() + ); + long createdStoryId = findResponseId(response); + assertThat(createdStoryId).isGreaterThan(0); + + response = httpClient.get(backlogServerUrl("/stories?projectId=" + createdProjectId)); + assertThat(findResponseId(response)).isEqualTo(createdStoryId); + + + response = httpClient.get(timesheetsServerUrl("/")); + assertThat(response.body).isEqualTo("Noop!"); + + response = httpClient.post(timesheetsServerUrl("/time-entries"), jsonMapBuilder() + .put("projectId", createdProjectId) + .put("userId", createdUserId) + .put("date", "2015-12-17") + .put("hours", 8) + .build() + ); + long createdTimeEntryId = findResponseId(response); + assertThat(createdTimeEntryId).isGreaterThan(0); + + response = httpClient.get(timesheetsServerUrl("/time-entries?userId=" + createdUserId)); + assertThat(findResponseId(response)).isEqualTo(createdTimeEntryId); + } +} diff --git a/integration-test/src/test/java/test/pivotal/pal/tracker/support/ApplicationServer.java b/integration-test/src/test/java/test/pivotal/pal/tracker/support/ApplicationServer.java new file mode 100644 index 0000000..023b1b8 --- /dev/null +++ b/integration-test/src/test/java/test/pivotal/pal/tracker/support/ApplicationServer.java @@ -0,0 +1,80 @@ +package test.pivotal.pal.tracker.support; + +import java.io.IOException; +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.util.Map; + +import static org.assertj.core.api.Assertions.fail; +import static test.pivotal.pal.tracker.support.MapBuilder.envMapBuilder; + +public class ApplicationServer { + + private final String jarPath; + private final String port; + + private Process serverProcess; + + public ApplicationServer(String jarPath, String port) { + this.jarPath = jarPath; + this.port = port; + } + + + public void start(Map env) throws IOException, InterruptedException { + ProcessBuilder processBuilder = new ProcessBuilder() + .command("java", "-jar", jarPath) + .inheritIO(); + + processBuilder.environment().put("SERVER_PORT", port); + env.forEach((key, value) -> processBuilder.environment().put(key, value)); + + serverProcess = processBuilder.start(); + } + + public void startWithDatabaseName(String dbName) throws IOException, InterruptedException { + String dbUrl = "jdbc:mysql://localhost:3306/" + dbName + "?useSSL=false&useTimezone=true&serverTimezone=UTC&useLegacyDatetimeCode=false"; + + start(envMapBuilder() + .put("SPRING_DATASOURCE_URL", dbUrl) + .put("REGISTRATION_SERVER_ENDPOINT", "http://localhost:8883") + .build() + ); + } + + public void stop() { + serverProcess.destroyForcibly(); + } + + + public static void waitOnPorts(String... ports) throws InterruptedException { + for (String port : ports) waitUntilServerIsUp(port); + } + + private static void waitUntilServerIsUp(String port) throws InterruptedException { + HttpClient httpClient = new HttpClient(); + int timeout = 120; + Instant start = Instant.now(); + boolean isUp = false; + + System.out.print("Waiting on port " + port + "..."); + + while (!isUp) { + try { + httpClient.get("http://localhost:" + port); + isUp = true; + System.out.println(" server is up."); + } catch (Throwable e) { + + long timeSpent = ChronoUnit.SECONDS.between(start, Instant.now()); + if (timeSpent > timeout) { + fail("Timed out waiting for server on port " + port); + } + + System.out.print("."); + Thread.sleep(200); + } + } + } +} + diff --git a/integration-test/src/test/java/test/pivotal/pal/tracker/support/HttpClient.java b/integration-test/src/test/java/test/pivotal/pal/tracker/support/HttpClient.java new file mode 100644 index 0000000..228761d --- /dev/null +++ b/integration-test/src/test/java/test/pivotal/pal/tracker/support/HttpClient.java @@ -0,0 +1,67 @@ +package test.pivotal.pal.tracker.support; + +import com.fasterxml.jackson.databind.ObjectMapper; +import okhttp3.*; + +import java.io.IOException; +import java.util.Map; + +public class HttpClient { + + private static final MediaType JSON = MediaType.parse("application/json"); + + private final OkHttpClient okHttp = new OkHttpClient(); + private final ObjectMapper objectMapper = new ObjectMapper(); + + + public Response get(String url) { + return fetch(new Request.Builder().url(url)); + } + + public Response post(String url, Map jsonBody) { + try { + Request.Builder reqBuilder = new Request.Builder() + .url(url) + .post(RequestBody.create(JSON, objectMapper.writeValueAsString(jsonBody))); + + return fetch(reqBuilder); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public Response put(String url, Map jsonBody) { + try { + Request.Builder reqBuilder = new Request.Builder() + .url(url) + .put(RequestBody.create(JSON, objectMapper.writeValueAsString(jsonBody))); + + return fetch(reqBuilder); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public Response delete(String url) { + return fetch(new Request.Builder().delete().url(url)); + } + + + private Response fetch(Request.Builder requestBuilder) { + try { + Request request = requestBuilder.build(); + + okhttp3.Response response = okHttp.newCall(request).execute(); + ResponseBody body = response.body(); + + if (body == null) { + return new Response(response.code(), ""); + } + + return new Response(response.code(), body.string()); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} + diff --git a/integration-test/src/test/java/test/pivotal/pal/tracker/support/MapBuilder.java b/integration-test/src/test/java/test/pivotal/pal/tracker/support/MapBuilder.java new file mode 100644 index 0000000..a47466f --- /dev/null +++ b/integration-test/src/test/java/test/pivotal/pal/tracker/support/MapBuilder.java @@ -0,0 +1,26 @@ +package test.pivotal.pal.tracker.support; + +import java.util.HashMap; +import java.util.Map; + +public class MapBuilder { + + private Map map = new HashMap<>(); + + public static MapBuilder envMapBuilder() { + return new MapBuilder<>(); + } + + public static MapBuilder jsonMapBuilder() { + return new MapBuilder<>(); + } + + public MapBuilder put(K key, V value) { + map.put(key, value); + return this; + } + + public Map build() { + return map; + } +} diff --git a/integration-test/src/test/java/test/pivotal/pal/tracker/support/Response.java b/integration-test/src/test/java/test/pivotal/pal/tracker/support/Response.java new file mode 100644 index 0000000..101b76f --- /dev/null +++ b/integration-test/src/test/java/test/pivotal/pal/tracker/support/Response.java @@ -0,0 +1,19 @@ +package test.pivotal.pal.tracker.support; + +public class Response { + public final int status; + public final String body; + + public Response(int status, String body) { + this.status = status; + this.body = body; + } + + @Override + public String toString() { + return "Response{" + + "status=" + status + + ", body='" + body + '\'' + + '}'; + } +} diff --git a/manifest-allocations.yml b/manifest-allocations.yml new file mode 100644 index 0000000..0049f3a --- /dev/null +++ b/manifest-allocations.yml @@ -0,0 +1,12 @@ +applications: +- name: tracker-allocations + path: ./applications/allocations-server/build/libs/allocations-server.jar + routes: + - route: allocations-pal-{UNIQUE_IDENTIFIER}.apps.chicken.pal.pivotal.io + memory: 1G + instances: 1 + env: + REGISTRATION_SERVER_ENDPOINT: http://FILL_ME_IN + JBP_CONFIG_OPEN_JDK_JRE: '{ jre: { version: 11.+ } }' + services: + - tracker-allocations-database diff --git a/manifest-backlog.yml b/manifest-backlog.yml new file mode 100644 index 0000000..2747909 --- /dev/null +++ b/manifest-backlog.yml @@ -0,0 +1,12 @@ +applications: +- name: tracker-backlog + path: ./applications/backlog-server/build/libs/backlog-server.jar + routes: + - route: backlog-pal-{UNIQUE_IDENTIFIER}.apps.chicken.pal.pivotal.io + memory: 1G + instances: 1 + env: + REGISTRATION_SERVER_ENDPOINT: http://FILL_ME_IN + JBP_CONFIG_OPEN_JDK_JRE: '{ jre: { version: 11.+ } }' + services: + - tracker-backlog-database diff --git a/manifest-registration.yml b/manifest-registration.yml new file mode 100644 index 0000000..3eb3e1d --- /dev/null +++ b/manifest-registration.yml @@ -0,0 +1,11 @@ +applications: +- name: tracker-registration + path: ./applications/registration-server/build/libs/registration-server.jar + routes: + - route: registration-pal-{UNIQUE_IDENTIFIER}.apps.chicken.pal.pivotal.io + memory: 1G + instances: 1 + env: + JBP_CONFIG_OPEN_JDK_JRE: '{ jre: { version: 11.+ } }' + services: + - tracker-registration-database diff --git a/manifest-timesheets.yml b/manifest-timesheets.yml new file mode 100644 index 0000000..abc6cf9 --- /dev/null +++ b/manifest-timesheets.yml @@ -0,0 +1,12 @@ +applications: +- name: tracker-timesheets + path: ./applications/timesheets-server/build/libs/timesheets-server.jar + routes: + - route: timesheets-pal-{UNIQUE_IDENTIFIER}.apps.chicken.pal.pivotal.io + memory: 1G + instances: 1 + env: + REGISTRATION_SERVER_ENDPOINT: http://FILL_ME_IN + JBP_CONFIG_OPEN_JDK_JRE: '{ jre: { version: 11.+ } }' + services: + - tracker-timesheets-database diff --git a/requests.http b/requests.http new file mode 100644 index 0000000..1d1532a --- /dev/null +++ b/requests.http @@ -0,0 +1,49 @@ +### Create user +POST {{registrationUrl}}/registration +Content-Type: application/json + +{"name": "Sally"} + +### Get user +GET {{registrationUrl}}/users/USER-ID + +### Get account +GET {{registrationUrl}}/accounts?ownerId=USER-ID + +### Create project +POST {{registrationUrl}}/projects +Content-Type: application/json + +{"name": "Basket Weaving III", "accountId": ACCOUNT-ID} + +### Get projects +GET {{registrationUrl}}/projects?accountId=ACCOUNT-ID + +### Create allocation +POST {{allocationsUrl}}/allocations +Content-Type: application/json + +{"projectId": PROJECT-ID, "userId": USER-ID, "firstDay": "2015-05-17", "lastDay": "2015-05-18"} + +### Get allocations +GET {{allocationsUrl}}/allocations?projectId=PROJECT-ID + +### Create story +POST {{backlogUrl}}/stories +Content-Type: application/json + +{"projectId": PROJECT-ID, "name": "Find some reeds"} + +### Get stories +GET {{backlogUrl}}/stories?projectId=PROJECT-ID + +### Create timesheet +POST {{timesheetsUrl}}/time-entries/ +Content-Type: application/json + +{"projectId": PROJECT-ID, "userId": USER-ID, "date": "2015-05-17", "hours": 6} + +### Get timesheets +GET {{timesheetsUrl}}/time-entries?userId=USER-ID + +### diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..307ee44 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,23 @@ +rootProject.name = "pal-tracker-distributed" + +include "applications:allocations-server" +include "applications:backlog-server" +include "applications:registration-server" +include "applications:timesheets-server" + +include "components:accounts" +include "components:allocations" +include "components:backlog" +include "components:projects" +include "components:timesheets" +include "components:users" + +include "components:rest-support" +include "components:test-support" + +include "databases:allocations-database" +include "databases:backlog-database" +include "databases:registration-database" +include "databases:timesheets-database" + +include "integration-test"