-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tests for service layer #954
base: dev
Are you sure you want to change the base?
Changes from 29 commits
de942db
67c5f3b
413fd40
a401d59
9645e31
a206824
ad07006
9511661
8c7676d
d1ced5b
c1fe53a
c0608c9
5989cbf
58e93b4
5e67e30
d90eaca
98f2ae7
d4727ca
9016ba1
b54037c
b284bb9
eeb1fd3
ee7323d
0996600
2eb82d5
e668ba9
38ae02a
f9c19f6
0a6ecc6
c6eea78
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package greencity.service.ubs; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import greencity.dto.order.UserWithSomeOrderDetailDto; | ||
import greencity.dto.pageble.PageableDto; | ||
import greencity.enums.SortingOrder; | ||
|
@@ -50,7 +51,8 @@ public PageableDto<UserWithSomeOrderDetailDto> getAllFields(CustomerPage page, S | |
users.getPageable().getPageNumber(), users.getTotalPages()); | ||
} | ||
|
||
private UserWithSomeOrderDetailDto mapToDto(User u) { | ||
@VisibleForTesting | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need it here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
UserWithSomeOrderDetailDto mapToDto(User u) { | ||
final UserWithSomeOrderDetailDto allFieldsFromTableDto = new UserWithSomeOrderDetailDto(); | ||
StringBuilder name = new StringBuilder(); | ||
allFieldsFromTableDto.setUserId(u.getId()); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package greencity.service.ubs; | ||
|
||
import com.azure.storage.blob.BlobClient; | ||
import com.azure.storage.blob.BlobContainerClient; | ||
import greencity.constant.ErrorMessage; | ||
import greencity.exceptions.BadRequestException; | ||
import greencity.exceptions.image.FileNotSavedException; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.core.env.PropertyResolver; | ||
import org.springframework.mock.env.MockEnvironment; | ||
import org.springframework.mock.web.MockMultipartFile; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.ArgumentMatchers.anyLong; | ||
import static org.mockito.Mockito.doAnswer; | ||
import static org.mockito.Mockito.doNothing; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
import static org.mockito.Mockito.doReturn; | ||
import static org.mockito.Mockito.spy; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class AzureCloudStorageServiceTest { | ||
|
||
private final String connectionString = | ||
"DefaultEndpointsProtocol=https;AccountName=2fdsgd;AccountKey=qV2VLads==;EndpointSuffix=core.windows.net"; | ||
|
||
private final String containerName = "container"; | ||
|
||
@Mock | ||
private PropertyResolver propertyResolver; | ||
|
||
private AzureCloudStorageService azureCloudStorageService; | ||
|
||
@Mock | ||
private BlobContainerClient containerClient; | ||
|
||
@Mock | ||
private BlobClient blobClient; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockEnvironment mockEnvironment = new MockEnvironment(); | ||
mockEnvironment.setProperty("azure.connection.string", connectionString); | ||
mockEnvironment.setProperty("azure.container.name", containerName); | ||
propertyResolver = mockEnvironment; | ||
azureCloudStorageService = spy(new AzureCloudStorageService(propertyResolver)); | ||
} | ||
|
||
@Test | ||
void checkUpload() { | ||
MultipartFile multipartFile = new MockMultipartFile("Image", "Image".getBytes(StandardCharsets.UTF_8)); | ||
doReturn(containerClient).when(azureCloudStorageService).containerClient(); | ||
when(containerClient.getBlobClient(anyString())).thenReturn(blobClient); | ||
doReturn("blobUrl").when(blobClient).getBlobUrl(); | ||
azureCloudStorageService.upload(multipartFile); | ||
assertNotNull(azureCloudStorageService.getConnectionString()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please check that values are equal to the values you set in the setUp method |
||
assertNotNull(azureCloudStorageService.getContainerName()); | ||
assertEquals(connectionString, azureCloudStorageService.getConnectionString()); | ||
assertEquals(containerName, azureCloudStorageService.getContainerName()); | ||
verify(containerClient).getBlobClient(anyString()); | ||
verify(blobClient).upload(any(InputStream.class), anyLong()); | ||
verify(blobClient).getBlobUrl(); | ||
} | ||
|
||
@Test | ||
void checkDelete() { | ||
doReturn(containerClient).when(azureCloudStorageService).containerClient(); | ||
when(containerClient.getBlobClient(anyString())).thenReturn(blobClient); | ||
when(blobClient.exists()).thenReturn(true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add test if |
||
doNothing().when(blobClient).delete(); | ||
azureCloudStorageService.delete("url/somepath/somefile.txt"); | ||
assertNotNull(azureCloudStorageService.getConnectionString()); | ||
assertNotNull(azureCloudStorageService.getContainerName()); | ||
verify(containerClient).getBlobClient(anyString()); | ||
assertEquals(true, blobClient.exists()); | ||
verify(blobClient).delete(); | ||
} | ||
|
||
@Test | ||
void checkDeleteIfClientNull() { | ||
doReturn(containerClient).when(azureCloudStorageService).containerClient(); | ||
when(containerClient.getBlobClient(anyString())).thenReturn(blobClient); | ||
when(blobClient.exists()).thenReturn(null); | ||
azureCloudStorageService.delete("url/somepath/somefile.txt"); | ||
verify(blobClient, never()).delete(); | ||
} | ||
|
||
@Test | ||
void checkDeleteIfClientFalse() { | ||
doReturn(containerClient).when(azureCloudStorageService).containerClient(); | ||
when(containerClient.getBlobClient(anyString())).thenReturn(blobClient); | ||
when(blobClient.exists()).thenReturn(false); | ||
azureCloudStorageService.delete("url/somepath/somefile.txt"); | ||
verify(blobClient, never()).delete(); | ||
} | ||
|
||
@Test | ||
void checkUploadThrowsException() { | ||
doReturn(containerClient).when(azureCloudStorageService).containerClient(); | ||
when(containerClient.getBlobClient(anyString())).thenReturn(blobClient); | ||
doAnswer(invocation -> { | ||
throw new IOException(); | ||
}).when(blobClient).upload(any(InputStream.class), anyLong()); | ||
MultipartFile multipartFile = new MockMultipartFile("Image", "Image".getBytes(StandardCharsets.UTF_8)); | ||
FileNotSavedException ex = | ||
assertThrows(FileNotSavedException.class, () -> azureCloudStorageService.upload(multipartFile)); | ||
assertEquals(ErrorMessage.FILE_NOT_SAVED, ex.getMessage()); | ||
} | ||
|
||
@Test | ||
void checkUploadNullImage() { | ||
MultipartFile multipartFile = null; | ||
IllegalArgumentException ex = | ||
assertThrows(IllegalArgumentException.class, () -> azureCloudStorageService.upload(multipartFile)); | ||
assertEquals(ErrorMessage.FILE_IS_NULL, ex.getMessage()); | ||
} | ||
|
||
@Test | ||
void checkInvalidConnectionString() { | ||
azureCloudStorageService = new AzureCloudStorageService(new MockEnvironment()); | ||
assertThrows(IllegalArgumentException.class, () -> azureCloudStorageService.containerClient()); | ||
} | ||
|
||
@Test | ||
void checkContainerClient() { | ||
BlobContainerClient client = azureCloudStorageService.containerClient(); | ||
assertEquals(containerName, client.getBlobContainerName()); | ||
} | ||
|
||
@Test | ||
void checkDeleteThrowsException() { | ||
String url = "aa#26#1"; | ||
BadRequestException ex = assertThrows(BadRequestException.class, () -> azureCloudStorageService.delete(url)); | ||
assertEquals(ErrorMessage.PARSING_URL_FAILED + url, ex.getMessage()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package greencity.service.ubs; | ||
|
||
import greencity.ModelUtils; | ||
import greencity.constant.ErrorMessage; | ||
import greencity.dto.order.UserWithSomeOrderDetailDto; | ||
import greencity.dto.pageble.PageableDto; | ||
import greencity.entity.user.User; | ||
import greencity.entity.user.employee.Employee; | ||
import greencity.enums.SortingOrder; | ||
import greencity.filters.CustomerPage; | ||
import greencity.filters.UserFilterCriteria; | ||
import greencity.repository.EmployeeRepository; | ||
import greencity.repository.UserRepository; | ||
import greencity.repository.UserTableRepo; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.data.domain.*; | ||
|
||
import javax.persistence.EntityNotFoundException; | ||
import java.util.*; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please do not use |
||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.ArgumentMatchers.*; | ||
import static org.mockito.Mockito.*; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
|
||
@ExtendWith(MockitoExtension.class) | ||
class ValuesForUserTableServiceImplTest { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please follow encapsulation object-oriented principle |
||
|
||
@Mock | ||
private UserRepository userRepository; | ||
|
||
@Mock | ||
private UserTableRepo userTableRepo; | ||
|
||
@Mock | ||
private EmployeeRepository employeeRepository; | ||
|
||
@InjectMocks | ||
private ValuesForUserTableServiceImpl valuesForUserTableService; | ||
|
||
@Test | ||
void checkGetAllFields() { | ||
User user = User.builder() | ||
.orders(new ArrayList<>()) | ||
.violations(5) | ||
.currentPoints(8) | ||
.build(); | ||
Pageable pageable = PageRequest.of(1, 1); | ||
Employee employee = ModelUtils.getEmployee(); | ||
List<Long> tariffsInfo = List.of(1L, 2L, 3L); | ||
UserWithSomeOrderDetailDto userWithSomeOrderDetail = valuesForUserTableService.mapToDto(user); | ||
when(employeeRepository.findByEmail(anyString())).thenReturn(Optional.of(employee)); | ||
when(employeeRepository.findTariffsInfoForEmployee(anyLong())).thenReturn(tariffsInfo); | ||
when(userRepository.getAllUsersByTariffsInfoId(anyLong())).thenReturn(tariffsInfo); | ||
when(userTableRepo.findAll(any(UserFilterCriteria.class), anyString(), any(SortingOrder.class), | ||
any(CustomerPage.class), Mockito.<Long>anyList())).thenReturn(new PageImpl<>(List.of(user), pageable, 1L)); | ||
PageableDto actual = valuesForUserTableService.getAllFields(new CustomerPage(), "column", SortingOrder.ASC, | ||
new UserFilterCriteria(), | ||
employee.getEmail()); | ||
verify(employeeRepository).findByEmail(anyString()); | ||
verify(employeeRepository).findTariffsInfoForEmployee(anyLong()); | ||
verify(userRepository, times(tariffsInfo.size())).getAllUsersByTariffsInfoId(anyLong()); | ||
verify(userTableRepo).findAll(any(UserFilterCriteria.class), anyString(), any(SortingOrder.class), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please verify all mocks |
||
any(CustomerPage.class), Mockito.<Long>anyList()); | ||
assertEquals(2L, actual.getTotalElements()); | ||
assertEquals(2, actual.getTotalPages()); | ||
assertEquals(List.of(userWithSomeOrderDetail), actual.getPage()); | ||
} | ||
|
||
@Test | ||
void checkGetAllFieldsIfEmployeeIsNull() { | ||
CustomerPage customerPage = new CustomerPage(); | ||
UserFilterCriteria userFilterCriteria = new UserFilterCriteria(); | ||
EntityNotFoundException ex = assertThrows(EntityNotFoundException.class, () -> valuesForUserTableService | ||
.getAllFields(customerPage, "column", SortingOrder.ASC, userFilterCriteria, "email")); | ||
assertEquals(ErrorMessage.EMPLOYEE_NOT_FOUND, ex.getMessage()); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,11 @@ | |
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import greencity.constant.ErrorMessage; | ||
import greencity.entity.order.TariffsInfo; | ||
import greencity.entity.user.employee.Employee; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
|
@@ -33,6 +37,8 @@ | |
import greencity.repository.ViolationRepository; | ||
import greencity.service.notification.NotificationServiceImpl; | ||
|
||
import javax.persistence.EntityNotFoundException; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.Mockito.any; | ||
|
@@ -105,35 +111,37 @@ void deleteViolationFromOrderByOrderId() { | |
} | ||
|
||
@Test | ||
@Disabled | ||
void checkAddUserViolation() { | ||
Employee employee = ModelUtils.getEmployee(); | ||
List<Long> tariffsInfos = List.of(1L, 2L, 3L); | ||
TariffsInfo tariffsInfo = ModelUtils.getTariffsInfo(); | ||
User user = ModelUtils.getTestUser(); | ||
Order order = user.getOrders().get(0); | ||
order.setTariffsInfo(tariffsInfo); | ||
order.setUser(user); | ||
AddingViolationsToUserDto add = ModelUtils.getAddingViolationsToUserDto(); | ||
add.setOrderID(order.getId()); | ||
when(orderRepository.findById(order.getId())).thenReturn(Optional.ofNullable(order)); | ||
when(userRepository.findUserByUuid("abc")).thenReturn(Optional.of(user)); | ||
when(userRepository.countTotalUsersViolations(1L)).thenReturn(1); | ||
when(employeeRepository.findByEmail(anyString())).thenReturn(Optional.ofNullable(employee)); | ||
when(employeeRepository.findTariffsInfoForEmployee(anyLong())).thenReturn(tariffsInfos); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add verify for all mocks |
||
violationService.addUserViolation(add, new MultipartFile[2], "abc"); | ||
|
||
assertEquals(1, user.getViolations()); | ||
} | ||
|
||
@Test | ||
@Disabled | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please remove import for |
||
void checkAddUserViolationThrowsException() { | ||
User user = ModelUtils.getTestUser(); | ||
Order order = user.getOrders().get(0); | ||
order.setUser(user); | ||
AddingViolationsToUserDto add = ModelUtils.getAddingViolationsToUserDto(); | ||
add.setOrderID(order.getId()); | ||
when(orderRepository.findById(order.getId())).thenReturn(Optional.ofNullable(order)); | ||
when(userRepository.findUserByUuid("abc")).thenReturn(Optional.of(user)); | ||
when(violationRepository.findByOrderId(order.getId())).thenReturn(Optional.of(ModelUtils.getViolation())); | ||
|
||
assertThrows(NotFoundException.class, | ||
EntityNotFoundException ex = assertThrows(EntityNotFoundException.class, | ||
() -> violationService.addUserViolation(add, new MultipartFile[2], "abc")); | ||
assertEquals(ErrorMessage.EMPLOYEE_NOT_FOUND, ex.getMessage()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add |
||
} | ||
|
||
@Test | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mock-maker-inline |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add tests for this method too