From 8912aebdb7ee56f98896002ad4e2f4da9bc380c6 Mon Sep 17 00:00:00 2001 From: Gabriele-Cardosi Date: Wed, 8 Jan 2025 13:40:58 +0100 Subject: [PATCH] [incubator-kie-issues#1742] Extend test coverage --- .../org/kie/dmn/feel/lang/FEELDialect.java | 38 ++++++++- .../kie/dmn/feel/lang/FEELDialectTest.java | 81 +++++++++++++++++++ 2 files changed, 115 insertions(+), 4 deletions(-) create mode 100644 kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/lang/FEELDialectTest.java diff --git a/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/lang/FEELDialect.java b/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/lang/FEELDialect.java index 9d821f2407f..81716c03107 100644 --- a/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/lang/FEELDialect.java +++ b/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/lang/FEELDialect.java @@ -19,6 +19,10 @@ package org.kie.dmn.feel.lang; import java.util.Arrays; +import java.util.HashSet; +import java.util.Optional; +import java.util.Set; + public enum FEELDialect { @@ -33,15 +37,41 @@ public enum FEELDialect { public String getNamespace() {return namespace;} + public final static Set STANDARD_FEEL_URIS = getStandardFEELDialectURIS(); + /** - * In current implementation, it returns BFEEL if provided namespace matches, otherwise returns FEEL + * It returns BFEEL if provided namespace matches, + * or FEEL if it matches any of the STANDARD_FEEL_URIS, + * or throws an IllegalArgumentException if no match is found + * * @param namespace * @return */ public static FEELDialect fromNamespace(String namespace) { - return Arrays.stream(FEELDialect.values()) + Optional byNamespace = Arrays.stream(FEELDialect.values()) .filter(dialect -> dialect.getNamespace().equals(namespace)) - .findFirst() - .orElse(FEELDialect.FEEL); + .findFirst(); + if (byNamespace.isPresent()) { + return byNamespace.get(); + } + Optional fromStandardFeelUris = getStandardFeelDialect(namespace); + if (fromStandardFeelUris.isPresent()) { + return fromStandardFeelUris.get(); + } + throw new IllegalArgumentException("Unknown FEEL dialect '" + namespace + "'"); + } + + static Optional getStandardFeelDialect(String namespace) { + return STANDARD_FEEL_URIS.contains(namespace) ? Optional.of(FEEL) : Optional.empty(); + } + + private static Set getStandardFEELDialectURIS() { + Set toReturn = new HashSet<>(); + toReturn.add(org.kie.dmn.model.v1_1.KieDMNModelInstrumentedBase.URI_FEEL); + toReturn.add(org.kie.dmn.model.v1_2.KieDMNModelInstrumentedBase.URI_FEEL); + toReturn.add(org.kie.dmn.model.v1_3.KieDMNModelInstrumentedBase.URI_FEEL); + toReturn.add(org.kie.dmn.model.v1_4.KieDMNModelInstrumentedBase.URI_FEEL); + toReturn.add(org.kie.dmn.model.v1_5.KieDMNModelInstrumentedBase.URI_FEEL); + return toReturn; } } \ No newline at end of file diff --git a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/lang/FEELDialectTest.java b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/lang/FEELDialectTest.java new file mode 100644 index 00000000000..5413711e1e5 --- /dev/null +++ b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/lang/FEELDialectTest.java @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.kie.dmn.feel.lang; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.kie.dmn.feel.lang.FEELDialect.STANDARD_FEEL_URIS; + +class FEELDialectTest { + + @Test + void fromNamespaceValid() { + String bFeelNamespace = "https://www.omg.org/spec/DMN/20240513/B-FEEL/"; + assertThat(FEELDialect.fromNamespace(bFeelNamespace)).isEqualTo(FEELDialect.BFEEL); + + String emptyNamespace = ""; + assertThat(FEELDialect.fromNamespace(emptyNamespace)).isEqualTo(FEELDialect.FEEL); + + STANDARD_FEEL_URIS.forEach(namespace -> assertThat(FEELDialect.fromNamespace(namespace)).isEqualTo(FEELDialect.FEEL)); + } + + @Test + void fromNamespaceInvalid() { + String unknownNameSpace = "whatever-get"; + assertThatThrownBy(() -> FEELDialect.fromNamespace(unknownNameSpace)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Unknown FEEL dialect '" + unknownNameSpace + "'"); + String nullNameSpace = null; + assertThatThrownBy(() -> FEELDialect.fromNamespace(nullNameSpace)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Unknown FEEL dialect '" + nullNameSpace + "'"); + } + + @Test + void getStandardFeelDialectValid() { + String bFeelNamespace = "https://www.omg.org/spec/DMN/20240513/B-FEEL/"; + assertThat(FEELDialect.fromNamespace(bFeelNamespace)).isEqualTo(FEELDialect.BFEEL); + + String emptyNamespace = ""; + assertThat(FEELDialect.fromNamespace(emptyNamespace)).isEqualTo(FEELDialect.FEEL); + + STANDARD_FEEL_URIS.forEach(namespace -> assertThat(FEELDialect.getStandardFeelDialect(namespace)) + .isNotEmpty() + .contains(FEELDialect.FEEL)); + } + + @Test + void getStandardFeelDialectInvalid() { + String bFeelNamespace = "https://www.omg.org/spec/DMN/20240513/B-FEEL/"; + assertThat(FEELDialect.getStandardFeelDialect(bFeelNamespace)) + .isEmpty(); + String emptyNamespace = ""; + assertThat(FEELDialect.getStandardFeelDialect(emptyNamespace)) + .isEmpty(); + String unknownNameSpace = "whatever-get"; + assertThat(FEELDialect.getStandardFeelDialect(unknownNameSpace)) + .isEmpty(); + String nullNameSpace = null; + assertThat(FEELDialect.getStandardFeelDialect(nullNameSpace)) + .isEmpty(); + } +} \ No newline at end of file