SECARSP-268 *Implement device policies state request mechanism: update data type...
[platform/core/security/suspicious-activity-monitor.git] / server / samserver / src / test / java / com / samsung / samserver / web / rest / controller / impl / RestDeviceTest.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.controller.impl;
7
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;
24 import org.junit.*;
25 import org.slf4j.*;
26 import java.util.*;
27
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.*;
34
35 /**
36  * Test class for the Device REST controller.
37  *
38  * @see RestDevice
39  */
40 @RunWith(SpringRunner.class)
41 @SpringBootTest(classes = SamserverApp.class)
42 public class RestDeviceTest {
43
44     @Autowired
45     RestDevice restDevice;
46
47     @Autowired
48     private RestDashboard restDashboard;
49
50     @Autowired
51     private DeviceTypeService deviceTypeService;
52
53     @Autowired
54     private DeviceService deviceService;
55
56     @Autowired
57     private ExceptionTranslator exceptionTranslator;
58
59     private MockMvc mockMvcDevice;
60     private MockMvc mockMvcDashboard;
61
62     private final Logger log = LoggerFactory.getLogger(RestDeviceTest.class);
63
64     @Before
65     public void setUp() {
66         this.mockMvcDevice = MockMvcBuilders.standaloneSetup(restDevice)
67                 .setControllerAdvice(exceptionTranslator)
68                 .setConversionService(createFormattingConversionService())
69                 .build();
70         this.mockMvcDashboard = MockMvcBuilders.standaloneSetup(restDashboard)
71                 .setControllerAdvice(exceptionTranslator)
72                 .setConversionService(createFormattingConversionService())
73                 .build();
74     }
75
76     @Test
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())
82                 .andDo(mvcResult -> {
83                     String duid = mvcResult.getResponse().getContentAsString();
84                     log.info("testRegisterDevice: duid = "+duid);
85                     assertThat(deviceService.findOne(duid).isPresent()).isTrue();
86                 });
87     }
88
89     //@Test
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())
95                 .andReturn()
96                 .getResponse().getContentAsString();
97
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)))
104         ;
105     }
106
107     @Test
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());
116
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)))
128         ;
129     }
130
131     //@Test
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())
137                 .andReturn()
138                 .getResponse().getContentAsString();
139
140         mockMvcDevice.perform(post("/api/device-service/send-data")
141                 .contentType(TestUtil.APPLICATION_JSON_UTF8)
142                 .param("duid", duid)
143                 .content(TestUtil.convertObjectToJsonBytes(createRandomData())))
144                 .andExpect(status().isOk());
145     }
146
147     @Test
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());
156
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)))
163                 .andReturn()
164                 .getResponse().getContentAsString();
165
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);
170
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();
177                 });
178     }
179
180     @Test
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());
189
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)))
196                 .andReturn()
197                 .getResponse().getContentAsString();
198
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);
203
204         mockMvcDevice.perform(post(curi))
205                 .andExpect(status().isOk());
206
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)))
213         ;
214     }
215
216 }