grafana -> notification-provider -> mattermost | see https://gitlab.fedy95.com/dev/notification-provider
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

216 lines
7.4 KiB

<?php
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;
use ReflectionException;
final class MattermostServiceTest extends UnitTester
{
public function testIsExtends(): void
{
$stub = $this->createStub(MattermostService::class);
self::assertTrue(is_subclass_of($stub, BaseService::class));
}
public function testIsImplements(): void
{
$stub = $this->createStub(MattermostService::class);
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,
GrafanaMessage $grafanaMessage,
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->sendGrafanaMessage($grafanaMessage)
);
}
/**
* @throws \JsonException
*/
public function sendGrafanaMessageDataProvider(): array
{
$faker = (Factory::create('en_EN'));
$channelName = $faker->sha256();
$botName = $faker->sha256();
$botIcon = $faker->sha256();
$title = $faker->sha256();
$message = $faker->sha256();
$imageUrl = $faker->sha256();
return [
[
'mattermostUrl' => $faker->sha256(),
'mattermostUri' => $faker->sha256(),
'channelName' => $channelName,
'botName' => $botName,
'botIcon' => $botIcon,
'message' =>
(new GrafanaMessage())
->setTitle($title)
->setMessage($message)
->setImageUrl($imageUrl),
'options' => [
'headers' => [
'Content-Type' => 'application/json',
],
'body' => json_encode([
'channel' => $channelName,
'username' => $botName,
'icon_url' => $botIcon,
'text' => sprintf("%s\n %s\n %s\n\n",
$title, $message, $imageUrl
),
], JSON_THROW_ON_ERROR),
]
]
];
}
}