From 74d0c29a364b0865e5195d6dabdf33903556ca25 Mon Sep 17 00:00:00 2001 From: VasylyshynDmytro Date: Fri, 13 Dec 2024 20:55:06 +0200 Subject: [PATCH] Add test --- .../controller/PlaceControllerTest.java | 10 +++---- .../java/greencity/client/RestClientTest.java | 27 +++++++++++++++---- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/core/src/test/java/greencity/controller/PlaceControllerTest.java b/core/src/test/java/greencity/controller/PlaceControllerTest.java index e559e9c9c..f41689164 100644 --- a/core/src/test/java/greencity/controller/PlaceControllerTest.java +++ b/core/src/test/java/greencity/controller/PlaceControllerTest.java @@ -1,9 +1,11 @@ package greencity.controller; +import greencity.constant.ErrorMessage; import greencity.converters.UserArgumentResolver; import greencity.dto.place.*; import greencity.entity.Place; import greencity.enums.PlaceStatus; +import greencity.exception.exceptions.NotFoundException; import greencity.service.UserService; import java.security.Principal; import java.time.DayOfWeek; @@ -20,7 +22,6 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; -import static org.mockito.Mockito.doNothing; import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.quality.Strictness; @@ -52,19 +53,16 @@ import static greencity.ModelUtils.getUserVO; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static greencity.ModelUtils.getPrincipal; import static greencity.enums.PlaceStatus.APPROVED; import static greencity.enums.PlaceStatus.PROPOSED; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @ExtendWith(MockitoExtension.class) @MockitoSettings(strictness = Strictness.LENIENT) diff --git a/service-api/src/test/java/greencity/client/RestClientTest.java b/service-api/src/test/java/greencity/client/RestClientTest.java index 927147003..6e40ef304 100644 --- a/service-api/src/test/java/greencity/client/RestClientTest.java +++ b/service-api/src/test/java/greencity/client/RestClientTest.java @@ -9,6 +9,7 @@ import static greencity.TestConst.USER_ID; import static greencity.constant.AppConstant.AUTHORIZATION; import greencity.dto.econews.InterestingEcoNewsDto; +import greencity.dto.place.UpdatePlaceStatusWithUserEmailDto; import greencity.dto.user.UserStatusDto; import greencity.dto.user.UserVO; import greencity.dto.user.UserManagementDto; @@ -22,6 +23,7 @@ import greencity.dto.PageableAdvancedDto; import greencity.dto.achievement.UserVOAchievement; import greencity.enums.EmailNotification; +import greencity.enums.PlaceStatus; import greencity.enums.Role; import greencity.enums.UserStatus; import greencity.message.ScheduledEmailMessage; @@ -55,11 +57,8 @@ import org.springframework.web.context.request.RequestContextHolder; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import static org.mockito.Mockito.any; -import static org.mockito.Mockito.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) class RestClientTest { @@ -679,4 +678,22 @@ void sendEmailNotificationPlacesTest() { verify(restTemplate).exchange(GREEN_CITY_USER_ADDRESS + RestTemplateLinks.SEND_SCHEDULED_NOTIFICATION, HttpMethod.POST, entity, Object.class); } + + @Test + void sendEmailNotificationChangesPlaceStatusTest() { + UpdatePlaceStatusWithUserEmailDto message = new UpdatePlaceStatusWithUserEmailDto(); + message.setPlaceName("TestPlace"); + message.setNewStatus(PlaceStatus.APPROVED); + message.setUserName("TestUser"); + message.setEmail("test@example.com"); + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + HttpEntity entity = new HttpEntity<>(message, headers); + String expectedUrl = GREEN_CITY_USER_ADDRESS + RestTemplateLinks.SEND_NOTIFICATION_STATUS_PLACE; + when(restTemplate.exchange(eq(expectedUrl), eq(HttpMethod.POST), any(HttpEntity.class), eq(Object.class))) + .thenReturn(ResponseEntity.ok().build()); + restClient.sendEmailNotificationChangesPlaceStatus(message); + verify(restTemplate, times(1)).exchange(eq(expectedUrl), eq(HttpMethod.POST), any(HttpEntity.class), + eq(Object.class)); + } }