def test_populate_dictionary_with_resource_containing_attributes(self):
        res_id = "/agreement/load_resource_w_attributes"
        res = core_model.Resource(res_id, None, None)
        res_attrs = {"the.test.attr": "1", "attr-2": "2", "attr_3": "3",
                     "attr||four": "4"}
        res.attributes = res_attrs

        resources = EntityDictionary(self.api.registry)
        resources._persist_resource(res_id, res)
        resources.populate_from_db()

        self.assertEqual(resources[res_id].attributes, res_attrs)
Exemple #2
0
    def test_delete_using_pop(self):
        res_id_1 = "/agreement/delete-resource-dict-using-clear_1"
        res_1 = core_model.Resource(res_id_1, None, None)

        resources = EntityDictionary(None)
        resources[res_id_1] = res_1

        resources.pop(res_id_1)

        db_results = self.db.entities.find({})

        self.assertEqual(len(resources), 0)
        self.assertEqual(db_results.count(), 0)
    def test_delete_using_pop(self):
        res_id_1 = "/agreement/delete-resource-dict-using-clear_1"
        res_1 = core_model.Resource(res_id_1, None, None)

        resources = EntityDictionary(None)
        resources[res_id_1] = res_1

        resources.pop(res_id_1)

        db_results = self.db.entities.find({})

        self.assertEqual(len(resources), 0)
        self.assertEqual(db_results.count(), 0)
Exemple #4
0
    def test_populate_dictionary_with_basic_link_from_DB(self):
        # Create resources and Link
        src_id = "124816"
        tar_id = "112358"
        src_res = core_model.Resource(src_id, occi_sla.AGREEMENT, None)
        tar_res = core_model.Resource(tar_id, occi_sla.AGREEMENT, None)

        lnk_id = "/agreement/load-link-entity-from-DB"
        lnk = core_model.Link(lnk_id, None, None, src_res, tar_res)

        links = EntityDictionary(self.api.registry)
        links._persist_resource(src_id, src_res)
        links._persist_resource(tar_id, tar_res)
        links._persist_link(lnk_id, lnk)

        links.populate_from_db()

        self.assertEqual(len(links), 3)
        self.assertTrue(isinstance(links[lnk_id], core_model.Link))
        self.assertEqual(links[lnk_id].identifier, lnk_id)
        # Removing assistive field templates for comparison
        temp_src = links[lnk_id].source.__dict__
        temp_trgt = links[lnk_id].target.__dict__
        temp_src.pop('templates')
        temp_trgt.pop('templates')
        self.assertEqual(temp_src, src_res.__dict__)
        self.assertEqual(temp_trgt, tar_res.__dict__)
