├── .gitignore ├── .travis.yml ├── README.md ├── api-ft ├── README.md ├── build.gradle ├── gradle │ ├── dependencies.gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── src │ └── main │ ├── java │ ├── cucumber │ │ ├── api │ │ │ ├── cli │ │ │ │ └── springboot │ │ │ │ │ └── CucumberSpringBootCliRunner.java │ │ │ └── spring │ │ │ │ └── context │ │ │ │ ├── SpringApplicationContextBridge.java │ │ │ │ └── initializer │ │ │ │ └── CucumberApplicationContextInitializer.java │ │ └── runtime │ │ │ ├── ClasspathClassFinder.java │ │ │ └── io │ │ │ ├── ClasspathResource.java │ │ │ └── ClasspathResourceLoader.java │ └── io │ │ └── smartcat │ │ └── spring │ │ └── cassandra │ │ └── showcase │ │ └── ft │ │ ├── Application.java │ │ ├── account │ │ ├── Account.java │ │ ├── AccountGenerators.java │ │ └── SignUpViaEmailRequestBuilder.java │ │ ├── common │ │ ├── HttpHeader.java │ │ ├── RequestBuilder.java │ │ ├── StringGenerators.java │ │ ├── UrlGenerators.java │ │ └── serialization │ │ │ └── ObjectMapperProvider.java │ │ ├── cucumber │ │ └── stepdefinitions │ │ │ ├── AccountStepDefinitions.java │ │ │ └── SmokeStepDefinitions.java │ │ ├── error │ │ └── RestError.java │ │ ├── health │ │ ├── Health.java │ │ └── HealthRequestBuilder.java │ │ └── util │ │ ├── RandomGenerators.java │ │ ├── cli │ │ └── CucumberArgumentsBuilder.java │ │ └── config │ │ ├── ApplicationConfig.java │ │ ├── cucumber │ │ ├── CucumberConfig.java │ │ └── CucumberPlugins.java │ │ └── server │ │ └── ServerConfig.java │ └── resources │ ├── application.yml │ └── features │ ├── account.feature │ └── smoke.feature ├── api ├── build.gradle ├── gradle │ ├── dependencies.gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── src │ ├── main │ ├── java │ │ └── io │ │ │ └── smartcat │ │ │ └── spring │ │ │ └── cassandra │ │ │ └── showcase │ │ │ ├── Application.java │ │ │ ├── adapter │ │ │ ├── cassandra │ │ │ │ ├── CassandraConfiguration.java │ │ │ │ └── migrations │ │ │ │ │ ├── data │ │ │ │ │ └── AddAdminAccountDataMigration.java │ │ │ │ │ └── schema │ │ │ │ │ ├── AddAccountByExternalSourceTableMigration.java │ │ │ │ │ └── InitializeSchema.java │ │ │ ├── http │ │ │ │ └── account │ │ │ │ │ ├── AccountHttpAdapter.java │ │ │ │ │ └── dto │ │ │ │ │ ├── AccountCreateRequest.java │ │ │ │ │ └── AccountDto.java │ │ │ └── persistence │ │ │ │ └── cassandra │ │ │ │ └── account │ │ │ │ ├── AccountByEmail.java │ │ │ │ ├── AccountByEmailAccessor.java │ │ │ │ ├── AccountByExternalSource.java │ │ │ │ ├── AccountByExternalSourceAccessor.java │ │ │ │ ├── AccountRepositoryCassandra.java │ │ │ │ └── AccountSchemaCreator.java │ │ │ └── domain │ │ │ └── account │ │ │ ├── Account.java │ │ │ ├── AccountRepository.java │ │ │ ├── AccountRole.java │ │ │ ├── AccountService.java │ │ │ ├── EmailAddress.java │ │ │ └── ExternalSource.java │ └── resources │ │ ├── application-test.yml │ │ └── application.yml │ └── test │ ├── java │ └── io │ │ └── smartcat │ │ └── spring │ │ └── cassandra │ │ └── showcase │ │ ├── adapter │ │ └── persistence │ │ │ └── cassandra │ │ │ └── account │ │ │ └── AccountRepositoryCassandraTest.java │ │ └── test │ │ ├── cassandra │ │ ├── CassandraTestExecutionListener.java │ │ ├── TestApplicationContext.java │ │ ├── TestCassandraConfig.java │ │ └── stub │ │ │ ├── ResultSetFutureStub.java │ │ │ └── SessionProxy.java │ │ └── generators │ │ ├── AccountGenerators.java │ │ └── EmailAddressGenerators.java │ └── resources │ ├── cassandra-topology.properties │ ├── cassandra-unit.yaml │ ├── db.cql │ └── logback-test.xml ├── common.gradle └── common └── gradle └── common.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle/ 2 | build/ 3 | target/ 4 | 5 | ## Intellij 6 | **/.idea 7 | *.iml 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | sudo: true 3 | jdk: 4 | - oraclejdk8 5 | notifications: 6 | email: false 7 | slack: 8 | secure: IoYUhvahhyfn0n5xjiA5DkLoTeUeHlyGhm9IcQdRqAkP0+Ecp7YZoTxFt7Lf21Kyvlk3tvoONo2QwvcZZzh6I7dBCC4IIoz4DJ6TXtQa+HLFseAoUn8zzPZLjTWOPmc4QCOHPpzcXeXzBrp5txHYUYIzmQGi9Fgh4WFJnJ2jCk6XJ1GGw3cevfIDEnWafSSOJmOyFRZzFoeNWir0XjX+APW49G554Og8RnM3usEQJQ8xUH9Ymyg6l2uUXu1k0X617fdF8LDOH9x6jNhjRH8uB8XVluymEndu4uXPQtvtivtYCWMGyQVrTcTmCgXlN74t1bi59jyG+HGN1XJAkEF2rz6s/QdIw75J0l/4azVfbP3bER8mUHrPHaHBgzk49mYaZinRGn+/fM/6rKz/SYh0FL+jWuVVUOhQD7hONPqMhFGGugByoo4/0JlaDV6MeehuaIn7Bfbzqf4105xv1oz7KrhUBU9nyoyLmKI6kkQUEEUsaapYm+hiP2cvhpms6Biiaw4R3tGiba9uXQG+rI65lMa+tIrHVAWTF6Hk28sJKiCRLaKOL77kW60Mj2bGIWNrl9IKlFrUvay4sP5W8FVguFDnlnuveGg05rHUQ/nz8+pQ6uA+HrqFvntbmaj+eHQ1hXqsuwMGjWNxBolhah1I25FFbTaLZDlcnkTyQ4KQNeM= 9 | before_script: cd api 10 | script: gradle build 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # spring-cassandra-showcase-application 2 | [![Build Status](https://travis-ci.org/smartcat-labs/spring-cassandra-showcase-application.svg?branch=master)](https://travis-ci.org/smartcat-labs/spring-cassandra-showcase-application) 3 | 4 | Showcase application for Cassandra database usage with Spring framework and DataStax Java driver. 5 | 6 | # General overview 7 | 8 | This project is showcase of Cassandra usage on Spring Boot projects. Currently This project is showing setup of Embedded Cassandra for testing of Spring Boot application on one side, and it is showing usage of our [Cassandra Migration Tool Java](https://github.com/smartcat-labs/cassandra-migration-tool-java) for DATA and SCHEMA migrations on the other side. [CassandraConfiguration](https://github.com/smartcat-labs/spring-cassandra-showcase-application/blob/master/api/src/main/java/io/smartcat/spring/cassandra/showcase/adapter/cassandra/CassandraConfiguration.java) holds all necessary configuration for cluster and session. It pulls values from [application.yml](https://github.com/smartcat-labs/spring-cassandra-showcase-application/blob/master/api/src/main/resources/application.yml). Configuration goes through couple of steps: 9 | 10 | * Create keyspace if it does not exist 11 | * Use keyspace 12 | * Execute all SCHEMA migrations 13 | 14 | Showcase is done in lightweight *DDD* fashion, with [hexagonal architecture](http://alistair.cockburn.us/Hexagonal+architecture) in mind. *adapter* package holds adapters to outside world (Cassandra configuration and implementation of Repository classes). *domain* package holds domain logic, Value and Entity objects and Repository interface. 15 | 16 | #SCHEMA migrations 17 | 18 | Execution of SCHEMA migrations are part of connection to cluster. Right after Session is created and keyspace is selected all unexecuted SCHEMA migrations are executed. More details on *Cassandra Migration Tool* could be found on this [link](https://github.com/smartcat-labs/cassandra-migration-tool-java). Important part for this showcase is that SCHEMA migrations are part of server startup and they are blocking since Application would not start if mapped objects are different then SCHEMA for connected cluster. 19 | 20 | #DATA migrations 21 | 22 | Execution of DATA migrations is done after application is fully started. This kind of setup is meant for ETL like operations on data which can be applied async and this data will be in sync eventually. This can be seen in [Application](https://github.com/smartcat-labs/spring-cassandra-showcase-application/blob/master/api/src/main/java/io/smartcat/spring/cassandra/showcase/Application.java) class where migrateData is executed after application is fully started. 23 | 24 | #Embedded Cassandra Setup 25 | 26 | First step of adding Embedded Cassandra to project is adding listener, configuring Embedded Cassandra and connecting it alltogether. Second problem we faced is testing asyn execution of statements. These problems are tackled in [test Cassandra package](https://github.com/smartcat-labs/spring-cassandra-showcase-application/tree/master/api/src/test/java/io/smartcat/spring/cassandra/showcase/test/cassandra) and explained in details on our [Setting up Embedded Cassandra on Spring projects](https://www.smartcat.io/blog/setting-up-embedded-cassandra-on-spring-project/) blog post. 27 | 28 | In a nutshell Test Listener implementation is used to hook creating and truncating tables for testing needs. We overrode Session to fake async execution as sync, which enable testing without waiting on results to be propagated in datbase. 29 | -------------------------------------------------------------------------------- /api-ft/README.md: -------------------------------------------------------------------------------- 1 | # spring-cassandra-showcase-application 2 | Placeholder folder for functional test on API level 3 | -------------------------------------------------------------------------------- /api-ft/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | apply from: '../common/gradle/common.gradle' 3 | repositories { 4 | maven { url 'http://repo.spring.io/libs-release' } 5 | mavenCentral() 6 | 7 | dependencies { 8 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${versions.springBoot}") 9 | } 10 | } 11 | } 12 | 13 | apply from: 'gradle/dependencies.gradle' 14 | apply plugin: 'spring-boot' 15 | apply plugin: 'eclipse' 16 | 17 | jar.baseName = 'spring-cassandra-showcase-ft' 18 | version = '1.0' 19 | mainClassName = 'io.smartcat.spring.cassandra.showcase.ft.Application' 20 | -------------------------------------------------------------------------------- /api-ft/gradle/dependencies.gradle: -------------------------------------------------------------------------------- 1 | repositories { 2 | maven { url 'http://repo.spring.io/libs-release' } 3 | mavenCentral() 4 | } 5 | 6 | dependencies { 7 | compile("org.springframework.boot:spring-boot-starter:${versions.springBoot}") 8 | compile('org.springframework:spring-test') 9 | 10 | compile("com.google.guava:guava:${versions.guava}") 11 | compile("info.cukes:cucumber-java:${versions.cucumber}") 12 | compile("info.cukes:cucumber-spring:${versions.cucumber}") 13 | compile('org.glassfish.jersey.core:jersey-client:2.17') 14 | compile("org.assertj:assertj-core:${versions.assertJ}") 15 | 16 | compile("com.fasterxml.jackson.core:jackson-core:${versions.jackson}") 17 | compile("com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:${versions.jackson}") 18 | compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:${versions.jackson}") 19 | compile("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:${versions.jackson}") 20 | 21 | // if we need to mock external API calls 22 | compile('com.github.tomakehurst:wiremock:1.56') 23 | 24 | compile('ch.qos.logback:logback-core:1.1.3') 25 | compile('org.slf4j:jul-to-slf4j:1.7.12') 26 | compile('org.reflections:reflections:0.9.10') 27 | 28 | compile('org.jsoup:jsoup:1.7.2') 29 | } 30 | -------------------------------------------------------------------------------- /api-ft/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smartcat-labs/spring-cassandra-showcase-application/090fa7ad8cba4f1be44c1ffd846a7c4c5ea17caf/api-ft/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /api-ft/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun May 10 20:25:01 BST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip 7 | -------------------------------------------------------------------------------- /api-ft/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /api-ft/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /api-ft/src/main/java/cucumber/api/cli/springboot/CucumberSpringBootCliRunner.java: -------------------------------------------------------------------------------- 1 | package cucumber.api.cli.springboot; 2 | 3 | import cucumber.runtime.ClasspathClassFinder; 4 | import cucumber.runtime.RuntimeOptions; 5 | import cucumber.runtime.io.ClasspathResourceLoader; 6 | import java.io.IOException; 7 | import java.util.List; 8 | import cucumber.runtime.Runtime; 9 | 10 | public class CucumberSpringBootCliRunner { 11 | 12 | private final ClassLoader classLoader; 13 | private final List args; 14 | private int exitCode = 0; 15 | 16 | public CucumberSpringBootCliRunner(ClassLoader classLoader, List args) { 17 | this.classLoader = classLoader; 18 | this.args = args; 19 | } 20 | 21 | public int getExitCode() { 22 | return exitCode; 23 | } 24 | 25 | public void run() throws IOException { 26 | final Runtime runtime = new Runtime(new ClasspathResourceLoader(classLoader), 27 | new ClasspathClassFinder(classLoader), classLoader, new RuntimeOptions(args)); 28 | runtime.run(); 29 | exitCode = runtime.exitStatus(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /api-ft/src/main/java/cucumber/api/spring/context/SpringApplicationContextBridge.java: -------------------------------------------------------------------------------- 1 | package cucumber.api.spring.context; 2 | 3 | import org.springframework.beans.BeansException; 4 | import org.springframework.context.ApplicationContext; 5 | import org.springframework.context.ApplicationContextAware; 6 | import org.springframework.stereotype.Component; 7 | 8 | @Component 9 | public class SpringApplicationContextBridge implements ApplicationContextAware { 10 | 11 | private static ApplicationContext context; 12 | 13 | @Override 14 | public void setApplicationContext(ApplicationContext context) throws BeansException { 15 | SpringApplicationContextBridge.context = context; 16 | } 17 | 18 | public static ApplicationContext getApplicationContext() { 19 | return context; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /api-ft/src/main/java/cucumber/api/spring/context/initializer/CucumberApplicationContextInitializer.java: -------------------------------------------------------------------------------- 1 | package cucumber.api.spring.context.initializer; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.ApplicationContext; 5 | import org.springframework.context.ApplicationContextInitializer; 6 | import org.springframework.context.ConfigurableApplicationContext; 7 | 8 | import cucumber.api.spring.context.SpringApplicationContextBridge; 9 | 10 | public class CucumberApplicationContextInitializer implements 11 | ApplicationContextInitializer { 12 | 13 | @Autowired 14 | ApplicationContext applicationContext; 15 | 16 | @Override 17 | public void initialize(final ConfigurableApplicationContext applicationContext) { 18 | applicationContext.setParent(SpringApplicationContextBridge.getApplicationContext()); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /api-ft/src/main/java/cucumber/runtime/ClasspathClassFinder.java: -------------------------------------------------------------------------------- 1 | package cucumber.runtime; 2 | 3 | import java.util.Collection; 4 | import org.reflections.Reflections; 5 | 6 | public class ClasspathClassFinder implements ClassFinder { 7 | 8 | private final ClassLoader classLoader; 9 | 10 | public ClasspathClassFinder(ClassLoader classLoader) { 11 | this.classLoader = classLoader; 12 | } 13 | 14 | @Override 15 | public Collection> getDescendants(Class parentType, 16 | String packageName) { 17 | return new Reflections(packageName).getSubTypesOf(parentType); 18 | } 19 | 20 | @SuppressWarnings("unchecked") 21 | @Override 22 | public Class loadClass(String className) throws ClassNotFoundException { 23 | return (Class) classLoader.loadClass(className); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /api-ft/src/main/java/cucumber/runtime/io/ClasspathResource.java: -------------------------------------------------------------------------------- 1 | package cucumber.runtime.io; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | 6 | public class ClasspathResource implements Resource { 7 | private final String path; 8 | private final String className; 9 | private final ClassLoader classLoader; 10 | 11 | public ClasspathResource(String className, String suffix, ClassLoader classLoader) { 12 | this.path = className + suffix; 13 | this.className = className; 14 | this.classLoader = classLoader; 15 | } 16 | 17 | @Override 18 | public String getPath() { 19 | return path; 20 | } 21 | 22 | @Override 23 | public String getAbsolutePath() { 24 | return path; 25 | } 26 | 27 | @Override 28 | public InputStream getInputStream() throws IOException { 29 | return classLoader.getResourceAsStream(className); 30 | } 31 | 32 | @Override 33 | public String getClassName(String extension) { 34 | String path = getPath(); 35 | return path.substring(0, path.length() - extension.length()).replace('/', '.'); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /api-ft/src/main/java/cucumber/runtime/io/ClasspathResourceLoader.java: -------------------------------------------------------------------------------- 1 | package cucumber.runtime.io; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.regex.Pattern; 6 | import org.reflections.Reflections; 7 | import org.reflections.scanners.ResourcesScanner; 8 | import org.reflections.scanners.SubTypesScanner; 9 | 10 | public class ClasspathResourceLoader implements ResourceLoader { 11 | 12 | public static final String CLASSPATH_SCHEME = "classpath:"; 13 | 14 | private final ClassLoader classLoader; 15 | 16 | public ClasspathResourceLoader(ClassLoader classLoader) { 17 | this.classLoader = classLoader; 18 | } 19 | 20 | @Override 21 | public Iterable resources(String path, String suffix) { 22 | String packageName = stripClasspathPrefix(path).replace("/", "."); 23 | if (suffix.equals(".class")) { 24 | return loadClasses(suffix, packageName); 25 | } else { 26 | return loadResources(suffix, packageName); 27 | } 28 | } 29 | 30 | private Iterable loadClasses(String suffix, String packageName) { 31 | List resources = new ArrayList<>(); 32 | Reflections reflections = new Reflections(packageName, new SubTypesScanner(false)); 33 | reflections.getSubTypesOf(Object.class).stream().map( 34 | (clazz) -> clazz.getName()).forEach((name) -> { 35 | resources.add(new ClasspathResource(name, suffix, classLoader)); 36 | }); 37 | return resources; 38 | } 39 | 40 | private Iterable loadResources(String suffix, String packageName) { 41 | List resources = new ArrayList<>(); 42 | Reflections reflections = new Reflections(packageName, new ResourcesScanner()); 43 | reflections.getResources(Pattern.compile(String.format( 44 | ".*?\\%s.*", suffix))).stream().forEach((resourceLocation) -> { 45 | resources.add(new ClasspathResource(resourceLocation, suffix, classLoader)); 46 | }); 47 | return resources; 48 | } 49 | 50 | private static String stripClasspathPrefix(String path) { 51 | return path.substring(CLASSPATH_SCHEME.length()); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /api-ft/src/main/java/io/smartcat/spring/cassandra/showcase/ft/Application.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.ft; 2 | 3 | import java.util.List; 4 | 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.boot.CommandLineRunner; 9 | import org.springframework.boot.SpringApplication; 10 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 11 | import org.springframework.context.annotation.ComponentScan; 12 | import org.springframework.context.annotation.Configuration; 13 | 14 | import cucumber.api.cli.springboot.CucumberSpringBootCliRunner; 15 | import io.smartcat.spring.cassandra.showcase.ft.util.cli.CucumberArgumentsBuilder; 16 | import io.smartcat.spring.cassandra.showcase.ft.util.config.ApplicationConfig; 17 | 18 | @Configuration 19 | @EnableAutoConfiguration 20 | @ComponentScan(basePackages = { 21 | "io.smartcat.spring.cassandra.showcase.ft", 22 | "cucumber.api.spring.context" 23 | }) 24 | public class Application implements CommandLineRunner { 25 | 26 | private static int exitCode = 0; 27 | 28 | private static final Logger log = LoggerFactory.getLogger(Application.class); 29 | 30 | public static final int ILLEGAL_ARGUMENTS_EXIT_CODE = 127; 31 | 32 | @Autowired 33 | private ApplicationConfig applicationConfig; 34 | 35 | public static void main(String[] args) throws Throwable { 36 | SpringApplication.run(Application.class, args); 37 | System.exit(exitCode); 38 | } 39 | 40 | @Override 41 | public void run(String... args) throws Exception { 42 | int exitCode = 0; 43 | try { 44 | final List argumentsBuilder = new CucumberArgumentsBuilder() 45 | .build(applicationConfig.getCucumberConfig(), args); 46 | final CucumberSpringBootCliRunner runner = new CucumberSpringBootCliRunner( 47 | getClass().getClassLoader(), argumentsBuilder); 48 | runner.run(); 49 | exitCode = runner.getExitCode(); 50 | } catch (IllegalArgumentException e) { 51 | exitCode = ILLEGAL_ARGUMENTS_EXIT_CODE; 52 | log.error(e.getMessage()); 53 | log.error("Error occured, stopping application"); 54 | } 55 | Application.exitCode = exitCode; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /api-ft/src/main/java/io/smartcat/spring/cassandra/showcase/ft/account/Account.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.ft.account; 2 | 3 | import java.time.Instant; 4 | 5 | import javax.ws.rs.core.Response; 6 | 7 | import com.fasterxml.jackson.annotation.JsonCreator; 8 | import com.fasterxml.jackson.annotation.JsonProperty; 9 | 10 | public class Account { 11 | 12 | private final String email; 13 | private final String firstName; 14 | private final String lastName; 15 | private final Instant registeredAt; 16 | private final String profileImageUrl; 17 | 18 | @JsonCreator 19 | public Account( 20 | @JsonProperty("email") final String email, 21 | @JsonProperty("firstName") final String firstName, 22 | @JsonProperty("lastName") final String lastName, 23 | @JsonProperty("registeredAt") final Instant registeredAt, 24 | @JsonProperty("profileImageUrl") final String profileImageUrl 25 | ) { 26 | this.email = email; 27 | this.firstName = firstName; 28 | this.lastName = lastName; 29 | this.registeredAt = registeredAt; 30 | this.profileImageUrl = profileImageUrl; 31 | } 32 | 33 | public String getEmail() { 34 | return email; 35 | } 36 | 37 | public String getFirstName() { 38 | return firstName; 39 | } 40 | 41 | public String getLastName() { 42 | return lastName; 43 | } 44 | 45 | public Instant getRegisteredAt() { 46 | return registeredAt; 47 | } 48 | 49 | public String getProfileImage() { 50 | return profileImageUrl; 51 | } 52 | 53 | public static Account fromResponse(final Response response) { 54 | response.bufferEntity(); 55 | return response.readEntity(Account.class); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /api-ft/src/main/java/io/smartcat/spring/cassandra/showcase/ft/account/AccountGenerators.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.ft.account; 2 | 3 | import java.util.Random; 4 | 5 | import io.smartcat.spring.cassandra.showcase.ft.common.UrlGenerators; 6 | 7 | public class AccountGenerators { 8 | 9 | public static String givenAnyLastName() { 10 | return "Doe"; 11 | } 12 | 13 | public static String givenAnyFirstName() { 14 | return "Johnatan"; 15 | } 16 | 17 | public static String givenAnyPassword() { 18 | return "mypassword123+*"; 19 | } 20 | 21 | public static String givenAnyEmail() { 22 | return givenUniqueEmail(); 23 | } 24 | 25 | public static String givenUniqueEmail() { 26 | return String.format("someone%d@example.com", new Random().nextInt()); 27 | } 28 | 29 | public static String givenProfileImageUrl() { 30 | return UrlGenerators.givenAnyUri().toString(); 31 | } 32 | } -------------------------------------------------------------------------------- /api-ft/src/main/java/io/smartcat/spring/cassandra/showcase/ft/account/SignUpViaEmailRequestBuilder.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.ft.account; 2 | 3 | import static io.smartcat.spring.cassandra.showcase.ft.account.AccountGenerators.givenAnyFirstName; 4 | import static io.smartcat.spring.cassandra.showcase.ft.account.AccountGenerators.givenAnyLastName; 5 | import static io.smartcat.spring.cassandra.showcase.ft.account.AccountGenerators.givenAnyPassword; 6 | import static io.smartcat.spring.cassandra.showcase.ft.account.AccountGenerators.givenUniqueEmail; 7 | 8 | import javax.ws.rs.client.Invocation; 9 | import javax.ws.rs.core.MediaType; 10 | 11 | import io.smartcat.spring.cassandra.showcase.ft.common.RequestBuilder; 12 | 13 | public class SignUpViaEmailRequestBuilder extends RequestBuilder { 14 | 15 | public SignUpViaEmailRequestBuilder() { 16 | super(); 17 | withHttpMethod(HttpMethod.POST); 18 | withEndPoint("account"); 19 | withContentType(MediaType.APPLICATION_JSON); 20 | 21 | withFirstName(givenAnyFirstName()); 22 | withLastName(givenAnyLastName()); 23 | withEmail(givenUniqueEmail()); 24 | withPassword(givenAnyPassword()); 25 | withProfileImageUrl(AccountGenerators.givenProfileImageUrl()); 26 | } 27 | 28 | public SignUpViaEmailRequestBuilder(final String email) { 29 | this(); 30 | withEmail(email); 31 | } 32 | 33 | public SignUpViaEmailRequestBuilder withEmail(final String email) { 34 | withBodyParameter(BodyParameters.EMAIL, email); 35 | return this; 36 | } 37 | 38 | public SignUpViaEmailRequestBuilder withFirstName(final String firstName) { 39 | withBodyParameter(BodyParameters.FIRST_NAME, firstName); 40 | return this; 41 | } 42 | 43 | public SignUpViaEmailRequestBuilder withLastName(final String lastName) { 44 | withBodyParameter(BodyParameters.LAST_NAME, lastName); 45 | return this; 46 | } 47 | 48 | public SignUpViaEmailRequestBuilder withPassword(final String password) { 49 | withBodyParameter(BodyParameters.PASSWORD, password); 50 | return this; 51 | } 52 | 53 | public SignUpViaEmailRequestBuilder withProfileImageUrl(final String profileImageUrl) { 54 | withBodyParameter(BodyParameters.PROFILE_IMAGE_URL, profileImageUrl); 55 | return this; 56 | } 57 | 58 | public static Invocation aSignUpViaEmailRequest(final String emailAddress) { 59 | return new SignUpViaEmailRequestBuilder(emailAddress).build(); 60 | } 61 | 62 | @Override 63 | protected SignUpViaEmailRequestBuilder getThis() { 64 | return this; 65 | } 66 | 67 | private class BodyParameters { 68 | public static final String EMAIL = "email"; 69 | public static final String FIRST_NAME = "firstName"; 70 | public static final String LAST_NAME = "lastName"; 71 | public static final String PASSWORD = "password"; 72 | public static final String PROFILE_IMAGE_URL = "profileImageUrl"; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /api-ft/src/main/java/io/smartcat/spring/cassandra/showcase/ft/common/HttpHeader.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.ft.common; 2 | 3 | public class HttpHeader { 4 | 5 | public final static String CONTENT_TYPE = "Content-Type"; 6 | public final static String ACCEPT = "Accept"; 7 | } 8 | -------------------------------------------------------------------------------- /api-ft/src/main/java/io/smartcat/spring/cassandra/showcase/ft/common/RequestBuilder.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.ft.common; 2 | 3 | import java.security.GeneralSecurityException; 4 | import java.security.SecureRandom; 5 | import java.security.cert.CertificateException; 6 | import java.security.cert.X509Certificate; 7 | import java.util.HashMap; 8 | import java.util.Map; 9 | 10 | import javax.net.ssl.SSLContext; 11 | import javax.net.ssl.TrustManager; 12 | import javax.net.ssl.X509TrustManager; 13 | import javax.ws.rs.client.Client; 14 | import javax.ws.rs.client.ClientBuilder; 15 | import javax.ws.rs.client.Entity; 16 | import javax.ws.rs.client.Invocation; 17 | import javax.ws.rs.client.WebTarget; 18 | import javax.ws.rs.core.MediaType; 19 | 20 | import org.glassfish.jersey.client.ClientConfig; 21 | import org.glassfish.jersey.filter.LoggingFilter; 22 | import org.slf4j.Logger; 23 | import org.slf4j.LoggerFactory; 24 | 25 | import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider; 26 | 27 | import cucumber.api.spring.context.SpringApplicationContextBridge; 28 | import io.smartcat.spring.cassandra.showcase.ft.common.serialization.ObjectMapperProvider; 29 | import io.smartcat.spring.cassandra.showcase.ft.util.config.server.ServerConfig; 30 | 31 | abstract public class RequestBuilder { 32 | 33 | private final static Logger LOGGER = LoggerFactory.getLogger(RequestBuilder.class); 34 | 35 | private String entryPoint; 36 | private Client client; 37 | private final Map headers; 38 | private final Map bodyParameters; 39 | private final Map queryParameters; 40 | private final ServerConfig serverConfig = SpringApplicationContextBridge.getApplicationContext() 41 | .getBean(ServerConfig.class); 42 | private HttpMethod httpMethod; 43 | private String endPoint; 44 | private String bodyText; 45 | 46 | public RequestBuilder() { 47 | try { 48 | this.client = buildClient(); 49 | } catch (final Exception e) { 50 | LOGGER.error("Error creating HTTP client:" + e.getMessage()); 51 | } 52 | this.entryPoint = serverConfig.getApiEntryPoint(); 53 | this.headers = new HashMap<>(); 54 | this.bodyParameters = new HashMap<>(); 55 | this.bodyText = ""; 56 | this.queryParameters = new HashMap<>(); 57 | 58 | withHttpMethod(HttpMethod.GET); 59 | } 60 | 61 | public T withHttpMethod(final HttpMethod httpMethod) { 62 | this.httpMethod = httpMethod; 63 | 64 | return getThis(); 65 | } 66 | 67 | public T withHttpHeader(final String name, final String value) { 68 | headers.put(name, value); 69 | 70 | return getThis(); 71 | } 72 | 73 | public T withContentType(final String contentType) { 74 | withHttpHeader(HttpHeader.CONTENT_TYPE, contentType); 75 | 76 | return getThis(); 77 | } 78 | 79 | public T withEntryPoint(final String entryPoint) { 80 | this.entryPoint = entryPoint; 81 | 82 | return getThis(); 83 | } 84 | 85 | public T withEndPoint(final String endPoint) { 86 | this.endPoint = endPoint; 87 | 88 | return getThis(); 89 | } 90 | 91 | public Invocation build() { 92 | final WebTarget target = createTarget(); 93 | 94 | final Invocation.Builder requestBuilder = target.request(); 95 | addHeadersTo(requestBuilder); 96 | 97 | return bodyParameters.isEmpty() 98 | ? (bodyText.isEmpty() 99 | ? buildWithoutBody(requestBuilder) 100 | : buildWithTextBody(requestBuilder)) 101 | : buildWithBody(requestBuilder); 102 | } 103 | 104 | protected T withBodyParameter(final String name, final Object value) { 105 | bodyParameters.put(name, value); 106 | 107 | return getThis(); 108 | } 109 | 110 | protected T withBodyText(final String bodyText) { 111 | this.bodyText = bodyText; 112 | 113 | return getThis(); 114 | } 115 | 116 | protected T withQueryParameter(final String name, final Object value) { 117 | queryParameters.put(name, value); 118 | 119 | return getThis(); 120 | } 121 | 122 | protected abstract T getThis(); 123 | 124 | protected enum HttpMethod { GET, POST, PUT, DELETE } 125 | 126 | private Client buildClient() throws GeneralSecurityException { 127 | final ClientConfig clientConfig = new ClientConfig(); 128 | clientConfig.register(JacksonJsonProvider.class); 129 | clientConfig.register(new ObjectMapperProvider()); 130 | clientConfig.register( 131 | new LoggingFilter(java.util.logging.Logger.getLogger("jersey-client"), true)); 132 | 133 | final SSLContext sslcontext = configureSslContext(); 134 | return ClientBuilder.newBuilder().sslContext(sslcontext).hostnameVerifier((s1, s2) -> true) 135 | .withConfig(clientConfig).build(); 136 | } 137 | 138 | private SSLContext configureSslContext() throws GeneralSecurityException { 139 | final SSLContext sslcontext = SSLContext.getInstance("TLS"); 140 | sslcontext.init(null, trustAllManager, new SecureRandom()); 141 | return sslcontext; 142 | } 143 | 144 | TrustManager[] trustAllManager = new TrustManager[] { new X509TrustManager() { 145 | @Override 146 | public X509Certificate[] getAcceptedIssuers() { 147 | return null; 148 | } 149 | 150 | @Override 151 | public void checkServerTrusted(final X509Certificate[] chain, final String authType) 152 | throws CertificateException { 153 | } 154 | 155 | @Override 156 | public void checkClientTrusted(final X509Certificate[] chain, final String authType) 157 | throws CertificateException { 158 | } 159 | } }; 160 | 161 | private void addHeadersTo(final Invocation.Builder requestBuilder) { 162 | headers.forEach(requestBuilder::header); 163 | } 164 | 165 | private WebTarget createTarget() { 166 | WebTarget target = client.target(String.format("%s/%s", entryPoint, endPoint)); 167 | 168 | for (final Map.Entry param : queryParameters.entrySet()) { 169 | target = target.queryParam(param.getKey(), param.getValue()); 170 | } 171 | 172 | return target; 173 | } 174 | 175 | private Invocation buildWithBody(final Invocation.Builder requestBuilder) { 176 | return requestBuilder.build( 177 | httpMethod.toString(), Entity.entity(bodyParameters, MediaType.APPLICATION_JSON)); 178 | } 179 | 180 | private Invocation buildWithTextBody(final Invocation.Builder requestBuilder) { 181 | return requestBuilder.build(httpMethod.toString(), 182 | Entity.entity(bodyText, MediaType.TEXT_PLAIN)); 183 | } 184 | 185 | private Invocation buildWithoutBody(final Invocation.Builder requestBuilder) { 186 | return requestBuilder.build(httpMethod.toString()); 187 | } 188 | } 189 | -------------------------------------------------------------------------------- /api-ft/src/main/java/io/smartcat/spring/cassandra/showcase/ft/common/StringGenerators.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.ft.common; 2 | 3 | import java.util.UUID; 4 | 5 | public class StringGenerators { 6 | 7 | public static String givenAnyString() { 8 | return "Some-string-123-+_!šđž"; 9 | } 10 | 11 | public static String givenAlphanumericString() { 12 | return "Somestring123"; 13 | } 14 | 15 | public static String givenUniqueString() { 16 | return UUID.randomUUID().toString(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /api-ft/src/main/java/io/smartcat/spring/cassandra/showcase/ft/common/UrlGenerators.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.ft.common; 2 | 3 | import java.net.URI; 4 | import java.net.URISyntaxException; 5 | 6 | public class UrlGenerators { 7 | 8 | public static URI givenAnyUri() { 9 | try { 10 | return new URI("http://example.com/some/uri"); 11 | } catch (URISyntaxException e) { 12 | throw new RuntimeException(e); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /api-ft/src/main/java/io/smartcat/spring/cassandra/showcase/ft/common/serialization/ObjectMapperProvider.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.ft.common.serialization; 2 | 3 | import javax.ws.rs.ext.ContextResolver; 4 | 5 | import com.fasterxml.jackson.databind.DeserializationFeature; 6 | import com.fasterxml.jackson.databind.ObjectMapper; 7 | import com.fasterxml.jackson.databind.SerializationFeature; 8 | import com.fasterxml.jackson.datatype.jsr310.JSR310Module; 9 | 10 | public class ObjectMapperProvider implements ContextResolver { 11 | 12 | @Override 13 | public ObjectMapper getContext(final Class type) { 14 | final ObjectMapper objectMapper = new ObjectMapper(); 15 | 16 | objectMapper.registerModule(new JSR310Module()); 17 | objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); 18 | 19 | objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); 20 | 21 | return objectMapper; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /api-ft/src/main/java/io/smartcat/spring/cassandra/showcase/ft/cucumber/stepdefinitions/AccountStepDefinitions.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.ft.cucumber.stepdefinitions; 2 | 3 | import static io.smartcat.spring.cassandra.showcase.ft.account.AccountGenerators.givenUniqueEmail; 4 | import static io.smartcat.spring.cassandra.showcase.ft.account.SignUpViaEmailRequestBuilder.aSignUpViaEmailRequest; 5 | import static org.assertj.core.api.Assertions.assertThat; 6 | 7 | import javax.ws.rs.core.Response; 8 | 9 | import org.springframework.test.context.ContextConfiguration; 10 | 11 | import cucumber.api.java.en.And; 12 | import cucumber.api.java.en.Then; 13 | import cucumber.api.java.en.When; 14 | import cucumber.api.spring.context.initializer.CucumberApplicationContextInitializer; 15 | import io.smartcat.spring.cassandra.showcase.ft.account.Account; 16 | 17 | @ContextConfiguration(initializers = { CucumberApplicationContextInitializer.class }) 18 | public class AccountStepDefinitions { 19 | 20 | private String usedEmail; 21 | private Response response; 22 | 23 | @When("^a valid sign up via email address request is sent$") 24 | public void a_valid_sign_up_via_email_address_request_is_sent() throws Throwable { 25 | usedEmail = givenUniqueEmail(); 26 | response = aSignUpViaEmailRequest(usedEmail).invoke(); 27 | } 28 | 29 | @Then("^the response status code should be (\\d+)$") 30 | public void the_response_status_code_should_be(final int expectedStatusCode) throws Throwable { 31 | assertThat(response.getStatus()).isEqualTo(expectedStatusCode); 32 | } 33 | 34 | @And("^account with that email address is created$") 35 | public void account_with_that_email_address_is_created() throws Throwable { 36 | assertThat(Account.fromResponse(response).getEmail()).isEqualTo(usedEmail); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /api-ft/src/main/java/io/smartcat/spring/cassandra/showcase/ft/cucumber/stepdefinitions/SmokeStepDefinitions.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.ft.cucumber.stepdefinitions; 2 | 3 | import static io.smartcat.spring.cassandra.showcase.ft.health.HealthRequestBuilder.aHealthRequest; 4 | import static org.assertj.core.api.Assertions.assertThat; 5 | 6 | import org.springframework.test.context.ContextConfiguration; 7 | 8 | import cucumber.api.java.en.Then; 9 | import cucumber.api.java.en.When; 10 | import cucumber.api.spring.context.initializer.CucumberApplicationContextInitializer; 11 | import io.smartcat.spring.cassandra.showcase.ft.health.Health; 12 | 13 | @ContextConfiguration(initializers = {CucumberApplicationContextInitializer.class}) 14 | public class SmokeStepDefinitions { 15 | 16 | private Health health; 17 | 18 | @When("^a request to the health endpoint is sent$") 19 | public void a_request_to_the_health_endpoint_is_sent() throws Throwable { 20 | health = Health.from(aHealthRequest()); 21 | } 22 | 23 | @Then("^it should report that status is UP$") 24 | public void it_should_report_that_status_is_UP() throws Throwable { 25 | assertThat(health.isUp()).isTrue(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /api-ft/src/main/java/io/smartcat/spring/cassandra/showcase/ft/error/RestError.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.ft.error; 2 | 3 | import javax.ws.rs.core.Response; 4 | 5 | import com.fasterxml.jackson.annotation.JsonCreator; 6 | import com.fasterxml.jackson.annotation.JsonProperty; 7 | 8 | public class RestError { 9 | 10 | private final String message; 11 | private final int code; 12 | 13 | @JsonCreator 14 | public RestError( 15 | @JsonProperty("message") final String message, 16 | @JsonProperty("code") final int code 17 | ) { 18 | this.message = message; 19 | this.code = code; 20 | } 21 | 22 | public String getMessage() { 23 | return message; 24 | } 25 | 26 | public int getCode() { 27 | return code; 28 | } 29 | 30 | public static RestError fromResponse(final Response response) { 31 | response.bufferEntity(); 32 | return response.readEntity(RestError.class); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /api-ft/src/main/java/io/smartcat/spring/cassandra/showcase/ft/health/Health.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.ft.health; 2 | 3 | import javax.ws.rs.client.Invocation; 4 | 5 | import com.fasterxml.jackson.annotation.JsonCreator; 6 | import com.fasterxml.jackson.annotation.JsonProperty; 7 | 8 | public class Health { 9 | 10 | private String status; 11 | 12 | @JsonCreator 13 | public Health(@JsonProperty("status") String status) { 14 | this.status = status; 15 | } 16 | 17 | public boolean isUp() { 18 | return status.equals("UP"); 19 | } 20 | 21 | public static Health from(Invocation request) { 22 | return request.invoke().readEntity(Health.class); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /api-ft/src/main/java/io/smartcat/spring/cassandra/showcase/ft/health/HealthRequestBuilder.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.ft.health; 2 | 3 | import javax.ws.rs.client.Invocation; 4 | 5 | import io.smartcat.spring.cassandra.showcase.ft.common.RequestBuilder; 6 | 7 | public class HealthRequestBuilder extends RequestBuilder { 8 | 9 | public HealthRequestBuilder() { 10 | super(); 11 | withHttpMethod(HttpMethod.GET); 12 | withEndPoint("health"); 13 | } 14 | 15 | public static Invocation aHealthRequest() { 16 | return new HealthRequestBuilder().build(); 17 | } 18 | 19 | @Override 20 | protected HealthRequestBuilder getThis() { 21 | return this; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /api-ft/src/main/java/io/smartcat/spring/cassandra/showcase/ft/util/RandomGenerators.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.ft.util; 2 | 3 | import java.util.Random; 4 | 5 | public class RandomGenerators { 6 | 7 | public static final Random RANDOM = new Random(); 8 | private static final char[] ALPHANUMERIC_SYMBOLS; 9 | 10 | static { 11 | // Initialize alphanumeric character array. 12 | final StringBuilder sb = new StringBuilder(62); 13 | for (char ch = '0'; ch <= '9'; ch++) { 14 | sb.append(ch); 15 | } 16 | for (char ch = 'a'; ch <= 'z'; ch++) { 17 | sb.append(ch); 18 | } 19 | for (char ch = 'A'; ch <= 'Z'; ch++) { 20 | sb.append(ch); 21 | } 22 | ALPHANUMERIC_SYMBOLS = sb.toString().toCharArray(); 23 | } 24 | 25 | public static int givenRandomIntegerInInterval(final int min, final int max) { 26 | return RANDOM.nextInt(max - min) + min; 27 | } 28 | 29 | public static String givenRandomAlphaNumbericString(final int min, final int max) { 30 | // Fast implementation of random alphanumeric string generator. 31 | final int stringLength = (min == max) ? max : RANDOM.nextInt(max - min) + min; 32 | final StringBuilder sb = new StringBuilder(stringLength); 33 | for (int length = 0; length < stringLength; length++) { 34 | sb.append(ALPHANUMERIC_SYMBOLS[RANDOM.nextInt(ALPHANUMERIC_SYMBOLS.length)]); 35 | } 36 | return sb.toString(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /api-ft/src/main/java/io/smartcat/spring/cassandra/showcase/ft/util/cli/CucumberArgumentsBuilder.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.ft.util.cli; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import io.smartcat.spring.cassandra.showcase.ft.util.config.cucumber.CucumberConfig; 7 | 8 | public class CucumberArgumentsBuilder { 9 | 10 | private static final String TAGS = "--tags"; 11 | private static final String PLUGIN = "--plugin"; 12 | private static final String MONOCHROME = "--monochrome"; 13 | private static final String NO_MONOCHROME = "--no-monochrome"; 14 | private static final String STRICT = "--strict"; 15 | private static final String NO_STRICT = "--no-strict"; 16 | private static final String DRY_RUN = "--dry-run"; 17 | private static final String NO_DRY_RUN = "--no-dry-run"; 18 | private static final String GLUE = "--glue"; 19 | private static final String DEFAULT_GLUE = "io.smartcat.spring.cassandra.showcase.ft.cucumber.stepdefinitions"; 20 | private static final String DEFAULT_FEATURES = "classpath:features"; 21 | 22 | /** 23 | * Builds list of arguments which are meant to be passed to cucumber 24 | * 25 | * @param config Cucumber configuration. 26 | * @param cliArguments Array of command-line arguments. 27 | * @return 28 | */ 29 | public List build(CucumberConfig config, String... cliArguments) 30 | throws IllegalArgumentException { 31 | 32 | List cucumberArguments = new ArrayList<>(); 33 | cucumberArguments.add(DEFAULT_FEATURES); 34 | addCliArgument(cucumberArguments, GLUE, DEFAULT_GLUE); 35 | // if tags are passed from command line use those, otherwise use ones from config file 36 | addParametersFromCommandlineOrConfigFile(cucumberArguments, TAGS, config.getTags(), 37 | cliArguments); 38 | 39 | config.getPlugins().stream().forEach((plugin) -> { 40 | addCliArgument(cucumberArguments, PLUGIN, plugin); 41 | }); 42 | 43 | cucumberArguments.add(config.isMonochrome() ? MONOCHROME : NO_MONOCHROME); 44 | cucumberArguments.add(config.isDryRun() ? DRY_RUN : NO_DRY_RUN); 45 | cucumberArguments.add(config.isStrict() ? STRICT : NO_STRICT); 46 | 47 | return cucumberArguments; 48 | } 49 | 50 | /** 51 | * Searches arguments from command-line for specified parameter name and if found adds it to cucumber runtime 52 | * configuration, or uses defaults passed in parameter {@code values}. Parameters are expected to be found in array 53 | * {@code cliArguments} in form of "{parameter, value}", e.g. {"--tags", "@smoke-test", "--glue", 54 | * "io.smartcat.spring.cassandra.showcase.ft.cucumber.stepdefinitions", "--tags", "~@ignore"} 55 | * 56 | * @param cucumberArguments List of arguments which will be built. 57 | * @param parameter Name of parameter that's being searched in list of passed parameters (e.g "--tags") 58 | * @param values List of default values that will be added to arguments list, if parameter with name passed in 59 | * {@code parameterName} is not found in {@code cliArguments} variable. 60 | * @param cliArguments Array of arguments that have been passed on command-line. 61 | */ 62 | private void addParametersFromCommandlineOrConfigFile(List cucumberArguments, 63 | String parameter, List values, String... cliArguments) 64 | throws IllegalArgumentException { 65 | /* 66 | If parameter wasn't specified on command line, this variable will stay false. 67 | This is needed in case parameter is specified multiple times on command-line 68 | (with different value), and we want to add all of its instances to arguments list. 69 | For example, "--tags" parameter can be passed on command-line multiple times with 70 | different values. 71 | */ 72 | boolean parameterNameFound = false; 73 | for (int i = 0; i < cliArguments.length; i++) { 74 | if (cliArguments[i].equals(parameter)) { 75 | addParameterValuePair(cucumberArguments, i, cliArguments); 76 | parameterNameFound = true; 77 | } 78 | } 79 | // If parameter was not found in arguments passed on command-line, add default value(s). 80 | if (!parameterNameFound) { 81 | addDefaultConfiguration(cucumberArguments, parameter, values); 82 | } 83 | } 84 | 85 | /** 86 | * Adds parameter-value pair read from command line to arguments list. 87 | * 88 | * @param cucumberArguments List of parameters and names which is being built. 89 | * @param pairStartIndex Index where parameter name is found in array of arguments passed form 90 | * command-line. 91 | * @param cliArguments Array of arguments received from command line. 92 | */ 93 | private void addParameterValuePair(List cucumberArguments, int pairStartIndex, 94 | String... cliArguments) throws IllegalArgumentException { 95 | if (cliArguments.length < pairStartIndex + 2) { 96 | throw new IllegalArgumentException("Invalid number of arguments."); 97 | } 98 | addCliArgument(cucumberArguments, cliArguments[pairStartIndex], 99 | cliArguments[pairStartIndex + 1]); 100 | } 101 | 102 | /** 103 | * Adds default set of parameter-value pairs to arguments list. 104 | * 105 | * @param args List of parameters and names which is being built. 106 | * @param parameter Name of parameter which we are setting default values for. 107 | * @param values Values that shall be assigned for specified parameter. 108 | */ 109 | private void addDefaultConfiguration(List args, String parameter, 110 | List values) { 111 | values.stream().forEach((value) -> { 112 | addCliArgument(args, parameter, value); 113 | }); 114 | } 115 | 116 | private void addCliArgument(List cucumberArguments, String parameter, String value) { 117 | cucumberArguments.add(parameter); 118 | cucumberArguments.add(value); 119 | } 120 | 121 | } 122 | -------------------------------------------------------------------------------- /api-ft/src/main/java/io/smartcat/spring/cassandra/showcase/ft/util/config/ApplicationConfig.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.ft.util.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Component; 5 | 6 | import io.smartcat.spring.cassandra.showcase.ft.util.config.cucumber.CucumberConfig; 7 | import io.smartcat.spring.cassandra.showcase.ft.util.config.server.ServerConfig; 8 | 9 | /** 10 | * Global configuration class. 11 | */ 12 | @Component 13 | public class ApplicationConfig { 14 | 15 | @Autowired 16 | private CucumberConfig cucumberConfig; 17 | 18 | @Autowired 19 | private ServerConfig serverConfig; 20 | 21 | public CucumberConfig getCucumberConfig() { 22 | return cucumberConfig; 23 | } 24 | 25 | public ServerConfig getServerConfig() { 26 | return serverConfig; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /api-ft/src/main/java/io/smartcat/spring/cassandra/showcase/ft/util/config/cucumber/CucumberConfig.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.ft.util.config.cucumber; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.beans.factory.annotation.Value; 7 | import org.springframework.boot.context.properties.ConfigurationProperties; 8 | import org.springframework.context.annotation.Configuration; 9 | 10 | @Configuration 11 | @ConfigurationProperties 12 | public class CucumberConfig { 13 | 14 | @Value("#{'${cucumber.tags}'.split(';')}") 15 | private List tags; 16 | 17 | @Autowired 18 | private CucumberPlugins plugins; 19 | 20 | @Value("${cucumber.monochrome}") 21 | private boolean monochrome; 22 | 23 | @Value("${cucumber.strict}") 24 | private boolean strict; 25 | 26 | @Value("${cucumber.dryRun}") 27 | private boolean dryRun; 28 | 29 | public List getTags() { 30 | return tags; 31 | } 32 | 33 | public List getPlugins() { 34 | return plugins.getPlugins(); 35 | } 36 | 37 | public boolean isMonochrome() { 38 | return monochrome; 39 | } 40 | 41 | public boolean isStrict() { 42 | return strict; 43 | } 44 | 45 | public boolean isDryRun() { 46 | return dryRun; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /api-ft/src/main/java/io/smartcat/spring/cassandra/showcase/ft/util/config/cucumber/CucumberPlugins.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.ft.util.config.cucumber; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.springframework.boot.context.properties.ConfigurationProperties; 7 | import org.springframework.stereotype.Component; 8 | 9 | @Component 10 | @ConfigurationProperties(prefix = "cucumber") 11 | public class CucumberPlugins { 12 | 13 | private List plugins = new ArrayList<>(); 14 | 15 | public List getPlugins() { 16 | return plugins; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /api-ft/src/main/java/io/smartcat/spring/cassandra/showcase/ft/util/config/server/ServerConfig.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.ft.util.config.server; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | import org.springframework.boot.context.properties.ConfigurationProperties; 5 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 6 | import org.springframework.context.annotation.Configuration; 7 | 8 | @Configuration 9 | @EnableConfigurationProperties 10 | @ConfigurationProperties(prefix = "server") 11 | public class ServerConfig { 12 | 13 | @Value("${server.apiEntryPoint}") 14 | private String apiEntryPoint; 15 | 16 | public String getApiEntryPoint() { 17 | return apiEntryPoint; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /api-ft/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | cucumber: 2 | tags: '~@ignore' 3 | plugins: 4 | - pretty 5 | - json:build/cucumber.json 6 | - junit:build/cucumber.xml 7 | - html:build/cucumber-html-report 8 | monochrome: false 9 | strict: true 10 | dryRun: false 11 | 12 | server: 13 | apiEntryPoint: http://localhost:8080 14 | -------------------------------------------------------------------------------- /api-ft/src/main/resources/features/account.feature: -------------------------------------------------------------------------------- 1 | Feature: Account 2 | Account registration, sign in 3 | 4 | Scenario: Sign up via email address - happy path 5 | When a valid sign up via email address request is sent 6 | Then the response status code should be 201 7 | And account with that email address is created -------------------------------------------------------------------------------- /api-ft/src/main/resources/features/smoke.feature: -------------------------------------------------------------------------------- 1 | @smoke-test 2 | Feature: Smoke tests 3 | Non-destructive tests able to run in all environments including production 4 | 5 | Scenario: API responds to ping 6 | When a request to the health endpoint is sent 7 | Then it should report that status is UP 8 | -------------------------------------------------------------------------------- /api/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | apply from: '../common/gradle/common.gradle' 3 | repositories { 4 | maven { url 'http://repo.spring.io/libs-release' } 5 | mavenCentral() 6 | 7 | dependencies { 8 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${versions.springBoot}") 9 | } 10 | } 11 | } 12 | 13 | apply from: 'gradle/dependencies.gradle' 14 | apply plugin: 'spring-boot' 15 | apply plugin: 'eclipse' 16 | 17 | jar.baseName = 'spring-cassandra-showcase' 18 | version = '1.0' 19 | mainClassName = 'io.smartcat.spring.cassandra.showcase.Application' 20 | -------------------------------------------------------------------------------- /api/gradle/dependencies.gradle: -------------------------------------------------------------------------------- 1 | repositories { 2 | maven { url 'http://repo.spring.io/libs-release' } 3 | mavenCentral() 4 | } 5 | 6 | dependencies { 7 | // Spring boot and spring security. 8 | compile("org.springframework.boot:spring-boot-starter-web:${versions.springBoot}") 9 | compile("org.springframework.boot:spring-boot-starter-actuator:${versions.springBoot}") 10 | compile("org.springframework.boot:spring-boot-starter-test:${versions.springBoot}") 11 | 12 | // FasterXML dependencies. 13 | compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:${versions.jackson}") 14 | compile("com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:${versions.jackson}") 15 | 16 | compile("com.google.guava:guava:${versions.guava}") 17 | 18 | // SnakeYML to support YML application configuration. 19 | compile('org.yaml:snakeyaml:1.15') 20 | 21 | compile('commons-validator:commons-validator:1.4.0') 22 | compile('org.apache.commons:commons-lang3:3.2.1') 23 | 24 | // Cassandra datastax driver for database access. 25 | compile('com.datastax.cassandra:cassandra-driver-core:2.1.9') 26 | compile('com.datastax.cassandra:cassandra-driver-mapping:2.1.9') 27 | compile('io.smartcat:cassandra-migration-tool:2.1.9.0') 28 | 29 | // JUnit and fluent assertions for unit testing. 30 | testCompile('junit:junit:4.12') 31 | testCompile("org.assertj:assertj-core:${versions.assertJ}") 32 | 33 | // Embedded cassandra for integration testing with database. 34 | testCompile('org.cassandraunit:cassandra-unit:2.1.3.1'){ 35 | exclude module: 'slf4j-log4j12' 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /api/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smartcat-labs/spring-cassandra-showcase-application/090fa7ad8cba4f1be44c1ffd846a7c4c5ea17caf/api/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /api/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun May 10 20:25:01 BST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip 7 | -------------------------------------------------------------------------------- /api/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /api/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /api/src/main/java/io/smartcat/spring/cassandra/showcase/Application.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 7 | import org.springframework.context.ConfigurableApplicationContext; 8 | import org.springframework.context.annotation.ComponentScan; 9 | 10 | import com.datastax.driver.core.Session; 11 | 12 | import io.smartcat.migration.MigrationEngine; 13 | import io.smartcat.migration.MigrationResources; 14 | import io.smartcat.spring.cassandra.showcase.adapter.cassandra.migrations.data.AddAdminAccountDataMigration; 15 | 16 | @EnableAutoConfiguration 17 | @ComponentScan 18 | public class Application { 19 | 20 | private final static Logger LOGGER = LoggerFactory.getLogger(Application.class); 21 | 22 | public static void main(final String[] args) throws Exception { 23 | final ConfigurableApplicationContext context = SpringApplication.run(Application.class, 24 | args); 25 | 26 | migrateData(context); 27 | } 28 | 29 | /** 30 | * Add data migrations here. This method is executed right after application 31 | * is started so data will be migrated while application is running. 32 | * 33 | * @param context Spring context 34 | */ 35 | private static void migrateData(final ConfigurableApplicationContext context) { 36 | LOGGER.info("Executing data migrations."); 37 | final Session session = (Session) context.getBean("session"); 38 | 39 | final MigrationResources resources = new MigrationResources(); 40 | resources.addMigration(new AddAdminAccountDataMigration(1)); 41 | 42 | MigrationEngine.withSession(session).migrate(resources); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /api/src/main/java/io/smartcat/spring/cassandra/showcase/adapter/cassandra/CassandraConfiguration.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.adapter.cassandra; 2 | 3 | import javax.annotation.PreDestroy; 4 | 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | import org.springframework.beans.factory.annotation.Value; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.context.annotation.Configuration; 10 | import org.springframework.context.annotation.Profile; 11 | 12 | import com.datastax.driver.core.Cluster; 13 | import com.datastax.driver.core.Host; 14 | import com.datastax.driver.core.Metadata; 15 | import com.datastax.driver.core.Session; 16 | 17 | import io.smartcat.migration.MigrationEngine; 18 | import io.smartcat.migration.MigrationResources; 19 | import io.smartcat.migration.MigrationType; 20 | import io.smartcat.spring.cassandra.showcase.adapter.cassandra.migrations.schema.AddAccountByExternalSourceTableMigration; 21 | import io.smartcat.spring.cassandra.showcase.adapter.cassandra.migrations.schema.InitializeSchema; 22 | 23 | @Configuration 24 | @Profile("default") 25 | public class CassandraConfiguration { 26 | 27 | private static final Logger LOGGER = LoggerFactory.getLogger(CassandraConfiguration.class); 28 | 29 | @Value("${cassandra.contactPoints}") 30 | private String contactPoints; 31 | 32 | @Value("${cassandra.port}") 33 | private int port; 34 | 35 | @Value("${cassandra.username}") 36 | private String username; 37 | 38 | @Value("${cassandra.password}") 39 | private String password; 40 | 41 | @Value("${cassandra.keyspace}") 42 | private String keyspace; 43 | 44 | @Value("${cassandra.replicationStrategy}") 45 | private String replicationStrategy; 46 | 47 | @Value("${cassandra.replicationFactor}") 48 | private int replicationFactor; 49 | 50 | private Cluster cluster; 51 | 52 | @Bean 53 | public Session session(){ 54 | final Session session = cluster().connect(); 55 | createKeyspaceIfNotExists(session); 56 | useKeyspace(session); 57 | migrateSchema(session); 58 | 59 | return session; 60 | } 61 | 62 | private Cluster cluster(){ 63 | LOGGER.info("Creating cluster with contact points: {} and port: {}", contactPoints, port); 64 | 65 | final String[] nodes = contactPoints.split(","); 66 | 67 | cluster = Cluster.builder().addContactPoints(nodes).withPort(port) 68 | .withCredentials(username, password).build(); 69 | metadata(cluster); 70 | 71 | return cluster; 72 | } 73 | 74 | private void metadata(final Cluster cluster) { 75 | final Metadata metadata = cluster.getMetadata(); 76 | LOGGER.info("Connected to cluster: {}", metadata.getClusterName()); 77 | 78 | for (final Host host : metadata.getAllHosts()) { 79 | LOGGER.info("Datacenter: {} host: {}", host.getDatacenter(), host.getAddress()); 80 | } 81 | } 82 | 83 | private void createKeyspaceIfNotExists(final Session session) { 84 | final String createKeyspace = String.format( 85 | "CREATE KEYSPACE IF NOT EXISTS %s WITH replication = " 86 | + "{'class':'%s', 'replication_factor':%d};", 87 | keyspace, replicationStrategy, replicationFactor); 88 | session.execute(createKeyspace); 89 | session.execute(String.format("USE %s;", keyspace)); 90 | } 91 | 92 | private void useKeyspace(final Session session) { 93 | session.execute(String.format("USE %s;", keyspace)); 94 | } 95 | 96 | @PreDestroy 97 | public void closeCluster() { 98 | LOGGER.info("Disposing cluster {}", cluster.getClusterName()); 99 | if (cluster != null){ 100 | cluster.close(); 101 | cluster = null; 102 | } 103 | } 104 | 105 | /** 106 | * Add schema migrations here. This method is executed right after keyspace 107 | * is created and it will execute migration in order they are defined. 108 | * 109 | * @param session Active cassandra session 110 | */ 111 | private void migrateSchema(final Session session) { 112 | final MigrationResources resources = new MigrationResources(); 113 | LOGGER.info("Executing schema migrations."); 114 | 115 | resources.addMigration(new InitializeSchema(1)); 116 | resources.addMigration(new AddAccountByExternalSourceTableMigration(2)); 117 | 118 | MigrationEngine.withSession(session).migrate(resources); 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /api/src/main/java/io/smartcat/spring/cassandra/showcase/adapter/cassandra/migrations/data/AddAdminAccountDataMigration.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.adapter.cassandra.migrations.data; 2 | 3 | import java.net.URI; 4 | import java.util.HashSet; 5 | import java.util.Set; 6 | 7 | import io.smartcat.migration.DataMigration; 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | 11 | import com.datastax.driver.mapping.Mapper; 12 | import com.datastax.driver.mapping.MappingManager; 13 | 14 | import io.smartcat.migration.exceptions.MigrationException; 15 | import io.smartcat.spring.cassandra.showcase.adapter.persistence.cassandra.account.AccountByEmail; 16 | import io.smartcat.spring.cassandra.showcase.domain.account.Account; 17 | import io.smartcat.spring.cassandra.showcase.domain.account.AccountRole; 18 | import io.smartcat.spring.cassandra.showcase.domain.account.EmailAddress; 19 | 20 | public class AddAdminAccountDataMigration extends DataMigration { 21 | 22 | private final static Logger LOGGER = LoggerFactory 23 | .getLogger(AddAdminAccountDataMigration.class); 24 | 25 | private Mapper accountByEmailMapper; 26 | 27 | public AddAdminAccountDataMigration(final int version) { 28 | super(version); 29 | } 30 | 31 | @Override 32 | public String getDescription() { 33 | return "Adding admin accounts to account table."; 34 | } 35 | 36 | @Override 37 | public void execute() throws MigrationException { 38 | try { 39 | addAdminAccounts(); 40 | } catch (final Exception e) { 41 | throw new MigrationException("Failed to execute AddAdminAccountDataMigration migration", 42 | e); 43 | } 44 | } 45 | 46 | private void addAdminAccounts() { 47 | final MappingManager mappingManager = new MappingManager(session); 48 | accountByEmailMapper = mappingManager.mapper(AccountByEmail.class); 49 | 50 | LOGGER.info("Creating admin account."); 51 | final AccountByEmail accountByEmail = new AccountByEmail(createAdminAccount()); 52 | 53 | accountByEmailMapper.save(accountByEmail); 54 | } 55 | 56 | private Account createAdminAccount() { 57 | final Set roles = new HashSet<>(); 58 | roles.add(AccountRole.ADMINISTRATOR); 59 | roles.add(AccountRole.CUSTOMER); 60 | 61 | final Account adminAccount = Account.createNew(new EmailAddress("joe@example.com"), 62 | "pass@123^", "Joe", "Doe", URI.create("http://www/example.com/joe-profile.jpg"), roles); 63 | return adminAccount; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /api/src/main/java/io/smartcat/spring/cassandra/showcase/adapter/cassandra/migrations/schema/AddAccountByExternalSourceTableMigration.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.adapter.cassandra.migrations.schema; 2 | 3 | import com.datastax.driver.core.Statement; 4 | import io.smartcat.migration.Migration; 5 | import io.smartcat.migration.MigrationType; 6 | import io.smartcat.migration.SchemaMigration; 7 | import io.smartcat.migration.exceptions.MigrationException; 8 | import io.smartcat.migration.exceptions.SchemaAgreementException; 9 | import io.smartcat.spring.cassandra.showcase.adapter.persistence.cassandra.account.AccountSchemaCreator; 10 | 11 | public class AddAccountByExternalSourceTableMigration extends SchemaMigration { 12 | 13 | public AddAccountByExternalSourceTableMigration(final int version) { 14 | super(version); 15 | } 16 | 17 | @Override 18 | public String getDescription() { 19 | return "Add account by external source migration."; 20 | } 21 | 22 | @Override 23 | public void execute() throws MigrationException { 24 | try { 25 | createAccountByExternalSourceSchema(); 26 | 27 | } catch (final Exception e) { 28 | throw new MigrationException( 29 | "Failed to execute AddAccountByExternalSourceTableMigration migration", e); 30 | } 31 | } 32 | 33 | private void createAccountByExternalSourceSchema() throws SchemaAgreementException { 34 | final Statement statement = AccountSchemaCreator.createAccountByExternalSourceTableIfNotExists(); 35 | executeWithSchemaAgreement(statement); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /api/src/main/java/io/smartcat/spring/cassandra/showcase/adapter/cassandra/migrations/schema/InitializeSchema.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.adapter.cassandra.migrations.schema; 2 | 3 | import com.datastax.driver.core.Statement; 4 | import io.smartcat.migration.Migration; 5 | import io.smartcat.migration.MigrationType; 6 | import io.smartcat.migration.SchemaMigration; 7 | import io.smartcat.migration.exceptions.MigrationException; 8 | import io.smartcat.migration.exceptions.SchemaAgreementException; 9 | import io.smartcat.spring.cassandra.showcase.adapter.persistence.cassandra.account.AccountSchemaCreator; 10 | 11 | public class InitializeSchema extends SchemaMigration { 12 | 13 | public InitializeSchema(final int version) { 14 | super(version); 15 | } 16 | 17 | @Override 18 | public String getDescription() { 19 | return "Initializing smartcatshowcase database schema."; 20 | } 21 | 22 | @Override 23 | public void execute() throws MigrationException { 24 | try { 25 | createAccountSchema(); 26 | 27 | } catch (final Exception e) { 28 | throw new MigrationException("Failed to execute InitializeSchema migration", 29 | e); 30 | } 31 | } 32 | 33 | private void createAccountSchema() throws SchemaAgreementException { 34 | final Statement statement = AccountSchemaCreator.createTableIfNotExists(); 35 | executeWithSchemaAgreement(statement); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /api/src/main/java/io/smartcat/spring/cassandra/showcase/adapter/http/account/AccountHttpAdapter.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.adapter.http.account; 2 | 3 | import javax.validation.Valid; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.web.bind.annotation.RequestBody; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RequestMethod; 10 | import org.springframework.web.bind.annotation.ResponseStatus; 11 | import org.springframework.web.bind.annotation.RestController; 12 | 13 | import io.smartcat.spring.cassandra.showcase.adapter.http.account.dto.AccountCreateRequest; 14 | import io.smartcat.spring.cassandra.showcase.adapter.http.account.dto.AccountDto; 15 | import io.smartcat.spring.cassandra.showcase.domain.account.AccountService; 16 | import io.smartcat.spring.cassandra.showcase.domain.account.EmailAddress; 17 | 18 | @RestController 19 | public class AccountHttpAdapter { 20 | 21 | @Autowired 22 | private AccountService accountService; 23 | 24 | @RequestMapping(value = "account", method = { RequestMethod.POST }) 25 | @ResponseStatus(HttpStatus.CREATED) 26 | public AccountDto register(@Valid @RequestBody final AccountCreateRequest request) { 27 | return accountService.createAccount(request.getFirstName(), request.getLastName(), 28 | new EmailAddress(request.getEmail()), request.getPassword(), request.profileImageUrl()); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /api/src/main/java/io/smartcat/spring/cassandra/showcase/adapter/http/account/dto/AccountCreateRequest.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.adapter.http.account.dto; 2 | 3 | import javax.validation.constraints.NotNull; 4 | 5 | import com.fasterxml.jackson.annotation.JsonCreator; 6 | import com.fasterxml.jackson.annotation.JsonProperty; 7 | 8 | public class AccountCreateRequest { 9 | 10 | @NotNull(message = "Email address is required.") 11 | private final String email; 12 | 13 | @NotNull(message = "Password is required.") 14 | private final String password; 15 | 16 | @NotNull(message = "First name is required.") 17 | private final String firstName; 18 | 19 | @NotNull(message = "Last name is required.") 20 | private final String lastName; 21 | 22 | @JsonProperty 23 | private final String profileImageUrl; 24 | 25 | @JsonCreator 26 | public AccountCreateRequest( 27 | @JsonProperty("email") String email, 28 | @JsonProperty("password") String password, 29 | @JsonProperty("firstName") String firstName, 30 | @JsonProperty("lastName") String lastName, 31 | @JsonProperty("profileImageUrl") String profileImageUrl 32 | ) { 33 | this.email = email; 34 | this.password = password; 35 | this.firstName = firstName; 36 | this.lastName = lastName; 37 | this.profileImageUrl = profileImageUrl; 38 | } 39 | 40 | public String getEmail() { 41 | return email; 42 | } 43 | 44 | public String getPassword() { 45 | return password; 46 | } 47 | 48 | public String getFirstName() { 49 | return firstName; 50 | } 51 | 52 | public String getLastName() { 53 | return lastName; 54 | } 55 | 56 | public String profileImageUrl() { 57 | return profileImageUrl; 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /api/src/main/java/io/smartcat/spring/cassandra/showcase/adapter/http/account/dto/AccountDto.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.adapter.http.account.dto; 2 | 3 | import java.time.Instant; 4 | import java.util.Set; 5 | 6 | import com.fasterxml.jackson.annotation.JsonCreator; 7 | import com.fasterxml.jackson.annotation.JsonProperty; 8 | 9 | import io.smartcat.spring.cassandra.showcase.domain.account.Account; 10 | import io.smartcat.spring.cassandra.showcase.domain.account.AccountRole; 11 | 12 | public class AccountDto { 13 | 14 | private final String email; 15 | private final String firstName; 16 | private final String lastName; 17 | private final Set roles; 18 | private final Instant registeredAt; 19 | private final String profileImageUrl; 20 | 21 | @JsonCreator 22 | public AccountDto( 23 | @JsonProperty("email") final String email, 24 | @JsonProperty("firstName") final String firstName, 25 | @JsonProperty("lastName") final String lastName, 26 | @JsonProperty("roles") final Set roles, 27 | @JsonProperty("registeredAt") final Instant registeredAt, 28 | @JsonProperty("profileImageUrl") final String profileImageUrl 29 | ) { 30 | this.email = email; 31 | this.firstName = firstName; 32 | this.lastName = lastName; 33 | this.roles = roles; 34 | this.registeredAt = registeredAt; 35 | this.profileImageUrl = profileImageUrl; 36 | } 37 | 38 | public String getEmail() { 39 | return email; 40 | } 41 | 42 | public String getFirstName() { 43 | return firstName; 44 | } 45 | 46 | public String getLastName() { 47 | return lastName; 48 | } 49 | 50 | public Set getRoles() { 51 | return roles; 52 | } 53 | 54 | public Instant getRegisteredAt() { 55 | return registeredAt; 56 | } 57 | 58 | public String getProfileImageUrl() { 59 | return profileImageUrl; 60 | } 61 | 62 | public static AccountDto fromAccount(final Account account) { 63 | return new AccountDto(account.email().address(), account.firstName(), account.lastName(), 64 | account.roles(), account.registeredAt(), account.profileImageUrl().toString()); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /api/src/main/java/io/smartcat/spring/cassandra/showcase/adapter/persistence/cassandra/account/AccountByEmail.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.adapter.persistence.cassandra.account; 2 | 3 | import java.net.URI; 4 | import java.util.Date; 5 | import java.util.Set; 6 | import java.util.stream.Collectors; 7 | 8 | import com.datastax.driver.mapping.annotations.Column; 9 | import com.datastax.driver.mapping.annotations.PartitionKey; 10 | import com.datastax.driver.mapping.annotations.Table; 11 | 12 | import io.smartcat.spring.cassandra.showcase.domain.account.Account; 13 | import io.smartcat.spring.cassandra.showcase.domain.account.AccountRole; 14 | import io.smartcat.spring.cassandra.showcase.domain.account.EmailAddress; 15 | 16 | /** 17 | * Account by email address which is unique in system for each account. 18 | * 19 | */ 20 | @Table(name = AccountByEmail.TABLE_NAME) 21 | public class AccountByEmail { 22 | 23 | public static final String TABLE_NAME = "account_by_email"; 24 | 25 | public static final String EMAIL_COLUMN = "email_address"; 26 | public static final String PASSWORD_COLUMN = "account_password"; 27 | public static final String FIRST_NAME_COLUMN = "first_name"; 28 | public static final String LAST_NAME_COLUMN = "last_name"; 29 | public static final String REGISTERED_AT_COLUMN = "registered_at"; 30 | public static final String ROLES_COLUMN = "roles"; 31 | public static final String PROFILE_IMAGE_URL_COLUMN = "profile_image_url"; 32 | 33 | @PartitionKey 34 | @Column(name = EMAIL_COLUMN) 35 | private String emailAddress; 36 | 37 | @Column(name = PASSWORD_COLUMN) 38 | private String password; 39 | 40 | @Column(name = FIRST_NAME_COLUMN) 41 | private String firstName; 42 | 43 | @Column(name = LAST_NAME_COLUMN) 44 | private String lastName; 45 | 46 | @Column(name = REGISTERED_AT_COLUMN) 47 | private Date registeredAt; 48 | 49 | @Column(name = ROLES_COLUMN) 50 | private Set roles; 51 | 52 | @Column(name = PROFILE_IMAGE_URL_COLUMN) 53 | private String profileImageUrl; 54 | 55 | public AccountByEmail() {} 56 | 57 | public AccountByEmail(final Account account) { 58 | emailAddress = account.email().address(); 59 | firstName = account.firstName(); 60 | lastName = account.lastName(); 61 | password = account.password(); 62 | roles = account.roles() 63 | .stream() 64 | .map(AccountRole::toString) 65 | .collect(Collectors.toSet()); 66 | registeredAt = Date.from(account.registeredAt()); 67 | profileImageUrl = account.profileImageUrl().toString(); 68 | } 69 | 70 | public String getEmailAddress() { 71 | return emailAddress; 72 | } 73 | 74 | public void setEmailAddress(final String emailAddress) { 75 | this.emailAddress = emailAddress; 76 | } 77 | 78 | public String getPassword() { 79 | return password; 80 | } 81 | 82 | public void setPassword(final String password) { 83 | this.password = password; 84 | } 85 | 86 | public String getFirstName() { 87 | return firstName; 88 | } 89 | 90 | public void setFirstName(final String firstName) { 91 | this.firstName = firstName; 92 | } 93 | 94 | public String getLastName() { 95 | return lastName; 96 | } 97 | 98 | public void setLastName(final String lastName) { 99 | this.lastName = lastName; 100 | } 101 | 102 | public Date getRegisteredAt() { 103 | return registeredAt; 104 | } 105 | 106 | public void setRegisteredAt(final Date registeredAt) { 107 | this.registeredAt = registeredAt; 108 | } 109 | 110 | public Set getRoles() { 111 | return roles; 112 | } 113 | 114 | public void setRoles(final Set roles) { 115 | this.roles = roles; 116 | } 117 | 118 | public String getProfileImageUrl() { 119 | return profileImageUrl; 120 | } 121 | 122 | public void setProfileImageUrl(final String profileImageUrl) { 123 | this.profileImageUrl = profileImageUrl; 124 | } 125 | 126 | public Account toAccount() { 127 | final Set accountRoles = roles 128 | .stream() 129 | .map(AccountRole::valueOf) 130 | .collect(Collectors.toSet()); 131 | 132 | final Account account = Account.fromDatabase(new EmailAddress(emailAddress), password, 133 | firstName, lastName, registeredAt.toInstant(), URI.create(profileImageUrl), 134 | accountRoles); 135 | 136 | return account; 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /api/src/main/java/io/smartcat/spring/cassandra/showcase/adapter/persistence/cassandra/account/AccountByEmailAccessor.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.adapter.persistence.cassandra.account; 2 | 3 | import java.util.Date; 4 | import java.util.Set; 5 | 6 | import com.datastax.driver.core.Statement; 7 | import com.datastax.driver.mapping.annotations.Accessor; 8 | import com.datastax.driver.mapping.annotations.Param; 9 | import com.datastax.driver.mapping.annotations.Query; 10 | 11 | @Accessor 12 | public interface AccountByEmailAccessor { 13 | 14 | @Query("INSERT INTO account_by_email (" 15 | + "email_address, " 16 | + "account_password, " 17 | + "first_name, " 18 | + "last_name, " 19 | + "registered_at, " 20 | + "roles, " 21 | + "profile_image_url" 22 | + ") VALUES (" 23 | + ":emailAddress, " 24 | + ":password, " 25 | + ":firstName, " 26 | + ":lastName, " 27 | + ":registeredAt, " 28 | + ":roles, " 29 | + ":profileImageUrl" 30 | + ") IF NOT EXISTS;") 31 | Statement insertAccountIfNotExists( 32 | @Param("emailAddress") String emailAddress, 33 | @Param("password") String password, 34 | @Param("firstName") String firstName, 35 | @Param("lastName") String lastName, 36 | @Param("registeredAt") Date registeredAt, 37 | @Param("roles") Set roles, 38 | @Param("profileImageUrl") String profileImageUrl); 39 | 40 | @Query("SELECT * FROM account_by_email WHERE email_address = :emailAddress;") 41 | Statement getAccountByEmailAddress(@Param("emailAddress") String emailAddress); 42 | } 43 | -------------------------------------------------------------------------------- /api/src/main/java/io/smartcat/spring/cassandra/showcase/adapter/persistence/cassandra/account/AccountByExternalSource.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.adapter.persistence.cassandra.account; 2 | 3 | import com.datastax.driver.mapping.annotations.Column; 4 | import com.datastax.driver.mapping.annotations.PartitionKey; 5 | import com.datastax.driver.mapping.annotations.Table; 6 | 7 | /** 8 | * Account by external source which is reverse index of account by email. It stores external source 9 | * and id as primary key and stores email as value. 10 | * 11 | */ 12 | @Table(name = AccountByExternalSource.TABLE_NAME) 13 | public class AccountByExternalSource { 14 | 15 | public static final String TABLE_NAME = "account_by_external_source"; 16 | 17 | public static final String EXTERNAL_SOURCE_ID_COLUMN = "external_source_id"; 18 | public static final String EXTERNAL_SOURCE_COLUMN = "external_source"; 19 | public static final String EMAIL_COLUMN = "email_address"; 20 | 21 | @PartitionKey(0) 22 | @Column(name = EXTERNAL_SOURCE_ID_COLUMN) 23 | private String externalSourceId; 24 | 25 | @PartitionKey(1) 26 | @Column(name = EXTERNAL_SOURCE_COLUMN) 27 | private String externalSource; 28 | 29 | @Column(name = EMAIL_COLUMN) 30 | private String emailAddress; 31 | 32 | public AccountByExternalSource() {} 33 | 34 | public AccountByExternalSource(final String externalSourceId, final String externalSource, 35 | final String emailAddress) { 36 | super(); 37 | this.externalSourceId = externalSourceId; 38 | this.externalSource = externalSource; 39 | this.emailAddress = emailAddress; 40 | } 41 | 42 | public String getEmailAddress() { 43 | return emailAddress; 44 | } 45 | 46 | public void setEmailAddress(final String emailAddress) { 47 | this.emailAddress = emailAddress; 48 | } 49 | 50 | public String getExternalSourceId() { 51 | return externalSourceId; 52 | } 53 | 54 | public void setExternalSourceId(final String externalSourceId) { 55 | this.externalSourceId = externalSourceId; 56 | } 57 | 58 | public String getExternalSource() { 59 | return externalSource; 60 | } 61 | 62 | public void setExternalSource(final String externalSource) { 63 | this.externalSource = externalSource; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /api/src/main/java/io/smartcat/spring/cassandra/showcase/adapter/persistence/cassandra/account/AccountByExternalSourceAccessor.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.adapter.persistence.cassandra.account; 2 | 3 | import com.datastax.driver.core.Statement; 4 | import com.datastax.driver.mapping.annotations.Accessor; 5 | import com.datastax.driver.mapping.annotations.Param; 6 | import com.datastax.driver.mapping.annotations.Query; 7 | 8 | @Accessor 9 | public interface AccountByExternalSourceAccessor { 10 | 11 | @Query("INSERT INTO account_by_external_source (" 12 | + "external_source_id, " 13 | + "external_source, " 14 | + "email_address" 15 | + ") VALUES (" 16 | + ":externalSourceId, " 17 | + ":externalSource, " 18 | + ":emailAddress" 19 | + ") IF NOT EXISTS;") 20 | Statement insertExternalSourceIfNotExists( 21 | @Param("externalSourceId") String externalSourceId, 22 | @Param("externalSource") String externalSource, 23 | @Param("emailAddress") String emailAddress); 24 | 25 | @Query("SELECT * FROM account_by_external_source WHERE external_source = :externalSource " 26 | + "AND external_source_id = :externalSourceId") 27 | Statement getAccountByExternalSource(@Param("externalSource") String externalSource, 28 | @Param("externalSourceId") String externalSourceId); 29 | } 30 | -------------------------------------------------------------------------------- /api/src/main/java/io/smartcat/spring/cassandra/showcase/adapter/persistence/cassandra/account/AccountRepositoryCassandra.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.adapter.persistence.cassandra.account; 2 | 3 | import java.util.Date; 4 | import java.util.Optional; 5 | import java.util.Set; 6 | import java.util.stream.Collectors; 7 | 8 | import javax.annotation.PostConstruct; 9 | 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.beans.factory.annotation.Value; 12 | import org.springframework.stereotype.Repository; 13 | 14 | import com.datastax.driver.core.ConsistencyLevel; 15 | import com.datastax.driver.core.ResultSet; 16 | import com.datastax.driver.core.Session; 17 | import com.datastax.driver.core.Statement; 18 | import com.datastax.driver.mapping.Mapper; 19 | import com.datastax.driver.mapping.MappingManager; 20 | 21 | import io.smartcat.spring.cassandra.showcase.domain.account.Account; 22 | import io.smartcat.spring.cassandra.showcase.domain.account.AccountRepository; 23 | import io.smartcat.spring.cassandra.showcase.domain.account.AccountRole; 24 | import io.smartcat.spring.cassandra.showcase.domain.account.EmailAddress; 25 | import io.smartcat.spring.cassandra.showcase.domain.account.ExternalSource; 26 | 27 | @Repository("accountRepositoryCassandra") 28 | public class AccountRepositoryCassandra implements AccountRepository { 29 | 30 | @Value("${cassandra.readConsistencyLevel}") 31 | private ConsistencyLevel readConsistencyLevel; 32 | 33 | @Value("${cassandra.writeConsistencyLevel}") 34 | private ConsistencyLevel writeConsistencyLevel; 35 | 36 | @Autowired 37 | public Session session; 38 | 39 | private Mapper accountByEmailMapper; 40 | private Mapper accountByExternalSourceMapper; 41 | private AccountByEmailAccessor accountByEmailAccessor; 42 | private AccountByExternalSourceAccessor accountByExternalSourceAccessor; 43 | 44 | @PostConstruct 45 | public void setupTable() { 46 | final MappingManager mappingManager = new MappingManager(session); 47 | accountByEmailMapper = mappingManager.mapper(AccountByEmail.class); 48 | accountByExternalSourceMapper = mappingManager.mapper(AccountByExternalSource.class); 49 | accountByEmailAccessor = mappingManager.createAccessor(AccountByEmailAccessor.class); 50 | accountByExternalSourceAccessor = mappingManager 51 | .createAccessor(AccountByExternalSourceAccessor.class); 52 | } 53 | 54 | @Override 55 | public Optional accountOfEmail(final EmailAddress email) { 56 | final Statement statement = accountByEmailAccessor 57 | .getAccountByEmailAddress(email.address()); 58 | statement.setConsistencyLevel(readConsistencyLevel); 59 | final AccountByEmail accountByEmail = 60 | accountByEmailMapper.map(session.execute(statement)).one(); 61 | 62 | return accountByEmail == null ? Optional.empty() : Optional.of(accountByEmail.toAccount()); 63 | } 64 | 65 | @Override 66 | public boolean createIfNotExists(final Account account) { 67 | final Set roles = account.roles() 68 | .stream() 69 | .map(AccountRole::toString) 70 | .collect(Collectors.toSet()); 71 | final Statement statement = accountByEmailAccessor.insertAccountIfNotExists( 72 | account.email().address(), 73 | account.password(), 74 | account.firstName(), 75 | account.lastName(), 76 | Date.from(account.registeredAt()), 77 | roles, 78 | account.profileImageUrl().toString()); 79 | statement.setConsistencyLevel(writeConsistencyLevel); 80 | final ResultSet result = session.execute(statement); 81 | 82 | return result.wasApplied(); 83 | } 84 | 85 | @Override 86 | public Optional accountOfTwitterId(final String twitterId) { 87 | final Statement statement = accountByExternalSourceAccessor 88 | .getAccountByExternalSource(ExternalSource.TWITTER.name(), twitterId); 89 | statement.setConsistencyLevel(readConsistencyLevel); 90 | final ResultSet result = session.execute(statement); 91 | final AccountByExternalSource accountByTwitterId = accountByExternalSourceMapper 92 | .map(result).one(); 93 | 94 | if (accountByTwitterId == null) { 95 | return Optional.empty(); 96 | } 97 | 98 | return accountOfEmail(new EmailAddress(accountByTwitterId.getEmailAddress())); 99 | } 100 | 101 | @Override 102 | public boolean linkTwitterToAccount(final String twitterId, final Account account) { 103 | final Statement statement = accountByExternalSourceAccessor.insertExternalSourceIfNotExists( 104 | twitterId, ExternalSource.TWITTER.name(), account.email().address()); 105 | 106 | statement.setConsistencyLevel(writeConsistencyLevel); 107 | final ResultSet result = session.execute(statement); 108 | 109 | return result.wasApplied(); 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /api/src/main/java/io/smartcat/spring/cassandra/showcase/adapter/persistence/cassandra/account/AccountSchemaCreator.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.adapter.persistence.cassandra.account; 2 | 3 | import static com.datastax.driver.core.DataType.set; 4 | import static com.datastax.driver.core.DataType.text; 5 | import static com.datastax.driver.core.DataType.timestamp; 6 | 7 | import com.datastax.driver.core.ConsistencyLevel; 8 | import com.datastax.driver.core.Session; 9 | import com.datastax.driver.core.SimpleStatement; 10 | import com.datastax.driver.core.Statement; 11 | import com.datastax.driver.core.schemabuilder.SchemaBuilder; 12 | 13 | /** 14 | * Helper class to isolate all tables related to account. 15 | * 16 | */ 17 | public class AccountSchemaCreator { 18 | 19 | /** 20 | * Main account table which holds account by email address which is unique per account (natural 21 | * primary key). 22 | */ 23 | public static Statement createTableIfNotExists() { 24 | final String createAccountByEmailTable = SchemaBuilder 25 | .createTable(AccountByEmail.TABLE_NAME) 26 | .addPartitionKey(AccountByEmail.EMAIL_COLUMN, text()) 27 | .addColumn(AccountByEmail.FIRST_NAME_COLUMN, text()) 28 | .addColumn(AccountByEmail.LAST_NAME_COLUMN, text()) 29 | .addColumn(AccountByEmail.PASSWORD_COLUMN, text()) 30 | .addColumn(AccountByEmail.REGISTERED_AT_COLUMN, timestamp()) 31 | .addColumn(AccountByEmail.ROLES_COLUMN, set(text())) 32 | .addColumn(AccountByEmail.PROFILE_IMAGE_URL_COLUMN, text()) 33 | .ifNotExists().withOptions() 34 | .comment("Accounts in system by email.") 35 | .buildInternal(); 36 | 37 | return new SimpleStatement(createAccountByEmailTable); 38 | } 39 | 40 | /** 41 | * Account by external ID is reverse index to account table by external source. It has compound 42 | * primary key which is combination of external source and ID of that source. This table will 43 | * allow to fetch account by two queries and it is optimal since this happens only during sign 44 | * in. 45 | */ 46 | public static Statement createAccountByExternalSourceTableIfNotExists() { 47 | final String createAccountByExternalSourceTable = SchemaBuilder 48 | .createTable(AccountByExternalSource.TABLE_NAME) 49 | .addPartitionKey(AccountByExternalSource.EXTERNAL_SOURCE_ID_COLUMN, text()) 50 | .addPartitionKey(AccountByExternalSource.EXTERNAL_SOURCE_COLUMN, text()) 51 | .addColumn(AccountByExternalSource.EMAIL_COLUMN, text()).ifNotExists().withOptions() 52 | .comment("Account email by external source.").buildInternal(); 53 | 54 | return new SimpleStatement(createAccountByExternalSourceTable); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /api/src/main/java/io/smartcat/spring/cassandra/showcase/domain/account/Account.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.domain.account; 2 | 3 | import java.net.URI; 4 | import java.time.Instant; 5 | import java.util.Set; 6 | import java.util.regex.Matcher; 7 | import java.util.regex.Pattern; 8 | 9 | public class Account { 10 | 11 | private final static int PASSWORD_MINIMUM_LENGTH = 8; 12 | 13 | private EmailAddress email; 14 | private String password; 15 | private String firstName; 16 | private String lastName; 17 | private Instant registeredAt; 18 | private URI profileImageUrl; 19 | private Set roles; 20 | 21 | public static Account createNew(final EmailAddress email, final String password, 22 | final String firstName, final String lastName, final URI profileImageUrl, 23 | final Set roles) { 24 | return new Account(email, password, firstName, lastName, Instant.now(), profileImageUrl, 25 | roles); 26 | } 27 | 28 | public static Account fromDatabase(final EmailAddress email, final String password, 29 | final String firstName, final String lastName, final Instant registeredAt, 30 | final URI profileImageUrl, final Set roles) { 31 | return new Account(email, password, firstName, lastName, registeredAt, profileImageUrl, 32 | roles); 33 | } 34 | 35 | private Account(final EmailAddress email, final String password, final String firstName, 36 | final String lastName, final Instant registeredAt, final URI profileImageUrl, 37 | final Set roles) { 38 | this.email = email; 39 | this.password = password; 40 | this.firstName = firstName; 41 | this.lastName = lastName; 42 | this.registeredAt = registeredAt; 43 | this.profileImageUrl = profileImageUrl; 44 | this.roles = roles; 45 | 46 | } 47 | 48 | public EmailAddress email() { 49 | return email; 50 | } 51 | 52 | public String firstName() { 53 | return firstName; 54 | } 55 | 56 | public String lastName() { 57 | return lastName; 58 | } 59 | 60 | public Set roles() { 61 | return roles; 62 | } 63 | 64 | public Instant registeredAt() { 65 | return registeredAt; 66 | } 67 | 68 | /** 69 | * Enforce a policy of minimum length and required character types. 70 | * 71 | * @param password password to authenticate this user 72 | */ 73 | protected void setPassword(final String password) { 74 | if (password.length() < PASSWORD_MINIMUM_LENGTH) { 75 | throw new IllegalArgumentException(String.format( 76 | "Password must be at least %s characters.", PASSWORD_MINIMUM_LENGTH)); 77 | } 78 | 79 | // This works with ASCII upper and lower case letters only. 80 | final Pattern pattern = Pattern.compile("^(?=.*[^a-zA-Z])(?=.*[a-z])(?=.*[A-Z])\\S*$"); 81 | final Matcher matcher = pattern.matcher(password); 82 | if (!matcher.matches()) { 83 | throw new IllegalArgumentException( 84 | "Password must contain at least a lower case letter, an upper case letter and " 85 | + "non-letter."); 86 | } 87 | 88 | this.password = password; 89 | } 90 | 91 | public String password() { 92 | return password; 93 | } 94 | 95 | public URI profileImageUrl() { 96 | return profileImageUrl; 97 | } 98 | 99 | public void updateProfileImageUrl(final URI profileImageUrl) { 100 | this.profileImageUrl = profileImageUrl; 101 | } 102 | 103 | public boolean isAdministrator() { 104 | return roles().contains(AccountRole.ADMINISTRATOR); 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /api/src/main/java/io/smartcat/spring/cassandra/showcase/domain/account/AccountRepository.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.domain.account; 2 | 3 | import java.util.Optional; 4 | 5 | import org.springframework.stereotype.Repository; 6 | 7 | @Repository 8 | public interface AccountRepository { 9 | 10 | /** 11 | * Try to find account based on email if it exists. 12 | * 13 | * @param email EmailAddress of account. 14 | * 15 | * @return Optional with account if it exists. 16 | */ 17 | Optional accountOfEmail(EmailAddress email); 18 | 19 | /** 20 | * Try to find account based on email if it exists. 21 | * 22 | * @param email EmailAddress of account. 23 | * 24 | * @return Optional with account if it exists. 25 | */ 26 | Optional accountOfTwitterId(String twitterId); 27 | 28 | /** 29 | * Account will be created only if account with same email address does not 30 | * already exist 31 | * 32 | * @param account Account to be inserted 33 | * @return true if account was inserted, otherwise false 34 | */ 35 | boolean createIfNotExists(Account account); 36 | 37 | /** 38 | * Link Twitter ID to existing account. 39 | * 40 | * @param twitterId Twitter ID of customer. 41 | * @param account Existing account in system. 42 | * 43 | * @return true if twitter ID is linked, false if twitter Id already exists. 44 | */ 45 | boolean linkTwitterToAccount(final String twitterId, final Account account); 46 | 47 | } 48 | -------------------------------------------------------------------------------- /api/src/main/java/io/smartcat/spring/cassandra/showcase/domain/account/AccountRole.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.domain.account; 2 | 3 | public enum AccountRole { 4 | CUSTOMER, ADMINISTRATOR 5 | } 6 | -------------------------------------------------------------------------------- /api/src/main/java/io/smartcat/spring/cassandra/showcase/domain/account/AccountService.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.domain.account; 2 | 3 | import java.net.URI; 4 | import java.util.HashSet; 5 | import java.util.Optional; 6 | import java.util.Set; 7 | 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.stereotype.Service; 10 | 11 | import io.smartcat.spring.cassandra.showcase.adapter.http.account.dto.AccountDto; 12 | 13 | @Service 14 | public class AccountService { 15 | 16 | @Autowired 17 | private AccountRepository accountRepository; 18 | 19 | public AccountDto createAccount( 20 | final String firstName, 21 | final String lastName, 22 | final EmailAddress emailAddress, 23 | final String password, 24 | final String profileImageUrl 25 | ) { 26 | final Set roles = new HashSet<>(); 27 | roles.add(AccountRole.CUSTOMER); 28 | 29 | Account account = Account.createNew(emailAddress, password, firstName, lastName, URI.create(profileImageUrl), 30 | roles); 31 | 32 | accountRepository.createIfNotExists(account); 33 | 34 | return AccountDto.fromAccount(account); 35 | } 36 | 37 | public Optional getBy(final EmailAddress emailAddress) { 38 | return accountRepository.accountOfEmail(emailAddress).map(AccountDto::fromAccount); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /api/src/main/java/io/smartcat/spring/cassandra/showcase/domain/account/EmailAddress.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.domain.account; 2 | 3 | import java.util.Objects; 4 | 5 | import org.apache.commons.validator.routines.EmailValidator; 6 | 7 | public class EmailAddress { 8 | 9 | private String address; 10 | 11 | public EmailAddress(final String address) { 12 | this.setAddress(address); 13 | } 14 | 15 | public String address() { 16 | return this.address; 17 | } 18 | 19 | protected void setAddress(final String address) { 20 | if (!EmailValidator.getInstance().isValid(address)) { 21 | throw new IllegalArgumentException( 22 | String.format("Provided email address '%s' is not valid.", address)); 23 | } 24 | this.address = address.toLowerCase(); 25 | } 26 | 27 | public boolean sameValueAs(final EmailAddress other) { 28 | return other != null && Objects.equals(this.address, other.address); 29 | } 30 | 31 | @Override 32 | public boolean equals(final Object o) { 33 | if (this == o) { 34 | return true; 35 | } 36 | if (o == null || getClass() != o.getClass()) { 37 | return false; 38 | } 39 | 40 | return sameValueAs((EmailAddress) o); 41 | } 42 | 43 | @Override 44 | public int hashCode() { 45 | return Objects.hash(this.address); 46 | } 47 | 48 | @Override 49 | public String toString() { 50 | return this.address; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /api/src/main/java/io/smartcat/spring/cassandra/showcase/domain/account/ExternalSource.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.domain.account; 2 | 3 | public enum ExternalSource { 4 | TWITTER, FACEBOOK, LINKEDIN 5 | } 6 | -------------------------------------------------------------------------------- /api/src/main/resources/application-test.yml: -------------------------------------------------------------------------------- 1 | cassandra: 2 | keyspace: smartcatshowcase_test 3 | contactPoints: localhost 4 | port: 9342 5 | replicationStrategy: NetworkTopologyStrategy 6 | startupTimeoutInSeconds: 30 7 | -------------------------------------------------------------------------------- /api/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | cassandra: 2 | contactPoints: 192.168.34.20 # vagrant db machine 3 | port: 9042 4 | keyspace: smartcatshowcase 5 | replicationStrategy: SimpleStrategy 6 | replicationFactor: 1 7 | readConsistencyLevel: ONE 8 | writeConsistencyLevel: ONE 9 | username: 10 | password: 11 | -------------------------------------------------------------------------------- /api/src/test/java/io/smartcat/spring/cassandra/showcase/adapter/persistence/cassandra/account/AccountRepositoryCassandraTest.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.adapter.persistence.cassandra.account; 2 | 3 | import static io.smartcat.spring.cassandra.showcase.test.generators.AccountGenerators.givenAnyAccount; 4 | import static io.smartcat.spring.cassandra.showcase.test.generators.AccountGenerators.givenAnyTwitterId; 5 | import static org.assertj.core.api.Assertions.assertThat; 6 | 7 | import java.util.Optional; 8 | 9 | import org.junit.Test; 10 | import org.junit.runner.RunWith; 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | import org.springframework.boot.test.ConfigFileApplicationContextInitializer; 13 | import org.springframework.context.annotation.Bean; 14 | import org.springframework.context.annotation.Configuration; 15 | import org.springframework.context.annotation.Profile; 16 | import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; 17 | import org.springframework.test.context.ActiveProfiles; 18 | import org.springframework.test.context.ContextConfiguration; 19 | import org.springframework.test.context.TestExecutionListeners; 20 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 21 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 22 | 23 | import io.smartcat.spring.cassandra.showcase.domain.account.Account; 24 | import io.smartcat.spring.cassandra.showcase.domain.account.AccountRepository; 25 | import io.smartcat.spring.cassandra.showcase.test.cassandra.CassandraTestExecutionListener; 26 | import io.smartcat.spring.cassandra.showcase.test.cassandra.TestCassandraConfig; 27 | 28 | @RunWith(SpringJUnit4ClassRunner.class) 29 | @ActiveProfiles("test") 30 | @ContextConfiguration( 31 | classes = { 32 | TestCassandraConfig.class, 33 | AccountRepositoryCassandraTest.TestConfig.class 34 | }, 35 | initializers = ConfigFileApplicationContextInitializer.class) 36 | @TestExecutionListeners({ 37 | CassandraTestExecutionListener.class, 38 | DependencyInjectionTestExecutionListener.class }) 39 | public class AccountRepositoryCassandraTest { 40 | 41 | @Configuration 42 | @Profile("test") 43 | static class TestConfig { 44 | @Bean 45 | static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { 46 | return new PropertySourcesPlaceholderConfigurer(); 47 | } 48 | 49 | @Bean(name = "accountRepositoryCassandra") 50 | public AccountRepository accountRepository() { 51 | return new AccountRepositoryCassandra(); 52 | } 53 | } 54 | 55 | @Autowired 56 | private AccountRepository accountRepository; 57 | 58 | @Test public void 59 | accountCreated_whenAccountDateIsValid() { 60 | final Account account = givenAnyAccount(); 61 | 62 | // when 63 | final boolean accountCreated = accountRepository.createIfNotExists(account); 64 | 65 | // then 66 | assertThat(accountCreated).isTrue(); 67 | } 68 | 69 | @Test public void 70 | accountNotCreated_whenAccountAlreadyExists() { 71 | final Account account = givenAnyAccount(); 72 | accountRepository.createIfNotExists(account); 73 | 74 | // when 75 | final boolean accountCreated = accountRepository.createIfNotExists(account); 76 | 77 | // then 78 | assertThat(accountCreated).isFalse(); 79 | } 80 | 81 | @Test public void 82 | accountByEmailReturned_whenAccountExistsInDatabase() { 83 | final Account account = givenAnyAccount(); 84 | accountRepository.createIfNotExists(account); 85 | 86 | // when 87 | final Optional accountFromDatabaseOptional = accountRepository 88 | .accountOfEmail(account.email()); 89 | 90 | // then 91 | assertThat(accountFromDatabaseOptional).isPresent(); 92 | 93 | final Account accountFromDatabase = accountFromDatabaseOptional.get(); 94 | assertAccountEquality(account, accountFromDatabase); 95 | } 96 | 97 | @Test public void 98 | accountByEmailNotReturned_whenAccountDoesNotExistsInDatabase() { 99 | final Account account = givenAnyAccount(); 100 | 101 | // when 102 | final Optional accountFromDatabase = accountRepository 103 | .accountOfEmail(account.email()); 104 | 105 | // then 106 | assertThat(accountFromDatabase).isEmpty(); 107 | } 108 | 109 | @Test public void 110 | accountByTwitterIdLinkedToAccount_whenAccountAlreadyExists() { 111 | final Account account = givenAnyAccount(); 112 | final String twitterId = givenAnyTwitterId(); 113 | 114 | final boolean twitterAccountLinked = accountRepository.linkTwitterToAccount(twitterId, 115 | account); 116 | 117 | assertThat(twitterAccountLinked).isTrue(); 118 | } 119 | 120 | @Test public void 121 | accountByTwitterIdNotLinkedToAccount_whenAccountAlreadyLinkedToTwitter() { 122 | final Account account = givenAnyAccount(); 123 | final String twitterId = givenAnyTwitterId(); 124 | accountRepository.linkTwitterToAccount(twitterId, account); 125 | 126 | final boolean twitterAccountLinked = accountRepository.linkTwitterToAccount(twitterId, 127 | account); 128 | 129 | assertThat(twitterAccountLinked).isFalse(); 130 | } 131 | 132 | @Test public void 133 | accountByTwitterIdReturned_whenAccountWithTwitterIdExists() { 134 | final Account account = givenAnyAccount(); 135 | final String twitterId = givenAnyTwitterId(); 136 | accountRepository.createIfNotExists(account); 137 | accountRepository.linkTwitterToAccount(twitterId, account); 138 | 139 | final Optional accountByTwitterIdOptional = accountRepository 140 | .accountOfTwitterId(twitterId); 141 | 142 | assertThat(accountByTwitterIdOptional).isPresent(); 143 | assertAccountEquality(account, accountByTwitterIdOptional.get()); 144 | } 145 | 146 | @Test public void 147 | accountByTwitterIdNotReturned_whenAccountWithTwitterIdDoesNotExists() { 148 | final String twitterId = givenAnyTwitterId(); 149 | 150 | final Optional accountByTwitterIdOptional = accountRepository 151 | .accountOfTwitterId(twitterId); 152 | 153 | assertThat(accountByTwitterIdOptional).isEmpty(); 154 | } 155 | 156 | private void assertAccountEquality(final Account account, final Account accountFromDatabase) { 157 | assertThat(accountFromDatabase.firstName()).isEqualTo(account.firstName()); 158 | assertThat(accountFromDatabase.lastName()).isEqualTo(account.lastName()); 159 | assertThat(accountFromDatabase.email()).isEqualTo(account.email()); 160 | 161 | } 162 | } 163 | -------------------------------------------------------------------------------- /api/src/test/java/io/smartcat/spring/cassandra/showcase/test/cassandra/CassandraTestExecutionListener.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.test.cassandra; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | import org.springframework.test.context.TestContext; 9 | import org.springframework.test.context.support.AbstractTestExecutionListener; 10 | 11 | import com.datastax.driver.core.Cluster; 12 | import com.datastax.driver.core.KeyspaceMetadata; 13 | import com.datastax.driver.core.Metadata; 14 | import com.datastax.driver.core.Session; 15 | import com.datastax.driver.core.TableMetadata; 16 | 17 | public class CassandraTestExecutionListener extends AbstractTestExecutionListener { 18 | 19 | private static final Logger LOGGER = LoggerFactory.getLogger(CassandraTestExecutionListener.class); 20 | 21 | public static final String KEYSPACE = "smartcatshowcase_test"; 22 | private static List tables; 23 | 24 | @Override 25 | public void afterTestMethod(final TestContext testContext) throws Exception { 26 | LOGGER.debug("AfterTest: clean embedded Cassandra."); 27 | final Session session = (Session) TestApplicationContext.getBean("session"); 28 | for (final String table : tables(session)) { 29 | LOGGER.debug("Truncating table {}", table); 30 | session.execute(String.format("TRUNCATE %s.%s;", KEYSPACE, table)); 31 | } 32 | super.afterTestMethod(testContext); 33 | } 34 | 35 | public List tables(final Session session) { 36 | if (tables == null) { 37 | tables = new ArrayList<>(); 38 | final Cluster cluster = session.getCluster(); 39 | final Metadata meta = cluster.getMetadata(); 40 | final KeyspaceMetadata keyspaceMeta = meta.getKeyspace(KEYSPACE); 41 | for (final TableMetadata tableMeta : keyspaceMeta.getTables()) { 42 | tables.add(tableMeta.getName()); 43 | } 44 | } 45 | 46 | return tables; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /api/src/test/java/io/smartcat/spring/cassandra/showcase/test/cassandra/TestApplicationContext.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.test.cassandra; 2 | 3 | import org.springframework.beans.BeansException; 4 | import org.springframework.context.ApplicationContext; 5 | import org.springframework.context.ApplicationContextAware; 6 | 7 | public class TestApplicationContext implements ApplicationContextAware { 8 | 9 | private static ApplicationContext context; 10 | 11 | @Override 12 | public void setApplicationContext(final ApplicationContext context) throws BeansException { 13 | TestApplicationContext.context = context; 14 | } 15 | 16 | public static Object getBean(final String beanName) { 17 | return context.getBean(beanName); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /api/src/test/java/io/smartcat/spring/cassandra/showcase/test/cassandra/TestCassandraConfig.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.test.cassandra; 2 | 3 | import java.time.Duration; 4 | 5 | import org.cassandraunit.CQLDataLoader; 6 | import org.cassandraunit.dataset.cql.ClassPathCQLDataSet; 7 | import org.cassandraunit.utils.EmbeddedCassandraServerHelper; 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | import org.springframework.beans.factory.DisposableBean; 11 | import org.springframework.beans.factory.annotation.Value; 12 | import org.springframework.context.annotation.Bean; 13 | import org.springframework.context.annotation.Configuration; 14 | import org.springframework.context.annotation.Profile; 15 | 16 | import com.datastax.driver.core.Cluster; 17 | import com.datastax.driver.core.Session; 18 | 19 | import io.smartcat.spring.cassandra.showcase.test.cassandra.stub.SessionProxy; 20 | 21 | @Configuration 22 | @Profile({"test"}) 23 | public class TestCassandraConfig implements DisposableBean { 24 | 25 | private static final Logger LOGGER = LoggerFactory.getLogger(TestCassandraConfig.class); 26 | 27 | private static final String CQL = "db.cql"; 28 | 29 | @Value("${cassandra.startupTimeoutInSeconds}") 30 | private long startupTimeoutInSeconds; 31 | 32 | @Value("${cassandra.contactPoints}") 33 | private String contactPoints; 34 | 35 | @Value("${cassandra.port}") 36 | private int port; 37 | 38 | @Value("${cassandra.keyspace}") 39 | private String keyspace; 40 | 41 | private static Cluster cluster; 42 | private static Session session; 43 | private static SessionProxy sessionProxy; 44 | 45 | @Bean 46 | public Session session() throws Exception { 47 | if (session == null) { 48 | initialize(); 49 | } 50 | 51 | if (sessionProxy == null) { 52 | sessionProxy = new SessionProxy(session); 53 | } 54 | 55 | return sessionProxy; 56 | } 57 | 58 | @Bean 59 | public TestApplicationContext testApplicationContext() { 60 | return new TestApplicationContext(); 61 | } 62 | 63 | private void initialize() throws Exception { 64 | LOGGER.info("Starting embedded cassandra server"); 65 | EmbeddedCassandraServerHelper.startEmbeddedCassandra("cassandra-unit.yaml", 66 | Duration.ofSeconds(startupTimeoutInSeconds).toMillis()); 67 | 68 | LOGGER.info("Connect to embedded db"); 69 | cluster = Cluster.builder().addContactPoints(contactPoints).withPort(port).build(); 70 | session = cluster.connect(); 71 | 72 | LOGGER.info("Initialize keyspace"); 73 | final CQLDataLoader cqlDataLoader = new CQLDataLoader(session); 74 | cqlDataLoader.load(new ClassPathCQLDataSet(CQL, false, true, keyspace)); 75 | } 76 | 77 | @Override 78 | public void destroy() throws Exception { 79 | if (cluster != null) { 80 | cluster.close(); 81 | cluster = null; 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /api/src/test/java/io/smartcat/spring/cassandra/showcase/test/cassandra/stub/ResultSetFutureStub.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.test.cassandra.stub; 2 | 3 | import java.util.concurrent.ExecutionException; 4 | import java.util.concurrent.Executor; 5 | import java.util.concurrent.TimeUnit; 6 | import java.util.concurrent.TimeoutException; 7 | 8 | import com.datastax.driver.core.ResultSet; 9 | import com.datastax.driver.core.ResultSetFuture; 10 | 11 | /** 12 | * ResultSetFuture implementation with only get() method defined. 13 | * 14 | * Used for tests to enable asynchronous calls executed synchronously. 15 | * 16 | */ 17 | public class ResultSetFutureStub implements ResultSetFuture { 18 | 19 | private final ResultSet resultSet; 20 | 21 | public ResultSetFutureStub(final ResultSet resultSet) { 22 | this.resultSet = resultSet; 23 | } 24 | 25 | @Override 26 | public void addListener(final Runnable listener, final Executor executor) { 27 | throw new UnsupportedOperationException(); 28 | } 29 | 30 | @Override 31 | public boolean isCancelled() { 32 | throw new UnsupportedOperationException(); 33 | } 34 | 35 | @Override 36 | public boolean isDone() { 37 | throw new UnsupportedOperationException(); 38 | } 39 | 40 | @Override 41 | public ResultSet get() throws InterruptedException, ExecutionException { 42 | return resultSet; 43 | } 44 | 45 | @Override 46 | public ResultSet get(final long timeout, final TimeUnit unit) 47 | throws InterruptedException, ExecutionException, TimeoutException { 48 | return resultSet; 49 | } 50 | 51 | @Override 52 | public ResultSet getUninterruptibly() { 53 | return resultSet; 54 | } 55 | 56 | @Override 57 | public ResultSet getUninterruptibly(final long timeout, final TimeUnit unit) 58 | throws TimeoutException { 59 | return resultSet; 60 | } 61 | 62 | @Override 63 | public boolean cancel(final boolean mayInterruptIfRunning) { 64 | throw new UnsupportedOperationException(); 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /api/src/test/java/io/smartcat/spring/cassandra/showcase/test/cassandra/stub/SessionProxy.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.test.cassandra.stub; 2 | 3 | import com.datastax.driver.core.CloseFuture; 4 | import com.datastax.driver.core.Cluster; 5 | import com.datastax.driver.core.PreparedStatement; 6 | import com.datastax.driver.core.RegularStatement; 7 | import com.datastax.driver.core.ResultSet; 8 | import com.datastax.driver.core.ResultSetFuture; 9 | import com.datastax.driver.core.Session; 10 | import com.datastax.driver.core.Statement; 11 | import com.google.common.util.concurrent.ListenableFuture; 12 | 13 | /** 14 | * Session proxy for synchronous execution of queries in order to keep tests 15 | * simple. 16 | * 17 | * Everything except executeAsync is pass-through call to actual session object. 18 | * Only executeAsync calls are wrapped into ResultSetFutureStub with synchronous 19 | * execution result. 20 | */ 21 | public class SessionProxy implements Session { 22 | 23 | private final Session session; 24 | 25 | public SessionProxy(final Session session) { 26 | this.session = session; 27 | } 28 | 29 | @Override 30 | public String getLoggedKeyspace() { 31 | return session.getLoggedKeyspace(); 32 | } 33 | 34 | @Override 35 | public Session init() { 36 | return session.init(); 37 | } 38 | 39 | @Override 40 | public ResultSet execute(final String query) { 41 | return session.execute(query); 42 | } 43 | 44 | @Override 45 | public ResultSet execute(final String query, final Object... values) { 46 | return session.execute(query, values); 47 | } 48 | 49 | @Override 50 | public ResultSet execute(final Statement statement) { 51 | return session.execute(statement); 52 | } 53 | 54 | @Override 55 | public ResultSetFuture executeAsync(final String query) { 56 | // This is executed as sync for testing purposes. 57 | return new ResultSetFutureStub(session.execute(query)); 58 | } 59 | 60 | @Override 61 | public ResultSetFuture executeAsync(final String query, final Object... values) { 62 | // This is executed as sync for testing purposes. 63 | return new ResultSetFutureStub(session.execute(query, values)); 64 | } 65 | 66 | @Override 67 | public ResultSetFuture executeAsync(final Statement statement) { 68 | // This is executed as sync for testing purposes. 69 | return new ResultSetFutureStub(session.execute(statement)); 70 | } 71 | 72 | @Override 73 | public PreparedStatement prepare(final String query) { 74 | return session.prepare(query); 75 | } 76 | 77 | @Override 78 | public PreparedStatement prepare(final RegularStatement statement) { 79 | return session.prepare(statement); 80 | } 81 | 82 | @Override 83 | public ListenableFuture prepareAsync(final String query) { 84 | return session.prepareAsync(query); 85 | } 86 | 87 | @Override 88 | public ListenableFuture prepareAsync(final RegularStatement statement) { 89 | return session.prepareAsync(statement); 90 | } 91 | 92 | @Override 93 | public CloseFuture closeAsync() { 94 | return session.closeAsync(); 95 | } 96 | 97 | @Override 98 | public void close() { 99 | session.close(); 100 | } 101 | 102 | @Override 103 | public boolean isClosed() { 104 | return session.isClosed(); 105 | } 106 | 107 | @Override 108 | public Cluster getCluster() { 109 | return session.getCluster(); 110 | } 111 | 112 | @Override 113 | public State getState() { 114 | return session.getState(); 115 | } 116 | 117 | } 118 | -------------------------------------------------------------------------------- /api/src/test/java/io/smartcat/spring/cassandra/showcase/test/generators/AccountGenerators.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.test.generators; 2 | 3 | import static io.smartcat.spring.cassandra.showcase.test.generators.EmailAddressGenerators.givenAnyEmailAddress; 4 | 5 | import java.net.URI; 6 | import java.util.Arrays; 7 | import java.util.HashSet; 8 | import java.util.Set; 9 | 10 | import io.smartcat.spring.cassandra.showcase.domain.account.Account; 11 | import io.smartcat.spring.cassandra.showcase.domain.account.AccountRole; 12 | 13 | public class AccountGenerators { 14 | 15 | public static String givenAnyLastName() { 16 | return "Doe"; 17 | } 18 | 19 | public static String givenAnyFirstName() { 20 | return "Johnatan"; 21 | } 22 | 23 | public static String givenAnyPassword() { 24 | return "somePassWord"; 25 | } 26 | 27 | public static String givenAnyTwitterId() { 28 | return "twitterId"; 29 | } 30 | 31 | public static Set givenAnyRoles() { 32 | return new HashSet<>(Arrays.asList(AccountRole.ADMINISTRATOR, AccountRole.CUSTOMER)); 33 | } 34 | 35 | public static Set givenRoleAdministrator() { 36 | return new HashSet<>(Arrays.asList(AccountRole.ADMINISTRATOR)); 37 | } 38 | 39 | public static URI givenAnyImageUrl() { 40 | return URI.create("http://www.example.com/some/image.jpg"); 41 | } 42 | 43 | public static Account givenAnyAccount() { 44 | return Account.createNew( 45 | givenAnyEmailAddress(), 46 | givenAnyPassword(), 47 | givenAnyFirstName(), 48 | givenAnyLastName(), 49 | givenAnyImageUrl(), 50 | givenAnyRoles()); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /api/src/test/java/io/smartcat/spring/cassandra/showcase/test/generators/EmailAddressGenerators.java: -------------------------------------------------------------------------------- 1 | package io.smartcat.spring.cassandra.showcase.test.generators; 2 | 3 | import java.util.Random; 4 | 5 | import io.smartcat.spring.cassandra.showcase.domain.account.EmailAddress; 6 | 7 | public class EmailAddressGenerators { 8 | 9 | private static final String[] VALID_ADDRESSES = { 10 | "johnatan@example.com", 11 | "alice@museum.com", 12 | "bob+something@gmail.com", 13 | "jane@foo-bar.net" 14 | }; 15 | 16 | private static final String[] INVALID_ADDRESSES = { 17 | "@", 18 | ".", 19 | "jane@", 20 | "@.com", 21 | "@com", 22 | "joe@localhost" 23 | }; 24 | 25 | public static EmailAddress givenAnyEmailAddress() { 26 | return new EmailAddress("joe@example.com"); 27 | } 28 | 29 | public static String givenAnEmailString() { 30 | return givenAnyEmailString(); 31 | } 32 | 33 | public static String givenAnyEmailString() { 34 | return "joe@example.com"; 35 | } 36 | 37 | public static String[] givenInvalidEmailAddresses() { 38 | return INVALID_ADDRESSES; 39 | } 40 | 41 | public static String[] givenValidEmailAddresses() { 42 | return VALID_ADDRESSES; 43 | } 44 | 45 | public static EmailAddress givenUniqueEmailAddress() { 46 | return new EmailAddress(String.format("someone%d@example.com", new Random().nextInt())); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /api/src/test/resources/cassandra-topology.properties: -------------------------------------------------------------------------------- 1 | #properties file used with PropertyFileSnitch to give topology to cassandra cluster 2 | #this way we can test distributed cluster giving it ip = data center:rack notation for 3 | #each node 4 | 127.0.0.1=DC1:RAC1 5 | -------------------------------------------------------------------------------- /api/src/test/resources/cassandra-unit.yaml: -------------------------------------------------------------------------------- 1 | # Cassandra storage config YAML 2 | 3 | # NOTE: 4 | # See http://wiki.apache.org/cassandra/StorageConfiguration for 5 | # full explanations of configuration directives 6 | # /NOTE 7 | 8 | # The name of the cluster. This is mainly used to prevent machines in 9 | # one logical cluster from joining another. 10 | cluster_name: 'Test Cluster' 11 | 12 | # You should always specify InitialToken when setting up a production 13 | # cluster for the first time, and often when adding capacity later. 14 | # The principle is that each node should be given an equal slice of 15 | # the token ring; see http://wiki.apache.org/cassandra/Operations 16 | # for more details. 17 | # 18 | # If blank, Cassandra will request a token bisecting the range of 19 | # the heaviest-loaded existing node. If there is no load information 20 | # available, such as is the case with a new cluster, it will pick 21 | # a random token, which will lead to hot spots. 22 | initial_token: 23 | 24 | # See http://wiki.apache.org/cassandra/HintedHandoff 25 | hinted_handoff_enabled: true 26 | # this defines the maximum amount of time a dead host will have hints 27 | # generated. After it has been dead this long, hints will be dropped. 28 | max_hint_window_in_ms: 3600000 # one hour 29 | 30 | # DEPRECATED : Sleep this long after delivering each hint 31 | #hinted_handoff_throttle_delay_in_ms: 1 32 | 33 | # authentication backend, implementing IAuthenticator; used to identify users 34 | authenticator: org.apache.cassandra.auth.AllowAllAuthenticator 35 | 36 | # Authorization backend, implementing IAuthorizer; used to limit access/provide permissions 37 | # Out of the box, Cassandra provides org.apache.cassandra.auth.{AllowAllAuthorizer, 38 | # CassandraAuthorizer}. 39 | # 40 | # - AllowAllAuthorizer allows any action to any user - set it to disable authorization. 41 | # - CassandraAuthorizer stores permissions in system_auth.permissions table. Please 42 | # increase system_auth keyspace replication factor if you use this authorizer. 43 | authorizer: org.apache.cassandra.auth.AllowAllAuthorizer 44 | 45 | # The partitioner is responsible for distributing rows (by key) across 46 | # nodes in the cluster. Any IPartitioner may be used, including your 47 | # own as long as it is on the classpath. Out of the box, Cassandra 48 | # provides org.apache.cassandra.dht.RandomPartitioner 49 | # org.apache.cassandra.dht.ByteOrderedPartitioner, 50 | # org.apache.cassandra.dht.OrderPreservingPartitioner (deprecated), 51 | # and org.apache.cassandra.dht.CollatingOrderPreservingPartitioner 52 | # (deprecated). 53 | # 54 | # - RandomPartitioner distributes rows across the cluster evenly by md5. 55 | # When in doubt, this is the best option. 56 | # - ByteOrderedPartitioner orders rows lexically by key bytes. BOP allows 57 | # scanning rows in key order, but the ordering can generate hot spots 58 | # for sequential insertion workloads. 59 | # - OrderPreservingPartitioner is an obsolete form of BOP, that stores 60 | # - keys in a less-efficient format and only works with keys that are 61 | # UTF8-encoded Strings. 62 | # - CollatingOPP colates according to EN,US rules rather than lexical byte 63 | # ordering. Use this as an example if you need custom collation. 64 | # 65 | # See http://wiki.apache.org/cassandra/Operations for more on 66 | # partitioners and token selection. 67 | partitioner: org.apache.cassandra.dht.RandomPartitioner 68 | 69 | # directories where Cassandra should store data on disk. 70 | data_file_directories: 71 | - target/embeddedCassandra/data 72 | 73 | # commit log 74 | commitlog_directory: target/embeddedCassandra/commitlog 75 | 76 | # Maximum size of the key cache in memory. 77 | # 78 | # Each key cache hit saves 1 seek and each row cache hit saves 2 seeks at the 79 | # minimum, sometimes more. The key cache is fairly tiny for the amount of 80 | # time it saves, so it's worthwhile to use it at large numbers. 81 | # The row cache saves even more time, but must store the whole values of 82 | # its rows, so it is extremely space-intensive. It's best to only use the 83 | # row cache if you have hot rows or static rows. 84 | # 85 | # NOTE: if you reduce the size, you may not get you hottest keys loaded on startup. 86 | # 87 | # Default value is empty to make it "auto" (min(5% of Heap (in MB), 100MB)). Set to 0 to disable key cache. 88 | key_cache_size_in_mb: 89 | 90 | # Duration in seconds after which Cassandra should 91 | # safe the keys cache. Caches are saved to saved_caches_directory as 92 | # specified in this configuration file. 93 | # 94 | # Saved caches greatly improve cold-start speeds, and is relatively cheap in 95 | # terms of I/O for the key cache. Row cache saving is much more expensive and 96 | # has limited use. 97 | # 98 | # Default is 14400 or 4 hours. 99 | key_cache_save_period: 14400 100 | 101 | # Number of keys from the key cache to save 102 | # Disabled by default, meaning all keys are going to be saved 103 | # key_cache_keys_to_save: 100 104 | 105 | # Maximum size of the row cache in memory. 106 | # NOTE: if you reduce the size, you may not get you hottest keys loaded on startup. 107 | # 108 | # Default value is 0, to disable row caching. 109 | row_cache_size_in_mb: 0 110 | 111 | # Duration in seconds after which Cassandra should 112 | # safe the row cache. Caches are saved to saved_caches_directory as specified 113 | # in this configuration file. 114 | # 115 | # Saved caches greatly improve cold-start speeds, and is relatively cheap in 116 | # terms of I/O for the key cache. Row cache saving is much more expensive and 117 | # has limited use. 118 | # 119 | # Default is 0 to disable saving the row cache. 120 | row_cache_save_period: 0 121 | 122 | # Number of keys from the row cache to save 123 | # Disabled by default, meaning all keys are going to be saved 124 | # row_cache_keys_to_save: 100 125 | 126 | # saved caches 127 | saved_caches_directory: target/embeddedCassandra/saved_caches 128 | 129 | # commitlog_sync may be either "periodic" or "batch." 130 | # When in batch mode, Cassandra won't ack writes until the commit log 131 | # has been fsynced to disk. It will wait up to 132 | # commitlog_sync_batch_window_in_ms milliseconds for other writes, before 133 | # performing the sync. 134 | # 135 | # commitlog_sync: batch 136 | # commitlog_sync_batch_window_in_ms: 50 137 | # 138 | # the other option is "periodic" where writes may be acked immediately 139 | # and the CommitLog is simply synced every commitlog_sync_period_in_ms 140 | # milliseconds. 141 | commitlog_sync: periodic 142 | commitlog_sync_period_in_ms: 10000 143 | 144 | # any class that implements the SeedProvider interface and has a 145 | # constructor that takes a Map of parameters will do. 146 | seed_provider: 147 | # Addresses of hosts that are deemed contact points. 148 | # Cassandra nodes use this list of hosts to find each other and learn 149 | # the topology of the ring. You must change this if you are running 150 | # multiple nodes! 151 | - class_name: org.apache.cassandra.locator.SimpleSeedProvider 152 | parameters: 153 | # seeds is actually a comma-delimited list of addresses. 154 | # Ex: ",," 155 | - seeds: "127.0.0.1" 156 | 157 | # For workloads with more data than can fit in memory, Cassandra's 158 | # bottleneck will be reads that need to fetch data from 159 | # disk. "concurrent_reads" should be set to (16 * number_of_drives) in 160 | # order to allow the operations to enqueue low enough in the stack 161 | # that the OS and drives can reorder them. 162 | # 163 | # On the other hand, since writes are almost never IO bound, the ideal 164 | # number of "concurrent_writes" is dependent on the number of cores in 165 | # your system; (8 * number_of_cores) is a good rule of thumb. 166 | concurrent_reads: 32 167 | concurrent_writes: 32 168 | 169 | # Total memory to use for memtables. Cassandra will flush the largest 170 | # memtable when this much memory is used. 171 | # If omitted, Cassandra will set it to 1/3 of the heap. 172 | # memtable_total_space_in_mb: 2048 173 | 174 | # Total space to use for commitlogs. 175 | # If space gets above this value (it will round up to the next nearest 176 | # segment multiple), Cassandra will flush every dirty CF in the oldest 177 | # segment and remove it. 178 | # commitlog_total_space_in_mb: 4096 179 | 180 | # This sets the amount of memtable flush writer threads. These will 181 | # be blocked by disk io, and each one will hold a memtable in memory 182 | # while blocked. If you have a large heap and many data directories, 183 | # you can increase this value for better flush performance. 184 | # By default this will be set to the amount of data directories defined. 185 | #memtable_flush_writers: 1 186 | 187 | # the number of full memtables to allow pending flush, that is, 188 | # waiting for a writer thread. At a minimum, this should be set to 189 | # the maximum number of secondary indexes created on a single CF. 190 | # memtable_flush_queue_size: 4 191 | 192 | # Whether to, when doing sequential writing, fsync() at intervals in 193 | # order to force the operating system to flush the dirty 194 | # buffers. Enable this to avoid sudden dirty buffer flushing from 195 | # impacting read latencies. Almost always a good idea on SSD:s; not 196 | # necessarily on platters. 197 | trickle_fsync: false 198 | trickle_fsync_interval_in_kb: 10240 199 | 200 | # TCP port, for commands and data 201 | storage_port: 7010 202 | 203 | # SSL port, for encrypted communication. Unused unless enabled in 204 | # encryption_options 205 | ssl_storage_port: 7011 206 | 207 | # Address to bind to and tell other Cassandra nodes to connect to. You 208 | # _must_ change this if you want multiple nodes to be able to 209 | # communicate! 210 | # 211 | # Leaving it blank leaves it up to InetAddress.getLocalHost(). This 212 | # will always do the Right Thing *if* the node is properly configured 213 | # (hostname, name resolution, etc), and the Right Thing is to use the 214 | # address associated with the hostname (it might not be). 215 | # 216 | # Setting this to 0.0.0.0 is always wrong. 217 | listen_address: 127.0.0.1 218 | 219 | # Address to broadcast to other Cassandra nodes 220 | # Leaving this blank will set it to the same value as listen_address 221 | # broadcast_address: 1.2.3.4 222 | 223 | # The address to bind the Thrift RPC service to -- clients connect 224 | # here. Unlike ListenAddress above, you *can* specify 0.0.0.0 here if 225 | # you want Thrift to listen on all interfaces. 226 | # 227 | # Leaving this blank has the same effect it does for ListenAddress, 228 | # (i.e. it will be based on the configured hostname of the node). 229 | rpc_address: localhost 230 | # port for Thrift to listen for clients on 231 | rpc_port: 9171 232 | 233 | start_native_transport: true 234 | # port for the CQL native transport to listen for clients on 235 | native_transport_port: 9342 236 | 237 | # enable or disable keepalive on rpc connections 238 | rpc_keepalive: true 239 | 240 | # Cassandra provides three options for the RPC Server: 241 | # 242 | # sync -> One connection per thread in the rpc pool (see below). 243 | # For a very large number of clients, memory will be your limiting 244 | # factor; on a 64 bit JVM, 128KB is the minimum stack size per thread. 245 | # Connection pooling is very, very strongly recommended. 246 | # 247 | # async -> Nonblocking server implementation with one thread to serve 248 | # rpc connections. This is not recommended for high throughput use 249 | # cases. Async has been tested to be about 50% slower than sync 250 | # or hsha and is deprecated: it will be removed in the next major release. 251 | # 252 | # hsha -> Stands for "half synchronous, half asynchronous." The rpc thread pool 253 | # (see below) is used to manage requests, but the threads are multiplexed 254 | # across the different clients. 255 | # 256 | # The default is sync because on Windows hsha is about 30% slower. On Linux, 257 | # sync/hsha performance is about the same, with hsha of course using less memory. 258 | rpc_server_type: sync 259 | 260 | # Uncomment rpc_min|max|thread to set request pool size. 261 | # You would primarily set max for the sync server to safeguard against 262 | # misbehaved clients; if you do hit the max, Cassandra will block until one 263 | # disconnects before accepting more. The defaults for sync are min of 16 and max 264 | # unlimited. 265 | # 266 | # For the Hsha server, the min and max both default to quadruple the number of 267 | # CPU cores. 268 | # 269 | # This configuration is ignored by the async server. 270 | # 271 | # rpc_min_threads: 16 272 | # rpc_max_threads: 2048 273 | 274 | # uncomment to set socket buffer sizes on rpc connections 275 | # rpc_send_buff_size_in_bytes: 276 | # rpc_recv_buff_size_in_bytes: 277 | 278 | # Frame size for thrift (maximum field length). 279 | # 0 disables TFramedTransport in favor of TSocket. This option 280 | # is deprecated; we strongly recommend using Framed mode. 281 | thrift_framed_transport_size_in_mb: 15 282 | 283 | # The max length of a thrift message, including all fields and 284 | # internal thrift overhead. 285 | thrift_max_message_length_in_mb: 16 286 | 287 | # Set to true to have Cassandra create a hard link to each sstable 288 | # flushed or streamed locally in a backups/ subdirectory of the 289 | # Keyspace data. Removing these links is the operator's 290 | # responsibility. 291 | incremental_backups: false 292 | 293 | # Whether or not to take a snapshot before each compaction. Be 294 | # careful using this option, since Cassandra won't clean up the 295 | # snapshots for you. Mostly useful if you're paranoid when there 296 | # is a data format change. 297 | snapshot_before_compaction: false 298 | 299 | # Whether or not a snapshot is taken of the data before keyspace truncation 300 | # or dropping of column families. The STRONGLY advised default of true 301 | # should be used to provide data safety. If you set this flag to false, you will 302 | # lose data on truncation or drop. 303 | auto_snapshot: false 304 | 305 | # Add column indexes to a row after its contents reach this size. 306 | # Increase if your column values are large, or if you have a very large 307 | # number of columns. The competing causes are, Cassandra has to 308 | # deserialize this much of the row to read a single column, so you want 309 | # it to be small - at least if you do many partial-row reads - but all 310 | # the index data is read for each access, so you don't want to generate 311 | # that wastefully either. 312 | column_index_size_in_kb: 64 313 | 314 | # Size limit for rows being compacted in memory. Larger rows will spill 315 | # over to disk and use a slower two-pass compaction process. A message 316 | # will be logged specifying the row key. 317 | # in_memory_compaction_limit_in_mb: 64 318 | 319 | # Number of simultaneous compactions to allow, NOT including 320 | # validation "compactions" for anti-entropy repair. Simultaneous 321 | # compactions can help preserve read performance in a mixed read/write 322 | # workload, by mitigating the tendency of small sstables to accumulate 323 | # during a single long running compactions. The default is usually 324 | # fine and if you experience problems with compaction running too 325 | # slowly or too fast, you should look at 326 | # compaction_throughput_mb_per_sec first. 327 | # 328 | # This setting has no effect on LeveledCompactionStrategy. 329 | # 330 | # concurrent_compactors defaults to the number of cores. 331 | # Uncomment to make compaction mono-threaded, the pre-0.8 default. 332 | #concurrent_compactors: 1 333 | 334 | # Multi-threaded compaction. When enabled, each compaction will use 335 | # up to one thread per core, plus one thread per sstable being merged. 336 | # This is usually only useful for SSD-based hardware: otherwise, 337 | # your concern is usually to get compaction to do LESS i/o (see: 338 | # compaction_throughput_mb_per_sec), not more. 339 | # multithreaded_compaction: false 340 | 341 | # Throttles compaction to the given total throughput across the entire 342 | # system. The faster you insert data, the faster you need to compact in 343 | # order to keep the sstable count down, but in general, setting this to 344 | # 16 to 32 times the rate you are inserting data is more than sufficient. 345 | # Setting this to 0 disables throttling. Note that this account for all types 346 | # of compaction, including validation compaction. 347 | compaction_throughput_mb_per_sec: 16 348 | 349 | # Track cached row keys during compaction, and re-cache their new 350 | # positions in the compacted sstable. Disable if you use really large 351 | # key caches. 352 | # compaction_preheat_key_cache: true 353 | 354 | # Throttles all outbound streaming file transfers on this node to the 355 | # given total throughput in Mbps. This is necessary because Cassandra does 356 | # mostly sequential IO when streaming data during bootstrap or repair, which 357 | # can lead to saturating the network connection and degrading rpc performance. 358 | # When unset, the default is 400 Mbps or 50 MB/s. 359 | # stream_throughput_outbound_megabits_per_sec: 400 360 | 361 | # DEPRECATED : Time to wait for a reply from other nodes before failing the command 362 | #rpc_timeout_in_ms: 10000 363 | 364 | # Enable socket timeout for streaming operation. 365 | # When a timeout occurs during streaming, streaming is retried from the start 366 | # of the current file. This *can* involve re-streaming an important amount of 367 | # data, so you should avoid setting the value too low. 368 | # Default value is 0, which never timeout streams. 369 | # streaming_socket_timeout_in_ms: 0 370 | 371 | # phi value that must be reached for a host to be marked down. 372 | # most users should never need to adjust this. 373 | # phi_convict_threshold: 8 374 | 375 | # endpoint_snitch -- Set this to a class that implements 376 | # IEndpointSnitch. The snitch has two functions: 377 | # - it teaches Cassandra enough about your network topology to route 378 | # requests efficiently 379 | # - it allows Cassandra to spread replicas around your cluster to avoid 380 | # correlated failures. It does this by grouping machines into 381 | # "datacenters" and "racks." Cassandra will do its best not to have 382 | # more than one replica on the same "rack" (which may not actually 383 | # be a physical location) 384 | # 385 | # IF YOU CHANGE THE SNITCH AFTER DATA IS INSERTED INTO THE CLUSTER, 386 | # YOU MUST RUN A FULL REPAIR, SINCE THE SNITCH AFFECTS WHERE REPLICAS 387 | # ARE PLACED. 388 | # 389 | # Out of the box, Cassandra provides 390 | # - SimpleSnitch: 391 | # Treats Strategy order as proximity. This improves cache locality 392 | # when disabling read repair, which can further improve throughput. 393 | # Only appropriate for single-datacenter deployments. 394 | # - PropertyFileSnitch: 395 | # Proximity is determined by rack and data center, which are 396 | # explicitly configured in cassandra-topology.properties. 397 | # - RackInferringSnitch: 398 | # Proximity is determined by rack and data center, which are 399 | # assumed to correspond to the 3rd and 2nd octet of each node's 400 | # IP address, respectively. Unless this happens to match your 401 | # deployment conventions (as it did Facebook's), this is best used 402 | # as an example of writing a custom Snitch class. 403 | # - Ec2Snitch: 404 | # Appropriate for EC2 deployments in a single Region. Loads Region 405 | # and Availability Zone information from the EC2 API. The Region is 406 | # treated as the Datacenter, and the Availability Zone as the rack. 407 | # Only private IPs are used, so this will not work across multiple 408 | # Regions. 409 | # - Ec2MultiRegionSnitch: 410 | # Uses public IPs as broadcast_address to allow cross-region 411 | # connectivity. (Thus, you should set seed addresses to the public 412 | # IP as well.) You will need to open the storage_port or 413 | # ssl_storage_port on the public IP firewall. (For intra-Region 414 | # traffic, Cassandra will switch to the private IP after 415 | # establishing a connection.) 416 | # 417 | # You can use a custom Snitch by setting this to the full class name 418 | # of the snitch, which will be assumed to be on your classpath. 419 | endpoint_snitch: PropertyFileSnitch 420 | 421 | # controls how often to perform the more expensive part of host score 422 | # calculation 423 | dynamic_snitch_update_interval_in_ms: 100 424 | # controls how often to reset all host scores, allowing a bad host to 425 | # possibly recover 426 | dynamic_snitch_reset_interval_in_ms: 600000 427 | # if set greater than zero and read_repair_chance is < 1.0, this will allow 428 | # 'pinning' of replicas to hosts in order to increase cache capacity. 429 | # The badness threshold will control how much worse the pinned host has to be 430 | # before the dynamic snitch will prefer other replicas over it. This is 431 | # expressed as a double which represents a percentage. Thus, a value of 432 | # 0.2 means Cassandra would continue to prefer the static snitch values 433 | # until the pinned host was 20% worse than the fastest. 434 | dynamic_snitch_badness_threshold: 0.1 435 | 436 | # request_scheduler -- Set this to a class that implements 437 | # RequestScheduler, which will schedule incoming client requests 438 | # according to the specific policy. This is useful for multi-tenancy 439 | # with a single Cassandra cluster. 440 | # NOTE: This is specifically for requests from the client and does 441 | # not affect inter node communication. 442 | # org.apache.cassandra.scheduler.NoScheduler - No scheduling takes place 443 | # org.apache.cassandra.scheduler.RoundRobinScheduler - Round robin of 444 | # client requests to a node with a separate queue for each 445 | # request_scheduler_id. The scheduler is further customized by 446 | # request_scheduler_options as described below. 447 | request_scheduler: org.apache.cassandra.scheduler.NoScheduler 448 | 449 | # Scheduler Options vary based on the type of scheduler 450 | # NoScheduler - Has no options 451 | # RoundRobin 452 | # - throttle_limit -- The throttle_limit is the number of in-flight 453 | # requests per client. Requests beyond 454 | # that limit are queued up until 455 | # running requests can complete. 456 | # The value of 80 here is twice the number of 457 | # concurrent_reads + concurrent_writes. 458 | # - default_weight -- default_weight is optional and allows for 459 | # overriding the default which is 1. 460 | # - weights -- Weights are optional and will default to 1 or the 461 | # overridden default_weight. The weight translates into how 462 | # many requests are handled during each turn of the 463 | # RoundRobin, based on the scheduler id. 464 | # 465 | # request_scheduler_options: 466 | # throttle_limit: 80 467 | # default_weight: 5 468 | # weights: 469 | # Keyspace1: 1 470 | # Keyspace2: 5 471 | 472 | # request_scheduler_id -- An identifer based on which to perform 473 | # the request scheduling. Currently the only valid option is keyspace. 474 | # request_scheduler_id: keyspace 475 | 476 | # index_interval controls the sampling of entries from the primrary 477 | # row index in terms of space versus time. The larger the interval, 478 | # the smaller and less effective the sampling will be. In technicial 479 | # terms, the interval coresponds to the number of index entries that 480 | # are skipped between taking each sample. All the sampled entries 481 | # must fit in memory. Generally, a value between 128 and 512 here 482 | # coupled with a large key cache size on CFs results in the best trade 483 | # offs. This value is not often changed, however if you have many 484 | # very small rows (many to an OS page), then increasing this will 485 | # often lower memory usage without a impact on performance. 486 | index_interval: 128 487 | 488 | # Enable or disable inter-node encryption 489 | # Default settings are TLS v1, RSA 1024-bit keys (it is imperative that 490 | # users generate their own keys) TLS_RSA_WITH_AES_128_CBC_SHA as the cipher 491 | # suite for authentication, key exchange and encryption of the actual data transfers. 492 | # NOTE: No custom encryption options are enabled at the moment 493 | # The available internode options are : all, none, dc, rack 494 | # 495 | # If set to dc cassandra will encrypt the traffic between the DCs 496 | # If set to rack cassandra will encrypt the traffic between the racks 497 | # 498 | # The passwords used in these options must match the passwords used when generating 499 | # the keystore and truststore. For instructions on generating these files, see: 500 | # http://download.oracle.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html#CreateKeystore 501 | # 502 | server_encryption_options: 503 | internode_encryption: none 504 | keystore: conf/.keystore 505 | keystore_password: cassandra 506 | truststore: conf/.truststore 507 | truststore_password: cassandra 508 | # More advanced defaults below: 509 | # protocol: TLS 510 | # algorithm: SunX509 511 | # store_type: JKS 512 | # cipher_suites: [TLS_RSA_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_256_CBC_SHA] 513 | 514 | # Tombstone warning threshold 515 | tombstone_warn_threshold: 2000 516 | -------------------------------------------------------------------------------- /api/src/test/resources/db.cql: -------------------------------------------------------------------------------- 1 | /* 2 | Create keyspace with Network Topology. This is more similar with keyspaces created on production 3 | servers. Also using lightweight transaction (ifNotExists) does not seem to work together with 4 | SimpleStrategy and consistency level EACH_QUORUM. 5 | */ 6 | CREATE KEYSPACE smartcatshowcase_test WITH replication = { 'class': 'NetworkTopologyStrategy', 'DC1': '1' }; 7 | USE smartcatshowcase_test; 8 | 9 | /* Account */ 10 | CREATE TABLE account_by_email ( 11 | email_address text, 12 | account_password text, 13 | first_name text, 14 | last_name text, 15 | registered_at timestamp, 16 | roles set, 17 | profile_image_url text, 18 | PRIMARY KEY (email_address) 19 | ) WITH COMMENT='Accounts in system by email.'; 20 | 21 | CREATE TABLE account_by_external_source ( 22 | external_source_id text, 23 | external_source text, 24 | email_address text, 25 | PRIMARY KEY ((external_source_id, external_source)) 26 | ) WITH COMMENT='Account email by external source.'; 27 | -------------------------------------------------------------------------------- /api/src/test/resources/logback-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /common.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext.versions.assertJ = '3.0.0' 3 | ext.versions.jackson = '2.5.3' 4 | 5 | repositories { 6 | mavenCentral() 7 | } 8 | } 9 | 10 | apply plugin: 'java' 11 | apply plugin: 'groovy' 12 | apply plugin: 'application' 13 | 14 | sourceCompatibility = 1.8 15 | 16 | task wrapper(type: Wrapper) { 17 | gradleVersion = '2.4' 18 | } 19 | 20 | tasks.withType(JavaCompile) { 21 | options.encoding = 'UTF-8' 22 | } 23 | -------------------------------------------------------------------------------- /common/gradle/common.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | mavenCentral() 4 | } 5 | } 6 | 7 | project.ext.versions = [ 8 | assertJ: '3.0.0', 9 | guava: '18.0', 10 | jackson: '2.5.3', 11 | cassandra: '2.1.6', 12 | cucumber: '1.2.3', 13 | springBoot: '1.2.3.RELEASE' 14 | ] 15 | 16 | apply plugin: 'java' 17 | apply plugin: 'groovy' 18 | apply plugin: 'application' 19 | 20 | sourceCompatibility = 1.8 21 | 22 | task wrapper(type: Wrapper) { 23 | gradleVersion = '2.4' 24 | } 25 | 26 | tasks.withType(JavaCompile) { 27 | options.encoding = 'UTF-8' 28 | } 29 | --------------------------------------------------------------------------------