Intuitive DSL for Java

Version 2.0.0 · src/test/java/ch/dbalabs/intuitivedsl/grammar/GrammarCompilerHardeningTest.java

Git clone
git clone https://www.dbalabs.ch/git/intuitive-dsl-java.git

GrammarCompilerHardeningTest.java

/*
 * This file is part of the Intuitive DSL project.
 * Copyright (c) 2026 DBA Labs - Switzerland. All rights reserved.
 *
 * This program is dual-licensed under a commercial license and the AGPLv3.
 * For commercial licensing, contact us at [email protected] or visit https://www.dbalabs.ch.
 *
 * AGPLv3 licensing:
 * 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, version 3 (19 November 2007).
 *
 * 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://www.gnu.org/licenses/agpl-3.0.html>.
 */

package ch.dbalabs.intuitivedsl.grammar;

import ch.dbalabs.intuitivedsl.exception.DslDefinitionException;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class GrammarCompilerHardeningTest {

    private final GrammarCompiler compiler = new GrammarCompiler();

    @Test
    void shouldCompileRepeatableParameterUsingEllipsisOnly() {
        GroupNode root = compiler.compile("GRANT ACCESS TO target_user ... ;");
        AlternativeNode main = (AlternativeNode) root.children().get(0);

        assertThat(main.children()).hasSize(3);
        assertThat(main.children().get(0)).isInstanceOf(KeywordNode.class)
                .extracting("word")
                .isEqualTo("GRANT ACCESS TO");
        assertThat(main.children().get(1)).isInstanceOf(ParameterNode.class);
        assertThat(main.children().get(1).isRepeatable()).isTrue();
        assertThat(main.children()).noneMatch(node -> node instanceof DelimiterNode d && ".".equals(d.value()));
    }

    @Test
    void shouldCompileRepeatableOptionalGroupUsingEllipsisOnly() {
        GroupNode root = compiler.compile("GENERATE REPORT [ WITH metric_name ] ... ;");
        AlternativeNode main = (AlternativeNode) root.children().get(0);

        assertThat(main.children()).hasSize(3);
        assertThat(main.children().get(1)).isInstanceOf(GroupNode.class);
        assertThat(((GroupNode) main.children().get(1)).type()).isEqualTo(GroupType.OPTIONAL);
        assertThat(main.children().get(1).isRepeatable()).isTrue();
    }

    @Test
    void shouldCompileRepeatableRequiredGroupUsingEllipsisOnly() {
        GroupNode root = compiler.compile("BLOCK TRAFFIC FROM { IP ip_address | SUBNET subnet_mask } ... ;");
        AlternativeNode main = (AlternativeNode) root.children().get(0);

        assertThat(main.children()).hasSize(3);
        assertThat(main.children().get(1)).isInstanceOf(GroupNode.class);
        assertThat(((GroupNode) main.children().get(1)).type()).isEqualTo(GroupType.REQUIRED);
        assertThat(main.children().get(1).isRepeatable()).isTrue();
    }

    @Test
    void shouldRejectLeadingEllipsisWithoutTargetNode() {
        assertThatThrownBy(() -> compiler.compile("... USER name ;"))
                .isInstanceOf(DslDefinitionException.class)
                .hasMessageContaining("Operator '...' needs a preceding node");
    }

    @Test
    void shouldRejectEmptyOptionalGroup() {
        assertThatThrownBy(() -> compiler.compile("DO [] ;"))
                .isInstanceOf(DslDefinitionException.class)
                .hasMessageContaining("Empty optional group");
    }

    @Test
    void shouldRejectEmptyRequiredGroup() {
        assertThatThrownBy(() -> compiler.compile("DO {} ;"))
                .isInstanceOf(DslDefinitionException.class)
                .hasMessageContaining("Empty required group");
    }

    @Test
    void shouldRejectEmptyMacroNameInModernSyntax() {
        assertThatThrownBy(() -> compiler.compile("DO ${} ;"))
                .isInstanceOf(DslDefinitionException.class)
                .hasMessageContaining("Empty macro name");
    }

    @Test
    void shouldRejectEmptyMacroNameInLegacySyntax() {
        assertThatThrownBy(() -> compiler.compile("DO <> ;"))
                .isInstanceOf(DslDefinitionException.class)
                .hasMessageContaining("Empty legacy macro name");
    }

    @Test
    void shouldMergeAdjacentKeywordsIntoSingleAstKeywordNode() {
        GroupNode root = compiler.compile("ARCHIVE SYSTEM RESTART ;");
        AlternativeNode main = (AlternativeNode) root.children().get(0);

        assertThat(main.children()).hasSize(2);
        assertThat(main.children().get(0)).isInstanceOf(KeywordNode.class)
                .extracting("word")
                .isEqualTo("ARCHIVE SYSTEM RESTART");
    }
}