Commit 8bb994b6 authored by Roman Ondráček's avatar Roman Ondráček

Config: add scheduler's task validation

Signed-off-by: Roman Ondráček's avatarRoman Ondráček <ondracek.roman@centrum.cz>
parent 0a037818
Pipeline #2289 passed with stages
in 13 minutes and 32 seconds
......@@ -82,6 +82,7 @@ class SchedulerFormFactory {
* @param FormFactory $factory Generic form factory
* @param SchedulerManager $manager Scheduler manager
* @param ServiceManager $serviceManager Service manager
* @param ApiSchemaManager $schemaManager JSON API schema manager
*/
public function __construct(FormFactory $factory, SchedulerManager $manager, ServiceManager $serviceManager, ApiSchemaManager $schemaManager) {
$this->manager = $manager;
......@@ -220,11 +221,11 @@ class SchedulerFormFactory {
try {
$json = Json::decode($value);
$this->schemaManager->setSchemaForRequest($json->mType ?? 'unknown');
$this->schemaManager->validate($json, true);
$this->schemaManager->validate($json);
} catch (JsonException $e) {
$message->addError(self::PREFIX . 'messages.messageInvalidJson');
} catch (NonExistingJsonSchemaException $e) {
$message->addError(self::PREFIX . 'messages.messageInvalidType');
$message->addError(new NotTranslate($e->getMessage()));
} catch (InvalidJsonException $e) {
$message->addError(new NotTranslate($e->getMessage()));
}
......
......@@ -105,10 +105,13 @@ class SchedulerImportFormFactory {
* @var FileUpload $file JSON file
*/
$file = $form->getValues()->file;
if ($file->getContentType() === 'application/json') {
if ($file->getContentType() === 'text/plain' ||
$file->getContentType() === 'application/json') {
$this->importJson($form, $file);
} elseif ($file->getContentType() === 'application/zip') {
$this->importZip($form, $file);
} else {
$this->presenter->flashError(self::TRANSLATOR_PREFIX . '.messages.invalidFile');
}
}
......@@ -129,12 +132,14 @@ class SchedulerImportFormFactory {
$this->manager->save($json);
$this->presenter->flashSuccess(self::TRANSLATOR_PREFIX . '.messages.success');
$this->presenter->redirect('Scheduler:default');
} catch (NonExistingJsonSchemaException $e) {
$this->presenter->flashError('config.messages.writeFailures.nonExistingJsonSchema');
} catch (IOException $e) {
$this->presenter->flashError('config.messages.writeFailures.ioError');
} catch (JsonException $e) {
$this->presenter->flashError('config.messages.writeFailures.invalidJson');
} catch (NonExistingJsonSchemaException $e) {
$this->presenter->flashError($e->getMessage());
} catch (InvalidJsonException $e) {
$this->presenter->flashError($e->getMessage());
}
}
......
......@@ -53,6 +53,11 @@ class SchedulerManager {
*/
private $fileName;
/**
* @var SchedulerSchemaManager Scheduler JSON schema manager
*/
private $schemaManager;
/**
* @var ServiceManager IQRF Gateway Daemon's service manager
*/
......@@ -70,13 +75,15 @@ class SchedulerManager {
* @param TaskTimeManager $timeManager Scheduler's task time specification manager
* @param ServiceManager $serviceManager IQRF Gateway Daemon's service manager
* @param CommandManager $commandManager Command manager
* @param SchedulerSchemaManager $schemaManager Scheduler JSON schema manager
*/
public function __construct(MainManager $mainManager, GenericManager $genericManager, TaskTimeManager $timeManager, ServiceManager $serviceManager, CommandManager $commandManager) {
public function __construct(MainManager $mainManager, GenericManager $genericManager, TaskTimeManager $timeManager, ServiceManager $serviceManager, CommandManager $commandManager, SchedulerSchemaManager $schemaManager) {
$this->genericConfigManager = $genericManager;
$this->serviceManager = $serviceManager;
$this->timeManager = $timeManager;
$path = $mainManager->getCacheDir() . '/scheduler/';
$this->fileManager = new JsonFileManager($path, $commandManager);
$this->schemaManager = $schemaManager;
}
/**
......@@ -246,6 +253,7 @@ class SchedulerManager {
unset($task->message->data->timeout);
}
}
$this->schemaManager->validate($config);
$this->timeManager->cronToArray($config);
$this->fileManager->write($this->fileName, $config);
}
......
......@@ -63,6 +63,7 @@ class SchedulerMigrationManager {
* Constructor
* @param MainManager $mainManager Main configuration manager
* @param ServiceManager $serviceManager Service manager
* @param SchedulerSchemaManager $schemaManager Scheduler JSON schema manager
*/
public function __construct(MainManager $mainManager, ServiceManager $serviceManager, SchedulerSchemaManager $schemaManager) {
$this->configDirectory = $mainManager->getCacheDir() . '/scheduler/';
......
......@@ -41,6 +41,9 @@ class TaskTimeManager {
*/
public function cronToArray(stdClass &$config): void {
$cron = &$config->timeSpec->cronTime;
if (is_array($cron)) {
return;
}
$cron = Strings::replace(Strings::trim($cron), '~\?~', '*');
if (in_array($cron, $this->aliases, true)) {
$cron = [$cron, '', '', '', '', '', ''];
......
......@@ -78,7 +78,7 @@
<button type='button' class='close' data-dismiss='alert'>
<span>&times;</span>
</button>
{_$flash->message}
{_$flash->message|breaklines}
</div>
{/snippet}
{include content}
......
......@@ -14,6 +14,7 @@ use App\ConfigModule\Models\ComponentSchemaManager;
use App\ConfigModule\Models\GenericManager;
use App\ConfigModule\Models\MainManager;
use App\ConfigModule\Models\SchedulerManager;
use App\ConfigModule\Models\SchedulerSchemaManager;
use App\ConfigModule\Models\TaskTimeManager;
use App\CoreModule\Entities\CommandStack;
use App\CoreModule\Models\CommandManager;
......@@ -155,6 +156,9 @@ class SchedulerManagerTest extends TestCase {
Assert::equal($expected, $this->fileManagerTemp->read('1', false));
}
/**
* Constructor
*/
public function __construct() {
$this->array = (object) [
'taskId' => 1,
......@@ -202,8 +206,10 @@ class SchedulerManagerTest extends TestCase {
$mainConfigManagerTemp->shouldReceive('getCacheDir')->andReturn($configTempPath);
$this->timeManager = new TaskTimeManager();
$this->serviceManager = Mockery::mock(ServiceManager::class);
$this->manager = new SchedulerManager($mainConfigManager, $genericConfigManager, $this->timeManager, $this->serviceManager, $commandManager);
$this->managerTemp = new SchedulerManager($mainConfigManagerTemp, $genericConfigManager, $this->timeManager, $this->serviceManager, $commandManager);
$schedulerSchemaManager = Mockery::mock(SchedulerSchemaManager::class);
$schedulerSchemaManager->shouldReceive('validate');
$this->manager = new SchedulerManager($mainConfigManager, $genericConfigManager, $this->timeManager, $this->serviceManager, $commandManager, $schedulerSchemaManager);
$this->managerTemp = new SchedulerManager($mainConfigManagerTemp, $genericConfigManager, $this->timeManager, $this->serviceManager, $commandManager, $schedulerSchemaManager);
}
/**
......
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