diff --git a/.gitignore b/.gitignore index 2e2f690..905e7e3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -/app/.phpunit.result.cache /.phpunit.result.cache diff --git a/app/.gitignore b/app/.gitignore index f6ed8cb..cc1eb0b 100644 --- a/app/.gitignore +++ b/app/.gitignore @@ -1,2 +1,3 @@ /.env +/.phpunit.result.cache /phpunit.xml diff --git a/app/src/Service/Mattermost/MattermostService.php b/app/src/Service/Mattermost/MattermostService.php index 0adae23..d72bca1 100644 --- a/app/src/Service/Mattermost/MattermostService.php +++ b/app/src/Service/Mattermost/MattermostService.php @@ -42,8 +42,11 @@ class MattermostService extends BaseService implements MattermostServiceInterfac $string = ''; foreach ($alertmanagerMessage->getAlerts() as $num => $message) { + if ('' !== $string) { + $string .= "\n --- \n"; + } $string .= sprintf( - "message %s\n - description: %s\n - summary: %s\n\n --- ", + "message %s\n - description: %s\n - summary: %s\n", $num, $message->getAnnotations()->getDescription(), $message->getAnnotations()->getSummary() diff --git a/app/tests/Unit/Model/Grafana/GrafanaMessageTest.php b/app/tests/Unit/Model/Grafana/GrafanaMessageTest.php index 183b146..3941ba7 100644 --- a/app/tests/Unit/Model/Grafana/GrafanaMessageTest.php +++ b/app/tests/Unit/Model/Grafana/GrafanaMessageTest.php @@ -251,6 +251,36 @@ final class GrafanaMessageTest extends UnitTester ); } + /** + * @throws ReflectionException + */ + public function testGetRuleUrl(): void + { + $grafanaMessage = new GrafanaMessage(); + $value = $this->faker->sha256(); + + $property = $this->getClassProperty($grafanaMessage, 'ruleUrl'); + $property->setValue($grafanaMessage, $value); + + self::assertEquals($value, $grafanaMessage->getRuleUrl()); + } + + /** + * @throws ReflectionException + */ + public function testSetRuleUrl(): void + { + $grafanaMessage = new GrafanaMessage(); + $value = $this->faker->sha256(); + + self::assertEquals($grafanaMessage, $grafanaMessage->setRuleUrl($value)); + self::assertEquals( + $value, + ($this->getClassProperty($grafanaMessage, 'ruleUrl')) + ->getValue($grafanaMessage) + ); + } + /** * @throws ReflectionException */ diff --git a/app/tests/Unit/Service/Mattermost/MattermostServiceTest.php b/app/tests/Unit/Service/Mattermost/MattermostServiceTest.php index 0d1ab8f..59b1e07 100644 --- a/app/tests/Unit/Service/Mattermost/MattermostServiceTest.php +++ b/app/tests/Unit/Service/Mattermost/MattermostServiceTest.php @@ -4,12 +4,16 @@ declare(strict_types=1); namespace App\Tests\Unit\Service\Mattermost; +use App\Model\Alertmanager\Alert; +use App\Model\Alertmanager\AlertmanagerMessage; +use App\Model\Alertmanager\Annotation; use App\Model\Grafana\GrafanaMessage; use App\Service\BaseService; use App\Service\Mattermost\MattermostService; use App\Service\Mattermost\MattermostServiceInterface; use Faker\Factory; use GuzzleHttp\Client; +use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Psr7\Request; use App\Tests\Unit\UnitTester; use Psr\Http\Message\ResponseInterface; @@ -31,18 +35,118 @@ final class MattermostServiceTest extends UnitTester self::assertInstanceOf(MattermostServiceInterface::class, $stub); } + /** + * @dataProvider sendAlertmanagerMessageDataProvider + * @throws GuzzleException + * @throws \JsonException + * @throws ReflectionException + */ + public function testSendAlertmanagerMessage( + string $mattermostUrl, + string $mattermostUri, + string $channelName, + string $botName, + string $botIcon, + AlertmanagerMessage $alertmanagerMessage, + array $options + ): void + { + $mattermostService = new MattermostService( + $mattermostUrl, $mattermostUri, $channelName, $botName, $botIcon + ); + $clientMock = $this->getMockBuilder(Client::class) + ->disableOriginalConstructor() + ->onlyMethods(['send']) + ->getMock(); + $clientProperty = $this->getClassProperty($mattermostService, 'client'); + $clientProperty->setValue($mattermostService, $clientMock); + $responseMock = $this->getMockForAbstractClass(ResponseInterface::class); + $request = new Request('POST', $mattermostUri); + + $clientMock->expects(self::once()) + ->method('send') + ->with($request, $options) + ->willReturn($responseMock); + + self::assertEquals( + $responseMock, + $mattermostService->sendAlertmanagerMessage($alertmanagerMessage) + ); + } + + /** + * @throws GuzzleException + * @throws \JsonException + */ + public function sendAlertmanagerMessageDataProvider(): array + { + $faker = (Factory::create('en_EN')); + $channelName = $faker->sha256(); + $botName = $faker->sha256(); + $botIcon = $faker->sha256(); + $description = [ + $faker->sha256(), + $faker->sha256() + ]; + $summary = [ + $faker->sha256(), + $faker->sha256() + ]; + + return [ + [ + 'mattermostUrl' => $faker->sha256(), + 'mattermostUri' => $faker->sha256(), + 'channelName' => $channelName, + 'botName' => $botName, + 'botIcon' => $botIcon, + 'message' => + (new AlertmanagerMessage()) + ->setAlerts([ + (new Alert()) + ->setAnnotations( + (new Annotation()) + ->setDescription($description[0]) + ->setSummary($summary[0]) + ), + (new Alert()) + ->setAnnotations( + (new Annotation()) + ->setDescription($description[1]) + ->setSummary($summary[1]) + ) + ] + ), + 'options' => [ + 'headers' => [ + 'Content-Type' => 'application/json', + ], + 'body' => json_encode([ + 'channel' => $channelName, + 'username' => $botName, + 'icon_url' => $botIcon, + 'text' => sprintf( + "message %s\n - description: %s\n - summary: %s\n\n --- \nmessage %s\n - description: %s\n - summary: %s\n", + 0, $description[0], $summary[0], 1, $description[1], $summary[1], + ), + ], JSON_THROW_ON_ERROR), + ] + ] + ]; + } + /** * @dataProvider sendGrafanaMessageDataProvider * @throws ReflectionException */ public function testSendGrafanaMessage( - string $mattermostUrl, - string $mattermostUri, - string $channelName, - string $botName, - string $botIcon, + string $mattermostUrl, + string $mattermostUri, + string $channelName, + string $botName, + string $botIcon, GrafanaMessage $grafanaMessage, - array $options + array $options ): void { $mattermostService = new MattermostService(