/tests/unit/WindowsAzure/ServiceRuntime/Internal/ChunkedGoalStateDeserializerTest.php

https://bitbucket.org/skudatech/azure-sdk-for-php · PHP · 230 lines · 144 code · 33 blank · 53 comment · 0 complexity · 15ed2820d071a648b9e574b19f4abb07 MD5 · raw file

  1. <?php
  2. /**
  3. * LICENSE: Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. *
  14. * PHP version 5
  15. *
  16. * @category Microsoft
  17. * @package Tests\Unit\WindowsAzure\ServiceRuntime\Internal
  18. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  19. * @copyright 2012 Microsoft Corporation
  20. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  21. * @link https://github.com/windowsazure/azure-sdk-for-php
  22. */
  23. namespace Tests\Unit\WindowsAzure\ServiceRuntime\Internal;
  24. use Tests\Framework\TestResources;
  25. use WindowsAzure\ServiceRuntime\Internal\ChunkedGoalStateDeserializer;
  26. use WindowsAzure\ServiceRuntime\Internal\FileInputChannel;
  27. require_once 'vfsStream/vfsStream.php';
  28. /**
  29. * Unit tests for class ChunkedGoalStateDeserializer.
  30. *
  31. * @category Microsoft
  32. * @package Tests\Unit\WindowsAzure\ServiceRuntime\Internal
  33. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  34. * @copyright 2012 Microsoft Corporation
  35. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  36. * @version Release: @package_version@
  37. * @link https://github.com/windowsazure/azure-sdk-for-php
  38. */
  39. class ChunkedGoalStateDeserializerTest extends \PHPUnit_Framework_TestCase
  40. {
  41. /**
  42. * @covers WindowsAzure\ServiceRuntime\Internal\ChunkedGoalStateDeserializer::__construct
  43. * @covers WindowsAzure\ServiceRuntime\Internal\ChunkedGoalStateDeserializer::initialize
  44. * @covers WindowsAzure\ServiceRuntime\Internal\ChunkedGoalStateDeserializer::deserialize
  45. */
  46. public function testDeserialize()
  47. {
  48. // Setup
  49. $rootDirectory = 'root';
  50. \vfsStreamWrapper::register();
  51. \vfsStreamWrapper::setRoot(new \vfsStreamDirectory($rootDirectory));
  52. $roleEnvironmentPath = 'mypath';
  53. $currentStateEndpoint = 'endpoint';
  54. $incarnation = 1;
  55. $expectedState = 'started';
  56. $fileName = 'file';
  57. $fileContent = '<?xml version="1.0" encoding="utf-8"?>' .
  58. '<GoalState xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' .
  59. 'xmlns:xsd="http://www.w3.org/2001/XMLSchema">' .
  60. '<Incarnation>' .
  61. $incarnation .
  62. '</Incarnation>' .
  63. '<ExpectedState>' .
  64. $expectedState .
  65. '</ExpectedState>' .
  66. '<RoleEnvironmentPath>' .
  67. $roleEnvironmentPath .
  68. '</RoleEnvironmentPath>' .
  69. '<CurrentStateEndpoint>' .
  70. $currentStateEndpoint .
  71. '</CurrentStateEndpoint>' .
  72. '<Deadline>9999-12-31T23:59:59.9999999</Deadline>' .
  73. '</GoalState>';
  74. $fileContent = dechex(strlen($fileContent)) . "\n" . $fileContent;
  75. $file = \vfsStream::newFile($fileName);
  76. $file->setContent($fileContent);
  77. \vfsStreamWrapper::getRoot()->addChild($file);
  78. $fileInputChannel = new FileInputChannel();
  79. $fileInputStream = $fileInputChannel->getInputStream(
  80. \vfsStream::url($rootDirectory . '/' . $fileName)
  81. );
  82. $chunkedGoalStateDeserializer = new ChunkedGoalStateDeserializer();
  83. $chunkedGoalStateDeserializer->initialize($fileInputStream);
  84. $goalState = $chunkedGoalStateDeserializer->deserialize();
  85. // Test
  86. $this->assertNotEquals(null, $goalState);
  87. $this->assertEquals($roleEnvironmentPath, $goalState->getEnvironmentPath());
  88. $this->assertNotEquals(null, $goalState->getDeadline());
  89. $this->assertEquals($currentStateEndpoint, $goalState->getCurrentStateEndpoint());
  90. $this->assertEquals($incarnation, $goalState->getIncarnation());
  91. $this->assertEquals($expectedState, $goalState->getExpectedState());
  92. }
  93. /**
  94. * @covers WindowsAzure\ServiceRuntime\Internal\ChunkedGoalStateDeserializer::__construct
  95. * @covers WindowsAzure\ServiceRuntime\Internal\ChunkedGoalStateDeserializer::initialize
  96. * @covers WindowsAzure\ServiceRuntime\Internal\ChunkedGoalStateDeserializer::deserialize
  97. */
  98. public function testDeserializeWithNewLineEnd()
  99. {
  100. // Setup
  101. $rootDirectory = 'root';
  102. \vfsStreamWrapper::register();
  103. \vfsStreamWrapper::setRoot(new \vfsStreamDirectory($rootDirectory));
  104. $roleEnvironmentPath = 'mypath';
  105. $currentStateEndpoint = 'endpoint';
  106. $incarnation = 1;
  107. $expectedState = 'started';
  108. $fileName = 'file';
  109. $fileContent = '<?xml version="1.0" encoding="utf-8"?>' .
  110. '<GoalState xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' .
  111. 'xmlns:xsd="http://www.w3.org/2001/XMLSchema">' .
  112. '<Incarnation>' .
  113. $incarnation .
  114. '</Incarnation>' .
  115. '<ExpectedState>' .
  116. $expectedState .
  117. '</ExpectedState>' .
  118. '<RoleEnvironmentPath>' .
  119. $roleEnvironmentPath .
  120. '</RoleEnvironmentPath>' .
  121. '<CurrentStateEndpoint>' .
  122. $currentStateEndpoint .
  123. '</CurrentStateEndpoint>' .
  124. '<Deadline>9999-12-31T23:59:59.9999999</Deadline>' .
  125. '</GoalState>';
  126. $fileContent = dechex(strlen($fileContent)) . "\n" . $fileContent . "\n";
  127. $file = \vfsStream::newFile($fileName);
  128. $file->setContent($fileContent);
  129. \vfsStreamWrapper::getRoot()->addChild($file);
  130. $fileInputChannel = new FileInputChannel();
  131. $fileInputStream = $fileInputChannel->getInputStream(
  132. \vfsStream::url($rootDirectory . '/' . $fileName)
  133. );
  134. $chunkedGoalStateDeserializer = new ChunkedGoalStateDeserializer();
  135. $chunkedGoalStateDeserializer->initialize($fileInputStream);
  136. $goalState = $chunkedGoalStateDeserializer->deserialize();
  137. // Test
  138. $this->assertNotEquals(null, $goalState);
  139. $this->assertEquals($roleEnvironmentPath, $goalState->getEnvironmentPath());
  140. $this->assertNotEquals(null, $goalState->getDeadline());
  141. $this->assertEquals($currentStateEndpoint, $goalState->getCurrentStateEndpoint());
  142. $this->assertEquals($incarnation, $goalState->getIncarnation());
  143. $this->assertEquals($expectedState, $goalState->getExpectedState());
  144. }
  145. /**
  146. * @covers WindowsAzure\ServiceRuntime\Internal\ChunkedGoalStateDeserializer::__construct
  147. * @covers WindowsAzure\ServiceRuntime\Internal\ChunkedGoalStateDeserializer::initialize
  148. * @covers WindowsAzure\ServiceRuntime\Internal\ChunkedGoalStateDeserializer::deserialize
  149. */
  150. public function testDeserializeWithNewLineStart()
  151. {
  152. // Setup
  153. $rootDirectory = 'root';
  154. \vfsStreamWrapper::register();
  155. \vfsStreamWrapper::setRoot(new \vfsStreamDirectory($rootDirectory));
  156. $roleEnvironmentPath = 'mypath';
  157. $currentStateEndpoint = 'endpoint';
  158. $incarnation = 1;
  159. $expectedState = 'started';
  160. $fileName = 'file';
  161. $fileContent = '<?xml version="1.0" encoding="utf-8"?>' .
  162. '<GoalState xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' .
  163. 'xmlns:xsd="http://www.w3.org/2001/XMLSchema">' .
  164. '<Incarnation>' .
  165. $incarnation .
  166. '</Incarnation>' .
  167. '<ExpectedState>' .
  168. $expectedState .
  169. '</ExpectedState>' .
  170. '<RoleEnvironmentPath>' .
  171. $roleEnvironmentPath .
  172. '</RoleEnvironmentPath>' .
  173. '<CurrentStateEndpoint>' .
  174. $currentStateEndpoint .
  175. '</CurrentStateEndpoint>' .
  176. '<Deadline>9999-12-31T23:59:59.9999999</Deadline>' .
  177. '</GoalState>';
  178. $fileContent = "\n" . dechex(strlen($fileContent)) . "\n" . $fileContent;
  179. $file = \vfsStream::newFile($fileName);
  180. $file->setContent($fileContent);
  181. \vfsStreamWrapper::getRoot()->addChild($file);
  182. $fileInputChannel = new FileInputChannel();
  183. $fileInputStream = $fileInputChannel->getInputStream(
  184. \vfsStream::url($rootDirectory . '/' . $fileName)
  185. );
  186. $chunkedGoalStateDeserializer = new ChunkedGoalStateDeserializer();
  187. $chunkedGoalStateDeserializer->initialize($fileInputStream);
  188. $goalState = $chunkedGoalStateDeserializer->deserialize();
  189. // Test
  190. $this->assertNotEquals(null, $goalState);
  191. $this->assertEquals($roleEnvironmentPath, $goalState->getEnvironmentPath());
  192. $this->assertNotEquals(null, $goalState->getDeadline());
  193. $this->assertEquals($currentStateEndpoint, $goalState->getCurrentStateEndpoint());
  194. $this->assertEquals($incarnation, $goalState->getIncarnation());
  195. $this->assertEquals($expectedState, $goalState->getExpectedState());
  196. }
  197. }