UpdateTemplateDefinition { private String checkNodeTemplate(AbstractNode node, Map<ParameterName, TemplateParameter.ParameterValueType> parameterValueTypeMap){ String errorInfo = null; Map<NodeType, NodeDefinition> nodeDefinitions = getDefinitions.getNodeDefinition(); if (nodeDefinitions.containsKey(node.getNodeType())){ NodeDefinition nodeDefinition = nodeDefinitions.get(node.getNodeType()); List<PropertyDefinition> propertyDefinitions = nodeDefinition.getPropertyDefinition(); Map<PropertyName, PropertyDefinition> nodePropertyDefinition = new HashMap<PropertyName, PropertyDefinition>(); if (propertyDefinitions!=null){ for (PropertyDefinition propertyDefinition : propertyDefinitions){ nodePropertyDefinition.put(propertyDefinition.getPropertyName(),propertyDefinition); } } if (node.getProperty()!=null){ for (Property property : node.getProperty()){ if (nodePropertyDefinition.containsKey(property.getPropertyName())){ PropertyDefinition.PropertyValueType type = nodePropertyDefinition.get(property.getPropertyName()).getPropertyValueType(); if (type.getIntValue()==0){ List<StringValue> stringValues = property.getPropertyValues().getStringValue(); for (StringValue stringValue : stringValues){ TemplateParameter.ParameterValueType valueType = parameterValueTypeMap.get(new ParameterName(stringValue.getValue())); if (valueType!=null){ if (type.getIntValue()!= valueType.getIntValue()){ return "The property " + property.getPropertyName().getValue() + " type is not right."; } } } } } else { errorInfo = "The property name " + property.getPropertyName().getValue() + " is not defined."; return errorInfo; } } } } else { errorInfo = "The node type " + node.getNodeType().getValue() + " is not defined."; return errorInfo; } return errorInfo; } UpdateTemplateDefinition(DataBroker dataBroker, TenantManage tenantManage); String checkTemplateDefinition(UserId userId, TemplateDefinition templateDefinition);  }
@Test public void testCheckNodeTemplate() throws Exception{ method = class1.getDeclaredMethod("checkNodeTemplate",new Class[]{ AbstractNode.class, Map.class }); method.setAccessible(true); field = class1.getDeclaredField("getDefinitions"); field.setAccessible(true); GetDefinitions getDefinitions = mock(GetDefinitions.class); AbstractNode node = mock(AbstractNode.class); NodeType nodeType = mock(NodeType.class); NodeDefinition nodeDefinition = mock(NodeDefinition.class); PropertyName propertyName = mock(PropertyName.class); Property property = mock(Property.class); StringValue stringValue = mock(StringValue.class); PropertyValues propertyValues = mock(PropertyValues.class); PropertyDefinition.PropertyValueType type = PropertyDefinition.PropertyValueType.String; PropertyDefinition propertyDefinition = mock(PropertyDefinition.class); TemplateParameter.ParameterValueType valueType = TemplateParameter.ParameterValueType.Int; List<Property> properties = new ArrayList<Property>(); List<StringValue> stringValues = new ArrayList<StringValue>(); List<PropertyDefinition> propertyDefinitions = new ArrayList<PropertyDefinition>(); Map<ParameterName, TemplateParameter.ParameterValueType> parameterValueTypeMap = new HashMap<ParameterName, TemplateParameter.ParameterValueType>(); Map<NodeType, NodeDefinition> nodeDefinitions = new HashMap<NodeType, NodeDefinition>(); parameterValueTypeMap.put(new ParameterName("ParameterName"),valueType); field.set(updateTemplateDefinition, getDefinitions); when(getDefinitions.getNodeDefinition()).thenReturn(nodeDefinitions); when(node.getNodeType()).thenReturn(nodeType); when(nodeType.getValue()).thenReturn("test"); Assert.assertTrue(method.invoke(updateTemplateDefinition,node,parameterValueTypeMap).equals("The node type " + node.getNodeType().getValue() + " is not defined.")); verify(node,times(3)).getNodeType(); nodeDefinitions.put(nodeType, nodeDefinition); propertyDefinitions.add(propertyDefinition); properties.add(property); stringValues.add(stringValue); Assert.assertTrue(nodeDefinitions.get(nodeType) == nodeDefinition); when(nodeDefinition.getPropertyDefinition()).thenReturn(propertyDefinitions); when(propertyDefinition.getPropertyName()).thenReturn(propertyName); when(propertyName.getValue()).thenReturn("test"); when(node.getProperty()) .thenReturn(null) .thenReturn(properties); Assert.assertTrue(method.invoke(updateTemplateDefinition, node, parameterValueTypeMap) == null); when(property.getPropertyName()) .thenReturn(mock(PropertyName.class)) .thenReturn(propertyName); Assert.assertTrue(method.invoke(updateTemplateDefinition, node, parameterValueTypeMap).equals("The property name " + property.getPropertyName().getValue() + " is not defined.")); when(propertyDefinition.getPropertyValueType()) .thenReturn(PropertyDefinition.PropertyValueType.Int) .thenReturn(type); Assert.assertTrue(method.invoke(updateTemplateDefinition, node, parameterValueTypeMap) == null); when(property.getPropertyValues()).thenReturn(propertyValues); when(propertyValues.getStringValue()).thenReturn(stringValues); when(stringValue.getValue()).thenReturn(new String("ParameterName")); Assert.assertTrue(method.invoke(updateTemplateDefinition, node, parameterValueTypeMap).equals("The property " + property.getPropertyName().getValue() + " type is not right.")); }