From 69ba9f45156cc8d17e6b786d74302f49f2e868a1 Mon Sep 17 00:00:00 2001 From: Kuldeep Date: Fri, 12 Jan 2024 10:20:34 +0530 Subject: [PATCH 1/2] added testcase and integrated sonar --- cart-service/pom.xml | 57 +++++++++++++++++++ .../car/cart/service/CartServiceTest.java | 46 +++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 cart-service/src/test/java/com/nashtech/car/cart/service/CartServiceTest.java diff --git a/cart-service/pom.xml b/cart-service/pom.xml index ae423e55..8944b8e9 100644 --- a/cart-service/pom.xml +++ b/cart-service/pom.xml @@ -44,6 +44,8 @@ 19 8.0.33 4.0.0 + 5.9.2 + 3.9.1.2184 @@ -90,6 +92,22 @@ spring-dotenv ${dotenv.version} + + org.junit.vintage + junit-vintage-engine + ${maven.junit.jupiter.version} + test + + + org.mockito + mockito-core + test + + + org.junit.jupiter + junit-jupiter + test + @@ -109,4 +127,43 @@ + + + sonar + + false + + + + nashtech + https://sonarcloud.io + b47aeba29df2889126c736ee7012a5a490edc34a + ${sonarConfig} + + **/*config*/**, + **/*common*/**, + **/*constant*/**, + **/*model*/**, + **/*Application.* + + + + + + org.sonarsource.scanner.maven + sonar-maven-plugin + ${maven.sonar.version} + + + verify + + sonar + + + + + + + + diff --git a/cart-service/src/test/java/com/nashtech/car/cart/service/CartServiceTest.java b/cart-service/src/test/java/com/nashtech/car/cart/service/CartServiceTest.java new file mode 100644 index 00000000..750b0a8e --- /dev/null +++ b/cart-service/src/test/java/com/nashtech/car/cart/service/CartServiceTest.java @@ -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 cartItemList = Collections.singletonList(cartItem); + Mockito.when(cartItemRepository.findByUserId(anyString())).thenReturn(cartItemList); + + List result = cartService.getFromCart("user1"); + + Assertions.assertNotNull(result); + Assertions.assertEquals(1, result.size()); + Assertions.assertEquals("123", result.get(0).getProductId()); + } +} + From 758b3c8d4c684ce581863b0fb79c3f34e5263176 Mon Sep 17 00:00:00 2001 From: Kuldeep Date: Fri, 12 Jan 2024 11:44:51 +0530 Subject: [PATCH 2/2] Sonar Integration added plugins for spotbugs, pmd, checkstyle --- cart-service/pom.xml | 180 +++++++++++++++++- .../spotbugs/spotbugs-security-exclude.xml | 5 + .../spotbugs/spotbugs-security-include.xml | 5 + 3 files changed, 182 insertions(+), 8 deletions(-) create mode 100644 cart-service/src/main/resources/spotbugs/spotbugs-security-exclude.xml create mode 100644 cart-service/src/main/resources/spotbugs/spotbugs-security-include.xml diff --git a/cart-service/pom.xml b/cart-service/pom.xml index 8944b8e9..621d643c 100644 --- a/cart-service/pom.xml +++ b/cart-service/pom.xml @@ -46,6 +46,12 @@ 4.0.0 5.9.2 3.9.1.2184 + 3.2.1 + 4.7.3.4 + 1.12.0 + 3.20.0 + 0.8.8 + .80 @@ -71,11 +77,6 @@ true - - org.springframework.boot - spring-boot-starter-data-rest - - org.springframework.boot spring-boot-starter-web @@ -93,11 +94,15 @@ ${dotenv.version} - org.junit.vintage - junit-vintage-engine - ${maven.junit.jupiter.version} + org.springframework.boot + spring-boot-starter-test test + + org.junit.jupiter + junit-jupiter-engine + ${junit-jupiter.version} + org.mockito mockito-core @@ -111,7 +116,162 @@ + + + + org.apache.maven.plugins + maven-pmd-plugin + ${maven.pmd.version} + + + org.apache.maven.plugins + maven-checkstyle-plugin + ${maven.checkstyle.version} + + + com.github.spotbugs + spotbugs-maven-plugin + ${maven.spotbugs.version} + + + org.sonarsource.scanner.maven + sonar-maven-plugin + ${sonarVersion} + + + org.jacoco + jacoco-maven-plugin + ${maven.jacoco.version} + + + + + + org.apache.maven.plugins + maven-pmd-plugin + ${maven.pmd.version} + + + validate + + pmd + + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + ${maven.checkstyle.version} + + true + false + false + + + + validate + validate + + check + + + + + + org.jacoco + jacoco-maven-plugin + ${maven.jacoco.version} + + + **/*config*/** + **/*common*/** + **/*constant*/** + **/*model*/** + **/*Application* + **/*entity*/** + **/resources/** + + + + + prepare-agent + + prepare-agent + + test-compile + + + report + test + + report + + + + jacoco-check + + check + + + + + PACKAGE + + + LINE + COVEREDRATIO + ${minimumCodeCoverage} + + + + + + + + + + + com.github.spotbugs + spotbugs-maven-plugin + ${maven.spotbugs.version} + + + check + test + + check + + + + + true + findbugs.xml + + + ${basedir}/src/main/resources/spotbugs/spotbugs-security-include.xml + + + ${basedir}/src/main/resources/spotbugs/spotbugs-security-exclude.xml + + + + com.h3xstream.findsecbugs + findsecbugs-plugin + ${maven.findbugs.version} + + + + + + org.apache.maven.plugins + maven-jar-plugin + 3.3.0 + + ${rootDir}/target + + org.springframework.boot spring-boot-maven-plugin @@ -139,6 +299,10 @@ https://sonarcloud.io b47aeba29df2889126c736ee7012a5a490edc34a ${sonarConfig} + target/checkstyle-result.xml + target/pmd.xml + target/findbugsXml.xml + target/coverage-reports/jacoco.exec **/*config*/**, **/*common*/**, diff --git a/cart-service/src/main/resources/spotbugs/spotbugs-security-exclude.xml b/cart-service/src/main/resources/spotbugs/spotbugs-security-exclude.xml new file mode 100644 index 00000000..32272a07 --- /dev/null +++ b/cart-service/src/main/resources/spotbugs/spotbugs-security-exclude.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/cart-service/src/main/resources/spotbugs/spotbugs-security-include.xml b/cart-service/src/main/resources/spotbugs/spotbugs-security-include.xml new file mode 100644 index 00000000..55c856c7 --- /dev/null +++ b/cart-service/src/main/resources/spotbugs/spotbugs-security-include.xml @@ -0,0 +1,5 @@ + + + + +