Commit 6dc323df authored by Matija Obreza's avatar Matija Obreza

Audit logs: ignore order of referenced entity IDs

parent 83ed6a53
......@@ -26,6 +26,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
......@@ -554,7 +555,7 @@ public class AuditTrailInterceptor extends EmptyInterceptor implements Initializ
* @return the collection
*/
private Collection<Object> convertEntityId(final Collection<?> previous) {
final Collection<Object> converted = new ArrayList<>();
final List<Object> converted = new ArrayList<>();
for (final Object p : previous) {
if (p instanceof EntityId) {
converted.add(((EntityId) p).getId());
......@@ -562,6 +563,13 @@ public class AuditTrailInterceptor extends EmptyInterceptor implements Initializ
converted.add(p);
}
}
converted.sort((a, b) -> {
if (a instanceof Long && b instanceof Long) {
return Long.compare((Long) a, (Long) b);
} else {
return Integer.compare(a.hashCode(), b.hashCode());
}
});
return converted;
}
......
/*
* Copyright 2018 Global Crop Diversity Trust
* Copyright 2019 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -19,12 +19,14 @@ package org.genesys.blocks.auditlog.model;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
......@@ -56,6 +58,9 @@ public class ExampleAuditedEntity extends BasicModel {
@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(name = "entity_set", joinColumns = @JoinColumn(name = "entityId", referencedColumnName = "id"))
private Set<String> set;
@ManyToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
private List<OtherEntity> others;
/**
* Gets the name.
......@@ -128,4 +133,22 @@ public class ExampleAuditedEntity extends BasicModel {
public void setSet(Set<String> set) {
this.set = set;
}
/**
* Gets the others.
*
* @return the others
*/
public List<OtherEntity> getOthers() {
return others;
}
/**
* Sets the others.
*
* @param others the new others
*/
public void setOthers(List<OtherEntity> others) {
this.others = others;
}
}
/*
* Copyright 2019 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.blocks.auditlog.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import org.genesys.blocks.model.BasicModel;
/**
* The Class OtherEntity.
*/
@Entity
public class OtherEntity extends BasicModel {
private static final long serialVersionUID = 1L;
@Column(unique = true)
private String name;
public OtherEntity() {
}
public OtherEntity(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
/*
* Copyright 2019 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.blocks.auditlog.persistence;
import org.genesys.blocks.auditlog.model.OtherEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
* The OtherEntityRepository.
*
* @author Matija Obreza
*/
@Repository
public interface OtherEntityRepository extends JpaRepository<OtherEntity, Long> {
OtherEntity findByName(String name);
}
......@@ -15,28 +15,27 @@
*/
package org.genesys.blocks.auditlog.service;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
import java.util.Arrays;
import java.util.List;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.genesys.blocks.auditlog.model.AuditAction;
import org.genesys.blocks.auditlog.model.AuditLog;
import org.genesys.blocks.auditlog.model.ExampleAuditedEntity;
import org.genesys.blocks.auditlog.model.OtherEntity;
import org.genesys.blocks.auditlog.persistence.ExampleAuditedEntityRepository;
import org.genesys.blocks.auditlog.persistence.OtherEntityRepository;
import org.genesys.blocks.auditlog.test.ServiceTest;
import org.genesys.blocks.model.ClassPK;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
/**
* The Class AuditTrailServiceTest.
*/
......@@ -54,9 +53,13 @@ public class AuditTrailServiceTest extends ServiceTest {
@Autowired
private ExampleAuditedEntityRepository exampleAuditedEntityRepository;
@Autowired
private OtherEntityRepository otherEntityRepository;
@Override
public void cleanup() {
exampleAuditedEntityRepository.deleteAll();
otherEntityRepository.deleteAll();
auditLogRepository.deleteAll();
}
......@@ -279,4 +282,58 @@ public class AuditTrailServiceTest extends ServiceTest {
}
/**
* Test referenced entity audit log.
*/
@Test
public void testReferencedEntityListAuditLog() {
OtherEntity other1 = new OtherEntity("Other1");
other1 = otherEntityRepository.save(other1);
OtherEntity other2 = new OtherEntity("Other2");
other2 = otherEntityRepository.save(other2);
ExampleAuditedEntity entity = new ExampleAuditedEntity();
entity.setName("Test 1");
entity = exampleAuditedEntityService.save(entity);
assertThat(listAuditLogs(entity), hasSize(0));
entity.setOthers(Arrays.asList(other1));
entity = exampleAuditedEntityService.save(entity);
assertThat(listAuditLogs(entity), hasSize(1));
assertThat(listAuditLogs(entity).get(0).getPreviousEntity(), is(nullValue()));
assertThat(listAuditLogs(entity).get(0).getNewEntity(), is(nullValue()));
assertThat(listAuditLogs(entity).get(0).getNewState(), is(Arrays.toString(new Long[] { other1.getId() })));
entity.getOthers().clear();
entity.getOthers().add(other1);
entity.getOthers().add(other2);
entity = exampleAuditedEntityService.save(entity);
assertThat(listAuditLogs(entity), hasSize(2));
assertThat(listAuditLogs(entity).get(0).getNewState(), is(Arrays.toString(new Long[] { other1.getId(), other2.getId() })));
entity.getOthers().clear();
entity.getOthers().add(other2);
entity.getOthers().add(other1);
entity = exampleAuditedEntityService.save(entity);
assertThat(listAuditLogs(entity), hasSize(2));
assertThat(listAuditLogs(entity).get(0).getNewState(), is(Arrays.toString(new Long[] { other1.getId(), other2.getId() })));
entity.getOthers().clear();
entity.getOthers().add(other1);
entity.getOthers().add(other2);
entity = exampleAuditedEntityService.save(entity);
assertThat(listAuditLogs(entity), hasSize(2));
assertThat(listAuditLogs(entity).get(0).getNewState(), is(Arrays.toString(new Long[] { other1.getId(), other2.getId() })));
entity.getOthers().clear();
entity.getOthers().add(other2);
entity = exampleAuditedEntityService.save(entity);
// printAuditLogs(entity);
// System.out.println("\n\n\n");
assertThat(listAuditLogs(entity), hasSize(3));
assertThat(listAuditLogs(entity).get(0).getNewState(), is(Arrays.toString(new Long[] { other2.getId() })));
}
}
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