Пример #1
0
def test_get_rule(mock):
    # Should raise an exception when the rule doesn't exist
    rule_uri = 'http://example.com#rule'
    with pytest.raises(ValueError):
        db_access.get_rule(rule_uri)

    # Should get uri, type and label of the rule
    rule_type = 'http://www.w3.org/ns/odrl/2/permission'
    rule_label = 'Rule'
    db_access.create_rule(rule_uri, rule_type, rule_label)
    action_uri = 'http://www.w3.org/ns/odrl/2/distribute'
    db_access.add_action_to_rule(action_uri, rule_uri)
    assignor_uri = 'https://example.com#assignor'
    assignee_uri = 'https://example.com#assignee'
    db_access.create_party(assignor_uri)
    db_access.create_party(assignee_uri)
    db_access.add_assignor_to_rule(assignor_uri, rule_uri)
    db_access.add_assignee_to_rule(assignee_uri, rule_uri)
    rule = db_access.get_rule(rule_uri)
    assert rule['URI'] == rule_uri
    assert rule['TYPE_URI'] == rule_type
    assert rule['TYPE_LABEL'] == 'Permission'
    assert rule['LABEL'] == rule_label

    # Should get actions associated with the rule
    action = rule['ACTIONS'][0]
    assert action['LABEL'] == 'Distribute'
    assert action['URI'] == action_uri
    assert action['DEFINITION'] == 'To supply the Asset to third-parties.'

    # Should get assignors and assignees associated with the rule
    assert rule['ASSIGNORS'] == [assignor_uri]
    assert rule['ASSIGNEES'] == [assignee_uri]
Пример #2
0
def test_delete_rule(mock):
    # Should not be able to delete if the rule is still used in any policies
    policy_uri = 'https://example.com#policy'
    db_access.create_policy(policy_uri)
    rule_uri = 'http://example.com#rule'
    rule_type = 'http://www.w3.org/ns/odrl/2/permission'
    rule_label = 'Rule'
    db_access.create_rule(rule_uri, rule_type, rule_label)
    assignor_uri = 'http://example.com#assignor'
    db_access.create_party(assignor_uri)
    db_access.add_assignor_to_rule(assignor_uri, rule_uri)
    assignee_uri = 'http://example.com#assignee'
    db_access.create_party(assignee_uri)
    db_access.add_assignee_to_rule(assignee_uri, rule_uri)
    action_uri = 'http://www.w3.org/ns/odrl/2/distribute'
    db_access.add_action_to_rule(action_uri, rule_uri)
    db_access.add_rule_to_policy(rule_uri, policy_uri)
    with pytest.raises(ValueError):
        db_access.delete_rule(rule_uri)

    # Should delete the rule
    db_access.remove_rule_from_policy(rule_uri, policy_uri)
    db_access.delete_rule(rule_uri)
    assert not db_access.rule_exists(rule_uri)

    # Should also remove any actions from the rule
    assert not db_access.rule_has_action(rule_uri, action_uri)

    # Should also remove any assignors from the rule
    assert not db_access.rule_has_assignor(assignor_uri, rule_uri)

    # Should also remove any assignees from the rule
    assert not db_access.rule_has_assignee(assignee_uri, rule_uri)
Пример #3
0
def test_party_exists(mock):
    # Should return false if the party does not exist
    party_uri = 'https://example.com#party'
    assert not db_access.party_exists(party_uri)

    # Should return true of the party exists
    db_access.create_party(party_uri)
    assert db_access.party_exists(party_uri)
Пример #4
0
def test_remove_assignee_from_rule(mock):
    # Should remove an assignee from a rule
    assignee_uri = 'http://example.com#assignee'
    rule_uri = 'https://example.com#rule'
    rule_type = 'http://www.w3.org/ns/odrl/2/permission'
    db_access.create_rule(rule_uri, rule_type)
    db_access.create_party(assignee_uri)
    db_access.add_assignee_to_rule(assignee_uri, rule_uri)
    db_access.remove_assignee_from_rule(assignee_uri, rule_uri)
    assert not db_access.rule_has_assignee(rule_uri, assignee_uri)
Пример #5
0
def test_create_party(mock):
    # Should create a party with the URI, label and comment given
    party_uri = 'https://example.com#party'
    label = 'Party'
    comment = 'This is a test party.'
    db_access.create_party(party_uri, label, comment)
    assert db_access.party_exists(party_uri)

    # Should raise an exception of the party already exists
    with pytest.raises(ValueError):
        db_access.create_party(party_uri)
Пример #6
0
def test_rule_has_assignee(mock):
    # Should return false if the rule doesn't have that assignee
    assignee_uri = 'http://example.com#assignee'
    rule_uri = 'https://example.com#rule'
    rule_type = 'http://www.w3.org/ns/odrl/2/permission'
    db_access.create_rule(rule_uri, rule_type)
    assert not db_access.rule_has_assignee(rule_uri, assignee_uri)

    # Should return true if the rule does have that assignee
    db_access.create_party(assignee_uri)
    db_access.add_assignee_to_rule(assignee_uri, rule_uri)
    assert db_access.rule_has_assignee(rule_uri, assignee_uri)
Пример #7
0
def test_get_assignors_for_rule(mock):
    rule_uri = 'https://example.com#rule'
    rule_type = 'http://www.w3.org/ns/odrl/2/permission'
    db_access.create_rule(rule_uri, rule_type)
    assignor1 = 'https://example.com#assignor1'
    assignor2 = 'https://example.com#assignor2'
    db_access.create_party(assignor1)
    db_access.create_party(assignor2)
    db_access.add_assignor_to_rule(assignor1, rule_uri)
    db_access.add_assignor_to_rule(assignor2, rule_uri)
    assignors = db_access.get_assignors_for_rule(rule_uri)
    assert assignors == [assignor1, assignor2]
Пример #8
0
def test_get_assignees_for_rule(mock):
    rule_uri = 'https://example.com#rule'
    rule_type = 'http://www.w3.org/ns/odrl/2/permission'
    rule_label = 'Rule'
    db_access.create_rule(rule_uri, rule_type, rule_label)
    assignee1 = 'https://example.com#assignee1'
    assignee2 = 'https://example.com#assignee2'
    db_access.create_party(assignee1)
    db_access.create_party(assignee2)
    db_access.add_assignee_to_rule(assignee1, rule_uri)
    db_access.add_assignee_to_rule(assignee2, rule_uri)
    assignees = db_access.get_assignees_for_rule(rule_uri)
    assert assignees == [assignee1, assignee2]
Пример #9
0
def test_get_party(mock):
    # Should raise exception if the Party does not exist
    uri = 'https://example.com/party/1'
    with pytest.raises(ValueError):
        db_access.get_party(uri)

    # Should retrieve info about a Party if it exists
    label = 'Party'
    comment = 'This is a party.'
    db_access.create_party(uri, label, comment)
    assert db_access.get_party(uri) == {
        'URI': uri,
        'LABEL': label,
        'COMMENT': comment
    }
Пример #10
0
def test_delete_party(mock):
    # Should raise an exception if the Party is currently assigned to a Rule
    party_uri = 'https://example.com#party'
    rule_uri = 'https://example.com#rule'
    rule_type = 'http://www.w3.org/ns/odrl/2/permission'
    db_access.create_party(party_uri)
    db_access.create_rule(rule_uri, rule_type)
    db_access.add_assignor_to_rule(party_uri, rule_uri)
    with pytest.raises(ValueError):
        db_access.delete_party(party_uri)

    # Should delete the Party if not currently assigned to a Rule (deleting the rule drops the assignment)
    db_access.delete_rule(rule_uri)
    db_access.delete_party(party_uri)
    assert not db_access.party_exists(party_uri)
