Skip to content

Commit

Permalink
refactor: make SchemaParser ready for dependency injection (#3321)
Browse files Browse the repository at this point in the history
  • Loading branch information
big-andy-coates authored Sep 11, 2019
1 parent c571508 commit 797010c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

import static io.confluent.ksql.schema.ksql.TypeContextUtil.getType;
import static io.confluent.ksql.util.ParserUtil.getLocation;
import static java.util.Objects.requireNonNull;

import com.google.common.annotations.VisibleForTesting;
import io.confluent.ksql.metastore.TypeRegistry;
import io.confluent.ksql.parser.tree.TableElement;
import io.confluent.ksql.parser.tree.TableElement.Namespace;
Expand All @@ -35,14 +35,17 @@

public final class SchemaParser {

private SchemaParser() { }
private final TypeRegistry typeRegistry;

@VisibleForTesting
static TableElements parse(final String schema) {
return parse(schema, TypeRegistry.EMPTY);
public SchemaParser(final TypeRegistry typeRegistry) {
this.typeRegistry = requireNonNull(typeRegistry, "typeRegistry");
}

public static TableElements parse(final String schema, final TypeRegistry typeRegistry) {
return new SchemaParser(typeRegistry).parse(schema);
}

public TableElements parse(final String schema) {
if (schema.trim().isEmpty()) {
return TableElements.of();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,43 @@
import static org.hamcrest.Matchers.hasItem;

import com.google.common.collect.Iterables;
import io.confluent.ksql.execution.expression.tree.Type;
import io.confluent.ksql.metastore.TypeRegistry;
import io.confluent.ksql.parser.tree.TableElement;
import io.confluent.ksql.parser.tree.TableElement.Namespace;
import io.confluent.ksql.parser.tree.TableElements;
import io.confluent.ksql.execution.expression.tree.Type;
import io.confluent.ksql.schema.ksql.types.SqlTypes;
import io.confluent.ksql.util.KsqlException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class SchemaParserTest {

@Rule
public final ExpectedException expectedException = ExpectedException.none();

@Mock
private TypeRegistry typeRegistry;
private SchemaParser parser;

@Before
public void setUp() {
parser = new SchemaParser(typeRegistry);
}

@Test
public void shouldParseValidSchema() {
// Given:
final String schema = "foo INTEGER, bar MAP<VARCHAR, VARCHAR>";

// When:
final TableElements elements = SchemaParser.parse(schema);
final TableElements elements = parser.parse(schema);

// Then:
assertThat(elements, contains(
Expand All @@ -57,7 +72,7 @@ public void shouldParseValidSchemaWithKeyField() {
final String schema = "ROWKEY STRING KEY, bar INT";

// When:
final TableElements elements = SchemaParser.parse(schema);
final TableElements elements = parser.parse(schema);

// Then:
assertThat(elements, contains(
Expand All @@ -72,7 +87,7 @@ public void shouldParseQuotedSchema() {
final String schema = "`END` VARCHAR";

// When:
final TableElements elements = SchemaParser.parse(schema);
final TableElements elements = parser.parse(schema);

// Then:
assertThat(elements, hasItem(
Expand All @@ -86,7 +101,7 @@ public void shouldParseQuotedMixedCase() {
final String schema = "`End` VARCHAR";

// When:
final TableElements elements = SchemaParser.parse(schema);
final TableElements elements = parser.parse(schema);

// Then:
assertThat(elements, hasItem(
Expand All @@ -100,7 +115,7 @@ public void shouldParseEmptySchema() {
final String schema = " \t\n\r";

// When:
final TableElements elements = SchemaParser.parse(schema);
final TableElements elements = parser.parse(schema);

// Then:
assertThat(Iterables.isEmpty(elements), is(true));
Expand All @@ -116,7 +131,7 @@ public void shouldThrowOnInvalidSchema() {
expectedException.expectMessage("Error parsing schema \"foo-bar INTEGER\" at 1:4: extraneous input '-' ");

// When:
SchemaParser.parse(schema);
parser.parse(schema);
}

@Test
Expand All @@ -129,6 +144,6 @@ public void shouldThrowOnReservedWord() {
expectedException.expectMessage("Error parsing schema \"CREATE INTEGER\" at 1:1: extraneous input 'CREATE' ");

// When:
SchemaParser.parse(schema);
parser.parse(schema);
}
}

0 comments on commit 797010c

Please sign in to comment.