diff --git a/core-back-end/build.gradle b/core-back-end/build.gradle index c80a545..5fbb513 100644 --- a/core-back-end/build.gradle +++ b/core-back-end/build.gradle @@ -28,6 +28,7 @@ dependencies { // implementation 'org.keycloak:keycloak-admin-client:23.0.3' implementation 'org.springframework.kafka:spring-kafka' testImplementation 'org.springframework.boot:spring-boot-starter-test' + testImplementation 'org.springframework.security:spring-security-test' testImplementation 'io.projectreactor:reactor-test' } diff --git a/core-back-end/src/main/java/org/student/CoreApplication.java b/core-back-end/src/main/java/org/student/CoreApplication.java index 16f6908..87ed4ad 100644 --- a/core-back-end/src/main/java/org/student/CoreApplication.java +++ b/core-back-end/src/main/java/org/student/CoreApplication.java @@ -5,7 +5,6 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; @SpringBootApplication @Controller @@ -17,9 +16,4 @@ public static void main(String[] args) { SpringApplication.run(CoreApplication.class, args); logger.info("[CORE MODULE] started."); } - - @RequestMapping("/") - public String home() { - return "redirect:/admin"; - } } \ No newline at end of file diff --git a/core-back-end/src/main/java/org/student/api/FilesController.java b/core-back-end/src/main/java/org/student/api/FilesController.java new file mode 100644 index 0000000..f2ecf40 --- /dev/null +++ b/core-back-end/src/main/java/org/student/api/FilesController.java @@ -0,0 +1,26 @@ +package org.student.api; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.http.HttpStatus; +import org.springframework.http.codec.multipart.FilePart; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestPart; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; +import reactor.core.publisher.Mono; + +@RestController +@RequestMapping("api/v1/files") +public class FilesController { + + private static final Logger logger = LogManager.getLogger(FilesController.class); + + @PostMapping("/upload") + public Mono uploadFile(@RequestPart("file") Mono filePartMono) { + return filePartMono.doOnNext(fp -> logger.info("Received file: " + fp.filename())). + then(Mono.just("File uploaded successfully.")); + } + +} diff --git a/core-back-end/src/main/resources/application.yaml b/core-back-end/src/main/resources/application.yaml index 079f1c7..902cc5c 100644 --- a/core-back-end/src/main/resources/application.yaml +++ b/core-back-end/src/main/resources/application.yaml @@ -1,18 +1,3 @@ -keycloak: - auth-server-url: http://localhost:8080/auth - realm: myrealm - resource: myclient - credentials: - secret: myclientsecret - security-constraints: - - securityCollections: - - name: secured area - authRoles: - - user - patterns: - - /secured/* - cors: true - spring: kafka: bootstrap-servers: localhost:9092 diff --git a/core-back-end/src/test/java/org/student/api/FilesControllerTest.java b/core-back-end/src/test/java/org/student/api/FilesControllerTest.java new file mode 100644 index 0000000..6108995 --- /dev/null +++ b/core-back-end/src/test/java/org/student/api/FilesControllerTest.java @@ -0,0 +1,37 @@ +package org.student.api; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest; +import org.springframework.core.io.ClassPathResource; +import org.springframework.http.MediaType; +import org.springframework.http.client.MultipartBodyBuilder; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.reactive.server.WebTestClient; +import org.springframework.web.reactive.function.BodyInserters; + +@ExtendWith(SpringExtension.class) +@WebFluxTest(FilesController.class) +public class FilesControllerTest { + + @Autowired + private WebTestClient webTestClient; + + @Test + @WithMockUser(username = "user") + public void testFileUploading(){ + ClassPathResource resource = new ClassPathResource("testFile.csv"); + MultipartBodyBuilder builder = new MultipartBodyBuilder(); + builder.part("file", resource); + + webTestClient.mutateWith(SecurityMockServerConfigurers.csrf()).post().uri("http://localhost:8888/api/v1/files/upload") + .contentType(MediaType.MULTIPART_FORM_DATA) + .body(BodyInserters.fromMultipartData(builder.build())) + .exchange() + .expectStatus().isOk() + .expectBody(String.class).isEqualTo("File uploaded successfully."); + } +} diff --git a/core-back-end/src/test/resources/application.yaml b/core-back-end/src/test/resources/application.yaml new file mode 100644 index 0000000..84c6fb1 --- /dev/null +++ b/core-back-end/src/test/resources/application.yaml @@ -0,0 +1,2 @@ +server: + port: 8888 \ No newline at end of file diff --git a/core-back-end/src/test/resources/testFile.csv b/core-back-end/src/test/resources/testFile.csv new file mode 100644 index 0000000..b20618a --- /dev/null +++ b/core-back-end/src/test/resources/testFile.csv @@ -0,0 +1 @@ +"test,test" \ No newline at end of file