Skip to content

Commit

Permalink
Support StreamConfiguration creation from JSON (#1023)
Browse files Browse the repository at this point in the history
  • Loading branch information
senior authored Oct 24, 2023
1 parent 087be16 commit 057e0f4
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 4 deletions.
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);
}
};

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);
}
}

@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

0 comments on commit 057e0f4

Please sign in to comment.