Skip to content

Commit

Permalink
Fixed checking for fields that are new in the post document and got a…
Browse files Browse the repository at this point in the history
…udit hook to the point that we *could* do something with the data.. it doesn't do anything still
  • Loading branch information
jewzaam committed Aug 11, 2014
1 parent 7146b99 commit a67a6e8
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 7 deletions.
39 changes: 35 additions & 4 deletions src/main/java/com/redhat/lightblue/hook/audit/AuditHook.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.redhat.lightblue.hook.audit;

import com.fasterxml.jackson.databind.JsonNode;
import com.redhat.lightblue.config.LightblueFactory;
import com.redhat.lightblue.crud.Operation;
import com.redhat.lightblue.hooks.CRUDHook;
import com.redhat.lightblue.hooks.HookDoc;
Expand Down Expand Up @@ -46,7 +45,6 @@ public void processHook(EntityMetadata md, HookConfiguration cfg, List<HookDoc>
}
AuditHookConfiguration auditConfig = (AuditHookConfiguration) cfg;

LightblueFactory factory = LightblueFactory.getInstance();
try {
Error.push(auditConfig.getEntityName());
Error.push(auditConfig.getVersion());
Expand All @@ -55,6 +53,7 @@ public void processHook(EntityMetadata md, HookConfiguration cfg, List<HookDoc>
for (HookDoc hd : processedDocuments) {
// find if each field changed
JsonNodeCursor preCursor = hd.getPreDoc().cursor();
JsonNodeCursor postCursor = hd.getPostDoc().cursor();

// record to hold changes
Map<Path, AuditData> audits = new HashMap<>();
Expand All @@ -67,9 +66,10 @@ public void processHook(EntityMetadata md, HookConfiguration cfg, List<HookDoc>
// non-container node, check if it changed
String preValue = node.asText();
// if operation is delete, post value doesn't exist.
String postValue = hd.getOperation() == Operation.DELETE ? null : hd.getPostDoc().get(path).asText();
JsonNode postNode = hd.getPostDoc().get(path);
String postValue = hd.getOperation() == Operation.DELETE || postNode == null ? null : postNode.asText();

if ((preValue != null && preValue.equals(postValue))
if ((preValue != null && !preValue.equals(postValue))
|| (preValue == null && postValue != null)) {
// something changed! audit it..
AuditData ad = new AuditData();
Expand All @@ -82,6 +82,35 @@ public void processHook(EntityMetadata md, HookConfiguration cfg, List<HookDoc>
// else continue processing
}

while (postCursor.next()) {
Path path = postCursor.getCurrentPath();
JsonNode node = postCursor.getCurrentNode();

// shortcut, don't check if we have an audit for the path already
if (!audits.containsKey(path) && node.isValueNode()) {
// non-container node, check if it changed
JsonNode preNode = hd.getPreDoc().get(path);
String preValue = preNode == null ? null : preNode.asText();
String postValue = node.asText();

if ((preValue != null && !preValue.equals(postValue))
|| (preValue == null && postValue != null)) {
// something changed! audit it..
AuditData ad = new AuditData();
ad.path = path;
ad.pre = preValue;
ad.post = postValue;
audits.put(path, ad);
}
}
// else continue processing
}

// if there's nothing to audit, stop
if (audits.isEmpty()) {
return;
}

List<JsonNode> identifyingNodes = new ArrayList<>();

if (!audits.isEmpty()) {
Expand Down Expand Up @@ -133,6 +162,8 @@ public void processHook(EntityMetadata md, HookConfiguration cfg, List<HookDoc>

// and close it
buff.append("]}");

// TODO do something with this...
}
}
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,37 @@ public void noMissingIdentifyingFields() throws Exception {
// process hook
hook.processHook(em, config, processedDocuments);
}

/**
* Check side effect when there is a difference between pre and post documents.
*
* @throws Exception
*/
@Test
public void differentPreAndPost() throws Exception {
String jsonSchemaString = FileUtil.readFile(COUNTRY_METADATA_FILENAME);
EntityMetadata em = parser.parseEntityMetadata(json(jsonSchemaString));

// create hook configuration
AuditHookConfiguration config = new AuditHookConfiguration(em.getName(), em.getVersion().getValue());

// ------------------------------------------------------------
// mock up document data
List<HookDoc> processedDocuments = new ArrayList<>();

// need a json node for pre and post data. will create together to make easier
JsonNode pre = json(FileUtil.readFile(getClass().getSimpleName() + "-differentPreAndPost-pre.json"));
JsonNode post = json(FileUtil.readFile(getClass().getSimpleName() + "-differentPreAndPost-post.json"));

HookDoc hd = new HookDoc(em, new JsonDoc(pre), new JsonDoc(post), Operation.UPDATE);

processedDocuments.add(hd);
// ------------------------------------------------------------

// create hook
AuditHook hook = new AuditHook();

// process hook
hook.processHook(em, config, processedDocuments);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "United States of America",
"iso2Code": "US",
"iso3Code": "USA",
"optionalField": "changed"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "United States of America",
"iso2Code": "US",
"iso3Code": "USA"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"name": "United States of America"
"name": "United States of America",
"optionalField": "changed"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "United States of America",
"iso2Code": "US"
"iso2Code": "US",
"optionalField": "changed"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "United States of America",
"iso2Code": "US",
"iso3Code": "USA"
"iso3Code": "USA",
"optionalField": "changed"
}
3 changes: 3 additions & 0 deletions src/test/resources/metadata/country.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
"constraints": {
"identity": true
}
},
"optionalField": {
"type": "string"
}
}
}
Expand Down

0 comments on commit a67a6e8

Please sign in to comment.