2 * In Samsung Ukraine R&D Center (SRK under a contract between)
3 * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
4 * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
6 package com.samsung.samserver.web.rest.crud;
8 import com.samsung.samserver.SamserverApp;
10 import com.samsung.samserver.domain.Device;
11 import com.samsung.samserver.repository.DeviceRepository;
12 import com.samsung.samserver.service.DeviceService;
13 import com.samsung.samserver.web.rest.TestUtil;
14 import com.samsung.samserver.web.rest.errors.ExceptionTranslator;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.MockitoAnnotations;
20 import org.springframework.beans.factory.annotation.Autowired;
21 import org.springframework.boot.test.context.SpringBootTest;
22 import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
23 import org.springframework.http.MediaType;
24 import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
25 import org.springframework.test.context.junit4.SpringRunner;
26 import org.springframework.test.web.servlet.MockMvc;
27 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
28 import org.springframework.transaction.annotation.Transactional;
30 import javax.persistence.EntityManager;
31 import java.time.Instant;
32 import java.time.temporal.ChronoUnit;
33 import java.util.List;
35 import static com.samsung.samserver.web.rest.TestUtil.createFormattingConversionService;
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.hamcrest.Matchers.hasItem;
38 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
39 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
42 * Test class for the DeviceResource REST controller.
46 @RunWith(SpringRunner.class)
47 @SpringBootTest(classes = SamserverApp.class)
48 public class DeviceResourceTest {
50 private static final String DEFAULT_DUID = "AAAAAAAAAA";
51 private static final String UPDATED_DUID = "BBBBBBBBBB";
53 private static final Instant DEFAULT_CTIME = Instant.ofEpochMilli(0L);
54 private static final Instant UPDATED_CTIME = Instant.now().truncatedTo(ChronoUnit.MILLIS);
56 private static final String DEFAULT_MODEL = "AAAAAAAAAA";
57 private static final String UPDATED_MODEL = "BBBBBBBBBB";
59 private static final String DEFAULT_LOCATION = "AAAAAAAAAA";
60 private static final String UPDATED_LOCATION = "BBBBBBBBBB";
62 private static final String DEFAULT_SN = "AAAAAAAAAA";
63 private static final String UPDATED_SN = "BBBBBBBBBB";
65 private static final String DEFAULT_DESCR = "AAAAAAAAAA";
66 private static final String UPDATED_DESCR = "BBBBBBBBBB";
68 private static final String DEFAULT_LOCKED = "AAAAAAAAAA";
69 private static final String UPDATED_LOCKED = "BBBBBBBBBB";
72 private DeviceRepository deviceRepository;
75 private DeviceService deviceService;
78 private MappingJackson2HttpMessageConverter jacksonMessageConverter;
81 private PageableHandlerMethodArgumentResolver pageableArgumentResolver;
84 private ExceptionTranslator exceptionTranslator;
87 private EntityManager em;
89 private MockMvc restDeviceMockMvc;
91 private Device device;
95 MockitoAnnotations.initMocks(this);
96 final DeviceResource deviceResource = new DeviceResource(deviceService);
97 this.restDeviceMockMvc = MockMvcBuilders.standaloneSetup(deviceResource)
98 .setCustomArgumentResolvers(pageableArgumentResolver)
99 .setControllerAdvice(exceptionTranslator)
100 .setConversionService(createFormattingConversionService())
101 .setMessageConverters(jacksonMessageConverter).build();
105 * Create an entity for this test.
107 * This is a static method, as tests for other entities might also need it,
108 * if they test an entity which requires the current entity.
110 public static Device createEntity(EntityManager em) {
111 Device device = new Device()
113 .ctime(DEFAULT_CTIME)
114 .model(DEFAULT_MODEL)
115 .location(DEFAULT_LOCATION)
117 .descr(DEFAULT_DESCR)
118 .locked(DEFAULT_LOCKED);
123 public void initTest() {
124 device = createEntity(em);
129 public void createDevice() throws Exception {
130 int databaseSizeBeforeCreate = deviceRepository.findAll().size();
133 restDeviceMockMvc.perform(post("/api/devices")
134 .contentType(TestUtil.APPLICATION_JSON_UTF8)
135 .content(TestUtil.convertObjectToJsonBytes(device)))
136 .andExpect(status().isCreated());
138 // Validate the Device in the database
139 List<Device> deviceList = deviceRepository.findAll();
140 assertThat(deviceList).hasSize(databaseSizeBeforeCreate + 1);
141 Device testDevice = deviceList.get(deviceList.size() - 1);
142 assertThat(testDevice.getDuid()).isEqualTo(DEFAULT_DUID);
143 assertThat(testDevice.getCtime()).isEqualTo(DEFAULT_CTIME);
144 assertThat(testDevice.getModel()).isEqualTo(DEFAULT_MODEL);
145 assertThat(testDevice.getLocation()).isEqualTo(DEFAULT_LOCATION);
146 assertThat(testDevice.getSn()).isEqualTo(DEFAULT_SN);
147 assertThat(testDevice.getDescr()).isEqualTo(DEFAULT_DESCR);
148 assertThat(testDevice.getLocked()).isEqualTo(DEFAULT_LOCKED);
153 public void createDeviceWithExistingId() throws Exception {
154 int databaseSizeBeforeCreate = deviceRepository.findAll().size();
156 // Create the Device with an existing ID
159 // An entity with an existing ID cannot be created, so this API call must fail
160 restDeviceMockMvc.perform(post("/api/devices")
161 .contentType(TestUtil.APPLICATION_JSON_UTF8)
162 .content(TestUtil.convertObjectToJsonBytes(device)))
163 .andExpect(status().isBadRequest());
165 // Validate the Device in the database
166 List<Device> deviceList = deviceRepository.findAll();
167 assertThat(deviceList).hasSize(databaseSizeBeforeCreate);
172 public void getAllDevices() throws Exception {
173 // Initialize the database
174 deviceRepository.saveAndFlush(device);
176 // Get all the deviceList
177 restDeviceMockMvc.perform(get("/api/devices?sort=id,desc"))
178 .andExpect(status().isOk())
179 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
180 .andExpect(jsonPath("$.[*].id").value(hasItem(device.getId().intValue())))
181 .andExpect(jsonPath("$.[*].duid").value(hasItem(DEFAULT_DUID.toString())))
182 .andExpect(jsonPath("$.[*].ctime").value(hasItem(DEFAULT_CTIME.toString())))
183 .andExpect(jsonPath("$.[*].model").value(hasItem(DEFAULT_MODEL.toString())))
184 .andExpect(jsonPath("$.[*].location").value(hasItem(DEFAULT_LOCATION.toString())))
185 .andExpect(jsonPath("$.[*].sn").value(hasItem(DEFAULT_SN.toString())))
186 .andExpect(jsonPath("$.[*].descr").value(hasItem(DEFAULT_DESCR.toString())))
187 .andExpect(jsonPath("$.[*].locked").value(hasItem(DEFAULT_LOCKED.toString())));
192 public void getDevice() throws Exception {
193 // Initialize the database
194 deviceRepository.saveAndFlush(device);
197 restDeviceMockMvc.perform(get("/api/devices/{id}", device.getId()))
198 .andExpect(status().isOk())
199 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
200 .andExpect(jsonPath("$.id").value(device.getId().intValue()))
201 .andExpect(jsonPath("$.duid").value(DEFAULT_DUID.toString()))
202 .andExpect(jsonPath("$.ctime").value(DEFAULT_CTIME.toString()))
203 .andExpect(jsonPath("$.model").value(DEFAULT_MODEL.toString()))
204 .andExpect(jsonPath("$.location").value(DEFAULT_LOCATION.toString()))
205 .andExpect(jsonPath("$.sn").value(DEFAULT_SN.toString()))
206 .andExpect(jsonPath("$.descr").value(DEFAULT_DESCR.toString()))
207 .andExpect(jsonPath("$.locked").value(DEFAULT_LOCKED.toString()));
212 public void getNonExistingDevice() throws Exception {
214 restDeviceMockMvc.perform(get("/api/devices/{id}", Long.MAX_VALUE))
215 .andExpect(status().isNotFound());
220 public void updateDevice() throws Exception {
221 // Initialize the database
222 deviceService.save(device);
224 int databaseSizeBeforeUpdate = deviceRepository.findAll().size();
227 Device updatedDevice = deviceRepository.findOne(device.getId());
228 // Disconnect from session so that the updates on updatedDevice are not directly saved in db
229 em.detach(updatedDevice);
232 .ctime(UPDATED_CTIME)
233 .model(UPDATED_MODEL)
234 .location(UPDATED_LOCATION)
236 .descr(UPDATED_DESCR)
237 .locked(UPDATED_LOCKED);
239 restDeviceMockMvc.perform(put("/api/devices")
240 .contentType(TestUtil.APPLICATION_JSON_UTF8)
241 .content(TestUtil.convertObjectToJsonBytes(updatedDevice)))
242 .andExpect(status().isOk());
244 // Validate the Device in the database
245 List<Device> deviceList = deviceRepository.findAll();
246 assertThat(deviceList).hasSize(databaseSizeBeforeUpdate);
247 Device testDevice = deviceList.get(deviceList.size() - 1);
248 assertThat(testDevice.getDuid()).isEqualTo(UPDATED_DUID);
249 assertThat(testDevice.getCtime()).isEqualTo(UPDATED_CTIME);
250 assertThat(testDevice.getModel()).isEqualTo(UPDATED_MODEL);
251 assertThat(testDevice.getLocation()).isEqualTo(UPDATED_LOCATION);
252 assertThat(testDevice.getSn()).isEqualTo(UPDATED_SN);
253 assertThat(testDevice.getDescr()).isEqualTo(UPDATED_DESCR);
254 assertThat(testDevice.getLocked()).isEqualTo(UPDATED_LOCKED);
259 public void updateNonExistingDevice() throws Exception {
260 int databaseSizeBeforeUpdate = deviceRepository.findAll().size();
264 // If the entity doesn't have an ID, it will be created instead of just being updated
265 restDeviceMockMvc.perform(put("/api/devices")
266 .contentType(TestUtil.APPLICATION_JSON_UTF8)
267 .content(TestUtil.convertObjectToJsonBytes(device)))
268 .andExpect(status().isCreated());
270 // Validate the Device in the database
271 List<Device> deviceList = deviceRepository.findAll();
272 assertThat(deviceList).hasSize(databaseSizeBeforeUpdate + 1);
277 public void deleteDevice() throws Exception {
278 // Initialize the database
279 deviceService.save(device);
281 int databaseSizeBeforeDelete = deviceRepository.findAll().size();
284 restDeviceMockMvc.perform(delete("/api/devices/{id}", device.getId())
285 .accept(TestUtil.APPLICATION_JSON_UTF8))
286 .andExpect(status().isOk());
288 // Validate the database is empty
289 List<Device> deviceList = deviceRepository.findAll();
290 assertThat(deviceList).hasSize(databaseSizeBeforeDelete - 1);
295 public void equalsVerifier() throws Exception {
296 TestUtil.equalsVerifier(Device.class);
297 Device device1 = new Device();
299 Device device2 = new Device();
300 device2.setId(device1.getId());
301 assertThat(device1).isEqualTo(device2);
303 assertThat(device1).isNotEqualTo(device2);
305 assertThat(device1).isNotEqualTo(device2);