From 4bae437bd6419f4678b4965ce27eee58446854d5 Mon Sep 17 00:00:00 2001 From: Amitabh Tiwari Date: Mon, 25 Jul 2022 17:16:48 +0530 Subject: [PATCH] Changes adding Live tests --- .../fileupload/controller/FileController.java | 27 +++---------------- .../service/FileUploadClientWithFallBack.java | 4 +-- .../FileUploadClientWithFallbackFactory.java | 4 +-- .../fileupload/service/UploadResource.java | 2 +- .../fileupload/service/UploadService.java | 2 +- .../OpenFeignFileUploadLiveTest.java | 22 +++++++++++---- 6 files changed, 27 insertions(+), 34 deletions(-) diff --git a/spring-cloud-modules/spring-cloud-openfeign-2/src/main/java/com/baeldung/cloud/openfeign/fileupload/controller/FileController.java b/spring-cloud-modules/spring-cloud-openfeign-2/src/main/java/com/baeldung/cloud/openfeign/fileupload/controller/FileController.java index 147c3f5498..abb71e82f9 100644 --- a/spring-cloud-modules/spring-cloud-openfeign-2/src/main/java/com/baeldung/cloud/openfeign/fileupload/controller/FileController.java +++ b/spring-cloud-modules/spring-cloud-openfeign-2/src/main/java/com/baeldung/cloud/openfeign/fileupload/controller/FileController.java @@ -1,37 +1,18 @@ 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.RequestPart; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; -import com.baeldung.cloud.openfeign.fileupload.service.UploadService; +import com.baeldung.cloud.openfeign.exception.NotFoundException; @RestController public class FileController { - @Autowired - private UploadService service; - - @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); + @PostMapping(value = "/upload-error") + public String uploadFileWithFallback(@RequestPart(value = "file") MultipartFile file) throws NotFoundException { + throw new NotFoundException("Not Found!!!"); } } \ No newline at end of file diff --git a/spring-cloud-modules/spring-cloud-openfeign-2/src/main/java/com/baeldung/cloud/openfeign/fileupload/service/FileUploadClientWithFallBack.java b/spring-cloud-modules/spring-cloud-openfeign-2/src/main/java/com/baeldung/cloud/openfeign/fileupload/service/FileUploadClientWithFallBack.java index a7ca3d6147..e842770f2d 100644 --- a/spring-cloud-modules/spring-cloud-openfeign-2/src/main/java/com/baeldung/cloud/openfeign/fileupload/service/FileUploadClientWithFallBack.java +++ b/spring-cloud-modules/spring-cloud-openfeign-2/src/main/java/com/baeldung/cloud/openfeign/fileupload/service/FileUploadClientWithFallBack.java @@ -8,8 +8,8 @@ import org.springframework.web.multipart.MultipartFile; 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 { - @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); } diff --git a/spring-cloud-modules/spring-cloud-openfeign-2/src/main/java/com/baeldung/cloud/openfeign/fileupload/service/FileUploadClientWithFallbackFactory.java b/spring-cloud-modules/spring-cloud-openfeign-2/src/main/java/com/baeldung/cloud/openfeign/fileupload/service/FileUploadClientWithFallbackFactory.java index abf9892c19..829cd0f535 100644 --- a/spring-cloud-modules/spring-cloud-openfeign-2/src/main/java/com/baeldung/cloud/openfeign/fileupload/service/FileUploadClientWithFallbackFactory.java +++ b/spring-cloud-modules/spring-cloud-openfeign-2/src/main/java/com/baeldung/cloud/openfeign/fileupload/service/FileUploadClientWithFallbackFactory.java @@ -8,8 +8,8 @@ import org.springframework.web.multipart.MultipartFile; 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 { - @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); } diff --git a/spring-cloud-modules/spring-cloud-openfeign-2/src/main/java/com/baeldung/cloud/openfeign/fileupload/service/UploadResource.java b/spring-cloud-modules/spring-cloud-openfeign-2/src/main/java/com/baeldung/cloud/openfeign/fileupload/service/UploadResource.java index 26e658a7f0..2d5090897d 100644 --- a/spring-cloud-modules/spring-cloud-openfeign-2/src/main/java/com/baeldung/cloud/openfeign/fileupload/service/UploadResource.java +++ b/spring-cloud-modules/spring-cloud-openfeign-2/src/main/java/com/baeldung/cloud/openfeign/fileupload/service/UploadResource.java @@ -9,7 +9,7 @@ import feign.Response; public interface UploadResource { - @RequestLine("POST /upload-file") + @RequestLine("POST /upload-error") @Headers("Content-Type: multipart/form-data") Response uploadFile(@Param("file") MultipartFile file); diff --git a/spring-cloud-modules/spring-cloud-openfeign-2/src/main/java/com/baeldung/cloud/openfeign/fileupload/service/UploadService.java b/spring-cloud-modules/spring-cloud-openfeign-2/src/main/java/com/baeldung/cloud/openfeign/fileupload/service/UploadService.java index 750d0ff201..244a5a2168 100644 --- a/spring-cloud-modules/spring-cloud-openfeign-2/src/main/java/com/baeldung/cloud/openfeign/fileupload/service/UploadService.java +++ b/spring-cloud-modules/spring-cloud-openfeign-2/src/main/java/com/baeldung/cloud/openfeign/fileupload/service/UploadService.java @@ -10,7 +10,7 @@ import feign.form.spring.SpringFormEncoder; @Service 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 private FileUploadClientWithFallbackFactory fileUploadClient; diff --git a/spring-cloud-modules/spring-cloud-openfeign-2/src/test/java/com/baeldung/cloud/openfeign/OpenFeignFileUploadLiveTest.java b/spring-cloud-modules/spring-cloud-openfeign-2/src/test/java/com/baeldung/cloud/openfeign/OpenFeignFileUploadLiveTest.java index 771660662e..6396be2453 100644 --- a/spring-cloud-modules/spring-cloud-openfeign-2/src/test/java/com/baeldung/cloud/openfeign/OpenFeignFileUploadLiveTest.java +++ b/spring-cloud-modules/spring-cloud-openfeign-2/src/test/java/com/baeldung/cloud/openfeign/OpenFeignFileUploadLiveTest.java @@ -14,6 +14,7 @@ import org.springframework.mock.web.MockMultipartFile; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.web.multipart.MultipartFile; +import com.baeldung.cloud.openfeign.exception.NotFoundException; import com.baeldung.cloud.openfeign.fileupload.service.UploadService; @RunWith(SpringRunner.class) @@ -25,8 +26,19 @@ public class OpenFeignFileUploadLiveTest { private static String FILE_NAME = "fileupload.txt"; - @Test - public void whenFeignBuilder_thenFileUploadSuccess() throws IOException { + @Test(expected = NotFoundException.class) + 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(); File file = new File(classloader.getResource(FILE_NAME).getFile()); Assert.assertTrue(file.exists()); @@ -36,14 +48,14 @@ public class OpenFeignFileUploadLiveTest { uploadService.uploadFileWithFallback(multipartFile); } - @Test - public void whenAnnotatedFeignClient_thenFileUploadSuccess() throws IOException { + @Test(expected = NotFoundException.class) + public void whenFileUploadWithMannualClient_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); + uploadService.uploadFileWithManualClient(multipartFile); } }