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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| import cn.hutool.core.io.IoUtil; import cn.hutool.core.net.url.UrlBuilder; import cn.hutool.core.net.url.UrlQuery; import cn.hutool.core.util.URLUtil; import cn.hutool.http.ContentType; import cn.hutool.http.HttpUtil; import cn.hutool.http.Method; import jakarta.annotation.Resource; import lombok.SneakyThrows; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest;
import java.io.FileNotFoundException; import java.util.List;
@SpringBootTest class OssServiceTest {
@Resource private IOssService ossService;
@Test @SneakyThrows void getUploadSTSAndDeleteBatchTest() { var presignedUrl = ossService.getPresignedUrlPut("test", "test.txt"); Assertions.assertNotNull(presignedUrl); var content = "Hello, Minio! Again!"; try (var response = HttpUtil.createRequest(Method.PUT, presignedUrl) .body(content, "application/octet-stream").execute()) { Assertions.assertTrue(response.isOk()); } var urlBuilder = UrlBuilder.of(presignedUrl).setQuery(new UrlQuery()); Assertions.assertEquals(content, IoUtil.readUtf8(urlBuilder.toURL().openStream())); ossService.deleteBatch(List.of(urlBuilder.toString())); Assertions.assertThrows(FileNotFoundException.class, () -> IoUtil.readUtf8(urlBuilder.toURL().openStream())); }
@Test void greenNormalTest() { var url = [foo]; var bytes = IoUtil.readBytes(URLUtil.getStream(URLUtil.url(url))); var preUrl = ossService.getPresignedUrlPut("test", "normal.jpg"); try (var response = HttpUtil.createRequest(Method.PUT, preUrl).contentType(ContentType.OCTET_STREAM.getValue()).body(bytes).execute()) { Assertions.assertTrue(response.isOk()); var urlBuilder = UrlBuilder.of(preUrl).setQuery(new UrlQuery()); Assertions.assertEquals(bytes.length, IoUtil.readBytes(URLUtil.getStream(urlBuilder.toURL())).length); var imgUrl = urlBuilder.toURL().toString(); var greenVO = ossService.green(imgUrl); Assertions.assertFalse(greenVO.getIsViolation()); Assertions.assertThrows(FileNotFoundException.class, () -> IoUtil.readUtf8(urlBuilder.toURL().openStream()));
} }
@Test void greenYellowTest() { var url = [bar]; var bytes = IoUtil.readBytes(URLUtil.getStream(URLUtil.url(url))); var preUrl = ossService.getPresignedUrlPut("test", "normal.jpg"); try (var response = HttpUtil.createRequest(Method.PUT, preUrl).body(bytes).execute()) { Assertions.assertTrue(response.isOk()); var urlBuilder = UrlBuilder.of(preUrl).setQuery(new UrlQuery()); Assertions.assertEquals(bytes.length, IoUtil.readBytes(URLUtil.getStream(urlBuilder.toURL())).length); var imgUrl = urlBuilder.toURL().toString(); var greenVO = ossService.green(imgUrl); Assertions.assertTrue(greenVO.getIsViolation()); var readBytes = IoUtil.readBytes(URLUtil.getStream(urlBuilder.toURL())); Assertions.assertEquals(readBytes.length, bytes.length); } }
}
|