Skip to content
Snippets Groups Projects
CrudTest.php 22.7 KiB
Newer Older
use Slim\Psr7\Stream;

require_once 'api/setup.php';
require_once 'api/routes.php';
require 'BaseTest.php';

class CrudTest extends BaseTest
{

    public function testCreateInvalid()
    {
        $response = $this->app->handle($this->_createRequest('POST', '/create/test'));
        $this->assertEquals(400, $response->getStatusCode());
    }

    public function testCreateInvalidMissingContent()
    {
        $response = $this->app->handle($this->_createRequest('POST', '/create/test'));
        $this->assertEquals(400, $response->getStatusCode());
        $this->assertContains("missing body", json_decode($response->getBody(), true)['errors']);
    }

    public function testCreateInvalidMissingTags()
    {
        $body = array("content" => ["meta" => true, "features" => []]);        
        $response = $this->app->handle($this->_createRequest('POST', '/create/test', $body));

        $this->assertEquals(400, $response->getStatusCode());
        $this->assertNotContains("content property empty", json_decode($response->getBody(), true)['errors']);
        $this->assertContains("features missing tags entry or invalid (should be an array)", json_decode($response->getBody(), true)['errors']);
    }

    public function testCreateInvalidInvalidT0T1()
    {
        $body = array("content" => ["meta" => true], "t0" => 'a');        
        $response = $this->app->handle($this->_createRequest('POST', '/create/test', $body));

        $this->assertEquals(400, $response->getStatusCode());
        $this->assertContains("t0 and t1 must time integer timestamps", json_decode($response->getBody(), true)['errors']);

        $body = array("content" => ["meta" => true], "t1" => 'a');        
        $response = $this->app->handle($this->_createRequest('POST', '/create/test', $body));

        $this->assertEquals(400, $response->getStatusCode());
        $this->assertContains("t0 and t1 must time integer timestamps", json_decode($response->getBody(), true)['errors']);

    }

    public function testCreate()
    {
        $body = array("content" => ["meta" => true], "features" => ["tags" => ["sim"]]);
        $response = $this->app->handle($this->_createRequest('POST', '/create/test', $body));
        $this->assertEquals(200, $response->getStatusCode(), json_encode($response->getBody()));
    }

    public function testCreateWithSmartDataUnit()
    {
        $body = array("content" => ["meta" => true], "features" => ["tags" => ["sim"]], "smartDataUnits" => [10, 12]);
        $response = $this->app->handle($this->_createRequest('POST', '/create/test', $body));
        $this->assertEquals(200, $response->getStatusCode(), json_encode($response->getBody()));
    }

    public function testCreateWithInvalidSmartDataUnit()
    {
        $body = array("content" => ["meta" => true], "features" => ["tags" => ["sim"]], "smartDataUnits" => ["aaa", "eee"]);
        $response = $this->app->handle($this->_createRequest('POST', '/create/test', $body));
        $this->assertEquals(400, $response->getStatusCode());
        $this->assertContains("smartDataUnits should be a array of longs", json_decode($response->getBody(), true)['errors']);
    }

    public function testCreateWithSmartDataSources()
    {
        $body = array("content" => ["meta" => true], "features" => ["tags" => ["sim"]], "smartDataSources" => ["ffdaf2342", [1,2,3,5]]);
        $response = $this->app->handle($this->_createRequest('POST', '/create/test', $body));
        $this->assertEquals(200, $response->getStatusCode(), json_encode($response->getBody()));
    }

    public function testCreateWithSmartDataSourcesInvalidSignature()
    {
        $body = array("content" => ["meta" => true], "features" => ["tags" => ["sim"]], "smartDataSources" => ["ffds-af-2342", [1,2,3,5]]);
        $response = $this->app->handle($this->_createRequest('POST', '/create/test', $body));
        $this->assertEquals(400, $response->getStatusCode());
        $this->assertContains("ffds-af-2342 is an invalid signature", json_decode($response->getBody(), true)['errors']);
    }

    public function testCreateWithSmartDataSourcesInvalidSphere()
    {
        $body = array("content" => ["meta" => true], "features" => ["tags" => ["sim"]], "smartDataSources" => ["ffdsaf2342", [1,2,3]]);
        $response = $this->app->handle($this->_createRequest('POST', '/create/test', $body));
        $this->assertEquals(400, $response->getStatusCode());
        $this->assertContains("[1,2,3] is an invalid sphere", json_decode($response->getBody(), true)['errors']);
    }

    public function testCreateGetWithSmartDataUnit()
    {

        $body = array("content" => ["meta" => true], "features" => ["tags" => ["sim"]], "smartDataUnits" => [10, 12]);
        $response = $this->app->handle($this->_createRequest('POST', '/create/test', $body));

        $this->assertEquals(200, $response->getStatusCode(), json_encode($response->getBody()));
        $response->getBody()->rewind();
        $body = json_decode($response->getBody()->getContents(), true);
        $this->assertTrue(array_key_exists('result', $body), 'missing result');
        $this->assertTrue(array_key_exists('smartDataContextId', $body['result']), 'missing smartdataid');
        $id = $body['result']['smartDataContextId'];
        $response = $this->app->handle($this->_createRequest('GET', "/get/test/$id"));
        $this->assertEquals(200, $response->getStatusCode(), json_encode($response->getBody()));
        $response->getBody()->rewind();
        $body = json_decode($response->getBody()->getContents(), true);
        $this->assertTrue(array_key_exists('result', $body), 'missing result');
        $this->assertTrue(array_key_exists('content', $body['result']), 'missing content');
        $this->assertEquals($body['result']['id'], $id, 'wrong smartdataid');
    }

    public function testAssociateWithSmartDataSources()
    {
        $body = array("content" => ["meta" => true], "features" => ["tags" => ["sim"]], "smartDataSources" => ["ffdaf2342", [1,2,3,5]]);
        $response = $this->app->handle($this->_createRequest('POST', '/create/test', $body));
        $this->assertEquals(200, $response->getStatusCode(), json_encode($response->getBody()));
        $response->getBody()->rewind();
        $body = json_decode($response->getBody()->getContents(), true);
        $this->assertTrue(array_key_exists('result', $body), 'missing result');
        $this->assertTrue(array_key_exists('smartDataContextId', $body['result']), 'missing smartdataid');
        $id = $body['result']['smartDataContextId'];
        $body = ["smartDataContextIds" => [$id], "smartDataSources" => ["ae7666", [4,5,6,7], [1,2,3,5]], "smartDataUnits" => [111,222]];
        $response = $this->app->handle($this->_createRequest('POST', '/associate/test', $body));
        $this->assertEquals(200, $response->getStatusCode(), json_encode($response->getBody()));
        $response->getBody()->rewind();
        $body = json_decode($response->getBody()->getContents(), true);
        $this->assertEquals(4, count($body['result'][0]['result']['smartDataSources']), "Wrong number of smartDataSources associated");
        $this->assertEquals(2, count($body['result'][0]['result']['smartDataUnits']), "Wrong number of smartDataUnits associated");
    }


    public function testUnassociateWithSmartDataSources()
    {
        $body = array("content" => ["meta" => true], "features" => ["tags" => ["sim"]], "smartDataSources" => ["ffdaf2342", [1,2,3,5]]);
        $response = $this->app->handle($this->_createRequest('POST', '/create/test', $body));
        $this->assertEquals(200, $response->getStatusCode(), json_encode($response->getBody()));
        $response->getBody()->rewind();
        $body = json_decode($response->getBody()->getContents(), true);
        $this->assertTrue(array_key_exists('result', $body), 'missing result');
        $this->assertTrue(array_key_exists('smartDataContextId', $body['result']), 'missing smartdataid');
        $id = $body['result']['smartDataContextId'];
        $body = ["smartDataContextIds" => [$id], "smartDataSources" => ["ae7666", [4,5,6,7], [1,2,3,5]], "smartDataUnits" => [111,222]];
        $response = $this->app->handle($this->_createRequest('POST', '/associate/test', $body));
        $this->assertEquals(200, $response->getStatusCode(), json_encode($response->getBody()));
        $response->getBody()->rewind();
        $body = json_decode($response->getBody()->getContents(), true);
        $this->assertEquals(4, count($body['result'][0]['result']['smartDataSources']), "Wrong number of smartDataSources associated");
        $this->assertEquals(2, count($body['result'][0]['result']['smartDataUnits']), "Wrong number of smartDataUnits associated");

        $body = ["smartDataContextIds" => [$id], "smartDataSources" => ["ae7666", [4,5,6,7]], "smartDataUnits" => [222]];
        $response = $this->app->handle($this->_createRequest('POST', '/unassociate/test', $body));
        $this->assertEquals(200, $response->getStatusCode(), json_encode($response->getBody()));
        $response->getBody()->rewind();
        $body = json_decode($response->getBody()->getContents(), true);
        $this->assertEquals(2, count($body['result'][0]['result']['smartDataSources']), "Wrong number of smartDataSources associated");
        $this->assertEquals(1, count($body['result'][0]['result']['smartDataUnits']), "Wrong number of smartDataUnits associated");
    }

