-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate json records into multiple messages (#40)
* implement JsonRecords object which parses json records type event into separate messages. * JsonRecords.java: make rv immutable; use try-with-resources with JsonReader Co-authored-by: Moonbow-1 <146731818+MoonBow-1@users.noreply.github.com> * comments for JsonRecords.java event passthroughs * make expected messages explicit * add EqualsVerifier and test for JsonRecords. --------- Co-authored-by: Moonbow-1 <146731818+MoonBow-1@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
302 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/main/java/com/teragrep/aer_02/json/JsonRecords.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Teragrep Eventhub Reader as an Azure Function | ||
* Copyright (C) 2024 Suomen Kanuuna Oy | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://github.com/teragrep/teragrep/blob/main/LICENSE>. | ||
* | ||
* | ||
* Additional permission under GNU Affero General Public License version 3 | ||
* section 7 | ||
* | ||
* If you modify this Program, or any covered work, by linking or combining it | ||
* with other code, such other code is not for that reason alone subject to any | ||
* of the requirements of the GNU Affero GPL version 3 as long as this Program | ||
* is the same Program as licensed from Suomen Kanuuna Oy without any additional | ||
* modifications. | ||
* | ||
* Supplemented terms under GNU Affero General Public License version 3 | ||
* section 7 | ||
* | ||
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified | ||
* versions must be marked as "Modified version of" The Program. | ||
* | ||
* Names of the licensors and authors may not be used for publicity purposes. | ||
* | ||
* No rights are granted for use of trade names, trademarks, or service marks | ||
* which are in The Program if any. | ||
* | ||
* Licensee must indemnify licensors and authors for any liability that these | ||
* contractual assumptions impose on licensors and authors. | ||
* | ||
* To the extent this program is licensed as part of the Commercial versions of | ||
* Teragrep, the applicable Commercial License may apply to this file if you as | ||
* a licensee so wish it. | ||
*/ | ||
package com.teragrep.aer_02.json; | ||
|
||
import jakarta.json.*; | ||
import jakarta.json.stream.JsonParsingException; | ||
|
||
import java.io.StringReader; | ||
import java.util.Objects; | ||
|
||
public final class JsonRecords { | ||
|
||
private final String event; | ||
|
||
public JsonRecords(final String event) { | ||
this.event = event; | ||
} | ||
|
||
/** | ||
* Expects <code>{"records":[{},{}, ..., {}]}</code> type JSON string. | ||
* | ||
* @return individual records as an array or the original event. | ||
*/ | ||
public String[] records() { | ||
final String[] rv = new String[] { | ||
event | ||
}; | ||
|
||
final JsonStructure mainStructure; | ||
try (final JsonReader reader = Json.createReader(new StringReader(event))) { | ||
mainStructure = reader.read(); | ||
} | ||
catch (JsonParsingException e) { | ||
// pass event through as-is if JSON parsing fails | ||
return rv; | ||
} | ||
|
||
final JsonValue recordsStructure = mainStructure.getValue("/records"); | ||
|
||
if (recordsStructure.getValueType().equals(JsonValue.ValueType.ARRAY)) { | ||
final JsonArray recordsArray = recordsStructure.asJsonArray(); | ||
String[] records = new String[recordsArray.size()]; | ||
|
||
for (int i = 0; i < recordsArray.size(); i++) { | ||
// Take string representation of inner value regardless of actual datatype | ||
records[i] = recordsArray.get(i).toString(); | ||
} | ||
|
||
return records; | ||
} | ||
|
||
// pass event through as-is if "records" is not an array type | ||
return rv; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) | ||
return true; | ||
if (o == null || getClass() != o.getClass()) | ||
return false; | ||
JsonRecords that = (JsonRecords) o; | ||
return Objects.equals(event, that.event); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(event); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
src/test/java/com/teragrep/aer_02/json/JsonRecordsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Teragrep Eventhub Reader as an Azure Function | ||
* Copyright (C) 2024 Suomen Kanuuna Oy | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://github.com/teragrep/teragrep/blob/main/LICENSE>. | ||
* | ||
* | ||
* Additional permission under GNU Affero General Public License version 3 | ||
* section 7 | ||
* | ||
* If you modify this Program, or any covered work, by linking or combining it | ||
* with other code, such other code is not for that reason alone subject to any | ||
* of the requirements of the GNU Affero GPL version 3 as long as this Program | ||
* is the same Program as licensed from Suomen Kanuuna Oy without any additional | ||
* modifications. | ||
* | ||
* Supplemented terms under GNU Affero General Public License version 3 | ||
* section 7 | ||
* | ||
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified | ||
* versions must be marked as "Modified version of" The Program. | ||
* | ||
* Names of the licensors and authors may not be used for publicity purposes. | ||
* | ||
* No rights are granted for use of trade names, trademarks, or service marks | ||
* which are in The Program if any. | ||
* | ||
* Licensee must indemnify licensors and authors for any liability that these | ||
* contractual assumptions impose on licensors and authors. | ||
* | ||
* To the extent this program is licensed as part of the Commercial versions of | ||
* Teragrep, the applicable Commercial License may apply to this file if you as | ||
* a licensee so wish it. | ||
*/ | ||
package com.teragrep.aer_02.json; | ||
|
||
import jakarta.json.Json; | ||
import nl.jqno.equalsverifier.EqualsVerifier; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class JsonRecordsTest { | ||
|
||
@Test | ||
void testRecordsAsObjectsCase() { | ||
final String records = Json | ||
.createObjectBuilder() | ||
.add("records", Json.createArrayBuilder().add(Json.createObjectBuilder().add("a", "b")).add(Json.createObjectBuilder().add("c", "d"))).build().toString(); | ||
JsonRecords jr = new JsonRecords(records); | ||
final String[] result = jr.records(); | ||
Assertions.assertEquals(2, result.length); | ||
Assertions.assertEquals("{\"a\":\"b\"}", result[0]); | ||
Assertions.assertEquals("{\"c\":\"d\"}", result[1]); | ||
} | ||
|
||
@Test | ||
void testRecordsAsStringsAndNumbersCase() { | ||
final String records = Json | ||
.createObjectBuilder() | ||
.add("records", Json.createArrayBuilder().add("abc").add(123).build()) | ||
.build() | ||
.toString(); | ||
JsonRecords jr = new JsonRecords(records); | ||
final String[] result = jr.records(); | ||
Assertions.assertEquals(2, result.length); | ||
Assertions.assertEquals("\"abc\"", result[0]); | ||
Assertions.assertEquals("123", result[1]); | ||
} | ||
|
||
@Test | ||
void testNonJsonRecordsCase() { | ||
final String records = "{{]///...<>;"; | ||
JsonRecords jr = new JsonRecords(records); | ||
final String[] result = jr.records(); | ||
Assertions.assertEquals(1, result.length); | ||
Assertions.assertEquals("{{]///...<>;", result[0]); | ||
} | ||
|
||
@Test | ||
void testEquals() { | ||
JsonRecords jr = new JsonRecords("{\"a\":\"b\",\"c\":\"d\"}"); | ||
JsonRecords jr2 = new JsonRecords("{\"a\":\"b\",\"c\":\"d\"}"); | ||
Assertions.assertEquals(jr, jr2); | ||
} | ||
|
||
@Test | ||
void testNotEquals() { | ||
JsonRecords jr = new JsonRecords("{\"a\":\"b\",\"c\":\"d\"}"); | ||
JsonRecords jr2 = new JsonRecords("{\"e\":\"f\",\"g\":\"h\"}"); | ||
Assertions.assertNotEquals(jr, jr2); | ||
} | ||
|
||
@Test | ||
void testEqualsContract() { | ||
EqualsVerifier.forClass(JsonRecords.class).verify(); | ||
} | ||
} |