diff --git a/src/main/java/com/aloc/aloc/admin/controller/AdminController.java b/src/main/java/com/aloc/aloc/admin/controller/AdminController.java index 90d0eced..e4d9d241 100644 --- a/src/main/java/com/aloc/aloc/admin/controller/AdminController.java +++ b/src/main/java/com/aloc/aloc/admin/controller/AdminController.java @@ -12,8 +12,8 @@ import com.aloc.aloc.course.dto.response.CourseResponseDto; import com.aloc.aloc.global.apipayload.CustomApiResponse; import com.aloc.aloc.global.apipayload.status.SuccessStatus; +import com.aloc.aloc.profilebackgroundcolor.dto.response.ColorResponseDto; import com.aloc.aloc.profilebackgroundcolor.service.ProfileBackgroundColorService; -import com.aloc.aloc.user.dto.response.ColorResponseDto; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.media.ArraySchema; diff --git a/src/main/java/com/aloc/aloc/global/init/DevDataInitializer.java b/src/main/java/com/aloc/aloc/global/init/DevDataInitializer.java index fc602b03..4bb06200 100644 --- a/src/main/java/com/aloc/aloc/global/init/DevDataInitializer.java +++ b/src/main/java/com/aloc/aloc/global/init/DevDataInitializer.java @@ -1,6 +1,6 @@ package com.aloc.aloc.global.init; -import com.aloc.aloc.profilebackgroundcolor.ProfileBackgroundColor; +import com.aloc.aloc.profilebackgroundcolor.entity.ProfileBackgroundColor; import com.aloc.aloc.profilebackgroundcolor.repository.ProfileBackgroundColorRepository; import java.util.List; import lombok.RequiredArgsConstructor; diff --git a/src/main/java/com/aloc/aloc/user/dto/response/ColorResponseDto.java b/src/main/java/com/aloc/aloc/profilebackgroundcolor/dto/response/ColorResponseDto.java similarity index 90% rename from src/main/java/com/aloc/aloc/user/dto/response/ColorResponseDto.java rename to src/main/java/com/aloc/aloc/profilebackgroundcolor/dto/response/ColorResponseDto.java index 603b8e5f..679ea467 100644 --- a/src/main/java/com/aloc/aloc/user/dto/response/ColorResponseDto.java +++ b/src/main/java/com/aloc/aloc/profilebackgroundcolor/dto/response/ColorResponseDto.java @@ -1,6 +1,6 @@ -package com.aloc.aloc.user.dto.response; +package com.aloc.aloc.profilebackgroundcolor.dto.response; -import com.aloc.aloc.profilebackgroundcolor.ProfileBackgroundColor; +import com.aloc.aloc.profilebackgroundcolor.entity.ProfileBackgroundColor; import io.swagger.v3.oas.annotations.media.Schema; import lombok.Builder; import lombok.Getter; diff --git a/src/main/java/com/aloc/aloc/profilebackgroundcolor/ProfileBackgroundColor.java b/src/main/java/com/aloc/aloc/profilebackgroundcolor/entity/ProfileBackgroundColor.java similarity index 70% rename from src/main/java/com/aloc/aloc/profilebackgroundcolor/ProfileBackgroundColor.java rename to src/main/java/com/aloc/aloc/profilebackgroundcolor/entity/ProfileBackgroundColor.java index 751e347b..b67f035d 100644 --- a/src/main/java/com/aloc/aloc/profilebackgroundcolor/ProfileBackgroundColor.java +++ b/src/main/java/com/aloc/aloc/profilebackgroundcolor/entity/ProfileBackgroundColor.java @@ -1,4 +1,4 @@ -package com.aloc.aloc.profilebackgroundcolor; +package com.aloc.aloc.profilebackgroundcolor.entity; import jakarta.persistence.Column; import jakarta.persistence.Entity; @@ -8,6 +8,10 @@ import lombok.Getter; import lombok.NoArgsConstructor; +// 프로필 배경 색상 엔티티 +// color1, color2, color3, color4, color5 : 색상 코드 +// type : common, rare, special +// degree : CSS 그라데이션 각도, 135, 180을 주로 사용 @Entity @Getter @Builder diff --git a/src/main/java/com/aloc/aloc/profilebackgroundcolor/repository/ProfileBackgroundColorRepository.java b/src/main/java/com/aloc/aloc/profilebackgroundcolor/repository/ProfileBackgroundColorRepository.java index 695f657e..40d60134 100644 --- a/src/main/java/com/aloc/aloc/profilebackgroundcolor/repository/ProfileBackgroundColorRepository.java +++ b/src/main/java/com/aloc/aloc/profilebackgroundcolor/repository/ProfileBackgroundColorRepository.java @@ -1,11 +1,16 @@ package com.aloc.aloc.profilebackgroundcolor.repository; -import com.aloc.aloc.profilebackgroundcolor.ProfileBackgroundColor; +import com.aloc.aloc.profilebackgroundcolor.entity.ProfileBackgroundColor; import java.util.List; import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +// 프로필 배경 색상 레포지토리 + +// 조회 +// 1. findByName 이름으로 조회 +// 2. findByType 타입으로 조회 @Repository public interface ProfileBackgroundColorRepository extends JpaRepository { diff --git a/src/main/java/com/aloc/aloc/profilebackgroundcolor/service/ProfileBackgroundColorService.java b/src/main/java/com/aloc/aloc/profilebackgroundcolor/service/ProfileBackgroundColorService.java index a86b1f73..7c84c3f0 100644 --- a/src/main/java/com/aloc/aloc/profilebackgroundcolor/service/ProfileBackgroundColorService.java +++ b/src/main/java/com/aloc/aloc/profilebackgroundcolor/service/ProfileBackgroundColorService.java @@ -1,14 +1,18 @@ package com.aloc.aloc.profilebackgroundcolor.service; -import com.aloc.aloc.profilebackgroundcolor.ProfileBackgroundColor; +import com.aloc.aloc.profilebackgroundcolor.dto.response.ColorResponseDto; +import com.aloc.aloc.profilebackgroundcolor.entity.ProfileBackgroundColor; import com.aloc.aloc.profilebackgroundcolor.repository.ProfileBackgroundColorRepository; -import com.aloc.aloc.user.dto.response.ColorResponseDto; import com.aloc.aloc.user.repository.UserRepository; import java.util.List; import java.util.Random; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +// 프로필 배경 색상 서비스 +// 1. getColorByName 이름으로 조회 +// 2. pickColor 랜덤 컬러 선택 +// 3. getAllColors 모든 컬러 조회 @Service @RequiredArgsConstructor public class ProfileBackgroundColorService { @@ -17,12 +21,18 @@ public class ProfileBackgroundColorService { private static final int COLOR_CHANGE_MONEY = 100; + // 1. getColorByName 이름으로 조회 + // input : 컬러 이름 + // output : 컬러 이름에 해당하는 컬러 public ProfileBackgroundColor getColorByName(String name) { return profileBackgroundColorRepository .findByName(name) .orElseThrow(() -> new IllegalArgumentException("해당 컬러가 없습니다. " + name)); } + // 2. pickColor 랜덤 컬러 선택 + // input : 없음 + // output : 랜덤 컬러 이름 public String pickColor() { Random random = new Random(); int draw = random.nextInt(100) + 1; @@ -41,6 +51,9 @@ public String pickColor() { .getName(); } + // 3. getAllColors 모든 컬러 조회 + // input : 없음 + // output : 모든 컬러 리스트 public List getAllColors() { return profileBackgroundColorRepository.findAll().stream().map(ColorResponseDto::of).toList(); } diff --git a/src/main/java/com/aloc/aloc/user/dto/response/UserColorChangeResponseDto.java b/src/main/java/com/aloc/aloc/user/dto/response/UserColorChangeResponseDto.java index 239dbdd2..433daf24 100644 --- a/src/main/java/com/aloc/aloc/user/dto/response/UserColorChangeResponseDto.java +++ b/src/main/java/com/aloc/aloc/user/dto/response/UserColorChangeResponseDto.java @@ -1,6 +1,7 @@ package com.aloc.aloc.user.dto.response; -import com.aloc.aloc.profilebackgroundcolor.ProfileBackgroundColor; +import com.aloc.aloc.profilebackgroundcolor.dto.response.ColorResponseDto; +import com.aloc.aloc.profilebackgroundcolor.entity.ProfileBackgroundColor; import io.swagger.v3.oas.annotations.media.Schema; import lombok.AllArgsConstructor; import lombok.Builder; diff --git a/src/main/java/com/aloc/aloc/user/dto/response/UserDetailResponseDto.java b/src/main/java/com/aloc/aloc/user/dto/response/UserDetailResponseDto.java index 2e8e7f53..9d2b74bd 100644 --- a/src/main/java/com/aloc/aloc/user/dto/response/UserDetailResponseDto.java +++ b/src/main/java/com/aloc/aloc/user/dto/response/UserDetailResponseDto.java @@ -1,6 +1,7 @@ package com.aloc.aloc.user.dto.response; -import com.aloc.aloc.profilebackgroundcolor.ProfileBackgroundColor; +import com.aloc.aloc.profilebackgroundcolor.dto.response.ColorResponseDto; +import com.aloc.aloc.profilebackgroundcolor.entity.ProfileBackgroundColor; import com.aloc.aloc.user.entity.User; import io.swagger.v3.oas.annotations.media.Schema; import java.time.LocalDateTime; diff --git a/src/main/java/com/aloc/aloc/user/service/UserService.java b/src/main/java/com/aloc/aloc/user/service/UserService.java index 648f6b0a..060eed51 100644 --- a/src/main/java/com/aloc/aloc/user/service/UserService.java +++ b/src/main/java/com/aloc/aloc/user/service/UserService.java @@ -1,6 +1,6 @@ package com.aloc.aloc.user.service; -import com.aloc.aloc.profilebackgroundcolor.ProfileBackgroundColor; +import com.aloc.aloc.profilebackgroundcolor.entity.ProfileBackgroundColor; import com.aloc.aloc.profilebackgroundcolor.repository.ProfileBackgroundColorRepository; import com.aloc.aloc.profilebackgroundcolor.service.ProfileBackgroundColorService; import com.aloc.aloc.scraper.BaekjoonRankScrapingService; diff --git a/src/main/java/com/aloc/aloc/user/service/mapper/UserMapper.java b/src/main/java/com/aloc/aloc/user/service/mapper/UserMapper.java index be1480c3..d3b1a622 100644 --- a/src/main/java/com/aloc/aloc/user/service/mapper/UserMapper.java +++ b/src/main/java/com/aloc/aloc/user/service/mapper/UserMapper.java @@ -1,6 +1,6 @@ package com.aloc.aloc.user.service.mapper; -import com.aloc.aloc.profilebackgroundcolor.ProfileBackgroundColor; +import com.aloc.aloc.profilebackgroundcolor.entity.ProfileBackgroundColor; import com.aloc.aloc.profilebackgroundcolor.service.ProfileBackgroundColorService; import com.aloc.aloc.user.dto.response.UserDetailResponseDto; import com.aloc.aloc.user.entity.User; diff --git a/src/test/java/com/aloc/aloc/AlocApplicationTests.java b/src/test/java/com/aloc/aloc/AlocApplicationTests.java index 7fa2dc2a..b605ab6e 100644 --- a/src/test/java/com/aloc/aloc/AlocApplicationTests.java +++ b/src/test/java/com/aloc/aloc/AlocApplicationTests.java @@ -2,8 +2,10 @@ import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; @SpringBootTest +@ActiveProfiles("test") class AlocApplicationTests { @Test diff --git a/src/test/java/com/aloc/aloc/common/fixture/TestFixture.java b/src/test/java/com/aloc/aloc/common/fixture/TestFixture.java index 8c307903..679f3cd9 100644 --- a/src/test/java/com/aloc/aloc/common/fixture/TestFixture.java +++ b/src/test/java/com/aloc/aloc/common/fixture/TestFixture.java @@ -11,6 +11,8 @@ import com.aloc.aloc.problem.dto.response.ProblemSolvedResponseDto; import com.aloc.aloc.problem.entity.Problem; import com.aloc.aloc.problem.enums.UserCourseProblemStatus; +import com.aloc.aloc.profilebackgroundcolor.dto.response.ColorResponseDto; +import com.aloc.aloc.profilebackgroundcolor.entity.ProfileBackgroundColor; import com.aloc.aloc.user.dto.response.UserDetailResponseDto; import com.aloc.aloc.user.entity.User; import com.aloc.aloc.user.enums.Authority; @@ -143,4 +145,50 @@ public static UserCourseProblem getMockUserCourseProblem( .problemOrder(problemOrder) .build(); } + + // ProfileBackgroundColor 관련 테스트 픽스처 + public static ProfileBackgroundColor getMockProfileBackgroundColor() { + return ProfileBackgroundColor.builder() + .name("TestColor") + .color1("#0000FF") + .color2("#87CEEB") + .color3(null) + .color4(null) + .color5(null) + .type("common") + .degree(135) + .build(); + } + + public static ProfileBackgroundColor getMockProfileBackgroundColorByType(String type) { + return ProfileBackgroundColor.builder() + .name("Test" + type) + .color1("#FF0000") + .color2("#00FF00") + .color3("#0000FF") + .color4(null) + .color5(null) + .type(type) + .degree(180) + .build(); + } + + public static ProfileBackgroundColor getMockProfileBackgroundColorByName( + String name, String type) { + return ProfileBackgroundColor.builder() + .name(name) + .color1("#FF5A5A") + .color2("#FFB800") + .color3(null) + .color4(null) + .color5(null) + .type(type) + .degree(135) + .build(); + } + + public static ColorResponseDto getMockColorResponseDto() { + ProfileBackgroundColor color = getMockProfileBackgroundColor(); + return ColorResponseDto.of(color); + } } diff --git a/src/test/java/com/aloc/aloc/profilebackgroundcolor/service/ProfileBackgroundColorServiceTest.java b/src/test/java/com/aloc/aloc/profilebackgroundcolor/service/ProfileBackgroundColorServiceTest.java new file mode 100644 index 00000000..00071043 --- /dev/null +++ b/src/test/java/com/aloc/aloc/profilebackgroundcolor/service/ProfileBackgroundColorServiceTest.java @@ -0,0 +1,70 @@ +package com.aloc.aloc.profilebackgroundcolor.service; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.verify; + +import com.aloc.aloc.common.fixture.TestFixture; +import com.aloc.aloc.profilebackgroundcolor.entity.ProfileBackgroundColor; +import com.aloc.aloc.profilebackgroundcolor.repository.ProfileBackgroundColorRepository; +import java.util.Optional; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +// 테스트 함수 목록 +// [getColorByName] 정상 케이스 - 존재하는 색상 이름으로 조회 +// [getColorByName] 예외 케이스 - 존재하지 않는 색상 이름으로 조회 + +@ExtendWith(MockitoExtension.class) +public class ProfileBackgroundColorServiceTest { + + @Mock private ProfileBackgroundColorRepository profileBackgroundColorRepository; + + @InjectMocks private ProfileBackgroundColorService profileBackgroundColorService; + + // [getColorByName] 정상 케이스 - 존재하는 색상 이름으로 조회 + @Test + void getColorByNameNormalCase() { + // given + String colorName = "TestColor"; + ProfileBackgroundColor expectedColor = TestFixture.getMockProfileBackgroundColor(); + given(profileBackgroundColorRepository.findByName(colorName)) + .willReturn(Optional.of(expectedColor)); + + // when + ProfileBackgroundColor result = profileBackgroundColorService.getColorByName(colorName); + + // then + assertThat(result).isEqualTo(expectedColor); + assertThat(result.getName()).isEqualTo(colorName); + assertThat(result.getType()).isEqualTo(expectedColor.getType()); + assertThat(result.getColor1()).isEqualTo(expectedColor.getColor1()); + assertThat(result.getColor2()).isEqualTo(expectedColor.getColor2()); + assertThat(result.getColor3()).isEqualTo(expectedColor.getColor3()); + assertThat(result.getColor4()).isEqualTo(expectedColor.getColor4()); + assertThat(result.getColor5()).isEqualTo(expectedColor.getColor5()); + assertThat(result.getDegree()).isEqualTo(expectedColor.getDegree()); + verify(profileBackgroundColorRepository).findByName(colorName); + } + + // [getColorByName] 예외 케이스 - 존재하지 않는 색상 이름으로 조회 + @Test + void getColorByNameExceptionCase() { + // given + String nonExistentColorName = "NonExistentColor"; + given(profileBackgroundColorRepository.findByName(nonExistentColorName)) + .willReturn(Optional.empty()); + + // when + assertThatThrownBy(() -> profileBackgroundColorService.getColorByName(nonExistentColorName)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("해당 컬러가 없습니다. " + nonExistentColorName); + + // then + verify(profileBackgroundColorRepository).findByName(nonExistentColorName); + } +}