-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
231 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
geo-service/src/main/java/com/cas/geoservice/dto/TrailGetRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
/* (C)2023 */ | ||
package com.cas.geoservice.dto; | ||
|
||
import lombok.*; | ||
|
45 changes: 45 additions & 0 deletions
45
geo-service/src/test/java/com/cas/geoservice/TrailControllerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.cas.geoservice; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.*; | ||
|
||
import com.cas.geoservice.controller.TrailController; | ||
import com.cas.geoservice.dto.*; | ||
import com.cas.geoservice.service.TrailService; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
@SpringBootTest | ||
public class TrailControllerTests { | ||
@Mock private TrailService trailService; | ||
@InjectMocks private TrailController trailController; | ||
|
||
private GenericMessage<TrailDto> message; | ||
private String username; | ||
@BeforeEach | ||
public void setUp() { | ||
username = "username"; | ||
message = | ||
GenericMessage.<TrailDto>builder() | ||
.status(HttpStatus.OK) | ||
.message("Trail Response") | ||
.build(); | ||
} | ||
|
||
@Test | ||
public void testGetTrail() { | ||
when(trailService.getTrail(any())).thenReturn(message); | ||
|
||
ResponseEntity<GenericMessage<TrailDto>> responseEntity = | ||
trailController.getTrail(username); | ||
|
||
assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); | ||
verify(trailService, times(1)).getTrail(any()); | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
geo-service/src/test/java/com/cas/geoservice/TrailServiceTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.cas.geoservice; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
import com.cas.geoservice.dto.GenericMessage; | ||
import com.cas.geoservice.dto.TrailDto; | ||
import com.cas.geoservice.dto.TrailGetRequest; | ||
import com.cas.geoservice.entity.Place; | ||
import com.cas.geoservice.entity.PlayerZone; | ||
import com.cas.geoservice.entity.Trail; | ||
import com.cas.geoservice.repository.PlayerZoneRepository; | ||
import com.cas.geoservice.repository.TrailRepository; | ||
import com.cas.geoservice.service.Impl.TrailServiceImpl; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@SpringBootTest | ||
public class TrailServiceTests { | ||
|
||
@Mock | ||
private TrailRepository trailRepository; | ||
|
||
@Mock | ||
private PlayerZoneRepository playerZoneRepository; | ||
|
||
@InjectMocks | ||
private TrailServiceImpl trailServiceImpl; | ||
|
||
private TrailGetRequest trailGetRequest; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
trailGetRequest = new TrailGetRequest("username"); | ||
} | ||
|
||
@Test | ||
public void testGetTrailWhenPlayerZoneNotExist() { | ||
when(playerZoneRepository.findByUsername(anyString())).thenReturn(null); | ||
|
||
GenericMessage<TrailDto> result = trailServiceImpl.getTrail(trailGetRequest); | ||
|
||
assertEquals(HttpStatus.BAD_REQUEST, result.getStatus()); | ||
assertEquals( | ||
"The player hasn't set zone, please set zone first...", result.getMessage()); | ||
} | ||
|
||
@Test | ||
public void testGetTrailWhenTrailNotExist() { | ||
PlayerZone playerZone = new PlayerZone(); | ||
playerZone.setName("Zone1"); | ||
|
||
when(playerZoneRepository.findByUsername(anyString())).thenReturn(playerZone); | ||
when(trailRepository.findByZone(anyString())).thenReturn(Optional.empty()); | ||
|
||
GenericMessage<TrailDto> result = trailServiceImpl.getTrail(trailGetRequest); | ||
|
||
assertEquals(HttpStatus.NOT_FOUND, result.getStatus()); | ||
assertEquals("No trail associated with this zone", result.getMessage()); | ||
} | ||
|
||
@Test | ||
public void testGetTrailSuccess() { | ||
PlayerZone playerZone = new PlayerZone(); | ||
playerZone.setName("Zone1"); | ||
|
||
List<Place> places = new ArrayList<>(); | ||
Place place1 = Place.builder().name("Place1").type("Type1").CoordinateX(1.0).CoordinateY(1.0).build(); | ||
Place place2 = Place.builder().name("Place2").type("Type2").CoordinateX(2.0).CoordinateY(2.0).build(); | ||
places.add(place1); | ||
places.add(place2); | ||
|
||
Trail trail = Trail.builder().id(1L).zone("Zone1").path(places).build(); | ||
|
||
place1.setTrail(trail); | ||
place2.setTrail(trail); | ||
|
||
when(playerZoneRepository.findByUsername(anyString())).thenReturn(playerZone); | ||
when(trailRepository.findByZone(anyString())).thenReturn(Optional.ofNullable(trail)); | ||
|
||
GenericMessage<TrailDto> result = trailServiceImpl.getTrail(trailGetRequest); | ||
|
||
assertEquals(HttpStatus.OK, result.getStatus()); | ||
assertEquals("Trail found successfully, and returned", result.getMessage()); | ||
|
||
// Additional verifications | ||
assertNotNull(result.getData()); | ||
assertEquals(1L, result.getData().getId()); | ||
assertEquals("Zone1", result.getData().getZone()); | ||
assertArrayEquals( | ||
places.stream().map(Place::toDto).toArray(), | ||
result.getData().getPath().toArray() | ||
); | ||
} | ||
} |