Skip to content

Commit

Permalink
Merge pull request #24 from Kasean/1-uploading-files-to-core-back-end…
Browse files Browse the repository at this point in the history
…-module

1 uploading files to core back end module
  • Loading branch information
Kasean authored Jan 5, 2024
2 parents 4ecc3d9 + 33e0aee commit f97b2af
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 25 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/java_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 16 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ apply plugin: 'java'
group = "org.student"
version = "1.0-SNAPSHOT"

var showTestLogs = false

subprojects {

Expand All @@ -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'
Expand Down
5 changes: 4 additions & 1 deletion containers-errors/Errors.md
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
org.apache.kafka.common.network.InvalidReceiveException: Invalid receive (size = 1195725856 larger than 104857600)
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

6 changes: 3 additions & 3 deletions core-back-end/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

}
6 changes: 0 additions & 6 deletions core-back-end/src/main/java/org/student/CoreApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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";
}
}
26 changes: 26 additions & 0 deletions core-back-end/src/main/java/org/student/api/FilesController.java
Original file line number Diff line number Diff line change
@@ -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<String> uploadFile(@RequestPart("file") Mono<FilePart> filePartMono) {
return filePartMono.doOnNext(fp -> logger.info("Received file: " + fp.filename())).
then(Mono.just("File uploaded successfully."));
}

}
15 changes: 0 additions & 15 deletions core-back-end/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.");
}
}
2 changes: 2 additions & 0 deletions core-back-end/src/test/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
server:
port: 8888
1 change: 1 addition & 0 deletions core-back-end/src/test/resources/testFile.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"test,test"
Empty file modified gradlew
100644 → 100755
Empty file.

0 comments on commit f97b2af

Please sign in to comment.