Skip to content

Commit

Permalink
MOSIP-38092, MOSIP-37854 : added Junit (#1030)
Browse files Browse the repository at this point in the history
* MOSIP-38092, MOSIP-37854 : added Junit

Signed-off-by: sudeep <[email protected]>

* MOSIP-38092, MOSIP-37854 : added Junit

Signed-off-by: sudeep <[email protected]>

---------

Signed-off-by: sudeep <[email protected]>
  • Loading branch information
Sudeep7353 authored Dec 9, 2024
1 parent c33eae6 commit f40eaee
Show file tree
Hide file tree
Showing 5 changed files with 327 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package io.mosip.pms.test.device.controller;

import static org.mockito.ArgumentMatchers.any;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.time.LocalDateTime;
Expand All @@ -13,9 +15,13 @@

import io.mosip.pms.common.request.dto.RequestWrapperV2;
import io.mosip.pms.common.response.dto.ResponseWrapperV2;
import io.mosip.pms.common.dto.PageResponseV2Dto;
import io.mosip.pms.device.dto.DeviceDetailFilterDto;
import io.mosip.pms.device.dto.DeviceDetailSummaryDto;
import io.mosip.pms.device.request.dto.DeactivateDeviceRequestDto;
import io.mosip.pms.partner.request.dto.SbiAndDeviceMappingRequestDto;
import io.mosip.pms.device.response.dto.DeviceDetailResponseDto;
import io.mosip.pms.partner.util.PartnerHelper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -30,6 +36,7 @@
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

Expand Down Expand Up @@ -78,12 +85,15 @@ public class DeviceDetailControllerTest {
private RestTemplate restTemplate;

@MockBean
private DeviceDetailServiceImpl deviceDetaillService;
private DeviceDetailServiceImpl deviceDetaillService;

@MockBean
PartnerHelper partnerHelper;

@Before
public void setup() {
Mockito.doNothing().when(auditUtil).auditRequest(any(), any(), any());
Mockito.doNothing().when(auditUtil).auditRequest(any(), any(), any(),any());
doNothing().when(auditUtil).auditRequest(any(), any(), any());
doNothing().when(auditUtil).auditRequest(any(), any(), any(),any());

IdDto response = new IdDto();
String stringResponse = new String();
Expand All @@ -100,14 +110,14 @@ public void setup() {
searchResponseWrapper.setResponse(searchResponse);
responseWrapper.setResponse(response);
responsewrapper.setResponse(stringResponse);
Mockito.when(deviceDetaillService.updateDeviceDetails(Mockito.any())).thenReturn(response);
Mockito.when(deviceDetaillService.createDeviceDetails(Mockito.any())).thenReturn(response);
Mockito.when(deviceDetaillService.updateDeviceDetailStatus(Mockito.any())).thenReturn(stringResponse);
Mockito.when(deviceDetaillService.searchDeviceDetails(Mockito.any(), Mockito.any())).thenReturn(searchResponse);
Mockito.when(deviceDetaillService.searchDeviceType(Mockito.any(), Mockito.any())).thenReturn(searchTypeResponse);
Mockito.when(deviceDetaillService.deviceFilterValues(Mockito.any())).thenReturn(filterResponse);
Mockito.when(deviceDetaillService.deviceTypeFilterValues(Mockito.any())).thenReturn(filterResponse);
Mockito.when(deviceDetaillService.deviceSubTypeFilterValues(Mockito.any())).thenReturn(filterResponse);
when(deviceDetaillService.updateDeviceDetails(Mockito.any())).thenReturn(response);
when(deviceDetaillService.createDeviceDetails(Mockito.any())).thenReturn(response);
when(deviceDetaillService.updateDeviceDetailStatus(Mockito.any())).thenReturn(stringResponse);
when(deviceDetaillService.searchDeviceDetails(Mockito.any(), Mockito.any())).thenReturn(searchResponse);
when(deviceDetaillService.searchDeviceType(Mockito.any(), Mockito.any())).thenReturn(searchTypeResponse);
when(deviceDetaillService.deviceFilterValues(Mockito.any())).thenReturn(filterResponse);
when(deviceDetaillService.deviceTypeFilterValues(Mockito.any())).thenReturn(filterResponse);
when(deviceDetaillService.deviceSubTypeFilterValues(Mockito.any())).thenReturn(filterResponse);
}

@WithMockUser(roles = {"PARTNER_ADMIN"})
Expand Down Expand Up @@ -455,7 +465,7 @@ public void inactiveMappingDeviceToSbi() throws Exception {
requestWrapper.setRequestTime(LocalDateTime.now());
SbiAndDeviceMappingRequestDto sbiAndDeviceMappingRequestDto = new SbiAndDeviceMappingRequestDto();
requestWrapper.setRequest(sbiAndDeviceMappingRequestDto);
Mockito.when(deviceDetaillService.inactiveMappingDeviceToSbi(requestWrapper.getRequest())).thenReturn(responseWrapper);
when(deviceDetaillService.inactiveMappingDeviceToSbi(requestWrapper.getRequest())).thenReturn(responseWrapper);
mockMvc.perform(MockMvcRequestBuilders.post("/devicedetail/inactive-mapping-device-to-sbi").contentType(MediaType.APPLICATION_JSON_VALUE)
.content(objectMapper.writeValueAsString(requestWrapper))).andExpect(status().isOk());
}
Expand All @@ -472,9 +482,51 @@ public void deactivateDeviceTest() throws Exception {
requestWrapper.setRequest(deactivateDeviceRequestDto);
DeviceDetailResponseDto deviceDetailResponseDto = new DeviceDetailResponseDto();
responseWrapper.setResponse(deviceDetailResponseDto);
Mockito.when(deviceDetaillService.deactivateDevice(Mockito.any())).thenReturn(responseWrapper);
when(deviceDetaillService.deactivateDevice(Mockito.any())).thenReturn(responseWrapper);
mockMvc.perform(MockMvcRequestBuilders.post("/devicedetail/deactivate-device").contentType(MediaType.APPLICATION_JSON_VALUE)
.content(objectMapper.writeValueAsString(requestWrapper))).andExpect(status().isOk());
}

@Test
@WithMockUser(roles = {"PARTNER_ADMIN"})
public void getAllDeviceDetailsTest() throws Exception {
String sortFieldName = "deviceId";
String sortType = "asc";
int pageNo = 0;
int pageSize = 8;
String partnerId = "test-partner";
String orgName = "test-org";
String deviceType = "Finger";
String deviceSubType = "single";
String status = "approved";
String make = "test-make";
String model = "test-model";
String sbiId = "test-sbi-id";
String sbiVersion = "1.0";
String deviceId = "test-device-id";

ResponseWrapperV2<PageResponseV2Dto<DeviceDetailSummaryDto>> mockResponse = new ResponseWrapperV2<>();
when(deviceDetaillService.getAllDeviceDetails(eq(sortFieldName), eq(sortType), eq(pageNo), eq(pageSize), any(DeviceDetailFilterDto.class)))
.thenReturn(mockResponse);

doNothing().when(partnerHelper).validateRequestParameters(any(), eq(sortFieldName), eq(sortType), eq(pageNo), eq(pageSize));

mockMvc.perform(MockMvcRequestBuilders.get("/devicedetail/search/v2")
.param("sortFieldName", sortFieldName)
.param("sortType", sortType)
.param("pageNo", String.valueOf(pageNo))
.param("pageSize", String.valueOf(pageSize))
.param("partnerId", partnerId)
.param("orgName", orgName)
.param("deviceType", deviceType)
.param("deviceSubType", deviceSubType)
.param("status", status)
.param("make", make)
.param("model", model)
.param("sbiId", sbiId)
.param("sbiVersion", sbiVersion)
.param("deviceId", deviceId))
.andExpect(MockMvcResultMatchers.status().isOk());
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package io.mosip.pms.test.device.service.impl;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.time.LocalDateTime;
Expand All @@ -14,9 +15,12 @@
import io.mosip.kernel.openid.bridge.model.AuthUserDetails;
import io.mosip.pms.common.entity.DeviceDetailSBI;
import io.mosip.pms.common.repository.DeviceDetailSbiRepository;
import io.mosip.pms.common.response.dto.ResponseWrapperV2;
import io.mosip.pms.device.authdevice.entity.SecureBiometricInterface;
import io.mosip.pms.device.authdevice.repository.DeviceDetailSummaryRepository;
import io.mosip.pms.device.authdevice.repository.SecureBiometricInterfaceRepository;
import io.mosip.pms.partner.request.dto.SbiAndDeviceMappingRequestDto;
import io.mosip.pms.partner.util.PartnerHelper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -26,7 +30,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.*;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
Expand All @@ -43,6 +47,7 @@
import io.mosip.pms.common.dto.FilterData;
import io.mosip.pms.common.dto.FilterDto;
import io.mosip.pms.common.dto.Pagination;
import io.mosip.pms.common.dto.PageResponseV2Dto;
import io.mosip.pms.common.dto.SearchFilter;
import io.mosip.pms.common.dto.SearchSort;
import io.mosip.pms.common.entity.Partner;
Expand All @@ -53,11 +58,14 @@
import io.mosip.pms.common.util.PageUtils;
import io.mosip.pms.common.validator.FilterColumnValidator;
import io.mosip.pms.device.authdevice.entity.DeviceDetail;
import io.mosip.pms.device.authdevice.entity.DeviceDetailEntity;
import io.mosip.pms.device.authdevice.entity.RegistrationDeviceSubType;
import io.mosip.pms.device.authdevice.repository.DeviceDetailRepository;
import io.mosip.pms.device.authdevice.repository.RegistrationDeviceSubTypeRepository;
import io.mosip.pms.device.authdevice.service.DeviceDetailService;
import io.mosip.pms.device.authdevice.service.impl.DeviceDetailServiceImpl;
import io.mosip.pms.device.dto.DeviceDetailFilterDto;
import io.mosip.pms.device.dto.DeviceDetailSummaryDto;
import io.mosip.pms.device.request.dto.DeviceDetailDto;
import io.mosip.pms.device.request.dto.DeviceDetailUpdateDto;
import io.mosip.pms.device.request.dto.DeviceSearchDto;
Expand Down Expand Up @@ -115,6 +123,12 @@ public class DeviceDetailServiceTest {
@Mock
SecurityContext securityContext;

@Mock
DeviceDetailSummaryRepository deviceDetailSummaryRepository;

@Mock
PartnerHelper partnerHelper;

private RequestWrapper<DeviceSearchDto> deviceRequestDto;

DeviceDetail deviceDetail = new DeviceDetail();
Expand Down Expand Up @@ -579,6 +593,78 @@ public void deactivateDeviceTestException3() throws Exception {
deviceDetaillService.deactivateDevice("23456");
}

@Test
public void getAllDeviceDetailsStatusAscSorting() {
String sortFieldName = "status";
String sortType = "ASC";
int pageNo = 0;
int pageSize = 10;
DeviceDetailFilterDto filterDto = new DeviceDetailFilterDto();

Pageable pageable = PageRequest.of(pageNo, pageSize);
DeviceDetailEntity entity = new DeviceDetailEntity();
entity.setDeviceId("123");
entity.setStatus("approved");
Page<DeviceDetailEntity> page = new PageImpl<>(List.of(entity), pageable, 1);

when(deviceDetailSummaryRepository.getSummaryOfAllDeviceDetailsByStatusAsc(any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), eq(pageable)))
.thenReturn(page);

ResponseWrapperV2<PageResponseV2Dto<DeviceDetailSummaryDto>> response = deviceDetaillService.getAllDeviceDetails(sortFieldName, sortType, pageNo, pageSize, filterDto);

// Assert
assertNotNull(response);
}

@Test
public void testGetAllDeviceDetails_withStatusDescSorting() {
String sortFieldName = "status";
String sortType = "DESC";
int pageNo = 0;
int pageSize = 10;
DeviceDetailFilterDto filterDto = new DeviceDetailFilterDto();

Pageable pageable = PageRequest.of(pageNo, pageSize);
DeviceDetailEntity entity = new DeviceDetailEntity();
entity.setDeviceId("123");
entity.setStatus("approved");
Page<DeviceDetailEntity> page = new PageImpl<>(List.of(entity), pageable, 1);

when(deviceDetailSummaryRepository.getSummaryOfAllDeviceDetailsByStatusDesc(any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), eq(pageable)))
.thenReturn(page);

ResponseWrapperV2<PageResponseV2Dto<DeviceDetailSummaryDto>> response = deviceDetaillService.getAllDeviceDetails(sortFieldName, sortType, pageNo, pageSize, filterDto);

// Assert
assertNotNull(response);
}

@Test
public void testGetAllDeviceDetails_withOtherFieldSorting() {
String sortFieldName = "deviceId";
String sortType = "ASC";
int pageNo = 0;
int pageSize = 10;
DeviceDetailFilterDto filterDto = new DeviceDetailFilterDto();

Pageable pageable = PageRequest.of(pageNo, pageSize);
DeviceDetailEntity entity = new DeviceDetailEntity();
entity.setDeviceId("123");
entity.setStatus("approved");
Page<DeviceDetailEntity> page = new PageImpl<>(List.of(entity), pageable, 1);

when(deviceDetailSummaryRepository.getSummaryOfAllDeviceDetails(any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), eq(pageable)))
.thenReturn(page);

Sort sort = mock(Sort.class);
when(partnerHelper.getSortingRequest(any(), eq(sortType))).thenReturn(sort);

ResponseWrapperV2<PageResponseV2Dto<DeviceDetailSummaryDto>> response = deviceDetaillService.getAllDeviceDetails(sortFieldName, sortType, pageNo, pageSize, filterDto);

// Assert
assertNotNull(response);
}

private io.mosip.kernel.openid.bridge.model.MosipUserDto getMosipUserDto() {
io.mosip.kernel.openid.bridge.model.MosipUserDto mosipUserDto = new io.mosip.kernel.openid.bridge.model.MosipUserDto();
mosipUserDto.setUserId("123");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -830,4 +830,14 @@ public void deactivatePolicyTest() throws Exception {
.thenReturn(responseWrapper);
policyManagementController.deactivatePolicy("policy123");
}

@Test
public void deactivatePolicyGroupTest() throws Exception {
String policyGroupId = "test-policy-group-id";
ResponseWrapperV2<DeactivatePolicyGroupResponseDto> responseWrapper = new ResponseWrapperV2<>();
when(policyManagementService.deactivatePolicyGroup(policyGroupId)).thenReturn(responseWrapper);

policyManagementController.deactivatePolicyGroup(policyGroupId);
}

}
Loading

0 comments on commit f40eaee

Please sign in to comment.