|
| 1 | +# Copyright (C) 2024 Eneo Tecnologia S.L. |
| 2 | +# |
| 3 | +# Authors: |
| 4 | +# Miguel Álvarez Adsuara <malvarez@redborder.com> |
| 5 | +# |
| 6 | +# This program is free software: you can redistribute it and/or modify it under the terms of the |
| 7 | +# GNU Affero General Public License as published by the Free Software Foundation, either version 3 |
| 8 | +# of the License, or (at your option) any later version. |
| 9 | +# |
| 10 | +# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without |
| 11 | +# even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | +# Affero General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU Affero General Public License along with this program. |
| 15 | +# If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +import json |
| 18 | +import unittest |
| 19 | +import pandas as pd |
| 20 | +from resources.src.ai.outliers_identifier import OutlierIdentifier |
| 21 | + |
| 22 | +class TestOutlierIdentifier(unittest.TestCase): |
| 23 | + |
| 24 | + def setUp(self): |
| 25 | + self.identifier = OutlierIdentifier() |
| 26 | + |
| 27 | + def test_prepare_data_valid_input(self): |
| 28 | + data = { |
| 29 | + "192.168.1.1": [ |
| 30 | + {"timestamp": "2024-11-14T12:00:00", "result": {"bytes": 500}}, |
| 31 | + {"timestamp": "2024-11-14T12:05:00", "result": {"bytes": 300}} |
| 32 | + ] |
| 33 | + } |
| 34 | + self.identifier.prepare_data(data) |
| 35 | + self.assertIsInstance(self.identifier.df, pd.DataFrame) |
| 36 | + self.assertIn('timestamp', self.identifier.df.columns) |
| 37 | + self.assertIn('bytes', self.identifier.df.columns) |
| 38 | + self.assertEqual(len(self.identifier.df), 2) |
| 39 | + |
| 40 | + def test_prepare_data_missing_bytes(self): |
| 41 | + data = { |
| 42 | + "192.168.1.1": [ |
| 43 | + {"timestamp": "2024-11-14T12:00:00", "result": {}}, |
| 44 | + {"timestamp": "2024-11-14T12:05:00", "result": {}} |
| 45 | + ] |
| 46 | + } |
| 47 | + self.identifier.prepare_data(data) |
| 48 | + self.assertEqual(self.identifier.df['bytes'].sum(), 0) |
| 49 | + |
| 50 | + def test_prepare_data_empty_input(self): |
| 51 | + data = {} |
| 52 | + self.identifier.prepare_data(data) |
| 53 | + self.assertTrue(self.identifier.df.empty) |
| 54 | + |
| 55 | + def test_train_model_valid_data(self): |
| 56 | + data = { |
| 57 | + "192.168.1.1": [ |
| 58 | + {"timestamp": "2024-11-14T12:00:00", "result": {"bytes": 500}}, |
| 59 | + {"timestamp": "2024-11-14T12:05:00", "result": {"bytes": 300}}, |
| 60 | + {"timestamp": "2024-11-14T12:10:00", "result": {"bytes": 1000}}, |
| 61 | + {"timestamp": "2024-11-14T12:15:00", "result": {"bytes": 700}}, |
| 62 | + {"timestamp": "2024-11-14T12:20:00", "result": {"bytes": 0}} |
| 63 | + ] |
| 64 | + } |
| 65 | + self.identifier.prepare_data(data) |
| 66 | + try: |
| 67 | + self.identifier.train_model(self.identifier.df[['hour', 'minute', 'day', 'dayofweek', 'dayofyear', 'rolling_mean', 'rolling_std', 'low_traffic']]) |
| 68 | + except Exception as e: |
| 69 | + self.fail(f"Training failed with exception: {e}") |
| 70 | + |
| 71 | + def test_identify_implicated_ips_no_outliers(self): |
| 72 | + data = { |
| 73 | + "192.168.1.1": [ |
| 74 | + {"timestamp": "2024-11-14T12:00:00", "result": {"bytes": 100}}, |
| 75 | + {"timestamp": "2024-11-14T12:05:00", "result": {"bytes": 100}} |
| 76 | + ] |
| 77 | + } |
| 78 | + outliers = [] |
| 79 | + self.identifier.prepare_data(data) |
| 80 | + self.identifier.train_model(self.identifier.df[['hour', 'minute', 'day', 'dayofweek', 'dayofyear', 'rolling_mean', 'rolling_std', 'low_traffic']]) |
| 81 | + result = self.identifier.identify_implicated_ips(outliers) |
| 82 | + self.assertEqual(result, {"ips": []}) |
| 83 | + |
| 84 | + def test_identify_implicated_ips_with_outliers(self): |
| 85 | + data = { |
| 86 | + "192.168.1.1": [ |
| 87 | + {"timestamp": "2024-11-14T12:00:00", "result": {"bytes": 100}}, |
| 88 | + {"timestamp": "2024-11-14T12:05:00", "result": {"bytes": 1000}}, # Anomalous traffic |
| 89 | + {"timestamp": "2024-11-14T12:10:00", "result": {"bytes": 100}}, |
| 90 | + ] |
| 91 | + } |
| 92 | + outliers = [{"timestamp": "2024-11-14T12:05:00"}] |
| 93 | + self.identifier.prepare_data(data) |
| 94 | + self.identifier.train_model(self.identifier.df[['hour', 'minute', 'day', 'dayofweek', 'dayofyear', 'rolling_mean', 'rolling_std', 'low_traffic']]) |
| 95 | + result = self.identifier.identify_implicated_ips(outliers) |
| 96 | + self.assertIn("192.168.1.1", result["ips"][0]["caused_by"]) |
| 97 | + |
| 98 | + def test_execute_with_valid_input(self): |
| 99 | + data = { |
| 100 | + "192.168.1.1": [ |
| 101 | + {"timestamp": "2024-11-14T12:00:00", "result": {"bytes": 500}}, |
| 102 | + {"timestamp": "2024-11-14T12:05:00", "result": {"bytes": 300}}, |
| 103 | + {"timestamp": "2024-11-14T12:10:00", "result": {"bytes": 1000}} |
| 104 | + ] |
| 105 | + } |
| 106 | + outliers = [{"timestamp": "2024-11-14T12:10:00"}] |
| 107 | + result = self.identifier.execute(outliers, data) |
| 108 | + self.assertIsInstance(result, str) |
| 109 | + parsed_result = json.loads(result) |
| 110 | + self.assertIn("ips", parsed_result) |
| 111 | + self.assertEqual(len(parsed_result["ips"]), 1) |
| 112 | + |
| 113 | + def test_train_and_execute_model_error_handling(self): |
| 114 | + data = None # Invalid data |
| 115 | + outliers = [{"timestamp": "2024-11-14T12:10:00"}] |
| 116 | + result = self.identifier.train_and_execute_model(outliers, data) |
| 117 | + self.assertIn("status", result) |
| 118 | + self.assertEqual(result["status"], "error") |
| 119 | + |
| 120 | + def test_return_error(self): |
| 121 | + error_message = "Test error" |
| 122 | + result = self.identifier.return_error(error_message) |
| 123 | + self.assertEqual(result["status"], "error") |
| 124 | + self.assertEqual(result["msg"], error_message) |
| 125 | + |
| 126 | +if __name__ == "__main__": |
| 127 | + unittest.main() |
0 commit comments