Exemple #5
0
    def test_retrieve_dictionary_keys(self):
        res_id_1 = "/agreement/delete-resource-dict-using-clear_1"
        res_1 = core_model.Resource(res_id_1, None, None)

        res_id_2 = "/agreement/delete-resource-dict-using-clear_2"
        res_2 = core_model.Resource(res_id_2, None, None)

        resources = EntityDictionary(None)
        resources[res_id_1] = res_1
        resources[res_id_2] = res_2

        s1 = set(resources.keys())
        s2 = {res_id_1, res_id_2}

        self.assertEqual(s1, s2)
    def test_retrieve_dictionary_keys(self):
        res_id_1 = "/agreement/delete-resource-dict-using-clear_1"
        res_1 = core_model.Resource(res_id_1, None, None)

        res_id_2 = "/agreement/delete-resource-dict-using-clear_2"
        res_2 = core_model.Resource(res_id_2, None, None)

        resources = EntityDictionary(None)
        resources[res_id_1] = res_1
        resources[res_id_2] = res_2

        s1 = set(resources.keys())
        s2 = {res_id_1, res_id_2}

        self.assertEqual(s1, s2)
    def test_no_policy_creation(self):
        """
			Check that if no link (aka device id) exist, no policy is created
		"""
        gold = core_model.Mixin('', 'gold', [occi_sla.AGREEMENT_TEMPLATE])
        compute = core_model.Mixin('', 'compute',
                                   [occi_sla.AGREEMENT_TEMPLATE])
        availability = core_model.Mixin('', 'availability',
                                        [occi_sla.AGREEMENT_TEMPLATE])
        mixins = [gold, compute, availability]
        res = core_model.Resource(self.id, occi_sla.AGREEMENT, mixins)
        res.attributes = {
            'occi.agreement.state': 'accepted',
            'occi.agreement.effectiveFrom': '2014-11-02T02:20:26+00:00',
            'occi.agreement.effectiveUntil': '2015-11-02T02:20:27+00:00'
        }

        northbound_api = api.build()
        resources = EntityDictionary(northbound_api.registry)
        resources[self.id] = res

        resources.registry.populate_resources()

        myrulesengine = rulesengine.RulesEngine(resources.registry)
        myrulesengine.active_policies = {}
        myrulesengine.active_agreements = {}
        # myrulesengine.registry=registry

        myrulesengine.start_engine(0)

        temp_policy_record = DB.policies.find({'agreement_id': self.id},
                                              {'_id': 0})

        self.assertEqual(temp_policy_record.count(), 0)
    def test_past_agreement_non_detection(self):
        """
		   Check that the temp agreement with pending state is not detected as active agreement.
		"""

        gold = core_model.Mixin('', 'gold', [occi_sla.AGREEMENT_TEMPLATE])
        compute = core_model.Mixin('', 'compute',
                                   [occi_sla.AGREEMENT_TEMPLATE])
        availability = core_model.Mixin('', 'availability',
                                        [occi_sla.AGREEMENT_TEMPLATE])
        mixins = [gold, compute, availability]
        res = core_model.Resource(self.id, occi_sla.AGREEMENT, mixins)
        res.attributes = {
            'occi.agreement.state': 'accepted',
            'occi.agreement.effectiveFrom': '2014-10-02T02:20:26+00:00',
            'occi.agreement.effectiveUntil': '2014-11-02T02:20:27+00:00'
        }

        northbound_api = api.build()
        resources = EntityDictionary(northbound_api.registry)
        resources[self.id] = res
        resources.registry.populate_resources()

        active_agreements = resources.registry.get_active_agreement_resources()

        self.assertEqual(active_agreements.__len__(), 0)
    def test_load_resource_with_mixin_type_from_DB(self):
        """
            retrieve a resource from the db with the same mixins
        """
        res_id = "/agreement/load-resource-with-mixin-from-db"
        test_resource = core_model.Resource(res_id, occi_sla.AGREEMENT,
                                            [test_data.epc_mixin])
        resources = EntityDictionary(self.api.registry)

        resources._persist_resource(res_id, test_resource)

        resources.populate_from_db()

        res_mixins = resources[res_id].mixins
        self.assertEqual(len(res_mixins), 1)
        self.assertTrue(isinstance(res_mixins[0], core_model.Mixin))
Exemple #10
0
    def test_active_agreement_detection(self):
        """
		   Check that the temp agreement is detected as active agreement.
		"""

        gold = core_model.Mixin('', 'gold', [occi_sla.AGREEMENT_TEMPLATE])
        compute = core_model.Mixin('', 'compute',
                                   [occi_sla.AGREEMENT_TEMPLATE])
        availability = core_model.Mixin('', 'availability',
                                        [occi_sla.AGREEMENT_TEMPLATE])
        mixins = [gold, compute, availability]
        res = core_model.Resource(self.id, occi_sla.AGREEMENT, mixins)
        res.attributes = {
            'occi.agreement.state': 'accepted',
            'occi.agreement.effectiveFrom': '2014-11-02T02:20:26+00:00',
            'occi.agreement.effectiveUntil': '2015-11-02T02:20:27+00:00'
        }

        northbound_api = api.build()
        resources = EntityDictionary(northbound_api.registry)
        resources[self.id] = res

        resources.registry.populate_resources()

        active_agreements = resources.registry.get_active_agreement_resources()

        if active_agreements.__len__() == 1:
            self.assertEqual(active_agreements[0].identifier, self.id)
        else:
            for agr in active_agreements:
                self.assertTrue(agr.identifier == self.id)
    def test_load_multiple_resource_with_mixins_from_DB(self):
        """
            retrieve a resource from the dictionary with the same mixins
        """
        res_id = "/agreement/load-mult-mixins-from-db"
        test_resource = core_model.Resource(res_id, None,
                                            [test_data.epc_mixin, test_data.ran_mixin])

        resources = EntityDictionary(self.api.registry)
        resources._persist_resource(res_id, test_resource)

        resources.populate_from_db()

        test_mxn_loc = [mix.location for mix in test_resource.mixins]
        rtrnd_mxn_loc = [mixin.location for mixin in resources[res_id].mixins]

        self.assertEqual(test_mxn_loc.sort(), rtrnd_mxn_loc.sort())
    def test_populate_dictionary_with_basic_link_from_DB(self):
        # Create resources and Link
        src_id = "124816"
        tar_id = "112358"
        src_res = core_model.Resource(src_id, occi_sla.AGREEMENT, None)
        tar_res = core_model.Resource(tar_id, occi_sla.AGREEMENT, None)

        lnk_id = "/agreement/load-link-entity-from-DB"
        lnk = core_model.Link(lnk_id, None, None, src_res, tar_res)

        links = EntityDictionary(self.api.registry)
        links._persist_resource(src_id, src_res)
        links._persist_resource(tar_id, tar_res)
        links._persist_link(lnk_id, lnk)

        links.populate_from_db()

        self.assertEqual(len(links), 3)
        self.assertTrue(isinstance(links[lnk_id], core_model.Link))
        self.assertEqual(links[lnk_id].identifier, lnk_id)
        # Removing assistive field templates for comparison
        temp_src = links[lnk_id].source.__dict__
        temp_trgt = links[lnk_id].target.__dict__
        temp_src.pop('templates')
        temp_trgt.pop('templates')
        self.assertEqual(temp_src, src_res.__dict__)
        self.assertEqual(temp_trgt, tar_res.__dict__)
