From 31c44af02bd2a32fd356d0d4d5974802162dd137 Mon Sep 17 00:00:00 2001 From: Chris Chabot Date: Tue, 12 May 2009 12:59:55 +0000 Subject: [PATCH] SHINDIG-1058 by Jinhui Du - Finishing 0.9 messaging changes & adds unit tests git-svn-id: https://svn.apache.org/repos/asf/incubator/shindig/trunk@773875 13f79535-47bb-0310-9956-ffa450edef68 --- php/src/social/model/Message.php | 67 +++++++++--------- php/src/social/model/MessageCollection.php | 21 +++--- php/src/social/sample/JsonDbOpensocialService.php | 14 +++- php/src/social/service/MessagesHandler.php | 18 +++-- php/src/social/servlet/DataServiceServlet.php | 7 ++ php/test/social/MessageRestTest.php | 83 ++++++++++++++--------- php/test/social/MessageTest.php | 28 ++++---- 7 files changed, 146 insertions(+), 92 deletions(-) diff --git a/php/src/social/model/Message.php b/php/src/social/model/Message.php index 6d3bf5e8..a18f6664 100644 --- a/php/src/social/model/Message.php +++ b/php/src/social/model/Message.php @@ -23,26 +23,30 @@ * http://opensocial-resources.googlecode.com/svn/spec/draft/OpenSocial-Specification.xml#opensocial.Message.Field */ class Message { - private $appUrl; - private $body; - private $bodyId; - private $collectionIds = array(); - private $id; - private $inReplyTo; - private $recipients = array(); - private $replies = array(); - private $senderId; - private $status; - private $timeSent; - private $title; - private $titleId; - private $type; - private $updated; - private $urls = array(); - - public static $DEFAULT_FIELDS = array('appUrl', 'body', 'bodyId', 'collectionIds', 'id', 'inReplyTo', 'recipients', 'replies', 'senderId', 'status', 'timeSent', 'title', 'titleId', 'type', 'updated', 'urls'); - - public $types = array( + // These fields should be referenced via getters and setters. 'public' only for json_encode. + public $appUrl; + public $body; + public $bodyId; + public $collectionIds; + public $id; + public $inReplyTo; + public $recipients; + public $replies; + public $senderId; + public $status; + public $timeSent; + public $title; + public $titleId; + public $type; + public $updated; + public $urls; + + public static $DEFAULT_FIELDS = array('appUrl', 'body', 'bodyId', + 'collectionIds', 'id', 'inReplyTo', 'recipients', 'replies', + 'senderId', 'status', 'timeSent', 'title', 'titleId', 'type', + 'updated', 'urls'); + + public static $TYPES = array( /* An email */ 'EMAIL', /* A short private message */ @@ -51,19 +55,18 @@ class Message { 'PRIVATE_MESSAGE', /* A message to a specific user that can be seen by more than that user */ 'PUBLIC_MESSAGE'); + + public static $STATUS = array('NEW', 'READ', 'DELETED'); - public $statuses = array('NEW', 'READ', 'DELETED'); - - public function __construct($initBody, $initTitle, $initType) { - $this->setBody($initBody); - $this->setTitle($initTitle); - $this->setType($initType); + public function __construct($id, $title) { + $this->setId($id); + $this->setTitle($title); } public function getAppUrl() { return $this->appUrl; } - + public function setAppUrl($url) { $this->url = $url; } @@ -115,11 +118,11 @@ class Message { public function setRecipients($recipients) { $this->recipients = $recipients; } - + public function getReplies() { return $this->replies; } - + public function setReplies($replies) { $this->replies = $replies; } @@ -135,7 +138,7 @@ class Message { public function getSenderId() { return $this->senderId; } - + public function setSenderId($senderId) { $this->senderId = $senderId; } @@ -171,7 +174,7 @@ class Message { public function setType($type) { $this->type = $type; } - + public function getUpdated() { return $this->updated; } @@ -195,7 +198,7 @@ class Message { public function setUrls($urls) { $this->urls = $urls; } - + /** * TODO implement either a standard 'sanitizing' facility or * define an interface that can be set on this class so diff --git a/php/src/social/model/MessageCollection.php b/php/src/social/model/MessageCollection.php index 516c597c..49156270 100644 --- a/php/src/social/model/MessageCollection.php +++ b/php/src/social/model/MessageCollection.php @@ -25,17 +25,20 @@ */ class MessageCollection { // Indicates the collection of all messages sent to the user - public static $INBOX = "@inbox"; + public static $INBOX = '@inbox'; // Indicates the collection of all messages sent by the user // and used as a special endpoint for posting outbound messages. - public static $OUTBOX = "@outbox"; - - private $id; - private $title; - private $total; - private $unread; - private $updated; - private $urls = array(); + public static $OUTBOX = '@outbox'; + // All the messages both sent from and to the user. + public static $ALL = '@all'; + + // These fileds should be referenced via getters and setters. 'public' only for json_encode. + public $id; + public $title; + public $total; + public $unread; + public $updated; + public $urls = array(); public static $DEFAULT_FIELDS = array('id', 'title', 'total', 'unread', 'updated', 'urls'); diff --git a/php/src/social/sample/JsonDbOpensocialService.php b/php/src/social/sample/JsonDbOpensocialService.php index e7d4d834..e42b1e33 100644 --- a/php/src/social/sample/JsonDbOpensocialService.php +++ b/php/src/social/sample/JsonDbOpensocialService.php @@ -485,6 +485,9 @@ class JsonDbOpensocialService implements ActivityService, PersonService, AppData public function createMessage($userId, $msgCollId, $message, $token) { $db = $this->getDb(); $messagesTable = $this->getAllMessageCollections(); + if ($msgCollId == '@outbox') { + $msgCollId = 'privateMessage'; + } if (! isset($messagesTable[$userId->getUserId($token)]) || ! isset($messagesTable[$userId->getUserId($token)][$msgCollId])) { throw new SocialSpiException("Message collection not found.", ResponseError::$BAD_REQUEST); } @@ -516,6 +519,9 @@ class JsonDbOpensocialService implements ActivityService, PersonService, AppData public function deleteMessages($userId, $msgCollId, $messageIds, $token) { $db = $this->getDb(); $messagesTable = $this->getAllMessageCollections(); + if ($msgCollId == '@inbox' || $msgCollId == '@outbox') { + $msgCollId = 'privateMessage'; + } if (! isset($messagesTable[$userId->getUserId($token)]) || ! isset($messagesTable[$userId->getUserId($token)][$msgCollId])) { throw new SocialSpiException("Message collection not found.", ResponseError::$BAD_REQUEST); } @@ -540,6 +546,9 @@ class JsonDbOpensocialService implements ActivityService, PersonService, AppData $collections = $this->getAllMessageCollections(); $results = array(); // TODO: Handles @inbox and @outbox. + if ($msgCollId == '@outbox' || $msgCollId == '@inbox') { + $msgCollId = 'privateMessage'; + } if (isset($collections[$userId->getUserId($token)]) && isset($collections[$userId->getUserId($token)][$msgCollId])) { $msgColl = $collections[$userId->getUserId($token)][$msgCollId]; if (! isset($msgColl['messages'])) { @@ -578,9 +587,12 @@ class JsonDbOpensocialService implements ActivityService, PersonService, AppData $msgCollection['total'] = 0; $msgCollection['unread'] = 0; $msgCollection['updated'] = time(); - $messagesTable[$userId->getUserId($token)][$msgCollection['id']] = $msgCollection; + $id = count($messagesTable[$userId->getUserId($token)]); + $msgCollection['id'] = $id; + $messagesTable[$userId->getUserId($token)][$id] = $msgCollection; $db[self::$MESSAGES_TABLE] = $messagesTable; $this->saveDb($db); + return $msgCollection; } catch (Exception $e) { throw new SocialSpiException("Message collection can't be created: " . $e->getMessage(), ResponseError::$INTERNAL_ERROR); } diff --git a/php/src/social/service/MessagesHandler.php b/php/src/social/service/MessagesHandler.php index 605c6ddc..44a81c16 100644 --- a/php/src/social/service/MessagesHandler.php +++ b/php/src/social/service/MessagesHandler.php @@ -94,6 +94,8 @@ class MessagesHandler extends DataRequestHandler { // Creates a message collection. $messageCollection = $requestItem->getParameter("entity"); HandlerPreconditions::requireNotEmpty($messageCollection, "Can't parse message collection."); + $title = isset($messageCollection['title']) ? trim($messageCollection['title']) : ''; + HandlerPreconditions::requireCondition(strlen($title) > 0, "Title must be specified."); return $this->service->createMessageCollection($userIds[0], $messageCollection, $requestItem->getToken()); } else { // Creates a message. @@ -104,8 +106,10 @@ class MessagesHandler extends DataRequestHandler { HandlerPreconditions::requireEmpty($messageIds, "messageId cannot be specified in create method."); // Message fields validation. - HandlerPreconditions::requireCondition(! ($message['title'] === null && $message['body'] === null), "title and/or body should be specified."); + $title = isset($message['title']) ? trim($message['title']) : ''; + HandlerPreconditions::requireCondition(strlen($title) > 0, "Title must be specified."); HandlerPreconditions::requireNotEmpty($message['recipients'], "Field recipients is required."); + HandlerPreconditions::requireCondition(is_array($message['recipients']), "recipients must be array."); return $this->service->createMessage($userIds[0], $msgCollId, $message, $requestItem->getToken()); } @@ -118,21 +122,23 @@ class MessagesHandler extends DataRequestHandler { $requestItem->applyUrlTemplate(self::$MESSAGES_PATH); $userIds = $requestItem->getUsers(); - HandlerPreconditions::requireSingular("UserId is not singular."); + HandlerPreconditions::requireSingular($userIds, "UserId is not singular."); $msgCollId = $requestItem->getParameter("msgCollId"); HandlerPreconditions::requireNotEmpty($msgCollId, "msgCollId is required."); $messageIds = $requestItem->getListParameter("messageId"); if (empty($messageIds)) { - // Updates message collection. NOTE: "message" is used here to represent message collection. - $messageCollection = $requestItem->getParameter("message"); + // Updates message collection. + $messageCollection = $requestItem->getParameter("entity"); + $messageCollection['id'] = $msgCollId; HandlerPreconditions::requireNotEmpty($messageCollection, "Can't parse message collection."); return $this->service->updateMessageCollection($userIds[0], $messageCollection, $requestItem->getToken()); } else { // Updates a message. - HandlerPreconditions::requireSingular("UserId is not singular."); - $message = $requestItem->getParameter("message"); + HandlerPreconditions::requireSingular($messageIds, "Message id is not singular."); + $message = $requestItem->getParameter("entity"); + $message['id'] = $messageIds[0]; HandlerPreconditions::requireNotEmpty($message, "Can't parse message."); return $this->service->updateMessage($userIds[0], $msgCollId, $message, $requestItem->getToken()); } diff --git a/php/src/social/servlet/DataServiceServlet.php b/php/src/social/servlet/DataServiceServlet.php index 3128297e..892737ae 100644 --- a/php/src/social/servlet/DataServiceServlet.php +++ b/php/src/social/servlet/DataServiceServlet.php @@ -110,6 +110,13 @@ class DataServiceServlet extends ApiServlet { //uri example: /social/rest/people/@self /gadgets/api/rest/cache/invalidate $servletRequest = array( 'url' => substr($_SERVER["REQUEST_URI"], strpos($_SERVER["REQUEST_URI"], '/rest') + 5)); + // Php version 5.2.9(linux) doesn't set HTTP_RAW_POST_DATA properly. + if (!isset($GLOBALS['HTTP_RAW_POST_DATA'])) { + $tmp = file_get_contents('php://input'); + if (!empty($tmp)) { + $GLOBALS['HTTP_RAW_POST_DATA'] = $tmp; + } + } if (isset($GLOBALS['HTTP_RAW_POST_DATA'])) { $servletRequest['postData'] = $GLOBALS['HTTP_RAW_POST_DATA']; if (get_magic_quotes_gpc()) { diff --git a/php/test/social/MessageRestTest.php b/php/test/social/MessageRestTest.php index 7c962f4a..431ecb81 100644 --- a/php/test/social/MessageRestTest.php +++ b/php/test/social/MessageRestTest.php @@ -20,26 +20,35 @@ require_once 'RestBase.php'; +/** + * It is an integration test. Since it's may be used by the specified container implementation. + * It'd better not to depends on the data in the json DB sample. + */ class MessageRestTest extends RestBase { - - private function getMessages($url) { - $ret = $this->curlRest($url, '', 'application/json', 'GET'); + + private function getAllEntities($url) { + $sep = strpos($url, '?') !== false ? '&' : '?'; + $ret = $this->curlRest($url . $sep . 'startIndex=0&count=1000000', '', 'application/json', 'GET'); $retDecoded = json_decode($ret, true); $this->assertTrue($ret != $retDecoded && $ret != null, "Invalid json response: $retDecoded"); return $retDecoded['entry']; } - + + /** + * NOTE: If there are lots of messages in the storage this test may take a long time as + * it retrieves all the message. + */ private function verifyLifeCycle($postData, $postDataFormat, $randomTitle) { - $url = '/messages/1/notification'; + $url = '/messages/1/@outbox'; - $cnt = count($this->getMessages($url)); + $cnt = count($this->getAllEntities($url)); // Creates the message. $ret = $this->curlRest($url, $postData, $postDataFormat, 'POST'); $this->assertTrue(empty($ret), "Create message failed. Response: $ret"); // Gets the message. - $messages = $this->getMessages($url); + $messages = $this->getAllEntities($url); $this->assertEquals($cnt + 1, count($messages), "Size of the messages is not right."); $fetchedMessage = null; foreach ($messages as $m) { @@ -53,7 +62,7 @@ class MessageRestTest extends RestBase { $ret = $this->curlRest($url . '/' . urlencode($fetchedMessage['id']), '', 'application/json', 'DELETE'); $this->assertTrue(empty($ret), "Delete the created message failed. Response: $ret"); - $messages = $this->getMessages($url, $randomTitle); + $messages = $this->getAllEntities($url, $randomTitle); $this->assertEquals($cnt, count($messages), "Size of the messages is not right after deletion."); } @@ -61,7 +70,7 @@ class MessageRestTest extends RestBase { $randomTitle = "[" . rand(0, 2048) . "] message test title."; $postData = '{ "id" : "msgid", - "recipients" : [1, 2, 3], + "recipients" : [2,3], "title" : "' . $randomTitle . '", "titleId" : "541141091700", "body" : "Short message from Joe to some friends", @@ -75,7 +84,6 @@ class MessageRestTest extends RestBase { public function testLifeCycleInXml() { $randomTitle = "[" . rand(0, 2048) . "] message test title."; $postData = ' - 1 2 3 ' . $randomTitle . ' @@ -89,7 +97,6 @@ class MessageRestTest extends RestBase { $randomTitle = "[" . rand(0, 2048) . "] message test title."; $postData = ' - 1 2 3 ' . $randomTitle . ' @@ -102,33 +109,47 @@ class MessageRestTest extends RestBase { public function testMessageCollectionLifeCycle() { $url = '/messages/1'; - $randomTitle = "[" . rand(0, 2048) . "] message collection test title."; + // Gets number of message collections in the repository. + $cnt = count($this->getAllEntities($url)); + + // Creates a message collection. + $createData = array(); + $createData['title'] = "[" . rand(0, 2048) . "] message collection test title."; + $createData['urls'] = array("http://abc.com/abc", "http://xyz.com/xyz"); + $ret = $this->curlRest($url, json_encode($createData), 'application/json', 'POST'); - $ret = $this->curlRest($url, '', 'application/json', 'GET'); + // Verifies that whether the message collection is created. $retDecoded = json_decode($ret, true); - $this->assertTrue($ret != $retDecoded && $ret != null, "Invalid json response: $retDecoded"); - $cnt = count($retDecoded['entry']); + $id = $retDecoded['entry']['id']; + $this->assertEquals($cnt + 1, count($this->getAllEntities($url)), "Wrong size of the collections. $ret"); - $id = 'msgCollId'; + // Updates the created message collection. + $newUrls = array("http://123.com/123"); + $newTitle = 'new title'; + $updateData = array(); + $updateData['id'] = $id; + $updateData['title'] = $newTitle; + $updateData['urls'] = $newUrls; - $postData = '{ - "id" : "' . $id . '", - "title" : "' . $randomTitle . '" - }'; - $ret = $this->curlRest($url, $postData, 'application/json', 'POST'); - $this->assertTrue(empty($ret), "Create message collection failed. Response: $ret"); + $ret = $this->curlRest($url . "/$id", json_encode($updateData), 'application/json', 'PUT'); + $this->assertTrue(empty($ret), "Update should return empty. $ret <$id>"); - $ret = $this->curlRest($url, '', 'application/json', 'GET'); - $retDecoded = json_decode($ret, true); - $this->assertTrue($ret != $retDecoded && $ret != null, "Invalid json response: $retDecoded"); - $this->assertEquals($cnt + 1, count($retDecoded['entry']), "Wrong size of the collections. $ret"); + $collections = $this->getAllEntities($url); + $this->assertEquals($cnt + 1, count($collections), "Wrong size of the collections."); + $found = false; + foreach ($collections as $collection) { + if ($collection['id'] == $id) { + $this->assertEquals($newTitle, $collection['title']); + $this->assertEquals($newUrls, $collection['urls']); + $found = true; + } + } + $this->assertTrue($found, "Created message not found."); + // Deletes the message collection. $ret = $this->curlRest($url . "/$id", '', 'application/json', 'DELETE'); - $ret = $this->curlRest($url, '', 'application/json', 'GET'); - $retDecoded = json_decode($ret, true); - $this->assertTrue($ret != $retDecoded && $ret != null, "Invalid json response: $retDecoded"); - $this->assertEquals($cnt, count($retDecoded['entry']), "Wrong size of the collections. $ret"); + // Verifies that the message collection is deleted. + $this->assertEquals($cnt, count($this->getAllEntities($url)), "Wrong size of the collections. $ret"); } } - diff --git a/php/test/social/MessageTest.php b/php/test/social/MessageTest.php index b4690388..c7fa6674 100644 --- a/php/test/social/MessageTest.php +++ b/php/test/social/MessageTest.php @@ -26,21 +26,23 @@ class MessageTest extends PHPUnit_Framework_TestCase { /** * @var Message */ - private $Message; + private $message; /** * Prepares the environment before running a test. */ protected function setUp() { parent::setUp(); - $this->Message = new Message('BODY', 'TITLE', 'NOTIFICATION'); + $this->message = new Message(1, 'TITLE'); + $this->message->setBody('BODY'); + $this->message->setType('NOTIFICATION'); } /** * Cleans up the environment after running a test. */ protected function tearDown() { - $this->Message = null; + $this->message = null; parent::tearDown(); } @@ -48,51 +50,51 @@ class MessageTest extends PHPUnit_Framework_TestCase { * Tests Message->getBody() */ public function testGetBody() { - $this->assertEquals('BODY', $this->Message->getBody()); + $this->assertEquals('BODY', $this->message->getBody()); } /** * Tests Message->getTitle() */ public function testGetTitle() { - $this->assertEquals('TITLE', $this->Message->getTitle()); + $this->assertEquals('TITLE', $this->message->getTitle()); } /** * Tests Message->getType() */ public function testGetType() { - $this->assertEquals('NOTIFICATION', $this->Message->getType()); + $this->assertEquals('NOTIFICATION', $this->message->getType()); } /** * Tests Message->sanitizeHTML() */ public function testSanitizeHTML() { - $this->assertEquals('ABC', $this->Message->sanitizeHTML('ABC')); + $this->assertEquals('ABC', $this->message->sanitizeHTML('ABC')); } /** * Tests Message->setBody() */ public function testSetBody() { - $this->Message->setBody('body'); - $this->assertEquals('body', $this->Message->getBody()); + $this->message->setBody('body'); + $this->assertEquals('body', $this->message->getBody()); } /** * Tests Message->setTitle() */ public function testSetTitle() { - $this->Message->setTitle('title'); - $this->assertEquals('title', $this->Message->getTitle()); + $this->message->setTitle('title'); + $this->assertEquals('title', $this->message->getTitle()); } /** * Tests Message->setType() */ public function testSetType() { - $this->Message->setType('EMAIL'); - $this->assertEquals('EMAIL', $this->Message->getType()); + $this->message->setType('EMAIL'); + $this->assertEquals('EMAIL', $this->message->getType()); } } -- 2.11.4.GIT