commit 6bd392171493597891c552eba405732385f53c55 Author: rviewer-team <99674447+rviewer-team@users.noreply.github.com> Date: Thu Jun 16 10:17:10 2022 +0200 Initial commit diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..194ab54 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,31 @@ +--- +name: Bug report 🐛 +about: Create a report to help us improve +title: '' +labels: '' +assignees: '' + +--- + +**Describe the bug** +A clear and concise description of what the bug is. + +**To Reproduce** +Steps to reproduce the behavior: +1. Go to '...' +2. Click on '....' +3. Scroll down to '....' +4. See error + +**Expected behavior** +A clear and concise description of what you expected to happen. + +**Screenshots** +If applicable, add screenshots to help explain your problem. + +**Desktop (please complete the following information):** + - OS: [e.g. Ubuntu 21.04] + - Docker version: [e.g. Docker version 20.10.12] + +**Additional context** +Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 0000000..6c1ee26 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1,5 @@ +blank_issues_enabled: true +contact_links: + - name: Ask a question ❓ + url: https://github.com/Rviewer-Challenges/skeleton-java-spring-rest/discussions/new + about: Ask a question or request support for using this skeleton \ No newline at end of file diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 0000000..be1cb51 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,20 @@ +--- +name: Feature request ✨ +about: Suggest an idea for this project +title: '' +labels: '' +assignees: '' + +--- + +**Is your feature request related to a problem? Please describe.** +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +**Describe the solution you'd like** +A clear and concise description of what you want to happen. + +**Describe alternatives you've considered** +A clear and concise description of any alternative solutions or features you've considered. + +**Additional context** +Add any other context or screenshots about the feature request here. diff --git a/.github/rviewer_logo--dark.png b/.github/rviewer_logo--dark.png new file mode 100644 index 0000000..94f24c0 Binary files /dev/null and b/.github/rviewer_logo--dark.png differ diff --git a/.github/workflows/sonarqube-scanner.yaml b/.github/workflows/sonarqube-scanner.yaml new file mode 100644 index 0000000..44f32e8 --- /dev/null +++ b/.github/workflows/sonarqube-scanner.yaml @@ -0,0 +1,70 @@ +on: + workflow_dispatch: + push: + branches: + - main + - devel + pull_request: + types: + - opened + - reopened + - synchronize + branches: + - main + - devel + +jobs: + secrets-gate: + runs-on: ubuntu-latest + outputs: + ok: ${{ steps.check-secrets.outputs.ok }} + steps: + - name: check for secrets needed to run SonarQube + id: check-secrets + run: | + if [ ! -z "${{ secrets.SONAR_TOKEN }}" ] && [ ! -z "${{ secrets.SONAR_HOST_URL }}" ]; then + echo "::set-output name=ok::true" + fi + + sonarqube: + needs: + - secrets-gate + if: ${{ needs.secrets-gate.outputs.ok == 'true' }} + runs-on: ubuntu-latest + services: + postgres-skeleton-db: + image: postgres + env: + POSTGRES_DB: postgres_rv_database + POSTGRES_USER: rv_user + POSTGRES_PASSWORD: rv_password + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + ports: + - 5432:5432 + steps: + - uses: actions/checkout@v2 + + - uses: actions/setup-java@v3 + with: + distribution: temurin + java-version: 17 + + - uses: gradle/gradle-build-action@v2 + with: + gradle-version: 7.4.2 + + - name: Gradle execute tests + run: gradle clean build + + - name: SonarQube Scan + uses: sonarsource/sonarqube-scan-action@master + with: + args: > + -Dsonar.projectKey=${{ github.event.repository.name }} + env: + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f35ca9e --- /dev/null +++ b/.gitignore @@ -0,0 +1,39 @@ +.gradle +build/ +!gradle/wrapper/gradle-wrapper.jar +!**/src/main/**/build/ +!**/src/test/**/build/ + +### IntelliJ IDEA ### +.idea/ +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/INSTRUCTIONS.md b/INSTRUCTIONS.md new file mode 100644 index 0000000..ee7fce6 --- /dev/null +++ b/INSTRUCTIONS.md @@ -0,0 +1,77 @@ + + +## Rviewer skeleton: Java, Spring & PostgreSQL + +[![Twitter](https://img.shields.io/badge/rviewer__-%231DA1F2.svg?style=for-the-badge&logo=Twitter&logoColor=white)](https://twitter.com/Rviewer_/) + +[![Rviewer Discord](https://badgen.net/discord/members/VVN4ur8FaQ)](https://discord.gg/VVN4ur8FaQ) + +
+ +This repository is a Java skeleton with Spring & PostgreSQL designed for quickly getting started developing an API. +Check the [Getting Started](#getting-started) for full details. + +## Technologies + +* [Java 18](https://openjdk.java.net/projects/jdk/18/) +* [Gradle 7](https://docs.gradle.org/7.0/release-notes.html) +* [Spring boot](https://spring.io/projects/spring-boot) +* [Lombok](https://projectlombok.org/) +* [Junit](https://junit.org/junit5/) +* [JaCoCo](https://docs.gradle.org/current/userguide/jacoco_plugin.html) +* [Docker](https://www.docker.com/) +* [Make](https://www.gnu.org/software/make/manual/make.html) + +## Getting Started + +Within the [Makefile](Makefile) you can handle the entire flow to get everything up & running: + +1. Install `make` on your computer, if you do not already have it. +2. Start the application: `make up` +3. Run the application tests: `make test` + +As you could see on the [Makefile](Makefile) script and the [Docker-Compose File](docker-compose.yml), the whole API +is containerized with Docker and the API is using the internal DNS to connect with the PostgreSQL instance. + +Go to `http://127.0.0.1:8080/ping` to see that everything is up & running! + +## Overview + +This skeleton is based on +a [Clean Architecture](https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html) approach, so you +could find the first basic layers: + +> You could find here two amazing articles ([here](https://www.educative.io/blog/clean-architecture-tutorial) +> and [here](https://www.freecodecamp.org/news/modern-clean-architecture/)) explaining the Clean Architecture with Java! +> (credits to [@bertilMuth](https://twitter.com/BertilMuth) and [@ryanthelin](https://dev.to/ryanthelin)). + +### Infrastructure + +Here you will find the different files to interact with the outside. In this folder you there are two different folders: + +* `controllers`: Here you will have the classes that handle the REST endpoints and the Request/Response +* `persistence`: Here it is the persistence layer, which interact with the PostgreSQL database, decoupling the rest of + the application + +You can use this as a starting point to continue with this architecture, or adapt it to your preferences. + +### Domain + +Any of your domain Entities, or Services, that models your business logic. These classes should be completely isolated +of any external dependency or framework, but interact with them. This layer should follow the Dependency Inversion +principle. + +## Support + +If you are having problems or need anything else, please let us know by +[raising a new issue](https://github.com/Rviewer-Challenges/skeleton-java-spring-rest/issues/new/choose). + +## License + +This project is licensed with the [MIT license](LICENSE). + +--- + +

+ Made with ❤️ by Rviewer +

diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..02697ef --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Rviewer + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..491d62e --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +.PHONY: down up test + +down: + docker-compose down + +up: + docker-compose run -d -p "8080:8080" java-skeleton-api gradle clean build bootRun -x test + +test: + docker-compose run --rm --no-deps -p "8080:8080" java-skeleton-api gradle test + +coverage: test \ No newline at end of file diff --git a/api.spec.yaml b/api.spec.yaml new file mode 100644 index 0000000..7cb2ece --- /dev/null +++ b/api.spec.yaml @@ -0,0 +1,175 @@ +openapi: 3.0.0 +info: + title: Shopping Cart API + description: | + Shopping Cart API is a REST API responsible for manage the shopping car for an e-commerce with a Blockchain storage + system, where the users could create, update and delete their cart. + version: 1.0.0 +paths: + /carts/{id}: + get: + summary: | + This endpoint returns a cart for the given id. + parameters: + - name: id + description: The id of the cart. + required: true + in: path + schema: + type: string + format: uuid + responses: + '200': + description: created + content: + application/json: + schema: + $ref: '#/components/schemas/CreateCartRes' + '404': + description: Cart not found + content: + application/json: + schema: + type: object + properties: + error: + type: string + example: Cart not found for the given ID + post: + summary: | + This endpoint creates a cart for the given id. + parameters: + - name: id + description: The id of the cart. + required: true + in: path + schema: + type: string + format: uuid + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/CreateCartReq' + responses: + '201': + description: created + '409': + description: Already exists + content: + application/json: + schema: + type: object + properties: + error: + type: string + example: Invalid identifier. + '400': + description: Invalid body provided + content: + application/json: + schema: + type: object + properties: + error: + type: string + example: Invalid body provided, check the payload. + patch: + summary: | + This endpoint updates totally or partially a cart for the given id. + parameters: + - name: id + description: The id of the cart. + required: true + in: path + schema: + type: string + format: uuid + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/UpdateCartReq' + responses: + '200': + description: created + '404': + description: Not found + content: + application/json: + schema: + type: object + properties: + error: + type: string + example: Cart not found for the given id + '400': + description: Invalid body provided + content: + application/json: + schema: + type: object + properties: + error: + type: string + example: Invalid body provided, check the payload. + delete: + summary: | + This endpoint deletes a cart for the given id. + parameters: + - name: id + description: The id of the cart. + required: true + in: path + schema: + type: string + format: uuid + responses: + '200': + description: deleted + '404': + description: Cart not found + content: + application/json: + schema: + type: object + properties: + error: + type: string + example: Cart not found for the given ID +components: + schemas: + CartItem: + type: object + properties: + id: + type: string + format: uuid + name: + type: string + quantity: + type: number + price: + type: number + CartItems: + type: array + items: + $ref: '#/components/schemas/CartItem' + CreateCartReq: + type: object + properties: + items: + $ref: '#/components/schemas/CartItems' + UpdateCartReq: + properties: + items: + $ref: '#/components/schemas/CartItems' + CreateCartRes: + type: object + properties: + id: + type: string + format: uuid + items: + $ref: '#/components/schemas/CartItems' + diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..38c066e --- /dev/null +++ b/build.gradle @@ -0,0 +1,95 @@ +plugins { + id 'org.springframework.boot' version '2.7.0' + id 'io.spring.dependency-management' version '1.0.11.RELEASE' + id 'java' + id 'jacoco' +} + +group = 'com.rviewer.skeletons' +version = '1.0.0' +sourceCompatibility = '17' + +configurations { + compileOnly { + extendsFrom annotationProcessor + } +} + +repositories { + mavenCentral() + maven { url 'https://repo.spring.io/milestone' } +} + +dependencies { + implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'org.springframework.boot:spring-boot-starter-jdbc' + compileOnly 'org.projectlombok:lombok' + developmentOnly 'org.springframework.boot:spring-boot-devtools' + testImplementation 'com.h2database:h2:2.1.212' + runtimeOnly 'org.postgresql:postgresql' + annotationProcessor 'org.projectlombok:lombok' + testImplementation 'org.springframework.boot:spring-boot-starter-test' +} + +tasks.named('test') { + useJUnitPlatform() +} + +test { + testLogging { + events "PASSED", "SKIPPED", "FAILED" + } + ignoreFailures = true + finalizedBy jacocoTestReport +} + +jacoco { + toolVersion = "0.8.8" + reportsDirectory = file("$buildDir/reports/jacoco") +} + +jacocoTestReport { + dependsOn test + reports { + xml.required = true + html.required = true + csv.required = false + } +} + +/* +************************************************************************************ +************************ Task to obtain a clear test report ************************ +************************************************************************************ +*/ +import org.gradle.api.tasks.testing.logging.TestExceptionFormat +import org.gradle.api.tasks.testing.logging.TestLogEvent +tasks.withType(Test) { + testLogging { + events TestLogEvent.FAILED, + TestLogEvent.PASSED + + exceptionFormat TestExceptionFormat.FULL + showExceptions true + showCauses true + showStackTraces true + + debug { + events TestLogEvent.FAILED, + TestLogEvent.PASSED + + exceptionFormat TestExceptionFormat.FULL + } + info.events = debug.events + info.exceptionFormat = debug.exceptionFormat + + afterSuite { desc, result -> + if (!desc.parent) { + def output = "Results: ${result.resultType} (${result.testCount} tests, ${result.successfulTestCount} passed, ${result.failedTestCount} failed, ${result.skippedTestCount} skipped)" + def startItem = '| ', endItem = ' |' + def repeatLength = startItem.length() + output.length() + endItem.length() + println('\n' + ('-' * repeatLength) + '\n' + startItem + output + endItem + '\n' + ('-' * repeatLength)) + } + } + } +} \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..f7be341 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,35 @@ +version: '3.9' + +services: + java-skeleton-api: + container_name: java-skeleton-api + image: gradle:latest # this is due to some Mac M1 issues with Docker :_ + depends_on: + - postgres-skeleton-db + volumes: + - "${PWD}:/home/gradle/project" + - "rv-gradle-cache:/home/gradle/.gradle" + working_dir: "/home/gradle/project" + ports: + - "8080:8080" + networks: + internal-net: + + postgres-skeleton-db: + container_name: postgres-skeleton-db + image: postgres:13.4-alpine + restart: on-failure + volumes: + - rv-volume:/var/lib/postgresql/data + env_file: + - postgres.dev.env + networks: + internal-net: + +volumes: + rv-volume: + rv-gradle-cache: + +networks: + internal-net: + name: rv-skeleton-net diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..41d9927 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..00e33ed --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.1-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..1b6c787 --- /dev/null +++ b/gradlew @@ -0,0 +1,234 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +APP_NAME="Gradle" +APP_BASE_NAME=${0##*/} + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..ac1b06f --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,89 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/postgres.dev.env b/postgres.dev.env new file mode 100644 index 0000000..3029892 --- /dev/null +++ b/postgres.dev.env @@ -0,0 +1,4 @@ +POSTGRES_NAME=postgres-skeleton-db +POSTGRES_DB=postgres_rv_database +POSTGRES_USER=rv_user +POSTGRES_PASSWORD=rv_password \ No newline at end of file diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..8f8814e --- /dev/null +++ b/readme.md @@ -0,0 +1,115 @@ +# Cartfidential + +Within the current landscape of digital commerce, there is no such progress and there are still the same problems of +scalability and performance with high traffic than some years ago. For this reason, we have decided to implement a new +type of ecommerce based on the [Blockchain](https://builtin.com/blockchain) technology, creating a decentralized network +to execute and store, in a decentralized way, the different processes of an ecommerce. + +That's why you will be in charge of creating a very important part of it, the shopping cart. + +## How it works? + +The aim of this API is to manage a shopping cart of our ecommerce website. So, through it, the Frontend Team will be +able to request and create, update or delete any item in the current cart. + +Every action of the API will generate a new _Block_ on the _Blockchain_ in order to create a history and persist the +information, so we can retrieve it later, but also keeps a history of which items have the user added to the cart. + +You could find the API description on the OpenAPI description file. + +### Workflow + +The workflow of this API is as follows: + +* Create + +1. Create request is received +2. Check if the `id` for a Cart is not already used + 1. If it has been used we return error + 2. If it has not been used, we create the cart with the given item and generates a new Block to add to the + Blockchain +3. Add the new _Block_ to the _Blockchain_ with the current information of this Cart + +* Update + +1. Update request is received +2. Check if the `id` for a Cart exists + 1. If not exists return error + 2. If exist retrieve it +3. Update the items in the cart and generate a new Block with the information +4. Add the new _Block_ to the _Blockchain_ + +* Delete + +1. Delete request is received +2. Check if the `id` for a Cart exists + 1. If not exists return error + 2. If exist retrieve it +3. Remove cart information and create a new _Block_ +4. Add the new _Block_ to the _Blockchain_ + +## Technical Considerations + +Keep in mind the following: + +1. Related with the Blockchain: + 1. The blockchain have a first block called 'genesis block', these block doesn't contain a reference of the previous + block, usually is hardcoded on the software + 2. Every Block has multiple properties: + 1. timestamp: the timestamp for the moment when the block was created + 2. lastHash: hash of the previous block on the _blockchain_ + 3. data: information we want to store in the block (in our case the votes) + 4. nonce: a unique number + 5. hash: a SHA256 string for the block, calculated concatenating the timestamp, lastHash, data and nonce. + 3. The implementation of the Blockchain must follow these contract: + ``` + Blockchain + /** Adds new block to blockchain */ + addBlock(block: Block): Block + /** The new blockchain that is a candidate for replacing the current blockchain */ + replace(blockchain: Blockchain): boolean + /** + * Validates the chain by checking if: + * - Genesis Block hash match given blockchain genesis block hash + * - every element's last hash value matches previous block's hash + * - data hasn't been tampered (which will produce a different hash value) + */ + isValid(blockchain: Blockchain): boolean + + Block + /** Generate the first block for the chain */ + static getGenesisBlock(): Block + /** Generate the hash for the given block */ + static generateHashFromBlock(block: Block): string + ``` + +## Technical requirements + +* Create a **clean**, **maintainable** and **well-designed** code. We expect to see a good and clear architecture that + allows to add or modify the solution without so much troubles. +* **Test** your code until you are comfortable with it. We don't expect a 100% of Code Coverage but some tests that + helps to have a more stable and confident base code. + +To understand how you take decisions during the implementation, **please write a COMMENTS.md** file explaining some of +the most important parts of the application. You would also be able to defend your code through +[Rviewer](https://rviewer.io), once you submit your solution. + +--- + +## How to submit your solution + +* Push your code to the `devel` branch - we encourage you to commit regularly to show your thinking process was. +* **Create a new Pull Request** to `main` branch & **merge it**. + +Once merged you **won't be able to change or add** anything to your solution, so double-check that everything is as +you expected! + +Remember that **there is no countdown**, so take your time and implement a solution that you are proud! + +--- + +

+ If you have any feedback or problem, let us know! 🤘 +

+ Made with ❤️ by Rviewer +

diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..08047b7 --- /dev/null +++ b/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'rviewer-skeleton-java-spring' diff --git a/sonar-project.properties b/sonar-project.properties new file mode 100644 index 0000000..b5b52eb --- /dev/null +++ b/sonar-project.properties @@ -0,0 +1,6 @@ +sonar.sources=src/main/java +sonar.java.binaries=build/classes/java/ +sonar.java.libraries=build/libs/rviewer-skeleton-java-spring-1.0.0.jar +sonar.language=java +sonar.sourceEncoding=UTF-8 +sonar.coverage.jacoco.xmlReportPaths=build/reports/jacoco/test/jacocoTestReport.xml diff --git a/src/main/java/com/rviewer/skeletons/RviewerSkeletonApplication.java b/src/main/java/com/rviewer/skeletons/RviewerSkeletonApplication.java new file mode 100644 index 0000000..4d3cf85 --- /dev/null +++ b/src/main/java/com/rviewer/skeletons/RviewerSkeletonApplication.java @@ -0,0 +1,13 @@ +package com.rviewer.skeletons; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class RviewerSkeletonApplication { + + public static void main(String[] args) { + SpringApplication.run(RviewerSkeletonApplication.class, args); + } + +} diff --git a/src/main/java/com/rviewer/skeletons/domain/responses/PongResponse.java b/src/main/java/com/rviewer/skeletons/domain/responses/PongResponse.java new file mode 100644 index 0000000..651efce --- /dev/null +++ b/src/main/java/com/rviewer/skeletons/domain/responses/PongResponse.java @@ -0,0 +1,12 @@ +package com.rviewer.skeletons.domain.responses; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@AllArgsConstructor +@Getter +public class PongResponse { + + private String message; + private int number; +} diff --git a/src/main/java/com/rviewer/skeletons/domain/services/PongService.java b/src/main/java/com/rviewer/skeletons/domain/services/PongService.java new file mode 100644 index 0000000..1cc4269 --- /dev/null +++ b/src/main/java/com/rviewer/skeletons/domain/services/PongService.java @@ -0,0 +1,17 @@ +package com.rviewer.skeletons.domain.services; + +import com.rviewer.skeletons.domain.responses.PongResponse; +import com.rviewer.skeletons.domain.services.persistence.DatabaseConnector; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class PongService { + + @Autowired + DatabaseConnector postgresConnector; + + public PongResponse getPong() { + return new PongResponse("pong", postgresConnector.getConnectionStatus()); + } +} diff --git a/src/main/java/com/rviewer/skeletons/domain/services/persistence/DatabaseConnector.java b/src/main/java/com/rviewer/skeletons/domain/services/persistence/DatabaseConnector.java new file mode 100644 index 0000000..3065c03 --- /dev/null +++ b/src/main/java/com/rviewer/skeletons/domain/services/persistence/DatabaseConnector.java @@ -0,0 +1,6 @@ +package com.rviewer.skeletons.domain.services.persistence; + +public interface DatabaseConnector { + + public int getConnectionStatus(); +} diff --git a/src/main/java/com/rviewer/skeletons/infrastructure/controllers/PingController.java b/src/main/java/com/rviewer/skeletons/infrastructure/controllers/PingController.java new file mode 100644 index 0000000..0c5356d --- /dev/null +++ b/src/main/java/com/rviewer/skeletons/infrastructure/controllers/PingController.java @@ -0,0 +1,21 @@ +package com.rviewer.skeletons.infrastructure.controllers; + +import com.rviewer.skeletons.domain.services.PongService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/") +public class PingController { + + @Autowired + private PongService pongService; + + @GetMapping("/ping") + public ResponseEntity getPing() { + return ResponseEntity.ok(pongService.getPong()); + } +} diff --git a/src/main/java/com/rviewer/skeletons/infrastructure/persistence/PostgresConnector.java b/src/main/java/com/rviewer/skeletons/infrastructure/persistence/PostgresConnector.java new file mode 100644 index 0000000..a160b12 --- /dev/null +++ b/src/main/java/com/rviewer/skeletons/infrastructure/persistence/PostgresConnector.java @@ -0,0 +1,18 @@ +package com.rviewer.skeletons.infrastructure.persistence; + +import com.rviewer.skeletons.domain.services.persistence.DatabaseConnector; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Component; + +@Component +public class PostgresConnector implements DatabaseConnector { + + @Autowired + JdbcTemplate jdbcTemplate; + + public int getConnectionStatus() { + return jdbcTemplate.queryForObject("SELECT 1+1", Integer.class); + } + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..14a0616 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,9 @@ +server.port=8080 +application.title=Java skeleton +application.version=1.0.0 + +spring.datasource.initialization-mode=always +spring.datasource.username = rv_user +spring.datasource.password = rv_password +spring.datasource.driverClassName = org.postgresql.Driver +spring.datasource.url = jdbc:postgresql://postgres-skeleton-db:5432/postgres_rv_database diff --git a/src/main/resources/banner.txt b/src/main/resources/banner.txt new file mode 100644 index 0000000..0c2529e --- /dev/null +++ b/src/main/resources/banner.txt @@ -0,0 +1,8 @@ +,------. ,--. +| .--. ' ,--. ,--. `--' ,---. ,--. ,--. ,---. ,--.--. +| '--'.' \ `' / ,--. | .-. : | |.'.| | | .-. : | .--' +| |\ \ \ / | | \ --. | .'. | \ --. | | +`--' '--' `--' `--' `----' '--' '--' `----' `--' + +${application.title} - v${application.version} +Powered by Rviewer \ No newline at end of file diff --git a/src/test/java/com/rviewer/skeletons/infrastructure/controllers/PingControllerTest.java b/src/test/java/com/rviewer/skeletons/infrastructure/controllers/PingControllerTest.java new file mode 100644 index 0000000..5eb8bdc --- /dev/null +++ b/src/test/java/com/rviewer/skeletons/infrastructure/controllers/PingControllerTest.java @@ -0,0 +1,34 @@ +package com.rviewer.skeletons.infrastructure.controllers; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.web.servlet.MockMvc; + +import static org.hamcrest.Matchers.containsString; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest +@AutoConfigureMockMvc +public class PingControllerTest { + + @Autowired + private MockMvc mockMvc; + + @Test + public void getPing_success() throws Exception { + mockMvc + .perform(get("/ping")) + .andExpect(status().isOk()); + } + + @Test + public void getPing_returnsPong() throws Exception { + mockMvc + .perform(get("/ping")) + .andExpect(content().string(containsString("pong"))); + } +} diff --git a/src/test/java/com/rviewer/skeletons/infrastructure/persistence/PostgresConnectorTest.java b/src/test/java/com/rviewer/skeletons/infrastructure/persistence/PostgresConnectorTest.java new file mode 100644 index 0000000..5d4da54 --- /dev/null +++ b/src/test/java/com/rviewer/skeletons/infrastructure/persistence/PostgresConnectorTest.java @@ -0,0 +1,25 @@ +package com.rviewer.skeletons.infrastructure.persistence; + +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.jdbc.core.JdbcTemplate; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.when; + +@SpringBootTest +public class PostgresConnectorTest { + + @Mock + private JdbcTemplate jdbcTemplate; + @Autowired + private PostgresConnector postgresConnector; + + @Test + public void getConnectionStatus_shouldReturn2() { + when(jdbcTemplate.queryForObject("SELECT 1+1", Integer.class)).thenReturn(2); + assertEquals(2, postgresConnector.getConnectionStatus()); + } +} diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties new file mode 100644 index 0000000..22896c4 --- /dev/null +++ b/src/test/resources/application.properties @@ -0,0 +1,9 @@ +server.port=8080 +application.title=Java skeleton +application.version=1.0.0 + +spring.datasource.driver-class-name=org.h2.Driver +spring.jpa.database-platform=org.hibernate.dialect.H2Dialect +spring.datasource.url=jdbc:h2:mem:testdb +spring.datasource.username=user +spring.datasource.password=password \ No newline at end of file diff --git a/src/test/resources/banner.txt b/src/test/resources/banner.txt new file mode 100644 index 0000000..0c2529e --- /dev/null +++ b/src/test/resources/banner.txt @@ -0,0 +1,8 @@ +,------. ,--. +| .--. ' ,--. ,--. `--' ,---. ,--. ,--. ,---. ,--.--. +| '--'.' \ `' / ,--. | .-. : | |.'.| | | .-. : | .--' +| |\ \ \ / | | \ --. | .'. | \ --. | | +`--' '--' `--' `--' `----' '--' '--' `----' `--' + +${application.title} - v${application.version} +Powered by Rviewer \ No newline at end of file