Skip to content
Snippets Groups Projects
Commit c5ad175c authored by Anesa Fazlagić's avatar Anesa Fazlagić
Browse files

added tests for the device

parent 42a8b400
No related branches found
No related tags found
2 merge requests!25Milestone 1,!21Unit tests for the core
Pipeline #
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
...@@ -4,7 +4,7 @@ import cz.muni.fi.pa165.core.smartmeter.SmartMeter; ...@@ -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.smartmeter.SmartMeterRepository;
import cz.muni.fi.pa165.core.user.User; import cz.muni.fi.pa165.core.user.User;
import cz.muni.fi.pa165.core.user.UserRepository; import cz.muni.fi.pa165.core.user.UserRepository;
import org.testng.annotations.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
...@@ -61,14 +61,7 @@ public class UnitTestHouseJPA { ...@@ -61,14 +61,7 @@ public class UnitTestHouseJPA {
@Test @Test
public void should_store_a_house() { public void should_store_a_house() {
//need to register a house first House house = helperRegisterHouse();
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));
assertThat(house).hasFieldOrPropertyWithValue("address", "Titova 6"); assertThat(house).hasFieldOrPropertyWithValue("address", "Titova 6");
assertThat(house).hasFieldOrPropertyWithValue("city", "Sarajevo"); assertThat(house).hasFieldOrPropertyWithValue("city", "Sarajevo");
...@@ -143,13 +136,12 @@ public class UnitTestHouseJPA { ...@@ -143,13 +136,12 @@ public class UnitTestHouseJPA {
houseRepository.deleteById(house1.getId()); houseRepository.deleteById(house1.getId());
Iterable houses = houseRepository.findAll(); assertThat(houseRepository.findAll()).hasSize(1).contains(house);
assertThat(houseRepository.findAll()).hasSize(1).doesNotContain(house1);
assertThat(houses).hasSize(1).contains(house);
} }
@Test @Test
public void should_delete_all_tutorials() { public void should_delete_all() {
entityManager.persist(helperRegisterHouse()); entityManager.persist(helperRegisterHouse());
entityManager.persist(helperRegisterHouse()); entityManager.persist(helperRegisterHouse());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment