Skip to content

Commit

Permalink
Fixed audit failure in resident (mosip#718)
Browse files Browse the repository at this point in the history
* Fixed service history issue.

* Fixed upload document issue.

* Fixed upload document issue.

* Fixed upload document issue.

Signed-off-by: kameshsr <[email protected]>
  • Loading branch information
kameshsr committed Oct 17, 2023
1 parent 7fc6287 commit d2d4e05
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@

import javax.annotation.PostConstruct;

import io.mosip.resident.constant.ResidentConstants;
import io.mosip.resident.exception.ApisResourceAccessException;
import io.mosip.resident.service.impl.IdentityServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -46,6 +50,12 @@ public class AuditUtil {

@Autowired
private ObjectMapper objectMapper;

@Autowired
private IdentityServiceImpl identityService;

@Autowired
private Environment environment;


/** The Constant UNKNOWN_HOST. */
Expand Down Expand Up @@ -90,7 +100,13 @@ public void setAuditRequestDto(EventEnum eventEnum) {
auditRequestDto.setApplicationName(eventEnum.getApplicationName());
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if(authentication != null) {
String name = authentication.getName();
String name = null;
try {
name = identityService.getAvailableclaimValue(
this.environment.getProperty(ResidentConstants.NAME_FROM_PROFILE));
} catch (ApisResourceAccessException e) {
throw new RuntimeException(e);
}
auditRequestDto.setSessionUserId(name);
auditRequestDto.setSessionUserName(name);
auditRequestDto.setCreatedBy(name);
Expand All @@ -104,6 +120,7 @@ public void setAuditRequestDto(EventEnum eventEnum) {
auditRequestDto.setEventId(eventEnum.getEventId());
auditRequestDto.setId(eventEnum.getId());
auditRequestDto.setIdType(eventEnum.getIdType());
auditRequestDto.setCreatedBy(ResidentConstants.RESIDENT);
callAuditManager(auditRequestDto);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,23 @@
import io.mosip.kernel.core.http.ResponseWrapper;
import io.mosip.kernel.core.util.DateUtils;
import io.mosip.resident.dto.AuditRequestDTO;
import io.mosip.resident.service.impl.IdentityServiceImpl;
import io.mosip.resident.util.AuditResponseDto;
import io.mosip.resident.util.AuditUtil;
import io.mosip.resident.util.EventEnum;
import io.mosip.resident.util.TokenGenerator;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.*;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
Expand Down Expand Up @@ -52,6 +57,12 @@ public class AuditUtilTest {
@Mock
private ObjectMapper objectMapper;

@Mock
private Environment environment;

@Mock
private IdentityServiceImpl identityService;

@Captor
ArgumentCaptor<HttpEntity> httpEntityCaptor;

Expand Down Expand Up @@ -80,7 +91,8 @@ public void setUp() throws Exception {

localDateTime = DateUtils.getUTCCurrentDateTime();
when(DateUtils.getUTCCurrentDateTime()).thenReturn(localDateTime);

when(identityService.getAvailableclaimValue(Mockito.anyString())).thenReturn("user1");
when(environment.getProperty(Mockito.anyString())).thenReturn("user1");
}

@Test
Expand Down Expand Up @@ -122,7 +134,7 @@ public void setAuditRequestDtoTest() throws Exception {

assertEquals("user1", httpEntity.getBody().getRequest().getSessionUserId());
assertEquals("user1", httpEntity.getBody().getRequest().getSessionUserName());
assertEquals("user1", httpEntity.getBody().getRequest().getCreatedBy());
assertEquals("RESIDENT", httpEntity.getBody().getRequest().getCreatedBy());

assertEquals(localDateTime, httpEntity.getBody().getRequest().getActionTimeStamp());

Expand Down

0 comments on commit d2d4e05

Please sign in to comment.