Skip to content
Merged
Show file tree
Hide file tree
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
@@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ByteBuffer> 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<ByteBuffer> mockReturnType() {
return Flux.just(ByteBuffer.wrap(new byte[0]));
}
Expand Down Expand Up @@ -149,4 +174,13 @@ private Flux<String> 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;
}
}