add: hub template

This commit is contained in:
Toolf
2024-02-12 18:18:38 +02:00
parent abd6bf0abe
commit 173a61d117
15 changed files with 518 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
import unittest
from unittest.mock import Mock
import redis
from app.adapters.agent_mqtt_adapter import AgentMQTTAdapter
from app.interfaces.store_gateway import StoreGateway
from app.entities.agent_data import AccelerometerData, AgentData, GpsData
from app.usecases.data_processing import process_agent_data_batch
class TestAgentMQTTAdapter(unittest.TestCase):
def setUp(self):
# Create a mock StoreGateway for testing
self.mock_store_gateway = Mock(spec=StoreGateway)
self.mock_redis = Mock(spec=redis.Redis)
# Create the AgentMQTTAdapter instance with the mock StoreGateway
self.agent_adapter = AgentMQTTAdapter(
broker_host="test_broker",
broker_port=1234,
topic="test_topic",
store_gateway=self.mock_store_gateway,
redis_client=self.mock_redis,
batch_size=1,
)
def test_on_message_valid_data(self):
# Test handling of valid incoming MQTT message
# (Assuming data is in the correct JSON format)
valid_json_data = '{"user_id": 1,"accelerometer": {"x": 0.1, "y": 0.2, "z": 0.3}, "gps": {"latitude": 10.123, "longitude": 20.456}, "timestamp": "2023-07-21T12:34:56Z"}'
mock_msg = Mock(payload=valid_json_data.encode("utf-8"))
self.mock_redis.llen.return_value = 1
self.mock_redis.rpop.return_value = valid_json_data
# Call on_message with the mock message
self.agent_adapter.on_message(None, None, mock_msg)
# Ensure that the store_gateway's save_data method is called once with the correct arguments
expected_agent_data = AgentData(
user_id=1,
accelerometer=AccelerometerData(
x=0.1,
y=0.2,
z=0.3,
),
gps=GpsData(
latitude=10.123,
longitude=20.456,
),
timestamp="2023-07-21T12:34:56Z",
)
self.mock_store_gateway.save_data.assert_called_once_with(
process_agent_data_batch([expected_agent_data])
)
def test_on_message_invalid_data(self):
# Test handling of invalid incoming MQTT message
# (Assuming data is missing required fields or has incorrect format)
invalid_json_data = '{"user_id": 1, "accelerometer": {"x": 0.1, "y": 0.2}, "gps": {"latitude": 10.123}, "timestamp": 12345}'
mock_msg = Mock(payload=invalid_json_data.encode("utf-8"))
# Call on_message with the mock message
self.agent_adapter.on_message(None, None, mock_msg)
# Ensure that the store_gateway's save_data method is not called (due to invalid data)
self.mock_store_gateway.save_data.assert_not_called()
if __name__ == "__main__":
unittest.main()

View File

@@ -0,0 +1,72 @@
import requests
import unittest
from unittest.mock import Mock, patch
from app.adapters.store_api_adapter import StoreApiAdapter
from app.entities.agent_data import AccelerometerData, AgentData, GpsData
from app.entities.processed_agent_data import ProcessedAgentData
class TestStoreApiAdapter(unittest.TestCase):
def setUp(self):
# Create the StoreApiAdapter instance
self.store_api_adapter = StoreApiAdapter(api_base_url="http://test-api.com")
@patch.object(requests, "post")
def test_save_data_success(self, mock_post):
# Test successful saving of data to the Store API
# Sample processed road data
agent_data = AgentData(
user_id=1,
accelerometer=AccelerometerData(
x=0.1,
y=0.2,
z=0.3,
),
gps=GpsData(
latitude=10.123,
longitude=20.456,
),
timestamp="2023-07-21T12:34:56Z",
)
processed_data = ProcessedAgentData(road_state="normal", agent_data=agent_data)
# Mock the response from the Store API
mock_response = Mock(status_code=201) # 201 indicates successful creation
mock_post.return_value = mock_response
# Call the save_data method
result = self.store_api_adapter.save_data(processed_data)
# Ensure that the post method of the mock is called with the correct arguments
mock_post.assert_called_once_with(
"http://test-api.com/agent_data", json=processed_data.model_dump()
)
# Ensure that the result is True, indicating successful saving
self.assertTrue(result)
@patch.object(requests, "post")
def test_save_data_failure(self, mock_post):
# Test failure to save data to the Store API
# Sample processed road data
agent_data = AgentData(
user_id=1,
accelerometer=AccelerometerData(
x=0.1,
y=0.2,
z=0.3,
),
gps=GpsData(
latitude=10.123,
longitude=20.456,
),
timestamp="2023-07-21T12:34:56Z",
)
processed_data = ProcessedAgentData(road_state="normal", agent_data=agent_data)
# Mock the response from the Store API
mock_response = Mock(status_code=400) # 400 indicates a client error
mock_post.return_value = mock_response
# Call the save_data method
result = self.store_api_adapter.save_data(processed_data)
# Ensure that the post method of the mock is called with the correct arguments
mock_post.assert_called_once_with(
"http://test-api.com/agent_data", json=processed_data.model_dump()
)
# Ensure that the result is False, indicating failure to save
self.assertFalse(result)
if __name__ == "__main__":
unittest.main()