Search in sources :

Example 1 with MessageImpl

use of org.jdiameter.client.impl.parser.MessageImpl in project jain-slee.diameter by RestComm.

the class DiameterActivityAnswerCreationHelper method testAnswerCreation.

public static void testAnswerCreation(DiameterActivity activity, String messageListFieldName, ArrayList<DiameterMessage> requestList) {
    try {
        Field f = activity.getClass().getDeclaredField(messageListFieldName);
        assertNotNull("There is no such field: " + messageListFieldName, f);
        f.setAccessible(true);
        boolean setOnIterate = false;
        if (f.get(activity) instanceof Collection) {
            f.set(activity, requestList);
        } else {
            setOnIterate = true;
        }
        for (DiameterMessage request : requestList) {
            if (setOnIterate) {
                f.set(activity, request);
            }
            DiameterMessageImpl requestImpl = (DiameterMessageImpl) request;
            // This is cause some of them are zeros :)
            ((MessageImpl) requestImpl.getGenericData()).setEndToEndIdentifier((long) (Math.random() * 10000));
            ((MessageImpl) requestImpl.getGenericData()).setHopByHopIdentifier((long) (Math.random() * 10000));
            Class<? extends DiameterActivity> activityClass = activity.getClass();
            String className = request.getClass().getName();
            String packageName = request.getClass().getPackage().getName();
            String methodName = "create" + (className.replace(packageName + ".", "").replace("RequestImpl", "Answer"));
            Method[] methods = activityClass.getMethods();
            for (Method createAnswerMethod : methods) {
                if (!createAnswerMethod.getName().equals(methodName)) {
                    // System.err.println("Skipping: "+createAnswerMethod.getName());
                    continue;
                }
                // System.err.println("Doing magic for: "+createAnswerMethod.getName());
                // here we can have 3 types of methods:
                // #1 without parameters
                // #2 with long, boolean parameters
                // #3 with different param!!
                Object[] args = null;
                Class<?>[] parameters = createAnswerMethod.getParameterTypes();
                if (parameters.length == 0) {
                    args = new Object[] {};
                // params are null
                } else {
                    args = new Object[parameters.length];
                    for (int i = 0; i < parameters.length; i++) {
                        args[i] = instantiate(parameters[i]);
                    }
                }
                // we only want to see that code matches and answer is not zero? do we want to test AVPs ?
                DiameterMessage answer = (DiameterMessage) createAnswerMethod.invoke(activity, args);
                assertNotNull("Answer is null for method name: " + methodName, answer);
                if (answer == null)
                    return;
                // some basic tests, we don't test values passed, maybe we should ?
                assertEquals("Command code is not equal", request.getCommand().getCode(), answer.getCommand().getCode());
                assertFalse("Answer should have R flag set to false.", answer.getCommand().isRequest());
                assertEquals("E2E is not equal", request.getHeader().getEndToEndId(), answer.getHeader().getEndToEndId());
                assertEquals("HBH is not equal", request.getHeader().getHopByHopId(), answer.getHeader().getHopByHopId());
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : DiameterMessage(net.java.slee.resource.diameter.base.events.DiameterMessage) Method(java.lang.reflect.Method) Field(java.lang.reflect.Field) Collection(java.util.Collection) DiameterMessageImpl(org.mobicents.slee.resource.diameter.base.events.DiameterMessageImpl) MessageImpl(org.jdiameter.client.impl.parser.MessageImpl) DiameterMessageImpl(org.mobicents.slee.resource.diameter.base.events.DiameterMessageImpl)

Example 2 with MessageImpl

use of org.jdiameter.client.impl.parser.MessageImpl in project jain-slee.diameter by RestComm.

the class DiameterMessageFactoryImpl method createMessage.

public ExtensionDiameterMessage createMessage(DiameterCommand command, DiameterAvp[] avps) throws AvpNotAllowedException {
    ApplicationId aid = command.getCode() == AccountingRequest.commandCode ? ApplicationId.createByAccAppId(0, command.getApplicationId()) : ApplicationId.createByAuthAppId(0, command.getApplicationId());
    ExtensionDiameterMessageImpl msg = (ExtensionDiameterMessageImpl) this.createDiameterMessage(null, avps, command.getCode(), aid);
    msg.getGenericData().setRequest(command.isRequest());
    ((MessageImpl) msg.getGenericData()).setProxiable(command.isProxiable());
    if (!command.isRequest()) {
        // just in case. answers never have T flag set
        ((MessageImpl) msg.getGenericData()).setReTransmitted(false);
    }
    return msg;
}
Also used : ExtensionDiameterMessageImpl(org.mobicents.slee.resource.diameter.base.events.ExtensionDiameterMessageImpl) ApplicationId(org.jdiameter.api.ApplicationId) ExtensionDiameterMessageImpl(org.mobicents.slee.resource.diameter.base.events.ExtensionDiameterMessageImpl) MessageImpl(org.jdiameter.client.impl.parser.MessageImpl)

Aggregations

MessageImpl (org.jdiameter.client.impl.parser.MessageImpl)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 DiameterMessage (net.java.slee.resource.diameter.base.events.DiameterMessage)1 ApplicationId (org.jdiameter.api.ApplicationId)1 DiameterMessageImpl (org.mobicents.slee.resource.diameter.base.events.DiameterMessageImpl)1 ExtensionDiameterMessageImpl (org.mobicents.slee.resource.diameter.base.events.ExtensionDiameterMessageImpl)1