/*
 *  Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 *   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 com.alibaba.nacos.naming.consistency.persistent.impl;

import com.alibaba.nacos.consistency.snapshot.Reader;
import com.alibaba.nacos.consistency.snapshot.Writer;
import com.alibaba.nacos.core.distributed.raft.RaftConfig;
import com.alibaba.nacos.core.distributed.raft.utils.RaftExecutor;
import com.alibaba.nacos.core.storage.kv.KvStorage;
import com.alibaba.nacos.sys.env.EnvUtil;
import com.alibaba.nacos.sys.utils.DiskUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.springframework.mock.env.MockEnvironment;

import java.nio.file.Paths;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class NamingSnapshotOperationTest {
    
    static {
        RaftExecutor.init(new RaftConfig());
        EnvUtil.setEnvironment(new MockEnvironment());
    }
    
    @Mock
    private KvStorage storage;
    
    private final String tmpDir = Paths.get(EnvUtil.getNacosTmpDir(), "rocks_test").toString();
    
    private final String snapshotDir = Paths.get(EnvUtil.getNacosTmpDir(), "rocks_snapshot_test").toString();
    
    private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    
    @Before
    public void init() {
        DiskUtils.deleteQuietly(Paths.get(EnvUtil.getNacosTmpDir()));
    }
    
    @After
    public void after() {
        storage.shutdown();
        DiskUtils.deleteQuietly(Paths.get(EnvUtil.getNacosTmpDir()));
    }
    
    @Test
    public void testNamingSnapshot() throws InterruptedException {
        AtomicBoolean result = new AtomicBoolean(false);
        NamingSnapshotOperation operation = new NamingSnapshotOperation(storage, lock);
        final Writer writer = new Writer(snapshotDir);
        final CountDownLatch latch = new CountDownLatch(1);
        
        operation.onSnapshotSave(writer, (isOk, throwable) -> {
            result.set(isOk && throwable == null);
            latch.countDown();
        });
        latch.await(10, TimeUnit.SECONDS);
        Assert.assertTrue(result.get());
        
        final Reader reader = new Reader(snapshotDir, writer.listFiles());
        boolean res = operation.onSnapshotLoad(reader);
        Assert.assertTrue(res);
    }
    
}

@@ -1024,54 +1024,8 @@
 il;%0A
-import com.alibaba.nacos.sys.utils.DiskUtils;%0A
 impo
@@ -1132,20 +1132,98 @@
 org.
-mockito.Mock
+junit.runner.RunWith;%0Aimport org.mockito.Mock;%0Aimport org.mockito.junit.MockitoJUnitRunner
 ;%0Aim
@@ -1493,16 +1493,144 @@
 eLock;%0A%0A
+import static org.mockito.ArgumentMatchers.any;%0Aimport static org.mockito.Mockito.doAnswer;%0A%0A@RunWith(MockitoJUnitRunner.class)%0A
 public c
@@ -1843,109 +1843,8 @@
    %0A
-    private final String tmpDir = Paths.get(EnvUtil.getNacosTmpDir(), %22rocks_test%22).toString();%0A    %0A
     
@@ -2045,107 +2045,254 @@
     
-@Before%0A    public void init() %7B%0A        DiskUtils.deleteQuietly(Paths.get(EnvUtil.getNacosTmpDir()
+private boolean isSnapshoted = false;%0A    %0A    @Before%0A    public void init() throws Exception %7B%0A        doAnswer(invocationOnMock -%3E %7B%0A            isSnapshoted = true;%0A            return null;%0A        %7D).when(storage).doSnapshot(any(String.class
 ));%0A
@@ -2371,78 +2371,8 @@
 ();%0A
-        DiskUtils.deleteQuietly(Paths.get(EnvUtil.getNacosTmpDir()));%0A
     
@@ -2925,16 +2925,57 @@
 CONDS);%0A
+        Assert.assertTrue(isSnapshoted);%0A
         

Fix Unit test for NamingSnapshotOperationTest.java (#5550)