PageRenderTime 69ms CodeModel.GetById 55ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

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

https://bitbucket.org/skudatech/azure-sdk-for-php
PHP | 304 lines | 201 code | 51 blank | 52 comment | 0 complexity | 72ec57a548229b981ff56a5cc133d5cc MD5 | raw file
  1<?php
  2
  3/**
  4 * LICENSE: Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 * http://www.apache.org/licenses/LICENSE-2.0
  8 *
  9 * Unless required by applicable law or agreed to in writing, software
 10 * distributed under the License is distributed on an "AS IS" BASIS,
 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12 * See the License for the specific language governing permissions and
 13 * limitations under the License.
 14 * 
 15 * PHP version 5
 16 *
 17 * @category  Microsoft
 18 * @package   Tests\Unit\WindowsAzure\ServiceRuntime\Internal
 19 * @author    Azure PHP SDK <azurephpsdk@microsoft.com>
 20 * @copyright 2012 Microsoft Corporation
 21 * @license   http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
 22 * @link      https://github.com/windowsazure/azure-sdk-for-php
 23 */
 24namespace Tests\Unit\WindowsAzure\ServiceRuntime\Internal;
 25
 26use Tests\Framework\TestResources;
 27use WindowsAzure\Common\Internal\Utilities;
 28use WindowsAzure\ServiceRuntime\Internal\AcquireCurrentState;
 29use WindowsAzure\ServiceRuntime\Internal\ChunkedGoalStateDeserializer;
 30use WindowsAzure\ServiceRuntime\Internal\CurrentStatus;
 31use WindowsAzure\ServiceRuntime\Internal\FileInputChannel;
 32use WindowsAzure\ServiceRuntime\Internal\FileOutputChannel;
 33use WindowsAzure\ServiceRuntime\Internal\Protocol1RuntimeClient;
 34use WindowsAzure\ServiceRuntime\Internal\Protocol1RuntimeCurrentStateClient;
 35use WindowsAzure\ServiceRuntime\Internal\Protocol1RuntimeGoalStateClient;
 36use WindowsAzure\ServiceRuntime\Internal\XmlCurrentStateSerializer;
 37use WindowsAzure\ServiceRuntime\Internal\XmlRoleEnvironmentDataDeserializer;
 38
 39/**
 40 * Unit tests for class Protocol1RuntimeClient.
 41 *
 42 * @category  Microsoft
 43 * @package   Tests\Unit\WindowsAzure\ServiceRuntime\Internal
 44 * @author    Azure PHP SDK <azurephpsdk@microsoft.com>
 45 * @copyright 2012 Microsoft Corporation
 46 * @license   http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
 47 * @version   Release: @package_version@
 48 * @link      https://github.com/windowsazure/azure-sdk-for-php
 49 */
 50class Protocol1RuntimeClientTest extends \PHPUnit_Framework_TestCase
 51{
 52    /**
 53     * @covers WindowsAzure\ServiceRuntime\Internal\Protocol1RuntimeClient::__construct
 54     */
 55    public function testConstruct()
 56    {
 57        // Setup
 58        $protocol1RuntimeCurrentStateClient =
 59            new Protocol1RuntimeCurrentStateClient(null, null);
 60        
 61        $protocol1RuntimeGoalStateClient = 
 62            new Protocol1RuntimeGoalStateClient(null, null, null, null);
 63        
 64        $endpoint = 'endpoint';
 65        
 66        $protocol1RuntimeClient = new Protocol1RuntimeClient(
 67            $protocol1RuntimeGoalStateClient,
 68            $protocol1RuntimeCurrentStateClient,
 69            $endpoint);
 70        
 71        // Test
 72        $this->assertInstanceOf('WindowsAzure\ServiceRuntime\Internal\Protocol1RuntimeClient',
 73            $protocol1RuntimeClient);
 74    }
 75    
 76    /**
 77     * @covers WindowsAzure\ServiceRuntime\Internal\Protocol1RuntimeClient::getCurrentGoalState
 78     */
 79    public function testGetCurrentGoalState()
 80    {
 81        // Setup
 82        $rootDirectory = 'root';
 83
 84        \vfsStreamWrapper::register();
 85        \vfsStreamWrapper::setRoot(new \vfsStreamDirectory($rootDirectory));
 86
 87        $roleEnvironmentFileName = 'roleEnvironment';
 88        $roleEnvironmentFileContent = '<?xml version="1.0" encoding="utf-8"?>' .
 89            '<RoleEnvironment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' .
 90            'xmlns:xsd="http://www.w3.org/2001/XMLSchema">' .
 91            '<Deployment id="deploymentId" emulated="false" />' .
 92            '<CurrentInstance id="id3" roleName="roleName" faultDomain="4" updateDomain="5">' .
 93            '</CurrentInstance>' .
 94            '<Roles />' .
 95            '</RoleEnvironment>';
 96
 97        $roleEnvironmentFile = \vfsStream::newFile($roleEnvironmentFileName);
 98        $roleEnvironmentFile->setContent($roleEnvironmentFileContent); 
 99
100        \vfsStreamWrapper::getRoot()->addChild($roleEnvironmentFile);
101
102        $goalStateFileName = 'goalstate';
103        $goalStateFileContent = '<?xml version="1.0" encoding="utf-8"?>' .
104            '<GoalState xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' .
105            'xmlns:xsd="http://www.w3.org/2001/XMLSchema">' .
106            '<Incarnation>1</Incarnation>' .
107            '<ExpectedState>Started</ExpectedState>' .
108            '<RoleEnvironmentPath>' .
109            \vfsStream::url($rootDirectory . '/' . $roleEnvironmentFileName) .
110            '</RoleEnvironmentPath>' .
111            '<CurrentStateEndpoint>none</CurrentStateEndpoint>' .
112            '<Deadline>9999-12-31T23:59:59.9999999</Deadline>' .
113            '</GoalState>';
114
115        $goalStateFileContent = dechex(strlen($goalStateFileContent)) . "\n" . $goalStateFileContent;
116
117        $file = \vfsStream::newFile($goalStateFileName);
118        $file->setContent($goalStateFileContent);
119
120        \vfsStreamWrapper::getRoot()->addChild($file);
121
122        $protocol1RuntimeCurrentStateClient =
123            new Protocol1RuntimeCurrentStateClient(null, null);
124
125        $goalStateDeserializer       = new ChunkedGoalStateDeserializer();
126        $roleEnvironmentDeserializer = new XmlRoleEnvironmentDataDeserializer();
127        $inputChannel                = new FileInputChannel();
128
129        $protocol1RuntimeGoalStateClient =
130            new Protocol1RuntimeGoalStateClient(
131                $protocol1RuntimeCurrentStateClient,
132                $goalStateDeserializer,
133                $roleEnvironmentDeserializer,
134                $inputChannel
135            );
136
137        $endpoint = \vfsStream::url($rootDirectory . '/' . $goalStateFileName);
138
139        $protocol1RuntimeClient = new Protocol1RuntimeClient(
140            $protocol1RuntimeGoalStateClient,
141            $protocol1RuntimeCurrentStateClient,
142            $endpoint
143        );
144
145        // Test
146        $this->assertInstanceOf(
147            'WindowsAzure\ServiceRuntime\Internal\GoalState',
148            $protocol1RuntimeClient->getCurrentGoalState()
149        );
150    }
151    
152   /**
153     * @covers WindowsAzure\ServiceRuntime\Internal\Protocol1RuntimeClient::getRoleEnvironmentData
154     */
155    public function testGetRoleEnvironmentData()
156    {
157        // Setup
158        $rootDirectory = 'root';
159
160        \vfsStreamWrapper::register();
161        \vfsStreamWrapper::setRoot(new \vfsStreamDirectory($rootDirectory));
162
163        $roleEnvironmentFileName = 'roleEnvironment';
164        $roleEnvironmentFileContent = '<?xml version="1.0" encoding="utf-8"?>' .
165            '<RoleEnvironment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' .
166            'xmlns:xsd="http://www.w3.org/2001/XMLSchema">' .
167            '<Deployment id="deploymentId" emulated="false" />' .
168            '<CurrentInstance id="id3" roleName="roleName" faultDomain="4" updateDomain="5">' .
169            '</CurrentInstance>' .
170            '<Roles />' .
171            '</RoleEnvironment>';
172
173        $roleEnvironmentFile = \vfsStream::newFile($roleEnvironmentFileName);
174        $roleEnvironmentFile->setContent($roleEnvironmentFileContent); 
175
176        \vfsStreamWrapper::getRoot()->addChild($roleEnvironmentFile);
177
178        $goalStateFileName = 'goalstate';
179        $goalStateFileContent = '<?xml version="1.0" encoding="utf-8"?>' .
180            '<GoalState xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' .
181            'xmlns:xsd="http://www.w3.org/2001/XMLSchema">' .
182            '<Incarnation>1</Incarnation>' .
183            '<ExpectedState>Started</ExpectedState>' .
184            '<RoleEnvironmentPath>' .
185            \vfsStream::url($rootDirectory . '/' . $roleEnvironmentFileName) .
186            '</RoleEnvironmentPath>' .
187            '<CurrentStateEndpoint>none</CurrentStateEndpoint>' .
188            '<Deadline>9999-12-31T23:59:59.9999999</Deadline>' .
189            '</GoalState>';
190
191        $goalStateFileContent = dechex(strlen($goalStateFileContent)) . "\n" . $goalStateFileContent;
192
193        $file = \vfsStream::newFile($goalStateFileName);
194        $file->setContent($goalStateFileContent);
195
196        \vfsStreamWrapper::getRoot()->addChild($file);
197
198        $protocol1RuntimeCurrentStateClient =
199            new Protocol1RuntimeCurrentStateClient(null, null);
200
201        $goalStateDeserializer       = new ChunkedGoalStateDeserializer();
202        $roleEnvironmentDeserializer = new XmlRoleEnvironmentDataDeserializer();
203        $inputChannel                = new FileInputChannel();
204
205        $protocol1RuntimeGoalStateClient =
206            new Protocol1RuntimeGoalStateClient(
207                $protocol1RuntimeCurrentStateClient,
208                $goalStateDeserializer,
209                $roleEnvironmentDeserializer,
210                $inputChannel
211            );
212
213        $endpoint = \vfsStream::url($rootDirectory . '/' . $goalStateFileName);
214
215        $protocol1RuntimeClient = new Protocol1RuntimeClient(
216            $protocol1RuntimeGoalStateClient,
217            $protocol1RuntimeCurrentStateClient,
218            $endpoint
219        );
220
221        // Test
222        $this->assertInstanceOf(
223            'WindowsAzure\ServiceRuntime\Internal\RoleEnvironmentData',
224            $protocol1RuntimeClient->getRoleEnvironmentData()
225        );
226    }
227    
228    /**
229     * @covers WindowsAzure\ServiceRuntime\Internal\Protocol1RuntimeClient::setCurrentState
230     */
231    public function testSetCurrentState()
232    {
233        // Setup
234        $rootDirectory = 'root';
235        $fileName = 'test';
236        $fileContents = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
237            '<CurrentState>' .
238            '<StatusLease ClientId="clientId">' .
239            '<Acquire>' .
240            '<Incarnation>1</Incarnation>' .
241            '<Status>Recycle</Status>' .
242            '<Expiration>2000-01-01T00:00:00.0000000Z</Expiration>' .
243            '</Acquire>' .
244            '</StatusLease>' .
245            '</CurrentState>';
246        
247        \vfsStreamWrapper::register(); 
248        \vfsStreamWrapper::setRoot(new \vfsStreamDirectory($rootDirectory));
249        
250        $file = \vfsStream::newFile($fileName);
251        \vfsStreamWrapper::getRoot()->addChild($file);
252        
253        $serializer = new XmlCurrentStateSerializer();
254        $fileOutputChannel = new FileOutputChannel();
255
256        $protocol1RuntimeCurrentStateClient =
257            new Protocol1RuntimeCurrentStateClient(
258                $serializer,
259                $fileOutputChannel
260            );
261
262        $protocol1RuntimeCurrentStateClient->setEndpoint(
263            \vfsStream::url($rootDirectory . '/' . $fileName)
264        );
265        
266        $goalStateDeserializer       = new ChunkedGoalStateDeserializer();
267        $roleEnvironmentDeserializer = new XmlRoleEnvironmentDataDeserializer();
268        $inputChannel                = new FileInputChannel();
269
270        $protocol1RuntimeGoalStateClient =
271            new Protocol1RuntimeGoalStateClient(
272                $protocol1RuntimeCurrentStateClient,
273                $goalStateDeserializer,
274                $roleEnvironmentDeserializer,
275                $inputChannel
276            );
277
278        $endpoint = 'endpoint';
279        $protocol1RuntimeClient = new Protocol1RuntimeClient(
280            $protocol1RuntimeGoalStateClient,
281            $protocol1RuntimeCurrentStateClient,
282            $endpoint
283        );
284        
285        // Test
286        $recycleState = new AcquireCurrentState(
287            'clientId',
288            1,
289            CurrentStatus::RECYCLE,
290            new \DateTime('2000-01-01', new \DateTimeZone('UTC'))
291        );
292        
293        $protocol1RuntimeClient->setCurrentState($recycleState);
294        
295        $fileInputChannel = new FileInputChannel();
296        $fileInputStream = $fileInputChannel->getInputStream(
297            \vfsStream::url($rootDirectory . '/' . $fileName)
298        );
299        
300        $inputChannelContents = stream_get_contents($fileInputStream);
301        $this->assertEquals($fileContents, $inputChannelContents);
302    }
303}
304