├── .gitignore
├── README.md
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
├── main
├── java
│ └── com
│ │ └── itnext
│ │ ├── SpringRestApplication.java
│ │ ├── config
│ │ └── SwaggerConfig.java
│ │ └── domain
│ │ ├── entities
│ │ ├── Author.java
│ │ ├── Book.java
│ │ └── Category.java
│ │ └── repositories
│ │ ├── AuthorRepository.java
│ │ ├── BookRepository.java
│ │ └── CategoryRepository.java
└── resources
│ └── application.properties
└── test
└── java
└── com
└── itnext
└── domain
└── repositories
├── AuthorRepositoryTest.java
└── BooksRepositoryTest.java
/.gitignore:
--------------------------------------------------------------------------------
1 | # Intellij
2 | .idea/
3 | *.iml
4 | *.iws
5 |
6 | # Mac
7 | .DS_Store
8 |
9 | # Maven
10 | log/
11 | target/
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [](https://circleci.com/gh/LINKIT-Group/spring-data-rest)
2 | # spring-data-rest
3 | Microservice example using Spring Data REST.
4 |
5 | To run it, go to the root folder and run "mvn spring-boot:run"
6 |
--------------------------------------------------------------------------------
/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 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home
58 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html
59 | if [ -z "$JAVA_HOME" ]; then
60 | if [ -x "/usr/libexec/java_home" ]; then
61 | export JAVA_HOME="`/usr/libexec/java_home`"
62 | else
63 | export JAVA_HOME="/Library/Java/Home"
64 | fi
65 | fi
66 | ;;
67 | esac
68 |
69 | if [ -z "$JAVA_HOME" ] ; then
70 | if [ -r /etc/gentoo-release ] ; then
71 | JAVA_HOME=`java-config --jre-home`
72 | fi
73 | fi
74 |
75 | if [ -z "$M2_HOME" ] ; then
76 | ## resolve links - $0 may be a link to maven's home
77 | PRG="$0"
78 |
79 | # need this for relative symlinks
80 | while [ -h "$PRG" ] ; do
81 | ls=`ls -ld "$PRG"`
82 | link=`expr "$ls" : '.*-> \(.*\)$'`
83 | if expr "$link" : '/.*' > /dev/null; then
84 | PRG="$link"
85 | else
86 | PRG="`dirname "$PRG"`/$link"
87 | fi
88 | done
89 |
90 | saveddir=`pwd`
91 |
92 | M2_HOME=`dirname "$PRG"`/..
93 |
94 | # make it fully qualified
95 | M2_HOME=`cd "$M2_HOME" && pwd`
96 |
97 | cd "$saveddir"
98 | # echo Using m2 at $M2_HOME
99 | fi
100 |
101 | # For Cygwin, ensure paths are in UNIX format before anything is touched
102 | if $cygwin ; then
103 | [ -n "$M2_HOME" ] &&
104 | M2_HOME=`cygpath --unix "$M2_HOME"`
105 | [ -n "$JAVA_HOME" ] &&
106 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
107 | [ -n "$CLASSPATH" ] &&
108 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
109 | fi
110 |
111 | # For Migwn, ensure paths are in UNIX format before anything is touched
112 | if $mingw ; then
113 | [ -n "$M2_HOME" ] &&
114 | M2_HOME="`(cd "$M2_HOME"; pwd)`"
115 | [ -n "$JAVA_HOME" ] &&
116 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`"
117 | # TODO classpath?
118 | fi
119 |
120 | if [ -z "$JAVA_HOME" ]; then
121 | javaExecutable="`which javac`"
122 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then
123 | # readlink(1) is not available as standard on Solaris 10.
124 | readLink=`which readlink`
125 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then
126 | if $darwin ; then
127 | javaHome="`dirname \"$javaExecutable\"`"
128 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac"
129 | else
130 | javaExecutable="`readlink -f \"$javaExecutable\"`"
131 | fi
132 | javaHome="`dirname \"$javaExecutable\"`"
133 | javaHome=`expr "$javaHome" : '\(.*\)/bin'`
134 | JAVA_HOME="$javaHome"
135 | export JAVA_HOME
136 | fi
137 | fi
138 | fi
139 |
140 | if [ -z "$JAVACMD" ] ; then
141 | if [ -n "$JAVA_HOME" ] ; then
142 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
143 | # IBM's JDK on AIX uses strange locations for the executables
144 | JAVACMD="$JAVA_HOME/jre/sh/java"
145 | else
146 | JAVACMD="$JAVA_HOME/bin/java"
147 | fi
148 | else
149 | JAVACMD="`which java`"
150 | fi
151 | fi
152 |
153 | if [ ! -x "$JAVACMD" ] ; then
154 | echo "Error: JAVA_HOME is not defined correctly." >&2
155 | echo " We cannot execute $JAVACMD" >&2
156 | exit 1
157 | fi
158 |
159 | if [ -z "$JAVA_HOME" ] ; then
160 | echo "Warning: JAVA_HOME environment variable is not set."
161 | fi
162 |
163 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher
164 |
165 | # traverses directory structure from process work directory to filesystem root
166 | # first directory with .mvn subdirectory is considered project base directory
167 | find_maven_basedir() {
168 |
169 | if [ -z "$1" ]
170 | then
171 | echo "Path not specified to find_maven_basedir"
172 | return 1
173 | fi
174 |
175 | basedir="$1"
176 | wdir="$1"
177 | while [ "$wdir" != '/' ] ; do
178 | if [ -d "$wdir"/.mvn ] ; then
179 | basedir=$wdir
180 | break
181 | fi
182 | # workaround for JBEAP-8937 (on Solaris 10/Sparc)
183 | if [ -d "${wdir}" ]; then
184 | wdir=`cd "$wdir/.."; pwd`
185 | fi
186 | # end of workaround
187 | done
188 | echo "${basedir}"
189 | }
190 |
191 | # concatenates all lines of a file
192 | concat_lines() {
193 | if [ -f "$1" ]; then
194 | echo "$(tr -s '\n' ' ' < "$1")"
195 | fi
196 | }
197 |
198 | BASE_DIR=`find_maven_basedir "$(pwd)"`
199 | if [ -z "$BASE_DIR" ]; then
200 | exit 1;
201 | fi
202 |
203 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"}
204 | echo $MAVEN_PROJECTBASEDIR
205 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS"
206 |
207 | # For Cygwin, switch paths to Windows format before running java
208 | if $cygwin; then
209 | [ -n "$M2_HOME" ] &&
210 | M2_HOME=`cygpath --path --windows "$M2_HOME"`
211 | [ -n "$JAVA_HOME" ] &&
212 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
213 | [ -n "$CLASSPATH" ] &&
214 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
215 | [ -n "$MAVEN_PROJECTBASEDIR" ] &&
216 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"`
217 | fi
218 |
219 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
220 |
221 | exec "$JAVACMD" \
222 | $MAVEN_OPTS \
223 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \
224 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
225 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@"
226 |
--------------------------------------------------------------------------------
/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 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn".
84 | @REM Fallback to current working directory if not found.
85 |
86 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR%
87 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir
88 |
89 | set EXEC_DIR=%CD%
90 | set WDIR=%EXEC_DIR%
91 | :findBaseDir
92 | IF EXIST "%WDIR%"\.mvn goto baseDirFound
93 | cd ..
94 | IF "%WDIR%"=="%CD%" goto baseDirNotFound
95 | set WDIR=%CD%
96 | goto findBaseDir
97 |
98 | :baseDirFound
99 | set MAVEN_PROJECTBASEDIR=%WDIR%
100 | cd "%EXEC_DIR%"
101 | goto endDetectBaseDir
102 |
103 | :baseDirNotFound
104 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR%
105 | cd "%EXEC_DIR%"
106 |
107 | :endDetectBaseDir
108 |
109 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig
110 |
111 | @setlocal EnableExtensions EnableDelayedExpansion
112 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a
113 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS%
114 |
115 | :endReadAdditionalConfig
116 |
117 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe"
118 |
119 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar"
120 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
121 |
122 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %*
123 | if ERRORLEVEL 1 goto error
124 | goto end
125 |
126 | :error
127 | set ERROR_CODE=1
128 |
129 | :end
130 | @endlocal & set ERROR_CODE=%ERROR_CODE%
131 |
132 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost
133 | @REM check for post script, once with legacy .bat ending and once with .cmd ending
134 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat"
135 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd"
136 | :skipRcPost
137 |
138 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on'
139 | if "%MAVEN_BATCH_PAUSE%" == "on" pause
140 |
141 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE%
142 |
143 | exit /B %ERROR_CODE%
144 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | com.itnext
7 | bookstore
8 | 0.0.1-SNAPSHOT
9 | jar
10 |
11 | spring-rest
12 | Spring data rest Demo
13 |
14 |
15 | org.springframework.boot
16 | spring-boot-starter-parent
17 | 1.5.4.RELEASE
18 |
19 |
20 |
21 |
22 | UTF-8
23 | UTF-8
24 | 1.8
25 | Dalston.SR1
26 |
27 |
28 |
29 |
30 |
31 |
32 | org.springframework.boot
33 | spring-boot-starter-data-rest
34 |
35 |
36 |
37 |
38 | org.springframework.boot
39 | spring-boot-starter-data-mongodb
40 |
41 |
42 |
43 | de.flapdoodle.embed
44 | de.flapdoodle.embed.mongo
45 | 1.50.5
46 |
47 |
48 |
49 |
50 |
51 | io.springfox
52 | springfox-data-rest
53 | 2.7.0
54 |
55 |
56 |
57 | io.springfox
58 | springfox-swagger-ui
59 | 2.7.0
60 |
61 |
62 |
63 | io.springfox
64 | springfox-swagger2
65 | 2.7.0
66 | compile
67 |
68 |
69 |
70 |
71 | org.springframework.boot
72 | spring-boot-starter-test
73 | test
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 | org.springframework.cloud
82 | spring-cloud-dependencies
83 | ${spring-cloud.version}
84 | pom
85 | import
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 | org.springframework.boot
94 | spring-boot-maven-plugin
95 |
96 |
97 |
98 |
99 |
100 |
--------------------------------------------------------------------------------
/src/main/java/com/itnext/SpringRestApplication.java:
--------------------------------------------------------------------------------
1 | package com.itnext;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
5 | import org.springframework.boot.autoconfigure.SpringBootApplication;
6 | import org.springframework.context.annotation.Import;
7 | import springfox.documentation.swagger2.annotations.EnableSwagger2;
8 |
9 | @SpringBootApplication
10 | @EnableAutoConfiguration
11 | @EnableSwagger2
12 | @Import({springfox.documentation.spring.data.rest.configuration.SpringDataRestConfiguration.class})
13 | public class SpringRestApplication {
14 |
15 | public static void main(String[] args) {
16 | SpringApplication.run(SpringRestApplication.class, args);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/com/itnext/config/SwaggerConfig.java:
--------------------------------------------------------------------------------
1 | package com.itnext.config;
2 |
3 | import org.springframework.context.annotation.Bean;
4 | import org.springframework.context.annotation.Configuration;
5 | import springfox.documentation.builders.PathSelectors;
6 | import springfox.documentation.builders.RequestHandlerSelectors;
7 | import springfox.documentation.spi.DocumentationType;
8 | import springfox.documentation.spring.web.plugins.Docket;
9 |
10 | /**
11 | * Created by rodrigo.chaves on 13/06/2017.
12 | */
13 | @Configuration
14 | public class SwaggerConfig {
15 | @Bean
16 | public Docket productApi() {
17 | return new Docket(DocumentationType.SWAGGER_2).select()
18 | .apis(RequestHandlerSelectors.any())
19 | .paths(PathSelectors.any())
20 | .build();
21 | }
22 | }
--------------------------------------------------------------------------------
/src/main/java/com/itnext/domain/entities/Author.java:
--------------------------------------------------------------------------------
1 | package com.itnext.domain.entities;
2 |
3 | import org.springframework.data.annotation.Id;
4 | import org.springframework.data.mongodb.core.mapping.DBRef;
5 | import org.springframework.data.mongodb.core.mapping.Document;
6 |
7 | import java.util.List;
8 |
9 | /**
10 | * Created by rodrigo.chaves on 12/06/2017.
11 | */
12 | @Document
13 | public class Author {
14 |
15 | @Id
16 | private String id;
17 |
18 | private String name;
19 |
20 | @DBRef
21 | private List books;
22 |
23 | public String getId() {
24 | return id;
25 | }
26 |
27 | public void setId(String id) {
28 | this.id = id;
29 | }
30 |
31 | public String getName() {
32 | return name;
33 | }
34 |
35 | public void setName(String name) {
36 | this.name = name;
37 | }
38 |
39 | public List getBooks() {
40 | return books;
41 | }
42 |
43 | public void setBooks(List books) {
44 | this.books = books;
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/main/java/com/itnext/domain/entities/Book.java:
--------------------------------------------------------------------------------
1 | package com.itnext.domain.entities;
2 |
3 | import org.springframework.data.annotation.Id;
4 | import org.springframework.data.mongodb.core.mapping.DBRef;
5 | import org.springframework.data.mongodb.core.mapping.Document;
6 |
7 | /**
8 | * Created by rodrigo.chaves on 12/06/2017.
9 | */
10 | @Document
11 | public class Book {
12 |
13 | @Id
14 | private String id;
15 |
16 | private String name;
17 |
18 | @DBRef
19 | private Category category;
20 |
21 | private Double price;
22 |
23 | public Book() {
24 | }
25 |
26 | public String getId() {
27 | return id;
28 | }
29 |
30 | public void setId(String id) {
31 | this.id = id;
32 | }
33 |
34 | public String getName() {
35 | return name;
36 | }
37 |
38 | public void setName(String name) {
39 | this.name = name;
40 | }
41 |
42 | public Category getCategory() {
43 | return category;
44 | }
45 |
46 | public void setCategory(Category category) {
47 | this.category = category;
48 | }
49 |
50 | public Double getPrice() {
51 | return price;
52 | }
53 |
54 | public void setPrice(Double price) {
55 | this.price = price;
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/src/main/java/com/itnext/domain/entities/Category.java:
--------------------------------------------------------------------------------
1 | package com.itnext.domain.entities;
2 |
3 | import org.springframework.data.annotation.Id;
4 | import org.springframework.data.mongodb.core.mapping.Document;
5 |
6 | /**
7 | * Created by rodrigo.chaves on 12/06/2017.
8 | */
9 | @Document
10 | public class Category {
11 |
12 | @Id
13 | private String id;
14 |
15 | private String name;
16 |
17 | public String getName() {
18 | return name;
19 | }
20 |
21 | public void setName(String name) {
22 | this.name = name;
23 | }
24 |
25 | public String getId() {
26 | return id;
27 | }
28 |
29 | public void setId(String id) {
30 | this.id = id;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/com/itnext/domain/repositories/AuthorRepository.java:
--------------------------------------------------------------------------------
1 | package com.itnext.domain.repositories;
2 |
3 | import com.itnext.domain.entities.Author;
4 | import com.itnext.domain.entities.Book;
5 | import org.springframework.data.mongodb.repository.MongoRepository;
6 | import org.springframework.data.rest.core.annotation.RepositoryRestResource;
7 |
8 | /**
9 | * Created by rodrigo.chaves on 12/06/2017.
10 | */
11 | @RepositoryRestResource(collectionResourceRel = "authors", path = "authors")
12 | public interface AuthorRepository extends MongoRepository {
13 | Author findOneByBooks(Book book);
14 | }
--------------------------------------------------------------------------------
/src/main/java/com/itnext/domain/repositories/BookRepository.java:
--------------------------------------------------------------------------------
1 | package com.itnext.domain.repositories;
2 |
3 | import com.itnext.domain.entities.Book;
4 | import com.itnext.domain.entities.Category;
5 | import org.springframework.data.mongodb.repository.MongoRepository;
6 | import org.springframework.data.rest.core.annotation.RepositoryRestResource;
7 |
8 | import java.util.List;
9 |
10 | /**
11 | * Created by rodrigo.chaves on 12/06/2017.
12 | */
13 | @RepositoryRestResource(collectionResourceRel = "books", path = "books")
14 | public interface BookRepository extends MongoRepository {
15 | List findByCategory(Category category);
16 | }
--------------------------------------------------------------------------------
/src/main/java/com/itnext/domain/repositories/CategoryRepository.java:
--------------------------------------------------------------------------------
1 | package com.itnext.domain.repositories;
2 |
3 | import com.itnext.domain.entities.Category;
4 | import org.springframework.data.mongodb.repository.MongoRepository;
5 | import org.springframework.data.rest.core.annotation.RepositoryRestResource;
6 |
7 | /**
8 | * Created by rodrigo.chaves on 12/06/2017.
9 | */
10 | @RepositoryRestResource(collectionResourceRel = "categories", path = "categories")
11 | public interface CategoryRepository extends MongoRepository {
12 | }
--------------------------------------------------------------------------------
/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.data.rest.base-path=/api
--------------------------------------------------------------------------------
/src/test/java/com/itnext/domain/repositories/AuthorRepositoryTest.java:
--------------------------------------------------------------------------------
1 | package com.itnext.domain.repositories;
2 |
3 | import com.itnext.domain.entities.Author;
4 | import com.itnext.domain.entities.Book;
5 | import com.itnext.domain.entities.Category;
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.context.SpringBootTest;
11 | import org.springframework.test.context.junit4.SpringRunner;
12 |
13 | import java.util.ArrayList;
14 |
15 | import static org.junit.Assert.assertEquals;
16 | import static org.junit.Assert.assertFalse;
17 | import static org.junit.Assert.assertNotNull;
18 |
19 | /**
20 | * Created by rodrigo.chaves on 13/06/2017.
21 | */
22 | @RunWith(SpringRunner.class)
23 | @SpringBootTest
24 | public class AuthorRepositoryTest {
25 | @Autowired
26 | private AuthorRepository authorRepository;
27 |
28 | @Autowired
29 | private BookRepository booksRepository;
30 |
31 | @Autowired
32 | private CategoryRepository categoryRepository;
33 |
34 | private Book book;
35 |
36 | private Author author;
37 |
38 | private Category category;
39 |
40 | @Before
41 | public void setup() {
42 | this.createCategory();
43 | this.createBook();
44 | this.createAuthor();
45 |
46 | }
47 |
48 | @Test
49 | public void findByBookId() throws Exception {
50 |
51 | Author authorByBooksById = authorRepository.findOneByBooks(book);
52 | assertFalse(authorByBooksById.getBooks().isEmpty());
53 | assertEquals(authorByBooksById.getId(), author.getId());
54 |
55 | }
56 |
57 | private void createBook() {
58 | book = new Book();
59 | book.setName("Patterns of Enterprise Application Architecture 2");
60 | book.setCategory(category);
61 | book.setPrice(10d);
62 | booksRepository.save(book);
63 | }
64 |
65 | private void createCategory() {
66 | category = new Category();
67 | category.setName("Computer Science");
68 | categoryRepository.save(category);
69 | }
70 |
71 | private void createAuthor() {
72 | author = new Author();
73 | author.setName("Martin Fowler");
74 | author.setBooks(new ArrayList() {
75 | {
76 | add(book);
77 | }
78 | });
79 | authorRepository.save(author);
80 | }
81 | }
--------------------------------------------------------------------------------
/src/test/java/com/itnext/domain/repositories/BooksRepositoryTest.java:
--------------------------------------------------------------------------------
1 | package com.itnext.domain.repositories;
2 |
3 | import com.itnext.domain.entities.Author;
4 | import com.itnext.domain.entities.Book;
5 | import com.itnext.domain.entities.Category;
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.context.SpringBootTest;
11 | import org.springframework.test.context.junit4.SpringRunner;
12 |
13 | import java.util.ArrayList;
14 | import java.util.List;
15 |
16 | import static org.junit.Assert.assertEquals;
17 | import static org.junit.Assert.assertFalse;
18 | import static org.junit.Assert.assertTrue;
19 |
20 | /**
21 | * Created by rodrigo.chaves on 13/06/2017.
22 | */
23 | @RunWith(SpringRunner.class)
24 | @SpringBootTest
25 | public class BooksRepositoryTest {
26 |
27 | @Autowired
28 | private AuthorRepository authorRepository;
29 |
30 | @Autowired
31 | private BookRepository booksRepository;
32 |
33 | @Autowired
34 | private CategoryRepository categoryRepository;
35 |
36 | private Book book;
37 |
38 | private Category category;
39 |
40 | @Test
41 | public void findByCategoryId() throws Exception {
42 | List booksByCategory = booksRepository.findByCategory(category);
43 | assertFalse(booksByCategory.isEmpty());
44 | assertEquals(booksByCategory.get(0).getCategory().getId(), category.getId());
45 | }
46 |
47 | @Before
48 | public void setup() {
49 | this.createCategories();
50 | this.createBook();
51 | this.createAuthor();
52 |
53 | }
54 |
55 | private void createBook() {
56 | book = new Book();
57 | book.setName("Patterns of Enterprise Application Architecture");
58 | book.setCategory(category);
59 | book.setPrice(10d);
60 | booksRepository.save(book);
61 | }
62 |
63 | private void createCategories() {
64 |
65 | category = new Category();
66 | category.setName("Health");
67 | categoryRepository.save(category);
68 |
69 | category = new Category();
70 | category.setName("Business");
71 | categoryRepository.save(category);
72 |
73 | category = new Category();
74 | category.setName("Computer Science");
75 | categoryRepository.save(category);
76 | }
77 |
78 | private void createAuthor() {
79 | Author author = new Author();
80 | author.setName("Martin Fowler");
81 | author.setBooks(new ArrayList() {
82 | {
83 | add(book);
84 | }
85 | });
86 | authorRepository.save(author);
87 | }
88 |
89 | }
--------------------------------------------------------------------------------