From 27113767e2aac9a2bcbb70d51e13c871af33296e Mon Sep 17 00:00:00 2001 From: J1aM1ng Date: Tue, 5 Dec 2023 05:58:50 -0500 Subject: [PATCH] feat: challenge controller test --- .../service/Impl/ChallengeServiceImpl.java | 6 +- .../ChallengeControllerTests.java | 72 +++++++++++++++++++ .../ChallengeServiceTests.java | 4 ++ 3 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 challenge-service/src/test/java/com/cas/challengeservice/ChallengeControllerTests.java create mode 100644 challenge-service/src/test/java/com/cas/challengeservice/ChallengeServiceTests.java 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 79aa4d3..13fc92e 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 @@ -22,7 +22,7 @@ public ChallengeServiceImpl(ChallengeTypeRepository challengeTypeRepository) { /** * get challenge * - * @param request getChallenge request playload + * @param request getChallenge request payload * @return 200 if getChallenge successful, 401 if Heart rate is not in the range of this * challenge typed, 404 if No challenge type associated with this type */ @@ -57,7 +57,7 @@ public GenericMessage getChallenge(ChallengeGetRequest request /** * add challenge * - * @param request createChallenge request playload + * @param request createChallenge request payload * @return 200 if createChallenge successful, 409 if challenge already exists */ @Override @@ -90,7 +90,7 @@ public GenericMessage addChallenge(ChallengeAddRequest request /** * delete challenge * - * @param request deleteChallenge request playload + * @param request deleteChallenge request payload * @return 200 if deleteChallenge successful, 404 if challenge not found */ @Override diff --git a/challenge-service/src/test/java/com/cas/challengeservice/ChallengeControllerTests.java b/challenge-service/src/test/java/com/cas/challengeservice/ChallengeControllerTests.java new file mode 100644 index 0000000..fb79132 --- /dev/null +++ b/challenge-service/src/test/java/com/cas/challengeservice/ChallengeControllerTests.java @@ -0,0 +1,72 @@ +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()); + } +} diff --git a/challenge-service/src/test/java/com/cas/challengeservice/ChallengeServiceTests.java b/challenge-service/src/test/java/com/cas/challengeservice/ChallengeServiceTests.java new file mode 100644 index 0000000..1bc5aa7 --- /dev/null +++ b/challenge-service/src/test/java/com/cas/challengeservice/ChallengeServiceTests.java @@ -0,0 +1,4 @@ +package com.cas.challengeservice; + +public class ChallengeServiceTests { +}