[NO-TICKET] *expand device registration info
[platform/core/security/suspicious-activity-monitor.git] / server / src / test / java / com / samsung / samserver / web / rest / crud / DeviceResourceTest.java
1 /*
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.
5  */
6 package com.samsung.samserver.web.rest.crud;
7
8 import com.samsung.samserver.SamserverApp;
9
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;
15
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;
29
30 import javax.persistence.EntityManager;
31 import java.time.Instant;
32 import java.time.temporal.ChronoUnit;
33 import java.util.List;
34
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.*;
40
41 /**
42  * Test class for the DeviceResource REST controller.
43  *
44  * @see DeviceResource
45  */
46 @RunWith(SpringRunner.class)
47 @SpringBootTest(classes = SamserverApp.class)
48 public class DeviceResourceTest {
49
50     private static final String DEFAULT_DUID = "AAAAAAAAAA";
51     private static final String UPDATED_DUID = "BBBBBBBBBB";
52
53     private static final Instant DEFAULT_CTIME = Instant.ofEpochMilli(0L);
54     private static final Instant UPDATED_CTIME = Instant.now().truncatedTo(ChronoUnit.MILLIS);
55
56     private static final String DEFAULT_MODEL = "AAAAAAAAAA";
57     private static final String UPDATED_MODEL = "BBBBBBBBBB";
58
59     private static final String DEFAULT_LOCATION = "AAAAAAAAAA";
60     private static final String UPDATED_LOCATION = "BBBBBBBBBB";
61
62     private static final String DEFAULT_SN = "AAAAAAAAAA";
63     private static final String UPDATED_SN = "BBBBBBBBBB";
64
65     private static final String DEFAULT_DESCR = "AAAAAAAAAA";
66     private static final String UPDATED_DESCR = "BBBBBBBBBB";
67
68     private static final String DEFAULT_LOCKED = "AAAAAAAAAA";
69     private static final String UPDATED_LOCKED = "BBBBBBBBBB";
70
71     @Autowired
72     private DeviceRepository deviceRepository;
73
74     @Autowired
75     private DeviceService deviceService;
76
77     @Autowired
78     private MappingJackson2HttpMessageConverter jacksonMessageConverter;
79
80     @Autowired
81     private PageableHandlerMethodArgumentResolver pageableArgumentResolver;
82
83     @Autowired
84     private ExceptionTranslator exceptionTranslator;
85
86     @Autowired
87     private EntityManager em;
88
89     private MockMvc restDeviceMockMvc;
90
91     private Device device;
92
93     @Before
94     public void setup() {
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();
102     }
103
104     /**
105      * Create an entity for this test.
106      *
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.
109      */
110     public static Device createEntity(EntityManager em) {
111         Device device = new Device()
112             .duid(DEFAULT_DUID)
113             .ctime(DEFAULT_CTIME)
114             .model(DEFAULT_MODEL)
115             .location(DEFAULT_LOCATION)
116             .sn(DEFAULT_SN)
117             .descr(DEFAULT_DESCR)
118             .locked(DEFAULT_LOCKED);
119         return device;
120     }
121
122     @Before
123     public void initTest() {
124         device = createEntity(em);
125     }
126
127     @Test
128     @Transactional
129     public void createDevice() throws Exception {
130         int databaseSizeBeforeCreate = deviceRepository.findAll().size();
131
132         // Create the Device
133         restDeviceMockMvc.perform(post("/api/devices")
134             .contentType(TestUtil.APPLICATION_JSON_UTF8)
135             .content(TestUtil.convertObjectToJsonBytes(device)))
136             .andExpect(status().isCreated());
137
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);
149     }
150
151     @Test
152     @Transactional
153     public void createDeviceWithExistingId() throws Exception {
154         int databaseSizeBeforeCreate = deviceRepository.findAll().size();
155
156         // Create the Device with an existing ID
157         device.setId(1L);
158
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());
164
165         // Validate the Device in the database
166         List<Device> deviceList = deviceRepository.findAll();
167         assertThat(deviceList).hasSize(databaseSizeBeforeCreate);
168     }
169
170     @Test
171     @Transactional
172     public void getAllDevices() throws Exception {
173         // Initialize the database
174         deviceRepository.saveAndFlush(device);
175
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())));
188     }
189
190     @Test
191     @Transactional
192     public void getDevice() throws Exception {
193         // Initialize the database
194         deviceRepository.saveAndFlush(device);
195
196         // Get the 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()));
208     }
209
210     @Test
211     @Transactional
212     public void getNonExistingDevice() throws Exception {
213         // Get the device
214         restDeviceMockMvc.perform(get("/api/devices/{id}", Long.MAX_VALUE))
215             .andExpect(status().isNotFound());
216     }
217
218     @Test
219     @Transactional
220     public void updateDevice() throws Exception {
221         // Initialize the database
222         deviceService.save(device);
223
224         int databaseSizeBeforeUpdate = deviceRepository.findAll().size();
225
226         // Update the device
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);
230         updatedDevice
231             .duid(UPDATED_DUID)
232             .ctime(UPDATED_CTIME)
233             .model(UPDATED_MODEL)
234             .location(UPDATED_LOCATION)
235             .sn(UPDATED_SN)
236             .descr(UPDATED_DESCR)
237             .locked(UPDATED_LOCKED);
238
239         restDeviceMockMvc.perform(put("/api/devices")
240             .contentType(TestUtil.APPLICATION_JSON_UTF8)
241             .content(TestUtil.convertObjectToJsonBytes(updatedDevice)))
242             .andExpect(status().isOk());
243
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);
255     }
256
257     @Test
258     @Transactional
259     public void updateNonExistingDevice() throws Exception {
260         int databaseSizeBeforeUpdate = deviceRepository.findAll().size();
261
262         // Create the Device
263
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());
269
270         // Validate the Device in the database
271         List<Device> deviceList = deviceRepository.findAll();
272         assertThat(deviceList).hasSize(databaseSizeBeforeUpdate + 1);
273     }
274
275     @Test
276     @Transactional
277     public void deleteDevice() throws Exception {
278         // Initialize the database
279         deviceService.save(device);
280
281         int databaseSizeBeforeDelete = deviceRepository.findAll().size();
282
283         // Get the device
284         restDeviceMockMvc.perform(delete("/api/devices/{id}", device.getId())
285             .accept(TestUtil.APPLICATION_JSON_UTF8))
286             .andExpect(status().isOk());
287
288         // Validate the database is empty
289         List<Device> deviceList = deviceRepository.findAll();
290         assertThat(deviceList).hasSize(databaseSizeBeforeDelete - 1);
291     }
292
293     @Test
294     @Transactional
295     public void equalsVerifier() throws Exception {
296         TestUtil.equalsVerifier(Device.class);
297         Device device1 = new Device();
298         device1.setId(1L);
299         Device device2 = new Device();
300         device2.setId(device1.getId());
301         assertThat(device1).isEqualTo(device2);
302         device2.setId(2L);
303         assertThat(device1).isNotEqualTo(device2);
304         device1.setId(null);
305         assertThat(device1).isNotEqualTo(device2);
306     }
307 }