<commit_before><?php
/**
 * @package syrup-component-bundle
 * @copyright 2015 Keboola
 * @author <NAME> <<EMAIL>>
 */

namespace Keboola\Syrup\Tests\Listener;

use Keboola\DebugLogUploader\UploaderS3;
use Keboola\StorageApi\Client;
use Keboola\StorageApi\ClientException;
use Keboola\StorageApi\MaintenanceException;
use Monolog\Handler\TestHandler;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Keboola\Syrup\Command\JobCommand;
use Keboola\Syrup\Exception\UserException;
use Keboola\Syrup\Listener\SyrupExceptionListener;
use Keboola\Syrup\Monolog\Formatter\JsonFormatter;
use Keboola\Syrup\Monolog\Processor\SyslogProcessor;
use Keboola\Syrup\Service\StorageApi\StorageApiService;

class SyrupExceptionListenerTest extends KernelTestCase
{
    /**
     * @var TestHandler
     */
    protected $testLogHandler;
    /**
     * @var SyrupExceptionListener
     */
    protected $listener;

    public function setUp()
    {
        static::bootKernel();

        $request = new Request();
        $request->headers->add(['X-StorageApi-Token' => SAPI_TOKEN]);

        $requestStack = new RequestStack();
        $requestStack->push($request);

        $storageApiService = new StorageApiService($requestStack, SAPI_URL);

        $uploader = new UploaderS3([
            'aws-access-key' => AWS_ACCESS_KEY_ID,
            'aws-secret-key' => AWS_SECRET_ACCESS_KEY,
            's3-upload-path' => AWS_S3_BUCKET . AWS_S3_BUCKET_LOGS_PATH,
            'aws-region' => AWS_REGION,
            'url-prefix' => 'https://connection.keboola.com/admin/utils/logs?file=',
        ]);
        $this->testLogHandler = new TestHandler();
        $this->testLogHandler->setFormatter(new JsonFormatter());
        $this->testLogHandler->pushProcessor(new SyslogProcessor(SYRUP_APP_NAME, $storageApiService, $uploader));
        $logger = new \Monolog\Logger('test', [$this->testLogHandler]);
        $this->listener = new SyrupExceptionListener(SYRUP_APP_NAME, $storageApiService, $logger);
    }

