refactor list doors in json
This commit is contained in:
1 parent
06621727e8
commit
0a27008cfd
3 files changed
+4
-38
No files matched your search
@@ -3,12 +3,10 @@ package application.controllers;
|
||||
import application.model.Door;
|
||||
import application.model.DoorMessage;
|
||||
import application.repositories.DoorRepository;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
@@ -30,15 +28,8 @@ public class DoorController {
|
||||
@RequestMapping
|
||||
public
|
||||
@ResponseBody
|
||||
String listDoorsJson(ModelMap model) throws JSONException {
|
||||
JSONArray doorArray = new JSONArray();
|
||||
for (Door door : doorRepository.findAll()) {
|
||||
JSONObject doorJSON = new JSONObject();
|
||||
doorJSON.put("doorId", door.getDoorId());
|
||||
doorJSON.put("message", door.getDoorMessage());
|
||||
doorArray.put(doorJSON);
|
||||
}
|
||||
return doorArray.toString();
|
||||
Iterable<Door> listDoorsJson() throws JSONException {
|
||||
return doorRepository.findAll();
|
||||
}
|
||||
|
||||
@RequestMapping("/{doorId}/message")
|
||||
|
||||
@@ -58,7 +58,6 @@ public class EventController {
|
||||
@RequestMapping(value = "/eventEntry/getAll")
|
||||
@ResponseBody
|
||||
public Iterable<EntryEvent> getAllEntry() throws JSONException {
|
||||
Iterable<EntryEvent> a = entryEventRepository.findAll();
|
||||
return entryEventRepository.findAll();
|
||||
}
|
||||
|
||||
|
||||
@@ -6,38 +6,14 @@ package test;
|
||||
import application.controllers.DoorController;
|
||||
import application.model.Door;
|
||||
import application.repositories.DoorRepository;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.ui.ModelMap;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static java.util.Arrays.*;
|
||||
|
||||
import java.util.List;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class DoorControllerTest {
|
||||
@Test
|
||||
public void listDoorsJsonTest(){
|
||||
DoorController doorController=new DoorController();
|
||||
ModelMap model = new ModelMap();
|
||||
String expectedDoorList="[{},{},{}]";
|
||||
List<Door> doorList = asList(new Door(), new Door(), new Door());
|
||||
|
||||
DoorRepository doorRepository= mock(DoorRepository.class);
|
||||
when(doorRepository.findAll()).thenReturn(doorList);
|
||||
String result=null;
|
||||
doorController.setDoorRepository(doorRepository);
|
||||
try {
|
||||
result= doorController.listDoorsJson(model);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
assertEquals(expectedDoorList,result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getUserMessageTest(){
|
||||
|
||||
Reference in new issue
Block a user