Skip to content

Commit

Permalink
GUVNOR-2939: Test the action Otherwise more precisely (apache#422)
Browse files Browse the repository at this point in the history
This PR is consequence of QE effort for internal selenium tests to
community tests migration.
  • Loading branch information
Jozef Marko authored and manstis committed Mar 14, 2017
1 parent 013354c commit 8468dac
Show file tree
Hide file tree
Showing 6 changed files with 2,011 additions and 1,716 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
/*
* Copyright 2017 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.drools.workbench.screens.guided.dtable.backend.server;

import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

import org.drools.workbench.models.datamodel.imports.Import;
import org.drools.workbench.models.datamodel.imports.Imports;
import org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint;
import org.drools.workbench.models.guided.dtable.shared.model.ConditionCol52;
import org.drools.workbench.models.guided.dtable.shared.model.DTCellValue52;
import org.drools.workbench.models.guided.dtable.shared.model.DescriptionCol52;
import org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52;
import org.drools.workbench.models.guided.dtable.shared.model.Pattern52;
import org.drools.workbench.models.guided.dtable.shared.model.RowNumberCol52;
import org.guvnor.common.services.backend.file.FileDiscoveryService;
import org.guvnor.common.services.project.model.Package;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.kie.workbench.common.services.shared.project.KieProjectService;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.uberfire.java.nio.file.FileSystem;
import org.uberfire.java.nio.file.Path;

import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class GuidedDecisionTableSourceServiceTest {

@Mock
Path path;

@Mock
Package packageMock;

@Mock
FileSystem fileSystem;

@Mock
KieProjectService projectService;

@Mock
FileDiscoveryService discoveryService;

GuidedDecisionTable52 model;

@InjectMocks
GuidedDecisionTableSourceService service;

Pattern52 pattern;

ConditionCol52 condition;

List<List<DTCellValue52>> data;

@Before
public void setUp() throws Exception {
// Simulates that no DSL files are present
when(projectService.resolvePackage(any())).thenReturn(packageMock);
when(discoveryService.discoverFiles(any(),
any())).thenReturn(new ArrayList<Path>());
when(fileSystem.supportedFileAttributeViews()).thenReturn(new HashSet<String>());
when(path.getFileSystem()).thenReturn(fileSystem);
when(path.toString()).thenReturn("/");
when(path.getFileName()).thenReturn(path);
when(path.toUri()).thenReturn(new URI("/"));

model = new GuidedDecisionTable52();

model.setPackageName("som.sample");
model.setImports(new Imports(Arrays.asList(new Import("com.sample.Person"))));
model.setRowNumberCol(new RowNumberCol52());
model.setDescriptionCol(new DescriptionCol52());

pattern = new Pattern52();
pattern.setBoundName("$p");
pattern.setFactType("Person");

condition = new ConditionCol52();
condition.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
condition.setHeader("name equals to");
condition.setFactField("name");
condition.setOperator("==");

pattern.setChildColumns(Arrays.asList(condition));

model.setConditionPatterns(Arrays.asList(pattern));

data = new ArrayList<>();
}

@Test
public void testOtherwise() throws Exception {
addRow(1,
"John",
false);
addRow(2,
"Peter",
false);
addRow(3,
null,
true);

model.setData(data);

String source = service.getSource(path,
model);
assertTrue("Expected: name == \"John\"",
source.contains("$p : Person( name == \"John\" )"));
assertTrue("Expected: name == \"Peter\"",
source.contains("$p : Person( name == \"Peter\" )"));
assertTrue("Expected: name not in ( \"John\", \"Peter\" )",
source.contains("$p : Person( name not in ( \"John\", \"Peter\" )"));
}

@Test
public void testOtherwiseTwoSameValues() throws Exception {
addRow(1,
"John",
false);
addRow(2,
"John",
false);
addRow(3,
null,
true);

model.setData(data);

String source = service.getSource(path,
model);
assertTrue("Expected: name not in ( \"John\" )",
source.contains("$p : Person( name not in ( \"John\" )"));
}

@Test
public void testOtherwiseEmptyValue() throws Exception {
addRow(1,
"John",
false);
addRow(2,
"",
false);
addRow(3,
null,
true);

model.setData(data);

String source = service.getSource(path,
model);
assertTrue("Expected: name not in ( \"John\", \"\" )",
source.contains("$p : Person( name not in ( \"John\", \"\" )"));
}

@Test
public void testOtherwiseEmptyAndNullValue() throws Exception {
addRow(1,
"",
false);
addRow(2,
null,
false);
addRow(3,
null,
true);

model.setData(data);

String source = service.getSource(path,
model);
assertTrue("Expected: name not in ( \"\" )",
source.contains("$p : Person( name not in ( \"\" )"));
}

@Test
public void testOtherwiseTwoTimes() throws Exception {
addRow(1,
"John",
false);
addRow(2,
null,
true);
addRow(3,
"Peter",
false);
addRow(4,
null,
true);

model.setData(data);

String source = service.getSource(path,
model);
assertTrue("Expected: name not in ( \"John\", \"Peter\" )",
source.contains("$p : Person( name not in ( \"John\", \"Peter\" )"));
source.replaceFirst("John",
"");
assertTrue("Expected: name not in ( \"John\", \"Peter\" )",
source.contains("$p : Person( name not in ( \"John\", \"Peter\" )"));
}

private void addRow(int rowNumber,
String conditionValue,
boolean isOtherwise) {
data.add(new ArrayList<DTCellValue52>() {{
add(new DTCellValue52(rowNumber));
add(new DTCellValue52("row " + rowNumber));
if (!isOtherwise) {
add(new DTCellValue52(conditionValue));
} else {
DTCellValue52 otherwise = new DTCellValue52();
otherwise.setOtherwise(true);
add(otherwise);
}
}});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@
import javax.enterprise.event.Observes;
import javax.inject.Inject;

import org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint;
import org.drools.workbench.models.guided.dtable.shared.model.BaseColumn;
import org.drools.workbench.models.guided.dtable.shared.model.ConditionCol52;
import org.drools.workbench.models.guided.dtable.shared.model.DescriptionCol52;
import org.drools.workbench.models.guided.dtable.shared.model.RowNumberCol52;
import org.drools.workbench.screens.guided.dtable.client.editor.clipboard.Clipboard;
import org.drools.workbench.screens.guided.dtable.client.resources.i18n.GuidedDecisionTableErraiConstants;
import org.drools.workbench.screens.guided.dtable.client.widget.table.events.cdi.DecisionTableSelectedEvent;
import org.drools.workbench.screens.guided.dtable.client.widget.table.events.cdi.DecisionTableSelectionsChangedEvent;
import org.drools.workbench.screens.guided.dtable.client.widget.table.utilities.ColumnUtilities;
import org.gwtbootstrap3.client.ui.constants.IconType;
import org.jboss.errai.ui.client.local.spi.TranslationService;
import org.uberfire.ext.widgets.common.client.menu.MenuItemFactory;
Expand Down Expand Up @@ -240,7 +239,7 @@ private boolean isOtherwiseEnabled(final List<GridData.SelectedCell> selections)
final GridData.SelectedCell selection = selections.get(0);
final int columnIndex = findUiColumnIndex(selection.getColumnIndex());
final BaseColumn column = activeDecisionTable.getModel().getExpandedColumns().get(columnIndex);
isOtherwiseEnabled = isOtherwiseEnabled && canAcceptOtherwiseValues(column);
isOtherwiseEnabled = isOtherwiseEnabled && ColumnUtilities.canAcceptOtherwiseValues(column);
return isOtherwiseEnabled;
}

Expand All @@ -267,29 +266,4 @@ private int findUiColumnIndex(final int modelColumnIndex) {
}
throw new IllegalStateException("Column was not found!");
}

// Check whether the given column can accept "otherwise" values
private boolean canAcceptOtherwiseValues(final BaseColumn column) {
if (!(column instanceof ConditionCol52)) {
return false;
}
final ConditionCol52 cc = (ConditionCol52) column;

//Check column contains literal values and uses the equals operator
if (cc.getConstraintValueType() != BaseSingleFieldConstraint.TYPE_LITERAL) {
return false;
}

//Check operator is supported
if (cc.getOperator() == null) {
return false;
}
if (cc.getOperator().equals("==")) {
return true;
}
if (cc.getOperator().equals("!=")) {
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.drools.workbench.models.guided.dtable.shared.model.Pattern52;
import org.drools.workbench.screens.guided.dtable.client.widget.table.events.gwt.BoundFactsChangedEvent;
import org.drools.workbench.screens.guided.dtable.client.widget.table.model.synchronizers.ModelSynchronizer;
import org.drools.workbench.screens.guided.dtable.client.widget.table.utilities.ColumnUtilities;

import static org.drools.workbench.screens.guided.dtable.client.widget.table.model.synchronizers.impl.ConditionColumnSynchronizer.PatternConditionMetaData;

Expand Down Expand Up @@ -439,7 +440,7 @@ private void cleanColumnData(final ConditionCol52 originalColumn,
diffs);

//Clear "otherwise" if the column cannot accept them
if (isOperatorUpdated && !canAcceptOtherwiseValues(editedColumn)) {
if (isOperatorUpdated && !ColumnUtilities.canAcceptOtherwiseValues(editedColumn)) {
removeOtherwiseStates(originalColumn);
}

Expand All @@ -456,26 +457,6 @@ private void cleanColumnData(final ConditionCol52 originalColumn,
}
}

// Check whether the given column can accept "otherwise" values
private boolean canAcceptOtherwiseValues(final ConditionCol52 column) {
//Check column contains literal values and uses the equals operator
if (column.getConstraintValueType() != BaseSingleFieldConstraint.TYPE_LITERAL) {
return false;
}

//Check operator is supported
if (column.getOperator() == null) {
return false;
}
if (column.getOperator().equals("==")) {
return true;
}
if (column.getOperator().equals("!=")) {
return true;
}
return false;
}

//Remove Otherwise state from column cells
private void removeOtherwiseStates(final BaseColumn column) {
final int columnIndex = this.model.getExpandedColumns().indexOf(column);
Expand Down
Loading

0 comments on commit 8468dac

Please sign in to comment.