diff --git a/challenge-service/src/main/java/com/cas/challengeservice/entity/ChallengeType.java b/challenge-service/src/main/java/com/cas/challengeservice/entity/ChallengeType.java index e3d2ade..1314916 100644 --- a/challenge-service/src/main/java/com/cas/challengeservice/entity/ChallengeType.java +++ b/challenge-service/src/main/java/com/cas/challengeservice/entity/ChallengeType.java @@ -6,8 +6,11 @@ import lombok.*; @Data -@NoArgsConstructor @AllArgsConstructor +@NoArgsConstructor +@EqualsAndHashCode +@Builder(toBuilder = true) +@Table @Entity public class ChallengeType { @Id diff --git a/challenge-service/src/main/java/com/cas/challengeservice/service/Impl/ChallengeServiceImpl.java b/challenge-service/src/main/java/com/cas/challengeservice/service/Impl/ChallengeServiceImpl.java index 13fc92e..0fabf1b 100644 --- a/challenge-service/src/main/java/com/cas/challengeservice/service/Impl/ChallengeServiceImpl.java +++ b/challenge-service/src/main/java/com/cas/challengeservice/service/Impl/ChallengeServiceImpl.java @@ -43,13 +43,13 @@ public GenericMessage getChallenge(ChallengeGetRequest request } else { return GenericMessage.builder() .status(HttpStatus.UNAUTHORIZED) - .message("Heart rate is not in the range of this challenge type") + .message("GetChallenge failed, user heart rate not in range for this challenge type") .build(); } } else { return GenericMessage.builder() .status(HttpStatus.NOT_FOUND) - .message("No challenge type associated with this type") + .message("GetChallenge failed, no challenge type found with this type") .build(); } } @@ -101,7 +101,7 @@ public GenericMessage deleteChallenge(ChallengeDeleteRequest r if (!existingChallengeType.isPresent()) { return GenericMessage.builder() .status(HttpStatus.NOT_FOUND) - .message("Challenge type not found") + .message("DeleteChallenge failed, no challenge type found with this type") .build(); } diff --git a/challenge-service/src/test/java/com/cas/challengeservice/ChallengeControllerTests.java b/challenge-service/src/test/java/com/cas/challengeservice/ChallengeControllerTests.java index 481fb1d..5373449 100644 --- a/challenge-service/src/test/java/com/cas/challengeservice/ChallengeControllerTests.java +++ b/challenge-service/src/test/java/com/cas/challengeservice/ChallengeControllerTests.java @@ -1,72 +1,74 @@ -//package com.cas.challengeservice; -//import static org.junit.jupiter.api.Assertions.assertEquals; -//import static org.mockito.ArgumentMatchers.any; -//import static org.mockito.Mockito.*; -// -//import com.cas.challengeservice.controller.ChallengeController; -//import com.cas.challengeservice.dto.*; -//import com.cas.challengeservice.service.ChallengeService; -//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 ChallengeControllerTests { -// @Mock private ChallengeService challengeService; -// @InjectMocks private ChallengeController challengeController; -// private Long userHeartRate; -// private String type; -// -// private GenericMessage message; -// private ChallengeGetRequest challengeGetRequest; -// private ChallengeAddRequest challengeAddRequest; -// private ChallengeDeleteRequest challengeDeleteRequest; -// -// @BeforeEach -// public void setUp() { -// userHeartRate = 70L; -// type = "Balance"; -// -// challengeGetRequest = new ChallengeGetRequest(userHeartRate, type); -// challengeAddRequest = new ChallengeAddRequest(); // add properties as necessary -// challengeDeleteRequest = new ChallengeDeleteRequest(); // add properties as necessary -// -// message = GenericMessage.builder().status(HttpStatus.OK).build(); -// } -// -// @Test -// public void testGetChallenge() { -// when(challengeService.getChallenge(any())).thenReturn(message); -// -// ResponseEntity> responseEntity = -// challengeController.getChallenge(userHeartRate, type); -// -// assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); -// verify(challengeService, times(1)).getChallenge(any()); -// } -// -// @Test -// public void testAddChallenge() { -// when(challengeService.addChallenge(any())).thenReturn(message); -// -// ResponseEntity> responseEntity = -// challengeController.addChallenge(challengeAddRequest); -// -// assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); -// verify(challengeService, times(1)).addChallenge(any()); -// } -// -// @Test -// public void testDeleteChallenge() { -// when(challengeService.deleteChallenge(any())).thenReturn(message); -// -// ResponseEntity> responseEntity = -// challengeController.deleteChallenge(challengeDeleteRequest); -// -// assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); -// verify(challengeService, times(1)).deleteChallenge(any()); -// } -//} +package com.cas.challengeservice; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +import com.cas.challengeservice.controller.ChallengeController; +import com.cas.challengeservice.dto.*; +import com.cas.challengeservice.service.ChallengeService; +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 ChallengeControllerTests { + @Mock private ChallengeService challengeService; + @InjectMocks private ChallengeController challengeController; + private Long userHeartRate; + private Long exerciseCount; + private String type; + + private GenericMessage message; + private ChallengeGetRequest challengeGetRequest; + private ChallengeAddRequest challengeAddRequest; + private ChallengeDeleteRequest challengeDeleteRequest; + + @BeforeEach + public void setUp() { + userHeartRate = 70L; + type = "Balance"; + exerciseCount = 30L; + + challengeGetRequest = ChallengeGetRequest.builder().userHeartRate(userHeartRate).type(type).build(); + challengeAddRequest = ChallengeAddRequest.builder().userHeartRate(userHeartRate).exerciseCount(exerciseCount).type(type).build(); + challengeDeleteRequest = ChallengeDeleteRequest.builder().type(type).build(); + + message = GenericMessage.builder().status(HttpStatus.OK).message("Challenge Response").build(); + } + + @Test + public void testGetChallenge() { + when(challengeService.getChallenge(any())).thenReturn(message); + + ResponseEntity> responseEntity = + challengeController.getChallenge(userHeartRate, type); + + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + verify(challengeService, times(1)).getChallenge(any()); + } + + @Test + public void testAddChallenge() { + when(challengeService.addChallenge(any())).thenReturn(message); + + ResponseEntity> responseEntity = + challengeController.addChallenge(challengeAddRequest); + + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + verify(challengeService, times(1)).addChallenge(any()); + } + + @Test + public void testDeleteChallenge() { + when(challengeService.deleteChallenge(any())).thenReturn(message); + + ResponseEntity> responseEntity = + challengeController.deleteChallenge(challengeDeleteRequest); + + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + verify(challengeService, times(1)).deleteChallenge(any()); + } +} diff --git a/challenge-service/src/test/java/com/cas/challengeservice/ChallengeServiceTests.java b/challenge-service/src/test/java/com/cas/challengeservice/ChallengeServiceTests.java index bd3e8ec..1345f34 100644 --- a/challenge-service/src/test/java/com/cas/challengeservice/ChallengeServiceTests.java +++ b/challenge-service/src/test/java/com/cas/challengeservice/ChallengeServiceTests.java @@ -1,124 +1,116 @@ -//package com.cas.challengeservice; -// -//import com.cas.challengeservice.dto.*; -//import com.cas.challengeservice.entity.ChallengeType; -//import com.cas.challengeservice.repository.ChallengeTypeRepository; -//import com.cas.challengeservice.service.ChallengeService; -// -//import com.cas.challengeservice.service.Impl.ChallengeServiceImpl; -//import org.junit.jupiter.api.BeforeEach; -//import org.junit.jupiter.api.Test; -//import org.mockito.InjectMocks; -//import org.mockito.Mock; -//import org.mockito.MockitoAnnotations; -//import org.springframework.boot.test.context.SpringBootTest; -//import org.springframework.http.HttpStatus; -// -//import java.util.Optional; -// -//import static org.junit.jupiter.api.Assertions.*; -//import static org.mockito.Mockito.*; -//@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() { -// challengeAddRequest = ChallengeAddRequest.builder() -// .type("challengeType") -// .userHeartRate(70L) -// .exerciseCount(10L) -// .build(); -// -// challengeDeleteRequest = ChallengeDeleteRequest.builder() -// .type("challengeType") -// .build(); -// -// challengeGetRequest = ChallengeGetRequest.builder() -// .type("challengeType") -// .userHeartRate(70L) -// .build(); -// } -// -// @Test -// public void testGetChallengeWhenChallengeTypeNotExist() { -// when(challengeTypeRepository.findByDescription(any())).thenReturn(Optional.empty()); -// -// GenericMessage result = challengeServiceImpl.getChallenge(challengeGetRequest); -// -// assertEquals(HttpStatus.NOT_FOUND, result.getStatus()); -// assertEquals("No challenge type found with this type", result.getMessage()); -// } -// -// @Test -// public void testGetChallengeWhenUserHeartRateNotInRange() { -// ChallengeType challengeType = new ChallengeType(1L, "challengeType", 100L, 10L); -// -// when(challengeTypeRepository.findByDescription(any())).thenReturn(Optional.of(challengeType)); -// -// GenericMessage result = challengeServiceImpl.getChallenge(challengeGetRequest); -// -// assertEquals(HttpStatus.UNAUTHORIZED, result.getStatus()); -// assertEquals("User heart rate not in range for this challenge type", result.getMessage()); -// } -// -// @Test -// public void testGetChallengeSuccess() { -// ChallengeType challengeType = new ChallengeType(1L, "challengeType", 70L, 10L); -// -// when(challengeTypeRepository.findByDescription(any())).thenReturn(Optional.of(challengeType)); -// -// GenericMessage result = challengeServiceImpl.getChallenge(challengeGetRequest); -// -// assertEquals(HttpStatus.OK, result.getStatus()); -// assertEquals("Challenge type found", result.getMessage()); -// assertEquals(challengeType.toDto(), result.getData()); -// } -// -// @Test -// public void testAddChallengeTypeWhenChallengeTypeExist() { -// when(challengeTypeRepository.findByDescription(any())).thenReturn(Optional.of(new ChallengeType())); -// -// GenericMessage result = challengeServiceImpl.addChallenge(challengeAddRequest); -// -// assertEquals(HttpStatus.CONFLICT, result.getStatus()); -// assertEquals("Challenge type already exists with this type", result.getMessage()); -// } -// -// @Test -// public void testAddChallengeTypeSuccess() { -// when(challengeTypeRepository.findByDescription(any())).thenReturn(Optional.empty()); -// when(challengeTypeRepository.save(any())).thenReturn(new ChallengeType()); -// -// GenericMessage result = challengeServiceImpl.addChallenge(challengeAddRequest); -// -// assertEquals(HttpStatus.CREATED, result.getStatus()); -// assertEquals("Challenge type has been added successfully", result.getMessage()); -// } -// -// @Test -// public void testDeleteChallengeTypeWhenChallengeTypeNotFound() { -// when(challengeTypeRepository.findByDescription(any())).thenReturn(Optional.empty()); -// -// GenericMessage result = challengeServiceImpl.deleteChallenge(challengeDeleteRequest); -// -// assertEquals(HttpStatus.NOT_FOUND, result.getStatus()); -// assertEquals("No challenge type found with this type", result.getMessage()); -// } -// -// @Test -// public void testDeleteChallengeTypeSuccess() { -// ChallengeType challengeType = new ChallengeType(1L, "challengeType", 70L, 10L); -// -// when(challengeTypeRepository.findByDescription(any())).thenReturn(Optional.of(challengeType)); -// -// GenericMessage result = challengeServiceImpl.deleteChallenge(challengeDeleteRequest); -// -// assertEquals(HttpStatus.OK, result.getStatus()); -// assertEquals("Challenge type has been deleted successfully", result.getMessage()); -// } -//} +package com.cas.challengeservice; + +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 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 java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; +@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() { + } + + @Test + public void testGetChallengeWhenChallengeTypeNotExist() { + ChallengeGetRequest request = new ChallengeGetRequest(70L, "challengeType"); + when(challengeTypeRepository.findByDescription(any())).thenReturn(Optional.empty()); + + GenericMessage result = challengeServiceImpl.getChallenge(request); + + assertEquals(HttpStatus.NOT_FOUND, result.getStatus()); + assertEquals("GetChallenge failed, no challenge type found with this type", result.getMessage()); + } + + @Test + public void testGetChallengeWhenUserHeartRateNotInRange() { + ChallengeGetRequest request = new ChallengeGetRequest(300L, "Muscle"); + + ChallengeType challengeType = ChallengeType.builder().description("Muscle").userHeartRate(200L).build(); + + when(challengeTypeRepository.findByDescription(any())).thenReturn(Optional.ofNullable(challengeType)); + + GenericMessage result = challengeServiceImpl.getChallenge(request); + + assertEquals(HttpStatus.UNAUTHORIZED, result.getStatus()); + assertEquals("GetChallenge failed, user heart rate not in range for this challenge type", result.getMessage()); + } + + @Test + public void testGetChallengeSuccess() { + ChallengeType challengeType = ChallengeType.builder().description("Cardio").userHeartRate(120L).build(); + ChallengeGetRequest request = new ChallengeGetRequest(120L, "Cardio"); + + when(challengeTypeRepository.findByDescription(any())).thenReturn(Optional.ofNullable(challengeType)); + + GenericMessage result = challengeServiceImpl.getChallenge(request); + + assertEquals(HttpStatus.OK, result.getStatus()); + assertEquals("Challenge type found successfully, and returned", result.getMessage()); + } + + @Test + public void testAddChallengeTypeWhenChallengeTypeExist() { + ChallengeType challengeType = ChallengeType.builder().description("Cardio").userHeartRate(120L).build(); + ChallengeAddRequest request = new ChallengeAddRequest(120L, 30L, "Cardio"); + + when(challengeTypeRepository.findByDescription(any())).thenReturn(Optional.ofNullable(challengeType)); + + GenericMessage result = challengeServiceImpl.addChallenge(request); + + assertEquals(HttpStatus.CONFLICT, result.getStatus()); + assertEquals("Challenge type already exists.", result.getMessage()); + } + + @Test + public void testAddChallengeTypeSuccess() { + ChallengeAddRequest request = new ChallengeAddRequest(70L, 10L, "testAddChallenge"); + when(challengeTypeRepository.findByDescription(any())).thenReturn(Optional.empty()); + + GenericMessage result = challengeServiceImpl.addChallenge(request); + + assertEquals(HttpStatus.CREATED, result.getStatus()); + assertEquals("Challenge type added successfully", result.getMessage()); + } + + @Test + public void testDeleteChallengeTypeWhenChallengeTypeNotFound() { + ChallengeDeleteRequest request = new ChallengeDeleteRequest("testDeleteChallenge"); + when(challengeTypeRepository.findByDescription(any())).thenReturn(Optional.empty()); + + GenericMessage result = challengeServiceImpl.deleteChallenge(request); + + assertEquals(HttpStatus.NOT_FOUND, result.getStatus()); + assertEquals("DeleteChallenge failed, no challenge type found with this type", 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 result = challengeServiceImpl.deleteChallenge(request); + + assertEquals(HttpStatus.OK, result.getStatus()); + assertEquals("Challenge type deleted successfully", result.getMessage()); + } +}