ONURESTControllerTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. * Genera los datos a manipular.
  30. * @param string $key Contiene la key a buscar en los datos.
  31. * @return array|string Retorna el array con los datos o el valor de la key pasada como parametro.
  32. * @throws \Exception Lanza un excepcion en caso de no encontrar la key.
  33. */
  34. protected function obtainData($key = null)
  35. {
  36. $datos = array();
  37. $datos['oltId'] = '';
  38. $datos['modelId'] = '1';
  39. $datos['napId'] = '1';
  40. $datos['profileId'] = '1';
  41. $datos['mac'] = '00:11:22:33';
  42. $datos['ponSerialNumber'] = 'pon';
  43. $datos['clientId'] = '1';
  44. $datos['transitionState'] = 'ts';
  45. $datos['tenancyId'] = 1;
  46. if ($key == null) {
  47. return $datos;
  48. } else {
  49. if (isset($datos[$key])) {
  50. return $datos[$key];
  51. } else {
  52. throw new \Exception("No se seteo la key del dato a obtener. key=" . $key);
  53. }
  54. }
  55. }
  56. /**
  57. * Realiza una busqueda.
  58. * get_onus -> /api/onus.{_format}
  59. * controller: ClientBundle:ClientREST:cget
  60. * Method: GET
  61. * @param string $uri Contiene la direccion.
  62. * @param array $data Contiene los filtros a utilizar en la busqueda.
  63. * @return null|Response Retorna el response.
  64. */
  65. private function generateGET($uri = null, $data = null)
  66. {
  67. $this->initDefault();
  68. if ($uri == null) {
  69. $uri = $this->getUri();
  70. }
  71. // realizo la consulta
  72. if ($data == null) {
  73. $data = array('mac' => $this->obtainData('mac'), 'tenancyId' => $this->obtainData('tenancyId'));
  74. }
  75. $this->getClient()->request('GET', $uri . $this->generateFilters($data));
  76. // obtengo la respuesta
  77. return $this->getClient()->getResponse();
  78. }
  79. /**
  80. * Realiza el alta.
  81. * post_onu -> /api/onus.{_format}
  82. * controller: ClientBundle:ClientREST:post
  83. * Method: POST
  84. */
  85. public function testPOST()
  86. {
  87. echo "\n";
  88. $this->initDefault();
  89. $this->getClient()->request('POST', $this->getUri(), $this->obtainData());
  90. // obtengo la respuesta
  91. $response = $this->getClient()->getResponse();
  92. $this->assertEquals(201, $response->getStatusCode(), "Error en la respuesta http.");
  93. }
  94. /**
  95. * Realiza una busqueda.
  96. * get_onus -> /api/onus.{_format}
  97. * controller: ClientBundle:ClientREST:cget
  98. * Method: GET
  99. */
  100. public function testGET_POST()
  101. {
  102. // obtengo la respuesta
  103. $response = $this->generateGET();
  104. // verifco el resultado
  105. $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
  106. $this->assertJson($response->getContent(), "No se obtuvo un objeto json.");
  107. $this->assertContains($this->obtainData('ponSerialNumber'), strtolower($response->getContent()), "Error al buscar al onu.");
  108. }
  109. /**
  110. * Realiza una modificacion.
  111. * put_onu -> /api/onus.{_format}
  112. * controller: ClientBundle:ClientREST:put
  113. * Method: PUT
  114. */
  115. public function testPUT()
  116. {
  117. $response = $this->generateGET();
  118. $id = $this->getProperty($response, 'id');
  119. $this->initDefault();
  120. // realizo la consulta
  121. $data = $this->obtainDataChange($this->obtainData(), array('ponSerialNumber' => 'pon_modifi', 'id' => $id));
  122. $this->getClient()->request('PUT', $this->getUriPutDelete() . $id, $data);
  123. // obtengo la respuesta
  124. $response = $this->getClient()->getResponse();
  125. $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
  126. }
  127. /**
  128. * Realiza una busqueda.
  129. * get_onus -> /api/onus.{_format}
  130. * controller: ClientBundle:ClientREST:cget
  131. * Method: GET
  132. */
  133. public function testGET_PUT()
  134. {
  135. $response = $this->generateGET();
  136. // verifco el resultado
  137. $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
  138. $this->assertJson($response->getContent(), "No se obtuvo un objeto json.");
  139. $this->assertContains('pon_modifi', strtolower($response->getContent()), "Error al buscar al onu modificado.");
  140. }
  141. /**
  142. * Realiza una baja.
  143. * delete_onu -> /api/onus.{_format}
  144. * controller: ClientBundle:ClientREST:delete
  145. * Method: DELETE
  146. */
  147. public function testDELETE()
  148. {
  149. // busco el id de
  150. $response = $this->generateGET();
  151. // obtengo el id de la respuesta de la busqueda
  152. $id = $this->getProperty($response, 'id');
  153. $this->initDefault();
  154. // realizo la consulta
  155. $data = array('tenancy' => 1);
  156. $this->getClient()->request('DELETE', $this->getUriPutDelete() . $id, $data);
  157. // obtengo la respuesta
  158. $response = $this->getClient()->getResponse();
  159. $this->assertEquals(204, $response->getStatusCode(), "Error en la respuesta http.");
  160. }
  161. /**
  162. * Realiza una busqueda.
  163. * get_onus -> /api/onus.{_format}
  164. * controller: ClientBundle:ClientREST:cget
  165. * Method: GET
  166. */
  167. public function testGET_DELETE()
  168. {
  169. $response = $this->generateGET();
  170. // verifco el resultado
  171. $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
  172. $json = json_decode($response->getContent());
  173. $this->assertTrue(empty($json), "No se obtuvo un objeto json.");
  174. }
  175. }