ONURESTControllerTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. namespace FTTHBundle\tests;
  3. use WebserviceBundle\tests\WebTestCaseBase;
  4. use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Bundle\FrameworkBundle\Client;
  7. /**
  8. * Class ClientRESTControllerTest
  9. * @package FTTHBundle\tests
  10. * En caso de modificar las tablas solo se deberia modificar la funcion "obtainData" agregando los campos necesarios.
  11. */
  12. class ONURESTControllerTest extends WebTestCaseBase
  13. {
  14. /**
  15. * @return string Retorna la uri a consultar.
  16. */
  17. protected function getUri()
  18. {
  19. return '/api/onus.json';
  20. }
  21. /**
  22. * @return string Retorna la uri a consultar.
  23. */
  24. protected function getUriPutDelete()
  25. {
  26. return '/api/onus/';
  27. }
  28. /**
  29. * Contiene los datos que debe retornar el webservice. La key es nombre del webservice y el
  30. * value son los datos a retornar.
  31. */
  32. protected function obtainDataWebService()
  33. {
  34. $datos = array();
  35. $datos['api/clients'] =
  36. json_encode(array(array("name" => "Stock", "id" => 1)));
  37. $datos['api/devices/check'] =
  38. json_encode(array(array('result' => true, 'errors' => null)));
  39. $datos['api/devices'] =
  40. json_encode(array(array('name' => true, 'errors' => null)));
  41. return $datos;
  42. }
  43. /**
  44. * Genera los datos a manipular.
  45. * @param string $key Contiene la key a buscar en los datos.
  46. * @return array|string Retorna el array con los datos o el valor de la key pasada como parametro.
  47. * @throws \Exception Lanza un excepcion en caso de no encontrar la key.
  48. */
  49. protected function obtainData($key = null)
  50. {
  51. $datos = array();
  52. $datos['oltId'] = '';
  53. $datos['modelId'] = '1';
  54. $datos['napId'] = '1';
  55. $datos['profileId'] = '1';
  56. $datos['mac'] = '00:11:22:33';
  57. $datos['ponSerialNumber'] = 'pon';
  58. $datos['clientId'] = array('name' => 'Stock GZ [pruebass]');
  59. $datos['transitionState'] = 'ts';
  60. $datos['tenancyId'] = 1;
  61. $datos['deviceId'] = 1;
  62. if ($key == null) {
  63. return $datos;
  64. } else {
  65. if (isset($datos[$key])) {
  66. return $datos[$key];
  67. } else {
  68. throw new \Exception("No se seteo la key del dato a obtener. key=" . $key);
  69. }
  70. }
  71. }
  72. /**
  73. * Realiza una busqueda.
  74. * get_onus -> /api/onus.{_format}
  75. * controller: ClientBundle:ClientREST:cget
  76. * Method: GET
  77. * @param string $uri Contiene la direccion.
  78. * @param array $data Contiene los filtros a utilizar en la busqueda.
  79. * @return null|Response Retorna el response.
  80. */
  81. private function generateGET($uri = null, $data = null)
  82. {
  83. $this->initDefault();
  84. if ($uri == null) {
  85. $uri = $this->getUri();
  86. }
  87. // realizo la consulta
  88. if ($data == null) {
  89. $data = array('mac' => $this->obtainData('mac'), 'tenancyId' => $this->obtainData('tenancyId'));
  90. }
  91. $this->getClient()->request('GET', $uri . $this->generateFilters($data));
  92. // obtengo la respuesta
  93. return $this->getClient()->getResponse();
  94. }
  95. /**
  96. * Realiza el alta.
  97. * post_onu -> /api/onus.{_format}
  98. * controller: ClientBundle:ClientREST:post
  99. * Method: POST
  100. */
  101. public function testPOST()
  102. {
  103. // inicializo con los datos del webservicemock
  104. $this->initDefault($this->obtainDataWebService());
  105. // seteo los datos del listener
  106. $this->setListener();
  107. // hago la inserccion llamando al servicio por post
  108. $this->getClient()->request('POST', $this->getUri(), $this->obtainData());
  109. // obtengo la respuesta
  110. $response = $this->getClient()->getResponse();
  111. $this->assertEquals(201, $response->getStatusCode(), "Error en la respuesta http.");
  112. }
  113. /**
  114. * Realiza una busqueda.
  115. * get_onus -> /api/onus.{_format}
  116. * controller: ClientBundle:ClientREST:cget
  117. * Method: GET
  118. */
  119. public function testGET_POST()
  120. {
  121. // obtengo la respuesta
  122. $response = $this->generateGET();
  123. // verifco el resultado
  124. $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
  125. $this->assertJson($response->getContent(), "No se obtuvo un objeto json.");
  126. $this->assertContains($this->obtainData('ponSerialNumber'), strtolower($response->getContent()), "Error al buscar al onu.");
  127. }
  128. /**
  129. * Realiza una modificacion.
  130. * put_onu -> /api/onus.{_format}
  131. * controller: ClientBundle:ClientREST:put
  132. * Method: PUT
  133. */
  134. public function testPUT()
  135. {
  136. // realizo la consulta
  137. $response = $this->generateGET();
  138. // busco el id
  139. $id = $this->getProperty($response, 'id');
  140. // inicializo con los datos del webservicemock
  141. $this->initDefault($this->obtainDataWebService());
  142. // seteo los datos del listener
  143. $this->setListener();
  144. // creo el nuevo set de datos a enviar.
  145. $data = $this->obtainDataChange($this->obtainData(), array('ponSerialNumber' => 'pon_modifi', 'id' => $id));
  146. // hago la modificacion llamando al servicio por put
  147. $this->getClient()->request('PUT', $this->getUriPutDelete() . $id, $data);
  148. // obtengo la respuesta
  149. $response = $this->getClient()->getResponse();
  150. $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
  151. }
  152. /**
  153. * Realiza una busqueda.
  154. * get_onus -> /api/onus.{_format}
  155. * controller: ClientBundle:ClientREST:cget
  156. * Method: GET
  157. */
  158. public function testGET_PUT()
  159. {
  160. $response = $this->generateGET();
  161. // verifco el resultado
  162. $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
  163. $this->assertJson($response->getContent(), "No se obtuvo un objeto json.");
  164. $this->assertContains('pon_modifi', strtolower($response->getContent()), "Error al buscar al onu modificado.");
  165. }
  166. /**
  167. * Aplica una transicion de un workflow a una entidad
  168. * apply_onus -> /api/onu/apply/{id}/{workflow}/{transition}.{_format}
  169. * controller: ClientBundle:ClientREST:cget
  170. * Method: GET
  171. */
  172. public function testAPPLY()
  173. {
  174. $this->initDefault($this->obtainDataWebService());
  175. $response = $this->generateGET();
  176. // verifco el resultado
  177. $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
  178. $this->assertJson($json = $response->getContent(), "No se obtuvo un objeto json.");
  179. $json = json_decode($json, true);
  180. $json = $json[0];
  181. $this->assertEquals("active", $json["administrativeState"]);
  182. $this->initDefault($this->obtainDataWebService());
  183. $this->getClient()->request('PATCH',
  184. $this->getUriPutDelete(). "apply/". $json["id"] . "/administrative_state/active_to_suspend.json" , array());
  185. $response = $this->getClient()->getResponse();
  186. $json = json_decode($response->getContent(), true);
  187. $this->assertEquals(201, $response->getStatusCode(), "Error en la respuesta http.");
  188. $this->assertEquals("suspend", $json["administrativeState"]);
  189. $response = $this->getClient()->getResponse();
  190. var_dump( $response->getStatusCode());
  191. }
  192. /**
  193. * Realiza una baja.
  194. * delete_onu -> /api/onus.{_format}
  195. * controller: ClientBundle:ClientREST:delete
  196. * Method: DELETE
  197. */
  198. public function testDELETE()
  199. {
  200. // realizo la consulta
  201. $response = $this->generateGET();
  202. // obtengo el id de la respuesta de la busqueda
  203. $id = $this->getProperty($response, 'id');
  204. $this->initDefault();
  205. // realizo la consulta
  206. $data = array('tenancy' => 1);
  207. $this->getClient()->request('DELETE', $this->getUriPutDelete() . $id, $data);
  208. // obtengo la respuesta
  209. $response = $this->getClient()->getResponse();
  210. $this->assertEquals(204, $response->getStatusCode(), "Error en la respuesta http.");
  211. }
  212. /**
  213. * Realiza una busqueda.
  214. * get_onus -> /api/onus.{_format}
  215. * controller: ClientBundle:ClientREST:cget
  216. * Method: GET
  217. */
  218. public function testGET_DELETE()
  219. {
  220. $response = $this->generateGET();
  221. // verifco el resultado
  222. $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
  223. $this->assertJson($response->getContent(), "No se obtuvo un objeto json.");
  224. }
  225. /**
  226. * Sobreescribe el device.device_listener
  227. */
  228. private function setListener()
  229. {
  230. $webservicemock = $this->getContainerObject('webService');
  231. $listener = $this->getContainerObject('device.device_listener');
  232. $listener->setWebservice($webservicemock);
  233. $this->setContainerObject('device.device_listener', $listener);
  234. $validator = $this->getContainerObject('device.device_validator');
  235. $validator->setWebservice($webservicemock);
  236. $this->setContainerObject('device.device_validator', $validator);
  237. }
  238. }