Skip to content

Commit

Permalink
Merge pull request #22 from elwyncrestha/test/doctor-list
Browse files Browse the repository at this point in the history
Feature: Added test cases for doctor.
  • Loading branch information
elwyncrestha committed Jul 3, 2020
2 parents c72b4ab + 6dc5b14 commit cb292f7
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
Expand Down Expand Up @@ -79,7 +80,7 @@ public void testFindDepartmentByIdShouldReturnDepartment() {
public void testFindAllShouldReturnNotEmptyList() {
final List<Department> departments = repository.findAll();

assertThat(departments.size(), equalTo(1));
assertThat(departments.size(), greaterThan(0));
}

@Test
Expand All @@ -93,19 +94,22 @@ public void testGetOneShouldReturnDepartment() {
@Test
@DatabaseSetup("/dataset/department/department-config.xml")
public void testDeleteByIdShouldDeleteDepartment() {
long count = repository.count();

repository.deleteById(MOCK_OPD_DEPARTMENT_ID);

assertThat(repository.findAll(), hasSize(0));
assertThat(repository.findAll(), hasSize((int) count - 1));
}

@Test
@DatabaseSetup("/dataset/department/department-config.xml")
public void testDeleteShouldDeleteDepartment() {
final Department department = repository.getOne(MOCK_OPD_DEPARTMENT_ID);
long count = repository.count();

final Department department = repository.getOne(MOCK_OPD_DEPARTMENT_ID);
repository.delete(department);

assertThat(repository.findAll(), hasSize(0));
assertThat(repository.findAll(), hasSize((int) count - 1));
}

@Test
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package com.pemits.webcare.api.doctor.repository;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.github.springtestdbunit.annotation.DatabaseSetup;
import com.pemits.webcare.BaseJpaTest;
import com.pemits.webcare.api.department.repository.DepartmentRepository;
import com.pemits.webcare.api.doctor.entity.Doctor;
import com.pemits.webcare.api.user.repository.UserRepository;

public class DoctorRepositoryTest extends BaseJpaTest {

private static final long MOCK_DOCTOR1_ID = 1L;
private static final String MOCK_DOCTOR1_SPECIALIZATION_FIELD = "Dummy speciality 1";
private static final long MOCK_DOCTOR2_ID = 2L;
private static final String MOCK_DOCTOR2_SPECIALIZATION_FIELD = "Dummy speciality 2";

@Autowired
private DoctorRepository repository;

@Autowired
private UserRepository userRepository;

@Autowired
private DepartmentRepository departmentRepository;

@Test
@DatabaseSetup("/dataset/user/users-of-type-doctor.xml")
@DatabaseSetup("/dataset/department/department-config.xml")
public void testSaveShouldReturnSavedDoctor() {
Doctor doctor = new Doctor();
doctor.setUser(userRepository.getOne(1L));
doctor.setDepartment(departmentRepository.getOne(1L));
doctor.setSpecializationField(MOCK_DOCTOR1_SPECIALIZATION_FIELD);

Doctor saved = repository.save(doctor);

assertThat(saved.getId(), notNullValue());
assertThat(repository.count(), equalTo(1L));
}

@Test
@DatabaseSetup("/dataset/user/users-of-type-doctor.xml")
@DatabaseSetup("/dataset/department/department-config.xml")
public void testSaveAllShouldReturnAllSavedDoctors() {
Doctor doctor1 = new Doctor();
doctor1.setUser(userRepository.getOne(1L));
doctor1.setDepartment(departmentRepository.getOne(1L));
doctor1.setSpecializationField("Dummy speciality 1");

Doctor doctor2 = new Doctor();
doctor2.setUser(userRepository.getOne(2L));
doctor2.setDepartment(departmentRepository.getOne(2L));
doctor2.setSpecializationField("Dummy speciality 2");

List<Doctor> saved = repository.saveAll(Arrays.asList(doctor1, doctor2));

assertThat(saved, hasSize(2));
assertThat(saved.get(0).getId(), notNullValue());
assertThat(saved.get(1).getId(), notNullValue());
assertThat(repository.count(), equalTo(2L));
}

@Test
@DatabaseSetup("/dataset/user/users-of-type-doctor.xml")
@DatabaseSetup("/dataset/department/department-config.xml")
@DatabaseSetup("/dataset/doctor/doctor-config.xml")
public void testSaveShouldUpdateDoctor() {
Doctor doctor = repository.getOne(1L);

assertThat(doctor.getSpecializationField(), is("Dummy speciality 1"));

String update = "Dummy speciality 9";
doctor.setSpecializationField(update);
repository.save(doctor);

assertThat(repository.getOne(1L).getSpecializationField(), is(update));
}

@Test
@DatabaseSetup("/dataset/user/users-of-type-doctor.xml")
@DatabaseSetup("/dataset/department/department-config.xml")
@DatabaseSetup("/dataset/doctor/doctor-config.xml")
public void testFindDoctorByIdShouldReturnDoctor() {
final Optional<Doctor> doctor = repository.findById(1L);

assertThat(doctor.isPresent(), is(true));
assertThat(doctor.get().getSpecializationField(), is("Dummy speciality 1"));
}

@Test
@DatabaseSetup("/dataset/user/users-of-type-doctor.xml")
@DatabaseSetup("/dataset/department/department-config.xml")
@DatabaseSetup("/dataset/doctor/doctor-config.xml")
public void testFindAllShouldReturnNotEmptyList() {
final List<Doctor> doctors = repository.findAll();

assertThat(doctors.size(), equalTo(4));
}

@Test
@DatabaseSetup("/dataset/user/users-of-type-doctor.xml")
@DatabaseSetup("/dataset/department/department-config.xml")
@DatabaseSetup("/dataset/doctor/doctor-config.xml")
public void testGetOneShouldReturnDoctor() {
final Doctor doctor = repository.getOne(MOCK_DOCTOR2_ID);

assertThat(doctor.getSpecializationField(), is(MOCK_DOCTOR2_SPECIALIZATION_FIELD));
}

@Test
@DatabaseSetup("/dataset/user/users-of-type-doctor.xml")
@DatabaseSetup("/dataset/department/department-config.xml")
@DatabaseSetup("/dataset/doctor/doctor-config.xml")
public void testDeleteByIdShouldDeleteDoctor() {
long count = repository.count();

repository.deleteById(MOCK_DOCTOR2_ID);

assertThat(repository.findAll(), hasSize((int) (count - 1)));
}

@Test
@DatabaseSetup("/dataset/user/users-of-type-doctor.xml")
@DatabaseSetup("/dataset/department/department-config.xml")
@DatabaseSetup("/dataset/doctor/doctor-config.xml")
public void testDeleteShouldDeleteDepartment() {
long count = repository.count();

final Doctor doctor = repository.getOne(MOCK_DOCTOR1_ID);
repository.delete(doctor);

assertThat(repository.findAll(), hasSize((int) (count - 1)));
}

@Test
@DatabaseSetup("/dataset/user/users-of-type-doctor.xml")
@DatabaseSetup("/dataset/department/department-config.xml")
@DatabaseSetup("/dataset/doctor/doctor-config.xml")
public void testDeleteAllShouldReturnCountZero() {
repository.deleteAll();

assertThat(repository.count(), is(0L));
}
}
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,25 @@
version="0"
name="OPD"
/>
<department
id="2"
created_at="2020-06-20 15:30:00"
last_modified_at="2020-06-20 15:30:00"
version="0"
name="Anesthetics"
/>
<department
id="3"
created_at="2020-06-20 15:30:00"
last_modified_at="2020-06-20 15:30:00"
version="0"
name="Breast Screening"
/>
<department
id="4"
created_at="2020-06-20 15:30:00"
last_modified_at="2020-06-20 15:30:00"
version="0"
name="Cardiology"
/>
</dataset>
39 changes: 39 additions & 0 deletions pemits-api/src/test/resources/dataset/doctor/doctor-config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<dataset>
<!-- NULL: created_by, last_modified_by, email -->
<doctor
id="1"
created_at="2020-06-20 15:30:00"
last_modified_at="2020-06-20 15:30:00"
version="0"
user_id="1"
department_id="1"
specialization_field="Dummy speciality 1"
/>
<doctor
id="2"
created_at="2020-06-20 15:30:00"
last_modified_at="2020-06-20 15:30:00"
version="0"
user_id="2"
department_id="1"
specialization_field="Dummy speciality 2"
/>
<doctor
id="3"
created_at="2020-06-20 15:30:00"
last_modified_at="2020-06-20 15:30:00"
version="0"
user_id="3"
department_id="2"
specialization_field="Dummy speciality 3"
/>
<doctor
id="4"
created_at="2020-06-20 15:30:00"
last_modified_at="2020-06-20 15:30:00"
version="0"
user_id="4"
department_id="3"
specialization_field="Dummy speciality 4"
/>
</dataset>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<dataset>
<!-- NULL: created_by, last_modified_by, email -->
<users
id="1"
created_at="2020-06-20 15:30:00"
last_modified_at="2020-06-20 15:30:00"
version="0"
username="john"
password="$2a$10$CGkAwfRBRIMVoEX8Ui9yx.NC03wKCjE19KIGVNET2F1mn0o58jkly"
name="John Doe"
status="1"
user_type="2"
/>
<users
id="2"
created_at="2020-06-20 15:30:00"
last_modified_at="2020-06-20 15:30:00"
version="0"
username="jane"
password="$2a$10$CGkAwfRBRIMVoEX8Ui9yx.NC03wKCjE19KIGVNET2F1mn0o58jkly"
name="Jane Doe"
status="1"
user_type="2"
/>
<users
id="3"
created_at="2020-06-20 15:30:00"
last_modified_at="2020-06-20 15:30:00"
version="0"
username="jack"
password="$2a$10$CGkAwfRBRIMVoEX8Ui9yx.NC03wKCjE19KIGVNET2F1mn0o58jkly"
name="Jack Doe"
status="1"
user_type="2"
/>
<users
id="4"
created_at="2020-06-20 15:30:00"
last_modified_at="2020-06-20 15:30:00"
version="0"
username="marry"
password="$2a$10$CGkAwfRBRIMVoEX8Ui9yx.NC03wKCjE19KIGVNET2F1mn0o58jkly"
name="Marry Doe"
status="1"
user_type="2"
/>
</dataset>

0 comments on commit cb292f7

Please sign in to comment.