class TestCollectorCRUD(APITestCase): """Test case for CRUD operations on Collector""" def setUp(self): # Collector test record self.data = { 'name': 'nfcol1', 'ip_address': '10.10.21.21', 'udp_port': 2055, 'col_type': 'rawforward', 'encapsulation_protocol': 'udp', } self.url_prefix = '/v1.0/secmon/collector/' self.notification_url_prefix = '/v1.0/secmon/notification/' # Store the test record in storage if not self._testMethodName.startswith('test_post_'): self.uuid = generate_uuid() # Generate an id except for POST self.uuid1 = generate_uuid() self.data.update({'id': self.uuid}) self.collector = Collector(**self.data) self.collector.save() # For POST & List, # Use the url with name 'collector_list' in urls.py file if self._testMethodName.startswith('test_post_') or \ self._testMethodName.startswith('test_list_'): self.url = reverse('collector_list', kwargs={'version': 'v1.0', 'namespace': 'secmon' }) def tearDown(self): # Delete the test record in storage and revert storage to original # state. if self._testMethodName.startswith('test_put_notification'): if not self._testMethodName.endswith('invalid_value'): Notification.get(self.uuid1).delete() if self._testMethodName.startswith('test_notification_post'): Notification.get(self.uuid).delete() if self._testMethodName.startswith('test_post_'): if not self._testMethodName.endswith('invalid_values'): Collector.get(self.uuid).delete() # Use 'id' of POST response else: self.collector.delete() def test_post_collector(self): """Test case to create Collector.""" # No 'id' provided response = self.client.post(self.url, self.data, format='json') self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.uuid = response.data.pop('id') self.assertTrue(is_valid_uuid(self.uuid)) self.assertEqual(unicode_to_ascii_dict(response.data), unicode_to_ascii_dict(self.data)) def test_notification_post(self): """Test case to create notification without id provided""" self.url = self.notification_url_prefix self.notification_data = { 'table_name': 'collector1', 'row_id': '33eb67b8-df63-4af5-b04a-97577a2477c1', 'operation': 'PUT', } response = self.client.post(self.url, self.notification_data, format='json') # self.uuid1 = response.data.pop('id') # self.assertEqual(self.uuid1, '1') self.uuid = response.data.pop('id') self.assertTrue(is_valid_uuid(self.uuid)) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.notification_data['row_data'] = None self.assertEqual(response.data, self.notification_data) def test_put_notification_with_id(self): """Test case to create notification with id provided""" self.url = self.notification_url_prefix + self.uuid1 + '/' self.notification_data = { 'id': self.uuid1, 'table_name': 'collector2', 'row_id': '33eb67b8-df63-4af5-b04a-97577a2477c1', 'operation': 'PUT', } response = self.client.put(self.url, self.notification_data, format='json') self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.notification_data['row_data'] = None self.notification_data['id'] = '1' self.uuid1 = '1' self.assertEqual(response.data, self.notification_data) def test_put_notification_with_invalid_value(self): """Test case to test notification with invalid values""" self.url = self.notification_url_prefix + self.uuid + '/' self.notification_data = { 'table_name': 'collector3', 'row_id': '', 'operation': 'PUT', } response = self.client.put(self.url, self.notification_data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) def test_delete_notification_with_valid_id(self): """Test case to test delete notification with valid id""" # post notification before deleting self.url = self.notification_url_prefix + '1/' self.notification_data = { 'table_name': 'collector4', 'row_id': '33eb67b8-df63-4af5-b04a-97577a2477c1', 'operation': 'PUT', } response = self.client.put(self.url, self.notification_data, format='json') # delete notification self.url = self.notification_url_prefix + '1/' response = self.client.delete(self.url) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) def test_delete_notification_with_invalid_id(self): """Test case to test delete notification with invalid id""" self.url = self.notification_url_prefix + self.uuid + '/' response = self.client.delete(self.url) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) def test_post_collector_with_id(self): # 'id' provided self.uuid = generate_uuid() self.data.update({'id': self.uuid}) response = self.client.post(self.url, self.data, format='json') self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(unicode_to_ascii_dict(response.data), unicode_to_ascii_dict(self.data)) def test_post_collector_with_invalid_values(self): """Test case to create Collector with invalid values""" self._invalid_values = { 'name': 'collector2', 'ip_address': '10.42.0.12', 'udp_port': '80000', 'col_type': 'rawforward', 'encapsulation_protocol': 'sflow', } # for update_value in self._invalid_values: self.data.update(self._invalid_values) response = self.client.post(self.url, self._invalid_values, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) LOG.debug(unicode_to_ascii_dict(response.data)) def test_get_collector_from_valid_pk(self): """Test case to get collector from secondary key value""" self.test_response = [] self.url = self.url_prefix + 'rawforward/' response = self.client.get(self.url) self.assertEqual(response.status_code, status.HTTP_200_OK) self.test_response.append(self.data) self.assertEqual(self.test_response, response.data) def test_get_collector_from_invalid_pk(self): """Test case to get collector from secondary key value""" self.url = self.url_prefix + 'sflow/' response = self.client.get(self.url) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) def test_list_collector(self): """Test case to list all Collector.""" response = self.client.get(self.url) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(unicode_to_ascii_dict(self.data) in unicode_to_ascii_dict(response.data)) def test_get_collector(self): """Test case to get or show Collector.""" self.url = self.url_prefix + self.uuid + '/' response = self.client.get(self.url) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(unicode_to_ascii_dict(response.data), unicode_to_ascii_dict(self.data)) def test_delete_collector(self): """Test case to delete Collector.""" self.url = self.url_prefix + self.uuid + '/' response = self.client.delete(self.url) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) def test_put_collector(self): """Test case to update Collector.""" self.url = self.url_prefix + self.uuid + '/' self._update_values = { 'id': self.uuid, 'name': 'new_collector1', 'ip_address': '10.42.0.12', 'udp_port': 2055, 'col_type': 'netflow', 'encapsulation_protocol': '', } response = self.client.put(self.url, self._update_values, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.data.update(self._update_values) # Update the self.collector with the new attribute value for key, value in self._update_values.items(): setattr(self.collector, key, value) self.assertEqual(unicode_to_ascii_dict(response.data), unicode_to_ascii_dict(self.data)) def test_put_collector_with_invalid_values(self): """Test case to update a Collector with invalid value of attributes""" self.url = self.url_prefix + self.uuid + '/' self._invalid_values = { 'id': generate_uuid(), # 'id' update not allowed 'name': 'new_collector1', 'ip_address': '10.42.0.12', 'udp_port': 70000, 'col_type': 'netflow', 'encapsulation_protocol': '', } response = self.client.put(self.url, self._invalid_values, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) LOG.debug(unicode_to_ascii_dict(response.data))
def test_delete_invalid_record(self): """test case to test delete with invalid record""" collector = Collector(**self.data2) with self.assertRaises(ResourceNotFound): collector.delete()