UpdateNode { private String checkInstance(UserId userId, Node node){ if (tenantManage.getNode(userId)!=null){ if (tenantManage.getNode(userId).containsKey(node.getNodeId())){ Node nodeExist = tenantManage.getNode(userId).get(node.getNodeId()); if (!nodeExist.getNodeName().equals(node.getNodeName())){ return "The node name should not be changed."; } if (!nodeExist.getNodeType().equals(node.getNodeType())){ return "The node type should not be changed."; } } } if (tenantManage.getNodeDataStore(userId)!=null){ if (tenantManage.getNodeDataStore(userId).containsKey(node.getNodeId())){ Node nodeExist = tenantManage.getNodeDataStore(userId).get(node.getNodeId()); if (!nodeExist.getNodeName().equals(node.getNodeName())){ return "The node name should not be changed."; } if (!nodeExist.getNodeType().equals(node.getNodeType())){ return "The node type should not be changed."; } } } if (node.getSubNode()!=null){ List<SubNode> subNodeList = node.getSubNode(); Boolean subNodeExist = false; for (SubNode subNode : subNodeList){ if (tenantManage.getNode(userId)!=null){ if (tenantManage.getNode(userId).containsKey(subNode.getNodeId())){ subNodeExist = true; } } if (tenantManage.getNodeDataStore(userId)!=null){ if (tenantManage.getNodeDataStore(userId)!=null){ if (tenantManage.getNodeDataStore(userId).containsKey(subNode.getNodeId())){ subNodeExist = true; } } } if (!subNodeExist){ return "The sub-node " + subNode.getNodeId().getValue() + " is not exist."; } } } return null; } UpdateNode(DataBroker dataBroker,TenantManage tenantManage); String NodeHandling(UserId userId,Node node);  }
@Test public void testcheckInstance() throws Exception { Class<UpdateNode> class1 = UpdateNode.class; Method method = class1.getDeclaredMethod("checkInstance", new Class[]{UserId.class, Node.class}); method.setAccessible(true); Class<TenantManage> class2 = TenantManage.class; Field field = class1.getDeclaredField("tenantManage"); field.setAccessible(true); Field field1 = class2.getDeclaredField("userNodeMap"); field1.setAccessible(true); Map<UserId, Map<NodeId, Node>> userNodeMap = new HashMap<UserId, Map<NodeId, Node>>(); Map<UserId, Map<NodeId, Node>> userNodeMap2 = new HashMap<UserId, Map<NodeId, Node>>(); Map<NodeId, Node> map1 = new HashMap<NodeId, Node>(); map1.put(nodeId, node2); userNodeMap.put(userId, map1); field1.set(field.get(updateNode), userNodeMap); when(tenantManage.getNode(userId)).thenReturn(map1); when(node.getNodeId()).thenReturn(nodeId); when(node2.getNodeName()).thenReturn(nodeName); when(node.getNodeName()).thenReturn(nodeName2); Assert.assertEquals(method.invoke(updateNode, userId, node), "The node name should not be changed."); when(node2.getNodeName()).thenReturn(nodeName); when(node.getNodeName()).thenReturn(nodeName); when(node2.getNodeType()).thenReturn(nodeType2); when(node.getNodeType()).thenReturn(nodeType); Assert.assertEquals(method.invoke(updateNode, userId, node), "The node type should not be changed."); field1.set(field.get(updateNode), userNodeMap2); Field field2 = class2.getDeclaredField("user"); field2.setAccessible(true); field2.set(field.get(updateNode), user); when(user.getObjects()).thenReturn(objects); when(user.getObjects().getNode()).thenReturn(nodeList); when(node2.getNodeId()).thenReturn(nodeId); when(node.getNodeId()).thenReturn(nodeId); when(node2.getNodeName()).thenReturn(nodeName2); when(node.getNodeName()).thenReturn(nodeName); Assert.assertEquals(method.invoke(updateNode, userId, node), "The node name should not be changed."); when(node2.getNodeName()).thenReturn(nodeName); when(node.getNodeName()).thenReturn(nodeName); when(node2.getNodeType()).thenReturn(nodeType2); when(node.getNodeType()).thenReturn(nodeType); Assert.assertEquals(method.invoke(updateNode, userId, node), "The node type should not be changed."); }