package com.alibaba.nacos.config.server.remote;

import com.alibaba.nacos.api.config.remote.request.ConfigQueryRequest;
import com.alibaba.nacos.api.config.remote.response.ConfigQueryResponse;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.remote.request.RequestMeta;
import com.alibaba.nacos.config.server.model.CacheItem;
import com.alibaba.nacos.config.server.service.ConfigCacheService;
import com.alibaba.nacos.config.server.service.repository.PersistService;
import com.alibaba.nacos.config.server.utils.DiskUtil;
import com.alibaba.nacos.config.server.utils.GroupKey2;
import com.alibaba.nacos.config.server.utils.PropertyUtil;
import com.alibaba.nacos.sys.env.EnvUtil;
import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.util.ReflectionTestUtils;

import java.io.File;
import java.io.IOException;

import static com.alibaba.nacos.api.common.Constants.ENCODE;
import static org.mockito.Mockito.when;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class ConfigQueryRequestHandlerTest {

    @InjectMocks
    private ConfigQueryRequestHandler configQueryRequestHandler;
    @Mock
    private PersistService persistService;
    @Mock
    private File file;


    @Before
    public void init() throws IOException {
        EnvUtil.setEnvironment(new StandardEnvironment());
        Mockito.mockStatic(ConfigCacheService.class);
        Mockito.mockStatic(PropertyUtil.class);
        Mockito.mockStatic(FileUtils.class);
        Mockito.mockStatic(DiskUtil.class);
        when(DiskUtil.targetFile(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(file);
        when(FileUtils.readFileToString(file,ENCODE)).thenReturn("content");
        when(file.exists()).thenReturn(true);
        ReflectionTestUtils.setField(configQueryRequestHandler, "persistService", persistService);
    }
    @Test
    public void testHandle() throws IOException, NacosException {
        ConfigQueryRequest configQueryRequest=new ConfigQueryRequest();
        configQueryRequest.setDataId("dataId");
        configQueryRequest.setGroup("group");
        final String groupKey = GroupKey2
                .getKey(configQueryRequest.getDataId(), configQueryRequest.getGroup(), configQueryRequest.getTenant());
        when(ConfigCacheService.tryReadLock(groupKey)).thenReturn(1);

        CacheItem cacheItem=new CacheItem(groupKey);
        cacheItem.setMd5("1");
        cacheItem.setLastModifiedTs(1L);
        when(ConfigCacheService.getContentCache(Mockito.any())).thenReturn(cacheItem);

        RequestMeta requestMeta = new RequestMeta();
        requestMeta.setClientIp("127.0.0.1");
        ConfigQueryResponse response = configQueryRequestHandler.handle(configQueryRequest, requestMeta);
        Assert.assertEquals(response.getContent(),"content");
    }


}
@@ -1,8 +1,625 @@
+/*%0A * Copyright 1999-2018 Alibaba Group Holding Ltd.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *      http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A%0A
 package 
@@ -2149,16 +2149,17 @@
 andler;%0A
+%0A
     @Moc
@@ -2203,16 +2203,17 @@
 ervice;%0A
+%0A
     @Moc
@@ -2238,17 +2238,16 @@
  file;%0A%0A
-%0A
     @Bef
@@ -2548,228 +2548,8 @@
 s);%0A
-        when(DiskUtil.targetFile(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(file);%0A        when(FileUtils.readFileToString(file,ENCODE)).thenReturn(%22content%22);%0A        when(file.exists()).thenReturn(true);%0A
     
@@ -2649,16 +2649,21 @@
 ;%0A    %7D%0A
+    %0A
     @Tes
@@ -2775,17 +2775,19 @@
 yRequest
-=
+ = 
 new Conf
@@ -3130,16 +3130,236 @@
 urn(1);%0A
+        when(DiskUtil.targetFile(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(file);%0A        when(FileUtils.readFileToString(file, ENCODE)).thenReturn(%22content%22);%0A        when(file.exists()).thenReturn(true);
 %0A       
@@ -3378,17 +3378,19 @@
 acheItem
-=
+ = 
 new Cach
@@ -3821,16 +3821,17 @@
 ntent(),
+ 
 %22content
@@ -3840,11 +3840,14 @@
 ;%0A    %7D%0A
-%0A
+    
 %0A%7D

Add unit tests for class ConfigQueryRequestHandler in nacos 2.0