import unittest

import sys
try:
    from unit.utils import *
    import unit.log_output as log_output
except ImportError: #for Python2
    from utils import *
    import log_output as log_output

sys.path.append("wrappers")
from modelverse import *

import threading

### Model operations
def null_operation(model):
    pass

def model_is_empty_operation(model):
    assert model is None
    
def ops_manual_callback(model):
    p1 = instantiate(model, "PetriNet_Runtime/Place")
    p2 = instantiate(model, "PetriNet_Runtime/Place")
    p3 = instantiate(model, "PetriNet_Runtime/Place")
    t1 = instantiate(model, "PetriNet_Runtime/Transition")
    p2t1 = instantiate(model, "PetriNet_Runtime/P2T", (p1, t1))
    p2t2 = instantiate(model, "PetriNet_Runtime/P2T", (p2, t1))
    t2p1 = instantiate(model, "PetriNet_Runtime/T2P", (t1, p3))
    attr_assign(model, p1, "tokens", 1)
    attr_assign(model, p1, "name", "p1")
    attr_assign(model, p2, "tokens", 2)
    attr_assign(model, p2, "name", "p2")
    attr_assign(model, p3, "tokens", 3)
    attr_assign(model, p3, "name", "p3")
    attr_assign(model, t1, "name", "t1")
    attr_assign(model, t1, "executing", False)
    attr_assign(model, p2t1, "weight", 1)
    attr_assign(model, p2t2, "weight", 1)
    attr_assign(model, t2p1, "weight", 2)

def pn_subfunc_callback_refine_PN(model):
    p1 = instantiate(model, "PetriNet/Place")
    attr_assign(model, p1, "name", "p1")
    attr_assign(model, p1, "tokens", 1)

    t1 = instantiate(model, "PetriNet/Transition")
    attr_assign(model, t1, "name", "t1")

    p2t = instantiate(model, "PetriNet/P2T", (p1, t1))
    attr_assign(model, p2t, "weight", 1)

def render_add_tracability(model):
    instantiate(model, "Association", ("abstract/Block", "rendered/Group"), ID="TracabilityLink")
            
def ops_add_tracability_D2R(model):
    instantiate(model, "Association", ("PetriNet/Place", "PetriNet_Runtime/Place"), ID="D2R_PlaceLink")
    instantiate(model, "Association", ("PetriNet/Transition", "PetriNet_Runtime/Transition"), ID="D2R_TransitionLink")

def ops_add_tracability_R2D(model):
    instantiate(model, "Association", ("PetriNet_Runtime/Place", "PetriNet/Place"), ID="R2D_PlaceLink")
    instantiate(model, "Association", ("PetriNet_Runtime/Transition", "PetriNet/Transition"), ID="R2D_TransitionLink")
            
def operation_exec_1(model):
    assert model is not None
    assert [{"__id": "MODEL_A/__0", "__type": "MODEL_A/A", "name": None}] == element_list_nice(model)
    instantiate(model, "MODEL_B/B")
    