Exemple #13
0
    def test_add_resource_with_kind(self):
        res_id = "/agreement/adding-resource-kind"
        res = core_model.Resource(res_id, occi_sla.AGREEMENT, None)

        resources = EntityDictionary(None)
        resources[res_id] = res

        result_dict = self.db.entities.find_one(res_id)
        self.assertEqual(result_dict["kind"], occi_sla.AGREEMENT.location)
    def test_populate_dictionary_with_basic_resource_from_DB(self):
        """
            This test retrieves a resource from the resource dictionary
        """
        # Create resource and save to DB
        res_id = "/agreement/load-basic-from-db"
        test_resource = core_model.Resource(res_id, occi_sla.AGREEMENT, None)

        resources = EntityDictionary(self.api.registry)
        # bypass normal resource add so that only the db record exists and not
        # the in memeory dictionary
        resources._persist_resource(res_id, test_resource)

        resources.populate_from_db()

        print test_resource.__dict__
        print resources[res_id].__dict__
        self.assertEqual(len(resources), 1)
        self.assertTrue(isinstance(resources[res_id], core_model.Resource))
Exemple #15
0
    def test_add_resource_to_dictionary(self):
        """
            Test that a resource is being added to the DB
        """
        resources = EntityDictionary(None)
        id = {"_id": "/agreement/4545-4545454-sdasdas"}
        res = core_model.Resource("4777", None, [])
        resources[id["_id"]] = res

        self.assertGreater(self.db.entities.find(id).count(), 0)
        LOG.info("Added dictionary item to db")
Exemple #16
0
    def test_resource_added_to_DB_reflects_resource_sent(self):
        """
            Test to ensure that the correct data is being sent to the DB
        """
        resources = EntityDictionary(None)
        id = {"_id": "/agreement/4545-4545454-sdasdas-blah"}
        res = core_model.Resource("1245", None, [])
        resources[id["_id"]] = res

        self.assertEqual(id["_id"],
                         self.db.entities.find_one({}, {"_id": 1})["_id"])
        LOG.info("Correct entity added to the db")
Exemple #17
0
    def test_mixins_are_being_added_to_db(self):
        """
            Test that mixins are being added to the DB
        """
        resources = EntityDictionary(None)

        id = {"_id": "/agreement/45454-ghghg53-454545"}
        res = core_model.Resource(id["_id"], None,
                                  [test_data.epc_mixin, test_data.ran_mixin])
        resources[id["_id"]] = res
        cursor = self.db.entities.find_one(id, {"_id": 0, "mixins": 1})
        mixins = cursor["mixins"]
        self.assertEqual(len(mixins), 2)
        LOG.info("Correct number of mixins added to DB with resource")
