Skip to content

Commit

Permalink
Merge pull request #56 from NashTech-Labs/feature/sonar-integration
Browse files Browse the repository at this point in the history
Feature/sonar integration
  • Loading branch information
vimal-knoldus authored Jan 12, 2024
2 parents 3232b36 + 758b3c8 commit 0c4a7f0
Show file tree
Hide file tree
Showing 4 changed files with 282 additions and 5 deletions.
231 changes: 226 additions & 5 deletions cart-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@
<maven.compiler.source>19</maven.compiler.source>
<mysql-connector.version>8.0.33</mysql-connector.version>
<dotenv.version>4.0.0</dotenv.version>
<maven.junit.jupiter.version>5.9.2</maven.junit.jupiter.version>
<maven.sonar.version>3.9.1.2184</maven.sonar.version>
<maven.checkstyle.version>3.2.1</maven.checkstyle.version>
<maven.spotbugs.version>4.7.3.4</maven.spotbugs.version>
<maven.findbugs.version>1.12.0</maven.findbugs.version>
<maven.pmd.version>3.20.0</maven.pmd.version>
<maven.jacoco.version>0.8.8</maven.jacoco.version>
<minimumCodeCoverage>.80</minimumCodeCoverage>
</properties>

<dependencies>
Expand All @@ -69,11 +77,6 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand All @@ -90,10 +93,185 @@
<artifactId>spring-dotenv</artifactId>
<version>${dotenv.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>${maven.pmd.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${maven.checkstyle.version}</version>
</plugin>
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<version>${maven.spotbugs.version}</version>
</plugin>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>${sonarVersion}</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${maven.jacoco.version}</version>
</plugin>
</plugins>
</pluginManagement>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>${maven.pmd.version}</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>pmd</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${maven.checkstyle.version}</version>
<configuration>
<consoleOutput>true</consoleOutput>
<failsOnError>false</failsOnError>
<failOnViolation>false</failOnViolation>
</configuration>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${maven.jacoco.version}</version>
<configuration>
<excludes>
<exclude>**/*config*/**</exclude>
<exclude>**/*common*/**</exclude>
<exclude>**/*constant*/**</exclude>
<exclude>**/*model*/**</exclude>
<exclude>**/*Application*</exclude>
<exclude>**/*entity*/**</exclude>
<exclude>**/resources/**</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<phase>test-compile</phase>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>jacoco-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule implementation="org.jacoco.maven.RuleConfiguration">
<element>PACKAGE</element>
<limits>
<limit implementation="org.jacoco.report.check.Limit">
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>${minimumCodeCoverage}
</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<version>${maven.spotbugs.version}</version>
<executions>
<execution>
<id>check</id>
<phase>test</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<failOnError>true</failOnError>
<spotbugsXmlOutputFilename>findbugs.xml
</spotbugsXmlOutputFilename>
<includeFilterFile>
${basedir}/src/main/resources/spotbugs/spotbugs-security-include.xml
</includeFilterFile>
<excludeFilterFile>
${basedir}/src/main/resources/spotbugs/spotbugs-security-exclude.xml
</excludeFilterFile>
<plugins>
<plugin>
<groupId>com.h3xstream.findsecbugs</groupId>
<artifactId>findsecbugs-plugin</artifactId>
<version>${maven.findbugs.version}</version>
</plugin>
</plugins>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<outputDirectory>${rootDir}/target</outputDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
Expand All @@ -109,4 +287,47 @@
</plugins>
</build>

<profiles>
<profile>
<id>sonar</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<!-- Sonar Config -->
<sonar.organization>nashtech</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
<sonarConfig>b47aeba29df2889126c736ee7012a5a490edc34a</sonarConfig> <!-- NOSONAR -->
<sonar.login>${sonarConfig}</sonar.login> <!-- NOSONAR -->
<sonar.java.checkstyle.reportPaths>target/checkstyle-result.xml</sonar.java.checkstyle.reportPaths>
<sonar.java.pmd.reportPaths>target/pmd.xml</sonar.java.pmd.reportPaths>
<sonar.java.findbugs.reportPaths>target/findbugsXml.xml</sonar.java.findbugs.reportPaths>
<sonar.jacoco.reportPath>target/coverage-reports/jacoco.exec</sonar.jacoco.reportPath>
<sonar.coverage.exclusions>
**/*config*/**,
**/*common*/**,
**/*constant*/**,
**/*model*/**,
**/*Application.*
</sonar.coverage.exclusions>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>${maven.sonar.version}</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>sonar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<FindBugsFilter>
<Match>
<Bug pattern="EI_EXPOSE_REP"/>
</Match>
</FindBugsFilter>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<FindBugsFilter>
<Match>
<Bug category="SECURITY"/>
</Match>
</FindBugsFilter>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.nashtech.car.cart.service;


import com.nashtech.car.cart.model.CartItem;
import com.nashtech.car.cart.repository.CartItemRepository;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import java.util.Collections;
import java.util.List;

import static org.mockito.ArgumentMatchers.anyString;

class CartServiceTest {

@Mock
private CartItemRepository cartItemRepository;

@InjectMocks
private CartService cartService;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}

@Test
void testGetFromCart() {
CartItem cartItem = new CartItem();
cartItem.setProductId("123");
List<CartItem> cartItemList = Collections.singletonList(cartItem);
Mockito.when(cartItemRepository.findByUserId(anyString())).thenReturn(cartItemList);

List<CartItem> result = cartService.getFromCart("user1");

Assertions.assertNotNull(result);
Assertions.assertEquals(1, result.size());
Assertions.assertEquals("123", result.get(0).getProductId());
}
}

0 comments on commit 0c4a7f0

Please sign in to comment.