Skip to content

Commit 30c4102

Browse files
committed
testad
1 parent 0762f18 commit 30c4102

4 files changed

Lines changed: 99 additions & 4 deletions

File tree

src/main/java/com/example/lostnfound/service/ImageService.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public class ImageService {
2727
private final Path uploadPath;
2828
private final EmbeddingService embeddingService;
2929

30+
@Value("${app.baseUrl}")
31+
private String baseUrl;
32+
3033
public ImageService(ImageRepository imageRepository, @Value("${app.upload.dir}") String uploadDir, EmbeddingService embeddingService) {
3134
this.imageRepository = imageRepository;
3235
this.uploadPath = Path.of(uploadDir);
@@ -42,13 +45,15 @@ private void createUploadDirectory() {
4245
}
4346
}
4447

45-
@Value("${app.baseUrl}")
46-
private String baseUrl;
4748
public String getImageUri(Long id) {
4849
return baseUrl + "/images/" + id;
4950
}
5051

5152
public Image saveImage(MultipartFile file) throws IOException, InterruptedException {
53+
if (file.isEmpty()) {
54+
throw new IllegalArgumentException("File must not be empty");
55+
}
56+
5257
String originalName = Objects.requireNonNull(file.getOriginalFilename(), "File must have a name");
5358

5459
String extension = "";
@@ -117,6 +122,13 @@ public List<Image> getAllImages() {
117122
}
118123

119124
float similarityScore(float[] embedding1, float[] embedding2) {
125+
Objects.requireNonNull(embedding1, "First embedding must not be null");
126+
Objects.requireNonNull(embedding2, "Second embedding must not be null");
127+
128+
if (embedding1.length != embedding2.length) {
129+
throw new IllegalArgumentException("Embeddings must have the same length");
130+
}
131+
120132
float score = 0;
121133
for (int i = 0; i < embedding1.length; i++) {
122134
score += (float) Math.pow(embedding1[i] - embedding2[i], 2);

src/main/java/com/example/lostnfound/service/PostService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ public List<Post> findTopKSimilarPosts(float[] embed, Long topK) {
116116

117117
public List<Post> getCustomizedPosts() throws UserNotFoundException {
118118
User currentUser = userService.getCurrentUser();
119+
if (currentUser.getEmbedding() == null) {
120+
throw new IllegalStateException("User has no embedding data");
121+
}
119122
return findTopKSimilarPosts(currentUser.getEmbedding(), Long.MAX_VALUE);
120123
}
121124

src/test/java/com/example/lostnfound/service/ImageServiceTest.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.example.lostnfound.service;
22

33
import com.example.lostnfound.exception.ImageNotFoundException;
4-
54
import com.example.lostnfound.exception.ImageStorageException;
6-
75
import com.example.lostnfound.model.Image;
86
import com.example.lostnfound.repository.ImageRepository;
97
import com.example.lostnfound.service.ai.embedding.EmbeddingService;
@@ -16,6 +14,7 @@
1614
import org.springframework.core.io.Resource;
1715
import org.springframework.core.io.UrlResource;
1816
import org.springframework.mock.web.MockMultipartFile;
17+
import org.springframework.test.util.ReflectionTestUtils;
1918

2019
import java.io.IOException;
2120
import java.io.InputStream;
@@ -183,4 +182,43 @@ void testGetAllImages() {
183182
assertEquals(2, result.size());
184183
assertSame(images, result);
185184
}
185+
186+
@Test
187+
void testSaveImage_EmptyFile() {
188+
MockMultipartFile emptyFile = new MockMultipartFile("file", "", "image/jpeg", new byte[0]);
189+
assertThrows(IllegalArgumentException.class, () -> imageService.saveImage(emptyFile));
190+
}
191+
192+
@Test
193+
void testSaveImage_NullFilename() {
194+
MockMultipartFile nullNameFile = new MockMultipartFile("file", null, "image/jpeg", "test".getBytes());
195+
assertThrows(IllegalArgumentException.class, () -> imageService.saveImage(nullNameFile));
196+
}
197+
198+
@Test
199+
void testGetImageUri() {
200+
ReflectionTestUtils.setField(imageService, "baseUrl", "http://localhost:8080");
201+
String uri = imageService.getImageUri(1L);
202+
assertEquals("http://localhost:8080/images/1", uri);
203+
}
204+
205+
@Test
206+
void testSimilarityScore_ZeroDistance() {
207+
float[] embedding1 = {1.0f, 2.0f, 3.0f};
208+
float[] embedding2 = {1.0f, 2.0f, 3.0f};
209+
assertEquals(0.0f, imageService.similarityScore(embedding1, embedding2), 0.0001f);
210+
}
211+
212+
@Test
213+
void testSimilarityScore_NullEmbeddings() {
214+
assertThrows(NullPointerException.class, () -> imageService.similarityScore(null, new float[]{1.0f}));
215+
assertThrows(NullPointerException.class, () -> imageService.similarityScore(new float[]{1.0f}, null));
216+
}
217+
218+
@Test
219+
void testSimilarityScore_DifferentLengths() {
220+
float[] embedding1 = {1.0f, 2.0f};
221+
float[] embedding2 = {1.0f, 2.0f, 3.0f};
222+
assertThrows(IllegalArgumentException.class, () -> imageService.similarityScore(embedding1, embedding2));
223+
}
186224
}

src/test/java/com/example/lostnfound/service/PostServiceTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,47 @@ void testGetCustomizedPosts() throws UserNotFoundException {
157157
verify(postRepo).findTopKSimilarPosts(any(float[].class), eq(Long.MAX_VALUE));
158158
}
159159

160+
@Test
161+
void testUpdatePost_PartialUpdate() throws PostNotFoundException {
162+
Post existingPost = new Post();
163+
existingPost.setId(1L);
164+
existingPost.setTitle("Old Title");
165+
existingPost.setDescription("Old Description");
166+
167+
Post updatedPost = new Post();
168+
updatedPost.setTitle("New Title");
169+
// description not set, should remain unchanged
170+
171+
when(postRepo.findById(1L)).thenReturn(Optional.of(existingPost));
172+
173+
postService.updatePost(1L, updatedPost);
174+
175+
assertEquals("New Title", existingPost.getTitle());
176+
assertEquals("Old Description", existingPost.getDescription());
177+
}
178+
179+
@Test
180+
void testGetCustomizedPosts_UserWithoutEmbedding() throws UserNotFoundException {
181+
User userWithoutEmbedding = new User();
182+
userWithoutEmbedding.setUserId(2L);
183+
userWithoutEmbedding.setEmbedding(null);
184+
185+
when(userService.getCurrentUser()).thenReturn(userWithoutEmbedding);
186+
187+
assertThrows(IllegalStateException.class, () -> postService.getCustomizedPosts());
188+
}
189+
190+
@Test
191+
void testSearchPosts() {
192+
String searchTerm = "lost phone";
193+
List<Post> expectedPosts = List.of(post);
194+
when(postRepo.searchPosts(searchTerm)).thenReturn(expectedPosts);
195+
196+
List<Post> results = postService.searchPosts(searchTerm);
197+
198+
assertEquals(expectedPosts, results);
199+
verify(postRepo).searchPosts(searchTerm);
200+
}
201+
160202
}
161203

0 commit comments

Comments
 (0)