PageRenderTime 59ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/kurniawanchan83/azure-sdk-for-php
PHP | 304 lines | 201 code | 51 blank | 52 comment | 0 complexity | 72ec57a548229b981ff56a5cc133d5cc 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\Common\Internal\Utilities;
  26. use WindowsAzure\ServiceRuntime\Internal\AcquireCurrentState;
  27. use WindowsAzure\ServiceRuntime\Internal\ChunkedGoalStateDeserializer;
  28. use WindowsAzure\ServiceRuntime\Internal\CurrentStatus;
  29. use WindowsAzure\ServiceRuntime\Internal\FileInputChannel;
  30. use WindowsAzure\ServiceRuntime\Internal\FileOutputChannel;
  31. use WindowsAzure\ServiceRuntime\Internal\Protocol1RuntimeClient;
  32. use WindowsAzure\ServiceRuntime\Internal\Protocol1RuntimeCurrentStateClient;
  33. use WindowsAzure\ServiceRuntime\Internal\Protocol1RuntimeGoalStateClient;
  34. use WindowsAzure\ServiceRuntime\Internal\XmlCurrentStateSerializer;
  35. use WindowsAzure\ServiceRuntime\Internal\XmlRoleEnvironmentDataDeserializer;
  36. /**
  37. * Unit tests for class Protocol1RuntimeClient.
  38. *
  39. * @category Microsoft
  40. * @package Tests\Unit\WindowsAzure\ServiceRuntime\Internal
  41. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  42. * @copyright 2012 Microsoft Corporation
  43. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  44. * @version Release: @package_version@
  45. * @link https://github.com/windowsazure/azure-sdk-for-php
  46. */
  47. class Protocol1RuntimeClientTest extends \PHPUnit_Framework_TestCase
  48. {
  49. /**
  50. * @covers WindowsAzure\ServiceRuntime\Internal\Protocol1RuntimeClient::__construct
  51. */
  52. public function testConstruct()
  53. {
  54. // Setup
  55. $protocol1RuntimeCurrentStateClient =
  56. new Protocol1RuntimeCurrentStateClient(null, null);
  57. $protocol1RuntimeGoalStateClient =
  58. new Protocol1RuntimeGoalStateClient(null, null, null, null);
  59. $endpoint = 'endpoint';
  60. $protocol1RuntimeClient = new Protocol1RuntimeClient(
  61. $protocol1RuntimeGoalStateClient,
  62. $protocol1RuntimeCurrentStateClient,
  63. $endpoint);
  64. // Test
  65. $this->assertInstanceOf('WindowsAzure\ServiceRuntime\Internal\Protocol1RuntimeClient',
  66. $protocol1RuntimeClient);
  67. }
  68. /**
  69. * @covers WindowsAzure\ServiceRuntime\Internal\Protocol1RuntimeClient::getCurrentGoalState
  70. */
  71. public function testGetCurrentGoalState()
  72. {
  73. // Setup
  74. $rootDirectory = 'root';
  75. \vfsStreamWrapper::register();
  76. \vfsStreamWrapper::setRoot(new \vfsStreamDirectory($rootDirectory));
  77. $roleEnvironmentFileName = 'roleEnvironment';
  78. $roleEnvironmentFileContent = '<?xml version="1.0" encoding="utf-8"?>' .
  79. '<RoleEnvironment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' .
  80. 'xmlns:xsd="http://www.w3.org/2001/XMLSchema">' .
  81. '<Deployment id="deploymentId" emulated="false" />' .
  82. '<CurrentInstance id="id3" roleName="roleName" faultDomain="4" updateDomain="5">' .
  83. '</CurrentInstance>' .
  84. '<Roles />' .
  85. '</RoleEnvironment>';
  86. $roleEnvironmentFile = \vfsStream::newFile($roleEnvironmentFileName);
  87. $roleEnvironmentFile->setContent($roleEnvironmentFileContent);
  88. \vfsStreamWrapper::getRoot()->addChild($roleEnvironmentFile);
  89. $goalStateFileName = 'goalstate';
  90. $goalStateFileContent = '<?xml version="1.0" encoding="utf-8"?>' .
  91. '<GoalState xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' .
  92. 'xmlns:xsd="http://www.w3.org/2001/XMLSchema">' .
  93. '<Incarnation>1</Incarnation>' .
  94. '<ExpectedState>Started</ExpectedState>' .
  95. '<RoleEnvironmentPath>' .
  96. \vfsStream::url($rootDirectory . '/' . $roleEnvironmentFileName) .
  97. '</RoleEnvironmentPath>' .
  98. '<CurrentStateEndpoint>none</CurrentStateEndpoint>' .
  99. '<Deadline>9999-12-31T23:59:59.9999999</Deadline>' .
  100. '</GoalState>';
  101. $goalStateFileContent = dechex(strlen($goalStateFileContent)) . "\n" . $goalStateFileContent;
  102. $file = \vfsStream::newFile($goalStateFileName);
  103. $file->setContent($goalStateFileContent);
  104. \vfsStreamWrapper::getRoot()->addChild($file);
  105. $protocol1RuntimeCurrentStateClient =
  106. new Protocol1RuntimeCurrentStateClient(null, null);
  107. $goalStateDeserializer = new ChunkedGoalStateDeserializer();
  108. $roleEnvironmentDeserializer = new XmlRoleEnvironmentDataDeserializer();
  109. $inputChannel = new FileInputChannel();
  110. $protocol1RuntimeGoalStateClient =
  111. new Protocol1RuntimeGoalStateClient(
  112. $protocol1RuntimeCurrentStateClient,
  113. $goalStateDeserializer,
  114. $roleEnvironmentDeserializer,
  115. $inputChannel
  116. );
  117. $endpoint = \vfsStream::url($rootDirectory . '/' . $goalStateFileName);
  118. $protocol1RuntimeClient = new Protocol1RuntimeClient(
  119. $protocol1RuntimeGoalStateClient,
  120. $protocol1RuntimeCurrentStateClient,
  121. $endpoint
  122. );
  123. // Test
  124. $this->assertInstanceOf(
  125. 'WindowsAzure\ServiceRuntime\Internal\GoalState',
  126. $protocol1RuntimeClient->getCurrentGoalState()
  127. );
  128. }
  129. /**
  130. * @covers WindowsAzure\ServiceRuntime\Internal\Protocol1RuntimeClient::getRoleEnvironmentData
  131. */
  132. public function testGetRoleEnvironmentData()
  133. {
  134. // Setup
  135. $rootDirectory = 'root';
  136. \vfsStreamWrapper::register();
  137. \vfsStreamWrapper::setRoot(new \vfsStreamDirectory($rootDirectory));
  138. $roleEnvironmentFileName = 'roleEnvironment';
  139. $roleEnvironmentFileContent = '<?xml version="1.0" encoding="utf-8"?>' .
  140. '<RoleEnvironment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' .
  141. 'xmlns:xsd="http://www.w3.org/2001/XMLSchema">' .
  142. '<Deployment id="deploymentId" emulated="false" />' .
  143. '<CurrentInstance id="id3" roleName="roleName" faultDomain="4" updateDomain="5">' .
  144. '</CurrentInstance>' .
  145. '<Roles />' .
  146. '</RoleEnvironment>';
  147. $roleEnvironmentFile = \vfsStream::newFile($roleEnvironmentFileName);
  148. $roleEnvironmentFile->setContent($roleEnvironmentFileContent);
  149. \vfsStreamWrapper::getRoot()->addChild($roleEnvironmentFile);
  150. $goalStateFileName = 'goalstate';
  151. $goalStateFileContent = '<?xml version="1.0" encoding="utf-8"?>' .
  152. '<GoalState xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' .
  153. 'xmlns:xsd="http://www.w3.org/2001/XMLSchema">' .
  154. '<Incarnation>1</Incarnation>' .
  155. '<ExpectedState>Started</ExpectedState>' .
  156. '<RoleEnvironmentPath>' .
  157. \vfsStream::url($rootDirectory . '/' . $roleEnvironmentFileName) .
  158. '</RoleEnvironmentPath>' .
  159. '<CurrentStateEndpoint>none</CurrentStateEndpoint>' .
  160. '<Deadline>9999-12-31T23:59:59.9999999</Deadline>' .
  161. '</GoalState>';
  162. $goalStateFileContent = dechex(strlen($goalStateFileContent)) . "\n" . $goalStateFileContent;
  163. $file = \vfsStream::newFile($goalStateFileName);
  164. $file->setContent($goalStateFileContent);
  165. \vfsStreamWrapper::getRoot()->addChild($file);
  166. $protocol1RuntimeCurrentStateClient =
  167. new Protocol1RuntimeCurrentStateClient(null, null);
  168. $goalStateDeserializer = new ChunkedGoalStateDeserializer();
  169. $roleEnvironmentDeserializer = new XmlRoleEnvironmentDataDeserializer();
  170. $inputChannel = new FileInputChannel();
  171. $protocol1RuntimeGoalStateClient =
  172. new Protocol1RuntimeGoalStateClient(
  173. $protocol1RuntimeCurrentStateClient,
  174. $goalStateDeserializer,
  175. $roleEnvironmentDeserializer,
  176. $inputChannel
  177. );
  178. $endpoint = \vfsStream::url($rootDirectory . '/' . $goalStateFileName);
  179. $protocol1RuntimeClient = new Protocol1RuntimeClient(
  180. $protocol1RuntimeGoalStateClient,
  181. $protocol1RuntimeCurrentStateClient,
  182. $endpoint
  183. );
  184. // Test
  185. $this->assertInstanceOf(
  186. 'WindowsAzure\ServiceRuntime\Internal\RoleEnvironmentData',
  187. $protocol1RuntimeClient->getRoleEnvironmentData()
  188. );
  189. }
  190. /**
  191. * @covers WindowsAzure\ServiceRuntime\Internal\Protocol1RuntimeClient::setCurrentState
  192. */
  193. public function testSetCurrentState()
  194. {
  195. // Setup
  196. $rootDirectory = 'root';
  197. $fileName = 'test';
  198. $fileContents = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
  199. '<CurrentState>' .
  200. '<StatusLease ClientId="clientId">' .
  201. '<Acquire>' .
  202. '<Incarnation>1</Incarnation>' .
  203. '<Status>Recycle</Status>' .
  204. '<Expiration>2000-01-01T00:00:00.0000000Z</Expiration>' .
  205. '</Acquire>' .
  206. '</StatusLease>' .
  207. '</CurrentState>';
  208. \vfsStreamWrapper::register();
  209. \vfsStreamWrapper::setRoot(new \vfsStreamDirectory($rootDirectory));
  210. $file = \vfsStream::newFile($fileName);
  211. \vfsStreamWrapper::getRoot()->addChild($file);
  212. $serializer = new XmlCurrentStateSerializer();
  213. $fileOutputChannel = new FileOutputChannel();
  214. $protocol1RuntimeCurrentStateClient =
  215. new Protocol1RuntimeCurrentStateClient(
  216. $serializer,
  217. $fileOutputChannel
  218. );
  219. $protocol1RuntimeCurrentStateClient->setEndpoint(
  220. \vfsStream::url($rootDirectory . '/' . $fileName)
  221. );
  222. $goalStateDeserializer = new ChunkedGoalStateDeserializer();
  223. $roleEnvironmentDeserializer = new XmlRoleEnvironmentDataDeserializer();
  224. $inputChannel = new FileInputChannel();
  225. $protocol1RuntimeGoalStateClient =
  226. new Protocol1RuntimeGoalStateClient(
  227. $protocol1RuntimeCurrentStateClient,
  228. $goalStateDeserializer,
  229. $roleEnvironmentDeserializer,
  230. $inputChannel
  231. );
  232. $endpoint = 'endpoint';
  233. $protocol1RuntimeClient = new Protocol1RuntimeClient(
  234. $protocol1RuntimeGoalStateClient,
  235. $protocol1RuntimeCurrentStateClient,
  236. $endpoint
  237. );
  238. // Test
  239. $recycleState = new AcquireCurrentState(
  240. 'clientId',
  241. 1,
  242. CurrentStatus::RECYCLE,
  243. new \DateTime('2000-01-01', new \DateTimeZone('UTC'))
  244. );
  245. $protocol1RuntimeClient->setCurrentState($recycleState);
  246. $fileInputChannel = new FileInputChannel();
  247. $fileInputStream = $fileInputChannel->getInputStream(
  248. \vfsStream::url($rootDirectory . '/' . $fileName)
  249. );
  250. $inputChannelContents = stream_get_contents($fileInputStream);
  251. $this->assertEquals($fileContents, $inputChannelContents);
  252. }
  253. }