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..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 @@ -17,11 +17,130 @@ 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.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)); + } + }