Newer
Older
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]];
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
$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');
}
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
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();
}