Skip to content

Commit

Permalink
Refactored API of AvroUtils and added unit test on them
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill380 committed Jul 19, 2016
1 parent 8a6ca02 commit bec6e8e
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 8 deletions.
Expand Up @@ -93,7 +93,7 @@ public static void copyJsonProperties(JsonProperties src, JsonProperties dst) {
}


public static JsonNode injectUuids(JsonNode json) {
public static void injectUuids(JsonNode json) {
boolean containerWithoutId = json.isContainerNode() && !json.has(UUID_FIELD);
boolean notArray = !json.isArray();
boolean childIsNotArray = !(json.size() == 1 && json.getElements().next() instanceof ArrayNode);
Expand All @@ -106,11 +106,9 @@ public static JsonNode injectUuids(JsonNode json) {
if (node.isContainerNode())
injectUuids(node);
}

return json;
}

public static JsonNode removeUuids(JsonNode json) {
public static void removeUuids(JsonNode json) {
boolean containerWithId = json.isContainerNode() && json.has(UUID_FIELD);
boolean isArray = json.isArray();
boolean childIsNotArray = !(json.size() == 1 && json.getElements().next().isArray());
Expand All @@ -123,7 +121,5 @@ public static JsonNode removeUuids(JsonNode json) {
if (node.isContainerNode())
removeUuids(node);
}

return json;
}
}
@@ -0,0 +1,37 @@
package org.kaaproject.kaa.server.common.core.algorithms;


import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

public class AvroUtilsTest {
private JsonNode data;
private JsonNode dataWithUUIDs;

@Before
public void setUp() throws IOException {
data = new ObjectMapper().readTree(AvroUtilsTest.class.getClassLoader().getResourceAsStream("uuids/data.json"));
dataWithUUIDs = new ObjectMapper().readTree(AvroUtilsTest.class.getClassLoader().getResourceAsStream("uuids/data_with_uuids.json"));
}

@Test
public void testInjectUuids() throws IOException {
AvroUtils.injectUuids(data);
String jsonWithUUIds = data.toString();
Assert.assertTrue("Generated json is not equal json with UUIDs", jsonWithUUIds.equals(dataWithUUIDs.toString()));
}


@Test
public void testRemoveUuids() throws IOException {
AvroUtils.removeUuids(dataWithUUIDs);
String json = dataWithUUIDs.toString();
Assert.assertTrue("Generated json is not equal json without UUIDs", json.equals(data.toString()));
}

}
22 changes: 22 additions & 0 deletions common/core/src/test/resources/uuids/data.json
@@ -0,0 +1,22 @@
{
"AddressList": {
"array": [
{
"label": "Kaa website",
"url": "http://www.kaaproject.org"
},
{
"label": "Kaa GitHub repository",
"url": "https://github.com/kaaproject/kaa"
},
{
"label": "Kaa docs",
"url": "http://docs.kaaproject.org/display/KAA/Kaa+IoT+Platform+Home"
},
{
"label": "Kaa configuration design reference",
"url": "http://docs.kaaproject.org/display/KAA/Configuration"
}
]
}
}
27 changes: 27 additions & 0 deletions common/core/src/test/resources/uuids/data_with_uuids.json
@@ -0,0 +1,27 @@
{
"AddressList": {
"array": [
{
"label": "Kaa website",
"url": "http://www.kaaproject.org",
"__uuid":null
},
{
"label": "Kaa GitHub repository",
"url": "https://github.com/kaaproject/kaa",
"__uuid":null
},
{
"label": "Kaa docs",
"url": "http://docs.kaaproject.org/display/KAA/Kaa+IoT+Platform+Home",
"__uuid":null
},
{
"label": "Kaa configuration design reference",
"url": "http://docs.kaaproject.org/display/KAA/Configuration",
"__uuid":null
}
]
},
"__uuid":null
}
Expand Up @@ -2597,7 +2597,7 @@ public RecordField getConfigurationRecordDataFromFile(String schema, String file
byte[] body = getFileContent(fileItemName);

JsonNode json = new ObjectMapper().readTree(body);
json = AvroUtils.injectUuids(json);
AvroUtils.injectUuids(json);
body = json.toString().getBytes();

GenericAvroConverter<GenericRecord> converter = new GenericAvroConverter<>(schema);
Expand Down
Expand Up @@ -365,7 +365,7 @@ public BaseData compute(List<EndpointGroupStateDto> key) {

private ConfigurationCacheEntry buildBaseResyncDelta(String endpointId, AbstractKaaData<?> mergedConf, EndpointObjectHash userConfigurationHash) throws IOException {
JsonNode json = new ObjectMapper().readTree(mergedConf.getRawData());
json = AvroUtils.removeUuids(json);
AvroUtils.removeUuids(json);
byte[] configuration = GenericAvroConverter.toRawData(json.toString(), mergedConf.getSchema().getRawSchema());
return new ConfigurationCacheEntry(configuration, new BaseBinaryDelta(configuration), EndpointObjectHash.fromSHA1(configuration),
userConfigurationHash);
Expand Down

0 comments on commit bec6e8e

Please sign in to comment.