UpdateFlow { private String checkDefinition(Flow flow){ String errorInfo = null; Map<MatchItemName, MatchItemDefinition> matchItemDefinitionMap =getDefinitions.getMatchItemDefinition(); if (flow.getMatchItem() != null) { if (matchItemDefinitionMap.isEmpty()){ return "The match item has not been defined."; } else { for (MatchItem matchItem : flow.getMatchItem()) { if (matchItemDefinitionMap.containsKey(matchItem.getMatchItemName())){ MatchItemValue matchItemValue = matchItem.getMatchItemValue(); MatchItemDefinition.MatchItemValueType matchItemValueType = matchItemDefinitionMap.get(matchItem.getMatchItemName()).getMatchItemValueType(); if (matchItemValue != null && matchItemValueType != null) { if (matchItemValueType.getIntValue()==0 && !(matchItemValue.getIntValue()==null&&matchItemValue.getStringValue()!=null&&matchItemValue.getRangeValue()==null)) { errorInfo = "The match item value type for" +matchItem.getMatchItemName().toString()+"should be string."; break; } if (matchItemValueType.getIntValue()==1 && !(matchItemValue.getIntValue()!=null&&matchItemValue.getStringValue()==null&&matchItemValue.getRangeValue()==null)) { errorInfo = "The match item value type for" + matchItem.getMatchItemName().toString()+"should be integer."; break; } if (matchItemValueType.getIntValue()==2 && !(matchItemValue.getIntValue()==null&&matchItemValue.getStringValue()==null&&matchItemValue.getRangeValue()!=null)) { errorInfo = "The match item value type for" + matchItem.getMatchItemName().toString()+"should be range."; break; } } } else { return "The match item has not been defined."; } } } if (errorInfo==null){ errorInfo = checkPredefine(flow.getMatchItem()); } } return errorInfo; } UpdateFlow(DataBroker dataBroker, TenantManage tenantManage); String FlowHandling(UserId userId, Flow flow);  }
@Test public void testcheckDefinition() throws Exception { Class<UpdateFlow> class3 = UpdateFlow.class; Method method = class3.getDeclaredMethod("checkDefinition",new Class[]{Flow.class}); method.setAccessible(true); CheckedFuture matchitemdefinitionFuture = mock(CheckedFuture.class); ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class); when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction); when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(matchitemdefinitionFuture); List<MatchItemDefinition> matchItemDefinitions = new ArrayList<MatchItemDefinition>(); List<MatchItemDefinition> matchItemDefinitionsnull = new ArrayList<MatchItemDefinition>(); MatchItemDefinition matchItemDefinition = mock(MatchItemDefinition.class); matchItemDefinitions.add(matchItemDefinition); when(matchItemDefinition.getMatchItemName()).thenReturn(matchItemName); Class<UpdateFlow> class1 = UpdateFlow.class; Class<GetDefinitions> class2 = GetDefinitions.class; Field field = class1.getDeclaredField("getDefinitions"); field.setAccessible(true); Field field1 = class2.getDeclaredField("matchItemDefinitionList"); field1.setAccessible(true); field1.set(field.get(updateFlow), matchItemDefinitions); when(flow.getMatchItem()).thenReturn(matchItemList); when(matchItem.getMatchItemName()).thenReturn(matchItemName2); Assert.assertEquals(method.invoke(updateFlow, flow), "The match item has not been defined."); field1.set(field.get(updateFlow), matchItemDefinitionsnull); Assert.assertEquals(method.invoke(updateFlow, flow), "The match item has not been defined."); field1.set(field.get(updateFlow), matchItemDefinitions); when(flow.getMatchItem()).thenReturn(matchItemListnull); Assert.assertEquals(method.invoke(updateFlow, flow), null); }