Ejemplo n.º 1
0
    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))
Ejemplo n.º 2
0
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()
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
    def test_get_events(self):
        prepare_events(self.event_dao)

        url = "/default/event_models"
        response = self.fetch(url)
        res = json.loads(response.body)
        self.assertEqual(self.event_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)
Ejemplo n.º 5
0
 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))
Ejemplo n.º 6
0
    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))
Ejemplo n.º 7
0
    def test_modify_events(self):
        prepare_events(self.cust_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 = "/platform/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 = "/platform/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 = "/platform/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 = "/platform/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)

        # 3. modify only affect cust table
        clear_events(self.default_dao)
        clear_events(self.cust_dao)
        prepare_events(self.default_dao)

        modified_event = SAMPLE_EVENT.copy()
        modified_event.remark = 'modified'
        response = self.fetch(url, method='POST', body='[%s]' % modified_event.get_json())
        res = json.loads(response.body)
        self.assertEqual(res['status'], 0)
        self.assertEqual(res['msg'], 'ok')

        # 4. default keeps the same, cust is updated
        self.assertEqual(self.cust_dao.get_model_by_app_name(SAMPLE_EVENT.app, SAMPLE_EVENT.name), modified_event)
        self.assertNotEqual(self.default_dao.get_model_by_app_name(SAMPLE_EVENT.app, SAMPLE_EVENT.name), modified_event)
Ejemplo n.º 8
0
#!/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)