Skip to content
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

test: Greeting 도메인 테스트 코드 작성 #458

Merged
merged 6 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.depromeet.mock.greeting;

import com.depromeet.greeting.port.out.AIPort;

public class FakeAIManager implements AIPort {
private static final String GENERATED_RESPONSE = "Hello, World!";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API 테스트에 페이크를 적절히 사용하신 것 배워 가겠습니다 !!! 멋진 코드


@Override
public String getSummary(String inputText) {
return GENERATED_RESPONSE;
}

@Override
public String getChatCompletions() {
return GENERATED_RESPONSE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.depromeet.mock.greeting;

import com.depromeet.greeting.domain.Greeting;
import com.depromeet.greeting.port.out.GreetingCachePort;
import java.util.HashMap;
import java.util.Map;

public class FakeGreetingCacheManager implements GreetingCachePort {
private static final String GREETING_KEY = "greeting";
private Map<String, String> data = new HashMap<>();

@Override
public Greeting saveGreeting(Greeting greeting) {
if (greeting.getMessage() != null) {
data.put(GREETING_KEY, greeting.getMessage());
}
return greeting;
}

@Override
public Greeting getGreeting() {
return new Greeting(data.get(GREETING_KEY));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.depromeet.service.greeting;

import static org.assertj.core.api.Assertions.*;

import com.depromeet.greeting.domain.Greeting;
import com.depromeet.greeting.port.out.AIPort;
import com.depromeet.greeting.port.out.GreetingCachePort;
import com.depromeet.greeting.service.GreetingService;
import com.depromeet.mock.greeting.FakeAIManager;
import com.depromeet.mock.greeting.FakeGreetingCacheManager;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class GreetingServiceTest {
private AIPort aiPort;
private GreetingCachePort greetingCachePort;
private GreetingService greetingService;

@BeforeEach
void init() {
aiPort = new FakeAIManager();
greetingCachePort = new FakeGreetingCacheManager();
greetingService = new GreetingService(aiPort, greetingCachePort);
}

@Test
public void 인삿말을_조회합니다() throws Exception {
// when
Greeting greeting = greetingService.getGreeting();

// then
assertThat(greeting.getMessage()).isEqualTo("Hello, World!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.depromeet.type.SuccessType;

public enum GreetingSuccessType implements SuccessType {
GET_RESPONSE_SUCCESS("AI_1", "AI 응답 받기에 성공하였습니다");
GET_RESPONSE_SUCCESS("GREETING_1", "인삿말 조회에 성공하였습니다");

private final String code;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@Service
@RequiredArgsConstructor
public class GreetingCacheService implements GreetingCachePort {
public class GreetingCacheManager implements GreetingCachePort {
private static final String GREETING_KEY = "greeting";
private final RedisTemplate<String, String> redisTemplate;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.depromeet.greeting.api;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import com.depromeet.config.ControllerTestConfig;
import com.depromeet.config.mock.WithCustomMockMember;
import com.depromeet.greeting.facade.GreetingFacade;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;

@WebMvcTest(GreetingController.class)
public class GreetingControllerTest extends ControllerTestConfig {
@MockBean GreetingFacade greetingFacade;

@Test
@WithCustomMockMember
public void 인삿말을_조회합니다() throws Exception {
mockMvc.perform(get("/greeting"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value("GREETING_1"))
.andExpect(jsonPath("$.message").value("인삿말 조회에 성공하였습니다"));
}
}
Loading