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

added tests for company and refactored a little bit of other test classes in core

parent c5ad175c
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.company;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.web.servlet.MockMvc;
@WebMvcTest(controllers = CompanyController.class)
public class CompanyControllerTest {
@Autowired
private MockMvc mockMvc;
private static String company = "/api/company";
}
package cz.muni.fi.pa165.core.company;
import cz.muni.fi.pa165.core.house.HouseRepository;
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.regex.Pattern;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@DataJpaTest
public class UnitTestCompanyJPA {
@Autowired
private TestEntityManager entityManager;
@Autowired
private CompanyRepository companyRepository;
@Autowired
private HouseRepository houseRepository;
@Autowired
private SmartMeterRepository smartMeterRepository;
@Autowired
private UserRepository userRepository;
@Test
public void createCompanyTesting() throws Exception {
String company = this.entityManager.persistAndGetId(new Company()).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(company).matches();
assertThat(isUUID).isTrue();
}
@Test
public void shouldFindNoneIfRepositoryIsEmpty() {
Iterable<Company> companies = companyRepository.findAll();
assertThat(companies).isEmpty();
}
private Company helperRegister() {
return companyRepository.save(new Company("Tesla"));
}
@Test
public void shouldStore() {
Company company = helperRegister();
assertThat(company).hasFieldOrPropertyWithValue("name", "Tesla");
}
@Test
public void shouldFindAll() {
Company company = new Company();
entityManager.persist(company);
Company company1 = new Company();
entityManager.persist(company1);
Company company2 = new Company();
entityManager.persist(company2);
assertThat(companyRepository.findAll()).hasSize(3).contains(company, company1, company2);
}
@Test
public void shouldFindById() {
Company company = new Company();
entityManager.persist(company);
Company company1 = new Company();
entityManager.persist(company1);
Company company2 = new Company();
entityManager.persist(company2);
Company found = companyRepository.findById(company2.getId()).get();
assertThat(found).isEqualTo(company2);
found = companyRepository.findById(company1.getId()).get();
assertThat(found).isEqualTo(company1);
}
@Test
public void shouldUpdateById() {
Company company = new Company();
entityManager.persist(company);
Company company1 = new Company();
entityManager.persist(company1);
Company updateCompany = companyRepository.findById(company1.getId()).get();
updateCompany.setName("Gorenje");
companyRepository.save(updateCompany);
Company check = companyRepository.findById(company1.getId()).get();
assertThat(check.getId()).isEqualTo(company1.getId());
assertThat(check.getName()).isEqualTo(company1.getName());
}
@Test
public void shouldDeleteById() {
Company company = new Company();
entityManager.persist(company);
Company company1 = helperRegister();
entityManager.persist(company1);
companyRepository.deleteById(company1.getId());
assertThat(companyRepository.findAll()).hasSize(1).contains(company);
assertThat(companyRepository.findAll()).hasSize(1).doesNotContain(company1);
}
@Test
public void shouldDeleteAll() {
entityManager.persist(helperRegister());
entityManager.persist(helperRegister());
companyRepository.deleteAll();
assertThat(companyRepository.findAll()).isEmpty();
}
}
//package cz.muni.fi.pa165.core.device;
//
//import cz.muni.fi.pa165.core.house.HouseRepository;
//import jakarta.activation.DataSource;
//import jakarta.persistence.EntityManager;
//import org.junit.Test;
//import org.junit.jupiter.api.extension.ExtendWith;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
//import org.springframework.jdbc.core.JdbcTemplate;
//import org.springframework.test.context.junit.jupiter.SpringExtension;
//
//import static org.assertj.core.api.FactoryBasedNavigableListAssert.assertThat;
//
//@ExtendWith(SpringExtension.class)
//@DataJpaTest
//public class DeviceControllerTest {
// @Autowired
// private DataSource dataSource;
// @Autowired private JdbcTemplate jdbcTemplate;
// @Autowired private EntityManager entityManager;
// @Autowired private DeviceRepository deviceRepository;
//
// @Test
// void injectedComponentsAreNotNull(){
// assertThat(dataSource).isNotNull();
// assertThat(jdbcTemplate).isNotNull();
// assertThat(entityManager).isNotNull();
// assertThat(deviceRepository).isNotNull();
// }
//}
......@@ -38,7 +38,6 @@ public class UnitTestDeviceJPA {
@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(
......@@ -50,7 +49,7 @@ public class UnitTestDeviceJPA {
}
@Test
public void should_find_no_devices_if_repository_is_empty() {
public void shouldFindNoDevicesIfRepositoryIsEmpty() {
Iterable<Device> devices = deviceRepository.findAll();
assertThat(devices).isEmpty();
}
......@@ -66,14 +65,14 @@ public class UnitTestDeviceJPA {
}
@Test
public void should_store_a_device() {
public void shouldStoreADevice() {
Device device = helperRegisterDevice();
assertThat(device).hasFieldOrPropertyWithValue("name", "Tesla Smart Meter A134");
}
@Test
public void should_find_all_devices() {
public void shouldFindAllDevices() {
Device device1 = new Device();
entityManager.persist(device1);
......@@ -84,7 +83,7 @@ public class UnitTestDeviceJPA {
}
@Test
public void should_find_device_by_id() {
public void shouldFindDeviceById() {
Device device1 = new Device();
entityManager.persist(device1);
......@@ -101,7 +100,7 @@ public class UnitTestDeviceJPA {
}
@Test
public void should_update_device_by_id() {
public void shouldUpdateDeviceById() {
Device device1 = new Device();
entityManager.persist(device1);
......@@ -123,7 +122,7 @@ public class UnitTestDeviceJPA {
}
@Test
public void should_delete_by_id() {
public void shouldDeleteById() {
Device device1 = new Device();
entityManager.persist(device1);
......@@ -139,7 +138,7 @@ public class UnitTestDeviceJPA {
}
@Test
public void should_delete_all() {
public void shouldDeleteAll() {
entityManager.persist(helperRegisterDevice());
entityManager.persist(helperRegisterDevice());
......
......@@ -31,7 +31,6 @@ public class UnitTestHouseJPA {
@Test
public void createHouseTesting() throws Exception {
this.entityManager.persist(new House());
String house = this.entityManager.persistAndGetId(new House()).toString();
// Regular expression to match UUID format
Pattern uuidPattern = Pattern.compile(
......@@ -43,7 +42,7 @@ public class UnitTestHouseJPA {
}
@Test
public void should_find_no_houses_if_repository_is_empty() {
public void shouldFindNoHousesIfRepositoryIsEmpty() {
Iterable<House> houses = houseRepository.findAll();
assertThat(houses).isEmpty();
}
......@@ -60,7 +59,7 @@ public class UnitTestHouseJPA {
}
@Test
public void should_store_a_house() {
public void shouldStoreAHouse() {
House house = helperRegisterHouse();
assertThat(house).hasFieldOrPropertyWithValue("address", "Titova 6");
......@@ -70,7 +69,7 @@ public class UnitTestHouseJPA {
}
@Test
public void should_find_all_houses() {
public void shouldFindAllHouses() {
House house1 = new House();
entityManager.persist(house1);
......@@ -86,7 +85,7 @@ public class UnitTestHouseJPA {
}
@Test
public void should_find_house_by_id() {
public void shouldFindHouseById() {
House house1 = new House();
entityManager.persist(house1);
House house2 = new House();
......@@ -102,7 +101,7 @@ public class UnitTestHouseJPA {
}
@Test
public void should_update_house_by_id() {
public void shouldUpdateHouseById() {
House house = new House();
entityManager.persist(house);
......@@ -127,7 +126,7 @@ public class UnitTestHouseJPA {
}
@Test
public void should_delete_tutorial_by_id() {
public void shouldDeleteById() {
House house = new House();
entityManager.persist(house);
......@@ -141,7 +140,7 @@ public class UnitTestHouseJPA {
}
@Test
public void should_delete_all() {
public void shouldDeleteAll() {
entityManager.persist(helperRegisterHouse());
entityManager.persist(helperRegisterHouse());
......
package cz.muni.fi.pa165.core.metrics;
import cz.muni.fi.pa165.core.house.HouseFacade;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(controllers = MetricsController.class)
public class MetricsControllerTest {
@Autowired
private MockMvc mockMvc;
private static String metrics = "/api/metrics";
@MockBean
private MetricsFacade metricsFacade;
@Test
public void shouldReturnEntranceDataGetAllHouses() throws Exception {
this.mockMvc.perform(get(metrics))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString("")));
}
}
package cz.muni.fi.pa165.core.user;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import java.util.List;
import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(controllers = UserController.class)
public class UserControllerTest {
@Autowired private MockMvc mockMvc;
@MockBean
private UserFacade userFacade;
@Autowired private ObjectMapper objectMapper;
@MockBean private UserService service;
private static String pathUser = "/api/user/all";
private static String pathSmartMether = "/api/smart-meter";
private static String manufacturer = "/api/manufacturer";
@BeforeEach
public void setUp() {
List.of(
new User()
.builder()
.firstName("Marek")
.lastName("Pavel")
.email("marek@gmail.cz")
.userType(UserType.NORMAL)
.password("adminadmin")
.build(),
new User()
.builder()
.firstName("John")
.lastName("Doe")
.email("john.doe@gmail.com")
.userType(UserType.NORMAL)
.password("password123")
.build(),
new User()
.builder()
.firstName("Jane")
.lastName("Doe")
.email("jane.doe@gmail.com")
.userType(UserType.ADMIN)
.password("qwertyuiop")
.build())
.stream()
.forEach(service::create);
}
@AfterEach
public void dropData() {
service.deleteAllHardDelete();
}
/*@Test
void getUserTest() throws Exception {
for (User user : service.findAll()) {
mockMvc.perform(get("/api/user/" + user.getId())).andExpect(status().isOk());
/*.andExpect(content().json("{\"id\":\"" + user.getId()
+ "\",\"username\":\""
+ user.getUsername()
+ "\",\"email\":\""
+ user.getEmail()
+ "\",\"firstName\":\""
+ user.getFirstName()
+ "\",\"lastName\":\""
+ user.getLastName()
+ "\"}"));
}
}*/
@Test
public void shouldReturnEntranceData() throws Exception {
this.mockMvc.perform(get(pathUser))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString(pathUser)));
}
}
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