Skip to content

Commit

Permalink
[incubator-kie-issues#1742] Extend test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriele-Cardosi committed Jan 8, 2025
1 parent 70c7668 commit 8912aeb
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -33,15 +37,41 @@ public enum FEELDialect {

public String getNamespace() {return namespace;}

public final static Set<String> STANDARD_FEEL_URIS = getStandardFEELDialectURIS();

/**
* In current implementation, it returns <code>BFEEL</code> if provided namespace matches, otherwise returns <code>FEEL</code>
* It returns <code>BFEEL</code> if provided namespace matches,
* or <code>FEEL</code> if it matches any of the <code>STANDARD_FEEL_URIS</code>,
* or throws an <code>IllegalArgumentException</code> if no match is found
*
* @param namespace
* @return
*/
public static FEELDialect fromNamespace(String namespace) {
return Arrays.stream(FEELDialect.values())
Optional<FEELDialect> byNamespace = Arrays.stream(FEELDialect.values())
.filter(dialect -> dialect.getNamespace().equals(namespace))
.findFirst()
.orElse(FEELDialect.FEEL);
.findFirst();
if (byNamespace.isPresent()) {
return byNamespace.get();
}
Optional<FEELDialect> fromStandardFeelUris = getStandardFeelDialect(namespace);
if (fromStandardFeelUris.isPresent()) {
return fromStandardFeelUris.get();
}
throw new IllegalArgumentException("Unknown FEEL dialect '" + namespace + "'");
}

static Optional<FEELDialect> getStandardFeelDialect(String namespace) {
return STANDARD_FEEL_URIS.contains(namespace) ? Optional.of(FEEL) : Optional.empty();
}

private static Set<String> getStandardFEELDialectURIS() {
Set<String> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 8912aeb

Please sign in to comment.