    public function testQuery()
    {
        $body = array("content" => ["meta" => true], "features" => ["tags" => ["sim"]], "smartDataSources" => ["ffdaf2342", [1,2,3,5]]);
        $response = $this->app->handle($this->_createRequest('POST', '/create/test', $body));
        $this->assertEquals(200, $response->getStatusCode(), json_encode($response->getBody()));
        $response->getBody()->rewind();
        $body = json_decode($response->getBody()->getContents(), true);
        $this->assertTrue(array_key_exists('result', $body), 'missing result');
        $this->assertTrue(array_key_exists('smartDataContextId', $body['result']), 'missing smartdataid');
        $id = $body['result']['smartDataContextId'];
        $body = ["smartDataContextIds" => [$id], "smartDataSources" => ["ae7666", [4,5,6,7], [1,2,3,5]], "smartDataUnits" => [111,222]];
        $response = $this->app->handle($this->_createRequest('POST', '/associate/test', $body));
        $this->assertEquals(200, $response->getStatusCode(), json_encode($response->getBody()));
        $response->getBody()->rewind();
        $body = json_decode($response->getBody()->getContents(), true);
        $this->assertEquals(4, count($body['result'][0]['result']['smartDataSources']), "Wrong number of smartDataSources associated");
        $this->assertEquals(2, count($body['result'][0]['result']['smartDataUnits']), "Wrong number of smartDataUnits associated");

        $body = ["smartDataSources" => [4,5,6,7]];
        $response = $this->app->handle($this->_createRequest('POST', '/query/test', $body));
        $this->assertEquals(200, $response->getStatusCode(), json_encode($response->getBody()));
        $response->getBody()->rewind();
        $body = json_decode($response->getBody()->getContents(), true);
        $this->assertGreaterThan(0, count($body['result']), "Wrong number of results");
    }

    public function testContexts()
    {
        $domain = "performance";

        $body = array("content" => ["meta" => true], "features" => ["tags" => ["sim"]], "smartDataSources" => ["ffdaf2342", [1,2,3,5]]);
        $response = $this->app->handle($this->_createRequest('POST', "/create/$domain", $body));
        $this->assertEquals(200, $response->getStatusCode(), json_encode($response->getBody()));
        $response->getBody()->rewind();
        $body = json_decode($response->getBody()->getContents(), true);
        $this->assertTrue(array_key_exists('result', $body), 'missing result');
        $this->assertTrue(array_key_exists('smartDataContextId', $body['result']), 'missing smartdataid');
        $id = $body['result']['smartDataContextId'];
        $body = ["smartDataContextIds" => [$id], "smartDataSources" => ["ae7666", [4,5,6,7], [1,2,3,5]], "smartDataUnits" => [111,222]];
        $response = $this->app->handle($this->_createRequest('POST', "/associate/$domain", $body));
        $this->assertEquals(200, $response->getStatusCode(), json_encode($response->getBody()));
        $response->getBody()->rewind();
        $body = json_decode($response->getBody()->getContents(), true);
        $this->assertEquals(4, count($body['result'][0]['result']['smartDataSources']), "Wrong number of smartDataSources associated");
        $this->assertEquals(2, count($body['result'][0]['result']['smartDataUnits']), "Wrong number of smartDataUnits associated");

        $start = time();
        $body = ["smartDataSources" => [[4,5,6,7]], "t0" => 10, "t1" => 300];
        $response = $this->app->handle($this->_createRequest('POST', "/contexts/$domain", $body));
        $this->assertEquals(200, $response->getStatusCode(), json_encode($response->getBody()));
        $response->getBody()->rewind();
        $body = json_decode($response->getBody()->getContents(), true);
        $this->assertGreaterThan(0, count($body['result']), "Wrong number of results");
    }


    public function testUpdateWithTimestampsAndContent() {
        // Step 1: Create a document
        $body = ["content" => ["meta" => true], "features" => ["tags" => ["sim"]], "smartDataUnits" => [10, 12]];
        $response = $this->app->handle($this->_createRequest('POST', '/create/test', $body));

        $this->assertEquals(200, $response->getStatusCode(), json_encode($response->getBody()));
        $response->getBody()->rewind();
        $body = json_decode($response->getBody()->getContents(), true);
        $this->assertTrue(array_key_exists('result', $body), 'missing result');
        $this->assertTrue(array_key_exists('smartDataContextId', $body['result']), 'missing smartDataContextId');
        $id = $body['result']['smartDataContextId'];

        // Step 2: Update the document with t0 and t1
        $updateBody = ["t0" => 1000, "t1" => 2000];
        $response = $this->app->handle($this->_createRequest('POST', "/update/test/$id", $updateBody));

        $this->assertEquals(200, $response->getStatusCode(), json_encode($response->getBody()));
        $response->getBody()->rewind();
        $updatedBody = json_decode($response->getBody()->getContents(), true);
        $this->assertTrue(array_key_exists('result', $updatedBody), 'missing result');
        $this->assertEquals($updatedBody['result']['t0'], 1000, 't0 was not updated correctly');
        $this->assertEquals($updatedBody['result']['t1'], 2000, 't1 was not updated correctly');

        // Step 3: Update the document with new content
        $newContent = ["content" => ["meta" => "updated"]];
        $response = $this->app->handle($this->_createRequest('POST', "/update/test/$id", $newContent));

        $this->assertEquals(200, $response->getStatusCode(), json_encode($response->getBody()));
        $response->getBody()->rewind();
        $updatedContentBody = json_decode($response->getBody()->getContents(), true);
        $this->assertTrue(array_key_exists('result', $updatedContentBody), 'missing result');
        $this->assertEquals($updatedContentBody['result']['content']['meta'], "updated", 'content was not updated correctly');

        // Step 4: Get the updated document and verify
        $response = $this->app->handle($this->_createRequest('GET', "/get/test/$id"));
        $this->assertEquals(200, $response->getStatusCode(), json_encode($response->getBody()));
        $response->getBody()->rewind();
        $finalBody = json_decode($response->getBody()->getContents(), true);
        $this->assertTrue(array_key_exists('result', $finalBody), 'missing result');
        $this->assertEquals($finalBody['result']['id'], $id, 'wrong smartdataid');
        $this->assertEquals($finalBody['result']['t0'], 1000, 't0 mismatch');
        $this->assertEquals($finalBody['result']['t1'], 2000, 't1 mismatch');
        $this->assertEquals($finalBody['result']['content']['meta'], "updated", 'content meta mismatch');
    }


    private $testFilePath = '/tmp/testFile.bin';
    private $testFileSize = 1024 * 1014 * 100; // 100MB

    private function createTestFile()
    {
        $file = fopen($this->testFilePath, 'wb');
        $data = random_bytes($this->testFileSize);
        fwrite($file, $data);
        fclose($file);
        return $data; // return the file data to compare later
    }

    private function removeTestFile()
    {
        if (file_exists($this->testFilePath)) {
            unlink($this->testFilePath);
        }
    }

    public function testAddUnstructuredData()
    {
        $originalData = $this->createTestFile();
        $domain = 'test';

        // Create a SmartDataContext first
        $body = array("content" => ["meta" => true], "features" => ["tags" => ["sim"]]);
        $response = $this->app->handle($this->_createRequest('POST', "/create/$domain", $body));
        $this->assertEquals(200, $response->getStatusCode(), json_encode($response->getBody()));

        $response->getBody()->rewind();
        $responseBody = json_decode($response->getBody()->getContents(), true);
        $this->assertArrayHasKey('result', $responseBody);
        $this->assertArrayHasKey('smartDataContextId', $responseBody['result']);
        $smartDataContextId = $responseBody['result']['smartDataContextId'];

        // Add unstructured data
        $request = $this->_createRequest('POST', "/unstructured/add/$domain/$smartDataContextId");
        $request = $request->withHeader('Content-Type', 'application/octet-stream');
        $request = $request->withHeader('Filename', basename($this->testFilePath));
        $stream = fopen($this->testFilePath, 'rb');
        $stream = new Stream($stream);
        $request = $request->withBody($stream);
        $response = $this->app->handle($request);
        $this->assertEquals(200, $response->getStatusCode(), json_encode($response->getBody()));

        // Ensure the object was stored correctly
        $response->getBody()->rewind();
        $body = json_decode($response->getBody()->getContents(), true);
        $this->assertArrayHasKey('result', $body);
        $this->assertArrayHasKey('objectId', $body['result']);
        $objectId = $body['result']['objectId'];

        // Retrieve the stored object to confirm it matches the original data
        $response = $this->app->handle($this->_createRequest('POST', "/unstructured/get/$domain", ["smartDataContextId" => $smartDataContextId, "objectId" => $objectId]));
        $this->assertEquals(200, $response->getStatusCode());

        $retrievedData = $response->getBody()->getContents();
        $this->assertEquals($originalData, $retrievedData, "The stored data does not match the original file content");

        $this->removeTestFile();
    }


    public function testRemoveUnstructuredData()
    {
        $this->createTestFile();
        $domain = 'test';

        // Create a SmartDataContext first and add unstructured data
        $body = array("content" => ["meta" => true], "features" => ["tags" => ["sim"]]);
        $response = $this->app->handle($this->_createRequest('POST', "/create/$domain", $body));
        $this->assertEquals(200, $response->getStatusCode(), json_encode($response->getBody()));

        $response->getBody()->rewind();
        $responseBody = json_decode($response->getBody()->getContents(), true);
        $this->assertArrayHasKey('result', $responseBody);
        $this->assertArrayHasKey('smartDataContextId', $responseBody['result']);
        $smartDataContextId = $responseBody['result']['smartDataContextId'];

        // Add unstructured data
        $request = $this->_createRequest('POST', "/unstructured/add/$domain/$smartDataContextId");
        $request = $request->withHeader('Content-Type', 'application/octet-stream');
        $request = $request->withHeader('Filename', basename($this->testFilePath));
        $request = $request->withBody(new Stream(fopen($this->testFilePath, 'rb')));
        $response = $this->app->handle($request);
        $this->assertEquals(200, $response->getStatusCode(), json_encode($response->getBody()));

        $response->getBody()->rewind();
        $responseBody = json_decode($response->getBody()->getContents(), true);
        $this->assertArrayHasKey('result', $responseBody);
        $this->assertArrayHasKey('objectId', $responseBody['result']);
        $objectId = $responseBody['result']['objectId'];

        // Remove the unstructured data
        $body = ["smartDataContextId" => $smartDataContextId, "objectId" => $objectId];
        $response = $this->app->handle($this->_createRequest('POST', "/unstructured/remove/$domain", $body));
        $this->assertEquals(200, $response->getStatusCode(), json_encode($response->getBody()));

        $this->removeTestFile();
    }

    public function testGetUnstructuredData()
    {
        $originalData = $this->createTestFile();
        $domain = 'test';

        // Create a SmartDataContext first and add unstructured data
        $body = array("content" => ["meta" => true], "features" => ["tags" => ["sim"]]);
        $response = $this->app->handle($this->_createRequest('POST', "/create/$domain", $body));
        $this->assertEquals(200, $response->getStatusCode(), json_encode($response->getBody()));

        $response->getBody()->rewind();
        $responseBody = json_decode($response->getBody()->getContents(), true);
        $this->assertArrayHasKey('result', $responseBody);
        $this->assertArrayHasKey('smartDataContextId', $responseBody['result']);
        $smartDataContextId = $responseBody['result']['smartDataContextId'];

        // Add unstructured data
        $request = $this->_createRequest('POST', "/unstructured/add/$domain/$smartDataContextId");
        $request = $request->withHeader('Content-Type', 'application/octet-stream');
        $request = $request->withHeader('Filename', basename($this->testFilePath));
        $request = $request->withBody(new Stream(fopen($this->testFilePath, 'rb')));
        $response = $this->app->handle($request);
        $this->assertEquals(200, $response->getStatusCode(), json_encode($response->getBody()));

        $response->getBody()->rewind();
        $responseBody = json_decode($response->getBody()->getContents(), true);
        $this->assertArrayHasKey('result', $responseBody);
        $this->assertArrayHasKey('objectId', $responseBody['result']);
        $objectId = $responseBody['result']['objectId'];

        // Retrieve the stored object
        $response = $this->app->handle($this->_createRequest('POST', "/unstructured/get/$domain", ["smartDataContextId" => $smartDataContextId, "objectId" => $objectId]));
        $this->assertEquals(200, $response->getStatusCode());

        $retrievedData = $response->getBody()->getContents();
        $this->assertEquals($originalData, $retrievedData, "The retrieved data does not match the original file content");

        $this->removeTestFile();
    }