-
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 23 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 |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package greencity.service.ubs; | ||
|
||
import com.azure.storage.blob.*; | ||
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.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.NullSource; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
import org.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. please do not use |
||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
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.anyString; | ||
import static org.mockito.Mockito.*; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class AzureCloudStorageServiceTest { | ||
|
||
@BeforeEach | ||
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. should after all class variables |
||
void setUp() { | ||
MockEnvironment mockEnvironment = new MockEnvironment(); | ||
mockEnvironment.setProperty("azure.connection.string", | ||
"DefaultEndpointsProtocol=https;AccountName=csb10032000a548f571;AccountKey=qV2VLVZlzxuEq8zGTgeiVE9puJiELNRPZcB9YgTSjZ3wKdWVA7kPjSOp6ESHlVMTJfHxB6N+iaV2TOlbe1GTvg==;EndpointSuffix=core.windows.net"); | ||
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 real values for tests |
||
mockEnvironment.setProperty("azure.container.name", "allfiles"); | ||
propertyResolver = mockEnvironment; | ||
azureCloudStorageService = spy(new AzureCloudStorageService(propertyResolver)); | ||
} | ||
|
||
@Mock | ||
private PropertyResolver propertyResolver; | ||
|
||
private AzureCloudStorageService azureCloudStorageService; | ||
|
||
@Mock | ||
private BlobContainerClient containerClient; | ||
|
||
@Mock | ||
private BlobClient blobClient; | ||
|
||
@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(propertyResolver.getProperty("azure.connection.string"), | ||
azureCloudStorageService.getConnectionString()); | ||
assertEquals(propertyResolver.getProperty("azure.container.name"), 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(); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(booleans = {false}) | ||
@NullSource | ||
void checkDeleteIfClientNullOrFalse(Boolean exists) { | ||
doReturn(containerClient).when(azureCloudStorageService).containerClient(); | ||
when(containerClient.getBlobClient(anyString())).thenReturn(blobClient); | ||
when(blobClient.exists()).thenReturn(exists); | ||
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(propertyResolver.getProperty("azure.container.name"), 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,77 @@ | ||
package greencity.service.ubs; | ||
|
||
import greencity.ModelUtils; | ||
import greencity.constant.ErrorMessage; | ||
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); | ||
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)); | ||
valuesForUserTableService.getAllFields(new CustomerPage(), "column", SortingOrder.ASC, new UserFilterCriteria(), | ||
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 if the result of |
||
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()); | ||
} | ||
|
||
@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