async def test_receive_data(self): """ Testing true case and edge cases for receiving data """ loop = asyncio.get_running_loop() nats = FakeNatsHandler("ground1", "0.0.0.0", "4222", loop=loop, user="******", password="******") await nats.connect() # testing whether receiving works shared_storage = {"pointing": ["sat1"], "packets_received": 0} message = nats.create_message(23.12, MessageSchemas.IOT_DATA_MESSAGE) message.sender_id = "sat1" self.assertTrue( check_pointing_data(message, nats, shared_storage, None)) await receive_data(message, nats, shared_storage, None) self.assertEqual(len(nats._dict["groundstation.packets_received"]), 1) self.assertEqual(shared_storage["packets_received"], 1) # testing whether edge case is actually not working shared_storage = {"pointing": ["sat1"], "packets_received": 0} message = nats.create_message(23.12, MessageSchemas.IOT_DATA_MESSAGE) message.sender_id = "sat2" self.assertFalse( check_pointing_data(message, nats, shared_storage, None))
async def test_take_action(self): """ Testing whether the RL service emits the proper messages upon receiving a new action """ loop = asyncio.get_running_loop() nats = FakeNatsHandler("rl1", "0.0.0.0", "4222", loop=loop, user="******", password="******") await nats.connect() # testing whether receiving works shared_storage = { "phonebook": { "sat1": False, "sat2": False }, "iot_phonebook": { "iot1": True, "iot2": False }, "ground_phonebook": { "grstn1": False, "grstn2": False }, "buffered_packets": 2365, "packets_received": { "grstn1": 212, "grstn2": 573 } } # testing correct internal case message = nats.create_message({ "action": "sending", "id": "sat1" }, MessageSchemas.RL_ACTION_MESSAGE) await take_action(message, nats, shared_storage, None) self.assertEqual(len(nats._dict["internal.rl-action.mode"]), 1) self.assertEqual(nats._dict["internal.rl-action.mode"][0].data, "sending") self.assertEqual(len(nats._dict["command.point"]), 1) self.assertEqual(nats._dict["command.point"][0].data, "sat1") # testing wrong not internal case message = nats.create_message({ "action": "sending", "id": "sat1" }, MessageSchemas.RL_ACTION_MESSAGE) message.origin_id = "sat2" await take_action(message, nats, shared_storage, None) self.assertEqual(len(nats._dict["internal.rl-action.mode"]), 1) self.assertEqual(nats._dict["internal.rl-action.mode"][0].data, "sending") self.assertEqual(len(nats._dict["command.point"]), 1) self.assertEqual(nats._dict["command.point"][0].data, "sat1")
async def test_buffer_packet(self): """ Testing relay callback and edge cases """ loop = asyncio.get_running_loop() nats = FakeNatsHandler("data1", "0.0.0.0", "4222", loop=loop, user="******", password="******") await nats.connect() shared_storage = {"pointing": ["sat1"], "mode": "receiving"} message = nats.create_message(23.12, MessageSchemas.IOT_DATA_MESSAGE) self.assertTrue( check_internal_data(message, nats, shared_storage, None)) await buffer_packet(message, nats, shared_storage, None) self.assertEqual(buffer.qsize(), 1) buffer.get() shared_storage = {"pointing": ["sat1"], "mode": "receiving"} message = nats.create_message(23.12, MessageSchemas.IOT_DATA_MESSAGE) message.sender_id = "sat2" self.assertFalse( check_internal_data(message, nats, shared_storage, None))
async def test_process_data(self): """ Testing application service callback """ loop = asyncio.get_running_loop() nats = FakeNatsHandler("app1", "0.0.0.0", "4222", loop=loop, user="******", password="******") await nats.connect() shared_storage = {} # testing correct case message = nats.create_message(23.45, MessageSchemas.IOT_DATA_MESSAGE) self.assertTrue( check_internal_data(message, nats, shared_storage, None)) await process_data(message, nats, shared_storage, None) self.assertEqual(len(nats._dict["internal.data.out"]), 1) # testing incorrect case (not internal) message = nats.create_message(23.45, MessageSchemas.IOT_DATA_MESSAGE) message.sender_id = "sat1" self.assertFalse( check_internal_data(message, nats, shared_storage, None))
async def test_receive_data(self): """ Testing true case and edge cases for iot send data """ loop = asyncio.get_running_loop() nats = FakeNatsHandler("data1", "0.0.0.0", "4222", loop=loop, user="******", password="******") await nats.connect() # testing whether receiving works shared_storage = { "pointing": ["sat1"], "pointing_to": "sat1", "mode": "receiving" } message = nats.create_message(23.12, MessageSchemas.IOT_DATA_MESSAGE) message.sender_id = "sat1" self.assertTrue( check_pointing_and_mode(message, nats, shared_storage, None)) await receive_packet(message, nats, shared_storage, None) self.assertEqual(len(nats._dict["internal.data.in"]), 1) # testing whether receiving blocks if in wrong mode shared_storage = { "pointing": ["sat1"], "pointing_to": "sat1", "mode": "sending" } message = nats.create_message(23.12, MessageSchemas.IOT_DATA_MESSAGE) message.sender_id = "sat1" self.assertFalse( check_pointing_and_mode(message, nats, shared_storage, None)) # testing whether receiving blocks if not pointing shared_storage = { "pointing": ["sat2"], "pointing_to": "sat1", "mode": "receiving" } message = nats.create_message(23.12, MessageSchemas.IOT_DATA_MESSAGE) message.sender_id = "sat1" self.assertFalse( check_pointing_and_mode(message, nats, shared_storage, None)) # testing whether receiving blocks not pointing shared_storage = { "pointing": ["sat1"], "pointing_to": "sat2", "mode": "receiving" } message = nats.create_message(23.12, MessageSchemas.IOT_DATA_MESSAGE) message.sender_id = "sat1" self.assertFalse( check_pointing_and_mode(message, nats, shared_storage, None))
async def test_update_phonebook(self): """ Testing whether phonebook updates work properly """ loop = asyncio.get_running_loop() nats = FakeNatsHandler("rl1", "0.0.0.0", "4222", loop=loop, user="******", password="******") await nats.connect() # testing whether receiving works shared_storage = { "phonebook": { "sat1": False, "sat2": False }, "iot_phonebook": { "iot1": True, "iot2": False }, "ground_phonebook": { "grstn1": False, "grstn2": False }, "buffered_packets": 2365, "packets_received": { "grstn1": 212, "grstn2": 573 } } # testing correct internal case message = nats.create_message({ "sat1": False, "sat2": True }, MessageSchemas.PHONEBOOK_MESSAGE) await update_phonebook(message, nats, shared_storage, None) self.assertTrue(shared_storage["phonebook"]["sat2"]) self.assertFalse(shared_storage["phonebook"]["sat1"]) # testing wrong not internal case message = nats.create_message({ "sat1": True, "sat2": False }, MessageSchemas.PHONEBOOK_MESSAGE) message.origin_id = "sat2" await update_phonebook(message, nats, shared_storage, None) self.assertTrue(shared_storage["phonebook"]["sat2"]) self.assertFalse(shared_storage["phonebook"]["sat1"])
async def test_fertilization_level(self): """ Tests creating fertilization data """ loop = asyncio.get_running_loop() nats = FakeNatsHandler("iot1", "0.0.0.0", "4222", loop=loop, user="******", password="******") await nats.connect() shared_storage = {"data_rate": 5} message = nats.create_message({"time": "2020-07-05T09:51:49.8"}, MessageSchemas.TIMESTEP_MESSAGE) await fertilization_level(message, nats, shared_storage, None) time_struct = datetime.strptime("2020-07-05T09:51:49.8", "%Y-%m-%dT%H:%M:%S.%f") total_seconds = (time_struct - datetime(1970, 1, 1)).total_seconds() seconds_in_week = total_seconds % (60 * 60 * 24 * 7) data_value = 0.25 * math.sin(2 * math.pi * seconds_in_week * (1 / 604800)) + 0.25 self.assertEqual(nats._dict["data.out"][0].data, data_value)
async def test_print_log(self): """ Testing printing the log info """ loop = asyncio.get_running_loop() nats = FakeNatsHandler("cubesat_1", "4222", loop=loop, user="******", password="******") await nats.connect() shared_storage = {"log_path": "logger_test"} message = nats.create_message( { "logged_at": "logging_service", "line_number": 43, "function": "logger_test", "level": "level one", "msg": "This is a test!" }, MessageSchemas.LOG_MESSAGE) await logging_service.print_log(message, nats, shared_storage, None) self.assertTrue("logger_test" in os.listdir()) os.remove("./logger_test")
async def test_update_rl(self): """ Testing whether rl service provides proper new inputs for RL environment """ loop = asyncio.get_running_loop() nats = FakeNatsHandler("rl1", "0.0.0.0", "4222", loop=loop, user="******", password="******") await nats.connect() # testing whether receiving works shared_storage = { "predict_mode": False, "phonebook": { "sat1": False, "sat2": False }, "iot_phonebook": { "iot1": True, "iot2": False }, "ground_phonebook": { "grstn1": False, "grstn2": False }, "buffered_packets": 2365, "packets_received": { "grstn1": 212, "grstn2": 573 } } message = nats.create_message(None, MessageSchemas.RL_MESSAGE) response = await update_rl(message, nats, shared_storage, None) self.assertIsInstance(response, Message) self.assertEqual( response.data.keys(), { "phonebook": None, "iot_phonebook": None, "ground_phonebook": None, "buffered_packets": None }.keys()) self.assertEquals(response.data["phonebook"], { "sat1": False, "sat2": False }) self.assertEquals(response.data["iot_phonebook"], { "iot1": True, "iot2": False }) self.assertEquals(response.data["ground_phonebook"], { "grstn1": False, "grstn2": False }) self.assertEquals(response.data["buffered_packets"], 2365)
async def test_send_visualization_packet(self): """ TODO """ logger = FakeLogger() loop = asyncio.get_running_loop() nats = FakeNatsHandler("cubesat_1", "4222", loop=loop, user="******", password="******") await nats.connect() f = open("./config/simulation_config/simulation/czml.json") shared_storage = json.load(f)["shared_storage"] shared_storage["swarm"]["cubesat_1"] = { "orbit": { "semimajor_axis": 6801395.04, "eccentricity": 0.0008641, "inclination": 0.0, "perigee_argument": 2.0150855958, "right_ascension_of_ascending_node": 3.6338243719, "anomaly": 4.270387838, "anomaly_type": "MEAN", "orbit_update_date": "2020-07-15T00:00:00.000", "frame": "EME", "attitude": "nadir_tracking" }, "target_in_view": True, "last_update_time": "2010-07-02T03:00:00.000", "mode": "receiving" } shared_storage["grstns"]["grstn_1"] = { "location": { "latitude": 0.0, "longitude": 0.0, "altitude": 0.0 } } data = { "sender_ID": "cubesat_2", "time_sent": "2021-07-31T03:00:00.000", "data": { "time": "2021-07-31T03:00:00.000" } } message = nats.create_message(data["data"], MessageSchemas.TIMESTEP_MESSAGE) print(shared_storage) await graphics_service.send_visualization_packet( message, nats, shared_storage, logger) for i in nats._dict["graphics.grstn2sat"]: print(i.data) self.assertTrue(False)
async def test_initialize_service(self): """ Test initialization of services based on config files """ loop = asyncio.get_running_loop() shared_storage = { "simulation": { "clock": False, "logging": False, "czml": False, "config": False }, "cubesats": { "cubesat_1": { "orbits": False, "rl": False, "rl_training": False, "data": False, "agriculture": False } }, "groundstations": { "groundstation_1": { "groundstation": False } }, "iots": { "iot_1": { "iot": False } }, "config_path": "./simulation_config" } nats = FakeNatsHandler("data1", "0.0.0.0", "4222", loop=loop, user="******", password="******") await nats.connect() logger = FakeLogger() message = nats.create_message("data", MessageSchemas.SERVICE_TYPE_MESSAGE) await initialize_service(message, nats, shared_storage, logger) self.assertTrue(shared_storage["cubesats"]["cubesat_1"]["data"])
async def test_iot_send_data(self): """ Tests whether a new data packet is sent upon receiving a timestep signal """ loop = asyncio.get_running_loop() nats = FakeNatsHandler("iot1", "0.0.0.0", "4222", loop=loop, user="******", password="******") await nats.connect() message = nats.create_message({"time": "2020-07-05T09:51:49.8"}, MessageSchemas.TIMESTEP_MESSAGE) shared_storage = {"data_rate": 5} await send_data(message, nats, shared_storage, None) self.assertEqual(len(nats._dict["data.out"]), 5)
async def test_send_ip_address(self): loop = asyncio.get_running_loop() nats = FakeNatsHandler("cubesat_1", "0.0.0.0", "4222", loop=loop, user="******", password="******") await nats.connect() shared_storage = dict() message = nats.create_message({"time": ""}, MessageSchemas.TIMESTEP_MESSAGE) await send_ip_address(message, nats, shared_storage, None) self.assertEqual(nats._dict["cluster.ip"][0].data, "api_host")
async def test_update_sat_ip(self): loop = asyncio.get_running_loop() nats = FakeNatsHandler("cubesat_1", "0.0.0.0", "4222", loop=loop, user="******", password="******") await nats.connect() shared_storage = {"ip_map": {}, "ip_cluster_map": {}} message = nats.create_message("ip_1", MessageSchemas.IP_ADDRESS_MESSAGE) await ground_service.update_sat_ip(message, nats, shared_storage, None) self.assertEqual(shared_storage["ip_map"], {"cubesat_1": "ip_1"}) self.assertEqual(shared_storage["ip_cluster_map"], {"cubesat_1": []})
async def test_update_cluster(self): loop = asyncio.get_running_loop() nats = FakeNatsHandler("cubesat_1", "0.0.0.0", "4222", loop=loop, user="******", password="******") await nats.connect() shared_storage = { "ip_map": { "cubesat_1": "ip_1", "cubesat_2": "ip_2", "cubesat_3": "ip_3" }, "ip_cluster_map": { "cubesat_1": [], "cubesat_2": [], "cubesat_3": [] } } message = nats.create_message( { "cubesat_1": True, "cubesat_2": True, "cubesat_3": True }, MessageSchemas.PHONEBOOK_MESSAGE) await update_cluster(message, nats, shared_storage, None) cluster_message = { "recipient": "cubesat_1", "ip_map": ["ip_2", "ip_3"] } self.assertTrue(nats._dict["command.cluster"][0], cluster_message) self.assertTrue( shared_storage["ip_cluster_map"]["cubesat_1"] == ["ip_2", "ip_3"])
async def test_soil_water_content(self): loop = asyncio.get_running_loop() nats = FakeNatsHandler("iot1", "0.0.0.0", "4222", loop=loop, user="******", password="******") await nats.connect() shared_storage = {"data_rate": 5} message = nats.create_message({"time": "2020-07-05T09:51:49.8"}, MessageSchemas.TIMESTEP_MESSAGE) await soil_water_content(message, nats, shared_storage, None) time_struct = datetime.strptime("2020-07-05T09:51:49.8", "%Y-%m-%dT%H:%M:%S.%f").timetuple() total_seconds = time_struct.tm_hour * 3600 + time_struct.tm_min * 60 + time_struct.tm_sec data_value = 10 * math.cos(2 * math.pi * total_seconds * (1 / 86400)) + 40 self.assertEqual(nats._dict["data.out"][0].data, data_value)
async def test_update_pointing_list(self): """ Tests whether the list of sats pointing at the ground station is properly updated """ loop = asyncio.get_running_loop() nats = FakeNatsHandler("ground1", "0.0.0.0", "4222", loop=loop, user="******", password="******") await nats.connect() shared_storage = {"pointing": []} message = nats.create_message( { "state": { "sat1": { "orbit": { "semimajor_axis": 12742500.0, "eccentricity": 0.0, "inclination": 0.78, "perigee_argument": 1.570796, "right_ascension_of_ascending_node": 0.0, "anomaly": 1.570796, "anomaly_type": "TRUE", "orbit_update_date": "2021-12-02T00:00:00.000", "frame": "EME", "attitude": "ground1" }, "target_in_view": True, "last_update_time": "2021-12-02T03:00:00.000" } } }, MessageSchemas.STATE_MESSAGE) message.origin_id = "sat1" await update_pointing_list(message, nats, shared_storage, None) self.assertEqual(shared_storage["pointing"], ["sat1"]) message = nats.create_message( { "state": { "sat2": { "orbit": { "semimajor_axis": 12742500.0, "eccentricity": 0.0, "inclination": 0.1, "perigee_argument": 1.0, "right_ascension_of_ascending_node": 0.0, "anomaly": 0.0, "anomaly_type": "TRUE", "orbit_update_date": "2021-12-02T00:00:00.000", "frame": "EME", "attitude": "ground1" }, "target_in_view": True, "last_update_time": "2021-12-02T03:00:00.000" } } }, MessageSchemas.STATE_MESSAGE) message.origin_id = "sat2" await update_pointing_list(message, nats, shared_storage, None) self.assertEqual(shared_storage["pointing"], ["sat1", "sat2"]) message = nats.create_message( { "state": { "sat1": { "orbit": { "semimajor_axis": 12742500.0, "eccentricity": 0.0, "inclination": 0.78, "perigee_argument": 1.570796, "right_ascension_of_ascending_node": 0.0, "anomaly": 1.570796, "anomaly_type": "TRUE", "orbit_update_date": "2021-12-02T00:00:00.000", "frame": "EME", "attitude": "iot2" }, "target_in_view": True, "last_update_time": "2021-12-02T03:00:00.000" } } }, MessageSchemas.STATE_MESSAGE) message.origin_id = "sat1" await update_pointing_list(message, nats, shared_storage, None) self.assertEqual(shared_storage["pointing"], ["sat2"]) message = nats.create_message( { "state": { "sat2": { "orbit": { "semimajor_axis": 12742500.0, "eccentricity": 0.0, "inclination": 0.1, "perigee_argument": 1.0, "right_ascension_of_ascending_node": 0.0, "anomaly": 0.0, "anomaly_type": "TRUE", "orbit_update_date": "2021-12-02T00:00:00.000", "frame": "EME", "attitude": "ground1" }, "target_in_view": False, "last_update_time": "2021-12-02T03:00:00.000" } } }, MessageSchemas.STATE_MESSAGE) message.origin_id = "sat2" await update_pointing_list(message, nats, shared_storage, None) self.assertEqual(shared_storage["pointing"], []) message = nats.create_message( { "state": { "sat1": { "orbit": { "semimajor_axis": 12742500.0, "eccentricity": 0.0, "inclination": 0.78, "perigee_argument": 1.570796, "right_ascension_of_ascending_node": 0.0, "anomaly": 1.570796, "anomaly_type": "TRUE", "orbit_update_date": "2021-12-02T00:00:00.000", "frame": "EME", "attitude": "ground1" }, "target_in_view": False, "last_update_time": "2021-12-02T03:00:00.000" } } }, MessageSchemas.STATE_MESSAGE) message.origin_id = "sat1" await update_pointing_list(message, nats, shared_storage, None) self.assertEqual(shared_storage["pointing"], [])