-
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?
Conversation
…d Orders For UserServiceImpl
# Conflicts: # service/src/test/java/greencity/service/ubs/OrdersForUserServiceImplTest.java
@@ -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); |
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
@BeforeEach | ||
void setUp() { | ||
MockEnvironment mockEnvironment = new MockEnvironment(); | ||
mockEnvironment.setProperty("azure.connection.string", " "); |
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 some values for properties
private AzureCloudStorageService azureCloudStorageService; | ||
|
||
@Mock | ||
BlobContainerClient containerClient; |
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.
private
BlobContainerClient containerClient; | ||
|
||
@Mock | ||
BlobClient blobClient; |
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.
private
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary check. multipartFile
could not be null
You created it here
doReturn("blobUrl").when(blobClient).getBlobUrl(); | ||
azureCloudStorageService.upload(multipartFile); | ||
assertNotNull(multipartFile); | ||
assertNotNull(azureCloudStorageService.getConnectionString()); |
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 check that values are equal to the values you set in the setUp method
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 comment
The reason will be displayed to describe this comment to others. Learn more.
please add test if blobClient.exists() == null
and blobClient.exists() == false
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class ValuesForUserTableServiceImplTest { |
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 follow encapsulation object-oriented principle
ValuesForUserTableServiceImpl valuesForUserTableService; | ||
|
||
@Test | ||
void getAllFields() { |
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 test if user does not exist
valuesForUserTableService.getAllFields(new CustomerPage(), "column", SortingOrder.ASC, new UserFilterCriteria(), | ||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
please verify all mocks
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
please add tests for this method too
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 comment
The reason will be displayed to describe this comment to others. Learn more.
please do not use *
@ExtendWith(MockitoExtension.class) | ||
class AzureCloudStorageServiceTest { | ||
|
||
@BeforeEach |
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.
should after all class variables
https://www.cs.rice.edu/~cork/book/node9.html
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 comment
The reason will be displayed to describe this comment to others. Learn more.
please do not use real values for tests
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 comment
The reason will be displayed to describe this comment to others. Learn more.
please check if the result of valuesForUserTableService.getAllFields
contains all expected values
() -> 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 comment
The reason will be displayed to describe this comment to others. Learn more.
please add verify
for orderRepository.findById(order.getId())).thenReturn(Optional.ofNullable(order)
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 comment
The reason will be displayed to describe this comment to others. Learn more.
please remove import for Disabled
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 comment
The reason will be displayed to describe this comment to others. Learn more.
please add verify for all mocks
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
same here
@@ -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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Kudos, SonarCloud Quality Gate passed! |
dev
JIRA
Code reviewers
Summary of issue
Classes ValuesForUserServiceImpl and AzureStorageService weren`t covered by tests at all, ViolationServiceImpl was not fully covered
Summary of change
re tying to upload isn
t nullTesting approach
Run tests in /service/src/test/java/greencity/service/ubs
CHECK LIST