From 2e130b374fed3edc10b448120be0db3e388bfb28 Mon Sep 17 00:00:00 2001 From: Gavriel Fleischer Date: Sun, 17 Feb 2019 01:33:47 +0200 Subject: don't save response + decoder test --- .../traccar/protocol/WatchProtocolDecoderTest.java | 43 ++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'test/org/traccar/protocol/WatchProtocolDecoderTest.java') diff --git a/test/org/traccar/protocol/WatchProtocolDecoderTest.java b/test/org/traccar/protocol/WatchProtocolDecoderTest.java index b7c8d83f9..c4e5bf61a 100644 --- a/test/org/traccar/protocol/WatchProtocolDecoderTest.java +++ b/test/org/traccar/protocol/WatchProtocolDecoderTest.java @@ -1,7 +1,17 @@ package org.traccar.protocol; +import io.netty.buffer.ByteBuf; import org.junit.Test; +import org.traccar.Context; import org.traccar.ProtocolTest; +import org.traccar.database.MediaManager; +import org.traccar.model.Position; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; public class WatchProtocolDecoderTest extends ProtocolTest { @@ -101,7 +111,40 @@ public class WatchProtocolDecoderTest extends ProtocolTest { verifyPosition(decoder, buffer( "[ZJ*014111001350304*0038*008a*UD,070318,021027,V,00.000000,N,000.000000,E,0,0,0,0,100,18,1000,50,00000000,4,255,460,0,9346,5223,42,9346,5214,20,9784,4083,11,9346,5221,5]")); + } + + @Test + public void testDecodeTK() throws Exception { + WatchProtocolDecoder decoder = new WatchProtocolDecoder(null); + verifyNull(decoder.decode(null, null, buffer("[CS*1234567890*0004*TK,1]"))); + + MockMediaManager mockMediaManager = new MockMediaManager("/tmp"); + Context.initMediaManager(mockMediaManager); + String hex = "7d5b5d2c2aff"; + Object decodedObject = decoder.decode(null, null, concatenateBuffers(buffer("[CS*1234567890*000e*TK,#!AMR"), binary(hex), buffer("]"))); + assertTrue("not a position", decodedObject instanceof Position); + Position position = (Position) decodedObject; + assertEquals("1234567890/mock.amr", position.getAttributes().get("audio")); + verifyFrame(concatenateBuffers(buffer("#!AMR"), binary(hex)), mockMediaManager.readFile("1234567890/mock.amr")); } + private static class MockMediaManager extends MediaManager { + Map files = new HashMap<>(); + + MockMediaManager(String path) { + super(path); + } + + @Override + public String writeFile(String uniqueId, ByteBuf buf, String extension) { + String fileName = uniqueId + "/mock." + extension; + files.put(fileName, buf); + return fileName; + } + + ByteBuf readFile(String fileName) { + return files.get(fileName); + } + } } -- cgit v1.2.3 From 074b1c302dcd6671737d44c69aa83d0fdd179168 Mon Sep 17 00:00:00 2001 From: Gavriel Fleischer Date: Sun, 17 Feb 2019 21:42:29 +0200 Subject: resolving review issues --- src/org/traccar/protocol/WatchFrameDecoder.java | 12 +++++------ src/org/traccar/protocol/WatchProtocolDecoder.java | 24 +++++++++------------- .../traccar/protocol/WatchProtocolDecoderTest.java | 2 +- 3 files changed, 17 insertions(+), 21 deletions(-) (limited to 'test/org/traccar/protocol/WatchProtocolDecoderTest.java') diff --git a/src/org/traccar/protocol/WatchFrameDecoder.java b/src/org/traccar/protocol/WatchFrameDecoder.java index b4a82c0be..f99bd52e2 100644 --- a/src/org/traccar/protocol/WatchFrameDecoder.java +++ b/src/org/traccar/protocol/WatchFrameDecoder.java @@ -31,10 +31,10 @@ public class WatchFrameDecoder extends BaseFrameDecoder { if (endIndex > 0) { ByteBuf frame = Unpooled.buffer(); while (buf.readerIndex() < endIndex) { - byte b = buf.readByte(); - if (b == '}') { - byte c = buf.readByte(); - switch (c) { + byte b1 = buf.readByte(); + if (b1 == '}') { + byte b2 = buf.readByte(); + switch (b2) { case 0x01: frame.writeByte('}'); break; @@ -52,10 +52,10 @@ public class WatchFrameDecoder extends BaseFrameDecoder { break; default: throw new IllegalArgumentException(String.format( - "unexpected byte at %d: 0x%02x", buf.readerIndex() - 1, c)); + "unexpected byte at %d: 0x%02x", buf.readerIndex() - 1, b2)); } } else { - frame.writeByte(b); + frame.writeByte(b1); } } return frame; diff --git a/src/org/traccar/protocol/WatchProtocolDecoder.java b/src/org/traccar/protocol/WatchProtocolDecoder.java index aaf63206c..70b207e9b 100644 --- a/src/org/traccar/protocol/WatchProtocolDecoder.java +++ b/src/org/traccar/protocol/WatchProtocolDecoder.java @@ -67,7 +67,7 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { .expression("(.*)") // cell and wifi .compile(); - private void sendTextResponse(Channel channel, String id, String index, String content) { + private void sendResponse(Channel channel, String id, String index, String content) { if (channel != null) { String response; if (index != null) { @@ -182,10 +182,6 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { return manufacturer; } - protected int nextIndexOf(ByteBuf buf, char delimiter) { - return buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) delimiter); - } - @Override protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { @@ -196,7 +192,7 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { manufacturer = buf.readSlice(2).toString(StandardCharsets.US_ASCII); buf.skipBytes(1); // '*' delimiter - int idIndex = nextIndexOf(buf, '*'); + int idIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); String id = buf.readSlice(idIndex - buf.readerIndex()).toString(StandardCharsets.US_ASCII); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id); if (deviceSession == null) { @@ -206,10 +202,10 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { buf.skipBytes(1); // '*' delimiter String index = null; - int contentIndex = nextIndexOf(buf, '*'); + int contentIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); if (contentIndex + 5 < buf.writerIndex() && buf.getByte(contentIndex + 5) == '*' && buf.toString(contentIndex + 1, 4, StandardCharsets.US_ASCII).matches("\\p{XDigit}+")) { - int indexLength = nextIndexOf(buf, '*') - buf.readerIndex(); + int indexLength = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*') - buf.readerIndex(); hasIndex = true; index = buf.readSlice(indexLength).toString(StandardCharsets.US_ASCII); buf.skipBytes(1); // '*' delimiter @@ -220,7 +216,7 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { buf.writerIndex(buf.writerIndex() - 1); // ']' ignore ending - contentIndex = nextIndexOf(buf, ','); + contentIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ','); if (contentIndex < 0) { contentIndex = buf.writerIndex(); } @@ -233,11 +229,11 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { if (type.equals("INIT")) { - sendTextResponse(channel, id, index, "INIT,1"); + sendResponse(channel, id, index, "INIT,1"); } else if (type.equals("LK")) { - sendTextResponse(channel, id, index, "LK"); + sendResponse(channel, id, index, "LK"); if (buf.isReadable()) { String[] values = buf.toString(StandardCharsets.US_ASCII).split(","); @@ -262,14 +258,14 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { if (position != null) { position.set(Position.KEY_ALARM, Position.ALARM_SOS); } - sendTextResponse(channel, id, index, "AL"); + sendResponse(channel, id, index, "AL"); } return position; } else if (type.equals("TKQ")) { - sendTextResponse(channel, id, index, "TKQ"); + sendResponse(channel, id, index, "TKQ"); } else if (type.equals("PULSE") || type.equals("heart") || type.equals("bphrt")) { @@ -311,7 +307,7 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { if (buf.readableBytes() == 1) { byte result = buf.readByte(); if (result != '1') { - LOGGER.error(type + "," + result); + LOGGER.warn(type + "," + result); } return null; } diff --git a/test/org/traccar/protocol/WatchProtocolDecoderTest.java b/test/org/traccar/protocol/WatchProtocolDecoderTest.java index c4e5bf61a..f1287d721 100644 --- a/test/org/traccar/protocol/WatchProtocolDecoderTest.java +++ b/test/org/traccar/protocol/WatchProtocolDecoderTest.java @@ -114,7 +114,7 @@ public class WatchProtocolDecoderTest extends ProtocolTest { } @Test - public void testDecodeTK() throws Exception { + public void testDecodeVoiceMessage() throws Exception { WatchProtocolDecoder decoder = new WatchProtocolDecoder(null); verifyNull(decoder.decode(null, null, buffer("[CS*1234567890*0004*TK,1]"))); -- cgit v1.2.3 From c82be0541481955a0832948574244f31757f7067 Mon Sep 17 00:00:00 2001 From: Gavriel Fleischer Date: Sun, 17 Feb 2019 21:53:54 +0200 Subject: moved MockMediaManager to BaseTest --- src/org/traccar/Context.java | 7 ++---- test/org/traccar/BaseTest.java | 29 ++++++++++++++++++++-- .../traccar/protocol/WatchProtocolDecoderTest.java | 23 +---------------- 3 files changed, 30 insertions(+), 29 deletions(-) (limited to 'test/org/traccar/protocol/WatchProtocolDecoderTest.java') diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index b0db90416..d679842cd 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -508,16 +508,13 @@ public final class Context { Context.getConfig().getBoolean("event.overspeed.preferLowest")); } - public static void init(IdentityManager testIdentityManager) { + public static void init(IdentityManager testIdentityManager, MediaManager testMediaManager) { config = new Config(); objectMapper = new ObjectMapper(); objectMapper.registerModule(new JSR353Module()); client = ClientBuilder.newClient().register(new ObjectMapperContextResolver()); identityManager = testIdentityManager; - } - - public static void initMediaManager(MediaManager mediaManager) { - Context.mediaManager = mediaManager; + mediaManager = testMediaManager; } public static BaseObjectManager getManager(Class clazz) { diff --git a/test/org/traccar/BaseTest.java b/test/org/traccar/BaseTest.java index fb1b5de8f..0b2c616ce 100644 --- a/test/org/traccar/BaseTest.java +++ b/test/org/traccar/BaseTest.java @@ -1,9 +1,34 @@ package org.traccar; +import io.netty.buffer.ByteBuf; +import org.traccar.database.MediaManager; + +import java.util.HashMap; +import java.util.Map; + public class BaseTest { - + + public static class MockMediaManager extends MediaManager { + Map files = new HashMap<>(); + + MockMediaManager() { + super(""); + } + + @Override + public String writeFile(String uniqueId, ByteBuf buf, String extension) { + String fileName = uniqueId + "/mock." + extension; + files.put(fileName, buf); + return fileName; + } + + public ByteBuf readFile(String fileName) { + return files.get(fileName); + } + } + static { - Context.init(new TestIdentityManager()); + Context.init(new TestIdentityManager(), new MockMediaManager()); } } diff --git a/test/org/traccar/protocol/WatchProtocolDecoderTest.java b/test/org/traccar/protocol/WatchProtocolDecoderTest.java index f1287d721..51846f51a 100644 --- a/test/org/traccar/protocol/WatchProtocolDecoderTest.java +++ b/test/org/traccar/protocol/WatchProtocolDecoderTest.java @@ -119,32 +119,11 @@ public class WatchProtocolDecoderTest extends ProtocolTest { verifyNull(decoder.decode(null, null, buffer("[CS*1234567890*0004*TK,1]"))); - MockMediaManager mockMediaManager = new MockMediaManager("/tmp"); - Context.initMediaManager(mockMediaManager); String hex = "7d5b5d2c2aff"; Object decodedObject = decoder.decode(null, null, concatenateBuffers(buffer("[CS*1234567890*000e*TK,#!AMR"), binary(hex), buffer("]"))); assertTrue("not a position", decodedObject instanceof Position); Position position = (Position) decodedObject; assertEquals("1234567890/mock.amr", position.getAttributes().get("audio")); - verifyFrame(concatenateBuffers(buffer("#!AMR"), binary(hex)), mockMediaManager.readFile("1234567890/mock.amr")); - } - - private static class MockMediaManager extends MediaManager { - Map files = new HashMap<>(); - - MockMediaManager(String path) { - super(path); - } - - @Override - public String writeFile(String uniqueId, ByteBuf buf, String extension) { - String fileName = uniqueId + "/mock." + extension; - files.put(fileName, buf); - return fileName; - } - - ByteBuf readFile(String fileName) { - return files.get(fileName); - } + verifyFrame(concatenateBuffers(buffer("#!AMR"), binary(hex)), ((MockMediaManager) Context.getMediaManager()).readFile("1234567890/mock.amr")); } } -- cgit v1.2.3