2026-06-30 24681c81c09022f584a57006f2534b5f74723414
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package cn.iocoder.yudao.module.iot.service.thingmodel;
 
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotThingModelDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.model.ThingModelProperty;
import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.model.dataType.ThingModelBoolOrEnumDataSpecs;
import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.model.dataType.ThingModelDateOrTextDataSpecs;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotDataSpecsDataTypeEnum;
import cn.iocoder.yudao.module.iot.service.device.IotDeviceModbusPointService;
import cn.iocoder.yudao.module.iot.service.product.IotProductService;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
 
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
 
import static org.junit.jupiter.api.Assertions.*;
 
/**
 * {@link IotThingModelServiceImpl} 的单元测试
 *
 * @author 芋道源码
 */
@Import(IotThingModelServiceImpl.class)
public class IotThingModelServiceImplTest extends BaseDbUnitTest {
 
    @Resource
    private IotThingModelServiceImpl thingModelService;
 
    @MockitoBean
    private IotProductService productService;
    @MockitoBean
    private IotDeviceModbusPointService deviceModbusPointService;
 
    @Test
    public void testConvertThingModelPropertyValue_boolFromBooleanTrue() {
        assertBoolValueConvertedToByte(true, (byte) 1);
    }
 
    @Test
    public void testConvertThingModelPropertyValue_boolFromBooleanFalse() {
        assertBoolValueConvertedToByte(false, (byte) 0);
    }
 
    @Test
    public void testConvertThingModelPropertyValue_boolFromStringTrue() {
        assertBoolValueConvertedToByte("true", (byte) 1);
    }
 
    @Test
    public void testConvertThingModelPropertyValue_boolFromStringFalse() {
        assertBoolValueConvertedToByte("false", (byte) 0);
    }
 
    @Test
    public void testConvertThingModelPropertyValue_boolFromNumberOne() {
        assertBoolValueConvertedToByte(1, (byte) 1);
    }
 
    @Test
    public void testConvertThingModelPropertyValue_boolFromNumberZero() {
        assertBoolValueConvertedToByte(0, (byte) 0);
    }
 
    @Test
    public void testConvertThingModelPropertyValue_boolInvalid() {
        IotThingModelDO thingModel = buildThingModel("PowerSwitch", IotDataSpecsDataTypeEnum.BOOL.getDataType());
 
        Object result = thingModelService.convertThingModelPropertyValue(thingModel, "yes");
 
        assertNull(result);
    }
 
    @Test
    public void testConvertThingModelPropertyValue_enumFromStringNumber() {
        IotThingModelDO thingModel = buildEnumThingModel("WorkMode", enumSpec("Auto", 1), enumSpec("Manual", 2));
 
        Object result = thingModelService.convertThingModelPropertyValue(thingModel, "1");
 
        assertEquals((byte) 1, result);
    }
 
    @Test
    public void testConvertThingModelPropertyValue_enumFromSpecName() {
        IotThingModelDO thingModel = buildEnumThingModel("WorkMode", enumSpec("Auto", 1), enumSpec("Manual", 2));
 
        Object result = thingModelService.convertThingModelPropertyValue(thingModel, "Manual");
 
        assertEquals((byte) 2, result);
    }
 
    @Test
    public void testConvertThingModelPropertyValue_enumInvalidSpecValue() {
        IotThingModelDO thingModel = buildEnumThingModel("WorkMode", enumSpec("Auto", 1), enumSpec("Manual", 2));
 
        Object result = thingModelService.convertThingModelPropertyValue(thingModel, 3);
 
        assertNull(result);
    }
 
    @Test
    public void testConvertThingModelPropertyValue_enumOutOfTinyIntRange() {
        IotThingModelDO thingModel = buildThingModel("WorkMode", IotDataSpecsDataTypeEnum.ENUM.getDataType());
 
        Object result = thingModelService.convertThingModelPropertyValue(thingModel, 128);
 
        assertNull(result);
    }
 