Пример #11
0
def test_get_all_parties(mock):
    uri1 = 'https://example.com/party/1'
    label1 = 'Party1'
    comment1 = 'This is the first party'
    uri2 = 'https://example.com/party/2'
    db_access.create_party(uri1, label1, comment1)
    db_access.create_party(uri2)
    assert db_access.get_all_parties() == [{
        'URI': uri1,
        'LABEL': label1,
        'COMMENT': comment1
    }, {
        'URI': uri2,
        'LABEL': None,
        'COMMENT': None
    }]
Пример #12
0
def test_get_rules_for_party(mock):
    party_uris = ['https://example.com/party/1', 'https://example.com/party/2']
    for party_uri in party_uris:
        db_access.create_party(party_uri)
    rule_uris = [
        'http://example.com/rule/1', 'http://example.com/rule/2',
        'http://example.com/rule/3', 'http://example.com/rule/4'
    ]
    rule_type = 'http://www.w3.org/ns/odrl/2/permission'
    for rule_uri in rule_uris:
        db_access.create_rule(rule_uri, rule_type)
    db_access.add_assignor_to_rule(party_uris[0], rule_uris[0])
    db_access.add_assignor_to_rule(party_uris[0], rule_uris[1])
    db_access.add_assignee_to_rule(party_uris[0], rule_uris[2])
    db_access.add_assignor_to_rule(party_uris[1], rule_uris[3])

    # Should list the parties assigned to a rule and only those parties
    assert {rule_uris[0], rule_uris[1],
            rule_uris[2]} == set(db_access.get_rules_for_party(party_uris[0]))
    assert [rule_uris[3]] == db_access.get_rules_for_party(party_uris[1])
Пример #13
0
def test_add_assignee_to_rule(mock):
    # Should raise an exception if the rule doesn't exist
    rule_uri = 'https://example.com#rule'
    assignee_uri = 'http://example.com#assignee'
    db_access.create_party(assignee_uri)
    with pytest.raises(ValueError):
        db_access.add_assignee_to_rule(assignee_uri, rule_uri)

    # Should raise an exception if the assignee doesn't exist
    rule_type = 'http://www.w3.org/ns/odrl/2/permission'
    db_access.delete_party(assignee_uri)
    db_access.create_rule(rule_uri, rule_type)
    with pytest.raises(ValueError):
        db_access.add_assignee_to_rule(assignee_uri, rule_uri)

    # Should add an assignee to a rule
    db_access.create_party(assignee_uri)
    db_access.add_assignee_to_rule(assignee_uri, rule_uri)
    assert db_access.rule_has_assignee(rule_uri, assignee_uri)

    # Should raise an exception if the rule already has that assignee
    with pytest.raises(ValueError):
        db_access.add_assignee_to_rule(assignee_uri, rule_uri)
def nem_513a():
    db_access.create_party('http://test.linked.data.gov.au/board/B-0068')
    db_access.create_party(
        'http://example.com/group/power-companies', 'Example Power Company',
        'This is a Party representing some hypothetical power companies.')
    policy_uri = _conf.BASE_URI + 'licence/8d2f3d11-ec65-4f75-8b4f-6dc621695678'
    attributes = {
        'status':
        'http://dd.eionet.europa.eu/vocabulary/datadictionary/status/submitted',
        'label':
        'National Electricity Rules 5.13A Distribution zone substation information',
        'legal_code':
        'https://www.aemc.gov.au/sites/default/files/2018-04/NER%20-%20v107%20-%20Chapter%205.PDF',
        'creator':
        'http://data.bioregionalassessments.gov.au/person/car587',
        'has_version':
        '107',
        'language':
        'http://www.lexvo.org/page/iso639-3/eng',
        'see_also':
        'https://www.aemc.gov.au/sites/default/files/2018-04/NER%20-%20v107%20-%20Chapter%205.PDF'
    }
    rules = [{
        'URI':
        _conf.BASE_URI + 'rule/' + str(uuid4()),
        'TYPE_LABEL':
        'Duty',
        'ACTIONS': [],
        'ASSIGNORS': [{
            'URI': 'http://test.linked.data.gov.au/board/B-0068'
        }],
        'ASSIGNEES': [{
            'URI': 'http://example.com/group/power-companies'
        }]
    }]
    functions.create_policy(policy_uri, attributes, rules)
def create_policy(policy_uri, attributes=None, rules=None):
    """
    Creates an entire policy with the given URI, attributes and Rules.
    Will attempt to use Rule Type and Actions whether they are given by URI or Label.
    If there is an error at any point, will rollback all changes to database to avoid leaving partially created
    policies.

    :param policy_uri:
    :param attributes:  Dictionary of optional attributes of the policy.
        Permitted attributes:   type, label, jurisdiction, legal_code, has_version, language, see_also
                                same_as, comment, logo, status
    :param rules: List of Rules. Each Rule should be a Dictionary containing the following elements:
        TYPE_URI*: string (i.e. 'http://www.w3.org/ns/odrl/2/duty')
        TYPE_LABEL*: string (i.e. 'duty')
        * At least one of TYPE_URI or TYPE_LABEL should be provided, but both are not required
        ASSIGNORS: List of Assignors. Each Assignor should be a Dictionary containing a URI, LABEL and COMMENT.
        ASSIGNEES: List of Assignees.  Each Assignee should be a Dictionary containing a URI, LABEL and COMMENT.
        ACTIONS: List of strings (URIs or labels)
    :return:
    """
    if not is_valid_uri(policy_uri):
        raise ValueError('Not a valid URI: ' + policy_uri)

    permitted_rule_types = []

    try:
        db_access.create_policy(policy_uri)
        if attributes:
            for attr_name, attr_value in attributes.items():
                db_access.set_policy_attribute(policy_uri, attr_name,
                                               attr_value)
        if rules:
            for rule in rules:
                rule_uri = _conf.BASE_URI + 'rules/' + str(uuid4())
                if 'TYPE_URI' in rule:
                    rule_type = rule['TYPE_URI']
                elif 'TYPE_LABEL' in rule:
                    if not permitted_rule_types:
                        permitted_rule_types = db_access.get_permitted_rule_types(
                        )
                    rule_type = get_rule_type_uri(rule['TYPE_LABEL'],
                                                  permitted_rule_types)
                    if not rule_type:
                        raise ValueError('Cannot create policy - Rule type ' +
                                         rule['TYPE_LABEL'] +
                                         ' is not permitted')
                else:
                    raise ValueError(
                        'Cannot create policy - no Rule type provided')
                db_access.create_rule(rule_uri, rule_type)
                for action in rule['ACTIONS']:
                    if action:
                        if is_valid_uri(action):
                            action_uri = action
                        else:
                            permitted_actions = db_access.get_all_actions()
                            action_uri = get_action_uri(
                                action, permitted_actions)
                            if not action_uri:
                                raise ValueError(
                                    'Cannot create policy - Action ' + action +
                                    ' is not permitted')
                        db_access.add_action_to_rule(action_uri, rule_uri)
                if 'ASSIGNORS' in rule:
                    for assignor in rule['ASSIGNORS']:
                        if not db_access.party_exists(assignor['URI']):
                            db_access.create_party(assignor['URI'],
                                                   assignor['LABEL'],
                                                   assignor['COMMENT'])
                        db_access.add_assignor_to_rule(assignor['URI'],
                                                       rule_uri)
                if 'ASSIGNEES' in rule:
                    for assignee in rule['ASSIGNEES']:
                        if not db_access.party_exists(assignee['URI']):
                            db_access.create_party(assignee['URI'],
                                                   assignee['LABEL'],
                                                   assignee['COMMENT'])
                        db_access.add_assignee_to_rule(assignee['URI'],
                                                       rule_uri)
                db_access.add_rule_to_policy(rule_uri, policy_uri)
    except Exception as error:
        db_access.rollback_db()
        raise error
    db_access.commit_db()