    public function testConsoleException()
    {
        $command = new JobCommand();
        $input = new ArrayInput([]);
        $output = new NullOutput();

        $message = uniqid();
        $level = 500;
        $event = new ConsoleExceptionEvent($command, $input, $output, new \Exception($message), $level);
        $this->listener->onConsoleException($event);
        $records = $this->testLogHandler->getRecords();
        $this->assertCount(1, $records);
        $record = current($records);
        $this->assertArrayHasKey('message', $record);
        $this->assertEquals($message, $record['message']);
        $this->assertArrayHasKey('level', $record);
        $this->assertEquals($level, $record['level']);
        $this->assertArrayHasKey('exceptionId', $record);
        $this->assertArrayHasKey('exception', $record);
        $this->assertArrayHasKey('message', $record['exception']);
        $this->assertArrayHasKey('code', $record['exception']);
        $this->assertArrayHasKey('attachment', $record['exception']);<commit_msg>tests(SyrupExceptionListener): sapi maintenance handling<commit_after><?php
/**
 * @package syrup-component-bundle
 * @copyright 2015 Keboola
 * @author <NAME> <<EMAIL>>
 */

namespace Keboola\Syrup\Tests\Listener;

use Keboola\DebugLogUploader\UploaderS3;
use Keboola\StorageApi\ClientException;
use Keboola\StorageApi\MaintenanceException;
use Monolog\Handler\TestHandler;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Keboola\Syrup\Command\JobCommand;
use Keboola\Syrup\Exception\UserException;
use Keboola\Syrup\Listener\SyrupExceptionListener;
use Keboola\Syrup\Monolog\Formatter\JsonFormatter;
use Keboola\Syrup\Monolog\Processor\SyslogProcessor;
use Keboola\Syrup\Service\StorageApi\StorageApiService;

class SyrupExceptionListenerTest extends KernelTestCase
{
    /**
     * @var TestHandler
     */
    protected $testLogHandler;
    /**
     * @var SyrupExceptionListener
     */
    protected $listener;

    public function setUp()
    {
        static::bootKernel();

        $request = new Request();
        $request->headers->add(['X-StorageApi-Token' => SAPI_TOKEN]);

        $requestStack = new RequestStack();
        $requestStack->push($request);

        $storageApiService = new StorageApiService($requestStack, SAPI_URL);

        $uploader = new UploaderS3([
            'aws-access-key' => AWS_ACCESS_KEY_ID,
            'aws-secret-key' => AWS_SECRET_ACCESS_KEY,
            's3-upload-path' => AWS_S3_BUCKET . AWS_S3_BUCKET_LOGS_PATH,
            'aws-region' => AWS_REGION,
            'url-prefix' => 'https://connection.keboola.com/admin/utils/logs?file=',
        ]);
        $this->testLogHandler = new TestHandler();
        $this->testLogHandler->setFormatter(new JsonFormatter());
        $this->testLogHandler->pushProcessor(new SyslogProcessor(SYRUP_APP_NAME, $storageApiService, $uploader));
        $logger = new \Monolog\Logger('test', [$this->testLogHandler]);
        $this->listener = new SyrupExceptionListener(SYRUP_APP_NAME, $storageApiService, $logger);
    }

    public function testMaintenance()
    {
        $request = new Request();
        $request->headers->add(['X-StorageApi-Token' => SAPI_TOKEN]);

        $requestStack = new RequestStack();
        $requestStack->push($request);

        $storageApiService = new StorageApiService($requestStack, 'https://maintenance-testing.us-east-1.keboola.com');

        $uploader = new UploaderS3([
            'aws-access-key' => AWS_ACCESS_KEY_ID,
            'aws-secret-key' => AWS_SECRET_ACCESS_KEY,
            's3-upload-path' => AWS_S3_BUCKET . AWS_S3_BUCKET_LOGS_PATH,
            'aws-region' => AWS_REGION,
            'url-prefix' => 'https://connection.keboola.com/admin/utils/logs?file=',
        ]);
        $testLogHandler = new TestHandler();
        $testLogHandler->setFormatter(new JsonFormatter());
        $testLogHandler->pushProcessor(new SyslogProcessor(SYRUP_APP_NAME, $storageApiService, $uploader));
        $logger = new \Monolog\Logger('test', [$testLogHandler]);

        $listener = new SyrupExceptionListener(SYRUP_APP_NAME, $storageApiService, $logger);
        $this->assertTrue($listener instanceof SyrupExceptionListener);
    }

    public function testConsoleException()
    {
        $command = new JobCommand();
        $input = new ArrayInput([]);
        $output = new NullOutput();

        $message = uniqid();
        $level = 500;
        $event = new ConsoleExceptionEvent($command, $input, $output, new \Exception($message), $level);
        $this->listener->onConsoleException($event);
        $records = $this->testLogHandler->getRecords();
        $this->assertCount(1, $records);
        $record = current($records);
        $this->assertArrayHasKey('message', $record);
        $this->assertEquals($message, $record['message']);
        $this->assertArrayHasKey('level', $record);
        $this->assertEquals($level, $record['level']);
        $this->assertArrayHasKey('exceptionId', $record);
        $this->assertArrayHasKey('exception', $record);
        $this->assertArrayHasKey('message', $record['exception']);
        $this->assertArrayHasKey('code', $record['exception']);
        $this->assertArrayHasKey('attachment', $record['exception']);
{'fraction_non_alphanumeric': 0.13763869841665627, 'fraction_numerical': 0.0038648547562648047, 'mean_word_length': 3.777843954734961, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 81, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7587869', 'n_tokens_mistral': 2380, 'n_tokens_neox': 2367, 'n_words': 369}