From 836c65b6fa7e9a90f9baf421713641dfcb7a90aa Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Wed, 12 Jun 2013 20:17:46 +0200 Subject: move to new restful api --- external/feedapi.php | 28 +++++++------- external/folderapi.php | 28 +++++++------- external/itemapi.php | 24 ++++++------ external/newsapiresult.php | 40 ------------------- tests/unit/controller/FeedControllerTest.php | 2 +- tests/unit/external/FeedAPITest.php | 46 ++++++++++------------ tests/unit/external/FolderAPITest.php | 37 +++++++++--------- tests/unit/external/ItemAPITest.php | 58 +++++++++++++--------------- 8 files changed, 110 insertions(+), 153 deletions(-) delete mode 100644 external/newsapiresult.php diff --git a/external/feedapi.php b/external/feedapi.php index dc7e1781b..b9dad00d2 100644 --- a/external/feedapi.php +++ b/external/feedapi.php @@ -28,6 +28,8 @@ namespace OCA\News\External; use \OCA\AppFramework\Core\API; use \OCA\AppFramework\Controller\Controller; use \OCA\AppFramework\Http\Request; +use \OCA\AppFramework\Http\JSONResponse; +use \OCA\AppFramework\Http\Http; use \OCA\News\BusinessLayer\FeedBusinessLayer; use \OCA\News\BusinessLayer\FolderBusinessLayer; @@ -77,7 +79,7 @@ class FeedAPI extends Controller { $this->itemBusinessLayer->getNewestItemId($userId); } catch(BusinessLayerException $ex) {} - return new NewsAPIResult($result); + return new JSONResponse($result); } @@ -104,14 +106,14 @@ class FeedAPI extends Controller { $this->itemBusinessLayer->getNewestItemId($userId); } catch(BusinessLayerException $ex) {} - return new NewsAPIResult($result); + return new JSONResponse($result); } catch(BusinessLayerExistsException $ex) { - return new NewsAPIResult(null, NewsAPIResult::EXISTS_ERROR, - $ex->getMessage()); + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_CONFLICT); } catch(BusinessLayerException $ex) { - return new NewsAPIResult(null, NewsAPIResult::NOT_FOUND_ERROR, - $ex->getMessage()); + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_NOT_FOUND); } } @@ -127,10 +129,10 @@ class FeedAPI extends Controller { try { $this->feedBusinessLayer->delete($feedId, $userId); - return new NewsAPIResult(); + return new JSONResponse(); } catch(BusinessLayerException $ex) { - return new NewsAPIResult(null, NewsAPIResult::NOT_FOUND_ERROR, - $ex->getMessage()); + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_NOT_FOUND); } } @@ -146,7 +148,7 @@ class FeedAPI extends Controller { $newestItemId = (int) $this->params('newestItemId'); $this->itemBusinessLayer->readFeed($feedId, $newestItemId, $userId); - return new NewsAPIResult(); + return new JSONResponse(); } @@ -162,10 +164,10 @@ class FeedAPI extends Controller { try { $this->feedBusinessLayer->move($feedId, $folderId, $userId); - return new NewsAPIResult(); + return new JSONResponse(); } catch(BusinessLayerException $ex) { - return new NewsAPIResult(null, NewsAPIResult::NOT_FOUND_ERROR, - $ex->getMessage()); + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_NOT_FOUND); } } diff --git a/external/folderapi.php b/external/folderapi.php index 5b90eff9e..3a8b7ea6f 100644 --- a/external/folderapi.php +++ b/external/folderapi.php @@ -28,6 +28,8 @@ namespace OCA\News\External; use \OCA\AppFramework\Core\API; use \OCA\AppFramework\Controller\Controller; use \OCA\AppFramework\Http\Request; +use \OCA\AppFramework\Http\JSONResponse; +use \OCA\AppFramework\Http\Http; use \OCA\News\BusinessLayer\FolderBusinessLayer; use \OCA\News\BusinessLayer\ItemBusinessLayer; @@ -65,7 +67,7 @@ class FolderAPI extends Controller { array_push($result['folders'], $folder->toAPI()); } - return new NewsAPIResult($result); + return new JSONResponse($result); } @@ -86,10 +88,10 @@ class FolderAPI extends Controller { $folder = $this->folderBusinessLayer->create($folderName, $userId); array_push($result['folders'], $folder->toAPI()); - return new NewsAPIResult($result); + return new JSONResponse($result); } catch(BusinessLayerExistsException $ex) { - return new NewsAPIResult(null, NewsAPIResult::EXISTS_ERROR, - $ex->getMessage()); + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_CONFLICT); } } @@ -105,10 +107,10 @@ class FolderAPI extends Controller { try { $this->folderBusinessLayer->delete($folderId, $userId); - return new NewsAPIResult(); + return new JSONResponse(); } catch(BusinessLayerException $ex) { - return new NewsAPIResult(null, NewsAPIResult::NOT_FOUND_ERROR, - $ex->getMessage()); + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_NOT_FOUND); } } @@ -125,15 +127,15 @@ class FolderAPI extends Controller { try { $this->folderBusinessLayer->rename($folderId, $folderName, $userId); - return new NewsAPIResult(); + return new JSONResponse(); } catch(BusinessLayerExistsException $ex) { - return new NewsAPIResult(null, NewsAPIResult::EXISTS_ERROR, - $ex->getMessage()); + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_CONFLICT); } catch(BusinessLayerException $ex) { - return new NewsAPIResult(null, NewsAPIResult::NOT_FOUND_ERROR, - $ex->getMessage()); + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_NOT_FOUND); } } @@ -149,7 +151,7 @@ class FolderAPI extends Controller { $newestItemId = (int) $this->params('newestItemId'); $this->itemBusinessLayer->readFolder($folderId, $newestItemId, $userId); - return new NewsAPIResult(); + return new JSONResponse(); } diff --git a/external/itemapi.php b/external/itemapi.php index fae0b3593..611a51fd2 100644 --- a/external/itemapi.php +++ b/external/itemapi.php @@ -28,6 +28,8 @@ namespace OCA\News\External; use \OCA\AppFramework\Core\API; use \OCA\AppFramework\Controller\Controller; use \OCA\AppFramework\Http\Request; +use \OCA\AppFramework\Http\JSONResponse; +use \OCA\AppFramework\Http\Http; use \OCA\News\BusinessLayer\ItemBusinessLayer; use \OCA\News\BusinessLayer\BusinessLayerException; @@ -81,7 +83,7 @@ class ItemAPI extends Controller { array_push($result['items'], $item->toAPI()); } - return new NewsAPIResult($result); + return new JSONResponse($result); } @@ -112,7 +114,7 @@ class ItemAPI extends Controller { array_push($result['items'], $item->toAPI()); } - return new NewsAPIResult($result); + return new JSONResponse($result); } @@ -121,10 +123,10 @@ class ItemAPI extends Controller { $itemId = (int) $this->params('itemId'); try { $this->itemBusinessLayer->read($itemId, $isRead, $userId); - return new NewsAPIResult(); + return new JSONResponse(); } catch(BusinessLayerException $ex){ - return new NewsAPIResult(null, NewsAPIResult::NOT_FOUND_ERROR, - $ex->getMessage()); + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_NOT_FOUND); } } @@ -135,10 +137,10 @@ class ItemAPI extends Controller { $guidHash = $this->params('guidHash'); try { $this->itemBusinessLayer->star($feedId, $guidHash, $isStarred, $userId); - return new NewsAPIResult(); + return new JSONResponse(); } catch(BusinessLayerException $ex){ - return new NewsAPIResult(null, NewsAPIResult::NOT_FOUND_ERROR, - $ex->getMessage()); + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_NOT_FOUND); } } @@ -193,7 +195,7 @@ class ItemAPI extends Controller { $newestItemId = (int) $this->params('newestItemId'); $this->itemBusinessLayer->readAll($newestItemId, $userId); - return new NewsAPIResult(); + return new JSONResponse(); } @@ -209,7 +211,7 @@ class ItemAPI extends Controller { } } - return new NewsAPIResult(); + return new JSONResponse(); } @@ -246,7 +248,7 @@ class ItemAPI extends Controller { } } - return new NewsAPIResult(); + return new JSONResponse(); } diff --git a/external/newsapiresult.php b/external/newsapiresult.php deleted file mode 100644 index e4034bc20..000000000 --- a/external/newsapiresult.php +++ /dev/null @@ -1,40 +0,0 @@ -. -* -*/ - -namespace OCA\News\External; - -use \OCA\AppFramework\External\APIResult; - -class NewsAPIResult extends APIResult { - - const EXISTS_ERROR = 409; - - public function __construct($data=null, $statusCode=NewsAPIResult::OK, - $message=null) { - parent::__construct($data, $statusCode, $message); - } - - -} \ No newline at end of file diff --git a/tests/unit/controller/FeedControllerTest.php b/tests/unit/controller/FeedControllerTest.php index 064fdeeb4..f7d0dc3f7 100644 --- a/tests/unit/controller/FeedControllerTest.php +++ b/tests/unit/controller/FeedControllerTest.php @@ -488,7 +488,7 @@ class FeedControllerTest extends ControllerTestUtility { $response = $this->controller->update(); $render = $response->render(); - $this->assertEquals('{"status":"error","data":[],"msg":"NO!"}', $render); + $this->assertEquals('{"data":[],"status":"error","msg":"NO!"}', $render); $this->assertTrue($response instanceof JSONResponse); } diff --git a/tests/unit/external/FeedAPITest.php b/tests/unit/external/FeedAPITest.php index fd65d742c..b4ab8bfe6 100644 --- a/tests/unit/external/FeedAPITest.php +++ b/tests/unit/external/FeedAPITest.php @@ -27,6 +27,7 @@ namespace OCA\News\External; use \OCA\AppFramework\Http\Request; use \OCA\AppFramework\Http\JSONResponse; +use \OCA\AppFramework\Http\Http; use \OCA\AppFramework\Utility\ControllerTestUtility; use \OCA\News\BusinessLayer\BusinessLayerException; @@ -200,9 +201,8 @@ class FeedAPITest extends ControllerTestUtility { $response = $this->feedAPI->delete(); - $this->assertNull($response->getData()); - $this->assertNull($response->getMessage()); - $this->assertEquals(NewsAPIResult::OK, $response->getStatusCode()); + $this->assertEmpty($response->getData()); + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); } @@ -227,9 +227,9 @@ class FeedAPITest extends ControllerTestUtility { $response = $this->feedAPI->delete(); - $this->assertNull($response->getData()); - $this->assertEquals($this->msg, $response->getMessage()); - $this->assertEquals(NewsAPIResult::NOT_FOUND_ERROR, $response->getStatusCode()); + $data = $response->getData(); + $this->assertEquals($this->msg, $data['message']); + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); } @@ -273,8 +273,7 @@ class FeedAPITest extends ControllerTestUtility { 'newestItemId' => 3 ), $response->getData()); - $this->assertNull($response->getMessage()); - $this->assertEquals(NewsAPIResult::OK, $response->getStatusCode()); + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); } @@ -317,8 +316,7 @@ class FeedAPITest extends ControllerTestUtility { 'feeds' => array($feeds[0]->toAPI()) ), $response->getData()); - $this->assertNull($response->getMessage()); - $this->assertEquals(NewsAPIResult::OK, $response->getStatusCode()); + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); } @@ -336,9 +334,9 @@ class FeedAPITest extends ControllerTestUtility { $response = $this->feedAPI->create(); - $this->assertNull($response->getData()); - $this->assertEquals($this->msg, $response->getMessage()); - $this->assertEquals(NewsAPIResult::EXISTS_ERROR, $response->getStatusCode()); + $data = $response->getData(); + $this->assertEquals($this->msg, $data['message']); + $this->assertEquals(NewsAPIResult::EXISTS_ERROR, $response->getStatus()); } @@ -352,9 +350,9 @@ class FeedAPITest extends ControllerTestUtility { $response = $this->feedAPI->create(); - $this->assertNull($response->getData()); - $this->assertEquals($this->msg, $response->getMessage()); - $this->assertEquals(NewsAPIResult::NOT_FOUND_ERROR, $response->getStatusCode()); + $data = $response->getData(); + $this->assertEquals($this->msg, $data['message']); + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); } @@ -387,9 +385,8 @@ class FeedAPITest extends ControllerTestUtility { $response = $this->feedAPI->read(); - $this->assertNull($response->getData()); - $this->assertNull($response->getMessage()); - $this->assertEquals(NewsAPIResult::OK, $response->getStatusCode()); + $this->assertEmpty($response->getData()); + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); } @@ -422,9 +419,8 @@ class FeedAPITest extends ControllerTestUtility { $response = $this->feedAPI->move(); - $this->assertNull($response->getData()); - $this->assertNull($response->getMessage()); - $this->assertEquals(NewsAPIResult::OK, $response->getStatusCode()); + $this->assertEmpty($response->getData()); + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); } @@ -438,8 +434,8 @@ class FeedAPITest extends ControllerTestUtility { $response = $this->feedAPI->move(); - $this->assertNull($response->getData()); - $this->assertEquals($this->msg, $response->getMessage()); - $this->assertEquals(NewsAPIResult::NOT_FOUND_ERROR, $response->getStatusCode()); + $data = $response->getData(); + $this->assertEquals($this->msg, $data['message']); + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); } } diff --git a/tests/unit/external/FolderAPITest.php b/tests/unit/external/FolderAPITest.php index f9e5490a3..f20cf0ccf 100644 --- a/tests/unit/external/FolderAPITest.php +++ b/tests/unit/external/FolderAPITest.php @@ -28,6 +28,7 @@ namespace OCA\News\External; use \OCA\AppFramework\Http\Request; use \OCA\AppFramework\Http\JSONResponse; use \OCA\AppFramework\Utility\ControllerTestUtility; +use \OCA\AppFramework\Http\Http; use \OCA\News\BusinessLayer\BusinessLayerException; use \OCA\News\BusinessLayer\BusinessLayerExistsException; @@ -179,9 +180,9 @@ class FolderAPITest extends ControllerTestUtility { $response = $this->folderAPI->create(); - $this->assertNull($response->getData()); - $this->assertEquals(NewsAPIResult::EXISTS_ERROR, $response->getStatusCode()); - $this->assertEquals($msg, $response->getMessage()); + $data = $response->getData(); + $this->assertEquals($msg, $data['message']); + $this->assertEquals(Http::STATUS_CONFLICT, $response->getStatus()); } @@ -206,7 +207,7 @@ class FolderAPITest extends ControllerTestUtility { $response = $this->folderAPI->delete(); - $this->assertNull($response->getData()); + $this->assertEmpty($response->getData()); } @@ -231,9 +232,9 @@ class FolderAPITest extends ControllerTestUtility { $response = $this->folderAPI->delete(); - $this->assertNull($response->getData()); - $this->assertEquals($this->msg, $response->getMessage()); - $this->assertEquals(NewsAPIResult::NOT_FOUND_ERROR, $response->getStatusCode()); + $data = $response->getData(); + $this->assertEquals($this->msg, $data['message']); + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); } @@ -269,9 +270,8 @@ class FolderAPITest extends ControllerTestUtility { $response = $this->folderAPI->update(); - $this->assertNull($response->getData()); - $this->assertNull($response->getMessage()); - $this->assertEquals(NewsAPIResult::OK, $response->getStatusCode()); + $this->assertEmpty($response->getData()); + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); } public function testUpdateDoesNotExist() { @@ -304,9 +304,9 @@ class FolderAPITest extends ControllerTestUtility { $response = $this->folderAPI->update(); - $this->assertNull($response->getData()); - $this->assertEquals($this->msg, $response->getMessage()); - $this->assertEquals(NewsAPIResult::NOT_FOUND_ERROR, $response->getStatusCode()); + $data = $response->getData(); + $this->assertEquals($this->msg, $data['message']); + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); } @@ -340,9 +340,9 @@ class FolderAPITest extends ControllerTestUtility { $response = $this->folderAPI->update(); - $this->assertNull($response->getData()); - $this->assertEquals($this->msg, $response->getMessage()); - $this->assertEquals(NewsAPIResult::EXISTS_ERROR, $response->getStatusCode()); + $data = $response->getData(); + $this->assertEquals($this->msg, $data['message']); + $this->assertEquals(Http::STATUS_CONFLICT, $response->getStatus()); } @@ -374,9 +374,8 @@ class FolderAPITest extends ControllerTestUtility { $response = $this->folderAPI->read(); - $this->assertNull($response->getData()); - $this->assertNull($response->getMessage()); - $this->assertEquals(NewsAPIResult::OK, $response->getStatusCode()); + $this->assertEmpty($response->getData()); + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); } diff --git a/tests/unit/external/ItemAPITest.php b/tests/unit/external/ItemAPITest.php index cc66d562d..f20e38b25 100644 --- a/tests/unit/external/ItemAPITest.php +++ b/tests/unit/external/ItemAPITest.php @@ -27,6 +27,7 @@ namespace OCA\News\External; use \OCA\AppFramework\Http\Request; use \OCA\AppFramework\Http\JSONResponse; +use \OCA\AppFramework\Http\Http; use \OCA\AppFramework\Utility\ControllerTestUtility; use \OCA\News\BusinessLayer\BusinessLayerException; @@ -228,9 +229,8 @@ class ItemAPITest extends ControllerTestUtility { $response = $this->itemAPI->read(); - $this->assertNull($response->getData()); - $this->assertNull($response->getMessage()); - $this->assertEquals(NewsAPIResult::OK, $response->getStatusCode()); + $this->assertEmpty($response->getData()); + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); } @@ -253,9 +253,9 @@ class ItemAPITest extends ControllerTestUtility { $response = $this->itemAPI->read(); - $this->assertNull($response->getData()); - $this->assertEquals($this->msg, $response->getMessage()); - $this->assertEquals(NewsAPIResult::NOT_FOUND_ERROR, $response->getStatusCode()); + $data = $response->getData(); + $this->assertEquals($this->msg, $data['message']); + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); } @@ -282,9 +282,8 @@ class ItemAPITest extends ControllerTestUtility { $response = $this->itemAPI->unread(); - $this->assertNull($response->getData()); - $this->assertNull($response->getMessage()); - $this->assertEquals(NewsAPIResult::OK, $response->getStatusCode()); + $this->assertEmpty($response->getData()); + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); } @@ -307,9 +306,9 @@ class ItemAPITest extends ControllerTestUtility { $response = $this->itemAPI->unread(); - $this->assertNull($response->getData()); - $this->assertEquals($this->msg, $response->getMessage()); - $this->assertEquals(NewsAPIResult::NOT_FOUND_ERROR, $response->getStatusCode()); + $data = $response->getData(); + $this->assertEquals($this->msg, $data['message']); + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); } @@ -338,9 +337,8 @@ class ItemAPITest extends ControllerTestUtility { $response = $this->itemAPI->star(); - $this->assertNull($response->getData()); - $this->assertNull($response->getMessage()); - $this->assertEquals(NewsAPIResult::OK, $response->getStatusCode()); + $data = $response->getData(); + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); } @@ -364,9 +362,9 @@ class ItemAPITest extends ControllerTestUtility { $response = $this->itemAPI->star(); - $this->assertNull($response->getData()); - $this->assertEquals($this->msg, $response->getMessage()); - $this->assertEquals(NewsAPIResult::NOT_FOUND_ERROR, $response->getStatusCode()); + $data = $response->getData(); + $this->assertEquals($this->msg, $data['message']); + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); } @@ -395,9 +393,8 @@ class ItemAPITest extends ControllerTestUtility { $response = $this->itemAPI->unstar(); - $this->assertNull($response->getData()); - $this->assertNull($response->getMessage()); - $this->assertEquals(NewsAPIResult::OK, $response->getStatusCode()); + $this->assertEmpty($response->getData()); + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); } @@ -421,9 +418,9 @@ class ItemAPITest extends ControllerTestUtility { $response = $this->itemAPI->unstar(); - $this->assertNull($response->getData()); - $this->assertEquals($this->msg, $response->getMessage()); - $this->assertEquals(NewsAPIResult::NOT_FOUND_ERROR, $response->getStatusCode()); + $data = $response->getData(); + $this->assertEquals($this->msg, $data['message']); + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); } @@ -450,9 +447,8 @@ class ItemAPITest extends ControllerTestUtility { $response = $this->itemAPI->readAll(); - $this->assertNull($response->getData()); - $this->assertNull($response->getMessage()); - $this->assertEquals(NewsAPIResult::OK, $response->getStatusCode()); + $this->assertEmpty($response->getData()); + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); } @@ -481,7 +477,7 @@ class ItemAPITest extends ControllerTestUtility { $this->equalTo(true), $this->equalTo($this->user)); $response = $this->itemAPI->readMultiple(); - $this->assertEquals(NewsAPIResult::OK, $response->getStatusCode()); + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); } @@ -534,7 +530,7 @@ class ItemAPITest extends ControllerTestUtility { $this->equalTo(false), $this->equalTo($this->user)); $response = $this->itemAPI->unreadMultiple(); - $this->assertEquals(NewsAPIResult::OK, $response->getStatusCode()); + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); } @@ -573,7 +569,7 @@ class ItemAPITest extends ControllerTestUtility { $this->equalTo(true), $this->equalTo($this->user)); $response = $this->itemAPI->starMultiple(); - $this->assertEquals(NewsAPIResult::OK, $response->getStatusCode()); + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); } @@ -647,7 +643,7 @@ class ItemAPITest extends ControllerTestUtility { $this->equalTo(false), $this->equalTo($this->user)); $response = $this->itemAPI->unstarMultiple(); - $this->assertEquals(NewsAPIResult::OK, $response->getStatusCode()); + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); } -- cgit v1.2.3