BAEL-5942 Code changes (#13147)
* BAEL-5942 Code changes * BAEL-5942 Code changes * BAEL-5942 Code updates from Review * BAEL-5942 Code updates from Review * BAEL-5942 Code updates from Review * BAEL-5942 Code updates from Review * BAEL-5942 Code updates from Review * BAEL-5942 Code updates from Review * BAEL-5942 Test updates from Review * BAEL-5942 Test updates from Review * BAEL-5942 Test updates from Review
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.file;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
public class CustomMultipartFile implements MultipartFile {
|
||||
private byte[] input;
|
||||
|
||||
public CustomMultipartFile(byte[] input) {
|
||||
this.input = input;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getOriginalFilename() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getContentType() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return input == null || input.length == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getSize() {
|
||||
return input.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getBytes() throws IOException {
|
||||
return input;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return new ByteArrayInputStream(input);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transferTo(File destination) throws IOException, IllegalStateException {
|
||||
try(FileOutputStream fos = new FileOutputStream(destination)) {
|
||||
fos.write(input);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.file;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
|
||||
class CustomMultipartFileUnitTest {
|
||||
|
||||
@Test
|
||||
void whenProvidingByteArray_thenMultipartFileCreated() throws IOException {
|
||||
byte[] inputArray = "Test String".getBytes();
|
||||
CustomMultipartFile customMultipartFile = new CustomMultipartFile(inputArray);
|
||||
Assertions.assertFalse(customMultipartFile.isEmpty());
|
||||
Assertions.assertArrayEquals(inputArray, customMultipartFile.getBytes());
|
||||
Assertions.assertEquals(inputArray.length, customMultipartFile.getSize());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenProvidingEmptyByteArray_thenMockMultipartFileIsEmpty() throws IOException {
|
||||
byte[] inputArray = "".getBytes();
|
||||
MockMultipartFile mockMultipartFile = new MockMultipartFile("tempFileName", inputArray);
|
||||
Assertions.assertTrue(mockMultipartFile.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenProvidingNullByteArray_thenMockMultipartFileIsEmpty() throws IOException {
|
||||
byte[] inputArray = null;
|
||||
MockMultipartFile mockMultipartFile = new MockMultipartFile("tempFileName", inputArray);
|
||||
Assertions.assertTrue(mockMultipartFile.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenProvidingByteArray_thenMultipartFileInputSizeMatches() throws IOException {
|
||||
byte[] inputArray = "Testing String".getBytes();
|
||||
CustomMultipartFile customMultipartFile = new CustomMultipartFile(inputArray);
|
||||
Assertions.assertEquals(inputArray.length, customMultipartFile.getSize());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenProvidingByteArray_thenMockMultipartFileCreated() throws IOException {
|
||||
byte[] inputArray = "Test String".getBytes();
|
||||
MockMultipartFile mockMultipartFile = new MockMultipartFile("tempFileName", inputArray);
|
||||
Assertions.assertFalse(mockMultipartFile.isEmpty());
|
||||
Assertions.assertArrayEquals(inputArray, mockMultipartFile.getBytes());
|
||||
Assertions.assertEquals(inputArray.length, mockMultipartFile.getSize());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user