views:

96

answers:

1

Hey there

Need to setup JMock code to test call back with google protobuf

Full project is located at http://github.com/andrewmilkowski/template-transport

In short, following are methods signatures (below)

what I need to do is to test method getLongValue, using Jmock JUnit4Mockery

what is the best and cleanest way to go about this

thanks much!

package com.argmaps.client.proto;

import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fepss.rpc.server.RpcApplication;
import com.fepss.rpc.client.RpcChannelImpl;

import org.apache.tapestry5.ioc.MappedConfiguration;

import com.google.protobuf.RpcController;
import com.google.protobuf.RpcCallback;

import com.argmaps.transport.proto.generated.TransportServer.ProtoService;
import com.argmaps.transport.proto.generated.TransportServer.ProtoService.Stub;
import com.argmaps.transport.proto.generated.TransportServer.DefaultLongValue;
import com.argmaps.transport.proto.generated.TransportServer.LongValue;
import com.argmaps.transport.proto.fepss.ProtoServer.TransportHandler;

public class TransportClient {

    protected final Log LOG = LogFactory.getLog(this.getClass().getName());

    private RpcController controller;

    private TransportHandler transportHandler;
    private ProtoService.Interface service;

    private void open(String host, int port) {

        RpcChannelImpl channel = new RpcChannelImpl(host, port);
     controller = channel.newRpcController();
     service = ProtoService.newStub(channel);

    }

    protected static class LongValueRpcCallback implements RpcCallback<LongValue> {

        private long longValue = 0L;

        @Override
     public void run(LongValue result) {
            longValue = result.getLongValue();
  }

        private long getLongValue() {

            return longValue;
        }
    }

    private void close() {

    }

    public long getLongValue(LongValueRpcCallback longValueRpcCallback) {

        DefaultLongValue defaultLongValue = DefaultLongValue.newBuilder().setLongValue(0L).build();

  service.getLongValue(controller, defaultLongValue, longValueRpcCallback);

        if (LOG.isDebugEnabled()) {
            LOG.debug("Long value from server:" + longValueRpcCallback.getLongValue());
        }

        return longValueRpcCallback.getLongValue();

    }

    public static void main(String[] args) {

        String host = "localhost";
        int port = 9090;

        final String portArgKey = "--port=";
        for (String cmd : args) {
            if (cmd.startsWith(portArgKey)) {
                port = Integer.parseInt(cmd.substring(portArgKey.length()));
                break;
            }
        }

        TransportClient c = new TransportClient();
        c.open(host, port);
        c.getLongValue(new LongValueRpcCallback());
        c.close();
    }

    public TransportClient() {

    }

    public static class TransportModule {

  public static void contributeIoHandler(MappedConfiguration<String, ProtoService> configruation) {

   configruation.add(ProtoService.getDescriptor().getFullName(), new TransportHandler());
  }
 }
}
A: 

Because of the callback, needed to:

  1. create abstract class LongValueRpcCallbackTemplate implements RpcCallback

  2. create class LongValueRpcCallback extends LongValueRpcCallbackTemplate

and then complete implementation in the test class

Test class:

package com.argmaps.client.proto;

import com.argmaps.transport.proto.generated.TransportServer; import com.fepss.rpc.client.RpcChannelImpl; import com.google.protobuf.RpcController; import org.jmock.Expectations; import org.junit.Test; import org.junit.Before; import org.junit.runner.RunWith;

import org.jmock.Mockery; import org.jmock.integration.junit4.JUnit4Mockery;

import static org.junit.Assert.assertEquals;

public class TransportClientTest {

Mockery context;

@Before
public void before() {

    context = new JUnit4Mockery();

}

private class TestLongValueRpcCallback extends LongValueRpcCallbackTemplate {

    private long longValue = 123456789L;

    @Override
    protected long getLongValue() {

        return longValue;
    }
}


@Test
public void testGetLongValue() {

    final TransportServer.ProtoService.Interface mockedTransportServer = context.mock(TransportServer.ProtoService.Interface.class);

    final RpcChannelImpl channel = new RpcChannelImpl("localhost", 9090);
    final RpcController controller = channel.newRpcController();
    final TransportServer.DefaultLongValue defaultLongValue = TransportServer.DefaultLongValue.newBuilder().setLongValue(0L).build();

    com.argmaps.client.proto.TransportClient testObject = new TransportClient(controller, mockedTransportServer);

    final TestLongValueRpcCallback testLongValueRpcCallback = new TestLongValueRpcCallback();

    final long testLongValue = 123456789L;

    context.checking(new Expectations() {
        {
            one(mockedTransportServer).getLongValue(controller, defaultLongValue, testLongValueRpcCallback);
        }
    });

    assertEquals(testLongValue, testObject.getLongValue(testLongValueRpcCallback));

}

}

Andrew Milkowski