-
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.
* rebase * rebase * rebase * rebase * #53 test(ReviewControllerTest): 테스트 코드 추가 * #53 refactor(ReviewService): 서비스 리팩토링 * #53 refactor(ReviewCommand): dto 리팩토링 * service fix * rebase * build fix
- Loading branch information
Showing
10 changed files
with
242 additions
and
33 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
22 changes: 19 additions & 3 deletions
22
src/main/java/com/example/api/review/ReviewRepository.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 |
---|---|---|
@@ -1,18 +1,34 @@ | ||
package com.example.api.review; | ||
|
||
import com.example.api.domain.Review; | ||
import java.util.List; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
@Repository | ||
public interface ReviewRepository extends JpaRepository<Review, Long> { | ||
|
||
@Query("SELECT r FROM Review r JOIN FETCH r.contract WHERE r.contract.offerEmployment.business.employer.accountId = :employerId") | ||
@Query("SELECT r FROM Review r " + | ||
"JOIN FETCH r.contract c " + | ||
"WHERE c.offerEmployment.business.businessId = :employerId") | ||
List<Review> loadReviewsByEmployerId(@Param("employerId") Long employerId); | ||
|
||
@Query("SELECT r FROM Review r WHERE (:reviewId IS NULL OR r.reviewId = :reviewId)") | ||
@Query("SELECT r FROM Review r " + | ||
"WHERE (:reviewId IS NULL OR r.reviewId = :reviewId)") | ||
List<Review> findReviewsByDynamicQuery(@Param("reviewId") Long reviewId); | ||
|
||
@Query("SELECT r FROM Review r " + | ||
"JOIN FETCH r.writer b " + | ||
"JOIN FETCH r.employee a " + | ||
"JOIN FETCH r.contract c " + | ||
"WHERE a.accountId = :accountId") | ||
List<Review> findReviewsByAccountIdWithDetails(@Param("accountId") Long accountId); | ||
|
||
List<Review> findReviewsByEmployee_AccountId(Long accountId); | ||
} | ||
|
||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.example.api.review.dto; | ||
|
||
public record ReviewCommand( | ||
Long accountId | ||
){} | ||
|
||
|
21 changes: 12 additions & 9 deletions
21
src/main/java/com/example/api/review/dto/ReviewResponse.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 |
---|---|---|
@@ -1,25 +1,28 @@ | ||
package com.example.api.review.dto; | ||
|
||
import com.example.api.domain.Account; | ||
import com.example.api.domain.Business; | ||
import com.example.api.domain.Review; | ||
import java.time.LocalDateTime; | ||
|
||
public record ReviewResponse( | ||
Long reviewId, | ||
Business writer, | ||
Account employee, | ||
Long reviewId, //Id | ||
String businessName, | ||
Long businessId, | ||
LocalDateTime contractStartTime, | ||
LocalDateTime contractEndTime, | ||
int reviewStarPoint, | ||
String reviewContent | ||
|
||
) { | ||
public static ReviewResponse from(Review review) { | ||
public static ReviewResponse from(final Review review) { | ||
return new ReviewResponse( | ||
review.getReviewId(), | ||
review.getWriter(), | ||
review.getEmployee(), | ||
review.getContract().getOfferEmployment().getBusiness().getBusinessName(), | ||
review.getContract().getOfferEmployment().getBusiness().getBusinessId(), | ||
review.getContract().getContractStartTime(), | ||
review.getContract().getContractEndTime(), | ||
review.getReviewStarPoint(), | ||
review.getReviewContent() | ||
); | ||
} | ||
} | ||
|
||
|
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
package com.example.api.inquiry; | ||
|
||
import com.example.api.account.entity.Nationality; | ||
import com.example.api.account.entity.UserRole; | ||
import com.example.api.account.repository.AccountRepository; | ||
import com.example.api.domain.Account; | ||
import com.example.api.domain.Inquiry; | ||
|
@@ -35,18 +37,17 @@ void setUp() { | |
accountRepository.deleteAll(); | ||
inquiryRepository.deleteAll(); | ||
|
||
Account account = new Account(); | ||
account.setAccountId(1L); | ||
account.setName("Alice"); | ||
account.setEmail("[email protected]"); | ||
account.setAge(25); | ||
account.setSex("F"); | ||
account.setPhoneNumber("010-1234-5678"); | ||
account.setProfileImage("user-uploads/1/profile.png"); | ||
account.setStarPoint(4.5f); | ||
account.setWorkCount(10); | ||
account.setOpenStatus(true); | ||
account.setDeleted(false); | ||
Account account = new Account( | ||
"user01", | ||
"password123", | ||
"Alice", | ||
"nickname01", | ||
"010-1234-5678", | ||
"[email protected]", | ||
Nationality.KOREAN, | ||
List.of(UserRole.EMPLOYEE), | ||
false | ||
); | ||
accountRepository.save(account); | ||
} | ||
|
||
|
@@ -111,4 +112,4 @@ void mapToInquiry_shouldMapCommandToInquiry() { | |
assertThat(inquiry).isNotNull(); | ||
assertThat(inquiry.getTitle()).isEqualTo("Test Title"); | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/test/java/com/example/api/review/controller/ReviewControllerTest.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,65 @@ | ||
package com.example.api.review.controller; | ||
|
||
import com.example.api.review.ReviewService; | ||
import com.example.api.review.dto.ReviewCommand; | ||
import com.example.api.review.dto.ReviewResponse; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import static org.mockito.Mockito.*; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
|
||
@WebMvcTest(ReviewController.class) | ||
public class ReviewControllerTest { | ||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@Mock | ||
private ReviewService reviewService; | ||
|
||
@InjectMocks | ||
private ReviewController reviewController; | ||
|
||
private ReviewResponse reviewResponse1; | ||
private ReviewResponse reviewResponse2; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
reviewResponse1 = new ReviewResponse( | ||
"Business A", 101L, LocalDateTime.now(), LocalDateTime.now(), 5, "Great service!"); | ||
reviewResponse2 = new ReviewResponse( | ||
"Business B", 102L, LocalDateTime.now(), LocalDateTime.now(), 4, "Good experience."); | ||
} | ||
|
||
@Test | ||
void testGetMyReviews() throws Exception { | ||
when(reviewService.getReviews(any(ReviewCommand.class))) | ||
.thenReturn(List.of(reviewResponse1, reviewResponse2)); | ||
mockMvc.perform(get("/api/v1/info/my/reviews") | ||
.param("accountId", "123")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.reviews").isArray()) | ||
.andExpect(jsonPath("$.reviews[0].businessName").value("Business A")) | ||
.andExpect(jsonPath("$.reviews[1].reviewContent").value("Good experience.")); | ||
} | ||
|
||
@Test | ||
void testGetMyReviewsNoData() throws Exception { | ||
when(reviewService.getReviews(any(ReviewCommand.class))) | ||
.thenReturn(Collections.emptyList()); | ||
mockMvc.perform(get("/api/v1/info/my/reviews") | ||
.param("accountId", "123")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.reviews").isEmpty()); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/test/java/com/example/api/review/service/ReviewServiceTest.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,41 @@ | ||
package com.example.api.review.service; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.example.api.global.BaseIntegrationTest; | ||
import com.example.api.review.ReviewService; | ||
import com.example.api.review.dto.ReviewCommand; | ||
import com.example.api.review.dto.ReviewResponse; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import java.util.List; | ||
|
||
@SpringBootTest | ||
public class ReviewServiceTest extends BaseIntegrationTest { | ||
@Autowired | ||
private ReviewService reviewService; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
} | ||
|
||
@Test | ||
void testGetReviews() { | ||
ReviewCommand reviewCommand = new ReviewCommand(1L); | ||
List<ReviewResponse> reviews = reviewService.getReviews(reviewCommand); | ||
assertThat(reviews).isNotEmpty(); | ||
assertThat(reviews.get(0).businessName()).isEqualTo("Tech Solutions Inc."); | ||
assertThat(reviews.get(0).reviewContent()).isEqualTo("Good work"); | ||
} | ||
|
||
@Test | ||
void testGetReviewsWhenNoReviews() { | ||
ReviewCommand reviewCommand = new ReviewCommand(999L); | ||
List<ReviewResponse> reviews = reviewService.getReviews(reviewCommand); | ||
assertThat(reviews).isEmpty(); | ||
} | ||
} | ||
|