Skip to content
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

Merged
merged 4 commits into from
Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
Copy link
Contributor

@baldimir baldimir Dec 14, 2018

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."

Copy link
Contributor Author

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

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.kie.dmn.core.fluent;

import org.drools.core.fluent.impl.BaseBatchFluent;
import org.kie.api.io.Resource;
import org.kie.internal.builder.fluent.CommandBasedExecutable;
import org.kie.internal.builder.fluent.DMNRuntimeFluent;
import org.kie.internal.builder.fluent.ExecutableBuilder;
Expand Down Expand Up @@ -50,8 +49,20 @@ public DMNRuntimeFluent setActiveModel(String namespace, String modelName) {
}

@Override
public DMNRuntimeFluent setActiveModel(Resource resource) {
ctx.addCommand(new SetDMNActiveModelCommand(resource));
public DMNRuntimeFluent setActiveModel(String resourcePath) {
ctx.addCommand(new SetDMNActiveModelCommand(resourcePath));
return this;
}

@Override
public DMNRuntimeFluent getModel(String namespace, String modelName) {
ctx.addCommand(new GetDMNModelCommand(namespace, modelName));
return this;
}

@Override
public DMNRuntimeFluent getModel(String resourcePath) {
ctx.addCommand(new GetDMNModelCommand(resourcePath));
return this;
}

Expand Down
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
Expand Up @@ -16,56 +16,25 @@

package org.kie.dmn.core.fluent;

import java.util.Objects;
import java.util.Optional;

import org.kie.api.command.ExecutableCommand;
import org.kie.api.io.Resource;
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 class SetDMNActiveModelCommand implements ExecutableCommand<DMNModel> {

private String namespace;
private String modelName;
private Resource resource;
public class SetDMNActiveModelCommand extends AbstractDMNModelCommand {

public SetDMNActiveModelCommand(String namespace, String modelName) {
this.namespace = Objects.requireNonNull(namespace, "namespace cannot be null");
this.modelName = Objects.requireNonNull(modelName, "modelName cannot be null");
super(namespace, modelName);
}

public SetDMNActiveModelCommand(Resource resource) {
this.resource = Objects.requireNonNull(resource, "resource cannot be null");
public SetDMNActiveModelCommand(String resourcePath) {
super(resourcePath);
}

@Override
public DMNModel execute(Context context) {
RegistryContext registryContext = (RegistryContext) context;
DMNRuntime dmnRuntime = registryContext.lookup(DMNRuntime.class);
if (dmnRuntime == null) {
throw new IllegalStateException("There is no DMNRuntime available");
}

DMNModel activeModel = retrieveDMNModel(dmnRuntime);
DMNModel activeModel = getDMNModel(context);;
registryContext.register(DMNModel.class, activeModel);
return activeModel;
}

private 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(resource != null) {
return dmnRuntime.getModels().stream()
.filter(model -> resource.getSourcePath().equals(model.getResource().getSourcePath()))
.findFirst()
.orElseThrow(() -> new IllegalStateException("Cannot find a DMN model with resource=" + resource));
}
throw new IllegalStateException("This should not happen");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.kie.api.runtime.ExecutableRunner;
import org.kie.api.runtime.RequestContext;
import org.kie.dmn.api.core.DMNMessage;
import org.kie.dmn.api.core.DMNModel;
import org.kie.dmn.api.core.DMNResult;
import org.kie.dmn.core.util.KieHelper;
import org.kie.internal.builder.fluent.ExecutableBuilder;
Expand All @@ -51,6 +52,8 @@ public void testFluentApi() {
.setInput("a number", 0)
.evaluateModel()
.out("dmnResult")
.getModel("http://www.trisotech.com/definitions/_99ccd4df-41ac-43c3-a563-d58f43149829", "typecheck in DT")
.out("dmnModel")
.getAllContext()
.out("result")
.getMessages()
Expand All @@ -62,9 +65,11 @@ public void testFluentApi() {
Map<String, Object> resultMap = requestContext.getOutput("result");
DMNResult dmnResult = requestContext.getOutput("dmnResult");
List<DMNMessage> messages = requestContext.getOutput("messages");
DMNModel dmnModel = requestContext.getOutput("dmnModel");

assertEquals(47, ((BigDecimal) resultMap.get("an odd decision")).intValue());
assertNotNull(dmnResult);
assertEquals(0, messages.size());
assertNotNull(dmnModel);
}
}
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();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicking (sorry:)): unnecessary empty line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Migrated all these catch(IllegalStateException ignored) to Assertj with a constraint on the actual message


}
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as previous comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Migrated all these catch(IllegalStateException ignored) to Assertj with a constraint on the actual message


}
registryContext.register(DMNRuntime.class, dmnRuntime);

DMNModel dmnModel = getDMNModelCommand.execute(registryContext);
assertEquals(resource.getSourcePath(), dmnModel.getResource().getSourcePath());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

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;
Expand All @@ -35,17 +36,24 @@

public class SetDMNActiveModelCommandTest {

static KieServices ks;
static ReleaseId releaseId;
static Resource resource;
static KieContainer kieContainer;

RegistryContext registryContext;
DMNRuntime dmnRuntime;
Resource resource;

@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() {
KieServices ks = KieServices.Factory.get();
ReleaseId releaseId = ks.newReleaseId("org.kie", "dmn-test-" + UUID.randomUUID(), "1.0");
resource = ks.getResources().newClassPathResource("org/kie/dmn/core/typecheck_in_DT.dmn", this.getClass());
KieContainer kieContainer = KieHelper.getKieContainer(releaseId, resource);

registryContext = new ContextImpl();

dmnRuntime = kieContainer.newKieSession().getKieRuntime(DMNRuntime.class);
Expand All @@ -72,7 +80,7 @@ public void execute() {

@Test
public void executeWithResource() {
SetDMNActiveModelCommand setDMNActiveModelCommand = new SetDMNActiveModelCommand(resource);
SetDMNActiveModelCommand setDMNActiveModelCommand = new SetDMNActiveModelCommand(resource.getSourcePath());

try {
setDMNActiveModelCommand.execute(registryContext);
Expand Down