刘光辉
3 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
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
package jnpf.fileStorage;
 
import io.minio.BucketExistsArgs;
import io.minio.MinioClient;
import org.dromara.x.file.storage.core.platform.MinioFileStorage;
import org.junit.Test;
 
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
 
public class SeaweedFsBucketVerifierTest {
 
    @Test
    public void acceptsAccessibleBucket() throws Exception {
        MinioFileStorage storage = storageWithBucketResult(true);
 
        new SeaweedFsBucketVerifier().verify(storage);
    }
 
    @Test
    public void rejectsMissingBucket() throws Exception {
        MinioFileStorage storage = storageWithBucketResult(false);
 
        IllegalStateException exception = assertThrows(IllegalStateException.class,
                () -> new SeaweedFsBucketVerifier().verify(storage));
 
        assertTrue(exception.getMessage().contains("jnpf-common"));
    }
 
    @Test
    public void reportsInaccessibleBucket() throws Exception {
        MinioFileStorage storage = mock(MinioFileStorage.class);
        MinioClient client = mock(MinioClient.class);
        when(storage.getBucketName()).thenReturn("jnpf-common");
        when(storage.getClient()).thenReturn(client);
        when(client.bucketExists(any(BucketExistsArgs.class)))
                .thenThrow(new IllegalArgumentException("connection failed"));
 
        IllegalStateException exception = assertThrows(IllegalStateException.class,
                () -> new SeaweedFsBucketVerifier().verify(storage));
 
        assertTrue(exception.getMessage().contains("无法访问"));
        assertTrue(exception.getCause() instanceof IllegalArgumentException);
    }
 
    private MinioFileStorage storageWithBucketResult(boolean exists) throws Exception {
        MinioFileStorage storage = mock(MinioFileStorage.class);
        MinioClient client = mock(MinioClient.class);
        when(storage.getBucketName()).thenReturn("jnpf-common");
        when(storage.getClient()).thenReturn(client);
        when(client.bucketExists(any(BucketExistsArgs.class))).thenReturn(exists);
        return storage;
    }
}