Commit 31058f2e authored by Romain Bioteau's avatar Romain Bioteau
Browse files

Merge pull request #294 from bonitasoft/fix/BS-14308-FormValidationMigration

BS-14308: For Bonita Groovy Validators, the parameter return type is
parents bff3d67f 17636c56
......@@ -35,7 +35,8 @@ Require-Bundle: org.eclipse.core.runtime,
org.bonitasoft.studio.document,
assertj-core;bundle-version="1.5.0";resolution:=optional,
org.junit;bundle-version="4.11.0";resolution:=optional,
org.eclipse.core.databinding
org.eclipse.core.databinding,
org.mockito;bundle-version="1.10.19";resolution:=optional
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-ActivationPolicy: lazy
Export-Package: org.bonitasoft.studio.importer.bar.custom.migration,or
......
/**
* 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.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.ecore.EClass;
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.eclipse.jdt.core.JavaModelException;
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 ValidatorMigrationTest {
@Mock
private Metamodel metamodel;
@Mock
private Model model;
@Mock
private Instance validator;
@Mock
private Instance expression;
@Mock
private EClass validatorEclass;
private ValidatorMigration migrationUnderTest;
@Before
public void setup() throws JavaModelException {
doReturn("uuidValidator").when(validator).getUuid();
final BasicEList<Instance> uniqueValidatorEList = new BasicEList<Instance>();
uniqueValidatorEList.add(validator);
doReturn(uniqueValidatorEList).when(model).getAllInstances("form.Validator");
doReturn("myParam").when(validator).get("parameter");
doReturn(expression).when(model).newInstance("expression.Expression");
doReturn(new BasicEList<Instance>()).when(model).getAllInstances("process.Data");
doReturn(new BasicEList<Instance>()).when(model).getAllInstances("form.Widget");
doReturn(new BasicEList<Instance>()).when(model).getAllInstances("process.Document");
doReturn(validatorEclass).when(validator).getEClass();
migrationUnderTest = spy(new ValidatorMigration());
doNothing().when(migrationUnderTest).buildRepository();
doNothing().when(migrationUnderTest).createValidatorDescriptor(validator);
}
@Test
public void testParameterUpdateToBooleanForBonitaGroovyValidator() throws Exception {
doReturn("org.bonitasoft.GroovyPageValidator").when(validator).get("validatorClass");
migrationUnderTest.migrateBefore(model, metamodel);
migrationUnderTest.migrateAfter(model, metamodel);
verify(expression).set("returnType", Boolean.class.getName());
verify(expression, times(2)).set("returnTypeFixed", true);
verify(migrationUnderTest).addReportChange(anyString(), anyString(), anyString(), anyString(), anyString(), eq(IStatus.OK));
}
@Test
public void testParameterUpdate() throws Exception {
doReturn("another").when(validator).get("validatorClass");
migrationUnderTest.migrateBefore(model, metamodel);
migrationUnderTest.migrateAfter(model, metamodel);
verify(expression, never()).set("returnType", Boolean.class.getName());
verify(expression).set("returnTypeFixed", false);
verify(migrationUnderTest).addReportChange(anyString(), anyString(), anyString(), anyString(), anyString(), eq(IStatus.WARNING));
}
}
/**
* Copyright (C) 2012 BonitaSoft S.A.
* BonitaSoft, 32 rue Gustave Eiffel - 38000 Grenoble
* Copyright (C) 2012-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
......@@ -28,6 +28,7 @@ import org.bonitasoft.studio.validators.descriptor.validator.ValidatorDescriptor
import org.bonitasoft.studio.validators.descriptor.validator.ValidatorFactory;
import org.bonitasoft.studio.validators.descriptor.validator.ValidatorType;
import org.bonitasoft.studio.validators.repository.ValidatorDescriptorRepositoryStore;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.edapt.migration.MigrationException;
import org.eclipse.emf.edapt.spi.migration.Instance;
import org.eclipse.emf.edapt.spi.migration.Metamodel;
......@@ -64,7 +65,7 @@ public class ValidatorMigration extends ReportCustomMigration {
@Override
public void migrateAfter(final Model model, final Metamodel metamodel)
throws MigrationException {
RepositoryManager.getInstance().getCurrentRepository().build(Repository.NULL_PROGRESS_MONITOR);
buildRepository();
for (final Instance validator : model.getAllInstances("form.Validator")) {
setDisplayName(validator, model);
setParameter(validator, model);
......@@ -76,8 +77,12 @@ public class ValidatorMigration extends ReportCustomMigration {
}
}
private void createValidatorDescriptor(final Instance validator) throws JavaModelException {
final String validatorClassName = validator.get("validatorClass");
protected void buildRepository() {
RepositoryManager.getInstance().getCurrentRepository().build(Repository.NULL_PROGRESS_MONITOR);
}
protected void createValidatorDescriptor(final Instance validator) throws JavaModelException {
final String validatorClassName = validator.get("validator Class");
final ValidatorDescriptorRepositoryStore validatorDescriptorStore = RepositoryManager.getInstance().getRepositoryStore(
ValidatorDescriptorRepositoryStore.class);
ValidatorDescriptor descriptor = validatorDescriptorStore.getValidatorDescriptor(validatorClassName);
......@@ -106,36 +111,75 @@ public class ValidatorMigration extends ReportCustomMigration {
}
}
private void setParameter(final Instance validator, final Model model) {
protected void setParameter(final Instance validator, final Model model) {
Instance expression = null;
if (validatorParameters.containsKey(validator.getUuid())) {
String stringToParse = validatorParameters.get(validator.getUuid());
final String className = validator.get("validatorClass");
if (className != null && (className.contains("GroovyPageValidator") || className.contains("GroovyFieldValidator"))) {
final String validatorUUID = validator.getUuid();
if (validatorParameters.containsKey(validatorUUID)) {
String stringToParse = validatorParameters.get(validatorUUID);
String newReturnType = String.class.getName();
boolean returnTypeFixed = false;
if (isBonitaGroovyBasedValidator(validator)) {
stringToParse = stringToParse.startsWith("${") ? stringToParse : "${" + stringToParse + "}";
newReturnType = Boolean.class.getName();
returnTypeFixed = true;
}
expression = getConverter(model, getScope(validator)).parse(stringToParse, String.class.getName(), false);
expression = getConverter(model, getScope(validator)).parse(stringToParse, newReturnType, returnTypeFixed);
if (ExpressionConstants.SCRIPT_TYPE.equals(expression.get("type"))) {
expression.set("name", "parameterScript");
}
addReportChange((String) validator.get("name"), validator.getEClass().getName(), validator.getUuid(),
Messages.validatorParameterMigrationDescription, Messages.validatorsProperty,
StringToExpressionConverter.getStatusForExpression(expression));
final int status = computeStatus(expression, newReturnType);
addReportChange(
(String) validator.get("name"),
validator.getEClass().getName(),
validatorUUID,
Messages.validatorParameterMigrationDescription,
Messages.validatorsProperty,
status);
} else {
expression = StringToExpressionConverter.createExpressionInstance(model, "", "", String.class.getName(), ExpressionConstants.CONSTANT_TYPE, false);
expression = createEmptyParameterExpression(validator, model);
}
validator.set("parameter", expression);
}
protected Instance createEmptyParameterExpression(final Instance validator, final Model model) {
final boolean isBonitaGroovyBasedValidator = isBonitaGroovyBasedValidator(validator);
final String returnType = isBonitaGroovyBasedValidator ? Boolean.class.getName() : String.class.getName();
return StringToExpressionConverter.createExpressionInstance(
model,
"",
"",
returnType,
ExpressionConstants.CONSTANT_TYPE,
isBonitaGroovyBasedValidator);
}
protected int computeStatus(final Instance expression, final String newReturnType) {
int status = StringToExpressionConverter.getStatusForExpression(expression);
if (!Boolean.class.getName().equals(newReturnType)) {
status = IStatus.WARNING;
}
return status;
}
protected boolean isBonitaGroovyBasedValidator(final Instance validator) {
final String className = validator.get("validatorClass");
return className != null
&& (className.contains("GroovyPageValidator") || className.contains("GroovyFieldValidator"));
}
private void setDisplayName(final Instance validator, final Model model) {
Instance expression = null;
if (validatorLabels.containsKey(validator.getUuid())) {
expression = getConverter(model, getScope(validator)).parse(validatorLabels.get(validator.getUuid()), String.class.getName(), true);
final String validatorUUID = validator.getUuid();
if (validatorLabels.containsKey(validatorUUID)) {
expression = getConverter(model, getScope(validator)).parse(validatorLabels.get(validatorUUID), String.class.getName(), true);
if (ExpressionConstants.SCRIPT_TYPE.equals(expression.get("type"))) {
expression.set("name", "errorMessageScript");
}
addReportChange((String) validator.get("name"), validator.getEClass().getName(), validator.getUuid(),
Messages.validatorDisplayNameMigrationDescription, Messages.validatorsProperty,
addReportChange((String) validator.get("name"),
validator.getEClass().getName(),
validatorUUID,
Messages.validatorDisplayNameMigrationDescription,
Messages.validatorsProperty,
StringToExpressionConverter.getStatusForExpression(expression));
} else {
expression = StringToExpressionConverter.createExpressionInstance(model, "", "", String.class.getName(), ExpressionConstants.CONSTANT_TYPE, true);
......
Markdown is supported
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