package org.jhipster.health.web.rest;
import org.jhipster.health.Application;
import org.jhipster.health.domain.User;
import org.jhipster.health.domain.Weight;
import org.jhipster.health.repository.UserRepository;
import org.jhipster.health.repository.WeightRepository;
import org.jhipster.health.repository.search.WeightSearchRepository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 WeightResource REST controller.
*
* @see WeightResource
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class WeightResourceIntTest {
private final Logger log = LoggerFactory.getLogger(WeightResourceIntTest.class);
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 Double DEFAULT_WEIGHT = 1D;
private static final Double UPDATED_WEIGHT = 2D;
@Inject
private WeightRepository weightRepository;
@Inject
private WeightSearchRepository weightSearchRepository;
@Inject
private UserRepository userRepository;
@Inject
private MappingJackson2HttpMessageConverter jacksonMessageConverter;
@Inject
private PageableHandlerMethodArgumentResolver pageableArgumentResolver;
@Inject
private EntityManager em;
@Inject
private WebApplicationContext context;
private MockMvc restWeightMockMvc;
private Weight weight;
@PostConstruct
public void setup() {
MockitoAnnotations.initMocks(this);
WeightResource weightResource = new WeightResource();
ReflectionTestUtils.setField(weightResource, "weightSearchRepository", weightSearchRepository);
ReflectionTestUtils.setField(weightResource, "weightRepository", weightRepository);
this.restWeightMockMvc = MockMvcBuilders.standaloneSetup(weightResource)
.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 Weight createEntity(EntityManager em) {
Weight weight = new Weight();
weight = new Weight()
.timestamp(DEFAULT_TIMESTAMP)
.weight(DEFAULT_WEIGHT);
return weight;
}
@Before
public void initTest() {
weightSearchRepository.deleteAll();
weight = createEntity(em);
}
@Test
@Transactional
public void createWeight() throws Exception {
int databaseSizeBeforeCreate = weightRepository.findAll().size();
// create security-aware mockMvc
restWeightMockMvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(springSecurity())
.build();
// Create the Weight
restWeightMockMvc.perform(post("/api/weights")
.with(user("user"))
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(weight)))
.andExpect(status().isCreated());
// Validate the Weight in the database
List<Weight> weights = weightRepository.findAll();
assertThat(weights).hasSize(databaseSizeBeforeCreate + 1);
Weight testWeight = weights.get(weights.size() - 1);
assertThat(testWeight.getTimestamp()).isEqualTo(DEFAULT_TIMESTAMP);
assertThat(testWeight.getWeight()).isEqualTo(DEFAULT_WEIGHT);
// Validate the Weight in ElasticSearch
Weight weightEs = weightSearchRepository.findOne(testWeight.getId());
assertThat(weightEs).isEqualToComparingFieldByField(testWeight);
}
@Test
@Transactional
public void checkTimestampIsRequired() throws Exception {
int databaseSizeBeforeTest = weightRepository.findAll().size();
// set the field null
weight.setTimestamp(null);
// Create the Weight, which fails.
restWeightMockMvc.perform(post("/api/weights")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(weight)))
.andExpect(status().isBadRequest());
List<Weight> weights = weightRepository.findAll();
assertThat(weights).hasSize(databaseSizeBeforeTest);
}
@Test
@Transactional
public void checkWeightIsRequired() throws Exception {
int databaseSizeBeforeTest = weightRepository.findAll().size();
// set the field null
weight.setWeight(null);
// Create the Weight, which fails.
restWeightMockMvc.perform(post("/api/weights")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(weight)))
.andExpect(status().isBadRequest());
List<Weight> weights = weightRepository.findAll();
assertThat(weights).hasSize(databaseSizeBeforeTest);
}
@Test
@Transactional
public void getAllWeights() throws Exception {
// Initialize the database
weightRepository.saveAndFlush(weight);
// create security-aware mockMvc
restWeightMockMvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(springSecurity())
.build();
// Get all the weights
restWeightMockMvc.perform(get("/api/weights?sort=id,desc")
.with(user("admin").roles("ADMIN")))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$.[*].id").value(hasItem(weight.getId().intValue())))
.andExpect(jsonPath("$.[*].timestamp").value(hasItem(DEFAULT_TIMESTAMP_STR)))
.andExpect(jsonPath("$.[*].weight").value(hasItem(DEFAULT_WEIGHT.doubleValue())));
}
@Test
@Transactional
public void getWeight() throws Exception {
// Initialize the database
weightRepository.saveAndFlush(weight);
// Get the weight
restWeightMockMvc.perform(get("/api/weights/{id}", weight.getId()))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$.id").value(weight.getId().intValue()))
.andExpect(jsonPath("$.timestamp").value(DEFAULT_TIMESTAMP_STR))
.andExpect(jsonPath("$.weight").value(DEFAULT_WEIGHT.doubleValue()));
}
@Test
@Transactional
public void getNonExistingWeight() throws Exception {
// Get the weight
restWeightMockMvc.perform(get("/api/weights/{id}", Long.MAX_VALUE))
.andExpect(status().isNotFound());
}
@Test
@Transactional
public void updateWeight() throws Exception {
// Initialize the database
weightRepository.saveAndFlush(weight);
weightSearchRepository.save(weight);
int databaseSizeBeforeUpdate = weightRepository.findAll().size();
// Update the weight
Weight updatedWeight = weightRepository.findOne(weight.getId());
updatedWeight
.timestamp(UPDATED_TIMESTAMP)
.weight(UPDATED_WEIGHT);
restWeightMockMvc.perform(put("/api/weights")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(updatedWeight)))
.andExpect(status().isOk());
// Validate the Weight in the database
List<Weight> weights = weightRepository.findAll();
assertThat(weights).hasSize(databaseSizeBeforeUpdate);
Weight testWeight = weights.get(weights.size() - 1);
assertThat(testWeight.getTimestamp()).isEqualTo(UPDATED_TIMESTAMP);
assertThat(testWeight.getWeight()).isEqualTo(UPDATED_WEIGHT);
// Validate the Weight in ElasticSearch
Weight weightEs = weightSearchRepository.findOne(testWeight.getId());
assertThat(weightEs).isEqualToComparingFieldByField(testWeight);
}
@Test
@Transactional
public void deleteWeight() throws Exception {
// Initialize the database
weightRepository.saveAndFlush(weight);
weightSearchRepository.save(weight);
int databaseSizeBeforeDelete = weightRepository.findAll().size();
// Get the weight
restWeightMockMvc.perform(delete("/api/weights/{id}", weight.getId())
.accept(TestUtil.APPLICATION_JSON_UTF8))
.andExpect(status().isOk());
// Validate ElasticSearch is empty
boolean weightExistsInEs = weightSearchRepository.exists(weight.getId());
assertThat(weightExistsInEs).isFalse();
// Validate the database is empty
List<Weight> weights = weightRepository.findAll();
assertThat(weights).hasSize(databaseSizeBeforeDelete - 1);
}
@Test
@Transactional
public void searchWeight() throws Exception {
// Initialize the database
weightRepository.saveAndFlush(weight);
weightSearchRepository.save(weight);
// Search the weight
restWeightMockMvc.perform(get("/api/_search/weights?query=id:" + weight.getId()))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$.[*].id").value(hasItem(weight.getId().intValue())))
.andExpect(jsonPath("$.[*].timestamp").value(hasItem(DEFAULT_TIMESTAMP_STR)))
.andExpect(jsonPath("$.[*].weight").value(hasItem(DEFAULT_WEIGHT.doubleValue())));
}
private void createByMonth(ZonedDateTime firstDate, ZonedDateTime firstDayOfLastMonth) {
log.debug("firstDate: {}, firstOfLastMonth: {}", firstDate.toString(), firstDayOfLastMonth.toString());
User user = userRepository.findOneByLogin("user").get();
weightRepository.saveAndFlush(new Weight(firstDate, 205D, user));
weightRepository.saveAndFlush(new Weight(firstDate.plusDays(10), 200D, user));
weightRepository.saveAndFlush(new Weight(firstDate.plusDays(20), 195D, user));
// last month
weightRepository.saveAndFlush(new Weight(firstDayOfLastMonth, 208D, user));
weightRepository.saveAndFlush(new Weight(firstDayOfLastMonth.plusDays(11), 206D, user));
weightRepository.saveAndFlush(new Weight(firstDayOfLastMonth.plusDays(23), 204D, user));
}
@Test
@Transactional
public void getForLast30Days() throws Exception {
ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime thirtyDaysAgo = now.minusDays(30);
ZonedDateTime firstDayOfLastMonth = now.withDayOfMonth(1).minusMonths(1);
createByMonth(thirtyDaysAgo, firstDayOfLastMonth);
// create security-aware mockMvc
restWeightMockMvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(springSecurity())
.build();
// Get all the weighIns
restWeightMockMvc.perform(get("/api/weights")
.with(user("user").roles("USER")))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$", hasSize(6)));
// Get the weighIns for the last 30 days
restWeightMockMvc.perform(get("/api/weight-by-days/{days}", 30)
.with(user("user").roles("USER")))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$.period").value("Last 30 Days"))
.andExpect(jsonPath("$.weighIns.[*].weight").value(hasItem(200D)));
}
@Test
@Transactional
public void getByLastMonth() throws Exception {
ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime firstOfMonth = now.withDayOfMonth(1);
ZonedDateTime firstDayOfLastMonth = firstOfMonth.minusMonths(1);
createByMonth(firstOfMonth, firstDayOfLastMonth);
// create security-aware mockMvc
restWeightMockMvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(springSecurity())
.build();
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM");
// Get the points for last week
restWeightMockMvc.perform(get("/api/weight-by-month/{yearAndMonth}", fmt.format(firstDayOfLastMonth))
.with(user("user").roles("USER")))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$.period").value(fmt.format(firstDayOfLastMonth)))
.andExpect(jsonPath("$.weighIns.[*].weight").value(hasItem(206D)));
}
}