Commit e583ae7e authored by Aurelien Pupier's avatar Aurelien Pupier
Browse files

BS-14309: handle more usecase when importing 5.x process

- when default value contains its own name in a String in the script,
don't add the bad depednency on itself
- support script finishing by a variable name and just one character
after
parent 6d6158a7
/**
* Copyright (C) 2015 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.studio.importer.bar.custom.migration;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import org.bonitasoft.studio.common.ExpressionConstants;
import org.bonitasoft.studio.model.process.ProcessPackage;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.edapt.spi.migration.Instance;
import org.eclipse.emf.edapt.spi.migration.Metamodel;
import org.eclipse.emf.edapt.spi.migration.Model;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class DataDefaultValueMigrationTest {
@Mock
private Metamodel metamodel;
@Mock
private Model model;
@Mock
private Instance expression;
private DataDefaultValueMigration migrationUnderTest;
@Before
public void setUp() throws Exception {
doReturn(new BasicEList<Instance>()).when(model).getAllInstances("form.Widget");
doReturn(new BasicEList<Instance>()).when(model).getAllInstances("process.Document");
doReturn(expression).when(model).newInstance("expression.Expression");
doReturn(ExpressionConstants.SCRIPT_TYPE).when(expression).get("type");
doReturn(new BasicEList<Instance>()).when(expression).get("referencedElements");
migrationUnderTest = spy(new DataDefaultValueMigration());
}
@Test
public void testDataDefaultValueWithStringIncludingItSelfDataName() throws Exception {
final BasicEList<Instance> processDatas = new BasicEList<Instance>();
final Instance processData1 = mock(Instance.class);
doReturn("myVar").when(processData1).get("name");
doReturn(false).when(processData1).get("multiple");
final Instance datatType = mock(Instance.class);
doReturn(datatType).when(processData1).get("dataType");
doReturn("${\"script including myVar in a String\"}").when(processData1).get("defaultValue");
doReturn("\"script including myVar in a String\"").when(expression).get("content");
doReturn(ProcessPackage.Literals.DATA_AWARE__DATA).when(processData1).getContainerReference();
doReturn(false).when(processData1).get("transient");
processDatas.add(processData1);
doReturn(processDatas).when(model).getAllInstances("process.Data");
final Instance processInstance = mock(Instance.class);
doReturn(true).when(processInstance).instanceOf("process.AbstractProcess");
doReturn(processDatas).when(processInstance).get("process.Data");
doReturn(processInstance).when(processData1).getContainer();
migrationUnderTest.migrateBefore(model, metamodel);
migrationUnderTest.migrateAfter(model, metamodel);
verify(expression).set("content", "\"script including myVar in a String\"");
verify(expression, never()).add(eq("referencedElements"), any(Instance.class));
}
}
......@@ -64,10 +64,12 @@ public class DataDefaultValueMigration extends ReportCustomMigration {
final String defaultValue = dataDefaultValue.get(uuid);
final StringToExpressionConverter converter = getConverter(model, getScope(data));
final String returnType = StringToExpressionConverter.getDataReturnType(data);
final String dataName = data.get("name");
converter.setDataToIgnore(dataName);
expression = converter.parse(defaultValue, returnType, false);
final String expressionType = expression.get("type");
if(ExpressionConstants.SCRIPT_TYPE.equals(expressionType)){
expression.set("name",data.get("name")+"DefaultValueScript");
expression.set("name",dataName+"DefaultValueScript");
final List<Instance> dependencies = expression.get("referencedElements");
boolean invalidDependency = false ;
for(final Instance dependency : dependencies){
......
......@@ -49,6 +49,8 @@ public class StringToExpressionConverter {
private boolean useSimulationDataScope = false;
private String dataNameToIgnore;
public StringToExpressionConverter(final Model model,
final Instance container) {
Assert.isNotNull(model);
......@@ -388,21 +390,15 @@ public class StringToExpressionConverter {
}
if (currentDataName != null) {
for (final String dataName : data.keySet()) {
if (currentDataName.contains(dataName)) {
if (currentDataName.contains(dataName) && !dataName.equals(dataNameToIgnore)) {
final int index = currentDataName.indexOf(dataName);
final boolean validPrefix = isValidPrefix(currentDataName,
index);
final boolean validSuffix = isValidSuffix(currentDataName,
dataName, index);
final boolean validPrefix = isValidPrefix(currentDataName, index);
final boolean validSuffix = isValidSuffix(currentDataName, dataName, index);
if (validPrefix && validSuffix) {
final Instance dependencyInstance = createVariableDependencyInstance(data
.get(dataName));
final List<Instance> instList = expression
.get("referencedElements");
if (!dependancyAlreadyExists(instList,
dependencyInstance)) {
expression.add("referencedElements",
dependencyInstance);
final Instance dependencyInstance = createVariableDependencyInstance(data.get(dataName));
final List<Instance> instList = expression.get("referencedElements");
if (!dependancyAlreadyExists(instList, dependencyInstance)) {
expression.add("referencedElements", dependencyInstance);
hasAdded = true;
}
}
......@@ -415,7 +411,7 @@ public class StringToExpressionConverter {
private boolean isValidSuffix(final String currentDataName,
final String dataName, final int index) {
boolean validSuffix = false;
if (index + dataName.length() < currentDataName.length() - 1) {
if (index + dataName.length() < currentDataName.length()) {
final String suffix = currentDataName.substring(
index + dataName.length(), index + dataName.length() + 1);
if (!Character.isLetter(suffix.toCharArray()[0])) {
......@@ -598,4 +594,8 @@ public class StringToExpressionConverter {
}
}
public void setDataToIgnore(final String dataName) {
dataNameToIgnore = dataName;
}
}
/**
* Copyright (C) 2015 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.studio.importer.bar.tests.dataDependencies;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNotNull;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import org.bonitasoft.studio.common.jface.FileActionDialog;
import org.bonitasoft.studio.importer.bar.tests.BarImporterTestUtil;
import org.bonitasoft.studio.model.process.Activity;
import org.bonitasoft.studio.model.process.Data;
import org.bonitasoft.studio.model.process.MainProcess;
import org.bonitasoft.studio.model.process.Pool;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.emf.ecore.resource.Resource;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
public class DataDependenciesMigrationIT {
private static boolean disablepopup;
@BeforeClass
public static void disablePopup() {
disablepopup = FileActionDialog.getDisablePopup();
FileActionDialog.setDisablePopup(true);
}
@AfterClass
public static void resetdisablePopup() {
FileActionDialog.setDisablePopup(disablepopup);
}
@Test
public void testDefaultValueContainingVarNameInAString() throws Exception {
final MainProcess mainProcess = importBar("DefaultValueContainingVarNameInAString--1.0.bar");
final Pool pool = (Pool) mainProcess.getElements().get(0);
final Data data = pool.getData().get(0);
assertThat(data.getDefaultValue().getReferencedElements()).isEmpty();
}
@Test
@Ignore("usecase not covered")
public void testDefaultValueContainingAnotherVarNameInAString() throws Exception {
final MainProcess mainProcess = importBar("DefaultValueContainingAnotherVarNameInAString--1.0.bar");
final Pool pool = (Pool) mainProcess.getElements().get(0);
final Data data = pool.getData().get(0);
assertThat(data.getDefaultValue().getReferencedElements()).isEmpty();
final Data data2 = pool.getData().get(1);
assertThat(data2.getDefaultValue().getReferencedElements()).isEmpty();
}
@Test
public void testStepDataInitializedWithProcessData() throws Exception {
final MainProcess mainProcess = importBar("StepDataInitializedWithProcessData--1.0.bar");
final Pool pool = (Pool) mainProcess.getElements().get(0);
final Data data = pool.getData().get(0);
assertThat(data.getDefaultValue().getReferencedElements()).isEmpty();
final Data stepData = ((Activity) pool.getElements().get(0)).getData().get(0);
assertThat(stepData.getDefaultValue().getReferencedElements()).isNotEmpty();
}
protected MainProcess importBar(final String barName) throws IOException, Exception {
final URL fileURL2 = FileLocator.toFileURL(DataDependenciesMigrationIT.class.getResource(barName)); //$NON-NLS-1$
final File migratedProcess = BarImporterTestUtil.migrateBar(fileURL2);
assertNotNull("Fail to migrate bar file", migratedProcess);
assertNotNull("Fail to migrate bar file", migratedProcess.exists());
final Resource resource = BarImporterTestUtil.assertIsLoadable(migratedProcess);
final MainProcess mainProcess = BarImporterTestUtil.getMainProcess(resource);
return mainProcess;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment