From 4efa9673278ce82200d47397770d5b3492c3054f Mon Sep 17 00:00:00 2001 From: Siarhei_Kakichau Date: Mon, 8 Jul 2024 12:34:05 +0300 Subject: [PATCH 1/3] #31 core back-end api refactoring --- .../org/student/api/ArtifactsController.java | 22 ++++++++++ .../java/org/student/api/FilesController.java | 26 ------------ .../api/models/ArtifactCreateRequest.java | 42 +++++++++++++++++++ .../api/models/ArtifactCreateResponce.java | 41 ++++++++++++++++++ .../student/api/models/ArtifactMateInfo.java | 38 +++++++++++++++++ .../org/student/api/FilesControllerTest.java | 2 +- 6 files changed, 144 insertions(+), 27 deletions(-) create mode 100644 core-back-end/src/main/java/org/student/api/ArtifactsController.java delete mode 100644 core-back-end/src/main/java/org/student/api/FilesController.java create mode 100644 core-back-end/src/main/java/org/student/api/models/ArtifactCreateRequest.java create mode 100644 core-back-end/src/main/java/org/student/api/models/ArtifactCreateResponce.java create mode 100644 core-back-end/src/main/java/org/student/api/models/ArtifactMateInfo.java diff --git a/core-back-end/src/main/java/org/student/api/ArtifactsController.java b/core-back-end/src/main/java/org/student/api/ArtifactsController.java new file mode 100644 index 0000000..8571e50 --- /dev/null +++ b/core-back-end/src/main/java/org/student/api/ArtifactsController.java @@ -0,0 +1,22 @@ +package org.student.api; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.web.bind.annotation.*; +import org.student.api.models.ArtifactCreateRequest; +import org.student.api.models.ArtifactCreateResponce; +import reactor.core.publisher.Mono; + +@RestController +@RequestMapping("api/v1/artifacts") +public class ArtifactsController { + + private static final Logger logger = LogManager.getLogger(ArtifactsController.class); + + @PostMapping("/upload") + public Mono uploadFile(@RequestBody ArtifactCreateRequest request) { + logger.info("Received artifact: {}", request.toString()); + return Mono.just(new ArtifactCreateResponce()); + } + +} 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 deleted file mode 100644 index f2ecf40..0000000 --- a/core-back-end/src/main/java/org/student/api/FilesController.java +++ /dev/null @@ -1,26 +0,0 @@ -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/java/org/student/api/models/ArtifactCreateRequest.java b/core-back-end/src/main/java/org/student/api/models/ArtifactCreateRequest.java new file mode 100644 index 0000000..3bdbf40 --- /dev/null +++ b/core-back-end/src/main/java/org/student/api/models/ArtifactCreateRequest.java @@ -0,0 +1,42 @@ +package org.student.api.models; + +import java.util.Arrays; + +public class ArtifactCreateRequest { + + private String name; + + private byte[] artifactBody; + + public ArtifactCreateRequest(String name, byte[] artifactBody) { + this.name = name; + this.artifactBody = artifactBody; + } + + public ArtifactCreateRequest() { + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public byte[] getArtifactBody() { + return artifactBody; + } + + public void setArtifactBody(byte[] artifactBody) { + this.artifactBody = artifactBody; + } + + @Override + public String toString() { + return "ArtifactCreateRequest{" + + "name='" + name + '\'' + + ", artifactBody=" + Arrays.toString(artifactBody) + + '}'; + } +} diff --git a/core-back-end/src/main/java/org/student/api/models/ArtifactCreateResponce.java b/core-back-end/src/main/java/org/student/api/models/ArtifactCreateResponce.java new file mode 100644 index 0000000..324bda3 --- /dev/null +++ b/core-back-end/src/main/java/org/student/api/models/ArtifactCreateResponce.java @@ -0,0 +1,41 @@ +package org.student.api.models; + +import java.util.UUID; + +public class ArtifactCreateResponce { + private UUID id; + + private ArtifactMateInfo metaInfo; + + public ArtifactCreateResponce(UUID id, ArtifactMateInfo metaInfo) { + this.id = id; + this.metaInfo = metaInfo; + } + + public ArtifactCreateResponce() { + } + + public UUID getId() { + return id; + } + + public void setId(UUID id) { + this.id = id; + } + + public ArtifactMateInfo getMetaInfo() { + return metaInfo; + } + + public void setMetaInfo(ArtifactMateInfo metaInfo) { + this.metaInfo = metaInfo; + } + + @Override + public String toString() { + return "ArtifactCreateResponce{" + + "id=" + id + + ", metaInfo=" + metaInfo + + '}'; + } +} diff --git a/core-back-end/src/main/java/org/student/api/models/ArtifactMateInfo.java b/core-back-end/src/main/java/org/student/api/models/ArtifactMateInfo.java new file mode 100644 index 0000000..ae5ef93 --- /dev/null +++ b/core-back-end/src/main/java/org/student/api/models/ArtifactMateInfo.java @@ -0,0 +1,38 @@ +package org.student.api.models; + +public class ArtifactMateInfo { + private String artifactName; + private long artifactSize; + + public ArtifactMateInfo(String artifactName, long artifactSize) { + this.artifactName = artifactName; + this.artifactSize = artifactSize; + } + + public ArtifactMateInfo() { + } + + public String getArtifactName() { + return artifactName; + } + + public void setArtifactName(String artifactName) { + this.artifactName = artifactName; + } + + public long getArtifactSize() { + return artifactSize; + } + + public void setArtifactSize(long artifactSize) { + this.artifactSize = artifactSize; + } + + @Override + public String toString() { + return "ArtifactMateInfo{" + + "artifactName='" + artifactName + '\'' + + ", artifactSize=" + artifactSize + + '}'; + } +} 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 index 6108995..626aa80 100644 --- a/core-back-end/src/test/java/org/student/api/FilesControllerTest.java +++ b/core-back-end/src/test/java/org/student/api/FilesControllerTest.java @@ -14,7 +14,7 @@ import org.springframework.web.reactive.function.BodyInserters; @ExtendWith(SpringExtension.class) -@WebFluxTest(FilesController.class) +@WebFluxTest(ArtifactsController.class) public class FilesControllerTest { @Autowired From fa3f004af34a6458b9b16997f8743567691110d0 Mon Sep 17 00:00:00 2001 From: Siarhei_Kakichau Date: Mon, 8 Jul 2024 12:49:41 +0300 Subject: [PATCH 2/3] #31 core back-end api implementation --- .../org/student/api/ArtifactsController.java | 27 +++++------ .../student/api/ArtifactsControllerImpl.java | 45 +++++++++++++++++++ .../api/models/ArtifactLoadResponse.java | 34 ++++++++++++++ ...ateResponce.java => ArtifactResponse.java} | 6 +-- .../org/student/api/FilesControllerTest.java | 2 +- 5 files changed, 97 insertions(+), 17 deletions(-) create mode 100644 core-back-end/src/main/java/org/student/api/ArtifactsControllerImpl.java create mode 100644 core-back-end/src/main/java/org/student/api/models/ArtifactLoadResponse.java rename core-back-end/src/main/java/org/student/api/models/{ArtifactCreateResponce.java => ArtifactResponse.java} (81%) diff --git a/core-back-end/src/main/java/org/student/api/ArtifactsController.java b/core-back-end/src/main/java/org/student/api/ArtifactsController.java index 8571e50..8e2f5e5 100644 --- a/core-back-end/src/main/java/org/student/api/ArtifactsController.java +++ b/core-back-end/src/main/java/org/student/api/ArtifactsController.java @@ -1,22 +1,23 @@ package org.student.api; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.student.api.models.ArtifactCreateRequest; -import org.student.api.models.ArtifactCreateResponce; +import org.student.api.models.ArtifactResponse; import reactor.core.publisher.Mono; -@RestController -@RequestMapping("api/v1/artifacts") -public class ArtifactsController { - - private static final Logger logger = LogManager.getLogger(ArtifactsController.class); +import java.util.List; +import java.util.UUID; +public interface ArtifactsController { @PostMapping("/upload") - public Mono uploadFile(@RequestBody ArtifactCreateRequest request) { - logger.info("Received artifact: {}", request.toString()); - return Mono.just(new ArtifactCreateResponce()); - } + Mono uploadFile(@RequestBody ArtifactCreateRequest request); + + @GetMapping("/getAll") + Mono> getAllFiles(); + @GetMapping("/loadArtifact/{id}") + Mono loadArtifact(@PathVariable UUID id); } diff --git a/core-back-end/src/main/java/org/student/api/ArtifactsControllerImpl.java b/core-back-end/src/main/java/org/student/api/ArtifactsControllerImpl.java new file mode 100644 index 0000000..834ed48 --- /dev/null +++ b/core-back-end/src/main/java/org/student/api/ArtifactsControllerImpl.java @@ -0,0 +1,45 @@ +package org.student.api; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.web.bind.annotation.*; +import org.student.api.models.ArtifactCreateRequest; +import org.student.api.models.ArtifactResponse; +import reactor.core.publisher.Mono; + +import java.util.Collections; +import java.util.List; +import java.util.UUID; + +@RestController +@RequestMapping("api/v1/artifacts") +public class ArtifactsControllerImpl implements ArtifactsController { + + private static final Logger logger = LogManager.getLogger(ArtifactsControllerImpl.class); + + @Override + @PostMapping("/upload") + public Mono uploadFile(@RequestBody ArtifactCreateRequest request) { + logger.info("Received artifact: {}", request.toString()); + return Mono.just(new ArtifactResponse()); + } + + @Override + @GetMapping("/getAll") + public Mono> getAllFiles() { + + List res = Collections.emptyList(); + logger.info("Received {} artifacts info from storage.", res.size()); + + return Mono.just(res); + } + + @Override + @GetMapping("/loadArtifact/{id}") + public Mono loadArtifact(@PathVariable UUID id) { + logger.info("loading artifact with id {}", id); + + return Mono.just(new ArtifactResponse()); + } + +} diff --git a/core-back-end/src/main/java/org/student/api/models/ArtifactLoadResponse.java b/core-back-end/src/main/java/org/student/api/models/ArtifactLoadResponse.java new file mode 100644 index 0000000..abcb78b --- /dev/null +++ b/core-back-end/src/main/java/org/student/api/models/ArtifactLoadResponse.java @@ -0,0 +1,34 @@ +package org.student.api.models; + +import java.util.Arrays; +import java.util.UUID; + +public class ArtifactLoadResponse extends ArtifactResponse{ + + private byte[] artifactBody; + + public ArtifactLoadResponse(byte[] artifactBody) { + this.artifactBody = artifactBody; + } + + public ArtifactLoadResponse(UUID id, ArtifactMateInfo metaInfo, byte[] artifactBody) { + super(id, metaInfo); + this.artifactBody = artifactBody; + } + + + public byte[] getArtifactBody() { + return artifactBody; + } + + public void setArtifactBody(byte[] artifactBody) { + this.artifactBody = artifactBody; + } + + @Override + public String toString() { + return "ArtifactLoadResponse{" + + "artifactBody=" + Arrays.toString(artifactBody) + + "} " + super.toString(); + } +} diff --git a/core-back-end/src/main/java/org/student/api/models/ArtifactCreateResponce.java b/core-back-end/src/main/java/org/student/api/models/ArtifactResponse.java similarity index 81% rename from core-back-end/src/main/java/org/student/api/models/ArtifactCreateResponce.java rename to core-back-end/src/main/java/org/student/api/models/ArtifactResponse.java index 324bda3..dadba95 100644 --- a/core-back-end/src/main/java/org/student/api/models/ArtifactCreateResponce.java +++ b/core-back-end/src/main/java/org/student/api/models/ArtifactResponse.java @@ -2,17 +2,17 @@ import java.util.UUID; -public class ArtifactCreateResponce { +public class ArtifactResponse { private UUID id; private ArtifactMateInfo metaInfo; - public ArtifactCreateResponce(UUID id, ArtifactMateInfo metaInfo) { + public ArtifactResponse(UUID id, ArtifactMateInfo metaInfo) { this.id = id; this.metaInfo = metaInfo; } - public ArtifactCreateResponce() { + public ArtifactResponse() { } public UUID getId() { 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 index 626aa80..1b6a6fd 100644 --- a/core-back-end/src/test/java/org/student/api/FilesControllerTest.java +++ b/core-back-end/src/test/java/org/student/api/FilesControllerTest.java @@ -14,7 +14,7 @@ import org.springframework.web.reactive.function.BodyInserters; @ExtendWith(SpringExtension.class) -@WebFluxTest(ArtifactsController.class) +@WebFluxTest(ArtifactsControllerImpl.class) public class FilesControllerTest { @Autowired From e3e5174d391ce86c53e51c17710363c0dfa2069e Mon Sep 17 00:00:00 2001 From: Siarhei_Kakichau Date: Mon, 8 Jul 2024 13:33:32 +0300 Subject: [PATCH 3/3] #31 test refactoring --- .../org/student/api/ArtifactsController.java | 8 +- .../student/api/ArtifactsControllerImpl.java | 33 ++++--- .../api/models/ArtifactCreateRequest.java | 16 +++ .../api/models/ArtifactLoadResponse.java | 13 +++ .../student/api/models/ArtifactMateInfo.java | 15 +++ .../student/api/models/ArtifactResponse.java | 14 +++ .../student/services/ArtifactsService.java | 21 ++++ .../org/student/api/FilesControllerTest.java | 98 ++++++++++++++++--- 8 files changed, 190 insertions(+), 28 deletions(-) create mode 100644 core-back-end/src/main/java/org/student/services/ArtifactsService.java diff --git a/core-back-end/src/main/java/org/student/api/ArtifactsController.java b/core-back-end/src/main/java/org/student/api/ArtifactsController.java index 8e2f5e5..1106ef3 100644 --- a/core-back-end/src/main/java/org/student/api/ArtifactsController.java +++ b/core-back-end/src/main/java/org/student/api/ArtifactsController.java @@ -5,7 +5,9 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.student.api.models.ArtifactCreateRequest; +import org.student.api.models.ArtifactLoadResponse; import org.student.api.models.ArtifactResponse; +import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import java.util.List; @@ -13,11 +15,11 @@ public interface ArtifactsController { @PostMapping("/upload") - Mono uploadFile(@RequestBody ArtifactCreateRequest request); + Mono uploadArtifact(@RequestBody ArtifactCreateRequest request); @GetMapping("/getAll") - Mono> getAllFiles(); + Flux getAllArtifacts(); @GetMapping("/loadArtifact/{id}") - Mono loadArtifact(@PathVariable UUID id); + Mono loadArtifact(@PathVariable UUID id); } diff --git a/core-back-end/src/main/java/org/student/api/ArtifactsControllerImpl.java b/core-back-end/src/main/java/org/student/api/ArtifactsControllerImpl.java index 834ed48..aab982a 100644 --- a/core-back-end/src/main/java/org/student/api/ArtifactsControllerImpl.java +++ b/core-back-end/src/main/java/org/student/api/ArtifactsControllerImpl.java @@ -4,7 +4,10 @@ import org.apache.logging.log4j.Logger; import org.springframework.web.bind.annotation.*; import org.student.api.models.ArtifactCreateRequest; +import org.student.api.models.ArtifactLoadResponse; import org.student.api.models.ArtifactResponse; +import org.student.services.ArtifactsService; +import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import java.util.Collections; @@ -17,29 +20,33 @@ public class ArtifactsControllerImpl implements ArtifactsController { private static final Logger logger = LogManager.getLogger(ArtifactsControllerImpl.class); + private final ArtifactsService artifactsService; + + public ArtifactsControllerImpl(ArtifactsService artifactsService) { + this.artifactsService = artifactsService; + } + @Override @PostMapping("/upload") - public Mono uploadFile(@RequestBody ArtifactCreateRequest request) { - logger.info("Received artifact: {}", request.toString()); - return Mono.just(new ArtifactResponse()); + public Mono uploadArtifact(@RequestBody ArtifactCreateRequest request) { + + logger.info("Creating artifact from request: {}", request); + + return artifactsService.upload(request).doOnNext(artifactResponse -> logger.info("Created artifact: {}", artifactResponse)); } @Override @GetMapping("/getAll") - public Mono> getAllFiles() { - - List res = Collections.emptyList(); - logger.info("Received {} artifacts info from storage.", res.size()); - - return Mono.just(res); + public Flux getAllArtifacts() { + return artifactsService.getAllArtifacts() + .doOnNext(artifact -> logger.info("Received artifact info from storage: {}", artifact)); } @Override @GetMapping("/loadArtifact/{id}") - public Mono loadArtifact(@PathVariable UUID id) { - logger.info("loading artifact with id {}", id); - - return Mono.just(new ArtifactResponse()); + public Mono loadArtifact(@PathVariable UUID id) { + return artifactsService.getArtifactById(id) + .doOnNext(artifact -> logger.info("Loading artifact with id {}", id)); } } diff --git a/core-back-end/src/main/java/org/student/api/models/ArtifactCreateRequest.java b/core-back-end/src/main/java/org/student/api/models/ArtifactCreateRequest.java index 3bdbf40..40f2398 100644 --- a/core-back-end/src/main/java/org/student/api/models/ArtifactCreateRequest.java +++ b/core-back-end/src/main/java/org/student/api/models/ArtifactCreateRequest.java @@ -1,6 +1,7 @@ package org.student.api.models; import java.util.Arrays; +import java.util.Objects; public class ArtifactCreateRequest { @@ -39,4 +40,19 @@ public String toString() { ", artifactBody=" + Arrays.toString(artifactBody) + '}'; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ArtifactCreateRequest that = (ArtifactCreateRequest) o; + return Objects.equals(name, that.name) && Arrays.equals(artifactBody, that.artifactBody); + } + + @Override + public int hashCode() { + int result = Objects.hash(name); + result = 31 * result + Arrays.hashCode(artifactBody); + return result; + } } diff --git a/core-back-end/src/main/java/org/student/api/models/ArtifactLoadResponse.java b/core-back-end/src/main/java/org/student/api/models/ArtifactLoadResponse.java index abcb78b..1c80beb 100644 --- a/core-back-end/src/main/java/org/student/api/models/ArtifactLoadResponse.java +++ b/core-back-end/src/main/java/org/student/api/models/ArtifactLoadResponse.java @@ -31,4 +31,17 @@ public String toString() { "artifactBody=" + Arrays.toString(artifactBody) + "} " + super.toString(); } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ArtifactLoadResponse that = (ArtifactLoadResponse) o; + return Arrays.equals(artifactBody, that.artifactBody); + } + + @Override + public int hashCode() { + return Arrays.hashCode(artifactBody); + } } diff --git a/core-back-end/src/main/java/org/student/api/models/ArtifactMateInfo.java b/core-back-end/src/main/java/org/student/api/models/ArtifactMateInfo.java index ae5ef93..842340d 100644 --- a/core-back-end/src/main/java/org/student/api/models/ArtifactMateInfo.java +++ b/core-back-end/src/main/java/org/student/api/models/ArtifactMateInfo.java @@ -1,5 +1,7 @@ package org.student.api.models; +import java.util.Objects; + public class ArtifactMateInfo { private String artifactName; private long artifactSize; @@ -35,4 +37,17 @@ public String toString() { ", artifactSize=" + artifactSize + '}'; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ArtifactMateInfo that = (ArtifactMateInfo) o; + return artifactSize == that.artifactSize && Objects.equals(artifactName, that.artifactName); + } + + @Override + public int hashCode() { + return Objects.hash(artifactName, artifactSize); + } } diff --git a/core-back-end/src/main/java/org/student/api/models/ArtifactResponse.java b/core-back-end/src/main/java/org/student/api/models/ArtifactResponse.java index dadba95..6262f6a 100644 --- a/core-back-end/src/main/java/org/student/api/models/ArtifactResponse.java +++ b/core-back-end/src/main/java/org/student/api/models/ArtifactResponse.java @@ -1,5 +1,6 @@ package org.student.api.models; +import java.util.Objects; import java.util.UUID; public class ArtifactResponse { @@ -38,4 +39,17 @@ public String toString() { ", metaInfo=" + metaInfo + '}'; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ArtifactResponse response = (ArtifactResponse) o; + return Objects.equals(id, response.id) && Objects.equals(metaInfo, response.metaInfo); + } + + @Override + public int hashCode() { + return Objects.hash(id, metaInfo); + } } diff --git a/core-back-end/src/main/java/org/student/services/ArtifactsService.java b/core-back-end/src/main/java/org/student/services/ArtifactsService.java new file mode 100644 index 0000000..a80c3c4 --- /dev/null +++ b/core-back-end/src/main/java/org/student/services/ArtifactsService.java @@ -0,0 +1,21 @@ +package org.student.services; + +import org.springframework.stereotype.Service; +import org.student.api.models.ArtifactCreateRequest; +import org.student.api.models.ArtifactLoadResponse; +import org.student.api.models.ArtifactResponse; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import java.util.UUID; + +@Service +public interface ArtifactsService { + + + Mono upload(ArtifactCreateRequest request); + + Flux getAllArtifacts(); + + Mono getArtifactById(UUID id); +} 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 index 1b6a6fd..ea49ba1 100644 --- a/core-back-end/src/test/java/org/student/api/FilesControllerTest.java +++ b/core-back-end/src/test/java/org/student/api/FilesControllerTest.java @@ -2,36 +2,110 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mockito; 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.boot.test.mock.mockito.MockBean; 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; +import org.student.api.models.ArtifactCreateRequest; +import org.student.api.models.ArtifactLoadResponse; +import org.student.api.models.ArtifactMateInfo; +import org.student.api.models.ArtifactResponse; +import org.student.services.ArtifactsService; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import java.util.List; +import java.util.UUID; @ExtendWith(SpringExtension.class) @WebFluxTest(ArtifactsControllerImpl.class) -public class FilesControllerTest { +class FilesControllerTest { @Autowired private WebTestClient webTestClient; + @MockBean + private ArtifactsService artifactsService; + @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())) + void testArtifactUploading() { + + var testBody = new byte[0]; + var testName = "Test name"; + + ArtifactCreateRequest request = new ArtifactCreateRequest(); + request.setArtifactBody(testBody); + request.setName(testName); + + ArtifactMateInfo metaInfo = new ArtifactMateInfo(request.getName(), request.getArtifactBody().length); + UUID id = UUID.randomUUID(); + + ArtifactResponse response = new ArtifactResponse(id, metaInfo); + + Mockito.when(artifactsService.upload(request)).thenReturn(Mono.just(response)); + + webTestClient.mutateWith(SecurityMockServerConfigurers.csrf()).post().uri("http://localhost:8888/api/v1/artifacts/upload") + .contentType(MediaType.APPLICATION_JSON) + .body(BodyInserters.fromValue(request)) .exchange() .expectStatus().isOk() - .expectBody(String.class).isEqualTo("File uploaded successfully."); + .expectBody(ArtifactResponse.class).isEqualTo(response); + + Mockito.verify(artifactsService).upload(request); + } + + @Test + @WithMockUser(username = "user") + void testGetAllMetaInfo() { + + var testBody1 = new byte[0]; + var testName1 = "Test name1"; + + var testBody2 = new byte[0]; + var testName2 = "Test name2"; + + ArtifactMateInfo metaInfo1 = new ArtifactMateInfo(testName1, testBody1.length); + UUID id1 = UUID.randomUUID(); + + ArtifactResponse response1 = new ArtifactResponse(id1, metaInfo1); + ArtifactMateInfo metaInfo2 = new ArtifactMateInfo(testName2, testBody2.length); + UUID id2 = UUID.randomUUID(); + + ArtifactResponse response2 = new ArtifactResponse(id2, metaInfo2); + + Mockito.when(artifactsService.getAllArtifacts()).thenReturn(Flux.just(response1, response2)); + + webTestClient.mutateWith(SecurityMockServerConfigurers.csrf()).get().uri("http://localhost:8888/api/v1/artifacts/getAll") + .exchange() + .expectStatus().isOk() + .expectBodyList(ArtifactResponse.class).isEqualTo(List.of(response1, response2)); + + Mockito.verify(artifactsService).getAllArtifacts(); + } + + @Test + @WithMockUser(username = "user") + void testArtLoading() { + UUID id = UUID.randomUUID(); + String name = "Test name"; + byte[] body = new byte[0]; + + ArtifactLoadResponse response = new ArtifactLoadResponse(id, new ArtifactMateInfo(name, body.length), body); + + Mockito.when(artifactsService.getArtifactById(id)).thenReturn(Mono.just(response)); + + webTestClient.mutateWith(SecurityMockServerConfigurers.csrf()).get().uri("http://localhost:8888/api/v1/artifacts/loadArtifact/" + id) + .exchange() + .expectStatus().isOk() + .expectBody(ArtifactLoadResponse.class).isEqualTo(response); + + Mockito.verify(artifactsService).getArtifactById(id); } }