diff --git a/core/src/main/java/cz/muni/fi/pa165/core/common/DomainObject.java b/core/src/main/java/cz/muni/fi/pa165/core/common/DomainObject.java index 174c3f7758c4c1706f46657d1b7370286a3c9fe2..d27f0df747994f91b0ec9305aa551b34164a709b 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/common/DomainObject.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/common/DomainObject.java @@ -14,6 +14,6 @@ import java.util.UUID; public abstract class DomainObject { @Id private String id = UUID.randomUUID().toString(); - public LocalDateTime createdDateTime; - public LocalDateTime deletedDateTime; + private LocalDateTime createdDateTime; + private LocalDateTime deletedDateTime; } diff --git a/core/src/main/java/cz/muni/fi/pa165/core/common/DomainService.java b/core/src/main/java/cz/muni/fi/pa165/core/common/DomainService.java index 30ebda5b9a4e25220d951d45211f1271fdb63496..f24bb511d262d6d203fa9642d927b23927deb80f 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/common/DomainService.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/common/DomainService.java @@ -8,12 +8,14 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; +import java.util.Arrays; import java.util.List; + /** * A generic service for domain objects. * - * @author Marek SkácelĂk * @param <T> the type of the domain object. + * @author Marek SkácelĂk */ public abstract class DomainService<T extends DomainObject> { @@ -34,7 +36,7 @@ public abstract class DomainService<T extends DomainObject> { */ @Transactional public T create(T entity) { - entity.createdDateTime = LocalDateTime.now(); + entity.setCreatedDateTime(LocalDateTime.now()); return getRepository().save(entity); } @@ -56,7 +58,7 @@ public abstract class DomainService<T extends DomainObject> { */ @Transactional(readOnly = true) public List<T> findAll() { - return getRepository().findAll().stream().filter(entity -> entity.deletedDateTime == null).toList(); + return getRepository().findAll().stream().filter(entity -> entity.getDeletedDateTime() == null).toList(); } /** @@ -81,7 +83,7 @@ public abstract class DomainService<T extends DomainObject> { public T findById(String id) { return getRepository() .findById(id) - .filter(entity -> entity.deletedDateTime == null) + .filter(entity -> entity.getDeletedDateTime() == null) .orElseThrow(() -> new EntityNotFoundException("Entity with '" + id + "' not found.")); } @@ -92,7 +94,7 @@ public abstract class DomainService<T extends DomainObject> { @Transactional public void deleteAll() { List<T> entities = findAll(); - entities.stream().map(entity -> entity.deletedDateTime = LocalDateTime.now()); + entities.stream().forEach(entity -> entity.setDeletedDateTime(LocalDateTime.now())); getRepository().saveAll(entities); } @@ -101,9 +103,7 @@ public abstract class DomainService<T extends DomainObject> { */ @Transactional public void deleteAllById(String[] ids) { - for (String id: ids) { - deleteById(id); - } + Arrays.stream(ids).forEach(this::deleteById); } /** @@ -118,7 +118,7 @@ public abstract class DomainService<T extends DomainObject> { T entity = findById(id); if (entity == null) throw new EntityNotFoundException("Entity '" + id + "' not found."); - entity.deletedDateTime = LocalDateTime.now(); + entity.setDeletedDateTime(LocalDateTime.now()); getRepository().save(entity); return entity; } @@ -138,8 +138,7 @@ public abstract class DomainService<T extends DomainObject> { throw new EntityNotFoundException("Entity '" + id + "' not found."); // TODO: change when ORM tool available entityToUpdate.setId(id); - getRepository().save(entityToUpdate); - return entity; + return getRepository().save(entityToUpdate); } diff --git a/core/src/main/java/cz/muni/fi/pa165/core/company/CompanyService.java b/core/src/main/java/cz/muni/fi/pa165/core/company/CompanyService.java index 32f8a085d9a8ba479df4902af1574d5b00bf714d..847630486f7227e80ed11a428f3ec0ca027cd4ce 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/company/CompanyService.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/company/CompanyService.java @@ -21,7 +21,7 @@ public class CompanyService extends DomainService<Company> { public Company findByName(String name) { return getRepository() .findByName(name) - .filter(entity -> entity.deletedDateTime == null) + .filter(entity -> entity.getDeletedDateTime() == null) .orElse(null); } } diff --git a/core/src/main/java/cz/muni/fi/pa165/core/smartmeter/SmartMeterFacade.java b/core/src/main/java/cz/muni/fi/pa165/core/smartmeter/SmartMeterFacade.java index 325cfbc8fa2b1e2a1f2a1afc22c4165d347f8b37..d295439bf515153daba14cd6718ebde0a24d2355 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/smartmeter/SmartMeterFacade.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/smartmeter/SmartMeterFacade.java @@ -20,10 +20,11 @@ public class SmartMeterFacade extends DomainFacade<SmartMeter, private final DeviceService deviceService; private final HouseService houseService; private final MetricsService metricsService; + @Autowired public SmartMeterFacade(SmartMeterService smartMeterService, - SmartMeterMapper smartMeterMapper, - DeviceService deviceService, HouseService houseService, MetricsService metricsService) { + SmartMeterMapper smartMeterMapper, + DeviceService deviceService, HouseService houseService, MetricsService metricsService) { super(smartMeterService, smartMeterMapper); this.smartMeterService = smartMeterService; this.deviceService = deviceService; @@ -32,7 +33,7 @@ public class SmartMeterFacade extends DomainFacade<SmartMeter, } @Override - public SmartMeterDto create(SmartMeterCreateDto smartMeterCreateDto){ + public SmartMeterDto create(SmartMeterCreateDto smartMeterCreateDto) { SmartMeter smartMeter = mapper.fromCreateDto(smartMeterCreateDto); smartMeter.setDevice(deviceService.findById(smartMeterCreateDto.getDeviceId())); smartMeter.setHouse(houseService.findById(smartMeterCreateDto.getHouseId())); @@ -41,7 +42,7 @@ public class SmartMeterFacade extends DomainFacade<SmartMeter, @Override @Transactional - public SmartMeterDto deleteById(String id){ + public SmartMeterDto deleteById(String id) { // soft delete Smart Devices SmartMeter smartMeter = smartMeterService.findById(id); metricsService.deleteAllById((String[]) smartMeter.getMetricsList().stream().map(sm -> smartMeter.getId()).toArray()); diff --git a/core/src/main/java/cz/muni/fi/pa165/core/user/UserService.java b/core/src/main/java/cz/muni/fi/pa165/core/user/UserService.java index a6c55162253a8df37f4833b43cf4bae2ead8472c..f51fd173d55f10369f68665b61dd0e4a7a1cc5bb 100644 --- a/core/src/main/java/cz/muni/fi/pa165/core/user/UserService.java +++ b/core/src/main/java/cz/muni/fi/pa165/core/user/UserService.java @@ -26,7 +26,7 @@ public class UserService extends DomainService<User> { public User findByUsername(String username) { return getRepository() .findByUsername(username) - .filter(entity -> entity.deletedDateTime == null) + .filter(entity -> entity.getDeletedDateTime() == null) .orElse(null); } @@ -43,7 +43,7 @@ public class UserService extends DomainService<User> { public boolean loginCheck(String userId, String password) { return getRepository() .findByIdAndPassword(userId, password) - .filter(entity -> entity.deletedDateTime == null) + .filter(entity -> entity.getDeletedDateTime() == null) .isPresent(); } } diff --git a/core/src/test/java/cz/muni/fi/pa165/core/company/CompanyServiceTest.java b/core/src/test/java/cz/muni/fi/pa165/core/company/CompanyServiceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..74b1ede9bc941886203d05d236b55339b13bb03a --- /dev/null +++ b/core/src/test/java/cz/muni/fi/pa165/core/company/CompanyServiceTest.java @@ -0,0 +1,212 @@ +package cz.muni.fi.pa165.core.company; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; + +import java.util.List; +import java.util.Optional; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.openMocks; + +/** + * CompanyService unit tests + * + * @author xskacel + */ + +public class CompanyServiceTest { + + @InjectMocks + private CompanyService companyService; + + @Mock + private CompanyRepository companyRepositoryMock; + + @BeforeEach + void init() { + openMocks(this); + } + + @Test + void shouldGetCompanyById() { + Company company = Company + .builder() + .name("Bohemia") + .build(); + + when(companyRepositoryMock.findById(company.getId())).thenReturn(Optional.of(company)); + + Company result = companyService.findById(company.getId()); + + verify(companyRepositoryMock).findById(company.getId()); + + assertEquals(company, result); + } + + @Test + void shouldCreateNewCompany() { + Company company = Company + .builder() + .name("New Company") + .build(); + + when(companyRepositoryMock.save(company)).thenReturn(company); + + Company createdCompany = companyService.create(company); + + verify(companyRepositoryMock).save(company); + + assertThat(createdCompany).isEqualTo(company); + } + + @Test + void shouldFindCompanyByName() { + Company company = Company.builder() + .name("Bohemia") + .build(); + + when(companyRepositoryMock.findByName(company.getName())).thenReturn(Optional.of(company)); + + Company result = companyService.findByName(company.getName()); + + verify(companyRepositoryMock).findByName(company.getName()); + + assertEquals(company, result); + } + + @Test + void shouldFindAllCompaniesPageableInt() { + Company firstCompany = Company.builder() + .name("Bohemia") + .build(); + + Company secondCompany = Company.builder() + .name("New Company") + .build(); + + Page<Company> page = new PageImpl<>(List.of(firstCompany, secondCompany)); + + int pageNumber = 0; + int pageSize = 10; + + when(companyRepositoryMock.findAll(PageRequest.of(pageNumber, pageSize))).thenReturn(page); + + Page<Company> result = companyService.findAllPageableInt(pageNumber); + + verify(companyRepositoryMock).findAll(PageRequest.of(pageNumber, pageSize)); + + assertThat(result).containsExactlyInAnyOrder(firstCompany, secondCompany); + } + + @Test + void shouldDeleteCompanyById() { + Company company = Company.builder() + .name("Bohemia") + .build(); + + when(companyRepositoryMock.findById(company.getId())).thenReturn(Optional.of(company)); + + Company deletedCompany = companyService.deleteById(company.getId()); + + verify(companyRepositoryMock).findById(company.getId()); + verify(companyRepositoryMock).save(company); // soft delete + + assertNotNull(deletedCompany.getDeletedDateTime()); + assertEquals(company, deletedCompany); + } + + @Test + void shouldUpdateCompany() { + Company originalCompany = Company.builder() + .name("Bohemia") + .build(); + + Company updatedCompany = Company.builder() + .name("New Name") + .build(); + + when(companyRepositoryMock.findById(originalCompany.getId())).thenReturn(Optional.of(originalCompany)); + + when(companyRepositoryMock.save(updatedCompany)).thenReturn(updatedCompany); + + Company result = companyService.update(updatedCompany, originalCompany.getId()); + + verify(companyRepositoryMock).findById(originalCompany.getId()); + verify(companyRepositoryMock).save(updatedCompany); + + assertEquals(originalCompany.getId(), result.getId()); + assertEquals(updatedCompany.getName(), result.getName()); + } + @Test + void shouldHardDeleteAllCompanies() { + companyService.hardDeleteAll(); + verify(companyRepositoryMock).deleteAll(); + } + + @Test + void shouldDeleteAllCompanies() { + var data = List.of( + Company.builder().name("Bohemia").build(), + Company.builder().name("Apple").build() + ); + when(companyRepositoryMock.findAll()).thenReturn(data); + companyService.deleteAll(); + verify(companyRepositoryMock).findAll(); + verify(companyRepositoryMock).saveAll(data); + } + @Test + void shouldDeleteAllCompaniesByIds() { + Company company1 = Company.builder().name("Bohemia").build(); + Company company2 = Company.builder().name("Apple").build(); + String[] ids = {company1.getId(), company2.getId()}; + when(companyRepositoryMock.findById(ids[0])).thenReturn(Optional.of(company1)); + when(companyRepositoryMock.findById(ids[1])).thenReturn(Optional.of(company2)); + + companyService.deleteAllById(ids); + + verify(companyRepositoryMock).save(company1); + verify(companyRepositoryMock).save(company2); + } + @Test + void shouldFindAllCompanies() { + List<Company> companies = List.of( + Company.builder().name("Bohemia").build(), + Company.builder().name("Apple").build() + ); + + when(companyRepositoryMock.findAll()).thenReturn(companies); + List<Company> result = companyService.findAll(); + verify(companyRepositoryMock).findAll(); + assertEquals(companies, result); + } + @Test + void shouldFindAllCompaniesPageable() { + int pageNumber = 0; + int pageSize = 10; + Pageable pageable = PageRequest.of(pageNumber, pageSize); + + List<Company> companies = List.of( + Company.builder().name("Bohemia").build(), + Company.builder().name("Apple").build() + ); + + Page<Company> pageResult = new PageImpl<>(companies, pageable, companies.size()); + when(companyRepositoryMock.findAll(pageable)).thenReturn(pageResult); + + Page<Company> result = companyService.findAllPageable(pageable); + verify(companyRepositoryMock).findAll(pageable); + + assertEquals(pageResult, result); + } +}