Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support StreamConfiguration creation from JSON #1023

Merged
merged 3 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/main/java/io/nats/client/api/StreamConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@

package io.nats.client.api;

import io.nats.client.support.JsonSerializable;
import io.nats.client.support.JsonUtils;
import io.nats.client.support.JsonValue;
import io.nats.client.support.*;

import java.time.Duration;
import java.util.*;
Expand Down Expand Up @@ -143,9 +141,21 @@ static StreamConfiguration instance(JsonValue v) {
this.firstSequence = b.firstSequence;
}

/**
* Returns a StreamConfiguration deserialized from its JSON form.
*
* @see #toJson()
* @param json
* @return StreamConfiguration for the given json
* @throws JsonParseException
*/
public static StreamConfiguration instance(String json) throws JsonParseException {
return instance(JsonParser.parse(json));
}

/**
* Returns a JSON representation of this consumer configuration.
*
*
* @return json consumer configuration to send to the server.
*/
public String toJson() {
Expand Down
81 changes: 81 additions & 0 deletions src/test/java/io/nats/client/api/StreamConfigurationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.nats.client.utils.ResourceUtils;
import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;

import java.time.Duration;
import java.time.ZonedDateTime;
Expand All @@ -30,6 +31,10 @@
import static io.nats.client.api.CompressionOption.None;
import static io.nats.client.api.CompressionOption.S2;
import static io.nats.client.api.ConsumerConfiguration.*;
import static io.nats.client.support.ApiConstants.*;
import static io.nats.client.support.ApiConstants.FIRST_SEQ;
import static io.nats.client.support.JsonValueUtils.*;
import static io.nats.client.support.JsonValueUtils.readLong;
import static org.junit.jupiter.api.Assertions.*;

public class StreamConfigurationTests extends JetStreamTestBase {
Expand Down Expand Up @@ -61,6 +66,82 @@ public void testRoundTrip() throws Exception {
});
}

@Test
public void testSerializationDeserialization() throws Exception {
String originalJson = ResourceUtils.dataAsString("StreamConfiguration.json");
StreamConfiguration sc = StreamConfiguration.instance(originalJson);
validate(sc, false);
String serializedJson = sc.toJson();
validate(StreamConfiguration.instance(serializedJson), false);
}

@Test
public void testSerializationDeserializationDefaults() throws Exception {
StreamConfiguration sc = StreamConfiguration.instance("");
assertNotNull(sc);
String serializedJson = sc.toJson();
assertNotNull(StreamConfiguration.instance(serializedJson));
}

@Test
public void testMissingJsonFields() throws Exception{
List<String> streamConfigFields = new ArrayList<String>(){
{
add(RETENTION);
add(COMPRESSION);
add(STORAGE);
add(DISCARD);
add(NAME);
add(DESCRIPTION);
add(MAX_CONSUMERS);
add(MAX_MSGS);
add(MAX_MSGS_PER_SUB);
add(MAX_BYTES);
add(MAX_AGE);
add(MAX_MSG_SIZE);
add(NUM_REPLICAS);
add(NO_ACK);
add(TEMPLATE_OWNER);
add(DUPLICATE_WINDOW);
add(SUBJECTS);
add(PLACEMENT);
add(REPUBLISH);
add(SUBJECT_TRANSFORM);
add(CONSUMER_LIMITS);
add(MIRROR);
add(SOURCES);
add(SEALED);
add(ALLOW_ROLLUP_HDRS);
add(ALLOW_DIRECT);
add(MIRROR_DIRECT);
add(DENY_DELETE);
add(DENY_PURGE);
add(DISCARD_NEW_PER_SUBJECT);
add(METADATA);
add(FIRST_SEQ);
}
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very interesting voodoo here, I like.


String originalJson = ResourceUtils.dataAsString("StreamConfiguration.json");

// Loops through each field in the StreamConfiguration JSON format and ensures that the
// StreamConfiguration can be built without that field being present in the JSON
for (String streamConfigFieldName: streamConfigFields) {
JsonValue originalParsedJson = JsonParser.parse(originalJson);
originalParsedJson.map.remove(streamConfigFieldName);
StreamConfiguration sc = StreamConfiguration.instance(originalParsedJson.toJson());
assertNotNull(sc);
}
senior marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
public void testInvalidNameInJson() throws Exception{
String originalJson = ResourceUtils.dataAsString("StreamConfiguration.json");
JsonValue originalParsedJson = JsonParser.parse(originalJson);
originalParsedJson.map.put(NAME, new JsonValue("Inavlid*Name"));
assertThrows(IllegalArgumentException.class, () -> StreamConfiguration.instance(originalParsedJson.toJson()));
}

@Test
public void testConstruction() {
StreamConfiguration testSc = getTestConfiguration();
Expand Down