├── README.md
└── social-multiplication
├── .gitignore
├── .mvn
└── wrapper
│ ├── maven-wrapper.jar
│ └── maven-wrapper.properties
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
├── main
├── java
│ └── microservices
│ │ └── book
│ │ ├── SocialMultiplicationApplication.java
│ │ └── multiplication
│ │ ├── controller
│ │ ├── MultiplicationController.java
│ │ └── MultiplicationResultAttemptController.java
│ │ ├── domain
│ │ ├── Multiplication.java
│ │ ├── MultiplicationResultAttempt.java
│ │ └── User.java
│ │ └── service
│ │ ├── MultiplicationService.java
│ │ ├── MultiplicationServiceImpl.java
│ │ ├── RandomGeneratorService.java
│ │ └── RandomGeneratorServiceImpl.java
└── resources
│ ├── application.properties
│ └── static
│ ├── index.html
│ ├── multiplication-client.js
│ └── styles.css
└── test
└── java
└── microservices
└── book
└── multiplication
├── controller
├── MultiplicationControllerTest.java
└── MultiplicationResultAttemptControllerTest.java
└── service
├── MultiplicationServiceImplTest.java
└── RandomGeneratorServiceImplTest.java
/README.md:
--------------------------------------------------------------------------------
1 | # Learn Microservices with Spring Boot - v3
2 |
3 | This project contains the version 3 of the application that is developed under the scope of the book *Learn Microservices with Spring Boot*. You can get a copy of the book on [Amazon](http://amzn.to/2FSB2ME) or [Apress](http://www.apress.com/book/9781484231647).
4 |
5 | The book shows you how to evolve a simple Spring Boot application to become a full Microservices Architecture, using Spring Cloud Eureka, Ribbon, Zuul and Hystrix to implement Service Discovery, Load Balancing, the API Gateway pattern and a Circuit Breaker. Besides, you'll learn how to implement End-to-End tests with Cucumber, an Event-Driven system and the best practices when building Microservices.
6 |
7 | ## About this version
8 |
9 | This is the first version in which we have a working social-multiplication application.
10 | You can play with it by running the Spring Boot application and navigating with your browser to
11 | `http://localhost:8080/index.html`.
12 |
13 | Our social-multiplication application follows now a 3 layered design, and contains unit tests for the *service* and
14 | *controller* layers.
15 |
--------------------------------------------------------------------------------
/social-multiplication/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | !.mvn/wrapper/maven-wrapper.jar
3 |
4 | ### STS ###
5 | .apt_generated
6 | .classpath
7 | .factorypath
8 | .project
9 | .settings
10 | .springBeans
11 |
12 | ### IntelliJ IDEA ###
13 | .idea
14 | *.iws
15 | *.iml
16 | *.ipr
17 |
18 | ### NetBeans ###
19 | nbproject/private/
20 | build/
21 | nbbuild/
22 | dist/
23 | nbdist/
24 | .nb-gradle/
--------------------------------------------------------------------------------
/social-multiplication/.mvn/wrapper/maven-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/microservices-practical/microservices-v3/957498f0b6bc48629fed9c5f7581d8a7b5080319/social-multiplication/.mvn/wrapper/maven-wrapper.jar
--------------------------------------------------------------------------------
/social-multiplication/.mvn/wrapper/maven-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip
2 |
--------------------------------------------------------------------------------
/social-multiplication/mvnw:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | # ----------------------------------------------------------------------------
3 | # Licensed to the Apache Software Foundation (ASF) under one
4 | # or more contributor license agreements. See the NOTICE file
5 | # distributed with this work for additional information
6 | # regarding copyright ownership. The ASF licenses this file
7 | # to you under the Apache License, Version 2.0 (the
8 | # "License"); you may not use this file except in compliance
9 | # with the License. You may obtain a copy of the License at
10 | #
11 | # http://www.apache.org/licenses/LICENSE-2.0
12 | #
13 | # Unless required by applicable law or agreed to in writing,
14 | # software distributed under the License is distributed on an
15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | # KIND, either express or implied. See the License for the
17 | # specific language governing permissions and limitations
18 | # under the License.
19 | # ----------------------------------------------------------------------------
20 |
21 | # ----------------------------------------------------------------------------
22 | # Maven2 Start Up Batch script
23 | #
24 | # Required ENV vars:
25 | # ------------------
26 | # JAVA_HOME - location of a JDK home dir
27 | #
28 | # Optional ENV vars
29 | # -----------------
30 | # M2_HOME - location of maven2's installed home dir
31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven
32 | # e.g. to debug Maven itself, use
33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files
35 | # ----------------------------------------------------------------------------
36 |
37 | if [ -z "$MAVEN_SKIP_RC" ] ; then
38 |
39 | if [ -f /etc/mavenrc ] ; then
40 | . /etc/mavenrc
41 | fi
42 |
43 | if [ -f "$HOME/.mavenrc" ] ; then
44 | . "$HOME/.mavenrc"
45 | fi
46 |
47 | fi
48 |
49 | # OS specific support. $var _must_ be set to either true or false.
50 | cygwin=false;
51 | darwin=false;
52 | mingw=false
53 | case "`uname`" in
54 | CYGWIN*) cygwin=true ;;
55 | MINGW*) mingw=true;;
56 | Darwin*) darwin=true
57 | #
58 | # Look for the Apple JDKs first to preserve the existing behaviour, and then look
59 | # for the new JDKs provided by Oracle.
60 | #
61 | if [ -z "$JAVA_HOME" ] && [ -L /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK ] ; then
62 | #
63 | # Apple JDKs
64 | #
65 | export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home
66 | fi
67 |
68 | if [ -z "$JAVA_HOME" ] && [ -L /System/Library/Java/JavaVirtualMachines/CurrentJDK ] ; then
69 | #
70 | # Apple JDKs
71 | #
72 | export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home
73 | fi
74 |
75 | if [ -z "$JAVA_HOME" ] && [ -L "/Library/Java/JavaVirtualMachines/CurrentJDK" ] ; then
76 | #
77 | # Oracle JDKs
78 | #
79 | export JAVA_HOME=/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home
80 | fi
81 |
82 | if [ -z "$JAVA_HOME" ] && [ -x "/usr/libexec/java_home" ]; then
83 | #
84 | # Apple JDKs
85 | #
86 | export JAVA_HOME=`/usr/libexec/java_home`
87 | fi
88 | ;;
89 | esac
90 |
91 | if [ -z "$JAVA_HOME" ] ; then
92 | if [ -r /etc/gentoo-release ] ; then
93 | JAVA_HOME=`java-config --jre-home`
94 | fi
95 | fi
96 |
97 | if [ -z "$M2_HOME" ] ; then
98 | ## resolve links - $0 may be a link to maven's home
99 | PRG="$0"
100 |
101 | # need this for relative symlinks
102 | while [ -h "$PRG" ] ; do
103 | ls=`ls -ld "$PRG"`
104 | link=`expr "$ls" : '.*-> \(.*\)$'`
105 | if expr "$link" : '/.*' > /dev/null; then
106 | PRG="$link"
107 | else
108 | PRG="`dirname "$PRG"`/$link"
109 | fi
110 | done
111 |
112 | saveddir=`pwd`
113 |
114 | M2_HOME=`dirname "$PRG"`/..
115 |
116 | # make it fully qualified
117 | M2_HOME=`cd "$M2_HOME" && pwd`
118 |
119 | cd "$saveddir"
120 | # echo Using m2 at $M2_HOME
121 | fi
122 |
123 | # For Cygwin, ensure paths are in UNIX format before anything is touched
124 | if $cygwin ; then
125 | [ -n "$M2_HOME" ] &&
126 | M2_HOME=`cygpath --unix "$M2_HOME"`
127 | [ -n "$JAVA_HOME" ] &&
128 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
129 | [ -n "$CLASSPATH" ] &&
130 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
131 | fi
132 |
133 | # For Migwn, ensure paths are in UNIX format before anything is touched
134 | if $mingw ; then
135 | [ -n "$M2_HOME" ] &&
136 | M2_HOME="`(cd "$M2_HOME"; pwd)`"
137 | [ -n "$JAVA_HOME" ] &&
138 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`"
139 | # TODO classpath?
140 | fi
141 |
142 | if [ -z "$JAVA_HOME" ]; then
143 | javaExecutable="`which javac`"
144 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then
145 | # readlink(1) is not available as standard on Solaris 10.
146 | readLink=`which readlink`
147 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then
148 | if $darwin ; then
149 | javaHome="`dirname \"$javaExecutable\"`"
150 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac"
151 | else
152 | javaExecutable="`readlink -f \"$javaExecutable\"`"
153 | fi
154 | javaHome="`dirname \"$javaExecutable\"`"
155 | javaHome=`expr "$javaHome" : '\(.*\)/bin'`
156 | JAVA_HOME="$javaHome"
157 | export JAVA_HOME
158 | fi
159 | fi
160 | fi
161 |
162 | if [ -z "$JAVACMD" ] ; then
163 | if [ -n "$JAVA_HOME" ] ; then
164 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
165 | # IBM's JDK on AIX uses strange locations for the executables
166 | JAVACMD="$JAVA_HOME/jre/sh/java"
167 | else
168 | JAVACMD="$JAVA_HOME/bin/java"
169 | fi
170 | else
171 | JAVACMD="`which java`"
172 | fi
173 | fi
174 |
175 | if [ ! -x "$JAVACMD" ] ; then
176 | echo "Error: JAVA_HOME is not defined correctly." >&2
177 | echo " We cannot execute $JAVACMD" >&2
178 | exit 1
179 | fi
180 |
181 | if [ -z "$JAVA_HOME" ] ; then
182 | echo "Warning: JAVA_HOME environment variable is not set."
183 | fi
184 |
185 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher
186 |
187 | # For Cygwin, switch paths to Windows format before running java
188 | if $cygwin; then
189 | [ -n "$M2_HOME" ] &&
190 | M2_HOME=`cygpath --path --windows "$M2_HOME"`
191 | [ -n "$JAVA_HOME" ] &&
192 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
193 | [ -n "$CLASSPATH" ] &&
194 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
195 | fi
196 |
197 | # traverses directory structure from process work directory to filesystem root
198 | # first directory with .mvn subdirectory is considered project base directory
199 | find_maven_basedir() {
200 | local basedir=$(pwd)
201 | local wdir=$(pwd)
202 | while [ "$wdir" != '/' ] ; do
203 | if [ -d "$wdir"/.mvn ] ; then
204 | basedir=$wdir
205 | break
206 | fi
207 | wdir=$(cd "$wdir/.."; pwd)
208 | done
209 | echo "${basedir}"
210 | }
211 |
212 | # concatenates all lines of a file
213 | concat_lines() {
214 | if [ -f "$1" ]; then
215 | echo "$(tr -s '\n' ' ' < "$1")"
216 | fi
217 | }
218 |
219 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-$(find_maven_basedir)}
220 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS"
221 |
222 | # Provide a "standardized" way to retrieve the CLI args that will
223 | # work with both Windows and non-Windows executions.
224 | MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@"
225 | export MAVEN_CMD_LINE_ARGS
226 |
227 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
228 |
229 | exec "$JAVACMD" \
230 | $MAVEN_OPTS \
231 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \
232 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
233 | ${WRAPPER_LAUNCHER} "$@"
234 |
--------------------------------------------------------------------------------
/social-multiplication/mvnw.cmd:
--------------------------------------------------------------------------------
1 | @REM ----------------------------------------------------------------------------
2 | @REM Licensed to the Apache Software Foundation (ASF) under one
3 | @REM or more contributor license agreements. See the NOTICE file
4 | @REM distributed with this work for additional information
5 | @REM regarding copyright ownership. The ASF licenses this file
6 | @REM to you under the Apache License, Version 2.0 (the
7 | @REM "License"); you may not use this file except in compliance
8 | @REM with the License. You may obtain a copy of the License at
9 | @REM
10 | @REM http://www.apache.org/licenses/LICENSE-2.0
11 | @REM
12 | @REM Unless required by applicable law or agreed to in writing,
13 | @REM software distributed under the License is distributed on an
14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | @REM KIND, either express or implied. See the License for the
16 | @REM specific language governing permissions and limitations
17 | @REM under the License.
18 | @REM ----------------------------------------------------------------------------
19 |
20 | @REM ----------------------------------------------------------------------------
21 | @REM Maven2 Start Up Batch script
22 | @REM
23 | @REM Required ENV vars:
24 | @REM JAVA_HOME - location of a JDK home dir
25 | @REM
26 | @REM Optional ENV vars
27 | @REM M2_HOME - location of maven2's installed home dir
28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands
29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending
30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven
31 | @REM e.g. to debug Maven itself, use
32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files
34 | @REM ----------------------------------------------------------------------------
35 |
36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on'
37 | @echo off
38 | @REM enable echoing my setting MAVEN_BATCH_ECHO to 'on'
39 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO%
40 |
41 | @REM set %HOME% to equivalent of $HOME
42 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%")
43 |
44 | @REM Execute a user defined script before this one
45 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre
46 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending
47 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat"
48 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd"
49 | :skipRcPre
50 |
51 | @setlocal
52 |
53 | set ERROR_CODE=0
54 |
55 | @REM To isolate internal variables from possible post scripts, we use another setlocal
56 | @setlocal
57 |
58 | @REM ==== START VALIDATION ====
59 | if not "%JAVA_HOME%" == "" goto OkJHome
60 |
61 | echo.
62 | echo Error: JAVA_HOME not found in your environment. >&2
63 | echo Please set the JAVA_HOME variable in your environment to match the >&2
64 | echo location of your Java installation. >&2
65 | echo.
66 | goto error
67 |
68 | :OkJHome
69 | if exist "%JAVA_HOME%\bin\java.exe" goto init
70 |
71 | echo.
72 | echo Error: JAVA_HOME is set to an invalid directory. >&2
73 | echo JAVA_HOME = "%JAVA_HOME%" >&2
74 | echo Please set the JAVA_HOME variable in your environment to match the >&2
75 | echo location of your Java installation. >&2
76 | echo.
77 | goto error
78 |
79 | @REM ==== END VALIDATION ====
80 |
81 | :init
82 |
83 | set MAVEN_CMD_LINE_ARGS=%*
84 |
85 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn".
86 | @REM Fallback to current working directory if not found.
87 |
88 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR%
89 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir
90 |
91 | set EXEC_DIR=%CD%
92 | set WDIR=%EXEC_DIR%
93 | :findBaseDir
94 | IF EXIST "%WDIR%"\.mvn goto baseDirFound
95 | cd ..
96 | IF "%WDIR%"=="%CD%" goto baseDirNotFound
97 | set WDIR=%CD%
98 | goto findBaseDir
99 |
100 | :baseDirFound
101 | set MAVEN_PROJECTBASEDIR=%WDIR%
102 | cd "%EXEC_DIR%"
103 | goto endDetectBaseDir
104 |
105 | :baseDirNotFound
106 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR%
107 | cd "%EXEC_DIR%"
108 |
109 | :endDetectBaseDir
110 |
111 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig
112 |
113 | @setlocal EnableExtensions EnableDelayedExpansion
114 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a
115 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS%
116 |
117 | :endReadAdditionalConfig
118 |
119 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe"
120 |
121 | set WRAPPER_JAR="".\.mvn\wrapper\maven-wrapper.jar""
122 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
123 |
124 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CMD_LINE_ARGS%
125 | if ERRORLEVEL 1 goto error
126 | goto end
127 |
128 | :error
129 | set ERROR_CODE=1
130 |
131 | :end
132 | @endlocal & set ERROR_CODE=%ERROR_CODE%
133 |
134 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost
135 | @REM check for post script, once with legacy .bat ending and once with .cmd ending
136 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat"
137 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd"
138 | :skipRcPost
139 |
140 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on'
141 | if "%MAVEN_BATCH_PAUSE%" == "on" pause
142 |
143 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE%
144 |
145 | exit /B %ERROR_CODE%
--------------------------------------------------------------------------------
/social-multiplication/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | microservices.book
7 | social-multiplication-v3
8 | 0.3.0-SNAPSHOT
9 | jar
10 |
11 | social-multiplication-v3
12 | Social Multiplication App (Learn Microservices with Spring Boot)
13 |
14 |
15 | org.springframework.boot
16 | spring-boot-starter-parent
17 | 1.5.7.RELEASE
18 |
19 |
20 |
21 |
22 | UTF-8
23 | UTF-8
24 | 1.8
25 |
26 |
27 |
28 |
29 | org.springframework.boot
30 | spring-boot-starter-web
31 |
32 |
33 |
34 |
35 | org.projectlombok
36 | lombok
37 | 1.16.18
38 | provided
39 |
40 |
41 |
42 |
43 | org.springframework.boot
44 | spring-boot-starter-test
45 | test
46 |
47 |
48 |
49 |
50 |
51 |
52 | org.springframework.boot
53 | spring-boot-maven-plugin
54 |
55 |
56 |
57 |
58 |
59 |
60 |
--------------------------------------------------------------------------------
/social-multiplication/src/main/java/microservices/book/SocialMultiplicationApplication.java:
--------------------------------------------------------------------------------
1 | package microservices.book;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class SocialMultiplicationApplication {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(SocialMultiplicationApplication.class, args);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/social-multiplication/src/main/java/microservices/book/multiplication/controller/MultiplicationController.java:
--------------------------------------------------------------------------------
1 | package microservices.book.multiplication.controller;
2 |
3 | import microservices.book.multiplication.domain.Multiplication;
4 | import microservices.book.multiplication.service.MultiplicationService;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.web.bind.annotation.GetMapping;
7 | import org.springframework.web.bind.annotation.RequestMapping;
8 | import org.springframework.web.bind.annotation.RestController;
9 |
10 | /**
11 | * This class implements a REST API for our Multiplication application.
12 | */
13 | @RestController
14 | @RequestMapping("/multiplications")
15 | final class MultiplicationController {
16 |
17 | private final MultiplicationService multiplicationService;
18 |
19 | @Autowired
20 | public MultiplicationController(final MultiplicationService multiplicationService) {
21 | this.multiplicationService = multiplicationService;
22 | }
23 |
24 | @GetMapping("/random")
25 | Multiplication getRandomMultiplication() {
26 | return multiplicationService.createRandomMultiplication();
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/social-multiplication/src/main/java/microservices/book/multiplication/controller/MultiplicationResultAttemptController.java:
--------------------------------------------------------------------------------
1 | package microservices.book.multiplication.controller;
2 |
3 | import lombok.Getter;
4 | import lombok.NoArgsConstructor;
5 | import lombok.RequiredArgsConstructor;
6 | import microservices.book.multiplication.domain.MultiplicationResultAttempt;
7 | import microservices.book.multiplication.service.MultiplicationService;
8 | import org.springframework.beans.factory.annotation.Autowired;
9 | import org.springframework.http.ResponseEntity;
10 | import org.springframework.web.bind.annotation.PostMapping;
11 | import org.springframework.web.bind.annotation.RequestBody;
12 | import org.springframework.web.bind.annotation.RequestMapping;
13 | import org.springframework.web.bind.annotation.RestController;
14 |
15 | /**
16 | * This class provides a REST API to POST the attempts from users.
17 | */
18 | @RestController
19 | @RequestMapping("/results")
20 | final class MultiplicationResultAttemptController {
21 |
22 | private final MultiplicationService multiplicationService;
23 |
24 | @Autowired
25 | MultiplicationResultAttemptController(final MultiplicationService multiplicationService) {
26 | this.multiplicationService = multiplicationService;
27 | }
28 |
29 | @PostMapping
30 | ResponseEntity postResult(@RequestBody MultiplicationResultAttempt multiplicationResultAttempt) {
31 | return ResponseEntity.ok(
32 | new ResultResponse(multiplicationService
33 | .checkAttempt(multiplicationResultAttempt)));
34 | }
35 |
36 | @RequiredArgsConstructor
37 | @NoArgsConstructor(force = true)
38 | @Getter
39 | static final class ResultResponse {
40 | private final boolean correct;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/social-multiplication/src/main/java/microservices/book/multiplication/domain/Multiplication.java:
--------------------------------------------------------------------------------
1 | package microservices.book.multiplication.domain;
2 |
3 | import lombok.EqualsAndHashCode;
4 | import lombok.Getter;
5 | import lombok.RequiredArgsConstructor;
6 | import lombok.ToString;
7 |
8 | /**
9 | * This class represents a Multiplication (a * b).
10 | */
11 | @RequiredArgsConstructor
12 | @Getter
13 | @ToString
14 | @EqualsAndHashCode
15 | public final class Multiplication {
16 |
17 | // Both factors
18 | private final int factorA;
19 | private final int factorB;
20 |
21 | // Empty constructor for JSON (de)serialization
22 | Multiplication() {
23 | this(0, 0);
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/social-multiplication/src/main/java/microservices/book/multiplication/domain/MultiplicationResultAttempt.java:
--------------------------------------------------------------------------------
1 | package microservices.book.multiplication.domain;
2 |
3 | import lombok.EqualsAndHashCode;
4 | import lombok.Getter;
5 | import lombok.RequiredArgsConstructor;
6 | import lombok.ToString;
7 |
8 | /**
9 | * Identifies the attempt from a {@link User} to solve a
10 | * {@link Multiplication}.
11 | */
12 | @RequiredArgsConstructor
13 | @Getter
14 | @ToString
15 | @EqualsAndHashCode
16 | public final class MultiplicationResultAttempt {
17 |
18 | private final User user;
19 | private final Multiplication multiplication;
20 | private final int resultAttempt;
21 |
22 | // Empty constructor for JSON (de)serialization
23 | MultiplicationResultAttempt() {
24 | user = null;
25 | multiplication = null;
26 | resultAttempt = -1;
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/social-multiplication/src/main/java/microservices/book/multiplication/domain/User.java:
--------------------------------------------------------------------------------
1 | package microservices.book.multiplication.domain;
2 |
3 | import lombok.EqualsAndHashCode;
4 | import lombok.Getter;
5 | import lombok.RequiredArgsConstructor;
6 | import lombok.ToString;
7 |
8 | /**
9 | * Stores information to identify the user.
10 | */
11 | @RequiredArgsConstructor
12 | @Getter
13 | @ToString
14 | @EqualsAndHashCode
15 | public final class User {
16 |
17 | private final String alias;
18 |
19 | // Empty constructor for JSON (de)serialization
20 | protected User() {
21 | alias = null;
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/social-multiplication/src/main/java/microservices/book/multiplication/service/MultiplicationService.java:
--------------------------------------------------------------------------------
1 | package microservices.book.multiplication.service;
2 |
3 | import microservices.book.multiplication.domain.Multiplication;
4 | import microservices.book.multiplication.domain.MultiplicationResultAttempt;
5 |
6 | public interface MultiplicationService {
7 |
8 | /**
9 | * Creates a Multiplication object with two randomly-generated factors
10 | * between 11 and 99.
11 | *
12 | * @return a Multiplication object with random factors
13 | */
14 | Multiplication createRandomMultiplication();
15 |
16 | /**
17 | * @return true if the attempt matches the result of the
18 | * multiplication, false otherwise.
19 | */
20 | boolean checkAttempt(final MultiplicationResultAttempt resultAttempt);
21 | }
22 |
--------------------------------------------------------------------------------
/social-multiplication/src/main/java/microservices/book/multiplication/service/MultiplicationServiceImpl.java:
--------------------------------------------------------------------------------
1 | package microservices.book.multiplication.service;
2 |
3 | import microservices.book.multiplication.domain.Multiplication;
4 | import microservices.book.multiplication.domain.MultiplicationResultAttempt;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.stereotype.Service;
7 |
8 | @Service
9 | final class MultiplicationServiceImpl implements MultiplicationService {
10 |
11 | private RandomGeneratorService randomGeneratorService;
12 |
13 | @Autowired
14 | public MultiplicationServiceImpl(final RandomGeneratorService randomGeneratorService) {
15 | this.randomGeneratorService = randomGeneratorService;
16 | }
17 |
18 | @Override
19 | public Multiplication createRandomMultiplication() {
20 | int factorA = randomGeneratorService.generateRandomFactor();
21 | int factorB = randomGeneratorService.generateRandomFactor();
22 | return new Multiplication(factorA, factorB);
23 | }
24 |
25 | @Override
26 | public boolean checkAttempt(final MultiplicationResultAttempt resultAttempt) {
27 | return resultAttempt.getResultAttempt() ==
28 | resultAttempt.getMultiplication().getFactorA() *
29 | resultAttempt.getMultiplication().getFactorB();
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/social-multiplication/src/main/java/microservices/book/multiplication/service/RandomGeneratorService.java:
--------------------------------------------------------------------------------
1 | package microservices.book.multiplication.service;
2 |
3 | public interface RandomGeneratorService {
4 |
5 | /**
6 | * @return a randomly-generated factor. It's always a number between 11 and 99.
7 | */
8 | int generateRandomFactor();
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/social-multiplication/src/main/java/microservices/book/multiplication/service/RandomGeneratorServiceImpl.java:
--------------------------------------------------------------------------------
1 | package microservices.book.multiplication.service;
2 |
3 | import org.springframework.stereotype.Service;
4 | import java.util.Random;
5 |
6 | @Service
7 | final class RandomGeneratorServiceImpl implements RandomGeneratorService {
8 |
9 | final static int MINIMUM_FACTOR = 11;
10 | final static int MAXIMUM_FACTOR = 99;
11 |
12 | @Override
13 | public int generateRandomFactor() {
14 | return new Random().nextInt((MAXIMUM_FACTOR - MINIMUM_FACTOR) + 1) + MINIMUM_FACTOR;
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/social-multiplication/src/main/resources/application.properties:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/microservices-practical/microservices-v3/957498f0b6bc48629fed9c5f7581d8a7b5080319/social-multiplication/src/main/resources/application.properties
--------------------------------------------------------------------------------
/social-multiplication/src/main/resources/static/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Multiplication v1
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
Welcome to Social Multiplication
13 |
This is your challenge for today:
14 |
15 | x =
16 |
17 |
18 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/social-multiplication/src/main/resources/static/multiplication-client.js:
--------------------------------------------------------------------------------
1 | function updateMultiplication() {
2 | $.ajax({
3 | url: "http://localhost:8080/multiplications/random"
4 | }).then(function(data) {
5 | // Cleans the form
6 | $("#attempt-form").find( "input[name='result-attempt']" ).val("");
7 | $("#attempt-form").find( "input[name='user-alias']" ).val("");
8 | // Gets a random challenge from API and loads the data in the HTML
9 | $('.multiplication-a').empty().append(data.factorA);
10 | $('.multiplication-b').empty().append(data.factorB);
11 | });
12 | }
13 |
14 | $(document).ready(function() {
15 |
16 | updateMultiplication();
17 |
18 | $("#attempt-form").submit(function( event ) {
19 |
20 | // Don't submit the form normally
21 | event.preventDefault();
22 |
23 | // Get some values from elements on the page
24 | var a = $('.multiplication-a').text();
25 | var b = $('.multiplication-b').text();
26 | var $form = $( this ),
27 | attempt = $form.find( "input[name='result-attempt']" ).val(),
28 | userAlias = $form.find( "input[name='user-alias']" ).val();
29 |
30 | // Compose the data in the format that the API is expecting
31 | var data = { user: { alias: userAlias}, multiplication: {factorA: a, factorB: b}, resultAttempt: attempt};
32 |
33 | // Send the data using post
34 | $.ajax({
35 | url: '/results',
36 | type: 'POST',
37 | data: JSON.stringify(data),
38 | contentType: "application/json; charset=utf-8",
39 | dataType: "json",
40 | success: function(result){
41 | if(result.correct) {
42 | $('.result-message').empty().append("The result is correct! Congratulations!");
43 | } else {
44 | $('.result-message').empty().append("Ooops that's not correct! But keep trying!");
45 | }
46 | }
47 | });
48 |
49 | updateMultiplication();
50 | });
51 | });
52 |
--------------------------------------------------------------------------------
/social-multiplication/src/main/resources/static/styles.css:
--------------------------------------------------------------------------------
1 | html, body {
2 | height: 100%;
3 | }
4 |
5 | html {
6 | display: table;
7 | margin: auto;
8 | }
9 |
10 | body {
11 | display: table-cell;
12 | vertical-align: middle;
13 | }
--------------------------------------------------------------------------------
/social-multiplication/src/test/java/microservices/book/multiplication/controller/MultiplicationControllerTest.java:
--------------------------------------------------------------------------------
1 | package microservices.book.multiplication.controller;
2 |
3 | import com.fasterxml.jackson.databind.ObjectMapper;
4 | import microservices.book.multiplication.domain.Multiplication;
5 | import microservices.book.multiplication.service.MultiplicationService;
6 | import org.junit.Before;
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 | import org.springframework.beans.factory.annotation.Autowired;
10 | import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
11 | import org.springframework.boot.test.json.JacksonTester;
12 | import org.springframework.boot.test.mock.mockito.MockBean;
13 | import org.springframework.http.HttpStatus;
14 | import org.springframework.http.MediaType;
15 | import org.springframework.mock.web.MockHttpServletResponse;
16 | import org.springframework.test.context.junit4.SpringRunner;
17 | import org.springframework.test.web.servlet.MockMvc;
18 |
19 | import static org.assertj.core.api.Assertions.assertThat;
20 | import static org.mockito.BDDMockito.given;
21 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
22 |
23 | @RunWith(SpringRunner.class)
24 | @WebMvcTest(MultiplicationController.class)
25 | public class MultiplicationControllerTest {
26 |
27 | @MockBean
28 | private MultiplicationService multiplicationService;
29 |
30 | @Autowired
31 | private MockMvc mvc;
32 |
33 | // This object will be magically initialized by the initFields method below.
34 | private JacksonTester json;
35 |
36 | @Before
37 | public void setup() {
38 | JacksonTester.initFields(this, new ObjectMapper());
39 | }
40 |
41 | @Test
42 | public void getRandomMultiplicationTest() throws Exception{
43 | // given
44 | given(multiplicationService.createRandomMultiplication())
45 | .willReturn(new Multiplication(70, 20));
46 |
47 | // when
48 | MockHttpServletResponse response = mvc.perform(
49 | get("/multiplications/random")
50 | .accept(MediaType.APPLICATION_JSON))
51 | .andReturn().getResponse();
52 |
53 | // then
54 | assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
55 | assertThat(response.getContentAsString())
56 | .isEqualTo(json.write(new Multiplication(70, 20)).getJson());
57 | }
58 |
59 | }
60 |
--------------------------------------------------------------------------------
/social-multiplication/src/test/java/microservices/book/multiplication/controller/MultiplicationResultAttemptControllerTest.java:
--------------------------------------------------------------------------------
1 | package microservices.book.multiplication.controller;
2 |
3 | import com.fasterxml.jackson.databind.ObjectMapper;
4 | import microservices.book.multiplication.domain.Multiplication;
5 | import microservices.book.multiplication.domain.MultiplicationResultAttempt;
6 | import microservices.book.multiplication.domain.User;
7 | import microservices.book.multiplication.service.MultiplicationService;
8 | import org.junit.Before;
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.autoconfigure.web.servlet.WebMvcTest;
13 | import org.springframework.boot.test.json.JacksonTester;
14 | import org.springframework.boot.test.mock.mockito.MockBean;
15 | import org.springframework.http.HttpStatus;
16 | import org.springframework.http.MediaType;
17 | import org.springframework.mock.web.MockHttpServletResponse;
18 | import org.springframework.test.context.junit4.SpringRunner;
19 | import org.springframework.test.web.servlet.MockMvc;
20 |
21 | import static microservices.book.multiplication.controller.MultiplicationResultAttemptController.ResultResponse;
22 | import static org.assertj.core.api.Assertions.assertThat;
23 | import static org.mockito.BDDMockito.given;
24 | import static org.mockito.Matchers.any;
25 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
26 |
27 | @RunWith(SpringRunner.class)
28 | @WebMvcTest(MultiplicationResultAttemptController.class)
29 | public class MultiplicationResultAttemptControllerTest {
30 |
31 | @MockBean
32 | private MultiplicationService multiplicationService;
33 |
34 | @Autowired
35 | private MockMvc mvc;
36 |
37 | // This object will be magically initialized by the initFields method below.
38 | private JacksonTester jsonResult;
39 | private JacksonTester jsonResponse;
40 |
41 | @Before
42 | public void setup() {
43 | JacksonTester.initFields(this, new ObjectMapper());
44 | }
45 |
46 | @Test
47 | public void postResultReturnCorrect() throws Exception {
48 | genericParameterizedTest(true);
49 | }
50 |
51 | @Test
52 | public void postResultReturnNotCorrect() throws Exception {
53 | genericParameterizedTest(false);
54 | }
55 |
56 | void genericParameterizedTest(final boolean correct) throws Exception {
57 | // given (remember we're not testing here the service itself)
58 | given(multiplicationService
59 | .checkAttempt(any(MultiplicationResultAttempt.class)))
60 | .willReturn(correct);
61 | User user = new User("john");
62 | Multiplication multiplication = new Multiplication(50, 70);
63 | MultiplicationResultAttempt attempt = new MultiplicationResultAttempt(
64 | user, multiplication, 3500);
65 |
66 | // when
67 | MockHttpServletResponse response = mvc.perform(
68 | post("/results").contentType(MediaType.APPLICATION_JSON)
69 | .content(jsonResult.write(attempt).getJson()))
70 | .andReturn().getResponse();
71 |
72 | // then
73 | assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
74 | assertThat(response.getContentAsString()).isEqualTo(
75 | jsonResponse.write(new ResultResponse(correct)).getJson());
76 | }
77 |
78 | }
--------------------------------------------------------------------------------
/social-multiplication/src/test/java/microservices/book/multiplication/service/MultiplicationServiceImplTest.java:
--------------------------------------------------------------------------------
1 | package microservices.book.multiplication.service;
2 |
3 | import microservices.book.multiplication.domain.Multiplication;
4 | import microservices.book.multiplication.domain.MultiplicationResultAttempt;
5 | import microservices.book.multiplication.domain.User;
6 | import org.junit.Before;
7 | import org.junit.Test;
8 | import org.mockito.Mock;
9 | import org.mockito.MockitoAnnotations;
10 |
11 | import static org.assertj.core.api.Assertions.assertThat;
12 | import static org.mockito.BDDMockito.given;
13 |
14 | public class MultiplicationServiceImplTest {
15 |
16 | private MultiplicationServiceImpl multiplicationServiceImpl;
17 |
18 | @Mock
19 | private RandomGeneratorService randomGeneratorService;
20 |
21 | @Before
22 | public void setUp() {
23 | // With this call to initMocks we tell Mockito to process the annotations
24 | MockitoAnnotations.initMocks(this);
25 | multiplicationServiceImpl = new MultiplicationServiceImpl(randomGeneratorService);
26 | }
27 |
28 | @Test
29 | public void createRandomMultiplicationTest() {
30 | // given (our mocked Random Generator service will return first 50, then 30)
31 | given(randomGeneratorService.generateRandomFactor()).willReturn(50, 30);
32 |
33 | // when
34 | Multiplication multiplication = multiplicationServiceImpl.createRandomMultiplication();
35 |
36 | // then
37 | assertThat(multiplication.getFactorA()).isEqualTo(50);
38 | assertThat(multiplication.getFactorB()).isEqualTo(30);
39 | }
40 |
41 | @Test
42 | public void checkCorrectAttemptTest() {
43 | // given
44 | Multiplication multiplication = new Multiplication(50, 60);
45 | User user = new User("john_doe");
46 | MultiplicationResultAttempt attempt = new MultiplicationResultAttempt(user, multiplication, 3000);
47 |
48 | // when
49 | boolean attemptResult = multiplicationServiceImpl.checkAttempt(attempt);
50 |
51 | // then
52 | assertThat(attemptResult).isTrue();
53 | }
54 |
55 | @Test
56 | public void checkWrongAttemptTest() {
57 | // given
58 | Multiplication multiplication = new Multiplication(50, 60);
59 | User user = new User("john_doe");
60 | MultiplicationResultAttempt attempt = new MultiplicationResultAttempt(user, multiplication, 3010);
61 |
62 | // when
63 | boolean attemptResult = multiplicationServiceImpl.checkAttempt(attempt);
64 |
65 | // then
66 | assertThat(attemptResult).isFalse();
67 | }
68 | }
--------------------------------------------------------------------------------
/social-multiplication/src/test/java/microservices/book/multiplication/service/RandomGeneratorServiceImplTest.java:
--------------------------------------------------------------------------------
1 | package microservices.book.multiplication.service;
2 |
3 | import org.junit.Before;
4 | import org.junit.Test;
5 |
6 | import java.util.List;
7 | import java.util.stream.Collectors;
8 | import java.util.stream.IntStream;
9 |
10 | import static org.assertj.core.api.Assertions.assertThat;
11 |
12 | public class RandomGeneratorServiceImplTest {
13 |
14 | private RandomGeneratorServiceImpl randomGeneratorServiceImpl;
15 |
16 | @Before
17 | public void setUp() {
18 | randomGeneratorServiceImpl = new RandomGeneratorServiceImpl();
19 | }
20 |
21 | @Test
22 | public void generateRandomFactorIsBetweenExpectedLimits() throws Exception {
23 | // when a good sample of randomly generated factors is generated
24 | List randomFactors = IntStream.range(0, 1000)
25 | .map(i -> randomGeneratorServiceImpl.generateRandomFactor())
26 | .boxed().collect(Collectors.toList());
27 |
28 | // then all of them should be between 11 and 100
29 | // because we want a middle-complexity calculation
30 | assertThat(randomFactors).containsOnlyElementsOf(IntStream.range(11, 100)
31 | .boxed().collect(Collectors.toList()));
32 | }
33 |
34 | }
--------------------------------------------------------------------------------