You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shindig.apache.org by ch...@apache.org on 2009/05/12 14:59:56 UTC
svn commit: r773875 - in /incubator/shindig/trunk/php: src/social/model/
src/social/sample/ src/social/service/ src/social/servlet/ test/social/
Author: chabotc
Date: Tue May 12 12:59:55 2009
New Revision: 773875
URL: http://svn.apache.org/viewvc?rev=773875&view=rev
Log:
SHINDIG-1058 by Jinhui Du - Finishing 0.9 messaging changes & adds unit tests
Modified:
incubator/shindig/trunk/php/src/social/model/Message.php
incubator/shindig/trunk/php/src/social/model/MessageCollection.php
incubator/shindig/trunk/php/src/social/sample/JsonDbOpensocialService.php
incubator/shindig/trunk/php/src/social/service/MessagesHandler.php
incubator/shindig/trunk/php/src/social/servlet/DataServiceServlet.php
incubator/shindig/trunk/php/test/social/MessageRestTest.php
incubator/shindig/trunk/php/test/social/MessageTest.php
Modified: incubator/shindig/trunk/php/src/social/model/Message.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/social/model/Message.php?rev=773875&r1=773874&r2=773875&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/social/model/Message.php (original)
+++ incubator/shindig/trunk/php/src/social/model/Message.php Tue May 12 12:59:55 2009
@@ -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 @@
'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 @@
public function setRecipients($recipients) {
$this->recipients = $recipients;
}
-
+
public function getReplies() {
return $this->replies;
}
-
+
public function setReplies($replies) {
$this->replies = $replies;
}
@@ -135,7 +138,7 @@
public function getSenderId() {
return $this->senderId;
}
-
+
public function setSenderId($senderId) {
$this->senderId = $senderId;
}
@@ -171,7 +174,7 @@
public function setType($type) {
$this->type = $type;
}
-
+
public function getUpdated() {
return $this->updated;
}
@@ -195,7 +198,7 @@
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
Modified: incubator/shindig/trunk/php/src/social/model/MessageCollection.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/social/model/MessageCollection.php?rev=773875&r1=773874&r2=773875&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/social/model/MessageCollection.php (original)
+++ incubator/shindig/trunk/php/src/social/model/MessageCollection.php Tue May 12 12:59:55 2009
@@ -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');
Modified: incubator/shindig/trunk/php/src/social/sample/JsonDbOpensocialService.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/social/sample/JsonDbOpensocialService.php?rev=773875&r1=773874&r2=773875&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/social/sample/JsonDbOpensocialService.php (original)
+++ incubator/shindig/trunk/php/src/social/sample/JsonDbOpensocialService.php Tue May 12 12:59:55 2009
@@ -485,6 +485,9 @@
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 @@
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 @@
$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 @@
$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);
}
Modified: incubator/shindig/trunk/php/src/social/service/MessagesHandler.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/social/service/MessagesHandler.php?rev=773875&r1=773874&r2=773875&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/social/service/MessagesHandler.php (original)
+++ incubator/shindig/trunk/php/src/social/service/MessagesHandler.php Tue May 12 12:59:55 2009
@@ -94,6 +94,8 @@
// 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 @@
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 @@
$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());
}
Modified: incubator/shindig/trunk/php/src/social/servlet/DataServiceServlet.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/social/servlet/DataServiceServlet.php?rev=773875&r1=773874&r2=773875&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/social/servlet/DataServiceServlet.php (original)
+++ incubator/shindig/trunk/php/src/social/servlet/DataServiceServlet.php Tue May 12 12:59:55 2009
@@ -110,6 +110,13 @@
//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()) {
Modified: incubator/shindig/trunk/php/test/social/MessageRestTest.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/test/social/MessageRestTest.php?rev=773875&r1=773874&r2=773875&view=diff
==============================================================================
--- incubator/shindig/trunk/php/test/social/MessageRestTest.php (original)
+++ incubator/shindig/trunk/php/test/social/MessageRestTest.php Tue May 12 12:59:55 2009
@@ -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 @@
$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 @@
$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 @@
public function testLifeCycleInXml() {
$randomTitle = "[" . rand(0, 2048) . "] message test title.";
$postData = '<message xmlns="http://ns.opensocial.org/2008/opensocial">
- <recipient>1</recipient>
<recipient>2</recipient>
<recipient>3</recipient>
<title>' . $randomTitle . '</title>
@@ -89,7 +97,6 @@
$randomTitle = "[" . rand(0, 2048) . "] message test title.";
$postData = '<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:osapi="http://opensocial.org/2008/opensocialapi">
- <osapi:recipient>1</osapi:recipient>
<osapi:recipient>2</osapi:recipient>
<osapi:recipient>3</osapi:recipient>
<title>' . $randomTitle . '</title>
@@ -102,33 +109,47 @@
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");
}
}
-
Modified: incubator/shindig/trunk/php/test/social/MessageTest.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/test/social/MessageTest.php?rev=773875&r1=773874&r2=773875&view=diff
==============================================================================
--- incubator/shindig/trunk/php/test/social/MessageTest.php (original)
+++ incubator/shindig/trunk/php/test/social/MessageTest.php Tue May 12 12:59:55 2009
@@ -26,21 +26,23 @@
/**
* @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 @@
* 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());
}
}