Skip to content

Commit

Permalink
Added unit testcases for code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkmati committed Nov 15, 2024
1 parent 91e0ea9 commit a1bc5be
Show file tree
Hide file tree
Showing 9 changed files with 1,028 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docker-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ jobs:
exit 1
fi
if (( $(echo "$COVERAGE_PERCENTAGE < 0" | bc -l) )); then
if (( $(echo "$COVERAGE_PERCENTAGE < 80" | bc -l) )); then
echo "Code coverage is below 80%, build failed."
exit 1
fi
Expand Down
13 changes: 9 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
</scm>
<properties>
<java.version>21</java.version>
<sonar.coverage.exclusions>**/sg/edu/nus/iss/shopsmart_backend/ShopsmartBackendApplication.java,
**/sg/edu/nus/iss/shopsmart_backend/config/**,
**/sg/edu/nus/iss/shopsmart_backend/exception/**,
**/sg/edu/nus/iss/shopsmart_backend/model/**,
**/sg/edu/nus/iss/shopsmart_backend/service/CommonService.java,
**/sg/edu/nus/iss/shopsmart_backend/utils/**</sonar.coverage.exclusions>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -129,10 +135,9 @@
<exclude>sg/edu/nus/iss/shopsmart_backend/ShopsmartBackendApplication.class</exclude>
<!-- Exclude all classes in the model package from code coverage -->
<exclude>sg/edu/nus/iss/shopsmart_backend/config/**</exclude>
<exclude>sg/edu/nus/iss/shopsmart_backend/controller/**</exclude>
<exclude>sg/edu/nus/iss/shopsmart_backend/exception/**</exclude>
<exclude>sg/edu/nus/iss/shopsmart_backend/model/**</exclude>
<exclude>sg/edu/nus/iss/shopsmart_backend/redis/**</exclude>
<exclude>sg/edu/nus/iss/shopsmart_backend/service/**</exclude>
<exclude>sg/edu/nus/iss/shopsmart_backend/service/CommonService.class</exclude>
<exclude>sg/edu/nus/iss/shopsmart_backend/util/**</exclude>
</excludes>
</configuration>
Expand Down Expand Up @@ -162,7 +167,7 @@
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0%</minimum>
<minimum>80%</minimum>
</limit>
</limits>
</rule>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public CompletableFuture<ApiResponseResolver> processApiRequest(ApiRequestResolv
});
}

private JsonNode addCommonFieldsToRequest(ApiRequestResolver apiRequestResolver){
public JsonNode addCommonFieldsToRequest(ApiRequestResolver apiRequestResolver){
if(apiRequestResolver.getRequestBody() != null && !apiRequestResolver.getRequestBody().isNull()
&& !apiRequestResolver.getRequestBody().isEmpty()){
ObjectNode requestBody = (ObjectNode) apiRequestResolver.getRequestBody();
Expand Down
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());
}
}
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());
}
}
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());
}
}
Loading

0 comments on commit a1bc5be

Please sign in to comment.