UpdateNode { private String checkDefinition(Node node){ String errorInfo = null; Map<NodeType, NodeDefinition> map = getDefinitions.getNodeDefinition(); if (map.isEmpty()){ return "This type of Node has not been defined."; } else { if (map.containsKey(node.getNodeType())){ List<Property> nodeProperties = node.getProperty(); List<PropertyDefinition> nodePropertyDefinitions = map.get(node.getNodeType()).getPropertyDefinition(); if (nodeProperties != null && nodePropertyDefinitions == null){ errorInfo = "This type of node has no properties."; } else if (nodeProperties != null && nodePropertyDefinitions != null){ errorInfo = checkProperty(nodeProperties, nodePropertyDefinitions); if (errorInfo != null){ } else { errorInfo = checkPredefine(nodeProperties); } } } else { return "This type of Node has not been defined."; } } return errorInfo; } UpdateNode(DataBroker dataBroker,TenantManage tenantManage); String NodeHandling(UserId userId,Node node);  }
@Test public void testcheckDefinition() throws Exception { Class<UpdateNode> class3 = UpdateNode.class; Method method = class3.getDeclaredMethod("checkDefinition", new Class[]{Node.class}); method.setAccessible(true); CheckedFuture nodedefinitionFuture = mock(CheckedFuture.class); ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class); when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction); when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(nodedefinitionFuture); List<NodeDefinition> nodeDefinitionList = new ArrayList<NodeDefinition>(); List<NodeDefinition> nodeDefinitionListnull = new ArrayList<NodeDefinition>(); NodeDefinition nodeDefinition = mock(NodeDefinition.class); nodeDefinitionList.add(nodeDefinition); when(nodeDefinition.getNodeType()).thenReturn(nodeType); Class<UpdateNode> class1 = UpdateNode.class; Class<GetDefinitions> class2 = GetDefinitions.class; Field field = class1.getDeclaredField("getDefinitions"); field.setAccessible(true); Field field1 = class2.getDeclaredField("nodeDefinitionList"); field1.setAccessible(true); System.out.println(field1.get(field.get(updateNode))); field1.set(field.get(updateNode), nodeDefinitionList); System.out.println(field1.get(field.get(updateNode))); Assert.assertTrue((List<NodeDefinition>) field1.get(field.get(updateNode)) == nodeDefinitionList); when(node.getNodeType()).thenReturn(nodeType); when(node.getProperty()).thenReturn(propertyList); when(nodeDefinition.getPropertyDefinition()).thenReturn(propertyDefinitions); when(property.getPropertyName()).thenReturn(propertyName); when(propertyDefinition.getPropertyName()).thenReturn(propertyName2); when(property.getPropertyName().getValue()).thenReturn(new String("propertyname")); Assert.assertEquals(method.invoke(updateNode, node), "The property propertyname has not been defined."); field1.set(field.get(updateNode), nodeDefinitionList); when(node.getNodeType()).thenReturn(nodeType); List<Property> propertyListnull = new ArrayList<Property>(); List<PropertyDefinition> propertyDefinitionListnull = new ArrayList<PropertyDefinition>(); when(node.getProperty()).thenReturn(propertyListnull); when(nodeDefinition.getPropertyDefinition()).thenReturn(propertyDefinitionListnull); Assert.assertEquals(method.invoke(updateNode, node), null); field1.set(field.get(updateNode), nodeDefinitionListnull); Assert.assertEquals(method.invoke(updateNode, node), "This type of Node has not been defined."); field1.set(field.get(updateNode), nodeDefinitionList); when(node.getNodeType()).thenReturn(nodeType2); Assert.assertEquals(method.invoke(updateNode, node), "This type of Node has not been defined."); }