Skip to content

Commit

Permalink
feat : 메시지 큐 모듈 적용 (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
GaBaljaintheroom authored Aug 10, 2024
1 parent 7944955 commit 862cf19
Show file tree
Hide file tree
Showing 26 changed files with 674 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.example.artist.service.dto.response.ArtistFilterTotalCountServiceResponse;
import com.example.artist.service.dto.response.ArtistSubscriptionServiceResponse;
import com.example.artist.service.dto.response.ArtistUnsubscriptionServiceResponse;
import com.example.mq.MessagePublisher;
import com.example.mq.message.ArtistSubscriptionServiceMessage;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.UUID;
Expand All @@ -30,6 +32,7 @@ public class ArtistService {

private final ArtistUseCase artistUseCase;
private final ArtistSubscriptionUseCase artistSubscriptionUseCase;
private final MessagePublisher messagePublisher;

public PaginationServiceResponse<ArtistSearchPaginationServiceParam> searchArtist(
ArtistSearchPaginationServiceRequest request
Expand Down Expand Up @@ -69,6 +72,14 @@ public ArtistSubscriptionServiceResponse subscribe(ArtistSubscriptionServiceRequ
request.userId()
);

var messages = subscriptions.stream()
.map(ArtistSubscriptionServiceMessage::from)
.toList();
messagePublisher.publishArtistSubscription(
"artistSubscription",
messages
);

return ArtistSubscriptionServiceResponse.builder()
.successSubscriptionArtistIds(
subscriptions.stream()
Expand All @@ -85,6 +96,14 @@ public ArtistUnsubscriptionServiceResponse unsubscribe(
request.userId()
);

var messages = unsubscribedArtists.stream()
.map(ArtistSubscriptionServiceMessage::from)
.toList();
messagePublisher.publishArtistSubscription(
"artistUnsubscription",
messages
);

return ArtistUnsubscriptionServiceResponse.builder()
.successUnsubscriptionArtistIds(
unsubscribedArtists.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.example.genre.service.dto.request.GenreUnsubscriptionServiceRequest;
import com.example.genre.service.dto.response.GenreSubscriptionServiceResponse;
import com.example.genre.service.dto.response.GenreUnsubscriptionServiceResponse;
import com.example.mq.MessagePublisher;
import com.example.mq.message.GenreSubscriptionServiceMessage;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
Expand All @@ -25,6 +27,7 @@ public class GenreService {

private final GenreUseCase genreUseCase;
private final GenreSubscriptionUseCase genreSubscriptionUseCase;
private final MessagePublisher messagePublisher;

public GenreSubscriptionServiceResponse subscribe(GenreSubscriptionServiceRequest request) {
List<Genre> existGenresInRequest = genreUseCase.findAllGenresInIds(request.genreIds());
Expand All @@ -37,6 +40,14 @@ public GenreSubscriptionServiceResponse subscribe(GenreSubscriptionServiceReques
request.userId()
);

var messages = subscriptions.stream()
.map(GenreSubscriptionServiceMessage::from)
.toList();
messagePublisher.publishGenreSubscription(
"genreSubscription",
messages
);

return GenreSubscriptionServiceResponse.builder()
.successSubscriptionGenreIds(
subscriptions.stream()
Expand All @@ -54,6 +65,14 @@ public GenreUnsubscriptionServiceResponse unsubscribe(
request.userId()
);

var messages = unsubscriptionGenres.stream()
.map(GenreSubscriptionServiceMessage::from)
.toList();
messagePublisher.publishGenreSubscription(
"genreUnsubscription",
messages
);

return GenreUnsubscriptionServiceResponse.builder()
.successUnsubscriptionGenreIds(
unsubscriptionGenres.stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.mq;

import com.example.mq.message.ArtistSubscriptionServiceMessage;
import com.example.mq.message.GenreSubscriptionServiceMessage;
import com.example.mq.message.ShowRelationArtistAndGenreServiceMessage;
import com.example.mq.message.TicketingReservationServiceMessage;
import java.util.List;

public interface MessagePublisher {

void publishShow(String topic, ShowRelationArtistAndGenreServiceMessage message);

void publishArtistSubscription(String topic, List<ArtistSubscriptionServiceMessage> messages);

void publishGenreSubscription(String topic, List<GenreSubscriptionServiceMessage> messages);

void publishTicketingReservation(String topic, List<TicketingReservationServiceMessage> messages);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.mq.message;

import java.util.UUID;
import lombok.Builder;
import org.example.entity.ArtistSubscription;

@Builder
public record ArtistSubscriptionServiceMessage(
UUID userId,
UUID artistId
) {
public static ArtistSubscriptionServiceMessage from(ArtistSubscription artistSubscription) {
return ArtistSubscriptionServiceMessage.builder()
.userId(artistSubscription.getUserId())
.artistId(artistSubscription.getArtistId())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.mq.message;

import java.util.UUID;
import lombok.Builder;
import org.example.entity.GenreSubscription;

@Builder
public record GenreSubscriptionServiceMessage(
UUID userId,
UUID genreId
) {

public static GenreSubscriptionServiceMessage from(GenreSubscription genreSubscription) {
return GenreSubscriptionServiceMessage.builder()
.userId(genreSubscription.getUserId())
.genreId(genreSubscription.getGenreId())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.mq.message;

import java.util.List;
import java.util.UUID;
import lombok.Builder;

@Builder
public record ShowRelationArtistAndGenreServiceMessage(
List<UUID> artistIds,
List<UUID> genreIds
) {

public static ShowRelationArtistAndGenreServiceMessage of(List<UUID> artistIds, List<UUID> genreIds) {
return ShowRelationArtistAndGenreServiceMessage.builder()
.artistIds(artistIds)
.genreIds(genreIds)
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.mq.message;

import com.example.show.controller.vo.TicketingApiType;
import java.time.LocalDateTime;
import java.util.UUID;

public record TicketingReservationServiceMessage(
LocalDateTime reserveAt,
String showName,
TicketingApiType type,
UUID userId,
UUID showId
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@


import com.example.component.FileUploadComponent;
import com.example.mq.MessagePublisher;
import com.example.mq.message.ShowRelationArtistAndGenreServiceMessage;
import com.example.show.error.ShowError;
import com.example.show.service.dto.request.ShowCreateServiceRequest;
import com.example.show.service.dto.request.ShowUpdateServiceRequest;
Expand All @@ -21,13 +23,22 @@ public class ShowAdminService {

private final ShowUseCase showUseCase;
private final FileUploadComponent fileUploadComponent;
private final MessagePublisher messagePublisher;

public void save(ShowCreateServiceRequest showCreateServiceRequest) {
String imageURL = fileUploadComponent.uploadFile("show", showCreateServiceRequest.post());

showUseCase.save(
showCreateServiceRequest.toDomainRequest(imageURL)
);

messagePublisher.publishShow(
"registerShow",
ShowRelationArtistAndGenreServiceMessage.of(
showCreateServiceRequest.artistIds(),
showCreateServiceRequest.genreIds()
)
);
}

public List<ShowInfoServiceResponse> findAllShowInfos() {
Expand All @@ -51,6 +62,16 @@ public ShowInfoServiceResponse findShowInfo(UUID id) {
public void updateShow(UUID id, ShowUpdateServiceRequest showUpdateServiceRequest) {
String imageUrl = fileUploadComponent.uploadFile("show", showUpdateServiceRequest.post());

var artistIdsToPublish = showUseCase.getArtistIdsToAdd(
showUpdateServiceRequest.artistIds(),
showUseCase.findShowArtistsByShowId(id)
);

var genreIdsToPublish = showUseCase.getGenreIdsToAdd(
showUpdateServiceRequest.genreIds(),
showUseCase.findShowGenresByShowId(id)
);

try {
showUseCase.updateShow(
id,
Expand All @@ -59,6 +80,16 @@ public void updateShow(UUID id, ShowUpdateServiceRequest showUpdateServiceReques
} catch (NoSuchElementException e) {
throw new BusinessException(ShowError.ENTITY_NOT_FOUND);
}

if (!artistIdsToPublish.isEmpty() || !genreIdsToPublish.isEmpty()) {
messagePublisher.publishShow(
"updateShow",
ShowRelationArtistAndGenreServiceMessage.of(
artistIdsToPublish,
genreIdsToPublish
)
);
}
}

public void deleteShow(UUID id) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package artist.service;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import artist.fixture.dto.ArtistRequestDtoFixture;
import artist.fixture.dto.ArtistResponseDtoFixture;
import com.example.artist.service.ArtistService;
import com.example.artist.service.dto.request.ArtistSubscriptionServiceRequest;
import com.example.artist.service.dto.request.ArtistUnsubscriptionServiceRequest;
import com.example.mq.MessagePublisher;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.UUID;
Expand All @@ -28,9 +33,13 @@ class ArtistServiceTest {
private final ArtistSubscriptionUseCase artistSubscriptionUseCase = mock(
ArtistSubscriptionUseCase.class
);

private final MessagePublisher messagePublisher = mock(MessagePublisher.class);

private final ArtistService artistService = new ArtistService(
artistUseCase,
artistSubscriptionUseCase
artistSubscriptionUseCase,
messagePublisher
);

@Test
Expand Down Expand Up @@ -251,6 +260,41 @@ void artistSubscribe() {
);
}

@Test
@DisplayName("아티스트를 구독하면 구독 성공한 아티스트 ID들을 메시지 발행한다.")
void artistSubscribePublishMessage() {
//given
List<UUID> artistsId = List.of(UUID.randomUUID(), UUID.randomUUID());
UUID userId = UUID.randomUUID();
var request = new ArtistSubscriptionServiceRequest(artistsId, userId);
var existArtistsInRequest = ArtistFixture.manSoloArtists(3);
given(
artistUseCase.findAllArtistInIds(request.artistIds())
).willReturn(
existArtistsInRequest
);

var existArtistIdsInRequest = existArtistsInRequest.stream()
.map(Artist::getId)
.toList();
int artistSubscriptionCount = 2;
given(
artistSubscriptionUseCase.subscribe(existArtistIdsInRequest, userId)
).willReturn(
ArtistSubscriptionFixture.artistSubscriptions(artistSubscriptionCount)
);

//when
var result = artistService.subscribe(request);

//then
assertThat(result).isNotNull();
verify(messagePublisher, times(1)).publishArtistSubscription(
eq("artistSubscription"),
anyList()
);
}

@Test
@DisplayName("아티스트를 구독 취소하면 구독 취소 성공한 아티스트 ID들을 반환한다.")
void artistUnsubscribe() {
Expand Down Expand Up @@ -278,6 +322,32 @@ void artistUnsubscribe() {
);
}

@Test
@DisplayName("아티스트를 구독 취소하면 구독 취소 성공한 아티스트 ID들을 메시지 발행한다.")
void artistUnsubscribePublishMessage() {
//given
List<UUID> artistsId = List.of(UUID.randomUUID(), UUID.randomUUID());
UUID userId = UUID.randomUUID();
var request = new ArtistUnsubscriptionServiceRequest(artistsId, userId);
int artistSubscriptionCount = 2;
given(
artistSubscriptionUseCase.unsubscribe(request.artistIds(), userId)
).willReturn(
ArtistSubscriptionFixture.artistSubscriptions(artistSubscriptionCount)
);

//when
var result = artistService.unsubscribe(request);

//then
assertThat(result).isNotNull();
verify(messagePublisher, times(1))
.publishArtistSubscription(
eq("artistUnsubscription"),
anyList()
);
}

@Test
@DisplayName("페이지네이션을 이용해 구독한 아티스트를 반환한다.")
void artistSubscribeWithPagination() {
Expand Down
Loading

0 comments on commit 862cf19

Please sign in to comment.