diff --git a/.gitignore b/.gitignore index 0c9d47c18..019b70b33 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ build .gradle .idea *.iml +out +ci/variables.yml diff --git a/build.gradle b/build.gradle new file mode 100644 index 000000000..0653f71aa --- /dev/null +++ b/build.gradle @@ -0,0 +1,67 @@ +import org.flywaydb.gradle.task.FlywayMigrateTask + +buildscript { + ext.kotlin_version = '1.1.4-3' +} + +plugins { + id 'java' + id 'org.springframework.boot' version '1.5.4.RELEASE' + id 'org.jetbrains.kotlin.jvm' version '1.1.4-3' + id 'org.jetbrains.kotlin.plugin.spring' version '1.1.4-3' + id 'org.jetbrains.kotlin.plugin.noarg' version '1.1.4-3' + id 'org.jetbrains.kotlin.plugin.jpa' version '1.1.4-3' + id 'org.flywaydb.flyway' version '4.2.0' +} + +repositories { + mavenCentral() +} + +compileKotlin { + kotlinOptions { + jvmTarget = '1.8' + } +} +compileTestKotlin { + kotlinOptions { + jvmTarget = '1.8' + } +} + +dependencies { + compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version" + compile 'org.springframework.boot:spring-boot-starter-web' + compile 'org.springframework.boot:spring-boot-starter-actuator' + compile 'org.springframework.boot:spring-boot-starter-data-jpa' + compile 'org.springframework.boot:spring-boot-starter-security' + compile 'com.fasterxml.jackson.module:jackson-module-kotlin:2.9.0' + compile 'mysql:mysql-connector-java:6.0.6' + testCompile 'com.nhaarman:mockito-kotlin:1.5.0' + testCompile 'org.springframework.boot:spring-boot-starter-test' +} + +def developmentDbUrl = "jdbc:mysql://localhost:3306/tracker_dev?user=tracker&useSSL=false&useTimezone=true&serverTimezone=UTC&useLegacyDatetimeCode=false" +bootRun.environment([ + "WELCOME_MESSAGE": "hello", + "SPRING_DATASOURCE_URL": developmentDbUrl, + "MANAGEMENT_SECURITY_ENABLED": false, +]) + +def testDbUrl = "jdbc:mysql://localhost:3306/tracker_test?user=tracker&useSSL=false&useTimezone=true&serverTimezone=UTC&useLegacyDatetimeCode=false" +test.environment([ + "WELCOME_MESSAGE": "Hello from test", + "SPRING_DATASOURCE_URL": testDbUrl, + "MANAGEMENT_SECURITY_ENABLED": false, +]) + +flyway { + url = developmentDbUrl + user = "tracker" + password = "" + locations = ["filesystem:databases/tracker/migrations"] +} + +task testMigrate(type: FlywayMigrateTask) { + url = testDbUrl +} diff --git a/ci/build.yml b/ci/build.yml new file mode 100644 index 000000000..85f0630a7 --- /dev/null +++ b/ci/build.yml @@ -0,0 +1,45 @@ +platform: linux + +image_resource: + type: docker-image + source: + repository: openjdk + tag: '8-jdk' + +inputs: + - name: pal-tracker + - name: version + +outputs: + - name: build-output + +caches: + - path: .gradle/ + +run: + path: bash + args: + - -exc + - | + + function stop_mysql { + service mysql stop + } + trap stop_mysql EXIT + + export DEBIAN_FRONTEND="noninteractive" + + apt-get update + apt-get install -y software-properties-common + apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db + apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8 + add-apt-repository 'deb http://ftp.osuosl.org/pub/mariadb/repo/10.2/ubuntu trusty main' + + apt-get -y install mariadb-server + service mysql start + + cd pal-tracker + + mysql -uroot < databases/tracker/create_databases.sql + ./gradlew -P version=$(cat ../version/number) testMigrate build + cp build/libs/pal-tracker-*.jar ../build-output \ No newline at end of file diff --git a/ci/pipeline.yml b/ci/pipeline.yml new file mode 100644 index 000000000..c34c3b2a9 --- /dev/null +++ b/ci/pipeline.yml @@ -0,0 +1,83 @@ +--- +resources: +- name: pal-tracker + type: git + source: + uri: {{github-repository}} + branch: master + private_key: {{github-private-key}} + +- name: pal-tracker-artifacts + type: s3 + source: + bucket: {{aws-bucket}} + regexp: releases/pal-tracker-(.*).jar + access_key_id: {{aws-access-key-id}} + secret_access_key: {{aws-secret-access-key}} + +- name: version + type: semver + source: + bucket: {{aws-bucket}} + key: pal-tracker/version + access_key_id: {{aws-access-key-id}} + secret_access_key: {{aws-secret-access-key}} + +- name: review-deployment + type: cf + source: + api: {{cf-api-url}} + username: {{cf-username}} + password: {{cf-password}} + organization: {{cf-org}} + space: review + +- name: production-deployment + type: cf + source: + api: {{cf-api-url}} + username: {{cf-username}} + password: {{cf-password}} + organization: {{cf-org}} + space: production + +jobs: +- name: build + plan: + - get: pal-tracker + trigger: true + - get: version + params: {bump: patch} + - task: build and test + file: pal-tracker/ci/build.yml + - put: pal-tracker-artifacts + params: + file: build-output/pal-tracker-*.jar + - put: version + params: + file: version/number + +- name: deploy-review + plan: + - get: pal-tracker + - get: pal-tracker-artifacts + trigger: true + passed: [build] + - put: review-deployment + params: + manifest: pal-tracker/manifest-review.yml + path: pal-tracker-artifacts/pal-tracker-*.jar + environment_variables: + WELCOME_MESSAGE: "Hello from the review environment" + +- name: deploy-production + plan: + - get: pal-tracker + - get: pal-tracker-artifacts + passed: [deploy-review] + - put: production-deployment + params: + manifest: pal-tracker/manifest-production.yml + path: pal-tracker-artifacts/pal-tracker-*.jar + environment_variables: + WELCOME_MESSAGE: "Hello from the production environment" \ No newline at end of file diff --git a/ci/variables.example.yml b/ci/variables.example.yml new file mode 100644 index 000000000..fe86f4a03 --- /dev/null +++ b/ci/variables.example.yml @@ -0,0 +1,12 @@ +cf-api-url: CF_API_URL +cf-username: CF_USERNAME +cf-password: CF_PASSWORD +cf-org: CF_ORG +aws-access-key-id: +aws-secret-access-key: +aws-bucket: +github-repository: git@github.com:GITHUB_USERNAME/pal-tracker.git +github-private-key: | + -----BEGIN RSA PRIVATE KEY----- + REPLACE WITH YOUR PRIVATE KEY HERE + -----END RSA PRIVATE KEY----- diff --git a/databases/tracker/create_databases.sql b/databases/tracker/create_databases.sql new file mode 100644 index 000000000..e99807019 --- /dev/null +++ b/databases/tracker/create_databases.sql @@ -0,0 +1,9 @@ +DROP DATABASE IF EXISTS tracker_dev; +DROP DATABASE IF EXISTS tracker_test; + +CREATE USER 'tracker'@'localhost' + IDENTIFIED BY ''; +GRANT ALL PRIVILEGES ON *.* TO 'tracker' @'localhost'; + +CREATE DATABASE tracker_dev; +CREATE DATABASE tracker_test; diff --git a/databases/tracker/migrations/V1__initial_schema.sql b/databases/tracker/migrations/V1__initial_schema.sql new file mode 100644 index 000000000..eaaa0c152 --- /dev/null +++ b/databases/tracker/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 VARCHAR(255), + hours INT, + + PRIMARY KEY (id) +) + ENGINE = innodb + DEFAULT CHARSET = utf8; \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 000000000..c74aa66dc 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 000000000..3b8ee0c29 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Tue Sep 05 08:21:19 AEST 2017 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-3.4.1-all.zip diff --git a/gradlew b/gradlew new file mode 100755 index 000000000..4453ccea3 --- /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="" + +# 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 000000000..e95643d6a --- /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= + +@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/manifest-production.yml b/manifest-production.yml new file mode 100644 index 000000000..b2ee4ba9c --- /dev/null +++ b/manifest-production.yml @@ -0,0 +1,9 @@ +--- +applications: +- name: pal-tracker + path: build/libs/pal-tracker.jar + host: dl-pal-tracker + services: + - tracker-database + env: + SECURITY_FORCE_HTTPS: true diff --git a/manifest-review.yml b/manifest-review.yml new file mode 100644 index 000000000..7f80f6eda --- /dev/null +++ b/manifest-review.yml @@ -0,0 +1,9 @@ +--- +applications: +- name: pal-tracker + path: build/libs/pal-tracker.jar + host: dl-pal-tracker-review + services: + - tracker-database + env: + SECURITY_FORCE_HTTPS: true diff --git a/pal-tracker-codebase.txt b/pal-tracker-codebase.txt deleted file mode 100644 index 0943c3cd3..000000000 --- a/pal-tracker-codebase.txt +++ /dev/null @@ -1 +0,0 @@ -pal-tracker codebase diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 000000000..e31056948 --- /dev/null +++ b/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'pal-tracker' diff --git a/src/main/kotlin/io/pivotal/pal/tracker/EnvController.kt b/src/main/kotlin/io/pivotal/pal/tracker/EnvController.kt new file mode 100644 index 000000000..dcb07a2ca --- /dev/null +++ b/src/main/kotlin/io/pivotal/pal/tracker/EnvController.kt @@ -0,0 +1,23 @@ +package io.pivotal.pal.tracker + +import org.springframework.beans.factory.annotation.Value +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.RestController + +@RestController +class EnvController( + @Value("\${PORT:NOT SET}") port: String, + @Value("\${MEMORY_LIMIT:NOT SET}") memoryLimit: String , + @Value("\${CF_INSTANCE_INDEX:NOT SET}") cfInstanceIndex: String, + @Value("\${CF_INSTANCE_ADDR:NOT SET}") cfInstanceAddr: String) { + + private val env: Map = mapOf( + "PORT" to port, + "MEMORY_LIMIT" to memoryLimit, + "CF_INSTANCE_INDEX" to cfInstanceIndex, + "CF_INSTANCE_ADDR" to cfInstanceAddr + ) + + @GetMapping("/env") + fun getEnv() = env +} diff --git a/src/main/kotlin/io/pivotal/pal/tracker/PalTrackerApplication.kt b/src/main/kotlin/io/pivotal/pal/tracker/PalTrackerApplication.kt new file mode 100644 index 000000000..efa93dbef --- /dev/null +++ b/src/main/kotlin/io/pivotal/pal/tracker/PalTrackerApplication.kt @@ -0,0 +1,11 @@ +package io.pivotal.pal.tracker + +import org.springframework.boot.SpringApplication +import org.springframework.boot.autoconfigure.SpringBootApplication + +@SpringBootApplication +class PalTrackerApplication + +fun main(args: Array) { + SpringApplication.run(PalTrackerApplication::class.java, *args) +} diff --git a/src/main/kotlin/io/pivotal/pal/tracker/SecurityConfiguration.kt b/src/main/kotlin/io/pivotal/pal/tracker/SecurityConfiguration.kt new file mode 100644 index 000000000..ac0b505ae --- /dev/null +++ b/src/main/kotlin/io/pivotal/pal/tracker/SecurityConfiguration.kt @@ -0,0 +1,31 @@ +package io.pivotal.pal.tracker + +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder +import org.springframework.security.config.annotation.web.builders.HttpSecurity +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter + +@EnableWebSecurity +class SecurityConfiguration : WebSecurityConfigurerAdapter() { + override fun configure(http: HttpSecurity) { + val forceHttps = System.getenv("SECURITY_FORCE_HTTPS") + if (forceHttps != null && forceHttps == "true") { + http.requiresChannel().anyRequest().requiresSecure() + } + + http + .authorizeRequests().antMatchers("/**").hasRole("USER") + .and() + .httpBasic() + .and() + .csrf().disable() + } + + @Autowired + fun configureGlobal(auth: AuthenticationManagerBuilder) { + auth + .inMemoryAuthentication() + .withUser("user").password("password").roles("USER") + } +} diff --git a/src/main/kotlin/io/pivotal/pal/tracker/TimeEntry.kt b/src/main/kotlin/io/pivotal/pal/tracker/TimeEntry.kt new file mode 100644 index 000000000..6d6765d31 --- /dev/null +++ b/src/main/kotlin/io/pivotal/pal/tracker/TimeEntry.kt @@ -0,0 +1,23 @@ +package io.pivotal.pal.tracker + +import javax.persistence.Entity +import javax.persistence.GeneratedValue +import javax.persistence.Id + +@Entity(name = "time_entries") +data class TimeEntry( + @Id @GeneratedValue val id: Long?, + val projectId: Long, + val userId: Long, + val date: String?, + val hours: Int +) { + + constructor(projectId: Long, userId: Long, date: String, hours: Int) : this( + id = null, + projectId = projectId, + userId = userId, + date = date, + hours = hours + ) +} diff --git a/src/main/kotlin/io/pivotal/pal/tracker/TimeEntryController.kt b/src/main/kotlin/io/pivotal/pal/tracker/TimeEntryController.kt new file mode 100644 index 000000000..0868c7d2f --- /dev/null +++ b/src/main/kotlin/io/pivotal/pal/tracker/TimeEntryController.kt @@ -0,0 +1,68 @@ +package io.pivotal.pal.tracker + +import org.springframework.boot.actuate.metrics.CounterService +import org.springframework.boot.actuate.metrics.GaugeService +import org.springframework.http.HttpStatus +import org.springframework.http.ResponseEntity +import org.springframework.web.bind.annotation.DeleteMapping +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PathVariable +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.PutMapping +import org.springframework.web.bind.annotation.RequestBody +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RestController + +@RestController +@RequestMapping("/time-entries") +class TimeEntryController( + private val timeEntriesRepo: TimeEntryRepository, + private val counter: CounterService, + private val gauge: GaugeService +) { + + @PostMapping + fun create(@RequestBody timeEntry: TimeEntry): ResponseEntity { + val createdTimeEntry = timeEntriesRepo.save(timeEntry) + counter.increment("TimeEntry.created") + gauge.submit("timeEntries.count", timeEntriesRepo.count().toDouble()) + return ResponseEntity(createdTimeEntry, HttpStatus.CREATED) + } + + @GetMapping("{id}") + fun read(@PathVariable id: Long): ResponseEntity { + val timeEntry = timeEntriesRepo.findOne(id) + return if (timeEntry != null) { + counter.increment("TimeEntry.read") + ResponseEntity(timeEntry, HttpStatus.OK) + } else { + ResponseEntity(HttpStatus.NOT_FOUND) + } + } + + @GetMapping + fun list(): ResponseEntity> { + counter.increment("TimeEntry.listed") + return ResponseEntity(timeEntriesRepo.findAll(), HttpStatus.OK) + } + + @PutMapping("{id}") + fun update(@PathVariable id: Long, @RequestBody timeEntry: TimeEntry): ResponseEntity { + val updatedTimeEntry = timeEntriesRepo.save(timeEntry.copy(id = id)) + return if (updatedTimeEntry != null) { + counter.increment("TimeEntry.updated") + ResponseEntity(updatedTimeEntry, HttpStatus.OK) + } else { + ResponseEntity(HttpStatus.NOT_FOUND) + } + } + + @DeleteMapping("{id}") + fun delete(@PathVariable id: Long): ResponseEntity { + timeEntriesRepo.delete(id) + counter.increment("TimeEntry.deleted") + gauge.submit("timeEntries.count", timeEntriesRepo.count().toDouble()) + + return ResponseEntity(HttpStatus.NO_CONTENT) + } +} diff --git a/src/main/kotlin/io/pivotal/pal/tracker/TimeEntryHealthIndicator.kt b/src/main/kotlin/io/pivotal/pal/tracker/TimeEntryHealthIndicator.kt new file mode 100644 index 000000000..2bdde2118 --- /dev/null +++ b/src/main/kotlin/io/pivotal/pal/tracker/TimeEntryHealthIndicator.kt @@ -0,0 +1,25 @@ +package io.pivotal.pal.tracker + +import org.springframework.boot.actuate.health.Health +import org.springframework.boot.actuate.health.HealthIndicator +import org.springframework.stereotype.Component + +@Component +class TimeEntryHealthIndicator(private val timeEntryRepo: TimeEntryRepository) : HealthIndicator { + + override fun health(): Health { + val builder = Health.Builder() + + if (timeEntryRepo.count() < MAX_TIME_ENTRIES) { + builder.up() + } else { + builder.down() + } + + return builder.build() + } + + companion object { + private val MAX_TIME_ENTRIES = 5 + } +} diff --git a/src/main/kotlin/io/pivotal/pal/tracker/TimeEntryRepository.kt b/src/main/kotlin/io/pivotal/pal/tracker/TimeEntryRepository.kt new file mode 100644 index 000000000..181609eb2 --- /dev/null +++ b/src/main/kotlin/io/pivotal/pal/tracker/TimeEntryRepository.kt @@ -0,0 +1,7 @@ +package io.pivotal.pal.tracker + +import org.springframework.data.repository.CrudRepository +import org.springframework.stereotype.Repository + +@Repository +interface TimeEntryRepository : CrudRepository diff --git a/src/main/kotlin/io/pivotal/pal/tracker/WelcomeController.kt b/src/main/kotlin/io/pivotal/pal/tracker/WelcomeController.kt new file mode 100644 index 000000000..f13a410b1 --- /dev/null +++ b/src/main/kotlin/io/pivotal/pal/tracker/WelcomeController.kt @@ -0,0 +1,12 @@ +package io.pivotal.pal.tracker + +import org.springframework.beans.factory.annotation.Value +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.RestController + +@RestController +class WelcomeController(@Value("\${welcome_message}") private val message: String) { + + @GetMapping("/") + fun sayHello() = message +} diff --git a/src/test/kotlin/test/pivotal/pal/tracker/EnvControllerTest.kt b/src/test/kotlin/test/pivotal/pal/tracker/EnvControllerTest.kt new file mode 100644 index 000000000..5d420bcae --- /dev/null +++ b/src/test/kotlin/test/pivotal/pal/tracker/EnvControllerTest.kt @@ -0,0 +1,24 @@ +package test.pivotal.pal.tracker + +import io.pivotal.pal.tracker.EnvController +import org.assertj.core.api.Assertions.assertThat +import org.junit.Test + +class EnvControllerTest { + @Test + fun getEnv() { + val controller = EnvController( + "8675", + "12G", + "34", + "123.sesame.street" + ) + + val env = controller.getEnv() + + assertThat(env["PORT"]).isEqualTo("8675") + assertThat(env["MEMORY_LIMIT"]).isEqualTo("12G") + assertThat(env["CF_INSTANCE_INDEX"]).isEqualTo("34") + assertThat(env["CF_INSTANCE_ADDR"]).isEqualTo("123.sesame.street") + } +} diff --git a/src/test/kotlin/test/pivotal/pal/tracker/TimeEntryControllerTest.kt b/src/test/kotlin/test/pivotal/pal/tracker/TimeEntryControllerTest.kt new file mode 100644 index 000000000..a9df42359 --- /dev/null +++ b/src/test/kotlin/test/pivotal/pal/tracker/TimeEntryControllerTest.kt @@ -0,0 +1,107 @@ +package test.pivotal.pal.tracker + +import com.nhaarman.mockito_kotlin.any +import com.nhaarman.mockito_kotlin.doReturn +import com.nhaarman.mockito_kotlin.mock +import com.nhaarman.mockito_kotlin.verify +import io.pivotal.pal.tracker.TimeEntry +import io.pivotal.pal.tracker.TimeEntryController +import io.pivotal.pal.tracker.TimeEntryRepository +import org.assertj.core.api.Assertions.assertThat +import org.junit.Before +import org.junit.Test +import org.springframework.boot.actuate.metrics.CounterService +import org.springframework.boot.actuate.metrics.GaugeService +import org.springframework.http.HttpStatus +import java.util.Arrays.asList + +class TimeEntryControllerTest { + + private lateinit var timeEntryRepository: TimeEntryRepository + private lateinit var controller: TimeEntryController + private val counterService: CounterService = mock() + private val gaugeService: GaugeService = mock() + + @Before + fun setUp() { + timeEntryRepository = mock() + controller = TimeEntryController(timeEntryRepository, counterService, gaugeService) + } + + @Test + fun testCreate() { + val expected = TimeEntry(1L, 123, 456, "today", 8) + doReturn(expected) + .`when`(timeEntryRepository) + .save(any()) + + val response = controller.create(TimeEntry(123, 456, "today", 8)) + + assertThat(response.statusCode).isEqualTo(HttpStatus.CREATED) + assertThat(response.body).isEqualTo(expected) + } + + @Test + fun testRead() { + val expected = TimeEntry(1L, 123, 456, "today", 8) + doReturn(expected) + .`when`(timeEntryRepository) + .findOne(1L) + + val response = controller.read(1L) + assertThat(response.statusCode).isEqualTo(HttpStatus.OK) + assertThat(response.body).isEqualTo(expected) + } + + @Test + fun testRead_NotFound() { + doReturn(null) + .`when`(timeEntryRepository) + .findOne(1L) + + val response = controller.read(1L) + assertThat(response.statusCode).isEqualTo(HttpStatus.NOT_FOUND) + } + + @Test + fun testList() { + val expected = asList( + TimeEntry(1L, 123, 456, "today", 8), + TimeEntry(2L, 789, 321, "yesterday", 4) + ) + doReturn(expected).`when`(timeEntryRepository).findAll() + + val response = controller.list() + assertThat(response.statusCode).isEqualTo(HttpStatus.OK) + assertThat(response.body).isEqualTo(expected) + } + + @Test + fun testUpdate() { + val expected = TimeEntry(1L, 987, 654, "yesterday", 4) + doReturn(expected) + .`when`(timeEntryRepository) + .save(any()) + + val response = controller.update(1L, expected) + assertThat(response.statusCode).isEqualTo(HttpStatus.OK) + assertThat(response.body).isEqualTo(expected) + } + + @Test + fun testUpdate_NotFound() { + doReturn(null) + .`when`(timeEntryRepository) + .save(any()) + + val response = controller.update(1L, TimeEntry(1L, 123, 456, "today", 8)) + assertThat(response.statusCode).isEqualTo(HttpStatus.NOT_FOUND) + } + + @Test + fun testDelete() { + val response = controller.delete(1L) + verify(timeEntryRepository).delete(1L) + assertThat(response.statusCode).isEqualTo(HttpStatus.NO_CONTENT) + } +} diff --git a/src/test/kotlin/test/pivotal/pal/tracker/WelcomeControllerTest.kt b/src/test/kotlin/test/pivotal/pal/tracker/WelcomeControllerTest.kt new file mode 100644 index 000000000..e2b22b2b5 --- /dev/null +++ b/src/test/kotlin/test/pivotal/pal/tracker/WelcomeControllerTest.kt @@ -0,0 +1,15 @@ +package test.pivotal.pal.tracker + +import io.pivotal.pal.tracker.WelcomeController +import org.assertj.core.api.Assertions.assertThat +import org.junit.Test + +class WelcomeControllerTest { + + @Test + fun itSaysHello() { + val controller = WelcomeController("A welcome message") + + assertThat(controller.sayHello()).isEqualTo("A welcome message") + } +} diff --git a/src/test/kotlin/test/pivotal/pal/trackerapi/HealthApiTest.kt b/src/test/kotlin/test/pivotal/pal/trackerapi/HealthApiTest.kt new file mode 100644 index 000000000..151c502a4 --- /dev/null +++ b/src/test/kotlin/test/pivotal/pal/trackerapi/HealthApiTest.kt @@ -0,0 +1,47 @@ +package test.pivotal.pal.trackerapi + +import com.jayway.jsonpath.JsonPath.parse +import io.pivotal.pal.tracker.PalTrackerApplication +import org.assertj.core.api.Assertions.assertThat +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.springframework.boot.context.embedded.LocalServerPort +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT +import org.springframework.boot.test.web.client.TestRestTemplate +import org.springframework.boot.web.client.RestTemplateBuilder +import org.springframework.http.HttpStatus +import org.springframework.test.context.junit4.SpringRunner + +@RunWith(SpringRunner::class) +@SpringBootTest(classes = arrayOf(PalTrackerApplication::class), webEnvironment = RANDOM_PORT) +class HealthApiTest { + + private lateinit var restTemplate: TestRestTemplate + + @LocalServerPort + private lateinit var port: String + + @Before + fun setUp() { + val builder = RestTemplateBuilder() + .rootUri("http://localhost:" + port) + .basicAuthorization("user", "password") + + restTemplate = TestRestTemplate(builder) + } + + @Test + fun healthTest() { + val response = this.restTemplate.getForEntity("/health", String::class.java) + + assertThat(response.statusCode).isEqualTo(HttpStatus.OK) + + val healthJson = parse(response.body) + + assertThat(healthJson.read("$.status", String::class.java)).isEqualTo("UP") + assertThat(healthJson.read("$.db.status", String::class.java)).isEqualTo("UP") + assertThat(healthJson.read("$.diskSpace.status", String::class.java)).isEqualTo("UP") + } +} diff --git a/src/test/kotlin/test/pivotal/pal/trackerapi/SecurityApiTest.kt b/src/test/kotlin/test/pivotal/pal/trackerapi/SecurityApiTest.kt new file mode 100644 index 000000000..44a49d029 --- /dev/null +++ b/src/test/kotlin/test/pivotal/pal/trackerapi/SecurityApiTest.kt @@ -0,0 +1,50 @@ +package test.pivotal.pal.trackerapi + +import io.pivotal.pal.tracker.PalTrackerApplication +import org.assertj.core.api.Assertions.assertThat +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.context.embedded.LocalServerPort +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT +import org.springframework.boot.test.web.client.TestRestTemplate +import org.springframework.boot.web.client.RestTemplateBuilder +import org.springframework.http.HttpStatus +import org.springframework.test.context.junit4.SpringRunner + +@RunWith(SpringRunner::class) +@SpringBootTest(classes = arrayOf(PalTrackerApplication::class), webEnvironment = RANDOM_PORT) +class SecurityApiTest { + + @LocalServerPort + private lateinit var port: String + private lateinit var authorizedRestTemplate: TestRestTemplate + + @Autowired + private lateinit var unAuthorizedRestTemplate: TestRestTemplate + + @Before + fun setUp() { + val builder = RestTemplateBuilder() + .rootUri("http://localhost:" + port) + .basicAuthorization("user", "password") + + authorizedRestTemplate = TestRestTemplate(builder) + } + + @Test + fun unauthorizedTest() { + val response = this.unAuthorizedRestTemplate.getForEntity("/", String::class.java) + + assertThat(response.statusCode).isEqualTo(HttpStatus.UNAUTHORIZED) + } + + @Test + fun authorizedTest() { + val response = this.authorizedRestTemplate.getForEntity("/", String::class.java) + + assertThat(response.statusCode).isEqualTo(HttpStatus.OK) + } +} diff --git a/src/test/kotlin/test/pivotal/pal/trackerapi/TimeEntryApiTest.kt b/src/test/kotlin/test/pivotal/pal/trackerapi/TimeEntryApiTest.kt new file mode 100644 index 000000000..4401ae0d7 --- /dev/null +++ b/src/test/kotlin/test/pivotal/pal/trackerapi/TimeEntryApiTest.kt @@ -0,0 +1,124 @@ +package test.pivotal.pal.trackerapi + +import com.jayway.jsonpath.JsonPath.parse +import com.mysql.cj.jdbc.MysqlDataSource +import io.pivotal.pal.tracker.PalTrackerApplication +import io.pivotal.pal.tracker.TimeEntry +import org.assertj.core.api.Assertions.assertThat +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.springframework.boot.context.embedded.LocalServerPort +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT +import org.springframework.boot.test.web.client.TestRestTemplate +import org.springframework.boot.web.client.RestTemplateBuilder +import org.springframework.http.HttpEntity +import org.springframework.http.HttpMethod +import org.springframework.http.HttpStatus +import org.springframework.jdbc.core.JdbcTemplate +import org.springframework.test.context.junit4.SpringRunner + +@RunWith(SpringRunner::class) +@SpringBootTest(classes = arrayOf(PalTrackerApplication::class), webEnvironment = RANDOM_PORT) +class TimeEntryApiTest { + + private lateinit var restTemplate: TestRestTemplate + + @LocalServerPort + private lateinit var port: String + private val timeEntry = TimeEntry(123, 456, "today", 8) + + @Before + fun setUp() { + val dataSource = MysqlDataSource() + dataSource.setUrl(System.getenv("SPRING_DATASOURCE_URL")) + + val jdbcTemplate = JdbcTemplate(dataSource) + jdbcTemplate.execute("TRUNCATE time_entries") + + val builder = RestTemplateBuilder() + .rootUri("http://localhost:" + port) + .basicAuthorization("user", "password") + + restTemplate = TestRestTemplate(builder) + } + + @Test + fun testCreate() { + val createResponse = restTemplate.postForEntity("/time-entries", timeEntry, String::class.java) + + assertThat(createResponse.statusCode).isEqualTo(HttpStatus.CREATED) + + val createJson = parse(createResponse.body) + assertThat(createJson.read("$.id", Long::class.java)).isGreaterThan(0) + assertThat(createJson.read("$.projectId", Long::class.java)).isEqualTo(123L) + assertThat(createJson.read("$.userId", Long::class.java)).isEqualTo(456L) + assertThat(createJson.read("$.date", String::class.java)).isEqualTo("today") + assertThat(createJson.read("$.hours", Long::class.java)).isEqualTo(8) + } + + @Test + fun testList() { + val id = createTimeEntry() + + val listResponse = restTemplate.getForEntity("/time-entries", String::class.java) + + assertThat(listResponse.statusCode).isEqualTo(HttpStatus.OK) + + val listJson = parse(listResponse.body) + + val timeEntries = listJson.read("$[*]", Collection::class.java) + assertThat(timeEntries.size).isEqualTo(1) + + val readId = listJson.read("$[0].id", Long::class.java) + assertThat(readId).isEqualTo(id) + } + + @Test + fun testRead() { + val id = createTimeEntry() + + val readResponse = this.restTemplate.getForEntity("/time-entries/" + id, String::class.java) + + assertThat(readResponse.statusCode).isEqualTo(HttpStatus.OK) + val readJson = parse(readResponse.body) + assertThat(readJson.read("$.id", Long::class.java)).isEqualTo(id) + assertThat(readJson.read("$.projectId", Long::class.java)).isEqualTo(123L) + assertThat(readJson.read("$.userId", Long::class.java)).isEqualTo(456L) + assertThat(readJson.read("$.date", String::class.java)).isEqualTo("today") + assertThat(readJson.read("$.hours", Long::class.java)).isEqualTo(8) + } + + @Test + fun testUpdate() { + val id = createTimeEntry() + val updatedTimeEntry = TimeEntry(2, 3, "tomorrow", 9) + + val updateResponse = restTemplate.exchange("/time-entries/" + id, HttpMethod.PUT, HttpEntity(updatedTimeEntry, null), String::class.java) + + assertThat(updateResponse.statusCode).isEqualTo(HttpStatus.OK) + + val updateJson = parse(updateResponse.body) + assertThat(updateJson.read("$.id", Long::class.java)).isEqualTo(id) + assertThat(updateJson.read("$.projectId", Long::class.java)).isEqualTo(2L) + assertThat(updateJson.read("$.userId", Long::class.java)).isEqualTo(3L) + assertThat(updateJson.read("$.date", String::class.java)).isEqualTo("tomorrow") + assertThat(updateJson.read("$.hours", Long::class.java)).isEqualTo(9) + } + + @Test + fun testDelete() { + val id = createTimeEntry() + + val deleteResponse = restTemplate.exchange("/time-entries/" + id, HttpMethod.DELETE, null, String::class.java) + + assertThat(deleteResponse.statusCode).isEqualTo(HttpStatus.NO_CONTENT) + + val deletedReadResponse = restTemplate.getForEntity("/time-entries/" + id, String::class.java) + assertThat(deletedReadResponse.statusCode).isEqualTo(HttpStatus.NOT_FOUND) + } + + private fun createTimeEntry() = + restTemplate.postForObject("/time-entries", timeEntry, TimeEntry::class.java).id!! +} diff --git a/src/test/kotlin/test/pivotal/pal/trackerapi/WelcomeApiTest.kt b/src/test/kotlin/test/pivotal/pal/trackerapi/WelcomeApiTest.kt new file mode 100644 index 000000000..dd6938e96 --- /dev/null +++ b/src/test/kotlin/test/pivotal/pal/trackerapi/WelcomeApiTest.kt @@ -0,0 +1,38 @@ +package test.pivotal.pal.trackerapi + +import io.pivotal.pal.tracker.PalTrackerApplication +import org.assertj.core.api.Assertions.assertThat +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.springframework.boot.context.embedded.LocalServerPort +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT +import org.springframework.boot.test.web.client.TestRestTemplate +import org.springframework.boot.web.client.RestTemplateBuilder +import org.springframework.test.context.junit4.SpringRunner + +@RunWith(SpringRunner::class) +@SpringBootTest(classes = arrayOf(PalTrackerApplication::class), webEnvironment = RANDOM_PORT) +class WelcomeApiTest { + + private lateinit var restTemplate: TestRestTemplate + + @LocalServerPort + private lateinit var port: String + + @Before + fun setUp() { + val builder = RestTemplateBuilder() + .rootUri("http://localhost:" + port) + .basicAuthorization("user", "password") + + restTemplate = TestRestTemplate(builder) + } + + @Test + fun exampleTest() { + val body = this.restTemplate.getForObject("/", String::class.java) + assertThat(body).isEqualTo("Hello from test") + } +}