def test_raise_exception_when_set_station_barrier_status_and_status_code_is_diffrent_than_201( self, mock_post): adapter = GatekeeperAdapter() station_name = "Test_2" barrier_status = "open" with self.assertRaises(GatekeeperAdapterResponseException) as error: adapter.set_station_barrier_status(station_name, barrier_status) expected_exception_msg = "Invalid response: 404, {'error': 'Not found'}" expected_call = call( json={ "station": station_name, "barrier_status": barrier_status }, url="http://gatekeeper:5000/api/v1/barrier", ) self.assertEqual(expected_exception_msg, str(error.exception)) mock_post.assert_called_once() self.assertIn(expected_call, mock_post.call_args_list)
def test_raise_exception_when_set_station_barrier_status_and_requests_raise_exception( self, mock_post): adapter = GatekeeperAdapter() station_name = "Test" barrier_status = "open" with self.assertRaises(GatekeeperAdapterResponseException) as error: adapter.set_station_barrier_status(station_name, barrier_status) expected_exception_msg = "Invalid request POST: test" expected_call = call( json={ "station": station_name, "barrier_status": barrier_status }, url="http://gatekeeper:5000/api/v1/barrier", ) self.assertEqual(expected_exception_msg, str(error.exception)) mock_post.assert_called_once() self.assertIn(expected_call, mock_post.call_args_list)
def test_set_station_barrier_status(self, mock_post): adapter = GatekeeperAdapter() station_name = "Test" barrier_status = "open" response_json = adapter.set_station_barrier_status( station_name, barrier_status) expected_json = {"status": "success"} expected_call = call( json={ "station": station_name, "barrier_status": barrier_status }, url="http://gatekeeper:5000/api/v1/barrier", ) self.assertEqual(response_json, expected_json) mock_post.assert_called_once() self.assertIn(expected_call, mock_post.call_args_list)
class StationHandler: def __init__(self): self.info_log_path = os.path.join(LOG_DIR, LOG_FILE_NAME) self.gatekeeper_adapter = GatekeeperAdapter() def train_goes_to_station(self, station_name): self.__save_station_to_info_log(station_name) self.__check_barrier_status(station_name) self.__open_barrier_after_time(station_name, CLOSE_BARRIER_TIME_IN_SEC) def __check_barrier_status(self, station_name): if self.__check_if_station_barrier_is_open(station_name): self.close_barrier(station_name) self.__save_closing_time_into_log(station_name) else: self.__save_error_to_info_log(station_name, "barrier closed") def __check_if_station_barrier_is_open(self, station_name): response_json = self.gatekeeper_adapter.get_station_barrier_status( station_name) return True if response_json["status"] == "open" else False def close_barrier(self, station_name): self.gatekeeper_adapter.set_station_barrier_status( station_name, "closed") def __save_closing_time_into_log(self, station_name): text = "time: {} station_name: {} status: barrier closed \n".format( datetime.now(), station_name) self.__append_text_to_info_log_file(text) def __save_error_to_info_log(self, station_name, error_info): text = "time: {} station_name: {} error_info: {} \n".format( datetime.now(), station_name, error_info) self.__append_text_to_info_log_file(text) def __save_station_to_info_log(self, station_name): text = "time: {} station_name: {} \n".format(datetime.now(), station_name) self.__append_text_to_info_log_file(text) def __append_text_to_info_log_file(self, text): file = open(self.info_log_path, "a") file.write(text) file.close() def __start_process_to_open_barrier_after_time(self, station_name, sec): time.sleep(sec) self.open_barrier(station_name) self.__save_opening_time_into_log(station_name) def open_barrier(self, station_name): self.gatekeeper_adapter.set_station_barrier_status( station_name, "open") def __save_opening_time_into_log(self, station_name): text = "time: {} station_name: {} status: barrier opened \n".format( datetime.now(), station_name) self.__append_text_to_info_log_file(text) def __open_barrier_after_time(self, station_name, sec): p = Process( target=self.__start_process_to_open_barrier_after_time, args=(station_name, sec), ) p.start() p.join()