Commit f8eb4299 by John Doe

added tests for node adapter factory

parent e31a3af2
module Joule
class Adapter
def initialize(url)
@backend = Backend.new(url)
end
def refresh(nilm)
db_service = UpdateDb.new(db: nilm.db)
result = StubService.new
result.absorb_status(db_service.run(@backend.dbinfo, @backend.schema))
module_service = UpdateModules.new(nilm)
result.absorb_status(module_service.run(@backend.module_info))
result
end
def refresh_stream(db_stream)
data = @backend.stream_info(db_stream)
service = UpdateStream.new(db_stream, data)
service.run
end
def save_stream(db_stream)
@backend.update_stream(db_stream)
end
def save_folder(db_folder)
@backend.update_folder(db_folder)
end
def load_data(db_stream, start_time, end_time, elements=[], resolution=nil)
data_service = LoadStreamData.new(@backend)
data_service.run(db_stream, start_time, end_time, elements, resolution)
unless data_service.success?
return nil
end
{
data: data_service.data,
decimation_factor: data_service.decimation_factor
}
end
def node_type
'joule'
end
......
# frozen_string_literal: true
module Joule
# Handles construction of database objects
class UpdateJouleModules
include ServiceStatus
# Handles construction of database objects
class UpdateJouleModules
include ServiceStatus
def initialize(nilm)
super()
@nilm = nilm
end
def initialize(nilm)
super()
@nilm = nilm
end
def run(module_info)
#module_info as returned by JouleBackend
if module_info.nil?
add_error("unable to retrieve module information")
return self
end
#remove the previous modules
@nilm.joule_modules.destroy_all
module_info.each do |info|
@nilm.joule_modules << _build_module(info)
end
def run(module_info)
#module_info as returned by JouleAdapter
if module_info.nil?
add_error("unable to retrieve module information")
return self
set_notice("refreshed modules")
self
end
def _build_module(info)
# create JouleModule and associated pipes from
# hash returned by the JouleAdapter.module_info
params = info.extract!(*JouleModule.joule_keys)
m = JouleModule.new(params)
# link inputs to database streams
info[:input_paths].each do |name, path|
m.joule_pipes << JoulePipe.new(direction: 'input',
name: name,
db_stream: _retrieve_stream(path))
end
#remove the previous modules
@nilm.joule_modules.destroy_all
module_info.each do |info|
@nilm.joule_modules << _build_module(info)
info[:output_paths].each do |name, path|
m.joule_pipes << JoulePipe.new(direction: 'output',
name: name,
db_stream: _retrieve_stream(path))
end
set_notice("refreshed modules")
self
end
def _build_module(info)
# create JouleModule and associated pipes from
# hash returned by the JouleAdapter.module_info
params = info.extract!(*JouleModule.joule_keys)
m = JouleModule.new(params)
# link inputs to database streams
info[:input_paths].each do |name, path|
m.joule_pipes << JoulePipe.new(direction: 'input',
name: name,
db_stream: _retrieve_stream(path))
end
info[:output_paths].each do |name, path|
m.joule_pipes << JoulePipe.new(direction: 'output',
name: name,
db_stream: _retrieve_stream(path))
return m
end
return m
end
def _retrieve_stream(path)
dbStream = @nilm.db.db_streams.find_by_path(path)
if dbStream.nil?
add_warning("[#{path}] not in database")
def _retrieve_stream(path)
dbStream = @nilm.db.db_streams.find_by_path(path)
if dbStream.nil?
add_warning("[#{path}] not in database")
end
dbStream
end
dbStream
end
end
......@@ -12,7 +12,7 @@ module Nilmdb
def run(dbinfo, schema)
# check to make sure dbinfo and schema are set
# if either is nil, the database is not available
if(dbinfo.nil? || schema.nil?)
if dbinfo.nil? || schema.nil?
add_error("cannot contact database at #{@db.url}")
@db.update_attributes(available: false)
return self
......
class NodeAdapterFactory
include HTTParty
default_timeout 5
open_timeout 5
read_timeout 5
def self.from_url(url)
begin
resp = self.class.get(url)
resp = get(url)
return nil unless resp.success?
info = resp.parsed_response
rescue
return nil
end
if info.include? 'NilmDB'
return Nilmdb::Adapter(url)
return Nilmdb::Adapter.new(url)
elsif info.include? 'Joule'
return Joule::Adapter(url)
return Joule::Adapter.new(url)
else
return nil
end
end
def self.from_nilm(nilm)
if nilm.type=='nilmdb'
return Nilmdb::Adapter(nilm.url)
elsif nilm.type=='joule'
return Joule::Adapter(nilm.url)
if nilm.node_type=='nilmdb'
return Nilmdb::Adapter.new(nilm.url)
elsif nilm.node_type=='joule'
return Joule::Adapter.new(nilm.url)
else
# try to figure out what this nilm is
return self.from_url(nilm.url)
......
......@@ -4,6 +4,7 @@ json.data do
json.role @role
if @nilm.db != nil
json.max_points_per_plot @nilm.db.max_points_per_plot
json.available @nilm.db.available
json.root_folder do
if @nilm.db.root_folder != nil
json.partial! 'db_folders/db_folder',
......@@ -14,6 +15,7 @@ json.data do
end
json.jouleModules(@nilm.joule_modules) do |m|
json.extract! m, *JouleModule.json_keys
json.url Rails.configuration.interface_url_template.call(m.joule_id)
json.nilm_id @nilm.id
end
end
......
......@@ -95,4 +95,6 @@ Rails.application.configure do
# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false
end
......@@ -39,4 +39,10 @@ Rails.application.configure do
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
# set up interface subdomain
config.interface_url_template = lambda do |id|
return "http://#{id}.interfaces.wattsworth.local"
end
end
......@@ -13,7 +13,7 @@ describe 'UpdateJouleModules' do
outputs={o1: '/path/2'})
backend.add_module("new2",inputs={i1: '/path/3',i2: '/path/4'},
outputs={o1: '/path/5',o2: '/path/5'})
service = UpdateJouleModules.new(nilm)
service = Joule::UpdateModules.new(nilm)
service.run(backend.module_info)
expect(service.success?).to be true
# new modules are in the database
......@@ -33,7 +33,7 @@ describe 'UpdateJouleModules' do
nilm = create(:nilm)
backend = MockJouleAdapter.new
backend.add_module("module",outputs={output: '/missing/path'})
service = UpdateJouleModules.new(nilm)
service = Joule::UpdateModules.new(nilm)
service.run(backend.module_info)
expect(service.warnings?).to be true
end
......@@ -44,21 +44,8 @@ describe 'UpdateJouleModules' do
backend = MockJouleAdapter.new
backend.add_module("module",inputs={input: '/matched/path1'},
outputs={output: '/matched/path2'})
service = UpdateJouleModules.new(nilm)
service = Joule::UpdateModules.new(nilm)
service.run(backend.module_info)
expect(service.warnings?).to be false
end
it 'returns error if Joule server is unavailable' do
nilm = Nilm.create(name: 'test', url: 'invalid')
mock_service = instance_double(UpdateDb,
run: StubService.new)
allow(UpdateDb).to receive(:new)
.and_return(mock_service)
service = UpdateNilm.new()
service.run(nilm)
expect(service.success?).to be false
expect(mock_service).to_not have_received(:run)
end
end
# frozen_string_literal: true
require 'rails_helper'
describe NodeAdapterFactory do
# use the NUC office server
let (:nilmdb_url) {'http://nuc/nilmdb'}
let (:joule_url) {'http://nuc:8088'}
it 'creates_adapter_from_url', :vcr do
adapter = NodeAdapterFactory.from_url(nilmdb_url)
expect(adapter.node_type).to eq('nilmdb')
adapter = NodeAdapterFactory.from_url(joule_url)
expect(adapter.node_type).to eq('joule')
end
it 'returns nil with invalid url', :vcr do
%w(http://www.google.com invalid_url).each do |url|
expect(NodeAdapterFactory.from_url(url)).to be_nil
end
end
it 'creates_adapter_from_nilm' do
nilm = create(:nilm, node_type: 'nilmdb')
adapter = NodeAdapterFactory.from_nilm(nilm)
expect(adapter.node_type).to eq('nilmdb')
nilm = create(:nilm, node_type: 'joule')
adapter = NodeAdapterFactory.from_nilm(nilm)
expect(adapter.node_type).to eq('joule')
end
it 'falls_back_to_url_when_node_type_is_invalid', :vcr do
nilm = create(:nilm, url: joule_url)
nilm.node_type='invalid'
adapter = NodeAdapterFactory.from_nilm(nilm)
expect(adapter.node_type).to eq('joule')
end
end
\ No newline at end of file
......@@ -132,7 +132,7 @@ RSpec.describe NilmsController, type: :request do
headers: john.create_new_auth_token
body = JSON.parse(response.body)
expect(body['data']['jouleModules'][0]['name']).to eq(test_module.name)
expect(body['data']['jouleModules'][0]['url']).to start_with("http://#{test_module.id}.modules")
expect(body['data']['jouleModules'][0]['url']).to start_with("http://#{test_module.joule_id}.interfaces")
end
it 'refreshes nilm data when requested' do
@auth_headers = john.create_new_auth_token
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment