diff --git a/core/src/test/java/cz/muni/fi/pa165/core/device/UnitTestDeviceJPA.java b/core/src/test/java/cz/muni/fi/pa165/core/device/UnitTestDeviceJPA.java new file mode 100644 index 0000000000000000000000000000000000000000..ef21f54aa4f469200c7d4cbfa8a11d2e10564ddf --- /dev/null +++ b/core/src/test/java/cz/muni/fi/pa165/core/device/UnitTestDeviceJPA.java @@ -0,0 +1,150 @@ +package cz.muni.fi.pa165.core.device; + +import cz.muni.fi.pa165.core.house.HouseRepository; +import cz.muni.fi.pa165.core.manufacturer.Manufacturer; +import cz.muni.fi.pa165.core.manufacturer.ManufacturerRepository; +import cz.muni.fi.pa165.core.smartmeter.SmartMeter; +import cz.muni.fi.pa165.core.smartmeter.SmartMeterRepository; +import cz.muni.fi.pa165.core.user.UserRepository; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.List; +import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@DataJpaTest +public class UnitTestDeviceJPA { + @Autowired + private TestEntityManager entityManager; + @Autowired + private HouseRepository houseRepository; + @Autowired + private SmartMeterRepository smartMeterRepository; + @Autowired + private DeviceRepository deviceRepository; + @Autowired + private ManufacturerRepository manufacturerRepository; + @Autowired + private UserRepository userRepository; + + @Test + public void createDeviceTesting() throws Exception { + this.entityManager.persist(new Device()); + String device = this.entityManager.persistAndGetId(new Device()).toString(); + // Regular expression to match UUID format + Pattern uuidPattern = Pattern.compile( + "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}", + Pattern.CASE_INSENSITIVE); + // Check if the string matches the UUID format + boolean isUUID = uuidPattern.matcher(device).matches(); + assertThat(isUUID).isTrue(); + } + + @Test + public void should_find_no_devices_if_repository_is_empty() { + Iterable<Device> devices = deviceRepository.findAll(); + assertThat(devices).isEmpty(); + } + + private Device helperRegisterDevice() { + Iterable<SmartMeter> smartMeterIterable; + smartMeterIterable = smartMeterRepository.saveAll(List.of(new SmartMeter(), new SmartMeter(), new SmartMeter())); + List<SmartMeter> list = StreamSupport.stream(smartMeterIterable.spliterator(), false) + .collect(Collectors.toList()); + //need to save a user to the database first to asign him to the new device + Manufacturer manufacturer = manufacturerRepository.save(new Manufacturer()); + return deviceRepository.save(new Device("Tesla Smart Meter A134", list, manufacturer)); + } + + @Test + public void should_store_a_device() { + Device device = helperRegisterDevice(); + + assertThat(device).hasFieldOrPropertyWithValue("name", "Tesla Smart Meter A134"); + } + + @Test + public void should_find_all_devices() { + Device device1 = new Device(); + entityManager.persist(device1); + + Device device2 = new Device(); + entityManager.persist(device2); + + assertThat(deviceRepository.findAll()).hasSize(2).contains(device1, device2); + } + + @Test + public void should_find_device_by_id() { + Device device1 = new Device(); + entityManager.persist(device1); + + Device device2 = new Device(); + entityManager.persist(device2); + + Device foundDevice = deviceRepository.findById(device2.getId()).get(); + + assertThat(foundDevice).isEqualTo(device2); + + foundDevice = deviceRepository.findById(device1.getId()).get(); + + assertThat(foundDevice).isEqualTo(device1); + } + + @Test + public void should_update_device_by_id() { + Device device1 = new Device(); + entityManager.persist(device1); + + Device device2 = new Device(); + entityManager.persist(device2); + + + + Device updatedDevice = deviceRepository.findById(device2.getId()).get(); + updatedDevice.setName("Smart Meter Gorenje V245"); + deviceRepository.save(updatedDevice); + + Device checkDevice = deviceRepository.findById(device2.getId()).get(); + + assertThat(checkDevice.getId()).isEqualTo(device2.getId()); + assertThat(checkDevice.getName()).isEqualTo(device2.getName()); + assertThat(checkDevice.getManufacturer()).isEqualTo(device2.getManufacturer()); + assertThat(checkDevice.getCreatedDateTime()).isEqualTo(device2.getCreatedDateTime()); + } + + @Test + public void should_delete_by_id() { + Device device1 = new Device(); + entityManager.persist(device1); + + Device device2 = new Device(); + entityManager.persist(device2); + + Device device = helperRegisterDevice(); + entityManager.persist(device); + + deviceRepository.deleteById(device.getId()); + + assertThat(deviceRepository.findAll()).hasSize(2).doesNotContain(device); + } + + @Test + public void should_delete_all() { + entityManager.persist(helperRegisterDevice()); + entityManager.persist(helperRegisterDevice()); + + houseRepository.deleteAll(); + + assertThat(houseRepository.findAll()).isEmpty(); + } +} \ No newline at end of file diff --git a/core/src/test/java/cz/muni/fi/pa165/core/house/UnitTestHouseJPA.java b/core/src/test/java/cz/muni/fi/pa165/core/house/UnitTestHouseJPA.java index a30ae24822913635dd306039289096fe39626c63..20a6a2a27fe99ca7db37ed691d17884bad701752 100644 --- a/core/src/test/java/cz/muni/fi/pa165/core/house/UnitTestHouseJPA.java +++ b/core/src/test/java/cz/muni/fi/pa165/core/house/UnitTestHouseJPA.java @@ -4,7 +4,7 @@ import cz.muni.fi.pa165.core.smartmeter.SmartMeter; import cz.muni.fi.pa165.core.smartmeter.SmartMeterRepository; import cz.muni.fi.pa165.core.user.User; import cz.muni.fi.pa165.core.user.UserRepository; -import org.testng.annotations.Test; +import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; @@ -61,14 +61,7 @@ public class UnitTestHouseJPA { @Test public void should_store_a_house() { - //need to register a house first - Iterable<SmartMeter> smartMeterIterable; - smartMeterIterable = smartMeterRepository.saveAll(List.of(new SmartMeter(), new SmartMeter(), new SmartMeter())); - List<SmartMeter> list = StreamSupport.stream(smartMeterIterable.spliterator(), false) - .collect(Collectors.toList()); - //need to save a user to the database first to asign him to the new house - User user = userRepository.save(new User()); - House house = houseRepository.save(new House("Titova 6", "Sarajevo", "Bosnia and Herzegovina", "71000", list, user)); + House house = helperRegisterHouse(); assertThat(house).hasFieldOrPropertyWithValue("address", "Titova 6"); assertThat(house).hasFieldOrPropertyWithValue("city", "Sarajevo"); @@ -143,13 +136,12 @@ public class UnitTestHouseJPA { houseRepository.deleteById(house1.getId()); - Iterable houses = houseRepository.findAll(); - - assertThat(houses).hasSize(1).contains(house); + assertThat(houseRepository.findAll()).hasSize(1).contains(house); + assertThat(houseRepository.findAll()).hasSize(1).doesNotContain(house1); } @Test - public void should_delete_all_tutorials() { + public void should_delete_all() { entityManager.persist(helperRegisterHouse()); entityManager.persist(helperRegisterHouse());