Skip to content

Commit

Permalink
Sitemap updates not propagated to listeners (#327)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimtng authored Sep 8, 2024
1 parent 96cd4d1 commit 741f18f
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 8 deletions.
42 changes: 34 additions & 8 deletions lib/openhab/core/sitemaps/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ module Sitemaps
# Provides sitemaps created in Ruby to openHAB
#
class Provider < Core::Provider
PREFIX = "jruby_"
SUFFIX = ".sitemap"
private_constant :PREFIX, :SUFFIX
private_constant :SUFFIX

class << self
# @!visibility private
Expand Down Expand Up @@ -49,6 +48,7 @@ def removeModelChangeListener(listener)

# @!visibility private
def unregister
clear
@registration.unregister
end

Expand Down Expand Up @@ -150,13 +150,30 @@ def clear
end
end

#
# Notify listeners about updated sitemap
#
# @param [String, org.openhab.core.model.sitemap.sitemap.Sitemap] sitemap The sitemap to update.
# @return [void]
#
def update(sitemap)
if sitemap.respond_to?(:to_str)
sitemap = get(sitemap).tap do |obj|
raise ArgumentError, "Sitemap #{sitemap} not found" unless obj
end
end
super
end

#
# Remove a sitemap.
#
# @param [String] sitemap_name
# @param [String, org.openhab.core.model.sitemap.sitemap.Sitemap] sitemap
# @return [Boolean] If a sitemap was removed
def remove(sitemap_name)
super("#{PREFIX}#{sitemap_name}#{SUFFIX}")
#
def remove(sitemap)
sitemap = sitemap.uid if sitemap.respond_to?(:uid)
super
end

private
Expand All @@ -169,15 +186,24 @@ def initialize
end

def notify_listeners_about_added_element(element)
@listeners.each { |l| l.model_changed(element.name, org.openhab.core.model.core.EventType::ADDED) }
model_name = "#{element.name}#{SUFFIX}"
@listeners.each do |l|
l.modelChanged(model_name, org.openhab.core.model.core.EventType::ADDED)
# Ensure that when a script is reloaded, the sitemap is updated.
# This is because our listener, org.openhab.core.io.rest.sitemap.SitemapSubscriptionService
# only handles MODIFIED events in its modelChanged() method.
l.modelChanged(model_name, org.openhab.core.model.core.EventType::MODIFIED)
end
end

def notify_listeners_about_removed_element(element)
@listeners.each { |l| l.model_changed(element.name, org.openhab.core.model.core.EventType::REMOVED) }
model_name = "#{element.name}#{SUFFIX}"
@listeners.each { |l| l.modelChanged(model_name, org.openhab.core.model.core.EventType::REMOVED) }
end

def notify_listeners_about_updated_element(_old_element, element)
@listeners.each { |l| l.model_changed(element.name, org.openhab.core.model.core.EventType::MODIFIED) }
model_name = "#{element.name}#{SUFFIX}"
@listeners.each { |l| l.modelChanged(model_name, org.openhab.core.model.core.EventType::MODIFIED) }
end
end
end
Expand Down
60 changes: 60 additions & 0 deletions spec/openhab/core/sitemaps/provider_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# frozen_string_literal: true

RSpec.describe OpenHAB::Core::Sitemaps::Provider do
let(:model_listener) { org.openhab.core.model.core.ModelRepositoryChangeListener.impl { nil } }
let(:provider) { described_class.instance }

before { provider.addModelChangeListener(model_listener) }
after { provider.removeModelChangeListener(model_listener) }

describe "#add" do
it "notifies listeners with the correct name" do
allow(model_listener).to receive(:modelChanged)

sitemaps.build do
sitemap "test"
end

expect(model_listener).to have_received(:modelChanged)
.with("test.sitemap", org.openhab.core.model.core.EventType::ADDED)

expect(model_listener).to have_received(:modelChanged)
.with("test.sitemap", org.openhab.core.model.core.EventType::MODIFIED)
ensure
sitemaps.remove("test")
end
end

describe "#update" do
it "notifies listeners with the correct name" do
sitemaps.build do
sitemap "test"
end

expect(model_listener).not_to receive(:modelChanged)
.with("test.sitemap", org.openhab.core.model.core.EventType::ADDED)

expect(model_listener).to receive(:modelChanged)
.with("test.sitemap", org.openhab.core.model.core.EventType::MODIFIED)

sitemaps.build(update: true) do
sitemap "test"
end
ensure
sitemaps.remove("test")
end
end

describe "#remove" do
it "notifies listeners with the correct name" do
sitemaps.build do
sitemap "test"
end

expect(model_listener).to receive(:modelChanged)
.with("test.sitemap", org.openhab.core.model.core.EventType::REMOVED)

provider.remove("test")
end
end
end

0 comments on commit 741f18f

Please sign in to comment.