Merge pull request #6126 from thingsboard/fix_bug_null_in_Metadata
[3.3.4] testScript_fix_bug_value_null
This commit is contained in:
		
						commit
						7b644bd5cb
					
				@ -38,7 +38,8 @@ public final class TbMsgMetaData implements Serializable {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public TbMsgMetaData(Map<String, String> data) {
 | 
			
		||||
        this.data = new ConcurrentHashMap<>(data);
 | 
			
		||||
        this.data = new ConcurrentHashMap<>();
 | 
			
		||||
        data.forEach(this::putValue);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@ -49,20 +50,20 @@ public final class TbMsgMetaData implements Serializable {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public String getValue(String key) {
 | 
			
		||||
        return data.get(key);
 | 
			
		||||
        return this.data.get(key);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void putValue(String key, String value) {
 | 
			
		||||
        if (key != null && value != null) {
 | 
			
		||||
            data.put(key, value);
 | 
			
		||||
            this.data.put(key, value);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Map<String, String> values() {
 | 
			
		||||
        return new HashMap<>(data);
 | 
			
		||||
        return new HashMap<>(this.data);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public TbMsgMetaData copy() {
 | 
			
		||||
        return new TbMsgMetaData(data);
 | 
			
		||||
        return new TbMsgMetaData(this.data);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,56 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Copyright © 2016-2022 The Thingsboard Authors
 | 
			
		||||
 *
 | 
			
		||||
 * Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
 * you may not use this file except in compliance with the License.
 | 
			
		||||
 * You may obtain a copy of the License at
 | 
			
		||||
 *
 | 
			
		||||
 *     http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
 *
 | 
			
		||||
 * Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
 * distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.common.msg;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.core.type.TypeReference;
 | 
			
		||||
import com.fasterxml.jackson.databind.JsonNode;
 | 
			
		||||
import com.fasterxml.jackson.databind.ObjectMapper;
 | 
			
		||||
import org.junit.Before;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
import static org.junit.Assert.assertEquals;
 | 
			
		||||
 | 
			
		||||
public class TbMsgMetaDataTest {
 | 
			
		||||
 | 
			
		||||
    private static final ObjectMapper objectMapper = new ObjectMapper();
 | 
			
		||||
    private final String metadataJsonStr = "{\"deviceName\":\"Test Device\",\"deviceType\":\"default\",\"ts\":\"1645112691407\"}";
 | 
			
		||||
    private JsonNode metadataJson;
 | 
			
		||||
    private Map<String, String> metadataExpected;
 | 
			
		||||
 | 
			
		||||
    @Before
 | 
			
		||||
    public void startInit() throws Exception {
 | 
			
		||||
        metadataJson = objectMapper.readValue(metadataJsonStr, JsonNode.class);
 | 
			
		||||
        metadataExpected = objectMapper.convertValue(metadataJson, new TypeReference<>() {
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testScript_whenMetadataWithoutPropertiesValueNull_returnMetadataWithAllValue() {
 | 
			
		||||
        TbMsgMetaData tbMsgMetaData = new TbMsgMetaData(metadataExpected);
 | 
			
		||||
        Map<String, String> dataActual = tbMsgMetaData.values();
 | 
			
		||||
        assertEquals(metadataExpected.size(), dataActual.size());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testScript_whenMetadataWithPropertiesValueNull_returnMetadataWithoutPropertiesValueEqualsNull() {
 | 
			
		||||
        metadataExpected.put("deviceName", null);
 | 
			
		||||
        TbMsgMetaData tbMsgMetaData = new TbMsgMetaData(metadataExpected);
 | 
			
		||||
        Map<String, String> dataActual = tbMsgMetaData.copy().getData();
 | 
			
		||||
        assertEquals(metadataExpected.size() - 1, dataActual.size());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user