aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/traccar/database
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java/org/traccar/database')
-rw-r--r--src/test/java/org/traccar/database/DataManagerTest.java81
-rw-r--r--src/test/java/org/traccar/database/GroupTreeTest.java56
2 files changed, 137 insertions, 0 deletions
diff --git a/src/test/java/org/traccar/database/DataManagerTest.java b/src/test/java/org/traccar/database/DataManagerTest.java
new file mode 100644
index 000000000..23043e96e
--- /dev/null
+++ b/src/test/java/org/traccar/database/DataManagerTest.java
@@ -0,0 +1,81 @@
+package org.traccar.database;
+
+import org.junit.Test;
+import org.traccar.model.Attribute;
+import org.traccar.model.Device;
+import org.traccar.model.Driver;
+import org.traccar.model.Geofence;
+import org.traccar.model.Group;
+import org.traccar.model.ManagedUser;
+import org.traccar.model.Position;
+import org.traccar.model.User;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class DataManagerTest {
+
+ @Test
+ public void constructObjectQuery() {
+ assertEquals("SELECT * FROM tc_users",
+ DataManager.constructObjectQuery(DataManager.ACTION_SELECT_ALL, User.class, false));
+ assertEquals("DELETE FROM tc_groups WHERE id = :id",
+ DataManager.constructObjectQuery(DataManager.ACTION_DELETE, Group.class, false));
+ assertEquals("SELECT * FROM tc_positions WHERE id = :id",
+ DataManager.constructObjectQuery(DataManager.ACTION_SELECT, Position.class, false));
+
+ String insertDevice = DataManager.constructObjectQuery(DataManager.ACTION_INSERT, Device.class, false);
+ assertFalse(insertDevice.contains("class"));
+ assertFalse(insertDevice.contains("id"));
+ assertFalse(insertDevice.contains("status"));
+ assertFalse(insertDevice.contains("geofenceIds"));
+
+ String updateDeviceStatus = DataManager.constructObjectQuery("update", Device.class, true);
+ assertTrue(updateDeviceStatus.contains("lastUpdate"));
+
+ String updateUser = DataManager.constructObjectQuery(DataManager.ACTION_UPDATE, User.class, false);
+ assertFalse(updateUser.contains("class"));
+ assertFalse(updateUser.contains("password"));
+ assertFalse(updateUser.contains("salt"));
+
+ String updateUserPassword = DataManager.constructObjectQuery(DataManager.ACTION_UPDATE, User.class, true);
+ assertFalse(updateUserPassword.contains("name"));
+ assertTrue(updateUserPassword.contains("hashedPassword"));
+ assertTrue(updateUserPassword.contains("salt"));
+
+ String insertPosition = DataManager.constructObjectQuery(DataManager.ACTION_INSERT, Position.class, false);
+ assertFalse(insertPosition.contains("type"));
+ assertFalse(insertPosition.contains("outdated"));
+
+ }
+
+ @Test
+ public void constructPermissionsQuery() {
+ assertEquals("SELECT userId, deviceId FROM tc_user_device",
+ DataManager.constructPermissionQuery(DataManager.ACTION_SELECT_ALL, User.class, Device.class));
+
+ assertEquals("SELECT userId, managedUserId FROM tc_user_user",
+ DataManager.constructPermissionQuery(DataManager.ACTION_SELECT_ALL, User.class, ManagedUser.class));
+
+ assertEquals("SELECT deviceId, driverId FROM tc_device_driver",
+ DataManager.constructPermissionQuery(DataManager.ACTION_SELECT_ALL, Device.class, Driver.class));
+
+ assertEquals("SELECT groupId, geofenceId FROM tc_group_geofence",
+ DataManager.constructPermissionQuery(DataManager.ACTION_SELECT_ALL, Group.class, Geofence.class));
+
+ assertEquals("INSERT INTO tc_user_device (userId, deviceId) VALUES (:userId, :deviceId)",
+ DataManager.constructPermissionQuery(DataManager.ACTION_INSERT, User.class, Device.class));
+
+ assertEquals("DELETE FROM tc_user_user WHERE userId = :userId AND managedUserId = :managedUserId",
+ DataManager.constructPermissionQuery(DataManager.ACTION_DELETE, User.class, ManagedUser.class));
+
+ assertEquals("INSERT INTO tc_device_geofence (deviceId, geofenceId) VALUES (:deviceId, :geofenceId)",
+ DataManager.constructPermissionQuery(DataManager.ACTION_INSERT, Device.class, Geofence.class));
+
+ assertEquals("DELETE FROM tc_group_attribute WHERE groupId = :groupId AND attributeId = :attributeId",
+ DataManager.constructPermissionQuery(DataManager.ACTION_DELETE, Group.class, Attribute.class));
+
+ }
+
+}
diff --git a/src/test/java/org/traccar/database/GroupTreeTest.java b/src/test/java/org/traccar/database/GroupTreeTest.java
new file mode 100644
index 000000000..b547aab60
--- /dev/null
+++ b/src/test/java/org/traccar/database/GroupTreeTest.java
@@ -0,0 +1,56 @@
+package org.traccar.database;
+
+import org.junit.Test;
+import org.traccar.model.Device;
+import org.traccar.model.Group;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+import static org.junit.Assert.assertEquals;
+
+public class GroupTreeTest {
+
+ private static Group createGroup(long id, String name, long parent) {
+ Group group = new Group();
+ group.setId(id);
+ group.setName(name);
+ group.setGroupId(parent);
+ return group;
+ }
+
+ private static Device createDevice(long id, String name, long parent) {
+ Device device = new Device();
+ device.setId(id);
+ device.setName(name);
+ device.setGroupId(parent);
+ return device;
+ }
+
+ @Test
+ public void testGetDescendants() {
+ Collection<Group> groups = new ArrayList<>();
+ groups.add(createGroup(1, "First", 0));
+ groups.add(createGroup(2, "Second", 1));
+ groups.add(createGroup(3, "Third", 2));
+ groups.add(createGroup(4, "Fourth", 2));
+ groups.add(createGroup(5, "Fifth", 4));
+
+ Collection<Device> devices = new ArrayList<>();
+ devices.add(createDevice(1, "One", 3));
+ devices.add(createDevice(2, "Two", 5));
+ devices.add(createDevice(3, "One", 5));
+
+ GroupTree groupTree = new GroupTree(groups, devices);
+
+ assertEquals(4, groupTree.getGroups(1).size());
+ assertEquals(3, groupTree.getGroups(2).size());
+ assertEquals(0, groupTree.getGroups(3).size());
+ assertEquals(1, groupTree.getGroups(4).size());
+
+ assertEquals(3, groupTree.getDevices(1).size());
+ assertEquals(1, groupTree.getDevices(3).size());
+ assertEquals(2, groupTree.getDevices(4).size());
+ }
+
+}