Skip to content

Commit

Permalink
Merge pull request #142 from kevinuehara/test
Browse files Browse the repository at this point in the history
Unity tests of logger and devices
  • Loading branch information
kevinuehara committed Sep 19, 2019
2 parents d17b78b + cfd2f48 commit 78a24ac
Show file tree
Hide file tree
Showing 25 changed files with 1,648 additions and 245 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
.vscode/
.idea/
docs/build/*
*egg-info
*egg-info
.coverage
htmlcov/
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,22 @@ python:
cache:
directories:
- node_modules
install:
- pip install -r requirements/requirements.txt
- pip install -r tests/requirements.txt
- pip install codecov
env:
global:
- DEV_MNGR_CRYPTO_PASS="kamehameHA"
- DEV_MNGR_CRYPTO_IV=1234567890123456
- DEV_MNGR_CRYPTO_SALT="shuriken"
script:
- 'docker build -t dojot/device-manager .'
- 'docker build -t dredd/test -f tests/Dockerfile .'
- 'docker-compose -p test -f tests/docker-compose.yaml up -d kafka data-broker postgres device-manager postgres-users'
- 'docker-compose -p test -f tests/docker-compose.yaml run --rm test-runner'
- python3 -m pytest --cov-report=html --cov=DeviceManager tests/
after_success:
- codecov
- travis/publish.sh
- travis/deploy-gh-pages.sh
35 changes: 29 additions & 6 deletions DeviceManager/BackendHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import traceback
import requests
from DeviceManager.utils import HTTPRequestError
from DeviceManager.KafkaNotifier import send_notification, DeviceEvent
from DeviceManager.KafkaNotifier import KafkaNotifier, DeviceEvent
import logging
from datetime import datetime
import time
Expand Down Expand Up @@ -53,38 +53,61 @@ def update(self, device):
"""
raise NotImplementedError('Abstract method called')


class KafkaHandler:

def __init__(self):
pass
self.kafkaNotifier = KafkaNotifier()

def create(self, device, meta):
"""
Publishes event to kafka broker, notifying device creation
"""

LOGGER.info(f" Publishing create event to Kafka")
send_notification(DeviceEvent.CREATE, device, meta)
self.kafkaNotifier.send_notification(DeviceEvent.CREATE, device, meta)

def remove(self, device, meta):
"""
Publishes event to kafka broker, notifying device removal
"""

LOGGER.info(f" Publishing remove event to Kafka")
send_notification(DeviceEvent.REMOVE, device, meta)
self.kafkaNotifier.send_notification(DeviceEvent.REMOVE, device, meta)

def update(self, device, meta):
"""
Publishes event to kafka broker, notifying device update
"""

LOGGER.info(f" Publishing create update to Kafka")
send_notification(DeviceEvent.UPDATE, device, meta)
self.kafkaNotifier.send_notification(DeviceEvent.UPDATE, device, meta)

def configure(self, device, meta):
"""
Publishes event to kafka broker, notifying device configuration
"""
LOGGER.info(f" Publishing configure event to Kafka")
send_notification(DeviceEvent.CONFIGURE, device, meta)
self.kafkaNotifier.send_notification(DeviceEvent.CONFIGURE, device, meta)

class KafkaInstanceHandler:

kafkaNotifier = None

def __init__(self):
pass

def getInstance(self, kafka_instance):
"""
Instantiates a connection with Kafka, was created because
previously the connection was being created in KafkaNotifier
once time every import.
:param kafka_instance: An instance of KafkaHandler.
:return An instance of KafkaHandler used to notify
"""

if kafka_instance is None:
self.kafkaNotifier = KafkaHandler()

return self.kafkaNotifier

0 comments on commit 78a24ac

Please sign in to comment.