Exemple #18
0
    def test_correct_mixins_are_being_added_to_db(self):
        """
            Test that the correct mixin ids are being added to the database.
            Using mixin location as the mixin id.
        """
        resources = EntityDictionary(None)

        id = {"_id": "/agreement/45454-ghghg53-aaees"}
        res = core_model.Resource(id["_id"], None,
                                  [test_data.epc_mixin, test_data.ran_mixin])
        resources[id["_id"]] = res
        cursor = self.db.entities.find_one(id, {"_id": 0, "mixins": 1})

        self.assertIn(test_data.epc_mixin.location, cursor["mixins"])
        self.assertIn(test_data.ran_mixin.location, cursor["mixins"])
        LOG.info("Correct mixins are being added to the DB")
Exemple #19
0
    def test_delete_multiple_entities_from_dictionary(self):
        """
            Delete multiple entities and ensure others not deleted are
            persisted
        """
        res_0 = core_model.Resource("res_0", None, None)
        res_1 = core_model.Resource("res_1", None, None)
        res_2 = core_model.Resource("res_2", None, None)
        res_3 = core_model.Resource("res_3", None, None)
        res_4 = core_model.Resource("res_4", None, None)
        res_5 = core_model.Resource("res_5", None, None)
        lnk_0 = core_model.Link("lnk_0", None, None, res_1, res_5)
        lnk_1 = core_model.Link("lnk_1", None, None, res_1, res_5)

        entities = EntityDictionary(None)
        entities[res_0.identifier] = res_0
        entities[res_1.identifier] = res_1
        entities[res_2.identifier] = res_2
        entities[res_3.identifier] = res_3
        entities[res_4.identifier] = res_4
        entities[res_5.identifier] = res_5
        entities[lnk_0.identifier] = lnk_0
        entities[lnk_1.identifier] = lnk_1

        del entities[res_1.identifier]
        del entities[res_5.identifier]
        del entities[lnk_1.identifier]

        self.assertIsNotNone(self.db.entities.find_one("res_0"))
        self.assertIsNotNone(self.db.entities.find_one("res_2"))
        self.assertIsNotNone(self.db.entities.find_one("res_3"))
        self.assertIsNotNone(self.db.entities.find_one("res_4"))
        self.assertIsNotNone(self.db.entities.find_one("lnk_0"))

        self.assertIsNone(self.db.entities.find_one("res_1"))
        self.assertIsNone(self.db.entities.find_one("res_5"))
        self.assertIsNone(self.db.entities.find_one("lnk_1"))

        self.assertTrue("res_0" in entities)
        self.assertTrue("res_2" in entities)
        self.assertTrue("res_3" in entities)
        self.assertTrue("res_4" in entities)
        self.assertTrue("lnk_0" in entities)

        self.assertFalse("res_1" in entities)
        self.assertFalse("res_5" in entities)
        self.assertFalse("lnk_1" in entities)
Exemple #20
0
    def test_add_resource_which_is_already_in_dictionary(self):
        """
            Add a resource with the same ID of a value already in the database
        """
        id = {"_id": "/agreement/4545-4545454-sdasdas-blah"}

        res_0 = core_model.Resource("11235", None, None)
        res_1 = core_model.Resource("18512", None, None)

        resources = EntityDictionary(None)
        resources[id["_id"]] = res_0
        resources[id["_id"]] = res_1

        self.assertEqual(self.db.entities.find().count(), 1)
        self.assertEqual(
            self.db.entities.find_one(id)["identifier"], res_1.identifier)
        LOG.info("Overwriting existing entry in dict reflected in DB")
Exemple #21
0
    def test_retrieve_correct_resource_with_mixins_from_dictionary(self):
        """
            retrieve a resource from the dictionary with the same mixins
        """
        test_resource_id = "/agreement/retrieve-mixins-from-dictionary"
        test_resource = core_model.Resource(test_resource_id, None,
                                            [test_data.epc_mixin])
        registry = api.build().registry
        resources = EntityDictionary(registry)
        resources[test_resource_id] = test_resource

        returned_resource = resources[test_resource_id]

        rtrnd_mxn = returned_resource.mixins[0]

        self.assertEqual(test_resource.mixins[0].location, rtrnd_mxn.location)
        self.assertEqual(test_resource.mixins[0].term, rtrnd_mxn.term)
        self.assertEqual(test_resource.mixins[0].scheme, rtrnd_mxn.scheme)
Exemple #22
0
    def test_delete_entity_from_dictionary_and_DB(self):
        """
            Tests that a delete on the dictionary removes entity from the
            database
        """
        # Create a resource
        id = {"_id": "/agreement/resource-for-deletion"}
        resource = core_model.Resource(id["_id"], None, [])

        entities = EntityDictionary(None)
        entities[id["_id"]] = resource

        # delete the resource

        del entities[id["_id"]]
        db_res = self.db.entities.find_one(id)

        self.assertEqual(db_res, None)
        self.assertEqual(len(entities), 0)
Exemple #23
0
    def test_policy_creation(self):
        """
			Check that an active agreement has been detected and a record inserted into the database
		"""
        availability = core_model.Mixin('', 'availability',
                                        [occi_sla.AGREEMENT_TEMPLATE])
        mixins = [availability]
        res = core_model.Resource(self.id, occi_sla.AGREEMENT, mixins)
        res.attributes = {
            'occi.agreement.state': 'accepted',
            'occi.agreement.effectiveFrom': '2014-11-02T02:20:26+00:00',
            'occi.agreement.effectiveUntil': '2015-11-02T02:20:27+00:00'
        }

        comp_res = core_model.Resource(self.compute_id, infrastructure.COMPUTE,
                                       [])
        comp_res.attributes = {}

        link_res = core_model.Link(self.link_id, occi_sla.AGREEMENT_LINK, [],
                                   res, comp_res)
        link_res.attributes = {}

        res.links = [link_res]

        northbound_api = api.build()
        resources = EntityDictionary(northbound_api.registry)
        resources[self.id] = res
        resources[self.compute_id] = comp_res
        resources[self.link_id] = link_res

        resources.registry.populate_resources()

        myrulesengine = rulesengine.RulesEngine(resources.registry)
        myrulesengine.active_policies = {}
        myrulesengine.active_agreements = {}

        myrulesengine.start_engine(0)

        temp_policy_record = DB.policies.find({'agreement_id': self.id},
                                              {'_id': 0})

        self.assertEqual(temp_policy_record.count(), 1)
Exemple #24
0
    def test_add_resource_with_attributes(self):
        res_id = "/agreement/adding-resource-with-agreements"
        res = core_model.Resource(res_id, None, None)

        res_attr = {
            "the.test.attr": "1",
            "attr-2": "2",
            "attr_3": "3",
            "attr||four": "4"
        }
        res.attributes = res_attr
        resources = EntityDictionary(None)
        resources[res_id] = res
        persisted_res = self.db.entities.find_one(res_id)
        pstd_res_attrs = persisted_res["attributes"]

        self.assertEqual(pstd_res_attrs["the^test^attr"],
                         res_attr["the.test.attr"])
        self.assertEqual(pstd_res_attrs["attr-2"], res_attr["attr-2"])
        self.assertEqual(pstd_res_attrs["attr_3"], res_attr["attr_3"])
Exemple #25
0
    def test_delete_entire_dictionary(self):
        """
            Delete the entire dictionary using "del dict_instance"
        """
        res_0 = core_model.Resource("res_0", None, None)
        res_1 = core_model.Resource("res_1", None, None)
        res_2 = core_model.Resource("res_2", None, None)
        res_3 = core_model.Resource("res_3", None, None)

        entities = EntityDictionary(None)
        entities[res_0.identifier] = res_0
        entities[res_1.identifier] = res_1
        entities[res_2.identifier] = res_2
        entities[res_3.identifier] = res_3

        del entities

        self.assertIsNone(self.db.entities.find_one("res_0"))
        self.assertIsNone(self.db.entities.find_one("res_1"))
        self.assertIsNone(self.db.entities.find_one("res_2"))
        self.assertIsNone(self.db.entities.find_one("res_3"))
Exemple #26
0
    def test_load_resource_with_mixin_type_from_DB(self):
        """
            retrieve a resource from the db with the same mixins
        """
        res_id = "/agreement/load-resource-with-mixin-from-db"
        test_resource = core_model.Resource(res_id, occi_sla.AGREEMENT,
                                            [test_data.epc_mixin])
        resources = EntityDictionary(self.api.registry)

        resources._persist_resource(res_id, test_resource)

        resources.populate_from_db()

        res_mixins = resources[res_id].mixins
        self.assertEqual(len(res_mixins), 1)
        self.assertTrue(isinstance(res_mixins[0], core_model.Mixin))
