package org.jhipster.health.web.rest;
import org.jhipster.health.Application;
import org.jhipster.health.domain.BloodPressure;
import org.jhipster.health.domain.User;
import org.jhipster.health.repository.BloodPressureRepository;
import org.jhipster.health.repository.UserRepository;
import org.jhipster.health.repository.search.BloodPressureSearchRepository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
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.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* Test class for the BloodPressureResource REST controller.
*
* @see BloodPressureResource
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class BloodPressureResourceIntTest {
private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").withZone(ZoneId.of("Z"));
private static final ZonedDateTime DEFAULT_TIMESTAMP = ZonedDateTime.ofInstant(Instant.ofEpochMilli(0L), ZoneId.systemDefault());
private static final ZonedDateTime UPDATED_TIMESTAMP = ZonedDateTime.now(ZoneId.systemDefault()).withNano(0);
private static final String DEFAULT_TIMESTAMP_STR = dateTimeFormatter.format(DEFAULT_TIMESTAMP);
private static final Integer DEFAULT_SYSTOLIC = 1;
private static final Integer UPDATED_SYSTOLIC = 2;
private static final Integer DEFAULT_DIASTOLIC = 1;
private static final Integer UPDATED_DIASTOLIC = 2;
@Inject
private BloodPressureRepository bloodPressureRepository;
@Inject
private BloodPressureSearchRepository bloodPressureSearchRepository;
@Inject
private UserRepository userRepository;
@Inject
private MappingJackson2HttpMessageConverter jacksonMessageConverter;
@Inject
private PageableHandlerMethodArgumentResolver pageableArgumentResolver;
@Inject
private EntityManager em;
@Inject
private WebApplicationContext context;
private MockMvc restBloodPressureMockMvc;
private BloodPressure bloodPressure;
@PostConstruct
public void setup() {
MockitoAnnotations.initMocks(this);
BloodPressureResource bloodPressureResource = new BloodPressureResource();
ReflectionTestUtils.setField(bloodPressureResource, "bloodPressureSearchRepository", bloodPressureSearchRepository);
ReflectionTestUtils.setField(bloodPressureResource, "bloodPressureRepository", bloodPressureRepository);
this.restBloodPressureMockMvc = MockMvcBuilders.standaloneSetup(bloodPressureResource)
.setCustomArgumentResolvers(pageableArgumentResolver)
.setMessageConverters(jacksonMessageConverter).build();
}
/**
* Create an entity for this test.
*
* This is a static method, as tests for other entities might also need it,
* if they test an entity which requires the current entity.
*/
public static BloodPressure createEntity(EntityManager em) {
BloodPressure bloodPressure = new BloodPressure();
bloodPressure = new BloodPressure()
.timestamp(DEFAULT_TIMESTAMP)
.systolic(DEFAULT_SYSTOLIC)
.diastolic(DEFAULT_DIASTOLIC);
return bloodPressure;
}
@Before
public void initTest() {
bloodPressureSearchRepository.deleteAll();
bloodPressure = createEntity(em);
}
@Test
@Transactional
public void createBloodPressure() throws Exception {
int databaseSizeBeforeCreate = bloodPressureRepository.findAll().size();
// create security-aware mockMvc
restBloodPressureMockMvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(springSecurity())
.build();
// Create the BloodPressure
restBloodPressureMockMvc.perform(post("/api/blood-pressures")
.with(user("user"))
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(bloodPressure)))
.andExpect(status().isCreated());
// Validate the BloodPressure in the database
List<BloodPressure> bloodPressures = bloodPressureRepository.findAll();
assertThat(bloodPressures).hasSize(databaseSizeBeforeCreate + 1);
BloodPressure testBloodPressure = bloodPressures.get(bloodPressures.size() - 1);
assertThat(testBloodPressure.getTimestamp()).isEqualTo(DEFAULT_TIMESTAMP);
assertThat(testBloodPressure.getSystolic()).isEqualTo(DEFAULT_SYSTOLIC);
assertThat(testBloodPressure.getDiastolic()).isEqualTo(DEFAULT_DIASTOLIC);
// Validate the BloodPressure in ElasticSearch
BloodPressure bloodPressureEs = bloodPressureSearchRepository.findOne(testBloodPressure.getId());
assertThat(bloodPressureEs).isEqualToComparingFieldByField(testBloodPressure);
}
@Test
@Transactional
public void checkTimestampIsRequired() throws Exception {
int databaseSizeBeforeTest = bloodPressureRepository.findAll().size();
// set the field null
bloodPressure.setTimestamp(null);
// Create the BloodPressure, which fails.
restBloodPressureMockMvc.perform(post("/api/blood-pressures")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(bloodPressure)))
.andExpect(status().isBadRequest());
List<BloodPressure> bloodPressures = bloodPressureRepository.findAll();
assertThat(bloodPressures).hasSize(databaseSizeBeforeTest);
}
@Test
@Transactional
public void checkSystolicIsRequired() throws Exception {
int databaseSizeBeforeTest = bloodPressureRepository.findAll().size();
// set the field null
bloodPressure.setSystolic(null);
// Create the BloodPressure, which fails.
restBloodPressureMockMvc.perform(post("/api/blood-pressures")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(bloodPressure)))
.andExpect(status().isBadRequest());
List<BloodPressure> bloodPressures = bloodPressureRepository.findAll();
assertThat(bloodPressures).hasSize(databaseSizeBeforeTest);
}
@Test
@Transactional
public void checkDiastolicIsRequired() throws Exception {
int databaseSizeBeforeTest = bloodPressureRepository.findAll().size();
// set the field null
bloodPressure.setDiastolic(null);
// Create the BloodPressure, which fails.
restBloodPressureMockMvc.perform(post("/api/blood-pressures")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(bloodPressure)))
.andExpect(status().isBadRequest());
List<BloodPressure> bloodPressures = bloodPressureRepository.findAll();
assertThat(bloodPressures).hasSize(databaseSizeBeforeTest);
}
@Test
@Transactional
public void getAllBloodPressures() throws Exception {
// Initialize the database
bloodPressureRepository.saveAndFlush(bloodPressure);
restBloodPressureMockMvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(springSecurity())
.build();
// Get all the bloodPressures
restBloodPressureMockMvc.perform(get("/api/blood-pressures?sort=id,desc")
.with(user("admin").roles("ADMIN")))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$.[*].id").value(hasItem(bloodPressure.getId().intValue())))
.andExpect(jsonPath("$.[*].timestamp").value(hasItem(DEFAULT_TIMESTAMP_STR)))
.andExpect(jsonPath("$.[*].systolic").value(hasItem(DEFAULT_SYSTOLIC)))
.andExpect(jsonPath("$.[*].diastolic").value(hasItem(DEFAULT_DIASTOLIC)));
}
@Test
@Transactional
public void getBloodPressure() throws Exception {
// Initialize the database
bloodPressureRepository.saveAndFlush(bloodPressure);
// Get the bloodPressure
restBloodPressureMockMvc.perform(get("/api/blood-pressures/{id}", bloodPressure.getId()))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$.id").value(bloodPressure.getId().intValue()))
.andExpect(jsonPath("$.timestamp").value(DEFAULT_TIMESTAMP_STR))
.andExpect(jsonPath("$.systolic").value(DEFAULT_SYSTOLIC))
.andExpect(jsonPath("$.diastolic").value(DEFAULT_DIASTOLIC));
}
@Test
@Transactional
public void getNonExistingBloodPressure() throws Exception {
// Get the bloodPressure
restBloodPressureMockMvc.perform(get("/api/blood-pressures/{id}", Long.MAX_VALUE))
.andExpect(status().isNotFound());
}
@Test
@Transactional
public void updateBloodPressure() throws Exception {
// Initialize the database
bloodPressureRepository.saveAndFlush(bloodPressure);
bloodPressureSearchRepository.save(bloodPressure);
int databaseSizeBeforeUpdate = bloodPressureRepository.findAll().size();
// Update the bloodPressure
BloodPressure updatedBloodPressure = bloodPressureRepository.findOne(bloodPressure.getId());
updatedBloodPressure
.timestamp(UPDATED_TIMESTAMP)
.systolic(UPDATED_SYSTOLIC)
.diastolic(UPDATED_DIASTOLIC);
restBloodPressureMockMvc.perform(put("/api/blood-pressures")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(updatedBloodPressure)))
.andExpect(status().isOk());
// Validate the BloodPressure in the database
List<BloodPressure> bloodPressures = bloodPressureRepository.findAll();
assertThat(bloodPressures).hasSize(databaseSizeBeforeUpdate);
BloodPressure testBloodPressure = bloodPressures.get(bloodPressures.size() - 1);
assertThat(testBloodPressure.getTimestamp()).isEqualTo(UPDATED_TIMESTAMP);
assertThat(testBloodPressure.getSystolic()).isEqualTo(UPDATED_SYSTOLIC);
assertThat(testBloodPressure.getDiastolic()).isEqualTo(UPDATED_DIASTOLIC);
// Validate the BloodPressure in ElasticSearch
BloodPressure bloodPressureEs = bloodPressureSearchRepository.findOne(testBloodPressure.getId());
assertThat(bloodPressureEs).isEqualToComparingFieldByField(testBloodPressure);
}
@Test
@Transactional
public void deleteBloodPressure() throws Exception {
// Initialize the database
bloodPressureRepository.saveAndFlush(bloodPressure);
bloodPressureSearchRepository.save(bloodPressure);
int databaseSizeBeforeDelete = bloodPressureRepository.findAll().size();
// Get the bloodPressure
restBloodPressureMockMvc.perform(delete("/api/blood-pressures/{id}", bloodPressure.getId())
.accept(TestUtil.APPLICATION_JSON_UTF8))
.andExpect(status().isOk());
// Validate ElasticSearch is empty
boolean bloodPressureExistsInEs = bloodPressureSearchRepository.exists(bloodPressure.getId());
assertThat(bloodPressureExistsInEs).isFalse();
// Validate the database is empty
List<BloodPressure> bloodPressures = bloodPressureRepository.findAll();
assertThat(bloodPressures).hasSize(databaseSizeBeforeDelete - 1);
}
@Test
@Transactional
public void searchBloodPressure() throws Exception {
// Initialize the database
bloodPressureRepository.saveAndFlush(bloodPressure);
bloodPressureSearchRepository.save(bloodPressure);
// Search the bloodPressure
restBloodPressureMockMvc.perform(get("/api/_search/blood-pressures?query=id:" + bloodPressure.getId()))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$.[*].id").value(hasItem(bloodPressure.getId().intValue())))
.andExpect(jsonPath("$.[*].timestamp").value(hasItem(DEFAULT_TIMESTAMP_STR)))
.andExpect(jsonPath("$.[*].systolic").value(hasItem(DEFAULT_SYSTOLIC)))
.andExpect(jsonPath("$.[*].diastolic").value(hasItem(DEFAULT_DIASTOLIC)));
}
private void createBloodPressureByMonth(ZonedDateTime firstDate, ZonedDateTime firstDayOfLastMonth) {
User user = userRepository.findOneByLogin("user").get();
bloodPressure = new BloodPressure(firstDate, 120, 80, user);
bloodPressureRepository.saveAndFlush(bloodPressure);
bloodPressure = new BloodPressure(firstDate.plusDays(10), 125, 75, user);
bloodPressureRepository.saveAndFlush(bloodPressure);
bloodPressure = new BloodPressure(firstDate.plusDays(20), 100, 69, user);
bloodPressureRepository.saveAndFlush(bloodPressure);
// last month
bloodPressure = new BloodPressure(firstDayOfLastMonth, 130, 90, user);
bloodPressureRepository.saveAndFlush(bloodPressure);
bloodPressure = new BloodPressure(firstDayOfLastMonth.plusDays(11), 135, 85, user);
bloodPressureRepository.saveAndFlush(bloodPressure);
bloodPressure = new BloodPressure(firstDayOfLastMonth.plusDays(23), 130, 75, user);
bloodPressureRepository.saveAndFlush(bloodPressure);
}
@Test
@Transactional
public void getBloodPressureForLast30Days() throws Exception {
ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime twentyNineDaysAgo = now.minusDays(29);
ZonedDateTime firstDayOfLastMonth = now.withDayOfMonth(1).minusMonths(1);
createBloodPressureByMonth(twentyNineDaysAgo, firstDayOfLastMonth);
// create security-aware mockMvc
restBloodPressureMockMvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(springSecurity())
.build();
// Get all the blood pressure readings
restBloodPressureMockMvc.perform(get("/api/blood-pressures")
.with(user("user").roles("USER")))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$", hasSize(6)));
// Get the blood pressure readings for the last 30 days
restBloodPressureMockMvc.perform(get("/api/bp-by-days/{days}", 30)
.with(user("user").roles("USER")))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.period").value("Last 30 Days"))
.andExpect(jsonPath("$.readings.[*].systolic").value(hasItem(120)))
.andExpect(jsonPath("$.readings.[*].diastolic").value(hasItem(69)));
}
@Test
@Transactional
public void getBloodPressureByMonth() throws Exception {
ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime firstOfMonth = now.withDayOfMonth(1);
ZonedDateTime firstDayOfLastMonth = firstOfMonth.minusMonths(1);
createBloodPressureByMonth(firstOfMonth, firstDayOfLastMonth);
// create security-aware mockMvc
restBloodPressureMockMvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(springSecurity())
.build();
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM");
// Get the points for last week
restBloodPressureMockMvc.perform(get("/api/bp-by-month/{yearAndMonth}", fmt.format(firstDayOfLastMonth))
.with(user("user").roles("USER")))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.period").value(fmt.format(firstDayOfLastMonth)))
.andExpect(jsonPath("$.readings.[*].systolic").value(hasItem(130)))
.andExpect(jsonPath("$.readings.[*].diastolic").value(hasItem(90)));
}
}