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/@outbox'; $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->getAllEntities($url); $this->assertEquals($cnt + 1, count($messages), "Size of the messages is not right."); $fetchedMessage = null; foreach ($messages as $m) { if ($m['title'] == $randomTitle) { $fetchedMessage = $m; } } $this->assertNotNull($fetchedMessage, "Couldn't find the created message with title $randomTitle"); // Deletes the message. $ret = $this->curlRest($url . '/' . urlencode($fetchedMessage['id']), '', 'application/json', 'DELETE'); $this->assertTrue(empty($ret), "Delete the created message failed. Response: $ret"); $messages = $this->getAllEntities($url, $randomTitle); $this->assertEquals($cnt, count($messages), "Size of the messages is not right after deletion."); } public function testLifeCycleInJson() { $randomTitle = "[" . rand(0, 2048) . "] message test title."; $postData = '{ "id" : "msgid", "recipients" : [2,3], "title" : "' . $randomTitle . '", "titleId" : "541141091700", "body" : "Short message from Joe to some friends", "bodyId" : "5491155811231", "type" : "privateMessage", "status" : "unread" }'; $this->verifyLifeCycle($postData, 'application/json', $randomTitle); } public function testLifeCycleInXml() { $randomTitle = "[" . rand(0, 2048) . "] message test title."; $postData = ' 2 3 ' . $randomTitle . ' msgid Click here to review your invitation. '; $this->verifyLifeCycle($postData, 'application/xml', $randomTitle); } public function testLifeCycleInAtom() { $randomTitle = "[" . rand(0, 2048) . "] message test title."; $postData = ' 2 3 ' . $randomTitle . ' {msgid} Click here to review your invitation. '; $this->verifyLifeCycle($postData, 'application/atom+xml', $randomTitle); } public function testMessageCollectionLifeCycle() { $url = '/messages/1'; // 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'); // Verifies that whether the message collection is created. $retDecoded = json_decode($ret, true); $id = $retDecoded['entry']['id']; $this->assertEquals($cnt + 1, count($this->getAllEntities($url)), "Wrong size of the collections. $ret"); // 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; $ret = $this->curlRest($url . "/$id", json_encode($updateData), 'application/json', 'PUT'); $this->assertTrue(empty($ret), "Update should return empty. $ret <$id>"); $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'); // Verifies that the message collection is deleted. $this->assertEquals($cnt, count($this->getAllEntities($url)), "Wrong size of the collections. $ret"); } }