Skip to content

Commit

Permalink
Merge pull request #61 from BbeumbungE/feat/BACK-292
Browse files Browse the repository at this point in the history
feat: 게시물 및 비전 API 테스트코드 작성, Canvas SSE Initial 이벤트 삭제 [BACK-292]
  • Loading branch information
Fishphobiagg authored Sep 26, 2023
2 parents b6c4aea + bb55f31 commit bc2901f
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 18 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ public class VisionDetectingService {
public Score calculateCanvasScore(Long canvasId) {
Canvas canvas = canvasFindDao.findById(canvasId);
Map<String, Float> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
18 changes: 18 additions & 0 deletions src/test/java/com/siliconvalley/SiliconValleyApplicationTests.java
Original file line number Diff line number Diff line change
@@ -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() {
}

}
13 changes: 0 additions & 13 deletions src/test/java/com/siliconvalley/SiliconvallyApplicationTests.java

This file was deleted.

90 changes: 90 additions & 0 deletions src/test/java/com/siliconvalley/post/PostingServiceTests.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
15 changes: 15 additions & 0 deletions src/test/java/com/siliconvalley/post/RankingServiceTests.java
Original file line number Diff line number Diff line change
@@ -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;


}
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit bc2901f

Please sign in to comment.