aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/traccar/BaseTest.java
diff options
context:
space:
mode:
authorYuriy Piskarev <yuriy.piskarev@gmail.com>2023-08-24 14:16:17 +0300
committerGitHub <noreply@github.com>2023-08-24 14:16:17 +0300
commitae406c7b49a72de24d81fd74386d9638342c90ee (patch)
tree6fbcf557375b98e926c78af9c757e62c79d72a1b /src/test/java/org/traccar/BaseTest.java
parent56ff656c908b19feb2fa3dcffa48cc3bcdfe9b3b (diff)
parent9aeedc90da24848ff97227d6f281eb4d1e1506ef (diff)
downloadtrackermap-server-ae406c7b49a72de24d81fd74386d9638342c90ee.tar.gz
trackermap-server-ae406c7b49a72de24d81fd74386d9638342c90ee.tar.bz2
trackermap-server-ae406c7b49a72de24d81fd74386d9638342c90ee.zip
Merge branch 'traccar:master' into master
Diffstat (limited to 'src/test/java/org/traccar/BaseTest.java')
-rw-r--r--src/test/java/org/traccar/BaseTest.java77
1 files changed, 56 insertions, 21 deletions
diff --git a/src/test/java/org/traccar/BaseTest.java b/src/test/java/org/traccar/BaseTest.java
index 0b2c616ce..2ace781f3 100644
--- a/src/test/java/org/traccar/BaseTest.java
+++ b/src/test/java/org/traccar/BaseTest.java
@@ -1,34 +1,69 @@
package org.traccar;
-import io.netty.buffer.ByteBuf;
+import io.netty.channel.Channel;
+import org.traccar.config.Config;
+import org.traccar.database.CommandsManager;
import org.traccar.database.MediaManager;
+import org.traccar.database.StatisticsManager;
+import org.traccar.model.Device;
+import org.traccar.session.ConnectionManager;
+import org.traccar.session.DeviceSession;
+import org.traccar.session.cache.CacheManager;
-import java.util.HashMap;
-import java.util.Map;
+import java.net.SocketAddress;
+import java.util.HashSet;
-public class BaseTest {
-
- public static class MockMediaManager extends MediaManager {
- Map<String, ByteBuf> files = new HashMap<>();
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
- MockMediaManager() {
- super("");
- }
+public class BaseTest {
- @Override
- public String writeFile(String uniqueId, ByteBuf buf, String extension) {
- String fileName = uniqueId + "/mock." + extension;
- files.put(fileName, buf);
- return fileName;
- }
+ protected <T extends BaseProtocolDecoder> T inject(T decoder) throws Exception {
+ var config = new Config();
+ decoder.setConfig(config);
+ var device = mock(Device.class);
+ when(device.getId()).thenReturn(1L);
+ var cacheManager = mock(CacheManager.class);
+ when(cacheManager.getConfig()).thenReturn(config);
+ when(cacheManager.getObject(eq(Device.class), anyLong())).thenReturn(device);
+ decoder.setCacheManager(cacheManager);
+ var connectionManager = mock(ConnectionManager.class);
+ var uniqueIdsProvided = new HashSet<Boolean>();
+ when(connectionManager.getDeviceSession(any(), any(), any(), any(String[].class))).thenAnswer(invocation -> {
+ var mock = new DeviceSession(1L, "", mock(Protocol.class), mock(Channel.class), mock(SocketAddress.class));
+ if (uniqueIdsProvided.isEmpty()) {
+ if (invocation.getArguments().length > 3) {
+ uniqueIdsProvided.add(true);
+ return mock;
+ }
+ return null;
+ } else {
+ return mock;
+ }
+ });
+ decoder.setConnectionManager(connectionManager);
+ decoder.setStatisticsManager(mock(StatisticsManager.class));
+ decoder.setMediaManager(mock(MediaManager.class));
+ decoder.setCommandsManager(mock(CommandsManager.class));
+ return decoder;
+ }
- public ByteBuf readFile(String fileName) {
- return files.get(fileName);
- }
+ protected <T extends BaseFrameDecoder> T inject(T decoder) throws Exception {
+ return decoder;
}
- static {
- Context.init(new TestIdentityManager(), new MockMediaManager());
+ protected <T extends BaseProtocolEncoder> T inject(T encoder) throws Exception {
+ var device = mock(Device.class);
+ when(device.getId()).thenReturn(1L);
+ when(device.getUniqueId()).thenReturn("123456789012345");
+ var cacheManager = mock(CacheManager.class);
+ when(cacheManager.getConfig()).thenReturn(mock(Config.class));
+ when(cacheManager.getObject(eq(Device.class), anyLong())).thenReturn(device);
+ encoder.setCacheManager(cacheManager);
+ return encoder;
}
}