def test_duplicated_thing_names(servient): """A Servient rejects Things with duplicated IDs.""" description_01 = { "@context": [WOT_TD_CONTEXT_URL], "id": uuid.uuid4().urn, "title": Faker().sentence() } description_02 = { "@context": [WOT_TD_CONTEXT_URL], "id": uuid.uuid4().urn, "title": Faker().sentence() } description_03 = { "@context": [WOT_TD_CONTEXT_URL], "id": description_01.get("id"), "title": Faker().sentence() } description_01_str = json.dumps(description_01) description_02_str = json.dumps(description_02) description_03_str = json.dumps(description_03) wot = WoT(servient=servient) wot.produce(description_01_str) wot.produce(description_02_str) with pytest.raises(ValueError): wot.produce(description_03_str)
def test_discovery_method_local(): """All TDs contained in the Servient are returned when using the local discovery method without defining the fragment nor the query fields.""" servient = Servient(dnssd_enabled=False) wot = WoT(servient=servient) wot.produce(ThingFragment(TD_DICT_01)) wot.produce(ThingFragment(TD_DICT_02)) future_done, found = tornado.concurrent.Future(), [] def resolve(): len(found) == 2 and not future_done.done() and future_done.set_result( True) @tornado.gen.coroutine def test_coroutine(): thing_filter = ThingFilterDict(method=DiscoveryMethod.LOCAL) observable = wot.discover(thing_filter) subscription = observable.subscribe( on_next=lambda td_str: found.append(ThingDescription(td_str) ) or resolve()) yield future_done assert_equal_td_sequences(found, [TD_DICT_01, TD_DICT_02]) subscription.dispose() run_test_coroutine(test_coroutine)
def test_discovery_fragment(): """The Thing filter fragment attribute enables discovering Things by matching TD fields.""" servient = Servient(dnssd_enabled=False) wot = WoT(servient=servient) wot.produce(ThingFragment(TD_DICT_01)) wot.produce(ThingFragment(TD_DICT_02)) def first(thing_filter): """Returns the first TD discovery for the given Thing filter.""" future_done, found = tornado.concurrent.Future(), [] def resolve(): not future_done.done() and future_done.set_result(True) @tornado.gen.coroutine def discover_first(): observable = wot.discover(thing_filter) subscription = observable.subscribe( on_next=lambda td_str: found.append(ThingDescription(td_str) ) or resolve()) yield future_done subscription.dispose() assert len(found) raise tornado.gen.Return(found[0]) return tornado.ioloop.IOLoop.current().run_sync( discover_first, timeout=TIMEOUT_DISCOVER) fragment_td_pairs = [({ "name": TD_DICT_01.get("name") }, TD_DICT_01), ({ "version": { "instance": "2.0.0" } }, TD_DICT_02), ({ "id": TD_DICT_02.get("id") }, TD_DICT_02), ({ "security": [{ "scheme": "psk" }] }, TD_DICT_01)] for fragment, td_expected in fragment_td_pairs: td_found = first( ThingFilterDict(method=DiscoveryMethod.LOCAL, fragment=fragment)) assert_equal_tds(td_found, td_expected)
def test_coroutine(): wot = WoT(servient=servient) td_01_str = json.dumps(TD_DICT_01) td_02_str = json.dumps(TD_DICT_02) wot.produce(td_01_str).expose() wot.produce(td_02_str) catalogue = yield fetch_catalogue(servient) assert len(catalogue) == 1 assert TD_DICT_01["id"] in catalogue
def test_coroutine(): wot = WoT(servient=servient) td_01_str = json.dumps(TD_DICT_01) td_02_str = json.dumps(TD_DICT_02) exposed_thing_01 = wot.produce(td_01_str) exposed_thing_02 = wot.produce(td_02_str) exposed_thing_01.expose() exposed_thing_02.expose() catalogue = yield fetch_catalogue(servient) assert len(catalogue) == 2 assert exposed_thing_01.thing.url_name in catalogue.get( TD_DICT_01["id"]) assert exposed_thing_02.thing.url_name in catalogue.get( TD_DICT_02["id"]) td_01_catalogue = yield fetch_catalogue_td(servient, TD_DICT_01["id"]) assert td_01_catalogue["id"] == TD_DICT_01["id"] assert td_01_catalogue["title"] == TD_DICT_01["title"] catalogue_expanded = yield fetch_catalogue(servient, expanded=True) num_props = len(TD_DICT_01.get("properties", {}).keys()) assert len(catalogue_expanded) == 2 assert TD_DICT_01["id"] in catalogue_expanded assert TD_DICT_02["id"] in catalogue_expanded assert len( catalogue_expanded[TD_DICT_01["id"]]["properties"]) == num_props
def test_produce_model_consumed_thing(): """Things can be produced from ConsumedThing instances.""" servient = Servient() wot = WoT(servient=servient) td_str = json.dumps(TD_EXAMPLE) consumed_thing = wot.consume(td_str) exposed_thing = wot.produce(consumed_thing) assert exposed_thing.id == consumed_thing.td.id assert exposed_thing.name == consumed_thing.td.name assert len(exposed_thing.properties) == len(consumed_thing.td.properties) assert len(exposed_thing.actions) == len(consumed_thing.td.actions) assert len(exposed_thing.events) == len(consumed_thing.td.events)
def test_produce_model_thing_template(): """Things can be produced from ThingTemplate instances.""" thing_id = Faker().url() thing_name = Faker().sentence() thing_template = ThingFragment({"id": thing_id, "name": thing_name}) servient = Servient() wot = WoT(servient=servient) exp_thing = wot.produce(thing_template) assert servient.get_exposed_thing(thing_id) assert exp_thing.id == thing_id assert exp_thing.name == thing_name
def test_produce_model_str(): """Things can be produced from TD documents serialized to JSON-LD string.""" td_str = json.dumps(TD_EXAMPLE) thing_id = TD_EXAMPLE.get("id") servient = Servient() wot = WoT(servient=servient) assert wot.servient is servient exp_thing = wot.produce(td_str) assert servient.get_exposed_thing(thing_id) assert exp_thing.thing.id == thing_id assert_exposed_thing_equal(exp_thing, TD_EXAMPLE)