Skip to content

Commit

Permalink
Merge pull request #541 from spacious-team/task-gh-465
Browse files Browse the repository at this point in the history
Добавить checker framework и github action для юнит тестов
  • Loading branch information
vananiev authored Nov 6, 2024
2 parents 449076d + 43af15a commit 5f30e36
Show file tree
Hide file tree
Showing 249 changed files with 1,973 additions and 1,350 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Unit Tests

on:
workflow_dispatch:
pull_request:
branches:
- 'master'
- 'develop'
push:
branches:
- 'master'
- 'develop'

jobs:
tests:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up JDK
uses: actions/setup-java@v3
with:
java-version: '22'
distribution: 'liberica'
cache: maven

- name: Maven Tests
run: mvn --batch-mode clean test

- name: Test Coverage
uses: codecov/codecov-action@v4
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

- name: SonarCloud Analyze
run: >
mvn --batch-mode sonar:sonar
-Dsonar.projectKey=spacious-team_investbook
-Dsonar.organization=spacious-team
-Dsonar.host.url=https://sonarcloud.io
-Dsonar.login=$SONAR_TOKEN
-Dsonar.coverage.jacoco.xmlReportPaths=./target/site/jacoco/jacoco.xml
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
[![spring-boot-version](https://img.shields.io/badge/spring--boot-3.3.4-brightgreen?style=flat-square)](/~https://github.com/spring-projects/spring-boot/releases)
[![hits-of-code](https://img.shields.io/badge/dynamic/json?style=flat-square&color=lightblue&label=hits-of-code&url=https://hitsofcode.com/github/spacious-team/investbook/json?branch=develop&query=$.count)](https://hitsofcode.com/github/spacious-team/investbook/view?branch=develop)
[![github-closed-pull-requests](https://img.shields.io/github/issues-pr-closed/spacious-team/investbook?style=flat-square&color=brightgreen)](/~https://github.com/spacious-team/investbook/pulls?q=is%3Apr+is%3Aclosed)
[![Unit tests](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fspacious-team%2Finvestbook%2Fbadge%3Fref%3Ddevelop&style=flat-square&label=test&logo=none)](
/~https://github.com/spacious-team/investbook/actions/workflows/unit-tests.yml)
[![github-workflow-status](https://img.shields.io/github/actions/workflow/status/spacious-team/investbook/publish-docker.yml?style=flat-square&branch=master)](/~https://github.com/spacious-team/investbook/actions/workflows/publish-docker.yml)
[![github-all-releases](https://img.shields.io/github/downloads/spacious-team/investbook/total?style=flat-square&logo=github&color=lightblue)](/~https://github.com/spacious-team/investbook/releases/latest)
[![docker-pulls](https://img.shields.io/docker/pulls/spaciousteam/investbook?style=flat-square&logo=docker&color=lightblue&logoColor=white)](https://hub.docker.com/r/spaciousteam/investbook)
Expand Down
76 changes: 76 additions & 0 deletions checkerframework.astub
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* InvestBook
* Copyright (C) 2024 Spacious Team <spacious-team@ya.ru>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.checker.nullness.qual.EnsuresNonNull;
import org.checkerframework.checker.nullness.qual.EnsuresNonNullIf;

package java.util;
public class Objects {

@EnsuresNonNull("#1")
static <T> T requireNonNull(@Nullable T obj);

@EnsuresNonNull("#1")
static <T> T requireNonNull(@Nullable T obj, String message);

@EnsuresNonNull("#1")
static <T> T requireNonNull(@Nullable T obj, Supplier<String> messageSupplier);

@EnsuresNonNullIf(expression="#1", result=true)
static boolean nonNull(@Nullable Object obj);

@EnsuresNonNullIf(expression="#1", result=false)
static boolean isNull(@Nullable Object obj);
}


package org.springframework.util;
class StringUtils {

@EnsuresNonNullIf(expression="#1", result=true)
static boolean hasText(@Nullable CharSequence str);

@EnsuresNonNullIf(expression="#1", result=true)
static boolean hasText(@Nullable String str);

@EnsuresNonNullIf(expression="#1", result=true)
static boolean hasLength(@Nullable CharSequence str);

@EnsuresNonNullIf(expression="#1", result=true)
static boolean hasLength(@Nullable String str);
}


package org.springframework.util;
class CollectionUtils {

@EnsuresNonNullIf(expression="#1", result=false)
static boolean isEmpty(@Nullable Collection<?> collection);

@EnsuresNonNullIf(expression="#1", result=false)
static boolean isEmpty(@Nullable Map<?, ?> map);
}


package org.slf4j;
interface Logger {

void warn(String format, @Nullable Object arg1, @Nullable Object arg2);

void warn(String format, @Nullable Object... arguments);
}
124 changes: 117 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@

<properties>
<!-- Valid version is (0-255).(0-255).(0-65535) -->
<win.msi.version>24.2</win.msi.version>
<win.msi.version>24.2</win.msi.version> <!-- TODO: remove dependencyManagement table-wrapper-api before release -->
<java.version>22</java.version>
<lombok.version>1.18.34</lombok.version>
<checkerframework.version>3.48.1</checkerframework.version>
</properties>

<repositories>
Expand All @@ -86,17 +88,23 @@
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<!-- version is same as for hibernate-jpamodelgen dep, just scope is changed -->
<!-- scope is changed, CbrForeignExchangeRateServiceXmlImpl.class requires -->
<scope>compile</scope>
</dependency>
<!-- TODO: remove before release -->
<dependency>
<groupId>com.github.spacious-team</groupId>
<artifactId>table-wrapper-api</artifactId>
<version>f2341ce264</version>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>com.github.spacious-team</groupId>
<artifactId>broker-report-parser-api</artifactId>
<version>2024.1</version>
<version>109c37e4a9</version>
</dependency>
<dependency>
<groupId>com.github.spacious-team</groupId>
Expand Down Expand Up @@ -164,10 +172,6 @@
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
Expand Down Expand Up @@ -209,6 +213,7 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
Expand All @@ -222,10 +227,95 @@
<artifactId>poi-scratchpad</artifactId>
<version>5.2.5</version> <!-- use same version as table-wrapper-spring-boot-starter provides -->
</dependency>
<dependency>
<groupId>org.checkerframework</groupId>
<artifactId>checker-qual</artifactId>
<version>${checkerframework.version}</version>
<scope>provided</scope> <!-- for static analysis tools only -->
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
<version>3.17.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.1</version> <!-- JUnit 5 requirement -->
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<fork>true</fork> <!-- Must fork or else JVM arguments are ignored. -->
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>${hibernate.version}</version>
</path>
<path>
<groupId>org.checkerframework</groupId>
<artifactId>checker</artifactId>
<version>${checkerframework.version}</version>
</path>
</annotationProcessorPaths>
<annotationProcessors>
<annotationProcessor>
lombok.launch.AnnotationProcessorHider$AnnotationProcessor
</annotationProcessor>
<annotationProcessor>
org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor
</annotationProcessor>
<annotationProcessor>
org.checkerframework.checker.nullness.NullnessChecker
</annotationProcessor>
</annotationProcessors>
<compilerArgs combine.children="append">
<!--arg>-Awarns</arg-->
<!-- V2022_1_0_1.class нельзя изменять, чтобы не менялась чек сумма миграции -->
<!-- Классы Entity(Pk)?_ сгенерированы hibernate-jpamodelgen -->
<arg>-AskipDefs=(Test$|V2022_1_0_1|Entity(Pk)?_$|generated.ValCurs)</arg>
<!-- Checker framework inner file /~https://github.com/typetools/checker-framework/blob/master/checker/src/main/java/org/checkerframework/checker/nullness/permit-nullness-assertion-exception.astub -->
<arg>-Astubs=permit-nullness-assertion-exception.astub${path.separator}${project.basedir}/checkerframework.astub</arg>
<arg>-AsuppressWarnings=methodref,type.arguments.not.inferred,lambda.param,expression.unparsable</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED</arg>
<arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
Expand Down Expand Up @@ -287,6 +377,26 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.12</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import jakarta.persistence.EntityManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
Expand Down Expand Up @@ -93,7 +94,7 @@ public boolean insert(Pojo object) {
log.error("Can't INSERT by optimized deprecated Hibernate method save(): {}", object, e);
}
}
Boolean result = transactionTemplateRequired.execute(_ -> createIfAbsent(object));
@Nullable Boolean result = transactionTemplateRequired.execute(_ -> createIfAbsent(object));
return Boolean.TRUE.equals(result);
}

Expand Down Expand Up @@ -123,14 +124,14 @@ public CreateResult<Pojo> createIfAbsentAndGet(Pojo object) {
* @implSpec Should be called in transaction
*/
private Optional<Entity> createIfAbsentInternal(Pojo object) {
Entity entity = null;
@Nullable Entity entity = null;
if (repository instanceof ConstraintAwareRepository<Entity, ID> caRepository) {
entity = converter.toEntity(object);
if (caRepository.exists(entity)) {
return Optional.empty();
}
} else {
ID id = getId(object);
@Nullable ID id = getId(object);
if (id != null && existsById(id)) {
return Optional.empty();
}
Expand All @@ -153,13 +154,13 @@ private Optional<Entity> createIfAbsentInternal(Pojo object) {
* @implSpec Should be called in transaction
*/
private CreateResult<Pojo> createIfAbsentAndGetInternal(Pojo object) {
Entity entity = null;
@Nullable Entity entity = null;
Optional<Entity> selectedEntity;
if (repository instanceof ConstraintAwareRepository<Entity, ID> caRepository) {
entity = converter.toEntity(object);
selectedEntity = caRepository.findBy(entity);
} else {
ID id = getId(object);
@Nullable ID id = getId(object);
selectedEntity = Optional.ofNullable(id)
.flatMap(repository::findById);
}
Expand Down
Loading

0 comments on commit 5f30e36

Please sign in to comment.