Changes adding Live tests
This commit is contained in:
parent
07b65a6d33
commit
4bae437bd6
@ -1,37 +1,18 @@
|
|||||||
package com.baeldung.cloud.openfeign.fileupload.controller;
|
package com.baeldung.cloud.openfeign.fileupload.controller;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestPart;
|
import org.springframework.web.bind.annotation.RequestPart;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import com.baeldung.cloud.openfeign.fileupload.service.UploadService;
|
import com.baeldung.cloud.openfeign.exception.NotFoundException;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
public class FileController {
|
public class FileController {
|
||||||
|
|
||||||
@Autowired
|
@PostMapping(value = "/upload-error")
|
||||||
private UploadService service;
|
public String uploadFileWithFallback(@RequestPart(value = "file") MultipartFile file) throws NotFoundException {
|
||||||
|
throw new NotFoundException("Not Found!!!");
|
||||||
@PostMapping(value = "/upload-mannual-client")
|
|
||||||
public boolean handleFileUploadWithManualClient(@RequestPart(value = "file") MultipartFile file) {
|
|
||||||
return service.uploadFileWithManualClient(file);
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping(value = "/upload-file")
|
|
||||||
public boolean handleFileUpload(@RequestPart(value = "file") MultipartFile file) {
|
|
||||||
return service.uploadFileWithManualClient(file);
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping(value = "/upload-with-fallbackfactory")
|
|
||||||
public String uploadFileWithFallbackFactory(@RequestPart(value = "file") MultipartFile file) {
|
|
||||||
return service.uploadFileWithFallbackFactory(file);
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping(value = "/upload-with-fallback")
|
|
||||||
public String uploadFileWithFallback(@RequestPart(value = "file") MultipartFile file) {
|
|
||||||
return service.uploadFileWithFallback(file);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -8,8 +8,8 @@ import org.springframework.web.multipart.MultipartFile;
|
|||||||
|
|
||||||
import com.baeldung.cloud.openfeign.fileupload.config.FeignSupportConfig;
|
import com.baeldung.cloud.openfeign.fileupload.config.FeignSupportConfig;
|
||||||
|
|
||||||
@FeignClient(name = "file", url = "http://localhost:8080", configuration = FeignSupportConfig.class, fallback = FileUploadClientWithFallbackImpl.class)
|
@FeignClient(name = "file", url = "http://localhost:8081", configuration = FeignSupportConfig.class, fallback = FileUploadClientWithFallbackImpl.class)
|
||||||
public interface FileUploadClientWithFallBack {
|
public interface FileUploadClientWithFallBack {
|
||||||
@PostMapping(value = "/upload-file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
@PostMapping(value = "/upload-error", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||||
String fileUpload(@RequestPart(value = "file") MultipartFile file);
|
String fileUpload(@RequestPart(value = "file") MultipartFile file);
|
||||||
}
|
}
|
||||||
|
@ -8,8 +8,8 @@ import org.springframework.web.multipart.MultipartFile;
|
|||||||
|
|
||||||
import com.baeldung.cloud.openfeign.fileupload.config.FeignSupportConfig;
|
import com.baeldung.cloud.openfeign.fileupload.config.FeignSupportConfig;
|
||||||
|
|
||||||
@FeignClient(name = "file", url = "http://localhost:8080", configuration = FeignSupportConfig.class, fallbackFactory = FileUploadClientFallbackFactory.class)
|
@FeignClient(name = "file", url = "http://localhost:8081", configuration = FeignSupportConfig.class, fallbackFactory = FileUploadClientFallbackFactory.class)
|
||||||
public interface FileUploadClientWithFallbackFactory {
|
public interface FileUploadClientWithFallbackFactory {
|
||||||
@PostMapping(value = "/upload-file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
@PostMapping(value = "/upload-error", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||||
String fileUpload(@RequestPart(value = "file") MultipartFile file);
|
String fileUpload(@RequestPart(value = "file") MultipartFile file);
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ import feign.Response;
|
|||||||
|
|
||||||
public interface UploadResource {
|
public interface UploadResource {
|
||||||
|
|
||||||
@RequestLine("POST /upload-file")
|
@RequestLine("POST /upload-error")
|
||||||
@Headers("Content-Type: multipart/form-data")
|
@Headers("Content-Type: multipart/form-data")
|
||||||
Response uploadFile(@Param("file") MultipartFile file);
|
Response uploadFile(@Param("file") MultipartFile file);
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ import feign.form.spring.SpringFormEncoder;
|
|||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class UploadService {
|
public class UploadService {
|
||||||
private static final String HTTP_FILE_UPLOAD_URL = "http://localhost:8080";
|
private static final String HTTP_FILE_UPLOAD_URL = "http://localhost:8081";
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private FileUploadClientWithFallbackFactory fileUploadClient;
|
private FileUploadClientWithFallbackFactory fileUploadClient;
|
||||||
|
@ -14,6 +14,7 @@ import org.springframework.mock.web.MockMultipartFile;
|
|||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import com.baeldung.cloud.openfeign.exception.NotFoundException;
|
||||||
import com.baeldung.cloud.openfeign.fileupload.service.UploadService;
|
import com.baeldung.cloud.openfeign.fileupload.service.UploadService;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@ -25,8 +26,19 @@ public class OpenFeignFileUploadLiveTest {
|
|||||||
|
|
||||||
private static String FILE_NAME = "fileupload.txt";
|
private static String FILE_NAME = "fileupload.txt";
|
||||||
|
|
||||||
@Test
|
@Test(expected = NotFoundException.class)
|
||||||
public void whenFeignBuilder_thenFileUploadSuccess() throws IOException {
|
public void whenFileUploadClientFallbackFactory_thenFileUploadError() throws IOException {
|
||||||
|
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
|
||||||
|
File file = new File(classloader.getResource(FILE_NAME).getFile());
|
||||||
|
Assert.assertTrue(file.exists());
|
||||||
|
FileInputStream input = new FileInputStream(file);
|
||||||
|
MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain",
|
||||||
|
IOUtils.toByteArray(input));
|
||||||
|
uploadService.uploadFileWithFallbackFactory(multipartFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = NotFoundException.class)
|
||||||
|
public void whenFileUploadClientFallback_thenFileUploadError() throws IOException {
|
||||||
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
|
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
|
||||||
File file = new File(classloader.getResource(FILE_NAME).getFile());
|
File file = new File(classloader.getResource(FILE_NAME).getFile());
|
||||||
Assert.assertTrue(file.exists());
|
Assert.assertTrue(file.exists());
|
||||||
@ -36,14 +48,14 @@ public class OpenFeignFileUploadLiveTest {
|
|||||||
uploadService.uploadFileWithFallback(multipartFile);
|
uploadService.uploadFileWithFallback(multipartFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = NotFoundException.class)
|
||||||
public void whenAnnotatedFeignClient_thenFileUploadSuccess() throws IOException {
|
public void whenFileUploadWithMannualClient_thenFileUploadError() throws IOException {
|
||||||
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
|
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
|
||||||
File file = new File(classloader.getResource(FILE_NAME).getFile());
|
File file = new File(classloader.getResource(FILE_NAME).getFile());
|
||||||
Assert.assertTrue(file.exists());
|
Assert.assertTrue(file.exists());
|
||||||
FileInputStream input = new FileInputStream(file);
|
FileInputStream input = new FileInputStream(file);
|
||||||
MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain",
|
MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain",
|
||||||
IOUtils.toByteArray(input));
|
IOUtils.toByteArray(input));
|
||||||
uploadService.uploadFileWithFallbackFactory(multipartFile);
|
uploadService.uploadFileWithManualClient(multipartFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user