Exemple #27
0
    def test_populate_dictionary_with_resource_containing_attributes(self):
        res_id = "/agreement/load_resource_w_attributes"
        res = core_model.Resource(res_id, None, None)
        res_attrs = {
            "the.test.attr": "1",
            "attr-2": "2",
            "attr_3": "3",
            "attr||four": "4"
        }
        res.attributes = res_attrs

        resources = EntityDictionary(self.api.registry)
        resources._persist_resource(res_id, res)
        resources.populate_from_db()

        self.assertEqual(resources[res_id].attributes, res_attrs)
Exemple #28
0
    def test_load_multiple_resource_with_mixins_from_DB(self):
        """
            retrieve a resource from the dictionary with the same mixins
        """
        res_id = "/agreement/load-mult-mixins-from-db"
        test_resource = core_model.Resource(
            res_id, None, [test_data.epc_mixin, test_data.ran_mixin])

        resources = EntityDictionary(self.api.registry)
        resources._persist_resource(res_id, test_resource)

        resources.populate_from_db()

        test_mxn_loc = [mix.location for mix in test_resource.mixins]
        rtrnd_mxn_loc = [mixin.location for mixin in resources[res_id].mixins]

        self.assertEqual(test_mxn_loc.sort(), rtrnd_mxn_loc.sort())
Exemple #29
0
    def test_correct_resource_links_collection_are_being_added_to_db(self):
        """
            Test if the correct links are being added to the resource record
            in the DB. note: these are links within resources. Not links as
            resources
        """
        # Create resources and Link
        src_res_id = "124816"
        tar_res_id = "112358"
        src_res = core_model.Resource(src_res_id, None, None)
        tar_res = core_model.Resource(tar_res_id, None, None)

        lnk_id = {"_id": "/agreement/add-link-entity"}
        lnk = core_model.Link(lnk_id["_id"], None, None, src_res, tar_res)

        # Load Link
        resources = EntityDictionary(None)
        resources[lnk_id["_id"]] = lnk

        result_dict = self.db.entities.find_one(lnk_id["_id"])
        self.assertEqual(result_dict["source"], src_res_id)
        self.assertEqual(result_dict["target"], tar_res_id)
        LOG.info("Correct resource links added to the DB")
Exemple #30
0
    def test_populate_dictionary_with_basic_resource_from_DB(self):
        """
            This test retrieves a resource from the resource dictionary
        """
        # Create resource and save to DB
        res_id = "/agreement/load-basic-from-db"
        test_resource = core_model.Resource(res_id, occi_sla.AGREEMENT, None)

        resources = EntityDictionary(self.api.registry)
        # bypass normal resource add so that only the db record exists and not
        # the in memeory dictionary
        resources._persist_resource(res_id, test_resource)

        resources.populate_from_db()

        print test_resource.__dict__
        print resources[res_id].__dict__
        self.assertEqual(len(resources), 1)
        self.assertTrue(isinstance(resources[res_id], core_model.Resource))
