-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DROOLS-3368] Extend scenario runner to support DMN runtime #2194
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2018 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed 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.core.fluent; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
import org.kie.api.command.ExecutableCommand; | ||
import org.kie.api.runtime.Context; | ||
import org.kie.dmn.api.core.DMNModel; | ||
import org.kie.dmn.api.core.DMNRuntime; | ||
import org.kie.internal.command.RegistryContext; | ||
|
||
public abstract class AbstractDMNModelCommand implements ExecutableCommand<DMNModel> { | ||
|
||
protected String namespace; | ||
protected String modelName; | ||
protected String resourcePath; | ||
|
||
public AbstractDMNModelCommand(String namespace, String modelName) { | ||
this.namespace = Objects.requireNonNull(namespace, "namespace cannot be null"); | ||
this.modelName = Objects.requireNonNull(modelName, "modelName cannot be null"); | ||
} | ||
|
||
public AbstractDMNModelCommand(String resourcePath) { | ||
this.resourcePath = Objects.requireNonNull(resourcePath, "resource cannot be null"); | ||
} | ||
|
||
protected DMNModel getDMNModel(Context context) { | ||
RegistryContext registryContext = (RegistryContext) context; | ||
DMNRuntime dmnRuntime = registryContext.lookup(DMNRuntime.class); | ||
if (dmnRuntime == null) { | ||
throw new IllegalStateException("There is no DMNRuntime available"); | ||
} | ||
|
||
return retrieveDMNModel(dmnRuntime); | ||
} | ||
|
||
protected DMNModel retrieveDMNModel(DMNRuntime dmnRuntime) { | ||
if(namespace != null && modelName != null) { | ||
return Optional | ||
.ofNullable(dmnRuntime.getModel(namespace, modelName)) | ||
.orElseThrow(() -> new IllegalStateException("Cannot find a DMN model with namespace=" + namespace + " and modelName=" + modelName)); | ||
} | ||
else if(resourcePath != null) { | ||
return dmnRuntime.getModels().stream() | ||
.filter(model -> Objects.equals(resourcePath, model.getResource().getSourcePath())) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalStateException("Cannot find a DMN model with resource=" + resourcePath)); | ||
} | ||
throw new IllegalStateException("This should not happen"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2018 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed 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.core.fluent; | ||
|
||
import org.kie.api.runtime.Context; | ||
import org.kie.dmn.api.core.DMNModel; | ||
|
||
public class GetDMNModelCommand extends AbstractDMNModelCommand { | ||
|
||
public GetDMNModelCommand(String namespace, String modelName) { | ||
super(namespace, modelName); | ||
} | ||
|
||
public GetDMNModelCommand(String resourcePath) { | ||
super(resourcePath); | ||
} | ||
|
||
@Override | ||
public DMNModel execute(Context context) { | ||
return getDMNModel(context); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright 2018 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed 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.core.fluent; | ||
|
||
import java.util.UUID; | ||
|
||
import org.drools.core.command.impl.ContextImpl; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.kie.api.KieServices; | ||
import org.kie.api.builder.ReleaseId; | ||
import org.kie.api.io.Resource; | ||
import org.kie.api.runtime.KieContainer; | ||
import org.kie.dmn.api.core.DMNModel; | ||
import org.kie.dmn.api.core.DMNRuntime; | ||
import org.kie.dmn.core.util.KieHelper; | ||
import org.kie.internal.command.RegistryContext; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.fail; | ||
|
||
public class GetDMNModelCommandTest { | ||
|
||
static KieServices ks; | ||
static ReleaseId releaseId; | ||
static Resource resource; | ||
static KieContainer kieContainer; | ||
|
||
RegistryContext registryContext; | ||
DMNRuntime dmnRuntime; | ||
|
||
@BeforeClass | ||
public static void staticInit() { | ||
ks = KieServices.Factory.get(); | ||
releaseId = ks.newReleaseId("org.kie", "dmn-test-" + UUID.randomUUID(), "1.0"); | ||
resource = ks.getResources().newClassPathResource("org/kie/dmn/core/typecheck_in_DT.dmn", GetDMNModelCommandTest.class); | ||
kieContainer = KieHelper.getKieContainer(releaseId, resource); | ||
} | ||
|
||
@Before | ||
public void init() { | ||
registryContext = new ContextImpl(); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpicking (sorry:)): unnecessary empty line. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
dmnRuntime = kieContainer.newKieSession().getKieRuntime(DMNRuntime.class); | ||
} | ||
|
||
@Test | ||
public void execute() { | ||
String namespace = "http://www.trisotech.com/definitions/_99ccd4df-41ac-43c3-a563-d58f43149829"; | ||
String modelName = "typecheck in DT"; | ||
GetDMNModelCommand getDMNModelCommand = new GetDMNModelCommand(namespace, modelName); | ||
|
||
try { | ||
getDMNModelCommand.execute(registryContext); | ||
fail(); | ||
} catch (IllegalStateException ignored) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this exception a correct state? This looks incorrect. This kind of catches in tests could hide potential bugs in the future. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Migrated all these |
||
|
||
} | ||
registryContext.register(DMNRuntime.class, dmnRuntime); | ||
|
||
DMNModel dmnModel = getDMNModelCommand.execute(registryContext); | ||
assertEquals(namespace, dmnModel.getNamespace()); | ||
assertEquals(modelName, dmnModel.getName()); | ||
} | ||
|
||
@Test | ||
public void executeWithResource() { | ||
GetDMNModelCommand getDMNModelCommand = new GetDMNModelCommand(resource.getSourcePath()); | ||
|
||
try { | ||
getDMNModelCommand.execute(registryContext); | ||
fail(); | ||
} catch (IllegalStateException ignored) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as previous comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Migrated all these |
||
|
||
} | ||
registryContext.register(DMNRuntime.class, dmnRuntime); | ||
|
||
DMNModel dmnModel = getDMNModelCommand.execute(registryContext); | ||
assertEquals(resource.getSourcePath(), dmnModel.getResource().getSourcePath()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see this was just moved here, however please change the error message here. This doesn't say anything. E.g. "Cannot retrieve the model! Resource path or model name is not set properly."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to "Both resourcePath and namespace+modelName are not set, this should not happen". It is not possible that both are
null
, it is just to have an error if in the future the logic will change without update this method