From 731daf916c96bd6e04841febdead3bf43457e8ce Mon Sep 17 00:00:00 2001 From: Hanna Kurhuzenkava Date: Fri, 26 Apr 2024 23:52:53 +0300 Subject: [PATCH] MAIN-T-121 Default search for Documents Update logic for document retrieval and search pattern Removed logic that retrieves all documents for a user in `DocumentService` and `DocumentFacade`. Simplified search pattern in `SearchSpec` and `DocumentSearchController`. Adjusted the corresponding API interfaces and modified various configuration files. --- server/.run/DokyApplication DEV.run.xml | 23 +++++ server/.run/DokyApplication.run.xml | 23 +++++ server/.run/server [apiTest].run.xml | 34 +++++++ server/.run/server [integrationTest].run.xml | 34 +++++++ server/.run/server [test].run.xml | 90 ++++++++++++++----- .../kotlin/org/hkurh/doky/DocumentSpec.kt | 37 +------- .../kotlin/org/hkurh/doky/SearchSpec.kt | 33 +++++-- .../hkurh/doky/documents/DocumentFacade.kt | 7 -- .../hkurh/doky/documents/DocumentService.kt | 7 -- .../hkurh/doky/documents/api/DocumentApi.kt | 11 ++- .../doky/documents/api/DocumentController.kt | 6 -- .../documents/db/DocumentEntityRepository.kt | 3 - .../documents/impl/DefaultDocumentFacade.kt | 4 - .../documents/impl/DefaultDocumentService.kt | 8 +- .../doky/search/api/DocumentSearchApi.kt | 8 +- .../search/api/DocumentSearchController.kt | 5 +- 16 files changed, 228 insertions(+), 105 deletions(-) create mode 100644 server/.run/DokyApplication DEV.run.xml create mode 100644 server/.run/DokyApplication.run.xml diff --git a/server/.run/DokyApplication DEV.run.xml b/server/.run/DokyApplication DEV.run.xml new file mode 100644 index 00000000..78460a48 --- /dev/null +++ b/server/.run/DokyApplication DEV.run.xml @@ -0,0 +1,23 @@ + + + + diff --git a/server/.run/DokyApplication.run.xml b/server/.run/DokyApplication.run.xml new file mode 100644 index 00000000..2df5aec8 --- /dev/null +++ b/server/.run/DokyApplication.run.xml @@ -0,0 +1,23 @@ + + + + diff --git a/server/.run/server [apiTest].run.xml b/server/.run/server [apiTest].run.xml index a3bf6d3d..d78c413a 100644 --- a/server/.run/server [apiTest].run.xml +++ b/server/.run/server [apiTest].run.xml @@ -33,4 +33,38 @@ false + + + + + + true + true + + + + + false + false + + diff --git a/server/.run/server [integrationTest].run.xml b/server/.run/server [integrationTest].run.xml index 4aec00fd..1ee2eead 100644 --- a/server/.run/server [integrationTest].run.xml +++ b/server/.run/server [integrationTest].run.xml @@ -33,4 +33,38 @@ false + + + + + + true + true + + + + + false + false + + diff --git a/server/.run/server [test].run.xml b/server/.run/server [test].run.xml index 963f500b..a39dae48 100644 --- a/server/.run/server [test].run.xml +++ b/server/.run/server [test].run.xml @@ -1,24 +1,70 @@ - - - - - - true - true - false - false - - + + + + + + true + true + + + + + false + false + + + + + + + + true + true + + + + + false + false + + diff --git a/server/src/apiTest/kotlin/org/hkurh/doky/DocumentSpec.kt b/server/src/apiTest/kotlin/org/hkurh/doky/DocumentSpec.kt index 0461f0cb..4be37c4e 100644 --- a/server/src/apiTest/kotlin/org/hkurh/doky/DocumentSpec.kt +++ b/server/src/apiTest/kotlin/org/hkurh/doky/DocumentSpec.kt @@ -3,17 +3,12 @@ package org.hkurh.doky import io.restassured.RestAssured.given import org.hamcrest.CoreMatchers.notNullValue import org.hkurh.doky.documents.api.DocumentRequest -import org.hkurh.doky.documents.db.DocumentEntityRepository -import org.hkurh.doky.users.db.UserEntityRepository import org.junit.Rule import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Assertions.assertNotNull -import org.junit.jupiter.api.Assertions.assertNull import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test import org.junit.rules.TemporaryFolder -import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Value import org.springframework.http.HttpStatus import org.springframework.test.context.jdbc.Sql @@ -33,28 +28,20 @@ class DocumentSpec : RestSpec() { val documentFileNameProperty = "fileName" val existedDocumentNameFirst = "Test_1" val existedDocumentFileNameFirst = "test.txt" - val existedDocumentNameSecond = "Test_2" val existedDocumentNameThird = "Test_3" - val existedDocumentNameFour = "Test_4" val newDocumentName = "Apples" val newDocumentDescription = "Do you like apples?" val uploadFileName = "test_1.txt" @Value("\${doky.filestorage.path}") - val fileStoragePath: String? = null + lateinit var fileStoragePath: String @Rule var temporaryFolder = TemporaryFolder() - @Autowired - lateinit var documentEntityRepository: DocumentEntityRepository - - @Autowired - lateinit var userEntityRepository: UserEntityRepository - @AfterEach fun tearDown() { - fileStoragePath?.let { + fileStoragePath.let { FileSystemUtils.deleteRecursively(Paths.get(it)) } } @@ -100,26 +87,6 @@ class DocumentSpec : RestSpec() { assertEquals(existedDocumentFileNameFirst, fileName) } - @Test - @DisplayName("Should get all existing documents for user") - @Sql(scripts = ["classpath:sql/DocumentSpec/setup.sql"], executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) - fun shouldGetAllExistingDocumentsForUser() { - // given - val requestSpec = prepareRequestSpecWithLogin().build() - - // when: - val response = given(requestSpec).get(endpoint) - - // then - response.then().statusCode(HttpStatus.OK.value()) - val documents: List> = response.path(".") - - assertNotNull(documents.find { d -> (existedDocumentNameFirst == d[documentNameProperty]) }) - assertNotNull(documents.find { d -> (existedDocumentNameSecond == d[documentNameProperty]) }) - assertNull(documents.find { d -> (existedDocumentNameThird == d[documentNameProperty]) }) - assertNull(documents.find { d -> (existedDocumentNameFour == d[documentNameProperty]) }) - } - @Test @DisplayName("Should return 404 when get existing document that belongs to another user") @Sql(scripts = ["classpath:sql/DocumentSpec/setup.sql"], executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) diff --git a/server/src/apiTest/kotlin/org/hkurh/doky/SearchSpec.kt b/server/src/apiTest/kotlin/org/hkurh/doky/SearchSpec.kt index c4674b91..494cdcd8 100644 --- a/server/src/apiTest/kotlin/org/hkurh/doky/SearchSpec.kt +++ b/server/src/apiTest/kotlin/org/hkurh/doky/SearchSpec.kt @@ -17,10 +17,12 @@ import org.springframework.test.context.jdbc.Sql @DisplayName("Document Search API test") class SearchSpec : RestSpec() { - val endpoint = "/documents/search" + val endpoint = "/documents" val documentNameProperty = "name" - val documentName1 = "Test note 1" - val documentName2 = "Test note 2" + val documentNameFirst = "Test note 1" + val documentNameSecond = "Lorem" + val documentNameThird = "Test note 2" + val documentNameFour = "Cras at nulla ex" @Value("\${doky.search.solr.core.documents:documents-test}") lateinit var coreName: String @@ -35,6 +37,27 @@ class SearchSpec : RestSpec() { documentIndexer.fullIndex() } + @Test + @DisplayName("Should get all existing documents for user") + @Sql(scripts = ["classpath:sql/SearchSpec/setup.sql"], executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) + @Sql(scripts = ["classpath:sql/SearchSpec/cleanup.sql"], executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) + fun shouldGetAllExistingDocumentsForUser() { + // given + val requestSpec = prepareRequestSpecWithLogin().build() + + // when: + val response = given(requestSpec).get(endpoint) + + // then + response.then().statusCode(HttpStatus.OK.value()) + val documents: List> = response.path(".") + + assertNotNull(documents.find { d -> (documentNameFirst == d[documentNameProperty]) }) + assertNotNull(documents.find { d -> (documentNameSecond == d[documentNameProperty]) }) + assertNull(documents.find { d -> (documentNameThird == d[documentNameProperty]) }) + assertNull(documents.find { d -> (documentNameFour == d[documentNameProperty]) }) + } + @Test @DisplayName("Should return only documents that allowed to user") @Sql(scripts = ["classpath:sql/SearchSpec/setup.sql"], executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) @@ -50,8 +73,8 @@ class SearchSpec : RestSpec() { response.then().statusCode(HttpStatus.OK.value()) val documents: List> = response.path(".") - assertNotNull(documents.find { d -> (documentName1 == d[documentNameProperty]) }) - assertNull(documents.find { d -> (documentName2 == d[documentNameProperty]) }) + assertNotNull(documents.find { d -> (documentNameFirst == d[documentNameProperty]) }) + assertNull(documents.find { d -> (documentNameThird == d[documentNameProperty]) }) } @Test diff --git a/server/src/main/kotlin/org/hkurh/doky/documents/DocumentFacade.kt b/server/src/main/kotlin/org/hkurh/doky/documents/DocumentFacade.kt index feb86fc1..5602d74c 100644 --- a/server/src/main/kotlin/org/hkurh/doky/documents/DocumentFacade.kt +++ b/server/src/main/kotlin/org/hkurh/doky/documents/DocumentFacade.kt @@ -37,13 +37,6 @@ interface DocumentFacade { */ fun findDocument(id: String): DocumentResponse? - /** - * Retrieves a list of all documents. - * - * @return A list of [DocumentResponse] objects representing the retrieved documents. The list may contain null values if the document retrieval failed. - */ - fun findAllDocuments(): List - /** * Saves a file and attach it to document with the given ID. * diff --git a/server/src/main/kotlin/org/hkurh/doky/documents/DocumentService.kt b/server/src/main/kotlin/org/hkurh/doky/documents/DocumentService.kt index ed80733c..08b4b6f4 100644 --- a/server/src/main/kotlin/org/hkurh/doky/documents/DocumentService.kt +++ b/server/src/main/kotlin/org/hkurh/doky/documents/DocumentService.kt @@ -23,13 +23,6 @@ interface DocumentService { */ fun find(id: String): DocumentEntity? - /** - * Finds all documents. - * - * @return A list of [DocumentEntity] objects representing the found documents. - */ - fun find(): List - /** * Saves the given document. * diff --git a/server/src/main/kotlin/org/hkurh/doky/documents/api/DocumentApi.kt b/server/src/main/kotlin/org/hkurh/doky/documents/api/DocumentApi.kt index 339fd36a..99fe4ef5 100644 --- a/server/src/main/kotlin/org/hkurh/doky/documents/api/DocumentApi.kt +++ b/server/src/main/kotlin/org/hkurh/doky/documents/api/DocumentApi.kt @@ -2,7 +2,6 @@ package org.hkurh.doky.documents.api import io.swagger.v3.oas.annotations.Operation import io.swagger.v3.oas.annotations.headers.Header -import io.swagger.v3.oas.annotations.media.ArraySchema import io.swagger.v3.oas.annotations.media.Content import io.swagger.v3.oas.annotations.media.Schema import io.swagger.v3.oas.annotations.responses.ApiResponse @@ -42,11 +41,11 @@ interface DocumentApi { @ApiResponses(ApiResponse(responseCode = "200", description = "Document is updated")) fun update(@PathVariable id: String, document: DocumentRequest): ResponseEntity<*>? - @ApiResponses( - ApiResponse(responseCode = "200", description = "List if documents is retrieved successfully", - content = [Content(array = ArraySchema(schema = Schema(implementation = DocumentResponse::class)))])) - @Operation(summary = "Get all documents that was created by current customer") - fun getAll(): ResponseEntity<*>? +// @ApiResponses( +// ApiResponse(responseCode = "200", description = "List if documents is retrieved successfully", +// content = [Content(array = ArraySchema(schema = Schema(implementation = DocumentResponse::class)))])) +// @Operation(summary = "Get all documents that was created by current customer") +// fun getAll(): ResponseEntity<*>? @Operation(summary = "Get metadata for document") @ApiResponses( diff --git a/server/src/main/kotlin/org/hkurh/doky/documents/api/DocumentController.kt b/server/src/main/kotlin/org/hkurh/doky/documents/api/DocumentController.kt index 4bb08d97..4a4619a5 100644 --- a/server/src/main/kotlin/org/hkurh/doky/documents/api/DocumentController.kt +++ b/server/src/main/kotlin/org/hkurh/doky/documents/api/DocumentController.kt @@ -61,12 +61,6 @@ class DocumentController(private val documentFacade: DocumentFacade) : DocumentA return ResponseEntity.ok(null) } - @GetMapping - override fun getAll(): ResponseEntity<*> { - val documents = documentFacade.findAllDocuments() - return ResponseEntity.ok(documents) - } - @GetMapping("/{id}") override fun get(@PathVariable id: String): ResponseEntity<*> { val document = documentFacade.findDocument(id) diff --git a/server/src/main/kotlin/org/hkurh/doky/documents/db/DocumentEntityRepository.kt b/server/src/main/kotlin/org/hkurh/doky/documents/db/DocumentEntityRepository.kt index 27ce5574..1b78fc93 100644 --- a/server/src/main/kotlin/org/hkurh/doky/documents/db/DocumentEntityRepository.kt +++ b/server/src/main/kotlin/org/hkurh/doky/documents/db/DocumentEntityRepository.kt @@ -11,9 +11,6 @@ import java.util.* */ interface DocumentEntityRepository : CrudRepository, JpaSpecificationExecutor { - @Query("select d from DocumentEntity d where d.creator.id = ?1") - fun findByCreatorId(documentId: Long): List - @Query("select d from DocumentEntity d where d.id = ?1 and d.creator.id = ?2") fun findByIdAndCreatorId(documentId: Long, creatorId: Long): DocumentEntity? diff --git a/server/src/main/kotlin/org/hkurh/doky/documents/impl/DefaultDocumentFacade.kt b/server/src/main/kotlin/org/hkurh/doky/documents/impl/DefaultDocumentFacade.kt index 34b8e01b..41c6f64c 100644 --- a/server/src/main/kotlin/org/hkurh/doky/documents/impl/DefaultDocumentFacade.kt +++ b/server/src/main/kotlin/org/hkurh/doky/documents/impl/DefaultDocumentFacade.kt @@ -42,10 +42,6 @@ class DefaultDocumentFacade( return documentService.find(id)?.toDto() } - override fun findAllDocuments(): List { - return documentService.find().map { it.toDto() } - } - @Transactional(propagation = Propagation.REQUIRED) override fun saveFile(file: MultipartFile, id: String) { val document = documentService.find(id) diff --git a/server/src/main/kotlin/org/hkurh/doky/documents/impl/DefaultDocumentService.kt b/server/src/main/kotlin/org/hkurh/doky/documents/impl/DefaultDocumentService.kt index 5cad6b2a..9af38d0b 100644 --- a/server/src/main/kotlin/org/hkurh/doky/documents/impl/DefaultDocumentService.kt +++ b/server/src/main/kotlin/org/hkurh/doky/documents/impl/DefaultDocumentService.kt @@ -11,8 +11,7 @@ import org.springframework.stereotype.Service class DefaultDocumentService( private val documentEntityRepository: DocumentEntityRepository, private val userService: UserService -) : - DocumentService { +) : DocumentService { override fun create(name: String, description: String?): DocumentEntity { val document = DocumentEntity() @@ -31,11 +30,6 @@ class DefaultDocumentService( return documentEntityRepository.findByIdAndCreatorId(documentId, currentUser.id) } - override fun find(): List { - val currentUser = userService.getCurrentUser() - return documentEntityRepository.findByCreatorId(currentUser.id) - } - override fun save(document: DocumentEntity) { documentEntityRepository.save(document) } diff --git a/server/src/main/kotlin/org/hkurh/doky/search/api/DocumentSearchApi.kt b/server/src/main/kotlin/org/hkurh/doky/search/api/DocumentSearchApi.kt index deff39a4..f0d74271 100644 --- a/server/src/main/kotlin/org/hkurh/doky/search/api/DocumentSearchApi.kt +++ b/server/src/main/kotlin/org/hkurh/doky/search/api/DocumentSearchApi.kt @@ -2,7 +2,13 @@ package org.hkurh.doky.search.api import io.swagger.v3.oas.annotations.security.SecurityRequirement import io.swagger.v3.oas.annotations.tags.Tag +import org.springframework.http.ResponseEntity +import org.springframework.web.bind.annotation.GetMapping @Tag(name = "Documents") @SecurityRequirement(name = "Bearer Token") -interface DocumentSearchApi +interface DocumentSearchApi { + + @GetMapping + fun search(q: String? = "*"): ResponseEntity<*> +} diff --git a/server/src/main/kotlin/org/hkurh/doky/search/api/DocumentSearchController.kt b/server/src/main/kotlin/org/hkurh/doky/search/api/DocumentSearchController.kt index fe53896f..9ac51b98 100644 --- a/server/src/main/kotlin/org/hkurh/doky/search/api/DocumentSearchController.kt +++ b/server/src/main/kotlin/org/hkurh/doky/search/api/DocumentSearchController.kt @@ -21,8 +21,9 @@ class DocumentSearchController( private val documentSearchFacade: DocumentSearchFacade ) : DocumentSearchApi { - @GetMapping("/search") - fun search(@RequestParam(name = "q") query: String): ResponseEntity<*> { + @GetMapping + override fun search(@RequestParam q: String?): ResponseEntity<*> { + val query = q?.ifEmpty { "*" } ?: "*" val documents = documentSearchFacade.search(query) return ResponseEntity.ok(documents) }