    @Test
    public void testConvertThingModelPropertyValue_dateFromLocalDateTime() {
        IotThingModelDO thingModel = buildThingModel("CollectTime", IotDataSpecsDataTypeEnum.DATE.getDataType());
        LocalDateTime collectTime = LocalDateTime.of(2025, 1, 2, 3, 4, 5);
 
        Object result = thingModelService.convertThingModelPropertyValue(thingModel, collectTime);
 
        assertEquals(LocalDateTimeUtil.toEpochMilli(collectTime), result);
    }
 
    @Test
    public void testConvertThingModelPropertyValue_dateFromString() {
        IotThingModelDO thingModel = buildThingModel("CollectTime", IotDataSpecsDataTypeEnum.DATE.getDataType());
        LocalDateTime collectTime = LocalDateTime.of(2025, 1, 2, 3, 4, 5);
 
        Object result = thingModelService.convertThingModelPropertyValue(thingModel, "2025-01-02 03:04:05");
 
        assertEquals(LocalDateTimeUtil.toEpochMilli(collectTime), result);
    }
 
    @Test
    public void testConvertThingModelPropertyValue_intInvalid() {
        IotThingModelDO thingModel = buildThingModel("Temperature", IotDataSpecsDataTypeEnum.INT.getDataType());
 
        Object result = thingModelService.convertThingModelPropertyValue(thingModel, "abc");
 
        assertNull(result);
    }
 
    @Test
    public void testConvertThingModelPropertyValue_textOverLength() {
        IotThingModelDO thingModel = buildTextThingModel("Remark", 3);
 
        Object result = thingModelService.convertThingModelPropertyValue(thingModel, "abcd");
 
        assertNull(result);
    }
 
    @Test
    public void testConvertThingModelPropertyValue_structToJson() {
        IotThingModelDO thingModel = buildThingModel("GeoLocation", IotDataSpecsDataTypeEnum.STRUCT.getDataType());
        Map<String, Object> value = new HashMap<>();
        value.put("Longitude", 120.1D);
 
        Object result = thingModelService.convertThingModelPropertyValue(thingModel, value);
 
        assertEquals(JsonUtils.toJsonString(value), result);
    }
 
    private void assertBoolValueConvertedToByte(Object reportedValue, byte expected) {
        IotThingModelDO thingModel = buildThingModel("PowerSwitch", IotDataSpecsDataTypeEnum.BOOL.getDataType());
 
        Object result = thingModelService.convertThingModelPropertyValue(thingModel, reportedValue);
 
        assertEquals(expected, result);
    }
 
    private IotThingModelDO buildThingModel(String identifier, String dataType) {
        ThingModelProperty property = new ThingModelProperty();
        property.setIdentifier(identifier);
        property.setDataType(dataType);
        return IotThingModelDO.builder().identifier(identifier).property(property).build();
    }
 
    private IotThingModelDO buildEnumThingModel(String identifier, ThingModelBoolOrEnumDataSpecs... dataSpecsList) {
        IotThingModelDO thingModel = buildThingModel(identifier, IotDataSpecsDataTypeEnum.ENUM.getDataType());
        thingModel.getProperty().setDataSpecsList(Arrays.asList(dataSpecsList));
        return thingModel;
    }
 
    private ThingModelBoolOrEnumDataSpecs enumSpec(String name, Integer value) {
        ThingModelBoolOrEnumDataSpecs dataSpecs = new ThingModelBoolOrEnumDataSpecs();
        dataSpecs.setName(name);
        dataSpecs.setValue(value);
        return dataSpecs;
    }
 
    private IotThingModelDO buildTextThingModel(String identifier, Integer length) {
        IotThingModelDO thingModel = buildThingModel(identifier, IotDataSpecsDataTypeEnum.TEXT.getDataType());
        ThingModelDateOrTextDataSpecs dataSpecs = new ThingModelDateOrTextDataSpecs();
        dataSpecs.setLength(length);
        thingModel.getProperty().setDataSpecs(dataSpecs);
        return thingModel;
    }
 
}