Skip to content

Commit

Permalink
add properties default value for SchemaInfoBuilder (apache#8952)
Browse files Browse the repository at this point in the history
### Motivation

When using SchemaInfoBuilder to create a SchemaInfo, not setting properties will result in an NPE. properties are optional in SchemaInfo and have default values.

### Modifications

Add default values for properties in SchemaInfoBuilder
  • Loading branch information
jianyun8023 authored Dec 15, 2020
1 parent a3ac12e commit cb6617f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class SchemaInfo {
/**
* Additional properties of the schema definition (implementation defined).
*/
@Builder.Default
private Map<String, String> properties = Collections.emptyMap();

public String getSchemaDefinition() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@
*/
package org.apache.pulsar.client.impl.schema;

import static org.testng.Assert.assertEquals;

import com.google.common.collect.Maps;
import org.apache.pulsar.client.api.Schema;
import org.apache.pulsar.common.schema.KeyValueEncodingType;
import org.apache.pulsar.common.schema.SchemaInfo;
import org.apache.pulsar.common.schema.SchemaType;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.Map;

import static org.testng.Assert.assertEquals;

/**
* Unit test {@link org.apache.pulsar.common.schema.SchemaInfo}.
*/
Expand Down Expand Up @@ -280,4 +284,38 @@ public void testSchemaInfoToString(SchemaInfo si, String jsonifiedStr) {
assertEquals(si.toString(), jsonifiedStr);
}

public static class SchemaInfoBuilderTest {

@Test
public void testUnsetProperties() {
final SchemaInfo schemaInfo = SchemaInfo.builder()
.type(SchemaType.STRING)
.schema(new byte[0])
.name("string")
.build();

assertEquals(schemaInfo.getSchema(), new byte[0]);
assertEquals(schemaInfo.getType(), SchemaType.STRING);
assertEquals(schemaInfo.getName(), "string");
assertEquals(schemaInfo.getProperties(), Maps.newHashMap());
}

@Test
public void testSetProperties() {
final Map<String, String> map = Maps.newHashMap();
map.put("test", "value");
final SchemaInfo schemaInfo = SchemaInfo.builder()
.type(SchemaType.STRING)
.schema(new byte[0])
.name("string")
.properties(map)
.build();

assertEquals(schemaInfo.getSchema(), new byte[0]);
assertEquals(schemaInfo.getType(), SchemaType.STRING);
assertEquals(schemaInfo.getName(), "string");
assertEquals(schemaInfo.getProperties(), Maps.newHashMap(map));
}

}
}

0 comments on commit cb6617f

Please sign in to comment.