File tree Expand file tree Collapse file tree 5 files changed +94
-16
lines changed
main/java/org/aitesting/microservices/driver/query/service/controllers
java/org/aitesting/microservices/driverquery/unit
resources/contracts/driverquery Expand file tree Collapse file tree 5 files changed +94
-16
lines changed Original file line number Diff line number Diff line change @@ -100,6 +100,13 @@ jacocoTestReport {
100100}
101101
102102jacocoTestCoverageVerification {
103+ afterEvaluate {
104+ classDirectories = files(classDirectories. files. collect {
105+ fileTree(dir : " ${ project.buildDir} /classes/main" ,
106+ exclude : [' **/controllers/DriverControllerExceptionHandler*' ,
107+ ' **/DriverQueryApplication*' ])
108+ })
109+ }
103110violationRules {
104111rule {
105112limit {
Original file line number Diff line number Diff line change @@ -33,7 +33,13 @@ public ResponseEntity<List<Driver>> getDrivers() {
3333 @ GetMapping ("driver/{id}" )
3434 public ResponseEntity <Driver > getDriver (@ PathVariable UUID id ) {
3535 logger .info (String .format ("Request for a driver with id: %s" , id ));
36- return new ResponseEntity <>(driverRepository .findOne (id ), HttpStatus .OK );
36+ Driver driver = driverRepository .findOne (id );
37+ if (driver != null ) {
38+ logger .info (String .format ("Found driver by ID: %s" , id ));
39+ return new ResponseEntity <>(driver , HttpStatus .OK );
40+ }
41+ logger .info (String .format ("No drivers found by ID: %s" , id ));
42+ return new ResponseEntity <>(HttpStatus .NOT_FOUND );
3743 }
3844
3945}
Original file line number Diff line number Diff line change 1+ package org .aitesting .microservices .driverquery .unit ;
2+
3+ import static org .aitesting .microservices .driverquery .configurations .TestConstants .id1 ;
4+ import static org .mockito .Mockito .times ;
5+ import static org .mockito .Mockito .verify ;
6+
7+ import org .aitesting .microservices .driver .query .service .controllers .DriverController ;
8+ import org .aitesting .microservices .driver .query .service .repositories .DriverRepository ;
9+ import org .junit .Test ;
10+ import org .junit .runner .RunWith ;
11+ import org .mockito .InjectMocks ;
12+ import org .mockito .Mock ;
13+ import org .mockito .runners .MockitoJUnitRunner ;
14+
15+ @ RunWith (MockitoJUnitRunner .class )
16+ public class DriverControllerUnitTests {
17+
18+ @ InjectMocks
19+ private DriverController driverController = new DriverController ();
20+
21+ @ Mock
22+ private DriverRepository driverRepository ;
23+
24+ @ Test
25+ public void getAllDrivers_FindAllIsCalled () {
26+ //arrange
27+
28+ //act
29+ driverController .getDrivers ();
30+
31+ //assert
32+ verify (driverRepository , times (1 )).findAll ();
33+ }
34+
35+ @ Test
36+ public void getDriver_FindOneIsCalled () {
37+ //arrange
38+
39+ //act
40+ driverController .getDriver (id1 );
41+
42+ //assert
43+ verify (driverRepository , times (1 )).findOne (id1 );
44+ }
45+ }
Original file line number Diff line number Diff line change 1+ package driverquery
2+
3+ import org.springframework.cloud.contract.spec.*
4+ [
5+ Contract . make {
6+ description(" When a GET request with a driverId is made, the driver is returned" )
7+ request {
8+ method ' GET'
9+ url ' /api/v1/driver/1f13bafe-9236-4edf-ac47-bf2f1d3caec6'
10+ }
11+ response {
12+ status 200
13+ }
14+ },
15+ Contract . make {
16+ description(" When a GET request with driver not in DB, should get 404" )
17+ request {
18+ method ' GET'
19+ url ' api/v1/driver/7a7ec638-7793-4bfe-a043-1dc667d7cb19444'
20+ }
21+ response {
22+ status 404
23+ }
24+ },
25+ Contract . make {
26+ description(" When GET request with bad string id, should get 400" )
27+ request {
28+ method ' GET'
29+ url ' api/v1/driver/badString'
30+ }
31+ response {
32+ status 400
33+ }
34+ }
35+ ]
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments