Overhaul of ACARS/PIREP APIs
This commit is contained in:
@@ -14,6 +14,33 @@ class AcarsTest extends TestCase
|
||||
$this->addData('base');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $route
|
||||
* @param $points
|
||||
* @param array $addtl_fields
|
||||
*/
|
||||
protected function allPointsInRoute($route, $points, $addtl_fields=[])
|
||||
{
|
||||
if(empty($addtl_fields)) {
|
||||
$addtl_fields = [];
|
||||
}
|
||||
|
||||
$fields = array_merge([
|
||||
'name',
|
||||
'order',
|
||||
'lat',
|
||||
'lon'
|
||||
], $addtl_fields);
|
||||
|
||||
$this->assertEquals(\count($route), \count($points));
|
||||
foreach($route as $idx => $point) {
|
||||
//$this->assertHasKeys($points[$idx], $fields);
|
||||
foreach($fields as $f) {
|
||||
$this->assertEquals($point[$f], $points[$idx][$f]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function getPirep($pirep_id)
|
||||
{
|
||||
$resp = $this->withHeaders($this->apiHeaders())
|
||||
@@ -55,30 +82,73 @@ class AcarsTest extends TestCase
|
||||
$this->assertEquals(PirepState::IN_PROGRESS, $pirep['state']);
|
||||
$this->assertEquals(PirepStatus::PREFILE, $pirep['status']);
|
||||
|
||||
$uri = '/api/pireps/' . $pirep_id . '/acars/position';
|
||||
|
||||
# Test missing positions field
|
||||
# Post an ACARS update
|
||||
$update = [];
|
||||
$response = $this->withHeaders($this->apiHeaders())->post($uri, $update);
|
||||
$response->assertStatus(400);
|
||||
|
||||
# Post an ACARS update
|
||||
$uri = '/api/pireps/' . $pirep_id . '/acars';
|
||||
$acars = factory(App\Models\Acars::class)->make()->toArray();
|
||||
$response = $this->withHeaders($this->apiHeaders())->post($uri, $acars);
|
||||
$response->assertStatus(201);
|
||||
unset($acars['id']);
|
||||
$update = ['positions' => [$acars]];
|
||||
$response = $this->withHeaders($this->apiHeaders())->post($uri, $update);
|
||||
$response->assertStatus(200);
|
||||
|
||||
$body = $response->json();
|
||||
$this->assertNotNull($body['id']);
|
||||
$this->assertEquals($pirep_id, $body['pirep_id']);
|
||||
$this->assertNotNull($body[0]['id']);
|
||||
$this->assertEquals($pirep_id, $body[0]['pirep_id']);
|
||||
//$this->assertHasKeys($body, $this->fillableFields(new \App\Models\Acars));
|
||||
|
||||
# Make sure PIREP state moved into ENROUTE
|
||||
$pirep = $this->getPirep($pirep_id);
|
||||
$this->assertEquals(PirepState::IN_PROGRESS, $pirep['state']);
|
||||
$this->assertEquals(PirepStatus::ENROUTE, $pirep['status']);
|
||||
|
||||
$uri = '/api/pireps/' . $pirep_id . '/acars';
|
||||
$response = $this->withHeaders($this->apiHeaders())->get($uri);
|
||||
$response->assertStatus(200);
|
||||
$body = $response->json();
|
||||
|
||||
/*$body = $response->json();
|
||||
$this->assertEquals(1, $this->count($body));
|
||||
$this->assertEquals($pirep_id, $body[0]['pirep_id']);*/
|
||||
$this->assertNotNull($body);
|
||||
$this->assertCount(1, $body);
|
||||
$this->assertEquals($acars['lat'], $body[0]['lat']);
|
||||
$this->assertEquals($acars['lon'], $body[0]['lon']);
|
||||
|
||||
//$this->allPointsInRoute([$acars], $body);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test publishing multiple, batched updates
|
||||
*/
|
||||
public function testMultipleAcarsPositionUpdates()
|
||||
{
|
||||
$pirep = factory(App\Models\Pirep::class)->make()->toArray();
|
||||
|
||||
$uri = '/api/pireps/prefile';
|
||||
$response = $this->withHeaders($this->apiHeaders())->post($uri, $pirep);
|
||||
$response->assertStatus(201);
|
||||
|
||||
$pirep_id = $response->json()['id'];
|
||||
|
||||
$uri = '/api/pireps/' . $pirep_id . '/acars/position';
|
||||
|
||||
# Post an ACARS update
|
||||
$acars_count = \random_int(5, 50);
|
||||
$acars = factory(App\Models\Acars::class, $acars_count)->make(['id'=>''])->toArray();
|
||||
|
||||
$update = ['positions' => $acars];
|
||||
$response = $this->withHeaders($this->apiHeaders())->post($uri, $update);
|
||||
$response->assertStatus(200)->assertJsonCount($acars_count);
|
||||
|
||||
$response = $this->withHeaders($this->apiHeaders())->get($uri);
|
||||
$response->assertStatus(200)->assertJsonCount($acars_count);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function testNonExistentPirepGet()
|
||||
{
|
||||
$uri = '/api/pireps/DOESNTEXIST/acars';
|
||||
@@ -86,35 +156,136 @@ class AcarsTest extends TestCase
|
||||
$response->assertStatus(404);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function testNonExistentPirepStore()
|
||||
{
|
||||
$uri = '/api/pireps/DOESNTEXIST/acars';
|
||||
$uri = '/api/pireps/DOESNTEXIST/acars/position';
|
||||
$acars = factory(App\Models\Acars::class)->make()->toArray();
|
||||
$response = $this->withHeaders($this->apiHeaders())->post($uri, $acars);
|
||||
$response->assertStatus(404);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function testAcarsIsoDate()
|
||||
{
|
||||
$pirep = factory(App\Models\Pirep::class)->make()->toArray();
|
||||
|
||||
$uri = '/api/pireps/prefile';
|
||||
$response = $this->withHeaders($this->apiHeaders())->post($uri, $pirep);
|
||||
$pirep_id = $response->json()['id'];
|
||||
|
||||
$dt = date('c');
|
||||
$uri = '/api/pireps/' . $pirep_id . '/acars';
|
||||
$acars = factory(App\Models\Acars::class)->make()->toArray();
|
||||
$acars['sim_time'] = $dt;
|
||||
$uri = '/api/pireps/' . $pirep_id . '/acars/position';
|
||||
$acars = factory(App\Models\Acars::class)->make([
|
||||
'sim_time' => $dt
|
||||
])->toArray();
|
||||
|
||||
$response = $this->withHeaders($this->apiHeaders())->post($uri, $acars);
|
||||
$response->assertStatus(201);
|
||||
|
||||
/*$uri = '/api/pireps/' . $pirep_id . '/acars';
|
||||
$response = $this->withHeaders($this->apiHeaders())->get($uri);
|
||||
$update = ['positions' => [$acars]];
|
||||
$response = $this->withHeaders($this->apiHeaders())->post($uri, $update);
|
||||
$response->assertStatus(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the validation
|
||||
*/
|
||||
public function testAcarsInvalidRoutePost()
|
||||
{
|
||||
$pirep = factory(App\Models\Pirep::class)->make()->toArray();
|
||||
|
||||
$uri = '/api/pireps/prefile';
|
||||
$response = $this->withHeaders($this->apiHeaders())->post($uri, $pirep);
|
||||
$pirep_id = $response->json()['id'];
|
||||
|
||||
$post_route = ['order' => 1, 'name' => 'NAVPOINT'];
|
||||
$uri = '/api/pireps/' . $pirep_id . '/route';
|
||||
$response = $this->withHeaders($this->apiHeaders())->post($uri, $post_route);
|
||||
$response->assertStatus(400);
|
||||
|
||||
$post_route = [
|
||||
['order' => 1, 'name' => 'NAVPOINT', 'lat' => 'notanumber', 'lon' => 34.11]
|
||||
];
|
||||
|
||||
$uri = '/api/pireps/' . $pirep_id . '/route';
|
||||
$response = $this->withHeaders($this->apiHeaders())->post($uri, $post_route);
|
||||
$response->assertStatus(400);
|
||||
}
|
||||
|
||||
public function testAcarsLogPost()
|
||||
{
|
||||
$pirep = factory(App\Models\Pirep::class)->make()->toArray();
|
||||
|
||||
$uri = '/api/pireps/prefile';
|
||||
$response = $this->withHeaders($this->apiHeaders())->post($uri, $pirep);
|
||||
$pirep_id = $response->json()['id'];
|
||||
|
||||
$acars = factory(App\Models\Acars::class)->make();
|
||||
$post_log = [
|
||||
'log' => $acars->log
|
||||
];
|
||||
|
||||
$uri = '/api/pireps/' . $pirep_id . '/acars/log';
|
||||
$response = $this->withHeaders($this->apiHeaders())->post($uri, $post_log);
|
||||
$response->assertStatus(201);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function testAcarsRoutePost()
|
||||
{
|
||||
$pirep = factory(App\Models\Pirep::class)->make()->toArray();
|
||||
|
||||
$uri = '/api/pireps/prefile';
|
||||
$response = $this->withHeaders($this->apiHeaders())->post($uri, $pirep);
|
||||
$pirep_id = $response->json()['id'];
|
||||
|
||||
$order = 1;
|
||||
$post_route = [];
|
||||
$route_count = \random_int(5, 50);
|
||||
|
||||
$route = factory(App\Models\Navdata::class, $route_count)->create();
|
||||
foreach($route as $position) {
|
||||
$post_route[] = [
|
||||
'order' => $order,
|
||||
'name' => $position->name,
|
||||
'lat' => $position->lat,
|
||||
'lon' => $position->lon,
|
||||
];
|
||||
|
||||
++$order;
|
||||
}
|
||||
|
||||
$uri = '/api/pireps/'.$pirep_id.'/route';
|
||||
$response = $this->withHeaders($this->apiHeaders())->post($uri, ['route' => $post_route]);
|
||||
$response->assertStatus(200)->assertJsonCount($route_count);
|
||||
|
||||
$body = $response->json();
|
||||
$this->assertEquals($dt, $body[0]['sim_time']);*/
|
||||
$this->allPointsInRoute($post_route, $body);
|
||||
|
||||
/**
|
||||
* Get
|
||||
*/
|
||||
|
||||
$uri = '/api/pireps/' . $pirep_id . '/route';
|
||||
$response = $this->withHeaders($this->apiHeaders())->get($uri);
|
||||
$response->assertStatus(200)->assertJsonCount($route_count);
|
||||
$body = $response->json();
|
||||
$this->allPointsInRoute($post_route, $body);
|
||||
|
||||
/**
|
||||
* Delete and then recheck
|
||||
*/
|
||||
$uri = '/api/pireps/' . $pirep_id . '/route';
|
||||
$response = $this->withHeaders($this->apiHeaders())->delete($uri);
|
||||
$response->assertStatus(200);
|
||||
|
||||
$uri = '/api/pireps/' . $pirep_id . '/route';
|
||||
$response = $this->withHeaders($this->apiHeaders())->get($uri);
|
||||
$response->assertStatus(200)->assertJsonCount(0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -193,10 +193,13 @@ class PIREPTest extends TestCase
|
||||
$response = $this->withHeaders($this->apiHeaders())->post($uri, $pirep);
|
||||
$pirep_id = $response->json()['id'];
|
||||
|
||||
$uri = '/api/pireps/' . $pirep_id . '/acars';
|
||||
$uri = '/api/pireps/' . $pirep_id . '/acars/position';
|
||||
$acars = factory(App\Models\Acars::class)->make()->toArray();
|
||||
$response = $this->withHeaders($this->apiHeaders())->post($uri, $acars);
|
||||
$response->assertStatus(201);
|
||||
$response = $this->withHeaders($this->apiHeaders())->post($uri, [
|
||||
'positions' => [$acars]
|
||||
]);
|
||||
|
||||
$response->assertStatus(200);
|
||||
|
||||
# Cancel it
|
||||
$uri = '/api/pireps/' . $pirep_id . '/cancel';
|
||||
@@ -204,7 +207,7 @@ class PIREPTest extends TestCase
|
||||
$response->assertStatus(200);
|
||||
|
||||
# Should get a 400 when posting an ACARS update
|
||||
$uri = '/api/pireps/' . $pirep_id . '/acars';
|
||||
$uri = '/api/pireps/' . $pirep_id . '/acars/position';
|
||||
$acars = factory(App\Models\Acars::class)->make()->toArray();
|
||||
$response = $this->withHeaders($this->apiHeaders())->post($uri, $acars);
|
||||
$response->assertStatus(400);
|
||||
|
||||
@@ -71,6 +71,27 @@ abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
|
||||
{
|
||||
$svc = app('\App\Services\DatabaseService');
|
||||
$file_path = base_path('tests/data/' . $file . '.yml');
|
||||
$svc->seed_from_yaml_file($file_path);
|
||||
try {
|
||||
$svc->seed_from_yaml_file($file_path);
|
||||
} catch (Exception $e) {
|
||||
}
|
||||
}
|
||||
|
||||
public function fillableFields(\Illuminate\Database\Eloquent\Model $model)
|
||||
{
|
||||
//$klass = new $model();
|
||||
return $model->fillable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure an object has the list of keys
|
||||
* @param $obj
|
||||
* @param array $keys
|
||||
*/
|
||||
public function assertHasKeys($obj, $keys=[])
|
||||
{
|
||||
foreach($keys as $key) {
|
||||
$this->assertArrayHasKey($key, $obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user