/** * Testa recuperação de lista de notificações */ public function testGetNotifications() { // Cria dados para testar $this->static_client->request('POST', '/ws/neo/coleta', array(), array(), array('CONTENT_TYPE' => 'application/json'), $this->coleta_modifications); $response = $this->static_client->getResponse(); $status = $response->getStatusCode(); $this->logger->debug("Response status: {$status}"); //$logger->debug("JSON da coleta: \n".$response->getContent()); $this->assertEquals($status, 200); // Verifica se o Software Coleta foi inserido $em = $this->container->get('doctrine')->getManager(); $software = $em->getRepository("CacicCommonBundle:Software")->getByName('Messaging account plugin for AIM'); $this->assertNotEmpty($software, "Software Messaging account plugin for AIM não encontrado após coleta"); // Só vai criar a notificação se existir um relatório cadastrado $user = $em->getRepository("CacicCommonBundle:Usuario")->find(1); $em->persist($user); $relatorio = new SoftwareRelatorio(); $relatorio->setNivelAcesso('publico'); $relatorio->setNomeRelatorio('Software'); $relatorio->setHabilitaNotificacao(true); $relatorio->setIdUsuario($user); $relatorio->addSoftware($software); $em->persist($relatorio); $em->flush(); // Insere software no relatório $idRelatorio = $relatorio->getIdRelatorio(); $idSoftware = $software->getIdSoftware(); $sql = "INSERT INTO relatorios_software (id_relatorio, id_software)\n VALUES ({$idRelatorio}, {$idSoftware})"; $stmt = $em->getConnection()->prepare($sql); $stmt->execute(); // Agora testa a busca pelo software $classProperty = $em->getRepository("CacicCommonBundle:ClassProperty")->findOneBy(array('nmPropertyName' => 'account-plugin-aim')); $this->assertNotEmpty($classProperty, "Propriedade não encontrada pelo nome account-plugin-aim"); $prop = $em->getRepository("CacicCommonBundle:PropriedadeSoftware")->findOneBy(array('classProperty' => $classProperty)); $this->assertNotEmpty($prop, "Propriedade de software não encontrada"); $prop = $em->getRepository("CacicCommonBundle:PropriedadeSoftware")->findOneBy(array('software' => $software)); $this->assertNotEmpty($prop, "Software não encontradao"); $softwareRelatorio = $em->getRepository("CacicCommonBundle:SoftwareRelatorio")->findSoftware('account-plugin-aim'); $this->assertNotEmpty($softwareRelatorio, "Relatório não encontrado para o software account-plugin-aim"); // Agora envia a modificação $this->static_client->request('POST', '/ws/neo/modifications', array(), array(), array('CONTENT_TYPE' => 'application/json'), $this->modifications); $response = $this->static_client->getResponse(); $status = $response->getStatusCode(); $this->logger->debug("Response status: {$status}"); $this->assertEquals($status, 200); // Limpa o cache para garantir o resultado $em->clear(); // Conecta à rota $crawler = $this->client->request('GET', '/notifications/get', array(), array(), array('CONTENT_TYPE' => 'application/json'), '{}'); // Testa rota $response = $this->client->getResponse(); $status = $response->getStatusCode(); $this->logger->debug("Response status: {$status}"); $this->assertEquals(200, $status); $this->assertNotEmpty($response->getContent(), "Conteúdo de notificações vazio"); //$this->logger->debug("NOTIFICATIONS:\n".$response->getContent()); // Debug JSON Content $tmpfile = sys_get_temp_dir() . '/notifications.json'; file_put_contents($tmpfile, $response->getContent()); $this->logger->debug("NOTIFICATIONS: file temp = {$tmpfile}"); $notifications_json = $response->getContent(); $notifications = json_decode($notifications_json, true); $this->assertCount(2, $notifications, "Não foram encontradas duas notificações"); // Testa o caso em que o software está desabilitado $softwareRelatorio = $em->getRepository("CacicCommonBundle:SoftwareRelatorio")->findSoftware('account-plugin-aim'); $softwareRelatorio->setHabilitaNotificacao(false); $em->persist($softwareRelatorio); $sql = "DELETE FROM notifications"; $stmt = $em->getConnection()->prepare($sql); $stmt->execute(); $em->flush(); // Agora envia a modificação $this->static_client->request('POST', '/ws/neo/modifications', array(), array(), array('CONTENT_TYPE' => 'application/json'), $this->modifications); $response = $this->static_client->getResponse(); $status = $response->getStatusCode(); $this->logger->debug("Response status: {$status}"); $this->assertEquals($status, 200); // Limpa o cache para garantir o resultado $em->clear(); // Conecta à rota $crawler = $this->client->request('GET', '/notifications/get', array(), array(), array('CONTENT_TYPE' => 'application/json'), '{}'); // Testa rota $response = $this->client->getResponse(); $status = $response->getStatusCode(); $this->logger->debug("Response status: {$status}"); $this->assertEquals(200, $status); $tmpfile = sys_get_temp_dir() . '/notifications2.json'; file_put_contents($tmpfile, $response->getContent()); $this->logger->debug("NOTIFICATIONS: file temp = {$tmpfile}"); $notifications_json = $response->getContent(); $notifications = json_decode($notifications_json, true); foreach ($notifications as $elm) { if ($elm['object'] == 'Software') { $this->fail("A coleta de software foi encontrada nas notificações, e não deveria estar"); } } }
/** * Testa criação da notificação a partir do envio de modificações */ public function testModificationsNotifications() { $logger = $this->container->get('logger'); $this->client->request('POST', '/ws/neo/coleta', array(), array(), array('CONTENT_TYPE' => 'application/json'), $this->coleta_modifications); $response = $this->client->getResponse(); $status = $response->getStatusCode(); $logger->debug("Response status: {$status}"); //$logger->debug("JSON da coleta: \n".$response->getContent()); $this->assertEquals($status, 200); // Verifica se o Software Coleta foi inserido $em = $this->container->get('doctrine')->getManager(); // Eduardo: 2015-04-08 // Agora é preciso ativar a notificação de software para recever o resultado $classe = $em->getRepository("CacicCommonBundle:Classe")->findOneBy(array('nmClassName' => 'SoftwareList')); $this->assertNotEmpty($classe, "Classe SoftwareList não encontrada"); $classProperty = $em->getRepository("CacicCommonBundle:ClassProperty")->findOneBy(array('idClass' => $classe, 'nmPropertyName' => 'account-plugin-aim')); $this->assertNotEmpty($classProperty); $software = $em->getRepository("CacicCommonBundle:Software")->findOneBy(array('idClassProperty' => $classProperty)); $this->assertNotEmpty($software, "Software não encontrado para o class_property = " . $classProperty->getNmPropertyName()); // Cria um relatório com esse software $user = $em->getRepository("CacicCommonBundle:Usuario")->find(1); $this->assertNotEmpty($user, "Usuário com ID = 1 não encontrado"); $relatorio = new SoftwareRelatorio(); $relatorio->setNivelAcesso('publico'); $relatorio->setNomeRelatorio('Software'); $relatorio->setHabilitaNotificacao(true); $relatorio->setIdUsuario($user); $relatorio->addSoftware($software); $software->addRelatorio($relatorio); $em->persist($software); $em->persist($relatorio); $em->flush(); // Vamos ver se o software esta realmente no relatorio $softwareRelatorio = $em->getRepository("CacicCommonBundle:SoftwareRelatorio")->findSoftware('account-plugin-aim'); $this->assertNotEmpty($softwareRelatorio, "Software account-plugin-aim não encontrado em nenhum relatório"); $softwareRelatorio->setHabilitaNotificacao(true); $em->persist($softwareRelatorio); $em->flush(); // Agora envia a modificação $this->client->request('POST', '/ws/neo/modifications', array(), array(), array('CONTENT_TYPE' => 'application/json'), $this->modifications); $response = $this->client->getResponse(); $status = $response->getStatusCode(); $logger->debug("Response status: {$status}"); //$logger->debug("JSON da coleta: \n".$response->getContent()); $this->assertEquals($status, 200); // Limpa o cache para garantir o resultado $em->clear(); // Testa se identificou o computador $so = $em->getRepository("CacicCommonBundle:So")->findOneBy(array('teSo' => 'Ubuntu 14.04.1 LTS-x86_64')); $this->assertNotEmpty($so); $computador = $em->getRepository("CacicCommonBundle:Computador")->findOneBy(array('teNodeAddress' => '9C:D2:1E:EA:E0:89', 'idSo' => $so)); $this->assertNotEmpty($computador); // Agora verifica se a notificação foi inserida com sucesso $notifications = $em->getRepository("CacicCommonBundle:Notifications")->findBy(array('notificationAcao' => 'DEL', 'object' => 'Software', 'idComputador' => $computador)); $this->assertCount(1, $notifications, "Não foi encontrada a notificação de remoção do software"); // Valida atributos $this->assertNotEmpty($notifications[0]->getBody(), "O campo body está vazio"); $this->assertNotEmpty($notifications[0]->getFromAddr(), "O campo from está vazio"); $this->assertNotEmpty($notifications[0]->getSubject(), "O campo subject está vazio"); $this->assertEquals('DEL', $notifications[0]->getNotificationAcao(), "A ação não está mapeada como exclusão (DEL)"); $this->assertEquals('Software', $notifications[0]->getObject(), "A ação não está mapeada como objeto Software"); $notifications = $em->getRepository("CacicCommonBundle:Notifications")->findBy(array('notificationAcao' => 'DEL', 'object' => 'Hardware', 'idComputador' => $computador)); $this->assertCount(1, $notifications, "Não foi encontrada a notificação de remoção do software"); // Valida atributos $this->assertNotEmpty($notifications[0]->getBody(), "O campo body está vazio"); $this->assertNotEmpty($notifications[0]->getFromAddr(), "O campo from está vazio"); $this->assertNotEmpty($notifications[0]->getSubject(), "O campo subject está vazio"); $this->assertEquals('DEL', $notifications[0]->getNotificationAcao(), "A ação não está mapeada como exclusão (DEL)"); $this->assertEquals('Hardware', $notifications[0]->getObject(), "A ação não está mapeada como objeto Hardware"); }