Commit 520cc726 authored by Maxym Borodenko's avatar Maxym Borodenko Committed by Matija Obreza

Return AuditLogs as a Page

parent 15e685c2
......@@ -25,6 +25,7 @@ import javax.validation.Valid;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.apache.commons.lang3.ArrayUtils;
import org.genesys.blocks.auditlog.model.AuditLog;
import org.genesys.blocks.auditlog.model.filters.AuditLogFilter;
import org.genesys.blocks.auditlog.service.AuditTrailService;
import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
......@@ -45,6 +46,8 @@ import org.gringlobal.service.InventoryService;
import org.gringlobal.service.filter.InventoryActionFilter;
import org.gringlobal.service.filter.InventoryFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
......@@ -114,11 +117,18 @@ public class InventoryController extends FilteredCRUDController<Inventory, Inven
* Retrieve a list of audit logs for the specified inventory
*
* @param inventoryId the inventoryId
* @param page the page request
* @return the list of all log entries
*/
@GetMapping("/auditlog/{id}")
public List<WrappedAuditLog> inventoryAuditLogs(@PathVariable(value = "id") final Long inventoryId) {
return auditService.listAuditLogs(crudService.get(inventoryId)).stream().map(WrappedAuditLog::new).collect(Collectors.toList());
public Page<WrappedAuditLog> inventoryAuditLogs(@PathVariable(value = "id") final Long inventoryId, @Parameter(hidden = true) final Pagination page) {
var filter = new AuditLogFilter();
filter.entityId = crudService.get(inventoryId).getId();
filter.classname = Inventory.class.getName();
Page<AuditLog> auditLogs = auditService.listAuditLogs(filter, page.toPageRequest(100));
var content = auditLogs.stream().map(WrappedAuditLog::new).collect(Collectors.toList());
return new PageImpl<>(content, auditLogs.getPageable(), auditLogs.getTotalElements());
}
@Override
......
......@@ -44,8 +44,6 @@ import org.springframework.mock.web.MockMultipartFile;
import org.springframework.security.test.context.support.WithUserDetails;
import org.springframework.transaction.annotation.Transactional;
import com.fasterxml.jackson.core.JsonProcessingException;
/**
* @author Matija Obreza
*/
......@@ -137,7 +135,7 @@ public class InventoryControllerTest extends AbstractApiV1Test {
Accession accession = accessionService.create(a);
assertThat(accession, is(notNullValue()));
Inventory savedInventory = addInventoryToDB(accession, "INV", 99l, null);
Inventory savedInventory = addInventoryToDB(accession, "INV", 99L, null);
assertThat(savedInventory, is(notNullValue()));
assertThat(inventoryRepository.count(), equalTo(2L));
......@@ -185,9 +183,9 @@ public class InventoryControllerTest extends AbstractApiV1Test {
}
@Test
public void testSetQuantity() throws JsonProcessingException, Exception {
public void testSetQuantity() throws Exception {
Accession accession = makeAccession();
Inventory savedInventory = addInventoryToDB(accession, "INV", -1l, null);
Inventory savedInventory = addInventoryToDB(accession, "INV", -1L, null);
assertThat(savedInventory, is(notNullValue()));
assertThat(inventoryRepository.count(), equalTo(2L));
assertThat(savedInventory.getQuantityOnHand(), nullValue());
......@@ -229,30 +227,34 @@ public class InventoryControllerTest extends AbstractApiV1Test {
/*@formatter:off*/
mockMvc
.perform(get(InventoryController.API_URL.concat("/auditlog/{id}"), savedInventory.getId())
.perform(get(InventoryController.API_URL.concat("/auditlog/{id}?s=propertyName"), savedInventory.getId())
.contentType(MediaType.APPLICATION_JSON)
)
// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray())
.andExpect(jsonPath("$.[0]").isMap())
.andExpect(jsonPath("$.[0].createdBy").isString())
.andExpect(jsonPath("$.[0].logDate").exists())
.andExpect(jsonPath("$.[0].classPk").isMap())
.andExpect(jsonPath("$.[0].classPk.classname", is(Inventory.class.getCanonicalName())))
.andExpect(jsonPath("$.[0].entityId", is(savedInventory.getId().intValue())))
.andExpect(jsonPath("$.[0].propertyName", is("inventoryNumberPart1")))
.andExpect(jsonPath("$.[0].previousState", is(numberPart1_V1)))
.andExpect(jsonPath("$.[0].newState", is(numberPart1_V2)))
.andExpect(jsonPath("$.[0].action", is(AuditAction.UPDATE.name())))
.andExpect(jsonPath("$.totalElements", is(1)))
.andExpect(jsonPath("$.sort[0]", not(nullValue())))
.andExpect(jsonPath("$.sort[0].direction", is("ASC")))
.andExpect(jsonPath("$.sort[0].property", is("propertyName")))
.andExpect(jsonPath("$.content").isArray())
.andExpect(jsonPath("$.content[0]").isMap())
.andExpect(jsonPath("$.content[0].createdBy").isString())
.andExpect(jsonPath("$.content[0].logDate").exists())
.andExpect(jsonPath("$.content[0].classPk").isMap())
.andExpect(jsonPath("$.content[0].classPk.classname", is(Inventory.class.getCanonicalName())))
.andExpect(jsonPath("$.content[0].entityId", is(savedInventory.getId().intValue())))
.andExpect(jsonPath("$.content[0].propertyName", is("inventoryNumberPart1")))
.andExpect(jsonPath("$.content[0].previousState", is(numberPart1_V1)))
.andExpect(jsonPath("$.content[0].newState", is(numberPart1_V2)))
.andExpect(jsonPath("$.content[0].action", is(AuditAction.UPDATE.name())))
;
/*@formatter:on*/
}
@Test
public void testInvalidSetQuantity() throws JsonProcessingException, Exception {
public void testInvalidSetQuantity() throws Exception {
Accession accession = makeAccession();
Inventory savedInventory = addInventoryToDB(accession, "INV", -1l, null);
Inventory savedInventory = addInventoryToDB(accession, "INV", -1L, null);
InventoryService.InventoryQuantity quantity = new InventoryService.InventoryQuantity();
quantity.id = savedInventory.getId();
......@@ -276,9 +278,9 @@ public class InventoryControllerTest extends AbstractApiV1Test {
}
@Test
public void testSet100SeedWeight() throws JsonProcessingException, Exception {
public void testSet100SeedWeight() throws Exception {
Accession accession = makeAccession();
Inventory savedInventory = addInventoryToDB(accession, "INV", -1l, null);
Inventory savedInventory = addInventoryToDB(accession, "INV", -1L, null);
assertThat(savedInventory, is(notNullValue()));
assertThat(inventoryRepository.count(), equalTo(2L));
assertThat(savedInventory.getQuantityOnHand(), nullValue());
......
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