-
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.
Added unit testcases for code coverage
- Loading branch information
Showing
9 changed files
with
1,028 additions
and
6 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
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
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
108 changes: 108 additions & 0 deletions
108
src/test/java/sg/edu/nus/iss/shopsmart_backend/controller/ApiControllerTest.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,108 @@ | ||
package sg.edu.nus.iss.shopsmart_backend.controller; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.swagger.v3.core.util.Json; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
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.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import sg.edu.nus.iss.shopsmart_backend.model.ApiRequestResolver; | ||
import sg.edu.nus.iss.shopsmart_backend.model.ApiResponseResolver; | ||
import sg.edu.nus.iss.shopsmart_backend.service.ApiService; | ||
import sg.edu.nus.iss.shopsmart_backend.service.CommonService; | ||
import sg.edu.nus.iss.shopsmart_backend.utils.Utils; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.doNothing; | ||
import static org.mockito.Mockito.when; | ||
|
||
class ApiControllerTest { | ||
private final ObjectMapper objectMapper = Json.mapper(); | ||
@Mock | ||
private ObjectMapper mapper; | ||
|
||
@Mock | ||
private ApiService apiService; | ||
@Mock | ||
private CommonService commonService; | ||
@Mock | ||
private Utils utils; | ||
@Mock | ||
private HttpServletResponse httpServletResponse; | ||
@Mock | ||
private HttpServletRequest httpServletRequest; | ||
|
||
@InjectMocks | ||
private ApiController apiController; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
MockitoAnnotations.openMocks(this); | ||
} | ||
|
||
@Test | ||
public void testHandleGetRequest() throws Exception { | ||
commonMocking(); | ||
|
||
ResponseEntity<JsonNode> response = apiController.handleGetRequest("test", | ||
httpServletRequest, httpServletResponse).get(); | ||
assertEquals(HttpStatus.OK, response.getStatusCode()); | ||
} | ||
|
||
@Test | ||
public void testHandlePostRequest() throws Exception { | ||
commonMocking(); | ||
|
||
ResponseEntity<JsonNode> response = apiController.handlePostRequest("test", objectMapper.createObjectNode(), | ||
httpServletRequest, httpServletResponse).get(); | ||
assertEquals(HttpStatus.OK, response.getStatusCode()); | ||
} | ||
|
||
@Test | ||
public void testHandlePutRequest() throws Exception { | ||
commonMocking(); | ||
|
||
ResponseEntity<JsonNode> response = apiController.handlePutRequest("test", objectMapper.createObjectNode(), | ||
httpServletRequest, httpServletResponse).get(); | ||
assertEquals(HttpStatus.OK, response.getStatusCode()); | ||
} | ||
|
||
@Test | ||
public void testHandlePatchRequest() throws Exception { | ||
commonMocking(); | ||
|
||
ResponseEntity<JsonNode> response = apiController.handlePatchRequest("test", objectMapper.createObjectNode(), | ||
httpServletRequest, httpServletResponse).get(); | ||
assertEquals(HttpStatus.OK, response.getStatusCode()); | ||
} | ||
|
||
@Test | ||
public void testHandleDeleteRequest() throws Exception { | ||
commonMocking(); | ||
|
||
ResponseEntity<JsonNode> response = apiController.handleDeleteRequest("test", | ||
httpServletRequest, httpServletResponse).get(); | ||
assertEquals(HttpStatus.OK, response.getStatusCode()); | ||
} | ||
|
||
private void commonMocking(){ | ||
ApiResponseResolver apiResponseResolver = new ApiResponseResolver(); | ||
apiResponseResolver.setStatusCode(HttpStatus.OK); | ||
apiResponseResolver.setRespData(objectMapper.createObjectNode()); | ||
ApiRequestResolver apiRequestResolver = new ApiRequestResolver(); | ||
when(commonService.createApiResolverRequest(any(), anyString(), any())).thenReturn(apiRequestResolver); | ||
when(apiService.processApiRequest(apiRequestResolver)).thenReturn(CompletableFuture.completedFuture(apiResponseResolver)); | ||
doNothing().when(utils).setUserIdCookieNeededOrRemove(any(), any(), any()); | ||
doNothing().when(utils).setSessionAndCookieDataForSession(any(), any(), any()); | ||
} | ||
} |
125 changes: 125 additions & 0 deletions
125
src/test/java/sg/edu/nus/iss/shopsmart_backend/controller/ProfileControllerTest.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,125 @@ | ||
package sg.edu.nus.iss.shopsmart_backend.controller; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import io.swagger.v3.core.util.Json; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
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.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import sg.edu.nus.iss.shopsmart_backend.model.ApiRequestResolver; | ||
import sg.edu.nus.iss.shopsmart_backend.model.ApiResponseResolver; | ||
import sg.edu.nus.iss.shopsmart_backend.service.CommonService; | ||
import sg.edu.nus.iss.shopsmart_backend.service.ProfileService; | ||
import sg.edu.nus.iss.shopsmart_backend.utils.Constants; | ||
import sg.edu.nus.iss.shopsmart_backend.utils.Utils; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.doNothing; | ||
import static org.mockito.Mockito.when; | ||
|
||
class ProfileControllerTest extends Constants { | ||
private final ObjectMapper objectMapper = Json.mapper(); | ||
|
||
@Mock | ||
private ProfileService profileService; | ||
@Mock | ||
private CommonService commonService; | ||
@Mock | ||
private Utils utils; | ||
@Mock | ||
private HttpServletResponse httpServletResponse; | ||
@Mock | ||
private HttpServletRequest httpServletRequest; | ||
|
||
@InjectMocks | ||
private ProfileController profileController; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
MockitoAnnotations.openMocks(this); | ||
} | ||
|
||
@Test | ||
public void testGenerateOtpForRegister() throws Exception { | ||
ApiResponseResolver apiResponseResolver = new ApiResponseResolver(); | ||
apiResponseResolver.setStatusCode(HttpStatus.OK); | ||
apiResponseResolver.setRespData(objectMapper.createObjectNode()); | ||
|
||
commonMocking(); | ||
|
||
when(profileService.generateOtpForRegister(any(), anyString())).thenReturn(CompletableFuture.completedFuture(apiResponseResolver)); | ||
ResponseEntity<JsonNode> response = profileController.generateOtpForRegister(CUSTOMER, objectMapper.createObjectNode(), | ||
httpServletRequest, httpServletResponse).get(); | ||
assertEquals(HttpStatus.OK, response.getStatusCode()); | ||
assertEquals(objectMapper.createObjectNode(), response.getBody()); | ||
} | ||
|
||
@Test | ||
public void testVerifyOtpForRegister() throws Exception { | ||
ObjectNode respData = objectMapper.createObjectNode(); | ||
respData.put(USER_ID, "1234"); | ||
ApiResponseResolver apiResponseResolver = new ApiResponseResolver(); | ||
apiResponseResolver.setStatusCode(HttpStatus.OK); | ||
apiResponseResolver.setRespData(respData); | ||
|
||
commonMocking(); | ||
doNothing().when(commonService).updateUserIdInRedisInSessionData(any()); | ||
|
||
when(profileService.validateOtpAndRegister(any(), anyString())).thenReturn(CompletableFuture.completedFuture(apiResponseResolver)); | ||
ResponseEntity<JsonNode> response = profileController.verifyOtpForRegister(CUSTOMER, objectMapper.createObjectNode(), | ||
httpServletRequest, httpServletResponse).get(); | ||
assertEquals(HttpStatus.OK, response.getStatusCode()); | ||
assertEquals(respData, response.getBody()); | ||
} | ||
|
||
@Test | ||
public void testGenerateOtpForLogin() throws Exception { | ||
ApiResponseResolver apiResponseResolver = new ApiResponseResolver(); | ||
apiResponseResolver.setStatusCode(HttpStatus.OK); | ||
apiResponseResolver.setRespData(objectMapper.createObjectNode()); | ||
|
||
commonMocking(); | ||
|
||
when(profileService.generateOtpForLogin(any(), anyString())).thenReturn(CompletableFuture.completedFuture(apiResponseResolver)); | ||
ResponseEntity<JsonNode> response = profileController.generateOtpForLogin(CUSTOMER, objectMapper.createObjectNode(), | ||
httpServletRequest, httpServletResponse).get(); | ||
assertEquals(HttpStatus.OK, response.getStatusCode()); | ||
assertEquals(objectMapper.createObjectNode(), response.getBody()); | ||
} | ||
|
||
@Test | ||
public void testVerifyOtpForLogin() throws Exception { | ||
ObjectNode respData = objectMapper.createObjectNode(); | ||
respData.put(USER_ID, "1234"); | ||
ApiResponseResolver apiResponseResolver = new ApiResponseResolver(); | ||
apiResponseResolver.setStatusCode(HttpStatus.OK); | ||
apiResponseResolver.setRespData(respData); | ||
|
||
commonMocking(); | ||
doNothing().when(commonService).updateUserIdInRedisInSessionData(any()); | ||
|
||
when(profileService.validateOtpAndLogin(any(), anyString())).thenReturn(CompletableFuture.completedFuture(apiResponseResolver)); | ||
ResponseEntity<JsonNode> response = profileController.verifyOtpForLogin(CUSTOMER, objectMapper.createObjectNode(), | ||
httpServletRequest, httpServletResponse).get(); | ||
assertEquals(HttpStatus.OK, response.getStatusCode()); | ||
assertEquals(respData, response.getBody()); | ||
} | ||
|
||
private void commonMocking(){ | ||
ApiRequestResolver apiRequestResolver = new ApiRequestResolver(); | ||
when(commonService.createApiResolverRequest(any(), anyString(), any())).thenReturn(apiRequestResolver); | ||
doNothing().when(utils).setUserIdCookieNeededOrRemove(any(), any(), any()); | ||
doNothing().when(utils).setSessionAndCookieDataForSession(any(), any(), any()); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
src/test/java/sg/edu/nus/iss/shopsmart_backend/controller/RedisControllerTest.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,78 @@ | ||
package sg.edu.nus.iss.shopsmart_backend.controller; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.swagger.v3.core.util.Json; | ||
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.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import sg.edu.nus.iss.shopsmart_backend.model.DataDynamicObject; | ||
import sg.edu.nus.iss.shopsmart_backend.service.RedisService; | ||
|
||
import java.util.HashMap; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.doNothing; | ||
import static org.mockito.Mockito.when; | ||
|
||
class RedisControllerTest { | ||
private final ObjectMapper objectMapper = Json.mapper(); | ||
|
||
@Mock | ||
private RedisService redisService; | ||
|
||
@InjectMocks | ||
private RedisController redisController; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
MockitoAnnotations.openMocks(this); | ||
} | ||
|
||
@Test | ||
public void testInsertDdoDataInRedis() { | ||
DataDynamicObject ddo = new DataDynamicObject(); | ||
when(redisService.insertDdoDataInRedis(anyString(), any(DataDynamicObject.class))).thenReturn("Success"); | ||
ResponseEntity<String> response = redisController.insertDdoDataInRedis("test", ddo); | ||
assertEquals(HttpStatus.OK, response.getStatusCode()); | ||
assertEquals("Success", response.getBody()); | ||
} | ||
|
||
@Test | ||
public void testGetDdoDataFromRedis() { | ||
when(redisService.getDdoDataFromRedis(anyString())).thenReturn(objectMapper.createObjectNode()); | ||
ResponseEntity<JsonNode> response = redisController.getDdoDataFromRedis("test"); | ||
assertEquals(HttpStatus.OK, response.getStatusCode()); | ||
assertEquals(objectMapper.createObjectNode(), response.getBody()); | ||
} | ||
|
||
@Test | ||
public void testInsertHashEntry() { | ||
doNothing().when(redisService).setHashValue(anyString(), anyString(), anyString()); | ||
ResponseEntity<String> response = redisController.insertHashEntry("test", new HashMap<>()); | ||
assertEquals(HttpStatus.OK, response.getStatusCode()); | ||
assertEquals("Success", response.getBody()); | ||
} | ||
|
||
@Test | ||
public void testGetHashEntry() { | ||
when(redisService.getHashValue(anyString(), anyString())).thenReturn("value"); | ||
ResponseEntity<String> response = redisController.getHashEntry("test", "test"); | ||
assertEquals(HttpStatus.OK, response.getStatusCode()); | ||
assertEquals("value", response.getBody()); | ||
} | ||
|
||
@Test | ||
public void testGetHashEntries() { | ||
when(redisService.getHashMap(anyString())).thenReturn(objectMapper.createObjectNode()); | ||
ResponseEntity<JsonNode> response = redisController.getHashEntries("test"); | ||
assertEquals(HttpStatus.OK, response.getStatusCode()); | ||
assertEquals(objectMapper.createObjectNode(), response.getBody()); | ||
} | ||
} |
Oops, something went wrong.