CMTSRESTControllerTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace CablemodemBundle\tests;
  3. use WebserviceBundle\tests\WebTestCaseBase;
  4. /**
  5. * Class CMTSRESTControllerTest
  6. * @package CablemodemBundle\tests
  7. * En caso de modificar las tablas solo se deberia modificar la funcion "obtainData" agregando los campos necesarios.
  8. */
  9. class CMTSRESTControllerTest extends WebTestCaseBase
  10. {
  11. /**
  12. * @return string Retorna la uri a consultar.
  13. */
  14. protected function getUri()
  15. {
  16. return '/api/cmtss.json';
  17. }
  18. /**
  19. * @return string Retorna la uri a consultar.
  20. */
  21. protected function getPOSTUri()
  22. {
  23. return '/api/cmts.json';
  24. }
  25. /**
  26. * @return string Retorna la uri a consultar.
  27. */
  28. protected function getUriPutDelete()
  29. {
  30. return '/api/cmtss/';
  31. }
  32. /**
  33. * Contiene los datos que debe retornar el webservice. La key es nombre del webservice y el
  34. * value son los datos a retornar.
  35. */
  36. protected function obtainDataWebService()
  37. {
  38. $datos = [];
  39. return $datos;
  40. }
  41. /**
  42. * Genera los datos a manipular.
  43. *
  44. * @param string $key Contiene la key a buscar en los datos.
  45. *
  46. * @return array|string Retorna el array con los datos o el valor de la key pasada como parametro.
  47. *
  48. * @throws \Exception Lanza un excepcion en caso de no encontrar la key.
  49. */
  50. protected function obtainData($key = null)
  51. {
  52. $datos = [
  53. 'name' => 'Test',
  54. 'host' => '127.0.0.1',
  55. 'snmpComunity' => 'test',
  56. 'snmpVersion' => 1,
  57. 'executeSnmp' => true,
  58. 'extraData' => '',
  59. 'tenancyId' => '1',
  60. 'modelId' => '1',
  61. ];
  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_cmtss -> /api/cmtss.{_format}
  75. * controller: CablemodemBundle:CMTSREST:cget
  76. * Method: GET
  77. *
  78. * @param string $uri Contiene la direccion.
  79. * @param array $data Contiene los filtros a utilizar en la busqueda.
  80. *
  81. * @return null|Response Retorna el response.
  82. */
  83. private function generateGET($uri = null, $data = null)
  84. {
  85. $this->initDefault();
  86. if ($uri == null) {
  87. $uri = $this->getUri();
  88. }
  89. if ($data == null) {
  90. $data = [
  91. 'name' => $this->obtainData('name'),
  92. 'tenancyId' => $this->obtainData('tenancyId'),
  93. ];
  94. }
  95. $this->getClient()->request('GET', $uri . $this->generateFilters($data));
  96. return $this->getClient()->getResponse();
  97. }
  98. /**
  99. * Sobreescribe el device.device_listener
  100. */
  101. private function setListener()
  102. {
  103. $webservicemock = $this->getContainerObject('webservice');
  104. $listener = $this->getContainerObject('device.device_listener');
  105. $listener->setWebservice($webservicemock);
  106. $validator = $this->getContainerObject('device.device_validator');
  107. $validator->setWebservice($webservicemock);
  108. }
  109. /**
  110. * Realiza el alta.
  111. * post_cmtss -> /api/cmtss.{_format}
  112. * controller: CablemodemBundle:CMTSREST:post
  113. * Method: POST
  114. */
  115. public function testPOST()
  116. {
  117. // inicializo con los datos del webservicemock
  118. $this->initDefault($this->obtainDataWebService());
  119. // seteo los datos del listener
  120. $this->setListener();
  121. // hago la inserccion llamando al servicio por post
  122. $this->getClient()->request('POST', $this->getPOSTUri(), $this->obtainData());
  123. // obtengo la respuesta
  124. $response = $this->getClient()->getResponse();
  125. $this->assertEquals(201, $response->getStatusCode(), "Error en la respuesta http.");
  126. }
  127. /**
  128. * Realiza una busqueda.
  129. * get_cmtss -> /api/cmtss.{_format}
  130. * controller: CablemodemBundle:CMTSREST:cget
  131. * Method: GET
  132. */
  133. public function testGET_POST()
  134. {
  135. // obtengo la respuesta
  136. $response = $this->generateGET();
  137. // verifco el resultado
  138. $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
  139. $this->assertJson($response->getContent(), "No se obtuvo un objeto json.");
  140. $this->assertContains($this->obtainData('name'), $response->getContent(), "Error al buscar CMTS.");
  141. }
  142. /**
  143. * Realiza una modificacion.
  144. * put_cmtss -> /api/cmtss.{_format}
  145. * controller: CablemodemBundle:CMTSREST:put
  146. * Method: PUT
  147. */
  148. public function testPUT()
  149. {
  150. // realizo la consulta
  151. $response = $this->generateGET();
  152. // busco el id
  153. $id = $this->getProperty($response, 'id');
  154. // inicializo con los datos del webservicemock
  155. $this->initDefault($this->obtainDataWebService());
  156. // seteo los datos del listener
  157. $this->setListener();
  158. // creo el nuevo set de datos a enviar.
  159. $data = $this->obtainDataChange($this->obtainData(), [
  160. 'name' => 'Test test',
  161. 'id' => $id,
  162. ]);
  163. // hago la modificacion llamando al servicio por put
  164. $this->getClient()->request('PUT', $this->getUriPutDelete() . $id, $data);
  165. // obtengo la respuesta
  166. $response = $this->getClient()->getResponse();
  167. $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
  168. }
  169. /**
  170. * Realiza una busqueda.
  171. * get_cmtss -> /api/cmtss.{_format}
  172. * controller: CablemodemBundle:CMTSREST:cget
  173. * Method: GET
  174. */
  175. public function testGET_PUT()
  176. {
  177. $response = $this->generateGET();
  178. // verifco el resultado
  179. $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
  180. $this->assertJson($response->getContent(), "No se obtuvo un objeto json.");
  181. $this->assertContains('Test test', $response->getContent(), "Error al buscar CMTS modificado.");
  182. }
  183. /**
  184. * Realiza una baja.
  185. * delete_cmtss -> /api/cmtss.{_format}
  186. * controller: CablemodemBundle:CMTSREST:delete
  187. * Method: DELETE
  188. */
  189. public function testDELETE()
  190. {
  191. // realizo la consulta
  192. $response = $this->generateGET();
  193. // obtengo el id de la respuesta de la busqueda
  194. $id = $this->getProperty($response, 'id');
  195. $this->initDefault();
  196. // realizo la consulta
  197. $data = [
  198. 'tenancy' => 1,
  199. ];
  200. $this->getClient()->request('DELETE', $this->getUriPutDelete() . $id, $data);
  201. // obtengo la respuesta
  202. $response = $this->getClient()->getResponse();
  203. $this->assertEquals(204, $response->getStatusCode(), "Error en la respuesta http.");
  204. }
  205. /**
  206. * Realiza una busqueda.
  207. * get_cmtss -> /api/cmtss.{_format}
  208. * controller: CablemodemBundle:CMTSREST:cget
  209. * Method: GET
  210. */
  211. public function testGET_DELETE()
  212. {
  213. $response = $this->generateGET();
  214. // verifco el resultado
  215. $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
  216. $this->assertJson($response->getContent(), "No se obtuvo un objeto json.");
  217. }
  218. }