Skip to content

Commit

Permalink
Merge pull request #458 from depromeet/test/457-greeting
Browse files Browse the repository at this point in the history
test: Greeting 도메인 ν…ŒμŠ€νŠΈ μ½”λ“œ μž‘μ„±
  • Loading branch information
its-sky authored Nov 8, 2024
2 parents 5f2a391 + 66bb096 commit dd09a93
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 2 deletions.
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!";

@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("인삿말 μ‘°νšŒμ— μ„±κ³΅ν•˜μ˜€μŠ΅λ‹ˆλ‹€"));
}
}

0 comments on commit dd09a93

Please sign in to comment.