Skip to content

Commit

Permalink
test: tests for geo done
Browse files Browse the repository at this point in the history
  • Loading branch information
json13245 committed Dec 6, 2023
1 parent a88d888 commit 2fb06c3
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* (C)2023 */
package com.cas.challengeservice;

import static org.junit.jupiter.api.Assertions.*;
Expand All @@ -7,7 +6,6 @@
import com.cas.challengeservice.dto.*;
import com.cas.challengeservice.entity.ChallengeType;
import com.cas.challengeservice.repository.ChallengeTypeRepository;
import com.cas.challengeservice.service.*;
import com.cas.challengeservice.service.Impl.ChallengeServiceImpl;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -19,109 +17,114 @@

@SpringBootTest
public class ChallengeServiceTests {
@Mock private ChallengeTypeRepository challengeTypeRepository;
@InjectMocks private ChallengeServiceImpl challengeServiceImpl;
private ChallengeAddRequest challengeAddRequest;
private ChallengeDeleteRequest challengeDeleteRequest;
private ChallengeGetRequest challengeGetRequest;

@BeforeEach
public void setUp() {}
@Mock
private ChallengeTypeRepository challengeTypeRepository;
@InjectMocks
private ChallengeServiceImpl challengeServiceImpl;
private ChallengeAddRequest challengeAddRequest;
private ChallengeDeleteRequest challengeDeleteRequest;
private ChallengeGetRequest challengeGetRequest;

@Test
public void testGetChallengeWhenChallengeTypeNotExist() {
ChallengeGetRequest request = new ChallengeGetRequest(70L, "challengeType");
when(challengeTypeRepository.findByDescription(any())).thenReturn(Optional.empty());
@BeforeEach
public void setUp() {
challengeGetRequest = new ChallengeGetRequest(70L, "challengeType");
challengeAddRequest = new ChallengeAddRequest(70L, 10L, "testAddChallenge");
challengeDeleteRequest = new ChallengeDeleteRequest("testDeleteChallenge");
}

GenericMessage<ChallengeTypeDto> result = challengeServiceImpl.getChallenge(request);
@Test
public void testGetChallengeWhenChallengeTypeNotExist() {
when(challengeTypeRepository.findByDescription(any())).thenReturn(Optional.empty());

assertEquals(HttpStatus.NOT_FOUND, result.getStatus());
assertEquals(
"GetChallenge failed, no challenge type found with this type", result.getMessage());
}
GenericMessage<ChallengeTypeDto> result = challengeServiceImpl.getChallenge(challengeGetRequest);

@Test
public void testGetChallengeWhenUserHeartRateNotInRange() {
ChallengeGetRequest request = new ChallengeGetRequest(300L, "Muscle");
assertEquals(HttpStatus.NOT_FOUND, result.getStatus());
assertEquals(
"GetChallenge failed, no challenge type found with this type", result.getMessage());
}

ChallengeType challengeType =
ChallengeType.builder().description("Muscle").userHeartRate(200L).build();
@Test
public void testGetChallengeWhenUserHeartRateNotInRange() {
ChallengeGetRequest request = new ChallengeGetRequest(300L, "Muscle");

when(challengeTypeRepository.findByDescription(any()))
.thenReturn(Optional.ofNullable(challengeType));
ChallengeType challengeType =
ChallengeType.builder().description("Muscle").userHeartRate(200L).build();

GenericMessage<ChallengeTypeDto> result = challengeServiceImpl.getChallenge(request);
when(challengeTypeRepository.findByDescription(any()))
.thenReturn(Optional.ofNullable(challengeType));

assertEquals(HttpStatus.UNAUTHORIZED, result.getStatus());
assertEquals(
"GetChallenge failed, user heart rate not in range for this challenge type",
result.getMessage());
}
GenericMessage<ChallengeTypeDto> result = challengeServiceImpl.getChallenge(request);

@Test
public void testGetChallengeSuccess() {
ChallengeType challengeType =
ChallengeType.builder().description("Cardio").userHeartRate(120L).build();
ChallengeGetRequest request = new ChallengeGetRequest(120L, "Cardio");
assertEquals(HttpStatus.UNAUTHORIZED, result.getStatus());
assertEquals(
"GetChallenge failed, user heart rate not in range for this challenge type",
result.getMessage());
}

when(challengeTypeRepository.findByDescription(any()))
.thenReturn(Optional.ofNullable(challengeType));
@Test
public void testGetChallengeSuccess() {
ChallengeType challengeType =
ChallengeType.builder().description("Cardio").userHeartRate(120L).build();
ChallengeGetRequest request = new ChallengeGetRequest(120L, "Cardio");

GenericMessage<ChallengeTypeDto> result = challengeServiceImpl.getChallenge(request);
when(challengeTypeRepository.findByDescription(any()))
.thenReturn(Optional.ofNullable(challengeType));

assertEquals(HttpStatus.OK, result.getStatus());
assertEquals("Challenge type found successfully, and returned", result.getMessage());
}
GenericMessage<ChallengeTypeDto> result = challengeServiceImpl.getChallenge(request);

@Test
public void testAddChallengeTypeWhenChallengeTypeExist() {
ChallengeType challengeType =
ChallengeType.builder().description("Cardio").userHeartRate(120L).build();
ChallengeAddRequest request = new ChallengeAddRequest(120L, 30L, "Cardio");
assertEquals(HttpStatus.OK, result.getStatus());
assertEquals("Challenge type found successfully, and returned", result.getMessage());
}

when(challengeTypeRepository.findByDescription(any()))
.thenReturn(Optional.ofNullable(challengeType));
@Test
public void testAddChallengeTypeWhenChallengeTypeExist() {
ChallengeType challengeType =
ChallengeType.builder().description("Cardio").userHeartRate(120L).build();
ChallengeAddRequest request = new ChallengeAddRequest(120L, 30L, "Cardio");

GenericMessage<ChallengeTypeDto> result = challengeServiceImpl.addChallenge(request);
when(challengeTypeRepository.findByDescription(any()))
.thenReturn(Optional.ofNullable(challengeType));

assertEquals(HttpStatus.CONFLICT, result.getStatus());
assertEquals("Challenge type already exists.", result.getMessage());
}
GenericMessage<ChallengeTypeDto> result = challengeServiceImpl.addChallenge(request);

@Test
public void testAddChallengeTypeSuccess() {
ChallengeAddRequest request = new ChallengeAddRequest(70L, 10L, "testAddChallenge");
when(challengeTypeRepository.findByDescription(any())).thenReturn(Optional.empty());
assertEquals(HttpStatus.CONFLICT, result.getStatus());
assertEquals("Challenge type already exists.", result.getMessage());
}

GenericMessage<ChallengeTypeDto> result = challengeServiceImpl.addChallenge(request);
@Test
public void testAddChallengeTypeSuccess() {
when(challengeTypeRepository.findByDescription(any())).thenReturn(Optional.empty());

assertEquals(HttpStatus.CREATED, result.getStatus());
assertEquals("Challenge type added successfully", result.getMessage());
}
GenericMessage<ChallengeTypeDto> result = challengeServiceImpl.addChallenge(challengeAddRequest);

@Test
public void testDeleteChallengeTypeWhenChallengeTypeNotFound() {
ChallengeDeleteRequest request = new ChallengeDeleteRequest("testDeleteChallenge");
when(challengeTypeRepository.findByDescription(any())).thenReturn(Optional.empty());
assertEquals(HttpStatus.CREATED, result.getStatus());
assertEquals("Challenge type added successfully", result.getMessage());
}

GenericMessage<ChallengeTypeDto> result = challengeServiceImpl.deleteChallenge(request);
@Test
public void testDeleteChallengeTypeWhenChallengeTypeNotFound() {

assertEquals(HttpStatus.NOT_FOUND, result.getStatus());
assertEquals(
"DeleteChallenge failed, no challenge type found with this type",
result.getMessage());
}
when(challengeTypeRepository.findByDescription(any())).thenReturn(Optional.empty());

@Test
public void testDeleteChallengeTypeSuccess() {
ChallengeType challengeType = ChallengeType.builder().description("Flexibility").build();
ChallengeDeleteRequest request = new ChallengeDeleteRequest("Flexibility");
when(challengeTypeRepository.findByDescription(any()))
.thenReturn(Optional.ofNullable(challengeType));
GenericMessage<ChallengeTypeDto> result = challengeServiceImpl.deleteChallenge(challengeDeleteRequest);

GenericMessage<ChallengeTypeDto> result = challengeServiceImpl.deleteChallenge(request);
assertEquals(HttpStatus.NOT_FOUND, result.getStatus());
assertEquals(
"DeleteChallenge failed, no challenge type found with this type",
result.getMessage());
}

assertEquals(HttpStatus.OK, result.getStatus());
assertEquals("Challenge type deleted successfully", result.getMessage());
}
@Test
public void testDeleteChallengeTypeSuccess() {
ChallengeType challengeType = ChallengeType.builder().description("Flexibility").build();
ChallengeDeleteRequest request = new ChallengeDeleteRequest("Flexibility");
when(challengeTypeRepository.findByDescription(any()))
.thenReturn(Optional.ofNullable(challengeType));

GenericMessage<ChallengeTypeDto> result = challengeServiceImpl.deleteChallenge(request);

assertEquals(HttpStatus.OK, result.getStatus());
assertEquals("Challenge type deleted successfully", result.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* (C)2023 */
package com.cas.geoservice.dto;

import lombok.*;
Expand Down
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 geo-service/src/test/java/com/cas/geoservice/TrailServiceTests.java
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()
);
}
}

0 comments on commit 2fb06c3

Please sign in to comment.