-
Notifications
You must be signed in to change notification settings - Fork 416
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
loannguyent5
committed
Oct 31, 2024
1 parent
a5e7436
commit b8caa56
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
order/src/test/java/com/yas/order/service/PromotionServiceTest.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,74 @@ | ||
package com.yas.order.service; | ||
|
||
import com.yas.order.config.ServiceUrlConfig; | ||
import com.yas.order.viewmodel.promotion.PromotionUsageVm; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
import org.springframework.web.client.RestClient; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
|
||
import static com.yas.order.utils.SecurityContextUtils.setUpSecurityContext; | ||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class PromotionServiceTest { | ||
|
||
private RestClient restClient; | ||
|
||
private ServiceUrlConfig serviceUrlConfig; | ||
|
||
private PromotionService promotionService; | ||
|
||
private RestClient.ResponseSpec responseSpec; | ||
|
||
private static final String PROMOTION_URL = "http://api.yas.local/promotion"; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
restClient = mock(RestClient.class); | ||
serviceUrlConfig = mock(ServiceUrlConfig.class); | ||
promotionService = new PromotionService(restClient, serviceUrlConfig); | ||
responseSpec = Mockito.mock(RestClient.ResponseSpec.class); | ||
setUpSecurityContext("test"); | ||
when(serviceUrlConfig.promotion()).thenReturn(PROMOTION_URL); | ||
} | ||
|
||
@Test | ||
void testUpdateUsagePromotion_ifNormalCase_shouldNoException() { | ||
|
||
List<PromotionUsageVm> promotionUsageVms = getPromotionUsageVms(); | ||
RestClient.RequestBodyUriSpec requestBodyUriSpec = mock(RestClient.RequestBodyUriSpec.class); | ||
|
||
when(restClient.post()).thenReturn(requestBodyUriSpec); | ||
when(requestBodyUriSpec.uri(any(URI.class))).thenReturn(requestBodyUriSpec); | ||
when(requestBodyUriSpec.headers(any())).thenReturn(requestBodyUriSpec); | ||
when(requestBodyUriSpec.body(any(Object.class))).thenReturn(requestBodyUriSpec); | ||
when(requestBodyUriSpec.retrieve()).thenReturn(responseSpec); | ||
|
||
assertDoesNotThrow(() -> promotionService.updateUsagePromotion(promotionUsageVms)); | ||
} | ||
|
||
private static @NotNull List<PromotionUsageVm> getPromotionUsageVms() { | ||
return List.of( | ||
PromotionUsageVm.builder() | ||
.promotionCode("123") | ||
.userId("user123") | ||
.orderId(1001L) | ||
.productId(5001L) | ||
.build(), | ||
|
||
PromotionUsageVm.builder() | ||
.promotionCode("1234") | ||
.userId("user456") | ||
.orderId(1002L) | ||
.productId(5002L) | ||
.build() | ||
); | ||
} | ||
} |