Search in sources :

Example 66 with ValidationException

use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-core-public by MangoAutomation.

the class PermissionServiceTest method validatePermission.

private void validatePermission(MangoPermission oldPermission, MangoPermission newPermission) {
    ProcessResult result = new ProcessResult();
    permissionService.validatePermission(result, "permission", Common.getUser(), oldPermission, newPermission);
    if (!result.isValid()) {
        throw new ValidationException(result);
    }
}
Also used : ExpectValidationException(com.infiniteautomation.mango.rules.ExpectValidationException) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult)

Example 67 with ValidationException

use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-core-public by MangoAutomation.

the class SystemPermissionService method update.

/**
 */
public void update(MangoPermission permission, PermissionDefinition def) throws ValidationException {
    Objects.requireNonNull(def, "Permission definition cannot be null");
    permissionService.ensureAdminRole(Common.getUser());
    ProcessResult validation = new ProcessResult();
    if (permission == null) {
        validation.addContextualMessage("permission", "validate.required");
        throw new ValidationException(validation);
    }
    permission.getRoles().stream().flatMap(Collection::stream).forEach(role -> {
        try {
            roleService.get(role.getXid());
        } catch (NotFoundException e) {
            validation.addGenericMessage("validate.role.notFound", role.getXid());
        }
    });
    if (validation.getHasMessages()) {
        throw new ValidationException(validation);
    }
    // Execute in transaction as def.setPermission may make database calls
    dao.doInTransaction(tx -> {
        dao.update(def.getPermissionTypeName(), def.getPermission(), permission);
        def.setPermission(permission);
    });
    this.eventPublisher.publishEvent(new SystemPermissionUpdated(def));
}
Also used : ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException)

Example 68 with ValidationException

use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-core-public by MangoAutomation.

the class MangoTestBase method createMockDataSource.

protected MockDataSourceVO createMockDataSource(String name, String xid, boolean enabled, MangoPermission readPermission, MangoPermission editPermission) {
    DataSourceService service = Common.getBean(DataSourceService.class);
    MockDataSourceVO vo = (MockDataSourceVO) ModuleRegistry.getDataSourceDefinition(MockDataSourceDefinition.TYPE_NAME).baseCreateDataSourceVO();
    vo.setXid(name);
    vo.setName(xid);
    vo.setEnabled(enabled);
    vo.setReadPermission(readPermission);
    vo.setEditPermission(editPermission);
    try {
        return (MockDataSourceVO) service.insert(vo);
    } catch (ValidationException e) {
        fail(e.getValidationErrorMessage(Common.getTranslations()));
        return null;
    }
}
Also used : MockDataSourceVO(com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) DataSourceService(com.infiniteautomation.mango.spring.service.DataSourceService)

Example 69 with ValidationException

use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-core-public by MangoAutomation.

the class MangoTestBase method createMockDataPoint.

protected DataPointVO createMockDataPoint(MockDataSourceVO ds, Consumer<DataPointVO> customizer) {
    DataPointService service = Common.getBean(DataPointService.class);
    DataPointVO dp = new DataPointVO();
    dp.setName(UUID.randomUUID().toString());
    dp.setDeviceName(ds.getName());
    dp.setPointLocator(new MockPointLocatorVO(DataType.NUMERIC, true));
    dp.setDataSourceId(ds.getId());
    customizer.accept(dp);
    try {
        return service.insert(dp);
    } catch (ValidationException e) {
        fail(e.getValidationErrorMessage(Common.getTranslations()));
        return null;
    }
}
Also used : DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) MockPointLocatorVO(com.serotonin.m2m2.vo.dataPoint.MockPointLocatorVO)

Example 70 with ValidationException

use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-core-public by MangoAutomation.

the class MangoTestBase method createMockPublisher.

/**
 * Create a publisher with points
 */
public MockPublisherVO createMockPublisher(boolean enabled, List<MockPublishedPointVO> points) {
    MockPublisherVO publisherVO = (MockPublisherVO) ModuleRegistry.getPublisherDefinition(MockPublisherDefinition.TYPE_NAME).baseCreatePublisherVO();
    publisherVO.setName(UUID.randomUUID().toString());
    publisherVO.setEnabled(enabled);
    PublisherService publisherService = Common.getBean(PublisherService.class);
    try {
        MockPublisherVO pub = (MockPublisherVO) publisherService.insert(publisherVO);
        PublishedPointService publishedPointService = Common.getBean(PublishedPointService.class);
        for (MockPublishedPointVO point : points) {
            publishedPointService.insert(point);
        }
        return pub;
    } catch (ValidationException e) {
        fail(e.getValidationErrorMessage(Common.getTranslations()));
        return null;
    }
}
Also used : ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) PublisherService(com.infiniteautomation.mango.spring.service.PublisherService) PublishedPointService(com.infiniteautomation.mango.spring.service.PublishedPointService) MockPublisherVO(com.serotonin.m2m2.vo.publish.mock.MockPublisherVO) MockPublishedPointVO(com.serotonin.m2m2.vo.publish.mock.MockPublishedPointVO)

Aggregations

ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)75 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)31 JsonException (com.serotonin.json.JsonException)22 NotFoundException (com.infiniteautomation.mango.util.exception.NotFoundException)20 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)16 MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)15 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)15 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)14 JsonValue (com.serotonin.json.type.JsonValue)10 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)9 JsonObject (com.serotonin.json.type.JsonObject)8 DataSourceVO (com.serotonin.m2m2.vo.dataSource.DataSourceVO)8 ProcessMessage (com.serotonin.m2m2.i18n.ProcessMessage)7 DataPointService (com.infiniteautomation.mango.spring.service.DataPointService)6 JsonArray (com.serotonin.json.type.JsonArray)6 DataPointWithEventDetectors (com.serotonin.m2m2.vo.dataPoint.DataPointWithEventDetectors)6 AbstractPointEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO)6 ArrayList (java.util.ArrayList)6 ExpectValidationException (com.infiniteautomation.mango.rules.ExpectValidationException)4 PublishedPointService (com.infiniteautomation.mango.spring.service.PublishedPointService)4