Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Improve] add ObsObjectStoreServiceImpl unit test #2488

Merged
merged 4 commits into from
Aug 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<FileDTO> result = service.list(dir);

assertNotNull(result);
assertFalse(result.isEmpty());
verify(obsClient, times(1)).listObjects(any(ListObjectsRequest.class));
}

}
Loading