Unverified Commit 697de0d9 authored by TAO Xinxiu (Isabelle)'s avatar TAO Xinxiu (Isabelle) Committed by GitHub
Browse files

Add OPTIONAL variables: URL, URI, GLOBAL_FILE, USER_FILE (#3767)

parent 5216be16
......@@ -38,6 +38,10 @@ public class GlobalFileParserValidator extends BaseParserValidator<String> {
super(model, ModelType.GLOBAL_FILE);
}
public GlobalFileParserValidator(String model, ModelType type) throws ModelSyntaxException {
super(model, type);
}
@Override
protected Converter<String> createConverter(String model) throws ModelSyntaxException {
return new IdentityConverter();
......
......@@ -25,8 +25,6 @@
*/
package org.ow2.proactive.scheduler.common.job.factories.spi.model.factory;
import java.net.URI;
import java.net.URL;
import java.util.Date;
......@@ -50,12 +48,16 @@ public enum ModelType {
REGEXP(RegexpParserValidator.class, String.class),
SHORT(ShortParserValidator.class, Short.class),
SPEL(SPELParserValidator.class, String.class),
URI(URIParserValidator.class, URI.class),
URL(URLParserValidator.class, URL.class),
URI(URIParserValidator.class, String.class),
OPTIONAL_URI(OptionalURIParserValidator.class, String.class),
URL(URLParserValidator.class, String.class),
OPTIONAL_URL(OptionalURLParserValidator.class, String.class),
HIDDEN(HiddenParserValidator.class, String.class),
CREDENTIAL(CredentialParserValidator.class, String.class),
USER_FILE(UserFileParserValidator.class, URI.class),
GLOBAL_FILE(GlobalFileParserValidator.class, URI.class);
USER_FILE(UserFileParserValidator.class, String.class),
OPTIONAL_USER_FILE(OptionalUserFileParserValidator.class, String.class),
GLOBAL_FILE(GlobalFileParserValidator.class, String.class),
OPTIONAL_GLOBAL_FILE(OptionalGlobalFileParserValidator.class, String.class);
// The parser validator of the model type
private Class typeParserValidator;
......
/*
* ProActive Parallel Suite(TM):
* The Open Source library for parallel and distributed
* Workflows & Scheduling, Orchestration, Cloud Automation
* and Big Data Analysis on Enterprise Grids & Clouds.
*
* Copyright (c) 2007 - 2017 ActiveEon
* Contact: contact@activeeon.com
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License
* as published by the Free Software Foundation: version 3 of
* the License.
*
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* If needed, contact us to obtain a release under GPL Version 2 or 3
* or a different license than the AGPL.
*/
package org.ow2.proactive.scheduler.common.job.factories.spi.model.factory;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.converter.Converter;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.exceptions.ModelSyntaxException;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.validator.GlobalFileValidator;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.validator.OptionalValidator;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.validator.Validator;
public class OptionalGlobalFileParserValidator extends GlobalFileParserValidator {
public OptionalGlobalFileParserValidator(String model) throws ModelSyntaxException {
super(model, ModelType.OPTIONAL_GLOBAL_FILE);
}
@Override
protected Validator<String> createValidator(String model, Converter<String> converter) {
return new OptionalValidator<>(new GlobalFileValidator());
}
}
/*
* ProActive Parallel Suite(TM):
* The Open Source library for parallel and distributed
* Workflows & Scheduling, Orchestration, Cloud Automation
* and Big Data Analysis on Enterprise Grids & Clouds.
*
* Copyright (c) 2007 - 2017 ActiveEon
* Contact: contact@activeeon.com
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License
* as published by the Free Software Foundation: version 3 of
* the License.
*
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* If needed, contact us to obtain a release under GPL Version 2 or 3
* or a different license than the AGPL.
*/
package org.ow2.proactive.scheduler.common.job.factories.spi.model.factory;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.converter.Converter;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.exceptions.ModelSyntaxException;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.validator.OptionalValidator;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.validator.URIValidator;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.validator.Validator;
public class OptionalURIParserValidator extends URIParserValidator {
public OptionalURIParserValidator(String model) throws ModelSyntaxException {
super(model, ModelType.OPTIONAL_URI);
}
@Override
protected Validator<String> createValidator(String model, Converter<String> converter) {
return new OptionalValidator<>(new URIValidator());
}
}
/*
* ProActive Parallel Suite(TM):
* The Open Source library for parallel and distributed
* Workflows & Scheduling, Orchestration, Cloud Automation
* and Big Data Analysis on Enterprise Grids & Clouds.
*
* Copyright (c) 2007 - 2017 ActiveEon
* Contact: contact@activeeon.com
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License
* as published by the Free Software Foundation: version 3 of
* the License.
*
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* If needed, contact us to obtain a release under GPL Version 2 or 3
* or a different license than the AGPL.
*/
package org.ow2.proactive.scheduler.common.job.factories.spi.model.factory;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.converter.Converter;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.exceptions.ModelSyntaxException;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.validator.OptionalValidator;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.validator.URLValidator;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.validator.Validator;
public class OptionalURLParserValidator extends URLParserValidator {
public OptionalURLParserValidator(String model) throws ModelSyntaxException {
super(model, ModelType.OPTIONAL_URL);
}
@Override
protected Validator<String> createValidator(String model, Converter<String> converter) {
return new OptionalValidator<>(new URLValidator());
}
}
/*
* ProActive Parallel Suite(TM):
* The Open Source library for parallel and distributed
* Workflows & Scheduling, Orchestration, Cloud Automation
* and Big Data Analysis on Enterprise Grids & Clouds.
*
* Copyright (c) 2007 - 2017 ActiveEon
* Contact: contact@activeeon.com
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License
* as published by the Free Software Foundation: version 3 of
* the License.
*
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* If needed, contact us to obtain a release under GPL Version 2 or 3
* or a different license than the AGPL.
*/
package org.ow2.proactive.scheduler.common.job.factories.spi.model.factory;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.converter.Converter;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.exceptions.ModelSyntaxException;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.validator.OptionalValidator;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.validator.UserFileValidator;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.validator.Validator;
public class OptionalUserFileParserValidator extends UserFileParserValidator {
public OptionalUserFileParserValidator(String model) throws ModelSyntaxException {
super(model, ModelType.OPTIONAL_USER_FILE);
}
@Override
protected Validator<String> createValidator(String model, Converter<String> converter) {
return new OptionalValidator<>(new UserFileValidator());
}
}
......@@ -25,28 +25,30 @@
*/
package org.ow2.proactive.scheduler.common.job.factories.spi.model.factory;
import java.net.URI;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.converter.Converter;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.converter.URIConverter;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.converter.IdentityConverter;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.exceptions.ModelSyntaxException;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.validator.AcceptAllValidator;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.validator.URIValidator;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.validator.Validator;
public class URIParserValidator extends BaseParserValidator<URI> {
public class URIParserValidator extends BaseParserValidator<String> {
public URIParserValidator(String model) throws ModelSyntaxException {
super(model, ModelType.URI);
}
public URIParserValidator(String model, ModelType type) throws ModelSyntaxException {
super(model, type);
}
@Override
protected Converter<URI> createConverter(String model) throws ModelSyntaxException {
return new URIConverter();
protected Converter<String> createConverter(String model) throws ModelSyntaxException {
return new IdentityConverter();
}
@Override
protected Validator<URI> createValidator(String model, Converter<URI> converter) throws ModelSyntaxException {
return new AcceptAllValidator<>();
protected Validator<String> createValidator(String model, Converter<String> converter) {
return new URIValidator();
}
}
......@@ -25,28 +25,30 @@
*/
package org.ow2.proactive.scheduler.common.job.factories.spi.model.factory;
import java.net.URL;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.converter.Converter;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.converter.URLConverter;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.converter.IdentityConverter;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.exceptions.ModelSyntaxException;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.validator.AcceptAllValidator;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.validator.URLValidator;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.validator.Validator;
public class URLParserValidator extends BaseParserValidator<URL> {
public class URLParserValidator extends BaseParserValidator<String> {
public URLParserValidator(String model) throws ModelSyntaxException {
super(model, ModelType.URL);
}
public URLParserValidator(String model, ModelType type) throws ModelSyntaxException {
super(model, type);
}
@Override
protected Converter<URL> createConverter(String model) throws ModelSyntaxException {
return new URLConverter();
protected Converter<String> createConverter(String model) throws ModelSyntaxException {
return new IdentityConverter();
}
@Override
protected Validator<URL> createValidator(String model, Converter<URL> converter) throws ModelSyntaxException {
return new AcceptAllValidator<>();
protected Validator<String> createValidator(String model, Converter<String> converter) {
return new URLValidator();
}
}
......@@ -38,6 +38,10 @@ public class UserFileParserValidator extends BaseParserValidator<String> {
super(model, ModelType.USER_FILE);
}
public UserFileParserValidator(String model, ModelType type) throws ModelSyntaxException {
super(model, type);
}
@Override
protected Converter<String> createConverter(String model) throws ModelSyntaxException {
return new IdentityConverter();
......
/*
* ProActive Parallel Suite(TM):
* The Open Source library for parallel and distributed
* Workflows & Scheduling, Orchestration, Cloud Automation
* and Big Data Analysis on Enterprise Grids & Clouds.
*
* Copyright (c) 2007 - 2017 ActiveEon
* Contact: contact@activeeon.com
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License
* as published by the Free Software Foundation: version 3 of
* the License.
*
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* If needed, contact us to obtain a release under GPL Version 2 or 3
* or a different license than the AGPL.
*/
package org.ow2.proactive.scheduler.common.job.factories.spi.model.validator;
import org.apache.commons.lang3.StringUtils;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.ModelValidatorContext;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.exceptions.ValidationException;
/**
* Validate all the blank parameter value. When the value is not blank, use its specific validator to check the value validity.
* @param <T>
*/
public class OptionalValidator<T> implements Validator<T> {
Validator<T> validator;
public OptionalValidator(Validator<T> validator) {
this.validator = validator;
}
@Override
public T validate(T parameterValue, ModelValidatorContext context) throws ValidationException {
// When the parameter value is not provided, it's validated. Otherwise, use its proper validator
if (StringUtils.isBlank(parameterValue.toString())) {
return parameterValue;
} else {
return validator.validate(parameterValue, context);
}
}
}
......@@ -23,21 +23,27 @@
* If needed, contact us to obtain a release under GPL Version 2 or 3
* or a different license than the AGPL.
*/
package org.ow2.proactive.scheduler.common.job.factories.spi.model.converter;
package org.ow2.proactive.scheduler.common.job.factories.spi.model.validator;
import java.net.URI;
import java.net.URISyntaxException;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.exceptions.ConversionException;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.ModelValidatorContext;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.exceptions.ValidationException;
public class URIConverter implements Converter<URI> {
/**
* @author ActiveEon Team
* @since 19/08/19s
*/
public class URIValidator implements Validator<String> {
@Override
public URI convert(String parameterValue) throws ConversionException {
public String validate(String parameterValue, ModelValidatorContext context) throws ValidationException {
try {
return new URI(parameterValue);
return new URI(parameterValue).toString();
} catch (URISyntaxException e) {
throw new ConversionException(parameterValue, URI.class, e.getMessage(), e);
throw new ValidationException(parameterValue + " is not a valid URI.", e);
}
}
}
......@@ -23,21 +23,27 @@
* If needed, contact us to obtain a release under GPL Version 2 or 3
* or a different license than the AGPL.
*/
package org.ow2.proactive.scheduler.common.job.factories.spi.model.converter;
package org.ow2.proactive.scheduler.common.job.factories.spi.model.validator;
import java.net.MalformedURLException;
import java.net.URL;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.exceptions.ConversionException;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.ModelValidatorContext;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.exceptions.ValidationException;
public class URLConverter implements Converter<URL> {
/**
* @author ActiveEon Team
* @since 19/08/19s
*/
public class URLValidator implements Validator<String> {
@Override
public URL convert(String parameterValue) throws ConversionException {
public String validate(String parameterValue, ModelValidatorContext context) throws ValidationException {
try {
return new URL(parameterValue);
return new URL(parameterValue).toString();
} catch (MalformedURLException e) {
throw new ConversionException(parameterValue, URL.class, e.getMessage(), e);
throw new ValidationException(parameterValue + " is not a valid URL.", e);
}
}
}
/*
* ProActive Parallel Suite(TM):
* The Open Source library for parallel and distributed
* Workflows & Scheduling, Orchestration, Cloud Automation
* and Big Data Analysis on Enterprise Grids & Clouds.
*
* Copyright (c) 2007 - 2017 ActiveEon
* Contact: contact@activeeon.com
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License
* as published by the Free Software Foundation: version 3 of
* the License.
*
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* If needed, contact us to obtain a release under GPL Version 2 or 3
* or a different license than the AGPL.
*/
package org.ow2.proactive.scheduler.common.job.factories.spi.model.factory;
import static org.mockito.Mockito.when;
import static org.ow2.proactive.scheduler.common.SchedulerConstants.GLOBALSPACE_NAME;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.ow2.proactive.scheduler.common.SchedulerSpaceInterface;
import org.ow2.proactive.scheduler.common.exception.NotConnectedException;
import org.ow2.proactive.scheduler.common.exception.PermissionException;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.ModelValidatorContext;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.exceptions.ConversionException;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.exceptions.ModelSyntaxException;
import org.ow2.proactive.scheduler.common.job.factories.spi.model.exceptions.ValidationException;
public class GlobalFileParserValidatorTest {
final String existGlobalFilePath = "global folder/exist-file.txt";
final String notExistGlobalFilePath = "not-exist-file.png";
@Mock
private ModelValidatorContext context;
@Mock
private SchedulerSpaceInterface schedulerSpaceInterface;
@Before
public void init() throws NotConnectedException, PermissionException {
MockitoAnnotations.initMocks(this);
when(context.getSpace()).thenReturn(schedulerSpaceInterface);
when(schedulerSpaceInterface.checkFileExists(GLOBALSPACE_NAME, existGlobalFilePath)).thenReturn(true);
when(schedulerSpaceInterface.checkFileExists(GLOBALSPACE_NAME, notExistGlobalFilePath)).thenReturn(false);
}
@Test
public void testThatValidGlobalFileIsOk() throws ModelSyntaxException, ValidationException, ConversionException {
String value = existGlobalFilePath;
GlobalFileParserValidator parserValidator = new GlobalFileParserValidator(ModelType.GLOBAL_FILE.name());
Assert.assertEquals(value, parserValidator.parseAndValidate(value, context));
}
@Test(expected = ValidationException.class)
public void testThatInvalidGlobalFileThrowException()
throws ModelSyntaxException, ValidationException, ConversionException {
GlobalFileParserValidator parserValidator = new GlobalFileParserValidator(ModelType.GLOBAL_FILE.name());
parserValidator.parseAndValidate(notExistGlobalFilePath, context);
}
@Test(expected = ValidationException.class)
public void testThatEmptyGlobalFileThrowException()
throws ModelSyntaxException, ValidationException, ConversionException {
String value = "";
GlobalFileParserValidator parserValidator = new GlobalFileParserValidator(ModelType.GLOBAL_FILE.name());
parserValidator.parseAndValidate(value, context);
}
// when context is not specified, the parserValidator is expected to not check global file existence.
@Test
public void testThatGivenNoContextEmptyGlobalFileIsOK()
throws ModelSyntaxException, ValidationException, ConversionException {
String value = "";
GlobalFileParserValidator parserValidator = new GlobalFileParserValidator(ModelType.GLOBAL_FILE.name());
Assert.assertEquals(value, parserValidator.parseAndValidate(value, null));