Commit f8eb4299 by John Doe

added tests for node adapter factory

parent e31a3af2
module Joule module Joule
class Adapter 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 def node_type
'joule' 'joule'
end end
......
# frozen_string_literal: true # frozen_string_literal: true
module Joule
# Handles construction of database objects # Handles construction of database objects
class UpdateJouleModules class UpdateJouleModules
include ServiceStatus include ServiceStatus
def initialize(nilm) def initialize(nilm)
...@@ -10,7 +10,7 @@ class UpdateJouleModules ...@@ -10,7 +10,7 @@ class UpdateJouleModules
end end
def run(module_info) def run(module_info)
#module_info as returned by JouleAdapter #module_info as returned by JouleBackend
if module_info.nil? if module_info.nil?
add_error("unable to retrieve module information") add_error("unable to retrieve module information")
return self return self
...@@ -51,4 +51,5 @@ class UpdateJouleModules ...@@ -51,4 +51,5 @@ class UpdateJouleModules
end end
dbStream dbStream
end end
end
end end
...@@ -12,7 +12,7 @@ module Nilmdb ...@@ -12,7 +12,7 @@ module Nilmdb
def run(dbinfo, schema) def run(dbinfo, schema)
# check to make sure dbinfo and schema are set # check to make sure dbinfo and schema are set
# if either is nil, the database is not available # 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}") add_error("cannot contact database at #{@db.url}")
@db.update_attributes(available: false) @db.update_attributes(available: false)
return self return self
......
class NodeAdapterFactory class NodeAdapterFactory
include HTTParty include HTTParty
default_timeout 5
open_timeout 5
read_timeout 5
def self.from_url(url) def self.from_url(url)
begin begin
resp = self.class.get(url) resp = get(url)
return nil unless resp.success? return nil unless resp.success?
info = resp.parsed_response info = resp.parsed_response
rescue rescue
return nil return nil
end end
if info.include? 'NilmDB' if info.include? 'NilmDB'
return Nilmdb::Adapter(url) return Nilmdb::Adapter.new(url)
elsif info.include? 'Joule' elsif info.include? 'Joule'
return Joule::Adapter(url) return Joule::Adapter.new(url)
else else
return nil return nil
end end
end end
def self.from_nilm(nilm) def self.from_nilm(nilm)
if nilm.type=='nilmdb' if nilm.node_type=='nilmdb'
return Nilmdb::Adapter(nilm.url) return Nilmdb::Adapter.new(nilm.url)
elsif nilm.type=='joule' elsif nilm.node_type=='joule'
return Joule::Adapter(nilm.url) return Joule::Adapter.new(nilm.url)
else else
# try to figure out what this nilm is # try to figure out what this nilm is
return self.from_url(nilm.url) return self.from_url(nilm.url)
......
...@@ -4,6 +4,7 @@ json.data do ...@@ -4,6 +4,7 @@ json.data do
json.role @role json.role @role
if @nilm.db != nil if @nilm.db != nil
json.max_points_per_plot @nilm.db.max_points_per_plot json.max_points_per_plot @nilm.db.max_points_per_plot
json.available @nilm.db.available
json.root_folder do json.root_folder do
if @nilm.db.root_folder != nil if @nilm.db.root_folder != nil
json.partial! 'db_folders/db_folder', json.partial! 'db_folders/db_folder',
...@@ -14,6 +15,7 @@ json.data do ...@@ -14,6 +15,7 @@ json.data do
end end
json.jouleModules(@nilm.joule_modules) do |m| json.jouleModules(@nilm.joule_modules) do |m|
json.extract! m, *JouleModule.json_keys json.extract! m, *JouleModule.json_keys
json.url Rails.configuration.interface_url_template.call(m.joule_id)
json.nilm_id @nilm.id json.nilm_id @nilm.id
end end
end end
......
...@@ -95,4 +95,6 @@ Rails.application.configure do ...@@ -95,4 +95,6 @@ Rails.application.configure do
# Do not dump schema after migrations. # Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false config.active_record.dump_schema_after_migration = false
end end
...@@ -39,4 +39,10 @@ Rails.application.configure do ...@@ -39,4 +39,10 @@ Rails.application.configure do
# Raises error for missing translations # Raises error for missing translations
# config.action_view.raise_on_missing_translations = true # 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 end
...@@ -13,7 +13,7 @@ describe 'UpdateJouleModules' do ...@@ -13,7 +13,7 @@ describe 'UpdateJouleModules' do
outputs={o1: '/path/2'}) outputs={o1: '/path/2'})
backend.add_module("new2",inputs={i1: '/path/3',i2: '/path/4'}, backend.add_module("new2",inputs={i1: '/path/3',i2: '/path/4'},
outputs={o1: '/path/5',o2: '/path/5'}) outputs={o1: '/path/5',o2: '/path/5'})
service = UpdateJouleModules.new(nilm) service = Joule::UpdateModules.new(nilm)
service.run(backend.module_info) service.run(backend.module_info)
expect(service.success?).to be true expect(service.success?).to be true
# new modules are in the database # new modules are in the database
...@@ -33,7 +33,7 @@ describe 'UpdateJouleModules' do ...@@ -33,7 +33,7 @@ describe 'UpdateJouleModules' do
nilm = create(:nilm) nilm = create(:nilm)
backend = MockJouleAdapter.new backend = MockJouleAdapter.new
backend.add_module("module",outputs={output: '/missing/path'}) backend.add_module("module",outputs={output: '/missing/path'})
service = UpdateJouleModules.new(nilm) service = Joule::UpdateModules.new(nilm)
service.run(backend.module_info) service.run(backend.module_info)
expect(service.warnings?).to be true expect(service.warnings?).to be true
end end
...@@ -44,21 +44,8 @@ describe 'UpdateJouleModules' do ...@@ -44,21 +44,8 @@ describe 'UpdateJouleModules' do
backend = MockJouleAdapter.new backend = MockJouleAdapter.new
backend.add_module("module",inputs={input: '/matched/path1'}, backend.add_module("module",inputs={input: '/matched/path1'},
outputs={output: '/matched/path2'}) outputs={output: '/matched/path2'})
service = UpdateJouleModules.new(nilm) service = Joule::UpdateModules.new(nilm)
service.run(backend.module_info) service.run(backend.module_info)
expect(service.warnings?).to be false expect(service.warnings?).to be false
end 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 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 ...@@ -132,7 +132,7 @@ RSpec.describe NilmsController, type: :request do
headers: john.create_new_auth_token headers: john.create_new_auth_token
body = JSON.parse(response.body) body = JSON.parse(response.body)
expect(body['data']['jouleModules'][0]['name']).to eq(test_module.name) 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 end
it 'refreshes nilm data when requested' do it 'refreshes nilm data when requested' do
@auth_headers = john.create_new_auth_token @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