Skip to content

Commit

Permalink
feat: challenge controller test
Browse files Browse the repository at this point in the history
  • Loading branch information
json13245 committed Dec 5, 2023
1 parent 1a5d8c9 commit 2711376
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -57,7 +57,7 @@ public GenericMessage<ChallengeTypeDto> 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
Expand Down Expand Up @@ -90,7 +90,7 @@ public GenericMessage<ChallengeTypeDto> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ChallengeTypeDto> 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.<ChallengeTypeDto>builder().status(HttpStatus.OK).build();
}

@Test
public void testGetChallenge() {
when(challengeService.getChallenge(any())).thenReturn(message);

ResponseEntity<GenericMessage<ChallengeTypeDto>> 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<GenericMessage<ChallengeTypeDto>> 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<GenericMessage<ChallengeTypeDto>> responseEntity =
challengeController.deleteChallenge(challengeDeleteRequest);

assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
verify(challengeService, times(1)).deleteChallenge(any());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.cas.challengeservice;

public class ChallengeServiceTests {
}

0 comments on commit 2711376

Please sign in to comment.