diff --git a/.github/workflows/java_ci.yml b/.github/workflows/java_ci.yml index ebc599c..51b089b 100644 --- a/.github/workflows/java_ci.yml +++ b/.github/workflows/java_ci.yml @@ -18,5 +18,8 @@ jobs: - name: Grant execute permission for gradlew run: chmod +x gradlew + - name: Clean cache + run: ./gradlew clean + - name: Build with Gradle run: ./gradlew build \ No newline at end of file diff --git a/build.gradle b/build.gradle index f5996c0..6d6787c 100644 --- a/build.gradle +++ b/build.gradle @@ -3,6 +3,7 @@ apply plugin: 'java' group = "org.student" version = "1.0-SNAPSHOT" +var showTestLogs = false subprojects { @@ -12,6 +13,21 @@ subprojects { mavenCentral() } + test { + testLogging { + events "passed", "skipped", "failed" + showStandardStreams = showTestLogs + } + + afterTest {desc, result -> + if (result.resultType == TestResult.ResultType.SUCCESS) { + println("Module: ${project.name}, Test result: Passed: ${desc.name}") + } else if (result.resultType == TestResult.ResultType.FAILURE) { + println("Module: ${project.name}, Test result: Failed: ${desc.name}") + } + } + } + dependencies { implementation 'org.apache.logging.log4j:log4j-api:2.14.1' diff --git a/containers-errors/Errors.md b/containers-errors/Errors.md index 54d105b..231e9c8 100644 --- a/containers-errors/Errors.md +++ b/containers-errors/Errors.md @@ -1 +1,4 @@ -org.apache.kafka.common.network.InvalidReceiveException: Invalid receive (size = 1195725856 larger than 104857600) \ No newline at end of file +org.apache.kafka.common.network.InvalidReceiveException: Invalid receive (size = 1195725856 larger than 104857600) + +java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test + \ No newline at end of file diff --git a/core-back-end/build.gradle b/core-back-end/build.gradle index c80a545..9e810d9 100644 --- a/core-back-end/build.gradle +++ b/core-back-end/build.gradle @@ -28,10 +28,10 @@ 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' } -tasks.named('test') { +tasks.test { useJUnitPlatform() -} - +} \ No newline at end of file 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 diff --git a/gradlew b/gradlew old mode 100644 new mode 100755