View source
<?php
namespace Drupal\Tests\farm_sensor\Functional;
use Drupal\asset\Entity\Asset;
use Drupal\asset\Entity\AssetInterface;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Url;
use Drupal\Tests\farm_test\Functional\FarmBrowserTestBase;
use GuzzleHttp\RequestOptions;
class SensorDataApiTest extends FarmBrowserTestBase {
protected $asset;
protected static $modules = [
'farm_sensor',
];
protected function setUp() : void {
parent::setUp();
$this->asset = Asset::create([
'type' => 'sensor',
'name' => $this
->randomMachineName(),
]);
$this->asset
->save();
}
public function testApiGet() {
$uri = $this
->buildPath($this->asset);
$url = Url::fromUri($uri);
$private_key = [
RequestOptions::QUERY => [
'private_key' => $this->asset
->get('private_key')->value,
],
];
$response = $this
->processRequest('GET', $url);
$this
->assertEquals(403, $response
->getStatusCode());
$response = $this
->processRequest('GET', $url, $private_key);
$this
->assertEquals(200, $response
->getStatusCode());
$data = Json::decode($response
->getBody());
$this
->assertEquals(0, count($data));
$this->asset
->set('public', TRUE)
->save();
$response = $this
->processRequest('GET', $url);
$this
->assertEquals(200, $response
->getStatusCode());
$data = Json::decode($response
->getBody());
$this
->assertEquals(0, count($data));
}
public function testApiPost() {
$uri = $this
->buildPath($this->asset);
$url = Url::fromUri($uri);
$private_key = [
RequestOptions::QUERY => [
'private_key' => $this->asset
->get('private_key')->value,
],
];
$this->asset
->set('public', TRUE)
->save();
$test_data = [
'test_1' => 100,
'test_2' => 200,
];
$payload = [
RequestOptions::BODY => Json::encode($test_data),
];
$response = $this
->processRequest('POST', $url, $payload);
$this
->assertEquals(403, $response
->getStatusCode());
$response = $this
->processRequest('POST', $url, $private_key + $payload);
$this
->assertEquals(201, $response
->getStatusCode());
$this->asset = Asset::load($this->asset
->id());
$data_streams = $this->asset
->get('data_stream')
->referencedEntities();
$this
->assertEquals(2, count($data_streams));
$response = $this
->processRequest('GET', $url);
$data = Json::decode($response
->getBody());
$this
->assertEquals(2, count($data));
$test_data = [
'test_1' => 101,
'test_2' => 201,
];
$payload = [
RequestOptions::BODY => Json::encode($test_data),
];
$response = $this
->processRequest('POST', $url, $private_key + $payload);
$this
->assertEquals(201, $response
->getStatusCode());
$this->asset = Asset::load($this->asset
->id());
$data_streams = $this->asset
->get('data_stream')
->referencedEntities();
$this
->assertEquals(2, count($data_streams));
$response = $this
->processRequest('GET', $url);
$data = Json::decode($response
->getBody());
$this
->assertEquals(4, count($data));
}
protected function buildPath(AssetInterface $asset) {
return "base://asset/{$asset->uuid()}/data/basic";
}
protected function processRequest(string $method, Url $url, array $request_options = []) {
$this
->refreshVariables();
$request_options[RequestOptions::HTTP_ERRORS] = FALSE;
$client = $this
->getSession()
->getDriver()
->getClient()
->getClient();
return $client
->request($method, $url
->setAbsolute(TRUE)
->toString(), $request_options);
}
}