├── .gitignore ├── .mvn └── wrapper │ ├── MavenWrapperDownloader.java │ ├── maven-wrapper.jar │ └── maven-wrapper.properties ├── README.md ├── mvnw ├── mvnw.cmd ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── school │ │ └── relationships │ │ ├── RelationshipsApplication.java │ │ ├── controllers │ │ ├── AdmissionFileController.java │ │ ├── DormitoryController.java │ │ ├── StudentController.java │ │ ├── StudentHasSubjectController.java │ │ ├── SubjectController.java │ │ ├── TeacherController.java │ │ └── TeacherHasSubjectController.java │ │ ├── entities │ │ ├── Admissionfile.java │ │ ├── Dormitory.java │ │ ├── Student.java │ │ ├── StudentHasSubject.java │ │ ├── StudentHasSubjectPK.java │ │ ├── Subject.java │ │ ├── Teacher.java │ │ ├── TeacherHasSubject.java │ │ └── TeacherHasSubjectPK.java │ │ ├── exception │ │ ├── NonRollbackException.java │ │ └── RollBackException.java │ │ ├── models │ │ ├── AdmissionFileModel.java │ │ ├── DormitoryModel.java │ │ ├── StudentHasSubjectModel.java │ │ ├── StudentModel.java │ │ ├── SubjectModel.java │ │ ├── TeacherHasSubjectModel.java │ │ └── TeacherModel.java │ │ ├── repositories │ │ ├── AdmissionfileRepository.java │ │ ├── DormitoryRepository.java │ │ ├── StudentHasSubjectRepository.java │ │ ├── StudentRepository.java │ │ ├── SubjectRepository.java │ │ ├── TeacherHasSubjectRepository.java │ │ └── TeacherRepository.java │ │ ├── services │ │ ├── AdmissionFileService.java │ │ ├── DormitoryService.java │ │ ├── StudentService.java │ │ ├── StudentSubjectService.java │ │ ├── SubjectService.java │ │ ├── TeacherService.java │ │ └── TeacherSubjectService.java │ │ └── util │ │ └── FormattingUtils.java └── resources │ ├── META-INF │ └── persistence.xml │ └── application.properties └── test └── java └── com └── school └── relationships ├── RelationshipsApplicationTests.java ├── SubjectServiceTests.java └── TeacherServiceTests.java /.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/**/target/ 5 | !**/src/test/**/target/ 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | !**/src/main/**/build/ 30 | !**/src/test/**/build/ 31 | 32 | ### VS Code ### 33 | .vscode/ 34 | -------------------------------------------------------------------------------- /.mvn/wrapper/MavenWrapperDownloader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2007-present the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | import java.net.*; 17 | import java.io.*; 18 | import java.nio.channels.*; 19 | import java.util.Properties; 20 | 21 | public class MavenWrapperDownloader { 22 | 23 | private static final String WRAPPER_VERSION = "0.5.6"; 24 | /** 25 | * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. 26 | */ 27 | private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" 28 | + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; 29 | 30 | /** 31 | * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to 32 | * use instead of the default one. 33 | */ 34 | private static final String MAVEN_WRAPPER_PROPERTIES_PATH = 35 | ".mvn/wrapper/maven-wrapper.properties"; 36 | 37 | /** 38 | * Path where the maven-wrapper.jar will be saved to. 39 | */ 40 | private static final String MAVEN_WRAPPER_JAR_PATH = 41 | ".mvn/wrapper/maven-wrapper.jar"; 42 | 43 | /** 44 | * Name of the property which should be used to override the default download url for the wrapper. 45 | */ 46 | private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; 47 | 48 | public static void main(String args[]) { 49 | System.out.println("- Downloader started"); 50 | File baseDirectory = new File(args[0]); 51 | System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); 52 | 53 | // If the maven-wrapper.properties exists, read it and check if it contains a custom 54 | // wrapperUrl parameter. 55 | File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); 56 | String url = DEFAULT_DOWNLOAD_URL; 57 | if(mavenWrapperPropertyFile.exists()) { 58 | FileInputStream mavenWrapperPropertyFileInputStream = null; 59 | try { 60 | mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); 61 | Properties mavenWrapperProperties = new Properties(); 62 | mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); 63 | url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); 64 | } catch (IOException e) { 65 | System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); 66 | } finally { 67 | try { 68 | if(mavenWrapperPropertyFileInputStream != null) { 69 | mavenWrapperPropertyFileInputStream.close(); 70 | } 71 | } catch (IOException e) { 72 | // Ignore ... 73 | } 74 | } 75 | } 76 | System.out.println("- Downloading from: " + url); 77 | 78 | File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); 79 | if(!outputFile.getParentFile().exists()) { 80 | if(!outputFile.getParentFile().mkdirs()) { 81 | System.out.println( 82 | "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); 83 | } 84 | } 85 | System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); 86 | try { 87 | downloadFileFromURL(url, outputFile); 88 | System.out.println("Done"); 89 | System.exit(0); 90 | } catch (Throwable e) { 91 | System.out.println("- Error downloading"); 92 | e.printStackTrace(); 93 | System.exit(1); 94 | } 95 | } 96 | 97 | private static void downloadFileFromURL(String urlString, File destination) throws Exception { 98 | if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { 99 | String username = System.getenv("MVNW_USERNAME"); 100 | char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); 101 | Authenticator.setDefault(new Authenticator() { 102 | @Override 103 | protected PasswordAuthentication getPasswordAuthentication() { 104 | return new PasswordAuthentication(username, password); 105 | } 106 | }); 107 | } 108 | URL website = new URL(urlString); 109 | ReadableByteChannel rbc; 110 | rbc = Channels.newChannel(website.openStream()); 111 | FileOutputStream fos = new FileOutputStream(destination); 112 | fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); 113 | fos.close(); 114 | rbc.close(); 115 | } 116 | 117 | } 118 | -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/johnmbindyo/mockito-unit-testing/47ea32a5247f46741852cb79a5d62782a9d5d011/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.2/apache-maven-3.8.2-bin.zip 2 | wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mockito-unit-testing 2 | Writing unit tests using mockito test framework. This repository provides a well crafted project to learn mockito 3 | For more on this topic ,[check this article](https://devsought.com/mockito-tutorial) 4 | -------------------------------------------------------------------------------- /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 | # https://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 | # Maven 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 Mingw, 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 | fi 118 | 119 | if [ -z "$JAVA_HOME" ]; then 120 | javaExecutable="`which javac`" 121 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 122 | # readlink(1) is not available as standard on Solaris 10. 123 | readLink=`which readlink` 124 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 125 | if $darwin ; then 126 | javaHome="`dirname \"$javaExecutable\"`" 127 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 128 | else 129 | javaExecutable="`readlink -f \"$javaExecutable\"`" 130 | fi 131 | javaHome="`dirname \"$javaExecutable\"`" 132 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 133 | JAVA_HOME="$javaHome" 134 | export JAVA_HOME 135 | fi 136 | fi 137 | fi 138 | 139 | if [ -z "$JAVACMD" ] ; then 140 | if [ -n "$JAVA_HOME" ] ; then 141 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 142 | # IBM's JDK on AIX uses strange locations for the executables 143 | JAVACMD="$JAVA_HOME/jre/sh/java" 144 | else 145 | JAVACMD="$JAVA_HOME/bin/java" 146 | fi 147 | else 148 | JAVACMD="`which java`" 149 | fi 150 | fi 151 | 152 | if [ ! -x "$JAVACMD" ] ; then 153 | echo "Error: JAVA_HOME is not defined correctly." >&2 154 | echo " We cannot execute $JAVACMD" >&2 155 | exit 1 156 | fi 157 | 158 | if [ -z "$JAVA_HOME" ] ; then 159 | echo "Warning: JAVA_HOME environment variable is not set." 160 | fi 161 | 162 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher 163 | 164 | # traverses directory structure from process work directory to filesystem root 165 | # first directory with .mvn subdirectory is considered project base directory 166 | find_maven_basedir() { 167 | 168 | if [ -z "$1" ] 169 | then 170 | echo "Path not specified to find_maven_basedir" 171 | return 1 172 | fi 173 | 174 | basedir="$1" 175 | wdir="$1" 176 | while [ "$wdir" != '/' ] ; do 177 | if [ -d "$wdir"/.mvn ] ; then 178 | basedir=$wdir 179 | break 180 | fi 181 | # workaround for JBEAP-8937 (on Solaris 10/Sparc) 182 | if [ -d "${wdir}" ]; then 183 | wdir=`cd "$wdir/.."; pwd` 184 | fi 185 | # end of workaround 186 | done 187 | echo "${basedir}" 188 | } 189 | 190 | # concatenates all lines of a file 191 | concat_lines() { 192 | if [ -f "$1" ]; then 193 | echo "$(tr -s '\n' ' ' < "$1")" 194 | fi 195 | } 196 | 197 | BASE_DIR=`find_maven_basedir "$(pwd)"` 198 | if [ -z "$BASE_DIR" ]; then 199 | exit 1; 200 | fi 201 | 202 | ########################################################################################## 203 | # Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 204 | # This allows using the maven wrapper in projects that prohibit checking in binary data. 205 | ########################################################################################## 206 | if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then 207 | if [ "$MVNW_VERBOSE" = true ]; then 208 | echo "Found .mvn/wrapper/maven-wrapper.jar" 209 | fi 210 | else 211 | if [ "$MVNW_VERBOSE" = true ]; then 212 | echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." 213 | fi 214 | if [ -n "$MVNW_REPOURL" ]; then 215 | jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" 216 | else 217 | jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" 218 | fi 219 | while IFS="=" read key value; do 220 | case "$key" in (wrapperUrl) jarUrl="$value"; break ;; 221 | esac 222 | done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" 223 | if [ "$MVNW_VERBOSE" = true ]; then 224 | echo "Downloading from: $jarUrl" 225 | fi 226 | wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" 227 | if $cygwin; then 228 | wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` 229 | fi 230 | 231 | if command -v wget > /dev/null; then 232 | if [ "$MVNW_VERBOSE" = true ]; then 233 | echo "Found wget ... using wget" 234 | fi 235 | if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then 236 | wget "$jarUrl" -O "$wrapperJarPath" 237 | else 238 | wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" 239 | fi 240 | elif command -v curl > /dev/null; then 241 | if [ "$MVNW_VERBOSE" = true ]; then 242 | echo "Found curl ... using curl" 243 | fi 244 | if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then 245 | curl -o "$wrapperJarPath" "$jarUrl" -f 246 | else 247 | curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f 248 | fi 249 | 250 | else 251 | if [ "$MVNW_VERBOSE" = true ]; then 252 | echo "Falling back to using Java to download" 253 | fi 254 | javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" 255 | # For Cygwin, switch paths to Windows format before running javac 256 | if $cygwin; then 257 | javaClass=`cygpath --path --windows "$javaClass"` 258 | fi 259 | if [ -e "$javaClass" ]; then 260 | if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then 261 | if [ "$MVNW_VERBOSE" = true ]; then 262 | echo " - Compiling MavenWrapperDownloader.java ..." 263 | fi 264 | # Compiling the Java class 265 | ("$JAVA_HOME/bin/javac" "$javaClass") 266 | fi 267 | if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then 268 | # Running the downloader 269 | if [ "$MVNW_VERBOSE" = true ]; then 270 | echo " - Running MavenWrapperDownloader.java ..." 271 | fi 272 | ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") 273 | fi 274 | fi 275 | fi 276 | fi 277 | ########################################################################################## 278 | # End of extension 279 | ########################################################################################## 280 | 281 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} 282 | if [ "$MVNW_VERBOSE" = true ]; then 283 | echo $MAVEN_PROJECTBASEDIR 284 | fi 285 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 286 | 287 | # For Cygwin, switch paths to Windows format before running java 288 | if $cygwin; then 289 | [ -n "$M2_HOME" ] && 290 | M2_HOME=`cygpath --path --windows "$M2_HOME"` 291 | [ -n "$JAVA_HOME" ] && 292 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 293 | [ -n "$CLASSPATH" ] && 294 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 295 | [ -n "$MAVEN_PROJECTBASEDIR" ] && 296 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` 297 | fi 298 | 299 | # Provide a "standardized" way to retrieve the CLI args that will 300 | # work with both Windows and non-Windows executions. 301 | MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" 302 | export MAVEN_CMD_LINE_ARGS 303 | 304 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 305 | 306 | exec "$JAVACMD" \ 307 | $MAVEN_OPTS \ 308 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 309 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 310 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" 311 | -------------------------------------------------------------------------------- /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 https://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 Maven 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 keystroke 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 set title of command window 39 | title %0 40 | @REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' 41 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 42 | 43 | @REM set %HOME% to equivalent of $HOME 44 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 45 | 46 | @REM Execute a user defined script before this one 47 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 48 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 49 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" 50 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" 51 | :skipRcPre 52 | 53 | @setlocal 54 | 55 | set ERROR_CODE=0 56 | 57 | @REM To isolate internal variables from possible post scripts, we use another setlocal 58 | @setlocal 59 | 60 | @REM ==== START VALIDATION ==== 61 | if not "%JAVA_HOME%" == "" goto OkJHome 62 | 63 | echo. 64 | echo Error: JAVA_HOME not found in your environment. >&2 65 | echo Please set the JAVA_HOME variable in your environment to match the >&2 66 | echo location of your Java installation. >&2 67 | echo. 68 | goto error 69 | 70 | :OkJHome 71 | if exist "%JAVA_HOME%\bin\java.exe" goto init 72 | 73 | echo. 74 | echo Error: JAVA_HOME is set to an invalid directory. >&2 75 | echo JAVA_HOME = "%JAVA_HOME%" >&2 76 | echo Please set the JAVA_HOME variable in your environment to match the >&2 77 | echo location of your Java installation. >&2 78 | echo. 79 | goto error 80 | 81 | @REM ==== END VALIDATION ==== 82 | 83 | :init 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 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" 121 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 122 | 123 | set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" 124 | 125 | FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( 126 | IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B 127 | ) 128 | 129 | @REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 130 | @REM This allows using the maven wrapper in projects that prohibit checking in binary data. 131 | if exist %WRAPPER_JAR% ( 132 | if "%MVNW_VERBOSE%" == "true" ( 133 | echo Found %WRAPPER_JAR% 134 | ) 135 | ) else ( 136 | if not "%MVNW_REPOURL%" == "" ( 137 | SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" 138 | ) 139 | if "%MVNW_VERBOSE%" == "true" ( 140 | echo Couldn't find %WRAPPER_JAR%, downloading it ... 141 | echo Downloading from: %DOWNLOAD_URL% 142 | ) 143 | 144 | powershell -Command "&{"^ 145 | "$webclient = new-object System.Net.WebClient;"^ 146 | "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ 147 | "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ 148 | "}"^ 149 | "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ 150 | "}" 151 | if "%MVNW_VERBOSE%" == "true" ( 152 | echo Finished downloading %WRAPPER_JAR% 153 | ) 154 | ) 155 | @REM End of extension 156 | 157 | @REM Provide a "standardized" way to retrieve the CLI args that will 158 | @REM work with both Windows and non-Windows executions. 159 | set MAVEN_CMD_LINE_ARGS=%* 160 | 161 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* 162 | if ERRORLEVEL 1 goto error 163 | goto end 164 | 165 | :error 166 | set ERROR_CODE=1 167 | 168 | :end 169 | @endlocal & set ERROR_CODE=%ERROR_CODE% 170 | 171 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost 172 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 173 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" 174 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" 175 | :skipRcPost 176 | 177 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 178 | if "%MAVEN_BATCH_PAUSE%" == "on" pause 179 | 180 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% 181 | 182 | exit /B %ERROR_CODE% 183 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.5.5 9 | 10 | 11 | com.school 12 | relationships 13 | 0.0.1-SNAPSHOT 14 | mockito_project 15 | Unit testing project 16 | 17 | 1.8 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-data-jpa 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-web 27 | 28 | 29 | 30 | mysql 31 | mysql-connector-java 32 | runtime 33 | 34 | 35 | org.projectlombok 36 | lombok 37 | true 38 | 39 | 40 | org.springframework.boot 41 | spring-boot-starter-test 42 | test 43 | 44 | 45 | org.junit.vintage 46 | junit-vintage-engine 47 | 48 | 49 | 50 | 51 | junit 52 | junit 53 | test 54 | jar 55 | 56 | 57 | org.springframework.boot 58 | spring-boot-devtools 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | maven-surefire-plugin 67 | 2.20.1 68 | 69 | 70 | **/*Tests.java 71 | 72 | 73 | 74 | 75 | org.jacoco 76 | jacoco-maven-plugin 77 | 0.8.4 78 | 79 | 80 | 81 | prepare-agent 82 | 83 | 84 | 85 | report 86 | prepare-package 87 | 88 | report 89 | 90 | 91 | 92 | 93 | 94 | org.springframework.boot 95 | spring-boot-maven-plugin 96 | 97 | 98 | 99 | org.projectlombok 100 | lombok 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/RelationshipsApplication.java: -------------------------------------------------------------------------------- 1 | package com.school.relationships; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class RelationshipsApplication { 8 | //https://hellokoding.com/jpa-hibernate-composite-primary-key-entity-mapping-example-with-mysql/ 9 | //https://www.baeldung.com/jpa-join-types 10 | //https://stackoverflow.com/questions/3325387/infinite-recursion-with-jackson-json-and-hibernate-jpa-issue 11 | public static void main(String[] args) { 12 | SpringApplication.run(RelationshipsApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/controllers/AdmissionFileController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.controllers; 7 | 8 | import com.fasterxml.jackson.core.JsonProcessingException; 9 | import com.fasterxml.jackson.databind.ObjectMapper; 10 | import com.school.relationships.models.AdmissionFileModel; 11 | import com.school.relationships.models.DormitoryModel; 12 | import com.school.relationships.services.AdmissionFileService; 13 | import java.util.HashMap; 14 | import java.util.Map; 15 | import lombok.extern.slf4j.Slf4j; 16 | import org.springframework.beans.factory.annotation.Autowired; 17 | import org.springframework.http.HttpStatus; 18 | import org.springframework.http.MediaType; 19 | import org.springframework.http.ResponseEntity; 20 | import org.springframework.web.bind.annotation.PostMapping; 21 | import org.springframework.web.bind.annotation.RequestBody; 22 | import org.springframework.web.bind.annotation.RequestMapping; 23 | import org.springframework.web.bind.annotation.RestController; 24 | 25 | /** 26 | * 27 | * @author hp 28 | */ 29 | @RestController 30 | @RequestMapping("/AdmissionFile") 31 | @Slf4j 32 | public class AdmissionFileController { 33 | 34 | @Autowired 35 | private AdmissionFileService admissionFileService; 36 | 37 | @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) 38 | public ResponseEntity createAdmissionFile(@RequestBody AdmissionFileModel model) throws JsonProcessingException { 39 | Map responseMap = new HashMap<>(); 40 | try { 41 | log.info("POST | Admission Creation Operation = {}", model); 42 | responseMap.put("Status", "Success"); 43 | Integer admissionNo = admissionFileService.createAdmissionFile(model); 44 | responseMap.put("AdmissionNo", admissionNo); 45 | return ResponseEntity.ok(new ObjectMapper().writeValueAsString(responseMap)); 46 | } catch (Exception e) { 47 | responseMap.put("Status", "Fail"); 48 | responseMap.put("Message", e.getMessage()); 49 | return new ResponseEntity<>(new ObjectMapper().writeValueAsString(responseMap), HttpStatus.INTERNAL_SERVER_ERROR); 50 | } 51 | 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/controllers/DormitoryController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.controllers; 7 | import com.fasterxml.jackson.core.JsonProcessingException; 8 | import com.fasterxml.jackson.databind.ObjectMapper; 9 | import com.school.relationships.models.DormitoryModel; 10 | import com.school.relationships.services.DormitoryService; 11 | import java.util.HashMap; 12 | import java.util.Map; 13 | import lombok.extern.slf4j.Slf4j; 14 | import org.springframework.beans.factory.annotation.Autowired; 15 | import org.springframework.http.HttpStatus; 16 | import org.springframework.http.MediaType; 17 | import org.springframework.http.ResponseEntity; 18 | import org.springframework.web.bind.annotation.PostMapping; 19 | import org.springframework.web.bind.annotation.RequestBody; 20 | import org.springframework.web.bind.annotation.RequestMapping; 21 | import org.springframework.web.bind.annotation.RestController; 22 | 23 | /** 24 | * 25 | * @author hp 26 | */ 27 | @RestController 28 | @RequestMapping("/Dormitory") 29 | @Slf4j 30 | public class DormitoryController { 31 | 32 | @Autowired 33 | private DormitoryService dormitoryService; 34 | 35 | @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) 36 | public ResponseEntity createDormitory(@RequestBody DormitoryModel model) throws JsonProcessingException { 37 | Map responseMap = new HashMap<>(); 38 | try { 39 | log.info("POST | Dormitory Creation Operation ={}", model); 40 | responseMap.put("Status", "Success"); 41 | Integer dormitoryID = dormitoryService.createDormitory(model); 42 | responseMap.put("DormitoryID", dormitoryID); 43 | return ResponseEntity.ok(new ObjectMapper().writeValueAsString(responseMap)); 44 | } catch (Exception e) { 45 | responseMap.put("Status", "Fail"); 46 | responseMap.put("Message", e.getMessage()); 47 | return new ResponseEntity<>(new ObjectMapper().writeValueAsString(responseMap), HttpStatus.INTERNAL_SERVER_ERROR); 48 | } 49 | 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/controllers/StudentController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.controllers; 7 | 8 | import com.fasterxml.jackson.core.JsonProcessingException; 9 | import com.fasterxml.jackson.databind.ObjectMapper; 10 | import com.school.relationships.entities.Student; 11 | import com.school.relationships.models.StudentModel; 12 | import com.school.relationships.services.StudentService; 13 | import java.util.HashMap; 14 | import java.util.Map; 15 | import java.util.Optional; 16 | import lombok.extern.slf4j.Slf4j; 17 | import org.springframework.beans.factory.annotation.Autowired; 18 | import org.springframework.http.HttpStatus; 19 | import org.springframework.http.MediaType; 20 | import org.springframework.http.ResponseEntity; 21 | import org.springframework.web.bind.annotation.GetMapping; 22 | import org.springframework.web.bind.annotation.PathVariable; 23 | import org.springframework.web.bind.annotation.PostMapping; 24 | import org.springframework.web.bind.annotation.RequestBody; 25 | import org.springframework.web.bind.annotation.RequestMapping; 26 | import org.springframework.web.bind.annotation.RestController; 27 | 28 | /** 29 | * 30 | * @author hp 31 | */ 32 | @RestController 33 | @RequestMapping("/Student") 34 | @Slf4j 35 | public class StudentController { 36 | 37 | @Autowired 38 | private StudentService studentService; 39 | 40 | @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) 41 | public ResponseEntity createStudent(@RequestBody StudentModel model) throws JsonProcessingException { 42 | Map responseMap = new HashMap<>(); 43 | try { 44 | log.info("POST | Student Creation Operation = {}", model); 45 | responseMap.put("Status", "Success"); 46 | studentService.createStudent(model); 47 | return ResponseEntity.ok(new ObjectMapper().writeValueAsString(responseMap)); 48 | } catch (Exception e) { 49 | responseMap.put("Status", "Fail"); 50 | responseMap.put("Message", e.getMessage()); 51 | return new ResponseEntity<>(new ObjectMapper().writeValueAsString(responseMap), HttpStatus.INTERNAL_SERVER_ERROR); 52 | } 53 | 54 | } 55 | 56 | @GetMapping(value = "/Subjects/{id}", produces = MediaType.APPLICATION_JSON_VALUE) 57 | public ResponseEntity subjectList(@PathVariable Integer id) throws JsonProcessingException { 58 | Map responseMap = new HashMap<>(); 59 | try { 60 | log.info("GET |Student Subjects Operation"); 61 | Optional result = studentService.findStudentWithTheirSubjects(id); 62 | responseMap.put("Status", "Success"); 63 | responseMap.put("Student's Subjects", result.get().getSubjectList()); 64 | return ResponseEntity.ok(new ObjectMapper().writeValueAsString(responseMap)); 65 | } catch (Exception e) { 66 | responseMap.put("Status", "Fail"); 67 | responseMap.put("Message", e.getMessage()); 68 | return new ResponseEntity<>(new ObjectMapper().writeValueAsString(responseMap), HttpStatus.INTERNAL_SERVER_ERROR); 69 | } 70 | 71 | } 72 | 73 | @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE) 74 | public ResponseEntity findStudent(@PathVariable Integer id) throws JsonProcessingException { 75 | Map responseMap = new HashMap<>(); 76 | try { 77 | log.info("GET |Student Operation"); 78 | Optional result = studentService.findStudentById(id); 79 | responseMap.put("Status", "Success"); 80 | responseMap.put("Student", result.get()); 81 | return ResponseEntity.ok(new ObjectMapper().writeValueAsString(responseMap)); 82 | } catch (Exception e) { 83 | responseMap.put("Status", "Fail"); 84 | responseMap.put("Message", e.getMessage()); 85 | return new ResponseEntity<>(new ObjectMapper().writeValueAsString(responseMap), HttpStatus.INTERNAL_SERVER_ERROR); 86 | } 87 | 88 | } 89 | 90 | } 91 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/controllers/StudentHasSubjectController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.controllers; 7 | 8 | import com.fasterxml.jackson.core.JsonProcessingException; 9 | import com.fasterxml.jackson.databind.ObjectMapper; 10 | import com.school.relationships.models.StudentHasSubjectModel; 11 | import com.school.relationships.models.StudentModel; 12 | import com.school.relationships.services.StudentSubjectService; 13 | import java.util.HashMap; 14 | import java.util.Map; 15 | import lombok.extern.slf4j.Slf4j; 16 | import org.springframework.beans.factory.annotation.Autowired; 17 | import org.springframework.http.HttpStatus; 18 | import org.springframework.http.MediaType; 19 | import org.springframework.http.ResponseEntity; 20 | import org.springframework.web.bind.annotation.PostMapping; 21 | import org.springframework.web.bind.annotation.RequestBody; 22 | import org.springframework.web.bind.annotation.RequestMapping; 23 | import org.springframework.web.bind.annotation.RestController; 24 | 25 | /** 26 | * 27 | * @author hp 28 | */ 29 | @RestController 30 | @RequestMapping("/StudentHasSubject") 31 | @Slf4j 32 | public class StudentHasSubjectController { 33 | 34 | @Autowired 35 | private StudentSubjectService studentSubjectService; 36 | 37 | @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) 38 | public ResponseEntity createStudentHasSubject(@RequestBody StudentHasSubjectModel model) throws JsonProcessingException { 39 | Map responseMap = new HashMap<>(); 40 | try { 41 | log.info("POST | Student-Subject Creation Operation = {}", model); 42 | responseMap.put("Status", "Success"); 43 | studentSubjectService.addStudentSubject(model); 44 | return ResponseEntity.ok(new ObjectMapper().writeValueAsString(responseMap)); 45 | } catch (Exception e) { 46 | responseMap.put("Status", "Fail"); 47 | responseMap.put("Message", e.getMessage()); 48 | return new ResponseEntity<>(new ObjectMapper().writeValueAsString(responseMap), HttpStatus.INTERNAL_SERVER_ERROR); 49 | } 50 | 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/controllers/SubjectController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.controllers; 7 | 8 | import com.fasterxml.jackson.core.JsonProcessingException; 9 | import com.fasterxml.jackson.databind.ObjectMapper; 10 | import com.school.relationships.models.DormitoryModel; 11 | import com.school.relationships.models.SubjectModel; 12 | import com.school.relationships.services.SubjectService; 13 | import java.util.HashMap; 14 | import java.util.Map; 15 | import lombok.extern.slf4j.Slf4j; 16 | import org.springframework.beans.factory.annotation.Autowired; 17 | import org.springframework.http.HttpStatus; 18 | import org.springframework.http.MediaType; 19 | import org.springframework.http.ResponseEntity; 20 | import org.springframework.web.bind.annotation.PostMapping; 21 | import org.springframework.web.bind.annotation.RequestBody; 22 | import org.springframework.web.bind.annotation.RequestMapping; 23 | import org.springframework.web.bind.annotation.RestController; 24 | 25 | /** 26 | * 27 | * @author hp 28 | */ 29 | @RestController 30 | @RequestMapping("/Subject") 31 | @Slf4j 32 | public class SubjectController { 33 | 34 | @Autowired 35 | private SubjectService subjectService; 36 | 37 | @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) 38 | public ResponseEntity createSubject(@RequestBody SubjectModel model) throws JsonProcessingException { 39 | Map responseMap = new HashMap<>(); 40 | try { 41 | log.info("POST | Subject Creation Operation ={}", model); 42 | responseMap.put("Status", "Success"); 43 | Integer subjectID = subjectService.createSubject(model); 44 | responseMap.put("SubjectID", subjectID); 45 | return ResponseEntity.ok(new ObjectMapper().writeValueAsString(responseMap)); 46 | } catch (Exception e) { 47 | responseMap.put("Status", "Fail"); 48 | responseMap.put("Message", e.getMessage()); 49 | return new ResponseEntity<>(new ObjectMapper().writeValueAsString(responseMap), HttpStatus.INTERNAL_SERVER_ERROR); 50 | } 51 | 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/controllers/TeacherController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.controllers; 7 | import com.fasterxml.jackson.core.JsonProcessingException; 8 | import com.fasterxml.jackson.databind.ObjectMapper; 9 | import com.school.relationships.entities.Teacher; 10 | import com.school.relationships.models.TeacherModel; 11 | import com.school.relationships.services.TeacherService; 12 | import java.util.HashMap; 13 | import java.util.Map; 14 | import java.util.Optional; 15 | import lombok.extern.slf4j.Slf4j; 16 | import org.springframework.beans.factory.annotation.Autowired; 17 | import org.springframework.http.HttpStatus; 18 | import org.springframework.http.MediaType; 19 | import org.springframework.http.ResponseEntity; 20 | import org.springframework.web.bind.annotation.GetMapping; 21 | import org.springframework.web.bind.annotation.PathVariable; 22 | import org.springframework.web.bind.annotation.PostMapping; 23 | import org.springframework.web.bind.annotation.RequestBody; 24 | import org.springframework.web.bind.annotation.RequestMapping; 25 | import org.springframework.web.bind.annotation.RestController; 26 | 27 | /** 28 | * 29 | * @author hp 30 | */ 31 | @RestController 32 | @RequestMapping("/Teacher") 33 | @Slf4j 34 | public class TeacherController { 35 | 36 | @Autowired 37 | private TeacherService teacherService; 38 | 39 | @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) 40 | public ResponseEntity createTeacher(@RequestBody TeacherModel model) throws JsonProcessingException { 41 | Map responseMap = new HashMap<>(); 42 | try { 43 | log.info("POST | Teacher Creation Operation = {}", model); 44 | responseMap.put("Status", "Success"); 45 | teacherService.createTeacher(model); 46 | return ResponseEntity.ok(new ObjectMapper().writeValueAsString(responseMap)); 47 | } catch (Exception e) { 48 | responseMap.put("Status", "Fail"); 49 | responseMap.put("Message", e.getMessage()); 50 | return new ResponseEntity<>(new ObjectMapper().writeValueAsString(responseMap), HttpStatus.INTERNAL_SERVER_ERROR); 51 | } 52 | } 53 | 54 | @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE) 55 | public ResponseEntity findTeacher(@PathVariable Integer id) throws JsonProcessingException { 56 | Map responseMap = new HashMap<>(); 57 | try { 58 | log.info("GET |Student Operation"); 59 | Optional result = teacherService.findTeacherById(id); 60 | responseMap.put("Status", "Success"); 61 | responseMap.put("Teacher", result.get()); 62 | return ResponseEntity.ok(new ObjectMapper().writeValueAsString(responseMap)); 63 | } catch (Exception e) { 64 | responseMap.put("Status", "Fail"); 65 | responseMap.put("Message", e.getMessage()); 66 | return new ResponseEntity<>(new ObjectMapper().writeValueAsString(responseMap), HttpStatus.INTERNAL_SERVER_ERROR); 67 | } 68 | 69 | } 70 | 71 | @GetMapping(value = "/Subjects/{id}", produces = MediaType.APPLICATION_JSON_VALUE) 72 | public ResponseEntity findSubjectsByTeacher(@PathVariable Integer id) throws JsonProcessingException { 73 | Map responseMap = new HashMap<>(); 74 | try { 75 | log.info("GET |Student Operation"); 76 | Optional result = teacherService.findTeacherAndSubjectsTaught(id); 77 | responseMap.put("Status", "Success"); 78 | responseMap.put("Teacher's Subjects", result.get().getSubjectList()); 79 | return ResponseEntity.ok(new ObjectMapper().writeValueAsString(responseMap)); 80 | } catch (Exception e) { 81 | responseMap.put("Status", "Fail"); 82 | responseMap.put("Message", e.getMessage()); 83 | return new ResponseEntity<>(new ObjectMapper().writeValueAsString(responseMap), HttpStatus.INTERNAL_SERVER_ERROR); 84 | } 85 | 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/controllers/TeacherHasSubjectController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.controllers; 7 | 8 | import com.fasterxml.jackson.core.JsonProcessingException; 9 | import com.fasterxml.jackson.databind.ObjectMapper; 10 | import com.school.relationships.models.TeacherHasSubjectModel; 11 | import com.school.relationships.services.TeacherSubjectService; 12 | import java.util.HashMap; 13 | import java.util.Map; 14 | import lombok.extern.slf4j.Slf4j; 15 | import org.springframework.beans.factory.annotation.Autowired; 16 | import org.springframework.http.HttpStatus; 17 | import org.springframework.http.MediaType; 18 | import org.springframework.http.ResponseEntity; 19 | import org.springframework.web.bind.annotation.PostMapping; 20 | import org.springframework.web.bind.annotation.RequestBody; 21 | import org.springframework.web.bind.annotation.RequestMapping; 22 | import org.springframework.web.bind.annotation.RestController; 23 | 24 | /** 25 | * 26 | * @author hp 27 | */ 28 | @RestController 29 | @RequestMapping("/TeacherHasSubject") 30 | @Slf4j 31 | public class TeacherHasSubjectController { 32 | 33 | @Autowired 34 | private TeacherSubjectService teacherSubjectService; 35 | 36 | @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) 37 | public ResponseEntity createTeacherHasSubject(@RequestBody TeacherHasSubjectModel model) throws JsonProcessingException { 38 | Map responseMap = new HashMap<>(); 39 | try { 40 | log.info("POST | Teacher-Subject Creation Operation = {}", model); 41 | responseMap.put("Status", "Success"); 42 | teacherSubjectService.addTeacherSubject(model); 43 | return ResponseEntity.ok(new ObjectMapper().writeValueAsString(responseMap)); 44 | } catch (Exception e) { 45 | responseMap.put("Status", "Fail"); 46 | responseMap.put("Message", e.getMessage()); 47 | return new ResponseEntity<>(new ObjectMapper().writeValueAsString(responseMap), HttpStatus.INTERNAL_SERVER_ERROR); 48 | } 49 | 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/entities/Admissionfile.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.entities; 7 | 8 | import java.io.Serializable; 9 | import java.util.List; 10 | import javax.persistence.Basic; 11 | import javax.persistence.CascadeType; 12 | import javax.persistence.Column; 13 | import javax.persistence.Entity; 14 | import javax.persistence.FetchType; 15 | import javax.persistence.GeneratedValue; 16 | import javax.persistence.GenerationType; 17 | import javax.persistence.Id; 18 | import javax.persistence.NamedQueries; 19 | import javax.persistence.NamedQuery; 20 | import javax.persistence.OneToMany; 21 | import javax.persistence.Table; 22 | import javax.xml.bind.annotation.XmlRootElement; 23 | import javax.xml.bind.annotation.XmlTransient; 24 | 25 | /** 26 | * 27 | * @author hp 28 | */ 29 | @Entity 30 | @Table(name = "admissionfile") 31 | @XmlRootElement 32 | @NamedQueries({ 33 | @NamedQuery(name = "Admissionfile.findAll", query = "SELECT a FROM Admissionfile a"), 34 | @NamedQuery(name = "Admissionfile.findByIdadmissionfile", query = "SELECT a FROM Admissionfile a WHERE a.idadmissionfile = :idadmissionfile"), 35 | @NamedQuery(name = "Admissionfile.findByLeavingCertificate", query = "SELECT a FROM Admissionfile a WHERE a.leavingCertificate = :leavingCertificate"), 36 | @NamedQuery(name = "Admissionfile.findByGuardian1Name", query = "SELECT a FROM Admissionfile a WHERE a.guardian1Name = :guardian1Name"), 37 | @NamedQuery(name = "Admissionfile.findByGuadian1ContactInfo", query = "SELECT a FROM Admissionfile a WHERE a.guadian1ContactInfo = :guadian1ContactInfo"), 38 | @NamedQuery(name = "Admissionfile.findByGuardian2Name", query = "SELECT a FROM Admissionfile a WHERE a.guardian2Name = :guardian2Name"), 39 | @NamedQuery(name = "Admissionfile.findByGuadian2ContactInfo", query = "SELECT a FROM Admissionfile a WHERE a.guadian2ContactInfo = :guadian2ContactInfo"), 40 | @NamedQuery(name = "Admissionfile.findByPreviousSchool", query = "SELECT a FROM Admissionfile a WHERE a.previousSchool = :previousSchool"), 41 | @NamedQuery(name = "Admissionfile.findByPreviousSchoolLeavingDocument", query = "SELECT a FROM Admissionfile a WHERE a.previousSchoolLeavingDocument = :previousSchoolLeavingDocument"), 42 | @NamedQuery(name = "Admissionfile.findByMedicalDocument", query = "SELECT a FROM Admissionfile a WHERE a.medicalDocument = :medicalDocument")}) 43 | public class Admissionfile implements Serializable { 44 | 45 | private static final long serialVersionUID = 1L; 46 | @Id 47 | @GeneratedValue(strategy = GenerationType.IDENTITY) 48 | @Basic(optional = false) 49 | @Column(name = "idadmissionfile") 50 | private Integer idadmissionfile; 51 | @Column(name = "leaving_certificate") 52 | private String leavingCertificate; 53 | @Column(name = "guardian_1_name") 54 | private String guardian1Name; 55 | @Column(name = "guadian_1_contact_info") 56 | private String guadian1ContactInfo; 57 | @Column(name = "guardian_2_name") 58 | private String guardian2Name; 59 | @Column(name = "guadian_2_contact_info") 60 | private String guadian2ContactInfo; 61 | @Column(name = "previous_school") 62 | private String previousSchool; 63 | @Column(name = "previous_school_leaving_document") 64 | private String previousSchoolLeavingDocument; 65 | @Column(name = "medical_document") 66 | private String medicalDocument; 67 | @OneToMany(cascade = CascadeType.ALL, mappedBy = "fileNo", fetch = FetchType.LAZY) 68 | private List studentList; 69 | 70 | public Admissionfile() { 71 | } 72 | 73 | public Admissionfile(Integer idadmissionfile) { 74 | this.idadmissionfile = idadmissionfile; 75 | } 76 | 77 | public Integer getIdadmissionfile() { 78 | return idadmissionfile; 79 | } 80 | 81 | public void setIdadmissionfile(Integer idadmissionfile) { 82 | this.idadmissionfile = idadmissionfile; 83 | } 84 | 85 | public String getLeavingCertificate() { 86 | return leavingCertificate; 87 | } 88 | 89 | public void setLeavingCertificate(String leavingCertificate) { 90 | this.leavingCertificate = leavingCertificate; 91 | } 92 | 93 | public String getGuardian1Name() { 94 | return guardian1Name; 95 | } 96 | 97 | public void setGuardian1Name(String guardian1Name) { 98 | this.guardian1Name = guardian1Name; 99 | } 100 | 101 | public String getGuadian1ContactInfo() { 102 | return guadian1ContactInfo; 103 | } 104 | 105 | public void setGuadian1ContactInfo(String guadian1ContactInfo) { 106 | this.guadian1ContactInfo = guadian1ContactInfo; 107 | } 108 | 109 | public String getGuardian2Name() { 110 | return guardian2Name; 111 | } 112 | 113 | public void setGuardian2Name(String guardian2Name) { 114 | this.guardian2Name = guardian2Name; 115 | } 116 | 117 | public String getGuadian2ContactInfo() { 118 | return guadian2ContactInfo; 119 | } 120 | 121 | public void setGuadian2ContactInfo(String guadian2ContactInfo) { 122 | this.guadian2ContactInfo = guadian2ContactInfo; 123 | } 124 | 125 | public String getPreviousSchool() { 126 | return previousSchool; 127 | } 128 | 129 | public void setPreviousSchool(String previousSchool) { 130 | this.previousSchool = previousSchool; 131 | } 132 | 133 | public String getPreviousSchoolLeavingDocument() { 134 | return previousSchoolLeavingDocument; 135 | } 136 | 137 | public void setPreviousSchoolLeavingDocument(String previousSchoolLeavingDocument) { 138 | this.previousSchoolLeavingDocument = previousSchoolLeavingDocument; 139 | } 140 | 141 | public String getMedicalDocument() { 142 | return medicalDocument; 143 | } 144 | 145 | public void setMedicalDocument(String medicalDocument) { 146 | this.medicalDocument = medicalDocument; 147 | } 148 | 149 | @XmlTransient 150 | public List getStudentList() { 151 | return studentList; 152 | } 153 | 154 | public void setStudentList(List studentList) { 155 | this.studentList = studentList; 156 | } 157 | 158 | @Override 159 | public int hashCode() { 160 | int hash = 0; 161 | hash += (idadmissionfile != null ? idadmissionfile.hashCode() : 0); 162 | return hash; 163 | } 164 | 165 | @Override 166 | public boolean equals(Object object) { 167 | // TODO: Warning - this method won't work in the case the id fields are not set 168 | if (!(object instanceof Admissionfile)) { 169 | return false; 170 | } 171 | Admissionfile other = (Admissionfile) object; 172 | if ((this.idadmissionfile == null && other.idadmissionfile != null) || (this.idadmissionfile != null && !this.idadmissionfile.equals(other.idadmissionfile))) { 173 | return false; 174 | } 175 | return true; 176 | } 177 | 178 | @Override 179 | public String toString() { 180 | return "org.elearning.elearningentities.Admissionfile[ idadmissionfile=" + idadmissionfile + " ]"; 181 | } 182 | 183 | } 184 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/entities/Dormitory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.entities; 7 | 8 | import java.io.Serializable; 9 | import java.util.List; 10 | import javax.persistence.Basic; 11 | import javax.persistence.CascadeType; 12 | import javax.persistence.Column; 13 | import javax.persistence.Entity; 14 | import javax.persistence.FetchType; 15 | import javax.persistence.GeneratedValue; 16 | import javax.persistence.GenerationType; 17 | import javax.persistence.Id; 18 | import javax.persistence.NamedQueries; 19 | import javax.persistence.NamedQuery; 20 | import javax.persistence.OneToMany; 21 | import javax.persistence.Table; 22 | import javax.xml.bind.annotation.XmlRootElement; 23 | import javax.xml.bind.annotation.XmlTransient; 24 | 25 | /** 26 | * 27 | * @author hp 28 | */ 29 | @Entity 30 | @Table(name = "dormitory") 31 | @XmlRootElement 32 | @NamedQueries({ 33 | @NamedQuery(name = "Dormitory.findAll", query = "SELECT d FROM Dormitory d"), 34 | @NamedQuery(name = "Dormitory.findByIddormitory", query = "SELECT d FROM Dormitory d WHERE d.iddormitory = :iddormitory"), 35 | @NamedQuery(name = "Dormitory.findByDormitoryname", query = "SELECT d FROM Dormitory d WHERE d.dormitoryname = :dormitoryname"), 36 | @NamedQuery(name = "Dormitory.findByNoOfWings", query = "SELECT d FROM Dormitory d WHERE d.noOfWings = :noOfWings")}) 37 | public class Dormitory implements Serializable { 38 | 39 | private static final long serialVersionUID = 1L; 40 | @Id 41 | @GeneratedValue(strategy = GenerationType.IDENTITY) 42 | @Basic(optional = false) 43 | @Column(name = "iddormitory") 44 | private Integer iddormitory; 45 | @Column(name = "dormitoryname") 46 | private String dormitoryname; 47 | @Column(name = "no_of_wings") 48 | private Integer noOfWings; 49 | @OneToMany(cascade = CascadeType.ALL, mappedBy = "dormitoryIddormitory", fetch = FetchType.LAZY) 50 | private List studentList; 51 | 52 | public Dormitory() { 53 | } 54 | 55 | public Dormitory(Integer iddormitory) { 56 | this.iddormitory = iddormitory; 57 | } 58 | 59 | public Integer getIddormitory() { 60 | return iddormitory; 61 | } 62 | 63 | public void setIddormitory(Integer iddormitory) { 64 | this.iddormitory = iddormitory; 65 | } 66 | 67 | public String getDormitoryname() { 68 | return dormitoryname; 69 | } 70 | 71 | public void setDormitoryname(String dormitoryname) { 72 | this.dormitoryname = dormitoryname; 73 | } 74 | 75 | public Integer getNoOfWings() { 76 | return noOfWings; 77 | } 78 | 79 | public void setNoOfWings(Integer noOfWings) { 80 | this.noOfWings = noOfWings; 81 | } 82 | 83 | @XmlTransient 84 | public List getStudentList() { 85 | return studentList; 86 | } 87 | 88 | public void setStudentList(List studentList) { 89 | this.studentList = studentList; 90 | } 91 | 92 | @Override 93 | public int hashCode() { 94 | int hash = 0; 95 | hash += (iddormitory != null ? iddormitory.hashCode() : 0); 96 | return hash; 97 | } 98 | 99 | @Override 100 | public boolean equals(Object object) { 101 | // TODO: Warning - this method won't work in the case the id fields are not set 102 | if (!(object instanceof Dormitory)) { 103 | return false; 104 | } 105 | Dormitory other = (Dormitory) object; 106 | if ((this.iddormitory == null && other.iddormitory != null) || (this.iddormitory != null && !this.iddormitory.equals(other.iddormitory))) { 107 | return false; 108 | } 109 | return true; 110 | } 111 | 112 | @Override 113 | public String toString() { 114 | return "org.elearning.elearningentities.Dormitory[ iddormitory=" + iddormitory + " ]"; 115 | } 116 | 117 | } 118 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/entities/Student.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.entities; 7 | 8 | import com.fasterxml.jackson.annotation.JsonFormat; 9 | import com.fasterxml.jackson.annotation.JsonIgnore; 10 | import com.fasterxml.jackson.annotation.JsonManagedReference; 11 | import java.io.Serializable; 12 | import java.util.Date; 13 | import java.util.List; 14 | import javax.persistence.Basic; 15 | import javax.persistence.Column; 16 | import javax.persistence.Entity; 17 | import javax.persistence.FetchType; 18 | import javax.persistence.GeneratedValue; 19 | import javax.persistence.GenerationType; 20 | import javax.persistence.Id; 21 | import javax.persistence.JoinColumn; 22 | import javax.persistence.JoinTable; 23 | import javax.persistence.ManyToMany; 24 | import javax.persistence.ManyToOne; 25 | import javax.persistence.NamedQueries; 26 | import javax.persistence.NamedQuery; 27 | import javax.persistence.Table; 28 | import javax.persistence.Temporal; 29 | import javax.persistence.TemporalType; 30 | import javax.xml.bind.annotation.XmlRootElement; 31 | import javax.xml.bind.annotation.XmlTransient; 32 | 33 | /** 34 | * 35 | * @author hp 36 | */ 37 | @Entity 38 | @Table(name = "student") 39 | @XmlRootElement 40 | @NamedQueries({ 41 | @NamedQuery(name = "Student.findAll", query = "SELECT s FROM Student s"), 42 | @NamedQuery(name = "Student.findByIdstudent", query = "SELECT s FROM Student s WHERE s.idstudent = :idstudent"), 43 | @NamedQuery(name = "Student.findBySurname", query = "SELECT s FROM Student s WHERE s.surname = :surname"), 44 | @NamedQuery(name = "Student.findByFirstName", query = "SELECT s FROM Student s WHERE s.firstName = :firstName"), 45 | @NamedQuery(name = "Student.findByLastName", query = "SELECT s FROM Student s WHERE s.lastName = :lastName"), 46 | @NamedQuery(name = "Student.findByDob", query = "SELECT s FROM Student s WHERE s.dob = :dob")}) 47 | public class Student implements Serializable { 48 | 49 | private static final long serialVersionUID = 1L; 50 | @JsonIgnore 51 | @Id 52 | @GeneratedValue(strategy = GenerationType.IDENTITY) 53 | @Basic(optional = false) 54 | @Column(name = "idstudent") 55 | private Integer idstudent; 56 | @Column(name = "surname") 57 | private String surname; 58 | @Column(name = "first_name") 59 | private String firstName; 60 | @Column(name = "last_name") 61 | private String lastName; 62 | @Column(name = "dob") 63 | @Temporal(TemporalType.DATE) 64 | private Date dob; 65 | //@JsonManagedReference 66 | @JsonIgnore 67 | @JoinTable(name = "student_has_subject", joinColumns = { 68 | @JoinColumn(name = "student_idstudent", referencedColumnName = "idstudent")}, inverseJoinColumns = { 69 | @JoinColumn(name = "subject_idsubject", referencedColumnName = "idsubject")}) 70 | @ManyToMany(fetch = FetchType.LAZY) 71 | private List subjectList; 72 | @JsonIgnore 73 | @JoinColumn(name = "file_no", referencedColumnName = "idadmissionfile") 74 | @ManyToOne(optional = false, fetch = FetchType.LAZY) 75 | private Admissionfile fileNo; 76 | @JsonIgnore 77 | @JoinColumn(name = "dormitory_iddormitory", referencedColumnName = "iddormitory") 78 | @ManyToOne(optional = false, fetch = FetchType.LAZY) 79 | private Dormitory dormitoryIddormitory; 80 | 81 | public Student() { 82 | } 83 | 84 | public Student(Integer idstudent) { 85 | this.idstudent = idstudent; 86 | } 87 | 88 | public Integer getIdstudent() { 89 | return idstudent; 90 | } 91 | 92 | public void setIdstudent(Integer idstudent) { 93 | this.idstudent = idstudent; 94 | } 95 | 96 | public String getSurname() { 97 | return surname; 98 | } 99 | 100 | public void setSurname(String surname) { 101 | this.surname = surname; 102 | } 103 | 104 | public String getFirstName() { 105 | return firstName; 106 | } 107 | 108 | public void setFirstName(String firstName) { 109 | this.firstName = firstName; 110 | } 111 | 112 | public String getLastName() { 113 | return lastName; 114 | } 115 | 116 | public void setLastName(String lastName) { 117 | this.lastName = lastName; 118 | } 119 | 120 | public Date getDob() { 121 | return dob; 122 | } 123 | 124 | public void setDob(Date dob) { 125 | this.dob = dob; 126 | } 127 | 128 | @XmlTransient 129 | public List getSubjectList() { 130 | return subjectList; 131 | } 132 | 133 | public void setSubjectList(List subjectList) { 134 | this.subjectList = subjectList; 135 | } 136 | 137 | public Admissionfile getFileNo() { 138 | return fileNo; 139 | } 140 | 141 | public void setFileNo(Admissionfile fileNo) { 142 | this.fileNo = fileNo; 143 | } 144 | 145 | public Dormitory getDormitoryIddormitory() { 146 | return dormitoryIddormitory; 147 | } 148 | 149 | public void setDormitoryIddormitory(Dormitory dormitoryIddormitory) { 150 | this.dormitoryIddormitory = dormitoryIddormitory; 151 | } 152 | 153 | @Override 154 | public int hashCode() { 155 | int hash = 0; 156 | hash += (idstudent != null ? idstudent.hashCode() : 0); 157 | return hash; 158 | } 159 | 160 | @Override 161 | public boolean equals(Object object) { 162 | // TODO: Warning - this method won't work in the case the id fields are not set 163 | if (!(object instanceof Student)) { 164 | return false; 165 | } 166 | Student other = (Student) object; 167 | if ((this.idstudent == null && other.idstudent != null) || (this.idstudent != null && !this.idstudent.equals(other.idstudent))) { 168 | return false; 169 | } 170 | return true; 171 | } 172 | 173 | @Override 174 | public String toString() { 175 | return "org.elearning.elearningentities.Student[ idstudent=" + idstudent + " ]"; 176 | } 177 | 178 | } 179 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/entities/StudentHasSubject.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.entities; 7 | 8 | import java.io.Serializable; 9 | import javax.persistence.EmbeddedId; 10 | import javax.persistence.Entity; 11 | import javax.persistence.Table; 12 | import javax.xml.bind.annotation.XmlRootElement; 13 | 14 | /** 15 | * 16 | * @author hp 17 | */ 18 | @Entity 19 | @Table(name = "student_has_subject") 20 | @XmlRootElement 21 | public class StudentHasSubject implements Serializable { 22 | 23 | private static final long serialVersionUID = 1L; 24 | @EmbeddedId 25 | private StudentHasSubjectPK studentHasSubjectPK; 26 | 27 | public StudentHasSubject() { 28 | } 29 | 30 | /** 31 | * @return the studentHasSubjectPK 32 | */ 33 | public StudentHasSubjectPK getStudentHasSubjectPK() { 34 | return studentHasSubjectPK; 35 | } 36 | 37 | /** 38 | * @param studentHasSubjectPK the studentHasSubjectPK to set 39 | */ 40 | public void setStudentHasSubjectPK(StudentHasSubjectPK studentHasSubjectPK) { 41 | this.studentHasSubjectPK = studentHasSubjectPK; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/entities/StudentHasSubjectPK.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.entities; 7 | 8 | import java.io.Serializable; 9 | import javax.persistence.Basic; 10 | import javax.persistence.Column; 11 | import javax.persistence.Embeddable; 12 | 13 | /** 14 | * 15 | * @author hp 16 | */ 17 | @Embeddable 18 | public class StudentHasSubjectPK implements Serializable { 19 | 20 | @Basic(optional = false) 21 | @Column(name = "student_idstudent") 22 | private int studentID; 23 | 24 | @Basic(optional = false) 25 | @Column(name = "subject_idsubject") 26 | private int subjectID; 27 | 28 | public StudentHasSubjectPK() { 29 | } 30 | 31 | public StudentHasSubjectPK(int studentID, int subjectID) { 32 | this.studentID = studentID; 33 | this.subjectID = subjectID; 34 | } 35 | 36 | @Override 37 | public int hashCode() { 38 | int hash = 7; 39 | hash = 37 * hash + this.studentID; 40 | hash = 37 * hash + this.subjectID; 41 | return hash; 42 | } 43 | 44 | @Override 45 | public boolean equals(Object obj) { 46 | if (this == obj) { 47 | return true; 48 | } 49 | if (obj == null) { 50 | return false; 51 | } 52 | if (getClass() != obj.getClass()) { 53 | return false; 54 | } 55 | final StudentHasSubjectPK other = (StudentHasSubjectPK) obj; 56 | if (this.studentID != other.studentID) { 57 | return false; 58 | } 59 | if (this.subjectID != other.subjectID) { 60 | return false; 61 | } 62 | return true; 63 | } 64 | 65 | /** 66 | * @return the studentID 67 | */ 68 | public int getStudentID() { 69 | return studentID; 70 | } 71 | 72 | /** 73 | * @param studentID the studentID to set 74 | */ 75 | public void setStudentID(int studentID) { 76 | this.studentID = studentID; 77 | } 78 | 79 | /** 80 | * @return the subjectID 81 | */ 82 | public int getSubjectID() { 83 | return subjectID; 84 | } 85 | 86 | /** 87 | * @param subjectID the subjectID to set 88 | */ 89 | public void setSubjectID(int subjectID) { 90 | this.subjectID = subjectID; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/entities/Subject.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.entities; 7 | 8 | import com.fasterxml.jackson.annotation.JsonBackReference; 9 | import com.fasterxml.jackson.annotation.JsonIgnore; 10 | import java.io.Serializable; 11 | import java.util.List; 12 | import javax.persistence.Basic; 13 | import javax.persistence.Column; 14 | import javax.persistence.Entity; 15 | import javax.persistence.FetchType; 16 | import javax.persistence.GeneratedValue; 17 | import javax.persistence.GenerationType; 18 | import javax.persistence.Id; 19 | import javax.persistence.JoinColumn; 20 | import javax.persistence.JoinTable; 21 | import javax.persistence.ManyToMany; 22 | import javax.persistence.NamedQueries; 23 | import javax.persistence.NamedQuery; 24 | import javax.persistence.Table; 25 | import javax.xml.bind.annotation.XmlRootElement; 26 | import javax.xml.bind.annotation.XmlTransient; 27 | 28 | /** 29 | * 30 | * @author hp 31 | */ 32 | @Entity 33 | @Table(name = "subject") 34 | @XmlRootElement 35 | @NamedQueries({ 36 | @NamedQuery(name = "Subject.findAll", query = "SELECT s FROM Subject s"), 37 | @NamedQuery(name = "Subject.findByIdsubject", query = "SELECT s FROM Subject s WHERE s.idsubject = :idsubject"), 38 | @NamedQuery(name = "Subject.findBySubjectname", query = "SELECT s FROM Subject s WHERE s.subjectname = :subjectname")}) 39 | public class Subject implements Serializable { 40 | 41 | private static final long serialVersionUID = 1L; 42 | @JsonIgnore 43 | @Id 44 | @GeneratedValue(strategy = GenerationType.IDENTITY) 45 | @Basic(optional = false) 46 | @Column(name = "idsubject") 47 | private Integer idsubject; 48 | @Column(name = "subjectname") 49 | private String subjectname; 50 | //@JsonBackReference 51 | @JsonIgnore 52 | @ManyToMany(mappedBy = "subjectList", fetch = FetchType.LAZY) 53 | private List studentList; 54 | @JsonIgnore 55 | @JoinTable(name = "teacher_has_subject", joinColumns = { 56 | @JoinColumn(name = "subject_idsubject", referencedColumnName = "idsubject")}, inverseJoinColumns = { 57 | @JoinColumn(name = "teacher_idteacher", referencedColumnName = "idteacher")}) 58 | @ManyToMany(fetch = FetchType.LAZY) 59 | private List teacherList; 60 | 61 | public Subject() { 62 | } 63 | 64 | public Subject(Integer idsubject) { 65 | this.idsubject = idsubject; 66 | } 67 | 68 | public Integer getIdsubject() { 69 | return idsubject; 70 | } 71 | 72 | public void setIdsubject(Integer idsubject) { 73 | this.idsubject = idsubject; 74 | } 75 | 76 | public String getSubjectname() { 77 | return subjectname; 78 | } 79 | 80 | public void setSubjectname(String subjectname) { 81 | this.subjectname = subjectname; 82 | } 83 | 84 | @XmlTransient 85 | public List getStudentList() { 86 | return studentList; 87 | } 88 | 89 | public void setStudentList(List studentList) { 90 | this.studentList = studentList; 91 | } 92 | 93 | @XmlTransient 94 | public List getTeacherList() { 95 | return teacherList; 96 | } 97 | 98 | public void setTeacherList(List teacherList) { 99 | this.teacherList = teacherList; 100 | } 101 | 102 | @Override 103 | public int hashCode() { 104 | int hash = 0; 105 | hash += (idsubject != null ? idsubject.hashCode() : 0); 106 | return hash; 107 | } 108 | 109 | @Override 110 | public boolean equals(Object object) { 111 | // TODO: Warning - this method won't work in the case the id fields are not set 112 | if (!(object instanceof Subject)) { 113 | return false; 114 | } 115 | Subject other = (Subject) object; 116 | if ((this.idsubject == null && other.idsubject != null) || (this.idsubject != null && !this.idsubject.equals(other.idsubject))) { 117 | return false; 118 | } 119 | return true; 120 | } 121 | 122 | @Override 123 | public String toString() { 124 | return "org.elearning.elearningentities.Subject[ idsubject=" + idsubject + " ]"; 125 | } 126 | 127 | } 128 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/entities/Teacher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.entities; 7 | 8 | import com.fasterxml.jackson.annotation.JsonIgnore; 9 | import java.io.Serializable; 10 | import java.util.List; 11 | import javax.persistence.Basic; 12 | import javax.persistence.Column; 13 | import javax.persistence.Entity; 14 | import javax.persistence.FetchType; 15 | import javax.persistence.GeneratedValue; 16 | import javax.persistence.GenerationType; 17 | import javax.persistence.Id; 18 | import javax.persistence.ManyToMany; 19 | import javax.persistence.NamedQueries; 20 | import javax.persistence.NamedQuery; 21 | import javax.persistence.Table; 22 | import javax.xml.bind.annotation.XmlRootElement; 23 | import javax.xml.bind.annotation.XmlTransient; 24 | 25 | /** 26 | * 27 | * @author hp 28 | */ 29 | @Entity 30 | @Table(name = "teacher") 31 | @XmlRootElement 32 | @NamedQueries({ 33 | @NamedQuery(name = "Teacher.findAll", query = "SELECT t FROM Teacher t"), 34 | @NamedQuery(name = "Teacher.findByIdteacher", query = "SELECT t FROM Teacher t WHERE t.idteacher = :idteacher"), 35 | @NamedQuery(name = "Teacher.findByFullName", query = "SELECT t FROM Teacher t WHERE t.fullName = :fullName"), 36 | @NamedQuery(name = "Teacher.findByTeacherNo", query = "SELECT t FROM Teacher t WHERE t.teacherNo = :teacherNo")}) 37 | public class Teacher implements Serializable { 38 | 39 | private static final long serialVersionUID = 1L; 40 | @JsonIgnore 41 | @Id 42 | @GeneratedValue(strategy = GenerationType.IDENTITY) 43 | @Basic(optional = false) 44 | @Column(name = "idteacher") 45 | private Integer idteacher; 46 | @Column(name = "full_name") 47 | private String fullName; 48 | @Column(name = "teacher_no") 49 | private String teacherNo; 50 | @JsonIgnore 51 | @ManyToMany(mappedBy = "teacherList", fetch = FetchType.LAZY) 52 | private List subjectList; 53 | 54 | public Teacher() { 55 | } 56 | 57 | public Teacher(Integer idteacher) { 58 | this.idteacher = idteacher; 59 | } 60 | 61 | public Integer getIdteacher() { 62 | return idteacher; 63 | } 64 | 65 | public void setIdteacher(Integer idteacher) { 66 | this.idteacher = idteacher; 67 | } 68 | 69 | public String getFullName() { 70 | return fullName; 71 | } 72 | 73 | public void setFullName(String fullName) { 74 | this.fullName = fullName; 75 | } 76 | 77 | public String getTeacherNo() { 78 | return teacherNo; 79 | } 80 | 81 | public void setTeacherNo(String teacherNo) { 82 | this.teacherNo = teacherNo; 83 | } 84 | 85 | @XmlTransient 86 | public List getSubjectList() { 87 | return subjectList; 88 | } 89 | 90 | public void setSubjectList(List subjectList) { 91 | this.subjectList = subjectList; 92 | } 93 | 94 | @Override 95 | public int hashCode() { 96 | int hash = 0; 97 | hash += (idteacher != null ? idteacher.hashCode() : 0); 98 | return hash; 99 | } 100 | 101 | @Override 102 | public boolean equals(Object object) { 103 | // TODO: Warning - this method won't work in the case the id fields are not set 104 | if (!(object instanceof Teacher)) { 105 | return false; 106 | } 107 | Teacher other = (Teacher) object; 108 | if ((this.idteacher == null && other.idteacher != null) || (this.idteacher != null && !this.idteacher.equals(other.idteacher))) { 109 | return false; 110 | } 111 | return true; 112 | } 113 | 114 | @Override 115 | public String toString() { 116 | return "org.elearning.elearningentities.Teacher[ idteacher=" + idteacher + " ]"; 117 | } 118 | 119 | } 120 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/entities/TeacherHasSubject.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.entities; 7 | 8 | import java.io.Serializable; 9 | import java.util.Objects; 10 | import javax.persistence.EmbeddedId; 11 | import javax.persistence.Entity; 12 | import javax.persistence.Table; 13 | import javax.xml.bind.annotation.XmlRootElement; 14 | 15 | /** 16 | * 17 | * @author hp 18 | */ 19 | @Entity 20 | @Table(name = "teacher_has_subject") 21 | @XmlRootElement 22 | public class TeacherHasSubject implements Serializable { 23 | 24 | private static final long serialVersionUID = 1L; 25 | @EmbeddedId 26 | private TeacherHasSubjectPK teacherHasSubjectPK; 27 | 28 | @Override 29 | public int hashCode() { 30 | int hash = 5; 31 | hash = 89 * hash + Objects.hashCode(this.teacherHasSubjectPK); 32 | return hash; 33 | } 34 | 35 | @Override 36 | public boolean equals(Object obj) { 37 | if (this == obj) { 38 | return true; 39 | } 40 | if (obj == null) { 41 | return false; 42 | } 43 | if (getClass() != obj.getClass()) { 44 | return false; 45 | } 46 | final TeacherHasSubject other = (TeacherHasSubject) obj; 47 | if (!Objects.equals(this.teacherHasSubjectPK, other.teacherHasSubjectPK)) { 48 | return false; 49 | } 50 | return true; 51 | } 52 | 53 | /** 54 | * @return the teacherHasSubjectPK 55 | */ 56 | public TeacherHasSubjectPK getTeacherHasSubjectPK() { 57 | return teacherHasSubjectPK; 58 | } 59 | 60 | /** 61 | * @param teacherHasSubjectPK the teacherHasSubjectPK to set 62 | */ 63 | public void setTeacherHasSubjectPK(TeacherHasSubjectPK teacherHasSubjectPK) { 64 | this.teacherHasSubjectPK = teacherHasSubjectPK; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/entities/TeacherHasSubjectPK.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.entities; 7 | 8 | import java.io.Serializable; 9 | import javax.persistence.Basic; 10 | import javax.persistence.Column; 11 | import javax.persistence.Embeddable; 12 | 13 | /** 14 | * 15 | * @author hp 16 | */ 17 | @Embeddable 18 | public class TeacherHasSubjectPK implements Serializable { 19 | 20 | @Basic(optional = false) 21 | @Column(name = "teacher_idteacher") 22 | private int teacherID; 23 | 24 | @Basic(optional = false) 25 | @Column(name = "subject_idsubject") 26 | private int subjectID; 27 | 28 | public TeacherHasSubjectPK() { 29 | } 30 | 31 | public TeacherHasSubjectPK(int teacherID, int subjectID) { 32 | this.teacherID = teacherID; 33 | this.subjectID = subjectID; 34 | } 35 | 36 | @Override 37 | public int hashCode() { 38 | int hash = 3; 39 | hash = 97 * hash + this.teacherID; 40 | hash = 97 * hash + this.subjectID; 41 | return hash; 42 | } 43 | 44 | @Override 45 | public boolean equals(Object obj) { 46 | if (this == obj) { 47 | return true; 48 | } 49 | if (obj == null) { 50 | return false; 51 | } 52 | if (getClass() != obj.getClass()) { 53 | return false; 54 | } 55 | final TeacherHasSubjectPK other = (TeacherHasSubjectPK) obj; 56 | if (this.teacherID != other.teacherID) { 57 | return false; 58 | } 59 | if (this.subjectID != other.subjectID) { 60 | return false; 61 | } 62 | return true; 63 | } 64 | 65 | /** 66 | * @return the teacherID 67 | */ 68 | public int getTeacherID() { 69 | return teacherID; 70 | } 71 | 72 | /** 73 | * @param teacherID the teacherID to set 74 | */ 75 | public void setTeacherID(int teacherID) { 76 | this.teacherID = teacherID; 77 | } 78 | 79 | /** 80 | * @return the subjectID 81 | */ 82 | public int getSubjectID() { 83 | return subjectID; 84 | } 85 | 86 | /** 87 | * @param subjectID the subjectID to set 88 | */ 89 | public void setSubjectID(int subjectID) { 90 | this.subjectID = subjectID; 91 | } 92 | 93 | } 94 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/exception/NonRollbackException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.exception; 7 | 8 | /** 9 | * 10 | * @author john.kyalo 11 | */ 12 | public class NonRollbackException extends Exception { 13 | 14 | public NonRollbackException() { 15 | } 16 | 17 | public NonRollbackException(String message) { 18 | super(message); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/exception/RollBackException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.exception; 7 | 8 | /** 9 | * 10 | * @author john.kyalo 11 | */ 12 | // 13 | public class RollBackException extends Exception { 14 | 15 | public RollBackException() { 16 | } 17 | 18 | public RollBackException(String message) { 19 | super(message); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/models/AdmissionFileModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.models; 7 | 8 | import com.fasterxml.jackson.annotation.JsonProperty; 9 | import lombok.AllArgsConstructor; 10 | import lombok.Data; 11 | import lombok.NoArgsConstructor; 12 | import lombok.ToString; 13 | 14 | /** 15 | * 16 | * @author hp 17 | */ 18 | @Data 19 | @AllArgsConstructor 20 | @NoArgsConstructor 21 | @ToString 22 | public class AdmissionFileModel { 23 | 24 | 25 | @JsonProperty("Guardian1Name") 26 | private String guardian1Name; 27 | @JsonProperty("Guardian1ContactInfo") 28 | private String guardian1ContactInfo; 29 | @JsonProperty("PreviousSchool") 30 | private String previousSchool; 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/models/DormitoryModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.models; 7 | 8 | import com.fasterxml.jackson.annotation.JsonProperty; 9 | import lombok.AllArgsConstructor; 10 | import lombok.Data; 11 | import lombok.NoArgsConstructor; 12 | import lombok.ToString; 13 | 14 | /** 15 | * 16 | * @author hp 17 | */ 18 | @Data 19 | @AllArgsConstructor 20 | @NoArgsConstructor 21 | @ToString 22 | public class DormitoryModel { 23 | 24 | @JsonProperty("DormitoryName") 25 | private String dormitoryName; 26 | @JsonProperty("NoOfWings") 27 | private Integer noOfWings; 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/models/StudentHasSubjectModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.models; 7 | 8 | import com.fasterxml.jackson.annotation.JsonProperty; 9 | import lombok.AllArgsConstructor; 10 | import lombok.Data; 11 | import lombok.NoArgsConstructor; 12 | import lombok.ToString; 13 | 14 | /** 15 | * 16 | * @author hp 17 | */ 18 | @Data 19 | @AllArgsConstructor 20 | @NoArgsConstructor 21 | @ToString 22 | public class StudentHasSubjectModel { 23 | 24 | @JsonProperty("StudentNumber") 25 | private Integer studentAdmissionNumber; 26 | @JsonProperty("SubjectID") 27 | private Integer subjectID; 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/models/StudentModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.models; 7 | 8 | import com.fasterxml.jackson.annotation.JsonFormat; 9 | import com.fasterxml.jackson.annotation.JsonProperty; 10 | import java.util.Date; 11 | import lombok.AllArgsConstructor; 12 | import lombok.Data; 13 | import lombok.NoArgsConstructor; 14 | import lombok.ToString; 15 | 16 | /** 17 | * 18 | * @author hp 19 | */ 20 | @Data 21 | @AllArgsConstructor 22 | @NoArgsConstructor 23 | @ToString 24 | public class StudentModel { 25 | 26 | @JsonProperty("Surname") 27 | private String surname; 28 | @JsonProperty("Firstname") 29 | private String firstname; 30 | @JsonProperty("Lastname") 31 | private String lasttname; 32 | @JsonProperty("Dob") 33 | @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd") 34 | private Date dateOfBirth; 35 | @JsonProperty("FileNumber") 36 | private Integer admissionFileNo; 37 | @JsonProperty("DormitoryNumber") 38 | private Integer dormitoryNo; 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/models/SubjectModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.models; 7 | 8 | import com.fasterxml.jackson.annotation.JsonProperty; 9 | import lombok.AllArgsConstructor; 10 | import lombok.Data; 11 | import lombok.NoArgsConstructor; 12 | import lombok.ToString; 13 | 14 | /** 15 | * 16 | * @author hp 17 | */ 18 | @Data 19 | @AllArgsConstructor 20 | @NoArgsConstructor 21 | @ToString 22 | public class SubjectModel { 23 | 24 | @JsonProperty("SubjectName") 25 | private String subjectName; 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/models/TeacherHasSubjectModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.models; 7 | 8 | import com.fasterxml.jackson.annotation.JsonProperty; 9 | import lombok.AllArgsConstructor; 10 | import lombok.Data; 11 | import lombok.NoArgsConstructor; 12 | import lombok.ToString; 13 | 14 | /** 15 | * 16 | * @author hp 17 | */ 18 | @Data 19 | @AllArgsConstructor 20 | @NoArgsConstructor 21 | @ToString 22 | public class TeacherHasSubjectModel { 23 | 24 | @JsonProperty("TeacherID") 25 | private Integer teacherID; 26 | @JsonProperty("SubjectID") 27 | private Integer subjectID; 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/models/TeacherModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.models; 7 | 8 | import com.fasterxml.jackson.annotation.JsonProperty; 9 | import lombok.AllArgsConstructor; 10 | import lombok.Data; 11 | import lombok.NoArgsConstructor; 12 | import lombok.ToString; 13 | 14 | /** 15 | * 16 | * @author hp 17 | */ 18 | @Data 19 | @AllArgsConstructor 20 | @NoArgsConstructor 21 | @ToString 22 | public class TeacherModel { 23 | 24 | @JsonProperty("TeacherName") 25 | private String teacherName; 26 | @JsonProperty("TeacherNumber") 27 | private String teacherNumber; 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/repositories/AdmissionfileRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.repositories; 7 | 8 | import com.school.relationships.entities.Admissionfile; 9 | import org.springframework.data.repository.CrudRepository; 10 | import org.springframework.stereotype.Repository; 11 | 12 | /** 13 | * 14 | * @author hp 15 | */ 16 | @Repository 17 | public interface AdmissionfileRepository extends CrudRepository { 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/repositories/DormitoryRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.repositories; 7 | 8 | import com.school.relationships.entities.Dormitory; 9 | import org.springframework.data.repository.CrudRepository; 10 | import org.springframework.stereotype.Repository; 11 | 12 | /** 13 | * 14 | * @author hp 15 | */ 16 | @Repository 17 | public interface DormitoryRepository extends CrudRepository{ 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/repositories/StudentHasSubjectRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.repositories; 7 | 8 | import com.school.relationships.entities.StudentHasSubject; 9 | import com.school.relationships.entities.StudentHasSubjectPK; 10 | import org.springframework.data.repository.CrudRepository; 11 | import org.springframework.stereotype.Repository; 12 | 13 | /** 14 | * 15 | * @author hp 16 | */ 17 | @Repository 18 | public interface StudentHasSubjectRepository extends CrudRepository { 19 | 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/repositories/StudentRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.repositories; 7 | 8 | import com.school.relationships.entities.Student; 9 | import java.util.Optional; 10 | import org.springframework.data.jpa.repository.Query; 11 | import org.springframework.data.repository.CrudRepository; 12 | import org.springframework.data.repository.query.Param; 13 | import org.springframework.stereotype.Repository; 14 | 15 | /** 16 | * 17 | * @author hp 18 | */ 19 | @Repository 20 | public interface StudentRepository extends CrudRepository { 21 | 22 | @Query("SELECT s FROM Student s JOIN FETCH s.subjectList WHERE s.idstudent = :idstudent") 23 | public Optional findStudentAndSubjectsTaken(@Param("idstudent") Integer idstudent); 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/repositories/SubjectRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.repositories; 7 | 8 | import com.school.relationships.entities.Subject; 9 | import org.springframework.data.repository.CrudRepository; 10 | import org.springframework.stereotype.Repository; 11 | 12 | /** 13 | * 14 | * @author hp 15 | */ 16 | @Repository 17 | public interface SubjectRepository extends CrudRepository{ 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/repositories/TeacherHasSubjectRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.repositories; 7 | 8 | import com.school.relationships.entities.TeacherHasSubject; 9 | import com.school.relationships.entities.TeacherHasSubjectPK; 10 | import org.springframework.data.repository.CrudRepository; 11 | import org.springframework.stereotype.Repository; 12 | 13 | /** 14 | * 15 | * @author hp 16 | */ 17 | @Repository 18 | public interface TeacherHasSubjectRepository extends CrudRepository { 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/repositories/TeacherRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.repositories; 7 | 8 | import com.school.relationships.entities.Teacher; 9 | import java.util.Optional; 10 | import org.springframework.data.jpa.repository.Query; 11 | import org.springframework.data.repository.CrudRepository; 12 | import org.springframework.data.repository.query.Param; 13 | import org.springframework.stereotype.Repository; 14 | 15 | /** 16 | * 17 | * @author hp 18 | */ 19 | @Repository 20 | public interface TeacherRepository extends CrudRepository { 21 | 22 | @Query("SELECT t FROM Teacher t JOIN FETCH t.subjectList WHERE t.idteacher = :idteacher") 23 | public Optional findTeacherAndSubjectsTaught(@Param("idteacher") Integer idteacher); 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/services/AdmissionFileService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.services; 7 | 8 | import com.school.relationships.entities.Admissionfile; 9 | import com.school.relationships.models.AdmissionFileModel; 10 | import com.school.relationships.repositories.AdmissionfileRepository; 11 | import java.util.Optional; 12 | import lombok.extern.slf4j.Slf4j; 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | import org.springframework.stereotype.Service; 15 | 16 | /** 17 | * 18 | * @author hp 19 | */ 20 | @Service 21 | @Slf4j 22 | public class AdmissionFileService { 23 | 24 | @Autowired 25 | private AdmissionfileRepository admissionfileRepository; 26 | 27 | public Integer createAdmissionFile(AdmissionFileModel model) { 28 | try { 29 | Admissionfile file = new Admissionfile(); 30 | file.setGuardian1Name(model.getGuardian1Name()); 31 | file.setGuadian1ContactInfo(model.getGuardian1ContactInfo()); 32 | file.setPreviousSchool(model.getPreviousSchool()); 33 | return admissionfileRepository.save(file).getIdadmissionfile(); 34 | } catch (Exception e) { 35 | log.error("Error creating admission file {}", e); 36 | throw e; 37 | } 38 | } 39 | 40 | public Optional findAdmissionFileByFileNumber(Integer idadmissionfile) { 41 | return admissionfileRepository.findById(idadmissionfile); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/services/DormitoryService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.services; 7 | 8 | import com.school.relationships.entities.Dormitory; 9 | import com.school.relationships.models.DormitoryModel; 10 | import com.school.relationships.repositories.DormitoryRepository; 11 | import java.util.Optional; 12 | import lombok.extern.slf4j.Slf4j; 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | import org.springframework.stereotype.Service; 15 | 16 | /** 17 | * 18 | * @author hp 19 | */ 20 | @Service 21 | @Slf4j 22 | public class DormitoryService { 23 | 24 | @Autowired 25 | private DormitoryRepository dormitoryRepository; 26 | 27 | public Integer createDormitory(DormitoryModel model) { 28 | try { 29 | Dormitory dormitory = new Dormitory(); 30 | dormitory.setDormitoryname(model.getDormitoryName()); 31 | dormitory.setNoOfWings(model.getNoOfWings()); 32 | return dormitoryRepository.save(dormitory).getIddormitory(); 33 | } catch (Exception e) { 34 | log.info("Error creating dormitory > {}", e); 35 | throw e; 36 | } 37 | } 38 | 39 | public Optional findDormitoryById(Integer iddormitory) { 40 | return dormitoryRepository.findById(iddormitory); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/services/StudentService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.services; 7 | 8 | import com.school.relationships.entities.Admissionfile; 9 | import com.school.relationships.entities.Student; 10 | import com.school.relationships.models.StudentModel; 11 | import com.school.relationships.repositories.StudentRepository; 12 | import java.util.Optional; 13 | import lombok.extern.slf4j.Slf4j; 14 | import org.springframework.beans.factory.annotation.Autowired; 15 | import org.springframework.stereotype.Service; 16 | 17 | /** 18 | * 19 | * @author hp 20 | */ 21 | @Service 22 | @Slf4j 23 | public class StudentService { 24 | 25 | @Autowired 26 | private StudentRepository studentRepository; 27 | @Autowired 28 | private DormitoryService dormitoryService; 29 | @Autowired 30 | private AdmissionFileService admissionFileService; 31 | 32 | public void createStudent(StudentModel model) { 33 | try { 34 | Student student = new Student(); 35 | Admissionfile file = admissionFileService.findAdmissionFileByFileNumber(model.getAdmissionFileNo()).get(); 36 | student.setFileNo(file); 37 | student.setDob(model.getDateOfBirth()); 38 | student.setFirstName(model.getFirstname()); 39 | student.setSurname(model.getSurname()); 40 | student.setLastName(model.getLasttname()); 41 | student.setDormitoryIddormitory(dormitoryService.findDormitoryById(model.getDormitoryNo()).get()); 42 | studentRepository.save(student); 43 | } catch (Exception e) { 44 | log.error("Error creating student > {}", e); 45 | throw e; 46 | } 47 | } 48 | 49 | public Optional findStudentWithTheirSubjects(Integer idstudent) { 50 | return studentRepository.findStudentAndSubjectsTaken(idstudent); 51 | } 52 | 53 | public Optional findStudentById(Integer idstudent) { 54 | return studentRepository.findById(idstudent); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/services/StudentSubjectService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.services; 7 | 8 | import com.school.relationships.entities.StudentHasSubject; 9 | import com.school.relationships.entities.StudentHasSubjectPK; 10 | import com.school.relationships.models.StudentHasSubjectModel; 11 | import com.school.relationships.repositories.StudentHasSubjectRepository; 12 | import lombok.extern.slf4j.Slf4j; 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | import org.springframework.stereotype.Service; 15 | 16 | /** 17 | * 18 | * @author hp 19 | */ 20 | @Service 21 | @Slf4j 22 | public class StudentSubjectService { 23 | 24 | @Autowired 25 | private StudentHasSubjectRepository studentHasSubjectRepository; 26 | 27 | public StudentHasSubjectPK addStudentSubject(StudentHasSubjectModel model) { 28 | try { 29 | StudentHasSubjectPK pK = new StudentHasSubjectPK(); 30 | pK.setStudentID(model.getStudentAdmissionNumber()); 31 | pK.setSubjectID(model.getSubjectID()); 32 | StudentHasSubject studentHasSubject = new StudentHasSubject(); 33 | studentHasSubject.setStudentHasSubjectPK(pK); 34 | return studentHasSubjectRepository.save(studentHasSubject).getStudentHasSubjectPK(); 35 | } catch (Exception e) { 36 | log.info("Error adding student-subject record > {}", e); 37 | throw e; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/services/SubjectService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.services; 7 | 8 | import com.school.relationships.entities.Subject; 9 | import com.school.relationships.models.SubjectModel; 10 | import com.school.relationships.repositories.SubjectRepository; 11 | import lombok.extern.slf4j.Slf4j; 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.stereotype.Service; 14 | 15 | /** 16 | * 17 | * @author hp 18 | */ 19 | @Service 20 | @Slf4j 21 | public class SubjectService { 22 | 23 | @Autowired 24 | private SubjectRepository subjectRepository; 25 | 26 | public Integer createSubject(SubjectModel model) { 27 | try { 28 | Subject subject = new Subject(); 29 | subject.setSubjectname(model.getSubjectName()); 30 | return subjectRepository.save(subject).getIdsubject(); 31 | } catch (Exception e) { 32 | log.error("Error creating subject > ", e); 33 | throw e; 34 | } 35 | } 36 | 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/services/TeacherService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.services; 7 | 8 | import com.school.relationships.entities.Teacher; 9 | import com.school.relationships.models.TeacherModel; 10 | import com.school.relationships.repositories.TeacherRepository; 11 | import com.school.relationships.util.FormattingUtils; 12 | import java.util.Optional; 13 | import lombok.extern.slf4j.Slf4j; 14 | import org.springframework.beans.factory.annotation.Autowired; 15 | import org.springframework.stereotype.Service; 16 | 17 | /** 18 | * 19 | * @author hp 20 | */ 21 | @Service 22 | @Slf4j 23 | public class TeacherService { 24 | 25 | @Autowired 26 | private TeacherRepository teacherRepository; 27 | @Autowired 28 | private FormattingUtils formattingUtils; 29 | 30 | public Teacher createTeacher(TeacherModel model) { 31 | try { 32 | Teacher teacher = new Teacher(); 33 | teacher.setFullName(model.getTeacherName()); 34 | teacher.setTeacherNo(formattingUtils.formatTeacherNumber(model.getTeacherNumber())); 35 | log.info("Created teacher name={},teacher no={}", teacher.getFullName(), teacher.getTeacherNo()); 36 | return teacherRepository.save(teacher); 37 | } catch (Exception e) { 38 | log.error("Error creating teacher > ", e); 39 | throw e; 40 | } 41 | } 42 | 43 | public Optional findTeacherById(Integer idteacher) { 44 | return teacherRepository.findById(idteacher); 45 | } 46 | 47 | public Optional findTeacherAndSubjectsTaught(Integer idteacher) { 48 | return teacherRepository.findTeacherAndSubjectsTaught(idteacher); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/services/TeacherSubjectService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.services; 7 | 8 | import com.school.relationships.entities.TeacherHasSubject; 9 | import com.school.relationships.entities.TeacherHasSubjectPK; 10 | import com.school.relationships.models.TeacherHasSubjectModel; 11 | import com.school.relationships.repositories.TeacherHasSubjectRepository; 12 | import lombok.extern.slf4j.Slf4j; 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | import org.springframework.stereotype.Service; 15 | 16 | /** 17 | * 18 | * @author hp 19 | */ 20 | @Service 21 | @Slf4j 22 | public class TeacherSubjectService { 23 | 24 | @Autowired 25 | TeacherHasSubjectRepository teacherHasSubjectRepository; 26 | 27 | public TeacherHasSubjectPK addTeacherSubject(TeacherHasSubjectModel model) { 28 | try { 29 | TeacherHasSubjectPK pK = new TeacherHasSubjectPK(); 30 | pK.setSubjectID(model.getSubjectID()); 31 | pK.setTeacherID(model.getTeacherID()); 32 | TeacherHasSubject teacherHasSubject = new TeacherHasSubject(); 33 | teacherHasSubject.setTeacherHasSubjectPK(pK); 34 | return teacherHasSubjectRepository.save(teacherHasSubject).getTeacherHasSubjectPK(); 35 | } catch (Exception e) { 36 | log.info("Error adding teacher-subject record > {}", e); 37 | throw e; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/school/relationships/util/FormattingUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships.util; 7 | 8 | import org.springframework.stereotype.Component; 9 | 10 | /** 11 | * 12 | * @author hp 13 | */ 14 | @Component 15 | public class FormattingUtils { 16 | 17 | public String formatTeacherNumber(String trNo) { 18 | return new StringBuilder().append("TR-").append(trNo.toUpperCase()).toString(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/persistence.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | org.eclipse.persistence.jpa.PersistenceProvider 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:mysql://localhost:3306/school 2 | spring.datasource.username=root 3 | spring.datasource.password=root 4 | spring.jpa.hibernate.ddl-auto=none 5 | server.port=8084 6 | -------------------------------------------------------------------------------- /src/test/java/com/school/relationships/RelationshipsApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.school.relationships; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class RelationshipsApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/com/school/relationships/SubjectServiceTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships; 7 | 8 | import com.school.relationships.entities.Subject; 9 | import com.school.relationships.models.SubjectModel; 10 | import com.school.relationships.repositories.SubjectRepository; 11 | import com.school.relationships.services.SubjectService; 12 | import lombok.extern.slf4j.Slf4j; 13 | import org.junit.Test; 14 | import org.junit.runner.RunWith; 15 | import org.mockito.InjectMocks; 16 | import org.mockito.Mock; 17 | import org.mockito.Mockito; 18 | import org.mockito.junit.MockitoJUnitRunner; 19 | 20 | /** 21 | * 22 | * @author hp 23 | */ 24 | @RunWith(MockitoJUnitRunner.class) 25 | @Slf4j 26 | public class SubjectServiceTests { 27 | 28 | @Mock 29 | private SubjectRepository subjectRepository; 30 | @InjectMocks 31 | private SubjectService subjectService; 32 | 33 | @Test 34 | public void testCreateSubjectSuccessful() { 35 | log.info("Started testing method testCreateSubjectSuccessful"); 36 | Mockito.when(subjectRepository.save(Mockito.any(Subject.class))).thenReturn(Mockito.mock(Subject.class)); 37 | 38 | SubjectModel mockSubjectModel = new SubjectModel(); 39 | mockSubjectModel.setSubjectName("Math"); 40 | subjectService.createSubject(mockSubjectModel); 41 | log.info("Finished testing method testCreateSubjectSuccessful"); 42 | } 43 | 44 | @Test(expected = NullPointerException.class) 45 | public void testCreateSubjectThrowsException() { 46 | log.info("Started testing method testCreateSubjectThrowsException"); 47 | Mockito.when(subjectRepository.save(Mockito.any(Subject.class))).thenThrow(NullPointerException.class); 48 | SubjectModel mockSubjectModel = new SubjectModel(); 49 | mockSubjectModel.setSubjectName("Science"); 50 | subjectService.createSubject(mockSubjectModel); 51 | log.info("Finished testing method testCreateSubjectThrowsException"); 52 | } 53 | 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/test/java/com/school/relationships/TeacherServiceTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package com.school.relationships; 7 | 8 | import com.school.relationships.entities.Teacher; 9 | import com.school.relationships.models.TeacherModel; 10 | import com.school.relationships.repositories.TeacherRepository; 11 | import com.school.relationships.services.TeacherService; 12 | import com.school.relationships.util.FormattingUtils; 13 | import lombok.extern.slf4j.Slf4j; 14 | import org.junit.Test; 15 | import org.junit.runner.RunWith; 16 | import org.mockito.InjectMocks; 17 | import org.mockito.Mock; 18 | import org.mockito.Mockito; 19 | import org.mockito.Spy; 20 | import org.mockito.invocation.InvocationOnMock; 21 | import org.mockito.junit.MockitoJUnitRunner; 22 | import org.mockito.stubbing.Answer; 23 | 24 | /** 25 | * 26 | * @author hp 27 | */ 28 | @RunWith(MockitoJUnitRunner.class) 29 | @Slf4j 30 | public class TeacherServiceTests { 31 | 32 | @Mock 33 | private TeacherRepository teacherRepository; 34 | @Spy 35 | private FormattingUtils formattingUtils; 36 | @InjectMocks 37 | private TeacherService teacherService; 38 | 39 | @Test 40 | public void testCreateTeacherSuccessful() { 41 | log.info("Started testing method createTeacherSuccessful"); 42 | Mockito.when(teacherRepository.save(Mockito.any(Teacher.class))).thenReturn(Mockito.mock(Teacher.class)); 43 | TeacherModel model = new TeacherModel(); 44 | model.setTeacherName("Mr. John"); 45 | model.setTeacherNumber("123"); 46 | teacherService.createTeacher(model); 47 | log.info("Finished testing method createTeacherSuccessful"); 48 | } 49 | 50 | @Test 51 | public void testCreateTeacherSuccessfulWithFormattingUtilsModification() { 52 | 53 | log.info("Started testing method testCreateTeacherSuccessfulWithFormattingUtilsModification"); 54 | Mockito.when(teacherRepository.save(Mockito.any(Teacher.class))).thenReturn(Mockito.mock(Teacher.class)); 55 | Mockito.when(formattingUtils.formatTeacherNumber(Mockito.anyString())).thenAnswer((InvocationOnMock iom) -> { 56 | String trNo = iom.getArgument(0, String.class); 57 | return new StringBuilder().append("tr-").append(trNo.toLowerCase()).toString(); 58 | }); 59 | TeacherModel model = new TeacherModel(); 60 | model.setTeacherName("Mr. John"); 61 | model.setTeacherNumber("123"); 62 | teacherService.createTeacher(model); 63 | log.info("Finished testing method testCreateTeacherSuccessfulWithFormattingUtilsModification"); 64 | } 65 | } 66 | --------------------------------------------------------------------------------