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 users",
                DataManager.constructObjectQuery(DataManager.ACTION_SELECT_ALL, User.class, false));
        assertEquals("DELETE FROM groups WHERE id = :id",
                DataManager.constructObjectQuery(DataManager.ACTION_DELETE, Group.class, false));
        assertEquals("SELECT * FROM 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 user_device",
                DataManager.constructPermissionQuery(DataManager.ACTION_SELECT_ALL, User.class, Device.class));

        assertEquals("SELECT userId, managedUserId FROM user_user",
                DataManager.constructPermissionQuery(DataManager.ACTION_SELECT_ALL, User.class, ManagedUser.class));

        assertEquals("SELECT deviceId, driverId FROM device_driver",
                DataManager.constructPermissionQuery(DataManager.ACTION_SELECT_ALL, Device.class, Driver.class));

        assertEquals("SELECT groupId, geofenceId FROM group_geofence",
                DataManager.constructPermissionQuery(DataManager.ACTION_SELECT_ALL, Group.class, Geofence.class));

        assertEquals("INSERT INTO user_device (userId, deviceId) VALUES (:userId, :deviceId)",
                DataManager.constructPermissionQuery(DataManager.ACTION_INSERT, User.class, Device.class));

        assertEquals("DELETE FROM user_user WHERE userId = :userId AND managedUserId = :managedUserId",
                DataManager.constructPermissionQuery(DataManager.ACTION_DELETE, User.class, ManagedUser.class));

        assertEquals("INSERT INTO device_geofence (deviceId, geofenceId) VALUES (:deviceId, :geofenceId)",
                DataManager.constructPermissionQuery(DataManager.ACTION_INSERT, Device.class, Geofence.class));

        assertEquals("DELETE FROM group_attribute WHERE groupId = :groupId AND attributeId = :attributeId",
                DataManager.constructPermissionQuery(DataManager.ACTION_DELETE, Group.class, Attribute.class));

    }

}

@@ -577,16 +577,19 @@
 ROM 
+tc_
 users%22,%0A
     
@@ -584,16 +584,16 @@
 users%22,%0A
-
         
@@ -715,24 +715,27 @@
 DELETE FROM 
+tc_
 groups WHERE
@@ -880,16 +880,19 @@
  * FROM 
+tc_
 position
@@ -2406,24 +2406,27 @@
 viceId FROM 
+tc_
 user_device%22
@@ -2592,24 +2592,27 @@
 UserId FROM 
+tc_
 user_user%22,%0A
@@ -2782,16 +2782,19 @@
 Id FROM 
+tc_
 device_d
@@ -2966,24 +2966,27 @@
 enceId FROM 
+tc_
 group_geofen
@@ -3140,16 +3140,19 @@
 RT INTO 
+tc_
 user_dev
@@ -3347,16 +3347,19 @@
 TE FROM 
+tc_
 user_use
@@ -3568,16 +3568,19 @@
 RT INTO 
+tc_
 device_g
@@ -3749,32 +3749,32 @@
 fence.class));%0A%0A
-
         assertEq
@@ -3791,16 +3791,19 @@
 TE FROM 
+tc_
 group_at

Update Database tests