Commit ca9cca77 authored by Anastasiia Gelmut's avatar Anastasiia Gelmut
Browse files

GONRG-1607 Fixed AuditLogger for int tests.

parent b865fd75
......@@ -50,7 +50,7 @@ public class PartitionApi {
public ResponseEntity create(@PathVariable("partitionId") String partitionId, @RequestBody @Valid PartitionInfo partitionInfo) {
this.partitionService.createPartition(partitionId, partitionInfo);
URI partitionLocation = ServletUriComponentsBuilder.fromCurrentRequest().buildAndExpand().toUri();
this.auditLogger.createdPartitionSuccess(Collections.singletonList(partitionId));
this.auditLogger.createPartitionSuccess(Collections.singletonList(partitionId));
return ResponseEntity.created(partitionLocation).build();
}
......@@ -59,7 +59,7 @@ public class PartitionApi {
@ResponseStatus(HttpStatus.NO_CONTENT)
public void patch(@PathVariable("partitionId") String partitionId, @RequestBody @Valid PartitionInfo partitionInfo) {
this.partitionService.updatePartition(partitionId, partitionInfo);
this.auditLogger.updatedPartitionSecretSuccess(Collections.singletonList(partitionId));
this.auditLogger.updatePartitionSecretSuccess(Collections.singletonList(partitionId));
}
@GetMapping("/{partitionId}")
......@@ -74,7 +74,7 @@ public class PartitionApi {
@PreAuthorize("@authorizationFilter.hasPermissions()")
public ResponseEntity delete(@PathVariable("partitionId") String partitionId) {
this.partitionService.deletePartition(partitionId);
this.auditLogger.deletedPartitionSuccess(Collections.singletonList(partitionId));
this.auditLogger.deletePartitionSuccess(Collections.singletonList(partitionId));
return ResponseEntity.noContent().build();
}
......
......@@ -12,22 +12,22 @@ import org.opengroup.osdu.core.common.logging.audit.AuditStatus;
public class AuditEvents {
private static final String CREATE_PARTITION_ACTION_ID = "PT001";
private static final String CREATE_PARTITION_MESSAGE = "Partition creation";
private static final String CREATE_PARTITION_MESSAGE = "Create partition";
private static final String READ_PARTITION_ACTION_ID = "PT002";
private static final String READ_PARTITION_MESSAGE = "Partition read";
private static final String READ_PARTITION_MESSAGE = "Read partition";
private static final String DELETE_PARTITION_ACTION_ID = "PT003";
private static final String DELETE_PARTITION_MESSAGE = "Partition removal";
private static final String DELETE_PARTITION_MESSAGE = "Delete partition";
private static final String READ_SERVICE_LIVENESS_ACTION_ID = "PT004";
private static final String READ_SERVICE_LIVENESS_MESSAGE = "Service run";
private static final String UPDATE_PARTITION_ACTION_ID = "PT005";
private static final String UPDATE_PARTITION_MESSAGE = "Partition updating";
private static final String UPDATE_PARTITION_MESSAGE = "Update partition";
private static final String READ_LIST_PARTITION_ACTION_ID = "PT006";
private static final String READ_LIST_PARTITION_MESSAGE = "Partition list read";
private static final String READ_LIST_PARTITION_MESSAGE = "Read partition list";
private final String user;
......
package org.opengroup.osdu.partition.logging;
import com.google.common.base.Strings;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.opengroup.osdu.core.common.entitlements.IEntitlementsFactory;
import org.opengroup.osdu.core.common.entitlements.IEntitlementsService;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.logging.audit.AuditPayload;
import org.opengroup.osdu.core.common.logging.audit.AuditStatus;
import org.opengroup.osdu.core.common.model.entitlements.EntitlementsException;
import org.opengroup.osdu.core.common.model.entitlements.Groups;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.context.annotation.RequestScope;
......@@ -16,22 +23,35 @@ public class AuditLogger {
private final JaxRsDpsLog logger;
private final IEntitlementsFactory factory;
private final DpsHeaders headers;
private AuditEvents events = null;
private AuditEvents getAuditEvents() {
if (this.events == null) {
this.events = new AuditEvents(this.headers.getUserEmail());
if (Strings.isNullOrEmpty(this.headers.getUserEmail())) {
IEntitlementsService service = this.factory.create(headers);
try {
Groups groups = service.getGroups();
this.events = new AuditEvents(groups.getMemberEmail());
} catch (EntitlementsException e) {
throw new AppException(HttpStatus.UNAUTHORIZED.value(), "Authentication Failure",
e.getMessage(), e);
}
} else {
this.events = new AuditEvents(this.headers.getUserEmail());
}
}
return this.events;
}
public void createdPartitionSuccess(List<String> resources) {
public void createPartitionSuccess(List<String> resources) {
writeLog(getAuditEvents().getCreatePartitionEvent(AuditStatus.SUCCESS, resources));
}
public void createdPartitionFailure(List<String> resources) {
public void createPartitionFailure(List<String> resources) {
writeLog(getAuditEvents().getCreatePartitionEvent(AuditStatus.FAILURE, resources));
}
......@@ -43,11 +63,11 @@ public class AuditLogger {
writeLog(getAuditEvents().getReadPartitionEvent(AuditStatus.FAILURE, resources));
}
public void deletedPartitionSuccess(List<String> resources) {
public void deletePartitionSuccess(List<String> resources) {
writeLog(getAuditEvents().getDeletePartitionEvent(AuditStatus.SUCCESS, resources));
}
public void deletedPartitionFailure(List<String> resources) {
public void deletePartitionFailure(List<String> resources) {
writeLog(getAuditEvents().getDeletePartitionEvent(AuditStatus.FAILURE, resources));
}
......@@ -59,11 +79,11 @@ public class AuditLogger {
writeLog(getAuditEvents().getReadServiceLivenessEvent(AuditStatus.FAILURE, resources));
}
public void updatedPartitionSecretSuccess(List<String> resources) {
public void updatePartitionSecretSuccess(List<String> resources) {
writeLog(getAuditEvents().getUpdatePartitionSecretEvent(AuditStatus.SUCCESS, resources));
}
public void updatedPartitionSecretFailure(List<String> resources) {
public void updatePartitionSecretFailure(List<String> resources) {
writeLog(getAuditEvents().getUpdatePartitionSecretEvent(AuditStatus.FAILURE, resources));
}
......
package org.opengroup.osdu.partition.logging;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Collections;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.entitlements.IEntitlementsFactory;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
@RunWith(MockitoJUnitRunner.class)
public class AuditLoggerTest {
@Mock
private JaxRsDpsLog log;
@Mock
private IEntitlementsFactory factory;
@Mock
private DpsHeaders headers;
@InjectMocks
private AuditLogger sut;
private List<String> resources;
@Before
public void setup() {
when(this.headers.getUserEmail()).thenReturn("test_user@email.com");
resources = Collections.singletonList("resources");
}
@Test
public void should_writeCreatePartitionSuccessEvent() {
this.sut.createPartitionSuccess(this.resources);
verify(this.log, times(1)).audit(any());
}
@Test
public void should_writeCreatePartitionFailureEvent() {
this.sut.createPartitionFailure(this.resources);
verify(this.log, times(1)).audit(any());
}
@Test
public void should_writeReadPartitionSuccessEvent() {
this.sut.readPartitionSuccess(this.resources);
verify(this.log, times(1)).audit(any());
}
@Test
public void should_writeReadPartitionFailureEvent() {
this.sut.readPartitionFailure(this.resources);
verify(this.log, times(1)).audit(any());
}
@Test
public void should_writeDeletePartitionSuccessEvent() {
this.sut.deletePartitionSuccess(this.resources);
verify(this.log, times(1)).audit(any());
}
@Test
public void should_writeDeletePartitionFailureEvent() {
this.sut.deletePartitionFailure(this.resources);
verify(this.log, times(1)).audit(any());
}
@Test
public void should_writeReadServiceLivenessSuccessEvent() {
this.sut.readServiceLivenessSuccess(this.resources);
verify(this.log, times(1)).audit(any());
}
@Test
public void should_writeReadServiceLivenessFailureEvent() {
this.sut.readServiceLivenessFailure(this.resources);
verify(this.log, times(1)).audit(any());
}
@Test
public void should_writeUpdatePartitionSecretSuccessEvent() {
this.sut.updatePartitionSecretSuccess(this.resources);
verify(this.log, times(1)).audit(any());
}
@Test
public void should_writeUpdatePartitionSecretFailureEvent() {
this.sut.updatePartitionSecretFailure(this.resources);
verify(this.log, times(1)).audit(any());
}
@Test
public void should_writeReadListPartitionSuccessEvent() {
this.sut.readListPartitionSuccess(this.resources);
verify(this.log, times(1)).audit(any());
}
@Test
public void should_writeReadListPartitionFailureEvent() {
this.sut.readListPartitionFailure(this.resources);
verify(this.log, times(1)).audit(any());
}
}
\ No newline at end of file
......@@ -45,9 +45,6 @@ public class AuthorizationService implements IAuthorizationService {
try {
AuthorizationResponse authorizationResponse = authorizationServiceImpl
.authorizeAny(headers, PARTITION_ADMIN_ROLE);
if (Objects.nonNull(authorizationResponse)) {
headers.put("user", authorizationResponse.getUser());
}
} catch (AppException e) {
throw e;
} catch (Exception e) {
......
......@@ -53,7 +53,7 @@ public class PartitionServiceImpl implements IPartitionService {
@Override
public PartitionInfo createPartition(String partitionId, PartitionInfo partitionInfo) {
if (this.partitionPropertyEntityRepository.findByPartitionId(partitionId).isPresent()) {
this.auditLogger.createdPartitionFailure(Collections.singletonList(partitionId));
this.auditLogger.createPartitionFailure(Collections.singletonList(partitionId));
throw new AppException(HttpStatus.SC_CONFLICT, UNKNOWN_ERROR_REASON,
"Partition already exists.");
}
......@@ -86,13 +86,13 @@ public class PartitionServiceImpl implements IPartitionService {
@Override
public PartitionInfo updatePartition(String partitionId, PartitionInfo partitionInfo) {
if (partitionInfo.getProperties().containsKey("id")) {
this.auditLogger.updatedPartitionSecretFailure(Collections.singletonList(partitionId));
this.auditLogger.updatePartitionSecretFailure(Collections.singletonList(partitionId));
throw new AppException(HttpStatus.SC_BAD_REQUEST, "can not update id",
"the field id can not be updated");
}
if (!this.partitionPropertyEntityRepository.findByPartitionId(partitionId).isPresent()) {
this.auditLogger.updatedPartitionSecretFailure(Collections.singletonList(partitionId));
this.auditLogger.updatePartitionSecretFailure(Collections.singletonList(partitionId));
throw new AppException(HttpStatus.SC_NOT_FOUND, UNKNOWN_ERROR_REASON,
"An attempt to update not existing partition.");
}
......@@ -161,7 +161,7 @@ public class PartitionServiceImpl implements IPartitionService {
@Override
public boolean deletePartition(String partitionId) {
if (!this.partitionPropertyEntityRepository.findByPartitionId(partitionId).isPresent()) {
this.auditLogger.deletedPartitionFailure(Collections.singletonList(partitionId));
this.auditLogger.deletePartitionFailure(Collections.singletonList(partitionId));
throw new AppException(HttpStatus.SC_NOT_FOUND, UNKNOWN_ERROR_REASON,
"An attempt to delete not existing partition.");
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment