Skip to content

Commit

Permalink
uploading files #1
Browse files Browse the repository at this point in the history
  • Loading branch information
Kasean committed Jan 5, 2024
1 parent 48f22c1 commit 7ae7fe7
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 21 deletions.
1 change: 1 addition & 0 deletions core-back-end/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}

Expand Down
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"

0 comments on commit 7ae7fe7

Please sign in to comment.