forked from apache/incubator-kie-kogito-runtimes
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[incubator-kie-kogito-runtimes#2608] Generating one definitions json …
…for each DMN model (apache#3424) * [incubator-kie-kogito-runtimes#2608] Generating one definitions json for each DMN model * [incubator-kie-kogito-runtimes#2608] Fix as per PR comment. Add tests. * [incubator-kie-kogito-runtimes#2608] Fix tests. * [incubator-kie-kogito-runtimes#2608] Fix tests. * [incubator-kie-kogito-runtimes#2608] Fix formatting. --------- Co-authored-by: Gabriele-Cardosi <[email protected]>
- Loading branch information
1 parent
6f40793
commit 11f0e5e
Showing
11 changed files
with
436 additions
and
30 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
.../kogito-codegen-decisions/src/main/java/org/kie/kogito/codegen/decision/CodegenUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* 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.kogito.codegen.decision; | ||
|
||
import org.kie.dmn.api.core.DMNModel; | ||
|
||
public class CodegenUtils { | ||
|
||
private CodegenUtils() { | ||
} | ||
|
||
public static String getDefinitionsFileFromModel(DMNModel dmnModel) { | ||
String modelName = geNameForDefinitionsFile(dmnModel); | ||
return modelName.replace(" ", "_").replace(".dmn", ".json"); | ||
} | ||
|
||
static String geNameForDefinitionsFile(DMNModel dmnModel) { | ||
if (dmnModel.getResource() != null && dmnModel.getResource().getSourcePath() != null) { | ||
String resourcePath = dmnModel.getResource().getSourcePath().replace('\\', '/'); | ||
return resourcePath.contains("/") ? resourcePath.substring(resourcePath.lastIndexOf('/') + 1) : resourcePath; | ||
} else { | ||
return dmnModel.getName() + ".dmn"; | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...ito-codegen-decisions/src/test/java/org/kie/kogito/codegen/decision/CodegenUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* 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.kogito.codegen.decision; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileReader; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Collections; | ||
|
||
import org.drools.io.FileSystemResource; | ||
import org.drools.util.FileUtils; | ||
import org.junit.jupiter.api.Test; | ||
import org.kie.api.io.Resource; | ||
import org.kie.dmn.api.core.DMNModel; | ||
import org.kie.dmn.api.core.DMNRuntime; | ||
import org.kie.dmn.core.internal.utils.DMNRuntimeBuilder; | ||
import org.kie.kogito.dmn.DMNKogito; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class CodegenUtilsTest { | ||
|
||
@Test | ||
void getDefinitionsFileFromModelWithSpace() { | ||
File dmnFile = FileUtils.getFile("Traffic Violation.dmn"); | ||
assertNotNull(dmnFile); | ||
assertTrue(dmnFile.exists()); | ||
Resource dmnResource = new FileSystemResource(dmnFile, StandardCharsets.UTF_8.name()); | ||
|
||
DMNRuntime dmnRuntime = DMNRuntimeBuilder.fromDefaults() | ||
.setRootClassLoader(Thread.currentThread().getContextClassLoader()) | ||
.buildConfiguration() | ||
.fromResources(Collections.singleton(dmnResource)) | ||
.getOrElseThrow(e -> new RuntimeException("Error compiling DMN model(s)", e)); | ||
assertThat(dmnRuntime.getModels()).hasSize(1); | ||
final DMNModel dmnModel = dmnRuntime.getModel("https://github.com/kiegroup/drools/kie-dmn/_A4BCA8B8-CF08-433F-93B2-A2598F19ECFF", "Traffic Violation Model Name"); | ||
assertNotNull(dmnModel); | ||
String expected = "Traffic_Violation.json"; | ||
assertEquals(expected, CodegenUtils.getDefinitionsFileFromModel(dmnModel)); | ||
} | ||
|
||
@Test | ||
void geNameForDefinitionsFileWithSourcePath() { | ||
File dmnFile = FileUtils.getFile("Traffic Violation.dmn"); | ||
assertNotNull(dmnFile); | ||
assertTrue(dmnFile.exists()); | ||
Resource dmnResource = new FileSystemResource(dmnFile, StandardCharsets.UTF_8.name()); | ||
DMNRuntime dmnRuntime = DMNRuntimeBuilder.fromDefaults() | ||
.setRootClassLoader(Thread.currentThread().getContextClassLoader()) | ||
.buildConfiguration() | ||
.fromResources(Collections.singleton(dmnResource)) | ||
.getOrElseThrow(e -> new RuntimeException("Error compiling DMN model(s)", e)); | ||
assertThat(dmnRuntime.getModels()).hasSize(1); | ||
final DMNModel dmnModel = dmnRuntime.getModel("https://github.com/kiegroup/drools/kie-dmn/_A4BCA8B8-CF08-433F-93B2-A2598F19ECFF", "Traffic Violation Model Name"); | ||
assertNotNull(dmnModel); | ||
String expected = "Traffic Violation.dmn"; | ||
assertEquals(expected, CodegenUtils.geNameForDefinitionsFile(dmnModel)); | ||
} | ||
|
||
@Test | ||
void geNameForDefinitionsFileWithoutSourcePath() throws FileNotFoundException { | ||
File dmnFile = FileUtils.getFile("Traffic Violation.dmn"); | ||
assertNotNull(dmnFile); | ||
assertTrue(dmnFile.exists()); | ||
DMNRuntime dmnRuntime = DMNKogito.createGenericDMNRuntime(new FileReader(dmnFile)); | ||
assertNotNull(dmnRuntime); | ||
assertThat(dmnRuntime.getModels()).hasSize(1); | ||
final DMNModel dmnModel = dmnRuntime.getModel("https://github.com/kiegroup/drools/kie-dmn/_A4BCA8B8-CF08-433F-93B2-A2598F19ECFF", "Traffic Violation Model Name"); | ||
assertNotNull(dmnModel); | ||
String expected = "Traffic Violation Model Name.dmn"; | ||
assertEquals(expected, CodegenUtils.geNameForDefinitionsFile(dmnModel)); | ||
} | ||
} |
Oops, something went wrong.