From 4b8ec146af8b15af79acfa765a01ddc933165451 Mon Sep 17 00:00:00 2001 From: yuluo-yx Date: Wed, 7 Aug 2024 16:04:57 +0800 Subject: [PATCH 1/2] [Improve] add ObsObjectStoreServiceImpl unit test Signed-off-by: yuluo-yx --- .../service/ObsObjectStoreServiceTest.java | 120 ++++++++++++++++++ 1 file changed, 120 insertions(+) diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/service/ObsObjectStoreServiceTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/service/ObsObjectStoreServiceTest.java index f0e6a0e2e67..b9027c78a3a 100644 --- a/manager/src/test/java/org/apache/hertzbeat/manager/service/ObsObjectStoreServiceTest.java +++ b/manager/src/test/java/org/apache/hertzbeat/manager/service/ObsObjectStoreServiceTest.java @@ -17,11 +17,131 @@ package org.apache.hertzbeat.manager.service; +import java.io.InputStream; +import java.util.List; +import com.obs.services.ObsClient; +import com.obs.services.model.ListObjectsRequest; +import com.obs.services.model.ObjectListing; +import com.obs.services.model.PutObjectResult; +import com.obs.services.model.ObsObject; +import org.apache.hertzbeat.manager.pojo.dto.FileDTO; import org.apache.hertzbeat.manager.service.impl.ObsObjectStoreServiceImpl; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; /** * test case for {@link ObsObjectStoreServiceImpl} */ class ObsObjectStoreServiceTest { + + @Mock + private ObsClient obsClient; + + private ObsObjectStoreServiceImpl service; + + private final String bucketName = "test-bucket"; + private final String rootPath = "root/path"; + + @BeforeEach + void setUp() { + + MockitoAnnotations.openMocks(this); + + this.service = new ObsObjectStoreServiceImpl( + obsClient, + bucketName, + rootPath + ); + } + + @Test + void testUpload() { + + String filePath = "file.txt"; + InputStream is = mock(InputStream.class); + var response = mock(PutObjectResult.class); + + when(obsClient.putObject(eq(bucketName), anyString(), eq(is))).thenReturn(response); + when(response.getStatusCode()).thenReturn(200); + + boolean result = service.upload(filePath, is); + + assertTrue(result); + verify(obsClient, times(1)).putObject(eq(bucketName), anyString(), eq(is)); + } + + @Test + void testRemove() { + + String filePath = "file.txt"; + + service.remove(filePath); + + verify(obsClient, times(1)).deleteObject(eq(bucketName), anyString()); + } + + @Test + void testIsExist() { + + String filePath = "file.txt"; + + when(obsClient.doesObjectExist(eq(bucketName), anyString())).thenReturn(true); + + boolean result = service.isExist(filePath); + + assertTrue(result); + verify(obsClient, times(1)).doesObjectExist(eq(bucketName), anyString()); + } + + @Test + void testDownload() { + + String filePath = "file.txt"; + var obsObject = mock(ObsObject.class); + + when(obsClient.getObject(eq(bucketName), anyString())).thenReturn(obsObject); + when(obsObject.getObjectContent()).thenReturn(mock(InputStream.class)); + + FileDTO result = service.download(filePath); + + assertNotNull(result); + assertEquals(filePath, result.getName()); + verify(obsClient, times(1)).getObject(eq(bucketName), anyString()); + } + + @Test + void testList() { + + String dir = "some/dir"; + var listObjectsResponse = mock(ObjectListing.class); + var objectSummary = mock(ObsObject.class); + + when(obsClient.listObjects(any(ListObjectsRequest.class))).thenReturn(listObjectsResponse); + when(listObjectsResponse.getObjects()).thenReturn(List.of(objectSummary)); + when(objectSummary.getObjectKey()).thenReturn("some/dir/file.txt"); + when(objectSummary.getObjectContent()).thenReturn(mock(InputStream.class)); + + List result = service.list(dir); + + assertNotNull(result); + assertFalse(result.isEmpty()); + verify(obsClient, times(1)).listObjects(any(ListObjectsRequest.class)); + } + } From 5a2f17c1a504801a6c0c962f1c795c36b0e49673 Mon Sep 17 00:00:00 2001 From: yuluo-yx Date: Wed, 7 Aug 2024 16:05:58 +0800 Subject: [PATCH 2/2] fix Signed-off-by: yuluo-yx --- .../hertzbeat/manager/service/ObsObjectStoreServiceTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/service/ObsObjectStoreServiceTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/service/ObsObjectStoreServiceTest.java index b9027c78a3a..89c76edb6f5 100644 --- a/manager/src/test/java/org/apache/hertzbeat/manager/service/ObsObjectStoreServiceTest.java +++ b/manager/src/test/java/org/apache/hertzbeat/manager/service/ObsObjectStoreServiceTest.java @@ -28,7 +28,6 @@ import org.apache.hertzbeat.manager.service.impl.ObsObjectStoreServiceImpl; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations;