Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
App Blocks
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
Genesys PGR
App Blocks
Commits
6dc323df
Commit
6dc323df
authored
Feb 17, 2019
by
Matija Obreza
1
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Audit logs: ignore order of referenced entity IDs
parent
83ed6a53
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
175 additions
and
10 deletions
+175
-10
auditlog/src/main/java/org/genesys/blocks/auditlog/component/AuditTrailInterceptor.java
...esys/blocks/auditlog/component/AuditTrailInterceptor.java
+9
-1
auditlog/src/test/java/org/genesys/blocks/auditlog/model/ExampleAuditedEntity.java
...g/genesys/blocks/auditlog/model/ExampleAuditedEntity.java
+24
-1
auditlog/src/test/java/org/genesys/blocks/auditlog/model/OtherEntity.java
...t/java/org/genesys/blocks/auditlog/model/OtherEntity.java
+47
-0
auditlog/src/test/java/org/genesys/blocks/auditlog/persistence/OtherEntityRepository.java
...ys/blocks/auditlog/persistence/OtherEntityRepository.java
+30
-0
auditlog/src/test/java/org/genesys/blocks/auditlog/service/AuditTrailServiceTest.java
...enesys/blocks/auditlog/service/AuditTrailServiceTest.java
+65
-8
No files found.
auditlog/src/main/java/org/genesys/blocks/auditlog/component/AuditTrailInterceptor.java
View file @
6dc323df
...
...
@@ -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
;
}
...
...
auditlog/src/test/java/org/genesys/blocks/auditlog/model/ExampleAuditedEntity.java
View file @
6dc323df
/*
* Copyright 201
8
Global Crop Diversity Trust
* Copyright 201
9
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
;
}
}
auditlog/src/test/java/org/genesys/blocks/auditlog/model/OtherEntity.java
0 → 100644
View file @
6dc323df
/*
* 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
;
}
}
auditlog/src/test/java/org/genesys/blocks/auditlog/persistence/OtherEntityRepository.java
0 → 100644
View file @
6dc323df
/*
* 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
);
}
auditlog/src/test/java/org/genesys/blocks/auditlog/service/AuditTrailServiceTest.java
View file @
6dc323df
...
...
@@ -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
()
})));
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment