def create_template(self): net_attr1 = self.create_attribute("net_attr_a", dimension='Volume') net_attr2 = self.create_attribute("net_attr_c", dimension=None) link_attr_1 = self.create_attribute("link_attr_a", dimension='Pressure') link_attr_2 = self.create_attribute("link_attr_b", dimension='Speed') link_attr_3 = self.create_attribute("link_attr_c", dimension='Length') node_attr_1 = self.create_attribute("node_attr_a", dimension='Volume') node_attr_2 = self.create_attribute("node_attr_b", dimension='Speed') node_attr_3 = self.create_attribute("node_attr_c", dimension='Monetary value') node_attr_4 = self.create_attribute("node_attr_d", dimension='Volumetric flow rate') group_attr_1 = self.create_attribute("grp_attr_1", dimension='Monetary value') group_attr_2 = self.create_attribute("grp_attr_2", dimension=None) template = JSONObject() template['name'] = 'Default Template ' + str(datetime.datetime.now()) types = [] #********************** #network type # #********************** net_type = JSONObject() net_type.name = "Default Network" net_type.alias = "Test type alias" net_type.resource_type = 'NETWORK' typeattrs = [] typeattr_1 = JSONObject() typeattr_1.attr_id = net_attr1.id typeattr_1.data_restriction = {'LESSTHAN': 10, 'NUMPLACES': 1} typeattr_1.unit_id = self.get_unit('m^3').id typeattrs.append(typeattr_1) typeattr_2 = JSONObject() typeattr_2.attr_id = net_attr2.id typeattrs.append(typeattr_2) net_type.typeattrs = typeattrs types.append(net_type) #********************** # node type # #********************** node_type = JSONObject() node_type.name = "Default Node" node_type.alias = "Test type alias" node_type.resource_type = 'NODE' typeattrs = [] typeattr_1 = JSONObject() typeattr_1.attr_id = node_attr_1.id typeattr_1.data_restriction = {'LESSTHAN': 10, 'NUMPLACES': 1} typeattr_1.unit_id = self.get_unit('m^3').id typeattrs.append(typeattr_1) typeattr_2 = JSONObject() typeattr_2.attr_id = node_attr_2.id typeattr_2.data_restriction = {'INCREASING': None} typeattrs.append(typeattr_2) typeattr_3 = JSONObject() typeattr_3.attr_id = node_attr_3.id typeattrs.append(typeattr_3) typeattr_4 = JSONObject() typeattr_4.attr_id = node_attr_4.id typeattr_4.unit_id = self.get_unit("m^3 s^-1").id typeattrs.append(typeattr_4) node_type.typeattrs = typeattrs types.append(node_type) #********************** #link type # #********************** link_type = JSONObject() link_type.name = "Default Link" link_type.resource_type = 'LINK' typeattrs = [] typeattr_1 = JSONObject() typeattr_1.attr_id = link_attr_1.id typeattrs.append(typeattr_1) typeattr_2 = JSONObject() typeattr_2.attr_id = link_attr_2.id typeattrs.append(typeattr_2) typeattr_3 = JSONObject() typeattr_3.attr_id = link_attr_3.id typeattrs.append(typeattr_3) link_type.typeattrs = typeattrs types.append(link_type) #********************** #group type # #********************** group_type = JSONObject() group_type.name = "Default Group" group_type.resource_type = 'GROUP' typeattrs = [] typeattr_1 = JSONObject() typeattr_1.attr_id = group_attr_1.id typeattrs.append(typeattr_1) typeattr_2 = JSONObject() typeattr_2.attr_id = group_attr_2.id typeattrs.append(typeattr_2) group_type.typeattrs = typeattrs types.append(group_type) template.templatetypes = types new_template_i = self.client.add_template(template) new_template = JSONObject(new_template_i) assert new_template.name == template.name, "Names are not the same!" assert new_template.id is not None, "New Template has no ID!" assert new_template.id > 0, "New Template has incorrect ID!" assert len(new_template.templatetypes) == len(types), "Resource types did not add correctly" for t in new_template.templatetypes[1].typeattrs: assert t.attr_id in (node_attr_1.id, node_attr_2.id, node_attr_3.id, node_attr_4.id),\ "Node types were not added correctly!" for t in new_template.templatetypes[2].typeattrs: assert t.attr_id in (link_attr_1.id, link_attr_2.id, link_attr_3.id),\ "Link types were not added correctly!" return new_template
def mock_template(client): link_attr_1 = client.testutils.create_attribute("link_attr_1", dimension='Pressure') link_attr_2 = client.testutils.create_attribute("link_attr_2", dimension='Speed') node_attr_1 = client.testutils.create_attribute("node_attr_1", dimension='Volume') node_attr_2 = client.testutils.create_attribute("node_attr_2", dimension='Speed') net_attr_1 = client.testutils.create_attribute("net_attr_2", dimension='Speed') template = JSONObject() template.name = 'Test template @ %s' % datetime.datetime.now() layout = {} layout['groups'] = '<groups>...</groups>' template.layout = layout template.templatetypes = [] # ********************** # type 1 # # ********************** type1 = JSONObject() type1.name = "Node type" type1.alias = "Node type alias" type1.resource_type = 'NODE' type1.typeattrs = [] tattr_1 = JSONObject() tattr_1.attr_id = node_attr_1.id tattr_1.description = "Type attribute 1 description" tattr_1.properties = {'test_property': "test property add type"} tattr_1.data_restriction = {'LESSTHAN': 10, 'NUMPLACES': 1} type1.typeattrs.append(tattr_1) tattr_2 = JSONObject() tattr_2.attr_id = node_attr_2.id tattr_2.description = "Type attribute 2 description" tattr_2.data_restriction = {'INCREASING': None} type1.typeattrs.append(tattr_2) template.templatetypes.append(type1) # ********************** # type 2 # # ********************** type2 = JSONObject() type2.name = "Link type" type2.alias = "Link type alias" type2.resource_type = 'LINK' type2.typeattrs = [] tattr_1 = JSONObject() tattr_1.attr_id = link_attr_1.id type2.typeattrs.append(tattr_1) tattr_2 = JSONObject() tattr_2.attr_id = link_attr_2.id type2.typeattrs.append(tattr_2) template.templatetypes.append(type2) # ********************** # type 3 # # ********************** type3 = JSONObject() type3.name = "Network Type" type3.alias = "Network Type alias" type3.resource_type = 'NETWORK' type3.typeattrs = [] tattr_3 = JSONObject() tattr_3.attr_id = net_attr_1.id tattr_3.data_restriction = {} type3.typeattrs.append(tattr_3) template.templatetypes.append(type3) new_template_i = client.add_template(template) # TODO: HACK to load the attr for tt in new_template_i.templatetypes: for ta in tt.typeattrs: ta.attr new_template_j = JSONObject(new_template_i) return new_template_j
def test_add_template(self): link_attr_1 = self.create_attr("link_attr_1", dimension='Pressure') link_attr_2 = self.create_attr("link_attr_2", dimension='Speed') node_attr_1 = self.create_attr("node_attr_1", dimension='Volume') node_attr_2 = self.create_attr("node_attr_2", dimension='Speed') net_attr_1 = self.create_attr("net_attr_2", dimension='Speed') template = JSONObject() template.name = 'Test template @ %s' % datetime.datetime.now() layout = {} layout['groups'] = '<groups>...</groups>' template.layout = layout template.templatetypes = [] #********************** #type 1 # #********************** type1 = JSONObject() type1.name = "Node type" type1.alias = "Node type alias" type1.resource_type = 'NODE' type1.typeattrs = [] tattr_1 = JSONObject() tattr_1.attr_id = node_attr_1.id tattr_1.description = "Type attribute 1 description" tattr_1.properties = {'test_property': "test property add type"} tattr_1.data_restriction = {'LESSTHAN': 10, 'NUMPLACES': 1} type1.typeattrs.append(tattr_1) tattr_2 = JSONObject() tattr_2.attr_id = node_attr_2.id tattr_2.description = "Type attribute 2 description" tattr_2.data_restriction = {'INCREASING': None} type1.typeattrs.append(tattr_2) template.templatetypes.append(type1) #********************** #type 2 # #********************** type2 = JSONObject() type2.name = "Link type" type2.alias = "Link type alias" type2.resource_type = 'LINK' type2.typeattrs = [] tattr_1 = JSONObject() tattr_1.attr_id = link_attr_1.id type2.typeattrs.append(tattr_1) tattr_2 = JSONObject() tattr_2.attr_id = link_attr_2.id type2.typeattrs.append(tattr_2) template.templatetypes.append(type2) #********************** #type 3 # #********************** type3 = JSONObject() type3.name = "Network Type" type3.alias = "Network Type alias" type3.resource_type = 'NETWORK' type3.typeattrs = [] tattr_3 = JSONObject() tattr_3.attr_id = net_attr_1.id tattr_3.data_restriction = {} type3.typeattrs.append(tattr_3) template.templatetypes.append(type3) new_template_i = add_template(template) #TODO: HACK to load the attr for tt in new_template_i.templatetypes: for ta in tt.typeattrs: ta.attr new_template_j = JSONObject(new_template_i) assert new_template_j.name == template.name, "Names are not the same!" assert str(new_template_j.layout) == str( template.layout), "Layouts are not the same!" assert new_template_j.id is not None, "New Template has no ID!" assert new_template_j.id > 0, "New Template has incorrect ID!" assert len(new_template_j.templatetypes ) == 3, "Resource types did not add correctly" for t in new_template_j.templatetypes[0].typeattrs: assert t.attr_id in (node_attr_1.id, node_attr_2.id) "Node types were not added correctly!" for t in new_template_j.templatetypes[1].typeattrs: assert t.attr_id in (link_attr_1.id, link_attr_2.id) "Node types were not added correctly!" return new_template_j