Skip to content

Commit

Permalink
MIGENG-328 Added 'ReasonableDefaults' rules (#37)
Browse files Browse the repository at this point in the history
* MIGENG-328 Added 'ReasonableDefaults' rules

* MIGENG-328 Refactored tests

* MIGENG-328 removed distributionManagement (#4)

* MIGENG-328 Refactored tests

Co-authored-by: Jonathan Vila <[email protected]>
  • Loading branch information
2 people authored and Jonathan Vila committed Jan 21, 2020
1 parent e84c302 commit 670af60
Show file tree
Hide file tree
Showing 24 changed files with 417 additions and 942 deletions.
17 changes: 0 additions & 17 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -195,21 +195,4 @@
</profile>
</profiles>

<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>

<scm>
<connection>scm:git:https://github.com/proj-xavier/xavier-analytics.git</connection>
<url>https://github.com/proj-xavier/xavier-analytics</url>
</scm>


</project>
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public class WorkloadInventoryReportModel
public static final String COMPLEXITY_HARD = "Hard";
public static final String COMPLEXITY_UNKNOWN = "Unknown";

public static final String DATACENTER_DEFAULT_VALUE = "No datacenter defined";
public static final String CLUSTER_DEFAULT_VALUE = "No cluster defined";
public static final String HOST_NAME_DEFAULT_VALUE = "No host defined";

@Id
@GeneratedValue(strategy = javax.persistence.GenerationType.AUTO, generator = "WORKLOADINVENTORYREPORTMODEL_ID_GENERATOR")
private Long id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ rule "Copy basic fields and agenda controller"
when
vmWorkloadInventoryModel : VMWorkloadInventoryModel(
provider != null ,
datacenter != null,
cluster != null,
// These fields became optional in https://issues.redhat.com/browse/MIGENG-328
// datacenter != null,
// cluster != null,
vmName != null,
diskSpace != null,
memory != null,
Expand All @@ -22,12 +23,15 @@ rule "Copy basic fields and agenda controller"
osProductName != null,
product != null,
version != null,
host_name != null,
// This field became optional in https://issues.redhat.com/browse/MIGENG-328
// host_name != null,
scanRunDate != null
)
then
WorkloadInventoryReportModel workloadInventoryReport = new WorkloadInventoryReportModel();
workloadInventoryReport.setProvider(vmWorkloadInventoryModel.getProvider());
// It could be that, since it's an optional input field, we are setting it to null:
// in such a case it will be up to 'ReasonableDefaults' rules it to the default value
workloadInventoryReport.setDatacenter(vmWorkloadInventoryModel.getDatacenter());
workloadInventoryReport.setCluster(vmWorkloadInventoryModel.getCluster());
workloadInventoryReport.setVmName(vmWorkloadInventoryModel.getVmName());
Expand All @@ -46,4 +50,5 @@ rule "Copy basic fields and agenda controller"
kcontext.getKieRuntime().getAgenda().getAgendaGroup("Complexity").setFocus();
kcontext.getKieRuntime().getAgenda().getAgendaGroup("Targets").setFocus();
kcontext.getKieRuntime().getAgenda().getAgendaGroup("Flags").setFocus();
kcontext.getKieRuntime().getAgenda().getAgendaGroup("ReasonableDefaults").setFocus();
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.jboss.xavier.analytics.rules.workload.inventory;

import org.jboss.xavier.analytics.pojo.output.workload.inventory.WorkloadInventoryReportModel

dialect "java"
agenda-group "ReasonableDefaults"
auto-focus true

rule "Fill 'datacenter' field with reasonable default"
when
workloadInventoryReport : WorkloadInventoryReportModel(
datacenter == null
)
then
workloadInventoryReport.setDatacenter(WorkloadInventoryReportModel.DATACENTER_DEFAULT_VALUE);
end

rule "Fill 'cluster' field with reasonable default"
when
workloadInventoryReport : WorkloadInventoryReportModel(
cluster == null
)
then
workloadInventoryReport.setCluster(WorkloadInventoryReportModel.CLUSTER_DEFAULT_VALUE);
end

rule "Fill 'host_name' field with reasonable default"
when
workloadInventoryReport : WorkloadInventoryReportModel(
host_name == null
)
then
workloadInventoryReport.setHost_name(WorkloadInventoryReportModel.HOST_NAME_DEFAULT_VALUE);
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.jboss.xavier.analytics.rules;

import org.junit.After;
import org.jboss.xavier.analytics.test.Utils;
import org.junit.Before;
import org.junit.Test;
import org.kie.api.KieServices;
import org.kie.api.event.rule.AgendaEventListener;
import org.kie.api.event.rule.DebugAgendaEventListener;
Expand All @@ -22,11 +23,17 @@ public abstract class BaseIntegrationTest {

protected AgendaEventListener agendaEventListener;

public BaseIntegrationTest(String kSessionName)
protected final String expectedKiePackageName;
protected final int expectedLoadedRules;


public BaseIntegrationTest(String kSessionName, String expectedKiePackageName, int expectedLoadedRules)
{
this.kSessionName = kSessionName;
// AgendaEventListeners allow one to monitor and check rules that activate, fire, etc
agendaEventListener = mock( AgendaEventListener.class );
this.expectedKiePackageName = expectedKiePackageName;
this.expectedLoadedRules = expectedLoadedRules;
}

@Before
Expand All @@ -39,8 +46,10 @@ public void setup()
kieSession.addEventListener(agendaEventListener);
}

@After
public void tearDown()
// check that the number of rule (from the DRL files) is the number of rules loaded
@Test
public void checkLoadedRulesNumber()
{
Utils.checkLoadedRulesNumber(kieSession, expectedKiePackageName, expectedLoadedRules);
}
}
27 changes: 23 additions & 4 deletions src/test/java/org/jboss/xavier/analytics/rules/BaseTest.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
package org.jboss.xavier.analytics.rules;

import org.jboss.xavier.analytics.test.Utils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.KieRepository;
import org.kie.api.builder.Message;
import org.kie.api.command.Command;
import org.kie.api.event.rule.AgendaEventListener;
import org.kie.api.event.rule.DebugAgendaEventListener;
import org.kie.api.event.rule.DebugRuleRuntimeEventListener;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.StatelessKieSession;
import org.kie.internal.command.CommandFactory;
import org.kie.internal.io.ResourceFactory;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
Expand All @@ -29,19 +33,23 @@ public abstract class BaseTest {

protected final String rulePath;
protected final ResourceType ruleResourceType;
protected final String expectedKiePackageName;
protected final int expectedLoadedRules;

protected StatelessKieSession kieSession;
protected KieFileSystem kieFileSystem;
protected KieServices kieServices;

protected AgendaEventListener agendaEventListener;

public BaseTest(String rulePath, ResourceType resourceType)
public BaseTest(String rulePath, ResourceType resourceType, String expectedKiePackageName, int expectedLoadedRules)
{
this.rulePath = rulePath;
this.ruleResourceType = resourceType;
// AgendaEventListeners allow one to monitor and check rules that activate, fire, etc
agendaEventListener = mock( AgendaEventListener.class );
this.expectedKiePackageName = expectedKiePackageName;
this.expectedLoadedRules = expectedLoadedRules;
}

@Before
Expand All @@ -68,6 +76,13 @@ public void setup()
kieSession.addEventListener(agendaEventListener);
}

// check that the number of rule (from the DRL files) is the number of rules loaded
@Test
public void checkLoadedRulesNumber()
{
Utils.checkLoadedRulesNumber(kieSession, expectedKiePackageName, expectedLoadedRules);
}

protected KieBuilder createAndBuildKieBuilder(URL resource)
{
File ruleFile = new File(resource.getPath());
Expand All @@ -84,8 +99,12 @@ protected void addAgendaGroupRuleToKieFileSystem(KieFileSystem kieFileSystem)
kieFileSystem.write(ResourceFactory.newFileResource(agendaFocusForTestFile).setResourceType(ResourceType.DRL));
}

@After
public void tearDown()
public Map<String, Object> createAndExecuteCommandsAndGetResults(final Map<String, Object> facts)
{
final List<Command> commands = new ArrayList<>();
commands.addAll(Utils.newInsertCommands(facts));
commands.add(CommandFactory.newFireAllRules(NUMBER_OF_FIRED_RULE_KEY));
commands.add(CommandFactory.newGetObjects(GET_OBJECTS_KEY));
return Utils.executeCommandsAndGetResults(kieSession, commands);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@
import org.jboss.xavier.analytics.test.Utils;
import org.junit.Assert;
import org.junit.Test;
import org.kie.api.command.Command;
import org.kie.api.io.ResourceType;
import org.kie.internal.command.CommandFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class EnvironmentTest extends BaseTest {
private static final String CUSTOMER_ID = "123";
Expand All @@ -25,13 +21,12 @@ public class EnvironmentTest extends BaseTest {
private static final int DEFAULT_SOURCE_PRODUCT_INDICATOR = 1;

public EnvironmentTest() {
super("/org/jboss/xavier/analytics/rules/initialcostsaving/Environment.drl", ResourceType.DRL);
super("/org/jboss/xavier/analytics/rules/initialcostsaving/Environment.drl", ResourceType.DRL,
"org.jboss.xavier.analytics.rules.initialcostsaving", 1);
}

@Test
public void test() {
Utils.checkLoadedRulesNumber(kieSession, "org.jboss.xavier.analytics.rules.initialcostsaving", 1);

Map<String, Object> facts = new HashMap<>();

UploadFormInputDataModel inputDataModel = new UploadFormInputDataModel();
Expand All @@ -44,21 +39,12 @@ public void test() {
inputDataModel.setFileName(FILE_NAME);
facts.put("inputDataModel", inputDataModel);

List<Command> commands = new ArrayList<>();
commands.addAll(Utils.newInsertCommands(facts));
commands.add(CommandFactory.newFireAllRules(NUMBER_OF_FIRED_RULE_KEY));
commands.add(CommandFactory.newGetObjects(GET_OBJECTS_KEY));

Map<String, Object> results = Utils.executeCommandsAndGetResults(kieSession, commands);
Map<String, Object> results = createAndExecuteCommandsAndGetResults(facts);

Assert.assertEquals(1, results.get(NUMBER_OF_FIRED_RULE_KEY));
Utils.verifyRulesFiredNames(this.agendaEventListener, "Copy input fields and agenda controller");

List<Object> objects = (List<Object>) results.get((GET_OBJECTS_KEY));
List<InitialSavingsEstimationReportModel> reports = objects.stream()
.filter(object -> object instanceof InitialSavingsEstimationReportModel)
.map(object -> (InitialSavingsEstimationReportModel) object)
.collect(Collectors.toList());
List<InitialSavingsEstimationReportModel> reports = Utils.extractModels(GET_OBJECTS_KEY, results, InitialSavingsEstimationReportModel.class);

// just one report has to be created
Assert.assertEquals(1, reports.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,19 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.hamcrest.CoreMatchers.instanceOf;

public class InitialSavingsEstimationReportTest extends BaseIntegrationTest
{
public InitialSavingsEstimationReportTest()
{
super("InitialCostSavingKSession0");
super("InitialCostSavingKSession0", "org.jboss.xavier.analytics.rules.initialcostsaving", 26);
}

@Test
public void test_SourceNewELAIndicator_0()
{
// check that the numbers of rule from the DRL file is the number of rules loaded
Utils.checkLoadedRulesNumber(kieSession, "org.jboss.xavier.analytics.rules.initialcostsaving", 26);

// create a Map with the facts (i.e. Objects) you want to put in the working memory
Map<String, Object> facts = new HashMap<>();

Expand Down Expand Up @@ -122,11 +118,7 @@ public void test_SourceNewELAIndicator_0()
Assert.assertEquals(true, environmentModel.getOpenStackIndicator());

// Pricing
List<Object> objects = (List<Object>) results.get((GET_OBJECTS_KEY));
List<PricingDataModel> pricingDataModelList = objects.stream()
.filter(object -> object instanceof PricingDataModel)
.map(object -> (PricingDataModel) object)
.collect(Collectors.toList());
List<PricingDataModel> pricingDataModelList = Utils.extractModels(GET_OBJECTS_KEY, results, PricingDataModel.class);

Assert.assertEquals(1, pricingDataModelList.size());
PricingDataModel pricingDataModel = pricingDataModelList.get(0);
Expand Down Expand Up @@ -286,9 +278,6 @@ public void test_SourceNewELAIndicator_0()
@Test @Ignore
public void test_SourceNewELAIndicator_1()
{
// check that the numbers of rule from the DRL file is the number of rules loaded
Utils.checkLoadedRulesNumber(kieSession, "org.jboss.xavier.analytics.rules", 23);

// create a Map with the facts (i.e. Objects) you want to put in the working memory
Map<String, Object> facts = new HashMap<>();
// always add a String fact with the name of the agenda group defined in the DRL file (e.g. "SourceCosts")
Expand Down Expand Up @@ -333,13 +322,9 @@ public void test_SourceNewELAIndicator_1()
// check the names of the rules fired are what you expect
Utils.verifyRulesFiredNames(this.agendaEventListener, "AgendaFocusForTest", "SourceCostsRules_0", "SourceCostsRules_sourceNewELAIndicator_1");

// retrieve the List of Objects that were available in the working memory from the results
List<Object> objects = (List<Object>) results.get((GET_OBJECTS_KEY));
// filter the type of object you're interested in checking (e.g. InitialSavingsEstimationReportModel)
List<InitialSavingsEstimationReportModel> reports = objects.stream()
.filter(object -> object instanceof InitialSavingsEstimationReportModel)
.map(object -> (InitialSavingsEstimationReportModel) object)
.collect(Collectors.toList());
// this method retrieves the List of Objects that were available in the working memory from the results
// and filters the type of object you're interested in retrieving (e.g. InitialSavingsEstimationReportModel)
List<InitialSavingsEstimationReportModel> reports = Utils.extractModels(GET_OBJECTS_KEY, results, InitialSavingsEstimationReportModel.class);

// Check that the number of object is the right one (in this case, there must be just one report)
Assert.assertEquals(1, reports.size());
Expand Down Expand Up @@ -379,9 +364,6 @@ public void test_SourceNewELAIndicator_1()
@Test @Ignore
public void test_SourceNewELAIndicator_2()
{
// check that the numbers of rule from the DRL file is the number of rules loaded
Utils.checkLoadedRulesNumber(kieSession, "org.jboss.xavier.analytics.rules", 23);

// create a Map with the facts (i.e. Objects) you want to put in the working memory
Map<String, Object> facts = new HashMap<>();
// always add a String fact with the name of the agenda group defined in the DRL file (e.g. "SourceCosts")
Expand Down Expand Up @@ -426,13 +408,9 @@ public void test_SourceNewELAIndicator_2()
// check the names of the rules fired are what you expect
Utils.verifyRulesFiredNames(this.agendaEventListener, "AgendaFocusForTest", "SourceCostsRules_0", "SourceCostsRules_sourceNewELAIndicator_2");

// retrieve the List of Objects that were available in the working memory from the results
List<Object> objects = (List<Object>) results.get((GET_OBJECTS_KEY));
// filter the type of object you're interested in checking (e.g. InitialSavingsEstimationReportModel)
List<InitialSavingsEstimationReportModel> reports = objects.stream()
.filter(object -> object instanceof InitialSavingsEstimationReportModel)
.map(object -> (InitialSavingsEstimationReportModel) object)
.collect(Collectors.toList());
// this method retrieves the List of Objects that were available in the working memory from the results
// and filters the type of object you're interested in retrieving (e.g. InitialSavingsEstimationReportModel)
List<InitialSavingsEstimationReportModel> reports = Utils.extractModels(GET_OBJECTS_KEY, results, InitialSavingsEstimationReportModel.class);

// Check that the number of object is the right one (in this case, there must be just one report)
Assert.assertEquals(1, reports.size());
Expand Down
Loading

0 comments on commit 670af60

Please sign in to comment.