def operation_MAN_0(model):
    # Check if both are present
    lst = element_list_nice(model)
    assert len(lst) == 6
    assert {"__id": "MODEL_A/String", "__type": "SimpleAttribute", "name": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_A/A", "__type": "Class", "name": "A", "lower_cardinality": None, "upper_cardinality": None, "abstract": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_A/A_name", "__type": "AttributeLink", "__source": "MODEL_A/A", "__target": "MODEL_A/String", "name": "name", "optional": False, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_B/String", "__type": "SimpleAttribute", "name": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_B/B", "__type": "Class", "name": "B", "lower_cardinality": None, "upper_cardinality": None, "abstract": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_B/B_name", "__type": "AttributeLink", "__source": "MODEL_B/B", "__target": "MODEL_B/String", "name": "name", "optional": False, "constraint": {"AL": ""}} in lst

    # Do minor merge operation
    instantiate(model, "Association", edge=("MODEL_A/A", "MODEL_B/B"), ID="trace")

    # Check again
    lst = element_list_nice(model)
    assert len(lst) == 7
    assert {"__id": "MODEL_A/String", "__type": "SimpleAttribute", "name": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_A/A", "__type": "Class", "name": "A", "lower_cardinality": None, "upper_cardinality": None, "abstract": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_A/A_name", "__type": "AttributeLink", "__source": "MODEL_A/A", "__target": "MODEL_A/String", "name": "name", "optional": False, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_B/String", "__type": "SimpleAttribute", "name": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_B/B", "__type": "Class", "name": "B", "lower_cardinality": None, "upper_cardinality": None, "abstract": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_B/B_name", "__type": "AttributeLink", "__source": "MODEL_B/B", "__target": "MODEL_B/String", "name": "name", "optional": False, "constraint": {"AL": ""}} in lst
    assert {"__id": "trace", "__type": "Association", "__source": "MODEL_A/A", "__target": "MODEL_B/B", "name": None, "source_lower_cardinality": None, "source_upper_cardinality": None, "target_lower_cardinality": None, "target_upper_cardinality": None, "abstract": None, "constraint": {"AL": ""}, "lower_cardinality": None, "upper_cardinality": None} in lst

def operation_MAN_1(model):
    # Check if both are present
    lst = element_list_nice(model)
    assert len(lst) == 6
    assert {"__id": "MODEL_A/String", "__type": "SimpleAttribute", "name": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_A/A", "__type": "Class", "name": "A", "lower_cardinality": None, "upper_cardinality": None, "abstract": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_A/A_name", "__type": "AttributeLink", "__source": "MODEL_A/A", "__target": "MODEL_A/String", "name": "name", "optional": False, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_B/String", "__type": "SimpleAttribute", "name": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_B/B", "__type": "Class", "name": "B", "lower_cardinality": None, "upper_cardinality": None, "abstract": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_B/B_name", "__type": "AttributeLink", "__source": "MODEL_B/B", "__target": "MODEL_B/String", "name": "name", "optional": False, "constraint": {"AL": ""}} in lst
            
def operation_MT_0(model):
    # Check if both are present
    lst = element_list_nice(model)
    assert len(lst) == 6
    assert {"__id": "MODEL_A/String", "__type": "SimpleAttribute", "name": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_A/A", "__type": "Class", "name": "A", "lower_cardinality": None, "upper_cardinality": None, "abstract": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_A/A_name", "__type": "AttributeLink", "__source": "MODEL_A/A", "__target": "MODEL_A/String", "name": "name", "optional": False, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_B/String", "__type": "SimpleAttribute", "name": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_B/B", "__type": "Class", "name": "B", "lower_cardinality": None, "upper_cardinality": None, "abstract": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_B/B_name", "__type": "AttributeLink", "__source": "MODEL_B/B", "__target": "MODEL_B/String", "name": "name", "optional": False, "constraint": {"AL": ""}} in lst

    # Do minor merge operation
    instantiate(model, "Association", edge=("MODEL_A/A", "MODEL_B/B"), ID="trace")

    # Check again
    lst = element_list_nice(model)
    assert len(lst) == 7
    assert {"__id": "MODEL_A/String", "__type": "SimpleAttribute", "name": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_A/A", "__type": "Class", "name": "A", "lower_cardinality": None, "upper_cardinality": None, "abstract": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_A/A_name", "__type": "AttributeLink", "__source": "MODEL_A/A", "__target": "MODEL_A/String", "name": "name", "optional": False, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_B/String", "__type": "SimpleAttribute", "name": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_B/B", "__type": "Class", "name": "B", "lower_cardinality": None, "upper_cardinality": None, "abstract": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_B/B_name", "__type": "AttributeLink", "__source": "MODEL_B/B", "__target": "MODEL_B/String", "name": "name", "optional": False, "constraint": {"AL": ""}} in lst
    assert {"__id": "trace", "__type": "Association", "__source": "MODEL_A/A", "__target": "MODEL_B/B", "name": None, "source_lower_cardinality": None, "source_upper_cardinality": None, "target_lower_cardinality": None, "target_upper_cardinality": None, "abstract": None, "constraint": {"AL": ""}, "lower_cardinality": None, "upper_cardinality": None} in lst

def operation_MT_1(model):
    # Check if both are present
    lst = element_list_nice(model)
    assert len(lst) == 6
    assert {"__id": "MODEL_A/String", "__type": "SimpleAttribute", "name": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_A/A", "__type": "Class", "name": "A", "lower_cardinality": None, "upper_cardinality": None, "abstract": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_A/A_name", "__type": "AttributeLink", "__source": "MODEL_A/A", "__target": "MODEL_A/String", "name": "name", "optional": False, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_B/String", "__type": "SimpleAttribute", "name": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_B/B", "__type": "Class", "name": "B", "lower_cardinality": None, "upper_cardinality": None, "abstract": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_B/B_name", "__type": "AttributeLink", "__source": "MODEL_B/B", "__target": "MODEL_B/String", "name": "name", "optional": False, "constraint": {"AL": ""}} in lst

def operation_AL_0(model):
    # Check if both are present
    lst = element_list_nice(model)
    assert len(lst) == 6
    assert {"__id": "MODEL_A/String", "__type": "SimpleAttribute", "name": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_A/A", "__type": "Class", "name": "A", "lower_cardinality": None, "upper_cardinality": None, "abstract": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_A/A_name", "__type": "AttributeLink", "__source": "MODEL_A/A", "__target": "MODEL_A/String", "name": "name", "optional": False, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_B/String", "__type": "SimpleAttribute", "name": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_B/B", "__type": "Class", "name": "B", "lower_cardinality": None, "upper_cardinality": None, "abstract": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_B/B_name", "__type": "AttributeLink", "__source": "MODEL_B/B", "__target": "MODEL_B/String", "name": "name", "optional": False, "constraint": {"AL": ""}} in lst

    # Do minor merge operation
    instantiate(model, "Association", edge=("MODEL_A/A", "MODEL_B/B"), ID="trace")

    # Check again
    lst = element_list_nice(model)
    assert len(lst) == 7
    assert {"__id": "MODEL_A/String", "__type": "SimpleAttribute", "name": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_A/A", "__type": "Class", "name": "A", "lower_cardinality": None, "upper_cardinality": None, "abstract": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_A/A_name", "__type": "AttributeLink", "__source": "MODEL_A/A", "__target": "MODEL_A/String", "name": "name", "optional": False, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_B/String", "__type": "SimpleAttribute", "name": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_B/B", "__type": "Class", "name": "B", "lower_cardinality": None, "upper_cardinality": None, "abstract": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_B/B_name", "__type": "AttributeLink", "__source": "MODEL_B/B", "__target": "MODEL_B/String", "name": "name", "optional": False, "constraint": {"AL": ""}} in lst
    assert {"__id": "trace", "__type": "Association", "__source": "MODEL_A/A", "__target": "MODEL_B/B", "name": None, "source_lower_cardinality": None, "source_upper_cardinality": None, "target_lower_cardinality": None, "target_upper_cardinality": None, "abstract": None, "constraint": {"AL": ""}, "lower_cardinality": None, "upper_cardinality": None} in lst

def operation_AL_1(model):
    # Check if both are present
    lst = element_list_nice(model)
    assert len(lst) == 6
    assert {"__id": "MODEL_A/String", "__type": "SimpleAttribute", "name": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_A/A", "__type": "Class", "name": "A", "lower_cardinality": None, "upper_cardinality": None, "abstract": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_A/A_name", "__type": "AttributeLink", "__source": "MODEL_A/A", "__target": "MODEL_A/String", "name": "name", "optional": False, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_B/String", "__type": "SimpleAttribute", "name": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_B/B", "__type": "Class", "name": "B", "lower_cardinality": None, "upper_cardinality": None, "abstract": None, "constraint": {"AL": ""}} in lst
    assert {"__id": "MODEL_B/B_name", "__type": "AttributeLink", "__source": "MODEL_B/B", "__target": "MODEL_B/String", "name": "name", "optional": False, "constraint": {"AL": ""}} in lst

model_hierarchy = \
            {"formalisms/": {"SimpleClassDiagrams": {},
                             "TypeMapping": {},
                             "Tracability": {},
                             "ProcessModel": {},
                             "ActionLanguage": {},
                             "ManualOperation": {},
                             "Bottom": {},
                            },
            "models/": {},
            "administration/": {"core": {},
                                "CoreFormalism": {},
                               },
            "users/": {"admin/": {},
                       "LoLA/": {},
                       "HUTN/": {},
                       "files/": {},
                       "DEVS/": {},
                       "DEVS_batch/": {},
                       "JSON/": {},
                      },
            "type mappings/": {"1": {},
                               "2": {},
                               "3": {},
                               "4": {},
                               "5": {},
                               "6": {},
                               "7": {},
                               "8": {},
                               "9": {},
                              },
            "merged/": {},
            "tmp/": {},
            "RAMified/": {},
            }

def verify_clean():
    compare_locations("", set())
    compare_locations("models", set())
    compare_locations("formalisms", set())

def get_model_list(location):
    try:
        location_parts = location.split("/")
        current = model_hierarchy
        while location_parts:
            l = location_parts.pop(0)
            if l != "":
                current = current[l + "/"]
        return set(current.keys())
    except:
        return set([])

def compare_locations(location, extra_to_default):
    assert model_list(location) == get_model_list(location) | set(extra_to_default)

def compare_unordered_lists(got, expected):
    assert len(got) == len(expected)
    for i in got:
        assert i in expected
    for i in expected:
        assert i in got

class TestModelverse(unittest.TestCase):
    proc = None

    def runTest(self):
        pass

    @classmethod
    def setUpClass(self):
        TestModelverse.proc, address = start_mvc()
        init(address)
        login("user", "user")

    @classmethod
    def tearDownClass(self):
        try:
            kill(TestModelverse.proc)
        except:
            print("Got exception during teardown.")

    def setUp(self):
        verify_clean()
        folder_create("users/user/test")

    def tearDown(self):
        model_delete("users/user/test")
        verify_clean()
        reset_context()

    def test_op_model_list(self):
        assert model_list("") == set(["formalisms/",
                                      "models/",
                                      "administration/",
                                      "type mappings/",
                                      "users/",
                                      "merged/",
                                      "RAMified/",
                                      "tmp/",
                                     ])
        assert model_list("formalisms") == set(["SimpleClassDiagrams",
                                                "ActionLanguage",
                                                "TypeMapping",
                                                "Tracability",
                                                "ProcessModel",
                                                "ManualOperation",
                                                "Bottom",
                                               ])
        assert model_list("formalisms/") == set(["SimpleClassDiagrams",
                                                 "ActionLanguage",
                                                 "TypeMapping",
                                                 "Tracability",
                                                 "ProcessModel",
                                                 "ManualOperation",
                                                 "Bottom",
                                                ])

        # Try unreadable location
        try:
            model_list("administration")
            self.fail()
        except ReadPermissionDenied:
            pass

        # Try non-existing location
        try:
            model_list("adfjafdla")
            self.fail()
        except UnknownLocation:
            pass

        # Try model list of a model itself
        assert model_list("formalisms/SimpleClassDiagrams") == set([])

        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams")
        model_add("users/user/test/b", "formalisms/SimpleClassDiagrams")
        model_add("users/user/test/c", "formalisms/SimpleClassDiagrams")
        assert model_list("users/user/test") == set(["a", "b", "c"])
        assert model_list("users/user/test/") == set(["a", "b", "c"])

    def test_op_model_list_full(self):
        assert model_list_full("") == set([("formalisms/", "admin", "admin", "221"),
                                           ("models/", "admin", "admin", "221"),
                                           ("administration/", "admin", "admin", "110"),
                                           ("type mappings/", "admin", "admin", "221"),
                                           ("users/", "admin", "admin", "221"),
                                           ("merged/", "admin", "nobody", "222"),
                                           ("RAMified/", "admin", "nobody", "222"),
                                           ("tmp/", "admin", "nobody", "222"),
                                          ])
        assert model_list_full("formalisms") == set([("SimpleClassDiagrams", "admin", "admin", "221"),
                                                     ("ActionLanguage", "admin", "admin", "221"),
                                                     ("TypeMapping", "admin", "admin", "221"),
                                                     ("Tracability", "admin", "admin", "221"),
                                                     ("ProcessModel", "admin", "admin", "221"),
                                                     ("ManualOperation", "admin", "admin", "221"),
                                                     ("Bottom", "admin", "admin", "221"),
                                                    ])

        # Try unreadable location
        try:
            model_list_full("administration")
            self.fail()
        except ReadPermissionDenied:
            pass

        # Try non-existing location
        try:
            model_list_full("adfjafdla")
            self.fail()
        except UnknownLocation:
            pass

        # Try model list of a model itself
        assert model_list_full("formalisms/SimpleClassDiagrams") == set([])

    def test_op_verify(self):
        # Verify M3 --> M3
        assert verify("formalisms/SimpleClassDiagrams", "formalisms/SimpleClassDiagrams") == "OK"
        # Verify with bottom
        assert verify("formalisms/SimpleClassDiagrams", "formalisms/Bottom") == "OK"
        # Verify the type mapping models
        for tm in model_types("formalisms/SimpleClassDiagrams"):
            assert verify(tm[1], "formalisms/TypeMapping") == "OK"

        # Verify wrong
        try:
            verify("formalisms/SimpleClassDiagrams", "formalisms/ProcessModel")
            self.fail()
        except UnknownMetamodellingHierarchy:
            pass

        # Verify M2 --> M3
        assert verify("formalisms/ProcessModel", "formalisms/SimpleClassDiagrams") == "OK"
        assert verify("formalisms/ProcessModel", "formalisms/Bottom") == "OK"
        for tm in model_types("formalisms/ProcessModel"):
            assert verify(tm[1], "formalisms/TypeMapping") == "OK"
        try:
            verify("formalisms/ProcessModel", "formalisms/ProcessModel")
            self.fail()
        except UnknownMetamodellingHierarchy:
            pass

        # Verify M1 --> M2
        model_add("users/user/test/a", "formalisms/ProcessModel")
        # Fails with lacking instances of Start and Finish class
        assert verify("users/user/test/a", "formalisms/ProcessModel").startswith("Lower cardinality violated for class: ")
        instantiate("users/user/test/a", "Start")
        # Fails with lacking instances of Finish class
        assert verify("users/user/test/a", "formalisms/ProcessModel") == "Lower cardinality violated for class: Finish"
        instantiate("users/user/test/a", "Finish")
        # Succeeds
        assert verify("users/user/test/a", "formalisms/ProcessModel") == "OK"

        # Test unreadable model
        try:
            verify("administration/core", "formalisms/ProcessModel")
            self.fail()
        except ReadPermissionDenied:
            pass

        # Test unreadable metamodel
        try:
            verify("formalisms/Bottom", "administration/core")
            self.fail()
        except ReadPermissionDenied:
            pass

        # Test non-existing model
        try:
            verify("adfadf", "formalisms/SimpleClassDiagrams")
            self.fail()
        except UnknownModel:
            pass

        # Test non-existing metamodel
        try:
            verify("formalisms/SimpleClassDiagrams", "adfka")
            self.fail()
        except UnknownModel:
            pass

        # Test verify of a folder (model)
        try:
            verify("users", "formalisms/SimpleClassDiagrams")
            self.fail()
        except NotAModel:
            pass

        # Test verify of a folder (metamodel)
        try:
            verify("users/user/test/a", "users")
            self.fail()
        except NotAModel:
            pass

    def test_op_permission_modify(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams")
        assert model_list_full("users/user/test") == set([("a", "user", "nobody", "200")])

        # Check that we can read
        element_list_nice("users/user/test/a")
        # Check that we can write
        instantiate("users/user/test/a", "Class")

        assert permission_modify("users/user/test/a", "100") == None
        assert model_list_full("users/user/test") == set([("a", "user", "nobody", "100")])
        # Check that we can read
        element_list_nice("users/user/test/a")
        # Check that we can't write
        try:
            instantiate("users/user/test/a", "Class")
            self.fail()
        except WritePermissionDenied:
            pass

        assert permission_modify("users/user/test/a", "000") == None
        assert model_list_full("users/user/test") == set([("a", "user", "nobody", "000")])
        # Check that we can't read
        try:
            element_list_nice("users/user/test/a")
            self.fail()
        except ReadPermissionDenied:
            pass
        # Check that we can't write
        try:
            instantiate("users/user/test/a", "Class")
            self.fail()
        except WritePermissionDenied:
            pass
        except ReadPermissionDenied:
            pass

        assert permission_modify("users/user/test/a", "200") == None
        assert model_list_full("users/user/test") == set([("a", "user", "nobody", "200")])

        # Check that we can read
        element_list_nice("users/user/test/a")
        # Check that we can write
        instantiate("users/user/test/a", "Class")

        # Cannot modify permissions if we don't own the model
        try:
            permission_modify("formalisms/SimpleClassDiagrams", "222")
            self.fail()
        except UserPermissionDenied:
            pass

        # Cannot modify permissions even if we have full access, as long as we are not the owner
        model_add("users/user/test/z", "formalisms/SimpleClassDiagrams")
        permission_owner("users/user/test/z", "admin")
        try:
            permission_modify("users/user/test/z", "222")
            self.fail()
        except UserPermissionDenied:
            pass
        try:
            permission_owner("users/user/test/z", "user")
            self.fail()
        except UserPermissionDenied:
            pass

        # Cannot change non-existing model
        before = model_list("")
        try:
            permission_modify("adfadf", "111")
            self.fail()
        except UnknownModel:
            assert model_list("") == before

        # Check for type of permission string
        # Too long
        try:
            permission_modify("users/user/test/a", "1111")
            self.fail()
        except IncorrectFormat:
            assert [i[3] for i in model_list_full("users/user/test/") if i[0] == "a"][0] == "200"

        # Above 2
        try:
            permission_modify("users/user/test/a", "131")
            self.fail()
        except IncorrectFormat:
            assert [i[3] for i in model_list_full("users/user/test/") if i[0] == "a"][0] == "200"

        # Below 0
        try:
            permission_modify("users/user/test/a", "-111")
            self.fail()
        except IncorrectFormat:
            assert [i[3] for i in model_list_full("users/user/test/") if i[0] == "a"][0] == "200"

        # Non-integer
        try:
            permission_modify("users/user/test/a", "2a1")
            self.fail()
        except IncorrectFormat:
            assert [i[3] for i in model_list_full("users/user/test/") if i[0] == "a"][0] == "200"

    def test_op_model_add(self):
        assert model_list_full("users/user/test") == set([])
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams")
        # Test permissions of newly created empty model
        assert model_list_full("users/user/test") == set([("a", "user", "nobody", "200")])

        model_add("users/user/test/b", "formalisms/SimpleClassDiagrams", \
            """
            Class A {
                name = "A"
            }
            Class B {
                name = "B"
                abstract = True
            }
            """)

        # Test permissions of newly created non-empty model
        assert model_list_full("users/user/test") == set([("a", "user", "nobody", "200"), ("b", "user", "nobody", "200")])
        # Test content of newly created model with code upload
        lst = element_list_nice("users/user/test/b")
        assert len(lst) == 2
        assert {'__id': "A", "__type": "Class", "abstract": None, "constraint": {"AL": ""}, "lower_cardinality": None, "upper_cardinality": None, "name": "A"} in lst
        assert {'__id': "B", "__type": "Class", "abstract": True, "constraint": {"AL": ""}, "lower_cardinality": None, "upper_cardinality": None, "name": "B"} in lst

        try:
            # Cannot upload model to unwritable location at root
            model_add("z", "formalisms/SimpleClassDiagrams")
            self.fail()
        except WritePermissionDenied:
            assert model_list_full("users/user/test") == set([("a", "user", "nobody", "200"), ("b", "user", "nobody", "200")])

        try:
            # Cannot upload model to unwritable location somewhere in hierarchy
            model_add("users/new_model", "formalisms/SimpleClassDiagrams")
            self.fail()
        except WritePermissionDenied:
            assert "new_model/" not in model_list("users/")

        try:
            # Cannot upload model to unwritable location somewhere in hierarchy with multiple new branches
            model_add("users/new_username/new_model", "formalisms/SimpleClassDiagrams")
            self.fail()
        except WritePermissionDenied:
            assert "new_username/" not in model_list("users/")

        model_add("users/user/test/c", "users/user/test/b", """
            A a {}
            """)
        try:
            # Cannot add this model, as it cannot be auto-typecasted to formalisms/SimpleClassDiagrams
            model_add("users/user/test/d", "users/user/test/c", """
                a c {}
                """)
            self.fail()
        except UnknownM3:
            assert model_list("users/user/test") == set(["a", "b", "c"])

        # We can add this model for multiple levels, as it can be automatically typecasted to formalisms/SimpleClassDiagrams, since it is empty
        model_add("users/user/test/d", "users/user/test/b")
        model_add("users/user/test/e", "users/user/test/d")
        model_add("users/user/test/f", "users/user/test/e")

        # Cannot instantiate a folder metamodel
        try:
            model_add("users/user/test/z", "users")
            self.fail()
        except NotAModel:
            assert "z" not in model_list("users/user/test")

    def test_op_model_add_compilation_error(self):
        #try:
        #    model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
        #        abdfaefeaf
        #        """)
        #    self.fail()
        #except CompilationError:
        #    pass

        #try:
        #    model_add("users/user/test/b", "formalisms/SimpleClassDiagrams", """
        #        Crass A {
        #            name = "b"
        #        }
        #        """)
        #    #TODO at the moment, this still succeeds!
        #    self.fail()
        #except:
        #    raise
        pass

    def test_op_model_move(self):
        # Test basic scenario
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams")
        assert model_list_full("users/user/test") == set([("a", "user", "nobody", "200")])
        assert model_move("users/user/test/a", "users/user/test/b") == None
        assert model_list_full("users/user/test") == set([("b", "user", "nobody", "200")])

        # Test if permissions are copied as well
        permission_modify("users/user/test/b", "222")
        assert model_list_full("users/user/test") == set([("b", "user", "nobody", "222")])
        assert model_move("users/user/test/b", "users/user/test/a") == None
        assert model_list_full("users/user/test") == set([("a", "user", "nobody", "222")])

        model_add("users/user/test/b", "formalisms/SimpleClassDiagrams")
        assert model_list_full("users/user/test") == set([("a", "user", "nobody", "222"), ("b", "user", "nobody", "200")])
        try:
            model_move("users/user/test/a", "users/user/test/b")
            self.fail()
        except LocationExists:
            pass
        assert model_list_full("users/user/test") == set([("a", "user", "nobody", "222"), ("b", "user", "nobody", "200")])

        # Test move if source doesn't exist
        try:
            model_move("users/user/test/z", "users/user/test/y")
            self.fail()
        except UnknownModel:
            pass

        # Test if we can move a model we can't write to (i.e., not allowed to remove it)
        try:
            model_move("formalisms/ProcessModel", "users/user/test/x")
            self.fail()
        except WritePermissionDenied:
            pass

        # Test if we can move a model to a place we can't write to
        try:
            model_move("users/user/test/a", "administration/ProcessModel2")
            self.fail()
        except WritePermissionDenied:
            pass

        # Test moving an entire folder
        folder_create("users/user/test/folder_a")
        model_add("users/user/test/folder_a/a", "formalisms/SimpleClassDiagrams")
        model_add("users/user/test/folder_a/b", "formalisms/SimpleClassDiagrams")
        model_add("users/user/test/folder_a/c", "formalisms/SimpleClassDiagrams")
        all_models = model_list_full("users/user/test/folder_a")
        model_move("users/user/test/folder_a", "users/user/test/folder_b")
        assert model_list_full("users/user/test/folder_b") == all_models

    def test_op_model_delete(self):
        # Test basic delete
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams")
        assert model_list_full("users/user/test") == set([("a", "user", "nobody", "200")])
        assert model_delete("users/user/test/a") == None
        assert model_list_full("users/user/test") == set([])

        # Test delete of non-existing
        assert model_list_full("users/user/test") == set([])
        try:
            model_delete("users/user/test/a")
            self.fail()
        except UnknownModel:
            pass

        # Test delete of non-writable models
        try:
            model_delete("formalisms/SimpleClassDiagrams")
            self.fail()
        except WritePermissionDenied:
            pass

        # Try delete of a folder
        folder_create("users/user/test/b/c/d")
        model_add("users/user/test/b/c/d/e", "formalisms/SimpleClassDiagrams")
        assert model_list("users/user/test") == set(["b/"])
        assert model_list("users/user/test/b") == set(["c/"])
        assert model_list("users/user/test/b/c") == set(["d/"])
        assert model_list("users/user/test/b/c/d") == set(["e"])
        assert model_delete("users/user/test/b/c") == None
        # Removes recursively all subfolders and containing models
        assert model_list("users/user/test") == set(["b/"])
        assert model_list("users/user/test/b") == set([])
        try:
            model_list("users/user/test/b/c")
            self.fail()
        except UnknownLocation:
            pass
        try:
            element_list("users/user/test/b/c/d/e")
            self.fail()
        except UnknownModel:
            pass

    def test_op_model_overwrite(self):
        # Test overwrite of simple model
        assert model_list_full("users/user/test") == set([])
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams")
        # Test permissions of newly created empty model
        assert model_list_full("users/user/test") == set([("a", "user", "nobody", "200")])
        assert element_list_nice("users/user/test/a") == []

        model_overwrite("users/user/test/a", \
            """
            Class A {
                name = "A"
            }
            Class B {
                name = "B"
                abstract = True
            }
            """)

        # Test permissions of newly created non-empty model
        assert model_list_full("users/user/test") == set([("a", "user", "nobody", "200")])
        # Test content of newly created model with code upload
        lst = element_list_nice("users/user/test/a")
        assert len(lst) == 2
        assert {'__id': "A", "__type": "Class", "abstract": None, "constraint": {"AL": ""}, "lower_cardinality": None, "upper_cardinality": None, "name": "A"} in lst
        assert {'__id': "B", "__type": "Class", "abstract": True, "constraint": {"AL": ""}, "lower_cardinality": None, "upper_cardinality": None, "name": "B"} in lst

        # Test non-writable model
        before = element_list("formalisms/SimpleClassDiagrams")
        try:
            model_overwrite("formalisms/SimpleClassDiagrams", "")
            self.fail()
        except WritePermissionDenied:
            # No changes were made
            assert element_list("formalisms/SimpleClassDiagrams") == before

        # Test non-existing model
        assert "adfafd" not in model_list("")
        try:
            model_overwrite("adfafd", "")
            self.fail()
        except UnknownModel:
            assert "adfafd" not in model_list("")

        # Test compilation error
        before = element_list_nice("users/user/test/a")
        try:
            model_overwrite("users/user/test/a", "afajk")
            self.fail()
        except CompilationError:
            assert element_list_nice("users/user/test/a") == before

        # Test overwrite of non-model location
        before = model_list("users/user/test")
        try:
            model_overwrite("users/user/test", "")
            self.fail()
        except NotAModel:
            assert model_list("users/user/test") == before

    def test_op_user_logout(self):
        try:
            element_list_nice("administration/core")
            self.fail()
        except ReadPermissionDenied:
            pass

        user_logout()
        login("admin", "admin")

        # Test user permissions
        assert len(element_list_nice("administration/core")) > 0

        user_logout()
        login("user", "user")

        try:
            element_list_nice("administration/core")
            self.fail()
        except ReadPermissionDenied:
            pass
    
    def test_op_all_instances(self):
        # Test M2 level
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            Class A {}
            Class B : A {}
            Association C (A, B) {}
            """)
        assert all_instances("users/user/test/a", "Class") == set(["A", "B", "C"])
        assert all_instances("users/user/test/a", "Association") == set(["C"])

        # Test M1 level
        model_add("users/user/test/b", "users/user/test/a", """
            A a {}
            B b {}
            C c(a, b) {}
            A d {}
            """)
        assert all_instances("users/user/test/b", "A") == set(["a", "b", "d"])
        assert all_instances("users/user/test/b", "B") == set(["b"])
        assert all_instances("users/user/test/b", "C") == set(["c"])

        # Test non-existing type
        try:
            all_instances("users/user/test/a", "non-existing")
            self.fail()
        except UnknownElement:
            pass

        # Test non-existing model
        try:
            all_instances("users/user/test/c", "Class")
            self.fail()
        except UnknownModel:
            pass

        # No read permission
        try:
            all_instances("administration/core", "Class")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permission, but can query
        assert type(all_instances("formalisms/SimpleClassDiagrams", "Class")) == set

        # Check with "folder" model
        try:
            all_instances("users", "a")
            self.fail()
        except NotAModel:
            pass

    def test_op_admin_promote_demote(self):
        # Verify that user doesn't have admin permissions
        try:
            element_list("administration/core")
            self.fail()
        except ReadPermissionDenied:
            pass

        # Cannot do admin_promote ourselves
        try:
            admin_promote("user")
        except AdminPermissionDenied:
            pass

        # Cannot do admin_demote ourselves
        try:
            admin_demote("user")
        except AdminPermissionDenied:
            pass

        # Switch to admin for now
        self.do_as_user("admin", admin_promote, ["user"])

        # Verify that we can do admin operations
        assert len(element_list("administration/core")) > 0

        # And check that we can promote/demote other users
        admin_promote("HUTN")
        admin_demote("HUTN")

        # Unknown user
        try:
            admin_promote("user3")
            self.fail()
        except UnknownUser:
            pass

        try:
            admin_demote("user4")
            self.fail()
        except UnknownUser:
            pass

        # Try with folders
        try:
            admin_promote("users")
            self.fail()
        except UnknownUser:
            pass

        try:
            admin_demote("users")
            self.fail()
        except UnknownUser:
            pass

        # Can also demote ourselves again
        admin_demote("user")

        # Verify that user doesn't have admin permissions
        try:
            element_list("administration/core")
            self.fail()
        except ReadPermissionDenied:
            pass

    def test_op_user_password(self):
        # Test initial password
        user_logout()
        try:
            login("user", "user2")
            self.fail()
        except PermissionDenied:
            pass
        login("user", "user")

        # Change it to user2
        user_password("user", "user2")
        user_logout()
        try:
            login("user", "user")
            self.fail()
        except PermissionDenied:
            pass
        login("user", "user2")

        # Reset for further tests
        user_password("user", "user")

        # Change other users password
        try:
            user_password("user2", "user10")
            self.fail()
        except UserPermissionDenied:
            user_logout()
            login("user2", "user2")
            user_logout()
            login("user", "user")

        # But admin can do that
        self.do_as_user("admin", user_password, ["user2", "user10"])
        user_logout()
        login("user2", "user10")
        user_logout()
        login("user", "user")

        # Admin cannot change password of non-existing user
        try:
            self.do_as_user("admin", user_password, ["user10", "user10"])
            self.fail()
        except UnknownUser:
            user_logout()
            # Check that we can still create the new user with whatever password we want
            login("user10", "user11")
            user_logout()
            login("user", "user")

        # Check on folder "model"
        try:
            self.do_as_user("admin", user_password, ["users", "users2"])
            self.fail()
        except UnknownUser:
            pass

    def test_op_model_types(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams")

        # Read model types of simple model
        lst = model_types("users/user/test/a")
        assert len(lst) == 1
        entry = lst.pop()
        assert entry[0] == "formalisms/SimpleClassDiagrams"
        assert entry[1].startswith("type mappings/")
        assert entry[2] == None

        # Create bottom and query afterwards
        alter_context("users/user/test/a", "formalisms/Bottom")
        element_list_nice("users/user/test/a")
        alter_context("users/user/test/a", "formalisms/SimpleClassDiagrams")

        lst = model_types("users/user/test/a")
        assert len(lst) == 2
        got = set()
        while lst:
            entry = lst.pop()
            assert entry[0] in ["formalisms/SimpleClassDiagrams", "formalisms/Bottom"]
            assert entry[0] not in got
            got.add(entry[0])
            assert entry[1].startswith("type mappings/")
            assert entry[2] == None

        # No type mapping for type mappings
        assert model_types(entry[1]) == set([("formalisms/TypeMapping", None, None)])

        # But there will be as soon as we open it
        element_list_nice(entry[1])
        lst = model_types(entry[1])
        assert len(lst) == 1
        entry = lst.pop()
        assert entry[0] == "formalisms/TypeMapping"
        assert entry[1].startswith("type mappings/")
        assert entry[2] == None

        # Test non-existing model
        try:
            model_types("adfadf")
            self.fail()
        except UnknownModel:
            pass

        # Check on folder "model"
        try:
            model_types("users")
            self.fail()
        except NotAModel:
            pass

    def test_op_folder_create(self):
        # Create a folder
        assert folder_create("users/user/test/folder_a") == None
        assert model_list_full("users/user/test/") == set([("folder_a/", "user", "nobody", "200")])

        # Create folder with trailing slash
        assert folder_create("users/user/test/folder_b/") == None
        assert model_list_full("users/user/test/") == set([("folder_a/", "user", "nobody", "200"), ("folder_b/", "user", "nobody", "200")])
        assert model_list_full("users/user/test/folder_b") == set()

        # Create multiple folders
        assert folder_create("users/user/test/folder_a/folder_b/folder_c/folder_d") == None
        assert model_list_full("users/user/test/") == set([("folder_a/", "user", "nobody", "200"), ("folder_b/", "user", "nobody", "200")])
        assert model_list_full("users/user/test/folder_a") == set([("folder_b/", "user", "nobody", "200")])
        assert model_list_full("users/user/test/folder_a/folder_b") == set([("folder_c/", "user", "nobody", "200")])
        assert model_list_full("users/user/test/folder_a/folder_b/folder_c") == set([("folder_d/", "user", "nobody", "200")])

        # Create folder in non-writable location
        try:
            folder_create("a")
            self.fail()
        except WritePermissionDenied:
            pass
        assert "a" not in model_list("") 

        # Create folder that already exists
        try:
            folder_create("users/user/test/folder_a")
            self.fail()
        except FolderExists:
            pass

    def test_op_alter_context(self):
        # Create a model that we will use
        model_add("users/user/test/a", "formalisms/ProcessModel", """
            Start start {}
            Finish finish {}
            """)

        # List elements using default context, that of creation
        lst = element_list_nice("users/user/test/a")
        assert len(lst) == 2
        assert {"__id": "start", "__type": "Start"} in lst
        assert {"__id": "finish", "__type": "Finish"} in lst

        # List elements using bottom context
        alter_context("users/user/test/a", "formalisms/Bottom")
        lst = element_list_nice("users/user/test/a")
        assert len(lst) == 2
        assert {"__id": "start", "__type": "Node"} in lst
        assert {"__id": "finish", "__type": "Node"} in lst

        # Switch back to default context
        alter_context("users/user/test/a", "formalisms/ProcessModel")
        lst = element_list_nice("users/user/test/a")
        assert len(lst) == 2
        assert {"__id": "start", "__type": "Start"} in lst
        assert {"__id": "finish", "__type": "Finish"} in lst

        # Switch to a non-existing context
        alter_context("users/user/test/a", "non-existing")
        try:
            element_list_nice("users/user/test/a")
            self.fail()
        except UnknownModel:
            pass

        # Set context for an unknown model
        # Note that this will only register locally, without communicating with the Modelverse
        # as such, errors are not noticed until the element is accessed!
        alter_context("non-existing", "formalisms/SimpleClassDiagrams")

    def test_op_element_list(self):
        model_add("users/user/test/a", "formalisms/ProcessModel", """
            Start start {}
            Finish finish {}
            Next nxt (start, finish) {}
            """)

        # Basic operation
        lst = element_list("users/user/test/a")
        assert len(lst) == 3
        assert ("start", "Start") in lst
        assert ("finish", "Finish") in lst
        assert ("nxt", "Next") in lst

        # Try on non-existing model
        try:
            element_list("a")
            self.fail()
        except UnknownModel:
            pass

        # Try a non-readable model
        try:
            element_list("administration/core")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permission, but can query
        assert type(element_list("formalisms/SimpleClassDiagrams")) == set

        # Check on folder "model"
        try:
            element_list("users")
            self.fail()
        except NotAModel:
            pass

    def test_op_element_list_nice(self):
        # Test simple element_list_nice
        model_add("users/user/test/a", "formalisms/ProcessModel", """
            Start start {}
            Finish finish {}
            Next nxt (start, finish) {}
            """)

        # Basic operation
        lst = element_list_nice("users/user/test/a")
        assert len(lst) == 3
        assert {"__id": "start", "__type": "Start"} in lst
        assert {"__id": "finish", "__type": "Finish"} in lst
        assert {"__id": "nxt", "__type": "Next", "__source": "start", "__target": "finish"} in lst

        # Test a model with attributes
        model_add("users/user/test/b", "formalisms/ProcessModel", """
            Start start {}
            Exec exec1 {
                name = "first exec"
            }
            Finish finish {}

            Data d1 {
                name = "data 1"
                type = "type 1"
            }

            Next n1 (start, exec1) {}
            Next n2 (exec1, finish) {}

            Produces p1 (exec1, d1) {
                name = "producer"
            }
            """)
        lst = element_list_nice("users/user/test/b")
        assert len(lst) == 7
        assert {"__id": "start", "__type": "Start"} in lst
        assert {"__id": "finish", "__type": "Finish"} in lst
        assert {"__id": "exec1", "__type": "Exec", "name": "first exec"} in lst
        assert {"__id": "d1", "__type": "Data", "name": "data 1", "type": "type 1"} in lst
        assert {"__id": "n1", "__type": "Next", "__source": "start", "__target": "exec1"} in lst
        assert {"__id": "n2", "__type": "Next", "__source": "exec1", "__target": "finish"} in lst
        assert {"__id": "p1", "__type": "Produces", "__source": "exec1", "__target": "d1", "name": "producer"} in lst

        # Try one with defined attributes
        model_add("users/user/test/c", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute Natural {
                name = "natural"
            }
            SimpleAttribute String {
                name = "string"
            }

            Class place {
                name = "Place"
                name : String
                capacity : Natural
            }
            Class transition {
                name = "Transition"
                name : String
            }
            Association p2t (place, transition) {
                name = "P2T"
                weight : Natural
            }
            Association t2p (transition, place) {
                name = "T2P"
                weight : Natural
            }
            """)

        lst = element_list_nice("users/user/test/c")
        assert len(lst) == 11
        assert {"__id": "Natural", "__type": "SimpleAttribute", "constraint": {"AL": ""}, "name": "natural"} in lst
        assert {"__id": "String", "__type": "SimpleAttribute", "constraint": {"AL": ""}, "name": "string"} in lst
        assert {"__id": "place", "name": "Place", "__type": "Class", "constraint": {"AL": ""}, "lower_cardinality": None, "upper_cardinality": None, "abstract": None} in lst
        assert {"__id": "transition", "name": "Transition", "__type": "Class", "constraint": {"AL": ""}, "lower_cardinality": None, "upper_cardinality": None, "abstract": None} in lst
        assert {"__id": "p2t", "name": "P2T", "__type": "Association", "__source": "place", "__target": "transition", "constraint": {"AL": ""}, "source_lower_cardinality": None, "source_upper_cardinality": None, "target_lower_cardinality": None, "target_upper_cardinality": None, "abstract": None, "lower_cardinality": None, "upper_cardinality": None} in lst
        assert {"__id": "t2p", "name": "T2P", "__type": "Association", "__source": "transition", "__target": "place", "constraint": {"AL": ""}, "source_lower_cardinality": None, "source_upper_cardinality": None, "target_lower_cardinality": None, "target_upper_cardinality": None, "abstract": None, "lower_cardinality": None, "upper_cardinality": None} in lst
        assert {"__id": "place_name", "__type": "AttributeLink", "__source": "place", "__target": "String", "name": "name", "optional": False, "constraint": {"AL": ""}} in lst
        assert {"__id": "place_capacity", "__type": "AttributeLink", "__source": "place", "__target": "Natural", "name": "capacity", "optional": False, "constraint": {"AL": ""}} in lst
        assert {"__id": "transition_name", "__type": "AttributeLink", "__source": "transition", "__target": "String", "name": "name", "optional": False, "constraint": {"AL": ""}} in lst
        assert {"__id": "p2t_weight", "__type": "AttributeLink", "__source": "p2t", "__target": "Natural", "name": "weight", "optional": False, "constraint": {"AL": ""}} in lst
        assert {"__id": "t2p_weight", "__type": "AttributeLink", "__source": "t2p", "__target": "Natural", "name": "weight", "optional": False, "constraint": {"AL": ""}} in lst

        # Try on non-existing model
        try:
            element_list_nice("a")
            self.fail()
        except UnknownModel:
            pass

        # Try a non-readable model
        try:
            element_list_nice("administration/core")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permission, but can query
        assert type(element_list_nice("formalisms/SimpleClassDiagrams")) == list

        # Check on folder "model"
        try:
            element_list_nice("users")
            self.fail()
        except NotAModel:
            pass

    def test_op_types(self):
        # Try operation of unopened model
        assert types("formalisms/ProcessModel") == element_list("formalisms/SimpleClassDiagrams")

        # Try simple operation
        model_add("users/user/test/a", "formalisms/ProcessModel", """
            Start start {}
            Finish finish {}
            Next nxt (start, finish) {}
            """)

        assert types("users/user/test/a") == element_list("formalisms/ProcessModel")
        alter_context("users/user/test/a", "formalisms/Bottom")
        assert types("users/user/test/a") == element_list("formalisms/Bottom")

        # Try for model that we cannot read
        try:
            types("administration/core")
            self.fail()
        except ReadPermissionDenied:
            pass
        
        # Try for non-existing model
        try:
            types("a")
            self.fail()
        except UnknownModel:
            pass

        # Try for a metamodel that we are not allowed to read, although the model may be read
        model_add("users/user/test/b", "formalisms/SimpleClassDiagrams")
        assert types("users/user/test/b") == element_list("formalisms/SimpleClassDiagrams")
        alter_context("users/user/test/b", "administration/core")
        try:
            types("users/user/test/b")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permission for either M or MM, but can query
        assert type(types("formalisms/SimpleClassDiagrams")) == set

        # Check on folder "model"
        try:
            types("users")
            self.fail()
        except NotAModel:
            pass

    def test_op_read_info(self):
        # Basic case
        model_add("users/user/test/a", "formalisms/ProcessModel", """
            Start start {}
            Finish finish {}
            Next nxt (start, finish) {}
            """)

        assert read_info("users/user/test/a", "start") == ("Start", None)
        assert read_info("users/user/test/a", "finish") == ("Finish", None)
        assert read_info("users/user/test/a", "nxt") == ("Next", ("start", "finish"))

        # Read using different metamodel
        alter_context("users/user/test/a", "formalisms/Bottom")
        assert read_info("users/user/test/a", "start") == ("Node", None)
        assert read_info("users/user/test/a", "finish") == ("Node", None)
        assert read_info("users/user/test/a", "nxt") == ("Edge", ("start", "finish"))

        # Non-existing element
        try:
            read_info("users/user/test/a", "notthere")
            self.fail()
        except UnknownElement:
            pass

        # Non-existing model
        try:
            read_info("users/user/test/b", "start")
            self.fail()
        except UnknownModel:
            pass

        # No read permission
        try:
            read_info("administration/core", "administration")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permission, but can query
        assert type(read_info("formalisms/SimpleClassDiagrams", "Class")) == tuple

        # Check on folder "model"
        try:
            read_info("users", "a")
            self.fail()
        except NotAModel:
            pass

    def test_op_read_attrs(self):
        # Basic behaviour
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute Natural {
                name = "natural"
            }
            SimpleAttribute String {
                name = "string"
            }

            Class Place {
                name = "Place"
                name : String
                capacity : Natural
            }
            Class Transition {
                name = "Transition"
                name : String
            }
            Association P2T (Place, Transition) {
                name = "P2T"
                weight : Natural
            }
            Association T2P (Transition, Place) {
                name = "T2P"
                weight : Natural
            }
            """)
        assert read_attrs("users/user/test/a", "Natural") == {"constraint": {"AL": ""}, "name": "natural"}
        assert read_attrs("users/user/test/a", "String") == {"constraint": {"AL": ""}, "name": "string"}
        assert read_attrs("users/user/test/a", "Place") == {"constraint": {"AL": ""}, "name": "Place", "abstract": None, "lower_cardinality": None, "upper_cardinality": None}
        assert read_attrs("users/user/test/a", "Transition") == {"constraint": {"AL": ""}, "name": "Transition", "abstract": None, "lower_cardinality": None, "upper_cardinality": None}
        assert read_attrs("users/user/test/a", "P2T") == {"constraint": {"AL": ""}, "name": "P2T", "source_lower_cardinality": None, "source_upper_cardinality": None, "target_lower_cardinality": None, "target_upper_cardinality": None, "abstract": None, "lower_cardinality": None, "upper_cardinality": None}
        assert read_attrs("users/user/test/a", "T2P") == {"constraint": {"AL": ""}, "name": "T2P", "source_lower_cardinality": None, "source_upper_cardinality": None, "target_lower_cardinality": None, "target_upper_cardinality": None, "abstract": None, "lower_cardinality": None, "upper_cardinality": None}

        model_add("users/user/test/b", "users/user/test/a", """
            Place p1 {
                name = "place 1"
                capacity = 2
            }
            Place p2 {
                name = "place 2"
                capacity = 1
            }
            Transition t1 {
                name = "transition"
            }
            
            P2T p2t (p1, t1) {
                weight = 4
            }
            T2P t2p (t1, p2) {
                weight = 10
            }
            """)
        assert read_attrs("users/user/test/b", "p1") == {"name": "place 1", "capacity": 2}
        assert read_attrs("users/user/test/b", "p2") == {"name": "place 2", "capacity": 1}
        assert read_attrs("users/user/test/b", "t1") == {"name": "transition"}
        assert read_attrs("users/user/test/b", "p2t") == {"weight": 4}
        assert read_attrs("users/user/test/b", "t2p") == {"weight": 10}

        # Non-existing model
        try:
            read_attrs("users/afa", "p1")
            self.fail()
        except UnknownModel:
            pass

        # Non-existing element
        try:
            read_attrs("users/user/test/a", "PPPPPPP")
            self.fail()
        except UnknownElement:
            pass

        # No read permissions
        try:
            read_attrs("administration/core", "formalisms")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permissions, but can query
        assert type(read_attrs("formalisms/SimpleClassDiagrams", "Class")) == type(dict())

        # Check on folder "model"
        try:
            read_attrs("users", "a")
            self.fail()
        except NotAModel:
            pass

    def test_op_attr_assign(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute Natural {
                name = "natural"
            }
            SimpleAttribute String {
                name = "string"
            }

            Class Place {
                name = "Place"
                name : String
                capacity : Natural
            }
            Class Transition {
                name = "Transition"
                name : String
            }
            Association P2T (Place, Transition) {
                name = "P2T"
                weight : Natural
            }
            Association T2P (Transition, Place) {
                name = "T2P"
                weight : Natural
            }
            """)
        assert read_attrs("users/user/test/a", "Natural") == {"constraint": {"AL": ""}, "name": "natural"}
        assert attr_assign("users/user/test/a", "Natural", "name", "nat") == None
        assert read_attrs("users/user/test/a", "Natural") == {"constraint": {"AL": ""}, "name": "nat"}

        assert read_attrs("users/user/test/a", "Place") == {"constraint": {"AL": ""}, "name": "Place", "abstract": None, "lower_cardinality": None, "upper_cardinality": None}
        assert attr_assign("users/user/test/a", "Place", "name", "NewPlace") == None
        assert attr_assign("users/user/test/a", "Place", "abstract", False) == None
        assert attr_assign("users/user/test/a", "Place", "lower_cardinality", 4) == None
        assert read_attrs("users/user/test/a", "Place") == {"constraint": {"AL": ""}, "name": "NewPlace", "abstract": False, "lower_cardinality": 4, "upper_cardinality": None}

        assert read_attrs("users/user/test/a", "P2T") == {"constraint": {"AL": ""}, "name": "P2T", "source_lower_cardinality": None, "source_upper_cardinality": None, "target_lower_cardinality": None, "target_upper_cardinality": None, "abstract": None, "lower_cardinality": None, "upper_cardinality": None}
        assert attr_assign("users/user/test/a", "P2T", "name", "PPP") == None
        assert attr_assign("users/user/test/a", "P2T", "source_lower_cardinality", 1) == None
        assert attr_assign("users/user/test/a", "P2T", "target_upper_cardinality", 10) == None
        assert read_attrs("users/user/test/a", "P2T") == {"constraint": {"AL": ""}, "name": "PPP", "source_lower_cardinality": 1, "source_upper_cardinality": None, "target_lower_cardinality": None, "target_upper_cardinality": 10, "abstract": None, "lower_cardinality": None, "upper_cardinality": None}

        model_add("users/user/test/b", "users/user/test/a", """
            Place p1 {
                name = "place 1"
                capacity = 2
            }
            Place p2 {
                name = "place 2"
                capacity = 1
            }
            Transition t1 {
                name = "transition"
            }
            
            P2T p2t (p1, t1) {
                weight = 4
            }
            T2P t2p (t1, p2) {
                weight = 10
            }
            """)
        assert read_attrs("users/user/test/b", "p1") == {"name": "place 1", "capacity": 2}
        assert attr_assign("users/user/test/b", "p1", "name", "place 3") == None
        assert attr_assign("users/user/test/b", "p1", "capacity", 3) == None
        assert read_attrs("users/user/test/b", "p1") == {"name": "place 3", "capacity": 3}

        assert read_attrs("users/user/test/b", "p2t") == {"weight": 4}
        assert attr_assign("users/user/test/b", "p2t", "weight", 0) == None
        assert read_attrs("users/user/test/b", "p2t") == {"weight": 0}

        # Non-existing model
        try:
            attr_assign("users/afa", "p1", "name", "abc")
            self.fail()
        except UnknownModel:
            pass

        # Non-existing element
        try:
            attr_assign("users/user/test/a", "PPPPPPP", "name", "abc")
            self.fail()
        except UnknownElement:
            pass

        # No read permissions
        try:
            attr_assign("administration/core", "formalisms", "name", "abc")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permissions
        try:
            attr_assign("formalisms/SimpleClassDiagrams", "Class", "name", "abc")
            self.fail()
        except WritePermissionDenied:
            pass

        # No such attribute
        try:
            attr_assign("users/user/test/a", "Place", "ddd", 1)
            self.fail()
        except UnknownAttribute:
            pass

        # Assign None, equals to attr_delete
        assert read_attrs("users/user/test/a", "Place") == {"constraint": {"AL": ""}, "name": "NewPlace", "abstract": False, "lower_cardinality": 4, "upper_cardinality": None}
        attr_assign("users/user/test/a", "Place", "name", None)
        assert read_attrs("users/user/test/a", "Place") == {"constraint": {"AL": ""}, "name": None, "abstract": False, "lower_cardinality": 4, "upper_cardinality": None}

        # Check on folder "model"
        try:
            attr_assign("users", "a", "b", "c")
            self.fail()
        except NotAModel:
            pass

    def test_op_attr_delete(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute Natural {
                name = "natural"
            }
            SimpleAttribute String {
                name = "string"
            }

            Class Place {
                name = "Place"
                name : String
                capacity : Natural
            }
            Class Transition {
                name = "Transition"
                name : String
            }
            Association P2T (Place, Transition) {
                name = "P2T"
                weight : Natural
            }
            Association T2P (Transition, Place) {
                name = "T2P"
                weight : Natural
            }
            """)
        assert read_attrs("users/user/test/a", "Natural") == {"constraint": {"AL": ""}, "name": "natural"}
        assert attr_delete("users/user/test/a", "Natural", "name") == None
        assert read_attrs("users/user/test/a", "Natural") == {"constraint": {"AL": ""}, "name": None}

        assert read_attrs("users/user/test/a", "Place") == {"constraint": {"AL": ""}, "name": "Place", "abstract": None, "lower_cardinality": None, "upper_cardinality": None}
        assert attr_delete("users/user/test/a", "Place", "name") == None
        assert attr_delete("users/user/test/a", "Place", "abstract") == None
        assert attr_delete("users/user/test/a", "Place", "lower_cardinality") == None
        assert read_attrs("users/user/test/a", "Place") == {"constraint": {"AL": ""}, "name": None, "abstract": None, "lower_cardinality": None, "upper_cardinality": None}

        assert read_attrs("users/user/test/a", "P2T") == {"constraint": {"AL": ""}, "name": "P2T", "source_lower_cardinality": None, "source_upper_cardinality": None, "target_lower_cardinality": None, "target_upper_cardinality": None, "abstract": None, "lower_cardinality": None, "upper_cardinality": None}
        assert attr_delete("users/user/test/a", "P2T", "name") == None
        assert attr_delete("users/user/test/a", "P2T", "source_lower_cardinality") == None
        assert attr_delete("users/user/test/a", "P2T", "target_upper_cardinality") == None
        assert read_attrs("users/user/test/a", "P2T") == {"constraint": {"AL": ""}, "name": None, "source_lower_cardinality": None, "source_upper_cardinality": None, "target_lower_cardinality": None, "target_upper_cardinality": None, "abstract": None, "lower_cardinality": None, "upper_cardinality": None}

        model_add("users/user/test/b", "users/user/test/a", """
            Place p1 {
                name = "place 1"
                capacity = 2
            }
            Place p2 {
                name = "place 2"
                capacity = 1
            }
            Transition t1 {
                name = "transition"
            }
            
            P2T p2t (p1, t1) {
                weight = 4
            }
            T2P t2p (t1, p2) {
                weight = 10
            }
            """)
        assert read_attrs("users/user/test/b", "p1") == {"name": "place 1", "capacity": 2}
        assert attr_delete("users/user/test/b", "p1", "capacity") == None
        assert read_attrs("users/user/test/b", "p1") == {"name": "place 1", "capacity": None}

        assert read_attrs("users/user/test/b", "p2t") == {"weight": 4}
        assert attr_delete("users/user/test/b", "p2t", "weight") == None
        assert read_attrs("users/user/test/b", "p2t") == {"weight": None}

        # Non-existing model
        try:
            attr_delete("users/afa", "p1", "name")
            self.fail()
        except UnknownModel:
            pass

        # Non-existing element
        try:
            attr_delete("users/user/test/a", "PPPPPPP", "name")
            self.fail()
        except UnknownElement:
            pass

        # No read permissions
        try:
            attr_delete("administration/core", "formalisms", "name")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permissions
        try:
            attr_delete("formalisms/SimpleClassDiagrams", "Class", "name")
            self.fail()
        except WritePermissionDenied:
            pass

        # No such attribute
        try:
            attr_delete("users/user/test/a", "Place", "ddd")
            self.fail()
        except UnknownAttribute:
            pass

        # Check on folder "model"
        try:
            attr_delete("users", "a", "b")
            self.fail()
        except NotAModel:
            pass

    def test_op_attr_delete_code(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute Natural {
                name = "natural"
            }
            SimpleAttribute String {
                name = "string"
            }

            Class Place {
                name = "Place"
                name : String
                capacity : Natural
            }
            Class Transition {
                name = "Transition"
                name : String
            }
            Association P2T (Place, Transition) {
                name = "P2T"
                weight : Natural
            }
            Association T2P (Transition, Place) {
                name = "T2P"
                weight : Natural
            }
            """)

        # Test what happens if removing a non-existing AL attribute
        assert read_attrs("users/user/test/a", "Place") == {"constraint": {"AL": ""}, "name": "Place", "abstract": None, "lower_cardinality": None, "upper_cardinality": None}
        attr_delete("users/user/test/a", "Place", "constraint")
        assert read_attrs("users/user/test/a", "Place") == {"constraint": {"AL": ""}, "name": "Place", "abstract": None, "lower_cardinality": None, "upper_cardinality": None}

        # Test what happens if removing an existing AL attribute
        attr_assign_code("users/user/test/a", "Place", "constraint", """
            String function a(value : Element):
                return "OK"!
            """)
        attrs = read_attrs("users/user/test/a", "Place")
        assert "AL" in attrs["constraint"]
        assert attrs["constraint"]["AL"] != ""
        attr_delete("users/user/test/a", "Place", "constraint")
        assert read_attrs("users/user/test/a", "Place") == {"constraint": {"AL": ""}, "name": "Place", "abstract": None, "lower_cardinality": None, "upper_cardinality": None}

        # Check on folder "model"
        try:
            attr_delete("users", "a", "b")
            self.fail()
        except NotAModel:
            pass

    def test_op_attr_assign_code(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute Natural {
                name = "natural"
            }
            SimpleAttribute String {
                name = "string"
            }

            Class Place {
                name = "Place"
                name : String
                capacity : Natural
            }
            Class Transition {
                name = "Transition"
                name : String
            }
            Association P2T (Place, Transition) {
                name = "P2T"
                weight : Natural
            }
            Association T2P (Transition, Place) {
                name = "T2P"
                weight : Natural
            }
            """)

        # Test ordinary case
        assert read_attrs("users/user/test/a", "Place") == {"constraint": {"AL": ""}, "name": "Place", "abstract": None, "lower_cardinality": None, "upper_cardinality": None}
        assert attr_assign_code("users/user/test/a", "Place", "constraint", """
            String function a(value : Element):
                return "OK"!
            """) == None
        attrs = read_attrs("users/user/test/a", "Place")
        assert "AL" in attrs["constraint"]
        assert attrs["constraint"]["AL"] != ""

        assert AL_text(attrs["constraint"]["AL"]).strip() == """
            String function a(value : Element):
                return "OK"!
            """.strip()

        # Test assign when already assigned
        attrs = read_attrs("users/user/test/a", "Place")
        old_location = attrs["constraint"]["AL"]
        old_text = AL_text(attrs["constraint"]["AL"])
        assert attr_assign_code("users/user/test/a", "Place", "constraint", """
            String function a(value : Element):
                return "NOK"!
            """) == None
        attrs = read_attrs("users/user/test/a", "Place")
        assert "AL" in attrs["constraint"]
        assert attrs["constraint"]["AL"] != ""
        assert attrs["constraint"]["AL"] != old_location
        assert AL_text(attrs["constraint"]["AL"]).strip() == """
            String function a(value : Element):
                return "NOK"!
            """.strip()
        assert AL_text(attrs["constraint"]["AL"]).strip () != old_text.strip()

        simple_code = \
            """
                String function a(value : Element):
                    return "OK"!
            """

        # Non-existing model
        try:
            attr_assign_code("users/afa", "p1", "name", simple_code)
            self.fail()
        except UnknownModel:
            pass

        # Non-existing element
        try:
            attr_assign_code("users/user/test/a", "PPPPPPP", "name", simple_code)
            self.fail()
        except UnknownElement:
            pass

        # No read permissions
        try:
            attr_assign_code("administration/core", "formalisms", "name", simple_code)
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permissions
        try:
            attr_assign_code("formalisms/SimpleClassDiagrams", "Class", "name", simple_code)
            self.fail()
        except WritePermissionDenied:
            pass

        # No such attribute
        try:
            attr_assign_code("users/user/test/a", "Place", "ddd", simple_code)
            self.fail()
        except UnknownAttribute:
            pass

        # Compilation error
        try:
            attr_assign_code("users/user/test/a", "Place", "constraint", "abc")
            self.fail()
        except CompilationError:
            pass

        # Check on folder "model"
        try:
            attr_assign_code("users", "a", "b", "c")
            self.fail()
        except NotAModel:
            pass

    def test_op_instantiate(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams")
        assert element_list("users/user/test/a") == set([])

        # Instantiate node element
        ID = instantiate("users/user/test/a", "Class")
        assert element_list("users/user/test/a") == set([(ID, "Class")])
        assert read_info("users/user/test/a", ID) == ("Class", None)

        # Instantiate node element with ID
        ID2 = instantiate("users/user/test/a", "Class", ID="new_node")
        assert ID2 == "new_node"
        assert element_list("users/user/test/a") == set([(ID, "Class"), (ID2, "Class")])
        assert read_info("users/user/test/a", ID2) == ("Class", None)

        # Instantiate edge element
        ID3 = instantiate("users/user/test/a", "Association", edge=(ID, ID2))
        assert element_list("users/user/test/a") == set([(ID, "Class"), (ID2, "Class"), (ID3, "Association")])
        assert read_info("users/user/test/a", ID3) == ("Association", (ID, ID2))

        # Instantiate edge element with ID
        ID4 = instantiate("users/user/test/a", "Association", edge=(ID, ID2), ID="new_edge")
        assert ID4 == "new_edge"
        assert element_list("users/user/test/a") == set([(ID, "Class"), (ID2, "Class"), (ID3, "Association"), (ID4, "Association")])
        assert read_info("users/user/test/a", ID4) == ("Association", (ID, ID2))

        # Instantiate edge from edge (source)
        ID5 = instantiate("users/user/test/a", "Association", edge=(ID3, ID))
        assert element_list("users/user/test/a") == set([(ID, "Class"), (ID2, "Class"), (ID3, "Association"), (ID4, "Association"), (ID5, "Association")])
        assert read_info("users/user/test/a", ID5) == ("Association", (ID3, ID))

        # Instantiate edge from edge (target)
        ID6 = instantiate("users/user/test/a", "Association", edge=(ID, ID3))
        assert element_list("users/user/test/a") == set([(ID, "Class"), (ID2, "Class"), (ID3, "Association"), (ID4, "Association"), (ID5, "Association"), (ID6, "Association")])
        assert read_info("users/user/test/a", ID6) == ("Association", (ID, ID3))

        # Instantiate edge from edge (source and target)
        ID7 = instantiate("users/user/test/a", "Association", edge=(ID3, ID4))
        assert element_list("users/user/test/a") == set([(ID, "Class"), (ID2, "Class"), (ID3, "Association"), (ID4, "Association"), (ID5, "Association"), (ID6, "Association"), (ID7, "Association")])
        assert read_info("users/user/test/a", ID7) == ("Association", (ID3, ID4))

        # Non-existing model
        try:
            instantiate("users/afa", "Association")
            self.fail()
        except UnknownModel:
            pass

        # Non-existing element
        before = element_list("users/user/test/a")
        try:
            instantiate("users/user/test/a", "AAAAAAA")
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # No read permissions
        try:
            instantiate("administration/core", "formalisms")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permissions
        before = element_list("users/user/test/a")
        try:
            instantiate("formalisms/SimpleClassDiagrams", "Class")
            self.fail()
        except WritePermissionDenied:
            assert element_list("users/user/test/a") == before

        # Pre-existing ID requested (node)
        before = element_list("users/user/test/a")
        try:
            instantiate("users/user/test/a", "Class", ID=ID)
            self.fail()
        except ElementExists:
            assert element_list("users/user/test/a") == before

        # Pre-existing ID requested (edge)
        before = element_list("users/user/test/a")
        try:
            instantiate("users/user/test/a", "Association", ID=ID, edge=(ID2, ID3))
            self.fail()
        except ElementExists:
            assert element_list("users/user/test/a") == before

        # Edge from non-existing ID
        before = element_list("users/user/test/a")
        try:
            instantiate("users/user/test/a", "Association", ID=ID, edge=("non-existing", ID3))
            self.fail()
        except ElementExists:
            assert element_list("users/user/test/a") == before

        # Edge to non-existing ID
        before = element_list("users/user/test/a")
        try:
            instantiate("users/user/test/a", "Association", ID=ID, edge=("non-existing", ID3))
            self.fail()
        except ElementExists:
            assert element_list("users/user/test/a") == before

        # Edge both non-existing ID
        before = element_list("users/user/test/a")
        try:
            instantiate("users/user/test/a", "Association", ID=ID, edge=("non-existing", "non-existing2"))
            self.fail()
        except ElementExists:
            assert element_list("users/user/test/a") == before

        # Garbage type
        before = element_list("users/user/test/a")
        try:
            instantiate("users/user/test/a", ("a", "b"))
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # Garbage edge src
        before = element_list("users/user/test/a")
        try:
            instantiate("users/user/test/a", "Association", edge=(('a', 'b'), "non-existing2"))
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # Garbage edge dst
        before = element_list("users/user/test/a")
        try:
            instantiate("users/user/test/a", "Association", edge=("non-existing", ('a', 'b')))
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # Test instantiating node as edge
        before = element_list("users/user/test/a")
        try:
            instantiate("users/user/test/a", "Class", edge=("a", "b"))
            self.fail()
        except IncorrectFormat:
            assert element_list("users/user/test/a") == before

        # Test instantiating edge as node
        before = element_list("users/user/test/a")
        try:
            instantiate("users/user/test/a", "Association")
            self.fail()
        except IncorrectFormat:
            assert element_list("users/user/test/a") == before

        # Check on folder "model"
        try:
            instantiate("users", "a")
            self.fail()
        except NotAModel:
            pass

    def test_op_delete_element(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            Class A {}
            """)

        # Test simple delete
        assert len(element_list_nice("users/user/test/a")) == 1
        assert delete_element("users/user/test/a", "A") == None
        assert len(element_list_nice("users/user/test/a")) == 0

        model_add("users/user/test/b", "formalisms/SimpleClassDiagrams", """
            Class A {}
            Class B {}
            Association (A, B) {}
            """)

        # Test delete of element that causes other elements to be deleted as well
        assert len(element_list_nice("users/user/test/b")) == 3
        assert delete_element("users/user/test/b", "A") == None
        assert len(element_list_nice("users/user/test/b")) == 1

        model_add("users/user/test/c", "formalisms/SimpleClassDiagrams", """
            Class A {}
            Class B {}
            Association C (A, B) {}
            """)

        # Test delete of edge that causes other elements to be deleted as well
        assert len(element_list_nice("users/user/test/c")) == 3
        assert delete_element("users/user/test/c", "C") == None
        assert len(element_list_nice("users/user/test/c")) == 2

        model_add("users/user/test/d", "formalisms/SimpleClassDiagrams", """
            Class A {
                name = "A"
            }
            """)

        # Test delete of a "hidden" element
        assert element_list_nice("users/user/test/d") == [{"__type" : "Class", "__id": "A", "lower_cardinality": None, "upper_cardinality": None, "constraint": {"AL": ""}, "name": "A", "abstract": None}]
        ID = [i for i in element_list("users/user/test/d") if i[1] == "Class_name"][0][0]
        assert delete_element("users/user/test/d", ID) == None
        assert element_list_nice("users/user/test/d") == [{"__type" : "Class", "__id": "A", "lower_cardinality": None, "upper_cardinality": None, "constraint": {"AL": ""}, "name": None, "abstract": None}]

        # Non-existing model
        try:
            delete_element("users/afa", "Association")
            self.fail()
        except UnknownModel:
            pass

        # Non-existing element
        before = element_list("users/user/test/a")
        try:
            delete_element("users/user/test/a", "AAAAAAA")
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # No read permissions
        try:
            delete_element("administration/core", "formalisms")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permissions
        before = element_list("formalisms/SimpleClassDiagrams")
        try:
            delete_element("formalisms/SimpleClassDiagrams", "Class")
            self.fail()
        except WritePermissionDenied:
            assert element_list("formalisms/SimpleClassDiagrams") == before

        # Give garbage
        assert element_list_nice("users/user/test/d") == [{"__type" : "Class", "__id": "A", "lower_cardinality": None, "upper_cardinality": None, "constraint": {"AL": ""}, "name": None, "abstract": None}]
        try:
            delete_element("users/user/test/d", ('abc', 'def'))
        except UnknownElement:
            assert element_list_nice("users/user/test/d") == [{"__type" : "Class", "__id": "A", "lower_cardinality": None, "upper_cardinality": None, "constraint": {"AL": ""}, "name": None, "abstract": None}]

        # Check on folder "model"
        try:
            delete_element("users", "a")
            self.fail()
        except NotAModel:
            pass

    def test_op_AL_text(self):
        # Test normal situation with code provided by assignment
        model_add("users/user/test/b", "formalisms/SimpleClassDiagrams", """
            Class A {}
            """)
        attr_assign_code("users/user/test/b", "A", "constraint", """
            String function main(value : Element):
                return "OK"!
            """)
        assert AL_text(read_attrs("users/user/test/b", "A")["constraint"]["AL"]).strip() == """
            String function main(value : Element):
                return "OK"!
            """.strip()

        # Test normal situation with code provided by HUTN parser (model)
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            Class A {
                constraint = $
                        String function main(value : Element):
                            return "OK"!
                    $
            }
            """)
        assert AL_text(read_attrs("users/user/test/a", "A")["constraint"]["AL"]).strip() == 'String function main(value : Element):\n\treturn "OK"!'

        # Test AL_text on non-existing location
        try:
            AL_text("abc")
            self.fail()
        except UnknownLocation:
            pass

        # No permissions on locations directly, so there are no checks that have to be done

    def test_op_read_outgoing(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute String {}
            Class A {
                name : String
            }
            Class B {}
            Association C (A, B) {}
            Association D (C, B) {}
            """)

        # Test normal operation
        assert read_outgoing("users/user/test/a", "A", "Association") == set(["C"])
        assert read_outgoing("users/user/test/a", "A", "AttributeLink") == set(["A_name"])

        # Test wildcard
        assert read_outgoing("users/user/test/a", "A", "") == set(["A_name", "C"])

        # Test association on association
        assert read_outgoing("users/user/test/a", "C", "") == set(["D"])

        # Test empty result
        assert read_outgoing("users/user/test/a", "B", "") == set([])

        # Test simpleattribute
        assert read_outgoing("users/user/test/a", "String", "") == set([])

        # Non-existing model
        try:
            read_outgoing("users/afa", "Association", "")
            self.fail()
        except UnknownModel:
            pass

        # Non-existing element
        before = element_list("users/user/test/a")
        try:
            read_outgoing("users/user/test/a", "AAAAAAA", "")
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # Non-existing type
        before = element_list("users/user/test/a")
        try:
            read_outgoing("users/user/test/a", "A", "AAAAAAA")
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # No read permissions
        try:
            read_outgoing("administration/core", "formalisms", "")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permissions, but can query
        assert type(read_outgoing("formalisms/SimpleClassDiagrams", "Class", "")) == type(set())

        # Check on folder "model"
        try:
            read_outgoing("users", "a", "")
            self.fail()
        except NotAModel:
            pass

    def test_op_read_incoming(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute String {}
            Class A {
                name : String
            }
            Class B {}
            Association C (A, B) {}
            Association E (A, A) {}
            Association D (C, B) {}
            Association F (D, E) {}
            """)

        # Test normal operation
        assert read_incoming("users/user/test/a", "B", "Association") == set(["C", "D"])

        # Test wildcard
        assert read_incoming("users/user/test/a", "B", "") == set(["C", "D"])

        # Test association on association
        assert read_incoming("users/user/test/a", "E", "") == set(["F"])

        # Test double result
        assert read_incoming("users/user/test/a", "A", "") == set(["E"])

        # Test simpleattribute
        assert read_incoming("users/user/test/a", "String", "") == set(["A_name"])

        # Test empty result
        assert read_incoming("users/user/test/a", "F", "") == set([])

        # Non-existing model
        try:
            read_incoming("users/afa", "Association", "")
            self.fail()
        except UnknownModel:
            pass

        # Non-existing element
        before = element_list("users/user/test/a")
        try:
            read_incoming("users/user/test/a", "AAAAAAA", "")
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # Non-existing type
        before = element_list("users/user/test/a")
        try:
            read_incoming("users/user/test/a", "A", "AAAAAAA")
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # No read permissions
        try:
            read_incoming("administration/core", "formalisms", "")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permissions, but can query
        assert type(read_incoming("formalisms/SimpleClassDiagrams", "Class", "")) == type(set())

        # Check on folder "model"
        try:
            read_incoming("users", "a", "")
            self.fail()
        except NotAModel:
            pass

    def test_op_read_association_source(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute String {}
            Class A {
                name : String
            }
            Class B {}
            Association C (A, B) {}
            Association E (A, A) {}
            Association D (C, B) {}
            Association F (D, E) {}
            """)

        # Test normal operation
        assert read_association_source("users/user/test/a", "C") == "A"

        # Test association on association
        assert read_association_source("users/user/test/a", "D") == "C"

        # Test node
        try:
            assert read_association_source("users/user/test/a", "A")
            self.fail()
        except NotAnAssociation:
            pass

        # Non-existing model
        try:
            read_association_source("users/afa", "Association")
            self.fail()
        except UnknownModel:
            pass

        # Non-existing element
        before = element_list("users/user/test/a")
        try:
            read_association_source("users/user/test/a", "AAAAAAA")
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # No read permissions
        try:
            read_association_source("administration/core", "formalisms")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permissions, but can query
        assert read_association_source("formalisms/SimpleClassDiagrams", "Association") == "Class"

        # Not an association
        try:
            read_association_source("users/user/test/a", "A")
            self.fail()
        except NotAnAssociation:
            pass

        # Check on folder "model"
        try:
            read_association_source("users", "a")
            self.fail()
        except NotAModel:
            pass

    def test_op_read_association_destination(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute String {}
            Class A {
                name : String
            }
            Class B {}
            Association C (A, B) {}
            Association E (A, A) {}
            Association D (C, B) {}
            Association F (D, E) {}
            """)

        # Test normal operation
        assert read_association_destination("users/user/test/a", "C") == "B"

        # Test association on association
        assert read_association_destination("users/user/test/a", "F") == "E"

        # Test node
        try:
            assert read_association_destination("users/user/test/a", "A")
            self.fail()
        except NotAnAssociation:
            pass

        # Non-existing model
        try:
            read_association_destination("users/afa", "Association")
            self.fail()
        except UnknownModel:
            pass

        # Non-existing element
        before = element_list("users/user/test/a")
        try:
            read_association_destination("users/user/test/a", "AAAAAAA")
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # No read permissions
        try:
            read_association_destination("administration/core", "formalisms")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permissions, but can query
        assert read_association_destination("formalisms/SimpleClassDiagrams", "Association") == "Class"

        # Not an association
        try:
            read_association_destination("users/user/test/a", "A")
            self.fail()
        except NotAnAssociation:
            pass

        # Check on folder "model"
        try:
            read_association_destination("users", "a")
            self.fail()
        except NotAModel:
            pass

    def test_op_connections_between(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute String {}
            Class A {
                name1 : String
                name2 : String
            }
            Class B {}
            Association C (A, B) {}
            Association D (A, B) {}
            Association E (A, A) {}
            Association F (C, B) {}
            Association G (E, F) {}
            """)

        # Test normal operation
        assert connections_between("users/user/test/a", "A", "A") == set(["Association", "Inheritance"])
        assert connections_between("users/user/test/a", "A", "B") == set(["Association", "Inheritance"])
        assert connections_between("users/user/test/a", "A", "C") == set(["Association", "Inheritance"])

        # Test none allowed
        assert connections_between("users/user/test/a", "A_name1", "A_name2") == set([])

        # Non-existing model
        try:
            connections_between("users/afa", "A", "B")
            self.fail()
        except UnknownModel:
            pass

        # Non-existing element (1)
        before = element_list("users/user/test/a")
        try:
            connections_between("users/user/test/a", "AA", "B")
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # Non-existing element (2)
        before = element_list("users/user/test/a")
        try:
            connections_between("users/user/test/a", "A", "BB")
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # No read permissions
        try:
            connections_between("administration/core", "formalisms", "formalisms")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permissions, but can query
        assert connections_between("formalisms/SimpleClassDiagrams", "Association", "Association") == set(["Association", "Inheritance"])

        # Check on folder "model"
        try:
            connections_between("users", "a", "b")
            self.fail()
        except NotAModel:
            pass

    def test_op_define_attribute(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute String {}
            SimpleAttribute Natural {}
            Class A {}
            Class B {}
            Association C (A, B) {}
            Association D (C, A) {}
            """)
        assert read_defined_attrs("users/user/test/a", "A") == ({}, {})

        # Test normal operation
        define_attribute("users/user/test/a", "A", "my_attr", "String")
        assert read_defined_attrs("users/user/test/a", "A") == ({"my_attr": "String"}, {})

        # Test second attribute of same type
        define_attribute("users/user/test/a", "A", "my_second_attr", "String")
        assert read_defined_attrs("users/user/test/a", "A") == ({"my_attr": "String", "my_second_attr": "String"}, {})

        # Test third attribute with different type
        define_attribute("users/user/test/a", "A", "a_third_one", "Natural")
        assert read_defined_attrs("users/user/test/a", "A") == ({"my_attr": "String", "my_second_attr": "String", "a_third_one": "Natural"}, {})

        # Test same on associations
        define_attribute("users/user/test/a", "C", "my_attr", "String")
        assert read_defined_attrs("users/user/test/a", "C") == ({"my_attr": "String"}, {})

        # Test second attribute of same type
        define_attribute("users/user/test/a", "C", "my_second_attr", "String")
        assert read_defined_attrs("users/user/test/a", "C") == ({"my_attr": "String", "my_second_attr": "String"}, {})

        # Test third attribute with different type
        define_attribute("users/user/test/a", "C", "a_third_one", "Natural")
        assert read_defined_attrs("users/user/test/a", "C") == ({"my_attr": "String", "my_second_attr": "String", "a_third_one": "Natural"}, {})

        # Non-existing model
        try:
            define_attribute("users/afa", "A", "B", "C")
            self.fail()
        except UnknownModel:
            pass

        # Non-existing element (1)
        before = element_list("users/user/test/a")
        try:
            define_attribute("users/user/test/a", "AA", "B", "Natural")
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # Non-existing element (2)
        before = element_list("users/user/test/a")
        try:
            define_attribute("users/user/test/a", "A", "B", "Natural2")
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # Attribute exists
        before = element_list("users/user/test/a")
        try:
            define_attribute("users/user/test/a", "A", "my_attr", "Natural")
            self.fail()
        except AttributeExists:
            assert element_list("users/user/test/a") == before

        # No read permissions
        before = element_list("users/user/test/a")
        try:
            define_attribute("administration/core", "formalisms", "name", "A")
            self.fail()
        except ReadPermissionDenied:
            assert element_list("users/user/test/a") == before

        # No write permissions
        before = element_list("users/user/test/a")
        try:
            define_attribute("formalisms/SimpleClassDiagrams", "Association", "new_name", "String")
            self.fail()
        except WritePermissionDenied:
            assert element_list("users/user/test/a") == before

        # None defined in non-SCD model
        model_add("users/user/test/b", "users/user/test/a", "A a {}")
        try:
            define_attribute("users/user/test/b", "a", "abc", "a")
            self.fail()
        except UnknownM3:
            pass

        # Check on folder "model"
        try:
            define_attribute("users", "a", "b", "c")
            self.fail()
        except NotAModel:
            pass

    def test_op_read_defined_attrs(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute String {}
            SimpleAttribute Natural {}
            Class A {
                name = "ABC"
                value : Natural
                new_name ?: String
                name : String
            }
            Association B (A, A) {
                name = "DEF"
                edge_value : Natural
                name : String
                other_value ?: String
            }
            Class C {}
            Class D : A {
                additional_attr : Natural
            }
            Association E : B (D, A) {
                next_attr : String
            }
            """)

        # Normal operation
        assert read_defined_attrs("users/user/test/a", "A") == ({"value": "Natural", "name": "String"}, {"new_name": "String"})
        assert read_defined_attrs("users/user/test/a", "B") == ({"edge_value": "Natural", "name": "String"}, {"other_value": "String"})
        assert read_defined_attrs("users/user/test/a", "C") == ({}, {})

        # Works with inheritance
        assert read_defined_attrs("users/user/test/a", "D") == ({"value": "Natural", "name": "String", "additional_attr": "Natural"}, {"new_name": "String"})
        assert read_defined_attrs("users/user/test/a", "E") == ({"edge_value": "Natural", "name": "String", "next_attr": "String"}, {"other_value": "String"})

        # Non-existing model
        try:
            read_defined_attrs("users/afa", "A")
            self.fail()
        except UnknownModel:
            pass

        # Non-existing element
        before = element_list("users/user/test/a")
        try:
            read_defined_attrs("users/user/test/a", "AA")
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # No read permissions
        before = element_list("users/user/test/a")
        try:
            read_defined_attrs("administration/core", "formalisms")
            self.fail()
        except ReadPermissionDenied:
            assert element_list("users/user/test/a") == before

        # No write permissions, but can still query
        assert read_defined_attrs("formalisms/SimpleClassDiagrams", "Association") == ({"name": "String"}, {"abstract": "Boolean", "constraint": "ActionCode", "lower_cardinality": "Natural", "upper_cardinality": "Natural", "source_lower_cardinality": "Natural", "source_upper_cardinality": "Natural", "target_lower_cardinality": "Natural", "target_upper_cardinality": "Natural"})

        # None defined in non-SCD model
        model_add("users/user/test/b", "users/user/test/a", "A a {}")
        try:
            read_defined_attrs("users/user/test/b", "a")
            self.fail()
        except UnknownM3:
            pass

        # Check on folder "model"
        try:
            read_defined_attrs("users", "a")
            self.fail()
        except NotAModel:
            pass

    def test_op_undefine_attribute(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute String {}
            SimpleAttribute Natural {}
            Class A {
                name = "ABC"
                value : Natural
                new_name ?: String
                name : String
            }
            Association B (A, A) {
                name = "DEF"
                edge_value : Natural
                name : String
                other_value ?: String
            }
            Class C {}
            Class D : A {
                additional_attr : Natural
            }
            Association E : B (D, A) {
                next_attr : String
            }
            """)

        assert read_defined_attrs("users/user/test/a", "A") == ({"value": "Natural", "name": "String"}, {"new_name": "String"})
        assert read_defined_attrs("users/user/test/a", "D") == ({"value": "Natural", "name": "String", "additional_attr": "Natural"}, {"new_name": "String"})
        undefine_attribute("users/user/test/a", "A", "name")
        assert read_defined_attrs("users/user/test/a", "A") == ({"value": "Natural"}, {"new_name": "String"})
        assert read_defined_attrs("users/user/test/a", "D") == ({"value": "Natural", "additional_attr": "Natural"}, {"new_name": "String"})

        assert read_defined_attrs("users/user/test/a", "B") == ({"edge_value": "Natural", "name": "String"}, {"other_value": "String"})
        assert read_defined_attrs("users/user/test/a", "E") == ({"edge_value": "Natural", "name": "String", "next_attr": "String"}, {"other_value": "String"})
        undefine_attribute("users/user/test/a", "B", "other_value")
        assert read_defined_attrs("users/user/test/a", "B") == ({"edge_value": "Natural", "name": "String"}, {})
        assert read_defined_attrs("users/user/test/a", "E") == ({"edge_value": "Natural", "name": "String", "next_attr": "String"}, {})

        # Remove superclass definition
        assert read_defined_attrs("users/user/test/a", "D") == ({"value": "Natural", "additional_attr": "Natural"}, {"new_name": "String"})
        try:
            undefine_attribute("users/user/test/a", "D", "value")
            self.fail()
        except SuperclassAttribute:
            pass
        assert read_defined_attrs("users/user/test/a", "D") == ({"value": "Natural", "additional_attr": "Natural"}, {"new_name": "String"})

        # Non-existing model
        try:
            undefine_attribute("users/afa", "A", "B")
            self.fail()
        except UnknownModel:
            pass

        # Non-existing element
        before = read_defined_attrs("users/user/test/a", "A")
        try:
            undefine_attribute("users/user/test/a", "AA", "B")
            self.fail()
        except UnknownElement:
            assert read_defined_attrs("users/user/test/a", "A") == before

        # Non-existing attribute
        before = read_defined_attrs("users/user/test/a", "A")
        try:
            undefine_attribute("users/user/test/a", "A", "adfadf")
            self.fail()
        except UnknownAttribute:
            assert read_defined_attrs("users/user/test/a", "A") == before

        # No read permissions
        try:
            undefine_attribute("administration/core", "formalisms", "name")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permissions
        before = read_defined_attrs("formalisms/SimpleClassDiagrams", "Class")
        try:
            undefine_attribute("formalisms/SimpleClassDiagrams", "Class", "name")
            self.fail()
        except WritePermissionDenied:
            assert read_defined_attrs("formalisms/SimpleClassDiagrams", "Class") == before

        # Check on folder "model"
        try:
            undefine_attribute("users", "a", "b")
            self.fail()
        except NotAModel:
            pass

    def test_op_attribute_optional(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute String {}
            SimpleAttribute Natural {}
            Class A {
                name = "ABC"
                value : Natural
                new_name ?: String
                name : String
            }
            Association B (A, A) {
                name = "DEF"
                edge_value : Natural
                name : String
                other_value ?: String
            }
            Class C {}
            Class D : A {
                additional_attr : Natural
            }
            Association E : B (D, A) {
                next_attr : String
            }
            """)

        # Check initial situation
        assert read_defined_attrs("users/user/test/a", "A") == ({"value": "Natural", "name": "String"}, {"new_name": "String"})
        assert read_defined_attrs("users/user/test/a", "B") == ({"edge_value": "Natural", "name": "String"}, {"other_value": "String"})
        assert read_defined_attrs("users/user/test/a", "C") == ({}, {})
        assert read_defined_attrs("users/user/test/a", "D") == ({"value": "Natural", "name": "String", "additional_attr": "Natural"}, {"new_name": "String"})
        assert read_defined_attrs("users/user/test/a", "E") == ({"edge_value": "Natural", "name": "String", "next_attr": "String"}, {"other_value": "String"})

        # mandatory -> optional
        attribute_optional("users/user/test/a", "A", "value", True)
        assert read_defined_attrs("users/user/test/a", "A") == ({"name": "String"}, {"new_name": "String", "value": "Natural"})
        # Check inheritance as well
        assert read_defined_attrs("users/user/test/a", "D") == ({"name": "String", "additional_attr": "Natural"}, {"new_name": "String", "value": "Natural"})

        # optional -> mandatory
        attribute_optional("users/user/test/a", "A", "new_name", False)
        assert read_defined_attrs("users/user/test/a", "A") == ({"name": "String", "new_name": "String"}, {"value": "Natural"})
        # Check inheritance as well
        assert read_defined_attrs("users/user/test/a", "D") == ({"name": "String", "additional_attr": "Natural", "new_name": "String"}, {"value": "Natural"})

        # mandatory -> mandatory
        attribute_optional("users/user/test/a", "A", "new_name", False)
        assert read_defined_attrs("users/user/test/a", "A") == ({"name": "String", "new_name": "String"}, {"value": "Natural"})
        # Check inheritance as well
        assert read_defined_attrs("users/user/test/a", "D") == ({"name": "String", "additional_attr": "Natural", "new_name": "String"}, {"value": "Natural"})

        # optional -> optional
        attribute_optional("users/user/test/a", "A", "value", True) 
        assert read_defined_attrs("users/user/test/a", "A") == ({"name": "String", "new_name": "String"}, {"value": "Natural"})
        # Check inheritance as well
        assert read_defined_attrs("users/user/test/a", "D") == ({"name": "String", "additional_attr": "Natural", "new_name": "String"}, {"value": "Natural"})

        # Non-existing model
        try:
            attribute_optional("users/afa", "A", "value", False)
            self.fail()
        except UnknownModel:
            pass

        # Non-existing element
        before = element_list("users/user/test/a")
        try:
            attribute_optional("users/user/test/a", "AA", "value", False)
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # No read permissions
        try:
            attribute_optional("administration/core", "formalisms", "name", True)
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permissions, but can still query
        before = element_list("formalisms/SimpleClassDiagrams")
        try:
            attribute_optional("formalisms/SimpleClassDiagrams", "Class", "name", True)
            self.fail()
        except WritePermissionDenied:
            assert element_list("formalisms/SimpleClassDiagrams") == before

        # None defined in non-SCD model
        model_add("users/user/test/b", "users/user/test/a", "A a {}")
        try:
            attribute_optional("users/user/test/b", "a", "b", False)
            self.fail()
        except UnknownM3:
            pass
        
        # Attribute not defined here
        try:
            attribute_optional("users/user/test/a", "D", "value", False)
            self.fail()
        except SuperclassAttribute:
            assert read_defined_attrs("users/user/test/a", "D") == ({"name": "String", "additional_attr": "Natural", "new_name": "String"}, {"value": "Natural"})

        # Non-existing attribute
        try:
            attribute_optional("users/user/test/a", "D", "adf", True)
            self.fail()
        except UnknownAttribute:
            assert read_defined_attrs("users/user/test/a", "D") == ({"name": "String", "additional_attr": "Natural", "new_name": "String"}, {"value": "Natural"})

        # Check on folder "model"
        try:
            attribute_optional("users", "a", "b", True)
            self.fail()
        except NotAModel:
            pass

    def test_op_attribute_type(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute String {}
            SimpleAttribute Natural {}
            Class A {
                name = "ABC"
                value : Natural
                new_name ?: String
                name : String
            }
            Association B (A, A) {
                name = "DEF"
                edge_value : Natural
                name : String
                other_value ?: String
            }
            Class C {}
            Class D : A {
                additional_attr : Natural
            }
            Association E : B (D, A) {
                next_attr : String
            }
            """)

        # Check initial situation
        assert read_defined_attrs("users/user/test/a", "A") == ({"value": "Natural", "name": "String"}, {"new_name": "String"})
        assert read_defined_attrs("users/user/test/a", "B") == ({"edge_value": "Natural", "name": "String"}, {"other_value": "String"})
        assert read_defined_attrs("users/user/test/a", "C") == ({}, {})
        assert read_defined_attrs("users/user/test/a", "D") == ({"value": "Natural", "name": "String", "additional_attr": "Natural"}, {"new_name": "String"})
        assert read_defined_attrs("users/user/test/a", "E") == ({"edge_value": "Natural", "name": "String", "next_attr": "String"}, {"other_value": "String"})

        # Change type of node
        attribute_type("users/user/test/a", "A", "value", "String")
        assert read_defined_attrs("users/user/test/a", "A") == ({"value": "String", "name": "String"}, {"new_name": "String"})
        # Check inheritance as well
        assert read_defined_attrs("users/user/test/a", "D") == ({"value": "String", "name": "String", "additional_attr": "Natural"}, {"new_name": "String"})

        # Change type of edge
        attribute_type("users/user/test/a", "B", "name", "Natural") 
        assert read_defined_attrs("users/user/test/a", "B") == ({"edge_value": "Natural", "name": "Natural"}, {"other_value": "String"})
        # Check inheritance as well
        assert read_defined_attrs("users/user/test/a", "E") == ({"edge_value": "Natural", "name": "Natural", "next_attr": "String"}, {"other_value": "String"})

        # Non-existing model
        try:
            attribute_type("users/afa", "A", "value", "String")
            self.fail()
        except UnknownModel:
            pass

        # Non-existing element
        before = element_list("users/user/test/a")
        try:
            attribute_type("users/user/test/a", "AA", "value", "String")
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # Non-existing type
        before = element_list("users/user/test/a")
        try:
            attribute_type("users/user/test/a", "A", "value", "SSS")
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # No read permissions
        try:
            attribute_type("administration/core", "formalisms", "name", "Natural")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permissions
        before = element_list("formalisms/SimpleClassDiagrams")
        try:
            attribute_type("formalisms/SimpleClassDiagrams", "Class", "name", "Natural")
            self.fail()
        except WritePermissionDenied:
            assert element_list("formalisms/SimpleClassDiagrams") == before

        # None defined in non-SCD model
        model_add("users/user/test/b", "users/user/test/a", "A a {}")
        try:
            attribute_type("users/user/test/b", "a", "b", "a")
            self.fail()
        except UnknownM3:
            pass

        # Attribute not defined here
        try:
            attribute_type("users/user/test/a", "D", "value", "Natural")
            self.fail()
        except SuperclassAttribute:
            assert read_defined_attrs("users/user/test/a", "D") == ({"value": "String", "name": "String", "additional_attr": "Natural"}, {"new_name": "String"})

        # Non-existing attribute
        try:
            attribute_type("users/user/test/a", "D", "adf", "Natural")
            self.fail()
        except UnknownAttribute:
            assert read_defined_attrs("users/user/test/a", "D") == ({"value": "String", "name": "String", "additional_attr": "Natural"}, {"new_name": "String"})

        # Check on folder "model"
        try:
            attribute_type("users", "a", "b", "c")
            self.fail()
        except NotAModel:
            pass

    def test_op_attribute_name(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute String {}
            SimpleAttribute Natural {}
            Class A {
                name = "ABC"
                value : Natural
                new_name ?: String
                name : String
            }
            Association B (A, A) {
                name = "DEF"
                edge_value : Natural
                name : String
                other_value ?: String
            }
            Class C {}
            Class D : A {
                additional_attr : Natural
            }
            Association E : B (D, A) {
                next_attr : String
            }
            """)

        # Check initial situation
        assert read_defined_attrs("users/user/test/a", "A") == ({"value": "Natural", "name": "String"}, {"new_name": "String"})
        assert read_defined_attrs("users/user/test/a", "B") == ({"edge_value": "Natural", "name": "String"}, {"other_value": "String"})
        assert read_defined_attrs("users/user/test/a", "C") == ({}, {})
        assert read_defined_attrs("users/user/test/a", "D") == ({"value": "Natural", "name": "String", "additional_attr": "Natural"}, {"new_name": "String"})
        assert read_defined_attrs("users/user/test/a", "E") == ({"edge_value": "Natural", "name": "String", "next_attr": "String"}, {"other_value": "String"})

        # Change name of node
        attribute_name("users/user/test/a", "A", "value", "new_value")
        assert read_defined_attrs("users/user/test/a", "A") == ({"new_value": "Natural", "name": "String"}, {"new_name": "String"})
        # Check inheritance as well
        assert read_defined_attrs("users/user/test/a", "D") == ({"new_value": "Natural", "name": "String", "additional_attr": "Natural"}, {"new_name": "String"})

        # Change name of edge
        attribute_name("users/user/test/a", "B", "name", "other_name") 
        assert read_defined_attrs("users/user/test/a", "B") == ({"edge_value": "Natural", "other_name": "String"}, {"other_value": "String"})
        # Check inheritance as well
        assert read_defined_attrs("users/user/test/a", "E") == ({"edge_value": "Natural", "other_name": "String", "next_attr": "String"}, {"other_value": "String"})

        # Attribute not defined here
        try:
            attribute_name("users/user/test/a", "D", "new_value", "newer_value")
            self.fail()
        except SuperclassAttribute:
            pass

        # Non-existing model
        try:
            attribute_name("users/afa", "A", "value", "str")
            self.fail()
        except UnknownModel:
            pass

        # Non-existing element
        before = element_list("users/user/test/a")
        try:
            attribute_name("users/user/test/a", "AA", "value", "val2")
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # Pre-existing name
        before = element_list("users/user/test/a")
        try:
            attribute_name("users/user/test/a", "A", "new_value", "name")
            self.fail()
        except AttributeExists:
            assert element_list("users/user/test/a") == before

        # No read permissions
        try:
            attribute_name("administration/core", "formalisms", "name", "natural")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permissions, but can still query
        before = element_list("formalisms/SimpleClassDiagrams")
        try:
            attribute_name("formalisms/SimpleClassDiagrams", "Class", "name", "value")
            self.fail()
        except WritePermissionDenied:
            assert element_list("formalisms/SimpleClassDiagrams") == before

        # None defined in non-SCD model
        model_add("users/user/test/b", "users/user/test/a", "A a {}")
        try:
            attribute_name("users/user/test/b", "a", "b", "c")
            self.fail()
        except UnknownM3:
            pass

        # Check on folder "model"
        try:
            attribute_name("users", "a", "b", "c")
            self.fail()
        except NotAModel:
            pass

    def test_op_group_create(self):
        # Test normal operation
        group_name_1 = "new_group"
        group_name_2 = "group2"
        group_name_3 = "other_group"
        group_name_4 = "second_group"

        assert group_list() == set([])
        group_create(group_name_1)
        assert group_list() == set([(group_name_1, True)])

        # Test second group
        assert group_list() == set([(group_name_1, True)])
        group_create(group_name_2)
        assert group_list() == set([(group_name_1, True), (group_name_2, True)])

        # Test other groups
        self.do_as_user("user2", group_create, [group_name_3])
        self.do_as_user("user2", group_create, [group_name_4])
        assert group_list() == set([(group_name_1, True), (group_name_2, True)])

        # Test non-admin groups
        self.do_as_user("user2", group_join, [group_name_3, "user"])
        assert group_list() == set([(group_name_1, True), (group_name_2, True), (group_name_3, False)])

        # Test create of existing group (member)
        before = group_list()
        try:
            group_create(group_name_1)
            self.fail()
        except GroupExists:
            assert group_list() == before

        # Test create of existing group (non-member)
        before = group_list()
        try:
            group_create(group_name_4)
            self.fail()
        except GroupExists:
            assert group_list() == before

        # Delete all groups again
        self.do_as_user("admin", group_delete, [group_name_1])
        self.do_as_user("admin", group_delete, [group_name_2])
        self.do_as_user("admin", group_delete, [group_name_3])
        self.do_as_user("admin", group_delete, [group_name_4])

    def test_op_group_delete(self):
        group_name_1 = "new_group"      # Group with admin permissions
        group_name_2 = "group2"         # Group without permissions
        group_name_3 = "other_group"    # No member
        group_name_4 = "second_group"   # Non-existing group

        # Ensure user2 exists
        self.do_as_user("user2", lambda : 1, [])

        # Create some groups first
        group_create(group_name_1)
        self.do_as_user("admin", group_create, [group_name_2])
        self.do_as_user("admin", group_join, [group_name_2, "user"])
        self.do_as_user("admin", group_create, [group_name_3])

        # Try to delete all 
        assert group_list() == set([(group_name_1, True), (group_name_2, False)])
        group_delete(group_name_1)
        assert group_list() == set([(group_name_2, False)])

        # Remove group that we don't own
        try:
            group_delete(group_name_2)
            self.fail()
        except GroupPermissionDenied:
            assert group_list() == set([(group_name_2, False)])

        # Remove group that we are not even member of
        try:
            group_delete(group_name_3)
            self.fail()
        except GroupPermissionDenied:
            assert group_list() == set([(group_name_2, False)])

        # Remove non-existing group
        try:
            group_delete(group_name_4)
        except UnknownGroup:
            assert group_list() == set([(group_name_2, False)])

        # Test if other users can no longer see removed groups
        group_create(group_name_1)
        group_join(group_name_1, "user2")
        assert self.do_as_user("user2", group_list, []) == set([(group_name_1, False)])

        group_delete(group_name_1)
        assert self.do_as_user("user2", group_list, []) == set([])

        # Delete all groups again
        self.do_as_user("admin", group_delete, [group_name_2])
        self.do_as_user("admin", group_delete, [group_name_3])

    def test_op_group_list(self):
        group_name_1 = "new_group"      # Group with admin permissions
        group_name_2 = "group2"         # Group without permissions
        group_name_3 = "other_group"    # No member
        group_name_4 = "second_group"   # Non-existing group

        # Ensure user2 exists
        self.do_as_user("user2", lambda : 1, [])

        # Create some groups first
        group_create(group_name_1)
        self.do_as_user("admin", group_create, [group_name_2])
        self.do_as_user("admin", group_join, [group_name_2, "user"])
        self.do_as_user("admin", group_create, [group_name_3])

        # Assure that groups of all users are correct
        assert group_list() == set([(group_name_1, True), (group_name_2, False)])

        assert self.do_as_user("user2", group_list, []) == set([])
        assert self.do_as_user("admin", group_list, []) == set([("admin", False), (group_name_2, True), (group_name_3, True)])

        # Delete a group to check that everything is correctly listed afterwards
        self.do_as_user("admin", group_delete, [group_name_2])
        assert group_list() == set([(group_name_1, True)])
        assert self.do_as_user("user2", group_list, []) == set([])
        assert self.do_as_user("admin", group_list, []) == set([("admin", False), (group_name_3, True)])

        # Delete all groups again
        self.do_as_user("admin", group_delete, [group_name_1])
        self.do_as_user("admin", group_delete, [group_name_3])

    def do_as_user(self, username, operation, args):
        user_logout()
        login(username, username)
        try:
            result = operation(*args)
        except:
            user_logout()
            login("user", "user")
            raise
        else:
            user_logout()
            login("user", "user")
            return result

    def test_op_group_join(self):
        group_name_1 = "new_group"      # Group with admin permissions
        group_name_2 = "group2"         # Group without permissions
        group_name_3 = "other_group"    # No member
        group_name_4 = "second_group"   # Non-existing group

        # Ensure user2 exists
        self.do_as_user("user2", lambda : 1, [])

        # Create some groups first
        group_create(group_name_1)
        self.do_as_user("admin", group_create, [group_name_2])
        self.do_as_user("admin", group_join, [group_name_2, "user"])
        self.do_as_user("admin", group_create, [group_name_3])

        # Test for group join basic use, of group where we are admin
        assert group_join(group_name_1, "user2") == None
        assert self.do_as_user("user2", group_list, []) == set([(group_name_1, False)])

        # Test for group join of group where we are no admin
        try:
            group_join(group_name_2, "user2") == None
            self.fail()
        except GroupPermissionDenied:
            assert self.do_as_user("user2", group_list, []) == set([(group_name_1, False)])

        # Test for group join of group where we are not even a member
        try:
            group_join(group_name_3, "user2") == None
            self.fail()
        except GroupPermissionDenied:
            assert self.do_as_user("user2", group_list, []) == set([(group_name_1, False)])

        # Test for group join of group that already has user as member
        assert self.do_as_user("user2", group_list, []) == set([(group_name_1, False)])
        try:
            group_join(group_name_1, "user2") == None
            self.fail()
        except UserExists:
            assert self.do_as_user("user2", group_list, []) == set([(group_name_1, False)])

        # Test for group join of non-existing user
        try:
            group_join(group_name_1, "user123") == None
            self.fail()
        except UnknownUser:
            pass

        # Test for group join of non-existing group
        try:
            group_join(group_name_4, "user2") == None
            self.fail()
        except UnknownGroup:
            pass

        # Delete all groups again
        self.do_as_user("admin", group_delete, [group_name_1])
        self.do_as_user("admin", group_delete, [group_name_2])
        self.do_as_user("admin", group_delete, [group_name_3])

    def test_op_group_kick(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams")

        group_name_1 = "new_group"      # Group with admin permissions
        group_name_2 = "group2"         # Group without permissions
        group_name_3 = "other_group"    # No member
        group_name_4 = "second_group"   # Non-existing group

        # Ensure user2 exists
        self.do_as_user("user2", lambda : 1, [])

        # Create some groups first
        group_create(group_name_1)
        self.do_as_user("admin", group_create, [group_name_2])
        self.do_as_user("admin", group_join, [group_name_2, "user"])

        # Kick user from group
        assert group_list() == set([(group_name_1, True), (group_name_2, False)])
        self.do_as_user("admin", group_kick, [group_name_2, "user"])
        assert group_list() == set([(group_name_1, True)])

        # Kick self (owner) from group
        assert group_list() == set([(group_name_1, True)])
        group_create(group_name_3)
        assert group_list() == set([(group_name_1, True), (group_name_3, True)])
        assert group_kick(group_name_3, "user") == None
        assert group_list() == set([(group_name_1, True)])

        # Empty groups are not deleted
        self.do_as_user("admin", group_join, [group_name_3, "user"])

        # Kick user not in group
        try:
            self.do_as_user("admin", group_kick, [group_name_2, "user2"])
            self.fail()
        except UserNotInGroup:
            pass

        # Non-existing user
        try:
            group_kick(group_name_1, "adfafd")
            self.fail()
        except UnknownUser:
            pass

        # Non-existing group
        try:
            group_kick("adfadf", "user")
            self.fail()
        except UnknownGroup:
            pass

        # Not admin of group
        self.do_as_user("admin", group_join, [group_name_2, "user"])
        self.do_as_user("admin", group_join, [group_name_2, "user2"])
        assert group_list() == set([(group_name_1, True), (group_name_2, False), (group_name_3, False)])
        assert self.do_as_user("user2", group_list, []) == set([(group_name_2, False)])
        try:
            group_kick(group_name_2, "user2")
            self.fail()
        except GroupPermissionDenied:
            assert self.do_as_user("user2", group_list, []) == set([(group_name_2, False)])

        self.do_as_user("admin", group_delete, [group_name_1])
        self.do_as_user("admin", group_delete, [group_name_2])
        self.do_as_user("admin", group_delete, [group_name_3])

    def do_create_user(self, name):
        self.do_as_user(name, lambda : 1, [])

    def do_test_permission(self, model, expected_permissions):
        for user, permission in expected_permissions.items():
            assert self.do_as_user(user, read_permissions, [model]) == permission

    def test_op_permission_owner_model(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams")

        group_create("users")
        permission_group("users/user/test/a", "users")
        permission_modify("users/user/test/a", "210")

        self.do_create_user("group_user")
        self.do_create_user("other_user")
        group_join("users", "group_user")

        # Test initial situation, where we have owners user:users with permission 210
        self.do_test_permission("users/user/test/a", {"admin": "RW", "user": "RW", "group_user": "R", "other_user": ""})

        # Change owner, but permissions stay the same
        permission_owner("users/user/test/a", "group_user")
        self.do_test_permission("users/user/test/a", {"admin": "RW", "user": "R", "group_user": "RW", "other_user": ""})

        # Can no longer modify permissions: only owner can do that
        try:
            permission_owner("users/user/test/a", "admin")
            self.fail()
        except UserPermissionDenied:
            self.do_test_permission("users/user/test/a", {"admin": "RW", "user": "R", "group_user": "RW", "other_user": ""})

        # Change owner to admin, who always has RW permissions
        self.do_as_user("group_user", permission_owner, ["users/user/test/a", "admin"])
        self.do_test_permission("users/user/test/a", {"admin": "RW", "user": "R", "group_user": "R", "other_user": ""})

        # Give permissions back to someone else, and test that admin can always do operations, even if not owner
        self.do_as_user("admin", permission_owner, ["users/user/test/a", "group_user"])
        self.do_test_permission("users/user/test/a", {"admin": "RW", "user": "R", "group_user": "RW", "other_user": ""})
        self.do_as_user("admin", permission_owner, ["users/user/test/a", "user"])
        self.do_test_permission("users/user/test/a", {"admin": "RW", "user": "RW", "group_user": "R", "other_user": ""})

        # Test non-existing model
        try:
            permission_owner("adfadf", "admin")
            self.fail()
        except UnknownModel:
            pass

        # Test non-existing user
        try:
            self.do_as_user("admin", permission_owner, ["users/user/test/a", "dfadf"])
            self.fail()
        except UnknownUser:
            pass

        # Test assign to user who is already owner
        self.do_test_permission("users/user/test/a", {"admin": "RW", "user": "RW", "group_user": "R", "other_user": ""})
        self.do_as_user("admin", permission_owner, ["users/user/test/a", "user"])
        self.do_test_permission("users/user/test/a", {"admin": "RW", "user": "RW", "group_user": "R", "other_user": ""})

        group_delete("users")

    def test_op_permission_owner_folder(self):
        folder_create("users/user/test/a/b/c")

        group_create("users")
        self.do_create_user("group_user")
        group_join("users", "group_user")

        permission_group("users/user/test/a/b", "users")
        permission_modify("users/user/test/a/b", "210")

        # Test permissions
        self.do_test_permission("users/user/test/", {"admin": "RW", "user": "RW", "group_user": "", "other_user": ""})
        self.do_test_permission("users/user/test/a", {"admin": "RW", "user": "RW", "group_user": "", "other_user": ""})
        self.do_test_permission("users/user/test/a/b", {"admin": "RW", "user": "RW", "group_user": "R", "other_user": ""})
        self.do_test_permission("users/user/test/a/b/c", {"admin": "RW", "user": "RW", "group_user": "", "other_user": ""})

        # But test if they are really applicable
        try:
            self.do_as_user("group_user", model_list, ["users/user/test"])
            self.fail()
        except ReadPermissionDenied:
            pass

        try:
            self.do_as_user("group_user", model_list, ["users/user/test/a"])
            self.fail()
        except ReadPermissionDenied:
            pass

        self.do_as_user("group_user", model_list, ["users/user/test/a/b"])

        try:
            self.do_as_user("group_user", model_list, ["users/user/test/a/b/c"])
            self.fail()
        except ReadPermissionDenied:
            pass

        group_delete("users")

    def test_op_read_permissions_model(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams")

        group_create("users")
        self.do_create_user("group_user")
        self.do_create_user("other_user")
        group_join("users", "group_user")

        permission_group("users/user/test/a", "users")
        permission_modify("users/user/test/a", "210")

        # Test all four cases
        assert self.do_as_user("user", read_permissions, ["users/user/test/a"]) == "RW"
        assert self.do_as_user("group_user", read_permissions, ["users/user/test/a"]) == "R"
        assert self.do_as_user("other_user", read_permissions, ["users/user/test/a"]) == ""
        assert self.do_as_user("admin", read_permissions, ["users/user/test/a"]) == "RW"

        # Test same for root location (empty string)
        assert self.do_as_user("user", read_permissions, [""]) == "R"
        assert self.do_as_user("group_user", read_permissions, [""]) == "R"
        assert self.do_as_user("other_user", read_permissions, [""]) == "R"
        assert self.do_as_user("admin", read_permissions, [""]) == "RW"

        # Test for non-existing location
        try:
            read_permissions("adfadsf")
            self.fail()
        except UnknownLocation:
            pass

        group_delete("users")

    def test_op_read_permissions_folder(self):
        folder_create("users/user/test/a")

        group_create("users")
        self.do_create_user("group_user")
        self.do_create_user("other_user")
        group_join("users", "group_user")

        permission_group("users/user/test/a", "users")
        permission_modify("users/user/test/a", "210")

        # Test all four cases
        assert self.do_as_user("user", read_permissions, ["users/user/test/a"]) == "RW"
        assert self.do_as_user("group_user", read_permissions, ["users/user/test/a"]) == "R"
        assert self.do_as_user("other_user", read_permissions, ["users/user/test/a"]) == ""
        assert self.do_as_user("admin", read_permissions, ["users/user/test/a"]) == "RW"

        # Test for non-existing location
        try:
            read_permissions("adfadsf")
            self.fail()
        except UnknownLocation:
            pass

        group_delete("users")

    def test_op_permission_group_model(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams")
        group_create("users")
        group_create("other_group")
        permission_modify("users/user/test/a", "210")

        self.do_create_user("group_user")
        self.do_create_user("other_user")
        group_join("users", "group_user")

        # Test initial situation, where we have owners user:users with permission 210
        self.do_test_permission("users/user/test/a", {"admin": "RW", "user": "RW", "group_user": "", "other_user": ""})

        # Change owner, but permissions stay the same
        permission_group("users/user/test/a", "users")
        self.do_test_permission("users/user/test/a", {"admin": "RW", "user": "RW", "group_user": "R", "other_user": ""})

        # Only owner can modify group permissions
        try:
            self.do_as_user("group_user", permission_group, ["users/user/test/a", "other_group"])
            self.fail()
        except UserPermissionDenied:
            self.do_test_permission("users/user/test/a", {"admin": "RW", "user": "RW", "group_user": "R", "other_user": ""})

        # Test that admin can always do operations, even if not owner
        # And test that user permissions still override group permissions
        self.do_as_user("admin", permission_group, ["users/user/test/a", "admin"])
        self.do_test_permission("users/user/test/a", {"admin": "RW", "user": "RW", "group_user": "", "other_user": ""})
        self.do_as_user("admin", permission_group, ["users/user/test/a", "other_group"])
        self.do_test_permission("users/user/test/a", {"admin": "RW", "user": "RW", "group_user": "", "other_user": ""})
        self.do_as_user("admin", permission_group, ["users/user/test/a", "users"])
        self.do_test_permission("users/user/test/a", {"admin": "RW", "user": "RW", "group_user": "R", "other_user": ""})

        # Test non-existing model
        try:
            permission_group("adfadf", "admin")
            self.fail()
        except UnknownModel:
            pass

        # Test non-existing group
        try:
            self.do_as_user("admin", permission_group, ["users/user/test/a", "dfadf"])
            self.fail()
        except UnknownGroup:
            pass

        # Test assign to group which is already owning group, resulting in no change
        self.do_test_permission("users/user/test/a", {"admin": "RW", "user": "RW", "group_user": "R", "other_user": ""})
        self.do_as_user("admin", permission_group, ["users/user/test/a", "users"])
        self.do_test_permission("users/user/test/a", {"admin": "RW", "user": "RW", "group_user": "R", "other_user": ""})

        self.do_as_user("admin", group_delete, ["other_group"])
        self.do_as_user("admin", group_delete, ["users"])

    def test_op_permission_group_folder(self):
        folder_create("users/user/test/a/b/c")

        group_create("users")
        self.do_create_user("group_user")
        group_join("users", "group_user")

        permission_group("users/user/test/a/b", "users")
        permission_modify("users/user/test/a/b", "210")

        # Test permissions
        self.do_test_permission("users/user/test/", {"admin": "RW", "user": "RW", "group_user": "", "other_user": ""})
        self.do_test_permission("users/user/test/a", {"admin": "RW", "user": "RW", "group_user": "", "other_user": ""})
        self.do_test_permission("users/user/test/a/b", {"admin": "RW", "user": "RW", "group_user": "R", "other_user": ""})
        self.do_test_permission("users/user/test/a/b/c", {"admin": "RW", "user": "RW", "group_user": "", "other_user": ""})

        # But test if they are really applicable
        try:
            self.do_as_user("group_user", model_list, ["users/user/test"])
            self.fail()
        except ReadPermissionDenied:
            pass

        try:
            self.do_as_user("group_user", model_list, ["users/user/test/a"])
            self.fail()
        except ReadPermissionDenied:
            pass

        self.do_as_user("group_user", model_list, ["users/user/test/a/b"])

        try:
            self.do_as_user("group_user", model_list, ["users/user/test/a/b/c"])
            self.fail()
        except ReadPermissionDenied:
            pass

        group_delete("users")

    def test_op_group_owner_add(self):
        group_create("group1")
        group_create("group2")
        group_create("group3")
        self.do_create_user("new_user")

        group_join("group1", "new_user")

        # Basic use of group owner: promote a user
        assert self.do_as_user("new_user", group_list, []) == set([("group1", False)])
        assert group_owner_add("group1", "new_user") == None
        assert self.do_as_user("new_user", group_list, []) == set([("group1", True)])

        # Or instantaneously make owner, thereby also making a member
        assert self.do_as_user("new_user", group_list, []) == set([("group1", True)])
        assert group_owner_add("group2", "new_user") == None
        assert self.do_as_user("new_user", group_list, []) == set([("group1", True), ("group2", True)])

        # Test for user that is already an owner
        assert group_owner_add("group1", "new_user") == None
        assert self.do_as_user("new_user", group_list, []) == set([("group1", True), ("group2", True)])

        # Test non-existing user
        try:
            group_owner_add("group1", "abc")
            self.fail()
        except UnknownUser:
            pass

        # Test non-existing group
        try:
            group_owner_add("group4", "new_user")
            self.fail()
        except UnknownGroup:
            assert self.do_as_user("new_user", group_list, []) == set([("group1", True), ("group2", True)])

        # Only an owner of the group can do this
        group_owner_delete("group2", "new_user")
        assert self.do_as_user("new_user", group_list, []) == set([("group1", True), ("group2", False)])
        try:
            self.do_as_user("new_user", group_owner_add, ["group2", "user"])
            self.fail()
        except GroupPermissionDenied:
            pass

        # Also non-members can't modify
        assert self.do_as_user("new_user", group_list, []) == set([("group1", True), ("group2", False)])
        try:
            self.do_as_user("new_user", group_owner_add, ["group3", "user"])
            self.fail()
        except GroupPermissionDenied:
            pass

        # Delete all groups again
        group_delete("group1")
        group_delete("group2")
        group_delete("group3")

    def test_op_group_owner_delete(self):
        group_create("group1")
        group_create("group2")
        group_create("group3")
        self.do_create_user("new_user")

        group_owner_add("group1", "new_user")
        group_owner_add("group2", "new_user")

        # Basic use: remove owner, but stay a member
        assert self.do_as_user("new_user", group_list, []) == set([("group1", True), ("group2", True)])
        assert group_owner_delete("group2", "new_user") == None
        assert self.do_as_user("new_user", group_list, []) == set([("group1", True), ("group2", False)])

        # Test for user that is already not an owner
        assert self.do_as_user("new_user", group_list, []) == set([("group1", True), ("group2", False)])
        assert group_owner_delete("group2", "new_user") == None
        assert self.do_as_user("new_user", group_list, []) == set([("group1", True), ("group2", False)])

        # Test for user that is not even a member
        assert self.do_as_user("new_user", group_list, []) == set([("group1", True), ("group2", False)])
        try:
            group_owner_delete("group3", "new_user")
            self.fail()
        except UserNotInGroup:
            assert self.do_as_user("new_user", group_list, []) == set([("group1", True), ("group2", False)])

        # Test non-existing user
        try:
            group_owner_delete("group1", "abc")
            self.fail()
        except UnknownUser:
            pass

        # Test non-existing group
        try:
            group_owner_delete("group4", "new_user")
            self.fail()
        except UnknownGroup:
            assert self.do_as_user("new_user", group_list, []) == set([("group1", True), ("group2", False)])

        # Only an owner of the group can do this
        group_owner_delete("group2", "new_user")
        assert group_list() == set([("group1", True), ("group2", True), ("group3", True)])
        try:
            self.do_as_user("new_user", group_owner_delete, ["group2", "user"])
            self.fail()
        except GroupPermissionDenied:
            assert group_list() == set([("group1", True), ("group2", True), ("group3", True)])

        # Also non-members can't modify
        assert self.do_as_user("new_user", group_list, []) == set([("group1", True), ("group2", False)])
        try:
            self.do_as_user("new_user", group_owner_delete, ["group3", "user"])
            self.fail()
        except GroupPermissionDenied:
            pass

        # Delete all groups again
        group_delete("group1")
        group_delete("group2")
        group_delete("group3")

    def test_op_conformance_add(self):
        # Add some conformance relations
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            Class A {}
            Class B {}
            Association C (A, B) {}
            """)

        # Add conformance bottom relation
        t = model_types("users/user/test/a")
        assert len(t) == 1
        t = t.pop()
        print(t)
        assert t[0] == "formalisms/SimpleClassDiagrams"
        assert t[1] != None
        assert t[2] == None

        # Check if the type is added, but without any type mapping associated to it
        conformance_add("users/user/test/a", "formalisms/Bottom")
        ts = model_types("users/user/test/a")
        assert len(ts) == 2
        while ts:
            t = ts.pop()
            assert t[0] in ["formalisms/SimpleClassDiagrams", "formalisms/Bottom"]
            if t[0] == "formalisms/SimpleClassDiagrams":
                assert t[1] != None
            elif t[1] == "formalisms/Bottom":
                # Type mapping is empty
                assert t[1] == None
            assert t[2] == None

        # Check if a type mapping is created when model is opened as bottom
        alter_context("users/user/test/a", "formalisms/Bottom")
        element_list("users/user/test/a")
        alter_context("users/user/test/a", "formalisms/SimpleClassDiagrams")

        ts = model_types("users/user/test/a")
        assert len(ts) == 2
        while ts:
            t = ts.pop()
            assert t[0] in ["formalisms/SimpleClassDiagrams", "formalisms/Bottom"]
            assert t[1] != None
            assert t[2] == None

        # Check if we can add a second one to the same MM
        conformance_add("users/user/test/a", "formalisms/Bottom")
        ts = model_types("users/user/test/a")
        assert len(ts) == 3
        assert ("formalisms/Bottom", None, None) in ts
        found = False
        while ts:
            t = ts.pop()
            assert t[0] in ["formalisms/SimpleClassDiagrams", "formalisms/Bottom"]
            if t[0] == "formalisms/SimpleClassDiagrams":
                assert t[1] != None
            elif t[0] == "formalisms/Bottom":
                # Type mapping is empty
                if t[1] != None:
                    found = True
            assert t[2] == None
        assert found

        # Check add of non-existing model
        try:
            conformance_add("users/user/test/b", "formalisms/Bottom")
            self.fail()
        except UnknownModel:
            pass

        # Check add of non-existing metamodel
        model_add("users/user/test/b", "formalisms/SimpleClassDiagrams")
        before = model_types("users/user/test/b")
        try:
            conformance_add("users/user/test/b", "formalisms/Bottom2")
            self.fail()
        except UnknownModel:
            assert model_types("users/user/test/b") == before

        # Check conformance_add for a folder model
        try:
            conformance_add("users/user/test", "formalisms/Bottom")
            self.fail()
        except NotAModel:
            pass

        # Check conformance_add for a folder metamodel
        try:
            conformance_add("users/user/test/a", "formalisms")
            self.fail()
        except NotAModel:
            pass

    def test_op_conformance_delete(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            Class A {}
            Class B {}
            Association C (A, B) {}
            """)

        # Add conformance bottom relation
        conformance_add("users/user/test/a", "formalisms/Bottom")

        # Verify initial state
        ts = model_types("users/user/test/a")
        assert len(ts) == 2
        while ts:
            t = ts.pop()
            assert t[0] in ["formalisms/SimpleClassDiagrams", "formalisms/Bottom"]
            if t[0] == "formalisms/SimpleClassDiagrams":
                assert t[1] != None
            elif t[1] == "formalisms/Bottom":
                # Type mapping is empty
                assert t[1] == None
            assert t[2] == None

        # Now remove it again
        conformance_delete("users/user/test/a", "formalisms/Bottom", "")
        ts = model_types("users/user/test/a")
        assert len(ts) == 1
        t = ts.pop()
        assert t[0] == "formalisms/SimpleClassDiagrams"
        assert t[1] != None
        assert t[2] == None

        # Add conformance bottom relation twice
        conformance_add("users/user/test/a", "formalisms/Bottom")
        conformance_add("users/user/test/a", "formalisms/Bottom")

        # Should be there twice, but set flattens it to once
        ts = model_types("users/user/test/a")
        assert len(ts) == 2
        while ts:
            t = ts.pop()
            assert t[0] in ["formalisms/SimpleClassDiagrams", "formalisms/Bottom"]
            if t[0] == "formalisms/SimpleClassDiagrams":
                assert t[1] != None
            elif t[1] == "formalisms/Bottom":
                # Type mapping is empty
                assert t[1] == None
            assert t[2] == None

        # Now remove it (once called, but invoked for all occurences!)
        conformance_delete("users/user/test/a", "formalisms/Bottom", "")

        ts = model_types("users/user/test/a")
        assert len(ts) == 1
        t = ts.pop()
        assert t[0] == "formalisms/SimpleClassDiagrams"
        assert t[1] != None
        assert t[2] == None

        # Now actually test filtering capabilities
        alter_context("users/user/test/a", "formalisms/Bottom")
        element_list("users/user/test/a")
        alter_context("users/user/test/a", "formalisms/SimpleClassDiagrams")

        # Model should be there with a type mapping model
        ts = model_types("users/user/test/a")
        assert len(ts) == 2
        while ts:
            t = ts.pop()
            assert t[0] in ["formalisms/SimpleClassDiagrams", "formalisms/Bottom"]
            assert t[1] != None
            assert t[2] == None

        # Add a second bottom relation
        conformance_add("users/user/test/a", "formalisms/Bottom")

        # And remove the one without model, to make sure that it is not a wildcard!
        conformance_delete("users/user/test/a", "formalisms/Bottom", "")

        ts = model_types("users/user/test/a")
        assert len(ts) == 2
        while ts:
            t = ts.pop()
            assert t[0] in ["formalisms/SimpleClassDiagrams", "formalisms/Bottom"]
            assert t[1] != None
            assert t[2] == None

        # Add again, but now remove the one with type mapping
        conformance_add("users/user/test/a", "formalisms/Bottom")
        
        # First try one that doesn't match...
        conformance_delete("users/user/test/a", "formalisms/Bottom", "NOTHING")

        ts = model_types("users/user/test/a")
        assert len(ts) == 3
        while ts:
            t = ts.pop()
            assert t[0] in ["formalisms/SimpleClassDiagrams", "formalisms/Bottom"]
            assert t[2] == None

        # Now try one with the actual name
        actual_name = [i for i in model_types("users/user/test/a") if i[0] == "formalisms/Bottom" and i[1] != None][0][1]

        assert len(model_types("users/user/test/a")) == 3
        conformance_delete("users/user/test/a", "formalisms/SimpleClassDiagrams", actual_name)
        # Nothing removed, because of wrong TM model
        assert len(model_types("users/user/test/a")) == 3

        # Now remove correct one
        conformance_delete("users/user/test/a", "formalisms/Bottom", actual_name)
        ts = model_types("users/user/test/a")
        assert len(ts) == 2
        while ts:
            t = ts.pop()
            assert t[0] in ["formalisms/SimpleClassDiagrams", "formalisms/Bottom"]
            if t[0] == "formalisms/SimpleClassDiagrams":
                assert t[1] != None
            elif t[1] == "formalisms/Bottom":
                # Type mapping is empty
                assert t[1] == None
            assert t[2] == None

        # Check non-existing model
        try:
            conformance_delete("users/user/test/b", "formalisms/Bottom", "")
            self.fail()
        except UnknownModel:
            pass

        # Check add of non-existing metamodel
        model_add("users/user/test/b", "formalisms/SimpleClassDiagrams")
        before = model_types("users/user/test/b")
        try:
            conformance_delete("users/user/test/b", "formalisms/Bottom2", "")
            self.fail()
        except UnknownModel:
            assert model_types("users/user/test/b") == before

        # Check folder as model
        try:
            conformance_delete("users/user/test", "formalisms/Bottom", "")
            self.fail()
        except NotAModel:
            pass

        # Check folder as metamodel
        try:
            conformance_delete("users/user/test/a", "formalisms", "")
            self.fail()
        except NotAModel:
            pass

    def test_op_transformation_between_MANUAL(self):
        # Add set of transformations
        def create_transf(name, inputs, outputs):
            transformation_add_MANUAL({"key_%s" % i: "users/user/test/" + i for i in inputs}, {"key_%s" % i: "users/user/test/" + i for i in outputs}, "users/user/test/" + name)

        model_add("users/user/test/A", "formalisms/SimpleClassDiagrams")
        model_add("users/user/test/B", "formalisms/SimpleClassDiagrams")
        model_add("users/user/test/C", "formalisms/SimpleClassDiagrams")
        model_add("users/user/test/D", "formalisms/SimpleClassDiagrams")
        model_add("users/user/test/E", "formalisms/SimpleClassDiagrams")

        create_transf("1", ["A"], ["B"])
        create_transf("2", ["A"], ["C", "D"])
        create_transf("3", ["B", "C"], ["D"])
        create_transf("4", ["A", "B"], ["D", "E"])

        # Test normal operation
        def check_transformations(inputs, outputs, expected):
            assert transformation_between({"key_" + i: "users/user/test/" + i for i in inputs}, {"key_" + i: "users/user/test/" + i for i in outputs}) == set(["users/user/test/" + i for i in expected])

        # Obvious ones, with a perfect signature match
        check_transformations(["A"], ["B"], ["1"])
        check_transformations(["A"], ["C", "D"], ["2"])
        check_transformations(["B", "C"], ["D"], ["3"])
        check_transformations(["A", "B"], ["D", "E"], ["4"])

        # Incomplete matches
        check_transformations(["A"], [], ["1", "2", "4"])
        check_transformations(["B"], [], ["3", "4"])
        check_transformations(["C"], [], ["3"])
        check_transformations(["D"], [], [])
        check_transformations(["E"], [], [])
        check_transformations([], ["A"], [])
        check_transformations([], ["B"], ["1"])
        check_transformations([], ["C"], ["2"])
        check_transformations([], ["D"], ["2", "3", "4"])
        check_transformations([], ["E"], ["4"])

        # Complete match, which doesn't return as it would give all transformations in the Modelverse...
        try:
            check_transformations([], [], [])
            self.fail()
        except IncorrectFormat:
            pass

        # Non-existing model (source)
        try:
            assert transformation_between({"key_A": "users/user/test/Z"}, {"key_B": "users/user/test/B"}) == set([])
            self.fail()
        except UnknownModel:
            pass

        # Key mismatch (source)
        assert transformation_between({"key_B": "users/user/test/A"}, {"key_B": "users/user/test/B"}) == set([])

        # Model mismatch (source)
        assert transformation_between({"key_A": "users/user/test/B"}, {"key_B": "users/user/test/B"}) == set([])

        # Non-existing model (target)
        try:
            assert transformation_between({"key_A": "users/user/test/A"}, {"key_B": "users/user/test/Z"}) == set([])
            self.fail()
        except UnknownModel:
            pass

        # Key mismatch (target)
        assert transformation_between({"key_A": "users/user/test/A"}, {"key_A": "users/user/test/B"}) == set([])

        # Model mismatch (target)
        assert transformation_between({"key_A": "users/user/test/A"}, {"key_B": "users/user/test/A"}) == set([])

        # Source model is folder
        try:
            transformation_between({"key_A": "users"}, {})
            self.fail()
        except NotAModel:
            pass

        # Target model is folder
        try:
            transformation_between({}, {"key_A": "users"})
            self.fail()
        except NotAModel:
            pass

    def test_op_transformation_between_AL(self):
        # Add set of transformations
        def create_transf(name, inputs, outputs):
            transformation_add_AL({"key_%s" % i: "users/user/test/" + i for i in inputs}, {"key_%s" % i: "users/user/test/" + i for i in outputs}, "users/user/test/" + name, "Void function main():\n\treturn!")

        model_add("users/user/test/A", "formalisms/SimpleClassDiagrams")
        model_add("users/user/test/B", "formalisms/SimpleClassDiagrams")
        model_add("users/user/test/C", "formalisms/SimpleClassDiagrams")
        model_add("users/user/test/D", "formalisms/SimpleClassDiagrams")
        model_add("users/user/test/E", "formalisms/SimpleClassDiagrams")

        create_transf("1", ["A"], ["B"])
        create_transf("2", ["A"], ["C", "D"])
        create_transf("3", ["B", "C"], ["D"])
        create_transf("4", ["A", "B"], ["D", "E"])

        # Test normal operation
        def check_transformations(inputs, outputs, expected):
            assert transformation_between({"key_" + i: "users/user/test/" + i for i in inputs}, {"key_" + i: "users/user/test/" + i for i in outputs}) == set(["users/user/test/" + i for i in expected])

        # Obvious ones, with a perfect signature match
        check_transformations(["A"], ["B"], ["1"])
        check_transformations(["A"], ["C", "D"], ["2"])
        check_transformations(["B", "C"], ["D"], ["3"])
        check_transformations(["A", "B"], ["D", "E"], ["4"])

        # Incomplete matches
        check_transformations(["A"], [], ["1", "2", "4"])
        check_transformations(["B"], [], ["3", "4"])
        check_transformations(["C"], [], ["3"])
        check_transformations(["D"], [], [])
        check_transformations(["E"], [], [])
        check_transformations([], ["A"], [])
        check_transformations([], ["B"], ["1"])
        check_transformations([], ["C"], ["2"])
        check_transformations([], ["D"], ["2", "3", "4"])
        check_transformations([], ["E"], ["4"])

        # Complete match, which doesn't return as it would give all transformations in the Modelverse...
        try:
            check_transformations([], [], [])
            self.fail()
        except IncorrectFormat:
            pass

        # Non-existing model (source)
        try:
            assert transformation_between({"key_A": "users/user/test/Z"}, {"key_B": "users/user/test/B"}) == set([])
            self.fail()
        except UnknownModel:
            pass

        # Key mismatch (source)
        assert transformation_between({"key_B": "users/user/test/A"}, {"key_B": "users/user/test/B"}) == set([])

        # Model mismatch (source)
        assert transformation_between({"key_A": "users/user/test/B"}, {"key_B": "users/user/test/B"}) == set([])

        # Non-existing model (target)
        try:
            assert transformation_between({"key_A": "users/user/test/A"}, {"key_B": "users/user/test/Z"}) == set([])
            self.fail()
        except UnknownModel:
            pass

        # Key mismatch (target)
        assert transformation_between({"key_A": "users/user/test/A"}, {"key_A": "users/user/test/B"}) == set([])

        # Model mismatch (target)
        assert transformation_between({"key_A": "users/user/test/A"}, {"key_B": "users/user/test/A"}) == set([])

        # Source model is folder
        try:
            transformation_between({"key_A": "users"}, {})
            self.fail()
        except NotAModel:
            pass

        # Target model is folder
        try:
            transformation_between({}, {"key_A": "users"})
            self.fail()
        except NotAModel:
            pass

    def test_op_transformation_between_MT(self):
        # Add set of transformations
        def create_transf(name, inputs, outputs):
            transformation_add_MT({"key_%s" % i: "users/user/test/" + i for i in inputs}, {"key_%s" % i: "users/user/test/" + i for i in outputs}, "users/user/test/" + name, "Composite a{}")

        model_add("users/user/test/A", "formalisms/SimpleClassDiagrams")
        model_add("users/user/test/B", "formalisms/SimpleClassDiagrams")
        model_add("users/user/test/C", "formalisms/SimpleClassDiagrams")
        model_add("users/user/test/D", "formalisms/SimpleClassDiagrams")
        model_add("users/user/test/E", "formalisms/SimpleClassDiagrams")

        create_transf("1", ["A"], ["B"])
        create_transf("2", ["A"], ["C", "D"])
        create_transf("3", ["B", "C"], ["D"])
        create_transf("4", ["A", "B"], ["D", "E"])

        # Test normal operation
        def check_transformations(inputs, outputs, expected):
            assert transformation_between({"key_" + i: "users/user/test/" + i for i in inputs}, {"key_" + i: "users/user/test/" + i for i in outputs}) == set(["users/user/test/" + i for i in expected])

        # Obvious ones, with a perfect signature match
        check_transformations(["A"], ["B"], ["1"])
        check_transformations(["A"], ["C", "D"], ["2"])
        check_transformations(["B", "C"], ["D"], ["3"])
        check_transformations(["A", "B"], ["D", "E"], ["4"])

        # Incomplete matches
        check_transformations(["A"], [], ["1", "2", "4"])
        check_transformations(["B"], [], ["3", "4"])
        check_transformations(["C"], [], ["3"])
        check_transformations(["D"], [], [])
        check_transformations(["E"], [], [])
        check_transformations([], ["A"], [])
        check_transformations([], ["B"], ["1"])
        check_transformations([], ["C"], ["2"])
        check_transformations([], ["D"], ["2", "3", "4"])
        check_transformations([], ["E"], ["4"])

        # Complete match, which doesn't return as it would give all transformations in the Modelverse...
        try:
            check_transformations([], [], [])
            self.fail()
        except IncorrectFormat:
            pass

        # Non-existing model (source)
        try:
            assert transformation_between({"key_A": "users/user/test/Z"}, {"key_B": "users/user/test/B"}) == set([])
            self.fail()
        except UnknownModel:
            pass

        # Key mismatch (source)
        assert transformation_between({"key_B": "users/user/test/A"}, {"key_B": "users/user/test/B"}) == set([])

        # Model mismatch (source)
        assert transformation_between({"key_A": "users/user/test/B"}, {"key_B": "users/user/test/B"}) == set([])

        # Non-existing model (target)
        try:
            assert transformation_between({"key_A": "users/user/test/A"}, {"key_B": "users/user/test/Z"}) == set([])
            self.fail()
        except UnknownModel:
            pass

        # Key mismatch (target)
        assert transformation_between({"key_A": "users/user/test/A"}, {"key_A": "users/user/test/B"}) == set([])

        # Model mismatch (target)
        assert transformation_between({"key_A": "users/user/test/A"}, {"key_B": "users/user/test/A"}) == set([])

        # Source model is folder
        try:
            transformation_between({"key_A": "users"}, {})
            self.fail()
        except NotAModel:
            pass

        # Target model is folder
        try:
            transformation_between({}, {"key_A": "users"})
            self.fail()
        except NotAModel:
            pass

    def test_op_transformation_add_MANUAL(self):
        # Add models for transformation
        model_add("users/user/test/A", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute String {}
            Class A {
                name = "A"
                name : String
            }
            """)
        model_add("users/user/test/B", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute String {}
            Class B {
                name = "B"
                name : String
            }
            """)
        model_add("users/user/test/a", "users/user/test/A", "A {}")
        model_add("users/user/test/b", "users/user/test/B", "B {}")

        # Add a transformation with normal signature
        transformation_add_MANUAL({"MODEL_A": "users/user/test/A"}, {"MODEL_B": "users/user/test/B"}, "users/user/test/c")

        # Add a transformation with normal signature and merged metamodel changes
        transformation_add_MANUAL({"MODEL_A": "users/user/test/A"}, {"MODEL_B": "users/user/test/B"}, "users/user/test/d", operation_MAN_0)
        transformation_add_MANUAL({"MODEL_A": "users/user/test/A", "MODEL_B": "users/user/test/B"}, {"MODEL_B": "users/user/test/B"}, "users/user/test/e", operation_MAN_1)

        try:
            transformation_add_MANUAL({"MODEL_A": "users/user/test/A", "MODEL_B": "users/user/test/B"}, {"MODEL_B": "users/user/test/A"}, "users/user/test/f")
            self.fail()
        except DifferingModelsForKey:
            pass

        # Add a transformation with empty signature
        transformation_add_MANUAL({}, {}, "users/user/test/g")

        # Add a transformation with empty signature and a callback
        try:
            transformation_add_MANUAL({}, {}, "users/user/test/h", null_operation)
            self.fail()
        except CallbackOnEmptySignature:
            assert "h" not in model_list("users/user/test")

        # Add transformation with unknown metamodel in signature (input)
        try:
            transformation_add_MANUAL({"MODEL_A": "adbdsf"}, {"MODEL_B": "users/user/test/A"}, "users/user/test/i")
            self.fail()
        except UnknownModel:
            assert "i" not in model_list("users/user/test")

        # Add transformation with unknown metamodel in signature (output)
        try:
            transformation_add_MANUAL({"MODEL_A": "users/user/test/A"}, {"MODEL_B": "adfad"}, "users/user/test/j")
            self.fail()
        except UnknownModel:
            assert "j" not in model_list("users/user/test")

        # Add transformation with unreadable metamodel in signature (input)
        try:
            transformation_add_MANUAL({"MODEL_A": "administration/core"}, {"MODEL_B": "users/user/test/B"}, "users/user/test/k")
            self.fail()
        except ReadPermissionDenied:
            assert "k" not in model_list("users/user/test")

        # Add transformation with unreadable metamodel in signature (output)
        try:
            transformation_add_MANUAL({"MODEL_A": "users/user/test/A"}, {"MODEL_B": "administration/core"}, "users/user/test/l")
            self.fail()
        except ReadPermissionDenied:
            assert "l" not in model_list("users/user/test")

        # Try to use a non-RAMifiable metamodel in input
        try:
            transformation_add_MANUAL({"MODEL_A": "users/user/test/a"}, {}, "users/user/test/m")
            self.fail()
        except UnknownM3:
            assert "m" not in model_list("users/user/test")

        # Try to use a non-RAMifiable metamodel in output
        try:
            transformation_add_MANUAL({}, {"MODEL_A": "users/user/test/a"}, "users/user/test/n")
            self.fail()
        except UnknownM3:
            assert "n" not in model_list("users/user/test")

        # Source metamodel is folder
        try:
            transformation_add_MANUAL({"MODEL_A": "users"}, {"MODEL_B": "formalisms/SimpleClassDiagrams"}, "users/user/test/p")
            self.fail()
        except NotAModel:
            assert "p" not in model_list("users/user/test")

        # Target metamodel is folder
        try:
            transformation_add_MANUAL({"MODEL_A": "formalisms/SimpleClassDiagrams"}, {"MODEL_B": "users"}, "users/user/test/q")
            self.fail()
        except NotAModel:
            assert "q" not in model_list("users/user/test")

    def test_op_transformation_add_AL(self):
        # Add models for transformation
        model_add("users/user/test/A", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute String {}
            Class A {
                name = "A"
                name : String
            }
            """)
        model_add("users/user/test/B", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute String {}
            Class B {
                name = "B"
                name : String
            }
            """)
        model_add("users/user/test/a", "users/user/test/A", "A {}")
        model_add("users/user/test/b", "users/user/test/B", "B {}")

        default_function = "Boolean function main(model : Element):\n\treturn True!"

        # Add a transformation with normal signature
        transformation_add_AL({"MODEL_A": "users/user/test/A"}, {"MODEL_B": "users/user/test/B"}, "users/user/test/c", default_function)

        # Add a transformation with normal signature and merged metamodel changes


        transformation_add_AL({"MODEL_A": "users/user/test/A"}, {"MODEL_B": "users/user/test/B"}, "users/user/test/d", default_function, operation_AL_0)
        transformation_add_AL({"MODEL_A": "users/user/test/A", "MODEL_B": "users/user/test/B"}, {"MODEL_B": "users/user/test/B"}, "users/user/test/e", default_function, operation_AL_1)

        try:
            transformation_add_AL({"MODEL_A": "users/user/test/A", "MODEL_B": "users/user/test/B"}, {"MODEL_B": "users/user/test/A"}, "users/user/test/f", default_function)
            self.fail()
        except DifferingModelsForKey:
            pass

        # Add a transformation with empty signature
        transformation_add_AL({}, {}, "users/user/test/g", default_function)

        # Add a transformation with empty signature and a callback
        try:
            transformation_add_AL({}, {}, "users/user/test/h", default_function, null_operation)
            self.fail()
        except CallbackOnEmptySignature:
            assert "h" not in model_list("users/user/test")

        # Add transformation with unknown metamodel in signature (input)
        try:
            transformation_add_AL({"MODEL_A": "adbdsf"}, {"MODEL_B": "users/user/test/A"}, "users/user/test/i", default_function)
            self.fail()
        except UnknownModel:
            assert "i" not in model_list("users/user/test")

        # Add transformation with unknown metamodel in signature (output)
        try:
            transformation_add_AL({"MODEL_A": "users/user/test/A"}, {"MODEL_B": "adfad"}, "users/user/test/j", default_function)
            self.fail()
        except UnknownModel:
            assert "j" not in model_list("users/user/test")

        # Add transformation with unreadable metamodel in signature (input)
        try:
            transformation_add_AL({"MODEL_A": "administration/core"}, {"MODEL_B": "users/user/test/B"}, "users/user/test/k", default_function)
            self.fail()
        except ReadPermissionDenied:
            assert "k" not in model_list("users/user/test")

        # Add transformation with unreadable metamodel in signature (output)
        try:
            transformation_add_AL({"MODEL_A": "users/user/test/A"}, {"MODEL_B": "administration/core"}, "users/user/test/l", default_function)
            self.fail()
        except ReadPermissionDenied:
            assert "l" not in model_list("users/user/test")

        # Try to use a non-RAMifiable metamodel in input
        try:
            transformation_add_AL({"MODEL_A": "users/user/test/a"}, {}, "users/user/test/m", default_function)
            self.fail()
        except UnknownM3:
            assert "m" not in model_list("users/user/test")

        # Try to use a non-RAMifiable metamodel in output
        try:
            transformation_add_AL({}, {"MODEL_A": "users/user/test/a"}, "users/user/test/n", default_function)
            self.fail()
        except UnknownM3:
            assert "n" not in model_list("users/user/test")

        # Compilation error
        try:
            transformation_add_AL({}, {}, "users/user/test/o", "adfadf")
            self.fail()
        except CompilationError:
            assert "o" not in model_list("users/user/test")

        # Source metamodel is folder
        try:
            transformation_add_AL({"MODEL_A": "users"}, {"MODEL_B": "formalisms/SimpleClassDiagrams"}, "users/user/test/p", default_function)
            self.fail()
        except NotAModel:
            assert "p" not in model_list("users/user/test")

        # Target metamodel is folder
        try:
            transformation_add_AL({"MODEL_A": "formalisms/SimpleClassDiagrams"}, {"MODEL_B": "users"}, "users/user/test/q", default_function)
            self.fail()
        except NotAModel:
            assert "q" not in model_list("users/user/test")

    def test_op_transformation_add_MT(self):
        # Add models for transformation
        model_add("users/user/test/A", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute String {}
            Class A {
                name = "A"
                name : String
            }
            """)
        model_add("users/user/test/B", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute String {}
            Class B {
                name = "B"
                name : String
            }
            """)
        model_add("users/user/test/a", "users/user/test/A", "A {}")
        model_add("users/user/test/b", "users/user/test/B", "B {}")

        default_function = "Composite c{}"

        # Add a transformation with normal signature
        transformation_add_MT({"MODEL_A": "users/user/test/A"}, {"MODEL_B": "users/user/test/B"}, "users/user/test/c", default_function)

        # Add a transformation with normal signature and merged metamodel changes
        transformation_add_MT({"MODEL_A": "users/user/test/A"}, {"MODEL_B": "users/user/test/B"}, "users/user/test/d", default_function, operation_MT_0)


        transformation_add_MT({"MODEL_A": "users/user/test/A", "MODEL_B": "users/user/test/B"}, {"MODEL_B": "users/user/test/B"}, "users/user/test/e", default_function, operation_MT_1)

        try:
            transformation_add_MT({"MODEL_A": "users/user/test/A", "MODEL_B": "users/user/test/B"}, {"MODEL_B": "users/user/test/A"}, "users/user/test/f", default_function)
            self.fail()
        except DifferingModelsForKey:
            pass

        # Add a transformation with empty signature
        try:
            transformation_add_MT({}, {}, "users/user/test/g", default_function)
            self.fail()
        except EmptySignature:
            assert "g" not in model_list("users/user/test")

        # Add a transformation with empty signature and a callback
        try:
            transformation_add_MT({}, {}, "users/user/test/h", default_function, null_operation)
            self.fail()
        except CallbackOnEmptySignature:
            assert "h" not in model_list("users/user/test")

        # Add transformation with unknown metamodel in signature (input)
        try:
            transformation_add_MT({"MODEL_A": "adbdsf"}, {"MODEL_B": "users/user/test/A"}, "users/user/test/i", default_function)
            self.fail()
        except UnknownModel:
            assert "i" not in model_list("users/user/test")

        # Add transformation with unknown metamodel in signature (output)
        try:
            transformation_add_MT({"MODEL_A": "users/user/test/A"}, {"MODEL_B": "adfad"}, "users/user/test/j", default_function)
            self.fail()
        except UnknownModel:
            assert "j" not in model_list("users/user/test")

        # Add transformation with unreadable metamodel in signature (input)
        try:
            transformation_add_MT({"MODEL_A": "administration/core"}, {"MODEL_B": "users/user/test/B"}, "users/user/test/k", default_function)
            self.fail()
        except ReadPermissionDenied:
            assert "k" not in model_list("users/user/test")

        # Add transformation with unreadable metamodel in signature (output)
        try:
            transformation_add_MT({"MODEL_A": "users/user/test/A"}, {"MODEL_B": "administration/core"}, "users/user/test/l", default_function)
            self.fail()
        except ReadPermissionDenied:
            assert "l" not in model_list("users/user/test")

        # Try to use a non-RAMifiable metamodel in input
        try:
            transformation_add_MT({"MODEL_A": "users/user/test/a"}, {}, "users/user/test/m", default_function)
            self.fail()
        except UnknownM3:
            assert "m" not in model_list("users/user/test")

        # Try to use a non-RAMifiable metamodel in output
        try:
            transformation_add_MT({}, {"MODEL_A": "users/user/test/a"}, "users/user/test/n", default_function)
            self.fail()
        except UnknownM3:
            assert "n" not in model_list("users/user/test")

        # Compilation error
        try:
            transformation_add_MT({"MODEL_A": "users/user/test/A"}, {}, "users/user/test/o", "adfa")
            self.fail()
        except CompilationError:
            assert "o" not in model_list("users/user/test")

        # Source metamodel is folder
        try:
            transformation_add_MT({"MODEL_A": "users"}, {"MODEL_B": "formalisms/SimpleClassDiagrams"}, "users/user/test/p", default_function)
            self.fail()
        except NotAModel:
            assert "p" not in model_list("users/user/test")

        # Target metamodel is folder
        try:
            transformation_add_MT({"MODEL_A": "formalisms/SimpleClassDiagrams"}, {"MODEL_B": "users"}, "users/user/test/q", default_function)
            self.fail()
        except NotAModel:
            assert "q" not in model_list("users/user/test")

    def test_op_transformation_execute_MANUAL(self):
        # Add models for transformation
        model_add("users/user/test/A", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute String {}
            Class A {
                name = "A"
                name : String
            }
            """)
        model_add("users/user/test/B", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute String {}
            Class B {
                name = "B"
                name : String
            }
            """)
        model_add("users/user/test/a", "users/user/test/A", "A {}")
        model_add("users/user/test/b", "users/user/test/B", "B {}")

        # Add some activity to execute
        transformation_add_MANUAL({}, {}, "users/user/test/c")

        # Now execute
        transformation_execute_MANUAL("users/user/test/c", {}, {}, model_is_empty_operation)

        # Now do the same, but with a model
        transformation_add_MANUAL({"MODEL_A": "users/user/test/A"}, {"MODEL_B": "users/user/test/B"}, "users/user/test/d")

        transformation_execute_MANUAL("users/user/test/d", {"MODEL_A": "users/user/test/a"}, {"MODEL_B": "users/user/test/aa"}, operation_exec_1)
        lst = element_list_nice("users/user/test/aa")
        assert len(lst) == 1
        assert lst[0]["__type"] == "B"
        assert lst[0]["name"] == None
        assert lst[0]["__id"].startswith("__")

        # Execute activity with non-existing model (input)
        try:
            transformation_execute_MANUAL("users/user/test/d", {"MODEL_A": "dfadf"}, {"MODEL_B": "users/user/test/f"}, null_operation)
            self.fail()
        except UnknownModel:
            assert "f" not in model_list("users/user/test")

        # Execute non-existing activity
        try:
            transformation_execute_MANUAL("adfadf", {}, {"MODEL_B": "users/user/test/abc"}, null_operation)
            self.fail()
        except UnknownModel:
            assert "abc" not in model_list("users/user/test")

        # Unbound source model
        try:
            transformation_execute_MANUAL("users/user/test/d", {}, {"MODEL_B": "users/user/test/f"}, null_operation)
            self.fail()
        except SourceModelNotBound:
            pass

        # Unknown source key
        try:
            transformation_execute_MANUAL("users/user/test/d", {"ABC": "users/user/test/a", "MODEL_A": "users/user/test/a"}, {"MODEL_B": "users/user/test/f"}, null_operation)
            self.fail()
        except SourceModelNotBound:
            pass

        # Unknown target key
        try:
            transformation_execute_MANUAL("users/user/test/d", {"MODEL_A": "users/user/test/a"}, {"DEF": "users/user/test/b"}, null_operation)
            self.fail()
        except TargetModelNotBound:
            pass

        # Read permissions denied source
        try:
            transformation_execute_MANUAL("users/user/test/d", {"MODEL_A": "administration/core"}, {"MODEL_B": "users/user/test/f"}, null_operation)
            self.fail()
        except ReadPermissionDenied:
            pass

        # Execute permissions denied activity
        try:
            transformation_execute_MANUAL("administration/core", {"MODEL_A": "users/user/test/a"}, {"MODEL_B": "users/user/test/f"}, null_operation)
            self.fail()
        except ExecutePermissionDenied:
            pass

        # Write permissions denied target (folder)
        try:
            transformation_execute_MANUAL("users/user/test/d", {"MODEL_A": "users/user/test/a"}, {"MODEL_B": "administration/abc"}, null_operation)
            self.fail()
        except WritePermissionDenied:
            pass

        # Write permissions denied target (model)
        try:
            transformation_execute_MANUAL("users/user/test/d", {"MODEL_A": "users/user/test/a"}, {"MODEL_B": "administration/core"}, null_operation)
            self.fail()
        except WritePermissionDenied:
            pass

        # Execute activity with non-conforming input models
        try:
            transformation_execute_MANUAL("users/user/test/d", {"MODEL_A": "users/user/test/b"}, {"MODEL_B": "users/user/test/f"}, null_operation)
            self.fail()
        except TypeMismatch:
            assert "f" not in model_list("users/user/test")

        # Execute activity with non-conforming output models that already exist
        assert len(element_list_nice("users/user/test/aa")) > 0
        try:
            transformation_execute_MANUAL("users/user/test/d", {"MODEL_A": "users/user/test/b"}, {"MODEL_B": "users/user/test/aa"}, null_operation)
            self.fail()
        except TypeMismatch:
            assert len(element_list_nice("users/user/test/aa")) > 0

        # Source model is folder
        try:
            transformation_execute_MANUAL("users/user/test/d", {"MODEL_A": "users"}, {"MODEL_B": "users/user/test/f"})
            self.fail()
        except NotAModel:
            pass

        # Target model is folder
        try:
            transformation_execute_MANUAL("users/user/test/d", {"MODEL_A": "users/user/test/b"}, {"MODEL_B": "users"})
            self.fail()
        except NotAModel:
            pass

        # Execute activity with colliding models, which means that it is copied
        transformation_add_MANUAL({"MODEL_A": "users/user/test/A"}, {"MODEL_A": "users/user/test/A"}, "users/user/test/f")
        transformation_execute_MANUAL("users/user/test/f", {"MODEL_A": "users/user/test/a"}, {"MODEL_A": "users/user/test/aaa"}, null_operation)
        assert len(element_list_nice("users/user/test/a")) == len(element_list_nice("users/user/test/aaa"))

    def test_op_transformation_execute_AL(self):
        # Add models for transformation
        model_add("users/user/test/A", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute String {}
            Class A {
                name = "A"
                name : String
            }
            """)
        model_add("users/user/test/B", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute String {}
            Class B {
                name = "B"
                name : String
            }
            """)
        model_add("users/user/test/a", "users/user/test/A", "A a {}")
        model_add("users/user/test/b", "users/user/test/B", "B b {}")

        default_code_empty = "Boolean function main(model : Element):\n\treturn True!"
        default_code_true = "include \"modelling.alh\"\nBoolean function main(model : Element):\n\tinstantiate_attribute(model, \"MODEL_A/a\", \"name\", \"CHANGED\")\n\tinstantiate_node(model, \"MODEL_B/B\", \"\")\n\treturn True!"
        default_code_false = "include \"modelling.alh\"\nBoolean function main(model : Element):\n\tinstantiate_attribute(model, \"MODEL_A/a\", \"name\", \"CHANGED\")\n\tinstantiate_node(model, \"MODEL_B/B\", \"\")\n\treturn False!"

        # Add some activity to execute
        transformation_add_AL({}, {}, "users/user/test/c", default_code_true)

        # Now execute
        transformation_execute_AL("users/user/test/c", {}, {})

        # Now do the same, but with a model
        transformation_add_AL({"MODEL_A": "users/user/test/A"}, {"MODEL_B": "users/user/test/B"}, "users/user/test/d", default_code_true)

        transformation_execute_AL("users/user/test/d", {"MODEL_A": "users/user/test/a"}, {"MODEL_B": "users/user/test/aa"})

        lst = element_list_nice("users/user/test/aa")
        assert len(lst) == 1
        assert lst[0]["__type"] == "B"
        assert lst[0]["name"] == None
        assert lst[0]["__id"].startswith("__")

        # Original input model remains unchanged, as it is not an output model
        lst = element_list_nice("users/user/test/a")
        assert len(lst) == 1
        assert lst[0]["__type"] == "A"
        assert lst[0]["name"] == None
        assert lst[0]["__id"] == "a"

        # Execute activity with non-existing model (input)
        try:
            transformation_execute_AL("users/user/test/d", {"MODEL_A": "dfadf"}, {"MODEL_B": "users/user/test/f"})
            self.fail()
        except UnknownModel:
            assert "f" not in model_list("users/user/test")

        # Execute non-existing activity
        try:
            transformation_execute_AL("adfadf", {}, {"MODEL_B": "users/user/test/abc"})
            self.fail()
        except UnknownModel:
            assert "abc" not in model_list("users/user/test")

        # Unbound source model
        try:
            transformation_execute_AL("users/user/test/d", {}, {"MODEL_B": "users/user/test/f"})
            self.fail()
        except SourceModelNotBound:
            pass

        # Unknown source key
        try:
            transformation_execute_AL("users/user/test/d", {"ABC": "users/user/test/a", "MODEL_A": "users/user/test/a"}, {"MODEL_B": "users/user/test/f"})
            self.fail()
        except SourceModelNotBound:
            pass

        # Unknown target key
        try:
            transformation_execute_AL("users/user/test/d", {"MODEL_A": "users/user/test/a"}, {"DEF": "users/user/test/b"})
            self.fail()
        except TargetModelNotBound:
            pass

        # Read permissions denied source
        try:
            transformation_execute_AL("users/user/test/d", {"MODEL_A": "administration/core"}, {"MODEL_B": "users/user/test/f"})
            self.fail()
        except ReadPermissionDenied:
            pass

        # Execute permissions denied activity
        try:
            transformation_execute_AL("administration/core", {"MODEL_A": "users/user/test/a"}, {"MODEL_B": "users/user/test/f"})
            self.fail()
        except ExecutePermissionDenied:
            pass

        # Write permissions denied target (folder)
        try:
            transformation_execute_AL("users/user/test/d", {"MODEL_A": "users/user/test/a"}, {"MODEL_B": "administration/abc"})
            self.fail()
        except WritePermissionDenied:
            pass

        # Write permissions denied target (model)
        try:
            transformation_execute_AL("users/user/test/d", {"MODEL_A": "users/user/test/a"}, {"MODEL_B": "administration/core"})
            self.fail()
        except WritePermissionDenied:
            pass

        # Execute activity with non-conforming input models
        try:
            transformation_execute_AL("users/user/test/d", {"MODEL_A": "users/user/test/b"}, {"MODEL_B": "users/user/test/f"})
            self.fail()
        except TypeMismatch:
            assert "f" not in model_list("users/user/test")

        # Execute activity with non-conforming output models that already exist
        try:
            transformation_execute_AL("users/user/test/d", {"MODEL_A": "users/user/test/b"}, {"MODEL_B": "users/user/test/c"})
            self.fail()
        except TypeMismatch:
            assert len(element_list("users/user/test/c")) > 0

        # Source model is folder
        try:
            transformation_execute_AL("users/user/test/d", {"MODEL_A": "users"}, {"MODEL_B": "users/user/test/f"})
            self.fail()
        except NotAModel:
            pass

        # Target model is folder
        try:
            transformation_execute_AL("users/user/test/d", {"MODEL_A": "users/user/test/b"}, {"MODEL_B": "users"})
            self.fail()
        except NotAModel:
            pass

        # Execute activity with colliding models, which means that it is copied
        transformation_add_AL({"MODEL_A": "users/user/test/A"}, {"MODEL_A": "users/user/test/A"}, "users/user/test/f", default_code_empty)
        transformation_execute_AL("users/user/test/f", {"MODEL_A": "users/user/test/a"}, {"MODEL_A": "users/user/test/aaa"})
        assert len(element_list_nice("users/user/test/a")) == len(element_list_nice("users/user/test/aaa"))

        # Make sure that models are not written when returning False
        transformation_add_AL({"MODEL_A": "users/user/test/A"}, {"MODEL_B": "users/user/test/B"}, "users/user/test/h", default_code_false)
        transformation_execute_AL("users/user/test/h", {"MODEL_A": "users/user/test/a"}, {"MODEL_B": "users/user/test/bb"})
        assert "bb" not in model_list("users/user/test")

        # Try out SC communication: requires back and forth computation
        code_SC = """
            include "io.alh"
            include "primitives.alh"

            Boolean function main(model : Element):
                output("init")
                log("Init OK")
                output(cast_integer(input()) + cast_integer(input()))
                log("Sent out value")
                return True!
        """
        import exec_AL
        ctrl = exec_AL.Controller(keep_running=False)
        thrd = threading.Thread(target=ctrl.start)
        thrd.daemon = True
        transformation_add_AL({"MODEL_A": "users/user/test/A"}, {"MODEL_B": "users/user/test/B"}, "users/user/test/i", code_SC)
        thrd.start()
        transformation_execute_AL("users/user/test/i", {"MODEL_A": "users/user/test/a"}, {"MODEL_B": "users/user/test/bb"}, statechart=(ctrl, "inp", "outp"))
        thrd.join()

    def test_op_transformation_execute_MT(self):
        # Add models for transformation
        model_add("users/user/test/A", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute String {}
            Class A {
                name = "A"
                name : String
            }
            """)
        model_add("users/user/test/B", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute String {}
            Class B {
                name = "B"
                name : String
            }
            """)
        model_add("users/user/test/a", "users/user/test/A", "A a {}")
        model_add("users/user/test/b", "users/user/test/B", "B b {}")

        default_code_empty = """
            Composite a {
                {Contains} Success success {}
            }
            Initial (a, success) {}
            """
        default_code_true = """
            Composite a {
                {Contains} Success success {}
                {Contains} Atomic b {
                    LHS {
                        Pre_MODEL_A/A {
                            label = "0"
                        }
                    }
                    RHS {
                        Post_MODEL_A/A {
                            label = "0"
                            value_name = $
                                String function value(model : Element, name : String, mapping : Element):
                                    return "CHANGED"!
                                $
                        }
                        Post_MODEL_B/B {
                            label = "1"
                        }
                    }
                }
            }
            Initial (a, b) {}
            OnSuccess (b, success) {}
            OnFailure (b, success) {}
            """
        default_code_false = """
            Composite a {
                {Contains} Failure failure {}
                {Contains} Atomic b {
                    LHS {
                        Pre_MODEL_A/A {
                            label = "0"
                        }
                    }
                    RHS {
                        Post_MODEL_A/A {
                            label = "0"
                            value_name = $
                                String function value(model : Element, name : String, mapping : Element):
                                    return "CHANGED"!
                                $
                        }
                        Post_MODEL_B/B {
                            label = "1"
                        }
                    }
                }
            }
            Initial (a, b) {}
            OnSuccess (b, failure) {}
            OnFailure (b, failure) {}
            """

        # Add some activity to execute
        try:
            transformation_add_MT({}, {}, "users/user/test/c", default_code_true)
            self.fail()
        except EmptySignature:
            assert "c" not in model_list("users/user/test")

        # Now do the same, but with a model
        transformation_add_MT({"MODEL_A": "users/user/test/A"}, {"MODEL_B": "users/user/test/B"}, "users/user/test/d", default_code_true)

        transformation_execute_MT("users/user/test/d", {"MODEL_A": "users/user/test/a"}, {"MODEL_B": "users/user/test/aa"})

        lst = element_list_nice("users/user/test/aa")
        assert len(lst) == 1
        assert lst[0]["__type"] == "B"
        assert lst[0]["name"] == None
        assert lst[0]["__id"].startswith("__")

        # Original input model remains unchanged, as it is not an output model
        lst = element_list_nice("users/user/test/a")
        assert len(lst) == 1
        assert lst[0]["__type"] == "A"
        assert lst[0]["name"] == None
        assert lst[0]["__id"] == "a"

        # Execute activity with non-existing model (input)
        try:
            transformation_execute_MT("users/user/test/d", {"MODEL_A": "dfadf"}, {"MODEL_B": "users/user/test/f"})
            self.fail()
        except UnknownModel:
            assert "f" not in model_list("users/user/test")

        # Execute non-existing activity
        try:
            transformation_execute_MT("adfadf", {}, {"MODEL_B": "users/user/test/abc"})
            self.fail()
        except UnknownModel:
            assert "abc" not in model_list("users/user/test")

        # Unbound source model
        try:
            transformation_execute_MT("users/user/test/d", {}, {"MODEL_B": "users/user/test/f"})
            self.fail()
        except SourceModelNotBound:
            pass

        # Unknown source key
        try:
            transformation_execute_MT("users/user/test/d", {"ABC": "users/user/test/a", "MODEL_A": "users/user/test/a"}, {"MODEL_B": "users/user/test/f"})
            self.fail()
        except SourceModelNotBound:
            pass

        # Unknown target key
        try:
            transformation_execute_MT("users/user/test/d", {"MODEL_A": "users/user/test/a"}, {"DEF": "users/user/test/b"})
            self.fail()
        except TargetModelNotBound:
            pass

        # Read permissions denied source
        try:
            transformation_execute_MT("users/user/test/d", {"MODEL_A": "administration/core"}, {"MODEL_B": "users/user/test/f"})
            self.fail()
        except ReadPermissionDenied:
            pass

        # Execute permissions denied activity
        try:
            transformation_execute_MT("administration/core", {"MODEL_A": "users/user/test/a"}, {"MODEL_B": "users/user/test/f"})
            self.fail()
        except ExecutePermissionDenied:
            pass

        # Write permissions denied target (folder)
        try:
            transformation_execute_MT("users/user/test/d", {"MODEL_A": "users/user/test/a"}, {"MODEL_B": "administration/abc"})
            self.fail()
        except WritePermissionDenied:
            pass

        # Write permissions denied target (model)
        try:
            transformation_execute_MT("users/user/test/d", {"MODEL_A": "users/user/test/a"}, {"MODEL_B": "administration/core"})
            self.fail()
        except WritePermissionDenied:
            pass

        # Execute activity with non-conforming input models
        try:
            transformation_execute_MT("users/user/test/d", {"MODEL_A": "users/user/test/b"}, {"MODEL_B": "users/user/test/f"})
            self.fail()
        except TypeMismatch:
            assert "f" not in model_list("users/user/test")

        # Execute activity with non-conforming output models that already exist
        try:
            transformation_execute_MT("users/user/test/d", {"MODEL_A": "users/user/test/b"}, {"MODEL_B": "users/user/test/d"})
            self.fail()
        except TypeMismatch:
            assert len(element_list_nice("users/user/test/d")) > 0

        # Source model is folder
        try:
            transformation_execute_MT("users/user/test/d", {"MODEL_A": "users"}, {"MODEL_B": "users/user/test/f"})
            self.fail()
        except NotAModel:
            pass

        # Target model is folder
        try:
            transformation_execute_MT("users/user/test/d", {"MODEL_A": "users/user/test/b"}, {"MODEL_B": "users"})
            self.fail()
        except NotAModel:
            pass

        # Execute activity with colliding models, which means that it is copied
        transformation_add_MT({"MODEL_A": "users/user/test/A"}, {"MODEL_A": "users/user/test/A"}, "users/user/test/f", default_code_empty)
        transformation_execute_MT("users/user/test/f", {"MODEL_A": "users/user/test/a"}, {"MODEL_A": "users/user/test/aaa"})
        assert len(element_list_nice("users/user/test/a")) == len(element_list_nice("users/user/test/aaa"))

        # Make sure that models are not written when returning False
        transformation_add_MT({"MODEL_A": "users/user/test/A"}, {"MODEL_B": "users/user/test/B"}, "users/user/test/h", default_code_false)
        transformation_execute_MT("users/user/test/h", {"MODEL_A": "users/user/test/a"}, {"MODEL_B": "users/user/test/bb"})
        assert "bb" not in model_list("users/user/test")

        # Try out SC communication: requires back and forth computation
        code_SC = """
            include "primitives.alh"
            include "io.alh"

            Composite a {
                {Contains} Success success {}
                {Contains} Atomic b {
                    LHS {}
                    RHS {
                        action = $
                            Void function act(model : Element, mapping : Element):
                                output("init")
                                log("Init OK")
                                output(cast_integer(input()) + cast_integer(input()))
                                log("Sent out value")
                                return!
                            $
                    }
                }
            }
            Initial (a, b) {}
            OnSuccess (b, success) {}
            OnFailure (b, success) {}
        """
        import exec_MT
        ctrl = exec_MT.Controller(keep_running=False)
        thrd = threading.Thread(target=ctrl.start)
        thrd.daemon = True
        transformation_add_MT({"MODEL_A": "users/user/test/A"}, {"MODEL_B": "users/user/test/B"}, "users/user/test/i", code_SC)
        thrd.start()
        transformation_execute_MT("users/user/test/i", {"MODEL_A": "users/user/test/a"}, {"MODEL_B": "users/user/test/bb"}, statechart=(ctrl, "inp", "outp"))
        thrd.join()

    def test_op_transformation_signature(self):
        # Add some transformations, content of them doesn't matter, only the signature
        model_add("users/user/test/A", "formalisms/SimpleClassDiagrams")
        model_add("users/user/test/B", "formalisms/SimpleClassDiagrams")
        model_add("users/user/test/C", "formalisms/SimpleClassDiagrams")
        import uuid

        def do_test_signature(inputs, outputs):
            counter = str(uuid.uuid4())

            # Test manual
            transformation_add_MANUAL(inputs, outputs, "users/user/test/manual/" + counter)
            assert transformation_signature("users/user/test/manual/" + counter) == (inputs, outputs)

            # Test AL
            transformation_add_AL(inputs, outputs, "users/user/test/al/" + counter, "Boolean function main(model : Element):\n\treturn True!")
            assert transformation_signature("users/user/test/al/" + counter) == (inputs, outputs)

            # Test MT
            if len(inputs) + len(outputs) > 1:
                transformation_add_MT(inputs, outputs, "users/user/test/mt/" + counter, "")
                assert transformation_signature("users/user/test/mt/" + counter) == (inputs, outputs)

        # Test normal operation for all three types of transformation
        do_test_signature({}, {})
        do_test_signature({"A": "users/user/test/A"}, {"B": "users/user/test/B"})
        do_test_signature({}, {"B": "users/user/test/B"})
        do_test_signature({"A": "users/user/test/A"}, {})
        do_test_signature({"A": "users/user/test/A", "B": "users/user/test/B"}, {"A": "users/user/test/A", "B": "users/user/test/B"})

        # Test non-existing model (type of transformation doesn't matter, as never created)
        try:
            transformation_signature("adfadf")
            self.fail()
        except UnknownModel:
            pass

        # Test non-executable model
        try:
            transformation_signature("administration/core")
            self.fail()
        except ExecutePermissionDenied:
            pass

        # Test model that is not an activity
        try:
            transformation_signature("users/user/test/A")
            self.fail()
        except NotAnActivity:
            pass

        # Try folder
        try:
            transformation_signature("users")
            self.fail()
        except NotAnActivity:
            pass

    def test_op_process_signature(self):
        # Add simple process
        model_add("users/user/test/a", "formalisms/ProcessModel", """
            Data a {
                name = "abc"
                type = "def"
            }
            """)

        # Query its signature
        assert process_signature("users/user/test/a") == {"abc": "def"}

        # Add process with some operations
        model_add("users/user/test/b", "formalisms/ProcessModel", """
            Start start {}
            Finish finish {}
            Exec initializePN {
                name = "users/user/test/initialize_PN"
            }
            Exec refinePN {
                name = "users/user/test/refine_PN"
            }
            Exec reachability {
                name = "users/user/test/reachability"
            }
            Exec reachability_print{
                name = "users/user/test/reachability_print"
            }

            Data pn {
                name = "pn"
                type = "users/user/test/PetriNet"
            }
            Data reachability_graph {
                name = "reachability"
                type = "users/user/test/ReachabilityGraph"
            }

            Next (start, initializePN) {}
            Next (initializePN, refinePN) {}
            Next (refinePN, reachability) {}
            Next (reachability, reachability_print) {}
            Next (reachability_print, finish) {}

            Produces (initializePN, pn) {
                name = "PetriNet"
            }
            Consumes (refinePN, pn) {
                name = "PetriNet"
            }
            Produces (refinePN, pn) {
                name = "PetriNet"
            }
            Consumes (reachability, pn) {
                name = "PetriNet"
            }
            Produces (reachability, reachability_graph) {
                name = "ReachabilityGraph"
            }
            Consumes (reachability_print, reachability_graph) {
                name = "ReachabilityGraph"
            }
            """)

        # Query its signature
        assert process_signature("users/user/test/b") == {"pn": "users/user/test/PetriNet", "reachability": "users/user/test/ReachabilityGraph"}

        # Empty model
        model_add("users/user/test/c", "formalisms/ProcessModel")
        assert process_signature("users/user/test/c") == {}

        # Multiple with same name (and same type)
        model_add("users/user/test/d", "formalisms/ProcessModel", """
            Data {
                name = "a"
                type = "b"
            }
            Data {
                name = "a"
                type = "b"
            }
            """)
        try:
            process_signature("users/user/test/d")
            self.fail()
        except NotAValidProcess:
            pass

        # Multiple with same name (and different type)
        model_add("users/user/test/e", "formalisms/ProcessModel", """
            Data {
                name = "a"
                type = "b"
            }
            Data {
                name = "a"
                type = "c"
            }
            """)
        try:
            process_signature("users/user/test/e")
            self.fail()
        except NotAValidProcess:
            pass

        # Try with something that is not even a process model
        model_add("users/user/test/f", "formalisms/SimpleClassDiagrams", """
            Class a {
                name = "a"
            }
            """)
        try:
            process_signature("users/user/test/f")
            self.fail()
        except NotAProcess:
            pass

        # Try non-existing model
        try:
            process_signature("users/user/test/g")
            self.fail()
        except UnknownModel:
            pass

        # Try folder
        try:
            process_signature("users")
            self.fail()
        except NotAModel:
            pass

        # Try unreadable model
        try:
            process_signature("administration/core")
            self.fail()
        except ExecutePermissionDenied:
            pass

    def test_op_process_execute(self):
        # This is only a minimal test, as actual execution will take a long time.
        # For example, the integration tests rely solely on the process_execute operation, thereby being a good test for this.
        # We now just test the execution in corner cases

        # Test with non-existing model
        try:
            process_execute("abc", {})
            self.fail()
        except UnknownModel:
            pass

        # Test with folder model
        try:
            process_execute("formalisms", {})
            self.fail()
        except NotAModel:
            pass

        # Test with unreadable model
        try:
            process_execute("administration/core", {})
            self.fail()
        except ExecutePermissionDenied:
            pass

        # Test with non-process model
        try:
            process_execute("formalisms/SimpleClassDiagrams", {})
            self.fail()
        except NotAProcess:
            pass

        # Test non-existing key in binding
        model_add("users/user/test/a", "formalisms/ProcessModel", """
            Data a {
                name = "abc"
                type = "def"
            }
            Exec b {
                name = "b"
            }
            Consumes (b, a) {
                name = "a"
            }
            Start {}
            Finish {}
            """)
        try:
            process_execute("users/user/test/a", {"def": "users/user/test/b"})
            self.fail()
        except SignatureMismatch:
            pass

        # Test non-readable model in binding
        model_add("users/user/test/b", "formalisms/ProcessModel", """
            Data a {
                name = "abc"
                type = "def"
            }
            Exec b {
                name = "b"
            }
            Consumes (b, a) {
                name = "a"
            }
            Start {}
            Finish {}
            """)
        try:
            process_execute("users/user/test/b", {"abc": "administration/core"})
            self.fail()
        except ReadPermissionDenied:
            pass

        # Test non-writeable model in binding
        model_add("users/user/test/c", "formalisms/ProcessModel", """
            Data a {
                name = "abc"
                type = "def"
            }
            Exec b {
                name = "b"
            }
            Produces (b, a) {
                name = "a"
            }
            Start {}
            Finish {}
            """)
        try:
            process_execute("users/user/test/c", {"abc": "formalisms/SimpleClassDiagrams"})
            self.fail()
        except WritePermissionDenied:
            pass

        # Test combination of both produce and consume
        model_add("users/user/test/d", "formalisms/ProcessModel", """
            Data a {
                name = "abc"
                type = "def"
            }
            Exec b {
                name = "b"
            }
            Produces (b, a) {
                name = "a"
            }
            Consumes (b, a) {
                name = "a"
            }
            Start {}
            Finish {}
            """)
        try:
            process_execute("users/user/test/d", {"abc": "formalisms/SimpleClassDiagrams"})
            self.fail()
        except WritePermissionDenied:
            pass

        # Test unused data
        model_add("users/user/test/e", "formalisms/ProcessModel", """
            Data a {
                name = "abc"
                type = "def"
            }
            Start b {}
            Finish c {}
            Next (b, c) {}
            """)
        process_execute("users/user/test/e", {"abc": "administration/core"})

        # Test folder model in binding
        try:
            process_execute("users/user/test/a", {"abc": "formalisms"})
            self.fail()
        except NotAModel:
            pass

    def test_modelling(self):
        # Add a model
        model_add("users/user/test/Empty", "formalisms/SimpleClassDiagrams")

        # Check that it exists
        compare_locations("users/user/test", set(["Empty"]))

        # Check that it conforms
        assert verify("users/user/test/Empty", "formalisms/SimpleClassDiagrams") == "OK"

        # Check for permissions
        assert model_list_full("users/user/test") == set([(name, "user", "nobody", "221") for name in get_model_list("users/user/test")]) | \
                                                set([("Empty", "user", "nobody", "200")])

        # Instantiate it further
        model_add("users/user/test/my_empty", "users/user/test/Empty")
        assert verify("users/user/test/my_empty", "users/user/test/Empty") == "OK"

        # Check that it exists
        compare_locations("users/user/test", set(["my_empty", "Empty"]))

        # Check that an instantiate of "A" fails
        try:
            res = instantiate("users/user/test/my_empty", "A")
            assert False
        except UnknownIdentifier:
            assert verify("users/user/test/Empty", "formalisms/SimpleClassDiagrams") == "OK"

        # Create something in the formalism
        instantiate("users/user/test/Empty", "Class", ID="A")
        attr_assign("users/user/test/Empty", "A", "name", "A")
        assert verify("users/user/test/Empty", "formalisms/SimpleClassDiagrams") == "OK"

        # Now instantiate that in the model as well, which now works
        instantiate("users/user/test/my_empty", "A")
        assert verify("users/user/test/my_empty", "users/user/test/Empty") == "OK"

    def test_overwrite(self):
        model_add("users/user/test/Empty", "formalisms/SimpleClassDiagrams")
        assert element_list("users/user/test/Empty") == set([])

        instantiate("users/user/test/Empty", "Class", ID="A")
        assert element_list("users/user/test/Empty") == set([("A", "Class")])

        model_overwrite("users/user/test/Empty", "")
        assert element_list("users/user/test/Empty") == set([])
        compare_locations("users/user/test", set(["Empty"]))
        
        assert element_list("users/user/test/Empty") == set([])

        instantiate("users/user/test/Empty", "Class", ID="B")
        compare_locations("users/user/test", set(["Empty"]))

    def test_operations(self):
        log = []

        model_add("users/user/test/PetriNet", "formalisms/SimpleClassDiagrams", open("integration/code/pn_design.mvc", "r").read())
        model_add("users/user/test/PetriNet_Runtime", "formalisms/SimpleClassDiagrams", open("integration/code/pn_runtime.mvc", "r").read())
        model_add("users/user/test/my_pn", "users/user/test/PetriNet", open("integration/code/pn_design_model.mvc", "r").read())

        transformation_add_MT({"PetriNet": "users/user/test/PetriNet"}, {}, "users/user/test/print_pn", open("integration/code/pn_print.mvc").read())
        transformation_add_MANUAL({"PetriNet": "users/user/test/PetriNet"}, {"PetriNet_Runtime": "users/user/test/PetriNet_Runtime"}, "users/user/test/pn_design_to_runtime", ops_add_tracability_D2R)
        transformation_add_AL({"PetriNet_Runtime": "users/user/test/PetriNet_Runtime"}, {"PetriNet_Runtime": "users/user/test/PetriNet_Runtime"}, "users/user/test/pn_simulate", open("integration/code/pn_simulate.alc").read())
        transformation_add_MT({"PetriNet_Runtime": "users/user/test/PetriNet_Runtime"}, {"PetriNet": "users/user/test/PetriNet"}, "users/user/test/pn_runtime_to_design", open("integration/code/pn_runtime_to_design.mvc").read(), ops_add_tracability_R2D)

        log = []
        ctrl = log_output.Controller(log, keep_running=False)
        thrd = threading.Thread(target=ctrl.start)
        thrd.daemon = True
        thrd.start()

        assert transformation_execute_MT("users/user/test/print_pn", {"PetriNet": "users/user/test/my_pn"}, {}, (ctrl, "inp", "outp")) == True
        thrd.join()
        assert set(log) == set(['"p1" --> 1',
                                '"p2" --> 2',
                                '"p3" --> 3'])

        assert transformation_execute_MANUAL("users/user/test/pn_design_to_runtime", {"PetriNet": "users/user/test/my_pn"}, {"PetriNet_Runtime": "users/user/test/my_pn_RT"}, ops_manual_callback) == True
        assert transformation_execute_AL("users/user/test/pn_simulate", {"PetriNet_Runtime": "users/user/test/my_pn_RT"}, {"PetriNet_Runtime": "users/user/test/my_pn_RT"}) == True
        assert transformation_execute_MT("users/user/test/pn_runtime_to_design", {"PetriNet_Runtime": "users/user/test/my_pn_RT"}, {"PetriNet": "users/user/test/my_pn"}) == True

        log = []
        ctrl = log_output.Controller(log, keep_running=False)
        thrd = threading.Thread(target=ctrl.start)
        thrd.daemon = True
        thrd.start()

        assert transformation_execute_MT("users/user/test/print_pn", {"PetriNet": "users/user/test/my_pn"}, {}, (ctrl, "inp", "outp")) == True
        thrd.join()
        assert set(log) == set(['"p1" --> 0',
                                '"p2" --> 1',
                                '"p3" --> 5'])

    def test_process_model_trivial_pn_subfunction(self):
        model_add("users/user/test/PetriNet", "formalisms/SimpleClassDiagrams", open("integration/code/pn_design.mvc", "r").read())
        model_add("users/user/test/ReachabilityGraph", "formalisms/SimpleClassDiagrams", open("integration/code/reachability_graph.mvc", "r").read())
        model_add("users/user/test/pn_reachability", "formalisms/ProcessModel", open("integration/code/pm_pn_reachability.mvc", "r").read())
        transformation_add_MT({}, {"PetriNet": "users/user/test/PetriNet"}, "users/user/test/initialize_PN", open("integration/code/initialize_PN.mvc", "r").read())
        transformation_add_MANUAL({"PetriNet": "users/user/test/PetriNet"}, {"PetriNet": "users/user/test/PetriNet"}, "users/user/test/refine_PN")
        transformation_add_AL({"PetriNet": "users/user/test/PetriNet"}, {"ReachabilityGraph": "users/user/test/ReachabilityGraph"}, "users/user/test/reachability", open("integration/code/reachability_subfunction.alc", "r").read())
        transformation_add_MT({"ReachabilityGraph": "users/user/test/ReachabilityGraph"}, {}, "users/user/test/reachability_print", open("integration/code/reachabilitygraph_print.mvc", 'r').read())


        log = []
        ctrl = log_output.Controller(log, keep_running=False)
        thrd = threading.Thread(target=ctrl.start)
        thrd.daemon = True
        thrd.start()

        process_execute("users/user/test/pn_reachability", {}, {"users/user/test/refine_PN": pn_subfunc_callback_refine_PN, "users/user/test/reachability_print": (ctrl, "inp", "outp")})
        thrd.join()

        assert set(log) == set(['"0": {"p1": 1}',
                                '"1": {"p1": 0}',
                                '"0" --["t1"]--> "1"'])

    def test_render(self):
        model_add("users/user/test/CausalBlockDiagrams", "formalisms/SimpleClassDiagrams", open("integration/code/cbd_design.mvc", 'r').read())
        model_add("users/user/test/MM_rendered_graphical", "formalisms/SimpleClassDiagrams", open("integration/code/MM_rendered_graphical.mvc", 'r').read())
        model_add("users/user/test/my_CBD", "users/user/test/CausalBlockDiagrams", open("integration/code/my_cbd.mvc", 'r').read())

        transformation_add_MT({"abstract": "users/user/test/CausalBlockDiagrams", "rendered": "users/user/test/MM_rendered_graphical"}, {"abstract": "users/user/test/CausalBlockDiagrams", "rendered": "users/user/test/MM_rendered_graphical"}, "users/user/test/render_graphical_CBD", open("integration/code/CBD_mapper.mvc", 'r').read(), render_add_tracability)
        result = model_render("users/user/test/my_CBD", "users/user/test/render_graphical_CBD", "users/user/test/my_perceptualized_CBD")

        assert len(result) == 23

    def test_SCCD_basic(self):
        model_add("users/user/test/SCCD", "formalisms/SimpleClassDiagrams", open("integration/code/SCCD.mvc", 'r').read())
        model_add("users/user/test/SCCD_Trace", "formalisms/SimpleClassDiagrams", open("integration/code/SCCD_Trace.mvc", 'r').read())

        model_add("users/user/test/my_SCCD", "users/user/test/SCCD", open("integration/code/SCCD_all.mvc", 'r').read())

        transformation_add_AL({"SCCD": "users/user/test/SCCD"}, {"trace": "users/user/test/SCCD_Trace"}, "users/user/test/SCCD_execute_afap", open("integration/code/SCCD_execute.alc", 'r').read().replace("afap = False", "afap = True"))
        transformation_execute_AL("users/user/test/SCCD_execute_afap", {"SCCD": "users/user/test/my_SCCD"}, {"trace": "users/user/test/my_SCCD_trace"})

        alter_context("users/user/test/my_SCCD_trace", "users/user/test/SCCD_Trace")
        lst = element_list_nice("users/user/test/my_SCCD_trace")

        lst.sort(key=lambda i: (i["timestamp"], i["name"]))
        result = [(i["timestamp"], str(i["name"])) for i in lst if i["name"] not in ["updateTimerValue", "updateTimerColour", "resetTimer"]]

        assert result == [(5.0, "displayRed"),
                          (20.0, "displayYellow"),
                          (20.5, "displayNone"),
                          (21.0, "displayYellow"),
                          (21.5, "displayNone"),
                          (22.0, "displayYellow"),
                          (22.5, "displayNone"),
                          (23.0, "displayYellow"),
                          (23.5, "displayNone"),
                          (24.0, "displayYellow"),
                          (24.5, "displayNone"),
                          (25.0, "displayYellow"),
                          (25.5, "displayNone"),
                          (26.0, "displayYellow"),
                          (26.5, "displayNone"),
                          (27.0, "displayYellow"),
                          (27.5, "displayNone"),
                          (28.0, "displayYellow"),
                          (28.5, "displayNone"),
                          (29.0, "displayYellow"),
                          (29.4, "displayNone"),
                          (29.4, "displayRed"),
                          (89.4, "displayGreen"),
                          (129.4, "displayNone"),
                          (129.4, "displayRed"),
                          (139.4, "displayYellow"),
                          (139.9, "displayNone"),
                          (140.4, "displayYellow"),
                          (140.9, "displayNone"),
                          (141.4, "displayYellow"),
                          (141.9, "displayNone"),
                          (142.4, "displayYellow"),
                          (142.9, "displayNone"),
                          (143.4, "displayYellow"),
                          (143.9, "displayNone"),
                          (144.4, "displayYellow"),
                          (144.9, "displayNone"),
                          (145.4, "displayYellow"),
                          (145.9, "displayNone"),
                          (146.4, "displayYellow"),
                          (146.9, "displayNone"),
                          (147.4, "displayYellow"),
                          (147.9, "displayNone"),
                          (148.4, "displayYellow"),
                         ]

    def test_userhome_expand(self):
        assert "my_model" not in model_list("~/test")
        model_add("~/test/my_model", "formalisms/SimpleClassDiagrams")
        assert "my_model" in model_list("~/test")
        assert model_list("~/test") == model_list("users/user/test")

    def test_switch_MM(self):
        model_add("users/user/test/PetriNet", "formalisms/SimpleClassDiagrams", open("integration/code/pn_design.mvc", "r").read())
        model_add("users/user/test/my_pn", "users/user/test/PetriNet", open("integration/code/pn_design_model.mvc", "r").read())

        got = element_list_nice("users/user/test/PetriNet")
        expected = \
            [{'__id': 'Natural', '__type': 'SimpleAttribute', 'constraint': {'AL': ''}, 'name': 'Natural'},
             {'__id': 'String', '__type': 'SimpleAttribute', 'constraint': {'AL': ''}, 'name': 'String'},
             {'__id': 'Place', '__type': 'Class', 'lower_cardinality': None, 'upper_cardinality': None, 'constraint': {'AL': ''}, 'name': 'Place', 'abstract': None},
             {'__id': 'Place_tokens', '__type': 'AttributeLink', '__source': 'Place', '__target': 'Natural', 'name': 'tokens', 'optional': False, 'constraint': {'AL': ''}},
             {'__id': 'Place_name', '__type': 'AttributeLink', '__source': 'Place', '__target': 'String', 'name': 'name', 'optional': False, 'constraint': {'AL': ''}},
             {'__id': 'Transition', '__type': 'Class', 'lower_cardinality': None, 'upper_cardinality': None, 'constraint': {'AL': ''}, 'name': 'Transition', 'abstract': None},
             {'__id': 'Transition_name', '__type': 'AttributeLink', '__source': 'Transition', '__target': 'String', 'name': 'name', 'optional': False, 'constraint': {'AL': ''}},
             {'__id': 'P2T', '__type': 'Association', '__source': 'Place', '__target': 'Transition', 'source_lower_cardinality': None, 'target_lower_cardinality': None, 'source_upper_cardinality': None, 'target_upper_cardinality': None, 'constraint': {'AL': ''}, 'name': 'P2T', "abstract": None, "lower_cardinality": None, "upper_cardinality": None},
             {'__id': 'P2T_weight', '__type': 'AttributeLink', '__source': 'P2T', '__target': 'Natural', 'name': 'weight', 'optional': False, 'constraint': {'AL': ''}},
             {'__id': 'T2P', '__type': 'Association', '__source': 'Transition', '__target': 'Place', 'source_lower_cardinality': None, 'target_lower_cardinality': None, 'source_upper_cardinality': None, 'target_upper_cardinality': None, 'constraint': {'AL': ''}, 'name': 'T2P', "abstract": None, "lower_cardinality": None, "upper_cardinality": None},
             {'__id': 'T2P_weight', '__type': 'AttributeLink', '__source': 'T2P', '__target': 'Natural', 'name': 'weight', 'optional': False, 'constraint': {'AL': ''}},
            ]
        compare_unordered_lists(got, expected)

        got = element_list_nice("users/user/test/my_pn")
        expected = \
            [{'__id': 'p1', '__type': 'Place', 'tokens': 1, 'name': 'p1'},
             {'__id': 'p2', '__type': 'Place', 'tokens': 2, 'name': 'p2'},
             {'__id': 'p3', '__type': 'Place', 'tokens': 3, 'name': 'p3'},
             {'__id': 't1', '__type': 'Transition', 'name': 't1'},
             {'__id': '__0', '__type': 'P2T', '__source': 'p1', '__target': 't1', 'weight': 1},
             {'__id': '__1', '__type': 'P2T', '__source': 'p2', '__target': 't1', 'weight': 1},
             {'__id': '__2', '__type': 'T2P', '__source': 't1', '__target': 'p3', 'weight': 2}
            ]
        compare_unordered_lists(got, expected)

        alter_context("users/user/test/PetriNet", "formalisms/Bottom")
        alter_context("users/user/test/my_pn", "formalisms/Bottom")

        count_nodes = 0
        count_edges = 0
        for entry in element_list_nice("users/user/test/PetriNet"):
            print(entry)
            assert entry["__type"] in ["Node", "Edge"]
            if entry["__type"] == "Node":
                assert len(entry) == 2
                count_nodes += 1
            else:
                assert len(entry) == 4
                count_edges += 1
        assert count_nodes == 20
        assert count_edges == 23

        count_nodes = 0
        count_edges = 0
        for entry in element_list_nice("users/user/test/my_pn"):
            assert entry["__type"] in ["Node", "Edge"]
            if entry["__type"] == "Node":
                assert len(entry) == 2
                count_nodes += 1
            else:
                assert len(entry) == 4
                count_edges += 1
        assert count_nodes == 14
        assert count_edges == 13

        alter_context("users/user/test/PetriNet", "users/user/test/PetriNet")
        alter_context("users/user/test/my_pn", "formalisms/SimpleClassDiagrams")
        try:
            element_list_nice("users/user/test/PetriNet")
            self.fail()
        except:
            pass
        try:
            element_list_nice("users/user/test/my_pn")
            self.fail()
        except:
            pass
