Repository navigation
Build #106: 支持单独更新 file metadata #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
api/src/main/java/com/ke/bella/files/protocol/UpdateMetadataOps.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.ke.bella.files.protocol; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Data | ||
| @AllArgsConstructor | ||
| @NoArgsConstructor | ||
| public class UpdateMetadataOps { | ||
|
|
||
| private String metadata; | ||
| } |
117 changes: 117 additions & 0 deletions
117
api/src/test/java/com/ke/bella/files/api/FileControllerUpdateMetadataTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| package com.ke.bella.files.api; | ||
|
|
||
| import static org.mockito.ArgumentMatchers.argThat; | ||
| import static org.mockito.ArgumentMatchers.eq; | ||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
|
||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.mockito.Mockito; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; | ||
| import org.springframework.test.util.ReflectionTestUtils; | ||
| import org.springframework.test.web.servlet.MockMvc; | ||
| import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.ke.bella.files.api.interceptor.FileApiResponseAdvice; | ||
| import com.ke.bella.files.protocol.FileOps; | ||
| import com.ke.bella.files.protocol.OpenAIFile; | ||
| import com.ke.bella.files.protocol.Scope; | ||
| import com.ke.bella.files.service.FileService; | ||
| import com.ke.bella.files.service.lock.FileUniquenessLock; | ||
|
|
||
| public class FileControllerUpdateMetadataTest { | ||
|
|
||
| private MockMvc mockMvc; | ||
| private FileService fileService; | ||
|
|
||
| @Before | ||
| public void setup() { | ||
| fileService = Mockito.mock(FileService.class); | ||
| FileController fileController = new FileController(); | ||
| ReflectionTestUtils.setField(fileController, "fileService", fileService); | ||
| ReflectionTestUtils.setField(fileController, "fl", Mockito.mock(FileUniquenessLock.class)); | ||
| mockMvc = MockMvcBuilders.standaloneSetup(fileController) | ||
| .setControllerAdvice(new FileApiResponseAdvice()) | ||
| .setMessageConverters(new MappingJackson2HttpMessageConverter(new ObjectMapper())) | ||
| .build(); | ||
| } | ||
|
|
||
| @Test | ||
| public void updateMetadata_FileSuccess() throws Exception { | ||
| assertSuccessfulUpdate("file-test-u", "file", false); | ||
| } | ||
|
|
||
| @Test | ||
| public void updateMetadata_DirectorySuccess() throws Exception { | ||
| assertSuccessfulUpdate("file-directory-u", "directory", true); | ||
| } | ||
|
|
||
| @Test | ||
| public void updateMetadata_MissingMetadata() throws Exception { | ||
| mockMvc.perform(put("/v1/files/{fileId}/metadata", "file-test-u") | ||
| .contentType(MediaType.APPLICATION_JSON) | ||
| .content("{}")) | ||
| .andExpect(status().isBadRequest()) | ||
| .andExpect(jsonPath("$.error.message").value("metadata is required")); | ||
|
|
||
| verify(fileService, never()).getFile("file-test-u"); | ||
| } | ||
|
|
||
| @Test | ||
| public void updateMetadata_FileNotFound() throws Exception { | ||
| when(fileService.getFile("file-missing-u")).thenReturn(null); | ||
|
|
||
| mockMvc.perform(put("/v1/files/{fileId}/metadata", "file-missing-u") | ||
| .contentType(MediaType.APPLICATION_JSON) | ||
| .content("{\"metadata\":\"{\\\"team\\\":\\\"search\\\"}\"}")) | ||
| .andExpect(status().isNotFound()) | ||
| .andExpect(jsonPath("$.error.message").exists()); | ||
|
|
||
| verify(fileService).getFile("file-missing-u"); | ||
| verify(fileService, never()).updateFile(Mockito.any(FileOps.class), eq(false), eq(Scope.METADATA)); | ||
| } | ||
|
|
||
| private void assertSuccessfulUpdate(String fileId, String nodeType, boolean directory) throws Exception { | ||
| String oldMetadata = "{\"team\":\"old\"}"; | ||
| String newMetadata = "{\"team\":\"search\"}"; | ||
| OpenAIFile existingFile = OpenAIFile.builder() | ||
| .id(fileId) | ||
| .filename("test.txt") | ||
| .metadata(oldMetadata) | ||
| .nodeType(nodeType) | ||
| .isDir(directory) | ||
| .description("unchanged") | ||
| .version(7L) | ||
| .build(); | ||
| OpenAIFile updatedFile = existingFile.toBuilder().metadata(newMetadata).build(); | ||
| when(fileService.getFile(fileId)).thenReturn(existingFile); | ||
| when(fileService.updateFile(argThat(op -> fileId.equals(op.getFileId()) | ||
| && newMetadata.equals(op.getMetadata()) | ||
| && op.getFilename() == null | ||
| && op.getDescription() == null | ||
| && op.getCities() == null | ||
| && op.getTags() == null), eq(false), eq(Scope.METADATA))) | ||
| .thenReturn(updatedFile); | ||
|
|
||
| mockMvc.perform(put("/v1/files/{fileId}/metadata", fileId) | ||
| .contentType(MediaType.APPLICATION_JSON) | ||
| .content("{\"metadata\":\"{\\\"team\\\":\\\"search\\\"}\"}")) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(jsonPath("$.id").value(fileId)) | ||
| .andExpect(jsonPath("$.metadata").value(newMetadata)) | ||
| .andExpect(jsonPath("$.filename").value("test.txt")) | ||
| .andExpect(jsonPath("$.description").value("unchanged")) | ||
| .andExpect(jsonPath("$.version").value(7)); | ||
|
|
||
| verify(fileService).getFile(fileId); | ||
| verify(fileService).updateFile(argThat(op -> fileId.equals(op.getFileId()) | ||
| && newMetadata.equals(op.getMetadata())), eq(false), eq(Scope.METADATA)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
fileIdidentifies a resource node, this check accepts it and the endpoint overwrites its metadata. Resource creation deliberately fixes metadata to{}, and the adjacent description, cities, and tags update handlers reject resources; this also exceeds the stated ordinary-file-and-directory scope. Add the equivalentNodeType.RESOURCEguard before constructing the update operation.Useful? React with 👍 / 👎.