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.controller.impl;
8 import com.fasterxml.jackson.core.type.TypeReference;
9 import com.fasterxml.jackson.databind.ObjectMapper;
10 import com.samsung.samserver.SamserverApp;
11 import com.samsung.samserver.domain.Device;
12 import com.samsung.samserver.service.DeviceService;
13 import com.samsung.samserver.service.DeviceTypeService;
14 import com.samsung.samserver.web.rest.TestUtil;
15 import com.samsung.samserver.web.rest.errors.ExceptionTranslator;
16 import com.samsung.samserver.web.rest.service.vm.*;
17 import org.hamcrest.Matchers;
18 import org.springframework.beans.factory.annotation.Autowired;
19 import org.springframework.boot.test.context.SpringBootTest;
20 import org.springframework.test.context.junit4.SpringRunner;
21 import org.springframework.test.web.servlet.MockMvc;
22 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
23 import org.junit.runner.RunWith;
28 import static com.samsung.samserver.web.rest.TestUtil.createFormattingConversionService;
29 import static org.assertj.core.api.Assertions.assertThat;
30 import static org.hamcrest.Matchers.*;
31 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
32 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
33 import static com.samsung.samserver.web.rest.controller.impl.SampleObject.*;
36 * Test class for the Device REST controller.
40 @RunWith(SpringRunner.class)
41 @SpringBootTest(classes = SamserverApp.class)
42 public class RestDeviceTest {
45 RestDevice restDevice;
48 private RestDashboard restDashboard;
51 private DeviceTypeService deviceTypeService;
54 private DeviceService deviceService;
57 private ExceptionTranslator exceptionTranslator;
59 private MockMvc mockMvcDevice;
60 private MockMvc mockMvcDashboard;
62 private final Logger log = LoggerFactory.getLogger(RestDeviceTest.class);
66 this.mockMvcDevice = MockMvcBuilders.standaloneSetup(restDevice)
67 .setControllerAdvice(exceptionTranslator)
68 .setConversionService(createFormattingConversionService())
70 this.mockMvcDashboard = MockMvcBuilders.standaloneSetup(restDashboard)
71 .setControllerAdvice(exceptionTranslator)
72 .setConversionService(createFormattingConversionService())
77 public void testRegisterDevice() throws Exception {
78 mockMvcDevice.perform(post("/api/device-service/register-device")
79 .contentType(TestUtil.APPLICATION_JSON_UTF8)
80 .content(TestUtil.convertObjectToJsonBytes(createRandomDInfo())))
81 .andExpect(status().isOk())
83 String duid = mvcResult.getResponse().getContentAsString();
84 log.info("testRegisterDevice: duid = "+duid);
85 assertThat(deviceService.findOne(duid).isPresent()).isTrue();
90 public void testGetUpdatesNotModified() throws Exception {
91 String duid = mockMvcDevice.perform(post("/api/device-service/register-device")
92 .contentType(TestUtil.APPLICATION_JSON_UTF8)
93 .content(TestUtil.convertObjectToJsonBytes(createRandomDInfo())))
94 .andExpect(status().isOk())
96 .getResponse().getContentAsString();
98 mockMvcDevice.perform(get("/api/device-service/get-updates")
99 .contentType(TestUtil.APPLICATION_JSON_UTF8)
100 .param("duid", duid))
101 .andExpect(status().isNotModified())
102 .andExpect(content().contentType(TestUtil.APPLICATION_JSON_UTF8))
103 .andExpect(jsonPath("$", hasSize(0)))
108 public void testGetUpdates() throws Exception {
109 Device device = createRandomDevice();
110 deviceTypeService.save(device.getDtype());
111 deviceService.save(device);
112 mockMvcDashboard.perform(put("/dashboard/devices/update")
113 .contentType(TestUtil.APPLICATION_JSON_UTF8)
114 .content(TestUtil.convertObjectToJsonBytes(createUIDeviceUpdate(device.getId()))))
115 .andExpect(status().isOk());
117 mockMvcDevice.perform(get("/api/device-service/get-updates")
118 .contentType(TestUtil.APPLICATION_JSON_UTF8)
119 .param("duid", device.getDuid()))
120 .andExpect(status().isOk())
121 .andExpect(content().contentType(TestUtil.APPLICATION_JSON_UTF8))
122 .andExpect(jsonPath("$", hasSize(1)))
123 .andExpect(jsonPath("$[0].type", is("settings")))
124 .andExpect(jsonPath("$[0].uri", Matchers.isA(String.class)))
125 .andExpect(jsonPath("$[0].curi", Matchers.isA(String.class)))
126 .andExpect(jsonPath("$[0].descr", Matchers.isA(String.class)))
127 .andExpect(jsonPath("$[0].rdate", Matchers.isA(String.class)))
132 public void testSendData() throws Exception {
133 String duid = mockMvcDevice.perform(post("/api/device-service/register-device")
134 .contentType(TestUtil.APPLICATION_JSON_UTF8)
135 .content(TestUtil.convertObjectToJsonBytes(createRandomDInfo())))
136 .andExpect(status().isOk())
138 .getResponse().getContentAsString();
140 mockMvcDevice.perform(post("/api/device-service/send-data")
141 .contentType(TestUtil.APPLICATION_JSON_UTF8)
143 .content(TestUtil.convertObjectToJsonBytes(createRandomData())))
144 .andExpect(status().isOk());
148 public void testGetUData() throws Exception {
149 Device device = createRandomDevice();
150 deviceTypeService.save(device.getDtype());
151 deviceService.save(device);
152 mockMvcDashboard.perform(put("/dashboard/devices/update")
153 .contentType(TestUtil.APPLICATION_JSON_UTF8)
154 .content(TestUtil.convertObjectToJsonBytes(createUIDeviceUpdate(device.getId()))))
155 .andExpect(status().isOk());
157 String resp = mockMvcDevice.perform(get("/api/device-service/get-updates")
158 .contentType(TestUtil.APPLICATION_JSON_UTF8)
159 .param("duid", device.getDuid()))
160 .andExpect(status().isOk())
161 .andExpect(content().contentType(TestUtil.APPLICATION_JSON_UTF8))
162 .andExpect(jsonPath("$", hasSize(1)))
164 .getResponse().getContentAsString();
166 ObjectMapper mapper = new ObjectMapper();
167 List<UInfo> list = mapper.readValue(resp, new TypeReference<List<UInfo>>(){});
168 String uri = list.get(0).getUri();
169 log.info("testGetUData: "+uri);
171 mockMvcDevice.perform(get(uri))
172 .andExpect(status().isOk())
173 .andDo(mvcResult -> {
174 String data = mvcResult.getResponse().getContentAsString();
175 log.info("testGetUData: data = "+data);
176 assertThat(!data.isEmpty()).isTrue();
181 public void testSendDataConfirm() throws Exception {
182 Device device = createRandomDevice();
183 deviceTypeService.save(device.getDtype());
184 deviceService.save(device);
185 mockMvcDashboard.perform(put("/dashboard/devices/update")
186 .contentType(TestUtil.APPLICATION_JSON_UTF8)
187 .content(TestUtil.convertObjectToJsonBytes(createUIDeviceUpdate(device.getId()))))
188 .andExpect(status().isOk());
190 String resp = mockMvcDevice.perform(get("/api/device-service/get-updates")
191 .contentType(TestUtil.APPLICATION_JSON_UTF8)
192 .param("duid", device.getDuid()))
193 .andExpect(status().isOk())
194 .andExpect(content().contentType(TestUtil.APPLICATION_JSON_UTF8))
195 .andExpect(jsonPath("$", hasSize(1)))
197 .getResponse().getContentAsString();
199 ObjectMapper mapper = new ObjectMapper();
200 List<UInfo> list = mapper.readValue(resp, new TypeReference<List<UInfo>>(){});
201 String curi = list.get(0).getCuri();
202 log.info("testSendDataConfirm: "+curi);
204 mockMvcDevice.perform(post(curi))
205 .andExpect(status().isOk());
207 mockMvcDevice.perform(get("/api/device-service/get-updates")
208 .contentType(TestUtil.APPLICATION_JSON_UTF8)
209 .param("duid", device.getDuid()))
210 .andExpect(status().isNotModified())
211 .andExpect(content().contentType(TestUtil.APPLICATION_JSON_UTF8))
212 .andExpect(jsonPath("$", hasSize(0)))