Skip to content
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
1 change: 0 additions & 1 deletion src/main/java/org/runimo/runimo/user/domain/EggStatus.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.runimo.runimo.user.domain;

public enum EggStatus {
WAITING, // 부화 대기
INCUBATING, // 부화 중
INCUBATED, // 부화 완료 대기
HATCHED // 부화 완료
Expand Down
25 changes: 12 additions & 13 deletions src/main/java/org/runimo/runimo/user/domain/IncubatingEgg.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,23 @@ public class IncubatingEgg extends BaseEntity {

@Column(name = "egg_status", nullable = false)
@Enumerated(EnumType.STRING)
private EggStatus status;
private EggStatus status = EggStatus.INCUBATING;

@Builder
public IncubatingEgg(Long userId, Long eggId, Long currentLovePointAmount,
Long hatchRequireAmount, EggStatus status) {
validateCreation(status, currentLovePointAmount, hatchRequireAmount);
validateCreation(currentLovePointAmount, hatchRequireAmount);
Objects.requireNonNull(userId);
Objects.requireNonNull(eggId);
Objects.requireNonNull(currentLovePointAmount);
Objects.requireNonNull(hatchRequireAmount);
this.userId = userId;
this.eggId = eggId;
this.currentLovePointAmount = currentLovePointAmount;
this.hatchRequireAmount = hatchRequireAmount;
this.status = status;
}

public void startIncubation() {
validateStateTransition(EggStatus.WAITING, "부화 대기중인 알에만 부화 시작가능");
this.status = EggStatus.INCUBATING;
if(status != null) {
this.status = status;
}
}

public void gainLovePoint(final Long amount) {
Expand All @@ -69,19 +70,17 @@ public void hatch() {
this.status = EggStatus.HATCHED;
}

private void validateCreation(final EggStatus status, final Long currentLovePointAmount,
private void validateCreation(final Long currentLovePointAmount,
final Long hatchRequireAmount) {
if (status == EggStatus.WAITING && currentLovePointAmount != 0) {
throw new IllegalArgumentException(
"부화 대기중인 알은 애정 포인트가 0이어야 합니다. 현재: " + currentLovePointAmount);
if(currentLovePointAmount < 0) {
throw new IllegalArgumentException("보유한 애정 포인트는 0보다 작을 수 없습니다.");
}
if (hatchRequireAmount <= 0) {
throw new IllegalArgumentException(
"부화에 필요한 애정 포인트는 0보다 커야 합니다. 현재: " + hatchRequireAmount);
}
}


private void validateStateTransition(final EggStatus expected, final String message) {
if (status != expected) {
throw new IllegalStateException(message + " 현재 상태: " + status);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.RequiredArgsConstructor;
import org.runimo.runimo.item.domain.Egg;
import org.runimo.runimo.user.domain.EggStatus;
import org.runimo.runimo.user.domain.IncubatingEgg;
import org.runimo.runimo.user.repository.IncubatingEggRepository;
import org.runimo.runimo.user.service.dtos.UseLovePointCommand;
Expand All @@ -21,6 +22,7 @@ public IncubatingEgg create(Long userId, Egg egg) {
.eggId(egg.getId())
.currentLovePointAmount(0L)
.hatchRequireAmount(egg.getHatchRequireAmount())
.status(EggStatus.INCUBATING)
.build();
return incubatingEggRepository.save(incubatingEgg);
}
Expand Down
50 changes: 0 additions & 50 deletions src/test/java/org/runimo/runimo/item/domain/IncubatingEggTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,6 @@

class IncubatingEggTest {

@Test
void startIncubation_성공() {
// given
IncubatingEgg incubatingEgg = IncubatingEgg.builder()
.userId(1L)
.eggId(100L)
.currentLovePointAmount(0L)
.hatchRequireAmount(10L)
.status(EggStatus.WAITING)
.build();

assertEquals(EggStatus.WAITING, incubatingEgg.getStatus());

// when
incubatingEgg.startIncubation();

// then
assertEquals(EggStatus.INCUBATING, incubatingEgg.getStatus());
}

@Test
void startIncubation_실패_이미_부화중() {
// given
IncubatingEgg incubatingEgg = IncubatingEgg.builder()
.userId(1L)
.eggId(100L)
.currentLovePointAmount(2L)
.hatchRequireAmount(10L)
.status(EggStatus.INCUBATING)
.build();

// when & then
assertThrows(IllegalStateException.class, incubatingEgg::startIncubation);
}

@Test
void gainLovePoint_성공() {
// given
Expand Down Expand Up @@ -129,21 +94,6 @@ class IncubatingEggTest {
assertThrows(IllegalStateException.class, incubatingEgg::hatch);
}

@Test
void hatch_실패_부화_대기중인_알() {
// given
IncubatingEgg incubatingEgg = IncubatingEgg.builder()
.userId(1L)
.eggId(100L)
.currentLovePointAmount(0L)
.hatchRequireAmount(10L)
.status(EggStatus.WAITING)
.build();

// when & then
assertThrows(IllegalStateException.class, incubatingEgg::hatch);
}

@Test
void gainLovePoint_과다_지급() {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.notNullValue;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -243,4 +244,57 @@ void tearDown() {
.statusCode(200)
.body("payload.love_point", equalTo(initialLovePoint));
}

@Test
@Sql(scripts = "/sql/user_item_test_data.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
void 알_등록_후_조회() throws JsonProcessingException {
String token = "Bearer " + jwtTokenFactory.generateAccessToken("test-user-uuid-1");

Integer eggId =
given()
.header("Authorization", token)
.contentType(ContentType.JSON)

.when()
.get("/api/v1/users/eggs")

.then()
.log().ifError()
.statusCode(200)
.body("payload.items", notNullValue())
.extract()
.path("payload.items[0].item_id");


RegisterEggRequest request = new RegisterEggRequest((long)eggId);

given()
.header("Authorization", token)
.contentType(ContentType.JSON)
.body(objectMapper.writeValueAsString(request))

.when()
.post("/api/v1/users/eggs")

.then()
.statusCode(201)
.log().ifError()
.body("payload.incubating_egg_id", notNullValue())
.body("payload.current_love_point_amount", equalTo(0));

given()
.header("Authorization", token)
.contentType(ContentType.JSON)

.when()
.get("/api/v1/users/eggs/incubators")
.then()
.log().all()
.statusCode(200)
.body("payload.incubating_eggs", notNullValue())
.body("payload.incubating_eggs[0].name", notNullValue());



}
}