package com.zsc.edu.gateway.service.iot;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zsc.edu.gateway.domain.iot.PropertyBuilder;
import com.zsc.edu.gateway.modules.iot.tsl.dto.PropertyDto;
import com.zsc.edu.gateway.modules.iot.tsl.entity.DataType;
import com.zsc.edu.gateway.modules.iot.tsl.entity.Property;
import com.zsc.edu.gateway.modules.iot.tsl.repo.PropertyRepository;
import com.zsc.edu.gateway.modules.iot.tsl.service.PropertyService;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
public class PropertyServiceTest {
    @Autowired
    private PropertyService service;
    @Autowired
    private PropertyRepository repo;

    private Property property1;
    private Property property2;

    @BeforeEach
    void setUp() {
        property1 = PropertyBuilder.builder().setName("PROPERTY_NAME_1").setProductId(121L)
                .setIoType(Property.IoType.READ_WRITE).setDataType(DataType.DATE).build();
        repo.insert(property1);
        property2 = PropertyBuilder.builder().setName("PROPERTY_NAME_2").setProductId(121L)
                .setIoType(Property.IoType.READ_WRITE).setDataType(DataType.DATE).build();
        repo.insert(property2);
    }

    @AfterEach
    void tearDown() {
        repo.delete(new LambdaQueryWrapper<Property>()
                .in(Property::getName, "PROPERTY_NAME_1", "PROPERTY_NAME_2", "PROPERTY_3", "PROPERTY_UPDATE"));
    }

    @Test
    void list() {
        LambdaQueryWrapper<Property> queryWrapper = new LambdaQueryWrapper<>();
        assertEquals(2, service.list(queryWrapper.like(Property::getName, "PROPERTY")).size());
        assertEquals(1, service.list(queryWrapper.eq(Property::getName, property1.getName())).size());
//        assertEquals(2, service.list().size());
    }

    @Test
    void createProduct() {
        PropertyDto dto = new PropertyDto(121L, "PROPERTY_3", "测试3", "测试", null, null);
        Property property = service.create(dto);
        assertNotNull(property);
        List<Property> properties = service.list(new LambdaQueryWrapper<Property>().like(Property::getName, "PROPERTY"));
        assertEquals(3, properties.size());
    }

    @Test
    void updateProduct() {
        PropertyDto dto = new PropertyDto();
        dto.setName("PROPERTY_UPDATE");
        dto.setIdentifier("测试更新");
        dto.setProductId(121L);
        service.update(dto, property2.getId());
        Property updatedProperty = repo.selectOne(new LambdaQueryWrapper<Property>().eq(Property::getName, dto.getName()));
        assertNotNull(updatedProperty);
        assertEquals("PROPERTY_UPDATE", updatedProperty.getName());
        assertEquals(updatedProperty.getId(), updatedProperty.getId());
    }
}
