Exemple #1
0
  private static Map<String, String> convertParameterMap(LoginApi api) {
    Map<String, String> result = new HashMap<>();

    Field[] fields = api.getClass().getDeclaredFields();
    for (Field field : fields) {
      ParameterName annotation = field.getAnnotation(ParameterName.class);
      if (annotation != null) {
        field.setAccessible(true);

        String key = annotation.value();
        try {
          Object valueObj = field.get(api);
          if (valueObj != null) {
            String value = valueObj.toString();
            if (value != null) {
              result.put(key, value);
            }
          }
        } catch (IllegalAccessException e) {
          e.printStackTrace();
        }
      }
    }
    return result;
  }
  @Test
  public void testCheckOperationTemplate() throws Exception {
    method =
        class1.getDeclaredMethod(
            "checkOperationTemplate", new Class[] {AbstractOperation.class, Map.class});
    method.setAccessible(true);
    field = class1.getDeclaredField("getDefinitions");
    field.setAccessible(true);

    GetDefinitions getDefinitions = mock(GetDefinitions.class);
    Action action = mock(Action.class);
    ActionName actionName = mock(ActionName.class);
    ActionDefinition actionDefinition = mock(ActionDefinition.class);
    AbstractOperation operation = mock(AbstractOperation.class);
    ConditionSegment conditionSegment = mock(ConditionSegment.class);
    ParameterName parameterName = mock(ParameterName.class);
    ParameterValues parameterValues = mock(ParameterValues.class);
    ConditionParameterName conditionParameterName = mock(ConditionParameterName.class);
    ConditionParameterDefinition definition = mock(ConditionParameterDefinition.class);
    ConditionParameterDefinition conditionParameterDefinition =
        mock(ConditionParameterDefinition.class);
    ConditionParameterTargetValue conditionParameterTargetValue =
        mock(ConditionParameterTargetValue.class);
    ConditionParameterDefinition.ParameterValueType type =
        ConditionParameterDefinition.ParameterValueType.String;
    TemplateParameter.ParameterValueType valueType = TemplateParameter.ParameterValueType.Int;
    ActionDefinition.ParameterValueType type1 = ActionDefinition.ParameterValueType.String;
    org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.operation.rev151010.action
            .instance.parameter.values.StringValue
        stringValue =
            mock(
                org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.operation
                    .rev151010.action.instance.parameter.values.StringValue.class);
    List<Action> actionList = new ArrayList<Action>();
    List<
            org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.operation
                .rev151010.action.instance.parameter.values.StringValue>
        stringValues =
            new ArrayList<
                org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.operation
                    .rev151010.action.instance.parameter.values.StringValue>();
    List<ConditionSegment> conditionSegmentList = new ArrayList<ConditionSegment>();
    Map<ParameterName, TemplateParameter.ParameterValueType> parameterValueTypeMap =
        new HashMap<ParameterName, TemplateParameter.ParameterValueType>();
    Map<ActionName, ActionDefinition> actionDefinitionMap =
        new HashMap<ActionName, ActionDefinition>();
    Map<ParameterName, ConditionParameterDefinition> conditionParameterDefinitionMap =
        mock(Map.class);

    field.set(updateTemplateDefinition, getDefinitions);
    conditionSegmentList.add(conditionSegment);
    parameterValueTypeMap.put(new ParameterName("ParameterName"), valueType);
    conditionParameterDefinitionMap.put(parameterName, conditionParameterDefinition);

    when(getDefinitions.getActionDefinition()).thenReturn(actionDefinitionMap);
    when(getDefinitions.getConditionParameterDefinition())
        .thenReturn(conditionParameterDefinitionMap);
    when(operation.getConditionSegment()).thenReturn(null).thenReturn(conditionSegmentList);
    when(operation.getAction()).thenReturn(null).thenReturn(actionList);
    Assert.assertTrue(
        method.invoke(updateTemplateDefinition, operation, parameterValueTypeMap) == null);
    // test condition
    when(conditionSegment.getConditionParameterName()).thenReturn(conditionParameterName);
    when(conditionParameterDefinitionMap.containsKey(conditionSegment.getConditionParameterName()))
        .thenReturn(false)
        .thenReturn(true);
    when(parameterName.getValue()).thenReturn("test");
    Assert.assertTrue(
        method
            .invoke(updateTemplateDefinition, operation, parameterValueTypeMap)
            .equals(
                "The Condition "
                    + conditionSegment.getConditionParameterName().getValue()
                    + " is not defined."));
    when(conditionSegment.getConditionParameterTargetValue())
        .thenReturn(null)
        .thenReturn(conditionParameterTargetValue);
    Assert.assertTrue(
        method.invoke(updateTemplateDefinition, operation, parameterValueTypeMap) == null);
    when(conditionParameterDefinitionMap.get(conditionSegment.getConditionParameterName()))
        .thenReturn(definition);
    when(definition.getParameterValueType())
        .thenReturn(ConditionParameterDefinition.ParameterValueType.Int)
        .thenReturn(type);
    Assert.assertTrue(
        method.invoke(updateTemplateDefinition, operation, parameterValueTypeMap) == null);
    when(conditionParameterTargetValue.getStringValue()).thenReturn("ParameterName");
    Assert.assertTrue(
        method
            .invoke(updateTemplateDefinition, operation, parameterValueTypeMap)
            .equals(
                "The condition "
                    + conditionSegment.getConditionParameterName().getValue()
                    + " type is not right."));

    // test action
    conditionSegmentList.clear();
    actionList.add(action);
    stringValues.add(stringValue);

    when(action.getActionName()).thenReturn(actionName);
    when(actionName.getValue()).thenReturn("test");
    Assert.assertTrue(
        method
            .invoke(updateTemplateDefinition, operation, parameterValueTypeMap)
            .equals("The action " + action.getActionName().getValue() + " is not defined."));
    actionDefinitionMap.put(actionName, actionDefinition);
    when(action.getParameterValues()).thenReturn(null).thenReturn(parameterValues);
    Assert.assertTrue(
        method.invoke(updateTemplateDefinition, operation, parameterValueTypeMap) == null);
    when(actionDefinition.getParameterValueType()).thenReturn(type1);
    when(parameterValues.getStringValue()).thenReturn(stringValues);
    when(stringValue.getValue()).thenReturn("ParameterName");
    Assert.assertTrue(
        method
            .invoke(updateTemplateDefinition, operation, parameterValueTypeMap)
            .equals("The action " + action.getActionName().getValue() + " type is not right."));
  }