Skip to content

Commit

Permalink
Merge pull request #110 from playkuround/test/landmarkInitData
Browse files Browse the repository at this point in the history
refactor: landmark initial data in test case
  • Loading branch information
redcarrot1 authored Aug 21, 2024
2 parents d9cf8fe + f8a521c commit 54a1a53
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ public class Landmark {

private long highestScore;

public Landmark(LandmarkType landmarkType, double latitude, double longitude, int recognitionRadius) {
this.name = landmarkType;
this.latitude = latitude;
this.longitude = longitude;
this.recognitionRadius = recognitionRadius;
this.firstUser = null;
this.highestScore = 0;
}

public void updateFirstUser(User user, long score) {
if (score == 0) return;
if (firstUser == null || this.highestScore < score) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.playkuround.playkuroundserver.domain.badge.domain.BadgeType;
import com.playkuround.playkuroundserver.domain.landmark.dao.LandmarkRepository;
import com.playkuround.playkuroundserver.domain.landmark.domain.Landmark;
import com.playkuround.playkuroundserver.domain.landmark.domain.LandmarkType;
import com.playkuround.playkuroundserver.domain.score.domain.ScoreType;
import com.playkuround.playkuroundserver.domain.user.dao.UserRepository;
import com.playkuround.playkuroundserver.domain.user.domain.User;
Expand All @@ -20,7 +21,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.web.servlet.MockMvc;

import java.util.List;
Expand All @@ -34,7 +34,6 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@IntegrationControllerTest
@Sql(scripts = {"/data-mysql.sql"}, executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
class AdventureApiTest {

@Autowired
Expand Down Expand Up @@ -74,7 +73,8 @@ void clean() {
@DisplayName("탐험을 하게 되면 total score 증가, adventure 저장, 랜드마크별 최고기록과 유저별 게임 최고기록이 업데이트 된다.")
void saveAdventure_1() throws Exception {
// given
Landmark landmark = landmarkRepository.findById(3L).get();
Landmark landmark = new Landmark(LandmarkType.수의학관, 37.541, 127.079, 100);
landmarkRepository.save(landmark);

AdventureSaveRequest adventureSaveRequest
= new AdventureSaveRequest(landmark.getId(), landmark.getLatitude(), landmark.getLongitude(), 100L, ScoreType.BOOK.name());
Expand Down Expand Up @@ -116,7 +116,8 @@ void saveAdventure_1() throws Exception {
@DisplayName("랜드마크가 존재하지 않으면 에러가 발생한다.")
void saveAdventure_2() throws Exception {
// given
Landmark landmark = landmarkRepository.findById(3L).get();
Landmark landmark = new Landmark(LandmarkType.경영관, 37.541, 127.079, 100);
landmarkRepository.save(landmark);

AdventureSaveRequest adventureSaveRequest
= new AdventureSaveRequest(-1L, landmark.getLatitude(), landmark.getLongitude(), 100L, ScoreType.BOOK.name());
Expand All @@ -141,7 +142,8 @@ void saveAdventure_2() throws Exception {
@DisplayName("인식 거리 밖에 있으면 에러가 발생한다.")
void saveAdventure_3() throws Exception {
// given
Landmark landmark = landmarkRepository.findById(3L).get();
Landmark landmark = new Landmark(LandmarkType.경영관, 37.541, 127.079, 100);
landmarkRepository.save(landmark);

AdventureSaveRequest adventureSaveRequest
= new AdventureSaveRequest(landmark.getId(), 0.0, 0.0, 100L, ScoreType.BOOK.name());
Expand All @@ -167,7 +169,8 @@ void saveAdventure_3() throws Exception {
@DisplayName("정상적인 ScoreType이 아니면 에러가 발생한다.")
void saveAdventure_4() throws Exception {
// given
Landmark landmark = landmarkRepository.findById(3L).get();
Landmark landmark = new Landmark(LandmarkType.경영관, 37.541, 127.079, 100);
landmarkRepository.save(landmark);

AdventureSaveRequest adventureSaveRequest
= new AdventureSaveRequest(landmark.getId(), landmark.getLatitude(), landmark.getLongitude(), 100L, "notFound");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import com.playkuround.playkuroundserver.domain.adventure.dto.AdventureSaveDto;
import com.playkuround.playkuroundserver.domain.adventure.exception.InvalidLandmarkLocationException;
import com.playkuround.playkuroundserver.domain.badge.dao.BadgeRepository;
import com.playkuround.playkuroundserver.domain.badge.dto.NewlyRegisteredBadge;
import com.playkuround.playkuroundserver.domain.landmark.dao.LandmarkRepository;
import com.playkuround.playkuroundserver.domain.landmark.domain.Landmark;
import com.playkuround.playkuroundserver.domain.landmark.domain.LandmarkType;
import com.playkuround.playkuroundserver.domain.landmark.exception.LandmarkNotFoundException;
import com.playkuround.playkuroundserver.domain.score.domain.ScoreType;
import com.playkuround.playkuroundserver.domain.user.dao.UserRepository;
Expand All @@ -21,31 +21,35 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.jdbc.Sql;

import java.util.List;
import java.util.Optional;

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

@IntegrationServiceTest
@Sql(scripts = {"/data-mysql.sql"}, executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
class AdventureServiceTest {

private final String redisSetKey = "ranking";
@Autowired
private BadgeRepository badgeRepository;

@Autowired
private UserRepository userRepository;

@Autowired
private AdventureRepository adventureRepository;

@Autowired
private RedisTemplate<String, String> redisTemplate;

@Autowired
private LandmarkRepository landmarkRepository;

@Autowired
private AdventureService adventureService;

private final String redisSetKey = "ranking";

@AfterEach
void clean() {
badgeRepository.deleteAllInBatch();
Expand All @@ -61,12 +65,14 @@ void saveAdventure_1() {
// given
User user = TestUtil.createUser();

Landmark landmark = landmarkRepository.findById(3L).get();
Landmark landmark = new Landmark(LandmarkType.경영관, 37.541, 127.079, 100);
landmarkRepository.save(landmark);

Location location = new Location(landmark.getLatitude(), landmark.getLongitude());
AdventureSaveDto adventureSaveDto = new AdventureSaveDto(user, landmark.getId(), location, 100, ScoreType.BOOK);

// when
NewlyRegisteredBadge newlyRegisteredBadge = adventureService.saveAdventure(adventureSaveDto);
adventureService.saveAdventure(adventureSaveDto);

// then
// Total Score 저장 및 최고 점수 갱신
Expand Down Expand Up @@ -95,7 +101,9 @@ void saveAdventure_2() {
// given
User user = TestUtil.createUser();

Landmark landmark = landmarkRepository.findById(3L).get();
Landmark landmark = new Landmark(LandmarkType.경영관, 37.541, 127.079, 100);
landmarkRepository.save(landmark);

Location location = new Location(landmark.getLatitude(), landmark.getLongitude());
AdventureSaveDto adventureSaveDto = new AdventureSaveDto(user, -1L, location, 100, ScoreType.BOOK);

Expand All @@ -114,7 +122,9 @@ void saveAdventure_3() {
// given
User user = TestUtil.createUser();

Landmark landmark = landmarkRepository.findById(3L).get();
Landmark landmark = new Landmark(LandmarkType.경영관, 37.541, 127.079, 100);
landmarkRepository.save(landmark);

Location location = new Location(0, 0);
AdventureSaveDto adventureSaveDto = new AdventureSaveDto(user, landmark.getId(), location, 100, ScoreType.BOOK);

Expand All @@ -132,19 +142,20 @@ void saveAdventure_3() {
void saveAdventure_4() {
// given
User user = TestUtil.createUser();

User otherUser = TestUtil.createUser("other@test.com", "other", Major.건축학부);
userRepository.saveAll(List.of(user, otherUser));

long highestScore = 1000;
Landmark landmark = landmarkRepository.findById(3L).get();
Landmark landmark = new Landmark(LandmarkType.경영관, 37.541, 127.079, 100);
landmark.updateFirstUser(otherUser, highestScore);
landmarkRepository.save(landmark); // update
landmarkRepository.save(landmark);

Location location = new Location(landmark.getLatitude(), landmark.getLongitude());
AdventureSaveDto adventureSaveDto = new AdventureSaveDto(user, landmark.getId(), location, 100, ScoreType.BOOK);

// when
NewlyRegisteredBadge newlyRegisteredBadge = adventureService.saveAdventure(adventureSaveDto);
adventureService.saveAdventure(adventureSaveDto);

// then
Optional<Landmark> optionalLandmark = landmarkRepository.findById(landmark.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class LandmarkApiTest {

@AfterEach
void clear() {
landmarkRepository.deleteAllInBatch();
userRepository.deleteAllInBatch();
}

Expand All @@ -45,38 +46,48 @@ class findNearestLandmark {
@Test
@DisplayName("위치 완전 동일")
void success_1() throws Exception {
// given
Landmark landmark = new Landmark(LandmarkType.경영관, 37.541, 127.079, 100);
landmarkRepository.save(landmark);

// expected
mockMvc.perform(get("/api/landmarks")
.param("latitude", "37.542602")
.param("longitude", "127.078250")
)
.param("latitude", String.valueOf(landmark.getLatitude()))
.param("longitude", String.valueOf(landmark.getLongitude())))
.andExpect(status().isOk())
.andExpect(jsonPath("$.response.distance").value(0.0))
.andExpect(jsonPath("$.response.landmarkId").value(19))
.andExpect(jsonPath("$.response.name").value(LandmarkType.인문학관.name()))
.andExpect(jsonPath("$.response.landmarkId").value(landmark.getId()))
.andExpect(jsonPath("$.response.name").value(landmark.getName().name()))
.andDo(print());
}

@Test
@DisplayName("허용 범위 내에서 조회")
void success_2() throws Exception {
// given
Landmark landmark = new Landmark(LandmarkType.경영관, 37.541, 127.079, 1000);
landmarkRepository.save(landmark);

// expected
mockMvc.perform(get("/api/landmarks")
.param("latitude", "37.542600")
.param("longitude", "127.078200")
)
.param("latitude", String.valueOf(landmark.getLatitude() + 0.0001))
.param("longitude", String.valueOf(landmark.getLongitude())))
.andExpect(status().isOk())
.andExpect(jsonPath("$.response.landmarkId").value(19))
.andExpect(jsonPath("$.response.name").value(LandmarkType.인문학관.name()))
.andExpect(jsonPath("$.response.distance").value(4.4130028352636055))
.andExpect(jsonPath("$.response.landmarkId").value(landmark.getId()))
.andExpect(jsonPath("$.response.name").value(landmark.getName().name()))
.andDo(print());
}

@Test
@DisplayName("허용 범위 밖에서 조회")
void success_3() throws Exception {
// given
Landmark landmark = new Landmark(LandmarkType.경영관, 37.541, 127.079, 100);
landmarkRepository.save(landmark);

// expected
mockMvc.perform(get("/api/landmarks")
.param("latitude", "13")
.param("longitude", "13")
)
.param("longitude", "13"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.response").isEmpty())
.andDo(print());
Expand All @@ -96,28 +107,29 @@ void success_1() throws Exception {
user.updateProfileBadge(BadgeType.ATTENDANCE_50);
userRepository.save(user);

Landmark landmark = landmarkRepository.findById(1L).get();
landmark.updateFirstUser(user, 1000);
long score = 1000;
Landmark landmark = new Landmark(LandmarkType.경영관, 37.541, 127.079, 100);
landmark.updateFirstUser(user, score);
landmarkRepository.save(landmark);

// expected
mockMvc.perform(get("/api/landmarks/{landmarkId}/highest", 1L))
mockMvc.perform(get("/api/landmarks/{landmarkId}/highest", landmark.getId()))
.andExpect(status().isOk())
.andExpect(jsonPath("$.response.score").value(1000))
.andExpect(jsonPath("$.response.score").value(score))
.andExpect(jsonPath("$.response.nickname").value(user.getNickname()))
.andExpect(jsonPath("$.response.profileBadge").value(user.getProfileBadge().name()))
.andDo(print());

// tear down
landmark.deleteRank();
landmarkRepository.save(landmark);
}

@Test
@DisplayName("랜드마크에 최고점 유저가 없다면 빈 응답을 반환한다")
void success_2() throws Exception {
// given
Landmark landmark = new Landmark(LandmarkType.경영관, 37.541, 127.079, 100);
landmarkRepository.save(landmark);

// expected
mockMvc.perform(get("/api/landmarks/{landmarkId}/highest", 1L))
mockMvc.perform(get("/api/landmarks/{landmarkId}/highest", landmark.getId()))
.andExpect(status().isOk())
.andExpect(jsonPath("$.response").isEmpty())
.andDo(print());
Expand Down
Loading

0 comments on commit 54a1a53

Please sign in to comment.