-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from playkuround/refactor/adventure
Refactor/adventure
- Loading branch information
Showing
6 changed files
with
205 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/test/java/com/playkuround/playkuroundserver/IntegrationServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.playkuround.playkuroundserver; | ||
|
||
import org.springframework.boot.autoconfigure.domain.EntityScan; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@SpringBootTest(properties = "spring.profiles.active=test") | ||
@EntityScan(basePackages = {"com.playkuround.playkuroundserver.domain"}) | ||
public @interface IntegrationServiceTest { | ||
} | ||
|
157 changes: 157 additions & 0 deletions
157
.../com/playkuround/playkuroundserver/domain/adventure/application/AdventureServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package com.playkuround.playkuroundserver.domain.adventure.application; | ||
|
||
import com.playkuround.playkuroundserver.IntegrationServiceTest; | ||
import com.playkuround.playkuroundserver.TestUtil; | ||
import com.playkuround.playkuroundserver.domain.adventure.dao.AdventureRepository; | ||
import com.playkuround.playkuroundserver.domain.adventure.domain.Adventure; | ||
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.exception.LandmarkNotFoundException; | ||
import com.playkuround.playkuroundserver.domain.score.domain.ScoreType; | ||
import com.playkuround.playkuroundserver.domain.user.dao.UserRepository; | ||
import com.playkuround.playkuroundserver.domain.user.domain.Major; | ||
import com.playkuround.playkuroundserver.domain.user.domain.User; | ||
import com.playkuround.playkuroundserver.global.util.Location; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
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; | ||
|
||
@AfterEach | ||
void clean() { | ||
badgeRepository.deleteAllInBatch(); | ||
adventureRepository.deleteAllInBatch(); | ||
landmarkRepository.deleteAllInBatch(); | ||
userRepository.deleteAllInBatch(); | ||
redisTemplate.delete(redisSetKey); | ||
} | ||
|
||
@Test | ||
@DisplayName("탐험을 하게 되면 total score 증가, adventure 저장, 랜드마크별 최고기록과 유저별 게임 최고기록이 업데이트 된다.") | ||
void saveAdventure_1() { | ||
// given | ||
User user = TestUtil.createUser(); | ||
|
||
Landmark landmark = landmarkRepository.findById(3L).get(); | ||
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); | ||
|
||
// then | ||
// Total Score 저장 및 최고 점수 갱신 | ||
List<User> users = userRepository.findAll(); | ||
assertThat(users).hasSize(1) | ||
.extracting("highestScore.highestCardScore") | ||
.containsExactly(adventureSaveDto.score()); | ||
|
||
// adventure 저장 | ||
List<Adventure> adventures = adventureRepository.findAll(); | ||
assertThat(adventures).hasSize(1) | ||
.extracting("score", "scoreType", "user.id", "landmark.id") | ||
.containsOnly(tuple(adventureSaveDto.score(), adventureSaveDto.scoreType(), users.get(0).getId(), landmark.getId())); | ||
|
||
// 랜드마크 최고 점수 갱신 | ||
Optional<Landmark> optionalLandmark = landmarkRepository.findById(landmark.getId()); | ||
assertThat(optionalLandmark).isPresent() | ||
.get() | ||
.extracting("highestScore", "firstUser.id") | ||
.contains(adventureSaveDto.score(), users.get(0).getId()); | ||
} | ||
|
||
@Test | ||
@DisplayName("랜드마크가 존재하지 않으면 에러가 발생한다.") | ||
void saveAdventure_2() { | ||
// given | ||
User user = TestUtil.createUser(); | ||
|
||
Landmark landmark = landmarkRepository.findById(3L).get(); | ||
Location location = new Location(landmark.getLatitude(), landmark.getLongitude()); | ||
AdventureSaveDto adventureSaveDto = new AdventureSaveDto(user, -1L, location, 100, ScoreType.BOOK); | ||
|
||
// expected | ||
assertThatThrownBy(() -> adventureService.saveAdventure(adventureSaveDto)) | ||
.isInstanceOf(LandmarkNotFoundException.class) | ||
.hasMessage("-1의 랜드마크 조회에 실패하였습니다."); | ||
|
||
List<Adventure> adventures = adventureRepository.findAll(); | ||
assertThat(adventures).isEmpty(); | ||
} | ||
|
||
@Test | ||
@DisplayName("인식 거리 밖에 있으면 에러가 발생한다.") | ||
void saveAdventure_3() { | ||
// given | ||
User user = TestUtil.createUser(); | ||
|
||
Landmark landmark = landmarkRepository.findById(3L).get(); | ||
Location location = new Location(0, 0); | ||
AdventureSaveDto adventureSaveDto = new AdventureSaveDto(user, landmark.getId(), location, 100, ScoreType.BOOK); | ||
|
||
// when | ||
assertThatThrownBy(() -> adventureService.saveAdventure(adventureSaveDto)) | ||
.isInstanceOf(InvalidLandmarkLocationException.class) | ||
.hasMessage("현재 위치와 랜드마크 위치가 너무 멉니다."); | ||
|
||
List<Adventure> adventures = adventureRepository.findAll(); | ||
assertThat(adventures).isEmpty(); | ||
} | ||
|
||
@Test | ||
@DisplayName("랜드마크 최고 기록자가 아니라면, 랜드마크 랭킹 1위는 업데이트 되지 않는다.") | ||
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.updateFirstUser(otherUser, highestScore); | ||
landmarkRepository.save(landmark); // update | ||
|
||
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); | ||
|
||
// then | ||
Optional<Landmark> optionalLandmark = landmarkRepository.findById(landmark.getId()); | ||
assertThat(optionalLandmark).isPresent() | ||
.get() | ||
.extracting("highestScore", "firstUser.id") | ||
.contains(highestScore, otherUser.getId()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters