-
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 19 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,12 @@ | ||
package greencity.exceptions.image; | ||
|
||
public class FileIsNullException extends RuntimeException { | ||
/** | ||
* Constructor with message. | ||
* | ||
* @param message message, that explains cause of the exception. | ||
*/ | ||
public FileIsNullException(String message) { | ||
super(message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package greencity.exceptions.image; | ||
|
||
import greencity.constant.ErrorMessage; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class FileIsNullExceptionTest { | ||
@Test | ||
void fileIsNullExceptionTest() { | ||
String message = "File equals null"; | ||
FileIsNullException ex = new FileIsNullException(message); | ||
assertEquals(ErrorMessage.FILE_IS_NULL, ex.getMessage()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,10 @@ | |
import com.azure.storage.blob.BlobContainerClient; | ||
import com.azure.storage.blob.BlobServiceClient; | ||
import com.azure.storage.blob.BlobServiceClientBuilder; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import greencity.constant.ErrorMessage; | ||
import greencity.exceptions.BadRequestException; | ||
import greencity.exceptions.image.FileIsNullException; | ||
import greencity.exceptions.image.FileNotSavedException; | ||
import lombok.Data; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
@@ -40,6 +42,9 @@ public AzureCloudStorageService(@Autowired PropertyResolver propertyResolver) { | |
*/ | ||
@Override | ||
public String upload(MultipartFile multipartFile) { | ||
if (multipartFile == null) { | ||
throw new FileIsNullException(ErrorMessage.FILE_IS_NULL); | ||
} | ||
final String blob = UUID.randomUUID().toString(); | ||
BlobClient client = containerClient() | ||
.getBlobClient(blob + multipartFile.getOriginalFilename()); | ||
|
@@ -65,7 +70,8 @@ public void delete(String url) { | |
} | ||
} | ||
|
||
private BlobContainerClient containerClient() { | ||
@VisibleForTesting | ||
BlobContainerClient containerClient() { | ||
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 tests for this method too |
||
BlobServiceClient serviceClient = new BlobServiceClientBuilder() | ||
.connectionString(connectionString).buildClient(); | ||
return serviceClient.getBlobContainerClient(containerName); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package greencity.service.ubs; | ||
|
||
import com.azure.storage.blob.*; | ||
import greencity.constant.ErrorMessage; | ||
import greencity.exceptions.BadRequestException; | ||
import greencity.exceptions.image.FileIsNullException; | ||
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.*; | ||
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.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", " "); | ||
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 use some values for properties |
||
mockEnvironment.setProperty("azure.container.name", " "); | ||
propertyResolver = mockEnvironment; | ||
azureCloudStorageService = spy(new AzureCloudStorageService(propertyResolver)); | ||
} | ||
|
||
@Mock | ||
private PropertyResolver propertyResolver; | ||
|
||
private AzureCloudStorageService azureCloudStorageService; | ||
|
||
@Mock | ||
BlobContainerClient containerClient; | ||
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. private |
||
|
||
@Mock | ||
BlobClient blobClient; | ||
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. private |
||
|
||
@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(multipartFile); | ||
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. Unnecessary check. |
||
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()); | ||
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 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; | ||
FileIsNullException ex = | ||
assertThrows(FileIsNullException.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 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,65 @@ | ||
package greencity.service.ubs; | ||
|
||
import greencity.ModelUtils; | ||
import greencity.entity.order.Order; | ||
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.glassfish.hk2.utilities.Stub; | ||
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 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.mockito.ArgumentMatchers.*; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@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 | ||
UserRepository userRepository; | ||
|
||
@Mock | ||
UserTableRepo userTableRepo; | ||
|
||
@Mock | ||
EmployeeRepository employeeRepository; | ||
|
||
@InjectMocks | ||
ValuesForUserTableServiceImpl valuesForUserTableService; | ||
|
||
@Test | ||
void getAllFields() { | ||
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 user does not exist |
||
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(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()); | ||
} | ||
|
||
} |
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 use
IllegalArgumentException
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.
And please add exception handler