Exemple #31
0
    def test_populate_dictionary_with_resource_containing_links(self):
        # Test entities - creating a resource with two links
        res_tar_1_id = "/an-id/res_tar_1"
        res_src_1_id = "/an-id/res_src_1"
        lnk_1_id = "/an-id/lnk_1_id"
        res_tar_2_id = "/an-id/res_tar_2"
        res_src_2_id = "/an-id/res_src_2"
        lnk_2_id = "/an-id/lnk_2_id"
        res_m_id = "/an-id/res_m"

        res_src_1 = core_model.Resource(res_src_1_id, occi_sla.AGREEMENT, None)
        res_tar_1 = core_model.Resource(res_tar_1_id, occi_sla.AGREEMENT, None)
        lnk_1 = core_model.Link(lnk_1_id, occi_sla.AGREEMENT_LINK, None,
                                res_src_1, res_tar_1)
        res_src_2 = core_model.Resource(res_src_2_id, occi_sla.AGREEMENT, None)
        res_tar_2 = core_model.Resource(res_tar_2_id, occi_sla.AGREEMENT, None)
        lnk_2 = core_model.Link(lnk_2_id, occi_sla.AGREEMENT_LINK, None,
                                res_src_2, res_tar_2)
        res_m = core_model.Resource(res_m_id, occi_sla.AGREEMENT, None,
                                    [lnk_1, lnk_2])

        # Persist all to DB but not dict in memory
        entities = EntityDictionary(self.api.registry)
        entities._persist_link(lnk_1_id, lnk_1)
        entities._persist_link(lnk_2_id, lnk_2)
        entities._persist_resource(res_src_1_id, res_src_1)
        entities._persist_resource(res_tar_1_id, res_tar_1)
        entities._persist_resource(res_src_2_id, res_src_2)
        entities._persist_resource(res_tar_2_id, res_tar_2)
        entities._persist_resource(res_m_id, res_m)

        entities.populate_from_db()

        self.assertEqual(len(entities), 7)
        self.assertEqual(len(entities[res_m_id].links), 2)

        for link in entities[res_m_id].links:
            if link.identifier == lnk_1_id:
                # Removing assistive field templates for comparison
                temp_src = link.source.__dict__
                temp_trgt = link.target.__dict__
                temp_src.pop('templates')
                temp_trgt.pop('templates')
                self.assertEqual(temp_src, res_src_1.__dict__)
                self.assertEqual(temp_trgt, res_tar_1.__dict__)
            else:
                # Removing assistive field templates for comparison
                temp_src = link.source.__dict__
                temp_trgt = link.target.__dict__
                temp_src.pop('templates')
                temp_trgt.pop('templates')
                self.assertEqual(temp_src, res_src_2.__dict__)
                self.assertEqual(temp_trgt, res_tar_2.__dict__)
    def test_populate_dictionary_with_resource_containing_links(self):
        # Test entities - creating a resource with two links
        res_tar_1_id = "/an-id/res_tar_1"
        res_src_1_id = "/an-id/res_src_1"
        lnk_1_id = "/an-id/lnk_1_id"
        res_tar_2_id = "/an-id/res_tar_2"
        res_src_2_id = "/an-id/res_src_2"
        lnk_2_id = "/an-id/lnk_2_id"
        res_m_id = "/an-id/res_m"

        res_src_1 = core_model.Resource(res_src_1_id, occi_sla.AGREEMENT, None)
        res_tar_1 = core_model.Resource(res_tar_1_id, occi_sla.AGREEMENT, None)
        lnk_1 = core_model.Link(lnk_1_id, occi_sla.AGREEMENT_LINK, None, res_src_1, res_tar_1)
        res_src_2 = core_model.Resource(res_src_2_id, occi_sla.AGREEMENT, None)
        res_tar_2 = core_model.Resource(res_tar_2_id, occi_sla.AGREEMENT, None)
        lnk_2 = core_model.Link(lnk_2_id, occi_sla.AGREEMENT_LINK, None, res_src_2, res_tar_2)
        res_m = core_model.Resource(res_m_id, occi_sla.AGREEMENT, None, [lnk_1, lnk_2])

        # Persist all to DB but not dict in memory
        entities = EntityDictionary(self.api.registry)
        entities._persist_link(lnk_1_id, lnk_1)
        entities._persist_link(lnk_2_id, lnk_2)
        entities._persist_resource(res_src_1_id, res_src_1)
        entities._persist_resource(res_tar_1_id, res_tar_1)
        entities._persist_resource(res_src_2_id, res_src_2)
        entities._persist_resource(res_tar_2_id, res_tar_2)
        entities._persist_resource(res_m_id, res_m)

        entities.populate_from_db()

        self.assertEqual(len(entities), 7)
        self.assertEqual(len(entities[res_m_id].links), 2)

        for link in entities[res_m_id].links:
            if link.identifier == lnk_1_id:
                # Removing assistive field templates for comparison
                temp_src = link.source.__dict__
                temp_trgt = link.target.__dict__
                temp_src.pop('templates')
                temp_trgt.pop('templates')
                self.assertEqual(temp_src, res_src_1.__dict__)
                self.assertEqual(temp_trgt, res_tar_1.__dict__)
            else:
                # Removing assistive field templates for comparison
                temp_src = link.source.__dict__
                temp_trgt = link.target.__dict__
                temp_src.pop('templates')
                temp_trgt.pop('templates')
                self.assertEqual(temp_src, res_src_2.__dict__)
                self.assertEqual(temp_trgt, res_tar_2.__dict__)