diff --git a/build.gradle b/build.gradle index a613391..2a43dab 100644 --- a/build.gradle +++ b/build.gradle @@ -76,6 +76,10 @@ dependencies { annotationProcessor 'org.projectlombok:lombok' testCompileOnly 'org.projectlombok:lombok' testAnnotationProcessor 'org.projectlombok:lombok' + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1' + testImplementation 'org.mockito:mockito-core:4.8.0' + testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1' + } test { diff --git a/src/main/java/com/siliconvalley/domain/google/service/VisionDetectingService.java b/src/main/java/com/siliconvalley/domain/google/service/VisionDetectingService.java index b5e88e1..9ec4899 100644 --- a/src/main/java/com/siliconvalley/domain/google/service/VisionDetectingService.java +++ b/src/main/java/com/siliconvalley/domain/google/service/VisionDetectingService.java @@ -22,8 +22,11 @@ public class VisionDetectingService { public Score calculateCanvasScore(Long canvasId) { Canvas canvas = canvasFindDao.findById(canvasId); Map detectResult = visionService.detectObjects(canvas.getCanvas()); - Float detectionScore = detectResult.get(canvas.getSubject().getPix2Pix().getVisionName()); + + if (detectResult.size() == 0) {return Score.LOW;} + Float detectionScore = detectResult.get(canvas.getSubject().getPix2Pix().getModelName()); return Score.determineScore(detectionScore); } + } diff --git a/src/main/java/com/siliconvalley/domain/sse/application/CanvasSseEmitterService.java b/src/main/java/com/siliconvalley/domain/sse/application/CanvasSseEmitterService.java index 06921c7..3988746 100644 --- a/src/main/java/com/siliconvalley/domain/sse/application/CanvasSseEmitterService.java +++ b/src/main/java/com/siliconvalley/domain/sse/application/CanvasSseEmitterService.java @@ -22,10 +22,6 @@ public SseEmitter connect(Long profileId) { } SseEmitter sseEmitter = canvasSseEmitterCreater.createEmitter(profileId); - // 연결 후 즉시 알림을 보내지 않으면 연결이 끊길 수 있으므로 연결 성공 알림 보내기 - String id = profileId + "_" + System.currentTimeMillis(); - sseEmitterSender.send(sseEmitter, id, new SseConnectSuccessResponse(profileId), profileId, "initial"); - return sseEmitter; } } \ No newline at end of file diff --git a/src/test/java/com/siliconvalley/SiliconValleyApplicationTests.java b/src/test/java/com/siliconvalley/SiliconValleyApplicationTests.java new file mode 100644 index 0000000..4588ca7 --- /dev/null +++ b/src/test/java/com/siliconvalley/SiliconValleyApplicationTests.java @@ -0,0 +1,18 @@ +package com.siliconvalley; + +import com.siliconvalley.domain.post.service.PostingService; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class SiliconValleyApplicationTests { + + @Autowired + PostingService postingService; + + @Test + void contextLoads() { + } + +} diff --git a/src/test/java/com/siliconvalley/SiliconvallyApplicationTests.java b/src/test/java/com/siliconvalley/SiliconvallyApplicationTests.java deleted file mode 100644 index 49ffed4..0000000 --- a/src/test/java/com/siliconvalley/SiliconvallyApplicationTests.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.siliconvalley; - -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; - -@SpringBootTest -class SiliconvallyApplicationTests { - - @Test - void contextLoads() { - } - -} diff --git a/src/test/java/com/siliconvalley/post/PostingServiceTests.java b/src/test/java/com/siliconvalley/post/PostingServiceTests.java new file mode 100644 index 0000000..80b13a5 --- /dev/null +++ b/src/test/java/com/siliconvalley/post/PostingServiceTests.java @@ -0,0 +1,90 @@ +package com.siliconvalley.post; + +import com.siliconvalley.domain.canvas.dao.CanvasFindDao; +import static org.junit.jupiter.api.Assertions.*; +import com.siliconvalley.domain.canvas.domain.Canvas; +import com.siliconvalley.domain.post.code.PostCode; +import com.siliconvalley.domain.post.dao.PostFindDao; +import com.siliconvalley.domain.post.dao.PostRepository; +import com.siliconvalley.domain.post.domain.Post; +import com.siliconvalley.domain.post.exception.IllegalDeleteException; +import com.siliconvalley.domain.post.service.PostingService; +import com.siliconvalley.domain.profile.domain.Profile; +import com.siliconvalley.global.common.dto.Response; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +@DisplayName(value = "게시물 CRUD 테스트") +public class PostingServiceTests { + + @InjectMocks + private PostingService postingService; + + @Mock + private PostFindDao postFindDao; + + @Mock + private PostRepository postRepository; + + @Mock + private CanvasFindDao canvasFindDao; + + @Test + @DisplayName(value = "그림에 대한 게시물을 작성한다.") + void 게시물작성() { + // Given + Long canvasId = 1L; + Canvas mockCanvas = mock(Canvas.class); + Post mockPost = mock(Post.class); + when(canvasFindDao.findById(canvasId)).thenReturn(mockCanvas); + when(mockCanvas.buildPost()).thenReturn(mockPost); + + // When + Response response = postingService.createPostForCanvas(canvasId); + + // Then + verify(postRepository).save(mockPost); + assertEquals(PostCode.POSTING_SUCCESS.getCode(), response.getStatus().getCode()); + } + + @Test + @DisplayName("게시물을 삭제한다.") + void 게시물삭제() { + // Given + Long postId = 1L; + Profile mockProfile = mock(Profile.class); + Post mockPost = mock(Post.class); + when(mockProfile.getId()).thenReturn(postId); + when(postFindDao.findById(postId)).thenReturn(mockPost); + when(mockPost.getId()).thenReturn(postId); + + // When + Response response = postingService.deletePost(postId, mockProfile); + + // Then + verify(postRepository).delete(mockPost); + assertEquals(PostCode.DELETE_POST_SUCCESS.getCode(), response.getStatus().getCode()); + } + + @Test + @DisplayName(value = "다른 유저의 게시물을 삭제한다.") + void 게시물삭제예외() { + // Given + Long postId = 1L; + Profile mockProfile = mock(Profile.class); + Post mockPost = mock(Post.class); + when(mockProfile.getId()).thenReturn(2L); // Different ID + when(postFindDao.findById(postId)).thenReturn(mockPost); + when(mockPost.getId()).thenReturn(postId); + + // Then + assertThrows(IllegalDeleteException.class, () -> postingService.deletePost(postId, mockProfile)); + } +} diff --git a/src/test/java/com/siliconvalley/post/RankingServiceTests.java b/src/test/java/com/siliconvalley/post/RankingServiceTests.java new file mode 100644 index 0000000..2d7ff3a --- /dev/null +++ b/src/test/java/com/siliconvalley/post/RankingServiceTests.java @@ -0,0 +1,15 @@ +package com.siliconvalley.post; + +import com.siliconvalley.domain.post.service.RankCachingService; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +public class RankingServiceTests { + + @InjectMocks + private RankCachingService rankCachingService; + + +} diff --git a/src/test/java/com/siliconvalley/vision/VisionDetectingServiceTests.java b/src/test/java/com/siliconvalley/vision/VisionDetectingServiceTests.java new file mode 100644 index 0000000..595bac2 --- /dev/null +++ b/src/test/java/com/siliconvalley/vision/VisionDetectingServiceTests.java @@ -0,0 +1,128 @@ +package com.siliconvalley.vision; + +import com.siliconvalley.domain.canvas.dao.CanvasFindDao; +import com.siliconvalley.domain.canvas.domain.Canvas; +import com.siliconvalley.domain.google.service.GoogleVisionApiService; +import com.siliconvalley.domain.google.service.VisionDetectingService; +import com.siliconvalley.domain.stage.domain.Score; +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + + +@ExtendWith(MockitoExtension.class) +@Slf4j +@DisplayName(value = "점수 측정 테스트") +public class VisionDetectingServiceTests { + + @InjectMocks + private VisionDetectingService visionDetectingService; + + @Mock + private CanvasFindDao canvasFindDao; + + @Mock + private GoogleVisionApiService visionService; + + @Test + @DisplayName(value = "다른 물체만 감지되었을 때 그림의 점수를 LOW(1)로 반환") + void 그림점수_낮음() { + // Given + Long canvasId = 1L; + Canvas mockCanvas = mock(Canvas.class, RETURNS_DEEP_STUBS); + when(canvasFindDao.findById(canvasId)).thenReturn(mockCanvas); + when(mockCanvas.getCanvas()).thenReturn("sampleCanvasData"); + when(visionService.detectObjects("sampleCanvasData")).thenReturn(Map.of("anotherModelName", 0.8f)); // 수정된 점수 + when(mockCanvas.getSubject().getPix2Pix().getModelName()).thenReturn("sampleModelName"); + + // When + Score result = visionDetectingService.calculateCanvasScore(canvasId); + + // Then + assertEquals(Score.LOW, result); + } + + @Test + @DisplayName(value = "목표 물체가 감지 되었을 때 그림의 점수를 MEDIUM(2)로 반환") + void 그림점수_중간() { + // Given + Long canvasId = 1L; + Canvas mockCanvas = mock(Canvas.class, RETURNS_DEEP_STUBS); + when(canvasFindDao.findById(canvasId)).thenReturn(mockCanvas); + when(mockCanvas.getCanvas()).thenReturn("sampleCanvasData"); + when(visionService.detectObjects("sampleCanvasData")).thenReturn(Map.of("sampleModelName", 0.3f)); // 수정된 점수 + when(mockCanvas.getSubject().getPix2Pix().getModelName()).thenReturn("sampleModelName"); + + // When + Score result = visionDetectingService.calculateCanvasScore(canvasId); + + // Then + assertEquals(Score.MEDIUM, result); + } + + @Test + @DisplayName(value = "목표 물체 감지율 80%이상일 때 그림의 점수를 HIGH(3)으로 반환") + void 그림점수_높음() { + // Given + Long canvasId = 1L; + Canvas mockCanvas = mock(Canvas.class, RETURNS_DEEP_STUBS); + when(canvasFindDao.findById(canvasId)).thenReturn(mockCanvas); + when(mockCanvas.getCanvas()).thenReturn("sampleCanvasData"); + when(visionService.detectObjects("sampleCanvasData")).thenReturn(Map.of("sampleModelName", 0.8f)); // 수정된 점수 + when(mockCanvas.getSubject().getPix2Pix().getModelName()).thenReturn("sampleModelName"); + + // When + Score result = visionDetectingService.calculateCanvasScore(canvasId); + + // Then + assertEquals(Score.HIGH, result); + } + + @Test + @DisplayName(value = "감지된 물체가 없을 경우 그림의 점수를 LOW(1)로 반환") + void 물체감지실패_점수낮음() { + // Given + Long canvasId = 1L; + Canvas mockCanvas = mock(Canvas.class, RETURNS_DEEP_STUBS); + when(canvasFindDao.findById(canvasId)).thenReturn(mockCanvas); + when(mockCanvas.getCanvas()).thenReturn("sampleCanvasData"); + + // 탐지된 물체가 없을 경우 빈 Map 반환 + when(visionService.detectObjects("sampleCanvasData")).thenReturn(Map.of()); + + // When + Score result = visionDetectingService.calculateCanvasScore(canvasId); + + // Then + assertEquals(Score.LOW, result); + } + + @Test + @DisplayName(value = "입출력 오류가 발생했을 경우 그림의 점수를 LOW(1)로 반환") + void 입출력오류_점수낮음() { + // Given + Long canvasId = 1L; + Canvas mockCanvas = mock(Canvas.class, RETURNS_DEEP_STUBS); + when(canvasFindDao.findById(canvasId)).thenReturn(mockCanvas); + when(mockCanvas.getCanvas()).thenReturn("sampleCanvasData"); + + // 탐지된 물체가 없을 경우 빈 Map 반환 + when(visionService.detectObjects("sampleCanvasData")).thenReturn(Map.of("Error", -1f)); + + // When + Score result = visionDetectingService.calculateCanvasScore(canvasId); + + // Then + assertEquals(Score.LOW, result); + } + +}