Mocking device com¶
For testing purpose, you can mock device communication.
To use a Mock device you have two choices:
- Mocking at the communication protocol level using a MockProtocol
- Mocking at the client level, using a MockClient
1) Using a Mock protocol¶
Using a Mock protocol will produce something closer that what you have with an IoTize device.
Using a pre implemented mock protocol¶
To do
MockProtocol moclProtocol = new MockProtocol(); // Map a specific response to a command mockProtocol.mapResponse(Command.GET("/1/2/3"), Response.SUCCESS("Hello World".getBytes())); // Command GET /1/2/3 will now always return a successfull response with "Hello World" payload // Map a dynamic response to a command: mockProtocol.mapResponse(Command.GET("/1/2/3"), new ResponseAdapter<byte[]>() { @Override public Response<byte[]> adapt(Command request) { Response<byte[]> response = // your custom response here... return response; } }); // Map a response with a custom filter mockProtocol.mapResponse(new Filter<Command>() { @Override public boolean isFiltered(Command command) { boolean isFiltered = // return false if you want to apply the ResponseAdapter to this command return isFiltered; } } , new ResponseAdapter<byte[]>() { @Override public Response<byte[]> adapt(Command request) { Response<byte[]> response = // your custom response here... return response; } });
Predefined mock protocol
Mirror protocol will always return a successful response. The payload of the response will be the encoded command.
let protocol: MockProtocol = MockProtocol.MIRROR();
ComProtocol protocol = MockProtocol.MIRROR();
See javadoc for Android of MockProtocol