Skip to content
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

Open
wants to merge 30 commits into
base: dev
Choose a base branch
from
Open

Tests for service layer #954

wants to merge 30 commits into from

Conversation

BranetE
Copy link
Contributor

@BranetE BranetE commented Jan 19, 2023

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

  1. Made Tests For AzureCloudStrogeService
  2. Added check if file were tying to upload isnt null
  3. Created FileIsNullException and tests for it
  4. Made tests for ValuesForUserServiceImpl
  5. Fixed two tests in ViolationServiceImpl which weren`t working and were disadled

Testing approach

Run tests in /service/src/test/java/greencity/service/ubs

CHECK LIST

  • СI passed
  • Сode coverage >=95%
  • PR is reviewed manually again (to make sure you have 100% ready code)
  • All reviewers agreed to merge the PR
  • I've checked new feature as logged in and logged out user if needed
  • PR meets all conventions

# Conflicts:
#	service/src/test/java/greencity/service/ubs/OrdersForUserServiceImplTest.java
@BranetE BranetE self-assigned this Jan 20, 2023
@BranetE BranetE added the review label Jan 20, 2023
@@ -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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use IllegalArgumentException

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", " ");

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;

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;

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);

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());
Copy link

@RomaMocherniuk RomaMocherniuk Jan 23, 2023

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);

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 {

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() {

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),

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() {

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.*;

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

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");

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(),

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());

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

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);

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.*;

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.*;

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

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sonarcloud
Copy link

sonarcloud bot commented Jan 30, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

100.0% 100.0% Coverage
0.0% 0.0% Duplication

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants