Skip to content

Commit

Permalink
Merge 247a9af into fce1503
Browse files Browse the repository at this point in the history
  • Loading branch information
maheshsubramanian committed Mar 8, 2020
2 parents fce1503 + 247a9af commit 7999a26
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

public class Events {

private List<String> globalAttributes;

private List<Event> events;

public Events() {
}

public Events(final List<Event> events) {
this.events = events;
public List<String> getGlobalAttributes() {
return globalAttributes;
}

public List<Event> getEvents() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import javax.json.JsonValue;
import javax.json.JsonValue.ValueType;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -89,7 +90,8 @@ private JsonObjectBuilder processObjectPayload(final JsonObject payload, final J
} else {
final String pathForField = getPathForField(jsonPath, fieldName);

final boolean anonymise = !fieldsToIgnore.contains(pathForField);
final boolean attributePathWhitelistedGlobally = isAttributePathEndingWithGloballyWhitelistedAttributeValues(pathForField);
final boolean anonymise = !attributePathWhitelistedGlobally && !fieldsToIgnore.contains(pathForField);
setFieldValueInObject(fieldName, jsonValue, builder, anonymise);
}
}
Expand All @@ -100,8 +102,7 @@ private JsonObjectBuilder processObjectPayload(final JsonObject payload, final J
private JsonArrayBuilder processArrayPayload(final JsonArray payload, final JsonArrayBuilder builder, final String jsonPath, final List<String> fieldsToIgnore) {


for (int counter = 0; counter < payload.size(); counter++) {
final JsonValue value = payload.get(counter);
for (final JsonValue value : payload) {
final ValueType fieldValueType = value.getValueType();
final String pathForField = getPathForField(jsonPath, "[*]");

Expand All @@ -118,7 +119,8 @@ private JsonArrayBuilder processArrayPayload(final JsonArray payload, final Json
builder.add(value);
}
} else {
final boolean anonymise = !fieldsToIgnore.contains(pathForField);
final boolean attributePathWhitelistedGlobally = isAttributePathEndingWithGloballyWhitelistedAttributeValues(pathForField);
final boolean anonymise = !attributePathWhitelistedGlobally && !fieldsToIgnore.contains(pathForField);
setFieldValueInArray(value, builder, anonymise);
}
}
Expand Down Expand Up @@ -152,6 +154,14 @@ private String getTransformedStringValue(final JsonValue value) {
return (String) stringPatternGeneratorFactory.getGenerator(valueAsString).convert(valueAsString);
}

private boolean isAttributePathEndingWithGloballyWhitelistedAttributeValues(final String jsonPathForAttribute) {
return getAttributesWhitelistedForAllEvents().stream().anyMatch(jsonPathForAttribute::endsWith);
}

private List<String> getAttributesWhitelistedForAllEvents() {
return CollectionUtils.isNotEmpty(EVENTS.getGlobalAttributes()) ? EVENTS.getGlobalAttributes() : newArrayList();
}

private List<String> getFieldsToIgnoreForEvent(final String eventName) {
final Optional<Event> optionalEvent = EVENTS.getEvents().stream().filter(e -> e.getEventName().equals(eventName)).findFirst();
return optionalEvent.isPresent() ? optionalEvent.get().getFieldsToBeIgnored() : emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"globalAttributes": {
"type": "array",
"description": "Fields that are considered for all events. This field does not denote a complete JSON path. It only identifies the ending of a json path (partial entry)",
"items": [
{
"type": "string"
}
]
},
"events": {
"type": "array",
"items": [
Expand All @@ -23,12 +32,14 @@
"required": [
"eventName",
"fieldsToBeIgnored"
]
],
"additionalProperties": false
}
]
}
},
"required": [
"events"
]
],
"additionalProperties": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,23 @@ public void shouldNotAnonymiseJsonArrayPayload() {
assertThat(anonymisedJsonArray.getString(6), is("SC208979B"));
}

@Test
public void shouldNotAnonymiseGloballyWhitelistedAttributes() {
final JsonObject anonymisedPayload = service.anonymiseObjectPayload(buildObjectPayload("test-object-data.json"), "test-object-event");
final JsonArray exampleArray = anonymisedPayload.getJsonArray("exampleArray");
assertThat(exampleArray.getJsonObject(0).getJsonArray("repeatingParentArrayAttribute").getJsonObject(0).getString("repeatingChildAttribute"), is("child1"));
assertThat(exampleArray.getJsonObject(0).getJsonArray("repeatingParentArrayAttribute").getJsonObject(0).getString("repeatingAttribute"), is("test1"));
assertThat(exampleArray.getJsonObject(0).getJsonArray("repeatingParentArrayAttribute").getJsonObject(0).getJsonArray("repeatingParentArrayAttribute").getJsonObject(0).getString("repeatingChildAttribute"), is("child2"));
assertThat(exampleArray.getJsonObject(0).getJsonArray("repeatingParentArrayAttribute").getJsonObject(0).getJsonArray("repeatingParentArrayAttribute").getJsonObject(0).getString("repeatingAttribute"), is("test2"));

final JsonObject exampleObject = anonymisedPayload.getJsonObject("example");
final JsonObject repeatingObject = exampleObject.getJsonObject("repeatingParentObjectAttribute");
assertThat(repeatingObject.getString("repeatingChildAttribute"), is("child3"));
assertThat(repeatingObject.getString("repeatingAttribute"), is("test3"));
assertThat(repeatingObject.getJsonObject("repeatingParentObjectAttribute").getString("repeatingChildAttribute"), is("child4"));
assertThat(repeatingObject.getJsonObject("repeatingParentObjectAttribute").getString("repeatingAttribute"), is("test4"));
}


private JsonObject buildObjectPayload(final String payloadFileName) {
final JsonReader jsonReader = createReader(new StringReader(getFileContentsAsString(payloadFileName)));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"globalAttributes": [
"repeatingParentArrayAttribute[*].repeatingChildAttribute",
"repeatingParentObjectAttribute.repeatingChildAttribute",
"repeatingAttribute"
],
"events": [
{
"eventName": "test-object-event",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@
"exampleArray": [
{
"attributeIntAsString": "001",
"attributeIntAsStringAnonymise": "001"
"attributeIntAsStringAnonymise": "001",
"repeatingParentArrayAttribute": [
{
"repeatingChildAttribute": "child1",
"repeatingAttribute" : "test1",
"repeatingParentArrayAttribute": [
{
"repeatingChildAttribute": "child2",
"repeatingAttribute" : "test2"
}
]
}
]
}
],
"example": {
Expand All @@ -27,6 +39,14 @@
},
"test234@mail.com",
"SC208979B"
]
],
"repeatingParentObjectAttribute": {
"repeatingChildAttribute": "child3",
"repeatingAttribute" : "test3",
"repeatingParentObjectAttribute": {
"repeatingChildAttribute": "child4",
"repeatingAttribute" : "test4"
}
}
}
}

0 comments on commit 7999a26

Please sign in to comment.