ONURESTControllerTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 ONURESTControllerTest
  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 getOLTUri()
  25. {
  26. return '/api/olts.json';
  27. }
  28. /**
  29. * @return string Retorna la uri a consultar.
  30. */
  31. protected function getNAPUri()
  32. {
  33. return '/api/naps.json';
  34. }
  35. /**
  36. * @return string Retorna la uri a consultar.
  37. */
  38. protected function getUriPutDelete()
  39. {
  40. return '/api/onus/';
  41. }
  42. /**
  43. * Contiene los datos que debe retornar el webservice. La key es nombre del webservice y el
  44. * value son los datos a retornar.
  45. */
  46. protected function obtainDataWebService()
  47. {
  48. $datos = array();
  49. $datos['api/clients'] =
  50. json_encode(array(array("name" => "Stock", "id" => 1)));
  51. $datos['api/devices/check'] =
  52. json_encode(array(array('result' => true, 'errors' => null)));
  53. $datos['api/devices'] =
  54. json_encode(array('id' => 1));
  55. $datos["client"] = array(array("id" => 1));
  56. return $datos;
  57. }
  58. /**
  59. * Genera los datos a manipular.
  60. * @param string $key Contiene la key a buscar en los datos.
  61. * @return array|string Retorna el array con los datos o el valor de la key pasada como parametro.
  62. * @throws \Exception Lanza un excepcion en caso de no encontrar la key.
  63. */
  64. protected function obtainData($key = null)
  65. {
  66. $datos = array();
  67. $datos['oltId'] = '';
  68. $datos['modelId'] = '1';
  69. $datos['napId'] = '1';
  70. $datos['nap'] = 1;
  71. $datos['profileId'] = '1';
  72. $datos['mac'] = '00:11:22:33';
  73. $datos['ponSerialNumber'] = 'pon';
  74. $datos['clientId'] = 1;
  75. $datos['tenancyId'] = 1;
  76. $datos['deviceId'] = 1;
  77. if ($key == null) {
  78. return $datos;
  79. } else {
  80. if (isset($datos[$key])) {
  81. return $datos[$key];
  82. } else {
  83. throw new \Exception("No se seteo la key del dato a obtener. key=" . $key);
  84. }
  85. }
  86. }
  87. /**
  88. * Genera los datos a manipular.
  89. * @param string $key Contiene la key a buscar en los datos.
  90. * @return array|string Retorna el array con los datos o el valor de la key pasada como parametro.
  91. * @throws \Exception Lanza un excepcion en caso de no encontrar la key.
  92. */
  93. protected function obtainNAPData($key = null)
  94. {
  95. $datos = array();
  96. $datos['olt'] = 1;
  97. $datos['name'] = 'NAP';
  98. $datos['tenancyId'] = 1;
  99. $datos['capacity'] = 8;
  100. if ($key == null) {
  101. return $datos;
  102. } else {
  103. if (isset($datos[$key])) {
  104. return $datos[$key];
  105. } else {
  106. throw new \Exception("No se seteo la key del dato a obtener. key=" . $key);
  107. }
  108. }
  109. }
  110. /**
  111. * Genera los datos a manipular.
  112. * @param string $key Contiene la key a buscar en los datos.
  113. * @return array|string Retorna el array con los datos o el valor de la key pasada como parametro.
  114. * @throws \Exception Lanza un excepcion en caso de no encontrar la key.
  115. */
  116. protected function obtainOLTData($key = null)
  117. {
  118. $datos = array();
  119. $datos['name'] = 'OLT';
  120. $datos['ip'] = '127.0.0.1';
  121. $datos['timeScan'] = 10;
  122. $datos['timeOltOctets'] = 5;
  123. $datos['timePonStats'] = 5;
  124. $datos['timeOnuStats'] = 5;
  125. $datos['tenancyId'] = 1;
  126. $datos['backups'] = 1;
  127. if ($key == null) {
  128. return $datos;
  129. } else {
  130. if (isset($datos[$key])) {
  131. return $datos[$key];
  132. } else {
  133. throw new \Exception("No se seteo la key del dato a obtener. key=" . $key);
  134. }
  135. }
  136. }
  137. /**
  138. * Realiza una busqueda.
  139. * get_onus -> /api/onus.{_format}
  140. * controller: ClientBundle:ClientREST:cget
  141. * Method: GET
  142. * @param string $uri Contiene la direccion.
  143. * @param array $data Contiene los filtros a utilizar en la busqueda.
  144. * @return null|Response Retorna el response.
  145. */
  146. private function generateGET($uri = null, $data = null)
  147. {
  148. $this->initDefault();
  149. if ($uri == null) {
  150. $uri = $this->getUri();
  151. }
  152. // realizo la consulta
  153. if ($data == null) {
  154. $data = array('mac' => $this->obtainData('mac'), 'tenancyId' => $this->obtainData('tenancyId'));
  155. }
  156. $this->getClient()->request('GET', $uri . $this->generateFilters($data));
  157. // obtengo la respuesta
  158. return $this->getClient()->getResponse();
  159. }
  160. /**
  161. * Sobreescribe el device.device_listener
  162. */
  163. private function setListener()
  164. {
  165. $webservicemock = $this->getContainerObject('webservice');
  166. $listener = $this->getContainerObject('device.device_listener');
  167. $listener->setWebservice($webservicemock);
  168. //$this->setContainerObject('device.device_listener', $listener);
  169. $validator = $this->getContainerObject('device.device_validator');
  170. $validator->setWebservice($webservicemock);
  171. //$this->setContainerObject('device.device_validator', $validator);
  172. }
  173. /**
  174. * Realiza el alta.
  175. * post_onu -> /api/onus.{_format}
  176. * controller: ClientBundle:ClientREST:post
  177. * Method: POST
  178. */
  179. public function testPOST()
  180. {
  181. // inicializo con los datos del webservicemock
  182. $this->initDefault($this->obtainDataWebService());
  183. // seteo los datos del listener
  184. $this->setListener();
  185. // hago la inserccion llamando al servicio por post
  186. $this->getClient()->request('POST', $this->getOLTUri(), $this->obtainOLTData());
  187. $response = $this->getClient()->getResponse();
  188. //var_dump($response);
  189. $this->getClient()->request('POST', $this->getNAPUri(), $this->obtainNAPData());
  190. $response = $this->getClient()->getResponse();
  191. $this->getClient()->request('POST', $this->getUri(), $this->obtainData());
  192. // obtengo la respuesta
  193. $response = $this->getClient()->getResponse();
  194. $this->assertEquals(201, $response->getStatusCode(), "Error en la respuesta http.");
  195. }
  196. /**
  197. * Realiza una busqueda.
  198. * get_onus -> /api/onus.{_format}
  199. * controller: ClientBundle:ClientREST:cget
  200. * Method: GET
  201. */
  202. public function testGET_POST()
  203. {
  204. // obtengo la respuesta
  205. $response = $this->generateGET();
  206. // verifco el resultado
  207. $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
  208. $this->assertJson($response->getContent(), "No se obtuvo un objeto json.");
  209. $this->assertContains($this->obtainData('ponSerialNumber'), strtolower($response->getContent()), "Error al buscar al onu.");
  210. }
  211. /**
  212. * Realiza una modificacion.
  213. * put_onu -> /api/onus.{_format}
  214. * controller: ClientBundle:ClientREST:put
  215. * Method: PUT
  216. */
  217. public function testPUT()
  218. {
  219. // realizo la consulta
  220. $response = $this->generateGET();
  221. // busco el id
  222. $id = $this->getProperty($response, 'id');
  223. // inicializo con los datos del webservicemock
  224. $this->initDefault($this->obtainDataWebService());
  225. // seteo los datos del listener
  226. $this->setListener();
  227. // creo el nuevo set de datos a enviar.
  228. $data = $this->obtainDataChange($this->obtainData(), array('ponSerialNumber' => 'pon_modifi', 'id' => $id));
  229. // hago la modificacion llamando al servicio por put
  230. $this->getClient()->request('PUT', $this->getUriPutDelete() . $id, $data);
  231. // obtengo la respuesta
  232. $response = $this->getClient()->getResponse();
  233. $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
  234. }
  235. /**
  236. * Realiza una busqueda.
  237. * get_onus -> /api/onus.{_format}
  238. * controller: ClientBundle:ClientREST:cget
  239. * Method: GET
  240. */
  241. public function testGET_PUT()
  242. {
  243. $response = $this->generateGET();
  244. // verifco el resultado
  245. $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
  246. $this->assertJson($response->getContent(), "No se obtuvo un objeto json.");
  247. $this->assertContains('pon_modifi', strtolower($response->getContent()), "Error al buscar al onu modificado.");
  248. }
  249. /**
  250. * Aplica una transicion de un workflow a una entidad
  251. * apply_onus -> /api/onu/{id}/apply/{workflow}/{transition}.{_format}
  252. * controller: ClientBundle:ClientREST:cget
  253. * Method: GET
  254. */
  255. public function testAPPLY()
  256. {
  257. $this->initDefault($this->obtainDataWebService());
  258. $response = $this->generateGET();
  259. // verifco el resultado
  260. $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
  261. $this->assertJson($json_orig = $response->getContent(), "No se obtuvo un objeto json.");
  262. $json = json_decode($json_orig, true);
  263. if(!isset($json[0])){
  264. $this->fail($json_orig);
  265. }
  266. $json = $json[0];
  267. $this->assertEquals("active", $json["administrativeState"]);
  268. $this->initDefault($this->obtainDataWebService());
  269. $original = $this->getClient()->getContainer()->get('device.device_listener');
  270. $fakeWebService = $this->getClient()->getContainer()->get('webservice');
  271. $original->setWebservice($fakeWebService);
  272. $this->getClient()->request('GET',
  273. $this->getUriPutDelete(). $json["id"] . "/apply/administrative_state/active_to_suspend.json" , array());
  274. $response = $this->getClient()->getResponse();
  275. $json = json_decode($response->getContent(), true);
  276. $this->assertEquals(201, $response->getStatusCode(), "Error en la respuesta http.");
  277. $this->assertEquals("suspend", $json["administrativeState"]);
  278. $response = $this->getClient()->getResponse();
  279. }
  280. /**
  281. * Realiza una baja.
  282. * delete_onu -> /api/onus.{_format}
  283. * controller: ClientBundle:ClientREST:delete
  284. * Method: DELETE
  285. */
  286. public function testDELETE()
  287. {
  288. // realizo la consulta
  289. $response = $this->generateGET();
  290. // obtengo el id de la respuesta de la busqueda
  291. $id = $this->getProperty($response, 'id');
  292. $this->initDefault();
  293. // realizo la consulta
  294. $data = array('tenancy' => 1);
  295. $this->getClient()->request('DELETE', $this->getUriPutDelete() . $id, $data);
  296. // obtengo la respuesta
  297. $response = $this->getClient()->getResponse();
  298. $this->assertEquals(204, $response->getStatusCode(), "Error en la respuesta http.");
  299. }
  300. /**
  301. * Realiza una busqueda.
  302. * get_onus -> /api/onus.{_format}
  303. * controller: ClientBundle:ClientREST:cget
  304. * Method: GET
  305. */
  306. public function testGET_DELETE()
  307. {
  308. $response = $this->generateGET();
  309. // verifco el resultado
  310. $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
  311. $this->assertJson($response->getContent(), "No se obtuvo un objeto json.");
  312. }
  313. /**
  314. * Realiza el alta.
  315. * post_onu -> /api/onus.{_format}
  316. * controller: ClientBundle:ClientREST:post
  317. * Method: POST
  318. */
  319. public function testMIGRATIONS()
  320. {
  321. // inicializo con los datos del webservicemock
  322. $this->initDefault($this->obtainDataWebService());
  323. // seteo los datos del listener
  324. $this->setListener();
  325. // hago la inserccion llamando al servicio por post
  326. $this->getClient()->request('POST', '/api/onus/onus/migrate.json', $this->obtainData());
  327. // obtengo la respuesta
  328. $response = $this->getClient()->getResponse();
  329. $this->assertEquals(201, $response->getStatusCode(), "Error en la respuesta http.");
  330. }
  331. /**
  332. * Realiza una busqueda.
  333. * get_onus -> /api/onus.{_format}
  334. * controller: ClientBundle:ClientREST:cget
  335. * Method: GET
  336. */
  337. public function testGET_MIGRATIONS()
  338. {
  339. // obtengo la respuesta
  340. $response = $this->generateGET();
  341. // verifco el resultado
  342. $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
  343. $this->assertJson($response->getContent(), "No se obtuvo un objeto json.");
  344. $this->assertContains($this->obtainData('ponSerialNumber'), strtolower($response->getContent()), "Error al buscar al onu.");
  345. }
  346. /**
  347. * Realiza una baja.
  348. * delete_onu -> /api/onus.{_format}
  349. * controller: ClientBundle:ClientREST:delete
  350. * Method: DELETE
  351. */
  352. public function testDELETE_MIGRATIONS()
  353. {
  354. // realizo la consulta
  355. $response = $this->generateGET();
  356. // obtengo el id de la respuesta de la busqueda
  357. $id = $this->getProperty($response, 'id');
  358. $this->initDefault();
  359. // realizo la consulta
  360. $data = array('tenancy' => 1);
  361. $this->getClient()->request('DELETE', $this->getUriPutDelete() . $id, $data);
  362. // obtengo la respuesta
  363. $response = $this->getClient()->getResponse();
  364. $this->assertEquals(204, $response->getStatusCode(), "Error en la respuesta http.");
  365. }
  366. /**
  367. * Realiza una busqueda.
  368. * get_onus -> /api/onus.{_format}
  369. * controller: ClientBundle:ClientREST:cget
  370. * Method: GET
  371. */
  372. public function testGET_DELETE_MIGRATIONS()
  373. {
  374. $response = $this->generateGET();
  375. // verifco el resultado
  376. $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
  377. $this->assertJson($response->getContent(), "No se obtuvo un objeto json.");
  378. }
  379. }