Skip to content

Commit

Permalink
Merge branch 'develop' into fix/101-change-timeline-cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
penrose15 authored Jul 25, 2024
2 parents 96387cd + 8a9d224 commit 41a30ff
Show file tree
Hide file tree
Showing 12 changed files with 478 additions and 19 deletions.
1 change: 1 addition & 0 deletions module-presentation/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
implementation 'org.springframework.boot:spring-boot-starter-validation'
testImplementation 'org.springframework.security:spring-security-test'

// swagger
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TimelineRequestDto {
@DateTimeFormat(pattern = "yyyy-MM-dd")
@Schema(description = "์ตœ์ดˆ ์กฐํšŒ ์ดํ›„ ๋‚˜์˜จ timeline ๋ฆฌ์ŠคํŠธ ์ค‘ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ์š”์†Œ์˜ memory recordAt")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.depromeet.pool.service;

import static com.depromeet.pool.service.PoolValidator.*;

import com.depromeet.exception.NotFoundException;
import com.depromeet.member.Member;
import com.depromeet.member.repository.MemberRepository;
Expand Down Expand Up @@ -86,10 +84,4 @@ private Pool getPool(Long poolId) {
.findById(poolId)
.orElseThrow(() -> new NotFoundException(PoolErrorType.NOT_FOUND));
}

private FavoritePool getFavoritePool(Long favoritePoolId) {
return poolRepository
.findFavoritePoolById(favoritePoolId)
.orElseThrow(() -> new NotFoundException(PoolErrorType.FAVORITE_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.depromeet.memory.config;

import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;

import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

@SpringJUnitWebConfig
public abstract class ControllerTestConfig {
@Autowired protected WebApplicationContext webApplicationContext;
protected MockMvc mockMvc;

@BeforeEach
void setUp() {
this.mockMvc =
MockMvcBuilders.webAppContextSetup(webApplicationContext)
.apply(springSecurity())
.defaultRequest(post("/**").with(csrf()))
.defaultRequest(patch("/**").with(csrf()))
.defaultRequest(delete("/**").with(csrf()))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package com.depromeet.memory.controller;

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

import com.depromeet.memory.api.MemoryController;
import com.depromeet.memory.config.ControllerTestConfig;
import com.depromeet.memory.facade.MemoryFacade;
import com.depromeet.memory.mock.WithCustomMockMember;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;

@WebMvcTest(MemoryController.class)
public class MemoryControllerTest extends ControllerTestConfig {
@Autowired private ObjectMapper objectMapper;

@MockBean MemoryFacade memoryFacade;

@Test
@WithCustomMockMember
void ์ˆ˜์˜๊ธฐ๋ก์„_์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค() throws Exception {
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("poolId", 1);
requestBody.put("item", "์˜ค๋ฆฌ๋ฐœ");
requestBody.put("heartRate", 115);
requestBody.put("pace", "05:00:00");
requestBody.put("kcal", 300);
requestBody.put("recordAt", "2024-07-24");
requestBody.put("startTime", "11:00:00");
requestBody.put("endTime", "11:50:00");
requestBody.put("lane", 25);
requestBody.put("diary", "์ผ๊ธฐ๋ฅผ ๊ธฐ๋กํ•œ๋‹ค");

List<Map<String, Object>> strokes = new ArrayList<>();
Map<String, Object> stroke = new HashMap<>();
stroke.put("name", "์ž์œ ํ˜•");
stroke.put("laps", 3);
stroke.put("meter", 150);
strokes.add(stroke);

requestBody.put("strokes", strokes);

List<Long> imageIdList = new ArrayList<>();
requestBody.put("imageIdList", imageIdList);

mockMvc.perform(
post("/api/memory")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(requestBody)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value("MEMORY_1"))
.andExpect(jsonPath("$.message").value("์ˆ˜์˜ ๊ธฐ๋ก ์ €์žฅ์— ์„ฑ๊ณตํ•˜์˜€์Šต๋‹ˆ๋‹ค"))
.andDo(print());
}

@Test
@WithCustomMockMember
public void ์ˆ˜์˜๊ธฐ๋ก์„_์กฐํšŒํ•ฉ๋‹ˆ๋‹ค() throws Exception {
mockMvc.perform(get("/api/memory/{memoryId}", 1))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value("MEMORY_2"))
.andExpect(jsonPath("$.message").value("์ˆ˜์˜ ๊ธฐ๋ก ์กฐํšŒ์— ์„ฑ๊ณตํ•˜์˜€์Šต๋‹ˆ๋‹ค"))
.andDo(print());
}

@Test
@WithCustomMockMember
public void ์ˆ˜์˜๊ธฐ๋ก์„_๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค() throws Exception {
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("poolId", 1);
requestBody.put("recordAt", "2024-07-24");
requestBody.put("startTime", "11:00:00");
requestBody.put("endTime", "11:50:00");
mockMvc.perform(
patch("/api/memory/{memoryId}", 1)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(requestBody)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value("MEMORY_3"))
.andExpect(jsonPath("$.message").value("์ˆ˜์˜ ๊ธฐ๋ก ์ˆ˜์ •์— ์„ฑ๊ณตํ•˜์˜€์Šต๋‹ˆ๋‹ค"))
.andDo(print());
}

@Test
@WithCustomMockMember
public void ์ˆ˜์˜๊ธฐ๋ก_ํƒ€์ž„๋ผ์ธ์„_์กฐํšŒํ•ฉ๋‹ˆ๋‹ค() throws Exception {
mockMvc.perform(
get("/api/memory/timeline")
.param("cursorId", "1L")
.param("date", "2024-07")
.param("size", "5"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value("MEMORY_4"))
.andExpect(jsonPath("$.message").value("ํƒ€์ž„๋ผ์ธ ์กฐํšŒ์— ์„ฑ๊ณตํ•˜์˜€์Šต๋‹ˆ๋‹ค"))
.andDo(print());
}

@Test
@WithCustomMockMember
public void ์ˆ˜์˜๊ธฐ๋ก_์บ˜๋ฆฐ๋”๋ฅผ_์กฐํšŒํ•ฉ๋‹ˆ๋‹ค() throws Exception {
mockMvc.perform(get("/api/memory/calendar").param("yearMonth", "2024-07"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value("MEMORY_5"))
.andExpect(jsonPath("$.message").value("์บ˜๋ฆฐ๋” ์กฐํšŒ์— ์„ฑ๊ณตํ•˜์˜€์Šต๋‹ˆ๋‹ค"))
.andDo(print());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.depromeet.memory.controller;

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

import com.depromeet.memory.config.ControllerTestConfig;
import com.depromeet.memory.mock.WithCustomMockMember;
import com.depromeet.pool.api.PoolController;
import com.depromeet.pool.service.PoolService;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.mockito.BDDMockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;

@WebMvcTest(PoolController.class)
public class PoolControllerTest extends ControllerTestConfig {
@Autowired private ObjectMapper objectMapper;

@MockBean PoolService poolService;

@Test
@WithCustomMockMember
void ์ด๋ฆ„์ฟผ๋ฆฌ๋กœ_์ˆ˜์˜์žฅ์„_์กฐํšŒํ•ฉ๋‹ˆ๋‹ค() throws Exception {
mockMvc.perform(get("/api/pool/search").param("nameQuery", "๊ฒ€์ƒ‰์ธ์ž"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value("POOL_1"))
.andExpect(jsonPath("$.message").value("์ˆ˜์˜์žฅ ๊ฒ€์ƒ‰์„ ์„ฑ๊ณตํ•˜์˜€์Šต๋‹ˆ๋‹ค"))
.andDo(print());
}

@Test
@WithCustomMockMember
void ์ฆ๊ฒจ์ฐพ๊ธฐ_์ˆ˜์˜์žฅ_๋ฐ_์ตœ๊ทผ_๊ฒ€์ƒ‰_์ˆ˜์˜์žฅ์„_์กฐํšŒํ•ฉ๋‹ˆ๋‹ค() throws Exception {
mockMvc.perform(get("/api/pool/search/initial"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value("POOL_2"))
.andExpect(jsonPath("$.message").value("์ฆ๊ฒจ์ฐพ๊ธฐ ๋ฐ ์ตœ๊ทผ ๊ฒ€์ƒ‰ ์ˆ˜์˜์žฅ ์กฐํšŒ๋ฅผ ์„ฑ๊ณตํ•˜์˜€์Šต๋‹ˆ๋‹ค"))
.andDo(print());
}

@Test
@WithCustomMockMember
void ์ˆ˜์˜์žฅ์„_์ฆ๊ฒจ์ฐพ๊ธฐ์—_๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค() throws Exception {
Map<String, Long> requestBody = new HashMap<>();
requestBody.put("poolId", 1L);
BDDMockito.given(poolService.putFavoritePool(any(), any())).willReturn("1");

mockMvc.perform(
put("/api/pool/favorite")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(requestBody)))
.andExpect(status().isCreated())
.andDo(print());
}

@Test
@WithCustomMockMember
void ์ˆ˜์˜์žฅ์„_์ฆ๊ฒจ์ฐพ๊ธฐ์—์„œ_์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค() throws Exception {
Map<String, Long> requestBody = new HashMap<>();
requestBody.put("poolId", 1L);
BDDMockito.given(poolService.putFavoritePool(any(), any())).willReturn(null);

mockMvc.perform(
put("/api/pool/favorite")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(requestBody)))
.andExpect(status().isNoContent())
.andDo(print());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.depromeet.memory.fixture;

import com.depromeet.member.Member;
import com.depromeet.member.MemberRole;

public class MemberFixture {
public static Member make(Long userId, String role) {
return Member.builder()
.id(userId)
.name("member")
.email("test@gmail.com")
.role(MemberRole.valueOf(role))
.goal(1000)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.depromeet.memory.fixture;

import com.depromeet.pool.Pool;

public class PoolFixture {
public static Pool make(String name, String address, Integer lane) {
return Pool.builder().name(name).address(address).lane(lane).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,35 @@
import java.util.Optional;

public class FakePoolRepository implements PoolRepository {
private Long autoGeneratedId = 0L;
private final List<Pool> data = new ArrayList<>();
private Long poolAutoGeneratedId = 0L;
private Long favoritePoolAutoGeneratedId = 0L;
private Long poolSerachAutoGeneratedId = 0L;
private final List<Pool> poolData = new ArrayList<>();
private final List<FavoritePool> favoritePoolData = new ArrayList<>();
private final List<PoolSearch> poolSearchData = new ArrayList<>();

@Override
public List<Pool> findPoolsByName(String nameQuery) {
return data.stream().filter(item -> item.getName().contains(nameQuery)).limit(3).toList();
return poolData.stream().filter(item -> item.getName().contains(nameQuery)).toList();
}

@Override
public List<FavoritePool> findFavoritePools(Long memberId) {
return null;
return favoritePoolData.stream()
.filter(item -> item.getMember().getId().equals(memberId))
.toList();
}

@Override
public List<PoolSearch> findSearchedPools(Long memberId) {
return null;
return poolSearchData.stream()
.filter(item -> item.getMember().getId().equals(memberId))
.toList();
}

@Override
public Optional<Pool> findById(Long poolId) {
return data.stream().filter(item -> item.getId().equals(poolId)).findAny();
return poolData.stream().filter(item -> item.getId().equals(poolId)).findAny();
}

@Override
Expand All @@ -39,24 +47,57 @@ public Optional<FavoritePool> findFavoritePoolById(Long favoritePoolId) {

@Override
public Pool save(Pool pool) {
return null;
Pool newPool =
Pool.builder()
.id(++poolAutoGeneratedId)
.name(pool.getName())
.address(pool.getAddress())
.lane(pool.getLane())
.build();
poolData.add(newPool);
return newPool;
}

@Override
public PoolSearch savePoolSearch(PoolSearch poolSearch) {
return null;
PoolSearch newPoolSearch =
PoolSearch.builder()
.id(++poolSerachAutoGeneratedId)
.pool(poolSearch.getPool())
.member(poolSearch.getMember())
.build();
poolSearchData.add(newPoolSearch);
return newPoolSearch;
}

@Override
public FavoritePool saveFavoritePool(FavoritePool favoritePool) {
return null;
FavoritePool newFavoritePool =
FavoritePool.builder()
.id(++favoritePoolAutoGeneratedId)
.pool(favoritePool.getPool())
.member(favoritePool.getMember())
.build();
favoritePoolData.add(newFavoritePool);
return newFavoritePool;
}

@Override
public boolean existsFavoritePool(FavoritePool favoritePool) {
return false;
return favoritePoolData.stream()
.anyMatch(
item ->
item.getPool().getId().equals(favoritePool.getPool().getId())
&& item.getMember()
.getId()
.equals(favoritePool.getMember().getId()));
}

@Override
public void deleteFavoritePool(FavoritePool favoritePool) {}
public void deleteFavoritePool(FavoritePool favoritePool) {
favoritePoolData.removeIf(
it ->
it.getPool().getId().equals(favoritePool.getPool().getId())
&& it.getMember().getId().equals(favoritePool.getMember().getId()));
}
}
Loading

0 comments on commit 41a30ff

Please sign in to comment.