Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure api client Event grid, Service bus #102

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add functional tests
  • Loading branch information
zajca committed Jun 1, 2023
commit cc117aec43a5f354299bff334c650053af9a8a07
10 changes: 10 additions & 0 deletions libs/azure-api-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ If even this is not enough for your use-case, you can implement your own

## Development

### Resources
To run functional tests (PHPUnit group "functional") you have to setup resources in Azure.

Requirements:
Expand All @@ -147,6 +148,15 @@ terraform -chdir="provisioning" init
terraform -chdir="provisioning" apply -var name_prefix=<respource_group_prefix> -var az_subscription_id=<az_subscription_id> -var az_tenant_id=<az_tenant_id>
terraform -chdir="provisioning" output -json | jq -r 'keys[] as $k | "\($k)=\(.[$k] | .value)"' >| .env
```
### Tests

```bash
# Run only unit tests
docker-compose run --rm dev-azure-api-client composer tests -- --exclude=functional

# Run only functional tests
docker-compose run --rm dev-azure-api-client composer tests -- --group=functional
```

## License

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);

namespace Keboola\AzureApiClient\Tests\EventGrid;

use DateTimeImmutable;
use Keboola\AzureApiClient\ApiClientConfiguration;
use Keboola\AzureApiClient\Authentication\Authenticator\CustomHeaderAuth;
use Keboola\AzureApiClient\Authentication\Authenticator\SASTokenAuthenticatorFactory;
use Keboola\AzureApiClient\EventGrid\EventGridApiClient;
use Keboola\AzureApiClient\EventGrid\Model\EventGridEvent;
use Keboola\AzureApiClient\Json;
use Keboola\AzureApiClient\ServiceBus\ServiceBusApiClient;
use Keboola\AzureApiClient\Tests\ReflectionPropertyAccessTestCase;
use PHPUnit\Framework\TestCase;

/**
* @group functional
*/
class EventGridApiClientFunctionalTest extends TestCase
{
use ReflectionPropertyAccessTestCase;

private function getClient(): EventGridApiClient
{
return new EventGridApiClient(
getenv('AZURE_API_CLIENT_CI__EVENT_GRID__TOPIC_HOSTNAME'),
new ApiClientConfiguration(
authenticator: new CustomHeaderAuth(
header: 'aeg-sas-key',
value: getenv('AZURE_API_CLIENT_CI__EVENT_GRID__ACCESS_KEY'),
),
)
);
}

private function getServiceBusClient(): ServiceBusApiClient
{
$endpoint = getenv('AZURE_API_CLIENT_CI__SERVICE_BUS__ENDPOINT');
return new ServiceBusApiClient(
serviceBusEndpoint: $endpoint,
configuration: new ApiClientConfiguration(
authenticator: new SASTokenAuthenticatorFactory(
url: $endpoint,
sharedAccessKeyName: 'RootManageSharedAccessKey',
sharedAccessKey: getenv('AZURE_API_CLIENT_CI__SERVICE_BUS__SHARED_ACCESS_KEY'),
),
)
);
}

public function testPublishEvent(): void
{
$client = $this->getClient();
$client->publishEvents([
new EventGridEvent(
'3e9825ed-b7db-44ea-a5c9-a1601fa43e23',
'TestSubject',
[
'Property1' => 'Value1',
'Property2' => 'Value2',
],
'Keboola.EventGridClient.TestEvent'
),
]);

$serviceBusClient = $this->getServiceBusClient();
$message = $serviceBusClient->peakMessage('queue-tests');
$this->assertNotNull($message);
// returned message has id assigned by service bus
self::assertNotSame('3e9825ed-b7db-44ea-a5c9-a1601fa43e23', $message->id);
$messageBody = Json::decodeArray($message->body);
self::assertSame('3e9825ed-b7db-44ea-a5c9-a1601fa43e23', $messageBody['id']);
self::assertSame('TestSubject', $messageBody['subject']);
self::assertSame([
'Property1' => 'Value1',
'Property2' => 'Value2',
], $messageBody['data']);
self::assertSame('Keboola.EventGridClient.TestEvent', $messageBody['eventType']);
self::assertSame('1.0', $messageBody['dataVersion']);
self::assertSame('1', $messageBody['metadataVersion']);
self::assertEqualsWithDelta(
(new DateTimeImmutable('now'))->getTimestamp(),
(new DateTimeImmutable($messageBody['eventTime']))->getTimestamp(),
10 // peak has timeout 10s
);
self::assertStringEndsWith('Microsoft.EventGrid/topics/' . getenv('AZURE_API_CLIENT_CI__EVENT_GRID__TOPIC_NAME'), $messageBody['topic']);
$serviceBusClient->deleteMessage($message->lockLocation);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Keboola\AzureApiClient\Tests\EventGrid;

use Keboola\AzureApiClient\ApiClientConfiguration;
use Keboola\AzureApiClient\Authentication\Authenticator\SASTokenAuthenticatorFactory;
use Keboola\AzureApiClient\Json;
use Keboola\AzureApiClient\ServiceBus\Model\ServiceBusBrokerMessageRequest;
use Keboola\AzureApiClient\ServiceBus\ServiceBusApiClient;
use Keboola\AzureApiClient\Tests\ReflectionPropertyAccessTestCase;
use PHPUnit\Framework\TestCase;

/**
* @group functional
*/
class ServiceBusApiClientFunctionalTest extends TestCase
{
use ReflectionPropertyAccessTestCase;

private function getClient(): ServiceBusApiClient
{
$endpoint = getenv('AZURE_API_CLIENT_CI__SERVICE_BUS__ENDPOINT');
return new ServiceBusApiClient(
serviceBusEndpoint: $endpoint,
configuration: new ApiClientConfiguration(
authenticator: new SASTokenAuthenticatorFactory(
url: $endpoint,
sharedAccessKeyName: 'RootManageSharedAccessKey',
sharedAccessKey: getenv('AZURE_API_CLIENT_CI__SERVICE_BUS__SHARED_ACCESS_KEY'),
),
)
);
}

public function testEvent(): void
{
$client = $this->getClient();
$messageSend = ServiceBusBrokerMessageRequest::createJson(
'123',
['testdata' => 'value']
);
$client->sendMessage(
'queue-tests',
$messageSend
);
$messageReceived = $client->peakMessage(
'queue-tests'
);
$this->assertNotNull($messageReceived);
self::assertSame($messageSend->id, $messageReceived->id);
$messageBody = Json::decodeArray($messageReceived->body);
self::assertSame([
'testdata' => 'value',
], $messageBody);
$client->deleteMessage($messageReceived->lockLocation);
}
}