diff --git a/sdk/core/azure-core/src/test/java/com/azure/core/util/Base64UtilTests.java b/sdk/core/azure-core/src/test/java/com/azure/core/util/Base64UtilTests.java new file mode 100644 index 000000000000..1e4f2926fb78 --- /dev/null +++ b/sdk/core/azure-core/src/test/java/com/azure/core/util/Base64UtilTests.java @@ -0,0 +1,51 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.core.util; + +import java.util.Arrays; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class Base64UtilTests { + @Test + public void testEncodeAndDecode() { + byte[] src = new byte[] { 65, 65, 69, 67, 65, 119, 81, 70, 66, 103, 99, 73, 67, 81 }; + byte[] dst = Base64Util.encode(src); + assertTrue(Arrays.equals(Base64Util.decode(dst), src)); + } + + @Test + public void testEncodeNullValue() { + assertNull(Base64Util.encode(null)); + } + + @Test + public void testDecodeNullValue() { + assertNull(Base64Util.decode(null)); + } + + @Test + public void testDecodeString() { + byte[] src = new byte[] { 65, 65, 69, 67, 65, 119, 81, 70, 66, 103, 99, 73, 67, 81 }; + String dstString = Base64Util.encodeToString(src); + assertTrue(Arrays.equals(Base64Util.decodeString(dstString), src)); + } + + @Test + public void testDecodeStringNullValue() { + assertNull(Base64Util.decodeString(null)); + } + + @Test + public void testEncodeURLWithoutPaddingNullValue() { + assertNull(Base64Util.encodeURLWithoutPadding(null)); + } + + @Test + public void testDecodeURLNullValue() { + assertNull(Base64Util.decodeURL(null)); + } +} diff --git a/sdk/core/azure-core/src/test/java/com/azure/core/util/FluxUtilTest.java b/sdk/core/azure-core/src/test/java/com/azure/core/util/FluxUtilTest.java index f354d1c5721d..0de295cabec6 100644 --- a/sdk/core/azure-core/src/test/java/com/azure/core/util/FluxUtilTest.java +++ b/sdk/core/azure-core/src/test/java/com/azure/core/util/FluxUtilTest.java @@ -9,9 +9,16 @@ import com.azure.core.http.rest.Response; import com.azure.core.http.rest.SimpleResponse; import com.azure.core.util.logging.ClientLogger; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.nio.ByteBuffer; +import java.nio.channels.AsynchronousFileChannel; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.StandardOpenOption; import org.junit.jupiter.api.Test; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -120,6 +127,24 @@ public void testPageFluxError() { .verifyErrorMessage(errMsg); } + @Test + public void testWriteFile() throws Exception { + String toReplace = "test"; + String original = "hello there"; + String target = "testo there"; + + Flux body = Flux.just(ByteBuffer.wrap(toReplace.getBytes(StandardCharsets.UTF_8))); + File file = createFileIfNotExist("target/test1"); + FileOutputStream stream = new FileOutputStream(file); + stream.write(original.getBytes(StandardCharsets.UTF_8)); + stream.close(); + try (AsynchronousFileChannel channel = AsynchronousFileChannel.open(file.toPath(), StandardOpenOption.WRITE)) { + FluxUtil.writeFile(body, channel).block(); + byte[] outputStream = Files.readAllBytes(file.toPath()); + assertTrue(Arrays.equals(outputStream, target.getBytes(StandardCharsets.UTF_8))); + } + } + public Flux mockReturnType() { return Flux.just(ByteBuffer.wrap(new byte[0])); } @@ -149,4 +174,13 @@ private Flux serviceCallCollection(Context context) { return Flux.just(msg.split(" ")); } + + private File createFileIfNotExist(String fileName) throws IOException { + File file = new File(fileName); + if (file.getParentFile() != null) { + file.getParentFile().mkdirs(); + } + file.createNewFile(); + return file; + } }