aboutsummaryrefslogtreecommitdiff
path: root/test/org/traccar/TestIdentityManager.java
blob: 82bcf5c63e0d7f374e2db3e717e1967261c8600f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package org.traccar;

import org.traccar.database.*;
import org.traccar.model.*;
import java.util.concurrent.ConcurrentHashMap;

import java.util.Map;

public final class TestIdentityManager implements IdentityManager {

    private static Map<Long, Device> devicesById = new ConcurrentHashMap<>();

    private static final long TEST_DEVICE_ID_START = 100;
    private static final String TEST_DEVICE_DEFAULT_UNIQUE_ID_PREFIX = "555";
    private static long newDeviceId = TEST_DEVICE_ID_START;

    public TestIdentityManager() {
    }

    public static Device createTestDevice() {
        Device device = new Device();
        device.setId(newDeviceId);
        device.setName("TestDevice" + newDeviceId);
        device.setUniqueId(TEST_DEVICE_DEFAULT_UNIQUE_ID_PREFIX + newDeviceId);
        devicesById.put(newDeviceId, device);
        newDeviceId++;
        return device;
    }

    private Device createDefaultTestDevice() {
        Device device = new Device();
        device.setId(1);
        device.setName("test");
        device.setUniqueId("123456789012345");
        return device;
    }

    @Override
    public Device getById(long id) {
        return id < TEST_DEVICE_ID_START ? createDefaultTestDevice() : devicesById.get(id);
    }

    @Override
    public Device getByUniqueId(String uniqueId) {
        if (uniqueId.length() > TEST_DEVICE_DEFAULT_UNIQUE_ID_PREFIX.length()
                && uniqueId.substring(0, TEST_DEVICE_DEFAULT_UNIQUE_ID_PREFIX.length())
                .equals(TEST_DEVICE_DEFAULT_UNIQUE_ID_PREFIX)) {
            try {
                long id = Long.parseLong(
                        uniqueId.substring(TEST_DEVICE_DEFAULT_UNIQUE_ID_PREFIX.length(), uniqueId.length()));
                if (id >= TEST_DEVICE_ID_START) {
                    return devicesById.get(id);
                }
            } catch (NumberFormatException e) {}
        }
        return createDefaultTestDevice();
    }

    @Override
    public Position getLastPosition(long deviceId) {
        return null;
    }

    @Override
    public boolean isLatestPosition(Position position) {
        return true;
    }

    @Override
    public boolean lookupAttributeBoolean(
            long deviceId, String attributeName, boolean defaultValue, boolean lookupConfig) {
        if (deviceId < TEST_DEVICE_ID_START) {
            return defaultValue;
        }

        Object result = devicesById.get(deviceId).getAttributes().get(attributeName);
        if (result != null) {
            return result instanceof String ? Boolean.parseBoolean((String) result) : (Boolean) result;
        }
        return defaultValue;
    }

    @Override
    public String lookupAttributeString(
            long deviceId, String attributeName, String defaultValue, boolean lookupConfig) {
        if (deviceId < TEST_DEVICE_ID_START) {
            return "alarm,result";
        }

        Object result = devicesById.get(deviceId).getAttributes().get(attributeName);
        return result != null ? (String) result : defaultValue;
    }

    @Override
    public int lookupAttributeInteger(
            long deviceId, String attributeName, int defaultValue, boolean lookupConfig) {
        if (deviceId < TEST_DEVICE_ID_START) {
            return defaultValue;
        }

        Object result = devicesById.get(deviceId).getAttributes().get(attributeName);
        if (result != null) {
            return result instanceof String ? Integer.parseInt((String) result) : ((Number) result).intValue();
        }
        return defaultValue;
    }

    @Override
    public long lookupAttributeLong(
            long deviceId, String attributeName, long defaultValue, boolean lookupConfig) {
        if (deviceId < TEST_DEVICE_ID_START) {
            return defaultValue;
        }

        Object result = devicesById.get(deviceId).getAttributes().get(attributeName);
        if (result != null) {
            return result instanceof String ? Long.parseLong((String) result) : ((Number) result).longValue();
        }
        return defaultValue;
    }

}