123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418 |
- <?php
- namespace FTTHBundle\tests;
- use WebserviceBundle\tests\WebTestCaseBase;
- use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Bundle\FrameworkBundle\Client;
- /**
- * Class ONURESTControllerTest
- * @package FTTHBundle\tests
- * En caso de modificar las tablas solo se deberia modificar la funcion "obtainData" agregando los campos necesarios.
- */
- class ONURESTControllerTest extends WebTestCaseBase
- {
- /**
- * @return string Retorna la uri a consultar.
- */
- protected function getUri()
- {
- return '/api/onus.json';
- }
- /**
- * @return string Retorna la uri a consultar.
- */
- protected function getOLTUri()
- {
- return '/api/olts.json';
- }
- /**
- * @return string Retorna la uri a consultar.
- */
- protected function getNAPUri()
- {
- return '/api/naps.json';
- }
- /**
- * @return string Retorna la uri a consultar.
- */
- protected function getUriPutDelete()
- {
- return '/api/onus/';
- }
- /**
- * Contiene los datos que debe retornar el webservice. La key es nombre del webservice y el
- * value son los datos a retornar.
- */
- protected function obtainDataWebService()
- {
- $datos = array();
- $datos['api/clients'] =
- json_encode(array(array("name" => "Stock", "id" => 1)));
- $datos['api/devices/check'] =
- json_encode(array(array('result' => true, 'errors' => null)));
- $datos['api/devices'] =
- json_encode(array('id' => 1));
- $datos["client"] = array(array("id" => 1));
- return $datos;
- }
- /**
- * Genera los datos a manipular.
- * @param string $key Contiene la key a buscar en los datos.
- * @return array|string Retorna el array con los datos o el valor de la key pasada como parametro.
- * @throws \Exception Lanza un excepcion en caso de no encontrar la key.
- */
- protected function obtainData($key = null)
- {
- $datos = array();
- $datos['oltId'] = '';
- $datos['modelId'] = '1';
- $datos['napId'] = '1';
- $datos['nap'] = 1;
- $datos['profileId'] = '1';
- $datos['mac'] = '00:11:22:33';
- $datos['ponSerialNumber'] = 'pon';
- $datos['clientId'] = 1;
- $datos['tenancyId'] = 1;
- $datos['deviceId'] = 1;
- if ($key == null) {
- return $datos;
- } else {
- if (isset($datos[$key])) {
- return $datos[$key];
- } else {
- throw new \Exception("No se seteo la key del dato a obtener. key=" . $key);
- }
- }
- }
- /**
- * Genera los datos a manipular.
- * @param string $key Contiene la key a buscar en los datos.
- * @return array|string Retorna el array con los datos o el valor de la key pasada como parametro.
- * @throws \Exception Lanza un excepcion en caso de no encontrar la key.
- */
- protected function obtainNAPData($key = null)
- {
- $datos = array();
- $datos['olt'] = 1;
- $datos['name'] = 'NAP';
- $datos['tenancyId'] = 1;
- $datos['capacity'] = 8;
- if ($key == null) {
- return $datos;
- } else {
- if (isset($datos[$key])) {
- return $datos[$key];
- } else {
- throw new \Exception("No se seteo la key del dato a obtener. key=" . $key);
- }
- }
- }
- /**
- * Genera los datos a manipular.
- * @param string $key Contiene la key a buscar en los datos.
- * @return array|string Retorna el array con los datos o el valor de la key pasada como parametro.
- * @throws \Exception Lanza un excepcion en caso de no encontrar la key.
- */
- protected function obtainOLTData($key = null)
- {
- $datos = array();
- $datos['name'] = 'OLT';
- $datos['ip'] = '127.0.0.1';
- $datos['timeScan'] = 10;
- $datos['timeOltOctets'] = 5;
- $datos['timePonStats'] = 5;
- $datos['timeOnuStats'] = 5;
- $datos['tenancyId'] = 1;
- $datos['backups'] = 1;
- if ($key == null) {
- return $datos;
- } else {
- if (isset($datos[$key])) {
- return $datos[$key];
- } else {
- throw new \Exception("No se seteo la key del dato a obtener. key=" . $key);
- }
- }
- }
- /**
- * Realiza una busqueda.
- * get_onus -> /api/onus.{_format}
- * controller: ClientBundle:ClientREST:cget
- * Method: GET
- * @param string $uri Contiene la direccion.
- * @param array $data Contiene los filtros a utilizar en la busqueda.
- * @return null|Response Retorna el response.
- */
- private function generateGET($uri = null, $data = null)
- {
- $this->initDefault();
- if ($uri == null) {
- $uri = $this->getUri();
- }
- // realizo la consulta
- if ($data == null) {
- $data = array('mac' => $this->obtainData('mac'), 'tenancyId' => $this->obtainData('tenancyId'));
- }
- $this->getClient()->request('GET', $uri . $this->generateFilters($data));
- // obtengo la respuesta
- return $this->getClient()->getResponse();
- }
- /**
- * Sobreescribe el device.device_listener
- */
- private function setListener()
- {
- $webservicemock = $this->getContainerObject('webservice');
- $listener = $this->getContainerObject('device.device_listener');
- $listener->setWebservice($webservicemock);
- //$this->setContainerObject('device.device_listener', $listener);
- $validator = $this->getContainerObject('device.device_validator');
- $validator->setWebservice($webservicemock);
- //$this->setContainerObject('device.device_validator', $validator);
- }
- /**
- * Realiza el alta.
- * post_onu -> /api/onus.{_format}
- * controller: ClientBundle:ClientREST:post
- * Method: POST
- */
- public function testPOST()
- {
- // inicializo con los datos del webservicemock
- $this->initDefault($this->obtainDataWebService());
- // seteo los datos del listener
- $this->setListener();
- // hago la inserccion llamando al servicio por post
- $this->getClient()->request('POST', $this->getOLTUri(), $this->obtainOLTData());
- $response = $this->getClient()->getResponse();
- //var_dump($response);
- $this->getClient()->request('POST', $this->getNAPUri(), $this->obtainNAPData());
- $response = $this->getClient()->getResponse();
- $this->getClient()->request('POST', $this->getUri(), $this->obtainData());
- // obtengo la respuesta
- $response = $this->getClient()->getResponse();
- $this->assertEquals(201, $response->getStatusCode(), "Error en la respuesta http.");
- }
- /**
- * Realiza una busqueda.
- * get_onus -> /api/onus.{_format}
- * controller: ClientBundle:ClientREST:cget
- * Method: GET
- */
- public function testGET_POST()
- {
- // obtengo la respuesta
- $response = $this->generateGET();
- // verifco el resultado
- $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
- $this->assertJson($response->getContent(), "No se obtuvo un objeto json.");
- $this->assertContains($this->obtainData('ponSerialNumber'), strtolower($response->getContent()), "Error al buscar al onu.");
- }
- /**
- * Realiza una modificacion.
- * put_onu -> /api/onus.{_format}
- * controller: ClientBundle:ClientREST:put
- * Method: PUT
- */
- public function testPUT()
- {
- // realizo la consulta
- $response = $this->generateGET();
- // busco el id
- $id = $this->getProperty($response, 'id');
- // inicializo con los datos del webservicemock
- $this->initDefault($this->obtainDataWebService());
- // seteo los datos del listener
- $this->setListener();
- // creo el nuevo set de datos a enviar.
- $data = $this->obtainDataChange($this->obtainData(), array('ponSerialNumber' => 'pon_modifi', 'id' => $id));
- // hago la modificacion llamando al servicio por put
- $this->getClient()->request('PUT', $this->getUriPutDelete() . $id, $data);
- // obtengo la respuesta
- $response = $this->getClient()->getResponse();
- $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
- }
- /**
- * Realiza una busqueda.
- * get_onus -> /api/onus.{_format}
- * controller: ClientBundle:ClientREST:cget
- * Method: GET
- */
- public function testGET_PUT()
- {
- $response = $this->generateGET();
- // verifco el resultado
- $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
- $this->assertJson($response->getContent(), "No se obtuvo un objeto json.");
- $this->assertContains('pon_modifi', strtolower($response->getContent()), "Error al buscar al onu modificado.");
- }
- /**
- * Aplica una transicion de un workflow a una entidad
- * apply_onus -> /api/onu/{id}/apply/{workflow}/{transition}.{_format}
- * controller: ClientBundle:ClientREST:cget
- * Method: GET
- */
- public function testAPPLY()
- {
- $this->initDefault($this->obtainDataWebService());
- $response = $this->generateGET();
- // verifco el resultado
- $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
- $this->assertJson($json_orig = $response->getContent(), "No se obtuvo un objeto json.");
- $json = json_decode($json_orig, true);
- if(!isset($json[0])){
- $this->fail($json_orig);
- }
- $json = $json[0];
- $this->assertEquals("active", $json["administrativeState"]);
- $this->initDefault($this->obtainDataWebService());
- $original = $this->getClient()->getContainer()->get('device.device_listener');
- $fakeWebService = $this->getClient()->getContainer()->get('webservice');
- $original->setWebservice($fakeWebService);
- $this->getClient()->request('GET',
- $this->getUriPutDelete(). $json["id"] . "/apply/administrative_state/active_to_suspend.json" , array());
- $response = $this->getClient()->getResponse();
- $json = json_decode($response->getContent(), true);
- $this->assertEquals(201, $response->getStatusCode(), "Error en la respuesta http.");
- $this->assertEquals("suspend", $json["administrativeState"]);
- $response = $this->getClient()->getResponse();
- }
- /**
- * Realiza una baja.
- * delete_onu -> /api/onus.{_format}
- * controller: ClientBundle:ClientREST:delete
- * Method: DELETE
- */
- public function testDELETE()
- {
- // realizo la consulta
- $response = $this->generateGET();
- // obtengo el id de la respuesta de la busqueda
- $id = $this->getProperty($response, 'id');
- $this->initDefault();
- // realizo la consulta
- $data = array('tenancy' => 1);
- $this->getClient()->request('DELETE', $this->getUriPutDelete() . $id, $data);
- // obtengo la respuesta
- $response = $this->getClient()->getResponse();
- $this->assertEquals(204, $response->getStatusCode(), "Error en la respuesta http.");
- }
- /**
- * Realiza una busqueda.
- * get_onus -> /api/onus.{_format}
- * controller: ClientBundle:ClientREST:cget
- * Method: GET
- */
- public function testGET_DELETE()
- {
- $response = $this->generateGET();
- // verifco el resultado
- $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
- $this->assertJson($response->getContent(), "No se obtuvo un objeto json.");
- }
- /**
- * Realiza el alta.
- * post_onu -> /api/onus.{_format}
- * controller: ClientBundle:ClientREST:post
- * Method: POST
- */
- public function testMIGRATIONS()
- {
- // inicializo con los datos del webservicemock
- $this->initDefault($this->obtainDataWebService());
- // seteo los datos del listener
- $this->setListener();
- // hago la inserccion llamando al servicio por post
- $this->getClient()->request('POST', '/api/onus/onus/migrate.json', $this->obtainData());
- // obtengo la respuesta
- $response = $this->getClient()->getResponse();
- $this->assertEquals(201, $response->getStatusCode(), "Error en la respuesta http.");
- }
- /**
- * Realiza una busqueda.
- * get_onus -> /api/onus.{_format}
- * controller: ClientBundle:ClientREST:cget
- * Method: GET
- */
- public function testGET_MIGRATIONS()
- {
- // obtengo la respuesta
- $response = $this->generateGET();
- // verifco el resultado
- $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
- $this->assertJson($response->getContent(), "No se obtuvo un objeto json.");
- $this->assertContains($this->obtainData('ponSerialNumber'), strtolower($response->getContent()), "Error al buscar al onu.");
- }
- /**
- * Realiza una baja.
- * delete_onu -> /api/onus.{_format}
- * controller: ClientBundle:ClientREST:delete
- * Method: DELETE
- */
- public function testDELETE_MIGRATIONS()
- {
- // realizo la consulta
- $response = $this->generateGET();
- // obtengo el id de la respuesta de la busqueda
- $id = $this->getProperty($response, 'id');
- $this->initDefault();
- // realizo la consulta
- $data = array('tenancy' => 1);
- $this->getClient()->request('DELETE', $this->getUriPutDelete() . $id, $data);
- // obtengo la respuesta
- $response = $this->getClient()->getResponse();
- $this->assertEquals(204, $response->getStatusCode(), "Error en la respuesta http.");
- }
- /**
- * Realiza una busqueda.
- * get_onus -> /api/onus.{_format}
- * controller: ClientBundle:ClientREST:cget
- * Method: GET
- */
- public function testGET_DELETE_MIGRATIONS()
- {
- $response = $this->generateGET();
- // verifco el resultado
- $this->assertEquals(200, $response->getStatusCode(), "Error en la respuesta http.");
- $this->assertJson($response->getContent(), "No se obtuvo un objeto json.");
- }
- }
|