def test_modify_events(self): prepare_events(self.event_dao) # use test event in 'new_app' to check new_app_event = SAMPLE_EVENT.copy() new_app_event.app = 'new_app' new_app_event.name = 'event1' # first check url = "/default/event_models?app=new_app" response = self.fetch(url) res = json.loads(response.body) self.assertEqual(len(res['values']), 1) get_event = EventModel.from_dict(res['values'][0]) self.assertEqual(new_app_event, get_event) # 1. modify event and test # 1.1 modify test event, so it is doesn't equal now new_app_event.remark = 'new_remark' self.assertNotEquals(new_app_event, get_event) # 1.2 post the modified event url = "/default/event_models" response = self.fetch(url, method='POST', body='[%s]' % new_app_event.get_json()) res = json.loads(response.body) self.assertEqual(res['status'], 0) self.assertEqual(res['msg'], 'ok') # 1.3 verify again url = "/default/event_models?app=new_app" response = self.fetch(url) res = json.loads(response.body) self.assertEqual(len(res['values']), 1) get_event = EventModel.from_dict(res['values'][0]) # now equal again self.assertEqual(new_app_event, get_event) # 2. add an event with post # 2.1 now there are 3 events url = "/default/event_models" response = self.fetch(url) res = json.loads(response.body) self.assertEqual(len(res['values']), 3) # 2.2 add one added_event = SAMPLE_EVENT.copy() added_event.name = 'added_event' response = self.fetch(url, method='POST', body='[%s]' % added_event.get_json()) res = json.loads(response.body) self.assertEqual(res['status'], 0) self.assertEqual(res['msg'], 'ok') # 2.3 now there are 4 response = self.fetch(url) res = json.loads(response.body) self.assertEqual(len(res['values']), 4)
def tests_modify_event(self): prepare_events(self.cust_dao) url = "/platform/event_models/event/{}/{}".format(SAMPLE_EVENT.app, SAMPLE_EVENT.name) response = self.fetch(url) res = json.loads(response.body) self.assertEqual(res['status'], 0) self.assertEqual(res['msg'], 'ok') self.assertEqual(len(res['values']), 1) get_event = EventModel.from_dict(res['values'][0]) self.assertEqual(get_event, SAMPLE_EVENT) # 1. modify SAMPLE_EVENT # 1.1 first not equal new_sample = SAMPLE_EVENT.copy() new_sample.remark = 'modified' self.assertNotEquals(get_event, new_sample) # 1.2 second modify response = self.fetch(url, method='POST', body=new_sample.get_json()) res = json.loads(response.body) self.assertEqual(res['status'], 0) self.assertEqual(res['msg'], 'ok') # 1.3 third check response = self.fetch(url) res = json.loads(response.body) self.assertEqual(res['status'], 0) self.assertEqual(res['msg'], 'ok') self.assertEqual(len(res['values']), 1) get_event = EventModel.from_dict(res['values'][0]) self.assertEqual(get_event, new_sample) # 2. add a new one # 2.1 get 404 url = "/platform/event_models/event/{}/{}".format(SAMPLE_EVENT.app, 'event_to_be_add') response = self.fetch(url) res = json.loads(response.body) self.assertEqual(res['status'], 404) # 2.2 add one event_to_be_add = SAMPLE_EVENT.copy() event_to_be_add.name = 'event_to_be_add' response = self.fetch(url, method='POST', body=event_to_be_add.get_json()) res = json.loads(response.body) self.assertEqual(res['status'], 0) self.assertEqual(res['msg'], 'ok') # 2.3 check response = self.fetch(url) res = json.loads(response.body) self.assertEqual(res['status'], 0) self.assertEqual(res['msg'], 'ok') self.assertEqual(len(res['values']), 1) get_event = EventModel.from_dict(res['values'][0]) self.assertEqual(get_event, event_to_be_add)
def test_simplified_event(): """ event 的简化形式 :return: """ event_dict = { 'name': 'test' } event_model = EventModel.from_dict(event_dict) event_model.sanity_check() assert event_model.get_dict() == { 'app': '', 'name': 'test', 'visible_name': '', 'remark': '', 'source': [], 'type': '', 'version': '', 'properties': [], }, '最简单event,其他字段空值自动填补' event_dict = { 'name': 'test', 'properties': [{ 'name': 'property1', 'type': 'long' }], } event_model = EventModel.from_dict(event_dict) event_model.sanity_check() assert event_model.get_dict() == { 'app': '', 'name': 'test', 'visible_name': '', 'remark': '', 'source': [], 'type': '', 'version': '', 'properties': [{ 'name': 'property1', 'type': 'long', 'subtype': '', 'visible_name': '', 'remark': '', }], }, 'property最简单形式'
def test_delete_event(self): prepare_events(self.cust_dao) url = "/platform/event_models/event/{}/{}".format(SAMPLE_EVENT.app, SAMPLE_EVENT.name) response = self.fetch(url) res = json.loads(response.body) self.assertEqual(res['status'], 0) self.assertEqual(res['msg'], 'ok') self.assertEqual(len(res['values']), 1) self.assertEqual(EventModel.from_dict(res['values'][0]), SAMPLE_EVENT) # delete url = "/platform/event_models/event/{}/{}".format(SAMPLE_EVENT.app, SAMPLE_EVENT.name) response = self.fetch(url, method='DELETE') res = json.loads(response.body) self.assertEqual(res['status'], 0) self.assertEqual(res['msg'], 'ok') # check response = self.fetch(url) res = json.loads(response.body) self.assertEqual(res['status'], 404) # can not delete default clear_events(self.cust_dao) prepare_events(self.default_dao) url = "/platform/event_models/event/{}/{}".format(SAMPLE_EVENT.app, SAMPLE_EVENT.name) response = self.fetch(url, method='DELETE') res = json.loads(response.body) self.assertEqual(res['status'], 0) self.assertEqual(res['msg'], 'ok') response = self.fetch(url) res = json.loads(response.body) self.assertEqual(res['status'], 0)
def test_get_events(self): prepare_events(self.cust_dao) # default 0, cust 3 url = "/default/event_models" response = self.fetch(url) res = json.loads(response.body) self.assertEqual(self.default_dao.count(), 0) self.assertEqual(res['status'], 0) self.assertEqual(res['msg'], 'ok') self.assertEqual(len(res['values']), 0) url = '/platform/event_models' response = self.fetch(url) res = json.loads(response.body) self.assertEqual(self.cust_dao.count(), 3) self.assertEqual(res['status'], 0) self.assertEqual(res['msg'], 'ok') self.assertEqual(len(res['values']), 3) return_events = res['values'] return_event = [_ for _ in return_events if _['name'] == SAMPLE_EVENT.name][0] self.assertEqual(EventModel.from_dict(return_event), SAMPLE_EVENT) # check get by app response = self.fetch(url + '?app=new_app') res = json.loads(response.body) self.assertEqual(len(res['values']), 1) # check get by type response = self.fetch(url + '?type=new_type') res = json.loads(response.body) self.assertEqual(len(res['values']), 1)
def test_invalid_event(): """ 各种event的非法形式 :return: """ # invalid event name event_dict = { 'name': '' } event_model = EventModel.from_dict(event_dict) with raises(RuntimeError): event_model.sanity_check() # invalid property name event_dict = { 'name': 'test', 'properties': [{ 'name': '', 'type': 'long' }], } with raises(RuntimeError): event_model = EventModel.from_dict(event_dict) event_model.sanity_check() # invalid property type event_dict = { 'name': 'test', 'properties': [{ 'name': 'property1', 'type': 'ttt' }], } with raises(RuntimeError): event_model = EventModel.from_dict(event_dict) event_model.sanity_check() # invalid property data structure event_dict = { 'name': 'test', 'properties': [[1, 2]], } with raises(RuntimeError): event_model = EventModel.from_dict(event_dict) event_model.sanity_check()
def load_config(): print("loading config") for _ in get_file_json_content('data/event_model_default.json'): add_event_to_registry(EventModel.from_dict(_)) for _ in get_file_json_content('data/common_variable_default.json'): add_variable_to_registry(VariableModel.from_dict(_)) for _ in get_file_json_content('data/realtime_variable_default.json'): add_variable_to_registry(VariableModel.from_dict(_)) for _ in get_file_json_content('data/profile_variable_default.json'): add_variable_to_registry(VariableModel.from_dict(_))
def test_get_event(self): prepare_events(self.event_dao) url = "/default/event_models/event/{}/{}".format(SAMPLE_EVENT.app, SAMPLE_EVENT.name) response = self.fetch(url) res = json.loads(response.body) self.assertEqual(res['status'], 0) self.assertEqual(res['msg'], 'ok') self.assertEqual(len(res['values']), 1) self.assertEqual(EventModel.from_dict(res['values'][0]), SAMPLE_EVENT) # get not exist url = "/default/event_models/event/{}/{}".format(SAMPLE_EVENT.app, 'not_exist') response = self.fetch(url) res = json.loads(response.body) self.assertEqual(res['status'], 404)
def post(self, app, name): """ 修改或新增一个event @API summary: 修改或新增一个event notes: 根据名称修改一个event tags: - default parameters: - name: app in: path required: true type: string description: event的app - name: name in: path required: true type: string description: event的名称 - name: event in: body required: true type: json description: event的json表示 produces: - application/json """ self.set_header('content-type', 'application/json') body = self.request.body try: new_model = EventModel.from_json(body) except Exception as err: return self.process_error(400, str(err)) try: EventModelDefaultDao().add_model(new_model) self.finish(json_dumps({"status": 0, "msg": "ok", "values": []})) except Exception as err: logger.error(err) self.process_error(500, str(err))
def post(self): """ 增加一组event @API summary: 增加一组event notes: 增加一组新的event,如果event已存在,则替代 tags: - platform parameters: - name: events in: body required: true type: json description: event表示,list结构 produces: - application/json """ self.set_header('content-type', 'application/json') body = self.request.body try: events = list() for _ in json.loads(body): event = EventModel.from_dict(_) add_event_to_registry(event) events.append(event) except Exception as err: print_exc() return self.process_error(400, str(err)) try: dao = EventModelCustDao() for event in events: dao.add_model(event) self.finish(json_dumps({"status": 0, "msg": "ok", "values": []})) except Exception as err: print_exc() return self.process_error(500, str(err))
def test_valid_event(): event_dict = { 'app': 'nebula', 'name': 'test', 'visible_name': 'test_event', 'remark': 'test_event', 'source': [], 'type': '', 'version': '1.0', 'properties': [{ 'name': 'property1', 'type': 'long', 'subtype': '', 'visible_name': 'property 1', 'remark': 'property 1', } ], } event_model = EventModel.from_dict(event_dict) event_model.sanity_check() assert event_dict == event_model.get_dict()
def test_registry(): assert ('nebula', 'test') not in get_all_events_in_dict() event_dict = { 'app': 'nebula', 'name': 'test', 'visible_name': 'test_event', 'remark': 'test_event', 'type': '', 'version': '1.0', 'properties': [{ 'name': 'property1', 'type': 'long', 'subtype': '', 'visible_name': 'property 1', 'remark': 'property 1', } ], } event_model = EventModel.from_dict(event_dict) event_model.sanity_check() add_event_to_registry(event_model) assert ('nebula', 'test') in get_all_events_in_dict() update_events_to_registry([]) assert ('nebula', 'test') not in get_all_events_in_dict() add_events_to_registry([event_model]) assert ('nebula', 'test') in get_all_events_in_dict() update_events_to_registry([]) assert ('nebula', 'test') not in get_all_events_in_dict() update_events_to_registry([event_model]) assert ('nebula', 'test') in get_all_events_in_dict() assert get_all_events_in_dict()[('nebula', 'test')] == event_model assert get_event_from_registry('nebula', 'test') assert get_all_events()[0] == event_model
def add_event_to_platform(self, body): """ 1,新增基础事件,参数同/platform/event_models 的post接口一样 :param body: :return: """ try: events = list() for _ in json.loads(body): event = EventModel.from_dict(_) add_event_to_registry(event) events.append(event) except: logger.error(traceback.format_exc()) return False try: dao = EventModelCustDao() for event in events: dao.add_model(event) return True except Exception as err: logger.error(traceback.format_exc()) return self.process_error(500, str(err))
def post(self): """ 美化一组event @API summary: 美化一组event notes: 美化一组event tags: - platform parameters: - name: event in: body required: true type: json description: 一组event的json表示 produces: - application/json """ self.set_header('content-type', 'application/json') body = self.request.body try: events = list() for _ in json.loads(body): event = EventModel.from_dict(_) events.append(event) events = [_.get_simplified_ordered_dict() for _ in events] self.finish( json_dumps({ "status": 0, "msg": "ok", "values": events })) except Exception as err: print_exc() return self.process_error(400, str(err))
#!/usr/bin/env python # -*- coding: utf-8 -*- import json from nebula.dao.event_model_dao import EventModelDefaultDao from nebula.dao.event_model_dao import EventModelCustDao from nebula_meta.event_model import EventModel, add_event_to_registry from base import WebTestCase from .unittest_util import TestClassDBUtil, db_env with open('nebula/tests/data/event_model.json') as json_file: SAMPLE_EVENTS = json.load(json_file) new_events = list() for _ in SAMPLE_EVENTS: new_event = EventModel.from_dict(_) add_event_to_registry(new_event) new_events.append(new_event) SAMPLE_EVENTS = new_events SAMPLE_EVENT = SAMPLE_EVENTS[0] def prepare_events(event_dao): event_dao.add_model(SAMPLE_EVENT) # add with new app new_event = SAMPLE_EVENT.copy() new_event.app = 'new_app' new_event.name = 'event1' event_dao.add_model(new_event)
def to_eventmodel(self): result = EventModel(self.app, self.name, self.visible_name, self.type, self.remark, json.loads(self.source), self.version, json.loads(self.properties)) add_event_to_registry(result) return result