diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a0814c2b264c396da4339d2cb9789d577012df12..7de05c2b26f3c6d2514c5c0b4c878673bbbc20ff 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,28 +1,70 @@ image: maven:3.8.5-openjdk-17 variables: + # This will suppress any download for dependencies and plugins or upload messages which would clutter the console log. MAVEN_OPTS: "-Dhttps.protocols=TLSv1.2 -Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true" MAVEN_CLI_OPTS: "--batch-mode --errors --fail-at-end --show-version -DinstallAtEnd=true -DdeployAtEnd=true" SPRING_PROFILES_ACTIVE: gitlab-ci - DATABASE_URL: "jdbc:mysql://mysql-service:3306/sparesti" - SPRING_DATASOURCE_USERNAME: root - SPRING_DATASOURCE_PASSWORD: root services: - - name: mysql:8.0.36 - alias: mysql-service - + - name: mysql:latest + alias: mysql + variables: + MYSQL_ROOT_PASSWORD: "root" # TODO Set this in GitLab's CI/CD environment variables for security in production + MYSQL_DATABASE: "sparesti" + stages: - - test + - compile_and_test + - generate_site_reports + - modify_generated_site_reports + - publish_pages cache: + key: "${CI_JOB_NAME}" # To keep cache across branches paths: - - .m2/repository - key: "$CI_BUILD_REF_NAME" # Separate cache for each branch or tag + - .m2/repository + +compile_and_test: + stage: compile_and_test + script: + - mvn clean test # Cleans the previous build and runs tests + when: always + +generate_site_reports: + stage: generate_site_reports + script: + - mvn site # Generates a website containing project documentation (including checkstyle report) + artifacts: + paths: + - target/site/ + only: + - master + +modify_generated_site_reports: # Modify the generated website containing project documentation to also link to JaCoCo coverage file + image: python:3.8-slim + stage: modify_generated_site_reports + script: + - pip install beautifulsoup4 # Used to parse contents of HTML files + - python scripts/add_jacoco_coverage_link_to_html.py # Script that modifies the HTML file to include a link to JaCoCo coverage file + artifacts: + paths: + - target/site/ + dependencies: + - generate_site_reports + only: + - master -test:jdk17: - stage: test +# We move the code coverage reports generated by jacoco and mvn site to be served by Gitlab Pages (Settings -> Pages) +publish_pages: + image: alpine:latest + stage: publish_pages + dependencies: + - modify_generated_site_reports script: - - docker run --publish 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:8.0.36 - - mvn clean test # Cleans the previous build and runs tests. - when: always \ No newline at end of file + - mkdir public + - cp -r target/site/* public/ + artifacts: + paths: + - public + only: + - master diff --git a/pom.xml b/pom.xml index 53af6c6c6997402f751c1ca3625b79828c18477c..6e4bdfb83e2951a74f197583746a89d2a4e859d0 100644 --- a/pom.xml +++ b/pom.xml @@ -126,7 +126,7 @@ <version>3.2.5</version> </plugin> - <!-- Jacoco --> + <!-- Produce code coverage reports --> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> @@ -137,9 +137,10 @@ <goal>prepare-agent</goal> </goals> </execution> + <!-- Attached to Maven test phase --> <execution> <id>report</id> - <phase>prepare-package</phase> + <phase>test</phase> <goals> <goal>report</goal> </goals> @@ -151,6 +152,51 @@ </excludes> </configuration> </plugin> + <!-- Plugin that generates a site for the current project --> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-site-plugin</artifactId> + <version>4.0.0-M13</version> + </plugin> + <!-- Plugin allow execution of system and Java programs --> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>exec-maven-plugin</artifactId> + <version>3.2.0</version> + <executions> + <execution> + <id>update-html-report</id> + <phase>prepare-package</phase> + <goals> + <goal>exec</goal> + </goals> + <configuration> + <executable>python</executable> + <workingDirectory>${project.basedir}/scripts</workingDirectory> + <arguments> + <argument>add_jacoco_coverage_link_to_html.py</argument> + </arguments> + </configuration> + </execution> + </executions> + </plugin> </plugins> </build> + <reporting> + <plugins> + <!-- Generate Checkstyle Report --> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-checkstyle-plugin</artifactId> + <version>3.3.1</version> + <reportSets> + <reportSet> + <reports> + <report>checkstyle</report> + </reports> + </reportSet> + </reportSets> + </plugin> + </plugins> + </reporting> </project> diff --git a/scripts/add_jacoco_coverage_link_to_html.py b/scripts/add_jacoco_coverage_link_to_html.py new file mode 100644 index 0000000000000000000000000000000000000000..44f057f4c8811a7f311dbf33eb4ce368f564575f --- /dev/null +++ b/scripts/add_jacoco_coverage_link_to_html.py @@ -0,0 +1,50 @@ +# Function is used to modify target/site/project-reports.html to include +# a link to the JaCoCo generated coverage files at target/site/jacoco/index.html. +def add_jacoco_coverage_link_to_html(file_path): + import bs4 + + # Open and read the HTML file at file_path + with open(file_path, 'r', encoding='utf-8') as file: + original_html = file.read() + + # Parse the content of the HTML file using Beautiful Soup (bs4) + soup = bs4.BeautifulSoup(original_html, features='html.parser') + + # Find the <ul> element that contains the Checkstyle link inside the Project Reports navigation section + # This is where we wanna add a list item for the JaCoCo coverage report + nav_list = soup.find("li", class_="active").find("ul", class_="nav nav-list") + + # Create the new list item for JaCoCo + new_li = soup.new_tag("li") + new_a = soup.new_tag("a", href="jacoco/index.html") + new_a.string = "Jacoco Coverage" + new_li.append(new_a) + + # Add the new list item to the navigation list + nav_list.append(new_li) + + # Find the table that contains the Checkstyle link + # This is where we wanna add a table that contains a link for the JaCoCo report + table = soup.find("table", class_="table table-striped") + + # Create the new table row for JaCoCo + new_tr = soup.new_tag("tr", **{"class": "a"}) + new_td1 = soup.new_tag("td") + new_a2 = soup.new_tag("a", href="jacoco/index.html") + new_a2.string = "Jacoco Coverage" + new_td2 = soup.new_tag("td") + new_td2.string = "Code coverage report generated by JaCoCo." + + new_td1.append(new_a2) + new_tr.append(new_td1) + new_tr.append(new_td2) + + # Add the new table row to the table + table.append(new_tr) + + # Write the modified HTML content back to the original file + with open(file_path, 'w', encoding='utf-8') as file: + file.write(str(soup.prettify())) + +file_path = 'target/site/project-reports.html' +add_jacoco_coverage_link_to_html(file_path) diff --git a/src/main/java/no/ntnu/idi/stud/savingsapp/model/user/User.java b/src/main/java/no/ntnu/idi/stud/savingsapp/model/user/User.java index 67b182a86294e5db4762829b54152652c3d5ba69..9d2c47d7cbc671346a4623ea99bd461f181faa7c 100644 --- a/src/main/java/no/ntnu/idi/stud/savingsapp/model/user/User.java +++ b/src/main/java/no/ntnu/idi/stud/savingsapp/model/user/User.java @@ -32,7 +32,7 @@ import org.springframework.security.core.userdetails.UserDetails; @AllArgsConstructor @NoArgsConstructor @Entity -@Table(name = "user") +@Table(name = "`user`") public class User implements UserDetails{ @Id diff --git a/src/main/resources/application-gitlab-ci.yml b/src/main/resources/application-gitlab-ci.yml new file mode 100644 index 0000000000000000000000000000000000000000..ed8ec32446c35a44baeaaf37ac9482291419bae1 --- /dev/null +++ b/src/main/resources/application-gitlab-ci.yml @@ -0,0 +1,25 @@ +server.port: 8080 + +spring: + datasource: + url: jdbc:mysql://mysql:3306/sparesti + username: root + password: root + jpa: + hibernate: + ddl-auto: update + show-sql: true + properties: + hibernate: + format_sql: true + defer-datasource-initialization: true + sql: + init: + mode: always + continue-on-error: true + +springdoc: + swagger-ui: + path: /swagger/index.html + api-docs: + path: /swagger/api-docs diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yml similarity index 100% rename from src/main/resources/application.yaml rename to src/main/resources/application.yml diff --git a/src/test/java/no/ntnu/idi/stud/savingsapp/DemoApplicationTests.java b/src/test/java/no/ntnu/idi/stud/savingsapp/DemoApplicationTests.java index a003cfbd22bfb331b6a12bb5bb6f3d2cdfd50383..f2bc8193c6ecdff89040c02a46a664220ffc75ed 100644 --- a/src/test/java/no/ntnu/idi/stud/savingsapp/DemoApplicationTests.java +++ b/src/test/java/no/ntnu/idi/stud/savingsapp/DemoApplicationTests.java @@ -13,13 +13,4 @@ class DemoApplicationTests { @Test void contextLoads() { } - - @Autowired - private TestRestTemplate restTemplate; - - @Test - void homeResponse() { - String body = this.restTemplate.getForObject("/", String.class); - assertEquals("Spring is here!", body); - } }